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