1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(C) 2020 Marvell International Ltd.
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <rte_branch_prediction.h>
22 #include <rte_common.h>
23 #include <rte_cycles.h>
25 #include <rte_ethdev.h>
26 #include <rte_graph_worker.h>
27 #include <rte_launch.h>
28 #include <rte_lcore.h>
30 #include <rte_mempool.h>
31 #include <rte_node_eth_api.h>
32 #include <rte_node_ip4_api.h>
33 #include <rte_per_lcore.h>
34 #include <rte_string_fns.h>
37 #include <cmdline_parse.h>
38 #include <cmdline_parse_etheraddr.h>
41 #define RTE_LOGTYPE_L3FWD_GRAPH RTE_LOGTYPE_USER1
44 * Configurable number of RX/TX ring descriptors
46 #define RTE_TEST_RX_DESC_DEFAULT 1024
47 #define RTE_TEST_TX_DESC_DEFAULT 1024
49 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
50 #define MAX_RX_QUEUE_PER_PORT 128
52 #define MAX_RX_QUEUE_PER_LCORE 16
54 #define MAX_LCORE_PARAMS 1024
58 /* Static global variables used within this file. */
59 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
60 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
62 /**< Ports set in promiscuous mode off by default. */
63 static int promiscuous_on;
65 static int numa_on = 1; /**< NUMA is enabled by default. */
66 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
69 static volatile bool force_quit;
71 /* Ethernet addresses of ports */
72 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
73 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
74 xmm_t val_eth[RTE_MAX_ETHPORTS];
76 /* Mask of enabled ports */
77 static uint32_t enabled_port_mask;
79 struct lcore_rx_queue {
82 char node_name[RTE_NODE_NAMESIZE];
88 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
90 struct rte_graph *graph;
91 char name[RTE_GRAPH_NAMESIZE];
93 } __rte_cache_aligned;
95 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
101 } __rte_cache_aligned;
103 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
104 static struct lcore_params lcore_params_array_default[] = {
105 {0, 0, 2}, {0, 1, 2}, {0, 2, 2}, {1, 0, 2}, {1, 1, 2},
106 {1, 2, 2}, {2, 0, 2}, {3, 0, 3}, {3, 1, 3},
109 static struct lcore_params *lcore_params = lcore_params_array_default;
110 static uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
112 static struct rte_eth_conf port_conf = {
114 .mq_mode = ETH_MQ_RX_RSS,
115 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
121 .rss_hf = ETH_RSS_IP,
125 .mq_mode = ETH_MQ_TX_NONE,
129 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
131 static struct rte_node_ethdev_config ethdev_conf[RTE_MAX_ETHPORTS];
133 struct ipv4_l3fwd_lpm_route {
139 #define IPV4_L3FWD_LPM_NUM_ROUTES \
140 (sizeof(ipv4_l3fwd_lpm_route_array) / \
141 sizeof(ipv4_l3fwd_lpm_route_array[0]))
142 /* 198.18.0.0/16 are set aside for RFC2544 benchmarking. */
143 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = {
144 {RTE_IPV4(198, 18, 0, 0), 24, 0}, {RTE_IPV4(198, 18, 1, 0), 24, 1},
145 {RTE_IPV4(198, 18, 2, 0), 24, 2}, {RTE_IPV4(198, 18, 3, 0), 24, 3},
146 {RTE_IPV4(198, 18, 4, 0), 24, 4}, {RTE_IPV4(198, 18, 5, 0), 24, 5},
147 {RTE_IPV4(198, 18, 6, 0), 24, 6}, {RTE_IPV4(198, 18, 7, 0), 24, 7},
151 check_lcore_params(void)
153 uint8_t queue, lcore;
157 for (i = 0; i < nb_lcore_params; ++i) {
158 queue = lcore_params[i].queue_id;
159 if (queue >= MAX_RX_QUEUE_PER_PORT) {
160 printf("Invalid queue number: %hhu\n", queue);
163 lcore = lcore_params[i].lcore_id;
164 if (!rte_lcore_is_enabled(lcore)) {
165 printf("Error: lcore %hhu is not enabled in lcore mask\n",
170 if (lcore == rte_get_master_lcore()) {
171 printf("Error: lcore %u is master lcore\n", lcore);
174 socketid = rte_lcore_to_socket_id(lcore);
175 if ((socketid != 0) && (numa_on == 0)) {
176 printf("Warning: lcore %hhu is on socket %d with numa off\n",
185 check_port_config(void)
190 for (i = 0; i < nb_lcore_params; ++i) {
191 portid = lcore_params[i].port_id;
192 if ((enabled_port_mask & (1 << portid)) == 0) {
193 printf("Port %u is not enabled in port mask\n", portid);
196 if (!rte_eth_dev_is_valid_port(portid)) {
197 printf("Port %u is not present on the board\n", portid);
206 get_port_n_rx_queues(const uint16_t port)
211 for (i = 0; i < nb_lcore_params; ++i) {
212 if (lcore_params[i].port_id == port) {
213 if (lcore_params[i].queue_id == queue + 1)
214 queue = lcore_params[i].queue_id;
216 rte_exit(EXIT_FAILURE,
217 "Queue ids of the port %d must be"
218 " in sequence and must start with 0\n",
219 lcore_params[i].port_id);
223 return (uint8_t)(++queue);
227 init_lcore_rx_queues(void)
229 uint16_t i, nb_rx_queue;
232 for (i = 0; i < nb_lcore_params; ++i) {
233 lcore = lcore_params[i].lcore_id;
234 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
235 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
236 printf("Error: too many queues (%u) for lcore: %u\n",
237 (unsigned int)nb_rx_queue + 1,
238 (unsigned int)lcore);
242 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
243 lcore_params[i].port_id;
244 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
245 lcore_params[i].queue_id;
246 lcore_conf[lcore].n_rx_queue++;
254 print_usage(const char *prgname)
257 "%s [EAL options] --"
260 " --config (port,queue,lcore)[,(port,queue,lcore)]"
261 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
262 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
264 " [--per-port-pool]\n\n"
266 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
267 " -P : Enable promiscuous mode\n"
268 " --config (port,queue,lcore): Rx queue configuration\n"
269 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for "
271 " --enable-jumbo: Enable jumbo frames\n"
272 " --max-pkt-len: Under the premise of enabling jumbo,\n"
273 " maximum packet length in decimal (64-9600)\n"
274 " --no-numa: Disable numa awareness\n"
275 " --per-port-pool: Use separate buffer pool per port\n\n",
280 parse_max_pkt_len(const char *pktlen)
285 /* Parse decimal string */
286 len = strtoul(pktlen, &end, 10);
287 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
297 parse_portmask(const char *portmask)
302 /* Parse hexadecimal string */
303 pm = strtoul(portmask, &end, 16);
304 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
314 parse_config(const char *q_arg)
316 enum fieldnames { FLD_PORT = 0, FLD_QUEUE, FLD_LCORE, _NUM_FLD };
317 unsigned long int_fld[_NUM_FLD];
318 const char *p, *p0 = q_arg;
319 char *str_fld[_NUM_FLD];
327 while ((p = strchr(p0, '(')) != NULL) {
334 if (size >= sizeof(s))
339 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
342 for (i = 0; i < _NUM_FLD; i++) {
344 int_fld[i] = strtoul(str_fld[i], &end, 0);
345 if (errno != 0 || end == str_fld[i])
349 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
350 printf("Exceeded max number of lcore params: %hu\n",
355 if (int_fld[FLD_PORT] >= RTE_MAX_ETHPORTS ||
356 int_fld[FLD_LCORE] >= RTE_MAX_LCORE) {
357 printf("Invalid port/lcore id\n");
361 lcore_params_array[nb_lcore_params].port_id =
362 (uint8_t)int_fld[FLD_PORT];
363 lcore_params_array[nb_lcore_params].queue_id =
364 (uint8_t)int_fld[FLD_QUEUE];
365 lcore_params_array[nb_lcore_params].lcore_id =
366 (uint8_t)int_fld[FLD_LCORE];
369 lcore_params = lcore_params_array;
375 parse_eth_dest(const char *optarg)
377 uint8_t c, *dest, peer_addr[6];
382 portid = strtoul(optarg, &port_end, 10);
383 if (errno != 0 || port_end == optarg || *port_end++ != ',')
384 rte_exit(EXIT_FAILURE, "Invalid eth-dest: %s", optarg);
385 if (portid >= RTE_MAX_ETHPORTS)
386 rte_exit(EXIT_FAILURE,
387 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n", portid,
390 if (cmdline_parse_etheraddr(NULL, port_end, &peer_addr,
391 sizeof(peer_addr)) < 0)
392 rte_exit(EXIT_FAILURE, "Invalid ethernet address: %s\n",
394 dest = (uint8_t *)&dest_eth_addr[portid];
395 for (c = 0; c < 6; c++)
396 dest[c] = peer_addr[c];
397 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
400 #define MAX_JUMBO_PKT_LEN 9600
401 #define MEMPOOL_CACHE_SIZE 256
403 static const char short_options[] = "p:" /* portmask */
404 "P" /* promiscuous */
407 #define CMD_LINE_OPT_CONFIG "config"
408 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
409 #define CMD_LINE_OPT_NO_NUMA "no-numa"
410 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
411 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
413 /* Long options mapped to a short option */
415 /* First long only option value must be >= 256, so that we won't
416 * conflict with short options
418 CMD_LINE_OPT_MIN_NUM = 256,
419 CMD_LINE_OPT_CONFIG_NUM,
420 CMD_LINE_OPT_ETH_DEST_NUM,
421 CMD_LINE_OPT_NO_NUMA_NUM,
422 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
423 CMD_LINE_OPT_PARSE_PER_PORT_POOL,
426 static const struct option lgopts[] = {
427 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
428 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
429 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
430 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
431 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
436 * This expression is used to calculate the number of mbufs needed
437 * depending on user input, taking into account memory for rx and
438 * tx hardware rings, cache per lcore and mtable per port per lcore.
439 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
442 #define NB_MBUF(nports) \
443 RTE_MAX((nports * nb_rx_queue * nb_rxd + \
444 nports * nb_lcores * RTE_GRAPH_BURST_SIZE + \
445 nports * n_tx_queue * nb_txd + \
446 nb_lcores * MEMPOOL_CACHE_SIZE), 8192u)
448 /* Parse the argument given in the command line of the application */
450 parse_args(int argc, char **argv)
452 char *prgname = argv[0];
459 /* Error or normal output strings. */
460 while ((opt = getopt_long(argc, argvopt, short_options, lgopts,
461 &option_index)) != EOF) {
466 enabled_port_mask = parse_portmask(optarg);
467 if (enabled_port_mask == 0) {
468 fprintf(stderr, "Invalid portmask\n");
469 print_usage(prgname);
479 case CMD_LINE_OPT_CONFIG_NUM:
480 ret = parse_config(optarg);
482 fprintf(stderr, "Invalid config\n");
483 print_usage(prgname);
488 case CMD_LINE_OPT_ETH_DEST_NUM:
489 parse_eth_dest(optarg);
492 case CMD_LINE_OPT_NO_NUMA_NUM:
496 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
497 const struct option lenopts = {"max-pkt-len",
498 required_argument, 0, 0};
500 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
501 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
504 * if no max-pkt-len set, use the default
505 * value RTE_ETHER_MAX_LEN.
507 if (getopt_long(argc, argvopt, "", &lenopts,
508 &option_index) == 0) {
509 ret = parse_max_pkt_len(optarg);
510 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
511 fprintf(stderr, "Invalid maximum "
513 print_usage(prgname);
516 port_conf.rxmode.max_rx_pkt_len = ret;
521 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
522 printf("Per port buffer pool is enabled\n");
527 print_usage(prgname);
533 argv[optind - 1] = prgname;
535 optind = 1; /* Reset getopt lib */
541 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
543 char buf[RTE_ETHER_ADDR_FMT_SIZE];
544 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
545 printf("%s%s", name, buf);
549 init_mem(uint16_t portid, uint32_t nb_mbuf)
555 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
556 if (rte_lcore_is_enabled(lcore_id) == 0)
560 socketid = rte_lcore_to_socket_id(lcore_id);
564 if (socketid >= NB_SOCKETS) {
565 rte_exit(EXIT_FAILURE,
566 "Socket %d of lcore %u is out of range %d\n",
567 socketid, lcore_id, NB_SOCKETS);
570 if (pktmbuf_pool[portid][socketid] == NULL) {
571 snprintf(s, sizeof(s), "mbuf_pool_%d:%d", portid,
573 /* Create a pool with priv size of a cacheline */
574 pktmbuf_pool[portid][socketid] =
575 rte_pktmbuf_pool_create(
576 s, nb_mbuf, MEMPOOL_CACHE_SIZE,
578 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
579 if (pktmbuf_pool[portid][socketid] == NULL)
580 rte_exit(EXIT_FAILURE,
581 "Cannot init mbuf pool on socket %d\n",
584 printf("Allocated mbuf pool on socket %d\n",
592 /* Check the link status of all ports in up to 9s, and print them finally */
594 check_all_ports_link_status(uint32_t port_mask)
596 #define CHECK_INTERVAL 100 /* 100ms */
597 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
598 uint8_t count, all_ports_up, print_flag = 0;
599 struct rte_eth_link link;
603 printf("\nChecking link status");
605 for (count = 0; count <= MAX_CHECK_TIME; count++) {
609 RTE_ETH_FOREACH_DEV(portid)
613 if ((port_mask & (1 << portid)) == 0)
615 memset(&link, 0, sizeof(link));
616 ret = rte_eth_link_get_nowait(portid, &link);
620 printf("Port %u link get failed: %s\n",
621 portid, rte_strerror(-ret));
624 /* Print link status if flag set */
625 if (print_flag == 1) {
626 if (link.link_status)
627 printf("Port%d Link Up. Speed %u Mbps "
629 portid, link.link_speed,
631 ETH_LINK_FULL_DUPLEX)
633 : ("half-duplex\n"));
635 printf("Port %d Link Down\n", portid);
638 /* Clear all_ports_up flag if any link down */
639 if (link.link_status == ETH_LINK_DOWN) {
644 /* After finally printing all link status, get out */
648 if (all_ports_up == 0) {
651 rte_delay_ms(CHECK_INTERVAL);
654 /* Set the print_flag if all ports up or timeout */
655 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
663 signal_handler(int signum)
665 if (signum == SIGINT || signum == SIGTERM) {
666 printf("\n\nSignal %d received, preparing to exit...\n",
675 const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
676 const char clr[] = {27, '[', '2', 'J', '\0'};
677 struct rte_graph_cluster_stats_param s_param;
678 struct rte_graph_cluster_stats *stats;
679 const char *pattern = "worker_*";
681 /* Prepare stats object */
682 memset(&s_param, 0, sizeof(s_param));
684 s_param.socket_id = SOCKET_ID_ANY;
685 s_param.graph_patterns = &pattern;
686 s_param.nb_graph_patterns = 1;
688 stats = rte_graph_cluster_stats_create(&s_param);
690 rte_exit(EXIT_FAILURE, "Unable to create stats object\n");
692 while (!force_quit) {
693 /* Clear screen and move to top left */
694 printf("%s%s", clr, topLeft);
695 rte_graph_cluster_stats_get(stats, 0);
699 rte_graph_cluster_stats_destroy(stats);
702 /* Main processing loop */
704 graph_main_loop(void *conf)
706 struct lcore_conf *qconf;
707 struct rte_graph *graph;
712 lcore_id = rte_lcore_id();
713 qconf = &lcore_conf[lcore_id];
714 graph = qconf->graph;
717 RTE_LOG(INFO, L3FWD_GRAPH, "Lcore %u has nothing to do\n",
722 RTE_LOG(INFO, L3FWD_GRAPH,
723 "Entering main loop on lcore %u, graph %s(%p)\n", lcore_id,
726 while (likely(!force_quit))
727 rte_graph_walk(graph);
733 main(int argc, char **argv)
735 /* Rewrite data of src and dst ether addr */
736 uint8_t rewrite_data[2 * sizeof(struct rte_ether_addr)];
737 static const char * const default_patterns[] = {
742 uint8_t nb_rx_queue, queue, socketid;
743 struct rte_graph_param graph_conf;
744 struct rte_eth_dev_info dev_info;
745 uint32_t nb_ports, nb_conf = 0;
746 uint32_t n_tx_queue, nb_lcores;
747 struct rte_eth_txconf *txconf;
748 uint16_t queueid, portid, i;
749 const char **node_patterns;
750 struct lcore_conf *qconf;
751 uint16_t nb_graphs = 0;
752 uint16_t nb_patterns;
758 ret = rte_eal_init(argc, argv);
760 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
765 signal(SIGINT, signal_handler);
766 signal(SIGTERM, signal_handler);
768 /* Pre-init dst MACs for all ports to 02:00:00:00:00:xx */
769 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
770 dest_eth_addr[portid] =
771 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
772 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
775 /* Parse application arguments (after the EAL ones) */
776 ret = parse_args(argc, argv);
778 rte_exit(EXIT_FAILURE, "Invalid L3FWD_GRAPH parameters\n");
780 if (check_lcore_params() < 0)
781 rte_exit(EXIT_FAILURE, "check_lcore_params() failed\n");
783 ret = init_lcore_rx_queues();
785 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues() failed\n");
787 if (check_port_config() < 0)
788 rte_exit(EXIT_FAILURE, "check_port_config() failed\n");
790 nb_ports = rte_eth_dev_count_avail();
791 nb_lcores = rte_lcore_count();
793 /* Initialize all ports */
794 RTE_ETH_FOREACH_DEV(portid)
796 struct rte_eth_conf local_port_conf = port_conf;
798 /* Skip ports that are not enabled */
799 if ((enabled_port_mask & (1 << portid)) == 0) {
800 printf("\nSkipping disabled port %d\n", portid);
805 printf("Initializing port %d ... ", portid);
808 nb_rx_queue = get_port_n_rx_queues(portid);
809 n_tx_queue = nb_lcores;
810 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
811 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
812 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
813 nb_rx_queue, n_tx_queue);
815 rte_eth_dev_info_get(portid, &dev_info);
816 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
817 local_port_conf.txmode.offloads |=
818 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
820 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
821 dev_info.flow_type_rss_offloads;
822 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
823 port_conf.rx_adv_conf.rss_conf.rss_hf) {
824 printf("Port %u modified RSS hash function based on "
826 "requested:%#" PRIx64 " configured:%#" PRIx64
828 portid, port_conf.rx_adv_conf.rss_conf.rss_hf,
829 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
832 ret = rte_eth_dev_configure(portid, nb_rx_queue,
833 n_tx_queue, &local_port_conf);
835 rte_exit(EXIT_FAILURE,
836 "Cannot configure device: err=%d, port=%d\n",
839 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
842 rte_exit(EXIT_FAILURE,
843 "Cannot adjust number of descriptors: err=%d, "
847 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
848 print_ethaddr(" Address:", &ports_eth_addr[portid]);
852 (const struct rte_ether_addr *)&dest_eth_addr[portid]);
856 * prepare src MACs for each port.
859 &ports_eth_addr[portid],
860 (struct rte_ether_addr *)(val_eth + portid) + 1);
863 if (!per_port_pool) {
864 /* portid = 0; this is *not* signifying the first port,
865 * rather, it signifies that portid is ignored.
867 ret = init_mem(0, NB_MBUF(nb_ports));
869 ret = init_mem(portid, NB_MBUF(1));
872 rte_exit(EXIT_FAILURE, "init_mem() failed\n");
874 /* Init one TX queue per couple (lcore,port) */
876 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
877 if (rte_lcore_is_enabled(lcore_id) == 0)
880 qconf = &lcore_conf[lcore_id];
883 socketid = (uint8_t)rte_lcore_to_socket_id(
888 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
891 txconf = &dev_info.default_txconf;
892 txconf->offloads = local_port_conf.txmode.offloads;
893 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
896 rte_exit(EXIT_FAILURE,
897 "rte_eth_tx_queue_setup: err=%d, "
903 /* Setup ethdev node config */
904 ethdev_conf[nb_conf].port_id = portid;
905 ethdev_conf[nb_conf].num_rx_queues = nb_rx_queue;
906 ethdev_conf[nb_conf].num_tx_queues = n_tx_queue;
908 ethdev_conf[nb_conf].mp = pktmbuf_pool[0];
911 ethdev_conf[nb_conf].mp = pktmbuf_pool[portid];
912 ethdev_conf[nb_conf].mp_count = NB_SOCKETS;
918 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
919 if (rte_lcore_is_enabled(lcore_id) == 0)
921 qconf = &lcore_conf[lcore_id];
922 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
925 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
926 struct rte_eth_rxconf rxq_conf;
928 portid = qconf->rx_queue_list[queue].port_id;
929 queueid = qconf->rx_queue_list[queue].queue_id;
932 socketid = (uint8_t)rte_lcore_to_socket_id(
937 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
940 rte_eth_dev_info_get(portid, &dev_info);
941 rxq_conf = dev_info.default_rxconf;
942 rxq_conf.offloads = port_conf.rxmode.offloads;
944 ret = rte_eth_rx_queue_setup(
945 portid, queueid, nb_rxd, socketid,
946 &rxq_conf, pktmbuf_pool[0][socketid]);
948 ret = rte_eth_rx_queue_setup(
949 portid, queueid, nb_rxd, socketid,
951 pktmbuf_pool[portid][socketid]);
953 rte_exit(EXIT_FAILURE,
954 "rte_eth_rx_queue_setup: err=%d, "
958 /* Add this queue node to its graph */
959 snprintf(qconf->rx_queue_list[queue].node_name,
960 RTE_NODE_NAMESIZE, "ethdev_rx-%u-%u", portid,
964 /* Alloc a graph to this lcore only if source exists */
965 if (qconf->n_rx_queue)
971 /* Ethdev node config, skip rx queue mapping */
972 ret = rte_node_eth_config(ethdev_conf, nb_conf, nb_graphs);
974 rte_exit(EXIT_FAILURE, "rte_node_eth_config: err=%d\n", ret);
977 RTE_ETH_FOREACH_DEV(portid)
979 if ((enabled_port_mask & (1 << portid)) == 0)
983 ret = rte_eth_dev_start(portid);
985 rte_exit(EXIT_FAILURE,
986 "rte_eth_dev_start: err=%d, port=%d\n", ret,
990 * If enabled, put device in promiscuous mode.
991 * This allows IO forwarding mode to forward packets
992 * to itself through 2 cross-connected ports of the
996 rte_eth_promiscuous_enable(portid);
1001 check_all_ports_link_status(enabled_port_mask);
1003 /* Graph Initialization */
1004 nb_patterns = RTE_DIM(default_patterns);
1005 node_patterns = malloc((MAX_RX_QUEUE_PER_LCORE + nb_patterns) *
1006 sizeof(*node_patterns));
1009 memcpy(node_patterns, default_patterns,
1010 nb_patterns * sizeof(*node_patterns));
1012 memset(&graph_conf, 0, sizeof(graph_conf));
1013 graph_conf.node_patterns = node_patterns;
1015 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1016 rte_graph_t graph_id;
1019 if (rte_lcore_is_enabled(lcore_id) == 0)
1022 qconf = &lcore_conf[lcore_id];
1024 /* Skip graph creation if no source exists */
1025 if (!qconf->n_rx_queue)
1028 /* Add rx node patterns of this lcore */
1029 for (i = 0; i < qconf->n_rx_queue; i++) {
1030 graph_conf.node_patterns[nb_patterns + i] =
1031 qconf->rx_queue_list[i].node_name;
1034 graph_conf.nb_node_patterns = nb_patterns + i;
1035 graph_conf.socket_id = rte_lcore_to_socket_id(lcore_id);
1037 snprintf(qconf->name, sizeof(qconf->name), "worker_%u",
1040 graph_id = rte_graph_create(qconf->name, &graph_conf);
1041 if (graph_id == RTE_GRAPH_ID_INVALID)
1042 rte_exit(EXIT_FAILURE,
1043 "rte_graph_create(): graph_id invalid"
1044 " for lcore %u\n", lcore_id);
1046 qconf->graph_id = graph_id;
1047 qconf->graph = rte_graph_lookup(qconf->name);
1049 rte_exit(EXIT_FAILURE,
1050 "rte_graph_lookup(): graph %s not found\n",
1054 memset(&rewrite_data, 0, sizeof(rewrite_data));
1055 rewrite_len = sizeof(rewrite_data);
1057 /* Add route to ip4 graph infra */
1058 for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
1059 char route_str[INET6_ADDRSTRLEN * 4];
1060 char abuf[INET6_ADDRSTRLEN];
1064 /* Skip unused ports */
1065 if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
1066 enabled_port_mask) == 0)
1069 dst_port = ipv4_l3fwd_lpm_route_array[i].if_out;
1071 in.s_addr = htonl(ipv4_l3fwd_lpm_route_array[i].ip);
1072 snprintf(route_str, sizeof(route_str), "%s / %d (%d)",
1073 inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
1074 ipv4_l3fwd_lpm_route_array[i].depth,
1075 ipv4_l3fwd_lpm_route_array[i].if_out);
1077 /* Use route index 'i' as next hop id */
1078 ret = rte_node_ip4_route_add(
1079 ipv4_l3fwd_lpm_route_array[i].ip,
1080 ipv4_l3fwd_lpm_route_array[i].depth, i,
1081 RTE_NODE_IP4_LOOKUP_NEXT_REWRITE);
1084 rte_exit(EXIT_FAILURE,
1085 "Unable to add ip4 route %s to graph\n",
1088 memcpy(rewrite_data, val_eth + dst_port, rewrite_len);
1090 /* Add next hop rewrite data for id 'i' */
1091 ret = rte_node_ip4_rewrite_add(i, rewrite_data,
1092 rewrite_len, dst_port);
1094 rte_exit(EXIT_FAILURE,
1095 "Unable to add next hop %u for "
1096 "route %s\n", i, route_str);
1098 RTE_LOG(INFO, L3FWD_GRAPH, "Added route %s, next_hop %u\n",
1102 /* Launch per-lcore init on every slave lcore */
1103 rte_eal_mp_remote_launch(graph_main_loop, NULL, SKIP_MASTER);
1105 /* Accumulate and print stats on master until exit */
1106 if (rte_graph_has_stats_feature())
1109 /* Wait for slave cores to exit */
1111 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1112 ret = rte_eal_wait_lcore(lcore_id);
1114 if (ret < 0 || rte_graph_destroy(
1115 rte_graph_from_name(lcore_conf[lcore_id].name))) {
1120 free(node_patterns);
1123 RTE_ETH_FOREACH_DEV(portid) {
1124 if ((enabled_port_mask & (1 << portid)) == 0)
1126 printf("Closing port %d...", portid);
1127 rte_eth_dev_stop(portid);
1128 rte_eth_dev_close(portid);