1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2010-2016 Intel Corporation 3 */ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 #include <string.h> 8 #include <stdint.h> 9 #include <inttypes.h> 10 #include <sys/types.h> 11 #include <sys/queue.h> 12 #include <netinet/in.h> 13 #include <setjmp.h> 14 #include <stdarg.h> 15 #include <ctype.h> 16 #include <errno.h> 17 #include <getopt.h> 18 #include <signal.h> 19 20 #include <rte_common.h> 21 #include <rte_log.h> 22 #include <rte_malloc.h> 23 #include <rte_memory.h> 24 #include <rte_memcpy.h> 25 #include <rte_eal.h> 26 #include <rte_launch.h> 27 #include <rte_cycles.h> 28 #include <rte_prefetch.h> 29 #include <rte_lcore.h> 30 #include <rte_per_lcore.h> 31 #include <rte_branch_prediction.h> 32 #include <rte_interrupts.h> 33 #include <rte_random.h> 34 #include <rte_debug.h> 35 #include <rte_ether.h> 36 #include <rte_ethdev.h> 37 #include <rte_mempool.h> 38 #include <rte_mbuf.h> 39 #include <rte_timer.h> 40 #include <rte_keepalive.h> 41 42 #include "shm.h" 43 44 #define NB_MBUF_PER_PORT 3000 45 46 #define MAX_PKT_BURST 32 47 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */ 48 49 /* 50 * Configurable number of RX/TX ring descriptors 51 */ 52 #define RX_DESC_DEFAULT 1024 53 #define TX_DESC_DEFAULT 1024 54 static uint16_t nb_rxd = RX_DESC_DEFAULT; 55 static uint16_t nb_txd = TX_DESC_DEFAULT; 56 57 /* ethernet addresses of ports */ 58 static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS]; 59 60 /* mask of enabled ports */ 61 static uint32_t l2fwd_enabled_port_mask; 62 63 /* list of enabled ports */ 64 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS]; 65 66 static unsigned int l2fwd_rx_queue_per_lcore = 1; 67 68 #define MAX_RX_QUEUE_PER_LCORE 16 69 #define MAX_TX_QUEUE_PER_PORT 16 70 struct __rte_cache_aligned lcore_queue_conf { 71 unsigned n_rx_port; 72 unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE]; 73 }; 74 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE]; 75 76 struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS]; 77 78 static struct rte_eth_conf port_conf = { 79 .txmode = { 80 .mq_mode = RTE_ETH_MQ_TX_NONE, 81 }, 82 }; 83 84 struct rte_mempool *l2fwd_pktmbuf_pool = NULL; 85 86 /* Per-port statistics struct */ 87 struct __rte_cache_aligned l2fwd_port_statistics { 88 uint64_t tx; 89 uint64_t rx; 90 uint64_t dropped; 91 }; 92 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS]; 93 94 /* A tsc-based timer responsible for triggering statistics printout */ 95 #define TIMER_MILLISECOND 1 96 #define MAX_TIMER_PERIOD 86400 /* 1 day max */ 97 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* 10 seconds */ 98 static int64_t check_period = 5; /* default check cycle is 5ms */ 99 100 /* Keepalive structure */ 101 struct rte_keepalive *rte_global_keepalive_info; 102 103 /* Termination signalling */ 104 static int terminate_signal_received; 105 106 /* Termination signal handler */ 107 static void handle_sigterm(__rte_unused int value) 108 { 109 terminate_signal_received = 1; 110 } 111 112 /* Print out statistics on packets dropped */ 113 static void 114 print_stats(__rte_unused struct rte_timer *ptr_timer, 115 __rte_unused void *ptr_data) 116 { 117 uint64_t total_packets_dropped, total_packets_tx, total_packets_rx; 118 uint16_t portid; 119 120 total_packets_dropped = 0; 121 total_packets_tx = 0; 122 total_packets_rx = 0; 123 124 const char clr[] = { 27, '[', '2', 'J', '\0' }; 125 const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' }; 126 127 /* Clear screen and move to top left */ 128 printf("%s%s", clr, topLeft); 129 130 printf("\nPort statistics ===================================="); 131 132 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) { 133 /* skip disabled ports */ 134 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) 135 continue; 136 printf("\nStatistics for port %u ------------------------------" 137 "\nPackets sent: %24"PRIu64 138 "\nPackets received: %20"PRIu64 139 "\nPackets dropped: %21"PRIu64, 140 portid, 141 port_statistics[portid].tx, 142 port_statistics[portid].rx, 143 port_statistics[portid].dropped); 144 145 total_packets_dropped += port_statistics[portid].dropped; 146 total_packets_tx += port_statistics[portid].tx; 147 total_packets_rx += port_statistics[portid].rx; 148 } 149 printf("\nAggregate statistics ===============================" 150 "\nTotal packets sent: %18"PRIu64 151 "\nTotal packets received: %14"PRIu64 152 "\nTotal packets dropped: %15"PRIu64, 153 total_packets_tx, 154 total_packets_rx, 155 total_packets_dropped); 156 printf("\n====================================================\n"); 157 158 fflush(stdout); 159 } 160 161 static void 162 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid) 163 { 164 struct rte_ether_hdr *eth; 165 void *tmp; 166 int sent; 167 unsigned dst_port; 168 struct rte_eth_dev_tx_buffer *buffer; 169 170 dst_port = l2fwd_dst_ports[portid]; 171 eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *); 172 173 /* 02:00:00:00:00:xx */ 174 tmp = ð->dst_addr.addr_bytes[0]; 175 *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40); 176 177 /* src addr */ 178 rte_ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], ð->src_addr); 179 180 buffer = tx_buffer[dst_port]; 181 sent = rte_eth_tx_buffer(dst_port, 0, buffer, m); 182 if (sent) 183 port_statistics[dst_port].tx += sent; 184 } 185 186 /* main processing loop */ 187 static void 188 l2fwd_main_loop(void) 189 { 190 struct rte_mbuf *pkts_burst[MAX_PKT_BURST]; 191 struct rte_mbuf *m; 192 int sent; 193 unsigned lcore_id; 194 uint64_t prev_tsc, diff_tsc, cur_tsc; 195 unsigned i, j, portid, nb_rx; 196 struct lcore_queue_conf *qconf; 197 const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) 198 / US_PER_S * BURST_TX_DRAIN_US; 199 struct rte_eth_dev_tx_buffer *buffer; 200 201 prev_tsc = 0; 202 203 lcore_id = rte_lcore_id(); 204 qconf = &lcore_queue_conf[lcore_id]; 205 206 if (qconf->n_rx_port == 0) { 207 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id); 208 return; 209 } 210 211 RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id); 212 213 for (i = 0; i < qconf->n_rx_port; i++) { 214 215 portid = qconf->rx_port_list[i]; 216 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id, 217 portid); 218 } 219 220 uint64_t tsc_initial = rte_rdtsc(); 221 uint64_t tsc_lifetime = rte_rand_max(8 * rte_get_tsc_hz()); 222 223 while (!terminate_signal_received) { 224 /* Keepalive heartbeat. 8< */ 225 rte_keepalive_mark_alive(rte_global_keepalive_info); 226 227 cur_tsc = rte_rdtsc(); 228 229 /* 230 * Die randomly within 7 secs for demo purposes if 231 * keepalive enabled 232 */ 233 if (check_period > 0 && cur_tsc - tsc_initial > tsc_lifetime) 234 break; 235 /* >8 End of keepalive heartbeat. */ 236 237 /* 238 * TX burst queue drain 239 */ 240 diff_tsc = cur_tsc - prev_tsc; 241 if (unlikely(diff_tsc > drain_tsc)) { 242 243 for (i = 0; i < qconf->n_rx_port; i++) { 244 245 portid = l2fwd_dst_ports[qconf->rx_port_list[i]]; 246 buffer = tx_buffer[portid]; 247 248 sent = rte_eth_tx_buffer_flush(portid, 0, buffer); 249 if (sent) 250 port_statistics[portid].tx += sent; 251 252 } 253 254 prev_tsc = cur_tsc; 255 } 256 257 /* 258 * Read packet from RX queues 259 */ 260 for (i = 0; i < qconf->n_rx_port; i++) { 261 262 portid = qconf->rx_port_list[i]; 263 nb_rx = rte_eth_rx_burst(portid, 0, 264 pkts_burst, MAX_PKT_BURST); 265 266 port_statistics[portid].rx += nb_rx; 267 268 for (j = 0; j < nb_rx; j++) { 269 m = pkts_burst[j]; 270 rte_prefetch0(rte_pktmbuf_mtod(m, void *)); 271 l2fwd_simple_forward(m, portid); 272 } 273 } 274 } 275 } 276 277 static int 278 l2fwd_launch_one_lcore(__rte_unused void *dummy) 279 { 280 l2fwd_main_loop(); 281 return 0; 282 } 283 284 /* display usage */ 285 static void 286 l2fwd_usage(const char *prgname) 287 { 288 printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n" 289 " -p PORTMASK: hexadecimal bitmask of ports to configure\n" 290 " -q NQ: number of queue (=ports) per lcore (default is 1)\n" 291 " -K PERIOD: Keepalive check period (5 default; 86400 max)\n" 292 " -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n", 293 prgname); 294 } 295 296 static int 297 l2fwd_parse_portmask(const char *portmask) 298 { 299 char *end = NULL; 300 unsigned long pm; 301 302 /* parse hexadecimal string */ 303 pm = strtoul(portmask, &end, 16); 304 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0')) 305 return 0; 306 307 return pm; 308 } 309 310 static unsigned int 311 l2fwd_parse_nqueue(const char *q_arg) 312 { 313 char *end = NULL; 314 unsigned long n; 315 316 /* parse hexadecimal string */ 317 n = strtoul(q_arg, &end, 10); 318 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 319 return 0; 320 if (n == 0) 321 return 0; 322 if (n >= MAX_RX_QUEUE_PER_LCORE) 323 return 0; 324 325 return n; 326 } 327 328 static int 329 l2fwd_parse_timer_period(const char *q_arg) 330 { 331 char *end = NULL; 332 int n; 333 334 /* parse number string */ 335 n = strtol(q_arg, &end, 10); 336 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 337 return -1; 338 if (n >= MAX_TIMER_PERIOD) 339 return -1; 340 341 return n; 342 } 343 344 static int 345 l2fwd_parse_check_period(const char *q_arg) 346 { 347 char *end = NULL; 348 int n; 349 350 /* parse number string */ 351 n = strtol(q_arg, &end, 10); 352 if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0')) 353 return -1; 354 if (n >= MAX_TIMER_PERIOD) 355 return -1; 356 357 return n; 358 } 359 360 /* Parse the argument given in the command line of the application */ 361 static int 362 l2fwd_parse_args(int argc, char **argv) 363 { 364 int opt, ret; 365 char **argvopt; 366 int option_index; 367 char *prgname = argv[0]; 368 static struct option lgopts[] = { 369 {NULL, 0, 0, 0} 370 }; 371 372 argvopt = argv; 373 374 while ((opt = getopt_long(argc, argvopt, "p:q:T:K:", 375 lgopts, &option_index)) != EOF) { 376 377 switch (opt) { 378 /* portmask */ 379 case 'p': 380 l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg); 381 if (l2fwd_enabled_port_mask == 0) { 382 printf("invalid portmask\n"); 383 l2fwd_usage(prgname); 384 return -1; 385 } 386 break; 387 388 /* nqueue */ 389 case 'q': 390 l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg); 391 if (l2fwd_rx_queue_per_lcore == 0) { 392 printf("invalid queue number\n"); 393 l2fwd_usage(prgname); 394 return -1; 395 } 396 break; 397 398 /* timer period */ 399 case 'T': 400 timer_period = l2fwd_parse_timer_period(optarg) 401 * (int64_t)(1000 * TIMER_MILLISECOND); 402 if (timer_period < 0) { 403 printf("invalid timer period\n"); 404 l2fwd_usage(prgname); 405 return -1; 406 } 407 break; 408 409 /* Check period */ 410 case 'K': 411 check_period = l2fwd_parse_check_period(optarg); 412 if (check_period < 0) { 413 printf("invalid check period\n"); 414 l2fwd_usage(prgname); 415 return -1; 416 } 417 break; 418 419 /* long options */ 420 case 0: 421 l2fwd_usage(prgname); 422 return -1; 423 424 default: 425 l2fwd_usage(prgname); 426 return -1; 427 } 428 } 429 430 if (optind >= 0) 431 argv[optind-1] = prgname; 432 433 ret = optind-1; 434 optind = 1; /* reset getopt lib */ 435 return ret; 436 } 437 438 /* Check the link status of all ports in up to 9s, and print them finally */ 439 static void 440 check_all_ports_link_status(uint32_t port_mask) 441 { 442 #define CHECK_INTERVAL 100 /* 100ms */ 443 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */ 444 uint16_t portid; 445 uint8_t count, all_ports_up, print_flag = 0; 446 struct rte_eth_link link; 447 int ret; 448 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN]; 449 450 printf("\nChecking link status"); 451 fflush(stdout); 452 for (count = 0; count <= MAX_CHECK_TIME; count++) { 453 all_ports_up = 1; 454 RTE_ETH_FOREACH_DEV(portid) { 455 if ((port_mask & (1 << portid)) == 0) 456 continue; 457 memset(&link, 0, sizeof(link)); 458 ret = rte_eth_link_get_nowait(portid, &link); 459 if (ret < 0) { 460 all_ports_up = 0; 461 if (print_flag == 1) 462 printf("Port %u link get failed: %s\n", 463 portid, rte_strerror(-ret)); 464 continue; 465 } 466 /* print link status if flag set */ 467 if (print_flag == 1) { 468 rte_eth_link_to_str(link_status_text, 469 sizeof(link_status_text), &link); 470 printf("Port %d %s\n", portid, 471 link_status_text); 472 continue; 473 } 474 /* clear all_ports_up flag if any link down */ 475 if (link.link_status == RTE_ETH_LINK_DOWN) { 476 all_ports_up = 0; 477 break; 478 } 479 } 480 /* after finally printing all link status, get out */ 481 if (print_flag == 1) 482 break; 483 484 if (all_ports_up == 0) { 485 printf("."); 486 fflush(stdout); 487 rte_delay_ms(CHECK_INTERVAL); 488 } 489 490 /* set the print_flag if all ports up or timeout */ 491 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) { 492 print_flag = 1; 493 printf("done\n"); 494 } 495 } 496 } 497 498 static void 499 dead_core(__rte_unused void *ptr_data, const int id_core) 500 { 501 if (terminate_signal_received) 502 return; 503 printf("Dead core %i - restarting..\n", id_core); 504 if (rte_eal_get_lcore_state(id_core) == WAIT) { 505 rte_eal_remote_launch(l2fwd_launch_one_lcore, NULL, id_core); 506 } else { 507 printf("..false positive!\n"); 508 } 509 } 510 511 static void 512 relay_core_state(void *ptr_data, const int id_core, 513 const enum rte_keepalive_state core_state, uint64_t last_alive) 514 { 515 rte_keepalive_relayed_state((struct rte_keepalive_shm *)ptr_data, 516 id_core, core_state, last_alive); 517 } 518 519 int 520 main(int argc, char **argv) 521 { 522 struct lcore_queue_conf *qconf; 523 int ret; 524 uint16_t nb_ports; 525 uint16_t nb_ports_available = 0; 526 uint16_t portid, last_port; 527 unsigned lcore_id, rx_lcore_id; 528 unsigned nb_ports_in_mask = 0; 529 unsigned int total_nb_mbufs; 530 struct sigaction signal_handler; 531 struct rte_keepalive_shm *ka_shm; 532 533 memset(&signal_handler, 0, sizeof(signal_handler)); 534 terminate_signal_received = 0; 535 signal_handler.sa_handler = &handle_sigterm; 536 if (sigaction(SIGINT, &signal_handler, NULL) == -1 || 537 sigaction(SIGTERM, &signal_handler, NULL) == -1) 538 rte_exit(EXIT_FAILURE, "SIGNAL\n"); 539 540 541 /* init EAL */ 542 ret = rte_eal_init(argc, argv); 543 if (ret < 0) 544 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n"); 545 argc -= ret; 546 argv += ret; 547 548 l2fwd_enabled_port_mask = 0; 549 550 /* parse application arguments (after the EAL ones) */ 551 ret = l2fwd_parse_args(argc, argv); 552 if (ret < 0) 553 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n"); 554 555 nb_ports = rte_eth_dev_count_avail(); 556 if (nb_ports == 0) 557 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n"); 558 559 /* create the mbuf pool */ 560 total_nb_mbufs = NB_MBUF_PER_PORT * nb_ports; 561 562 l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", 563 total_nb_mbufs, 32, 0, RTE_MBUF_DEFAULT_BUF_SIZE, 564 rte_socket_id()); 565 if (l2fwd_pktmbuf_pool == NULL) 566 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n"); 567 568 /* reset l2fwd_dst_ports */ 569 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) 570 l2fwd_dst_ports[portid] = 0; 571 last_port = 0; 572 573 /* 574 * Each logical core is assigned a dedicated TX queue on each port. 575 */ 576 RTE_ETH_FOREACH_DEV(portid) { 577 /* skip ports that are not enabled */ 578 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) 579 continue; 580 581 if (nb_ports_in_mask % 2) { 582 l2fwd_dst_ports[portid] = last_port; 583 l2fwd_dst_ports[last_port] = portid; 584 } else 585 last_port = portid; 586 587 nb_ports_in_mask++; 588 } 589 if (nb_ports_in_mask % 2) { 590 printf("Notice: odd number of ports in portmask.\n"); 591 l2fwd_dst_ports[last_port] = last_port; 592 } 593 594 rx_lcore_id = 1; 595 qconf = NULL; 596 597 /* Initialize the port/queue configuration of each logical core */ 598 RTE_ETH_FOREACH_DEV(portid) { 599 /* skip ports that are not enabled */ 600 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) 601 continue; 602 603 /* get the lcore_id for this port */ 604 while (rte_lcore_is_enabled(rx_lcore_id) == 0 || 605 lcore_queue_conf[rx_lcore_id].n_rx_port == 606 l2fwd_rx_queue_per_lcore) { 607 rx_lcore_id++; 608 if (rx_lcore_id >= RTE_MAX_LCORE) 609 rte_exit(EXIT_FAILURE, "Not enough cores\n"); 610 } 611 612 if (qconf != &lcore_queue_conf[rx_lcore_id]) 613 /* Assigned a new logical core in the loop above. */ 614 qconf = &lcore_queue_conf[rx_lcore_id]; 615 616 qconf->rx_port_list[qconf->n_rx_port] = portid; 617 qconf->n_rx_port++; 618 printf("Lcore %u: RX port %u\n", 619 rx_lcore_id, portid); 620 } 621 622 /* Initialise each port */ 623 RTE_ETH_FOREACH_DEV(portid) { 624 struct rte_eth_dev_info dev_info; 625 struct rte_eth_rxconf rxq_conf; 626 struct rte_eth_txconf txq_conf; 627 struct rte_eth_conf local_port_conf = port_conf; 628 629 /* skip ports that are not enabled */ 630 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) { 631 printf("Skipping disabled port %u\n", portid); 632 continue; 633 } 634 nb_ports_available++; 635 636 /* init port */ 637 printf("Initializing port %u... ", portid); 638 fflush(stdout); 639 640 ret = rte_eth_dev_info_get(portid, &dev_info); 641 if (ret != 0) 642 rte_exit(EXIT_FAILURE, 643 "Error during getting device (port %u) info: %s\n", 644 portid, strerror(-ret)); 645 646 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE) 647 local_port_conf.txmode.offloads |= 648 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE; 649 ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf); 650 if (ret < 0) 651 rte_exit(EXIT_FAILURE, 652 "Cannot configure device: err=%d, port=%u\n", 653 ret, portid); 654 655 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, 656 &nb_txd); 657 if (ret < 0) 658 rte_exit(EXIT_FAILURE, 659 "Cannot adjust number of descriptors: err=%d, port=%u\n", 660 ret, portid); 661 662 ret = rte_eth_macaddr_get(portid, 663 &l2fwd_ports_eth_addr[portid]); 664 if (ret < 0) 665 rte_exit(EXIT_FAILURE, 666 "Cannot mac address: err=%d, port=%u\n", 667 ret, portid); 668 669 /* init one RX queue */ 670 fflush(stdout); 671 rxq_conf = dev_info.default_rxconf; 672 rxq_conf.offloads = local_port_conf.rxmode.offloads; 673 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd, 674 rte_eth_dev_socket_id(portid), 675 &rxq_conf, 676 l2fwd_pktmbuf_pool); 677 if (ret < 0) 678 rte_exit(EXIT_FAILURE, 679 "rte_eth_rx_queue_setup:err=%d, port=%u\n", 680 ret, portid); 681 682 /* init one TX queue on each port */ 683 fflush(stdout); 684 txq_conf = dev_info.default_txconf; 685 txq_conf.offloads = local_port_conf.txmode.offloads; 686 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd, 687 rte_eth_dev_socket_id(portid), 688 &txq_conf); 689 if (ret < 0) 690 rte_exit(EXIT_FAILURE, 691 "rte_eth_tx_queue_setup:err=%d, port=%u\n", 692 ret, portid); 693 694 /* Initialize TX buffers */ 695 tx_buffer[portid] = rte_zmalloc_socket("tx_buffer", 696 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0, 697 rte_eth_dev_socket_id(portid)); 698 if (tx_buffer[portid] == NULL) 699 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n", 700 portid); 701 702 rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST); 703 704 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid], 705 rte_eth_tx_buffer_count_callback, 706 &port_statistics[portid].dropped); 707 if (ret < 0) 708 rte_exit(EXIT_FAILURE, 709 "Cannot set error callback for tx buffer on port %u\n", 710 portid); 711 712 /* Start device */ 713 ret = rte_eth_dev_start(portid); 714 if (ret < 0) 715 rte_exit(EXIT_FAILURE, 716 "rte_eth_dev_start:err=%d, port=%u\n", 717 ret, portid); 718 719 ret = rte_eth_promiscuous_enable(portid); 720 if (ret != 0) 721 rte_exit(EXIT_FAILURE, 722 "rte_eth_promiscuous_enable:err=%s, port=%u\n", 723 rte_strerror(-ret), portid); 724 725 printf("Port %u, MAC address: " 726 RTE_ETHER_ADDR_PRT_FMT "\n\n", 727 portid, 728 RTE_ETHER_ADDR_BYTES(&l2fwd_ports_eth_addr[portid])); 729 730 /* initialize port stats */ 731 memset(&port_statistics, 0, sizeof(port_statistics)); 732 } 733 734 if (!nb_ports_available) { 735 rte_exit(EXIT_FAILURE, 736 "All available ports are disabled. Please set portmask.\n"); 737 } 738 739 check_all_ports_link_status(l2fwd_enabled_port_mask); 740 741 struct rte_timer hb_timer, stats_timer; 742 743 rte_timer_subsystem_init(); 744 rte_timer_init(&stats_timer); 745 746 ka_shm = NULL; 747 if (check_period > 0) { 748 ka_shm = rte_keepalive_shm_create(); 749 if (ka_shm == NULL) 750 rte_exit(EXIT_FAILURE, 751 "rte_keepalive_shm_create() failed"); 752 /* Initialize keepalive functionality. 8< */ 753 rte_global_keepalive_info = 754 rte_keepalive_create(&dead_core, ka_shm); 755 if (rte_global_keepalive_info == NULL) 756 rte_exit(EXIT_FAILURE, "init_keep_alive() failed"); 757 /* >8 End of initializing keepalive functionality. */ 758 rte_keepalive_register_relay_callback(rte_global_keepalive_info, 759 relay_core_state, ka_shm); 760 rte_timer_init(&hb_timer); 761 if (rte_timer_reset(&hb_timer, 762 (check_period * rte_get_timer_hz()) / 1000, 763 PERIODICAL, 764 rte_lcore_id(), 765 (void(*)(struct rte_timer*, void*)) 766 &rte_keepalive_dispatch_pings, 767 rte_global_keepalive_info 768 ) != 0 ) 769 rte_exit(EXIT_FAILURE, "Keepalive setup failure.\n"); 770 } 771 if (timer_period > 0) { 772 /* Issues the pings keepalive_dispatch_pings(). 8< */ 773 if (rte_timer_reset(&stats_timer, 774 (timer_period * rte_get_timer_hz()) / 1000, 775 PERIODICAL, 776 rte_lcore_id(), 777 &print_stats, NULL 778 ) != 0 ) 779 rte_exit(EXIT_FAILURE, "Stats setup failure.\n"); 780 /* >8 End of issuing the pings keepalive_dispatch_pings(). */ 781 } 782 /* launch per-lcore init on every worker lcore */ 783 RTE_LCORE_FOREACH_WORKER(lcore_id) { 784 struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id]; 785 786 if (qconf->n_rx_port == 0) 787 RTE_LOG(INFO, L2FWD, 788 "lcore %u has nothing to do\n", 789 lcore_id 790 ); 791 else { 792 rte_eal_remote_launch( 793 l2fwd_launch_one_lcore, 794 NULL, 795 lcore_id 796 ); 797 rte_keepalive_register_core(rte_global_keepalive_info, 798 lcore_id); 799 } 800 } 801 while (!terminate_signal_received) { 802 rte_timer_manage(); 803 rte_delay_ms(5); 804 } 805 806 RTE_LCORE_FOREACH_WORKER(lcore_id) { 807 if (rte_eal_wait_lcore(lcore_id) < 0) 808 return -1; 809 } 810 811 if (ka_shm != NULL) 812 rte_keepalive_shm_cleanup(ka_shm); 813 814 /* clean up the EAL */ 815 rte_eal_cleanup(); 816 817 return 0; 818 } 819