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