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