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