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 <sys/utsname.h>
46 #include <linux/if_packet.h>
47 #include <linux/filter.h>
48 #include <ctype.h>
49 #include <fcntl.h>
50 #include <unistd.h>
51 #ifndef __ANDROID__
52 #include <bits/wordsize.h>
53 #endif
54 #include <net/ethernet.h>
55 #include <netinet/ip.h>
56 #include <arpa/inet.h>
57 #include <stdint.h>
58 #include <string.h>
59 #include <assert.h>
60 #include <net/if.h>
61 #include <inttypes.h>
62 #include <poll.h>
63
64 #include "psock_lib.h"
65
66 #ifndef bug_on
67 # define bug_on(cond) assert(!(cond))
68 #endif
69
70 #ifndef __aligned_tpacket
71 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
72 #endif
73
74 #ifndef __align_tpacket
75 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
76 #endif
77
78 #define NUM_PACKETS 100
79 #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1))
80
81 struct ring {
82 struct iovec *rd;
83 uint8_t *mm_space;
84 size_t mm_len, rd_len;
85 struct sockaddr_ll ll;
86 void (*walk)(int sock, struct ring *ring);
87 int type, rd_num, flen, version;
88 union {
89 struct tpacket_req req;
90 struct tpacket_req3 req3;
91 };
92 };
93
94 struct block_desc {
95 uint32_t version;
96 uint32_t offset_to_priv;
97 struct tpacket_hdr_v1 h1;
98 };
99
100 union frame_map {
101 struct {
102 struct tpacket_hdr tp_h __aligned_tpacket;
103 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket_hdr));
104 } *v1;
105 struct {
106 struct tpacket2_hdr tp_h __aligned_tpacket;
107 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
108 } *v2;
109 void *raw;
110 };
111
112 static unsigned int total_packets, total_bytes;
113
pfsocket(int ver)114 static int pfsocket(int ver)
115 {
116 int ret, sock = socket(PF_PACKET, SOCK_RAW, 0);
117 if (sock == -1) {
118 perror("socket");
119 exit(1);
120 }
121
122 ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
123 if (ret == -1) {
124 perror("setsockopt");
125 exit(1);
126 }
127
128 return sock;
129 }
130
status_bar_update(void)131 static void status_bar_update(void)
132 {
133 if (total_packets % 10 == 0) {
134 fprintf(stderr, ".");
135 fflush(stderr);
136 }
137 }
138
test_payload(void * pay,size_t len)139 static void test_payload(void *pay, size_t len)
140 {
141 struct ethhdr *eth = pay;
142
143 if (len < sizeof(struct ethhdr)) {
144 fprintf(stderr, "test_payload: packet too "
145 "small: %zu bytes!\n", len);
146 exit(1);
147 }
148
149 if (eth->h_proto != htons(ETH_P_IP)) {
150 fprintf(stderr, "test_payload: wrong ethernet "
151 "type: 0x%x!\n", ntohs(eth->h_proto));
152 exit(1);
153 }
154 }
155
create_payload(void * pay,size_t * len)156 static void create_payload(void *pay, size_t *len)
157 {
158 int i;
159 struct ethhdr *eth = pay;
160 struct iphdr *ip = pay + sizeof(*eth);
161
162 /* Lets create some broken crap, that still passes
163 * our BPF filter.
164 */
165
166 *len = DATA_LEN + 42;
167
168 memset(pay, 0xff, ETH_ALEN * 2);
169 eth->h_proto = htons(ETH_P_IP);
170
171 for (i = 0; i < sizeof(*ip); ++i)
172 ((uint8_t *) pay)[i + sizeof(*eth)] = (uint8_t) rand();
173
174 ip->ihl = 5;
175 ip->version = 4;
176 ip->protocol = 0x11;
177 ip->frag_off = 0;
178 ip->ttl = 64;
179 ip->tot_len = htons((uint16_t) *len - sizeof(*eth));
180
181 ip->saddr = htonl(INADDR_LOOPBACK);
182 ip->daddr = htonl(INADDR_LOOPBACK);
183
184 memset(pay + sizeof(*eth) + sizeof(*ip),
185 DATA_CHAR, DATA_LEN);
186 }
187
__v1_rx_kernel_ready(struct tpacket_hdr * hdr)188 static inline int __v1_rx_kernel_ready(struct tpacket_hdr *hdr)
189 {
190 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
191 }
192
__v1_rx_user_ready(struct tpacket_hdr * hdr)193 static inline void __v1_rx_user_ready(struct tpacket_hdr *hdr)
194 {
195 hdr->tp_status = TP_STATUS_KERNEL;
196 __sync_synchronize();
197 }
198
__v2_rx_kernel_ready(struct tpacket2_hdr * hdr)199 static inline int __v2_rx_kernel_ready(struct tpacket2_hdr *hdr)
200 {
201 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
202 }
203
__v2_rx_user_ready(struct tpacket2_hdr * hdr)204 static inline void __v2_rx_user_ready(struct tpacket2_hdr *hdr)
205 {
206 hdr->tp_status = TP_STATUS_KERNEL;
207 __sync_synchronize();
208 }
209
__v1_v2_rx_kernel_ready(void * base,int version)210 static inline int __v1_v2_rx_kernel_ready(void *base, int version)
211 {
212 switch (version) {
213 case TPACKET_V1:
214 return __v1_rx_kernel_ready(base);
215 case TPACKET_V2:
216 return __v2_rx_kernel_ready(base);
217 default:
218 bug_on(1);
219 return 0;
220 }
221 }
222
__v1_v2_rx_user_ready(void * base,int version)223 static inline void __v1_v2_rx_user_ready(void *base, int version)
224 {
225 switch (version) {
226 case TPACKET_V1:
227 __v1_rx_user_ready(base);
228 break;
229 case TPACKET_V2:
230 __v2_rx_user_ready(base);
231 break;
232 }
233 }
234
walk_v1_v2_rx(int sock,struct ring * ring)235 static void walk_v1_v2_rx(int sock, struct ring *ring)
236 {
237 struct pollfd pfd;
238 int udp_sock[2];
239 union frame_map ppd;
240 unsigned int frame_num = 0;
241
242 bug_on(ring->type != PACKET_RX_RING);
243
244 pair_udp_open(udp_sock, PORT_BASE);
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
__v3_tx_kernel_ready(struct tpacket3_hdr * hdr)316 static inline int __v3_tx_kernel_ready(struct tpacket3_hdr *hdr)
317 {
318 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
319 }
320
__v3_tx_user_ready(struct tpacket3_hdr * hdr)321 static inline void __v3_tx_user_ready(struct tpacket3_hdr *hdr)
322 {
323 hdr->tp_status = TP_STATUS_SEND_REQUEST;
324 __sync_synchronize();
325 }
326
__tx_kernel_ready(void * base,int version)327 static inline int __tx_kernel_ready(void *base, int version)
328 {
329 switch (version) {
330 case TPACKET_V1:
331 return __v1_tx_kernel_ready(base);
332 case TPACKET_V2:
333 return __v2_tx_kernel_ready(base);
334 case TPACKET_V3:
335 return __v3_tx_kernel_ready(base);
336 default:
337 bug_on(1);
338 return 0;
339 }
340 }
341
__tx_user_ready(void * base,int version)342 static inline void __tx_user_ready(void *base, int version)
343 {
344 switch (version) {
345 case TPACKET_V1:
346 __v1_tx_user_ready(base);
347 break;
348 case TPACKET_V2:
349 __v2_tx_user_ready(base);
350 break;
351 case TPACKET_V3:
352 __v3_tx_user_ready(base);
353 break;
354 }
355 }
356
__v1_v2_set_packet_loss_discard(int sock)357 static void __v1_v2_set_packet_loss_discard(int sock)
358 {
359 int ret, discard = 1;
360
361 ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
362 sizeof(discard));
363 if (ret == -1) {
364 perror("setsockopt");
365 exit(1);
366 }
367 }
368
get_next_frame(struct ring * ring,int n)369 static inline void *get_next_frame(struct ring *ring, int n)
370 {
371 uint8_t *f0 = ring->rd[0].iov_base;
372
373 switch (ring->version) {
374 case TPACKET_V1:
375 case TPACKET_V2:
376 return ring->rd[n].iov_base;
377 case TPACKET_V3:
378 return f0 + (n * ring->req3.tp_frame_size);
379 default:
380 bug_on(1);
381 return NULL;
382 }
383 }
384
walk_tx(int sock,struct ring * ring)385 static void walk_tx(int sock, struct ring *ring)
386 {
387 struct pollfd pfd;
388 int rcv_sock, ret;
389 size_t packet_len;
390 union frame_map ppd;
391 char packet[1024];
392 unsigned int frame_num = 0, got = 0;
393 struct sockaddr_ll ll = {
394 .sll_family = PF_PACKET,
395 .sll_halen = ETH_ALEN,
396 };
397 int nframes;
398
399 /* TPACKET_V{1,2} sets up the ring->rd* related variables based
400 * on frames (e.g., rd_num is tp_frame_nr) whereas V3 sets these
401 * up based on blocks (e.g, rd_num is tp_block_nr)
402 */
403 if (ring->version <= TPACKET_V2)
404 nframes = ring->rd_num;
405 else
406 nframes = ring->req3.tp_frame_nr;
407
408 bug_on(ring->type != PACKET_TX_RING);
409 bug_on(nframes < NUM_PACKETS);
410
411 rcv_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
412 if (rcv_sock == -1) {
413 perror("socket");
414 exit(1);
415 }
416
417 pair_udp_setfilter(rcv_sock);
418
419 ll.sll_ifindex = if_nametoindex("lo");
420 ret = bind(rcv_sock, (struct sockaddr *) &ll, sizeof(ll));
421 if (ret == -1) {
422 perror("bind");
423 exit(1);
424 }
425
426 memset(&pfd, 0, sizeof(pfd));
427 pfd.fd = sock;
428 pfd.events = POLLOUT | POLLERR;
429 pfd.revents = 0;
430
431 total_packets = NUM_PACKETS;
432 create_payload(packet, &packet_len);
433
434 while (total_packets > 0) {
435 void *next = get_next_frame(ring, frame_num);
436
437 while (__tx_kernel_ready(next, ring->version) &&
438 total_packets > 0) {
439 ppd.raw = next;
440
441 switch (ring->version) {
442 case TPACKET_V1:
443 ppd.v1->tp_h.tp_snaplen = packet_len;
444 ppd.v1->tp_h.tp_len = packet_len;
445
446 memcpy((uint8_t *) ppd.raw + TPACKET_HDRLEN -
447 sizeof(struct sockaddr_ll), packet,
448 packet_len);
449 total_bytes += ppd.v1->tp_h.tp_snaplen;
450 break;
451
452 case TPACKET_V2:
453 ppd.v2->tp_h.tp_snaplen = packet_len;
454 ppd.v2->tp_h.tp_len = packet_len;
455
456 memcpy((uint8_t *) ppd.raw + TPACKET2_HDRLEN -
457 sizeof(struct sockaddr_ll), packet,
458 packet_len);
459 total_bytes += ppd.v2->tp_h.tp_snaplen;
460 break;
461 case TPACKET_V3: {
462 struct tpacket3_hdr *tx = next;
463
464 tx->tp_snaplen = packet_len;
465 tx->tp_len = packet_len;
466 tx->tp_next_offset = 0;
467
468 memcpy((uint8_t *)tx + TPACKET3_HDRLEN -
469 sizeof(struct sockaddr_ll), packet,
470 packet_len);
471 total_bytes += tx->tp_snaplen;
472 break;
473 }
474 }
475
476 status_bar_update();
477 total_packets--;
478
479 __tx_user_ready(next, ring->version);
480
481 frame_num = (frame_num + 1) % nframes;
482 }
483
484 poll(&pfd, 1, 1);
485 }
486
487 bug_on(total_packets != 0);
488
489 ret = sendto(sock, NULL, 0, 0, NULL, 0);
490 if (ret == -1) {
491 perror("sendto");
492 exit(1);
493 }
494
495 while ((ret = recvfrom(rcv_sock, packet, sizeof(packet),
496 0, NULL, NULL)) > 0 &&
497 total_packets < NUM_PACKETS) {
498 got += ret;
499 test_payload(packet, ret);
500
501 status_bar_update();
502 total_packets++;
503 }
504
505 close(rcv_sock);
506
507 if (total_packets != NUM_PACKETS) {
508 fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
509 ring->version, total_packets, NUM_PACKETS);
510 exit(1);
511 }
512
513 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, got);
514 }
515
walk_v1_v2(int sock,struct ring * ring)516 static void walk_v1_v2(int sock, struct ring *ring)
517 {
518 if (ring->type == PACKET_RX_RING)
519 walk_v1_v2_rx(sock, ring);
520 else
521 walk_tx(sock, ring);
522 }
523
524 static uint64_t __v3_prev_block_seq_num = 0;
525
__v3_test_block_seq_num(struct block_desc * pbd)526 void __v3_test_block_seq_num(struct block_desc *pbd)
527 {
528 if (__v3_prev_block_seq_num + 1 != pbd->h1.seq_num) {
529 fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected "
530 "seq:%"PRIu64" != actual seq:%"PRIu64"\n",
531 __v3_prev_block_seq_num, __v3_prev_block_seq_num + 1,
532 (uint64_t) pbd->h1.seq_num);
533 exit(1);
534 }
535
536 __v3_prev_block_seq_num = pbd->h1.seq_num;
537 }
538
__v3_test_block_len(struct block_desc * pbd,uint32_t bytes,int block_num)539 static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num)
540 {
541 if (pbd->h1.num_pkts && bytes != pbd->h1.blk_len) {
542 fprintf(stderr, "\nblock:%u with %upackets, expected "
543 "len:%u != actual len:%u\n", block_num,
544 pbd->h1.num_pkts, bytes, pbd->h1.blk_len);
545 exit(1);
546 }
547 }
548
__v3_test_block_header(struct block_desc * pbd,const int block_num)549 static void __v3_test_block_header(struct block_desc *pbd, const int block_num)
550 {
551 if ((pbd->h1.block_status & TP_STATUS_USER) == 0) {
552 fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num);
553 exit(1);
554 }
555
556 __v3_test_block_seq_num(pbd);
557 }
558
__v3_walk_block(struct block_desc * pbd,const int block_num)559 static void __v3_walk_block(struct block_desc *pbd, const int block_num)
560 {
561 int num_pkts = pbd->h1.num_pkts, i;
562 unsigned long bytes = 0, bytes_with_padding = ALIGN_8(sizeof(*pbd));
563 struct tpacket3_hdr *ppd;
564
565 __v3_test_block_header(pbd, block_num);
566
567 ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd +
568 pbd->h1.offset_to_first_pkt);
569
570 for (i = 0; i < num_pkts; ++i) {
571 bytes += ppd->tp_snaplen;
572
573 if (ppd->tp_next_offset)
574 bytes_with_padding += ppd->tp_next_offset;
575 else
576 bytes_with_padding += ALIGN_8(ppd->tp_snaplen + ppd->tp_mac);
577
578 test_payload((uint8_t *) ppd + ppd->tp_mac, ppd->tp_snaplen);
579
580 status_bar_update();
581 total_packets++;
582
583 ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
584 __sync_synchronize();
585 }
586
587 __v3_test_block_len(pbd, bytes_with_padding, block_num);
588 total_bytes += bytes;
589 }
590
__v3_flush_block(struct block_desc * pbd)591 void __v3_flush_block(struct block_desc *pbd)
592 {
593 pbd->h1.block_status = TP_STATUS_KERNEL;
594 __sync_synchronize();
595 }
596
walk_v3_rx(int sock,struct ring * ring)597 static void walk_v3_rx(int sock, struct ring *ring)
598 {
599 unsigned int block_num = 0;
600 struct pollfd pfd;
601 struct block_desc *pbd;
602 int udp_sock[2];
603
604 bug_on(ring->type != PACKET_RX_RING);
605
606 pair_udp_open(udp_sock, PORT_BASE);
607
608 memset(&pfd, 0, sizeof(pfd));
609 pfd.fd = sock;
610 pfd.events = POLLIN | POLLERR;
611 pfd.revents = 0;
612
613 pair_udp_send(udp_sock, NUM_PACKETS);
614
615 while (total_packets < NUM_PACKETS * 2) {
616 pbd = (struct block_desc *) ring->rd[block_num].iov_base;
617
618 while ((pbd->h1.block_status & TP_STATUS_USER) == 0)
619 poll(&pfd, 1, 1);
620
621 __v3_walk_block(pbd, block_num);
622 __v3_flush_block(pbd);
623
624 block_num = (block_num + 1) % ring->rd_num;
625 }
626
627 pair_udp_close(udp_sock);
628
629 if (total_packets != 2 * NUM_PACKETS) {
630 fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
631 total_packets, NUM_PACKETS);
632 exit(1);
633 }
634
635 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
636 }
637
walk_v3(int sock,struct ring * ring)638 static void walk_v3(int sock, struct ring *ring)
639 {
640 if (ring->type == PACKET_RX_RING)
641 walk_v3_rx(sock, ring);
642 else
643 walk_tx(sock, ring);
644 }
645
__v1_v2_fill(struct ring * ring,unsigned int blocks)646 static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
647 {
648 ring->req.tp_block_size = getpagesize() << 2;
649 ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
650 ring->req.tp_block_nr = blocks;
651
652 ring->req.tp_frame_nr = ring->req.tp_block_size /
653 ring->req.tp_frame_size *
654 ring->req.tp_block_nr;
655
656 ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
657 ring->walk = walk_v1_v2;
658 ring->rd_num = ring->req.tp_frame_nr;
659 ring->flen = ring->req.tp_frame_size;
660 }
661
__v3_fill(struct ring * ring,unsigned int blocks,int type)662 static void __v3_fill(struct ring *ring, unsigned int blocks, int type)
663 {
664 if (type == PACKET_RX_RING) {
665 ring->req3.tp_retire_blk_tov = 64;
666 ring->req3.tp_sizeof_priv = 0;
667 ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
668 }
669 ring->req3.tp_block_size = getpagesize() << 2;
670 ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
671 ring->req3.tp_block_nr = blocks;
672
673 ring->req3.tp_frame_nr = ring->req3.tp_block_size /
674 ring->req3.tp_frame_size *
675 ring->req3.tp_block_nr;
676
677 ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
678 ring->walk = walk_v3;
679 ring->rd_num = ring->req3.tp_block_nr;
680 ring->flen = ring->req3.tp_block_size;
681 }
682
setup_ring(int sock,struct ring * ring,int version,int type)683 static void setup_ring(int sock, struct ring *ring, int version, int type)
684 {
685 int ret = 0;
686 unsigned int blocks = 256;
687
688 ring->type = type;
689 ring->version = version;
690
691 switch (version) {
692 case TPACKET_V1:
693 case TPACKET_V2:
694 if (type == PACKET_TX_RING)
695 __v1_v2_set_packet_loss_discard(sock);
696 __v1_v2_fill(ring, blocks);
697 ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
698 sizeof(ring->req));
699 break;
700
701 case TPACKET_V3:
702 __v3_fill(ring, blocks, type);
703 ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
704 sizeof(ring->req3));
705 break;
706 }
707
708 if (ret == -1) {
709 perror("setsockopt");
710 exit(1);
711 }
712
713 ring->rd_len = ring->rd_num * sizeof(*ring->rd);
714 ring->rd = malloc(ring->rd_len);
715 if (ring->rd == NULL) {
716 perror("malloc");
717 exit(1);
718 }
719
720 total_packets = 0;
721 total_bytes = 0;
722 }
723
mmap_ring(int sock,struct ring * ring)724 static void mmap_ring(int sock, struct ring *ring)
725 {
726 int i;
727
728 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
729 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
730 if (ring->mm_space == MAP_FAILED) {
731 perror("mmap");
732 exit(1);
733 }
734
735 memset(ring->rd, 0, ring->rd_len);
736 for (i = 0; i < ring->rd_num; ++i) {
737 ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
738 ring->rd[i].iov_len = ring->flen;
739 }
740 }
741
bind_ring(int sock,struct ring * ring)742 static void bind_ring(int sock, struct ring *ring)
743 {
744 int ret;
745
746 pair_udp_setfilter(sock);
747
748 ring->ll.sll_family = PF_PACKET;
749 ring->ll.sll_protocol = htons(ETH_P_ALL);
750 ring->ll.sll_ifindex = if_nametoindex("lo");
751 ring->ll.sll_hatype = 0;
752 ring->ll.sll_pkttype = 0;
753 ring->ll.sll_halen = 0;
754
755 ret = bind(sock, (struct sockaddr *) &ring->ll, sizeof(ring->ll));
756 if (ret == -1) {
757 perror("bind");
758 exit(1);
759 }
760 }
761
walk_ring(int sock,struct ring * ring)762 static void walk_ring(int sock, struct ring *ring)
763 {
764 ring->walk(sock, ring);
765 }
766
unmap_ring(int sock,struct ring * ring)767 static void unmap_ring(int sock, struct ring *ring)
768 {
769 munmap(ring->mm_space, ring->mm_len);
770 free(ring->rd);
771 }
772
test_kernel_bit_width(void)773 static int test_kernel_bit_width(void)
774 {
775 char in[512], *ptr;
776 int num = 0, fd;
777 ssize_t ret;
778
779 fd = open("/proc/kallsyms", O_RDONLY);
780 if (fd == -1) {
781 perror("open");
782 exit(1);
783 }
784
785 ret = read(fd, in, sizeof(in));
786 if (ret <= 0) {
787 perror("read");
788 exit(1);
789 }
790
791 close(fd);
792
793 ptr = in;
794 while(!isspace(*ptr)) {
795 num++;
796 ptr++;
797 }
798
799 return num * 4;
800 }
801
test_user_bit_width(void)802 static int test_user_bit_width(void)
803 {
804 return __WORDSIZE;
805 }
806
807 static const char *tpacket_str[] = {
808 [TPACKET_V1] = "TPACKET_V1",
809 [TPACKET_V2] = "TPACKET_V2",
810 [TPACKET_V3] = "TPACKET_V3",
811 };
812
813 static const char *type_str[] = {
814 [PACKET_RX_RING] = "PACKET_RX_RING",
815 [PACKET_TX_RING] = "PACKET_TX_RING",
816 };
817
test_tpacket(int version,int type)818 static int test_tpacket(int version, int type)
819 {
820 int sock;
821 struct ring ring;
822
823 fprintf(stderr, "test: %s with %s ", tpacket_str[version],
824 type_str[type]);
825 fflush(stderr);
826
827 if (version == TPACKET_V1 &&
828 test_kernel_bit_width() != test_user_bit_width()) {
829 fprintf(stderr, "test: skip %s %s since user and kernel "
830 "space have different bit width\n",
831 tpacket_str[version], type_str[type]);
832 return 0;
833 }
834
835 sock = pfsocket(version);
836 memset(&ring, 0, sizeof(ring));
837 setup_ring(sock, &ring, version, type);
838 mmap_ring(sock, &ring);
839 bind_ring(sock, &ring);
840 walk_ring(sock, &ring);
841 unmap_ring(sock, &ring);
842 close(sock);
843
844 fprintf(stderr, "\n");
845 return 0;
846 }
847
get_kernel_version(int * version,int * patchlevel)848 void get_kernel_version(int *version, int *patchlevel)
849 {
850 int ret, sublevel;
851 struct utsname utsname;
852
853 ret = uname(&utsname);
854 if (ret) {
855 perror("uname");
856 exit(1);
857 }
858
859 ret = sscanf(utsname.release, "%d.%d.%d", version, patchlevel,
860 &sublevel);
861 if (ret < 0) {
862 perror("sscanf");
863 exit(1);
864 } else if (ret != 3) {
865 printf("Malformed kernel version %s\n", &utsname.release);
866 exit(1);
867 }
868 }
869
main(void)870 int main(void)
871 {
872 int ret = 0;
873 int version, patchlevel;
874
875 get_kernel_version(&version, &patchlevel);
876
877 ret |= test_tpacket(TPACKET_V1, PACKET_RX_RING);
878 ret |= test_tpacket(TPACKET_V1, PACKET_TX_RING);
879
880 ret |= test_tpacket(TPACKET_V2, PACKET_RX_RING);
881 ret |= test_tpacket(TPACKET_V2, PACKET_TX_RING);
882
883 ret |= test_tpacket(TPACKET_V3, PACKET_RX_RING);
884 if (version > 4 || (version == 4 && patchlevel >= 11))
885 ret |= test_tpacket(TPACKET_V3, PACKET_TX_RING);
886
887 if (ret)
888 return 1;
889
890 printf("OK. All tests passed\n");
891 return 0;
892 }
893