Advertisement
Guest User

xdpsock_2_ifaces.c

a guest
Aug 4th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 44.64 KB | None | 0 0
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2017 - 2018 Intel Corporation. */
  3.  
  4. #include <asm/barrier.h>
  5. #include <errno.h>
  6. #include <getopt.h>
  7. #include <libgen.h>
  8. #include <linux/bpf.h>
  9. #include <linux/compiler.h>
  10. #include <linux/if_link.h>
  11. #include <linux/if_xdp.h>
  12. #include <linux/if_ether.h>
  13. #include <linux/ip.h>
  14. #include <linux/limits.h>
  15. #include <linux/udp.h>
  16. #include <arpa/inet.h>
  17. #include <locale.h>
  18. #include <net/ethernet.h>
  19. #include <net/if.h>
  20. #include <poll.h>
  21. #include <pthread.h>
  22. #include <signal.h>
  23. #include <stdbool.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <sys/capability.h>
  28. #include <sys/mman.h>
  29. #include <sys/resource.h>
  30. #include <sys/socket.h>
  31. #include <sys/types.h>
  32. #include <sys/un.h>
  33. #include <time.h>
  34. #include <unistd.h>
  35.  
  36. #include <bpf/libbpf.h>
  37. #include <bpf/xsk.h>
  38. #include <bpf/bpf.h>
  39. #include "xdpsock.h"
  40.  
  41. #ifndef SOL_XDP
  42. #define SOL_XDP 283
  43. #endif
  44.  
  45. #ifndef AF_XDP
  46. #define AF_XDP 44
  47. #endif
  48.  
  49. #ifndef PF_XDP
  50. #define PF_XDP AF_XDP
  51. #endif
  52.  
  53. #define NUM_FRAMES (4 * 1024)
  54. #define MIN_PKT_SIZE 64
  55.  
  56. #define DEBUG_HEXDUMP 0
  57.  
  58. typedef __u64 u64;
  59. typedef __u32 u32;
  60. typedef __u16 u16;
  61. typedef __u8  u8;
  62.  
  63. static unsigned long prev_time;
  64.  
  65. enum benchmark_type {
  66.     BENCH_RXDROP = 0,
  67.     BENCH_TXONLY = 1,
  68.     BENCH_L2FWD = 2,
  69. };
  70.  
  71. static enum benchmark_type opt_bench = BENCH_RXDROP;
  72. static u32 opt_xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST;
  73. static int opt_num_ports = 0;
  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 = "";
  85. static u32 irq_no;
  86. static int irqs_at_init = -1;
  87. static int opt_poll;
  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;
  97. static bool opt_busy_poll;
  98. static bool opt_reduced_cap;
  99.  
  100. struct xsk_ring_stats {
  101.     unsigned long rx_npkts;
  102.     unsigned long tx_npkts;
  103.     unsigned long rx_dropped_npkts;
  104.     unsigned long rx_invalid_npkts;
  105.     unsigned long tx_invalid_npkts;
  106.     unsigned long rx_full_npkts;
  107.     unsigned long rx_fill_empty_npkts;
  108.     unsigned long tx_empty_npkts;
  109.     unsigned long prev_rx_npkts;
  110.     unsigned long prev_tx_npkts;
  111.     unsigned long prev_rx_dropped_npkts;
  112.     unsigned long prev_rx_invalid_npkts;
  113.     unsigned long prev_tx_invalid_npkts;
  114.     unsigned long prev_rx_full_npkts;
  115.     unsigned long prev_rx_fill_empty_npkts;
  116.     unsigned long prev_tx_empty_npkts;
  117. };
  118.  
  119. struct xsk_driver_stats {
  120.     unsigned long intrs;
  121.     unsigned long prev_intrs;
  122. };
  123.  
  124. struct xsk_app_stats {
  125.     unsigned long rx_empty_polls;
  126.     unsigned long fill_fail_polls;
  127.     unsigned long copy_tx_sendtos;
  128.     unsigned long tx_wakeup_sendtos;
  129.     unsigned long opt_polls;
  130.     unsigned long prev_rx_empty_polls;
  131.     unsigned long prev_fill_fail_polls;
  132.     unsigned long prev_copy_tx_sendtos;
  133.     unsigned long prev_tx_wakeup_sendtos;
  134.     unsigned long prev_opt_polls;
  135. };
  136.  
  137. struct xsk_umem_info {
  138.     struct xsk_umem *umem;
  139.     void *buffer;
  140. };
  141.  
  142. /*
  143.  * A port is an (interface, queue) pair, each ports needs its own fill and
  144.  * completion rings
  145.  */
  146. struct xsk_port_info {
  147.     char *ifname;
  148.     int ifindex;
  149.     int queue;
  150.     struct xsk_umem_info *umem;
  151.     struct xsk_ring_prod fq;
  152.     struct xsk_ring_cons cq;
  153. };
  154.  
  155. struct xsk_socket_info {
  156.     struct xsk_ring_cons rx;
  157.     struct xsk_ring_prod tx;
  158.     struct xsk_port_info *port;
  159.     struct xsk_socket *xsk;
  160.     struct xsk_ring_stats ring_stats;
  161.     struct xsk_app_stats app_stats;
  162.     struct xsk_driver_stats drv_stats;
  163.     u32 outstanding_tx;
  164. };
  165.  
  166. static int num_socks;
  167. struct xsk_port_info ports[MAX_PORTS];
  168. struct xsk_socket_info *xsks[MAX_SOCKS];
  169. struct xsk_umem_info umem;
  170. int sock;
  171.  
  172. static unsigned long get_nsecs(void)
  173. {
  174.     struct timespec ts;
  175.  
  176.     clock_gettime(CLOCK_MONOTONIC, &ts);
  177.     return ts.tv_sec * 1000000000UL + ts.tv_nsec;
  178. }
  179.  
  180. static void print_benchmark(bool running)
  181. {
  182.     const char *bench_str = "INVALID";
  183.  
  184.     if (opt_bench == BENCH_RXDROP)
  185.         bench_str = "rxdrop";
  186.     else if (opt_bench == BENCH_TXONLY)
  187.         bench_str = "txonly";
  188.     else if (opt_bench == BENCH_L2FWD)
  189.         bench_str = "l2fwd";
  190.  
  191.     printf("%s ", bench_str);
  192.     if (opt_xdp_flags & XDP_FLAGS_SKB_MODE)
  193.         printf("xdp-skb ");
  194.     else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE)
  195.         printf("xdp-drv ");
  196.     else
  197.         printf("    ");
  198.  
  199.     if (opt_poll)
  200.         printf("poll() ");
  201.  
  202.     if (running) {
  203.         printf("running...");
  204.         fflush(stdout);
  205.     }
  206. }
  207.  
  208. static int xsk_get_xdp_stats(int fd, struct xsk_socket_info *xsk)
  209. {
  210.     struct xdp_statistics stats;
  211.     socklen_t optlen;
  212.     int err;
  213.  
  214.     optlen = sizeof(stats);
  215.     err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen);
  216.     if (err)
  217.         return err;
  218.  
  219.     if (optlen == sizeof(struct xdp_statistics)) {
  220.         xsk->ring_stats.rx_dropped_npkts = stats.rx_dropped;
  221.         xsk->ring_stats.rx_invalid_npkts = stats.rx_invalid_descs;
  222.         xsk->ring_stats.tx_invalid_npkts = stats.tx_invalid_descs;
  223.         xsk->ring_stats.rx_full_npkts = stats.rx_ring_full;
  224.         xsk->ring_stats.rx_fill_empty_npkts = stats.rx_fill_ring_empty_descs;
  225.         xsk->ring_stats.tx_empty_npkts = stats.tx_ring_empty_descs;
  226.         return 0;
  227.     }
  228.  
  229.     return -EINVAL;
  230. }
  231.  
  232. static void dump_app_stats(struct xsk_socket_info *xsk, long dt)
  233. {
  234.     char *fmt = "%-18s %'-14.0f %'-14lu\n";
  235.     double rx_empty_polls_ps, fill_fail_polls_ps, copy_tx_sendtos_ps,
  236.             tx_wakeup_sendtos_ps, opt_polls_ps;
  237.  
  238.     rx_empty_polls_ps = (xsk->app_stats.rx_empty_polls -
  239.                 xsk->app_stats.prev_rx_empty_polls) * 1000000000. / dt;
  240.     fill_fail_polls_ps = (xsk->app_stats.fill_fail_polls -
  241.                 xsk->app_stats.prev_fill_fail_polls) * 1000000000. / dt;
  242.     copy_tx_sendtos_ps = (xsk->app_stats.copy_tx_sendtos -
  243.                 xsk->app_stats.prev_copy_tx_sendtos) * 1000000000. / dt;
  244.     tx_wakeup_sendtos_ps = (xsk->app_stats.tx_wakeup_sendtos -
  245.                 xsk->app_stats.prev_tx_wakeup_sendtos)
  246.                                     * 1000000000. / dt;
  247.     opt_polls_ps = (xsk->app_stats.opt_polls -
  248.                 xsk->app_stats.prev_opt_polls) * 1000000000. / dt;
  249.  
  250.     printf("%-18s %-14s %-14s\n", "", "calls/s", "count");
  251.     printf(fmt, "rx empty polls", rx_empty_polls_ps, xsk->app_stats.rx_empty_polls);
  252.     printf(fmt, "fill fail polls", fill_fail_polls_ps,
  253.                         xsk->app_stats.fill_fail_polls);
  254.     printf(fmt, "copy tx sendtos", copy_tx_sendtos_ps,
  255.                         xsk->app_stats.copy_tx_sendtos);
  256.     printf(fmt, "tx wakeup sendtos", tx_wakeup_sendtos_ps,
  257.                         xsk->app_stats.tx_wakeup_sendtos);
  258.     printf(fmt, "opt polls", opt_polls_ps, xsk->app_stats.opt_polls);
  259.  
  260.     xsk->app_stats.prev_rx_empty_polls = xsk->app_stats.rx_empty_polls;
  261.     xsk->app_stats.prev_fill_fail_polls = xsk->app_stats.fill_fail_polls;
  262.     xsk->app_stats.prev_copy_tx_sendtos = xsk->app_stats.copy_tx_sendtos;
  263.     xsk->app_stats.prev_tx_wakeup_sendtos = xsk->app_stats.tx_wakeup_sendtos;
  264.     xsk->app_stats.prev_opt_polls = xsk->app_stats.opt_polls;
  265. }
  266.  
  267. static bool get_interrupt_number(void)
  268. {
  269.     FILE *f_int_proc;
  270.     char line[4096];
  271.     bool found = false;
  272.  
  273.     f_int_proc = fopen("/proc/interrupts", "r");
  274.     if (f_int_proc == NULL) {
  275.         printf("Failed to open /proc/interrupts.\n");
  276.         return found;
  277.     }
  278.  
  279.     while (!feof(f_int_proc) && !found) {
  280.         /* Make sure to read a full line at a time */
  281.         if (fgets(line, sizeof(line), f_int_proc) == NULL ||
  282.                 line[strlen(line) - 1] != '\n') {
  283.             printf("Error reading from interrupts file\n");
  284.             break;
  285.         }
  286.  
  287.         /* Extract interrupt number from line */
  288.         if (strstr(line, opt_irq_str) != NULL) {
  289.             irq_no = atoi(line);
  290.             found = true;
  291.             break;
  292.         }
  293.     }
  294.  
  295.     fclose(f_int_proc);
  296.  
  297.     return found;
  298. }
  299.  
  300. static int get_irqs(void)
  301. {
  302.     char count_path[PATH_MAX];
  303.     int total_intrs = -1;
  304.     FILE *f_count_proc;
  305.     char line[4096];
  306.  
  307.     snprintf(count_path, sizeof(count_path),
  308.         "/sys/kernel/irq/%i/per_cpu_count", irq_no);
  309.     f_count_proc = fopen(count_path, "r");
  310.     if (f_count_proc == NULL) {
  311.         printf("Failed to open %s\n", count_path);
  312.         return total_intrs;
  313.     }
  314.  
  315.     if (fgets(line, sizeof(line), f_count_proc) == NULL ||
  316.             line[strlen(line) - 1] != '\n') {
  317.         printf("Error reading from %s\n", count_path);
  318.     } else {
  319.         static const char com[2] = ",";
  320.         char *token;
  321.  
  322.         total_intrs = 0;
  323.         token = strtok(line, com);
  324.         while (token != NULL) {
  325.             /* sum up interrupts across all cores */
  326.             total_intrs += atoi(token);
  327.             token = strtok(NULL, com);
  328.         }
  329.     }
  330.  
  331.     fclose(f_count_proc);
  332.  
  333.     return total_intrs;
  334. }
  335.  
  336. static void dump_driver_stats(long dt)
  337. {
  338.     int i;
  339.  
  340.     for (i = 0; i < num_socks && xsks[i]; i++) {
  341.         char *fmt = "%-18s %'-14.0f %'-14lu\n";
  342.         double intrs_ps;
  343.         int n_ints = get_irqs();
  344.  
  345.         if (n_ints < 0) {
  346.             printf("error getting intr info for intr %i\n", irq_no);
  347.             return;
  348.         }
  349.         xsks[i]->drv_stats.intrs = n_ints - irqs_at_init;
  350.  
  351.         intrs_ps = (xsks[i]->drv_stats.intrs - xsks[i]->drv_stats.prev_intrs) *
  352.              1000000000. / dt;
  353.  
  354.         printf("\n%-18s %-14s %-14s\n", "", "intrs/s", "count");
  355.         printf(fmt, "irqs", intrs_ps, xsks[i]->drv_stats.intrs);
  356.  
  357.         xsks[i]->drv_stats.prev_intrs = xsks[i]->drv_stats.intrs;
  358.     }
  359. }
  360.  
  361. static void dump_stats(void)
  362. {
  363.     unsigned long now = get_nsecs();
  364.     long dt = now - prev_time;
  365.     int i;
  366.  
  367.     prev_time = now;
  368.  
  369.     for (i = 0; i < num_socks && xsks[i]; i++) {
  370.         char *fmt = "%-18s %'-14.0f %'-14lu\n";
  371.         double rx_pps, tx_pps, dropped_pps, rx_invalid_pps, full_pps, fill_empty_pps,
  372.             tx_invalid_pps, tx_empty_pps;
  373.  
  374.         rx_pps = (xsks[i]->ring_stats.rx_npkts - xsks[i]->ring_stats.prev_rx_npkts) *
  375.              1000000000. / dt;
  376.         tx_pps = (xsks[i]->ring_stats.tx_npkts - xsks[i]->ring_stats.prev_tx_npkts) *
  377.              1000000000. / dt;
  378.  
  379.         printf("\n sock%d@%s:%d ", i, ports[i % opt_num_ports].ifname, ports[i % opt_num_ports].queue);
  380.         print_benchmark(false);
  381.         printf("\n");
  382.  
  383.         printf("%-18s %-14s %-14s %-14.2f\n", "", "pps", "pkts",
  384.                dt / 1000000000.);
  385.         printf(fmt, "rx", rx_pps, xsks[i]->ring_stats.rx_npkts);
  386.         printf(fmt, "tx", tx_pps, xsks[i]->ring_stats.tx_npkts);
  387.  
  388.         xsks[i]->ring_stats.prev_rx_npkts = xsks[i]->ring_stats.rx_npkts;
  389.         xsks[i]->ring_stats.prev_tx_npkts = xsks[i]->ring_stats.tx_npkts;
  390.  
  391.         if (opt_extra_stats) {
  392.             if (!xsk_get_xdp_stats(xsk_socket__fd(xsks[i]->xsk), xsks[i])) {
  393.                 dropped_pps = (xsks[i]->ring_stats.rx_dropped_npkts -
  394.                         xsks[i]->ring_stats.prev_rx_dropped_npkts) *
  395.                             1000000000. / dt;
  396.                 rx_invalid_pps = (xsks[i]->ring_stats.rx_invalid_npkts -
  397.                         xsks[i]->ring_stats.prev_rx_invalid_npkts) *
  398.                             1000000000. / dt;
  399.                 tx_invalid_pps = (xsks[i]->ring_stats.tx_invalid_npkts -
  400.                         xsks[i]->ring_stats.prev_tx_invalid_npkts) *
  401.                             1000000000. / dt;
  402.                 full_pps = (xsks[i]->ring_stats.rx_full_npkts -
  403.                         xsks[i]->ring_stats.prev_rx_full_npkts) *
  404.                             1000000000. / dt;
  405.                 fill_empty_pps = (xsks[i]->ring_stats.rx_fill_empty_npkts -
  406.                         xsks[i]->ring_stats.prev_rx_fill_empty_npkts) *
  407.                             1000000000. / dt;
  408.                 tx_empty_pps = (xsks[i]->ring_stats.tx_empty_npkts -
  409.                         xsks[i]->ring_stats.prev_tx_empty_npkts) *
  410.                             1000000000. / dt;
  411.  
  412.                 printf(fmt, "rx dropped", dropped_pps,
  413.                        xsks[i]->ring_stats.rx_dropped_npkts);
  414.                 printf(fmt, "rx invalid", rx_invalid_pps,
  415.                        xsks[i]->ring_stats.rx_invalid_npkts);
  416.                 printf(fmt, "tx invalid", tx_invalid_pps,
  417.                        xsks[i]->ring_stats.tx_invalid_npkts);
  418.                 printf(fmt, "rx queue full", full_pps,
  419.                        xsks[i]->ring_stats.rx_full_npkts);
  420.                 printf(fmt, "fill ring empty", fill_empty_pps,
  421.                        xsks[i]->ring_stats.rx_fill_empty_npkts);
  422.                 printf(fmt, "tx ring empty", tx_empty_pps,
  423.                        xsks[i]->ring_stats.tx_empty_npkts);
  424.  
  425.                 xsks[i]->ring_stats.prev_rx_dropped_npkts =
  426.                     xsks[i]->ring_stats.rx_dropped_npkts;
  427.                 xsks[i]->ring_stats.prev_rx_invalid_npkts =
  428.                     xsks[i]->ring_stats.rx_invalid_npkts;
  429.                 xsks[i]->ring_stats.prev_tx_invalid_npkts =
  430.                     xsks[i]->ring_stats.tx_invalid_npkts;
  431.                 xsks[i]->ring_stats.prev_rx_full_npkts =
  432.                     xsks[i]->ring_stats.rx_full_npkts;
  433.                 xsks[i]->ring_stats.prev_rx_fill_empty_npkts =
  434.                     xsks[i]->ring_stats.rx_fill_empty_npkts;
  435.                 xsks[i]->ring_stats.prev_tx_empty_npkts =
  436.                     xsks[i]->ring_stats.tx_empty_npkts;
  437.             } else {
  438.                 printf("%-15s\n", "Error retrieving extra stats");
  439.             }
  440.         }
  441.  
  442.         if (opt_app_stats)
  443.             dump_app_stats(xsks[i], dt);
  444.     }
  445.  
  446.     if (irq_no)
  447.         dump_driver_stats(dt);
  448.  
  449.     printf("\n");
  450. }
  451.  
  452. static bool is_benchmark_done(void)
  453. {
  454.     if (opt_duration > 0) {
  455.         unsigned long dt = (get_nsecs() - start_time);
  456.  
  457.         if (dt >= opt_duration)
  458.             benchmark_done = true;
  459.     }
  460.     return benchmark_done;
  461. }
  462.  
  463. static void *poller(void *arg)
  464. {
  465.     (void)arg;
  466.     while (!is_benchmark_done()) {
  467.         sleep(opt_interval);
  468.         dump_stats();
  469.     }
  470.  
  471.     return NULL;
  472. }
  473.  
  474. static void int_exit(int sig)
  475. {
  476.     benchmark_done = true;
  477. }
  478.  
  479. static void __exit_with_error(int error, const char *file, const char *func,
  480.                   int line)
  481. {
  482.     fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func,
  483.         line, error, strerror(error));
  484.     exit(EXIT_FAILURE);
  485. }
  486.  
  487. #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__)
  488.  
  489. static void xdpsock_cleanup(void)
  490. {
  491.     int i, cmd = CLOSE_CONN;
  492.  
  493.     dump_stats();
  494.     for (i = 0; i < num_socks; i++)
  495.         xsk_socket__delete(xsks[i]->xsk);
  496.     (void)xsk_umem__delete(umem.umem);
  497.  
  498.     if (opt_reduced_cap) {
  499.         if (write(sock, &cmd, sizeof(int)) < 0)
  500.             exit_with_error(errno);
  501.     }
  502. }
  503.  
  504. static void swap_mac_addresses(void *data)
  505. {
  506.     struct ether_header *eth = (struct ether_header *)data;
  507.     struct ether_addr *src_addr = (struct ether_addr *)&eth->ether_shost;
  508.     struct ether_addr *dst_addr = (struct ether_addr *)&eth->ether_dhost;
  509.     struct ether_addr tmp;
  510.  
  511.     tmp = *src_addr;
  512.     *src_addr = *dst_addr;
  513.     *dst_addr = tmp;
  514. }
  515.  
  516. static void hex_dump(void *pkt, size_t length, u64 addr)
  517. {
  518.     const unsigned char *address = (unsigned char *)pkt;
  519.     const unsigned char *line = address;
  520.     size_t line_size = 32;
  521.     unsigned char c;
  522.     char buf[32];
  523.     int i = 0;
  524.  
  525.     if (!DEBUG_HEXDUMP)
  526.         return;
  527.  
  528.     sprintf(buf, "addr=%llu", addr);
  529.     printf("length = %zu\n", length);
  530.     printf("%s | ", buf);
  531.     while (length-- > 0) {
  532.         printf("%02X ", *address++);
  533.         if (!(++i % line_size) || (length == 0 && i % line_size)) {
  534.             if (length == 0) {
  535.                 while (i++ % line_size)
  536.                     printf("__ ");
  537.             }
  538.             printf(" | ");  /* right close */
  539.             while (line < address) {
  540.                 c = *line++;
  541.                 printf("%c", (c < 33 || c == 255) ? 0x2E : c);
  542.             }
  543.             printf("\n");
  544.             if (length > 0)
  545.                 printf("%s | ", buf);
  546.         }
  547.     }
  548.     printf("\n");
  549. }
  550.  
  551. static void *memset32_htonl(void *dest, u32 val, u32 size)
  552. {
  553.     u32 *ptr = (u32 *)dest;
  554.     int i;
  555.  
  556.     val = htonl(val);
  557.  
  558.     for (i = 0; i < (size & (~0x3)); i += 4)
  559.         ptr[i >> 2] = val;
  560.  
  561.     for (; i < size; i++)
  562.         ((char *)dest)[i] = ((char *)&val)[i & 3];
  563.  
  564.     return dest;
  565. }
  566.  
  567. /*
  568.  * This function code has been taken from
  569.  * Linux kernel lib/checksum.c
  570.  */
  571. static inline unsigned short from32to16(unsigned int x)
  572. {
  573.     /* add up 16-bit and 16-bit for 16+c bit */
  574.     x = (x & 0xffff) + (x >> 16);
  575.     /* add up carry.. */
  576.     x = (x & 0xffff) + (x >> 16);
  577.     return x;
  578. }
  579.  
  580. /*
  581.  * This function code has been taken from
  582.  * Linux kernel lib/checksum.c
  583.  */
  584. static unsigned int do_csum(const unsigned char *buff, int len)
  585. {
  586.     unsigned int result = 0;
  587.     int odd;
  588.  
  589.     if (len <= 0)
  590.         goto out;
  591.     odd = 1 & (unsigned long)buff;
  592.     if (odd) {
  593. #ifdef __LITTLE_ENDIAN
  594.         result += (*buff << 8);
  595. #else
  596.         result = *buff;
  597. #endif
  598.         len--;
  599.         buff++;
  600.     }
  601.     if (len >= 2) {
  602.         if (2 & (unsigned long)buff) {
  603.             result += *(unsigned short *)buff;
  604.             len -= 2;
  605.             buff += 2;
  606.         }
  607.         if (len >= 4) {
  608.             const unsigned char *end = buff +
  609.                            ((unsigned int)len & ~3);
  610.             unsigned int carry = 0;
  611.  
  612.             do {
  613.                 unsigned int w = *(unsigned int *)buff;
  614.  
  615.                 buff += 4;
  616.                 result += carry;
  617.                 result += w;
  618.                 carry = (w > result);
  619.             } while (buff < end);
  620.             result += carry;
  621.             result = (result & 0xffff) + (result >> 16);
  622.         }
  623.         if (len & 2) {
  624.             result += *(unsigned short *)buff;
  625.             buff += 2;
  626.         }
  627.     }
  628.     if (len & 1)
  629. #ifdef __LITTLE_ENDIAN
  630.         result += *buff;
  631. #else
  632.         result += (*buff << 8);
  633. #endif
  634.     result = from32to16(result);
  635.     if (odd)
  636.         result = ((result >> 8) & 0xff) | ((result & 0xff) << 8);
  637. out:
  638.     return result;
  639. }
  640.  
  641. __sum16 ip_fast_csum(const void *iph, unsigned int ihl);
  642.  
  643. /*
  644.  *  This is a version of ip_compute_csum() optimized for IP headers,
  645.  *  which always checksum on 4 octet boundaries.
  646.  *  This function code has been taken from
  647.  *  Linux kernel lib/checksum.c
  648.  */
  649. __sum16 ip_fast_csum(const void *iph, unsigned int ihl)
  650. {
  651.     return (__force __sum16)~do_csum(iph, ihl * 4);
  652. }
  653.  
  654. /*
  655.  * Fold a partial checksum
  656.  * This function code has been taken from
  657.  * Linux kernel include/asm-generic/checksum.h
  658.  */
  659. static inline __sum16 csum_fold(__wsum csum)
  660. {
  661.     u32 sum = (__force u32)csum;
  662.  
  663.     sum = (sum & 0xffff) + (sum >> 16);
  664.     sum = (sum & 0xffff) + (sum >> 16);
  665.     return (__force __sum16)~sum;
  666. }
  667.  
  668. /*
  669.  * This function code has been taken from
  670.  * Linux kernel lib/checksum.c
  671.  */
  672. static inline u32 from64to32(u64 x)
  673. {
  674.     /* add up 32-bit and 32-bit for 32+c bit */
  675.     x = (x & 0xffffffff) + (x >> 32);
  676.     /* add up carry.. */
  677.     x = (x & 0xffffffff) + (x >> 32);
  678.     return (u32)x;
  679. }
  680.  
  681. __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  682.               __u32 len, __u8 proto, __wsum sum);
  683.  
  684. /*
  685.  * This function code has been taken from
  686.  * Linux kernel lib/checksum.c
  687.  */
  688. __wsum csum_tcpudp_nofold(__be32 saddr, __be32 daddr,
  689.               __u32 len, __u8 proto, __wsum sum)
  690. {
  691.     unsigned long long s = (__force u32)sum;
  692.  
  693.     s += (__force u32)saddr;
  694.     s += (__force u32)daddr;
  695. #ifdef __BIG_ENDIAN__
  696.     s += proto + len;
  697. #else
  698.     s += (proto + len) << 8;
  699. #endif
  700.     return (__force __wsum)from64to32(s);
  701. }
  702.  
  703. /*
  704.  * This function has been taken from
  705.  * Linux kernel include/asm-generic/checksum.h
  706.  */
  707. static inline __sum16
  708. csum_tcpudp_magic(__be32 saddr, __be32 daddr, __u32 len,
  709.           __u8 proto, __wsum sum)
  710. {
  711.     return csum_fold(csum_tcpudp_nofold(saddr, daddr, len, proto, sum));
  712. }
  713.  
  714. static inline u16 udp_csum(u32 saddr, u32 daddr, u32 len,
  715.                u8 proto, u16 *udp_pkt)
  716. {
  717.     u32 csum = 0;
  718.     u32 cnt = 0;
  719.  
  720.     /* udp hdr and data */
  721.     for (; cnt < len; cnt += 2)
  722.         csum += udp_pkt[cnt >> 1];
  723.  
  724.     return csum_tcpudp_magic(saddr, daddr, len, proto, csum);
  725. }
  726.  
  727. #define ETH_FCS_SIZE 4
  728.  
  729. #define PKT_HDR_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \
  730.               sizeof(struct udphdr))
  731.  
  732. #define PKT_SIZE        (opt_pkt_size - ETH_FCS_SIZE)
  733. #define IP_PKT_SIZE     (PKT_SIZE - sizeof(struct ethhdr))
  734. #define UDP_PKT_SIZE        (IP_PKT_SIZE - sizeof(struct iphdr))
  735. #define UDP_PKT_DATA_SIZE   (UDP_PKT_SIZE - sizeof(struct udphdr))
  736.  
  737. static u8 pkt_data[XSK_UMEM__DEFAULT_FRAME_SIZE];
  738.  
  739. static void gen_eth_hdr_data(void)
  740. {
  741.     struct udphdr *udp_hdr = (struct udphdr *)(pkt_data +
  742.                            sizeof(struct ethhdr) +
  743.                            sizeof(struct iphdr));
  744.     struct iphdr *ip_hdr = (struct iphdr *)(pkt_data +
  745.                         sizeof(struct ethhdr));
  746.     struct ethhdr *eth_hdr = (struct ethhdr *)pkt_data;
  747.  
  748.     /* ethernet header */
  749.     memcpy(eth_hdr->h_dest, "\x3c\xfd\xfe\x9e\x7f\x71", ETH_ALEN);
  750.     memcpy(eth_hdr->h_source, "\xec\xb1\xd7\x98\x3a\xc0", ETH_ALEN);
  751.     eth_hdr->h_proto = htons(ETH_P_IP);
  752.  
  753.     /* IP header */
  754.     ip_hdr->version = IPVERSION;
  755.     ip_hdr->ihl = 0x5; /* 20 byte header */
  756.     ip_hdr->tos = 0x0;
  757.     ip_hdr->tot_len = htons(IP_PKT_SIZE);
  758.     ip_hdr->id = 0;
  759.     ip_hdr->frag_off = 0;
  760.     ip_hdr->ttl = IPDEFTTL;
  761.     ip_hdr->protocol = IPPROTO_UDP;
  762.     ip_hdr->saddr = htonl(0x0a0a0a10);
  763.     ip_hdr->daddr = htonl(0x0a0a0a20);
  764.  
  765.     /* IP header checksum */
  766.     ip_hdr->check = 0;
  767.     ip_hdr->check = ip_fast_csum((const void *)ip_hdr, ip_hdr->ihl);
  768.  
  769.     /* UDP header */
  770.     udp_hdr->source = htons(0x1000);
  771.     udp_hdr->dest = htons(0x1000);
  772.     udp_hdr->len = htons(UDP_PKT_SIZE);
  773.  
  774.     /* UDP data */
  775.     memset32_htonl(pkt_data + PKT_HDR_SIZE, opt_pkt_fill_pattern,
  776.                UDP_PKT_DATA_SIZE);
  777.  
  778.     /* UDP header checksum */
  779.     udp_hdr->check = 0;
  780.     udp_hdr->check = udp_csum(ip_hdr->saddr, ip_hdr->daddr, UDP_PKT_SIZE,
  781.                   IPPROTO_UDP, (u16 *)udp_hdr);
  782. }
  783.  
  784. static void gen_eth_frame(struct xsk_umem_info *umem, u64 addr)
  785. {
  786.     memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data,
  787.            PKT_SIZE);
  788. }
  789.  
  790. static void xsk_configure_umem(struct xsk_umem_info *umem,
  791.                                void *buffer, u64 size)
  792. {
  793.     struct xsk_umem_config cfg = {
  794.         /* We recommend that you set the fill ring size >= HW RX ring size +
  795.          * AF_XDP RX ring size. Make sure you fill up the fill ring
  796.          * with buffers at regular intervals, and you will with this setting
  797.          * avoid allocation failures in the driver. These are usually quite
  798.          * expensive since drivers have not been written to assume that
  799.          * allocation failures are common. For regular sockets, kernel
  800.          * allocated memory is used that only runs out in OOM situations
  801.          * that should be rare.
  802.          */
  803.         .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS * 2,
  804.         .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS,
  805.         .frame_size = opt_xsk_frame_size,
  806.         .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM,
  807.         .flags = opt_umem_flags
  808.     };
  809.     int ret;
  810.  
  811.     /* Use the queues of the first port (always present) for umem init */
  812.     ret = xsk_umem__create(&umem->umem, buffer, size, &ports[0].fq,
  813.                            &ports[0].cq, &cfg);
  814.     if (ret)
  815.         exit_with_error(-ret);
  816.  
  817.     umem->buffer = buffer;
  818. }
  819.  
  820. static void xsk_populate_fill_ring(struct xsk_port_info *port, int start)
  821. {
  822.     int ret, i;
  823.     u32 idx;
  824.  
  825.     ret = xsk_ring_prod__reserve(&port->fq,
  826.                      XSK_RING_PROD__DEFAULT_NUM_DESCS * 2, &idx);
  827.     if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS * 2)
  828.         exit_with_error(-ret);
  829.     for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS * 2; i++)
  830.         *xsk_ring_prod__fill_addr(&port->fq, idx++) =
  831.             (start + i) * opt_xsk_frame_size;
  832.     xsk_ring_prod__submit(&port->fq, XSK_RING_PROD__DEFAULT_NUM_DESCS * 2);
  833. }
  834.  
  835. static struct xsk_socket_info *xsk_configure_socket(struct xsk_port_info *port,
  836.                                                     bool rx, bool tx)
  837. {
  838.     struct xsk_socket_config cfg;
  839.     struct xsk_socket_info *xsk;
  840.     struct xsk_ring_cons *rxr;
  841.     struct xsk_ring_prod *txr;
  842.     int ret;
  843.  
  844.     xsk = calloc(1, sizeof(*xsk));
  845.     if (!xsk)
  846.         exit_with_error(errno);
  847.  
  848.     xsk->port = port;
  849.     cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS;
  850.     cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS;
  851.     if (opt_num_xsks > 1 || opt_reduced_cap)
  852.         cfg.libbpf_flags = XSK_LIBBPF_FLAGS__INHIBIT_PROG_LOAD;
  853.     else
  854.         cfg.libbpf_flags = 0;
  855.     cfg.xdp_flags = opt_xdp_flags;
  856.     cfg.bind_flags = opt_xdp_bind_flags;
  857.  
  858.     rxr = rx ? &xsk->rx : NULL;
  859.     txr = tx ? &xsk->tx : NULL;
  860.     ret = xsk_socket__create_shared(&xsk->xsk, port->ifname, port->queue,
  861.                                     port->umem->umem, rxr, txr, &port->fq,
  862.                                     &port->cq, &cfg);
  863.     if (ret)
  864.         exit_with_error(-ret);
  865.  
  866.     xsk->app_stats.rx_empty_polls = 0;
  867.     xsk->app_stats.fill_fail_polls = 0;
  868.     xsk->app_stats.copy_tx_sendtos = 0;
  869.     xsk->app_stats.tx_wakeup_sendtos = 0;
  870.     xsk->app_stats.opt_polls = 0;
  871.     xsk->app_stats.prev_rx_empty_polls = 0;
  872.     xsk->app_stats.prev_fill_fail_polls = 0;
  873.     xsk->app_stats.prev_copy_tx_sendtos = 0;
  874.     xsk->app_stats.prev_tx_wakeup_sendtos = 0;
  875.     xsk->app_stats.prev_opt_polls = 0;
  876.  
  877.     return xsk;
  878. }
  879.  
  880. static struct option long_options[] = {
  881.     {"rxdrop", no_argument, 0, 'r'},
  882.     {"txonly", no_argument, 0, 't'},
  883.     {"l2fwd", no_argument, 0, 'l'},
  884.     {"iface", required_argument, 0, 'i'},
  885.     {"queue", required_argument, 0, 'q'},
  886.     {"poll", no_argument, 0, 'p'},
  887.     {"xdp-skb", no_argument, 0, 'S'},
  888.     {"xdp-native", no_argument, 0, 'N'},
  889.     {"interval", required_argument, 0, 'n'},
  890.     {"zero-copy", no_argument, 0, 'z'},
  891.     {"copy", no_argument, 0, 'c'},
  892.     {"frame-size", required_argument, 0, 'f'},
  893.     {"no-need-wakeup", no_argument, 0, 'm'},
  894.     {"unaligned", no_argument, 0, 'u'},
  895.     {"shared-umem", no_argument, 0, 'M'},
  896.     {"force", no_argument, 0, 'F'},
  897.     {"duration", required_argument, 0, 'd'},
  898.     {"batch-size", required_argument, 0, 'b'},
  899.     {"tx-pkt-count", required_argument, 0, 'C'},
  900.     {"tx-pkt-size", required_argument, 0, 's'},
  901.     {"tx-pkt-pattern", required_argument, 0, 'P'},
  902.     {"extra-stats", no_argument, 0, 'x'},
  903.     {"quiet", no_argument, 0, 'Q'},
  904.     {"app-stats", no_argument, 0, 'a'},
  905.     {"irq-string", no_argument, 0, 'I'},
  906.     {"busy-poll", no_argument, 0, 'B'},
  907.     {"reduce-cap", no_argument, 0, 'R'},
  908.     {0, 0, 0, 0}
  909. };
  910.  
  911. static void usage(const char *prog)
  912. {
  913.     const char *str =
  914.         "  Usage: %s [OPTIONS]\n"
  915.         "  Options:\n"
  916.         "  -r, --rxdrop     Discard all incoming packets (default)\n"
  917.         "  -t, --txonly     Only send packets\n"
  918.         "  -l, --l2fwd      MAC swap L2 forwarding\n"
  919.         "  -i, --iface=if[:q]   Run on interface if and queue q\n"
  920.         "           (up to %d interfaces can be specified for the l2fwd benchmark)\n"
  921.         "           (default queue 0)\n"
  922.         "  -p, --poll       Use poll syscall\n"
  923.         "  -S, --xdp-skb=n  Use XDP skb-mod\n"
  924.         "  -N, --xdp-native=n   Enforce XDP native mode\n"
  925.         "  -n, --interval=n Specify statistics update interval (default 1 sec).\n"
  926.         "  -z, --zero-copy      Force zero-copy mode.\n"
  927.         "  -c, --copy           Force copy mode.\n"
  928.         "  -m, --no-need-wakeup Turn off use of driver need wakeup flag.\n"
  929.         "  -f, --frame-size=n   Set the frame size (must be a power of two in aligned mode, default is %d).\n"
  930.         "  -u, --unaligned  Enable unaligned chunk placement\n"
  931.         "  -M, --shared-umem    Enable XDP_SHARED_UMEM (cannot be used with -R)\n"
  932.         "  -F, --force      Force loading the XDP prog\n"
  933.         "  -d, --duration=n Duration in secs to run command.\n"
  934.         "           Default: forever.\n"
  935.         "  -b, --batch-size=n   Batch size for sending or receiving\n"
  936.         "           packets. Default: %d\n"
  937.         "  -C, --tx-pkt-count=n Number of packets to send.\n"
  938.         "           Default: Continuous packets.\n"
  939.         "  -s, --tx-pkt-size=n  Transmit packet size.\n"
  940.         "           (Default: %d bytes)\n"
  941.         "           Min size: %d, Max size %d.\n"
  942.         "  -P, --tx-pkt-pattern=nPacket fill pattern. Default: 0x%x\n"
  943.         "  -x, --extra-stats    Display extra statistics.\n"
  944.         "  -Q, --quiet          Do not display any stats.\n"
  945.         "  -a, --app-stats  Display application (syscall) statistics.\n"
  946.         "  -I, --irq-string Display driver interrupt statistics for interface associated with irq-string.\n"
  947.         "  -B, --busy-poll      Busy poll.\n"
  948.         "  -R, --reduce-cap Use reduced capabilities (cannot be used with -M)\n"
  949.         "\n";
  950.     fprintf(stderr, str, prog, MAX_PORTS, XSK_UMEM__DEFAULT_FRAME_SIZE,
  951.         opt_batch_size, MIN_PKT_SIZE, MIN_PKT_SIZE,
  952.         XSK_UMEM__DEFAULT_FRAME_SIZE, opt_pkt_fill_pattern);
  953.  
  954.     exit(EXIT_FAILURE);
  955. }
  956.  
  957. static void parse_command_line(int argc, char **argv)
  958. {
  959.     int option_index, c;
  960.  
  961.     opterr = 0;
  962.  
  963.     for (;;) {
  964.         c = getopt_long(argc, argv, "Frtli:pSNn:czf:muMd:b:C:s:P:xQaI:BR",
  965.                 long_options, &option_index);
  966.         if (c == -1)
  967.             break;
  968.  
  969.         switch (c) {
  970.         case 'r':
  971.             opt_bench = BENCH_RXDROP;
  972.             break;
  973.         case 't':
  974.             opt_bench = BENCH_TXONLY;
  975.             break;
  976.         case 'l':
  977.             opt_bench = BENCH_L2FWD;
  978.             break;
  979.         case 'i':
  980.             if (opt_num_ports >= MAX_PORTS) {
  981.                 fprintf(stderr, "ERROR: at most %d interfaces are supported\n",
  982.                     MAX_PORTS);
  983.                 usage(basename(argv[0]));
  984.             }
  985.             for (int i = 0; optarg[i] != 0; i++) {
  986.                 if (optarg[i] == ':') {
  987.                     optarg[i] = 0;
  988.                     ports[opt_num_ports].queue = atoi(optarg + i + 1);
  989.                     break;
  990.                 }
  991.             }
  992.             ports[opt_num_ports].ifname = optarg;
  993.             opt_num_ports++;
  994.             break;
  995.         case 'p':
  996.             opt_poll = 1;
  997.             break;
  998.         case 'S':
  999.             opt_xdp_flags |= XDP_FLAGS_SKB_MODE;
  1000.             opt_xdp_bind_flags |= XDP_COPY;
  1001.             break;
  1002.         case 'N':
  1003.             /* default, set below */
  1004.             break;
  1005.         case 'n':
  1006.             opt_interval = atoi(optarg);
  1007.             break;
  1008.         case 'z':
  1009.             opt_xdp_bind_flags |= XDP_ZEROCOPY;
  1010.             break;
  1011.         case 'c':
  1012.             opt_xdp_bind_flags |= XDP_COPY;
  1013.             break;
  1014.         case 'u':
  1015.             opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG;
  1016.             opt_unaligned_chunks = 1;
  1017.             opt_mmap_flags = MAP_HUGETLB;
  1018.             break;
  1019.         case 'F':
  1020.             opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST;
  1021.             break;
  1022.         case 'f':
  1023.             opt_xsk_frame_size = atoi(optarg);
  1024.             break;
  1025.         case 'm':
  1026.             opt_need_wakeup = false;
  1027.             opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP;
  1028.             break;
  1029.         case 'M':
  1030.             opt_num_xsks = MAX_SOCKS;
  1031.             break;
  1032.         case 'd':
  1033.             opt_duration = atoi(optarg);
  1034.             opt_duration *= 1000000000;
  1035.             break;
  1036.         case 'b':
  1037.             opt_batch_size = atoi(optarg);
  1038.             break;
  1039.         case 'C':
  1040.             opt_pkt_count = atoi(optarg);
  1041.             break;
  1042.         case 's':
  1043.             opt_pkt_size = atoi(optarg);
  1044.             if (opt_pkt_size > (XSK_UMEM__DEFAULT_FRAME_SIZE) ||
  1045.                 opt_pkt_size < MIN_PKT_SIZE) {
  1046.                 fprintf(stderr,
  1047.                     "ERROR: Invalid frame size %d\n",
  1048.                     opt_pkt_size);
  1049.                 usage(basename(argv[0]));
  1050.             }
  1051.             break;
  1052.         case 'P':
  1053.             opt_pkt_fill_pattern = strtol(optarg, NULL, 16);
  1054.             break;
  1055.         case 'x':
  1056.             opt_extra_stats = 1;
  1057.             break;
  1058.         case 'Q':
  1059.             opt_quiet = 1;
  1060.             break;
  1061.         case 'a':
  1062.             opt_app_stats = 1;
  1063.             break;
  1064.         case 'I':
  1065.             opt_irq_str = optarg;
  1066.             if (get_interrupt_number())
  1067.                 irqs_at_init = get_irqs();
  1068.             if (irqs_at_init < 0) {
  1069.                 fprintf(stderr, "ERROR: Failed to get irqs for %s\n", opt_irq_str);
  1070.                 usage(basename(argv[0]));
  1071.             }
  1072.             break;
  1073.         case 'B':
  1074.             opt_busy_poll = 1;
  1075.             break;
  1076.         case 'R':
  1077.             opt_reduced_cap = true;
  1078.             break;
  1079.         default:
  1080.             usage(basename(argv[0]));
  1081.         }
  1082.     }
  1083.  
  1084.     if (!(opt_xdp_flags & XDP_FLAGS_SKB_MODE))
  1085.         opt_xdp_flags |= XDP_FLAGS_DRV_MODE;
  1086.  
  1087.     if (opt_num_ports > 1 && opt_bench != BENCH_L2FWD) {
  1088.         fprintf(stderr, "ERROR: multiple interfaces are only supported for the "
  1089.                 "l2fwd benchmark\n");
  1090.         usage(basename(argv[0]));
  1091.     }
  1092.  
  1093.     for (int i = 0; i < opt_num_ports; i++) {
  1094.         ports[i].ifindex = if_nametoindex(ports[i].ifname);
  1095.         if (!ports[i].ifindex) {
  1096.             fprintf(stderr, "ERROR: interface \"%s\" does not exist\n",
  1097.                 ports[i].ifname);
  1098.             usage(basename(argv[0]));
  1099.         }
  1100.     }
  1101.  
  1102.     if (opt_num_xsks == MAX_SOCKS) {
  1103.         opt_num_xsks /= opt_num_ports;
  1104.     }
  1105.  
  1106.     if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) &&
  1107.         !opt_unaligned_chunks) {
  1108.         fprintf(stderr, "--frame-size=%d is not a power of two\n",
  1109.             opt_xsk_frame_size);
  1110.         usage(basename(argv[0]));
  1111.     }
  1112.  
  1113.     if (opt_reduced_cap && opt_num_xsks > 1) {
  1114.         fprintf(stderr, "ERROR: -M and -R cannot be used together\n");
  1115.         usage(basename(argv[0]));
  1116.     }
  1117. }
  1118.  
  1119. static void kick_tx(struct xsk_socket_info *xsk)
  1120. {
  1121.     int ret;
  1122.  
  1123.     ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0);
  1124.     if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN ||
  1125.         errno == EBUSY || errno == ENETDOWN)
  1126.         return;
  1127.     exit_with_error(errno);
  1128. }
  1129.  
  1130. static inline void complete_tx_l2fwd(struct xsk_socket_info *complete_xsk,
  1131.                                      struct xsk_socket_info *fill_xsk)
  1132. {
  1133.     struct xsk_port_info *complete_port = complete_xsk->port;
  1134.     struct xsk_port_info *fill_port = fill_xsk->port;
  1135.     u32 idx_cq = 0, idx_fq = 0;
  1136.     unsigned int rcvd;
  1137.     size_t ndescs;
  1138.  
  1139.     if (!complete_xsk->outstanding_tx)
  1140.         return;
  1141.  
  1142.     /* In copy mode, Tx is driven by a syscall so we need to use e.g. sendto() to
  1143.      * really send the packets. In zero-copy mode we do not have to do this, since Tx
  1144.      * is driven by the NAPI loop. So as an optimization, we do not have to call
  1145.      * sendto() all the time in zero-copy mode for l2fwd.
  1146.      */
  1147.     if (opt_xdp_bind_flags & XDP_COPY) {
  1148.         complete_xsk->app_stats.copy_tx_sendtos++;
  1149.         kick_tx(complete_xsk);
  1150.     }
  1151.  
  1152.     ndescs = (complete_xsk->outstanding_tx > opt_batch_size) ? opt_batch_size :
  1153.         complete_xsk->outstanding_tx;
  1154.  
  1155.     /* re-add completed Tx buffers */
  1156.     rcvd = xsk_ring_cons__peek(&complete_port->cq, ndescs, &idx_cq);
  1157.     if (rcvd > 0) {
  1158.         unsigned int i;
  1159.         int ret;
  1160.  
  1161.         ret = xsk_ring_prod__reserve(&fill_port->fq, rcvd, &idx_fq);
  1162.         while (ret != rcvd) {
  1163.             if (ret < 0)
  1164.                 exit_with_error(-ret);
  1165.             if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&fill_port->fq)) {
  1166.                 fill_xsk->app_stats.fill_fail_polls++;
  1167.                 recvfrom(xsk_socket__fd(fill_xsk->xsk), NULL, 0, MSG_DONTWAIT,
  1168.                          NULL, NULL);
  1169.             }
  1170.             ret = xsk_ring_prod__reserve(&fill_port->fq, rcvd, &idx_fq);
  1171.         }
  1172.  
  1173.         for (i = 0; i < rcvd; i++)
  1174.             *xsk_ring_prod__fill_addr(&fill_port->fq, idx_fq++) =
  1175.                 *xsk_ring_cons__comp_addr(&complete_port->cq, idx_cq++);
  1176.  
  1177.         xsk_ring_prod__submit(&fill_port->fq, rcvd);
  1178.         xsk_ring_cons__release(&complete_port->cq, rcvd);
  1179.         complete_xsk->outstanding_tx -= rcvd;
  1180.     }
  1181. }
  1182.  
  1183. static inline void complete_tx_only(struct xsk_socket_info *xsk,
  1184.                     int batch_size)
  1185. {
  1186.     unsigned int rcvd;
  1187.     u32 idx;
  1188.  
  1189.     if (!xsk->outstanding_tx)
  1190.         return;
  1191.  
  1192.     if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx)) {
  1193.         xsk->app_stats.tx_wakeup_sendtos++;
  1194.         kick_tx(xsk);
  1195.     }
  1196.  
  1197.     rcvd = xsk_ring_cons__peek(&xsk->port->cq, batch_size, &idx);
  1198.     if (rcvd > 0) {
  1199.         xsk_ring_cons__release(&xsk->port->cq, rcvd);
  1200.         xsk->outstanding_tx -= rcvd;
  1201.     }
  1202. }
  1203.  
  1204. static void rx_drop(struct xsk_socket_info *xsk)
  1205. {
  1206.     unsigned int rcvd, i;
  1207.     u32 idx_rx = 0, idx_fq = 0;
  1208.     int ret;
  1209.  
  1210.     rcvd = xsk_ring_cons__peek(&xsk->rx, opt_batch_size, &idx_rx);
  1211.     if (!rcvd) {
  1212.         if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->port->fq)) {
  1213.             xsk->app_stats.rx_empty_polls++;
  1214.             recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
  1215.         }
  1216.         return;
  1217.     }
  1218.  
  1219.     ret = xsk_ring_prod__reserve(&xsk->port->fq, rcvd, &idx_fq);
  1220.     while (ret != rcvd) {
  1221.         if (ret < 0)
  1222.             exit_with_error(-ret);
  1223.         if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&xsk->port->fq)) {
  1224.             xsk->app_stats.fill_fail_polls++;
  1225.             recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL);
  1226.         }
  1227.         ret = xsk_ring_prod__reserve(&xsk->port->fq, rcvd, &idx_fq);
  1228.     }
  1229.  
  1230.     for (i = 0; i < rcvd; i++) {
  1231.         u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr;
  1232.         u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len;
  1233.         u64 orig = xsk_umem__extract_addr(addr);
  1234.  
  1235.         addr = xsk_umem__add_offset_to_addr(addr);
  1236.         char *pkt = xsk_umem__get_data(xsk->port->umem->buffer, addr);
  1237.  
  1238.         swap_mac_addresses(pkt);
  1239.  
  1240.         hex_dump(pkt, len, addr);
  1241.         *xsk_ring_prod__fill_addr(&xsk->port->fq, idx_fq++) = orig;
  1242.     }
  1243.  
  1244.     xsk_ring_prod__submit(&xsk->port->fq, rcvd);
  1245.     xsk_ring_cons__release(&xsk->rx, rcvd);
  1246.     xsk->ring_stats.rx_npkts += rcvd;
  1247. }
  1248.  
  1249. static void rx_drop_all(void)
  1250. {
  1251.     struct pollfd fds[MAX_SOCKS] = {};
  1252.     int i, ret;
  1253.  
  1254.     for (i = 0; i < num_socks; i++) {
  1255.         fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
  1256.         fds[i].events = POLLIN;
  1257.     }
  1258.  
  1259.     for (;;) {
  1260.         if (opt_poll) {
  1261.             for (i = 0; i < num_socks; i++)
  1262.                 xsks[i]->app_stats.opt_polls++;
  1263.             ret = poll(fds, num_socks, opt_timeout);
  1264.             if (ret <= 0)
  1265.                 continue;
  1266.         }
  1267.  
  1268.         for (i = 0; i < num_socks; i++)
  1269.             rx_drop(xsks[i]);
  1270.  
  1271.         if (benchmark_done)
  1272.             break;
  1273.     }
  1274. }
  1275.  
  1276. static void tx_only(struct xsk_socket_info *xsk, u32 *frame_nb, int batch_size)
  1277. {
  1278.     u32 idx;
  1279.     unsigned int i;
  1280.  
  1281.     while (xsk_ring_prod__reserve(&xsk->tx, batch_size, &idx) <
  1282.                       batch_size) {
  1283.         complete_tx_only(xsk, batch_size);
  1284.         if (benchmark_done)
  1285.             return;
  1286.     }
  1287.  
  1288.     for (i = 0; i < batch_size; i++) {
  1289.         struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx,
  1290.                                   idx + i);
  1291.         tx_desc->addr = (*frame_nb + i) * opt_xsk_frame_size;
  1292.         tx_desc->len = PKT_SIZE;
  1293.     }
  1294.  
  1295.     xsk_ring_prod__submit(&xsk->tx, batch_size);
  1296.     xsk->ring_stats.tx_npkts += batch_size;
  1297.     xsk->outstanding_tx += batch_size;
  1298.     *frame_nb += batch_size;
  1299.     *frame_nb %= NUM_FRAMES;
  1300.     complete_tx_only(xsk, batch_size);
  1301. }
  1302.  
  1303. static inline int get_batch_size(int pkt_cnt)
  1304. {
  1305.     if (!opt_pkt_count)
  1306.         return opt_batch_size;
  1307.  
  1308.     if (pkt_cnt + opt_batch_size <= opt_pkt_count)
  1309.         return opt_batch_size;
  1310.  
  1311.     return opt_pkt_count - pkt_cnt;
  1312. }
  1313.  
  1314. static void complete_tx_only_all(void)
  1315. {
  1316.     bool pending;
  1317.     int i;
  1318.  
  1319.     do {
  1320.         pending = false;
  1321.         for (i = 0; i < num_socks; i++) {
  1322.             if (xsks[i]->outstanding_tx) {
  1323.                 complete_tx_only(xsks[i], opt_batch_size);
  1324.                 pending = !!xsks[i]->outstanding_tx;
  1325.             }
  1326.         }
  1327.     } while (pending);
  1328. }
  1329.  
  1330. static void tx_only_all(void)
  1331. {
  1332.     struct pollfd fds[MAX_SOCKS] = {};
  1333.     u32 frame_nb[MAX_SOCKS] = {};
  1334.     int pkt_cnt = 0;
  1335.     int i, ret;
  1336.  
  1337.     for (i = 0; i < num_socks; i++) {
  1338.         fds[0].fd = xsk_socket__fd(xsks[i]->xsk);
  1339.         fds[0].events = POLLOUT;
  1340.     }
  1341.  
  1342.     while ((opt_pkt_count && pkt_cnt < opt_pkt_count) || !opt_pkt_count) {
  1343.         int batch_size = get_batch_size(pkt_cnt);
  1344.  
  1345.         if (opt_poll) {
  1346.             for (i = 0; i < num_socks; i++)
  1347.                 xsks[i]->app_stats.opt_polls++;
  1348.             ret = poll(fds, num_socks, opt_timeout);
  1349.             if (ret <= 0)
  1350.                 continue;
  1351.  
  1352.             if (!(fds[0].revents & POLLOUT))
  1353.                 continue;
  1354.         }
  1355.  
  1356.         for (i = 0; i < num_socks; i++)
  1357.             tx_only(xsks[i], &frame_nb[i], batch_size);
  1358.  
  1359.         pkt_cnt += batch_size;
  1360.  
  1361.         if (benchmark_done)
  1362.             break;
  1363.     }
  1364.  
  1365.     if (opt_pkt_count)
  1366.         complete_tx_only_all();
  1367. }
  1368.  
  1369. static void l2fwd(struct xsk_socket_info *rx_xsk,
  1370.                   struct xsk_socket_info *tx_xsk)
  1371. {
  1372.     unsigned int rcvd, i;
  1373.     u32 idx_rx = 0, idx_tx = 0;
  1374.     int ret;
  1375.  
  1376.     complete_tx_l2fwd(tx_xsk, rx_xsk);
  1377.  
  1378.     rcvd = xsk_ring_cons__peek(&rx_xsk->rx, opt_batch_size, &idx_rx);
  1379.     if (!rcvd) {
  1380.         if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&rx_xsk->port->fq)) {
  1381.             rx_xsk->app_stats.rx_empty_polls++;
  1382.             recvfrom(xsk_socket__fd(rx_xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL,
  1383.                      NULL);
  1384.         }
  1385.         return;
  1386.     }
  1387.     rx_xsk->ring_stats.rx_npkts += rcvd;
  1388.  
  1389.     ret = xsk_ring_prod__reserve(&tx_xsk->tx, rcvd, &idx_tx);
  1390.     while (ret != rcvd) {
  1391.         if (ret < 0)
  1392.             exit_with_error(-ret);
  1393.         complete_tx_l2fwd(tx_xsk, rx_xsk);
  1394.         if (opt_busy_poll || xsk_ring_prod__needs_wakeup(&tx_xsk->tx)) {
  1395.             tx_xsk->app_stats.tx_wakeup_sendtos++;
  1396.             kick_tx(tx_xsk);
  1397.         }
  1398.         ret = xsk_ring_prod__reserve(&tx_xsk->tx, rcvd, &idx_tx);
  1399.     }
  1400.  
  1401.     for (i = 0; i < rcvd; i++) {
  1402.         u64 addr = xsk_ring_cons__rx_desc(&rx_xsk->rx, idx_rx)->addr;
  1403.         u32 len = xsk_ring_cons__rx_desc(&rx_xsk->rx, idx_rx++)->len;
  1404.         u64 orig = addr;
  1405.  
  1406.         addr = xsk_umem__add_offset_to_addr(addr);
  1407.         char *pkt = xsk_umem__get_data(rx_xsk->port->umem->buffer, addr);
  1408.  
  1409.         swap_mac_addresses(pkt);
  1410.  
  1411.         hex_dump(pkt, len, addr);
  1412.         xsk_ring_prod__tx_desc(&tx_xsk->tx, idx_tx)->addr = orig;
  1413.         xsk_ring_prod__tx_desc(&tx_xsk->tx, idx_tx++)->len = len;
  1414.     }
  1415.  
  1416.     xsk_ring_prod__submit(&tx_xsk->tx, rcvd);
  1417.     xsk_ring_cons__release(&rx_xsk->rx, rcvd);
  1418.  
  1419.     tx_xsk->ring_stats.tx_npkts += rcvd;
  1420.     tx_xsk->outstanding_tx += rcvd;
  1421. }
  1422.  
  1423. static void l2fwd_all(void)
  1424. {
  1425.     struct pollfd fds[MAX_SOCKS] = {};
  1426.     int i, ret;
  1427.  
  1428.     for (;;) {
  1429.         if (opt_poll) {
  1430.             for (i = 0; i < num_socks; i++) {
  1431.                 fds[i].fd = xsk_socket__fd(xsks[i]->xsk);
  1432.                 fds[i].events = POLLIN;
  1433.                 xsks[i]->app_stats.opt_polls++;
  1434.             }
  1435.             ret = poll(fds, num_socks, opt_timeout);
  1436.             if (ret <= 0)
  1437.                 continue;
  1438.         }
  1439.  
  1440.         for (i = 0; i < num_socks; i++)
  1441.             l2fwd(xsks[i], xsks[(i + 1) % num_socks]);
  1442.  
  1443.         if (benchmark_done)
  1444.             break;
  1445.     }
  1446. }
  1447.  
  1448. static void load_xdp_program(char **argv, struct bpf_object **obj)
  1449. {
  1450.     struct bpf_prog_load_attr prog_load_attr = {
  1451.         .prog_type      = BPF_PROG_TYPE_XDP,
  1452.     };
  1453.     char xdp_filename[256];
  1454.     int prog_fd;
  1455.  
  1456.     snprintf(xdp_filename, sizeof(xdp_filename), "%s_kern.o", argv[0]);
  1457.     prog_load_attr.file = xdp_filename;
  1458.  
  1459.     if (bpf_prog_load_xattr(&prog_load_attr, obj, &prog_fd))
  1460.         exit(EXIT_FAILURE);
  1461.     if (prog_fd < 0) {
  1462.         fprintf(stderr, "ERROR: no program found: %s\n",
  1463.             strerror(prog_fd));
  1464.         exit(EXIT_FAILURE);
  1465.     }
  1466.  
  1467.     for (int i = 0; i < opt_num_ports; i++) {
  1468.         if (bpf_set_link_xdp_fd(ports[i].ifindex, prog_fd, opt_xdp_flags) < 0) {
  1469.             fprintf(stderr, "ERROR: link set xdp fd failed on %s\n",
  1470.                 ports[i].ifname);
  1471.             exit(EXIT_FAILURE);
  1472.         }
  1473.     }
  1474. }
  1475.  
  1476. static void enter_xsks_into_map(struct bpf_object *obj)
  1477. {
  1478.     struct bpf_map *map;
  1479.     int i, xsks_map;
  1480.  
  1481.     map = bpf_object__find_map_by_name(obj, "xsks_map");
  1482.     xsks_map = bpf_map__fd(map);
  1483.     if (xsks_map < 0) {
  1484.         fprintf(stderr, "ERROR: no xsks map found: %s\n",
  1485.             strerror(xsks_map));
  1486.             exit(EXIT_FAILURE);
  1487.     }
  1488.  
  1489.     for (i = 0; i < num_socks; i++) {
  1490.         int fd = xsk_socket__fd(xsks[i]->xsk);
  1491.         int key, ret;
  1492.  
  1493.         key = i;
  1494.         ret = bpf_map_update_elem(xsks_map, &key, &fd, 0);
  1495.         if (ret) {
  1496.             fprintf(stderr, "ERROR: bpf_map_update_elem %d\n", i);
  1497.             exit(EXIT_FAILURE);
  1498.         }
  1499.     }
  1500. }
  1501.  
  1502. static void apply_setsockopt(struct xsk_socket_info *xsk)
  1503. {
  1504.     int sock_opt;
  1505.  
  1506.     if (!opt_busy_poll)
  1507.         return;
  1508.  
  1509.     sock_opt = 1;
  1510.     if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_PREFER_BUSY_POLL,
  1511.                (void *)&sock_opt, sizeof(sock_opt)) < 0)
  1512.         exit_with_error(errno);
  1513.  
  1514.     sock_opt = 20;
  1515.     if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL,
  1516.                (void *)&sock_opt, sizeof(sock_opt)) < 0)
  1517.         exit_with_error(errno);
  1518.  
  1519.     sock_opt = opt_batch_size;
  1520.     if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL_BUDGET,
  1521.                (void *)&sock_opt, sizeof(sock_opt)) < 0)
  1522.         exit_with_error(errno);
  1523. }
  1524.  
  1525. static int recv_xsks_map_fd_from_ctrl_node(int sock, int *_fd)
  1526. {
  1527.     char cms[CMSG_SPACE(sizeof(int))];
  1528.     struct cmsghdr *cmsg;
  1529.     struct msghdr msg;
  1530.     struct iovec iov;
  1531.     int value;
  1532.     int len;
  1533.  
  1534.     iov.iov_base = &value;
  1535.     iov.iov_len = sizeof(int);
  1536.  
  1537.     msg.msg_name = 0;
  1538.     msg.msg_namelen = 0;
  1539.     msg.msg_iov = &iov;
  1540.     msg.msg_iovlen = 1;
  1541.     msg.msg_flags = 0;
  1542.     msg.msg_control = (caddr_t)cms;
  1543.     msg.msg_controllen = sizeof(cms);
  1544.  
  1545.     len = recvmsg(sock, &msg, 0);
  1546.  
  1547.     if (len < 0) {
  1548.         fprintf(stderr, "Recvmsg failed length incorrect.\n");
  1549.         return -EINVAL;
  1550.     }
  1551.  
  1552.     if (len == 0) {
  1553.         fprintf(stderr, "Recvmsg failed no data\n");
  1554.         return -EINVAL;
  1555.     }
  1556.  
  1557.     cmsg = CMSG_FIRSTHDR(&msg);
  1558.     *_fd = *(int *)CMSG_DATA(cmsg);
  1559.  
  1560.     return 0;
  1561. }
  1562.  
  1563. static int
  1564. recv_xsks_map_fd(int *xsks_map_fd)
  1565. {
  1566.     struct sockaddr_un server;
  1567.     int err;
  1568.  
  1569.     sock = socket(AF_UNIX, SOCK_STREAM, 0);
  1570.     if (sock < 0) {
  1571.         fprintf(stderr, "Error opening socket stream: %s", strerror(errno));
  1572.         return errno;
  1573.     }
  1574.  
  1575.     server.sun_family = AF_UNIX;
  1576.     strcpy(server.sun_path, SOCKET_NAME);
  1577.  
  1578.     if (connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un)) < 0) {
  1579.         close(sock);
  1580.         fprintf(stderr, "Error connecting stream socket: %s", strerror(errno));
  1581.         return errno;
  1582.     }
  1583.  
  1584.     err = recv_xsks_map_fd_from_ctrl_node(sock, xsks_map_fd);
  1585.     if (err) {
  1586.         fprintf(stderr, "Error %d receiving fd\n", err);
  1587.         return err;
  1588.     }
  1589.     return 0;
  1590. }
  1591.  
  1592. int main(int argc, char **argv)
  1593. {
  1594.     struct __user_cap_header_struct hdr = { _LINUX_CAPABILITY_VERSION_3, 0 };
  1595.     struct __user_cap_data_struct data[2] = { { 0 } };
  1596.     bool rx = false, tx = false;
  1597.     struct bpf_object *obj;
  1598.     int xsks_map_fd = 0;
  1599.     pthread_t pt;
  1600.     int i, ret;
  1601.     void *bufs;
  1602.  
  1603.     parse_command_line(argc, argv);
  1604.  
  1605.     if (opt_reduced_cap) {
  1606.         if (capget(&hdr, data)  < 0)
  1607.             fprintf(stderr, "Error getting capabilities\n");
  1608.  
  1609.         data->effective &= CAP_TO_MASK(CAP_NET_RAW);
  1610.         data->permitted &= CAP_TO_MASK(CAP_NET_RAW);
  1611.  
  1612.         if (capset(&hdr, data) < 0)
  1613.             fprintf(stderr, "Setting capabilities failed\n");
  1614.  
  1615.         if (capget(&hdr, data)  < 0) {
  1616.             fprintf(stderr, "Error getting capabilities\n");
  1617.         } else {
  1618.             fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
  1619.                 data[0].effective, data[0].inheritable, data[0].permitted);
  1620.             fprintf(stderr, "Capabilities EFF %x Caps INH %x Caps Per %x\n",
  1621.                 data[1].effective, data[1].inheritable, data[1].permitted);
  1622.         }
  1623.     } else {
  1624.         if (opt_num_xsks > 1)
  1625.             load_xdp_program(argv, &obj);
  1626.     }
  1627.  
  1628.     /* Reserve memory for the umem. Use hugepages if unaligned chunk mode */
  1629.     bufs = mmap(NULL, NUM_FRAMES * opt_xsk_frame_size * opt_num_ports,
  1630.             PROT_READ | PROT_WRITE,
  1631.             MAP_PRIVATE | MAP_ANONYMOUS | opt_mmap_flags, -1, 0);
  1632.     if (bufs == MAP_FAILED) {
  1633.         printf("ERROR: mmap failed\n");
  1634.         exit(EXIT_FAILURE);
  1635.     }
  1636.  
  1637.     /* Create sockets... */
  1638.     xsk_configure_umem(&umem, bufs,
  1639.                        NUM_FRAMES * opt_xsk_frame_size * opt_num_ports);
  1640.     for (i = 0; i < opt_num_ports; i++)
  1641.         ports[i].umem = &umem;
  1642.  
  1643.     if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD)
  1644.         rx = true;
  1645.     if (opt_bench == BENCH_L2FWD || opt_bench == BENCH_TXONLY)
  1646.         tx = true;
  1647.  
  1648.     for (i = 0; i < opt_num_xsks * opt_num_ports; i++) {
  1649.         xsks[num_socks++] = xsk_configure_socket(&ports[i % opt_num_ports], rx,
  1650.                                                  tx);
  1651.         apply_setsockopt(xsks[i]);
  1652.     }  
  1653.  
  1654.     if (opt_bench == BENCH_RXDROP || opt_bench == BENCH_L2FWD) {
  1655.         for (i = 0; i < opt_num_ports; i++)
  1656.             xsk_populate_fill_ring(&ports[i], i * NUM_FRAMES);
  1657.  
  1658.     } else {
  1659.         gen_eth_hdr_data();
  1660.  
  1661.         for (i = 0; i < NUM_FRAMES; i++)
  1662.             gen_eth_frame(&umem, i * opt_xsk_frame_size);
  1663.     }
  1664.  
  1665.     if (opt_num_xsks > 1 && opt_bench != BENCH_TXONLY)
  1666.         enter_xsks_into_map(obj);
  1667.  
  1668.     if (opt_reduced_cap) {
  1669.         ret = recv_xsks_map_fd(&xsks_map_fd);
  1670.         if (ret) {
  1671.             fprintf(stderr, "Error %d receiving xsks_map_fd\n", ret);
  1672.             exit_with_error(ret);
  1673.         }
  1674.         if (xsks[0]->xsk) {
  1675.             ret = xsk_socket__update_xskmap(xsks[0]->xsk, xsks_map_fd);
  1676.             if (ret) {
  1677.                 fprintf(stderr, "Update of BPF map failed(%d)\n", ret);
  1678.                 exit_with_error(ret);
  1679.             }
  1680.         }
  1681.     }
  1682.  
  1683.     signal(SIGINT, int_exit);
  1684.     signal(SIGTERM, int_exit);
  1685.     signal(SIGABRT, int_exit);
  1686.  
  1687.     setlocale(LC_ALL, "");
  1688.  
  1689.     if (!opt_quiet) {
  1690.         ret = pthread_create(&pt, NULL, poller, NULL);
  1691.         if (ret)
  1692.             exit_with_error(ret);
  1693.     }
  1694.  
  1695.     prev_time = get_nsecs();
  1696.     start_time = prev_time;
  1697.  
  1698.     if (opt_bench == BENCH_RXDROP)
  1699.         rx_drop_all();
  1700.     else if (opt_bench == BENCH_TXONLY)
  1701.         tx_only_all();
  1702.     else
  1703.         l2fwd_all();
  1704.  
  1705.     benchmark_done = true;
  1706.  
  1707.     if (!opt_quiet)
  1708.         pthread_join(pt, NULL);
  1709.  
  1710.     xdpsock_cleanup();
  1711.  
  1712.     munmap(bufs, NUM_FRAMES * opt_xsk_frame_size);
  1713.  
  1714.     return 0;
  1715. }
  1716.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement