1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 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_atomic.h>
28 #include <rte_cycles.h>
29 #include <rte_prefetch.h>
30 #include <rte_lcore.h>
31 #include <rte_per_lcore.h>
32 #include <rte_branch_prediction.h>
33 #include <rte_interrupts.h>
34 #include <rte_random.h>
35 #include <rte_debug.h>
36 #include <rte_ether.h>
37 #include <rte_mempool.h>
42 #include <rte_string_fns.h>
43 #include <rte_cpuflags.h>
45 #include <cmdline_parse.h>
46 #include <cmdline_parse_etheraddr.h>
49 #include "l3fwd_event.h"
51 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
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 or Exact match. */
64 static int l3fwd_lpm_on;
65 static int l3fwd_em_on;
67 /* Global variables. */
69 static int numa_on = 1; /**< NUMA is enabled by default. */
70 static int parse_ptype; /**< Parse packet type using rx callback, and */
71 /**< disabled by default */
72 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
75 volatile bool force_quit;
77 /* ethernet addresses of ports */
78 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
79 struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
81 xmm_t val_eth[RTE_MAX_ETHPORTS];
83 /* mask of enabled ports */
84 uint32_t enabled_port_mask;
86 /* Used only in exact match mode. */
87 int ipv6; /**< ipv6 is false by default. */
88 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
90 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
96 } __rte_cache_aligned;
98 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
99 static struct lcore_params lcore_params_array_default[] = {
111 static struct lcore_params * lcore_params = lcore_params_array_default;
112 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
113 sizeof(lcore_params_array_default[0]);
115 static struct rte_eth_conf port_conf = {
117 .mq_mode = ETH_MQ_RX_RSS,
118 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
120 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
125 .rss_hf = ETH_RSS_IP,
129 .mq_mode = ETH_MQ_TX_NONE,
133 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
134 static uint8_t lkp_per_socket[NB_SOCKETS];
136 struct l3fwd_lkp_mode {
138 int (*check_ptype)(int);
139 rte_rx_callback_fn cb_parse_ptype;
140 int (*main_loop)(void *);
141 void* (*get_ipv4_lookup_struct)(int);
142 void* (*get_ipv6_lookup_struct)(int);
145 static struct l3fwd_lkp_mode l3fwd_lkp;
147 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
149 .check_ptype = em_check_ptype,
150 .cb_parse_ptype = em_cb_parse_ptype,
151 .main_loop = em_main_loop,
152 .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
153 .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
156 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
158 .check_ptype = lpm_check_ptype,
159 .cb_parse_ptype = lpm_cb_parse_ptype,
160 .main_loop = lpm_main_loop,
161 .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
162 .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
166 * Setup lookup methods for forwarding.
167 * Currently exact-match and longest-prefix-match
168 * are supported ones.
171 setup_l3fwd_lookup_tables(void)
173 /* Setup HASH lookup functions. */
175 l3fwd_lkp = l3fwd_em_lkp;
176 /* Setup LPM lookup functions. */
178 l3fwd_lkp = l3fwd_lpm_lkp;
182 check_lcore_params(void)
184 uint8_t queue, lcore;
188 for (i = 0; i < nb_lcore_params; ++i) {
189 queue = lcore_params[i].queue_id;
190 if (queue >= MAX_RX_QUEUE_PER_PORT) {
191 printf("invalid queue number: %hhu\n", queue);
194 lcore = lcore_params[i].lcore_id;
195 if (!rte_lcore_is_enabled(lcore)) {
196 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
199 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
201 printf("warning: lcore %hhu is on socket %d with numa off \n",
209 check_port_config(void)
214 for (i = 0; i < nb_lcore_params; ++i) {
215 portid = lcore_params[i].port_id;
216 if ((enabled_port_mask & (1 << portid)) == 0) {
217 printf("port %u is not enabled in port mask\n", portid);
220 if (!rte_eth_dev_is_valid_port(portid)) {
221 printf("port %u is not present on the board\n", portid);
229 get_port_n_rx_queues(const uint16_t port)
234 for (i = 0; i < nb_lcore_params; ++i) {
235 if (lcore_params[i].port_id == port) {
236 if (lcore_params[i].queue_id == queue+1)
237 queue = lcore_params[i].queue_id;
239 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
240 " in sequence and must start with 0\n",
241 lcore_params[i].port_id);
244 return (uint8_t)(++queue);
248 init_lcore_rx_queues(void)
250 uint16_t i, nb_rx_queue;
253 for (i = 0; i < nb_lcore_params; ++i) {
254 lcore = lcore_params[i].lcore_id;
255 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
256 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
257 printf("error: too many queues (%u) for lcore: %u\n",
258 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
261 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
262 lcore_params[i].port_id;
263 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
264 lcore_params[i].queue_id;
265 lcore_conf[lcore].n_rx_queue++;
273 print_usage(const char *prgname)
275 fprintf(stderr, "%s [EAL options] --"
280 " --config (port,queue,lcore)[,(port,queue,lcore)]"
281 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
282 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
284 " [--hash-entry-num]"
289 " [--eventq-sched]\n\n"
291 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
292 " -P : Enable promiscuous mode\n"
293 " -E : Enable exact match\n"
294 " -L : Enable longest prefix match (default)\n"
295 " --config (port,queue,lcore): Rx queue configuration\n"
296 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
297 " --enable-jumbo: Enable jumbo frames\n"
298 " --max-pkt-len: Under the premise of enabling jumbo,\n"
299 " maximum packet length in decimal (64-9600)\n"
300 " --no-numa: Disable numa awareness\n"
301 " --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
302 " --ipv6: Set if running ipv6 packets\n"
303 " --parse-ptype: Set to use software to analyze packet type\n"
304 " --per-port-pool: Use separate buffer pool per port\n"
305 " --mode: Packet transfer mode for I/O, poll or eventdev\n"
306 " Default mode = poll\n"
307 " --eventq-sched: Event queue synchronization method\n"
308 " ordered, atomic or parallel.\n"
310 " Valid only if --mode=eventdev\n"
311 " --event-eth-rxqs: Number of ethernet RX queues per device.\n"
313 " Valid only if --mode=eventdev\n\n",
318 parse_max_pkt_len(const char *pktlen)
323 /* parse decimal string */
324 len = strtoul(pktlen, &end, 10);
325 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
335 parse_portmask(const char *portmask)
340 /* parse hexadecimal string */
341 pm = strtoul(portmask, &end, 16);
342 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
349 parse_hash_entry_number(const char *hash_entry_num)
352 unsigned long hash_en;
353 /* parse hexadecimal string */
354 hash_en = strtoul(hash_entry_num, &end, 16);
355 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
365 parse_config(const char *q_arg)
368 const char *p, *p0 = q_arg;
376 unsigned long int_fld[_NUM_FLD];
377 char *str_fld[_NUM_FLD];
383 while ((p = strchr(p0,'(')) != NULL) {
385 if((p0 = strchr(p,')')) == NULL)
389 if(size >= sizeof(s))
392 snprintf(s, sizeof(s), "%.*s", size, p);
393 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
395 for (i = 0; i < _NUM_FLD; i++){
397 int_fld[i] = strtoul(str_fld[i], &end, 0);
398 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
401 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
402 printf("exceeded max number of lcore params: %hu\n",
406 lcore_params_array[nb_lcore_params].port_id =
407 (uint8_t)int_fld[FLD_PORT];
408 lcore_params_array[nb_lcore_params].queue_id =
409 (uint8_t)int_fld[FLD_QUEUE];
410 lcore_params_array[nb_lcore_params].lcore_id =
411 (uint8_t)int_fld[FLD_LCORE];
414 lcore_params = lcore_params_array;
419 parse_eth_dest(const char *optarg)
423 uint8_t c, *dest, peer_addr[6];
426 portid = strtoul(optarg, &port_end, 10);
427 if (errno != 0 || port_end == optarg || *port_end++ != ',')
428 rte_exit(EXIT_FAILURE,
429 "Invalid eth-dest: %s", optarg);
430 if (portid >= RTE_MAX_ETHPORTS)
431 rte_exit(EXIT_FAILURE,
432 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
433 portid, RTE_MAX_ETHPORTS);
435 if (cmdline_parse_etheraddr(NULL, port_end,
436 &peer_addr, sizeof(peer_addr)) < 0)
437 rte_exit(EXIT_FAILURE,
438 "Invalid ethernet address: %s\n",
440 dest = (uint8_t *)&dest_eth_addr[portid];
441 for (c = 0; c < 6; c++)
442 dest[c] = peer_addr[c];
443 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
447 parse_mode(const char *optarg)
449 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
451 if (!strcmp(optarg, "poll"))
452 evt_rsrc->enabled = false;
453 else if (!strcmp(optarg, "eventdev"))
454 evt_rsrc->enabled = true;
458 parse_eventq_sched(const char *optarg)
460 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
462 if (!strcmp(optarg, "ordered"))
463 evt_rsrc->sched_type = RTE_SCHED_TYPE_ORDERED;
464 if (!strcmp(optarg, "atomic"))
465 evt_rsrc->sched_type = RTE_SCHED_TYPE_ATOMIC;
466 if (!strcmp(optarg, "parallel"))
467 evt_rsrc->sched_type = RTE_SCHED_TYPE_PARALLEL;
471 parse_event_eth_rx_queues(const char *eth_rx_queues)
473 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
475 uint8_t num_eth_rx_queues;
477 /* parse decimal string */
478 num_eth_rx_queues = strtoul(eth_rx_queues, &end, 10);
479 if ((eth_rx_queues[0] == '\0') || (end == NULL) || (*end != '\0'))
482 if (num_eth_rx_queues == 0)
485 evt_rsrc->eth_rx_queues = num_eth_rx_queues;
488 #define MAX_JUMBO_PKT_LEN 9600
490 static const char short_options[] =
492 "P" /* promiscuous */
493 "L" /* enable long prefix match */
494 "E" /* enable exact match */
497 #define CMD_LINE_OPT_CONFIG "config"
498 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
499 #define CMD_LINE_OPT_NO_NUMA "no-numa"
500 #define CMD_LINE_OPT_IPV6 "ipv6"
501 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
502 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
503 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
504 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
505 #define CMD_LINE_OPT_MODE "mode"
506 #define CMD_LINE_OPT_EVENTQ_SYNC "eventq-sched"
507 #define CMD_LINE_OPT_EVENT_ETH_RX_QUEUES "event-eth-rxqs"
509 /* long options mapped to a short option */
511 /* first long only option value must be >= 256, so that we won't
512 * conflict with short options */
513 CMD_LINE_OPT_MIN_NUM = 256,
514 CMD_LINE_OPT_CONFIG_NUM,
515 CMD_LINE_OPT_ETH_DEST_NUM,
516 CMD_LINE_OPT_NO_NUMA_NUM,
517 CMD_LINE_OPT_IPV6_NUM,
518 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
519 CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
520 CMD_LINE_OPT_PARSE_PTYPE_NUM,
521 CMD_LINE_OPT_PARSE_PER_PORT_POOL,
522 CMD_LINE_OPT_MODE_NUM,
523 CMD_LINE_OPT_EVENTQ_SYNC_NUM,
524 CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM,
527 static const struct option lgopts[] = {
528 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
529 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
530 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
531 {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
532 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
533 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
534 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
535 {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
536 {CMD_LINE_OPT_MODE, 1, 0, CMD_LINE_OPT_MODE_NUM},
537 {CMD_LINE_OPT_EVENTQ_SYNC, 1, 0, CMD_LINE_OPT_EVENTQ_SYNC_NUM},
538 {CMD_LINE_OPT_EVENT_ETH_RX_QUEUES, 1, 0,
539 CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM},
544 * This expression is used to calculate the number of mbufs needed
545 * depending on user input, taking into account memory for rx and
546 * tx hardware rings, cache per lcore and mtable per port per lcore.
547 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
550 #define NB_MBUF(nports) RTE_MAX( \
551 (nports*nb_rx_queue*nb_rxd + \
552 nports*nb_lcores*MAX_PKT_BURST + \
553 nports*n_tx_queue*nb_txd + \
554 nb_lcores*MEMPOOL_CACHE_SIZE), \
557 /* Parse the argument given in the command line of the application */
559 parse_args(int argc, char **argv)
564 char *prgname = argv[0];
565 uint8_t lcore_params = 0;
566 uint8_t eventq_sched = 0;
567 uint8_t eth_rx_q = 0;
568 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
572 /* Error or normal output strings. */
573 while ((opt = getopt_long(argc, argvopt, short_options,
574 lgopts, &option_index)) != EOF) {
579 enabled_port_mask = parse_portmask(optarg);
580 if (enabled_port_mask == 0) {
581 fprintf(stderr, "Invalid portmask\n");
582 print_usage(prgname);
600 case CMD_LINE_OPT_CONFIG_NUM:
601 ret = parse_config(optarg);
603 fprintf(stderr, "Invalid config\n");
604 print_usage(prgname);
610 case CMD_LINE_OPT_ETH_DEST_NUM:
611 parse_eth_dest(optarg);
614 case CMD_LINE_OPT_NO_NUMA_NUM:
618 case CMD_LINE_OPT_IPV6_NUM:
622 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
623 const struct option lenopts = {
624 "max-pkt-len", required_argument, 0, 0
627 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
628 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
631 * if no max-pkt-len set, use the default
632 * value RTE_ETHER_MAX_LEN.
634 if (getopt_long(argc, argvopt, "",
635 &lenopts, &option_index) == 0) {
636 ret = parse_max_pkt_len(optarg);
637 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
639 "invalid maximum packet length\n");
640 print_usage(prgname);
643 port_conf.rxmode.max_rx_pkt_len = ret;
648 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
649 ret = parse_hash_entry_number(optarg);
650 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
651 hash_entry_number = ret;
653 fprintf(stderr, "invalid hash entry number\n");
654 print_usage(prgname);
659 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
660 printf("soft parse-ptype is enabled\n");
664 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
665 printf("per port buffer pool is enabled\n");
669 case CMD_LINE_OPT_MODE_NUM:
673 case CMD_LINE_OPT_EVENTQ_SYNC_NUM:
674 parse_eventq_sched(optarg);
678 case CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM:
679 parse_event_eth_rx_queues(optarg);
684 print_usage(prgname);
689 /* If both LPM and EM are selected, return error. */
690 if (l3fwd_lpm_on && l3fwd_em_on) {
691 fprintf(stderr, "LPM and EM are mutually exclusive, select only one\n");
695 if (evt_rsrc->enabled && lcore_params) {
696 fprintf(stderr, "lcore config is not valid when event mode is selected\n");
700 if (!evt_rsrc->enabled && eth_rx_q) {
701 fprintf(stderr, "eth_rx_queues is valid only when event mode is selected\n");
705 if (!evt_rsrc->enabled && eventq_sched) {
706 fprintf(stderr, "eventq_sched is valid only when event mode is selected\n");
711 * Nothing is selected, pick longest-prefix match
714 if (!l3fwd_lpm_on && !l3fwd_em_on) {
715 fprintf(stderr, "LPM or EM none selected, default LPM on\n");
720 * ipv6 and hash flags are valid only for
721 * exact macth, reset them to default for
722 * longest-prefix match.
726 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
730 argv[optind-1] = prgname;
733 optind = 1; /* reset getopt lib */
738 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
740 char buf[RTE_ETHER_ADDR_FMT_SIZE];
741 rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
742 printf("%s%s", name, buf);
746 init_mem(uint16_t portid, unsigned int nb_mbuf)
748 struct lcore_conf *qconf;
753 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
754 if (rte_lcore_is_enabled(lcore_id) == 0)
758 socketid = rte_lcore_to_socket_id(lcore_id);
762 if (socketid >= NB_SOCKETS) {
763 rte_exit(EXIT_FAILURE,
764 "Socket %d of lcore %u is out of range %d\n",
765 socketid, lcore_id, NB_SOCKETS);
768 if (pktmbuf_pool[portid][socketid] == NULL) {
769 snprintf(s, sizeof(s), "mbuf_pool_%d:%d",
771 pktmbuf_pool[portid][socketid] =
772 rte_pktmbuf_pool_create(s, nb_mbuf,
773 MEMPOOL_CACHE_SIZE, 0,
774 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
775 if (pktmbuf_pool[portid][socketid] == NULL)
776 rte_exit(EXIT_FAILURE,
777 "Cannot init mbuf pool on socket %d\n",
780 printf("Allocated mbuf pool on socket %d\n",
783 /* Setup either LPM or EM(f.e Hash). But, only once per
786 if (!lkp_per_socket[socketid]) {
787 l3fwd_lkp.setup(socketid);
788 lkp_per_socket[socketid] = 1;
791 qconf = &lcore_conf[lcore_id];
792 qconf->ipv4_lookup_struct =
793 l3fwd_lkp.get_ipv4_lookup_struct(socketid);
794 qconf->ipv6_lookup_struct =
795 l3fwd_lkp.get_ipv6_lookup_struct(socketid);
800 /* Check the link status of all ports in up to 9s, and print them finally */
802 check_all_ports_link_status(uint32_t port_mask)
804 #define CHECK_INTERVAL 100 /* 100ms */
805 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
807 uint8_t count, all_ports_up, print_flag = 0;
808 struct rte_eth_link link;
810 char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
812 printf("\nChecking link status");
814 for (count = 0; count <= MAX_CHECK_TIME; count++) {
818 RTE_ETH_FOREACH_DEV(portid) {
821 if ((port_mask & (1 << portid)) == 0)
823 memset(&link, 0, sizeof(link));
824 ret = rte_eth_link_get_nowait(portid, &link);
828 printf("Port %u link get failed: %s\n",
829 portid, rte_strerror(-ret));
832 /* print link status if flag set */
833 if (print_flag == 1) {
834 rte_eth_link_to_str(link_status_text,
835 sizeof(link_status_text), &link);
836 printf("Port %d %s\n", portid,
840 /* clear all_ports_up flag if any link down */
841 if (link.link_status == ETH_LINK_DOWN) {
846 /* after finally printing all link status, get out */
850 if (all_ports_up == 0) {
853 rte_delay_ms(CHECK_INTERVAL);
856 /* set the print_flag if all ports up or timeout */
857 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
865 signal_handler(int signum)
867 if (signum == SIGINT || signum == SIGTERM) {
868 printf("\n\nSignal %d received, preparing to exit...\n",
875 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
878 printf("Port %d: softly parse packet type info\n", portid);
879 if (rte_eth_add_rx_callback(portid, queueid,
880 l3fwd_lkp.cb_parse_ptype,
884 printf("Failed to add rx callback: port=%d\n", portid);
888 if (l3fwd_lkp.check_ptype(portid))
891 printf("port %d cannot parse packet type, please add --%s\n",
892 portid, CMD_LINE_OPT_PARSE_PTYPE);
897 l3fwd_poll_resource_setup(void)
899 uint8_t nb_rx_queue, queue, socketid;
900 struct rte_eth_dev_info dev_info;
901 uint32_t n_tx_queue, nb_lcores;
902 struct rte_eth_txconf *txconf;
903 struct lcore_conf *qconf;
904 uint16_t queueid, portid;
905 unsigned int nb_ports;
906 unsigned int lcore_id;
909 if (check_lcore_params() < 0)
910 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
912 ret = init_lcore_rx_queues();
914 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
916 nb_ports = rte_eth_dev_count_avail();
918 if (check_port_config() < 0)
919 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
921 nb_lcores = rte_lcore_count();
923 /* initialize all ports */
924 RTE_ETH_FOREACH_DEV(portid) {
925 struct rte_eth_conf local_port_conf = port_conf;
927 /* skip ports that are not enabled */
928 if ((enabled_port_mask & (1 << portid)) == 0) {
929 printf("\nSkipping disabled port %d\n", portid);
934 printf("Initializing port %d ... ", portid );
937 nb_rx_queue = get_port_n_rx_queues(portid);
938 n_tx_queue = nb_lcores;
939 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
940 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
941 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
942 nb_rx_queue, (unsigned)n_tx_queue );
944 ret = rte_eth_dev_info_get(portid, &dev_info);
946 rte_exit(EXIT_FAILURE,
947 "Error during getting device (port %u) info: %s\n",
948 portid, strerror(-ret));
950 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
951 local_port_conf.txmode.offloads |=
952 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
954 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
955 dev_info.flow_type_rss_offloads;
956 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
957 port_conf.rx_adv_conf.rss_conf.rss_hf) {
958 printf("Port %u modified RSS hash function based on hardware support,"
959 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
961 port_conf.rx_adv_conf.rss_conf.rss_hf,
962 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
965 ret = rte_eth_dev_configure(portid, nb_rx_queue,
966 (uint16_t)n_tx_queue, &local_port_conf);
968 rte_exit(EXIT_FAILURE,
969 "Cannot configure device: err=%d, port=%d\n",
972 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
975 rte_exit(EXIT_FAILURE,
976 "Cannot adjust number of descriptors: err=%d, "
977 "port=%d\n", ret, portid);
979 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
981 rte_exit(EXIT_FAILURE,
982 "Cannot get MAC address: err=%d, port=%d\n",
985 print_ethaddr(" Address:", &ports_eth_addr[portid]);
987 print_ethaddr("Destination:",
988 (const struct rte_ether_addr *)&dest_eth_addr[portid]);
992 * prepare src MACs for each port.
994 rte_ether_addr_copy(&ports_eth_addr[portid],
995 (struct rte_ether_addr *)(val_eth + portid) + 1);
998 if (!per_port_pool) {
999 /* portid = 0; this is *not* signifying the first port,
1000 * rather, it signifies that portid is ignored.
1002 ret = init_mem(0, NB_MBUF(nb_ports));
1004 ret = init_mem(portid, NB_MBUF(1));
1007 rte_exit(EXIT_FAILURE, "init_mem failed\n");
1009 /* init one TX queue per couple (lcore,port) */
1011 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1012 if (rte_lcore_is_enabled(lcore_id) == 0)
1017 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1021 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1024 txconf = &dev_info.default_txconf;
1025 txconf->offloads = local_port_conf.txmode.offloads;
1026 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1029 rte_exit(EXIT_FAILURE,
1030 "rte_eth_tx_queue_setup: err=%d, "
1031 "port=%d\n", ret, portid);
1033 qconf = &lcore_conf[lcore_id];
1034 qconf->tx_queue_id[portid] = queueid;
1037 qconf->tx_port_id[qconf->n_tx_port] = portid;
1043 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1044 if (rte_lcore_is_enabled(lcore_id) == 0)
1046 qconf = &lcore_conf[lcore_id];
1047 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1049 /* init RX queues */
1050 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1051 struct rte_eth_rxconf rxq_conf;
1053 portid = qconf->rx_queue_list[queue].port_id;
1054 queueid = qconf->rx_queue_list[queue].queue_id;
1058 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1062 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1065 ret = rte_eth_dev_info_get(portid, &dev_info);
1067 rte_exit(EXIT_FAILURE,
1068 "Error during getting device (port %u) info: %s\n",
1069 portid, strerror(-ret));
1071 rxq_conf = dev_info.default_rxconf;
1072 rxq_conf.offloads = port_conf.rxmode.offloads;
1074 ret = rte_eth_rx_queue_setup(portid, queueid,
1077 pktmbuf_pool[0][socketid]);
1079 ret = rte_eth_rx_queue_setup(portid, queueid,
1082 pktmbuf_pool[portid][socketid]);
1084 rte_exit(EXIT_FAILURE,
1085 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
1092 l3fwd_service_enable(uint32_t service_id)
1094 uint8_t min_service_count = UINT8_MAX;
1095 uint32_t slcore_array[RTE_MAX_LCORE];
1096 unsigned int slcore = 0;
1097 uint8_t service_count;
1098 int32_t slcore_count;
1100 if (!rte_service_lcore_count())
1103 slcore_count = rte_service_lcore_list(slcore_array, RTE_MAX_LCORE);
1104 if (slcore_count < 0)
1106 /* Get the core which has least number of services running. */
1107 while (slcore_count--) {
1108 /* Reset default mapping */
1109 if (rte_service_map_lcore_set(service_id,
1110 slcore_array[slcore_count], 0) != 0)
1112 service_count = rte_service_lcore_count_services(
1113 slcore_array[slcore_count]);
1114 if (service_count < min_service_count) {
1115 slcore = slcore_array[slcore_count];
1116 min_service_count = service_count;
1119 if (rte_service_map_lcore_set(service_id, slcore, 1))
1121 rte_service_lcore_start(slcore);
1127 l3fwd_event_service_setup(void)
1129 struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
1130 struct rte_event_dev_info evdev_info;
1131 uint32_t service_id, caps;
1134 rte_event_dev_info_get(evt_rsrc->event_d_id, &evdev_info);
1135 if (!(evdev_info.event_dev_cap & RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED)) {
1136 ret = rte_event_dev_service_id_get(evt_rsrc->event_d_id,
1138 if (ret != -ESRCH && ret != 0)
1139 rte_exit(EXIT_FAILURE,
1140 "Error in starting eventdev service\n");
1141 l3fwd_service_enable(service_id);
1144 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++) {
1145 ret = rte_event_eth_rx_adapter_caps_get(evt_rsrc->event_d_id,
1146 evt_rsrc->rx_adptr.rx_adptr[i], &caps);
1148 rte_exit(EXIT_FAILURE,
1149 "Failed to get Rx adapter[%d] caps\n",
1150 evt_rsrc->rx_adptr.rx_adptr[i]);
1151 ret = rte_event_eth_rx_adapter_service_id_get(
1152 evt_rsrc->event_d_id,
1154 if (ret != -ESRCH && ret != 0)
1155 rte_exit(EXIT_FAILURE,
1156 "Error in starting Rx adapter[%d] service\n",
1157 evt_rsrc->rx_adptr.rx_adptr[i]);
1158 l3fwd_service_enable(service_id);
1161 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++) {
1162 ret = rte_event_eth_tx_adapter_caps_get(evt_rsrc->event_d_id,
1163 evt_rsrc->tx_adptr.tx_adptr[i], &caps);
1165 rte_exit(EXIT_FAILURE,
1166 "Failed to get Rx adapter[%d] caps\n",
1167 evt_rsrc->tx_adptr.tx_adptr[i]);
1168 ret = rte_event_eth_tx_adapter_service_id_get(
1169 evt_rsrc->event_d_id,
1171 if (ret != -ESRCH && ret != 0)
1172 rte_exit(EXIT_FAILURE,
1173 "Error in starting Rx adapter[%d] service\n",
1174 evt_rsrc->tx_adptr.tx_adptr[i]);
1175 l3fwd_service_enable(service_id);
1180 main(int argc, char **argv)
1182 struct l3fwd_event_resources *evt_rsrc;
1183 struct lcore_conf *qconf;
1184 uint16_t queueid, portid;
1185 unsigned int lcore_id;
1190 ret = rte_eal_init(argc, argv);
1192 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1197 signal(SIGINT, signal_handler);
1198 signal(SIGTERM, signal_handler);
1200 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
1201 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1202 dest_eth_addr[portid] =
1203 RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
1204 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
1207 evt_rsrc = l3fwd_get_eventdev_rsrc();
1208 /* parse application arguments (after the EAL ones) */
1209 ret = parse_args(argc, argv);
1211 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1213 /* Setup function pointers for lookup method. */
1214 setup_l3fwd_lookup_tables();
1216 evt_rsrc->per_port_pool = per_port_pool;
1217 evt_rsrc->pkt_pool = pktmbuf_pool;
1218 evt_rsrc->port_mask = enabled_port_mask;
1219 /* Configure eventdev parameters if user has requested */
1220 if (evt_rsrc->enabled) {
1221 l3fwd_event_resource_setup(&port_conf);
1223 l3fwd_lkp.main_loop = evt_rsrc->ops.em_event_loop;
1225 l3fwd_lkp.main_loop = evt_rsrc->ops.lpm_event_loop;
1226 l3fwd_event_service_setup();
1228 l3fwd_poll_resource_setup();
1231 RTE_ETH_FOREACH_DEV(portid) {
1232 if ((enabled_port_mask & (1 << portid)) == 0) {
1236 ret = rte_eth_dev_start(portid);
1238 rte_exit(EXIT_FAILURE,
1239 "rte_eth_dev_start: err=%d, port=%d\n",
1243 * If enabled, put device in promiscuous mode.
1244 * This allows IO forwarding mode to forward packets
1245 * to itself through 2 cross-connected ports of the
1248 if (promiscuous_on) {
1249 ret = rte_eth_promiscuous_enable(portid);
1251 rte_exit(EXIT_FAILURE,
1252 "rte_eth_promiscuous_enable: err=%s, port=%u\n",
1253 rte_strerror(-ret), portid);
1259 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1260 if (rte_lcore_is_enabled(lcore_id) == 0)
1262 qconf = &lcore_conf[lcore_id];
1263 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1264 portid = qconf->rx_queue_list[queue].port_id;
1265 queueid = qconf->rx_queue_list[queue].queue_id;
1266 if (prepare_ptype_parser(portid, queueid) == 0)
1267 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1271 check_all_ports_link_status(enabled_port_mask);
1274 /* launch per-lcore init on every lcore */
1275 rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MASTER);
1276 if (evt_rsrc->enabled) {
1277 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++)
1278 rte_event_eth_rx_adapter_stop(
1279 evt_rsrc->rx_adptr.rx_adptr[i]);
1280 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++)
1281 rte_event_eth_tx_adapter_stop(
1282 evt_rsrc->tx_adptr.tx_adptr[i]);
1284 RTE_ETH_FOREACH_DEV(portid) {
1285 if ((enabled_port_mask & (1 << portid)) == 0)
1287 rte_eth_dev_stop(portid);
1290 rte_eal_mp_wait_lcore();
1291 RTE_ETH_FOREACH_DEV(portid) {
1292 if ((enabled_port_mask & (1 << portid)) == 0)
1294 rte_eth_dev_close(portid);
1297 rte_event_dev_stop(evt_rsrc->event_d_id);
1298 rte_event_dev_close(evt_rsrc->event_d_id);
1301 rte_eal_mp_wait_lcore();
1303 RTE_ETH_FOREACH_DEV(portid) {
1304 if ((enabled_port_mask & (1 << portid)) == 0)
1306 printf("Closing port %d...", portid);
1307 rte_eth_dev_stop(portid);
1308 rte_eth_dev_close(portid);