cryptodev: rename functions to get session size
[dpdk.git] / examples / ipsec-secgw / ipsec-secgw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <netinet/in.h>
11 #include <netinet/ip.h>
12 #include <netinet/ip6.h>
13 #include <string.h>
14 #include <sys/queue.h>
15 #include <stdarg.h>
16 #include <errno.h>
17 #include <getopt.h>
18
19 #include <rte_common.h>
20 #include <rte_byteorder.h>
21 #include <rte_log.h>
22 #include <rte_eal.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_mempool.h>
36 #include <rte_mbuf.h>
37 #include <rte_acl.h>
38 #include <rte_lpm.h>
39 #include <rte_lpm6.h>
40 #include <rte_hash.h>
41 #include <rte_jhash.h>
42 #include <rte_cryptodev.h>
43 #include <rte_security.h>
44
45 #include "ipsec.h"
46 #include "parser.h"
47
48 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
49
50 #define MAX_JUMBO_PKT_LEN  9600
51
52 #define MEMPOOL_CACHE_SIZE 256
53
54 #define NB_MBUF (32000)
55
56 #define CDEV_QUEUE_DESC 2048
57 #define CDEV_MAP_ENTRIES 1024
58 #define CDEV_MP_NB_OBJS 2048
59 #define CDEV_MP_CACHE_SZ 64
60 #define MAX_QUEUE_PAIRS 1
61
62 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
63
64 #define NB_SOCKETS 4
65
66 /* Configure how many packets ahead to prefetch, when reading packets */
67 #define PREFETCH_OFFSET 3
68
69 #define MAX_RX_QUEUE_PER_LCORE 16
70
71 #define MAX_LCORE_PARAMS 1024
72
73 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
74
75 /*
76  * Configurable number of RX/TX ring descriptors
77  */
78 #define IPSEC_SECGW_RX_DESC_DEFAULT 1024
79 #define IPSEC_SECGW_TX_DESC_DEFAULT 1024
80 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
81 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
82
83 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
84 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
85         (((uint64_t)((a) & 0xff) << 56) | \
86         ((uint64_t)((b) & 0xff) << 48) | \
87         ((uint64_t)((c) & 0xff) << 40) | \
88         ((uint64_t)((d) & 0xff) << 32) | \
89         ((uint64_t)((e) & 0xff) << 24) | \
90         ((uint64_t)((f) & 0xff) << 16) | \
91         ((uint64_t)((g) & 0xff) << 8)  | \
92         ((uint64_t)(h) & 0xff))
93 #else
94 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
95         (((uint64_t)((h) & 0xff) << 56) | \
96         ((uint64_t)((g) & 0xff) << 48) | \
97         ((uint64_t)((f) & 0xff) << 40) | \
98         ((uint64_t)((e) & 0xff) << 32) | \
99         ((uint64_t)((d) & 0xff) << 24) | \
100         ((uint64_t)((c) & 0xff) << 16) | \
101         ((uint64_t)((b) & 0xff) << 8) | \
102         ((uint64_t)(a) & 0xff))
103 #endif
104 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
105
106 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
107                 addr.addr_bytes[0], addr.addr_bytes[1], \
108                 addr.addr_bytes[2], addr.addr_bytes[3], \
109                 addr.addr_bytes[4], addr.addr_bytes[5], \
110                 0, 0)
111
112 /* port/source ethernet addr and destination ethernet addr */
113 struct ethaddr_info {
114         uint64_t src, dst;
115 };
116
117 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
118         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
119         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
120         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
121         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
122 };
123
124 #define CMD_LINE_OPT_CONFIG             "config"
125 #define CMD_LINE_OPT_SINGLE_SA          "single-sa"
126 #define CMD_LINE_OPT_CRYPTODEV_MASK     "cryptodev_mask"
127
128 enum {
129         /* long options mapped to a short option */
130
131         /* first long only option value must be >= 256, so that we won't
132          * conflict with short options
133          */
134         CMD_LINE_OPT_MIN_NUM = 256,
135         CMD_LINE_OPT_CONFIG_NUM,
136         CMD_LINE_OPT_SINGLE_SA_NUM,
137         CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
138 };
139
140 static const struct option lgopts[] = {
141         {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
142         {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
143         {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
144         {NULL, 0, 0, 0}
145 };
146
147 /* mask of enabled ports */
148 static uint32_t enabled_port_mask;
149 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
150 static uint32_t unprotected_port_mask;
151 static int32_t promiscuous_on = 1;
152 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
153 static uint32_t nb_lcores;
154 static uint32_t single_sa;
155 static uint32_t single_sa_idx;
156 static uint32_t frame_size;
157
158 struct lcore_rx_queue {
159         uint16_t port_id;
160         uint8_t queue_id;
161 } __rte_cache_aligned;
162
163 struct lcore_params {
164         uint16_t port_id;
165         uint8_t queue_id;
166         uint8_t lcore_id;
167 } __rte_cache_aligned;
168
169 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
170
171 static struct lcore_params *lcore_params;
172 static uint16_t nb_lcore_params;
173
174 static struct rte_hash *cdev_map_in;
175 static struct rte_hash *cdev_map_out;
176
177 struct buffer {
178         uint16_t len;
179         struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
180 };
181
182 struct lcore_conf {
183         uint16_t nb_rx_queue;
184         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
185         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
186         struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
187         struct ipsec_ctx inbound;
188         struct ipsec_ctx outbound;
189         struct rt_ctx *rt4_ctx;
190         struct rt_ctx *rt6_ctx;
191 } __rte_cache_aligned;
192
193 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
194
195 static struct rte_eth_conf port_conf = {
196         .rxmode = {
197                 .mq_mode        = ETH_MQ_RX_RSS,
198                 .max_rx_pkt_len = ETHER_MAX_LEN,
199                 .split_hdr_size = 0,
200                 .offloads = DEV_RX_OFFLOAD_CHECKSUM |
201                             DEV_RX_OFFLOAD_CRC_STRIP,
202                 .ignore_offload_bitfield = 1,
203         },
204         .rx_adv_conf = {
205                 .rss_conf = {
206                         .rss_key = NULL,
207                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
208                                 ETH_RSS_TCP | ETH_RSS_SCTP,
209                 },
210         },
211         .txmode = {
212                 .mq_mode = ETH_MQ_TX_NONE,
213                 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
214                              DEV_TX_OFFLOAD_MULTI_SEGS),
215         },
216 };
217
218 static struct socket_ctx socket_ctx[NB_SOCKETS];
219
220 struct traffic_type {
221         const uint8_t *data[MAX_PKT_BURST * 2];
222         struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
223         uint32_t res[MAX_PKT_BURST * 2];
224         uint32_t num;
225 };
226
227 struct ipsec_traffic {
228         struct traffic_type ipsec;
229         struct traffic_type ip4;
230         struct traffic_type ip6;
231 };
232
233 static inline void
234 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
235 {
236         uint8_t *nlp;
237         struct ether_hdr *eth;
238
239         eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
240         if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
241                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
242                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p));
243                 if (*nlp == IPPROTO_ESP)
244                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
245                 else {
246                         t->ip4.data[t->ip4.num] = nlp;
247                         t->ip4.pkts[(t->ip4.num)++] = pkt;
248                 }
249         } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
250                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
251                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt));
252                 if (*nlp == IPPROTO_ESP)
253                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
254                 else {
255                         t->ip6.data[t->ip6.num] = nlp;
256                         t->ip6.pkts[(t->ip6.num)++] = pkt;
257                 }
258         } else {
259                 /* Unknown/Unsupported type, drop the packet */
260                 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n");
261                 rte_pktmbuf_free(pkt);
262         }
263
264         /* Check if the packet has been processed inline. For inline protocol
265          * processed packets, the metadata in the mbuf can be used to identify
266          * the security processing done on the packet. The metadata will be
267          * used to retrieve the application registered userdata associated
268          * with the security session.
269          */
270
271         if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
272                 struct ipsec_sa *sa;
273                 struct ipsec_mbuf_metadata *priv;
274                 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
275                                                 rte_eth_dev_get_sec_ctx(
276                                                 pkt->port);
277
278                 /* Retrieve the userdata registered. Here, the userdata
279                  * registered is the SA pointer.
280                  */
281
282                 sa = (struct ipsec_sa *)
283                                 rte_security_get_userdata(ctx, pkt->udata64);
284
285                 if (sa == NULL) {
286                         /* userdata could not be retrieved */
287                         return;
288                 }
289
290                 /* Save SA as priv member in mbuf. This will be used in the
291                  * IPsec selector(SP-SA) check.
292                  */
293
294                 priv = get_priv(pkt);
295                 priv->sa = sa;
296         }
297 }
298
299 static inline void
300 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
301                 uint16_t nb_pkts)
302 {
303         int32_t i;
304
305         t->ipsec.num = 0;
306         t->ip4.num = 0;
307         t->ip6.num = 0;
308
309         for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
310                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
311                                         void *));
312                 prepare_one_packet(pkts[i], t);
313         }
314         /* Process left packets */
315         for (; i < nb_pkts; i++)
316                 prepare_one_packet(pkts[i], t);
317 }
318
319 static inline void
320 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port)
321 {
322         struct ip *ip;
323         struct ether_hdr *ethhdr;
324
325         ip = rte_pktmbuf_mtod(pkt, struct ip *);
326
327         ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN);
328
329         if (ip->ip_v == IPVERSION) {
330                 pkt->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
331                 pkt->l3_len = sizeof(struct ip);
332                 pkt->l2_len = ETHER_HDR_LEN;
333
334                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
335         } else {
336                 pkt->ol_flags |= PKT_TX_IPV6;
337                 pkt->l3_len = sizeof(struct ip6_hdr);
338                 pkt->l2_len = ETHER_HDR_LEN;
339
340                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
341         }
342
343         memcpy(&ethhdr->s_addr, &ethaddr_tbl[port].src,
344                         sizeof(struct ether_addr));
345         memcpy(&ethhdr->d_addr, &ethaddr_tbl[port].dst,
346                         sizeof(struct ether_addr));
347 }
348
349 static inline void
350 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port)
351 {
352         int32_t i;
353         const int32_t prefetch_offset = 2;
354
355         for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
356                 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
357                 prepare_tx_pkt(pkts[i], port);
358         }
359         /* Process left packets */
360         for (; i < nb_pkts; i++)
361                 prepare_tx_pkt(pkts[i], port);
362 }
363
364 /* Send burst of packets on an output interface */
365 static inline int32_t
366 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
367 {
368         struct rte_mbuf **m_table;
369         int32_t ret;
370         uint16_t queueid;
371
372         queueid = qconf->tx_queue_id[port];
373         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
374
375         prepare_tx_burst(m_table, n, port);
376
377         ret = rte_eth_tx_burst(port, queueid, m_table, n);
378         if (unlikely(ret < n)) {
379                 do {
380                         rte_pktmbuf_free(m_table[ret]);
381                 } while (++ret < n);
382         }
383
384         return 0;
385 }
386
387 /* Enqueue a single packet, and send burst if queue is filled */
388 static inline int32_t
389 send_single_packet(struct rte_mbuf *m, uint16_t port)
390 {
391         uint32_t lcore_id;
392         uint16_t len;
393         struct lcore_conf *qconf;
394
395         lcore_id = rte_lcore_id();
396
397         qconf = &lcore_conf[lcore_id];
398         len = qconf->tx_mbufs[port].len;
399         qconf->tx_mbufs[port].m_table[len] = m;
400         len++;
401
402         /* enough pkts to be sent */
403         if (unlikely(len == MAX_PKT_BURST)) {
404                 send_burst(qconf, MAX_PKT_BURST, port);
405                 len = 0;
406         }
407
408         qconf->tx_mbufs[port].len = len;
409         return 0;
410 }
411
412 static inline void
413 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
414                 uint16_t lim)
415 {
416         struct rte_mbuf *m;
417         uint32_t i, j, res, sa_idx;
418
419         if (ip->num == 0 || sp == NULL)
420                 return;
421
422         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
423                         ip->num, DEFAULT_MAX_CATEGORIES);
424
425         j = 0;
426         for (i = 0; i < ip->num; i++) {
427                 m = ip->pkts[i];
428                 res = ip->res[i];
429                 if (res & BYPASS) {
430                         ip->pkts[j++] = m;
431                         continue;
432                 }
433                 if (res & DISCARD) {
434                         rte_pktmbuf_free(m);
435                         continue;
436                 }
437
438                 /* Only check SPI match for processed IPSec packets */
439                 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) {
440                         rte_pktmbuf_free(m);
441                         continue;
442                 }
443
444                 sa_idx = ip->res[i] & PROTECT_MASK;
445                 if (sa_idx >= IPSEC_SA_MAX_ENTRIES ||
446                                 !inbound_sa_check(sa, m, sa_idx)) {
447                         rte_pktmbuf_free(m);
448                         continue;
449                 }
450                 ip->pkts[j++] = m;
451         }
452         ip->num = j;
453 }
454
455 static inline void
456 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
457                 struct ipsec_traffic *traffic)
458 {
459         struct rte_mbuf *m;
460         uint16_t idx, nb_pkts_in, i, n_ip4, n_ip6;
461
462         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
463                         traffic->ipsec.num, MAX_PKT_BURST);
464
465         n_ip4 = traffic->ip4.num;
466         n_ip6 = traffic->ip6.num;
467
468         /* SP/ACL Inbound check ipsec and ip4 */
469         for (i = 0; i < nb_pkts_in; i++) {
470                 m = traffic->ipsec.pkts[i];
471                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
472                 if (ip->ip_v == IPVERSION) {
473                         idx = traffic->ip4.num++;
474                         traffic->ip4.pkts[idx] = m;
475                         traffic->ip4.data[idx] = rte_pktmbuf_mtod_offset(m,
476                                         uint8_t *, offsetof(struct ip, ip_p));
477                 } else if (ip->ip_v == IP6_VERSION) {
478                         idx = traffic->ip6.num++;
479                         traffic->ip6.pkts[idx] = m;
480                         traffic->ip6.data[idx] = rte_pktmbuf_mtod_offset(m,
481                                         uint8_t *,
482                                         offsetof(struct ip6_hdr, ip6_nxt));
483                 } else
484                         rte_pktmbuf_free(m);
485         }
486
487         inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
488                         n_ip4);
489
490         inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
491                         n_ip6);
492 }
493
494 static inline void
495 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
496                 struct traffic_type *ipsec)
497 {
498         struct rte_mbuf *m;
499         uint32_t i, j, sa_idx;
500
501         if (ip->num == 0 || sp == NULL)
502                 return;
503
504         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
505                         ip->num, DEFAULT_MAX_CATEGORIES);
506
507         j = 0;
508         for (i = 0; i < ip->num; i++) {
509                 m = ip->pkts[i];
510                 sa_idx = ip->res[i] & PROTECT_MASK;
511                 if (ip->res[i] & DISCARD)
512                         rte_pktmbuf_free(m);
513                 else if (sa_idx < IPSEC_SA_MAX_ENTRIES) {
514                         ipsec->res[ipsec->num] = sa_idx;
515                         ipsec->pkts[ipsec->num++] = m;
516                 } else /* BYPASS */
517                         ip->pkts[j++] = m;
518         }
519         ip->num = j;
520 }
521
522 static inline void
523 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
524                 struct ipsec_traffic *traffic)
525 {
526         struct rte_mbuf *m;
527         uint16_t idx, nb_pkts_out, i;
528
529         /* Drop any IPsec traffic from protected ports */
530         for (i = 0; i < traffic->ipsec.num; i++)
531                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
532
533         traffic->ipsec.num = 0;
534
535         outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
536
537         outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
538
539         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
540                         traffic->ipsec.res, traffic->ipsec.num,
541                         MAX_PKT_BURST);
542
543         for (i = 0; i < nb_pkts_out; i++) {
544                 m = traffic->ipsec.pkts[i];
545                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
546                 if (ip->ip_v == IPVERSION) {
547                         idx = traffic->ip4.num++;
548                         traffic->ip4.pkts[idx] = m;
549                 } else {
550                         idx = traffic->ip6.num++;
551                         traffic->ip6.pkts[idx] = m;
552                 }
553         }
554 }
555
556 static inline void
557 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
558                 struct ipsec_traffic *traffic)
559 {
560         struct rte_mbuf *m;
561         uint32_t nb_pkts_in, i, idx;
562
563         /* Drop any IPv4 traffic from unprotected ports */
564         for (i = 0; i < traffic->ip4.num; i++)
565                 rte_pktmbuf_free(traffic->ip4.pkts[i]);
566
567         traffic->ip4.num = 0;
568
569         /* Drop any IPv6 traffic from unprotected ports */
570         for (i = 0; i < traffic->ip6.num; i++)
571                 rte_pktmbuf_free(traffic->ip6.pkts[i]);
572
573         traffic->ip6.num = 0;
574
575         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
576                         traffic->ipsec.num, MAX_PKT_BURST);
577
578         for (i = 0; i < nb_pkts_in; i++) {
579                 m = traffic->ipsec.pkts[i];
580                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
581                 if (ip->ip_v == IPVERSION) {
582                         idx = traffic->ip4.num++;
583                         traffic->ip4.pkts[idx] = m;
584                 } else {
585                         idx = traffic->ip6.num++;
586                         traffic->ip6.pkts[idx] = m;
587                 }
588         }
589 }
590
591 static inline void
592 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
593                 struct ipsec_traffic *traffic)
594 {
595         struct rte_mbuf *m;
596         uint32_t nb_pkts_out, i;
597         struct ip *ip;
598
599         /* Drop any IPsec traffic from protected ports */
600         for (i = 0; i < traffic->ipsec.num; i++)
601                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
602
603         traffic->ipsec.num = 0;
604
605         for (i = 0; i < traffic->ip4.num; i++)
606                 traffic->ip4.res[i] = single_sa_idx;
607
608         for (i = 0; i < traffic->ip6.num; i++)
609                 traffic->ip6.res[i] = single_sa_idx;
610
611         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
612                         traffic->ip4.res, traffic->ip4.num,
613                         MAX_PKT_BURST);
614
615         /* They all sue the same SA (ip4 or ip6 tunnel) */
616         m = traffic->ipsec.pkts[i];
617         ip = rte_pktmbuf_mtod(m, struct ip *);
618         if (ip->ip_v == IPVERSION)
619                 traffic->ip4.num = nb_pkts_out;
620         else
621                 traffic->ip6.num = nb_pkts_out;
622 }
623
624 static inline int32_t
625 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
626 {
627         struct ipsec_mbuf_metadata *priv;
628         struct ipsec_sa *sa;
629
630         priv = get_priv(pkt);
631
632         sa = priv->sa;
633         if (unlikely(sa == NULL)) {
634                 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
635                 goto fail;
636         }
637
638         if (is_ipv6)
639                 return sa->portid;
640
641         /* else */
642         return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
643
644 fail:
645         if (is_ipv6)
646                 return -1;
647
648         /* else */
649         return 0;
650 }
651
652 static inline void
653 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
654 {
655         uint32_t hop[MAX_PKT_BURST * 2];
656         uint32_t dst_ip[MAX_PKT_BURST * 2];
657         int32_t pkt_hop = 0;
658         uint16_t i, offset;
659         uint16_t lpm_pkts = 0;
660
661         if (nb_pkts == 0)
662                 return;
663
664         /* Need to do an LPM lookup for non-inline packets. Inline packets will
665          * have port ID in the SA
666          */
667
668         for (i = 0; i < nb_pkts; i++) {
669                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
670                         /* Security offload not enabled. So an LPM lookup is
671                          * required to get the hop
672                          */
673                         offset = offsetof(struct ip, ip_dst);
674                         dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
675                                         uint32_t *, offset);
676                         dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
677                         lpm_pkts++;
678                 }
679         }
680
681         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
682
683         lpm_pkts = 0;
684
685         for (i = 0; i < nb_pkts; i++) {
686                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
687                         /* Read hop from the SA */
688                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
689                 } else {
690                         /* Need to use hop returned by lookup */
691                         pkt_hop = hop[lpm_pkts++];
692                 }
693
694                 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
695                         rte_pktmbuf_free(pkts[i]);
696                         continue;
697                 }
698                 send_single_packet(pkts[i], pkt_hop & 0xff);
699         }
700 }
701
702 static inline void
703 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
704 {
705         int32_t hop[MAX_PKT_BURST * 2];
706         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
707         uint8_t *ip6_dst;
708         int32_t pkt_hop = 0;
709         uint16_t i, offset;
710         uint16_t lpm_pkts = 0;
711
712         if (nb_pkts == 0)
713                 return;
714
715         /* Need to do an LPM lookup for non-inline packets. Inline packets will
716          * have port ID in the SA
717          */
718
719         for (i = 0; i < nb_pkts; i++) {
720                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
721                         /* Security offload not enabled. So an LPM lookup is
722                          * required to get the hop
723                          */
724                         offset = offsetof(struct ip6_hdr, ip6_dst);
725                         ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
726                                         offset);
727                         memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
728                         lpm_pkts++;
729                 }
730         }
731
732         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
733                         lpm_pkts);
734
735         lpm_pkts = 0;
736
737         for (i = 0; i < nb_pkts; i++) {
738                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
739                         /* Read hop from the SA */
740                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
741                 } else {
742                         /* Need to use hop returned by lookup */
743                         pkt_hop = hop[lpm_pkts++];
744                 }
745
746                 if (pkt_hop == -1) {
747                         rte_pktmbuf_free(pkts[i]);
748                         continue;
749                 }
750                 send_single_packet(pkts[i], pkt_hop & 0xff);
751         }
752 }
753
754 static inline void
755 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
756                 uint8_t nb_pkts, uint16_t portid)
757 {
758         struct ipsec_traffic traffic;
759
760         prepare_traffic(pkts, &traffic, nb_pkts);
761
762         if (unlikely(single_sa)) {
763                 if (UNPROTECTED_PORT(portid))
764                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
765                 else
766                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
767         } else {
768                 if (UNPROTECTED_PORT(portid))
769                         process_pkts_inbound(&qconf->inbound, &traffic);
770                 else
771                         process_pkts_outbound(&qconf->outbound, &traffic);
772         }
773
774         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
775         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
776 }
777
778 static inline void
779 drain_buffers(struct lcore_conf *qconf)
780 {
781         struct buffer *buf;
782         uint32_t portid;
783
784         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
785                 buf = &qconf->tx_mbufs[portid];
786                 if (buf->len == 0)
787                         continue;
788                 send_burst(qconf, buf->len, portid);
789                 buf->len = 0;
790         }
791 }
792
793 /* main processing loop */
794 static int32_t
795 main_loop(__attribute__((unused)) void *dummy)
796 {
797         struct rte_mbuf *pkts[MAX_PKT_BURST];
798         uint32_t lcore_id;
799         uint64_t prev_tsc, diff_tsc, cur_tsc;
800         int32_t i, nb_rx;
801         uint16_t portid;
802         uint8_t queueid;
803         struct lcore_conf *qconf;
804         int32_t socket_id;
805         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
806                         / US_PER_S * BURST_TX_DRAIN_US;
807         struct lcore_rx_queue *rxql;
808
809         prev_tsc = 0;
810         lcore_id = rte_lcore_id();
811         qconf = &lcore_conf[lcore_id];
812         rxql = qconf->rx_queue_list;
813         socket_id = rte_lcore_to_socket_id(lcore_id);
814
815         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
816         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
817         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
818         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
819         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
820         qconf->inbound.cdev_map = cdev_map_in;
821         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
822         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
823         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
824         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
825         qconf->outbound.cdev_map = cdev_map_out;
826         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
827
828         if (qconf->nb_rx_queue == 0) {
829                 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
830                 return 0;
831         }
832
833         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
834
835         for (i = 0; i < qconf->nb_rx_queue; i++) {
836                 portid = rxql[i].port_id;
837                 queueid = rxql[i].queue_id;
838                 RTE_LOG(INFO, IPSEC,
839                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
840                         lcore_id, portid, queueid);
841         }
842
843         while (1) {
844                 cur_tsc = rte_rdtsc();
845
846                 /* TX queue buffer drain */
847                 diff_tsc = cur_tsc - prev_tsc;
848
849                 if (unlikely(diff_tsc > drain_tsc)) {
850                         drain_buffers(qconf);
851                         prev_tsc = cur_tsc;
852                 }
853
854                 /* Read packet from RX queues */
855                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
856                         portid = rxql[i].port_id;
857                         queueid = rxql[i].queue_id;
858                         nb_rx = rte_eth_rx_burst(portid, queueid,
859                                         pkts, MAX_PKT_BURST);
860
861                         if (nb_rx > 0)
862                                 process_pkts(qconf, pkts, nb_rx, portid);
863                 }
864         }
865 }
866
867 static int32_t
868 check_params(void)
869 {
870         uint8_t lcore;
871         uint16_t portid;
872         uint16_t i;
873         int32_t socket_id;
874
875         if (lcore_params == NULL) {
876                 printf("Error: No port/queue/core mappings\n");
877                 return -1;
878         }
879
880         for (i = 0; i < nb_lcore_params; ++i) {
881                 lcore = lcore_params[i].lcore_id;
882                 if (!rte_lcore_is_enabled(lcore)) {
883                         printf("error: lcore %hhu is not enabled in "
884                                 "lcore mask\n", lcore);
885                         return -1;
886                 }
887                 socket_id = rte_lcore_to_socket_id(lcore);
888                 if (socket_id != 0 && numa_on == 0) {
889                         printf("warning: lcore %hhu is on socket %d "
890                                 "with numa off\n",
891                                 lcore, socket_id);
892                 }
893                 portid = lcore_params[i].port_id;
894                 if ((enabled_port_mask & (1 << portid)) == 0) {
895                         printf("port %u is not enabled in port mask\n", portid);
896                         return -1;
897                 }
898                 if (!rte_eth_dev_is_valid_port(portid)) {
899                         printf("port %u is not present on the board\n", portid);
900                         return -1;
901                 }
902         }
903         return 0;
904 }
905
906 static uint8_t
907 get_port_nb_rx_queues(const uint16_t port)
908 {
909         int32_t queue = -1;
910         uint16_t i;
911
912         for (i = 0; i < nb_lcore_params; ++i) {
913                 if (lcore_params[i].port_id == port &&
914                                 lcore_params[i].queue_id > queue)
915                         queue = lcore_params[i].queue_id;
916         }
917         return (uint8_t)(++queue);
918 }
919
920 static int32_t
921 init_lcore_rx_queues(void)
922 {
923         uint16_t i, nb_rx_queue;
924         uint8_t lcore;
925
926         for (i = 0; i < nb_lcore_params; ++i) {
927                 lcore = lcore_params[i].lcore_id;
928                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
929                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
930                         printf("error: too many queues (%u) for lcore: %u\n",
931                                         nb_rx_queue + 1, lcore);
932                         return -1;
933                 }
934                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
935                         lcore_params[i].port_id;
936                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
937                         lcore_params[i].queue_id;
938                 lcore_conf[lcore].nb_rx_queue++;
939         }
940         return 0;
941 }
942
943 /* display usage */
944 static void
945 print_usage(const char *prgname)
946 {
947         fprintf(stderr, "%s [EAL options] --"
948                 " -p PORTMASK"
949                 " [-P]"
950                 " [-u PORTMASK]"
951                 " [-j FRAMESIZE]"
952                 " -f CONFIG_FILE"
953                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
954                 " [--single-sa SAIDX]"
955                 " [--cryptodev_mask MASK]"
956                 "\n\n"
957                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
958                 "  -P : Enable promiscuous mode\n"
959                 "  -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
960                 "  -j FRAMESIZE: Enable jumbo frame with 'FRAMESIZE' as maximum\n"
961                 "                packet size\n"
962                 "  -f CONFIG_FILE: Configuration file\n"
963                 "  --config (port,queue,lcore): Rx queue configuration\n"
964                 "  --single-sa SAIDX: Use single SA index for outbound traffic,\n"
965                 "                     bypassing the SP\n"
966                 "  --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
967                 "                         devices to configure\n"
968                 "\n",
969                 prgname);
970 }
971
972 static int32_t
973 parse_portmask(const char *portmask)
974 {
975         char *end = NULL;
976         unsigned long pm;
977
978         /* parse hexadecimal string */
979         pm = strtoul(portmask, &end, 16);
980         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
981                 return -1;
982
983         if ((pm == 0) && errno)
984                 return -1;
985
986         return pm;
987 }
988
989 static int32_t
990 parse_decimal(const char *str)
991 {
992         char *end = NULL;
993         unsigned long num;
994
995         num = strtoul(str, &end, 10);
996         if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
997                 return -1;
998
999         return num;
1000 }
1001
1002 static int32_t
1003 parse_config(const char *q_arg)
1004 {
1005         char s[256];
1006         const char *p, *p0 = q_arg;
1007         char *end;
1008         enum fieldnames {
1009                 FLD_PORT = 0,
1010                 FLD_QUEUE,
1011                 FLD_LCORE,
1012                 _NUM_FLD
1013         };
1014         unsigned long int_fld[_NUM_FLD];
1015         char *str_fld[_NUM_FLD];
1016         int32_t i;
1017         uint32_t size;
1018
1019         nb_lcore_params = 0;
1020
1021         while ((p = strchr(p0, '(')) != NULL) {
1022                 ++p;
1023                 p0 = strchr(p, ')');
1024                 if (p0 == NULL)
1025                         return -1;
1026
1027                 size = p0 - p;
1028                 if (size >= sizeof(s))
1029                         return -1;
1030
1031                 snprintf(s, sizeof(s), "%.*s", size, p);
1032                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1033                                 _NUM_FLD)
1034                         return -1;
1035                 for (i = 0; i < _NUM_FLD; i++) {
1036                         errno = 0;
1037                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1038                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1039                                 return -1;
1040                 }
1041                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1042                         printf("exceeded max number of lcore params: %hu\n",
1043                                 nb_lcore_params);
1044                         return -1;
1045                 }
1046                 lcore_params_array[nb_lcore_params].port_id =
1047                         (uint8_t)int_fld[FLD_PORT];
1048                 lcore_params_array[nb_lcore_params].queue_id =
1049                         (uint8_t)int_fld[FLD_QUEUE];
1050                 lcore_params_array[nb_lcore_params].lcore_id =
1051                         (uint8_t)int_fld[FLD_LCORE];
1052                 ++nb_lcore_params;
1053         }
1054         lcore_params = lcore_params_array;
1055         return 0;
1056 }
1057
1058 static int32_t
1059 parse_args(int32_t argc, char **argv)
1060 {
1061         int32_t opt, ret;
1062         char **argvopt;
1063         int32_t option_index;
1064         char *prgname = argv[0];
1065         int32_t f_present = 0;
1066
1067         argvopt = argv;
1068
1069         while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:",
1070                                 lgopts, &option_index)) != EOF) {
1071
1072                 switch (opt) {
1073                 case 'p':
1074                         enabled_port_mask = parse_portmask(optarg);
1075                         if (enabled_port_mask == 0) {
1076                                 printf("invalid portmask\n");
1077                                 print_usage(prgname);
1078                                 return -1;
1079                         }
1080                         break;
1081                 case 'P':
1082                         printf("Promiscuous mode selected\n");
1083                         promiscuous_on = 1;
1084                         break;
1085                 case 'u':
1086                         unprotected_port_mask = parse_portmask(optarg);
1087                         if (unprotected_port_mask == 0) {
1088                                 printf("invalid unprotected portmask\n");
1089                                 print_usage(prgname);
1090                                 return -1;
1091                         }
1092                         break;
1093                 case 'f':
1094                         if (f_present == 1) {
1095                                 printf("\"-f\" option present more than "
1096                                         "once!\n");
1097                                 print_usage(prgname);
1098                                 return -1;
1099                         }
1100                         if (parse_cfg_file(optarg) < 0) {
1101                                 printf("parsing file \"%s\" failed\n",
1102                                         optarg);
1103                                 print_usage(prgname);
1104                                 return -1;
1105                         }
1106                         f_present = 1;
1107                         break;
1108                 case 'j':
1109                         {
1110                                 int32_t size = parse_decimal(optarg);
1111                                 if (size <= 1518) {
1112                                         printf("Invalid jumbo frame size\n");
1113                                         if (size < 0) {
1114                                                 print_usage(prgname);
1115                                                 return -1;
1116                                         }
1117                                         printf("Using default value 9000\n");
1118                                         frame_size = 9000;
1119                                 } else {
1120                                         frame_size = size;
1121                                 }
1122                         }
1123                         printf("Enabled jumbo frames size %u\n", frame_size);
1124                         break;
1125                 case CMD_LINE_OPT_CONFIG_NUM:
1126                         ret = parse_config(optarg);
1127                         if (ret) {
1128                                 printf("Invalid config\n");
1129                                 print_usage(prgname);
1130                                 return -1;
1131                         }
1132                         break;
1133                 case CMD_LINE_OPT_SINGLE_SA_NUM:
1134                         ret = parse_decimal(optarg);
1135                         if (ret == -1) {
1136                                 printf("Invalid argument[sa_idx]\n");
1137                                 print_usage(prgname);
1138                                 return -1;
1139                         }
1140
1141                         /* else */
1142                         single_sa = 1;
1143                         single_sa_idx = ret;
1144                         printf("Configured with single SA index %u\n",
1145                                         single_sa_idx);
1146                         break;
1147                 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1148                         ret = parse_portmask(optarg);
1149                         if (ret == -1) {
1150                                 printf("Invalid argument[portmask]\n");
1151                                 print_usage(prgname);
1152                                 return -1;
1153                         }
1154
1155                         /* else */
1156                         enabled_cryptodev_mask = ret;
1157                         break;
1158                 default:
1159                         print_usage(prgname);
1160                         return -1;
1161                 }
1162         }
1163
1164         if (f_present == 0) {
1165                 printf("Mandatory option \"-f\" not present\n");
1166                 return -1;
1167         }
1168
1169         if (optind >= 0)
1170                 argv[optind-1] = prgname;
1171
1172         ret = optind-1;
1173         optind = 1; /* reset getopt lib */
1174         return ret;
1175 }
1176
1177 static void
1178 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1179 {
1180         char buf[ETHER_ADDR_FMT_SIZE];
1181         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1182         printf("%s%s", name, buf);
1183 }
1184
1185 /* Check the link status of all ports in up to 9s, and print them finally */
1186 static void
1187 check_all_ports_link_status(uint32_t port_mask)
1188 {
1189 #define CHECK_INTERVAL 100 /* 100ms */
1190 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1191         uint16_t portid;
1192         uint8_t count, all_ports_up, print_flag = 0;
1193         struct rte_eth_link link;
1194
1195         printf("\nChecking link status");
1196         fflush(stdout);
1197         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1198                 all_ports_up = 1;
1199                 RTE_ETH_FOREACH_DEV(portid) {
1200                         if ((port_mask & (1 << portid)) == 0)
1201                                 continue;
1202                         memset(&link, 0, sizeof(link));
1203                         rte_eth_link_get_nowait(portid, &link);
1204                         /* print link status if flag set */
1205                         if (print_flag == 1) {
1206                                 if (link.link_status)
1207                                         printf(
1208                                         "Port%d Link Up - speed %u Mbps -%s\n",
1209                                                 portid, link.link_speed,
1210                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1211                                         ("full-duplex") : ("half-duplex\n"));
1212                                 else
1213                                         printf("Port %d Link Down\n", portid);
1214                                 continue;
1215                         }
1216                         /* clear all_ports_up flag if any link down */
1217                         if (link.link_status == ETH_LINK_DOWN) {
1218                                 all_ports_up = 0;
1219                                 break;
1220                         }
1221                 }
1222                 /* after finally printing all link status, get out */
1223                 if (print_flag == 1)
1224                         break;
1225
1226                 if (all_ports_up == 0) {
1227                         printf(".");
1228                         fflush(stdout);
1229                         rte_delay_ms(CHECK_INTERVAL);
1230                 }
1231
1232                 /* set the print_flag if all ports up or timeout */
1233                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1234                         print_flag = 1;
1235                         printf("done\n");
1236                 }
1237         }
1238 }
1239
1240 static int32_t
1241 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1242                 uint16_t qp, struct lcore_params *params,
1243                 struct ipsec_ctx *ipsec_ctx,
1244                 const struct rte_cryptodev_capabilities *cipher,
1245                 const struct rte_cryptodev_capabilities *auth,
1246                 const struct rte_cryptodev_capabilities *aead)
1247 {
1248         int32_t ret = 0;
1249         unsigned long i;
1250         struct cdev_key key = { 0 };
1251
1252         key.lcore_id = params->lcore_id;
1253         if (cipher)
1254                 key.cipher_algo = cipher->sym.cipher.algo;
1255         if (auth)
1256                 key.auth_algo = auth->sym.auth.algo;
1257         if (aead)
1258                 key.aead_algo = aead->sym.aead.algo;
1259
1260         ret = rte_hash_lookup(map, &key);
1261         if (ret != -ENOENT)
1262                 return 0;
1263
1264         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1265                 if (ipsec_ctx->tbl[i].id == cdev_id)
1266                         break;
1267
1268         if (i == ipsec_ctx->nb_qps) {
1269                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1270                         printf("Maximum number of crypto devices assigned to "
1271                                 "a core, increase MAX_QP_PER_LCORE value\n");
1272                         return 0;
1273                 }
1274                 ipsec_ctx->tbl[i].id = cdev_id;
1275                 ipsec_ctx->tbl[i].qp = qp;
1276                 ipsec_ctx->nb_qps++;
1277                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1278                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1279                                 cdev_id, qp, i);
1280         }
1281
1282         ret = rte_hash_add_key_data(map, &key, (void *)i);
1283         if (ret < 0) {
1284                 printf("Faled to insert cdev mapping for (lcore %u, "
1285                                 "cdev %u, qp %u), errno %d\n",
1286                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1287                                 ipsec_ctx->tbl[i].qp, ret);
1288                 return 0;
1289         }
1290
1291         return 1;
1292 }
1293
1294 static int32_t
1295 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1296                 uint16_t qp, struct lcore_params *params)
1297 {
1298         int32_t ret = 0;
1299         const struct rte_cryptodev_capabilities *i, *j;
1300         struct rte_hash *map;
1301         struct lcore_conf *qconf;
1302         struct ipsec_ctx *ipsec_ctx;
1303         const char *str;
1304
1305         qconf = &lcore_conf[params->lcore_id];
1306
1307         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1308                 map = cdev_map_out;
1309                 ipsec_ctx = &qconf->outbound;
1310                 str = "Outbound";
1311         } else {
1312                 map = cdev_map_in;
1313                 ipsec_ctx = &qconf->inbound;
1314                 str = "Inbound";
1315         }
1316
1317         /* Required cryptodevs with operation chainning */
1318         if (!(dev_info->feature_flags &
1319                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1320                 return ret;
1321
1322         for (i = dev_info->capabilities;
1323                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1324                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1325                         continue;
1326
1327                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1328                         ret |= add_mapping(map, str, cdev_id, qp, params,
1329                                         ipsec_ctx, NULL, NULL, i);
1330                         continue;
1331                 }
1332
1333                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1334                         continue;
1335
1336                 for (j = dev_info->capabilities;
1337                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1338                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1339                                 continue;
1340
1341                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1342                                 continue;
1343
1344                         ret |= add_mapping(map, str, cdev_id, qp, params,
1345                                                 ipsec_ctx, i, j, NULL);
1346                 }
1347         }
1348
1349         return ret;
1350 }
1351
1352 /* Check if the device is enabled by cryptodev_mask */
1353 static int
1354 check_cryptodev_mask(uint8_t cdev_id)
1355 {
1356         if (enabled_cryptodev_mask & (1 << cdev_id))
1357                 return 0;
1358
1359         return -1;
1360 }
1361
1362 static int32_t
1363 cryptodevs_init(void)
1364 {
1365         struct rte_cryptodev_config dev_conf;
1366         struct rte_cryptodev_qp_conf qp_conf;
1367         uint16_t idx, max_nb_qps, qp, i;
1368         int16_t cdev_id, port_id;
1369         struct rte_hash_parameters params = { 0 };
1370
1371         params.entries = CDEV_MAP_ENTRIES;
1372         params.key_len = sizeof(struct cdev_key);
1373         params.hash_func = rte_jhash;
1374         params.hash_func_init_val = 0;
1375         params.socket_id = rte_socket_id();
1376
1377         params.name = "cdev_map_in";
1378         cdev_map_in = rte_hash_create(&params);
1379         if (cdev_map_in == NULL)
1380                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1381                                 rte_errno);
1382
1383         params.name = "cdev_map_out";
1384         cdev_map_out = rte_hash_create(&params);
1385         if (cdev_map_out == NULL)
1386                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1387                                 rte_errno);
1388
1389         printf("lcore/cryptodev/qp mappings:\n");
1390
1391         uint32_t max_sess_sz = 0, sess_sz;
1392         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1393                 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
1394                 if (sess_sz > max_sess_sz)
1395                         max_sess_sz = sess_sz;
1396         }
1397         RTE_ETH_FOREACH_DEV(port_id) {
1398                 void *sec_ctx;
1399
1400                 if ((enabled_port_mask & (1 << port_id)) == 0)
1401                         continue;
1402
1403                 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
1404                 if (sec_ctx == NULL)
1405                         continue;
1406
1407                 sess_sz = rte_security_session_get_size(sec_ctx);
1408                 if (sess_sz > max_sess_sz)
1409                         max_sess_sz = sess_sz;
1410         }
1411
1412         idx = 0;
1413         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1414                 struct rte_cryptodev_info cdev_info;
1415
1416                 if (check_cryptodev_mask((uint8_t)cdev_id))
1417                         continue;
1418
1419                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1420
1421                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1422                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1423                 else
1424                         max_nb_qps = nb_lcore_params;
1425
1426                 qp = 0;
1427                 i = 0;
1428                 while (qp < max_nb_qps && i < nb_lcore_params) {
1429                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1430                                                 &lcore_params[idx]))
1431                                 qp++;
1432                         idx++;
1433                         idx = idx % nb_lcore_params;
1434                         i++;
1435                 }
1436
1437                 if (qp == 0)
1438                         continue;
1439
1440                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1441                 dev_conf.nb_queue_pairs = qp;
1442
1443                 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1444                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1445                         struct rte_mempool *sess_mp;
1446
1447                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1448                                         "sess_mp_%u", dev_conf.socket_id);
1449                         sess_mp = rte_mempool_create(mp_name,
1450                                         CDEV_MP_NB_OBJS,
1451                                         max_sess_sz,
1452                                         CDEV_MP_CACHE_SZ,
1453                                         0, NULL, NULL, NULL,
1454                                         NULL, dev_conf.socket_id,
1455                                         0);
1456                         if (sess_mp == NULL)
1457                                 rte_exit(EXIT_FAILURE,
1458                                         "Cannot create session pool on socket %d\n",
1459                                         dev_conf.socket_id);
1460                         else
1461                                 printf("Allocated session pool on socket %d\n",
1462                                         dev_conf.socket_id);
1463                         socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1464                 }
1465
1466                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1467                         rte_panic("Failed to initialize cryptodev %u\n",
1468                                         cdev_id);
1469
1470                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1471                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1472                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1473                                         &qp_conf, dev_conf.socket_id,
1474                                         socket_ctx[dev_conf.socket_id].session_pool))
1475                                 rte_panic("Failed to setup queue %u for "
1476                                                 "cdev_id %u\n", 0, cdev_id);
1477
1478                 if (rte_cryptodev_start(cdev_id))
1479                         rte_panic("Failed to start cryptodev %u\n",
1480                                         cdev_id);
1481         }
1482
1483         /* create session pools for eth devices that implement security */
1484         RTE_ETH_FOREACH_DEV(port_id) {
1485                 if ((enabled_port_mask & (1 << port_id)) &&
1486                                 rte_eth_dev_get_sec_ctx(port_id)) {
1487                         int socket_id = rte_eth_dev_socket_id(port_id);
1488
1489                         if (!socket_ctx[socket_id].session_pool) {
1490                                 char mp_name[RTE_MEMPOOL_NAMESIZE];
1491                                 struct rte_mempool *sess_mp;
1492
1493                                 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1494                                                 "sess_mp_%u", socket_id);
1495                                 sess_mp = rte_mempool_create(mp_name,
1496                                                 CDEV_MP_NB_OBJS,
1497                                                 max_sess_sz,
1498                                                 CDEV_MP_CACHE_SZ,
1499                                                 0, NULL, NULL, NULL,
1500                                                 NULL, socket_id,
1501                                                 0);
1502                                 if (sess_mp == NULL)
1503                                         rte_exit(EXIT_FAILURE,
1504                                                 "Cannot create session pool "
1505                                                 "on socket %d\n", socket_id);
1506                                 else
1507                                         printf("Allocated session pool "
1508                                                 "on socket %d\n", socket_id);
1509                                 socket_ctx[socket_id].session_pool = sess_mp;
1510                         }
1511                 }
1512         }
1513
1514
1515         printf("\n");
1516
1517         return 0;
1518 }
1519
1520 static void
1521 port_init(uint16_t portid)
1522 {
1523         struct rte_eth_dev_info dev_info;
1524         struct rte_eth_txconf *txconf;
1525         uint16_t nb_tx_queue, nb_rx_queue;
1526         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1527         int32_t ret, socket_id;
1528         struct lcore_conf *qconf;
1529         struct ether_addr ethaddr;
1530         struct rte_eth_conf local_port_conf = port_conf;
1531
1532         rte_eth_dev_info_get(portid, &dev_info);
1533
1534         printf("Configuring device port %u:\n", portid);
1535
1536         rte_eth_macaddr_get(portid, &ethaddr);
1537         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1538         print_ethaddr("Address: ", &ethaddr);
1539         printf("\n");
1540
1541         nb_rx_queue = get_port_nb_rx_queues(portid);
1542         nb_tx_queue = nb_lcores;
1543
1544         if (nb_rx_queue > dev_info.max_rx_queues)
1545                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1546                                 "(max rx queue is %u)\n",
1547                                 nb_rx_queue, dev_info.max_rx_queues);
1548
1549         if (nb_tx_queue > dev_info.max_tx_queues)
1550                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1551                                 "(max tx queue is %u)\n",
1552                                 nb_tx_queue, dev_info.max_tx_queues);
1553
1554         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1555                         nb_rx_queue, nb_tx_queue);
1556
1557         if (frame_size) {
1558                 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
1559                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1560         }
1561
1562         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY)
1563                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SECURITY;
1564         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SECURITY)
1565                 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_SECURITY;
1566         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1567                 local_port_conf.txmode.offloads |=
1568                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1569         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1570                         &local_port_conf);
1571         if (ret < 0)
1572                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1573                                 "err=%d, port=%d\n", ret, portid);
1574
1575         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1576         if (ret < 0)
1577                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1578                                 "err=%d, port=%d\n", ret, portid);
1579
1580         /* init one TX queue per lcore */
1581         tx_queueid = 0;
1582         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1583                 if (rte_lcore_is_enabled(lcore_id) == 0)
1584                         continue;
1585
1586                 if (numa_on)
1587                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1588                 else
1589                         socket_id = 0;
1590
1591                 /* init TX queue */
1592                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1593
1594                 txconf = &dev_info.default_txconf;
1595                 txconf->txq_flags = ETH_TXQ_FLAGS_IGNORE;
1596                 txconf->offloads = local_port_conf.txmode.offloads;
1597
1598                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1599                                 socket_id, txconf);
1600                 if (ret < 0)
1601                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1602                                         "err=%d, port=%d\n", ret, portid);
1603
1604                 qconf = &lcore_conf[lcore_id];
1605                 qconf->tx_queue_id[portid] = tx_queueid;
1606                 tx_queueid++;
1607
1608                 /* init RX queues */
1609                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1610                         struct rte_eth_rxconf rxq_conf;
1611
1612                         if (portid != qconf->rx_queue_list[queue].port_id)
1613                                 continue;
1614
1615                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
1616
1617                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1618                                         socket_id);
1619
1620                         rxq_conf = dev_info.default_rxconf;
1621                         rxq_conf.offloads = local_port_conf.rxmode.offloads;
1622                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1623                                         nb_rxd, socket_id, &rxq_conf,
1624                                         socket_ctx[socket_id].mbuf_pool);
1625                         if (ret < 0)
1626                                 rte_exit(EXIT_FAILURE,
1627                                         "rte_eth_rx_queue_setup: err=%d, "
1628                                         "port=%d\n", ret, portid);
1629                 }
1630         }
1631         printf("\n");
1632 }
1633
1634 static void
1635 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1636 {
1637         char s[64];
1638         uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) :
1639                         RTE_MBUF_DEFAULT_BUF_SIZE;
1640
1641
1642         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1643         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1644                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1645                         buff_size,
1646                         socket_id);
1647         if (ctx->mbuf_pool == NULL)
1648                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1649                                 socket_id);
1650         else
1651                 printf("Allocated mbuf pool on socket %d\n", socket_id);
1652 }
1653
1654 static inline int
1655 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
1656 {
1657         struct ipsec_sa *sa;
1658
1659         /* For inline protocol processing, the metadata in the event will
1660          * uniquely identify the security session which raised the event.
1661          * Application would then need the userdata it had registered with the
1662          * security session to process the event.
1663          */
1664
1665         sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
1666
1667         if (sa == NULL) {
1668                 /* userdata could not be retrieved */
1669                 return -1;
1670         }
1671
1672         /* Sequence number over flow. SA need to be re-established */
1673         RTE_SET_USED(sa);
1674         return 0;
1675 }
1676
1677 static int
1678 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
1679                  void *param, void *ret_param)
1680 {
1681         uint64_t md;
1682         struct rte_eth_event_ipsec_desc *event_desc = NULL;
1683         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
1684                                         rte_eth_dev_get_sec_ctx(port_id);
1685
1686         RTE_SET_USED(param);
1687
1688         if (type != RTE_ETH_EVENT_IPSEC)
1689                 return -1;
1690
1691         event_desc = ret_param;
1692         if (event_desc == NULL) {
1693                 printf("Event descriptor not set\n");
1694                 return -1;
1695         }
1696
1697         md = event_desc->metadata;
1698
1699         if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
1700                 return inline_ipsec_event_esn_overflow(ctx, md);
1701         else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
1702                 printf("Invalid IPsec event reported\n");
1703                 return -1;
1704         }
1705
1706         return -1;
1707 }
1708
1709 int32_t
1710 main(int32_t argc, char **argv)
1711 {
1712         int32_t ret;
1713         uint32_t lcore_id;
1714         uint8_t socket_id;
1715         uint16_t portid;
1716
1717         /* init EAL */
1718         ret = rte_eal_init(argc, argv);
1719         if (ret < 0)
1720                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1721         argc -= ret;
1722         argv += ret;
1723
1724         /* parse application arguments (after the EAL ones) */
1725         ret = parse_args(argc, argv);
1726         if (ret < 0)
1727                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1728
1729         if ((unprotected_port_mask & enabled_port_mask) !=
1730                         unprotected_port_mask)
1731                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1732                                 unprotected_port_mask);
1733
1734         if (check_params() < 0)
1735                 rte_exit(EXIT_FAILURE, "check_params failed\n");
1736
1737         ret = init_lcore_rx_queues();
1738         if (ret < 0)
1739                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1740
1741         nb_lcores = rte_lcore_count();
1742
1743         /* Replicate each context per socket */
1744         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1745                 if (rte_lcore_is_enabled(lcore_id) == 0)
1746                         continue;
1747
1748                 if (numa_on)
1749                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1750                 else
1751                         socket_id = 0;
1752
1753                 if (socket_ctx[socket_id].mbuf_pool)
1754                         continue;
1755
1756                 sa_init(&socket_ctx[socket_id], socket_id);
1757
1758                 sp4_init(&socket_ctx[socket_id], socket_id);
1759
1760                 sp6_init(&socket_ctx[socket_id], socket_id);
1761
1762                 rt_init(&socket_ctx[socket_id], socket_id);
1763
1764                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1765         }
1766
1767         RTE_ETH_FOREACH_DEV(portid) {
1768                 if ((enabled_port_mask & (1 << portid)) == 0)
1769                         continue;
1770
1771                 port_init(portid);
1772         }
1773
1774         cryptodevs_init();
1775
1776         /* start ports */
1777         RTE_ETH_FOREACH_DEV(portid) {
1778                 if ((enabled_port_mask & (1 << portid)) == 0)
1779                         continue;
1780
1781                 /* Start device */
1782                 ret = rte_eth_dev_start(portid);
1783                 if (ret < 0)
1784                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1785                                         "err=%d, port=%d\n", ret, portid);
1786                 /*
1787                  * If enabled, put device in promiscuous mode.
1788                  * This allows IO forwarding mode to forward packets
1789                  * to itself through 2 cross-connected  ports of the
1790                  * target machine.
1791                  */
1792                 if (promiscuous_on)
1793                         rte_eth_promiscuous_enable(portid);
1794
1795                 rte_eth_dev_callback_register(portid,
1796                         RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
1797         }
1798
1799         check_all_ports_link_status(enabled_port_mask);
1800
1801         /* launch per-lcore init on every lcore */
1802         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1803         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1804                 if (rte_eal_wait_lcore(lcore_id) < 0)
1805                         return -1;
1806         }
1807
1808         return 0;
1809 }