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