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