examples/ipsec-secgw: add fragment TTL option
[dpdk.git] / examples / ipsec-secgw / ipsec-secgw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <netinet/in.h>
11 #include <netinet/ip.h>
12 #include <netinet/ip6.h>
13 #include <string.h>
14 #include <sys/queue.h>
15 #include <stdarg.h>
16 #include <errno.h>
17 #include <getopt.h>
18
19 #include <rte_common.h>
20 #include <rte_byteorder.h>
21 #include <rte_log.h>
22 #include <rte_eal.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_cycles.h>
26 #include <rte_prefetch.h>
27 #include <rte_lcore.h>
28 #include <rte_per_lcore.h>
29 #include <rte_branch_prediction.h>
30 #include <rte_interrupts.h>
31 #include <rte_random.h>
32 #include <rte_debug.h>
33 #include <rte_ether.h>
34 #include <rte_ethdev.h>
35 #include <rte_mempool.h>
36 #include <rte_mbuf.h>
37 #include <rte_acl.h>
38 #include <rte_lpm.h>
39 #include <rte_lpm6.h>
40 #include <rte_hash.h>
41 #include <rte_jhash.h>
42 #include <rte_cryptodev.h>
43 #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         if (prm->enable == 0)
1428                 return;
1429
1430         printf("replay window size: %u\n", prm->window_size);
1431         printf("ESN: %s\n", (prm->enable_esn == 0) ? "disabled" : "enabled");
1432         printf("SA flags: %#" PRIx64 "\n", prm->flags);
1433         printf("Frag TTL: %" PRIu64 " ns\n", frag_ttl_ns);
1434 }
1435
1436 static int32_t
1437 parse_args(int32_t argc, char **argv)
1438 {
1439         int opt;
1440         int64_t ret;
1441         char **argvopt;
1442         int32_t option_index;
1443         char *prgname = argv[0];
1444         int32_t f_present = 0;
1445
1446         argvopt = argv;
1447
1448         while ((opt = getopt_long(argc, argvopt, "aelp:Pu:f:j:w:",
1449                                 lgopts, &option_index)) != EOF) {
1450
1451                 switch (opt) {
1452                 case 'p':
1453                         enabled_port_mask = parse_portmask(optarg);
1454                         if (enabled_port_mask == 0) {
1455                                 printf("invalid portmask\n");
1456                                 print_usage(prgname);
1457                                 return -1;
1458                         }
1459                         break;
1460                 case 'P':
1461                         printf("Promiscuous mode selected\n");
1462                         promiscuous_on = 1;
1463                         break;
1464                 case 'u':
1465                         unprotected_port_mask = parse_portmask(optarg);
1466                         if (unprotected_port_mask == 0) {
1467                                 printf("invalid unprotected portmask\n");
1468                                 print_usage(prgname);
1469                                 return -1;
1470                         }
1471                         break;
1472                 case 'f':
1473                         if (f_present == 1) {
1474                                 printf("\"-f\" option present more than "
1475                                         "once!\n");
1476                                 print_usage(prgname);
1477                                 return -1;
1478                         }
1479                         cfgfile = optarg;
1480                         f_present = 1;
1481                         break;
1482                 case 'j':
1483                         ret = parse_decimal(optarg);
1484                         if (ret < RTE_MBUF_DEFAULT_BUF_SIZE ||
1485                                         ret > UINT16_MAX) {
1486                                 printf("Invalid frame buffer size value: %s\n",
1487                                         optarg);
1488                                 print_usage(prgname);
1489                                 return -1;
1490                         }
1491                         frame_buf_size = ret;
1492                         printf("Custom frame buffer size %u\n", frame_buf_size);
1493                         break;
1494                 case 'l':
1495                         app_sa_prm.enable = 1;
1496                         break;
1497                 case 'w':
1498                         app_sa_prm.enable = 1;
1499                         app_sa_prm.window_size = parse_decimal(optarg);
1500                         break;
1501                 case 'e':
1502                         app_sa_prm.enable = 1;
1503                         app_sa_prm.enable_esn = 1;
1504                         break;
1505                 case 'a':
1506                         app_sa_prm.enable = 1;
1507                         app_sa_prm.flags |= RTE_IPSEC_SAFLAG_SQN_ATOM;
1508                         break;
1509                 case CMD_LINE_OPT_CONFIG_NUM:
1510                         ret = parse_config(optarg);
1511                         if (ret) {
1512                                 printf("Invalid config\n");
1513                                 print_usage(prgname);
1514                                 return -1;
1515                         }
1516                         break;
1517                 case CMD_LINE_OPT_SINGLE_SA_NUM:
1518                         ret = parse_decimal(optarg);
1519                         if (ret == -1 || ret > UINT32_MAX) {
1520                                 printf("Invalid argument[sa_idx]\n");
1521                                 print_usage(prgname);
1522                                 return -1;
1523                         }
1524
1525                         /* else */
1526                         single_sa = 1;
1527                         single_sa_idx = ret;
1528                         printf("Configured with single SA index %u\n",
1529                                         single_sa_idx);
1530                         break;
1531                 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1532                         ret = parse_portmask(optarg);
1533                         if (ret == -1) {
1534                                 printf("Invalid argument[portmask]\n");
1535                                 print_usage(prgname);
1536                                 return -1;
1537                         }
1538
1539                         /* else */
1540                         enabled_cryptodev_mask = ret;
1541                         break;
1542                 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1543                         ret = parse_mask(optarg, &dev_rx_offload);
1544                         if (ret != 0) {
1545                                 printf("Invalid argument for \'%s\': %s\n",
1546                                         CMD_LINE_OPT_RX_OFFLOAD, optarg);
1547                                 print_usage(prgname);
1548                                 return -1;
1549                         }
1550                         break;
1551                 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1552                         ret = parse_mask(optarg, &dev_tx_offload);
1553                         if (ret != 0) {
1554                                 printf("Invalid argument for \'%s\': %s\n",
1555                                         CMD_LINE_OPT_TX_OFFLOAD, optarg);
1556                                 print_usage(prgname);
1557                                 return -1;
1558                         }
1559                         break;
1560                 case CMD_LINE_OPT_REASSEMBLE_NUM:
1561                         ret = parse_decimal(optarg);
1562                         if (ret < 0 || ret > UINT32_MAX) {
1563                                 printf("Invalid argument for \'%s\': %s\n",
1564                                         CMD_LINE_OPT_REASSEMBLE, optarg);
1565                                 print_usage(prgname);
1566                                 return -1;
1567                         }
1568                         frag_tbl_sz = ret;
1569                         break;
1570                 case CMD_LINE_OPT_MTU_NUM:
1571                         ret = parse_decimal(optarg);
1572                         if (ret < 0 || ret > RTE_IPV4_MAX_PKT_LEN) {
1573                                 printf("Invalid argument for \'%s\': %s\n",
1574                                         CMD_LINE_OPT_MTU, optarg);
1575                                 print_usage(prgname);
1576                                 return -1;
1577                         }
1578                         mtu_size = ret;
1579                         break;
1580                 case CMD_LINE_OPT_FRAG_TTL_NUM:
1581                         ret = parse_decimal(optarg);
1582                         if (ret < 0 || ret > MAX_FRAG_TTL_NS) {
1583                                 printf("Invalid argument for \'%s\': %s\n",
1584                                         CMD_LINE_OPT_MTU, optarg);
1585                                 print_usage(prgname);
1586                                 return -1;
1587                         }
1588                         frag_ttl_ns = ret;
1589                         break;
1590                 default:
1591                         print_usage(prgname);
1592                         return -1;
1593                 }
1594         }
1595
1596         if (f_present == 0) {
1597                 printf("Mandatory option \"-f\" not present\n");
1598                 return -1;
1599         }
1600
1601         /* check do we need to enable multi-seg support */
1602         if (multi_seg_required()) {
1603                 /* legacy mode doesn't support multi-seg */
1604                 app_sa_prm.enable = 1;
1605                 printf("frame buf size: %u, mtu: %u, "
1606                         "number of reassemble entries: %u\n"
1607                         "multi-segment support is required\n",
1608                         frame_buf_size, mtu_size, frag_tbl_sz);
1609         }
1610
1611         print_app_sa_prm(&app_sa_prm);
1612
1613         if (optind >= 0)
1614                 argv[optind-1] = prgname;
1615
1616         ret = optind-1;
1617         optind = 1; /* reset getopt lib */
1618         return ret;
1619 }
1620
1621 static void
1622 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1623 {
1624         char buf[RTE_ETHER_ADDR_FMT_SIZE];
1625         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1626         printf("%s%s", name, buf);
1627 }
1628
1629 /*
1630  * Update destination ethaddr for the port.
1631  */
1632 int
1633 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr)
1634 {
1635         if (port >= RTE_DIM(ethaddr_tbl))
1636                 return -EINVAL;
1637
1638         ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1639         return 0;
1640 }
1641
1642 /* Check the link status of all ports in up to 9s, and print them finally */
1643 static void
1644 check_all_ports_link_status(uint32_t port_mask)
1645 {
1646 #define CHECK_INTERVAL 100 /* 100ms */
1647 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1648         uint16_t portid;
1649         uint8_t count, all_ports_up, print_flag = 0;
1650         struct rte_eth_link link;
1651         int ret;
1652
1653         printf("\nChecking link status");
1654         fflush(stdout);
1655         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1656                 all_ports_up = 1;
1657                 RTE_ETH_FOREACH_DEV(portid) {
1658                         if ((port_mask & (1 << portid)) == 0)
1659                                 continue;
1660                         memset(&link, 0, sizeof(link));
1661                         ret = rte_eth_link_get_nowait(portid, &link);
1662                         if (ret < 0) {
1663                                 all_ports_up = 0;
1664                                 if (print_flag == 1)
1665                                         printf("Port %u link get failed: %s\n",
1666                                                 portid, rte_strerror(-ret));
1667                                 continue;
1668                         }
1669                         /* print link status if flag set */
1670                         if (print_flag == 1) {
1671                                 if (link.link_status)
1672                                         printf(
1673                                         "Port%d Link Up - speed %u Mbps -%s\n",
1674                                                 portid, link.link_speed,
1675                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1676                                         ("full-duplex") : ("half-duplex\n"));
1677                                 else
1678                                         printf("Port %d Link Down\n", portid);
1679                                 continue;
1680                         }
1681                         /* clear all_ports_up flag if any link down */
1682                         if (link.link_status == ETH_LINK_DOWN) {
1683                                 all_ports_up = 0;
1684                                 break;
1685                         }
1686                 }
1687                 /* after finally printing all link status, get out */
1688                 if (print_flag == 1)
1689                         break;
1690
1691                 if (all_ports_up == 0) {
1692                         printf(".");
1693                         fflush(stdout);
1694                         rte_delay_ms(CHECK_INTERVAL);
1695                 }
1696
1697                 /* set the print_flag if all ports up or timeout */
1698                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1699                         print_flag = 1;
1700                         printf("done\n");
1701                 }
1702         }
1703 }
1704
1705 static int32_t
1706 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1707                 uint16_t qp, struct lcore_params *params,
1708                 struct ipsec_ctx *ipsec_ctx,
1709                 const struct rte_cryptodev_capabilities *cipher,
1710                 const struct rte_cryptodev_capabilities *auth,
1711                 const struct rte_cryptodev_capabilities *aead)
1712 {
1713         int32_t ret = 0;
1714         unsigned long i;
1715         struct cdev_key key = { 0 };
1716
1717         key.lcore_id = params->lcore_id;
1718         if (cipher)
1719                 key.cipher_algo = cipher->sym.cipher.algo;
1720         if (auth)
1721                 key.auth_algo = auth->sym.auth.algo;
1722         if (aead)
1723                 key.aead_algo = aead->sym.aead.algo;
1724
1725         ret = rte_hash_lookup(map, &key);
1726         if (ret != -ENOENT)
1727                 return 0;
1728
1729         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1730                 if (ipsec_ctx->tbl[i].id == cdev_id)
1731                         break;
1732
1733         if (i == ipsec_ctx->nb_qps) {
1734                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1735                         printf("Maximum number of crypto devices assigned to "
1736                                 "a core, increase MAX_QP_PER_LCORE value\n");
1737                         return 0;
1738                 }
1739                 ipsec_ctx->tbl[i].id = cdev_id;
1740                 ipsec_ctx->tbl[i].qp = qp;
1741                 ipsec_ctx->nb_qps++;
1742                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1743                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1744                                 cdev_id, qp, i);
1745         }
1746
1747         ret = rte_hash_add_key_data(map, &key, (void *)i);
1748         if (ret < 0) {
1749                 printf("Faled to insert cdev mapping for (lcore %u, "
1750                                 "cdev %u, qp %u), errno %d\n",
1751                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1752                                 ipsec_ctx->tbl[i].qp, ret);
1753                 return 0;
1754         }
1755
1756         return 1;
1757 }
1758
1759 static int32_t
1760 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1761                 uint16_t qp, struct lcore_params *params)
1762 {
1763         int32_t ret = 0;
1764         const struct rte_cryptodev_capabilities *i, *j;
1765         struct rte_hash *map;
1766         struct lcore_conf *qconf;
1767         struct ipsec_ctx *ipsec_ctx;
1768         const char *str;
1769
1770         qconf = &lcore_conf[params->lcore_id];
1771
1772         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1773                 map = cdev_map_out;
1774                 ipsec_ctx = &qconf->outbound;
1775                 str = "Outbound";
1776         } else {
1777                 map = cdev_map_in;
1778                 ipsec_ctx = &qconf->inbound;
1779                 str = "Inbound";
1780         }
1781
1782         /* Required cryptodevs with operation chainning */
1783         if (!(dev_info->feature_flags &
1784                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1785                 return ret;
1786
1787         for (i = dev_info->capabilities;
1788                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1789                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1790                         continue;
1791
1792                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1793                         ret |= add_mapping(map, str, cdev_id, qp, params,
1794                                         ipsec_ctx, NULL, NULL, i);
1795                         continue;
1796                 }
1797
1798                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1799                         continue;
1800
1801                 for (j = dev_info->capabilities;
1802                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1803                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1804                                 continue;
1805
1806                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1807                                 continue;
1808
1809                         ret |= add_mapping(map, str, cdev_id, qp, params,
1810                                                 ipsec_ctx, i, j, NULL);
1811                 }
1812         }
1813
1814         return ret;
1815 }
1816
1817 /* Check if the device is enabled by cryptodev_mask */
1818 static int
1819 check_cryptodev_mask(uint8_t cdev_id)
1820 {
1821         if (enabled_cryptodev_mask & (1 << cdev_id))
1822                 return 0;
1823
1824         return -1;
1825 }
1826
1827 static int32_t
1828 cryptodevs_init(void)
1829 {
1830         struct rte_cryptodev_config dev_conf;
1831         struct rte_cryptodev_qp_conf qp_conf;
1832         uint16_t idx, max_nb_qps, qp, i;
1833         int16_t cdev_id;
1834         struct rte_hash_parameters params = { 0 };
1835
1836         const uint64_t mseg_flag = multi_seg_required() ?
1837                                 RTE_CRYPTODEV_FF_IN_PLACE_SGL : 0;
1838
1839         params.entries = CDEV_MAP_ENTRIES;
1840         params.key_len = sizeof(struct cdev_key);
1841         params.hash_func = rte_jhash;
1842         params.hash_func_init_val = 0;
1843         params.socket_id = rte_socket_id();
1844
1845         params.name = "cdev_map_in";
1846         cdev_map_in = rte_hash_create(&params);
1847         if (cdev_map_in == NULL)
1848                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1849                                 rte_errno);
1850
1851         params.name = "cdev_map_out";
1852         cdev_map_out = rte_hash_create(&params);
1853         if (cdev_map_out == NULL)
1854                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1855                                 rte_errno);
1856
1857         printf("lcore/cryptodev/qp mappings:\n");
1858
1859         idx = 0;
1860         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1861                 struct rte_cryptodev_info cdev_info;
1862
1863                 if (check_cryptodev_mask((uint8_t)cdev_id))
1864                         continue;
1865
1866                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1867
1868                 if ((mseg_flag & cdev_info.feature_flags) != mseg_flag)
1869                         rte_exit(EXIT_FAILURE,
1870                                 "Device %hd does not support \'%s\' feature\n",
1871                                 cdev_id,
1872                                 rte_cryptodev_get_feature_name(mseg_flag));
1873
1874                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1875                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1876                 else
1877                         max_nb_qps = nb_lcore_params;
1878
1879                 qp = 0;
1880                 i = 0;
1881                 while (qp < max_nb_qps && i < nb_lcore_params) {
1882                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1883                                                 &lcore_params[idx]))
1884                                 qp++;
1885                         idx++;
1886                         idx = idx % nb_lcore_params;
1887                         i++;
1888                 }
1889
1890                 if (qp == 0)
1891                         continue;
1892
1893                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1894                 dev_conf.nb_queue_pairs = qp;
1895                 dev_conf.ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
1896
1897                 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1898                 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1899                         rte_exit(EXIT_FAILURE,
1900                                 "Device does not support at least %u "
1901                                 "sessions", CDEV_MP_NB_OBJS);
1902
1903                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1904                         rte_panic("Failed to initialize cryptodev %u\n",
1905                                         cdev_id);
1906
1907                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1908                 qp_conf.mp_session =
1909                         socket_ctx[dev_conf.socket_id].session_pool;
1910                 qp_conf.mp_session_private =
1911                         socket_ctx[dev_conf.socket_id].session_priv_pool;
1912                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1913                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1914                                         &qp_conf, dev_conf.socket_id))
1915                                 rte_panic("Failed to setup queue %u for "
1916                                                 "cdev_id %u\n", 0, cdev_id);
1917
1918                 if (rte_cryptodev_start(cdev_id))
1919                         rte_panic("Failed to start cryptodev %u\n",
1920                                         cdev_id);
1921         }
1922
1923         printf("\n");
1924
1925         return 0;
1926 }
1927
1928 static void
1929 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
1930 {
1931         uint32_t frame_size;
1932         struct rte_eth_dev_info dev_info;
1933         struct rte_eth_txconf *txconf;
1934         uint16_t nb_tx_queue, nb_rx_queue;
1935         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1936         int32_t ret, socket_id;
1937         struct lcore_conf *qconf;
1938         struct rte_ether_addr ethaddr;
1939         struct rte_eth_conf local_port_conf = port_conf;
1940
1941         ret = rte_eth_dev_info_get(portid, &dev_info);
1942         if (ret != 0)
1943                 rte_exit(EXIT_FAILURE,
1944                         "Error during getting device (port %u) info: %s\n",
1945                         portid, strerror(-ret));
1946
1947         /* limit allowed HW offloafs, as user requested */
1948         dev_info.rx_offload_capa &= dev_rx_offload;
1949         dev_info.tx_offload_capa &= dev_tx_offload;
1950
1951         printf("Configuring device port %u:\n", portid);
1952
1953         ret = rte_eth_macaddr_get(portid, &ethaddr);
1954         if (ret != 0)
1955                 rte_exit(EXIT_FAILURE,
1956                         "Error getting MAC address (port %u): %s\n",
1957                         portid, rte_strerror(-ret));
1958
1959         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(&ethaddr);
1960         print_ethaddr("Address: ", &ethaddr);
1961         printf("\n");
1962
1963         nb_rx_queue = get_port_nb_rx_queues(portid);
1964         nb_tx_queue = nb_lcores;
1965
1966         if (nb_rx_queue > dev_info.max_rx_queues)
1967                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1968                                 "(max rx queue is %u)\n",
1969                                 nb_rx_queue, dev_info.max_rx_queues);
1970
1971         if (nb_tx_queue > dev_info.max_tx_queues)
1972                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1973                                 "(max tx queue is %u)\n",
1974                                 nb_tx_queue, dev_info.max_tx_queues);
1975
1976         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1977                         nb_rx_queue, nb_tx_queue);
1978
1979         frame_size = MTU_TO_FRAMELEN(mtu_size);
1980         if (frame_size > local_port_conf.rxmode.max_rx_pkt_len)
1981                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1982         local_port_conf.rxmode.max_rx_pkt_len = frame_size;
1983
1984         if (multi_seg_required()) {
1985                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SCATTER;
1986                 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
1987         }
1988
1989         local_port_conf.rxmode.offloads |= req_rx_offloads;
1990         local_port_conf.txmode.offloads |= req_tx_offloads;
1991
1992         /* Check that all required capabilities are supported */
1993         if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
1994                         local_port_conf.rxmode.offloads)
1995                 rte_exit(EXIT_FAILURE,
1996                         "Error: port %u required RX offloads: 0x%" PRIx64
1997                         ", avaialbe RX offloads: 0x%" PRIx64 "\n",
1998                         portid, local_port_conf.rxmode.offloads,
1999                         dev_info.rx_offload_capa);
2000
2001         if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
2002                         local_port_conf.txmode.offloads)
2003                 rte_exit(EXIT_FAILURE,
2004                         "Error: port %u required TX offloads: 0x%" PRIx64
2005                         ", avaialbe TX offloads: 0x%" PRIx64 "\n",
2006                         portid, local_port_conf.txmode.offloads,
2007                         dev_info.tx_offload_capa);
2008
2009         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
2010                 local_port_conf.txmode.offloads |=
2011                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
2012
2013         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_IPV4_CKSUM)
2014                 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_IPV4_CKSUM;
2015
2016         printf("port %u configurng rx_offloads=0x%" PRIx64
2017                 ", tx_offloads=0x%" PRIx64 "\n",
2018                 portid, local_port_conf.rxmode.offloads,
2019                 local_port_conf.txmode.offloads);
2020
2021         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
2022                 dev_info.flow_type_rss_offloads;
2023         if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
2024                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
2025                 printf("Port %u modified RSS hash function based on hardware support,"
2026                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
2027                         portid,
2028                         port_conf.rx_adv_conf.rss_conf.rss_hf,
2029                         local_port_conf.rx_adv_conf.rss_conf.rss_hf);
2030         }
2031
2032         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
2033                         &local_port_conf);
2034         if (ret < 0)
2035                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
2036                                 "err=%d, port=%d\n", ret, portid);
2037
2038         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
2039         if (ret < 0)
2040                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
2041                                 "err=%d, port=%d\n", ret, portid);
2042
2043         /* init one TX queue per lcore */
2044         tx_queueid = 0;
2045         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2046                 if (rte_lcore_is_enabled(lcore_id) == 0)
2047                         continue;
2048
2049                 if (numa_on)
2050                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2051                 else
2052                         socket_id = 0;
2053
2054                 /* init TX queue */
2055                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
2056
2057                 txconf = &dev_info.default_txconf;
2058                 txconf->offloads = local_port_conf.txmode.offloads;
2059
2060                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
2061                                 socket_id, txconf);
2062                 if (ret < 0)
2063                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
2064                                         "err=%d, port=%d\n", ret, portid);
2065
2066                 qconf = &lcore_conf[lcore_id];
2067                 qconf->tx_queue_id[portid] = tx_queueid;
2068
2069                 /* Pre-populate pkt offloads based on capabilities */
2070                 qconf->outbound.ipv4_offloads = PKT_TX_IPV4;
2071                 qconf->outbound.ipv6_offloads = PKT_TX_IPV6;
2072                 if (local_port_conf.txmode.offloads & DEV_TX_OFFLOAD_IPV4_CKSUM)
2073                         qconf->outbound.ipv4_offloads |= PKT_TX_IP_CKSUM;
2074
2075                 tx_queueid++;
2076
2077                 /* init RX queues */
2078                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
2079                         struct rte_eth_rxconf rxq_conf;
2080
2081                         if (portid != qconf->rx_queue_list[queue].port_id)
2082                                 continue;
2083
2084                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
2085
2086                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
2087                                         socket_id);
2088
2089                         rxq_conf = dev_info.default_rxconf;
2090                         rxq_conf.offloads = local_port_conf.rxmode.offloads;
2091                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
2092                                         nb_rxd, socket_id, &rxq_conf,
2093                                         socket_ctx[socket_id].mbuf_pool);
2094                         if (ret < 0)
2095                                 rte_exit(EXIT_FAILURE,
2096                                         "rte_eth_rx_queue_setup: err=%d, "
2097                                         "port=%d\n", ret, portid);
2098                 }
2099         }
2100         printf("\n");
2101 }
2102
2103 static size_t
2104 max_session_size(void)
2105 {
2106         size_t max_sz, sz;
2107         void *sec_ctx;
2108         int16_t cdev_id, port_id, n;
2109
2110         max_sz = 0;
2111         n =  rte_cryptodev_count();
2112         for (cdev_id = 0; cdev_id != n; cdev_id++) {
2113                 sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2114                 if (sz > max_sz)
2115                         max_sz = sz;
2116                 /*
2117                  * If crypto device is security capable, need to check the
2118                  * size of security session as well.
2119                  */
2120
2121                 /* Get security context of the crypto device */
2122                 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
2123                 if (sec_ctx == NULL)
2124                         continue;
2125
2126                 /* Get size of security session */
2127                 sz = rte_security_session_get_size(sec_ctx);
2128                 if (sz > max_sz)
2129                         max_sz = sz;
2130         }
2131
2132         RTE_ETH_FOREACH_DEV(port_id) {
2133                 if ((enabled_port_mask & (1 << port_id)) == 0)
2134                         continue;
2135
2136                 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
2137                 if (sec_ctx == NULL)
2138                         continue;
2139
2140                 sz = rte_security_session_get_size(sec_ctx);
2141                 if (sz > max_sz)
2142                         max_sz = sz;
2143         }
2144
2145         return max_sz;
2146 }
2147
2148 static void
2149 session_pool_init(struct socket_ctx *ctx, int32_t socket_id, size_t sess_sz)
2150 {
2151         char mp_name[RTE_MEMPOOL_NAMESIZE];
2152         struct rte_mempool *sess_mp;
2153
2154         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2155                         "sess_mp_%u", socket_id);
2156         sess_mp = rte_cryptodev_sym_session_pool_create(
2157                         mp_name, CDEV_MP_NB_OBJS,
2158                         sess_sz, CDEV_MP_CACHE_SZ, 0,
2159                         socket_id);
2160         ctx->session_pool = sess_mp;
2161
2162         if (ctx->session_pool == NULL)
2163                 rte_exit(EXIT_FAILURE,
2164                         "Cannot init session pool on socket %d\n", socket_id);
2165         else
2166                 printf("Allocated session pool on socket %d\n", socket_id);
2167 }
2168
2169 static void
2170 session_priv_pool_init(struct socket_ctx *ctx, int32_t socket_id,
2171         size_t sess_sz)
2172 {
2173         char mp_name[RTE_MEMPOOL_NAMESIZE];
2174         struct rte_mempool *sess_mp;
2175
2176         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2177                         "sess_mp_priv_%u", socket_id);
2178         sess_mp = rte_mempool_create(mp_name,
2179                         CDEV_MP_NB_OBJS,
2180                         sess_sz,
2181                         CDEV_MP_CACHE_SZ,
2182                         0, NULL, NULL, NULL,
2183                         NULL, socket_id,
2184                         0);
2185         ctx->session_priv_pool = sess_mp;
2186
2187         if (ctx->session_priv_pool == NULL)
2188                 rte_exit(EXIT_FAILURE,
2189                         "Cannot init session priv pool on socket %d\n",
2190                         socket_id);
2191         else
2192                 printf("Allocated session priv pool on socket %d\n",
2193                         socket_id);
2194 }
2195
2196 static void
2197 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
2198 {
2199         char s[64];
2200         int32_t ms;
2201
2202         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
2203         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
2204                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
2205                         frame_buf_size, socket_id);
2206
2207         /*
2208          * if multi-segment support is enabled, then create a pool
2209          * for indirect mbufs.
2210          */
2211         ms = multi_seg_required();
2212         if (ms != 0) {
2213                 snprintf(s, sizeof(s), "mbuf_pool_indir_%d", socket_id);
2214                 ctx->mbuf_pool_indir = rte_pktmbuf_pool_create(s, nb_mbuf,
2215                         MEMPOOL_CACHE_SIZE, 0, 0, socket_id);
2216         }
2217
2218         if (ctx->mbuf_pool == NULL || (ms != 0 && ctx->mbuf_pool_indir == NULL))
2219                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
2220                                 socket_id);
2221         else
2222                 printf("Allocated mbuf pool on socket %d\n", socket_id);
2223 }
2224
2225 static inline int
2226 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
2227 {
2228         struct ipsec_sa *sa;
2229
2230         /* For inline protocol processing, the metadata in the event will
2231          * uniquely identify the security session which raised the event.
2232          * Application would then need the userdata it had registered with the
2233          * security session to process the event.
2234          */
2235
2236         sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
2237
2238         if (sa == NULL) {
2239                 /* userdata could not be retrieved */
2240                 return -1;
2241         }
2242
2243         /* Sequence number over flow. SA need to be re-established */
2244         RTE_SET_USED(sa);
2245         return 0;
2246 }
2247
2248 static int
2249 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2250                  void *param, void *ret_param)
2251 {
2252         uint64_t md;
2253         struct rte_eth_event_ipsec_desc *event_desc = NULL;
2254         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
2255                                         rte_eth_dev_get_sec_ctx(port_id);
2256
2257         RTE_SET_USED(param);
2258
2259         if (type != RTE_ETH_EVENT_IPSEC)
2260                 return -1;
2261
2262         event_desc = ret_param;
2263         if (event_desc == NULL) {
2264                 printf("Event descriptor not set\n");
2265                 return -1;
2266         }
2267
2268         md = event_desc->metadata;
2269
2270         if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
2271                 return inline_ipsec_event_esn_overflow(ctx, md);
2272         else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
2273                 printf("Invalid IPsec event reported\n");
2274                 return -1;
2275         }
2276
2277         return -1;
2278 }
2279
2280 static uint16_t
2281 rx_callback(__rte_unused uint16_t port, __rte_unused uint16_t queue,
2282         struct rte_mbuf *pkt[], uint16_t nb_pkts,
2283         __rte_unused uint16_t max_pkts, void *user_param)
2284 {
2285         uint64_t tm;
2286         uint32_t i, k;
2287         struct lcore_conf *lc;
2288         struct rte_mbuf *mb;
2289         struct rte_ether_hdr *eth;
2290
2291         lc = user_param;
2292         k = 0;
2293         tm = 0;
2294
2295         for (i = 0; i != nb_pkts; i++) {
2296
2297                 mb = pkt[i];
2298                 eth = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
2299                 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
2300
2301                         struct rte_ipv4_hdr *iph;
2302
2303                         iph = (struct rte_ipv4_hdr *)(eth + 1);
2304                         if (rte_ipv4_frag_pkt_is_fragmented(iph)) {
2305
2306                                 mb->l2_len = sizeof(*eth);
2307                                 mb->l3_len = sizeof(*iph);
2308                                 tm = (tm != 0) ? tm : rte_rdtsc();
2309                                 mb = rte_ipv4_frag_reassemble_packet(
2310                                         lc->frag.tbl, &lc->frag.dr,
2311                                         mb, tm, iph);
2312
2313                                 if (mb != NULL) {
2314                                         /* fix ip cksum after reassemble. */
2315                                         iph = rte_pktmbuf_mtod_offset(mb,
2316                                                 struct rte_ipv4_hdr *,
2317                                                 mb->l2_len);
2318                                         iph->hdr_checksum = 0;
2319                                         iph->hdr_checksum = rte_ipv4_cksum(iph);
2320                                 }
2321                         }
2322                 } else if (eth->ether_type ==
2323                                 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
2324
2325                         struct rte_ipv6_hdr *iph;
2326                         struct ipv6_extension_fragment *fh;
2327
2328                         iph = (struct rte_ipv6_hdr *)(eth + 1);
2329                         fh = rte_ipv6_frag_get_ipv6_fragment_header(iph);
2330                         if (fh != NULL) {
2331                                 mb->l2_len = sizeof(*eth);
2332                                 mb->l3_len = (uintptr_t)fh - (uintptr_t)iph +
2333                                         sizeof(*fh);
2334                                 tm = (tm != 0) ? tm : rte_rdtsc();
2335                                 mb = rte_ipv6_frag_reassemble_packet(
2336                                         lc->frag.tbl, &lc->frag.dr,
2337                                         mb, tm, iph, fh);
2338                                 if (mb != NULL)
2339                                         /* fix l3_len after reassemble. */
2340                                         mb->l3_len = mb->l3_len - sizeof(*fh);
2341                         }
2342                 }
2343
2344                 pkt[k] = mb;
2345                 k += (mb != NULL);
2346         }
2347
2348         /* some fragments were encountered, drain death row */
2349         if (tm != 0)
2350                 rte_ip_frag_free_death_row(&lc->frag.dr, 0);
2351
2352         return k;
2353 }
2354
2355
2356 static int
2357 reassemble_lcore_init(struct lcore_conf *lc, uint32_t cid)
2358 {
2359         int32_t sid;
2360         uint32_t i;
2361         uint64_t frag_cycles;
2362         const struct lcore_rx_queue *rxq;
2363         const struct rte_eth_rxtx_callback *cb;
2364
2365         /* create fragment table */
2366         sid = rte_lcore_to_socket_id(cid);
2367         frag_cycles = (rte_get_tsc_hz() + NS_PER_S - 1) /
2368                 NS_PER_S * frag_ttl_ns;
2369
2370         lc->frag.tbl = rte_ip_frag_table_create(frag_tbl_sz,
2371                 FRAG_TBL_BUCKET_ENTRIES, frag_tbl_sz, frag_cycles, sid);
2372         if (lc->frag.tbl == NULL) {
2373                 printf("%s(%u): failed to create fragment table of size: %u, "
2374                         "error code: %d\n",
2375                         __func__, cid, frag_tbl_sz, rte_errno);
2376                 return -ENOMEM;
2377         }
2378
2379         /* setup reassemble RX callbacks for all queues */
2380         for (i = 0; i != lc->nb_rx_queue; i++) {
2381
2382                 rxq = lc->rx_queue_list + i;
2383                 cb = rte_eth_add_rx_callback(rxq->port_id, rxq->queue_id,
2384                         rx_callback, lc);
2385                 if (cb == NULL) {
2386                         printf("%s(%u): failed to install RX callback for "
2387                                 "portid=%u, queueid=%u, error code: %d\n",
2388                                 __func__, cid,
2389                                 rxq->port_id, rxq->queue_id, rte_errno);
2390                         return -ENOMEM;
2391                 }
2392         }
2393
2394         return 0;
2395 }
2396
2397 static int
2398 reassemble_init(void)
2399 {
2400         int32_t rc;
2401         uint32_t i, lc;
2402
2403         rc = 0;
2404         for (i = 0; i != nb_lcore_params; i++) {
2405                 lc = lcore_params[i].lcore_id;
2406                 rc = reassemble_lcore_init(lcore_conf + lc, lc);
2407                 if (rc != 0)
2408                         break;
2409         }
2410
2411         return rc;
2412 }
2413
2414 int32_t
2415 main(int32_t argc, char **argv)
2416 {
2417         int32_t ret;
2418         uint32_t lcore_id;
2419         uint32_t i;
2420         uint8_t socket_id;
2421         uint16_t portid;
2422         uint64_t req_rx_offloads, req_tx_offloads;
2423         size_t sess_sz;
2424
2425         /* init EAL */
2426         ret = rte_eal_init(argc, argv);
2427         if (ret < 0)
2428                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2429         argc -= ret;
2430         argv += ret;
2431
2432         /* parse application arguments (after the EAL ones) */
2433         ret = parse_args(argc, argv);
2434         if (ret < 0)
2435                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
2436
2437         /* parse configuration file */
2438         if (parse_cfg_file(cfgfile) < 0) {
2439                 printf("parsing file \"%s\" failed\n",
2440                         optarg);
2441                 print_usage(argv[0]);
2442                 return -1;
2443         }
2444
2445         if ((unprotected_port_mask & enabled_port_mask) !=
2446                         unprotected_port_mask)
2447                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2448                                 unprotected_port_mask);
2449
2450         if (check_params() < 0)
2451                 rte_exit(EXIT_FAILURE, "check_params failed\n");
2452
2453         ret = init_lcore_rx_queues();
2454         if (ret < 0)
2455                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2456
2457         nb_lcores = rte_lcore_count();
2458
2459         sess_sz = max_session_size();
2460
2461         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2462                 if (rte_lcore_is_enabled(lcore_id) == 0)
2463                         continue;
2464
2465                 if (numa_on)
2466                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2467                 else
2468                         socket_id = 0;
2469
2470                 /* mbuf_pool is initialised by the pool_init() function*/
2471                 if (socket_ctx[socket_id].mbuf_pool)
2472                         continue;
2473
2474                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
2475                 session_pool_init(&socket_ctx[socket_id], socket_id, sess_sz);
2476                 session_priv_pool_init(&socket_ctx[socket_id], socket_id,
2477                         sess_sz);
2478         }
2479
2480         RTE_ETH_FOREACH_DEV(portid) {
2481                 if ((enabled_port_mask & (1 << portid)) == 0)
2482                         continue;
2483
2484                 sa_check_offloads(portid, &req_rx_offloads, &req_tx_offloads);
2485                 port_init(portid, req_rx_offloads, req_tx_offloads);
2486         }
2487
2488         cryptodevs_init();
2489
2490         /* start ports */
2491         RTE_ETH_FOREACH_DEV(portid) {
2492                 if ((enabled_port_mask & (1 << portid)) == 0)
2493                         continue;
2494
2495                 /*
2496                  * Start device
2497                  * note: device must be started before a flow rule
2498                  * can be installed.
2499                  */
2500                 ret = rte_eth_dev_start(portid);
2501                 if (ret < 0)
2502                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
2503                                         "err=%d, port=%d\n", ret, portid);
2504                 /*
2505                  * If enabled, put device in promiscuous mode.
2506                  * This allows IO forwarding mode to forward packets
2507                  * to itself through 2 cross-connected  ports of the
2508                  * target machine.
2509                  */
2510                 if (promiscuous_on) {
2511                         ret = rte_eth_promiscuous_enable(portid);
2512                         if (ret != 0)
2513                                 rte_exit(EXIT_FAILURE,
2514                                         "rte_eth_promiscuous_enable: err=%s, port=%d\n",
2515                                         rte_strerror(-ret), portid);
2516                 }
2517
2518                 rte_eth_dev_callback_register(portid,
2519                         RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
2520         }
2521
2522         /* fragment reassemble is enabled */
2523         if (frag_tbl_sz != 0) {
2524                 ret = reassemble_init();
2525                 if (ret != 0)
2526                         rte_exit(EXIT_FAILURE, "failed at reassemble init");
2527         }
2528
2529         /* Replicate each context per socket */
2530         for (i = 0; i < NB_SOCKETS && i < rte_socket_count(); i++) {
2531                 socket_id = rte_socket_id_by_idx(i);
2532                 if ((socket_ctx[socket_id].mbuf_pool != NULL) &&
2533                         (socket_ctx[socket_id].sa_in == NULL) &&
2534                         (socket_ctx[socket_id].sa_out == NULL)) {
2535                         sa_init(&socket_ctx[socket_id], socket_id);
2536                         sp4_init(&socket_ctx[socket_id], socket_id);
2537                         sp6_init(&socket_ctx[socket_id], socket_id);
2538                         rt_init(&socket_ctx[socket_id], socket_id);
2539                 }
2540         }
2541
2542         check_all_ports_link_status(enabled_port_mask);
2543
2544         /* launch per-lcore init on every lcore */
2545         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
2546         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
2547                 if (rte_eal_wait_lcore(lcore_id) < 0)
2548                         return -1;
2549         }
2550
2551         return 0;
2552 }