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