4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <sys/types.h>
40 #include <sys/queue.h>
47 #include <rte_common.h>
49 #include <rte_byteorder.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
69 #include <rte_mempool.h>
74 #include <rte_string_fns.h>
75 #include <rte_cpuflags.h>
77 #include <cmdline_parse.h>
78 #include <cmdline_parse_etheraddr.h>
83 * Configurable number of RX/TX ring descriptors
85 #define RTE_TEST_RX_DESC_DEFAULT 128
86 #define RTE_TEST_TX_DESC_DEFAULT 512
88 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
89 #define MAX_RX_QUEUE_PER_PORT 128
91 #define MAX_LCORE_PARAMS 1024
93 /* Static global variables used within this file. */
94 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
95 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
97 /**< Ports set in promiscuous mode off by default. */
98 static int promiscuous_on;
100 /* Select Longest-Prefix or Exact match. */
101 static int l3fwd_lpm_on;
102 static int l3fwd_em_on;
104 static int numa_on = 1; /**< NUMA is enabled by default. */
105 static int parse_ptype; /**< Parse packet type using rx callback, and */
106 /**< disabled by default */
108 /* Global variables. */
110 volatile bool force_quit;
112 /* ethernet addresses of ports */
113 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
114 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
116 xmm_t val_eth[RTE_MAX_ETHPORTS];
118 /* mask of enabled ports */
119 uint32_t enabled_port_mask;
121 /* Used only in exact match mode. */
122 int ipv6; /**< ipv6 is false by default. */
123 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
125 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
127 struct lcore_params {
131 } __rte_cache_aligned;
133 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
134 static struct lcore_params lcore_params_array_default[] = {
146 static struct lcore_params * lcore_params = lcore_params_array_default;
147 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
148 sizeof(lcore_params_array_default[0]);
150 static struct rte_eth_conf port_conf = {
152 .mq_mode = ETH_MQ_RX_RSS,
153 .max_rx_pkt_len = ETHER_MAX_LEN,
155 .header_split = 0, /**< Header Split disabled */
156 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
157 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
158 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
159 .hw_strip_crc = 1, /**< CRC stripped by hardware */
164 .rss_hf = ETH_RSS_IP,
168 .mq_mode = ETH_MQ_TX_NONE,
172 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
174 struct l3fwd_lkp_mode {
176 int (*check_ptype)(int);
177 rte_rx_callback_fn cb_parse_ptype;
178 int (*main_loop)(void *);
179 void* (*get_ipv4_lookup_struct)(int);
180 void* (*get_ipv6_lookup_struct)(int);
183 static struct l3fwd_lkp_mode l3fwd_lkp;
185 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
187 .check_ptype = em_check_ptype,
188 .cb_parse_ptype = em_cb_parse_ptype,
189 .main_loop = em_main_loop,
190 .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
191 .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
194 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
196 .check_ptype = lpm_check_ptype,
197 .cb_parse_ptype = lpm_cb_parse_ptype,
198 .main_loop = lpm_main_loop,
199 .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
200 .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
204 * Setup lookup methods for forwarding.
205 * Currently exact-match and longest-prefix-match
206 * are supported ones.
209 setup_l3fwd_lookup_tables(void)
211 /* Setup HASH lookup functions. */
213 l3fwd_lkp = l3fwd_em_lkp;
214 /* Setup LPM lookup functions. */
216 l3fwd_lkp = l3fwd_lpm_lkp;
220 check_lcore_params(void)
222 uint8_t queue, lcore;
226 for (i = 0; i < nb_lcore_params; ++i) {
227 queue = lcore_params[i].queue_id;
228 if (queue >= MAX_RX_QUEUE_PER_PORT) {
229 printf("invalid queue number: %hhu\n", queue);
232 lcore = lcore_params[i].lcore_id;
233 if (!rte_lcore_is_enabled(lcore)) {
234 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
237 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
239 printf("warning: lcore %hhu is on socket %d with numa off \n",
247 check_port_config(const unsigned nb_ports)
252 for (i = 0; i < nb_lcore_params; ++i) {
253 portid = lcore_params[i].port_id;
254 if ((enabled_port_mask & (1 << portid)) == 0) {
255 printf("port %u is not enabled in port mask\n", portid);
258 if (portid >= nb_ports) {
259 printf("port %u is not present on the board\n", portid);
267 get_port_n_rx_queues(const uint8_t port)
272 for (i = 0; i < nb_lcore_params; ++i) {
273 if (lcore_params[i].port_id == port) {
274 if (lcore_params[i].queue_id == queue+1)
275 queue = lcore_params[i].queue_id;
277 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
278 " in sequence and must start with 0\n",
279 lcore_params[i].port_id);
282 return (uint8_t)(++queue);
286 init_lcore_rx_queues(void)
288 uint16_t i, nb_rx_queue;
291 for (i = 0; i < nb_lcore_params; ++i) {
292 lcore = lcore_params[i].lcore_id;
293 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
294 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
295 printf("error: too many queues (%u) for lcore: %u\n",
296 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
299 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
300 lcore_params[i].port_id;
301 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
302 lcore_params[i].queue_id;
303 lcore_conf[lcore].n_rx_queue++;
311 print_usage(const char *prgname)
313 printf("%s [EAL options] --"
318 " --config (port,queue,lcore)[,(port,queue,lcore)]"
319 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
320 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
322 " [--hash-entry-num]"
324 " [--parse-ptype]\n\n"
326 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
327 " -P : Enable promiscuous mode\n"
328 " -E : Enable exact match\n"
329 " -L : Enable longest prefix match (default)\n"
330 " --config (port,queue,lcore): Rx queue configuration\n"
331 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
332 " --enable-jumbo: Enable jumbo frames\n"
333 " --max-pkt-len: Under the premise of enabling jumbo,\n"
334 " maximum packet length in decimal (64-9600)\n"
335 " --no-numa: Disable numa awareness\n"
336 " --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
337 " --ipv6: Set if running ipv6 packets\n"
338 " --parse-ptype: Set to use software to analyze packet type\n\n",
343 parse_max_pkt_len(const char *pktlen)
348 /* parse decimal string */
349 len = strtoul(pktlen, &end, 10);
350 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
360 parse_portmask(const char *portmask)
365 /* parse hexadecimal string */
366 pm = strtoul(portmask, &end, 16);
367 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
377 parse_hash_entry_number(const char *hash_entry_num)
380 unsigned long hash_en;
381 /* parse hexadecimal string */
382 hash_en = strtoul(hash_entry_num, &end, 16);
383 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
393 parse_config(const char *q_arg)
396 const char *p, *p0 = q_arg;
404 unsigned long int_fld[_NUM_FLD];
405 char *str_fld[_NUM_FLD];
411 while ((p = strchr(p0,'(')) != NULL) {
413 if((p0 = strchr(p,')')) == NULL)
417 if(size >= sizeof(s))
420 snprintf(s, sizeof(s), "%.*s", size, p);
421 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
423 for (i = 0; i < _NUM_FLD; i++){
425 int_fld[i] = strtoul(str_fld[i], &end, 0);
426 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
429 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
430 printf("exceeded max number of lcore params: %hu\n",
434 lcore_params_array[nb_lcore_params].port_id =
435 (uint8_t)int_fld[FLD_PORT];
436 lcore_params_array[nb_lcore_params].queue_id =
437 (uint8_t)int_fld[FLD_QUEUE];
438 lcore_params_array[nb_lcore_params].lcore_id =
439 (uint8_t)int_fld[FLD_LCORE];
442 lcore_params = lcore_params_array;
447 parse_eth_dest(const char *optarg)
451 uint8_t c, *dest, peer_addr[6];
454 portid = strtoul(optarg, &port_end, 10);
455 if (errno != 0 || port_end == optarg || *port_end++ != ',')
456 rte_exit(EXIT_FAILURE,
457 "Invalid eth-dest: %s", optarg);
458 if (portid >= RTE_MAX_ETHPORTS)
459 rte_exit(EXIT_FAILURE,
460 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
461 portid, RTE_MAX_ETHPORTS);
463 if (cmdline_parse_etheraddr(NULL, port_end,
464 &peer_addr, sizeof(peer_addr)) < 0)
465 rte_exit(EXIT_FAILURE,
466 "Invalid ethernet address: %s\n",
468 dest = (uint8_t *)&dest_eth_addr[portid];
469 for (c = 0; c < 6; c++)
470 dest[c] = peer_addr[c];
471 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
474 #define MAX_JUMBO_PKT_LEN 9600
475 #define MEMPOOL_CACHE_SIZE 256
477 static const char short_options[] =
479 "P" /* promiscuous */
480 "L" /* enable long prefix match */
481 "E" /* enable exact match */
484 #define CMD_LINE_OPT_CONFIG "config"
485 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
486 #define CMD_LINE_OPT_NO_NUMA "no-numa"
487 #define CMD_LINE_OPT_IPV6 "ipv6"
488 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
489 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
490 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
492 /* long options mapped to a short option */
494 /* first long only option value must be >= 256, so that we won't
495 * conflict with short options */
496 CMD_LINE_OPT_MIN_NUM = 256,
497 CMD_LINE_OPT_CONFIG_NUM,
498 CMD_LINE_OPT_ETH_DEST_NUM,
499 CMD_LINE_OPT_NO_NUMA_NUM,
500 CMD_LINE_OPT_IPV6_NUM,
501 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
502 CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
503 CMD_LINE_OPT_PARSE_PTYPE_NUM,
506 static const struct option lgopts[] = {
507 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
508 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
509 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
510 {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
511 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
512 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
513 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
518 * This expression is used to calculate the number of mbufs needed
519 * depending on user input, taking into account memory for rx and
520 * tx hardware rings, cache per lcore and mtable per port per lcore.
521 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
524 #define NB_MBUF RTE_MAX( \
525 (nb_ports*nb_rx_queue*nb_rxd + \
526 nb_ports*nb_lcores*MAX_PKT_BURST + \
527 nb_ports*n_tx_queue*nb_txd + \
528 nb_lcores*MEMPOOL_CACHE_SIZE), \
531 /* Parse the argument given in the command line of the application */
533 parse_args(int argc, char **argv)
538 char *prgname = argv[0];
542 /* Error or normal output strings. */
543 const char *str1 = "L3FWD: Invalid portmask";
544 const char *str2 = "L3FWD: Promiscuous mode selected";
545 const char *str3 = "L3FWD: Exact match selected";
546 const char *str4 = "L3FWD: Longest-prefix match selected";
547 const char *str5 = "L3FWD: Invalid config";
548 const char *str6 = "L3FWD: NUMA is disabled";
549 const char *str7 = "L3FWD: IPV6 is specified";
551 "L3FWD: Jumbo frame is enabled - disabling simple TX path";
552 const char *str9 = "L3FWD: Invalid packet length";
553 const char *str10 = "L3FWD: Set jumbo frame max packet len to ";
554 const char *str11 = "L3FWD: Invalid hash entry number";
556 "L3FWD: LPM and EM are mutually exclusive, select only one";
557 const char *str13 = "L3FWD: LPM or EM none selected, default LPM on";
559 while ((opt = getopt_long(argc, argvopt, short_options,
560 lgopts, &option_index)) != EOF) {
565 enabled_port_mask = parse_portmask(optarg);
566 if (enabled_port_mask == 0) {
567 printf("%s\n", str1);
568 print_usage(prgname);
574 printf("%s\n", str2);
579 printf("%s\n", str3);
584 printf("%s\n", str4);
589 case CMD_LINE_OPT_CONFIG_NUM:
590 ret = parse_config(optarg);
592 printf("%s\n", str5);
593 print_usage(prgname);
598 case CMD_LINE_OPT_ETH_DEST_NUM:
599 parse_eth_dest(optarg);
602 case CMD_LINE_OPT_NO_NUMA_NUM:
603 printf("%s\n", str6);
607 case CMD_LINE_OPT_IPV6_NUM:
612 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
613 struct option lenopts = {
614 "max-pkt-len", required_argument, 0, 0
617 printf("%s\n", str8);
618 port_conf.rxmode.jumbo_frame = 1;
621 * if no max-pkt-len set, use the default
622 * value ETHER_MAX_LEN.
624 if (getopt_long(argc, argvopt, "",
625 &lenopts, &option_index) == 0) {
626 ret = parse_max_pkt_len(optarg);
628 (ret > MAX_JUMBO_PKT_LEN)) {
629 printf("%s\n", str9);
630 print_usage(prgname);
633 port_conf.rxmode.max_rx_pkt_len = ret;
635 printf("%s %u\n", str10,
636 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
640 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
641 ret = parse_hash_entry_number(optarg);
642 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
643 hash_entry_number = ret;
645 printf("%s\n", str11);
646 print_usage(prgname);
651 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
652 printf("soft parse-ptype is enabled\n");
657 print_usage(prgname);
662 /* If both LPM and EM are selected, return error. */
663 if (l3fwd_lpm_on && l3fwd_em_on) {
664 printf("%s\n", str12);
669 * Nothing is selected, pick longest-prefix match
672 if (!l3fwd_lpm_on && !l3fwd_em_on) {
674 printf("%s\n", str13);
678 * ipv6 and hash flags are valid only for
679 * exact macth, reset them to default for
680 * longest-prefix match.
684 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
688 argv[optind-1] = prgname;
691 optind = 1; /* reset getopt lib */
696 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
698 char buf[ETHER_ADDR_FMT_SIZE];
699 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
700 printf("%s%s", name, buf);
704 init_mem(unsigned nb_mbuf)
706 struct lcore_conf *qconf;
711 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
712 if (rte_lcore_is_enabled(lcore_id) == 0)
716 socketid = rte_lcore_to_socket_id(lcore_id);
720 if (socketid >= NB_SOCKETS) {
721 rte_exit(EXIT_FAILURE,
722 "Socket %d of lcore %u is out of range %d\n",
723 socketid, lcore_id, NB_SOCKETS);
726 if (pktmbuf_pool[socketid] == NULL) {
727 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
728 pktmbuf_pool[socketid] =
729 rte_pktmbuf_pool_create(s, nb_mbuf,
730 MEMPOOL_CACHE_SIZE, 0,
731 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
732 if (pktmbuf_pool[socketid] == NULL)
733 rte_exit(EXIT_FAILURE,
734 "Cannot init mbuf pool on socket %d\n",
737 printf("Allocated mbuf pool on socket %d\n",
740 /* Setup either LPM or EM(f.e Hash). */
741 l3fwd_lkp.setup(socketid);
743 qconf = &lcore_conf[lcore_id];
744 qconf->ipv4_lookup_struct =
745 l3fwd_lkp.get_ipv4_lookup_struct(socketid);
746 qconf->ipv6_lookup_struct =
747 l3fwd_lkp.get_ipv6_lookup_struct(socketid);
752 /* Check the link status of all ports in up to 9s, and print them finally */
754 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
756 #define CHECK_INTERVAL 100 /* 100ms */
757 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
758 uint8_t portid, count, all_ports_up, print_flag = 0;
759 struct rte_eth_link link;
761 printf("\nChecking link status");
763 for (count = 0; count <= MAX_CHECK_TIME; count++) {
767 for (portid = 0; portid < port_num; portid++) {
770 if ((port_mask & (1 << portid)) == 0)
772 memset(&link, 0, sizeof(link));
773 rte_eth_link_get_nowait(portid, &link);
774 /* print link status if flag set */
775 if (print_flag == 1) {
776 if (link.link_status)
777 printf("Port %d Link Up - speed %u "
778 "Mbps - %s\n", (uint8_t)portid,
779 (unsigned)link.link_speed,
780 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
781 ("full-duplex") : ("half-duplex\n"));
783 printf("Port %d Link Down\n",
787 /* clear all_ports_up flag if any link down */
788 if (link.link_status == ETH_LINK_DOWN) {
793 /* after finally printing all link status, get out */
797 if (all_ports_up == 0) {
800 rte_delay_ms(CHECK_INTERVAL);
803 /* set the print_flag if all ports up or timeout */
804 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
812 signal_handler(int signum)
814 if (signum == SIGINT || signum == SIGTERM) {
815 printf("\n\nSignal %d received, preparing to exit...\n",
822 prepare_ptype_parser(uint8_t portid, uint16_t queueid)
825 printf("Port %d: softly parse packet type info\n", portid);
826 if (rte_eth_add_rx_callback(portid, queueid,
827 l3fwd_lkp.cb_parse_ptype,
831 printf("Failed to add rx callback: port=%d\n", portid);
835 if (l3fwd_lkp.check_ptype(portid))
838 printf("port %d cannot parse packet type, please add --%s\n",
839 portid, CMD_LINE_OPT_PARSE_PTYPE);
844 main(int argc, char **argv)
846 struct lcore_conf *qconf;
847 struct rte_eth_dev_info dev_info;
848 struct rte_eth_txconf *txconf;
853 uint32_t n_tx_queue, nb_lcores;
854 uint8_t portid, nb_rx_queue, queue, socketid;
857 ret = rte_eal_init(argc, argv);
859 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
864 signal(SIGINT, signal_handler);
865 signal(SIGTERM, signal_handler);
867 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
868 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
869 dest_eth_addr[portid] =
870 ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
871 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
874 /* parse application arguments (after the EAL ones) */
875 ret = parse_args(argc, argv);
877 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
879 if (check_lcore_params() < 0)
880 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
882 ret = init_lcore_rx_queues();
884 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
886 nb_ports = rte_eth_dev_count();
888 if (check_port_config(nb_ports) < 0)
889 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
891 nb_lcores = rte_lcore_count();
893 /* Setup function pointers for lookup method. */
894 setup_l3fwd_lookup_tables();
896 /* initialize all ports */
897 for (portid = 0; portid < nb_ports; portid++) {
898 /* skip ports that are not enabled */
899 if ((enabled_port_mask & (1 << portid)) == 0) {
900 printf("\nSkipping disabled port %d\n", portid);
905 printf("Initializing port %d ... ", portid );
908 nb_rx_queue = get_port_n_rx_queues(portid);
909 n_tx_queue = nb_lcores;
910 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
911 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
912 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
913 nb_rx_queue, (unsigned)n_tx_queue );
914 ret = rte_eth_dev_configure(portid, nb_rx_queue,
915 (uint16_t)n_tx_queue, &port_conf);
917 rte_exit(EXIT_FAILURE,
918 "Cannot configure device: err=%d, port=%d\n",
921 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
924 rte_exit(EXIT_FAILURE,
925 "Cannot adjust number of descriptors: err=%d, "
926 "port=%d\n", ret, portid);
928 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
929 print_ethaddr(" Address:", &ports_eth_addr[portid]);
931 print_ethaddr("Destination:",
932 (const struct ether_addr *)&dest_eth_addr[portid]);
936 * prepare src MACs for each port.
938 ether_addr_copy(&ports_eth_addr[portid],
939 (struct ether_addr *)(val_eth + portid) + 1);
942 ret = init_mem(NB_MBUF);
944 rte_exit(EXIT_FAILURE, "init_mem failed\n");
946 /* init one TX queue per couple (lcore,port) */
948 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
949 if (rte_lcore_is_enabled(lcore_id) == 0)
954 (uint8_t)rte_lcore_to_socket_id(lcore_id);
958 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
961 rte_eth_dev_info_get(portid, &dev_info);
962 txconf = &dev_info.default_txconf;
963 if (port_conf.rxmode.jumbo_frame)
964 txconf->txq_flags = 0;
965 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
968 rte_exit(EXIT_FAILURE,
969 "rte_eth_tx_queue_setup: err=%d, "
970 "port=%d\n", ret, portid);
972 qconf = &lcore_conf[lcore_id];
973 qconf->tx_queue_id[portid] = queueid;
976 qconf->tx_port_id[qconf->n_tx_port] = portid;
982 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
983 if (rte_lcore_is_enabled(lcore_id) == 0)
985 qconf = &lcore_conf[lcore_id];
986 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
989 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
990 portid = qconf->rx_queue_list[queue].port_id;
991 queueid = qconf->rx_queue_list[queue].queue_id;
995 (uint8_t)rte_lcore_to_socket_id(lcore_id);
999 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1002 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1005 pktmbuf_pool[socketid]);
1007 rte_exit(EXIT_FAILURE,
1008 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
1016 for (portid = 0; portid < nb_ports; portid++) {
1017 if ((enabled_port_mask & (1 << portid)) == 0) {
1021 ret = rte_eth_dev_start(portid);
1023 rte_exit(EXIT_FAILURE,
1024 "rte_eth_dev_start: err=%d, port=%d\n",
1028 * If enabled, put device in promiscuous mode.
1029 * This allows IO forwarding mode to forward packets
1030 * to itself through 2 cross-connected ports of the
1034 rte_eth_promiscuous_enable(portid);
1039 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1040 if (rte_lcore_is_enabled(lcore_id) == 0)
1042 qconf = &lcore_conf[lcore_id];
1043 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1044 portid = qconf->rx_queue_list[queue].port_id;
1045 queueid = qconf->rx_queue_list[queue].queue_id;
1046 if (prepare_ptype_parser(portid, queueid) == 0)
1047 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1052 check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
1055 /* launch per-lcore init on every lcore */
1056 rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MASTER);
1057 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1058 if (rte_eal_wait_lcore(lcore_id) < 0) {
1065 for (portid = 0; portid < nb_ports; portid++) {
1066 if ((enabled_port_mask & (1 << portid)) == 0)
1068 printf("Closing port %d...", portid);
1069 rte_eth_dev_stop(portid);
1070 rte_eth_dev_close(portid);