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