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