1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright(c) 2020 Intel Corporation. */ 3 4 /* 5 * Some functions in this program are taken from 6 * Linux kernel samples/bpf/xdpsock* and modified 7 * for use. 8 * 9 * See test_xsk.sh for detailed information on test topology 10 * and prerequisite network setup. 11 * 12 * This test program contains two threads, each thread is single socket with 13 * a unique UMEM. It validates in-order packet delivery and packet content 14 * by sending packets to each other. 15 * 16 * Tests Information: 17 * ------------------ 18 * These selftests test AF_XDP SKB and Native/DRV modes using veth 19 * Virtual Ethernet interfaces. 20 * 21 * For each mode, the following tests are run: 22 * a. nopoll - soft-irq processing in run-to-completion mode 23 * b. poll - using poll() syscall 24 * c. Socket Teardown 25 * Create a Tx and a Rx socket, Tx from one socket, Rx on another. Destroy 26 * both sockets, then repeat multiple times. Only nopoll mode is used 27 * d. Bi-directional sockets 28 * Configure sockets as bi-directional tx/rx sockets, sets up fill and 29 * completion rings on each socket, tx/rx in both directions. Only nopoll 30 * mode is used 31 * e. Statistics 32 * Trigger some error conditions and ensure that the appropriate statistics 33 * are incremented. Within this test, the following statistics are tested: 34 * i. rx dropped 35 * Increase the UMEM frame headroom to a value which results in 36 * insufficient space in the rx buffer for both the packet and the headroom. 37 * ii. tx invalid 38 * Set the 'len' field of tx descriptors to an invalid value (umem frame 39 * size + 1). 40 * iii. rx ring full 41 * Reduce the size of the RX ring to a fraction of the fill ring size. 42 * iv. fill queue empty 43 * Do not populate the fill queue and then try to receive pkts. 44 * f. bpf_link resource persistence 45 * Configure sockets at indexes 0 and 1, run a traffic on queue ids 0, 46 * then remove xsk sockets from queue 0 on both veth interfaces and 47 * finally run a traffic on queues ids 1 48 * g. unaligned mode 49 * h. tests for invalid and corner case Tx descriptors so that the correct ones 50 * are discarded and let through, respectively. 51 * i. 2K frame size tests 52 * j. If multi-buffer is supported, send 9k packets divided into 3 frames 53 * k. If multi-buffer and huge pages are supported, send 9k packets in a single frame 54 * using unaligned mode 55 * l. If multi-buffer is supported, try various nasty combinations of descriptors to 56 * check if they pass the validation or not 57 * 58 * Flow: 59 * ----- 60 * - Single process spawns two threads: Tx and Rx 61 * - Each of these two threads attach to a veth interface 62 * - Each thread creates one AF_XDP socket connected to a unique umem for each 63 * veth interface 64 * - Tx thread Transmits a number of packets from veth<xxxx> to veth<yyyy> 65 * - Rx thread verifies if all packets were received and delivered in-order, 66 * and have the right content 67 * 68 * Enable/disable packet dump mode: 69 * -------------------------- 70 * To enable L2 - L4 headers and payload dump of each packet on STDOUT, add 71 * parameter -D to params array in test_xsk.sh, i.e. params=("-S" "-D") 72 */ 73 74 #define _GNU_SOURCE 75 #include <assert.h> 76 #include <fcntl.h> 77 #include <errno.h> 78 #include <getopt.h> 79 #include <linux/if_link.h> 80 #include <linux/if_ether.h> 81 #include <linux/mman.h> 82 #include <linux/netdev.h> 83 #include <linux/bitmap.h> 84 #include <linux/ethtool.h> 85 #include <arpa/inet.h> 86 #include <net/if.h> 87 #include <locale.h> 88 #include <poll.h> 89 #include <pthread.h> 90 #include <signal.h> 91 #include <stdio.h> 92 #include <stdlib.h> 93 #include <libgen.h> 94 #include <string.h> 95 #include <stddef.h> 96 #include <sys/mman.h> 97 #include <sys/socket.h> 98 #include <sys/time.h> 99 #include <sys/types.h> 100 #include <unistd.h> 101 102 #include "xsk_xdp_progs.skel.h" 103 #include "xsk.h" 104 #include "xskxceiver.h" 105 #include <bpf/bpf.h> 106 #include <linux/filter.h> 107 #include "../kselftest.h" 108 #include "xsk_xdp_common.h" 109 110 #include <network_helpers.h> 111 112 static bool opt_verbose; 113 static bool opt_print_tests; 114 static enum test_mode opt_mode = TEST_MODE_ALL; 115 static u32 opt_run_test = RUN_ALL_TESTS; 116 117 void test__fail(void) { /* for network_helpers.c */ } 118 119 static void __exit_with_error(int error, const char *file, const char *func, int line) 120 { 121 ksft_test_result_fail("[%s:%s:%i]: ERROR: %d/\"%s\"\n", file, func, line, error, 122 strerror(error)); 123 ksft_exit_xfail(); 124 } 125 126 #define exit_with_error(error) __exit_with_error(error, __FILE__, __func__, __LINE__) 127 #define busy_poll_string(test) (test)->ifobj_tx->busy_poll ? "BUSY-POLL " : "" 128 static char *mode_string(struct test_spec *test) 129 { 130 switch (test->mode) { 131 case TEST_MODE_SKB: 132 return "SKB"; 133 case TEST_MODE_DRV: 134 return "DRV"; 135 case TEST_MODE_ZC: 136 return "ZC"; 137 default: 138 return "BOGUS"; 139 } 140 } 141 142 static void report_failure(struct test_spec *test) 143 { 144 if (test->fail) 145 return; 146 147 ksft_test_result_fail("FAIL: %s %s%s\n", mode_string(test), busy_poll_string(test), 148 test->name); 149 test->fail = true; 150 } 151 152 /* The payload is a word consisting of a packet sequence number in the upper 153 * 16-bits and a intra packet data sequence number in the lower 16 bits. So the 3rd packet's 154 * 5th word of data will contain the number (2<<16) | 4 as they are numbered from 0. 155 */ 156 static void write_payload(void *dest, u32 pkt_nb, u32 start, u32 size) 157 { 158 u32 *ptr = (u32 *)dest, i; 159 160 start /= sizeof(*ptr); 161 size /= sizeof(*ptr); 162 for (i = 0; i < size; i++) 163 ptr[i] = htonl(pkt_nb << 16 | (i + start)); 164 } 165 166 static void gen_eth_hdr(struct xsk_socket_info *xsk, struct ethhdr *eth_hdr) 167 { 168 memcpy(eth_hdr->h_dest, xsk->dst_mac, ETH_ALEN); 169 memcpy(eth_hdr->h_source, xsk->src_mac, ETH_ALEN); 170 eth_hdr->h_proto = htons(ETH_P_LOOPBACK); 171 } 172 173 static bool is_umem_valid(struct ifobject *ifobj) 174 { 175 return !!ifobj->umem->umem; 176 } 177 178 static u32 mode_to_xdp_flags(enum test_mode mode) 179 { 180 return (mode == TEST_MODE_SKB) ? XDP_FLAGS_SKB_MODE : XDP_FLAGS_DRV_MODE; 181 } 182 183 static u64 umem_size(struct xsk_umem_info *umem) 184 { 185 return umem->num_frames * umem->frame_size; 186 } 187 188 static int xsk_configure_umem(struct ifobject *ifobj, struct xsk_umem_info *umem, void *buffer, 189 u64 size) 190 { 191 struct xsk_umem_config cfg = { 192 .fill_size = XSK_RING_PROD__DEFAULT_NUM_DESCS, 193 .comp_size = XSK_RING_CONS__DEFAULT_NUM_DESCS, 194 .frame_size = umem->frame_size, 195 .frame_headroom = umem->frame_headroom, 196 .flags = XSK_UMEM__DEFAULT_FLAGS 197 }; 198 int ret; 199 200 if (umem->fill_size) 201 cfg.fill_size = umem->fill_size; 202 203 if (umem->comp_size) 204 cfg.comp_size = umem->comp_size; 205 206 if (umem->unaligned_mode) 207 cfg.flags |= XDP_UMEM_UNALIGNED_CHUNK_FLAG; 208 209 ret = xsk_umem__create(&umem->umem, buffer, size, 210 &umem->fq, &umem->cq, &cfg); 211 if (ret) 212 return ret; 213 214 umem->buffer = buffer; 215 if (ifobj->shared_umem && ifobj->rx_on) { 216 umem->base_addr = umem_size(umem); 217 umem->next_buffer = umem_size(umem); 218 } 219 220 return 0; 221 } 222 223 static u64 umem_alloc_buffer(struct xsk_umem_info *umem) 224 { 225 u64 addr; 226 227 addr = umem->next_buffer; 228 umem->next_buffer += umem->frame_size; 229 if (umem->next_buffer >= umem->base_addr + umem_size(umem)) 230 umem->next_buffer = umem->base_addr; 231 232 return addr; 233 } 234 235 static void umem_reset_alloc(struct xsk_umem_info *umem) 236 { 237 umem->next_buffer = 0; 238 } 239 240 static void enable_busy_poll(struct xsk_socket_info *xsk) 241 { 242 int sock_opt; 243 244 sock_opt = 1; 245 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_PREFER_BUSY_POLL, 246 (void *)&sock_opt, sizeof(sock_opt)) < 0) 247 exit_with_error(errno); 248 249 sock_opt = 20; 250 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL, 251 (void *)&sock_opt, sizeof(sock_opt)) < 0) 252 exit_with_error(errno); 253 254 sock_opt = xsk->batch_size; 255 if (setsockopt(xsk_socket__fd(xsk->xsk), SOL_SOCKET, SO_BUSY_POLL_BUDGET, 256 (void *)&sock_opt, sizeof(sock_opt)) < 0) 257 exit_with_error(errno); 258 } 259 260 static int __xsk_configure_socket(struct xsk_socket_info *xsk, struct xsk_umem_info *umem, 261 struct ifobject *ifobject, bool shared) 262 { 263 struct xsk_socket_config cfg = {}; 264 struct xsk_ring_cons *rxr; 265 struct xsk_ring_prod *txr; 266 267 xsk->umem = umem; 268 cfg.rx_size = xsk->rxqsize; 269 cfg.tx_size = XSK_RING_PROD__DEFAULT_NUM_DESCS; 270 cfg.bind_flags = ifobject->bind_flags; 271 if (shared) 272 cfg.bind_flags |= XDP_SHARED_UMEM; 273 if (ifobject->mtu > MAX_ETH_PKT_SIZE) 274 cfg.bind_flags |= XDP_USE_SG; 275 if (umem->comp_size) 276 cfg.tx_size = umem->comp_size; 277 if (umem->fill_size) 278 cfg.rx_size = umem->fill_size; 279 280 txr = ifobject->tx_on ? &xsk->tx : NULL; 281 rxr = ifobject->rx_on ? &xsk->rx : NULL; 282 return xsk_socket__create(&xsk->xsk, ifobject->ifindex, 0, umem->umem, rxr, txr, &cfg); 283 } 284 285 static bool ifobj_zc_avail(struct ifobject *ifobject) 286 { 287 size_t umem_sz = DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE; 288 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 289 struct xsk_socket_info *xsk; 290 struct xsk_umem_info *umem; 291 bool zc_avail = false; 292 void *bufs; 293 int ret; 294 295 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); 296 if (bufs == MAP_FAILED) 297 exit_with_error(errno); 298 299 umem = calloc(1, sizeof(struct xsk_umem_info)); 300 if (!umem) { 301 munmap(bufs, umem_sz); 302 exit_with_error(ENOMEM); 303 } 304 umem->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE; 305 ret = xsk_configure_umem(ifobject, umem, bufs, umem_sz); 306 if (ret) 307 exit_with_error(-ret); 308 309 xsk = calloc(1, sizeof(struct xsk_socket_info)); 310 if (!xsk) 311 goto out; 312 ifobject->bind_flags = XDP_USE_NEED_WAKEUP | XDP_ZEROCOPY; 313 ifobject->rx_on = true; 314 xsk->rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS; 315 ret = __xsk_configure_socket(xsk, umem, ifobject, false); 316 if (!ret) 317 zc_avail = true; 318 319 xsk_socket__delete(xsk->xsk); 320 free(xsk); 321 out: 322 munmap(umem->buffer, umem_sz); 323 xsk_umem__delete(umem->umem); 324 free(umem); 325 return zc_avail; 326 } 327 328 #define MAX_SKB_FRAGS_PATH "/proc/sys/net/core/max_skb_frags" 329 static unsigned int get_max_skb_frags(void) 330 { 331 unsigned int max_skb_frags = 0; 332 FILE *file; 333 334 file = fopen(MAX_SKB_FRAGS_PATH, "r"); 335 if (!file) { 336 ksft_print_msg("Error opening %s\n", MAX_SKB_FRAGS_PATH); 337 return 0; 338 } 339 340 if (fscanf(file, "%u", &max_skb_frags) != 1) 341 ksft_print_msg("Error reading %s\n", MAX_SKB_FRAGS_PATH); 342 343 fclose(file); 344 return max_skb_frags; 345 } 346 347 static struct option long_options[] = { 348 {"interface", required_argument, 0, 'i'}, 349 {"busy-poll", no_argument, 0, 'b'}, 350 {"verbose", no_argument, 0, 'v'}, 351 {"mode", required_argument, 0, 'm'}, 352 {"list", no_argument, 0, 'l'}, 353 {"test", required_argument, 0, 't'}, 354 {"help", no_argument, 0, 'h'}, 355 {0, 0, 0, 0} 356 }; 357 358 static void print_usage(char **argv) 359 { 360 const char *str = 361 " Usage: xskxceiver [OPTIONS]\n" 362 " Options:\n" 363 " -i, --interface Use interface\n" 364 " -v, --verbose Verbose output\n" 365 " -b, --busy-poll Enable busy poll\n" 366 " -m, --mode Run only mode skb, drv, or zc\n" 367 " -l, --list List all available tests\n" 368 " -t, --test Run a specific test. Enter number from -l option.\n" 369 " -h, --help Display this help and exit\n"; 370 371 ksft_print_msg(str, basename(argv[0])); 372 ksft_exit_xfail(); 373 } 374 375 static bool validate_interface(struct ifobject *ifobj) 376 { 377 if (!strcmp(ifobj->ifname, "")) 378 return false; 379 return true; 380 } 381 382 static void parse_command_line(struct ifobject *ifobj_tx, struct ifobject *ifobj_rx, int argc, 383 char **argv) 384 { 385 struct ifobject *ifobj; 386 u32 interface_nb = 0; 387 int option_index, c; 388 389 opterr = 0; 390 391 for (;;) { 392 c = getopt_long(argc, argv, "i:vbm:lt:", long_options, &option_index); 393 if (c == -1) 394 break; 395 396 switch (c) { 397 case 'i': 398 if (interface_nb == 0) 399 ifobj = ifobj_tx; 400 else if (interface_nb == 1) 401 ifobj = ifobj_rx; 402 else 403 break; 404 405 memcpy(ifobj->ifname, optarg, 406 min_t(size_t, MAX_INTERFACE_NAME_CHARS, strlen(optarg))); 407 408 ifobj->ifindex = if_nametoindex(ifobj->ifname); 409 if (!ifobj->ifindex) 410 exit_with_error(errno); 411 412 interface_nb++; 413 break; 414 case 'v': 415 opt_verbose = true; 416 break; 417 case 'b': 418 ifobj_tx->busy_poll = true; 419 ifobj_rx->busy_poll = true; 420 break; 421 case 'm': 422 if (!strncmp("skb", optarg, strlen(optarg))) 423 opt_mode = TEST_MODE_SKB; 424 else if (!strncmp("drv", optarg, strlen(optarg))) 425 opt_mode = TEST_MODE_DRV; 426 else if (!strncmp("zc", optarg, strlen(optarg))) 427 opt_mode = TEST_MODE_ZC; 428 else 429 print_usage(argv); 430 break; 431 case 'l': 432 opt_print_tests = true; 433 break; 434 case 't': 435 errno = 0; 436 opt_run_test = strtol(optarg, NULL, 0); 437 if (errno) 438 print_usage(argv); 439 break; 440 case 'h': 441 default: 442 print_usage(argv); 443 } 444 } 445 } 446 447 static int set_ring_size(struct ifobject *ifobj) 448 { 449 int ret; 450 u32 ctr = 0; 451 452 while (ctr++ < SOCK_RECONF_CTR) { 453 ret = set_hw_ring_size(ifobj->ifname, &ifobj->ring); 454 if (!ret) 455 break; 456 457 /* Retry if it fails */ 458 if (ctr >= SOCK_RECONF_CTR || errno != EBUSY) 459 return -errno; 460 461 usleep(USLEEP_MAX); 462 } 463 464 return ret; 465 } 466 467 static int hw_ring_size_reset(struct ifobject *ifobj) 468 { 469 ifobj->ring.tx_pending = ifobj->set_ring.default_tx; 470 ifobj->ring.rx_pending = ifobj->set_ring.default_rx; 471 return set_ring_size(ifobj); 472 } 473 474 static void __test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx, 475 struct ifobject *ifobj_rx) 476 { 477 u32 i, j; 478 479 for (i = 0; i < MAX_INTERFACES; i++) { 480 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx; 481 482 ifobj->xsk = &ifobj->xsk_arr[0]; 483 ifobj->use_poll = false; 484 ifobj->use_fill_ring = true; 485 ifobj->release_rx = true; 486 ifobj->validation_func = NULL; 487 ifobj->use_metadata = false; 488 489 if (i == 0) { 490 ifobj->rx_on = false; 491 ifobj->tx_on = true; 492 } else { 493 ifobj->rx_on = true; 494 ifobj->tx_on = false; 495 } 496 497 memset(ifobj->umem, 0, sizeof(*ifobj->umem)); 498 ifobj->umem->num_frames = DEFAULT_UMEM_BUFFERS; 499 ifobj->umem->frame_size = XSK_UMEM__DEFAULT_FRAME_SIZE; 500 501 for (j = 0; j < MAX_SOCKETS; j++) { 502 memset(&ifobj->xsk_arr[j], 0, sizeof(ifobj->xsk_arr[j])); 503 ifobj->xsk_arr[j].rxqsize = XSK_RING_CONS__DEFAULT_NUM_DESCS; 504 ifobj->xsk_arr[j].batch_size = DEFAULT_BATCH_SIZE; 505 if (i == 0) 506 ifobj->xsk_arr[j].pkt_stream = test->tx_pkt_stream_default; 507 else 508 ifobj->xsk_arr[j].pkt_stream = test->rx_pkt_stream_default; 509 510 memcpy(ifobj->xsk_arr[j].src_mac, g_mac, ETH_ALEN); 511 memcpy(ifobj->xsk_arr[j].dst_mac, g_mac, ETH_ALEN); 512 ifobj->xsk_arr[j].src_mac[5] += ((j * 2) + 0); 513 ifobj->xsk_arr[j].dst_mac[5] += ((j * 2) + 1); 514 } 515 } 516 517 if (ifobj_tx->hw_ring_size_supp) 518 hw_ring_size_reset(ifobj_tx); 519 520 test->ifobj_tx = ifobj_tx; 521 test->ifobj_rx = ifobj_rx; 522 test->current_step = 0; 523 test->total_steps = 1; 524 test->nb_sockets = 1; 525 test->fail = false; 526 test->set_ring = false; 527 test->adjust_tail = false; 528 test->adjust_tail_support = false; 529 test->mtu = MAX_ETH_PKT_SIZE; 530 test->xdp_prog_rx = ifobj_rx->xdp_progs->progs.xsk_def_prog; 531 test->xskmap_rx = ifobj_rx->xdp_progs->maps.xsk; 532 test->xdp_prog_tx = ifobj_tx->xdp_progs->progs.xsk_def_prog; 533 test->xskmap_tx = ifobj_tx->xdp_progs->maps.xsk; 534 } 535 536 static void test_spec_init(struct test_spec *test, struct ifobject *ifobj_tx, 537 struct ifobject *ifobj_rx, enum test_mode mode, 538 const struct test_spec *test_to_run) 539 { 540 struct pkt_stream *tx_pkt_stream; 541 struct pkt_stream *rx_pkt_stream; 542 u32 i; 543 544 tx_pkt_stream = test->tx_pkt_stream_default; 545 rx_pkt_stream = test->rx_pkt_stream_default; 546 memset(test, 0, sizeof(*test)); 547 test->tx_pkt_stream_default = tx_pkt_stream; 548 test->rx_pkt_stream_default = rx_pkt_stream; 549 550 for (i = 0; i < MAX_INTERFACES; i++) { 551 struct ifobject *ifobj = i ? ifobj_rx : ifobj_tx; 552 553 ifobj->bind_flags = XDP_USE_NEED_WAKEUP; 554 if (mode == TEST_MODE_ZC) 555 ifobj->bind_flags |= XDP_ZEROCOPY; 556 else 557 ifobj->bind_flags |= XDP_COPY; 558 } 559 560 strncpy(test->name, test_to_run->name, MAX_TEST_NAME_SIZE); 561 test->test_func = test_to_run->test_func; 562 test->mode = mode; 563 __test_spec_init(test, ifobj_tx, ifobj_rx); 564 } 565 566 static void test_spec_reset(struct test_spec *test) 567 { 568 __test_spec_init(test, test->ifobj_tx, test->ifobj_rx); 569 } 570 571 static void test_spec_set_xdp_prog(struct test_spec *test, struct bpf_program *xdp_prog_rx, 572 struct bpf_program *xdp_prog_tx, struct bpf_map *xskmap_rx, 573 struct bpf_map *xskmap_tx) 574 { 575 test->xdp_prog_rx = xdp_prog_rx; 576 test->xdp_prog_tx = xdp_prog_tx; 577 test->xskmap_rx = xskmap_rx; 578 test->xskmap_tx = xskmap_tx; 579 } 580 581 static int test_spec_set_mtu(struct test_spec *test, int mtu) 582 { 583 int err; 584 585 if (test->ifobj_rx->mtu != mtu) { 586 err = xsk_set_mtu(test->ifobj_rx->ifindex, mtu); 587 if (err) 588 return err; 589 test->ifobj_rx->mtu = mtu; 590 } 591 if (test->ifobj_tx->mtu != mtu) { 592 err = xsk_set_mtu(test->ifobj_tx->ifindex, mtu); 593 if (err) 594 return err; 595 test->ifobj_tx->mtu = mtu; 596 } 597 598 return 0; 599 } 600 601 static void pkt_stream_reset(struct pkt_stream *pkt_stream) 602 { 603 if (pkt_stream) { 604 pkt_stream->current_pkt_nb = 0; 605 pkt_stream->nb_rx_pkts = 0; 606 } 607 } 608 609 static struct pkt *pkt_stream_get_next_tx_pkt(struct pkt_stream *pkt_stream) 610 { 611 if (pkt_stream->current_pkt_nb >= pkt_stream->nb_pkts) 612 return NULL; 613 614 return &pkt_stream->pkts[pkt_stream->current_pkt_nb++]; 615 } 616 617 static struct pkt *pkt_stream_get_next_rx_pkt(struct pkt_stream *pkt_stream, u32 *pkts_sent) 618 { 619 while (pkt_stream->current_pkt_nb < pkt_stream->nb_pkts) { 620 (*pkts_sent)++; 621 if (pkt_stream->pkts[pkt_stream->current_pkt_nb].valid) 622 return &pkt_stream->pkts[pkt_stream->current_pkt_nb++]; 623 pkt_stream->current_pkt_nb++; 624 } 625 return NULL; 626 } 627 628 static void pkt_stream_delete(struct pkt_stream *pkt_stream) 629 { 630 free(pkt_stream->pkts); 631 free(pkt_stream); 632 } 633 634 static void pkt_stream_restore_default(struct test_spec *test) 635 { 636 struct pkt_stream *tx_pkt_stream = test->ifobj_tx->xsk->pkt_stream; 637 struct pkt_stream *rx_pkt_stream = test->ifobj_rx->xsk->pkt_stream; 638 639 if (tx_pkt_stream != test->tx_pkt_stream_default) { 640 pkt_stream_delete(test->ifobj_tx->xsk->pkt_stream); 641 test->ifobj_tx->xsk->pkt_stream = test->tx_pkt_stream_default; 642 } 643 644 if (rx_pkt_stream != test->rx_pkt_stream_default) { 645 pkt_stream_delete(test->ifobj_rx->xsk->pkt_stream); 646 test->ifobj_rx->xsk->pkt_stream = test->rx_pkt_stream_default; 647 } 648 } 649 650 static struct pkt_stream *__pkt_stream_alloc(u32 nb_pkts) 651 { 652 struct pkt_stream *pkt_stream; 653 654 pkt_stream = calloc(1, sizeof(*pkt_stream)); 655 if (!pkt_stream) 656 return NULL; 657 658 pkt_stream->pkts = calloc(nb_pkts, sizeof(*pkt_stream->pkts)); 659 if (!pkt_stream->pkts) { 660 free(pkt_stream); 661 return NULL; 662 } 663 664 pkt_stream->nb_pkts = nb_pkts; 665 return pkt_stream; 666 } 667 668 static bool pkt_continues(u32 options) 669 { 670 return options & XDP_PKT_CONTD; 671 } 672 673 static u32 ceil_u32(u32 a, u32 b) 674 { 675 return (a + b - 1) / b; 676 } 677 678 static u32 pkt_nb_frags(u32 frame_size, struct pkt_stream *pkt_stream, struct pkt *pkt) 679 { 680 u32 nb_frags = 1, next_frag; 681 682 if (!pkt) 683 return 1; 684 685 if (!pkt_stream->verbatim) { 686 if (!pkt->valid || !pkt->len) 687 return 1; 688 return ceil_u32(pkt->len, frame_size); 689 } 690 691 /* Search for the end of the packet in verbatim mode */ 692 if (!pkt_continues(pkt->options)) 693 return nb_frags; 694 695 next_frag = pkt_stream->current_pkt_nb; 696 pkt++; 697 while (next_frag++ < pkt_stream->nb_pkts) { 698 nb_frags++; 699 if (!pkt_continues(pkt->options) || !pkt->valid) 700 break; 701 pkt++; 702 } 703 return nb_frags; 704 } 705 706 static bool set_pkt_valid(int offset, u32 len) 707 { 708 return len <= MAX_ETH_JUMBO_SIZE; 709 } 710 711 static void pkt_set(struct pkt_stream *pkt_stream, struct pkt *pkt, int offset, u32 len) 712 { 713 pkt->offset = offset; 714 pkt->len = len; 715 pkt->valid = set_pkt_valid(offset, len); 716 } 717 718 static void pkt_stream_pkt_set(struct pkt_stream *pkt_stream, struct pkt *pkt, int offset, u32 len) 719 { 720 bool prev_pkt_valid = pkt->valid; 721 722 pkt_set(pkt_stream, pkt, offset, len); 723 pkt_stream->nb_valid_entries += pkt->valid - prev_pkt_valid; 724 } 725 726 static u32 pkt_get_buffer_len(struct xsk_umem_info *umem, u32 len) 727 { 728 return ceil_u32(len, umem->frame_size) * umem->frame_size; 729 } 730 731 static struct pkt_stream *__pkt_stream_generate(u32 nb_pkts, u32 pkt_len, u32 nb_start, u32 nb_off) 732 { 733 struct pkt_stream *pkt_stream; 734 u32 i; 735 736 pkt_stream = __pkt_stream_alloc(nb_pkts); 737 if (!pkt_stream) 738 exit_with_error(ENOMEM); 739 740 pkt_stream->nb_pkts = nb_pkts; 741 pkt_stream->max_pkt_len = pkt_len; 742 for (i = 0; i < nb_pkts; i++) { 743 struct pkt *pkt = &pkt_stream->pkts[i]; 744 745 pkt_stream_pkt_set(pkt_stream, pkt, 0, pkt_len); 746 pkt->pkt_nb = nb_start + i * nb_off; 747 } 748 749 return pkt_stream; 750 } 751 752 static struct pkt_stream *pkt_stream_generate(u32 nb_pkts, u32 pkt_len) 753 { 754 return __pkt_stream_generate(nb_pkts, pkt_len, 0, 1); 755 } 756 757 static struct pkt_stream *pkt_stream_clone(struct pkt_stream *pkt_stream) 758 { 759 return pkt_stream_generate(pkt_stream->nb_pkts, pkt_stream->pkts[0].len); 760 } 761 762 static void pkt_stream_replace_ifobject(struct ifobject *ifobj, u32 nb_pkts, u32 pkt_len) 763 { 764 ifobj->xsk->pkt_stream = pkt_stream_generate(nb_pkts, pkt_len); 765 } 766 767 static void pkt_stream_replace(struct test_spec *test, u32 nb_pkts, u32 pkt_len) 768 { 769 pkt_stream_replace_ifobject(test->ifobj_tx, nb_pkts, pkt_len); 770 pkt_stream_replace_ifobject(test->ifobj_rx, nb_pkts, pkt_len); 771 } 772 773 static void __pkt_stream_replace_half(struct ifobject *ifobj, u32 pkt_len, 774 int offset) 775 { 776 struct pkt_stream *pkt_stream; 777 u32 i; 778 779 pkt_stream = pkt_stream_clone(ifobj->xsk->pkt_stream); 780 for (i = 1; i < ifobj->xsk->pkt_stream->nb_pkts; i += 2) 781 pkt_stream_pkt_set(pkt_stream, &pkt_stream->pkts[i], offset, pkt_len); 782 783 ifobj->xsk->pkt_stream = pkt_stream; 784 } 785 786 static void pkt_stream_replace_half(struct test_spec *test, u32 pkt_len, int offset) 787 { 788 __pkt_stream_replace_half(test->ifobj_tx, pkt_len, offset); 789 __pkt_stream_replace_half(test->ifobj_rx, pkt_len, offset); 790 } 791 792 static void pkt_stream_receive_half(struct test_spec *test) 793 { 794 struct pkt_stream *pkt_stream = test->ifobj_tx->xsk->pkt_stream; 795 u32 i; 796 797 test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(pkt_stream->nb_pkts, 798 pkt_stream->pkts[0].len); 799 pkt_stream = test->ifobj_rx->xsk->pkt_stream; 800 for (i = 1; i < pkt_stream->nb_pkts; i += 2) 801 pkt_stream->pkts[i].valid = false; 802 803 pkt_stream->nb_valid_entries /= 2; 804 } 805 806 static void pkt_stream_even_odd_sequence(struct test_spec *test) 807 { 808 struct pkt_stream *pkt_stream; 809 u32 i; 810 811 for (i = 0; i < test->nb_sockets; i++) { 812 pkt_stream = test->ifobj_tx->xsk_arr[i].pkt_stream; 813 pkt_stream = __pkt_stream_generate(pkt_stream->nb_pkts / 2, 814 pkt_stream->pkts[0].len, i, 2); 815 test->ifobj_tx->xsk_arr[i].pkt_stream = pkt_stream; 816 817 pkt_stream = test->ifobj_rx->xsk_arr[i].pkt_stream; 818 pkt_stream = __pkt_stream_generate(pkt_stream->nb_pkts / 2, 819 pkt_stream->pkts[0].len, i, 2); 820 test->ifobj_rx->xsk_arr[i].pkt_stream = pkt_stream; 821 } 822 } 823 824 static u64 pkt_get_addr(struct pkt *pkt, struct xsk_umem_info *umem) 825 { 826 if (!pkt->valid) 827 return pkt->offset; 828 return pkt->offset + umem_alloc_buffer(umem); 829 } 830 831 static void pkt_stream_cancel(struct pkt_stream *pkt_stream) 832 { 833 pkt_stream->current_pkt_nb--; 834 } 835 836 static void pkt_generate(struct xsk_socket_info *xsk, struct xsk_umem_info *umem, u64 addr, u32 len, 837 u32 pkt_nb, u32 bytes_written) 838 { 839 void *data = xsk_umem__get_data(umem->buffer, addr); 840 841 if (len < MIN_PKT_SIZE) 842 return; 843 844 if (!bytes_written) { 845 gen_eth_hdr(xsk, data); 846 847 len -= PKT_HDR_SIZE; 848 data += PKT_HDR_SIZE; 849 } else { 850 bytes_written -= PKT_HDR_SIZE; 851 } 852 853 write_payload(data, pkt_nb, bytes_written, len); 854 } 855 856 static struct pkt_stream *__pkt_stream_generate_custom(struct ifobject *ifobj, struct pkt *frames, 857 u32 nb_frames, bool verbatim) 858 { 859 u32 i, len = 0, pkt_nb = 0, payload = 0; 860 struct pkt_stream *pkt_stream; 861 862 pkt_stream = __pkt_stream_alloc(nb_frames); 863 if (!pkt_stream) 864 exit_with_error(ENOMEM); 865 866 for (i = 0; i < nb_frames; i++) { 867 struct pkt *pkt = &pkt_stream->pkts[pkt_nb]; 868 struct pkt *frame = &frames[i]; 869 870 pkt->offset = frame->offset; 871 if (verbatim) { 872 *pkt = *frame; 873 pkt->pkt_nb = payload; 874 if (!frame->valid || !pkt_continues(frame->options)) 875 payload++; 876 } else { 877 if (frame->valid) 878 len += frame->len; 879 if (frame->valid && pkt_continues(frame->options)) 880 continue; 881 882 pkt->pkt_nb = pkt_nb; 883 pkt->len = len; 884 pkt->valid = frame->valid; 885 pkt->options = 0; 886 887 len = 0; 888 } 889 890 print_verbose("offset: %d len: %u valid: %u options: %u pkt_nb: %u\n", 891 pkt->offset, pkt->len, pkt->valid, pkt->options, pkt->pkt_nb); 892 893 if (pkt->valid && pkt->len > pkt_stream->max_pkt_len) 894 pkt_stream->max_pkt_len = pkt->len; 895 896 if (pkt->valid) 897 pkt_stream->nb_valid_entries++; 898 899 pkt_nb++; 900 } 901 902 pkt_stream->nb_pkts = pkt_nb; 903 pkt_stream->verbatim = verbatim; 904 return pkt_stream; 905 } 906 907 static void pkt_stream_generate_custom(struct test_spec *test, struct pkt *pkts, u32 nb_pkts) 908 { 909 struct pkt_stream *pkt_stream; 910 911 pkt_stream = __pkt_stream_generate_custom(test->ifobj_tx, pkts, nb_pkts, true); 912 test->ifobj_tx->xsk->pkt_stream = pkt_stream; 913 914 pkt_stream = __pkt_stream_generate_custom(test->ifobj_rx, pkts, nb_pkts, false); 915 test->ifobj_rx->xsk->pkt_stream = pkt_stream; 916 } 917 918 static void pkt_print_data(u32 *data, u32 cnt) 919 { 920 u32 i; 921 922 for (i = 0; i < cnt; i++) { 923 u32 seqnum, pkt_nb; 924 925 seqnum = ntohl(*data) & 0xffff; 926 pkt_nb = ntohl(*data) >> 16; 927 ksft_print_msg("%u:%u ", pkt_nb, seqnum); 928 data++; 929 } 930 } 931 932 static void pkt_dump(void *pkt, u32 len, bool eth_header) 933 { 934 struct ethhdr *ethhdr = pkt; 935 u32 i, *data; 936 937 if (eth_header) { 938 /*extract L2 frame */ 939 ksft_print_msg("DEBUG>> L2: dst mac: "); 940 for (i = 0; i < ETH_ALEN; i++) 941 ksft_print_msg("%02X", ethhdr->h_dest[i]); 942 943 ksft_print_msg("\nDEBUG>> L2: src mac: "); 944 for (i = 0; i < ETH_ALEN; i++) 945 ksft_print_msg("%02X", ethhdr->h_source[i]); 946 947 data = pkt + PKT_HDR_SIZE; 948 } else { 949 data = pkt; 950 } 951 952 /*extract L5 frame */ 953 ksft_print_msg("\nDEBUG>> L5: seqnum: "); 954 pkt_print_data(data, PKT_DUMP_NB_TO_PRINT); 955 ksft_print_msg("...."); 956 if (len > PKT_DUMP_NB_TO_PRINT * sizeof(u32)) { 957 ksft_print_msg("\n.... "); 958 pkt_print_data(data + len / sizeof(u32) - PKT_DUMP_NB_TO_PRINT, 959 PKT_DUMP_NB_TO_PRINT); 960 } 961 ksft_print_msg("\n---------------------------------------\n"); 962 } 963 964 static bool is_offset_correct(struct xsk_umem_info *umem, struct pkt *pkt, u64 addr) 965 { 966 u32 headroom = umem->unaligned_mode ? 0 : umem->frame_headroom; 967 u32 offset = addr % umem->frame_size, expected_offset; 968 int pkt_offset = pkt->valid ? pkt->offset : 0; 969 970 if (!umem->unaligned_mode) 971 pkt_offset = 0; 972 973 expected_offset = (pkt_offset + headroom + XDP_PACKET_HEADROOM) % umem->frame_size; 974 975 if (offset == expected_offset) 976 return true; 977 978 ksft_print_msg("[%s] expected [%u], got [%u]\n", __func__, expected_offset, offset); 979 return false; 980 } 981 982 static bool is_metadata_correct(struct pkt *pkt, void *buffer, u64 addr) 983 { 984 void *data = xsk_umem__get_data(buffer, addr); 985 struct xdp_info *meta = data - sizeof(struct xdp_info); 986 987 if (meta->count != pkt->pkt_nb) { 988 ksft_print_msg("[%s] expected meta_count [%d], got meta_count [%llu]\n", 989 __func__, pkt->pkt_nb, 990 (unsigned long long)meta->count); 991 return false; 992 } 993 994 return true; 995 } 996 997 static bool is_adjust_tail_supported(struct xsk_xdp_progs *skel_rx) 998 { 999 struct bpf_map *data_map; 1000 int adjust_value = 0; 1001 int key = 0; 1002 int ret; 1003 1004 data_map = bpf_object__find_map_by_name(skel_rx->obj, "xsk_xdp_.bss"); 1005 if (!data_map || !bpf_map__is_internal(data_map)) { 1006 ksft_print_msg("Error: could not find bss section of XDP program\n"); 1007 exit_with_error(errno); 1008 } 1009 1010 ret = bpf_map_lookup_elem(bpf_map__fd(data_map), &key, &adjust_value); 1011 if (ret) { 1012 ksft_print_msg("Error: bpf_map_lookup_elem failed with error %d\n", ret); 1013 exit_with_error(errno); 1014 } 1015 1016 /* Set the 'adjust_value' variable to -EOPNOTSUPP in the XDP program if the adjust_tail 1017 * helper is not supported. Skip the adjust_tail test case in this scenario. 1018 */ 1019 return adjust_value != -EOPNOTSUPP; 1020 } 1021 1022 static bool is_frag_valid(struct xsk_umem_info *umem, u64 addr, u32 len, u32 expected_pkt_nb, 1023 u32 bytes_processed) 1024 { 1025 u32 seqnum, pkt_nb, *pkt_data, words_to_end, expected_seqnum; 1026 void *data = xsk_umem__get_data(umem->buffer, addr); 1027 1028 addr -= umem->base_addr; 1029 1030 if (addr >= umem->num_frames * umem->frame_size || 1031 addr + len > umem->num_frames * umem->frame_size) { 1032 ksft_print_msg("Frag invalid addr: %llx len: %u\n", 1033 (unsigned long long)addr, len); 1034 return false; 1035 } 1036 if (!umem->unaligned_mode && addr % umem->frame_size + len > umem->frame_size) { 1037 ksft_print_msg("Frag crosses frame boundary addr: %llx len: %u\n", 1038 (unsigned long long)addr, len); 1039 return false; 1040 } 1041 1042 pkt_data = data; 1043 if (!bytes_processed) { 1044 pkt_data += PKT_HDR_SIZE / sizeof(*pkt_data); 1045 len -= PKT_HDR_SIZE; 1046 } else { 1047 bytes_processed -= PKT_HDR_SIZE; 1048 } 1049 1050 expected_seqnum = bytes_processed / sizeof(*pkt_data); 1051 seqnum = ntohl(*pkt_data) & 0xffff; 1052 pkt_nb = ntohl(*pkt_data) >> 16; 1053 1054 if (expected_pkt_nb != pkt_nb) { 1055 ksft_print_msg("[%s] expected pkt_nb [%u], got pkt_nb [%u]\n", 1056 __func__, expected_pkt_nb, pkt_nb); 1057 goto error; 1058 } 1059 if (expected_seqnum != seqnum) { 1060 ksft_print_msg("[%s] expected seqnum at start [%u], got seqnum [%u]\n", 1061 __func__, expected_seqnum, seqnum); 1062 goto error; 1063 } 1064 1065 words_to_end = len / sizeof(*pkt_data) - 1; 1066 pkt_data += words_to_end; 1067 seqnum = ntohl(*pkt_data) & 0xffff; 1068 expected_seqnum += words_to_end; 1069 if (expected_seqnum != seqnum) { 1070 ksft_print_msg("[%s] expected seqnum at end [%u], got seqnum [%u]\n", 1071 __func__, expected_seqnum, seqnum); 1072 goto error; 1073 } 1074 1075 return true; 1076 1077 error: 1078 pkt_dump(data, len, !bytes_processed); 1079 return false; 1080 } 1081 1082 static bool is_pkt_valid(struct pkt *pkt, void *buffer, u64 addr, u32 len) 1083 { 1084 if (pkt->len != len) { 1085 ksft_print_msg("[%s] expected packet length [%d], got length [%d]\n", 1086 __func__, pkt->len, len); 1087 pkt_dump(xsk_umem__get_data(buffer, addr), len, true); 1088 return false; 1089 } 1090 1091 return true; 1092 } 1093 1094 static int kick_tx(struct xsk_socket_info *xsk) 1095 { 1096 int ret; 1097 1098 ret = sendto(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, 0); 1099 if (ret >= 0) 1100 return TEST_PASS; 1101 if (errno == ENOBUFS || errno == EAGAIN || errno == EBUSY || errno == ENETDOWN) { 1102 usleep(100); 1103 return TEST_PASS; 1104 } 1105 return TEST_FAILURE; 1106 } 1107 1108 static int kick_rx(struct xsk_socket_info *xsk) 1109 { 1110 int ret; 1111 1112 ret = recvfrom(xsk_socket__fd(xsk->xsk), NULL, 0, MSG_DONTWAIT, NULL, NULL); 1113 if (ret < 0) 1114 return TEST_FAILURE; 1115 1116 return TEST_PASS; 1117 } 1118 1119 static int complete_pkts(struct xsk_socket_info *xsk, int batch_size) 1120 { 1121 unsigned int rcvd; 1122 u32 idx; 1123 int ret; 1124 1125 if (xsk_ring_prod__needs_wakeup(&xsk->tx)) { 1126 ret = kick_tx(xsk); 1127 if (ret) 1128 return TEST_FAILURE; 1129 } 1130 1131 rcvd = xsk_ring_cons__peek(&xsk->umem->cq, batch_size, &idx); 1132 if (rcvd) { 1133 if (rcvd > xsk->outstanding_tx) { 1134 u64 addr = *xsk_ring_cons__comp_addr(&xsk->umem->cq, idx + rcvd - 1); 1135 1136 ksft_print_msg("[%s] Too many packets completed\n", __func__); 1137 ksft_print_msg("Last completion address: %llx\n", 1138 (unsigned long long)addr); 1139 return TEST_FAILURE; 1140 } 1141 1142 xsk_ring_cons__release(&xsk->umem->cq, rcvd); 1143 xsk->outstanding_tx -= rcvd; 1144 } 1145 1146 return TEST_PASS; 1147 } 1148 1149 static int __receive_pkts(struct test_spec *test, struct xsk_socket_info *xsk) 1150 { 1151 u32 frags_processed = 0, nb_frags = 0, pkt_len = 0; 1152 u32 idx_rx = 0, idx_fq = 0, rcvd, pkts_sent = 0; 1153 struct pkt_stream *pkt_stream = xsk->pkt_stream; 1154 struct ifobject *ifobj = test->ifobj_rx; 1155 struct xsk_umem_info *umem = xsk->umem; 1156 struct pollfd fds = { }; 1157 struct pkt *pkt; 1158 u64 first_addr = 0; 1159 int ret; 1160 1161 fds.fd = xsk_socket__fd(xsk->xsk); 1162 fds.events = POLLIN; 1163 1164 ret = kick_rx(xsk); 1165 if (ret) 1166 return TEST_FAILURE; 1167 1168 if (ifobj->use_poll) { 1169 ret = poll(&fds, 1, POLL_TMOUT); 1170 if (ret < 0) 1171 return TEST_FAILURE; 1172 1173 if (!ret) { 1174 if (!is_umem_valid(test->ifobj_tx)) 1175 return TEST_PASS; 1176 1177 ksft_print_msg("ERROR: [%s] Poll timed out\n", __func__); 1178 return TEST_CONTINUE; 1179 } 1180 1181 if (!(fds.revents & POLLIN)) 1182 return TEST_CONTINUE; 1183 } 1184 1185 rcvd = xsk_ring_cons__peek(&xsk->rx, xsk->batch_size, &idx_rx); 1186 if (!rcvd) 1187 return TEST_CONTINUE; 1188 1189 if (ifobj->use_fill_ring) { 1190 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq); 1191 while (ret != rcvd) { 1192 if (xsk_ring_prod__needs_wakeup(&umem->fq)) { 1193 ret = poll(&fds, 1, POLL_TMOUT); 1194 if (ret < 0) 1195 return TEST_FAILURE; 1196 } 1197 ret = xsk_ring_prod__reserve(&umem->fq, rcvd, &idx_fq); 1198 } 1199 } 1200 1201 while (frags_processed < rcvd) { 1202 const struct xdp_desc *desc = xsk_ring_cons__rx_desc(&xsk->rx, idx_rx++); 1203 u64 addr = desc->addr, orig; 1204 1205 orig = xsk_umem__extract_addr(addr); 1206 addr = xsk_umem__add_offset_to_addr(addr); 1207 1208 if (!nb_frags) { 1209 pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &pkts_sent); 1210 if (!pkt) { 1211 ksft_print_msg("[%s] received too many packets addr: %lx len %u\n", 1212 __func__, addr, desc->len); 1213 return TEST_FAILURE; 1214 } 1215 } 1216 1217 print_verbose("Rx: addr: %lx len: %u options: %u pkt_nb: %u valid: %u\n", 1218 addr, desc->len, desc->options, pkt->pkt_nb, pkt->valid); 1219 1220 if (!is_frag_valid(umem, addr, desc->len, pkt->pkt_nb, pkt_len) || 1221 !is_offset_correct(umem, pkt, addr) || (ifobj->use_metadata && 1222 !is_metadata_correct(pkt, umem->buffer, addr))) 1223 return TEST_FAILURE; 1224 1225 if (!nb_frags++) 1226 first_addr = addr; 1227 frags_processed++; 1228 pkt_len += desc->len; 1229 if (ifobj->use_fill_ring) 1230 *xsk_ring_prod__fill_addr(&umem->fq, idx_fq++) = orig; 1231 1232 if (pkt_continues(desc->options)) 1233 continue; 1234 1235 /* The complete packet has been received */ 1236 if (!is_pkt_valid(pkt, umem->buffer, first_addr, pkt_len) || 1237 !is_offset_correct(umem, pkt, addr)) 1238 return TEST_FAILURE; 1239 1240 pkt_stream->nb_rx_pkts++; 1241 nb_frags = 0; 1242 pkt_len = 0; 1243 } 1244 1245 if (nb_frags) { 1246 /* In the middle of a packet. Start over from beginning of packet. */ 1247 idx_rx -= nb_frags; 1248 xsk_ring_cons__cancel(&xsk->rx, nb_frags); 1249 if (ifobj->use_fill_ring) { 1250 idx_fq -= nb_frags; 1251 xsk_ring_prod__cancel(&umem->fq, nb_frags); 1252 } 1253 frags_processed -= nb_frags; 1254 } 1255 1256 if (ifobj->use_fill_ring) 1257 xsk_ring_prod__submit(&umem->fq, frags_processed); 1258 if (ifobj->release_rx) 1259 xsk_ring_cons__release(&xsk->rx, frags_processed); 1260 1261 pthread_mutex_lock(&pacing_mutex); 1262 pkts_in_flight -= pkts_sent; 1263 pthread_mutex_unlock(&pacing_mutex); 1264 pkts_sent = 0; 1265 1266 return TEST_CONTINUE; 1267 } 1268 1269 bool all_packets_received(struct test_spec *test, struct xsk_socket_info *xsk, u32 sock_num, 1270 unsigned long *bitmap) 1271 { 1272 struct pkt_stream *pkt_stream = xsk->pkt_stream; 1273 1274 if (!pkt_stream) { 1275 __set_bit(sock_num, bitmap); 1276 return false; 1277 } 1278 1279 if (pkt_stream->nb_rx_pkts == pkt_stream->nb_valid_entries) { 1280 __set_bit(sock_num, bitmap); 1281 if (bitmap_full(bitmap, test->nb_sockets)) 1282 return true; 1283 } 1284 1285 return false; 1286 } 1287 1288 static int receive_pkts(struct test_spec *test) 1289 { 1290 struct timeval tv_end, tv_now, tv_timeout = {THREAD_TMOUT, 0}; 1291 DECLARE_BITMAP(bitmap, test->nb_sockets); 1292 struct xsk_socket_info *xsk; 1293 u32 sock_num = 0; 1294 int res, ret; 1295 1296 ret = gettimeofday(&tv_now, NULL); 1297 if (ret) 1298 exit_with_error(errno); 1299 1300 timeradd(&tv_now, &tv_timeout, &tv_end); 1301 1302 while (1) { 1303 xsk = &test->ifobj_rx->xsk_arr[sock_num]; 1304 1305 if ((all_packets_received(test, xsk, sock_num, bitmap))) 1306 break; 1307 1308 res = __receive_pkts(test, xsk); 1309 if (!(res == TEST_PASS || res == TEST_CONTINUE)) 1310 return res; 1311 1312 ret = gettimeofday(&tv_now, NULL); 1313 if (ret) 1314 exit_with_error(errno); 1315 1316 if (timercmp(&tv_now, &tv_end, >)) { 1317 ksft_print_msg("ERROR: [%s] Receive loop timed out\n", __func__); 1318 return TEST_FAILURE; 1319 } 1320 sock_num = (sock_num + 1) % test->nb_sockets; 1321 } 1322 1323 return TEST_PASS; 1324 } 1325 1326 static int __send_pkts(struct ifobject *ifobject, struct xsk_socket_info *xsk, bool timeout) 1327 { 1328 u32 i, idx = 0, valid_pkts = 0, valid_frags = 0, buffer_len; 1329 struct pkt_stream *pkt_stream = xsk->pkt_stream; 1330 struct xsk_umem_info *umem = ifobject->umem; 1331 bool use_poll = ifobject->use_poll; 1332 struct pollfd fds = { }; 1333 int ret; 1334 1335 buffer_len = pkt_get_buffer_len(umem, pkt_stream->max_pkt_len); 1336 /* pkts_in_flight might be negative if many invalid packets are sent */ 1337 if (pkts_in_flight >= (int)((umem_size(umem) - xsk->batch_size * buffer_len) / 1338 buffer_len)) { 1339 ret = kick_tx(xsk); 1340 if (ret) 1341 return TEST_FAILURE; 1342 return TEST_CONTINUE; 1343 } 1344 1345 fds.fd = xsk_socket__fd(xsk->xsk); 1346 fds.events = POLLOUT; 1347 1348 while (xsk_ring_prod__reserve(&xsk->tx, xsk->batch_size, &idx) < xsk->batch_size) { 1349 if (use_poll) { 1350 ret = poll(&fds, 1, POLL_TMOUT); 1351 if (timeout) { 1352 if (ret < 0) { 1353 ksft_print_msg("ERROR: [%s] Poll error %d\n", 1354 __func__, errno); 1355 return TEST_FAILURE; 1356 } 1357 if (ret == 0) 1358 return TEST_PASS; 1359 break; 1360 } 1361 if (ret <= 0) { 1362 ksft_print_msg("ERROR: [%s] Poll error %d\n", 1363 __func__, errno); 1364 return TEST_FAILURE; 1365 } 1366 } 1367 1368 complete_pkts(xsk, xsk->batch_size); 1369 } 1370 1371 for (i = 0; i < xsk->batch_size; i++) { 1372 struct pkt *pkt = pkt_stream_get_next_tx_pkt(pkt_stream); 1373 u32 nb_frags_left, nb_frags, bytes_written = 0; 1374 1375 if (!pkt) 1376 break; 1377 1378 nb_frags = pkt_nb_frags(umem->frame_size, pkt_stream, pkt); 1379 if (nb_frags > xsk->batch_size - i) { 1380 pkt_stream_cancel(pkt_stream); 1381 xsk_ring_prod__cancel(&xsk->tx, xsk->batch_size - i); 1382 break; 1383 } 1384 nb_frags_left = nb_frags; 1385 1386 while (nb_frags_left--) { 1387 struct xdp_desc *tx_desc = xsk_ring_prod__tx_desc(&xsk->tx, idx + i); 1388 1389 tx_desc->addr = pkt_get_addr(pkt, ifobject->umem); 1390 if (pkt_stream->verbatim) { 1391 tx_desc->len = pkt->len; 1392 tx_desc->options = pkt->options; 1393 } else if (nb_frags_left) { 1394 tx_desc->len = umem->frame_size; 1395 tx_desc->options = XDP_PKT_CONTD; 1396 } else { 1397 tx_desc->len = pkt->len - bytes_written; 1398 tx_desc->options = 0; 1399 } 1400 if (pkt->valid) 1401 pkt_generate(xsk, umem, tx_desc->addr, tx_desc->len, pkt->pkt_nb, 1402 bytes_written); 1403 bytes_written += tx_desc->len; 1404 1405 print_verbose("Tx addr: %llx len: %u options: %u pkt_nb: %u\n", 1406 tx_desc->addr, tx_desc->len, tx_desc->options, pkt->pkt_nb); 1407 1408 if (nb_frags_left) { 1409 i++; 1410 if (pkt_stream->verbatim) 1411 pkt = pkt_stream_get_next_tx_pkt(pkt_stream); 1412 } 1413 } 1414 1415 if (pkt && pkt->valid) { 1416 valid_pkts++; 1417 valid_frags += nb_frags; 1418 } 1419 } 1420 1421 pthread_mutex_lock(&pacing_mutex); 1422 pkts_in_flight += valid_pkts; 1423 pthread_mutex_unlock(&pacing_mutex); 1424 1425 xsk_ring_prod__submit(&xsk->tx, i); 1426 xsk->outstanding_tx += valid_frags; 1427 1428 if (use_poll) { 1429 ret = poll(&fds, 1, POLL_TMOUT); 1430 if (ret <= 0) { 1431 if (ret == 0 && timeout) 1432 return TEST_PASS; 1433 1434 ksft_print_msg("ERROR: [%s] Poll error %d\n", __func__, ret); 1435 return TEST_FAILURE; 1436 } 1437 } 1438 1439 if (!timeout) { 1440 if (complete_pkts(xsk, i)) 1441 return TEST_FAILURE; 1442 1443 usleep(10); 1444 return TEST_PASS; 1445 } 1446 1447 return TEST_CONTINUE; 1448 } 1449 1450 static int wait_for_tx_completion(struct xsk_socket_info *xsk) 1451 { 1452 struct timeval tv_end, tv_now, tv_timeout = {THREAD_TMOUT, 0}; 1453 int ret; 1454 1455 ret = gettimeofday(&tv_now, NULL); 1456 if (ret) 1457 exit_with_error(errno); 1458 timeradd(&tv_now, &tv_timeout, &tv_end); 1459 1460 while (xsk->outstanding_tx) { 1461 ret = gettimeofday(&tv_now, NULL); 1462 if (ret) 1463 exit_with_error(errno); 1464 if (timercmp(&tv_now, &tv_end, >)) { 1465 ksft_print_msg("ERROR: [%s] Transmission loop timed out\n", __func__); 1466 return TEST_FAILURE; 1467 } 1468 1469 complete_pkts(xsk, xsk->batch_size); 1470 } 1471 1472 return TEST_PASS; 1473 } 1474 1475 bool all_packets_sent(struct test_spec *test, unsigned long *bitmap) 1476 { 1477 return bitmap_full(bitmap, test->nb_sockets); 1478 } 1479 1480 static int send_pkts(struct test_spec *test, struct ifobject *ifobject) 1481 { 1482 bool timeout = !is_umem_valid(test->ifobj_rx); 1483 DECLARE_BITMAP(bitmap, test->nb_sockets); 1484 u32 i, ret; 1485 1486 while (!(all_packets_sent(test, bitmap))) { 1487 for (i = 0; i < test->nb_sockets; i++) { 1488 struct pkt_stream *pkt_stream; 1489 1490 pkt_stream = ifobject->xsk_arr[i].pkt_stream; 1491 if (!pkt_stream || pkt_stream->current_pkt_nb >= pkt_stream->nb_pkts) { 1492 __set_bit(i, bitmap); 1493 continue; 1494 } 1495 ret = __send_pkts(ifobject, &ifobject->xsk_arr[i], timeout); 1496 if (ret == TEST_CONTINUE && !test->fail) 1497 continue; 1498 1499 if ((ret || test->fail) && !timeout) 1500 return TEST_FAILURE; 1501 1502 if (ret == TEST_PASS && timeout) 1503 return ret; 1504 1505 ret = wait_for_tx_completion(&ifobject->xsk_arr[i]); 1506 if (ret) 1507 return TEST_FAILURE; 1508 } 1509 } 1510 1511 return TEST_PASS; 1512 } 1513 1514 static int get_xsk_stats(struct xsk_socket *xsk, struct xdp_statistics *stats) 1515 { 1516 int fd = xsk_socket__fd(xsk), err; 1517 socklen_t optlen, expected_len; 1518 1519 optlen = sizeof(*stats); 1520 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, stats, &optlen); 1521 if (err) { 1522 ksft_print_msg("[%s] getsockopt(XDP_STATISTICS) error %u %s\n", 1523 __func__, -err, strerror(-err)); 1524 return TEST_FAILURE; 1525 } 1526 1527 expected_len = sizeof(struct xdp_statistics); 1528 if (optlen != expected_len) { 1529 ksft_print_msg("[%s] getsockopt optlen error. Expected: %u got: %u\n", 1530 __func__, expected_len, optlen); 1531 return TEST_FAILURE; 1532 } 1533 1534 return TEST_PASS; 1535 } 1536 1537 static int validate_rx_dropped(struct ifobject *ifobject) 1538 { 1539 struct xsk_socket *xsk = ifobject->xsk->xsk; 1540 struct xdp_statistics stats; 1541 int err; 1542 1543 err = kick_rx(ifobject->xsk); 1544 if (err) 1545 return TEST_FAILURE; 1546 1547 err = get_xsk_stats(xsk, &stats); 1548 if (err) 1549 return TEST_FAILURE; 1550 1551 /* The receiver calls getsockopt after receiving the last (valid) 1552 * packet which is not the final packet sent in this test (valid and 1553 * invalid packets are sent in alternating fashion with the final 1554 * packet being invalid). Since the last packet may or may not have 1555 * been dropped already, both outcomes must be allowed. 1556 */ 1557 if (stats.rx_dropped == ifobject->xsk->pkt_stream->nb_pkts / 2 || 1558 stats.rx_dropped == ifobject->xsk->pkt_stream->nb_pkts / 2 - 1) 1559 return TEST_PASS; 1560 1561 return TEST_FAILURE; 1562 } 1563 1564 static int validate_rx_full(struct ifobject *ifobject) 1565 { 1566 struct xsk_socket *xsk = ifobject->xsk->xsk; 1567 struct xdp_statistics stats; 1568 int err; 1569 1570 usleep(1000); 1571 err = kick_rx(ifobject->xsk); 1572 if (err) 1573 return TEST_FAILURE; 1574 1575 err = get_xsk_stats(xsk, &stats); 1576 if (err) 1577 return TEST_FAILURE; 1578 1579 if (stats.rx_ring_full) 1580 return TEST_PASS; 1581 1582 return TEST_FAILURE; 1583 } 1584 1585 static int validate_fill_empty(struct ifobject *ifobject) 1586 { 1587 struct xsk_socket *xsk = ifobject->xsk->xsk; 1588 struct xdp_statistics stats; 1589 int err; 1590 1591 usleep(1000); 1592 err = kick_rx(ifobject->xsk); 1593 if (err) 1594 return TEST_FAILURE; 1595 1596 err = get_xsk_stats(xsk, &stats); 1597 if (err) 1598 return TEST_FAILURE; 1599 1600 if (stats.rx_fill_ring_empty_descs) 1601 return TEST_PASS; 1602 1603 return TEST_FAILURE; 1604 } 1605 1606 static int validate_tx_invalid_descs(struct ifobject *ifobject) 1607 { 1608 struct xsk_socket *xsk = ifobject->xsk->xsk; 1609 int fd = xsk_socket__fd(xsk); 1610 struct xdp_statistics stats; 1611 socklen_t optlen; 1612 int err; 1613 1614 optlen = sizeof(stats); 1615 err = getsockopt(fd, SOL_XDP, XDP_STATISTICS, &stats, &optlen); 1616 if (err) { 1617 ksft_print_msg("[%s] getsockopt(XDP_STATISTICS) error %u %s\n", 1618 __func__, -err, strerror(-err)); 1619 return TEST_FAILURE; 1620 } 1621 1622 if (stats.tx_invalid_descs != ifobject->xsk->pkt_stream->nb_pkts / 2) { 1623 ksft_print_msg("[%s] tx_invalid_descs incorrect. Got [%llu] expected [%u]\n", 1624 __func__, 1625 (unsigned long long)stats.tx_invalid_descs, 1626 ifobject->xsk->pkt_stream->nb_pkts); 1627 return TEST_FAILURE; 1628 } 1629 1630 return TEST_PASS; 1631 } 1632 1633 static void xsk_configure_socket(struct test_spec *test, struct ifobject *ifobject, 1634 struct xsk_umem_info *umem, bool tx) 1635 { 1636 int i, ret; 1637 1638 for (i = 0; i < test->nb_sockets; i++) { 1639 bool shared = (ifobject->shared_umem && tx) ? true : !!i; 1640 u32 ctr = 0; 1641 1642 while (ctr++ < SOCK_RECONF_CTR) { 1643 ret = __xsk_configure_socket(&ifobject->xsk_arr[i], umem, 1644 ifobject, shared); 1645 if (!ret) 1646 break; 1647 1648 /* Retry if it fails as xsk_socket__create() is asynchronous */ 1649 if (ctr >= SOCK_RECONF_CTR) 1650 exit_with_error(-ret); 1651 usleep(USLEEP_MAX); 1652 } 1653 if (ifobject->busy_poll) 1654 enable_busy_poll(&ifobject->xsk_arr[i]); 1655 } 1656 } 1657 1658 static void thread_common_ops_tx(struct test_spec *test, struct ifobject *ifobject) 1659 { 1660 xsk_configure_socket(test, ifobject, test->ifobj_rx->umem, true); 1661 ifobject->xsk = &ifobject->xsk_arr[0]; 1662 ifobject->xskmap = test->ifobj_rx->xskmap; 1663 memcpy(ifobject->umem, test->ifobj_rx->umem, sizeof(struct xsk_umem_info)); 1664 ifobject->umem->base_addr = 0; 1665 } 1666 1667 static void xsk_populate_fill_ring(struct xsk_umem_info *umem, struct pkt_stream *pkt_stream, 1668 bool fill_up) 1669 { 1670 u32 rx_frame_size = umem->frame_size - XDP_PACKET_HEADROOM; 1671 u32 idx = 0, filled = 0, buffers_to_fill, nb_pkts; 1672 int ret; 1673 1674 if (umem->num_frames < XSK_RING_PROD__DEFAULT_NUM_DESCS) 1675 buffers_to_fill = umem->num_frames; 1676 else 1677 buffers_to_fill = umem->fill_size; 1678 1679 ret = xsk_ring_prod__reserve(&umem->fq, buffers_to_fill, &idx); 1680 if (ret != buffers_to_fill) 1681 exit_with_error(ENOSPC); 1682 1683 while (filled < buffers_to_fill) { 1684 struct pkt *pkt = pkt_stream_get_next_rx_pkt(pkt_stream, &nb_pkts); 1685 u64 addr; 1686 u32 i; 1687 1688 for (i = 0; i < pkt_nb_frags(rx_frame_size, pkt_stream, pkt); i++) { 1689 if (!pkt) { 1690 if (!fill_up) 1691 break; 1692 addr = filled * umem->frame_size + umem->base_addr; 1693 } else if (pkt->offset >= 0) { 1694 addr = pkt->offset % umem->frame_size + umem_alloc_buffer(umem); 1695 } else { 1696 addr = pkt->offset + umem_alloc_buffer(umem); 1697 } 1698 1699 *xsk_ring_prod__fill_addr(&umem->fq, idx++) = addr; 1700 if (++filled >= buffers_to_fill) 1701 break; 1702 } 1703 } 1704 xsk_ring_prod__submit(&umem->fq, filled); 1705 xsk_ring_prod__cancel(&umem->fq, buffers_to_fill - filled); 1706 1707 pkt_stream_reset(pkt_stream); 1708 umem_reset_alloc(umem); 1709 } 1710 1711 static void thread_common_ops(struct test_spec *test, struct ifobject *ifobject) 1712 { 1713 u64 umem_sz = ifobject->umem->num_frames * ifobject->umem->frame_size; 1714 int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE; 1715 LIBBPF_OPTS(bpf_xdp_query_opts, opts); 1716 void *bufs; 1717 int ret; 1718 u32 i; 1719 1720 if (ifobject->umem->unaligned_mode) 1721 mmap_flags |= MAP_HUGETLB | MAP_HUGE_2MB; 1722 1723 if (ifobject->shared_umem) 1724 umem_sz *= 2; 1725 1726 bufs = mmap(NULL, umem_sz, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); 1727 if (bufs == MAP_FAILED) 1728 exit_with_error(errno); 1729 1730 ret = xsk_configure_umem(ifobject, ifobject->umem, bufs, umem_sz); 1731 if (ret) 1732 exit_with_error(-ret); 1733 1734 xsk_configure_socket(test, ifobject, ifobject->umem, false); 1735 1736 ifobject->xsk = &ifobject->xsk_arr[0]; 1737 1738 if (!ifobject->rx_on) 1739 return; 1740 1741 xsk_populate_fill_ring(ifobject->umem, ifobject->xsk->pkt_stream, ifobject->use_fill_ring); 1742 1743 for (i = 0; i < test->nb_sockets; i++) { 1744 ifobject->xsk = &ifobject->xsk_arr[i]; 1745 ret = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk, i); 1746 if (ret) 1747 exit_with_error(errno); 1748 } 1749 } 1750 1751 static void *worker_testapp_validate_tx(void *arg) 1752 { 1753 struct test_spec *test = (struct test_spec *)arg; 1754 struct ifobject *ifobject = test->ifobj_tx; 1755 int err; 1756 1757 if (test->current_step == 1) { 1758 if (!ifobject->shared_umem) 1759 thread_common_ops(test, ifobject); 1760 else 1761 thread_common_ops_tx(test, ifobject); 1762 } 1763 1764 err = send_pkts(test, ifobject); 1765 1766 if (!err && ifobject->validation_func) 1767 err = ifobject->validation_func(ifobject); 1768 if (err) 1769 report_failure(test); 1770 1771 pthread_exit(NULL); 1772 } 1773 1774 static void *worker_testapp_validate_rx(void *arg) 1775 { 1776 struct test_spec *test = (struct test_spec *)arg; 1777 struct ifobject *ifobject = test->ifobj_rx; 1778 int err; 1779 1780 if (test->current_step == 1) { 1781 thread_common_ops(test, ifobject); 1782 } else { 1783 xsk_clear_xskmap(ifobject->xskmap); 1784 err = xsk_update_xskmap(ifobject->xskmap, ifobject->xsk->xsk, 0); 1785 if (err) { 1786 ksft_print_msg("Error: Failed to update xskmap, error %s\n", 1787 strerror(-err)); 1788 exit_with_error(-err); 1789 } 1790 } 1791 1792 pthread_barrier_wait(&barr); 1793 1794 err = receive_pkts(test); 1795 1796 if (!err && ifobject->validation_func) 1797 err = ifobject->validation_func(ifobject); 1798 1799 if (err) { 1800 if (test->adjust_tail && !is_adjust_tail_supported(ifobject->xdp_progs)) 1801 test->adjust_tail_support = false; 1802 else 1803 report_failure(test); 1804 } 1805 1806 pthread_exit(NULL); 1807 } 1808 1809 static u64 ceil_u64(u64 a, u64 b) 1810 { 1811 return (a + b - 1) / b; 1812 } 1813 1814 static void testapp_clean_xsk_umem(struct ifobject *ifobj) 1815 { 1816 u64 umem_sz = ifobj->umem->num_frames * ifobj->umem->frame_size; 1817 1818 if (ifobj->shared_umem) 1819 umem_sz *= 2; 1820 1821 umem_sz = ceil_u64(umem_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE; 1822 xsk_umem__delete(ifobj->umem->umem); 1823 munmap(ifobj->umem->buffer, umem_sz); 1824 } 1825 1826 static void handler(int signum) 1827 { 1828 pthread_exit(NULL); 1829 } 1830 1831 static bool xdp_prog_changed_rx(struct test_spec *test) 1832 { 1833 struct ifobject *ifobj = test->ifobj_rx; 1834 1835 return ifobj->xdp_prog != test->xdp_prog_rx || ifobj->mode != test->mode; 1836 } 1837 1838 static bool xdp_prog_changed_tx(struct test_spec *test) 1839 { 1840 struct ifobject *ifobj = test->ifobj_tx; 1841 1842 return ifobj->xdp_prog != test->xdp_prog_tx || ifobj->mode != test->mode; 1843 } 1844 1845 static void xsk_reattach_xdp(struct ifobject *ifobj, struct bpf_program *xdp_prog, 1846 struct bpf_map *xskmap, enum test_mode mode) 1847 { 1848 int err; 1849 1850 xsk_detach_xdp_program(ifobj->ifindex, mode_to_xdp_flags(ifobj->mode)); 1851 err = xsk_attach_xdp_program(xdp_prog, ifobj->ifindex, mode_to_xdp_flags(mode)); 1852 if (err) { 1853 ksft_print_msg("Error attaching XDP program\n"); 1854 exit_with_error(-err); 1855 } 1856 1857 if (ifobj->mode != mode && (mode == TEST_MODE_DRV || mode == TEST_MODE_ZC)) 1858 if (!xsk_is_in_mode(ifobj->ifindex, XDP_FLAGS_DRV_MODE)) { 1859 ksft_print_msg("ERROR: XDP prog not in DRV mode\n"); 1860 exit_with_error(EINVAL); 1861 } 1862 1863 ifobj->xdp_prog = xdp_prog; 1864 ifobj->xskmap = xskmap; 1865 ifobj->mode = mode; 1866 } 1867 1868 static void xsk_attach_xdp_progs(struct test_spec *test, struct ifobject *ifobj_rx, 1869 struct ifobject *ifobj_tx) 1870 { 1871 if (xdp_prog_changed_rx(test)) 1872 xsk_reattach_xdp(ifobj_rx, test->xdp_prog_rx, test->xskmap_rx, test->mode); 1873 1874 if (!ifobj_tx || ifobj_tx->shared_umem) 1875 return; 1876 1877 if (xdp_prog_changed_tx(test)) 1878 xsk_reattach_xdp(ifobj_tx, test->xdp_prog_tx, test->xskmap_tx, test->mode); 1879 } 1880 1881 static int __testapp_validate_traffic(struct test_spec *test, struct ifobject *ifobj1, 1882 struct ifobject *ifobj2) 1883 { 1884 pthread_t t0, t1; 1885 int err; 1886 1887 if (test->mtu > MAX_ETH_PKT_SIZE) { 1888 if (test->mode == TEST_MODE_ZC && (!ifobj1->multi_buff_zc_supp || 1889 (ifobj2 && !ifobj2->multi_buff_zc_supp))) { 1890 ksft_test_result_skip("Multi buffer for zero-copy not supported.\n"); 1891 return TEST_SKIP; 1892 } 1893 if (test->mode != TEST_MODE_ZC && (!ifobj1->multi_buff_supp || 1894 (ifobj2 && !ifobj2->multi_buff_supp))) { 1895 ksft_test_result_skip("Multi buffer not supported.\n"); 1896 return TEST_SKIP; 1897 } 1898 } 1899 err = test_spec_set_mtu(test, test->mtu); 1900 if (err) { 1901 ksft_print_msg("Error, could not set mtu.\n"); 1902 exit_with_error(err); 1903 } 1904 1905 if (ifobj2) { 1906 if (pthread_barrier_init(&barr, NULL, 2)) 1907 exit_with_error(errno); 1908 pkt_stream_reset(ifobj2->xsk->pkt_stream); 1909 } 1910 1911 test->current_step++; 1912 pkt_stream_reset(ifobj1->xsk->pkt_stream); 1913 pkts_in_flight = 0; 1914 1915 signal(SIGUSR1, handler); 1916 /*Spawn RX thread */ 1917 pthread_create(&t0, NULL, ifobj1->func_ptr, test); 1918 1919 if (ifobj2) { 1920 pthread_barrier_wait(&barr); 1921 if (pthread_barrier_destroy(&barr)) 1922 exit_with_error(errno); 1923 1924 /*Spawn TX thread */ 1925 pthread_create(&t1, NULL, ifobj2->func_ptr, test); 1926 1927 pthread_join(t1, NULL); 1928 } 1929 1930 if (!ifobj2) 1931 pthread_kill(t0, SIGUSR1); 1932 else 1933 pthread_join(t0, NULL); 1934 1935 if (test->total_steps == test->current_step || test->fail) { 1936 u32 i; 1937 1938 if (ifobj2) 1939 for (i = 0; i < test->nb_sockets; i++) 1940 xsk_socket__delete(ifobj2->xsk_arr[i].xsk); 1941 1942 for (i = 0; i < test->nb_sockets; i++) 1943 xsk_socket__delete(ifobj1->xsk_arr[i].xsk); 1944 1945 testapp_clean_xsk_umem(ifobj1); 1946 if (ifobj2 && !ifobj2->shared_umem) 1947 testapp_clean_xsk_umem(ifobj2); 1948 } 1949 1950 return !!test->fail; 1951 } 1952 1953 static int testapp_validate_traffic(struct test_spec *test) 1954 { 1955 struct ifobject *ifobj_rx = test->ifobj_rx; 1956 struct ifobject *ifobj_tx = test->ifobj_tx; 1957 1958 if ((ifobj_rx->umem->unaligned_mode && !ifobj_rx->unaligned_supp) || 1959 (ifobj_tx->umem->unaligned_mode && !ifobj_tx->unaligned_supp)) { 1960 ksft_test_result_skip("No huge pages present.\n"); 1961 return TEST_SKIP; 1962 } 1963 1964 if (test->set_ring) { 1965 if (ifobj_tx->hw_ring_size_supp) { 1966 if (set_ring_size(ifobj_tx)) { 1967 ksft_test_result_skip("Failed to change HW ring size.\n"); 1968 return TEST_FAILURE; 1969 } 1970 } else { 1971 ksft_test_result_skip("Changing HW ring size not supported.\n"); 1972 return TEST_SKIP; 1973 } 1974 } 1975 1976 xsk_attach_xdp_progs(test, ifobj_rx, ifobj_tx); 1977 return __testapp_validate_traffic(test, ifobj_rx, ifobj_tx); 1978 } 1979 1980 static int testapp_validate_traffic_single_thread(struct test_spec *test, struct ifobject *ifobj) 1981 { 1982 return __testapp_validate_traffic(test, ifobj, NULL); 1983 } 1984 1985 static int testapp_teardown(struct test_spec *test) 1986 { 1987 int i; 1988 1989 for (i = 0; i < MAX_TEARDOWN_ITER; i++) { 1990 if (testapp_validate_traffic(test)) 1991 return TEST_FAILURE; 1992 test_spec_reset(test); 1993 } 1994 1995 return TEST_PASS; 1996 } 1997 1998 static void swap_directions(struct ifobject **ifobj1, struct ifobject **ifobj2) 1999 { 2000 thread_func_t tmp_func_ptr = (*ifobj1)->func_ptr; 2001 struct ifobject *tmp_ifobj = (*ifobj1); 2002 2003 (*ifobj1)->func_ptr = (*ifobj2)->func_ptr; 2004 (*ifobj2)->func_ptr = tmp_func_ptr; 2005 2006 *ifobj1 = *ifobj2; 2007 *ifobj2 = tmp_ifobj; 2008 } 2009 2010 static int testapp_bidirectional(struct test_spec *test) 2011 { 2012 int res; 2013 2014 test->ifobj_tx->rx_on = true; 2015 test->ifobj_rx->tx_on = true; 2016 test->total_steps = 2; 2017 if (testapp_validate_traffic(test)) 2018 return TEST_FAILURE; 2019 2020 print_verbose("Switching Tx/Rx direction\n"); 2021 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 2022 res = __testapp_validate_traffic(test, test->ifobj_rx, test->ifobj_tx); 2023 2024 swap_directions(&test->ifobj_rx, &test->ifobj_tx); 2025 return res; 2026 } 2027 2028 static int swap_xsk_resources(struct test_spec *test) 2029 { 2030 int ret; 2031 2032 test->ifobj_tx->xsk_arr[0].pkt_stream = NULL; 2033 test->ifobj_rx->xsk_arr[0].pkt_stream = NULL; 2034 test->ifobj_tx->xsk_arr[1].pkt_stream = test->tx_pkt_stream_default; 2035 test->ifobj_rx->xsk_arr[1].pkt_stream = test->rx_pkt_stream_default; 2036 test->ifobj_tx->xsk = &test->ifobj_tx->xsk_arr[1]; 2037 test->ifobj_rx->xsk = &test->ifobj_rx->xsk_arr[1]; 2038 2039 ret = xsk_update_xskmap(test->ifobj_rx->xskmap, test->ifobj_rx->xsk->xsk, 0); 2040 if (ret) 2041 return TEST_FAILURE; 2042 2043 return TEST_PASS; 2044 } 2045 2046 static int testapp_xdp_prog_cleanup(struct test_spec *test) 2047 { 2048 test->total_steps = 2; 2049 test->nb_sockets = 2; 2050 if (testapp_validate_traffic(test)) 2051 return TEST_FAILURE; 2052 2053 if (swap_xsk_resources(test)) 2054 return TEST_FAILURE; 2055 return testapp_validate_traffic(test); 2056 } 2057 2058 static int testapp_headroom(struct test_spec *test) 2059 { 2060 test->ifobj_rx->umem->frame_headroom = UMEM_HEADROOM_TEST_SIZE; 2061 return testapp_validate_traffic(test); 2062 } 2063 2064 static int testapp_stats_rx_dropped(struct test_spec *test) 2065 { 2066 if (test->mode == TEST_MODE_ZC) { 2067 ksft_test_result_skip("Can not run RX_DROPPED test for ZC mode\n"); 2068 return TEST_SKIP; 2069 } 2070 2071 pkt_stream_replace_half(test, MIN_PKT_SIZE * 4, 0); 2072 test->ifobj_rx->umem->frame_headroom = test->ifobj_rx->umem->frame_size - 2073 XDP_PACKET_HEADROOM - MIN_PKT_SIZE * 3; 2074 pkt_stream_receive_half(test); 2075 test->ifobj_rx->validation_func = validate_rx_dropped; 2076 return testapp_validate_traffic(test); 2077 } 2078 2079 static int testapp_stats_tx_invalid_descs(struct test_spec *test) 2080 { 2081 pkt_stream_replace_half(test, XSK_UMEM__INVALID_FRAME_SIZE, 0); 2082 test->ifobj_tx->validation_func = validate_tx_invalid_descs; 2083 return testapp_validate_traffic(test); 2084 } 2085 2086 static int testapp_stats_rx_full(struct test_spec *test) 2087 { 2088 pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE); 2089 test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE); 2090 2091 test->ifobj_rx->xsk->rxqsize = DEFAULT_UMEM_BUFFERS; 2092 test->ifobj_rx->release_rx = false; 2093 test->ifobj_rx->validation_func = validate_rx_full; 2094 return testapp_validate_traffic(test); 2095 } 2096 2097 static int testapp_stats_fill_empty(struct test_spec *test) 2098 { 2099 pkt_stream_replace(test, DEFAULT_UMEM_BUFFERS + DEFAULT_UMEM_BUFFERS / 2, MIN_PKT_SIZE); 2100 test->ifobj_rx->xsk->pkt_stream = pkt_stream_generate(DEFAULT_UMEM_BUFFERS, MIN_PKT_SIZE); 2101 2102 test->ifobj_rx->use_fill_ring = false; 2103 test->ifobj_rx->validation_func = validate_fill_empty; 2104 return testapp_validate_traffic(test); 2105 } 2106 2107 static int testapp_send_receive_unaligned(struct test_spec *test) 2108 { 2109 test->ifobj_tx->umem->unaligned_mode = true; 2110 test->ifobj_rx->umem->unaligned_mode = true; 2111 /* Let half of the packets straddle a 4K buffer boundary */ 2112 pkt_stream_replace_half(test, MIN_PKT_SIZE, -MIN_PKT_SIZE / 2); 2113 2114 return testapp_validate_traffic(test); 2115 } 2116 2117 static int testapp_send_receive_unaligned_mb(struct test_spec *test) 2118 { 2119 test->mtu = MAX_ETH_JUMBO_SIZE; 2120 test->ifobj_tx->umem->unaligned_mode = true; 2121 test->ifobj_rx->umem->unaligned_mode = true; 2122 pkt_stream_replace(test, DEFAULT_PKT_CNT, MAX_ETH_JUMBO_SIZE); 2123 return testapp_validate_traffic(test); 2124 } 2125 2126 static int testapp_single_pkt(struct test_spec *test) 2127 { 2128 struct pkt pkts[] = {{0, MIN_PKT_SIZE, 0, true}}; 2129 2130 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 2131 return testapp_validate_traffic(test); 2132 } 2133 2134 static int testapp_send_receive_mb(struct test_spec *test) 2135 { 2136 test->mtu = MAX_ETH_JUMBO_SIZE; 2137 pkt_stream_replace(test, DEFAULT_PKT_CNT, MAX_ETH_JUMBO_SIZE); 2138 2139 return testapp_validate_traffic(test); 2140 } 2141 2142 static int testapp_invalid_desc_mb(struct test_spec *test) 2143 { 2144 struct xsk_umem_info *umem = test->ifobj_tx->umem; 2145 u64 umem_size = umem->num_frames * umem->frame_size; 2146 struct pkt pkts[] = { 2147 /* Valid packet for synch to start with */ 2148 {0, MIN_PKT_SIZE, 0, true, 0}, 2149 /* Zero frame len is not legal */ 2150 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2151 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2152 {0, 0, 0, false, 0}, 2153 /* Invalid address in the second frame */ 2154 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2155 {umem_size, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2156 /* Invalid len in the middle */ 2157 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2158 {0, XSK_UMEM__INVALID_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2159 /* Invalid options in the middle */ 2160 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2161 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XSK_DESC__INVALID_OPTION}, 2162 /* Transmit 2 frags, receive 3 */ 2163 {0, XSK_UMEM__MAX_FRAME_SIZE, 0, true, XDP_PKT_CONTD}, 2164 {0, XSK_UMEM__MAX_FRAME_SIZE, 0, true, 0}, 2165 /* Middle frame crosses chunk boundary with small length */ 2166 {0, XSK_UMEM__LARGE_FRAME_SIZE, 0, false, XDP_PKT_CONTD}, 2167 {-MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false, 0}, 2168 /* Valid packet for synch so that something is received */ 2169 {0, MIN_PKT_SIZE, 0, true, 0}}; 2170 2171 if (umem->unaligned_mode) { 2172 /* Crossing a chunk boundary allowed */ 2173 pkts[12].valid = true; 2174 pkts[13].valid = true; 2175 } 2176 2177 test->mtu = MAX_ETH_JUMBO_SIZE; 2178 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 2179 return testapp_validate_traffic(test); 2180 } 2181 2182 static int testapp_invalid_desc(struct test_spec *test) 2183 { 2184 struct xsk_umem_info *umem = test->ifobj_tx->umem; 2185 u64 umem_size = umem->num_frames * umem->frame_size; 2186 struct pkt pkts[] = { 2187 /* Zero packet address allowed */ 2188 {0, MIN_PKT_SIZE, 0, true}, 2189 /* Allowed packet */ 2190 {0, MIN_PKT_SIZE, 0, true}, 2191 /* Straddling the start of umem */ 2192 {-2, MIN_PKT_SIZE, 0, false}, 2193 /* Packet too large */ 2194 {0, XSK_UMEM__INVALID_FRAME_SIZE, 0, false}, 2195 /* Up to end of umem allowed */ 2196 {umem_size - MIN_PKT_SIZE - 2 * umem->frame_size, MIN_PKT_SIZE, 0, true}, 2197 /* After umem ends */ 2198 {umem_size, MIN_PKT_SIZE, 0, false}, 2199 /* Straddle the end of umem */ 2200 {umem_size - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false}, 2201 /* Straddle a 4K boundary */ 2202 {0x1000 - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, false}, 2203 /* Straddle a 2K boundary */ 2204 {0x800 - MIN_PKT_SIZE / 2, MIN_PKT_SIZE, 0, true}, 2205 /* Valid packet for synch so that something is received */ 2206 {0, MIN_PKT_SIZE, 0, true}}; 2207 2208 if (umem->unaligned_mode) { 2209 /* Crossing a page boundary allowed */ 2210 pkts[7].valid = true; 2211 } 2212 if (umem->frame_size == XSK_UMEM__DEFAULT_FRAME_SIZE / 2) { 2213 /* Crossing a 2K frame size boundary not allowed */ 2214 pkts[8].valid = false; 2215 } 2216 2217 if (test->ifobj_tx->shared_umem) { 2218 pkts[4].offset += umem_size; 2219 pkts[5].offset += umem_size; 2220 pkts[6].offset += umem_size; 2221 } 2222 2223 pkt_stream_generate_custom(test, pkts, ARRAY_SIZE(pkts)); 2224 return testapp_validate_traffic(test); 2225 } 2226 2227 static int testapp_xdp_drop(struct test_spec *test) 2228 { 2229 struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs; 2230 struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs; 2231 2232 test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_drop, skel_tx->progs.xsk_xdp_drop, 2233 skel_rx->maps.xsk, skel_tx->maps.xsk); 2234 2235 pkt_stream_receive_half(test); 2236 return testapp_validate_traffic(test); 2237 } 2238 2239 static int testapp_xdp_metadata_copy(struct test_spec *test) 2240 { 2241 struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs; 2242 struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs; 2243 struct bpf_map *data_map; 2244 int count = 0; 2245 int key = 0; 2246 2247 test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_populate_metadata, 2248 skel_tx->progs.xsk_xdp_populate_metadata, 2249 skel_rx->maps.xsk, skel_tx->maps.xsk); 2250 test->ifobj_rx->use_metadata = true; 2251 2252 data_map = bpf_object__find_map_by_name(skel_rx->obj, "xsk_xdp_.bss"); 2253 if (!data_map || !bpf_map__is_internal(data_map)) { 2254 ksft_print_msg("Error: could not find bss section of XDP program\n"); 2255 return TEST_FAILURE; 2256 } 2257 2258 if (bpf_map_update_elem(bpf_map__fd(data_map), &key, &count, BPF_ANY)) { 2259 ksft_print_msg("Error: could not update count element\n"); 2260 return TEST_FAILURE; 2261 } 2262 2263 return testapp_validate_traffic(test); 2264 } 2265 2266 static int testapp_xdp_shared_umem(struct test_spec *test) 2267 { 2268 struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs; 2269 struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs; 2270 2271 test->total_steps = 1; 2272 test->nb_sockets = 2; 2273 2274 test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_shared_umem, 2275 skel_tx->progs.xsk_xdp_shared_umem, 2276 skel_rx->maps.xsk, skel_tx->maps.xsk); 2277 2278 pkt_stream_even_odd_sequence(test); 2279 2280 return testapp_validate_traffic(test); 2281 } 2282 2283 static int testapp_poll_txq_tmout(struct test_spec *test) 2284 { 2285 test->ifobj_tx->use_poll = true; 2286 /* create invalid frame by set umem frame_size and pkt length equal to 2048 */ 2287 test->ifobj_tx->umem->frame_size = 2048; 2288 pkt_stream_replace(test, 2 * DEFAULT_PKT_CNT, 2048); 2289 return testapp_validate_traffic_single_thread(test, test->ifobj_tx); 2290 } 2291 2292 static int testapp_poll_rxq_tmout(struct test_spec *test) 2293 { 2294 test->ifobj_rx->use_poll = true; 2295 return testapp_validate_traffic_single_thread(test, test->ifobj_rx); 2296 } 2297 2298 static int testapp_too_many_frags(struct test_spec *test) 2299 { 2300 struct pkt *pkts; 2301 u32 max_frags, i; 2302 int ret; 2303 2304 if (test->mode == TEST_MODE_ZC) { 2305 max_frags = test->ifobj_tx->xdp_zc_max_segs; 2306 } else { 2307 max_frags = get_max_skb_frags(); 2308 if (!max_frags) { 2309 ksft_print_msg("Couldn't retrieve MAX_SKB_FRAGS from system, using default (17) value\n"); 2310 max_frags = 17; 2311 } 2312 max_frags += 1; 2313 } 2314 2315 pkts = calloc(2 * max_frags + 2, sizeof(struct pkt)); 2316 if (!pkts) 2317 return TEST_FAILURE; 2318 2319 test->mtu = MAX_ETH_JUMBO_SIZE; 2320 2321 /* Valid packet for synch */ 2322 pkts[0].len = MIN_PKT_SIZE; 2323 pkts[0].valid = true; 2324 2325 /* One valid packet with the max amount of frags */ 2326 for (i = 1; i < max_frags + 1; i++) { 2327 pkts[i].len = MIN_PKT_SIZE; 2328 pkts[i].options = XDP_PKT_CONTD; 2329 pkts[i].valid = true; 2330 } 2331 pkts[max_frags].options = 0; 2332 2333 /* An invalid packet with the max amount of frags but signals packet 2334 * continues on the last frag 2335 */ 2336 for (i = max_frags + 1; i < 2 * max_frags + 1; i++) { 2337 pkts[i].len = MIN_PKT_SIZE; 2338 pkts[i].options = XDP_PKT_CONTD; 2339 pkts[i].valid = false; 2340 } 2341 2342 /* Valid packet for synch */ 2343 pkts[2 * max_frags + 1].len = MIN_PKT_SIZE; 2344 pkts[2 * max_frags + 1].valid = true; 2345 2346 pkt_stream_generate_custom(test, pkts, 2 * max_frags + 2); 2347 ret = testapp_validate_traffic(test); 2348 2349 free(pkts); 2350 return ret; 2351 } 2352 2353 static int xsk_load_xdp_programs(struct ifobject *ifobj) 2354 { 2355 ifobj->xdp_progs = xsk_xdp_progs__open_and_load(); 2356 if (libbpf_get_error(ifobj->xdp_progs)) 2357 return libbpf_get_error(ifobj->xdp_progs); 2358 2359 return 0; 2360 } 2361 2362 static void xsk_unload_xdp_programs(struct ifobject *ifobj) 2363 { 2364 xsk_xdp_progs__destroy(ifobj->xdp_progs); 2365 } 2366 2367 /* Simple test */ 2368 static bool hugepages_present(void) 2369 { 2370 size_t mmap_sz = 2 * DEFAULT_UMEM_BUFFERS * XSK_UMEM__DEFAULT_FRAME_SIZE; 2371 void *bufs; 2372 2373 bufs = mmap(NULL, mmap_sz, PROT_READ | PROT_WRITE, 2374 MAP_PRIVATE | MAP_ANONYMOUS | MAP_HUGETLB, -1, MAP_HUGE_2MB); 2375 if (bufs == MAP_FAILED) 2376 return false; 2377 2378 mmap_sz = ceil_u64(mmap_sz, HUGEPAGE_SIZE) * HUGEPAGE_SIZE; 2379 munmap(bufs, mmap_sz); 2380 return true; 2381 } 2382 2383 static void init_iface(struct ifobject *ifobj, thread_func_t func_ptr) 2384 { 2385 LIBBPF_OPTS(bpf_xdp_query_opts, query_opts); 2386 int err; 2387 2388 ifobj->func_ptr = func_ptr; 2389 2390 err = xsk_load_xdp_programs(ifobj); 2391 if (err) { 2392 ksft_print_msg("Error loading XDP program\n"); 2393 exit_with_error(err); 2394 } 2395 2396 if (hugepages_present()) 2397 ifobj->unaligned_supp = true; 2398 2399 err = bpf_xdp_query(ifobj->ifindex, XDP_FLAGS_DRV_MODE, &query_opts); 2400 if (err) { 2401 ksft_print_msg("Error querying XDP capabilities\n"); 2402 exit_with_error(-err); 2403 } 2404 if (query_opts.feature_flags & NETDEV_XDP_ACT_RX_SG) 2405 ifobj->multi_buff_supp = true; 2406 if (query_opts.feature_flags & NETDEV_XDP_ACT_XSK_ZEROCOPY) { 2407 if (query_opts.xdp_zc_max_segs > 1) { 2408 ifobj->multi_buff_zc_supp = true; 2409 ifobj->xdp_zc_max_segs = query_opts.xdp_zc_max_segs; 2410 } else { 2411 ifobj->xdp_zc_max_segs = 0; 2412 } 2413 } 2414 } 2415 2416 static int testapp_send_receive(struct test_spec *test) 2417 { 2418 return testapp_validate_traffic(test); 2419 } 2420 2421 static int testapp_send_receive_2k_frame(struct test_spec *test) 2422 { 2423 test->ifobj_tx->umem->frame_size = 2048; 2424 test->ifobj_rx->umem->frame_size = 2048; 2425 pkt_stream_replace(test, DEFAULT_PKT_CNT, MIN_PKT_SIZE); 2426 return testapp_validate_traffic(test); 2427 } 2428 2429 static int testapp_poll_rx(struct test_spec *test) 2430 { 2431 test->ifobj_rx->use_poll = true; 2432 return testapp_validate_traffic(test); 2433 } 2434 2435 static int testapp_poll_tx(struct test_spec *test) 2436 { 2437 test->ifobj_tx->use_poll = true; 2438 return testapp_validate_traffic(test); 2439 } 2440 2441 static int testapp_aligned_inv_desc(struct test_spec *test) 2442 { 2443 return testapp_invalid_desc(test); 2444 } 2445 2446 static int testapp_aligned_inv_desc_2k_frame(struct test_spec *test) 2447 { 2448 test->ifobj_tx->umem->frame_size = 2048; 2449 test->ifobj_rx->umem->frame_size = 2048; 2450 return testapp_invalid_desc(test); 2451 } 2452 2453 static int testapp_unaligned_inv_desc(struct test_spec *test) 2454 { 2455 test->ifobj_tx->umem->unaligned_mode = true; 2456 test->ifobj_rx->umem->unaligned_mode = true; 2457 return testapp_invalid_desc(test); 2458 } 2459 2460 static int testapp_unaligned_inv_desc_4001_frame(struct test_spec *test) 2461 { 2462 u64 page_size, umem_size; 2463 2464 /* Odd frame size so the UMEM doesn't end near a page boundary. */ 2465 test->ifobj_tx->umem->frame_size = 4001; 2466 test->ifobj_rx->umem->frame_size = 4001; 2467 test->ifobj_tx->umem->unaligned_mode = true; 2468 test->ifobj_rx->umem->unaligned_mode = true; 2469 /* This test exists to test descriptors that staddle the end of 2470 * the UMEM but not a page. 2471 */ 2472 page_size = sysconf(_SC_PAGESIZE); 2473 umem_size = test->ifobj_tx->umem->num_frames * test->ifobj_tx->umem->frame_size; 2474 assert(umem_size % page_size > MIN_PKT_SIZE); 2475 assert(umem_size % page_size < page_size - MIN_PKT_SIZE); 2476 2477 return testapp_invalid_desc(test); 2478 } 2479 2480 static int testapp_aligned_inv_desc_mb(struct test_spec *test) 2481 { 2482 return testapp_invalid_desc_mb(test); 2483 } 2484 2485 static int testapp_unaligned_inv_desc_mb(struct test_spec *test) 2486 { 2487 test->ifobj_tx->umem->unaligned_mode = true; 2488 test->ifobj_rx->umem->unaligned_mode = true; 2489 return testapp_invalid_desc_mb(test); 2490 } 2491 2492 static int testapp_xdp_metadata(struct test_spec *test) 2493 { 2494 return testapp_xdp_metadata_copy(test); 2495 } 2496 2497 static int testapp_xdp_metadata_mb(struct test_spec *test) 2498 { 2499 test->mtu = MAX_ETH_JUMBO_SIZE; 2500 return testapp_xdp_metadata_copy(test); 2501 } 2502 2503 static int testapp_hw_sw_min_ring_size(struct test_spec *test) 2504 { 2505 int ret; 2506 2507 test->set_ring = true; 2508 test->total_steps = 2; 2509 test->ifobj_tx->ring.tx_pending = DEFAULT_BATCH_SIZE; 2510 test->ifobj_tx->ring.rx_pending = DEFAULT_BATCH_SIZE * 2; 2511 test->ifobj_tx->xsk->batch_size = 1; 2512 test->ifobj_rx->xsk->batch_size = 1; 2513 ret = testapp_validate_traffic(test); 2514 if (ret) 2515 return ret; 2516 2517 /* Set batch size to hw_ring_size - 1 */ 2518 test->ifobj_tx->xsk->batch_size = DEFAULT_BATCH_SIZE - 1; 2519 test->ifobj_rx->xsk->batch_size = DEFAULT_BATCH_SIZE - 1; 2520 return testapp_validate_traffic(test); 2521 } 2522 2523 static int testapp_hw_sw_max_ring_size(struct test_spec *test) 2524 { 2525 u32 max_descs = XSK_RING_PROD__DEFAULT_NUM_DESCS * 4; 2526 int ret; 2527 2528 test->set_ring = true; 2529 test->total_steps = 2; 2530 test->ifobj_tx->ring.tx_pending = test->ifobj_tx->ring.tx_max_pending; 2531 test->ifobj_tx->ring.rx_pending = test->ifobj_tx->ring.rx_max_pending; 2532 test->ifobj_rx->umem->num_frames = max_descs; 2533 test->ifobj_rx->umem->fill_size = max_descs; 2534 test->ifobj_rx->umem->comp_size = max_descs; 2535 test->ifobj_tx->xsk->batch_size = XSK_RING_PROD__DEFAULT_NUM_DESCS; 2536 test->ifobj_rx->xsk->batch_size = XSK_RING_PROD__DEFAULT_NUM_DESCS; 2537 2538 ret = testapp_validate_traffic(test); 2539 if (ret) 2540 return ret; 2541 2542 /* Set batch_size to 8152 for testing, as the ice HW ignores the 3 lowest bits when 2543 * updating the Rx HW tail register. 2544 */ 2545 test->ifobj_tx->xsk->batch_size = test->ifobj_tx->ring.tx_max_pending - 8; 2546 test->ifobj_rx->xsk->batch_size = test->ifobj_tx->ring.tx_max_pending - 8; 2547 pkt_stream_replace(test, max_descs, MIN_PKT_SIZE); 2548 return testapp_validate_traffic(test); 2549 } 2550 2551 static int testapp_xdp_adjust_tail(struct test_spec *test, int adjust_value) 2552 { 2553 struct xsk_xdp_progs *skel_rx = test->ifobj_rx->xdp_progs; 2554 struct xsk_xdp_progs *skel_tx = test->ifobj_tx->xdp_progs; 2555 2556 test_spec_set_xdp_prog(test, skel_rx->progs.xsk_xdp_adjust_tail, 2557 skel_tx->progs.xsk_xdp_adjust_tail, 2558 skel_rx->maps.xsk, skel_tx->maps.xsk); 2559 2560 skel_rx->bss->adjust_value = adjust_value; 2561 2562 return testapp_validate_traffic(test); 2563 } 2564 2565 static int testapp_adjust_tail(struct test_spec *test, u32 value, u32 pkt_len) 2566 { 2567 int ret; 2568 2569 test->adjust_tail_support = true; 2570 test->adjust_tail = true; 2571 test->total_steps = 1; 2572 2573 pkt_stream_replace_ifobject(test->ifobj_tx, DEFAULT_BATCH_SIZE, pkt_len); 2574 pkt_stream_replace_ifobject(test->ifobj_rx, DEFAULT_BATCH_SIZE, pkt_len + value); 2575 2576 ret = testapp_xdp_adjust_tail(test, value); 2577 if (ret) 2578 return ret; 2579 2580 if (!test->adjust_tail_support) { 2581 ksft_test_result_skip("%s %sResize pkt with bpf_xdp_adjust_tail() not supported\n", 2582 mode_string(test), busy_poll_string(test)); 2583 return TEST_SKIP; 2584 } 2585 2586 return 0; 2587 } 2588 2589 static int testapp_adjust_tail_shrink(struct test_spec *test) 2590 { 2591 /* Shrink by 4 bytes for testing purpose */ 2592 return testapp_adjust_tail(test, -4, MIN_PKT_SIZE * 2); 2593 } 2594 2595 static int testapp_adjust_tail_shrink_mb(struct test_spec *test) 2596 { 2597 test->mtu = MAX_ETH_JUMBO_SIZE; 2598 /* Shrink by the frag size */ 2599 return testapp_adjust_tail(test, -XSK_UMEM__MAX_FRAME_SIZE, XSK_UMEM__LARGE_FRAME_SIZE * 2); 2600 } 2601 2602 static int testapp_adjust_tail_grow(struct test_spec *test) 2603 { 2604 /* Grow by 4 bytes for testing purpose */ 2605 return testapp_adjust_tail(test, 4, MIN_PKT_SIZE * 2); 2606 } 2607 2608 static int testapp_adjust_tail_grow_mb(struct test_spec *test) 2609 { 2610 test->mtu = MAX_ETH_JUMBO_SIZE; 2611 /* Grow by (frag_size - last_frag_Size) - 1 to stay inside the last fragment */ 2612 return testapp_adjust_tail(test, (XSK_UMEM__MAX_FRAME_SIZE / 2) - 1, 2613 XSK_UMEM__LARGE_FRAME_SIZE * 2); 2614 } 2615 2616 static void run_pkt_test(struct test_spec *test) 2617 { 2618 int ret; 2619 2620 ret = test->test_func(test); 2621 2622 if (ret == TEST_PASS) 2623 ksft_test_result_pass("PASS: %s %s%s\n", mode_string(test), busy_poll_string(test), 2624 test->name); 2625 pkt_stream_restore_default(test); 2626 } 2627 2628 static struct ifobject *ifobject_create(void) 2629 { 2630 struct ifobject *ifobj; 2631 2632 ifobj = calloc(1, sizeof(struct ifobject)); 2633 if (!ifobj) 2634 return NULL; 2635 2636 ifobj->xsk_arr = calloc(MAX_SOCKETS, sizeof(*ifobj->xsk_arr)); 2637 if (!ifobj->xsk_arr) 2638 goto out_xsk_arr; 2639 2640 ifobj->umem = calloc(1, sizeof(*ifobj->umem)); 2641 if (!ifobj->umem) 2642 goto out_umem; 2643 2644 return ifobj; 2645 2646 out_umem: 2647 free(ifobj->xsk_arr); 2648 out_xsk_arr: 2649 free(ifobj); 2650 return NULL; 2651 } 2652 2653 static void ifobject_delete(struct ifobject *ifobj) 2654 { 2655 free(ifobj->umem); 2656 free(ifobj->xsk_arr); 2657 free(ifobj); 2658 } 2659 2660 static bool is_xdp_supported(int ifindex) 2661 { 2662 int flags = XDP_FLAGS_DRV_MODE; 2663 2664 LIBBPF_OPTS(bpf_link_create_opts, opts, .flags = flags); 2665 struct bpf_insn insns[2] = { 2666 BPF_MOV64_IMM(BPF_REG_0, XDP_PASS), 2667 BPF_EXIT_INSN() 2668 }; 2669 int prog_fd, insn_cnt = ARRAY_SIZE(insns); 2670 int err; 2671 2672 prog_fd = bpf_prog_load(BPF_PROG_TYPE_XDP, NULL, "GPL", insns, insn_cnt, NULL); 2673 if (prog_fd < 0) 2674 return false; 2675 2676 err = bpf_xdp_attach(ifindex, prog_fd, flags, NULL); 2677 if (err) { 2678 close(prog_fd); 2679 return false; 2680 } 2681 2682 bpf_xdp_detach(ifindex, flags, NULL); 2683 close(prog_fd); 2684 2685 return true; 2686 } 2687 2688 static const struct test_spec tests[] = { 2689 {.name = "SEND_RECEIVE", .test_func = testapp_send_receive}, 2690 {.name = "SEND_RECEIVE_2K_FRAME", .test_func = testapp_send_receive_2k_frame}, 2691 {.name = "SEND_RECEIVE_SINGLE_PKT", .test_func = testapp_single_pkt}, 2692 {.name = "POLL_RX", .test_func = testapp_poll_rx}, 2693 {.name = "POLL_TX", .test_func = testapp_poll_tx}, 2694 {.name = "POLL_RXQ_FULL", .test_func = testapp_poll_rxq_tmout}, 2695 {.name = "POLL_TXQ_FULL", .test_func = testapp_poll_txq_tmout}, 2696 {.name = "SEND_RECEIVE_UNALIGNED", .test_func = testapp_send_receive_unaligned}, 2697 {.name = "ALIGNED_INV_DESC", .test_func = testapp_aligned_inv_desc}, 2698 {.name = "ALIGNED_INV_DESC_2K_FRAME_SIZE", .test_func = testapp_aligned_inv_desc_2k_frame}, 2699 {.name = "UNALIGNED_INV_DESC", .test_func = testapp_unaligned_inv_desc}, 2700 {.name = "UNALIGNED_INV_DESC_4001_FRAME_SIZE", 2701 .test_func = testapp_unaligned_inv_desc_4001_frame}, 2702 {.name = "UMEM_HEADROOM", .test_func = testapp_headroom}, 2703 {.name = "TEARDOWN", .test_func = testapp_teardown}, 2704 {.name = "BIDIRECTIONAL", .test_func = testapp_bidirectional}, 2705 {.name = "STAT_RX_DROPPED", .test_func = testapp_stats_rx_dropped}, 2706 {.name = "STAT_TX_INVALID", .test_func = testapp_stats_tx_invalid_descs}, 2707 {.name = "STAT_RX_FULL", .test_func = testapp_stats_rx_full}, 2708 {.name = "STAT_FILL_EMPTY", .test_func = testapp_stats_fill_empty}, 2709 {.name = "XDP_PROG_CLEANUP", .test_func = testapp_xdp_prog_cleanup}, 2710 {.name = "XDP_DROP_HALF", .test_func = testapp_xdp_drop}, 2711 {.name = "XDP_SHARED_UMEM", .test_func = testapp_xdp_shared_umem}, 2712 {.name = "XDP_METADATA_COPY", .test_func = testapp_xdp_metadata}, 2713 {.name = "XDP_METADATA_COPY_MULTI_BUFF", .test_func = testapp_xdp_metadata_mb}, 2714 {.name = "SEND_RECEIVE_9K_PACKETS", .test_func = testapp_send_receive_mb}, 2715 {.name = "SEND_RECEIVE_UNALIGNED_9K_PACKETS", 2716 .test_func = testapp_send_receive_unaligned_mb}, 2717 {.name = "ALIGNED_INV_DESC_MULTI_BUFF", .test_func = testapp_aligned_inv_desc_mb}, 2718 {.name = "UNALIGNED_INV_DESC_MULTI_BUFF", .test_func = testapp_unaligned_inv_desc_mb}, 2719 {.name = "TOO_MANY_FRAGS", .test_func = testapp_too_many_frags}, 2720 {.name = "HW_SW_MIN_RING_SIZE", .test_func = testapp_hw_sw_min_ring_size}, 2721 {.name = "HW_SW_MAX_RING_SIZE", .test_func = testapp_hw_sw_max_ring_size}, 2722 {.name = "XDP_ADJUST_TAIL_SHRINK", .test_func = testapp_adjust_tail_shrink}, 2723 {.name = "XDP_ADJUST_TAIL_SHRINK_MULTI_BUFF", .test_func = testapp_adjust_tail_shrink_mb}, 2724 {.name = "XDP_ADJUST_TAIL_GROW", .test_func = testapp_adjust_tail_grow}, 2725 {.name = "XDP_ADJUST_TAIL_GROW_MULTI_BUFF", .test_func = testapp_adjust_tail_grow_mb}, 2726 }; 2727 2728 static void print_tests(void) 2729 { 2730 u32 i; 2731 2732 printf("Tests:\n"); 2733 for (i = 0; i < ARRAY_SIZE(tests); i++) 2734 printf("%u: %s\n", i, tests[i].name); 2735 } 2736 2737 int main(int argc, char **argv) 2738 { 2739 struct pkt_stream *rx_pkt_stream_default; 2740 struct pkt_stream *tx_pkt_stream_default; 2741 struct ifobject *ifobj_tx, *ifobj_rx; 2742 u32 i, j, failed_tests = 0, nb_tests; 2743 int modes = TEST_MODE_SKB + 1; 2744 struct test_spec test; 2745 bool shared_netdev; 2746 int ret; 2747 2748 /* Use libbpf 1.0 API mode */ 2749 libbpf_set_strict_mode(LIBBPF_STRICT_ALL); 2750 2751 ifobj_tx = ifobject_create(); 2752 if (!ifobj_tx) 2753 exit_with_error(ENOMEM); 2754 ifobj_rx = ifobject_create(); 2755 if (!ifobj_rx) 2756 exit_with_error(ENOMEM); 2757 2758 setlocale(LC_ALL, ""); 2759 2760 parse_command_line(ifobj_tx, ifobj_rx, argc, argv); 2761 2762 if (opt_print_tests) { 2763 print_tests(); 2764 ksft_exit_xpass(); 2765 } 2766 if (opt_run_test != RUN_ALL_TESTS && opt_run_test >= ARRAY_SIZE(tests)) { 2767 ksft_print_msg("Error: test %u does not exist.\n", opt_run_test); 2768 ksft_exit_xfail(); 2769 } 2770 2771 shared_netdev = (ifobj_tx->ifindex == ifobj_rx->ifindex); 2772 ifobj_tx->shared_umem = shared_netdev; 2773 ifobj_rx->shared_umem = shared_netdev; 2774 2775 if (!validate_interface(ifobj_tx) || !validate_interface(ifobj_rx)) 2776 print_usage(argv); 2777 2778 if (is_xdp_supported(ifobj_tx->ifindex)) { 2779 modes++; 2780 if (ifobj_zc_avail(ifobj_tx)) 2781 modes++; 2782 } 2783 2784 ret = get_hw_ring_size(ifobj_tx->ifname, &ifobj_tx->ring); 2785 if (!ret) { 2786 ifobj_tx->hw_ring_size_supp = true; 2787 ifobj_tx->set_ring.default_tx = ifobj_tx->ring.tx_pending; 2788 ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending; 2789 } 2790 2791 init_iface(ifobj_rx, worker_testapp_validate_rx); 2792 init_iface(ifobj_tx, worker_testapp_validate_tx); 2793 2794 test_spec_init(&test, ifobj_tx, ifobj_rx, 0, &tests[0]); 2795 tx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE); 2796 rx_pkt_stream_default = pkt_stream_generate(DEFAULT_PKT_CNT, MIN_PKT_SIZE); 2797 if (!tx_pkt_stream_default || !rx_pkt_stream_default) 2798 exit_with_error(ENOMEM); 2799 test.tx_pkt_stream_default = tx_pkt_stream_default; 2800 test.rx_pkt_stream_default = rx_pkt_stream_default; 2801 2802 if (opt_run_test == RUN_ALL_TESTS) 2803 nb_tests = ARRAY_SIZE(tests); 2804 else 2805 nb_tests = 1; 2806 if (opt_mode == TEST_MODE_ALL) { 2807 ksft_set_plan(modes * nb_tests); 2808 } else { 2809 if (opt_mode == TEST_MODE_DRV && modes <= TEST_MODE_DRV) { 2810 ksft_print_msg("Error: XDP_DRV mode not supported.\n"); 2811 ksft_exit_xfail(); 2812 } 2813 if (opt_mode == TEST_MODE_ZC && modes <= TEST_MODE_ZC) { 2814 ksft_print_msg("Error: zero-copy mode not supported.\n"); 2815 ksft_exit_xfail(); 2816 } 2817 2818 ksft_set_plan(nb_tests); 2819 } 2820 2821 for (i = 0; i < modes; i++) { 2822 if (opt_mode != TEST_MODE_ALL && i != opt_mode) 2823 continue; 2824 2825 for (j = 0; j < ARRAY_SIZE(tests); j++) { 2826 if (opt_run_test != RUN_ALL_TESTS && j != opt_run_test) 2827 continue; 2828 2829 test_spec_init(&test, ifobj_tx, ifobj_rx, i, &tests[j]); 2830 run_pkt_test(&test); 2831 usleep(USLEEP_MAX); 2832 2833 if (test.fail) 2834 failed_tests++; 2835 } 2836 } 2837 2838 if (ifobj_tx->hw_ring_size_supp) 2839 hw_ring_size_reset(ifobj_tx); 2840 2841 pkt_stream_delete(tx_pkt_stream_default); 2842 pkt_stream_delete(rx_pkt_stream_default); 2843 xsk_unload_xdp_programs(ifobj_tx); 2844 xsk_unload_xdp_programs(ifobj_rx); 2845 ifobject_delete(ifobj_tx); 2846 ifobject_delete(ifobj_rx); 2847 2848 if (failed_tests) 2849 ksft_exit_fail(); 2850 else 2851 ksft_exit_pass(); 2852 } 2853