--- libaitio/src/bpf.c 2013/06/26 12:30:17 1.1.2.15 +++ libaitio/src/bpf.c 2013/06/26 13:33:55 1.1.2.16 @@ -154,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; @@ -231,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) { @@ -243,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 A"); + } } 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 B"); + } } return rlen; @@ -278,6 +269,9 @@ io_etherRecv(int eth, void * __restrict buf, size_t bu { ssize_t rlen = 0; void *zcache = NULL; +#ifdef __FreeBSD__ + struct bpf_zbuf_header *bzh; +#endif if (!buf || !buflen) { io_SetErr(EINVAL, "invalid arguments"); @@ -290,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 }