examples/ipsec-secgw: get security context from lcore conf
[dpdk.git] / examples / ipsec-secgw / ipsec-secgw.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016 Intel Corporation
3  */
4
5 #include <stdbool.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <sys/types.h>
11 #include <netinet/in.h>
12 #include <netinet/ip.h>
13 #include <netinet/ip6.h>
14 #include <string.h>
15 #include <sys/queue.h>
16 #include <stdarg.h>
17 #include <errno.h>
18 #include <signal.h>
19 #include <getopt.h>
20
21 #include <rte_common.h>
22 #include <rte_bitmap.h>
23 #include <rte_byteorder.h>
24 #include <rte_log.h>
25 #include <rte_eal.h>
26 #include <rte_launch.h>
27 #include <rte_cycles.h>
28 #include <rte_prefetch.h>
29 #include <rte_lcore.h>
30 #include <rte_per_lcore.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_interrupts.h>
33 #include <rte_random.h>
34 #include <rte_debug.h>
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_mempool.h>
38 #include <rte_mbuf.h>
39 #include <rte_acl.h>
40 #include <rte_lpm.h>
41 #include <rte_lpm6.h>
42 #include <rte_hash.h>
43 #include <rte_jhash.h>
44 #include <rte_cryptodev.h>
45 #include <rte_security.h>
46 #include <rte_eventdev.h>
47 #include <rte_ip.h>
48 #include <rte_ip_frag.h>
49 #include <rte_alarm.h>
50 #include <rte_telemetry.h>
51
52 #include "event_helper.h"
53 #include "flow.h"
54 #include "ipsec.h"
55 #include "ipsec_worker.h"
56 #include "parser.h"
57 #include "sad.h"
58
59 volatile bool force_quit;
60
61 #define MAX_JUMBO_PKT_LEN  9600
62
63 #define MEMPOOL_CACHE_SIZE 256
64
65 #define CDEV_QUEUE_DESC 2048
66 #define CDEV_MAP_ENTRIES 16384
67 #define CDEV_MP_CACHE_SZ 64
68 #define CDEV_MP_CACHE_MULTIPLIER 1.5 /* from rte_mempool.c */
69 #define MAX_QUEUE_PAIRS 1
70
71 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
72
73 #define MAX_LCORE_PARAMS 1024
74
75 /*
76  * Configurable number of RX/TX ring descriptors
77  */
78 #define IPSEC_SECGW_RX_DESC_DEFAULT 1024
79 #define IPSEC_SECGW_TX_DESC_DEFAULT 1024
80 static uint16_t nb_rxd = IPSEC_SECGW_RX_DESC_DEFAULT;
81 static uint16_t nb_txd = IPSEC_SECGW_TX_DESC_DEFAULT;
82
83 #define ETHADDR_TO_UINT64(addr) __BYTES_TO_UINT64( \
84                 (addr)->addr_bytes[0], (addr)->addr_bytes[1], \
85                 (addr)->addr_bytes[2], (addr)->addr_bytes[3], \
86                 (addr)->addr_bytes[4], (addr)->addr_bytes[5], \
87                 0, 0)
88
89 #define FRAG_TBL_BUCKET_ENTRIES 4
90 #define MAX_FRAG_TTL_NS         (10LL * NS_PER_S)
91
92 #define MTU_TO_FRAMELEN(x)      ((x) + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN)
93
94 struct ethaddr_info ethaddr_tbl[RTE_MAX_ETHPORTS] = {
95         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x7e, 0x94, 0x9a) },
96         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x22, 0xa1, 0xd9) },
97         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x08, 0x69, 0x26) },
98         { 0, ETHADDR(0x00, 0x16, 0x3e, 0x49, 0x9e, 0xdd) }
99 };
100
101 struct flow_info flow_info_tbl[RTE_MAX_ETHPORTS];
102
103 #define CMD_LINE_OPT_CONFIG             "config"
104 #define CMD_LINE_OPT_SINGLE_SA          "single-sa"
105 #define CMD_LINE_OPT_CRYPTODEV_MASK     "cryptodev_mask"
106 #define CMD_LINE_OPT_TRANSFER_MODE      "transfer-mode"
107 #define CMD_LINE_OPT_SCHEDULE_TYPE      "event-schedule-type"
108 #define CMD_LINE_OPT_RX_OFFLOAD         "rxoffload"
109 #define CMD_LINE_OPT_TX_OFFLOAD         "txoffload"
110 #define CMD_LINE_OPT_REASSEMBLE         "reassemble"
111 #define CMD_LINE_OPT_MTU                "mtu"
112 #define CMD_LINE_OPT_FRAG_TTL           "frag-ttl"
113 #define CMD_LINE_OPT_EVENT_VECTOR       "event-vector"
114 #define CMD_LINE_OPT_VECTOR_SIZE        "vector-size"
115 #define CMD_LINE_OPT_VECTOR_TIMEOUT     "vector-tmo"
116 #define CMD_LINE_OPT_VECTOR_POOL_SZ     "vector-pool-sz"
117 #define CMD_LINE_OPT_PER_PORT_POOL      "per-port-pool"
118
119 #define CMD_LINE_ARG_EVENT      "event"
120 #define CMD_LINE_ARG_POLL       "poll"
121 #define CMD_LINE_ARG_ORDERED    "ordered"
122 #define CMD_LINE_ARG_ATOMIC     "atomic"
123 #define CMD_LINE_ARG_PARALLEL   "parallel"
124
125 enum {
126         /* long options mapped to a short option */
127
128         /* first long only option value must be >= 256, so that we won't
129          * conflict with short options
130          */
131         CMD_LINE_OPT_MIN_NUM = 256,
132         CMD_LINE_OPT_CONFIG_NUM,
133         CMD_LINE_OPT_SINGLE_SA_NUM,
134         CMD_LINE_OPT_CRYPTODEV_MASK_NUM,
135         CMD_LINE_OPT_TRANSFER_MODE_NUM,
136         CMD_LINE_OPT_SCHEDULE_TYPE_NUM,
137         CMD_LINE_OPT_RX_OFFLOAD_NUM,
138         CMD_LINE_OPT_TX_OFFLOAD_NUM,
139         CMD_LINE_OPT_REASSEMBLE_NUM,
140         CMD_LINE_OPT_MTU_NUM,
141         CMD_LINE_OPT_FRAG_TTL_NUM,
142         CMD_LINE_OPT_EVENT_VECTOR_NUM,
143         CMD_LINE_OPT_VECTOR_SIZE_NUM,
144         CMD_LINE_OPT_VECTOR_TIMEOUT_NUM,
145         CMD_LINE_OPT_VECTOR_POOL_SZ_NUM,
146         CMD_LINE_OPT_PER_PORT_POOL_NUM,
147 };
148
149 static const struct option lgopts[] = {
150         {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
151         {CMD_LINE_OPT_SINGLE_SA, 1, 0, CMD_LINE_OPT_SINGLE_SA_NUM},
152         {CMD_LINE_OPT_CRYPTODEV_MASK, 1, 0, CMD_LINE_OPT_CRYPTODEV_MASK_NUM},
153         {CMD_LINE_OPT_TRANSFER_MODE, 1, 0, CMD_LINE_OPT_TRANSFER_MODE_NUM},
154         {CMD_LINE_OPT_SCHEDULE_TYPE, 1, 0, CMD_LINE_OPT_SCHEDULE_TYPE_NUM},
155         {CMD_LINE_OPT_RX_OFFLOAD, 1, 0, CMD_LINE_OPT_RX_OFFLOAD_NUM},
156         {CMD_LINE_OPT_TX_OFFLOAD, 1, 0, CMD_LINE_OPT_TX_OFFLOAD_NUM},
157         {CMD_LINE_OPT_REASSEMBLE, 1, 0, CMD_LINE_OPT_REASSEMBLE_NUM},
158         {CMD_LINE_OPT_MTU, 1, 0, CMD_LINE_OPT_MTU_NUM},
159         {CMD_LINE_OPT_FRAG_TTL, 1, 0, CMD_LINE_OPT_FRAG_TTL_NUM},
160         {CMD_LINE_OPT_EVENT_VECTOR, 0, 0, CMD_LINE_OPT_EVENT_VECTOR_NUM},
161         {CMD_LINE_OPT_VECTOR_SIZE, 1, 0, CMD_LINE_OPT_VECTOR_SIZE_NUM},
162         {CMD_LINE_OPT_VECTOR_TIMEOUT, 1, 0, CMD_LINE_OPT_VECTOR_TIMEOUT_NUM},
163         {CMD_LINE_OPT_VECTOR_POOL_SZ, 1, 0, CMD_LINE_OPT_VECTOR_POOL_SZ_NUM},
164         {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PER_PORT_POOL_NUM},
165         {NULL, 0, 0, 0}
166 };
167
168 uint32_t unprotected_port_mask;
169 uint32_t single_sa_idx;
170 /* mask of enabled ports */
171 static uint32_t enabled_port_mask;
172 static uint64_t enabled_cryptodev_mask = UINT64_MAX;
173 static int32_t promiscuous_on = 1;
174 static int32_t numa_on = 1; /**< NUMA is enabled by default. */
175 static uint32_t nb_lcores;
176 static uint32_t single_sa;
177 uint32_t nb_bufs_in_pool;
178
179 /*
180  * RX/TX HW offload capabilities to enable/use on ethernet ports.
181  * By default all capabilities are enabled.
182  */
183 static uint64_t dev_rx_offload = UINT64_MAX;
184 static uint64_t dev_tx_offload = UINT64_MAX;
185
186 /*
187  * global values that determine multi-seg policy
188  */
189 uint32_t frag_tbl_sz;
190 static uint32_t frame_buf_size = RTE_MBUF_DEFAULT_BUF_SIZE;
191 uint32_t mtu_size = RTE_ETHER_MTU;
192 static uint64_t frag_ttl_ns = MAX_FRAG_TTL_NS;
193 static uint32_t stats_interval;
194
195 /* application wide librte_ipsec/SA parameters */
196 struct app_sa_prm app_sa_prm = {
197                         .enable = 0,
198                         .cache_sz = SA_CACHE_SZ,
199                         .udp_encap = 0
200                 };
201 static const char *cfgfile;
202
203 struct lcore_params {
204         uint16_t port_id;
205         uint8_t queue_id;
206         uint8_t lcore_id;
207 } __rte_cache_aligned;
208
209 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
210
211 static struct lcore_params *lcore_params;
212 static uint16_t nb_lcore_params;
213
214 static struct rte_hash *cdev_map_in;
215 static struct rte_hash *cdev_map_out;
216
217 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
218
219 static struct rte_eth_conf port_conf = {
220         .rxmode = {
221                 .mq_mode        = RTE_ETH_MQ_RX_RSS,
222                 .split_hdr_size = 0,
223                 .offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM,
224         },
225         .rx_adv_conf = {
226                 .rss_conf = {
227                         .rss_key = NULL,
228                         .rss_hf = RTE_ETH_RSS_IP | RTE_ETH_RSS_UDP |
229                                 RTE_ETH_RSS_TCP | RTE_ETH_RSS_SCTP,
230                 },
231         },
232         .txmode = {
233                 .mq_mode = RTE_ETH_MQ_TX_NONE,
234         },
235 };
236
237 struct socket_ctx socket_ctx[NB_SOCKETS];
238
239 bool per_port_pool;
240
241 /*
242  * Determine is multi-segment support required:
243  *  - either frame buffer size is smaller then mtu
244  *  - or reassemble support is requested
245  */
246 static int
247 multi_seg_required(void)
248 {
249         return (MTU_TO_FRAMELEN(mtu_size) + RTE_PKTMBUF_HEADROOM >
250                 frame_buf_size || frag_tbl_sz != 0);
251 }
252
253
254 struct ipsec_core_statistics core_statistics[RTE_MAX_LCORE];
255
256 /* Print out statistics on packet distribution */
257 static void
258 print_stats_cb(__rte_unused void *param)
259 {
260         uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
261         float burst_percent, rx_per_call, tx_per_call;
262         unsigned int coreid;
263
264         total_packets_dropped = 0;
265         total_packets_tx = 0;
266         total_packets_rx = 0;
267
268         const char clr[] = { 27, '[', '2', 'J', '\0' };
269         const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
270
271         /* Clear screen and move to top left */
272         printf("%s%s", clr, topLeft);
273
274         printf("\nCore statistics ====================================");
275
276         for (coreid = 0; coreid < RTE_MAX_LCORE; coreid++) {
277                 /* skip disabled cores */
278                 if (rte_lcore_is_enabled(coreid) == 0)
279                         continue;
280                 burst_percent = (float)(core_statistics[coreid].burst_rx * 100)/
281                                         core_statistics[coreid].rx;
282                 rx_per_call =  (float)(core_statistics[coreid].rx)/
283                                        core_statistics[coreid].rx_call;
284                 tx_per_call =  (float)(core_statistics[coreid].tx)/
285                                        core_statistics[coreid].tx_call;
286                 printf("\nStatistics for core %u ------------------------------"
287                            "\nPackets received: %20"PRIu64
288                            "\nPackets sent: %24"PRIu64
289                            "\nPackets dropped: %21"PRIu64
290                            "\nBurst percent: %23.2f"
291                            "\nPackets per Rx call: %17.2f"
292                            "\nPackets per Tx call: %17.2f",
293                            coreid,
294                            core_statistics[coreid].rx,
295                            core_statistics[coreid].tx,
296                            core_statistics[coreid].dropped,
297                            burst_percent,
298                            rx_per_call,
299                            tx_per_call);
300
301                 total_packets_dropped += core_statistics[coreid].dropped;
302                 total_packets_tx += core_statistics[coreid].tx;
303                 total_packets_rx += core_statistics[coreid].rx;
304         }
305         printf("\nAggregate statistics ==============================="
306                    "\nTotal packets received: %14"PRIu64
307                    "\nTotal packets sent: %18"PRIu64
308                    "\nTotal packets dropped: %15"PRIu64,
309                    total_packets_rx,
310                    total_packets_tx,
311                    total_packets_dropped);
312         printf("\n====================================================\n");
313
314         rte_eal_alarm_set(stats_interval * US_PER_S, print_stats_cb, NULL);
315 }
316
317 static void
318 split46_traffic(struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t num)
319 {
320         uint32_t i, n4, n6;
321         struct ip *ip;
322         struct rte_mbuf *m;
323
324         n4 = trf->ip4.num;
325         n6 = trf->ip6.num;
326
327         for (i = 0; i < num; i++) {
328
329                 m = mb[i];
330                 ip = rte_pktmbuf_mtod(m, struct ip *);
331
332                 if (ip->ip_v == IPVERSION) {
333                         trf->ip4.pkts[n4] = m;
334                         trf->ip4.data[n4] = rte_pktmbuf_mtod_offset(m,
335                                         uint8_t *, offsetof(struct ip, ip_p));
336                         n4++;
337                 } else if (ip->ip_v == IP6_VERSION) {
338                         trf->ip6.pkts[n6] = m;
339                         trf->ip6.data[n6] = rte_pktmbuf_mtod_offset(m,
340                                         uint8_t *,
341                                         offsetof(struct ip6_hdr, ip6_nxt));
342                         n6++;
343                 } else
344                         free_pkts(&m, 1);
345         }
346
347         trf->ip4.num = n4;
348         trf->ip6.num = n6;
349 }
350
351
352 static inline void
353 process_pkts_inbound(struct ipsec_ctx *ipsec_ctx,
354                 struct ipsec_traffic *traffic)
355 {
356         unsigned int lcoreid = rte_lcore_id();
357         uint16_t nb_pkts_in, n_ip4, n_ip6;
358
359         n_ip4 = traffic->ip4.num;
360         n_ip6 = traffic->ip6.num;
361
362         if (app_sa_prm.enable == 0) {
363                 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
364                                 traffic->ipsec.num, MAX_PKT_BURST);
365                 split46_traffic(traffic, traffic->ipsec.pkts, nb_pkts_in);
366         } else {
367                 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
368                         traffic->ipsec.saptr, traffic->ipsec.num);
369                 ipsec_process(ipsec_ctx, traffic);
370         }
371
372         inbound_sp_sa(ipsec_ctx->sp4_ctx,
373                 ipsec_ctx->sa_ctx, &traffic->ip4, n_ip4,
374                 &core_statistics[lcoreid].inbound.spd4);
375
376         inbound_sp_sa(ipsec_ctx->sp6_ctx,
377                 ipsec_ctx->sa_ctx, &traffic->ip6, n_ip6,
378                 &core_statistics[lcoreid].inbound.spd6);
379 }
380
381 static inline void
382 outbound_spd_lookup(struct sp_ctx *sp,
383                 struct traffic_type *ip,
384                 struct traffic_type *ipsec,
385                 struct ipsec_spd_stats *stats)
386 {
387         struct rte_mbuf *m;
388         uint32_t i, j, sa_idx;
389
390         if (ip->num == 0 || sp == NULL)
391                 return;
392
393         rte_acl_classify((struct rte_acl_ctx *)sp, ip->data, ip->res,
394                         ip->num, DEFAULT_MAX_CATEGORIES);
395
396         for (i = 0, j = 0; i < ip->num; i++) {
397                 m = ip->pkts[i];
398                 sa_idx = ip->res[i] - 1;
399
400                 if (unlikely(ip->res[i] == DISCARD)) {
401                         free_pkts(&m, 1);
402
403                         stats->discard++;
404                 } else if (unlikely(ip->res[i] == BYPASS)) {
405                         ip->pkts[j++] = m;
406
407                         stats->bypass++;
408                 } else {
409                         ipsec->res[ipsec->num] = sa_idx;
410                         ipsec->pkts[ipsec->num++] = m;
411
412                         stats->protect++;
413                 }
414         }
415         ip->num = j;
416 }
417
418 static inline void
419 process_pkts_outbound(struct ipsec_ctx *ipsec_ctx,
420                 struct ipsec_traffic *traffic)
421 {
422         struct rte_mbuf *m;
423         uint16_t idx, nb_pkts_out, i;
424         unsigned int lcoreid = rte_lcore_id();
425
426         /* Drop any IPsec traffic from protected ports */
427         free_pkts(traffic->ipsec.pkts, traffic->ipsec.num);
428
429         traffic->ipsec.num = 0;
430
431         outbound_spd_lookup(ipsec_ctx->sp4_ctx,
432                 &traffic->ip4, &traffic->ipsec,
433                 &core_statistics[lcoreid].outbound.spd4);
434
435         outbound_spd_lookup(ipsec_ctx->sp6_ctx,
436                 &traffic->ip6, &traffic->ipsec,
437                 &core_statistics[lcoreid].outbound.spd6);
438
439         if (app_sa_prm.enable == 0) {
440
441                 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
442                                 traffic->ipsec.res, traffic->ipsec.num,
443                                 MAX_PKT_BURST);
444
445                 for (i = 0; i < nb_pkts_out; i++) {
446                         m = traffic->ipsec.pkts[i];
447                         struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
448                         if (ip->ip_v == IPVERSION) {
449                                 idx = traffic->ip4.num++;
450                                 traffic->ip4.pkts[idx] = m;
451                         } else {
452                                 idx = traffic->ip6.num++;
453                                 traffic->ip6.pkts[idx] = m;
454                         }
455                 }
456         } else {
457                 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
458                         traffic->ipsec.saptr, traffic->ipsec.num);
459                 ipsec_process(ipsec_ctx, traffic);
460         }
461 }
462
463 static inline void
464 process_pkts_inbound_nosp(struct ipsec_ctx *ipsec_ctx,
465                 struct ipsec_traffic *traffic)
466 {
467         struct rte_mbuf *m;
468         uint32_t nb_pkts_in, i, idx;
469
470         if (app_sa_prm.enable == 0) {
471
472                 nb_pkts_in = ipsec_inbound(ipsec_ctx, traffic->ipsec.pkts,
473                                 traffic->ipsec.num, MAX_PKT_BURST);
474
475                 for (i = 0; i < nb_pkts_in; i++) {
476                         m = traffic->ipsec.pkts[i];
477                         struct ip *ip = rte_pktmbuf_mtod(m, struct ip *);
478                         if (ip->ip_v == IPVERSION) {
479                                 idx = traffic->ip4.num++;
480                                 traffic->ip4.pkts[idx] = m;
481                         } else {
482                                 idx = traffic->ip6.num++;
483                                 traffic->ip6.pkts[idx] = m;
484                         }
485                 }
486         } else {
487                 inbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.pkts,
488                         traffic->ipsec.saptr, traffic->ipsec.num);
489                 ipsec_process(ipsec_ctx, traffic);
490         }
491 }
492
493 static inline void
494 process_pkts_outbound_nosp(struct ipsec_ctx *ipsec_ctx,
495                 struct ipsec_traffic *traffic)
496 {
497         struct rte_mbuf *m;
498         uint32_t nb_pkts_out, i, n;
499         struct ip *ip;
500
501         /* Drop any IPsec traffic from protected ports */
502         free_pkts(traffic->ipsec.pkts, traffic->ipsec.num);
503
504         n = 0;
505
506         for (i = 0; i < traffic->ip4.num; i++) {
507                 traffic->ipsec.pkts[n] = traffic->ip4.pkts[i];
508                 traffic->ipsec.res[n++] = single_sa_idx;
509         }
510
511         for (i = 0; i < traffic->ip6.num; i++) {
512                 traffic->ipsec.pkts[n] = traffic->ip6.pkts[i];
513                 traffic->ipsec.res[n++] = single_sa_idx;
514         }
515
516         traffic->ip4.num = 0;
517         traffic->ip6.num = 0;
518         traffic->ipsec.num = n;
519
520         if (app_sa_prm.enable == 0) {
521
522                 nb_pkts_out = ipsec_outbound(ipsec_ctx, traffic->ipsec.pkts,
523                                 traffic->ipsec.res, traffic->ipsec.num,
524                                 MAX_PKT_BURST);
525
526                 /* They all sue the same SA (ip4 or ip6 tunnel) */
527                 m = traffic->ipsec.pkts[0];
528                 ip = rte_pktmbuf_mtod(m, struct ip *);
529                 if (ip->ip_v == IPVERSION) {
530                         traffic->ip4.num = nb_pkts_out;
531                         for (i = 0; i < nb_pkts_out; i++)
532                                 traffic->ip4.pkts[i] = traffic->ipsec.pkts[i];
533                 } else {
534                         traffic->ip6.num = nb_pkts_out;
535                         for (i = 0; i < nb_pkts_out; i++)
536                                 traffic->ip6.pkts[i] = traffic->ipsec.pkts[i];
537                 }
538         } else {
539                 outbound_sa_lookup(ipsec_ctx->sa_ctx, traffic->ipsec.res,
540                         traffic->ipsec.saptr, traffic->ipsec.num);
541                 ipsec_process(ipsec_ctx, traffic);
542         }
543 }
544
545 static inline void
546 process_pkts(struct lcore_conf *qconf, struct rte_mbuf **pkts,
547              uint8_t nb_pkts, uint16_t portid, struct rte_security_ctx *ctx)
548 {
549         struct ipsec_traffic traffic;
550
551         prepare_traffic(ctx, pkts, &traffic, nb_pkts);
552
553         if (unlikely(single_sa)) {
554                 if (is_unprotected_port(portid))
555                         process_pkts_inbound_nosp(&qconf->inbound, &traffic);
556                 else
557                         process_pkts_outbound_nosp(&qconf->outbound, &traffic);
558         } else {
559                 if (is_unprotected_port(portid))
560                         process_pkts_inbound(&qconf->inbound, &traffic);
561                 else
562                         process_pkts_outbound(&qconf->outbound, &traffic);
563         }
564
565         route4_pkts(qconf->rt4_ctx, traffic.ip4.pkts, traffic.ip4.num);
566         route6_pkts(qconf->rt6_ctx, traffic.ip6.pkts, traffic.ip6.num);
567 }
568
569 static inline void
570 drain_crypto_buffers(struct lcore_conf *qconf)
571 {
572         uint32_t i;
573         struct ipsec_ctx *ctx;
574
575         /* drain inbound buffers*/
576         ctx = &qconf->inbound;
577         for (i = 0; i != ctx->nb_qps; i++) {
578                 if (ctx->tbl[i].len != 0)
579                         enqueue_cop_burst(ctx->tbl  + i);
580         }
581
582         /* drain outbound buffers*/
583         ctx = &qconf->outbound;
584         for (i = 0; i != ctx->nb_qps; i++) {
585                 if (ctx->tbl[i].len != 0)
586                         enqueue_cop_burst(ctx->tbl  + i);
587         }
588 }
589
590 static void
591 drain_inbound_crypto_queues(const struct lcore_conf *qconf,
592                 struct ipsec_ctx *ctx)
593 {
594         uint32_t n;
595         struct ipsec_traffic trf;
596         unsigned int lcoreid = rte_lcore_id();
597
598         if (app_sa_prm.enable == 0) {
599
600                 /* dequeue packets from crypto-queue */
601                 n = ipsec_inbound_cqp_dequeue(ctx, trf.ipsec.pkts,
602                         RTE_DIM(trf.ipsec.pkts));
603
604                 trf.ip4.num = 0;
605                 trf.ip6.num = 0;
606
607                 /* split traffic by ipv4-ipv6 */
608                 split46_traffic(&trf, trf.ipsec.pkts, n);
609         } else
610                 ipsec_cqp_process(ctx, &trf);
611
612         /* process ipv4 packets */
613         if (trf.ip4.num != 0) {
614                 inbound_sp_sa(ctx->sp4_ctx, ctx->sa_ctx, &trf.ip4, 0,
615                         &core_statistics[lcoreid].inbound.spd4);
616                 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
617         }
618
619         /* process ipv6 packets */
620         if (trf.ip6.num != 0) {
621                 inbound_sp_sa(ctx->sp6_ctx, ctx->sa_ctx, &trf.ip6, 0,
622                         &core_statistics[lcoreid].inbound.spd6);
623                 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
624         }
625 }
626
627 static void
628 drain_outbound_crypto_queues(const struct lcore_conf *qconf,
629                 struct ipsec_ctx *ctx)
630 {
631         uint32_t n;
632         struct ipsec_traffic trf;
633
634         if (app_sa_prm.enable == 0) {
635
636                 /* dequeue packets from crypto-queue */
637                 n = ipsec_outbound_cqp_dequeue(ctx, trf.ipsec.pkts,
638                         RTE_DIM(trf.ipsec.pkts));
639
640                 trf.ip4.num = 0;
641                 trf.ip6.num = 0;
642
643                 /* split traffic by ipv4-ipv6 */
644                 split46_traffic(&trf, trf.ipsec.pkts, n);
645         } else
646                 ipsec_cqp_process(ctx, &trf);
647
648         /* process ipv4 packets */
649         if (trf.ip4.num != 0)
650                 route4_pkts(qconf->rt4_ctx, trf.ip4.pkts, trf.ip4.num);
651
652         /* process ipv6 packets */
653         if (trf.ip6.num != 0)
654                 route6_pkts(qconf->rt6_ctx, trf.ip6.pkts, trf.ip6.num);
655 }
656
657 /* main processing loop */
658 void
659 ipsec_poll_mode_worker(void)
660 {
661         struct rte_mbuf *pkts[MAX_PKT_BURST];
662         uint32_t lcore_id;
663         uint64_t prev_tsc, diff_tsc, cur_tsc;
664         int32_t i, nb_rx;
665         uint16_t portid;
666         uint8_t queueid;
667         struct lcore_conf *qconf;
668         int32_t rc, socket_id;
669         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1)
670                         / US_PER_S * BURST_TX_DRAIN_US;
671         struct lcore_rx_queue *rxql;
672
673         prev_tsc = 0;
674         lcore_id = rte_lcore_id();
675         qconf = &lcore_conf[lcore_id];
676         rxql = qconf->rx_queue_list;
677         socket_id = rte_lcore_to_socket_id(lcore_id);
678
679         qconf->rt4_ctx = socket_ctx[socket_id].rt_ip4;
680         qconf->rt6_ctx = socket_ctx[socket_id].rt_ip6;
681         qconf->inbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_in;
682         qconf->inbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_in;
683         qconf->inbound.sa_ctx = socket_ctx[socket_id].sa_in;
684         qconf->inbound.cdev_map = cdev_map_in;
685         qconf->inbound.session_pool = socket_ctx[socket_id].session_pool;
686         qconf->inbound.session_priv_pool =
687                         socket_ctx[socket_id].session_priv_pool;
688         qconf->outbound.sp4_ctx = socket_ctx[socket_id].sp_ip4_out;
689         qconf->outbound.sp6_ctx = socket_ctx[socket_id].sp_ip6_out;
690         qconf->outbound.sa_ctx = socket_ctx[socket_id].sa_out;
691         qconf->outbound.cdev_map = cdev_map_out;
692         qconf->outbound.session_pool = socket_ctx[socket_id].session_pool;
693         qconf->outbound.session_priv_pool =
694                         socket_ctx[socket_id].session_priv_pool;
695         qconf->frag.pool_indir = socket_ctx[socket_id].mbuf_pool_indir;
696
697         rc = ipsec_sad_lcore_cache_init(app_sa_prm.cache_sz);
698         if (rc != 0) {
699                 RTE_LOG(ERR, IPSEC,
700                         "SAD cache init on lcore %u, failed with code: %d\n",
701                         lcore_id, rc);
702                 return;
703         }
704
705         if (qconf->nb_rx_queue == 0) {
706                 RTE_LOG(DEBUG, IPSEC, "lcore %u has nothing to do\n",
707                         lcore_id);
708                 return;
709         }
710
711         RTE_LOG(INFO, IPSEC, "entering main loop on lcore %u\n", lcore_id);
712
713         for (i = 0; i < qconf->nb_rx_queue; i++) {
714                 portid = rxql[i].port_id;
715                 queueid = rxql[i].queue_id;
716                 RTE_LOG(INFO, IPSEC,
717                         " -- lcoreid=%u portid=%u rxqueueid=%hhu\n",
718                         lcore_id, portid, queueid);
719         }
720
721         while (!force_quit) {
722                 cur_tsc = rte_rdtsc();
723
724                 /* TX queue buffer drain */
725                 diff_tsc = cur_tsc - prev_tsc;
726
727                 if (unlikely(diff_tsc > drain_tsc)) {
728                         drain_tx_buffers(qconf);
729                         drain_crypto_buffers(qconf);
730                         prev_tsc = cur_tsc;
731                 }
732
733                 for (i = 0; i < qconf->nb_rx_queue; ++i) {
734
735                         /* Read packets from RX queues */
736                         portid = rxql[i].port_id;
737                         queueid = rxql[i].queue_id;
738                         nb_rx = rte_eth_rx_burst(portid, queueid,
739                                         pkts, MAX_PKT_BURST);
740
741                         if (nb_rx > 0) {
742                                 core_stats_update_rx(nb_rx);
743                                 process_pkts(qconf, pkts, nb_rx, portid,
744                                              rxql->sec_ctx);
745                         }
746
747                         /* dequeue and process completed crypto-ops */
748                         if (is_unprotected_port(portid))
749                                 drain_inbound_crypto_queues(qconf,
750                                         &qconf->inbound);
751                         else
752                                 drain_outbound_crypto_queues(qconf,
753                                         &qconf->outbound);
754                 }
755         }
756 }
757
758 int
759 check_flow_params(uint16_t fdir_portid, uint8_t fdir_qid)
760 {
761         uint16_t i;
762         uint16_t portid;
763         uint8_t queueid;
764
765         for (i = 0; i < nb_lcore_params; ++i) {
766                 portid = lcore_params_array[i].port_id;
767                 if (portid == fdir_portid) {
768                         queueid = lcore_params_array[i].queue_id;
769                         if (queueid == fdir_qid)
770                                 break;
771                 }
772
773                 if (i == nb_lcore_params - 1)
774                         return -1;
775         }
776
777         return 1;
778 }
779
780 static int32_t
781 check_poll_mode_params(struct eh_conf *eh_conf)
782 {
783         uint8_t lcore;
784         uint16_t portid;
785         uint16_t i;
786         int32_t socket_id;
787
788         if (!eh_conf)
789                 return -EINVAL;
790
791         if (eh_conf->mode != EH_PKT_TRANSFER_MODE_POLL)
792                 return 0;
793
794         if (lcore_params == NULL) {
795                 printf("Error: No port/queue/core mappings\n");
796                 return -1;
797         }
798
799         for (i = 0; i < nb_lcore_params; ++i) {
800                 lcore = lcore_params[i].lcore_id;
801                 if (!rte_lcore_is_enabled(lcore)) {
802                         printf("error: lcore %hhu is not enabled in "
803                                 "lcore mask\n", lcore);
804                         return -1;
805                 }
806                 socket_id = rte_lcore_to_socket_id(lcore);
807                 if (socket_id != 0 && numa_on == 0) {
808                         printf("warning: lcore %hhu is on socket %d "
809                                 "with numa off\n",
810                                 lcore, socket_id);
811                 }
812                 portid = lcore_params[i].port_id;
813                 if ((enabled_port_mask & (1 << portid)) == 0) {
814                         printf("port %u is not enabled in port mask\n", portid);
815                         return -1;
816                 }
817                 if (!rte_eth_dev_is_valid_port(portid)) {
818                         printf("port %u is not present on the board\n", portid);
819                         return -1;
820                 }
821         }
822         return 0;
823 }
824
825 static uint8_t
826 get_port_nb_rx_queues(const uint16_t port)
827 {
828         int32_t queue = -1;
829         uint16_t i;
830
831         for (i = 0; i < nb_lcore_params; ++i) {
832                 if (lcore_params[i].port_id == port &&
833                                 lcore_params[i].queue_id > queue)
834                         queue = lcore_params[i].queue_id;
835         }
836         return (uint8_t)(++queue);
837 }
838
839 static int32_t
840 init_lcore_rx_queues(void)
841 {
842         uint16_t i, nb_rx_queue;
843         uint8_t lcore;
844
845         for (i = 0; i < nb_lcore_params; ++i) {
846                 lcore = lcore_params[i].lcore_id;
847                 nb_rx_queue = lcore_conf[lcore].nb_rx_queue;
848                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
849                         printf("error: too many queues (%u) for lcore: %u\n",
850                                         nb_rx_queue + 1, lcore);
851                         return -1;
852                 }
853                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
854                         lcore_params[i].port_id;
855                 lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
856                         lcore_params[i].queue_id;
857                 lcore_conf[lcore].nb_rx_queue++;
858         }
859         return 0;
860 }
861
862 /* display usage */
863 static void
864 print_usage(const char *prgname)
865 {
866         fprintf(stderr, "%s [EAL options] --"
867                 " -p PORTMASK"
868                 " [-P]"
869                 " [-u PORTMASK]"
870                 " [-j FRAMESIZE]"
871                 " [-l]"
872                 " [-w REPLAY_WINDOW_SIZE]"
873                 " [-e]"
874                 " [-a]"
875                 " [-c]"
876                 " [-t STATS_INTERVAL]"
877                 " [-s NUMBER_OF_MBUFS_IN_PKT_POOL]"
878                 " -f CONFIG_FILE"
879                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
880                 " [--single-sa SAIDX]"
881                 " [--cryptodev_mask MASK]"
882                 " [--transfer-mode MODE]"
883                 " [--event-schedule-type TYPE]"
884                 " [--" CMD_LINE_OPT_RX_OFFLOAD " RX_OFFLOAD_MASK]"
885                 " [--" CMD_LINE_OPT_TX_OFFLOAD " TX_OFFLOAD_MASK]"
886                 " [--" CMD_LINE_OPT_REASSEMBLE " REASSEMBLE_TABLE_SIZE]"
887                 " [--" CMD_LINE_OPT_MTU " MTU]"
888                 " [--event-vector]"
889                 " [--vector-size SIZE]"
890                 " [--vector-tmo TIMEOUT in ns]"
891                 "\n\n"
892                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
893                 "  -P : Enable promiscuous mode\n"
894                 "  -u PORTMASK: Hexadecimal bitmask of unprotected ports\n"
895                 "  -j FRAMESIZE: Data buffer size, minimum (and default)\n"
896                 "     value: RTE_MBUF_DEFAULT_BUF_SIZE\n"
897                 "  -l enables code-path that uses librte_ipsec\n"
898                 "  -w REPLAY_WINDOW_SIZE specifies IPsec SQN replay window\n"
899                 "     size for each SA\n"
900                 "  -e enables ESN\n"
901                 "  -a enables SA SQN atomic behaviour\n"
902                 "  -c specifies inbound SAD cache size,\n"
903                 "     zero value disables the cache (default value: 128)\n"
904                 "  -t specifies statistics screen update interval,\n"
905                 "     zero disables statistics screen (default value: 0)\n"
906                 "  -s number of mbufs in packet pool, if not specified number\n"
907                 "     of mbufs will be calculated based on number of cores,\n"
908                 "     ports and crypto queues\n"
909                 "  -f CONFIG_FILE: Configuration file\n"
910                 "  --config (port,queue,lcore): Rx queue configuration. In poll\n"
911                 "                               mode determines which queues from\n"
912                 "                               which ports are mapped to which cores.\n"
913                 "                               In event mode this option is not used\n"
914                 "                               as packets are dynamically scheduled\n"
915                 "                               to cores by HW.\n"
916                 "  --single-sa SAIDX: In poll mode use single SA index for\n"
917                 "                     outbound traffic, bypassing the SP\n"
918                 "                     In event mode selects driver submode,\n"
919                 "                     SA index value is ignored\n"
920                 "  --cryptodev_mask MASK: Hexadecimal bitmask of the crypto\n"
921                 "                         devices to configure\n"
922                 "  --transfer-mode MODE\n"
923                 "               \"poll\"  : Packet transfer via polling (default)\n"
924                 "               \"event\" : Packet transfer via event device\n"
925                 "  --event-schedule-type TYPE queue schedule type, used only when\n"
926                 "                             transfer mode is set to event\n"
927                 "               \"ordered\"  : Ordered (default)\n"
928                 "               \"atomic\"   : Atomic\n"
929                 "               \"parallel\" : Parallel\n"
930                 "  --" CMD_LINE_OPT_RX_OFFLOAD
931                 ": bitmask of the RX HW offload capabilities to enable/use\n"
932                 "                         (RTE_ETH_RX_OFFLOAD_*)\n"
933                 "  --" CMD_LINE_OPT_TX_OFFLOAD
934                 ": bitmask of the TX HW offload capabilities to enable/use\n"
935                 "                         (RTE_ETH_TX_OFFLOAD_*)\n"
936                 "  --" CMD_LINE_OPT_REASSEMBLE " NUM"
937                 ": max number of entries in reassemble(fragment) table\n"
938                 "    (zero (default value) disables reassembly)\n"
939                 "  --" CMD_LINE_OPT_MTU " MTU"
940                 ": MTU value on all ports (default value: 1500)\n"
941                 "    outgoing packets with bigger size will be fragmented\n"
942                 "    incoming packets with bigger size will be discarded\n"
943                 "  --" CMD_LINE_OPT_FRAG_TTL " FRAG_TTL_NS"
944                 ": fragments lifetime in nanoseconds, default\n"
945                 "    and maximum value is 10.000.000.000 ns (10 s)\n"
946                 "  --event-vector enables event vectorization\n"
947                 "  --vector-size Max vector size (default value: 16)\n"
948                 "  --vector-tmo Max vector timeout in nanoseconds"
949                 "    (default value: 102400)\n"
950                 "  --" CMD_LINE_OPT_PER_PORT_POOL " Enable per port mbuf pool\n"
951                 "  --" CMD_LINE_OPT_VECTOR_POOL_SZ " Vector pool size\n"
952                 "                    (default value is based on mbuf count)\n"
953                 "\n",
954                 prgname);
955 }
956
957 static int
958 parse_mask(const char *str, uint64_t *val)
959 {
960         char *end;
961         unsigned long t;
962
963         errno = 0;
964         t = strtoul(str, &end, 0);
965         if (errno != 0 || end[0] != 0)
966                 return -EINVAL;
967
968         *val = t;
969         return 0;
970 }
971
972 static int32_t
973 parse_portmask(const char *portmask)
974 {
975         char *end = NULL;
976         unsigned long pm;
977
978         errno = 0;
979
980         /* parse hexadecimal string */
981         pm = strtoul(portmask, &end, 16);
982         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
983                 return -1;
984
985         if ((pm == 0) && errno)
986                 return -1;
987
988         return pm;
989 }
990
991 static int64_t
992 parse_decimal(const char *str)
993 {
994         char *end = NULL;
995         uint64_t num;
996
997         num = strtoull(str, &end, 10);
998         if ((str[0] == '\0') || (end == NULL) || (*end != '\0')
999                 || num > INT64_MAX)
1000                 return -1;
1001
1002         return num;
1003 }
1004
1005 static int32_t
1006 parse_config(const char *q_arg)
1007 {
1008         char s[256];
1009         const char *p, *p0 = q_arg;
1010         char *end;
1011         enum fieldnames {
1012                 FLD_PORT = 0,
1013                 FLD_QUEUE,
1014                 FLD_LCORE,
1015                 _NUM_FLD
1016         };
1017         unsigned long int_fld[_NUM_FLD];
1018         char *str_fld[_NUM_FLD];
1019         int32_t i;
1020         uint32_t size;
1021
1022         nb_lcore_params = 0;
1023
1024         while ((p = strchr(p0, '(')) != NULL) {
1025                 ++p;
1026                 p0 = strchr(p, ')');
1027                 if (p0 == NULL)
1028                         return -1;
1029
1030                 size = p0 - p;
1031                 if (size >= sizeof(s))
1032                         return -1;
1033
1034                 snprintf(s, sizeof(s), "%.*s", size, p);
1035                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
1036                                 _NUM_FLD)
1037                         return -1;
1038                 for (i = 0; i < _NUM_FLD; i++) {
1039                         errno = 0;
1040                         int_fld[i] = strtoul(str_fld[i], &end, 0);
1041                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
1042                                 return -1;
1043                 }
1044                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
1045                         printf("exceeded max number of lcore params: %hu\n",
1046                                 nb_lcore_params);
1047                         return -1;
1048                 }
1049                 lcore_params_array[nb_lcore_params].port_id =
1050                         (uint8_t)int_fld[FLD_PORT];
1051                 lcore_params_array[nb_lcore_params].queue_id =
1052                         (uint8_t)int_fld[FLD_QUEUE];
1053                 lcore_params_array[nb_lcore_params].lcore_id =
1054                         (uint8_t)int_fld[FLD_LCORE];
1055                 ++nb_lcore_params;
1056         }
1057         lcore_params = lcore_params_array;
1058         return 0;
1059 }
1060
1061 static void
1062 print_app_sa_prm(const struct app_sa_prm *prm)
1063 {
1064         printf("librte_ipsec usage: %s\n",
1065                 (prm->enable == 0) ? "disabled" : "enabled");
1066
1067         printf("replay window size: %u\n", prm->window_size);
1068         printf("ESN: %s\n", (prm->enable_esn == 0) ? "disabled" : "enabled");
1069         printf("SA flags: %#" PRIx64 "\n", prm->flags);
1070         printf("Frag TTL: %" PRIu64 " ns\n", frag_ttl_ns);
1071 }
1072
1073 static int
1074 parse_transfer_mode(struct eh_conf *conf, const char *optarg)
1075 {
1076         if (!strcmp(CMD_LINE_ARG_POLL, optarg))
1077                 conf->mode = EH_PKT_TRANSFER_MODE_POLL;
1078         else if (!strcmp(CMD_LINE_ARG_EVENT, optarg))
1079                 conf->mode = EH_PKT_TRANSFER_MODE_EVENT;
1080         else {
1081                 printf("Unsupported packet transfer mode\n");
1082                 return -EINVAL;
1083         }
1084
1085         return 0;
1086 }
1087
1088 static int
1089 parse_schedule_type(struct eh_conf *conf, const char *optarg)
1090 {
1091         struct eventmode_conf *em_conf = NULL;
1092
1093         /* Get eventmode conf */
1094         em_conf = conf->mode_params;
1095
1096         if (!strcmp(CMD_LINE_ARG_ORDERED, optarg))
1097                 em_conf->ext_params.sched_type = RTE_SCHED_TYPE_ORDERED;
1098         else if (!strcmp(CMD_LINE_ARG_ATOMIC, optarg))
1099                 em_conf->ext_params.sched_type = RTE_SCHED_TYPE_ATOMIC;
1100         else if (!strcmp(CMD_LINE_ARG_PARALLEL, optarg))
1101                 em_conf->ext_params.sched_type = RTE_SCHED_TYPE_PARALLEL;
1102         else {
1103                 printf("Unsupported queue schedule type\n");
1104                 return -EINVAL;
1105         }
1106
1107         return 0;
1108 }
1109
1110 static int32_t
1111 parse_args(int32_t argc, char **argv, struct eh_conf *eh_conf)
1112 {
1113         int opt;
1114         int64_t ret;
1115         char **argvopt;
1116         int32_t option_index;
1117         char *prgname = argv[0];
1118         int32_t f_present = 0;
1119         struct eventmode_conf *em_conf = NULL;
1120
1121         argvopt = argv;
1122
1123         while ((opt = getopt_long(argc, argvopt, "aelp:Pu:f:j:w:c:t:s:",
1124                                 lgopts, &option_index)) != EOF) {
1125
1126                 switch (opt) {
1127                 case 'p':
1128                         enabled_port_mask = parse_portmask(optarg);
1129                         if (enabled_port_mask == 0) {
1130                                 printf("invalid portmask\n");
1131                                 print_usage(prgname);
1132                                 return -1;
1133                         }
1134                         break;
1135                 case 'P':
1136                         printf("Promiscuous mode selected\n");
1137                         promiscuous_on = 1;
1138                         break;
1139                 case 'u':
1140                         unprotected_port_mask = parse_portmask(optarg);
1141                         if (unprotected_port_mask == 0) {
1142                                 printf("invalid unprotected portmask\n");
1143                                 print_usage(prgname);
1144                                 return -1;
1145                         }
1146                         break;
1147                 case 'f':
1148                         if (f_present == 1) {
1149                                 printf("\"-f\" option present more than "
1150                                         "once!\n");
1151                                 print_usage(prgname);
1152                                 return -1;
1153                         }
1154                         cfgfile = optarg;
1155                         f_present = 1;
1156                         break;
1157
1158                 case 's':
1159                         ret = parse_decimal(optarg);
1160                         if (ret < 0) {
1161                                 printf("Invalid number of buffers in a pool: "
1162                                         "%s\n", optarg);
1163                                 print_usage(prgname);
1164                                 return -1;
1165                         }
1166
1167                         nb_bufs_in_pool = ret;
1168                         break;
1169
1170                 case 'j':
1171                         ret = parse_decimal(optarg);
1172                         if (ret < RTE_MBUF_DEFAULT_BUF_SIZE ||
1173                                         ret > UINT16_MAX) {
1174                                 printf("Invalid frame buffer size value: %s\n",
1175                                         optarg);
1176                                 print_usage(prgname);
1177                                 return -1;
1178                         }
1179                         frame_buf_size = ret;
1180                         printf("Custom frame buffer size %u\n", frame_buf_size);
1181                         break;
1182                 case 'l':
1183                         app_sa_prm.enable = 1;
1184                         break;
1185                 case 'w':
1186                         app_sa_prm.window_size = parse_decimal(optarg);
1187                         break;
1188                 case 'e':
1189                         app_sa_prm.enable_esn = 1;
1190                         break;
1191                 case 'a':
1192                         app_sa_prm.enable = 1;
1193                         app_sa_prm.flags |= RTE_IPSEC_SAFLAG_SQN_ATOM;
1194                         break;
1195                 case 'c':
1196                         ret = parse_decimal(optarg);
1197                         if (ret < 0) {
1198                                 printf("Invalid SA cache size: %s\n", optarg);
1199                                 print_usage(prgname);
1200                                 return -1;
1201                         }
1202                         app_sa_prm.cache_sz = ret;
1203                         break;
1204                 case 't':
1205                         ret = parse_decimal(optarg);
1206                         if (ret < 0) {
1207                                 printf("Invalid interval value: %s\n", optarg);
1208                                 print_usage(prgname);
1209                                 return -1;
1210                         }
1211                         stats_interval = ret;
1212                         break;
1213                 case CMD_LINE_OPT_CONFIG_NUM:
1214                         ret = parse_config(optarg);
1215                         if (ret) {
1216                                 printf("Invalid config\n");
1217                                 print_usage(prgname);
1218                                 return -1;
1219                         }
1220                         break;
1221                 case CMD_LINE_OPT_SINGLE_SA_NUM:
1222                         ret = parse_decimal(optarg);
1223                         if (ret == -1 || ret > UINT32_MAX) {
1224                                 printf("Invalid argument[sa_idx]\n");
1225                                 print_usage(prgname);
1226                                 return -1;
1227                         }
1228
1229                         /* else */
1230                         single_sa = 1;
1231                         single_sa_idx = ret;
1232                         eh_conf->ipsec_mode = EH_IPSEC_MODE_TYPE_DRIVER;
1233                         printf("Configured with single SA index %u\n",
1234                                         single_sa_idx);
1235                         break;
1236                 case CMD_LINE_OPT_CRYPTODEV_MASK_NUM:
1237                         ret = parse_portmask(optarg);
1238                         if (ret == -1) {
1239                                 printf("Invalid argument[portmask]\n");
1240                                 print_usage(prgname);
1241                                 return -1;
1242                         }
1243
1244                         /* else */
1245                         enabled_cryptodev_mask = ret;
1246                         break;
1247
1248                 case CMD_LINE_OPT_TRANSFER_MODE_NUM:
1249                         ret = parse_transfer_mode(eh_conf, optarg);
1250                         if (ret < 0) {
1251                                 printf("Invalid packet transfer mode\n");
1252                                 print_usage(prgname);
1253                                 return -1;
1254                         }
1255                         break;
1256
1257                 case CMD_LINE_OPT_SCHEDULE_TYPE_NUM:
1258                         ret = parse_schedule_type(eh_conf, optarg);
1259                         if (ret < 0) {
1260                                 printf("Invalid queue schedule type\n");
1261                                 print_usage(prgname);
1262                                 return -1;
1263                         }
1264                         break;
1265
1266                 case CMD_LINE_OPT_RX_OFFLOAD_NUM:
1267                         ret = parse_mask(optarg, &dev_rx_offload);
1268                         if (ret != 0) {
1269                                 printf("Invalid argument for \'%s\': %s\n",
1270                                         CMD_LINE_OPT_RX_OFFLOAD, optarg);
1271                                 print_usage(prgname);
1272                                 return -1;
1273                         }
1274                         break;
1275                 case CMD_LINE_OPT_TX_OFFLOAD_NUM:
1276                         ret = parse_mask(optarg, &dev_tx_offload);
1277                         if (ret != 0) {
1278                                 printf("Invalid argument for \'%s\': %s\n",
1279                                         CMD_LINE_OPT_TX_OFFLOAD, optarg);
1280                                 print_usage(prgname);
1281                                 return -1;
1282                         }
1283                         break;
1284                 case CMD_LINE_OPT_REASSEMBLE_NUM:
1285                         ret = parse_decimal(optarg);
1286                         if (ret < 0 || ret > UINT32_MAX) {
1287                                 printf("Invalid argument for \'%s\': %s\n",
1288                                         CMD_LINE_OPT_REASSEMBLE, optarg);
1289                                 print_usage(prgname);
1290                                 return -1;
1291                         }
1292                         frag_tbl_sz = ret;
1293                         break;
1294                 case CMD_LINE_OPT_MTU_NUM:
1295                         ret = parse_decimal(optarg);
1296                         if (ret < 0 || ret > RTE_IPV4_MAX_PKT_LEN) {
1297                                 printf("Invalid argument for \'%s\': %s\n",
1298                                         CMD_LINE_OPT_MTU, optarg);
1299                                 print_usage(prgname);
1300                                 return -1;
1301                         }
1302                         mtu_size = ret;
1303                         break;
1304                 case CMD_LINE_OPT_FRAG_TTL_NUM:
1305                         ret = parse_decimal(optarg);
1306                         if (ret < 0 || ret > MAX_FRAG_TTL_NS) {
1307                                 printf("Invalid argument for \'%s\': %s\n",
1308                                         CMD_LINE_OPT_MTU, optarg);
1309                                 print_usage(prgname);
1310                                 return -1;
1311                         }
1312                         frag_ttl_ns = ret;
1313                         break;
1314                 case CMD_LINE_OPT_EVENT_VECTOR_NUM:
1315                         em_conf = eh_conf->mode_params;
1316                         em_conf->ext_params.event_vector = 1;
1317                         break;
1318                 case CMD_LINE_OPT_VECTOR_SIZE_NUM:
1319                         ret = parse_decimal(optarg);
1320
1321                         if (ret > MAX_PKT_BURST_VEC) {
1322                                 printf("Invalid argument for \'%s\': %s\n",
1323                                         CMD_LINE_OPT_VECTOR_SIZE, optarg);
1324                                 print_usage(prgname);
1325                                 return -1;
1326                         }
1327                         em_conf = eh_conf->mode_params;
1328                         em_conf->ext_params.vector_size = ret;
1329                         break;
1330                 case CMD_LINE_OPT_VECTOR_TIMEOUT_NUM:
1331                         ret = parse_decimal(optarg);
1332
1333                         em_conf = eh_conf->mode_params;
1334                         em_conf->vector_tmo_ns = ret;
1335                         break;
1336                 case CMD_LINE_OPT_VECTOR_POOL_SZ_NUM:
1337                         ret = parse_decimal(optarg);
1338
1339                         em_conf = eh_conf->mode_params;
1340                         em_conf->vector_pool_sz = ret;
1341                         break;
1342                 case CMD_LINE_OPT_PER_PORT_POOL_NUM:
1343                         per_port_pool = 1;
1344                         break;
1345                 default:
1346                         print_usage(prgname);
1347                         return -1;
1348                 }
1349         }
1350
1351         if (f_present == 0) {
1352                 printf("Mandatory option \"-f\" not present\n");
1353                 return -1;
1354         }
1355
1356         /* check do we need to enable multi-seg support */
1357         if (multi_seg_required()) {
1358                 /* legacy mode doesn't support multi-seg */
1359                 app_sa_prm.enable = 1;
1360                 printf("frame buf size: %u, mtu: %u, "
1361                         "number of reassemble entries: %u\n"
1362                         "multi-segment support is required\n",
1363                         frame_buf_size, mtu_size, frag_tbl_sz);
1364         }
1365
1366         print_app_sa_prm(&app_sa_prm);
1367
1368         if (optind >= 0)
1369                 argv[optind-1] = prgname;
1370
1371         ret = optind-1;
1372         optind = 1; /* reset getopt lib */
1373         return ret;
1374 }
1375
1376 static void
1377 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
1378 {
1379         char buf[RTE_ETHER_ADDR_FMT_SIZE];
1380         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
1381         printf("%s%s", name, buf);
1382 }
1383
1384 /*
1385  * Update destination ethaddr for the port.
1386  */
1387 int
1388 add_dst_ethaddr(uint16_t port, const struct rte_ether_addr *addr)
1389 {
1390         if (port >= RTE_DIM(ethaddr_tbl))
1391                 return -EINVAL;
1392
1393         ethaddr_tbl[port].dst = ETHADDR_TO_UINT64(addr);
1394         return 0;
1395 }
1396
1397 /* Check the link status of all ports in up to 9s, and print them finally */
1398 static void
1399 check_all_ports_link_status(uint32_t port_mask)
1400 {
1401 #define CHECK_INTERVAL 100 /* 100ms */
1402 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1403         uint16_t portid;
1404         uint8_t count, all_ports_up, print_flag = 0;
1405         struct rte_eth_link link;
1406         int ret;
1407         char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1408
1409         printf("\nChecking link status");
1410         fflush(stdout);
1411         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1412                 all_ports_up = 1;
1413                 RTE_ETH_FOREACH_DEV(portid) {
1414                         if ((port_mask & (1 << portid)) == 0)
1415                                 continue;
1416                         memset(&link, 0, sizeof(link));
1417                         ret = rte_eth_link_get_nowait(portid, &link);
1418                         if (ret < 0) {
1419                                 all_ports_up = 0;
1420                                 if (print_flag == 1)
1421                                         printf("Port %u link get failed: %s\n",
1422                                                 portid, rte_strerror(-ret));
1423                                 continue;
1424                         }
1425                         /* print link status if flag set */
1426                         if (print_flag == 1) {
1427                                 rte_eth_link_to_str(link_status_text,
1428                                         sizeof(link_status_text), &link);
1429                                 printf("Port %d %s\n", portid,
1430                                        link_status_text);
1431                                 continue;
1432                         }
1433                         /* clear all_ports_up flag if any link down */
1434                         if (link.link_status == RTE_ETH_LINK_DOWN) {
1435                                 all_ports_up = 0;
1436                                 break;
1437                         }
1438                 }
1439                 /* after finally printing all link status, get out */
1440                 if (print_flag == 1)
1441                         break;
1442
1443                 if (all_ports_up == 0) {
1444                         printf(".");
1445                         fflush(stdout);
1446                         rte_delay_ms(CHECK_INTERVAL);
1447                 }
1448
1449                 /* set the print_flag if all ports up or timeout */
1450                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1451                         print_flag = 1;
1452                         printf("done\n");
1453                 }
1454         }
1455 }
1456
1457 static int32_t
1458 add_mapping(struct rte_hash *map, const char *str, uint16_t cdev_id,
1459                 uint16_t qp, struct lcore_params *params,
1460                 struct ipsec_ctx *ipsec_ctx,
1461                 const struct rte_cryptodev_capabilities *cipher,
1462                 const struct rte_cryptodev_capabilities *auth,
1463                 const struct rte_cryptodev_capabilities *aead)
1464 {
1465         int32_t ret = 0;
1466         unsigned long i;
1467         struct cdev_key key = { 0 };
1468
1469         key.lcore_id = params->lcore_id;
1470         if (cipher)
1471                 key.cipher_algo = cipher->sym.cipher.algo;
1472         if (auth)
1473                 key.auth_algo = auth->sym.auth.algo;
1474         if (aead)
1475                 key.aead_algo = aead->sym.aead.algo;
1476
1477         ret = rte_hash_lookup(map, &key);
1478         if (ret != -ENOENT)
1479                 return 0;
1480
1481         for (i = 0; i < ipsec_ctx->nb_qps; i++)
1482                 if (ipsec_ctx->tbl[i].id == cdev_id)
1483                         break;
1484
1485         if (i == ipsec_ctx->nb_qps) {
1486                 if (ipsec_ctx->nb_qps == MAX_QP_PER_LCORE) {
1487                         printf("Maximum number of crypto devices assigned to "
1488                                 "a core, increase MAX_QP_PER_LCORE value\n");
1489                         return 0;
1490                 }
1491                 ipsec_ctx->tbl[i].id = cdev_id;
1492                 ipsec_ctx->tbl[i].qp = qp;
1493                 ipsec_ctx->nb_qps++;
1494                 printf("%s cdev mapping: lcore %u using cdev %u qp %u "
1495                                 "(cdev_id_qp %lu)\n", str, key.lcore_id,
1496                                 cdev_id, qp, i);
1497         }
1498
1499         ret = rte_hash_add_key_data(map, &key, (void *)i);
1500         if (ret < 0) {
1501                 printf("Failed to insert cdev mapping for (lcore %u, "
1502                                 "cdev %u, qp %u), errno %d\n",
1503                                 key.lcore_id, ipsec_ctx->tbl[i].id,
1504                                 ipsec_ctx->tbl[i].qp, ret);
1505                 return 0;
1506         }
1507
1508         return 1;
1509 }
1510
1511 static int32_t
1512 add_cdev_mapping(struct rte_cryptodev_info *dev_info, uint16_t cdev_id,
1513                 uint16_t qp, struct lcore_params *params)
1514 {
1515         int32_t ret = 0;
1516         const struct rte_cryptodev_capabilities *i, *j;
1517         struct rte_hash *map;
1518         struct lcore_conf *qconf;
1519         struct ipsec_ctx *ipsec_ctx;
1520         const char *str;
1521
1522         qconf = &lcore_conf[params->lcore_id];
1523
1524         if ((unprotected_port_mask & (1 << params->port_id)) == 0) {
1525                 map = cdev_map_out;
1526                 ipsec_ctx = &qconf->outbound;
1527                 str = "Outbound";
1528         } else {
1529                 map = cdev_map_in;
1530                 ipsec_ctx = &qconf->inbound;
1531                 str = "Inbound";
1532         }
1533
1534         /* Required cryptodevs with operation chaining */
1535         if (!(dev_info->feature_flags &
1536                                 RTE_CRYPTODEV_FF_SYM_OPERATION_CHAINING))
1537                 return ret;
1538
1539         for (i = dev_info->capabilities;
1540                         i->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; i++) {
1541                 if (i->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1542                         continue;
1543
1544                 if (i->sym.xform_type == RTE_CRYPTO_SYM_XFORM_AEAD) {
1545                         ret |= add_mapping(map, str, cdev_id, qp, params,
1546                                         ipsec_ctx, NULL, NULL, i);
1547                         continue;
1548                 }
1549
1550                 if (i->sym.xform_type != RTE_CRYPTO_SYM_XFORM_CIPHER)
1551                         continue;
1552
1553                 for (j = dev_info->capabilities;
1554                                 j->op != RTE_CRYPTO_OP_TYPE_UNDEFINED; j++) {
1555                         if (j->op != RTE_CRYPTO_OP_TYPE_SYMMETRIC)
1556                                 continue;
1557
1558                         if (j->sym.xform_type != RTE_CRYPTO_SYM_XFORM_AUTH)
1559                                 continue;
1560
1561                         ret |= add_mapping(map, str, cdev_id, qp, params,
1562                                                 ipsec_ctx, i, j, NULL);
1563                 }
1564         }
1565
1566         return ret;
1567 }
1568
1569 /* Check if the device is enabled by cryptodev_mask */
1570 static int
1571 check_cryptodev_mask(uint8_t cdev_id)
1572 {
1573         if (enabled_cryptodev_mask & (1 << cdev_id))
1574                 return 0;
1575
1576         return -1;
1577 }
1578
1579 static uint16_t
1580 cryptodevs_init(uint16_t req_queue_num)
1581 {
1582         struct rte_cryptodev_config dev_conf;
1583         struct rte_cryptodev_qp_conf qp_conf;
1584         uint16_t idx, max_nb_qps, qp, total_nb_qps, i;
1585         int16_t cdev_id;
1586         struct rte_hash_parameters params = { 0 };
1587
1588         const uint64_t mseg_flag = multi_seg_required() ?
1589                                 RTE_CRYPTODEV_FF_IN_PLACE_SGL : 0;
1590
1591         params.entries = CDEV_MAP_ENTRIES;
1592         params.key_len = sizeof(struct cdev_key);
1593         params.hash_func = rte_jhash;
1594         params.hash_func_init_val = 0;
1595         params.socket_id = rte_socket_id();
1596
1597         params.name = "cdev_map_in";
1598         cdev_map_in = rte_hash_create(&params);
1599         if (cdev_map_in == NULL)
1600                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1601                                 rte_errno);
1602
1603         params.name = "cdev_map_out";
1604         cdev_map_out = rte_hash_create(&params);
1605         if (cdev_map_out == NULL)
1606                 rte_panic("Failed to create cdev_map hash table, errno = %d\n",
1607                                 rte_errno);
1608
1609         printf("lcore/cryptodev/qp mappings:\n");
1610
1611         idx = 0;
1612         total_nb_qps = 0;
1613         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
1614                 struct rte_cryptodev_info cdev_info;
1615
1616                 if (check_cryptodev_mask((uint8_t)cdev_id))
1617                         continue;
1618
1619                 rte_cryptodev_info_get(cdev_id, &cdev_info);
1620
1621                 if ((mseg_flag & cdev_info.feature_flags) != mseg_flag)
1622                         rte_exit(EXIT_FAILURE,
1623                                 "Device %hd does not support \'%s\' feature\n",
1624                                 cdev_id,
1625                                 rte_cryptodev_get_feature_name(mseg_flag));
1626
1627                 if (nb_lcore_params > cdev_info.max_nb_queue_pairs)
1628                         max_nb_qps = cdev_info.max_nb_queue_pairs;
1629                 else
1630                         max_nb_qps = nb_lcore_params;
1631
1632                 qp = 0;
1633                 i = 0;
1634                 while (qp < max_nb_qps && i < nb_lcore_params) {
1635                         if (add_cdev_mapping(&cdev_info, cdev_id, qp,
1636                                                 &lcore_params[idx]))
1637                                 qp++;
1638                         idx++;
1639                         idx = idx % nb_lcore_params;
1640                         i++;
1641                 }
1642
1643                 qp = RTE_MIN(max_nb_qps, RTE_MAX(req_queue_num, qp));
1644                 if (qp == 0)
1645                         continue;
1646
1647                 total_nb_qps += qp;
1648                 dev_conf.socket_id = rte_cryptodev_socket_id(cdev_id);
1649                 dev_conf.nb_queue_pairs = qp;
1650                 dev_conf.ff_disable = RTE_CRYPTODEV_FF_ASYMMETRIC_CRYPTO;
1651
1652                 uint32_t dev_max_sess = cdev_info.sym.max_nb_sessions;
1653                 if (dev_max_sess != 0 &&
1654                                 dev_max_sess < get_nb_crypto_sessions())
1655                         rte_exit(EXIT_FAILURE,
1656                                 "Device does not support at least %u "
1657                                 "sessions", get_nb_crypto_sessions());
1658
1659                 if (rte_cryptodev_configure(cdev_id, &dev_conf))
1660                         rte_panic("Failed to initialize cryptodev %u\n",
1661                                         cdev_id);
1662
1663                 qp_conf.nb_descriptors = CDEV_QUEUE_DESC;
1664                 qp_conf.mp_session =
1665                         socket_ctx[dev_conf.socket_id].session_pool;
1666                 qp_conf.mp_session_private =
1667                         socket_ctx[dev_conf.socket_id].session_priv_pool;
1668                 for (qp = 0; qp < dev_conf.nb_queue_pairs; qp++)
1669                         if (rte_cryptodev_queue_pair_setup(cdev_id, qp,
1670                                         &qp_conf, dev_conf.socket_id))
1671                                 rte_panic("Failed to setup queue %u for "
1672                                                 "cdev_id %u\n", 0, cdev_id);
1673
1674                 if (rte_cryptodev_start(cdev_id))
1675                         rte_panic("Failed to start cryptodev %u\n",
1676                                         cdev_id);
1677         }
1678
1679         printf("\n");
1680
1681         return total_nb_qps;
1682 }
1683
1684 static int
1685 check_ptype(int portid)
1686 {
1687         int l3_ipv4 = 0, l3_ipv6 = 0, l4_udp = 0, tunnel_esp = 0;
1688         int i, nb_ptypes;
1689         uint32_t mask;
1690
1691         mask = (RTE_PTYPE_L3_MASK | RTE_PTYPE_L4_MASK |
1692                       RTE_PTYPE_TUNNEL_MASK);
1693
1694         nb_ptypes = rte_eth_dev_get_supported_ptypes(portid, mask, NULL, 0);
1695         if (nb_ptypes <= 0)
1696                 return 0;
1697
1698         uint32_t ptypes[nb_ptypes];
1699
1700         nb_ptypes = rte_eth_dev_get_supported_ptypes(portid, mask, ptypes, nb_ptypes);
1701         for (i = 0; i < nb_ptypes; ++i) {
1702                 if (RTE_ETH_IS_IPV4_HDR(ptypes[i]))
1703                         l3_ipv4 = 1;
1704                 if (RTE_ETH_IS_IPV6_HDR(ptypes[i]))
1705                         l3_ipv6 = 1;
1706                 if ((ptypes[i] & RTE_PTYPE_TUNNEL_MASK) == RTE_PTYPE_TUNNEL_ESP)
1707                         tunnel_esp = 1;
1708                 if ((ptypes[i] & RTE_PTYPE_L4_MASK) == RTE_PTYPE_L4_UDP)
1709                         l4_udp = 1;
1710         }
1711
1712         if (l3_ipv4 == 0)
1713                 printf("port %d cannot parse RTE_PTYPE_L3_IPV4\n", portid);
1714
1715         if (l3_ipv6 == 0)
1716                 printf("port %d cannot parse RTE_PTYPE_L3_IPV6\n", portid);
1717
1718         if (l4_udp == 0)
1719                 printf("port %d cannot parse RTE_PTYPE_L4_UDP\n", portid);
1720
1721         if (tunnel_esp == 0)
1722                 printf("port %d cannot parse RTE_PTYPE_TUNNEL_ESP\n", portid);
1723
1724         if (l3_ipv4 && l3_ipv6 && l4_udp && tunnel_esp)
1725                 return 1;
1726
1727         return 0;
1728
1729 }
1730
1731 static inline void
1732 parse_ptype(struct rte_mbuf *m)
1733 {
1734         uint32_t packet_type = RTE_PTYPE_UNKNOWN;
1735         const struct rte_ipv4_hdr *iph4;
1736         const struct rte_ipv6_hdr *iph6;
1737         const struct rte_ether_hdr *eth;
1738         const struct rte_udp_hdr *udp;
1739         uint16_t nat_port, ether_type;
1740         int next_proto = 0;
1741         size_t ext_len = 0;
1742         const uint8_t *p;
1743         uint32_t l3len;
1744
1745         eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
1746         ether_type = eth->ether_type;
1747
1748         if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
1749                 iph4 = (const struct rte_ipv4_hdr *)(eth + 1);
1750                 l3len = ((iph4->version_ihl & RTE_IPV4_HDR_IHL_MASK) *
1751                                RTE_IPV4_IHL_MULTIPLIER);
1752
1753                 if (l3len == sizeof(struct rte_ipv4_hdr))
1754                         packet_type |= RTE_PTYPE_L3_IPV4;
1755                 else
1756                         packet_type |= RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
1757
1758                 next_proto = iph4->next_proto_id;
1759                 p = (const uint8_t *)iph4;
1760         } else if (ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
1761                 iph6 = (const struct rte_ipv6_hdr *)(eth + 1);
1762                 l3len = sizeof(struct ip6_hdr);
1763
1764                 /* determine l3 header size up to ESP extension */
1765                 next_proto = iph6->proto;
1766                 p = (const uint8_t *)iph6;
1767                 while (next_proto != IPPROTO_ESP && l3len < m->data_len &&
1768                         (next_proto = rte_ipv6_get_next_ext(p + l3len,
1769                                                 next_proto, &ext_len)) >= 0)
1770                         l3len += ext_len;
1771
1772                 /* Skip IPv6 header exceeds first segment length */
1773                 if (unlikely(l3len + RTE_ETHER_HDR_LEN > m->data_len))
1774                         goto exit;
1775
1776                 if (l3len == sizeof(struct ip6_hdr))
1777                         packet_type |= RTE_PTYPE_L3_IPV6;
1778                 else
1779                         packet_type |= RTE_PTYPE_L3_IPV6_EXT;
1780         }
1781
1782         switch (next_proto) {
1783         case IPPROTO_ESP:
1784                 packet_type |= RTE_PTYPE_TUNNEL_ESP;
1785                 break;
1786         case IPPROTO_UDP:
1787                 if (app_sa_prm.udp_encap == 1) {
1788                         udp = (const struct rte_udp_hdr *)(p + l3len);
1789                         nat_port = rte_cpu_to_be_16(IPSEC_NAT_T_PORT);
1790                         if (udp->src_port == nat_port ||
1791                             udp->dst_port == nat_port)
1792                                 packet_type |=
1793                                         MBUF_PTYPE_TUNNEL_ESP_IN_UDP;
1794                 }
1795                 break;
1796         default:
1797                 break;
1798         }
1799 exit:
1800         m->packet_type = packet_type;
1801 }
1802
1803 static uint16_t
1804 parse_ptype_cb(uint16_t port __rte_unused, uint16_t queue __rte_unused,
1805                struct rte_mbuf *pkts[], uint16_t nb_pkts,
1806                uint16_t max_pkts __rte_unused,
1807                void *user_param __rte_unused)
1808 {
1809         uint32_t i;
1810
1811         if (unlikely(nb_pkts == 0))
1812                 return nb_pkts;
1813
1814         rte_prefetch0(rte_pktmbuf_mtod(pkts[0], struct ether_hdr *));
1815         for (i = 0; i < (unsigned int) (nb_pkts - 1); ++i) {
1816                 rte_prefetch0(rte_pktmbuf_mtod(pkts[i+1],
1817                         struct ether_hdr *));
1818                 parse_ptype(pkts[i]);
1819         }
1820         parse_ptype(pkts[i]);
1821
1822         return nb_pkts;
1823 }
1824
1825 static void
1826 port_init(uint16_t portid, uint64_t req_rx_offloads, uint64_t req_tx_offloads)
1827 {
1828         struct rte_eth_dev_info dev_info;
1829         struct rte_eth_txconf *txconf;
1830         uint16_t nb_tx_queue, nb_rx_queue;
1831         uint16_t tx_queueid, rx_queueid, queue, lcore_id;
1832         int32_t ret, socket_id;
1833         struct lcore_conf *qconf;
1834         struct rte_ether_addr ethaddr;
1835         struct rte_eth_conf local_port_conf = port_conf;
1836         int ptype_supported;
1837
1838         ret = rte_eth_dev_info_get(portid, &dev_info);
1839         if (ret != 0)
1840                 rte_exit(EXIT_FAILURE,
1841                         "Error during getting device (port %u) info: %s\n",
1842                         portid, strerror(-ret));
1843
1844         /* limit allowed HW offloads, as user requested */
1845         dev_info.rx_offload_capa &= dev_rx_offload;
1846         dev_info.tx_offload_capa &= dev_tx_offload;
1847
1848         printf("Configuring device port %u:\n", portid);
1849
1850         ret = rte_eth_macaddr_get(portid, &ethaddr);
1851         if (ret != 0)
1852                 rte_exit(EXIT_FAILURE,
1853                         "Error getting MAC address (port %u): %s\n",
1854                         portid, rte_strerror(-ret));
1855
1856         ethaddr_tbl[portid].src = ETHADDR_TO_UINT64(&ethaddr);
1857         print_ethaddr("Address: ", &ethaddr);
1858         printf("\n");
1859
1860         nb_rx_queue = get_port_nb_rx_queues(portid);
1861         nb_tx_queue = nb_lcores;
1862
1863         if (nb_rx_queue > dev_info.max_rx_queues)
1864                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1865                                 "(max rx queue is %u)\n",
1866                                 nb_rx_queue, dev_info.max_rx_queues);
1867
1868         if (nb_tx_queue > dev_info.max_tx_queues)
1869                 rte_exit(EXIT_FAILURE, "Error: queue %u not available "
1870                                 "(max tx queue is %u)\n",
1871                                 nb_tx_queue, dev_info.max_tx_queues);
1872
1873         printf("Creating queues: nb_rx_queue=%d nb_tx_queue=%u...\n",
1874                         nb_rx_queue, nb_tx_queue);
1875
1876         local_port_conf.rxmode.mtu = mtu_size;
1877
1878         if (multi_seg_required()) {
1879                 local_port_conf.rxmode.offloads |= RTE_ETH_RX_OFFLOAD_SCATTER;
1880                 local_port_conf.txmode.offloads |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
1881         }
1882
1883         local_port_conf.rxmode.offloads |= req_rx_offloads;
1884         local_port_conf.txmode.offloads |= req_tx_offloads;
1885
1886         /* Check that all required capabilities are supported */
1887         if ((local_port_conf.rxmode.offloads & dev_info.rx_offload_capa) !=
1888                         local_port_conf.rxmode.offloads)
1889                 rte_exit(EXIT_FAILURE,
1890                         "Error: port %u required RX offloads: 0x%" PRIx64
1891                         ", available RX offloads: 0x%" PRIx64 "\n",
1892                         portid, local_port_conf.rxmode.offloads,
1893                         dev_info.rx_offload_capa);
1894
1895         if ((local_port_conf.txmode.offloads & dev_info.tx_offload_capa) !=
1896                         local_port_conf.txmode.offloads)
1897                 rte_exit(EXIT_FAILURE,
1898                         "Error: port %u required TX offloads: 0x%" PRIx64
1899                         ", available TX offloads: 0x%" PRIx64 "\n",
1900                         portid, local_port_conf.txmode.offloads,
1901                         dev_info.tx_offload_capa);
1902
1903         if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
1904                 local_port_conf.txmode.offloads |=
1905                         RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
1906
1907         printf("port %u configuring rx_offloads=0x%" PRIx64
1908                 ", tx_offloads=0x%" PRIx64 "\n",
1909                 portid, local_port_conf.rxmode.offloads,
1910                 local_port_conf.txmode.offloads);
1911
1912         local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1913                 dev_info.flow_type_rss_offloads;
1914         if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1915                         port_conf.rx_adv_conf.rss_conf.rss_hf) {
1916                 printf("Port %u modified RSS hash function based on hardware support,"
1917                         "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1918                         portid,
1919                         port_conf.rx_adv_conf.rss_conf.rss_hf,
1920                         local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1921         }
1922
1923         ret = rte_eth_dev_configure(portid, nb_rx_queue, nb_tx_queue,
1924                         &local_port_conf);
1925         if (ret < 0)
1926                 rte_exit(EXIT_FAILURE, "Cannot configure device: "
1927                                 "err=%d, port=%d\n", ret, portid);
1928
1929         ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd);
1930         if (ret < 0)
1931                 rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: "
1932                                 "err=%d, port=%d\n", ret, portid);
1933
1934         /* Check if required ptypes are supported */
1935         ptype_supported = check_ptype(portid);
1936         if (!ptype_supported)
1937                 printf("Port %d: softly parse packet type info\n", portid);
1938
1939         /* init one TX queue per lcore */
1940         tx_queueid = 0;
1941         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1942                 if (rte_lcore_is_enabled(lcore_id) == 0)
1943                         continue;
1944
1945                 if (numa_on)
1946                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1947                 else
1948                         socket_id = 0;
1949
1950                 /* init TX queue */
1951                 printf("Setup txq=%u,%d,%d\n", lcore_id, tx_queueid, socket_id);
1952
1953                 txconf = &dev_info.default_txconf;
1954                 txconf->offloads = local_port_conf.txmode.offloads;
1955
1956                 ret = rte_eth_tx_queue_setup(portid, tx_queueid, nb_txd,
1957                                 socket_id, txconf);
1958                 if (ret < 0)
1959                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: "
1960                                         "err=%d, port=%d\n", ret, portid);
1961
1962                 qconf = &lcore_conf[lcore_id];
1963                 qconf->tx_queue_id[portid] = tx_queueid;
1964
1965                 /* Pre-populate pkt offloads based on capabilities */
1966                 qconf->outbound.ipv4_offloads = RTE_MBUF_F_TX_IPV4;
1967                 qconf->outbound.ipv6_offloads = RTE_MBUF_F_TX_IPV6;
1968                 if (local_port_conf.txmode.offloads & RTE_ETH_TX_OFFLOAD_IPV4_CKSUM)
1969                         qconf->outbound.ipv4_offloads |= RTE_MBUF_F_TX_IP_CKSUM;
1970
1971                 tx_queueid++;
1972
1973                 /* init RX queues */
1974                 for (queue = 0; queue < qconf->nb_rx_queue; ++queue) {
1975                         struct rte_eth_rxconf rxq_conf;
1976                         struct rte_mempool *pool;
1977
1978                         if (portid != qconf->rx_queue_list[queue].port_id)
1979                                 continue;
1980
1981                         rx_queueid = qconf->rx_queue_list[queue].queue_id;
1982
1983                         printf("Setup rxq=%d,%d,%d\n", portid, rx_queueid,
1984                                         socket_id);
1985
1986                         rxq_conf = dev_info.default_rxconf;
1987                         rxq_conf.offloads = local_port_conf.rxmode.offloads;
1988
1989                         if (per_port_pool)
1990                                 pool = socket_ctx[socket_id].mbuf_pool[portid];
1991                         else
1992                                 pool = socket_ctx[socket_id].mbuf_pool[0];
1993
1994                         ret = rte_eth_rx_queue_setup(portid, rx_queueid,
1995                                         nb_rxd, socket_id, &rxq_conf, pool);
1996                         if (ret < 0)
1997                                 rte_exit(EXIT_FAILURE,
1998                                         "rte_eth_rx_queue_setup: err=%d, "
1999                                         "port=%d\n", ret, portid);
2000
2001                         /* Register Rx callback if ptypes are not supported */
2002                         if (!ptype_supported &&
2003                             !rte_eth_add_rx_callback(portid, queue,
2004                                                      parse_ptype_cb, NULL)) {
2005                                 printf("Failed to add rx callback: port=%d, "
2006                                        "queue=%d\n", portid, queue);
2007                         }
2008
2009
2010                 }
2011         }
2012         printf("\n");
2013 }
2014
2015 static size_t
2016 max_session_size(void)
2017 {
2018         size_t max_sz, sz;
2019         void *sec_ctx;
2020         int16_t cdev_id, port_id, n;
2021
2022         max_sz = 0;
2023         n =  rte_cryptodev_count();
2024         for (cdev_id = 0; cdev_id != n; cdev_id++) {
2025                 sz = rte_cryptodev_sym_get_private_session_size(cdev_id);
2026                 if (sz > max_sz)
2027                         max_sz = sz;
2028                 /*
2029                  * If crypto device is security capable, need to check the
2030                  * size of security session as well.
2031                  */
2032
2033                 /* Get security context of the crypto device */
2034                 sec_ctx = rte_cryptodev_get_sec_ctx(cdev_id);
2035                 if (sec_ctx == NULL)
2036                         continue;
2037
2038                 /* Get size of security session */
2039                 sz = rte_security_session_get_size(sec_ctx);
2040                 if (sz > max_sz)
2041                         max_sz = sz;
2042         }
2043
2044         RTE_ETH_FOREACH_DEV(port_id) {
2045                 if ((enabled_port_mask & (1 << port_id)) == 0)
2046                         continue;
2047
2048                 sec_ctx = rte_eth_dev_get_sec_ctx(port_id);
2049                 if (sec_ctx == NULL)
2050                         continue;
2051
2052                 sz = rte_security_session_get_size(sec_ctx);
2053                 if (sz > max_sz)
2054                         max_sz = sz;
2055         }
2056
2057         return max_sz;
2058 }
2059
2060 static void
2061 session_pool_init(struct socket_ctx *ctx, int32_t socket_id, size_t sess_sz)
2062 {
2063         char mp_name[RTE_MEMPOOL_NAMESIZE];
2064         struct rte_mempool *sess_mp;
2065         uint32_t nb_sess;
2066
2067         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2068                         "sess_mp_%u", socket_id);
2069         nb_sess = (get_nb_crypto_sessions() + CDEV_MP_CACHE_SZ *
2070                 rte_lcore_count());
2071         nb_sess = RTE_MAX(nb_sess, CDEV_MP_CACHE_SZ *
2072                         CDEV_MP_CACHE_MULTIPLIER);
2073         sess_mp = rte_cryptodev_sym_session_pool_create(
2074                         mp_name, nb_sess, sess_sz, CDEV_MP_CACHE_SZ, 0,
2075                         socket_id);
2076         ctx->session_pool = sess_mp;
2077
2078         if (ctx->session_pool == NULL)
2079                 rte_exit(EXIT_FAILURE,
2080                         "Cannot init session pool on socket %d\n", socket_id);
2081         else
2082                 printf("Allocated session pool on socket %d\n", socket_id);
2083 }
2084
2085 static void
2086 session_priv_pool_init(struct socket_ctx *ctx, int32_t socket_id,
2087         size_t sess_sz)
2088 {
2089         char mp_name[RTE_MEMPOOL_NAMESIZE];
2090         struct rte_mempool *sess_mp;
2091         uint32_t nb_sess;
2092
2093         snprintf(mp_name, RTE_MEMPOOL_NAMESIZE,
2094                         "sess_mp_priv_%u", socket_id);
2095         nb_sess = (get_nb_crypto_sessions() + CDEV_MP_CACHE_SZ *
2096                 rte_lcore_count());
2097         nb_sess = RTE_MAX(nb_sess, CDEV_MP_CACHE_SZ *
2098                         CDEV_MP_CACHE_MULTIPLIER);
2099         sess_mp = rte_mempool_create(mp_name,
2100                         nb_sess,
2101                         sess_sz,
2102                         CDEV_MP_CACHE_SZ,
2103                         0, NULL, NULL, NULL,
2104                         NULL, socket_id,
2105                         0);
2106         ctx->session_priv_pool = sess_mp;
2107
2108         if (ctx->session_priv_pool == NULL)
2109                 rte_exit(EXIT_FAILURE,
2110                         "Cannot init session priv pool on socket %d\n",
2111                         socket_id);
2112         else
2113                 printf("Allocated session priv pool on socket %d\n",
2114                         socket_id);
2115 }
2116
2117 static void
2118 pool_init(struct socket_ctx *ctx, int32_t socket_id, int portid,
2119           uint32_t nb_mbuf)
2120 {
2121         char s[64];
2122         int32_t ms;
2123
2124
2125         /* mbuf_pool is initialised by the pool_init() function*/
2126         if (socket_ctx[socket_id].mbuf_pool[portid])
2127                 return;
2128
2129         snprintf(s, sizeof(s), "mbuf_pool_%d_%d", socket_id, portid);
2130         ctx->mbuf_pool[portid] = rte_pktmbuf_pool_create(s, nb_mbuf,
2131                                                          MEMPOOL_CACHE_SIZE,
2132                                                          ipsec_metadata_size(),
2133                                                          frame_buf_size,
2134                                                          socket_id);
2135
2136         /*
2137          * if multi-segment support is enabled, then create a pool
2138          * for indirect mbufs. This is not per-port but global.
2139          */
2140         ms = multi_seg_required();
2141         if (ms != 0 && !ctx->mbuf_pool_indir) {
2142                 snprintf(s, sizeof(s), "mbuf_pool_indir_%d", socket_id);
2143                 ctx->mbuf_pool_indir = rte_pktmbuf_pool_create(s, nb_mbuf,
2144                         MEMPOOL_CACHE_SIZE, 0, 0, socket_id);
2145         }
2146
2147         if (ctx->mbuf_pool[portid] == NULL ||
2148             (ms != 0 && ctx->mbuf_pool_indir == NULL))
2149                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n",
2150                                 socket_id);
2151         else
2152                 printf("Allocated mbuf pool on socket %d\n", socket_id);
2153 }
2154
2155 static inline int
2156 inline_ipsec_event_esn_overflow(struct rte_security_ctx *ctx, uint64_t md)
2157 {
2158         struct ipsec_sa *sa;
2159
2160         /* For inline protocol processing, the metadata in the event will
2161          * uniquely identify the security session which raised the event.
2162          * Application would then need the userdata it had registered with the
2163          * security session to process the event.
2164          */
2165
2166         sa = (struct ipsec_sa *)rte_security_get_userdata(ctx, md);
2167
2168         if (sa == NULL) {
2169                 /* userdata could not be retrieved */
2170                 return -1;
2171         }
2172
2173         /* Sequence number over flow. SA need to be re-established */
2174         RTE_SET_USED(sa);
2175         return 0;
2176 }
2177
2178 static int
2179 inline_ipsec_event_callback(uint16_t port_id, enum rte_eth_event_type type,
2180                  void *param, void *ret_param)
2181 {
2182         uint64_t md;
2183         struct rte_eth_event_ipsec_desc *event_desc = NULL;
2184         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
2185                                         rte_eth_dev_get_sec_ctx(port_id);
2186
2187         RTE_SET_USED(param);
2188
2189         if (type != RTE_ETH_EVENT_IPSEC)
2190                 return -1;
2191
2192         event_desc = ret_param;
2193         if (event_desc == NULL) {
2194                 printf("Event descriptor not set\n");
2195                 return -1;
2196         }
2197
2198         md = event_desc->metadata;
2199
2200         if (event_desc->subtype == RTE_ETH_EVENT_IPSEC_ESN_OVERFLOW)
2201                 return inline_ipsec_event_esn_overflow(ctx, md);
2202         else if (event_desc->subtype >= RTE_ETH_EVENT_IPSEC_MAX) {
2203                 printf("Invalid IPsec event reported\n");
2204                 return -1;
2205         }
2206
2207         return -1;
2208 }
2209
2210 static int
2211 ethdev_reset_event_callback(uint16_t port_id,
2212                 enum rte_eth_event_type type,
2213                  void *param __rte_unused, void *ret_param __rte_unused)
2214 {
2215         printf("Reset Event on port id %d type %d\n", port_id, type);
2216         printf("Force quit application");
2217         force_quit = true;
2218         return 0;
2219 }
2220
2221 static uint16_t
2222 rx_callback(__rte_unused uint16_t port, __rte_unused uint16_t queue,
2223         struct rte_mbuf *pkt[], uint16_t nb_pkts,
2224         __rte_unused uint16_t max_pkts, void *user_param)
2225 {
2226         uint64_t tm;
2227         uint32_t i, k;
2228         struct lcore_conf *lc;
2229         struct rte_mbuf *mb;
2230         struct rte_ether_hdr *eth;
2231
2232         lc = user_param;
2233         k = 0;
2234         tm = 0;
2235
2236         for (i = 0; i != nb_pkts; i++) {
2237
2238                 mb = pkt[i];
2239                 eth = rte_pktmbuf_mtod(mb, struct rte_ether_hdr *);
2240                 if (eth->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)) {
2241
2242                         struct rte_ipv4_hdr *iph;
2243
2244                         iph = (struct rte_ipv4_hdr *)(eth + 1);
2245                         if (rte_ipv4_frag_pkt_is_fragmented(iph)) {
2246
2247                                 mb->l2_len = sizeof(*eth);
2248                                 mb->l3_len = sizeof(*iph);
2249                                 tm = (tm != 0) ? tm : rte_rdtsc();
2250                                 mb = rte_ipv4_frag_reassemble_packet(
2251                                         lc->frag.tbl, &lc->frag.dr,
2252                                         mb, tm, iph);
2253
2254                                 if (mb != NULL) {
2255                                         /* fix ip cksum after reassemble. */
2256                                         iph = rte_pktmbuf_mtod_offset(mb,
2257                                                 struct rte_ipv4_hdr *,
2258                                                 mb->l2_len);
2259                                         iph->hdr_checksum = 0;
2260                                         iph->hdr_checksum = rte_ipv4_cksum(iph);
2261                                 }
2262                         }
2263                 } else if (eth->ether_type ==
2264                                 rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV6)) {
2265
2266                         struct rte_ipv6_hdr *iph;
2267                         struct rte_ipv6_fragment_ext *fh;
2268
2269                         iph = (struct rte_ipv6_hdr *)(eth + 1);
2270                         fh = rte_ipv6_frag_get_ipv6_fragment_header(iph);
2271                         if (fh != NULL) {
2272                                 mb->l2_len = sizeof(*eth);
2273                                 mb->l3_len = (uintptr_t)fh - (uintptr_t)iph +
2274                                         sizeof(*fh);
2275                                 tm = (tm != 0) ? tm : rte_rdtsc();
2276                                 mb = rte_ipv6_frag_reassemble_packet(
2277                                         lc->frag.tbl, &lc->frag.dr,
2278                                         mb, tm, iph, fh);
2279                                 if (mb != NULL)
2280                                         /* fix l3_len after reassemble. */
2281                                         mb->l3_len = mb->l3_len - sizeof(*fh);
2282                         }
2283                 }
2284
2285                 pkt[k] = mb;
2286                 k += (mb != NULL);
2287         }
2288
2289         /* some fragments were encountered, drain death row */
2290         if (tm != 0)
2291                 rte_ip_frag_free_death_row(&lc->frag.dr, 0);
2292
2293         return k;
2294 }
2295
2296
2297 static int
2298 reassemble_lcore_init(struct lcore_conf *lc, uint32_t cid)
2299 {
2300         int32_t sid;
2301         uint32_t i;
2302         uint64_t frag_cycles;
2303         const struct lcore_rx_queue *rxq;
2304         const struct rte_eth_rxtx_callback *cb;
2305
2306         /* create fragment table */
2307         sid = rte_lcore_to_socket_id(cid);
2308         frag_cycles = (rte_get_tsc_hz() + NS_PER_S - 1) /
2309                 NS_PER_S * frag_ttl_ns;
2310
2311         lc->frag.tbl = rte_ip_frag_table_create(frag_tbl_sz,
2312                 FRAG_TBL_BUCKET_ENTRIES, frag_tbl_sz, frag_cycles, sid);
2313         if (lc->frag.tbl == NULL) {
2314                 printf("%s(%u): failed to create fragment table of size: %u, "
2315                         "error code: %d\n",
2316                         __func__, cid, frag_tbl_sz, rte_errno);
2317                 return -ENOMEM;
2318         }
2319
2320         /* setup reassemble RX callbacks for all queues */
2321         for (i = 0; i != lc->nb_rx_queue; i++) {
2322
2323                 rxq = lc->rx_queue_list + i;
2324                 cb = rte_eth_add_rx_callback(rxq->port_id, rxq->queue_id,
2325                         rx_callback, lc);
2326                 if (cb == NULL) {
2327                         printf("%s(%u): failed to install RX callback for "
2328                                 "portid=%u, queueid=%u, error code: %d\n",
2329                                 __func__, cid,
2330                                 rxq->port_id, rxq->queue_id, rte_errno);
2331                         return -ENOMEM;
2332                 }
2333         }
2334
2335         return 0;
2336 }
2337
2338 static int
2339 reassemble_init(void)
2340 {
2341         int32_t rc;
2342         uint32_t i, lc;
2343
2344         rc = 0;
2345         for (i = 0; i != nb_lcore_params; i++) {
2346                 lc = lcore_params[i].lcore_id;
2347                 rc = reassemble_lcore_init(lcore_conf + lc, lc);
2348                 if (rc != 0)
2349                         break;
2350         }
2351
2352         return rc;
2353 }
2354
2355 static void
2356 create_default_ipsec_flow(uint16_t port_id, uint64_t rx_offloads)
2357 {
2358         struct rte_flow_action action[2];
2359         struct rte_flow_item pattern[2];
2360         struct rte_flow_attr attr = {0};
2361         struct rte_flow_error err;
2362         struct rte_flow *flow;
2363         int ret;
2364
2365         if (!(rx_offloads & RTE_ETH_RX_OFFLOAD_SECURITY))
2366                 return;
2367
2368         /* Add the default rte_flow to enable SECURITY for all ESP packets */
2369
2370         pattern[0].type = RTE_FLOW_ITEM_TYPE_ESP;
2371         pattern[0].spec = NULL;
2372         pattern[0].mask = NULL;
2373         pattern[0].last = NULL;
2374         pattern[1].type = RTE_FLOW_ITEM_TYPE_END;
2375
2376         action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
2377         action[0].conf = NULL;
2378         action[1].type = RTE_FLOW_ACTION_TYPE_END;
2379         action[1].conf = NULL;
2380
2381         attr.ingress = 1;
2382
2383         ret = rte_flow_validate(port_id, &attr, pattern, action, &err);
2384         if (ret)
2385                 return;
2386
2387         flow = rte_flow_create(port_id, &attr, pattern, action, &err);
2388         if (flow == NULL)
2389                 return;
2390
2391         flow_info_tbl[port_id].rx_def_flow = flow;
2392         RTE_LOG(INFO, IPSEC,
2393                 "Created default flow enabling SECURITY for all ESP traffic on port %d\n",
2394                 port_id);
2395 }
2396
2397 static void
2398 signal_handler(int signum)
2399 {
2400         if (signum == SIGINT || signum == SIGTERM) {
2401                 printf("\n\nSignal %d received, preparing to exit...\n",
2402                                 signum);
2403                 force_quit = true;
2404         }
2405 }
2406
2407 static void
2408 ev_mode_sess_verify(struct ipsec_sa *sa, int nb_sa)
2409 {
2410         struct rte_ipsec_session *ips;
2411         int32_t i;
2412
2413         if (!sa || !nb_sa)
2414                 return;
2415
2416         for (i = 0; i < nb_sa; i++) {
2417                 ips = ipsec_get_primary_session(&sa[i]);
2418                 if (ips->type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL)
2419                         rte_exit(EXIT_FAILURE, "Event mode supports only "
2420                                  "inline protocol sessions\n");
2421         }
2422
2423 }
2424
2425 static int32_t
2426 check_event_mode_params(struct eh_conf *eh_conf)
2427 {
2428         struct eventmode_conf *em_conf = NULL;
2429         struct lcore_params *params;
2430         uint16_t portid;
2431
2432         if (!eh_conf || !eh_conf->mode_params)
2433                 return -EINVAL;
2434
2435         /* Get eventmode conf */
2436         em_conf = eh_conf->mode_params;
2437
2438         if (eh_conf->mode == EH_PKT_TRANSFER_MODE_POLL &&
2439             em_conf->ext_params.sched_type != SCHED_TYPE_NOT_SET) {
2440                 printf("error: option --event-schedule-type applies only to "
2441                        "event mode\n");
2442                 return -EINVAL;
2443         }
2444
2445         if (eh_conf->mode != EH_PKT_TRANSFER_MODE_EVENT)
2446                 return 0;
2447
2448         /* Set schedule type to ORDERED if it wasn't explicitly set by user */
2449         if (em_conf->ext_params.sched_type == SCHED_TYPE_NOT_SET)
2450                 em_conf->ext_params.sched_type = RTE_SCHED_TYPE_ORDERED;
2451
2452         /*
2453          * Event mode currently supports only inline protocol sessions.
2454          * If there are other types of sessions configured then exit with
2455          * error.
2456          */
2457         ev_mode_sess_verify(sa_in, nb_sa_in);
2458         ev_mode_sess_verify(sa_out, nb_sa_out);
2459
2460
2461         /* Option --config does not apply to event mode */
2462         if (nb_lcore_params > 0) {
2463                 printf("error: option --config applies only to poll mode\n");
2464                 return -EINVAL;
2465         }
2466
2467         /*
2468          * In order to use the same port_init routine for both poll and event
2469          * modes initialize lcore_params with one queue for each eth port
2470          */
2471         lcore_params = lcore_params_array;
2472         RTE_ETH_FOREACH_DEV(portid) {
2473                 if ((enabled_port_mask & (1 << portid)) == 0)
2474                         continue;
2475
2476                 params = &lcore_params[nb_lcore_params++];
2477                 params->port_id = portid;
2478                 params->queue_id = 0;
2479                 params->lcore_id = rte_get_next_lcore(0, 0, 1);
2480         }
2481
2482         return 0;
2483 }
2484
2485 static void
2486 inline_sessions_free(struct sa_ctx *sa_ctx)
2487 {
2488         struct rte_ipsec_session *ips;
2489         struct ipsec_sa *sa;
2490         int32_t ret;
2491         uint32_t i;
2492
2493         if (!sa_ctx)
2494                 return;
2495
2496         for (i = 0; i < sa_ctx->nb_sa; i++) {
2497
2498                 sa = &sa_ctx->sa[i];
2499                 if (!sa->spi)
2500                         continue;
2501
2502                 ips = ipsec_get_primary_session(sa);
2503                 if (ips->type != RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL &&
2504                     ips->type != RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO)
2505                         continue;
2506
2507                 if (!rte_eth_dev_is_valid_port(sa->portid))
2508                         continue;
2509
2510                 ret = rte_security_session_destroy(
2511                                 rte_eth_dev_get_sec_ctx(sa->portid),
2512                                 ips->security.ses);
2513                 if (ret)
2514                         RTE_LOG(ERR, IPSEC, "Failed to destroy security "
2515                                             "session type %d, spi %d\n",
2516                                             ips->type, sa->spi);
2517         }
2518 }
2519
2520 static uint32_t
2521 calculate_nb_mbufs(uint16_t nb_ports, uint16_t nb_crypto_qp, uint32_t nb_rxq,
2522                 uint32_t nb_txq)
2523 {
2524         return RTE_MAX((nb_rxq * nb_rxd +
2525                         nb_ports * nb_lcores * MAX_PKT_BURST +
2526                         nb_ports * nb_txq * nb_txd +
2527                         nb_lcores * MEMPOOL_CACHE_SIZE +
2528                         nb_crypto_qp * CDEV_QUEUE_DESC +
2529                         nb_lcores * frag_tbl_sz *
2530                         FRAG_TBL_BUCKET_ENTRIES),
2531                        8192U);
2532 }
2533
2534
2535 static int
2536 handle_telemetry_cmd_ipsec_secgw_stats(const char *cmd __rte_unused,
2537                 const char *params, struct rte_tel_data *data)
2538 {
2539         uint64_t total_pkts_dropped = 0, total_pkts_tx = 0, total_pkts_rx = 0;
2540         unsigned int coreid;
2541
2542         rte_tel_data_start_dict(data);
2543
2544         if (params) {
2545                 coreid = (uint32_t)atoi(params);
2546                 if (rte_lcore_is_enabled(coreid) == 0)
2547                         return -EINVAL;
2548
2549                 total_pkts_dropped = core_statistics[coreid].dropped;
2550                 total_pkts_tx = core_statistics[coreid].tx;
2551                 total_pkts_rx = core_statistics[coreid].rx;
2552
2553         } else {
2554                 for (coreid = 0; coreid < RTE_MAX_LCORE; coreid++) {
2555
2556                         /* skip disabled cores */
2557                         if (rte_lcore_is_enabled(coreid) == 0)
2558                                 continue;
2559
2560                         total_pkts_dropped += core_statistics[coreid].dropped;
2561                         total_pkts_tx += core_statistics[coreid].tx;
2562                         total_pkts_rx += core_statistics[coreid].rx;
2563                 }
2564         }
2565
2566         /* add telemetry key/values pairs */
2567         rte_tel_data_add_dict_u64(data, "packets received",
2568                                 total_pkts_rx);
2569
2570         rte_tel_data_add_dict_u64(data, "packets transmitted",
2571                                 total_pkts_tx);
2572
2573         rte_tel_data_add_dict_u64(data, "packets dropped",
2574                                 total_pkts_dropped);
2575
2576
2577         return 0;
2578 }
2579
2580 static void
2581 update_lcore_statistics(struct ipsec_core_statistics *total, uint32_t coreid)
2582 {
2583         struct ipsec_core_statistics *lcore_stats;
2584
2585         /* skip disabled cores */
2586         if (rte_lcore_is_enabled(coreid) == 0)
2587                 return;
2588
2589         lcore_stats = &core_statistics[coreid];
2590
2591         total->rx = lcore_stats->rx;
2592         total->dropped = lcore_stats->dropped;
2593         total->tx = lcore_stats->tx;
2594
2595         /* outbound stats */
2596         total->outbound.spd6.protect += lcore_stats->outbound.spd6.protect;
2597         total->outbound.spd6.bypass += lcore_stats->outbound.spd6.bypass;
2598         total->outbound.spd6.discard += lcore_stats->outbound.spd6.discard;
2599
2600         total->outbound.spd4.protect += lcore_stats->outbound.spd4.protect;
2601         total->outbound.spd4.bypass += lcore_stats->outbound.spd4.bypass;
2602         total->outbound.spd4.discard += lcore_stats->outbound.spd4.discard;
2603
2604         total->outbound.sad.miss += lcore_stats->outbound.sad.miss;
2605
2606         /* inbound stats */
2607         total->inbound.spd6.protect += lcore_stats->inbound.spd6.protect;
2608         total->inbound.spd6.bypass += lcore_stats->inbound.spd6.bypass;
2609         total->inbound.spd6.discard += lcore_stats->inbound.spd6.discard;
2610
2611         total->inbound.spd4.protect += lcore_stats->inbound.spd4.protect;
2612         total->inbound.spd4.bypass += lcore_stats->inbound.spd4.bypass;
2613         total->inbound.spd4.discard += lcore_stats->inbound.spd4.discard;
2614
2615         total->inbound.sad.miss += lcore_stats->inbound.sad.miss;
2616
2617
2618         /* routing stats */
2619         total->lpm4.miss += lcore_stats->lpm4.miss;
2620         total->lpm6.miss += lcore_stats->lpm6.miss;
2621 }
2622
2623 static void
2624 update_statistics(struct ipsec_core_statistics *total, uint32_t coreid)
2625 {
2626         memset(total, 0, sizeof(*total));
2627
2628         if (coreid != UINT32_MAX) {
2629                 update_lcore_statistics(total, coreid);
2630         } else {
2631                 for (coreid = 0; coreid < RTE_MAX_LCORE; coreid++)
2632                         update_lcore_statistics(total, coreid);
2633         }
2634 }
2635
2636 static int
2637 handle_telemetry_cmd_ipsec_secgw_stats_outbound(const char *cmd __rte_unused,
2638                 const char *params, struct rte_tel_data *data)
2639 {
2640         struct ipsec_core_statistics total_stats;
2641
2642         struct rte_tel_data *spd4_data = rte_tel_data_alloc();
2643         struct rte_tel_data *spd6_data = rte_tel_data_alloc();
2644         struct rte_tel_data *sad_data = rte_tel_data_alloc();
2645         unsigned int coreid = UINT32_MAX;
2646         int rc = 0;
2647
2648         /* verify allocated telemetry data structures */
2649         if (!spd4_data || !spd6_data || !sad_data) {
2650                 rc = -ENOMEM;
2651                 goto exit;
2652         }
2653
2654         /* initialize telemetry data structs as dicts */
2655         rte_tel_data_start_dict(data);
2656
2657         rte_tel_data_start_dict(spd4_data);
2658         rte_tel_data_start_dict(spd6_data);
2659         rte_tel_data_start_dict(sad_data);
2660
2661         if (params) {
2662                 coreid = (uint32_t)atoi(params);
2663                 if (rte_lcore_is_enabled(coreid) == 0) {
2664                         rc = -EINVAL;
2665                         goto exit;
2666                 }
2667         }
2668
2669         update_statistics(&total_stats, coreid);
2670
2671         /* add spd 4 telemetry key/values pairs */
2672
2673         rte_tel_data_add_dict_u64(spd4_data, "protect",
2674                 total_stats.outbound.spd4.protect);
2675         rte_tel_data_add_dict_u64(spd4_data, "bypass",
2676                 total_stats.outbound.spd4.bypass);
2677         rte_tel_data_add_dict_u64(spd4_data, "discard",
2678                 total_stats.outbound.spd4.discard);
2679
2680         rte_tel_data_add_dict_container(data, "spd4", spd4_data, 0);
2681
2682         /* add spd 6 telemetry key/values pairs */
2683
2684         rte_tel_data_add_dict_u64(spd6_data, "protect",
2685                 total_stats.outbound.spd6.protect);
2686         rte_tel_data_add_dict_u64(spd6_data, "bypass",
2687                 total_stats.outbound.spd6.bypass);
2688         rte_tel_data_add_dict_u64(spd6_data, "discard",
2689                 total_stats.outbound.spd6.discard);
2690
2691         rte_tel_data_add_dict_container(data, "spd6", spd6_data, 0);
2692
2693         /* add sad telemetry key/values pairs */
2694
2695         rte_tel_data_add_dict_u64(sad_data, "miss",
2696                 total_stats.outbound.sad.miss);
2697
2698         rte_tel_data_add_dict_container(data, "sad", sad_data, 0);
2699
2700 exit:
2701         if (rc) {
2702                 rte_tel_data_free(spd4_data);
2703                 rte_tel_data_free(spd6_data);
2704                 rte_tel_data_free(sad_data);
2705         }
2706         return rc;
2707 }
2708
2709 static int
2710 handle_telemetry_cmd_ipsec_secgw_stats_inbound(const char *cmd __rte_unused,
2711                 const char *params, struct rte_tel_data *data)
2712 {
2713         struct ipsec_core_statistics total_stats;
2714
2715         struct rte_tel_data *spd4_data = rte_tel_data_alloc();
2716         struct rte_tel_data *spd6_data = rte_tel_data_alloc();
2717         struct rte_tel_data *sad_data = rte_tel_data_alloc();
2718         unsigned int coreid = UINT32_MAX;
2719         int rc = 0;
2720
2721         /* verify allocated telemetry data structures */
2722         if (!spd4_data || !spd6_data || !sad_data) {
2723                 rc = -ENOMEM;
2724                 goto exit;
2725         }
2726
2727         /* initialize telemetry data structs as dicts */
2728         rte_tel_data_start_dict(data);
2729         rte_tel_data_start_dict(spd4_data);
2730         rte_tel_data_start_dict(spd6_data);
2731         rte_tel_data_start_dict(sad_data);
2732
2733         /* add children dicts to parent dict */
2734
2735         if (params) {
2736                 coreid = (uint32_t)atoi(params);
2737                 if (rte_lcore_is_enabled(coreid) == 0) {
2738                         rc = -EINVAL;
2739                         goto exit;
2740                 }
2741         }
2742
2743         update_statistics(&total_stats, coreid);
2744
2745         /* add sad telemetry key/values pairs */
2746
2747         rte_tel_data_add_dict_u64(sad_data, "miss",
2748                 total_stats.inbound.sad.miss);
2749
2750         rte_tel_data_add_dict_container(data, "sad", sad_data, 0);
2751
2752         /* add spd 4 telemetry key/values pairs */
2753
2754         rte_tel_data_add_dict_u64(spd4_data, "protect",
2755                 total_stats.inbound.spd4.protect);
2756         rte_tel_data_add_dict_u64(spd4_data, "bypass",
2757                 total_stats.inbound.spd4.bypass);
2758         rte_tel_data_add_dict_u64(spd4_data, "discard",
2759                 total_stats.inbound.spd4.discard);
2760
2761         rte_tel_data_add_dict_container(data, "spd4", spd4_data, 0);
2762
2763         /* add spd 6 telemetry key/values pairs */
2764
2765         rte_tel_data_add_dict_u64(spd6_data, "protect",
2766                 total_stats.inbound.spd6.protect);
2767         rte_tel_data_add_dict_u64(spd6_data, "bypass",
2768                 total_stats.inbound.spd6.bypass);
2769         rte_tel_data_add_dict_u64(spd6_data, "discard",
2770                 total_stats.inbound.spd6.discard);
2771
2772         rte_tel_data_add_dict_container(data, "spd6", spd6_data, 0);
2773
2774 exit:
2775         if (rc) {
2776                 rte_tel_data_free(spd4_data);
2777                 rte_tel_data_free(spd6_data);
2778                 rte_tel_data_free(sad_data);
2779         }
2780         return rc;
2781 }
2782
2783 static int
2784 handle_telemetry_cmd_ipsec_secgw_stats_routing(const char *cmd __rte_unused,
2785                 const char *params, struct rte_tel_data *data)
2786 {
2787         struct ipsec_core_statistics total_stats;
2788
2789         struct rte_tel_data *lpm4_data = rte_tel_data_alloc();
2790         struct rte_tel_data *lpm6_data = rte_tel_data_alloc();
2791         unsigned int coreid = UINT32_MAX;
2792         int rc = 0;
2793
2794         /* verify allocated telemetry data structures */
2795         if (!lpm4_data || !lpm6_data) {
2796                 rc = -ENOMEM;
2797                 goto exit;
2798         }
2799
2800         /* initialize telemetry data structs as dicts */
2801         rte_tel_data_start_dict(data);
2802         rte_tel_data_start_dict(lpm4_data);
2803         rte_tel_data_start_dict(lpm6_data);
2804
2805
2806         if (params) {
2807                 coreid = (uint32_t)atoi(params);
2808                 if (rte_lcore_is_enabled(coreid) == 0) {
2809                         rc = -EINVAL;
2810                         goto exit;
2811                 }
2812         }
2813
2814         update_statistics(&total_stats, coreid);
2815
2816         /* add lpm 4 telemetry key/values pairs */
2817         rte_tel_data_add_dict_u64(lpm4_data, "miss",
2818                 total_stats.lpm4.miss);
2819
2820         rte_tel_data_add_dict_container(data, "IPv4 LPM", lpm4_data, 0);
2821
2822         /* add lpm 6 telemetry key/values pairs */
2823         rte_tel_data_add_dict_u64(lpm6_data, "miss",
2824                 total_stats.lpm6.miss);
2825
2826         rte_tel_data_add_dict_container(data, "IPv6 LPM", lpm6_data, 0);
2827
2828 exit:
2829         if (rc) {
2830                 rte_tel_data_free(lpm4_data);
2831                 rte_tel_data_free(lpm6_data);
2832         }
2833         return rc;
2834 }
2835
2836 static void
2837 ipsec_secgw_telemetry_init(void)
2838 {
2839         rte_telemetry_register_cmd("/examples/ipsec-secgw/stats",
2840                 handle_telemetry_cmd_ipsec_secgw_stats,
2841                 "Returns global stats. "
2842                 "Optional Parameters: int <logical core id>");
2843
2844         rte_telemetry_register_cmd("/examples/ipsec-secgw/stats/outbound",
2845                 handle_telemetry_cmd_ipsec_secgw_stats_outbound,
2846                 "Returns outbound global stats. "
2847                 "Optional Parameters: int <logical core id>");
2848
2849         rte_telemetry_register_cmd("/examples/ipsec-secgw/stats/inbound",
2850                 handle_telemetry_cmd_ipsec_secgw_stats_inbound,
2851                 "Returns inbound global stats. "
2852                 "Optional Parameters: int <logical core id>");
2853
2854         rte_telemetry_register_cmd("/examples/ipsec-secgw/stats/routing",
2855                 handle_telemetry_cmd_ipsec_secgw_stats_routing,
2856                 "Returns routing stats. "
2857                 "Optional Parameters: int <logical core id>");
2858 }
2859
2860
2861 int32_t
2862 main(int32_t argc, char **argv)
2863 {
2864         int32_t ret;
2865         uint32_t lcore_id, nb_txq, nb_rxq = 0;
2866         uint32_t cdev_id;
2867         uint32_t i;
2868         uint8_t socket_id;
2869         uint16_t portid, nb_crypto_qp, nb_ports = 0;
2870         uint64_t req_rx_offloads[RTE_MAX_ETHPORTS];
2871         uint64_t req_tx_offloads[RTE_MAX_ETHPORTS];
2872         struct eh_conf *eh_conf = NULL;
2873         size_t sess_sz;
2874
2875         nb_bufs_in_pool = 0;
2876
2877         /* init EAL */
2878         ret = rte_eal_init(argc, argv);
2879         if (ret < 0)
2880                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
2881         argc -= ret;
2882         argv += ret;
2883
2884         force_quit = false;
2885         signal(SIGINT, signal_handler);
2886         signal(SIGTERM, signal_handler);
2887
2888         /* initialize event helper configuration */
2889         eh_conf = eh_conf_init();
2890         if (eh_conf == NULL)
2891                 rte_exit(EXIT_FAILURE, "Failed to init event helper config");
2892
2893         /* parse application arguments (after the EAL ones) */
2894         ret = parse_args(argc, argv, eh_conf);
2895         if (ret < 0)
2896                 rte_exit(EXIT_FAILURE, "Invalid parameters\n");
2897
2898         ipsec_secgw_telemetry_init();
2899
2900         /* parse configuration file */
2901         if (parse_cfg_file(cfgfile) < 0) {
2902                 printf("parsing file \"%s\" failed\n",
2903                         optarg);
2904                 print_usage(argv[0]);
2905                 return -1;
2906         }
2907
2908         if ((unprotected_port_mask & enabled_port_mask) !=
2909                         unprotected_port_mask)
2910                 rte_exit(EXIT_FAILURE, "Invalid unprotected portmask 0x%x\n",
2911                                 unprotected_port_mask);
2912
2913         if (unprotected_port_mask && !nb_sa_in)
2914                 rte_exit(EXIT_FAILURE, "Cannot use unprotected portmask without configured SA inbound\n");
2915
2916         if (check_poll_mode_params(eh_conf) < 0)
2917                 rte_exit(EXIT_FAILURE, "check_poll_mode_params failed\n");
2918
2919         if (check_event_mode_params(eh_conf) < 0)
2920                 rte_exit(EXIT_FAILURE, "check_event_mode_params failed\n");
2921
2922         ret = init_lcore_rx_queues();
2923         if (ret < 0)
2924                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
2925
2926         nb_lcores = rte_lcore_count();
2927
2928         sess_sz = max_session_size();
2929
2930         /*
2931          * In event mode request minimum number of crypto queues
2932          * to be reserved equal to number of ports.
2933          */
2934         if (eh_conf->mode == EH_PKT_TRANSFER_MODE_EVENT)
2935                 nb_crypto_qp = rte_eth_dev_count_avail();
2936         else
2937                 nb_crypto_qp = 0;
2938
2939         nb_crypto_qp = cryptodevs_init(nb_crypto_qp);
2940
2941         if (nb_bufs_in_pool == 0) {
2942                 RTE_ETH_FOREACH_DEV(portid) {
2943                         if ((enabled_port_mask & (1 << portid)) == 0)
2944                                 continue;
2945                         nb_ports++;
2946                         nb_rxq += get_port_nb_rx_queues(portid);
2947                 }
2948
2949                 nb_txq = nb_lcores;
2950
2951                 nb_bufs_in_pool = calculate_nb_mbufs(nb_ports, nb_crypto_qp,
2952                                                 nb_rxq, nb_txq);
2953         }
2954
2955         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
2956                 if (rte_lcore_is_enabled(lcore_id) == 0)
2957                         continue;
2958
2959                 if (numa_on)
2960                         socket_id = (uint8_t)rte_lcore_to_socket_id(lcore_id);
2961                 else
2962                         socket_id = 0;
2963
2964                 if (per_port_pool) {
2965                         RTE_ETH_FOREACH_DEV(portid) {
2966                                 if ((enabled_port_mask & (1 << portid)) == 0)
2967                                         continue;
2968
2969                                 pool_init(&socket_ctx[socket_id], socket_id,
2970                                           portid, nb_bufs_in_pool);
2971                         }
2972                 } else {
2973                         pool_init(&socket_ctx[socket_id], socket_id, 0,
2974                                   nb_bufs_in_pool);
2975                 }
2976
2977                 if (socket_ctx[socket_id].session_pool)
2978                         continue;
2979
2980                 session_pool_init(&socket_ctx[socket_id], socket_id, sess_sz);
2981                 session_priv_pool_init(&socket_ctx[socket_id], socket_id,
2982                         sess_sz);
2983         }
2984         printf("Number of mbufs in packet pool %d\n", nb_bufs_in_pool);
2985
2986         RTE_ETH_FOREACH_DEV(portid) {
2987                 if ((enabled_port_mask & (1 << portid)) == 0)
2988                         continue;
2989
2990                 sa_check_offloads(portid, &req_rx_offloads[portid],
2991                                 &req_tx_offloads[portid]);
2992                 port_init(portid, req_rx_offloads[portid],
2993                                 req_tx_offloads[portid]);
2994         }
2995
2996         /*
2997          * Set the enabled port mask in helper config for use by helper
2998          * sub-system. This will be used while initializing devices using
2999          * helper sub-system.
3000          */
3001         eh_conf->eth_portmask = enabled_port_mask;
3002
3003         /* Initialize eventmode components */
3004         ret = eh_devs_init(eh_conf);
3005         if (ret < 0)
3006                 rte_exit(EXIT_FAILURE, "eh_devs_init failed, err=%d\n", ret);
3007
3008         /* start ports */
3009         RTE_ETH_FOREACH_DEV(portid) {
3010                 if ((enabled_port_mask & (1 << portid)) == 0)
3011                         continue;
3012
3013                 ret = rte_eth_dev_start(portid);
3014                 if (ret < 0)
3015                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: "
3016                                         "err=%d, port=%d\n", ret, portid);
3017
3018                 /* Create flow after starting the device */
3019                 create_default_ipsec_flow(portid, req_rx_offloads[portid]);
3020
3021                 /*
3022                  * If enabled, put device in promiscuous mode.
3023                  * This allows IO forwarding mode to forward packets
3024                  * to itself through 2 cross-connected  ports of the
3025                  * target machine.
3026                  */
3027                 if (promiscuous_on) {
3028                         ret = rte_eth_promiscuous_enable(portid);
3029                         if (ret != 0)
3030                                 rte_exit(EXIT_FAILURE,
3031                                         "rte_eth_promiscuous_enable: err=%s, port=%d\n",
3032                                         rte_strerror(-ret), portid);
3033                 }
3034
3035                 rte_eth_dev_callback_register(portid, RTE_ETH_EVENT_INTR_RESET,
3036                         ethdev_reset_event_callback, NULL);
3037
3038                 rte_eth_dev_callback_register(portid,
3039                         RTE_ETH_EVENT_IPSEC, inline_ipsec_event_callback, NULL);
3040         }
3041
3042         /* fragment reassemble is enabled */
3043         if (frag_tbl_sz != 0) {
3044                 ret = reassemble_init();
3045                 if (ret != 0)
3046                         rte_exit(EXIT_FAILURE, "failed at reassemble init");
3047         }
3048
3049         /* Replicate each context per socket */
3050         for (i = 0; i < NB_SOCKETS && i < rte_socket_count(); i++) {
3051                 socket_id = rte_socket_id_by_idx(i);
3052                 if ((socket_ctx[socket_id].session_pool != NULL) &&
3053                         (socket_ctx[socket_id].sa_in == NULL) &&
3054                         (socket_ctx[socket_id].sa_out == NULL)) {
3055                         sa_init(&socket_ctx[socket_id], socket_id);
3056                         sp4_init(&socket_ctx[socket_id], socket_id);
3057                         sp6_init(&socket_ctx[socket_id], socket_id);
3058                         rt_init(&socket_ctx[socket_id], socket_id);
3059                 }
3060         }
3061
3062         flow_init();
3063
3064         /* Get security context if available and only if dynamic field is
3065          * registered for fast path access.
3066          */
3067         if (!rte_security_dynfield_is_registered())
3068                 goto skip_sec_ctx;
3069
3070         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
3071                 for (i = 0; i < lcore_conf[lcore_id].nb_rx_queue; i++) {
3072                         portid = lcore_conf[lcore_id].rx_queue_list[i].port_id;
3073                         lcore_conf[lcore_id].rx_queue_list[i].sec_ctx =
3074                                 rte_eth_dev_get_sec_ctx(portid);
3075                 }
3076         }
3077 skip_sec_ctx:
3078
3079         check_all_ports_link_status(enabled_port_mask);
3080
3081         if (stats_interval > 0)
3082                 rte_eal_alarm_set(stats_interval * US_PER_S,
3083                                 print_stats_cb, NULL);
3084         else
3085                 RTE_LOG(INFO, IPSEC, "Stats display disabled\n");
3086
3087         /* launch per-lcore init on every lcore */
3088         rte_eal_mp_remote_launch(ipsec_launch_one_lcore, eh_conf, CALL_MAIN);
3089         RTE_LCORE_FOREACH_WORKER(lcore_id) {
3090                 if (rte_eal_wait_lcore(lcore_id) < 0)
3091                         return -1;
3092         }
3093
3094         /* Uninitialize eventmode components */
3095         ret = eh_devs_uninit(eh_conf);
3096         if (ret < 0)
3097                 rte_exit(EXIT_FAILURE, "eh_devs_uninit failed, err=%d\n", ret);
3098
3099         /* Free eventmode configuration memory */
3100         eh_conf_uninit(eh_conf);
3101
3102         /* Destroy inline inbound and outbound sessions */
3103         for (i = 0; i < NB_SOCKETS && i < rte_socket_count(); i++) {
3104                 socket_id = rte_socket_id_by_idx(i);
3105                 inline_sessions_free(socket_ctx[socket_id].sa_in);
3106                 inline_sessions_free(socket_ctx[socket_id].sa_out);
3107         }
3108
3109         for (cdev_id = 0; cdev_id < rte_cryptodev_count(); cdev_id++) {
3110                 printf("Closing cryptodev %d...", cdev_id);
3111                 rte_cryptodev_stop(cdev_id);
3112                 rte_cryptodev_close(cdev_id);
3113                 printf(" Done\n");
3114         }
3115
3116         RTE_ETH_FOREACH_DEV(portid) {
3117                 if ((enabled_port_mask & (1 << portid)) == 0)
3118                         continue;
3119
3120                 printf("Closing port %d...", portid);
3121                 if (flow_info_tbl[portid].rx_def_flow) {
3122                         struct rte_flow_error err;
3123
3124                         ret = rte_flow_destroy(portid,
3125                                 flow_info_tbl[portid].rx_def_flow, &err);
3126                         if (ret)
3127                                 RTE_LOG(ERR, IPSEC, "Failed to destroy flow "
3128                                         " for port %u, err msg: %s\n", portid,
3129                                         err.message);
3130                 }
3131                 ret = rte_eth_dev_stop(portid);
3132                 if (ret != 0)
3133                         RTE_LOG(ERR, IPSEC,
3134                                 "rte_eth_dev_stop: err=%s, port=%u\n",
3135                                 rte_strerror(-ret), portid);
3136
3137                 rte_eth_dev_close(portid);
3138                 printf(" Done\n");
3139         }
3140
3141         /* clean up the EAL */
3142         rte_eal_cleanup();
3143         printf("Bye...\n");
3144
3145         return 0;
3146 }