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"
64 # define bug_on(cond) assert(!(cond))
67 #ifndef __aligned_tpacket
68 # define __aligned_tpacket __attribute__((aligned(TPACKET_ALIGNMENT)))
71 #ifndef __align_tpacket
72 # define __align_tpacket(x) __attribute__((aligned(TPACKET_ALIGN(x))))
75 #define NUM_PACKETS 100
76 #define ALIGN_8(x) (((x) + 8 - 1) & ~(8 - 1))
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
;
86 struct tpacket_req req
;
87 struct tpacket_req3 req3
;
93 uint32_t offset_to_priv
;
94 struct tpacket_hdr_v1 h1
;
99 struct tpacket_hdr tp_h __aligned_tpacket
;
100 struct sockaddr_ll s_ll
__align_tpacket(sizeof(struct tpacket_hdr
));
103 struct tpacket2_hdr tp_h __aligned_tpacket
;
104 struct sockaddr_ll s_ll
__align_tpacket(sizeof(struct tpacket2_hdr
));
109 static unsigned int total_packets
, total_bytes
;
111 static int pfsocket(int ver
)
113 int ret
, sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
119 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_VERSION
, &ver
, sizeof(ver
));
121 perror("setsockopt");
128 static void status_bar_update(void)
130 if (total_packets
% 10 == 0) {
131 fprintf(stderr
, ".");
136 static void test_payload(void *pay
, size_t len
)
138 struct ethhdr
*eth
= pay
;
140 if (len
< sizeof(struct ethhdr
)) {
141 fprintf(stderr
, "test_payload: packet too "
142 "small: %zu bytes!\n", len
);
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
));
153 static void create_payload(void *pay
, size_t *len
)
156 struct ethhdr
*eth
= pay
;
157 struct iphdr
*ip
= pay
+ sizeof(*eth
);
159 /* Lets create some broken crap, that still passes
163 *len
= DATA_LEN
+ 42;
165 memset(pay
, 0xff, ETH_ALEN
* 2);
166 eth
->h_proto
= htons(ETH_P_IP
);
168 for (i
= 0; i
< sizeof(*ip
); ++i
)
169 ((uint8_t *) pay
)[i
+ sizeof(*eth
)] = (uint8_t) rand();
176 ip
->tot_len
= htons((uint16_t) *len
- sizeof(*eth
));
178 ip
->saddr
= htonl(INADDR_LOOPBACK
);
179 ip
->daddr
= htonl(INADDR_LOOPBACK
);
181 memset(pay
+ sizeof(*eth
) + sizeof(*ip
),
182 DATA_CHAR
, DATA_LEN
);
185 static inline int __v1_rx_kernel_ready(struct tpacket_hdr
*hdr
)
187 return ((hdr
->tp_status
& TP_STATUS_USER
) == TP_STATUS_USER
);
190 static inline void __v1_rx_user_ready(struct tpacket_hdr
*hdr
)
192 hdr
->tp_status
= TP_STATUS_KERNEL
;
193 __sync_synchronize();
196 static inline int __v2_rx_kernel_ready(struct tpacket2_hdr
*hdr
)
198 return ((hdr
->tp_status
& TP_STATUS_USER
) == TP_STATUS_USER
);
201 static inline void __v2_rx_user_ready(struct tpacket2_hdr
*hdr
)
203 hdr
->tp_status
= TP_STATUS_KERNEL
;
204 __sync_synchronize();
207 static inline int __v1_v2_rx_kernel_ready(void *base
, int version
)
211 return __v1_rx_kernel_ready(base
);
213 return __v2_rx_kernel_ready(base
);
220 static inline void __v1_v2_rx_user_ready(void *base
, int version
)
224 __v1_rx_user_ready(base
);
227 __v2_rx_user_ready(base
);
232 static void walk_v1_v2_rx(int sock
, struct ring
*ring
)
237 unsigned int frame_num
= 0;
239 bug_on(ring
->type
!= PACKET_RX_RING
);
241 pair_udp_open(udp_sock
, PORT_BASE
);
242 pair_udp_setfilter(sock
);
244 memset(&pfd
, 0, sizeof(pfd
));
246 pfd
.events
= POLLIN
| POLLERR
;
249 pair_udp_send(udp_sock
, NUM_PACKETS
);
251 while (total_packets
< NUM_PACKETS
* 2) {
252 while (__v1_v2_rx_kernel_ready(ring
->rd
[frame_num
].iov_base
,
254 ppd
.raw
= ring
->rd
[frame_num
].iov_base
;
256 switch (ring
->version
) {
258 test_payload((uint8_t *) ppd
.raw
+ ppd
.v1
->tp_h
.tp_mac
,
259 ppd
.v1
->tp_h
.tp_snaplen
);
260 total_bytes
+= ppd
.v1
->tp_h
.tp_snaplen
;
264 test_payload((uint8_t *) ppd
.raw
+ ppd
.v2
->tp_h
.tp_mac
,
265 ppd
.v2
->tp_h
.tp_snaplen
);
266 total_bytes
+= ppd
.v2
->tp_h
.tp_snaplen
;
273 __v1_v2_rx_user_ready(ppd
.raw
, ring
->version
);
275 frame_num
= (frame_num
+ 1) % ring
->rd_num
;
281 pair_udp_close(udp_sock
);
283 if (total_packets
!= 2 * NUM_PACKETS
) {
284 fprintf(stderr
, "walk_v%d_rx: received %u out of %u pkts\n",
285 ring
->version
, total_packets
, NUM_PACKETS
);
289 fprintf(stderr
, " %u pkts (%u bytes)", NUM_PACKETS
, total_bytes
>> 1);
292 static inline int __v1_tx_kernel_ready(struct tpacket_hdr
*hdr
)
294 return !(hdr
->tp_status
& (TP_STATUS_SEND_REQUEST
| TP_STATUS_SENDING
));
297 static inline void __v1_tx_user_ready(struct tpacket_hdr
*hdr
)
299 hdr
->tp_status
= TP_STATUS_SEND_REQUEST
;
300 __sync_synchronize();
303 static inline int __v2_tx_kernel_ready(struct tpacket2_hdr
*hdr
)
305 return !(hdr
->tp_status
& (TP_STATUS_SEND_REQUEST
| TP_STATUS_SENDING
));
308 static inline void __v2_tx_user_ready(struct tpacket2_hdr
*hdr
)
310 hdr
->tp_status
= TP_STATUS_SEND_REQUEST
;
311 __sync_synchronize();
314 static inline int __v1_v2_tx_kernel_ready(void *base
, int version
)
318 return __v1_tx_kernel_ready(base
);
320 return __v2_tx_kernel_ready(base
);
327 static inline void __v1_v2_tx_user_ready(void *base
, int version
)
331 __v1_tx_user_ready(base
);
334 __v2_tx_user_ready(base
);
339 static void __v1_v2_set_packet_loss_discard(int sock
)
341 int ret
, discard
= 1;
343 ret
= setsockopt(sock
, SOL_PACKET
, PACKET_LOSS
, (void *) &discard
,
346 perror("setsockopt");
351 static void walk_v1_v2_tx(int sock
, struct ring
*ring
)
358 unsigned int frame_num
= 0, got
= 0;
359 struct sockaddr_ll ll
= {
360 .sll_family
= PF_PACKET
,
361 .sll_halen
= ETH_ALEN
,
364 bug_on(ring
->type
!= PACKET_TX_RING
);
365 bug_on(ring
->rd_num
< NUM_PACKETS
);
367 rcv_sock
= socket(PF_PACKET
, SOCK_RAW
, htons(ETH_P_ALL
));
368 if (rcv_sock
== -1) {
373 pair_udp_setfilter(rcv_sock
);
375 ll
.sll_ifindex
= if_nametoindex("lo");
376 ret
= bind(rcv_sock
, (struct sockaddr
*) &ll
, sizeof(ll
));
382 memset(&pfd
, 0, sizeof(pfd
));
384 pfd
.events
= POLLOUT
| POLLERR
;
387 total_packets
= NUM_PACKETS
;
388 create_payload(packet
, &packet_len
);
390 while (total_packets
> 0) {
391 while (__v1_v2_tx_kernel_ready(ring
->rd
[frame_num
].iov_base
,
394 ppd
.raw
= ring
->rd
[frame_num
].iov_base
;
396 switch (ring
->version
) {
398 ppd
.v1
->tp_h
.tp_snaplen
= packet_len
;
399 ppd
.v1
->tp_h
.tp_len
= packet_len
;
401 memcpy((uint8_t *) ppd
.raw
+ TPACKET_HDRLEN
-
402 sizeof(struct sockaddr_ll
), packet
,
404 total_bytes
+= ppd
.v1
->tp_h
.tp_snaplen
;
408 ppd
.v2
->tp_h
.tp_snaplen
= packet_len
;
409 ppd
.v2
->tp_h
.tp_len
= packet_len
;
411 memcpy((uint8_t *) ppd
.raw
+ TPACKET2_HDRLEN
-
412 sizeof(struct sockaddr_ll
), packet
,
414 total_bytes
+= ppd
.v2
->tp_h
.tp_snaplen
;
421 __v1_v2_tx_user_ready(ppd
.raw
, ring
->version
);
423 frame_num
= (frame_num
+ 1) % ring
->rd_num
;
429 bug_on(total_packets
!= 0);
431 ret
= sendto(sock
, NULL
, 0, 0, NULL
, 0);
437 while ((ret
= recvfrom(rcv_sock
, packet
, sizeof(packet
),
438 0, NULL
, NULL
)) > 0 &&
439 total_packets
< NUM_PACKETS
) {
441 test_payload(packet
, ret
);
449 if (total_packets
!= NUM_PACKETS
) {
450 fprintf(stderr
, "walk_v%d_rx: received %u out of %u pkts\n",
451 ring
->version
, total_packets
, NUM_PACKETS
);
455 fprintf(stderr
, " %u pkts (%u bytes)", NUM_PACKETS
, got
);
458 static void walk_v1_v2(int sock
, struct ring
*ring
)
460 if (ring
->type
== PACKET_RX_RING
)
461 walk_v1_v2_rx(sock
, ring
);
463 walk_v1_v2_tx(sock
, ring
);
466 static uint64_t __v3_prev_block_seq_num
= 0;
468 void __v3_test_block_seq_num(struct block_desc
*pbd
)
470 if (__v3_prev_block_seq_num
+ 1 != pbd
->h1
.seq_num
) {
471 fprintf(stderr
, "\nprev_block_seq_num:%"PRIu64
", expected "
472 "seq:%"PRIu64
" != actual seq:%"PRIu64
"\n",
473 __v3_prev_block_seq_num
, __v3_prev_block_seq_num
+ 1,
474 (uint64_t) pbd
->h1
.seq_num
);
478 __v3_prev_block_seq_num
= pbd
->h1
.seq_num
;
481 static void __v3_test_block_len(struct block_desc
*pbd
, uint32_t bytes
, int block_num
)
483 if (pbd
->h1
.num_pkts
&& bytes
!= pbd
->h1
.blk_len
) {
484 fprintf(stderr
, "\nblock:%u with %upackets, expected "
485 "len:%u != actual len:%u\n", block_num
,
486 pbd
->h1
.num_pkts
, bytes
, pbd
->h1
.blk_len
);
491 static void __v3_test_block_header(struct block_desc
*pbd
, const int block_num
)
493 if ((pbd
->h1
.block_status
& TP_STATUS_USER
) == 0) {
494 fprintf(stderr
, "\nblock %u: not in TP_STATUS_USER\n", block_num
);
498 __v3_test_block_seq_num(pbd
);
501 static void __v3_walk_block(struct block_desc
*pbd
, const int block_num
)
503 int num_pkts
= pbd
->h1
.num_pkts
, i
;
504 unsigned long bytes
= 0, bytes_with_padding
= ALIGN_8(sizeof(*pbd
));
505 struct tpacket3_hdr
*ppd
;
507 __v3_test_block_header(pbd
, block_num
);
509 ppd
= (struct tpacket3_hdr
*) ((uint8_t *) pbd
+
510 pbd
->h1
.offset_to_first_pkt
);
512 for (i
= 0; i
< num_pkts
; ++i
) {
513 bytes
+= ppd
->tp_snaplen
;
515 if (ppd
->tp_next_offset
)
516 bytes_with_padding
+= ppd
->tp_next_offset
;
518 bytes_with_padding
+= ALIGN_8(ppd
->tp_snaplen
+ ppd
->tp_mac
);
520 test_payload((uint8_t *) ppd
+ ppd
->tp_mac
, ppd
->tp_snaplen
);
525 ppd
= (struct tpacket3_hdr
*) ((uint8_t *) ppd
+ ppd
->tp_next_offset
);
526 __sync_synchronize();
529 __v3_test_block_len(pbd
, bytes_with_padding
, block_num
);
530 total_bytes
+= bytes
;
533 void __v3_flush_block(struct block_desc
*pbd
)
535 pbd
->h1
.block_status
= TP_STATUS_KERNEL
;
536 __sync_synchronize();
539 static void walk_v3_rx(int sock
, struct ring
*ring
)
541 unsigned int block_num
= 0;
543 struct block_desc
*pbd
;
546 bug_on(ring
->type
!= PACKET_RX_RING
);
548 pair_udp_open(udp_sock
, PORT_BASE
);
549 pair_udp_setfilter(sock
);
551 memset(&pfd
, 0, sizeof(pfd
));
553 pfd
.events
= POLLIN
| POLLERR
;
556 pair_udp_send(udp_sock
, NUM_PACKETS
);
558 while (total_packets
< NUM_PACKETS
* 2) {
559 pbd
= (struct block_desc
*) ring
->rd
[block_num
].iov_base
;
561 while ((pbd
->h1
.block_status
& TP_STATUS_USER
) == 0)
564 __v3_walk_block(pbd
, block_num
);
565 __v3_flush_block(pbd
);
567 block_num
= (block_num
+ 1) % ring
->rd_num
;
570 pair_udp_close(udp_sock
);
572 if (total_packets
!= 2 * NUM_PACKETS
) {
573 fprintf(stderr
, "walk_v3_rx: received %u out of %u pkts\n",
574 total_packets
, NUM_PACKETS
);
578 fprintf(stderr
, " %u pkts (%u bytes)", NUM_PACKETS
, total_bytes
>> 1);
581 static void walk_v3(int sock
, struct ring
*ring
)
583 if (ring
->type
== PACKET_RX_RING
)
584 walk_v3_rx(sock
, ring
);
589 static void __v1_v2_fill(struct ring
*ring
, unsigned int blocks
)
591 ring
->req
.tp_block_size
= getpagesize() << 2;
592 ring
->req
.tp_frame_size
= TPACKET_ALIGNMENT
<< 7;
593 ring
->req
.tp_block_nr
= blocks
;
595 ring
->req
.tp_frame_nr
= ring
->req
.tp_block_size
/
596 ring
->req
.tp_frame_size
*
597 ring
->req
.tp_block_nr
;
599 ring
->mm_len
= ring
->req
.tp_block_size
* ring
->req
.tp_block_nr
;
600 ring
->walk
= walk_v1_v2
;
601 ring
->rd_num
= ring
->req
.tp_frame_nr
;
602 ring
->flen
= ring
->req
.tp_frame_size
;
605 static void __v3_fill(struct ring
*ring
, unsigned int blocks
)
607 ring
->req3
.tp_retire_blk_tov
= 64;
608 ring
->req3
.tp_sizeof_priv
= 0;
609 ring
->req3
.tp_feature_req_word
= TP_FT_REQ_FILL_RXHASH
;
611 ring
->req3
.tp_block_size
= getpagesize() << 2;
612 ring
->req3
.tp_frame_size
= TPACKET_ALIGNMENT
<< 7;
613 ring
->req3
.tp_block_nr
= blocks
;
615 ring
->req3
.tp_frame_nr
= ring
->req3
.tp_block_size
/
616 ring
->req3
.tp_frame_size
*
617 ring
->req3
.tp_block_nr
;
619 ring
->mm_len
= ring
->req3
.tp_block_size
* ring
->req3
.tp_block_nr
;
620 ring
->walk
= walk_v3
;
621 ring
->rd_num
= ring
->req3
.tp_block_nr
;
622 ring
->flen
= ring
->req3
.tp_block_size
;
625 static void setup_ring(int sock
, struct ring
*ring
, int version
, int type
)
628 unsigned int blocks
= 256;
631 ring
->version
= version
;
636 if (type
== PACKET_TX_RING
)
637 __v1_v2_set_packet_loss_discard(sock
);
638 __v1_v2_fill(ring
, blocks
);
639 ret
= setsockopt(sock
, SOL_PACKET
, type
, &ring
->req
,
644 __v3_fill(ring
, blocks
);
645 ret
= setsockopt(sock
, SOL_PACKET
, type
, &ring
->req3
,
651 perror("setsockopt");
655 ring
->rd_len
= ring
->rd_num
* sizeof(*ring
->rd
);
656 ring
->rd
= malloc(ring
->rd_len
);
657 if (ring
->rd
== NULL
) {
666 static void mmap_ring(int sock
, struct ring
*ring
)
670 ring
->mm_space
= mmap(0, ring
->mm_len
, PROT_READ
| PROT_WRITE
,
671 MAP_SHARED
| MAP_LOCKED
| MAP_POPULATE
, sock
, 0);
672 if (ring
->mm_space
== MAP_FAILED
) {
677 memset(ring
->rd
, 0, ring
->rd_len
);
678 for (i
= 0; i
< ring
->rd_num
; ++i
) {
679 ring
->rd
[i
].iov_base
= ring
->mm_space
+ (i
* ring
->flen
);
680 ring
->rd
[i
].iov_len
= ring
->flen
;
684 static void bind_ring(int sock
, struct ring
*ring
)
688 ring
->ll
.sll_family
= PF_PACKET
;
689 ring
->ll
.sll_protocol
= htons(ETH_P_ALL
);
690 ring
->ll
.sll_ifindex
= if_nametoindex("lo");
691 ring
->ll
.sll_hatype
= 0;
692 ring
->ll
.sll_pkttype
= 0;
693 ring
->ll
.sll_halen
= 0;
695 ret
= bind(sock
, (struct sockaddr
*) &ring
->ll
, sizeof(ring
->ll
));
702 static void walk_ring(int sock
, struct ring
*ring
)
704 ring
->walk(sock
, ring
);
707 static void unmap_ring(int sock
, struct ring
*ring
)
709 munmap(ring
->mm_space
, ring
->mm_len
);
713 static int test_kernel_bit_width(void)
719 fd
= open("/proc/kallsyms", O_RDONLY
);
725 ret
= read(fd
, in
, sizeof(in
));
734 while(!isspace(*ptr
)) {
742 static int test_user_bit_width(void)
747 static const char *tpacket_str
[] = {
748 [TPACKET_V1
] = "TPACKET_V1",
749 [TPACKET_V2
] = "TPACKET_V2",
750 [TPACKET_V3
] = "TPACKET_V3",
753 static const char *type_str
[] = {
754 [PACKET_RX_RING
] = "PACKET_RX_RING",
755 [PACKET_TX_RING
] = "PACKET_TX_RING",
758 static int test_tpacket(int version
, int type
)
763 fprintf(stderr
, "test: %s with %s ", tpacket_str
[version
],
767 if (version
== TPACKET_V1
&&
768 test_kernel_bit_width() != test_user_bit_width()) {
769 fprintf(stderr
, "test: skip %s %s since user and kernel "
770 "space have different bit width\n",
771 tpacket_str
[version
], type_str
[type
]);
775 sock
= pfsocket(version
);
776 memset(&ring
, 0, sizeof(ring
));
777 setup_ring(sock
, &ring
, version
, type
);
778 mmap_ring(sock
, &ring
);
779 bind_ring(sock
, &ring
);
780 walk_ring(sock
, &ring
);
781 unmap_ring(sock
, &ring
);
784 fprintf(stderr
, "\n");
792 ret
|= test_tpacket(TPACKET_V1
, PACKET_RX_RING
);
793 ret
|= test_tpacket(TPACKET_V1
, PACKET_TX_RING
);
795 ret
|= test_tpacket(TPACKET_V2
, PACKET_RX_RING
);
796 ret
|= test_tpacket(TPACKET_V2
, PACKET_TX_RING
);
798 ret
|= test_tpacket(TPACKET_V3
, PACKET_RX_RING
);
803 printf("OK. All tests passed\n");