--- libaitio/src/bpf.c 2013/06/25 19:28:48 1.1.2.13 +++ libaitio/src/bpf.c 2013/06/26 13:33:55 1.1.2.16 @@ -122,17 +122,6 @@ io_etherOpen(const char *csIface, int flags, int whdr, return -1; } - if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) { - LOGERR; - close(eth); - return -1; - } - if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) { - LOGERR; - close(eth); - return -1; - } - if (!zcbuf) { if (ioctl(eth, BIOCGBLEN, &n) == -1) { LOGERR; @@ -165,7 +154,7 @@ io_etherOpen(const char *csIface, int flags, int whdr, close(eth); return -1; } - if (ioctl(eth, BIOCSETZBUF, (struct bpf_zbuf*) *zcbuf) == -1) { + if (ioctl(eth, BIOCSETZBUF, *zcbuf) == -1) { LOGERR; io_etherClose(eth, zcbuf); return -1; @@ -189,6 +178,18 @@ io_etherOpen(const char *csIface, int flags, int whdr, return -1; } + n = 1; + if (whdr && ioctl(eth, BIOCSHDRCMPLT, &n) == -1) { + LOGERR; + io_etherClose(eth, zcbuf); + return -1; + } + if (ioctl(eth, BIOCIMMEDIATE, &n) == -1) { + LOGERR; + io_etherClose(eth, zcbuf); + return -1; + } + return eth; } @@ -230,11 +231,10 @@ chkZCbuf(struct bpf_zbuf_header * __restrict bzh) return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen)); } -static inline ssize_t -nextZCbuf(void ** __restrict zcache, struct bpf_zbuf * __restrict zbuf, - const void * __restrict buf) +static inline size_t +nextZCbuf(void ** __restrict zcache, struct bpf_zbuf * __restrict zbuf) { - ssize_t rlen = -1; + size_t rlen = 0; struct bpf_zbuf_header *bzh; if (!*zcache || *zcache == zbuf->bz_bufb) { @@ -242,21 +242,13 @@ nextZCbuf(void ** __restrict zcache, struct bpf_zbuf * if (chkZCbuf(bzh)) { rlen = atomic_load_acq_int(&bzh->bzh_kernel_len); *zcache = zbuf->bz_bufa; - if (buf) - buf = ((caddr_t) *zcache) + sizeof(struct bpf_zbuf_header); - ackZCbuf(bzh); - } else - io_SetErr(EAGAIN, "kernel owns the buffer"); + } } else if (*zcache == zbuf->bz_bufa) { bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb; if (chkZCbuf(bzh)) { rlen = atomic_load_acq_int(&bzh->bzh_kernel_len); *zcache = zbuf->bz_bufb; - if (buf) - buf = ((caddr_t) *zcache) + sizeof(struct bpf_zbuf_header); - ackZCbuf(bzh); - } else - io_SetErr(EAGAIN, "kernel owns the buffer"); + } } return rlen; @@ -276,7 +268,10 @@ ssize_t io_etherRecv(int eth, void * __restrict buf, size_t buflen, void * __restrict zcbuf) { ssize_t rlen = 0; - void **zcache = NULL; + void *zcache = NULL; +#ifdef __FreeBSD__ + struct bpf_zbuf_header *bzh; +#endif if (!buf || !buflen) { io_SetErr(EINVAL, "invalid arguments"); @@ -289,8 +284,14 @@ io_etherRecv(int eth, void * __restrict buf, size_t bu LOGERR; } else { #ifdef __FreeBSD__ - rlen = nextZCbuf(zcache, (struct bpf_zbuf*) zcbuf, buf); + rlen = (ssize_t) nextZCbuf(&zcache, (struct bpf_zbuf*) zcbuf); + if (rlen > 0) { + bzh = (struct bpf_zbuf_header*) zcache; + memcpy(buf, zcache + sizeof(struct bpf_zbuf_header), buflen); + ackZCbuf(bzh); + } #else + rlen = -1; io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported"); #endif }