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