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