xref: /dpdk/examples/ipsec-secgw/ipsec.c (revision 2ede1422fa57225b0864702083a8c7bea2c5117e)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
25139d5d9SMarcin Smoczynski  * Copyright(c) 2016-2020 Intel Corporation
3d299106eSSergio Gonzalez Monroy  */
455d4c775SDaniel Mrzyglod #include <sys/types.h>
5d299106eSSergio Gonzalez Monroy #include <netinet/in.h>
6d299106eSSergio Gonzalez Monroy #include <netinet/ip.h>
7d299106eSSergio Gonzalez Monroy 
8d299106eSSergio Gonzalez Monroy #include <rte_branch_prediction.h>
96938fc92SVolodymyr Fialko #include <rte_event_crypto_adapter.h>
10d299106eSSergio Gonzalez Monroy #include <rte_log.h>
11d299106eSSergio Gonzalez Monroy #include <rte_crypto.h>
12ec17993aSAkhil Goyal #include <rte_security.h>
13d299106eSSergio Gonzalez Monroy #include <rte_cryptodev.h>
145139d5d9SMarcin Smoczynski #include <rte_ipsec.h>
15ec17993aSAkhil Goyal #include <rte_ethdev.h>
16d299106eSSergio Gonzalez Monroy #include <rte_mbuf.h>
17d299106eSSergio Gonzalez Monroy #include <rte_hash.h>
18d299106eSSergio Gonzalez Monroy 
19d299106eSSergio Gonzalez Monroy #include "ipsec.h"
20c64278c0SSergio Gonzalez Monroy #include "esp.h"
21d299106eSSergio Gonzalez Monroy 
220ccfd14bSAnoob Joseph static inline void
230ccfd14bSAnoob Joseph set_ipsec_conf(struct ipsec_sa *sa, struct rte_security_ipsec_xform *ipsec)
240ccfd14bSAnoob Joseph {
250ccfd14bSAnoob Joseph 	if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
260ccfd14bSAnoob Joseph 		struct rte_security_ipsec_tunnel_param *tunnel =
270ccfd14bSAnoob Joseph 				&ipsec->tunnel;
28b1a3ac78SMariusz Drost 		if (IS_IP4_TUNNEL(sa->flags)) {
290ccfd14bSAnoob Joseph 			tunnel->type =
300ccfd14bSAnoob Joseph 				RTE_SECURITY_IPSEC_TUNNEL_IPV4;
310ccfd14bSAnoob Joseph 			tunnel->ipv4.ttl = IPDEFTTL;
320ccfd14bSAnoob Joseph 
330ccfd14bSAnoob Joseph 			memcpy((uint8_t *)&tunnel->ipv4.src_ip,
340ccfd14bSAnoob Joseph 				(uint8_t *)&sa->src.ip.ip4, 4);
350ccfd14bSAnoob Joseph 
360ccfd14bSAnoob Joseph 			memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
370ccfd14bSAnoob Joseph 				(uint8_t *)&sa->dst.ip.ip4, 4);
380d9b0263SAkhil Goyal 		} else if (IS_IP6_TUNNEL(sa->flags)) {
390d9b0263SAkhil Goyal 			tunnel->type =
400d9b0263SAkhil Goyal 				RTE_SECURITY_IPSEC_TUNNEL_IPV6;
410d9b0263SAkhil Goyal 			tunnel->ipv6.hlimit = IPDEFTTL;
420d9b0263SAkhil Goyal 			tunnel->ipv6.dscp = 0;
430d9b0263SAkhil Goyal 			tunnel->ipv6.flabel = 0;
44*2ede1422SRobin Jarry 			tunnel->ipv6.src_addr = sa->src.ip.ip6;
45*2ede1422SRobin Jarry 			tunnel->ipv6.dst_addr = sa->dst.ip.ip6;
460ccfd14bSAnoob Joseph 		}
470d9b0263SAkhil Goyal 		/* TODO support for Transport */
480ccfd14bSAnoob Joseph 	}
490f56ca1aSHemant Agrawal 	ipsec->replay_win_sz = app_sa_prm.window_size;
500f56ca1aSHemant Agrawal 	ipsec->options.esn = app_sa_prm.enable_esn;
519a1cc8f1STejasree Kondoj 	ipsec->options.udp_encap = sa->udp_encap;
52d8d51d4fSRahul Bhansali 	if (IS_HW_REASSEMBLY_EN(sa->flags))
53d8d51d4fSRahul Bhansali 		ipsec->options.ip_reassembly_en = 1;
540ccfd14bSAnoob Joseph }
550ccfd14bSAnoob Joseph 
56a8781df8SAkhil Goyal static inline int
57a8781df8SAkhil Goyal verify_crypto_xform(const struct rte_cryptodev_capabilities *capabilities,
58a8781df8SAkhil Goyal 		struct rte_crypto_sym_xform *crypto_xform)
59a8781df8SAkhil Goyal {
60a8781df8SAkhil Goyal 	const struct rte_cryptodev_capabilities *crypto_cap;
61a8781df8SAkhil Goyal 	int j = 0;
62a8781df8SAkhil Goyal 
63a8781df8SAkhil Goyal 	while ((crypto_cap = &capabilities[j++])->op != RTE_CRYPTO_OP_TYPE_UNDEFINED) {
64a8781df8SAkhil Goyal 		if (crypto_cap->op == RTE_CRYPTO_OP_TYPE_SYMMETRIC &&
65a8781df8SAkhil Goyal 				crypto_cap->sym.xform_type == crypto_xform->type) {
66a8781df8SAkhil Goyal 			if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AEAD &&
67a8781df8SAkhil Goyal 					crypto_cap->sym.aead.algo == crypto_xform->aead.algo) {
68a8781df8SAkhil Goyal 				if (rte_cryptodev_sym_capability_check_aead(&crypto_cap->sym,
69a8781df8SAkhil Goyal 						crypto_xform->aead.key.length,
70a8781df8SAkhil Goyal 						crypto_xform->aead.digest_length,
71a8781df8SAkhil Goyal 						crypto_xform->aead.aad_length,
72a8781df8SAkhil Goyal 						crypto_xform->aead.iv.length) == 0)
73a8781df8SAkhil Goyal 					return 0;
74a8781df8SAkhil Goyal 			}
75a8781df8SAkhil Goyal 			if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_CIPHER &&
76a8781df8SAkhil Goyal 					crypto_cap->sym.cipher.algo == crypto_xform->cipher.algo) {
77a8781df8SAkhil Goyal 				if (rte_cryptodev_sym_capability_check_cipher(&crypto_cap->sym,
78a8781df8SAkhil Goyal 						crypto_xform->cipher.key.length,
79a8781df8SAkhil Goyal 						crypto_xform->cipher.iv.length) == 0)
80a8781df8SAkhil Goyal 					return 0;
81a8781df8SAkhil Goyal 			}
82a8781df8SAkhil Goyal 			if (crypto_xform->type == RTE_CRYPTO_SYM_XFORM_AUTH &&
83a8781df8SAkhil Goyal 					crypto_cap->sym.auth.algo == crypto_xform->auth.algo) {
84a8781df8SAkhil Goyal 				if (rte_cryptodev_sym_capability_check_auth(&crypto_cap->sym,
85a8781df8SAkhil Goyal 						crypto_xform->auth.key.length,
86a8781df8SAkhil Goyal 						crypto_xform->auth.digest_length,
87a8781df8SAkhil Goyal 						crypto_xform->auth.iv.length) == 0)
88a8781df8SAkhil Goyal 					return 0;
89a8781df8SAkhil Goyal 			}
90a8781df8SAkhil Goyal 		}
91a8781df8SAkhil Goyal 	}
92a8781df8SAkhil Goyal 
93a8781df8SAkhil Goyal 	return -ENOTSUP;
94a8781df8SAkhil Goyal }
95a8781df8SAkhil Goyal 
96a8781df8SAkhil Goyal static inline int
97a8781df8SAkhil Goyal verify_crypto_capabilities(const struct rte_cryptodev_capabilities *capabilities,
98a8781df8SAkhil Goyal 		struct rte_crypto_sym_xform *crypto_xform)
99a8781df8SAkhil Goyal {
100c3ebd47aSRadu Nicolau 	if (crypto_xform->next != NULL)
101a8781df8SAkhil Goyal 		return (verify_crypto_xform(capabilities, crypto_xform) ||
102a8781df8SAkhil Goyal 		    verify_crypto_xform(capabilities, crypto_xform->next));
103a8781df8SAkhil Goyal 	else
104c3ebd47aSRadu Nicolau 		return verify_crypto_xform(capabilities, crypto_xform);
105a8781df8SAkhil Goyal }
106a8781df8SAkhil Goyal 
107a8781df8SAkhil Goyal static inline int
108a8781df8SAkhil Goyal verify_ipsec_capabilities(struct rte_security_ipsec_xform *ipsec_xform,
109a8781df8SAkhil Goyal 		const struct rte_security_capability *sec_cap)
110a8781df8SAkhil Goyal {
111a8781df8SAkhil Goyal 	/* Verify security capabilities */
112a8781df8SAkhil Goyal 
113a8781df8SAkhil Goyal 	if (ipsec_xform->options.esn == 1 && sec_cap->ipsec.options.esn == 0) {
114a8781df8SAkhil Goyal 		RTE_LOG(INFO, USER1, "ESN is not supported\n");
115a8781df8SAkhil Goyal 		return -ENOTSUP;
116a8781df8SAkhil Goyal 	}
117a8781df8SAkhil Goyal 
118a8781df8SAkhil Goyal 	if (ipsec_xform->options.udp_encap == 1 &&
119a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.udp_encap == 0) {
120a8781df8SAkhil Goyal 		RTE_LOG(INFO, USER1, "UDP encapsulation is not supported\n");
121a8781df8SAkhil Goyal 		return -ENOTSUP;
122a8781df8SAkhil Goyal 	}
123a8781df8SAkhil Goyal 
124a8781df8SAkhil Goyal 	if (ipsec_xform->options.udp_ports_verify == 1 &&
125a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.udp_ports_verify == 0) {
126a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1,
127a8781df8SAkhil Goyal 			"UDP encapsulation ports verification is not supported\n");
128a8781df8SAkhil Goyal 		return -ENOTSUP;
129a8781df8SAkhil Goyal 	}
130a8781df8SAkhil Goyal 
131a8781df8SAkhil Goyal 	if (ipsec_xform->options.copy_dscp == 1 &&
132a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.copy_dscp == 0) {
133a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Copy DSCP is not supported\n");
134a8781df8SAkhil Goyal 		return -ENOTSUP;
135a8781df8SAkhil Goyal 	}
136a8781df8SAkhil Goyal 
137a8781df8SAkhil Goyal 	if (ipsec_xform->options.copy_flabel == 1 &&
138a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.copy_flabel == 0) {
139a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Copy Flow Label is not supported\n");
140a8781df8SAkhil Goyal 		return -ENOTSUP;
141a8781df8SAkhil Goyal 	}
142a8781df8SAkhil Goyal 
143a8781df8SAkhil Goyal 	if (ipsec_xform->options.copy_df == 1 &&
144a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.copy_df == 0) {
145a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Copy DP bit is not supported\n");
146a8781df8SAkhil Goyal 		return -ENOTSUP;
147a8781df8SAkhil Goyal 	}
148a8781df8SAkhil Goyal 
149a8781df8SAkhil Goyal 	if (ipsec_xform->options.dec_ttl == 1 &&
150a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.dec_ttl == 0) {
151a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Decrement TTL is not supported\n");
152a8781df8SAkhil Goyal 		return -ENOTSUP;
153a8781df8SAkhil Goyal 	}
154a8781df8SAkhil Goyal 
155a8781df8SAkhil Goyal 	if (ipsec_xform->options.ecn == 1 && sec_cap->ipsec.options.ecn == 0) {
156a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "ECN is not supported\n");
157a8781df8SAkhil Goyal 		return -ENOTSUP;
158a8781df8SAkhil Goyal 	}
159a8781df8SAkhil Goyal 
160a8781df8SAkhil Goyal 	if (ipsec_xform->options.stats == 1 &&
161a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.stats == 0) {
162a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Stats is not supported\n");
163a8781df8SAkhil Goyal 		return -ENOTSUP;
164a8781df8SAkhil Goyal 	}
165a8781df8SAkhil Goyal 
166a8781df8SAkhil Goyal 	if ((ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) &&
167a8781df8SAkhil Goyal 	    (ipsec_xform->options.iv_gen_disable == 1) &&
168a8781df8SAkhil Goyal 	    (sec_cap->ipsec.options.iv_gen_disable != 1)) {
169a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Application provided IV is not supported\n");
170a8781df8SAkhil Goyal 		return -ENOTSUP;
171a8781df8SAkhil Goyal 	}
172a8781df8SAkhil Goyal 
173a8781df8SAkhil Goyal 	if ((ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) &&
174a8781df8SAkhil Goyal 	    (ipsec_xform->options.tunnel_hdr_verify >
175a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.tunnel_hdr_verify)) {
176a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Tunnel header verify is not supported\n");
177a8781df8SAkhil Goyal 		return -ENOTSUP;
178a8781df8SAkhil Goyal 	}
179a8781df8SAkhil Goyal 
180a8781df8SAkhil Goyal 	if (ipsec_xform->options.ip_csum_enable == 1 &&
181a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.ip_csum_enable == 0) {
182a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Inner IP checksum is not supported\n");
183a8781df8SAkhil Goyal 		return -ENOTSUP;
184a8781df8SAkhil Goyal 	}
185a8781df8SAkhil Goyal 
186a8781df8SAkhil Goyal 	if (ipsec_xform->options.l4_csum_enable == 1 &&
187a8781df8SAkhil Goyal 	    sec_cap->ipsec.options.l4_csum_enable == 0) {
188a8781df8SAkhil Goyal 		RTE_LOG(DEBUG, USER1, "Inner L4 checksum is not supported\n");
189a8781df8SAkhil Goyal 		return -ENOTSUP;
190a8781df8SAkhil Goyal 	}
191a8781df8SAkhil Goyal 
192a8781df8SAkhil Goyal 	if (ipsec_xform->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS) {
193a8781df8SAkhil Goyal 		if (ipsec_xform->replay_win_sz > sec_cap->ipsec.replay_win_sz_max) {
194a8781df8SAkhil Goyal 			RTE_LOG(DEBUG, USER1, "Replay window size is not supported\n");
195a8781df8SAkhil Goyal 			return -ENOTSUP;
196a8781df8SAkhil Goyal 		}
197a8781df8SAkhil Goyal 	}
198a8781df8SAkhil Goyal 
199a8781df8SAkhil Goyal 	return 0;
200a8781df8SAkhil Goyal }
201a8781df8SAkhil Goyal 
202a8781df8SAkhil Goyal 
203a8781df8SAkhil Goyal static inline int
20479bdb787SAkhil Goyal verify_security_capabilities(void *ctx,
2058a77c1b7SAkhil Goyal 		struct rte_security_session_conf *sess_conf,
2068a77c1b7SAkhil Goyal 		uint32_t *ol_flags)
207a8781df8SAkhil Goyal {
208a8781df8SAkhil Goyal 	struct rte_security_capability_idx sec_cap_idx;
209a8781df8SAkhil Goyal 	const struct rte_security_capability *sec_cap;
210a8781df8SAkhil Goyal 
211a8781df8SAkhil Goyal 	sec_cap_idx.action = sess_conf->action_type;
212a8781df8SAkhil Goyal 	sec_cap_idx.protocol = sess_conf->protocol;
213a8781df8SAkhil Goyal 	sec_cap_idx.ipsec.proto = sess_conf->ipsec.proto;
214a8781df8SAkhil Goyal 	sec_cap_idx.ipsec.mode = sess_conf->ipsec.mode;
215a8781df8SAkhil Goyal 	sec_cap_idx.ipsec.direction = sess_conf->ipsec.direction;
216a8781df8SAkhil Goyal 
217a8781df8SAkhil Goyal 	sec_cap = rte_security_capability_get(ctx, &sec_cap_idx);
218a8781df8SAkhil Goyal 	if (sec_cap == NULL)
219a8781df8SAkhil Goyal 		return -ENOTSUP;
220a8781df8SAkhil Goyal 
221a8781df8SAkhil Goyal 	if (verify_crypto_capabilities(sec_cap->crypto_capabilities,
222a8781df8SAkhil Goyal 				sess_conf->crypto_xform))
223a8781df8SAkhil Goyal 		return -ENOTSUP;
224a8781df8SAkhil Goyal 
225a8781df8SAkhil Goyal 	if (verify_ipsec_capabilities(&sess_conf->ipsec, sec_cap))
226a8781df8SAkhil Goyal 		return -ENOTSUP;
227a8781df8SAkhil Goyal 
2288a77c1b7SAkhil Goyal 	if (ol_flags != NULL)
2298a77c1b7SAkhil Goyal 		*ol_flags = sec_cap->ol_flags;
2308a77c1b7SAkhil Goyal 
231a8781df8SAkhil Goyal 	return 0;
232a8781df8SAkhil Goyal }
233a8781df8SAkhil Goyal 
2343e5f4625SKonstantin Ananyev int
235a8ade121SVolodymyr Fialko create_lookaside_session(struct ipsec_ctx *ipsec_ctx_lcore[],
2366938fc92SVolodymyr Fialko 	struct socket_ctx *skt_ctx, const struct eventmode_conf *em_conf,
2376938fc92SVolodymyr Fialko 	struct ipsec_sa *sa, struct rte_ipsec_session *ips)
238d299106eSSergio Gonzalez Monroy {
239a8ade121SVolodymyr Fialko 	uint16_t cdev_id = RTE_CRYPTO_MAX_DEVS;
2406938fc92SVolodymyr Fialko 	enum rte_crypto_op_sess_type sess_type;
241c5aa9617SAkhil Goyal 	struct rte_cryptodev_info cdev_info;
2426938fc92SVolodymyr Fialko 	enum rte_crypto_op_type op_type;
2434e942500SSergio Gonzalez Monroy 	unsigned long cdev_id_qp = 0;
244a8ade121SVolodymyr Fialko 	struct ipsec_ctx *ipsec_ctx;
2456938fc92SVolodymyr Fialko 	struct cdev_key key = { 0 };
2466938fc92SVolodymyr Fialko 	void *sess = NULL;
247a8ade121SVolodymyr Fialko 	uint32_t lcore_id;
248a8ade121SVolodymyr Fialko 	int32_t ret = 0;
249d299106eSSergio Gonzalez Monroy 
250a8ade121SVolodymyr Fialko 	RTE_LCORE_FOREACH(lcore_id) {
251a8ade121SVolodymyr Fialko 		ipsec_ctx = ipsec_ctx_lcore[lcore_id];
252d299106eSSergio Gonzalez Monroy 
253a8ade121SVolodymyr Fialko 		/* Core is not bound to any cryptodev, skip it */
254a8ade121SVolodymyr Fialko 		if (ipsec_ctx->cdev_map == NULL)
255a8ade121SVolodymyr Fialko 			continue;
256a8ade121SVolodymyr Fialko 
257a8ade121SVolodymyr Fialko 		/* Looking for cryptodev, which can handle this SA */
2584b978938SSivaprasad Tummala 		key.lcore_id = lcore_id;
259d299106eSSergio Gonzalez Monroy 		key.cipher_algo = (uint8_t)sa->cipher_algo;
260d299106eSSergio Gonzalez Monroy 		key.auth_algo = (uint8_t)sa->auth_algo;
261d00f3890SAviad Yehezkel 		key.aead_algo = (uint8_t)sa->aead_algo;
262d299106eSSergio Gonzalez Monroy 
263d299106eSSergio Gonzalez Monroy 		ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
264d299106eSSergio Gonzalez Monroy 				(void **)&cdev_id_qp);
265a8ade121SVolodymyr Fialko 		if (ret == -ENOENT)
266a8ade121SVolodymyr Fialko 			continue;
267d299106eSSergio Gonzalez Monroy 		if (ret < 0) {
268ec17993aSAkhil Goyal 			RTE_LOG(ERR, IPSEC,
269ec17993aSAkhil Goyal 					"No cryptodev: core %u, cipher_algo %u, "
270d00f3890SAviad Yehezkel 					"auth_algo %u, aead_algo %u\n",
271d00f3890SAviad Yehezkel 					key.lcore_id,
272d00f3890SAviad Yehezkel 					key.cipher_algo,
273d00f3890SAviad Yehezkel 					key.auth_algo,
274d00f3890SAviad Yehezkel 					key.aead_algo);
275a8ade121SVolodymyr Fialko 			return ret;
276d299106eSSergio Gonzalez Monroy 		}
277d299106eSSergio Gonzalez Monroy 
278a8ade121SVolodymyr Fialko 		/* Verify that all cores are using same cryptodev for current
279a8ade121SVolodymyr Fialko 		 * algorithm combination, required by SA.
280a8ade121SVolodymyr Fialko 		 * Current cryptodev mapping process will map SA to the first
281a8ade121SVolodymyr Fialko 		 * cryptodev that matches requirements, so it's a double check,
282a8ade121SVolodymyr Fialko 		 * not an additional restriction.
283a8ade121SVolodymyr Fialko 		 */
284a8ade121SVolodymyr Fialko 		if (cdev_id == RTE_CRYPTO_MAX_DEVS)
285a8ade121SVolodymyr Fialko 			cdev_id = ipsec_ctx->tbl[cdev_id_qp].id;
286a8ade121SVolodymyr Fialko 		else if (cdev_id != ipsec_ctx->tbl[cdev_id_qp].id) {
287f406064fSRadu Nicolau 			struct rte_cryptodev_info dev_info_1, dev_info_2;
288f406064fSRadu Nicolau 			rte_cryptodev_info_get(cdev_id, &dev_info_1);
289f406064fSRadu Nicolau 			rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
290f406064fSRadu Nicolau 					&dev_info_2);
291f406064fSRadu Nicolau 			if (dev_info_1.driver_id == dev_info_2.driver_id) {
292f406064fSRadu Nicolau 				RTE_LOG(WARNING, IPSEC,
293f406064fSRadu Nicolau 					"SA mapped to multiple cryptodevs for SPI %d\n",
294f406064fSRadu Nicolau 					sa->spi);
295f406064fSRadu Nicolau 
296f406064fSRadu Nicolau 			} else {
297f406064fSRadu Nicolau 				RTE_LOG(WARNING, IPSEC,
298f406064fSRadu Nicolau 					"SA mapped to multiple cryptodevs of different types for SPI %d\n",
299f406064fSRadu Nicolau 					sa->spi);
300f406064fSRadu Nicolau 
301f406064fSRadu Nicolau 			}
302a8ade121SVolodymyr Fialko 		}
303a8ade121SVolodymyr Fialko 
304a8ade121SVolodymyr Fialko 		/* Store per core queue pair information */
305a8ade121SVolodymyr Fialko 		sa->cqp[lcore_id] = &ipsec_ctx->tbl[cdev_id_qp];
306a8ade121SVolodymyr Fialko 	}
307a8ade121SVolodymyr Fialko 	if (cdev_id == RTE_CRYPTO_MAX_DEVS) {
308a8ade121SVolodymyr Fialko 		RTE_LOG(WARNING, IPSEC, "No cores found to handle SA\n");
309a8ade121SVolodymyr Fialko 		return 0;
310a8ade121SVolodymyr Fialko 	}
311a8ade121SVolodymyr Fialko 
312a8ade121SVolodymyr Fialko 	RTE_LOG(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
313a8ade121SVolodymyr Fialko 			"%u\n", sa->spi, cdev_id);
314d299106eSSergio Gonzalez Monroy 
3155139d5d9SMarcin Smoczynski 	if (ips->type != RTE_SECURITY_ACTION_TYPE_NONE &&
3165139d5d9SMarcin Smoczynski 		ips->type != RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) {
317ec17993aSAkhil Goyal 		struct rte_security_session_conf sess_conf = {
3184a67af84SMarcin Smoczynski 			.action_type = ips->type,
319ec17993aSAkhil Goyal 			.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
320376ee1deSRadu Nicolau 			{.ipsec = {
321ec17993aSAkhil Goyal 				.spi = sa->spi,
322ec17993aSAkhil Goyal 				.salt = sa->salt,
323ec17993aSAkhil Goyal 				.options = { 0 },
3240f56ca1aSHemant Agrawal 				.replay_win_sz = 0,
325ec17993aSAkhil Goyal 				.direction = sa->direction,
326ec17993aSAkhil Goyal 				.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
327b1a3ac78SMariusz Drost 				.mode = (IS_TUNNEL(sa->flags)) ?
328ec17993aSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
329ec17993aSAkhil Goyal 					RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
330376ee1deSRadu Nicolau 			} },
3310ccfd14bSAnoob Joseph 			.crypto_xform = sa->xforms,
3320ccfd14bSAnoob Joseph 			.userdata = NULL,
333ec17993aSAkhil Goyal 
334ec17993aSAkhil Goyal 		};
335ec17993aSAkhil Goyal 
3364a67af84SMarcin Smoczynski 		if (ips->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
33779bdb787SAkhil Goyal 			void *ctx = rte_cryptodev_get_sec_ctx(cdev_id);
338ec17993aSAkhil Goyal 
3390ccfd14bSAnoob Joseph 			/* Set IPsec parameters in conf */
3400ccfd14bSAnoob Joseph 			set_ipsec_conf(sa, &(sess_conf.ipsec));
341ec17993aSAkhil Goyal 
3428a77c1b7SAkhil Goyal 			if (verify_security_capabilities(ctx, &sess_conf, NULL)) {
343a8781df8SAkhil Goyal 				RTE_LOG(ERR, IPSEC,
344a8781df8SAkhil Goyal 					"Requested security session config not supported\n");
345a8781df8SAkhil Goyal 				return -1;
346a8781df8SAkhil Goyal 			}
347a8781df8SAkhil Goyal 
3484a67af84SMarcin Smoczynski 			ips->security.ses = rte_security_session_create(ctx,
3493f3fc330SAkhil Goyal 					&sess_conf, skt_ctx->session_pool);
3504a67af84SMarcin Smoczynski 			if (ips->security.ses == NULL) {
351ec17993aSAkhil Goyal 				RTE_LOG(ERR, IPSEC,
352ec17993aSAkhil Goyal 				"SEC Session init failed: err: %d\n", ret);
353ec17993aSAkhil Goyal 				return -1;
354ec17993aSAkhil Goyal 			}
3558e814e18SVolodymyr Fialko 			ips->security.ctx = ctx;
3566938fc92SVolodymyr Fialko 
3576938fc92SVolodymyr Fialko 			sess = ips->security.ses;
3586938fc92SVolodymyr Fialko 			op_type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
3596938fc92SVolodymyr Fialko 			sess_type = RTE_CRYPTO_OP_SECURITY_SESSION;
3603a690d5aSBernard Iremonger 		} else {
3613a690d5aSBernard Iremonger 			RTE_LOG(ERR, IPSEC, "Inline not supported\n");
3623a690d5aSBernard Iremonger 			return -1;
3633a690d5aSBernard Iremonger 		}
3643a690d5aSBernard Iremonger 	} else {
3655139d5d9SMarcin Smoczynski 		struct rte_cryptodev_info info;
3665139d5d9SMarcin Smoczynski 
3675139d5d9SMarcin Smoczynski 		rte_cryptodev_info_get(cdev_id, &info);
368a8781df8SAkhil Goyal 
369a8781df8SAkhil Goyal 		if (ips->type == RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) {
3705139d5d9SMarcin Smoczynski 			if (!(info.feature_flags &
3715139d5d9SMarcin Smoczynski 				RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO))
3725139d5d9SMarcin Smoczynski 				return -ENOTSUP;
3735139d5d9SMarcin Smoczynski 
3745139d5d9SMarcin Smoczynski 		}
375a8781df8SAkhil Goyal 
376a8781df8SAkhil Goyal 		if (verify_crypto_capabilities(info.capabilities, sa->xforms)) {
377a8781df8SAkhil Goyal 			RTE_LOG(ERR, IPSEC,
378a8781df8SAkhil Goyal 				"Requested crypto session config not supported\n");
379a8781df8SAkhil Goyal 			return -1;
380a8781df8SAkhil Goyal 		}
381a8781df8SAkhil Goyal 
3828e814e18SVolodymyr Fialko 		ips->crypto.dev_id = cdev_id;
383bdce2564SAkhil Goyal 		ips->crypto.ses = rte_cryptodev_sym_session_create(cdev_id,
384bdce2564SAkhil Goyal 				sa->xforms, skt_ctx->session_pool);
3853a690d5aSBernard Iremonger 
3868e814e18SVolodymyr Fialko 		rte_cryptodev_info_get(cdev_id, &cdev_info);
3873a690d5aSBernard Iremonger 	}
3883a690d5aSBernard Iremonger 
3896938fc92SVolodymyr Fialko 	/* Setup meta data required by event crypto adapter */
3906938fc92SVolodymyr Fialko 	if (em_conf->enable_event_crypto_adapter && sess != NULL) {
3916938fc92SVolodymyr Fialko 		union rte_event_crypto_metadata m_data;
3926938fc92SVolodymyr Fialko 		const struct eventdev_params *eventdev_conf;
3936938fc92SVolodymyr Fialko 
3946938fc92SVolodymyr Fialko 		eventdev_conf = &(em_conf->eventdev_config[0]);
3956938fc92SVolodymyr Fialko 		memset(&m_data, 0, sizeof(m_data));
3966938fc92SVolodymyr Fialko 
3976938fc92SVolodymyr Fialko 		/* Fill in response information */
3986938fc92SVolodymyr Fialko 		m_data.response_info.sched_type = em_conf->ext_params.sched_type;
3996938fc92SVolodymyr Fialko 		m_data.response_info.op = RTE_EVENT_OP_NEW;
4006938fc92SVolodymyr Fialko 		m_data.response_info.queue_id = eventdev_conf->ev_cpt_queue_id;
4016938fc92SVolodymyr Fialko 
4026938fc92SVolodymyr Fialko 		/* Fill in request information */
4036938fc92SVolodymyr Fialko 		m_data.request_info.cdev_id = cdev_id;
4046938fc92SVolodymyr Fialko 		m_data.request_info.queue_pair_id = 0;
4056938fc92SVolodymyr Fialko 
4066938fc92SVolodymyr Fialko 		/* Attach meta info to session */
4076938fc92SVolodymyr Fialko 		rte_cryptodev_session_event_mdata_set(cdev_id, sess, op_type,
4086938fc92SVolodymyr Fialko 				sess_type, &m_data, sizeof(m_data));
4096938fc92SVolodymyr Fialko 	}
4106938fc92SVolodymyr Fialko 
4113a690d5aSBernard Iremonger 	return 0;
4123a690d5aSBernard Iremonger }
4133a690d5aSBernard Iremonger 
4143a690d5aSBernard Iremonger int
4154a67af84SMarcin Smoczynski create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa,
4164a67af84SMarcin Smoczynski 		struct rte_ipsec_session *ips)
4173a690d5aSBernard Iremonger {
4183a690d5aSBernard Iremonger 	int32_t ret = 0;
41979bdb787SAkhil Goyal 	void *sec_ctx;
4203a690d5aSBernard Iremonger 	struct rte_security_session_conf sess_conf = {
4214a67af84SMarcin Smoczynski 		.action_type = ips->type,
4223a690d5aSBernard Iremonger 		.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
4233a690d5aSBernard Iremonger 		{.ipsec = {
4243a690d5aSBernard Iremonger 			.spi = sa->spi,
4253a690d5aSBernard Iremonger 			.salt = sa->salt,
4263a690d5aSBernard Iremonger 			.options = { 0 },
4270f56ca1aSHemant Agrawal 			.replay_win_sz = 0,
4283a690d5aSBernard Iremonger 			.direction = sa->direction,
4296019feadSRadu Nicolau 			.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP
4303a690d5aSBernard Iremonger 		} },
4313a690d5aSBernard Iremonger 		.crypto_xform = sa->xforms,
4323a690d5aSBernard Iremonger 		.userdata = NULL,
4333a690d5aSBernard Iremonger 	};
4343a690d5aSBernard Iremonger 
4356019feadSRadu Nicolau 	if (IS_TRANSPORT(sa->flags)) {
4366019feadSRadu Nicolau 		sess_conf.ipsec.mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT;
4376019feadSRadu Nicolau 		if (IS_IP4(sa->flags)) {
4386019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.type =
4396019feadSRadu Nicolau 				RTE_SECURITY_IPSEC_TUNNEL_IPV4;
4406019feadSRadu Nicolau 
4416019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.ipv4.src_ip.s_addr =
4426019feadSRadu Nicolau 				sa->src.ip.ip4;
4436019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.ipv4.dst_ip.s_addr =
4446019feadSRadu Nicolau 				sa->dst.ip.ip4;
4456019feadSRadu Nicolau 		} else if (IS_IP6(sa->flags)) {
4466019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.type =
4476019feadSRadu Nicolau 				RTE_SECURITY_IPSEC_TUNNEL_IPV6;
4486019feadSRadu Nicolau 
449*2ede1422SRobin Jarry 			sess_conf.ipsec.tunnel.ipv6.src_addr = sa->src.ip.ip6;
450*2ede1422SRobin Jarry 			sess_conf.ipsec.tunnel.ipv6.dst_addr = sa->dst.ip.ip6;
4516019feadSRadu Nicolau 		}
4526019feadSRadu Nicolau 	} else if (IS_TUNNEL(sa->flags)) {
4536019feadSRadu Nicolau 		sess_conf.ipsec.mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL;
4546019feadSRadu Nicolau 
4556019feadSRadu Nicolau 		if (IS_IP4(sa->flags)) {
4566019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.type =
4576019feadSRadu Nicolau 				RTE_SECURITY_IPSEC_TUNNEL_IPV4;
4586019feadSRadu Nicolau 
4596019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.ipv4.src_ip.s_addr =
4606019feadSRadu Nicolau 				sa->src.ip.ip4;
4616019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.ipv4.dst_ip.s_addr =
4626019feadSRadu Nicolau 				sa->dst.ip.ip4;
4636019feadSRadu Nicolau 		} else if (IS_IP6(sa->flags)) {
4646019feadSRadu Nicolau 			sess_conf.ipsec.tunnel.type =
4656019feadSRadu Nicolau 				RTE_SECURITY_IPSEC_TUNNEL_IPV6;
4666019feadSRadu Nicolau 
467*2ede1422SRobin Jarry 			sess_conf.ipsec.tunnel.ipv6.src_addr = sa->src.ip.ip6;
468*2ede1422SRobin Jarry 			sess_conf.ipsec.tunnel.ipv6.dst_addr = sa->dst.ip.ip6;
4696019feadSRadu Nicolau 		} else {
4706019feadSRadu Nicolau 			RTE_LOG(ERR, IPSEC, "invalid tunnel type\n");
4716019feadSRadu Nicolau 			return -1;
4726019feadSRadu Nicolau 		}
4736019feadSRadu Nicolau 	}
4746019feadSRadu Nicolau 
4759ae86b4cSRadu Nicolau 	if (sa->udp_encap) {
4769ae86b4cSRadu Nicolau 		sess_conf.ipsec.options.udp_encap = 1;
4779ae86b4cSRadu Nicolau 		sess_conf.ipsec.udp.sport = htons(sa->udp.sport);
4789ae86b4cSRadu Nicolau 		sess_conf.ipsec.udp.dport = htons(sa->udp.dport);
4799ae86b4cSRadu Nicolau 	}
4809ae86b4cSRadu Nicolau 
481560029d5SRadu Nicolau 	if (sa->esn > 0) {
482560029d5SRadu Nicolau 		sess_conf.ipsec.options.esn = 1;
483560029d5SRadu Nicolau 		sess_conf.ipsec.esn.value = sa->esn;
484560029d5SRadu Nicolau 	}
485560029d5SRadu Nicolau 
486560029d5SRadu Nicolau 
4873a690d5aSBernard Iremonger 	RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n",
4883a690d5aSBernard Iremonger 		sa->spi, sa->portid);
4893a690d5aSBernard Iremonger 
4904a67af84SMarcin Smoczynski 	if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
491ec17993aSAkhil Goyal 		struct rte_flow_error err;
492a4677f78SNélio Laranjeiro 		int ret = 0;
493ec17993aSAkhil Goyal 
49479bdb787SAkhil Goyal 		sec_ctx = rte_eth_dev_get_sec_ctx(sa->portid);
4953a690d5aSBernard Iremonger 		if (sec_ctx == NULL) {
4963a690d5aSBernard Iremonger 			RTE_LOG(ERR, IPSEC,
4973a690d5aSBernard Iremonger 				" rte_eth_dev_get_sec_ctx failed\n");
4983a690d5aSBernard Iremonger 			return -1;
4993a690d5aSBernard Iremonger 		}
5003a690d5aSBernard Iremonger 
5018a77c1b7SAkhil Goyal 		if (verify_security_capabilities(sec_ctx, &sess_conf,
5028a77c1b7SAkhil Goyal 					&ips->security.ol_flags)) {
503a8781df8SAkhil Goyal 			RTE_LOG(ERR, IPSEC,
504a8781df8SAkhil Goyal 				"Requested security session config not supported\n");
505a8781df8SAkhil Goyal 			return -1;
506a8781df8SAkhil Goyal 		}
507a8781df8SAkhil Goyal 
5084a67af84SMarcin Smoczynski 		ips->security.ses = rte_security_session_create(sec_ctx,
5093f3fc330SAkhil Goyal 				&sess_conf, skt_ctx->session_pool);
5104a67af84SMarcin Smoczynski 		if (ips->security.ses == NULL) {
511ec17993aSAkhil Goyal 			RTE_LOG(ERR, IPSEC,
512ec17993aSAkhil Goyal 				"SEC Session init failed: err: %d\n", ret);
513ec17993aSAkhil Goyal 			return -1;
514ec17993aSAkhil Goyal 		}
515ec17993aSAkhil Goyal 
5164a67af84SMarcin Smoczynski 		ips->security.ctx = sec_ctx;
517ec17993aSAkhil Goyal 		sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
518ec17993aSAkhil Goyal 
519b1a3ac78SMariusz Drost 		if (IS_IP6(sa->flags)) {
520b1a3ac78SMariusz Drost 			sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
521b1a3ac78SMariusz Drost 			sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
522ec17993aSAkhil Goyal 			sa->pattern[1].spec = &sa->ipv6_spec;
5239ac91e2fSRobin Jarry 			sa->ipv6_spec.hdr.dst_addr = sa->dst.ip.ip6;
5249ac91e2fSRobin Jarry 			sa->ipv6_spec.hdr.src_addr = sa->src.ip.ip6;
525b1a3ac78SMariusz Drost 		} else if (IS_IP4(sa->flags)) {
526b1a3ac78SMariusz Drost 			sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
527b1a3ac78SMariusz Drost 			sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
528ec17993aSAkhil Goyal 			sa->pattern[1].spec = &sa->ipv4_spec;
529b1a3ac78SMariusz Drost 
530ec17993aSAkhil Goyal 			sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
531ec17993aSAkhil Goyal 			sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
532ec17993aSAkhil Goyal 		}
533ec17993aSAkhil Goyal 
5349ae86b4cSRadu Nicolau 		sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
5359ae86b4cSRadu Nicolau 
5369ae86b4cSRadu Nicolau 		if (sa->udp_encap) {
5379ae86b4cSRadu Nicolau 
5389ae86b4cSRadu Nicolau 			sa->udp_spec.hdr.dst_port =
5399ae86b4cSRadu Nicolau 					rte_cpu_to_be_16(sa->udp.dport);
5409ae86b4cSRadu Nicolau 			sa->udp_spec.hdr.src_port =
5419ae86b4cSRadu Nicolau 					rte_cpu_to_be_16(sa->udp.sport);
5429ae86b4cSRadu Nicolau 
5439ae86b4cSRadu Nicolau 			sa->pattern[2].mask = &rte_flow_item_udp_mask;
5449ae86b4cSRadu Nicolau 			sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_UDP;
5459ae86b4cSRadu Nicolau 			sa->pattern[2].spec = &sa->udp_spec;
5469ae86b4cSRadu Nicolau 
5479ae86b4cSRadu Nicolau 			sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_ESP;
5489ae86b4cSRadu Nicolau 			sa->pattern[3].spec = &sa->esp_spec;
5499ae86b4cSRadu Nicolau 			sa->pattern[3].mask = &rte_flow_item_esp_mask;
5509ae86b4cSRadu Nicolau 
5519ae86b4cSRadu Nicolau 			sa->pattern[4].type = RTE_FLOW_ITEM_TYPE_END;
5529ae86b4cSRadu Nicolau 		} else {
553ec17993aSAkhil Goyal 			sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
554ec17993aSAkhil Goyal 			sa->pattern[2].spec = &sa->esp_spec;
555ec17993aSAkhil Goyal 			sa->pattern[2].mask = &rte_flow_item_esp_mask;
556ec17993aSAkhil Goyal 
557ec17993aSAkhil Goyal 			sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
5589ae86b4cSRadu Nicolau 		}
559ec17993aSAkhil Goyal 
560ec17993aSAkhil Goyal 		sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
5614a67af84SMarcin Smoczynski 		sa->action[0].conf = ips->security.ses;
562ec17993aSAkhil Goyal 
563ec17993aSAkhil Goyal 		sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
564ec17993aSAkhil Goyal 
565ec17993aSAkhil Goyal 		sa->attr.egress = (sa->direction ==
566ec17993aSAkhil Goyal 				RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
567a4cde424SNélio Laranjeiro 		sa->attr.ingress = (sa->direction ==
568a4cde424SNélio Laranjeiro 				RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
569a4677f78SNélio Laranjeiro 		if (sa->attr.ingress) {
5706019feadSRadu Nicolau 			uint8_t rss_key[64];
571a4677f78SNélio Laranjeiro 			struct rte_eth_rss_conf rss_conf = {
572a4677f78SNélio Laranjeiro 				.rss_key = rss_key,
5736019feadSRadu Nicolau 				.rss_key_len = sizeof(rss_key),
574a4677f78SNélio Laranjeiro 			};
5750109baf1SMarcin Zapolski 			struct rte_eth_dev_info dev_info;
576a4677f78SNélio Laranjeiro 			uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
57719b3bc47SAdrien Mazarguil 			struct rte_flow_action_rss action_rss;
578a4677f78SNélio Laranjeiro 			unsigned int i;
579a4677f78SNélio Laranjeiro 			unsigned int j;
580a4677f78SNélio Laranjeiro 
581513f192bSAnkur Dwivedi 			/* Don't create flow if default flow is created */
582513f192bSAnkur Dwivedi 			if (flow_info_tbl[sa->portid].rx_def_flow)
583513f192bSAnkur Dwivedi 				return 0;
584513f192bSAnkur Dwivedi 
58503ad0e5cSIvan Ilchenko 			ret = rte_eth_dev_info_get(sa->portid, &dev_info);
58603ad0e5cSIvan Ilchenko 			if (ret != 0) {
58703ad0e5cSIvan Ilchenko 				RTE_LOG(ERR, IPSEC,
58803ad0e5cSIvan Ilchenko 					"Error during getting device (port %u) info: %s\n",
58903ad0e5cSIvan Ilchenko 					sa->portid, strerror(-ret));
59003ad0e5cSIvan Ilchenko 				return ret;
59103ad0e5cSIvan Ilchenko 			}
59203ad0e5cSIvan Ilchenko 
593a4677f78SNélio Laranjeiro 			sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
594a4677f78SNélio Laranjeiro 			/* Try RSS. */
595a4677f78SNélio Laranjeiro 			sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
596a4677f78SNélio Laranjeiro 			sa->action[1].conf = &action_rss;
59723742f21SBernard Iremonger 			ret = rte_eth_dev_rss_hash_conf_get(sa->portid,
59823742f21SBernard Iremonger 					&rss_conf);
59923742f21SBernard Iremonger 			if (ret != 0) {
60023742f21SBernard Iremonger 				RTE_LOG(ERR, IPSEC,
60123742f21SBernard Iremonger 					"rte_eth_dev_rss_hash_conf_get:ret=%d\n",
60223742f21SBernard Iremonger 					ret);
60323742f21SBernard Iremonger 				return -1;
60423742f21SBernard Iremonger 			}
6053a690d5aSBernard Iremonger 			for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i)
60619b3bc47SAdrien Mazarguil 				queue[j++] = i;
6073a690d5aSBernard Iremonger 
608ac8d22deSAdrien Mazarguil 			action_rss = (struct rte_flow_action_rss){
609ac8d22deSAdrien Mazarguil 					.types = rss_conf.rss_hf,
610ac8d22deSAdrien Mazarguil 					.key_len = rss_conf.rss_key_len,
611ac8d22deSAdrien Mazarguil 					.queue_num = j,
612ac8d22deSAdrien Mazarguil 					.key = rss_key,
613ac8d22deSAdrien Mazarguil 					.queue = queue,
614ac8d22deSAdrien Mazarguil 			};
615a4677f78SNélio Laranjeiro 			ret = rte_flow_validate(sa->portid, &sa->attr,
616a4677f78SNélio Laranjeiro 						sa->pattern, sa->action,
617a4677f78SNélio Laranjeiro 						&err);
618a4677f78SNélio Laranjeiro 			if (!ret)
619a4677f78SNélio Laranjeiro 				goto flow_create;
620a4677f78SNélio Laranjeiro 			/* Try Queue. */
621a4677f78SNélio Laranjeiro 			sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
622a4677f78SNélio Laranjeiro 			sa->action[1].conf =
623a4677f78SNélio Laranjeiro 				&(struct rte_flow_action_queue){
624a4677f78SNélio Laranjeiro 				.index = 0,
625a4677f78SNélio Laranjeiro 			};
626a4677f78SNélio Laranjeiro 			ret = rte_flow_validate(sa->portid, &sa->attr,
627a4677f78SNélio Laranjeiro 						sa->pattern, sa->action,
628a4677f78SNélio Laranjeiro 						&err);
6296138c2daSRadu Nicolau 			/* Try End. */
6306138c2daSRadu Nicolau 			sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
6316138c2daSRadu Nicolau 			sa->action[1].conf = NULL;
6326138c2daSRadu Nicolau 			ret = rte_flow_validate(sa->portid, &sa->attr,
6336138c2daSRadu Nicolau 						sa->pattern, sa->action,
6346138c2daSRadu Nicolau 						&err);
635a4677f78SNélio Laranjeiro 			if (ret)
636a4677f78SNélio Laranjeiro 				goto flow_create_failure;
637a90e6ce6SNélio Laranjeiro 		} else if (sa->attr.egress &&
6384a67af84SMarcin Smoczynski 				(ips->security.ol_flags &
639a90e6ce6SNélio Laranjeiro 					RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
640a90e6ce6SNélio Laranjeiro 			sa->action[1].type =
641a90e6ce6SNélio Laranjeiro 					RTE_FLOW_ACTION_TYPE_PASSTHRU;
642a90e6ce6SNélio Laranjeiro 			sa->action[2].type =
643a90e6ce6SNélio Laranjeiro 					RTE_FLOW_ACTION_TYPE_END;
644a4677f78SNélio Laranjeiro 		}
645a4677f78SNélio Laranjeiro flow_create:
646ec17993aSAkhil Goyal 		sa->flow = rte_flow_create(sa->portid,
647ec17993aSAkhil Goyal 				&sa->attr, sa->pattern, sa->action, &err);
648ec17993aSAkhil Goyal 		if (sa->flow == NULL) {
649a4677f78SNélio Laranjeiro flow_create_failure:
650ec17993aSAkhil Goyal 			RTE_LOG(ERR, IPSEC,
651ec17993aSAkhil Goyal 				"Failed to create ipsec flow msg: %s\n",
652ec17993aSAkhil Goyal 				err.message);
653ec17993aSAkhil Goyal 			return -1;
654ec17993aSAkhil Goyal 		}
6554a67af84SMarcin Smoczynski 	} else if (ips->type ==	RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
65679bdb787SAkhil Goyal 		sec_ctx = rte_eth_dev_get_sec_ctx(sa->portid);
6573a690d5aSBernard Iremonger 
6583a690d5aSBernard Iremonger 		if (sec_ctx == NULL) {
6590ccfd14bSAnoob Joseph 			RTE_LOG(ERR, IPSEC,
6600ccfd14bSAnoob Joseph 				"Ethernet device doesn't have security features registered\n");
6610ccfd14bSAnoob Joseph 			return -1;
6620ccfd14bSAnoob Joseph 		}
6630ccfd14bSAnoob Joseph 
6640ccfd14bSAnoob Joseph 		/* Set IPsec parameters in conf */
6650ccfd14bSAnoob Joseph 		set_ipsec_conf(sa, &(sess_conf.ipsec));
6660ccfd14bSAnoob Joseph 
6670ccfd14bSAnoob Joseph 		/* Save SA as userdata for the security session. When
6680ccfd14bSAnoob Joseph 		 * the packet is received, this userdata will be
6690ccfd14bSAnoob Joseph 		 * retrieved using the metadata from the packet.
6700ccfd14bSAnoob Joseph 		 *
671fa4de2ccSAnoob Joseph 		 * The PMD is expected to set similar metadata for other
672fa4de2ccSAnoob Joseph 		 * operations, like rte_eth_event, which are tied to
673fa4de2ccSAnoob Joseph 		 * security session. In such cases, the userdata could
674fa4de2ccSAnoob Joseph 		 * be obtained to uniquely identify the security
675fa4de2ccSAnoob Joseph 		 * parameters denoted.
6760ccfd14bSAnoob Joseph 		 */
6770ccfd14bSAnoob Joseph 
6780ccfd14bSAnoob Joseph 		sess_conf.userdata = (void *) sa;
6790ccfd14bSAnoob Joseph 
6808a77c1b7SAkhil Goyal 		if (verify_security_capabilities(sec_ctx, &sess_conf,
6818a77c1b7SAkhil Goyal 					&ips->security.ol_flags)) {
682a8781df8SAkhil Goyal 			RTE_LOG(ERR, IPSEC,
683a8781df8SAkhil Goyal 				"Requested security session config not supported\n");
684a8781df8SAkhil Goyal 			return -1;
685a8781df8SAkhil Goyal 		}
686a8781df8SAkhil Goyal 
6874a67af84SMarcin Smoczynski 		ips->security.ses = rte_security_session_create(sec_ctx,
6883f3fc330SAkhil Goyal 					&sess_conf, skt_ctx->session_pool);
6894a67af84SMarcin Smoczynski 		if (ips->security.ses == NULL) {
6900ccfd14bSAnoob Joseph 			RTE_LOG(ERR, IPSEC,
6910ccfd14bSAnoob Joseph 				"SEC Session init failed: err: %d\n", ret);
6920ccfd14bSAnoob Joseph 			return -1;
6930ccfd14bSAnoob Joseph 		}
6940ccfd14bSAnoob Joseph 
6954a67af84SMarcin Smoczynski 		ips->security.ctx = sec_ctx;
696ec17993aSAkhil Goyal 	}
697d299106eSSergio Gonzalez Monroy 
698d299106eSSergio Gonzalez Monroy 	return 0;
699d299106eSSergio Gonzalez Monroy }
700d299106eSSergio Gonzalez Monroy 
7016738c0a9SPraveen Shetty int
7026738c0a9SPraveen Shetty create_ipsec_esp_flow(struct ipsec_sa *sa)
7036738c0a9SPraveen Shetty {
7046738c0a9SPraveen Shetty 	int ret = 0;
7050b512a92SVolodymyr Fialko 	struct rte_flow_error err = {};
7066738c0a9SPraveen Shetty 	if (sa->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
7076738c0a9SPraveen Shetty 		RTE_LOG(ERR, IPSEC,
7086738c0a9SPraveen Shetty 			"No Flow director rule for Egress traffic\n");
7096738c0a9SPraveen Shetty 		return -1;
7106738c0a9SPraveen Shetty 	}
7116738c0a9SPraveen Shetty 	if (sa->flags == TRANSPORT) {
7126738c0a9SPraveen Shetty 		RTE_LOG(ERR, IPSEC,
7136738c0a9SPraveen Shetty 			"No Flow director rule for transport mode\n");
7146738c0a9SPraveen Shetty 		return -1;
7156738c0a9SPraveen Shetty 	}
7166738c0a9SPraveen Shetty 	sa->action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
7176738c0a9SPraveen Shetty 	sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
7186738c0a9SPraveen Shetty 	sa->action[0].conf = &(struct rte_flow_action_queue) {
7196738c0a9SPraveen Shetty 				.index = sa->fdir_qid,
7206738c0a9SPraveen Shetty 	};
7216738c0a9SPraveen Shetty 	sa->attr.egress = 0;
7226738c0a9SPraveen Shetty 	sa->attr.ingress = 1;
7236738c0a9SPraveen Shetty 	if (IS_IP6(sa->flags)) {
7246738c0a9SPraveen Shetty 		sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
7256738c0a9SPraveen Shetty 		sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
7266738c0a9SPraveen Shetty 		sa->pattern[1].spec = &sa->ipv6_spec;
7279ac91e2fSRobin Jarry 		sa->ipv6_spec.hdr.dst_addr = sa->dst.ip.ip6;
7289ac91e2fSRobin Jarry 		sa->ipv6_spec.hdr.src_addr = sa->src.ip.ip6;
7296738c0a9SPraveen Shetty 		sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
7306738c0a9SPraveen Shetty 		sa->pattern[2].spec = &sa->esp_spec;
7316738c0a9SPraveen Shetty 		sa->pattern[2].mask = &rte_flow_item_esp_mask;
7326738c0a9SPraveen Shetty 		sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
7336738c0a9SPraveen Shetty 		sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
7346738c0a9SPraveen Shetty 	} else if (IS_IP4(sa->flags)) {
7356738c0a9SPraveen Shetty 		sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
7366738c0a9SPraveen Shetty 		sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
7376738c0a9SPraveen Shetty 		sa->pattern[1].spec = &sa->ipv4_spec;
7386738c0a9SPraveen Shetty 		sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
7396738c0a9SPraveen Shetty 		sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
7406738c0a9SPraveen Shetty 		sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
7416738c0a9SPraveen Shetty 		sa->pattern[2].spec = &sa->esp_spec;
7426738c0a9SPraveen Shetty 		sa->pattern[2].mask = &rte_flow_item_esp_mask;
7436738c0a9SPraveen Shetty 		sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
7446738c0a9SPraveen Shetty 		sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
7456738c0a9SPraveen Shetty 	}
7466738c0a9SPraveen Shetty 	sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
7476738c0a9SPraveen Shetty 
7486738c0a9SPraveen Shetty 	ret = rte_flow_validate(sa->portid, &sa->attr, sa->pattern, sa->action,
7496738c0a9SPraveen Shetty 				&err);
7506738c0a9SPraveen Shetty 	if (ret < 0) {
7516738c0a9SPraveen Shetty 		RTE_LOG(ERR, IPSEC, "Flow validation failed %s\n", err.message);
7526738c0a9SPraveen Shetty 		return ret;
7536738c0a9SPraveen Shetty 	}
7546738c0a9SPraveen Shetty 
7556738c0a9SPraveen Shetty 	sa->flow = rte_flow_create(sa->portid, &sa->attr, sa->pattern,
7566738c0a9SPraveen Shetty 					sa->action, &err);
7576738c0a9SPraveen Shetty 	if (!sa->flow) {
7586738c0a9SPraveen Shetty 		RTE_LOG(ERR, IPSEC, "Flow creation failed %s\n", err.message);
7596738c0a9SPraveen Shetty 		return -1;
7606738c0a9SPraveen Shetty 	}
7616738c0a9SPraveen Shetty 
7626738c0a9SPraveen Shetty 	return 0;
7636738c0a9SPraveen Shetty }
7646738c0a9SPraveen Shetty 
765d87152e7SKonstantin Ananyev /*
766d87152e7SKonstantin Ananyev  * queue crypto-ops into PMD queue.
767d87152e7SKonstantin Ananyev  */
768d87152e7SKonstantin Ananyev void
769d87152e7SKonstantin Ananyev enqueue_cop_burst(struct cdev_qp *cqp)
770d299106eSSergio Gonzalez Monroy {
771d87152e7SKonstantin Ananyev 	uint32_t i, len, ret;
772d299106eSSergio Gonzalez Monroy 
773d87152e7SKonstantin Ananyev 	len = cqp->len;
774d87152e7SKonstantin Ananyev 	ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
775d87152e7SKonstantin Ananyev 	if (ret < len) {
7765d8f0bafSOlivier Matz 		RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
777d299106eSSergio Gonzalez Monroy 			" enqueued %u crypto ops out of %u\n",
778d87152e7SKonstantin Ananyev 			cqp->id, cqp->qp, ret, len);
779d87152e7SKonstantin Ananyev 			/* drop packets that we fail to enqueue */
780d87152e7SKonstantin Ananyev 			for (i = ret; i < len; i++)
7811329602bSAnoob Joseph 				free_pkts(&cqp->buf[i]->sym->m_src, 1);
782d299106eSSergio Gonzalez Monroy 	}
783d299106eSSergio Gonzalez Monroy 	cqp->in_flight += ret;
784d299106eSSergio Gonzalez Monroy 	cqp->len = 0;
785d299106eSSergio Gonzalez Monroy }
786d87152e7SKonstantin Ananyev 
787d87152e7SKonstantin Ananyev static inline void
788d87152e7SKonstantin Ananyev enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
789d87152e7SKonstantin Ananyev {
790d87152e7SKonstantin Ananyev 	cqp->buf[cqp->len++] = cop;
791d87152e7SKonstantin Ananyev 
792d87152e7SKonstantin Ananyev 	if (cqp->len == MAX_PKT_BURST)
793d87152e7SKonstantin Ananyev 		enqueue_cop_burst(cqp);
794d299106eSSergio Gonzalez Monroy }
795d299106eSSergio Gonzalez Monroy 
796c64278c0SSergio Gonzalez Monroy static inline void
797c64278c0SSergio Gonzalez Monroy ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
798ba66534fSMarcin Smoczynski 		struct rte_mbuf *pkts[], void *sas[],
799c64278c0SSergio Gonzalez Monroy 		uint16_t nb_pkts)
800d299106eSSergio Gonzalez Monroy {
801906257e9SSergio Gonzalez Monroy 	int32_t ret = 0, i;
802d299106eSSergio Gonzalez Monroy 	struct ipsec_mbuf_metadata *priv;
803ec17993aSAkhil Goyal 	struct rte_crypto_sym_op *sym_cop;
804d299106eSSergio Gonzalez Monroy 	struct ipsec_sa *sa;
8054a67af84SMarcin Smoczynski 	struct rte_ipsec_session *ips;
806d299106eSSergio Gonzalez Monroy 
807d299106eSSergio Gonzalez Monroy 	for (i = 0; i < nb_pkts; i++) {
80885f84767SSergio Gonzalez Monroy 		if (unlikely(sas[i] == NULL)) {
8091329602bSAnoob Joseph 			free_pkts(&pkts[i], 1);
81085f84767SSergio Gonzalez Monroy 			continue;
81185f84767SSergio Gonzalez Monroy 		}
81285f84767SSergio Gonzalez Monroy 
813d299106eSSergio Gonzalez Monroy 		rte_prefetch0(sas[i]);
814d299106eSSergio Gonzalez Monroy 		rte_prefetch0(pkts[i]);
815d299106eSSergio Gonzalez Monroy 
816d299106eSSergio Gonzalez Monroy 		priv = get_priv(pkts[i]);
817ba66534fSMarcin Smoczynski 		sa = ipsec_mask_saptr(sas[i]);
818d299106eSSergio Gonzalez Monroy 		priv->sa = sa;
819ba66534fSMarcin Smoczynski 		ips = ipsec_get_primary_session(sa);
820d299106eSSergio Gonzalez Monroy 
8214a67af84SMarcin Smoczynski 		switch (ips->type) {
822ec17993aSAkhil Goyal 		case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
823ec17993aSAkhil Goyal 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
824ec17993aSAkhil Goyal 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
825ec17993aSAkhil Goyal 
826ec17993aSAkhil Goyal 			rte_prefetch0(&priv->sym_cop);
827ec17993aSAkhil Goyal 
828a8ade121SVolodymyr Fialko 			if (unlikely(ips->security.ses == NULL)) {
8291329602bSAnoob Joseph 				free_pkts(&pkts[i], 1);
830ec17993aSAkhil Goyal 				continue;
831ec17993aSAkhil Goyal 			}
832ec17993aSAkhil Goyal 
8339a1cc8f1STejasree Kondoj 			if (unlikely((pkts[i]->packet_type &
8349a1cc8f1STejasree Kondoj 					(RTE_PTYPE_TUNNEL_MASK |
8359a1cc8f1STejasree Kondoj 					RTE_PTYPE_L4_MASK)) ==
8369a1cc8f1STejasree Kondoj 					MBUF_PTYPE_TUNNEL_ESP_IN_UDP &&
8379a1cc8f1STejasree Kondoj 					sa->udp_encap != 1)) {
8389a1cc8f1STejasree Kondoj 				free_pkts(&pkts[i], 1);
8399a1cc8f1STejasree Kondoj 				continue;
8409a1cc8f1STejasree Kondoj 			}
8419a1cc8f1STejasree Kondoj 
842ec17993aSAkhil Goyal 			sym_cop = get_sym_cop(&priv->cop);
843ec17993aSAkhil Goyal 			sym_cop->m_src = pkts[i];
844ec17993aSAkhil Goyal 
845ec17993aSAkhil Goyal 			rte_security_attach_session(&priv->cop,
8464a67af84SMarcin Smoczynski 				ips->security.ses);
847ec17993aSAkhil Goyal 			break;
8485139d5d9SMarcin Smoczynski 
8495139d5d9SMarcin Smoczynski 		case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
8505139d5d9SMarcin Smoczynski 			RTE_LOG(ERR, IPSEC, "CPU crypto is not supported by the"
8515139d5d9SMarcin Smoczynski 					" legacy mode.");
8521329602bSAnoob Joseph 			free_pkts(&pkts[i], 1);
8535139d5d9SMarcin Smoczynski 			continue;
8545139d5d9SMarcin Smoczynski 
855ec17993aSAkhil Goyal 		case RTE_SECURITY_ACTION_TYPE_NONE:
856ec17993aSAkhil Goyal 
857d299106eSSergio Gonzalez Monroy 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
858a60c05b5SSergio Gonzalez Monroy 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
859d299106eSSergio Gonzalez Monroy 
860d299106eSSergio Gonzalez Monroy 			rte_prefetch0(&priv->sym_cop);
861d299106eSSergio Gonzalez Monroy 
862a8ade121SVolodymyr Fialko 			if (unlikely(ips->crypto.ses == NULL)) {
8631329602bSAnoob Joseph 				free_pkts(&pkts[i], 1);
864d299106eSSergio Gonzalez Monroy 				continue;
865d299106eSSergio Gonzalez Monroy 			}
866d299106eSSergio Gonzalez Monroy 
867d299106eSSergio Gonzalez Monroy 			rte_crypto_op_attach_sym_session(&priv->cop,
8684a67af84SMarcin Smoczynski 					ips->crypto.ses);
869d299106eSSergio Gonzalez Monroy 
870c64278c0SSergio Gonzalez Monroy 			ret = xform_func(pkts[i], sa, &priv->cop);
871d299106eSSergio Gonzalez Monroy 			if (unlikely(ret)) {
8721329602bSAnoob Joseph 				free_pkts(&pkts[i], 1);
873d299106eSSergio Gonzalez Monroy 				continue;
874d299106eSSergio Gonzalez Monroy 			}
875ec17993aSAkhil Goyal 			break;
876ec17993aSAkhil Goyal 		case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
8774a67af84SMarcin Smoczynski 			RTE_ASSERT(ips->security.ses != NULL);
8783da37f68SRadu Nicolau 			ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
8794a67af84SMarcin Smoczynski 			if (ips->security.ol_flags &
8804a67af84SMarcin Smoczynski 				RTE_SECURITY_TX_OLOAD_NEED_MDATA)
8810ccfd14bSAnoob Joseph 				rte_security_set_pkt_metadata(
8824a67af84SMarcin Smoczynski 					ips->security.ctx, ips->security.ses,
8834a67af84SMarcin Smoczynski 					pkts[i], NULL);
8840ccfd14bSAnoob Joseph 			continue;
885ec17993aSAkhil Goyal 		case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
8864a67af84SMarcin Smoczynski 			RTE_ASSERT(ips->security.ses != NULL);
887ec17993aSAkhil Goyal 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
888ec17993aSAkhil Goyal 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
889ec17993aSAkhil Goyal 
890ec17993aSAkhil Goyal 			rte_prefetch0(&priv->sym_cop);
891ec17993aSAkhil Goyal 			rte_security_attach_session(&priv->cop,
8924a67af84SMarcin Smoczynski 					ips->security.ses);
893ec17993aSAkhil Goyal 
894ec17993aSAkhil Goyal 			ret = xform_func(pkts[i], sa, &priv->cop);
895ec17993aSAkhil Goyal 			if (unlikely(ret)) {
8961329602bSAnoob Joseph 				free_pkts(&pkts[i], 1);
897ec17993aSAkhil Goyal 				continue;
898ec17993aSAkhil Goyal 			}
899ec17993aSAkhil Goyal 
9003da37f68SRadu Nicolau 			ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
9014a67af84SMarcin Smoczynski 			if (ips->security.ol_flags &
9024a67af84SMarcin Smoczynski 				RTE_SECURITY_TX_OLOAD_NEED_MDATA)
903ec17993aSAkhil Goyal 				rte_security_set_pkt_metadata(
9044a67af84SMarcin Smoczynski 					ips->security.ctx, ips->security.ses,
9054a67af84SMarcin Smoczynski 					pkts[i], NULL);
906ec17993aSAkhil Goyal 			continue;
907ec17993aSAkhil Goyal 		}
908d299106eSSergio Gonzalez Monroy 
909f406064fSRadu Nicolau 		RTE_ASSERT(sa->cqp[ipsec_ctx->lcore_id] != NULL);
910a8ade121SVolodymyr Fialko 		enqueue_cop(sa->cqp[ipsec_ctx->lcore_id], &priv->cop);
911d299106eSSergio Gonzalez Monroy 	}
912c64278c0SSergio Gonzalez Monroy }
913d299106eSSergio Gonzalez Monroy 
914d87152e7SKonstantin Ananyev static inline int32_t
915d87152e7SKonstantin Ananyev ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
916d87152e7SKonstantin Ananyev 	      struct rte_mbuf *pkts[], uint16_t max_pkts)
917d87152e7SKonstantin Ananyev {
918d87152e7SKonstantin Ananyev 	int32_t nb_pkts, ret;
919d87152e7SKonstantin Ananyev 	struct ipsec_mbuf_metadata *priv;
920d87152e7SKonstantin Ananyev 	struct ipsec_sa *sa;
921d87152e7SKonstantin Ananyev 	struct rte_mbuf *pkt;
922d87152e7SKonstantin Ananyev 
923d87152e7SKonstantin Ananyev 	nb_pkts = 0;
924d87152e7SKonstantin Ananyev 	while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
925d87152e7SKonstantin Ananyev 		pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
926d87152e7SKonstantin Ananyev 		rte_prefetch0(pkt);
927d87152e7SKonstantin Ananyev 		priv = get_priv(pkt);
928d87152e7SKonstantin Ananyev 		sa = priv->sa;
929d87152e7SKonstantin Ananyev 		ret = xform_func(pkt, sa, &priv->cop);
930d87152e7SKonstantin Ananyev 		if (unlikely(ret)) {
9311329602bSAnoob Joseph 			free_pkts(&pkt, 1);
932d87152e7SKonstantin Ananyev 			continue;
933d87152e7SKonstantin Ananyev 		}
934d87152e7SKonstantin Ananyev 		pkts[nb_pkts++] = pkt;
935d87152e7SKonstantin Ananyev 	}
936d87152e7SKonstantin Ananyev 
937d87152e7SKonstantin Ananyev 	return nb_pkts;
938d87152e7SKonstantin Ananyev }
939d87152e7SKonstantin Ananyev 
940c64278c0SSergio Gonzalez Monroy static inline int
941c64278c0SSergio Gonzalez Monroy ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
942c64278c0SSergio Gonzalez Monroy 	      struct rte_mbuf *pkts[], uint16_t max_pkts)
943c64278c0SSergio Gonzalez Monroy {
944906257e9SSergio Gonzalez Monroy 	int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
945c64278c0SSergio Gonzalez Monroy 	struct ipsec_mbuf_metadata *priv;
946c64278c0SSergio Gonzalez Monroy 	struct rte_crypto_op *cops[max_pkts];
947c64278c0SSergio Gonzalez Monroy 	struct ipsec_sa *sa;
948c64278c0SSergio Gonzalez Monroy 	struct rte_mbuf *pkt;
949c64278c0SSergio Gonzalez Monroy 
950833e36b8SRadu Nicolau 	for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
951d299106eSSergio Gonzalez Monroy 		struct cdev_qp *cqp;
952833e36b8SRadu Nicolau 
953833e36b8SRadu Nicolau 		cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
954833e36b8SRadu Nicolau 		if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
955833e36b8SRadu Nicolau 			ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
956d299106eSSergio Gonzalez Monroy 
957833e36b8SRadu Nicolau 		if (cqp->in_flight == 0)
958d299106eSSergio Gonzalez Monroy 			continue;
959d299106eSSergio Gonzalez Monroy 
960d299106eSSergio Gonzalez Monroy 		nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
961d299106eSSergio Gonzalez Monroy 				cops, max_pkts - nb_pkts);
962d299106eSSergio Gonzalez Monroy 
963d299106eSSergio Gonzalez Monroy 		cqp->in_flight -= nb_cops;
964d299106eSSergio Gonzalez Monroy 
965d299106eSSergio Gonzalez Monroy 		for (j = 0; j < nb_cops; j++) {
966d299106eSSergio Gonzalez Monroy 			pkt = cops[j]->sym->m_src;
967d299106eSSergio Gonzalez Monroy 			rte_prefetch0(pkt);
968d299106eSSergio Gonzalez Monroy 
969d299106eSSergio Gonzalez Monroy 			priv = get_priv(pkt);
970d299106eSSergio Gonzalez Monroy 			sa = priv->sa;
971d299106eSSergio Gonzalez Monroy 
97250705e8eSThomas Monjalon 			RTE_ASSERT(sa != NULL);
973d299106eSSergio Gonzalez Monroy 
9744a67af84SMarcin Smoczynski 			if (ipsec_get_action_type(sa) ==
9754a67af84SMarcin Smoczynski 				RTE_SECURITY_ACTION_TYPE_NONE) {
976c64278c0SSergio Gonzalez Monroy 				ret = xform_func(pkt, sa, cops[j]);
977ec17993aSAkhil Goyal 				if (unlikely(ret)) {
9781329602bSAnoob Joseph 					free_pkts(&pkt, 1);
979ec17993aSAkhil Goyal 					continue;
980ec17993aSAkhil Goyal 				}
9814a67af84SMarcin Smoczynski 			} else if (ipsec_get_action_type(sa) ==
98274ac7558SMichael Shamis 				RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
98374ac7558SMichael Shamis 				if (cops[j]->status) {
9841329602bSAnoob Joseph 					free_pkts(&pkt, 1);
98574ac7558SMichael Shamis 					continue;
98674ac7558SMichael Shamis 				}
987ec17993aSAkhil Goyal 			}
988d299106eSSergio Gonzalez Monroy 			pkts[nb_pkts++] = pkt;
989d299106eSSergio Gonzalez Monroy 		}
990d299106eSSergio Gonzalez Monroy 	}
991d299106eSSergio Gonzalez Monroy 
992d299106eSSergio Gonzalez Monroy 	/* return packets */
993d299106eSSergio Gonzalez Monroy 	return nb_pkts;
994d299106eSSergio Gonzalez Monroy }
995d299106eSSergio Gonzalez Monroy 
996d299106eSSergio Gonzalez Monroy uint16_t
997d299106eSSergio Gonzalez Monroy ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
998d299106eSSergio Gonzalez Monroy 		uint16_t nb_pkts, uint16_t len)
999d299106eSSergio Gonzalez Monroy {
1000ba66534fSMarcin Smoczynski 	void *sas[nb_pkts];
1001d299106eSSergio Gonzalez Monroy 
1002d299106eSSergio Gonzalez Monroy 	inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
1003d299106eSSergio Gonzalez Monroy 
1004c64278c0SSergio Gonzalez Monroy 	ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
1005c64278c0SSergio Gonzalez Monroy 
1006d87152e7SKonstantin Ananyev 	return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
1007d87152e7SKonstantin Ananyev }
1008d87152e7SKonstantin Ananyev 
1009d87152e7SKonstantin Ananyev uint16_t
1010d87152e7SKonstantin Ananyev ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
1011d87152e7SKonstantin Ananyev 		uint16_t len)
1012d87152e7SKonstantin Ananyev {
1013c64278c0SSergio Gonzalez Monroy 	return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
1014d299106eSSergio Gonzalez Monroy }
1015d299106eSSergio Gonzalez Monroy 
1016d299106eSSergio Gonzalez Monroy uint16_t
1017d299106eSSergio Gonzalez Monroy ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
1018d299106eSSergio Gonzalez Monroy 		uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
1019d299106eSSergio Gonzalez Monroy {
1020ba66534fSMarcin Smoczynski 	void *sas[nb_pkts];
1021d299106eSSergio Gonzalez Monroy 
1022d299106eSSergio Gonzalez Monroy 	outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
1023d299106eSSergio Gonzalez Monroy 
1024c64278c0SSergio Gonzalez Monroy 	ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
1025c64278c0SSergio Gonzalez Monroy 
1026d87152e7SKonstantin Ananyev 	return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
1027d87152e7SKonstantin Ananyev }
1028d87152e7SKonstantin Ananyev 
1029d87152e7SKonstantin Ananyev uint16_t
1030d87152e7SKonstantin Ananyev ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
1031d87152e7SKonstantin Ananyev 		uint16_t len)
1032d87152e7SKonstantin Ananyev {
1033c64278c0SSergio Gonzalez Monroy 	return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);
1034d299106eSSergio Gonzalez Monroy }
1035