1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(C) 2020 Marvell International Ltd. 3 */ 4 5 #include <arpa/inet.h> 6 #include <errno.h> 7 #include <getopt.h> 8 #include <inttypes.h> 9 #include <signal.h> 10 #include <stdarg.h> 11 #include <stdbool.h> 12 #include <stdint.h> 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <sys/socket.h> 17 #include <sys/types.h> 18 #include <sys/queue.h> 19 #include <unistd.h> 20 21 #include <rte_branch_prediction.h> 22 #include <rte_common.h> 23 #include <rte_cycles.h> 24 #include <rte_eal.h> 25 #include <rte_ethdev.h> 26 #define RTE_GRAPH_MODEL_SELECT RTE_GRAPH_MODEL_RTC 27 #include <rte_graph_worker.h> 28 #include <rte_launch.h> 29 #include <rte_lcore.h> 30 #include <rte_log.h> 31 #include <rte_lpm6.h> 32 #include <rte_mempool.h> 33 #include <rte_node_eth_api.h> 34 #include <rte_node_ip4_api.h> 35 #include <rte_node_ip6_api.h> 36 #include <rte_per_lcore.h> 37 #include <rte_string_fns.h> 38 #include <rte_vect.h> 39 40 #include <cmdline_parse.h> 41 #include <cmdline_parse_etheraddr.h> 42 43 /* Log type */ 44 #define RTE_LOGTYPE_L3FWD_GRAPH RTE_LOGTYPE_USER1 45 46 /* 47 * Configurable number of RX/TX ring descriptors 48 */ 49 #define RX_DESC_DEFAULT 1024 50 #define TX_DESC_DEFAULT 1024 51 52 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS 53 #define MAX_RX_QUEUE_PER_PORT 128 54 55 #define MAX_RX_QUEUE_PER_LCORE 16 56 57 #define MAX_LCORE_PARAMS 1024 58 59 #define NB_SOCKETS 8 60 61 /* Graph module */ 62 #define WORKER_MODEL_RTC "rtc" 63 #define WORKER_MODEL_MCORE_DISPATCH "dispatch" 64 /* Static global variables used within this file. */ 65 static uint16_t nb_rxd = RX_DESC_DEFAULT; 66 static uint16_t nb_txd = TX_DESC_DEFAULT; 67 68 /**< Ports set in promiscuous mode off by default. */ 69 static int promiscuous_on; 70 71 static int numa_on = 1; /**< NUMA is enabled by default. */ 72 static int per_port_pool; /**< Use separate buffer pools per port; disabled */ 73 /**< by default */ 74 75 static volatile bool force_quit; 76 77 /* Ethernet addresses of ports */ 78 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS]; 79 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS]; 80 xmm_t val_eth[RTE_MAX_ETHPORTS]; 81 82 /* Mask of enabled ports */ 83 static uint32_t enabled_port_mask; 84 85 /* Pcap trace */ 86 static char pcap_filename[RTE_GRAPH_PCAP_FILE_SZ]; 87 static uint64_t packet_to_capture; 88 static int pcap_trace_enable; 89 90 91 struct lcore_rx_queue { 92 uint16_t port_id; 93 uint16_t queue_id; 94 char node_name[RTE_NODE_NAMESIZE]; 95 }; 96 97 static uint8_t model_conf = RTE_GRAPH_MODEL_DEFAULT; 98 99 /* Lcore conf */ 100 struct __rte_cache_aligned lcore_conf { 101 uint16_t n_rx_queue; 102 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE]; 103 104 struct rte_graph *graph; 105 char name[RTE_GRAPH_NAMESIZE]; 106 rte_graph_t graph_id; 107 }; 108 109 static struct lcore_conf lcore_conf[RTE_MAX_LCORE]; 110 111 struct __rte_cache_aligned lcore_params { 112 uint16_t port_id; 113 uint16_t queue_id; 114 uint32_t lcore_id; 115 }; 116 117 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS]; 118 static struct lcore_params lcore_params_array_default[] = { 119 {0, 0, 2}, {0, 1, 2}, {0, 2, 2}, {1, 0, 2}, {1, 1, 2}, 120 {1, 2, 2}, {2, 0, 2}, {3, 0, 3}, {3, 1, 3}, 121 }; 122 123 static struct lcore_params *lcore_params = lcore_params_array_default; 124 static uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default); 125 126 static struct rte_eth_conf port_conf = { 127 .rxmode = { 128 .mq_mode = RTE_ETH_MQ_RX_RSS, 129 }, 130 .rx_adv_conf = { 131 .rss_conf = { 132 .rss_key = NULL, 133 .rss_hf = RTE_ETH_RSS_IP, 134 }, 135 }, 136 .txmode = { 137 .mq_mode = RTE_ETH_MQ_TX_NONE, 138 }, 139 }; 140 141 static uint32_t max_pkt_len; 142 143 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS]; 144 145 static struct rte_node_ethdev_config ethdev_conf[RTE_MAX_ETHPORTS]; 146 147 struct ipv4_l3fwd_lpm_route { 148 uint32_t ip; 149 uint8_t depth; 150 uint8_t if_out; 151 }; 152 153 struct ipv6_l3fwd_lpm_route { 154 struct rte_ipv6_addr ip; 155 uint8_t depth; 156 uint8_t if_out; 157 }; 158 159 #define IPV4_L3FWD_LPM_NUM_ROUTES \ 160 (sizeof(ipv4_l3fwd_lpm_route_array) / \ 161 sizeof(ipv4_l3fwd_lpm_route_array[0])) 162 /* 198.18.0.0/16 are set aside for RFC2544 benchmarking. */ 163 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = { 164 {RTE_IPV4(198, 18, 0, 0), 24, 0}, {RTE_IPV4(198, 18, 1, 0), 24, 1}, 165 {RTE_IPV4(198, 18, 2, 0), 24, 2}, {RTE_IPV4(198, 18, 3, 0), 24, 3}, 166 {RTE_IPV4(198, 18, 4, 0), 24, 4}, {RTE_IPV4(198, 18, 5, 0), 24, 5}, 167 {RTE_IPV4(198, 18, 6, 0), 24, 6}, {RTE_IPV4(198, 18, 7, 0), 24, 7}, 168 }; 169 170 #define IPV6_L3FWD_LPM_NUM_ROUTES \ 171 (sizeof(ipv6_l3fwd_lpm_route_array) / \ 172 sizeof(ipv6_l3fwd_lpm_route_array[0])) 173 174 static struct ipv6_l3fwd_lpm_route ipv6_l3fwd_lpm_route_array[] = { 175 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 0), 48, 0}, 176 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 1), 48, 1}, 177 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 2), 48, 2}, 178 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 3), 48, 3}, 179 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 4), 48, 4}, 180 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 5), 48, 5}, 181 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 6), 48, 6}, 182 {RTE_IPV6(0x2001, 0xdb08, 0x1234, 0, 0, 0, 0, 2), 48, 7}, 183 }; 184 185 static int 186 check_worker_model_params(void) 187 { 188 if (model_conf == RTE_GRAPH_MODEL_MCORE_DISPATCH && 189 nb_lcore_params > 1) { 190 printf("Exceeded max number of lcore params for remote model: %hu\n", 191 nb_lcore_params); 192 return -1; 193 } 194 195 return 0; 196 } 197 198 static int 199 check_lcore_params(void) 200 { 201 uint16_t queue, i; 202 int socketid; 203 uint32_t lcore; 204 205 for (i = 0; i < nb_lcore_params; ++i) { 206 queue = lcore_params[i].queue_id; 207 if (queue >= MAX_RX_QUEUE_PER_PORT) { 208 printf("Invalid queue number: %" PRIu16 "\n", queue); 209 return -1; 210 } 211 lcore = lcore_params[i].lcore_id; 212 if (!rte_lcore_is_enabled(lcore)) { 213 printf("Error: lcore %u is not enabled in lcore mask\n", 214 lcore); 215 return -1; 216 } 217 218 if (lcore == rte_get_main_lcore()) { 219 printf("Error: lcore %u is main lcore\n", lcore); 220 return -1; 221 } 222 socketid = rte_lcore_to_socket_id(lcore); 223 if ((socketid != 0) && (numa_on == 0)) { 224 printf("Warning: lcore %u is on socket %d with numa off\n", 225 lcore, socketid); 226 } 227 } 228 229 return 0; 230 } 231 232 static int 233 check_port_config(void) 234 { 235 uint16_t portid; 236 uint16_t i; 237 238 for (i = 0; i < nb_lcore_params; ++i) { 239 portid = lcore_params[i].port_id; 240 if ((enabled_port_mask & (1 << portid)) == 0) { 241 printf("Port %u is not enabled in port mask\n", portid); 242 return -1; 243 } 244 if (!rte_eth_dev_is_valid_port(portid)) { 245 printf("Port %u is not present on the board\n", portid); 246 return -1; 247 } 248 } 249 250 return 0; 251 } 252 253 static uint16_t 254 get_port_n_rx_queues(const uint16_t port) 255 { 256 int queue = -1; 257 uint16_t i; 258 259 for (i = 0; i < nb_lcore_params; ++i) { 260 if (lcore_params[i].port_id == port) { 261 if (lcore_params[i].queue_id == queue + 1) 262 queue = lcore_params[i].queue_id; 263 else 264 rte_exit(EXIT_FAILURE, 265 "Queue ids of the port %d must be" 266 " in sequence and must start with 0\n", 267 lcore_params[i].port_id); 268 } 269 } 270 271 return (uint16_t)(++queue); 272 } 273 274 static int 275 init_lcore_rx_queues(void) 276 { 277 uint16_t i, nb_rx_queue; 278 uint32_t lcore; 279 280 for (i = 0; i < nb_lcore_params; ++i) { 281 lcore = lcore_params[i].lcore_id; 282 nb_rx_queue = lcore_conf[lcore].n_rx_queue; 283 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) { 284 printf("Error: too many queues (%u) for lcore: %u\n", 285 (unsigned int)nb_rx_queue + 1, 286 lcore); 287 return -1; 288 } 289 290 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id = 291 lcore_params[i].port_id; 292 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id = 293 lcore_params[i].queue_id; 294 lcore_conf[lcore].n_rx_queue++; 295 } 296 297 return 0; 298 } 299 300 /* Display usage */ 301 static void 302 print_usage(const char *prgname) 303 { 304 fprintf(stderr, 305 "%s [EAL options] --" 306 " -p PORTMASK" 307 " [-P]" 308 " --config (port,queue,lcore)[,(port,queue,lcore)]" 309 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]" 310 " [--max-pkt-len PKTLEN]" 311 " [--no-numa]" 312 " [--per-port-pool]" 313 " [--num-pkt-cap]\n\n" 314 315 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n" 316 " -P : Enable promiscuous mode\n" 317 " --config (port,queue,lcore): Rx queue configuration\n" 318 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for " 319 "port X\n" 320 " --max-pkt-len PKTLEN: maximum packet length in decimal (64-9600)\n" 321 " --model NAME: walking model name, dispatch or rtc(by default)\n" 322 " --no-numa: Disable numa awareness\n" 323 " --per-port-pool: Use separate buffer pool per port\n" 324 " --pcap-enable: Enables pcap capture\n" 325 " --pcap-num-cap NUMPKT: Number of packets to capture\n" 326 " --pcap-file-name NAME: Pcap file name\n\n", 327 prgname); 328 } 329 330 static uint64_t 331 parse_num_pkt_cap(const char *num_pkt_cap) 332 { 333 uint64_t num_pkt; 334 char *end = NULL; 335 336 /* Parse decimal string */ 337 num_pkt = strtoull(num_pkt_cap, &end, 10); 338 if ((num_pkt_cap[0] == '\0') || (end == NULL) || (*end != '\0')) 339 return 0; 340 341 if (num_pkt == 0) 342 return 0; 343 344 return num_pkt; 345 } 346 347 static int 348 parse_max_pkt_len(const char *pktlen) 349 { 350 unsigned long len; 351 char *end = NULL; 352 353 /* Parse decimal string */ 354 len = strtoul(pktlen, &end, 10); 355 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0')) 356 return -1; 357 358 if (len == 0) 359 return -1; 360 361 return len; 362 } 363 364 static void 365 parse_worker_model(const char *model) 366 { 367 if (strcmp(model, WORKER_MODEL_MCORE_DISPATCH) == 0) 368 model_conf = RTE_GRAPH_MODEL_MCORE_DISPATCH; 369 else if (strcmp(model, WORKER_MODEL_RTC) == 0) 370 model_conf = RTE_GRAPH_MODEL_RTC; 371 else 372 rte_exit(EXIT_FAILURE, "Invalid worker model: %s", model); 373 374 #if defined(RTE_GRAPH_MODEL_SELECT) 375 if (model_conf != RTE_GRAPH_MODEL_SELECT) 376 printf("Warning: model mismatch, will use the RTE_GRAPH_MODEL_SELECT model\n"); 377 model_conf = RTE_GRAPH_MODEL_SELECT; 378 #endif 379 } 380 381 static int 382 parse_portmask(const char *portmask) 383 { 384 char *end = NULL; 385 unsigned long pm; 386 387 /* Parse hexadecimal string */ 388 pm = strtoul(portmask, &end, 16); 389 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 390 return 0; 391 392 return pm; 393 } 394 395 static int 396 parse_config(const char *q_arg) 397 { 398 enum fieldnames { FLD_PORT = 0, FLD_QUEUE, FLD_LCORE, _NUM_FLD }; 399 unsigned long int_fld[_NUM_FLD]; 400 const char *p, *p0 = q_arg; 401 char *str_fld[_NUM_FLD]; 402 uint32_t size; 403 char s[256]; 404 char *end; 405 int i; 406 407 nb_lcore_params = 0; 408 409 while ((p = strchr(p0, '(')) != NULL) { 410 ++p; 411 p0 = strchr(p, ')'); 412 if (p0 == NULL) 413 return -1; 414 415 size = p0 - p; 416 if (size >= sizeof(s)) 417 return -1; 418 419 memcpy(s, p, size); 420 s[size] = '\0'; 421 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != 422 _NUM_FLD) 423 return -1; 424 for (i = 0; i < _NUM_FLD; i++) { 425 errno = 0; 426 int_fld[i] = strtoul(str_fld[i], &end, 0); 427 if (errno != 0 || end == str_fld[i]) 428 return -1; 429 } 430 431 if (nb_lcore_params >= MAX_LCORE_PARAMS) { 432 printf("Exceeded max number of lcore params: %hu\n", 433 nb_lcore_params); 434 return -1; 435 } 436 437 if (int_fld[FLD_PORT] >= RTE_MAX_ETHPORTS || 438 int_fld[FLD_LCORE] >= RTE_MAX_LCORE) { 439 printf("Invalid port/lcore id\n"); 440 return -1; 441 } 442 443 lcore_params_array[nb_lcore_params].port_id = 444 (uint16_t)int_fld[FLD_PORT]; 445 lcore_params_array[nb_lcore_params].queue_id = 446 (uint16_t)int_fld[FLD_QUEUE]; 447 lcore_params_array[nb_lcore_params].lcore_id = 448 (uint32_t)int_fld[FLD_LCORE]; 449 ++nb_lcore_params; 450 } 451 lcore_params = lcore_params_array; 452 453 return 0; 454 } 455 456 static void 457 parse_eth_dest(const char *optarg) 458 { 459 uint8_t c, *dest, peer_addr[6]; 460 uint16_t portid; 461 char *port_end; 462 463 errno = 0; 464 portid = strtoul(optarg, &port_end, 10); 465 if (errno != 0 || port_end == optarg || *port_end++ != ',') 466 rte_exit(EXIT_FAILURE, "Invalid eth-dest: %s", optarg); 467 if (portid >= RTE_MAX_ETHPORTS) 468 rte_exit(EXIT_FAILURE, 469 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n", portid, 470 RTE_MAX_ETHPORTS); 471 472 if (cmdline_parse_etheraddr(NULL, port_end, &peer_addr, 473 sizeof(peer_addr)) < 0) 474 rte_exit(EXIT_FAILURE, "Invalid ethernet address: %s\n", 475 port_end); 476 dest = (uint8_t *)&dest_eth_addr[portid]; 477 for (c = 0; c < 6; c++) 478 dest[c] = peer_addr[c]; 479 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid]; 480 } 481 482 #define MAX_JUMBO_PKT_LEN 9600 483 #define MEMPOOL_CACHE_SIZE 256 484 485 static const char short_options[] = "p:" /* portmask */ 486 "P" /* promiscuous */ 487 ; 488 489 #define CMD_LINE_OPT_CONFIG "config" 490 #define CMD_LINE_OPT_ETH_DEST "eth-dest" 491 #define CMD_LINE_OPT_NO_NUMA "no-numa" 492 #define CMD_LINE_OPT_MAX_PKT_LEN "max-pkt-len" 493 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool" 494 #define CMD_LINE_OPT_PCAP_ENABLE "pcap-enable" 495 #define CMD_LINE_OPT_NUM_PKT_CAP "pcap-num-cap" 496 #define CMD_LINE_OPT_PCAP_FILENAME "pcap-file-name" 497 #define CMD_LINE_OPT_WORKER_MODEL "model" 498 499 enum { 500 /* Long options mapped to a short option */ 501 502 /* First long only option value must be >= 256, so that we won't 503 * conflict with short options 504 */ 505 CMD_LINE_OPT_MIN_NUM = 256, 506 CMD_LINE_OPT_CONFIG_NUM, 507 CMD_LINE_OPT_ETH_DEST_NUM, 508 CMD_LINE_OPT_NO_NUMA_NUM, 509 CMD_LINE_OPT_MAX_PKT_LEN_NUM, 510 CMD_LINE_OPT_PARSE_PER_PORT_POOL, 511 CMD_LINE_OPT_PARSE_PCAP_ENABLE, 512 CMD_LINE_OPT_PARSE_NUM_PKT_CAP, 513 CMD_LINE_OPT_PCAP_FILENAME_CAP, 514 CMD_LINE_OPT_WORKER_MODEL_TYPE, 515 }; 516 517 static const struct option lgopts[] = { 518 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM}, 519 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM}, 520 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM}, 521 {CMD_LINE_OPT_MAX_PKT_LEN, 1, 0, CMD_LINE_OPT_MAX_PKT_LEN_NUM}, 522 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL}, 523 {CMD_LINE_OPT_PCAP_ENABLE, 0, 0, CMD_LINE_OPT_PARSE_PCAP_ENABLE}, 524 {CMD_LINE_OPT_NUM_PKT_CAP, 1, 0, CMD_LINE_OPT_PARSE_NUM_PKT_CAP}, 525 {CMD_LINE_OPT_PCAP_FILENAME, 1, 0, CMD_LINE_OPT_PCAP_FILENAME_CAP}, 526 {CMD_LINE_OPT_WORKER_MODEL, 1, 0, CMD_LINE_OPT_WORKER_MODEL_TYPE}, 527 {NULL, 0, 0, 0}, 528 }; 529 530 /* 531 * This expression is used to calculate the number of mbufs needed 532 * depending on user input, taking into account memory for rx and 533 * tx hardware rings, cache per lcore and mtable per port per lcore. 534 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum 535 * value of 8192 536 */ 537 #define NB_MBUF(nports) \ 538 RTE_MAX((nports * nb_rx_queue * nb_rxd + \ 539 nports * nb_lcores * RTE_GRAPH_BURST_SIZE + \ 540 nports * n_tx_queue * nb_txd + \ 541 nb_lcores * MEMPOOL_CACHE_SIZE), 8192u) 542 543 /* Parse the argument given in the command line of the application */ 544 static int 545 parse_args(int argc, char **argv) 546 { 547 char *prgname = argv[0]; 548 int option_index; 549 char **argvopt; 550 int opt, ret; 551 552 argvopt = argv; 553 554 /* Error or normal output strings. */ 555 while ((opt = getopt_long(argc, argvopt, short_options, lgopts, 556 &option_index)) != EOF) { 557 558 switch (opt) { 559 /* Portmask */ 560 case 'p': 561 enabled_port_mask = parse_portmask(optarg); 562 if (enabled_port_mask == 0) { 563 fprintf(stderr, "Invalid portmask\n"); 564 print_usage(prgname); 565 return -1; 566 } 567 break; 568 569 case 'P': 570 promiscuous_on = 1; 571 break; 572 573 /* Long options */ 574 case CMD_LINE_OPT_CONFIG_NUM: 575 ret = parse_config(optarg); 576 if (ret) { 577 fprintf(stderr, "Invalid config\n"); 578 print_usage(prgname); 579 return -1; 580 } 581 break; 582 583 case CMD_LINE_OPT_ETH_DEST_NUM: 584 parse_eth_dest(optarg); 585 break; 586 587 case CMD_LINE_OPT_NO_NUMA_NUM: 588 numa_on = 0; 589 break; 590 591 case CMD_LINE_OPT_MAX_PKT_LEN_NUM: { 592 max_pkt_len = parse_max_pkt_len(optarg); 593 break; 594 } 595 596 case CMD_LINE_OPT_PARSE_PER_PORT_POOL: 597 printf("Per port buffer pool is enabled\n"); 598 per_port_pool = 1; 599 break; 600 601 case CMD_LINE_OPT_PARSE_PCAP_ENABLE: 602 printf("Packet capture enabled\n"); 603 pcap_trace_enable = 1; 604 break; 605 606 case CMD_LINE_OPT_PARSE_NUM_PKT_CAP: 607 packet_to_capture = parse_num_pkt_cap(optarg); 608 printf("Number of packets to capture: %"PRIu64"\n", 609 packet_to_capture); 610 break; 611 612 case CMD_LINE_OPT_PCAP_FILENAME_CAP: 613 rte_strlcpy(pcap_filename, optarg, 614 sizeof(pcap_filename)); 615 printf("Pcap file name: %s\n", pcap_filename); 616 break; 617 618 case CMD_LINE_OPT_WORKER_MODEL_TYPE: 619 printf("Use new worker model: %s\n", optarg); 620 parse_worker_model(optarg); 621 break; 622 623 default: 624 print_usage(prgname); 625 return -1; 626 } 627 } 628 629 if (optind >= 0) 630 argv[optind - 1] = prgname; 631 ret = optind - 1; 632 optind = 1; /* Reset getopt lib */ 633 634 return ret; 635 } 636 637 static void 638 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr) 639 { 640 char buf[RTE_ETHER_ADDR_FMT_SIZE]; 641 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr); 642 printf("%s%s", name, buf); 643 } 644 645 static int 646 init_mem(uint16_t portid, uint32_t nb_mbuf) 647 { 648 uint32_t lcore_id; 649 int socketid; 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[portid][socketid] == NULL) { 668 snprintf(s, sizeof(s), "mbuf_pool_%d:%d", portid, 669 socketid); 670 /* Create a pool with priv size of a cacheline */ 671 pktmbuf_pool[portid][socketid] = 672 rte_pktmbuf_pool_create( 673 s, nb_mbuf, MEMPOOL_CACHE_SIZE, 674 RTE_CACHE_LINE_SIZE, 675 RTE_MBUF_DEFAULT_BUF_SIZE, socketid); 676 if (pktmbuf_pool[portid][socketid] == NULL) 677 rte_exit(EXIT_FAILURE, 678 "Cannot init mbuf pool on socket %d\n", 679 socketid); 680 else 681 printf("Allocated mbuf pool on socket %d\n", 682 socketid); 683 } 684 } 685 686 return 0; 687 } 688 689 /* Check the link status of all ports in up to 9s, and print them finally */ 690 static void 691 check_all_ports_link_status(uint32_t port_mask) 692 { 693 #define CHECK_INTERVAL 100 /* 100ms */ 694 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 695 uint8_t count, all_ports_up, print_flag = 0; 696 struct rte_eth_link link; 697 uint16_t portid; 698 int ret; 699 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN]; 700 701 printf("\nChecking link status"); 702 fflush(stdout); 703 for (count = 0; count <= MAX_CHECK_TIME; count++) { 704 if (force_quit) 705 return; 706 all_ports_up = 1; 707 RTE_ETH_FOREACH_DEV(portid) 708 { 709 if (force_quit) 710 return; 711 if ((port_mask & (1 << portid)) == 0) 712 continue; 713 memset(&link, 0, sizeof(link)); 714 ret = rte_eth_link_get_nowait(portid, &link); 715 if (ret < 0) { 716 all_ports_up = 0; 717 if (print_flag == 1) 718 printf("Port %u link get failed: %s\n", 719 portid, rte_strerror(-ret)); 720 continue; 721 } 722 /* Print link status if flag set */ 723 if (print_flag == 1) { 724 rte_eth_link_to_str(link_status_text, 725 sizeof(link_status_text), &link); 726 printf("Port %d %s\n", portid, 727 link_status_text); 728 continue; 729 } 730 /* Clear all_ports_up flag if any link down */ 731 if (link.link_status == RTE_ETH_LINK_DOWN) { 732 all_ports_up = 0; 733 break; 734 } 735 } 736 /* After finally printing all link status, get out */ 737 if (print_flag == 1) 738 break; 739 740 if (all_ports_up == 0) { 741 printf("."); 742 fflush(stdout); 743 rte_delay_ms(CHECK_INTERVAL); 744 } 745 746 /* Set the print_flag if all ports up or timeout */ 747 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 748 print_flag = 1; 749 printf("Done\n"); 750 } 751 } 752 } 753 754 static void 755 signal_handler(int signum) 756 { 757 if (signum == SIGINT || signum == SIGTERM) { 758 printf("\n\nSignal %d received, preparing to exit...\n", 759 signum); 760 force_quit = true; 761 } 762 } 763 764 static void 765 print_stats(void) 766 { 767 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'}; 768 const char clr[] = {27, '[', '2', 'J', '\0'}; 769 struct rte_graph_cluster_stats_param s_param; 770 struct rte_graph_cluster_stats *stats; 771 const char *pattern = "worker_*"; 772 773 /* Prepare stats object */ 774 memset(&s_param, 0, sizeof(s_param)); 775 s_param.f = stdout; 776 s_param.socket_id = SOCKET_ID_ANY; 777 s_param.graph_patterns = &pattern; 778 s_param.nb_graph_patterns = 1; 779 780 stats = rte_graph_cluster_stats_create(&s_param); 781 if (stats == NULL) 782 rte_exit(EXIT_FAILURE, "Unable to create stats object\n"); 783 784 while (!force_quit) { 785 /* Clear screen and move to top left */ 786 printf("%s%s", clr, topLeft); 787 rte_graph_cluster_stats_get(stats, 0); 788 rte_delay_ms(1E3); 789 } 790 791 rte_graph_cluster_stats_destroy(stats); 792 } 793 794 /* Main processing loop. 8< */ 795 static int 796 graph_main_loop(void *conf) 797 { 798 struct lcore_conf *qconf; 799 struct rte_graph *graph; 800 uint32_t lcore_id; 801 802 RTE_SET_USED(conf); 803 804 lcore_id = rte_lcore_id(); 805 qconf = &lcore_conf[lcore_id]; 806 graph = qconf->graph; 807 808 if (!graph) { 809 RTE_LOG(INFO, L3FWD_GRAPH, "Lcore %u has nothing to do\n", 810 lcore_id); 811 return 0; 812 } 813 814 RTE_LOG(INFO, L3FWD_GRAPH, 815 "Entering main loop on lcore %u, graph %s(%p)\n", lcore_id, 816 qconf->name, graph); 817 818 while (likely(!force_quit)) 819 rte_graph_walk(graph); 820 821 return 0; 822 } 823 /* >8 End of main processing loop. */ 824 825 static uint32_t 826 eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu) 827 { 828 uint32_t overhead_len; 829 830 if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu) 831 overhead_len = max_rx_pktlen - max_mtu; 832 else 833 overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN; 834 835 return overhead_len; 836 } 837 838 static int 839 config_port_max_pkt_len(struct rte_eth_conf *conf, 840 struct rte_eth_dev_info *dev_info) 841 { 842 uint32_t overhead_len; 843 844 if (max_pkt_len == 0) 845 return 0; 846 847 if (max_pkt_len < RTE_ETHER_MIN_LEN || max_pkt_len > MAX_JUMBO_PKT_LEN) 848 return -1; 849 850 overhead_len = eth_dev_get_overhead_len(dev_info->max_rx_pktlen, 851 dev_info->max_mtu); 852 conf->rxmode.mtu = max_pkt_len - overhead_len; 853 854 if (conf->rxmode.mtu > RTE_ETHER_MTU) 855 conf->txmode.offloads |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS; 856 857 return 0; 858 } 859 860 static void 861 graph_config_mcore_dispatch(struct rte_graph_param graph_conf) 862 { 863 uint16_t nb_patterns = graph_conf.nb_node_patterns; 864 int worker_count = rte_lcore_count() - 1; 865 int main_lcore_id = rte_get_main_lcore(); 866 rte_graph_t main_graph_id = 0; 867 struct rte_node *node_tmp; 868 struct lcore_conf *qconf; 869 struct rte_graph *graph; 870 rte_graph_t graph_id; 871 rte_graph_off_t off; 872 int n_rx_node = 0; 873 int worker_lcore; 874 rte_node_t count; 875 int i, j; 876 int ret; 877 878 for (j = 0; j < nb_lcore_params; j++) { 879 qconf = &lcore_conf[lcore_params[j].lcore_id]; 880 /* Add rx node patterns of all lcore */ 881 for (i = 0; i < qconf->n_rx_queue; i++) { 882 char *node_name = qconf->rx_queue_list[i].node_name; 883 unsigned int lcore_id = lcore_params[j].lcore_id; 884 885 graph_conf.node_patterns[nb_patterns + n_rx_node + i] = node_name; 886 n_rx_node++; 887 ret = rte_graph_model_mcore_dispatch_node_lcore_affinity_set(node_name, 888 lcore_id); 889 if (ret == 0) 890 printf("Set node %s affinity to lcore %u\n", node_name, 891 lcore_params[j].lcore_id); 892 } 893 } 894 895 graph_conf.nb_node_patterns = nb_patterns + n_rx_node; 896 graph_conf.socket_id = rte_lcore_to_socket_id(main_lcore_id); 897 898 qconf = &lcore_conf[main_lcore_id]; 899 snprintf(qconf->name, sizeof(qconf->name), "worker_%u", 900 main_lcore_id); 901 902 /* create main graph */ 903 main_graph_id = rte_graph_create(qconf->name, &graph_conf); 904 if (main_graph_id == RTE_GRAPH_ID_INVALID) 905 rte_exit(EXIT_FAILURE, 906 "rte_graph_create(): main_graph_id invalid for lcore %u\n", 907 main_lcore_id); 908 909 /* set the graph model for the main graph */ 910 rte_graph_worker_model_set(RTE_GRAPH_MODEL_MCORE_DISPATCH); 911 qconf->graph_id = main_graph_id; 912 qconf->graph = rte_graph_lookup(qconf->name); 913 if (!qconf->graph) 914 rte_exit(EXIT_FAILURE, 915 "rte_graph_lookup(): graph %s not found\n", 916 qconf->name); 917 918 graph = qconf->graph; 919 worker_lcore = lcore_params[nb_lcore_params - 1].lcore_id; 920 rte_graph_foreach_node(count, off, graph, node_tmp) { 921 /* Need to set the node Lcore affinity before clone graph for each lcore */ 922 if (node_tmp->dispatch.lcore_id == RTE_MAX_LCORE) { 923 worker_lcore = rte_get_next_lcore(worker_lcore, true, 1); 924 ret = rte_graph_model_mcore_dispatch_node_lcore_affinity_set(node_tmp->name, 925 worker_lcore); 926 if (ret == 0) 927 printf("Set node %s affinity to lcore %u\n", 928 node_tmp->name, worker_lcore); 929 } 930 } 931 932 worker_lcore = main_lcore_id; 933 for (i = 0; i < worker_count; i++) { 934 worker_lcore = rte_get_next_lcore(worker_lcore, true, 1); 935 936 qconf = &lcore_conf[worker_lcore]; 937 snprintf(qconf->name, sizeof(qconf->name), "cloned-%u", worker_lcore); 938 graph_id = rte_graph_clone(main_graph_id, qconf->name, &graph_conf); 939 ret = rte_graph_model_mcore_dispatch_core_bind(graph_id, worker_lcore); 940 if (ret == 0) 941 printf("bind graph %d to lcore %u\n", graph_id, worker_lcore); 942 943 /* full cloned graph name */ 944 snprintf(qconf->name, sizeof(qconf->name), "%s", 945 rte_graph_id_to_name(graph_id)); 946 qconf->graph_id = graph_id; 947 qconf->graph = rte_graph_lookup(qconf->name); 948 if (!qconf->graph) 949 rte_exit(EXIT_FAILURE, 950 "Failed to lookup graph %s\n", 951 qconf->name); 952 continue; 953 } 954 } 955 956 static void 957 graph_config_rtc(struct rte_graph_param graph_conf) 958 { 959 uint16_t nb_patterns = graph_conf.nb_node_patterns; 960 struct lcore_conf *qconf; 961 rte_graph_t graph_id; 962 uint32_t lcore_id; 963 rte_edge_t i; 964 965 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 966 if (rte_lcore_is_enabled(lcore_id) == 0) 967 continue; 968 969 qconf = &lcore_conf[lcore_id]; 970 /* Skip graph creation if no source exists */ 971 if (!qconf->n_rx_queue) 972 continue; 973 /* Add rx node patterns of this lcore */ 974 for (i = 0; i < qconf->n_rx_queue; i++) { 975 graph_conf.node_patterns[nb_patterns + i] = 976 qconf->rx_queue_list[i].node_name; 977 } 978 graph_conf.nb_node_patterns = nb_patterns + i; 979 graph_conf.socket_id = rte_lcore_to_socket_id(lcore_id); 980 snprintf(qconf->name, sizeof(qconf->name), "worker_%u", 981 lcore_id); 982 graph_id = rte_graph_create(qconf->name, &graph_conf); 983 if (graph_id == RTE_GRAPH_ID_INVALID) 984 rte_exit(EXIT_FAILURE, 985 "rte_graph_create(): graph_id invalid for lcore %u\n", 986 lcore_id); 987 qconf->graph_id = graph_id; 988 qconf->graph = rte_graph_lookup(qconf->name); 989 if (!qconf->graph) 990 rte_exit(EXIT_FAILURE, 991 "rte_graph_lookup(): graph %s not found\n", 992 qconf->name); 993 } 994 } 995 996 int 997 main(int argc, char **argv) 998 { 999 /* Rewrite data of src and dst ether addr */ 1000 uint8_t rewrite_data[2 * sizeof(struct rte_ether_addr)]; 1001 /* Graph initialization. 8< */ 1002 static const char * const default_patterns[] = { 1003 "ip4*", 1004 "ethdev_tx-*", 1005 "pkt_drop", 1006 }; 1007 uint8_t socketid; 1008 uint16_t nb_rx_queue, queue; 1009 struct rte_graph_param graph_conf; 1010 struct rte_eth_dev_info dev_info; 1011 uint32_t nb_ports, nb_conf = 0; 1012 uint32_t n_tx_queue, nb_lcores; 1013 struct rte_eth_txconf *txconf; 1014 uint16_t queueid, portid, i; 1015 const char **node_patterns; 1016 struct lcore_conf *qconf; 1017 uint16_t nb_graphs = 0; 1018 uint16_t nb_patterns; 1019 uint8_t rewrite_len; 1020 uint32_t lcore_id; 1021 int ret; 1022 1023 /* Init EAL */ 1024 ret = rte_eal_init(argc, argv); 1025 if (ret < 0) 1026 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n"); 1027 argc -= ret; 1028 argv += ret; 1029 1030 force_quit = false; 1031 signal(SIGINT, signal_handler); 1032 signal(SIGTERM, signal_handler); 1033 1034 /* Pre-init dst MACs for all ports to 02:00:00:00:00:xx */ 1035 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 1036 dest_eth_addr[portid] = 1037 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40); 1038 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid]; 1039 } 1040 1041 /* Parse application arguments (after the EAL ones) */ 1042 ret = parse_args(argc, argv); 1043 if (ret < 0) 1044 rte_exit(EXIT_FAILURE, "Invalid L3FWD_GRAPH parameters\n"); 1045 1046 if (check_lcore_params() < 0) 1047 rte_exit(EXIT_FAILURE, "check_lcore_params() failed\n"); 1048 1049 if (check_worker_model_params() < 0) 1050 rte_exit(EXIT_FAILURE, "check_worker_model_params() failed\n"); 1051 1052 ret = init_lcore_rx_queues(); 1053 if (ret < 0) 1054 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues() failed\n"); 1055 1056 if (check_port_config() < 0) 1057 rte_exit(EXIT_FAILURE, "check_port_config() failed\n"); 1058 1059 nb_ports = rte_eth_dev_count_avail(); 1060 nb_lcores = rte_lcore_count(); 1061 1062 /* Initialize all ports. 8< */ 1063 RTE_ETH_FOREACH_DEV(portid) 1064 { 1065 struct rte_eth_conf local_port_conf = port_conf; 1066 1067 /* Skip ports that are not enabled */ 1068 if ((enabled_port_mask & (1 << portid)) == 0) { 1069 printf("\nSkipping disabled port %d\n", portid); 1070 continue; 1071 } 1072 1073 /* Init port */ 1074 printf("Initializing port %d ... ", portid); 1075 fflush(stdout); 1076 1077 nb_rx_queue = get_port_n_rx_queues(portid); 1078 n_tx_queue = nb_lcores; 1079 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT) 1080 n_tx_queue = MAX_TX_QUEUE_PER_PORT; 1081 printf("Creating queues: nb_rxq=%d nb_txq=%u... ", 1082 nb_rx_queue, n_tx_queue); 1083 1084 ret = rte_eth_dev_info_get(portid, &dev_info); 1085 if (ret != 0) 1086 rte_exit(EXIT_FAILURE, 1087 "Unable to get info for port %u\n", portid); 1088 1089 ret = config_port_max_pkt_len(&local_port_conf, &dev_info); 1090 if (ret != 0) 1091 rte_exit(EXIT_FAILURE, 1092 "Invalid max packet length: %u (port %u)\n", 1093 max_pkt_len, portid); 1094 1095 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE) 1096 local_port_conf.txmode.offloads |= 1097 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE; 1098 1099 local_port_conf.rx_adv_conf.rss_conf.rss_hf &= 1100 dev_info.flow_type_rss_offloads; 1101 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf != 1102 port_conf.rx_adv_conf.rss_conf.rss_hf) { 1103 printf("Port %u modified RSS hash function based on " 1104 "hardware support," 1105 "requested:%#" PRIx64 " configured:%#" PRIx64 1106 "\n", 1107 portid, port_conf.rx_adv_conf.rss_conf.rss_hf, 1108 local_port_conf.rx_adv_conf.rss_conf.rss_hf); 1109 } 1110 1111 ret = rte_eth_dev_configure(portid, nb_rx_queue, 1112 n_tx_queue, &local_port_conf); 1113 if (ret < 0) 1114 rte_exit(EXIT_FAILURE, 1115 "Cannot configure device: err=%d, port=%d\n", 1116 ret, portid); 1117 1118 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, 1119 &nb_txd); 1120 if (ret < 0) 1121 rte_exit(EXIT_FAILURE, 1122 "Cannot adjust number of descriptors: err=%d, " 1123 "port=%d\n", 1124 ret, portid); 1125 1126 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]); 1127 print_ethaddr(" Address:", &ports_eth_addr[portid]); 1128 printf(", "); 1129 print_ethaddr( 1130 "Destination:", 1131 (const struct rte_ether_addr *)&dest_eth_addr[portid]); 1132 printf(", "); 1133 1134 /* 1135 * prepare src MACs for each port. 1136 */ 1137 rte_ether_addr_copy( 1138 &ports_eth_addr[portid], 1139 (struct rte_ether_addr *)(val_eth + portid) + 1); 1140 1141 /* Init memory */ 1142 if (!per_port_pool) { 1143 /* portid = 0; this is *not* signifying the first port, 1144 * rather, it signifies that portid is ignored. 1145 */ 1146 ret = init_mem(0, NB_MBUF(nb_ports)); 1147 } else { 1148 ret = init_mem(portid, NB_MBUF(1)); 1149 } 1150 if (ret < 0) 1151 rte_exit(EXIT_FAILURE, "init_mem() failed\n"); 1152 1153 /* Init one TX queue per couple (lcore,port) */ 1154 queueid = 0; 1155 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1156 if (rte_lcore_is_enabled(lcore_id) == 0) 1157 continue; 1158 1159 qconf = &lcore_conf[lcore_id]; 1160 1161 if (numa_on) 1162 socketid = (uint8_t)rte_lcore_to_socket_id( 1163 lcore_id); 1164 else 1165 socketid = 0; 1166 1167 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid); 1168 fflush(stdout); 1169 1170 txconf = &dev_info.default_txconf; 1171 txconf->offloads = local_port_conf.txmode.offloads; 1172 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd, 1173 socketid, txconf); 1174 if (ret < 0) 1175 rte_exit(EXIT_FAILURE, 1176 "rte_eth_tx_queue_setup: err=%d, " 1177 "port=%d\n", 1178 ret, portid); 1179 queueid++; 1180 } 1181 1182 /* Setup ethdev node config */ 1183 ethdev_conf[nb_conf].port_id = portid; 1184 ethdev_conf[nb_conf].num_rx_queues = nb_rx_queue; 1185 ethdev_conf[nb_conf].num_tx_queues = n_tx_queue; 1186 if (!per_port_pool) 1187 ethdev_conf[nb_conf].mp = pktmbuf_pool[0]; 1188 1189 else 1190 ethdev_conf[nb_conf].mp = pktmbuf_pool[portid]; 1191 ethdev_conf[nb_conf].mp_count = NB_SOCKETS; 1192 1193 nb_conf++; 1194 printf("\n"); 1195 } 1196 1197 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) { 1198 if (rte_lcore_is_enabled(lcore_id) == 0) 1199 continue; 1200 qconf = &lcore_conf[lcore_id]; 1201 printf("\nInitializing rx queues on lcore %u ... ", lcore_id); 1202 fflush(stdout); 1203 /* Init RX queues */ 1204 for (queue = 0; queue < qconf->n_rx_queue; ++queue) { 1205 struct rte_eth_rxconf rxq_conf; 1206 1207 portid = qconf->rx_queue_list[queue].port_id; 1208 queueid = qconf->rx_queue_list[queue].queue_id; 1209 1210 if (numa_on) 1211 socketid = (uint8_t)rte_lcore_to_socket_id( 1212 lcore_id); 1213 else 1214 socketid = 0; 1215 1216 printf("rxq=%d,%d,%d ", portid, queueid, socketid); 1217 fflush(stdout); 1218 1219 ret = rte_eth_dev_info_get(portid, &dev_info); 1220 if (ret < 0) 1221 rte_exit(EXIT_FAILURE, 1222 "rte_eth_dev_info_get: err=%d, port=%u\n", 1223 ret, portid); 1224 1225 rxq_conf = dev_info.default_rxconf; 1226 rxq_conf.offloads = port_conf.rxmode.offloads; 1227 if (!per_port_pool) 1228 ret = rte_eth_rx_queue_setup( 1229 portid, queueid, nb_rxd, socketid, 1230 &rxq_conf, pktmbuf_pool[0][socketid]); 1231 else 1232 ret = rte_eth_rx_queue_setup( 1233 portid, queueid, nb_rxd, socketid, 1234 &rxq_conf, 1235 pktmbuf_pool[portid][socketid]); 1236 if (ret < 0) 1237 rte_exit(EXIT_FAILURE, 1238 "rte_eth_rx_queue_setup: err=%d, " 1239 "port=%d\n", 1240 ret, portid); 1241 1242 /* Add this queue node to its graph */ 1243 snprintf(qconf->rx_queue_list[queue].node_name, 1244 RTE_NODE_NAMESIZE, "ethdev_rx-%u-%u", portid, 1245 queueid); 1246 } 1247 1248 /* Alloc a graph to this lcore only if source exists */ 1249 if (qconf->n_rx_queue) 1250 nb_graphs++; 1251 } 1252 1253 printf("\n"); 1254 1255 /* Ethdev node config, skip rx queue mapping */ 1256 ret = rte_node_eth_config(ethdev_conf, nb_conf, nb_graphs); 1257 /* >8 End of graph creation. */ 1258 if (ret) 1259 rte_exit(EXIT_FAILURE, "rte_node_eth_config: err=%d\n", ret); 1260 1261 /* Start ports */ 1262 RTE_ETH_FOREACH_DEV(portid) 1263 { 1264 if ((enabled_port_mask & (1 << portid)) == 0) 1265 continue; 1266 1267 /* Start device */ 1268 ret = rte_eth_dev_start(portid); 1269 if (ret < 0) 1270 rte_exit(EXIT_FAILURE, 1271 "rte_eth_dev_start: err=%d, port=%d\n", ret, 1272 portid); 1273 1274 /* 1275 * If enabled, put device in promiscuous mode. 1276 * This allows IO forwarding mode to forward packets 1277 * to itself through 2 cross-connected ports of the 1278 * target machine. 1279 */ 1280 if (promiscuous_on) 1281 rte_eth_promiscuous_enable(portid); 1282 } 1283 1284 printf("\n"); 1285 1286 check_all_ports_link_status(enabled_port_mask); 1287 1288 /* Graph Initialization */ 1289 nb_patterns = RTE_DIM(default_patterns); 1290 node_patterns = malloc((MAX_RX_QUEUE_PER_LCORE + nb_patterns) * 1291 sizeof(*node_patterns)); 1292 if (!node_patterns) 1293 return -ENOMEM; 1294 memcpy(node_patterns, default_patterns, 1295 nb_patterns * sizeof(*node_patterns)); 1296 1297 memset(&graph_conf, 0, sizeof(graph_conf)); 1298 graph_conf.node_patterns = node_patterns; 1299 graph_conf.nb_node_patterns = nb_patterns; 1300 1301 /* Pcap config */ 1302 graph_conf.pcap_enable = pcap_trace_enable; 1303 graph_conf.num_pkt_to_capture = packet_to_capture; 1304 graph_conf.pcap_filename = pcap_filename; 1305 1306 if (model_conf == RTE_GRAPH_MODEL_MCORE_DISPATCH) 1307 graph_config_mcore_dispatch(graph_conf); 1308 else 1309 graph_config_rtc(graph_conf); 1310 1311 rte_graph_worker_model_set(model_conf); 1312 /* >8 End of graph initialization. */ 1313 1314 memset(&rewrite_data, 0, sizeof(rewrite_data)); 1315 rewrite_len = sizeof(rewrite_data); 1316 1317 /* Add routes and rewrite data to graph infra. 8< */ 1318 for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) { 1319 char route_str[INET6_ADDRSTRLEN * 4]; 1320 char abuf[INET6_ADDRSTRLEN]; 1321 struct in_addr in; 1322 uint32_t dst_port; 1323 1324 /* Skip unused ports */ 1325 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out & 1326 enabled_port_mask) == 0) 1327 continue; 1328 1329 dst_port = ipv4_l3fwd_lpm_route_array[i].if_out; 1330 1331 in.s_addr = htonl(ipv4_l3fwd_lpm_route_array[i].ip); 1332 snprintf(route_str, sizeof(route_str), "%s / %d (%d)", 1333 inet_ntop(AF_INET, &in, abuf, sizeof(abuf)), 1334 ipv4_l3fwd_lpm_route_array[i].depth, 1335 ipv4_l3fwd_lpm_route_array[i].if_out); 1336 1337 /* Use route index 'i' as next hop id */ 1338 ret = rte_node_ip4_route_add( 1339 ipv4_l3fwd_lpm_route_array[i].ip, 1340 ipv4_l3fwd_lpm_route_array[i].depth, i, 1341 RTE_NODE_IP4_LOOKUP_NEXT_REWRITE); 1342 1343 if (ret < 0) 1344 rte_exit(EXIT_FAILURE, 1345 "Unable to add ip4 route %s to graph\n", 1346 route_str); 1347 1348 memcpy(rewrite_data, val_eth + dst_port, rewrite_len); 1349 1350 /* Add next hop rewrite data for id 'i' */ 1351 ret = rte_node_ip4_rewrite_add(i, rewrite_data, 1352 rewrite_len, dst_port); 1353 if (ret < 0) 1354 rte_exit(EXIT_FAILURE, 1355 "Unable to add next hop %u for " 1356 "route %s\n", i, route_str); 1357 1358 RTE_LOG(INFO, L3FWD_GRAPH, "Added route %s, next_hop %u\n", 1359 route_str, i); 1360 } 1361 1362 for (i = 0; i < IPV6_L3FWD_LPM_NUM_ROUTES; i++) { 1363 char route_str[INET6_ADDRSTRLEN * 4]; 1364 char abuf[INET6_ADDRSTRLEN]; 1365 uint32_t dst_port; 1366 1367 /* Skip unused ports */ 1368 if ((1 << ipv6_l3fwd_lpm_route_array[i].if_out & 1369 enabled_port_mask) == 0) 1370 continue; 1371 1372 dst_port = ipv6_l3fwd_lpm_route_array[i].if_out; 1373 1374 snprintf(route_str, sizeof(route_str), "%s / %d (%d)", 1375 inet_ntop(AF_INET6, &ipv6_l3fwd_lpm_route_array[i].ip, abuf, sizeof(abuf)), 1376 ipv6_l3fwd_lpm_route_array[i].depth, 1377 ipv6_l3fwd_lpm_route_array[i].if_out); 1378 1379 /* Use route index 'i' as next hop id */ 1380 ret = rte_node_ip6_route_add(&ipv6_l3fwd_lpm_route_array[i].ip, 1381 ipv6_l3fwd_lpm_route_array[i].depth, i, 1382 RTE_NODE_IP6_LOOKUP_NEXT_REWRITE); 1383 1384 if (ret < 0) 1385 rte_exit(EXIT_FAILURE, 1386 "Unable to add ip6 route %s to graph\n", 1387 route_str); 1388 1389 memcpy(rewrite_data, val_eth + dst_port, rewrite_len); 1390 1391 /* Add next hop rewrite data for id 'i' */ 1392 ret = rte_node_ip6_rewrite_add(i, rewrite_data, 1393 rewrite_len, dst_port); 1394 if (ret < 0) 1395 rte_exit(EXIT_FAILURE, 1396 "Unable to add next hop %u for " 1397 "route %s\n", i, route_str); 1398 1399 RTE_LOG(INFO, L3FWD_GRAPH, "Added route %s, next_hop %u\n", 1400 route_str, i); 1401 } 1402 /* >8 End of adding routes and rewrite data to graph infa. */ 1403 1404 /* Launch per-lcore init on every worker lcore */ 1405 rte_eal_mp_remote_launch(graph_main_loop, NULL, SKIP_MAIN); 1406 1407 /* Accumulate and print stats on main until exit */ 1408 if (rte_graph_has_stats_feature()) 1409 print_stats(); 1410 1411 /* Wait for worker cores to exit */ 1412 ret = 0; 1413 RTE_LCORE_FOREACH_WORKER(lcore_id) { 1414 ret = rte_eal_wait_lcore(lcore_id); 1415 /* Destroy graph */ 1416 if (ret < 0 || rte_graph_destroy( 1417 rte_graph_from_name(lcore_conf[lcore_id].name))) { 1418 ret = -1; 1419 break; 1420 } 1421 } 1422 free(node_patterns); 1423 1424 /* Stop ports */ 1425 RTE_ETH_FOREACH_DEV(portid) { 1426 if ((enabled_port_mask & (1 << portid)) == 0) 1427 continue; 1428 printf("Closing port %d...", portid); 1429 ret = rte_eth_dev_stop(portid); 1430 if (ret != 0) 1431 printf("Failed to stop port %u: %s\n", 1432 portid, rte_strerror(-ret)); 1433 rte_eth_dev_close(portid); 1434 printf(" Done\n"); 1435 } 1436 1437 /* clean up the EAL */ 1438 rte_eal_cleanup(); 1439 printf("Bye...\n"); 1440 1441 return ret; 1442 } 1443