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>
24 #include <rte_ethdev.h>
26 #include <rte_mempool.h>
27 #include <rte_per_lcore.h>
28 #include <rte_string_fns.h>
31 #include <cmdline_parse.h>
32 #include <cmdline_parse_etheraddr.h>
35 #define RTE_LOGTYPE_L3FWD_GRAPH RTE_LOGTYPE_USER1
38 * Configurable number of RX/TX ring descriptors
40 #define RTE_TEST_RX_DESC_DEFAULT 1024
41 #define RTE_TEST_TX_DESC_DEFAULT 1024
43 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
44 #define MAX_RX_QUEUE_PER_PORT 128
46 #define MAX_RX_QUEUE_PER_LCORE 16
48 #define MAX_LCORE_PARAMS 1024
52 /**< Ports set in promiscuous mode off by default. */
53 static int promiscuous_on;
55 static int numa_on = 1; /**< NUMA is enabled by default. */
56 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
59 static volatile bool force_quit;
61 /* Ethernet addresses of ports */
62 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
63 xmm_t val_eth[RTE_MAX_ETHPORTS];
65 /* Mask of enabled ports */
66 static uint32_t enabled_port_mask;
68 struct lcore_rx_queue {
76 struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
77 } __rte_cache_aligned;
79 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
85 } __rte_cache_aligned;
87 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
88 static struct lcore_params lcore_params_array_default[] = {
89 {0, 0, 2}, {0, 1, 2}, {0, 2, 2}, {1, 0, 2}, {1, 1, 2},
90 {1, 2, 2}, {2, 0, 2}, {3, 0, 3}, {3, 1, 3},
93 static struct lcore_params *lcore_params = lcore_params_array_default;
94 static uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
96 static struct rte_eth_conf port_conf = {
98 .mq_mode = ETH_MQ_RX_RSS,
99 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
105 .rss_hf = ETH_RSS_IP,
109 .mq_mode = ETH_MQ_TX_NONE,
114 check_lcore_params(void)
116 uint8_t queue, lcore;
120 for (i = 0; i < nb_lcore_params; ++i) {
121 queue = lcore_params[i].queue_id;
122 if (queue >= MAX_RX_QUEUE_PER_PORT) {
123 printf("Invalid queue number: %hhu\n", queue);
126 lcore = lcore_params[i].lcore_id;
127 if (!rte_lcore_is_enabled(lcore)) {
128 printf("Error: lcore %hhu is not enabled in lcore mask\n",
133 if (lcore == rte_get_master_lcore()) {
134 printf("Error: lcore %u is master lcore\n", lcore);
137 socketid = rte_lcore_to_socket_id(lcore);
138 if ((socketid != 0) && (numa_on == 0)) {
139 printf("Warning: lcore %hhu is on socket %d with numa off\n",
148 check_port_config(void)
153 for (i = 0; i < nb_lcore_params; ++i) {
154 portid = lcore_params[i].port_id;
155 if ((enabled_port_mask & (1 << portid)) == 0) {
156 printf("Port %u is not enabled in port mask\n", portid);
159 if (!rte_eth_dev_is_valid_port(portid)) {
160 printf("Port %u is not present on the board\n", portid);
169 init_lcore_rx_queues(void)
171 uint16_t i, nb_rx_queue;
174 for (i = 0; i < nb_lcore_params; ++i) {
175 lcore = lcore_params[i].lcore_id;
176 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
177 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
178 printf("Error: too many queues (%u) for lcore: %u\n",
179 (unsigned int)nb_rx_queue + 1,
180 (unsigned int)lcore);
184 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
185 lcore_params[i].port_id;
186 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
187 lcore_params[i].queue_id;
188 lcore_conf[lcore].n_rx_queue++;
196 print_usage(const char *prgname)
199 "%s [EAL options] --"
202 " --config (port,queue,lcore)[,(port,queue,lcore)]"
203 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
204 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
206 " [--per-port-pool]\n\n"
208 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
209 " -P : Enable promiscuous mode\n"
210 " --config (port,queue,lcore): Rx queue configuration\n"
211 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for "
213 " --enable-jumbo: Enable jumbo frames\n"
214 " --max-pkt-len: Under the premise of enabling jumbo,\n"
215 " maximum packet length in decimal (64-9600)\n"
216 " --no-numa: Disable numa awareness\n"
217 " --per-port-pool: Use separate buffer pool per port\n\n",
222 parse_max_pkt_len(const char *pktlen)
227 /* Parse decimal string */
228 len = strtoul(pktlen, &end, 10);
229 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
239 parse_portmask(const char *portmask)
244 /* Parse hexadecimal string */
245 pm = strtoul(portmask, &end, 16);
246 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
256 parse_config(const char *q_arg)
258 enum fieldnames { FLD_PORT = 0, FLD_QUEUE, FLD_LCORE, _NUM_FLD };
259 unsigned long int_fld[_NUM_FLD];
260 const char *p, *p0 = q_arg;
261 char *str_fld[_NUM_FLD];
269 while ((p = strchr(p0, '(')) != NULL) {
276 if (size >= sizeof(s))
281 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
284 for (i = 0; i < _NUM_FLD; i++) {
286 int_fld[i] = strtoul(str_fld[i], &end, 0);
287 if (errno != 0 || end == str_fld[i])
291 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
292 printf("Exceeded max number of lcore params: %hu\n",
297 if (int_fld[FLD_PORT] >= RTE_MAX_ETHPORTS ||
298 int_fld[FLD_LCORE] >= RTE_MAX_LCORE) {
299 printf("Invalid port/lcore id\n");
303 lcore_params_array[nb_lcore_params].port_id =
304 (uint8_t)int_fld[FLD_PORT];
305 lcore_params_array[nb_lcore_params].queue_id =
306 (uint8_t)int_fld[FLD_QUEUE];
307 lcore_params_array[nb_lcore_params].lcore_id =
308 (uint8_t)int_fld[FLD_LCORE];
311 lcore_params = lcore_params_array;
317 parse_eth_dest(const char *optarg)
319 uint8_t c, *dest, peer_addr[6];
324 portid = strtoul(optarg, &port_end, 10);
325 if (errno != 0 || port_end == optarg || *port_end++ != ',')
326 rte_exit(EXIT_FAILURE, "Invalid eth-dest: %s", optarg);
327 if (portid >= RTE_MAX_ETHPORTS)
328 rte_exit(EXIT_FAILURE,
329 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n", portid,
332 if (cmdline_parse_etheraddr(NULL, port_end, &peer_addr,
333 sizeof(peer_addr)) < 0)
334 rte_exit(EXIT_FAILURE, "Invalid ethernet address: %s\n",
336 dest = (uint8_t *)&dest_eth_addr[portid];
337 for (c = 0; c < 6; c++)
338 dest[c] = peer_addr[c];
339 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
342 #define MAX_JUMBO_PKT_LEN 9600
343 #define MEMPOOL_CACHE_SIZE 256
345 static const char short_options[] = "p:" /* portmask */
346 "P" /* promiscuous */
349 #define CMD_LINE_OPT_CONFIG "config"
350 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
351 #define CMD_LINE_OPT_NO_NUMA "no-numa"
352 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
353 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
355 /* Long options mapped to a short option */
357 /* First long only option value must be >= 256, so that we won't
358 * conflict with short options
360 CMD_LINE_OPT_MIN_NUM = 256,
361 CMD_LINE_OPT_CONFIG_NUM,
362 CMD_LINE_OPT_ETH_DEST_NUM,
363 CMD_LINE_OPT_NO_NUMA_NUM,
364 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
365 CMD_LINE_OPT_PARSE_PER_PORT_POOL,
368 static const struct option lgopts[] = {
369 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
370 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
371 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
372 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
373 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
378 * This expression is used to calculate the number of mbufs needed
379 * depending on user input, taking into account memory for rx and
380 * tx hardware rings, cache per lcore and mtable per port per lcore.
381 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
384 #define NB_MBUF(nports) \
385 RTE_MAX((nports * nb_rx_queue * nb_rxd + \
386 nports * nb_lcores * RTE_GRAPH_BURST_SIZE + \
387 nports * n_tx_queue * nb_txd + \
388 nb_lcores * MEMPOOL_CACHE_SIZE), 8192u)
390 /* Parse the argument given in the command line of the application */
392 parse_args(int argc, char **argv)
394 char *prgname = argv[0];
401 /* Error or normal output strings. */
402 while ((opt = getopt_long(argc, argvopt, short_options, lgopts,
403 &option_index)) != EOF) {
408 enabled_port_mask = parse_portmask(optarg);
409 if (enabled_port_mask == 0) {
410 fprintf(stderr, "Invalid portmask\n");
411 print_usage(prgname);
421 case CMD_LINE_OPT_CONFIG_NUM:
422 ret = parse_config(optarg);
424 fprintf(stderr, "Invalid config\n");
425 print_usage(prgname);
430 case CMD_LINE_OPT_ETH_DEST_NUM:
431 parse_eth_dest(optarg);
434 case CMD_LINE_OPT_NO_NUMA_NUM:
438 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
439 const struct option lenopts = {"max-pkt-len",
440 required_argument, 0, 0};
442 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
443 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
446 * if no max-pkt-len set, use the default
447 * value RTE_ETHER_MAX_LEN.
449 if (getopt_long(argc, argvopt, "", &lenopts,
450 &option_index) == 0) {
451 ret = parse_max_pkt_len(optarg);
452 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
453 fprintf(stderr, "Invalid maximum "
455 print_usage(prgname);
458 port_conf.rxmode.max_rx_pkt_len = ret;
463 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
464 printf("Per port buffer pool is enabled\n");
469 print_usage(prgname);
475 argv[optind - 1] = prgname;
477 optind = 1; /* Reset getopt lib */
483 signal_handler(int signum)
485 if (signum == SIGINT || signum == SIGTERM) {
486 printf("\n\nSignal %d received, preparing to exit...\n",
493 main(int argc, char **argv)
499 ret = rte_eal_init(argc, argv);
501 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
506 signal(SIGINT, signal_handler);
507 signal(SIGTERM, signal_handler);
509 /* Pre-init dst MACs for all ports to 02:00:00:00:00:xx */
510 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
511 dest_eth_addr[portid] =
512 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
513 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
516 /* Parse application arguments (after the EAL ones) */
517 ret = parse_args(argc, argv);
519 rte_exit(EXIT_FAILURE, "Invalid L3FWD_GRAPH parameters\n");
521 if (check_lcore_params() < 0)
522 rte_exit(EXIT_FAILURE, "check_lcore_params() failed\n");
524 ret = init_lcore_rx_queues();
526 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues() failed\n");
528 if (check_port_config() < 0)
529 rte_exit(EXIT_FAILURE, "check_port_config() failed\n");