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