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