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