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_lcore.h>
28 #include <rte_mempool.h>
29 #include <rte_per_lcore.h>
30 #include <rte_string_fns.h>
33 #include <cmdline_parse.h>
34 #include <cmdline_parse_etheraddr.h>
37 #define RTE_LOGTYPE_L3FWD_GRAPH RTE_LOGTYPE_USER1
40 * Configurable number of RX/TX ring descriptors
42 #define RTE_TEST_RX_DESC_DEFAULT 1024
43 #define RTE_TEST_TX_DESC_DEFAULT 1024
45 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
46 #define MAX_RX_QUEUE_PER_PORT 128
48 #define MAX_RX_QUEUE_PER_LCORE 16
50 #define MAX_LCORE_PARAMS 1024
54 /* Static global variables used within this file. */
55 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
56 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
58 /**< Ports set in promiscuous mode off by default. */
59 static int promiscuous_on;
61 static int numa_on = 1; /**< NUMA is enabled by default. */
62 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
65 static volatile bool force_quit;
67 /* Ethernet addresses of ports */
68 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
69 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
70 xmm_t val_eth[RTE_MAX_ETHPORTS];
72 /* Mask of enabled ports */
73 static uint32_t enabled_port_mask;
75 struct lcore_rx_queue {
83 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
84 } __rte_cache_aligned;
86 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
92 } __rte_cache_aligned;
94 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
95 static struct lcore_params lcore_params_array_default[] = {
96 {0, 0, 2}, {0, 1, 2}, {0, 2, 2}, {1, 0, 2}, {1, 1, 2},
97 {1, 2, 2}, {2, 0, 2}, {3, 0, 3}, {3, 1, 3},
100 static struct lcore_params *lcore_params = lcore_params_array_default;
101 static uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
103 static struct rte_eth_conf port_conf = {
105 .mq_mode = ETH_MQ_RX_RSS,
106 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
112 .rss_hf = ETH_RSS_IP,
116 .mq_mode = ETH_MQ_TX_NONE,
120 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
123 check_lcore_params(void)
125 uint8_t queue, lcore;
129 for (i = 0; i < nb_lcore_params; ++i) {
130 queue = lcore_params[i].queue_id;
131 if (queue >= MAX_RX_QUEUE_PER_PORT) {
132 printf("Invalid queue number: %hhu\n", queue);
135 lcore = lcore_params[i].lcore_id;
136 if (!rte_lcore_is_enabled(lcore)) {
137 printf("Error: lcore %hhu is not enabled in lcore mask\n",
142 if (lcore == rte_get_master_lcore()) {
143 printf("Error: lcore %u is master lcore\n", lcore);
146 socketid = rte_lcore_to_socket_id(lcore);
147 if ((socketid != 0) && (numa_on == 0)) {
148 printf("Warning: lcore %hhu is on socket %d with numa off\n",
157 check_port_config(void)
162 for (i = 0; i < nb_lcore_params; ++i) {
163 portid = lcore_params[i].port_id;
164 if ((enabled_port_mask & (1 << portid)) == 0) {
165 printf("Port %u is not enabled in port mask\n", portid);
168 if (!rte_eth_dev_is_valid_port(portid)) {
169 printf("Port %u is not present on the board\n", portid);
178 get_port_n_rx_queues(const uint16_t port)
183 for (i = 0; i < nb_lcore_params; ++i) {
184 if (lcore_params[i].port_id == port) {
185 if (lcore_params[i].queue_id == queue + 1)
186 queue = lcore_params[i].queue_id;
188 rte_exit(EXIT_FAILURE,
189 "Queue ids of the port %d must be"
190 " in sequence and must start with 0\n",
191 lcore_params[i].port_id);
195 return (uint8_t)(++queue);
199 init_lcore_rx_queues(void)
201 uint16_t i, nb_rx_queue;
204 for (i = 0; i < nb_lcore_params; ++i) {
205 lcore = lcore_params[i].lcore_id;
206 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
207 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
208 printf("Error: too many queues (%u) for lcore: %u\n",
209 (unsigned int)nb_rx_queue + 1,
210 (unsigned int)lcore);
214 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
215 lcore_params[i].port_id;
216 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
217 lcore_params[i].queue_id;
218 lcore_conf[lcore].n_rx_queue++;
226 print_usage(const char *prgname)
229 "%s [EAL options] --"
232 " --config (port,queue,lcore)[,(port,queue,lcore)]"
233 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
234 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
236 " [--per-port-pool]\n\n"
238 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
239 " -P : Enable promiscuous mode\n"
240 " --config (port,queue,lcore): Rx queue configuration\n"
241 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for "
243 " --enable-jumbo: Enable jumbo frames\n"
244 " --max-pkt-len: Under the premise of enabling jumbo,\n"
245 " maximum packet length in decimal (64-9600)\n"
246 " --no-numa: Disable numa awareness\n"
247 " --per-port-pool: Use separate buffer pool per port\n\n",
252 parse_max_pkt_len(const char *pktlen)
257 /* Parse decimal string */
258 len = strtoul(pktlen, &end, 10);
259 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
269 parse_portmask(const char *portmask)
274 /* Parse hexadecimal string */
275 pm = strtoul(portmask, &end, 16);
276 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
286 parse_config(const char *q_arg)
288 enum fieldnames { FLD_PORT = 0, FLD_QUEUE, FLD_LCORE, _NUM_FLD };
289 unsigned long int_fld[_NUM_FLD];
290 const char *p, *p0 = q_arg;
291 char *str_fld[_NUM_FLD];
299 while ((p = strchr(p0, '(')) != NULL) {
306 if (size >= sizeof(s))
311 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
314 for (i = 0; i < _NUM_FLD; i++) {
316 int_fld[i] = strtoul(str_fld[i], &end, 0);
317 if (errno != 0 || end == str_fld[i])
321 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
322 printf("Exceeded max number of lcore params: %hu\n",
327 if (int_fld[FLD_PORT] >= RTE_MAX_ETHPORTS ||
328 int_fld[FLD_LCORE] >= RTE_MAX_LCORE) {
329 printf("Invalid port/lcore id\n");
333 lcore_params_array[nb_lcore_params].port_id =
334 (uint8_t)int_fld[FLD_PORT];
335 lcore_params_array[nb_lcore_params].queue_id =
336 (uint8_t)int_fld[FLD_QUEUE];
337 lcore_params_array[nb_lcore_params].lcore_id =
338 (uint8_t)int_fld[FLD_LCORE];
341 lcore_params = lcore_params_array;
347 parse_eth_dest(const char *optarg)
349 uint8_t c, *dest, peer_addr[6];
354 portid = strtoul(optarg, &port_end, 10);
355 if (errno != 0 || port_end == optarg || *port_end++ != ',')
356 rte_exit(EXIT_FAILURE, "Invalid eth-dest: %s", optarg);
357 if (portid >= RTE_MAX_ETHPORTS)
358 rte_exit(EXIT_FAILURE,
359 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n", portid,
362 if (cmdline_parse_etheraddr(NULL, port_end, &peer_addr,
363 sizeof(peer_addr)) < 0)
364 rte_exit(EXIT_FAILURE, "Invalid ethernet address: %s\n",
366 dest = (uint8_t *)&dest_eth_addr[portid];
367 for (c = 0; c < 6; c++)
368 dest[c] = peer_addr[c];
369 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
372 #define MAX_JUMBO_PKT_LEN 9600
373 #define MEMPOOL_CACHE_SIZE 256
375 static const char short_options[] = "p:" /* portmask */
376 "P" /* promiscuous */
379 #define CMD_LINE_OPT_CONFIG "config"
380 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
381 #define CMD_LINE_OPT_NO_NUMA "no-numa"
382 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
383 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
385 /* Long options mapped to a short option */
387 /* First long only option value must be >= 256, so that we won't
388 * conflict with short options
390 CMD_LINE_OPT_MIN_NUM = 256,
391 CMD_LINE_OPT_CONFIG_NUM,
392 CMD_LINE_OPT_ETH_DEST_NUM,
393 CMD_LINE_OPT_NO_NUMA_NUM,
394 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
395 CMD_LINE_OPT_PARSE_PER_PORT_POOL,
398 static const struct option lgopts[] = {
399 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
400 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
401 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
402 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
403 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
408 * This expression is used to calculate the number of mbufs needed
409 * depending on user input, taking into account memory for rx and
410 * tx hardware rings, cache per lcore and mtable per port per lcore.
411 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
414 #define NB_MBUF(nports) \
415 RTE_MAX((nports * nb_rx_queue * nb_rxd + \
416 nports * nb_lcores * RTE_GRAPH_BURST_SIZE + \
417 nports * n_tx_queue * nb_txd + \
418 nb_lcores * MEMPOOL_CACHE_SIZE), 8192u)
420 /* Parse the argument given in the command line of the application */
422 parse_args(int argc, char **argv)
424 char *prgname = argv[0];
431 /* Error or normal output strings. */
432 while ((opt = getopt_long(argc, argvopt, short_options, lgopts,
433 &option_index)) != EOF) {
438 enabled_port_mask = parse_portmask(optarg);
439 if (enabled_port_mask == 0) {
440 fprintf(stderr, "Invalid portmask\n");
441 print_usage(prgname);
451 case CMD_LINE_OPT_CONFIG_NUM:
452 ret = parse_config(optarg);
454 fprintf(stderr, "Invalid config\n");
455 print_usage(prgname);
460 case CMD_LINE_OPT_ETH_DEST_NUM:
461 parse_eth_dest(optarg);
464 case CMD_LINE_OPT_NO_NUMA_NUM:
468 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
469 const struct option lenopts = {"max-pkt-len",
470 required_argument, 0, 0};
472 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
473 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
476 * if no max-pkt-len set, use the default
477 * value RTE_ETHER_MAX_LEN.
479 if (getopt_long(argc, argvopt, "", &lenopts,
480 &option_index) == 0) {
481 ret = parse_max_pkt_len(optarg);
482 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
483 fprintf(stderr, "Invalid maximum "
485 print_usage(prgname);
488 port_conf.rxmode.max_rx_pkt_len = ret;
493 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
494 printf("Per port buffer pool is enabled\n");
499 print_usage(prgname);
505 argv[optind - 1] = prgname;
507 optind = 1; /* Reset getopt lib */
513 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
515 char buf[RTE_ETHER_ADDR_FMT_SIZE];
516 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
517 printf("%s%s", name, buf);
521 init_mem(uint16_t portid, uint32_t nb_mbuf)
527 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
528 if (rte_lcore_is_enabled(lcore_id) == 0)
532 socketid = rte_lcore_to_socket_id(lcore_id);
536 if (socketid >= NB_SOCKETS) {
537 rte_exit(EXIT_FAILURE,
538 "Socket %d of lcore %u is out of range %d\n",
539 socketid, lcore_id, NB_SOCKETS);
542 if (pktmbuf_pool[portid][socketid] == NULL) {
543 snprintf(s, sizeof(s), "mbuf_pool_%d:%d", portid,
545 /* Create a pool with priv size of a cacheline */
546 pktmbuf_pool[portid][socketid] =
547 rte_pktmbuf_pool_create(
548 s, nb_mbuf, MEMPOOL_CACHE_SIZE,
550 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
551 if (pktmbuf_pool[portid][socketid] == NULL)
552 rte_exit(EXIT_FAILURE,
553 "Cannot init mbuf pool on socket %d\n",
556 printf("Allocated mbuf pool on socket %d\n",
564 /* Check the link status of all ports in up to 9s, and print them finally */
566 check_all_ports_link_status(uint32_t port_mask)
568 #define CHECK_INTERVAL 100 /* 100ms */
569 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
570 uint8_t count, all_ports_up, print_flag = 0;
571 struct rte_eth_link link;
574 printf("\nChecking link status");
576 for (count = 0; count <= MAX_CHECK_TIME; count++) {
580 RTE_ETH_FOREACH_DEV(portid)
584 if ((port_mask & (1 << portid)) == 0)
586 memset(&link, 0, sizeof(link));
587 rte_eth_link_get_nowait(portid, &link);
588 /* Print link status if flag set */
589 if (print_flag == 1) {
590 if (link.link_status)
591 printf("Port%d Link Up. Speed %u Mbps "
593 portid, link.link_speed,
595 ETH_LINK_FULL_DUPLEX)
597 : ("half-duplex\n"));
599 printf("Port %d Link Down\n", portid);
602 /* Clear all_ports_up flag if any link down */
603 if (link.link_status == ETH_LINK_DOWN) {
608 /* After finally printing all link status, get out */
612 if (all_ports_up == 0) {
615 rte_delay_ms(CHECK_INTERVAL);
618 /* Set the print_flag if all ports up or timeout */
619 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
627 signal_handler(int signum)
629 if (signum == SIGINT || signum == SIGTERM) {
630 printf("\n\nSignal %d received, preparing to exit...\n",
637 main(int argc, char **argv)
639 uint8_t nb_rx_queue, queue, socketid;
640 struct rte_eth_dev_info dev_info;
641 uint32_t n_tx_queue, nb_lcores;
642 struct rte_eth_txconf *txconf;
643 uint16_t queueid, portid;
644 struct lcore_conf *qconf;
650 ret = rte_eal_init(argc, argv);
652 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
657 signal(SIGINT, signal_handler);
658 signal(SIGTERM, signal_handler);
660 /* Pre-init dst MACs for all ports to 02:00:00:00:00:xx */
661 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
662 dest_eth_addr[portid] =
663 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
664 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
667 /* Parse application arguments (after the EAL ones) */
668 ret = parse_args(argc, argv);
670 rte_exit(EXIT_FAILURE, "Invalid L3FWD_GRAPH parameters\n");
672 if (check_lcore_params() < 0)
673 rte_exit(EXIT_FAILURE, "check_lcore_params() failed\n");
675 ret = init_lcore_rx_queues();
677 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues() failed\n");
679 if (check_port_config() < 0)
680 rte_exit(EXIT_FAILURE, "check_port_config() failed\n");
682 nb_ports = rte_eth_dev_count_avail();
683 nb_lcores = rte_lcore_count();
685 /* Initialize all ports */
686 RTE_ETH_FOREACH_DEV(portid)
688 struct rte_eth_conf local_port_conf = port_conf;
690 /* Skip ports that are not enabled */
691 if ((enabled_port_mask & (1 << portid)) == 0) {
692 printf("\nSkipping disabled port %d\n", portid);
697 printf("Initializing port %d ... ", portid);
700 nb_rx_queue = get_port_n_rx_queues(portid);
701 n_tx_queue = nb_lcores;
702 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
703 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
704 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
705 nb_rx_queue, n_tx_queue);
707 rte_eth_dev_info_get(portid, &dev_info);
708 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
709 local_port_conf.txmode.offloads |=
710 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
712 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
713 dev_info.flow_type_rss_offloads;
714 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
715 port_conf.rx_adv_conf.rss_conf.rss_hf) {
716 printf("Port %u modified RSS hash function based on "
718 "requested:%#" PRIx64 " configured:%#" PRIx64
720 portid, port_conf.rx_adv_conf.rss_conf.rss_hf,
721 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
724 ret = rte_eth_dev_configure(portid, nb_rx_queue,
725 n_tx_queue, &local_port_conf);
727 rte_exit(EXIT_FAILURE,
728 "Cannot configure device: err=%d, port=%d\n",
731 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
734 rte_exit(EXIT_FAILURE,
735 "Cannot adjust number of descriptors: err=%d, "
739 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
740 print_ethaddr(" Address:", &ports_eth_addr[portid]);
744 (const struct rte_ether_addr *)&dest_eth_addr[portid]);
748 * prepare src MACs for each port.
751 &ports_eth_addr[portid],
752 (struct rte_ether_addr *)(val_eth + portid) + 1);
755 if (!per_port_pool) {
756 /* portid = 0; this is *not* signifying the first port,
757 * rather, it signifies that portid is ignored.
759 ret = init_mem(0, NB_MBUF(nb_ports));
761 ret = init_mem(portid, NB_MBUF(1));
764 rte_exit(EXIT_FAILURE, "init_mem() failed\n");
766 /* Init one TX queue per couple (lcore,port) */
768 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
769 if (rte_lcore_is_enabled(lcore_id) == 0)
772 qconf = &lcore_conf[lcore_id];
775 socketid = (uint8_t)rte_lcore_to_socket_id(
780 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
783 txconf = &dev_info.default_txconf;
784 txconf->offloads = local_port_conf.txmode.offloads;
785 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
788 rte_exit(EXIT_FAILURE,
789 "rte_eth_tx_queue_setup: err=%d, "
798 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
799 if (rte_lcore_is_enabled(lcore_id) == 0)
801 qconf = &lcore_conf[lcore_id];
802 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
805 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
806 struct rte_eth_rxconf rxq_conf;
808 portid = qconf->rx_queue_list[queue].port_id;
809 queueid = qconf->rx_queue_list[queue].queue_id;
812 socketid = (uint8_t)rte_lcore_to_socket_id(
817 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
820 rte_eth_dev_info_get(portid, &dev_info);
821 rxq_conf = dev_info.default_rxconf;
822 rxq_conf.offloads = port_conf.rxmode.offloads;
824 ret = rte_eth_rx_queue_setup(
825 portid, queueid, nb_rxd, socketid,
826 &rxq_conf, pktmbuf_pool[0][socketid]);
828 ret = rte_eth_rx_queue_setup(
829 portid, queueid, nb_rxd, socketid,
831 pktmbuf_pool[portid][socketid]);
833 rte_exit(EXIT_FAILURE,
834 "rte_eth_rx_queue_setup: err=%d, "
844 RTE_ETH_FOREACH_DEV(portid)
846 if ((enabled_port_mask & (1 << portid)) == 0)
850 ret = rte_eth_dev_start(portid);
852 rte_exit(EXIT_FAILURE,
853 "rte_eth_dev_start: err=%d, port=%d\n", ret,
857 * If enabled, put device in promiscuous mode.
858 * This allows IO forwarding mode to forward packets
859 * to itself through 2 cross-connected ports of the
863 rte_eth_promiscuous_enable(portid);
868 check_all_ports_link_status(enabled_port_mask);
871 RTE_ETH_FOREACH_DEV(portid) {
872 if ((enabled_port_mask & (1 << portid)) == 0)
874 printf("Closing port %d...", portid);
875 rte_eth_dev_stop(portid);
876 rte_eth_dev_close(portid);