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_launch.h>
56 #include <rte_atomic.h>
57 #include <rte_cycles.h>
58 #include <rte_prefetch.h>
59 #include <rte_lcore.h>
60 #include <rte_per_lcore.h>
61 #include <rte_branch_prediction.h>
62 #include <rte_interrupts.h>
64 #include <rte_random.h>
65 #include <rte_debug.h>
66 #include <rte_ether.h>
67 #include <rte_ethdev.h>
68 #include <rte_mempool.h>
73 #include <rte_string_fns.h>
74 #include <rte_cpuflags.h>
76 #include <cmdline_parse.h>
77 #include <cmdline_parse_etheraddr.h>
82 * Configurable number of RX/TX ring descriptors
84 #define RTE_TEST_RX_DESC_DEFAULT 128
85 #define RTE_TEST_TX_DESC_DEFAULT 512
87 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
88 #define MAX_RX_QUEUE_PER_PORT 128
90 #define MAX_LCORE_PARAMS 1024
92 /* Static global variables used within this file. */
93 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
94 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
96 /**< Ports set in promiscuous mode off by default. */
97 static int promiscuous_on;
99 /* Select Longest-Prefix or Exact match. */
100 static int l3fwd_lpm_on;
101 static int l3fwd_em_on;
103 static int numa_on = 1; /**< NUMA is enabled by default. */
104 static int parse_ptype; /**< Parse packet type using rx callback, and */
105 /**< disabled by default */
107 /* Global variables. */
109 volatile bool force_quit;
111 /* ethernet addresses of ports */
112 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
113 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
115 xmm_t val_eth[RTE_MAX_ETHPORTS];
117 /* mask of enabled ports */
118 uint32_t enabled_port_mask;
120 /* Used only in exact match mode. */
121 int ipv6; /**< ipv6 is false by default. */
122 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
124 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
126 struct lcore_params {
130 } __rte_cache_aligned;
132 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
133 static struct lcore_params lcore_params_array_default[] = {
145 static struct lcore_params * lcore_params = lcore_params_array_default;
146 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
147 sizeof(lcore_params_array_default[0]);
149 static struct rte_eth_conf port_conf = {
151 .mq_mode = ETH_MQ_RX_RSS,
152 .max_rx_pkt_len = ETHER_MAX_LEN,
154 .header_split = 0, /**< Header Split disabled */
155 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
156 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
157 .jumbo_frame = 0, /**< Jumbo Frame Support disabled */
158 .hw_strip_crc = 1, /**< CRC stripped by hardware */
163 .rss_hf = ETH_RSS_IP,
167 .mq_mode = ETH_MQ_TX_NONE,
171 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
173 struct l3fwd_lkp_mode {
175 int (*check_ptype)(int);
176 rte_rx_callback_fn cb_parse_ptype;
177 int (*main_loop)(void *);
178 void* (*get_ipv4_lookup_struct)(int);
179 void* (*get_ipv6_lookup_struct)(int);
182 static struct l3fwd_lkp_mode l3fwd_lkp;
184 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
186 .check_ptype = em_check_ptype,
187 .cb_parse_ptype = em_cb_parse_ptype,
188 .main_loop = em_main_loop,
189 .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
190 .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
193 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
195 .check_ptype = lpm_check_ptype,
196 .cb_parse_ptype = lpm_cb_parse_ptype,
197 .main_loop = lpm_main_loop,
198 .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
199 .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
203 * Setup lookup methods for forwarding.
204 * Currently exact-match and longest-prefix-match
205 * are supported ones.
208 setup_l3fwd_lookup_tables(void)
210 /* Setup HASH lookup functions. */
212 l3fwd_lkp = l3fwd_em_lkp;
213 /* Setup LPM lookup functions. */
215 l3fwd_lkp = l3fwd_lpm_lkp;
219 check_lcore_params(void)
221 uint8_t queue, lcore;
225 for (i = 0; i < nb_lcore_params; ++i) {
226 queue = lcore_params[i].queue_id;
227 if (queue >= MAX_RX_QUEUE_PER_PORT) {
228 printf("invalid queue number: %hhu\n", queue);
231 lcore = lcore_params[i].lcore_id;
232 if (!rte_lcore_is_enabled(lcore)) {
233 printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
236 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
238 printf("warning: lcore %hhu is on socket %d with numa off \n",
246 check_port_config(const unsigned nb_ports)
251 for (i = 0; i < nb_lcore_params; ++i) {
252 portid = lcore_params[i].port_id;
253 if ((enabled_port_mask & (1 << portid)) == 0) {
254 printf("port %u is not enabled in port mask\n", portid);
257 if (portid >= nb_ports) {
258 printf("port %u is not present on the board\n", portid);
266 get_port_n_rx_queues(const uint16_t port)
271 for (i = 0; i < nb_lcore_params; ++i) {
272 if (lcore_params[i].port_id == port) {
273 if (lcore_params[i].queue_id == queue+1)
274 queue = lcore_params[i].queue_id;
276 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
277 " in sequence and must start with 0\n",
278 lcore_params[i].port_id);
281 return (uint8_t)(++queue);
285 init_lcore_rx_queues(void)
287 uint16_t i, nb_rx_queue;
290 for (i = 0; i < nb_lcore_params; ++i) {
291 lcore = lcore_params[i].lcore_id;
292 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
293 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
294 printf("error: too many queues (%u) for lcore: %u\n",
295 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
298 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
299 lcore_params[i].port_id;
300 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
301 lcore_params[i].queue_id;
302 lcore_conf[lcore].n_rx_queue++;
310 print_usage(const char *prgname)
312 printf("%s [EAL options] --"
317 " --config (port,queue,lcore)[,(port,queue,lcore)]"
318 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
319 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
321 " [--hash-entry-num]"
323 " [--parse-ptype]\n\n"
325 " -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
326 " -P : Enable promiscuous mode\n"
327 " -E : Enable exact match\n"
328 " -L : Enable longest prefix match (default)\n"
329 " --config (port,queue,lcore): Rx queue configuration\n"
330 " --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
331 " --enable-jumbo: Enable jumbo frames\n"
332 " --max-pkt-len: Under the premise of enabling jumbo,\n"
333 " maximum packet length in decimal (64-9600)\n"
334 " --no-numa: Disable numa awareness\n"
335 " --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
336 " --ipv6: Set if running ipv6 packets\n"
337 " --parse-ptype: Set to use software to analyze packet type\n\n",
342 parse_max_pkt_len(const char *pktlen)
347 /* parse decimal string */
348 len = strtoul(pktlen, &end, 10);
349 if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
359 parse_portmask(const char *portmask)
364 /* parse hexadecimal string */
365 pm = strtoul(portmask, &end, 16);
366 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
376 parse_hash_entry_number(const char *hash_entry_num)
379 unsigned long hash_en;
380 /* parse hexadecimal string */
381 hash_en = strtoul(hash_entry_num, &end, 16);
382 if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
392 parse_config(const char *q_arg)
395 const char *p, *p0 = q_arg;
403 unsigned long int_fld[_NUM_FLD];
404 char *str_fld[_NUM_FLD];
410 while ((p = strchr(p0,'(')) != NULL) {
412 if((p0 = strchr(p,')')) == NULL)
416 if(size >= sizeof(s))
419 snprintf(s, sizeof(s), "%.*s", size, p);
420 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
422 for (i = 0; i < _NUM_FLD; i++){
424 int_fld[i] = strtoul(str_fld[i], &end, 0);
425 if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
428 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
429 printf("exceeded max number of lcore params: %hu\n",
433 lcore_params_array[nb_lcore_params].port_id =
434 (uint8_t)int_fld[FLD_PORT];
435 lcore_params_array[nb_lcore_params].queue_id =
436 (uint8_t)int_fld[FLD_QUEUE];
437 lcore_params_array[nb_lcore_params].lcore_id =
438 (uint8_t)int_fld[FLD_LCORE];
441 lcore_params = lcore_params_array;
446 parse_eth_dest(const char *optarg)
450 uint8_t c, *dest, peer_addr[6];
453 portid = strtoul(optarg, &port_end, 10);
454 if (errno != 0 || port_end == optarg || *port_end++ != ',')
455 rte_exit(EXIT_FAILURE,
456 "Invalid eth-dest: %s", optarg);
457 if (portid >= RTE_MAX_ETHPORTS)
458 rte_exit(EXIT_FAILURE,
459 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
460 portid, RTE_MAX_ETHPORTS);
462 if (cmdline_parse_etheraddr(NULL, port_end,
463 &peer_addr, sizeof(peer_addr)) < 0)
464 rte_exit(EXIT_FAILURE,
465 "Invalid ethernet address: %s\n",
467 dest = (uint8_t *)&dest_eth_addr[portid];
468 for (c = 0; c < 6; c++)
469 dest[c] = peer_addr[c];
470 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
473 #define MAX_JUMBO_PKT_LEN 9600
474 #define MEMPOOL_CACHE_SIZE 256
476 static const char short_options[] =
478 "P" /* promiscuous */
479 "L" /* enable long prefix match */
480 "E" /* enable exact match */
483 #define CMD_LINE_OPT_CONFIG "config"
484 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
485 #define CMD_LINE_OPT_NO_NUMA "no-numa"
486 #define CMD_LINE_OPT_IPV6 "ipv6"
487 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
488 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
489 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
491 /* long options mapped to a short option */
493 /* first long only option value must be >= 256, so that we won't
494 * conflict with short options */
495 CMD_LINE_OPT_MIN_NUM = 256,
496 CMD_LINE_OPT_CONFIG_NUM,
497 CMD_LINE_OPT_ETH_DEST_NUM,
498 CMD_LINE_OPT_NO_NUMA_NUM,
499 CMD_LINE_OPT_IPV6_NUM,
500 CMD_LINE_OPT_ENABLE_JUMBO_NUM,
501 CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
502 CMD_LINE_OPT_PARSE_PTYPE_NUM,
505 static const struct option lgopts[] = {
506 {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
507 {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
508 {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
509 {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
510 {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
511 {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
512 {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
517 * This expression is used to calculate the number of mbufs needed
518 * depending on user input, taking into account memory for rx and
519 * tx hardware rings, cache per lcore and mtable per port per lcore.
520 * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
523 #define NB_MBUF RTE_MAX( \
524 (nb_ports*nb_rx_queue*nb_rxd + \
525 nb_ports*nb_lcores*MAX_PKT_BURST + \
526 nb_ports*n_tx_queue*nb_txd + \
527 nb_lcores*MEMPOOL_CACHE_SIZE), \
530 /* Parse the argument given in the command line of the application */
532 parse_args(int argc, char **argv)
537 char *prgname = argv[0];
541 /* Error or normal output strings. */
542 const char *str1 = "L3FWD: Invalid portmask";
543 const char *str2 = "L3FWD: Promiscuous mode selected";
544 const char *str3 = "L3FWD: Exact match selected";
545 const char *str4 = "L3FWD: Longest-prefix match selected";
546 const char *str5 = "L3FWD: Invalid config";
547 const char *str6 = "L3FWD: NUMA is disabled";
548 const char *str7 = "L3FWD: IPV6 is specified";
550 "L3FWD: Jumbo frame is enabled - disabling simple TX path";
551 const char *str9 = "L3FWD: Invalid packet length";
552 const char *str10 = "L3FWD: Set jumbo frame max packet len to ";
553 const char *str11 = "L3FWD: Invalid hash entry number";
555 "L3FWD: LPM and EM are mutually exclusive, select only one";
556 const char *str13 = "L3FWD: LPM or EM none selected, default LPM on";
558 while ((opt = getopt_long(argc, argvopt, short_options,
559 lgopts, &option_index)) != EOF) {
564 enabled_port_mask = parse_portmask(optarg);
565 if (enabled_port_mask == 0) {
566 printf("%s\n", str1);
567 print_usage(prgname);
573 printf("%s\n", str2);
578 printf("%s\n", str3);
583 printf("%s\n", str4);
588 case CMD_LINE_OPT_CONFIG_NUM:
589 ret = parse_config(optarg);
591 printf("%s\n", str5);
592 print_usage(prgname);
597 case CMD_LINE_OPT_ETH_DEST_NUM:
598 parse_eth_dest(optarg);
601 case CMD_LINE_OPT_NO_NUMA_NUM:
602 printf("%s\n", str6);
606 case CMD_LINE_OPT_IPV6_NUM:
611 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
612 struct option lenopts = {
613 "max-pkt-len", required_argument, 0, 0
616 printf("%s\n", str8);
617 port_conf.rxmode.jumbo_frame = 1;
620 * if no max-pkt-len set, use the default
621 * value ETHER_MAX_LEN.
623 if (getopt_long(argc, argvopt, "",
624 &lenopts, &option_index) == 0) {
625 ret = parse_max_pkt_len(optarg);
627 (ret > MAX_JUMBO_PKT_LEN)) {
628 printf("%s\n", str9);
629 print_usage(prgname);
632 port_conf.rxmode.max_rx_pkt_len = ret;
634 printf("%s %u\n", str10,
635 (unsigned int)port_conf.rxmode.max_rx_pkt_len);
639 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
640 ret = parse_hash_entry_number(optarg);
641 if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
642 hash_entry_number = ret;
644 printf("%s\n", str11);
645 print_usage(prgname);
650 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
651 printf("soft parse-ptype is enabled\n");
656 print_usage(prgname);
661 /* If both LPM and EM are selected, return error. */
662 if (l3fwd_lpm_on && l3fwd_em_on) {
663 printf("%s\n", str12);
668 * Nothing is selected, pick longest-prefix match
671 if (!l3fwd_lpm_on && !l3fwd_em_on) {
673 printf("%s\n", str13);
677 * ipv6 and hash flags are valid only for
678 * exact macth, reset them to default for
679 * longest-prefix match.
683 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
687 argv[optind-1] = prgname;
690 optind = 1; /* reset getopt lib */
695 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
697 char buf[ETHER_ADDR_FMT_SIZE];
698 ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
699 printf("%s%s", name, buf);
703 init_mem(unsigned nb_mbuf)
705 struct lcore_conf *qconf;
710 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
711 if (rte_lcore_is_enabled(lcore_id) == 0)
715 socketid = rte_lcore_to_socket_id(lcore_id);
719 if (socketid >= NB_SOCKETS) {
720 rte_exit(EXIT_FAILURE,
721 "Socket %d of lcore %u is out of range %d\n",
722 socketid, lcore_id, NB_SOCKETS);
725 if (pktmbuf_pool[socketid] == NULL) {
726 snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
727 pktmbuf_pool[socketid] =
728 rte_pktmbuf_pool_create(s, nb_mbuf,
729 MEMPOOL_CACHE_SIZE, 0,
730 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
731 if (pktmbuf_pool[socketid] == NULL)
732 rte_exit(EXIT_FAILURE,
733 "Cannot init mbuf pool on socket %d\n",
736 printf("Allocated mbuf pool on socket %d\n",
739 /* Setup either LPM or EM(f.e Hash). */
740 l3fwd_lkp.setup(socketid);
742 qconf = &lcore_conf[lcore_id];
743 qconf->ipv4_lookup_struct =
744 l3fwd_lkp.get_ipv4_lookup_struct(socketid);
745 qconf->ipv6_lookup_struct =
746 l3fwd_lkp.get_ipv6_lookup_struct(socketid);
751 /* Check the link status of all ports in up to 9s, and print them finally */
753 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
755 #define CHECK_INTERVAL 100 /* 100ms */
756 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
758 uint8_t 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)
778 "Port%d Link Up. Speed %u Mbps -%s\n",
779 portid, link.link_speed,
780 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
781 ("full-duplex") : ("half-duplex\n"));
783 printf("Port %d Link Down\n", portid);
786 /* clear all_ports_up flag if any link down */
787 if (link.link_status == ETH_LINK_DOWN) {
792 /* after finally printing all link status, get out */
796 if (all_ports_up == 0) {
799 rte_delay_ms(CHECK_INTERVAL);
802 /* set the print_flag if all ports up or timeout */
803 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
811 signal_handler(int signum)
813 if (signum == SIGINT || signum == SIGTERM) {
814 printf("\n\nSignal %d received, preparing to exit...\n",
821 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
824 printf("Port %d: softly parse packet type info\n", portid);
825 if (rte_eth_add_rx_callback(portid, queueid,
826 l3fwd_lkp.cb_parse_ptype,
830 printf("Failed to add rx callback: port=%d\n", portid);
834 if (l3fwd_lkp.check_ptype(portid))
837 printf("port %d cannot parse packet type, please add --%s\n",
838 portid, CMD_LINE_OPT_PARSE_PTYPE);
843 main(int argc, char **argv)
845 struct lcore_conf *qconf;
846 struct rte_eth_dev_info dev_info;
847 struct rte_eth_txconf *txconf;
850 uint16_t queueid, portid;
852 uint32_t n_tx_queue, nb_lcores;
853 uint8_t nb_rx_queue, queue, socketid;
856 ret = rte_eal_init(argc, argv);
858 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
863 signal(SIGINT, signal_handler);
864 signal(SIGTERM, signal_handler);
866 /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
867 for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
868 dest_eth_addr[portid] =
869 ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
870 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
873 /* parse application arguments (after the EAL ones) */
874 ret = parse_args(argc, argv);
876 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
878 if (check_lcore_params() < 0)
879 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
881 ret = init_lcore_rx_queues();
883 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
885 nb_ports = rte_eth_dev_count();
887 if (check_port_config(nb_ports) < 0)
888 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
890 nb_lcores = rte_lcore_count();
892 /* Setup function pointers for lookup method. */
893 setup_l3fwd_lookup_tables();
895 /* initialize all ports */
896 for (portid = 0; portid < nb_ports; portid++) {
897 /* skip ports that are not enabled */
898 if ((enabled_port_mask & (1 << portid)) == 0) {
899 printf("\nSkipping disabled port %d\n", portid);
904 printf("Initializing port %d ... ", portid );
907 nb_rx_queue = get_port_n_rx_queues(portid);
908 n_tx_queue = nb_lcores;
909 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
910 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
911 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
912 nb_rx_queue, (unsigned)n_tx_queue );
913 ret = rte_eth_dev_configure(portid, nb_rx_queue,
914 (uint16_t)n_tx_queue, &port_conf);
916 rte_exit(EXIT_FAILURE,
917 "Cannot configure device: err=%d, port=%d\n",
920 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
923 rte_exit(EXIT_FAILURE,
924 "Cannot adjust number of descriptors: err=%d, "
925 "port=%d\n", ret, portid);
927 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
928 print_ethaddr(" Address:", &ports_eth_addr[portid]);
930 print_ethaddr("Destination:",
931 (const struct ether_addr *)&dest_eth_addr[portid]);
935 * prepare src MACs for each port.
937 ether_addr_copy(&ports_eth_addr[portid],
938 (struct ether_addr *)(val_eth + portid) + 1);
941 ret = init_mem(NB_MBUF);
943 rte_exit(EXIT_FAILURE, "init_mem failed\n");
945 /* init one TX queue per couple (lcore,port) */
947 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
948 if (rte_lcore_is_enabled(lcore_id) == 0)
953 (uint8_t)rte_lcore_to_socket_id(lcore_id);
957 printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
960 rte_eth_dev_info_get(portid, &dev_info);
961 txconf = &dev_info.default_txconf;
962 if (port_conf.rxmode.jumbo_frame)
963 txconf->txq_flags = 0;
964 ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
967 rte_exit(EXIT_FAILURE,
968 "rte_eth_tx_queue_setup: err=%d, "
969 "port=%d\n", ret, portid);
971 qconf = &lcore_conf[lcore_id];
972 qconf->tx_queue_id[portid] = queueid;
975 qconf->tx_port_id[qconf->n_tx_port] = portid;
981 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
982 if (rte_lcore_is_enabled(lcore_id) == 0)
984 qconf = &lcore_conf[lcore_id];
985 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
988 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
989 portid = qconf->rx_queue_list[queue].port_id;
990 queueid = qconf->rx_queue_list[queue].queue_id;
994 (uint8_t)rte_lcore_to_socket_id(lcore_id);
998 printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1001 ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1004 pktmbuf_pool[socketid]);
1006 rte_exit(EXIT_FAILURE,
1007 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
1015 for (portid = 0; portid < nb_ports; portid++) {
1016 if ((enabled_port_mask & (1 << portid)) == 0) {
1020 ret = rte_eth_dev_start(portid);
1022 rte_exit(EXIT_FAILURE,
1023 "rte_eth_dev_start: err=%d, port=%d\n",
1027 * If enabled, put device in promiscuous mode.
1028 * This allows IO forwarding mode to forward packets
1029 * to itself through 2 cross-connected ports of the
1033 rte_eth_promiscuous_enable(portid);
1038 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1039 if (rte_lcore_is_enabled(lcore_id) == 0)
1041 qconf = &lcore_conf[lcore_id];
1042 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1043 portid = qconf->rx_queue_list[queue].port_id;
1044 queueid = qconf->rx_queue_list[queue].queue_id;
1045 if (prepare_ptype_parser(portid, queueid) == 0)
1046 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1051 check_all_ports_link_status(nb_ports, enabled_port_mask);
1054 /* launch per-lcore init on every lcore */
1055 rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MASTER);
1056 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1057 if (rte_eal_wait_lcore(lcore_id) < 0) {
1064 for (portid = 0; portid < nb_ports; portid++) {
1065 if ((enabled_port_mask & (1 << portid)) == 0)
1067 printf("Closing port %d...", portid);
1068 rte_eth_dev_stop(portid);
1069 rte_eth_dev_close(portid);