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