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