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