examples/ipsec-secgw: add default flow for inline Rx
[dpdk.git] / examples / ipsec-secgw / ipsec-secgw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <netinet/in.h>
11 #include <netinet/ip.h>
12 #include <netinet/ip6.h>
13 #include <string.h>
14 #include <sys/queue.h>
15 #include <stdarg.h>
16 #include <errno.h>
17 #include <getopt.h>
18
19 #include <rte_common.h>
20 #include <rte_byteorder.h>
21 #include <rte_log.h>
22 #include <rte_eal.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_mempool.h>
36 #include <rte_mbuf.h>
37 #include <rte_acl.h>
38 #include <rte_lpm.h>
39 #include <rte_lpm6.h>
40 #include <rte_hash.h>
41 #include <rte_jhash.h>
42 #include <rte_cryptodev.h>
43 #include <rte_security.h>
44 #include <rte_ip.h>
45 #include <rte_ip_frag.h>
46
47 #include "ipsec.h"
48 #include "parser.h"
49 #include "sad.h"
50
51 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
52
53 #define MAX_JUMBO_PKT_LEN  9600
54
55 #define MEMPOOL_CACHE_SIZE 256
56
57 #define NB_MBUF (32000)
58
59 #define CDEV_QUEUE_DESC 2048
60 #define CDEV_MAP_ENTRIES 16384
61 #define CDEV_MP_NB_OBJS 1024
62 #define CDEV_MP_CACHE_SZ 64
63 #define MAX_QUEUE_PAIRS 1
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 1024
82 #define IPSEC_SECGW_TX_DESC_DEFAULT 1024
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 #define FRAG_TBL_BUCKET_ENTRIES 4
116 #define MAX_FRAG_TTL_NS         (10LL * NS_PER_S)
117
118 #define MTU_TO_FRAMELEN(x)      ((x) + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN)
119
120 /* port/source ethernet addr and destination ethernet addr */
121 struct ethaddr_info {
122         uint64_t src, dst;
123 };
124
125 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
126         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
127         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
128         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
129         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
130 };
131
132 struct flow_info flow_info_tbl[RTE_MAX_ETHPORTS];
133
134 #define CMD_LINE_OPT_CONFIG             "config"
135 #define CMD_LINE_OPT_SINGLE_SA          "single-sa"
136 #define CMD_LINE_OPT_CRYPTODEV_MASK     "cryptodev_mask"
137 #define CMD_LINE_OPT_RX_OFFLOAD         "rxoffload"
138 #define CMD_LINE_OPT_TX_OFFLOAD         "txoffload"
139 #define CMD_LINE_OPT_REASSEMBLE         "reassemble"
140 #define CMD_LINE_OPT_MTU                "mtu"
141 #define CMD_LINE_OPT_FRAG_TTL           "frag-ttl"
142
143 enum {
144         /* long options mapped to a short option */
145
146         /* first long only option value must be >= 256, so that we won't
147          * conflict with short options
148          */
149         CMD_LINE_OPT_MIN_NUM = 256,
150         CMD_LINE_OPT_CONFIG_NUM,
151         CMD_LINE_OPT_SINGLE_SA_NUM,
152         CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
153         CMD_LINE_OPT_RX_OFFLOAD_NUM,
154         CMD_LINE_OPT_TX_OFFLOAD_NUM,
155         CMD_LINE_OPT_REASSEMBLE_NUM,
156         CMD_LINE_OPT_MTU_NUM,
157         CMD_LINE_OPT_FRAG_TTL_NUM,
158 };
159
160 static const struct option lgopts[] = {
161         {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
162         {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
163         {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
164         {CMD_LINE_OPT_RX_OFFLOAD, 1, 0, CMD_LINE_OPT_RX_OFFLOAD_NUM},
165         {CMD_LINE_OPT_TX_OFFLOAD, 1, 0, CMD_LINE_OPT_TX_OFFLOAD_NUM},
166         {CMD_LINE_OPT_REASSEMBLE, 1, 0, CMD_LINE_OPT_REASSEMBLE_NUM},
167         {CMD_LINE_OPT_MTU, 1, 0, CMD_LINE_OPT_MTU_NUM},
168         {CMD_LINE_OPT_FRAG_TTL, 1, 0, CMD_LINE_OPT_FRAG_TTL_NUM},
169         {NULL, 0, 0, 0}
170 };
171
172 /* mask of enabled ports */
173 static uint32_t enabled_port_mask;
174 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
175 static uint32_t unprotected_port_mask;
176 static int32_t promiscuous_on = 1;
177 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
178 static uint32_t nb_lcores;
179 static uint32_t single_sa;
180 static uint32_t single_sa_idx;
181
182 /*
183  * RX/TX HW offload capabilities to enable/use on ethernet ports.
184  * By default all capabilities are enabled.
185  */
186 static uint64_t dev_rx_offload = UINT64_MAX;
187 static uint64_t dev_tx_offload = UINT64_MAX;
188
189 /*
190  * global values that determine multi-seg policy
191  */
192 static uint32_t frag_tbl_sz;
193 static uint32_t frame_buf_size = RTE_MBUF_DEFAULT_BUF_SIZE;
194 static uint32_t mtu_size = RTE_ETHER_MTU;
195 static uint64_t frag_ttl_ns = MAX_FRAG_TTL_NS;
196
197 /* application wide librte_ipsec/SA parameters */
198 struct app_sa_prm app_sa_prm = {
199                         .enable = 0,
200                         .cache_sz = SA_CACHE_SZ
201                 };
202 static const char *cfgfile;
203
204 struct lcore_rx_queue {
205         uint16_t port_id;
206         uint8_t queue_id;
207 } __rte_cache_aligned;
208
209 struct lcore_params {
210         uint16_t port_id;
211         uint8_t queue_id;
212         uint8_t lcore_id;
213 } __rte_cache_aligned;
214
215 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
216
217 static struct lcore_params *lcore_params;
218 static uint16_t nb_lcore_params;
219
220 static struct rte_hash *cdev_map_in;
221 static struct rte_hash *cdev_map_out;
222
223 struct buffer {
224         uint16_t len;
225         struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
226 };
227
228 struct lcore_conf {
229         uint16_t nb_rx_queue;
230         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
231         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
232         struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
233         struct ipsec_ctx inbound;
234         struct ipsec_ctx outbound;
235         struct rt_ctx *rt4_ctx;
236         struct rt_ctx *rt6_ctx;
237         struct {
238                 struct rte_ip_frag_tbl *tbl;
239                 struct rte_mempool *pool_dir;
240                 struct rte_mempool *pool_indir;
241                 struct rte_ip_frag_death_row dr;
242         } frag;
243 } __rte_cache_aligned;
244
245 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
246
247 static struct rte_eth_conf port_conf = {
248         .rxmode = {
249                 .mq_mode        = ETH_MQ_RX_RSS,
250                 .max_rx_pkt_len = RTE_ETHER_MAX_LEN,
251                 .split_hdr_size = 0,
252                 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
253         },
254         .rx_adv_conf = {
255                 .rss_conf = {
256                         .rss_key = NULL,
257                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
258                                 ETH_RSS_TCP | ETH_RSS_SCTP,
259                 },
260         },
261         .txmode = {
262                 .mq_mode = ETH_MQ_TX_NONE,
263         },
264 };
265
266 static struct socket_ctx socket_ctx[NB_SOCKETS];
267
268 /*
269  * Determine is multi-segment support required:
270  *  - either frame buffer size is smaller then mtu
271  *  - or reassmeble support is requested
272  */
273 static int
274 multi_seg_required(void)
275 {
276         return (MTU_TO_FRAMELEN(mtu_size) + RTE_PKTMBUF_HEADROOM >
277                 frame_buf_size || frag_tbl_sz != 0);
278 }
279
280 static inline void
281 adjust_ipv4_pktlen(struct rte_mbuf *m, const struct rte_ipv4_hdr *iph,
282         uint32_t l2_len)
283 {
284         uint32_t plen, trim;
285
286         plen = rte_be_to_cpu_16(iph->total_length) + l2_len;
287         if (plen < m->pkt_len) {
288                 trim = m->pkt_len - plen;
289                 rte_pktmbuf_trim(m, trim);
290         }
291 }
292
293 static inline void
294 adjust_ipv6_pktlen(struct rte_mbuf *m, const struct rte_ipv6_hdr *iph,
295         uint32_t l2_len)
296 {
297         uint32_t plen, trim;
298
299         plen = rte_be_to_cpu_16(iph->payload_len) + sizeof(*iph) + l2_len;
300         if (plen < m->pkt_len) {
301                 trim = m->pkt_len - plen;
302                 rte_pktmbuf_trim(m, trim);
303         }
304 }
305
306 static inline void
307 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
308 {
309         const struct rte_ether_hdr *eth;
310         const struct rte_ipv4_hdr *iph4;
311         const struct rte_ipv6_hdr *iph6;
312
313         eth = rte_pktmbuf_mtod(pkt, const struct rte_ether_hdr *);
314         if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
315
316                 iph4 = (const struct rte_ipv4_hdr *)rte_pktmbuf_adj(pkt,
317                         RTE_ETHER_HDR_LEN);
318                 adjust_ipv4_pktlen(pkt, iph4, 0);
319
320                 if (iph4->next_proto_id == IPPROTO_ESP)
321                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
322                 else {
323                         t->ip4.data[t->ip4.num] = &iph4->next_proto_id;
324                         t->ip4.pkts[(t->ip4.num)++] = pkt;
325                 }
326                 pkt->l2_len = 0;
327                 pkt->l3_len = sizeof(*iph4);
328                 pkt->packet_type |= RTE_PTYPE_L3_IPV4;
329         } else if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
330                 int next_proto;
331                 size_t l3len, ext_len;
332                 uint8_t *p;
333
334                 /* get protocol type */
335                 iph6 = (const struct rte_ipv6_hdr *)rte_pktmbuf_adj(pkt,
336                         RTE_ETHER_HDR_LEN);
337                 adjust_ipv6_pktlen(pkt, iph6, 0);
338
339                 next_proto = iph6->proto;
340
341                 /* determine l3 header size up to ESP extension */
342                 l3len = sizeof(struct ip6_hdr);
343                 p = rte_pktmbuf_mtod(pkt, uint8_t *);
344                 while (next_proto != IPPROTO_ESP && l3len < pkt->data_len &&
345                         (next_proto = rte_ipv6_get_next_ext(p + l3len,
346                                                 next_proto, &ext_len)) >= 0)
347                         l3len += ext_len;
348
349                 /* drop packet when IPv6 header exceeds first segment length */
350                 if (unlikely(l3len > pkt->data_len)) {
351                         rte_pktmbuf_free(pkt);
352                         return;
353                 }
354
355                 if (next_proto == IPPROTO_ESP)
356                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
357                 else {
358                         t->ip6.data[t->ip6.num] = &iph6->proto;
359                         t->ip6.pkts[(t->ip6.num)++] = pkt;
360                 }
361                 pkt->l2_len = 0;
362                 pkt->l3_len = l3len;
363                 pkt->packet_type |= RTE_PTYPE_L3_IPV6;
364         } else {
365                 /* Unknown/Unsupported type, drop the packet */
366                 RTE_LOG(ERR, IPSEC, "Unsupported packet type 0x%x\n",
367                         rte_be_to_cpu_16(eth->ether_type));
368                 rte_pktmbuf_free(pkt);
369                 return;
370         }
371
372         /* Check if the packet has been processed inline. For inline protocol
373          * processed packets, the metadata in the mbuf can be used to identify
374          * the security processing done on the packet. The metadata will be
375          * used to retrieve the application registered userdata associated
376          * with the security session.
377          */
378
379         if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
380                 struct ipsec_sa *sa;
381                 struct ipsec_mbuf_metadata *priv;
382                 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
383                                                 rte_eth_dev_get_sec_ctx(
384                                                 pkt->port);
385
386                 /* Retrieve the userdata registered. Here, the userdata
387                  * registered is the SA pointer.
388                  */
389
390                 sa = (struct ipsec_sa *)
391                                 rte_security_get_userdata(ctx, pkt->udata64);
392
393                 if (sa == NULL) {
394                         /* userdata could not be retrieved */
395                         return;
396                 }
397
398                 /* Save SA as priv member in mbuf. This will be used in the
399                  * IPsec selector(SP-SA) check.
400                  */
401
402                 priv = get_priv(pkt);
403                 priv->sa = sa;
404         }
405 }
406
407 static inline void
408 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
409                 uint16_t nb_pkts)
410 {
411         int32_t i;
412
413         t->ipsec.num = 0;
414         t->ip4.num = 0;
415         t->ip6.num = 0;
416
417         for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
418                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
419                                         void *));
420                 prepare_one_packet(pkts[i], t);
421         }
422         /* Process left packets */
423         for (; i < nb_pkts; i++)
424                 prepare_one_packet(pkts[i], t);
425 }
426
427 static inline void
428 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port,
429                 const struct lcore_conf *qconf)
430 {
431         struct ip *ip;
432         struct rte_ether_hdr *ethhdr;
433
434         ip = rte_pktmbuf_mtod(pkt, struct ip *);
435
436         ethhdr = (struct rte_ether_hdr *)
437                 rte_pktmbuf_prepend(pkt, RTE_ETHER_HDR_LEN);
438
439         if (ip->ip_v == IPVERSION) {
440                 pkt->ol_flags |= qconf->outbound.ipv4_offloads;
441                 pkt->l3_len = sizeof(struct ip);
442                 pkt->l2_len = RTE_ETHER_HDR_LEN;
443
444                 ip->ip_sum = 0;
445
446                 /* calculate IPv4 cksum in SW */
447                 if ((pkt->ol_flags & PKT_TX_IP_CKSUM) == 0)
448                         ip->ip_sum = rte_ipv4_cksum((struct rte_ipv4_hdr *)ip);
449
450                 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);
451         } else {
452                 pkt->ol_flags |= qconf->outbound.ipv6_offloads;
453                 pkt->l3_len = sizeof(struct ip6_hdr);
454                 pkt->l2_len = RTE_ETHER_HDR_LEN;
455
456                 ethhdr->ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6);
457         }
458
459         memcpy(&ethhdr->s_addr, &ethaddr_tbl[port].src,
460                         sizeof(struct rte_ether_addr));
461         memcpy(&ethhdr->d_addr, &ethaddr_tbl[port].dst,
462                         sizeof(struct rte_ether_addr));
463 }
464
465 static inline void
466 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port,
467                 const struct lcore_conf *qconf)
468 {
469         int32_t i;
470         const int32_t prefetch_offset = 2;
471
472         for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
473                 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
474                 prepare_tx_pkt(pkts[i], port, qconf);
475         }
476         /* Process left packets */
477         for (; i < nb_pkts; i++)
478                 prepare_tx_pkt(pkts[i], port, qconf);
479 }
480
481 /* Send burst of packets on an output interface */
482 static inline int32_t
483 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
484 {
485         struct rte_mbuf **m_table;
486         int32_t ret;
487         uint16_t queueid;
488
489         queueid = qconf->tx_queue_id[port];
490         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
491
492         prepare_tx_burst(m_table, n, port, qconf);
493
494         ret = rte_eth_tx_burst(port, queueid, m_table, n);
495         if (unlikely(ret < n)) {
496                 do {
497                         rte_pktmbuf_free(m_table[ret]);
498                 } while (++ret < n);
499         }
500
501         return 0;
502 }
503
504 /*
505  * Helper function to fragment and queue for TX one packet.
506  */
507 static inline uint32_t
508 send_fragment_packet(struct lcore_conf *qconf, struct rte_mbuf *m,
509         uint16_t port, uint8_t proto)
510 {
511         struct buffer *tbl;
512         uint32_t len, n;
513         int32_t rc;
514
515         tbl =  qconf->tx_mbufs + port;
516         len = tbl->len;
517
518         /* free space for new fragments */
519         if (len + RTE_LIBRTE_IP_FRAG_MAX_FRAG >=  RTE_DIM(tbl->m_table)) {
520                 send_burst(qconf, len, port);
521                 len = 0;
522         }
523
524         n = RTE_DIM(tbl->m_table) - len;
525
526         if (proto == IPPROTO_IP)
527                 rc = rte_ipv4_fragment_packet(m, tbl->m_table + len,
528                         n, mtu_size, qconf->frag.pool_dir,
529                         qconf->frag.pool_indir);
530         else
531                 rc = rte_ipv6_fragment_packet(m, tbl->m_table + len,
532                         n, mtu_size, qconf->frag.pool_dir,
533                         qconf->frag.pool_indir);
534
535         if (rc >= 0)
536                 len += rc;
537         else
538                 RTE_LOG(ERR, IPSEC,
539                         "%s: failed to fragment packet with size %u, "
540                         "error code: %d\n",
541                         __func__, m->pkt_len, rte_errno);
542
543         rte_pktmbuf_free(m);
544         return len;
545 }
546
547 /* Enqueue a single packet, and send burst if queue is filled */
548 static inline int32_t
549 send_single_packet(struct rte_mbuf *m, uint16_t port, uint8_t proto)
550 {
551         uint32_t lcore_id;
552         uint16_t len;
553         struct lcore_conf *qconf;
554
555         lcore_id = rte_lcore_id();
556
557         qconf = &lcore_conf[lcore_id];
558         len = qconf->tx_mbufs[port].len;
559
560         if (m->pkt_len <= mtu_size) {
561                 qconf->tx_mbufs[port].m_table[len] = m;
562                 len++;
563
564         /* need to fragment the packet */
565         } else if (frag_tbl_sz > 0)
566                 len = send_fragment_packet(qconf, m, port, proto);
567         else
568                 rte_pktmbuf_free(m);
569
570         /* enough pkts to be sent */
571         if (unlikely(len == MAX_PKT_BURST)) {
572                 send_burst(qconf, MAX_PKT_BURST, port);
573                 len = 0;
574         }
575
576         qconf->tx_mbufs[port].len = len;
577         return 0;
578 }
579
580 static inline void
581 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
582                 uint16_t lim)
583 {
584         struct rte_mbuf *m;
585         uint32_t i, j, res, sa_idx;
586
587         if (ip->num == 0 || sp == NULL)
588                 return;
589
590         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
591                         ip->num, DEFAULT_MAX_CATEGORIES);
592
593         j = 0;
594         for (i = 0; i < ip->num; i++) {
595                 m = ip->pkts[i];
596                 res = ip->res[i];
597                 if (res == BYPASS) {
598                         ip->pkts[j++] = m;
599                         continue;
600                 }
601                 if (res == DISCARD) {
602                         rte_pktmbuf_free(m);
603                         continue;
604                 }
605
606                 /* Only check SPI match for processed IPSec packets */
607                 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) {
608                         rte_pktmbuf_free(m);
609                         continue;
610                 }
611
612                 sa_idx = res - 1;
613                 if (!inbound_sa_check(sa, m, sa_idx)) {
614                         rte_pktmbuf_free(m);
615                         continue;
616                 }
617                 ip->pkts[j++] = m;
618         }
619         ip->num = j;
620 }
621
622 static void
623 split46_traffic(struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t num)
624 {
625         uint32_t i, n4, n6;
626         struct ip *ip;
627         struct rte_mbuf *m;
628
629         n4 = trf->ip4.num;
630         n6 = trf->ip6.num;
631
632         for (i = 0; i < num; i++) {
633
634                 m = mb[i];
635                 ip = rte_pktmbuf_mtod(m, struct ip *);
636
637                 if (ip->ip_v == IPVERSION) {
638                         trf->ip4.pkts[n4] = m;
639                         trf->ip4.data[n4] = rte_pktmbuf_mtod_offset(m,
640                                         uint8_t *, offsetof(struct ip, ip_p));
641                         n4++;
642                 } else if (ip->ip_v == IP6_VERSION) {
643                         trf->ip6.pkts[n6] = m;
644                         trf->ip6.data[n6] = rte_pktmbuf_mtod_offset(m,
645                                         uint8_t *,
646                                         offsetof(struct ip6_hdr, ip6_nxt));
647                         n6++;
648                 } else
649                         rte_pktmbuf_free(m);
650         }
651
652         trf->ip4.num = n4;
653         trf->ip6.num = n6;
654 }
655
656
657 static inline void
658 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
659                 struct ipsec_traffic *traffic)
660 {
661         uint16_t nb_pkts_in, n_ip4, n_ip6;
662
663         n_ip4 = traffic->ip4.num;
664         n_ip6 = traffic->ip6.num;
665
666         if (app_sa_prm.enable == 0) {
667                 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
668                                 traffic->ipsec.num, MAX_PKT_BURST);
669                 split46_traffic(traffic, traffic->ipsec.pkts, nb_pkts_in);
670         } else {
671                 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
672                         traffic->ipsec.saptr, traffic->ipsec.num);
673                 ipsec_process(ipsec_ctx, traffic);
674         }
675
676         inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
677                         n_ip4);
678
679         inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
680                         n_ip6);
681 }
682
683 static inline void
684 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
685                 struct traffic_type *ipsec)
686 {
687         struct rte_mbuf *m;
688         uint32_t i, j, sa_idx;
689
690         if (ip->num == 0 || sp == NULL)
691                 return;
692
693         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
694                         ip->num, DEFAULT_MAX_CATEGORIES);
695
696         j = 0;
697         for (i = 0; i < ip->num; i++) {
698                 m = ip->pkts[i];
699                 sa_idx = ip->res[i] - 1;
700                 if (ip->res[i] == DISCARD)
701                         rte_pktmbuf_free(m);
702                 else if (ip->res[i] == BYPASS)
703                         ip->pkts[j++] = m;
704                 else {
705                         ipsec->res[ipsec->num] = sa_idx;
706                         ipsec->pkts[ipsec->num++] = m;
707                 }
708         }
709         ip->num = j;
710 }
711
712 static inline void
713 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
714                 struct ipsec_traffic *traffic)
715 {
716         struct rte_mbuf *m;
717         uint16_t idx, nb_pkts_out, i;
718
719         /* Drop any IPsec traffic from protected ports */
720         for (i = 0; i < traffic->ipsec.num; i++)
721                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
722
723         traffic->ipsec.num = 0;
724
725         outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
726
727         outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
728
729         if (app_sa_prm.enable == 0) {
730
731                 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
732                                 traffic->ipsec.res, traffic->ipsec.num,
733                                 MAX_PKT_BURST);
734
735                 for (i = 0; i < nb_pkts_out; i++) {
736                         m = traffic->ipsec.pkts[i];
737                         struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
738                         if (ip->ip_v == IPVERSION) {
739                                 idx = traffic->ip4.num++;
740                                 traffic->ip4.pkts[idx] = m;
741                         } else {
742                                 idx = traffic->ip6.num++;
743                                 traffic->ip6.pkts[idx] = m;
744                         }
745                 }
746         } else {
747                 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
748                         traffic->ipsec.saptr, traffic->ipsec.num);
749                 ipsec_process(ipsec_ctx, traffic);
750         }
751 }
752
753 static inline void
754 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
755                 struct ipsec_traffic *traffic)
756 {
757         struct rte_mbuf *m;
758         uint32_t nb_pkts_in, i, idx;
759
760         /* Drop any IPv4 traffic from unprotected ports */
761         for (i = 0; i < traffic->ip4.num; i++)
762                 rte_pktmbuf_free(traffic->ip4.pkts[i]);
763
764         traffic->ip4.num = 0;
765
766         /* Drop any IPv6 traffic from unprotected ports */
767         for (i = 0; i < traffic->ip6.num; i++)
768                 rte_pktmbuf_free(traffic->ip6.pkts[i]);
769
770         traffic->ip6.num = 0;
771
772         if (app_sa_prm.enable == 0) {
773
774                 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
775                                 traffic->ipsec.num, MAX_PKT_BURST);
776
777                 for (i = 0; i < nb_pkts_in; i++) {
778                         m = traffic->ipsec.pkts[i];
779                         struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
780                         if (ip->ip_v == IPVERSION) {
781                                 idx = traffic->ip4.num++;
782                                 traffic->ip4.pkts[idx] = m;
783                         } else {
784                                 idx = traffic->ip6.num++;
785                                 traffic->ip6.pkts[idx] = m;
786                         }
787                 }
788         } else {
789                 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
790                         traffic->ipsec.saptr, traffic->ipsec.num);
791                 ipsec_process(ipsec_ctx, traffic);
792         }
793 }
794
795 static inline void
796 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
797                 struct ipsec_traffic *traffic)
798 {
799         struct rte_mbuf *m;
800         uint32_t nb_pkts_out, i, n;
801         struct ip *ip;
802
803         /* Drop any IPsec traffic from protected ports */
804         for (i = 0; i < traffic->ipsec.num; i++)
805                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
806
807         n = 0;
808
809         for (i = 0; i < traffic->ip4.num; i++) {
810                 traffic->ipsec.pkts[n] = traffic->ip4.pkts[i];
811                 traffic->ipsec.res[n++] = single_sa_idx;
812         }
813
814         for (i = 0; i < traffic->ip6.num; i++) {
815                 traffic->ipsec.pkts[n] = traffic->ip6.pkts[i];
816                 traffic->ipsec.res[n++] = single_sa_idx;
817         }
818
819         traffic->ip4.num = 0;
820         traffic->ip6.num = 0;
821         traffic->ipsec.num = n;
822
823         if (app_sa_prm.enable == 0) {
824
825                 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
826                                 traffic->ipsec.res, traffic->ipsec.num,
827                                 MAX_PKT_BURST);
828
829                 /* They all sue the same SA (ip4 or ip6 tunnel) */
830                 m = traffic->ipsec.pkts[0];
831                 ip = rte_pktmbuf_mtod(m, struct ip *);
832                 if (ip->ip_v == IPVERSION) {
833                         traffic->ip4.num = nb_pkts_out;
834                         for (i = 0; i < nb_pkts_out; i++)
835                                 traffic->ip4.pkts[i] = traffic->ipsec.pkts[i];
836                 } else {
837                         traffic->ip6.num = nb_pkts_out;
838                         for (i = 0; i < nb_pkts_out; i++)
839                                 traffic->ip6.pkts[i] = traffic->ipsec.pkts[i];
840                 }
841         } else {
842                 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
843                         traffic->ipsec.saptr, traffic->ipsec.num);
844                 ipsec_process(ipsec_ctx, traffic);
845         }
846 }
847
848 static inline int32_t
849 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
850 {
851         struct ipsec_mbuf_metadata *priv;
852         struct ipsec_sa *sa;
853
854         priv = get_priv(pkt);
855
856         sa = priv->sa;
857         if (unlikely(sa == NULL)) {
858                 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
859                 goto fail;
860         }
861
862         if (is_ipv6)
863                 return sa->portid;
864
865         /* else */
866         return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
867
868 fail:
869         if (is_ipv6)
870                 return -1;
871
872         /* else */
873         return 0;
874 }
875
876 static inline void
877 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
878 {
879         uint32_t hop[MAX_PKT_BURST * 2];
880         uint32_t dst_ip[MAX_PKT_BURST * 2];
881         int32_t pkt_hop = 0;
882         uint16_t i, offset;
883         uint16_t lpm_pkts = 0;
884
885         if (nb_pkts == 0)
886                 return;
887
888         /* Need to do an LPM lookup for non-inline packets. Inline packets will
889          * have port ID in the SA
890          */
891
892         for (i = 0; i < nb_pkts; i++) {
893                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
894                         /* Security offload not enabled. So an LPM lookup is
895                          * required to get the hop
896                          */
897                         offset = offsetof(struct ip, ip_dst);
898                         dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
899                                         uint32_t *, offset);
900                         dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
901                         lpm_pkts++;
902                 }
903         }
904
905         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
906
907         lpm_pkts = 0;
908
909         for (i = 0; i < nb_pkts; i++) {
910                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
911                         /* Read hop from the SA */
912                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
913                 } else {
914                         /* Need to use hop returned by lookup */
915                         pkt_hop = hop[lpm_pkts++];
916                 }
917
918                 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
919                         rte_pktmbuf_free(pkts[i]);
920                         continue;
921                 }
922                 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IP);
923         }
924 }
925
926 static inline void
927 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
928 {
929         int32_t hop[MAX_PKT_BURST * 2];
930         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
931         uint8_t *ip6_dst;
932         int32_t pkt_hop = 0;
933         uint16_t i, offset;
934         uint16_t lpm_pkts = 0;
935
936         if (nb_pkts == 0)
937                 return;
938
939         /* Need to do an LPM lookup for non-inline packets. Inline packets will
940          * have port ID in the SA
941          */
942
943         for (i = 0; i < nb_pkts; i++) {
944                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
945                         /* Security offload not enabled. So an LPM lookup is
946                          * required to get the hop
947                          */
948                         offset = offsetof(struct ip6_hdr, ip6_dst);
949                         ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
950                                         offset);
951                         memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
952                         lpm_pkts++;
953                 }
954         }
955
956         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
957                         lpm_pkts);
958
959         lpm_pkts = 0;
960
961         for (i = 0; i < nb_pkts; i++) {
962                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
963                         /* Read hop from the SA */
964                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
965                 } else {
966                         /* Need to use hop returned by lookup */
967                         pkt_hop = hop[lpm_pkts++];
968                 }
969
970                 if (pkt_hop == -1) {
971                         rte_pktmbuf_free(pkts[i]);
972                         continue;
973                 }
974                 send_single_packet(pkts[i], pkt_hop & 0xff, IPPROTO_IPV6);
975         }
976 }
977
978 static inline void
979 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
980                 uint8_t nb_pkts, uint16_t portid)
981 {
982         struct ipsec_traffic traffic;
983
984         prepare_traffic(pkts, &traffic, nb_pkts);
985
986         if (unlikely(single_sa)) {
987                 if (UNPROTECTED_PORT(portid))
988                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
989                 else
990                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
991         } else {
992                 if (UNPROTECTED_PORT(portid))
993                         process_pkts_inbound(&qconf->inbound, &traffic);
994                 else
995                         process_pkts_outbound(&qconf->outbound, &traffic);
996         }
997
998         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
999         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
1000 }
1001
1002 static inline void
1003 drain_tx_buffers(struct lcore_conf *qconf)
1004 {
1005         struct buffer *buf;
1006         uint32_t portid;
1007
1008         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1009                 buf = &qconf->tx_mbufs[portid];
1010                 if (buf->len == 0)
1011                         continue;
1012                 send_burst(qconf, buf->len, portid);
1013                 buf->len = 0;
1014         }
1015 }
1016
1017 static inline void
1018 drain_crypto_buffers(struct lcore_conf *qconf)
1019 {
1020         uint32_t i;
1021         struct ipsec_ctx *ctx;
1022
1023         /* drain inbound buffers*/
1024         ctx = &qconf->inbound;
1025         for (i = 0; i != ctx->nb_qps; i++) {
1026                 if (ctx->tbl[i].len != 0)
1027                         enqueue_cop_burst(ctx->tbl  + i);
1028         }
1029
1030         /* drain outbound buffers*/
1031         ctx = &qconf->outbound;
1032         for (i = 0; i != ctx->nb_qps; i++) {
1033                 if (ctx->tbl[i].len != 0)
1034                         enqueue_cop_burst(ctx->tbl  + i);
1035         }
1036 }
1037
1038 static void
1039 drain_inbound_crypto_queues(const struct lcore_conf *qconf,
1040                 struct ipsec_ctx *ctx)
1041 {
1042         uint32_t n;
1043         struct ipsec_traffic trf;
1044
1045         if (app_sa_prm.enable == 0) {
1046
1047                 /* dequeue packets from crypto-queue */
1048                 n = ipsec_inbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1049                         RTE_DIM(trf.ipsec.pkts));
1050
1051                 trf.ip4.num = 0;
1052                 trf.ip6.num = 0;
1053
1054                 /* split traffic by ipv4-ipv6 */
1055                 split46_traffic(&trf, trf.ipsec.pkts, n);
1056         } else
1057                 ipsec_cqp_process(ctx, &trf);
1058
1059         /* process ipv4 packets */
1060         if (trf.ip4.num != 0) {
1061                 inbound_sp_sa(ctx->sp4_ctx, ctx->sa_ctx, &trf.ip4, 0);
1062                 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1063         }
1064
1065         /* process ipv6 packets */
1066         if (trf.ip6.num != 0) {
1067                 inbound_sp_sa(ctx->sp6_ctx, ctx->sa_ctx, &trf.ip6, 0);
1068                 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1069         }
1070 }
1071
1072 static void
1073 drain_outbound_crypto_queues(const struct lcore_conf *qconf,
1074                 struct ipsec_ctx *ctx)
1075 {
1076         uint32_t n;
1077         struct ipsec_traffic trf;
1078
1079         if (app_sa_prm.enable == 0) {
1080
1081                 /* dequeue packets from crypto-queue */
1082                 n = ipsec_outbound_cqp_dequeue(ctx, trf.ipsec.pkts,
1083                         RTE_DIM(trf.ipsec.pkts));
1084
1085                 trf.ip4.num = 0;
1086                 trf.ip6.num = 0;
1087
1088                 /* split traffic by ipv4-ipv6 */
1089                 split46_traffic(&trf, trf.ipsec.pkts, n);
1090         } else
1091                 ipsec_cqp_process(ctx, &trf);
1092
1093         /* process ipv4 packets */
1094         if (trf.ip4.num != 0)
1095                 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
1096
1097         /* process ipv6 packets */
1098         if (trf.ip6.num != 0)
1099                 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
1100 }
1101
1102 /* main processing loop */
1103 static int32_t
1104 main_loop(__attribute__((unused)) void *dummy)
1105 {
1106         struct rte_mbuf *pkts[MAX_PKT_BURST];
1107         uint32_t lcore_id;
1108         uint64_t prev_tsc, diff_tsc, cur_tsc;
1109         int32_t i, nb_rx;
1110         uint16_t portid;
1111         uint8_t queueid;
1112         struct lcore_conf *qconf;
1113         int32_t rc, socket_id;
1114         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
1115                         / US_PER_S * BURST_TX_DRAIN_US;
1116         struct lcore_rx_queue *rxql;
1117
1118         prev_tsc = 0;
1119         lcore_id = rte_lcore_id();
1120         qconf = &lcore_conf[lcore_id];
1121         rxql = qconf->rx_queue_list;
1122         socket_id = rte_lcore_to_socket_id(lcore_id);
1123
1124         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
1125         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
1126         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
1127         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
1128         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
1129         qconf->inbound.cdev_map = cdev_map_in;
1130         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
1131         qconf->inbound.session_priv_pool =
1132                         socket_ctx[socket_id].session_priv_pool;
1133         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
1134         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
1135         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
1136         qconf->outbound.cdev_map = cdev_map_out;
1137         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
1138         qconf->outbound.session_priv_pool =
1139                         socket_ctx[socket_id].session_priv_pool;
1140         qconf->frag.pool_dir = socket_ctx[socket_id].mbuf_pool;
1141         qconf->frag.pool_indir = socket_ctx[socket_id].mbuf_pool_indir;
1142
1143         rc = ipsec_sad_lcore_cache_init(app_sa_prm.cache_sz);
1144         if (rc != 0) {
1145                 RTE_LOG(ERR, IPSEC,
1146                         "SAD cache init on lcore %u, failed with code: %d\n",
1147                         lcore_id, rc);
1148                 return rc;
1149         }
1150
1151         if (qconf->nb_rx_queue == 0) {
1152                 RTE_LOG(DEBUG, IPSEC, "lcore %u has nothing to do\n",
1153                         lcore_id);
1154                 return 0;
1155         }
1156
1157         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
1158
1159         for (i = 0; i < qconf->nb_rx_queue; i++) {
1160                 portid = rxql[i].port_id;
1161                 queueid = rxql[i].queue_id;
1162                 RTE_LOG(INFO, IPSEC,
1163                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
1164                         lcore_id, portid, queueid);
1165         }
1166
1167         while (1) {
1168                 cur_tsc = rte_rdtsc();
1169
1170                 /* TX queue buffer drain */
1171                 diff_tsc = cur_tsc - prev_tsc;
1172
1173                 if (unlikely(diff_tsc > drain_tsc)) {
1174                         drain_tx_buffers(qconf);
1175                         drain_crypto_buffers(qconf);
1176                         prev_tsc = cur_tsc;
1177                 }
1178
1179                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
1180
1181                         /* Read packets from RX queues */
1182                         portid = rxql[i].port_id;
1183                         queueid = rxql[i].queue_id;
1184                         nb_rx = rte_eth_rx_burst(portid, queueid,
1185                                         pkts, MAX_PKT_BURST);
1186
1187                         if (nb_rx > 0)
1188                                 process_pkts(qconf, pkts, nb_rx, portid);
1189
1190                         /* dequeue and process completed crypto-ops */
1191                         if (UNPROTECTED_PORT(portid))
1192                                 drain_inbound_crypto_queues(qconf,
1193                                         &qconf->inbound);
1194                         else
1195                                 drain_outbound_crypto_queues(qconf,
1196                                         &qconf->outbound);
1197                 }
1198         }
1199 }
1200
1201 static int32_t
1202 check_params(void)
1203 {
1204         uint8_t lcore;
1205         uint16_t portid;
1206         uint16_t i;
1207         int32_t socket_id;
1208
1209         if (lcore_params == NULL) {
1210                 printf("Error: No port/queue/core mappings\n");
1211                 return -1;
1212         }
1213
1214         for (i = 0; i < nb_lcore_params; ++i) {
1215                 lcore = lcore_params[i].lcore_id;
1216                 if (!rte_lcore_is_enabled(lcore)) {
1217                         printf("error: lcore %hhu is not enabled in "
1218                                 "lcore mask\n", lcore);
1219                         return -1;
1220                 }
1221                 socket_id = rte_lcore_to_socket_id(lcore);
1222                 if (socket_id != 0 && numa_on == 0) {
1223                         printf("warning: lcore %hhu is on socket %d "
1224                                 "with numa off\n",
1225                                 lcore, socket_id);
1226                 }
1227                 portid = lcore_params[i].port_id;
1228                 if ((enabled_port_mask & (1 << portid)) == 0) {
1229                         printf("port %u is not enabled in port mask\n", portid);
1230                         return -1;
1231                 }
1232                 if (!rte_eth_dev_is_valid_port(portid)) {
1233                         printf("port %u is not present on the board\n", portid);
1234                         return -1;
1235                 }
1236         }
1237         return 0;
1238 }
1239
1240 static uint8_t
1241 get_port_nb_rx_queues(const uint16_t port)
1242 {
1243         int32_t queue = -1;
1244         uint16_t i;
1245
1246         for (i = 0; i < nb_lcore_params; ++i) {
1247                 if (lcore_params[i].port_id == port &&
1248                                 lcore_params[i].queue_id > queue)
1249                         queue = lcore_params[i].queue_id;
1250         }
1251         return (uint8_t)(++queue);
1252 }
1253
1254 static int32_t
1255 init_lcore_rx_queues(void)
1256 {
1257         uint16_t i, nb_rx_queue;
1258         uint8_t lcore;
1259
1260         for (i = 0; i < nb_lcore_params; ++i) {
1261                 lcore = lcore_params[i].lcore_id;
1262                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
1263                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
1264                         printf("error: too many queues (%u) for lcore: %u\n",
1265                                         nb_rx_queue + 1, lcore);
1266                         return -1;
1267                 }
1268                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
1269                         lcore_params[i].port_id;
1270                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
1271                         lcore_params[i].queue_id;
1272                 lcore_conf[lcore].nb_rx_queue++;
1273         }
1274         return 0;
1275 }
1276
1277 /* display usage */
1278 static void
1279 print_usage(const char *prgname)
1280 {
1281         fprintf(stderr, "%s [EAL options] --"
1282                 " -p PORTMASK"
1283                 " [-P]"
1284                 " [-u PORTMASK]"
1285                 " [-j FRAMESIZE]"
1286                 " [-l]"
1287                 " [-w REPLAY_WINDOW_SIZE]"
1288                 " [-e]"
1289                 " [-a]"
1290                 " [-c]"
1291                 " -f CONFIG_FILE"
1292                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
1293                 " [--single-sa SAIDX]"
1294                 " [--cryptodev_mask MASK]"
1295                 " [--" CMD_LINE_OPT_RX_OFFLOAD " RX_OFFLOAD_MASK]"
1296                 " [--" CMD_LINE_OPT_TX_OFFLOAD " TX_OFFLOAD_MASK]"
1297                 " [--" CMD_LINE_OPT_REASSEMBLE " REASSEMBLE_TABLE_SIZE]"
1298                 " [--" CMD_LINE_OPT_MTU " MTU]"
1299                 "\n\n"
1300                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
1301                 "  -P : Enable promiscuous mode\n"
1302                 "  -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
1303                 "  -j FRAMESIZE: Data buffer size, minimum (and default)\n"
1304                 "     value: RTE_MBUF_DEFAULT_BUF_SIZE\n"
1305                 "  -l enables code-path that uses librte_ipsec\n"
1306                 "  -w REPLAY_WINDOW_SIZE specifies IPsec SQN replay window\n"
1307                 "     size for each SA\n"
1308                 "  -e enables ESN\n"
1309                 "  -a enables SA SQN atomic behaviour\n"
1310                 "  -c specifies inbound SAD cache size,\n"
1311                 "     zero value disables the cache (default value: 128)\n"
1312                 "  -f CONFIG_FILE: Configuration file\n"
1313                 "  --config (port,queue,lcore): Rx queue configuration\n"
1314                 "  --single-sa SAIDX: Use single SA index for outbound traffic,\n"
1315                 "                     bypassing the SP\n"
1316                 "  --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
1317                 "                         devices to configure\n"
1318                 "  --" CMD_LINE_OPT_RX_OFFLOAD
1319                 ": bitmask of the RX HW offload capabilities to enable/use\n"
1320                 "                         (DEV_RX_OFFLOAD_*)\n"
1321                 "  --" CMD_LINE_OPT_TX_OFFLOAD
1322                 ": bitmask of the TX HW offload capabilities to enable/use\n"
1323                 "                         (DEV_TX_OFFLOAD_*)\n"
1324                 "  --" CMD_LINE_OPT_REASSEMBLE " NUM"
1325                 ": max number of entries in reassemble(fragment) table\n"
1326                 "    (zero (default value) disables reassembly)\n"
1327                 "  --" CMD_LINE_OPT_MTU " MTU"
1328                 ": MTU value on all ports (default value: 1500)\n"
1329                 "    outgoing packets with bigger size will be fragmented\n"
1330                 "    incoming packets with bigger size will be discarded\n"
1331                 "  --" CMD_LINE_OPT_FRAG_TTL " FRAG_TTL_NS"
1332                 ": fragments lifetime in nanoseconds, default\n"
1333                 "    and maximum value is 10.000.000.000 ns (10 s)\n"
1334                 "\n",
1335                 prgname);
1336 }
1337
1338 static int
1339 parse_mask(const char *str, uint64_t *val)
1340 {
1341         char *end;
1342         unsigned long t;
1343
1344         errno = 0;
1345         t = strtoul(str, &end, 0);
1346         if (errno != 0 || end[0] != 0)
1347                 return -EINVAL;
1348
1349         *val = t;
1350         return 0;
1351 }
1352
1353 static int32_t
1354 parse_portmask(const char *portmask)
1355 {
1356         char *end = NULL;
1357         unsigned long pm;
1358
1359         /* parse hexadecimal string */
1360         pm = strtoul(portmask, &end, 16);
1361         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
1362                 return -1;
1363
1364         if ((pm == 0) && errno)
1365                 return -1;
1366
1367         return pm;
1368 }
1369
1370 static int64_t
1371 parse_decimal(const char *str)
1372 {
1373         char *end = NULL;
1374         uint64_t num;
1375
1376         num = strtoull(str, &end, 10);
1377         if ((str[0] == '\0') || (end == NULL) || (*end != '\0')
1378                 || num > INT64_MAX)
1379                 return -1;
1380
1381         return num;
1382 }
1383
1384 static int32_t
1385 parse_config(const char *q_arg)
1386 {
1387         char s[256];
1388         const char *p, *p0 = q_arg;
1389         char *end;
1390         enum fieldnames {
1391                 FLD_PORT = 0,
1392                 FLD_QUEUE,
1393                 FLD_LCORE,
1394                 _NUM_FLD
1395         };
1396         unsigned long int_fld[_NUM_FLD];
1397         char *str_fld[_NUM_FLD];
1398         int32_t i;
1399         uint32_t size;
1400
1401         nb_lcore_params = 0;
1402
1403         while ((p = strchr(p0, '(')) != NULL) {
1404                 ++p;
1405                 p0 = strchr(p, ')');
1406                 if (p0 == NULL)
1407                         return -1;
1408
1409                 size = p0 - p;
1410                 if (size >= sizeof(s))
1411                         return -1;
1412
1413                 snprintf(s, sizeof(s), "%.*s", size, p);
1414                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1415                                 _NUM_FLD)
1416                         return -1;
1417                 for (i = 0; i < _NUM_FLD; i++) {
1418                         errno = 0;
1419                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1420                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1421                                 return -1;
1422                 }
1423                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1424                         printf("exceeded max number of lcore params: %hu\n",
1425                                 nb_lcore_params);
1426                         return -1;
1427                 }
1428                 lcore_params_array[nb_lcore_params].port_id =
1429                         (uint8_t)int_fld[FLD_PORT];
1430                 lcore_params_array[nb_lcore_params].queue_id =
1431                         (uint8_t)int_fld[FLD_QUEUE];
1432                 lcore_params_array[nb_lcore_params].lcore_id =
1433                         (uint8_t)int_fld[FLD_LCORE];
1434                 ++nb_lcore_params;
1435         }
1436         lcore_params = lcore_params_array;
1437         return 0;
1438 }
1439
1440 static void
1441 print_app_sa_prm(const struct app_sa_prm *prm)
1442 {
1443         printf("librte_ipsec usage: %s\n",
1444                 (prm->enable == 0) ? "disabled" : "enabled");
1445
1446         printf("replay window size: %u\n", prm->window_size);
1447         printf("ESN: %s\n", (prm->enable_esn == 0) ? "disabled" : "enabled");
1448         printf("SA flags: %#" PRIx64 "\n", prm->flags);
1449         printf("Frag TTL: %" PRIu64 " ns\n", frag_ttl_ns);
1450 }
1451
1452 static int32_t
1453 parse_args(int32_t argc, char **argv)
1454 {
1455         int opt;
1456         int64_t ret;
1457         char **argvopt;
1458         int32_t option_index;
1459         char *prgname = argv[0];
1460         int32_t f_present = 0;
1461
1462         argvopt = argv;
1463
1464         while ((opt = getopt_long(argc, argvopt, "aelp:Pu:f:j:w:c:",
1465                                 lgopts, &option_index)) != EOF) {
1466
1467                 switch (opt) {
1468                 case 'p':
1469                         enabled_port_mask = parse_portmask(optarg);
1470                         if (enabled_port_mask == 0) {
1471                                 printf("invalid portmask\n");
1472                                 print_usage(prgname);
1473                                 return -1;
1474                         }
1475                         break;
1476                 case 'P':
1477                         printf("Promiscuous mode selected\n");
1478                         promiscuous_on = 1;
1479                         break;
1480                 case 'u':
1481                         unprotected_port_mask = parse_portmask(optarg);
1482                         if (unprotected_port_mask == 0) {
1483                                 printf("invalid unprotected portmask\n");
1484                                 print_usage(prgname);
1485                                 return -1;
1486                         }
1487                         break;
1488                 case 'f':
1489                         if (f_present == 1) {
1490                                 printf("\"-f\" option present more than "
1491                                         "once!\n");
1492                                 print_usage(prgname);
1493                                 return -1;
1494                         }
1495                         cfgfile = optarg;
1496                         f_present = 1;
1497                         break;
1498                 case 'j':
1499                         ret = parse_decimal(optarg);
1500                         if (ret < RTE_MBUF_DEFAULT_BUF_SIZE ||
1501                                         ret > UINT16_MAX) {
1502                                 printf("Invalid frame buffer size value: %s\n",
1503                                         optarg);
1504                                 print_usage(prgname);
1505                                 return -1;
1506                         }
1507                         frame_buf_size = ret;
1508                         printf("Custom frame buffer size %u\n", frame_buf_size);
1509                         break;
1510                 case 'l':
1511                         app_sa_prm.enable = 1;
1512                         break;
1513                 case 'w':
1514                         app_sa_prm.window_size = parse_decimal(optarg);
1515                         break;
1516                 case 'e':
1517                         app_sa_prm.enable_esn = 1;
1518                         break;
1519                 case 'a':
1520                         app_sa_prm.enable = 1;
1521                         app_sa_prm.flags |= RTE_IPSEC_SAFLAG_SQN_ATOM;
1522                         break;
1523                 case 'c':
1524                         ret = parse_decimal(optarg);
1525                         if (ret < 0) {
1526                                 printf("Invalid SA cache size: %s\n", optarg);
1527                                 print_usage(prgname);
1528                                 return -1;
1529                         }
1530                         app_sa_prm.cache_sz = ret;
1531                         break;
1532                 case CMD_LINE_OPT_CONFIG_NUM:
1533                         ret = parse_config(optarg);
1534                         if (ret) {
1535                                 printf("Invalid config\n");
1536                                 print_usage(prgname);
1537                                 return -1;
1538                         }
1539                         break;
1540                 case CMD_LINE_OPT_SINGLE_SA_NUM:
1541                         ret = parse_decimal(optarg);
1542                         if (ret == -1 || ret > UINT32_MAX) {
1543                                 printf("Invalid argument[sa_idx]\n");
1544                                 print_usage(prgname);
1545                                 return -1;
1546                         }
1547
1548                         /* else */
1549                         single_sa = 1;
1550                         single_sa_idx = ret;
1551                         printf("Configured with single SA index %u\n",
1552                                         single_sa_idx);
1553                         break;
1554                 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1555                         ret = parse_portmask(optarg);
1556                         if (ret == -1) {
1557                                 printf("Invalid argument[portmask]\n");
1558                                 print_usage(prgname);
1559                                 return -1;
1560                         }
1561
1562                         /* else */
1563                         enabled_cryptodev_mask = ret;
1564                         break;
1565                 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1566                         ret = parse_mask(optarg, &dev_rx_offload);
1567                         if (ret != 0) {
1568                                 printf("Invalid argument for \'%s\': %s\n",
1569                                         CMD_LINE_OPT_RX_OFFLOAD, optarg);
1570                                 print_usage(prgname);
1571                                 return -1;
1572                         }
1573                         break;
1574                 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1575                         ret = parse_mask(optarg, &dev_tx_offload);
1576                         if (ret != 0) {
1577                                 printf("Invalid argument for \'%s\': %s\n",
1578                                         CMD_LINE_OPT_TX_OFFLOAD, optarg);
1579                                 print_usage(prgname);
1580                                 return -1;
1581                         }
1582                         break;
1583                 case CMD_LINE_OPT_REASSEMBLE_NUM:
1584                         ret = parse_decimal(optarg);
1585                         if (ret < 0 || ret > UINT32_MAX) {
1586                                 printf("Invalid argument for \'%s\': %s\n",
1587                                         CMD_LINE_OPT_REASSEMBLE, optarg);
1588                                 print_usage(prgname);
1589                                 return -1;
1590                         }
1591                         frag_tbl_sz = ret;
1592                         break;
1593                 case CMD_LINE_OPT_MTU_NUM:
1594                         ret = parse_decimal(optarg);
1595                         if (ret < 0 || ret > RTE_IPV4_MAX_PKT_LEN) {
1596                                 printf("Invalid argument for \'%s\': %s\n",
1597                                         CMD_LINE_OPT_MTU, optarg);
1598                                 print_usage(prgname);
1599                                 return -1;
1600                         }
1601                         mtu_size = ret;
1602                         break;
1603                 case CMD_LINE_OPT_FRAG_TTL_NUM:
1604                         ret = parse_decimal(optarg);
1605                         if (ret < 0 || ret > MAX_FRAG_TTL_NS) {
1606                                 printf("Invalid argument for \'%s\': %s\n",
1607                                         CMD_LINE_OPT_MTU, optarg);
1608                                 print_usage(prgname);
1609                                 return -1;
1610                         }
1611                         frag_ttl_ns = ret;
1612                         break;
1613                 default:
1614                         print_usage(prgname);
1615                         return -1;
1616                 }
1617         }
1618
1619         if (f_present == 0) {
1620                 printf("Mandatory option \"-f\" not present\n");
1621                 return -1;
1622         }
1623
1624         /* check do we need to enable multi-seg support */
1625         if (multi_seg_required()) {
1626                 /* legacy mode doesn't support multi-seg */
1627                 app_sa_prm.enable = 1;
1628                 printf("frame buf size: %u, mtu: %u, "
1629                         "number of reassemble entries: %u\n"
1630                         "multi-segment support is required\n",
1631                         frame_buf_size, mtu_size, frag_tbl_sz);
1632         }
1633
1634         print_app_sa_prm(&app_sa_prm);
1635
1636         if (optind >= 0)
1637                 argv[optind-1] = prgname;
1638
1639         ret = optind-1;
1640         optind = 1; /* reset getopt lib */
1641         return ret;
1642 }
1643
1644 static void
1645 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1646 {
1647         char buf[RTE_ETHER_ADDR_FMT_SIZE];
1648         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1649         printf("%s%s", name, buf);
1650 }
1651
1652 /*
1653  * Update destination ethaddr for the port.
1654  */
1655 int
1656 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr)
1657 {
1658         if (port >= RTE_DIM(ethaddr_tbl))
1659                 return -EINVAL;
1660
1661         ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1662         return 0;
1663 }
1664
1665 /* Check the link status of all ports in up to 9s, and print them finally */
1666 static void
1667 check_all_ports_link_status(uint32_t port_mask)
1668 {
1669 #define CHECK_INTERVAL 100 /* 100ms */
1670 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1671         uint16_t portid;
1672         uint8_t count, all_ports_up, print_flag = 0;
1673         struct rte_eth_link link;
1674         int ret;
1675
1676         printf("\nChecking link status");
1677         fflush(stdout);
1678         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1679                 all_ports_up = 1;
1680                 RTE_ETH_FOREACH_DEV(portid) {
1681                         if ((port_mask & (1 << portid)) == 0)
1682                                 continue;
1683                         memset(&link, 0, sizeof(link));
1684                         ret = rte_eth_link_get_nowait(portid, &link);
1685                         if (ret < 0) {
1686                                 all_ports_up = 0;
1687                                 if (print_flag == 1)
1688                                         printf("Port %u link get failed: %s\n",
1689                                                 portid, rte_strerror(-ret));
1690                                 continue;
1691                         }
1692                         /* print link status if flag set */
1693                         if (print_flag == 1) {
1694                                 if (link.link_status)
1695                                         printf(
1696                                         "Port%d Link Up - speed %u Mbps -%s\n",
1697                                                 portid, link.link_speed,
1698                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1699                                         ("full-duplex") : ("half-duplex\n"));
1700                                 else
1701                                         printf("Port %d Link Down\n", portid);
1702                                 continue;
1703                         }
1704                         /* clear all_ports_up flag if any link down */
1705                         if (link.link_status == ETH_LINK_DOWN) {
1706                                 all_ports_up = 0;
1707                                 break;
1708                         }
1709                 }
1710                 /* after finally printing all link status, get out */
1711                 if (print_flag == 1)
1712                         break;
1713
1714                 if (all_ports_up == 0) {
1715                         printf(".");
1716                         fflush(stdout);
1717                         rte_delay_ms(CHECK_INTERVAL);
1718                 }
1719
1720                 /* set the print_flag if all ports up or timeout */
1721                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1722                         print_flag = 1;
1723                         printf("done\n");
1724                 }
1725         }
1726 }
1727
1728 static int32_t
1729 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1730                 uint16_t qp, struct lcore_params *params,
1731                 struct ipsec_ctx *ipsec_ctx,
1732                 const struct rte_cryptodev_capabilities *cipher,
1733                 const struct rte_cryptodev_capabilities *auth,
1734                 const struct rte_cryptodev_capabilities *aead)
1735 {
1736         int32_t ret = 0;
1737         unsigned long i;
1738         struct cdev_key key = { 0 };
1739
1740         key.lcore_id = params->lcore_id;
1741         if (cipher)
1742                 key.cipher_algo = cipher->sym.cipher.algo;
1743         if (auth)
1744                 key.auth_algo = auth->sym.auth.algo;
1745         if (aead)
1746                 key.aead_algo = aead->sym.aead.algo;
1747
1748         ret = rte_hash_lookup(map, &key);
1749         if (ret != -ENOENT)
1750                 return 0;
1751
1752         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1753                 if (ipsec_ctx->tbl[i].id == cdev_id)
1754                         break;
1755
1756         if (i == ipsec_ctx->nb_qps) {
1757                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1758                         printf("Maximum number of crypto devices assigned to "
1759                                 "a core, increase MAX_QP_PER_LCORE value\n");
1760                         return 0;
1761                 }
1762                 ipsec_ctx->tbl[i].id = cdev_id;
1763                 ipsec_ctx->tbl[i].qp = qp;
1764                 ipsec_ctx->nb_qps++;
1765                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1766                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1767                                 cdev_id, qp, i);
1768         }
1769
1770         ret = rte_hash_add_key_data(map, &key, (void *)i);
1771         if (ret < 0) {
1772                 printf("Faled to insert cdev mapping for (lcore %u, "
1773                                 "cdev %u, qp %u), errno %d\n",
1774                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1775                                 ipsec_ctx->tbl[i].qp, ret);
1776                 return 0;
1777         }
1778
1779         return 1;
1780 }
1781
1782 static int32_t
1783 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1784                 uint16_t qp, struct lcore_params *params)
1785 {
1786         int32_t ret = 0;
1787         const struct rte_cryptodev_capabilities *i, *j;
1788         struct rte_hash *map;
1789         struct lcore_conf *qconf;
1790         struct ipsec_ctx *ipsec_ctx;
1791         const char *str;
1792
1793         qconf = &lcore_conf[params->lcore_id];
1794
1795         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1796                 map = cdev_map_out;
1797                 ipsec_ctx = &qconf->outbound;
1798                 str = "Outbound";
1799         } else {
1800                 map = cdev_map_in;
1801                 ipsec_ctx = &qconf->inbound;
1802                 str = "Inbound";
1803         }
1804
1805         /* Required cryptodevs with operation chainning */
1806         if (!(dev_info->feature_flags &
1807                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1808                 return ret;
1809
1810         for (i = dev_info->capabilities;
1811                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1812                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1813                         continue;
1814
1815                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1816                         ret |= add_mapping(map, str, cdev_id, qp, params,
1817                                         ipsec_ctx, NULL, NULL, i);
1818                         continue;
1819                 }
1820
1821                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1822                         continue;
1823
1824                 for (j = dev_info->capabilities;
1825                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1826                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1827                                 continue;
1828
1829                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1830                                 continue;
1831
1832                         ret |= add_mapping(map, str, cdev_id, qp, params,
1833                                                 ipsec_ctx, i, j, NULL);
1834                 }
1835         }
1836
1837         return ret;
1838 }
1839
1840 /* Check if the device is enabled by cryptodev_mask */
1841 static int
1842 check_cryptodev_mask(uint8_t cdev_id)
1843 {
1844         if (enabled_cryptodev_mask & (1 << cdev_id))
1845                 return 0;
1846
1847         return -1;
1848 }
1849
1850 static int32_t
1851 cryptodevs_init(void)
1852 {
1853         struct rte_cryptodev_config dev_conf;
1854         struct rte_cryptodev_qp_conf qp_conf;
1855         uint16_t idx, max_nb_qps, qp, i;
1856         int16_t cdev_id;
1857         struct rte_hash_parameters params = { 0 };
1858
1859         const uint64_t mseg_flag = multi_seg_required() ?
1860                                 RTE_CRYPTODEV_FF_IN_PLACE_SGL : 0;
1861
1862         params.entries = CDEV_MAP_ENTRIES;
1863         params.key_len = sizeof(struct cdev_key);
1864         params.hash_func = rte_jhash;
1865         params.hash_func_init_val = 0;
1866         params.socket_id = rte_socket_id();
1867
1868         params.name = "cdev_map_in";
1869         cdev_map_in = rte_hash_create(&params);
1870         if (cdev_map_in == NULL)
1871                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1872                                 rte_errno);
1873
1874         params.name = "cdev_map_out";
1875         cdev_map_out = rte_hash_create(&params);
1876         if (cdev_map_out == NULL)
1877                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1878                                 rte_errno);
1879
1880         printf("lcore/cryptodev/qp mappings:\n");
1881
1882         idx = 0;
1883         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1884                 struct rte_cryptodev_info cdev_info;
1885
1886                 if (check_cryptodev_mask((uint8_t)cdev_id))
1887                         continue;
1888
1889                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1890
1891                 if ((mseg_flag & cdev_info.feature_flags) != mseg_flag)
1892                         rte_exit(EXIT_FAILURE,
1893                                 "Device %hd does not support \'%s\' feature\n",
1894                                 cdev_id,
1895                                 rte_cryptodev_get_feature_name(mseg_flag));
1896
1897                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1898                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1899                 else
1900                         max_nb_qps = nb_lcore_params;
1901
1902                 qp = 0;
1903                 i = 0;
1904                 while (qp < max_nb_qps && i < nb_lcore_params) {
1905                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1906                                                 &lcore_params[idx]))
1907                                 qp++;
1908                         idx++;
1909                         idx = idx % nb_lcore_params;
1910                         i++;
1911                 }
1912
1913                 if (qp == 0)
1914                         continue;
1915
1916                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1917                 dev_conf.nb_queue_pairs = qp;
1918                 dev_conf.ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
1919
1920                 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1921                 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1922                         rte_exit(EXIT_FAILURE,
1923                                 "Device does not support at least %u "
1924                                 "sessions", CDEV_MP_NB_OBJS);
1925
1926                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1927                         rte_panic("Failed to initialize cryptodev %u\n",
1928                                         cdev_id);
1929
1930                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1931                 qp_conf.mp_session =
1932                         socket_ctx[dev_conf.socket_id].session_pool;
1933                 qp_conf.mp_session_private =
1934                         socket_ctx[dev_conf.socket_id].session_priv_pool;
1935                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1936                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1937                                         &qp_conf, dev_conf.socket_id))
1938                                 rte_panic("Failed to setup queue %u for "
1939                                                 "cdev_id %u\n", 0, cdev_id);
1940
1941                 if (rte_cryptodev_start(cdev_id))
1942                         rte_panic("Failed to start cryptodev %u\n",
1943                                         cdev_id);
1944         }
1945
1946         printf("\n");
1947
1948         return 0;
1949 }
1950
1951 static void
1952 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
1953 {
1954         uint32_t frame_size;
1955         struct rte_eth_dev_info dev_info;
1956         struct rte_eth_txconf *txconf;
1957         uint16_t nb_tx_queue, nb_rx_queue;
1958         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1959         int32_t ret, socket_id;
1960         struct lcore_conf *qconf;
1961         struct rte_ether_addr ethaddr;
1962         struct rte_eth_conf local_port_conf = port_conf;
1963
1964         ret = rte_eth_dev_info_get(portid, &dev_info);
1965         if (ret != 0)
1966                 rte_exit(EXIT_FAILURE,
1967                         "Error during getting device (port %u) info: %s\n",
1968                         portid, strerror(-ret));
1969
1970         /* limit allowed HW offloafs, as user requested */
1971         dev_info.rx_offload_capa &= dev_rx_offload;
1972         dev_info.tx_offload_capa &= dev_tx_offload;
1973
1974         printf("Configuring device port %u:\n", portid);
1975
1976         ret = rte_eth_macaddr_get(portid, &ethaddr);
1977         if (ret != 0)
1978                 rte_exit(EXIT_FAILURE,
1979                         "Error getting MAC address (port %u): %s\n",
1980                         portid, rte_strerror(-ret));
1981
1982         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(&ethaddr);
1983         print_ethaddr("Address: ", &ethaddr);
1984         printf("\n");
1985
1986         nb_rx_queue = get_port_nb_rx_queues(portid);
1987         nb_tx_queue = nb_lcores;
1988
1989         if (nb_rx_queue > dev_info.max_rx_queues)
1990                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1991                                 "(max rx queue is %u)\n",
1992                                 nb_rx_queue, dev_info.max_rx_queues);
1993
1994         if (nb_tx_queue > dev_info.max_tx_queues)
1995                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1996                                 "(max tx queue is %u)\n",
1997                                 nb_tx_queue, dev_info.max_tx_queues);
1998
1999         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
2000                         nb_rx_queue, nb_tx_queue);
2001
2002         frame_size = MTU_TO_FRAMELEN(mtu_size);
2003         if (frame_size > local_port_conf.rxmode.max_rx_pkt_len)
2004                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
2005         local_port_conf.rxmode.max_rx_pkt_len = frame_size;
2006
2007         if (multi_seg_required()) {
2008                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
2009                 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
2010         }
2011
2012         local_port_conf.rxmode.offloads |= req_rx_offloads;
2013         local_port_conf.txmode.offloads |= req_tx_offloads;
2014
2015         /* Check that all required capabilities are supported */
2016         if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
2017                         local_port_conf.rxmode.offloads)
2018                 rte_exit(EXIT_FAILURE,
2019                         "Error: port %u required RX offloads: 0x%" PRIx64
2020                         ", avaialbe RX offloads: 0x%" PRIx64 "\n",
2021                         portid, local_port_conf.rxmode.offloads,
2022                         dev_info.rx_offload_capa);
2023
2024         if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
2025                         local_port_conf.txmode.offloads)
2026                 rte_exit(EXIT_FAILURE,
2027                         "Error: port %u required TX offloads: 0x%" PRIx64
2028                         ", avaialbe TX offloads: 0x%" PRIx64 "\n",
2029                         portid, local_port_conf.txmode.offloads,
2030                         dev_info.tx_offload_capa);
2031
2032         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2033                 local_port_conf.txmode.offloads |=
2034                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2035
2036         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
2037                 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
2038
2039         printf("port %u configurng rx_offloads=0x%" PRIx64
2040                 ", tx_offloads=0x%" PRIx64 "\n",
2041                 portid, local_port_conf.rxmode.offloads,
2042                 local_port_conf.txmode.offloads);
2043
2044         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2045                 dev_info.flow_type_rss_offloads;
2046         if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2047                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
2048                 printf("Port %u modified RSS hash function based on hardware support,"
2049                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2050                         portid,
2051                         port_conf.rx_adv_conf.rss_conf.rss_hf,
2052                         local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2053         }
2054
2055         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
2056                         &local_port_conf);
2057         if (ret < 0)
2058                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2059                                 "err=%d, port=%d\n", ret, portid);
2060
2061         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
2062         if (ret < 0)
2063                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
2064                                 "err=%d, port=%d\n", ret, portid);
2065
2066         /* init one TX queue per lcore */
2067         tx_queueid = 0;
2068         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2069                 if (rte_lcore_is_enabled(lcore_id) == 0)
2070                         continue;
2071
2072                 if (numa_on)
2073                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2074                 else
2075                         socket_id = 0;
2076
2077                 /* init TX queue */
2078                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
2079
2080                 txconf = &dev_info.default_txconf;
2081                 txconf->offloads = local_port_conf.txmode.offloads;
2082
2083                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
2084                                 socket_id, txconf);
2085                 if (ret < 0)
2086                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
2087                                         "err=%d, port=%d\n", ret, portid);
2088
2089                 qconf = &lcore_conf[lcore_id];
2090                 qconf->tx_queue_id[portid] = tx_queueid;
2091
2092                 /* Pre-populate pkt offloads based on capabilities */
2093                 qconf->outbound.ipv4_offloads = PKT_TX_IPV4;
2094                 qconf->outbound.ipv6_offloads = PKT_TX_IPV6;
2095                 if (local_port_conf.txmode.offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
2096                         qconf->outbound.ipv4_offloads |= PKT_TX_IP_CKSUM;
2097
2098                 tx_queueid++;
2099
2100                 /* init RX queues */
2101                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
2102                         struct rte_eth_rxconf rxq_conf;
2103
2104                         if (portid != qconf->rx_queue_list[queue].port_id)
2105                                 continue;
2106
2107                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
2108
2109                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
2110                                         socket_id);
2111
2112                         rxq_conf = dev_info.default_rxconf;
2113                         rxq_conf.offloads = local_port_conf.rxmode.offloads;
2114                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
2115                                         nb_rxd, socket_id, &rxq_conf,
2116                                         socket_ctx[socket_id].mbuf_pool);
2117                         if (ret < 0)
2118                                 rte_exit(EXIT_FAILURE,
2119                                         "rte_eth_rx_queue_setup: err=%d, "
2120                                         "port=%d\n", ret, portid);
2121                 }
2122         }
2123         printf("\n");
2124 }
2125
2126 static size_t
2127 max_session_size(void)
2128 {
2129         size_t max_sz, sz;
2130         void *sec_ctx;
2131         int16_t cdev_id, port_id, n;
2132
2133         max_sz = 0;
2134         n =  rte_cryptodev_count();
2135         for (cdev_id = 0; cdev_id != n; cdev_id++) {
2136                 sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2137                 if (sz > max_sz)
2138                         max_sz = sz;
2139                 /*
2140                  * If crypto device is security capable, need to check the
2141                  * size of security session as well.
2142                  */
2143
2144                 /* Get security context of the crypto device */
2145                 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
2146                 if (sec_ctx == NULL)
2147                         continue;
2148
2149                 /* Get size of security session */
2150                 sz = rte_security_session_get_size(sec_ctx);
2151                 if (sz > max_sz)
2152                         max_sz = sz;
2153         }
2154
2155         RTE_ETH_FOREACH_DEV(port_id) {
2156                 if ((enabled_port_mask & (1 << port_id)) == 0)
2157                         continue;
2158
2159                 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
2160                 if (sec_ctx == NULL)
2161                         continue;
2162
2163                 sz = rte_security_session_get_size(sec_ctx);
2164                 if (sz > max_sz)
2165                         max_sz = sz;
2166         }
2167
2168         return max_sz;
2169 }
2170
2171 static void
2172 session_pool_init(struct socket_ctx *ctx, int32_t socket_id, size_t sess_sz)
2173 {
2174         char mp_name[RTE_MEMPOOL_NAMESIZE];
2175         struct rte_mempool *sess_mp;
2176
2177         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2178                         "sess_mp_%u", socket_id);
2179         sess_mp = rte_cryptodev_sym_session_pool_create(
2180                         mp_name, CDEV_MP_NB_OBJS,
2181                         sess_sz, CDEV_MP_CACHE_SZ, 0,
2182                         socket_id);
2183         ctx->session_pool = sess_mp;
2184
2185         if (ctx->session_pool == NULL)
2186                 rte_exit(EXIT_FAILURE,
2187                         "Cannot init session pool on socket %d\n", socket_id);
2188         else
2189                 printf("Allocated session pool on socket %d\n", socket_id);
2190 }
2191
2192 static void
2193 session_priv_pool_init(struct socket_ctx *ctx, int32_t socket_id,
2194         size_t sess_sz)
2195 {
2196         char mp_name[RTE_MEMPOOL_NAMESIZE];
2197         struct rte_mempool *sess_mp;
2198
2199         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2200                         "sess_mp_priv_%u", socket_id);
2201         sess_mp = rte_mempool_create(mp_name,
2202                         CDEV_MP_NB_OBJS,
2203                         sess_sz,
2204                         CDEV_MP_CACHE_SZ,
2205                         0, NULL, NULL, NULL,
2206                         NULL, socket_id,
2207                         0);
2208         ctx->session_priv_pool = sess_mp;
2209
2210         if (ctx->session_priv_pool == NULL)
2211                 rte_exit(EXIT_FAILURE,
2212                         "Cannot init session priv pool on socket %d\n",
2213                         socket_id);
2214         else
2215                 printf("Allocated session priv pool on socket %d\n",
2216                         socket_id);
2217 }
2218
2219 static void
2220 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
2221 {
2222         char s[64];
2223         int32_t ms;
2224
2225         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
2226         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
2227                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
2228                         frame_buf_size, socket_id);
2229
2230         /*
2231          * if multi-segment support is enabled, then create a pool
2232          * for indirect mbufs.
2233          */
2234         ms = multi_seg_required();
2235         if (ms != 0) {
2236                 snprintf(s, sizeof(s), "mbuf_pool_indir_%d", socket_id);
2237                 ctx->mbuf_pool_indir = rte_pktmbuf_pool_create(s, nb_mbuf,
2238                         MEMPOOL_CACHE_SIZE, 0, 0, socket_id);
2239         }
2240
2241         if (ctx->mbuf_pool == NULL || (ms != 0 && ctx->mbuf_pool_indir == NULL))
2242                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
2243                                 socket_id);
2244         else
2245                 printf("Allocated mbuf pool on socket %d\n", socket_id);
2246 }
2247
2248 static inline int
2249 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
2250 {
2251         struct ipsec_sa *sa;
2252
2253         /* For inline protocol processing, the metadata in the event will
2254          * uniquely identify the security session which raised the event.
2255          * Application would then need the userdata it had registered with the
2256          * security session to process the event.
2257          */
2258
2259         sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
2260
2261         if (sa == NULL) {
2262                 /* userdata could not be retrieved */
2263                 return -1;
2264         }
2265
2266         /* Sequence number over flow. SA need to be re-established */
2267         RTE_SET_USED(sa);
2268         return 0;
2269 }
2270
2271 static int
2272 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2273                  void *param, void *ret_param)
2274 {
2275         uint64_t md;
2276         struct rte_eth_event_ipsec_desc *event_desc = NULL;
2277         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
2278                                         rte_eth_dev_get_sec_ctx(port_id);
2279
2280         RTE_SET_USED(param);
2281
2282         if (type != RTE_ETH_EVENT_IPSEC)
2283                 return -1;
2284
2285         event_desc = ret_param;
2286         if (event_desc == NULL) {
2287                 printf("Event descriptor not set\n");
2288                 return -1;
2289         }
2290
2291         md = event_desc->metadata;
2292
2293         if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
2294                 return inline_ipsec_event_esn_overflow(ctx, md);
2295         else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
2296                 printf("Invalid IPsec event reported\n");
2297                 return -1;
2298         }
2299
2300         return -1;
2301 }
2302
2303 static uint16_t
2304 rx_callback(__rte_unused uint16_t port, __rte_unused uint16_t queue,
2305         struct rte_mbuf *pkt[], uint16_t nb_pkts,
2306         __rte_unused uint16_t max_pkts, void *user_param)
2307 {
2308         uint64_t tm;
2309         uint32_t i, k;
2310         struct lcore_conf *lc;
2311         struct rte_mbuf *mb;
2312         struct rte_ether_hdr *eth;
2313
2314         lc = user_param;
2315         k = 0;
2316         tm = 0;
2317
2318         for (i = 0; i != nb_pkts; i++) {
2319
2320                 mb = pkt[i];
2321                 eth = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
2322                 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
2323
2324                         struct rte_ipv4_hdr *iph;
2325
2326                         iph = (struct rte_ipv4_hdr *)(eth + 1);
2327                         if (rte_ipv4_frag_pkt_is_fragmented(iph)) {
2328
2329                                 mb->l2_len = sizeof(*eth);
2330                                 mb->l3_len = sizeof(*iph);
2331                                 tm = (tm != 0) ? tm : rte_rdtsc();
2332                                 mb = rte_ipv4_frag_reassemble_packet(
2333                                         lc->frag.tbl, &lc->frag.dr,
2334                                         mb, tm, iph);
2335
2336                                 if (mb != NULL) {
2337                                         /* fix ip cksum after reassemble. */
2338                                         iph = rte_pktmbuf_mtod_offset(mb,
2339                                                 struct rte_ipv4_hdr *,
2340                                                 mb->l2_len);
2341                                         iph->hdr_checksum = 0;
2342                                         iph->hdr_checksum = rte_ipv4_cksum(iph);
2343                                 }
2344                         }
2345                 } else if (eth->ether_type ==
2346                                 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
2347
2348                         struct rte_ipv6_hdr *iph;
2349                         struct ipv6_extension_fragment *fh;
2350
2351                         iph = (struct rte_ipv6_hdr *)(eth + 1);
2352                         fh = rte_ipv6_frag_get_ipv6_fragment_header(iph);
2353                         if (fh != NULL) {
2354                                 mb->l2_len = sizeof(*eth);
2355                                 mb->l3_len = (uintptr_t)fh - (uintptr_t)iph +
2356                                         sizeof(*fh);
2357                                 tm = (tm != 0) ? tm : rte_rdtsc();
2358                                 mb = rte_ipv6_frag_reassemble_packet(
2359                                         lc->frag.tbl, &lc->frag.dr,
2360                                         mb, tm, iph, fh);
2361                                 if (mb != NULL)
2362                                         /* fix l3_len after reassemble. */
2363                                         mb->l3_len = mb->l3_len - sizeof(*fh);
2364                         }
2365                 }
2366
2367                 pkt[k] = mb;
2368                 k += (mb != NULL);
2369         }
2370
2371         /* some fragments were encountered, drain death row */
2372         if (tm != 0)
2373                 rte_ip_frag_free_death_row(&lc->frag.dr, 0);
2374
2375         return k;
2376 }
2377
2378
2379 static int
2380 reassemble_lcore_init(struct lcore_conf *lc, uint32_t cid)
2381 {
2382         int32_t sid;
2383         uint32_t i;
2384         uint64_t frag_cycles;
2385         const struct lcore_rx_queue *rxq;
2386         const struct rte_eth_rxtx_callback *cb;
2387
2388         /* create fragment table */
2389         sid = rte_lcore_to_socket_id(cid);
2390         frag_cycles = (rte_get_tsc_hz() + NS_PER_S - 1) /
2391                 NS_PER_S * frag_ttl_ns;
2392
2393         lc->frag.tbl = rte_ip_frag_table_create(frag_tbl_sz,
2394                 FRAG_TBL_BUCKET_ENTRIES, frag_tbl_sz, frag_cycles, sid);
2395         if (lc->frag.tbl == NULL) {
2396                 printf("%s(%u): failed to create fragment table of size: %u, "
2397                         "error code: %d\n",
2398                         __func__, cid, frag_tbl_sz, rte_errno);
2399                 return -ENOMEM;
2400         }
2401
2402         /* setup reassemble RX callbacks for all queues */
2403         for (i = 0; i != lc->nb_rx_queue; i++) {
2404
2405                 rxq = lc->rx_queue_list + i;
2406                 cb = rte_eth_add_rx_callback(rxq->port_id, rxq->queue_id,
2407                         rx_callback, lc);
2408                 if (cb == NULL) {
2409                         printf("%s(%u): failed to install RX callback for "
2410                                 "portid=%u, queueid=%u, error code: %d\n",
2411                                 __func__, cid,
2412                                 rxq->port_id, rxq->queue_id, rte_errno);
2413                         return -ENOMEM;
2414                 }
2415         }
2416
2417         return 0;
2418 }
2419
2420 static int
2421 reassemble_init(void)
2422 {
2423         int32_t rc;
2424         uint32_t i, lc;
2425
2426         rc = 0;
2427         for (i = 0; i != nb_lcore_params; i++) {
2428                 lc = lcore_params[i].lcore_id;
2429                 rc = reassemble_lcore_init(lcore_conf + lc, lc);
2430                 if (rc != 0)
2431                         break;
2432         }
2433
2434         return rc;
2435 }
2436
2437 static void
2438 create_default_ipsec_flow(uint16_t port_id, uint64_t rx_offloads)
2439 {
2440         struct rte_flow_action action[2];
2441         struct rte_flow_item pattern[2];
2442         struct rte_flow_attr attr = {0};
2443         struct rte_flow_error err;
2444         struct rte_flow *flow;
2445         int ret;
2446
2447         if (!(rx_offloads & DEV_RX_OFFLOAD_SECURITY))
2448                 return;
2449
2450         /* Add the default rte_flow to enable SECURITY for all ESP packets */
2451
2452         pattern[0].type = RTE_FLOW_ITEM_TYPE_ESP;
2453         pattern[0].spec = NULL;
2454         pattern[0].mask = NULL;
2455         pattern[0].last = NULL;
2456         pattern[1].type = RTE_FLOW_ITEM_TYPE_END;
2457
2458         action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
2459         action[0].conf = NULL;
2460         action[1].type = RTE_FLOW_ACTION_TYPE_END;
2461         action[1].conf = NULL;
2462
2463         attr.ingress = 1;
2464
2465         ret = rte_flow_validate(port_id, &attr, pattern, action, &err);
2466         if (ret)
2467                 return;
2468
2469         flow = rte_flow_create(port_id, &attr, pattern, action, &err);
2470         if (flow == NULL)
2471                 return;
2472
2473         flow_info_tbl[port_id].rx_def_flow = flow;
2474         RTE_LOG(INFO, IPSEC,
2475                 "Created default flow enabling SECURITY for all ESP traffic on port %d\n",
2476                 port_id);
2477 }
2478
2479 int32_t
2480 main(int32_t argc, char **argv)
2481 {
2482         int32_t ret;
2483         uint32_t lcore_id;
2484         uint32_t i;
2485         uint8_t socket_id;
2486         uint16_t portid;
2487         uint64_t req_rx_offloads[RTE_MAX_ETHPORTS];
2488         uint64_t req_tx_offloads[RTE_MAX_ETHPORTS];
2489         size_t sess_sz;
2490
2491         /* init EAL */
2492         ret = rte_eal_init(argc, argv);
2493         if (ret < 0)
2494                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2495         argc -= ret;
2496         argv += ret;
2497
2498         /* parse application arguments (after the EAL ones) */
2499         ret = parse_args(argc, argv);
2500         if (ret < 0)
2501                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
2502
2503         /* parse configuration file */
2504         if (parse_cfg_file(cfgfile) < 0) {
2505                 printf("parsing file \"%s\" failed\n",
2506                         optarg);
2507                 print_usage(argv[0]);
2508                 return -1;
2509         }
2510
2511         if ((unprotected_port_mask & enabled_port_mask) !=
2512                         unprotected_port_mask)
2513                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2514                                 unprotected_port_mask);
2515
2516         if (check_params() < 0)
2517                 rte_exit(EXIT_FAILURE, "check_params failed\n");
2518
2519         ret = init_lcore_rx_queues();
2520         if (ret < 0)
2521                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2522
2523         nb_lcores = rte_lcore_count();
2524
2525         sess_sz = max_session_size();
2526
2527         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2528                 if (rte_lcore_is_enabled(lcore_id) == 0)
2529                         continue;
2530
2531                 if (numa_on)
2532                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2533                 else
2534                         socket_id = 0;
2535
2536                 /* mbuf_pool is initialised by the pool_init() function*/
2537                 if (socket_ctx[socket_id].mbuf_pool)
2538                         continue;
2539
2540                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
2541                 session_pool_init(&socket_ctx[socket_id], socket_id, sess_sz);
2542                 session_priv_pool_init(&socket_ctx[socket_id], socket_id,
2543                         sess_sz);
2544         }
2545
2546         RTE_ETH_FOREACH_DEV(portid) {
2547                 if ((enabled_port_mask & (1 << portid)) == 0)
2548                         continue;
2549
2550                 sa_check_offloads(portid, &req_rx_offloads[portid],
2551                                 &req_tx_offloads[portid]);
2552                 port_init(portid, req_rx_offloads[portid],
2553                                 req_tx_offloads[portid]);
2554         }
2555
2556         cryptodevs_init();
2557
2558         /* start ports */
2559         RTE_ETH_FOREACH_DEV(portid) {
2560                 if ((enabled_port_mask & (1 << portid)) == 0)
2561                         continue;
2562
2563                 /* Create flow before starting the device */
2564                 create_default_ipsec_flow(portid, req_rx_offloads[portid]);
2565
2566                 ret = rte_eth_dev_start(portid);
2567                 if (ret < 0)
2568                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
2569                                         "err=%d, port=%d\n", ret, portid);
2570                 /*
2571                  * If enabled, put device in promiscuous mode.
2572                  * This allows IO forwarding mode to forward packets
2573                  * to itself through 2 cross-connected  ports of the
2574                  * target machine.
2575                  */
2576                 if (promiscuous_on) {
2577                         ret = rte_eth_promiscuous_enable(portid);
2578                         if (ret != 0)
2579                                 rte_exit(EXIT_FAILURE,
2580                                         "rte_eth_promiscuous_enable: err=%s, port=%d\n",
2581                                         rte_strerror(-ret), portid);
2582                 }
2583
2584                 rte_eth_dev_callback_register(portid,
2585                         RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
2586         }
2587
2588         /* fragment reassemble is enabled */
2589         if (frag_tbl_sz != 0) {
2590                 ret = reassemble_init();
2591                 if (ret != 0)
2592                         rte_exit(EXIT_FAILURE, "failed at reassemble init");
2593         }
2594
2595         /* Replicate each context per socket */
2596         for (i = 0; i < NB_SOCKETS && i < rte_socket_count(); i++) {
2597                 socket_id = rte_socket_id_by_idx(i);
2598                 if ((socket_ctx[socket_id].mbuf_pool != NULL) &&
2599                         (socket_ctx[socket_id].sa_in == NULL) &&
2600                         (socket_ctx[socket_id].sa_out == NULL)) {
2601                         sa_init(&socket_ctx[socket_id], socket_id);
2602                         sp4_init(&socket_ctx[socket_id], socket_id);
2603                         sp6_init(&socket_ctx[socket_id], socket_id);
2604                         rt_init(&socket_ctx[socket_id], socket_id);
2605                 }
2606         }
2607
2608         check_all_ports_link_status(enabled_port_mask);
2609
2610         /* launch per-lcore init on every lcore */
2611         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2612         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2613                 if (rte_eal_wait_lcore(lcore_id) < 0)
2614                         return -1;
2615         }
2616
2617         return 0;
2618 }