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