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