examples/l2fwd-crypto: use key-value list of supported algorithms
[dpdk.git] / examples / l2fwd-crypto / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 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 <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <setjmp.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <getopt.h>
48
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_common.h>
52 #include <rte_cryptodev.h>
53 #include <rte_cycles.h>
54 #include <rte_debug.h>
55 #include <rte_eal.h>
56 #include <rte_ether.h>
57 #include <rte_ethdev.h>
58 #include <rte_interrupts.h>
59 #include <rte_ip.h>
60 #include <rte_launch.h>
61 #include <rte_lcore.h>
62 #include <rte_log.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_memcpy.h>
66 #include <rte_memory.h>
67 #include <rte_mempool.h>
68 #include <rte_memzone.h>
69 #include <rte_pci.h>
70 #include <rte_per_lcore.h>
71 #include <rte_prefetch.h>
72 #include <rte_random.h>
73 #include <rte_ring.h>
74
75 enum cdev_type {
76         CDEV_TYPE_ANY,
77         CDEV_TYPE_HW,
78         CDEV_TYPE_SW
79 };
80
81 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
82
83 #define NB_MBUF   8192
84
85 #define MAX_STR_LEN 32
86 #define MAX_KEY_SIZE 128
87 #define MAX_PKT_BURST 32
88 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
89
90 /*
91  * Configurable number of RX/TX ring descriptors
92  */
93 #define RTE_TEST_RX_DESC_DEFAULT 128
94 #define RTE_TEST_TX_DESC_DEFAULT 512
95
96 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
97 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
98
99 /* ethernet addresses of ports */
100 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
101
102 /* mask of enabled ports */
103 static uint64_t l2fwd_enabled_port_mask;
104 static uint64_t l2fwd_enabled_crypto_mask;
105
106 /* list of enabled ports */
107 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
108
109
110 struct pkt_buffer {
111         unsigned len;
112         struct rte_mbuf *buffer[MAX_PKT_BURST];
113 };
114
115 struct op_buffer {
116         unsigned len;
117         struct rte_crypto_op *buffer[MAX_PKT_BURST];
118 };
119
120 #define MAX_RX_QUEUE_PER_LCORE 16
121 #define MAX_TX_QUEUE_PER_PORT 16
122
123 enum l2fwd_crypto_xform_chain {
124         L2FWD_CRYPTO_CIPHER_HASH,
125         L2FWD_CRYPTO_HASH_CIPHER,
126         L2FWD_CRYPTO_CIPHER_ONLY,
127         L2FWD_CRYPTO_HASH_ONLY
128 };
129
130 struct l2fwd_key {
131         uint8_t *data;
132         uint32_t length;
133         phys_addr_t phys_addr;
134 };
135
136 char supported_auth_algo[RTE_CRYPTO_AUTH_LIST_END][MAX_STR_LEN];
137 char supported_cipher_algo[RTE_CRYPTO_CIPHER_LIST_END][MAX_STR_LEN];
138
139 /** l2fwd crypto application command line options */
140 struct l2fwd_crypto_options {
141         unsigned portmask;
142         unsigned nb_ports_per_lcore;
143         unsigned refresh_period;
144         unsigned single_lcore:1;
145
146         enum cdev_type type;
147         unsigned sessionless:1;
148
149         enum l2fwd_crypto_xform_chain xform_chain;
150
151         struct rte_crypto_sym_xform cipher_xform;
152         unsigned ckey_param;
153         int ckey_random_size;
154
155         struct l2fwd_key iv;
156         unsigned iv_param;
157         int iv_random_size;
158
159         struct rte_crypto_sym_xform auth_xform;
160         uint8_t akey_param;
161         int akey_random_size;
162
163         struct l2fwd_key aad;
164         unsigned aad_param;
165         int aad_random_size;
166
167         int digest_size;
168
169         uint16_t block_size;
170         char string_type[MAX_STR_LEN];
171 };
172
173 /** l2fwd crypto lcore params */
174 struct l2fwd_crypto_params {
175         uint8_t dev_id;
176         uint8_t qp_id;
177
178         unsigned digest_length;
179         unsigned block_size;
180
181         struct l2fwd_key iv;
182         struct l2fwd_key aad;
183         struct rte_cryptodev_sym_session *session;
184
185         uint8_t do_cipher;
186         uint8_t do_hash;
187         uint8_t hash_verify;
188
189         enum rte_crypto_cipher_algorithm cipher_algo;
190         enum rte_crypto_auth_algorithm auth_algo;
191 };
192
193 /** lcore configuration */
194 struct lcore_queue_conf {
195         unsigned nb_rx_ports;
196         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
197
198         unsigned nb_crypto_devs;
199         unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
200
201         struct op_buffer op_buf[RTE_MAX_ETHPORTS];
202         struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
203 } __rte_cache_aligned;
204
205 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
206
207 static const struct rte_eth_conf port_conf = {
208         .rxmode = {
209                 .mq_mode = ETH_MQ_RX_NONE,
210                 .max_rx_pkt_len = ETHER_MAX_LEN,
211                 .split_hdr_size = 0,
212                 .header_split   = 0, /**< Header Split disabled */
213                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
214                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
215                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
216                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
217         },
218         .txmode = {
219                 .mq_mode = ETH_MQ_TX_NONE,
220         },
221 };
222
223 struct rte_mempool *l2fwd_pktmbuf_pool;
224 struct rte_mempool *l2fwd_crypto_op_pool;
225
226 /* Per-port statistics struct */
227 struct l2fwd_port_statistics {
228         uint64_t tx;
229         uint64_t rx;
230
231         uint64_t crypto_enqueued;
232         uint64_t crypto_dequeued;
233
234         uint64_t dropped;
235 } __rte_cache_aligned;
236
237 struct l2fwd_crypto_statistics {
238         uint64_t enqueued;
239         uint64_t dequeued;
240
241         uint64_t errors;
242 } __rte_cache_aligned;
243
244 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
245 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
246
247 /* A tsc-based timer responsible for triggering statistics printout */
248 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
249 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
250
251 /* default period is 10 seconds */
252 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
253
254 /* Print out statistics on packets dropped */
255 static void
256 print_stats(void)
257 {
258         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
259         uint64_t total_packets_enqueued, total_packets_dequeued,
260                 total_packets_errors;
261         unsigned portid;
262         uint64_t cdevid;
263
264         total_packets_dropped = 0;
265         total_packets_tx = 0;
266         total_packets_rx = 0;
267         total_packets_enqueued = 0;
268         total_packets_dequeued = 0;
269         total_packets_errors = 0;
270
271         const char clr[] = { 27, '[', '2', 'J', '\0' };
272         const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
273
274                 /* Clear screen and move to top left */
275         printf("%s%s", clr, topLeft);
276
277         printf("\nPort statistics ====================================");
278
279         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
280                 /* skip disabled ports */
281                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
282                         continue;
283                 printf("\nStatistics for port %u ------------------------------"
284                            "\nPackets sent: %32"PRIu64
285                            "\nPackets received: %28"PRIu64
286                            "\nPackets dropped: %29"PRIu64,
287                            portid,
288                            port_statistics[portid].tx,
289                            port_statistics[portid].rx,
290                            port_statistics[portid].dropped);
291
292                 total_packets_dropped += port_statistics[portid].dropped;
293                 total_packets_tx += port_statistics[portid].tx;
294                 total_packets_rx += port_statistics[portid].rx;
295         }
296         printf("\nCrypto statistics ==================================");
297
298         for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
299                 /* skip disabled ports */
300                 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
301                         continue;
302                 printf("\nStatistics for cryptodev %"PRIu64
303                                 " -------------------------"
304                            "\nPackets enqueued: %28"PRIu64
305                            "\nPackets dequeued: %28"PRIu64
306                            "\nPackets errors: %30"PRIu64,
307                            cdevid,
308                            crypto_statistics[cdevid].enqueued,
309                            crypto_statistics[cdevid].dequeued,
310                            crypto_statistics[cdevid].errors);
311
312                 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
313                 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
314                 total_packets_errors += crypto_statistics[cdevid].errors;
315         }
316         printf("\nAggregate statistics ==============================="
317                    "\nTotal packets received: %22"PRIu64
318                    "\nTotal packets enqueued: %22"PRIu64
319                    "\nTotal packets dequeued: %22"PRIu64
320                    "\nTotal packets sent: %26"PRIu64
321                    "\nTotal packets dropped: %23"PRIu64
322                    "\nTotal packets crypto errors: %17"PRIu64,
323                    total_packets_rx,
324                    total_packets_enqueued,
325                    total_packets_dequeued,
326                    total_packets_tx,
327                    total_packets_dropped,
328                    total_packets_errors);
329         printf("\n====================================================\n");
330 }
331
332 static void
333 fill_supported_algorithm_tables(void)
334 {
335         unsigned i;
336
337         for (i = 0; i < RTE_CRYPTO_AUTH_LIST_END; i++)
338                 strcpy(supported_auth_algo[i], "NOT_SUPPORTED");
339
340         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_AES_GCM], "AES_GCM");
341         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_MD5_HMAC], "MD5_HMAC");
342         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_NULL], "NULL");
343         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA1_HMAC], "SHA1_HMAC");
344         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA224_HMAC], "SHA224_HMAC");
345         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA256_HMAC], "SHA256_HMAC");
346         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA384_HMAC], "SHA384_HMAC");
347         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SHA512_HMAC], "SHA512_HMAC");
348         strcpy(supported_auth_algo[RTE_CRYPTO_AUTH_SNOW3G_UIA2], "SNOW3G_UIA2");
349
350         for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++)
351                 strcpy(supported_cipher_algo[i], "NOT_SUPPORTED");
352
353         strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_CBC], "AES_CBC");
354         strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_AES_GCM], "AES_GCM");
355         strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_NULL], "NULL");
356         strcpy(supported_cipher_algo[RTE_CRYPTO_CIPHER_SNOW3G_UEA2], "SNOW3G_UEA2");
357 }
358
359
360 static int
361 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
362                 struct l2fwd_crypto_params *cparams)
363 {
364         struct rte_crypto_op **op_buffer;
365         unsigned ret;
366
367         op_buffer = (struct rte_crypto_op **)
368                         qconf->op_buf[cparams->dev_id].buffer;
369
370         ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
371                         cparams->qp_id, op_buffer, (uint16_t) n);
372
373         crypto_statistics[cparams->dev_id].enqueued += ret;
374         if (unlikely(ret < n)) {
375                 crypto_statistics[cparams->dev_id].errors += (n - ret);
376                 do {
377                         rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
378                         rte_crypto_op_free(op_buffer[ret]);
379                 } while (++ret < n);
380         }
381
382         return 0;
383 }
384
385 static int
386 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
387                 struct l2fwd_crypto_params *cparams)
388 {
389         unsigned lcore_id, len;
390         struct lcore_queue_conf *qconf;
391
392         lcore_id = rte_lcore_id();
393
394         qconf = &lcore_queue_conf[lcore_id];
395         len = qconf->op_buf[cparams->dev_id].len;
396         qconf->op_buf[cparams->dev_id].buffer[len] = op;
397         len++;
398
399         /* enough ops to be sent */
400         if (len == MAX_PKT_BURST) {
401                 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
402                 len = 0;
403         }
404
405         qconf->op_buf[cparams->dev_id].len = len;
406         return 0;
407 }
408
409 static int
410 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
411                 struct rte_crypto_op *op,
412                 struct l2fwd_crypto_params *cparams)
413 {
414         struct ether_hdr *eth_hdr;
415         struct ipv4_hdr *ip_hdr;
416
417         unsigned ipdata_offset, pad_len, data_len;
418         char *padding;
419
420         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
421
422         if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
423                 return -1;
424
425         ipdata_offset = sizeof(struct ether_hdr);
426
427         ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
428                         ipdata_offset);
429
430         ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
431                         * IPV4_IHL_MULTIPLIER;
432
433
434         /* Zero pad data to be crypto'd so it is block aligned */
435         data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
436         pad_len = data_len % cparams->block_size ? cparams->block_size -
437                         (data_len % cparams->block_size) : 0;
438
439         if (pad_len) {
440                 padding = rte_pktmbuf_append(m, pad_len);
441                 if (unlikely(!padding))
442                         return -1;
443
444                 data_len += pad_len;
445                 memset(padding, 0, pad_len);
446         }
447
448         /* Set crypto operation data parameters */
449         rte_crypto_op_attach_sym_session(op, cparams->session);
450
451         if (cparams->do_hash) {
452                 if (!cparams->hash_verify) {
453                         /* Append space for digest to end of packet */
454                         op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
455                                 cparams->digest_length);
456                 } else {
457                         op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
458                                 cparams->digest_length);
459                 }
460
461                 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
462                                 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
463                 op->sym->auth.digest.length = cparams->digest_length;
464
465                 /* For SNOW3G algorithms, offset/length must be in bits */
466                 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2) {
467                         op->sym->auth.data.offset = ipdata_offset << 3;
468                         op->sym->auth.data.length = data_len << 3;
469                 } else {
470                         op->sym->auth.data.offset = ipdata_offset;
471                         op->sym->auth.data.length = data_len;
472                 }
473
474                 if (cparams->aad.length) {
475                         op->sym->auth.aad.data = cparams->aad.data;
476                         op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
477                         op->sym->auth.aad.length = cparams->aad.length;
478                 }
479         }
480
481         if (cparams->do_cipher) {
482                 op->sym->cipher.iv.data = cparams->iv.data;
483                 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
484                 op->sym->cipher.iv.length = cparams->iv.length;
485
486                 /* For SNOW3G algorithms, offset/length must be in bits */
487                 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2) {
488                         op->sym->cipher.data.offset = ipdata_offset << 3;
489                         if (cparams->do_hash && cparams->hash_verify)
490                                 /* Do not cipher the hash tag */
491                                 op->sym->cipher.data.length = (data_len -
492                                         cparams->digest_length) << 3;
493                         else
494                                 op->sym->cipher.data.length = data_len << 3;
495
496                 } else {
497                         op->sym->cipher.data.offset = ipdata_offset;
498                         if (cparams->do_hash && cparams->hash_verify)
499                                 /* Do not cipher the hash tag */
500                                 op->sym->cipher.data.length = data_len -
501                                         cparams->digest_length;
502                         else
503                                 op->sym->cipher.data.length = data_len;
504                 }
505         }
506
507         op->sym->m_src = m;
508
509         return l2fwd_crypto_enqueue(op, cparams);
510 }
511
512
513 /* Send the burst of packets on an output interface */
514 static int
515 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
516                 uint8_t port)
517 {
518         struct rte_mbuf **pkt_buffer;
519         unsigned ret;
520
521         pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
522
523         ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
524         port_statistics[port].tx += ret;
525         if (unlikely(ret < n)) {
526                 port_statistics[port].dropped += (n - ret);
527                 do {
528                         rte_pktmbuf_free(pkt_buffer[ret]);
529                 } while (++ret < n);
530         }
531
532         return 0;
533 }
534
535 /* Enqueue packets for TX and prepare them to be sent */
536 static int
537 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
538 {
539         unsigned lcore_id, len;
540         struct lcore_queue_conf *qconf;
541
542         lcore_id = rte_lcore_id();
543
544         qconf = &lcore_queue_conf[lcore_id];
545         len = qconf->pkt_buf[port].len;
546         qconf->pkt_buf[port].buffer[len] = m;
547         len++;
548
549         /* enough pkts to be sent */
550         if (unlikely(len == MAX_PKT_BURST)) {
551                 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
552                 len = 0;
553         }
554
555         qconf->pkt_buf[port].len = len;
556         return 0;
557 }
558
559 static void
560 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
561 {
562         struct ether_hdr *eth;
563         void *tmp;
564         unsigned dst_port;
565
566         dst_port = l2fwd_dst_ports[portid];
567         eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
568
569         /* 02:00:00:00:00:xx */
570         tmp = &eth->d_addr.addr_bytes[0];
571         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
572
573         /* src addr */
574         ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
575
576         l2fwd_send_packet(m, (uint8_t) dst_port);
577 }
578
579 /** Generate random key */
580 static void
581 generate_random_key(uint8_t *key, unsigned length)
582 {
583         unsigned i;
584
585         for (i = 0; i < length; i++)
586                 key[i] = rand() % 0xff;
587 }
588
589 static struct rte_cryptodev_sym_session *
590 initialize_crypto_session(struct l2fwd_crypto_options *options,
591                 uint8_t cdev_id)
592 {
593         struct rte_crypto_sym_xform *first_xform;
594
595         if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
596                 first_xform = &options->cipher_xform;
597                 first_xform->next = &options->auth_xform;
598         } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
599                 first_xform = &options->auth_xform;
600                 first_xform->next = &options->cipher_xform;
601         } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
602                 first_xform = &options->cipher_xform;
603         } else {
604                 first_xform = &options->auth_xform;
605         }
606
607         /* Setup Cipher Parameters */
608         return rte_cryptodev_sym_session_create(cdev_id, first_xform);
609 }
610
611 static void
612 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
613
614 /* main processing loop */
615 static void
616 l2fwd_main_loop(struct l2fwd_crypto_options *options)
617 {
618         struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
619         struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
620
621         unsigned lcore_id = rte_lcore_id();
622         uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
623         unsigned i, j, portid, nb_rx;
624         struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
625         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
626                         US_PER_S * BURST_TX_DRAIN_US;
627         struct l2fwd_crypto_params *cparams;
628         struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
629
630         if (qconf->nb_rx_ports == 0) {
631                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
632                 return;
633         }
634
635         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
636
637         l2fwd_crypto_options_print(options);
638
639         for (i = 0; i < qconf->nb_rx_ports; i++) {
640
641                 portid = qconf->rx_port_list[i];
642                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
643                         portid);
644         }
645
646         for (i = 0; i < qconf->nb_crypto_devs; i++) {
647                 port_cparams[i].do_cipher = 0;
648                 port_cparams[i].do_hash = 0;
649
650                 switch (options->xform_chain) {
651                 case L2FWD_CRYPTO_CIPHER_HASH:
652                 case L2FWD_CRYPTO_HASH_CIPHER:
653                         port_cparams[i].do_cipher = 1;
654                         port_cparams[i].do_hash = 1;
655                         break;
656                 case L2FWD_CRYPTO_HASH_ONLY:
657                         port_cparams[i].do_hash = 1;
658                         break;
659                 case L2FWD_CRYPTO_CIPHER_ONLY:
660                         port_cparams[i].do_cipher = 1;
661                         break;
662                 }
663
664                 port_cparams[i].dev_id = qconf->cryptodev_list[i];
665                 port_cparams[i].qp_id = 0;
666
667                 port_cparams[i].block_size = options->block_size;
668
669                 if (port_cparams[i].do_hash) {
670                         port_cparams[i].digest_length =
671                                         options->auth_xform.auth.digest_length;
672                         if (options->auth_xform.auth.add_auth_data_length) {
673                                 port_cparams[i].aad.data = options->aad.data;
674                                 port_cparams[i].aad.length =
675                                         options->auth_xform.auth.add_auth_data_length;
676                                 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
677                                 if (!options->aad_param)
678                                         generate_random_key(port_cparams[i].aad.data,
679                                                 port_cparams[i].aad.length);
680
681                         }
682
683                         if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
684                                 port_cparams[i].hash_verify = 1;
685                         else
686                                 port_cparams[i].hash_verify = 0;
687
688                         port_cparams[i].auth_algo = options->auth_xform.auth.algo;
689                 }
690
691                 if (port_cparams[i].do_cipher) {
692                         port_cparams[i].iv.data = options->iv.data;
693                         port_cparams[i].iv.length = options->iv.length;
694                         port_cparams[i].iv.phys_addr = options->iv.phys_addr;
695                         if (!options->iv_param)
696                                 generate_random_key(port_cparams[i].iv.data,
697                                                 port_cparams[i].iv.length);
698
699                         port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
700                 }
701
702                 port_cparams[i].session = initialize_crypto_session(options,
703                                 port_cparams[i].dev_id);
704
705                 if (port_cparams[i].session == NULL)
706                         return;
707                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
708                                 port_cparams[i].dev_id);
709         }
710
711         while (1) {
712
713                 cur_tsc = rte_rdtsc();
714
715                 /*
716                  * TX burst queue drain
717                  */
718                 diff_tsc = cur_tsc - prev_tsc;
719                 if (unlikely(diff_tsc > drain_tsc)) {
720                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
721                                 if (qconf->pkt_buf[portid].len == 0)
722                                         continue;
723                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
724                                                  qconf->pkt_buf[portid].len,
725                                                  (uint8_t) portid);
726                                 qconf->pkt_buf[portid].len = 0;
727                         }
728
729                         /* if timer is enabled */
730                         if (timer_period > 0) {
731
732                                 /* advance the timer */
733                                 timer_tsc += diff_tsc;
734
735                                 /* if timer has reached its timeout */
736                                 if (unlikely(timer_tsc >=
737                                                 (uint64_t)timer_period)) {
738
739                                         /* do this only on master core */
740                                         if (lcore_id == rte_get_master_lcore()
741                                                 && options->refresh_period) {
742                                                 print_stats();
743                                                 timer_tsc = 0;
744                                         }
745                                 }
746                         }
747
748                         prev_tsc = cur_tsc;
749                 }
750
751                 /*
752                  * Read packet from RX queues
753                  */
754                 for (i = 0; i < qconf->nb_rx_ports; i++) {
755                         portid = qconf->rx_port_list[i];
756
757                         cparams = &port_cparams[i];
758
759                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
760                                                  pkts_burst, MAX_PKT_BURST);
761
762                         port_statistics[portid].rx += nb_rx;
763
764                         if (nb_rx) {
765                                 /*
766                                  * If we can't allocate a crypto_ops, then drop
767                                  * the rest of the burst and dequeue and
768                                  * process the packets to free offload structs
769                                  */
770                                 if (rte_crypto_op_bulk_alloc(
771                                                 l2fwd_crypto_op_pool,
772                                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
773                                                 ops_burst, nb_rx) !=
774                                                                 nb_rx) {
775                                         for (j = 0; j < nb_rx; j++)
776                                                 rte_pktmbuf_free(pkts_burst[i]);
777
778                                         nb_rx = 0;
779                                 }
780
781                                 /* Enqueue packets from Crypto device*/
782                                 for (j = 0; j < nb_rx; j++) {
783                                         m = pkts_burst[j];
784
785                                         l2fwd_simple_crypto_enqueue(m,
786                                                         ops_burst[j], cparams);
787                                 }
788                         }
789
790                         /* Dequeue packets from Crypto device */
791                         do {
792                                 nb_rx = rte_cryptodev_dequeue_burst(
793                                                 cparams->dev_id, cparams->qp_id,
794                                                 ops_burst, MAX_PKT_BURST);
795
796                                 crypto_statistics[cparams->dev_id].dequeued +=
797                                                 nb_rx;
798
799                                 /* Forward crypto'd packets */
800                                 for (j = 0; j < nb_rx; j++) {
801                                         m = ops_burst[j]->sym->m_src;
802
803                                         rte_crypto_op_free(ops_burst[j]);
804                                         l2fwd_simple_forward(m, portid);
805                                 }
806                         } while (nb_rx == MAX_PKT_BURST);
807                 }
808         }
809 }
810
811 static int
812 l2fwd_launch_one_lcore(void *arg)
813 {
814         l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
815         return 0;
816 }
817
818 /* Display command line arguments usage */
819 static void
820 l2fwd_crypto_usage(const char *prgname)
821 {
822         printf("%s [EAL options] --\n"
823                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
824                 "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
825                 "  -s manage all ports from single lcore\n"
826                 "  -T PERIOD: statistics will be refreshed each PERIOD seconds"
827                 " (0 to disable, 10 default, 86400 maximum)\n"
828
829                 "  --cdev_type HW / SW / ANY\n"
830                 "  --chain HASH_CIPHER / CIPHER_HASH\n"
831
832                 "  --cipher_algo ALGO\n"
833                 "  --cipher_op ENCRYPT / DECRYPT\n"
834                 "  --cipher_key KEY (bytes separated with \":\")\n"
835                 "  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
836                 "  --iv IV (bytes separated with \":\")\n"
837                 "  --iv_random_size SIZE: size of IV when generated randomly\n"
838
839                 "  --auth_algo ALGO\n"
840                 "  --auth_op GENERATE / VERIFY\n"
841                 "  --auth_key KEY (bytes separated with \":\")\n"
842                 "  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
843                 "  --aad AAD (bytes separated with \":\")\n"
844                 "  --aad_random_size SIZE: size of AAD when generated randomly\n"
845                 "  --digest_size SIZE: size of digest to be generated/verified\n"
846
847                 "  --sessionless\n",
848                prgname);
849 }
850
851 /** Parse crypto device type command line argument */
852 static int
853 parse_cryptodev_type(enum cdev_type *type, char *optarg)
854 {
855         if (strcmp("HW", optarg) == 0) {
856                 *type = CDEV_TYPE_HW;
857                 return 0;
858         } else if (strcmp("SW", optarg) == 0) {
859                 *type = CDEV_TYPE_SW;
860                 return 0;
861         } else if (strcmp("ANY", optarg) == 0) {
862                 *type = CDEV_TYPE_ANY;
863                 return 0;
864         }
865
866         return -1;
867 }
868
869 /** Parse crypto chain xform command line argument */
870 static int
871 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
872 {
873         if (strcmp("CIPHER_HASH", optarg) == 0) {
874                 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
875                 return 0;
876         } else if (strcmp("HASH_CIPHER", optarg) == 0) {
877                 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
878                 return 0;
879         } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
880                 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
881                 return 0;
882         } else if (strcmp("HASH_ONLY", optarg) == 0) {
883                 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
884                 return 0;
885         }
886
887         return -1;
888 }
889
890 /** Parse crypto cipher algo option command line argument */
891 static int
892 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
893 {
894         unsigned i;
895
896         for (i = 0; i < RTE_CRYPTO_CIPHER_LIST_END; i++) {
897                 if (!strcmp(supported_cipher_algo[i], optarg)) {
898                         *algo = i;
899                         return 0;
900                 }
901         }
902
903         printf("Cipher algorithm  not supported!\n");
904         return -1;
905 }
906
907 /** Parse crypto cipher operation command line argument */
908 static int
909 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
910 {
911         if (strcmp("ENCRYPT", optarg) == 0) {
912                 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
913                 return 0;
914         } else if (strcmp("DECRYPT", optarg) == 0) {
915                 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
916                 return 0;
917         }
918
919         printf("Cipher operation not supported!\n");
920         return -1;
921 }
922
923 /** Parse crypto key command line argument */
924 static int
925 parse_key(uint8_t *data, char *input_arg)
926 {
927         unsigned byte_count;
928         char *token;
929
930         for (byte_count = 0, token = strtok(input_arg, ":");
931                         (byte_count < MAX_KEY_SIZE) && (token != NULL);
932                         token = strtok(NULL, ":")) {
933
934                 int number = (int)strtol(token, NULL, 16);
935
936                 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
937                         return -1;
938
939                 data[byte_count++] = (uint8_t)number;
940         }
941
942         return byte_count;
943 }
944
945 /** Parse size param*/
946 static int
947 parse_size(int *size, const char *q_arg)
948 {
949         char *end = NULL;
950         unsigned long n;
951
952         /* parse hexadecimal string */
953         n = strtoul(q_arg, &end, 10);
954         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
955                 n = 0;
956
957         if (n == 0) {
958                 printf("invalid size\n");
959                 return -1;
960         }
961
962         *size = n;
963         return 0;
964 }
965
966 /** Parse crypto cipher operation command line argument */
967 static int
968 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
969 {
970         unsigned i;
971
972         for (i = 0; i < RTE_CRYPTO_AUTH_LIST_END; i++) {
973                 if (!strcmp(supported_auth_algo[i], optarg)) {
974                         *algo = i;
975                         return 0;
976                 }
977         }
978
979         printf("Authentication algorithm specified not supported!\n");
980         return -1;
981 }
982
983 static int
984 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
985 {
986         if (strcmp("VERIFY", optarg) == 0) {
987                 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
988                 return 0;
989         } else if (strcmp("GENERATE", optarg) == 0) {
990                 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
991                 return 0;
992         }
993
994         printf("Authentication operation specified not supported!\n");
995         return -1;
996 }
997
998 /** Parse long options */
999 static int
1000 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1001                 struct option *lgopts, int option_index)
1002 {
1003         int retval;
1004
1005         if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1006                 retval = parse_cryptodev_type(&options->type, optarg);
1007                 if (retval == 0)
1008                         strcpy(options->string_type, optarg);
1009                 return retval;
1010         }
1011
1012         else if (strcmp(lgopts[option_index].name, "chain") == 0)
1013                 return parse_crypto_opt_chain(options, optarg);
1014
1015         /* Cipher options */
1016         else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1017                 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1018                                 optarg);
1019
1020         else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1021                 return parse_cipher_op(&options->cipher_xform.cipher.op,
1022                                 optarg);
1023
1024         else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1025                 options->ckey_param = 1;
1026                 options->cipher_xform.cipher.key.length =
1027                         parse_key(options->cipher_xform.cipher.key.data, optarg);
1028                 if (options->cipher_xform.cipher.key.length > 0)
1029                         return 0;
1030                 else
1031                         return -1;
1032         }
1033
1034         else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1035                 return parse_size(&options->ckey_random_size, optarg);
1036
1037         else if (strcmp(lgopts[option_index].name, "iv") == 0) {
1038                 options->iv_param = 1;
1039                 options->iv.length =
1040                         parse_key(options->iv.data, optarg);
1041                 if (options->iv.length > 0)
1042                         return 0;
1043                 else
1044                         return -1;
1045         }
1046
1047         else if (strcmp(lgopts[option_index].name, "iv_random_size") == 0)
1048                 return parse_size(&options->iv_random_size, optarg);
1049
1050         /* Authentication options */
1051         else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1052                 return parse_auth_algo(&options->auth_xform.auth.algo,
1053                                 optarg);
1054         }
1055
1056         else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1057                 return parse_auth_op(&options->auth_xform.auth.op,
1058                                 optarg);
1059
1060         else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1061                 options->akey_param = 1;
1062                 options->auth_xform.auth.key.length =
1063                         parse_key(options->auth_xform.auth.key.data, optarg);
1064                 if (options->auth_xform.auth.key.length > 0)
1065                         return 0;
1066                 else
1067                         return -1;
1068         }
1069
1070         else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1071                 return parse_size(&options->akey_random_size, optarg);
1072         }
1073
1074         else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1075                 options->aad_param = 1;
1076                 options->aad.length =
1077                         parse_key(options->aad.data, optarg);
1078                 if (options->aad.length > 0)
1079                         return 0;
1080                 else
1081                         return -1;
1082         }
1083
1084         else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1085                 return parse_size(&options->aad_random_size, optarg);
1086         }
1087
1088         else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1089                 return parse_size(&options->digest_size, optarg);
1090         }
1091
1092         else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1093                 options->sessionless = 1;
1094                 return 0;
1095         }
1096
1097         return -1;
1098 }
1099
1100 /** Parse port mask */
1101 static int
1102 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1103                 const char *q_arg)
1104 {
1105         char *end = NULL;
1106         unsigned long pm;
1107
1108         /* parse hexadecimal string */
1109         pm = strtoul(q_arg, &end, 16);
1110         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1111                 pm = 0;
1112
1113         options->portmask = pm;
1114         if (options->portmask == 0) {
1115                 printf("invalid portmask specified\n");
1116                 return -1;
1117         }
1118
1119         return pm;
1120 }
1121
1122 /** Parse number of queues */
1123 static int
1124 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1125                 const char *q_arg)
1126 {
1127         char *end = NULL;
1128         unsigned long n;
1129
1130         /* parse hexadecimal string */
1131         n = strtoul(q_arg, &end, 10);
1132         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1133                 n = 0;
1134         else if (n >= MAX_RX_QUEUE_PER_LCORE)
1135                 n = 0;
1136
1137         options->nb_ports_per_lcore = n;
1138         if (options->nb_ports_per_lcore == 0) {
1139                 printf("invalid number of ports selected\n");
1140                 return -1;
1141         }
1142
1143         return 0;
1144 }
1145
1146 /** Parse timer period */
1147 static int
1148 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1149                 const char *q_arg)
1150 {
1151         char *end = NULL;
1152         unsigned long n;
1153
1154         /* parse number string */
1155         n = (unsigned)strtol(q_arg, &end, 10);
1156         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1157                 n = 0;
1158
1159         if (n >= MAX_TIMER_PERIOD) {
1160                 printf("Warning refresh period specified %lu is greater than "
1161                                 "max value %lu! using max value",
1162                                 n, MAX_TIMER_PERIOD);
1163                 n = MAX_TIMER_PERIOD;
1164         }
1165
1166         options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1167
1168         return 0;
1169 }
1170
1171 /** Generate default options for application */
1172 static void
1173 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1174 {
1175         srand(time(NULL));
1176
1177         options->portmask = 0xffffffff;
1178         options->nb_ports_per_lcore = 1;
1179         options->refresh_period = 10000;
1180         options->single_lcore = 0;
1181         options->sessionless = 0;
1182
1183         options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1184
1185         /* Cipher Data */
1186         options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1187         options->cipher_xform.next = NULL;
1188         options->ckey_param = 0;
1189         options->ckey_random_size = -1;
1190         options->cipher_xform.cipher.key.length = 0;
1191         options->iv_param = 0;
1192         options->iv_random_size = -1;
1193         options->iv.length = 0;
1194
1195         options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1196         options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1197
1198         /* Authentication Data */
1199         options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1200         options->auth_xform.next = NULL;
1201         options->akey_param = 0;
1202         options->akey_random_size = -1;
1203         options->auth_xform.auth.key.length = 0;
1204         options->aad_param = 0;
1205         options->aad_random_size = -1;
1206         options->aad.length = 0;
1207         options->digest_size = -1;
1208
1209         options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1210         options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1211
1212         options->type = CDEV_TYPE_ANY;
1213 }
1214
1215 static void
1216 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1217 {
1218         printf("Options:-\nn");
1219         printf("portmask: %x\n", options->portmask);
1220         printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1221         printf("refresh period : %u\n", options->refresh_period);
1222         printf("single lcore mode: %s\n",
1223                         options->single_lcore ? "enabled" : "disabled");
1224         printf("stats_printing: %s\n",
1225                         options->refresh_period == 0 ? "disabled" : "enabled");
1226
1227         printf("sessionless crypto: %s\n",
1228                         options->sessionless ? "enabled" : "disabled");
1229 }
1230
1231 /* Parse the argument given in the command line of the application */
1232 static int
1233 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1234                 int argc, char **argv)
1235 {
1236         int opt, retval, option_index;
1237         char **argvopt = argv, *prgname = argv[0];
1238
1239         static struct option lgopts[] = {
1240                         { "sessionless", no_argument, 0, 0 },
1241
1242                         { "cdev_type", required_argument, 0, 0 },
1243                         { "chain", required_argument, 0, 0 },
1244
1245                         { "cipher_algo", required_argument, 0, 0 },
1246                         { "cipher_op", required_argument, 0, 0 },
1247                         { "cipher_key", required_argument, 0, 0 },
1248                         { "cipher_key_random_size", required_argument, 0, 0 },
1249
1250                         { "auth_algo", required_argument, 0, 0 },
1251                         { "auth_op", required_argument, 0, 0 },
1252                         { "auth_key", required_argument, 0, 0 },
1253                         { "auth_key_random_size", required_argument, 0, 0 },
1254
1255                         { "iv", required_argument, 0, 0 },
1256                         { "iv_random_size", required_argument, 0, 0 },
1257                         { "aad", required_argument, 0, 0 },
1258                         { "aad_random_size", required_argument, 0, 0 },
1259                         { "digest_size", required_argument, 0, 0 },
1260
1261                         { "sessionless", no_argument, 0, 0 },
1262
1263                         { NULL, 0, 0, 0 }
1264         };
1265
1266         l2fwd_crypto_default_options(options);
1267
1268         while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1269                         &option_index)) != EOF) {
1270                 switch (opt) {
1271                 /* long options */
1272                 case 0:
1273                         retval = l2fwd_crypto_parse_args_long_options(options,
1274                                         lgopts, option_index);
1275                         if (retval < 0) {
1276                                 l2fwd_crypto_usage(prgname);
1277                                 return -1;
1278                         }
1279                         break;
1280
1281                 /* portmask */
1282                 case 'p':
1283                         retval = l2fwd_crypto_parse_portmask(options, optarg);
1284                         if (retval < 0) {
1285                                 l2fwd_crypto_usage(prgname);
1286                                 return -1;
1287                         }
1288                         break;
1289
1290                 /* nqueue */
1291                 case 'q':
1292                         retval = l2fwd_crypto_parse_nqueue(options, optarg);
1293                         if (retval < 0) {
1294                                 l2fwd_crypto_usage(prgname);
1295                                 return -1;
1296                         }
1297                         break;
1298
1299                 /* single  */
1300                 case 's':
1301                         options->single_lcore = 1;
1302
1303                         break;
1304
1305                 /* timer period */
1306                 case 'T':
1307                         retval = l2fwd_crypto_parse_timer_period(options,
1308                                         optarg);
1309                         if (retval < 0) {
1310                                 l2fwd_crypto_usage(prgname);
1311                                 return -1;
1312                         }
1313                         break;
1314
1315                 default:
1316                         l2fwd_crypto_usage(prgname);
1317                         return -1;
1318                 }
1319         }
1320
1321
1322         if (optind >= 0)
1323                 argv[optind-1] = prgname;
1324
1325         retval = optind-1;
1326         optind = 0; /* reset getopt lib */
1327
1328         return retval;
1329 }
1330
1331 /* Check the link status of all ports in up to 9s, and print them finally */
1332 static void
1333 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1334 {
1335 #define CHECK_INTERVAL 100 /* 100ms */
1336 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1337         uint8_t portid, count, all_ports_up, print_flag = 0;
1338         struct rte_eth_link link;
1339
1340         printf("\nChecking link status");
1341         fflush(stdout);
1342         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1343                 all_ports_up = 1;
1344                 for (portid = 0; portid < port_num; portid++) {
1345                         if ((port_mask & (1 << portid)) == 0)
1346                                 continue;
1347                         memset(&link, 0, sizeof(link));
1348                         rte_eth_link_get_nowait(portid, &link);
1349                         /* print link status if flag set */
1350                         if (print_flag == 1) {
1351                                 if (link.link_status)
1352                                         printf("Port %d Link Up - speed %u "
1353                                                 "Mbps - %s\n", (uint8_t)portid,
1354                                                 (unsigned)link.link_speed,
1355                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1356                                         ("full-duplex") : ("half-duplex\n"));
1357                                 else
1358                                         printf("Port %d Link Down\n",
1359                                                 (uint8_t)portid);
1360                                 continue;
1361                         }
1362                         /* clear all_ports_up flag if any link down */
1363                         if (link.link_status == 0) {
1364                                 all_ports_up = 0;
1365                                 break;
1366                         }
1367                 }
1368                 /* after finally printing all link status, get out */
1369                 if (print_flag == 1)
1370                         break;
1371
1372                 if (all_ports_up == 0) {
1373                         printf(".");
1374                         fflush(stdout);
1375                         rte_delay_ms(CHECK_INTERVAL);
1376                 }
1377
1378                 /* set the print_flag if all ports up or timeout */
1379                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1380                         print_flag = 1;
1381                         printf("done\n");
1382                 }
1383         }
1384 }
1385
1386 /* Check if device has to be HW/SW or any */
1387 static int
1388 check_type(struct l2fwd_crypto_options *options, struct rte_cryptodev_info *dev_info)
1389 {
1390         if (options->type == CDEV_TYPE_HW &&
1391                         (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1392                 return 0;
1393         if (options->type == CDEV_TYPE_SW &&
1394                         !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1395                 return 0;
1396         if (options->type == CDEV_TYPE_ANY)
1397                 return 0;
1398
1399         return -1;
1400 }
1401
1402 static inline int
1403 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1404                 uint16_t increment)
1405 {
1406         uint16_t supp_size;
1407
1408         for (supp_size = min; supp_size <= max; supp_size += increment) {
1409                 if (length == supp_size)
1410                         return 0;
1411         }
1412
1413         return -1;
1414 }
1415 static int
1416 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1417                 uint8_t *enabled_cdevs)
1418 {
1419         unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1420         const struct rte_cryptodev_capabilities *cap;
1421         enum rte_crypto_auth_algorithm cap_auth_algo;
1422         enum rte_crypto_auth_algorithm opt_auth_algo;
1423         enum rte_crypto_cipher_algorithm cap_cipher_algo;
1424         enum rte_crypto_cipher_algorithm opt_cipher_algo;
1425         int retval;
1426
1427         cdev_count = rte_cryptodev_count();
1428         if (cdev_count == 0) {
1429                 printf("No crypto devices available\n");
1430                 return -1;
1431         }
1432
1433         for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1434                         cdev_id++) {
1435                 struct rte_cryptodev_qp_conf qp_conf;
1436                 struct rte_cryptodev_info dev_info;
1437
1438                 struct rte_cryptodev_config conf = {
1439                         .nb_queue_pairs = 1,
1440                         .socket_id = SOCKET_ID_ANY,
1441                         .session_mp = {
1442                                 .nb_objs = 2048,
1443                                 .cache_size = 64
1444                         }
1445                 };
1446
1447                 rte_cryptodev_info_get(cdev_id, &dev_info);
1448
1449                 /* Set cipher parameters */
1450                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1451                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1452                                 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
1453                         /* Check if device supports cipher algo */
1454                         i = 0;
1455                         opt_cipher_algo = options->cipher_xform.cipher.algo;
1456                         cap = &dev_info.capabilities[i];
1457                         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1458                                 cap_cipher_algo = cap->sym.cipher.algo;
1459                                 if (cap->sym.xform_type ==
1460                                                 RTE_CRYPTO_SYM_XFORM_CIPHER) {
1461                                         if (cap_cipher_algo == opt_cipher_algo) {
1462                                                 if (check_type(options, &dev_info) == 0)
1463                                                         break;
1464                                         }
1465                                 }
1466                                 cap = &dev_info.capabilities[++i];
1467                         }
1468
1469                         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1470                                 printf("Algorithm %s not supported by cryptodev %u"
1471                                         " or device not of preferred type (%s)\n",
1472                                         supported_cipher_algo[opt_cipher_algo],
1473                                         cdev_id,
1474                                         options->string_type);
1475                                 continue;
1476                         }
1477
1478                         options->block_size = cap->sym.cipher.block_size;
1479                         /*
1480                          * Check if length of provided IV is supported
1481                          * by the algorithm chosen.
1482                          */
1483                         if (options->iv_param) {
1484                                 if (check_supported_size(options->iv.length,
1485                                                 cap->sym.cipher.iv_size.min,
1486                                                 cap->sym.cipher.iv_size.max,
1487                                                 cap->sym.cipher.iv_size.increment)
1488                                                         != 0) {
1489                                         printf("Unsupported IV length\n");
1490                                         return -1;
1491                                 }
1492                         /*
1493                          * Check if length of IV to be randomly generated
1494                          * is supported by the algorithm chosen.
1495                          */
1496                         } else if (options->iv_random_size != -1) {
1497                                 if (check_supported_size(options->iv_random_size,
1498                                                 cap->sym.cipher.iv_size.min,
1499                                                 cap->sym.cipher.iv_size.max,
1500                                                 cap->sym.cipher.iv_size.increment)
1501                                                         != 0) {
1502                                         printf("Unsupported IV length\n");
1503                                         return -1;
1504                                 }
1505                                 options->iv.length = options->iv_random_size;
1506                         /* No size provided, use minimum size. */
1507                         } else
1508                                 options->iv.length = cap->sym.cipher.iv_size.min;
1509
1510                         /*
1511                          * Check if length of provided cipher key is supported
1512                          * by the algorithm chosen.
1513                          */
1514                         if (options->ckey_param) {
1515                                 if (check_supported_size(
1516                                                 options->cipher_xform.cipher.key.length,
1517                                                 cap->sym.cipher.key_size.min,
1518                                                 cap->sym.cipher.key_size.max,
1519                                                 cap->sym.cipher.key_size.increment)
1520                                                         != 0) {
1521                                         printf("Unsupported cipher key length\n");
1522                                         return -1;
1523                                 }
1524                         /*
1525                          * Check if length of the cipher key to be randomly generated
1526                          * is supported by the algorithm chosen.
1527                          */
1528                         } else if (options->ckey_random_size != -1) {
1529                                 if (check_supported_size(options->ckey_random_size,
1530                                                 cap->sym.cipher.key_size.min,
1531                                                 cap->sym.cipher.key_size.max,
1532                                                 cap->sym.cipher.key_size.increment)
1533                                                         != 0) {
1534                                         printf("Unsupported cipher key length\n");
1535                                         return -1;
1536                                 }
1537                                 options->cipher_xform.cipher.key.length =
1538                                                         options->ckey_random_size;
1539                         /* No size provided, use minimum size. */
1540                         } else
1541                                 options->cipher_xform.cipher.key.length =
1542                                                 cap->sym.cipher.key_size.min;
1543
1544                         if (!options->ckey_param)
1545                                 generate_random_key(
1546                                         options->cipher_xform.cipher.key.data,
1547                                         options->cipher_xform.cipher.key.length);
1548
1549                 }
1550
1551                 /* Set auth parameters */
1552                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
1553                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
1554                                 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
1555                         /* Check if device supports auth algo */
1556                         i = 0;
1557                         opt_auth_algo = options->auth_xform.auth.algo;
1558                         cap = &dev_info.capabilities[i];
1559                         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1560                                 cap_auth_algo = cap->sym.auth.algo;
1561                                 if ((cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) &&
1562                                                 (cap_auth_algo == opt_auth_algo) &&
1563                                                 (check_type(options, &dev_info) == 0)) {
1564                                         break;
1565                                 }
1566                                 cap = &dev_info.capabilities[++i];
1567                         }
1568
1569                         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1570                                 printf("Algorithm %s not supported by cryptodev %u"
1571                                         " or device not of preferred type (%s)\n",
1572                                         supported_auth_algo[opt_auth_algo],
1573                                         cdev_id,
1574                                         options->string_type);
1575                                 continue;
1576                         }
1577
1578                         options->block_size = cap->sym.auth.block_size;
1579                         /*
1580                          * Check if length of provided AAD is supported
1581                          * by the algorithm chosen.
1582                          */
1583                         if (options->aad_param) {
1584                                 if (check_supported_size(options->aad.length,
1585                                                 cap->sym.auth.aad_size.min,
1586                                                 cap->sym.auth.aad_size.max,
1587                                                 cap->sym.auth.aad_size.increment)
1588                                                         != 0) {
1589                                         printf("Unsupported AAD length\n");
1590                                         return -1;
1591                                 }
1592                         /*
1593                          * Check if length of AAD to be randomly generated
1594                          * is supported by the algorithm chosen.
1595                          */
1596                         } else if (options->aad_random_size != -1) {
1597                                 if (check_supported_size(options->aad_random_size,
1598                                                 cap->sym.auth.aad_size.min,
1599                                                 cap->sym.auth.aad_size.max,
1600                                                 cap->sym.auth.aad_size.increment)
1601                                                         != 0) {
1602                                         printf("Unsupported AAD length\n");
1603                                         return -1;
1604                                 }
1605                                 options->aad.length = options->aad_random_size;
1606                         /* No size provided, use minimum size. */
1607                         } else
1608                                 options->aad.length = cap->sym.auth.aad_size.min;
1609
1610                         options->auth_xform.auth.add_auth_data_length =
1611                                                 options->aad.length;
1612
1613                         /*
1614                          * Check if length of provided auth key is supported
1615                          * by the algorithm chosen.
1616                          */
1617                         if (options->akey_param) {
1618                                 if (check_supported_size(
1619                                                 options->auth_xform.auth.key.length,
1620                                                 cap->sym.auth.key_size.min,
1621                                                 cap->sym.auth.key_size.max,
1622                                                 cap->sym.auth.key_size.increment)
1623                                                         != 0) {
1624                                         printf("Unsupported auth key length\n");
1625                                         return -1;
1626                                 }
1627                         /*
1628                          * Check if length of the auth key to be randomly generated
1629                          * is supported by the algorithm chosen.
1630                          */
1631                         } else if (options->akey_random_size != -1) {
1632                                 if (check_supported_size(options->akey_random_size,
1633                                                 cap->sym.auth.key_size.min,
1634                                                 cap->sym.auth.key_size.max,
1635                                                 cap->sym.auth.key_size.increment)
1636                                                         != 0) {
1637                                         printf("Unsupported auth key length\n");
1638                                         return -1;
1639                                 }
1640                                 options->auth_xform.auth.key.length =
1641                                                         options->akey_random_size;
1642                         /* No size provided, use minimum size. */
1643                         } else
1644                                 options->auth_xform.auth.key.length =
1645                                                 cap->sym.auth.key_size.min;
1646
1647                         if (!options->akey_param)
1648                                 generate_random_key(
1649                                         options->auth_xform.auth.key.data,
1650                                         options->auth_xform.auth.key.length);
1651
1652                         /* Check if digest size is supported by the algorithm. */
1653                         if (options->digest_size != -1) {
1654                                 if (check_supported_size(options->digest_size,
1655                                                 cap->sym.auth.digest_size.min,
1656                                                 cap->sym.auth.digest_size.max,
1657                                                 cap->sym.auth.digest_size.increment)
1658                                                         != 0) {
1659                                         printf("Unsupported digest length\n");
1660                                         return -1;
1661                                 }
1662                                 options->auth_xform.auth.digest_length =
1663                                                         options->digest_size;
1664                         /* No size provided, use minimum size. */
1665                         } else
1666                                 options->auth_xform.auth.digest_length =
1667                                                 cap->sym.auth.digest_size.min;
1668                 }
1669
1670                 retval = rte_cryptodev_configure(cdev_id, &conf);
1671                 if (retval < 0) {
1672                         printf("Failed to configure cryptodev %u", cdev_id);
1673                         return -1;
1674                 }
1675
1676                 qp_conf.nb_descriptors = 2048;
1677
1678                 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1679                                 SOCKET_ID_ANY);
1680                 if (retval < 0) {
1681                         printf("Failed to setup queue pair %u on cryptodev %u",
1682                                         0, cdev_id);
1683                         return -1;
1684                 }
1685
1686                 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1687
1688                 enabled_cdevs[cdev_id] = 1;
1689                 enabled_cdev_count++;
1690         }
1691
1692         return enabled_cdev_count;
1693 }
1694
1695 static int
1696 initialize_ports(struct l2fwd_crypto_options *options)
1697 {
1698         uint8_t last_portid, portid;
1699         unsigned enabled_portcount = 0;
1700         unsigned nb_ports = rte_eth_dev_count();
1701
1702         if (nb_ports == 0) {
1703                 printf("No Ethernet ports - bye\n");
1704                 return -1;
1705         }
1706
1707         if (nb_ports > RTE_MAX_ETHPORTS)
1708                 nb_ports = RTE_MAX_ETHPORTS;
1709
1710         /* Reset l2fwd_dst_ports */
1711         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1712                 l2fwd_dst_ports[portid] = 0;
1713
1714         for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1715                 int retval;
1716
1717                 /* Skip ports that are not enabled */
1718                 if ((options->portmask & (1 << portid)) == 0)
1719                         continue;
1720
1721                 /* init port */
1722                 printf("Initializing port %u... ", (unsigned) portid);
1723                 fflush(stdout);
1724                 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1725                 if (retval < 0) {
1726                         printf("Cannot configure device: err=%d, port=%u\n",
1727                                   retval, (unsigned) portid);
1728                         return -1;
1729                 }
1730
1731                 /* init one RX queue */
1732                 fflush(stdout);
1733                 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1734                                              rte_eth_dev_socket_id(portid),
1735                                              NULL, l2fwd_pktmbuf_pool);
1736                 if (retval < 0) {
1737                         printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1738                                         retval, (unsigned) portid);
1739                         return -1;
1740                 }
1741
1742                 /* init one TX queue on each port */
1743                 fflush(stdout);
1744                 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1745                                 rte_eth_dev_socket_id(portid),
1746                                 NULL);
1747                 if (retval < 0) {
1748                         printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1749                                 retval, (unsigned) portid);
1750
1751                         return -1;
1752                 }
1753
1754                 /* Start device */
1755                 retval = rte_eth_dev_start(portid);
1756                 if (retval < 0) {
1757                         printf("rte_eth_dev_start:err=%d, port=%u\n",
1758                                         retval, (unsigned) portid);
1759                         return -1;
1760                 }
1761
1762                 rte_eth_promiscuous_enable(portid);
1763
1764                 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1765
1766                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1767                                 (unsigned) portid,
1768                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1769                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1770                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1771                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1772                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1773                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1774
1775                 /* initialize port stats */
1776                 memset(&port_statistics, 0, sizeof(port_statistics));
1777
1778                 /* Setup port forwarding table */
1779                 if (enabled_portcount % 2) {
1780                         l2fwd_dst_ports[portid] = last_portid;
1781                         l2fwd_dst_ports[last_portid] = portid;
1782                 } else {
1783                         last_portid = portid;
1784                 }
1785
1786                 l2fwd_enabled_port_mask |= (1 << portid);
1787                 enabled_portcount++;
1788         }
1789
1790         if (enabled_portcount == 1) {
1791                 l2fwd_dst_ports[last_portid] = last_portid;
1792         } else if (enabled_portcount % 2) {
1793                 printf("odd number of ports in portmask- bye\n");
1794                 return -1;
1795         }
1796
1797         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1798
1799         return enabled_portcount;
1800 }
1801
1802 static void
1803 reserve_key_memory(struct l2fwd_crypto_options *options)
1804 {
1805         options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1806                                                 MAX_KEY_SIZE, 0);
1807         if (options->cipher_xform.cipher.key.data == NULL)
1808                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1809
1810
1811         options->auth_xform.auth.key.data = rte_malloc("auth key",
1812                                                 MAX_KEY_SIZE, 0);
1813         if (options->auth_xform.auth.key.data == NULL)
1814                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1815
1816         options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1817         if (options->iv.data == NULL)
1818                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1819         options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1820
1821         options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
1822         if (options->aad.data == NULL)
1823                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
1824         options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
1825 }
1826
1827 int
1828 main(int argc, char **argv)
1829 {
1830         struct lcore_queue_conf *qconf;
1831         struct l2fwd_crypto_options options;
1832
1833         uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1834         unsigned lcore_id, rx_lcore_id;
1835         int ret, enabled_cdevcount, enabled_portcount;
1836         uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
1837
1838         /* init EAL */
1839         ret = rte_eal_init(argc, argv);
1840         if (ret < 0)
1841                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1842         argc -= ret;
1843         argv += ret;
1844
1845         /* reserve memory for Cipher/Auth key and IV */
1846         reserve_key_memory(&options);
1847
1848         /* fill out the supported algorithm tables */
1849         fill_supported_algorithm_tables();
1850
1851         /* parse application arguments (after the EAL ones) */
1852         ret = l2fwd_crypto_parse_args(&options, argc, argv);
1853         if (ret < 0)
1854                 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1855
1856         /* create the mbuf pool */
1857         l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1858                         sizeof(struct rte_crypto_op),
1859                         RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1860         if (l2fwd_pktmbuf_pool == NULL)
1861                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1862
1863         /* create crypto op pool */
1864         l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1865                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1866                         rte_socket_id());
1867         if (l2fwd_crypto_op_pool == NULL)
1868                 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1869
1870         /* Enable Ethernet ports */
1871         enabled_portcount = initialize_ports(&options);
1872         if (enabled_portcount < 1)
1873                 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1874
1875         nb_ports = rte_eth_dev_count();
1876         /* Initialize the port/queue configuration of each logical core */
1877         for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1878                         portid < nb_ports; portid++) {
1879
1880                 /* skip ports that are not enabled */
1881                 if ((options.portmask & (1 << portid)) == 0)
1882                         continue;
1883
1884                 if (options.single_lcore && qconf == NULL) {
1885                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1886                                 rx_lcore_id++;
1887                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1888                                         rte_exit(EXIT_FAILURE,
1889                                                         "Not enough cores\n");
1890                         }
1891                 } else if (!options.single_lcore) {
1892                         /* get the lcore_id for this port */
1893                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1894                                lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1895                                options.nb_ports_per_lcore) {
1896                                 rx_lcore_id++;
1897                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1898                                         rte_exit(EXIT_FAILURE,
1899                                                         "Not enough cores\n");
1900                         }
1901                 }
1902
1903                 /* Assigned a new logical core in the loop above. */
1904                 if (qconf != &lcore_queue_conf[rx_lcore_id])
1905                         qconf = &lcore_queue_conf[rx_lcore_id];
1906
1907                 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1908                 qconf->nb_rx_ports++;
1909
1910                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1911         }
1912
1913         /* Enable Crypto devices */
1914         enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
1915                         enabled_cdevs);
1916         if (enabled_cdevcount < 0)
1917                 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
1918
1919         if (enabled_cdevcount < enabled_portcount)
1920                 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
1921                                 "has to be more or equal to number of ports (%d)\n",
1922                                 enabled_cdevcount, enabled_portcount);
1923
1924         nb_cryptodevs = rte_cryptodev_count();
1925
1926         /* Initialize the port/cryptodev configuration of each logical core */
1927         for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1928                         cdev_id < nb_cryptodevs && enabled_cdevcount;
1929                         cdev_id++) {
1930                 /* Crypto op not supported by crypto device */
1931                 if (!enabled_cdevs[cdev_id])
1932                         continue;
1933
1934                 if (options.single_lcore && qconf == NULL) {
1935                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1936                                 rx_lcore_id++;
1937                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1938                                         rte_exit(EXIT_FAILURE,
1939                                                         "Not enough cores\n");
1940                         }
1941                 } else if (!options.single_lcore) {
1942                         /* get the lcore_id for this port */
1943                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1944                                lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1945                                options.nb_ports_per_lcore) {
1946                                 rx_lcore_id++;
1947                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1948                                         rte_exit(EXIT_FAILURE,
1949                                                         "Not enough cores\n");
1950                         }
1951                 }
1952
1953                 /* Assigned a new logical core in the loop above. */
1954                 if (qconf != &lcore_queue_conf[rx_lcore_id])
1955                         qconf = &lcore_queue_conf[rx_lcore_id];
1956
1957                 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1958                 qconf->nb_crypto_devs++;
1959
1960                 enabled_cdevcount--;
1961
1962                 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1963                                 (unsigned)cdev_id);
1964         }
1965
1966         /* launch per-lcore init on every lcore */
1967         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1968                         CALL_MASTER);
1969         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1970                 if (rte_eal_wait_lcore(lcore_id) < 0)
1971                         return -1;
1972         }
1973
1974         return 0;
1975 }