examples/l2fwd-crypto: add cipher/hash only cases
[dpdk.git] / examples / l2fwd-crypto / main.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015-2016 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <inttypes.h>
40 #include <sys/types.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <setjmp.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <getopt.h>
48
49 #include <rte_atomic.h>
50 #include <rte_branch_prediction.h>
51 #include <rte_common.h>
52 #include <rte_cryptodev.h>
53 #include <rte_cycles.h>
54 #include <rte_debug.h>
55 #include <rte_eal.h>
56 #include <rte_ether.h>
57 #include <rte_ethdev.h>
58 #include <rte_interrupts.h>
59 #include <rte_ip.h>
60 #include <rte_launch.h>
61 #include <rte_lcore.h>
62 #include <rte_log.h>
63 #include <rte_malloc.h>
64 #include <rte_mbuf.h>
65 #include <rte_memcpy.h>
66 #include <rte_memory.h>
67 #include <rte_mempool.h>
68 #include <rte_memzone.h>
69 #include <rte_pci.h>
70 #include <rte_per_lcore.h>
71 #include <rte_prefetch.h>
72 #include <rte_random.h>
73 #include <rte_ring.h>
74
75 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
76
77 #define NB_MBUF   8192
78
79 #define MAX_KEY_SIZE 128
80 #define MAX_PKT_BURST 32
81 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
82
83 /*
84  * Configurable number of RX/TX ring descriptors
85  */
86 #define RTE_TEST_RX_DESC_DEFAULT 128
87 #define RTE_TEST_TX_DESC_DEFAULT 512
88
89 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
90 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
91
92 /* ethernet addresses of ports */
93 static struct ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
94
95 /* mask of enabled ports */
96 static uint64_t l2fwd_enabled_port_mask;
97 static uint64_t l2fwd_enabled_crypto_mask;
98
99 /* list of enabled ports */
100 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
101
102
103 struct pkt_buffer {
104         unsigned len;
105         struct rte_mbuf *buffer[MAX_PKT_BURST];
106 };
107
108 struct op_buffer {
109         unsigned len;
110         struct rte_crypto_op *buffer[MAX_PKT_BURST];
111 };
112
113 #define MAX_RX_QUEUE_PER_LCORE 16
114 #define MAX_TX_QUEUE_PER_PORT 16
115
116 enum l2fwd_crypto_xform_chain {
117         L2FWD_CRYPTO_CIPHER_HASH,
118         L2FWD_CRYPTO_HASH_CIPHER,
119         L2FWD_CRYPTO_CIPHER_ONLY,
120         L2FWD_CRYPTO_HASH_ONLY
121 };
122
123 struct l2fwd_key {
124         uint8_t *data;
125         uint32_t length;
126         phys_addr_t phys_addr;
127 };
128
129 /** l2fwd crypto application command line options */
130 struct l2fwd_crypto_options {
131         unsigned portmask;
132         unsigned nb_ports_per_lcore;
133         unsigned refresh_period;
134         unsigned single_lcore:1;
135
136         enum rte_cryptodev_type cdev_type;
137         unsigned sessionless:1;
138
139         enum l2fwd_crypto_xform_chain xform_chain;
140
141         struct rte_crypto_sym_xform cipher_xform;
142         unsigned ckey_param;
143
144         struct l2fwd_key iv;
145         unsigned iv_param;
146
147         struct rte_crypto_sym_xform auth_xform;
148         uint8_t akey_param;
149
150         struct l2fwd_key aad;
151         unsigned aad_param;
152 };
153
154 /** l2fwd crypto lcore params */
155 struct l2fwd_crypto_params {
156         uint8_t dev_id;
157         uint8_t qp_id;
158
159         unsigned digest_length;
160         unsigned block_size;
161         struct l2fwd_key iv;
162         struct l2fwd_key aad;
163         struct rte_cryptodev_sym_session *session;
164
165         uint8_t do_cipher;
166         uint8_t do_hash;
167 };
168
169 /** lcore configuration */
170 struct lcore_queue_conf {
171         unsigned nb_rx_ports;
172         unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
173
174         unsigned nb_crypto_devs;
175         unsigned cryptodev_list[MAX_RX_QUEUE_PER_LCORE];
176
177         struct op_buffer op_buf[RTE_MAX_ETHPORTS];
178         struct pkt_buffer pkt_buf[RTE_MAX_ETHPORTS];
179 } __rte_cache_aligned;
180
181 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
182
183 static const struct rte_eth_conf port_conf = {
184         .rxmode = {
185                 .mq_mode = ETH_MQ_RX_NONE,
186                 .max_rx_pkt_len = ETHER_MAX_LEN,
187                 .split_hdr_size = 0,
188                 .header_split   = 0, /**< Header Split disabled */
189                 .hw_ip_checksum = 0, /**< IP checksum offload disabled */
190                 .hw_vlan_filter = 0, /**< VLAN filtering disabled */
191                 .jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
192                 .hw_strip_crc   = 0, /**< CRC stripped by hardware */
193         },
194         .txmode = {
195                 .mq_mode = ETH_MQ_TX_NONE,
196         },
197 };
198
199 struct rte_mempool *l2fwd_pktmbuf_pool;
200 struct rte_mempool *l2fwd_crypto_op_pool;
201
202 /* Per-port statistics struct */
203 struct l2fwd_port_statistics {
204         uint64_t tx;
205         uint64_t rx;
206
207         uint64_t crypto_enqueued;
208         uint64_t crypto_dequeued;
209
210         uint64_t dropped;
211 } __rte_cache_aligned;
212
213 struct l2fwd_crypto_statistics {
214         uint64_t enqueued;
215         uint64_t dequeued;
216
217         uint64_t errors;
218 } __rte_cache_aligned;
219
220 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
221 struct l2fwd_crypto_statistics crypto_statistics[RTE_MAX_ETHPORTS];
222
223 /* A tsc-based timer responsible for triggering statistics printout */
224 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
225 #define MAX_TIMER_PERIOD 86400UL /* 1 day max */
226
227 /* default period is 10 seconds */
228 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000;
229
230 /* Print out statistics on packets dropped */
231 static void
232 print_stats(void)
233 {
234         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
235         uint64_t total_packets_enqueued, total_packets_dequeued,
236                 total_packets_errors;
237         unsigned portid;
238         uint64_t cdevid;
239
240         total_packets_dropped = 0;
241         total_packets_tx = 0;
242         total_packets_rx = 0;
243         total_packets_enqueued = 0;
244         total_packets_dequeued = 0;
245         total_packets_errors = 0;
246
247         const char clr[] = { 27, '[', '2', 'J', '\0' };
248         const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
249
250                 /* Clear screen and move to top left */
251         printf("%s%s", clr, topLeft);
252
253         printf("\nPort statistics ====================================");
254
255         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
256                 /* skip disabled ports */
257                 if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
258                         continue;
259                 printf("\nStatistics for port %u ------------------------------"
260                            "\nPackets sent: %32"PRIu64
261                            "\nPackets received: %28"PRIu64
262                            "\nPackets dropped: %29"PRIu64,
263                            portid,
264                            port_statistics[portid].tx,
265                            port_statistics[portid].rx,
266                            port_statistics[portid].dropped);
267
268                 total_packets_dropped += port_statistics[portid].dropped;
269                 total_packets_tx += port_statistics[portid].tx;
270                 total_packets_rx += port_statistics[portid].rx;
271         }
272         printf("\nCrypto statistics ==================================");
273
274         for (cdevid = 0; cdevid < RTE_CRYPTO_MAX_DEVS; cdevid++) {
275                 /* skip disabled ports */
276                 if ((l2fwd_enabled_crypto_mask & (1lu << cdevid)) == 0)
277                         continue;
278                 printf("\nStatistics for cryptodev %"PRIu64
279                                 " -------------------------"
280                            "\nPackets enqueued: %28"PRIu64
281                            "\nPackets dequeued: %28"PRIu64
282                            "\nPackets errors: %30"PRIu64,
283                            cdevid,
284                            crypto_statistics[cdevid].enqueued,
285                            crypto_statistics[cdevid].dequeued,
286                            crypto_statistics[cdevid].errors);
287
288                 total_packets_enqueued += crypto_statistics[cdevid].enqueued;
289                 total_packets_dequeued += crypto_statistics[cdevid].dequeued;
290                 total_packets_errors += crypto_statistics[cdevid].errors;
291         }
292         printf("\nAggregate statistics ==============================="
293                    "\nTotal packets received: %22"PRIu64
294                    "\nTotal packets enqueued: %22"PRIu64
295                    "\nTotal packets dequeued: %22"PRIu64
296                    "\nTotal packets sent: %26"PRIu64
297                    "\nTotal packets dropped: %23"PRIu64
298                    "\nTotal packets crypto errors: %17"PRIu64,
299                    total_packets_rx,
300                    total_packets_enqueued,
301                    total_packets_dequeued,
302                    total_packets_tx,
303                    total_packets_dropped,
304                    total_packets_errors);
305         printf("\n====================================================\n");
306 }
307
308
309
310 static int
311 l2fwd_crypto_send_burst(struct lcore_queue_conf *qconf, unsigned n,
312                 struct l2fwd_crypto_params *cparams)
313 {
314         struct rte_crypto_op **op_buffer;
315         unsigned ret;
316
317         op_buffer = (struct rte_crypto_op **)
318                         qconf->op_buf[cparams->dev_id].buffer;
319
320         ret = rte_cryptodev_enqueue_burst(cparams->dev_id,
321                         cparams->qp_id, op_buffer, (uint16_t) n);
322
323         crypto_statistics[cparams->dev_id].enqueued += ret;
324         if (unlikely(ret < n)) {
325                 crypto_statistics[cparams->dev_id].errors += (n - ret);
326                 do {
327                         rte_pktmbuf_free(op_buffer[ret]->sym->m_src);
328                         rte_crypto_op_free(op_buffer[ret]);
329                 } while (++ret < n);
330         }
331
332         return 0;
333 }
334
335 static int
336 l2fwd_crypto_enqueue(struct rte_crypto_op *op,
337                 struct l2fwd_crypto_params *cparams)
338 {
339         unsigned lcore_id, len;
340         struct lcore_queue_conf *qconf;
341
342         lcore_id = rte_lcore_id();
343
344         qconf = &lcore_queue_conf[lcore_id];
345         len = qconf->op_buf[cparams->dev_id].len;
346         qconf->op_buf[cparams->dev_id].buffer[len] = op;
347         len++;
348
349         /* enough ops to be sent */
350         if (len == MAX_PKT_BURST) {
351                 l2fwd_crypto_send_burst(qconf, MAX_PKT_BURST, cparams);
352                 len = 0;
353         }
354
355         qconf->op_buf[cparams->dev_id].len = len;
356         return 0;
357 }
358
359 static int
360 l2fwd_simple_crypto_enqueue(struct rte_mbuf *m,
361                 struct rte_crypto_op *op,
362                 struct l2fwd_crypto_params *cparams)
363 {
364         struct ether_hdr *eth_hdr;
365         struct ipv4_hdr *ip_hdr;
366
367         unsigned ipdata_offset, pad_len, data_len;
368         char *padding;
369
370         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
371
372         if (eth_hdr->ether_type != rte_cpu_to_be_16(ETHER_TYPE_IPv4))
373                 return -1;
374
375         ipdata_offset = sizeof(struct ether_hdr);
376
377         ip_hdr = (struct ipv4_hdr *)(rte_pktmbuf_mtod(m, char *) +
378                         ipdata_offset);
379
380         ipdata_offset += (ip_hdr->version_ihl & IPV4_HDR_IHL_MASK)
381                         * IPV4_IHL_MULTIPLIER;
382
383
384         /* Zero pad data to be crypto'd so it is block aligned */
385         data_len  = rte_pktmbuf_data_len(m) - ipdata_offset;
386         pad_len = data_len % cparams->block_size ? cparams->block_size -
387                         (data_len % cparams->block_size) : 0;
388
389         if (pad_len) {
390                 padding = rte_pktmbuf_append(m, pad_len);
391                 if (unlikely(!padding))
392                         return -1;
393
394                 data_len += pad_len;
395                 memset(padding, 0, pad_len);
396         }
397
398         /* Set crypto operation data parameters */
399         rte_crypto_op_attach_sym_session(op, cparams->session);
400
401         if (cparams->do_hash) {
402                 /* Append space for digest to end of packet */
403                 op->sym->auth.digest.data = (uint8_t *)rte_pktmbuf_append(m,
404                                 cparams->digest_length);
405                 op->sym->auth.digest.phys_addr = rte_pktmbuf_mtophys_offset(m,
406                                 rte_pktmbuf_pkt_len(m) - cparams->digest_length);
407                 op->sym->auth.digest.length = cparams->digest_length;
408
409                 op->sym->auth.data.offset = ipdata_offset;
410                 op->sym->auth.data.length = data_len;
411
412                 if (cparams->aad.length) {
413                         op->sym->auth.aad.data = cparams->aad.data;
414                         op->sym->auth.aad.phys_addr = cparams->aad.phys_addr;
415                         op->sym->auth.aad.length = cparams->aad.length;
416                 }
417         }
418
419         if (cparams->do_cipher) {
420                 op->sym->cipher.iv.data = cparams->iv.data;
421                 op->sym->cipher.iv.phys_addr = cparams->iv.phys_addr;
422                 op->sym->cipher.iv.length = cparams->iv.length;
423
424                 op->sym->cipher.data.offset = ipdata_offset;
425                 op->sym->cipher.data.length = data_len;
426         }
427
428         op->sym->m_src = m;
429
430         return l2fwd_crypto_enqueue(op, cparams);
431 }
432
433
434 /* Send the burst of packets on an output interface */
435 static int
436 l2fwd_send_burst(struct lcore_queue_conf *qconf, unsigned n,
437                 uint8_t port)
438 {
439         struct rte_mbuf **pkt_buffer;
440         unsigned ret;
441
442         pkt_buffer = (struct rte_mbuf **)qconf->pkt_buf[port].buffer;
443
444         ret = rte_eth_tx_burst(port, 0, pkt_buffer, (uint16_t)n);
445         port_statistics[port].tx += ret;
446         if (unlikely(ret < n)) {
447                 port_statistics[port].dropped += (n - ret);
448                 do {
449                         rte_pktmbuf_free(pkt_buffer[ret]);
450                 } while (++ret < n);
451         }
452
453         return 0;
454 }
455
456 /* Enqueue packets for TX and prepare them to be sent */
457 static int
458 l2fwd_send_packet(struct rte_mbuf *m, uint8_t port)
459 {
460         unsigned lcore_id, len;
461         struct lcore_queue_conf *qconf;
462
463         lcore_id = rte_lcore_id();
464
465         qconf = &lcore_queue_conf[lcore_id];
466         len = qconf->pkt_buf[port].len;
467         qconf->pkt_buf[port].buffer[len] = m;
468         len++;
469
470         /* enough pkts to be sent */
471         if (unlikely(len == MAX_PKT_BURST)) {
472                 l2fwd_send_burst(qconf, MAX_PKT_BURST, port);
473                 len = 0;
474         }
475
476         qconf->pkt_buf[port].len = len;
477         return 0;
478 }
479
480 static void
481 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
482 {
483         struct ether_hdr *eth;
484         void *tmp;
485         unsigned dst_port;
486
487         dst_port = l2fwd_dst_ports[portid];
488         eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
489
490         /* 02:00:00:00:00:xx */
491         tmp = &eth->d_addr.addr_bytes[0];
492         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
493
494         /* src addr */
495         ether_addr_copy(&l2fwd_ports_eth_addr[dst_port], &eth->s_addr);
496
497         l2fwd_send_packet(m, (uint8_t) dst_port);
498 }
499
500 /** Generate random key */
501 static void
502 generate_random_key(uint8_t *key, unsigned length)
503 {
504         unsigned i;
505
506         for (i = 0; i < length; i++)
507                 key[i] = rand() % 0xff;
508 }
509
510 static struct rte_cryptodev_sym_session *
511 initialize_crypto_session(struct l2fwd_crypto_options *options,
512                 uint8_t cdev_id)
513 {
514         struct rte_crypto_sym_xform *first_xform;
515
516         if (options->xform_chain == L2FWD_CRYPTO_CIPHER_HASH) {
517                 first_xform = &options->cipher_xform;
518                 first_xform->next = &options->auth_xform;
519         } else if (options->xform_chain == L2FWD_CRYPTO_HASH_CIPHER) {
520                 first_xform = &options->auth_xform;
521                 first_xform->next = &options->cipher_xform;
522         } else if (options->xform_chain == L2FWD_CRYPTO_CIPHER_ONLY) {
523                 first_xform = &options->cipher_xform;
524         } else {
525                 first_xform = &options->auth_xform;
526         }
527
528         /* Setup Cipher Parameters */
529         return rte_cryptodev_sym_session_create(cdev_id, first_xform);
530 }
531
532 static void
533 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options);
534
535 /* main processing loop */
536 static void
537 l2fwd_main_loop(struct l2fwd_crypto_options *options)
538 {
539         struct rte_mbuf *m, *pkts_burst[MAX_PKT_BURST];
540         struct rte_crypto_op *ops_burst[MAX_PKT_BURST];
541
542         unsigned lcore_id = rte_lcore_id();
543         uint64_t prev_tsc = 0, diff_tsc, cur_tsc, timer_tsc = 0;
544         unsigned i, j, portid, nb_rx;
545         struct lcore_queue_conf *qconf = &lcore_queue_conf[lcore_id];
546         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) /
547                         US_PER_S * BURST_TX_DRAIN_US;
548         struct l2fwd_crypto_params *cparams;
549         struct l2fwd_crypto_params port_cparams[qconf->nb_crypto_devs];
550
551         if (qconf->nb_rx_ports == 0) {
552                 RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
553                 return;
554         }
555
556         RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
557
558         l2fwd_crypto_options_print(options);
559
560         for (i = 0; i < qconf->nb_rx_ports; i++) {
561
562                 portid = qconf->rx_port_list[i];
563                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
564                         portid);
565         }
566
567         for (i = 0; i < qconf->nb_crypto_devs; i++) {
568                 port_cparams[i].do_cipher = 0;
569                 port_cparams[i].do_hash = 0;
570
571                 switch (options->xform_chain) {
572                 case L2FWD_CRYPTO_CIPHER_HASH:
573                 case L2FWD_CRYPTO_HASH_CIPHER:
574                         port_cparams[i].do_cipher = 1;
575                         port_cparams[i].do_hash = 1;
576                         break;
577                 case L2FWD_CRYPTO_HASH_ONLY:
578                         port_cparams[i].do_hash = 1;
579                         break;
580                 case L2FWD_CRYPTO_CIPHER_ONLY:
581                         port_cparams[i].do_cipher = 1;
582                         break;
583                 }
584
585                 port_cparams[i].dev_id = qconf->cryptodev_list[i];
586                 port_cparams[i].qp_id = 0;
587
588                 port_cparams[i].block_size = 64;
589
590                 if (port_cparams[i].do_hash) {
591                         port_cparams[i].digest_length = 20;
592
593                         port_cparams[i].aad.length =
594                                         options->auth_xform.auth.add_auth_data_length;
595                         port_cparams[i].aad.phys_addr = options->aad.phys_addr;
596                         port_cparams[i].aad.data = options->aad.data;
597                         if (!options->aad_param)
598                                 generate_random_key(options->aad.data,
599                                                 port_cparams[i].aad.length);
600                 }
601
602                 if (port_cparams[i].do_cipher) {
603                         port_cparams[i].iv.length = 16;
604                         port_cparams[i].iv.data = options->iv.data;
605                         port_cparams[i].iv.phys_addr = options->iv.phys_addr;
606                         if (!options->iv_param)
607                                 generate_random_key(options->iv.data,
608                                         port_cparams[i].iv.length);
609                 }
610
611
612                 port_cparams[i].session = initialize_crypto_session(options,
613                                 port_cparams[i].dev_id);
614
615                 if (port_cparams[i].session == NULL)
616                         return;
617                 RTE_LOG(INFO, L2FWD, " -- lcoreid=%u cryptoid=%u\n", lcore_id,
618                                 port_cparams[i].dev_id);
619         }
620
621         while (1) {
622
623                 cur_tsc = rte_rdtsc();
624
625                 /*
626                  * TX burst queue drain
627                  */
628                 diff_tsc = cur_tsc - prev_tsc;
629                 if (unlikely(diff_tsc > drain_tsc)) {
630
631                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
632                                 if (qconf->pkt_buf[portid].len == 0)
633                                         continue;
634                                 l2fwd_send_burst(&lcore_queue_conf[lcore_id],
635                                                  qconf->pkt_buf[portid].len,
636                                                  (uint8_t) portid);
637                                 qconf->pkt_buf[portid].len = 0;
638                         }
639
640                         /* if timer is enabled */
641                         if (timer_period > 0) {
642
643                                 /* advance the timer */
644                                 timer_tsc += diff_tsc;
645
646                                 /* if timer has reached its timeout */
647                                 if (unlikely(timer_tsc >=
648                                                 (uint64_t)timer_period)) {
649
650                                         /* do this only on master core */
651                                         if (lcore_id == rte_get_master_lcore()
652                                                 && options->refresh_period) {
653                                                 print_stats();
654                                                 timer_tsc = 0;
655                                         }
656                                 }
657                         }
658
659                         prev_tsc = cur_tsc;
660                 }
661
662                 /*
663                  * Read packet from RX queues
664                  */
665                 for (i = 0; i < qconf->nb_rx_ports; i++) {
666                         portid = qconf->rx_port_list[i];
667
668                         cparams = &port_cparams[i];
669
670                         nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
671                                                  pkts_burst, MAX_PKT_BURST);
672
673                         port_statistics[portid].rx += nb_rx;
674
675                         if (nb_rx) {
676                                 /*
677                                  * If we can't allocate a crypto_ops, then drop
678                                  * the rest of the burst and dequeue and
679                                  * process the packets to free offload structs
680                                  */
681                                 if (rte_crypto_op_bulk_alloc(
682                                                 l2fwd_crypto_op_pool,
683                                                 RTE_CRYPTO_OP_TYPE_SYMMETRIC,
684                                                 ops_burst, nb_rx) !=
685                                                                 nb_rx) {
686                                         for (j = 0; j < nb_rx; j++)
687                                                 rte_pktmbuf_free(pkts_burst[i]);
688
689                                         nb_rx = 0;
690                                 }
691
692                                 /* Enqueue packets from Crypto device*/
693                                 for (j = 0; j < nb_rx; j++) {
694                                         m = pkts_burst[j];
695
696                                         l2fwd_simple_crypto_enqueue(m,
697                                                         ops_burst[j], cparams);
698                                 }
699                         }
700
701                         /* Dequeue packets from Crypto device */
702                         do {
703                                 nb_rx = rte_cryptodev_dequeue_burst(
704                                                 cparams->dev_id, cparams->qp_id,
705                                                 ops_burst, MAX_PKT_BURST);
706
707                                 crypto_statistics[cparams->dev_id].dequeued +=
708                                                 nb_rx;
709
710                                 /* Forward crypto'd packets */
711                                 for (j = 0; j < nb_rx; j++) {
712                                         m = ops_burst[j]->sym->m_src;
713
714                                         rte_crypto_op_free(ops_burst[j]);
715                                         l2fwd_simple_forward(m, portid);
716                                 }
717                         } while (nb_rx == MAX_PKT_BURST);
718                 }
719         }
720 }
721
722 static int
723 l2fwd_launch_one_lcore(void *arg)
724 {
725         l2fwd_main_loop((struct l2fwd_crypto_options *)arg);
726         return 0;
727 }
728
729 /* Display command line arguments usage */
730 static void
731 l2fwd_crypto_usage(const char *prgname)
732 {
733         printf("%s [EAL options] -- --cdev TYPE [optional parameters]\n"
734                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
735                 "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
736                 "  -s manage all ports from single lcore"
737                 "  -t PERIOD: statistics will be refreshed each PERIOD seconds"
738                 " (0 to disable, 10 default, 86400 maximum)\n"
739
740                 "  --cdev AESNI_MB / QAT\n"
741                 "  --chain HASH_CIPHER / CIPHER_HASH\n"
742
743                 "  --cipher_algo ALGO\n"
744                 "  --cipher_op ENCRYPT / DECRYPT\n"
745                 "  --cipher_key KEY\n"
746                 "  --iv IV\n"
747
748                 "  --auth_algo ALGO\n"
749                 "  --auth_op GENERATE / VERIFY\n"
750                 "  --auth_key KEY\n"
751                 "  --aad AAD\n"
752
753                 "  --sessionless\n",
754                prgname);
755 }
756
757 /** Parse crypto device type command line argument */
758 static int
759 parse_cryptodev_type(enum rte_cryptodev_type *type, char *optarg)
760 {
761         if (strcmp("AESNI_MB", optarg) == 0) {
762                 *type = RTE_CRYPTODEV_AESNI_MB_PMD;
763                 return 0;
764         } else if (strcmp("QAT", optarg) == 0) {
765                 *type = RTE_CRYPTODEV_QAT_SYM_PMD;
766                 return 0;
767         }
768
769         return -1;
770 }
771
772 /** Parse crypto chain xform command line argument */
773 static int
774 parse_crypto_opt_chain(struct l2fwd_crypto_options *options, char *optarg)
775 {
776         if (strcmp("CIPHER_HASH", optarg) == 0) {
777                 options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
778                 return 0;
779         } else if (strcmp("HASH_CIPHER", optarg) == 0) {
780                 options->xform_chain = L2FWD_CRYPTO_HASH_CIPHER;
781                 return 0;
782         } else if (strcmp("CIPHER_ONLY", optarg) == 0) {
783                 options->xform_chain = L2FWD_CRYPTO_CIPHER_ONLY;
784                 return 0;
785         } else if (strcmp("HASH_ONLY", optarg) == 0) {
786                 options->xform_chain = L2FWD_CRYPTO_HASH_ONLY;
787                 return 0;
788         }
789
790         return -1;
791 }
792
793 /** Parse crypto cipher algo option command line argument */
794 static int
795 parse_cipher_algo(enum rte_crypto_cipher_algorithm *algo, char *optarg)
796 {
797         if (strcmp("AES_CBC", optarg) == 0) {
798                 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
799                 return 0;
800         } else if (strcmp("AES_GCM", optarg) == 0) {
801                 *algo = RTE_CRYPTO_CIPHER_AES_GCM;
802                 return 0;
803         }
804
805         printf("Cipher algorithm  not supported!\n");
806         return -1;
807 }
808
809 /** Parse crypto cipher operation command line argument */
810 static int
811 parse_cipher_op(enum rte_crypto_cipher_operation *op, char *optarg)
812 {
813         if (strcmp("ENCRYPT", optarg) == 0) {
814                 *op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
815                 return 0;
816         } else if (strcmp("DECRYPT", optarg) == 0) {
817                 *op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
818                 return 0;
819         }
820
821         printf("Cipher operation not supported!\n");
822         return -1;
823 }
824
825 /** Parse crypto key command line argument */
826 static int
827 parse_key(uint8_t *data, char *input_arg)
828 {
829         unsigned byte_count;
830         char *token;
831
832         for (byte_count = 0, token = strtok(input_arg, ":");
833                         (byte_count < MAX_KEY_SIZE) && (token != NULL);
834                         token = strtok(NULL, ":")) {
835
836                 int number = (int)strtol(token, NULL, 16);
837
838                 if (errno == EINVAL || errno == ERANGE || number > 0xFF)
839                         return -1;
840
841                 data[byte_count++] = (uint8_t)number;
842         }
843
844         return 0;
845 }
846
847 /** Parse crypto cipher operation command line argument */
848 static int
849 parse_auth_algo(enum rte_crypto_auth_algorithm *algo, char *optarg)
850 {
851         if (strcmp("MD5_HMAC", optarg) == 0) {
852                 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
853                 return 0;
854         } else if (strcmp("SHA1_HMAC", optarg) == 0) {
855                 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
856                 return 0;
857         } else if (strcmp("SHA224_HMAC", optarg) == 0) {
858                 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
859                 return 0;
860         } else if (strcmp("SHA256_HMAC", optarg) == 0) {
861                 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
862                 return 0;
863         }  else if (strcmp("SHA384_HMAC", optarg) == 0) {
864                 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
865                 return 0;
866         } else if (strcmp("SHA512_HMAC", optarg) == 0) {
867                 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
868                 return 0;
869         }
870
871         printf("Authentication algorithm specified not supported!\n");
872         return -1;
873 }
874
875 static int
876 parse_auth_op(enum rte_crypto_auth_operation *op, char *optarg)
877 {
878         if (strcmp("VERIFY", optarg) == 0) {
879                 *op = RTE_CRYPTO_AUTH_OP_VERIFY;
880                 return 0;
881         } else if (strcmp("GENERATE", optarg) == 0) {
882                 *op = RTE_CRYPTO_AUTH_OP_GENERATE;
883                 return 0;
884         }
885
886         printf("Authentication operation specified not supported!\n");
887         return -1;
888 }
889
890 /** Parse long options */
891 static int
892 l2fwd_crypto_parse_args_long_options(struct l2fwd_crypto_options *options,
893                 struct option *lgopts, int option_index)
894 {
895         if (strcmp(lgopts[option_index].name, "cdev_type") == 0)
896                 return parse_cryptodev_type(&options->cdev_type, optarg);
897
898         else if (strcmp(lgopts[option_index].name, "chain") == 0)
899                 return parse_crypto_opt_chain(options, optarg);
900
901         /* Cipher options */
902         else if (strcmp(lgopts[option_index].name, "cipher_algo") == 0)
903                 return parse_cipher_algo(&options->cipher_xform.cipher.algo,
904                                 optarg);
905
906         else if (strcmp(lgopts[option_index].name, "cipher_op") == 0)
907                 return parse_cipher_op(&options->cipher_xform.cipher.op,
908                                 optarg);
909
910         else if (strcmp(lgopts[option_index].name, "cipher_key") == 0) {
911                 options->ckey_param = 1;
912                 return parse_key(options->cipher_xform.cipher.key.data, optarg);
913         }
914
915         else if (strcmp(lgopts[option_index].name, "iv") == 0) {
916                 options->iv_param = 1;
917                 return parse_key(options->iv.data, optarg);
918         }
919
920         /* Authentication options */
921         else if (strcmp(lgopts[option_index].name, "auth_algo") == 0)
922                 return parse_auth_algo(&options->auth_xform.auth.algo,
923                                 optarg);
924
925         else if (strcmp(lgopts[option_index].name, "auth_op") == 0)
926                 return parse_auth_op(&options->auth_xform.auth.op,
927                                 optarg);
928
929         else if (strcmp(lgopts[option_index].name, "auth_key") == 0) {
930                 options->akey_param = 1;
931                 return parse_key(options->auth_xform.auth.key.data, optarg);
932         }
933
934         else if (strcmp(lgopts[option_index].name, "aad") == 0) {
935                 options->aad_param = 1;
936                 return parse_key(options->aad.data, optarg);
937         }
938
939         else if (strcmp(lgopts[option_index].name, "sessionless") == 0) {
940                 options->sessionless = 1;
941                 return 0;
942         }
943
944         return -1;
945 }
946
947 /** Parse port mask */
948 static int
949 l2fwd_crypto_parse_portmask(struct l2fwd_crypto_options *options,
950                 const char *q_arg)
951 {
952         char *end = NULL;
953         unsigned long pm;
954
955         /* parse hexadecimal string */
956         pm = strtoul(q_arg, &end, 16);
957         if ((pm == '\0') || (end == NULL) || (*end != '\0'))
958                 pm = 0;
959
960         options->portmask = pm;
961         if (options->portmask == 0) {
962                 printf("invalid portmask specified\n");
963                 return -1;
964         }
965
966         return pm;
967 }
968
969 /** Parse number of queues */
970 static int
971 l2fwd_crypto_parse_nqueue(struct l2fwd_crypto_options *options,
972                 const char *q_arg)
973 {
974         char *end = NULL;
975         unsigned long n;
976
977         /* parse hexadecimal string */
978         n = strtoul(q_arg, &end, 10);
979         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
980                 n = 0;
981         else if (n >= MAX_RX_QUEUE_PER_LCORE)
982                 n = 0;
983
984         options->nb_ports_per_lcore = n;
985         if (options->nb_ports_per_lcore == 0) {
986                 printf("invalid number of ports selected\n");
987                 return -1;
988         }
989
990         return 0;
991 }
992
993 /** Parse timer period */
994 static int
995 l2fwd_crypto_parse_timer_period(struct l2fwd_crypto_options *options,
996                 const char *q_arg)
997 {
998         char *end = NULL;
999         unsigned long n;
1000
1001         /* parse number string */
1002         n = (unsigned)strtol(q_arg, &end, 10);
1003         if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
1004                 n = 0;
1005
1006         if (n >= MAX_TIMER_PERIOD) {
1007                 printf("Warning refresh period specified %lu is greater than "
1008                                 "max value %lu! using max value",
1009                                 n, MAX_TIMER_PERIOD);
1010                 n = MAX_TIMER_PERIOD;
1011         }
1012
1013         options->refresh_period = n * 1000 * TIMER_MILLISECOND;
1014
1015         return 0;
1016 }
1017
1018 /** Generate default options for application */
1019 static void
1020 l2fwd_crypto_default_options(struct l2fwd_crypto_options *options)
1021 {
1022         srand(time(NULL));
1023
1024         options->portmask = 0xffffffff;
1025         options->nb_ports_per_lcore = 1;
1026         options->refresh_period = 10000;
1027         options->single_lcore = 0;
1028         options->sessionless = 0;
1029
1030         options->cdev_type = RTE_CRYPTODEV_AESNI_MB_PMD;
1031         options->xform_chain = L2FWD_CRYPTO_CIPHER_HASH;
1032
1033         /* Cipher Data */
1034         options->cipher_xform.type = RTE_CRYPTO_SYM_XFORM_CIPHER;
1035         options->cipher_xform.next = NULL;
1036         options->ckey_param = 0;
1037         options->iv_param = 0;
1038
1039         options->cipher_xform.cipher.algo = RTE_CRYPTO_CIPHER_AES_CBC;
1040         options->cipher_xform.cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
1041
1042         options->cipher_xform.cipher.key.length = 16;
1043
1044
1045         /* Authentication Data */
1046         options->auth_xform.type = RTE_CRYPTO_SYM_XFORM_AUTH;
1047         options->auth_xform.next = NULL;
1048         options->akey_param = 0;
1049         options->aad_param = 0;
1050
1051         options->auth_xform.auth.algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
1052         options->auth_xform.auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
1053
1054         options->auth_xform.auth.add_auth_data_length = 0;
1055         options->auth_xform.auth.digest_length = 20;
1056
1057         options->auth_xform.auth.key.length = 20;
1058 }
1059
1060 static void
1061 l2fwd_crypto_options_print(struct l2fwd_crypto_options *options)
1062 {
1063         printf("Options:-\nn");
1064         printf("portmask: %x\n", options->portmask);
1065         printf("ports per lcore: %u\n", options->nb_ports_per_lcore);
1066         printf("refresh period : %u\n", options->refresh_period);
1067         printf("single lcore mode: %s\n",
1068                         options->single_lcore ? "enabled" : "disabled");
1069         printf("stats_printing: %s\n",
1070                         options->refresh_period == 0 ? "disabled" : "enabled");
1071
1072         switch (options->cdev_type) {
1073         case RTE_CRYPTODEV_AESNI_MB_PMD:
1074                 printf("cryptodev type: AES-NI MB PMD\n"); break;
1075         case RTE_CRYPTODEV_QAT_SYM_PMD:
1076                 printf("cryptodev type: QAT PMD\n"); break;
1077         default:
1078                 break;
1079         }
1080
1081         printf("sessionless crypto: %s\n",
1082                         options->sessionless ? "enabled" : "disabled");
1083 }
1084
1085 /* Parse the argument given in the command line of the application */
1086 static int
1087 l2fwd_crypto_parse_args(struct l2fwd_crypto_options *options,
1088                 int argc, char **argv)
1089 {
1090         int opt, retval, option_index;
1091         char **argvopt = argv, *prgname = argv[0];
1092
1093         static struct option lgopts[] = {
1094                         { "sessionless", no_argument, 0, 0 },
1095
1096                         { "cdev_type", required_argument, 0, 0 },
1097                         { "chain", required_argument, 0, 0 },
1098
1099                         { "cipher_algo", required_argument, 0, 0 },
1100                         { "cipher_op", required_argument, 0, 0 },
1101                         { "cipher_key", required_argument, 0, 0 },
1102
1103                         { "auth_algo", required_argument, 0, 0 },
1104                         { "auth_op", required_argument, 0, 0 },
1105                         { "auth_key", required_argument, 0, 0 },
1106
1107                         { "iv", required_argument, 0, 0 },
1108                         { "aad", required_argument, 0, 0 },
1109
1110                         { "sessionless", no_argument, 0, 0 },
1111
1112                         { NULL, 0, 0, 0 }
1113         };
1114
1115         l2fwd_crypto_default_options(options);
1116
1117         while ((opt = getopt_long(argc, argvopt, "p:q:st:", lgopts,
1118                         &option_index)) != EOF) {
1119                 switch (opt) {
1120                 /* long options */
1121                 case 0:
1122                         retval = l2fwd_crypto_parse_args_long_options(options,
1123                                         lgopts, option_index);
1124                         if (retval < 0) {
1125                                 l2fwd_crypto_usage(prgname);
1126                                 return -1;
1127                         }
1128                         break;
1129
1130                 /* portmask */
1131                 case 'p':
1132                         retval = l2fwd_crypto_parse_portmask(options, optarg);
1133                         if (retval < 0) {
1134                                 l2fwd_crypto_usage(prgname);
1135                                 return -1;
1136                         }
1137                         break;
1138
1139                 /* nqueue */
1140                 case 'q':
1141                         retval = l2fwd_crypto_parse_nqueue(options, optarg);
1142                         if (retval < 0) {
1143                                 l2fwd_crypto_usage(prgname);
1144                                 return -1;
1145                         }
1146                         break;
1147
1148                 /* single  */
1149                 case 's':
1150                         options->single_lcore = 1;
1151
1152                         break;
1153
1154                 /* timer period */
1155                 case 't':
1156                         retval = l2fwd_crypto_parse_timer_period(options,
1157                                         optarg);
1158                         if (retval < 0) {
1159                                 l2fwd_crypto_usage(prgname);
1160                                 return -1;
1161                         }
1162                         break;
1163
1164                 default:
1165                         l2fwd_crypto_usage(prgname);
1166                         return -1;
1167                 }
1168         }
1169
1170
1171         if (optind >= 0)
1172                 argv[optind-1] = prgname;
1173
1174         retval = optind-1;
1175         optind = 0; /* reset getopt lib */
1176
1177         return retval;
1178 }
1179
1180 /* Check the link status of all ports in up to 9s, and print them finally */
1181 static void
1182 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1183 {
1184 #define CHECK_INTERVAL 100 /* 100ms */
1185 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1186         uint8_t portid, count, all_ports_up, print_flag = 0;
1187         struct rte_eth_link link;
1188
1189         printf("\nChecking link status");
1190         fflush(stdout);
1191         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1192                 all_ports_up = 1;
1193                 for (portid = 0; portid < port_num; portid++) {
1194                         if ((port_mask & (1 << portid)) == 0)
1195                                 continue;
1196                         memset(&link, 0, sizeof(link));
1197                         rte_eth_link_get_nowait(portid, &link);
1198                         /* print link status if flag set */
1199                         if (print_flag == 1) {
1200                                 if (link.link_status)
1201                                         printf("Port %d Link Up - speed %u "
1202                                                 "Mbps - %s\n", (uint8_t)portid,
1203                                                 (unsigned)link.link_speed,
1204                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1205                                         ("full-duplex") : ("half-duplex\n"));
1206                                 else
1207                                         printf("Port %d Link Down\n",
1208                                                 (uint8_t)portid);
1209                                 continue;
1210                         }
1211                         /* clear all_ports_up flag if any link down */
1212                         if (link.link_status == 0) {
1213                                 all_ports_up = 0;
1214                                 break;
1215                         }
1216                 }
1217                 /* after finally printing all link status, get out */
1218                 if (print_flag == 1)
1219                         break;
1220
1221                 if (all_ports_up == 0) {
1222                         printf(".");
1223                         fflush(stdout);
1224                         rte_delay_ms(CHECK_INTERVAL);
1225                 }
1226
1227                 /* set the print_flag if all ports up or timeout */
1228                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1229                         print_flag = 1;
1230                         printf("done\n");
1231                 }
1232         }
1233 }
1234
1235 static int
1236 initialize_cryptodevs(struct l2fwd_crypto_options *options, unsigned nb_ports)
1237 {
1238         unsigned i, cdev_id, cdev_count, enabled_cdev_count = 0;
1239         int retval;
1240
1241         if (options->cdev_type == RTE_CRYPTODEV_QAT_SYM_PMD) {
1242                 if (rte_cryptodev_count() < nb_ports)
1243                         return -1;
1244         } else if (options->cdev_type == RTE_CRYPTODEV_AESNI_MB_PMD) {
1245                 for (i = 0; i < nb_ports; i++) {
1246                         int retval = rte_eal_vdev_init(CRYPTODEV_NAME_AESNI_MB_PMD,
1247                                         NULL);
1248                         if (retval < 0)
1249                                 return -1;
1250                 }
1251         }
1252
1253         cdev_count = rte_cryptodev_count();
1254         for (cdev_id = 0;
1255                         cdev_id < cdev_count && enabled_cdev_count < nb_ports;
1256                         cdev_id++) {
1257                 struct rte_cryptodev_qp_conf qp_conf;
1258                 struct rte_cryptodev_info dev_info;
1259
1260                 struct rte_cryptodev_config conf = {
1261                         .nb_queue_pairs = 1,
1262                         .socket_id = SOCKET_ID_ANY,
1263                         .session_mp = {
1264                                 .nb_objs = 2048,
1265                                 .cache_size = 64
1266                         }
1267                 };
1268
1269                 rte_cryptodev_info_get(cdev_id, &dev_info);
1270
1271                 if (dev_info.dev_type != options->cdev_type)
1272                         continue;
1273
1274
1275                 retval = rte_cryptodev_configure(cdev_id, &conf);
1276                 if (retval < 0) {
1277                         printf("Failed to configure cryptodev %u", cdev_id);
1278                         return -1;
1279                 }
1280
1281                 qp_conf.nb_descriptors = 2048;
1282
1283                 retval = rte_cryptodev_queue_pair_setup(cdev_id, 0, &qp_conf,
1284                                 SOCKET_ID_ANY);
1285                 if (retval < 0) {
1286                         printf("Failed to setup queue pair %u on cryptodev %u",
1287                                         0, cdev_id);
1288                         return -1;
1289                 }
1290
1291                 l2fwd_enabled_crypto_mask |= (1 << cdev_id);
1292
1293                 enabled_cdev_count++;
1294         }
1295
1296         return enabled_cdev_count;
1297 }
1298
1299 static int
1300 initialize_ports(struct l2fwd_crypto_options *options)
1301 {
1302         uint8_t last_portid, portid;
1303         unsigned enabled_portcount = 0;
1304         unsigned nb_ports = rte_eth_dev_count();
1305
1306         if (nb_ports == 0) {
1307                 printf("No Ethernet ports - bye\n");
1308                 return -1;
1309         }
1310
1311         if (nb_ports > RTE_MAX_ETHPORTS)
1312                 nb_ports = RTE_MAX_ETHPORTS;
1313
1314         /* Reset l2fwd_dst_ports */
1315         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
1316                 l2fwd_dst_ports[portid] = 0;
1317
1318         for (last_portid = 0, portid = 0; portid < nb_ports; portid++) {
1319                 int retval;
1320
1321                 /* Skip ports that are not enabled */
1322                 if ((options->portmask & (1 << portid)) == 0)
1323                         continue;
1324
1325                 /* init port */
1326                 printf("Initializing port %u... ", (unsigned) portid);
1327                 fflush(stdout);
1328                 retval = rte_eth_dev_configure(portid, 1, 1, &port_conf);
1329                 if (retval < 0) {
1330                         printf("Cannot configure device: err=%d, port=%u\n",
1331                                   retval, (unsigned) portid);
1332                         return -1;
1333                 }
1334
1335                 /* init one RX queue */
1336                 fflush(stdout);
1337                 retval = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
1338                                              rte_eth_dev_socket_id(portid),
1339                                              NULL, l2fwd_pktmbuf_pool);
1340                 if (retval < 0) {
1341                         printf("rte_eth_rx_queue_setup:err=%d, port=%u\n",
1342                                         retval, (unsigned) portid);
1343                         return -1;
1344                 }
1345
1346                 /* init one TX queue on each port */
1347                 fflush(stdout);
1348                 retval = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1349                                 rte_eth_dev_socket_id(portid),
1350                                 NULL);
1351                 if (retval < 0) {
1352                         printf("rte_eth_tx_queue_setup:err=%d, port=%u\n",
1353                                 retval, (unsigned) portid);
1354
1355                         return -1;
1356                 }
1357
1358                 /* Start device */
1359                 retval = rte_eth_dev_start(portid);
1360                 if (retval < 0) {
1361                         printf("rte_eth_dev_start:err=%d, port=%u\n",
1362                                         retval, (unsigned) portid);
1363                         return -1;
1364                 }
1365
1366                 rte_eth_promiscuous_enable(portid);
1367
1368                 rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]);
1369
1370                 printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
1371                                 (unsigned) portid,
1372                                 l2fwd_ports_eth_addr[portid].addr_bytes[0],
1373                                 l2fwd_ports_eth_addr[portid].addr_bytes[1],
1374                                 l2fwd_ports_eth_addr[portid].addr_bytes[2],
1375                                 l2fwd_ports_eth_addr[portid].addr_bytes[3],
1376                                 l2fwd_ports_eth_addr[portid].addr_bytes[4],
1377                                 l2fwd_ports_eth_addr[portid].addr_bytes[5]);
1378
1379                 /* initialize port stats */
1380                 memset(&port_statistics, 0, sizeof(port_statistics));
1381
1382                 /* Setup port forwarding table */
1383                 if (enabled_portcount % 2) {
1384                         l2fwd_dst_ports[portid] = last_portid;
1385                         l2fwd_dst_ports[last_portid] = portid;
1386                 } else {
1387                         last_portid = portid;
1388                 }
1389
1390                 l2fwd_enabled_port_mask |= (1 << portid);
1391                 enabled_portcount++;
1392         }
1393
1394         if (enabled_portcount == 1) {
1395                 l2fwd_dst_ports[last_portid] = last_portid;
1396         } else if (enabled_portcount % 2) {
1397                 printf("odd number of ports in portmask- bye\n");
1398                 return -1;
1399         }
1400
1401         check_all_ports_link_status(nb_ports, l2fwd_enabled_port_mask);
1402
1403         return enabled_portcount;
1404 }
1405
1406 static void
1407 reserve_key_memory(struct l2fwd_crypto_options *options)
1408 {
1409         options->cipher_xform.cipher.key.data = rte_malloc("crypto key",
1410                                                 MAX_KEY_SIZE, 0);
1411         if (options->cipher_xform.cipher.key.data == NULL)
1412                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for cipher key");
1413
1414
1415         options->auth_xform.auth.key.data = rte_malloc("auth key",
1416                                                 MAX_KEY_SIZE, 0);
1417         if (options->auth_xform.auth.key.data == NULL)
1418                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for auth key");
1419
1420         options->iv.data = rte_malloc("iv", MAX_KEY_SIZE, 0);
1421         if (options->iv.data == NULL)
1422                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for IV");
1423         options->iv.phys_addr = rte_malloc_virt2phy(options->iv.data);
1424
1425         options->aad.data = rte_malloc("aad", MAX_KEY_SIZE, 0);
1426         if (options->aad.data == NULL)
1427                 rte_exit(EXIT_FAILURE, "Failed to allocate memory for AAD");
1428         options->aad.phys_addr = rte_malloc_virt2phy(options->aad.data);
1429 }
1430
1431 int
1432 main(int argc, char **argv)
1433 {
1434         struct lcore_queue_conf *qconf;
1435         struct l2fwd_crypto_options options;
1436
1437         uint8_t nb_ports, nb_cryptodevs, portid, cdev_id;
1438         unsigned lcore_id, rx_lcore_id;
1439         int ret, enabled_cdevcount, enabled_portcount;
1440
1441         /* init EAL */
1442         ret = rte_eal_init(argc, argv);
1443         if (ret < 0)
1444                 rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
1445         argc -= ret;
1446         argv += ret;
1447
1448         /* reserve memory for Cipher/Auth key and IV */
1449         reserve_key_memory(&options);
1450
1451         /* parse application arguments (after the EAL ones) */
1452         ret = l2fwd_crypto_parse_args(&options, argc, argv);
1453         if (ret < 0)
1454                 rte_exit(EXIT_FAILURE, "Invalid L2FWD-CRYPTO arguments\n");
1455
1456         /* create the mbuf pool */
1457         l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 512,
1458                         sizeof(struct rte_crypto_op),
1459                         RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id());
1460         if (l2fwd_pktmbuf_pool == NULL)
1461                 rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n");
1462
1463         /* create crypto op pool */
1464         l2fwd_crypto_op_pool = rte_crypto_op_pool_create("crypto_op_pool",
1465                         RTE_CRYPTO_OP_TYPE_SYMMETRIC, NB_MBUF, 128, 0,
1466                         rte_socket_id());
1467         if (l2fwd_crypto_op_pool == NULL)
1468                 rte_exit(EXIT_FAILURE, "Cannot create crypto op pool\n");
1469
1470         /* Enable Ethernet ports */
1471         enabled_portcount = initialize_ports(&options);
1472         if (enabled_portcount < 1)
1473                 rte_exit(EXIT_FAILURE, "Failed to initial Ethernet ports\n");
1474
1475         nb_ports = rte_eth_dev_count();
1476         /* Initialize the port/queue configuration of each logical core */
1477         for (rx_lcore_id = 0, qconf = NULL, portid = 0;
1478                         portid < nb_ports; portid++) {
1479
1480                 /* skip ports that are not enabled */
1481                 if ((options.portmask & (1 << portid)) == 0)
1482                         continue;
1483
1484                 if (options.single_lcore && qconf == NULL) {
1485                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1486                                 rx_lcore_id++;
1487                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1488                                         rte_exit(EXIT_FAILURE,
1489                                                         "Not enough cores\n");
1490                         }
1491                 } else if (!options.single_lcore) {
1492                         /* get the lcore_id for this port */
1493                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1494                                lcore_queue_conf[rx_lcore_id].nb_rx_ports ==
1495                                options.nb_ports_per_lcore) {
1496                                 rx_lcore_id++;
1497                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1498                                         rte_exit(EXIT_FAILURE,
1499                                                         "Not enough cores\n");
1500                         }
1501                 }
1502
1503                 /* Assigned a new logical core in the loop above. */
1504                 if (qconf != &lcore_queue_conf[rx_lcore_id])
1505                         qconf = &lcore_queue_conf[rx_lcore_id];
1506
1507                 qconf->rx_port_list[qconf->nb_rx_ports] = portid;
1508                 qconf->nb_rx_ports++;
1509
1510                 printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned)portid);
1511         }
1512
1513
1514         /* Enable Crypto devices */
1515         enabled_cdevcount = initialize_cryptodevs(&options, enabled_portcount);
1516         if (enabled_cdevcount < 1)
1517                 rte_exit(EXIT_FAILURE, "Failed to initial crypto devices\n");
1518
1519         nb_cryptodevs = rte_cryptodev_count();
1520         /* Initialize the port/queue configuration of each logical core */
1521         for (rx_lcore_id = 0, qconf = NULL, cdev_id = 0;
1522                         cdev_id < nb_cryptodevs && enabled_cdevcount;
1523                         cdev_id++) {
1524                 struct rte_cryptodev_info info;
1525
1526                 rte_cryptodev_info_get(cdev_id, &info);
1527
1528                 /* skip devices of the wrong type */
1529                 if (options.cdev_type != info.dev_type)
1530                         continue;
1531
1532                 if (options.single_lcore && qconf == NULL) {
1533                         while (rte_lcore_is_enabled(rx_lcore_id) == 0) {
1534                                 rx_lcore_id++;
1535                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1536                                         rte_exit(EXIT_FAILURE,
1537                                                         "Not enough cores\n");
1538                         }
1539                 } else if (!options.single_lcore) {
1540                         /* get the lcore_id for this port */
1541                         while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
1542                                lcore_queue_conf[rx_lcore_id].nb_crypto_devs ==
1543                                options.nb_ports_per_lcore) {
1544                                 rx_lcore_id++;
1545                                 if (rx_lcore_id >= RTE_MAX_LCORE)
1546                                         rte_exit(EXIT_FAILURE,
1547                                                         "Not enough cores\n");
1548                         }
1549                 }
1550
1551                 /* Assigned a new logical core in the loop above. */
1552                 if (qconf != &lcore_queue_conf[rx_lcore_id])
1553                         qconf = &lcore_queue_conf[rx_lcore_id];
1554
1555                 qconf->cryptodev_list[qconf->nb_crypto_devs] = cdev_id;
1556                 qconf->nb_crypto_devs++;
1557
1558                 enabled_cdevcount--;
1559
1560                 printf("Lcore %u: cryptodev %u\n", rx_lcore_id,
1561                                 (unsigned)cdev_id);
1562         }
1563
1564         if (!options.akey_param)
1565                 generate_random_key(options.auth_xform.auth.key.data,
1566                                 options.auth_xform.auth.key.length);
1567
1568         if (!options.ckey_param)
1569                 generate_random_key(options.cipher_xform.cipher.key.data,
1570                                 options.cipher_xform.cipher.key.length);
1571
1572
1573         /* launch per-lcore init on every lcore */
1574         rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, (void *)&options,
1575                         CALL_MASTER);
1576         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1577                 if (rte_eal_wait_lcore(lcore_id) < 0)
1578                         return -1;
1579         }
1580
1581         return 0;
1582 }