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