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