examples/l3fwd-graph: add ethdev configuration changes
[dpdk.git] / examples / l3fwd-graph / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4
5 #include <arpa/inet.h>
6 #include <errno.h>
7 #include <getopt.h>
8 #include <inttypes.h>
9 #include <signal.h>
10 #include <stdarg.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <unistd.h>
20
21 #include <rte_branch_prediction.h>
22 #include <rte_common.h>
23 #include <rte_cycles.h>
24 #include <rte_eal.h>
25 #include <rte_ethdev.h>
26 #include <rte_lcore.h>
27 #include <rte_log.h>
28 #include <rte_mempool.h>
29 #include <rte_per_lcore.h>
30 #include <rte_string_fns.h>
31 #include <rte_vect.h>
32
33 #include <cmdline_parse.h>
34 #include <cmdline_parse_etheraddr.h>
35
36 /* Log type */
37 #define RTE_LOGTYPE_L3FWD_GRAPH RTE_LOGTYPE_USER1
38
39 /*
40  * Configurable number of RX/TX ring descriptors
41  */
42 #define RTE_TEST_RX_DESC_DEFAULT 1024
43 #define RTE_TEST_TX_DESC_DEFAULT 1024
44
45 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
46 #define MAX_RX_QUEUE_PER_PORT 128
47
48 #define MAX_RX_QUEUE_PER_LCORE 16
49
50 #define MAX_LCORE_PARAMS 1024
51
52 #define NB_SOCKETS 8
53
54 /* Static global variables used within this file. */
55 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
56 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
57
58 /**< Ports set in promiscuous mode off by default. */
59 static int promiscuous_on;
60
61 static int numa_on = 1;   /**< NUMA is enabled by default. */
62 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
63                           /**< by default */
64
65 static volatile bool force_quit;
66
67 /* Ethernet addresses of ports */
68 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
69 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
70 xmm_t val_eth[RTE_MAX_ETHPORTS];
71
72 /* Mask of enabled ports */
73 static uint32_t enabled_port_mask;
74
75 struct lcore_rx_queue {
76         uint16_t port_id;
77         uint8_t queue_id;
78 };
79
80 /* Lcore conf */
81 struct lcore_conf {
82         uint16_t n_rx_queue;
83         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
84 } __rte_cache_aligned;
85
86 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
87
88 struct lcore_params {
89         uint16_t port_id;
90         uint8_t queue_id;
91         uint8_t lcore_id;
92 } __rte_cache_aligned;
93
94 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
95 static struct lcore_params lcore_params_array_default[] = {
96         {0, 0, 2}, {0, 1, 2}, {0, 2, 2}, {1, 0, 2}, {1, 1, 2},
97         {1, 2, 2}, {2, 0, 2}, {3, 0, 3}, {3, 1, 3},
98 };
99
100 static struct lcore_params *lcore_params = lcore_params_array_default;
101 static uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
102
103 static struct rte_eth_conf port_conf = {
104         .rxmode = {
105                 .mq_mode = ETH_MQ_RX_RSS,
106                 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
107                 .split_hdr_size = 0,
108         },
109         .rx_adv_conf = {
110                 .rss_conf = {
111                                 .rss_key = NULL,
112                                 .rss_hf = ETH_RSS_IP,
113                 },
114         },
115         .txmode = {
116                 .mq_mode = ETH_MQ_TX_NONE,
117         },
118 };
119
120 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
121
122 static int
123 check_lcore_params(void)
124 {
125         uint8_t queue, lcore;
126         int socketid;
127         uint16_t i;
128
129         for (i = 0; i < nb_lcore_params; ++i) {
130                 queue = lcore_params[i].queue_id;
131                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
132                         printf("Invalid queue number: %hhu\n", queue);
133                         return -1;
134                 }
135                 lcore = lcore_params[i].lcore_id;
136                 if (!rte_lcore_is_enabled(lcore)) {
137                         printf("Error: lcore %hhu is not enabled in lcore mask\n",
138                                lcore);
139                         return -1;
140                 }
141
142                 if (lcore == rte_get_master_lcore()) {
143                         printf("Error: lcore %u is master lcore\n", lcore);
144                         return -1;
145                 }
146                 socketid = rte_lcore_to_socket_id(lcore);
147                 if ((socketid != 0) && (numa_on == 0)) {
148                         printf("Warning: lcore %hhu is on socket %d with numa off\n",
149                                lcore, socketid);
150                 }
151         }
152
153         return 0;
154 }
155
156 static int
157 check_port_config(void)
158 {
159         uint16_t portid;
160         uint16_t i;
161
162         for (i = 0; i < nb_lcore_params; ++i) {
163                 portid = lcore_params[i].port_id;
164                 if ((enabled_port_mask & (1 << portid)) == 0) {
165                         printf("Port %u is not enabled in port mask\n", portid);
166                         return -1;
167                 }
168                 if (!rte_eth_dev_is_valid_port(portid)) {
169                         printf("Port %u is not present on the board\n", portid);
170                         return -1;
171                 }
172         }
173
174         return 0;
175 }
176
177 static uint8_t
178 get_port_n_rx_queues(const uint16_t port)
179 {
180         int queue = -1;
181         uint16_t i;
182
183         for (i = 0; i < nb_lcore_params; ++i) {
184                 if (lcore_params[i].port_id == port) {
185                         if (lcore_params[i].queue_id == queue + 1)
186                                 queue = lcore_params[i].queue_id;
187                         else
188                                 rte_exit(EXIT_FAILURE,
189                                          "Queue ids of the port %d must be"
190                                          " in sequence and must start with 0\n",
191                                          lcore_params[i].port_id);
192                 }
193         }
194
195         return (uint8_t)(++queue);
196 }
197
198 static int
199 init_lcore_rx_queues(void)
200 {
201         uint16_t i, nb_rx_queue;
202         uint8_t lcore;
203
204         for (i = 0; i < nb_lcore_params; ++i) {
205                 lcore = lcore_params[i].lcore_id;
206                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
207                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
208                         printf("Error: too many queues (%u) for lcore: %u\n",
209                                (unsigned int)nb_rx_queue + 1,
210                                (unsigned int)lcore);
211                         return -1;
212                 }
213
214                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
215                         lcore_params[i].port_id;
216                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
217                         lcore_params[i].queue_id;
218                 lcore_conf[lcore].n_rx_queue++;
219         }
220
221         return 0;
222 }
223
224 /* Display usage */
225 static void
226 print_usage(const char *prgname)
227 {
228         fprintf(stderr,
229                 "%s [EAL options] --"
230                 " -p PORTMASK"
231                 " [-P]"
232                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
233                 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
234                 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
235                 " [--no-numa]"
236                 " [--per-port-pool]\n\n"
237
238                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
239                 "  -P : Enable promiscuous mode\n"
240                 "  --config (port,queue,lcore): Rx queue configuration\n"
241                 "  --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for "
242                 "port X\n"
243                 "  --enable-jumbo: Enable jumbo frames\n"
244                 "  --max-pkt-len: Under the premise of enabling jumbo,\n"
245                 "                 maximum packet length in decimal (64-9600)\n"
246                 "  --no-numa: Disable numa awareness\n"
247                 "  --per-port-pool: Use separate buffer pool per port\n\n",
248                 prgname);
249 }
250
251 static int
252 parse_max_pkt_len(const char *pktlen)
253 {
254         unsigned long len;
255         char *end = NULL;
256
257         /* Parse decimal string */
258         len = strtoul(pktlen, &end, 10);
259         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
260                 return -1;
261
262         if (len == 0)
263                 return -1;
264
265         return len;
266 }
267
268 static int
269 parse_portmask(const char *portmask)
270 {
271         char *end = NULL;
272         unsigned long pm;
273
274         /* Parse hexadecimal string */
275         pm = strtoul(portmask, &end, 16);
276         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
277                 return -1;
278
279         if (pm == 0)
280                 return -1;
281
282         return pm;
283 }
284
285 static int
286 parse_config(const char *q_arg)
287 {
288         enum fieldnames { FLD_PORT = 0, FLD_QUEUE, FLD_LCORE, _NUM_FLD };
289         unsigned long int_fld[_NUM_FLD];
290         const char *p, *p0 = q_arg;
291         char *str_fld[_NUM_FLD];
292         uint32_t size;
293         char s[256];
294         char *end;
295         int i;
296
297         nb_lcore_params = 0;
298
299         while ((p = strchr(p0, '(')) != NULL) {
300                 ++p;
301                 p0 = strchr(p, ')');
302                 if (p0 == NULL)
303                         return -1;
304
305                 size = p0 - p;
306                 if (size >= sizeof(s))
307                         return -1;
308
309                 memcpy(s, p, size);
310                 s[size] = '\0';
311                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
312                     _NUM_FLD)
313                         return -1;
314                 for (i = 0; i < _NUM_FLD; i++) {
315                         errno = 0;
316                         int_fld[i] = strtoul(str_fld[i], &end, 0);
317                         if (errno != 0 || end == str_fld[i])
318                                 return -1;
319                 }
320
321                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
322                         printf("Exceeded max number of lcore params: %hu\n",
323                                nb_lcore_params);
324                         return -1;
325                 }
326
327                 if (int_fld[FLD_PORT] >= RTE_MAX_ETHPORTS ||
328                     int_fld[FLD_LCORE] >= RTE_MAX_LCORE) {
329                         printf("Invalid port/lcore id\n");
330                         return -1;
331                 }
332
333                 lcore_params_array[nb_lcore_params].port_id =
334                         (uint8_t)int_fld[FLD_PORT];
335                 lcore_params_array[nb_lcore_params].queue_id =
336                         (uint8_t)int_fld[FLD_QUEUE];
337                 lcore_params_array[nb_lcore_params].lcore_id =
338                         (uint8_t)int_fld[FLD_LCORE];
339                 ++nb_lcore_params;
340         }
341         lcore_params = lcore_params_array;
342
343         return 0;
344 }
345
346 static void
347 parse_eth_dest(const char *optarg)
348 {
349         uint8_t c, *dest, peer_addr[6];
350         uint16_t portid;
351         char *port_end;
352
353         errno = 0;
354         portid = strtoul(optarg, &port_end, 10);
355         if (errno != 0 || port_end == optarg || *port_end++ != ',')
356                 rte_exit(EXIT_FAILURE, "Invalid eth-dest: %s", optarg);
357         if (portid >= RTE_MAX_ETHPORTS)
358                 rte_exit(EXIT_FAILURE,
359                          "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n", portid,
360                          RTE_MAX_ETHPORTS);
361
362         if (cmdline_parse_etheraddr(NULL, port_end, &peer_addr,
363                                     sizeof(peer_addr)) < 0)
364                 rte_exit(EXIT_FAILURE, "Invalid ethernet address: %s\n",
365                          port_end);
366         dest = (uint8_t *)&dest_eth_addr[portid];
367         for (c = 0; c < 6; c++)
368                 dest[c] = peer_addr[c];
369         *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
370 }
371
372 #define MAX_JUMBO_PKT_LEN  9600
373 #define MEMPOOL_CACHE_SIZE 256
374
375 static const char short_options[] = "p:" /* portmask */
376                                     "P"  /* promiscuous */
377         ;
378
379 #define CMD_LINE_OPT_CONFIG        "config"
380 #define CMD_LINE_OPT_ETH_DEST      "eth-dest"
381 #define CMD_LINE_OPT_NO_NUMA       "no-numa"
382 #define CMD_LINE_OPT_ENABLE_JUMBO  "enable-jumbo"
383 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
384 enum {
385         /* Long options mapped to a short option */
386
387         /* First long only option value must be >= 256, so that we won't
388          * conflict with short options
389          */
390         CMD_LINE_OPT_MIN_NUM = 256,
391         CMD_LINE_OPT_CONFIG_NUM,
392         CMD_LINE_OPT_ETH_DEST_NUM,
393         CMD_LINE_OPT_NO_NUMA_NUM,
394         CMD_LINE_OPT_ENABLE_JUMBO_NUM,
395         CMD_LINE_OPT_PARSE_PER_PORT_POOL,
396 };
397
398 static const struct option lgopts[] = {
399         {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
400         {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
401         {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
402         {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
403         {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
404         {NULL, 0, 0, 0},
405 };
406
407 /*
408  * This expression is used to calculate the number of mbufs needed
409  * depending on user input, taking  into account memory for rx and
410  * tx hardware rings, cache per lcore and mtable per port per lcore.
411  * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
412  * value of 8192
413  */
414 #define NB_MBUF(nports)                                                        \
415         RTE_MAX((nports * nb_rx_queue * nb_rxd +                               \
416                  nports * nb_lcores * RTE_GRAPH_BURST_SIZE +                   \
417                  nports * n_tx_queue * nb_txd +                                \
418                  nb_lcores * MEMPOOL_CACHE_SIZE), 8192u)
419
420 /* Parse the argument given in the command line of the application */
421 static int
422 parse_args(int argc, char **argv)
423 {
424         char *prgname = argv[0];
425         int option_index;
426         char **argvopt;
427         int opt, ret;
428
429         argvopt = argv;
430
431         /* Error or normal output strings. */
432         while ((opt = getopt_long(argc, argvopt, short_options, lgopts,
433                                   &option_index)) != EOF) {
434
435                 switch (opt) {
436                 /* Portmask */
437                 case 'p':
438                         enabled_port_mask = parse_portmask(optarg);
439                         if (enabled_port_mask == 0) {
440                                 fprintf(stderr, "Invalid portmask\n");
441                                 print_usage(prgname);
442                                 return -1;
443                         }
444                         break;
445
446                 case 'P':
447                         promiscuous_on = 1;
448                         break;
449
450                 /* Long options */
451                 case CMD_LINE_OPT_CONFIG_NUM:
452                         ret = parse_config(optarg);
453                         if (ret) {
454                                 fprintf(stderr, "Invalid config\n");
455                                 print_usage(prgname);
456                                 return -1;
457                         }
458                         break;
459
460                 case CMD_LINE_OPT_ETH_DEST_NUM:
461                         parse_eth_dest(optarg);
462                         break;
463
464                 case CMD_LINE_OPT_NO_NUMA_NUM:
465                         numa_on = 0;
466                         break;
467
468                 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
469                         const struct option lenopts = {"max-pkt-len",
470                                                        required_argument, 0, 0};
471
472                         port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
473                         port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
474
475                         /*
476                          * if no max-pkt-len set, use the default
477                          * value RTE_ETHER_MAX_LEN.
478                          */
479                         if (getopt_long(argc, argvopt, "", &lenopts,
480                                         &option_index) == 0) {
481                                 ret = parse_max_pkt_len(optarg);
482                                 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
483                                         fprintf(stderr, "Invalid maximum "
484                                                         "packet length\n");
485                                         print_usage(prgname);
486                                         return -1;
487                                 }
488                                 port_conf.rxmode.max_rx_pkt_len = ret;
489                         }
490                         break;
491                 }
492
493                 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
494                         printf("Per port buffer pool is enabled\n");
495                         per_port_pool = 1;
496                         break;
497
498                 default:
499                         print_usage(prgname);
500                         return -1;
501                 }
502         }
503
504         if (optind >= 0)
505                 argv[optind - 1] = prgname;
506         ret = optind - 1;
507         optind = 1; /* Reset getopt lib */
508
509         return ret;
510 }
511
512 static void
513 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
514 {
515         char buf[RTE_ETHER_ADDR_FMT_SIZE];
516         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
517         printf("%s%s", name, buf);
518 }
519
520 static int
521 init_mem(uint16_t portid, uint32_t nb_mbuf)
522 {
523         uint32_t lcore_id;
524         int socketid;
525         char s[64];
526
527         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
528                 if (rte_lcore_is_enabled(lcore_id) == 0)
529                         continue;
530
531                 if (numa_on)
532                         socketid = rte_lcore_to_socket_id(lcore_id);
533                 else
534                         socketid = 0;
535
536                 if (socketid >= NB_SOCKETS) {
537                         rte_exit(EXIT_FAILURE,
538                                  "Socket %d of lcore %u is out of range %d\n",
539                                  socketid, lcore_id, NB_SOCKETS);
540                 }
541
542                 if (pktmbuf_pool[portid][socketid] == NULL) {
543                         snprintf(s, sizeof(s), "mbuf_pool_%d:%d", portid,
544                                  socketid);
545                         /* Create a pool with priv size of a cacheline */
546                         pktmbuf_pool[portid][socketid] =
547                                 rte_pktmbuf_pool_create(
548                                         s, nb_mbuf, MEMPOOL_CACHE_SIZE,
549                                         RTE_CACHE_LINE_SIZE,
550                                         RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
551                         if (pktmbuf_pool[portid][socketid] == NULL)
552                                 rte_exit(EXIT_FAILURE,
553                                          "Cannot init mbuf pool on socket %d\n",
554                                          socketid);
555                         else
556                                 printf("Allocated mbuf pool on socket %d\n",
557                                        socketid);
558                 }
559         }
560
561         return 0;
562 }
563
564 /* Check the link status of all ports in up to 9s, and print them finally */
565 static void
566 check_all_ports_link_status(uint32_t port_mask)
567 {
568 #define CHECK_INTERVAL 100 /* 100ms */
569 #define MAX_CHECK_TIME 90  /* 9s (90 * 100ms) in total */
570         uint8_t count, all_ports_up, print_flag = 0;
571         struct rte_eth_link link;
572         uint16_t portid;
573
574         printf("\nChecking link status");
575         fflush(stdout);
576         for (count = 0; count <= MAX_CHECK_TIME; count++) {
577                 if (force_quit)
578                         return;
579                 all_ports_up = 1;
580                 RTE_ETH_FOREACH_DEV(portid)
581                 {
582                         if (force_quit)
583                                 return;
584                         if ((port_mask & (1 << portid)) == 0)
585                                 continue;
586                         memset(&link, 0, sizeof(link));
587                         rte_eth_link_get_nowait(portid, &link);
588                         /* Print link status if flag set */
589                         if (print_flag == 1) {
590                                 if (link.link_status)
591                                         printf("Port%d Link Up. Speed %u Mbps "
592                                                "-%s\n",
593                                                portid, link.link_speed,
594                                                (link.link_duplex ==
595                                                 ETH_LINK_FULL_DUPLEX)
596                                                        ? ("full-duplex")
597                                                        : ("half-duplex\n"));
598                                 else
599                                         printf("Port %d Link Down\n", portid);
600                                 continue;
601                         }
602                         /* Clear all_ports_up flag if any link down */
603                         if (link.link_status == ETH_LINK_DOWN) {
604                                 all_ports_up = 0;
605                                 break;
606                         }
607                 }
608                 /* After finally printing all link status, get out */
609                 if (print_flag == 1)
610                         break;
611
612                 if (all_ports_up == 0) {
613                         printf(".");
614                         fflush(stdout);
615                         rte_delay_ms(CHECK_INTERVAL);
616                 }
617
618                 /* Set the print_flag if all ports up or timeout */
619                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
620                         print_flag = 1;
621                         printf("Done\n");
622                 }
623         }
624 }
625
626 static void
627 signal_handler(int signum)
628 {
629         if (signum == SIGINT || signum == SIGTERM) {
630                 printf("\n\nSignal %d received, preparing to exit...\n",
631                        signum);
632                 force_quit = true;
633         }
634 }
635
636 int
637 main(int argc, char **argv)
638 {
639         uint8_t nb_rx_queue, queue, socketid;
640         struct rte_eth_dev_info dev_info;
641         uint32_t n_tx_queue, nb_lcores;
642         struct rte_eth_txconf *txconf;
643         uint16_t queueid, portid;
644         struct lcore_conf *qconf;
645         uint32_t lcore_id;
646         uint32_t nb_ports;
647         int ret;
648
649         /* Init EAL */
650         ret = rte_eal_init(argc, argv);
651         if (ret < 0)
652                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
653         argc -= ret;
654         argv += ret;
655
656         force_quit = false;
657         signal(SIGINT, signal_handler);
658         signal(SIGTERM, signal_handler);
659
660         /* Pre-init dst MACs for all ports to 02:00:00:00:00:xx */
661         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
662                 dest_eth_addr[portid] =
663                         RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
664                 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
665         }
666
667         /* Parse application arguments (after the EAL ones) */
668         ret = parse_args(argc, argv);
669         if (ret < 0)
670                 rte_exit(EXIT_FAILURE, "Invalid L3FWD_GRAPH parameters\n");
671
672         if (check_lcore_params() < 0)
673                 rte_exit(EXIT_FAILURE, "check_lcore_params() failed\n");
674
675         ret = init_lcore_rx_queues();
676         if (ret < 0)
677                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues() failed\n");
678
679         if (check_port_config() < 0)
680                 rte_exit(EXIT_FAILURE, "check_port_config() failed\n");
681
682         nb_ports = rte_eth_dev_count_avail();
683         nb_lcores = rte_lcore_count();
684
685         /* Initialize all ports */
686         RTE_ETH_FOREACH_DEV(portid)
687         {
688                 struct rte_eth_conf local_port_conf = port_conf;
689
690                 /* Skip ports that are not enabled */
691                 if ((enabled_port_mask & (1 << portid)) == 0) {
692                         printf("\nSkipping disabled port %d\n", portid);
693                         continue;
694                 }
695
696                 /* Init port */
697                 printf("Initializing port %d ... ", portid);
698                 fflush(stdout);
699
700                 nb_rx_queue = get_port_n_rx_queues(portid);
701                 n_tx_queue = nb_lcores;
702                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
703                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
704                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
705                        nb_rx_queue, n_tx_queue);
706
707                 rte_eth_dev_info_get(portid, &dev_info);
708                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
709                         local_port_conf.txmode.offloads |=
710                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
711
712                 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
713                         dev_info.flow_type_rss_offloads;
714                 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
715                     port_conf.rx_adv_conf.rss_conf.rss_hf) {
716                         printf("Port %u modified RSS hash function based on "
717                                "hardware support,"
718                                "requested:%#" PRIx64 " configured:%#" PRIx64
719                                "\n",
720                                portid, port_conf.rx_adv_conf.rss_conf.rss_hf,
721                                local_port_conf.rx_adv_conf.rss_conf.rss_hf);
722                 }
723
724                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
725                                             n_tx_queue, &local_port_conf);
726                 if (ret < 0)
727                         rte_exit(EXIT_FAILURE,
728                                  "Cannot configure device: err=%d, port=%d\n",
729                                  ret, portid);
730
731                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
732                                                        &nb_txd);
733                 if (ret < 0)
734                         rte_exit(EXIT_FAILURE,
735                                  "Cannot adjust number of descriptors: err=%d, "
736                                  "port=%d\n",
737                                  ret, portid);
738
739                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
740                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
741                 printf(", ");
742                 print_ethaddr(
743                         "Destination:",
744                         (const struct rte_ether_addr *)&dest_eth_addr[portid]);
745                 printf(", ");
746
747                 /*
748                  * prepare src MACs for each port.
749                  */
750                 rte_ether_addr_copy(
751                         &ports_eth_addr[portid],
752                         (struct rte_ether_addr *)(val_eth + portid) + 1);
753
754                 /* Init memory */
755                 if (!per_port_pool) {
756                         /* portid = 0; this is *not* signifying the first port,
757                          * rather, it signifies that portid is ignored.
758                          */
759                         ret = init_mem(0, NB_MBUF(nb_ports));
760                 } else {
761                         ret = init_mem(portid, NB_MBUF(1));
762                 }
763                 if (ret < 0)
764                         rte_exit(EXIT_FAILURE, "init_mem() failed\n");
765
766                 /* Init one TX queue per couple (lcore,port) */
767                 queueid = 0;
768                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
769                         if (rte_lcore_is_enabled(lcore_id) == 0)
770                                 continue;
771
772                         qconf = &lcore_conf[lcore_id];
773
774                         if (numa_on)
775                                 socketid = (uint8_t)rte_lcore_to_socket_id(
776                                         lcore_id);
777                         else
778                                 socketid = 0;
779
780                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
781                         fflush(stdout);
782
783                         txconf = &dev_info.default_txconf;
784                         txconf->offloads = local_port_conf.txmode.offloads;
785                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
786                                                      socketid, txconf);
787                         if (ret < 0)
788                                 rte_exit(EXIT_FAILURE,
789                                          "rte_eth_tx_queue_setup: err=%d, "
790                                          "port=%d\n",
791                                          ret, portid);
792                         queueid++;
793                 }
794
795                 printf("\n");
796         }
797
798         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
799                 if (rte_lcore_is_enabled(lcore_id) == 0)
800                         continue;
801                 qconf = &lcore_conf[lcore_id];
802                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
803                 fflush(stdout);
804                 /* Init RX queues */
805                 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
806                         struct rte_eth_rxconf rxq_conf;
807
808                         portid = qconf->rx_queue_list[queue].port_id;
809                         queueid = qconf->rx_queue_list[queue].queue_id;
810
811                         if (numa_on)
812                                 socketid = (uint8_t)rte_lcore_to_socket_id(
813                                         lcore_id);
814                         else
815                                 socketid = 0;
816
817                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
818                         fflush(stdout);
819
820                         rte_eth_dev_info_get(portid, &dev_info);
821                         rxq_conf = dev_info.default_rxconf;
822                         rxq_conf.offloads = port_conf.rxmode.offloads;
823                         if (!per_port_pool)
824                                 ret = rte_eth_rx_queue_setup(
825                                         portid, queueid, nb_rxd, socketid,
826                                         &rxq_conf, pktmbuf_pool[0][socketid]);
827                         else
828                                 ret = rte_eth_rx_queue_setup(
829                                         portid, queueid, nb_rxd, socketid,
830                                         &rxq_conf,
831                                         pktmbuf_pool[portid][socketid]);
832                         if (ret < 0)
833                                 rte_exit(EXIT_FAILURE,
834                                          "rte_eth_rx_queue_setup: err=%d, "
835                                          "port=%d\n",
836                                          ret, portid);
837
838                 }
839         }
840
841         printf("\n");
842
843         /* Start ports */
844         RTE_ETH_FOREACH_DEV(portid)
845         {
846                 if ((enabled_port_mask & (1 << portid)) == 0)
847                         continue;
848
849                 /* Start device */
850                 ret = rte_eth_dev_start(portid);
851                 if (ret < 0)
852                         rte_exit(EXIT_FAILURE,
853                                  "rte_eth_dev_start: err=%d, port=%d\n", ret,
854                                  portid);
855
856                 /*
857                  * If enabled, put device in promiscuous mode.
858                  * This allows IO forwarding mode to forward packets
859                  * to itself through 2 cross-connected  ports of the
860                  * target machine.
861                  */
862                 if (promiscuous_on)
863                         rte_eth_promiscuous_enable(portid);
864         }
865
866         printf("\n");
867
868         check_all_ports_link_status(enabled_port_mask);
869
870         /* Stop ports */
871         RTE_ETH_FOREACH_DEV(portid) {
872                 if ((enabled_port_mask & (1 << portid)) == 0)
873                         continue;
874                 printf("Closing port %d...", portid);
875                 rte_eth_dev_stop(portid);
876                 rte_eth_dev_close(portid);
877                 printf(" Done\n");
878         }
879         printf("Bye...\n");
880
881         return ret;
882 }