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