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