Date: Mon, 24 Dec 2007 23:05:46 GMT From: Robert Watson <rwatson@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 131558 for review Message-ID: <200712242305.lBON5k4g045111@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=131558 Change 131558 by rwatson@rwatson_cinnamon on 2007/12/24 23:04:49 Fix up commenting, ifdef'ing, and indentation a bit. Affected files ... .. //depot/projects/zcopybpf/src/contrib/libpcap/pcap-bpf.c#18 edit Differences ... ==== //depot/projects/zcopybpf/src/contrib/libpcap/pcap-bpf.c#18 (text+ko) ==== @@ -39,8 +39,6 @@ #include <sys/ioctl.h> #include <sys/utsname.h> -#include <machine/atomic.h> - #include <net/if.h> #ifdef _AIX @@ -90,6 +88,10 @@ #endif /* _AIX */ +#ifdef BIOCSETBUFMODE +#include <machine/atomic.h> +#endif + #include <ctype.h> #include <errno.h> #include <netdb.h> @@ -156,10 +158,6 @@ { struct bpf_zbuf_header *bzh; - /* - * If we've never used a buffer before, or if the last buffer was - * zbuf2, try zbuf1. - */ if (p->zbuffer == p->zbuf2 || p->zbuffer == NULL) { bzh = (struct bpf_zbuf_header *)p->zbuf1; if (bzh->bzh_user_gen != @@ -243,13 +241,13 @@ "BIOCROTZBUF: %s", strerror(errno)); return (-1); } - - /* - * Last chance for data. - */ return (pcap_next_zbuf_shm(p, cc)); } +/* + * Notify kernel that we are done with the buffer. We don't reset zbuffer so + * that we know which buffer to use next time around. + */ static int pcap_ack_zbuf(pcap_t *p) { @@ -795,15 +793,12 @@ goto bad; } +#ifdef BIOCSETBUFMODE /* - * XXXRW: Depending on the availability of zero-copy BPF, we take one - * of two strategies here: if it is available and usable, we go ahead - * and set it up; otherwise we play the song-and-dance to try to - * probe an acceptable read buffer size. Zero-copy BPF requires that - * buffers be mapped into memory before selecting the interface to - * attach to, so we do that here also. + * If the BPF extension to set buffer mode is present, try setting + * the mode to zero-copy. If that fails, use regular buffering. If + * it succeeds but other setup fails, return an error to the user. */ -#ifdef BIOCSETBUFMODE bufmode = BPF_BUFMODE_ZBUF; if (ioctl(fd, BIOCSETBUFMODE, (caddr_t)&bufmode) == 0) { p->zerocopy = 1; @@ -812,14 +807,9 @@ pcap_strerror(errno)); goto bad; } - - /* - * XXXRW: This logic should be revisited. - */ p->zbufsize = 32768; if (p->zbufsize > zbufmax) p->zbufsize = zbufmax; - p->zbuf1 = mmap(NULL, p->zbufsize, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0); p->zbuf2 = mmap(NULL, p->zbufsize, PROT_READ | PROT_WRITE, @@ -829,18 +819,15 @@ pcap_strerror(errno)); goto bad; } - bzero(&bz, sizeof(bz)); bz.bz_bufa = p->zbuf1; bz.bz_bufb = p->zbuf2; bz.bz_buflen = p->zbufsize; - if (ioctl(fd, BIOCSETZBUF, (caddr_t)&bz) < 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETZBUF: %s", pcap_strerror(errno)); goto bad; } - (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) < 0) { snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", @@ -852,41 +839,43 @@ } else { #endif - /* - * Try finding a good size for the buffer; 32768 may be too - * big, so keep cutting it in half until we find a size - * that works, or run out of sizes to try. If the default - * is larger, don't make it smaller. - * - * XXX - there should be a user-accessible hook to set the - * initial buffer size. - */ - if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || v < 32768) - v = 32768; - for ( ; v != 0; v >>= 1) { - /* Ignore the return value - this is because the call fails - * on BPF systems that don't have kernel malloc. And if - * the call fails, it's no big deal, we just continue to - * use the standard buffer size. + /* + * Try finding a good size for the buffer; 32768 may be too + * big, so keep cutting it in half until we find a size + * that works, or run out of sizes to try. If the default + * is larger, don't make it smaller. + * + * XXX - there should be a user-accessible hook to set the + * initial buffer size. */ - (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v); + if ((ioctl(fd, BIOCGBLEN, (caddr_t)&v) < 0) || v < 32768) + v = 32768; + for ( ; v != 0; v >>= 1) { + /* Ignore the return value - this is because the call + * fails on BPF systems that don't have kernel + * malloc. And if the call fails, it's no big deal, + * we just continue to use the standard buffer size. + */ + (void) ioctl(fd, BIOCSBLEN, (caddr_t)&v); + + (void)strncpy(ifr.ifr_name, device, + sizeof(ifr.ifr_name)); + if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0) + break; /* that size worked; we're done */ - (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); - if (ioctl(fd, BIOCSETIF, (caddr_t)&ifr) >= 0) - break; /* that size worked; we're done */ + if (errno != ENOBUFS) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, + "BIOCSETIF: %s: %s", + device, pcap_strerror(errno)); + goto bad; + } + } - if (errno != ENOBUFS) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, "BIOCSETIF: %s: %s", - device, pcap_strerror(errno)); + if (v == 0) { + snprintf(ebuf, PCAP_ERRBUF_SIZE, + "BIOCSBLEN: %s: No buffer size worked", device); goto bad; } - } - - if (v == 0) { - snprintf(ebuf, PCAP_ERRBUF_SIZE, - "BIOCSBLEN: %s: No buffer size worked", device); - goto bad; - } #ifdef BIOCSETBUFMODE } #endif
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200712242305.lBON5k4g045111>