PERFORCE change 128901 for review

Robert Watson rwatson at FreeBSD.org
Sat Nov 10 08:20:43 PST 2007


http://perforce.freebsd.org/chv.cgi?CH=128901

Change 128901 by rwatson at rwatson_cinnamon on 2007/11/10 16:19:53

	Make 'fd' global so that it is available in signal handlers; when
	exiting bpfnull, also query the BPF descriptor for statistics and
	print them as well.

Affected files ...

.. //depot/projects/zcopybpf/utils/bpfnull/bpfnull.c#20 edit

Differences ...

==== //depot/projects/zcopybpf/utils/bpfnull/bpfnull.c#20 (text+ko) ====

@@ -60,6 +60,7 @@
 static struct ifreq	 ifr;
 static pcap_dumper_t	*dp;
 static pcap_t		*p;
+static int		 bpffd = -1;
 static char		*fflag = "-";
 static unsigned long	 cflag;
 static int		 Iflag;
@@ -92,6 +93,7 @@
 static int
 handle_int(int sig __unused)
 {
+	struct bpf_stat bs;
 
 	putchar('\n');
 	printf("%lu cycles spent processing packets\n", sum);
@@ -99,6 +101,13 @@
 	printf("%lu cycles spent not sleeping\n", ssum);
 	printf("%lu cycles spent before buffer reclaims\n", psum);
 	printf("%lu packets processed\n", packet_count);
+
+	if (ioctl(bpffd, BIOCGSTATS, &bs) < 0)
+		err(-1, "BIOCGSTATS");
+
+	printf("%u packets received (BPF)\n", bs.bs_recv);
+	printf("%u packets dropped (BPF)\n", bs.bs_drop);
+
 	exit(0);
 }
 
@@ -173,7 +182,7 @@
 }
 
 static void
-bpf_wait_for_fullbuf(int fd)
+bpf_wait_for_fullbuf(void)
 {
 	fd_set s_set, r_set;
 	struct bpf_zbuf bz;
@@ -190,10 +199,10 @@
 		err(1, "malloc");
 	tv.tv_sec = 1;
 	tv.tv_usec = 0;
-	FD_SET(fd, &s_set);
+	FD_SET(bpffd, &s_set);
         for (;;) {
 		r_set = s_set;
-		n = select(fd + 1, &r_set, NULL, NULL, &tv);
+		n = select(bpffd + 1, &r_set, NULL, NULL, &tv);
 		e = rdtsc();
 		if (n < 0) {
 			fprintf(stderr,"owned by select\n");
@@ -201,13 +210,13 @@
 		}
 		if (vflag)
 			(void) fprintf(stderr, "select wakeup\n");
-		if (n != 0 && !FD_ISSET(fd, &r_set) && vflag)
+		if (n != 0 && !FD_ISSET(bpffd, &r_set) && vflag)
 			printf("No timeout and fd is not ready!\n");
 #ifdef BPF_BUFMODE_ZBUF
 		if (zflag == 0) {
 #endif
 			c = rdtsc();
-			n = read(fd, pbuf, bflag);
+			n = read(bpffd, pbuf, bflag);
 			d = rdtsc();
 			if (n < 0)
 				err(1, "read failed");
@@ -225,7 +234,7 @@
 			bzhb = (struct bpf_zbuf_header *)bufb;
 			if (n == 0) {
 				c = rdtsc();
-				if (ioctl(fd, BIOCROTZBUF, &bz) < 0)
+				if (ioctl(bpffd, BIOCROTZBUF, &bz) < 0)
 					err(1, "ioctl");
 				d = rdtsc();
 				rsum += d - c;
@@ -293,7 +302,7 @@
 
 #ifdef BPF_BUFMODE_ZBUF
 static int
-bpf_zbuf_init(int fd, struct bpf_zbuf *bz)
+bpf_zbuf_init(struct bpf_zbuf *bz)
 {
 	int bmode;
 
@@ -311,9 +320,9 @@
 	bz->bz_bufa = bufa;
 	bz->bz_bufb = bufb;
 	bmode = BPF_BUFMODE_ZBUF;
-	if (ioctl(fd, BIOCSETBUFMODE, &bmode) < 0)
+	if (ioctl(bpffd, BIOCSETBUFMODE, &bmode) < 0)
 		err(1, "ioctl(BIOCGETBUFMODE)");
-	if (ioctl(fd, BIOCSETZBUF, bz) < 0)
+	if (ioctl(bpffd, BIOCSETZBUF, bz) < 0)
 		err(1, "ioctl(BIOCSETZBUF)");
 	if (vflag)
 		(void) fprintf(stderr,
@@ -323,21 +332,21 @@
 #endif
 
 static int
-bpf_rbuf_init(int fd)
+bpf_rbuf_init(void)
 {
 	int v, bmode;
 
 #ifdef BPF_BUFMODE_ZBUF
 	bmode = BPF_BUFMODE_BUFFER;
-	if (ioctl(fd, BIOCGETBUFMODE, &bmode) < 0)
+	if (ioctl(bpffd, BIOCGETBUFMODE, &bmode) < 0)
 		err(1, "ioctl(BIOCGETBUFMODE)");
 #endif
 	for (v = bflag; v != 0; v >>= 1) {
-		(void) ioctl(fd, BIOCSBLEN, &v);
-		if (ioctl(fd, BIOCSETIF, &ifr) == 0)
+		(void) ioctl(bpffd, BIOCSBLEN, &v);
+		if (ioctl(bpffd, BIOCSETIF, &ifr) == 0)
 			break;
 	}
-	if (ioctl(fd, BIOCFLUSH, NULL) < 0)
+	if (ioctl(bpffd, BIOCFLUSH, NULL) < 0)
 		err(1, "ioctl(BIOCFLUSH)");
 	return (0);
 }
@@ -345,7 +354,7 @@
 int
 main(int argc, char *argv[])
 {
-	int opt, in;
+	int opt;
 	struct bpf_zbuf bz;
 	char ch;
 
@@ -401,14 +410,14 @@
 	}
 	bzero(&ifr, sizeof(ifr));
 	strlcpy(ifr.ifr_name, iflag, sizeof(ifr.ifr_name));
-	in = bpf_open();
-	if (in == -1) {
+	bpffd = bpf_open();
+	if (bpffd == -1) {
 		(void) fprintf(stderr, "bpfnull: no bpf device available\n");
 		exit(1);
 	}
 	if (vflag)
 		(void) fprintf(stderr,
-		    "DEBUG: obtained bpf fd=%d\n", in);
+		    "DEBUG: obtained bpf fd=%d\n", bpffd);
 	bpf_init_dumpfile();
 #ifdef BPF_BUFMODE_ZBUF
 	if (zflag) {
@@ -416,15 +425,15 @@
 			(void) fprintf(stderr,
 			    "DEBUG: bufmode=zerocopy\n");
 		bzero(&bz, sizeof(bz));
-		bpf_zbuf_init(in, &bz);
-		if (ioctl(in, BIOCSETIF, &ifr) < 0)
+		bpf_zbuf_init(bpffd, &bz);
+		if (ioctl(bpffd, BIOCSETIF, &ifr) < 0)
 			err(1, "ioctl(BIOCSETIF)");
 	} else {
 #endif
 		if (vflag)
 			(void) fprintf(stderr,
 			    "DEBUG: bufmode=buffer\n");
-		bpf_rbuf_init(in);
+		bpf_rbuf_init();
 #ifdef BPF_BUFMODE_ZBUF
 	}
 #endif
@@ -433,7 +442,7 @@
 			(void) fprintf(stderr,
 			    "DEBUG: setting BIOCIMMEDIATE\n");
 		opt = 1;
-		if (ioctl(in, BIOCIMMEDIATE, &opt) < 0)
+		if (ioctl(bpffd, BIOCIMMEDIATE, &opt) < 0)
 			err(1, "BIOCIMMEDIATE");
 	}
 	if (Pflag) {
@@ -441,12 +450,12 @@
 			(void) fprintf(stderr,
 			    "DEBUG: putting card into promiscuous "
 			    "mode\n");
-		if (ioctl(in, BIOCPROMISC, NULL) < 0)
+		if (ioctl(bpffd, BIOCPROMISC, NULL) < 0)
 			err(1, "BIOCPROMISC");
 	}
 	if (vflag)
 		(void) fprintf(stderr,
 		    "DEBUG: attaching to %s\n", iflag);
-	bpf_wait_for_fullbuf(in);
+	bpf_wait_for_fullbuf();
 	return (0);
 }


More information about the p4-projects mailing list