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,
124 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
129 .rss_hf = ETH_RSS_IP,
133 .mq_mode = ETH_MQ_TX_NONE,
137 static uint32_t max_pkt_len;
139 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
140 static uint8_t lkp_per_socket[NB_SOCKETS];
142 struct l3fwd_lkp_mode {
144 int (*check_ptype)(int);
145 rte_rx_callback_fn cb_parse_ptype;
146 int (*main_loop)(void *);
147 void* (*get_ipv4_lookup_struct)(int);
148 void* (*get_ipv6_lookup_struct)(int);
151 static struct l3fwd_lkp_mode l3fwd_lkp;
153 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
155 .check_ptype = em_check_ptype,
156 .cb_parse_ptype = em_cb_parse_ptype,
157 .main_loop = em_main_loop,
158 .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
159 .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
162 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
164 .check_ptype = lpm_check_ptype,
165 .cb_parse_ptype = lpm_cb_parse_ptype,
166 .main_loop = lpm_main_loop,
167 .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
168 .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
171 static struct l3fwd_lkp_mode l3fwd_fib_lkp = {
173 .check_ptype = lpm_check_ptype,
174 .cb_parse_ptype = lpm_cb_parse_ptype,
175 .main_loop = fib_main_loop,
176 .get_ipv4_lookup_struct = fib_get_ipv4_l3fwd_lookup_struct,
177 .get_ipv6_lookup_struct = fib_get_ipv6_l3fwd_lookup_struct,
181 * 198.18.0.0/16 are set aside for RFC2544 benchmarking (RFC5735).
182 * 198.18.{0-7}.0/24 = Port {0-7}
184 const struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
185 {RTE_IPV4(198, 18, 0, 0), 24, 0},
186 {RTE_IPV4(198, 18, 1, 0), 24, 1},
187 {RTE_IPV4(198, 18, 2, 0), 24, 2},
188 {RTE_IPV4(198, 18, 3, 0), 24, 3},
189 {RTE_IPV4(198, 18, 4, 0), 24, 4},
190 {RTE_IPV4(198, 18, 5, 0), 24, 5},
191 {RTE_IPV4(198, 18, 6, 0), 24, 6},
192 {RTE_IPV4(198, 18, 7, 0), 24, 7},
196 * 2001:200::/48 is IANA reserved range for IPv6 benchmarking (RFC5180).
197 * 2001:200:0:{0-7}::/64 = Port {0-7}
199 const struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
200 {{32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 0},
201 {{32, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 1},
202 {{32, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 2},
203 {{32, 1, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 3},
204 {{32, 1, 2, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 4},
205 {{32, 1, 2, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 5},
206 {{32, 1, 2, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 6},
207 {{32, 1, 2, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 7},
211 * Setup lookup methods for forwarding.
212 * Currently exact-match, longest-prefix-match and forwarding information
213 * base are the supported ones.
216 setup_l3fwd_lookup_tables(void)
218 /* Setup HASH lookup functions. */
219 if (lookup_mode == L3FWD_LOOKUP_EM)
220 l3fwd_lkp = l3fwd_em_lkp;
221 /* Setup FIB lookup functions. */
222 else if (lookup_mode == L3FWD_LOOKUP_FIB)
223 l3fwd_lkp = l3fwd_fib_lkp;
224 /* Setup LPM lookup functions. */
226 l3fwd_lkp = l3fwd_lpm_lkp;
230 check_lcore_params(void)
232 uint8_t queue, lcore;
236 for (i = 0; i < nb_lcore_params; ++i) {
237 queue = lcore_params[i].queue_id;
238 if (queue >= MAX_RX_QUEUE_PER_PORT) {
239 printf("invalid queue number: %hhu\n", queue);
242 lcore = lcore_params[i].lcore_id;
243 if (!rte_lcore_is_enabled(lcore)) {
244 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
247 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
249 printf("warning: lcore %hhu is on socket %d with numa off \n",
257 check_port_config(void)
262 for (i = 0; i < nb_lcore_params; ++i) {
263 portid = lcore_params[i].port_id;
264 if ((enabled_port_mask & (1 << portid)) == 0) {
265 printf("port %u is not enabled in port mask\n", portid);
268 if (!rte_eth_dev_is_valid_port(portid)) {
269 printf("port %u is not present on the board\n", portid);
277 get_port_n_rx_queues(const uint16_t port)
282 for (i = 0; i < nb_lcore_params; ++i) {
283 if (lcore_params[i].port_id == port) {
284 if (lcore_params[i].queue_id == queue+1)
285 queue = lcore_params[i].queue_id;
287 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
288 " in sequence and must start with 0\n",
289 lcore_params[i].port_id);
292 return (uint8_t)(++queue);
296 init_lcore_rx_queues(void)
298 uint16_t i, nb_rx_queue;
301 for (i = 0; i < nb_lcore_params; ++i) {
302 lcore = lcore_params[i].lcore_id;
303 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
304 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
305 printf("error: too many queues (%u) for lcore: %u\n",
306 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
309 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
310 lcore_params[i].port_id;
311 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
312 lcore_params[i].queue_id;
313 lcore_conf[lcore].n_rx_queue++;
321 print_usage(const char *prgname)
323 fprintf(stderr, "%s [EAL options] --"
327 " --config (port,queue,lcore)[,(port,queue,lcore)]"
328 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
329 " [--max-pkt-len PKTLEN]"
331 " [--hash-entry-num]"
340 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
341 " -P : Enable promiscuous mode\n"
342 " --lookup: Select the lookup method\n"
344 " Accepted: em (Exact Match), lpm (Longest Prefix Match), fib (Forwarding Information Base)\n"
345 " --config (port,queue,lcore): Rx queue configuration\n"
346 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
347 " --max-pkt-len PKTLEN: maximum packet length in decimal (64-9600)\n"
348 " --no-numa: Disable numa awareness\n"
349 " --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
350 " --ipv6: Set if running ipv6 packets\n"
351 " --parse-ptype: Set to use software to analyze packet type\n"
352 " --per-port-pool: Use separate buffer pool per port\n"
353 " --mode: Packet transfer mode for I/O, poll or eventdev\n"
354 " Default mode = poll\n"
355 " --eventq-sched: Event queue synchronization method\n"
356 " ordered, atomic or parallel.\n"
358 " Valid only if --mode=eventdev\n"
359 " --event-eth-rxqs: Number of ethernet RX queues per device.\n"
361 " Valid only if --mode=eventdev\n"
362 " -E : Enable exact match, legacy flag please use --lookup=em instead\n"
363 " -L : Enable longest prefix match, legacy flag please use --lookup=lpm instead\n\n",
368 parse_max_pkt_len(const char *pktlen)
373 /* parse decimal string */
374 len = strtoul(pktlen, &end, 10);
375 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
385 parse_portmask(const char *portmask)
390 /* parse hexadecimal string */
391 pm = strtoul(portmask, &end, 16);
392 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
399 parse_hash_entry_number(const char *hash_entry_num)
402 unsigned long hash_en;
403 /* parse hexadecimal string */
404 hash_en = strtoul(hash_entry_num, &end, 16);
405 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
415 parse_config(const char *q_arg)
418 const char *p, *p0 = q_arg;
426 unsigned long int_fld[_NUM_FLD];
427 char *str_fld[_NUM_FLD];
433 while ((p = strchr(p0,'(')) != NULL) {
435 if((p0 = strchr(p,')')) == NULL)
439 if(size >= sizeof(s))
442 snprintf(s, sizeof(s), "%.*s", size, p);
443 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
445 for (i = 0; i < _NUM_FLD; i++){
447 int_fld[i] = strtoul(str_fld[i], &end, 0);
448 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
451 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
452 printf("exceeded max number of lcore params: %hu\n",
456 lcore_params_array[nb_lcore_params].port_id =
457 (uint8_t)int_fld[FLD_PORT];
458 lcore_params_array[nb_lcore_params].queue_id =
459 (uint8_t)int_fld[FLD_QUEUE];
460 lcore_params_array[nb_lcore_params].lcore_id =
461 (uint8_t)int_fld[FLD_LCORE];
464 lcore_params = lcore_params_array;
469 parse_eth_dest(const char *optarg)
473 uint8_t c, *dest, peer_addr[6];
476 portid = strtoul(optarg, &port_end, 10);
477 if (errno != 0 || port_end == optarg || *port_end++ != ',')
478 rte_exit(EXIT_FAILURE,
479 "Invalid eth-dest: %s", optarg);
480 if (portid >= RTE_MAX_ETHPORTS)
481 rte_exit(EXIT_FAILURE,
482 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
483 portid, RTE_MAX_ETHPORTS);
485 if (cmdline_parse_etheraddr(NULL, port_end,
486 &peer_addr, sizeof(peer_addr)) < 0)
487 rte_exit(EXIT_FAILURE,
488 "Invalid ethernet address: %s\n",
490 dest = (uint8_t *)&dest_eth_addr[portid];
491 for (c = 0; c < 6; c++)
492 dest[c] = peer_addr[c];
493 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
497 parse_mode(const char *optarg)
499 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
501 if (!strcmp(optarg, "poll"))
502 evt_rsrc->enabled = false;
503 else if (!strcmp(optarg, "eventdev"))
504 evt_rsrc->enabled = true;
508 parse_eventq_sched(const char *optarg)
510 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
512 if (!strcmp(optarg, "ordered"))
513 evt_rsrc->sched_type = RTE_SCHED_TYPE_ORDERED;
514 if (!strcmp(optarg, "atomic"))
515 evt_rsrc->sched_type = RTE_SCHED_TYPE_ATOMIC;
516 if (!strcmp(optarg, "parallel"))
517 evt_rsrc->sched_type = RTE_SCHED_TYPE_PARALLEL;
521 parse_event_eth_rx_queues(const char *eth_rx_queues)
523 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
525 uint8_t num_eth_rx_queues;
527 /* parse decimal string */
528 num_eth_rx_queues = strtoul(eth_rx_queues, &end, 10);
529 if ((eth_rx_queues[0] == '\0') || (end == NULL) || (*end != '\0'))
532 if (num_eth_rx_queues == 0)
535 evt_rsrc->eth_rx_queues = num_eth_rx_queues;
539 parse_lookup(const char *optarg)
541 if (!strcmp(optarg, "em"))
542 lookup_mode = L3FWD_LOOKUP_EM;
543 else if (!strcmp(optarg, "lpm"))
544 lookup_mode = L3FWD_LOOKUP_LPM;
545 else if (!strcmp(optarg, "fib"))
546 lookup_mode = L3FWD_LOOKUP_FIB;
548 fprintf(stderr, "Invalid lookup option! Accepted options: em, lpm, fib\n");
554 #define MAX_JUMBO_PKT_LEN 9600
556 static const char short_options[] =
558 "P" /* promiscuous */
559 "L" /* legacy enable long prefix match */
560 "E" /* legacy enable exact match */
563 #define CMD_LINE_OPT_CONFIG "config"
564 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
565 #define CMD_LINE_OPT_NO_NUMA "no-numa"
566 #define CMD_LINE_OPT_IPV6 "ipv6"
567 #define CMD_LINE_OPT_MAX_PKT_LEN "max-pkt-len"
568 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
569 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
570 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
571 #define CMD_LINE_OPT_MODE "mode"
572 #define CMD_LINE_OPT_EVENTQ_SYNC "eventq-sched"
573 #define CMD_LINE_OPT_EVENT_ETH_RX_QUEUES "event-eth-rxqs"
574 #define CMD_LINE_OPT_LOOKUP "lookup"
576 /* long options mapped to a short option */
578 /* first long only option value must be >= 256, so that we won't
579 * conflict with short options */
580 CMD_LINE_OPT_MIN_NUM = 256,
581 CMD_LINE_OPT_CONFIG_NUM,
582 CMD_LINE_OPT_ETH_DEST_NUM,
583 CMD_LINE_OPT_NO_NUMA_NUM,
584 CMD_LINE_OPT_IPV6_NUM,
585 CMD_LINE_OPT_MAX_PKT_LEN_NUM,
586 CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
587 CMD_LINE_OPT_PARSE_PTYPE_NUM,
588 CMD_LINE_OPT_PARSE_PER_PORT_POOL,
589 CMD_LINE_OPT_MODE_NUM,
590 CMD_LINE_OPT_EVENTQ_SYNC_NUM,
591 CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM,
592 CMD_LINE_OPT_LOOKUP_NUM,
595 static const struct option lgopts[] = {
596 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
597 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
598 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
599 {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
600 {CMD_LINE_OPT_MAX_PKT_LEN, 1, 0, CMD_LINE_OPT_MAX_PKT_LEN_NUM},
601 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
602 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
603 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
604 {CMD_LINE_OPT_MODE, 1, 0, CMD_LINE_OPT_MODE_NUM},
605 {CMD_LINE_OPT_EVENTQ_SYNC, 1, 0, CMD_LINE_OPT_EVENTQ_SYNC_NUM},
606 {CMD_LINE_OPT_EVENT_ETH_RX_QUEUES, 1, 0,
607 CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM},
608 {CMD_LINE_OPT_LOOKUP, 1, 0, CMD_LINE_OPT_LOOKUP_NUM},
613 * This expression is used to calculate the number of mbufs needed
614 * depending on user input, taking into account memory for rx and
615 * tx hardware rings, cache per lcore and mtable per port per lcore.
616 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
619 #define NB_MBUF(nports) RTE_MAX( \
620 (nports*nb_rx_queue*nb_rxd + \
621 nports*nb_lcores*MAX_PKT_BURST + \
622 nports*n_tx_queue*nb_txd + \
623 nb_lcores*MEMPOOL_CACHE_SIZE), \
626 /* Parse the argument given in the command line of the application */
628 parse_args(int argc, char **argv)
633 char *prgname = argv[0];
634 uint8_t lcore_params = 0;
635 uint8_t eventq_sched = 0;
636 uint8_t eth_rx_q = 0;
637 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
641 /* Error or normal output strings. */
642 while ((opt = getopt_long(argc, argvopt, short_options,
643 lgopts, &option_index)) != EOF) {
648 enabled_port_mask = parse_portmask(optarg);
649 if (enabled_port_mask == 0) {
650 fprintf(stderr, "Invalid portmask\n");
651 print_usage(prgname);
661 if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
662 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
665 lookup_mode = L3FWD_LOOKUP_EM;
669 if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
670 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
673 lookup_mode = L3FWD_LOOKUP_LPM;
677 case CMD_LINE_OPT_CONFIG_NUM:
678 ret = parse_config(optarg);
680 fprintf(stderr, "Invalid config\n");
681 print_usage(prgname);
687 case CMD_LINE_OPT_ETH_DEST_NUM:
688 parse_eth_dest(optarg);
691 case CMD_LINE_OPT_NO_NUMA_NUM:
695 case CMD_LINE_OPT_IPV6_NUM:
699 case CMD_LINE_OPT_MAX_PKT_LEN_NUM:
700 max_pkt_len = parse_max_pkt_len(optarg);
703 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
704 ret = parse_hash_entry_number(optarg);
705 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
706 hash_entry_number = ret;
708 fprintf(stderr, "invalid hash entry number\n");
709 print_usage(prgname);
714 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
715 printf("soft parse-ptype is enabled\n");
719 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
720 printf("per port buffer pool is enabled\n");
724 case CMD_LINE_OPT_MODE_NUM:
728 case CMD_LINE_OPT_EVENTQ_SYNC_NUM:
729 parse_eventq_sched(optarg);
733 case CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM:
734 parse_event_eth_rx_queues(optarg);
738 case CMD_LINE_OPT_LOOKUP_NUM:
739 if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
740 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
743 ret = parse_lookup(optarg);
745 * If parse_lookup was passed an invalid lookup type
746 * then return -1. Error log included within
747 * parse_lookup for simplicity.
754 print_usage(prgname);
759 if (evt_rsrc->enabled && lcore_params) {
760 fprintf(stderr, "lcore config is not valid when event mode is selected\n");
764 if (!evt_rsrc->enabled && eth_rx_q) {
765 fprintf(stderr, "eth_rx_queues is valid only when event mode is selected\n");
769 if (!evt_rsrc->enabled && eventq_sched) {
770 fprintf(stderr, "eventq_sched is valid only when event mode is selected\n");
775 * Nothing is selected, pick longest-prefix match
778 if (lookup_mode == L3FWD_LOOKUP_DEFAULT) {
779 fprintf(stderr, "Neither LPM, EM, or FIB selected, defaulting to LPM\n");
780 lookup_mode = L3FWD_LOOKUP_LPM;
784 * ipv6 and hash flags are valid only for
785 * exact match, reset them to default for
786 * longest-prefix match.
788 if (lookup_mode == L3FWD_LOOKUP_LPM) {
790 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
794 argv[optind-1] = prgname;
797 optind = 1; /* reset getopt lib */
802 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
804 char buf[RTE_ETHER_ADDR_FMT_SIZE];
805 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
806 printf("%s%s", name, buf);
810 init_mem(uint16_t portid, unsigned int nb_mbuf)
812 struct lcore_conf *qconf;
817 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
818 if (rte_lcore_is_enabled(lcore_id) == 0)
822 socketid = rte_lcore_to_socket_id(lcore_id);
826 if (socketid >= NB_SOCKETS) {
827 rte_exit(EXIT_FAILURE,
828 "Socket %d of lcore %u is out of range %d\n",
829 socketid, lcore_id, NB_SOCKETS);
832 if (pktmbuf_pool[portid][socketid] == NULL) {
833 snprintf(s, sizeof(s), "mbuf_pool_%d:%d",
835 pktmbuf_pool[portid][socketid] =
836 rte_pktmbuf_pool_create(s, nb_mbuf,
837 MEMPOOL_CACHE_SIZE, 0,
838 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
839 if (pktmbuf_pool[portid][socketid] == NULL)
840 rte_exit(EXIT_FAILURE,
841 "Cannot init mbuf pool on socket %d\n",
844 printf("Allocated mbuf pool on socket %d\n",
847 /* Setup LPM, EM(f.e Hash) or FIB. But, only once per
850 if (!lkp_per_socket[socketid]) {
851 l3fwd_lkp.setup(socketid);
852 lkp_per_socket[socketid] = 1;
855 qconf = &lcore_conf[lcore_id];
856 qconf->ipv4_lookup_struct =
857 l3fwd_lkp.get_ipv4_lookup_struct(socketid);
858 qconf->ipv6_lookup_struct =
859 l3fwd_lkp.get_ipv6_lookup_struct(socketid);
864 /* Check the link status of all ports in up to 9s, and print them finally */
866 check_all_ports_link_status(uint32_t port_mask)
868 #define CHECK_INTERVAL 100 /* 100ms */
869 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
871 uint8_t count, all_ports_up, print_flag = 0;
872 struct rte_eth_link link;
874 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
876 printf("\nChecking link status");
878 for (count = 0; count <= MAX_CHECK_TIME; count++) {
882 RTE_ETH_FOREACH_DEV(portid) {
885 if ((port_mask & (1 << portid)) == 0)
887 memset(&link, 0, sizeof(link));
888 ret = rte_eth_link_get_nowait(portid, &link);
892 printf("Port %u link get failed: %s\n",
893 portid, rte_strerror(-ret));
896 /* print link status if flag set */
897 if (print_flag == 1) {
898 rte_eth_link_to_str(link_status_text,
899 sizeof(link_status_text), &link);
900 printf("Port %d %s\n", portid,
904 /* clear all_ports_up flag if any link down */
905 if (link.link_status == ETH_LINK_DOWN) {
910 /* after finally printing all link status, get out */
914 if (all_ports_up == 0) {
917 rte_delay_ms(CHECK_INTERVAL);
920 /* set the print_flag if all ports up or timeout */
921 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
929 signal_handler(int signum)
931 if (signum == SIGINT || signum == SIGTERM) {
932 printf("\n\nSignal %d received, preparing to exit...\n",
939 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
942 printf("Port %d: softly parse packet type info\n", portid);
943 if (rte_eth_add_rx_callback(portid, queueid,
944 l3fwd_lkp.cb_parse_ptype,
948 printf("Failed to add rx callback: port=%d\n", portid);
952 if (l3fwd_lkp.check_ptype(portid))
955 printf("port %d cannot parse packet type, please add --%s\n",
956 portid, CMD_LINE_OPT_PARSE_PTYPE);
961 eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
963 uint32_t overhead_len;
965 if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
966 overhead_len = max_rx_pktlen - max_mtu;
968 overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
974 config_port_max_pkt_len(struct rte_eth_conf *conf,
975 struct rte_eth_dev_info *dev_info)
977 uint32_t overhead_len;
979 if (max_pkt_len == 0)
982 if (max_pkt_len < RTE_ETHER_MIN_LEN || max_pkt_len > MAX_JUMBO_PKT_LEN)
985 overhead_len = eth_dev_get_overhead_len(dev_info->max_rx_pktlen,
987 conf->rxmode.mtu = max_pkt_len - overhead_len;
989 if (conf->rxmode.mtu > RTE_ETHER_MTU)
990 conf->txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
996 l3fwd_poll_resource_setup(void)
998 uint8_t nb_rx_queue, queue, socketid;
999 struct rte_eth_dev_info dev_info;
1000 uint32_t n_tx_queue, nb_lcores;
1001 struct rte_eth_txconf *txconf;
1002 struct lcore_conf *qconf;
1003 uint16_t queueid, portid;
1004 unsigned int nb_ports;
1005 unsigned int lcore_id;
1008 if (check_lcore_params() < 0)
1009 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1011 ret = init_lcore_rx_queues();
1013 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1015 nb_ports = rte_eth_dev_count_avail();
1017 if (check_port_config() < 0)
1018 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1020 nb_lcores = rte_lcore_count();
1022 /* initialize all ports */
1023 RTE_ETH_FOREACH_DEV(portid) {
1024 struct rte_eth_conf local_port_conf = port_conf;
1026 /* skip ports that are not enabled */
1027 if ((enabled_port_mask & (1 << portid)) == 0) {
1028 printf("\nSkipping disabled port %d\n", portid);
1033 printf("Initializing port %d ... ", portid );
1036 nb_rx_queue = get_port_n_rx_queues(portid);
1037 n_tx_queue = nb_lcores;
1038 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1039 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1040 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1041 nb_rx_queue, (unsigned)n_tx_queue );
1043 ret = rte_eth_dev_info_get(portid, &dev_info);
1045 rte_exit(EXIT_FAILURE,
1046 "Error during getting device (port %u) info: %s\n",
1047 portid, strerror(-ret));
1049 ret = config_port_max_pkt_len(&local_port_conf, &dev_info);
1051 rte_exit(EXIT_FAILURE,
1052 "Invalid max packet length: %u (port %u)\n",
1053 max_pkt_len, portid);
1055 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1056 local_port_conf.txmode.offloads |=
1057 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1059 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1060 dev_info.flow_type_rss_offloads;
1062 if (dev_info.max_rx_queues == 1)
1063 local_port_conf.rxmode.mq_mode = ETH_MQ_RX_NONE;
1065 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1066 port_conf.rx_adv_conf.rss_conf.rss_hf) {
1067 printf("Port %u modified RSS hash function based on hardware support,"
1068 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1070 port_conf.rx_adv_conf.rss_conf.rss_hf,
1071 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1074 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1075 (uint16_t)n_tx_queue, &local_port_conf);
1077 rte_exit(EXIT_FAILURE,
1078 "Cannot configure device: err=%d, port=%d\n",
1081 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1084 rte_exit(EXIT_FAILURE,
1085 "Cannot adjust number of descriptors: err=%d, "
1086 "port=%d\n", ret, portid);
1088 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1090 rte_exit(EXIT_FAILURE,
1091 "Cannot get MAC address: err=%d, port=%d\n",
1094 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1096 print_ethaddr("Destination:",
1097 (const struct rte_ether_addr *)&dest_eth_addr[portid]);
1101 * prepare src MACs for each port.
1103 rte_ether_addr_copy(&ports_eth_addr[portid],
1104 (struct rte_ether_addr *)(val_eth + portid) + 1);
1107 if (!per_port_pool) {
1108 /* portid = 0; this is *not* signifying the first port,
1109 * rather, it signifies that portid is ignored.
1111 ret = init_mem(0, NB_MBUF(nb_ports));
1113 ret = init_mem(portid, NB_MBUF(1));
1116 rte_exit(EXIT_FAILURE, "init_mem failed\n");
1118 /* init one TX queue per couple (lcore,port) */
1120 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1121 if (rte_lcore_is_enabled(lcore_id) == 0)
1126 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1130 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1133 txconf = &dev_info.default_txconf;
1134 txconf->offloads = local_port_conf.txmode.offloads;
1135 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1138 rte_exit(EXIT_FAILURE,
1139 "rte_eth_tx_queue_setup: err=%d, "
1140 "port=%d\n", ret, portid);
1142 qconf = &lcore_conf[lcore_id];
1143 qconf->tx_queue_id[portid] = queueid;
1146 qconf->tx_port_id[qconf->n_tx_port] = portid;
1152 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1153 if (rte_lcore_is_enabled(lcore_id) == 0)
1155 qconf = &lcore_conf[lcore_id];
1156 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1158 /* init RX queues */
1159 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1160 struct rte_eth_rxconf rxq_conf;
1162 portid = qconf->rx_queue_list[queue].port_id;
1163 queueid = qconf->rx_queue_list[queue].queue_id;
1167 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1171 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1174 ret = rte_eth_dev_info_get(portid, &dev_info);
1176 rte_exit(EXIT_FAILURE,
1177 "Error during getting device (port %u) info: %s\n",
1178 portid, strerror(-ret));
1180 rxq_conf = dev_info.default_rxconf;
1181 rxq_conf.offloads = port_conf.rxmode.offloads;
1183 ret = rte_eth_rx_queue_setup(portid, queueid,
1186 pktmbuf_pool[0][socketid]);
1188 ret = rte_eth_rx_queue_setup(portid, queueid,
1191 pktmbuf_pool[portid][socketid]);
1193 rte_exit(EXIT_FAILURE,
1194 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
1201 l3fwd_service_enable(uint32_t service_id)
1203 uint8_t min_service_count = UINT8_MAX;
1204 uint32_t slcore_array[RTE_MAX_LCORE];
1205 unsigned int slcore = 0;
1206 uint8_t service_count;
1207 int32_t slcore_count;
1209 if (!rte_service_lcore_count())
1212 slcore_count = rte_service_lcore_list(slcore_array, RTE_MAX_LCORE);
1213 if (slcore_count < 0)
1215 /* Get the core which has least number of services running. */
1216 while (slcore_count--) {
1217 /* Reset default mapping */
1218 if (rte_service_map_lcore_set(service_id,
1219 slcore_array[slcore_count], 0) != 0)
1221 service_count = rte_service_lcore_count_services(
1222 slcore_array[slcore_count]);
1223 if (service_count < min_service_count) {
1224 slcore = slcore_array[slcore_count];
1225 min_service_count = service_count;
1228 if (rte_service_map_lcore_set(service_id, slcore, 1))
1230 rte_service_lcore_start(slcore);
1236 l3fwd_event_service_setup(void)
1238 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
1239 struct rte_event_dev_info evdev_info;
1240 uint32_t service_id, caps;
1243 rte_event_dev_info_get(evt_rsrc->event_d_id, &evdev_info);
1244 if (!(evdev_info.event_dev_cap & RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED)) {
1245 ret = rte_event_dev_service_id_get(evt_rsrc->event_d_id,
1247 if (ret != -ESRCH && ret != 0)
1248 rte_exit(EXIT_FAILURE,
1249 "Error in starting eventdev service\n");
1250 l3fwd_service_enable(service_id);
1253 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++) {
1254 ret = rte_event_eth_rx_adapter_caps_get(evt_rsrc->event_d_id,
1255 evt_rsrc->rx_adptr.rx_adptr[i], &caps);
1257 rte_exit(EXIT_FAILURE,
1258 "Failed to get Rx adapter[%d] caps\n",
1259 evt_rsrc->rx_adptr.rx_adptr[i]);
1260 ret = rte_event_eth_rx_adapter_service_id_get(
1261 evt_rsrc->event_d_id,
1263 if (ret != -ESRCH && ret != 0)
1264 rte_exit(EXIT_FAILURE,
1265 "Error in starting Rx adapter[%d] service\n",
1266 evt_rsrc->rx_adptr.rx_adptr[i]);
1267 l3fwd_service_enable(service_id);
1270 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++) {
1271 ret = rte_event_eth_tx_adapter_caps_get(evt_rsrc->event_d_id,
1272 evt_rsrc->tx_adptr.tx_adptr[i], &caps);
1274 rte_exit(EXIT_FAILURE,
1275 "Failed to get Rx adapter[%d] caps\n",
1276 evt_rsrc->tx_adptr.tx_adptr[i]);
1277 ret = rte_event_eth_tx_adapter_service_id_get(
1278 evt_rsrc->event_d_id,
1280 if (ret != -ESRCH && ret != 0)
1281 rte_exit(EXIT_FAILURE,
1282 "Error in starting Rx adapter[%d] service\n",
1283 evt_rsrc->tx_adptr.tx_adptr[i]);
1284 l3fwd_service_enable(service_id);
1289 main(int argc, char **argv)
1291 struct l3fwd_event_resources *evt_rsrc;
1292 struct lcore_conf *qconf;
1293 uint16_t queueid, portid;
1294 unsigned int lcore_id;
1299 ret = rte_eal_init(argc, argv);
1301 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1306 signal(SIGINT, signal_handler);
1307 signal(SIGTERM, signal_handler);
1309 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
1310 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1311 dest_eth_addr[portid] =
1312 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
1313 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
1316 evt_rsrc = l3fwd_get_eventdev_rsrc();
1317 /* parse application arguments (after the EAL ones) */
1318 ret = parse_args(argc, argv);
1320 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1322 /* Setup function pointers for lookup method. */
1323 setup_l3fwd_lookup_tables();
1325 evt_rsrc->per_port_pool = per_port_pool;
1326 evt_rsrc->pkt_pool = pktmbuf_pool;
1327 evt_rsrc->port_mask = enabled_port_mask;
1328 /* Configure eventdev parameters if user has requested */
1329 if (evt_rsrc->enabled) {
1330 l3fwd_event_resource_setup(&port_conf);
1331 if (lookup_mode == L3FWD_LOOKUP_EM)
1332 l3fwd_lkp.main_loop = evt_rsrc->ops.em_event_loop;
1333 else if (lookup_mode == L3FWD_LOOKUP_FIB)
1334 l3fwd_lkp.main_loop = evt_rsrc->ops.fib_event_loop;
1336 l3fwd_lkp.main_loop = evt_rsrc->ops.lpm_event_loop;
1337 l3fwd_event_service_setup();
1339 l3fwd_poll_resource_setup();
1342 RTE_ETH_FOREACH_DEV(portid) {
1343 if ((enabled_port_mask & (1 << portid)) == 0) {
1347 ret = rte_eth_dev_start(portid);
1349 rte_exit(EXIT_FAILURE,
1350 "rte_eth_dev_start: err=%d, port=%d\n",
1354 * If enabled, put device in promiscuous mode.
1355 * This allows IO forwarding mode to forward packets
1356 * to itself through 2 cross-connected ports of the
1359 if (promiscuous_on) {
1360 ret = rte_eth_promiscuous_enable(portid);
1362 rte_exit(EXIT_FAILURE,
1363 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
1364 rte_strerror(-ret), portid);
1370 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1371 if (rte_lcore_is_enabled(lcore_id) == 0)
1373 qconf = &lcore_conf[lcore_id];
1374 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1375 portid = qconf->rx_queue_list[queue].port_id;
1376 queueid = qconf->rx_queue_list[queue].queue_id;
1377 if (prepare_ptype_parser(portid, queueid) == 0)
1378 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1382 check_all_ports_link_status(enabled_port_mask);
1385 /* launch per-lcore init on every lcore */
1386 rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MAIN);
1387 if (evt_rsrc->enabled) {
1388 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++)
1389 rte_event_eth_rx_adapter_stop(
1390 evt_rsrc->rx_adptr.rx_adptr[i]);
1391 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++)
1392 rte_event_eth_tx_adapter_stop(
1393 evt_rsrc->tx_adptr.tx_adptr[i]);
1395 RTE_ETH_FOREACH_DEV(portid) {
1396 if ((enabled_port_mask & (1 << portid)) == 0)
1398 ret = rte_eth_dev_stop(portid);
1400 printf("rte_eth_dev_stop: err=%d, port=%u\n",
1404 rte_eal_mp_wait_lcore();
1405 RTE_ETH_FOREACH_DEV(portid) {
1406 if ((enabled_port_mask & (1 << portid)) == 0)
1408 rte_eth_dev_close(portid);
1411 rte_event_dev_stop(evt_rsrc->event_d_id);
1412 rte_event_dev_close(evt_rsrc->event_d_id);
1415 rte_eal_mp_wait_lcore();
1417 RTE_ETH_FOREACH_DEV(portid) {
1418 if ((enabled_port_mask & (1 << portid)) == 0)
1420 printf("Closing port %d...", portid);
1421 ret = rte_eth_dev_stop(portid);
1423 printf("rte_eth_dev_stop: err=%d, port=%u\n",
1425 rte_eth_dev_close(portid);
1430 /* clean up the EAL */