1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2014 Intel Corporation 3 */ 4 5 6 #include <stdio.h> 7 #include <inttypes.h> 8 #include <signal.h> 9 #include <unistd.h> 10 #include <rte_cycles.h> 11 #include <rte_ethdev.h> 12 #include <rte_byteorder.h> 13 #include <rte_malloc.h> 14 #include "packet_burst_generator.h" 15 #include "test.h" 16 17 #define NB_ETHPORTS_USED (1) 18 #define NB_SOCKETS (2) 19 #define MEMPOOL_CACHE_SIZE 250 20 #define MAX_PKT_BURST (32) 21 #define RTE_TEST_RX_DESC_DEFAULT (1024) 22 #define RTE_TEST_TX_DESC_DEFAULT (1024) 23 #define RTE_PORT_ALL (~(uint16_t)0x0) 24 25 /* how long test would take at full line rate */ 26 #define RTE_TEST_DURATION (2) 27 28 /* 29 * RX and TX Prefetch, Host, and Write-back threshold values should be 30 * carefully set for optimal performance. Consult the network 31 * controller's datasheet and supporting DPDK documentation for guidance 32 * on how these parameters should be set. 33 */ 34 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */ 35 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */ 36 #define RX_WTHRESH 0 /**< Default values of RX write-back threshold reg. */ 37 38 /* 39 * These default values are optimized for use with the Intel(R) 82599 10 GbE 40 * Controller and the DPDK ixgbe PMD. Consider using other values for other 41 * network controllers and/or network drivers. 42 */ 43 #define TX_PTHRESH 32 /**< Default values of TX prefetch threshold reg. */ 44 #define TX_HTHRESH 0 /**< Default values of TX host threshold reg. */ 45 #define TX_WTHRESH 0 /**< Default values of TX write-back threshold reg. */ 46 47 #define MAX_TRAFFIC_BURST 2048 48 49 #define NB_MBUF RTE_MAX( \ 50 (unsigned)(nb_ports*nb_rx_queue*nb_rxd + \ 51 nb_ports*nb_lcores*MAX_PKT_BURST + \ 52 nb_ports*nb_tx_queue*nb_txd + \ 53 nb_lcores*MEMPOOL_CACHE_SIZE + \ 54 nb_ports*MAX_TRAFFIC_BURST), \ 55 (unsigned)8192) 56 57 58 static struct rte_mempool *mbufpool[NB_SOCKETS]; 59 /* ethernet addresses of ports */ 60 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS]; 61 62 static struct rte_eth_conf port_conf = { 63 .rxmode = { 64 .mq_mode = RTE_ETH_MQ_RX_NONE, 65 .split_hdr_size = 0, 66 }, 67 .txmode = { 68 .mq_mode = RTE_ETH_MQ_TX_NONE, 69 }, 70 .lpbk_mode = 1, /* enable loopback */ 71 }; 72 73 static struct rte_eth_rxconf rx_conf = { 74 .rx_thresh = { 75 .pthresh = RX_PTHRESH, 76 .hthresh = RX_HTHRESH, 77 .wthresh = RX_WTHRESH, 78 }, 79 .rx_free_thresh = 32, 80 }; 81 82 static struct rte_eth_txconf tx_conf = { 83 .tx_thresh = { 84 .pthresh = TX_PTHRESH, 85 .hthresh = TX_HTHRESH, 86 .wthresh = TX_WTHRESH, 87 }, 88 .tx_free_thresh = 32, /* Use PMD default values */ 89 .tx_rs_thresh = 32, /* Use PMD default values */ 90 }; 91 92 enum { 93 LCORE_INVALID = 0, 94 LCORE_AVAIL, 95 LCORE_USED, 96 }; 97 98 struct lcore_conf { 99 uint8_t status; 100 uint8_t socketid; 101 uint16_t nb_ports; 102 uint16_t portlist[RTE_MAX_ETHPORTS]; 103 } __rte_cache_aligned; 104 105 struct lcore_conf lcore_conf[RTE_MAX_LCORE]; 106 107 static uint64_t link_mbps; 108 109 enum { 110 SC_CONTINUOUS = 0, 111 SC_BURST_POLL_FIRST, 112 SC_BURST_XMIT_FIRST, 113 }; 114 115 static uint32_t sc_flag; 116 117 /* Check the link status of all ports in up to 3s, and print them finally */ 118 static void 119 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask) 120 { 121 #define CHECK_INTERVAL 100 /* 100ms */ 122 #define MAX_CHECK_TIME 30 /* 3s (30 * 100ms) in total */ 123 uint16_t portid; 124 uint8_t count, all_ports_up, print_flag = 0; 125 struct rte_eth_link link; 126 int ret; 127 char link_status[RTE_ETH_LINK_MAX_STR_LEN]; 128 129 printf("Checking link statuses...\n"); 130 fflush(stdout); 131 for (count = 0; count <= MAX_CHECK_TIME; count++) { 132 all_ports_up = 1; 133 for (portid = 0; portid < port_num; portid++) { 134 if ((port_mask & (1 << portid)) == 0) 135 continue; 136 memset(&link, 0, sizeof(link)); 137 ret = rte_eth_link_get_nowait(portid, &link); 138 if (ret < 0) { 139 all_ports_up = 0; 140 if (print_flag == 1) 141 printf("Port %u link get failed: %s\n", 142 portid, rte_strerror(-ret)); 143 continue; 144 } 145 146 /* print link status if flag set */ 147 if (print_flag == 1) { 148 if (link.link_status && link_mbps == 0) 149 link_mbps = link.link_speed; 150 151 rte_eth_link_to_str(link_status, 152 sizeof(link_status), &link); 153 printf("Port %d %s\n", portid, link_status); 154 continue; 155 } 156 /* clear all_ports_up flag if any link down */ 157 if (link.link_status == RTE_ETH_LINK_DOWN) { 158 all_ports_up = 0; 159 break; 160 } 161 } 162 /* after finally printing all link status, get out */ 163 if (print_flag == 1) 164 break; 165 166 if (all_ports_up == 0) { 167 fflush(stdout); 168 rte_delay_ms(CHECK_INTERVAL); 169 } 170 171 /* set the print_flag if all ports up or timeout */ 172 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) 173 print_flag = 1; 174 } 175 } 176 177 static void 178 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr) 179 { 180 char buf[RTE_ETHER_ADDR_FMT_SIZE]; 181 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr); 182 printf("%s%s", name, buf); 183 } 184 185 static int 186 init_traffic(struct rte_mempool *mp, 187 struct rte_mbuf **pkts_burst, uint32_t burst_size) 188 { 189 struct rte_ether_hdr pkt_eth_hdr; 190 struct rte_ipv4_hdr pkt_ipv4_hdr; 191 struct rte_udp_hdr pkt_udp_hdr; 192 uint32_t pktlen; 193 static uint8_t src_mac[] = { 0x00, 0xFF, 0xAA, 0xFF, 0xAA, 0xFF }; 194 static uint8_t dst_mac[] = { 0x00, 0xAA, 0xFF, 0xAA, 0xFF, 0xAA }; 195 196 197 initialize_eth_header(&pkt_eth_hdr, 198 (struct rte_ether_addr *)src_mac, 199 (struct rte_ether_addr *)dst_mac, RTE_ETHER_TYPE_IPV4, 0, 0); 200 201 pktlen = initialize_ipv4_header(&pkt_ipv4_hdr, 202 IPV4_ADDR(10, 0, 0, 1), 203 IPV4_ADDR(10, 0, 0, 2), 26); 204 printf("IPv4 pktlen %u\n", pktlen); 205 206 pktlen = initialize_udp_header(&pkt_udp_hdr, 0, 0, 18); 207 208 printf("UDP pktlen %u\n", pktlen); 209 210 return generate_packet_burst(mp, pkts_burst, &pkt_eth_hdr, 211 0, &pkt_ipv4_hdr, 1, 212 &pkt_udp_hdr, burst_size, 213 PACKET_BURST_GEN_PKT_LEN, 1); 214 } 215 216 static int 217 init_lcores(void) 218 { 219 unsigned lcore_id; 220 221 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 222 lcore_conf[lcore_id].socketid = 223 rte_lcore_to_socket_id(lcore_id); 224 if (rte_lcore_is_enabled(lcore_id) == 0) { 225 lcore_conf[lcore_id].status = LCORE_INVALID; 226 continue; 227 } else 228 lcore_conf[lcore_id].status = LCORE_AVAIL; 229 } 230 return 0; 231 } 232 233 static int 234 init_mbufpool(unsigned nb_mbuf) 235 { 236 int socketid; 237 unsigned lcore_id; 238 char s[64]; 239 240 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 241 if (rte_lcore_is_enabled(lcore_id) == 0) 242 continue; 243 244 socketid = rte_lcore_to_socket_id(lcore_id); 245 if (socketid >= NB_SOCKETS) { 246 rte_exit(EXIT_FAILURE, 247 "Socket %d of lcore %u is out of range %d\n", 248 socketid, lcore_id, NB_SOCKETS); 249 } 250 if (mbufpool[socketid] == NULL) { 251 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid); 252 mbufpool[socketid] = 253 rte_pktmbuf_pool_create(s, nb_mbuf, 254 MEMPOOL_CACHE_SIZE, 0, 255 RTE_MBUF_DEFAULT_BUF_SIZE, socketid); 256 if (mbufpool[socketid] == NULL) 257 rte_exit(EXIT_FAILURE, 258 "Cannot init mbuf pool on socket %d\n", 259 socketid); 260 else 261 printf("Allocated mbuf pool on socket %d\n", 262 socketid); 263 } 264 } 265 return 0; 266 } 267 268 static uint16_t 269 alloc_lcore(uint16_t socketid) 270 { 271 unsigned lcore_id; 272 273 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 274 if (LCORE_AVAIL != lcore_conf[lcore_id].status || 275 lcore_conf[lcore_id].socketid != socketid || 276 lcore_id == rte_get_main_lcore()) 277 continue; 278 lcore_conf[lcore_id].status = LCORE_USED; 279 lcore_conf[lcore_id].nb_ports = 0; 280 return lcore_id; 281 } 282 283 return (uint16_t)-1; 284 } 285 286 static volatile uint64_t stop; 287 static uint64_t count; 288 static uint64_t drop; 289 static uint64_t idle; 290 291 static void 292 reset_count(void) 293 { 294 count = 0; 295 drop = 0; 296 idle = 0; 297 } 298 299 static void 300 stats_display(uint16_t port_id) 301 { 302 struct rte_eth_stats stats; 303 rte_eth_stats_get(port_id, &stats); 304 305 printf(" RX-packets: %-10"PRIu64" RX-missed: %-10"PRIu64" RX-bytes: " 306 "%-"PRIu64"\n", 307 stats.ipackets, stats.imissed, stats.ibytes); 308 printf(" RX-errors: %-10"PRIu64" RX-nombuf: %-10"PRIu64"\n", 309 stats.ierrors, stats.rx_nombuf); 310 printf(" TX-packets: %-10"PRIu64" TX-errors: %-10"PRIu64" TX-bytes: " 311 "%-"PRIu64"\n", 312 stats.opackets, stats.oerrors, stats.obytes); 313 } 314 315 static void 316 signal_handler(int signum) 317 { 318 /* USR1 signal, stop testing */ 319 if (signum == SIGUSR1) { 320 printf("Force Stop!\n"); 321 stop = 1; 322 } 323 324 /* USR2 signal, print stats */ 325 if (signum == SIGUSR2) 326 stats_display(0); 327 } 328 329 struct rte_mbuf **tx_burst; 330 331 uint64_t (*do_measure)(struct lcore_conf *conf, 332 struct rte_mbuf *pkts_burst[], 333 uint64_t total_pkts); 334 335 static uint64_t 336 measure_rxtx(struct lcore_conf *conf, 337 struct rte_mbuf *pkts_burst[], 338 uint64_t total_pkts) 339 { 340 unsigned i, portid, nb_rx, nb_tx; 341 uint64_t prev_tsc, cur_tsc; 342 343 prev_tsc = rte_rdtsc(); 344 345 while (likely(!stop)) { 346 for (i = 0; i < conf->nb_ports; i++) { 347 portid = conf->portlist[i]; 348 nb_rx = rte_eth_rx_burst(portid, 0, 349 pkts_burst, MAX_PKT_BURST); 350 if (unlikely(nb_rx == 0)) { 351 idle++; 352 continue; 353 } 354 355 count += nb_rx; 356 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx); 357 if (unlikely(nb_tx < nb_rx)) { 358 drop += (nb_rx - nb_tx); 359 do { 360 rte_pktmbuf_free(pkts_burst[nb_tx]); 361 } while (++nb_tx < nb_rx); 362 } 363 } 364 if (unlikely(count >= total_pkts)) 365 break; 366 } 367 368 cur_tsc = rte_rdtsc(); 369 370 return cur_tsc - prev_tsc; 371 } 372 373 static uint64_t 374 measure_rxonly(struct lcore_conf *conf, 375 struct rte_mbuf *pkts_burst[], 376 uint64_t total_pkts) 377 { 378 unsigned i, portid, nb_rx, nb_tx; 379 uint64_t diff_tsc, cur_tsc; 380 381 diff_tsc = 0; 382 while (likely(!stop)) { 383 for (i = 0; i < conf->nb_ports; i++) { 384 portid = conf->portlist[i]; 385 386 cur_tsc = rte_rdtsc(); 387 nb_rx = rte_eth_rx_burst(portid, 0, 388 pkts_burst, MAX_PKT_BURST); 389 if (unlikely(nb_rx == 0)) { 390 idle++; 391 continue; 392 } 393 diff_tsc += rte_rdtsc() - cur_tsc; 394 395 count += nb_rx; 396 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx); 397 if (unlikely(nb_tx < nb_rx)) { 398 drop += (nb_rx - nb_tx); 399 do { 400 rte_pktmbuf_free(pkts_burst[nb_tx]); 401 } while (++nb_tx < nb_rx); 402 } 403 } 404 if (unlikely(count >= total_pkts)) 405 break; 406 } 407 408 return diff_tsc; 409 } 410 411 static uint64_t 412 measure_txonly(struct lcore_conf *conf, 413 struct rte_mbuf *pkts_burst[], 414 uint64_t total_pkts) 415 { 416 unsigned i, portid, nb_rx, nb_tx; 417 uint64_t diff_tsc, cur_tsc; 418 419 printf("do tx measure\n"); 420 diff_tsc = 0; 421 while (likely(!stop)) { 422 for (i = 0; i < conf->nb_ports; i++) { 423 portid = conf->portlist[i]; 424 nb_rx = rte_eth_rx_burst(portid, 0, 425 pkts_burst, MAX_PKT_BURST); 426 if (unlikely(nb_rx == 0)) { 427 idle++; 428 continue; 429 } 430 431 count += nb_rx; 432 433 cur_tsc = rte_rdtsc(); 434 nb_tx = rte_eth_tx_burst(portid, 0, pkts_burst, nb_rx); 435 if (unlikely(nb_tx < nb_rx)) { 436 drop += (nb_rx - nb_tx); 437 do { 438 rte_pktmbuf_free(pkts_burst[nb_tx]); 439 } while (++nb_tx < nb_rx); 440 } 441 diff_tsc += rte_rdtsc() - cur_tsc; 442 } 443 if (unlikely(count >= total_pkts)) 444 break; 445 } 446 447 return diff_tsc; 448 } 449 450 /* main processing loop */ 451 static int 452 main_loop(__rte_unused void *args) 453 { 454 #define PACKET_SIZE 64 455 #define FRAME_GAP 12 456 #define MAC_PREAMBLE 8 457 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 458 unsigned lcore_id; 459 unsigned i, portid, nb_rx = 0, nb_tx = 0; 460 struct lcore_conf *conf; 461 int pkt_per_port; 462 uint64_t diff_tsc; 463 uint64_t packets_per_second, total_packets; 464 465 lcore_id = rte_lcore_id(); 466 conf = &lcore_conf[lcore_id]; 467 if (conf->status != LCORE_USED) 468 return 0; 469 470 pkt_per_port = MAX_TRAFFIC_BURST; 471 472 int idx = 0; 473 for (i = 0; i < conf->nb_ports; i++) { 474 int num = pkt_per_port; 475 portid = conf->portlist[i]; 476 printf("inject %d packet to port %d\n", num, portid); 477 while (num) { 478 nb_tx = RTE_MIN(MAX_PKT_BURST, num); 479 nb_tx = rte_eth_tx_burst(portid, 0, 480 &tx_burst[idx], nb_tx); 481 num -= nb_tx; 482 idx += nb_tx; 483 } 484 } 485 printf("Total packets inject to prime ports = %u\n", idx); 486 487 packets_per_second = (link_mbps * 1000 * 1000) / 488 ((PACKET_SIZE + FRAME_GAP + MAC_PREAMBLE) * CHAR_BIT); 489 printf("Each port will do %"PRIu64" packets per second\n", 490 packets_per_second); 491 492 total_packets = RTE_TEST_DURATION * conf->nb_ports * packets_per_second; 493 printf("Test will stop after at least %"PRIu64" packets received\n", 494 + total_packets); 495 496 diff_tsc = do_measure(conf, pkts_burst, total_packets); 497 498 for (i = 0; i < conf->nb_ports; i++) { 499 portid = conf->portlist[i]; 500 int nb_free = 0; 501 uint64_t timeout = 10000; 502 do { /* dry out */ 503 nb_rx = rte_eth_rx_burst(portid, 0, 504 pkts_burst, MAX_PKT_BURST); 505 nb_tx = 0; 506 while (nb_tx < nb_rx) 507 rte_pktmbuf_free(pkts_burst[nb_tx++]); 508 nb_free += nb_rx; 509 510 if (unlikely(nb_rx == 0)) 511 timeout--; 512 } while (nb_free != pkt_per_port && timeout != 0); 513 printf("free %d (expected %d) mbuf left in port %u\n", nb_free, 514 pkt_per_port, portid); 515 } 516 517 if (count == 0) 518 return -1; 519 520 printf("%"PRIu64" packet, %"PRIu64" drop, %"PRIu64" idle\n", 521 count, drop, idle); 522 printf("Result: %"PRIu64" cycles per packet\n", diff_tsc / count); 523 524 return 0; 525 } 526 527 static uint64_t start; 528 529 static inline int 530 poll_burst(void *args) 531 { 532 #define MAX_IDLE (10000) 533 unsigned lcore_id; 534 struct rte_mbuf **pkts_burst; 535 uint64_t diff_tsc, cur_tsc; 536 uint16_t next[RTE_MAX_ETHPORTS]; 537 struct lcore_conf *conf; 538 uint32_t pkt_per_port = *((uint32_t *)args); 539 unsigned i, portid, nb_rx = 0; 540 uint64_t total; 541 uint64_t timeout = MAX_IDLE; 542 int num[RTE_MAX_ETHPORTS]; 543 544 lcore_id = rte_lcore_id(); 545 conf = &lcore_conf[lcore_id]; 546 if (conf->status != LCORE_USED) 547 return 0; 548 549 total = pkt_per_port * conf->nb_ports; 550 printf("start to receive total expect %"PRIu64"\n", total); 551 552 pkts_burst = (struct rte_mbuf **) 553 rte_calloc_socket("poll_burst", 554 total, sizeof(void *), 555 RTE_CACHE_LINE_SIZE, conf->socketid); 556 if (!pkts_burst) 557 return -1; 558 559 for (i = 0; i < conf->nb_ports; i++) { 560 portid = conf->portlist[i]; 561 next[portid] = i * pkt_per_port; 562 num[portid] = pkt_per_port; 563 } 564 565 rte_wait_until_equal_64(&start, 1, __ATOMIC_ACQUIRE); 566 567 cur_tsc = rte_rdtsc(); 568 while (total) { 569 for (i = 0; i < conf->nb_ports; i++) { 570 portid = conf->portlist[i]; 571 nb_rx = rte_eth_rx_burst(portid, 0, 572 &pkts_burst[next[portid]], 573 RTE_MIN(MAX_PKT_BURST, num[portid])); 574 if (unlikely(nb_rx == 0)) { 575 timeout--; 576 if (unlikely(timeout == 0)) 577 goto timeout; 578 continue; 579 } 580 next[portid] += nb_rx; 581 num[portid] -= nb_rx; 582 total -= nb_rx; 583 } 584 } 585 timeout: 586 diff_tsc = rte_rdtsc() - cur_tsc; 587 588 printf("%"PRIu64" packets lost, IDLE %"PRIu64" times\n", 589 total, MAX_IDLE - timeout); 590 /* clean up */ 591 total = pkt_per_port * conf->nb_ports - total; 592 for (i = 0; i < total; i++) 593 rte_pktmbuf_free(pkts_burst[i]); 594 595 rte_free(pkts_burst); 596 597 if (total > 0) 598 return diff_tsc / total; 599 else 600 return -1; 601 } 602 603 static int 604 exec_burst(uint32_t flags, int lcore) 605 { 606 unsigned int portid, nb_tx = 0; 607 struct lcore_conf *conf; 608 uint32_t pkt_per_port; 609 int num, i, idx = 0; 610 int diff_tsc; 611 612 conf = &lcore_conf[lcore]; 613 614 pkt_per_port = MAX_TRAFFIC_BURST; 615 num = pkt_per_port * conf->nb_ports; 616 617 /* only when polling first */ 618 if (flags == SC_BURST_POLL_FIRST) 619 __atomic_store_n(&start, 1, __ATOMIC_RELAXED); 620 else 621 __atomic_store_n(&start, 0, __ATOMIC_RELAXED); 622 623 /* start polling thread 624 * if in POLL_FIRST mode, poll once launched; 625 * otherwise, not actually poll yet 626 */ 627 rte_eal_remote_launch(poll_burst, 628 (void *)&pkt_per_port, lcore); 629 630 /* start xmit */ 631 i = 0; 632 while (num) { 633 nb_tx = RTE_MIN(MAX_PKT_BURST, num); 634 portid = conf->portlist[i]; 635 nb_tx = rte_eth_tx_burst(portid, 0, &tx_burst[idx], nb_tx); 636 idx += nb_tx; 637 num -= nb_tx; 638 i = (i >= conf->nb_ports - 1) ? 0 : (i + 1); 639 } 640 641 sleep(5); 642 643 /* only when polling second */ 644 if (flags == SC_BURST_XMIT_FIRST) 645 __atomic_store_n(&start, 1, __ATOMIC_RELEASE); 646 647 /* wait for polling finished */ 648 diff_tsc = rte_eal_wait_lcore(lcore); 649 if (diff_tsc < 0) { 650 printf("exec_burst: Failed to measure cycles per packet\n"); 651 return -1; 652 } 653 654 printf("Result: %d cycles per packet\n", diff_tsc); 655 656 return 0; 657 } 658 659 static int 660 test_pmd_perf(void) 661 { 662 uint16_t nb_ports, num, nb_lcores, worker_id = (uint16_t)-1; 663 uint16_t nb_rxd = MAX_TRAFFIC_BURST; 664 uint16_t nb_txd = MAX_TRAFFIC_BURST; 665 uint16_t portid; 666 uint16_t nb_rx_queue = 1, nb_tx_queue = 1; 667 int socketid = -1; 668 int ret; 669 670 printf("Start PMD RXTX cycles cost test.\n"); 671 672 signal(SIGUSR1, signal_handler); 673 signal(SIGUSR2, signal_handler); 674 675 nb_ports = rte_eth_dev_count_avail(); 676 if (nb_ports < NB_ETHPORTS_USED) { 677 printf("At least %u port(s) used for perf. test\n", 678 NB_ETHPORTS_USED); 679 return -1; 680 } 681 682 nb_lcores = rte_lcore_count(); 683 684 memset(lcore_conf, 0, sizeof(lcore_conf)); 685 init_lcores(); 686 687 init_mbufpool(NB_MBUF); 688 689 if (sc_flag == SC_CONTINUOUS) { 690 nb_rxd = RTE_TEST_RX_DESC_DEFAULT; 691 nb_txd = RTE_TEST_TX_DESC_DEFAULT; 692 } 693 printf("CONFIG RXD=%d TXD=%d\n", nb_rxd, nb_txd); 694 695 reset_count(); 696 num = 0; 697 RTE_ETH_FOREACH_DEV(portid) { 698 if (socketid == -1) { 699 socketid = rte_eth_dev_socket_id(portid); 700 worker_id = alloc_lcore(socketid); 701 if (worker_id == (uint16_t)-1) { 702 printf("No avail lcore to run test\n"); 703 return -1; 704 } 705 printf("Performance test runs on lcore %u socket %u\n", 706 worker_id, socketid); 707 } 708 709 if (socketid != rte_eth_dev_socket_id(portid)) { 710 printf("Skip port %d\n", portid); 711 continue; 712 } 713 714 /* port configure */ 715 ret = rte_eth_dev_configure(portid, nb_rx_queue, 716 nb_tx_queue, &port_conf); 717 if (ret < 0) 718 rte_exit(EXIT_FAILURE, 719 "Cannot configure device: err=%d, port=%d\n", 720 ret, portid); 721 722 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]); 723 if (ret < 0) 724 rte_exit(EXIT_FAILURE, 725 "Cannot get mac address: err=%d, port=%d\n", 726 ret, portid); 727 728 printf("Port %u ", portid); 729 print_ethaddr("Address:", &ports_eth_addr[portid]); 730 printf("\n"); 731 732 /* tx queue setup */ 733 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd, 734 socketid, &tx_conf); 735 if (ret < 0) 736 rte_exit(EXIT_FAILURE, 737 "rte_eth_tx_queue_setup: err=%d, " 738 "port=%d\n", ret, portid); 739 740 /* rx queue steup */ 741 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 742 socketid, &rx_conf, 743 mbufpool[socketid]); 744 if (ret < 0) 745 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d," 746 "port=%d\n", ret, portid); 747 748 /* Start device */ 749 stop = 0; 750 ret = rte_eth_dev_start(portid); 751 if (ret < 0) 752 rte_exit(EXIT_FAILURE, 753 "rte_eth_dev_start: err=%d, port=%d\n", 754 ret, portid); 755 756 /* always enable promiscuous */ 757 ret = rte_eth_promiscuous_enable(portid); 758 if (ret != 0) 759 rte_exit(EXIT_FAILURE, 760 "rte_eth_promiscuous_enable: err=%s, port=%d\n", 761 rte_strerror(-ret), portid); 762 763 lcore_conf[worker_id].portlist[num++] = portid; 764 lcore_conf[worker_id].nb_ports++; 765 } 766 check_all_ports_link_status(nb_ports, RTE_PORT_ALL); 767 768 if (tx_burst == NULL) { 769 tx_burst = (struct rte_mbuf **) 770 rte_calloc_socket("tx_buff", 771 MAX_TRAFFIC_BURST * nb_ports, 772 sizeof(void *), 773 RTE_CACHE_LINE_SIZE, socketid); 774 if (!tx_burst) 775 return -1; 776 } 777 778 init_traffic(mbufpool[socketid], 779 tx_burst, MAX_TRAFFIC_BURST * nb_ports); 780 781 printf("Generate %d packets @socket %d\n", 782 MAX_TRAFFIC_BURST * nb_ports, socketid); 783 784 if (sc_flag == SC_CONTINUOUS) { 785 /* do both rxtx by default */ 786 if (NULL == do_measure) 787 do_measure = measure_rxtx; 788 789 rte_eal_remote_launch(main_loop, NULL, worker_id); 790 791 if (rte_eal_wait_lcore(worker_id) < 0) 792 return -1; 793 } else if (sc_flag == SC_BURST_POLL_FIRST || 794 sc_flag == SC_BURST_XMIT_FIRST) 795 if (exec_burst(sc_flag, worker_id) < 0) 796 return -1; 797 798 /* port tear down */ 799 RTE_ETH_FOREACH_DEV(portid) { 800 if (socketid != rte_eth_dev_socket_id(portid)) 801 continue; 802 803 ret = rte_eth_dev_stop(portid); 804 if (ret != 0) 805 printf("rte_eth_dev_stop: err=%s, port=%u\n", 806 rte_strerror(-ret), portid); 807 } 808 809 return 0; 810 } 811 812 int 813 test_set_rxtx_conf(cmdline_fixed_string_t mode) 814 { 815 printf("mode switch to %s\n", mode); 816 817 if (!strcmp(mode, "vector")) { 818 /* vector rx, tx */ 819 tx_conf.tx_rs_thresh = 32; 820 tx_conf.tx_free_thresh = 32; 821 return 0; 822 } else if (!strcmp(mode, "scalar")) { 823 /* bulk alloc rx, full-featured tx */ 824 tx_conf.tx_rs_thresh = 32; 825 tx_conf.tx_free_thresh = 32; 826 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_CHECKSUM; 827 return 0; 828 } else if (!strcmp(mode, "hybrid")) { 829 /* bulk alloc rx, vector tx 830 * when vec macro not define, 831 * using the same rx/tx as scalar 832 */ 833 tx_conf.tx_rs_thresh = 32; 834 tx_conf.tx_free_thresh = 32; 835 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_CHECKSUM; 836 return 0; 837 } else if (!strcmp(mode, "full")) { 838 /* full feature rx,tx pair */ 839 tx_conf.tx_rs_thresh = 32; 840 tx_conf.tx_free_thresh = 32; 841 port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_SCATTER; 842 return 0; 843 } 844 845 return -1; 846 } 847 848 int 849 test_set_rxtx_anchor(cmdline_fixed_string_t type) 850 { 851 printf("type switch to %s\n", type); 852 853 if (!strcmp(type, "rxtx")) { 854 do_measure = measure_rxtx; 855 return 0; 856 } else if (!strcmp(type, "rxonly")) { 857 do_measure = measure_rxonly; 858 return 0; 859 } else if (!strcmp(type, "txonly")) { 860 do_measure = measure_txonly; 861 return 0; 862 } 863 864 return -1; 865 } 866 867 int 868 test_set_rxtx_sc(cmdline_fixed_string_t type) 869 { 870 printf("stream control switch to %s\n", type); 871 872 if (!strcmp(type, "continuous")) { 873 sc_flag = SC_CONTINUOUS; 874 return 0; 875 } else if (!strcmp(type, "poll_before_xmit")) { 876 sc_flag = SC_BURST_POLL_FIRST; 877 return 0; 878 } else if (!strcmp(type, "poll_after_xmit")) { 879 sc_flag = SC_BURST_XMIT_FIRST; 880 return 0; 881 } 882 883 return -1; 884 } 885 886 REGISTER_TEST_COMMAND(pmd_perf_autotest, test_pmd_perf); 887