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