Annotation of libaitio/example/bpf.c, revision 1.2.2.1
1.2 misho 1: #include <stdio.h>
2: #include <stdlib.h>
3: #include <fcntl.h>
4: #include <unistd.h>
5: #include <string.h>
6: #include <errno.h>
7: #include <poll.h>
8: #include <getopt.h>
9: #include <pthread.h>
10: #include <assert.h>
11: #include <sys/types.h>
12: #include <sys/param.h>
13: #include <sys/limits.h>
14: #include <sys/socket.h>
15: #include <sys/ioctl.h>
16: #include <sys/mman.h>
17: #include <net/if.h>
18: #include <net/bpf.h>
19: #include <machine/atomic.h>
20:
21: #ifdef __FreeBSD__
22: #include <net/ethernet.h>
23: #endif
24: #ifdef __OpenBSD__
25: #include <net/ethertypes.h>
26: #include <netinet/in_systm.h>
27: #endif
28: #include <net/if_arp.h>
29: #include <netinet/in.h>
30: #include <netinet/ip.h>
31: #include <netinet/ip6.h>
32: #include <netinet/udp.h>
33: #include <netinet/tcp.h>
34: #include <netinet/ip_icmp.h>
35: #include <arpa/inet.h>
36: #ifdef __OpenBSD__
37: #include <netinet/if_ether.h>
38: #endif
39:
40: #include <aitio.h>
41:
42: #ifndef roundup
43: #define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) /* to any y */
44: #endif
45:
46: #define time_spec_sub(vvp, uvp) \
47: do { \
48: (vvp)->tv_sec -= (uvp)->tv_sec; \
49: (vvp)->tv_nsec -= (uvp)->tv_nsec; \
50: if ((vvp)->tv_nsec < 0) { \
51: (vvp)->tv_sec--; \
52: (vvp)->tv_nsec += 1000000000; \
53: } \
54: } while (0)
55:
56:
57: int Verbose, flg;
58:
59:
60: static void *
61: ShowPkt(void *buffer)
62: {
63: char Proto = 0, szStr[BUFSIZ], szLine[BUFSIZ], szWrk[BUFSIZ], szShow[USHRT_MAX] = { 0 };
64: struct bpf_hdr *bpf = buffer;
65: #ifdef __FreeBSD__
66: struct bpf_zbuf_header *bzh = buffer;
67: struct icmphdr *icmp;
68: #endif
69: #ifdef __OpenBSD__
70: struct icmp *icmp;
71: #endif
72: struct ether_header *eth;
73: struct ip *ip;
74: struct ip6_hdr *ipv6;
75: struct arphdr *arp;
76: struct udphdr *udp;
77: struct tcphdr *tcp;
78:
79: assert(buffer);
80:
81: snprintf(szLine, BUFSIZ, "#Packet length: %d\n>>> Ethernet ...\n", bpf->bh_datalen);
82: strlcat(szShow, szLine, USHRT_MAX);
83: eth = (struct ether_header *) (buffer + bpf->bh_hdrlen);
84:
85: switch (ntohs(eth->ether_type)) {
86: case ETHERTYPE_ARP:
87: strlcpy(szWrk, "(ARP)", sizeof szWrk);
88: break;
89: case ETHERTYPE_IP:
90: strlcpy(szWrk, "(IP)", sizeof szWrk);
91: break;
92: case ETHERTYPE_IPV6:
93: strlcpy(szWrk, "(IPv6)", sizeof szWrk);
94: break;
95: default:
96: memset(szWrk, 0, sizeof szWrk);
97: }
98: snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_shost[0], eth->ether_shost[1],
99: eth->ether_shost[2], eth->ether_shost[3], eth->ether_shost[4], eth->ether_shost[5]);
100: snprintf(szLine, BUFSIZ, "ether_type: %04x%s, src_mac: %s, ", ntohs(eth->ether_type), szWrk, szStr);
101: strlcat(szShow, szLine, USHRT_MAX);
102: snprintf(szStr, BUFSIZ, "%02x:%02x:%02x:%02x:%02x:%02x", eth->ether_dhost[0], eth->ether_dhost[1],
103: eth->ether_dhost[2], eth->ether_dhost[3], eth->ether_dhost[4], eth->ether_dhost[5]);
104: snprintf(szLine, BUFSIZ, "dst_mac: %s\n", szStr);
105: strlcat(szShow, szLine, USHRT_MAX);
106:
107: switch (ntohs(eth->ether_type)) {
108: case ETHERTYPE_ARP:
109: arp = (struct arphdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
110: strlcat(szShow, "\t>>> ARP ...\n", USHRT_MAX);
111:
112: switch (ntohs(arp->ar_op)) {
113: case ARPOP_REQUEST:
114: strlcpy(szStr, "Request", sizeof szStr);
115: break;
116: case ARPOP_REPLY:
117: strlcpy(szStr, "Reply", sizeof szStr);
118: break;
119: case ARPOP_REVREQUEST:
120: strlcpy(szStr, "RevRequest", sizeof szStr);
121: break;
122: case ARPOP_REVREPLY:
123: strlcpy(szStr, "RevReply", sizeof szStr);
124: break;
125: case ARPOP_INVREQUEST:
126: strlcpy(szStr, "InvRequest", sizeof szStr);
127: break;
128: case ARPOP_INVREPLY:
129: strlcpy(szStr, "InvReply", sizeof szStr);
130: break;
131: default:
132: strlcpy(szStr, "*Unknown*", sizeof szStr);
133: }
134: snprintf(szLine, BUFSIZ, "\tAddr_fmt: %d, Proto_fmt: %d, Addr_len: %d, Proto_len: %d, %s\n",
135: ntohs(arp->ar_hrd), ntohs(arp->ar_pro), arp->ar_hln, arp->ar_pln, szStr);
136: strlcat(szShow, szLine, USHRT_MAX);
137: break;
138: case ETHERTYPE_IP:
139: ip = (struct ip*) (((caddr_t) eth) + ETHER_HDR_LEN);
140: strlcat(szShow, "\t>>> IP ...\n", USHRT_MAX);
141:
142: snprintf(szStr, BUFSIZ, "Frag_off: %d", ntohs(ip->ip_off) & IP_OFFMASK);
143: if (ntohs(ip->ip_off) & IP_RF)
144: strlcat(szStr, "|RF", sizeof szStr);
145: if (ntohs(ip->ip_off) & IP_DF)
146: strlcat(szStr, "|DF", sizeof szStr);
147: if (ntohs(ip->ip_off) & IP_MF)
148: strlcat(szStr, "|MF", sizeof szStr);
149: snprintf(szLine, BUFSIZ, "\tTOS: %02x, TTL: %d, ID: %04x, Cksum: %04x, Len: %d, %s\n",
150: ip->ip_tos, ip->ip_ttl, ntohs(ip->ip_id), ntohs(ip->ip_sum),
151: ntohs(ip->ip_len), szStr);
152: strlcat(szShow, szLine, USHRT_MAX);
153: snprintf(szLine, BUFSIZ, "\tProto: %d, Src_addr: %s, Dst_addr: %s\n",
154: ip->ip_p, inet_ntop(AF_INET, &ip->ip_src, szStr, BUFSIZ),
155: inet_ntop(AF_INET, &ip->ip_dst, szWrk, BUFSIZ));
156: strlcat(szShow, szLine, USHRT_MAX);
157:
158: Proto = ip->ip_p;
159: break;
160: case ETHERTYPE_IPV6:
161: ipv6 = (struct ip6_hdr*) (((caddr_t) eth) + ETHER_HDR_LEN);
162: strlcat(szShow, "\t>>> IPv6 ...\n", USHRT_MAX);
163:
164: snprintf(szLine, BUFSIZ, "\tHLim: %d, Payload_len: %d, Flow: %u\n", ipv6->ip6_hlim,
165: htons(ipv6->ip6_plen), htonl(ipv6->ip6_flow) & IPV6_FLOWLABEL_MASK);
166: strlcat(szShow, szLine, USHRT_MAX);
167: inet_ntop(AF_INET6, &ipv6->ip6_src, szStr, BUFSIZ);
168: inet_ntop(AF_INET6, &ipv6->ip6_dst, szWrk, BUFSIZ);
169: snprintf(szLine, BUFSIZ, "\tNext: %d, Src_addr: %s, Dst_addr: %s\n", ipv6->ip6_nxt, szStr, szWrk);
170: strlcat(szShow, szLine, USHRT_MAX);
171:
172: Proto = ipv6->ip6_nxt;
173: break;
174: }
175:
176: if (Proto && (ntohs(eth->ether_type) == ETHERTYPE_IP || ntohs(eth->ether_type) == ETHERTYPE_IPV6))
177: switch (Proto) {
178: case IPPROTO_TCP:
179: strlcat(szShow, "\t\t>>> TCP ...\n", USHRT_MAX);
180: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
181: tcp = (struct tcphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
182: else
183: tcp = (struct tcphdr*) (((caddr_t) ip) + sizeof(struct ip));
184:
185: snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, seq: %u, ack: %u\n",
186: ntohs(tcp->th_sport), ntohs(tcp->th_dport), tcp->th_seq, tcp->th_ack);
187: strlcat(szShow, szLine, USHRT_MAX);
188: snprintf(szWrk, BUFSIZ, "%d", (u_char) tcp->th_off >> 4);
189: if (tcp->th_flags & TH_FIN)
190: strlcat(szWrk, "|FIN", sizeof szWrk);
191: if (tcp->th_flags & TH_SYN)
192: strlcat(szWrk, "|SYN", sizeof szWrk);
193: if (tcp->th_flags & TH_RST)
194: strlcat(szWrk, "|RST", sizeof szWrk);
195: if (tcp->th_flags & TH_PUSH)
196: strlcat(szWrk, "|PUSH", sizeof szWrk);
197: if (tcp->th_flags & TH_ACK)
198: strlcat(szWrk, "|ACK", sizeof szWrk);
199: if (tcp->th_flags & TH_URG)
200: strlcat(szWrk, "|URG", sizeof szWrk);
201: if (tcp->th_flags & TH_ECE)
202: strlcat(szWrk, "|ECE", sizeof szWrk);
203: if (tcp->th_flags & TH_CWR)
204: strlcat(szWrk, "|CWR", sizeof szWrk);
205: snprintf(szLine, BUFSIZ, "\t\toff: %s, win: %d, urp: %04x, cksum: %04x\n",
206: szWrk, ntohs(tcp->th_win), ntohs(tcp->th_urp), ntohs(tcp->th_sum));
207: strlcat(szShow, szLine, USHRT_MAX);
208: break;
209: case IPPROTO_UDP:
210: strlcat(szShow, "\t\t>>> UDP ...\n", USHRT_MAX);
211: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
212: udp = (struct udphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
213: else
214: udp = (struct udphdr*) (((caddr_t) ip) + sizeof(struct ip));
215:
216: snprintf(szLine, BUFSIZ, "\t\tsrc_port: %d, dst_port: %d, len: %d, cksum: %04x\n",
217: ntohs(udp->uh_sport), ntohs(udp->uh_dport),
218: ntohs(udp->uh_ulen), ntohs(udp->uh_sum));
219: strlcat(szShow, szLine, USHRT_MAX);
220: break;
221: case IPPROTO_ICMP:
222: strlcat(szShow, "\t\t>>> ICMP ...\n", USHRT_MAX);
223: #ifdef __FreeBSD__
224: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
225: icmp = (struct icmphdr*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
226: else
227: icmp = (struct icmphdr*) (((caddr_t) ip) + sizeof(struct ip));
228: #endif
229: #ifdef __OpenBSD__
230: if (ntohs(eth->ether_type) == ETHERTYPE_IPV6)
231: icmp = (struct icmp*) (((caddr_t) ipv6) + sizeof(struct ip6_hdr));
232: else
233: icmp = (struct icmp*) (((caddr_t) ip) + sizeof(struct ip));
234: #endif
235:
236: snprintf(szLine, BUFSIZ, "\t\ttype: %d, code: %d: cksum: %04x\n",
237: icmp->icmp_type, icmp->icmp_code, ntohs(icmp->icmp_cksum));
238: strlcat(szShow, szLine, USHRT_MAX);
239: break;
240: }
241:
242: printf("%s===\n", szShow);
243: return NULL;
244: }
245:
246: // ----------------------
247:
248: int
249: main(int argc, char **argv)
250: {
251: u_int n, count = (u_int) -1;
252: register int i;
253: int dev, fd, ret, siz = 0;
254: char szStr[BUFSIZ], szMap[MAXPATHLEN] = { 0 }, *buffer = NULL;
255: struct ifreq ifr;
256: struct pollfd pfd = { 0 };
257: pthread_t tid;
258: char ch, mode = 'R';
259: struct timespec ts_start, ts_end;
260: void *bz = NULL;
261:
262: while ((ch = getopt(argc, argv, "hvwzs:p:f:")) != -1)
263: switch (ch) {
264: case 'w':
265: mode = 'W';
266: break;
267: case 's':
268: siz = strtol(optarg, NULL, 0);
269: break;
270: case 'p':
271: count = strtol(optarg, NULL, 0);
272: break;
273: case 'f':
274: strlcpy(szMap, optarg, sizeof szMap);
275: break;
276: case 'z':
277: flg++;
278: break;
279: case 'v':
280: Verbose++;
281: break;
282: case 'h':
283: default:
284: return 1;
285: }
286: argc -= optind;
287: argv += optind;
288: if (argc < 1)
289: return 1;
290:
291: if (**argv == '/')
292: strlcpy(szStr, strrchr(*argv, '/') + 1, sizeof szStr);
293: else
294: strlcpy(szStr, *argv, sizeof szStr);
295:
296: #ifdef __FreeBSD__
1.2.2.1 ! misho 297: dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, (flg) ? &bz : NULL);
1.2 misho 298: if (dev == -1)
1.2.2.1 ! misho 299: dev = io_etherOpen(szStr, O_RDWR | O_NONBLOCK, 42, 0, (u_int*) &siz, NULL);
1.2 misho 300: #else
1.2.2.1 ! misho 301: dev = io_etherOpen(szStr, O_RDWR, 42, 0, (u_int*) &siz, NULL);
1.2 misho 302: #endif
303: if (dev == -1) {
304: printf("Error:: #%d - %s\n", io_GetErrno(), io_GetError());
305: return 1;
306: } else
307: printf("dev=%d(%s)\n", dev, szStr);
308:
309: if (ioctl(dev, BIOCGBLEN, &siz) == -1) {
310: perror("ioctl(BIOCGBLEN)");
311: io_etherClose(dev, &bz);
312: return 1;
313: } else
314: printf("BPF buffer len=%d\n", siz);
315:
316: if (*szMap) {
317: fd = open(szMap, O_RDWR);
318: if (fd == -1) {
319: perror("open(map)");
320: io_etherClose(dev, &bz);
321: return 1;
322: }
323: buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
324: close(fd);
325: } else
326: buffer = mmap(NULL, siz, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
327: if (buffer == MAP_FAILED) {
328: perror("mmap()");
329: io_etherClose(dev, &bz);
330: return 1;
331: }
332:
333: pfd.fd = dev;
334: assert(!clock_gettime(CLOCK_REALTIME, &ts_start));
335: if (mode == 'R') {
336: pfd.events = POLLIN;
337: for (i = 0; i < count; i++) {
338: if ((ret = poll(&pfd, 1, -1)) == -1)
339: break;
340:
341: ret = io_etherRecv(dev, buffer, siz, bz);
342: if (!ret)
343: continue;
344: if (ret == -1)
345: printf("%d) io_etherRecv(%d) #%d - %s\n", i, ret,
346: io_GetErrno(), io_GetError());
347: if (Verbose) {
348: printf("%d) +readed %d bytes\n", i, ret);
349: ShowPkt(buffer);
350: }
351: }
352: } else {
353: pfd.events = POLLOUT;
354: for (i = 0; i < count; i++) {
355: if (poll(&pfd, 1, -1) == -1)
356: break;
357:
358: ret = io_etherSend(dev, buffer, siz);
359: if (ret == -1)
360: printf("io_etherSend(%d) #%d - %s\n", ret, io_GetErrno(), io_GetError());
361: if (Verbose)
362: printf("+writed %d bytes\n", ret);
363: }
364: }
365: assert(!clock_gettime(CLOCK_REALTIME, &ts_end));
366:
367: munmap(buffer, siz);
368: io_etherClose(dev, &bz);
369:
370: time_spec_sub(&ts_end, &ts_start);
371: printf("%d.%09lu for %d iterations\n", ts_end.tv_sec, ts_end.tv_nsec, i);
372: ts_end.tv_sec *= 1000000000 / i;
373: printf("0.%09lu per/iteration\n", ts_end.tv_sec + ts_end.tv_nsec / i);
374: return 0;
375: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>