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>
54 #include <rte_launch.h>
55 #include <rte_atomic.h>
56 #include <rte_cycles.h>
57 #include <rte_prefetch.h>
58 #include <rte_lcore.h>
59 #include <rte_per_lcore.h>
60 #include <rte_branch_prediction.h>
61 #include <rte_interrupts.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_mempool.h>
71 #include <rte_string_fns.h>
72 #include <rte_cpuflags.h>
74 #include <cmdline_parse.h>
75 #include <cmdline_parse_etheraddr.h>
80 * Configurable number of RX/TX ring descriptors
82 #define RTE_TEST_RX_DESC_DEFAULT 128
83 #define RTE_TEST_TX_DESC_DEFAULT 512
85 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
86 #define MAX_RX_QUEUE_PER_PORT 128
88 #define MAX_LCORE_PARAMS 1024
90 /* Static global variables used within this file. */
91 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
92 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
94 /**< Ports set in promiscuous mode off by default. */
95 static int promiscuous_on;
97 /* Select Longest-Prefix or Exact match. */
98 static int l3fwd_lpm_on;
99 static int l3fwd_em_on;
101 static int numa_on = 1; /**< NUMA is enabled by default. */
102 static int parse_ptype; /**< Parse packet type using rx callback, and */
103 /**< disabled by default */
105 /* Global variables. */
107 volatile bool force_quit;
109 /* ethernet addresses of ports */
110 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
111 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
113 xmm_t val_eth[RTE_MAX_ETHPORTS];
115 /* mask of enabled ports */
116 uint32_t enabled_port_mask;
118 /* Used only in exact match mode. */
119 int ipv6; /**< ipv6 is false by default. */
120 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
122 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
124 struct lcore_params {
128 } __rte_cache_aligned;
130 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
131 static struct lcore_params lcore_params_array_default[] = {
143 static struct lcore_params * lcore_params = lcore_params_array_default;
144 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
145 sizeof(lcore_params_array_default[0]);
147 static struct rte_eth_conf port_conf = {
149 .mq_mode = ETH_MQ_RX_RSS,
150 .max_rx_pkt_len = ETHER_MAX_LEN,
152 .header_split = 0, /**< Header Split disabled */
153 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
154 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
155 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
156 .hw_strip_crc = 1, /**< CRC stripped by hardware */
161 .rss_hf = ETH_RSS_IP,
165 .mq_mode = ETH_MQ_TX_NONE,
169 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
171 struct l3fwd_lkp_mode {
173 int (*check_ptype)(int);
174 rte_rx_callback_fn cb_parse_ptype;
175 int (*main_loop)(void *);
176 void* (*get_ipv4_lookup_struct)(int);
177 void* (*get_ipv6_lookup_struct)(int);
180 static struct l3fwd_lkp_mode l3fwd_lkp;
182 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
184 .check_ptype = em_check_ptype,
185 .cb_parse_ptype = em_cb_parse_ptype,
186 .main_loop = em_main_loop,
187 .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
188 .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
191 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
193 .check_ptype = lpm_check_ptype,
194 .cb_parse_ptype = lpm_cb_parse_ptype,
195 .main_loop = lpm_main_loop,
196 .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
197 .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
201 * Setup lookup methods for forwarding.
202 * Currently exact-match and longest-prefix-match
203 * are supported ones.
206 setup_l3fwd_lookup_tables(void)
208 /* Setup HASH lookup functions. */
210 l3fwd_lkp = l3fwd_em_lkp;
211 /* Setup LPM lookup functions. */
213 l3fwd_lkp = l3fwd_lpm_lkp;
217 check_lcore_params(void)
219 uint8_t queue, lcore;
223 for (i = 0; i < nb_lcore_params; ++i) {
224 queue = lcore_params[i].queue_id;
225 if (queue >= MAX_RX_QUEUE_PER_PORT) {
226 printf("invalid queue number: %hhu\n", queue);
229 lcore = lcore_params[i].lcore_id;
230 if (!rte_lcore_is_enabled(lcore)) {
231 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
234 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
236 printf("warning: lcore %hhu is on socket %d with numa off \n",
244 check_port_config(const unsigned nb_ports)
249 for (i = 0; i < nb_lcore_params; ++i) {
250 portid = lcore_params[i].port_id;
251 if ((enabled_port_mask & (1 << portid)) == 0) {
252 printf("port %u is not enabled in port mask\n", portid);
255 if (portid >= nb_ports) {
256 printf("port %u is not present on the board\n", portid);
264 get_port_n_rx_queues(const uint16_t port)
269 for (i = 0; i < nb_lcore_params; ++i) {
270 if (lcore_params[i].port_id == port) {
271 if (lcore_params[i].queue_id == queue+1)
272 queue = lcore_params[i].queue_id;
274 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
275 " in sequence and must start with 0\n",
276 lcore_params[i].port_id);
279 return (uint8_t)(++queue);
283 init_lcore_rx_queues(void)
285 uint16_t i, nb_rx_queue;
288 for (i = 0; i < nb_lcore_params; ++i) {
289 lcore = lcore_params[i].lcore_id;
290 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
291 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
292 printf("error: too many queues (%u) for lcore: %u\n",
293 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
296 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
297 lcore_params[i].port_id;
298 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
299 lcore_params[i].queue_id;
300 lcore_conf[lcore].n_rx_queue++;
308 print_usage(const char *prgname)
310 printf("%s [EAL options] --"
315 " --config (port,queue,lcore)[,(port,queue,lcore)]"
316 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
317 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
319 " [--hash-entry-num]"
321 " [--parse-ptype]\n\n"
323 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
324 " -P : Enable promiscuous mode\n"
325 " -E : Enable exact match\n"
326 " -L : Enable longest prefix match (default)\n"
327 " --config (port,queue,lcore): Rx queue configuration\n"
328 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
329 " --enable-jumbo: Enable jumbo frames\n"
330 " --max-pkt-len: Under the premise of enabling jumbo,\n"
331 " maximum packet length in decimal (64-9600)\n"
332 " --no-numa: Disable numa awareness\n"
333 " --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
334 " --ipv6: Set if running ipv6 packets\n"
335 " --parse-ptype: Set to use software to analyze packet type\n\n",
340 parse_max_pkt_len(const char *pktlen)
345 /* parse decimal string */
346 len = strtoul(pktlen, &end, 10);
347 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
357 parse_portmask(const char *portmask)
362 /* parse hexadecimal string */
363 pm = strtoul(portmask, &end, 16);
364 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
374 parse_hash_entry_number(const char *hash_entry_num)
377 unsigned long hash_en;
378 /* parse hexadecimal string */
379 hash_en = strtoul(hash_entry_num, &end, 16);
380 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
390 parse_config(const char *q_arg)
393 const char *p, *p0 = q_arg;
401 unsigned long int_fld[_NUM_FLD];
402 char *str_fld[_NUM_FLD];
408 while ((p = strchr(p0,'(')) != NULL) {
410 if((p0 = strchr(p,')')) == NULL)
414 if(size >= sizeof(s))
417 snprintf(s, sizeof(s), "%.*s", size, p);
418 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
420 for (i = 0; i < _NUM_FLD; i++){
422 int_fld[i] = strtoul(str_fld[i], &end, 0);
423 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
426 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
427 printf("exceeded max number of lcore params: %hu\n",
431 lcore_params_array[nb_lcore_params].port_id =
432 (uint8_t)int_fld[FLD_PORT];
433 lcore_params_array[nb_lcore_params].queue_id =
434 (uint8_t)int_fld[FLD_QUEUE];
435 lcore_params_array[nb_lcore_params].lcore_id =
436 (uint8_t)int_fld[FLD_LCORE];
439 lcore_params = lcore_params_array;
444 parse_eth_dest(const char *optarg)
448 uint8_t c, *dest, peer_addr[6];
451 portid = strtoul(optarg, &port_end, 10);
452 if (errno != 0 || port_end == optarg || *port_end++ != ',')
453 rte_exit(EXIT_FAILURE,
454 "Invalid eth-dest: %s", optarg);
455 if (portid >= RTE_MAX_ETHPORTS)
456 rte_exit(EXIT_FAILURE,
457 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
458 portid, RTE_MAX_ETHPORTS);
460 if (cmdline_parse_etheraddr(NULL, port_end,
461 &peer_addr, sizeof(peer_addr)) < 0)
462 rte_exit(EXIT_FAILURE,
463 "Invalid ethernet address: %s\n",
465 dest = (uint8_t *)&dest_eth_addr[portid];
466 for (c = 0; c < 6; c++)
467 dest[c] = peer_addr[c];
468 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
471 #define MAX_JUMBO_PKT_LEN 9600
472 #define MEMPOOL_CACHE_SIZE 256
474 static const char short_options[] =
476 "P" /* promiscuous */
477 "L" /* enable long prefix match */
478 "E" /* enable exact match */
481 #define CMD_LINE_OPT_CONFIG "config"
482 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
483 #define CMD_LINE_OPT_NO_NUMA "no-numa"
484 #define CMD_LINE_OPT_IPV6 "ipv6"
485 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
486 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
487 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
489 /* long options mapped to a short option */
491 /* first long only option value must be >= 256, so that we won't
492 * conflict with short options */
493 CMD_LINE_OPT_MIN_NUM = 256,
494 CMD_LINE_OPT_CONFIG_NUM,
495 CMD_LINE_OPT_ETH_DEST_NUM,
496 CMD_LINE_OPT_NO_NUMA_NUM,
497 CMD_LINE_OPT_IPV6_NUM,
498 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
499 CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
500 CMD_LINE_OPT_PARSE_PTYPE_NUM,
503 static const struct option lgopts[] = {
504 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
505 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
506 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
507 {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
508 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
509 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
510 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
515 * This expression is used to calculate the number of mbufs needed
516 * depending on user input, taking into account memory for rx and
517 * tx hardware rings, cache per lcore and mtable per port per lcore.
518 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
521 #define NB_MBUF RTE_MAX( \
522 (nb_ports*nb_rx_queue*nb_rxd + \
523 nb_ports*nb_lcores*MAX_PKT_BURST + \
524 nb_ports*n_tx_queue*nb_txd + \
525 nb_lcores*MEMPOOL_CACHE_SIZE), \
528 /* Parse the argument given in the command line of the application */
530 parse_args(int argc, char **argv)
535 char *prgname = argv[0];
539 /* Error or normal output strings. */
540 const char *str1 = "L3FWD: Invalid portmask";
541 const char *str2 = "L3FWD: Promiscuous mode selected";
542 const char *str3 = "L3FWD: Exact match selected";
543 const char *str4 = "L3FWD: Longest-prefix match selected";
544 const char *str5 = "L3FWD: Invalid config";
545 const char *str6 = "L3FWD: NUMA is disabled";
546 const char *str7 = "L3FWD: IPV6 is specified";
548 "L3FWD: Jumbo frame is enabled - disabling simple TX path";
549 const char *str9 = "L3FWD: Invalid packet length";
550 const char *str10 = "L3FWD: Set jumbo frame max packet len to ";
551 const char *str11 = "L3FWD: Invalid hash entry number";
553 "L3FWD: LPM and EM are mutually exclusive, select only one";
554 const char *str13 = "L3FWD: LPM or EM none selected, default LPM on";
556 while ((opt = getopt_long(argc, argvopt, short_options,
557 lgopts, &option_index)) != EOF) {
562 enabled_port_mask = parse_portmask(optarg);
563 if (enabled_port_mask == 0) {
564 printf("%s\n", str1);
565 print_usage(prgname);
571 printf("%s\n", str2);
576 printf("%s\n", str3);
581 printf("%s\n", str4);
586 case CMD_LINE_OPT_CONFIG_NUM:
587 ret = parse_config(optarg);
589 printf("%s\n", str5);
590 print_usage(prgname);
595 case CMD_LINE_OPT_ETH_DEST_NUM:
596 parse_eth_dest(optarg);
599 case CMD_LINE_OPT_NO_NUMA_NUM:
600 printf("%s\n", str6);
604 case CMD_LINE_OPT_IPV6_NUM:
609 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
610 struct option lenopts = {
611 "max-pkt-len", required_argument, 0, 0
614 printf("%s\n", str8);
615 port_conf.rxmode.jumbo_frame = 1;
618 * if no max-pkt-len set, use the default
619 * value ETHER_MAX_LEN.
621 if (getopt_long(argc, argvopt, "",
622 &lenopts, &option_index) == 0) {
623 ret = parse_max_pkt_len(optarg);
625 (ret > MAX_JUMBO_PKT_LEN)) {
626 printf("%s\n", str9);
627 print_usage(prgname);
630 port_conf.rxmode.max_rx_pkt_len = ret;
632 printf("%s %u\n", str10,
633 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
637 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
638 ret = parse_hash_entry_number(optarg);
639 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
640 hash_entry_number = ret;
642 printf("%s\n", str11);
643 print_usage(prgname);
648 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
649 printf("soft parse-ptype is enabled\n");
654 print_usage(prgname);
659 /* If both LPM and EM are selected, return error. */
660 if (l3fwd_lpm_on && l3fwd_em_on) {
661 printf("%s\n", str12);
666 * Nothing is selected, pick longest-prefix match
669 if (!l3fwd_lpm_on && !l3fwd_em_on) {
671 printf("%s\n", str13);
675 * ipv6 and hash flags are valid only for
676 * exact macth, reset them to default for
677 * longest-prefix match.
681 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
685 argv[optind-1] = prgname;
688 optind = 1; /* reset getopt lib */
693 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
695 char buf[ETHER_ADDR_FMT_SIZE];
696 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
697 printf("%s%s", name, buf);
701 init_mem(unsigned nb_mbuf)
703 struct lcore_conf *qconf;
708 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
709 if (rte_lcore_is_enabled(lcore_id) == 0)
713 socketid = rte_lcore_to_socket_id(lcore_id);
717 if (socketid >= NB_SOCKETS) {
718 rte_exit(EXIT_FAILURE,
719 "Socket %d of lcore %u is out of range %d\n",
720 socketid, lcore_id, NB_SOCKETS);
723 if (pktmbuf_pool[socketid] == NULL) {
724 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
725 pktmbuf_pool[socketid] =
726 rte_pktmbuf_pool_create(s, nb_mbuf,
727 MEMPOOL_CACHE_SIZE, 0,
728 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
729 if (pktmbuf_pool[socketid] == NULL)
730 rte_exit(EXIT_FAILURE,
731 "Cannot init mbuf pool on socket %d\n",
734 printf("Allocated mbuf pool on socket %d\n",
737 /* Setup either LPM or EM(f.e Hash). */
738 l3fwd_lkp.setup(socketid);
740 qconf = &lcore_conf[lcore_id];
741 qconf->ipv4_lookup_struct =
742 l3fwd_lkp.get_ipv4_lookup_struct(socketid);
743 qconf->ipv6_lookup_struct =
744 l3fwd_lkp.get_ipv6_lookup_struct(socketid);
749 /* Check the link status of all ports in up to 9s, and print them finally */
751 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
753 #define CHECK_INTERVAL 100 /* 100ms */
754 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
756 uint8_t count, all_ports_up, print_flag = 0;
757 struct rte_eth_link link;
759 printf("\nChecking link status");
761 for (count = 0; count <= MAX_CHECK_TIME; count++) {
765 for (portid = 0; portid < port_num; portid++) {
768 if ((port_mask & (1 << portid)) == 0)
770 memset(&link, 0, sizeof(link));
771 rte_eth_link_get_nowait(portid, &link);
772 /* print link status if flag set */
773 if (print_flag == 1) {
774 if (link.link_status)
776 "Port%d Link Up. Speed %u Mbps -%s\n",
777 portid, link.link_speed,
778 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
779 ("full-duplex") : ("half-duplex\n"));
781 printf("Port %d Link Down\n", portid);
784 /* clear all_ports_up flag if any link down */
785 if (link.link_status == ETH_LINK_DOWN) {
790 /* after finally printing all link status, get out */
794 if (all_ports_up == 0) {
797 rte_delay_ms(CHECK_INTERVAL);
800 /* set the print_flag if all ports up or timeout */
801 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
809 signal_handler(int signum)
811 if (signum == SIGINT || signum == SIGTERM) {
812 printf("\n\nSignal %d received, preparing to exit...\n",
819 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
822 printf("Port %d: softly parse packet type info\n", portid);
823 if (rte_eth_add_rx_callback(portid, queueid,
824 l3fwd_lkp.cb_parse_ptype,
828 printf("Failed to add rx callback: port=%d\n", portid);
832 if (l3fwd_lkp.check_ptype(portid))
835 printf("port %d cannot parse packet type, please add --%s\n",
836 portid, CMD_LINE_OPT_PARSE_PTYPE);
841 main(int argc, char **argv)
843 struct lcore_conf *qconf;
844 struct rte_eth_dev_info dev_info;
845 struct rte_eth_txconf *txconf;
848 uint16_t queueid, portid;
850 uint32_t n_tx_queue, nb_lcores;
851 uint8_t nb_rx_queue, queue, socketid;
854 ret = rte_eal_init(argc, argv);
856 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
861 signal(SIGINT, signal_handler);
862 signal(SIGTERM, signal_handler);
864 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
865 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
866 dest_eth_addr[portid] =
867 ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
868 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
871 /* parse application arguments (after the EAL ones) */
872 ret = parse_args(argc, argv);
874 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
876 if (check_lcore_params() < 0)
877 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
879 ret = init_lcore_rx_queues();
881 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
883 nb_ports = rte_eth_dev_count();
885 if (check_port_config(nb_ports) < 0)
886 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
888 nb_lcores = rte_lcore_count();
890 /* Setup function pointers for lookup method. */
891 setup_l3fwd_lookup_tables();
893 /* initialize all ports */
894 for (portid = 0; portid < nb_ports; portid++) {
895 /* skip ports that are not enabled */
896 if ((enabled_port_mask & (1 << portid)) == 0) {
897 printf("\nSkipping disabled port %d\n", portid);
902 printf("Initializing port %d ... ", portid );
905 nb_rx_queue = get_port_n_rx_queues(portid);
906 n_tx_queue = nb_lcores;
907 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
908 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
909 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
910 nb_rx_queue, (unsigned)n_tx_queue );
911 ret = rte_eth_dev_configure(portid, nb_rx_queue,
912 (uint16_t)n_tx_queue, &port_conf);
914 rte_exit(EXIT_FAILURE,
915 "Cannot configure device: err=%d, port=%d\n",
918 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
921 rte_exit(EXIT_FAILURE,
922 "Cannot adjust number of descriptors: err=%d, "
923 "port=%d\n", ret, portid);
925 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
926 print_ethaddr(" Address:", &ports_eth_addr[portid]);
928 print_ethaddr("Destination:",
929 (const struct ether_addr *)&dest_eth_addr[portid]);
933 * prepare src MACs for each port.
935 ether_addr_copy(&ports_eth_addr[portid],
936 (struct ether_addr *)(val_eth + portid) + 1);
939 ret = init_mem(NB_MBUF);
941 rte_exit(EXIT_FAILURE, "init_mem failed\n");
943 /* init one TX queue per couple (lcore,port) */
945 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
946 if (rte_lcore_is_enabled(lcore_id) == 0)
951 (uint8_t)rte_lcore_to_socket_id(lcore_id);
955 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
958 rte_eth_dev_info_get(portid, &dev_info);
959 txconf = &dev_info.default_txconf;
960 if (port_conf.rxmode.jumbo_frame)
961 txconf->txq_flags = 0;
962 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
965 rte_exit(EXIT_FAILURE,
966 "rte_eth_tx_queue_setup: err=%d, "
967 "port=%d\n", ret, portid);
969 qconf = &lcore_conf[lcore_id];
970 qconf->tx_queue_id[portid] = queueid;
973 qconf->tx_port_id[qconf->n_tx_port] = portid;
979 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
980 if (rte_lcore_is_enabled(lcore_id) == 0)
982 qconf = &lcore_conf[lcore_id];
983 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
986 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
987 portid = qconf->rx_queue_list[queue].port_id;
988 queueid = qconf->rx_queue_list[queue].queue_id;
992 (uint8_t)rte_lcore_to_socket_id(lcore_id);
996 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
999 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1002 pktmbuf_pool[socketid]);
1004 rte_exit(EXIT_FAILURE,
1005 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
1013 for (portid = 0; portid < nb_ports; portid++) {
1014 if ((enabled_port_mask & (1 << portid)) == 0) {
1018 ret = rte_eth_dev_start(portid);
1020 rte_exit(EXIT_FAILURE,
1021 "rte_eth_dev_start: err=%d, port=%d\n",
1025 * If enabled, put device in promiscuous mode.
1026 * This allows IO forwarding mode to forward packets
1027 * to itself through 2 cross-connected ports of the
1031 rte_eth_promiscuous_enable(portid);
1036 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1037 if (rte_lcore_is_enabled(lcore_id) == 0)
1039 qconf = &lcore_conf[lcore_id];
1040 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1041 portid = qconf->rx_queue_list[queue].port_id;
1042 queueid = qconf->rx_queue_list[queue].queue_id;
1043 if (prepare_ptype_parser(portid, queueid) == 0)
1044 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1049 check_all_ports_link_status(nb_ports, enabled_port_mask);
1052 /* launch per-lcore init on every lcore */
1053 rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MASTER);
1054 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1055 if (rte_eal_wait_lcore(lcore_id) < 0) {
1062 for (portid = 0; portid < nb_ports; portid++) {
1063 if ((enabled_port_mask & (1 << portid)) == 0)
1065 printf("Closing port %d...", portid);
1066 rte_eth_dev_stop(portid);
1067 rte_eth_dev_close(portid);