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