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