5f9d560dd9415fd0e774a1b287155f232ef967c1
[dpdk.git] / examples / ipsec-secgw / ipsec.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2017 Intel Corporation
3  */
4 #include <sys/types.h>
5 #include <netinet/in.h>
6 #include <netinet/ip.h>
7
8 #include <rte_branch_prediction.h>
9 #include <rte_log.h>
10 #include <rte_crypto.h>
11 #include <rte_security.h>
12 #include <rte_cryptodev.h>
13 #include <rte_ethdev.h>
14 #include <rte_mbuf.h>
15 #include <rte_hash.h>
16
17 #include "ipsec.h"
18 #include "esp.h"
19
20 static inline void
21 set_ipsec_conf(struct ipsec_sa *sa, struct rte_security_ipsec_xform *ipsec)
22 {
23         if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
24                 struct rte_security_ipsec_tunnel_param *tunnel =
25                                 &ipsec->tunnel;
26                 if (IS_IP4_TUNNEL(sa->flags)) {
27                         tunnel->type =
28                                 RTE_SECURITY_IPSEC_TUNNEL_IPV4;
29                         tunnel->ipv4.ttl = IPDEFTTL;
30
31                         memcpy((uint8_t *)&tunnel->ipv4.src_ip,
32                                 (uint8_t *)&sa->src.ip.ip4, 4);
33
34                         memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
35                                 (uint8_t *)&sa->dst.ip.ip4, 4);
36                 }
37                 /* TODO support for Transport and IPV6 tunnel */
38         }
39         ipsec->esn_soft_limit = IPSEC_OFFLOAD_ESN_SOFTLIMIT;
40 }
41
42 int
43 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
44 {
45         struct rte_cryptodev_info cdev_info;
46         unsigned long cdev_id_qp = 0;
47         int32_t ret = 0;
48         struct cdev_key key = { 0 };
49
50         key.lcore_id = (uint8_t)rte_lcore_id();
51
52         key.cipher_algo = (uint8_t)sa->cipher_algo;
53         key.auth_algo = (uint8_t)sa->auth_algo;
54         key.aead_algo = (uint8_t)sa->aead_algo;
55
56         ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
57                         (void **)&cdev_id_qp);
58         if (ret < 0) {
59                 RTE_LOG(ERR, IPSEC,
60                                 "No cryptodev: core %u, cipher_algo %u, "
61                                 "auth_algo %u, aead_algo %u\n",
62                                 key.lcore_id,
63                                 key.cipher_algo,
64                                 key.auth_algo,
65                                 key.aead_algo);
66                 return -1;
67         }
68
69         RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
70                         "%u qp %u\n", sa->spi,
71                         ipsec_ctx->tbl[cdev_id_qp].id,
72                         ipsec_ctx->tbl[cdev_id_qp].qp);
73
74         if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
75                 struct rte_security_session_conf sess_conf = {
76                         .action_type = sa->type,
77                         .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
78                         {.ipsec = {
79                                 .spi = sa->spi,
80                                 .salt = sa->salt,
81                                 .options = { 0 },
82                                 .direction = sa->direction,
83                                 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
84                                 .mode = (IS_TUNNEL(sa->flags)) ?
85                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
86                                         RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
87                         } },
88                         .crypto_xform = sa->xforms,
89                         .userdata = NULL,
90
91                 };
92
93                 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
94                         struct rte_security_ctx *ctx = (struct rte_security_ctx *)
95                                                         rte_cryptodev_get_sec_ctx(
96                                                         ipsec_ctx->tbl[cdev_id_qp].id);
97
98                         /* Set IPsec parameters in conf */
99                         set_ipsec_conf(sa, &(sess_conf.ipsec));
100
101                         sa->sec_session = rte_security_session_create(ctx,
102                                         &sess_conf, ipsec_ctx->session_priv_pool);
103                         if (sa->sec_session == NULL) {
104                                 RTE_LOG(ERR, IPSEC,
105                                 "SEC Session init failed: err: %d\n", ret);
106                                 return -1;
107                         }
108                 } else {
109                         RTE_LOG(ERR, IPSEC, "Inline not supported\n");
110                         return -1;
111                 }
112         } else {
113                 sa->crypto_session = rte_cryptodev_sym_session_create(
114                                 ipsec_ctx->session_pool);
115                 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
116                                 sa->crypto_session, sa->xforms,
117                                 ipsec_ctx->session_priv_pool);
118
119                 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
120                                 &cdev_info);
121         }
122
123         sa->cdev_id_qp = cdev_id_qp;
124
125         return 0;
126 }
127
128 int
129 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa)
130 {
131         int32_t ret = 0;
132         struct rte_security_ctx *sec_ctx;
133         struct rte_security_session_conf sess_conf = {
134                 .action_type = sa->type,
135                 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
136                 {.ipsec = {
137                         .spi = sa->spi,
138                         .salt = sa->salt,
139                         .options = { 0 },
140                         .direction = sa->direction,
141                         .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
142                         .mode = (sa->flags == IP4_TUNNEL ||
143                                         sa->flags == IP6_TUNNEL) ?
144                                         RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
145                                         RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
146                 } },
147                 .crypto_xform = sa->xforms,
148                 .userdata = NULL,
149         };
150
151         RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n",
152                 sa->spi, sa->portid);
153
154         if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
155                 struct rte_flow_error err;
156                 const struct rte_security_capability *sec_cap;
157                 int ret = 0;
158
159                 sec_ctx = (struct rte_security_ctx *)
160                                         rte_eth_dev_get_sec_ctx(
161                                         sa->portid);
162                 if (sec_ctx == NULL) {
163                         RTE_LOG(ERR, IPSEC,
164                                 " rte_eth_dev_get_sec_ctx failed\n");
165                         return -1;
166                 }
167
168                 sa->sec_session = rte_security_session_create(sec_ctx,
169                                 &sess_conf, skt_ctx->session_pool);
170                 if (sa->sec_session == NULL) {
171                         RTE_LOG(ERR, IPSEC,
172                                 "SEC Session init failed: err: %d\n", ret);
173                         return -1;
174                 }
175
176                 sec_cap = rte_security_capabilities_get(sec_ctx);
177
178                 /* iterate until ESP tunnel*/
179                 while (sec_cap->action != RTE_SECURITY_ACTION_TYPE_NONE) {
180                         if (sec_cap->action == sa->type &&
181                             sec_cap->protocol ==
182                                 RTE_SECURITY_PROTOCOL_IPSEC &&
183                             sec_cap->ipsec.mode ==
184                                 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
185                             sec_cap->ipsec.direction == sa->direction)
186                                 break;
187                         sec_cap++;
188                 }
189
190                 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
191                         RTE_LOG(ERR, IPSEC,
192                                 "No suitable security capability found\n");
193                         return -1;
194                 }
195
196                 sa->ol_flags = sec_cap->ol_flags;
197                 sa->security_ctx = sec_ctx;
198                 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
199
200                 if (IS_IP6(sa->flags)) {
201                         sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
202                         sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
203                         sa->pattern[1].spec = &sa->ipv6_spec;
204
205                         memcpy(sa->ipv6_spec.hdr.dst_addr,
206                                 sa->dst.ip.ip6.ip6_b, 16);
207                         memcpy(sa->ipv6_spec.hdr.src_addr,
208                                sa->src.ip.ip6.ip6_b, 16);
209                 } else if (IS_IP4(sa->flags)) {
210                         sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
211                         sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
212                         sa->pattern[1].spec = &sa->ipv4_spec;
213
214                         sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
215                         sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
216                 }
217
218                 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
219                 sa->pattern[2].spec = &sa->esp_spec;
220                 sa->pattern[2].mask = &rte_flow_item_esp_mask;
221                 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
222
223                 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
224
225                 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
226                 sa->action[0].conf = sa->sec_session;
227
228                 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
229
230                 sa->attr.egress = (sa->direction ==
231                                 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
232                 sa->attr.ingress = (sa->direction ==
233                                 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
234                 if (sa->attr.ingress) {
235                         uint8_t rss_key[40];
236                         struct rte_eth_rss_conf rss_conf = {
237                                 .rss_key = rss_key,
238                                 .rss_key_len = 40,
239                         };
240                         struct rte_eth_dev_info dev_info;
241                         uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
242                         struct rte_flow_action_rss action_rss;
243                         unsigned int i;
244                         unsigned int j;
245
246                         rte_eth_dev_info_get(sa->portid, &dev_info);
247                         sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
248                         /* Try RSS. */
249                         sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
250                         sa->action[1].conf = &action_rss;
251                         ret = rte_eth_dev_rss_hash_conf_get(sa->portid,
252                                         &rss_conf);
253                         if (ret != 0) {
254                                 RTE_LOG(ERR, IPSEC,
255                                         "rte_eth_dev_rss_hash_conf_get:ret=%d\n",
256                                         ret);
257                                 return -1;
258                         }
259                         for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i)
260                                 queue[j++] = i;
261
262                         action_rss = (struct rte_flow_action_rss){
263                                         .types = rss_conf.rss_hf,
264                                         .key_len = rss_conf.rss_key_len,
265                                         .queue_num = j,
266                                         .key = rss_key,
267                                         .queue = queue,
268                         };
269                         ret = rte_flow_validate(sa->portid, &sa->attr,
270                                                 sa->pattern, sa->action,
271                                                 &err);
272                         if (!ret)
273                                 goto flow_create;
274                         /* Try Queue. */
275                         sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
276                         sa->action[1].conf =
277                                 &(struct rte_flow_action_queue){
278                                 .index = 0,
279                         };
280                         ret = rte_flow_validate(sa->portid, &sa->attr,
281                                                 sa->pattern, sa->action,
282                                                 &err);
283                         /* Try End. */
284                         sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
285                         sa->action[1].conf = NULL;
286                         ret = rte_flow_validate(sa->portid, &sa->attr,
287                                                 sa->pattern, sa->action,
288                                                 &err);
289                         if (ret)
290                                 goto flow_create_failure;
291                 } else if (sa->attr.egress &&
292                            (sa->ol_flags &
293                                     RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
294                         sa->action[1].type =
295                                         RTE_FLOW_ACTION_TYPE_PASSTHRU;
296                         sa->action[2].type =
297                                         RTE_FLOW_ACTION_TYPE_END;
298                 }
299 flow_create:
300                 sa->flow = rte_flow_create(sa->portid,
301                                 &sa->attr, sa->pattern, sa->action, &err);
302                 if (sa->flow == NULL) {
303 flow_create_failure:
304                         RTE_LOG(ERR, IPSEC,
305                                 "Failed to create ipsec flow msg: %s\n",
306                                 err.message);
307                         return -1;
308                 }
309         } else if (sa->type ==  RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
310                 const struct rte_security_capability *sec_cap;
311
312                 sec_ctx = (struct rte_security_ctx *)
313                                 rte_eth_dev_get_sec_ctx(sa->portid);
314
315                 if (sec_ctx == NULL) {
316                         RTE_LOG(ERR, IPSEC,
317                                 "Ethernet device doesn't have security features registered\n");
318                         return -1;
319                 }
320
321                 /* Set IPsec parameters in conf */
322                 set_ipsec_conf(sa, &(sess_conf.ipsec));
323
324                 /* Save SA as userdata for the security session. When
325                  * the packet is received, this userdata will be
326                  * retrieved using the metadata from the packet.
327                  *
328                  * The PMD is expected to set similar metadata for other
329                  * operations, like rte_eth_event, which are tied to
330                  * security session. In such cases, the userdata could
331                  * be obtained to uniquely identify the security
332                  * parameters denoted.
333                  */
334
335                 sess_conf.userdata = (void *) sa;
336
337                 sa->sec_session = rte_security_session_create(sec_ctx,
338                                         &sess_conf, skt_ctx->session_pool);
339                 if (sa->sec_session == NULL) {
340                         RTE_LOG(ERR, IPSEC,
341                                 "SEC Session init failed: err: %d\n", ret);
342                         return -1;
343                 }
344
345                 sec_cap = rte_security_capabilities_get(sec_ctx);
346                 if (sec_cap == NULL) {
347                         RTE_LOG(ERR, IPSEC,
348                                 "No capabilities registered\n");
349                         return -1;
350                 }
351
352                 /* iterate until ESP tunnel*/
353                 while (sec_cap->action !=
354                                 RTE_SECURITY_ACTION_TYPE_NONE) {
355                         if (sec_cap->action == sa->type &&
356                             sec_cap->protocol ==
357                                 RTE_SECURITY_PROTOCOL_IPSEC &&
358                             sec_cap->ipsec.mode ==
359                                 sess_conf.ipsec.mode &&
360                             sec_cap->ipsec.direction == sa->direction)
361                                 break;
362                         sec_cap++;
363                 }
364
365                 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
366                         RTE_LOG(ERR, IPSEC,
367                                 "No suitable security capability found\n");
368                         return -1;
369                 }
370
371                 sa->ol_flags = sec_cap->ol_flags;
372                 sa->security_ctx = sec_ctx;
373         }
374         sa->cdev_id_qp = 0;
375
376         return 0;
377 }
378
379 /*
380  * queue crypto-ops into PMD queue.
381  */
382 void
383 enqueue_cop_burst(struct cdev_qp *cqp)
384 {
385         uint32_t i, len, ret;
386
387         len = cqp->len;
388         ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
389         if (ret < len) {
390                 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
391                         " enqueued %u crypto ops out of %u\n",
392                         cqp->id, cqp->qp, ret, len);
393                         /* drop packets that we fail to enqueue */
394                         for (i = ret; i < len; i++)
395                                 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
396         }
397         cqp->in_flight += ret;
398         cqp->len = 0;
399 }
400
401 static inline void
402 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
403 {
404         cqp->buf[cqp->len++] = cop;
405
406         if (cqp->len == MAX_PKT_BURST)
407                 enqueue_cop_burst(cqp);
408 }
409
410 static inline void
411 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
412                 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
413                 uint16_t nb_pkts)
414 {
415         int32_t ret = 0, i;
416         struct ipsec_mbuf_metadata *priv;
417         struct rte_crypto_sym_op *sym_cop;
418         struct ipsec_sa *sa;
419
420         for (i = 0; i < nb_pkts; i++) {
421                 if (unlikely(sas[i] == NULL)) {
422                         rte_pktmbuf_free(pkts[i]);
423                         continue;
424                 }
425
426                 rte_prefetch0(sas[i]);
427                 rte_prefetch0(pkts[i]);
428
429                 priv = get_priv(pkts[i]);
430                 sa = sas[i];
431                 priv->sa = sa;
432
433                 switch (sa->type) {
434                 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
435                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
436                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
437
438                         rte_prefetch0(&priv->sym_cop);
439
440                         if ((unlikely(sa->sec_session == NULL)) &&
441                                 create_lookaside_session(ipsec_ctx, sa)) {
442                                 rte_pktmbuf_free(pkts[i]);
443                                 continue;
444                         }
445
446                         sym_cop = get_sym_cop(&priv->cop);
447                         sym_cop->m_src = pkts[i];
448
449                         rte_security_attach_session(&priv->cop,
450                                         sa->sec_session);
451                         break;
452                 case RTE_SECURITY_ACTION_TYPE_NONE:
453
454                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
455                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
456
457                         rte_prefetch0(&priv->sym_cop);
458
459                         if ((unlikely(sa->crypto_session == NULL)) &&
460                                 create_lookaside_session(ipsec_ctx, sa)) {
461                                 rte_pktmbuf_free(pkts[i]);
462                                 continue;
463                         }
464
465                         rte_crypto_op_attach_sym_session(&priv->cop,
466                                         sa->crypto_session);
467
468                         ret = xform_func(pkts[i], sa, &priv->cop);
469                         if (unlikely(ret)) {
470                                 rte_pktmbuf_free(pkts[i]);
471                                 continue;
472                         }
473                         break;
474                 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
475                         RTE_ASSERT(sa->sec_session != NULL);
476                         ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
477                         if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
478                                 rte_security_set_pkt_metadata(
479                                                 sa->security_ctx,
480                                                 sa->sec_session, pkts[i], NULL);
481                         continue;
482                 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
483                         RTE_ASSERT(sa->sec_session != NULL);
484                         priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
485                         priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
486
487                         rte_prefetch0(&priv->sym_cop);
488                         rte_security_attach_session(&priv->cop,
489                                         sa->sec_session);
490
491                         ret = xform_func(pkts[i], sa, &priv->cop);
492                         if (unlikely(ret)) {
493                                 rte_pktmbuf_free(pkts[i]);
494                                 continue;
495                         }
496
497                         ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
498                         if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
499                                 rte_security_set_pkt_metadata(
500                                                 sa->security_ctx,
501                                                 sa->sec_session, pkts[i], NULL);
502                         continue;
503                 }
504
505                 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
506                 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
507         }
508 }
509
510 static inline int32_t
511 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
512               struct rte_mbuf *pkts[], uint16_t max_pkts)
513 {
514         int32_t nb_pkts, ret;
515         struct ipsec_mbuf_metadata *priv;
516         struct ipsec_sa *sa;
517         struct rte_mbuf *pkt;
518
519         nb_pkts = 0;
520         while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
521                 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
522                 rte_prefetch0(pkt);
523                 priv = get_priv(pkt);
524                 sa = priv->sa;
525                 ret = xform_func(pkt, sa, &priv->cop);
526                 if (unlikely(ret)) {
527                         rte_pktmbuf_free(pkt);
528                         continue;
529                 }
530                 pkts[nb_pkts++] = pkt;
531         }
532
533         return nb_pkts;
534 }
535
536 static inline int
537 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
538               struct rte_mbuf *pkts[], uint16_t max_pkts)
539 {
540         int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
541         struct ipsec_mbuf_metadata *priv;
542         struct rte_crypto_op *cops[max_pkts];
543         struct ipsec_sa *sa;
544         struct rte_mbuf *pkt;
545
546         for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
547                 struct cdev_qp *cqp;
548
549                 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
550                 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
551                         ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
552
553                 if (cqp->in_flight == 0)
554                         continue;
555
556                 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
557                                 cops, max_pkts - nb_pkts);
558
559                 cqp->in_flight -= nb_cops;
560
561                 for (j = 0; j < nb_cops; j++) {
562                         pkt = cops[j]->sym->m_src;
563                         rte_prefetch0(pkt);
564
565                         priv = get_priv(pkt);
566                         sa = priv->sa;
567
568                         RTE_ASSERT(sa != NULL);
569
570                         if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
571                                 ret = xform_func(pkt, sa, cops[j]);
572                                 if (unlikely(ret)) {
573                                         rte_pktmbuf_free(pkt);
574                                         continue;
575                                 }
576                         }
577                         pkts[nb_pkts++] = pkt;
578                 }
579         }
580
581         /* return packets */
582         return nb_pkts;
583 }
584
585 uint16_t
586 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
587                 uint16_t nb_pkts, uint16_t len)
588 {
589         struct ipsec_sa *sas[nb_pkts];
590
591         inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
592
593         ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
594
595         return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
596 }
597
598 uint16_t
599 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
600                 uint16_t len)
601 {
602         return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
603 }
604
605 uint16_t
606 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
607                 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
608 {
609         struct ipsec_sa *sas[nb_pkts];
610
611         outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
612
613         ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
614
615         return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
616 }
617
618 uint16_t
619 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
620                 uint16_t len)
621 {
622         return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);
623 }