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