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