1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2017 - 2018 Intel Corporation. */
4 #include <asm/barrier.h>
9 #include <linux/compiler.h>
10 #include <linux/if_link.h>
11 #include <linux/if_xdp.h>
12 #include <linux/if_ether.h>
14 #include <linux/udp.h>
15 #include <arpa/inet.h>
17 #include <net/ethernet.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
29 #include <sys/types.h>
33 #include <bpf/libbpf.h>
50 #define NUM_FRAMES (4 * 1024)
51 #define MIN_PKT_SIZE 64
53 #define DEBUG_HEXDUMP 0
60 static unsigned long prev_time
;
68 static enum benchmark_type opt_bench
= BENCH_RXDROP
;
69 static u32 opt_xdp_flags
= XDP_FLAGS_UPDATE_IF_NOEXIST
;
70 static const char *opt_if
= "";
71 static int opt_ifindex
;
73 static unsigned long opt_duration
;
74 static unsigned long start_time
;
75 static bool benchmark_done
;
76 static u32 opt_batch_size
= 64;
77 static int opt_pkt_count
;
78 static u16 opt_pkt_size
= MIN_PKT_SIZE
;
79 static u32 opt_pkt_fill_pattern
= 0x12345678;
81 static int opt_interval
= 1;
82 static u32 opt_xdp_bind_flags
= XDP_USE_NEED_WAKEUP
;
83 static u32 opt_umem_flags
;
84 static int opt_unaligned_chunks
;
85 static int opt_mmap_flags
;
86 static int opt_xsk_frame_size
= XSK_UMEM__DEFAULT_FRAME_SIZE
;
87 static int opt_timeout
= 1000;
88 static bool opt_need_wakeup
= true;
89 static u32 opt_num_xsks
= 1;
92 struct xsk_umem_info
{
93 struct xsk_ring_prod fq
;
94 struct xsk_ring_cons cq
;
95 struct xsk_umem
*umem
;
99 struct xsk_socket_info
{
100 struct xsk_ring_cons rx
;
101 struct xsk_ring_prod tx
;
102 struct xsk_umem_info
*umem
;
103 struct xsk_socket
*xsk
;
104 unsigned long rx_npkts
;
105 unsigned long tx_npkts
;
106 unsigned long prev_rx_npkts
;
107 unsigned long prev_tx_npkts
;
111 static int num_socks
;
112 struct xsk_socket_info
*xsks
[MAX_SOCKS
];
114 static unsigned long get_nsecs(void)
118 clock_gettime(CLOCK_MONOTONIC
, &ts
);
119 return ts
.tv_sec
* 1000000000UL + ts
.tv_nsec
;
122 static void print_benchmark(bool running
)
124 const char *bench_str
= "INVALID";
126 if (opt_bench
== BENCH_RXDROP
)
127 bench_str
= "rxdrop";
128 else if (opt_bench
== BENCH_TXONLY
)
129 bench_str
= "txonly";
130 else if (opt_bench
== BENCH_L2FWD
)
133 printf("%s:%d %s ", opt_if
, opt_queue
, bench_str
);
134 if (opt_xdp_flags
& XDP_FLAGS_SKB_MODE
)
136 else if (opt_xdp_flags
& XDP_FLAGS_DRV_MODE
)
145 printf("running...");
150 static void dump_stats(void)
152 unsigned long now
= get_nsecs();
153 long dt
= now
- prev_time
;
158 for (i
= 0; i
< num_socks
&& xsks
[i
]; i
++) {
159 char *fmt
= "%-15s %'-11.0f %'-11lu\n";
160 double rx_pps
, tx_pps
;
162 rx_pps
= (xsks
[i
]->rx_npkts
- xsks
[i
]->prev_rx_npkts
) *
164 tx_pps
= (xsks
[i
]->tx_npkts
- xsks
[i
]->prev_tx_npkts
) *
167 printf("\n sock%d@", i
);
168 print_benchmark(false);
171 printf("%-15s %-11s %-11s %-11.2f\n", "", "pps", "pkts",
173 printf(fmt
, "rx", rx_pps
, xsks
[i
]->rx_npkts
);
174 printf(fmt
, "tx", tx_pps
, xsks
[i
]->tx_npkts
);
176 xsks
[i
]->prev_rx_npkts
= xsks
[i
]->rx_npkts
;
177 xsks
[i
]->prev_tx_npkts
= xsks
[i
]->tx_npkts
;
181 static bool is_benchmark_done(void)
183 if (opt_duration
> 0) {
184 unsigned long dt
= (get_nsecs() - start_time
);
186 if (dt
>= opt_duration
)
187 benchmark_done
= true;
189 return benchmark_done
;
192 static void *poller(void *arg
)
195 while (!is_benchmark_done()) {
203 static void remove_xdp_program(void)
205 u32 curr_prog_id
= 0;
207 if (bpf_get_link_xdp_id(opt_ifindex
, &curr_prog_id
, opt_xdp_flags
)) {
208 printf("bpf_get_link_xdp_id failed\n");
211 if (prog_id
== curr_prog_id
)
212 bpf_set_link_xdp_fd(opt_ifindex
, -1, opt_xdp_flags
);
213 else if (!curr_prog_id
)
214 printf("couldn't find a prog id on a given interface\n");
216 printf("program on interface changed, not removing\n");
219 static void int_exit(int sig
)
221 benchmark_done
= true;
224 static void xdpsock_cleanup(void)
226 struct xsk_umem
*umem
= xsks
[0]->umem
->umem
;
230 for (i
= 0; i
< num_socks
; i
++)
231 xsk_socket__delete(xsks
[i
]->xsk
);
232 (void)xsk_umem__delete(umem
);
233 remove_xdp_program();
236 static void __exit_with_error(int error
, const char *file
, const char *func
,
239 fprintf(stderr
, "%s:%s:%i: errno: %d/\"%s\"\n", file
, func
,
240 line
, error
, strerror(error
));
242 remove_xdp_program();
246 #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, \
248 static void swap_mac_addresses(void *data
)
250 struct ether_header
*eth
= (struct ether_header
*)data
;
251 struct ether_addr
*src_addr
= (struct ether_addr
*)ð
->ether_shost
;
252 struct ether_addr
*dst_addr
= (struct ether_addr
*)ð
->ether_dhost
;
253 struct ether_addr tmp
;
256 *src_addr
= *dst_addr
;
260 static void hex_dump(void *pkt
, size_t length
, u64 addr
)
262 const unsigned char *address
= (unsigned char *)pkt
;
263 const unsigned char *line
= address
;
264 size_t line_size
= 32;
272 sprintf(buf
, "addr=%llu", addr
);
273 printf("length = %zu\n", length
);
274 printf("%s | ", buf
);
275 while (length
-- > 0) {
276 printf("%02X ", *address
++);
277 if (!(++i
% line_size
) || (length
== 0 && i
% line_size
)) {
279 while (i
++ % line_size
)
282 printf(" | "); /* right close */
283 while (line
< address
) {
285 printf("%c", (c
< 33 || c
== 255) ? 0x2E : c
);
289 printf("%s | ", buf
);
295 static void *memset32_htonl(void *dest
, u32 val
, u32 size
)
297 u32
*ptr
= (u32
*)dest
;
302 for (i
= 0; i
< (size
& (~0x3)); i
+= 4)
305 for (; i
< size
; i
++)
306 ((char *)dest
)[i
] = ((char *)&val
)[i
& 3];
312 * This function code has been taken from
313 * Linux kernel lib/checksum.c
315 static inline unsigned short from32to16(unsigned int x
)
317 /* add up 16-bit and 16-bit for 16+c bit */
318 x
= (x
& 0xffff) + (x
>> 16);
320 x
= (x
& 0xffff) + (x
>> 16);
325 * This function code has been taken from
326 * Linux kernel lib/checksum.c
328 static unsigned int do_csum(const unsigned char *buff
, int len
)
330 unsigned int result
= 0;
335 odd
= 1 & (unsigned long)buff
;
337 #ifdef __LITTLE_ENDIAN
338 result
+= (*buff
<< 8);
346 if (2 & (unsigned long)buff
) {
347 result
+= *(unsigned short *)buff
;
352 const unsigned char *end
= buff
+
353 ((unsigned int)len
& ~3);
354 unsigned int carry
= 0;
357 unsigned int w
= *(unsigned int *)buff
;
362 carry
= (w
> result
);
363 } while (buff
< end
);
365 result
= (result
& 0xffff) + (result
>> 16);
368 result
+= *(unsigned short *)buff
;
373 #ifdef __LITTLE_ENDIAN
376 result
+= (*buff
<< 8);
378 result
= from32to16(result
);
380 result
= ((result
>> 8) & 0xff) | ((result
& 0xff) << 8);
385 __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
);
388 * This is a version of ip_compute_csum() optimized for IP headers,
389 * which always checksum on 4 octet boundaries.
390 * This function code has been taken from
391 * Linux kernel lib/checksum.c
393 __sum16
ip_fast_csum(const void *iph
, unsigned int ihl
)
395 return (__force __sum16
)~do_csum(iph
, ihl
* 4);
399 * Fold a partial checksum
400 * This function code has been taken from
401 * Linux kernel include/asm-generic/checksum.h
403 static inline __sum16
csum_fold(__wsum csum
)
405 u32 sum
= (__force u32
)csum
;
407 sum
= (sum
& 0xffff) + (sum
>> 16);
408 sum
= (sum
& 0xffff) + (sum
>> 16);
409 return (__force __sum16
)~sum
;
413 * This function code has been taken from
414 * Linux kernel lib/checksum.c
416 static inline u32
from64to32(u64 x
)
418 /* add up 32-bit and 32-bit for 32+c bit */
419 x
= (x
& 0xffffffff) + (x
>> 32);
421 x
= (x
& 0xffffffff) + (x
>> 32);
425 __wsum
csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
,
426 __u32 len
, __u8 proto
, __wsum sum
);
429 * This function code has been taken from
430 * Linux kernel lib/checksum.c
432 __wsum
csum_tcpudp_nofold(__be32 saddr
, __be32 daddr
,
433 __u32 len
, __u8 proto
, __wsum sum
)
435 unsigned long long s
= (__force u32
)sum
;
437 s
+= (__force u32
)saddr
;
438 s
+= (__force u32
)daddr
;
439 #ifdef __BIG_ENDIAN__
442 s
+= (proto
+ len
) << 8;
444 return (__force __wsum
)from64to32(s
);
448 * This function has been taken from
449 * Linux kernel include/asm-generic/checksum.h
451 static inline __sum16
452 csum_tcpudp_magic(__be32 saddr
, __be32 daddr
, __u32 len
,
453 __u8 proto
, __wsum sum
)
455 return csum_fold(csum_tcpudp_nofold(saddr
, daddr
, len
, proto
, sum
));
458 static inline u16
udp_csum(u32 saddr
, u32 daddr
, u32 len
,
459 u8 proto
, u16
*udp_pkt
)
464 /* udp hdr and data */
465 for (; cnt
< len
; cnt
+= 2)
466 csum
+= udp_pkt
[cnt
>> 1];
468 return csum_tcpudp_magic(saddr
, daddr
, len
, proto
, csum
);
471 #define ETH_FCS_SIZE 4
473 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
474 sizeof(struct udphdr))
476 #define PKT_SIZE (opt_pkt_size - ETH_FCS_SIZE)
477 #define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
478 #define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
479 #define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
481 static u8 pkt_data
[XSK_UMEM__DEFAULT_FRAME_SIZE
];
483 static void gen_eth_hdr_data(void)
485 struct udphdr
*udp_hdr
= (struct udphdr
*)(pkt_data
+
486 sizeof(struct ethhdr
) +
487 sizeof(struct iphdr
));
488 struct iphdr
*ip_hdr
= (struct iphdr
*)(pkt_data
+
489 sizeof(struct ethhdr
));
490 struct ethhdr
*eth_hdr
= (struct ethhdr
*)pkt_data
;
492 /* ethernet header */
493 memcpy(eth_hdr
->h_dest
, "\x3c\xfd\xfe\x9e\x7f\x71", ETH_ALEN
);
494 memcpy(eth_hdr
->h_source
, "\xec\xb1\xd7\x98\x3a\xc0", ETH_ALEN
);
495 eth_hdr
->h_proto
= htons(ETH_P_IP
);
498 ip_hdr
->version
= IPVERSION
;
499 ip_hdr
->ihl
= 0x5; /* 20 byte header */
501 ip_hdr
->tot_len
= htons(IP_PKT_SIZE
);
503 ip_hdr
->frag_off
= 0;
504 ip_hdr
->ttl
= IPDEFTTL
;
505 ip_hdr
->protocol
= IPPROTO_UDP
;
506 ip_hdr
->saddr
= htonl(0x0a0a0a10);
507 ip_hdr
->daddr
= htonl(0x0a0a0a20);
509 /* IP header checksum */
511 ip_hdr
->check
= ip_fast_csum((const void *)ip_hdr
, ip_hdr
->ihl
);
514 udp_hdr
->source
= htons(0x1000);
515 udp_hdr
->dest
= htons(0x1000);
516 udp_hdr
->len
= htons(UDP_PKT_SIZE
);
519 memset32_htonl(pkt_data
+ PKT_HDR_SIZE
, opt_pkt_fill_pattern
,
522 /* UDP header checksum */
524 udp_hdr
->check
= udp_csum(ip_hdr
->saddr
, ip_hdr
->daddr
, UDP_PKT_SIZE
,
525 IPPROTO_UDP
, (u16
*)udp_hdr
);
528 static void gen_eth_frame(struct xsk_umem_info
*umem
, u64 addr
)
530 memcpy(xsk_umem__get_data(umem
->buffer
, addr
), pkt_data
,
534 static struct xsk_umem_info
*xsk_configure_umem(void *buffer
, u64 size
)
536 struct xsk_umem_info
*umem
;
537 struct xsk_umem_config cfg
= {
538 .fill_size
= XSK_RING_PROD__DEFAULT_NUM_DESCS
,
539 .comp_size
= XSK_RING_CONS__DEFAULT_NUM_DESCS
,
540 .frame_size
= opt_xsk_frame_size
,
541 .frame_headroom
= XSK_UMEM__DEFAULT_FRAME_HEADROOM
,
542 .flags
= opt_umem_flags
546 umem
= calloc(1, sizeof(*umem
));
548 exit_with_error(errno
);
550 ret
= xsk_umem__create(&umem
->umem
, buffer
, size
, &umem
->fq
, &umem
->cq
,
553 exit_with_error(-ret
);
555 umem
->buffer
= buffer
;
559 static void xsk_populate_fill_ring(struct xsk_umem_info
*umem
)
564 ret
= xsk_ring_prod__reserve(&umem
->fq
,
565 XSK_RING_PROD__DEFAULT_NUM_DESCS
, &idx
);
566 if (ret
!= XSK_RING_PROD__DEFAULT_NUM_DESCS
)
567 exit_with_error(-ret
);
568 for (i
= 0; i
< XSK_RING_PROD__DEFAULT_NUM_DESCS
; i
++)
569 *xsk_ring_prod__fill_addr(&umem
->fq
, idx
++) =
570 i
* opt_xsk_frame_size
;
571 xsk_ring_prod__submit(&umem
->fq
, XSK_RING_PROD__DEFAULT_NUM_DESCS
);
574 static struct xsk_socket_info
*xsk_configure_socket(struct xsk_umem_info
*umem
,
577 struct xsk_socket_config cfg
;
578 struct xsk_socket_info
*xsk
;
579 struct xsk_ring_cons
*rxr
;
580 struct xsk_ring_prod
*txr
;
583 xsk
= calloc(1, sizeof(*xsk
));
585 exit_with_error(errno
);
588 cfg
.rx_size
= XSK_RING_CONS__DEFAULT_NUM_DESCS
;
589 cfg
.tx_size
= XSK_RING_PROD__DEFAULT_NUM_DESCS
;
590 if (opt_num_xsks
> 1)
591 cfg
.libbpf_flags
= XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD
;
593 cfg
.libbpf_flags
= 0;
594 cfg
.xdp_flags
= opt_xdp_flags
;
595 cfg
.bind_flags
= opt_xdp_bind_flags
;
597 rxr
= rx
? &xsk
->rx
: NULL
;
598 txr
= tx
? &xsk
->tx
: NULL
;
599 ret
= xsk_socket__create(&xsk
->xsk
, opt_if
, opt_queue
, umem
->umem
,
602 exit_with_error(-ret
);
604 ret
= bpf_get_link_xdp_id(opt_ifindex
, &prog_id
, opt_xdp_flags
);
606 exit_with_error(-ret
);
611 static struct option long_options
[] = {
612 {"rxdrop", no_argument
, 0, 'r'},
613 {"txonly", no_argument
, 0, 't'},
614 {"l2fwd", no_argument
, 0, 'l'},
615 {"interface", required_argument
, 0, 'i'},
616 {"queue", required_argument
, 0, 'q'},
617 {"poll", no_argument
, 0, 'p'},
618 {"xdp-skb", no_argument
, 0, 'S'},
619 {"xdp-native", no_argument
, 0, 'N'},
620 {"interval", required_argument
, 0, 'n'},
621 {"zero-copy", no_argument
, 0, 'z'},
622 {"copy", no_argument
, 0, 'c'},
623 {"frame-size", required_argument
, 0, 'f'},
624 {"no-need-wakeup", no_argument
, 0, 'm'},
625 {"unaligned", no_argument
, 0, 'u'},
626 {"shared-umem", no_argument
, 0, 'M'},
627 {"force", no_argument
, 0, 'F'},
628 {"duration", required_argument
, 0, 'd'},
629 {"batch-size", required_argument
, 0, 'b'},
630 {"tx-pkt-count", required_argument
, 0, 'C'},
631 {"tx-pkt-size", required_argument
, 0, 's'},
632 {"tx-pkt-pattern", required_argument
, 0, 'P'},
636 static void usage(const char *prog
)
639 " Usage: %s [OPTIONS]\n"
641 " -r, --rxdrop Discard all incoming packets (default)\n"
642 " -t, --txonly Only send packets\n"
643 " -l, --l2fwd MAC swap L2 forwarding\n"
644 " -i, --interface=n Run on interface n\n"
645 " -q, --queue=n Use queue n (default 0)\n"
646 " -p, --poll Use poll syscall\n"
647 " -S, --xdp-skb=n Use XDP skb-mod\n"
648 " -N, --xdp-native=n Enforce XDP native mode\n"
649 " -n, --interval=n Specify statistics update interval (default 1 sec).\n"
650 " -z, --zero-copy Force zero-copy mode.\n"
651 " -c, --copy Force copy mode.\n"
652 " -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
653 " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n"
654 " -u, --unaligned Enable unaligned chunk placement\n"
655 " -M, --shared-umem Enable XDP_SHARED_UMEM\n"
656 " -F, --force Force loading the XDP prog\n"
657 " -d, --duration=n Duration in secs to run command.\n"
658 " Default: forever.\n"
659 " -b, --batch-size=n Batch size for sending or receiving\n"
660 " packets. Default: %d\n"
661 " -C, --tx-pkt-count=n Number of packets to send.\n"
662 " Default: Continuous packets.\n"
663 " -s, --tx-pkt-size=n Transmit packet size.\n"
664 " (Default: %d bytes)\n"
665 " Min size: %d, Max size %d.\n"
666 " -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
668 fprintf(stderr
, str
, prog
, XSK_UMEM__DEFAULT_FRAME_SIZE
,
669 opt_batch_size
, MIN_PKT_SIZE
, MIN_PKT_SIZE
,
670 XSK_UMEM__DEFAULT_FRAME_SIZE
, opt_pkt_fill_pattern
);
675 static void parse_command_line(int argc
, char **argv
)
682 c
= getopt_long(argc
, argv
, "Frtli:q:pSNn:czf:muMd:b:C:s:P:",
683 long_options
, &option_index
);
689 opt_bench
= BENCH_RXDROP
;
692 opt_bench
= BENCH_TXONLY
;
695 opt_bench
= BENCH_L2FWD
;
701 opt_queue
= atoi(optarg
);
707 opt_xdp_flags
|= XDP_FLAGS_SKB_MODE
;
708 opt_xdp_bind_flags
|= XDP_COPY
;
711 /* default, set below */
714 opt_interval
= atoi(optarg
);
717 opt_xdp_bind_flags
|= XDP_ZEROCOPY
;
720 opt_xdp_bind_flags
|= XDP_COPY
;
723 opt_umem_flags
|= XDP_UMEM_UNALIGNED_CHUNK_FLAG
;
724 opt_unaligned_chunks
= 1;
725 opt_mmap_flags
= MAP_HUGETLB
;
728 opt_xdp_flags
&= ~XDP_FLAGS_UPDATE_IF_NOEXIST
;
731 opt_xsk_frame_size
= atoi(optarg
);
734 opt_need_wakeup
= false;
735 opt_xdp_bind_flags
&= ~XDP_USE_NEED_WAKEUP
;
738 opt_num_xsks
= MAX_SOCKS
;
741 opt_duration
= atoi(optarg
);
742 opt_duration
*= 1000000000;
745 opt_batch_size
= atoi(optarg
);
748 opt_pkt_count
= atoi(optarg
);
751 opt_pkt_size
= atoi(optarg
);
752 if (opt_pkt_size
> (XSK_UMEM__DEFAULT_FRAME_SIZE
) ||
753 opt_pkt_size
< MIN_PKT_SIZE
) {
755 "ERROR: Invalid frame size %d\n",
757 usage(basename(argv
[0]));
761 opt_pkt_fill_pattern
= strtol(optarg
, NULL
, 16);
764 usage(basename(argv
[0]));
768 if (!(opt_xdp_flags
& XDP_FLAGS_SKB_MODE
))
769 opt_xdp_flags
|= XDP_FLAGS_DRV_MODE
;
771 opt_ifindex
= if_nametoindex(opt_if
);
773 fprintf(stderr
, "ERROR: interface \"%s\" does not exist\n",
775 usage(basename(argv
[0]));
778 if ((opt_xsk_frame_size
& (opt_xsk_frame_size
- 1)) &&
779 !opt_unaligned_chunks
) {
780 fprintf(stderr
, "--frame-size=%d is not a power of two\n",
782 usage(basename(argv
[0]));
786 static void kick_tx(struct xsk_socket_info
*xsk
)
790 ret
= sendto(xsk_socket__fd(xsk
->xsk
), NULL
, 0, MSG_DONTWAIT
, NULL
, 0);
791 if (ret
>= 0 || errno
== ENOBUFS
|| errno
== EAGAIN
||
792 errno
== EBUSY
|| errno
== ENETDOWN
)
794 exit_with_error(errno
);
797 static inline void complete_tx_l2fwd(struct xsk_socket_info
*xsk
,
800 struct xsk_umem_info
*umem
= xsk
->umem
;
801 u32 idx_cq
= 0, idx_fq
= 0;
805 if (!xsk
->outstanding_tx
)
808 if (!opt_need_wakeup
|| xsk_ring_prod__needs_wakeup(&xsk
->tx
))
811 ndescs
= (xsk
->outstanding_tx
> opt_batch_size
) ? opt_batch_size
:
814 /* re-add completed Tx buffers */
815 rcvd
= xsk_ring_cons__peek(&umem
->cq
, ndescs
, &idx_cq
);
820 ret
= xsk_ring_prod__reserve(&umem
->fq
, rcvd
, &idx_fq
);
821 while (ret
!= rcvd
) {
823 exit_with_error(-ret
);
824 if (xsk_ring_prod__needs_wakeup(&umem
->fq
))
825 ret
= poll(fds
, num_socks
, opt_timeout
);
826 ret
= xsk_ring_prod__reserve(&umem
->fq
, rcvd
, &idx_fq
);
829 for (i
= 0; i
< rcvd
; i
++)
830 *xsk_ring_prod__fill_addr(&umem
->fq
, idx_fq
++) =
831 *xsk_ring_cons__comp_addr(&umem
->cq
, idx_cq
++);
833 xsk_ring_prod__submit(&xsk
->umem
->fq
, rcvd
);
834 xsk_ring_cons__release(&xsk
->umem
->cq
, rcvd
);
835 xsk
->outstanding_tx
-= rcvd
;
836 xsk
->tx_npkts
+= rcvd
;
840 static inline void complete_tx_only(struct xsk_socket_info
*xsk
,
846 if (!xsk
->outstanding_tx
)
849 if (!opt_need_wakeup
|| xsk_ring_prod__needs_wakeup(&xsk
->tx
))
852 rcvd
= xsk_ring_cons__peek(&xsk
->umem
->cq
, batch_size
, &idx
);
854 xsk_ring_cons__release(&xsk
->umem
->cq
, rcvd
);
855 xsk
->outstanding_tx
-= rcvd
;
856 xsk
->tx_npkts
+= rcvd
;
860 static void rx_drop(struct xsk_socket_info
*xsk
, struct pollfd
*fds
)
862 unsigned int rcvd
, i
;
863 u32 idx_rx
= 0, idx_fq
= 0;
866 rcvd
= xsk_ring_cons__peek(&xsk
->rx
, opt_batch_size
, &idx_rx
);
868 if (xsk_ring_prod__needs_wakeup(&xsk
->umem
->fq
))
869 ret
= poll(fds
, num_socks
, opt_timeout
);
873 ret
= xsk_ring_prod__reserve(&xsk
->umem
->fq
, rcvd
, &idx_fq
);
874 while (ret
!= rcvd
) {
876 exit_with_error(-ret
);
877 if (xsk_ring_prod__needs_wakeup(&xsk
->umem
->fq
))
878 ret
= poll(fds
, num_socks
, opt_timeout
);
879 ret
= xsk_ring_prod__reserve(&xsk
->umem
->fq
, rcvd
, &idx_fq
);
882 for (i
= 0; i
< rcvd
; i
++) {
883 u64 addr
= xsk_ring_cons__rx_desc(&xsk
->rx
, idx_rx
)->addr
;
884 u32 len
= xsk_ring_cons__rx_desc(&xsk
->rx
, idx_rx
++)->len
;
885 u64 orig
= xsk_umem__extract_addr(addr
);
887 addr
= xsk_umem__add_offset_to_addr(addr
);
888 char *pkt
= xsk_umem__get_data(xsk
->umem
->buffer
, addr
);
890 hex_dump(pkt
, len
, addr
);
891 *xsk_ring_prod__fill_addr(&xsk
->umem
->fq
, idx_fq
++) = orig
;
894 xsk_ring_prod__submit(&xsk
->umem
->fq
, rcvd
);
895 xsk_ring_cons__release(&xsk
->rx
, rcvd
);
896 xsk
->rx_npkts
+= rcvd
;
899 static void rx_drop_all(void)
901 struct pollfd fds
[MAX_SOCKS
] = {};
904 for (i
= 0; i
< num_socks
; i
++) {
905 fds
[i
].fd
= xsk_socket__fd(xsks
[i
]->xsk
);
906 fds
[i
].events
= POLLIN
;
911 ret
= poll(fds
, num_socks
, opt_timeout
);
916 for (i
= 0; i
< num_socks
; i
++)
917 rx_drop(xsks
[i
], fds
);
924 static void tx_only(struct xsk_socket_info
*xsk
, u32 frame_nb
, int batch_size
)
929 while (xsk_ring_prod__reserve(&xsk
->tx
, batch_size
, &idx
) <
931 complete_tx_only(xsk
, batch_size
);
934 for (i
= 0; i
< batch_size
; i
++) {
935 struct xdp_desc
*tx_desc
= xsk_ring_prod__tx_desc(&xsk
->tx
,
937 tx_desc
->addr
= (frame_nb
+ i
) << XSK_UMEM__DEFAULT_FRAME_SHIFT
;
938 tx_desc
->len
= PKT_SIZE
;
941 xsk_ring_prod__submit(&xsk
->tx
, batch_size
);
942 xsk
->outstanding_tx
+= batch_size
;
943 frame_nb
+= batch_size
;
944 frame_nb
%= NUM_FRAMES
;
945 complete_tx_only(xsk
, batch_size
);
948 static inline int get_batch_size(int pkt_cnt
)
951 return opt_batch_size
;
953 if (pkt_cnt
+ opt_batch_size
<= opt_pkt_count
)
954 return opt_batch_size
;
956 return opt_pkt_count
- pkt_cnt
;
959 static void complete_tx_only_all(void)
966 for (i
= 0; i
< num_socks
; i
++) {
967 if (xsks
[i
]->outstanding_tx
) {
968 complete_tx_only(xsks
[i
], opt_batch_size
);
969 pending
= !!xsks
[i
]->outstanding_tx
;
975 static void tx_only_all(void)
977 struct pollfd fds
[MAX_SOCKS
] = {};
978 u32 frame_nb
[MAX_SOCKS
] = {};
982 for (i
= 0; i
< num_socks
; i
++) {
983 fds
[0].fd
= xsk_socket__fd(xsks
[i
]->xsk
);
984 fds
[0].events
= POLLOUT
;
987 while ((opt_pkt_count
&& pkt_cnt
< opt_pkt_count
) || !opt_pkt_count
) {
988 int batch_size
= get_batch_size(pkt_cnt
);
991 ret
= poll(fds
, num_socks
, opt_timeout
);
995 if (!(fds
[0].revents
& POLLOUT
))
999 for (i
= 0; i
< num_socks
; i
++)
1000 tx_only(xsks
[i
], frame_nb
[i
], batch_size
);
1002 pkt_cnt
+= batch_size
;
1009 complete_tx_only_all();
1012 static void l2fwd(struct xsk_socket_info
*xsk
, struct pollfd
*fds
)
1014 unsigned int rcvd
, i
;
1015 u32 idx_rx
= 0, idx_tx
= 0;
1018 complete_tx_l2fwd(xsk
, fds
);
1020 rcvd
= xsk_ring_cons__peek(&xsk
->rx
, opt_batch_size
, &idx_rx
);
1022 if (xsk_ring_prod__needs_wakeup(&xsk
->umem
->fq
))
1023 ret
= poll(fds
, num_socks
, opt_timeout
);
1027 ret
= xsk_ring_prod__reserve(&xsk
->tx
, rcvd
, &idx_tx
);
1028 while (ret
!= rcvd
) {
1030 exit_with_error(-ret
);
1031 if (xsk_ring_prod__needs_wakeup(&xsk
->tx
))
1033 ret
= xsk_ring_prod__reserve(&xsk
->tx
, rcvd
, &idx_tx
);
1036 for (i
= 0; i
< rcvd
; i
++) {
1037 u64 addr
= xsk_ring_cons__rx_desc(&xsk
->rx
, idx_rx
)->addr
;
1038 u32 len
= xsk_ring_cons__rx_desc(&xsk
->rx
, idx_rx
++)->len
;
1041 addr
= xsk_umem__add_offset_to_addr(addr
);
1042 char *pkt
= xsk_umem__get_data(xsk
->umem
->buffer
, addr
);
1044 swap_mac_addresses(pkt
);
1046 hex_dump(pkt
, len
, addr
);
1047 xsk_ring_prod__tx_desc(&xsk
->tx
, idx_tx
)->addr
= orig
;
1048 xsk_ring_prod__tx_desc(&xsk
->tx
, idx_tx
++)->len
= len
;
1051 xsk_ring_prod__submit(&xsk
->tx
, rcvd
);
1052 xsk_ring_cons__release(&xsk
->rx
, rcvd
);
1054 xsk
->rx_npkts
+= rcvd
;
1055 xsk
->outstanding_tx
+= rcvd
;
1058 static void l2fwd_all(void)
1060 struct pollfd fds
[MAX_SOCKS
] = {};
1063 for (i
= 0; i
< num_socks
; i
++) {
1064 fds
[i
].fd
= xsk_socket__fd(xsks
[i
]->xsk
);
1065 fds
[i
].events
= POLLOUT
| POLLIN
;
1070 ret
= poll(fds
, num_socks
, opt_timeout
);
1075 for (i
= 0; i
< num_socks
; i
++)
1076 l2fwd(xsks
[i
], fds
);
1083 static void load_xdp_program(char **argv
, struct bpf_object
**obj
)
1085 struct bpf_prog_load_attr prog_load_attr
= {
1086 .prog_type
= BPF_PROG_TYPE_XDP
,
1088 char xdp_filename
[256];
1091 snprintf(xdp_filename
, sizeof(xdp_filename
), "%s_kern.o", argv
[0]);
1092 prog_load_attr
.file
= xdp_filename
;
1094 if (bpf_prog_load_xattr(&prog_load_attr
, obj
, &prog_fd
))
1097 fprintf(stderr
, "ERROR: no program found: %s\n",
1102 if (bpf_set_link_xdp_fd(opt_ifindex
, prog_fd
, opt_xdp_flags
) < 0) {
1103 fprintf(stderr
, "ERROR: link set xdp fd failed\n");
1108 static void enter_xsks_into_map(struct bpf_object
*obj
)
1110 struct bpf_map
*map
;
1113 map
= bpf_object__find_map_by_name(obj
, "xsks_map");
1114 xsks_map
= bpf_map__fd(map
);
1116 fprintf(stderr
, "ERROR: no xsks map found: %s\n",
1117 strerror(xsks_map
));
1121 for (i
= 0; i
< num_socks
; i
++) {
1122 int fd
= xsk_socket__fd(xsks
[i
]->xsk
);
1126 ret
= bpf_map_update_elem(xsks_map
, &key
, &fd
, 0);
1128 fprintf(stderr
, "ERROR: bpf_map_update_elem %d\n", i
);
1134 int main(int argc
, char **argv
)
1136 struct rlimit r
= {RLIM_INFINITY
, RLIM_INFINITY
};
1137 bool rx
= false, tx
= false;
1138 struct xsk_umem_info
*umem
;
1139 struct bpf_object
*obj
;
1144 parse_command_line(argc
, argv
);
1146 if (setrlimit(RLIMIT_MEMLOCK
, &r
)) {
1147 fprintf(stderr
, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
1152 if (opt_num_xsks
> 1)
1153 load_xdp_program(argv
, &obj
);
1155 /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
1156 bufs
= mmap(NULL
, NUM_FRAMES
* opt_xsk_frame_size
,
1157 PROT_READ
| PROT_WRITE
,
1158 MAP_PRIVATE
| MAP_ANONYMOUS
| opt_mmap_flags
, -1, 0);
1159 if (bufs
== MAP_FAILED
) {
1160 printf("ERROR: mmap failed\n");
1164 /* Create sockets... */
1165 umem
= xsk_configure_umem(bufs
, NUM_FRAMES
* opt_xsk_frame_size
);
1166 if (opt_bench
== BENCH_RXDROP
|| opt_bench
== BENCH_L2FWD
) {
1168 xsk_populate_fill_ring(umem
);
1170 if (opt_bench
== BENCH_L2FWD
|| opt_bench
== BENCH_TXONLY
)
1172 for (i
= 0; i
< opt_num_xsks
; i
++)
1173 xsks
[num_socks
++] = xsk_configure_socket(umem
, rx
, tx
);
1175 if (opt_bench
== BENCH_TXONLY
) {
1178 for (i
= 0; i
< NUM_FRAMES
; i
++)
1179 gen_eth_frame(umem
, i
* opt_xsk_frame_size
);
1182 if (opt_num_xsks
> 1 && opt_bench
!= BENCH_TXONLY
)
1183 enter_xsks_into_map(obj
);
1185 signal(SIGINT
, int_exit
);
1186 signal(SIGTERM
, int_exit
);
1187 signal(SIGABRT
, int_exit
);
1189 setlocale(LC_ALL
, "");
1191 ret
= pthread_create(&pt
, NULL
, poller
, NULL
);
1193 exit_with_error(ret
);
1195 prev_time
= get_nsecs();
1196 start_time
= prev_time
;
1198 if (opt_bench
== BENCH_RXDROP
)
1200 else if (opt_bench
== BENCH_TXONLY
)
1205 benchmark_done
= true;
1207 pthread_join(pt
, NULL
);