--- libaitio/src/bpf.c 2013/06/26 15:31:17 1.1.2.18 +++ libaitio/src/bpf.c 2013/06/26 16:22:12 1.1.2.19 @@ -232,25 +232,30 @@ chkZCbuf(struct bpf_zbuf_header *bzh) } static inline ssize_t -nextZCbuf(int eth, void ** __restrict zcache, struct bpf_zbuf * __restrict zbuf) +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) + ioctl(eth, BIOCROTZBUF, &bz); return rlen; } #endif @@ -268,16 +273,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"); @@ -291,13 +286,8 @@ io_etherRecv(int eth, void * __restrict buf, size_t bu } 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)) + rlen = nextZCbuf(eth, (struct bpf_zbuf*) zcbuf, buf, buflen); + if (!rlen) continue; } while (0); #else