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