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