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