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