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