2 * Copyright 2013 Red Hat, Inc.
3 * Author: Daniel Borkmann <dborkman@redhat.com>
4 * Chetan Loke <loke.chetan@gmail.com> (TPACKET_V3 usage example)
6 * A basic test of packet socket's TPACKET_V1/TPACKET_V2/TPACKET_V3 behavior.
9 * Test the setup of the TPACKET socket with different patterns that are
10 * known to fail (TODO) resp. succeed (OK).
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
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
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.
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
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.
41 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <linux/if_packet.h>
46 #include <linux/filter.h>
50 #include <bits/wordsize.h>
51 #include <net/ethernet.h>
52 #include <netinet/ip.h>
53 #include <arpa/inet.h>
61 #include "psock_lib.h"
63 #include "../kselftest.h"
66 # define bug_on(cond) assert(!(cond))
69 #ifndef __aligned_tpacket
70 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
73 #ifndef __align_tpacket
74 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
77 #define NUM_PACKETS 100
78 #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1))
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;
88 struct tpacket_req req;
89 struct tpacket_req3 req3;
95 uint32_t offset_to_priv;
96 struct tpacket_hdr_v1 h1;
101 struct tpacket_hdr tp_h __aligned_tpacket;
102 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket_hdr));
105 struct tpacket2_hdr tp_h __aligned_tpacket;
106 struct sockaddr_ll s_ll __align_tpacket(sizeof(struct tpacket2_hdr));
111 static unsigned int total_packets, total_bytes;
113 static int pfsocket(int ver)
115 int ret, sock = socket(PF_PACKET, SOCK_RAW, 0);
121 ret = setsockopt(sock, SOL_PACKET, PACKET_VERSION, &ver, sizeof(ver));
123 perror("setsockopt");
130 static void status_bar_update(void)
132 if (total_packets % 10 == 0) {
133 fprintf(stderr, ".");
138 static void test_payload(void *pay, size_t len)
140 struct ethhdr *eth = pay;
142 if (len < sizeof(struct ethhdr)) {
143 fprintf(stderr, "test_payload: packet too "
144 "small: %zu bytes!\n", len);
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));
155 static void create_payload(void *pay, size_t *len)
158 struct ethhdr *eth = pay;
159 struct iphdr *ip = pay + sizeof(*eth);
161 /* Lets create some broken crap, that still passes
165 *len = DATA_LEN + 42;
167 memset(pay, 0xff, ETH_ALEN * 2);
168 eth->h_proto = htons(ETH_P_IP);
170 for (i = 0; i < sizeof(*ip); ++i)
171 ((uint8_t *) pay)[i + sizeof(*eth)] = (uint8_t) rand();
178 ip->tot_len = htons((uint16_t) *len - sizeof(*eth));
180 ip->saddr = htonl(INADDR_LOOPBACK);
181 ip->daddr = htonl(INADDR_LOOPBACK);
183 memset(pay + sizeof(*eth) + sizeof(*ip),
184 DATA_CHAR, DATA_LEN);
187 static inline int __v1_rx_kernel_ready(struct tpacket_hdr *hdr)
189 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
192 static inline void __v1_rx_user_ready(struct tpacket_hdr *hdr)
194 hdr->tp_status = TP_STATUS_KERNEL;
195 __sync_synchronize();
198 static inline int __v2_rx_kernel_ready(struct tpacket2_hdr *hdr)
200 return ((hdr->tp_status & TP_STATUS_USER) == TP_STATUS_USER);
203 static inline void __v2_rx_user_ready(struct tpacket2_hdr *hdr)
205 hdr->tp_status = TP_STATUS_KERNEL;
206 __sync_synchronize();
209 static inline int __v1_v2_rx_kernel_ready(void *base, int version)
213 return __v1_rx_kernel_ready(base);
215 return __v2_rx_kernel_ready(base);
222 static inline void __v1_v2_rx_user_ready(void *base, int version)
226 __v1_rx_user_ready(base);
229 __v2_rx_user_ready(base);
234 static void walk_v1_v2_rx(int sock, struct ring *ring)
239 unsigned int frame_num = 0;
241 bug_on(ring->type != PACKET_RX_RING);
243 pair_udp_open(udp_sock, PORT_BASE);
245 memset(&pfd, 0, sizeof(pfd));
247 pfd.events = POLLIN | POLLERR;
250 pair_udp_send(udp_sock, NUM_PACKETS);
252 while (total_packets < NUM_PACKETS * 2) {
253 while (__v1_v2_rx_kernel_ready(ring->rd[frame_num].iov_base,
255 ppd.raw = ring->rd[frame_num].iov_base;
257 switch (ring->version) {
259 test_payload((uint8_t *) ppd.raw + ppd.v1->tp_h.tp_mac,
260 ppd.v1->tp_h.tp_snaplen);
261 total_bytes += ppd.v1->tp_h.tp_snaplen;
265 test_payload((uint8_t *) ppd.raw + ppd.v2->tp_h.tp_mac,
266 ppd.v2->tp_h.tp_snaplen);
267 total_bytes += ppd.v2->tp_h.tp_snaplen;
274 __v1_v2_rx_user_ready(ppd.raw, ring->version);
276 frame_num = (frame_num + 1) % ring->rd_num;
282 pair_udp_close(udp_sock);
284 if (total_packets != 2 * NUM_PACKETS) {
285 fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
286 ring->version, total_packets, NUM_PACKETS);
290 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
293 static inline int __v1_tx_kernel_ready(struct tpacket_hdr *hdr)
295 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
298 static inline void __v1_tx_user_ready(struct tpacket_hdr *hdr)
300 hdr->tp_status = TP_STATUS_SEND_REQUEST;
301 __sync_synchronize();
304 static inline int __v2_tx_kernel_ready(struct tpacket2_hdr *hdr)
306 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
309 static inline void __v2_tx_user_ready(struct tpacket2_hdr *hdr)
311 hdr->tp_status = TP_STATUS_SEND_REQUEST;
312 __sync_synchronize();
315 static inline int __v3_tx_kernel_ready(struct tpacket3_hdr *hdr)
317 return !(hdr->tp_status & (TP_STATUS_SEND_REQUEST | TP_STATUS_SENDING));
320 static inline void __v3_tx_user_ready(struct tpacket3_hdr *hdr)
322 hdr->tp_status = TP_STATUS_SEND_REQUEST;
323 __sync_synchronize();
326 static inline int __tx_kernel_ready(void *base, int version)
330 return __v1_tx_kernel_ready(base);
332 return __v2_tx_kernel_ready(base);
334 return __v3_tx_kernel_ready(base);
341 static inline void __tx_user_ready(void *base, int version)
345 __v1_tx_user_ready(base);
348 __v2_tx_user_ready(base);
351 __v3_tx_user_ready(base);
356 static void __v1_v2_set_packet_loss_discard(int sock)
358 int ret, discard = 1;
360 ret = setsockopt(sock, SOL_PACKET, PACKET_LOSS, (void *) &discard,
363 perror("setsockopt");
368 static inline void *get_next_frame(struct ring *ring, int n)
370 uint8_t *f0 = ring->rd[0].iov_base;
372 switch (ring->version) {
375 return ring->rd[n].iov_base;
377 return f0 + (n * ring->req3.tp_frame_size);
383 static void walk_tx(int sock, struct ring *ring)
390 unsigned int frame_num = 0, got = 0;
391 struct sockaddr_ll ll = {
392 .sll_family = PF_PACKET,
393 .sll_halen = ETH_ALEN,
397 /* TPACKET_V{1,2} sets up the ring->rd* related variables based
398 * on frames (e.g., rd_num is tp_frame_nr) whereas V3 sets these
399 * up based on blocks (e.g, rd_num is tp_block_nr)
401 if (ring->version <= TPACKET_V2)
402 nframes = ring->rd_num;
404 nframes = ring->req3.tp_frame_nr;
406 bug_on(ring->type != PACKET_TX_RING);
407 bug_on(nframes < NUM_PACKETS);
409 rcv_sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
410 if (rcv_sock == -1) {
415 pair_udp_setfilter(rcv_sock);
417 ll.sll_ifindex = if_nametoindex("lo");
418 ret = bind(rcv_sock, (struct sockaddr *) &ll, sizeof(ll));
424 memset(&pfd, 0, sizeof(pfd));
426 pfd.events = POLLOUT | POLLERR;
429 total_packets = NUM_PACKETS;
430 create_payload(packet, &packet_len);
432 while (total_packets > 0) {
433 void *next = get_next_frame(ring, frame_num);
435 while (__tx_kernel_ready(next, ring->version) &&
439 switch (ring->version) {
441 ppd.v1->tp_h.tp_snaplen = packet_len;
442 ppd.v1->tp_h.tp_len = packet_len;
444 memcpy((uint8_t *) ppd.raw + TPACKET_HDRLEN -
445 sizeof(struct sockaddr_ll), packet,
447 total_bytes += ppd.v1->tp_h.tp_snaplen;
451 ppd.v2->tp_h.tp_snaplen = packet_len;
452 ppd.v2->tp_h.tp_len = packet_len;
454 memcpy((uint8_t *) ppd.raw + TPACKET2_HDRLEN -
455 sizeof(struct sockaddr_ll), packet,
457 total_bytes += ppd.v2->tp_h.tp_snaplen;
460 struct tpacket3_hdr *tx = next;
462 tx->tp_snaplen = packet_len;
463 tx->tp_len = packet_len;
464 tx->tp_next_offset = 0;
466 memcpy((uint8_t *)tx + TPACKET3_HDRLEN -
467 sizeof(struct sockaddr_ll), packet,
469 total_bytes += tx->tp_snaplen;
477 __tx_user_ready(next, ring->version);
479 frame_num = (frame_num + 1) % nframes;
485 bug_on(total_packets != 0);
487 ret = sendto(sock, NULL, 0, 0, NULL, 0);
493 while ((ret = recvfrom(rcv_sock, packet, sizeof(packet),
494 0, NULL, NULL)) > 0 &&
495 total_packets < NUM_PACKETS) {
497 test_payload(packet, ret);
505 if (total_packets != NUM_PACKETS) {
506 fprintf(stderr, "walk_v%d_rx: received %u out of %u pkts\n",
507 ring->version, total_packets, NUM_PACKETS);
511 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, got);
514 static void walk_v1_v2(int sock, struct ring *ring)
516 if (ring->type == PACKET_RX_RING)
517 walk_v1_v2_rx(sock, ring);
522 static uint64_t __v3_prev_block_seq_num = 0;
524 void __v3_test_block_seq_num(struct block_desc *pbd)
526 if (__v3_prev_block_seq_num + 1 != pbd->h1.seq_num) {
527 fprintf(stderr, "\nprev_block_seq_num:%"PRIu64", expected "
528 "seq:%"PRIu64" != actual seq:%"PRIu64"\n",
529 __v3_prev_block_seq_num, __v3_prev_block_seq_num + 1,
530 (uint64_t) pbd->h1.seq_num);
534 __v3_prev_block_seq_num = pbd->h1.seq_num;
537 static void __v3_test_block_len(struct block_desc *pbd, uint32_t bytes, int block_num)
539 if (pbd->h1.num_pkts && bytes != pbd->h1.blk_len) {
540 fprintf(stderr, "\nblock:%u with %upackets, expected "
541 "len:%u != actual len:%u\n", block_num,
542 pbd->h1.num_pkts, bytes, pbd->h1.blk_len);
547 static void __v3_test_block_header(struct block_desc *pbd, const int block_num)
549 if ((pbd->h1.block_status & TP_STATUS_USER) == 0) {
550 fprintf(stderr, "\nblock %u: not in TP_STATUS_USER\n", block_num);
554 __v3_test_block_seq_num(pbd);
557 static void __v3_walk_block(struct block_desc *pbd, const int block_num)
559 int num_pkts = pbd->h1.num_pkts, i;
560 unsigned long bytes = 0, bytes_with_padding = ALIGN_8(sizeof(*pbd));
561 struct tpacket3_hdr *ppd;
563 __v3_test_block_header(pbd, block_num);
565 ppd = (struct tpacket3_hdr *) ((uint8_t *) pbd +
566 pbd->h1.offset_to_first_pkt);
568 for (i = 0; i < num_pkts; ++i) {
569 bytes += ppd->tp_snaplen;
571 if (ppd->tp_next_offset)
572 bytes_with_padding += ppd->tp_next_offset;
574 bytes_with_padding += ALIGN_8(ppd->tp_snaplen + ppd->tp_mac);
576 test_payload((uint8_t *) ppd + ppd->tp_mac, ppd->tp_snaplen);
581 ppd = (struct tpacket3_hdr *) ((uint8_t *) ppd + ppd->tp_next_offset);
582 __sync_synchronize();
585 __v3_test_block_len(pbd, bytes_with_padding, block_num);
586 total_bytes += bytes;
589 void __v3_flush_block(struct block_desc *pbd)
591 pbd->h1.block_status = TP_STATUS_KERNEL;
592 __sync_synchronize();
595 static void walk_v3_rx(int sock, struct ring *ring)
597 unsigned int block_num = 0;
599 struct block_desc *pbd;
602 bug_on(ring->type != PACKET_RX_RING);
604 pair_udp_open(udp_sock, PORT_BASE);
606 memset(&pfd, 0, sizeof(pfd));
608 pfd.events = POLLIN | POLLERR;
611 pair_udp_send(udp_sock, NUM_PACKETS);
613 while (total_packets < NUM_PACKETS * 2) {
614 pbd = (struct block_desc *) ring->rd[block_num].iov_base;
616 while ((pbd->h1.block_status & TP_STATUS_USER) == 0)
619 __v3_walk_block(pbd, block_num);
620 __v3_flush_block(pbd);
622 block_num = (block_num + 1) % ring->rd_num;
625 pair_udp_close(udp_sock);
627 if (total_packets != 2 * NUM_PACKETS) {
628 fprintf(stderr, "walk_v3_rx: received %u out of %u pkts\n",
629 total_packets, NUM_PACKETS);
633 fprintf(stderr, " %u pkts (%u bytes)", NUM_PACKETS, total_bytes >> 1);
636 static void walk_v3(int sock, struct ring *ring)
638 if (ring->type == PACKET_RX_RING)
639 walk_v3_rx(sock, ring);
644 static void __v1_v2_fill(struct ring *ring, unsigned int blocks)
646 ring->req.tp_block_size = getpagesize() << 2;
647 ring->req.tp_frame_size = TPACKET_ALIGNMENT << 7;
648 ring->req.tp_block_nr = blocks;
650 ring->req.tp_frame_nr = ring->req.tp_block_size /
651 ring->req.tp_frame_size *
652 ring->req.tp_block_nr;
654 ring->mm_len = ring->req.tp_block_size * ring->req.tp_block_nr;
655 ring->walk = walk_v1_v2;
656 ring->rd_num = ring->req.tp_frame_nr;
657 ring->flen = ring->req.tp_frame_size;
660 static void __v3_fill(struct ring *ring, unsigned int blocks, int type)
662 if (type == PACKET_RX_RING) {
663 ring->req3.tp_retire_blk_tov = 64;
664 ring->req3.tp_sizeof_priv = 0;
665 ring->req3.tp_feature_req_word = TP_FT_REQ_FILL_RXHASH;
667 ring->req3.tp_block_size = getpagesize() << 2;
668 ring->req3.tp_frame_size = TPACKET_ALIGNMENT << 7;
669 ring->req3.tp_block_nr = blocks;
671 ring->req3.tp_frame_nr = ring->req3.tp_block_size /
672 ring->req3.tp_frame_size *
673 ring->req3.tp_block_nr;
675 ring->mm_len = ring->req3.tp_block_size * ring->req3.tp_block_nr;
676 ring->walk = walk_v3;
677 ring->rd_num = ring->req3.tp_block_nr;
678 ring->flen = ring->req3.tp_block_size;
681 static void setup_ring(int sock, struct ring *ring, int version, int type)
684 unsigned int blocks = 256;
687 ring->version = version;
692 if (type == PACKET_TX_RING)
693 __v1_v2_set_packet_loss_discard(sock);
694 __v1_v2_fill(ring, blocks);
695 ret = setsockopt(sock, SOL_PACKET, type, &ring->req,
700 __v3_fill(ring, blocks, type);
701 ret = setsockopt(sock, SOL_PACKET, type, &ring->req3,
707 perror("setsockopt");
711 ring->rd_len = ring->rd_num * sizeof(*ring->rd);
712 ring->rd = malloc(ring->rd_len);
713 if (ring->rd == NULL) {
722 static void mmap_ring(int sock, struct ring *ring)
726 ring->mm_space = mmap(0, ring->mm_len, PROT_READ | PROT_WRITE,
727 MAP_SHARED | MAP_LOCKED | MAP_POPULATE, sock, 0);
728 if (ring->mm_space == MAP_FAILED) {
733 memset(ring->rd, 0, ring->rd_len);
734 for (i = 0; i < ring->rd_num; ++i) {
735 ring->rd[i].iov_base = ring->mm_space + (i * ring->flen);
736 ring->rd[i].iov_len = ring->flen;
740 static void bind_ring(int sock, struct ring *ring)
744 pair_udp_setfilter(sock);
746 ring->ll.sll_family = PF_PACKET;
747 ring->ll.sll_protocol = htons(ETH_P_ALL);
748 ring->ll.sll_ifindex = if_nametoindex("lo");
749 ring->ll.sll_hatype = 0;
750 ring->ll.sll_pkttype = 0;
751 ring->ll.sll_halen = 0;
753 ret = bind(sock, (struct sockaddr *) &ring->ll, sizeof(ring->ll));
760 static void walk_ring(int sock, struct ring *ring)
762 ring->walk(sock, ring);
765 static void unmap_ring(int sock, struct ring *ring)
767 munmap(ring->mm_space, ring->mm_len);
771 static int test_kernel_bit_width(void)
777 fd = open("/proc/kallsyms", O_RDONLY);
783 ret = read(fd, in, sizeof(in));
792 while(!isspace(*ptr)) {
800 static int test_user_bit_width(void)
805 static const char *tpacket_str[] = {
806 [TPACKET_V1] = "TPACKET_V1",
807 [TPACKET_V2] = "TPACKET_V2",
808 [TPACKET_V3] = "TPACKET_V3",
811 static const char *type_str[] = {
812 [PACKET_RX_RING] = "PACKET_RX_RING",
813 [PACKET_TX_RING] = "PACKET_TX_RING",
816 static int test_tpacket(int version, int type)
821 fprintf(stderr, "test: %s with %s ", tpacket_str[version],
825 if (version == TPACKET_V1 &&
826 test_kernel_bit_width() != test_user_bit_width()) {
827 fprintf(stderr, "test: skip %s %s since user and kernel "
828 "space have different bit width\n",
829 tpacket_str[version], type_str[type]);
833 sock = pfsocket(version);
834 memset(&ring, 0, sizeof(ring));
835 setup_ring(sock, &ring, version, type);
836 mmap_ring(sock, &ring);
837 bind_ring(sock, &ring);
838 walk_ring(sock, &ring);
839 unmap_ring(sock, &ring);
842 fprintf(stderr, "\n");
850 ret |= test_tpacket(TPACKET_V1, PACKET_RX_RING);
851 ret |= test_tpacket(TPACKET_V1, PACKET_TX_RING);
853 ret |= test_tpacket(TPACKET_V2, PACKET_RX_RING);
854 ret |= test_tpacket(TPACKET_V2, PACKET_TX_RING);
856 ret |= test_tpacket(TPACKET_V3, PACKET_RX_RING);
857 ret |= test_tpacket(TPACKET_V3, PACKET_TX_RING);
862 printf("OK. All tests passed\n");