xref: /f-stack/dpdk/examples/ipsec-secgw/ipsec.c (revision 2d9fd380)
1d30ea906Sjfb8856606 /* SPDX-License-Identifier: BSD-3-Clause
2*2d9fd380Sjfb8856606  * Copyright(c) 2016-2020 Intel Corporation
3a9643ea8Slogwang  */
4a9643ea8Slogwang #include <sys/types.h>
5a9643ea8Slogwang #include <netinet/in.h>
6a9643ea8Slogwang #include <netinet/ip.h>
7a9643ea8Slogwang 
8a9643ea8Slogwang #include <rte_branch_prediction.h>
9a9643ea8Slogwang #include <rte_log.h>
10a9643ea8Slogwang #include <rte_crypto.h>
112bfe3f2eSlogwang #include <rte_security.h>
12a9643ea8Slogwang #include <rte_cryptodev.h>
13*2d9fd380Sjfb8856606 #include <rte_ipsec.h>
142bfe3f2eSlogwang #include <rte_ethdev.h>
15a9643ea8Slogwang #include <rte_mbuf.h>
16a9643ea8Slogwang #include <rte_hash.h>
17a9643ea8Slogwang 
18a9643ea8Slogwang #include "ipsec.h"
19a9643ea8Slogwang #include "esp.h"
20a9643ea8Slogwang 
21d30ea906Sjfb8856606 static inline void
set_ipsec_conf(struct ipsec_sa * sa,struct rte_security_ipsec_xform * ipsec)22d30ea906Sjfb8856606 set_ipsec_conf(struct ipsec_sa *sa, struct rte_security_ipsec_xform *ipsec)
23d30ea906Sjfb8856606 {
24d30ea906Sjfb8856606 	if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
25d30ea906Sjfb8856606 		struct rte_security_ipsec_tunnel_param *tunnel =
26d30ea906Sjfb8856606 				&ipsec->tunnel;
274b05018fSfengbojiang 		if (IS_IP4_TUNNEL(sa->flags)) {
28d30ea906Sjfb8856606 			tunnel->type =
29d30ea906Sjfb8856606 				RTE_SECURITY_IPSEC_TUNNEL_IPV4;
30d30ea906Sjfb8856606 			tunnel->ipv4.ttl = IPDEFTTL;
31d30ea906Sjfb8856606 
32d30ea906Sjfb8856606 			memcpy((uint8_t *)&tunnel->ipv4.src_ip,
33d30ea906Sjfb8856606 				(uint8_t *)&sa->src.ip.ip4, 4);
34d30ea906Sjfb8856606 
35d30ea906Sjfb8856606 			memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
36d30ea906Sjfb8856606 				(uint8_t *)&sa->dst.ip.ip4, 4);
374418919fSjohnjiang 		} else if (IS_IP6_TUNNEL(sa->flags)) {
384418919fSjohnjiang 			tunnel->type =
394418919fSjohnjiang 				RTE_SECURITY_IPSEC_TUNNEL_IPV6;
404418919fSjohnjiang 			tunnel->ipv6.hlimit = IPDEFTTL;
414418919fSjohnjiang 			tunnel->ipv6.dscp = 0;
424418919fSjohnjiang 			tunnel->ipv6.flabel = 0;
434418919fSjohnjiang 
444418919fSjohnjiang 			memcpy((uint8_t *)&tunnel->ipv6.src_addr,
454418919fSjohnjiang 				(uint8_t *)&sa->src.ip.ip6.ip6_b, 16);
464418919fSjohnjiang 
474418919fSjohnjiang 			memcpy((uint8_t *)&tunnel->ipv6.dst_addr,
484418919fSjohnjiang 				(uint8_t *)&sa->dst.ip.ip6.ip6_b, 16);
49d30ea906Sjfb8856606 		}
504418919fSjohnjiang 		/* TODO support for Transport */
51d30ea906Sjfb8856606 	}
52d30ea906Sjfb8856606 	ipsec->esn_soft_limit = IPSEC_OFFLOAD_ESN_SOFTLIMIT;
534418919fSjohnjiang 	ipsec->replay_win_sz = app_sa_prm.window_size;
544418919fSjohnjiang 	ipsec->options.esn = app_sa_prm.enable_esn;
55d30ea906Sjfb8856606 }
56d30ea906Sjfb8856606 
574418919fSjohnjiang int
create_lookaside_session(struct ipsec_ctx * ipsec_ctx,struct ipsec_sa * sa,struct rte_ipsec_session * ips)584418919fSjohnjiang create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa,
594418919fSjohnjiang 		struct rte_ipsec_session *ips)
60a9643ea8Slogwang {
612bfe3f2eSlogwang 	struct rte_cryptodev_info cdev_info;
62a9643ea8Slogwang 	unsigned long cdev_id_qp = 0;
632bfe3f2eSlogwang 	int32_t ret = 0;
64a9643ea8Slogwang 	struct cdev_key key = { 0 };
65a9643ea8Slogwang 
66a9643ea8Slogwang 	key.lcore_id = (uint8_t)rte_lcore_id();
67a9643ea8Slogwang 
68a9643ea8Slogwang 	key.cipher_algo = (uint8_t)sa->cipher_algo;
69a9643ea8Slogwang 	key.auth_algo = (uint8_t)sa->auth_algo;
702bfe3f2eSlogwang 	key.aead_algo = (uint8_t)sa->aead_algo;
71a9643ea8Slogwang 
72a9643ea8Slogwang 	ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
73a9643ea8Slogwang 			(void **)&cdev_id_qp);
74a9643ea8Slogwang 	if (ret < 0) {
752bfe3f2eSlogwang 		RTE_LOG(ERR, IPSEC,
762bfe3f2eSlogwang 				"No cryptodev: core %u, cipher_algo %u, "
772bfe3f2eSlogwang 				"auth_algo %u, aead_algo %u\n",
782bfe3f2eSlogwang 				key.lcore_id,
792bfe3f2eSlogwang 				key.cipher_algo,
802bfe3f2eSlogwang 				key.auth_algo,
812bfe3f2eSlogwang 				key.aead_algo);
82a9643ea8Slogwang 		return -1;
83a9643ea8Slogwang 	}
84a9643ea8Slogwang 
852bfe3f2eSlogwang 	RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
86a9643ea8Slogwang 			"%u qp %u\n", sa->spi,
87a9643ea8Slogwang 			ipsec_ctx->tbl[cdev_id_qp].id,
88a9643ea8Slogwang 			ipsec_ctx->tbl[cdev_id_qp].qp);
89a9643ea8Slogwang 
90*2d9fd380Sjfb8856606 	if (ips->type != RTE_SECURITY_ACTION_TYPE_NONE &&
91*2d9fd380Sjfb8856606 		ips->type != RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) {
922bfe3f2eSlogwang 		struct rte_security_session_conf sess_conf = {
934418919fSjohnjiang 			.action_type = ips->type,
942bfe3f2eSlogwang 			.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
952bfe3f2eSlogwang 			{.ipsec = {
962bfe3f2eSlogwang 				.spi = sa->spi,
972bfe3f2eSlogwang 				.salt = sa->salt,
982bfe3f2eSlogwang 				.options = { 0 },
994418919fSjohnjiang 				.replay_win_sz = 0,
1002bfe3f2eSlogwang 				.direction = sa->direction,
1012bfe3f2eSlogwang 				.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1024b05018fSfengbojiang 				.mode = (IS_TUNNEL(sa->flags)) ?
1032bfe3f2eSlogwang 					RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
1042bfe3f2eSlogwang 					RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
1052bfe3f2eSlogwang 			} },
106d30ea906Sjfb8856606 			.crypto_xform = sa->xforms,
107d30ea906Sjfb8856606 			.userdata = NULL,
108a9643ea8Slogwang 
1092bfe3f2eSlogwang 		};
1102bfe3f2eSlogwang 
1114418919fSjohnjiang 		if (ips->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
1122bfe3f2eSlogwang 			struct rte_security_ctx *ctx = (struct rte_security_ctx *)
1132bfe3f2eSlogwang 							rte_cryptodev_get_sec_ctx(
1142bfe3f2eSlogwang 							ipsec_ctx->tbl[cdev_id_qp].id);
1152bfe3f2eSlogwang 
116d30ea906Sjfb8856606 			/* Set IPsec parameters in conf */
117d30ea906Sjfb8856606 			set_ipsec_conf(sa, &(sess_conf.ipsec));
1182bfe3f2eSlogwang 
1194418919fSjohnjiang 			ips->security.ses = rte_security_session_create(ctx,
120*2d9fd380Sjfb8856606 					&sess_conf, ipsec_ctx->session_pool,
121*2d9fd380Sjfb8856606 					ipsec_ctx->session_priv_pool);
1224418919fSjohnjiang 			if (ips->security.ses == NULL) {
1232bfe3f2eSlogwang 				RTE_LOG(ERR, IPSEC,
1242bfe3f2eSlogwang 				"SEC Session init failed: err: %d\n", ret);
1252bfe3f2eSlogwang 				return -1;
1262bfe3f2eSlogwang 			}
1274418919fSjohnjiang 		} else {
1284418919fSjohnjiang 			RTE_LOG(ERR, IPSEC, "Inline not supported\n");
1294418919fSjohnjiang 			return -1;
1304418919fSjohnjiang 		}
1314418919fSjohnjiang 	} else {
132*2d9fd380Sjfb8856606 		if (ips->type == RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) {
133*2d9fd380Sjfb8856606 			struct rte_cryptodev_info info;
134*2d9fd380Sjfb8856606 			uint16_t cdev_id;
135*2d9fd380Sjfb8856606 
136*2d9fd380Sjfb8856606 			cdev_id = ipsec_ctx->tbl[cdev_id_qp].id;
137*2d9fd380Sjfb8856606 			rte_cryptodev_info_get(cdev_id, &info);
138*2d9fd380Sjfb8856606 			if (!(info.feature_flags &
139*2d9fd380Sjfb8856606 				RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO))
140*2d9fd380Sjfb8856606 				return -ENOTSUP;
141*2d9fd380Sjfb8856606 
142*2d9fd380Sjfb8856606 			ips->crypto.dev_id = cdev_id;
143*2d9fd380Sjfb8856606 		}
1444418919fSjohnjiang 		ips->crypto.ses = rte_cryptodev_sym_session_create(
1454418919fSjohnjiang 				ipsec_ctx->session_pool);
1464418919fSjohnjiang 		rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
1474418919fSjohnjiang 				ips->crypto.ses, sa->xforms,
1484418919fSjohnjiang 				ipsec_ctx->session_priv_pool);
1494418919fSjohnjiang 
1504418919fSjohnjiang 		rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
1514418919fSjohnjiang 				&cdev_info);
1524418919fSjohnjiang 	}
1534418919fSjohnjiang 
1544418919fSjohnjiang 	sa->cdev_id_qp = cdev_id_qp;
1554418919fSjohnjiang 
1564418919fSjohnjiang 	return 0;
1574418919fSjohnjiang }
1584418919fSjohnjiang 
1594418919fSjohnjiang int
create_inline_session(struct socket_ctx * skt_ctx,struct ipsec_sa * sa,struct rte_ipsec_session * ips)1604418919fSjohnjiang create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa,
1614418919fSjohnjiang 		struct rte_ipsec_session *ips)
1624418919fSjohnjiang {
1634418919fSjohnjiang 	int32_t ret = 0;
1644418919fSjohnjiang 	struct rte_security_ctx *sec_ctx;
1654418919fSjohnjiang 	struct rte_security_session_conf sess_conf = {
1664418919fSjohnjiang 		.action_type = ips->type,
1674418919fSjohnjiang 		.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
1684418919fSjohnjiang 		{.ipsec = {
1694418919fSjohnjiang 			.spi = sa->spi,
1704418919fSjohnjiang 			.salt = sa->salt,
1714418919fSjohnjiang 			.options = { 0 },
1724418919fSjohnjiang 			.replay_win_sz = 0,
1734418919fSjohnjiang 			.direction = sa->direction,
1744418919fSjohnjiang 			.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
1754418919fSjohnjiang 			.mode = (sa->flags == IP4_TUNNEL ||
1764418919fSjohnjiang 					sa->flags == IP6_TUNNEL) ?
1774418919fSjohnjiang 					RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
1784418919fSjohnjiang 					RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
1794418919fSjohnjiang 		} },
1804418919fSjohnjiang 		.crypto_xform = sa->xforms,
1814418919fSjohnjiang 		.userdata = NULL,
1824418919fSjohnjiang 	};
1834418919fSjohnjiang 
1844418919fSjohnjiang 	RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n",
1854418919fSjohnjiang 		sa->spi, sa->portid);
1864418919fSjohnjiang 
1874418919fSjohnjiang 	if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
1882bfe3f2eSlogwang 		struct rte_flow_error err;
1892bfe3f2eSlogwang 		const struct rte_security_capability *sec_cap;
190d30ea906Sjfb8856606 		int ret = 0;
1912bfe3f2eSlogwang 
1924418919fSjohnjiang 		sec_ctx = (struct rte_security_ctx *)
1934418919fSjohnjiang 					rte_eth_dev_get_sec_ctx(
1944418919fSjohnjiang 					sa->portid);
1954418919fSjohnjiang 		if (sec_ctx == NULL) {
1964418919fSjohnjiang 			RTE_LOG(ERR, IPSEC,
1974418919fSjohnjiang 				" rte_eth_dev_get_sec_ctx failed\n");
1984418919fSjohnjiang 			return -1;
1994418919fSjohnjiang 		}
2004418919fSjohnjiang 
2014418919fSjohnjiang 		ips->security.ses = rte_security_session_create(sec_ctx,
202*2d9fd380Sjfb8856606 				&sess_conf, skt_ctx->session_pool,
203*2d9fd380Sjfb8856606 				skt_ctx->session_priv_pool);
2044418919fSjohnjiang 		if (ips->security.ses == NULL) {
2052bfe3f2eSlogwang 			RTE_LOG(ERR, IPSEC,
2062bfe3f2eSlogwang 				"SEC Session init failed: err: %d\n", ret);
2072bfe3f2eSlogwang 			return -1;
2082bfe3f2eSlogwang 		}
2092bfe3f2eSlogwang 
2104418919fSjohnjiang 		sec_cap = rte_security_capabilities_get(sec_ctx);
2112bfe3f2eSlogwang 
2122bfe3f2eSlogwang 		/* iterate until ESP tunnel*/
2134418919fSjohnjiang 		while (sec_cap->action != RTE_SECURITY_ACTION_TYPE_NONE) {
2144418919fSjohnjiang 			if (sec_cap->action == ips->type &&
2152bfe3f2eSlogwang 			    sec_cap->protocol ==
2162bfe3f2eSlogwang 				RTE_SECURITY_PROTOCOL_IPSEC &&
2172bfe3f2eSlogwang 			    sec_cap->ipsec.mode ==
2184418919fSjohnjiang 				RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
2192bfe3f2eSlogwang 			    sec_cap->ipsec.direction == sa->direction)
2202bfe3f2eSlogwang 				break;
2212bfe3f2eSlogwang 			sec_cap++;
2222bfe3f2eSlogwang 		}
2232bfe3f2eSlogwang 
2242bfe3f2eSlogwang 		if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
2252bfe3f2eSlogwang 			RTE_LOG(ERR, IPSEC,
2262bfe3f2eSlogwang 				"No suitable security capability found\n");
2272bfe3f2eSlogwang 			return -1;
2282bfe3f2eSlogwang 		}
2292bfe3f2eSlogwang 
2304418919fSjohnjiang 		ips->security.ol_flags = sec_cap->ol_flags;
2314418919fSjohnjiang 		ips->security.ctx = sec_ctx;
2322bfe3f2eSlogwang 		sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
2332bfe3f2eSlogwang 
2344b05018fSfengbojiang 		if (IS_IP6(sa->flags)) {
2354b05018fSfengbojiang 			sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
2364b05018fSfengbojiang 			sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
2372bfe3f2eSlogwang 			sa->pattern[1].spec = &sa->ipv6_spec;
2384b05018fSfengbojiang 
2392bfe3f2eSlogwang 			memcpy(sa->ipv6_spec.hdr.dst_addr,
2402bfe3f2eSlogwang 				sa->dst.ip.ip6.ip6_b, 16);
2412bfe3f2eSlogwang 			memcpy(sa->ipv6_spec.hdr.src_addr,
2422bfe3f2eSlogwang 			       sa->src.ip.ip6.ip6_b, 16);
2434b05018fSfengbojiang 		} else if (IS_IP4(sa->flags)) {
2444b05018fSfengbojiang 			sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
2454b05018fSfengbojiang 			sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
2462bfe3f2eSlogwang 			sa->pattern[1].spec = &sa->ipv4_spec;
2474b05018fSfengbojiang 
2482bfe3f2eSlogwang 			sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
2492bfe3f2eSlogwang 			sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
2502bfe3f2eSlogwang 		}
2512bfe3f2eSlogwang 
2522bfe3f2eSlogwang 		sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
2532bfe3f2eSlogwang 		sa->pattern[2].spec = &sa->esp_spec;
2542bfe3f2eSlogwang 		sa->pattern[2].mask = &rte_flow_item_esp_mask;
2552bfe3f2eSlogwang 		sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
2562bfe3f2eSlogwang 
2572bfe3f2eSlogwang 		sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
2582bfe3f2eSlogwang 
2592bfe3f2eSlogwang 		sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
2604418919fSjohnjiang 		sa->action[0].conf = ips->security.ses;
2612bfe3f2eSlogwang 
2622bfe3f2eSlogwang 		sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
2632bfe3f2eSlogwang 
2642bfe3f2eSlogwang 		sa->attr.egress = (sa->direction ==
2652bfe3f2eSlogwang 				RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
2662bfe3f2eSlogwang 		sa->attr.ingress = (sa->direction ==
2672bfe3f2eSlogwang 				RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
268d30ea906Sjfb8856606 		if (sa->attr.ingress) {
269d30ea906Sjfb8856606 			uint8_t rss_key[40];
270d30ea906Sjfb8856606 			struct rte_eth_rss_conf rss_conf = {
271d30ea906Sjfb8856606 				.rss_key = rss_key,
272d30ea906Sjfb8856606 				.rss_key_len = 40,
273d30ea906Sjfb8856606 			};
2744b05018fSfengbojiang 			struct rte_eth_dev_info dev_info;
275d30ea906Sjfb8856606 			uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
276d30ea906Sjfb8856606 			struct rte_flow_action_rss action_rss;
277d30ea906Sjfb8856606 			unsigned int i;
278d30ea906Sjfb8856606 			unsigned int j;
279d30ea906Sjfb8856606 
280*2d9fd380Sjfb8856606 			/* Don't create flow if default flow is created */
281*2d9fd380Sjfb8856606 			if (flow_info_tbl[sa->portid].rx_def_flow)
282*2d9fd380Sjfb8856606 				return 0;
283*2d9fd380Sjfb8856606 
2844418919fSjohnjiang 			ret = rte_eth_dev_info_get(sa->portid, &dev_info);
2854418919fSjohnjiang 			if (ret != 0) {
2864418919fSjohnjiang 				RTE_LOG(ERR, IPSEC,
2874418919fSjohnjiang 					"Error during getting device (port %u) info: %s\n",
2884418919fSjohnjiang 					sa->portid, strerror(-ret));
2894418919fSjohnjiang 				return ret;
2904418919fSjohnjiang 			}
2914418919fSjohnjiang 
292d30ea906Sjfb8856606 			sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
293d30ea906Sjfb8856606 			/* Try RSS. */
294d30ea906Sjfb8856606 			sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
295d30ea906Sjfb8856606 			sa->action[1].conf = &action_rss;
2964418919fSjohnjiang 			ret = rte_eth_dev_rss_hash_conf_get(sa->portid,
297d30ea906Sjfb8856606 					&rss_conf);
2984418919fSjohnjiang 			if (ret != 0) {
2994418919fSjohnjiang 				RTE_LOG(ERR, IPSEC,
3004418919fSjohnjiang 					"rte_eth_dev_rss_hash_conf_get:ret=%d\n",
3014418919fSjohnjiang 					ret);
3024418919fSjohnjiang 				return -1;
3034418919fSjohnjiang 			}
3044418919fSjohnjiang 			for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i)
305d30ea906Sjfb8856606 				queue[j++] = i;
3064418919fSjohnjiang 
307d30ea906Sjfb8856606 			action_rss = (struct rte_flow_action_rss){
308d30ea906Sjfb8856606 					.types = rss_conf.rss_hf,
309d30ea906Sjfb8856606 					.key_len = rss_conf.rss_key_len,
310d30ea906Sjfb8856606 					.queue_num = j,
311d30ea906Sjfb8856606 					.key = rss_key,
312d30ea906Sjfb8856606 					.queue = queue,
313d30ea906Sjfb8856606 			};
314d30ea906Sjfb8856606 			ret = rte_flow_validate(sa->portid, &sa->attr,
315d30ea906Sjfb8856606 						sa->pattern, sa->action,
316d30ea906Sjfb8856606 						&err);
317d30ea906Sjfb8856606 			if (!ret)
318d30ea906Sjfb8856606 				goto flow_create;
319d30ea906Sjfb8856606 			/* Try Queue. */
320d30ea906Sjfb8856606 			sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
321d30ea906Sjfb8856606 			sa->action[1].conf =
322d30ea906Sjfb8856606 				&(struct rte_flow_action_queue){
323d30ea906Sjfb8856606 				.index = 0,
324d30ea906Sjfb8856606 			};
325d30ea906Sjfb8856606 			ret = rte_flow_validate(sa->portid, &sa->attr,
326d30ea906Sjfb8856606 						sa->pattern, sa->action,
327d30ea906Sjfb8856606 						&err);
328d30ea906Sjfb8856606 			/* Try End. */
329d30ea906Sjfb8856606 			sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
330d30ea906Sjfb8856606 			sa->action[1].conf = NULL;
331d30ea906Sjfb8856606 			ret = rte_flow_validate(sa->portid, &sa->attr,
332d30ea906Sjfb8856606 						sa->pattern, sa->action,
333d30ea906Sjfb8856606 						&err);
334d30ea906Sjfb8856606 			if (ret)
335d30ea906Sjfb8856606 				goto flow_create_failure;
336d30ea906Sjfb8856606 		} else if (sa->attr.egress &&
3374418919fSjohnjiang 				(ips->security.ol_flags &
338d30ea906Sjfb8856606 					RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
339d30ea906Sjfb8856606 			sa->action[1].type =
340d30ea906Sjfb8856606 					RTE_FLOW_ACTION_TYPE_PASSTHRU;
341d30ea906Sjfb8856606 			sa->action[2].type =
342d30ea906Sjfb8856606 					RTE_FLOW_ACTION_TYPE_END;
343d30ea906Sjfb8856606 		}
344d30ea906Sjfb8856606 flow_create:
3452bfe3f2eSlogwang 		sa->flow = rte_flow_create(sa->portid,
3462bfe3f2eSlogwang 				&sa->attr, sa->pattern, sa->action, &err);
3472bfe3f2eSlogwang 		if (sa->flow == NULL) {
348d30ea906Sjfb8856606 flow_create_failure:
3492bfe3f2eSlogwang 			RTE_LOG(ERR, IPSEC,
3502bfe3f2eSlogwang 				"Failed to create ipsec flow msg: %s\n",
3512bfe3f2eSlogwang 				err.message);
3522bfe3f2eSlogwang 			return -1;
3532bfe3f2eSlogwang 		}
3544418919fSjohnjiang 	} else if (ips->type ==	RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
355d30ea906Sjfb8856606 		const struct rte_security_capability *sec_cap;
356d30ea906Sjfb8856606 
3574418919fSjohnjiang 		sec_ctx = (struct rte_security_ctx *)
3584418919fSjohnjiang 				rte_eth_dev_get_sec_ctx(sa->portid);
3594418919fSjohnjiang 
3604418919fSjohnjiang 		if (sec_ctx == NULL) {
361d30ea906Sjfb8856606 			RTE_LOG(ERR, IPSEC,
362d30ea906Sjfb8856606 				"Ethernet device doesn't have security features registered\n");
363d30ea906Sjfb8856606 			return -1;
364d30ea906Sjfb8856606 		}
365d30ea906Sjfb8856606 
366d30ea906Sjfb8856606 		/* Set IPsec parameters in conf */
367d30ea906Sjfb8856606 		set_ipsec_conf(sa, &(sess_conf.ipsec));
368d30ea906Sjfb8856606 
369d30ea906Sjfb8856606 		/* Save SA as userdata for the security session. When
370d30ea906Sjfb8856606 		 * the packet is received, this userdata will be
371d30ea906Sjfb8856606 		 * retrieved using the metadata from the packet.
372d30ea906Sjfb8856606 		 *
373d30ea906Sjfb8856606 		 * The PMD is expected to set similar metadata for other
374d30ea906Sjfb8856606 		 * operations, like rte_eth_event, which are tied to
375d30ea906Sjfb8856606 		 * security session. In such cases, the userdata could
376d30ea906Sjfb8856606 		 * be obtained to uniquely identify the security
377d30ea906Sjfb8856606 		 * parameters denoted.
378d30ea906Sjfb8856606 		 */
379d30ea906Sjfb8856606 
380d30ea906Sjfb8856606 		sess_conf.userdata = (void *) sa;
381d30ea906Sjfb8856606 
3824418919fSjohnjiang 		ips->security.ses = rte_security_session_create(sec_ctx,
383*2d9fd380Sjfb8856606 					&sess_conf, skt_ctx->session_pool,
384*2d9fd380Sjfb8856606 					skt_ctx->session_priv_pool);
3854418919fSjohnjiang 		if (ips->security.ses == NULL) {
386d30ea906Sjfb8856606 			RTE_LOG(ERR, IPSEC,
387d30ea906Sjfb8856606 				"SEC Session init failed: err: %d\n", ret);
388d30ea906Sjfb8856606 			return -1;
389d30ea906Sjfb8856606 		}
390d30ea906Sjfb8856606 
3914418919fSjohnjiang 		sec_cap = rte_security_capabilities_get(sec_ctx);
392d30ea906Sjfb8856606 		if (sec_cap == NULL) {
393d30ea906Sjfb8856606 			RTE_LOG(ERR, IPSEC,
394d30ea906Sjfb8856606 				"No capabilities registered\n");
395d30ea906Sjfb8856606 			return -1;
396d30ea906Sjfb8856606 		}
397d30ea906Sjfb8856606 
398d30ea906Sjfb8856606 		/* iterate until ESP tunnel*/
399d30ea906Sjfb8856606 		while (sec_cap->action !=
400d30ea906Sjfb8856606 				RTE_SECURITY_ACTION_TYPE_NONE) {
4014418919fSjohnjiang 			if (sec_cap->action == ips->type &&
402d30ea906Sjfb8856606 			    sec_cap->protocol ==
403d30ea906Sjfb8856606 				RTE_SECURITY_PROTOCOL_IPSEC &&
404d30ea906Sjfb8856606 			    sec_cap->ipsec.mode ==
4054b05018fSfengbojiang 				sess_conf.ipsec.mode &&
406d30ea906Sjfb8856606 			    sec_cap->ipsec.direction == sa->direction)
407d30ea906Sjfb8856606 				break;
408d30ea906Sjfb8856606 			sec_cap++;
409d30ea906Sjfb8856606 		}
410d30ea906Sjfb8856606 
411d30ea906Sjfb8856606 		if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
412d30ea906Sjfb8856606 			RTE_LOG(ERR, IPSEC,
413d30ea906Sjfb8856606 				"No suitable security capability found\n");
414d30ea906Sjfb8856606 			return -1;
415d30ea906Sjfb8856606 		}
416d30ea906Sjfb8856606 
4174418919fSjohnjiang 		ips->security.ol_flags = sec_cap->ol_flags;
4184418919fSjohnjiang 		ips->security.ctx = sec_ctx;
4192bfe3f2eSlogwang 	}
420*2d9fd380Sjfb8856606 
421*2d9fd380Sjfb8856606 	return 0;
422*2d9fd380Sjfb8856606 }
423*2d9fd380Sjfb8856606 
424*2d9fd380Sjfb8856606 int
create_ipsec_esp_flow(struct ipsec_sa * sa)425*2d9fd380Sjfb8856606 create_ipsec_esp_flow(struct ipsec_sa *sa)
426*2d9fd380Sjfb8856606 {
427*2d9fd380Sjfb8856606 	int ret = 0;
428*2d9fd380Sjfb8856606 	struct rte_flow_error err;
429*2d9fd380Sjfb8856606 	if (sa->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) {
430*2d9fd380Sjfb8856606 		RTE_LOG(ERR, IPSEC,
431*2d9fd380Sjfb8856606 			"No Flow director rule for Egress traffic\n");
432*2d9fd380Sjfb8856606 		return -1;
433*2d9fd380Sjfb8856606 	}
434*2d9fd380Sjfb8856606 	if (sa->flags == TRANSPORT) {
435*2d9fd380Sjfb8856606 		RTE_LOG(ERR, IPSEC,
436*2d9fd380Sjfb8856606 			"No Flow director rule for transport mode\n");
437*2d9fd380Sjfb8856606 		return -1;
438*2d9fd380Sjfb8856606 	}
439*2d9fd380Sjfb8856606 	sa->action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE;
440*2d9fd380Sjfb8856606 	sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
441*2d9fd380Sjfb8856606 	sa->action[0].conf = &(struct rte_flow_action_queue) {
442*2d9fd380Sjfb8856606 				.index = sa->fdir_qid,
443*2d9fd380Sjfb8856606 	};
444*2d9fd380Sjfb8856606 	sa->attr.egress = 0;
445*2d9fd380Sjfb8856606 	sa->attr.ingress = 1;
446*2d9fd380Sjfb8856606 	if (IS_IP6(sa->flags)) {
447*2d9fd380Sjfb8856606 		sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
448*2d9fd380Sjfb8856606 		sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
449*2d9fd380Sjfb8856606 		sa->pattern[1].spec = &sa->ipv6_spec;
450*2d9fd380Sjfb8856606 		memcpy(sa->ipv6_spec.hdr.dst_addr,
451*2d9fd380Sjfb8856606 			sa->dst.ip.ip6.ip6_b, sizeof(sa->dst.ip.ip6.ip6_b));
452*2d9fd380Sjfb8856606 		memcpy(sa->ipv6_spec.hdr.src_addr,
453*2d9fd380Sjfb8856606 			sa->src.ip.ip6.ip6_b, sizeof(sa->src.ip.ip6.ip6_b));
454*2d9fd380Sjfb8856606 		sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
455*2d9fd380Sjfb8856606 		sa->pattern[2].spec = &sa->esp_spec;
456*2d9fd380Sjfb8856606 		sa->pattern[2].mask = &rte_flow_item_esp_mask;
457*2d9fd380Sjfb8856606 		sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
458*2d9fd380Sjfb8856606 		sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
459*2d9fd380Sjfb8856606 	} else if (IS_IP4(sa->flags)) {
460*2d9fd380Sjfb8856606 		sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
461*2d9fd380Sjfb8856606 		sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
462*2d9fd380Sjfb8856606 		sa->pattern[1].spec = &sa->ipv4_spec;
463*2d9fd380Sjfb8856606 		sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
464*2d9fd380Sjfb8856606 		sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
465*2d9fd380Sjfb8856606 		sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
466*2d9fd380Sjfb8856606 		sa->pattern[2].spec = &sa->esp_spec;
467*2d9fd380Sjfb8856606 		sa->pattern[2].mask = &rte_flow_item_esp_mask;
468*2d9fd380Sjfb8856606 		sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
469*2d9fd380Sjfb8856606 		sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
470*2d9fd380Sjfb8856606 	}
471*2d9fd380Sjfb8856606 	sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
472*2d9fd380Sjfb8856606 
473*2d9fd380Sjfb8856606 	ret = rte_flow_validate(sa->portid, &sa->attr, sa->pattern, sa->action,
474*2d9fd380Sjfb8856606 				&err);
475*2d9fd380Sjfb8856606 	if (ret < 0) {
476*2d9fd380Sjfb8856606 		RTE_LOG(ERR, IPSEC, "Flow validation failed %s\n", err.message);
477*2d9fd380Sjfb8856606 		return ret;
478*2d9fd380Sjfb8856606 	}
479*2d9fd380Sjfb8856606 
480*2d9fd380Sjfb8856606 	sa->flow = rte_flow_create(sa->portid, &sa->attr, sa->pattern,
481*2d9fd380Sjfb8856606 					sa->action, &err);
482*2d9fd380Sjfb8856606 	if (!sa->flow) {
483*2d9fd380Sjfb8856606 		RTE_LOG(ERR, IPSEC, "Flow creation failed %s\n", err.message);
484*2d9fd380Sjfb8856606 		return -1;
485*2d9fd380Sjfb8856606 	}
486a9643ea8Slogwang 
487a9643ea8Slogwang 	return 0;
488a9643ea8Slogwang }
489a9643ea8Slogwang 
4901646932aSjfb8856606 /*
4911646932aSjfb8856606  * queue crypto-ops into PMD queue.
4921646932aSjfb8856606  */
4931646932aSjfb8856606 void
enqueue_cop_burst(struct cdev_qp * cqp)4941646932aSjfb8856606 enqueue_cop_burst(struct cdev_qp *cqp)
495a9643ea8Slogwang {
4961646932aSjfb8856606 	uint32_t i, len, ret;
497a9643ea8Slogwang 
4981646932aSjfb8856606 	len = cqp->len;
4991646932aSjfb8856606 	ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
5001646932aSjfb8856606 	if (ret < len) {
5012bfe3f2eSlogwang 		RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
502a9643ea8Slogwang 			" enqueued %u crypto ops out of %u\n",
5031646932aSjfb8856606 			cqp->id, cqp->qp, ret, len);
5041646932aSjfb8856606 			/* drop packets that we fail to enqueue */
5051646932aSjfb8856606 			for (i = ret; i < len; i++)
506*2d9fd380Sjfb8856606 				free_pkts(&cqp->buf[i]->sym->m_src, 1);
507a9643ea8Slogwang 	}
508a9643ea8Slogwang 	cqp->in_flight += ret;
509a9643ea8Slogwang 	cqp->len = 0;
510a9643ea8Slogwang }
5111646932aSjfb8856606 
5121646932aSjfb8856606 static inline void
enqueue_cop(struct cdev_qp * cqp,struct rte_crypto_op * cop)5131646932aSjfb8856606 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
5141646932aSjfb8856606 {
5151646932aSjfb8856606 	cqp->buf[cqp->len++] = cop;
5161646932aSjfb8856606 
5171646932aSjfb8856606 	if (cqp->len == MAX_PKT_BURST)
5181646932aSjfb8856606 		enqueue_cop_burst(cqp);
519a9643ea8Slogwang }
520a9643ea8Slogwang 
521a9643ea8Slogwang static inline void
ipsec_enqueue(ipsec_xform_fn xform_func,struct ipsec_ctx * ipsec_ctx,struct rte_mbuf * pkts[],void * sas[],uint16_t nb_pkts)522a9643ea8Slogwang ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
5234418919fSjohnjiang 		struct rte_mbuf *pkts[], void *sas[],
524a9643ea8Slogwang 		uint16_t nb_pkts)
525a9643ea8Slogwang {
526a9643ea8Slogwang 	int32_t ret = 0, i;
527a9643ea8Slogwang 	struct ipsec_mbuf_metadata *priv;
5282bfe3f2eSlogwang 	struct rte_crypto_sym_op *sym_cop;
529a9643ea8Slogwang 	struct ipsec_sa *sa;
5304418919fSjohnjiang 	struct rte_ipsec_session *ips;
531a9643ea8Slogwang 
532a9643ea8Slogwang 	for (i = 0; i < nb_pkts; i++) {
533a9643ea8Slogwang 		if (unlikely(sas[i] == NULL)) {
534*2d9fd380Sjfb8856606 			free_pkts(&pkts[i], 1);
535a9643ea8Slogwang 			continue;
536a9643ea8Slogwang 		}
537a9643ea8Slogwang 
538a9643ea8Slogwang 		rte_prefetch0(sas[i]);
539a9643ea8Slogwang 		rte_prefetch0(pkts[i]);
540a9643ea8Slogwang 
541a9643ea8Slogwang 		priv = get_priv(pkts[i]);
5424418919fSjohnjiang 		sa = ipsec_mask_saptr(sas[i]);
543a9643ea8Slogwang 		priv->sa = sa;
5444418919fSjohnjiang 		ips = ipsec_get_primary_session(sa);
545a9643ea8Slogwang 
5464418919fSjohnjiang 		switch (ips->type) {
5472bfe3f2eSlogwang 		case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
548a9643ea8Slogwang 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
5492bfe3f2eSlogwang 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
550a9643ea8Slogwang 
551a9643ea8Slogwang 			rte_prefetch0(&priv->sym_cop);
5522bfe3f2eSlogwang 
5534418919fSjohnjiang 			if ((unlikely(ips->security.ses == NULL)) &&
5544418919fSjohnjiang 				create_lookaside_session(ipsec_ctx, sa, ips)) {
555*2d9fd380Sjfb8856606 				free_pkts(&pkts[i], 1);
5562bfe3f2eSlogwang 				continue;
5572bfe3f2eSlogwang 			}
5582bfe3f2eSlogwang 
5592bfe3f2eSlogwang 			sym_cop = get_sym_cop(&priv->cop);
5602bfe3f2eSlogwang 			sym_cop->m_src = pkts[i];
5612bfe3f2eSlogwang 
5622bfe3f2eSlogwang 			rte_security_attach_session(&priv->cop,
5634418919fSjohnjiang 				ips->security.ses);
5642bfe3f2eSlogwang 			break;
565*2d9fd380Sjfb8856606 
566*2d9fd380Sjfb8856606 		case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
567*2d9fd380Sjfb8856606 			RTE_LOG(ERR, IPSEC, "CPU crypto is not supported by the"
568*2d9fd380Sjfb8856606 					" legacy mode.");
569*2d9fd380Sjfb8856606 			free_pkts(&pkts[i], 1);
570*2d9fd380Sjfb8856606 			continue;
571*2d9fd380Sjfb8856606 
5722bfe3f2eSlogwang 		case RTE_SECURITY_ACTION_TYPE_NONE:
5732bfe3f2eSlogwang 
5742bfe3f2eSlogwang 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
5752bfe3f2eSlogwang 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
5762bfe3f2eSlogwang 
5772bfe3f2eSlogwang 			rte_prefetch0(&priv->sym_cop);
578a9643ea8Slogwang 
5794418919fSjohnjiang 			if ((unlikely(ips->crypto.ses == NULL)) &&
5804418919fSjohnjiang 				create_lookaside_session(ipsec_ctx, sa, ips)) {
581*2d9fd380Sjfb8856606 				free_pkts(&pkts[i], 1);
582a9643ea8Slogwang 				continue;
583a9643ea8Slogwang 			}
584a9643ea8Slogwang 
585a9643ea8Slogwang 			rte_crypto_op_attach_sym_session(&priv->cop,
5864418919fSjohnjiang 					ips->crypto.ses);
587a9643ea8Slogwang 
588a9643ea8Slogwang 			ret = xform_func(pkts[i], sa, &priv->cop);
589a9643ea8Slogwang 			if (unlikely(ret)) {
590*2d9fd380Sjfb8856606 				free_pkts(&pkts[i], 1);
591a9643ea8Slogwang 				continue;
592a9643ea8Slogwang 			}
5932bfe3f2eSlogwang 			break;
5942bfe3f2eSlogwang 		case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
5954418919fSjohnjiang 			RTE_ASSERT(ips->security.ses != NULL);
596d30ea906Sjfb8856606 			ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
5974418919fSjohnjiang 			if (ips->security.ol_flags &
5984418919fSjohnjiang 				RTE_SECURITY_TX_OLOAD_NEED_MDATA)
599d30ea906Sjfb8856606 				rte_security_set_pkt_metadata(
6004418919fSjohnjiang 					ips->security.ctx, ips->security.ses,
6014418919fSjohnjiang 					pkts[i], NULL);
602d30ea906Sjfb8856606 			continue;
6032bfe3f2eSlogwang 		case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
6044418919fSjohnjiang 			RTE_ASSERT(ips->security.ses != NULL);
6052bfe3f2eSlogwang 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
6062bfe3f2eSlogwang 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
6072bfe3f2eSlogwang 
6082bfe3f2eSlogwang 			rte_prefetch0(&priv->sym_cop);
6092bfe3f2eSlogwang 			rte_security_attach_session(&priv->cop,
6104418919fSjohnjiang 					ips->security.ses);
6112bfe3f2eSlogwang 
6122bfe3f2eSlogwang 			ret = xform_func(pkts[i], sa, &priv->cop);
6132bfe3f2eSlogwang 			if (unlikely(ret)) {
614*2d9fd380Sjfb8856606 				free_pkts(&pkts[i], 1);
6152bfe3f2eSlogwang 				continue;
6162bfe3f2eSlogwang 			}
6172bfe3f2eSlogwang 
618d30ea906Sjfb8856606 			ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
6194418919fSjohnjiang 			if (ips->security.ol_flags &
6204418919fSjohnjiang 				RTE_SECURITY_TX_OLOAD_NEED_MDATA)
6212bfe3f2eSlogwang 				rte_security_set_pkt_metadata(
6224418919fSjohnjiang 					ips->security.ctx, ips->security.ses,
6234418919fSjohnjiang 					pkts[i], NULL);
6242bfe3f2eSlogwang 			continue;
6252bfe3f2eSlogwang 		}
626a9643ea8Slogwang 
627a9643ea8Slogwang 		RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
628a9643ea8Slogwang 		enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
629a9643ea8Slogwang 	}
630a9643ea8Slogwang }
631a9643ea8Slogwang 
6321646932aSjfb8856606 static inline int32_t
ipsec_inline_dequeue(ipsec_xform_fn xform_func,struct ipsec_ctx * ipsec_ctx,struct rte_mbuf * pkts[],uint16_t max_pkts)6331646932aSjfb8856606 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
6341646932aSjfb8856606 	      struct rte_mbuf *pkts[], uint16_t max_pkts)
6351646932aSjfb8856606 {
6361646932aSjfb8856606 	int32_t nb_pkts, ret;
6371646932aSjfb8856606 	struct ipsec_mbuf_metadata *priv;
6381646932aSjfb8856606 	struct ipsec_sa *sa;
6391646932aSjfb8856606 	struct rte_mbuf *pkt;
6401646932aSjfb8856606 
6411646932aSjfb8856606 	nb_pkts = 0;
6421646932aSjfb8856606 	while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
6431646932aSjfb8856606 		pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
6441646932aSjfb8856606 		rte_prefetch0(pkt);
6451646932aSjfb8856606 		priv = get_priv(pkt);
6461646932aSjfb8856606 		sa = priv->sa;
6471646932aSjfb8856606 		ret = xform_func(pkt, sa, &priv->cop);
6481646932aSjfb8856606 		if (unlikely(ret)) {
649*2d9fd380Sjfb8856606 			free_pkts(&pkt, 1);
6501646932aSjfb8856606 			continue;
6511646932aSjfb8856606 		}
6521646932aSjfb8856606 		pkts[nb_pkts++] = pkt;
6531646932aSjfb8856606 	}
6541646932aSjfb8856606 
6551646932aSjfb8856606 	return nb_pkts;
6561646932aSjfb8856606 }
6571646932aSjfb8856606 
658a9643ea8Slogwang static inline int
ipsec_dequeue(ipsec_xform_fn xform_func,struct ipsec_ctx * ipsec_ctx,struct rte_mbuf * pkts[],uint16_t max_pkts)659a9643ea8Slogwang ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
660a9643ea8Slogwang 	      struct rte_mbuf *pkts[], uint16_t max_pkts)
661a9643ea8Slogwang {
662a9643ea8Slogwang 	int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
663a9643ea8Slogwang 	struct ipsec_mbuf_metadata *priv;
664a9643ea8Slogwang 	struct rte_crypto_op *cops[max_pkts];
665a9643ea8Slogwang 	struct ipsec_sa *sa;
666a9643ea8Slogwang 	struct rte_mbuf *pkt;
667a9643ea8Slogwang 
668a9643ea8Slogwang 	for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
669a9643ea8Slogwang 		struct cdev_qp *cqp;
670a9643ea8Slogwang 
671a9643ea8Slogwang 		cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
672a9643ea8Slogwang 		if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
673a9643ea8Slogwang 			ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
674a9643ea8Slogwang 
675a9643ea8Slogwang 		if (cqp->in_flight == 0)
676a9643ea8Slogwang 			continue;
677a9643ea8Slogwang 
678a9643ea8Slogwang 		nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
679a9643ea8Slogwang 				cops, max_pkts - nb_pkts);
680a9643ea8Slogwang 
681a9643ea8Slogwang 		cqp->in_flight -= nb_cops;
682a9643ea8Slogwang 
683a9643ea8Slogwang 		for (j = 0; j < nb_cops; j++) {
684a9643ea8Slogwang 			pkt = cops[j]->sym->m_src;
685a9643ea8Slogwang 			rte_prefetch0(pkt);
686a9643ea8Slogwang 
687a9643ea8Slogwang 			priv = get_priv(pkt);
688a9643ea8Slogwang 			sa = priv->sa;
689a9643ea8Slogwang 
690a9643ea8Slogwang 			RTE_ASSERT(sa != NULL);
691a9643ea8Slogwang 
6924418919fSjohnjiang 			if (ipsec_get_action_type(sa) ==
6934418919fSjohnjiang 				RTE_SECURITY_ACTION_TYPE_NONE) {
694a9643ea8Slogwang 				ret = xform_func(pkt, sa, cops[j]);
6952bfe3f2eSlogwang 				if (unlikely(ret)) {
696*2d9fd380Sjfb8856606 					free_pkts(&pkt, 1);
6972bfe3f2eSlogwang 					continue;
6982bfe3f2eSlogwang 				}
6994418919fSjohnjiang 			} else if (ipsec_get_action_type(sa) ==
7004418919fSjohnjiang 				RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
7014418919fSjohnjiang 				if (cops[j]->status) {
702*2d9fd380Sjfb8856606 					free_pkts(&pkt, 1);
7034418919fSjohnjiang 					continue;
7044418919fSjohnjiang 				}
7052bfe3f2eSlogwang 			}
706a9643ea8Slogwang 			pkts[nb_pkts++] = pkt;
707a9643ea8Slogwang 		}
708a9643ea8Slogwang 	}
709a9643ea8Slogwang 
710a9643ea8Slogwang 	/* return packets */
711a9643ea8Slogwang 	return nb_pkts;
712a9643ea8Slogwang }
713a9643ea8Slogwang 
714a9643ea8Slogwang uint16_t
ipsec_inbound(struct ipsec_ctx * ctx,struct rte_mbuf * pkts[],uint16_t nb_pkts,uint16_t len)715a9643ea8Slogwang ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
716a9643ea8Slogwang 		uint16_t nb_pkts, uint16_t len)
717a9643ea8Slogwang {
7184418919fSjohnjiang 	void *sas[nb_pkts];
719a9643ea8Slogwang 
720a9643ea8Slogwang 	inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
721a9643ea8Slogwang 
722a9643ea8Slogwang 	ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
723a9643ea8Slogwang 
7241646932aSjfb8856606 	return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
7251646932aSjfb8856606 }
7261646932aSjfb8856606 
7271646932aSjfb8856606 uint16_t
ipsec_inbound_cqp_dequeue(struct ipsec_ctx * ctx,struct rte_mbuf * pkts[],uint16_t len)7281646932aSjfb8856606 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
7291646932aSjfb8856606 		uint16_t len)
7301646932aSjfb8856606 {
731a9643ea8Slogwang 	return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
732a9643ea8Slogwang }
733a9643ea8Slogwang 
734a9643ea8Slogwang uint16_t
ipsec_outbound(struct ipsec_ctx * ctx,struct rte_mbuf * pkts[],uint32_t sa_idx[],uint16_t nb_pkts,uint16_t len)735a9643ea8Slogwang ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
736a9643ea8Slogwang 		uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
737a9643ea8Slogwang {
7384418919fSjohnjiang 	void *sas[nb_pkts];
739a9643ea8Slogwang 
740a9643ea8Slogwang 	outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
741a9643ea8Slogwang 
742a9643ea8Slogwang 	ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
743a9643ea8Slogwang 
7441646932aSjfb8856606 	return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
7451646932aSjfb8856606 }
7461646932aSjfb8856606 
7471646932aSjfb8856606 uint16_t
ipsec_outbound_cqp_dequeue(struct ipsec_ctx * ctx,struct rte_mbuf * pkts[],uint16_t len)7481646932aSjfb8856606 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
7491646932aSjfb8856606 		uint16_t len)
7501646932aSjfb8856606 {
751a9643ea8Slogwang 	return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);
752a9643ea8Slogwang }
753