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