examples: use separate crypto session mempools
[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
45 #include "ipsec.h"
46 #include "parser.h"
47
48 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
49
50 #define MAX_JUMBO_PKT_LEN  9600
51
52 #define MEMPOOL_CACHE_SIZE 256
53
54 #define NB_MBUF (32000)
55
56 #define CDEV_QUEUE_DESC 2048
57 #define CDEV_MAP_ENTRIES 16384
58 #define CDEV_MP_NB_OBJS 1024
59 #define CDEV_MP_CACHE_SZ 64
60 #define MAX_QUEUE_PAIRS 1
61
62 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
63
64 #define NB_SOCKETS 4
65
66 /* Configure how many packets ahead to prefetch, when reading packets */
67 #define PREFETCH_OFFSET 3
68
69 #define MAX_RX_QUEUE_PER_LCORE 16
70
71 #define MAX_LCORE_PARAMS 1024
72
73 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
74
75 /*
76  * Configurable number of RX/TX ring descriptors
77  */
78 #define IPSEC_SECGW_RX_DESC_DEFAULT 1024
79 #define IPSEC_SECGW_TX_DESC_DEFAULT 1024
80 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
81 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
82
83 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
84 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
85         (((uint64_t)((a) & 0xff) << 56) | \
86         ((uint64_t)((b) & 0xff) << 48) | \
87         ((uint64_t)((c) & 0xff) << 40) | \
88         ((uint64_t)((d) & 0xff) << 32) | \
89         ((uint64_t)((e) & 0xff) << 24) | \
90         ((uint64_t)((f) & 0xff) << 16) | \
91         ((uint64_t)((g) & 0xff) << 8)  | \
92         ((uint64_t)(h) & 0xff))
93 #else
94 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
95         (((uint64_t)((h) & 0xff) << 56) | \
96         ((uint64_t)((g) & 0xff) << 48) | \
97         ((uint64_t)((f) & 0xff) << 40) | \
98         ((uint64_t)((e) & 0xff) << 32) | \
99         ((uint64_t)((d) & 0xff) << 24) | \
100         ((uint64_t)((c) & 0xff) << 16) | \
101         ((uint64_t)((b) & 0xff) << 8) | \
102         ((uint64_t)(a) & 0xff))
103 #endif
104 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
105
106 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
107                 addr.addr_bytes[0], addr.addr_bytes[1], \
108                 addr.addr_bytes[2], addr.addr_bytes[3], \
109                 addr.addr_bytes[4], addr.addr_bytes[5], \
110                 0, 0)
111
112 /* port/source ethernet addr and destination ethernet addr */
113 struct ethaddr_info {
114         uint64_t src, dst;
115 };
116
117 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
118         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
119         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
120         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
121         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
122 };
123
124 #define CMD_LINE_OPT_CONFIG             "config"
125 #define CMD_LINE_OPT_SINGLE_SA          "single-sa"
126 #define CMD_LINE_OPT_CRYPTODEV_MASK     "cryptodev_mask"
127
128 enum {
129         /* long options mapped to a short option */
130
131         /* first long only option value must be >= 256, so that we won't
132          * conflict with short options
133          */
134         CMD_LINE_OPT_MIN_NUM = 256,
135         CMD_LINE_OPT_CONFIG_NUM,
136         CMD_LINE_OPT_SINGLE_SA_NUM,
137         CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
138 };
139
140 static const struct option lgopts[] = {
141         {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
142         {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
143         {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
144         {NULL, 0, 0, 0}
145 };
146
147 /* mask of enabled ports */
148 static uint32_t enabled_port_mask;
149 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
150 static uint32_t unprotected_port_mask;
151 static int32_t promiscuous_on = 1;
152 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
153 static uint32_t nb_lcores;
154 static uint32_t single_sa;
155 static uint32_t single_sa_idx;
156 static uint32_t frame_size;
157
158 struct lcore_rx_queue {
159         uint16_t port_id;
160         uint8_t queue_id;
161 } __rte_cache_aligned;
162
163 struct lcore_params {
164         uint16_t port_id;
165         uint8_t queue_id;
166         uint8_t lcore_id;
167 } __rte_cache_aligned;
168
169 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
170
171 static struct lcore_params *lcore_params;
172 static uint16_t nb_lcore_params;
173
174 static struct rte_hash *cdev_map_in;
175 static struct rte_hash *cdev_map_out;
176
177 struct buffer {
178         uint16_t len;
179         struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
180 };
181
182 struct lcore_conf {
183         uint16_t nb_rx_queue;
184         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
185         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
186         struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
187         struct ipsec_ctx inbound;
188         struct ipsec_ctx outbound;
189         struct rt_ctx *rt4_ctx;
190         struct rt_ctx *rt6_ctx;
191 } __rte_cache_aligned;
192
193 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
194
195 static struct rte_eth_conf port_conf = {
196         .rxmode = {
197                 .mq_mode        = ETH_MQ_RX_RSS,
198                 .max_rx_pkt_len = ETHER_MAX_LEN,
199                 .split_hdr_size = 0,
200                 .offloads = DEV_RX_OFFLOAD_CHECKSUM,
201         },
202         .rx_adv_conf = {
203                 .rss_conf = {
204                         .rss_key = NULL,
205                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
206                                 ETH_RSS_TCP | ETH_RSS_SCTP,
207                 },
208         },
209         .txmode = {
210                 .mq_mode = ETH_MQ_TX_NONE,
211                 .offloads = (DEV_TX_OFFLOAD_IPV4_CKSUM |
212                              DEV_TX_OFFLOAD_MULTI_SEGS),
213         },
214 };
215
216 static struct socket_ctx socket_ctx[NB_SOCKETS];
217
218 struct traffic_type {
219         const uint8_t *data[MAX_PKT_BURST * 2];
220         struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
221         uint32_t res[MAX_PKT_BURST * 2];
222         uint32_t num;
223 };
224
225 struct ipsec_traffic {
226         struct traffic_type ipsec;
227         struct traffic_type ip4;
228         struct traffic_type ip6;
229 };
230
231 static inline void
232 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
233 {
234         uint8_t *nlp;
235         struct ether_hdr *eth;
236
237         eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
238         if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
239                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
240                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p));
241                 if (*nlp == IPPROTO_ESP)
242                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
243                 else {
244                         t->ip4.data[t->ip4.num] = nlp;
245                         t->ip4.pkts[(t->ip4.num)++] = pkt;
246                 }
247         } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
248                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
249                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt));
250                 if (*nlp == IPPROTO_ESP)
251                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
252                 else {
253                         t->ip6.data[t->ip6.num] = nlp;
254                         t->ip6.pkts[(t->ip6.num)++] = pkt;
255                 }
256         } else {
257                 /* Unknown/Unsupported type, drop the packet */
258                 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n");
259                 rte_pktmbuf_free(pkt);
260         }
261
262         /* Check if the packet has been processed inline. For inline protocol
263          * processed packets, the metadata in the mbuf can be used to identify
264          * the security processing done on the packet. The metadata will be
265          * used to retrieve the application registered userdata associated
266          * with the security session.
267          */
268
269         if (pkt->ol_flags & PKT_RX_SEC_OFFLOAD) {
270                 struct ipsec_sa *sa;
271                 struct ipsec_mbuf_metadata *priv;
272                 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
273                                                 rte_eth_dev_get_sec_ctx(
274                                                 pkt->port);
275
276                 /* Retrieve the userdata registered. Here, the userdata
277                  * registered is the SA pointer.
278                  */
279
280                 sa = (struct ipsec_sa *)
281                                 rte_security_get_userdata(ctx, pkt->udata64);
282
283                 if (sa == NULL) {
284                         /* userdata could not be retrieved */
285                         return;
286                 }
287
288                 /* Save SA as priv member in mbuf. This will be used in the
289                  * IPsec selector(SP-SA) check.
290                  */
291
292                 priv = get_priv(pkt);
293                 priv->sa = sa;
294         }
295 }
296
297 static inline void
298 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
299                 uint16_t nb_pkts)
300 {
301         int32_t i;
302
303         t->ipsec.num = 0;
304         t->ip4.num = 0;
305         t->ip6.num = 0;
306
307         for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
308                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
309                                         void *));
310                 prepare_one_packet(pkts[i], t);
311         }
312         /* Process left packets */
313         for (; i < nb_pkts; i++)
314                 prepare_one_packet(pkts[i], t);
315 }
316
317 static inline void
318 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port)
319 {
320         struct ip *ip;
321         struct ether_hdr *ethhdr;
322
323         ip = rte_pktmbuf_mtod(pkt, struct ip *);
324
325         ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN);
326
327         if (ip->ip_v == IPVERSION) {
328                 pkt->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
329                 pkt->l3_len = sizeof(struct ip);
330                 pkt->l2_len = ETHER_HDR_LEN;
331
332                 ip->ip_sum = 0;
333                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
334         } else {
335                 pkt->ol_flags |= PKT_TX_IPV6;
336                 pkt->l3_len = sizeof(struct ip6_hdr);
337                 pkt->l2_len = ETHER_HDR_LEN;
338
339                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
340         }
341
342         memcpy(&ethhdr->s_addr, &ethaddr_tbl[port].src,
343                         sizeof(struct ether_addr));
344         memcpy(&ethhdr->d_addr, &ethaddr_tbl[port].dst,
345                         sizeof(struct ether_addr));
346 }
347
348 static inline void
349 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port)
350 {
351         int32_t i;
352         const int32_t prefetch_offset = 2;
353
354         for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
355                 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
356                 prepare_tx_pkt(pkts[i], port);
357         }
358         /* Process left packets */
359         for (; i < nb_pkts; i++)
360                 prepare_tx_pkt(pkts[i], port);
361 }
362
363 /* Send burst of packets on an output interface */
364 static inline int32_t
365 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
366 {
367         struct rte_mbuf **m_table;
368         int32_t ret;
369         uint16_t queueid;
370
371         queueid = qconf->tx_queue_id[port];
372         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
373
374         prepare_tx_burst(m_table, n, port);
375
376         ret = rte_eth_tx_burst(port, queueid, m_table, n);
377         if (unlikely(ret < n)) {
378                 do {
379                         rte_pktmbuf_free(m_table[ret]);
380                 } while (++ret < n);
381         }
382
383         return 0;
384 }
385
386 /* Enqueue a single packet, and send burst if queue is filled */
387 static inline int32_t
388 send_single_packet(struct rte_mbuf *m, uint16_t port)
389 {
390         uint32_t lcore_id;
391         uint16_t len;
392         struct lcore_conf *qconf;
393
394         lcore_id = rte_lcore_id();
395
396         qconf = &lcore_conf[lcore_id];
397         len = qconf->tx_mbufs[port].len;
398         qconf->tx_mbufs[port].m_table[len] = m;
399         len++;
400
401         /* enough pkts to be sent */
402         if (unlikely(len == MAX_PKT_BURST)) {
403                 send_burst(qconf, MAX_PKT_BURST, port);
404                 len = 0;
405         }
406
407         qconf->tx_mbufs[port].len = len;
408         return 0;
409 }
410
411 static inline void
412 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
413                 uint16_t lim)
414 {
415         struct rte_mbuf *m;
416         uint32_t i, j, res, sa_idx;
417
418         if (ip->num == 0 || sp == NULL)
419                 return;
420
421         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
422                         ip->num, DEFAULT_MAX_CATEGORIES);
423
424         j = 0;
425         for (i = 0; i < ip->num; i++) {
426                 m = ip->pkts[i];
427                 res = ip->res[i];
428                 if (res & BYPASS) {
429                         ip->pkts[j++] = m;
430                         continue;
431                 }
432                 if (res & DISCARD) {
433                         rte_pktmbuf_free(m);
434                         continue;
435                 }
436
437                 /* Only check SPI match for processed IPSec packets */
438                 if (i < lim && ((m->ol_flags & PKT_RX_SEC_OFFLOAD) == 0)) {
439                         rte_pktmbuf_free(m);
440                         continue;
441                 }
442
443                 sa_idx = ip->res[i] & PROTECT_MASK;
444                 if (sa_idx >= IPSEC_SA_MAX_ENTRIES ||
445                                 !inbound_sa_check(sa, m, sa_idx)) {
446                         rte_pktmbuf_free(m);
447                         continue;
448                 }
449                 ip->pkts[j++] = m;
450         }
451         ip->num = j;
452 }
453
454 static inline void
455 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
456                 struct ipsec_traffic *traffic)
457 {
458         struct rte_mbuf *m;
459         uint16_t idx, nb_pkts_in, i, n_ip4, n_ip6;
460
461         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
462                         traffic->ipsec.num, MAX_PKT_BURST);
463
464         n_ip4 = traffic->ip4.num;
465         n_ip6 = traffic->ip6.num;
466
467         /* SP/ACL Inbound check ipsec and ip4 */
468         for (i = 0; i < nb_pkts_in; i++) {
469                 m = traffic->ipsec.pkts[i];
470                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
471                 if (ip->ip_v == IPVERSION) {
472                         idx = traffic->ip4.num++;
473                         traffic->ip4.pkts[idx] = m;
474                         traffic->ip4.data[idx] = rte_pktmbuf_mtod_offset(m,
475                                         uint8_t *, offsetof(struct ip, ip_p));
476                 } else if (ip->ip_v == IP6_VERSION) {
477                         idx = traffic->ip6.num++;
478                         traffic->ip6.pkts[idx] = m;
479                         traffic->ip6.data[idx] = rte_pktmbuf_mtod_offset(m,
480                                         uint8_t *,
481                                         offsetof(struct ip6_hdr, ip6_nxt));
482                 } else
483                         rte_pktmbuf_free(m);
484         }
485
486         inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
487                         n_ip4);
488
489         inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
490                         n_ip6);
491 }
492
493 static inline void
494 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
495                 struct traffic_type *ipsec)
496 {
497         struct rte_mbuf *m;
498         uint32_t i, j, sa_idx;
499
500         if (ip->num == 0 || sp == NULL)
501                 return;
502
503         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
504                         ip->num, DEFAULT_MAX_CATEGORIES);
505
506         j = 0;
507         for (i = 0; i < ip->num; i++) {
508                 m = ip->pkts[i];
509                 sa_idx = ip->res[i] & PROTECT_MASK;
510                 if (ip->res[i] & DISCARD)
511                         rte_pktmbuf_free(m);
512                 else if (ip->res[i] & BYPASS)
513                         ip->pkts[j++] = m;
514                 else if (sa_idx < IPSEC_SA_MAX_ENTRIES) {
515                         ipsec->res[ipsec->num] = sa_idx;
516                         ipsec->pkts[ipsec->num++] = m;
517                 } else /* invalid SA idx */
518                         rte_pktmbuf_free(m);
519         }
520         ip->num = j;
521 }
522
523 static inline void
524 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
525                 struct ipsec_traffic *traffic)
526 {
527         struct rte_mbuf *m;
528         uint16_t idx, nb_pkts_out, i;
529
530         /* Drop any IPsec traffic from protected ports */
531         for (i = 0; i < traffic->ipsec.num; i++)
532                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
533
534         traffic->ipsec.num = 0;
535
536         outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
537
538         outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
539
540         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
541                         traffic->ipsec.res, traffic->ipsec.num,
542                         MAX_PKT_BURST);
543
544         for (i = 0; i < nb_pkts_out; i++) {
545                 m = traffic->ipsec.pkts[i];
546                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
547                 if (ip->ip_v == IPVERSION) {
548                         idx = traffic->ip4.num++;
549                         traffic->ip4.pkts[idx] = m;
550                 } else {
551                         idx = traffic->ip6.num++;
552                         traffic->ip6.pkts[idx] = m;
553                 }
554         }
555 }
556
557 static inline void
558 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
559                 struct ipsec_traffic *traffic)
560 {
561         struct rte_mbuf *m;
562         uint32_t nb_pkts_in, i, idx;
563
564         /* Drop any IPv4 traffic from unprotected ports */
565         for (i = 0; i < traffic->ip4.num; i++)
566                 rte_pktmbuf_free(traffic->ip4.pkts[i]);
567
568         traffic->ip4.num = 0;
569
570         /* Drop any IPv6 traffic from unprotected ports */
571         for (i = 0; i < traffic->ip6.num; i++)
572                 rte_pktmbuf_free(traffic->ip6.pkts[i]);
573
574         traffic->ip6.num = 0;
575
576         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
577                         traffic->ipsec.num, MAX_PKT_BURST);
578
579         for (i = 0; i < nb_pkts_in; i++) {
580                 m = traffic->ipsec.pkts[i];
581                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
582                 if (ip->ip_v == IPVERSION) {
583                         idx = traffic->ip4.num++;
584                         traffic->ip4.pkts[idx] = m;
585                 } else {
586                         idx = traffic->ip6.num++;
587                         traffic->ip6.pkts[idx] = m;
588                 }
589         }
590 }
591
592 static inline void
593 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
594                 struct ipsec_traffic *traffic)
595 {
596         struct rte_mbuf *m;
597         uint32_t nb_pkts_out, i;
598         struct ip *ip;
599
600         /* Drop any IPsec traffic from protected ports */
601         for (i = 0; i < traffic->ipsec.num; i++)
602                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
603
604         traffic->ipsec.num = 0;
605
606         for (i = 0; i < traffic->ip4.num; i++)
607                 traffic->ip4.res[i] = single_sa_idx;
608
609         for (i = 0; i < traffic->ip6.num; i++)
610                 traffic->ip6.res[i] = single_sa_idx;
611
612         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
613                         traffic->ip4.res, traffic->ip4.num,
614                         MAX_PKT_BURST);
615
616         /* They all sue the same SA (ip4 or ip6 tunnel) */
617         m = traffic->ipsec.pkts[i];
618         ip = rte_pktmbuf_mtod(m, struct ip *);
619         if (ip->ip_v == IPVERSION)
620                 traffic->ip4.num = nb_pkts_out;
621         else
622                 traffic->ip6.num = nb_pkts_out;
623 }
624
625 static inline int32_t
626 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
627 {
628         struct ipsec_mbuf_metadata *priv;
629         struct ipsec_sa *sa;
630
631         priv = get_priv(pkt);
632
633         sa = priv->sa;
634         if (unlikely(sa == NULL)) {
635                 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
636                 goto fail;
637         }
638
639         if (is_ipv6)
640                 return sa->portid;
641
642         /* else */
643         return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
644
645 fail:
646         if (is_ipv6)
647                 return -1;
648
649         /* else */
650         return 0;
651 }
652
653 static inline void
654 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
655 {
656         uint32_t hop[MAX_PKT_BURST * 2];
657         uint32_t dst_ip[MAX_PKT_BURST * 2];
658         int32_t pkt_hop = 0;
659         uint16_t i, offset;
660         uint16_t lpm_pkts = 0;
661
662         if (nb_pkts == 0)
663                 return;
664
665         /* Need to do an LPM lookup for non-inline packets. Inline packets will
666          * have port ID in the SA
667          */
668
669         for (i = 0; i < nb_pkts; i++) {
670                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
671                         /* Security offload not enabled. So an LPM lookup is
672                          * required to get the hop
673                          */
674                         offset = offsetof(struct ip, ip_dst);
675                         dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
676                                         uint32_t *, offset);
677                         dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
678                         lpm_pkts++;
679                 }
680         }
681
682         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
683
684         lpm_pkts = 0;
685
686         for (i = 0; i < nb_pkts; i++) {
687                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
688                         /* Read hop from the SA */
689                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
690                 } else {
691                         /* Need to use hop returned by lookup */
692                         pkt_hop = hop[lpm_pkts++];
693                 }
694
695                 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
696                         rte_pktmbuf_free(pkts[i]);
697                         continue;
698                 }
699                 send_single_packet(pkts[i], pkt_hop & 0xff);
700         }
701 }
702
703 static inline void
704 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
705 {
706         int32_t hop[MAX_PKT_BURST * 2];
707         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
708         uint8_t *ip6_dst;
709         int32_t pkt_hop = 0;
710         uint16_t i, offset;
711         uint16_t lpm_pkts = 0;
712
713         if (nb_pkts == 0)
714                 return;
715
716         /* Need to do an LPM lookup for non-inline packets. Inline packets will
717          * have port ID in the SA
718          */
719
720         for (i = 0; i < nb_pkts; i++) {
721                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
722                         /* Security offload not enabled. So an LPM lookup is
723                          * required to get the hop
724                          */
725                         offset = offsetof(struct ip6_hdr, ip6_dst);
726                         ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
727                                         offset);
728                         memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
729                         lpm_pkts++;
730                 }
731         }
732
733         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
734                         lpm_pkts);
735
736         lpm_pkts = 0;
737
738         for (i = 0; i < nb_pkts; i++) {
739                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
740                         /* Read hop from the SA */
741                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
742                 } else {
743                         /* Need to use hop returned by lookup */
744                         pkt_hop = hop[lpm_pkts++];
745                 }
746
747                 if (pkt_hop == -1) {
748                         rte_pktmbuf_free(pkts[i]);
749                         continue;
750                 }
751                 send_single_packet(pkts[i], pkt_hop & 0xff);
752         }
753 }
754
755 static inline void
756 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
757                 uint8_t nb_pkts, uint16_t portid)
758 {
759         struct ipsec_traffic traffic;
760
761         prepare_traffic(pkts, &traffic, nb_pkts);
762
763         if (unlikely(single_sa)) {
764                 if (UNPROTECTED_PORT(portid))
765                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
766                 else
767                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
768         } else {
769                 if (UNPROTECTED_PORT(portid))
770                         process_pkts_inbound(&qconf->inbound, &traffic);
771                 else
772                         process_pkts_outbound(&qconf->outbound, &traffic);
773         }
774
775         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
776         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
777 }
778
779 static inline void
780 drain_buffers(struct lcore_conf *qconf)
781 {
782         struct buffer *buf;
783         uint32_t portid;
784
785         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
786                 buf = &qconf->tx_mbufs[portid];
787                 if (buf->len == 0)
788                         continue;
789                 send_burst(qconf, buf->len, portid);
790                 buf->len = 0;
791         }
792 }
793
794 /* main processing loop */
795 static int32_t
796 main_loop(__attribute__((unused)) void *dummy)
797 {
798         struct rte_mbuf *pkts[MAX_PKT_BURST];
799         uint32_t lcore_id;
800         uint64_t prev_tsc, diff_tsc, cur_tsc;
801         int32_t i, nb_rx;
802         uint16_t portid;
803         uint8_t queueid;
804         struct lcore_conf *qconf;
805         int32_t socket_id;
806         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
807                         / US_PER_S * BURST_TX_DRAIN_US;
808         struct lcore_rx_queue *rxql;
809
810         prev_tsc = 0;
811         lcore_id = rte_lcore_id();
812         qconf = &lcore_conf[lcore_id];
813         rxql = qconf->rx_queue_list;
814         socket_id = rte_lcore_to_socket_id(lcore_id);
815
816         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
817         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
818         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
819         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
820         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
821         qconf->inbound.cdev_map = cdev_map_in;
822         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
823         qconf->inbound.session_priv_pool =
824                         socket_ctx[socket_id].session_priv_pool;
825         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
826         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
827         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
828         qconf->outbound.cdev_map = cdev_map_out;
829         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
830         qconf->outbound.session_priv_pool =
831                         socket_ctx[socket_id].session_priv_pool;
832
833         if (qconf->nb_rx_queue == 0) {
834                 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
835                 return 0;
836         }
837
838         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
839
840         for (i = 0; i < qconf->nb_rx_queue; i++) {
841                 portid = rxql[i].port_id;
842                 queueid = rxql[i].queue_id;
843                 RTE_LOG(INFO, IPSEC,
844                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
845                         lcore_id, portid, queueid);
846         }
847
848         while (1) {
849                 cur_tsc = rte_rdtsc();
850
851                 /* TX queue buffer drain */
852                 diff_tsc = cur_tsc - prev_tsc;
853
854                 if (unlikely(diff_tsc > drain_tsc)) {
855                         drain_buffers(qconf);
856                         prev_tsc = cur_tsc;
857                 }
858
859                 /* Read packet from RX queues */
860                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
861                         portid = rxql[i].port_id;
862                         queueid = rxql[i].queue_id;
863                         nb_rx = rte_eth_rx_burst(portid, queueid,
864                                         pkts, MAX_PKT_BURST);
865
866                         if (nb_rx > 0)
867                                 process_pkts(qconf, pkts, nb_rx, portid);
868                 }
869         }
870 }
871
872 static int32_t
873 check_params(void)
874 {
875         uint8_t lcore;
876         uint16_t portid;
877         uint16_t i;
878         int32_t socket_id;
879
880         if (lcore_params == NULL) {
881                 printf("Error: No port/queue/core mappings\n");
882                 return -1;
883         }
884
885         for (i = 0; i < nb_lcore_params; ++i) {
886                 lcore = lcore_params[i].lcore_id;
887                 if (!rte_lcore_is_enabled(lcore)) {
888                         printf("error: lcore %hhu is not enabled in "
889                                 "lcore mask\n", lcore);
890                         return -1;
891                 }
892                 socket_id = rte_lcore_to_socket_id(lcore);
893                 if (socket_id != 0 && numa_on == 0) {
894                         printf("warning: lcore %hhu is on socket %d "
895                                 "with numa off\n",
896                                 lcore, socket_id);
897                 }
898                 portid = lcore_params[i].port_id;
899                 if ((enabled_port_mask & (1 << portid)) == 0) {
900                         printf("port %u is not enabled in port mask\n", portid);
901                         return -1;
902                 }
903                 if (!rte_eth_dev_is_valid_port(portid)) {
904                         printf("port %u is not present on the board\n", portid);
905                         return -1;
906                 }
907         }
908         return 0;
909 }
910
911 static uint8_t
912 get_port_nb_rx_queues(const uint16_t port)
913 {
914         int32_t queue = -1;
915         uint16_t i;
916
917         for (i = 0; i < nb_lcore_params; ++i) {
918                 if (lcore_params[i].port_id == port &&
919                                 lcore_params[i].queue_id > queue)
920                         queue = lcore_params[i].queue_id;
921         }
922         return (uint8_t)(++queue);
923 }
924
925 static int32_t
926 init_lcore_rx_queues(void)
927 {
928         uint16_t i, nb_rx_queue;
929         uint8_t lcore;
930
931         for (i = 0; i < nb_lcore_params; ++i) {
932                 lcore = lcore_params[i].lcore_id;
933                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
934                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
935                         printf("error: too many queues (%u) for lcore: %u\n",
936                                         nb_rx_queue + 1, lcore);
937                         return -1;
938                 }
939                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
940                         lcore_params[i].port_id;
941                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
942                         lcore_params[i].queue_id;
943                 lcore_conf[lcore].nb_rx_queue++;
944         }
945         return 0;
946 }
947
948 /* display usage */
949 static void
950 print_usage(const char *prgname)
951 {
952         fprintf(stderr, "%s [EAL options] --"
953                 " -p PORTMASK"
954                 " [-P]"
955                 " [-u PORTMASK]"
956                 " [-j FRAMESIZE]"
957                 " -f CONFIG_FILE"
958                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
959                 " [--single-sa SAIDX]"
960                 " [--cryptodev_mask MASK]"
961                 "\n\n"
962                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
963                 "  -P : Enable promiscuous mode\n"
964                 "  -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
965                 "  -j FRAMESIZE: Enable jumbo frame with 'FRAMESIZE' as maximum\n"
966                 "                packet size\n"
967                 "  -f CONFIG_FILE: Configuration file\n"
968                 "  --config (port,queue,lcore): Rx queue configuration\n"
969                 "  --single-sa SAIDX: Use single SA index for outbound traffic,\n"
970                 "                     bypassing the SP\n"
971                 "  --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
972                 "                         devices to configure\n"
973                 "\n",
974                 prgname);
975 }
976
977 static int32_t
978 parse_portmask(const char *portmask)
979 {
980         char *end = NULL;
981         unsigned long pm;
982
983         /* parse hexadecimal string */
984         pm = strtoul(portmask, &end, 16);
985         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
986                 return -1;
987
988         if ((pm == 0) && errno)
989                 return -1;
990
991         return pm;
992 }
993
994 static int32_t
995 parse_decimal(const char *str)
996 {
997         char *end = NULL;
998         unsigned long num;
999
1000         num = strtoul(str, &end, 10);
1001         if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
1002                 return -1;
1003
1004         return num;
1005 }
1006
1007 static int32_t
1008 parse_config(const char *q_arg)
1009 {
1010         char s[256];
1011         const char *p, *p0 = q_arg;
1012         char *end;
1013         enum fieldnames {
1014                 FLD_PORT = 0,
1015                 FLD_QUEUE,
1016                 FLD_LCORE,
1017                 _NUM_FLD
1018         };
1019         unsigned long int_fld[_NUM_FLD];
1020         char *str_fld[_NUM_FLD];
1021         int32_t i;
1022         uint32_t size;
1023
1024         nb_lcore_params = 0;
1025
1026         while ((p = strchr(p0, '(')) != NULL) {
1027                 ++p;
1028                 p0 = strchr(p, ')');
1029                 if (p0 == NULL)
1030                         return -1;
1031
1032                 size = p0 - p;
1033                 if (size >= sizeof(s))
1034                         return -1;
1035
1036                 snprintf(s, sizeof(s), "%.*s", size, p);
1037                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1038                                 _NUM_FLD)
1039                         return -1;
1040                 for (i = 0; i < _NUM_FLD; i++) {
1041                         errno = 0;
1042                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1043                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1044                                 return -1;
1045                 }
1046                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1047                         printf("exceeded max number of lcore params: %hu\n",
1048                                 nb_lcore_params);
1049                         return -1;
1050                 }
1051                 lcore_params_array[nb_lcore_params].port_id =
1052                         (uint8_t)int_fld[FLD_PORT];
1053                 lcore_params_array[nb_lcore_params].queue_id =
1054                         (uint8_t)int_fld[FLD_QUEUE];
1055                 lcore_params_array[nb_lcore_params].lcore_id =
1056                         (uint8_t)int_fld[FLD_LCORE];
1057                 ++nb_lcore_params;
1058         }
1059         lcore_params = lcore_params_array;
1060         return 0;
1061 }
1062
1063 static int32_t
1064 parse_args(int32_t argc, char **argv)
1065 {
1066         int32_t opt, ret;
1067         char **argvopt;
1068         int32_t option_index;
1069         char *prgname = argv[0];
1070         int32_t f_present = 0;
1071
1072         argvopt = argv;
1073
1074         while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:",
1075                                 lgopts, &option_index)) != EOF) {
1076
1077                 switch (opt) {
1078                 case 'p':
1079                         enabled_port_mask = parse_portmask(optarg);
1080                         if (enabled_port_mask == 0) {
1081                                 printf("invalid portmask\n");
1082                                 print_usage(prgname);
1083                                 return -1;
1084                         }
1085                         break;
1086                 case 'P':
1087                         printf("Promiscuous mode selected\n");
1088                         promiscuous_on = 1;
1089                         break;
1090                 case 'u':
1091                         unprotected_port_mask = parse_portmask(optarg);
1092                         if (unprotected_port_mask == 0) {
1093                                 printf("invalid unprotected portmask\n");
1094                                 print_usage(prgname);
1095                                 return -1;
1096                         }
1097                         break;
1098                 case 'f':
1099                         if (f_present == 1) {
1100                                 printf("\"-f\" option present more than "
1101                                         "once!\n");
1102                                 print_usage(prgname);
1103                                 return -1;
1104                         }
1105                         if (parse_cfg_file(optarg) < 0) {
1106                                 printf("parsing file \"%s\" failed\n",
1107                                         optarg);
1108                                 print_usage(prgname);
1109                                 return -1;
1110                         }
1111                         f_present = 1;
1112                         break;
1113                 case 'j':
1114                         {
1115                                 int32_t size = parse_decimal(optarg);
1116                                 if (size <= 1518) {
1117                                         printf("Invalid jumbo frame size\n");
1118                                         if (size < 0) {
1119                                                 print_usage(prgname);
1120                                                 return -1;
1121                                         }
1122                                         printf("Using default value 9000\n");
1123                                         frame_size = 9000;
1124                                 } else {
1125                                         frame_size = size;
1126                                 }
1127                         }
1128                         printf("Enabled jumbo frames size %u\n", frame_size);
1129                         break;
1130                 case CMD_LINE_OPT_CONFIG_NUM:
1131                         ret = parse_config(optarg);
1132                         if (ret) {
1133                                 printf("Invalid config\n");
1134                                 print_usage(prgname);
1135                                 return -1;
1136                         }
1137                         break;
1138                 case CMD_LINE_OPT_SINGLE_SA_NUM:
1139                         ret = parse_decimal(optarg);
1140                         if (ret == -1) {
1141                                 printf("Invalid argument[sa_idx]\n");
1142                                 print_usage(prgname);
1143                                 return -1;
1144                         }
1145
1146                         /* else */
1147                         single_sa = 1;
1148                         single_sa_idx = ret;
1149                         printf("Configured with single SA index %u\n",
1150                                         single_sa_idx);
1151                         break;
1152                 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1153                         ret = parse_portmask(optarg);
1154                         if (ret == -1) {
1155                                 printf("Invalid argument[portmask]\n");
1156                                 print_usage(prgname);
1157                                 return -1;
1158                         }
1159
1160                         /* else */
1161                         enabled_cryptodev_mask = ret;
1162                         break;
1163                 default:
1164                         print_usage(prgname);
1165                         return -1;
1166                 }
1167         }
1168
1169         if (f_present == 0) {
1170                 printf("Mandatory option \"-f\" not present\n");
1171                 return -1;
1172         }
1173
1174         if (optind >= 0)
1175                 argv[optind-1] = prgname;
1176
1177         ret = optind-1;
1178         optind = 1; /* reset getopt lib */
1179         return ret;
1180 }
1181
1182 static void
1183 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1184 {
1185         char buf[ETHER_ADDR_FMT_SIZE];
1186         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1187         printf("%s%s", name, buf);
1188 }
1189
1190 /* Check the link status of all ports in up to 9s, and print them finally */
1191 static void
1192 check_all_ports_link_status(uint32_t port_mask)
1193 {
1194 #define CHECK_INTERVAL 100 /* 100ms */
1195 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1196         uint16_t portid;
1197         uint8_t count, all_ports_up, print_flag = 0;
1198         struct rte_eth_link link;
1199
1200         printf("\nChecking link status");
1201         fflush(stdout);
1202         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1203                 all_ports_up = 1;
1204                 RTE_ETH_FOREACH_DEV(portid) {
1205                         if ((port_mask & (1 << portid)) == 0)
1206                                 continue;
1207                         memset(&link, 0, sizeof(link));
1208                         rte_eth_link_get_nowait(portid, &link);
1209                         /* print link status if flag set */
1210                         if (print_flag == 1) {
1211                                 if (link.link_status)
1212                                         printf(
1213                                         "Port%d Link Up - speed %u Mbps -%s\n",
1214                                                 portid, link.link_speed,
1215                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1216                                         ("full-duplex") : ("half-duplex\n"));
1217                                 else
1218                                         printf("Port %d Link Down\n", portid);
1219                                 continue;
1220                         }
1221                         /* clear all_ports_up flag if any link down */
1222                         if (link.link_status == ETH_LINK_DOWN) {
1223                                 all_ports_up = 0;
1224                                 break;
1225                         }
1226                 }
1227                 /* after finally printing all link status, get out */
1228                 if (print_flag == 1)
1229                         break;
1230
1231                 if (all_ports_up == 0) {
1232                         printf(".");
1233                         fflush(stdout);
1234                         rte_delay_ms(CHECK_INTERVAL);
1235                 }
1236
1237                 /* set the print_flag if all ports up or timeout */
1238                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1239                         print_flag = 1;
1240                         printf("done\n");
1241                 }
1242         }
1243 }
1244
1245 static int32_t
1246 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1247                 uint16_t qp, struct lcore_params *params,
1248                 struct ipsec_ctx *ipsec_ctx,
1249                 const struct rte_cryptodev_capabilities *cipher,
1250                 const struct rte_cryptodev_capabilities *auth,
1251                 const struct rte_cryptodev_capabilities *aead)
1252 {
1253         int32_t ret = 0;
1254         unsigned long i;
1255         struct cdev_key key = { 0 };
1256
1257         key.lcore_id = params->lcore_id;
1258         if (cipher)
1259                 key.cipher_algo = cipher->sym.cipher.algo;
1260         if (auth)
1261                 key.auth_algo = auth->sym.auth.algo;
1262         if (aead)
1263                 key.aead_algo = aead->sym.aead.algo;
1264
1265         ret = rte_hash_lookup(map, &key);
1266         if (ret != -ENOENT)
1267                 return 0;
1268
1269         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1270                 if (ipsec_ctx->tbl[i].id == cdev_id)
1271                         break;
1272
1273         if (i == ipsec_ctx->nb_qps) {
1274                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1275                         printf("Maximum number of crypto devices assigned to "
1276                                 "a core, increase MAX_QP_PER_LCORE value\n");
1277                         return 0;
1278                 }
1279                 ipsec_ctx->tbl[i].id = cdev_id;
1280                 ipsec_ctx->tbl[i].qp = qp;
1281                 ipsec_ctx->nb_qps++;
1282                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1283                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1284                                 cdev_id, qp, i);
1285         }
1286
1287         ret = rte_hash_add_key_data(map, &key, (void *)i);
1288         if (ret < 0) {
1289                 printf("Faled to insert cdev mapping for (lcore %u, "
1290                                 "cdev %u, qp %u), errno %d\n",
1291                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1292                                 ipsec_ctx->tbl[i].qp, ret);
1293                 return 0;
1294         }
1295
1296         return 1;
1297 }
1298
1299 static int32_t
1300 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1301                 uint16_t qp, struct lcore_params *params)
1302 {
1303         int32_t ret = 0;
1304         const struct rte_cryptodev_capabilities *i, *j;
1305         struct rte_hash *map;
1306         struct lcore_conf *qconf;
1307         struct ipsec_ctx *ipsec_ctx;
1308         const char *str;
1309
1310         qconf = &lcore_conf[params->lcore_id];
1311
1312         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1313                 map = cdev_map_out;
1314                 ipsec_ctx = &qconf->outbound;
1315                 str = "Outbound";
1316         } else {
1317                 map = cdev_map_in;
1318                 ipsec_ctx = &qconf->inbound;
1319                 str = "Inbound";
1320         }
1321
1322         /* Required cryptodevs with operation chainning */
1323         if (!(dev_info->feature_flags &
1324                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1325                 return ret;
1326
1327         for (i = dev_info->capabilities;
1328                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1329                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1330                         continue;
1331
1332                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1333                         ret |= add_mapping(map, str, cdev_id, qp, params,
1334                                         ipsec_ctx, NULL, NULL, i);
1335                         continue;
1336                 }
1337
1338                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1339                         continue;
1340
1341                 for (j = dev_info->capabilities;
1342                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1343                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1344                                 continue;
1345
1346                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1347                                 continue;
1348
1349                         ret |= add_mapping(map, str, cdev_id, qp, params,
1350                                                 ipsec_ctx, i, j, NULL);
1351                 }
1352         }
1353
1354         return ret;
1355 }
1356
1357 /* Check if the device is enabled by cryptodev_mask */
1358 static int
1359 check_cryptodev_mask(uint8_t cdev_id)
1360 {
1361         if (enabled_cryptodev_mask & (1 << cdev_id))
1362                 return 0;
1363
1364         return -1;
1365 }
1366
1367 static int32_t
1368 cryptodevs_init(void)
1369 {
1370         struct rte_cryptodev_config dev_conf;
1371         struct rte_cryptodev_qp_conf qp_conf;
1372         uint16_t idx, max_nb_qps, qp, i;
1373         int16_t cdev_id, port_id;
1374         struct rte_hash_parameters params = { 0 };
1375
1376         params.entries = CDEV_MAP_ENTRIES;
1377         params.key_len = sizeof(struct cdev_key);
1378         params.hash_func = rte_jhash;
1379         params.hash_func_init_val = 0;
1380         params.socket_id = rte_socket_id();
1381
1382         params.name = "cdev_map_in";
1383         cdev_map_in = rte_hash_create(&params);
1384         if (cdev_map_in == NULL)
1385                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1386                                 rte_errno);
1387
1388         params.name = "cdev_map_out";
1389         cdev_map_out = rte_hash_create(&params);
1390         if (cdev_map_out == NULL)
1391                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1392                                 rte_errno);
1393
1394         printf("lcore/cryptodev/qp mappings:\n");
1395
1396         uint32_t max_sess_sz = 0, sess_sz;
1397         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1398                 void *sec_ctx;
1399
1400                 /* Get crypto priv session size */
1401                 sess_sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
1402                 if (sess_sz > max_sess_sz)
1403                         max_sess_sz = sess_sz;
1404
1405                 /*
1406                  * If crypto device is security capable, need to check the
1407                  * size of security session as well.
1408                  */
1409
1410                 /* Get security context of the crypto device */
1411                 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
1412                 if (sec_ctx == NULL)
1413                         continue;
1414
1415                 /* Get size of security session */
1416                 sess_sz = rte_security_session_get_size(sec_ctx);
1417                 if (sess_sz > max_sess_sz)
1418                         max_sess_sz = sess_sz;
1419         }
1420         RTE_ETH_FOREACH_DEV(port_id) {
1421                 void *sec_ctx;
1422
1423                 if ((enabled_port_mask & (1 << port_id)) == 0)
1424                         continue;
1425
1426                 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
1427                 if (sec_ctx == NULL)
1428                         continue;
1429
1430                 sess_sz = rte_security_session_get_size(sec_ctx);
1431                 if (sess_sz > max_sess_sz)
1432                         max_sess_sz = sess_sz;
1433         }
1434
1435         idx = 0;
1436         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1437                 struct rte_cryptodev_info cdev_info;
1438
1439                 if (check_cryptodev_mask((uint8_t)cdev_id))
1440                         continue;
1441
1442                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1443
1444                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1445                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1446                 else
1447                         max_nb_qps = nb_lcore_params;
1448
1449                 qp = 0;
1450                 i = 0;
1451                 while (qp < max_nb_qps && i < nb_lcore_params) {
1452                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1453                                                 &lcore_params[idx]))
1454                                 qp++;
1455                         idx++;
1456                         idx = idx % nb_lcore_params;
1457                         i++;
1458                 }
1459
1460                 if (qp == 0)
1461                         continue;
1462
1463                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1464                 dev_conf.nb_queue_pairs = qp;
1465
1466                 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1467                 if (dev_max_sess != 0 && dev_max_sess < CDEV_MP_NB_OBJS)
1468                         rte_exit(EXIT_FAILURE,
1469                                 "Device does not support at least %u "
1470                                 "sessions", CDEV_MP_NB_OBJS);
1471
1472                 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1473                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1474                         struct rte_mempool *sess_mp;
1475
1476                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1477                                         "sess_mp_%u", dev_conf.socket_id);
1478                         sess_mp = rte_cryptodev_sym_session_pool_create(
1479                                         mp_name, CDEV_MP_NB_OBJS,
1480                                         0, CDEV_MP_CACHE_SZ, 0,
1481                                         dev_conf.socket_id);
1482                         socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1483                 }
1484
1485                 if (!socket_ctx[dev_conf.socket_id].session_priv_pool) {
1486                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1487                         struct rte_mempool *sess_mp;
1488
1489                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1490                                         "sess_mp_priv_%u", dev_conf.socket_id);
1491                         sess_mp = rte_mempool_create(mp_name,
1492                                         CDEV_MP_NB_OBJS,
1493                                         max_sess_sz,
1494                                         CDEV_MP_CACHE_SZ,
1495                                         0, NULL, NULL, NULL,
1496                                         NULL, dev_conf.socket_id,
1497                                         0);
1498                         socket_ctx[dev_conf.socket_id].session_priv_pool =
1499                                         sess_mp;
1500                 }
1501
1502                 if (!socket_ctx[dev_conf.socket_id].session_priv_pool ||
1503                                 !socket_ctx[dev_conf.socket_id].session_pool)
1504                         rte_exit(EXIT_FAILURE,
1505                                 "Cannot create session pool on socket %d\n",
1506                                 dev_conf.socket_id);
1507                 else
1508                         printf("Allocated session pool on socket %d\n",
1509                                         dev_conf.socket_id);
1510
1511                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1512                         rte_panic("Failed to initialize cryptodev %u\n",
1513                                         cdev_id);
1514
1515                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1516                 qp_conf.mp_session =
1517                         socket_ctx[dev_conf.socket_id].session_pool;
1518                 qp_conf.mp_session_private =
1519                         socket_ctx[dev_conf.socket_id].session_priv_pool;
1520                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1521                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1522                                         &qp_conf, dev_conf.socket_id))
1523                                 rte_panic("Failed to setup queue %u for "
1524                                                 "cdev_id %u\n", 0, cdev_id);
1525
1526                 if (rte_cryptodev_start(cdev_id))
1527                         rte_panic("Failed to start cryptodev %u\n",
1528                                         cdev_id);
1529         }
1530
1531         /* create session pools for eth devices that implement security */
1532         RTE_ETH_FOREACH_DEV(port_id) {
1533                 if ((enabled_port_mask & (1 << port_id)) &&
1534                                 rte_eth_dev_get_sec_ctx(port_id)) {
1535                         int socket_id = rte_eth_dev_socket_id(port_id);
1536
1537                         if (!socket_ctx[socket_id].session_pool) {
1538                                 char mp_name[RTE_MEMPOOL_NAMESIZE];
1539                                 struct rte_mempool *sess_mp;
1540
1541                                 snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1542                                                 "sess_mp_%u", socket_id);
1543                                 sess_mp = rte_mempool_create(mp_name,
1544                                                 (CDEV_MP_NB_OBJS * 2),
1545                                                 max_sess_sz,
1546                                                 CDEV_MP_CACHE_SZ,
1547                                                 0, NULL, NULL, NULL,
1548                                                 NULL, socket_id,
1549                                                 0);
1550                                 if (sess_mp == NULL)
1551                                         rte_exit(EXIT_FAILURE,
1552                                                 "Cannot create session pool "
1553                                                 "on socket %d\n", socket_id);
1554                                 else
1555                                         printf("Allocated session pool "
1556                                                 "on socket %d\n", socket_id);
1557                                 socket_ctx[socket_id].session_pool = sess_mp;
1558                         }
1559                 }
1560         }
1561
1562
1563         printf("\n");
1564
1565         return 0;
1566 }
1567
1568 static void
1569 port_init(uint16_t portid)
1570 {
1571         struct rte_eth_dev_info dev_info;
1572         struct rte_eth_txconf *txconf;
1573         uint16_t nb_tx_queue, nb_rx_queue;
1574         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1575         int32_t ret, socket_id;
1576         struct lcore_conf *qconf;
1577         struct ether_addr ethaddr;
1578         struct rte_eth_conf local_port_conf = port_conf;
1579
1580         rte_eth_dev_info_get(portid, &dev_info);
1581
1582         printf("Configuring device port %u:\n", portid);
1583
1584         rte_eth_macaddr_get(portid, &ethaddr);
1585         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1586         print_ethaddr("Address: ", &ethaddr);
1587         printf("\n");
1588
1589         nb_rx_queue = get_port_nb_rx_queues(portid);
1590         nb_tx_queue = nb_lcores;
1591
1592         if (nb_rx_queue > dev_info.max_rx_queues)
1593                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1594                                 "(max rx queue is %u)\n",
1595                                 nb_rx_queue, dev_info.max_rx_queues);
1596
1597         if (nb_tx_queue > dev_info.max_tx_queues)
1598                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1599                                 "(max tx queue is %u)\n",
1600                                 nb_tx_queue, dev_info.max_tx_queues);
1601
1602         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1603                         nb_rx_queue, nb_tx_queue);
1604
1605         if (frame_size) {
1606                 local_port_conf.rxmode.max_rx_pkt_len = frame_size;
1607                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1608         }
1609
1610         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY)
1611                 local_port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SECURITY;
1612         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SECURITY)
1613                 local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_SECURITY;
1614         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
1615                 local_port_conf.txmode.offloads |=
1616                         DEV_TX_OFFLOAD_MBUF_FAST_FREE;
1617
1618         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1619                 dev_info.flow_type_rss_offloads;
1620         if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1621                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
1622                 printf("Port %u modified RSS hash function based on hardware support,"
1623                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1624                         portid,
1625                         port_conf.rx_adv_conf.rss_conf.rss_hf,
1626                         local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1627         }
1628
1629         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1630                         &local_port_conf);
1631         if (ret < 0)
1632                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1633                                 "err=%d, port=%d\n", ret, portid);
1634
1635         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1636         if (ret < 0)
1637                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1638                                 "err=%d, port=%d\n", ret, portid);
1639
1640         /* init one TX queue per lcore */
1641         tx_queueid = 0;
1642         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1643                 if (rte_lcore_is_enabled(lcore_id) == 0)
1644                         continue;
1645
1646                 if (numa_on)
1647                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1648                 else
1649                         socket_id = 0;
1650
1651                 /* init TX queue */
1652                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1653
1654                 txconf = &dev_info.default_txconf;
1655                 txconf->offloads = local_port_conf.txmode.offloads;
1656
1657                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1658                                 socket_id, txconf);
1659                 if (ret < 0)
1660                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1661                                         "err=%d, port=%d\n", ret, portid);
1662
1663                 qconf = &lcore_conf[lcore_id];
1664                 qconf->tx_queue_id[portid] = tx_queueid;
1665                 tx_queueid++;
1666
1667                 /* init RX queues */
1668                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1669                         struct rte_eth_rxconf rxq_conf;
1670
1671                         if (portid != qconf->rx_queue_list[queue].port_id)
1672                                 continue;
1673
1674                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
1675
1676                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1677                                         socket_id);
1678
1679                         rxq_conf = dev_info.default_rxconf;
1680                         rxq_conf.offloads = local_port_conf.rxmode.offloads;
1681                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1682                                         nb_rxd, socket_id, &rxq_conf,
1683                                         socket_ctx[socket_id].mbuf_pool);
1684                         if (ret < 0)
1685                                 rte_exit(EXIT_FAILURE,
1686                                         "rte_eth_rx_queue_setup: err=%d, "
1687                                         "port=%d\n", ret, portid);
1688                 }
1689         }
1690         printf("\n");
1691 }
1692
1693 static void
1694 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1695 {
1696         char s[64];
1697         uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) :
1698                         RTE_MBUF_DEFAULT_BUF_SIZE;
1699
1700
1701         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1702         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1703                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1704                         buff_size,
1705                         socket_id);
1706         if (ctx->mbuf_pool == NULL)
1707                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1708                                 socket_id);
1709         else
1710                 printf("Allocated mbuf pool on socket %d\n", socket_id);
1711 }
1712
1713 static inline int
1714 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
1715 {
1716         struct ipsec_sa *sa;
1717
1718         /* For inline protocol processing, the metadata in the event will
1719          * uniquely identify the security session which raised the event.
1720          * Application would then need the userdata it had registered with the
1721          * security session to process the event.
1722          */
1723
1724         sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
1725
1726         if (sa == NULL) {
1727                 /* userdata could not be retrieved */
1728                 return -1;
1729         }
1730
1731         /* Sequence number over flow. SA need to be re-established */
1732         RTE_SET_USED(sa);
1733         return 0;
1734 }
1735
1736 static int
1737 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
1738                  void *param, void *ret_param)
1739 {
1740         uint64_t md;
1741         struct rte_eth_event_ipsec_desc *event_desc = NULL;
1742         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
1743                                         rte_eth_dev_get_sec_ctx(port_id);
1744
1745         RTE_SET_USED(param);
1746
1747         if (type != RTE_ETH_EVENT_IPSEC)
1748                 return -1;
1749
1750         event_desc = ret_param;
1751         if (event_desc == NULL) {
1752                 printf("Event descriptor not set\n");
1753                 return -1;
1754         }
1755
1756         md = event_desc->metadata;
1757
1758         if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
1759                 return inline_ipsec_event_esn_overflow(ctx, md);
1760         else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
1761                 printf("Invalid IPsec event reported\n");
1762                 return -1;
1763         }
1764
1765         return -1;
1766 }
1767
1768 int32_t
1769 main(int32_t argc, char **argv)
1770 {
1771         int32_t ret;
1772         uint32_t lcore_id;
1773         uint8_t socket_id;
1774         uint16_t portid;
1775
1776         /* init EAL */
1777         ret = rte_eal_init(argc, argv);
1778         if (ret < 0)
1779                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1780         argc -= ret;
1781         argv += ret;
1782
1783         /* parse application arguments (after the EAL ones) */
1784         ret = parse_args(argc, argv);
1785         if (ret < 0)
1786                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1787
1788         if ((unprotected_port_mask & enabled_port_mask) !=
1789                         unprotected_port_mask)
1790                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1791                                 unprotected_port_mask);
1792
1793         if (check_params() < 0)
1794                 rte_exit(EXIT_FAILURE, "check_params failed\n");
1795
1796         ret = init_lcore_rx_queues();
1797         if (ret < 0)
1798                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1799
1800         nb_lcores = rte_lcore_count();
1801
1802         /* Replicate each context per socket */
1803         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1804                 if (rte_lcore_is_enabled(lcore_id) == 0)
1805                         continue;
1806
1807                 if (numa_on)
1808                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1809                 else
1810                         socket_id = 0;
1811
1812                 if (socket_ctx[socket_id].mbuf_pool)
1813                         continue;
1814
1815                 sa_init(&socket_ctx[socket_id], socket_id);
1816
1817                 sp4_init(&socket_ctx[socket_id], socket_id);
1818
1819                 sp6_init(&socket_ctx[socket_id], socket_id);
1820
1821                 rt_init(&socket_ctx[socket_id], socket_id);
1822
1823                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1824         }
1825
1826         RTE_ETH_FOREACH_DEV(portid) {
1827                 if ((enabled_port_mask & (1 << portid)) == 0)
1828                         continue;
1829
1830                 port_init(portid);
1831         }
1832
1833         cryptodevs_init();
1834
1835         /* start ports */
1836         RTE_ETH_FOREACH_DEV(portid) {
1837                 if ((enabled_port_mask & (1 << portid)) == 0)
1838                         continue;
1839
1840                 /* Start device */
1841                 ret = rte_eth_dev_start(portid);
1842                 if (ret < 0)
1843                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1844                                         "err=%d, port=%d\n", ret, portid);
1845                 /*
1846                  * If enabled, put device in promiscuous mode.
1847                  * This allows IO forwarding mode to forward packets
1848                  * to itself through 2 cross-connected  ports of the
1849                  * target machine.
1850                  */
1851                 if (promiscuous_on)
1852                         rte_eth_promiscuous_enable(portid);
1853
1854                 rte_eth_dev_callback_register(portid,
1855                         RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
1856         }
1857
1858         check_all_ports_link_status(enabled_port_mask);
1859
1860         /* launch per-lcore init on every lcore */
1861         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1862         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1863                 if (rte_eal_wait_lcore(lcore_id) < 0)
1864                         return -1;
1865         }
1866
1867         return 0;
1868 }