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