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