examples/l2fwd-crypto: fix AEAD IV setting
[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 #include <fcntl.h>
49 #include <unistd.h>
50
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_common.h>
54 #include <rte_cryptodev.h>
55 #include <rte_cycles.h>
56 #include <rte_debug.h>
57 #include <rte_eal.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_interrupts.h>
61 #include <rte_ip.h>
62 #include <rte_launch.h>
63 #include <rte_lcore.h>
64 #include <rte_log.h>
65 #include <rte_malloc.h>
66 #include <rte_mbuf.h>
67 #include <rte_memcpy.h>
68 #include <rte_memory.h>
69 #include <rte_mempool.h>
70 #include <rte_memzone.h>
71 #include <rte_pci.h>
72 #include <rte_per_lcore.h>
73 #include <rte_prefetch.h>
74 #include <rte_random.h>
75 #include <rte_hexdump.h>
76
77 enum cdev_type {
78         CDEV_TYPE_ANY,
79         CDEV_TYPE_HW,
80         CDEV_TYPE_SW
81 };
82
83 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
84
85 #define NB_MBUF   8192
86
87 #define MAX_STR_LEN 32
88 #define MAX_KEY_SIZE 128
89 #define MAX_PKT_BURST 32
90 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
91 #define MAX_SESSIONS 32
92 #define SESSION_POOL_CACHE_SIZE 0
93
94 #define MAXIMUM_IV_LENGTH       16
95 #define IV_OFFSET               (sizeof(struct rte_crypto_op) + \
96                                 sizeof(struct rte_crypto_sym_op))
97
98 /*
99  * Configurable number of RX/TX ring descriptors
100  */
101 #define RTE_TEST_RX_DESC_DEFAULT 128
102 #define RTE_TEST_TX_DESC_DEFAULT 512
103
104 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
105 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
106
107 /* ethernet addresses of ports */
108 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
109
110 /* mask of enabled ports */
111 static uint64_t l2fwd_enabled_port_mask;
112 static uint64_t l2fwd_enabled_crypto_mask;
113
114 /* list of enabled ports */
115 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
116
117
118 struct pkt_buffer {
119         unsigned len;
120         struct rte_mbuf *buffer[MAX_PKT_BURST];
121 };
122
123 struct op_buffer {
124         unsigned len;
125         struct rte_crypto_op *buffer[MAX_PKT_BURST];
126 };
127
128 #define MAX_RX_QUEUE_PER_LCORE 16
129 #define MAX_TX_QUEUE_PER_PORT 16
130
131 enum l2fwd_crypto_xform_chain {
132         L2FWD_CRYPTO_CIPHER_HASH,
133         L2FWD_CRYPTO_HASH_CIPHER,
134         L2FWD_CRYPTO_CIPHER_ONLY,
135         L2FWD_CRYPTO_HASH_ONLY,
136         L2FWD_CRYPTO_AEAD
137 };
138
139 struct l2fwd_key {
140         uint8_t *data;
141         uint32_t length;
142         phys_addr_t phys_addr;
143 };
144
145 struct l2fwd_iv {
146         uint8_t *data;
147         uint16_t length;
148 };
149
150 /** l2fwd crypto application command line options */
151 struct l2fwd_crypto_options {
152         unsigned portmask;
153         unsigned nb_ports_per_lcore;
154         unsigned refresh_period;
155         unsigned single_lcore:1;
156
157         enum cdev_type type;
158         unsigned sessionless:1;
159
160         enum l2fwd_crypto_xform_chain xform_chain;
161
162         struct rte_crypto_sym_xform cipher_xform;
163         unsigned ckey_param;
164         int ckey_random_size;
165
166         struct l2fwd_iv cipher_iv;
167         unsigned int cipher_iv_param;
168         int cipher_iv_random_size;
169
170         struct rte_crypto_sym_xform auth_xform;
171         uint8_t akey_param;
172         int akey_random_size;
173
174         struct l2fwd_iv auth_iv;
175         unsigned int auth_iv_param;
176         int auth_iv_random_size;
177
178         struct rte_crypto_sym_xform aead_xform;
179         unsigned int aead_key_param;
180         int aead_key_random_size;
181
182         struct l2fwd_iv aead_iv;
183         unsigned int aead_iv_param;
184         int aead_iv_random_size;
185
186         struct l2fwd_key aad;
187         unsigned aad_param;
188         int aad_random_size;
189
190         int digest_size;
191
192         uint16_t block_size;
193         char string_type[MAX_STR_LEN];
194
195         uint64_t cryptodev_mask;
196 };
197
198 /** l2fwd crypto lcore params */
199 struct l2fwd_crypto_params {
200         uint8_t dev_id;
201         uint8_t qp_id;
202
203         unsigned digest_length;
204         unsigned block_size;
205
206         struct l2fwd_iv cipher_iv;
207         struct l2fwd_iv auth_iv;
208         struct l2fwd_iv aead_iv;
209         struct l2fwd_key aad;
210         struct rte_cryptodev_sym_session *session;
211
212         uint8_t do_cipher;
213         uint8_t do_hash;
214         uint8_t do_aead;
215         uint8_t hash_verify;
216
217         enum rte_crypto_cipher_algorithm cipher_algo;
218         enum rte_crypto_auth_algorithm auth_algo;
219         enum rte_crypto_aead_algorithm aead_algo;
220 };
221
222 /** lcore configuration */
223 struct lcore_queue_conf {
224         unsigned nb_rx_ports;
225         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
226
227         unsigned nb_crypto_devs;
228         unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
229
230         struct op_buffer op_buf[RTE_CRYPTO_MAX_DEVS];
231         struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
232 } __rte_cache_aligned;
233
234 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
235
236 static const struct rte_eth_conf port_conf = {
237         .rxmode = {
238                 .mq_mode = ETH_MQ_RX_NONE,
239                 .max_rx_pkt_len = ETHER_MAX_LEN,
240                 .split_hdr_size = 0,
241                 .header_split   = 0, /**< Header Split disabled */
242                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
243                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
244                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
245                 .hw_strip_crc   = 1, /**< CRC stripped by hardware */
246         },
247         .txmode = {
248                 .mq_mode = ETH_MQ_TX_NONE,
249         },
250 };
251
252 struct rte_mempool *l2fwd_pktmbuf_pool;
253 struct rte_mempool *l2fwd_crypto_op_pool;
254 struct rte_mempool *session_pool_socket[RTE_MAX_NUMA_NODES] = { 0 };
255
256 /* Per-port statistics struct */
257 struct l2fwd_port_statistics {
258         uint64_t tx;
259         uint64_t rx;
260
261         uint64_t crypto_enqueued;
262         uint64_t crypto_dequeued;
263
264         uint64_t dropped;
265 } __rte_cache_aligned;
266
267 struct l2fwd_crypto_statistics {
268         uint64_t enqueued;
269         uint64_t dequeued;
270
271         uint64_t errors;
272 } __rte_cache_aligned;
273
274 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
275 struct l2fwd_crypto_statistics crypto_statistics[RTE_CRYPTO_MAX_DEVS];
276
277 /* A tsc-based timer responsible for triggering statistics printout */
278 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
279 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
280
281 /* default period is 10 seconds */
282 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
283
284 /* Print out statistics on packets dropped */
285 static void
286 print_stats(void)
287 {
288         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
289         uint64_t total_packets_enqueued, total_packets_dequeued,
290                 total_packets_errors;
291         unsigned portid;
292         uint64_t cdevid;
293
294         total_packets_dropped = 0;
295         total_packets_tx = 0;
296         total_packets_rx = 0;
297         total_packets_enqueued = 0;
298         total_packets_dequeued = 0;
299         total_packets_errors = 0;
300
301         const char clr[] = { 27, '[', '2', 'J', '\0' };
302         const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
303
304                 /* Clear screen and move to top left */
305         printf("%s%s", clr, topLeft);
306
307         printf("\nPort statistics ====================================");
308
309         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
310                 /* skip disabled ports */
311                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
312                         continue;
313                 printf("\nStatistics for port %u ------------------------------"
314                            "\nPackets sent: %32"PRIu64
315                            "\nPackets received: %28"PRIu64
316                            "\nPackets dropped: %29"PRIu64,
317                            portid,
318                            port_statistics[portid].tx,
319                            port_statistics[portid].rx,
320                            port_statistics[portid].dropped);
321
322                 total_packets_dropped += port_statistics[portid].dropped;
323                 total_packets_tx += port_statistics[portid].tx;
324                 total_packets_rx += port_statistics[portid].rx;
325         }
326         printf("\nCrypto statistics ==================================");
327
328         for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
329                 /* skip disabled ports */
330                 if ((l2fwd_enabled_crypto_mask & (((uint64_t)1) << cdevid)) == 0)
331                         continue;
332                 printf("\nStatistics for cryptodev %"PRIu64
333                                 " -------------------------"
334                            "\nPackets enqueued: %28"PRIu64
335                            "\nPackets dequeued: %28"PRIu64
336                            "\nPackets errors: %30"PRIu64,
337                            cdevid,
338                            crypto_statistics[cdevid].enqueued,
339                            crypto_statistics[cdevid].dequeued,
340                            crypto_statistics[cdevid].errors);
341
342                 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
343                 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
344                 total_packets_errors += crypto_statistics[cdevid].errors;
345         }
346         printf("\nAggregate statistics ==============================="
347                    "\nTotal packets received: %22"PRIu64
348                    "\nTotal packets enqueued: %22"PRIu64
349                    "\nTotal packets dequeued: %22"PRIu64
350                    "\nTotal packets sent: %26"PRIu64
351                    "\nTotal packets dropped: %23"PRIu64
352                    "\nTotal packets crypto errors: %17"PRIu64,
353                    total_packets_rx,
354                    total_packets_enqueued,
355                    total_packets_dequeued,
356                    total_packets_tx,
357                    total_packets_dropped,
358                    total_packets_errors);
359         printf("\n====================================================\n");
360 }
361
362 static int
363 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
364                 struct l2fwd_crypto_params *cparams)
365 {
366         struct rte_crypto_op **op_buffer;
367         unsigned ret;
368
369         op_buffer = (struct rte_crypto_op **)
370                         qconf->op_buf[cparams->dev_id].buffer;
371
372         ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
373                         cparams->qp_id, op_buffer, (uint16_t) n);
374
375         crypto_statistics[cparams->dev_id].enqueued += ret;
376         if (unlikely(ret < n)) {
377                 crypto_statistics[cparams->dev_id].errors += (n - ret);
378                 do {
379                         rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
380                         rte_crypto_op_free(op_buffer[ret]);
381                 } while (++ret < n);
382         }
383
384         return 0;
385 }
386
387 static int
388 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
389                 struct l2fwd_crypto_params *cparams)
390 {
391         unsigned lcore_id, len;
392         struct lcore_queue_conf *qconf;
393
394         lcore_id = rte_lcore_id();
395
396         qconf = &lcore_queue_conf[lcore_id];
397         len = qconf->op_buf[cparams->dev_id].len;
398         qconf->op_buf[cparams->dev_id].buffer[len] = op;
399         len++;
400
401         /* enough ops to be sent */
402         if (len == MAX_PKT_BURST) {
403                 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
404                 len = 0;
405         }
406
407         qconf->op_buf[cparams->dev_id].len = len;
408         return 0;
409 }
410
411 static int
412 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
413                 struct rte_crypto_op *op,
414                 struct l2fwd_crypto_params *cparams)
415 {
416         struct ether_hdr *eth_hdr;
417         struct ipv4_hdr *ip_hdr;
418
419         uint32_t ipdata_offset, data_len;
420         uint32_t pad_len = 0;
421         char *padding;
422
423         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
424
425         if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
426                 return -1;
427
428         ipdata_offset = sizeof(struct ether_hdr);
429
430         ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
431                         ipdata_offset);
432
433         ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
434                         * IPV4_IHL_MULTIPLIER;
435
436
437         /* Zero pad data to be crypto'd so it is block aligned */
438         data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
439
440         if (cparams->do_hash && cparams->hash_verify)
441                 data_len -= cparams->digest_length;
442
443         if (cparams->do_cipher) {
444                 /*
445                  * Following algorithms are block cipher algorithms,
446                  * and might need padding
447                  */
448                 switch (cparams->cipher_algo) {
449                 case RTE_CRYPTO_CIPHER_AES_CBC:
450                 case RTE_CRYPTO_CIPHER_AES_ECB:
451                 case RTE_CRYPTO_CIPHER_DES_CBC:
452                 case RTE_CRYPTO_CIPHER_3DES_CBC:
453                 case RTE_CRYPTO_CIPHER_3DES_ECB:
454                         if (data_len % cparams->block_size)
455                                 pad_len = cparams->block_size -
456                                         (data_len % cparams->block_size);
457                         break;
458                 default:
459                         pad_len = 0;
460                 }
461
462                 if (pad_len) {
463                         padding = rte_pktmbuf_append(m, pad_len);
464                         if (unlikely(!padding))
465                                 return -1;
466
467                         data_len += pad_len;
468                         memset(padding, 0, pad_len);
469                 }
470         }
471
472         /* Set crypto operation data parameters */
473         rte_crypto_op_attach_sym_session(op, cparams->session);
474
475         if (cparams->do_hash) {
476                 if (cparams->auth_iv.length) {
477                         uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op,
478                                                 uint8_t *,
479                                                 IV_OFFSET +
480                                                 cparams->cipher_iv.length);
481                         /*
482                          * Copy IV at the end of the crypto operation,
483                          * after the cipher IV, if added
484                          */
485                         rte_memcpy(iv_ptr, cparams->auth_iv.data,
486                                         cparams->auth_iv.length);
487                 }
488                 if (!cparams->hash_verify) {
489                         /* Append space for digest to end of packet */
490                         op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
491                                 cparams->digest_length);
492                 } else {
493                         op->sym->auth.digest.data = rte_pktmbuf_mtod(m,
494                                 uint8_t *) + ipdata_offset + data_len;
495                 }
496
497                 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
498                                 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
499
500                 /* For wireless algorithms, offset/length must be in bits */
501                 if (cparams->auth_algo == RTE_CRYPTO_AUTH_SNOW3G_UIA2 ||
502                                 cparams->auth_algo == RTE_CRYPTO_AUTH_KASUMI_F9 ||
503                                 cparams->auth_algo == RTE_CRYPTO_AUTH_ZUC_EIA3) {
504                         op->sym->auth.data.offset = ipdata_offset << 3;
505                         op->sym->auth.data.length = data_len << 3;
506                 } else {
507                         op->sym->auth.data.offset = ipdata_offset;
508                         op->sym->auth.data.length = data_len;
509                 }
510         }
511
512         if (cparams->do_cipher) {
513                 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
514                                                         IV_OFFSET);
515                 /* Copy IV at the end of the crypto operation */
516                 rte_memcpy(iv_ptr, cparams->cipher_iv.data,
517                                 cparams->cipher_iv.length);
518
519                 /* For wireless algorithms, offset/length must be in bits */
520                 if (cparams->cipher_algo == RTE_CRYPTO_CIPHER_SNOW3G_UEA2 ||
521                                 cparams->cipher_algo == RTE_CRYPTO_CIPHER_KASUMI_F8 ||
522                                 cparams->cipher_algo == RTE_CRYPTO_CIPHER_ZUC_EEA3) {
523                         op->sym->cipher.data.offset = ipdata_offset << 3;
524                         op->sym->cipher.data.length = data_len << 3;
525                 } else {
526                         op->sym->cipher.data.offset = ipdata_offset;
527                         op->sym->cipher.data.length = data_len;
528                 }
529         }
530
531         if (cparams->do_aead) {
532                 uint8_t *iv_ptr = rte_crypto_op_ctod_offset(op, uint8_t *,
533                                                         IV_OFFSET);
534                 /* Copy IV at the end of the crypto operation */
535                 rte_memcpy(iv_ptr, cparams->aead_iv.data, cparams->aead_iv.length);
536
537                 op->sym->aead.data.offset = ipdata_offset;
538                 op->sym->aead.data.length = data_len;
539
540                 if (!cparams->hash_verify) {
541                         /* Append space for digest to end of packet */
542                         op->sym->aead.digest.data = (uint8_t *)rte_pktmbuf_append(m,
543                                 cparams->digest_length);
544                 } else {
545                         op->sym->aead.digest.data = rte_pktmbuf_mtod(m,
546                                 uint8_t *) + ipdata_offset + data_len;
547                 }
548
549                 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
550                                 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
551
552                 if (cparams->aad.length) {
553                         op->sym->aead.aad.data = cparams->aad.data;
554                         op->sym->aead.aad.phys_addr = cparams->aad.phys_addr;
555                 }
556         }
557
558         op->sym->m_src = m;
559
560         return l2fwd_crypto_enqueue(op, cparams);
561 }
562
563
564 /* Send the burst of packets on an output interface */
565 static int
566 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
567                 uint8_t port)
568 {
569         struct rte_mbuf **pkt_buffer;
570         unsigned ret;
571
572         pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
573
574         ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
575         port_statistics[port].tx += ret;
576         if (unlikely(ret < n)) {
577                 port_statistics[port].dropped += (n - ret);
578                 do {
579                         rte_pktmbuf_free(pkt_buffer[ret]);
580                 } while (++ret < n);
581         }
582
583         return 0;
584 }
585
586 /* Enqueue packets for TX and prepare them to be sent */
587 static int
588 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
589 {
590         unsigned lcore_id, len;
591         struct lcore_queue_conf *qconf;
592
593         lcore_id = rte_lcore_id();
594
595         qconf = &lcore_queue_conf[lcore_id];
596         len = qconf->pkt_buf[port].len;
597         qconf->pkt_buf[port].buffer[len] = m;
598         len++;
599
600         /* enough pkts to be sent */
601         if (unlikely(len == MAX_PKT_BURST)) {
602                 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
603                 len = 0;
604         }
605
606         qconf->pkt_buf[port].len = len;
607         return 0;
608 }
609
610 static void
611 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
612 {
613         struct ether_hdr *eth;
614         void *tmp;
615         unsigned dst_port;
616
617         dst_port = l2fwd_dst_ports[portid];
618         eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
619
620         /* 02:00:00:00:00:xx */
621         tmp = &eth->d_addr.addr_bytes[0];
622         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
623
624         /* src addr */
625         ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
626
627         l2fwd_send_packet(m, (uint8_t) dst_port);
628 }
629
630 /** Generate random key */
631 static void
632 generate_random_key(uint8_t *key, unsigned length)
633 {
634         int fd;
635         int ret;
636
637         fd = open("/dev/urandom", O_RDONLY);
638         if (fd < 0)
639                 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
640
641         ret = read(fd, key, length);
642         close(fd);
643
644         if (ret != (signed)length)
645                 rte_exit(EXIT_FAILURE, "Failed to generate random key\n");
646 }
647
648 static struct rte_cryptodev_sym_session *
649 initialize_crypto_session(struct l2fwd_crypto_options *options, uint8_t cdev_id)
650 {
651         struct rte_crypto_sym_xform *first_xform;
652         struct rte_cryptodev_sym_session *session;
653         uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
654         struct rte_mempool *sess_mp = session_pool_socket[socket_id];
655
656         if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
657                 first_xform = &options->aead_xform;
658         } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
659                 first_xform = &options->cipher_xform;
660                 first_xform->next = &options->auth_xform;
661         } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
662                 first_xform = &options->auth_xform;
663                 first_xform->next = &options->cipher_xform;
664         } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
665                 first_xform = &options->cipher_xform;
666         } else {
667                 first_xform = &options->auth_xform;
668         }
669
670         session = rte_cryptodev_sym_session_create(sess_mp);
671
672         if (session == NULL)
673                 return NULL;
674
675         if (rte_cryptodev_sym_session_init(cdev_id, session,
676                                 first_xform, sess_mp) < 0)
677                 return NULL;
678
679         return session;
680 }
681
682 static void
683 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
684
685 /* main processing loop */
686 static void
687 l2fwd_main_loop(struct l2fwd_crypto_options *options)
688 {
689         struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
690         struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
691
692         unsigned lcore_id = rte_lcore_id();
693         uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
694         unsigned i, j, portid, nb_rx, len;
695         struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
696         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
697                         US_PER_S * BURST_TX_DRAIN_US;
698         struct l2fwd_crypto_params *cparams;
699         struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
700         struct rte_cryptodev_sym_session *session;
701
702         if (qconf->nb_rx_ports == 0) {
703                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
704                 return;
705         }
706
707         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
708
709         for (i = 0; i < qconf->nb_rx_ports; i++) {
710
711                 portid = qconf->rx_port_list[i];
712                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
713                         portid);
714         }
715
716         for (i = 0; i < qconf->nb_crypto_devs; i++) {
717                 port_cparams[i].do_cipher = 0;
718                 port_cparams[i].do_hash = 0;
719                 port_cparams[i].do_aead = 0;
720
721                 switch (options->xform_chain) {
722                 case L2FWD_CRYPTO_AEAD:
723                         port_cparams[i].do_aead = 1;
724                         break;
725                 case L2FWD_CRYPTO_CIPHER_HASH:
726                 case L2FWD_CRYPTO_HASH_CIPHER:
727                         port_cparams[i].do_cipher = 1;
728                         port_cparams[i].do_hash = 1;
729                         break;
730                 case L2FWD_CRYPTO_HASH_ONLY:
731                         port_cparams[i].do_hash = 1;
732                         break;
733                 case L2FWD_CRYPTO_CIPHER_ONLY:
734                         port_cparams[i].do_cipher = 1;
735                         break;
736                 }
737
738                 port_cparams[i].dev_id = qconf->cryptodev_list[i];
739                 port_cparams[i].qp_id = 0;
740
741                 port_cparams[i].block_size = options->block_size;
742
743                 if (port_cparams[i].do_hash) {
744                         port_cparams[i].auth_iv.data = options->auth_iv.data;
745                         port_cparams[i].auth_iv.length = options->auth_iv.length;
746                         if (!options->auth_iv_param)
747                                 generate_random_key(port_cparams[i].auth_iv.data,
748                                                 port_cparams[i].auth_iv.length);
749                         if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_VERIFY)
750                                 port_cparams[i].hash_verify = 1;
751                         else
752                                 port_cparams[i].hash_verify = 0;
753
754                         port_cparams[i].auth_algo = options->auth_xform.auth.algo;
755                         port_cparams[i].digest_length =
756                                         options->auth_xform.auth.digest_length;
757                         /* Set IV parameters */
758                         if (options->auth_iv.length) {
759                                 options->auth_xform.auth.iv.offset =
760                                         IV_OFFSET + options->cipher_iv.length;
761                                 options->auth_xform.auth.iv.length =
762                                         options->auth_iv.length;
763                         }
764                 }
765
766                 if (port_cparams[i].do_aead) {
767                         port_cparams[i].aead_iv.data = options->aead_iv.data;
768                         port_cparams[i].aead_iv.length = options->aead_iv.length;
769                         if (!options->aead_iv_param)
770                                 generate_random_key(port_cparams[i].aead_iv.data,
771                                                 port_cparams[i].aead_iv.length);
772                         port_cparams[i].aead_algo = options->aead_xform.aead.algo;
773                         port_cparams[i].digest_length =
774                                         options->aead_xform.aead.digest_length;
775                         if (options->aead_xform.aead.aad_length) {
776                                 port_cparams[i].aad.data = options->aad.data;
777                                 port_cparams[i].aad.phys_addr = options->aad.phys_addr;
778                                 port_cparams[i].aad.length = options->aad.length;
779                                 if (!options->aad_param)
780                                         generate_random_key(port_cparams[i].aad.data,
781                                                 port_cparams[i].aad.length);
782
783                         } else
784                                 port_cparams[i].aad.length = 0;
785
786                         if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_DECRYPT)
787                                 port_cparams[i].hash_verify = 1;
788                         else
789                                 port_cparams[i].hash_verify = 0;
790
791                         /* Set IV parameters */
792                         options->aead_xform.aead.iv.offset = IV_OFFSET;
793                         options->aead_xform.aead.iv.length = options->aead_iv.length;
794                 }
795
796                 if (port_cparams[i].do_cipher) {
797                         port_cparams[i].cipher_iv.data = options->cipher_iv.data;
798                         port_cparams[i].cipher_iv.length = options->cipher_iv.length;
799                         if (!options->cipher_iv_param)
800                                 generate_random_key(port_cparams[i].cipher_iv.data,
801                                                 port_cparams[i].cipher_iv.length);
802
803                         port_cparams[i].cipher_algo = options->cipher_xform.cipher.algo;
804                         /* Set IV parameters */
805                         options->cipher_xform.cipher.iv.offset = IV_OFFSET;
806                         options->cipher_xform.cipher.iv.length =
807                                                 options->cipher_iv.length;
808                 }
809
810                 session = initialize_crypto_session(options,
811                                 port_cparams[i].dev_id);
812                 if (session == NULL)
813                         rte_exit(EXIT_FAILURE, "Failed to initialize crypto session\n");
814
815                 port_cparams[i].session = session;
816
817                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
818                                 port_cparams[i].dev_id);
819         }
820
821         l2fwd_crypto_options_print(options);
822
823         /*
824          * Initialize previous tsc timestamp before the loop,
825          * to avoid showing the port statistics immediately,
826          * so user can see the crypto information.
827          */
828         prev_tsc = rte_rdtsc();
829         while (1) {
830
831                 cur_tsc = rte_rdtsc();
832
833                 /*
834                  * Crypto device/TX burst queue drain
835                  */
836                 diff_tsc = cur_tsc - prev_tsc;
837                 if (unlikely(diff_tsc > drain_tsc)) {
838                         /* Enqueue all crypto ops remaining in buffers */
839                         for (i = 0; i < qconf->nb_crypto_devs; i++) {
840                                 cparams = &port_cparams[i];
841                                 len = qconf->op_buf[cparams->dev_id].len;
842                                 l2fwd_crypto_send_burst(qconf, len, cparams);
843                                 qconf->op_buf[cparams->dev_id].len = 0;
844                         }
845                         /* Transmit all packets remaining in buffers */
846                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
847                                 if (qconf->pkt_buf[portid].len == 0)
848                                         continue;
849                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
850                                                  qconf->pkt_buf[portid].len,
851                                                  (uint8_t) portid);
852                                 qconf->pkt_buf[portid].len = 0;
853                         }
854
855                         /* if timer is enabled */
856                         if (timer_period > 0) {
857
858                                 /* advance the timer */
859                                 timer_tsc += diff_tsc;
860
861                                 /* if timer has reached its timeout */
862                                 if (unlikely(timer_tsc >=
863                                                 (uint64_t)timer_period)) {
864
865                                         /* do this only on master core */
866                                         if (lcore_id == rte_get_master_lcore()
867                                                 && options->refresh_period) {
868                                                 print_stats();
869                                                 timer_tsc = 0;
870                                         }
871                                 }
872                         }
873
874                         prev_tsc = cur_tsc;
875                 }
876
877                 /*
878                  * Read packet from RX queues
879                  */
880                 for (i = 0; i < qconf->nb_rx_ports; i++) {
881                         portid = qconf->rx_port_list[i];
882
883                         cparams = &port_cparams[i];
884
885                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
886                                                  pkts_burst, MAX_PKT_BURST);
887
888                         port_statistics[portid].rx += nb_rx;
889
890                         if (nb_rx) {
891                                 /*
892                                  * If we can't allocate a crypto_ops, then drop
893                                  * the rest of the burst and dequeue and
894                                  * process the packets to free offload structs
895                                  */
896                                 if (rte_crypto_op_bulk_alloc(
897                                                 l2fwd_crypto_op_pool,
898                                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
899                                                 ops_burst, nb_rx) !=
900                                                                 nb_rx) {
901                                         for (j = 0; j < nb_rx; j++)
902                                                 rte_pktmbuf_free(pkts_burst[j]);
903
904                                         nb_rx = 0;
905                                 }
906
907                                 /* Enqueue packets from Crypto device*/
908                                 for (j = 0; j < nb_rx; j++) {
909                                         m = pkts_burst[j];
910
911                                         l2fwd_simple_crypto_enqueue(m,
912                                                         ops_burst[j], cparams);
913                                 }
914                         }
915
916                         /* Dequeue packets from Crypto device */
917                         do {
918                                 nb_rx = rte_cryptodev_dequeue_burst(
919                                                 cparams->dev_id, cparams->qp_id,
920                                                 ops_burst, MAX_PKT_BURST);
921
922                                 crypto_statistics[cparams->dev_id].dequeued +=
923                                                 nb_rx;
924
925                                 /* Forward crypto'd packets */
926                                 for (j = 0; j < nb_rx; j++) {
927                                         m = ops_burst[j]->sym->m_src;
928
929                                         rte_crypto_op_free(ops_burst[j]);
930                                         l2fwd_simple_forward(m, portid);
931                                 }
932                         } while (nb_rx == MAX_PKT_BURST);
933                 }
934         }
935 }
936
937 static int
938 l2fwd_launch_one_lcore(void *arg)
939 {
940         l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
941         return 0;
942 }
943
944 /* Display command line arguments usage */
945 static void
946 l2fwd_crypto_usage(const char *prgname)
947 {
948         printf("%s [EAL options] --\n"
949                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
950                 "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
951                 "  -s manage all ports from single lcore\n"
952                 "  -T PERIOD: statistics will be refreshed each PERIOD seconds"
953                 " (0 to disable, 10 default, 86400 maximum)\n"
954
955                 "  --cdev_type HW / SW / ANY\n"
956                 "  --chain HASH_CIPHER / CIPHER_HASH / CIPHER_ONLY /"
957                 " HASH_ONLY / AEAD\n"
958
959                 "  --cipher_algo ALGO\n"
960                 "  --cipher_op ENCRYPT / DECRYPT\n"
961                 "  --cipher_key KEY (bytes separated with \":\")\n"
962                 "  --cipher_key_random_size SIZE: size of cipher key when generated randomly\n"
963                 "  --cipher_iv IV (bytes separated with \":\")\n"
964                 "  --cipher_iv_random_size SIZE: size of cipher IV when generated randomly\n"
965
966                 "  --auth_algo ALGO\n"
967                 "  --auth_op GENERATE / VERIFY\n"
968                 "  --auth_key KEY (bytes separated with \":\")\n"
969                 "  --auth_key_random_size SIZE: size of auth key when generated randomly\n"
970                 "  --auth_iv IV (bytes separated with \":\")\n"
971                 "  --auth_iv_random_size SIZE: size of auth IV when generated randomly\n"
972
973                 "  --aead_algo ALGO\n"
974                 "  --aead_op ENCRYPT / DECRYPT\n"
975                 "  --aead_key KEY (bytes separated with \":\")\n"
976                 "  --aead_key_random_size SIZE: size of AEAD key when generated randomly\n"
977                 "  --aead_iv IV (bytes separated with \":\")\n"
978                 "  --aead_iv_random_size SIZE: size of AEAD IV when generated randomly\n"
979                 "  --aad AAD (bytes separated with \":\")\n"
980                 "  --aad_random_size SIZE: size of AAD when generated randomly\n"
981
982                 "  --digest_size SIZE: size of digest to be generated/verified\n"
983
984                 "  --sessionless\n"
985                 "  --cryptodev_mask MASK: hexadecimal bitmask of crypto devices to configure\n",
986                prgname);
987 }
988
989 /** Parse crypto device type command line argument */
990 static int
991 parse_cryptodev_type(enum cdev_type *type, char *optarg)
992 {
993         if (strcmp("HW", optarg) == 0) {
994                 *type = CDEV_TYPE_HW;
995                 return 0;
996         } else if (strcmp("SW", optarg) == 0) {
997                 *type = CDEV_TYPE_SW;
998                 return 0;
999         } else if (strcmp("ANY", optarg) == 0) {
1000                 *type = CDEV_TYPE_ANY;
1001                 return 0;
1002         }
1003
1004         return -1;
1005 }
1006
1007 /** Parse crypto chain xform command line argument */
1008 static int
1009 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
1010 {
1011         if (strcmp("CIPHER_HASH", optarg) == 0) {
1012                 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1013                 return 0;
1014         } else if (strcmp("HASH_CIPHER", optarg) == 0) {
1015                 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
1016                 return 0;
1017         } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
1018                 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
1019                 return 0;
1020         } else if (strcmp("HASH_ONLY", optarg) == 0) {
1021                 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
1022                 return 0;
1023         } else if (strcmp("AEAD", optarg) == 0) {
1024                 options->xform_chain = L2FWD_CRYPTO_AEAD;
1025                 return 0;
1026         }
1027
1028         return -1;
1029 }
1030
1031 /** Parse crypto cipher algo option command line argument */
1032 static int
1033 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
1034 {
1035
1036         if (rte_cryptodev_get_cipher_algo_enum(algo, optarg) < 0) {
1037                 RTE_LOG(ERR, USER1, "Cipher algorithm specified "
1038                                 "not supported!\n");
1039                 return -1;
1040         }
1041
1042         return 0;
1043 }
1044
1045 /** Parse crypto cipher operation command line argument */
1046 static int
1047 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
1048 {
1049         if (strcmp("ENCRYPT", optarg) == 0) {
1050                 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1051                 return 0;
1052         } else if (strcmp("DECRYPT", optarg) == 0) {
1053                 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
1054                 return 0;
1055         }
1056
1057         printf("Cipher operation not supported!\n");
1058         return -1;
1059 }
1060
1061 /** Parse crypto key command line argument */
1062 static int
1063 parse_key(uint8_t *data, char *input_arg)
1064 {
1065         unsigned byte_count;
1066         char *token;
1067
1068         for (byte_count = 0, token = strtok(input_arg, ":");
1069                         (byte_count < MAX_KEY_SIZE) && (token != NULL);
1070                         token = strtok(NULL, ":")) {
1071
1072                 int number = (int)strtol(token, NULL, 16);
1073
1074                 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
1075                         return -1;
1076
1077                 data[byte_count++] = (uint8_t)number;
1078         }
1079
1080         return byte_count;
1081 }
1082
1083 /** Parse size param*/
1084 static int
1085 parse_size(int *size, const char *q_arg)
1086 {
1087         char *end = NULL;
1088         unsigned long n;
1089
1090         /* parse hexadecimal string */
1091         n = strtoul(q_arg, &end, 10);
1092         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1093                 n = 0;
1094
1095         if (n == 0) {
1096                 printf("invalid size\n");
1097                 return -1;
1098         }
1099
1100         *size = n;
1101         return 0;
1102 }
1103
1104 /** Parse crypto cipher operation command line argument */
1105 static int
1106 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
1107 {
1108         if (rte_cryptodev_get_auth_algo_enum(algo, optarg) < 0) {
1109                 RTE_LOG(ERR, USER1, "Authentication algorithm specified "
1110                                 "not supported!\n");
1111                 return -1;
1112         }
1113
1114         return 0;
1115 }
1116
1117 static int
1118 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
1119 {
1120         if (strcmp("VERIFY", optarg) == 0) {
1121                 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
1122                 return 0;
1123         } else if (strcmp("GENERATE", optarg) == 0) {
1124                 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
1125                 return 0;
1126         }
1127
1128         printf("Authentication operation specified not supported!\n");
1129         return -1;
1130 }
1131
1132 static int
1133 parse_aead_algo(enum rte_crypto_aead_algorithm *algo, char *optarg)
1134 {
1135         if (rte_cryptodev_get_aead_algo_enum(algo, optarg) < 0) {
1136                 RTE_LOG(ERR, USER1, "AEAD algorithm specified "
1137                                 "not supported!\n");
1138                 return -1;
1139         }
1140
1141         return 0;
1142 }
1143
1144 static int
1145 parse_aead_op(enum rte_crypto_aead_operation *op, char *optarg)
1146 {
1147         if (strcmp("ENCRYPT", optarg) == 0) {
1148                 *op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1149                 return 0;
1150         } else if (strcmp("DECRYPT", optarg) == 0) {
1151                 *op = RTE_CRYPTO_AEAD_OP_DECRYPT;
1152                 return 0;
1153         }
1154
1155         printf("AEAD operation specified not supported!\n");
1156         return -1;
1157 }
1158 static int
1159 parse_cryptodev_mask(struct l2fwd_crypto_options *options,
1160                 const char *q_arg)
1161 {
1162         char *end = NULL;
1163         uint64_t pm;
1164
1165         /* parse hexadecimal string */
1166         pm = strtoul(q_arg, &end, 16);
1167         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1168                 pm = 0;
1169
1170         options->cryptodev_mask = pm;
1171         if (options->cryptodev_mask == 0) {
1172                 printf("invalid cryptodev_mask specified\n");
1173                 return -1;
1174         }
1175
1176         return 0;
1177 }
1178
1179 /** Parse long options */
1180 static int
1181 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
1182                 struct option *lgopts, int option_index)
1183 {
1184         int retval;
1185
1186         if (strcmp(lgopts[option_index].name, "cdev_type") == 0) {
1187                 retval = parse_cryptodev_type(&options->type, optarg);
1188                 if (retval == 0)
1189                         snprintf(options->string_type, MAX_STR_LEN,
1190                                 "%s", optarg);
1191                 return retval;
1192         }
1193
1194         else if (strcmp(lgopts[option_index].name, "chain") == 0)
1195                 return parse_crypto_opt_chain(options, optarg);
1196
1197         /* Cipher options */
1198         else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
1199                 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
1200                                 optarg);
1201
1202         else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
1203                 return parse_cipher_op(&options->cipher_xform.cipher.op,
1204                                 optarg);
1205
1206         else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
1207                 options->ckey_param = 1;
1208                 options->cipher_xform.cipher.key.length =
1209                         parse_key(options->cipher_xform.cipher.key.data, optarg);
1210                 if (options->cipher_xform.cipher.key.length > 0)
1211                         return 0;
1212                 else
1213                         return -1;
1214         }
1215
1216         else if (strcmp(lgopts[option_index].name, "cipher_key_random_size") == 0)
1217                 return parse_size(&options->ckey_random_size, optarg);
1218
1219         else if (strcmp(lgopts[option_index].name, "cipher_iv") == 0) {
1220                 options->cipher_iv_param = 1;
1221                 options->cipher_iv.length =
1222                         parse_key(options->cipher_iv.data, optarg);
1223                 if (options->cipher_iv.length > 0)
1224                         return 0;
1225                 else
1226                         return -1;
1227         }
1228
1229         else if (strcmp(lgopts[option_index].name, "cipher_iv_random_size") == 0)
1230                 return parse_size(&options->cipher_iv_random_size, optarg);
1231
1232         /* Authentication options */
1233         else if (strcmp(lgopts[option_index].name, "auth_algo") == 0) {
1234                 return parse_auth_algo(&options->auth_xform.auth.algo,
1235                                 optarg);
1236         }
1237
1238         else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
1239                 return parse_auth_op(&options->auth_xform.auth.op,
1240                                 optarg);
1241
1242         else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
1243                 options->akey_param = 1;
1244                 options->auth_xform.auth.key.length =
1245                         parse_key(options->auth_xform.auth.key.data, optarg);
1246                 if (options->auth_xform.auth.key.length > 0)
1247                         return 0;
1248                 else
1249                         return -1;
1250         }
1251
1252         else if (strcmp(lgopts[option_index].name, "auth_key_random_size") == 0) {
1253                 return parse_size(&options->akey_random_size, optarg);
1254         }
1255
1256         else if (strcmp(lgopts[option_index].name, "auth_iv") == 0) {
1257                 options->auth_iv_param = 1;
1258                 options->auth_iv.length =
1259                         parse_key(options->auth_iv.data, optarg);
1260                 if (options->auth_iv.length > 0)
1261                         return 0;
1262                 else
1263                         return -1;
1264         }
1265
1266         else if (strcmp(lgopts[option_index].name, "auth_iv_random_size") == 0)
1267                 return parse_size(&options->auth_iv_random_size, optarg);
1268
1269         /* AEAD options */
1270         else if (strcmp(lgopts[option_index].name, "aead_algo") == 0) {
1271                 return parse_aead_algo(&options->aead_xform.aead.algo,
1272                                 optarg);
1273         }
1274
1275         else if (strcmp(lgopts[option_index].name, "aead_op") == 0)
1276                 return parse_aead_op(&options->aead_xform.aead.op,
1277                                 optarg);
1278
1279         else if (strcmp(lgopts[option_index].name, "aead_key") == 0) {
1280                 options->aead_key_param = 1;
1281                 options->aead_xform.aead.key.length =
1282                         parse_key(options->aead_xform.aead.key.data, optarg);
1283                 if (options->aead_xform.aead.key.length > 0)
1284                         return 0;
1285                 else
1286                         return -1;
1287         }
1288
1289         else if (strcmp(lgopts[option_index].name, "aead_key_random_size") == 0)
1290                 return parse_size(&options->aead_key_random_size, optarg);
1291
1292
1293         else if (strcmp(lgopts[option_index].name, "aead_iv") == 0) {
1294                 options->aead_iv_param = 1;
1295                 options->aead_iv.length =
1296                         parse_key(options->aead_iv.data, optarg);
1297                 if (options->aead_iv.length > 0)
1298                         return 0;
1299                 else
1300                         return -1;
1301         }
1302
1303         else if (strcmp(lgopts[option_index].name, "aead_iv_random_size") == 0)
1304                 return parse_size(&options->aead_iv_random_size, optarg);
1305
1306         else if (strcmp(lgopts[option_index].name, "aad") == 0) {
1307                 options->aad_param = 1;
1308                 options->aad.length =
1309                         parse_key(options->aad.data, optarg);
1310                 if (options->aad.length > 0)
1311                         return 0;
1312                 else
1313                         return -1;
1314         }
1315
1316         else if (strcmp(lgopts[option_index].name, "aad_random_size") == 0) {
1317                 return parse_size(&options->aad_random_size, optarg);
1318         }
1319
1320         else if (strcmp(lgopts[option_index].name, "digest_size") == 0) {
1321                 return parse_size(&options->digest_size, optarg);
1322         }
1323
1324         else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
1325                 options->sessionless = 1;
1326                 return 0;
1327         }
1328
1329         else if (strcmp(lgopts[option_index].name, "cryptodev_mask") == 0)
1330                 return parse_cryptodev_mask(options, optarg);
1331
1332         return -1;
1333 }
1334
1335 /** Parse port mask */
1336 static int
1337 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
1338                 const char *q_arg)
1339 {
1340         char *end = NULL;
1341         unsigned long pm;
1342
1343         /* parse hexadecimal string */
1344         pm = strtoul(q_arg, &end, 16);
1345         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
1346                 pm = 0;
1347
1348         options->portmask = pm;
1349         if (options->portmask == 0) {
1350                 printf("invalid portmask specified\n");
1351                 return -1;
1352         }
1353
1354         return pm;
1355 }
1356
1357 /** Parse number of queues */
1358 static int
1359 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
1360                 const char *q_arg)
1361 {
1362         char *end = NULL;
1363         unsigned long n;
1364
1365         /* parse hexadecimal string */
1366         n = strtoul(q_arg, &end, 10);
1367         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1368                 n = 0;
1369         else if (n >= MAX_RX_QUEUE_PER_LCORE)
1370                 n = 0;
1371
1372         options->nb_ports_per_lcore = n;
1373         if (options->nb_ports_per_lcore == 0) {
1374                 printf("invalid number of ports selected\n");
1375                 return -1;
1376         }
1377
1378         return 0;
1379 }
1380
1381 /** Parse timer period */
1382 static int
1383 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
1384                 const char *q_arg)
1385 {
1386         char *end = NULL;
1387         unsigned long n;
1388
1389         /* parse number string */
1390         n = (unsigned)strtol(q_arg, &end, 10);
1391         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1392                 n = 0;
1393
1394         if (n >= MAX_TIMER_PERIOD) {
1395                 printf("Warning refresh period specified %lu is greater than "
1396                                 "max value %lu! using max value",
1397                                 n, MAX_TIMER_PERIOD);
1398                 n = MAX_TIMER_PERIOD;
1399         }
1400
1401         options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1402
1403         return 0;
1404 }
1405
1406 /** Generate default options for application */
1407 static void
1408 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1409 {
1410         options->portmask = 0xffffffff;
1411         options->nb_ports_per_lcore = 1;
1412         options->refresh_period = 10000;
1413         options->single_lcore = 0;
1414         options->sessionless = 0;
1415
1416         options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1417
1418         /* Cipher Data */
1419         options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1420         options->cipher_xform.next = NULL;
1421         options->ckey_param = 0;
1422         options->ckey_random_size = -1;
1423         options->cipher_xform.cipher.key.length = 0;
1424         options->cipher_iv_param = 0;
1425         options->cipher_iv_random_size = -1;
1426         options->cipher_iv.length = 0;
1427
1428         options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1429         options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1430
1431         /* Authentication Data */
1432         options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1433         options->auth_xform.next = NULL;
1434         options->akey_param = 0;
1435         options->akey_random_size = -1;
1436         options->auth_xform.auth.key.length = 0;
1437         options->auth_iv_param = 0;
1438         options->auth_iv_random_size = -1;
1439         options->auth_iv.length = 0;
1440
1441         options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1442         options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
1443
1444         /* AEAD Data */
1445         options->aead_xform.type = RTE_CRYPTO_SYM_XFORM_AEAD;
1446         options->aead_xform.next = NULL;
1447         options->aead_key_param = 0;
1448         options->aead_key_random_size = -1;
1449         options->aead_xform.aead.key.length = 0;
1450         options->aead_iv_param = 0;
1451         options->aead_iv_random_size = -1;
1452         options->aead_iv.length = 0;
1453
1454         options->auth_xform.aead.algo = RTE_CRYPTO_AEAD_AES_GCM;
1455         options->auth_xform.aead.op = RTE_CRYPTO_AEAD_OP_ENCRYPT;
1456
1457         options->aad_param = 0;
1458         options->aad_random_size = -1;
1459         options->aad.length = 0;
1460
1461         options->digest_size = -1;
1462
1463         options->type = CDEV_TYPE_ANY;
1464         options->cryptodev_mask = UINT64_MAX;
1465 }
1466
1467 static void
1468 display_cipher_info(struct l2fwd_crypto_options *options)
1469 {
1470         printf("\n---- Cipher information ---\n");
1471         printf("Algorithm: %s\n",
1472                 rte_crypto_cipher_algorithm_strings[options->cipher_xform.cipher.algo]);
1473         rte_hexdump(stdout, "Cipher key:",
1474                         options->cipher_xform.cipher.key.data,
1475                         options->cipher_xform.cipher.key.length);
1476         rte_hexdump(stdout, "IV:", options->cipher_iv.data, options->cipher_iv.length);
1477 }
1478
1479 static void
1480 display_auth_info(struct l2fwd_crypto_options *options)
1481 {
1482         printf("\n---- Authentication information ---\n");
1483         printf("Algorithm: %s\n",
1484                 rte_crypto_auth_algorithm_strings[options->auth_xform.auth.algo]);
1485         rte_hexdump(stdout, "Auth key:",
1486                         options->auth_xform.auth.key.data,
1487                         options->auth_xform.auth.key.length);
1488         rte_hexdump(stdout, "IV:", options->auth_iv.data, options->auth_iv.length);
1489 }
1490
1491 static void
1492 display_aead_info(struct l2fwd_crypto_options *options)
1493 {
1494         printf("\n---- AEAD information ---\n");
1495         printf("Algorithm: %s\n",
1496                 rte_crypto_aead_algorithm_strings[options->aead_xform.aead.algo]);
1497         rte_hexdump(stdout, "AEAD key:",
1498                         options->aead_xform.aead.key.data,
1499                         options->aead_xform.aead.key.length);
1500         rte_hexdump(stdout, "IV:", options->aead_iv.data, options->aead_iv.length);
1501         rte_hexdump(stdout, "AAD:", options->aad.data, options->aad.length);
1502 }
1503
1504 static void
1505 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1506 {
1507         char string_cipher_op[MAX_STR_LEN];
1508         char string_auth_op[MAX_STR_LEN];
1509         char string_aead_op[MAX_STR_LEN];
1510
1511         if (options->cipher_xform.cipher.op == RTE_CRYPTO_CIPHER_OP_ENCRYPT)
1512                 strcpy(string_cipher_op, "Encrypt");
1513         else
1514                 strcpy(string_cipher_op, "Decrypt");
1515
1516         if (options->auth_xform.auth.op == RTE_CRYPTO_AUTH_OP_GENERATE)
1517                 strcpy(string_auth_op, "Auth generate");
1518         else
1519                 strcpy(string_auth_op, "Auth verify");
1520
1521         if (options->aead_xform.aead.op == RTE_CRYPTO_AEAD_OP_ENCRYPT)
1522                 strcpy(string_aead_op, "Authenticated encryption");
1523         else
1524                 strcpy(string_aead_op, "Authenticated decryption");
1525
1526
1527         printf("Options:-\nn");
1528         printf("portmask: %x\n", options->portmask);
1529         printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1530         printf("refresh period : %u\n", options->refresh_period);
1531         printf("single lcore mode: %s\n",
1532                         options->single_lcore ? "enabled" : "disabled");
1533         printf("stats_printing: %s\n",
1534                         options->refresh_period == 0 ? "disabled" : "enabled");
1535
1536         printf("sessionless crypto: %s\n",
1537                         options->sessionless ? "enabled" : "disabled");
1538
1539         if (options->ckey_param && (options->ckey_random_size != -1))
1540                 printf("Cipher key already parsed, ignoring size of random key\n");
1541
1542         if (options->akey_param && (options->akey_random_size != -1))
1543                 printf("Auth key already parsed, ignoring size of random key\n");
1544
1545         if (options->cipher_iv_param && (options->cipher_iv_random_size != -1))
1546                 printf("Cipher IV already parsed, ignoring size of random IV\n");
1547
1548         if (options->auth_iv_param && (options->auth_iv_random_size != -1))
1549                 printf("Auth IV already parsed, ignoring size of random IV\n");
1550
1551         if (options->aad_param && (options->aad_random_size != -1))
1552                 printf("AAD already parsed, ignoring size of random AAD\n");
1553
1554         printf("\nCrypto chain: ");
1555         switch (options->xform_chain) {
1556         case L2FWD_CRYPTO_AEAD:
1557                 printf("Input --> %s --> Output\n", string_aead_op);
1558                 display_aead_info(options);
1559                 break;
1560         case L2FWD_CRYPTO_CIPHER_HASH:
1561                 printf("Input --> %s --> %s --> Output\n",
1562                         string_cipher_op, string_auth_op);
1563                 display_cipher_info(options);
1564                 display_auth_info(options);
1565                 break;
1566         case L2FWD_CRYPTO_HASH_CIPHER:
1567                 printf("Input --> %s --> %s --> Output\n",
1568                         string_auth_op, string_cipher_op);
1569                 display_cipher_info(options);
1570                 display_auth_info(options);
1571                 break;
1572         case L2FWD_CRYPTO_HASH_ONLY:
1573                 printf("Input --> %s --> Output\n", string_auth_op);
1574                 display_auth_info(options);
1575                 break;
1576         case L2FWD_CRYPTO_CIPHER_ONLY:
1577                 printf("Input --> %s --> Output\n", string_cipher_op);
1578                 display_cipher_info(options);
1579                 break;
1580         }
1581 }
1582
1583 /* Parse the argument given in the command line of the application */
1584 static int
1585 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1586                 int argc, char **argv)
1587 {
1588         int opt, retval, option_index;
1589         char **argvopt = argv, *prgname = argv[0];
1590
1591         static struct option lgopts[] = {
1592                         { "sessionless", no_argument, 0, 0 },
1593
1594                         { "cdev_type", required_argument, 0, 0 },
1595                         { "chain", required_argument, 0, 0 },
1596
1597                         { "cipher_algo", required_argument, 0, 0 },
1598                         { "cipher_op", required_argument, 0, 0 },
1599                         { "cipher_key", required_argument, 0, 0 },
1600                         { "cipher_key_random_size", required_argument, 0, 0 },
1601                         { "cipher_iv", required_argument, 0, 0 },
1602                         { "cipher_iv_random_size", required_argument, 0, 0 },
1603
1604                         { "auth_algo", required_argument, 0, 0 },
1605                         { "auth_op", required_argument, 0, 0 },
1606                         { "auth_key", required_argument, 0, 0 },
1607                         { "auth_key_random_size", required_argument, 0, 0 },
1608                         { "auth_iv", required_argument, 0, 0 },
1609                         { "auth_iv_random_size", required_argument, 0, 0 },
1610
1611                         { "aead_algo", required_argument, 0, 0 },
1612                         { "aead_op", required_argument, 0, 0 },
1613                         { "aead_key", required_argument, 0, 0 },
1614                         { "aead_key_random_size", required_argument, 0, 0 },
1615                         { "aead_iv", required_argument, 0, 0 },
1616                         { "aead_iv_random_size", required_argument, 0, 0 },
1617
1618                         { "aad", required_argument, 0, 0 },
1619                         { "aad_random_size", required_argument, 0, 0 },
1620
1621                         { "digest_size", required_argument, 0, 0 },
1622
1623                         { "sessionless", no_argument, 0, 0 },
1624                         { "cryptodev_mask", required_argument, 0, 0},
1625
1626                         { NULL, 0, 0, 0 }
1627         };
1628
1629         l2fwd_crypto_default_options(options);
1630
1631         while ((opt = getopt_long(argc, argvopt, "p:q:sT:", lgopts,
1632                         &option_index)) != EOF) {
1633                 switch (opt) {
1634                 /* long options */
1635                 case 0:
1636                         retval = l2fwd_crypto_parse_args_long_options(options,
1637                                         lgopts, option_index);
1638                         if (retval < 0) {
1639                                 l2fwd_crypto_usage(prgname);
1640                                 return -1;
1641                         }
1642                         break;
1643
1644                 /* portmask */
1645                 case 'p':
1646                         retval = l2fwd_crypto_parse_portmask(options, optarg);
1647                         if (retval < 0) {
1648                                 l2fwd_crypto_usage(prgname);
1649                                 return -1;
1650                         }
1651                         break;
1652
1653                 /* nqueue */
1654                 case 'q':
1655                         retval = l2fwd_crypto_parse_nqueue(options, optarg);
1656                         if (retval < 0) {
1657                                 l2fwd_crypto_usage(prgname);
1658                                 return -1;
1659                         }
1660                         break;
1661
1662                 /* single  */
1663                 case 's':
1664                         options->single_lcore = 1;
1665
1666                         break;
1667
1668                 /* timer period */
1669                 case 'T':
1670                         retval = l2fwd_crypto_parse_timer_period(options,
1671                                         optarg);
1672                         if (retval < 0) {
1673                                 l2fwd_crypto_usage(prgname);
1674                                 return -1;
1675                         }
1676                         break;
1677
1678                 default:
1679                         l2fwd_crypto_usage(prgname);
1680                         return -1;
1681                 }
1682         }
1683
1684
1685         if (optind >= 0)
1686                 argv[optind-1] = prgname;
1687
1688         retval = optind-1;
1689         optind = 1; /* reset getopt lib */
1690
1691         return retval;
1692 }
1693
1694 /* Check the link status of all ports in up to 9s, and print them finally */
1695 static void
1696 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1697 {
1698 #define CHECK_INTERVAL 100 /* 100ms */
1699 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1700         uint8_t portid, count, all_ports_up, print_flag = 0;
1701         struct rte_eth_link link;
1702
1703         printf("\nChecking link status");
1704         fflush(stdout);
1705         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1706                 all_ports_up = 1;
1707                 for (portid = 0; portid < port_num; portid++) {
1708                         if ((port_mask & (1 << portid)) == 0)
1709                                 continue;
1710                         memset(&link, 0, sizeof(link));
1711                         rte_eth_link_get_nowait(portid, &link);
1712                         /* print link status if flag set */
1713                         if (print_flag == 1) {
1714                                 if (link.link_status)
1715                                         printf("Port %d Link Up - speed %u "
1716                                                 "Mbps - %s\n", (uint8_t)portid,
1717                                                 (unsigned)link.link_speed,
1718                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1719                                         ("full-duplex") : ("half-duplex\n"));
1720                                 else
1721                                         printf("Port %d Link Down\n",
1722                                                 (uint8_t)portid);
1723                                 continue;
1724                         }
1725                         /* clear all_ports_up flag if any link down */
1726                         if (link.link_status == ETH_LINK_DOWN) {
1727                                 all_ports_up = 0;
1728                                 break;
1729                         }
1730                 }
1731                 /* after finally printing all link status, get out */
1732                 if (print_flag == 1)
1733                         break;
1734
1735                 if (all_ports_up == 0) {
1736                         printf(".");
1737                         fflush(stdout);
1738                         rte_delay_ms(CHECK_INTERVAL);
1739                 }
1740
1741                 /* set the print_flag if all ports up or timeout */
1742                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1743                         print_flag = 1;
1744                         printf("done\n");
1745                 }
1746         }
1747 }
1748
1749 /* Check if device has to be HW/SW or any */
1750 static int
1751 check_type(const struct l2fwd_crypto_options *options,
1752                 const struct rte_cryptodev_info *dev_info)
1753 {
1754         if (options->type == CDEV_TYPE_HW &&
1755                         (dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1756                 return 0;
1757         if (options->type == CDEV_TYPE_SW &&
1758                         !(dev_info->feature_flags & RTE_CRYPTODEV_FF_HW_ACCELERATED))
1759                 return 0;
1760         if (options->type == CDEV_TYPE_ANY)
1761                 return 0;
1762
1763         return -1;
1764 }
1765
1766 static const struct rte_cryptodev_capabilities *
1767 check_device_support_cipher_algo(const struct l2fwd_crypto_options *options,
1768                 const struct rte_cryptodev_info *dev_info,
1769                 uint8_t cdev_id)
1770 {
1771         unsigned int i = 0;
1772         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1773         enum rte_crypto_cipher_algorithm cap_cipher_algo;
1774         enum rte_crypto_cipher_algorithm opt_cipher_algo =
1775                                         options->cipher_xform.cipher.algo;
1776
1777         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1778                 cap_cipher_algo = cap->sym.cipher.algo;
1779                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_CIPHER) {
1780                         if (cap_cipher_algo == opt_cipher_algo) {
1781                                 if (check_type(options, dev_info) == 0)
1782                                         break;
1783                         }
1784                 }
1785                 cap = &dev_info->capabilities[++i];
1786         }
1787
1788         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1789                 printf("Algorithm %s not supported by cryptodev %u"
1790                         " or device not of preferred type (%s)\n",
1791                         rte_crypto_cipher_algorithm_strings[opt_cipher_algo],
1792                         cdev_id,
1793                         options->string_type);
1794                 return NULL;
1795         }
1796
1797         return cap;
1798 }
1799
1800 static const struct rte_cryptodev_capabilities *
1801 check_device_support_auth_algo(const struct l2fwd_crypto_options *options,
1802                 const struct rte_cryptodev_info *dev_info,
1803                 uint8_t cdev_id)
1804 {
1805         unsigned int i = 0;
1806         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1807         enum rte_crypto_auth_algorithm cap_auth_algo;
1808         enum rte_crypto_auth_algorithm opt_auth_algo =
1809                                         options->auth_xform.auth.algo;
1810
1811         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1812                 cap_auth_algo = cap->sym.auth.algo;
1813                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AUTH) {
1814                         if (cap_auth_algo == opt_auth_algo) {
1815                                 if (check_type(options, dev_info) == 0)
1816                                         break;
1817                         }
1818                 }
1819                 cap = &dev_info->capabilities[++i];
1820         }
1821
1822         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1823                 printf("Algorithm %s not supported by cryptodev %u"
1824                         " or device not of preferred type (%s)\n",
1825                         rte_crypto_auth_algorithm_strings[opt_auth_algo],
1826                         cdev_id,
1827                         options->string_type);
1828                 return NULL;
1829         }
1830
1831         return cap;
1832 }
1833
1834 static const struct rte_cryptodev_capabilities *
1835 check_device_support_aead_algo(const struct l2fwd_crypto_options *options,
1836                 const struct rte_cryptodev_info *dev_info,
1837                 uint8_t cdev_id)
1838 {
1839         unsigned int i = 0;
1840         const struct rte_cryptodev_capabilities *cap = &dev_info->capabilities[0];
1841         enum rte_crypto_aead_algorithm cap_aead_algo;
1842         enum rte_crypto_aead_algorithm opt_aead_algo =
1843                                         options->aead_xform.aead.algo;
1844
1845         while (cap->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1846                 cap_aead_algo = cap->sym.aead.algo;
1847                 if (cap->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1848                         if (cap_aead_algo == opt_aead_algo) {
1849                                 if (check_type(options, dev_info) == 0)
1850                                         break;
1851                         }
1852                 }
1853                 cap = &dev_info->capabilities[++i];
1854         }
1855
1856         if (cap->op == RTE_CRYPTO_OP_TYPE_UNDEFINED) {
1857                 printf("Algorithm %s not supported by cryptodev %u"
1858                         " or device not of preferred type (%s)\n",
1859                         rte_crypto_aead_algorithm_strings[opt_aead_algo],
1860                         cdev_id,
1861                         options->string_type);
1862                 return NULL;
1863         }
1864
1865         return cap;
1866 }
1867
1868 /* Check if the device is enabled by cryptodev_mask */
1869 static int
1870 check_cryptodev_mask(struct l2fwd_crypto_options *options,
1871                 uint8_t cdev_id)
1872 {
1873         if (options->cryptodev_mask & (1 << cdev_id))
1874                 return 0;
1875
1876         return -1;
1877 }
1878
1879 static inline int
1880 check_supported_size(uint16_t length, uint16_t min, uint16_t max,
1881                 uint16_t increment)
1882 {
1883         uint16_t supp_size;
1884
1885         /* Single value */
1886         if (increment == 0) {
1887                 if (length == min)
1888                         return 0;
1889                 else
1890                         return -1;
1891         }
1892
1893         /* Range of values */
1894         for (supp_size = min; supp_size <= max; supp_size += increment) {
1895                 if (length == supp_size)
1896                         return 0;
1897         }
1898
1899         return -1;
1900 }
1901
1902 static int
1903 check_iv_param(const struct rte_crypto_param_range *iv_range_size,
1904                 unsigned int iv_param, int iv_random_size,
1905                 uint16_t *iv_length)
1906 {
1907         /*
1908          * Check if length of provided IV is supported
1909          * by the algorithm chosen.
1910          */
1911         if (iv_param) {
1912                 if (check_supported_size(*iv_length,
1913                                 iv_range_size->min,
1914                                 iv_range_size->max,
1915                                 iv_range_size->increment)
1916                                         != 0) {
1917                         printf("Unsupported IV length\n");
1918                         return -1;
1919                 }
1920         /*
1921          * Check if length of IV to be randomly generated
1922          * is supported by the algorithm chosen.
1923          */
1924         } else if (iv_random_size != -1) {
1925                 if (check_supported_size(iv_random_size,
1926                                 iv_range_size->min,
1927                                 iv_range_size->max,
1928                                 iv_range_size->increment)
1929                                         != 0) {
1930                         printf("Unsupported IV length\n");
1931                         return -1;
1932                 }
1933                 *iv_length = iv_random_size;
1934         /* No size provided, use minimum size. */
1935         } else
1936                 *iv_length = iv_range_size->min;
1937
1938         return 0;
1939 }
1940
1941 static int
1942 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports,
1943                 uint8_t *enabled_cdevs)
1944 {
1945         unsigned int cdev_id, cdev_count, enabled_cdev_count = 0;
1946         const struct rte_cryptodev_capabilities *cap;
1947         unsigned int sess_sz, max_sess_sz = 0;
1948         int retval;
1949
1950         cdev_count = rte_cryptodev_count();
1951         if (cdev_count == 0) {
1952                 printf("No crypto devices available\n");
1953                 return -1;
1954         }
1955
1956         for (cdev_id = 0; cdev_id < cdev_count; cdev_id++) {
1957                 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1958                 if (sess_sz > max_sess_sz)
1959                         max_sess_sz = sess_sz;
1960         }
1961
1962         for (cdev_id = 0; cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1963                         cdev_id++) {
1964                 struct rte_cryptodev_qp_conf qp_conf;
1965                 struct rte_cryptodev_info dev_info;
1966                 uint8_t socket_id = rte_cryptodev_socket_id(cdev_id);
1967
1968                 struct rte_cryptodev_config conf = {
1969                         .nb_queue_pairs = 1,
1970                         .socket_id = socket_id,
1971                 };
1972
1973                 if (check_cryptodev_mask(options, (uint8_t)cdev_id))
1974                         continue;
1975
1976                 rte_cryptodev_info_get(cdev_id, &dev_info);
1977
1978                 if (session_pool_socket[socket_id] == NULL) {
1979                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1980                         struct rte_mempool *sess_mp;
1981
1982                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1983                                 "sess_mp_%u", socket_id);
1984
1985                         /*
1986                          * Create enough objects for session headers and
1987                          * device private data
1988                          */
1989                         sess_mp = rte_mempool_create(mp_name,
1990                                                 MAX_SESSIONS * 2,
1991                                                 max_sess_sz,
1992                                                 SESSION_POOL_CACHE_SIZE,
1993                                                 0, NULL, NULL, NULL,
1994                                                 NULL, socket_id,
1995                                                 0);
1996
1997                         if (sess_mp == NULL) {
1998                                 printf("Cannot create session pool on socket %d\n",
1999                                         socket_id);
2000                                 return -ENOMEM;
2001                         }
2002
2003                         printf("Allocated session pool on socket %d\n", socket_id);
2004                         session_pool_socket[socket_id] = sess_mp;
2005                 }
2006
2007                 /* Set AEAD parameters */
2008                 if (options->xform_chain == L2FWD_CRYPTO_AEAD) {
2009                         /* Check if device supports AEAD algo */
2010                         cap = check_device_support_aead_algo(options, &dev_info,
2011                                                         cdev_id);
2012                         if (cap == NULL)
2013                                 continue;
2014
2015                         options->block_size = cap->sym.aead.block_size;
2016
2017                         check_iv_param(&cap->sym.aead.iv_size,
2018                                         options->aead_iv_param,
2019                                         options->aead_iv_random_size,
2020                                         &options->aead_iv.length);
2021
2022                         /*
2023                          * Check if length of provided AEAD key is supported
2024                          * by the algorithm chosen.
2025                          */
2026                         if (options->aead_key_param) {
2027                                 if (check_supported_size(
2028                                                 options->aead_xform.aead.key.length,
2029                                                 cap->sym.aead.key_size.min,
2030                                                 cap->sym.aead.key_size.max,
2031                                                 cap->sym.aead.key_size.increment)
2032                                                         != 0) {
2033                                         printf("Unsupported aead key length\n");
2034                                         return -1;
2035                                 }
2036                         /*
2037                          * Check if length of the aead key to be randomly generated
2038                          * is supported by the algorithm chosen.
2039                          */
2040                         } else if (options->aead_key_random_size != -1) {
2041                                 if (check_supported_size(options->ckey_random_size,
2042                                                 cap->sym.aead.key_size.min,
2043                                                 cap->sym.aead.key_size.max,
2044                                                 cap->sym.aead.key_size.increment)
2045                                                         != 0) {
2046                                         printf("Unsupported aead key length\n");
2047                                         return -1;
2048                                 }
2049                                 options->aead_xform.aead.key.length =
2050                                                         options->ckey_random_size;
2051                         /* No size provided, use minimum size. */
2052                         } else
2053                                 options->aead_xform.aead.key.length =
2054                                                 cap->sym.aead.key_size.min;
2055
2056                         if (!options->aead_key_param)
2057                                 generate_random_key(
2058                                         options->aead_xform.aead.key.data,
2059                                         options->aead_xform.aead.key.length);
2060
2061                         /*
2062                          * Check if length of provided AAD is supported
2063                          * by the algorithm chosen.
2064                          */
2065                         if (options->aad_param) {
2066                                 if (check_supported_size(options->aad.length,
2067                                                 cap->sym.aead.aad_size.min,
2068                                                 cap->sym.aead.aad_size.max,
2069                                                 cap->sym.aead.aad_size.increment)
2070                                                         != 0) {
2071                                         printf("Unsupported AAD length\n");
2072                                         return -1;
2073                                 }
2074                         /*
2075                          * Check if length of AAD to be randomly generated
2076                          * is supported by the algorithm chosen.
2077                          */
2078                         } else if (options->aad_random_size != -1) {
2079                                 if (check_supported_size(options->aad_random_size,
2080                                                 cap->sym.aead.aad_size.min,
2081                                                 cap->sym.aead.aad_size.max,
2082                                                 cap->sym.aead.aad_size.increment)
2083                                                         != 0) {
2084                                         printf("Unsupported AAD length\n");
2085                                         return -1;
2086                                 }
2087                                 options->aad.length = options->aad_random_size;
2088                         /* No size provided, use minimum size. */
2089                         } else
2090                                 options->aad.length = cap->sym.auth.aad_size.min;
2091
2092                         options->aead_xform.aead.aad_length =
2093                                                 options->aad.length;
2094
2095                         /* Check if digest size is supported by the algorithm. */
2096                         if (options->digest_size != -1) {
2097                                 if (check_supported_size(options->digest_size,
2098                                                 cap->sym.aead.digest_size.min,
2099                                                 cap->sym.aead.digest_size.max,
2100                                                 cap->sym.aead.digest_size.increment)
2101                                                         != 0) {
2102                                         printf("Unsupported digest length\n");
2103                                         return -1;
2104                                 }
2105                                 options->aead_xform.aead.digest_length =
2106                                                         options->digest_size;
2107                         /* No size provided, use minimum size. */
2108                         } else
2109                                 options->aead_xform.aead.digest_length =
2110                                                 cap->sym.aead.digest_size.min;
2111                 }
2112
2113                 /* Set cipher parameters */
2114                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2115                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2116                                 options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
2117                         /* Check if device supports cipher algo */
2118                         cap = check_device_support_cipher_algo(options, &dev_info,
2119                                                         cdev_id);
2120                         if (cap == NULL)
2121                                 continue;
2122
2123                         options->block_size = cap->sym.cipher.block_size;
2124
2125                         check_iv_param(&cap->sym.cipher.iv_size,
2126                                         options->cipher_iv_param,
2127                                         options->cipher_iv_random_size,
2128                                         &options->cipher_iv.length);
2129
2130                         /*
2131                          * Check if length of provided cipher key is supported
2132                          * by the algorithm chosen.
2133                          */
2134                         if (options->ckey_param) {
2135                                 if (check_supported_size(
2136                                                 options->cipher_xform.cipher.key.length,
2137                                                 cap->sym.cipher.key_size.min,
2138                                                 cap->sym.cipher.key_size.max,
2139                                                 cap->sym.cipher.key_size.increment)
2140                                                         != 0) {
2141                                         printf("Unsupported cipher key length\n");
2142                                         return -1;
2143                                 }
2144                         /*
2145                          * Check if length of the cipher key to be randomly generated
2146                          * is supported by the algorithm chosen.
2147                          */
2148                         } else if (options->ckey_random_size != -1) {
2149                                 if (check_supported_size(options->ckey_random_size,
2150                                                 cap->sym.cipher.key_size.min,
2151                                                 cap->sym.cipher.key_size.max,
2152                                                 cap->sym.cipher.key_size.increment)
2153                                                         != 0) {
2154                                         printf("Unsupported cipher key length\n");
2155                                         return -1;
2156                                 }
2157                                 options->cipher_xform.cipher.key.length =
2158                                                         options->ckey_random_size;
2159                         /* No size provided, use minimum size. */
2160                         } else
2161                                 options->cipher_xform.cipher.key.length =
2162                                                 cap->sym.cipher.key_size.min;
2163
2164                         if (!options->ckey_param)
2165                                 generate_random_key(
2166                                         options->cipher_xform.cipher.key.data,
2167                                         options->cipher_xform.cipher.key.length);
2168
2169                 }
2170
2171                 /* Set auth parameters */
2172                 if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH ||
2173                                 options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER ||
2174                                 options->xform_chain == L2FWD_CRYPTO_HASH_ONLY) {
2175                         /* Check if device supports auth algo */
2176                         cap = check_device_support_auth_algo(options, &dev_info,
2177                                                         cdev_id);
2178                         if (cap == NULL)
2179                                 continue;
2180
2181                         check_iv_param(&cap->sym.auth.iv_size,
2182                                         options->auth_iv_param,
2183                                         options->auth_iv_random_size,
2184                                         &options->auth_iv.length);
2185                         /*
2186                          * Check if length of provided auth key is supported
2187                          * by the algorithm chosen.
2188                          */
2189                         if (options->akey_param) {
2190                                 if (check_supported_size(
2191                                                 options->auth_xform.auth.key.length,
2192                                                 cap->sym.auth.key_size.min,
2193                                                 cap->sym.auth.key_size.max,
2194                                                 cap->sym.auth.key_size.increment)
2195                                                         != 0) {
2196                                         printf("Unsupported auth key length\n");
2197                                         return -1;
2198                                 }
2199                         /*
2200                          * Check if length of the auth key to be randomly generated
2201                          * is supported by the algorithm chosen.
2202                          */
2203                         } else if (options->akey_random_size != -1) {
2204                                 if (check_supported_size(options->akey_random_size,
2205                                                 cap->sym.auth.key_size.min,
2206                                                 cap->sym.auth.key_size.max,
2207                                                 cap->sym.auth.key_size.increment)
2208                                                         != 0) {
2209                                         printf("Unsupported auth key length\n");
2210                                         return -1;
2211                                 }
2212                                 options->auth_xform.auth.key.length =
2213                                                         options->akey_random_size;
2214                         /* No size provided, use minimum size. */
2215                         } else
2216                                 options->auth_xform.auth.key.length =
2217                                                 cap->sym.auth.key_size.min;
2218
2219                         if (!options->akey_param)
2220                                 generate_random_key(
2221                                         options->auth_xform.auth.key.data,
2222                                         options->auth_xform.auth.key.length);
2223
2224                         /* Check if digest size is supported by the algorithm. */
2225                         if (options->digest_size != -1) {
2226                                 if (check_supported_size(options->digest_size,
2227                                                 cap->sym.auth.digest_size.min,
2228                                                 cap->sym.auth.digest_size.max,
2229                                                 cap->sym.auth.digest_size.increment)
2230                                                         != 0) {
2231                                         printf("Unsupported digest length\n");
2232                                         return -1;
2233                                 }
2234                                 options->auth_xform.auth.digest_length =
2235                                                         options->digest_size;
2236                         /* No size provided, use minimum size. */
2237                         } else
2238                                 options->auth_xform.auth.digest_length =
2239                                                 cap->sym.auth.digest_size.min;
2240                 }
2241
2242                 retval = rte_cryptodev_configure(cdev_id, &conf);
2243                 if (retval < 0) {
2244                         printf("Failed to configure cryptodev %u", cdev_id);
2245                         return -1;
2246                 }
2247
2248                 qp_conf.nb_descriptors = 2048;
2249
2250                 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
2251                                 socket_id, session_pool_socket[socket_id]);
2252                 if (retval < 0) {
2253                         printf("Failed to setup queue pair %u on cryptodev %u",
2254                                         0, cdev_id);
2255                         return -1;
2256                 }
2257
2258                 retval = rte_cryptodev_start(cdev_id);
2259                 if (retval < 0) {
2260                         printf("Failed to start device %u: error %d\n",
2261                                         cdev_id, retval);
2262                         return -1;
2263                 }
2264
2265                 l2fwd_enabled_crypto_mask |= (((uint64_t)1) << cdev_id);
2266
2267                 enabled_cdevs[cdev_id] = 1;
2268                 enabled_cdev_count++;
2269         }
2270
2271         return enabled_cdev_count;
2272 }
2273
2274 static int
2275 initialize_ports(struct l2fwd_crypto_options *options)
2276 {
2277         uint8_t last_portid, portid;
2278         unsigned enabled_portcount = 0;
2279         unsigned nb_ports = rte_eth_dev_count();
2280
2281         if (nb_ports == 0) {
2282                 printf("No Ethernet ports - bye\n");
2283                 return -1;
2284         }
2285
2286         /* Reset l2fwd_dst_ports */
2287         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
2288                 l2fwd_dst_ports[portid] = 0;
2289
2290         for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
2291                 int retval;
2292
2293                 /* Skip ports that are not enabled */
2294                 if ((options->portmask & (1 << portid)) == 0)
2295                         continue;
2296
2297                 /* init port */
2298                 printf("Initializing port %u... ", (unsigned) portid);
2299                 fflush(stdout);
2300                 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
2301                 if (retval < 0) {
2302                         printf("Cannot configure device: err=%d, port=%u\n",
2303                                   retval, (unsigned) portid);
2304                         return -1;
2305                 }
2306
2307                 retval = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
2308                                                           &nb_txd);
2309                 if (retval < 0) {
2310                         printf("Cannot adjust number of descriptors: err=%d, port=%u\n",
2311                                 retval, (unsigned) portid);
2312                         return -1;
2313                 }
2314
2315                 /* init one RX queue */
2316                 fflush(stdout);
2317                 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
2318                                              rte_eth_dev_socket_id(portid),
2319                                              NULL, l2fwd_pktmbuf_pool);
2320                 if (retval < 0) {
2321                         printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
2322                                         retval, (unsigned) portid);
2323                         return -1;
2324                 }
2325
2326                 /* init one TX queue on each port */
2327                 fflush(stdout);
2328                 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
2329                                 rte_eth_dev_socket_id(portid),
2330                                 NULL);
2331                 if (retval < 0) {
2332                         printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
2333                                 retval, (unsigned) portid);
2334
2335                         return -1;
2336                 }
2337
2338                 /* Start device */
2339                 retval = rte_eth_dev_start(portid);
2340                 if (retval < 0) {
2341                         printf("rte_eth_dev_start:err=%d, port=%u\n",
2342                                         retval, (unsigned) portid);
2343                         return -1;
2344                 }
2345
2346                 rte_eth_promiscuous_enable(portid);
2347
2348                 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
2349
2350                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
2351                                 (unsigned) portid,
2352                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
2353                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
2354                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
2355                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
2356                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
2357                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
2358
2359                 /* initialize port stats */
2360                 memset(&port_statistics, 0, sizeof(port_statistics));
2361
2362                 /* Setup port forwarding table */
2363                 if (enabled_portcount % 2) {
2364                         l2fwd_dst_ports[portid] = last_portid;
2365                         l2fwd_dst_ports[last_portid] = portid;
2366                 } else {
2367                         last_portid = portid;
2368                 }
2369
2370                 l2fwd_enabled_port_mask |= (1 << portid);
2371                 enabled_portcount++;
2372         }
2373
2374         if (enabled_portcount == 1) {
2375                 l2fwd_dst_ports[last_portid] = last_portid;
2376         } else if (enabled_portcount % 2) {
2377                 printf("odd number of ports in portmask- bye\n");
2378                 return -1;
2379         }
2380
2381         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
2382
2383         return enabled_portcount;
2384 }
2385
2386 static void
2387 reserve_key_memory(struct l2fwd_crypto_options *options)
2388 {
2389         options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
2390                                                 MAX_KEY_SIZE, 0);
2391         if (options->cipher_xform.cipher.key.data == NULL)
2392                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
2393
2394         options->auth_xform.auth.key.data = rte_malloc("auth key",
2395                                                 MAX_KEY_SIZE, 0);
2396         if (options->auth_xform.auth.key.data == NULL)
2397                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
2398
2399         options->aead_xform.aead.key.data = rte_malloc("aead key",
2400                                                 MAX_KEY_SIZE, 0);
2401         if (options->aead_xform.aead.key.data == NULL)
2402                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD key");
2403
2404         options->cipher_iv.data = rte_malloc("cipher iv", MAX_KEY_SIZE, 0);
2405         if (options->cipher_iv.data == NULL)
2406                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher IV");
2407
2408         options->auth_iv.data = rte_malloc("auth iv", MAX_KEY_SIZE, 0);
2409         if (options->auth_iv.data == NULL)
2410                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth IV");
2411
2412         options->aead_iv.data = rte_malloc("aead_iv", MAX_KEY_SIZE, 0);
2413         if (options->aead_iv.data == NULL)
2414                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AEAD iv");
2415
2416         options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
2417         if (options->aad.data == NULL)
2418                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
2419         options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
2420 }
2421
2422 int
2423 main(int argc, char **argv)
2424 {
2425         struct lcore_queue_conf *qconf;
2426         struct l2fwd_crypto_options options;
2427
2428         uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
2429         unsigned lcore_id, rx_lcore_id;
2430         int ret, enabled_cdevcount, enabled_portcount;
2431         uint8_t enabled_cdevs[RTE_CRYPTO_MAX_DEVS] = {0};
2432
2433         /* init EAL */
2434         ret = rte_eal_init(argc, argv);
2435         if (ret < 0)
2436                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
2437         argc -= ret;
2438         argv += ret;
2439
2440         /* reserve memory for Cipher/Auth key and IV */
2441         reserve_key_memory(&options);
2442
2443         /* parse application arguments (after the EAL ones) */
2444         ret = l2fwd_crypto_parse_args(&options, argc, argv);
2445         if (ret < 0)
2446                 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
2447
2448         /* create the mbuf pool */
2449         l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
2450                         sizeof(struct rte_crypto_op),
2451                         RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
2452         if (l2fwd_pktmbuf_pool == NULL)
2453                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
2454
2455         /* create crypto op pool */
2456         l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
2457                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, MAXIMUM_IV_LENGTH,
2458                         rte_socket_id());
2459         if (l2fwd_crypto_op_pool == NULL)
2460                 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
2461
2462         /* Enable Ethernet ports */
2463         enabled_portcount = initialize_ports(&options);
2464         if (enabled_portcount < 1)
2465                 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
2466
2467         nb_ports = rte_eth_dev_count();
2468         /* Initialize the port/queue configuration of each logical core */
2469         for (rx_lcore_id = 0, qconf = NULL, portid = 0;
2470                         portid < nb_ports; portid++) {
2471
2472                 /* skip ports that are not enabled */
2473                 if ((options.portmask & (1 << portid)) == 0)
2474                         continue;
2475
2476                 if (options.single_lcore && qconf == NULL) {
2477                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2478                                 rx_lcore_id++;
2479                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2480                                         rte_exit(EXIT_FAILURE,
2481                                                         "Not enough cores\n");
2482                         }
2483                 } else if (!options.single_lcore) {
2484                         /* get the lcore_id for this port */
2485                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2486                                lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
2487                                options.nb_ports_per_lcore) {
2488                                 rx_lcore_id++;
2489                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2490                                         rte_exit(EXIT_FAILURE,
2491                                                         "Not enough cores\n");
2492                         }
2493                 }
2494
2495                 /* Assigned a new logical core in the loop above. */
2496                 if (qconf != &lcore_queue_conf[rx_lcore_id])
2497                         qconf = &lcore_queue_conf[rx_lcore_id];
2498
2499                 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
2500                 qconf->nb_rx_ports++;
2501
2502                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
2503         }
2504
2505         /* Enable Crypto devices */
2506         enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount,
2507                         enabled_cdevs);
2508         if (enabled_cdevcount < 0)
2509                 rte_exit(EXIT_FAILURE, "Failed to initialize crypto devices\n");
2510
2511         if (enabled_cdevcount < enabled_portcount)
2512                 rte_exit(EXIT_FAILURE, "Number of capable crypto devices (%d) "
2513                                 "has to be more or equal to number of ports (%d)\n",
2514                                 enabled_cdevcount, enabled_portcount);
2515
2516         nb_cryptodevs = rte_cryptodev_count();
2517
2518         /* Initialize the port/cryptodev configuration of each logical core */
2519         for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
2520                         cdev_id < nb_cryptodevs && enabled_cdevcount;
2521                         cdev_id++) {
2522                 /* Crypto op not supported by crypto device */
2523                 if (!enabled_cdevs[cdev_id])
2524                         continue;
2525
2526                 if (options.single_lcore && qconf == NULL) {
2527                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
2528                                 rx_lcore_id++;
2529                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2530                                         rte_exit(EXIT_FAILURE,
2531                                                         "Not enough cores\n");
2532                         }
2533                 } else if (!options.single_lcore) {
2534                         /* get the lcore_id for this port */
2535                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
2536                                lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
2537                                options.nb_ports_per_lcore) {
2538                                 rx_lcore_id++;
2539                                 if (rx_lcore_id >= RTE_MAX_LCORE)
2540                                         rte_exit(EXIT_FAILURE,
2541                                                         "Not enough cores\n");
2542                         }
2543                 }
2544
2545                 /* Assigned a new logical core in the loop above. */
2546                 if (qconf != &lcore_queue_conf[rx_lcore_id])
2547                         qconf = &lcore_queue_conf[rx_lcore_id];
2548
2549                 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
2550                 qconf->nb_crypto_devs++;
2551
2552                 enabled_cdevcount--;
2553
2554                 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
2555                                 (unsigned)cdev_id);
2556         }
2557
2558         /* launch per-lcore init on every lcore */
2559         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
2560                         CALL_MASTER);
2561         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2562                 if (rte_eal_wait_lcore(lcore_id) < 0)
2563                         return -1;
2564         }
2565
2566         return 0;
2567 }