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