1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2021 Intel Corporation
11 #include <sys/queue.h>
18 #include <rte_common.h>
20 #include <rte_byteorder.h>
22 #include <rte_malloc.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.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_mempool.h>
41 #include <rte_string_fns.h>
42 #include <rte_cpuflags.h>
44 #include <cmdline_parse.h>
45 #include <cmdline_parse_etheraddr.h>
48 #include "l3fwd_event.h"
49 #include "l3fwd_route.h"
51 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_LCORE
52 #define MAX_RX_QUEUE_PER_PORT 128
54 #define MAX_LCORE_PARAMS 1024
56 /* Static global variables used within this file. */
57 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
58 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
60 /**< Ports set in promiscuous mode off by default. */
61 static int promiscuous_on;
63 /* Select Longest-Prefix, Exact match or Forwarding Information Base. */
64 enum L3FWD_LOOKUP_MODE {
70 static enum L3FWD_LOOKUP_MODE lookup_mode;
72 /* Global variables. */
74 static int numa_on = 1; /**< NUMA is enabled by default. */
75 static int parse_ptype; /**< Parse packet type using rx callback, and */
76 /**< disabled by default */
77 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
80 volatile bool force_quit;
82 /* ethernet addresses of ports */
83 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
84 struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
86 xmm_t val_eth[RTE_MAX_ETHPORTS];
88 /* mask of enabled ports */
89 uint32_t enabled_port_mask;
91 /* Used only in exact match mode. */
92 int ipv6; /**< ipv6 is false by default. */
93 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
95 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[] = {
116 static struct lcore_params * lcore_params = lcore_params_array_default;
117 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
118 sizeof(lcore_params_array_default[0]);
120 static struct rte_eth_conf port_conf = {
122 .mq_mode = ETH_MQ_RX_RSS,
123 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
125 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
130 .rss_hf = ETH_RSS_IP,
134 .mq_mode = ETH_MQ_TX_NONE,
138 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
139 static uint8_t lkp_per_socket[NB_SOCKETS];
141 struct l3fwd_lkp_mode {
143 int (*check_ptype)(int);
144 rte_rx_callback_fn cb_parse_ptype;
145 int (*main_loop)(void *);
146 void* (*get_ipv4_lookup_struct)(int);
147 void* (*get_ipv6_lookup_struct)(int);
150 static struct l3fwd_lkp_mode l3fwd_lkp;
152 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
154 .check_ptype = em_check_ptype,
155 .cb_parse_ptype = em_cb_parse_ptype,
156 .main_loop = em_main_loop,
157 .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
158 .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
161 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
163 .check_ptype = lpm_check_ptype,
164 .cb_parse_ptype = lpm_cb_parse_ptype,
165 .main_loop = lpm_main_loop,
166 .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
167 .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
170 static struct l3fwd_lkp_mode l3fwd_fib_lkp = {
172 .check_ptype = lpm_check_ptype,
173 .cb_parse_ptype = lpm_cb_parse_ptype,
174 .main_loop = fib_main_loop,
175 .get_ipv4_lookup_struct = fib_get_ipv4_l3fwd_lookup_struct,
176 .get_ipv6_lookup_struct = fib_get_ipv6_l3fwd_lookup_struct,
180 * 198.18.0.0/16 are set aside for RFC2544 benchmarking (RFC5735).
181 * 198.18.{0-7}.0/24 = Port {0-7}
183 const struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
184 {RTE_IPV4(198, 18, 0, 0), 24, 0},
185 {RTE_IPV4(198, 18, 1, 0), 24, 1},
186 {RTE_IPV4(198, 18, 2, 0), 24, 2},
187 {RTE_IPV4(198, 18, 3, 0), 24, 3},
188 {RTE_IPV4(198, 18, 4, 0), 24, 4},
189 {RTE_IPV4(198, 18, 5, 0), 24, 5},
190 {RTE_IPV4(198, 18, 6, 0), 24, 6},
191 {RTE_IPV4(198, 18, 7, 0), 24, 7},
195 * 2001:200::/48 is IANA reserved range for IPv6 benchmarking (RFC5180).
196 * 2001:200:0:{0-7}::/64 = Port {0-7}
198 const struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
199 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 0},
200 {{32, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 1},
201 {{32, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 2},
202 {{32, 1, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 3},
203 {{32, 1, 2, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 4},
204 {{32, 1, 2, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 5},
205 {{32, 1, 2, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 6},
206 {{32, 1, 2, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 7},
210 * Setup lookup methods for forwarding.
211 * Currently exact-match, longest-prefix-match and forwarding information
212 * base are the supported ones.
215 setup_l3fwd_lookup_tables(void)
217 /* Setup HASH lookup functions. */
218 if (lookup_mode == L3FWD_LOOKUP_EM)
219 l3fwd_lkp = l3fwd_em_lkp;
220 /* Setup FIB lookup functions. */
221 else if (lookup_mode == L3FWD_LOOKUP_FIB)
222 l3fwd_lkp = l3fwd_fib_lkp;
223 /* Setup LPM lookup functions. */
225 l3fwd_lkp = l3fwd_lpm_lkp;
229 check_lcore_params(void)
231 uint8_t queue, lcore;
235 for (i = 0; i < nb_lcore_params; ++i) {
236 queue = lcore_params[i].queue_id;
237 if (queue >= MAX_RX_QUEUE_PER_PORT) {
238 printf("invalid queue number: %hhu\n", queue);
241 lcore = lcore_params[i].lcore_id;
242 if (!rte_lcore_is_enabled(lcore)) {
243 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
246 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
248 printf("warning: lcore %hhu is on socket %d with numa off \n",
256 check_port_config(void)
261 for (i = 0; i < nb_lcore_params; ++i) {
262 portid = lcore_params[i].port_id;
263 if ((enabled_port_mask & (1 << portid)) == 0) {
264 printf("port %u is not enabled in port mask\n", portid);
267 if (!rte_eth_dev_is_valid_port(portid)) {
268 printf("port %u is not present on the board\n", portid);
276 get_port_n_rx_queues(const uint16_t port)
281 for (i = 0; i < nb_lcore_params; ++i) {
282 if (lcore_params[i].port_id == port) {
283 if (lcore_params[i].queue_id == queue+1)
284 queue = lcore_params[i].queue_id;
286 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
287 " in sequence and must start with 0\n",
288 lcore_params[i].port_id);
291 return (uint8_t)(++queue);
295 init_lcore_rx_queues(void)
297 uint16_t i, nb_rx_queue;
300 for (i = 0; i < nb_lcore_params; ++i) {
301 lcore = lcore_params[i].lcore_id;
302 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
303 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
304 printf("error: too many queues (%u) for lcore: %u\n",
305 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
308 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
309 lcore_params[i].port_id;
310 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
311 lcore_params[i].queue_id;
312 lcore_conf[lcore].n_rx_queue++;
320 print_usage(const char *prgname)
322 fprintf(stderr, "%s [EAL options] --"
326 " --config (port,queue,lcore)[,(port,queue,lcore)]"
327 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
328 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
330 " [--hash-entry-num]"
339 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
340 " -P : Enable promiscuous mode\n"
341 " --lookup: Select the lookup method\n"
343 " Accepted: em (Exact Match), lpm (Longest Prefix Match), fib (Forwarding Information Base)\n"
344 " --config (port,queue,lcore): Rx queue configuration\n"
345 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
346 " --enable-jumbo: Enable jumbo frames\n"
347 " --max-pkt-len: Under the premise of enabling jumbo,\n"
348 " maximum packet length in decimal (64-9600)\n"
349 " --no-numa: Disable numa awareness\n"
350 " --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
351 " --ipv6: Set if running ipv6 packets\n"
352 " --parse-ptype: Set to use software to analyze packet type\n"
353 " --per-port-pool: Use separate buffer pool per port\n"
354 " --mode: Packet transfer mode for I/O, poll or eventdev\n"
355 " Default mode = poll\n"
356 " --eventq-sched: Event queue synchronization method\n"
357 " ordered, atomic or parallel.\n"
359 " Valid only if --mode=eventdev\n"
360 " --event-eth-rxqs: Number of ethernet RX queues per device.\n"
362 " Valid only if --mode=eventdev\n"
363 " -E : Enable exact match, legacy flag please use --lookup=em instead\n"
364 " -L : Enable longest prefix match, legacy flag please use --lookup=lpm instead\n\n",
369 parse_max_pkt_len(const char *pktlen)
374 /* parse decimal string */
375 len = strtoul(pktlen, &end, 10);
376 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
386 parse_portmask(const char *portmask)
391 /* parse hexadecimal string */
392 pm = strtoul(portmask, &end, 16);
393 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
400 parse_hash_entry_number(const char *hash_entry_num)
403 unsigned long hash_en;
404 /* parse hexadecimal string */
405 hash_en = strtoul(hash_entry_num, &end, 16);
406 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
416 parse_config(const char *q_arg)
419 const char *p, *p0 = q_arg;
427 unsigned long int_fld[_NUM_FLD];
428 char *str_fld[_NUM_FLD];
434 while ((p = strchr(p0,'(')) != NULL) {
436 if((p0 = strchr(p,')')) == NULL)
440 if(size >= sizeof(s))
443 snprintf(s, sizeof(s), "%.*s", size, p);
444 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
446 for (i = 0; i < _NUM_FLD; i++){
448 int_fld[i] = strtoul(str_fld[i], &end, 0);
449 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
452 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
453 printf("exceeded max number of lcore params: %hu\n",
457 lcore_params_array[nb_lcore_params].port_id =
458 (uint8_t)int_fld[FLD_PORT];
459 lcore_params_array[nb_lcore_params].queue_id =
460 (uint8_t)int_fld[FLD_QUEUE];
461 lcore_params_array[nb_lcore_params].lcore_id =
462 (uint8_t)int_fld[FLD_LCORE];
465 lcore_params = lcore_params_array;
470 parse_eth_dest(const char *optarg)
474 uint8_t c, *dest, peer_addr[6];
477 portid = strtoul(optarg, &port_end, 10);
478 if (errno != 0 || port_end == optarg || *port_end++ != ',')
479 rte_exit(EXIT_FAILURE,
480 "Invalid eth-dest: %s", optarg);
481 if (portid >= RTE_MAX_ETHPORTS)
482 rte_exit(EXIT_FAILURE,
483 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
484 portid, RTE_MAX_ETHPORTS);
486 if (cmdline_parse_etheraddr(NULL, port_end,
487 &peer_addr, sizeof(peer_addr)) < 0)
488 rte_exit(EXIT_FAILURE,
489 "Invalid ethernet address: %s\n",
491 dest = (uint8_t *)&dest_eth_addr[portid];
492 for (c = 0; c < 6; c++)
493 dest[c] = peer_addr[c];
494 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
498 parse_mode(const char *optarg)
500 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
502 if (!strcmp(optarg, "poll"))
503 evt_rsrc->enabled = false;
504 else if (!strcmp(optarg, "eventdev"))
505 evt_rsrc->enabled = true;
509 parse_eventq_sched(const char *optarg)
511 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
513 if (!strcmp(optarg, "ordered"))
514 evt_rsrc->sched_type = RTE_SCHED_TYPE_ORDERED;
515 if (!strcmp(optarg, "atomic"))
516 evt_rsrc->sched_type = RTE_SCHED_TYPE_ATOMIC;
517 if (!strcmp(optarg, "parallel"))
518 evt_rsrc->sched_type = RTE_SCHED_TYPE_PARALLEL;
522 parse_event_eth_rx_queues(const char *eth_rx_queues)
524 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
526 uint8_t num_eth_rx_queues;
528 /* parse decimal string */
529 num_eth_rx_queues = strtoul(eth_rx_queues, &end, 10);
530 if ((eth_rx_queues[0] == '\0') || (end == NULL) || (*end != '\0'))
533 if (num_eth_rx_queues == 0)
536 evt_rsrc->eth_rx_queues = num_eth_rx_queues;
540 parse_lookup(const char *optarg)
542 if (!strcmp(optarg, "em"))
543 lookup_mode = L3FWD_LOOKUP_EM;
544 else if (!strcmp(optarg, "lpm"))
545 lookup_mode = L3FWD_LOOKUP_LPM;
546 else if (!strcmp(optarg, "fib"))
547 lookup_mode = L3FWD_LOOKUP_FIB;
549 fprintf(stderr, "Invalid lookup option! Accepted options: em, lpm, fib\n");
555 #define MAX_JUMBO_PKT_LEN 9600
557 static const char short_options[] =
559 "P" /* promiscuous */
560 "L" /* legacy enable long prefix match */
561 "E" /* legacy enable exact match */
564 #define CMD_LINE_OPT_CONFIG "config"
565 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
566 #define CMD_LINE_OPT_NO_NUMA "no-numa"
567 #define CMD_LINE_OPT_IPV6 "ipv6"
568 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
569 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
570 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
571 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
572 #define CMD_LINE_OPT_MODE "mode"
573 #define CMD_LINE_OPT_EVENTQ_SYNC "eventq-sched"
574 #define CMD_LINE_OPT_EVENT_ETH_RX_QUEUES "event-eth-rxqs"
575 #define CMD_LINE_OPT_LOOKUP "lookup"
577 /* long options mapped to a short option */
579 /* first long only option value must be >= 256, so that we won't
580 * conflict with short options */
581 CMD_LINE_OPT_MIN_NUM = 256,
582 CMD_LINE_OPT_CONFIG_NUM,
583 CMD_LINE_OPT_ETH_DEST_NUM,
584 CMD_LINE_OPT_NO_NUMA_NUM,
585 CMD_LINE_OPT_IPV6_NUM,
586 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
587 CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
588 CMD_LINE_OPT_PARSE_PTYPE_NUM,
589 CMD_LINE_OPT_PARSE_PER_PORT_POOL,
590 CMD_LINE_OPT_MODE_NUM,
591 CMD_LINE_OPT_EVENTQ_SYNC_NUM,
592 CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM,
593 CMD_LINE_OPT_LOOKUP_NUM,
596 static const struct option lgopts[] = {
597 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
598 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
599 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
600 {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
601 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
602 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
603 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
604 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
605 {CMD_LINE_OPT_MODE, 1, 0, CMD_LINE_OPT_MODE_NUM},
606 {CMD_LINE_OPT_EVENTQ_SYNC, 1, 0, CMD_LINE_OPT_EVENTQ_SYNC_NUM},
607 {CMD_LINE_OPT_EVENT_ETH_RX_QUEUES, 1, 0,
608 CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM},
609 {CMD_LINE_OPT_LOOKUP, 1, 0, CMD_LINE_OPT_LOOKUP_NUM},
614 * This expression is used to calculate the number of mbufs needed
615 * depending on user input, taking into account memory for rx and
616 * tx hardware rings, cache per lcore and mtable per port per lcore.
617 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
620 #define NB_MBUF(nports) RTE_MAX( \
621 (nports*nb_rx_queue*nb_rxd + \
622 nports*nb_lcores*MAX_PKT_BURST + \
623 nports*n_tx_queue*nb_txd + \
624 nb_lcores*MEMPOOL_CACHE_SIZE), \
627 /* Parse the argument given in the command line of the application */
629 parse_args(int argc, char **argv)
634 char *prgname = argv[0];
635 uint8_t lcore_params = 0;
636 uint8_t eventq_sched = 0;
637 uint8_t eth_rx_q = 0;
638 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
642 /* Error or normal output strings. */
643 while ((opt = getopt_long(argc, argvopt, short_options,
644 lgopts, &option_index)) != EOF) {
649 enabled_port_mask = parse_portmask(optarg);
650 if (enabled_port_mask == 0) {
651 fprintf(stderr, "Invalid portmask\n");
652 print_usage(prgname);
662 if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
663 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
666 lookup_mode = L3FWD_LOOKUP_EM;
670 if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
671 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
674 lookup_mode = L3FWD_LOOKUP_LPM;
678 case CMD_LINE_OPT_CONFIG_NUM:
679 ret = parse_config(optarg);
681 fprintf(stderr, "Invalid config\n");
682 print_usage(prgname);
688 case CMD_LINE_OPT_ETH_DEST_NUM:
689 parse_eth_dest(optarg);
692 case CMD_LINE_OPT_NO_NUMA_NUM:
696 case CMD_LINE_OPT_IPV6_NUM:
700 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
701 const struct option lenopts = {
702 "max-pkt-len", required_argument, 0, 0
705 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
706 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
709 * if no max-pkt-len set, use the default
710 * value RTE_ETHER_MAX_LEN.
712 if (getopt_long(argc, argvopt, "",
713 &lenopts, &option_index) == 0) {
714 ret = parse_max_pkt_len(optarg);
715 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
717 "invalid maximum packet length\n");
718 print_usage(prgname);
721 port_conf.rxmode.max_rx_pkt_len = ret;
726 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
727 ret = parse_hash_entry_number(optarg);
728 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
729 hash_entry_number = ret;
731 fprintf(stderr, "invalid hash entry number\n");
732 print_usage(prgname);
737 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
738 printf("soft parse-ptype is enabled\n");
742 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
743 printf("per port buffer pool is enabled\n");
747 case CMD_LINE_OPT_MODE_NUM:
751 case CMD_LINE_OPT_EVENTQ_SYNC_NUM:
752 parse_eventq_sched(optarg);
756 case CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM:
757 parse_event_eth_rx_queues(optarg);
761 case CMD_LINE_OPT_LOOKUP_NUM:
762 if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
763 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
766 ret = parse_lookup(optarg);
768 * If parse_lookup was passed an invalid lookup type
769 * then return -1. Error log included within
770 * parse_lookup for simplicity.
777 print_usage(prgname);
782 if (evt_rsrc->enabled && lcore_params) {
783 fprintf(stderr, "lcore config is not valid when event mode is selected\n");
787 if (!evt_rsrc->enabled && eth_rx_q) {
788 fprintf(stderr, "eth_rx_queues is valid only when event mode is selected\n");
792 if (!evt_rsrc->enabled && eventq_sched) {
793 fprintf(stderr, "eventq_sched is valid only when event mode is selected\n");
798 * Nothing is selected, pick longest-prefix match
801 if (lookup_mode == L3FWD_LOOKUP_DEFAULT) {
802 fprintf(stderr, "Neither LPM, EM, or FIB selected, defaulting to LPM\n");
803 lookup_mode = L3FWD_LOOKUP_LPM;
807 * ipv6 and hash flags are valid only for
808 * exact match, reset them to default for
809 * longest-prefix match.
811 if (lookup_mode == L3FWD_LOOKUP_LPM) {
813 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
817 argv[optind-1] = prgname;
820 optind = 1; /* reset getopt lib */
825 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
827 char buf[RTE_ETHER_ADDR_FMT_SIZE];
828 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
829 printf("%s%s", name, buf);
833 init_mem(uint16_t portid, unsigned int nb_mbuf)
835 struct lcore_conf *qconf;
840 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
841 if (rte_lcore_is_enabled(lcore_id) == 0)
845 socketid = rte_lcore_to_socket_id(lcore_id);
849 if (socketid >= NB_SOCKETS) {
850 rte_exit(EXIT_FAILURE,
851 "Socket %d of lcore %u is out of range %d\n",
852 socketid, lcore_id, NB_SOCKETS);
855 if (pktmbuf_pool[portid][socketid] == NULL) {
856 snprintf(s, sizeof(s), "mbuf_pool_%d:%d",
858 pktmbuf_pool[portid][socketid] =
859 rte_pktmbuf_pool_create(s, nb_mbuf,
860 MEMPOOL_CACHE_SIZE, 0,
861 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
862 if (pktmbuf_pool[portid][socketid] == NULL)
863 rte_exit(EXIT_FAILURE,
864 "Cannot init mbuf pool on socket %d\n",
867 printf("Allocated mbuf pool on socket %d\n",
870 /* Setup LPM, EM(f.e Hash) or FIB. But, only once per
873 if (!lkp_per_socket[socketid]) {
874 l3fwd_lkp.setup(socketid);
875 lkp_per_socket[socketid] = 1;
878 qconf = &lcore_conf[lcore_id];
879 qconf->ipv4_lookup_struct =
880 l3fwd_lkp.get_ipv4_lookup_struct(socketid);
881 qconf->ipv6_lookup_struct =
882 l3fwd_lkp.get_ipv6_lookup_struct(socketid);
887 /* Check the link status of all ports in up to 9s, and print them finally */
889 check_all_ports_link_status(uint32_t port_mask)
891 #define CHECK_INTERVAL 100 /* 100ms */
892 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
894 uint8_t count, all_ports_up, print_flag = 0;
895 struct rte_eth_link link;
897 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
899 printf("\nChecking link status");
901 for (count = 0; count <= MAX_CHECK_TIME; count++) {
905 RTE_ETH_FOREACH_DEV(portid) {
908 if ((port_mask & (1 << portid)) == 0)
910 memset(&link, 0, sizeof(link));
911 ret = rte_eth_link_get_nowait(portid, &link);
915 printf("Port %u link get failed: %s\n",
916 portid, rte_strerror(-ret));
919 /* print link status if flag set */
920 if (print_flag == 1) {
921 rte_eth_link_to_str(link_status_text,
922 sizeof(link_status_text), &link);
923 printf("Port %d %s\n", portid,
927 /* clear all_ports_up flag if any link down */
928 if (link.link_status == ETH_LINK_DOWN) {
933 /* after finally printing all link status, get out */
937 if (all_ports_up == 0) {
940 rte_delay_ms(CHECK_INTERVAL);
943 /* set the print_flag if all ports up or timeout */
944 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
952 signal_handler(int signum)
954 if (signum == SIGINT || signum == SIGTERM) {
955 printf("\n\nSignal %d received, preparing to exit...\n",
962 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
965 printf("Port %d: softly parse packet type info\n", portid);
966 if (rte_eth_add_rx_callback(portid, queueid,
967 l3fwd_lkp.cb_parse_ptype,
971 printf("Failed to add rx callback: port=%d\n", portid);
975 if (l3fwd_lkp.check_ptype(portid))
978 printf("port %d cannot parse packet type, please add --%s\n",
979 portid, CMD_LINE_OPT_PARSE_PTYPE);
984 l3fwd_poll_resource_setup(void)
986 uint8_t nb_rx_queue, queue, socketid;
987 struct rte_eth_dev_info dev_info;
988 uint32_t n_tx_queue, nb_lcores;
989 struct rte_eth_txconf *txconf;
990 struct lcore_conf *qconf;
991 uint16_t queueid, portid;
992 unsigned int nb_ports;
993 unsigned int lcore_id;
996 if (check_lcore_params() < 0)
997 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
999 ret = init_lcore_rx_queues();
1001 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1003 nb_ports = rte_eth_dev_count_avail();
1005 if (check_port_config() < 0)
1006 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1008 nb_lcores = rte_lcore_count();
1010 /* initialize all ports */
1011 RTE_ETH_FOREACH_DEV(portid) {
1012 struct rte_eth_conf local_port_conf = port_conf;
1014 /* skip ports that are not enabled */
1015 if ((enabled_port_mask & (1 << portid)) == 0) {
1016 printf("\nSkipping disabled port %d\n", portid);
1021 printf("Initializing port %d ... ", portid );
1024 nb_rx_queue = get_port_n_rx_queues(portid);
1025 n_tx_queue = nb_lcores;
1026 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1027 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1028 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1029 nb_rx_queue, (unsigned)n_tx_queue );
1031 ret = rte_eth_dev_info_get(portid, &dev_info);
1033 rte_exit(EXIT_FAILURE,
1034 "Error during getting device (port %u) info: %s\n",
1035 portid, strerror(-ret));
1037 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1038 local_port_conf.txmode.offloads |=
1039 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1041 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1042 dev_info.flow_type_rss_offloads;
1044 if (dev_info.max_rx_queues == 1)
1045 local_port_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
1047 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1048 port_conf.rx_adv_conf.rss_conf.rss_hf) {
1049 printf("Port %u modified RSS hash function based on hardware support,"
1050 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1052 port_conf.rx_adv_conf.rss_conf.rss_hf,
1053 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1056 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1057 (uint16_t)n_tx_queue, &local_port_conf);
1059 rte_exit(EXIT_FAILURE,
1060 "Cannot configure device: err=%d, port=%d\n",
1063 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1066 rte_exit(EXIT_FAILURE,
1067 "Cannot adjust number of descriptors: err=%d, "
1068 "port=%d\n", ret, portid);
1070 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1072 rte_exit(EXIT_FAILURE,
1073 "Cannot get MAC address: err=%d, port=%d\n",
1076 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1078 print_ethaddr("Destination:",
1079 (const struct rte_ether_addr *)&dest_eth_addr[portid]);
1083 * prepare src MACs for each port.
1085 rte_ether_addr_copy(&ports_eth_addr[portid],
1086 (struct rte_ether_addr *)(val_eth + portid) + 1);
1089 if (!per_port_pool) {
1090 /* portid = 0; this is *not* signifying the first port,
1091 * rather, it signifies that portid is ignored.
1093 ret = init_mem(0, NB_MBUF(nb_ports));
1095 ret = init_mem(portid, NB_MBUF(1));
1098 rte_exit(EXIT_FAILURE, "init_mem failed\n");
1100 /* init one TX queue per couple (lcore,port) */
1102 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1103 if (rte_lcore_is_enabled(lcore_id) == 0)
1108 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1112 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1115 txconf = &dev_info.default_txconf;
1116 txconf->offloads = local_port_conf.txmode.offloads;
1117 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1120 rte_exit(EXIT_FAILURE,
1121 "rte_eth_tx_queue_setup: err=%d, "
1122 "port=%d\n", ret, portid);
1124 qconf = &lcore_conf[lcore_id];
1125 qconf->tx_queue_id[portid] = queueid;
1128 qconf->tx_port_id[qconf->n_tx_port] = portid;
1134 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1135 if (rte_lcore_is_enabled(lcore_id) == 0)
1137 qconf = &lcore_conf[lcore_id];
1138 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1140 /* init RX queues */
1141 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1142 struct rte_eth_rxconf rxq_conf;
1144 portid = qconf->rx_queue_list[queue].port_id;
1145 queueid = qconf->rx_queue_list[queue].queue_id;
1149 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1153 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1156 ret = rte_eth_dev_info_get(portid, &dev_info);
1158 rte_exit(EXIT_FAILURE,
1159 "Error during getting device (port %u) info: %s\n",
1160 portid, strerror(-ret));
1162 rxq_conf = dev_info.default_rxconf;
1163 rxq_conf.offloads = port_conf.rxmode.offloads;
1165 ret = rte_eth_rx_queue_setup(portid, queueid,
1168 pktmbuf_pool[0][socketid]);
1170 ret = rte_eth_rx_queue_setup(portid, queueid,
1173 pktmbuf_pool[portid][socketid]);
1175 rte_exit(EXIT_FAILURE,
1176 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
1183 l3fwd_service_enable(uint32_t service_id)
1185 uint8_t min_service_count = UINT8_MAX;
1186 uint32_t slcore_array[RTE_MAX_LCORE];
1187 unsigned int slcore = 0;
1188 uint8_t service_count;
1189 int32_t slcore_count;
1191 if (!rte_service_lcore_count())
1194 slcore_count = rte_service_lcore_list(slcore_array, RTE_MAX_LCORE);
1195 if (slcore_count < 0)
1197 /* Get the core which has least number of services running. */
1198 while (slcore_count--) {
1199 /* Reset default mapping */
1200 if (rte_service_map_lcore_set(service_id,
1201 slcore_array[slcore_count], 0) != 0)
1203 service_count = rte_service_lcore_count_services(
1204 slcore_array[slcore_count]);
1205 if (service_count < min_service_count) {
1206 slcore = slcore_array[slcore_count];
1207 min_service_count = service_count;
1210 if (rte_service_map_lcore_set(service_id, slcore, 1))
1212 rte_service_lcore_start(slcore);
1218 l3fwd_event_service_setup(void)
1220 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
1221 struct rte_event_dev_info evdev_info;
1222 uint32_t service_id, caps;
1225 rte_event_dev_info_get(evt_rsrc->event_d_id, &evdev_info);
1226 if (!(evdev_info.event_dev_cap & RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED)) {
1227 ret = rte_event_dev_service_id_get(evt_rsrc->event_d_id,
1229 if (ret != -ESRCH && ret != 0)
1230 rte_exit(EXIT_FAILURE,
1231 "Error in starting eventdev service\n");
1232 l3fwd_service_enable(service_id);
1235 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++) {
1236 ret = rte_event_eth_rx_adapter_caps_get(evt_rsrc->event_d_id,
1237 evt_rsrc->rx_adptr.rx_adptr[i], &caps);
1239 rte_exit(EXIT_FAILURE,
1240 "Failed to get Rx adapter[%d] caps\n",
1241 evt_rsrc->rx_adptr.rx_adptr[i]);
1242 ret = rte_event_eth_rx_adapter_service_id_get(
1243 evt_rsrc->event_d_id,
1245 if (ret != -ESRCH && ret != 0)
1246 rte_exit(EXIT_FAILURE,
1247 "Error in starting Rx adapter[%d] service\n",
1248 evt_rsrc->rx_adptr.rx_adptr[i]);
1249 l3fwd_service_enable(service_id);
1252 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++) {
1253 ret = rte_event_eth_tx_adapter_caps_get(evt_rsrc->event_d_id,
1254 evt_rsrc->tx_adptr.tx_adptr[i], &caps);
1256 rte_exit(EXIT_FAILURE,
1257 "Failed to get Rx adapter[%d] caps\n",
1258 evt_rsrc->tx_adptr.tx_adptr[i]);
1259 ret = rte_event_eth_tx_adapter_service_id_get(
1260 evt_rsrc->event_d_id,
1262 if (ret != -ESRCH && ret != 0)
1263 rte_exit(EXIT_FAILURE,
1264 "Error in starting Rx adapter[%d] service\n",
1265 evt_rsrc->tx_adptr.tx_adptr[i]);
1266 l3fwd_service_enable(service_id);
1271 main(int argc, char **argv)
1273 struct l3fwd_event_resources *evt_rsrc;
1274 struct lcore_conf *qconf;
1275 uint16_t queueid, portid;
1276 unsigned int lcore_id;
1281 ret = rte_eal_init(argc, argv);
1283 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1288 signal(SIGINT, signal_handler);
1289 signal(SIGTERM, signal_handler);
1291 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
1292 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1293 dest_eth_addr[portid] =
1294 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
1295 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
1298 evt_rsrc = l3fwd_get_eventdev_rsrc();
1299 /* parse application arguments (after the EAL ones) */
1300 ret = parse_args(argc, argv);
1302 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1304 /* Setup function pointers for lookup method. */
1305 setup_l3fwd_lookup_tables();
1307 evt_rsrc->per_port_pool = per_port_pool;
1308 evt_rsrc->pkt_pool = pktmbuf_pool;
1309 evt_rsrc->port_mask = enabled_port_mask;
1310 /* Configure eventdev parameters if user has requested */
1311 if (evt_rsrc->enabled) {
1312 l3fwd_event_resource_setup(&port_conf);
1313 if (lookup_mode == L3FWD_LOOKUP_EM)
1314 l3fwd_lkp.main_loop = evt_rsrc->ops.em_event_loop;
1315 else if (lookup_mode == L3FWD_LOOKUP_FIB)
1316 l3fwd_lkp.main_loop = evt_rsrc->ops.fib_event_loop;
1318 l3fwd_lkp.main_loop = evt_rsrc->ops.lpm_event_loop;
1319 l3fwd_event_service_setup();
1321 l3fwd_poll_resource_setup();
1324 RTE_ETH_FOREACH_DEV(portid) {
1325 if ((enabled_port_mask & (1 << portid)) == 0) {
1329 ret = rte_eth_dev_start(portid);
1331 rte_exit(EXIT_FAILURE,
1332 "rte_eth_dev_start: err=%d, port=%d\n",
1336 * If enabled, put device in promiscuous mode.
1337 * This allows IO forwarding mode to forward packets
1338 * to itself through 2 cross-connected ports of the
1341 if (promiscuous_on) {
1342 ret = rte_eth_promiscuous_enable(portid);
1344 rte_exit(EXIT_FAILURE,
1345 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
1346 rte_strerror(-ret), portid);
1352 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1353 if (rte_lcore_is_enabled(lcore_id) == 0)
1355 qconf = &lcore_conf[lcore_id];
1356 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1357 portid = qconf->rx_queue_list[queue].port_id;
1358 queueid = qconf->rx_queue_list[queue].queue_id;
1359 if (prepare_ptype_parser(portid, queueid) == 0)
1360 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1364 check_all_ports_link_status(enabled_port_mask);
1367 /* launch per-lcore init on every lcore */
1368 rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MAIN);
1369 if (evt_rsrc->enabled) {
1370 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++)
1371 rte_event_eth_rx_adapter_stop(
1372 evt_rsrc->rx_adptr.rx_adptr[i]);
1373 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++)
1374 rte_event_eth_tx_adapter_stop(
1375 evt_rsrc->tx_adptr.tx_adptr[i]);
1377 RTE_ETH_FOREACH_DEV(portid) {
1378 if ((enabled_port_mask & (1 << portid)) == 0)
1380 ret = rte_eth_dev_stop(portid);
1382 printf("rte_eth_dev_stop: err=%d, port=%u\n",
1386 rte_eal_mp_wait_lcore();
1387 RTE_ETH_FOREACH_DEV(portid) {
1388 if ((enabled_port_mask & (1 << portid)) == 0)
1390 rte_eth_dev_close(portid);
1393 rte_event_dev_stop(evt_rsrc->event_d_id);
1394 rte_event_dev_close(evt_rsrc->event_d_id);
1397 rte_eal_mp_wait_lcore();
1399 RTE_ETH_FOREACH_DEV(portid) {
1400 if ((enabled_port_mask & (1 << portid)) == 0)
1402 printf("Closing port %d...", portid);
1403 ret = rte_eth_dev_stop(portid);
1405 printf("rte_eth_dev_stop: err=%d, port=%u\n",
1407 rte_eth_dev_close(portid);
1412 /* clean up the EAL */