99dc270cb1fb5077038b291cf34cb4de231815c0
[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                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv4);
308         } else {
309                 pkt->ol_flags |= PKT_TX_IPV6;
310                 pkt->l3_len = sizeof(struct ip6_hdr);
311                 pkt->l2_len = ETHER_HDR_LEN;
312
313                 ethhdr->ether_type = rte_cpu_to_be_16(ETHER_TYPE_IPv6);
314         }
315
316         memcpy(&ethhdr->s_addr, &ethaddr_tbl[port].src,
317                         sizeof(struct ether_addr));
318         memcpy(&ethhdr->d_addr, &ethaddr_tbl[port].dst,
319                         sizeof(struct ether_addr));
320 }
321
322 static inline void
323 prepare_tx_burst(struct rte_mbuf *pkts[], uint16_t nb_pkts, uint8_t port)
324 {
325         int32_t i;
326         const int32_t prefetch_offset = 2;
327
328         for (i = 0; i < (nb_pkts - prefetch_offset); i++) {
329                 rte_mbuf_prefetch_part2(pkts[i + prefetch_offset]);
330                 prepare_tx_pkt(pkts[i], port);
331         }
332         /* Process left packets */
333         for (; i < nb_pkts; i++)
334                 prepare_tx_pkt(pkts[i], port);
335 }
336
337 /* Send burst of packets on an output interface */
338 static inline int32_t
339 send_burst(struct lcore_conf *qconf, uint16_t n, uint8_t port)
340 {
341         struct rte_mbuf **m_table;
342         int32_t ret;
343         uint16_t queueid;
344
345         queueid = qconf->tx_queue_id[port];
346         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
347
348         prepare_tx_burst(m_table, n, port);
349
350         ret = rte_eth_tx_burst(port, queueid, m_table, n);
351         if (unlikely(ret < n)) {
352                 do {
353                         rte_pktmbuf_free(m_table[ret]);
354                 } while (++ret < n);
355         }
356
357         return 0;
358 }
359
360 /* Enqueue a single packet, and send burst if queue is filled */
361 static inline int32_t
362 send_single_packet(struct rte_mbuf *m, uint8_t port)
363 {
364         uint32_t lcore_id;
365         uint16_t len;
366         struct lcore_conf *qconf;
367
368         lcore_id = rte_lcore_id();
369
370         qconf = &lcore_conf[lcore_id];
371         len = qconf->tx_mbufs[port].len;
372         qconf->tx_mbufs[port].m_table[len] = m;
373         len++;
374
375         /* enough pkts to be sent */
376         if (unlikely(len == MAX_PKT_BURST)) {
377                 send_burst(qconf, MAX_PKT_BURST, port);
378                 len = 0;
379         }
380
381         qconf->tx_mbufs[port].len = len;
382         return 0;
383 }
384
385 static inline void
386 inbound_sp_sa(struct sp_ctx *sp, struct sa_ctx *sa, struct traffic_type *ip,
387                 uint16_t lim)
388 {
389         struct rte_mbuf *m;
390         uint32_t i, j, res, sa_idx;
391
392         if (ip->num == 0 || sp == NULL)
393                 return;
394
395         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
396                         ip->num, DEFAULT_MAX_CATEGORIES);
397
398         j = 0;
399         for (i = 0; i < ip->num; i++) {
400                 m = ip->pkts[i];
401                 res = ip->res[i];
402                 if (res & BYPASS) {
403                         ip->pkts[j++] = m;
404                         continue;
405                 }
406                 if (res & DISCARD || i < lim) {
407                         rte_pktmbuf_free(m);
408                         continue;
409                 }
410                 /* Only check SPI match for processed IPSec packets */
411                 sa_idx = ip->res[i] & PROTECT_MASK;
412                 if (sa_idx == 0 || !inbound_sa_check(sa, m, sa_idx)) {
413                         rte_pktmbuf_free(m);
414                         continue;
415                 }
416                 ip->pkts[j++] = m;
417         }
418         ip->num = j;
419 }
420
421 static inline void
422 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
423                 struct ipsec_traffic *traffic)
424 {
425         struct rte_mbuf *m;
426         uint16_t idx, nb_pkts_in, i, n_ip4, n_ip6;
427
428         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
429                         traffic->ipsec.num, MAX_PKT_BURST);
430
431         n_ip4 = traffic->ip4.num;
432         n_ip6 = traffic->ip6.num;
433
434         /* SP/ACL Inbound check ipsec and ip4 */
435         for (i = 0; i < nb_pkts_in; i++) {
436                 m = traffic->ipsec.pkts[i];
437                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
438                 if (ip->ip_v == IPVERSION) {
439                         idx = traffic->ip4.num++;
440                         traffic->ip4.pkts[idx] = m;
441                         traffic->ip4.data[idx] = rte_pktmbuf_mtod_offset(m,
442                                         uint8_t *, offsetof(struct ip, ip_p));
443                 } else if (ip->ip_v == IP6_VERSION) {
444                         idx = traffic->ip6.num++;
445                         traffic->ip6.pkts[idx] = m;
446                         traffic->ip6.data[idx] = rte_pktmbuf_mtod_offset(m,
447                                         uint8_t *,
448                                         offsetof(struct ip6_hdr, ip6_nxt));
449                 } else
450                         rte_pktmbuf_free(m);
451         }
452
453         inbound_sp_sa(ipsec_ctx->sp4_ctx, ipsec_ctx->sa_ctx, &traffic->ip4,
454                         n_ip4);
455
456         inbound_sp_sa(ipsec_ctx->sp6_ctx, ipsec_ctx->sa_ctx, &traffic->ip6,
457                         n_ip6);
458 }
459
460 static inline void
461 outbound_sp(struct sp_ctx *sp, struct traffic_type *ip,
462                 struct traffic_type *ipsec)
463 {
464         struct rte_mbuf *m;
465         uint32_t i, j, sa_idx;
466
467         if (ip->num == 0 || sp == NULL)
468                 return;
469
470         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
471                         ip->num, DEFAULT_MAX_CATEGORIES);
472
473         j = 0;
474         for (i = 0; i < ip->num; i++) {
475                 m = ip->pkts[i];
476                 sa_idx = ip->res[i] & PROTECT_MASK;
477                 if ((ip->res[i] == 0) || (ip->res[i] & DISCARD))
478                         rte_pktmbuf_free(m);
479                 else if (sa_idx != 0) {
480                         ipsec->res[ipsec->num] = sa_idx;
481                         ipsec->pkts[ipsec->num++] = m;
482                 } else /* BYPASS */
483                         ip->pkts[j++] = m;
484         }
485         ip->num = j;
486 }
487
488 static inline void
489 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
490                 struct ipsec_traffic *traffic)
491 {
492         struct rte_mbuf *m;
493         uint16_t idx, nb_pkts_out, i;
494
495         /* Drop any IPsec traffic from protected ports */
496         for (i = 0; i < traffic->ipsec.num; i++)
497                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
498
499         traffic->ipsec.num = 0;
500
501         outbound_sp(ipsec_ctx->sp4_ctx, &traffic->ip4, &traffic->ipsec);
502
503         outbound_sp(ipsec_ctx->sp6_ctx, &traffic->ip6, &traffic->ipsec);
504
505         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
506                         traffic->ipsec.res, traffic->ipsec.num,
507                         MAX_PKT_BURST);
508
509         for (i = 0; i < nb_pkts_out; i++) {
510                 m = traffic->ipsec.pkts[i];
511                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
512                 if (ip->ip_v == IPVERSION) {
513                         idx = traffic->ip4.num++;
514                         traffic->ip4.pkts[idx] = m;
515                 } else {
516                         idx = traffic->ip6.num++;
517                         traffic->ip6.pkts[idx] = m;
518                 }
519         }
520 }
521
522 static inline void
523 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
524                 struct ipsec_traffic *traffic)
525 {
526         struct rte_mbuf *m;
527         uint32_t nb_pkts_in, i, idx;
528
529         /* Drop any IPv4 traffic from unprotected ports */
530         for (i = 0; i < traffic->ip4.num; i++)
531                 rte_pktmbuf_free(traffic->ip4.pkts[i]);
532
533         traffic->ip4.num = 0;
534
535         /* Drop any IPv6 traffic from unprotected ports */
536         for (i = 0; i < traffic->ip6.num; i++)
537                 rte_pktmbuf_free(traffic->ip6.pkts[i]);
538
539         traffic->ip6.num = 0;
540
541         nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
542                         traffic->ipsec.num, MAX_PKT_BURST);
543
544         for (i = 0; i < nb_pkts_in; i++) {
545                 m = traffic->ipsec.pkts[i];
546                 struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
547                 if (ip->ip_v == IPVERSION) {
548                         idx = traffic->ip4.num++;
549                         traffic->ip4.pkts[idx] = m;
550                 } else {
551                         idx = traffic->ip6.num++;
552                         traffic->ip6.pkts[idx] = m;
553                 }
554         }
555 }
556
557 static inline void
558 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
559                 struct ipsec_traffic *traffic)
560 {
561         struct rte_mbuf *m;
562         uint32_t nb_pkts_out, i;
563         struct ip *ip;
564
565         /* Drop any IPsec traffic from protected ports */
566         for (i = 0; i < traffic->ipsec.num; i++)
567                 rte_pktmbuf_free(traffic->ipsec.pkts[i]);
568
569         traffic->ipsec.num = 0;
570
571         for (i = 0; i < traffic->ip4.num; i++)
572                 traffic->ip4.res[i] = single_sa_idx;
573
574         for (i = 0; i < traffic->ip6.num; i++)
575                 traffic->ip6.res[i] = single_sa_idx;
576
577         nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ip4.pkts,
578                         traffic->ip4.res, traffic->ip4.num,
579                         MAX_PKT_BURST);
580
581         /* They all sue the same SA (ip4 or ip6 tunnel) */
582         m = traffic->ipsec.pkts[i];
583         ip = rte_pktmbuf_mtod(m, struct ip *);
584         if (ip->ip_v == IPVERSION)
585                 traffic->ip4.num = nb_pkts_out;
586         else
587                 traffic->ip6.num = nb_pkts_out;
588 }
589
590 static inline void
591 route4_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
592 {
593         uint32_t hop[MAX_PKT_BURST * 2];
594         uint32_t dst_ip[MAX_PKT_BURST * 2];
595         uint16_t i, offset;
596
597         if (nb_pkts == 0)
598                 return;
599
600         for (i = 0; i < nb_pkts; i++) {
601                 offset = offsetof(struct ip, ip_dst);
602                 dst_ip[i] = *rte_pktmbuf_mtod_offset(pkts[i],
603                                 uint32_t *, offset);
604                 dst_ip[i] = rte_be_to_cpu_32(dst_ip[i]);
605         }
606
607         rte_lpm_lookup_bulk((struct rte_lpm *)rt_ctx, dst_ip, hop, nb_pkts);
608
609         for (i = 0; i < nb_pkts; i++) {
610                 if ((hop[i] & RTE_LPM_LOOKUP_SUCCESS) == 0) {
611                         rte_pktmbuf_free(pkts[i]);
612                         continue;
613                 }
614                 send_single_packet(pkts[i], hop[i] & 0xff);
615         }
616 }
617
618 static inline void
619 route6_pkts(struct rt_ctx *rt_ctx, struct rte_mbuf *pkts[], uint8_t nb_pkts)
620 {
621         int32_t hop[MAX_PKT_BURST * 2];
622         uint8_t dst_ip[MAX_PKT_BURST * 2][16];
623         uint8_t *ip6_dst;
624         uint16_t i, offset;
625
626         if (nb_pkts == 0)
627                 return;
628
629         for (i = 0; i < nb_pkts; i++) {
630                 offset = offsetof(struct ip6_hdr, ip6_dst);
631                 ip6_dst = rte_pktmbuf_mtod_offset(pkts[i], uint8_t *, offset);
632                 memcpy(&dst_ip[i][0], ip6_dst, 16);
633         }
634
635         rte_lpm6_lookup_bulk_func((struct rte_lpm6 *)rt_ctx, dst_ip,
636                         hop, nb_pkts);
637
638         for (i = 0; i < nb_pkts; i++) {
639                 if (hop[i] == -1) {
640                         rte_pktmbuf_free(pkts[i]);
641                         continue;
642                 }
643                 send_single_packet(pkts[i], hop[i] & 0xff);
644         }
645 }
646
647 static inline void
648 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
649                 uint8_t nb_pkts, uint8_t portid)
650 {
651         struct ipsec_traffic traffic;
652
653         prepare_traffic(pkts, &traffic, nb_pkts);
654
655         if (unlikely(single_sa)) {
656                 if (UNPROTECTED_PORT(portid))
657                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
658                 else
659                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
660         } else {
661                 if (UNPROTECTED_PORT(portid))
662                         process_pkts_inbound(&qconf->inbound, &traffic);
663                 else
664                         process_pkts_outbound(&qconf->outbound, &traffic);
665         }
666
667         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
668         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
669 }
670
671 static inline void
672 drain_buffers(struct lcore_conf *qconf)
673 {
674         struct buffer *buf;
675         uint32_t portid;
676
677         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
678                 buf = &qconf->tx_mbufs[portid];
679                 if (buf->len == 0)
680                         continue;
681                 send_burst(qconf, buf->len, portid);
682                 buf->len = 0;
683         }
684 }
685
686 /* main processing loop */
687 static int32_t
688 main_loop(__attribute__((unused)) void *dummy)
689 {
690         struct rte_mbuf *pkts[MAX_PKT_BURST];
691         uint32_t lcore_id;
692         uint64_t prev_tsc, diff_tsc, cur_tsc;
693         int32_t i, nb_rx;
694         uint8_t portid, queueid;
695         struct lcore_conf *qconf;
696         int32_t socket_id;
697         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
698                         / US_PER_S * BURST_TX_DRAIN_US;
699         struct lcore_rx_queue *rxql;
700
701         prev_tsc = 0;
702         lcore_id = rte_lcore_id();
703         qconf = &lcore_conf[lcore_id];
704         rxql = qconf->rx_queue_list;
705         socket_id = rte_lcore_to_socket_id(lcore_id);
706
707         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
708         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
709         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
710         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
711         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
712         qconf->inbound.cdev_map = cdev_map_in;
713         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
714         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
715         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
716         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
717         qconf->outbound.cdev_map = cdev_map_out;
718         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
719
720         if (qconf->nb_rx_queue == 0) {
721                 RTE_LOG(INFO, IPSEC, "lcore %u has nothing to do\n", lcore_id);
722                 return 0;
723         }
724
725         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
726
727         for (i = 0; i < qconf->nb_rx_queue; i++) {
728                 portid = rxql[i].port_id;
729                 queueid = rxql[i].queue_id;
730                 RTE_LOG(INFO, IPSEC,
731                         " -- lcoreid=%u portid=%hhu rxqueueid=%hhu\n",
732                         lcore_id, portid, queueid);
733         }
734
735         while (1) {
736                 cur_tsc = rte_rdtsc();
737
738                 /* TX queue buffer drain */
739                 diff_tsc = cur_tsc - prev_tsc;
740
741                 if (unlikely(diff_tsc > drain_tsc)) {
742                         drain_buffers(qconf);
743                         prev_tsc = cur_tsc;
744                 }
745
746                 /* Read packet from RX queues */
747                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
748                         portid = rxql[i].port_id;
749                         queueid = rxql[i].queue_id;
750                         nb_rx = rte_eth_rx_burst(portid, queueid,
751                                         pkts, MAX_PKT_BURST);
752
753                         if (nb_rx > 0)
754                                 process_pkts(qconf, pkts, nb_rx, portid);
755                 }
756         }
757 }
758
759 static int32_t
760 check_params(void)
761 {
762         uint8_t lcore, portid, nb_ports;
763         uint16_t i;
764         int32_t socket_id;
765
766         if (lcore_params == NULL) {
767                 printf("Error: No port/queue/core mappings\n");
768                 return -1;
769         }
770
771         nb_ports = rte_eth_dev_count();
772
773         for (i = 0; i < nb_lcore_params; ++i) {
774                 lcore = lcore_params[i].lcore_id;
775                 if (!rte_lcore_is_enabled(lcore)) {
776                         printf("error: lcore %hhu is not enabled in "
777                                 "lcore mask\n", lcore);
778                         return -1;
779                 }
780                 socket_id = rte_lcore_to_socket_id(lcore);
781                 if (socket_id != 0 && numa_on == 0) {
782                         printf("warning: lcore %hhu is on socket %d "
783                                 "with numa off\n",
784                                 lcore, socket_id);
785                 }
786                 portid = lcore_params[i].port_id;
787                 if ((enabled_port_mask & (1 << portid)) == 0) {
788                         printf("port %u is not enabled in port mask\n", portid);
789                         return -1;
790                 }
791                 if (portid >= nb_ports) {
792                         printf("port %u is not present on the board\n", portid);
793                         return -1;
794                 }
795         }
796         return 0;
797 }
798
799 static uint8_t
800 get_port_nb_rx_queues(const uint8_t port)
801 {
802         int32_t queue = -1;
803         uint16_t i;
804
805         for (i = 0; i < nb_lcore_params; ++i) {
806                 if (lcore_params[i].port_id == port &&
807                                 lcore_params[i].queue_id > queue)
808                         queue = lcore_params[i].queue_id;
809         }
810         return (uint8_t)(++queue);
811 }
812
813 static int32_t
814 init_lcore_rx_queues(void)
815 {
816         uint16_t i, nb_rx_queue;
817         uint8_t lcore;
818
819         for (i = 0; i < nb_lcore_params; ++i) {
820                 lcore = lcore_params[i].lcore_id;
821                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
822                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
823                         printf("error: too many queues (%u) for lcore: %u\n",
824                                         nb_rx_queue + 1, lcore);
825                         return -1;
826                 }
827                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
828                         lcore_params[i].port_id;
829                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
830                         lcore_params[i].queue_id;
831                 lcore_conf[lcore].nb_rx_queue++;
832         }
833         return 0;
834 }
835
836 /* display usage */
837 static void
838 print_usage(const char *prgname)
839 {
840         printf("%s [EAL options] -- -p PORTMASK -P -u PORTMASK"
841                 "  --"OPTION_CONFIG" (port,queue,lcore)[,(port,queue,lcore]"
842                 " --single-sa SAIDX -f CONFIG_FILE\n"
843                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
844                 "  -P : enable promiscuous mode\n"
845                 "  -u PORTMASK: hexadecimal bitmask of unprotected ports\n"
846                 "  --"OPTION_CONFIG": (port,queue,lcore): "
847                 "rx queues configuration\n"
848                 "  --single-sa SAIDX: use single SA index for outbound, "
849                 "bypassing the SP\n"
850                 "  -f CONFIG_FILE: Configuration file path\n",
851                 prgname);
852 }
853
854 static int32_t
855 parse_portmask(const char *portmask)
856 {
857         char *end = NULL;
858         unsigned long pm;
859
860         /* parse hexadecimal string */
861         pm = strtoul(portmask, &end, 16);
862         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
863                 return -1;
864
865         if ((pm == 0) && errno)
866                 return -1;
867
868         return pm;
869 }
870
871 static int32_t
872 parse_decimal(const char *str)
873 {
874         char *end = NULL;
875         unsigned long num;
876
877         num = strtoul(str, &end, 10);
878         if ((str[0] == '\0') || (end == NULL) || (*end != '\0'))
879                 return -1;
880
881         return num;
882 }
883
884 static int32_t
885 parse_config(const char *q_arg)
886 {
887         char s[256];
888         const char *p, *p0 = q_arg;
889         char *end;
890         enum fieldnames {
891                 FLD_PORT = 0,
892                 FLD_QUEUE,
893                 FLD_LCORE,
894                 _NUM_FLD
895         };
896         unsigned long int_fld[_NUM_FLD];
897         char *str_fld[_NUM_FLD];
898         int32_t i;
899         uint32_t size;
900
901         nb_lcore_params = 0;
902
903         while ((p = strchr(p0, '(')) != NULL) {
904                 ++p;
905                 p0 = strchr(p, ')');
906                 if (p0 == NULL)
907                         return -1;
908
909                 size = p0 - p;
910                 if (size >= sizeof(s))
911                         return -1;
912
913                 snprintf(s, sizeof(s), "%.*s", size, p);
914                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
915                                 _NUM_FLD)
916                         return -1;
917                 for (i = 0; i < _NUM_FLD; i++) {
918                         errno = 0;
919                         int_fld[i] = strtoul(str_fld[i], &end, 0);
920                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
921                                 return -1;
922                 }
923                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
924                         printf("exceeded max number of lcore params: %hu\n",
925                                 nb_lcore_params);
926                         return -1;
927                 }
928                 lcore_params_array[nb_lcore_params].port_id =
929                         (uint8_t)int_fld[FLD_PORT];
930                 lcore_params_array[nb_lcore_params].queue_id =
931                         (uint8_t)int_fld[FLD_QUEUE];
932                 lcore_params_array[nb_lcore_params].lcore_id =
933                         (uint8_t)int_fld[FLD_LCORE];
934                 ++nb_lcore_params;
935         }
936         lcore_params = lcore_params_array;
937         return 0;
938 }
939
940 #define __STRNCMP(name, opt) (!strncmp(name, opt, sizeof(opt)))
941 static int32_t
942 parse_args_long_options(struct option *lgopts, int32_t option_index)
943 {
944         int32_t ret = -1;
945         const char *optname = lgopts[option_index].name;
946
947         if (__STRNCMP(optname, OPTION_CONFIG)) {
948                 ret = parse_config(optarg);
949                 if (ret)
950                         printf("invalid config\n");
951         }
952
953         if (__STRNCMP(optname, OPTION_SINGLE_SA)) {
954                 ret = parse_decimal(optarg);
955                 if (ret != -1) {
956                         single_sa = 1;
957                         single_sa_idx = ret;
958                         printf("Configured with single SA index %u\n",
959                                         single_sa_idx);
960                         ret = 0;
961                 }
962         }
963
964         return ret;
965 }
966 #undef __STRNCMP
967
968 static int32_t
969 parse_args(int32_t argc, char **argv)
970 {
971         int32_t opt, ret;
972         char **argvopt;
973         int32_t option_index;
974         char *prgname = argv[0];
975         static struct option lgopts[] = {
976                 {OPTION_CONFIG, 1, 0, 0},
977                 {OPTION_SINGLE_SA, 1, 0, 0},
978                 {NULL, 0, 0, 0}
979         };
980         int32_t f_present = 0;
981
982         argvopt = argv;
983
984         while ((opt = getopt_long(argc, argvopt, "p:Pu:f:",
985                                 lgopts, &option_index)) != EOF) {
986
987                 switch (opt) {
988                 case 'p':
989                         enabled_port_mask = parse_portmask(optarg);
990                         if (enabled_port_mask == 0) {
991                                 printf("invalid portmask\n");
992                                 print_usage(prgname);
993                                 return -1;
994                         }
995                         break;
996                 case 'P':
997                         printf("Promiscuous mode selected\n");
998                         promiscuous_on = 1;
999                         break;
1000                 case 'u':
1001                         unprotected_port_mask = parse_portmask(optarg);
1002                         if (unprotected_port_mask == 0) {
1003                                 printf("invalid unprotected portmask\n");
1004                                 print_usage(prgname);
1005                                 return -1;
1006                         }
1007                         break;
1008                 case 'f':
1009                         if (f_present == 1) {
1010                                 printf("\"-f\" option present more than "
1011                                         "once!\n");
1012                                 print_usage(prgname);
1013                                 return -1;
1014                         }
1015                         if (parse_cfg_file(optarg) < 0) {
1016                                 printf("parsing file \"%s\" failed\n",
1017                                         optarg);
1018                                 print_usage(prgname);
1019                                 return -1;
1020                         }
1021                         f_present = 1;
1022                         break;
1023                 case 0:
1024                         if (parse_args_long_options(lgopts, option_index)) {
1025                                 print_usage(prgname);
1026                                 return -1;
1027                         }
1028                         break;
1029                 default:
1030                         print_usage(prgname);
1031                         return -1;
1032                 }
1033         }
1034
1035         if (f_present == 0) {
1036                 printf("Mandatory option \"-f\" not present\n");
1037                 return -1;
1038         }
1039
1040         if (optind >= 0)
1041                 argv[optind-1] = prgname;
1042
1043         ret = optind-1;
1044         optind = 1; /* reset getopt lib */
1045         return ret;
1046 }
1047
1048 static void
1049 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
1050 {
1051         char buf[ETHER_ADDR_FMT_SIZE];
1052         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
1053         printf("%s%s", name, buf);
1054 }
1055
1056 /* Check the link status of all ports in up to 9s, and print them finally */
1057 static void
1058 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
1059 {
1060 #define CHECK_INTERVAL 100 /* 100ms */
1061 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1062         uint8_t portid, count, all_ports_up, print_flag = 0;
1063         struct rte_eth_link link;
1064
1065         printf("\nChecking link status");
1066         fflush(stdout);
1067         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1068                 all_ports_up = 1;
1069                 for (portid = 0; portid < port_num; portid++) {
1070                         if ((port_mask & (1 << portid)) == 0)
1071                                 continue;
1072                         memset(&link, 0, sizeof(link));
1073                         rte_eth_link_get_nowait(portid, &link);
1074                         /* print link status if flag set */
1075                         if (print_flag == 1) {
1076                                 if (link.link_status)
1077                                         printf("Port %d Link Up - speed %u "
1078                                                 "Mbps - %s\n", (uint8_t)portid,
1079                                                 (uint32_t)link.link_speed,
1080                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
1081                                         ("full-duplex") : ("half-duplex\n"));
1082                                 else
1083                                         printf("Port %d Link Down\n",
1084                                                 (uint8_t)portid);
1085                                 continue;
1086                         }
1087                         /* clear all_ports_up flag if any link down */
1088                         if (link.link_status == ETH_LINK_DOWN) {
1089                                 all_ports_up = 0;
1090                                 break;
1091                         }
1092                 }
1093                 /* after finally printing all link status, get out */
1094                 if (print_flag == 1)
1095                         break;
1096
1097                 if (all_ports_up == 0) {
1098                         printf(".");
1099                         fflush(stdout);
1100                         rte_delay_ms(CHECK_INTERVAL);
1101                 }
1102
1103                 /* set the print_flag if all ports up or timeout */
1104                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1105                         print_flag = 1;
1106                         printf("done\n");
1107                 }
1108         }
1109 }
1110
1111 static int32_t
1112 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1113                 uint16_t qp, struct lcore_params *params,
1114                 struct ipsec_ctx *ipsec_ctx,
1115                 const struct rte_cryptodev_capabilities *cipher,
1116                 const struct rte_cryptodev_capabilities *auth)
1117 {
1118         int32_t ret = 0;
1119         unsigned long i;
1120         struct cdev_key key = { 0 };
1121
1122         key.lcore_id = params->lcore_id;
1123         if (cipher)
1124                 key.cipher_algo = cipher->sym.cipher.algo;
1125         if (auth)
1126                 key.auth_algo = auth->sym.auth.algo;
1127
1128         ret = rte_hash_lookup(map, &key);
1129         if (ret != -ENOENT)
1130                 return 0;
1131
1132         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1133                 if (ipsec_ctx->tbl[i].id == cdev_id)
1134                         break;
1135
1136         if (i == ipsec_ctx->nb_qps) {
1137                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1138                         printf("Maximum number of crypto devices assigned to "
1139                                 "a core, increase MAX_QP_PER_LCORE value\n");
1140                         return 0;
1141                 }
1142                 ipsec_ctx->tbl[i].id = cdev_id;
1143                 ipsec_ctx->tbl[i].qp = qp;
1144                 ipsec_ctx->nb_qps++;
1145                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1146                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1147                                 cdev_id, qp, i);
1148         }
1149
1150         ret = rte_hash_add_key_data(map, &key, (void *)i);
1151         if (ret < 0) {
1152                 printf("Faled to insert cdev mapping for (lcore %u, "
1153                                 "cdev %u, qp %u), errno %d\n",
1154                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1155                                 ipsec_ctx->tbl[i].qp, ret);
1156                 return 0;
1157         }
1158
1159         return 1;
1160 }
1161
1162 static int32_t
1163 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1164                 uint16_t qp, struct lcore_params *params)
1165 {
1166         int32_t ret = 0;
1167         const struct rte_cryptodev_capabilities *i, *j;
1168         struct rte_hash *map;
1169         struct lcore_conf *qconf;
1170         struct ipsec_ctx *ipsec_ctx;
1171         const char *str;
1172
1173         qconf = &lcore_conf[params->lcore_id];
1174
1175         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1176                 map = cdev_map_out;
1177                 ipsec_ctx = &qconf->outbound;
1178                 str = "Outbound";
1179         } else {
1180                 map = cdev_map_in;
1181                 ipsec_ctx = &qconf->inbound;
1182                 str = "Inbound";
1183         }
1184
1185         /* Required cryptodevs with operation chainning */
1186         if (!(dev_info->feature_flags &
1187                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1188                 return ret;
1189
1190         for (i = dev_info->capabilities;
1191                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1192                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1193                         continue;
1194
1195                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1196                         continue;
1197
1198                 for (j = dev_info->capabilities;
1199                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1200                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1201                                 continue;
1202
1203                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1204                                 continue;
1205
1206                         ret |= add_mapping(map, str, cdev_id, qp, params,
1207                                         ipsec_ctx, i, j);
1208                 }
1209         }
1210
1211         return ret;
1212 }
1213
1214 static int32_t
1215 cryptodevs_init(void)
1216 {
1217         struct rte_cryptodev_config dev_conf;
1218         struct rte_cryptodev_qp_conf qp_conf;
1219         uint16_t idx, max_nb_qps, qp, i;
1220         int16_t cdev_id;
1221         struct rte_hash_parameters params = { 0 };
1222
1223         params.entries = CDEV_MAP_ENTRIES;
1224         params.key_len = sizeof(struct cdev_key);
1225         params.hash_func = rte_jhash;
1226         params.hash_func_init_val = 0;
1227         params.socket_id = rte_socket_id();
1228
1229         params.name = "cdev_map_in";
1230         cdev_map_in = rte_hash_create(&params);
1231         if (cdev_map_in == NULL)
1232                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1233                                 rte_errno);
1234
1235         params.name = "cdev_map_out";
1236         cdev_map_out = rte_hash_create(&params);
1237         if (cdev_map_out == NULL)
1238                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1239                                 rte_errno);
1240
1241         printf("lcore/cryptodev/qp mappings:\n");
1242
1243         uint32_t max_sess_sz = 0, sess_sz;
1244         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1245                 sess_sz = rte_cryptodev_get_private_session_size(cdev_id);
1246                 if (sess_sz > max_sess_sz)
1247                         max_sess_sz = sess_sz;
1248         }
1249
1250         idx = 0;
1251         /* Start from last cdev id to give HW priority */
1252         for (cdev_id = rte_cryptodev_count() - 1; cdev_id >= 0; cdev_id--) {
1253                 struct rte_cryptodev_info cdev_info;
1254
1255                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1256
1257                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1258                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1259                 else
1260                         max_nb_qps = nb_lcore_params;
1261
1262                 qp = 0;
1263                 i = 0;
1264                 while (qp < max_nb_qps && i < nb_lcore_params) {
1265                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1266                                                 &lcore_params[idx]))
1267                                 qp++;
1268                         idx++;
1269                         idx = idx % nb_lcore_params;
1270                         i++;
1271                 }
1272
1273                 if (qp == 0)
1274                         continue;
1275
1276                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1277                 dev_conf.nb_queue_pairs = qp;
1278
1279                 if (!socket_ctx[dev_conf.socket_id].session_pool) {
1280                         char mp_name[RTE_MEMPOOL_NAMESIZE];
1281                         struct rte_mempool *sess_mp;
1282
1283                         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
1284                                         "sess_mp_%u", dev_conf.socket_id);
1285                         sess_mp = rte_mempool_create(mp_name,
1286                                         CDEV_MP_NB_OBJS,
1287                                         max_sess_sz,
1288                                         CDEV_MP_CACHE_SZ,
1289                                         0, NULL, NULL, NULL,
1290                                         NULL, dev_conf.socket_id,
1291                                         0);
1292                         if (sess_mp == NULL)
1293                                 rte_exit(EXIT_FAILURE,
1294                                         "Cannot create session pool on socket %d\n",
1295                                         dev_conf.socket_id);
1296                         else
1297                                 printf("Allocated session pool on socket %d\n",
1298                                         dev_conf.socket_id);
1299                         socket_ctx[dev_conf.socket_id].session_pool = sess_mp;
1300                 }
1301
1302                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1303                         rte_panic("Failed to initialize cryptodev %u\n",
1304                                         cdev_id);
1305
1306                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1307                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1308                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1309                                         &qp_conf, dev_conf.socket_id,
1310                                         socket_ctx[dev_conf.socket_id].session_pool))
1311                                 rte_panic("Failed to setup queue %u for "
1312                                                 "cdev_id %u\n", 0, cdev_id);
1313
1314                 if (rte_cryptodev_start(cdev_id))
1315                         rte_panic("Failed to start cryptodev %u\n",
1316                                         cdev_id);
1317         }
1318
1319         printf("\n");
1320
1321         return 0;
1322 }
1323
1324 static void
1325 port_init(uint8_t portid)
1326 {
1327         struct rte_eth_dev_info dev_info;
1328         struct rte_eth_txconf *txconf;
1329         uint16_t nb_tx_queue, nb_rx_queue;
1330         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1331         int32_t ret, socket_id;
1332         struct lcore_conf *qconf;
1333         struct ether_addr ethaddr;
1334
1335         rte_eth_dev_info_get(portid, &dev_info);
1336
1337         printf("Configuring device port %u:\n", portid);
1338
1339         rte_eth_macaddr_get(portid, &ethaddr);
1340         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(ethaddr);
1341         print_ethaddr("Address: ", &ethaddr);
1342         printf("\n");
1343
1344         nb_rx_queue = get_port_nb_rx_queues(portid);
1345         nb_tx_queue = nb_lcores;
1346
1347         if (nb_rx_queue > dev_info.max_rx_queues)
1348                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1349                                 "(max rx queue is %u)\n",
1350                                 nb_rx_queue, dev_info.max_rx_queues);
1351
1352         if (nb_tx_queue > dev_info.max_tx_queues)
1353                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1354                                 "(max tx queue is %u)\n",
1355                                 nb_tx_queue, dev_info.max_tx_queues);
1356
1357         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1358                         nb_rx_queue, nb_tx_queue);
1359
1360         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1361                         &port_conf);
1362         if (ret < 0)
1363                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1364                                 "err=%d, port=%d\n", ret, portid);
1365
1366         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1367         if (ret < 0)
1368                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1369                                 "err=%d, port=%d\n", ret, portid);
1370
1371         /* init one TX queue per lcore */
1372         tx_queueid = 0;
1373         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1374                 if (rte_lcore_is_enabled(lcore_id) == 0)
1375                         continue;
1376
1377                 if (numa_on)
1378                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1379                 else
1380                         socket_id = 0;
1381
1382                 /* init TX queue */
1383                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1384
1385                 txconf = &dev_info.default_txconf;
1386                 txconf->txq_flags = 0;
1387
1388                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1389                                 socket_id, txconf);
1390                 if (ret < 0)
1391                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1392                                         "err=%d, port=%d\n", ret, portid);
1393
1394                 qconf = &lcore_conf[lcore_id];
1395                 qconf->tx_queue_id[portid] = tx_queueid;
1396                 tx_queueid++;
1397
1398                 /* init RX queues */
1399                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1400                         if (portid != qconf->rx_queue_list[queue].port_id)
1401                                 continue;
1402
1403                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
1404
1405                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1406                                         socket_id);
1407
1408                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1409                                         nb_rxd, socket_id, NULL,
1410                                         socket_ctx[socket_id].mbuf_pool);
1411                         if (ret < 0)
1412                                 rte_exit(EXIT_FAILURE,
1413                                         "rte_eth_rx_queue_setup: err=%d, "
1414                                         "port=%d\n", ret, portid);
1415                 }
1416         }
1417         printf("\n");
1418 }
1419
1420 static void
1421 pool_init(struct socket_ctx *ctx, int32_t socket_id, uint32_t nb_mbuf)
1422 {
1423         char s[64];
1424
1425         snprintf(s, sizeof(s), "mbuf_pool_%d", socket_id);
1426         ctx->mbuf_pool = rte_pktmbuf_pool_create(s, nb_mbuf,
1427                         MEMPOOL_CACHE_SIZE, ipsec_metadata_size(),
1428                         RTE_MBUF_DEFAULT_BUF_SIZE,
1429                         socket_id);
1430         if (ctx->mbuf_pool == NULL)
1431                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
1432                                 socket_id);
1433         else
1434                 printf("Allocated mbuf pool on socket %d\n", socket_id);
1435 }
1436
1437 int32_t
1438 main(int32_t argc, char **argv)
1439 {
1440         int32_t ret;
1441         uint32_t lcore_id, nb_ports;
1442         uint8_t portid, socket_id;
1443
1444         /* init EAL */
1445         ret = rte_eal_init(argc, argv);
1446         if (ret < 0)
1447                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1448         argc -= ret;
1449         argv += ret;
1450
1451         /* parse application arguments (after the EAL ones) */
1452         ret = parse_args(argc, argv);
1453         if (ret < 0)
1454                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
1455
1456         if ((unprotected_port_mask & enabled_port_mask) !=
1457                         unprotected_port_mask)
1458                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
1459                                 unprotected_port_mask);
1460
1461         nb_ports = rte_eth_dev_count();
1462
1463         if (check_params() < 0)
1464                 rte_exit(EXIT_FAILURE, "check_params failed\n");
1465
1466         ret = init_lcore_rx_queues();
1467         if (ret < 0)
1468                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1469
1470         nb_lcores = rte_lcore_count();
1471
1472         /* Replicate each context per socket */
1473         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1474                 if (rte_lcore_is_enabled(lcore_id) == 0)
1475                         continue;
1476
1477                 if (numa_on)
1478                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1479                 else
1480                         socket_id = 0;
1481
1482                 if (socket_ctx[socket_id].mbuf_pool)
1483                         continue;
1484
1485                 sa_init(&socket_ctx[socket_id], socket_id);
1486
1487                 sp4_init(&socket_ctx[socket_id], socket_id);
1488
1489                 sp6_init(&socket_ctx[socket_id], socket_id);
1490
1491                 rt_init(&socket_ctx[socket_id], socket_id);
1492
1493                 pool_init(&socket_ctx[socket_id], socket_id, NB_MBUF);
1494         }
1495
1496         for (portid = 0; portid < nb_ports; portid++) {
1497                 if ((enabled_port_mask & (1 << portid)) == 0)
1498                         continue;
1499
1500                 port_init(portid);
1501         }
1502
1503         cryptodevs_init();
1504
1505         /* start ports */
1506         for (portid = 0; portid < nb_ports; portid++) {
1507                 if ((enabled_port_mask & (1 << portid)) == 0)
1508                         continue;
1509
1510                 /* Start device */
1511                 ret = rte_eth_dev_start(portid);
1512                 if (ret < 0)
1513                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
1514                                         "err=%d, port=%d\n", ret, portid);
1515                 /*
1516                  * If enabled, put device in promiscuous mode.
1517                  * This allows IO forwarding mode to forward packets
1518                  * to itself through 2 cross-connected  ports of the
1519                  * target machine.
1520                  */
1521                 if (promiscuous_on)
1522                         rte_eth_promiscuous_enable(portid);
1523         }
1524
1525         check_all_ports_link_status((uint8_t)nb_ports, enabled_port_mask);
1526
1527         /* launch per-lcore init on every lcore */
1528         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1529         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1530                 if (rte_eal_wait_lcore(lcore_id) < 0)
1531                         return -1;
1532         }
1533
1534         return 0;
1535 }