New upstream version 17.11.4
[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                 ip->ip_sum = 0;
306                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
307         } else {
308                 pkt->ol_flags |= PKT_TX_IPV6;
309                 pkt->l3_len = sizeof(struct ip6_hdr);
310                 pkt->l2_len = ETHER_HDR_LEN;
311
312                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
313         }
314
315         memcpy(&ethhdr->s_addr, &ethaddr_tbl[port].src,
316                         sizeof(struct ether_addr));
317         memcpy(&ethhdr->d_addr, &ethaddr_tbl[port].dst,
318                         sizeof(struct ether_addr));
319 }
320
321 static inline void
322 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint16_t port)
323 {
324         int32_t i;
325         const int32_t prefetch_offset = 2;
326
327         for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
328                 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
329                 prepare_tx_pkt(pkts[i], port);
330         }
331         /* Process left packets */
332         for (; i < nb_pkts; i++)
333                 prepare_tx_pkt(pkts[i], port);
334 }
335
336 /* Send burst of packets on an output interface */
337 static inline int32_t
338 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
339 {
340         struct rte_mbuf **m_table;
341         int32_t ret;
342         uint16_t queueid;
343
344         queueid = qconf->tx_queue_id[port];
345         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
346
347         prepare_tx_burst(m_table, n, port);
348
349         ret = rte_eth_tx_burst(port, queueid, m_table, n);
350         if (unlikely(ret < n)) {
351                 do {
352                         rte_pktmbuf_free(m_table[ret]);
353                 } while (++ret < n);
354         }
355
356         return 0;
357 }
358
359 /* Enqueue a single packet, and send burst if queue is filled */
360 static inline int32_t
361 send_single_packet(struct rte_mbuf *m, uint16_t port)
362 {
363         uint32_t lcore_id;
364         uint16_t len;
365         struct lcore_conf *qconf;
366
367         lcore_id = rte_lcore_id();
368
369         qconf = &lcore_conf[lcore_id];
370         len = qconf->tx_mbufs[port].len;
371         qconf->tx_mbufs[port].m_table[len] = m;
372         len++;
373
374         /* enough pkts to be sent */
375         if (unlikely(len == MAX_PKT_BURST)) {
376                 send_burst(qconf, MAX_PKT_BURST, port);
377                 len = 0;
378         }
379
380         qconf->tx_mbufs[port].len = len;
381         return 0;
382 }
383
384 static inline void
385 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
386                 uint16_t lim)
387 {
388         struct rte_mbuf *m;
389         uint32_t i, j, res, sa_idx;
390
391         if (ip->num == 0 || sp == NULL)
392                 return;
393
394         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
395                         ip->num, DEFAULT_MAX_CATEGORIES);
396
397         j = 0;
398         for (i = 0; i < ip->num; i++) {
399                 m = ip->pkts[i];
400                 res = ip->res[i];
401                 if (res & BYPASS) {
402                         ip->pkts[j++] = m;
403                         continue;
404                 }
405                 if (res & DISCARD || i < lim) {
406                         rte_pktmbuf_free(m);
407                         continue;
408                 }
409                 /* Only check SPI match for processed IPSec packets */
410                 sa_idx = ip->res[i] & PROTECT_MASK;
411                 if (sa_idx >= IPSEC_SA_MAX_ENTRIES ||
412                                 !inbound_sa_check(sa, m, sa_idx)) {
413                         rte_pktmbuf_free(m);
414                         continue;
415                 }
416                 ip->pkts[j++] = m;
417         }
418         ip->num = j;
419 }
420
421 static inline void
422 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
423                 struct ipsec_traffic *traffic)
424 {
425         struct rte_mbuf *m;
426         uint16_t idx, nb_pkts_in, i, n_ip4, n_ip6;
427
428         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
429                         traffic->ipsec.num, MAX_PKT_BURST);
430
431         n_ip4 = traffic->ip4.num;
432         n_ip6 = traffic->ip6.num;
433
434         /* SP/ACL Inbound check ipsec and ip4 */
435         for (i = 0; i < nb_pkts_in; i++) {
436                 m = traffic->ipsec.pkts[i];
437                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
438                 if (ip->ip_v == IPVERSION) {
439                         idx = traffic->ip4.num++;
440                         traffic->ip4.pkts[idx] = m;
441                         traffic->ip4.data[idx] = rte_pktmbuf_mtod_offset(m,
442                                         uint8_t *, offsetof(struct ip, ip_p));
443                 } else if (ip->ip_v == IP6_VERSION) {
444                         idx = traffic->ip6.num++;
445                         traffic->ip6.pkts[idx] = m;
446                         traffic->ip6.data[idx] = rte_pktmbuf_mtod_offset(m,
447                                         uint8_t *,
448                                         offsetof(struct ip6_hdr, ip6_nxt));
449                 } else
450                         rte_pktmbuf_free(m);
451         }
452
453         inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
454                         n_ip4);
455
456         inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
457                         n_ip6);
458 }
459
460 static inline void
461 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
462                 struct traffic_type *ipsec)
463 {
464         struct rte_mbuf *m;
465         uint32_t i, j, sa_idx;
466
467         if (ip->num == 0 || sp == NULL)
468                 return;
469
470         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
471                         ip->num, DEFAULT_MAX_CATEGORIES);
472
473         j = 0;
474         for (i = 0; i < ip->num; i++) {
475                 m = ip->pkts[i];
476                 sa_idx = ip->res[i] & PROTECT_MASK;
477                 if (ip->res[i] & DISCARD)
478                         rte_pktmbuf_free(m);
479                 else if (ip->res[i] & BYPASS)
480                         ip->pkts[j++] = m;
481                 else if (sa_idx < IPSEC_SA_MAX_ENTRIES) {
482                         ipsec->res[ipsec->num] = sa_idx;
483                         ipsec->pkts[ipsec->num++] = m;
484                 } else /* invalid SA idx */
485                         rte_pktmbuf_free(m);
486         }
487         ip->num = j;
488 }
489
490 static inline void
491 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
492                 struct ipsec_traffic *traffic)
493 {
494         struct rte_mbuf *m;
495         uint16_t idx, nb_pkts_out, i;
496
497         /* Drop any IPsec traffic from protected ports */
498         for (i = 0; i < traffic->ipsec.num; i++)
499                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
500
501         traffic->ipsec.num = 0;
502
503         outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
504
505         outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
506
507         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
508                         traffic->ipsec.res, traffic->ipsec.num,
509                         MAX_PKT_BURST);
510
511         for (i = 0; i < nb_pkts_out; i++) {
512                 m = traffic->ipsec.pkts[i];
513                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
514                 if (ip->ip_v == IPVERSION) {
515                         idx = traffic->ip4.num++;
516                         traffic->ip4.pkts[idx] = m;
517                 } else {
518                         idx = traffic->ip6.num++;
519                         traffic->ip6.pkts[idx] = m;
520                 }
521         }
522 }
523
524 static inline void
525 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
526                 struct ipsec_traffic *traffic)
527 {
528         struct rte_mbuf *m;
529         uint32_t nb_pkts_in, i, idx;
530
531         /* Drop any IPv4 traffic from unprotected ports */
532         for (i = 0; i < traffic->ip4.num; i++)
533                 rte_pktmbuf_free(traffic->ip4.pkts[i]);
534
535         traffic->ip4.num = 0;
536
537         /* Drop any IPv6 traffic from unprotected ports */
538         for (i = 0; i < traffic->ip6.num; i++)
539                 rte_pktmbuf_free(traffic->ip6.pkts[i]);
540
541         traffic->ip6.num = 0;
542
543         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
544                         traffic->ipsec.num, MAX_PKT_BURST);
545
546         for (i = 0; i < nb_pkts_in; i++) {
547                 m = traffic->ipsec.pkts[i];
548                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
549                 if (ip->ip_v == IPVERSION) {
550                         idx = traffic->ip4.num++;
551                         traffic->ip4.pkts[idx] = m;
552                 } else {
553                         idx = traffic->ip6.num++;
554                         traffic->ip6.pkts[idx] = m;
555                 }
556         }
557 }
558
559 static inline void
560 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
561                 struct ipsec_traffic *traffic)
562 {
563         struct rte_mbuf *m;
564         uint32_t nb_pkts_out, i;
565         struct ip *ip;
566
567         /* Drop any IPsec traffic from protected ports */
568         for (i = 0; i < traffic->ipsec.num; i++)
569                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
570
571         traffic->ipsec.num = 0;
572
573         for (i = 0; i < traffic->ip4.num; i++)
574                 traffic->ip4.res[i] = single_sa_idx;
575
576         for (i = 0; i < traffic->ip6.num; i++)
577                 traffic->ip6.res[i] = single_sa_idx;
578
579         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
580                         traffic->ip4.res, traffic->ip4.num,
581                         MAX_PKT_BURST);
582
583         /* They all sue the same SA (ip4 or ip6 tunnel) */
584         m = traffic->ipsec.pkts[i];
585         ip = rte_pktmbuf_mtod(m, struct ip *);
586         if (ip->ip_v == IPVERSION)
587                 traffic->ip4.num = nb_pkts_out;
588         else
589                 traffic->ip6.num = nb_pkts_out;
590 }
591
592 static inline int32_t
593 get_hop_for_offload_pkt(struct rte_mbuf *pkt, int is_ipv6)
594 {
595         struct ipsec_mbuf_metadata *priv;
596         struct ipsec_sa *sa;
597
598         priv = get_priv(pkt);
599
600         sa = priv->sa;
601         if (unlikely(sa == NULL)) {
602                 RTE_LOG(ERR, IPSEC, "SA not saved in private data\n");
603                 goto fail;
604         }
605
606         if (is_ipv6)
607                 return sa->portid;
608
609         /* else */
610         return (sa->portid | RTE_LPM_LOOKUP_SUCCESS);
611
612 fail:
613         if (is_ipv6)
614                 return -1;
615
616         /* else */
617         return 0;
618 }
619
620 static inline void
621 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
622 {
623         uint32_t hop[MAX_PKT_BURST * 2];
624         uint32_t dst_ip[MAX_PKT_BURST * 2];
625         int32_t pkt_hop = 0;
626         uint16_t i, offset;
627         uint16_t lpm_pkts = 0;
628
629         if (nb_pkts == 0)
630                 return;
631
632         /* Need to do an LPM lookup for non-inline packets. Inline packets will
633          * have port ID in the SA
634          */
635
636         for (i = 0; i < nb_pkts; i++) {
637                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
638                         /* Security offload not enabled. So an LPM lookup is
639                          * required to get the hop
640                          */
641                         offset = offsetof(struct ip, ip_dst);
642                         dst_ip[lpm_pkts] = *rte_pktmbuf_mtod_offset(pkts[i],
643                                         uint32_t *, offset);
644                         dst_ip[lpm_pkts] = rte_be_to_cpu_32(dst_ip[lpm_pkts]);
645                         lpm_pkts++;
646                 }
647         }
648
649         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, lpm_pkts);
650
651         lpm_pkts = 0;
652
653         for (i = 0; i < nb_pkts; i++) {
654                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
655                         /* Read hop from the SA */
656                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 0);
657                 } else {
658                         /* Need to use hop returned by lookup */
659                         pkt_hop = hop[lpm_pkts++];
660                 }
661
662                 if ((pkt_hop & RTE_LPM_LOOKUP_SUCCESS) == 0) {
663                         rte_pktmbuf_free(pkts[i]);
664                         continue;
665                 }
666                 send_single_packet(pkts[i], pkt_hop & 0xff);
667         }
668 }
669
670 static inline void
671 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
672 {
673         int32_t hop[MAX_PKT_BURST * 2];
674         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
675         uint8_t *ip6_dst;
676         int32_t pkt_hop = 0;
677         uint16_t i, offset;
678         uint16_t lpm_pkts = 0;
679
680         if (nb_pkts == 0)
681                 return;
682
683         /* Need to do an LPM lookup for non-inline packets. Inline packets will
684          * have port ID in the SA
685          */
686
687         for (i = 0; i < nb_pkts; i++) {
688                 if (!(pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD)) {
689                         /* Security offload not enabled. So an LPM lookup is
690                          * required to get the hop
691                          */
692                         offset = offsetof(struct ip6_hdr, ip6_dst);
693                         ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *,
694                                         offset);
695                         memcpy(&dst_ip[lpm_pkts][0], ip6_dst, 16);
696                         lpm_pkts++;
697                 }
698         }
699
700         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip, hop,
701                         lpm_pkts);
702
703         lpm_pkts = 0;
704
705         for (i = 0; i < nb_pkts; i++) {
706                 if (pkts[i]->ol_flags & PKT_TX_SEC_OFFLOAD) {
707                         /* Read hop from the SA */
708                         pkt_hop = get_hop_for_offload_pkt(pkts[i], 1);
709                 } else {
710                         /* Need to use hop returned by lookup */
711                         pkt_hop = hop[lpm_pkts++];
712                 }
713
714                 if (pkt_hop == -1) {
715                         rte_pktmbuf_free(pkts[i]);
716                         continue;
717                 }
718                 send_single_packet(pkts[i], pkt_hop & 0xff);
719         }
720 }
721
722 static inline void
723 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
724                 uint8_t nb_pkts, uint16_t portid)
725 {
726         struct ipsec_traffic traffic;
727
728         prepare_traffic(pkts, &traffic, nb_pkts);
729
730         if (unlikely(single_sa)) {
731                 if (UNPROTECTED_PORT(portid))
732                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
733                 else
734                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
735         } else {
736                 if (UNPROTECTED_PORT(portid))
737                         process_pkts_inbound(&qconf->inbound, &traffic);
738                 else
739                         process_pkts_outbound(&qconf->outbound, &traffic);
740         }
741
742         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
743         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
744 }
745
746 static inline void
747 drain_buffers(struct lcore_conf *qconf)
748 {
749         struct buffer *buf;
750         uint32_t portid;
751
752         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
753                 buf = &qconf->tx_mbufs[portid];
754                 if (buf->len == 0)
755                         continue;
756                 send_burst(qconf, buf->len, portid);
757                 buf->len = 0;
758         }
759 }
760
761 /* main processing loop */
762 static int32_t
763 main_loop(__attribute__((unused)) void *dummy)
764 {
765         struct rte_mbuf *pkts[MAX_PKT_BURST];
766         uint32_t lcore_id;
767         uint64_t prev_tsc, diff_tsc, cur_tsc;
768         int32_t i, nb_rx;
769         uint16_t portid;
770         uint8_t queueid;
771         struct lcore_conf *qconf;
772         int32_t socket_id;
773         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
774                         / US_PER_S * BURST_TX_DRAIN_US;
775         struct lcore_rx_queue *rxql;
776
777         prev_tsc = 0;
778         lcore_id = rte_lcore_id();
779         qconf = &lcore_conf[lcore_id];
780         rxql = qconf->rx_queue_list;
781         socket_id = rte_lcore_to_socket_id(lcore_id);
782
783         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
784         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
785         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
786         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
787         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
788         qconf->inbound.cdev_map = cdev_map_in;
789         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
790         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
791         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
792         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
793         qconf->outbound.cdev_map = cdev_map_out;
794         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
795
796         if (qconf->nb_rx_queue == 0) {
797                 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
798                 return 0;
799         }
800
801         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
802
803         for (i = 0; i < qconf->nb_rx_queue; i++) {
804                 portid = rxql[i].port_id;
805                 queueid = rxql[i].queue_id;
806                 RTE_LOG(INFO, IPSEC,
807                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
808                         lcore_id, portid, queueid);
809         }
810
811         while (1) {
812                 cur_tsc = rte_rdtsc();
813
814                 /* TX queue buffer drain */
815                 diff_tsc = cur_tsc - prev_tsc;
816
817                 if (unlikely(diff_tsc > drain_tsc)) {
818                         drain_buffers(qconf);
819                         prev_tsc = cur_tsc;
820                 }
821
822                 /* Read packet from RX queues */
823                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
824                         portid = rxql[i].port_id;
825                         queueid = rxql[i].queue_id;
826                         nb_rx = rte_eth_rx_burst(portid, queueid,
827                                         pkts, MAX_PKT_BURST);
828
829                         if (nb_rx > 0)
830                                 process_pkts(qconf, pkts, nb_rx, portid);
831                 }
832         }
833 }
834
835 static int32_t
836 check_params(void)
837 {
838         uint8_t lcore;
839         uint16_t portid, nb_ports;
840         uint16_t i;
841         int32_t socket_id;
842
843         if (lcore_params == NULL) {
844                 printf("Error: No port/queue/core mappings\n");
845                 return -1;
846         }
847
848         nb_ports = rte_eth_dev_count();
849
850         for (i = 0; i < nb_lcore_params; ++i) {
851                 lcore = lcore_params[i].lcore_id;
852                 if (!rte_lcore_is_enabled(lcore)) {
853                         printf("error: lcore %hhu is not enabled in "
854                                 "lcore mask\n", lcore);
855                         return -1;
856                 }
857                 socket_id = rte_lcore_to_socket_id(lcore);
858                 if (socket_id != 0 && numa_on == 0) {
859                         printf("warning: lcore %hhu is on socket %d "
860                                 "with numa off\n",
861                                 lcore, socket_id);
862                 }
863                 portid = lcore_params[i].port_id;
864                 if ((enabled_port_mask & (1 << portid)) == 0) {
865                         printf("port %u is not enabled in port mask\n", portid);
866                         return -1;
867                 }
868                 if (portid >= nb_ports) {
869                         printf("port %u is not present on the board\n", portid);
870                         return -1;
871                 }
872         }
873         return 0;
874 }
875
876 static uint8_t
877 get_port_nb_rx_queues(const uint16_t port)
878 {
879         int32_t queue = -1;
880         uint16_t i;
881
882         for (i = 0; i < nb_lcore_params; ++i) {
883                 if (lcore_params[i].port_id == port &&
884                                 lcore_params[i].queue_id > queue)
885                         queue = lcore_params[i].queue_id;
886         }
887         return (uint8_t)(++queue);
888 }
889
890 static int32_t
891 init_lcore_rx_queues(void)
892 {
893         uint16_t i, nb_rx_queue;
894         uint8_t lcore;
895
896         for (i = 0; i < nb_lcore_params; ++i) {
897                 lcore = lcore_params[i].lcore_id;
898                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
899                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
900                         printf("error: too many queues (%u) for lcore: %u\n",
901                                         nb_rx_queue + 1, lcore);
902                         return -1;
903                 }
904                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
905                         lcore_params[i].port_id;
906                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
907                         lcore_params[i].queue_id;
908                 lcore_conf[lcore].nb_rx_queue++;
909         }
910         return 0;
911 }
912
913 /* display usage */
914 static void
915 print_usage(const char *prgname)
916 {
917         printf("%s [EAL options] -- -p PORTMASK -P -u PORTMASK"
918                 "  --"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]"
919                 " --single-sa SAIDX -f CONFIG_FILE\n"
920                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
921                 "  -P : enable promiscuous mode\n"
922                 "  -u PORTMASK: hexadecimal bitmask of unprotected ports\n"
923                 "  -j FRAMESIZE: jumbo frame maximum size\n"
924                 "  --"OPTION_CONFIG": (port,queue,lcore): "
925                 "rx queues configuration\n"
926                 "  --single-sa SAIDX: use single SA index for outbound, "
927                 "bypassing the SP\n"
928                 "  -f CONFIG_FILE: Configuration file path\n",
929                 prgname);
930 }
931
932 static int32_t
933 parse_portmask(const char *portmask)
934 {
935         char *end = NULL;
936         unsigned long pm;
937
938         /* parse hexadecimal string */
939         pm = strtoul(portmask, &end, 16);
940         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
941                 return -1;
942
943         if ((pm == 0) && errno)
944                 return -1;
945
946         return pm;
947 }
948
949 static int32_t
950 parse_decimal(const char *str)
951 {
952         char *end = NULL;
953         unsigned long num;
954
955         num = strtoul(str, &end, 10);
956         if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
957                 return -1;
958
959         return num;
960 }
961
962 static int32_t
963 parse_config(const char *q_arg)
964 {
965         char s[256];
966         const char *p, *p0 = q_arg;
967         char *end;
968         enum fieldnames {
969                 FLD_PORT = 0,
970                 FLD_QUEUE,
971                 FLD_LCORE,
972                 _NUM_FLD
973         };
974         unsigned long int_fld[_NUM_FLD];
975         char *str_fld[_NUM_FLD];
976         int32_t i;
977         uint32_t size;
978
979         nb_lcore_params = 0;
980
981         while ((p = strchr(p0, '(')) != NULL) {
982                 ++p;
983                 p0 = strchr(p, ')');
984                 if (p0 == NULL)
985                         return -1;
986
987                 size = p0 - p;
988                 if (size >= sizeof(s))
989                         return -1;
990
991                 snprintf(s, sizeof(s), "%.*s", size, p);
992                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
993                                 _NUM_FLD)
994                         return -1;
995                 for (i = 0; i < _NUM_FLD; i++) {
996                         errno = 0;
997                         int_fld[i] = strtoul(str_fld[i], &end, 0);
998                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
999                                 return -1;
1000                 }
1001                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1002                         printf("exceeded max number of lcore params: %hu\n",
1003                                 nb_lcore_params);
1004                         return -1;
1005                 }
1006                 lcore_params_array[nb_lcore_params].port_id =
1007                         (uint8_t)int_fld[FLD_PORT];
1008                 lcore_params_array[nb_lcore_params].queue_id =
1009                         (uint8_t)int_fld[FLD_QUEUE];
1010                 lcore_params_array[nb_lcore_params].lcore_id =
1011                         (uint8_t)int_fld[FLD_LCORE];
1012                 ++nb_lcore_params;
1013         }
1014         lcore_params = lcore_params_array;
1015         return 0;
1016 }
1017
1018 #define __STRNCMP(name, opt) (!strncmp(name, opt, sizeof(opt)))
1019 static int32_t
1020 parse_args_long_options(struct option *lgopts, int32_t option_index)
1021 {
1022         int32_t ret = -1;
1023         const char *optname = lgopts[option_index].name;
1024
1025         if (__STRNCMP(optname, OPTION_CONFIG)) {
1026                 ret = parse_config(optarg);
1027                 if (ret)
1028                         printf("invalid config\n");
1029         }
1030
1031         if (__STRNCMP(optname, OPTION_SINGLE_SA)) {
1032                 ret = parse_decimal(optarg);
1033                 if (ret != -1) {
1034                         single_sa = 1;
1035                         single_sa_idx = ret;
1036                         printf("Configured with single SA index %u\n",
1037                                         single_sa_idx);
1038                         ret = 0;
1039                 }
1040         }
1041
1042         return ret;
1043 }
1044 #undef __STRNCMP
1045
1046 static int32_t
1047 parse_args(int32_t argc, char **argv)
1048 {
1049         int32_t opt, ret;
1050         char **argvopt;
1051         int32_t option_index;
1052         char *prgname = argv[0];
1053         static struct option lgopts[] = {
1054                 {OPTION_CONFIG, 1, 0, 0},
1055                 {OPTION_SINGLE_SA, 1, 0, 0},
1056                 {NULL, 0, 0, 0}
1057         };
1058         int32_t f_present = 0;
1059
1060         argvopt = argv;
1061
1062         while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:",
1063                                 lgopts, &option_index)) != EOF) {
1064
1065                 switch (opt) {
1066                 case 'p':
1067                         enabled_port_mask = parse_portmask(optarg);
1068                         if (enabled_port_mask == 0) {
1069                                 printf("invalid portmask\n");
1070                                 print_usage(prgname);
1071                                 return -1;
1072                         }
1073                         break;
1074                 case 'P':
1075                         printf("Promiscuous mode selected\n");
1076                         promiscuous_on = 1;
1077                         break;
1078                 case 'u':
1079                         unprotected_port_mask = parse_portmask(optarg);
1080                         if (unprotected_port_mask == 0) {
1081                                 printf("invalid unprotected portmask\n");
1082                                 print_usage(prgname);
1083                                 return -1;
1084                         }
1085                         break;
1086                 case 'f':
1087                         if (f_present == 1) {
1088                                 printf("\"-f\" option present more than "
1089                                         "once!\n");
1090                                 print_usage(prgname);
1091                                 return -1;
1092                         }
1093                         if (parse_cfg_file(optarg) < 0) {
1094                                 printf("parsing file \"%s\" failed\n",
1095                                         optarg);
1096                                 print_usage(prgname);
1097                                 return -1;
1098                         }
1099                         f_present = 1;
1100                         break;
1101                 case 'j':
1102                         {
1103                                 int32_t size = parse_decimal(optarg);
1104                                 if (size <= 1518) {
1105                                         printf("Invalid jumbo frame size\n");
1106                                         if (size < 0) {
1107                                                 print_usage(prgname);
1108                                                 return -1;
1109                                         }
1110                                         printf("Using default value 9000\n");
1111                                         frame_size = 9000;
1112                                 } else {
1113                                         frame_size = size;
1114                                 }
1115                         }
1116                         printf("Enabled jumbo frames size %u\n", frame_size);
1117                         break;
1118                 case 0:
1119                         if (parse_args_long_options(lgopts, option_index)) {
1120                                 print_usage(prgname);
1121                                 return -1;
1122                         }
1123                         break;
1124                 default:
1125                         print_usage(prgname);
1126                         return -1;
1127                 }
1128         }
1129
1130         if (f_present == 0) {
1131                 printf("Mandatory option \"-f\" not present\n");
1132                 return -1;
1133         }
1134
1135         if (optind >= 0)
1136                 argv[optind-1] = prgname;
1137
1138         ret = optind-1;
1139         optind = 1; /* reset getopt lib */
1140         return ret;
1141 }
1142
1143 static void
1144 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1145 {
1146         char buf[ETHER_ADDR_FMT_SIZE];
1147         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1148         printf("%s%s", name, buf);
1149 }
1150
1151 /* Check the link status of all ports in up to 9s, and print them finally */
1152 static void
1153 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1154 {
1155 #define CHECK_INTERVAL 100 /* 100ms */
1156 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1157         uint16_t portid;
1158         uint8_t count, all_ports_up, print_flag = 0;
1159         struct rte_eth_link link;
1160
1161         printf("\nChecking link status");
1162         fflush(stdout);
1163         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1164                 all_ports_up = 1;
1165                 for (portid = 0; portid < port_num; portid++) {
1166                         if ((port_mask & (1 << portid)) == 0)
1167                                 continue;
1168                         memset(&link, 0, sizeof(link));
1169                         rte_eth_link_get_nowait(portid, &link);
1170                         /* print link status if flag set */
1171                         if (print_flag == 1) {
1172                                 if (link.link_status)
1173                                         printf(
1174                                         "Port%d Link Up - speed %u Mbps -%s\n",
1175                                                 portid, link.link_speed,
1176                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1177                                         ("full-duplex") : ("half-duplex\n"));
1178                                 else
1179                                         printf("Port %d Link Down\n", portid);
1180                                 continue;
1181                         }
1182                         /* clear all_ports_up flag if any link down */
1183                         if (link.link_status == ETH_LINK_DOWN) {
1184                                 all_ports_up = 0;
1185                                 break;
1186                         }
1187                 }
1188                 /* after finally printing all link status, get out */
1189                 if (print_flag == 1)
1190                         break;
1191
1192                 if (all_ports_up == 0) {
1193                         printf(".");
1194                         fflush(stdout);
1195                         rte_delay_ms(CHECK_INTERVAL);
1196                 }
1197
1198                 /* set the print_flag if all ports up or timeout */
1199                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1200                         print_flag = 1;
1201                         printf("done\n");
1202                 }
1203         }
1204 }
1205
1206 static int32_t
1207 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1208                 uint16_t qp, struct lcore_params *params,
1209                 struct ipsec_ctx *ipsec_ctx,
1210                 const struct rte_cryptodev_capabilities *cipher,
1211                 const struct rte_cryptodev_capabilities *auth,
1212                 const struct rte_cryptodev_capabilities *aead)
1213 {
1214         int32_t ret = 0;
1215         unsigned long i;
1216         struct cdev_key key = { 0 };
1217
1218         key.lcore_id = params->lcore_id;
1219         if (cipher)
1220                 key.cipher_algo = cipher->sym.cipher.algo;
1221         if (auth)
1222                 key.auth_algo = auth->sym.auth.algo;
1223         if (aead)
1224                 key.aead_algo = aead->sym.aead.algo;
1225
1226         ret = rte_hash_lookup(map, &key);
1227         if (ret != -ENOENT)
1228                 return 0;
1229
1230         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1231                 if (ipsec_ctx->tbl[i].id == cdev_id)
1232                         break;
1233
1234         if (i == ipsec_ctx->nb_qps) {
1235                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1236                         printf("Maximum number of crypto devices assigned to "
1237                                 "a core, increase MAX_QP_PER_LCORE value\n");
1238                         return 0;
1239                 }
1240                 ipsec_ctx->tbl[i].id = cdev_id;
1241                 ipsec_ctx->tbl[i].qp = qp;
1242                 ipsec_ctx->nb_qps++;
1243                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1244                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1245                                 cdev_id, qp, i);
1246         }
1247
1248         ret = rte_hash_add_key_data(map, &key, (void *)i);
1249         if (ret < 0) {
1250                 printf("Faled to insert cdev mapping for (lcore %u, "
1251                                 "cdev %u, qp %u), errno %d\n",
1252                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1253                                 ipsec_ctx->tbl[i].qp, ret);
1254                 return 0;
1255         }
1256
1257         return 1;
1258 }
1259
1260 static int32_t
1261 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1262                 uint16_t qp, struct lcore_params *params)
1263 {
1264         int32_t ret = 0;
1265         const struct rte_cryptodev_capabilities *i, *j;
1266         struct rte_hash *map;
1267         struct lcore_conf *qconf;
1268         struct ipsec_ctx *ipsec_ctx;
1269         const char *str;
1270
1271         qconf = &lcore_conf[params->lcore_id];
1272
1273         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1274                 map = cdev_map_out;
1275                 ipsec_ctx = &qconf->outbound;
1276                 str = "Outbound";
1277         } else {
1278                 map = cdev_map_in;
1279                 ipsec_ctx = &qconf->inbound;
1280                 str = "Inbound";
1281         }
1282
1283         /* Required cryptodevs with operation chainning */
1284         if (!(dev_info->feature_flags &
1285                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1286                 return ret;
1287
1288         for (i = dev_info->capabilities;
1289                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1290                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1291                         continue;
1292
1293                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1294                         ret |= add_mapping(map, str, cdev_id, qp, params,
1295                                         ipsec_ctx, NULL, NULL, i);
1296                         continue;
1297                 }
1298
1299                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1300                         continue;
1301
1302                 for (j = dev_info->capabilities;
1303                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1304                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1305                                 continue;
1306
1307                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1308                                 continue;
1309
1310                         ret |= add_mapping(map, str, cdev_id, qp, params,
1311                                                 ipsec_ctx, i, j, NULL);
1312                 }
1313         }
1314
1315         return ret;
1316 }
1317
1318 static int32_t
1319 cryptodevs_init(void)
1320 {
1321         struct rte_cryptodev_config dev_conf;
1322         struct rte_cryptodev_qp_conf qp_conf;
1323         uint16_t idx, max_nb_qps, qp, i;
1324         int16_t cdev_id;
1325         struct rte_hash_parameters params = { 0 };
1326
1327         params.entries = CDEV_MAP_ENTRIES;
1328         params.key_len = sizeof(struct cdev_key);
1329         params.hash_func = rte_jhash;
1330         params.hash_func_init_val = 0;
1331         params.socket_id = rte_socket_id();
1332
1333         params.name = "cdev_map_in";
1334         cdev_map_in = rte_hash_create(&params);
1335         if (cdev_map_in == NULL)
1336                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1337                                 rte_errno);
1338
1339         params.name = "cdev_map_out";
1340         cdev_map_out = rte_hash_create(&params);
1341         if (cdev_map_out == NULL)
1342                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1343                                 rte_errno);
1344
1345         printf("lcore/cryptodev/qp mappings:\n");
1346
1347         uint32_t max_sess_sz = 0, sess_sz;
1348         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1349                 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1350                 if (sess_sz > max_sess_sz)
1351                         max_sess_sz = sess_sz;
1352         }
1353
1354         idx = 0;
1355         /* Start from last cdev id to give HW priority */
1356         for (cdev_id = rte_cryptodev_count() - 1; cdev_id >= 0; cdev_id--) {
1357                 struct rte_cryptodev_info cdev_info;
1358
1359                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1360
1361                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1362                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1363                 else
1364                         max_nb_qps = nb_lcore_params;
1365
1366                 qp = 0;
1367                 i = 0;
1368                 while (qp < max_nb_qps && i < nb_lcore_params) {
1369                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1370                                                 &lcore_params[idx]))
1371                                 qp++;
1372                         idx++;
1373                         idx = idx % nb_lcore_params;
1374                         i++;
1375                 }
1376
1377                 if (qp == 0)
1378                         continue;
1379
1380                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1381                 dev_conf.nb_queue_pairs = qp;
1382
1383                 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1384                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1385                         struct rte_mempool *sess_mp;
1386
1387                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1388                                         "sess_mp_%u", dev_conf.socket_id);
1389                         sess_mp = rte_mempool_create(mp_name,
1390                                         CDEV_MP_NB_OBJS,
1391                                         max_sess_sz,
1392                                         CDEV_MP_CACHE_SZ,
1393                                         0, NULL, NULL, NULL,
1394                                         NULL, dev_conf.socket_id,
1395                                         0);
1396                         if (sess_mp == NULL)
1397                                 rte_exit(EXIT_FAILURE,
1398                                         "Cannot create session pool on socket %d\n",
1399                                         dev_conf.socket_id);
1400                         else
1401                                 printf("Allocated session pool on socket %d\n",
1402                                         dev_conf.socket_id);
1403                         socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1404                 }
1405
1406                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1407                         rte_panic("Failed to initialize cryptodev %u\n",
1408                                         cdev_id);
1409
1410                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1411                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1412                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1413                                         &qp_conf, dev_conf.socket_id,
1414                                         socket_ctx[dev_conf.socket_id].session_pool))
1415                                 rte_panic("Failed to setup queue %u for "
1416                                                 "cdev_id %u\n", 0, cdev_id);
1417
1418                 if (rte_cryptodev_start(cdev_id))
1419                         rte_panic("Failed to start cryptodev %u\n",
1420                                         cdev_id);
1421         }
1422
1423         printf("\n");
1424
1425         return 0;
1426 }
1427
1428 static void
1429 port_init(uint16_t portid)
1430 {
1431         struct rte_eth_dev_info dev_info;
1432         struct rte_eth_txconf *txconf;
1433         uint16_t nb_tx_queue, nb_rx_queue;
1434         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1435         int32_t ret, socket_id;
1436         struct lcore_conf *qconf;
1437         struct ether_addr ethaddr;
1438
1439         rte_eth_dev_info_get(portid, &dev_info);
1440
1441         printf("Configuring device port %u:\n", portid);
1442
1443         rte_eth_macaddr_get(portid, &ethaddr);
1444         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1445         print_ethaddr("Address: ", &ethaddr);
1446         printf("\n");
1447
1448         nb_rx_queue = get_port_nb_rx_queues(portid);
1449         nb_tx_queue = nb_lcores;
1450
1451         if (nb_rx_queue > dev_info.max_rx_queues)
1452                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1453                                 "(max rx queue is %u)\n",
1454                                 nb_rx_queue, dev_info.max_rx_queues);
1455
1456         if (nb_tx_queue > dev_info.max_tx_queues)
1457                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1458                                 "(max tx queue is %u)\n",
1459                                 nb_tx_queue, dev_info.max_tx_queues);
1460
1461         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1462                         nb_rx_queue, nb_tx_queue);
1463
1464         if (frame_size) {
1465                 port_conf.rxmode.max_rx_pkt_len = frame_size;
1466                 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1467         }
1468
1469         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY)
1470                 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SECURITY;
1471         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SECURITY)
1472                 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_SECURITY;
1473
1474         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1475                         &port_conf);
1476         if (ret < 0)
1477                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1478                                 "err=%d, port=%d\n", ret, portid);
1479
1480         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1481         if (ret < 0)
1482                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1483                                 "err=%d, port=%d\n", ret, portid);
1484
1485         /* init one TX queue per lcore */
1486         tx_queueid = 0;
1487         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1488                 if (rte_lcore_is_enabled(lcore_id) == 0)
1489                         continue;
1490
1491                 if (numa_on)
1492                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1493                 else
1494                         socket_id = 0;
1495
1496                 /* init TX queue */
1497                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1498
1499                 txconf = &dev_info.default_txconf;
1500                 txconf->txq_flags = 0;
1501
1502                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1503                                 socket_id, txconf);
1504                 if (ret < 0)
1505                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1506                                         "err=%d, port=%d\n", ret, portid);
1507
1508                 qconf = &lcore_conf[lcore_id];
1509                 qconf->tx_queue_id[portid] = tx_queueid;
1510                 tx_queueid++;
1511
1512                 /* init RX queues */
1513                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1514                         if (portid != qconf->rx_queue_list[queue].port_id)
1515                                 continue;
1516
1517                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
1518
1519                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1520                                         socket_id);
1521
1522                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1523                                         nb_rxd, socket_id, NULL,
1524                                         socket_ctx[socket_id].mbuf_pool);
1525                         if (ret < 0)
1526                                 rte_exit(EXIT_FAILURE,
1527                                         "rte_eth_rx_queue_setup: err=%d, "
1528                                         "port=%d\n", ret, portid);
1529                 }
1530         }
1531         printf("\n");
1532 }
1533
1534 static void
1535 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1536 {
1537         char s[64];
1538         uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) :
1539                         RTE_MBUF_DEFAULT_BUF_SIZE;
1540
1541
1542         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1543         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1544                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1545                         buff_size,
1546                         socket_id);
1547         if (ctx->mbuf_pool == NULL)
1548                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1549                                 socket_id);
1550         else
1551                 printf("Allocated mbuf pool on socket %d\n", socket_id);
1552 }
1553
1554 int32_t
1555 main(int32_t argc, char **argv)
1556 {
1557         int32_t ret;
1558         uint32_t lcore_id;
1559         uint8_t socket_id;
1560         uint16_t portid, nb_ports;
1561
1562         /* init EAL */
1563         ret = rte_eal_init(argc, argv);
1564         if (ret < 0)
1565                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1566         argc -= ret;
1567         argv += ret;
1568
1569         /* parse application arguments (after the EAL ones) */
1570         ret = parse_args(argc, argv);
1571         if (ret < 0)
1572                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1573
1574         if ((unprotected_port_mask & enabled_port_mask) !=
1575                         unprotected_port_mask)
1576                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1577                                 unprotected_port_mask);
1578
1579         nb_ports = rte_eth_dev_count();
1580
1581         if (check_params() < 0)
1582                 rte_exit(EXIT_FAILURE, "check_params failed\n");
1583
1584         ret = init_lcore_rx_queues();
1585         if (ret < 0)
1586                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1587
1588         nb_lcores = rte_lcore_count();
1589
1590         /* Replicate each context per socket */
1591         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1592                 if (rte_lcore_is_enabled(lcore_id) == 0)
1593                         continue;
1594
1595                 if (numa_on)
1596                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1597                 else
1598                         socket_id = 0;
1599
1600                 if (socket_ctx[socket_id].mbuf_pool)
1601                         continue;
1602
1603                 sa_init(&socket_ctx[socket_id], socket_id);
1604
1605                 sp4_init(&socket_ctx[socket_id], socket_id);
1606
1607                 sp6_init(&socket_ctx[socket_id], socket_id);
1608
1609                 rt_init(&socket_ctx[socket_id], socket_id);
1610
1611                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1612         }
1613
1614         for (portid = 0; portid < nb_ports; portid++) {
1615                 if ((enabled_port_mask & (1 << portid)) == 0)
1616                         continue;
1617
1618                 port_init(portid);
1619         }
1620
1621         cryptodevs_init();
1622
1623         /* start ports */
1624         for (portid = 0; portid < nb_ports; portid++) {
1625                 if ((enabled_port_mask & (1 << portid)) == 0)
1626                         continue;
1627
1628                 /* Start device */
1629                 ret = rte_eth_dev_start(portid);
1630                 if (ret < 0)
1631                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1632                                         "err=%d, port=%d\n", ret, portid);
1633                 /*
1634                  * If enabled, put device in promiscuous mode.
1635                  * This allows IO forwarding mode to forward packets
1636                  * to itself through 2 cross-connected  ports of the
1637                  * target machine.
1638                  */
1639                 if (promiscuous_on)
1640                         rte_eth_promiscuous_enable(portid);
1641         }
1642
1643         check_all_ports_link_status(nb_ports, enabled_port_mask);
1644
1645         /* launch per-lcore init on every lcore */
1646         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1647         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1648                 if (rte_eal_wait_lcore(lcore_id) < 0)
1649                         return -1;
1650         }
1651
1652         return 0;
1653 }