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