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