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