vhost: combine select with sleep
[dpdk.git] / examples / dpdk_qat / 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 <stdint.h>
37 #include <inttypes.h>
38 #include <sys/types.h>
39 #include <string.h>
40 #include <sys/queue.h>
41 #include <stdarg.h>
42 #include <errno.h>
43 #include <getopt.h>
44
45 #include <rte_common.h>
46 #include <rte_byteorder.h>
47 #include <rte_log.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
50 #include <rte_tailq.h>
51 #include <rte_eal.h>
52 #include <rte_per_lcore.h>
53 #include <rte_launch.h>
54 #include <rte_atomic.h>
55 #include <rte_cycles.h>
56 #include <rte_prefetch.h>
57 #include <rte_lcore.h>
58 #include <rte_per_lcore.h>
59 #include <rte_branch_prediction.h>
60 #include <rte_interrupts.h>
61 #include <rte_pci.h>
62 #include <rte_random.h>
63 #include <rte_debug.h>
64 #include <rte_ether.h>
65 #include <rte_ethdev.h>
66 #include <rte_ring.h>
67 #include <rte_mempool.h>
68 #include <rte_mbuf.h>
69 #include <rte_ip.h>
70 #include <rte_string_fns.h>
71
72 #include "crypto.h"
73
74 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
75 #define NB_MBUF   (32 * 1024)
76
77 #define MAX_PKT_BURST 32
78 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
79
80 #define TX_QUEUE_FLUSH_MASK 0xFFFFFFFF
81 #define TSC_COUNT_LIMIT 1000
82
83 #define ACTION_ENCRYPT 1
84 #define ACTION_DECRYPT 2
85
86 /*
87  * Configurable number of RX/TX ring descriptors
88  */
89 #define RTE_TEST_RX_DESC_DEFAULT 128
90 #define RTE_TEST_TX_DESC_DEFAULT 512
91 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
92 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
93
94 /* ethernet addresses of ports */
95 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
96
97 /* mask of enabled ports */
98 static unsigned enabled_port_mask = 0;
99 static int promiscuous_on = 1; /**< Ports set in promiscuous mode on by default. */
100
101 /* list of enabled ports */
102 static uint32_t dst_ports[RTE_MAX_ETHPORTS];
103
104 struct mbuf_table {
105         uint16_t len;
106         struct rte_mbuf *m_table[MAX_PKT_BURST];
107 };
108
109 struct lcore_rx_queue {
110         uint8_t port_id;
111         uint8_t queue_id;
112 };
113
114 #define MAX_RX_QUEUE_PER_LCORE 16
115
116 #define MAX_LCORE_PARAMS 1024
117 struct lcore_params {
118         uint8_t port_id;
119         uint8_t queue_id;
120         uint8_t lcore_id;
121 };
122
123 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
124 static struct lcore_params lcore_params_array_default[] = {
125         {0, 0, 2},
126         {0, 1, 2},
127         {0, 2, 2},
128         {1, 0, 2},
129         {1, 1, 2},
130         {1, 2, 2},
131         {2, 0, 2},
132         {3, 0, 3},
133         {3, 1, 3},
134 };
135
136 static struct lcore_params * lcore_params = lcore_params_array_default;
137 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
138                                 sizeof(lcore_params_array_default[0]);
139
140 static struct rte_eth_conf port_conf = {
141         .rxmode = {
142                 .mq_mode        = ETH_MQ_RX_RSS,
143                 .split_hdr_size = 0,
144                 .header_split   = 0, /**< Header Split disabled */
145                 .hw_ip_checksum = 1, /**< IP checksum offload enabled */
146                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
147                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
148                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
149         },
150         .rx_adv_conf = {
151                 .rss_conf = {
152                         .rss_key = NULL,
153                         .rss_hf = ETH_RSS_IP,
154                 },
155         },
156         .txmode = {
157                 .mq_mode = ETH_MQ_TX_NONE,
158         },
159 };
160
161 static struct rte_mempool * pktmbuf_pool[RTE_MAX_NUMA_NODES];
162
163 struct lcore_conf {
164         uint64_t tsc;
165         uint64_t tsc_count;
166         uint32_t tx_mask;
167         uint16_t n_rx_queue;
168         uint16_t rx_queue_list_pos;
169         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
170         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
171         struct mbuf_table rx_mbuf;
172         uint32_t rx_mbuf_pos;
173         uint32_t rx_curr_queue;
174         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
175 } __rte_cache_aligned;
176
177 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
178
179 static inline struct rte_mbuf *
180 nic_rx_get_packet(struct lcore_conf *qconf)
181 {
182         struct rte_mbuf *pkt;
183
184         if (unlikely(qconf->n_rx_queue == 0))
185                 return NULL;
186
187         /* Look for the next queue with packets; return if none */
188         if (unlikely(qconf->rx_mbuf_pos == qconf->rx_mbuf.len)) {
189                 uint32_t i;
190
191                 qconf->rx_mbuf_pos = 0;
192                 for (i = 0; i < qconf->n_rx_queue; i++) {
193                         qconf->rx_mbuf.len = rte_eth_rx_burst(
194                                 qconf->rx_queue_list[qconf->rx_curr_queue].port_id,
195                                 qconf->rx_queue_list[qconf->rx_curr_queue].queue_id,
196                                 qconf->rx_mbuf.m_table, MAX_PKT_BURST);
197
198                         qconf->rx_curr_queue++;
199                         if (unlikely(qconf->rx_curr_queue == qconf->n_rx_queue))
200                                 qconf->rx_curr_queue = 0;
201                         if (likely(qconf->rx_mbuf.len > 0))
202                                 break;
203                 }
204                 if (unlikely(i == qconf->n_rx_queue))
205                         return NULL;
206         }
207
208         /* Get the next packet from the current queue; if last packet, go to next queue */
209         pkt = qconf->rx_mbuf.m_table[qconf->rx_mbuf_pos];
210         qconf->rx_mbuf_pos++;
211
212         return pkt;
213 }
214
215 static inline void
216 nic_tx_flush_queues(struct lcore_conf *qconf)
217 {
218         uint8_t portid;
219
220         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
221                 struct rte_mbuf **m_table = NULL;
222                 uint16_t queueid, len;
223                 uint32_t n, i;
224
225                 if (likely((qconf->tx_mask & (1 << portid)) == 0))
226                         continue;
227
228                 len = qconf->tx_mbufs[portid].len;
229                 if (likely(len == 0))
230                         continue;
231
232                 queueid = qconf->tx_queue_id[portid];
233                 m_table = qconf->tx_mbufs[portid].m_table;
234
235                 n = rte_eth_tx_burst(portid, queueid, m_table, len);
236                 for (i = n; i < len; i++){
237                         rte_pktmbuf_free(m_table[i]);
238                 }
239
240                 qconf->tx_mbufs[portid].len = 0;
241         }
242
243         qconf->tx_mask = TX_QUEUE_FLUSH_MASK;
244 }
245
246 static inline void
247 nic_tx_send_packet(struct rte_mbuf *pkt, uint8_t port)
248 {
249         struct lcore_conf *qconf;
250         uint32_t lcoreid;
251         uint16_t len;
252
253         if (unlikely(pkt == NULL)) {
254                 return;
255         }
256
257         lcoreid = rte_lcore_id();
258         qconf = &lcore_conf[lcoreid];
259
260         len = qconf->tx_mbufs[port].len;
261         qconf->tx_mbufs[port].m_table[len] = pkt;
262         len++;
263
264         /* enough pkts to be sent */
265         if (unlikely(len == MAX_PKT_BURST)) {
266                 uint32_t n, i;
267                 uint16_t queueid;
268
269                 queueid = qconf->tx_queue_id[port];
270                 n = rte_eth_tx_burst(port, queueid, qconf->tx_mbufs[port].m_table, MAX_PKT_BURST);
271                 for (i = n; i < MAX_PKT_BURST; i++){
272                         rte_pktmbuf_free(qconf->tx_mbufs[port].m_table[i]);
273                 }
274
275                 qconf->tx_mask &= ~(1 << port);
276                 len = 0;
277         }
278
279         qconf->tx_mbufs[port].len = len;
280 }
281
282 /* main processing loop */
283 static __attribute__((noreturn)) int
284 main_loop(__attribute__((unused)) void *dummy)
285 {
286         uint32_t lcoreid;
287         struct lcore_conf *qconf;
288         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
289
290         lcoreid = rte_lcore_id();
291         qconf = &lcore_conf[lcoreid];
292
293         printf("Thread %u starting...\n", lcoreid);
294
295         for (;;) {
296                 struct rte_mbuf *pkt;
297                 uint32_t pkt_from_nic_rx = 0;
298                 uint8_t port;
299
300                 /* Flush TX queues */
301                 qconf->tsc_count++;
302                 if (unlikely(qconf->tsc_count == TSC_COUNT_LIMIT)) {
303                         uint64_t tsc, diff_tsc;
304
305                         tsc = rte_rdtsc();
306
307                         diff_tsc = tsc - qconf->tsc;
308                         if (unlikely(diff_tsc > drain_tsc)) {
309                                 nic_tx_flush_queues(qconf);
310                                 crypto_flush_tx_queue(lcoreid);
311                                 qconf->tsc = tsc;
312                         }
313
314                         qconf->tsc_count = 0;
315                 }
316
317                 /*
318                  * Check the Intel QuickAssist queues first
319                  *
320                  ***/
321                 pkt = (struct rte_mbuf *) crypto_get_next_response();
322                 if (pkt == NULL) {
323                         pkt = nic_rx_get_packet(qconf);
324                         pkt_from_nic_rx = 1;
325                 }
326                 if (pkt == NULL)
327                         continue;
328                 /* Send packet to either QAT encrypt, QAT decrypt or NIC TX */
329                 if (pkt_from_nic_rx) {
330                         struct ipv4_hdr *ip  = (struct ipv4_hdr *) (rte_pktmbuf_mtod(pkt, unsigned char *) +
331                                         sizeof(struct ether_hdr));
332                         if (ip->src_addr & rte_cpu_to_be_32(ACTION_ENCRYPT)) {
333                                 if (CRYPTO_RESULT_FAIL == crypto_encrypt(pkt,
334                                         (enum cipher_alg)((ip->src_addr >> 16) & 0xFF),
335                                         (enum hash_alg)((ip->src_addr >> 8) & 0xFF)))
336                                         rte_pktmbuf_free(pkt);
337                                 continue;
338                         }
339
340                         if (ip->src_addr & rte_cpu_to_be_32(ACTION_DECRYPT)) {
341                                 if(CRYPTO_RESULT_FAIL == crypto_decrypt(pkt,
342                                         (enum cipher_alg)((ip->src_addr >> 16) & 0xFF),
343                                         (enum hash_alg)((ip->src_addr >> 8) & 0xFF)))
344                                         rte_pktmbuf_free(pkt);
345                                 continue;
346                         }
347                 }
348
349                 port = dst_ports[pkt->port];
350
351                 /* Transmit the packet */
352                 nic_tx_send_packet(pkt, (uint8_t)port);
353         }
354 }
355
356 static inline unsigned
357 get_port_max_rx_queues(uint8_t port_id)
358 {
359         struct rte_eth_dev_info dev_info;
360
361         rte_eth_dev_info_get(port_id, &dev_info);
362         return dev_info.max_rx_queues;
363 }
364
365 static inline unsigned
366 get_port_max_tx_queues(uint8_t port_id)
367 {
368         struct rte_eth_dev_info dev_info;
369
370         rte_eth_dev_info_get(port_id, &dev_info);
371         return dev_info.max_tx_queues;
372 }
373
374 static int
375 check_lcore_params(void)
376 {
377         uint16_t i;
378
379         for (i = 0; i < nb_lcore_params; ++i) {
380                 if (lcore_params[i].queue_id >= get_port_max_rx_queues(lcore_params[i].port_id)) {
381                         printf("invalid queue number: %hhu\n", lcore_params[i].queue_id);
382                         return -1;
383                 }
384                 if (!rte_lcore_is_enabled(lcore_params[i].lcore_id)) {
385                         printf("error: lcore %hhu is not enabled in lcore mask\n",
386                                 lcore_params[i].lcore_id);
387                         return -1;
388                 }
389         }
390         return 0;
391 }
392
393 static int
394 check_port_config(const unsigned nb_ports)
395 {
396         unsigned portid;
397         uint16_t i;
398
399         for (i = 0; i < nb_lcore_params; ++i) {
400                 portid = lcore_params[i].port_id;
401                 if ((enabled_port_mask & (1 << portid)) == 0) {
402                         printf("port %u is not enabled in port mask\n", portid);
403                         return -1;
404                 }
405                 if (portid >= nb_ports) {
406                         printf("port %u is not present on the board\n", portid);
407                         return -1;
408                 }
409         }
410         return 0;
411 }
412
413 static uint8_t
414 get_port_n_rx_queues(const uint8_t port)
415 {
416         int queue = -1;
417         uint16_t i;
418
419         for (i = 0; i < nb_lcore_params; ++i) {
420                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
421                         queue = lcore_params[i].queue_id;
422         }
423         return (uint8_t)(++queue);
424 }
425
426 static int
427 init_lcore_rx_queues(void)
428 {
429         uint16_t i, nb_rx_queue;
430         uint8_t lcore;
431
432         for (i = 0; i < nb_lcore_params; ++i) {
433                 lcore = lcore_params[i].lcore_id;
434                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
435                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
436                         printf("error: too many queues (%u) for lcore: %u\n",
437                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
438                         return -1;
439                 }
440                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
441                         lcore_params[i].port_id;
442                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
443                         lcore_params[i].queue_id;
444                 lcore_conf[lcore].n_rx_queue++;
445         }
446         return 0;
447 }
448
449 /* display usage */
450 static void
451 print_usage(const char *prgname)
452 {
453         printf ("%s [EAL options] -- -p PORTMASK [--no-promisc]"
454                 "  [--config '(port,queue,lcore)[,(port,queue,lcore)]'\n"
455                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
456                 "  --no-promisc: disable promiscuous mode (default is ON)\n"
457                 "  --config '(port,queue,lcore)': rx queues configuration\n",
458                 prgname);
459 }
460
461 static unsigned
462 parse_portmask(const char *portmask)
463 {
464         char *end = NULL;
465         unsigned pm;
466
467         /* parse hexadecimal string */
468         pm = strtoul(portmask, &end, 16);
469         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
470                 return 0;
471
472         return pm;
473 }
474
475 static int
476 parse_config(const char *q_arg)
477 {
478         char s[256];
479         const char *p, *p_end = q_arg;
480         char *end;
481         enum fieldnames {
482                 FLD_PORT = 0,
483                 FLD_QUEUE,
484                 FLD_LCORE,
485                 _NUM_FLD
486         };
487         unsigned long int_fld[_NUM_FLD];
488         char *str_fld[_NUM_FLD];
489         int i;
490         unsigned size;
491
492         nb_lcore_params = 0;
493
494         while ((p = strchr(p_end,'(')) != NULL) {
495                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
496                         printf("exceeded max number of lcore params: %hu\n",
497                                 nb_lcore_params);
498                         return -1;
499                 }
500                 ++p;
501                 if((p_end = strchr(p,')')) == NULL)
502                         return -1;
503
504                 size = p_end - p;
505                 if(size >= sizeof(s))
506                         return -1;
507
508                 snprintf(s, sizeof(s), "%.*s", size, p);
509                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
510                         return -1;
511                 for (i = 0; i < _NUM_FLD; i++) {
512                         errno = 0;
513                         int_fld[i] = strtoul(str_fld[i], &end, 0);
514                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
515                                 return -1;
516                 }
517                 lcore_params_array[nb_lcore_params].port_id = (uint8_t)int_fld[FLD_PORT];
518                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
519                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
520                 ++nb_lcore_params;
521         }
522         lcore_params = lcore_params_array;
523         return 0;
524 }
525
526 /* Parse the argument given in the command line of the application */
527 static int
528 parse_args(int argc, char **argv)
529 {
530         int opt, ret;
531         char **argvopt;
532         int option_index;
533         char *prgname = argv[0];
534         static struct option lgopts[] = {
535                 {"config", 1, 0, 0},
536                 {"no-promisc", 0, 0, 0},
537                 {NULL, 0, 0, 0}
538         };
539
540         argvopt = argv;
541
542         while ((opt = getopt_long(argc, argvopt, "p:",
543                                 lgopts, &option_index)) != EOF) {
544
545                 switch (opt) {
546                 /* portmask */
547                 case 'p':
548                         enabled_port_mask = parse_portmask(optarg);
549                         if (enabled_port_mask == 0) {
550                                 printf("invalid portmask\n");
551                                 print_usage(prgname);
552                                 return -1;
553                         }
554                         break;
555
556                 /* long options */
557                 case 0:
558                         if (strcmp(lgopts[option_index].name, "config") == 0) {
559                                 ret = parse_config(optarg);
560                                 if (ret) {
561                                         printf("invalid config\n");
562                                         print_usage(prgname);
563                                         return -1;
564                                 }
565                         }
566                         if (strcmp(lgopts[option_index].name, "no-promisc") == 0) {
567                                 printf("Promiscuous mode disabled\n");
568                                 promiscuous_on = 0;
569                         }
570                         break;
571                 default:
572                         print_usage(prgname);
573                         return -1;
574                 }
575         }
576
577         if (enabled_port_mask == 0) {
578                 printf("portmask not specified\n");
579                 print_usage(prgname);
580                 return -1;
581         }
582
583         if (optind >= 0)
584                 argv[optind-1] = prgname;
585
586         ret = optind-1;
587         optind = 0; /* reset getopt lib */
588         return ret;
589 }
590
591 static void
592 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
593 {
594         char buf[ETHER_ADDR_FMT_SIZE];
595         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
596         printf("%s%s", name, buf);
597 }
598
599 static int
600 init_mem(void)
601 {
602         const unsigned flags = 0;
603         int socketid;
604         unsigned lcoreid;
605         char s[64];
606
607         RTE_LCORE_FOREACH(lcoreid) {
608                 socketid = rte_lcore_to_socket_id(lcoreid);
609                 if (socketid >= RTE_MAX_NUMA_NODES) {
610                         printf("Socket %d of lcore %u is out of range %d\n",
611                                 socketid, lcoreid, RTE_MAX_NUMA_NODES);
612                         return -1;
613                 }
614                 if (pktmbuf_pool[socketid] == NULL) {
615                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
616                         pktmbuf_pool[socketid] =
617                                 rte_mempool_create(s, NB_MBUF, MBUF_SIZE, 32,
618                                         sizeof(struct rte_pktmbuf_pool_private),
619                                         rte_pktmbuf_pool_init, NULL,
620                                         rte_pktmbuf_init, NULL,
621                                         socketid, flags);
622                         if (pktmbuf_pool[socketid] == NULL) {
623                                 printf("Cannot init mbuf pool on socket %d\n", socketid);
624                                 return -1;
625                         }
626                         printf("Allocated mbuf pool on socket %d\n", socketid);
627                 }
628         }
629         return 0;
630 }
631
632 int
633 main(int argc, char **argv)
634 {
635         struct lcore_conf *qconf;
636         struct rte_eth_link link;
637         int ret;
638         unsigned nb_ports;
639         uint16_t queueid;
640         unsigned lcoreid;
641         uint32_t nb_tx_queue;
642         uint8_t portid, nb_rx_queue, queue, socketid, last_port;
643         unsigned nb_ports_in_mask = 0;
644
645         /* init EAL */
646         ret = rte_eal_init(argc, argv);
647         if (ret < 0)
648                 return -1;
649         argc -= ret;
650         argv += ret;
651
652         /* parse application arguments (after the EAL ones) */
653         ret = parse_args(argc, argv);
654         if (ret < 0)
655                 return -1;
656
657         if (check_lcore_params() < 0)
658                 rte_panic("check_lcore_params failed\n");
659
660         ret = init_lcore_rx_queues();
661         if (ret < 0)
662                 return -1;
663
664         ret = init_mem();
665         if (ret < 0)
666                 return -1;
667
668         nb_ports = rte_eth_dev_count();
669         if (nb_ports > RTE_MAX_ETHPORTS)
670                 nb_ports = RTE_MAX_ETHPORTS;
671
672         if (check_port_config(nb_ports) < 0)
673                 rte_panic("check_port_config failed\n");
674
675         /* reset dst_ports */
676         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
677                 dst_ports[portid] = 0;
678         last_port = 0;
679
680         /*
681          * Each logical core is assigned a dedicated TX queue on each port.
682          */
683         for (portid = 0; portid < nb_ports; portid++) {
684                 /* skip ports that are not enabled */
685                 if ((enabled_port_mask & (1 << portid)) == 0)
686                         continue;
687
688                 if (nb_ports_in_mask % 2) {
689                         dst_ports[portid] = last_port;
690                         dst_ports[last_port] = portid;
691                 }
692                 else
693                         last_port = portid;
694
695                 nb_ports_in_mask++;
696         }
697         if (nb_ports_in_mask % 2) {
698                 printf("Notice: odd number of ports in portmask.\n");
699                 dst_ports[last_port] = last_port;
700         }
701
702         /* initialize all ports */
703         for (portid = 0; portid < nb_ports; portid++) {
704                 /* skip ports that are not enabled */
705                 if ((enabled_port_mask & (1 << portid)) == 0) {
706                         printf("\nSkipping disabled port %d\n", portid);
707                         continue;
708                 }
709
710                 /* init port */
711                 printf("Initializing port %d ... ", portid );
712                 fflush(stdout);
713
714                 nb_rx_queue = get_port_n_rx_queues(portid);
715                 if (nb_rx_queue > get_port_max_rx_queues(portid))
716                         rte_panic("Number of rx queues %d exceeds max number of rx queues %u"
717                                 " for port %d\n", nb_rx_queue, get_port_max_rx_queues(portid),
718                                 portid);
719                 nb_tx_queue = rte_lcore_count();
720                 if (nb_tx_queue > get_port_max_tx_queues(portid))
721                         rte_panic("Number of lcores %u exceeds max number of tx queues %u"
722                                 " for port %d\n", nb_tx_queue, get_port_max_tx_queues(portid),
723                                 portid);
724                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
725                         nb_rx_queue, (unsigned)nb_tx_queue );
726                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
727                                         (uint16_t)nb_tx_queue, &port_conf);
728                 if (ret < 0)
729                         rte_panic("Cannot configure device: err=%d, port=%d\n",
730                                 ret, portid);
731
732                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
733                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
734                 printf(", ");
735
736                 /* init one TX queue per couple (lcore,port) */
737                 queueid = 0;
738                 RTE_LCORE_FOREACH(lcoreid) {
739                         socketid = (uint8_t)rte_lcore_to_socket_id(lcoreid);
740                         printf("txq=%u,%d,%d ", lcoreid, queueid, socketid);
741                         fflush(stdout);
742                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
743                                         socketid,
744                                         NULL);
745                         if (ret < 0)
746                                 rte_panic("rte_eth_tx_queue_setup: err=%d, "
747                                         "port=%d\n", ret, portid);
748
749                         qconf = &lcore_conf[lcoreid];
750                         qconf->tx_queue_id[portid] = queueid;
751                         queueid++;
752                 }
753                 printf("\n");
754         }
755
756         RTE_LCORE_FOREACH(lcoreid) {
757                 qconf = &lcore_conf[lcoreid];
758                 printf("\nInitializing rx queues on lcore %u ... ", lcoreid );
759                 fflush(stdout);
760                 /* init RX queues */
761                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
762                         portid = qconf->rx_queue_list[queue].port_id;
763                         queueid = qconf->rx_queue_list[queue].queue_id;
764                         socketid = (uint8_t)rte_lcore_to_socket_id(lcoreid);
765                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
766                         fflush(stdout);
767
768                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
769                                         socketid,
770                                         NULL,
771                                         pktmbuf_pool[socketid]);
772                         if (ret < 0)
773                                 rte_panic("rte_eth_rx_queue_setup: err=%d,"
774                                                 "port=%d\n", ret, portid);
775                 }
776         }
777
778         printf("\n");
779
780         /* start ports */
781         for (portid = 0; portid < nb_ports; portid++) {
782                 if ((enabled_port_mask & (1 << portid)) == 0)
783                         continue;
784                 /* Start device */
785                 ret = rte_eth_dev_start(portid);
786                 if (ret < 0)
787                         rte_panic("rte_eth_dev_start: err=%d, port=%d\n",
788                                 ret, portid);
789
790                 printf("done: Port %d ", portid);
791
792                 /* get link status */
793                 rte_eth_link_get(portid, &link);
794                 if (link.link_status)
795                         printf(" Link Up - speed %u Mbps - %s\n",
796                                (unsigned) link.link_speed,
797                                (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
798                                ("full-duplex") : ("half-duplex\n"));
799                 else
800                         printf(" Link Down\n");
801                 /*
802                  * If enabled, put device in promiscuous mode.
803                  * This allows IO forwarding mode to forward packets
804                  * to itself through 2 cross-connected  ports of the
805                  * target machine.
806                  */
807                 if (promiscuous_on)
808                         rte_eth_promiscuous_enable(portid);
809         }
810         printf("Crypto: Initializing Crypto...\n");
811         if (crypto_init() != 0)
812                 return -1;
813
814         RTE_LCORE_FOREACH(lcoreid) {
815                 if (per_core_crypto_init(lcoreid) != 0) {
816                 printf("Crypto: Cannot init lcore crypto on lcore %u\n", (unsigned)lcoreid);
817                         return -1;
818                 }
819         }
820         printf("Crypto: Initialization complete\n");
821         /* launch per-lcore init on every lcore */
822         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
823         RTE_LCORE_FOREACH_SLAVE(lcoreid) {
824                 if (rte_eal_wait_lcore(lcoreid) < 0)
825                         return -1;
826         }
827
828         return 0;
829 }