Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Björn Töpel | dac0914 | 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 | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 4 | #include <asm/barrier.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 5 | #include <errno.h> |
| 6 | #include <getopt.h> |
| 7 | #include <libgen.h> |
| 8 | #include <linux/bpf.h> |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 9 | #include <linux/compiler.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 10 | #include <linux/if_link.h> |
| 11 | #include <linux/if_xdp.h> |
| 12 | #include <linux/if_ether.h> |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 13 | #include <locale.h> |
| 14 | #include <net/ethernet.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 15 | #include <net/if.h> |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 16 | #include <poll.h> |
| 17 | #include <pthread.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 18 | #include <signal.h> |
| 19 | #include <stdbool.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 23 | #include <sys/mman.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 24 | #include <sys/resource.h> |
| 25 | #include <sys/socket.h> |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 26 | #include <sys/types.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 27 | #include <time.h> |
| 28 | #include <unistd.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 29 | |
Daniel T. Lee | 4d18f6d | 2019-06-16 00:14:47 +0900 | [diff] [blame] | 30 | #include "libbpf.h" |
| 31 | #include "xsk.h" |
Jakub Kicinski | 2bf3e2e | 2018-05-14 22:35:02 -0700 | [diff] [blame] | 32 | #include <bpf/bpf.h> |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 33 | |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 34 | #ifndef SOL_XDP |
| 35 | #define SOL_XDP 283 |
| 36 | #endif |
| 37 | |
| 38 | #ifndef AF_XDP |
| 39 | #define AF_XDP 44 |
| 40 | #endif |
| 41 | |
| 42 | #ifndef PF_XDP |
| 43 | #define PF_XDP AF_XDP |
| 44 | #endif |
| 45 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 46 | #define NUM_FRAMES (4 * 1024) |
| 47 | #define BATCH_SIZE 64 |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 48 | |
| 49 | #define DEBUG_HEXDUMP 0 |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 50 | #define MAX_SOCKS 8 |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 51 | |
Björn Töpel | a412ef5 | 2018-06-04 13:57:14 +0200 | [diff] [blame] | 52 | typedef __u64 u64; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 53 | typedef __u32 u32; |
| 54 | |
| 55 | static unsigned long prev_time; |
| 56 | |
| 57 | enum benchmark_type { |
| 58 | BENCH_RXDROP = 0, |
| 59 | BENCH_TXONLY = 1, |
| 60 | BENCH_L2FWD = 2, |
| 61 | }; |
| 62 | |
| 63 | static enum benchmark_type opt_bench = BENCH_RXDROP; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 64 | static u32 opt_xdp_flags = XDP_FLAGS_UPDATE_IF_NOEXIST; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 65 | static const char *opt_if = ""; |
| 66 | static int opt_ifindex; |
| 67 | static int opt_queue; |
| 68 | static int opt_poll; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 69 | static int opt_interval = 1; |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 70 | static u32 opt_xdp_bind_flags = XDP_USE_NEED_WAKEUP; |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 71 | static u32 opt_umem_flags; |
| 72 | static int opt_unaligned_chunks; |
| 73 | static u32 opt_xdp_bind_flags; |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 74 | static int opt_xsk_frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE; |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 75 | static int opt_timeout = 1000; |
| 76 | static bool opt_need_wakeup = true; |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 77 | static __u32 prog_id; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 78 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 79 | struct xsk_umem_info { |
| 80 | struct xsk_ring_prod fq; |
| 81 | struct xsk_ring_cons cq; |
| 82 | struct xsk_umem *umem; |
| 83 | void *buffer; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 86 | struct xsk_socket_info { |
| 87 | struct xsk_ring_cons rx; |
| 88 | struct xsk_ring_prod tx; |
| 89 | struct xsk_umem_info *umem; |
| 90 | struct xsk_socket *xsk; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 91 | unsigned long rx_npkts; |
| 92 | unsigned long tx_npkts; |
| 93 | unsigned long prev_rx_npkts; |
| 94 | unsigned long prev_tx_npkts; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 95 | u32 outstanding_tx; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 98 | static int num_socks; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 99 | struct xsk_socket_info *xsks[MAX_SOCKS]; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 100 | |
| 101 | static unsigned long get_nsecs(void) |
| 102 | { |
| 103 | struct timespec ts; |
| 104 | |
| 105 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 106 | return ts.tv_sec * 1000000000UL + ts.tv_nsec; |
| 107 | } |
| 108 | |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 109 | static void print_benchmark(bool running) |
| 110 | { |
| 111 | const char *bench_str = "INVALID"; |
| 112 | |
| 113 | if (opt_bench == BENCH_RXDROP) |
| 114 | bench_str = "rxdrop"; |
| 115 | else if (opt_bench == BENCH_TXONLY) |
| 116 | bench_str = "txonly"; |
| 117 | else if (opt_bench == BENCH_L2FWD) |
| 118 | bench_str = "l2fwd"; |
| 119 | |
| 120 | printf("%s:%d %s ", opt_if, opt_queue, bench_str); |
| 121 | if (opt_xdp_flags & XDP_FLAGS_SKB_MODE) |
| 122 | printf("xdp-skb "); |
| 123 | else if (opt_xdp_flags & XDP_FLAGS_DRV_MODE) |
| 124 | printf("xdp-drv "); |
| 125 | else |
| 126 | printf(" "); |
| 127 | |
| 128 | if (opt_poll) |
| 129 | printf("poll() "); |
| 130 | |
| 131 | if (running) { |
| 132 | printf("running..."); |
| 133 | fflush(stdout); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static void dump_stats(void) |
| 138 | { |
| 139 | unsigned long now = get_nsecs(); |
| 140 | long dt = now - prev_time; |
| 141 | int i; |
| 142 | |
| 143 | prev_time = now; |
| 144 | |
Prashant Bhole | 11c3f51 | 2018-08-31 10:00:49 +0900 | [diff] [blame] | 145 | for (i = 0; i < num_socks && xsks[i]; i++) { |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 146 | char *fmt = "%-15s %'-11.0f %'-11lu\n"; |
| 147 | double rx_pps, tx_pps; |
| 148 | |
| 149 | rx_pps = (xsks[i]->rx_npkts - xsks[i]->prev_rx_npkts) * |
| 150 | 1000000000. / dt; |
| 151 | tx_pps = (xsks[i]->tx_npkts - xsks[i]->prev_tx_npkts) * |
| 152 | 1000000000. / dt; |
| 153 | |
| 154 | printf("\n sock%d@", i); |
| 155 | print_benchmark(false); |
| 156 | printf("\n"); |
| 157 | |
| 158 | printf("%-15s %-11s %-11s %-11.2f\n", "", "pps", "pkts", |
| 159 | dt / 1000000000.); |
| 160 | printf(fmt, "rx", rx_pps, xsks[i]->rx_npkts); |
| 161 | printf(fmt, "tx", tx_pps, xsks[i]->tx_npkts); |
| 162 | |
| 163 | xsks[i]->prev_rx_npkts = xsks[i]->rx_npkts; |
| 164 | xsks[i]->prev_tx_npkts = xsks[i]->tx_npkts; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | static void *poller(void *arg) |
| 169 | { |
| 170 | (void)arg; |
| 171 | for (;;) { |
| 172 | sleep(opt_interval); |
| 173 | dump_stats(); |
| 174 | } |
| 175 | |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 179 | static void remove_xdp_program(void) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 180 | { |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 181 | __u32 curr_prog_id = 0; |
| 182 | |
Maciej Fijalkowski | 3b7a8ec | 2019-02-01 22:42:30 +0100 | [diff] [blame] | 183 | if (bpf_get_link_xdp_id(opt_ifindex, &curr_prog_id, opt_xdp_flags)) { |
| 184 | printf("bpf_get_link_xdp_id failed\n"); |
| 185 | exit(EXIT_FAILURE); |
| 186 | } |
| 187 | if (prog_id == curr_prog_id) |
| 188 | bpf_set_link_xdp_fd(opt_ifindex, -1, opt_xdp_flags); |
| 189 | else if (!curr_prog_id) |
| 190 | printf("couldn't find a prog id on a given interface\n"); |
| 191 | else |
| 192 | printf("program on interface changed, not removing\n"); |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void int_exit(int sig) |
| 196 | { |
| 197 | struct xsk_umem *umem = xsks[0]->umem->umem; |
| 198 | |
| 199 | (void)sig; |
| 200 | |
| 201 | dump_stats(); |
| 202 | xsk_socket__delete(xsks[0]->xsk); |
| 203 | (void)xsk_umem__delete(umem); |
| 204 | remove_xdp_program(); |
| 205 | |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 206 | exit(EXIT_SUCCESS); |
| 207 | } |
| 208 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 209 | static void __exit_with_error(int error, const char *file, const char *func, |
| 210 | int line) |
| 211 | { |
| 212 | fprintf(stderr, "%s:%s:%i: errno: %d/\"%s\"\n", file, func, |
| 213 | line, error, strerror(error)); |
| 214 | dump_stats(); |
| 215 | remove_xdp_program(); |
| 216 | exit(EXIT_FAILURE); |
| 217 | } |
| 218 | |
| 219 | #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, \ |
| 220 | __LINE__) |
| 221 | |
| 222 | static const char pkt_data[] = |
| 223 | "\x3c\xfd\xfe\x9e\x7f\x71\xec\xb1\xd7\x98\x3a\xc0\x08\x00\x45\x00" |
| 224 | "\x00\x2e\x00\x00\x00\x00\x40\x11\x88\x97\x05\x08\x07\x08\xc8\x14" |
| 225 | "\x1e\x04\x10\x92\x10\x92\x00\x1a\x6d\xa3\x34\x33\x1f\x69\x40\x6b" |
| 226 | "\x54\x59\xb6\x14\x2d\x11\x44\xbf\xaf\xd9\xbe\xaa"; |
| 227 | |
| 228 | static void swap_mac_addresses(void *data) |
| 229 | { |
| 230 | struct ether_header *eth = (struct ether_header *)data; |
| 231 | struct ether_addr *src_addr = (struct ether_addr *)ð->ether_shost; |
| 232 | struct ether_addr *dst_addr = (struct ether_addr *)ð->ether_dhost; |
| 233 | struct ether_addr tmp; |
| 234 | |
| 235 | tmp = *src_addr; |
| 236 | *src_addr = *dst_addr; |
| 237 | *dst_addr = tmp; |
| 238 | } |
| 239 | |
| 240 | static void hex_dump(void *pkt, size_t length, u64 addr) |
| 241 | { |
| 242 | const unsigned char *address = (unsigned char *)pkt; |
| 243 | const unsigned char *line = address; |
| 244 | size_t line_size = 32; |
| 245 | unsigned char c; |
| 246 | char buf[32]; |
| 247 | int i = 0; |
| 248 | |
| 249 | if (!DEBUG_HEXDUMP) |
| 250 | return; |
| 251 | |
| 252 | sprintf(buf, "addr=%llu", addr); |
| 253 | printf("length = %zu\n", length); |
| 254 | printf("%s | ", buf); |
| 255 | while (length-- > 0) { |
| 256 | printf("%02X ", *address++); |
| 257 | if (!(++i % line_size) || (length == 0 && i % line_size)) { |
| 258 | if (length == 0) { |
| 259 | while (i++ % line_size) |
| 260 | printf("__ "); |
| 261 | } |
| 262 | printf(" | "); /* right close */ |
| 263 | while (line < address) { |
| 264 | c = *line++; |
| 265 | printf("%c", (c < 33 || c == 255) ? 0x2E : c); |
| 266 | } |
| 267 | printf("\n"); |
| 268 | if (length > 0) |
| 269 | printf("%s | ", buf); |
| 270 | } |
| 271 | } |
| 272 | printf("\n"); |
| 273 | } |
| 274 | |
| 275 | static size_t gen_eth_frame(struct xsk_umem_info *umem, u64 addr) |
| 276 | { |
| 277 | memcpy(xsk_umem__get_data(umem->buffer, addr), pkt_data, |
| 278 | sizeof(pkt_data) - 1); |
| 279 | return sizeof(pkt_data) - 1; |
| 280 | } |
| 281 | |
| 282 | static struct xsk_umem_info *xsk_configure_umem(void *buffer, u64 size) |
| 283 | { |
| 284 | struct xsk_umem_info *umem; |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 285 | struct xsk_umem_config cfg = { |
| 286 | .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, |
| 287 | .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS, |
| 288 | .frame_size = opt_xsk_frame_size, |
| 289 | .frame_headroom = XSK_UMEM__DEFAULT_FRAME_HEADROOM, |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 290 | .flags = opt_umem_flags |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 291 | }; |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 292 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 293 | int ret; |
| 294 | |
| 295 | umem = calloc(1, sizeof(*umem)); |
| 296 | if (!umem) |
| 297 | exit_with_error(errno); |
| 298 | |
| 299 | ret = xsk_umem__create(&umem->umem, buffer, size, &umem->fq, &umem->cq, |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 300 | &cfg); |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 301 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 302 | if (ret) |
| 303 | exit_with_error(-ret); |
| 304 | |
| 305 | umem->buffer = buffer; |
| 306 | return umem; |
| 307 | } |
| 308 | |
| 309 | static struct xsk_socket_info *xsk_configure_socket(struct xsk_umem_info *umem) |
| 310 | { |
| 311 | struct xsk_socket_config cfg; |
| 312 | struct xsk_socket_info *xsk; |
| 313 | int ret; |
| 314 | u32 idx; |
| 315 | int i; |
| 316 | |
| 317 | xsk = calloc(1, sizeof(*xsk)); |
| 318 | if (!xsk) |
| 319 | exit_with_error(errno); |
| 320 | |
| 321 | xsk->umem = umem; |
| 322 | cfg.rx_size = XSK_RING_CONS__DEFAULT_NUM_DESCS; |
| 323 | cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS; |
| 324 | cfg.libbpf_flags = 0; |
| 325 | cfg.xdp_flags = opt_xdp_flags; |
| 326 | cfg.bind_flags = opt_xdp_bind_flags; |
| 327 | ret = xsk_socket__create(&xsk->xsk, opt_if, opt_queue, umem->umem, |
| 328 | &xsk->rx, &xsk->tx, &cfg); |
| 329 | if (ret) |
| 330 | exit_with_error(-ret); |
| 331 | |
| 332 | ret = bpf_get_link_xdp_id(opt_ifindex, &prog_id, opt_xdp_flags); |
| 333 | if (ret) |
| 334 | exit_with_error(-ret); |
| 335 | |
| 336 | ret = xsk_ring_prod__reserve(&xsk->umem->fq, |
| 337 | XSK_RING_PROD__DEFAULT_NUM_DESCS, |
| 338 | &idx); |
| 339 | if (ret != XSK_RING_PROD__DEFAULT_NUM_DESCS) |
| 340 | exit_with_error(-ret); |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 341 | for (i = 0; i < XSK_RING_PROD__DEFAULT_NUM_DESCS; i++) |
| 342 | *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx++) = |
| 343 | i * opt_xsk_frame_size; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 344 | xsk_ring_prod__submit(&xsk->umem->fq, |
| 345 | XSK_RING_PROD__DEFAULT_NUM_DESCS); |
| 346 | |
| 347 | return xsk; |
| 348 | } |
| 349 | |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 350 | static struct option long_options[] = { |
| 351 | {"rxdrop", no_argument, 0, 'r'}, |
| 352 | {"txonly", no_argument, 0, 't'}, |
| 353 | {"l2fwd", no_argument, 0, 'l'}, |
| 354 | {"interface", required_argument, 0, 'i'}, |
| 355 | {"queue", required_argument, 0, 'q'}, |
| 356 | {"poll", no_argument, 0, 'p'}, |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 357 | {"xdp-skb", no_argument, 0, 'S'}, |
| 358 | {"xdp-native", no_argument, 0, 'N'}, |
| 359 | {"interval", required_argument, 0, 'n'}, |
Björn Töpel | 58c50ae | 2018-08-28 14:44:35 +0200 | [diff] [blame] | 360 | {"zero-copy", no_argument, 0, 'z'}, |
| 361 | {"copy", no_argument, 0, 'c'}, |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 362 | {"frame-size", required_argument, 0, 'f'}, |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 363 | {"no-need-wakeup", no_argument, 0, 'm'}, |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 364 | {"unaligned", no_argument, 0, 'u'}, |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 365 | {0, 0, 0, 0} |
| 366 | }; |
| 367 | |
| 368 | static void usage(const char *prog) |
| 369 | { |
| 370 | const char *str = |
| 371 | " Usage: %s [OPTIONS]\n" |
| 372 | " Options:\n" |
| 373 | " -r, --rxdrop Discard all incoming packets (default)\n" |
| 374 | " -t, --txonly Only send packets\n" |
| 375 | " -l, --l2fwd MAC swap L2 forwarding\n" |
| 376 | " -i, --interface=n Run on interface n\n" |
| 377 | " -q, --queue=n Use queue n (default 0)\n" |
| 378 | " -p, --poll Use poll syscall\n" |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 379 | " -S, --xdp-skb=n Use XDP skb-mod\n" |
| 380 | " -N, --xdp-native=n Enfore XDP native mode\n" |
| 381 | " -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] | 382 | " -z, --zero-copy Force zero-copy mode.\n" |
| 383 | " -c, --copy Force copy mode.\n" |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 384 | " -f, --frame-size=n Set the frame size (must be a power of two, default is %d).\n" |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 385 | " -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^] | 386 | " -f, --frame-size=n Set the frame size (must be a power of two in aligned mode, default is %d).\n" |
| 387 | " -u, --unaligned Enable unaligned chunk placement\n" |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 388 | "\n"; |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 389 | fprintf(stderr, str, prog, XSK_UMEM__DEFAULT_FRAME_SIZE); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 390 | exit(EXIT_FAILURE); |
| 391 | } |
| 392 | |
| 393 | static void parse_command_line(int argc, char **argv) |
| 394 | { |
| 395 | int option_index, c; |
| 396 | |
| 397 | opterr = 0; |
| 398 | |
| 399 | for (;;) { |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 400 | c = getopt_long(argc, argv, "Frtli:q:psSNn:czf:mu", |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 401 | long_options, &option_index); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 402 | if (c == -1) |
| 403 | break; |
| 404 | |
| 405 | switch (c) { |
| 406 | case 'r': |
| 407 | opt_bench = BENCH_RXDROP; |
| 408 | break; |
| 409 | case 't': |
| 410 | opt_bench = BENCH_TXONLY; |
| 411 | break; |
| 412 | case 'l': |
| 413 | opt_bench = BENCH_L2FWD; |
| 414 | break; |
| 415 | case 'i': |
| 416 | opt_if = optarg; |
| 417 | break; |
| 418 | case 'q': |
| 419 | opt_queue = atoi(optarg); |
| 420 | break; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 421 | case 'p': |
| 422 | opt_poll = 1; |
| 423 | break; |
| 424 | case 'S': |
| 425 | opt_xdp_flags |= XDP_FLAGS_SKB_MODE; |
Björn Töpel | 9f5232c | 2018-06-04 14:06:01 +0200 | [diff] [blame] | 426 | opt_xdp_bind_flags |= XDP_COPY; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 427 | break; |
| 428 | case 'N': |
| 429 | opt_xdp_flags |= XDP_FLAGS_DRV_MODE; |
| 430 | break; |
| 431 | case 'n': |
| 432 | opt_interval = atoi(optarg); |
| 433 | break; |
Björn Töpel | 58c50ae | 2018-08-28 14:44:35 +0200 | [diff] [blame] | 434 | case 'z': |
| 435 | opt_xdp_bind_flags |= XDP_ZEROCOPY; |
| 436 | break; |
| 437 | case 'c': |
| 438 | opt_xdp_bind_flags |= XDP_COPY; |
| 439 | break; |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 440 | case 'u': |
| 441 | opt_umem_flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG; |
| 442 | opt_unaligned_chunks = 1; |
| 443 | break; |
Maciej Fijalkowski | 743e568 | 2019-02-01 22:42:28 +0100 | [diff] [blame] | 444 | case 'F': |
| 445 | opt_xdp_flags &= ~XDP_FLAGS_UPDATE_IF_NOEXIST; |
| 446 | break; |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 447 | case 'f': |
| 448 | opt_xsk_frame_size = atoi(optarg); |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 449 | case 'm': |
| 450 | opt_need_wakeup = false; |
| 451 | opt_xdp_bind_flags &= ~XDP_USE_NEED_WAKEUP; |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 452 | break; |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 453 | |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 454 | default: |
| 455 | usage(basename(argv[0])); |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | opt_ifindex = if_nametoindex(opt_if); |
| 460 | if (!opt_ifindex) { |
| 461 | fprintf(stderr, "ERROR: interface \"%s\" does not exist\n", |
| 462 | opt_if); |
| 463 | usage(basename(argv[0])); |
| 464 | } |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 465 | |
Kevin Laatz | c543f54 | 2019-08-27 02:25:28 +0000 | [diff] [blame^] | 466 | if ((opt_xsk_frame_size & (opt_xsk_frame_size - 1)) && |
| 467 | !opt_unaligned_chunks) { |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 468 | fprintf(stderr, "--frame-size=%d is not a power of two\n", |
| 469 | opt_xsk_frame_size); |
| 470 | usage(basename(argv[0])); |
| 471 | } |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 472 | } |
| 473 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 474 | static void kick_tx(struct xsk_socket_info *xsk) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 475 | { |
| 476 | int ret; |
| 477 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 478 | ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0); |
Magnus Karlsson | c03079c | 2018-06-29 09:48:19 +0200 | [diff] [blame] | 479 | if (ret >= 0 || errno == ENOBUFS || errno == EAGAIN || errno == EBUSY) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 480 | return; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 481 | exit_with_error(errno); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 484 | static inline void complete_tx_l2fwd(struct xsk_socket_info *xsk, |
| 485 | struct pollfd *fds) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 486 | { |
Yonghong Song | b74e21a | 2019-02-28 22:19:41 -0800 | [diff] [blame] | 487 | u32 idx_cq = 0, idx_fq = 0; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 488 | unsigned int rcvd; |
| 489 | size_t ndescs; |
| 490 | |
| 491 | if (!xsk->outstanding_tx) |
| 492 | return; |
| 493 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 494 | if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx)) |
| 495 | kick_tx(xsk); |
| 496 | |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 497 | ndescs = (xsk->outstanding_tx > BATCH_SIZE) ? BATCH_SIZE : |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 498 | xsk->outstanding_tx; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 499 | |
| 500 | /* re-add completed Tx buffers */ |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 501 | rcvd = xsk_ring_cons__peek(&xsk->umem->cq, ndescs, &idx_cq); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 502 | if (rcvd > 0) { |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 503 | unsigned int i; |
| 504 | int ret; |
| 505 | |
| 506 | ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq); |
| 507 | while (ret != rcvd) { |
| 508 | if (ret < 0) |
| 509 | exit_with_error(-ret); |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 510 | if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) |
| 511 | ret = poll(fds, num_socks, opt_timeout); |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 512 | ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, |
| 513 | &idx_fq); |
| 514 | } |
| 515 | for (i = 0; i < rcvd; i++) |
| 516 | *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = |
| 517 | *xsk_ring_cons__comp_addr(&xsk->umem->cq, |
| 518 | idx_cq++); |
| 519 | |
| 520 | xsk_ring_prod__submit(&xsk->umem->fq, rcvd); |
| 521 | xsk_ring_cons__release(&xsk->umem->cq, rcvd); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 522 | xsk->outstanding_tx -= rcvd; |
| 523 | xsk->tx_npkts += rcvd; |
| 524 | } |
| 525 | } |
| 526 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 527 | static inline void complete_tx_only(struct xsk_socket_info *xsk) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 528 | { |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 529 | unsigned int rcvd; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 530 | u32 idx; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 531 | |
| 532 | if (!xsk->outstanding_tx) |
| 533 | return; |
| 534 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 535 | if (!opt_need_wakeup || xsk_ring_prod__needs_wakeup(&xsk->tx)) |
| 536 | kick_tx(xsk); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 537 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 538 | rcvd = xsk_ring_cons__peek(&xsk->umem->cq, BATCH_SIZE, &idx); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 539 | if (rcvd > 0) { |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 540 | xsk_ring_cons__release(&xsk->umem->cq, rcvd); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 541 | xsk->outstanding_tx -= rcvd; |
| 542 | xsk->tx_npkts += rcvd; |
| 543 | } |
| 544 | } |
| 545 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 546 | static void rx_drop(struct xsk_socket_info *xsk, struct pollfd *fds) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 547 | { |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 548 | unsigned int rcvd, i; |
Yonghong Song | b74e21a | 2019-02-28 22:19:41 -0800 | [diff] [blame] | 549 | u32 idx_rx = 0, idx_fq = 0; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 550 | int ret; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 551 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 552 | rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx); |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 553 | if (!rcvd) { |
| 554 | if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) |
| 555 | ret = poll(fds, num_socks, opt_timeout); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 556 | return; |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 557 | } |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 558 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 559 | ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq); |
| 560 | while (ret != rcvd) { |
| 561 | if (ret < 0) |
| 562 | exit_with_error(-ret); |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 563 | if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) |
| 564 | ret = poll(fds, num_socks, opt_timeout); |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 565 | ret = xsk_ring_prod__reserve(&xsk->umem->fq, rcvd, &idx_fq); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 566 | } |
| 567 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 568 | for (i = 0; i < rcvd; i++) { |
| 569 | u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr; |
| 570 | u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len; |
| 571 | char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 572 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 573 | hex_dump(pkt, len, addr); |
| 574 | *xsk_ring_prod__fill_addr(&xsk->umem->fq, idx_fq++) = addr; |
| 575 | } |
| 576 | |
| 577 | xsk_ring_prod__submit(&xsk->umem->fq, rcvd); |
| 578 | xsk_ring_cons__release(&xsk->rx, rcvd); |
| 579 | xsk->rx_npkts += rcvd; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | static void rx_drop_all(void) |
| 583 | { |
| 584 | struct pollfd fds[MAX_SOCKS + 1]; |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 585 | int i, ret; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 586 | |
| 587 | memset(fds, 0, sizeof(fds)); |
| 588 | |
| 589 | for (i = 0; i < num_socks; i++) { |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 590 | fds[i].fd = xsk_socket__fd(xsks[i]->xsk); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 591 | fds[i].events = POLLIN; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | for (;;) { |
| 595 | if (opt_poll) { |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 596 | ret = poll(fds, num_socks, opt_timeout); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 597 | if (ret <= 0) |
| 598 | continue; |
| 599 | } |
| 600 | |
| 601 | for (i = 0; i < num_socks; i++) |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 602 | rx_drop(xsks[i], fds); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 603 | } |
| 604 | } |
| 605 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 606 | static void tx_only(struct xsk_socket_info *xsk, u32 frame_nb) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 607 | { |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 608 | u32 idx; |
| 609 | |
| 610 | if (xsk_ring_prod__reserve(&xsk->tx, BATCH_SIZE, &idx) == BATCH_SIZE) { |
| 611 | unsigned int i; |
| 612 | |
| 613 | for (i = 0; i < BATCH_SIZE; i++) { |
| 614 | xsk_ring_prod__tx_desc(&xsk->tx, idx + i)->addr = |
| 615 | (frame_nb + i) << XSK_UMEM__DEFAULT_FRAME_SHIFT; |
| 616 | xsk_ring_prod__tx_desc(&xsk->tx, idx + i)->len = |
| 617 | sizeof(pkt_data) - 1; |
| 618 | } |
| 619 | |
| 620 | xsk_ring_prod__submit(&xsk->tx, BATCH_SIZE); |
| 621 | xsk->outstanding_tx += BATCH_SIZE; |
| 622 | frame_nb += BATCH_SIZE; |
| 623 | frame_nb %= NUM_FRAMES; |
| 624 | } |
| 625 | |
| 626 | complete_tx_only(xsk); |
| 627 | } |
| 628 | |
| 629 | static void tx_only_all(void) |
| 630 | { |
| 631 | struct pollfd fds[MAX_SOCKS]; |
| 632 | u32 frame_nb[MAX_SOCKS] = {}; |
| 633 | int i, ret; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 634 | |
| 635 | memset(fds, 0, sizeof(fds)); |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 636 | for (i = 0; i < num_socks; i++) { |
| 637 | fds[0].fd = xsk_socket__fd(xsks[i]->xsk); |
| 638 | fds[0].events = POLLOUT; |
| 639 | } |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 640 | |
| 641 | for (;;) { |
| 642 | if (opt_poll) { |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 643 | ret = poll(fds, num_socks, opt_timeout); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 644 | if (ret <= 0) |
| 645 | continue; |
| 646 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 647 | if (!(fds[0].revents & POLLOUT)) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 648 | continue; |
| 649 | } |
| 650 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 651 | for (i = 0; i < num_socks; i++) |
| 652 | tx_only(xsks[i], frame_nb[i]); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 656 | static void l2fwd(struct xsk_socket_info *xsk, struct pollfd *fds) |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 657 | { |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 658 | unsigned int rcvd, i; |
| 659 | u32 idx_rx = 0, idx_tx = 0; |
| 660 | int ret; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 661 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 662 | complete_tx_l2fwd(xsk, fds); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 663 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 664 | rcvd = xsk_ring_cons__peek(&xsk->rx, BATCH_SIZE, &idx_rx); |
| 665 | if (!rcvd) { |
| 666 | if (xsk_ring_prod__needs_wakeup(&xsk->umem->fq)) |
| 667 | ret = poll(fds, num_socks, opt_timeout); |
| 668 | return; |
| 669 | } |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 670 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 671 | ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx); |
| 672 | while (ret != rcvd) { |
| 673 | if (ret < 0) |
| 674 | exit_with_error(-ret); |
| 675 | if (xsk_ring_prod__needs_wakeup(&xsk->tx)) |
| 676 | kick_tx(xsk); |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 677 | ret = xsk_ring_prod__reserve(&xsk->tx, rcvd, &idx_tx); |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | for (i = 0; i < rcvd; i++) { |
| 681 | u64 addr = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx)->addr; |
| 682 | u32 len = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++)->len; |
| 683 | char *pkt = xsk_umem__get_data(xsk->umem->buffer, addr); |
| 684 | |
| 685 | swap_mac_addresses(pkt); |
| 686 | |
| 687 | hex_dump(pkt, len, addr); |
| 688 | xsk_ring_prod__tx_desc(&xsk->tx, idx_tx)->addr = addr; |
| 689 | xsk_ring_prod__tx_desc(&xsk->tx, idx_tx++)->len = len; |
| 690 | } |
| 691 | |
| 692 | xsk_ring_prod__submit(&xsk->tx, rcvd); |
| 693 | xsk_ring_cons__release(&xsk->rx, rcvd); |
| 694 | |
| 695 | xsk->rx_npkts += rcvd; |
| 696 | xsk->outstanding_tx += rcvd; |
| 697 | } |
| 698 | |
| 699 | static void l2fwd_all(void) |
| 700 | { |
| 701 | struct pollfd fds[MAX_SOCKS]; |
| 702 | int i, ret; |
| 703 | |
| 704 | memset(fds, 0, sizeof(fds)); |
| 705 | |
| 706 | for (i = 0; i < num_socks; i++) { |
| 707 | fds[i].fd = xsk_socket__fd(xsks[i]->xsk); |
| 708 | fds[i].events = POLLOUT | POLLIN; |
| 709 | } |
| 710 | |
| 711 | for (;;) { |
| 712 | if (opt_poll) { |
| 713 | ret = poll(fds, num_socks, opt_timeout); |
| 714 | if (ret <= 0) |
| 715 | continue; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 716 | } |
| 717 | |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 718 | for (i = 0; i < num_socks; i++) |
| 719 | l2fwd(xsks[i], fds); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 720 | } |
| 721 | } |
| 722 | |
| 723 | int main(int argc, char **argv) |
| 724 | { |
| 725 | struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 726 | struct xsk_umem_info *umem; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 727 | pthread_t pt; |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 728 | void *bufs; |
| 729 | int ret; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 730 | |
| 731 | parse_command_line(argc, argv); |
| 732 | |
| 733 | if (setrlimit(RLIMIT_MEMLOCK, &r)) { |
| 734 | fprintf(stderr, "ERROR: setrlimit(RLIMIT_MEMLOCK) \"%s\"\n", |
| 735 | strerror(errno)); |
| 736 | exit(EXIT_FAILURE); |
| 737 | } |
| 738 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 739 | ret = posix_memalign(&bufs, getpagesize(), /* PAGE_SIZE aligned */ |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 740 | NUM_FRAMES * opt_xsk_frame_size); |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 741 | if (ret) |
| 742 | exit_with_error(ret); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 743 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 744 | /* Create sockets... */ |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 745 | umem = xsk_configure_umem(bufs, NUM_FRAMES * opt_xsk_frame_size); |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 746 | xsks[num_socks++] = xsk_configure_socket(umem); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 747 | |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 748 | if (opt_bench == BENCH_TXONLY) { |
| 749 | int i; |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 750 | |
Maxim Mikityanskiy | 123e8da1 | 2019-06-26 17:35:27 +0300 | [diff] [blame] | 751 | for (i = 0; i < NUM_FRAMES; i++) |
| 752 | (void)gen_eth_frame(umem, i * opt_xsk_frame_size); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | signal(SIGINT, int_exit); |
| 756 | signal(SIGTERM, int_exit); |
| 757 | signal(SIGABRT, int_exit); |
| 758 | |
| 759 | setlocale(LC_ALL, ""); |
| 760 | |
| 761 | ret = pthread_create(&pt, NULL, poller, NULL); |
Magnus Karlsson | 248c7f9 | 2019-02-21 10:21:27 +0100 | [diff] [blame] | 762 | if (ret) |
| 763 | exit_with_error(ret); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 764 | |
| 765 | prev_time = get_nsecs(); |
| 766 | |
| 767 | if (opt_bench == BENCH_RXDROP) |
| 768 | rx_drop_all(); |
| 769 | else if (opt_bench == BENCH_TXONLY) |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 770 | tx_only_all(); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 771 | else |
Magnus Karlsson | 46738f7 | 2019-08-14 09:27:21 +0200 | [diff] [blame] | 772 | l2fwd_all(); |
Magnus Karlsson | b4b8faa | 2018-05-02 13:01:36 +0200 | [diff] [blame] | 773 | |
| 774 | return 0; |
| 775 | } |