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