ethdev: update VMDq/DCB support
[dpdk.git] / examples / l2fwd / main.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without 
8  *   modification, are permitted provided that the following conditions 
9  *   are met:
10  * 
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 
16  *       distribution.
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.
20  * 
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.
32  * 
33  */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <setjmp.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <getopt.h>
48
49 #include <rte_common.h>
50 #include <rte_log.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_launch.h>
58 #include <rte_atomic.h>
59 #include <rte_cycles.h>
60 #include <rte_prefetch.h>
61 #include <rte_lcore.h>
62 #include <rte_per_lcore.h>
63 #include <rte_branch_prediction.h>
64 #include <rte_interrupts.h>
65 #include <rte_pci.h>
66 #include <rte_random.h>
67 #include <rte_debug.h>
68 #include <rte_ether.h>
69 #include <rte_ethdev.h>
70 #include <rte_ring.h>
71 #include <rte_mempool.h>
72 #include <rte_mbuf.h>
73
74 #include "main.h"
75
76 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
77
78 #define L2FWD_MAX_PORTS 32
79
80 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
81 #define NB_MBUF   8192
82
83 /*
84  * RX and TX Prefetch, Host, and Write-back threshold values should be
85  * carefully set for optimal performance. Consult the network
86  * controller's datasheet and supporting DPDK documentation for guidance
87  * on how these parameters should be set.
88  */
89 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
90 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
91 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
92
93 /*
94  * These default values are optimized for use with the Intel(R) 82599 10 GbE
95  * Controller and the DPDK ixgbe PMD. Consider using other values for other
96  * network controllers and/or network drivers.
97  */
98 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
99 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
100 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
101
102 #define MAX_PKT_BURST 32
103 #define BURST_TX_DRAIN 200000ULL /* around 100us at 2 Ghz */
104
105 #define SOCKET0 0
106
107 /*
108  * Configurable number of RX/TX ring descriptors
109  */
110 #define RTE_TEST_RX_DESC_DEFAULT 128
111 #define RTE_TEST_TX_DESC_DEFAULT 512
112 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
113 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
114
115 /* ethernet addresses of ports */
116 static struct ether_addr l2fwd_ports_eth_addr[L2FWD_MAX_PORTS];
117
118 /* mask of enabled ports */
119 static uint32_t l2fwd_enabled_port_mask = 0;
120
121 /* list of enabled ports */
122 static uint32_t l2fwd_dst_ports[L2FWD_MAX_PORTS];
123
124 static unsigned int l2fwd_rx_queue_per_lcore = 1;
125
126 struct mbuf_table {
127         unsigned len;
128         struct rte_mbuf *m_table[MAX_PKT_BURST];
129 };
130
131 #define MAX_RX_QUEUE_PER_LCORE 16
132 #define MAX_TX_QUEUE_PER_PORT 16
133 struct lcore_queue_conf {
134         unsigned n_rx_port;
135         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
136         struct mbuf_table tx_mbufs[L2FWD_MAX_PORTS];
137
138 } __rte_cache_aligned;
139 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
140
141 static const struct rte_eth_conf port_conf = {
142         .rxmode = {
143                 .split_hdr_size = 0,
144                 .header_split   = 0, /**< Header Split disabled */
145                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
146                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
147                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
148                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
149         },
150         .txmode = {
151                 .mq_mode = ETH_MQ_TX_NONE,
152         },
153 };
154
155 static const struct rte_eth_rxconf rx_conf = {
156         .rx_thresh = {
157                 .pthresh = RX_PTHRESH,
158                 .hthresh = RX_HTHRESH,
159                 .wthresh = RX_WTHRESH,
160         },
161 };
162
163 static const struct rte_eth_txconf tx_conf = {
164         .tx_thresh = {
165                 .pthresh = TX_PTHRESH,
166                 .hthresh = TX_HTHRESH,
167                 .wthresh = TX_WTHRESH,
168         },
169         .tx_free_thresh = 0, /* Use PMD default values */
170         .tx_rs_thresh = 0, /* Use PMD default values */
171 };
172
173 struct rte_mempool * l2fwd_pktmbuf_pool = NULL;
174
175 /* Per-port statistics struct */
176 struct l2fwd_port_statistics {
177         uint64_t tx;
178         uint64_t rx;
179         uint64_t dropped;
180 } __rte_cache_aligned;
181 struct l2fwd_port_statistics port_statistics[L2FWD_MAX_PORTS];
182
183 /* A tsc-based timer responsible for triggering statistics printout */
184 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
185 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
186 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
187
188 /* Print out statistics on packets dropped */
189 static void
190 print_stats(void)
191 {
192         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
193         unsigned portid;
194
195         total_packets_dropped = 0;
196         total_packets_tx = 0;
197         total_packets_rx = 0;
198
199         const char clr[] = { 27, '[', '2', 'J', '\0' };
200         const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
201
202                 /* Clear screen and move to top left */
203         printf("%s%s", clr, topLeft);
204
205         printf("\nPort statistics ====================================");
206
207         for (portid = 0; portid < L2FWD_MAX_PORTS; portid++) {
208                 /* skip disabled ports */
209                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
210                         continue;
211                 printf("\nStatistics for port %u ------------------------------"
212                            "\nPackets sent: %24"PRIu64
213                            "\nPackets received: %20"PRIu64
214                            "\nPackets dropped: %21"PRIu64,
215                            portid,
216                            port_statistics[portid].tx,
217                            port_statistics[portid].rx,
218                            port_statistics[portid].dropped);
219
220                 total_packets_dropped += port_statistics[portid].dropped;
221                 total_packets_tx += port_statistics[portid].tx;
222                 total_packets_rx += port_statistics[portid].rx;
223         }
224         printf("\nAggregate statistics ==============================="
225                    "\nTotal packets sent: %18"PRIu64
226                    "\nTotal packets received: %14"PRIu64
227                    "\nTotal packets dropped: %15"PRIu64,
228                    total_packets_tx,
229                    total_packets_rx,
230                    total_packets_dropped);
231         printf("\n====================================================\n");
232 }
233
234 /* Send the packet on an output interface */
235 static int
236 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n, uint8_t port)
237 {
238         struct rte_mbuf **m_table;
239         unsigned ret;
240         unsigned queueid =0;
241
242         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
243
244         ret = rte_eth_tx_burst(port, (uint16_t) queueid, m_table, (uint16_t) n);
245         port_statistics[port].tx += ret;
246         if (unlikely(ret < n)) {
247                 port_statistics[port].dropped += (n - ret);
248                 do {
249                         rte_pktmbuf_free(m_table[ret]);
250                 } while (++ret < n);
251         }
252
253         return 0;
254 }
255
256 /* Send the packet on an output interface */
257 static int
258 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
259 {
260         unsigned lcore_id, len;
261         struct lcore_queue_conf *qconf;
262
263         lcore_id = rte_lcore_id();
264
265         qconf = &lcore_queue_conf[lcore_id];
266         len = qconf->tx_mbufs[port].len;
267         qconf->tx_mbufs[port].m_table[len] = m;
268         len++;
269
270         /* enough pkts to be sent */
271         if (unlikely(len == MAX_PKT_BURST)) {
272                 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
273                 len = 0;
274         }
275
276         qconf->tx_mbufs[port].len = len;
277         return 0;
278 }
279
280 static void
281 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
282 {
283         struct ether_hdr *eth;
284         void *tmp;
285         unsigned dst_port;
286
287         dst_port = l2fwd_dst_ports[portid];
288         eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
289
290         /* 02:00:00:00:00:xx */
291         tmp = &eth->d_addr.addr_bytes[0];
292         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
293
294         /* src addr */
295         ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
296
297         l2fwd_send_packet(m, (uint8_t) dst_port);
298 }
299
300 /* main processing loop */
301 static void
302 l2fwd_main_loop(void)
303 {
304         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
305         struct rte_mbuf *m;
306         unsigned lcore_id;
307         uint64_t prev_tsc = 0;
308         uint64_t diff_tsc, cur_tsc, timer_tsc;
309         unsigned i, j, portid, nb_rx;
310         struct lcore_queue_conf *qconf;
311
312         timer_tsc = 0;
313
314         lcore_id = rte_lcore_id();
315         qconf = &lcore_queue_conf[lcore_id];
316
317         if (qconf->n_rx_port == 0) {
318                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
319                 while(1);
320         }
321
322         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
323
324         for (i = 0; i < qconf->n_rx_port; i++) {
325
326                 portid = qconf->rx_port_list[i];
327                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
328                         portid);
329         }
330
331         while (1) {
332
333                 cur_tsc = rte_rdtsc();
334
335                 /*
336                  * TX burst queue drain
337                  */
338                 diff_tsc = cur_tsc - prev_tsc;
339                 if (unlikely(diff_tsc > BURST_TX_DRAIN)) {
340
341                         for (portid = 0; portid < L2FWD_MAX_PORTS; portid++) {
342                                 if (qconf->tx_mbufs[portid].len == 0)
343                                         continue;
344                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
345                                                  qconf->tx_mbufs[portid].len,
346                                                  (uint8_t) portid);
347                                 qconf->tx_mbufs[portid].len = 0;
348                         }
349
350                         /* if timer is enabled */
351                         if (timer_period > 0) {
352
353                                 /* advance the timer */
354                                 timer_tsc += diff_tsc;
355
356                                 /* if timer has reached its timeout */
357                                 if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
358
359                                         /* do this only on master core */
360                                         if (lcore_id == rte_get_master_lcore()) {
361                                                 print_stats();
362                                                 /* reset the timer */
363                                                 timer_tsc = 0;
364                                         }
365                                 }
366                         }
367
368                         prev_tsc = cur_tsc;
369                 }
370
371                 /*
372                  * Read packet from RX queues
373                  */
374                 for (i = 0; i < qconf->n_rx_port; i++) {
375
376                         portid = qconf->rx_port_list[i];
377                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
378                                                  pkts_burst, MAX_PKT_BURST);
379
380                         port_statistics[portid].rx += nb_rx;
381
382                         for (j = 0; j < nb_rx; j++) {
383                                 m = pkts_burst[j];
384                                 rte_prefetch0(rte_pktmbuf_mtod(m, void *));
385                                 l2fwd_simple_forward(m, portid);
386                         }
387                 }
388         }
389 }
390
391 static int
392 l2fwd_launch_one_lcore(__attribute__((unused)) void *dummy)
393 {
394         l2fwd_main_loop();
395         return 0;
396 }
397
398 /* display usage */
399 static void
400 l2fwd_usage(const char *prgname)
401 {
402         printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
403                "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
404                "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
405                    "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
406                prgname);
407 }
408
409 static int
410 l2fwd_parse_portmask(const char *portmask)
411 {
412         char *end = NULL;
413         unsigned long pm;
414
415         /* parse hexadecimal string */
416         pm = strtoul(portmask, &end, 16);
417         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
418                 return -1;
419
420         if (pm == 0)
421                 return -1;
422
423         return pm;
424 }
425
426 static unsigned int
427 l2fwd_parse_nqueue(const char *q_arg)
428 {
429         char *end = NULL;
430         unsigned long n;
431
432         /* parse hexadecimal string */
433         n = strtoul(q_arg, &end, 10);
434         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
435                 return 0;
436         if (n == 0)
437                 return 0;
438         if (n >= MAX_RX_QUEUE_PER_LCORE)
439                 return 0;
440
441         return n;
442 }
443
444 static int
445 l2fwd_parse_timer_period(const char *q_arg)
446 {
447         char *end = NULL;
448         int n;
449
450         /* parse number string */
451         n = strtol(q_arg, &end, 10);
452         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
453                 return -1;
454         if (n >= MAX_TIMER_PERIOD)
455                 return -1;
456
457         return n;
458 }
459
460 /* Parse the argument given in the command line of the application */
461 static int
462 l2fwd_parse_args(int argc, char **argv)
463 {
464         int opt, ret;
465         char **argvopt;
466         int option_index;
467         char *prgname = argv[0];
468         static struct option lgopts[] = {
469                 {NULL, 0, 0, 0}
470         };
471
472         argvopt = argv;
473
474         while ((opt = getopt_long(argc, argvopt, "p:q:T:",
475                                   lgopts, &option_index)) != EOF) {
476
477                 switch (opt) {
478                 /* portmask */
479                 case 'p':
480                         l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
481                         if (l2fwd_enabled_port_mask == 0) {
482                                 printf("invalid portmask\n");
483                                 l2fwd_usage(prgname);
484                                 return -1;
485                         }
486                         break;
487
488                 /* nqueue */
489                 case 'q':
490                         l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
491                         if (l2fwd_rx_queue_per_lcore == 0) {
492                                 printf("invalid queue number\n");
493                                 l2fwd_usage(prgname);
494                                 return -1;
495                         }
496                         break;
497
498                 /* timer period */
499                 case 'T':
500                         timer_period = l2fwd_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
501                         if (timer_period < 0) {
502                                 printf("invalid timer period\n");
503                                 l2fwd_usage(prgname);
504                                 return -1;
505                         }
506                         break;
507
508                 /* long options */
509                 case 0:
510                         l2fwd_usage(prgname);
511                         return -1;
512
513                 default:
514                         l2fwd_usage(prgname);
515                         return -1;
516                 }
517         }
518
519         if (optind >= 0)
520                 argv[optind-1] = prgname;
521
522         ret = optind-1;
523         optind = 0; /* reset getopt lib */
524         return ret;
525 }
526
527 /* Check the link status of all ports in up to 9s, and print them finally */
528 static void
529 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
530 {
531 #define CHECK_INTERVAL 100 /* 100ms */
532 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
533         uint8_t portid, count, all_ports_up, print_flag = 0;
534         struct rte_eth_link link;
535
536         printf("\nChecking link status");
537         fflush(stdout);
538         for (count = 0; count <= MAX_CHECK_TIME; count++) {
539                 all_ports_up = 1;
540                 for (portid = 0; portid < port_num; portid++) {
541                         if ((port_mask & (1 << portid)) == 0)
542                                 continue;
543                         memset(&link, 0, sizeof(link));
544                         rte_eth_link_get_nowait(portid, &link);
545                         /* print link status if flag set */
546                         if (print_flag == 1) {
547                                 if (link.link_status)
548                                         printf("Port %d Link Up - speed %u "
549                                                 "Mbps - %s\n", (uint8_t)portid,
550                                                 (unsigned)link.link_speed,
551                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
552                                         ("full-duplex") : ("half-duplex\n"));
553                                 else
554                                         printf("Port %d Link Down\n",
555                                                 (uint8_t)portid);
556                                 continue;
557                         }
558                         /* clear all_ports_up flag if any link down */
559                         if (link.link_status == 0) {
560                                 all_ports_up = 0;
561                                 break;
562                         }
563                 }
564                 /* after finally printing all link status, get out */
565                 if (print_flag == 1)
566                         break;
567
568                 if (all_ports_up == 0) {
569                         printf(".");
570                         fflush(stdout);
571                         rte_delay_ms(CHECK_INTERVAL);
572                 }
573
574                 /* set the print_flag if all ports up or timeout */
575                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
576                         print_flag = 1;
577                         printf("done\n");
578                 }
579         }
580 }
581
582 int
583 MAIN(int argc, char **argv)
584 {
585         struct lcore_queue_conf *qconf;
586         struct rte_eth_dev_info dev_info;
587         int ret;
588         uint8_t nb_ports;
589         uint8_t portid, last_port;
590         unsigned lcore_id, rx_lcore_id;
591         unsigned nb_ports_in_mask = 0;
592
593         /* init EAL */
594         ret = rte_eal_init(argc, argv);
595         if (ret < 0)
596                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
597         argc -= ret;
598         argv += ret;
599
600         /* parse application arguments (after the EAL ones) */
601         ret = l2fwd_parse_args(argc, argv);
602         if (ret < 0)
603                 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
604
605         /* create the mbuf pool */
606         l2fwd_pktmbuf_pool =
607                 rte_mempool_create("mbuf_pool", NB_MBUF,
608                                    MBUF_SIZE, 32,
609                                    sizeof(struct rte_pktmbuf_pool_private),
610                                    rte_pktmbuf_pool_init, NULL,
611                                    rte_pktmbuf_init, NULL,
612                                    SOCKET0, 0);
613         if (l2fwd_pktmbuf_pool == NULL)
614                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
615
616         /* init driver(s) */
617         if (rte_pmd_init_all() < 0)
618                 rte_exit(EXIT_FAILURE, "Cannot init pmd\n");
619
620         if (rte_eal_pci_probe() < 0)
621                 rte_exit(EXIT_FAILURE, "Cannot probe PCI\n");
622
623         nb_ports = rte_eth_dev_count();
624         if (nb_ports == 0)
625                 rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
626
627         if (nb_ports > L2FWD_MAX_PORTS)
628                 nb_ports = L2FWD_MAX_PORTS;
629
630         /* reset l2fwd_dst_ports */
631         for (portid = 0; portid < L2FWD_MAX_PORTS; portid++)
632                 l2fwd_dst_ports[portid] = 0;
633         last_port = 0;
634
635         /*
636          * Each logical core is assigned a dedicated TX queue on each port.
637          */
638         for (portid = 0; portid < nb_ports; portid++) {
639                 /* skip ports that are not enabled */
640                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
641                         continue;
642
643                 if (nb_ports_in_mask % 2) {
644                         l2fwd_dst_ports[portid] = last_port;
645                         l2fwd_dst_ports[last_port] = portid;
646                 }
647                 else
648                         last_port = portid;
649
650                 nb_ports_in_mask++;
651
652                 rte_eth_dev_info_get(portid, &dev_info);
653         }
654         if (nb_ports_in_mask < 2 || nb_ports_in_mask % 2) {
655                 printf("Notice: odd number of ports in portmask.\n");
656                 l2fwd_dst_ports[last_port] = last_port;
657         }
658
659         rx_lcore_id = 0;
660         qconf = NULL;
661
662         /* Initialize the port/queue configuration of each logical core */
663         for (portid = 0; portid < nb_ports; portid++) {
664                 /* skip ports that are not enabled */
665                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
666                         continue;
667
668                 /* get the lcore_id for this port */
669                 while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
670                        lcore_queue_conf[rx_lcore_id].n_rx_port ==
671                        l2fwd_rx_queue_per_lcore) {
672                         rx_lcore_id++;
673                         if (rx_lcore_id >= RTE_MAX_LCORE)
674                                 rte_exit(EXIT_FAILURE, "Not enough cores\n");
675                 }
676
677                 if (qconf != &lcore_queue_conf[rx_lcore_id])
678                         /* Assigned a new logical core in the loop above. */
679                         qconf = &lcore_queue_conf[rx_lcore_id];
680
681                 qconf->rx_port_list[qconf->n_rx_port] = portid;
682                 qconf->n_rx_port++;
683                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid);
684         }
685
686         /* Initialise each port */
687         for (portid = 0; portid < nb_ports; portid++) {
688                 /* skip ports that are not enabled */
689                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
690                         printf("Skipping disabled port %u\n", (unsigned) portid);
691                         continue;
692                 }
693                 /* init port */
694                 printf("Initializing port %u... ", (unsigned) portid);
695                 fflush(stdout);
696                 ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
697                 if (ret < 0)
698                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
699                                   ret, (unsigned) portid);
700
701                 rte_eth_macaddr_get(portid,&l2fwd_ports_eth_addr[portid]);
702
703                 /* init one RX queue */
704                 fflush(stdout);
705                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
706                                              rte_eth_dev_socket_id(portid), &rx_conf,
707                                              l2fwd_pktmbuf_pool);
708                 if (ret < 0)
709                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
710                                   ret, (unsigned) portid);
711
712                 /* init one TX queue logical core on each port */
713                 fflush(stdout);
714                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
715                                 SOCKET0, &tx_conf);
716                 if (ret < 0)
717                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
718                                 ret, (unsigned) portid);
719
720                 /* Start device */
721                 ret = rte_eth_dev_start(portid);
722                 if (ret < 0)
723                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
724                                   ret, (unsigned) portid);
725
726                 printf("done: \n");
727
728                 rte_eth_promiscuous_enable(portid);
729
730                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
731                                 (unsigned) portid,
732                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
733                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
734                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
735                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
736                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
737                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
738
739                 /* initialize port stats */
740                 memset(&port_statistics, 0, sizeof(port_statistics));
741         }
742
743         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
744
745         /* launch per-lcore init on every lcore */
746         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MASTER);
747         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
748                 if (rte_eal_wait_lcore(lcore_id) < 0)
749                         return -1;
750         }
751
752         return 0;
753 }
754