examples/ipsec-secgw: support security offload
[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_pci.h>
61 #include <rte_random.h>
62 #include <rte_debug.h>
63 #include <rte_ether.h>
64 #include <rte_ethdev.h>
65 #include <rte_mempool.h>
66 #include <rte_mbuf.h>
67 #include <rte_acl.h>
68 #include <rte_lpm.h>
69 #include <rte_lpm6.h>
70 #include <rte_hash.h>
71 #include <rte_jhash.h>
72 #include <rte_cryptodev.h>
73
74 #include "ipsec.h"
75 #include "parser.h"
76
77 #define RTE_LOGTYPE_IPSEC RTE_LOGTYPE_USER1
78
79 #define MAX_JUMBO_PKT_LEN  9600
80
81 #define MEMPOOL_CACHE_SIZE 256
82
83 #define NB_MBUF (32000)
84
85 #define CDEV_QUEUE_DESC 2048
86 #define CDEV_MAP_ENTRIES 1024
87 #define CDEV_MP_NB_OBJS 2048
88 #define CDEV_MP_CACHE_SZ 64
89 #define MAX_QUEUE_PAIRS 1
90
91 #define OPTION_CONFIG           "config"
92 #define OPTION_SINGLE_SA        "single-sa"
93
94 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
95
96 #define NB_SOCKETS 4
97
98 /* Configure how many packets ahead to prefetch, when reading packets */
99 #define PREFETCH_OFFSET 3
100
101 #define MAX_RX_QUEUE_PER_LCORE 16
102
103 #define MAX_LCORE_PARAMS 1024
104
105 #define UNPROTECTED_PORT(port) (unprotected_port_mask & (1 << portid))
106
107 /*
108  * Configurable number of RX/TX ring descriptors
109  */
110 #define IPSEC_SECGW_RX_DESC_DEFAULT 128
111 #define IPSEC_SECGW_TX_DESC_DEFAULT 512
112 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
113 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
114
115 #if RTE_BYTE_ORDER != RTE_LITTLE_ENDIAN
116 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
117         (((uint64_t)((a) & 0xff) << 56) | \
118         ((uint64_t)((b) & 0xff) << 48) | \
119         ((uint64_t)((c) & 0xff) << 40) | \
120         ((uint64_t)((d) & 0xff) << 32) | \
121         ((uint64_t)((e) & 0xff) << 24) | \
122         ((uint64_t)((f) & 0xff) << 16) | \
123         ((uint64_t)((g) & 0xff) << 8)  | \
124         ((uint64_t)(h) & 0xff))
125 #else
126 #define __BYTES_TO_UINT64(a, b, c, d, e, f, g, h) \
127         (((uint64_t)((h) & 0xff) << 56) | \
128         ((uint64_t)((g) & 0xff) << 48) | \
129         ((uint64_t)((f) & 0xff) << 40) | \
130         ((uint64_t)((e) & 0xff) << 32) | \
131         ((uint64_t)((d) & 0xff) << 24) | \
132         ((uint64_t)((c) & 0xff) << 16) | \
133         ((uint64_t)((b) & 0xff) << 8) | \
134         ((uint64_t)(a) & 0xff))
135 #endif
136 #define ETHADDR(a, b, c, d, e, f) (__BYTES_TO_UINT64(a, b, c, d, e, f, 0, 0))
137
138 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
139                 addr.addr_bytes[0], addr.addr_bytes[1], \
140                 addr.addr_bytes[2], addr.addr_bytes[3], \
141                 addr.addr_bytes[4], addr.addr_bytes[5], \
142                 0, 0)
143
144 /* port/source ethernet addr and destination ethernet addr */
145 struct ethaddr_info {
146         uint64_t src, dst;
147 };
148
149 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
150         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
151         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
152         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
153         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
154 };
155
156 /* mask of enabled ports */
157 static uint32_t enabled_port_mask;
158 static uint32_t unprotected_port_mask;
159 static int32_t promiscuous_on = 1;
160 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
161 static uint32_t nb_lcores;
162 static uint32_t single_sa;
163 static uint32_t single_sa_idx;
164 static uint32_t frame_size;
165
166 struct lcore_rx_queue {
167         uint16_t port_id;
168         uint8_t queue_id;
169 } __rte_cache_aligned;
170
171 struct lcore_params {
172         uint16_t port_id;
173         uint8_t queue_id;
174         uint8_t lcore_id;
175 } __rte_cache_aligned;
176
177 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
178
179 static struct lcore_params *lcore_params;
180 static uint16_t nb_lcore_params;
181
182 static struct rte_hash *cdev_map_in;
183 static struct rte_hash *cdev_map_out;
184
185 struct buffer {
186         uint16_t len;
187         struct rte_mbuf *m_table[MAX_PKT_BURST] __rte_aligned(sizeof(void *));
188 };
189
190 struct lcore_conf {
191         uint16_t nb_rx_queue;
192         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
193         uint16_t tx_queue_id[RTE_MAX_ETHPORTS];
194         struct buffer tx_mbufs[RTE_MAX_ETHPORTS];
195         struct ipsec_ctx inbound;
196         struct ipsec_ctx outbound;
197         struct rt_ctx *rt4_ctx;
198         struct rt_ctx *rt6_ctx;
199 } __rte_cache_aligned;
200
201 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
202
203 static struct rte_eth_conf port_conf = {
204         .rxmode = {
205                 .mq_mode        = ETH_MQ_RX_RSS,
206                 .max_rx_pkt_len = ETHER_MAX_LEN,
207                 .split_hdr_size = 0,
208                 .offloads = DEV_RX_OFFLOAD_CHECKSUM |
209                             DEV_RX_OFFLOAD_CRC_STRIP,
210                 .ignore_offload_bitfield = 1,
211         },
212         .rx_adv_conf = {
213                 .rss_conf = {
214                         .rss_key = NULL,
215                         .rss_hf = ETH_RSS_IP | ETH_RSS_UDP |
216                                 ETH_RSS_TCP | ETH_RSS_SCTP,
217                 },
218         },
219         .txmode = {
220                 .mq_mode = ETH_MQ_TX_NONE,
221         },
222 };
223
224 static struct socket_ctx socket_ctx[NB_SOCKETS];
225
226 struct traffic_type {
227         const uint8_t *data[MAX_PKT_BURST * 2];
228         struct rte_mbuf *pkts[MAX_PKT_BURST * 2];
229         uint32_t res[MAX_PKT_BURST * 2];
230         uint32_t num;
231 };
232
233 struct ipsec_traffic {
234         struct traffic_type ipsec;
235         struct traffic_type ip4;
236         struct traffic_type ip6;
237 };
238
239 static inline void
240 prepare_one_packet(struct rte_mbuf *pkt, struct ipsec_traffic *t)
241 {
242         uint8_t *nlp;
243         struct ether_hdr *eth;
244
245         eth = rte_pktmbuf_mtod(pkt, struct ether_hdr *);
246         if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv4)) {
247                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
248                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip, ip_p));
249                 if (*nlp == IPPROTO_ESP)
250                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
251                 else {
252                         t->ip4.data[t->ip4.num] = nlp;
253                         t->ip4.pkts[(t->ip4.num)++] = pkt;
254                 }
255         } else if (eth->ether_type == rte_cpu_to_be_16(ETHER_TYPE_IPv6)) {
256                 nlp = (uint8_t *)rte_pktmbuf_adj(pkt, ETHER_HDR_LEN);
257                 nlp = RTE_PTR_ADD(nlp, offsetof(struct ip6_hdr, ip6_nxt));
258                 if (*nlp == IPPROTO_ESP)
259                         t->ipsec.pkts[(t->ipsec.num)++] = pkt;
260                 else {
261                         t->ip6.data[t->ip6.num] = nlp;
262                         t->ip6.pkts[(t->ip6.num)++] = pkt;
263                 }
264         } else {
265                 /* Unknown/Unsupported type, drop the packet */
266                 RTE_LOG(ERR, IPSEC, "Unsupported packet type\n");
267                 rte_pktmbuf_free(pkt);
268         }
269 }
270
271 static inline void
272 prepare_traffic(struct rte_mbuf **pkts, struct ipsec_traffic *t,
273                 uint16_t nb_pkts)
274 {
275         int32_t i;
276
277         t->ipsec.num = 0;
278         t->ip4.num = 0;
279         t->ip6.num = 0;
280
281         for (i = 0; i < (nb_pkts - PREFETCH_OFFSET); i++) {
282                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i + PREFETCH_OFFSET],
283                                         void *));
284                 prepare_one_packet(pkts[i], t);
285         }
286         /* Process left packets */
287         for (; i < nb_pkts; i++)
288                 prepare_one_packet(pkts[i], t);
289 }
290
291 static inline void
292 prepare_tx_pkt(struct rte_mbuf *pkt, uint16_t port)
293 {
294         struct ip *ip;
295         struct ether_hdr *ethhdr;
296
297         ip = rte_pktmbuf_mtod(pkt, struct ip *);
298
299         ethhdr = (struct ether_hdr *)rte_pktmbuf_prepend(pkt, ETHER_HDR_LEN);
300
301         if (ip->ip_v == IPVERSION) {
302                 pkt->ol_flags |= PKT_TX_IP_CKSUM | PKT_TX_IPV4;
303                 pkt->l3_len = sizeof(struct ip);
304                 pkt->l2_len = ETHER_HDR_LEN;
305
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 == 0 || !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] == 0) || (ip->res[i] & DISCARD))
477                         rte_pktmbuf_free(m);
478                 else if (sa_idx != 0) {
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 void
590 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
591 {
592         uint32_t hop[MAX_PKT_BURST * 2];
593         uint32_t dst_ip[MAX_PKT_BURST * 2];
594         uint16_t i, offset;
595
596         if (nb_pkts == 0)
597                 return;
598
599         for (i = 0; i < nb_pkts; i++) {
600                 offset = offsetof(struct ip, ip_dst);
601                 dst_ip[i] = *rte_pktmbuf_mtod_offset(pkts[i],
602                                 uint32_t *, offset);
603                 dst_ip[i] = rte_be_to_cpu_32(dst_ip[i]);
604         }
605
606         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, nb_pkts);
607
608         for (i = 0; i < nb_pkts; i++) {
609                 if ((hop[i] & RTE_LPM_LOOKUP_SUCCESS) == 0) {
610                         rte_pktmbuf_free(pkts[i]);
611                         continue;
612                 }
613                 send_single_packet(pkts[i], hop[i] & 0xff);
614         }
615 }
616
617 static inline void
618 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
619 {
620         int32_t hop[MAX_PKT_BURST * 2];
621         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
622         uint8_t *ip6_dst;
623         uint16_t i, offset;
624
625         if (nb_pkts == 0)
626                 return;
627
628         for (i = 0; i < nb_pkts; i++) {
629                 offset = offsetof(struct ip6_hdr, ip6_dst);
630                 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *, offset);
631                 memcpy(&dst_ip[i][0], ip6_dst, 16);
632         }
633
634         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip,
635                         hop, nb_pkts);
636
637         for (i = 0; i < nb_pkts; i++) {
638                 if (hop[i] == -1) {
639                         rte_pktmbuf_free(pkts[i]);
640                         continue;
641                 }
642                 send_single_packet(pkts[i], hop[i] & 0xff);
643         }
644 }
645
646 static inline void
647 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
648                 uint8_t nb_pkts, uint16_t portid)
649 {
650         struct ipsec_traffic traffic;
651
652         prepare_traffic(pkts, &traffic, nb_pkts);
653
654         if (unlikely(single_sa)) {
655                 if (UNPROTECTED_PORT(portid))
656                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
657                 else
658                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
659         } else {
660                 if (UNPROTECTED_PORT(portid))
661                         process_pkts_inbound(&qconf->inbound, &traffic);
662                 else
663                         process_pkts_outbound(&qconf->outbound, &traffic);
664         }
665
666         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
667         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
668 }
669
670 static inline void
671 drain_buffers(struct lcore_conf *qconf)
672 {
673         struct buffer *buf;
674         uint32_t portid;
675
676         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
677                 buf = &qconf->tx_mbufs[portid];
678                 if (buf->len == 0)
679                         continue;
680                 send_burst(qconf, buf->len, portid);
681                 buf->len = 0;
682         }
683 }
684
685 /* main processing loop */
686 static int32_t
687 main_loop(__attribute__((unused)) void *dummy)
688 {
689         struct rte_mbuf *pkts[MAX_PKT_BURST];
690         uint32_t lcore_id;
691         uint64_t prev_tsc, diff_tsc, cur_tsc;
692         int32_t i, nb_rx;
693         uint16_t portid;
694         uint8_t queueid;
695         struct lcore_conf *qconf;
696         int32_t socket_id;
697         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
698                         / US_PER_S * BURST_TX_DRAIN_US;
699         struct lcore_rx_queue *rxql;
700
701         prev_tsc = 0;
702         lcore_id = rte_lcore_id();
703         qconf = &lcore_conf[lcore_id];
704         rxql = qconf->rx_queue_list;
705         socket_id = rte_lcore_to_socket_id(lcore_id);
706
707         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
708         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
709         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
710         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
711         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
712         qconf->inbound.cdev_map = cdev_map_in;
713         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
714         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
715         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
716         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
717         qconf->outbound.cdev_map = cdev_map_out;
718         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
719
720         if (qconf->nb_rx_queue == 0) {
721                 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
722                 return 0;
723         }
724
725         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
726
727         for (i = 0; i < qconf->nb_rx_queue; i++) {
728                 portid = rxql[i].port_id;
729                 queueid = rxql[i].queue_id;
730                 RTE_LOG(INFO, IPSEC,
731                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
732                         lcore_id, portid, queueid);
733         }
734
735         while (1) {
736                 cur_tsc = rte_rdtsc();
737
738                 /* TX queue buffer drain */
739                 diff_tsc = cur_tsc - prev_tsc;
740
741                 if (unlikely(diff_tsc > drain_tsc)) {
742                         drain_buffers(qconf);
743                         prev_tsc = cur_tsc;
744                 }
745
746                 /* Read packet from RX queues */
747                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
748                         portid = rxql[i].port_id;
749                         queueid = rxql[i].queue_id;
750                         nb_rx = rte_eth_rx_burst(portid, queueid,
751                                         pkts, MAX_PKT_BURST);
752
753                         if (nb_rx > 0)
754                                 process_pkts(qconf, pkts, nb_rx, portid);
755                 }
756         }
757 }
758
759 static int32_t
760 check_params(void)
761 {
762         uint8_t lcore;
763         uint16_t portid, nb_ports;
764         uint16_t i;
765         int32_t socket_id;
766
767         if (lcore_params == NULL) {
768                 printf("Error: No port/queue/core mappings\n");
769                 return -1;
770         }
771
772         nb_ports = rte_eth_dev_count();
773
774         for (i = 0; i < nb_lcore_params; ++i) {
775                 lcore = lcore_params[i].lcore_id;
776                 if (!rte_lcore_is_enabled(lcore)) {
777                         printf("error: lcore %hhu is not enabled in "
778                                 "lcore mask\n", lcore);
779                         return -1;
780                 }
781                 socket_id = rte_lcore_to_socket_id(lcore);
782                 if (socket_id != 0 && numa_on == 0) {
783                         printf("warning: lcore %hhu is on socket %d "
784                                 "with numa off\n",
785                                 lcore, socket_id);
786                 }
787                 portid = lcore_params[i].port_id;
788                 if ((enabled_port_mask & (1 << portid)) == 0) {
789                         printf("port %u is not enabled in port mask\n", portid);
790                         return -1;
791                 }
792                 if (portid >= nb_ports) {
793                         printf("port %u is not present on the board\n", portid);
794                         return -1;
795                 }
796         }
797         return 0;
798 }
799
800 static uint8_t
801 get_port_nb_rx_queues(const uint16_t port)
802 {
803         int32_t queue = -1;
804         uint16_t i;
805
806         for (i = 0; i < nb_lcore_params; ++i) {
807                 if (lcore_params[i].port_id == port &&
808                                 lcore_params[i].queue_id > queue)
809                         queue = lcore_params[i].queue_id;
810         }
811         return (uint8_t)(++queue);
812 }
813
814 static int32_t
815 init_lcore_rx_queues(void)
816 {
817         uint16_t i, nb_rx_queue;
818         uint8_t lcore;
819
820         for (i = 0; i < nb_lcore_params; ++i) {
821                 lcore = lcore_params[i].lcore_id;
822                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
823                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
824                         printf("error: too many queues (%u) for lcore: %u\n",
825                                         nb_rx_queue + 1, lcore);
826                         return -1;
827                 }
828                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
829                         lcore_params[i].port_id;
830                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
831                         lcore_params[i].queue_id;
832                 lcore_conf[lcore].nb_rx_queue++;
833         }
834         return 0;
835 }
836
837 /* display usage */
838 static void
839 print_usage(const char *prgname)
840 {
841         printf("%s [EAL options] -- -p PORTMASK -P -u PORTMASK"
842                 "  --"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]"
843                 " --single-sa SAIDX -f CONFIG_FILE\n"
844                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
845                 "  -P : enable promiscuous mode\n"
846                 "  -u PORTMASK: hexadecimal bitmask of unprotected ports\n"
847                 "  -j FRAMESIZE: jumbo frame maximum size\n"
848                 "  --"OPTION_CONFIG": (port,queue,lcore): "
849                 "rx queues configuration\n"
850                 "  --single-sa SAIDX: use single SA index for outbound, "
851                 "bypassing the SP\n"
852                 "  -f CONFIG_FILE: Configuration file path\n",
853                 prgname);
854 }
855
856 static int32_t
857 parse_portmask(const char *portmask)
858 {
859         char *end = NULL;
860         unsigned long pm;
861
862         /* parse hexadecimal string */
863         pm = strtoul(portmask, &end, 16);
864         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
865                 return -1;
866
867         if ((pm == 0) && errno)
868                 return -1;
869
870         return pm;
871 }
872
873 static int32_t
874 parse_decimal(const char *str)
875 {
876         char *end = NULL;
877         unsigned long num;
878
879         num = strtoul(str, &end, 10);
880         if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
881                 return -1;
882
883         return num;
884 }
885
886 static int32_t
887 parse_config(const char *q_arg)
888 {
889         char s[256];
890         const char *p, *p0 = q_arg;
891         char *end;
892         enum fieldnames {
893                 FLD_PORT = 0,
894                 FLD_QUEUE,
895                 FLD_LCORE,
896                 _NUM_FLD
897         };
898         unsigned long int_fld[_NUM_FLD];
899         char *str_fld[_NUM_FLD];
900         int32_t i;
901         uint32_t size;
902
903         nb_lcore_params = 0;
904
905         while ((p = strchr(p0, '(')) != NULL) {
906                 ++p;
907                 p0 = strchr(p, ')');
908                 if (p0 == NULL)
909                         return -1;
910
911                 size = p0 - p;
912                 if (size >= sizeof(s))
913                         return -1;
914
915                 snprintf(s, sizeof(s), "%.*s", size, p);
916                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
917                                 _NUM_FLD)
918                         return -1;
919                 for (i = 0; i < _NUM_FLD; i++) {
920                         errno = 0;
921                         int_fld[i] = strtoul(str_fld[i], &end, 0);
922                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
923                                 return -1;
924                 }
925                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
926                         printf("exceeded max number of lcore params: %hu\n",
927                                 nb_lcore_params);
928                         return -1;
929                 }
930                 lcore_params_array[nb_lcore_params].port_id =
931                         (uint8_t)int_fld[FLD_PORT];
932                 lcore_params_array[nb_lcore_params].queue_id =
933                         (uint8_t)int_fld[FLD_QUEUE];
934                 lcore_params_array[nb_lcore_params].lcore_id =
935                         (uint8_t)int_fld[FLD_LCORE];
936                 ++nb_lcore_params;
937         }
938         lcore_params = lcore_params_array;
939         return 0;
940 }
941
942 #define __STRNCMP(name, opt) (!strncmp(name, opt, sizeof(opt)))
943 static int32_t
944 parse_args_long_options(struct option *lgopts, int32_t option_index)
945 {
946         int32_t ret = -1;
947         const char *optname = lgopts[option_index].name;
948
949         if (__STRNCMP(optname, OPTION_CONFIG)) {
950                 ret = parse_config(optarg);
951                 if (ret)
952                         printf("invalid config\n");
953         }
954
955         if (__STRNCMP(optname, OPTION_SINGLE_SA)) {
956                 ret = parse_decimal(optarg);
957                 if (ret != -1) {
958                         single_sa = 1;
959                         single_sa_idx = ret;
960                         printf("Configured with single SA index %u\n",
961                                         single_sa_idx);
962                         ret = 0;
963                 }
964         }
965
966         return ret;
967 }
968 #undef __STRNCMP
969
970 static int32_t
971 parse_args(int32_t argc, char **argv)
972 {
973         int32_t opt, ret;
974         char **argvopt;
975         int32_t option_index;
976         char *prgname = argv[0];
977         static struct option lgopts[] = {
978                 {OPTION_CONFIG, 1, 0, 0},
979                 {OPTION_SINGLE_SA, 1, 0, 0},
980                 {NULL, 0, 0, 0}
981         };
982         int32_t f_present = 0;
983
984         argvopt = argv;
985
986         while ((opt = getopt_long(argc, argvopt, "p:Pu:f:j:",
987                                 lgopts, &option_index)) != EOF) {
988
989                 switch (opt) {
990                 case 'p':
991                         enabled_port_mask = parse_portmask(optarg);
992                         if (enabled_port_mask == 0) {
993                                 printf("invalid portmask\n");
994                                 print_usage(prgname);
995                                 return -1;
996                         }
997                         break;
998                 case 'P':
999                         printf("Promiscuous mode selected\n");
1000                         promiscuous_on = 1;
1001                         break;
1002                 case 'u':
1003                         unprotected_port_mask = parse_portmask(optarg);
1004                         if (unprotected_port_mask == 0) {
1005                                 printf("invalid unprotected portmask\n");
1006                                 print_usage(prgname);
1007                                 return -1;
1008                         }
1009                         break;
1010                 case 'f':
1011                         if (f_present == 1) {
1012                                 printf("\"-f\" option present more than "
1013                                         "once!\n");
1014                                 print_usage(prgname);
1015                                 return -1;
1016                         }
1017                         if (parse_cfg_file(optarg) < 0) {
1018                                 printf("parsing file \"%s\" failed\n",
1019                                         optarg);
1020                                 print_usage(prgname);
1021                                 return -1;
1022                         }
1023                         f_present = 1;
1024                         break;
1025                 case 'j':
1026                         {
1027                                 int32_t size = parse_decimal(optarg);
1028                                 if (size <= 1518) {
1029                                         printf("Invalid jumbo frame size\n");
1030                                         if (size < 0) {
1031                                                 print_usage(prgname);
1032                                                 return -1;
1033                                         }
1034                                         printf("Using default value 9000\n");
1035                                         frame_size = 9000;
1036                                 } else {
1037                                         frame_size = size;
1038                                 }
1039                         }
1040                         printf("Enabled jumbo frames size %u\n", frame_size);
1041                         break;
1042                 case 0:
1043                         if (parse_args_long_options(lgopts, option_index)) {
1044                                 print_usage(prgname);
1045                                 return -1;
1046                         }
1047                         break;
1048                 default:
1049                         print_usage(prgname);
1050                         return -1;
1051                 }
1052         }
1053
1054         if (f_present == 0) {
1055                 printf("Mandatory option \"-f\" not present\n");
1056                 return -1;
1057         }
1058
1059         if (optind >= 0)
1060                 argv[optind-1] = prgname;
1061
1062         ret = optind-1;
1063         optind = 1; /* reset getopt lib */
1064         return ret;
1065 }
1066
1067 static void
1068 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1069 {
1070         char buf[ETHER_ADDR_FMT_SIZE];
1071         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1072         printf("%s%s", name, buf);
1073 }
1074
1075 /* Check the link status of all ports in up to 9s, and print them finally */
1076 static void
1077 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
1078 {
1079 #define CHECK_INTERVAL 100 /* 100ms */
1080 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1081         uint16_t portid;
1082         uint8_t count, all_ports_up, print_flag = 0;
1083         struct rte_eth_link link;
1084
1085         printf("\nChecking link status");
1086         fflush(stdout);
1087         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1088                 all_ports_up = 1;
1089                 for (portid = 0; portid < port_num; portid++) {
1090                         if ((port_mask & (1 << portid)) == 0)
1091                                 continue;
1092                         memset(&link, 0, sizeof(link));
1093                         rte_eth_link_get_nowait(portid, &link);
1094                         /* print link status if flag set */
1095                         if (print_flag == 1) {
1096                                 if (link.link_status)
1097                                         printf(
1098                                         "Port%d Link Up - speed %u Mbps -%s\n",
1099                                                 portid, link.link_speed,
1100                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1101                                         ("full-duplex") : ("half-duplex\n"));
1102                                 else
1103                                         printf("Port %d Link Down\n", portid);
1104                                 continue;
1105                         }
1106                         /* clear all_ports_up flag if any link down */
1107                         if (link.link_status == ETH_LINK_DOWN) {
1108                                 all_ports_up = 0;
1109                                 break;
1110                         }
1111                 }
1112                 /* after finally printing all link status, get out */
1113                 if (print_flag == 1)
1114                         break;
1115
1116                 if (all_ports_up == 0) {
1117                         printf(".");
1118                         fflush(stdout);
1119                         rte_delay_ms(CHECK_INTERVAL);
1120                 }
1121
1122                 /* set the print_flag if all ports up or timeout */
1123                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1124                         print_flag = 1;
1125                         printf("done\n");
1126                 }
1127         }
1128 }
1129
1130 static int32_t
1131 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1132                 uint16_t qp, struct lcore_params *params,
1133                 struct ipsec_ctx *ipsec_ctx,
1134                 const struct rte_cryptodev_capabilities *cipher,
1135                 const struct rte_cryptodev_capabilities *auth,
1136                 const struct rte_cryptodev_capabilities *aead)
1137 {
1138         int32_t ret = 0;
1139         unsigned long i;
1140         struct cdev_key key = { 0 };
1141
1142         key.lcore_id = params->lcore_id;
1143         if (cipher)
1144                 key.cipher_algo = cipher->sym.cipher.algo;
1145         if (auth)
1146                 key.auth_algo = auth->sym.auth.algo;
1147         if (aead)
1148                 key.aead_algo = aead->sym.aead.algo;
1149
1150         ret = rte_hash_lookup(map, &key);
1151         if (ret != -ENOENT)
1152                 return 0;
1153
1154         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1155                 if (ipsec_ctx->tbl[i].id == cdev_id)
1156                         break;
1157
1158         if (i == ipsec_ctx->nb_qps) {
1159                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1160                         printf("Maximum number of crypto devices assigned to "
1161                                 "a core, increase MAX_QP_PER_LCORE value\n");
1162                         return 0;
1163                 }
1164                 ipsec_ctx->tbl[i].id = cdev_id;
1165                 ipsec_ctx->tbl[i].qp = qp;
1166                 ipsec_ctx->nb_qps++;
1167                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1168                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1169                                 cdev_id, qp, i);
1170         }
1171
1172         ret = rte_hash_add_key_data(map, &key, (void *)i);
1173         if (ret < 0) {
1174                 printf("Faled to insert cdev mapping for (lcore %u, "
1175                                 "cdev %u, qp %u), errno %d\n",
1176                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1177                                 ipsec_ctx->tbl[i].qp, ret);
1178                 return 0;
1179         }
1180
1181         return 1;
1182 }
1183
1184 static int32_t
1185 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1186                 uint16_t qp, struct lcore_params *params)
1187 {
1188         int32_t ret = 0;
1189         const struct rte_cryptodev_capabilities *i, *j;
1190         struct rte_hash *map;
1191         struct lcore_conf *qconf;
1192         struct ipsec_ctx *ipsec_ctx;
1193         const char *str;
1194
1195         qconf = &lcore_conf[params->lcore_id];
1196
1197         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1198                 map = cdev_map_out;
1199                 ipsec_ctx = &qconf->outbound;
1200                 str = "Outbound";
1201         } else {
1202                 map = cdev_map_in;
1203                 ipsec_ctx = &qconf->inbound;
1204                 str = "Inbound";
1205         }
1206
1207         /* Required cryptodevs with operation chainning */
1208         if (!(dev_info->feature_flags &
1209                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1210                 return ret;
1211
1212         for (i = dev_info->capabilities;
1213                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1214                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1215                         continue;
1216
1217                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1218                         ret |= add_mapping(map, str, cdev_id, qp, params,
1219                                         ipsec_ctx, NULL, NULL, i);
1220                         continue;
1221                 }
1222
1223                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1224                         continue;
1225
1226                 for (j = dev_info->capabilities;
1227                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1228                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1229                                 continue;
1230
1231                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1232                                 continue;
1233
1234                         ret |= add_mapping(map, str, cdev_id, qp, params,
1235                                                 ipsec_ctx, i, j, NULL);
1236                 }
1237         }
1238
1239         return ret;
1240 }
1241
1242 static int32_t
1243 cryptodevs_init(void)
1244 {
1245         struct rte_cryptodev_config dev_conf;
1246         struct rte_cryptodev_qp_conf qp_conf;
1247         uint16_t idx, max_nb_qps, qp, i;
1248         int16_t cdev_id;
1249         struct rte_hash_parameters params = { 0 };
1250
1251         params.entries = CDEV_MAP_ENTRIES;
1252         params.key_len = sizeof(struct cdev_key);
1253         params.hash_func = rte_jhash;
1254         params.hash_func_init_val = 0;
1255         params.socket_id = rte_socket_id();
1256
1257         params.name = "cdev_map_in";
1258         cdev_map_in = rte_hash_create(&params);
1259         if (cdev_map_in == NULL)
1260                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1261                                 rte_errno);
1262
1263         params.name = "cdev_map_out";
1264         cdev_map_out = rte_hash_create(&params);
1265         if (cdev_map_out == NULL)
1266                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1267                                 rte_errno);
1268
1269         printf("lcore/cryptodev/qp mappings:\n");
1270
1271         uint32_t max_sess_sz = 0, sess_sz;
1272         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1273                 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1274                 if (sess_sz > max_sess_sz)
1275                         max_sess_sz = sess_sz;
1276         }
1277
1278         idx = 0;
1279         /* Start from last cdev id to give HW priority */
1280         for (cdev_id = rte_cryptodev_count() - 1; cdev_id >= 0; cdev_id--) {
1281                 struct rte_cryptodev_info cdev_info;
1282
1283                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1284
1285                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1286                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1287                 else
1288                         max_nb_qps = nb_lcore_params;
1289
1290                 qp = 0;
1291                 i = 0;
1292                 while (qp < max_nb_qps && i < nb_lcore_params) {
1293                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1294                                                 &lcore_params[idx]))
1295                                 qp++;
1296                         idx++;
1297                         idx = idx % nb_lcore_params;
1298                         i++;
1299                 }
1300
1301                 if (qp == 0)
1302                         continue;
1303
1304                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1305                 dev_conf.nb_queue_pairs = qp;
1306
1307                 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1308                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1309                         struct rte_mempool *sess_mp;
1310
1311                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1312                                         "sess_mp_%u", dev_conf.socket_id);
1313                         sess_mp = rte_mempool_create(mp_name,
1314                                         CDEV_MP_NB_OBJS,
1315                                         max_sess_sz,
1316                                         CDEV_MP_CACHE_SZ,
1317                                         0, NULL, NULL, NULL,
1318                                         NULL, dev_conf.socket_id,
1319                                         0);
1320                         if (sess_mp == NULL)
1321                                 rte_exit(EXIT_FAILURE,
1322                                         "Cannot create session pool on socket %d\n",
1323                                         dev_conf.socket_id);
1324                         else
1325                                 printf("Allocated session pool on socket %d\n",
1326                                         dev_conf.socket_id);
1327                         socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1328                 }
1329
1330                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1331                         rte_panic("Failed to initialize cryptodev %u\n",
1332                                         cdev_id);
1333
1334                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1335                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1336                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1337                                         &qp_conf, dev_conf.socket_id,
1338                                         socket_ctx[dev_conf.socket_id].session_pool))
1339                                 rte_panic("Failed to setup queue %u for "
1340                                                 "cdev_id %u\n", 0, cdev_id);
1341
1342                 if (rte_cryptodev_start(cdev_id))
1343                         rte_panic("Failed to start cryptodev %u\n",
1344                                         cdev_id);
1345         }
1346
1347         printf("\n");
1348
1349         return 0;
1350 }
1351
1352 static void
1353 port_init(uint16_t portid)
1354 {
1355         struct rte_eth_dev_info dev_info;
1356         struct rte_eth_txconf *txconf;
1357         uint16_t nb_tx_queue, nb_rx_queue;
1358         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1359         int32_t ret, socket_id;
1360         struct lcore_conf *qconf;
1361         struct ether_addr ethaddr;
1362
1363         rte_eth_dev_info_get(portid, &dev_info);
1364
1365         printf("Configuring device port %u:\n", portid);
1366
1367         rte_eth_macaddr_get(portid, &ethaddr);
1368         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1369         print_ethaddr("Address: ", &ethaddr);
1370         printf("\n");
1371
1372         nb_rx_queue = get_port_nb_rx_queues(portid);
1373         nb_tx_queue = nb_lcores;
1374
1375         if (nb_rx_queue > dev_info.max_rx_queues)
1376                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1377                                 "(max rx queue is %u)\n",
1378                                 nb_rx_queue, dev_info.max_rx_queues);
1379
1380         if (nb_tx_queue > dev_info.max_tx_queues)
1381                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1382                                 "(max tx queue is %u)\n",
1383                                 nb_tx_queue, dev_info.max_tx_queues);
1384
1385         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1386                         nb_rx_queue, nb_tx_queue);
1387
1388         if (frame_size) {
1389                 port_conf.rxmode.max_rx_pkt_len = frame_size;
1390                 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
1391         }
1392
1393         if (dev_info.rx_offload_capa & DEV_RX_OFFLOAD_SECURITY)
1394                 port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_SECURITY;
1395         if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_SECURITY)
1396                 port_conf.txmode.offloads |= DEV_TX_OFFLOAD_SECURITY;
1397
1398         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1399                         &port_conf);
1400         if (ret < 0)
1401                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1402                                 "err=%d, port=%d\n", ret, portid);
1403
1404         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1405         if (ret < 0)
1406                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1407                                 "err=%d, port=%d\n", ret, portid);
1408
1409         /* init one TX queue per lcore */
1410         tx_queueid = 0;
1411         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1412                 if (rte_lcore_is_enabled(lcore_id) == 0)
1413                         continue;
1414
1415                 if (numa_on)
1416                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1417                 else
1418                         socket_id = 0;
1419
1420                 /* init TX queue */
1421                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1422
1423                 txconf = &dev_info.default_txconf;
1424                 txconf->txq_flags = 0;
1425
1426                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1427                                 socket_id, txconf);
1428                 if (ret < 0)
1429                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1430                                         "err=%d, port=%d\n", ret, portid);
1431
1432                 qconf = &lcore_conf[lcore_id];
1433                 qconf->tx_queue_id[portid] = tx_queueid;
1434                 tx_queueid++;
1435
1436                 /* init RX queues */
1437                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1438                         if (portid != qconf->rx_queue_list[queue].port_id)
1439                                 continue;
1440
1441                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
1442
1443                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1444                                         socket_id);
1445
1446                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1447                                         nb_rxd, socket_id, NULL,
1448                                         socket_ctx[socket_id].mbuf_pool);
1449                         if (ret < 0)
1450                                 rte_exit(EXIT_FAILURE,
1451                                         "rte_eth_rx_queue_setup: err=%d, "
1452                                         "port=%d\n", ret, portid);
1453                 }
1454         }
1455         printf("\n");
1456 }
1457
1458 static void
1459 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1460 {
1461         char s[64];
1462         uint32_t buff_size = frame_size ? (frame_size + RTE_PKTMBUF_HEADROOM) :
1463                         RTE_MBUF_DEFAULT_BUF_SIZE;
1464
1465
1466         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1467         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1468                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1469                         buff_size,
1470                         socket_id);
1471         if (ctx->mbuf_pool == NULL)
1472                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1473                                 socket_id);
1474         else
1475                 printf("Allocated mbuf pool on socket %d\n", socket_id);
1476 }
1477
1478 int32_t
1479 main(int32_t argc, char **argv)
1480 {
1481         int32_t ret;
1482         uint32_t lcore_id;
1483         uint8_t socket_id;
1484         uint16_t portid, nb_ports;
1485
1486         /* init EAL */
1487         ret = rte_eal_init(argc, argv);
1488         if (ret < 0)
1489                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1490         argc -= ret;
1491         argv += ret;
1492
1493         /* parse application arguments (after the EAL ones) */
1494         ret = parse_args(argc, argv);
1495         if (ret < 0)
1496                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1497
1498         if ((unprotected_port_mask & enabled_port_mask) !=
1499                         unprotected_port_mask)
1500                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1501                                 unprotected_port_mask);
1502
1503         nb_ports = rte_eth_dev_count();
1504
1505         if (check_params() < 0)
1506                 rte_exit(EXIT_FAILURE, "check_params failed\n");
1507
1508         ret = init_lcore_rx_queues();
1509         if (ret < 0)
1510                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1511
1512         nb_lcores = rte_lcore_count();
1513
1514         /* Replicate each context per socket */
1515         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1516                 if (rte_lcore_is_enabled(lcore_id) == 0)
1517                         continue;
1518
1519                 if (numa_on)
1520                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1521                 else
1522                         socket_id = 0;
1523
1524                 if (socket_ctx[socket_id].mbuf_pool)
1525                         continue;
1526
1527                 sa_init(&socket_ctx[socket_id], socket_id);
1528
1529                 sp4_init(&socket_ctx[socket_id], socket_id);
1530
1531                 sp6_init(&socket_ctx[socket_id], socket_id);
1532
1533                 rt_init(&socket_ctx[socket_id], socket_id);
1534
1535                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1536         }
1537
1538         for (portid = 0; portid < nb_ports; portid++) {
1539                 if ((enabled_port_mask & (1 << portid)) == 0)
1540                         continue;
1541
1542                 port_init(portid);
1543         }
1544
1545         cryptodevs_init();
1546
1547         /* start ports */
1548         for (portid = 0; portid < nb_ports; portid++) {
1549                 if ((enabled_port_mask & (1 << portid)) == 0)
1550                         continue;
1551
1552                 /* Start device */
1553                 ret = rte_eth_dev_start(portid);
1554                 if (ret < 0)
1555                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1556                                         "err=%d, port=%d\n", ret, portid);
1557                 /*
1558                  * If enabled, put device in promiscuous mode.
1559                  * This allows IO forwarding mode to forward packets
1560                  * to itself through 2 cross-connected  ports of the
1561                  * target machine.
1562                  */
1563                 if (promiscuous_on)
1564                         rte_eth_promiscuous_enable(portid);
1565         }
1566
1567         check_all_ports_link_status(nb_ports, enabled_port_mask);
1568
1569         /* launch per-lcore init on every lcore */
1570         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1571         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1572                 if (rte_eal_wait_lcore(lcore_id) < 0)
1573                         return -1;
1574         }
1575
1576         return 0;
1577 }