1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2013 Red Hat, Inc.
4 * Author: Daniel Borkmann <dborkman@redhat.com>
5 * Chetan Loke <loke.chetan@gmail.com> (TPACKET_V3 usage example)
7 * A basic test of packet socket's TPACKET_V1/TPACKET_V2/TPACKET_V3 behavior.
10 * Test the setup of the TPACKET socket with different patterns that are
11 * known to fail (TODO) resp. succeed (OK).
14 * Open a pair of packet sockets and send resp. receive an a priori known
15 * packet pattern accross the sockets and check if it was received resp.
16 * sent correctly. Fanout in combination with RX_RING is currently not
19 * The test currently runs for
20 * - TPACKET_V1: RX_RING, TX_RING
21 * - TPACKET_V2: RX_RING, TX_RING
22 * - TPACKET_V3: RX_RING
27 #include <sys/types.h>
29 #include <sys/socket.h>
31 #include <linux/if_packet.h>
32 #include <linux/filter.h>
36 #include <bits/wordsize.h>
37 #include <net/ethernet.h>
38 #include <netinet/ip.h>
39 #include <arpa/inet.h>
47 #include "psock_lib.h"
49 #include "../kselftest.h"
52 # define bug_on(cond) assert(!(cond))
55 #ifndef __aligned_tpacket
56 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
59 #ifndef __align_tpacket
60 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
63 #define NUM_PACKETS 100
64 #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1))
69 size_t mm_len, rd_len;
70 struct sockaddr_ll ll;
71 void (*walk)(int sock, struct ring *ring);
72 int type, rd_num, flen, version;
74 struct tpacket_req req;
75 struct tpacket_req3 req3;
81 uint32_t offset_to_priv;
82 struct tpacket_hdr_v1 h1;
87 struct tpacket_hdr tp_h __aligned_tpacket;
88 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket_hdr));
91 struct tpacket2_hdr tp_h __aligned_tpacket;
92 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
97 static unsigned int total_packets, total_bytes;
99 static int pfsocket(int ver)
101 int ret, sock = socket(PF_PACKET, SOCK_RAW, 0);
107 ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
109 perror("setsockopt");
116 static void status_bar_update(void)
118 if (total_packets % 10 == 0) {
119 fprintf(stderr, ".");
124 static void test_payload(void *pay, size_t len)
126 struct ethhdr *eth = pay;
128 if (len < sizeof(struct ethhdr)) {
129 fprintf(stderr, "test_payload: packet too "
130 "small: %zu bytes!\n", len);
134 if (eth->h_proto != htons(ETH_P_IP)) {
135 fprintf(stderr, "test_payload: wrong ethernet "
136 "type: 0x%x!\n", ntohs(eth->h_proto));
141 static void create_payload(void *pay, size_t *len)
144 struct ethhdr *eth = pay;
145 struct iphdr *ip = pay + sizeof(*eth);
147 /* Lets create some broken crap, that still passes
151 *len = DATA_LEN + 42;
153 memset(pay, 0xff, ETH_ALEN * 2);
154 eth->h_proto = htons(ETH_P_IP);
156 for (i = 0; i < sizeof(*ip); ++i)
157 ((uint8_t *) pay)[i + sizeof(*eth)] = (uint8_t) rand();
164 ip->tot_len = htons((uint16_t) *len - sizeof(*eth));
166 ip->saddr = htonl(INADDR_LOOPBACK);
167 ip->daddr = htonl(INADDR_LOOPBACK);
169 memset(pay + sizeof(*eth) + sizeof(*ip),
170 DATA_CHAR, DATA_LEN);
173 static inline int __v1_rx_kernel_ready(struct tpacket_hdr *hdr)
175 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
178 static inline void __v1_rx_user_ready(struct tpacket_hdr *hdr)
180 hdr->tp_status = TP_STATUS_KERNEL;
181 __sync_synchronize();
184 static inline int __v2_rx_kernel_ready(struct tpacket2_hdr *hdr)
186 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
189 static inline void __v2_rx_user_ready(struct tpacket2_hdr *hdr)
191 hdr->tp_status = TP_STATUS_KERNEL;
192 __sync_synchronize();
195 static inline int __v1_v2_rx_kernel_ready(void *base, int version)
199 return __v1_rx_kernel_ready(base);
201 return __v2_rx_kernel_ready(base);
208 static inline void __v1_v2_rx_user_ready(void *base, int version)
212 __v1_rx_user_ready(base);
215 __v2_rx_user_ready(base);
220 static void walk_v1_v2_rx(int sock, struct ring *ring)
225 unsigned int frame_num = 0;
227 bug_on(ring->type != PACKET_RX_RING);
229 pair_udp_open(udp_sock, PORT_BASE);
231 memset(&pfd, 0, sizeof(pfd));
233 pfd.events = POLLIN | POLLERR;
236 pair_udp_send(udp_sock, NUM_PACKETS);
238 while (total_packets < NUM_PACKETS * 2) {
239 while (__v1_v2_rx_kernel_ready(ring->rd[frame_num].iov_base,
241 ppd.raw = ring->rd[frame_num].iov_base;
243 switch (ring->version) {
245 test_payload((uint8_t *) ppd.raw + ppd.v1->tp_h.tp_mac,
246 ppd.v1->tp_h.tp_snaplen);
247 total_bytes += ppd.v1->tp_h.tp_snaplen;
251 test_payload((uint8_t *) ppd.raw + ppd.v2->tp_h.tp_mac,
252 ppd.v2->tp_h.tp_snaplen);
253 total_bytes += ppd.v2->tp_h.tp_snaplen;
260 __v1_v2_rx_user_ready(ppd.raw, ring->version);
262 frame_num = (frame_num + 1) % ring->rd_num;
268 pair_udp_close(udp_sock);
270 if (total_packets != 2 * NUM_PACKETS) {
271 fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
272 ring->version, total_packets, NUM_PACKETS);
276 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
279 static inline int __v1_tx_kernel_ready(struct tpacket_hdr *hdr)
281 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
284 static inline void __v1_tx_user_ready(struct tpacket_hdr *hdr)
286 hdr->tp_status = TP_STATUS_SEND_REQUEST;
287 __sync_synchronize();
290 static inline int __v2_tx_kernel_ready(struct tpacket2_hdr *hdr)
292 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
295 static inline void __v2_tx_user_ready(struct tpacket2_hdr *hdr)
297 hdr->tp_status = TP_STATUS_SEND_REQUEST;
298 __sync_synchronize();
301 static inline int __v3_tx_kernel_ready(struct tpacket3_hdr *hdr)
303 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
306 static inline void __v3_tx_user_ready(struct tpacket3_hdr *hdr)
308 hdr->tp_status = TP_STATUS_SEND_REQUEST;
309 __sync_synchronize();
312 static inline int __tx_kernel_ready(void *base, int version)
316 return __v1_tx_kernel_ready(base);
318 return __v2_tx_kernel_ready(base);
320 return __v3_tx_kernel_ready(base);
327 static inline void __tx_user_ready(void *base, int version)
331 __v1_tx_user_ready(base);
334 __v2_tx_user_ready(base);
337 __v3_tx_user_ready(base);
342 static void __v1_v2_set_packet_loss_discard(int sock)
344 int ret, discard = 1;
346 ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
349 perror("setsockopt");
354 static inline void *get_next_frame(struct ring *ring, int n)
356 uint8_t *f0 = ring->rd[0].iov_base;
358 switch (ring->version) {
361 return ring->rd[n].iov_base;
363 return f0 + (n * ring->req3.tp_frame_size);
369 static void walk_tx(int sock, struct ring *ring)
376 unsigned int frame_num = 0, got = 0;
377 struct sockaddr_ll ll = {
378 .sll_family = PF_PACKET,
379 .sll_halen = ETH_ALEN,
383 /* TPACKET_V{1,2} sets up the ring->rd* related variables based
384 * on frames (e.g., rd_num is tp_frame_nr) whereas V3 sets these
385 * up based on blocks (e.g, rd_num is tp_block_nr)
387 if (ring->version <= TPACKET_V2)
388 nframes = ring->rd_num;
390 nframes = ring->req3.tp_frame_nr;
392 bug_on(ring->type != PACKET_TX_RING);
393 bug_on(nframes < NUM_PACKETS);
395 rcv_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
396 if (rcv_sock == -1) {
401 pair_udp_setfilter(rcv_sock);
403 ll.sll_ifindex = if_nametoindex("lo");
404 ret = bind(rcv_sock, (struct sockaddr *) &ll, sizeof(ll));
410 memset(&pfd, 0, sizeof(pfd));
412 pfd.events = POLLOUT | POLLERR;
415 total_packets = NUM_PACKETS;
416 create_payload(packet, &packet_len);
418 while (total_packets > 0) {
419 void *next = get_next_frame(ring, frame_num);
421 while (__tx_kernel_ready(next, ring->version) &&
425 switch (ring->version) {
427 ppd.v1->tp_h.tp_snaplen = packet_len;
428 ppd.v1->tp_h.tp_len = packet_len;
430 memcpy((uint8_t *) ppd.raw + TPACKET_HDRLEN -
431 sizeof(struct sockaddr_ll), packet,
433 total_bytes += ppd.v1->tp_h.tp_snaplen;
437 ppd.v2->tp_h.tp_snaplen = packet_len;
438 ppd.v2->tp_h.tp_len = packet_len;
440 memcpy((uint8_t *) ppd.raw + TPACKET2_HDRLEN -
441 sizeof(struct sockaddr_ll), packet,
443 total_bytes += ppd.v2->tp_h.tp_snaplen;
446 struct tpacket3_hdr *tx = next;
448 tx->tp_snaplen = packet_len;
449 tx->tp_len = packet_len;
450 tx->tp_next_offset = 0;
452 memcpy((uint8_t *)tx + TPACKET3_HDRLEN -
453 sizeof(struct sockaddr_ll), packet,
455 total_bytes += tx->tp_snaplen;
463 __tx_user_ready(next, ring->version);
465 frame_num = (frame_num + 1) % nframes;
471 bug_on(total_packets != 0);
473 ret = sendto(sock, NULL, 0, 0, NULL, 0);
479 while ((ret = recvfrom(rcv_sock, packet, sizeof(packet),
480 0, NULL, NULL)) > 0 &&
481 total_packets < NUM_PACKETS) {
483 test_payload(packet, ret);
491 if (total_packets != NUM_PACKETS) {
492 fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
493 ring->version, total_packets, NUM_PACKETS);
497 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, got);
500 static void walk_v1_v2(int sock, struct ring *ring)
502 if (ring->type == PACKET_RX_RING)
503 walk_v1_v2_rx(sock, ring);
508 static uint64_t __v3_prev_block_seq_num = 0;
510 void __v3_test_block_seq_num(struct block_desc *pbd)
512 if (__v3_prev_block_seq_num + 1 != pbd->h1.seq_num) {
513 fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected "
514 "seq:%"PRIu64" != actual seq:%"PRIu64"\n",
515 __v3_prev_block_seq_num, __v3_prev_block_seq_num + 1,
516 (uint64_t) pbd->h1.seq_num);
520 __v3_prev_block_seq_num = pbd->h1.seq_num;
523 static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num)
525 if (pbd->h1.num_pkts && bytes != pbd->h1.blk_len) {
526 fprintf(stderr, "\nblock:%u with %upackets, expected "
527 "len:%u != actual len:%u\n", block_num,
528 pbd->h1.num_pkts, bytes, pbd->h1.blk_len);
533 static void __v3_test_block_header(struct block_desc *pbd, const int block_num)
535 if ((pbd->h1.block_status & TP_STATUS_USER) == 0) {
536 fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num);
540 __v3_test_block_seq_num(pbd);
543 static void __v3_walk_block(struct block_desc *pbd, const int block_num)
545 int num_pkts = pbd->h1.num_pkts, i;
546 unsigned long bytes = 0, bytes_with_padding = ALIGN_8(sizeof(*pbd));
547 struct tpacket3_hdr *ppd;
549 __v3_test_block_header(pbd, block_num);
551 ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd +
552 pbd->h1.offset_to_first_pkt);
554 for (i = 0; i < num_pkts; ++i) {
555 bytes += ppd->tp_snaplen;
557 if (ppd->tp_next_offset)
558 bytes_with_padding += ppd->tp_next_offset;
560 bytes_with_padding += ALIGN_8(ppd->tp_snaplen + ppd->tp_mac);
562 test_payload((uint8_t *) ppd + ppd->tp_mac, ppd->tp_snaplen);
567 ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
568 __sync_synchronize();
571 __v3_test_block_len(pbd, bytes_with_padding, block_num);
572 total_bytes += bytes;
575 void __v3_flush_block(struct block_desc *pbd)
577 pbd->h1.block_status = TP_STATUS_KERNEL;
578 __sync_synchronize();
581 static void walk_v3_rx(int sock, struct ring *ring)
583 unsigned int block_num = 0;
585 struct block_desc *pbd;
588 bug_on(ring->type != PACKET_RX_RING);
590 pair_udp_open(udp_sock, PORT_BASE);
592 memset(&pfd, 0, sizeof(pfd));
594 pfd.events = POLLIN | POLLERR;
597 pair_udp_send(udp_sock, NUM_PACKETS);
599 while (total_packets < NUM_PACKETS * 2) {
600 pbd = (struct block_desc *) ring->rd[block_num].iov_base;
602 while ((pbd->h1.block_status & TP_STATUS_USER) == 0)
605 __v3_walk_block(pbd, block_num);
606 __v3_flush_block(pbd);
608 block_num = (block_num + 1) % ring->rd_num;
611 pair_udp_close(udp_sock);
613 if (total_packets != 2 * NUM_PACKETS) {
614 fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
615 total_packets, NUM_PACKETS);
619 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
622 static void walk_v3(int sock, struct ring *ring)
624 if (ring->type == PACKET_RX_RING)
625 walk_v3_rx(sock, ring);
630 static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
632 ring->req.tp_block_size = getpagesize() << 2;
633 ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
634 ring->req.tp_block_nr = blocks;
636 ring->req.tp_frame_nr = ring->req.tp_block_size /
637 ring->req.tp_frame_size *
638 ring->req.tp_block_nr;
640 ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
641 ring->walk = walk_v1_v2;
642 ring->rd_num = ring->req.tp_frame_nr;
643 ring->flen = ring->req.tp_frame_size;
646 static void __v3_fill(struct ring *ring, unsigned int blocks, int type)
648 if (type == PACKET_RX_RING) {
649 ring->req3.tp_retire_blk_tov = 64;
650 ring->req3.tp_sizeof_priv = 0;
651 ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
653 ring->req3.tp_block_size = getpagesize() << 2;
654 ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
655 ring->req3.tp_block_nr = blocks;
657 ring->req3.tp_frame_nr = ring->req3.tp_block_size /
658 ring->req3.tp_frame_size *
659 ring->req3.tp_block_nr;
661 ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
662 ring->walk = walk_v3;
663 ring->rd_num = ring->req3.tp_block_nr;
664 ring->flen = ring->req3.tp_block_size;
667 static void setup_ring(int sock, struct ring *ring, int version, int type)
670 unsigned int blocks = 256;
673 ring->version = version;
678 if (type == PACKET_TX_RING)
679 __v1_v2_set_packet_loss_discard(sock);
680 __v1_v2_fill(ring, blocks);
681 ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
686 __v3_fill(ring, blocks, type);
687 ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
693 perror("setsockopt");
697 ring->rd_len = ring->rd_num * sizeof(*ring->rd);
698 ring->rd = malloc(ring->rd_len);
699 if (ring->rd == NULL) {
708 static void mmap_ring(int sock, struct ring *ring)
712 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
713 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
714 if (ring->mm_space == MAP_FAILED) {
719 memset(ring->rd, 0, ring->rd_len);
720 for (i = 0; i < ring->rd_num; ++i) {
721 ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
722 ring->rd[i].iov_len = ring->flen;
726 static void bind_ring(int sock, struct ring *ring)
730 pair_udp_setfilter(sock);
732 ring->ll.sll_family = PF_PACKET;
733 ring->ll.sll_protocol = htons(ETH_P_ALL);
734 ring->ll.sll_ifindex = if_nametoindex("lo");
735 ring->ll.sll_hatype = 0;
736 ring->ll.sll_pkttype = 0;
737 ring->ll.sll_halen = 0;
739 ret = bind(sock, (struct sockaddr *) &ring->ll, sizeof(ring->ll));
746 static void walk_ring(int sock, struct ring *ring)
748 ring->walk(sock, ring);
751 static void unmap_ring(int sock, struct ring *ring)
753 munmap(ring->mm_space, ring->mm_len);
757 static int test_kernel_bit_width(void)
763 fd = open("/proc/kallsyms", O_RDONLY);
769 ret = read(fd, in, sizeof(in));
778 while(!isspace(*ptr)) {
786 static int test_user_bit_width(void)
791 static const char *tpacket_str[] = {
792 [TPACKET_V1] = "TPACKET_V1",
793 [TPACKET_V2] = "TPACKET_V2",
794 [TPACKET_V3] = "TPACKET_V3",
797 static const char *type_str[] = {
798 [PACKET_RX_RING] = "PACKET_RX_RING",
799 [PACKET_TX_RING] = "PACKET_TX_RING",
802 static int test_tpacket(int version, int type)
807 fprintf(stderr, "test: %s with %s ", tpacket_str[version],
811 if (version == TPACKET_V1 &&
812 test_kernel_bit_width() != test_user_bit_width()) {
813 fprintf(stderr, "test: skip %s %s since user and kernel "
814 "space have different bit width\n",
815 tpacket_str[version], type_str[type]);
819 sock = pfsocket(version);
820 memset(&ring, 0, sizeof(ring));
821 setup_ring(sock, &ring, version, type);
822 mmap_ring(sock, &ring);
823 bind_ring(sock, &ring);
824 walk_ring(sock, &ring);
825 unmap_ring(sock, &ring);
828 fprintf(stderr, "\n");
836 ret |= test_tpacket(TPACKET_V1, PACKET_RX_RING);
837 ret |= test_tpacket(TPACKET_V1, PACKET_TX_RING);
839 ret |= test_tpacket(TPACKET_V2, PACKET_RX_RING);
840 ret |= test_tpacket(TPACKET_V2, PACKET_TX_RING);
842 ret |= test_tpacket(TPACKET_V3, PACKET_RX_RING);
843 ret |= test_tpacket(TPACKET_V3, PACKET_TX_RING);
848 printf("OK. All tests passed\n");