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