1 /*
2 * Copyright 2013 Red Hat, Inc.
3 * Author: Daniel Borkmann <dborkman@redhat.com>
4 * Chetan Loke <loke.chetan@gmail.com> (TPACKET_V3 usage example)
5 *
6 * A basic test of packet socket's TPACKET_V1/TPACKET_V2/TPACKET_V3 behavior.
7 *
8 * Control:
9 * Test the setup of the TPACKET socket with different patterns that are
10 * known to fail (TODO) resp. succeed (OK).
11 *
12 * Datapath:
13 * Open a pair of packet sockets and send resp. receive an a priori known
14 * packet pattern accross the sockets and check if it was received resp.
15 * sent correctly. Fanout in combination with RX_RING is currently not
16 * tested here.
17 *
18 * The test currently runs for
19 * - TPACKET_V1: RX_RING, TX_RING
20 * - TPACKET_V2: RX_RING, TX_RING
21 * - TPACKET_V3: RX_RING
22 *
23 * License (GPLv2):
24 *
25 * This program is free software; you can redistribute it and/or modify it
26 * under the terms and conditions of the GNU General Public License,
27 * version 2, as published by the Free Software Foundation.
28 *
29 * This program is distributed in the hope it will be useful, but WITHOUT
30 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31 * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
32 * more details.
33 *
34 * You should have received a copy of the GNU General Public License along with
35 * this program; if not, write to the Free Software Foundation, Inc.,
36 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
37 */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/socket.h>
44 #include <sys/mman.h>
45 #include <linux/if_packet.h>
46 #include <linux/filter.h>
47 #include <ctype.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #ifndef __ANDROID__
51 #include <bits/wordsize.h>
52 #endif
53 #include <net/ethernet.h>
54 #include <netinet/ip.h>
55 #include <arpa/inet.h>
56 #include <stdint.h>
57 #include <string.h>
58 #include <assert.h>
59 #include <net/if.h>
60 #include <inttypes.h>
61 #include <poll.h>
62
63 #include "psock_lib.h"
64
65 #ifndef bug_on
66 # define bug_on(cond) assert(!(cond))
67 #endif
68
69 #ifndef __aligned_tpacket
70 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
71 #endif
72
73 #ifndef __align_tpacket
74 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
75 #endif
76
77 #define NUM_PACKETS 100
78 #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1))
79
80 struct ring {
81 struct iovec *rd;
82 uint8_t *mm_space;
83 size_t mm_len, rd_len;
84 struct sockaddr_ll ll;
85 void (*walk)(int sock, struct ring *ring);
86 int type, rd_num, flen, version;
87 union {
88 struct tpacket_req req;
89 struct tpacket_req3 req3;
90 };
91 };
92
93 struct block_desc {
94 uint32_t version;
95 uint32_t offset_to_priv;
96 struct tpacket_hdr_v1 h1;
97 };
98
99 union frame_map {
100 struct {
101 struct tpacket_hdr tp_h __aligned_tpacket;
102 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket_hdr));
103 } *v1;
104 struct {
105 struct tpacket2_hdr tp_h __aligned_tpacket;
106 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
107 } *v2;
108 void *raw;
109 };
110
111 static unsigned int total_packets, total_bytes;
112
pfsocket(int ver)113 static int pfsocket(int ver)
114 {
115 int ret, sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
116 if (sock == -1) {
117 perror("socket");
118 exit(1);
119 }
120
121 ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
122 if (ret == -1) {
123 perror("setsockopt");
124 exit(1);
125 }
126
127 return sock;
128 }
129
status_bar_update(void)130 static void status_bar_update(void)
131 {
132 if (total_packets % 10 == 0) {
133 fprintf(stderr, ".");
134 fflush(stderr);
135 }
136 }
137
test_payload(void * pay,size_t len)138 static void test_payload(void *pay, size_t len)
139 {
140 struct ethhdr *eth = pay;
141
142 if (len < sizeof(struct ethhdr)) {
143 fprintf(stderr, "test_payload: packet too "
144 "small: %zu bytes!\n", len);
145 exit(1);
146 }
147
148 if (eth->h_proto != htons(ETH_P_IP)) {
149 fprintf(stderr, "test_payload: wrong ethernet "
150 "type: 0x%x!\n", ntohs(eth->h_proto));
151 exit(1);
152 }
153 }
154
create_payload(void * pay,size_t * len)155 static void create_payload(void *pay, size_t *len)
156 {
157 int i;
158 struct ethhdr *eth = pay;
159 struct iphdr *ip = pay + sizeof(*eth);
160
161 /* Lets create some broken crap, that still passes
162 * our BPF filter.
163 */
164
165 *len = DATA_LEN + 42;
166
167 memset(pay, 0xff, ETH_ALEN * 2);
168 eth->h_proto = htons(ETH_P_IP);
169
170 for (i = 0; i < sizeof(*ip); ++i)
171 ((uint8_t *) pay)[i + sizeof(*eth)] = (uint8_t) rand();
172
173 ip->ihl = 5;
174 ip->version = 4;
175 ip->protocol = 0x11;
176 ip->frag_off = 0;
177 ip->ttl = 64;
178 ip->tot_len = htons((uint16_t) *len - sizeof(*eth));
179
180 ip->saddr = htonl(INADDR_LOOPBACK);
181 ip->daddr = htonl(INADDR_LOOPBACK);
182
183 memset(pay + sizeof(*eth) + sizeof(*ip),
184 DATA_CHAR, DATA_LEN);
185 }
186
__v1_rx_kernel_ready(struct tpacket_hdr * hdr)187 static inline int __v1_rx_kernel_ready(struct tpacket_hdr *hdr)
188 {
189 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
190 }
191
__v1_rx_user_ready(struct tpacket_hdr * hdr)192 static inline void __v1_rx_user_ready(struct tpacket_hdr *hdr)
193 {
194 hdr->tp_status = TP_STATUS_KERNEL;
195 __sync_synchronize();
196 }
197
__v2_rx_kernel_ready(struct tpacket2_hdr * hdr)198 static inline int __v2_rx_kernel_ready(struct tpacket2_hdr *hdr)
199 {
200 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
201 }
202
__v2_rx_user_ready(struct tpacket2_hdr * hdr)203 static inline void __v2_rx_user_ready(struct tpacket2_hdr *hdr)
204 {
205 hdr->tp_status = TP_STATUS_KERNEL;
206 __sync_synchronize();
207 }
208
__v1_v2_rx_kernel_ready(void * base,int version)209 static inline int __v1_v2_rx_kernel_ready(void *base, int version)
210 {
211 switch (version) {
212 case TPACKET_V1:
213 return __v1_rx_kernel_ready(base);
214 case TPACKET_V2:
215 return __v2_rx_kernel_ready(base);
216 default:
217 bug_on(1);
218 return 0;
219 }
220 }
221
__v1_v2_rx_user_ready(void * base,int version)222 static inline void __v1_v2_rx_user_ready(void *base, int version)
223 {
224 switch (version) {
225 case TPACKET_V1:
226 __v1_rx_user_ready(base);
227 break;
228 case TPACKET_V2:
229 __v2_rx_user_ready(base);
230 break;
231 }
232 }
233
walk_v1_v2_rx(int sock,struct ring * ring)234 static void walk_v1_v2_rx(int sock, struct ring *ring)
235 {
236 struct pollfd pfd;
237 int udp_sock[2];
238 union frame_map ppd;
239 unsigned int frame_num = 0;
240
241 bug_on(ring->type != PACKET_RX_RING);
242
243 pair_udp_open(udp_sock, PORT_BASE);
244 pair_udp_setfilter(sock);
245
246 memset(&pfd, 0, sizeof(pfd));
247 pfd.fd = sock;
248 pfd.events = POLLIN | POLLERR;
249 pfd.revents = 0;
250
251 pair_udp_send(udp_sock, NUM_PACKETS);
252
253 while (total_packets < NUM_PACKETS * 2) {
254 while (__v1_v2_rx_kernel_ready(ring->rd[frame_num].iov_base,
255 ring->version)) {
256 ppd.raw = ring->rd[frame_num].iov_base;
257
258 switch (ring->version) {
259 case TPACKET_V1:
260 test_payload((uint8_t *) ppd.raw + ppd.v1->tp_h.tp_mac,
261 ppd.v1->tp_h.tp_snaplen);
262 total_bytes += ppd.v1->tp_h.tp_snaplen;
263 break;
264
265 case TPACKET_V2:
266 test_payload((uint8_t *) ppd.raw + ppd.v2->tp_h.tp_mac,
267 ppd.v2->tp_h.tp_snaplen);
268 total_bytes += ppd.v2->tp_h.tp_snaplen;
269 break;
270 }
271
272 status_bar_update();
273 total_packets++;
274
275 __v1_v2_rx_user_ready(ppd.raw, ring->version);
276
277 frame_num = (frame_num + 1) % ring->rd_num;
278 }
279
280 poll(&pfd, 1, 1);
281 }
282
283 pair_udp_close(udp_sock);
284
285 if (total_packets != 2 * NUM_PACKETS) {
286 fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
287 ring->version, total_packets, NUM_PACKETS);
288 exit(1);
289 }
290
291 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
292 }
293
__v1_tx_kernel_ready(struct tpacket_hdr * hdr)294 static inline int __v1_tx_kernel_ready(struct tpacket_hdr *hdr)
295 {
296 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
297 }
298
__v1_tx_user_ready(struct tpacket_hdr * hdr)299 static inline void __v1_tx_user_ready(struct tpacket_hdr *hdr)
300 {
301 hdr->tp_status = TP_STATUS_SEND_REQUEST;
302 __sync_synchronize();
303 }
304
__v2_tx_kernel_ready(struct tpacket2_hdr * hdr)305 static inline int __v2_tx_kernel_ready(struct tpacket2_hdr *hdr)
306 {
307 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
308 }
309
__v2_tx_user_ready(struct tpacket2_hdr * hdr)310 static inline void __v2_tx_user_ready(struct tpacket2_hdr *hdr)
311 {
312 hdr->tp_status = TP_STATUS_SEND_REQUEST;
313 __sync_synchronize();
314 }
315
__v1_v2_tx_kernel_ready(void * base,int version)316 static inline int __v1_v2_tx_kernel_ready(void *base, int version)
317 {
318 switch (version) {
319 case TPACKET_V1:
320 return __v1_tx_kernel_ready(base);
321 case TPACKET_V2:
322 return __v2_tx_kernel_ready(base);
323 default:
324 bug_on(1);
325 return 0;
326 }
327 }
328
__v1_v2_tx_user_ready(void * base,int version)329 static inline void __v1_v2_tx_user_ready(void *base, int version)
330 {
331 switch (version) {
332 case TPACKET_V1:
333 __v1_tx_user_ready(base);
334 break;
335 case TPACKET_V2:
336 __v2_tx_user_ready(base);
337 break;
338 }
339 }
340
__v1_v2_set_packet_loss_discard(int sock)341 static void __v1_v2_set_packet_loss_discard(int sock)
342 {
343 int ret, discard = 1;
344
345 ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
346 sizeof(discard));
347 if (ret == -1) {
348 perror("setsockopt");
349 exit(1);
350 }
351 }
352
walk_v1_v2_tx(int sock,struct ring * ring)353 static void walk_v1_v2_tx(int sock, struct ring *ring)
354 {
355 struct pollfd pfd;
356 int rcv_sock, ret;
357 size_t packet_len;
358 union frame_map ppd;
359 char packet[1024];
360 unsigned int frame_num = 0, got = 0;
361 struct sockaddr_ll ll = {
362 .sll_family = PF_PACKET,
363 .sll_halen = ETH_ALEN,
364 };
365
366 bug_on(ring->type != PACKET_TX_RING);
367 bug_on(ring->rd_num < NUM_PACKETS);
368
369 rcv_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
370 if (rcv_sock == -1) {
371 perror("socket");
372 exit(1);
373 }
374
375 pair_udp_setfilter(rcv_sock);
376
377 ll.sll_ifindex = if_nametoindex("lo");
378 ret = bind(rcv_sock, (struct sockaddr *) &ll, sizeof(ll));
379 if (ret == -1) {
380 perror("bind");
381 exit(1);
382 }
383
384 memset(&pfd, 0, sizeof(pfd));
385 pfd.fd = sock;
386 pfd.events = POLLOUT | POLLERR;
387 pfd.revents = 0;
388
389 total_packets = NUM_PACKETS;
390 create_payload(packet, &packet_len);
391
392 while (total_packets > 0) {
393 while (__v1_v2_tx_kernel_ready(ring->rd[frame_num].iov_base,
394 ring->version) &&
395 total_packets > 0) {
396 ppd.raw = ring->rd[frame_num].iov_base;
397
398 switch (ring->version) {
399 case TPACKET_V1:
400 ppd.v1->tp_h.tp_snaplen = packet_len;
401 ppd.v1->tp_h.tp_len = packet_len;
402
403 memcpy((uint8_t *) ppd.raw + TPACKET_HDRLEN -
404 sizeof(struct sockaddr_ll), packet,
405 packet_len);
406 total_bytes += ppd.v1->tp_h.tp_snaplen;
407 break;
408
409 case TPACKET_V2:
410 ppd.v2->tp_h.tp_snaplen = packet_len;
411 ppd.v2->tp_h.tp_len = packet_len;
412
413 memcpy((uint8_t *) ppd.raw + TPACKET2_HDRLEN -
414 sizeof(struct sockaddr_ll), packet,
415 packet_len);
416 total_bytes += ppd.v2->tp_h.tp_snaplen;
417 break;
418 }
419
420 status_bar_update();
421 total_packets--;
422
423 __v1_v2_tx_user_ready(ppd.raw, ring->version);
424
425 frame_num = (frame_num + 1) % ring->rd_num;
426 }
427
428 poll(&pfd, 1, 1);
429 }
430
431 bug_on(total_packets != 0);
432
433 ret = sendto(sock, NULL, 0, 0, NULL, 0);
434 if (ret == -1) {
435 perror("sendto");
436 exit(1);
437 }
438
439 while ((ret = recvfrom(rcv_sock, packet, sizeof(packet),
440 0, NULL, NULL)) > 0 &&
441 total_packets < NUM_PACKETS) {
442 got += ret;
443 test_payload(packet, ret);
444
445 status_bar_update();
446 total_packets++;
447 }
448
449 close(rcv_sock);
450
451 if (total_packets != NUM_PACKETS) {
452 fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
453 ring->version, total_packets, NUM_PACKETS);
454 exit(1);
455 }
456
457 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, got);
458 }
459
walk_v1_v2(int sock,struct ring * ring)460 static void walk_v1_v2(int sock, struct ring *ring)
461 {
462 if (ring->type == PACKET_RX_RING)
463 walk_v1_v2_rx(sock, ring);
464 else
465 walk_v1_v2_tx(sock, ring);
466 }
467
468 static uint64_t __v3_prev_block_seq_num = 0;
469
__v3_test_block_seq_num(struct block_desc * pbd)470 void __v3_test_block_seq_num(struct block_desc *pbd)
471 {
472 if (__v3_prev_block_seq_num + 1 != pbd->h1.seq_num) {
473 fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected "
474 "seq:%"PRIu64" != actual seq:%"PRIu64"\n",
475 __v3_prev_block_seq_num, __v3_prev_block_seq_num + 1,
476 (uint64_t) pbd->h1.seq_num);
477 exit(1);
478 }
479
480 __v3_prev_block_seq_num = pbd->h1.seq_num;
481 }
482
__v3_test_block_len(struct block_desc * pbd,uint32_t bytes,int block_num)483 static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num)
484 {
485 if (pbd->h1.num_pkts && bytes != pbd->h1.blk_len) {
486 fprintf(stderr, "\nblock:%u with %upackets, expected "
487 "len:%u != actual len:%u\n", block_num,
488 pbd->h1.num_pkts, bytes, pbd->h1.blk_len);
489 exit(1);
490 }
491 }
492
__v3_test_block_header(struct block_desc * pbd,const int block_num)493 static void __v3_test_block_header(struct block_desc *pbd, const int block_num)
494 {
495 if ((pbd->h1.block_status & TP_STATUS_USER) == 0) {
496 fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num);
497 exit(1);
498 }
499
500 __v3_test_block_seq_num(pbd);
501 }
502
__v3_walk_block(struct block_desc * pbd,const int block_num)503 static void __v3_walk_block(struct block_desc *pbd, const int block_num)
504 {
505 int num_pkts = pbd->h1.num_pkts, i;
506 unsigned long bytes = 0, bytes_with_padding = ALIGN_8(sizeof(*pbd));
507 struct tpacket3_hdr *ppd;
508
509 __v3_test_block_header(pbd, block_num);
510
511 ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd +
512 pbd->h1.offset_to_first_pkt);
513
514 for (i = 0; i < num_pkts; ++i) {
515 bytes += ppd->tp_snaplen;
516
517 if (ppd->tp_next_offset)
518 bytes_with_padding += ppd->tp_next_offset;
519 else
520 bytes_with_padding += ALIGN_8(ppd->tp_snaplen + ppd->tp_mac);
521
522 test_payload((uint8_t *) ppd + ppd->tp_mac, ppd->tp_snaplen);
523
524 status_bar_update();
525 total_packets++;
526
527 ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
528 __sync_synchronize();
529 }
530
531 __v3_test_block_len(pbd, bytes_with_padding, block_num);
532 total_bytes += bytes;
533 }
534
__v3_flush_block(struct block_desc * pbd)535 void __v3_flush_block(struct block_desc *pbd)
536 {
537 pbd->h1.block_status = TP_STATUS_KERNEL;
538 __sync_synchronize();
539 }
540
walk_v3_rx(int sock,struct ring * ring)541 static void walk_v3_rx(int sock, struct ring *ring)
542 {
543 unsigned int block_num = 0;
544 struct pollfd pfd;
545 struct block_desc *pbd;
546 int udp_sock[2];
547
548 bug_on(ring->type != PACKET_RX_RING);
549
550 pair_udp_open(udp_sock, PORT_BASE);
551 pair_udp_setfilter(sock);
552
553 memset(&pfd, 0, sizeof(pfd));
554 pfd.fd = sock;
555 pfd.events = POLLIN | POLLERR;
556 pfd.revents = 0;
557
558 pair_udp_send(udp_sock, NUM_PACKETS);
559
560 while (total_packets < NUM_PACKETS * 2) {
561 pbd = (struct block_desc *) ring->rd[block_num].iov_base;
562
563 while ((pbd->h1.block_status & TP_STATUS_USER) == 0)
564 poll(&pfd, 1, 1);
565
566 __v3_walk_block(pbd, block_num);
567 __v3_flush_block(pbd);
568
569 block_num = (block_num + 1) % ring->rd_num;
570 }
571
572 pair_udp_close(udp_sock);
573
574 if (total_packets != 2 * NUM_PACKETS) {
575 fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
576 total_packets, NUM_PACKETS);
577 exit(1);
578 }
579
580 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
581 }
582
walk_v3(int sock,struct ring * ring)583 static void walk_v3(int sock, struct ring *ring)
584 {
585 if (ring->type == PACKET_RX_RING)
586 walk_v3_rx(sock, ring);
587 else
588 bug_on(1);
589 }
590
__v1_v2_fill(struct ring * ring,unsigned int blocks)591 static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
592 {
593 ring->req.tp_block_size = getpagesize() << 2;
594 ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
595 ring->req.tp_block_nr = blocks;
596
597 ring->req.tp_frame_nr = ring->req.tp_block_size /
598 ring->req.tp_frame_size *
599 ring->req.tp_block_nr;
600
601 ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
602 ring->walk = walk_v1_v2;
603 ring->rd_num = ring->req.tp_frame_nr;
604 ring->flen = ring->req.tp_frame_size;
605 }
606
__v3_fill(struct ring * ring,unsigned int blocks)607 static void __v3_fill(struct ring *ring, unsigned int blocks)
608 {
609 ring->req3.tp_retire_blk_tov = 64;
610 ring->req3.tp_sizeof_priv = 0;
611 ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
612
613 ring->req3.tp_block_size = getpagesize() << 2;
614 ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
615 ring->req3.tp_block_nr = blocks;
616
617 ring->req3.tp_frame_nr = ring->req3.tp_block_size /
618 ring->req3.tp_frame_size *
619 ring->req3.tp_block_nr;
620
621 ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
622 ring->walk = walk_v3;
623 ring->rd_num = ring->req3.tp_block_nr;
624 ring->flen = ring->req3.tp_block_size;
625 }
626
setup_ring(int sock,struct ring * ring,int version,int type)627 static void setup_ring(int sock, struct ring *ring, int version, int type)
628 {
629 int ret = 0;
630 unsigned int blocks = 256;
631
632 ring->type = type;
633 ring->version = version;
634
635 switch (version) {
636 case TPACKET_V1:
637 case TPACKET_V2:
638 if (type == PACKET_TX_RING)
639 __v1_v2_set_packet_loss_discard(sock);
640 __v1_v2_fill(ring, blocks);
641 ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
642 sizeof(ring->req));
643 break;
644
645 case TPACKET_V3:
646 __v3_fill(ring, blocks);
647 ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
648 sizeof(ring->req3));
649 break;
650 }
651
652 if (ret == -1) {
653 perror("setsockopt");
654 exit(1);
655 }
656
657 ring->rd_len = ring->rd_num * sizeof(*ring->rd);
658 ring->rd = malloc(ring->rd_len);
659 if (ring->rd == NULL) {
660 perror("malloc");
661 exit(1);
662 }
663
664 total_packets = 0;
665 total_bytes = 0;
666 }
667
mmap_ring(int sock,struct ring * ring)668 static void mmap_ring(int sock, struct ring *ring)
669 {
670 int i;
671
672 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
673 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
674 if (ring->mm_space == MAP_FAILED) {
675 perror("mmap");
676 exit(1);
677 }
678
679 memset(ring->rd, 0, ring->rd_len);
680 for (i = 0; i < ring->rd_num; ++i) {
681 ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
682 ring->rd[i].iov_len = ring->flen;
683 }
684 }
685
bind_ring(int sock,struct ring * ring)686 static void bind_ring(int sock, struct ring *ring)
687 {
688 int ret;
689
690 ring->ll.sll_family = PF_PACKET;
691 ring->ll.sll_protocol = htons(ETH_P_ALL);
692 ring->ll.sll_ifindex = if_nametoindex("lo");
693 ring->ll.sll_hatype = 0;
694 ring->ll.sll_pkttype = 0;
695 ring->ll.sll_halen = 0;
696
697 ret = bind(sock, (struct sockaddr *) &ring->ll, sizeof(ring->ll));
698 if (ret == -1) {
699 perror("bind");
700 exit(1);
701 }
702 }
703
walk_ring(int sock,struct ring * ring)704 static void walk_ring(int sock, struct ring *ring)
705 {
706 ring->walk(sock, ring);
707 }
708
unmap_ring(int sock,struct ring * ring)709 static void unmap_ring(int sock, struct ring *ring)
710 {
711 munmap(ring->mm_space, ring->mm_len);
712 free(ring->rd);
713 }
714
test_kernel_bit_width(void)715 static int test_kernel_bit_width(void)
716 {
717 char in[512], *ptr;
718 int num = 0, fd;
719 ssize_t ret;
720
721 fd = open("/proc/kallsyms", O_RDONLY);
722 if (fd == -1) {
723 perror("open");
724 exit(1);
725 }
726
727 ret = read(fd, in, sizeof(in));
728 if (ret <= 0) {
729 perror("read");
730 exit(1);
731 }
732
733 close(fd);
734
735 ptr = in;
736 while(!isspace(*ptr)) {
737 num++;
738 ptr++;
739 }
740
741 return num * 4;
742 }
743
test_user_bit_width(void)744 static int test_user_bit_width(void)
745 {
746 return __WORDSIZE;
747 }
748
749 static const char *tpacket_str[] = {
750 [TPACKET_V1] = "TPACKET_V1",
751 [TPACKET_V2] = "TPACKET_V2",
752 [TPACKET_V3] = "TPACKET_V3",
753 };
754
755 static const char *type_str[] = {
756 [PACKET_RX_RING] = "PACKET_RX_RING",
757 [PACKET_TX_RING] = "PACKET_TX_RING",
758 };
759
test_tpacket(int version,int type)760 static int test_tpacket(int version, int type)
761 {
762 int sock;
763 struct ring ring;
764
765 fprintf(stderr, "test: %s with %s ", tpacket_str[version],
766 type_str[type]);
767 fflush(stderr);
768
769 if (version == TPACKET_V1 &&
770 test_kernel_bit_width() != test_user_bit_width()) {
771 fprintf(stderr, "test: skip %s %s since user and kernel "
772 "space have different bit width\n",
773 tpacket_str[version], type_str[type]);
774 return 0;
775 }
776
777 sock = pfsocket(version);
778 memset(&ring, 0, sizeof(ring));
779 setup_ring(sock, &ring, version, type);
780 mmap_ring(sock, &ring);
781 bind_ring(sock, &ring);
782 walk_ring(sock, &ring);
783 unmap_ring(sock, &ring);
784 close(sock);
785
786 fprintf(stderr, "\n");
787 return 0;
788 }
789
main(void)790 int main(void)
791 {
792 int ret = 0;
793
794 ret |= test_tpacket(TPACKET_V1, PACKET_RX_RING);
795 ret |= test_tpacket(TPACKET_V1, PACKET_TX_RING);
796
797 ret |= test_tpacket(TPACKET_V2, PACKET_RX_RING);
798 ret |= test_tpacket(TPACKET_V2, PACKET_TX_RING);
799
800 ret |= test_tpacket(TPACKET_V3, PACKET_RX_RING);
801
802 if (ret)
803 return 1;
804
805 printf("OK. All tests passed\n");
806 return 0;
807 }
808