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