--- libaitio/src/bpf.c 2013/06/26 15:31:17 1.1.2.18 +++ libaitio/src/bpf.c 2013/06/26 16:34:28 1.1.2.20 @@ -231,26 +231,34 @@ chkZCbuf(struct bpf_zbuf_header *bzh) return (bzh->bzh_user_gen != atomic_load_acq_int(&bzh->bzh_kernel_gen)); } -static inline ssize_t -nextZCbuf(int eth, void ** __restrict zcache, struct bpf_zbuf * __restrict zbuf) +static ssize_t +nextZCbuf(int eth, struct bpf_zbuf * __restrict zbuf, void * __restrict buf, size_t buflen) { ssize_t rlen = 0; + struct bpf_zbuf bz; struct bpf_zbuf_header *bzh; + off_t pos = 0; - if (!*zcache || *zcache == zbuf->bz_bufb) { - bzh = (struct bpf_zbuf_header *) zbuf->bz_bufa; - if (chkZCbuf(bzh)) { - *zcache = zbuf->bz_bufa; - rlen = atomic_load_acq_int(&bzh->bzh_kernel_len); - } - } else if (*zcache == zbuf->bz_bufa) { - bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb; - if (chkZCbuf(bzh)) { - *zcache = zbuf->bz_bufb; - rlen = atomic_load_acq_int(&bzh->bzh_kernel_len); - } + bzh = (struct bpf_zbuf_header *) zbuf->bz_bufa; + if (chkZCbuf(bzh)) { + rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen); + memcpy(buf + pos, zbuf->bz_bufa + sizeof(struct bpf_zbuf_header), rlen); + ackZCbuf(bzh); + pos += rlen; } + bzh = (struct bpf_zbuf_header *) zbuf->bz_bufb; + if (chkZCbuf(bzh)) { + rlen = MIN(atomic_load_acq_int(&bzh->bzh_kernel_len), buflen); + memcpy(buf + pos, zbuf->bz_bufb + sizeof(struct bpf_zbuf_header), rlen); + ackZCbuf(bzh); + pos += rlen; + } + if (!pos) { + if ((rlen = ioctl(eth, BIOCROTZBUF, &bz)) == -1) + LOGERR; + } else + rlen = pos; return rlen; } #endif @@ -268,16 +276,6 @@ ssize_t io_etherRecv(int eth, void * __restrict buf, size_t buflen, void * __restrict zcbuf) { ssize_t rlen = 0; - void *zcache = NULL; -#ifdef __FreeBSD__ - struct bpf_zbuf bz; - struct bpf_zbuf_header *bzh; -#else - if (zcbuf) { - io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported"); - return -1; - } -#endif if (!buf || !buflen) { io_SetErr(EINVAL, "invalid arguments"); @@ -290,16 +288,9 @@ io_etherRecv(int eth, void * __restrict buf, size_t bu LOGERR; } else { #ifdef __FreeBSD__ - do { - rlen = nextZCbuf(eth, &zcache, (struct bpf_zbuf*) zcbuf); - if (rlen > 0) { - bzh = (struct bpf_zbuf_header*) zcache; - memcpy(buf, zcache + sizeof(struct bpf_zbuf_header), - MIN(buflen, rlen)); - } - if (!rlen && !ioctl(eth, BIOCROTZBUF, &bz)) - continue; - } while (0); + rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen); + if (!rlen) + rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen); #else rlen = -1; io_SetErr(ENOTSUP, "bpf zero copy buffer mode is not supported");