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