1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <stdint.h> 8 #include <inttypes.h> 9 #include <sys/types.h> 10 #include <string.h> 11 #include <sys/queue.h> 12 #include <stdarg.h> 13 #include <errno.h> 14 #include <getopt.h> 15 #include <signal.h> 16 #include <stdbool.h> 17 18 #include <rte_common.h> 19 #include <rte_vect.h> 20 #include <rte_byteorder.h> 21 #include <rte_log.h> 22 #include <rte_memory.h> 23 #include <rte_memcpy.h> 24 #include <rte_eal.h> 25 #include <rte_launch.h> 26 #include <rte_atomic.h> 27 #include <rte_cycles.h> 28 #include <rte_prefetch.h> 29 #include <rte_lcore.h> 30 #include <rte_per_lcore.h> 31 #include <rte_branch_prediction.h> 32 #include <rte_interrupts.h> 33 #include <rte_random.h> 34 #include <rte_debug.h> 35 #include <rte_ether.h> 36 #include <rte_ethdev.h> 37 #include <rte_mempool.h> 38 #include <rte_mbuf.h> 39 #include <rte_ip.h> 40 #include <rte_tcp.h> 41 #include <rte_udp.h> 42 #include <rte_string_fns.h> 43 #include <rte_cpuflags.h> 44 45 #include <cmdline_parse.h> 46 #include <cmdline_parse_etheraddr.h> 47 48 #include "l3fwd.h" 49 50 /* 51 * Configurable number of RX/TX ring descriptors 52 */ 53 #define RTE_TEST_RX_DESC_DEFAULT 1024 54 #define RTE_TEST_TX_DESC_DEFAULT 1024 55 56 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS 57 #define MAX_RX_QUEUE_PER_PORT 128 58 59 #define MAX_LCORE_PARAMS 1024 60 61 /* Static global variables used within this file. */ 62 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT; 63 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT; 64 65 /**< Ports set in promiscuous mode off by default. */ 66 static int promiscuous_on; 67 68 /* Select Longest-Prefix or Exact match. */ 69 static int l3fwd_lpm_on; 70 static int l3fwd_em_on; 71 72 /* Global variables. */ 73 74 static int numa_on = 1; /**< NUMA is enabled by default. */ 75 static int parse_ptype; /**< Parse packet type using rx callback, and */ 76 /**< disabled by default */ 77 static int per_port_pool; /**< Use separate buffer pools per port; disabled */ 78 /**< by default */ 79 80 volatile bool force_quit; 81 82 /* ethernet addresses of ports */ 83 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS]; 84 struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS]; 85 86 xmm_t val_eth[RTE_MAX_ETHPORTS]; 87 88 /* mask of enabled ports */ 89 uint32_t enabled_port_mask; 90 91 /* Used only in exact match mode. */ 92 int ipv6; /**< ipv6 is false by default. */ 93 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT; 94 95 struct lcore_conf lcore_conf[RTE_MAX_LCORE]; 96 97 struct lcore_params { 98 uint16_t port_id; 99 uint8_t queue_id; 100 uint8_t lcore_id; 101 } __rte_cache_aligned; 102 103 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS]; 104 static struct lcore_params lcore_params_array_default[] = { 105 {0, 0, 2}, 106 {0, 1, 2}, 107 {0, 2, 2}, 108 {1, 0, 2}, 109 {1, 1, 2}, 110 {1, 2, 2}, 111 {2, 0, 2}, 112 {3, 0, 3}, 113 {3, 1, 3}, 114 }; 115 116 static struct lcore_params * lcore_params = lcore_params_array_default; 117 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) / 118 sizeof(lcore_params_array_default[0]); 119 120 static struct rte_eth_conf port_conf = { 121 .rxmode = { 122 .mq_mode = ETH_MQ_RX_RSS, 123 .max_rx_pkt_len = RTE_ETHER_MAX_LEN, 124 .split_hdr_size = 0, 125 .offloads = DEV_RX_OFFLOAD_CHECKSUM, 126 }, 127 .rx_adv_conf = { 128 .rss_conf = { 129 .rss_key = NULL, 130 .rss_hf = ETH_RSS_IP, 131 }, 132 }, 133 .txmode = { 134 .mq_mode = ETH_MQ_TX_NONE, 135 }, 136 }; 137 138 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS]; 139 static uint8_t lkp_per_socket[NB_SOCKETS]; 140 141 struct l3fwd_lkp_mode { 142 void (*setup)(int); 143 int (*check_ptype)(int); 144 rte_rx_callback_fn cb_parse_ptype; 145 int (*main_loop)(void *); 146 void* (*get_ipv4_lookup_struct)(int); 147 void* (*get_ipv6_lookup_struct)(int); 148 }; 149 150 static struct l3fwd_lkp_mode l3fwd_lkp; 151 152 static struct l3fwd_lkp_mode l3fwd_em_lkp = { 153 .setup = setup_hash, 154 .check_ptype = em_check_ptype, 155 .cb_parse_ptype = em_cb_parse_ptype, 156 .main_loop = em_main_loop, 157 .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct, 158 .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct, 159 }; 160 161 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = { 162 .setup = setup_lpm, 163 .check_ptype = lpm_check_ptype, 164 .cb_parse_ptype = lpm_cb_parse_ptype, 165 .main_loop = lpm_main_loop, 166 .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct, 167 .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct, 168 }; 169 170 /* 171 * Setup lookup methods for forwarding. 172 * Currently exact-match and longest-prefix-match 173 * are supported ones. 174 */ 175 static void 176 setup_l3fwd_lookup_tables(void) 177 { 178 /* Setup HASH lookup functions. */ 179 if (l3fwd_em_on) 180 l3fwd_lkp = l3fwd_em_lkp; 181 /* Setup LPM lookup functions. */ 182 else 183 l3fwd_lkp = l3fwd_lpm_lkp; 184 } 185 186 static int 187 check_lcore_params(void) 188 { 189 uint8_t queue, lcore; 190 uint16_t i; 191 int socketid; 192 193 for (i = 0; i < nb_lcore_params; ++i) { 194 queue = lcore_params[i].queue_id; 195 if (queue >= MAX_RX_QUEUE_PER_PORT) { 196 printf("invalid queue number: %hhu\n", queue); 197 return -1; 198 } 199 lcore = lcore_params[i].lcore_id; 200 if (!rte_lcore_is_enabled(lcore)) { 201 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore); 202 return -1; 203 } 204 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) && 205 (numa_on == 0)) { 206 printf("warning: lcore %hhu is on socket %d with numa off \n", 207 lcore, socketid); 208 } 209 } 210 return 0; 211 } 212 213 static int 214 check_port_config(void) 215 { 216 uint16_t portid; 217 uint16_t i; 218 219 for (i = 0; i < nb_lcore_params; ++i) { 220 portid = lcore_params[i].port_id; 221 if ((enabled_port_mask & (1 << portid)) == 0) { 222 printf("port %u is not enabled in port mask\n", portid); 223 return -1; 224 } 225 if (!rte_eth_dev_is_valid_port(portid)) { 226 printf("port %u is not present on the board\n", portid); 227 return -1; 228 } 229 } 230 return 0; 231 } 232 233 static uint8_t 234 get_port_n_rx_queues(const uint16_t port) 235 { 236 int queue = -1; 237 uint16_t i; 238 239 for (i = 0; i < nb_lcore_params; ++i) { 240 if (lcore_params[i].port_id == port) { 241 if (lcore_params[i].queue_id == queue+1) 242 queue = lcore_params[i].queue_id; 243 else 244 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be" 245 " in sequence and must start with 0\n", 246 lcore_params[i].port_id); 247 } 248 } 249 return (uint8_t)(++queue); 250 } 251 252 static int 253 init_lcore_rx_queues(void) 254 { 255 uint16_t i, nb_rx_queue; 256 uint8_t lcore; 257 258 for (i = 0; i < nb_lcore_params; ++i) { 259 lcore = lcore_params[i].lcore_id; 260 nb_rx_queue = lcore_conf[lcore].n_rx_queue; 261 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) { 262 printf("error: too many queues (%u) for lcore: %u\n", 263 (unsigned)nb_rx_queue + 1, (unsigned)lcore); 264 return -1; 265 } else { 266 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id = 267 lcore_params[i].port_id; 268 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id = 269 lcore_params[i].queue_id; 270 lcore_conf[lcore].n_rx_queue++; 271 } 272 } 273 return 0; 274 } 275 276 /* display usage */ 277 static void 278 print_usage(const char *prgname) 279 { 280 fprintf(stderr, "%s [EAL options] --" 281 " -p PORTMASK" 282 " [-P]" 283 " [-E]" 284 " [-L]" 285 " --config (port,queue,lcore)[,(port,queue,lcore)]" 286 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]" 287 " [--enable-jumbo [--max-pkt-len PKTLEN]]" 288 " [--no-numa]" 289 " [--hash-entry-num]" 290 " [--ipv6]" 291 " [--parse-ptype]" 292 " [--per-port-pool]\n\n" 293 294 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n" 295 " -P : Enable promiscuous mode\n" 296 " -E : Enable exact match\n" 297 " -L : Enable longest prefix match (default)\n" 298 " --config (port,queue,lcore): Rx queue configuration\n" 299 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n" 300 " --enable-jumbo: Enable jumbo frames\n" 301 " --max-pkt-len: Under the premise of enabling jumbo,\n" 302 " maximum packet length in decimal (64-9600)\n" 303 " --no-numa: Disable numa awareness\n" 304 " --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n" 305 " --ipv6: Set if running ipv6 packets\n" 306 " --parse-ptype: Set to use software to analyze packet type\n" 307 " --per-port-pool: Use separate buffer pool per port\n\n", 308 prgname); 309 } 310 311 static int 312 parse_max_pkt_len(const char *pktlen) 313 { 314 char *end = NULL; 315 unsigned long len; 316 317 /* parse decimal string */ 318 len = strtoul(pktlen, &end, 10); 319 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0')) 320 return -1; 321 322 if (len == 0) 323 return -1; 324 325 return len; 326 } 327 328 static int 329 parse_portmask(const char *portmask) 330 { 331 char *end = NULL; 332 unsigned long pm; 333 334 /* parse hexadecimal string */ 335 pm = strtoul(portmask, &end, 16); 336 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 337 return -1; 338 339 if (pm == 0) 340 return -1; 341 342 return pm; 343 } 344 345 static int 346 parse_hash_entry_number(const char *hash_entry_num) 347 { 348 char *end = NULL; 349 unsigned long hash_en; 350 /* parse hexadecimal string */ 351 hash_en = strtoul(hash_entry_num, &end, 16); 352 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0')) 353 return -1; 354 355 if (hash_en == 0) 356 return -1; 357 358 return hash_en; 359 } 360 361 static int 362 parse_config(const char *q_arg) 363 { 364 char s[256]; 365 const char *p, *p0 = q_arg; 366 char *end; 367 enum fieldnames { 368 FLD_PORT = 0, 369 FLD_QUEUE, 370 FLD_LCORE, 371 _NUM_FLD 372 }; 373 unsigned long int_fld[_NUM_FLD]; 374 char *str_fld[_NUM_FLD]; 375 int i; 376 unsigned size; 377 378 nb_lcore_params = 0; 379 380 while ((p = strchr(p0,'(')) != NULL) { 381 ++p; 382 if((p0 = strchr(p,')')) == NULL) 383 return -1; 384 385 size = p0 - p; 386 if(size >= sizeof(s)) 387 return -1; 388 389 snprintf(s, sizeof(s), "%.*s", size, p); 390 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD) 391 return -1; 392 for (i = 0; i < _NUM_FLD; i++){ 393 errno = 0; 394 int_fld[i] = strtoul(str_fld[i], &end, 0); 395 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255) 396 return -1; 397 } 398 if (nb_lcore_params >= MAX_LCORE_PARAMS) { 399 printf("exceeded max number of lcore params: %hu\n", 400 nb_lcore_params); 401 return -1; 402 } 403 lcore_params_array[nb_lcore_params].port_id = 404 (uint8_t)int_fld[FLD_PORT]; 405 lcore_params_array[nb_lcore_params].queue_id = 406 (uint8_t)int_fld[FLD_QUEUE]; 407 lcore_params_array[nb_lcore_params].lcore_id = 408 (uint8_t)int_fld[FLD_LCORE]; 409 ++nb_lcore_params; 410 } 411 lcore_params = lcore_params_array; 412 return 0; 413 } 414 415 static void 416 parse_eth_dest(const char *optarg) 417 { 418 uint16_t portid; 419 char *port_end; 420 uint8_t c, *dest, peer_addr[6]; 421 422 errno = 0; 423 portid = strtoul(optarg, &port_end, 10); 424 if (errno != 0 || port_end == optarg || *port_end++ != ',') 425 rte_exit(EXIT_FAILURE, 426 "Invalid eth-dest: %s", optarg); 427 if (portid >= RTE_MAX_ETHPORTS) 428 rte_exit(EXIT_FAILURE, 429 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n", 430 portid, RTE_MAX_ETHPORTS); 431 432 if (cmdline_parse_etheraddr(NULL, port_end, 433 &peer_addr, sizeof(peer_addr)) < 0) 434 rte_exit(EXIT_FAILURE, 435 "Invalid ethernet address: %s\n", 436 port_end); 437 dest = (uint8_t *)&dest_eth_addr[portid]; 438 for (c = 0; c < 6; c++) 439 dest[c] = peer_addr[c]; 440 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid]; 441 } 442 443 #define MAX_JUMBO_PKT_LEN 9600 444 #define MEMPOOL_CACHE_SIZE 256 445 446 static const char short_options[] = 447 "p:" /* portmask */ 448 "P" /* promiscuous */ 449 "L" /* enable long prefix match */ 450 "E" /* enable exact match */ 451 ; 452 453 #define CMD_LINE_OPT_CONFIG "config" 454 #define CMD_LINE_OPT_ETH_DEST "eth-dest" 455 #define CMD_LINE_OPT_NO_NUMA "no-numa" 456 #define CMD_LINE_OPT_IPV6 "ipv6" 457 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo" 458 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num" 459 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype" 460 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool" 461 enum { 462 /* long options mapped to a short option */ 463 464 /* first long only option value must be >= 256, so that we won't 465 * conflict with short options */ 466 CMD_LINE_OPT_MIN_NUM = 256, 467 CMD_LINE_OPT_CONFIG_NUM, 468 CMD_LINE_OPT_ETH_DEST_NUM, 469 CMD_LINE_OPT_NO_NUMA_NUM, 470 CMD_LINE_OPT_IPV6_NUM, 471 CMD_LINE_OPT_ENABLE_JUMBO_NUM, 472 CMD_LINE_OPT_HASH_ENTRY_NUM_NUM, 473 CMD_LINE_OPT_PARSE_PTYPE_NUM, 474 CMD_LINE_OPT_PARSE_PER_PORT_POOL, 475 }; 476 477 static const struct option lgopts[] = { 478 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM}, 479 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM}, 480 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM}, 481 {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM}, 482 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM}, 483 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM}, 484 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM}, 485 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL}, 486 {NULL, 0, 0, 0} 487 }; 488 489 /* 490 * This expression is used to calculate the number of mbufs needed 491 * depending on user input, taking into account memory for rx and 492 * tx hardware rings, cache per lcore and mtable per port per lcore. 493 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum 494 * value of 8192 495 */ 496 #define NB_MBUF(nports) RTE_MAX( \ 497 (nports*nb_rx_queue*nb_rxd + \ 498 nports*nb_lcores*MAX_PKT_BURST + \ 499 nports*n_tx_queue*nb_txd + \ 500 nb_lcores*MEMPOOL_CACHE_SIZE), \ 501 (unsigned)8192) 502 503 /* Parse the argument given in the command line of the application */ 504 static int 505 parse_args(int argc, char **argv) 506 { 507 int opt, ret; 508 char **argvopt; 509 int option_index; 510 char *prgname = argv[0]; 511 512 argvopt = argv; 513 514 /* Error or normal output strings. */ 515 while ((opt = getopt_long(argc, argvopt, short_options, 516 lgopts, &option_index)) != EOF) { 517 518 switch (opt) { 519 /* portmask */ 520 case 'p': 521 enabled_port_mask = parse_portmask(optarg); 522 if (enabled_port_mask == 0) { 523 fprintf(stderr, "Invalid portmask\n"); 524 print_usage(prgname); 525 return -1; 526 } 527 break; 528 529 case 'P': 530 promiscuous_on = 1; 531 break; 532 533 case 'E': 534 l3fwd_em_on = 1; 535 break; 536 537 case 'L': 538 l3fwd_lpm_on = 1; 539 break; 540 541 /* long options */ 542 case CMD_LINE_OPT_CONFIG_NUM: 543 ret = parse_config(optarg); 544 if (ret) { 545 fprintf(stderr, "Invalid config\n"); 546 print_usage(prgname); 547 return -1; 548 } 549 break; 550 551 case CMD_LINE_OPT_ETH_DEST_NUM: 552 parse_eth_dest(optarg); 553 break; 554 555 case CMD_LINE_OPT_NO_NUMA_NUM: 556 numa_on = 0; 557 break; 558 559 case CMD_LINE_OPT_IPV6_NUM: 560 ipv6 = 1; 561 break; 562 563 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: { 564 const struct option lenopts = { 565 "max-pkt-len", required_argument, 0, 0 566 }; 567 568 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME; 569 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS; 570 571 /* 572 * if no max-pkt-len set, use the default 573 * value RTE_ETHER_MAX_LEN. 574 */ 575 if (getopt_long(argc, argvopt, "", 576 &lenopts, &option_index) == 0) { 577 ret = parse_max_pkt_len(optarg); 578 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) { 579 fprintf(stderr, 580 "invalid maximum packet length\n"); 581 print_usage(prgname); 582 return -1; 583 } 584 port_conf.rxmode.max_rx_pkt_len = ret; 585 } 586 break; 587 } 588 589 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM: 590 ret = parse_hash_entry_number(optarg); 591 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) { 592 hash_entry_number = ret; 593 } else { 594 fprintf(stderr, "invalid hash entry number\n"); 595 print_usage(prgname); 596 return -1; 597 } 598 break; 599 600 case CMD_LINE_OPT_PARSE_PTYPE_NUM: 601 printf("soft parse-ptype is enabled\n"); 602 parse_ptype = 1; 603 break; 604 605 case CMD_LINE_OPT_PARSE_PER_PORT_POOL: 606 printf("per port buffer pool is enabled\n"); 607 per_port_pool = 1; 608 break; 609 610 default: 611 print_usage(prgname); 612 return -1; 613 } 614 } 615 616 /* If both LPM and EM are selected, return error. */ 617 if (l3fwd_lpm_on && l3fwd_em_on) { 618 fprintf(stderr, "LPM and EM are mutually exclusive, select only one\n"); 619 return -1; 620 } 621 622 /* 623 * Nothing is selected, pick longest-prefix match 624 * as default match. 625 */ 626 if (!l3fwd_lpm_on && !l3fwd_em_on) { 627 fprintf(stderr, "LPM or EM none selected, default LPM on\n"); 628 l3fwd_lpm_on = 1; 629 } 630 631 /* 632 * ipv6 and hash flags are valid only for 633 * exact macth, reset them to default for 634 * longest-prefix match. 635 */ 636 if (l3fwd_lpm_on) { 637 ipv6 = 0; 638 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT; 639 } 640 641 if (optind >= 0) 642 argv[optind-1] = prgname; 643 644 ret = optind-1; 645 optind = 1; /* reset getopt lib */ 646 return ret; 647 } 648 649 static void 650 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr) 651 { 652 char buf[RTE_ETHER_ADDR_FMT_SIZE]; 653 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr); 654 printf("%s%s", name, buf); 655 } 656 657 static int 658 init_mem(uint16_t portid, unsigned int nb_mbuf) 659 { 660 struct lcore_conf *qconf; 661 int socketid; 662 unsigned lcore_id; 663 char s[64]; 664 665 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 666 if (rte_lcore_is_enabled(lcore_id) == 0) 667 continue; 668 669 if (numa_on) 670 socketid = rte_lcore_to_socket_id(lcore_id); 671 else 672 socketid = 0; 673 674 if (socketid >= NB_SOCKETS) { 675 rte_exit(EXIT_FAILURE, 676 "Socket %d of lcore %u is out of range %d\n", 677 socketid, lcore_id, NB_SOCKETS); 678 } 679 680 if (pktmbuf_pool[portid][socketid] == NULL) { 681 snprintf(s, sizeof(s), "mbuf_pool_%d:%d", 682 portid, socketid); 683 pktmbuf_pool[portid][socketid] = 684 rte_pktmbuf_pool_create(s, nb_mbuf, 685 MEMPOOL_CACHE_SIZE, 0, 686 RTE_MBUF_DEFAULT_BUF_SIZE, socketid); 687 if (pktmbuf_pool[portid][socketid] == NULL) 688 rte_exit(EXIT_FAILURE, 689 "Cannot init mbuf pool on socket %d\n", 690 socketid); 691 else 692 printf("Allocated mbuf pool on socket %d\n", 693 socketid); 694 695 /* Setup either LPM or EM(f.e Hash). But, only once per 696 * available socket. 697 */ 698 if (!lkp_per_socket[socketid]) { 699 l3fwd_lkp.setup(socketid); 700 lkp_per_socket[socketid] = 1; 701 } 702 } 703 qconf = &lcore_conf[lcore_id]; 704 qconf->ipv4_lookup_struct = 705 l3fwd_lkp.get_ipv4_lookup_struct(socketid); 706 qconf->ipv6_lookup_struct = 707 l3fwd_lkp.get_ipv6_lookup_struct(socketid); 708 } 709 return 0; 710 } 711 712 /* Check the link status of all ports in up to 9s, and print them finally */ 713 static void 714 check_all_ports_link_status(uint32_t port_mask) 715 { 716 #define CHECK_INTERVAL 100 /* 100ms */ 717 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 718 uint16_t portid; 719 uint8_t count, all_ports_up, print_flag = 0; 720 struct rte_eth_link link; 721 int ret; 722 723 printf("\nChecking link status"); 724 fflush(stdout); 725 for (count = 0; count <= MAX_CHECK_TIME; count++) { 726 if (force_quit) 727 return; 728 all_ports_up = 1; 729 RTE_ETH_FOREACH_DEV(portid) { 730 if (force_quit) 731 return; 732 if ((port_mask & (1 << portid)) == 0) 733 continue; 734 memset(&link, 0, sizeof(link)); 735 ret = rte_eth_link_get_nowait(portid, &link); 736 if (ret < 0) { 737 all_ports_up = 0; 738 if (print_flag == 1) 739 printf("Port %u link get failed: %s\n", 740 portid, rte_strerror(-ret)); 741 continue; 742 } 743 /* print link status if flag set */ 744 if (print_flag == 1) { 745 if (link.link_status) 746 printf( 747 "Port%d Link Up. Speed %u Mbps -%s\n", 748 portid, link.link_speed, 749 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ? 750 ("full-duplex") : ("half-duplex\n")); 751 else 752 printf("Port %d Link Down\n", portid); 753 continue; 754 } 755 /* clear all_ports_up flag if any link down */ 756 if (link.link_status == ETH_LINK_DOWN) { 757 all_ports_up = 0; 758 break; 759 } 760 } 761 /* after finally printing all link status, get out */ 762 if (print_flag == 1) 763 break; 764 765 if (all_ports_up == 0) { 766 printf("."); 767 fflush(stdout); 768 rte_delay_ms(CHECK_INTERVAL); 769 } 770 771 /* set the print_flag if all ports up or timeout */ 772 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 773 print_flag = 1; 774 printf("done\n"); 775 } 776 } 777 } 778 779 static void 780 signal_handler(int signum) 781 { 782 if (signum == SIGINT || signum == SIGTERM) { 783 printf("\n\nSignal %d received, preparing to exit...\n", 784 signum); 785 force_quit = true; 786 } 787 } 788 789 static int 790 prepare_ptype_parser(uint16_t portid, uint16_t queueid) 791 { 792 if (parse_ptype) { 793 printf("Port %d: softly parse packet type info\n", portid); 794 if (rte_eth_add_rx_callback(portid, queueid, 795 l3fwd_lkp.cb_parse_ptype, 796 NULL)) 797 return 1; 798 799 printf("Failed to add rx callback: port=%d\n", portid); 800 return 0; 801 } 802 803 if (l3fwd_lkp.check_ptype(portid)) 804 return 1; 805 806 printf("port %d cannot parse packet type, please add --%s\n", 807 portid, CMD_LINE_OPT_PARSE_PTYPE); 808 return 0; 809 } 810 811 int 812 main(int argc, char **argv) 813 { 814 struct lcore_conf *qconf; 815 struct rte_eth_dev_info dev_info; 816 struct rte_eth_txconf *txconf; 817 int ret; 818 unsigned nb_ports; 819 uint16_t queueid, portid; 820 unsigned lcore_id; 821 uint32_t n_tx_queue, nb_lcores; 822 uint8_t nb_rx_queue, queue, socketid; 823 824 /* init EAL */ 825 ret = rte_eal_init(argc, argv); 826 if (ret < 0) 827 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n"); 828 argc -= ret; 829 argv += ret; 830 831 force_quit = false; 832 signal(SIGINT, signal_handler); 833 signal(SIGTERM, signal_handler); 834 835 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */ 836 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 837 dest_eth_addr[portid] = 838 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40); 839 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid]; 840 } 841 842 /* parse application arguments (after the EAL ones) */ 843 ret = parse_args(argc, argv); 844 if (ret < 0) 845 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n"); 846 847 if (check_lcore_params() < 0) 848 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n"); 849 850 ret = init_lcore_rx_queues(); 851 if (ret < 0) 852 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n"); 853 854 nb_ports = rte_eth_dev_count_avail(); 855 856 if (check_port_config() < 0) 857 rte_exit(EXIT_FAILURE, "check_port_config failed\n"); 858 859 nb_lcores = rte_lcore_count(); 860 861 /* Setup function pointers for lookup method. */ 862 setup_l3fwd_lookup_tables(); 863 864 /* initialize all ports */ 865 RTE_ETH_FOREACH_DEV(portid) { 866 struct rte_eth_conf local_port_conf = port_conf; 867 868 /* skip ports that are not enabled */ 869 if ((enabled_port_mask & (1 << portid)) == 0) { 870 printf("\nSkipping disabled port %d\n", portid); 871 continue; 872 } 873 874 /* init port */ 875 printf("Initializing port %d ... ", portid ); 876 fflush(stdout); 877 878 nb_rx_queue = get_port_n_rx_queues(portid); 879 n_tx_queue = nb_lcores; 880 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT) 881 n_tx_queue = MAX_TX_QUEUE_PER_PORT; 882 printf("Creating queues: nb_rxq=%d nb_txq=%u... ", 883 nb_rx_queue, (unsigned)n_tx_queue ); 884 885 ret = rte_eth_dev_info_get(portid, &dev_info); 886 if (ret != 0) 887 rte_exit(EXIT_FAILURE, 888 "Error during getting device (port %u) info: %s\n", 889 portid, strerror(-ret)); 890 891 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) 892 local_port_conf.txmode.offloads |= 893 DEV_TX_OFFLOAD_MBUF_FAST_FREE; 894 895 local_port_conf.rx_adv_conf.rss_conf.rss_hf &= 896 dev_info.flow_type_rss_offloads; 897 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf != 898 port_conf.rx_adv_conf.rss_conf.rss_hf) { 899 printf("Port %u modified RSS hash function based on hardware support," 900 "requested:%#"PRIx64" configured:%#"PRIx64"\n", 901 portid, 902 port_conf.rx_adv_conf.rss_conf.rss_hf, 903 local_port_conf.rx_adv_conf.rss_conf.rss_hf); 904 } 905 906 ret = rte_eth_dev_configure(portid, nb_rx_queue, 907 (uint16_t)n_tx_queue, &local_port_conf); 908 if (ret < 0) 909 rte_exit(EXIT_FAILURE, 910 "Cannot configure device: err=%d, port=%d\n", 911 ret, portid); 912 913 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, 914 &nb_txd); 915 if (ret < 0) 916 rte_exit(EXIT_FAILURE, 917 "Cannot adjust number of descriptors: err=%d, " 918 "port=%d\n", ret, portid); 919 920 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]); 921 if (ret < 0) 922 rte_exit(EXIT_FAILURE, 923 "Cannot get MAC address: err=%d, port=%d\n", 924 ret, portid); 925 926 print_ethaddr(" Address:", &ports_eth_addr[portid]); 927 printf(", "); 928 print_ethaddr("Destination:", 929 (const struct rte_ether_addr *)&dest_eth_addr[portid]); 930 printf(", "); 931 932 /* 933 * prepare src MACs for each port. 934 */ 935 rte_ether_addr_copy(&ports_eth_addr[portid], 936 (struct rte_ether_addr *)(val_eth + portid) + 1); 937 938 /* init memory */ 939 if (!per_port_pool) { 940 /* portid = 0; this is *not* signifying the first port, 941 * rather, it signifies that portid is ignored. 942 */ 943 ret = init_mem(0, NB_MBUF(nb_ports)); 944 } else { 945 ret = init_mem(portid, NB_MBUF(1)); 946 } 947 if (ret < 0) 948 rte_exit(EXIT_FAILURE, "init_mem failed\n"); 949 950 /* init one TX queue per couple (lcore,port) */ 951 queueid = 0; 952 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 953 if (rte_lcore_is_enabled(lcore_id) == 0) 954 continue; 955 956 if (numa_on) 957 socketid = 958 (uint8_t)rte_lcore_to_socket_id(lcore_id); 959 else 960 socketid = 0; 961 962 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid); 963 fflush(stdout); 964 965 txconf = &dev_info.default_txconf; 966 txconf->offloads = local_port_conf.txmode.offloads; 967 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd, 968 socketid, txconf); 969 if (ret < 0) 970 rte_exit(EXIT_FAILURE, 971 "rte_eth_tx_queue_setup: err=%d, " 972 "port=%d\n", ret, portid); 973 974 qconf = &lcore_conf[lcore_id]; 975 qconf->tx_queue_id[portid] = queueid; 976 queueid++; 977 978 qconf->tx_port_id[qconf->n_tx_port] = portid; 979 qconf->n_tx_port++; 980 } 981 printf("\n"); 982 } 983 984 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 985 if (rte_lcore_is_enabled(lcore_id) == 0) 986 continue; 987 qconf = &lcore_conf[lcore_id]; 988 printf("\nInitializing rx queues on lcore %u ... ", lcore_id ); 989 fflush(stdout); 990 /* init RX queues */ 991 for(queue = 0; queue < qconf->n_rx_queue; ++queue) { 992 struct rte_eth_rxconf rxq_conf; 993 994 portid = qconf->rx_queue_list[queue].port_id; 995 queueid = qconf->rx_queue_list[queue].queue_id; 996 997 if (numa_on) 998 socketid = 999 (uint8_t)rte_lcore_to_socket_id(lcore_id); 1000 else 1001 socketid = 0; 1002 1003 printf("rxq=%d,%d,%d ", portid, queueid, socketid); 1004 fflush(stdout); 1005 1006 ret = rte_eth_dev_info_get(portid, &dev_info); 1007 if (ret != 0) 1008 rte_exit(EXIT_FAILURE, 1009 "Error during getting device (port %u) info: %s\n", 1010 portid, strerror(-ret)); 1011 1012 rxq_conf = dev_info.default_rxconf; 1013 rxq_conf.offloads = port_conf.rxmode.offloads; 1014 if (!per_port_pool) 1015 ret = rte_eth_rx_queue_setup(portid, queueid, 1016 nb_rxd, socketid, 1017 &rxq_conf, 1018 pktmbuf_pool[0][socketid]); 1019 else 1020 ret = rte_eth_rx_queue_setup(portid, queueid, 1021 nb_rxd, socketid, 1022 &rxq_conf, 1023 pktmbuf_pool[portid][socketid]); 1024 if (ret < 0) 1025 rte_exit(EXIT_FAILURE, 1026 "rte_eth_rx_queue_setup: err=%d, port=%d\n", 1027 ret, portid); 1028 } 1029 } 1030 1031 printf("\n"); 1032 1033 /* start ports */ 1034 RTE_ETH_FOREACH_DEV(portid) { 1035 if ((enabled_port_mask & (1 << portid)) == 0) { 1036 continue; 1037 } 1038 /* Start device */ 1039 ret = rte_eth_dev_start(portid); 1040 if (ret < 0) 1041 rte_exit(EXIT_FAILURE, 1042 "rte_eth_dev_start: err=%d, port=%d\n", 1043 ret, portid); 1044 1045 /* 1046 * If enabled, put device in promiscuous mode. 1047 * This allows IO forwarding mode to forward packets 1048 * to itself through 2 cross-connected ports of the 1049 * target machine. 1050 */ 1051 if (promiscuous_on) { 1052 ret = rte_eth_promiscuous_enable(portid); 1053 if (ret != 0) 1054 rte_exit(EXIT_FAILURE, 1055 "rte_eth_promiscuous_enable: err=%s, port=%u\n", 1056 rte_strerror(-ret), portid); 1057 } 1058 } 1059 1060 printf("\n"); 1061 1062 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1063 if (rte_lcore_is_enabled(lcore_id) == 0) 1064 continue; 1065 qconf = &lcore_conf[lcore_id]; 1066 for (queue = 0; queue < qconf->n_rx_queue; ++queue) { 1067 portid = qconf->rx_queue_list[queue].port_id; 1068 queueid = qconf->rx_queue_list[queue].queue_id; 1069 if (prepare_ptype_parser(portid, queueid) == 0) 1070 rte_exit(EXIT_FAILURE, "ptype check fails\n"); 1071 } 1072 } 1073 1074 1075 check_all_ports_link_status(enabled_port_mask); 1076 1077 ret = 0; 1078 /* launch per-lcore init on every lcore */ 1079 rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MASTER); 1080 RTE_LCORE_FOREACH_SLAVE(lcore_id) { 1081 if (rte_eal_wait_lcore(lcore_id) < 0) { 1082 ret = -1; 1083 break; 1084 } 1085 } 1086 1087 /* stop ports */ 1088 RTE_ETH_FOREACH_DEV(portid) { 1089 if ((enabled_port_mask & (1 << portid)) == 0) 1090 continue; 1091 printf("Closing port %d...", portid); 1092 rte_eth_dev_stop(portid); 1093 rte_eth_dev_close(portid); 1094 printf(" Done\n"); 1095 } 1096 printf("Bye...\n"); 1097 1098 return ret; 1099 } 1100