diff options
author | 2020-06-24 22:03:40 +0000 | |
---|---|---|
committer | 2020-06-24 22:03:40 +0000 | |
commit | 3209772dfcc3950dd5df01bc44eebf75e637511e (patch) | |
tree | 674a3736a2e3e277e801c3c22c1430cb8a2d032f /sys/netinet | |
parent | First stab at making signal handling work. (diff) | |
download | wireguard-openbsd-3209772dfcc3950dd5df01bc44eebf75e637511e.tar.xz wireguard-openbsd-3209772dfcc3950dd5df01bc44eebf75e637511e.zip |
kernel: use gettime(9)/getuptime(9) in lieu of time_second(9)/time_uptime(9)
time_second(9) and time_uptime(9) are widely used in the kernel to
quickly get the system UTC or system uptime as a time_t. However,
time_t is 64-bit everywhere, so it is not generally safe to use them
on 32-bit platforms: you have a split-read problem if your hardware
cannot perform atomic 64-bit reads.
This patch replaces time_second(9) with gettime(9), a safer successor
interface, throughout the kernel. Similarly, time_uptime(9) is replaced
with getuptime(9).
There is a performance cost on 32-bit platforms in exchange for
eliminating the split-read problem: instead of two register reads you
now have a lockless read loop to pull the values from the timehands.
This is really not *too* bad in the grand scheme of things, but
compared to what we were doing before it is several times slower.
There is no performance cost on 64-bit (__LP64__) platforms.
With input from visa@, dlg@, and tedu@.
Several bugs squashed by visa@.
ok kettenis@
Diffstat (limited to 'sys/netinet')
-rw-r--r-- | sys/netinet/if_ether.c | 24 | ||||
-rw-r--r-- | sys/netinet/ip_ipsp.c | 6 | ||||
-rw-r--r-- | sys/netinet/ip_output.c | 4 | ||||
-rw-r--r-- | sys/netinet/ip_spd.c | 6 | ||||
-rw-r--r-- | sys/netinet/ipsec_input.c | 10 | ||||
-rw-r--r-- | sys/netinet/ipsec_output.c | 8 |
6 files changed, 29 insertions, 29 deletions
diff --git a/sys/netinet/if_ether.c b/sys/netinet/if_ether.c index f950dbebc33..fceeffc3e10 100644 --- a/sys/netinet/if_ether.c +++ b/sys/netinet/if_ether.c @@ -1,4 +1,4 @@ -/* $OpenBSD: if_ether.c,v 1.242 2019/11/07 11:23:23 krw Exp $ */ +/* $OpenBSD: if_ether.c,v 1.243 2020/06/24 22:03:43 cheloha Exp $ */ /* $NetBSD: if_ether.c,v 1.31 1996/05/11 12:59:58 mycroft Exp $ */ /* @@ -120,7 +120,7 @@ arptimer(void *arg) LIST_FOREACH_SAFE(la, &arp_list, la_list, nla) { struct rtentry *rt = la->la_rt; - if (rt->rt_expire && rt->rt_expire < time_uptime) + if (rt->rt_expire && rt->rt_expire < getuptime()) arptfree(rt); /* timer has expired; clear */ } NET_UNLOCK(); @@ -192,7 +192,7 @@ arp_rtrequest(struct ifnet *ifp, int req, struct rtentry *rt) la->la_rt = rt; rt->rt_flags |= RTF_LLINFO; if ((rt->rt_flags & RTF_LOCAL) == 0) - rt->rt_expire = time_uptime; + rt->rt_expire = getuptime(); LIST_INSERT_HEAD(&arp_list, la, la_list); break; @@ -320,7 +320,7 @@ arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, rt = rt_getll(rt0); if (ISSET(rt->rt_flags, RTF_REJECT) && - (rt->rt_expire == 0 || rt->rt_expire > time_uptime )) { + (rt->rt_expire == 0 || rt->rt_expire > getuptime() )) { m_freem(m); return (rt == rt0 ? EHOSTDOWN : EHOSTUNREACH); } @@ -348,15 +348,15 @@ arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, * Check the address family and length is valid, the address * is resolved; otherwise, try to resolve. */ - if ((rt->rt_expire == 0 || rt->rt_expire > time_uptime) && + if ((rt->rt_expire == 0 || rt->rt_expire > getuptime()) && sdl->sdl_family == AF_LINK && sdl->sdl_alen != 0) { memcpy(desten, LLADDR(sdl), sdl->sdl_alen); /* refresh ARP entry when timeout gets close */ if (rt->rt_expire != 0 && - rt->rt_expire - arpt_keep / 8 < time_uptime && - la->la_refreshed + 30 < time_uptime) { - la->la_refreshed = time_uptime; + rt->rt_expire - arpt_keep / 8 < getuptime() && + la->la_refreshed + 30 < getuptime()) { + la->la_refreshed = getuptime(); arprequest(ifp, &satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr, &satosin(dst)->sin_addr.s_addr, @@ -396,13 +396,13 @@ arpresolve(struct ifnet *ifp, struct rtentry *rt0, struct mbuf *m, /* This should never happen. (Should it? -gwr) */ printf("%s: unresolved and rt_expire == 0\n", __func__); /* Set expiration time to now (expired). */ - rt->rt_expire = time_uptime; + rt->rt_expire = getuptime(); } #endif if (rt->rt_expire) { rt->rt_flags &= ~RTF_REJECT; - if (la->la_asked == 0 || rt->rt_expire != time_uptime) { - rt->rt_expire = time_uptime; + if (la->la_asked == 0 || rt->rt_expire != getuptime()) { + rt->rt_expire = getuptime(); if (la->la_asked++ < arp_maxtries) arprequest(ifp, &satosin(rt->rt_ifa->ifa_addr)->sin_addr.s_addr, @@ -661,7 +661,7 @@ arpcache(struct ifnet *ifp, struct ether_arp *ea, struct rtentry *rt) sdl->sdl_alen = sizeof(ea->arp_sha); memcpy(LLADDR(sdl), ea->arp_sha, sizeof(ea->arp_sha)); if (rt->rt_expire) - rt->rt_expire = time_uptime + arpt_keep; + rt->rt_expire = getuptime() + arpt_keep; rt->rt_flags &= ~RTF_REJECT; /* Notify userland that an ARP resolution has been done. */ diff --git a/sys/netinet/ip_ipsp.c b/sys/netinet/ip_ipsp.c index 8405bb5c18f..1755056a647 100644 --- a/sys/netinet/ip_ipsp.c +++ b/sys/netinet/ip_ipsp.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_ipsp.c,v 1.235 2020/04/23 19:38:08 tobhe Exp $ */ +/* $OpenBSD: ip_ipsp.c,v 1.236 2020/06/24 22:03:43 cheloha Exp $ */ /* * The authors of this code are John Ioannidis (ji@tla.org), * Angelos D. Keromytis (kermit@csd.uch.gr), @@ -708,7 +708,7 @@ puttdb(struct tdb *tdbp) ipsecstat_inc(ipsec_tunnels); #endif /* IPSEC */ - ipsec_last_added = time_uptime; + ipsec_last_added = getuptime(); } void @@ -804,7 +804,7 @@ tdb_alloc(u_int rdomain) TAILQ_INIT(&tdbp->tdb_policy_head); /* Record establishment time. */ - tdbp->tdb_established = time_second; + tdbp->tdb_established = gettime(); /* Save routing domain */ tdbp->tdb_rdomain = rdomain; diff --git a/sys/netinet/ip_output.c b/sys/netinet/ip_output.c index 2fca6974b35..788f47da48d 100644 --- a/sys/netinet/ip_output.c +++ b/sys/netinet/ip_output.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_output.c,v 1.356 2020/03/06 10:40:13 tobhe Exp $ */ +/* $OpenBSD: ip_output.c,v 1.357 2020/06/24 22:03:43 cheloha Exp $ */ /* $NetBSD: ip_output.c,v 1.28 1996/02/13 23:43:07 christos Exp $ */ /* @@ -589,7 +589,7 @@ ip_output_ipsec_send(struct tdb *tdb, struct mbuf *m, struct route *ro, int fwd) ip = mtod(m, struct ip *); if (ip_mtudisc && (ip->ip_off & htons(IP_DF)) && tdb->tdb_mtu && ntohs(ip->ip_len) > tdb->tdb_mtu && - tdb->tdb_mtutimeout > time_second) { + tdb->tdb_mtutimeout > gettime()) { struct rtentry *rt = NULL; int rt_mtucloned = 0; int transportmode = 0; diff --git a/sys/netinet/ip_spd.c b/sys/netinet/ip_spd.c index 727cad81994..9487ee4893d 100644 --- a/sys/netinet/ip_spd.c +++ b/sys/netinet/ip_spd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ip_spd.c,v 1.101 2019/12/10 17:58:54 tobhe Exp $ */ +/* $OpenBSD: ip_spd.c,v 1.102 2020/06/24 22:03:43 cheloha Exp $ */ /* * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) * @@ -438,7 +438,7 @@ ipsp_spd_lookup(struct mbuf *m, int af, int hlen, int *error, int direction, if (ipo->ipo_last_searched <= ipsec_last_added) { /* "Touch" the entry. */ if (dignore == 0) - ipo->ipo_last_searched = time_uptime; + ipo->ipo_last_searched = getuptime(); /* Find an appropriate SA from the existing ones. */ ipo->ipo_tdb = @@ -560,7 +560,7 @@ ipsp_spd_lookup(struct mbuf *m, int af, int hlen, int *error, int direction, /* Find whether there exists an appropriate SA. */ if (ipo->ipo_last_searched <= ipsec_last_added) { if (dignore == 0) - ipo->ipo_last_searched = time_uptime; + ipo->ipo_last_searched = getuptime(); ipo->ipo_tdb = gettdbbysrc(rdomain, diff --git a/sys/netinet/ipsec_input.c b/sys/netinet/ipsec_input.c index 7303c3cb303..050dc7b1128 100644 --- a/sys/netinet/ipsec_input.c +++ b/sys/netinet/ipsec_input.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ipsec_input.c,v 1.170 2020/04/23 19:38:08 tobhe Exp $ */ +/* $OpenBSD: ipsec_input.c,v 1.171 2020/06/24 22:03:43 cheloha Exp $ */ /* * The authors of this code are John Ioannidis (ji@tla.org), * Angelos D. Keromytis (kermit@csd.uch.gr) and @@ -316,7 +316,7 @@ ipsec_common_input(struct mbuf *m, int skip, int protoff, int af, int sproto, /* Register first use, setup expiration timer. */ if (tdbp->tdb_first_use == 0) { - tdbp->tdb_first_use = time_second; + tdbp->tdb_first_use = gettime(); if (tdbp->tdb_flags & TDBF_FIRSTUSE) timeout_add_sec(&tdbp->tdb_first_tmo, tdbp->tdb_exp_first_use); @@ -453,7 +453,7 @@ ipsec_common_input_cb(struct mbuf *m, struct tdb *tdbp, int skip, int protoff) af = tdbp->tdb_dst.sa.sa_family; sproto = tdbp->tdb_sproto; - tdbp->tdb_last_used = time_second; + tdbp->tdb_last_used = gettime(); /* Sanity check */ if (m == NULL) { @@ -984,7 +984,7 @@ ipsec_common_ctlinput(u_int rdomain, int cmd, struct sockaddr *sa, /* Store adjusted MTU in tdb */ tdbp->tdb_mtu = mtu; - tdbp->tdb_mtutimeout = time_second + + tdbp->tdb_mtutimeout = gettime() + ip_mtudisc_timeout; DPRINTF(("%s: spi %08x mtu %d adjust %ld\n", __func__, ntohl(tdbp->tdb_spi), tdbp->tdb_mtu, @@ -1039,7 +1039,7 @@ udpencap_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v) if ((adjust = ipsec_hdrsz(tdbp)) != -1) { /* Store adjusted MTU in tdb */ tdbp->tdb_mtu = mtu - adjust; - tdbp->tdb_mtutimeout = time_second + + tdbp->tdb_mtutimeout = gettime() + ip_mtudisc_timeout; DPRINTF(("%s: spi %08x mtu %d adjust %ld\n", __func__, diff --git a/sys/netinet/ipsec_output.c b/sys/netinet/ipsec_output.c index c5a13312f67..ed4551d1f47 100644 --- a/sys/netinet/ipsec_output.c +++ b/sys/netinet/ipsec_output.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ipsec_output.c,v 1.76 2020/04/23 19:38:09 tobhe Exp $ */ +/* $OpenBSD: ipsec_output.c,v 1.77 2020/06/24 22:03:43 cheloha Exp $ */ /* * The author of this code is Angelos D. Keromytis (angelos@cis.upenn.edu) * @@ -135,7 +135,7 @@ ipsp_process_packet(struct mbuf *m, struct tdb *tdb, int af, int tunalready) * Register first use if applicable, setup relevant expiration timer. */ if (tdb->tdb_first_use == 0) { - tdb->tdb_first_use = time_second; + tdb->tdb_first_use = gettime(); if (tdb->tdb_flags & TDBF_FIRSTUSE) timeout_add_sec(&tdb->tdb_first_tmo, tdb->tdb_exp_first_use); @@ -474,7 +474,7 @@ ipsp_process_done(struct mbuf *m, struct tdb *tdb) struct m_tag *mtag; int roff, error; - tdb->tdb_last_used = time_second; + tdb->tdb_last_used = gettime(); if ((tdb->tdb_flags & TDBF_UDPENCAP) != 0) { struct mbuf *mi; @@ -698,7 +698,7 @@ ipsec_adjust_mtu(struct mbuf *m, u_int32_t mtu) mtu -= adjust; tdbp->tdb_mtu = mtu; - tdbp->tdb_mtutimeout = time_second + ip_mtudisc_timeout; + tdbp->tdb_mtutimeout = gettime() + ip_mtudisc_timeout; DPRINTF(("ipsec_adjust_mtu: " "spi %08x mtu %d adjust %ld mbuf %p\n", ntohl(tdbp->tdb_spi), tdbp->tdb_mtu, |