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