New upstream version 18.08
[deb_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         },
203         .rx_adv_conf = {
204                 .rss_conf = {
205                         .rss_key = NULL,
206                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
207                                 ETH_RSS_TCP | ETH_RSS_SCTP,
208                 },
209         },
210         .txmode = {
211                 .mq_mode = ETH_MQ_TX_NONE,
212                 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
213                              DEV_TX_OFFLOAD_MULTI_SEGS),
214         },
215 };
216
217 static struct socket_ctx socket_ctx[NB_SOCKETS];
218
219 struct traffic_type {
220         const uint8_t *data[MAX_PKT_BURST * 2];
221         struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
222         uint32_t res[MAX_PKT_BURST * 2];
223         uint32_t num;
224 };
225
226 struct ipsec_traffic {
227         struct traffic_type ipsec;
228         struct traffic_type ip4;
229         struct traffic_type ip6;
230 };
231
232 static inline void
233 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
234 {
235         uint8_t *nlp;
236         struct ether_hdr *eth;
237
238         eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
239         if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
240                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
241                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p));
242                 if (*nlp == IPPROTO_ESP)
243                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
244                 else {
245                         t->ip4.data[t->ip4.num] = nlp;
246                         t->ip4.pkts[(t->ip4.num)++] = pkt;
247                 }
248         } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
249                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
250                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt));
251                 if (*nlp == IPPROTO_ESP)
252                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
253                 else {
254                         t->ip6.data[t->ip6.num] = nlp;
255                         t->ip6.pkts[(t->ip6.num)++] = pkt;
256                 }
257         } else {
258                 /* Unknown/Unsupported type, drop the packet */
259                 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n");
260                 rte_pktmbuf_free(pkt);
261         }
262
263         /* Check if the packet has been processed inline. For inline protocol
264          * processed packets, the metadata in the mbuf can be used to identify
265          * the security processing done on the packet. The metadata will be
266          * used to retrieve the application registered userdata associated
267          * with the security session.
268          */
269
270         if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
271                 struct ipsec_sa *sa;
272                 struct ipsec_mbuf_metadata *priv;
273                 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
274                                                 rte_eth_dev_get_sec_ctx(
275                                                 pkt->port);
276
277                 /* Retrieve the userdata registered. Here, the userdata
278                  * registered is the SA pointer.
279                  */
280
281                 sa = (struct ipsec_sa *)
282                                 rte_security_get_userdata(ctx, pkt->udata64);
283
284                 if (sa == NULL) {
285                         /* userdata could not be retrieved */
286                         return;
287                 }
288
289                 /* Save SA as priv member in mbuf. This will be used in the
290                  * IPsec selector(SP-SA) check.
291                  */
292
293                 priv = get_priv(pkt);
294                 priv->sa = sa;
295         }
296 }
297
298 static inline void
299 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
300                 uint16_t nb_pkts)
301 {
302         int32_t i;
303
304         t->ipsec.num = 0;
305         t->ip4.num = 0;
306         t->ip6.num = 0;
307
308         for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
309                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
310                                         void *));
311                 prepare_one_packet(pkts[i], t);
312         }
313         /* Process left packets */
314         for (; i < nb_pkts; i++)
315                 prepare_one_packet(pkts[i], t);
316 }
317
318 static inline void
319 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port)
320 {
321         struct ip *ip;
322         struct ether_hdr *ethhdr;
323
324         ip = rte_pktmbuf_mtod(pkt, struct ip *);
325
326         ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN);
327
328         if (ip->ip_v == IPVERSION) {
329                 pkt->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
330                 pkt->l3_len = sizeof(struct ip);
331                 pkt->l2_len = ETHER_HDR_LEN;
332
333                 ip->ip_sum = 0;
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 (ip->res[i] & BYPASS)
514                         ip->pkts[j++] = m;
515                 else if (sa_idx < IPSEC_SA_MAX_ENTRIES) {
516                         ipsec->res[ipsec->num] = sa_idx;
517                         ipsec->pkts[ipsec->num++] = m;
518                 } else /* invalid SA idx */
519                         rte_pktmbuf_free(m);
520         }
521         ip->num = j;
522 }
523
524 static inline void
525 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
526                 struct ipsec_traffic *traffic)
527 {
528         struct rte_mbuf *m;
529         uint16_t idx, nb_pkts_out, i;
530
531         /* Drop any IPsec traffic from protected ports */
532         for (i = 0; i < traffic->ipsec.num; i++)
533                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
534
535         traffic->ipsec.num = 0;
536
537         outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
538
539         outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
540
541         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
542                         traffic->ipsec.res, traffic->ipsec.num,
543                         MAX_PKT_BURST);
544
545         for (i = 0; i < nb_pkts_out; i++) {
546                 m = traffic->ipsec.pkts[i];
547                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
548                 if (ip->ip_v == IPVERSION) {
549                         idx = traffic->ip4.num++;
550                         traffic->ip4.pkts[idx] = m;
551                 } else {
552                         idx = traffic->ip6.num++;
553                         traffic->ip6.pkts[idx] = m;
554                 }
555         }
556 }
557
558 static inline void
559 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
560                 struct ipsec_traffic *traffic)
561 {
562         struct rte_mbuf *m;
563         uint32_t nb_pkts_in, i, idx;
564
565         /* Drop any IPv4 traffic from unprotected ports */
566         for (i = 0; i < traffic->ip4.num; i++)
567                 rte_pktmbuf_free(traffic->ip4.pkts[i]);
568
569         traffic->ip4.num = 0;
570
571         /* Drop any IPv6 traffic from unprotected ports */
572         for (i = 0; i < traffic->ip6.num; i++)
573                 rte_pktmbuf_free(traffic->ip6.pkts[i]);
574
575         traffic->ip6.num = 0;
576
577         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
578                         traffic->ipsec.num, MAX_PKT_BURST);
579
580         for (i = 0; i < nb_pkts_in; i++) {
581                 m = traffic->ipsec.pkts[i];
582                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
583                 if (ip->ip_v == IPVERSION) {
584                         idx = traffic->ip4.num++;
585                         traffic->ip4.pkts[idx] = m;
586                 } else {
587                         idx = traffic->ip6.num++;
588                         traffic->ip6.pkts[idx] = m;
589                 }
590         }
591 }
592
593 static inline void
594 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
595                 struct ipsec_traffic *traffic)
596 {
597         struct rte_mbuf *m;
598         uint32_t nb_pkts_out, i;
599         struct ip *ip;
600
601         /* Drop any IPsec traffic from protected ports */
602         for (i = 0; i < traffic->ipsec.num; i++)
603                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
604
605         traffic->ipsec.num = 0;
606
607         for (i = 0; i < traffic->ip4.num; i++)
608                 traffic->ip4.res[i] = single_sa_idx;
609
610         for (i = 0; i < traffic->ip6.num; i++)
611                 traffic->ip6.res[i] = single_sa_idx;
612
613         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
614                         traffic->ip4.res, traffic->ip4.num,
615                         MAX_PKT_BURST);
616
617         /* They all sue the same SA (ip4 or ip6 tunnel) */
618         m = traffic->ipsec.pkts[i];
619         ip = rte_pktmbuf_mtod(m, struct ip *);
620         if (ip->ip_v == IPVERSION)
621                 traffic->ip4.num = nb_pkts_out;
622         else
623                 traffic->ip6.num = nb_pkts_out;
624 }
625
626 static inline int32_t
627 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
628 {
629         struct ipsec_mbuf_metadata *priv;
630         struct ipsec_sa *sa;
631
632         priv = get_priv(pkt);
633
634         sa = priv->sa;
635         if (unlikely(sa == NULL)) {
636                 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
637                 goto fail;
638         }
639
640         if (is_ipv6)
641                 return sa->portid;
642
643         /* else */
644         return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
645
646 fail:
647         if (is_ipv6)
648                 return -1;
649
650         /* else */
651         return 0;
652 }
653
654 static inline void
655 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
656 {
657         uint32_t hop[MAX_PKT_BURST * 2];
658         uint32_t dst_ip[MAX_PKT_BURST * 2];
659         int32_t pkt_hop = 0;
660         uint16_t i, offset;
661         uint16_t lpm_pkts = 0;
662
663         if (nb_pkts == 0)
664                 return;
665
666         /* Need to do an LPM lookup for non-inline packets. Inline packets will
667          * have port ID in the SA
668          */
669
670         for (i = 0; i < nb_pkts; i++) {
671                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
672                         /* Security offload not enabled. So an LPM lookup is
673                          * required to get the hop
674                          */
675                         offset = offsetof(struct ip, ip_dst);
676                         dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
677                                         uint32_t *, offset);
678                         dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
679                         lpm_pkts++;
680                 }
681         }
682
683         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
684
685         lpm_pkts = 0;
686
687         for (i = 0; i < nb_pkts; i++) {
688                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
689                         /* Read hop from the SA */
690                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
691                 } else {
692                         /* Need to use hop returned by lookup */
693                         pkt_hop = hop[lpm_pkts++];
694                 }
695
696                 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
697                         rte_pktmbuf_free(pkts[i]);
698                         continue;
699                 }
700                 send_single_packet(pkts[i], pkt_hop & 0xff);
701         }
702 }
703
704 static inline void
705 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
706 {
707         int32_t hop[MAX_PKT_BURST * 2];
708         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
709         uint8_t *ip6_dst;
710         int32_t pkt_hop = 0;
711         uint16_t i, offset;
712         uint16_t lpm_pkts = 0;
713
714         if (nb_pkts == 0)
715                 return;
716
717         /* Need to do an LPM lookup for non-inline packets. Inline packets will
718          * have port ID in the SA
719          */
720
721         for (i = 0; i < nb_pkts; i++) {
722                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
723                         /* Security offload not enabled. So an LPM lookup is
724                          * required to get the hop
725                          */
726                         offset = offsetof(struct ip6_hdr, ip6_dst);
727                         ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
728                                         offset);
729                         memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
730                         lpm_pkts++;
731                 }
732         }
733
734         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
735                         lpm_pkts);
736
737         lpm_pkts = 0;
738
739         for (i = 0; i < nb_pkts; i++) {
740                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
741                         /* Read hop from the SA */
742                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
743                 } else {
744                         /* Need to use hop returned by lookup */
745                         pkt_hop = hop[lpm_pkts++];
746                 }
747
748                 if (pkt_hop == -1) {
749                         rte_pktmbuf_free(pkts[i]);
750                         continue;
751                 }
752                 send_single_packet(pkts[i], pkt_hop & 0xff);
753         }
754 }
755
756 static inline void
757 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
758                 uint8_t nb_pkts, uint16_t portid)
759 {
760         struct ipsec_traffic traffic;
761
762         prepare_traffic(pkts, &traffic, nb_pkts);
763
764         if (unlikely(single_sa)) {
765                 if (UNPROTECTED_PORT(portid))
766                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
767                 else
768                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
769         } else {
770                 if (UNPROTECTED_PORT(portid))
771                         process_pkts_inbound(&qconf->inbound, &traffic);
772                 else
773                         process_pkts_outbound(&qconf->outbound, &traffic);
774         }
775
776         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
777         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
778 }
779
780 static inline void
781 drain_buffers(struct lcore_conf *qconf)
782 {
783         struct buffer *buf;
784         uint32_t portid;
785
786         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
787                 buf = &qconf->tx_mbufs[portid];
788                 if (buf->len == 0)
789                         continue;
790                 send_burst(qconf, buf->len, portid);
791                 buf->len = 0;
792         }
793 }
794
795 /* main processing loop */
796 static int32_t
797 main_loop(__attribute__((unused)) void *dummy)
798 {
799         struct rte_mbuf *pkts[MAX_PKT_BURST];
800         uint32_t lcore_id;
801         uint64_t prev_tsc, diff_tsc, cur_tsc;
802         int32_t i, nb_rx;
803         uint16_t portid;
804         uint8_t queueid;
805         struct lcore_conf *qconf;
806         int32_t socket_id;
807         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
808                         / US_PER_S * BURST_TX_DRAIN_US;
809         struct lcore_rx_queue *rxql;
810
811         prev_tsc = 0;
812         lcore_id = rte_lcore_id();
813         qconf = &lcore_conf[lcore_id];
814         rxql = qconf->rx_queue_list;
815         socket_id = rte_lcore_to_socket_id(lcore_id);
816
817         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
818         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
819         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
820         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
821         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
822         qconf->inbound.cdev_map = cdev_map_in;
823         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
824         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
825         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
826         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
827         qconf->outbound.cdev_map = cdev_map_out;
828         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
829
830         if (qconf->nb_rx_queue == 0) {
831                 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
832                 return 0;
833         }
834
835         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
836
837         for (i = 0; i < qconf->nb_rx_queue; i++) {
838                 portid = rxql[i].port_id;
839                 queueid = rxql[i].queue_id;
840                 RTE_LOG(INFO, IPSEC,
841                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
842                         lcore_id, portid, queueid);
843         }
844
845         while (1) {
846                 cur_tsc = rte_rdtsc();
847
848                 /* TX queue buffer drain */
849                 diff_tsc = cur_tsc - prev_tsc;
850
851                 if (unlikely(diff_tsc > drain_tsc)) {
852                         drain_buffers(qconf);
853                         prev_tsc = cur_tsc;
854                 }
855
856                 /* Read packet from RX queues */
857                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
858                         portid = rxql[i].port_id;
859                         queueid = rxql[i].queue_id;
860                         nb_rx = rte_eth_rx_burst(portid, queueid,
861                                         pkts, MAX_PKT_BURST);
862
863                         if (nb_rx > 0)
864                                 process_pkts(qconf, pkts, nb_rx, portid);
865                 }
866         }
867 }
868
869 static int32_t
870 check_params(void)
871 {
872         uint8_t lcore;
873         uint16_t portid;
874         uint16_t i;
875         int32_t socket_id;
876
877         if (lcore_params == NULL) {
878                 printf("Error: No port/queue/core mappings\n");
879                 return -1;
880         }
881
882         for (i = 0; i < nb_lcore_params; ++i) {
883                 lcore = lcore_params[i].lcore_id;
884                 if (!rte_lcore_is_enabled(lcore)) {
885                         printf("error: lcore %hhu is not enabled in "
886                                 "lcore mask\n", lcore);
887                         return -1;
888                 }
889                 socket_id = rte_lcore_to_socket_id(lcore);
890                 if (socket_id != 0 && numa_on == 0) {
891                         printf("warning: lcore %hhu is on socket %d "
892                                 "with numa off\n",
893                                 lcore, socket_id);
894                 }
895                 portid = lcore_params[i].port_id;
896                 if ((enabled_port_mask & (1 << portid)) == 0) {
897                         printf("port %u is not enabled in port mask\n", portid);
898                         return -1;
899                 }
900                 if (!rte_eth_dev_is_valid_port(portid)) {
901                         printf("port %u is not present on the board\n", portid);
902                         return -1;
903                 }
904         }
905         return 0;
906 }
907
908 static uint8_t
909 get_port_nb_rx_queues(const uint16_t port)
910 {
911         int32_t queue = -1;
912         uint16_t i;
913
914         for (i = 0; i < nb_lcore_params; ++i) {
915                 if (lcore_params[i].port_id == port &&
916                                 lcore_params[i].queue_id > queue)
917                         queue = lcore_params[i].queue_id;
918         }
919         return (uint8_t)(++queue);
920 }
921
922 static int32_t
923 init_lcore_rx_queues(void)
924 {
925         uint16_t i, nb_rx_queue;
926         uint8_t lcore;
927
928         for (i = 0; i < nb_lcore_params; ++i) {
929                 lcore = lcore_params[i].lcore_id;
930                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
931                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
932                         printf("error: too many queues (%u) for lcore: %u\n",
933                                         nb_rx_queue + 1, lcore);
934                         return -1;
935                 }
936                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
937                         lcore_params[i].port_id;
938                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
939                         lcore_params[i].queue_id;
940                 lcore_conf[lcore].nb_rx_queue++;
941         }
942         return 0;
943 }
944
945 /* display usage */
946 static void
947 print_usage(const char *prgname)
948 {
949         fprintf(stderr, "%s [EAL options] --"
950                 " -p PORTMASK"
951                 " [-P]"
952                 " [-u PORTMASK]"
953                 " [-j FRAMESIZE]"
954                 " -f CONFIG_FILE"
955                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
956                 " [--single-sa SAIDX]"
957                 " [--cryptodev_mask MASK]"
958                 "\n\n"
959                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
960                 "  -P : Enable promiscuous mode\n"
961                 "  -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
962                 "  -j FRAMESIZE: Enable jumbo frame with 'FRAMESIZE' as maximum\n"
963                 "                packet size\n"
964                 "  -f CONFIG_FILE: Configuration file\n"
965                 "  --config (port,queue,lcore): Rx queue configuration\n"
966                 "  --single-sa SAIDX: Use single SA index for outbound traffic,\n"
967                 "                     bypassing the SP\n"
968                 "  --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
969                 "                         devices to configure\n"
970                 "\n",
971                 prgname);
972 }
973
974 static int32_t
975 parse_portmask(const char *portmask)
976 {
977         char *end = NULL;
978         unsigned long pm;
979
980         /* parse hexadecimal string */
981         pm = strtoul(portmask, &end, 16);
982         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
983                 return -1;
984
985         if ((pm == 0) && errno)
986                 return -1;
987
988         return pm;
989 }
990
991 static int32_t
992 parse_decimal(const char *str)
993 {
994         char *end = NULL;
995         unsigned long num;
996
997         num = strtoul(str, &end, 10);
998         if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
999                 return -1;
1000
1001         return num;
1002 }
1003
1004 static int32_t
1005 parse_config(const char *q_arg)
1006 {
1007         char s[256];
1008         const char *p, *p0 = q_arg;
1009         char *end;
1010         enum fieldnames {
1011                 FLD_PORT = 0,
1012                 FLD_QUEUE,
1013                 FLD_LCORE,
1014                 _NUM_FLD
1015         };
1016         unsigned long int_fld[_NUM_FLD];
1017         char *str_fld[_NUM_FLD];
1018         int32_t i;
1019         uint32_t size;
1020
1021         nb_lcore_params = 0;
1022
1023         while ((p = strchr(p0, '(')) != NULL) {
1024                 ++p;
1025                 p0 = strchr(p, ')');
1026                 if (p0 == NULL)
1027                         return -1;
1028
1029                 size = p0 - p;
1030                 if (size >= sizeof(s))
1031                         return -1;
1032
1033                 snprintf(s, sizeof(s), "%.*s", size, p);
1034                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1035                                 _NUM_FLD)
1036                         return -1;
1037                 for (i = 0; i < _NUM_FLD; i++) {
1038                         errno = 0;
1039                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1040                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1041                                 return -1;
1042                 }
1043                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1044                         printf("exceeded max number of lcore params: %hu\n",
1045                                 nb_lcore_params);
1046                         return -1;
1047                 }
1048                 lcore_params_array[nb_lcore_params].port_id =
1049                         (uint8_t)int_fld[FLD_PORT];
1050                 lcore_params_array[nb_lcore_params].queue_id =
1051                         (uint8_t)int_fld[FLD_QUEUE];
1052                 lcore_params_array[nb_lcore_params].lcore_id =
1053                         (uint8_t)int_fld[FLD_LCORE];
1054                 ++nb_lcore_params;
1055         }
1056         lcore_params = lcore_params_array;
1057         return 0;
1058 }
1059
1060 static int32_t
1061 parse_args(int32_t argc, char **argv)
1062 {
1063         int32_t opt, ret;
1064         char **argvopt;
1065         int32_t option_index;
1066         char *prgname = argv[0];
1067         int32_t f_present = 0;
1068
1069         argvopt = argv;
1070
1071         while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:",
1072                                 lgopts, &option_index)) != EOF) {
1073
1074                 switch (opt) {
1075                 case 'p':
1076                         enabled_port_mask = parse_portmask(optarg);
1077                         if (enabled_port_mask == 0) {
1078                                 printf("invalid portmask\n");
1079                                 print_usage(prgname);
1080                                 return -1;
1081                         }
1082                         break;
1083                 case 'P':
1084                         printf("Promiscuous mode selected\n");
1085                         promiscuous_on = 1;
1086                         break;
1087                 case 'u':
1088                         unprotected_port_mask = parse_portmask(optarg);
1089                         if (unprotected_port_mask == 0) {
1090                                 printf("invalid unprotected portmask\n");
1091                                 print_usage(prgname);
1092                                 return -1;
1093                         }
1094                         break;
1095                 case 'f':
1096                         if (f_present == 1) {
1097                                 printf("\"-f\" option present more than "
1098                                         "once!\n");
1099                                 print_usage(prgname);
1100                                 return -1;
1101                         }
1102                         if (parse_cfg_file(optarg) < 0) {
1103                                 printf("parsing file \"%s\" failed\n",
1104                                         optarg);
1105                                 print_usage(prgname);
1106                                 return -1;
1107                         }
1108                         f_present = 1;
1109                         break;
1110                 case 'j':
1111                         {
1112                                 int32_t size = parse_decimal(optarg);
1113                                 if (size <= 1518) {
1114                                         printf("Invalid jumbo frame size\n");
1115                                         if (size < 0) {
1116                                                 print_usage(prgname);
1117                                                 return -1;
1118                                         }
1119                                         printf("Using default value 9000\n");
1120                                         frame_size = 9000;
1121                                 } else {
1122                                         frame_size = size;
1123                                 }
1124                         }
1125                         printf("Enabled jumbo frames size %u\n", frame_size);
1126                         break;
1127                 case CMD_LINE_OPT_CONFIG_NUM:
1128                         ret = parse_config(optarg);
1129                         if (ret) {
1130                                 printf("Invalid config\n");
1131                                 print_usage(prgname);
1132                                 return -1;
1133                         }
1134                         break;
1135                 case CMD_LINE_OPT_SINGLE_SA_NUM:
1136                         ret = parse_decimal(optarg);
1137                         if (ret == -1) {
1138                                 printf("Invalid argument[sa_idx]\n");
1139                                 print_usage(prgname);
1140                                 return -1;
1141                         }
1142
1143                         /* else */
1144                         single_sa = 1;
1145                         single_sa_idx = ret;
1146                         printf("Configured with single SA index %u\n",
1147                                         single_sa_idx);
1148                         break;
1149                 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1150                         ret = parse_portmask(optarg);
1151                         if (ret == -1) {
1152                                 printf("Invalid argument[portmask]\n");
1153                                 print_usage(prgname);
1154                                 return -1;
1155                         }
1156
1157                         /* else */
1158                         enabled_cryptodev_mask = ret;
1159                         break;
1160                 default:
1161                         print_usage(prgname);
1162                         return -1;
1163                 }
1164         }
1165
1166         if (f_present == 0) {
1167                 printf("Mandatory option \"-f\" not present\n");
1168                 return -1;
1169         }
1170
1171         if (optind >= 0)
1172                 argv[optind-1] = prgname;
1173
1174         ret = optind-1;
1175         optind = 1; /* reset getopt lib */
1176         return ret;
1177 }
1178
1179 static void
1180 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1181 {
1182         char buf[ETHER_ADDR_FMT_SIZE];
1183         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1184         printf("%s%s", name, buf);
1185 }
1186
1187 /* Check the link status of all ports in up to 9s, and print them finally */
1188 static void
1189 check_all_ports_link_status(uint32_t port_mask)
1190 {
1191 #define CHECK_INTERVAL 100 /* 100ms */
1192 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1193         uint16_t portid;
1194         uint8_t count, all_ports_up, print_flag = 0;
1195         struct rte_eth_link link;
1196
1197         printf("\nChecking link status");
1198         fflush(stdout);
1199         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1200                 all_ports_up = 1;
1201                 RTE_ETH_FOREACH_DEV(portid) {
1202                         if ((port_mask & (1 << portid)) == 0)
1203                                 continue;
1204                         memset(&link, 0, sizeof(link));
1205                         rte_eth_link_get_nowait(portid, &link);
1206                         /* print link status if flag set */
1207                         if (print_flag == 1) {
1208                                 if (link.link_status)
1209                                         printf(
1210                                         "Port%d Link Up - speed %u Mbps -%s\n",
1211                                                 portid, link.link_speed,
1212                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1213                                         ("full-duplex") : ("half-duplex\n"));
1214                                 else
1215                                         printf("Port %d Link Down\n", portid);
1216                                 continue;
1217                         }
1218                         /* clear all_ports_up flag if any link down */
1219                         if (link.link_status == ETH_LINK_DOWN) {
1220                                 all_ports_up = 0;
1221                                 break;
1222                         }
1223                 }
1224                 /* after finally printing all link status, get out */
1225                 if (print_flag == 1)
1226                         break;
1227
1228                 if (all_ports_up == 0) {
1229                         printf(".");
1230                         fflush(stdout);
1231                         rte_delay_ms(CHECK_INTERVAL);
1232                 }
1233
1234                 /* set the print_flag if all ports up or timeout */
1235                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1236                         print_flag = 1;
1237                         printf("done\n");
1238                 }
1239         }
1240 }
1241
1242 static int32_t
1243 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1244                 uint16_t qp, struct lcore_params *params,
1245                 struct ipsec_ctx *ipsec_ctx,
1246                 const struct rte_cryptodev_capabilities *cipher,
1247                 const struct rte_cryptodev_capabilities *auth,
1248                 const struct rte_cryptodev_capabilities *aead)
1249 {
1250         int32_t ret = 0;
1251         unsigned long i;
1252         struct cdev_key key = { 0 };
1253
1254         key.lcore_id = params->lcore_id;
1255         if (cipher)
1256                 key.cipher_algo = cipher->sym.cipher.algo;
1257         if (auth)
1258                 key.auth_algo = auth->sym.auth.algo;
1259         if (aead)
1260                 key.aead_algo = aead->sym.aead.algo;
1261
1262         ret = rte_hash_lookup(map, &key);
1263         if (ret != -ENOENT)
1264                 return 0;
1265
1266         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1267                 if (ipsec_ctx->tbl[i].id == cdev_id)
1268                         break;
1269
1270         if (i == ipsec_ctx->nb_qps) {
1271                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1272                         printf("Maximum number of crypto devices assigned to "
1273                                 "a core, increase MAX_QP_PER_LCORE value\n");
1274                         return 0;
1275                 }
1276                 ipsec_ctx->tbl[i].id = cdev_id;
1277                 ipsec_ctx->tbl[i].qp = qp;
1278                 ipsec_ctx->nb_qps++;
1279                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1280                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1281                                 cdev_id, qp, i);
1282         }
1283
1284         ret = rte_hash_add_key_data(map, &key, (void *)i);
1285         if (ret < 0) {
1286                 printf("Faled to insert cdev mapping for (lcore %u, "
1287                                 "cdev %u, qp %u), errno %d\n",
1288                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1289                                 ipsec_ctx->tbl[i].qp, ret);
1290                 return 0;
1291         }
1292
1293         return 1;
1294 }
1295
1296 static int32_t
1297 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1298                 uint16_t qp, struct lcore_params *params)
1299 {
1300         int32_t ret = 0;
1301         const struct rte_cryptodev_capabilities *i, *j;
1302         struct rte_hash *map;
1303         struct lcore_conf *qconf;
1304         struct ipsec_ctx *ipsec_ctx;
1305         const char *str;
1306
1307         qconf = &lcore_conf[params->lcore_id];
1308
1309         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1310                 map = cdev_map_out;
1311                 ipsec_ctx = &qconf->outbound;
1312                 str = "Outbound";
1313         } else {
1314                 map = cdev_map_in;
1315                 ipsec_ctx = &qconf->inbound;
1316                 str = "Inbound";
1317         }
1318
1319         /* Required cryptodevs with operation chainning */
1320         if (!(dev_info->feature_flags &
1321                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1322                 return ret;
1323
1324         for (i = dev_info->capabilities;
1325                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1326                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1327                         continue;
1328
1329                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1330                         ret |= add_mapping(map, str, cdev_id, qp, params,
1331                                         ipsec_ctx, NULL, NULL, i);
1332                         continue;
1333                 }
1334
1335                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1336                         continue;
1337
1338                 for (j = dev_info->capabilities;
1339                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1340                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1341                                 continue;
1342
1343                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1344                                 continue;
1345
1346                         ret |= add_mapping(map, str, cdev_id, qp, params,
1347                                                 ipsec_ctx, i, j, NULL);
1348                 }
1349         }
1350
1351         return ret;
1352 }
1353
1354 /* Check if the device is enabled by cryptodev_mask */
1355 static int
1356 check_cryptodev_mask(uint8_t cdev_id)
1357 {
1358         if (enabled_cryptodev_mask & (1 << cdev_id))
1359                 return 0;
1360
1361         return -1;
1362 }
1363
1364 static int32_t
1365 cryptodevs_init(void)
1366 {
1367         struct rte_cryptodev_config dev_conf;
1368         struct rte_cryptodev_qp_conf qp_conf;
1369         uint16_t idx, max_nb_qps, qp, i;
1370         int16_t cdev_id, port_id;
1371         struct rte_hash_parameters params = { 0 };
1372
1373         params.entries = CDEV_MAP_ENTRIES;
1374         params.key_len = sizeof(struct cdev_key);
1375         params.hash_func = rte_jhash;
1376         params.hash_func_init_val = 0;
1377         params.socket_id = rte_socket_id();
1378
1379         params.name = "cdev_map_in";
1380         cdev_map_in = rte_hash_create(&params);
1381         if (cdev_map_in == NULL)
1382                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1383                                 rte_errno);
1384
1385         params.name = "cdev_map_out";
1386         cdev_map_out = rte_hash_create(&params);
1387         if (cdev_map_out == NULL)
1388                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1389                                 rte_errno);
1390
1391         printf("lcore/cryptodev/qp mappings:\n");
1392
1393         uint32_t max_sess_sz = 0, sess_sz;
1394         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1395                 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
1396                 if (sess_sz > max_sess_sz)
1397                         max_sess_sz = sess_sz;
1398         }
1399         RTE_ETH_FOREACH_DEV(port_id) {
1400                 void *sec_ctx;
1401
1402                 if ((enabled_port_mask & (1 << port_id)) == 0)
1403                         continue;
1404
1405                 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
1406                 if (sec_ctx == NULL)
1407                         continue;
1408
1409                 sess_sz = rte_security_session_get_size(sec_ctx);
1410                 if (sess_sz > max_sess_sz)
1411                         max_sess_sz = sess_sz;
1412         }
1413
1414         idx = 0;
1415         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1416                 struct rte_cryptodev_info cdev_info;
1417
1418                 if (check_cryptodev_mask((uint8_t)cdev_id))
1419                         continue;
1420
1421                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1422
1423                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1424                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1425                 else
1426                         max_nb_qps = nb_lcore_params;
1427
1428                 qp = 0;
1429                 i = 0;
1430                 while (qp < max_nb_qps && i < nb_lcore_params) {
1431                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1432                                                 &lcore_params[idx]))
1433                                 qp++;
1434                         idx++;
1435                         idx = idx % nb_lcore_params;
1436                         i++;
1437                 }
1438
1439                 if (qp == 0)
1440                         continue;
1441
1442                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1443                 dev_conf.nb_queue_pairs = qp;
1444
1445                 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1446                 if (dev_max_sess != 0 && dev_max_sess < (CDEV_MP_NB_OBJS / 2))
1447                         rte_exit(EXIT_FAILURE,
1448                                 "Device does not support at least %u "
1449                                 "sessions", CDEV_MP_NB_OBJS / 2);
1450
1451                 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1452                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1453                         struct rte_mempool *sess_mp;
1454
1455                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1456                                         "sess_mp_%u", dev_conf.socket_id);
1457                         sess_mp = rte_mempool_create(mp_name,
1458                                         CDEV_MP_NB_OBJS,
1459                                         max_sess_sz,
1460                                         CDEV_MP_CACHE_SZ,
1461                                         0, NULL, NULL, NULL,
1462                                         NULL, dev_conf.socket_id,
1463                                         0);
1464                         if (sess_mp == NULL)
1465                                 rte_exit(EXIT_FAILURE,
1466                                         "Cannot create session pool on socket %d\n",
1467                                         dev_conf.socket_id);
1468                         else
1469                                 printf("Allocated session pool on socket %d\n",
1470                                         dev_conf.socket_id);
1471                         socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1472                 }
1473
1474                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1475                         rte_panic("Failed to initialize cryptodev %u\n",
1476                                         cdev_id);
1477
1478                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1479                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1480                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1481                                         &qp_conf, dev_conf.socket_id,
1482                                         socket_ctx[dev_conf.socket_id].session_pool))
1483                                 rte_panic("Failed to setup queue %u for "
1484                                                 "cdev_id %u\n", 0, cdev_id);
1485
1486                 if (rte_cryptodev_start(cdev_id))
1487                         rte_panic("Failed to start cryptodev %u\n",
1488                                         cdev_id);
1489         }
1490
1491         /* create session pools for eth devices that implement security */
1492         RTE_ETH_FOREACH_DEV(port_id) {
1493                 if ((enabled_port_mask & (1 << port_id)) &&
1494                                 rte_eth_dev_get_sec_ctx(port_id)) {
1495                         int socket_id = rte_eth_dev_socket_id(port_id);
1496
1497                         if (!socket_ctx[socket_id].session_pool) {
1498                                 char mp_name[RTE_MEMPOOL_NAMESIZE];
1499                                 struct rte_mempool *sess_mp;
1500
1501                                 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1502                                                 "sess_mp_%u", socket_id);
1503                                 sess_mp = rte_mempool_create(mp_name,
1504                                                 CDEV_MP_NB_OBJS,
1505                                                 max_sess_sz,
1506                                                 CDEV_MP_CACHE_SZ,
1507                                                 0, NULL, NULL, NULL,
1508                                                 NULL, socket_id,
1509                                                 0);
1510                                 if (sess_mp == NULL)
1511                                         rte_exit(EXIT_FAILURE,
1512                                                 "Cannot create session pool "
1513                                                 "on socket %d\n", socket_id);
1514                                 else
1515                                         printf("Allocated session pool "
1516                                                 "on socket %d\n", socket_id);
1517                                 socket_ctx[socket_id].session_pool = sess_mp;
1518                         }
1519                 }
1520         }
1521
1522
1523         printf("\n");
1524
1525         return 0;
1526 }
1527
1528 static void
1529 port_init(uint16_t portid)
1530 {
1531         struct rte_eth_dev_info dev_info;
1532         struct rte_eth_txconf *txconf;
1533         uint16_t nb_tx_queue, nb_rx_queue;
1534         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1535         int32_t ret, socket_id;
1536         struct lcore_conf *qconf;
1537         struct ether_addr ethaddr;
1538         struct rte_eth_conf local_port_conf = port_conf;
1539
1540         rte_eth_dev_info_get(portid, &dev_info);
1541
1542         printf("Configuring device port %u:\n", portid);
1543
1544         rte_eth_macaddr_get(portid, &ethaddr);
1545         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1546         print_ethaddr("Address: ", &ethaddr);
1547         printf("\n");
1548
1549         nb_rx_queue = get_port_nb_rx_queues(portid);
1550         nb_tx_queue = nb_lcores;
1551
1552         if (nb_rx_queue > dev_info.max_rx_queues)
1553                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1554                                 "(max rx queue is %u)\n",
1555                                 nb_rx_queue, dev_info.max_rx_queues);
1556
1557         if (nb_tx_queue > dev_info.max_tx_queues)
1558                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1559                                 "(max tx queue is %u)\n",
1560                                 nb_tx_queue, dev_info.max_tx_queues);
1561
1562         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1563                         nb_rx_queue, nb_tx_queue);
1564
1565         if (frame_size) {
1566                 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
1567                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1568         }
1569
1570         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY)
1571                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SECURITY;
1572         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SECURITY)
1573                 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_SECURITY;
1574         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1575                 local_port_conf.txmode.offloads |=
1576                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1577
1578         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1579                 dev_info.flow_type_rss_offloads;
1580         if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1581                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
1582                 printf("Port %u modified RSS hash function based on hardware support,"
1583                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1584                         portid,
1585                         port_conf.rx_adv_conf.rss_conf.rss_hf,
1586                         local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1587         }
1588
1589         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1590                         &local_port_conf);
1591         if (ret < 0)
1592                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1593                                 "err=%d, port=%d\n", ret, portid);
1594
1595         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1596         if (ret < 0)
1597                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1598                                 "err=%d, port=%d\n", ret, portid);
1599
1600         /* init one TX queue per lcore */
1601         tx_queueid = 0;
1602         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1603                 if (rte_lcore_is_enabled(lcore_id) == 0)
1604                         continue;
1605
1606                 if (numa_on)
1607                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1608                 else
1609                         socket_id = 0;
1610
1611                 /* init TX queue */
1612                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1613
1614                 txconf = &dev_info.default_txconf;
1615                 txconf->offloads = local_port_conf.txmode.offloads;
1616
1617                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1618                                 socket_id, txconf);
1619                 if (ret < 0)
1620                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1621                                         "err=%d, port=%d\n", ret, portid);
1622
1623                 qconf = &lcore_conf[lcore_id];
1624                 qconf->tx_queue_id[portid] = tx_queueid;
1625                 tx_queueid++;
1626
1627                 /* init RX queues */
1628                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1629                         struct rte_eth_rxconf rxq_conf;
1630
1631                         if (portid != qconf->rx_queue_list[queue].port_id)
1632                                 continue;
1633
1634                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
1635
1636                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1637                                         socket_id);
1638
1639                         rxq_conf = dev_info.default_rxconf;
1640                         rxq_conf.offloads = local_port_conf.rxmode.offloads;
1641                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1642                                         nb_rxd, socket_id, &rxq_conf,
1643                                         socket_ctx[socket_id].mbuf_pool);
1644                         if (ret < 0)
1645                                 rte_exit(EXIT_FAILURE,
1646                                         "rte_eth_rx_queue_setup: err=%d, "
1647                                         "port=%d\n", ret, portid);
1648                 }
1649         }
1650         printf("\n");
1651 }
1652
1653 static void
1654 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1655 {
1656         char s[64];
1657         uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) :
1658                         RTE_MBUF_DEFAULT_BUF_SIZE;
1659
1660
1661         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1662         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1663                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1664                         buff_size,
1665                         socket_id);
1666         if (ctx->mbuf_pool == NULL)
1667                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1668                                 socket_id);
1669         else
1670                 printf("Allocated mbuf pool on socket %d\n", socket_id);
1671 }
1672
1673 static inline int
1674 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
1675 {
1676         struct ipsec_sa *sa;
1677
1678         /* For inline protocol processing, the metadata in the event will
1679          * uniquely identify the security session which raised the event.
1680          * Application would then need the userdata it had registered with the
1681          * security session to process the event.
1682          */
1683
1684         sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
1685
1686         if (sa == NULL) {
1687                 /* userdata could not be retrieved */
1688                 return -1;
1689         }
1690
1691         /* Sequence number over flow. SA need to be re-established */
1692         RTE_SET_USED(sa);
1693         return 0;
1694 }
1695
1696 static int
1697 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
1698                  void *param, void *ret_param)
1699 {
1700         uint64_t md;
1701         struct rte_eth_event_ipsec_desc *event_desc = NULL;
1702         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
1703                                         rte_eth_dev_get_sec_ctx(port_id);
1704
1705         RTE_SET_USED(param);
1706
1707         if (type != RTE_ETH_EVENT_IPSEC)
1708                 return -1;
1709
1710         event_desc = ret_param;
1711         if (event_desc == NULL) {
1712                 printf("Event descriptor not set\n");
1713                 return -1;
1714         }
1715
1716         md = event_desc->metadata;
1717
1718         if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
1719                 return inline_ipsec_event_esn_overflow(ctx, md);
1720         else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
1721                 printf("Invalid IPsec event reported\n");
1722                 return -1;
1723         }
1724
1725         return -1;
1726 }
1727
1728 int32_t
1729 main(int32_t argc, char **argv)
1730 {
1731         int32_t ret;
1732         uint32_t lcore_id;
1733         uint8_t socket_id;
1734         uint16_t portid;
1735
1736         /* init EAL */
1737         ret = rte_eal_init(argc, argv);
1738         if (ret < 0)
1739                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1740         argc -= ret;
1741         argv += ret;
1742
1743         /* parse application arguments (after the EAL ones) */
1744         ret = parse_args(argc, argv);
1745         if (ret < 0)
1746                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1747
1748         if ((unprotected_port_mask & enabled_port_mask) !=
1749                         unprotected_port_mask)
1750                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1751                                 unprotected_port_mask);
1752
1753         if (check_params() < 0)
1754                 rte_exit(EXIT_FAILURE, "check_params failed\n");
1755
1756         ret = init_lcore_rx_queues();
1757         if (ret < 0)
1758                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1759
1760         nb_lcores = rte_lcore_count();
1761
1762         /* Replicate each context per socket */
1763         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1764                 if (rte_lcore_is_enabled(lcore_id) == 0)
1765                         continue;
1766
1767                 if (numa_on)
1768                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1769                 else
1770                         socket_id = 0;
1771
1772                 if (socket_ctx[socket_id].mbuf_pool)
1773                         continue;
1774
1775                 sa_init(&socket_ctx[socket_id], socket_id);
1776
1777                 sp4_init(&socket_ctx[socket_id], socket_id);
1778
1779                 sp6_init(&socket_ctx[socket_id], socket_id);
1780
1781                 rt_init(&socket_ctx[socket_id], socket_id);
1782
1783                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1784         }
1785
1786         RTE_ETH_FOREACH_DEV(portid) {
1787                 if ((enabled_port_mask & (1 << portid)) == 0)
1788                         continue;
1789
1790                 port_init(portid);
1791         }
1792
1793         cryptodevs_init();
1794
1795         /* start ports */
1796         RTE_ETH_FOREACH_DEV(portid) {
1797                 if ((enabled_port_mask & (1 << portid)) == 0)
1798                         continue;
1799
1800                 /* Start device */
1801                 ret = rte_eth_dev_start(portid);
1802                 if (ret < 0)
1803                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1804                                         "err=%d, port=%d\n", ret, portid);
1805                 /*
1806                  * If enabled, put device in promiscuous mode.
1807                  * This allows IO forwarding mode to forward packets
1808                  * to itself through 2 cross-connected  ports of the
1809                  * target machine.
1810                  */
1811                 if (promiscuous_on)
1812                         rte_eth_promiscuous_enable(portid);
1813
1814                 rte_eth_dev_callback_register(portid,
1815                         RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
1816         }
1817
1818         check_all_ports_link_status(enabled_port_mask);
1819
1820         /* launch per-lcore init on every lcore */
1821         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1822         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1823                 if (rte_eal_wait_lcore(lcore_id) < 0)
1824                         return -1;
1825         }
1826
1827         return 0;
1828 }