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