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