remove trailing whitespaces
[dpdk.git] / examples / l2fwd / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 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 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <sys/types.h>
40 #include <sys/queue.h>
41 #include <netinet/in.h>
42 #include <setjmp.h>
43 #include <stdarg.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include <getopt.h>
47
48 #include <rte_common.h>
49 #include <rte_log.h>
50 #include <rte_memory.h>
51 #include <rte_memcpy.h>
52 #include <rte_memzone.h>
53 #include <rte_tailq.h>
54 #include <rte_eal.h>
55 #include <rte_per_lcore.h>
56 #include <rte_launch.h>
57 #include <rte_atomic.h>
58 #include <rte_cycles.h>
59 #include <rte_prefetch.h>
60 #include <rte_lcore.h>
61 #include <rte_per_lcore.h>
62 #include <rte_branch_prediction.h>
63 #include <rte_interrupts.h>
64 #include <rte_pci.h>
65 #include <rte_random.h>
66 #include <rte_debug.h>
67 #include <rte_ether.h>
68 #include <rte_ethdev.h>
69 #include <rte_ring.h>
70 #include <rte_mempool.h>
71 #include <rte_mbuf.h>
72
73 #include "main.h"
74
75 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
76
77 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
78 #define NB_MBUF   8192
79
80 /*
81  * RX and TX Prefetch, Host, and Write-back threshold values should be
82  * carefully set for optimal performance. Consult the network
83  * controller's datasheet and supporting DPDK documentation for guidance
84  * on how these parameters should be set.
85  */
86 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
87 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
88 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
89
90 /*
91  * These default values are optimized for use with the Intel(R) 82599 10 GbE
92  * Controller and the DPDK ixgbe PMD. Consider using other values for other
93  * network controllers and/or network drivers.
94  */
95 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
96 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
97 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
98
99 #define MAX_PKT_BURST 32
100 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
101
102 /*
103  * Configurable number of RX/TX ring descriptors
104  */
105 #define RTE_TEST_RX_DESC_DEFAULT 128
106 #define RTE_TEST_TX_DESC_DEFAULT 512
107 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
108 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
109
110 /* ethernet addresses of ports */
111 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
112
113 /* mask of enabled ports */
114 static uint32_t l2fwd_enabled_port_mask = 0;
115
116 /* list of enabled ports */
117 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
118
119 static unsigned int l2fwd_rx_queue_per_lcore = 1;
120
121 struct mbuf_table {
122         unsigned len;
123         struct rte_mbuf *m_table[MAX_PKT_BURST];
124 };
125
126 #define MAX_RX_QUEUE_PER_LCORE 16
127 #define MAX_TX_QUEUE_PER_PORT 16
128 struct lcore_queue_conf {
129         unsigned n_rx_port;
130         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
131         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
132
133 } __rte_cache_aligned;
134 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
135
136 static const struct rte_eth_conf port_conf = {
137         .rxmode = {
138                 .split_hdr_size = 0,
139                 .header_split   = 0, /**< Header Split disabled */
140                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
141                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
142                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
143                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
144         },
145         .txmode = {
146                 .mq_mode = ETH_MQ_TX_NONE,
147         },
148 };
149
150 static const struct rte_eth_rxconf rx_conf = {
151         .rx_thresh = {
152                 .pthresh = RX_PTHRESH,
153                 .hthresh = RX_HTHRESH,
154                 .wthresh = RX_WTHRESH,
155         },
156 };
157
158 static const struct rte_eth_txconf tx_conf = {
159         .tx_thresh = {
160                 .pthresh = TX_PTHRESH,
161                 .hthresh = TX_HTHRESH,
162                 .wthresh = TX_WTHRESH,
163         },
164         .tx_free_thresh = 0, /* Use PMD default values */
165         .tx_rs_thresh = 0, /* Use PMD default values */
166         /*
167         * As the example won't handle mult-segments and offload cases,
168         * set the flag by default.
169         */
170         .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS | ETH_TXQ_FLAGS_NOOFFLOADS,
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[RTE_MAX_ETHPORTS];
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 < RTE_MAX_ETHPORTS; 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 burst of packets 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 /* Enqueue packets for TX and prepare them to be sent */
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, diff_tsc, cur_tsc, timer_tsc;
308         unsigned i, j, portid, nb_rx;
309         struct lcore_queue_conf *qconf;
310         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
311
312         prev_tsc = 0;
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                 return;
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 > drain_tsc)) {
341
342                         for (portid = 0; portid < RTE_MAX_ETHPORTS; 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         uint8_t nb_ports;
590         uint8_t nb_ports_available;
591         uint8_t portid, last_port;
592         unsigned lcore_id, rx_lcore_id;
593         unsigned nb_ports_in_mask = 0;
594
595         /* init EAL */
596         ret = rte_eal_init(argc, argv);
597         if (ret < 0)
598                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
599         argc -= ret;
600         argv += ret;
601
602         /* parse application arguments (after the EAL ones) */
603         ret = l2fwd_parse_args(argc, argv);
604         if (ret < 0)
605                 rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
606
607         /* create the mbuf pool */
608         l2fwd_pktmbuf_pool =
609                 rte_mempool_create("mbuf_pool", NB_MBUF,
610                                    MBUF_SIZE, 32,
611                                    sizeof(struct rte_pktmbuf_pool_private),
612                                    rte_pktmbuf_pool_init, NULL,
613                                    rte_pktmbuf_init, NULL,
614                                    rte_socket_id(), 0);
615         if (l2fwd_pktmbuf_pool == NULL)
616                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\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 > RTE_MAX_ETHPORTS)
626                 nb_ports = RTE_MAX_ETHPORTS;
627
628         /* reset l2fwd_dst_ports */
629         for (portid = 0; portid < RTE_MAX_ETHPORTS; 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) {
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         nb_ports_available = nb_ports;
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                         nb_ports_available--;
692                         continue;
693                 }
694                 /* init port */
695                 printf("Initializing port %u... ", (unsigned) portid);
696                 fflush(stdout);
697                 ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
698                 if (ret < 0)
699                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
700                                   ret, (unsigned) portid);
701
702                 rte_eth_macaddr_get(portid,&l2fwd_ports_eth_addr[portid]);
703
704                 /* init one RX queue */
705                 fflush(stdout);
706                 ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
707                                              rte_eth_dev_socket_id(portid), &rx_conf,
708                                              l2fwd_pktmbuf_pool);
709                 if (ret < 0)
710                         rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
711                                   ret, (unsigned) portid);
712
713                 /* init one TX queue on each port */
714                 fflush(stdout);
715                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
716                                 rte_eth_dev_socket_id(portid), &tx_conf);
717                 if (ret < 0)
718                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
719                                 ret, (unsigned) portid);
720
721                 /* Start device */
722                 ret = rte_eth_dev_start(portid);
723                 if (ret < 0)
724                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
725                                   ret, (unsigned) portid);
726
727                 printf("done: \n");
728
729                 rte_eth_promiscuous_enable(portid);
730
731                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
732                                 (unsigned) 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         if (!nb_ports_available) {
745                 rte_exit(EXIT_FAILURE,
746                         "All available ports are disabled. Please set portmask.\n");
747         }
748
749         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
750
751         /* launch per-lcore init on every lcore */
752         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MASTER);
753         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
754                 if (rte_eal_wait_lcore(lcore_id) < 0)
755                         return -1;
756         }
757
758         return 0;
759 }
760