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/limits.h>
15 #include <linux/udp.h>
16 #include <arpa/inet.h>
18 #include <net/ethernet.h>
28 #include <sys/resource.h>
29 #include <sys/socket.h>
30 #include <sys/types.h>
34 #include <bpf/libbpf.h>
51 #define NUM_FRAMES (4 * 1024)
52 #define MIN_PKT_SIZE 64
54 #define DEBUG_HEXDUMP 0
61 static unsigned long prev_time;
69 static enum benchmark_type opt_bench = BENCH_RXDROP;
70 static u32 opt_xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
71 static const char *opt_if = "";
72 static int opt_ifindex;
74 static unsigned long opt_duration;
75 static unsigned long start_time;
76 static bool benchmark_done;
77 static u32 opt_batch_size = 64;
78 static int opt_pkt_count;
79 static u16 opt_pkt_size = MIN_PKT_SIZE;
80 static u32 opt_pkt_fill_pattern = 0x12345678;
81 static bool opt_extra_stats;
82 static bool opt_quiet;
83 static bool opt_app_stats;
84 static const char *opt_irq_str = "";
86 static int irqs_at_init = -1;
88 static int opt_interval = 1;
89 static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP;
90 static u32 opt_umem_flags;
91 static int opt_unaligned_chunks;
92 static int opt_mmap_flags;
93 static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE;
94 static int opt_timeout = 1000;
95 static bool opt_need_wakeup = true;
96 static u32 opt_num_xsks = 1;
99 struct xsk_ring_stats {
100 unsigned long rx_npkts;
101 unsigned long tx_npkts;
102 unsigned long rx_dropped_npkts;
103 unsigned long rx_invalid_npkts;
104 unsigned long tx_invalid_npkts;
105 unsigned long rx_full_npkts;
106 unsigned long rx_fill_empty_npkts;
107 unsigned long tx_empty_npkts;
108 unsigned long prev_rx_npkts;
109 unsigned long prev_tx_npkts;
110 unsigned long prev_rx_dropped_npkts;
111 unsigned long prev_rx_invalid_npkts;
112 unsigned long prev_tx_invalid_npkts;
113 unsigned long prev_rx_full_npkts;
114 unsigned long prev_rx_fill_empty_npkts;
115 unsigned long prev_tx_empty_npkts;
118 struct xsk_driver_stats {
120 unsigned long prev_intrs;
123 struct xsk_app_stats {
124 unsigned long rx_empty_polls;
125 unsigned long fill_fail_polls;
126 unsigned long copy_tx_sendtos;
127 unsigned long tx_wakeup_sendtos;
128 unsigned long opt_polls;
129 unsigned long prev_rx_empty_polls;
130 unsigned long prev_fill_fail_polls;
131 unsigned long prev_copy_tx_sendtos;
132 unsigned long prev_tx_wakeup_sendtos;
133 unsigned long prev_opt_polls;
136 struct xsk_umem_info {
137 struct xsk_ring_prod fq;
138 struct xsk_ring_cons cq;
139 struct xsk_umem *umem;
143 struct xsk_socket_info {
144 struct xsk_ring_cons rx;
145 struct xsk_ring_prod tx;
146 struct xsk_umem_info *umem;
147 struct xsk_socket *xsk;
148 struct xsk_ring_stats ring_stats;
149 struct xsk_app_stats app_stats;
150 struct xsk_driver_stats drv_stats;
154 static int num_socks;
155 struct xsk_socket_info *xsks[MAX_SOCKS];
157 static unsigned long get_nsecs(void)
161 clock_gettime(CLOCK_MONOTONIC, &ts);
162 return ts.tv_sec * 1000000000UL + ts.tv_nsec;
165 static void print_benchmark(bool running)
167 const char *bench_str = "INVALID";
169 if (opt_bench == BENCH_RXDROP)
170 bench_str = "rxdrop";
171 else if (opt_bench == BENCH_TXONLY)
172 bench_str = "txonly";
173 else if (opt_bench == BENCH_L2FWD)
176 printf("%s:%d %s ", opt_if, opt_queue, bench_str);
177 if (opt_xdp_flags & XDP_FLAGS_SKB_MODE)
179 else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE)
188 printf("running...");
193 static int xsk_get_xdp_stats(int fd, struct xsk_socket_info *xsk)
195 struct xdp_statistics stats;
199 optlen = sizeof(stats);
200 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
204 if (optlen == sizeof(struct xdp_statistics)) {
205 xsk->ring_stats.rx_dropped_npkts = stats.rx_dropped;
206 xsk->ring_stats.rx_invalid_npkts = stats.rx_invalid_descs;
207 xsk->ring_stats.tx_invalid_npkts = stats.tx_invalid_descs;
208 xsk->ring_stats.rx_full_npkts = stats.rx_ring_full;
209 xsk->ring_stats.rx_fill_empty_npkts = stats.rx_fill_ring_empty_descs;
210 xsk->ring_stats.tx_empty_npkts = stats.tx_ring_empty_descs;
217 static void dump_app_stats(long dt)
221 for (i = 0; i < num_socks && xsks[i]; i++) {
222 char *fmt = "%-18s %'-14.0f %'-14lu\n";
223 double rx_empty_polls_ps, fill_fail_polls_ps, copy_tx_sendtos_ps,
224 tx_wakeup_sendtos_ps, opt_polls_ps;
226 rx_empty_polls_ps = (xsks[i]->app_stats.rx_empty_polls -
227 xsks[i]->app_stats.prev_rx_empty_polls) * 1000000000. / dt;
228 fill_fail_polls_ps = (xsks[i]->app_stats.fill_fail_polls -
229 xsks[i]->app_stats.prev_fill_fail_polls) * 1000000000. / dt;
230 copy_tx_sendtos_ps = (xsks[i]->app_stats.copy_tx_sendtos -
231 xsks[i]->app_stats.prev_copy_tx_sendtos) * 1000000000. / dt;
232 tx_wakeup_sendtos_ps = (xsks[i]->app_stats.tx_wakeup_sendtos -
233 xsks[i]->app_stats.prev_tx_wakeup_sendtos)
235 opt_polls_ps = (xsks[i]->app_stats.opt_polls -
236 xsks[i]->app_stats.prev_opt_polls) * 1000000000. / dt;
238 printf("\n%-18s %-14s %-14s\n", "", "calls/s", "count");
239 printf(fmt, "rx empty polls", rx_empty_polls_ps, xsks[i]->app_stats.rx_empty_polls);
240 printf(fmt, "fill fail polls", fill_fail_polls_ps,
241 xsks[i]->app_stats.fill_fail_polls);
242 printf(fmt, "copy tx sendtos", copy_tx_sendtos_ps,
243 xsks[i]->app_stats.copy_tx_sendtos);
244 printf(fmt, "tx wakeup sendtos", tx_wakeup_sendtos_ps,
245 xsks[i]->app_stats.tx_wakeup_sendtos);
246 printf(fmt, "opt polls", opt_polls_ps, xsks[i]->app_stats.opt_polls);
248 xsks[i]->app_stats.prev_rx_empty_polls = xsks[i]->app_stats.rx_empty_polls;
249 xsks[i]->app_stats.prev_fill_fail_polls = xsks[i]->app_stats.fill_fail_polls;
250 xsks[i]->app_stats.prev_copy_tx_sendtos = xsks[i]->app_stats.copy_tx_sendtos;
251 xsks[i]->app_stats.prev_tx_wakeup_sendtos = xsks[i]->app_stats.tx_wakeup_sendtos;
252 xsks[i]->app_stats.prev_opt_polls = xsks[i]->app_stats.opt_polls;
256 static bool get_interrupt_number(void)
262 f_int_proc = fopen("/proc/interrupts", "r");
263 if (f_int_proc == NULL) {
264 printf("Failed to open /proc/interrupts.\n");
268 while (!feof(f_int_proc) && !found) {
269 /* Make sure to read a full line at a time */
270 if (fgets(line, sizeof(line), f_int_proc) == NULL ||
271 line[strlen(line) - 1] != '\n') {
272 printf("Error reading from interrupts file\n");
276 /* Extract interrupt number from line */
277 if (strstr(line, opt_irq_str) != NULL) {
289 static int get_irqs(void)
291 char count_path[PATH_MAX];
292 int total_intrs = -1;
296 snprintf(count_path, sizeof(count_path),
297 "/sys/kernel/irq/%i/per_cpu_count", irq_no);
298 f_count_proc = fopen(count_path, "r");
299 if (f_count_proc == NULL) {
300 printf("Failed to open %s\n", count_path);
304 if (fgets(line, sizeof(line), f_count_proc) == NULL ||
305 line[strlen(line) - 1] != '\n') {
306 printf("Error reading from %s\n", count_path);
308 static const char com[2] = ",";
312 token = strtok(line, com);
313 while (token != NULL) {
314 /* sum up interrupts across all cores */
315 total_intrs += atoi(token);
316 token = strtok(NULL, com);
320 fclose(f_count_proc);
325 static void dump_driver_stats(long dt)
329 for (i = 0; i < num_socks && xsks[i]; i++) {
330 char *fmt = "%-18s %'-14.0f %'-14lu\n";
332 int n_ints = get_irqs();
335 printf("error getting intr info for intr %i\n", irq_no);
338 xsks[i]->drv_stats.intrs = n_ints - irqs_at_init;
340 intrs_ps = (xsks[i]->drv_stats.intrs - xsks[i]->drv_stats.prev_intrs) *
343 printf("\n%-18s %-14s %-14s\n", "", "intrs/s", "count");
344 printf(fmt, "irqs", intrs_ps, xsks[i]->drv_stats.intrs);
346 xsks[i]->drv_stats.prev_intrs = xsks[i]->drv_stats.intrs;
350 static void dump_stats(void)
352 unsigned long now = get_nsecs();
353 long dt = now - prev_time;
358 for (i = 0; i < num_socks && xsks[i]; i++) {
359 char *fmt = "%-18s %'-14.0f %'-14lu\n";
360 double rx_pps, tx_pps, dropped_pps, rx_invalid_pps, full_pps, fill_empty_pps,
361 tx_invalid_pps, tx_empty_pps;
363 rx_pps = (xsks[i]->ring_stats.rx_npkts - xsks[i]->ring_stats.prev_rx_npkts) *
365 tx_pps = (xsks[i]->ring_stats.tx_npkts - xsks[i]->ring_stats.prev_tx_npkts) *
368 printf("\n sock%d@", i);
369 print_benchmark(false);
372 printf("%-18s %-14s %-14s %-14.2f\n", "", "pps", "pkts",
374 printf(fmt, "rx", rx_pps, xsks[i]->ring_stats.rx_npkts);
375 printf(fmt, "tx", tx_pps, xsks[i]->ring_stats.tx_npkts);
377 xsks[i]->ring_stats.prev_rx_npkts = xsks[i]->ring_stats.rx_npkts;
378 xsks[i]->ring_stats.prev_tx_npkts = xsks[i]->ring_stats.tx_npkts;
380 if (opt_extra_stats) {
381 if (!xsk_get_xdp_stats(xsk_socket__fd(xsks[i]->xsk), xsks[i])) {
382 dropped_pps = (xsks[i]->ring_stats.rx_dropped_npkts -
383 xsks[i]->ring_stats.prev_rx_dropped_npkts) *
385 rx_invalid_pps = (xsks[i]->ring_stats.rx_invalid_npkts -
386 xsks[i]->ring_stats.prev_rx_invalid_npkts) *
388 tx_invalid_pps = (xsks[i]->ring_stats.tx_invalid_npkts -
389 xsks[i]->ring_stats.prev_tx_invalid_npkts) *
391 full_pps = (xsks[i]->ring_stats.rx_full_npkts -
392 xsks[i]->ring_stats.prev_rx_full_npkts) *
394 fill_empty_pps = (xsks[i]->ring_stats.rx_fill_empty_npkts -
395 xsks[i]->ring_stats.prev_rx_fill_empty_npkts) *
397 tx_empty_pps = (xsks[i]->ring_stats.tx_empty_npkts -
398 xsks[i]->ring_stats.prev_tx_empty_npkts) *
401 printf(fmt, "rx dropped", dropped_pps,
402 xsks[i]->ring_stats.rx_dropped_npkts);
403 printf(fmt, "rx invalid", rx_invalid_pps,
404 xsks[i]->ring_stats.rx_invalid_npkts);
405 printf(fmt, "tx invalid", tx_invalid_pps,
406 xsks[i]->ring_stats.tx_invalid_npkts);
407 printf(fmt, "rx queue full", full_pps,
408 xsks[i]->ring_stats.rx_full_npkts);
409 printf(fmt, "fill ring empty", fill_empty_pps,
410 xsks[i]->ring_stats.rx_fill_empty_npkts);
411 printf(fmt, "tx ring empty", tx_empty_pps,
412 xsks[i]->ring_stats.tx_empty_npkts);
414 xsks[i]->ring_stats.prev_rx_dropped_npkts =
415 xsks[i]->ring_stats.rx_dropped_npkts;
416 xsks[i]->ring_stats.prev_rx_invalid_npkts =
417 xsks[i]->ring_stats.rx_invalid_npkts;
418 xsks[i]->ring_stats.prev_tx_invalid_npkts =
419 xsks[i]->ring_stats.tx_invalid_npkts;
420 xsks[i]->ring_stats.prev_rx_full_npkts =
421 xsks[i]->ring_stats.rx_full_npkts;
422 xsks[i]->ring_stats.prev_rx_fill_empty_npkts =
423 xsks[i]->ring_stats.rx_fill_empty_npkts;
424 xsks[i]->ring_stats.prev_tx_empty_npkts =
425 xsks[i]->ring_stats.tx_empty_npkts;
427 printf("%-15s\n", "Error retrieving extra stats");
435 dump_driver_stats(dt);
438 static bool is_benchmark_done(void)
440 if (opt_duration > 0) {
441 unsigned long dt = (get_nsecs() - start_time);
443 if (dt >= opt_duration)
444 benchmark_done = true;
446 return benchmark_done;
449 static void *poller(void *arg)
452 while (!is_benchmark_done()) {
460 static void remove_xdp_program(void)
462 u32 curr_prog_id = 0;
464 if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) {
465 printf("bpf_get_link_xdp_id failed\n");
468 if (prog_id == curr_prog_id)
469 bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags);
470 else if (!curr_prog_id)
471 printf("couldn't find a prog id on a given interface\n");
473 printf("program on interface changed, not removing\n");
476 static void int_exit(int sig)
478 benchmark_done = true;
481 static void xdpsock_cleanup(void)
483 struct xsk_umem *umem = xsks[0]->umem->umem;
487 for (i = 0; i < num_socks; i++)
488 xsk_socket__delete(xsks[i]->xsk);
489 (void)xsk_umem__delete(umem);
490 remove_xdp_program();
493 static void __exit_with_error(int error, const char *file, const char *func,
496 fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
497 line, error, strerror(error));
499 remove_xdp_program();
503 #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, \
505 static void swap_mac_addresses(void *data)
507 struct ether_header *eth = (struct ether_header *)data;
508 struct ether_addr *src_addr = (struct ether_addr *)ð->ether_shost;
509 struct ether_addr *dst_addr = (struct ether_addr *)ð->ether_dhost;
510 struct ether_addr tmp;
513 *src_addr = *dst_addr;
517 static void hex_dump(void *pkt, size_t length, u64 addr)
519 const unsigned char *address = (unsigned char *)pkt;
520 const unsigned char *line = address;
521 size_t line_size = 32;
529 sprintf(buf, "addr=%llu", addr);
530 printf("length = %zu\n", length);
531 printf("%s | ", buf);
532 while (length-- > 0) {
533 printf("%02X ", *address++);
534 if (!(++i % line_size) || (length == 0 && i % line_size)) {
536 while (i++ % line_size)
539 printf(" | "); /* right close */
540 while (line < address) {
542 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
546 printf("%s | ", buf);
552 static void *memset32_htonl(void *dest, u32 val, u32 size)
554 u32 *ptr = (u32 *)dest;
559 for (i = 0; i < (size & (~0x3)); i += 4)
562 for (; i < size; i++)
563 ((char *)dest)[i] = ((char *)&val)[i & 3];
569 * This function code has been taken from
570 * Linux kernel lib/checksum.c
572 static inline unsigned short from32to16(unsigned int x)
574 /* add up 16-bit and 16-bit for 16+c bit */
575 x = (x & 0xffff) + (x >> 16);
577 x = (x & 0xffff) + (x >> 16);
582 * This function code has been taken from
583 * Linux kernel lib/checksum.c
585 static unsigned int do_csum(const unsigned char *buff, int len)
587 unsigned int result = 0;
592 odd = 1 & (unsigned long)buff;
594 #ifdef __LITTLE_ENDIAN
595 result += (*buff << 8);
603 if (2 & (unsigned long)buff) {
604 result += *(unsigned short *)buff;
609 const unsigned char *end = buff +
610 ((unsigned int)len & ~3);
611 unsigned int carry = 0;
614 unsigned int w = *(unsigned int *)buff;
619 carry = (w > result);
620 } while (buff < end);
622 result = (result & 0xffff) + (result >> 16);
625 result += *(unsigned short *)buff;
630 #ifdef __LITTLE_ENDIAN
633 result += (*buff << 8);
635 result = from32to16(result);
637 result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
642 __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
645 * This is a version of ip_compute_csum() optimized for IP headers,
646 * which always checksum on 4 octet boundaries.
647 * This function code has been taken from
648 * Linux kernel lib/checksum.c
650 __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
652 return (__force __sum16)~do_csum(iph, ihl * 4);
656 * Fold a partial checksum
657 * This function code has been taken from
658 * Linux kernel include/asm-generic/checksum.h
660 static inline __sum16 csum_fold(__wsum csum)
662 u32 sum = (__force u32)csum;
664 sum = (sum & 0xffff) + (sum >> 16);
665 sum = (sum & 0xffff) + (sum >> 16);
666 return (__force __sum16)~sum;
670 * This function code has been taken from
671 * Linux kernel lib/checksum.c
673 static inline u32 from64to32(u64 x)
675 /* add up 32-bit and 32-bit for 32+c bit */
676 x = (x & 0xffffffff) + (x >> 32);
678 x = (x & 0xffffffff) + (x >> 32);
682 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
683 __u32 len, __u8 proto, __wsum sum);
686 * This function code has been taken from
687 * Linux kernel lib/checksum.c
689 __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
690 __u32 len, __u8 proto, __wsum sum)
692 unsigned long long s = (__force u32)sum;
694 s += (__force u32)saddr;
695 s += (__force u32)daddr;
696 #ifdef __BIG_ENDIAN__
699 s += (proto + len) << 8;
701 return (__force __wsum)from64to32(s);
705 * This function has been taken from
706 * Linux kernel include/asm-generic/checksum.h
708 static inline __sum16
709 csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
710 __u8 proto, __wsum sum)
712 return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
715 static inline u16 udp_csum(u32 saddr, u32 daddr, u32 len,
716 u8 proto, u16 *udp_pkt)
721 /* udp hdr and data */
722 for (; cnt < len; cnt += 2)
723 csum += udp_pkt[cnt >> 1];
725 return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
728 #define ETH_FCS_SIZE 4
730 #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
731 sizeof(struct udphdr))
733 #define PKT_SIZE (opt_pkt_size - ETH_FCS_SIZE)
734 #define IP_PKT_SIZE (PKT_SIZE - sizeof(struct ethhdr))
735 #define UDP_PKT_SIZE (IP_PKT_SIZE - sizeof(struct iphdr))
736 #define UDP_PKT_DATA_SIZE (UDP_PKT_SIZE - sizeof(struct udphdr))
738 static u8 pkt_data[XSK_UMEM__DEFAULT_FRAME_SIZE];
740 static void gen_eth_hdr_data(void)
742 struct udphdr *udp_hdr = (struct udphdr *)(pkt_data +
743 sizeof(struct ethhdr) +
744 sizeof(struct iphdr));
745 struct iphdr *ip_hdr = (struct iphdr *)(pkt_data +
746 sizeof(struct ethhdr));
747 struct ethhdr *eth_hdr = (struct ethhdr *)pkt_data;
749 /* ethernet header */
750 memcpy(eth_hdr->h_dest, "\x3c\xfd\xfe\x9e\x7f\x71", ETH_ALEN);
751 memcpy(eth_hdr->h_source, "\xec\xb1\xd7\x98\x3a\xc0", ETH_ALEN);
752 eth_hdr->h_proto = htons(ETH_P_IP);
755 ip_hdr->version = IPVERSION;
756 ip_hdr->ihl = 0x5; /* 20 byte header */
758 ip_hdr->tot_len = htons(IP_PKT_SIZE);
760 ip_hdr->frag_off = 0;
761 ip_hdr->ttl = IPDEFTTL;
762 ip_hdr->protocol = IPPROTO_UDP;
763 ip_hdr->saddr = htonl(0x0a0a0a10);
764 ip_hdr->daddr = htonl(0x0a0a0a20);
766 /* IP header checksum */
768 ip_hdr->check = ip_fast_csum((const void *)ip_hdr, ip_hdr->ihl);
771 udp_hdr->source = htons(0x1000);
772 udp_hdr->dest = htons(0x1000);
773 udp_hdr->len = htons(UDP_PKT_SIZE);
776 memset32_htonl(pkt_data + PKT_HDR_SIZE, opt_pkt_fill_pattern,
779 /* UDP header checksum */
781 udp_hdr->check = udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE,
782 IPPROTO_UDP, (u16 *)udp_hdr);
785 static void gen_eth_frame(struct xsk_umem_info *umem, u64 addr)
787 memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data,
791 static struct xsk_umem_info *xsk_configure_umem(void *buffer, u64 size)
793 struct xsk_umem_info *umem;
794 struct xsk_umem_config cfg = {
795 /* We recommend that you set the fill ring size >= HW RX ring size +
796 * AF_XDP RX ring size. Make sure you fill up the fill ring
797 * with buffers at regular intervals, and you will with this setting
798 * avoid allocation failures in the driver. These are usually quite
799 * expensive since drivers have not been written to assume that
800 * allocation failures are common. For regular sockets, kernel
801 * allocated memory is used that only runs out in OOM situations
802 * that should be rare.
804 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
805 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
806 .frame_size = opt_xsk_frame_size,
807 .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
808 .flags = opt_umem_flags
812 umem = calloc(1, sizeof(*umem));
814 exit_with_error(errno);
816 ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq,
819 exit_with_error(-ret);
821 umem->buffer = buffer;
825 static void xsk_populate_fill_ring(struct xsk_umem_info *umem)
830 ret = xsk_ring_prod__reserve(&umem->fq,
831 XSK_RING_PROD__DEFAULT_NUM_DESCS * 2, &idx);
832 if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS * 2)
833 exit_with_error(-ret);
834 for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS * 2; i++)
835 *xsk_ring_prod__fill_addr(&umem->fq, idx++) =
836 i * opt_xsk_frame_size;
837 xsk_ring_prod__submit(&umem->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS * 2);
840 static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem,
843 struct xsk_socket_config cfg;
844 struct xsk_socket_info *xsk;
845 struct xsk_ring_cons *rxr;
846 struct xsk_ring_prod *txr;
849 xsk = calloc(1, sizeof(*xsk));
851 exit_with_error(errno);
854 cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
855 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
856 if (opt_num_xsks > 1)
857 cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
859 cfg.libbpf_flags = 0;
860 cfg.xdp_flags = opt_xdp_flags;
861 cfg.bind_flags = opt_xdp_bind_flags;
863 rxr = rx ? &xsk->rx : NULL;
864 txr = tx ? &xsk->tx : NULL;
865 ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem,
868 exit_with_error(-ret);
870 ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags);
872 exit_with_error(-ret);
874 xsk->app_stats.rx_empty_polls = 0;
875 xsk->app_stats.fill_fail_polls = 0;
876 xsk->app_stats.copy_tx_sendtos = 0;
877 xsk->app_stats.tx_wakeup_sendtos = 0;
878 xsk->app_stats.opt_polls = 0;
879 xsk->app_stats.prev_rx_empty_polls = 0;
880 xsk->app_stats.prev_fill_fail_polls = 0;
881 xsk->app_stats.prev_copy_tx_sendtos = 0;
882 xsk->app_stats.prev_tx_wakeup_sendtos = 0;
883 xsk->app_stats.prev_opt_polls = 0;
888 static struct option long_options[] = {
889 {"rxdrop", no_argument, 0, 'r'},
890 {"txonly", no_argument, 0, 't'},
891 {"l2fwd", no_argument, 0, 'l'},
892 {"interface", required_argument, 0, 'i'},
893 {"queue", required_argument, 0, 'q'},
894 {"poll", no_argument, 0, 'p'},
895 {"xdp-skb", no_argument, 0, 'S'},
896 {"xdp-native", no_argument, 0, 'N'},
897 {"interval", required_argument, 0, 'n'},
898 {"zero-copy", no_argument, 0, 'z'},
899 {"copy", no_argument, 0, 'c'},
900 {"frame-size", required_argument, 0, 'f'},
901 {"no-need-wakeup", no_argument, 0, 'm'},
902 {"unaligned", no_argument, 0, 'u'},
903 {"shared-umem", no_argument, 0, 'M'},
904 {"force", no_argument, 0, 'F'},
905 {"duration", required_argument, 0, 'd'},
906 {"batch-size", required_argument, 0, 'b'},
907 {"tx-pkt-count", required_argument, 0, 'C'},
908 {"tx-pkt-size", required_argument, 0, 's'},
909 {"tx-pkt-pattern", required_argument, 0, 'P'},
910 {"extra-stats", no_argument, 0, 'x'},
911 {"quiet", no_argument, 0, 'Q'},
912 {"app-stats", no_argument, 0, 'a'},
913 {"irq-string", no_argument, 0, 'I'},
917 static void usage(const char *prog)
920 " Usage: %s [OPTIONS]\n"
922 " -r, --rxdrop Discard all incoming packets (default)\n"
923 " -t, --txonly Only send packets\n"
924 " -l, --l2fwd MAC swap L2 forwarding\n"
925 " -i, --interface=n Run on interface n\n"
926 " -q, --queue=n Use queue n (default 0)\n"
927 " -p, --poll Use poll syscall\n"
928 " -S, --xdp-skb=n Use XDP skb-mod\n"
929 " -N, --xdp-native=n Enforce XDP native mode\n"
930 " -n, --interval=n Specify statistics update interval (default 1 sec).\n"
931 " -z, --zero-copy Force zero-copy mode.\n"
932 " -c, --copy Force copy mode.\n"
933 " -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
934 " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n"
935 " -u, --unaligned Enable unaligned chunk placement\n"
936 " -M, --shared-umem Enable XDP_SHARED_UMEM\n"
937 " -F, --force Force loading the XDP prog\n"
938 " -d, --duration=n Duration in secs to run command.\n"
939 " Default: forever.\n"
940 " -b, --batch-size=n Batch size for sending or receiving\n"
941 " packets. Default: %d\n"
942 " -C, --tx-pkt-count=n Number of packets to send.\n"
943 " Default: Continuous packets.\n"
944 " -s, --tx-pkt-size=n Transmit packet size.\n"
945 " (Default: %d bytes)\n"
946 " Min size: %d, Max size %d.\n"
947 " -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
948 " -x, --extra-stats Display extra statistics.\n"
949 " -Q, --quiet Do not display any stats.\n"
950 " -a, --app-stats Display application (syscall) statistics.\n"
951 " -I, --irq-string Display driver interrupt statistics for interface associated with irq-string.\n"
953 fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE,
954 opt_batch_size, MIN_PKT_SIZE, MIN_PKT_SIZE,
955 XSK_UMEM__DEFAULT_FRAME_SIZE, opt_pkt_fill_pattern);
960 static void parse_command_line(int argc, char **argv)
967 c = getopt_long(argc, argv, "Frtli:q:pSNn:czf:muMd:b:C:s:P:xQaI:",
968 long_options, &option_index);
974 opt_bench = BENCH_RXDROP;
977 opt_bench = BENCH_TXONLY;
980 opt_bench = BENCH_L2FWD;
986 opt_queue = atoi(optarg);
992 opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
993 opt_xdp_bind_flags |= XDP_COPY;
996 /* default, set below */
999 opt_interval = atoi(optarg);
1002 opt_xdp_bind_flags |= XDP_ZEROCOPY;
1005 opt_xdp_bind_flags |= XDP_COPY;
1008 opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
1009 opt_unaligned_chunks = 1;
1010 opt_mmap_flags = MAP_HUGETLB;
1013 opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
1016 opt_xsk_frame_size = atoi(optarg);
1019 opt_need_wakeup = false;
1020 opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
1023 opt_num_xsks = MAX_SOCKS;
1026 opt_duration = atoi(optarg);
1027 opt_duration *= 1000000000;
1030 opt_batch_size = atoi(optarg);
1033 opt_pkt_count = atoi(optarg);
1036 opt_pkt_size = atoi(optarg);
1037 if (opt_pkt_size > (XSK_UMEM__DEFAULT_FRAME_SIZE) ||
1038 opt_pkt_size < MIN_PKT_SIZE) {
1040 "ERROR: Invalid frame size %d\n",
1042 usage(basename(argv[0]));
1046 opt_pkt_fill_pattern = strtol(optarg, NULL, 16);
1049 opt_extra_stats = 1;
1058 opt_irq_str = optarg;
1059 if (get_interrupt_number())
1060 irqs_at_init = get_irqs();
1061 if (irqs_at_init < 0) {
1062 fprintf(stderr, "ERROR: Failed to get irqs for %s\n", opt_irq_str);
1063 usage(basename(argv[0]));
1068 usage(basename(argv[0]));
1072 if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
1073 opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
1075 opt_ifindex = if_nametoindex(opt_if);
1077 fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
1079 usage(basename(argv[0]));
1082 if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) &&
1083 !opt_unaligned_chunks) {
1084 fprintf(stderr, "--frame-size=%d is not a power of two\n",
1085 opt_xsk_frame_size);
1086 usage(basename(argv[0]));
1090 static void kick_tx(struct xsk_socket_info *xsk)
1094 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
1095 if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN ||
1096 errno == EBUSY || errno == ENETDOWN)
1098 exit_with_error(errno);
1101 static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk,
1104 struct xsk_umem_info *umem = xsk->umem;
1105 u32 idx_cq = 0, idx_fq = 0;
1109 if (!xsk->outstanding_tx)
1112 /* In copy mode, Tx is driven by a syscall so we need to use e.g. sendto() to
1113 * really send the packets. In zero-copy mode we do not have to do this, since Tx
1114 * is driven by the NAPI loop. So as an optimization, we do not have to call
1115 * sendto() all the time in zero-copy mode for l2fwd.
1117 if (opt_xdp_bind_flags & XDP_COPY) {
1118 xsk->app_stats.copy_tx_sendtos++;
1122 ndescs = (xsk->outstanding_tx > opt_batch_size) ? opt_batch_size :
1123 xsk->outstanding_tx;
1125 /* re-add completed Tx buffers */
1126 rcvd = xsk_ring_cons__peek(&umem->cq, ndescs, &idx_cq);
1131 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
1132 while (ret != rcvd) {
1134 exit_with_error(-ret);
1135 if (xsk_ring_prod__needs_wakeup(&umem->fq)) {
1136 xsk->app_stats.fill_fail_polls++;
1137 ret = poll(fds, num_socks, opt_timeout);
1139 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq);
1142 for (i = 0; i < rcvd; i++)
1143 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) =
1144 *xsk_ring_cons__comp_addr(&umem->cq, idx_cq++);
1146 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1147 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
1148 xsk->outstanding_tx -= rcvd;
1149 xsk->ring_stats.tx_npkts += rcvd;
1153 static inline void complete_tx_only(struct xsk_socket_info *xsk,
1159 if (!xsk->outstanding_tx)
1162 if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
1163 xsk->app_stats.tx_wakeup_sendtos++;
1167 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx);
1169 xsk_ring_cons__release(&xsk->umem->cq, rcvd);
1170 xsk->outstanding_tx -= rcvd;
1171 xsk->ring_stats.tx_npkts += rcvd;
1175 static void rx_drop(struct xsk_socket_info *xsk, struct pollfd *fds)
1177 unsigned int rcvd, i;
1178 u32 idx_rx = 0, idx_fq = 0;
1181 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
1183 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
1184 xsk->app_stats.rx_empty_polls++;
1185 ret = poll(fds, num_socks, opt_timeout);
1190 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
1191 while (ret != rcvd) {
1193 exit_with_error(-ret);
1194 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
1195 xsk->app_stats.fill_fail_polls++;
1196 ret = poll(fds, num_socks, opt_timeout);
1198 ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq);
1201 for (i = 0; i < rcvd; i++) {
1202 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1203 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
1204 u64 orig = xsk_umem__extract_addr(addr);
1206 addr = xsk_umem__add_offset_to_addr(addr);
1207 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1209 hex_dump(pkt, len, addr);
1210 *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = orig;
1213 xsk_ring_prod__submit(&xsk->umem->fq, rcvd);
1214 xsk_ring_cons__release(&xsk->rx, rcvd);
1215 xsk->ring_stats.rx_npkts += rcvd;
1218 static void rx_drop_all(void)
1220 struct pollfd fds[MAX_SOCKS] = {};
1223 for (i = 0; i < num_socks; i++) {
1224 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
1225 fds[i].events = POLLIN;
1230 for (i = 0; i < num_socks; i++)
1231 xsks[i]->app_stats.opt_polls++;
1232 ret = poll(fds, num_socks, opt_timeout);
1237 for (i = 0; i < num_socks; i++)
1238 rx_drop(xsks[i], fds);
1245 static void tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
1250 while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <
1252 complete_tx_only(xsk, batch_size);
1257 for (i = 0; i < batch_size; i++) {
1258 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx,
1260 tx_desc->addr = (*frame_nb + i) * opt_xsk_frame_size;
1261 tx_desc->len = PKT_SIZE;
1264 xsk_ring_prod__submit(&xsk->tx, batch_size);
1265 xsk->outstanding_tx += batch_size;
1266 *frame_nb += batch_size;
1267 *frame_nb %= NUM_FRAMES;
1268 complete_tx_only(xsk, batch_size);
1271 static inline int get_batch_size(int pkt_cnt)
1274 return opt_batch_size;
1276 if (pkt_cnt + opt_batch_size <= opt_pkt_count)
1277 return opt_batch_size;
1279 return opt_pkt_count - pkt_cnt;
1282 static void complete_tx_only_all(void)
1289 for (i = 0; i < num_socks; i++) {
1290 if (xsks[i]->outstanding_tx) {
1291 complete_tx_only(xsks[i], opt_batch_size);
1292 pending = !!xsks[i]->outstanding_tx;
1298 static void tx_only_all(void)
1300 struct pollfd fds[MAX_SOCKS] = {};
1301 u32 frame_nb[MAX_SOCKS] = {};
1305 for (i = 0; i < num_socks; i++) {
1306 fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
1307 fds[0].events = POLLOUT;
1310 while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
1311 int batch_size = get_batch_size(pkt_cnt);
1314 for (i = 0; i < num_socks; i++)
1315 xsks[i]->app_stats.opt_polls++;
1316 ret = poll(fds, num_socks, opt_timeout);
1320 if (!(fds[0].revents & POLLOUT))
1324 for (i = 0; i < num_socks; i++)
1325 tx_only(xsks[i], &frame_nb[i], batch_size);
1327 pkt_cnt += batch_size;
1334 complete_tx_only_all();
1337 static void l2fwd(struct xsk_socket_info *xsk, struct pollfd *fds)
1339 unsigned int rcvd, i;
1340 u32 idx_rx = 0, idx_tx = 0;
1343 complete_tx_l2fwd(xsk, fds);
1345 rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
1347 if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) {
1348 xsk->app_stats.rx_empty_polls++;
1349 ret = poll(fds, num_socks, opt_timeout);
1354 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
1355 while (ret != rcvd) {
1357 exit_with_error(-ret);
1358 complete_tx_l2fwd(xsk, fds);
1359 if (xsk_ring_prod__needs_wakeup(&xsk->tx)) {
1360 xsk->app_stats.tx_wakeup_sendtos++;
1363 ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx);
1366 for (i = 0; i < rcvd; i++) {
1367 u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
1368 u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
1371 addr = xsk_umem__add_offset_to_addr(addr);
1372 char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr);
1374 swap_mac_addresses(pkt);
1376 hex_dump(pkt, len, addr);
1377 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = orig;
1378 xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len;
1381 xsk_ring_prod__submit(&xsk->tx, rcvd);
1382 xsk_ring_cons__release(&xsk->rx, rcvd);
1384 xsk->ring_stats.rx_npkts += rcvd;
1385 xsk->outstanding_tx += rcvd;
1388 static void l2fwd_all(void)
1390 struct pollfd fds[MAX_SOCKS] = {};
1393 for (i = 0; i < num_socks; i++) {
1394 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
1395 fds[i].events = POLLOUT | POLLIN;
1400 for (i = 0; i < num_socks; i++)
1401 xsks[i]->app_stats.opt_polls++;
1402 ret = poll(fds, num_socks, opt_timeout);
1407 for (i = 0; i < num_socks; i++)
1408 l2fwd(xsks[i], fds);
1415 static void load_xdp_program(char **argv, struct bpf_object **obj)
1417 struct bpf_prog_load_attr prog_load_attr = {
1418 .prog_type = BPF_PROG_TYPE_XDP,
1420 char xdp_filename[256];
1423 snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
1424 prog_load_attr.file = xdp_filename;
1426 if (bpf_prog_load_xattr(&prog_load_attr, obj, &prog_fd))
1429 fprintf(stderr, "ERROR: no program found: %s\n",
1434 if (bpf_set_link_xdp_fd(opt_ifindex, prog_fd, opt_xdp_flags) < 0) {
1435 fprintf(stderr, "ERROR: link set xdp fd failed\n");
1440 static void enter_xsks_into_map(struct bpf_object *obj)
1442 struct bpf_map *map;
1445 map = bpf_object__find_map_by_name(obj, "xsks_map");
1446 xsks_map = bpf_map__fd(map);
1448 fprintf(stderr, "ERROR: no xsks map found: %s\n",
1449 strerror(xsks_map));
1453 for (i = 0; i < num_socks; i++) {
1454 int fd = xsk_socket__fd(xsks[i]->xsk);
1458 ret = bpf_map_update_elem(xsks_map, &key, &fd, 0);
1460 fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
1466 int main(int argc, char **argv)
1468 struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
1469 bool rx = false, tx = false;
1470 struct xsk_umem_info *umem;
1471 struct bpf_object *obj;
1476 parse_command_line(argc, argv);
1478 if (setrlimit(RLIMIT_MEMLOCK, &r)) {
1479 fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n",
1484 if (opt_num_xsks > 1)
1485 load_xdp_program(argv, &obj);
1487 /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
1488 bufs = mmap(NULL, NUM_FRAMES * opt_xsk_frame_size,
1489 PROT_READ | PROT_WRITE,
1490 MAP_PRIVATE | MAP_ANONYMOUS | opt_mmap_flags, -1, 0);
1491 if (bufs == MAP_FAILED) {
1492 printf("ERROR: mmap failed\n");
1496 /* Create sockets... */
1497 umem = xsk_configure_umem(bufs, NUM_FRAMES * opt_xsk_frame_size);
1498 if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD) {
1500 xsk_populate_fill_ring(umem);
1502 if (opt_bench == BENCH_L2FWD || opt_bench == BENCH_TXONLY)
1504 for (i = 0; i < opt_num_xsks; i++)
1505 xsks[num_socks++] = xsk_configure_socket(umem, rx, tx);
1507 if (opt_bench == BENCH_TXONLY) {
1510 for (i = 0; i < NUM_FRAMES; i++)
1511 gen_eth_frame(umem, i * opt_xsk_frame_size);
1514 if (opt_num_xsks > 1 && opt_bench != BENCH_TXONLY)
1515 enter_xsks_into_map(obj);
1517 signal(SIGINT, int_exit);
1518 signal(SIGTERM, int_exit);
1519 signal(SIGABRT, int_exit);
1521 setlocale(LC_ALL, "");
1523 prev_time = get_nsecs();
1524 start_time = prev_time;
1527 ret = pthread_create(&pt, NULL, poller, NULL);
1529 exit_with_error(ret);
1533 if (opt_bench == BENCH_RXDROP)
1535 else if (opt_bench == BENCH_TXONLY)
1540 benchmark_done = true;
1543 pthread_join(pt, NULL);
1547 munmap(bufs, NUM_FRAMES * opt_xsk_frame_size);