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