PERFORCE change 113764 for review

Robert Watson rwatson at FreeBSD.org
Wed Jan 31 14:58:13 UTC 2007


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

Change 113764 by rwatson at rwatson_cinnamon on 2007/01/31 14:57:30

	Also call poll and select to see what the status of a bpf device is
	once a second.
	
	Set immediate mode.  Probably want a run-time flag for this.

Affected files ...

.. //depot/projects/zcopybpf/utils/zbuf_tap/zbuf_tap.c#3 edit

Differences ...

==== //depot/projects/zcopybpf/utils/zbuf_tap/zbuf_tap.c#3 (text+ko) ====

@@ -38,6 +38,8 @@
 #include <sys/types.h>
 #include <sys/ioctl.h>
 #include <sys/mman.h>
+#include <sys/poll.h>
+#include <sys/select.h>
 #include <sys/socket.h>
 
 #include <net/if.h>
@@ -46,6 +48,7 @@
 #include <err.h>
 #include <limits.h>
 #include <stdio.h>
+#include <string.h>
 #include <unistd.h>
 
 #include "zbuf_tap.h"
@@ -55,9 +58,12 @@
 {
 	u_char *bufa, *bufb, *buf;
 	u_int buflen, maxbuflen;
+	struct pollfd pollfd;
 	char name[PATH_MAX];
 	int bpf_fd, tap_fd;
+	struct timeval tv;
 	int i, tap_unit;
+	fd_set set;
 
 	buflen = getpagesize() * 2;
 
@@ -91,6 +97,9 @@
 	if (bpf_setzbuf(bpf_fd, bufa, bufb, buflen) < 0)
 		err(-1, "bpf_setzbuf");
 
+	if (bpf_setimmediate(bpf_fd, 1) < 0)
+		err(-1, "bpf_setimmediate");
+
 	if (bpf_captureall(bpf_fd) < 0)
 		err(-1, "bpf_captureall");
 
@@ -100,13 +109,43 @@
 
 	while (1) {
 		sleep(1);
+		/*
+		 * Exercise various poll mechanisms to see which say
+		 * something is ready to read.
+		 */
 		if (bpf_getznext(bpf_fd, (void **)&buf, &buflen) < 0)
 			err(-1, "bpf_getznext");
 		printf("bpf_getznext returned (0x%x, %d)\n", (uintptr_t)buf,
 		    buflen);
+
 		if (ioctl(bpf_fd, FIONREAD, &i) < 0)
 			err(-1, "ioctl(FIONREAD)");
 		printf("FIONREAD returned %d\n", i);
+
+		bzero(&pollfd, sizeof(pollfd));
+		pollfd.fd = bpf_fd;
+		pollfd.events = POLLIN;
+		i = poll(&pollfd, 1, 0);
+		if (i < 0)
+			err(-1, "poll");
+		if (i == 0)
+			printf("poll returned 0\n");
+		else
+			printf("poll returned revents of 0x%x\n",
+			    pollfd.revents);
+
+		FD_ZERO(&set);
+		FD_SET(bpf_fd, &set);
+		bzero(&tv, sizeof(tv));
+		tv.tv_sec = 0;
+		tv.tv_usec = 0;
+		if (select(bpf_fd + 1, &set, NULL, NULL, &tv) < 0)
+			err(-1, "select");
+		if (FD_ISSET(bpf_fd, &set))
+			printf("select returned readable\n");
+		else
+			printf("select returned not readable\n");
+
 		if (buf != NULL) {
 			if (bpf_ackzbuf(bpf_fd, buf, buflen) < 0)
 				err(-1, "bpf_ackzbuf(0x%x, %d)",


More information about the p4-projects mailing list