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