diff options
author | cheloha <cheloha@cvs.openbsd.org> | 2021-01-02 02:46:07 +0000 |
---|---|---|
committer | cheloha <cheloha@cvs.openbsd.org> | 2021-01-02 02:46:07 +0000 |
commit | e57558ab4cb74767a182488122551f5f71e6be80 (patch) | |
tree | 67c6502ca0bec7bb1e22cb6165b22fa27c1f12cf | |
parent | b8c52e8c46e4752bc2d9017e0d5adf491f4e6f5a (diff) |
bpf(4): remove ticks
Change bd_rtout to a uint64_t of nanoseconds. Update the code in
bpfioctl() and bpfread() accordingly.
Add a local copy of nsecuptime() to make the diff smaller. This will
need to move to kern_tc.c if/when we have another user elsewhere in
the kernel.
Prompted by mpi@. With input from dlg@.
ok dlg@ mpi@ visa@
-rw-r--r-- | sys/net/bpf.c | 55 | ||||
-rw-r--r-- | sys/net/bpfdesc.h | 4 |
2 files changed, 34 insertions, 25 deletions
diff --git a/sys/net/bpf.c b/sys/net/bpf.c index 1ac05458f60..fe7ca2e6a01 100644 --- a/sys/net/bpf.c +++ b/sys/net/bpf.c @@ -1,4 +1,4 @@ -/* $OpenBSD: bpf.c,v 1.199 2020/12/26 16:30:58 cheloha Exp $ */ +/* $OpenBSD: bpf.c,v 1.200 2021/01/02 02:46:06 cheloha Exp $ */ /* $NetBSD: bpf.c,v 1.33 1997/02/21 23:59:35 thorpej Exp $ */ /* @@ -60,6 +60,7 @@ #include <sys/selinfo.h> #include <sys/sigio.h> #include <sys/task.h> +#include <sys/time.h> #include <net/if.h> #include <net/bpf.h> @@ -77,9 +78,6 @@ #define PRINET 26 /* interruptible */ -/* from kern/kern_clock.c; incremented each clock tick. */ -extern int ticks; - /* * The default read buffer size is patchable. */ @@ -422,15 +420,30 @@ bpfclose(dev_t dev, int flag, int mode, struct proc *p) (d)->bd_sbuf = (d)->bd_fbuf; \ (d)->bd_slen = 0; \ (d)->bd_fbuf = NULL; + +/* + * TODO Move nsecuptime() into kern_tc.c and document it when we have + * more users elsewhere in the kernel. + */ +static uint64_t +nsecuptime(void) +{ + struct timespec now; + + nanouptime(&now); + return TIMESPEC_TO_NSEC(&now); +} + /* * bpfread - read next chunk of packets from buffers */ int bpfread(dev_t dev, struct uio *uio, int ioflag) { + uint64_t end, now; struct bpf_d *d; caddr_t hbuf; - int end, error, hlen, nticks; + int error, hlen; KERNEL_ASSERT_LOCKED(); @@ -453,8 +466,12 @@ bpfread(dev_t dev, struct uio *uio, int ioflag) /* * If there's a timeout, mark when the read should end. */ - if (d->bd_rtout) - end = ticks + (int)d->bd_rtout; + if (d->bd_rtout != 0) { + now = nsecuptime(); + end = now + d->bd_rtout; + if (end < now) + end = UINT64_MAX; + } /* * If the hold buffer is empty, then do a timed sleep, which @@ -489,11 +506,11 @@ bpfread(dev_t dev, struct uio *uio, int ioflag) error = msleep_nsec(d, &d->bd_mtx, PRINET|PCATCH, "bpf", INFSLP); d->bd_nreaders--; - } else if ((nticks = end - ticks) > 0) { + } else if ((now = nsecuptime()) < end) { /* Read timeout has not expired yet. */ d->bd_nreaders++; - error = msleep(d, &d->bd_mtx, PRINET|PCATCH, "bpf", - nticks); + error = msleep_nsec(d, &d->bd_mtx, PRINET|PCATCH, + "bpf", end - now); d->bd_nreaders--; } else { /* Read timeout has expired. */ @@ -861,27 +878,19 @@ bpfioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) case BIOCSRTIMEOUT: { struct timeval *tv = (struct timeval *)addr; - u_long rtout; + uint64_t rtout; - /* Compute number of ticks. */ if (tv->tv_sec < 0 || !timerisvalid(tv)) { error = EINVAL; break; } - if (tv->tv_sec > INT_MAX / hz) { - error = EOVERFLOW; - break; - } - rtout = tv->tv_sec * hz; - if (tv->tv_usec / tick > INT_MAX - rtout) { + rtout = TIMEVAL_TO_NSEC(tv); + if (rtout > MAXTSLP) { error = EOVERFLOW; break; } - rtout += tv->tv_usec / tick; mtx_enter(&d->bd_mtx); d->bd_rtout = rtout; - if (d->bd_rtout == 0 && tv->tv_usec != 0) - d->bd_rtout = 1; mtx_leave(&d->bd_mtx); break; } @@ -893,9 +902,9 @@ bpfioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) { struct timeval *tv = (struct timeval *)addr; + memset(tv, 0, sizeof(*tv)); mtx_enter(&d->bd_mtx); - tv->tv_sec = d->bd_rtout / hz; - tv->tv_usec = (d->bd_rtout % hz) * tick; + NSEC_TO_TIMEVAL(d->bd_rtout, tv); mtx_leave(&d->bd_mtx); break; } diff --git a/sys/net/bpfdesc.h b/sys/net/bpfdesc.h index 065db296ecf..c2bfb50083e 100644 --- a/sys/net/bpfdesc.h +++ b/sys/net/bpfdesc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bpfdesc.h,v 1.43 2020/12/26 16:30:58 cheloha Exp $ */ +/* $OpenBSD: bpfdesc.h,v 1.44 2021/01/02 02:46:06 cheloha Exp $ */ /* $NetBSD: bpfdesc.h,v 1.11 1995/09/27 18:30:42 thorpej Exp $ */ /* @@ -78,7 +78,7 @@ struct bpf_d { int bd_in_uiomove; /* for debugging purpose */ struct bpf_if *bd_bif; /* interface descriptor */ - u_long bd_rtout; /* [m] Read timeout in 'ticks' */ + uint64_t bd_rtout; /* [m] Read timeout in nanoseconds */ u_long bd_nreaders; /* [m] # threads asleep in bpfread() */ int bd_rnonblock; /* true if nonblocking reads are set */ struct bpf_program_smr |