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