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