xref: /f-stack/dpdk/examples/ipsec-secgw/ipsec.c (revision 9bd490e8)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 
37 #include <rte_branch_prediction.h>
38 #include <rte_log.h>
39 #include <rte_crypto.h>
40 #include <rte_security.h>
41 #include <rte_cryptodev.h>
42 #include <rte_ethdev.h>
43 #include <rte_mbuf.h>
44 #include <rte_hash.h>
45 
46 #include "ipsec.h"
47 #include "esp.h"
48 
49 static inline int
50 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
51 {
52 	struct rte_cryptodev_info cdev_info;
53 	unsigned long cdev_id_qp = 0;
54 	int32_t ret = 0;
55 	struct cdev_key key = { 0 };
56 
57 	key.lcore_id = (uint8_t)rte_lcore_id();
58 
59 	key.cipher_algo = (uint8_t)sa->cipher_algo;
60 	key.auth_algo = (uint8_t)sa->auth_algo;
61 	key.aead_algo = (uint8_t)sa->aead_algo;
62 
63 	if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
64 		ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
65 				(void **)&cdev_id_qp);
66 		if (ret < 0) {
67 			RTE_LOG(ERR, IPSEC,
68 				"No cryptodev: core %u, cipher_algo %u, "
69 				"auth_algo %u, aead_algo %u\n",
70 				key.lcore_id,
71 				key.cipher_algo,
72 				key.auth_algo,
73 				key.aead_algo);
74 			return -1;
75 		}
76 	}
77 
78 	RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
79 			"%u qp %u\n", sa->spi,
80 			ipsec_ctx->tbl[cdev_id_qp].id,
81 			ipsec_ctx->tbl[cdev_id_qp].qp);
82 
83 	if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
84 		struct rte_security_session_conf sess_conf = {
85 			.action_type = sa->type,
86 			.protocol = RTE_SECURITY_PROTOCOL_IPSEC,
87 			{.ipsec = {
88 				.spi = sa->spi,
89 				.salt = sa->salt,
90 				.options = { 0 },
91 				.direction = sa->direction,
92 				.proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
93 				.mode = (sa->flags == IP4_TUNNEL ||
94 						sa->flags == IP6_TUNNEL) ?
95 					RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
96 					RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
97 			} },
98 			.crypto_xform = sa->xforms
99 
100 		};
101 
102 		if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
103 			struct rte_security_ctx *ctx = (struct rte_security_ctx *)
104 							rte_cryptodev_get_sec_ctx(
105 							ipsec_ctx->tbl[cdev_id_qp].id);
106 
107 			if (sess_conf.ipsec.mode ==
108 					RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
109 				struct rte_security_ipsec_tunnel_param *tunnel =
110 						&sess_conf.ipsec.tunnel;
111 				if (sa->flags == IP4_TUNNEL) {
112 					tunnel->type =
113 						RTE_SECURITY_IPSEC_TUNNEL_IPV4;
114 					tunnel->ipv4.ttl = IPDEFTTL;
115 
116 					memcpy((uint8_t *)&tunnel->ipv4.src_ip,
117 						(uint8_t *)&sa->src.ip.ip4, 4);
118 
119 					memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
120 						(uint8_t *)&sa->dst.ip.ip4, 4);
121 				}
122 				/* TODO support for Transport and IPV6 tunnel */
123 			}
124 
125 			sa->sec_session = rte_security_session_create(ctx,
126 					&sess_conf, ipsec_ctx->session_pool);
127 			if (sa->sec_session == NULL) {
128 				RTE_LOG(ERR, IPSEC,
129 				"SEC Session init failed: err: %d\n", ret);
130 				return -1;
131 			}
132 		} else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
133 			struct rte_flow_error err;
134 			struct rte_security_ctx *ctx = (struct rte_security_ctx *)
135 							rte_eth_dev_get_sec_ctx(
136 							sa->portid);
137 			const struct rte_security_capability *sec_cap;
138 
139 			sa->sec_session = rte_security_session_create(ctx,
140 					&sess_conf, ipsec_ctx->session_pool);
141 			if (sa->sec_session == NULL) {
142 				RTE_LOG(ERR, IPSEC,
143 				"SEC Session init failed: err: %d\n", ret);
144 				return -1;
145 			}
146 
147 			sec_cap = rte_security_capabilities_get(ctx);
148 
149 			/* iterate until ESP tunnel*/
150 			while (sec_cap->action !=
151 					RTE_SECURITY_ACTION_TYPE_NONE) {
152 
153 				if (sec_cap->action == sa->type &&
154 				    sec_cap->protocol ==
155 					RTE_SECURITY_PROTOCOL_IPSEC &&
156 				    sec_cap->ipsec.mode ==
157 					RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
158 				    sec_cap->ipsec.direction == sa->direction)
159 					break;
160 				sec_cap++;
161 			}
162 
163 			if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
164 				RTE_LOG(ERR, IPSEC,
165 				"No suitable security capability found\n");
166 				return -1;
167 			}
168 
169 			sa->ol_flags = sec_cap->ol_flags;
170 			sa->security_ctx = ctx;
171 			sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
172 
173 			sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
174 			sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
175 			if (sa->flags & IP6_TUNNEL) {
176 				sa->pattern[1].spec = &sa->ipv6_spec;
177 				memcpy(sa->ipv6_spec.hdr.dst_addr,
178 					sa->dst.ip.ip6.ip6_b, 16);
179 				memcpy(sa->ipv6_spec.hdr.src_addr,
180 				       sa->src.ip.ip6.ip6_b, 16);
181 			} else {
182 				sa->pattern[1].spec = &sa->ipv4_spec;
183 				sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
184 				sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
185 			}
186 
187 			sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
188 			sa->pattern[2].spec = &sa->esp_spec;
189 			sa->pattern[2].mask = &rte_flow_item_esp_mask;
190 			sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
191 
192 			sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
193 
194 			sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
195 			sa->action[0].conf = sa->sec_session;
196 
197 			sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
198 
199 			sa->attr.egress = (sa->direction ==
200 					RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
201 			sa->attr.ingress = (sa->direction ==
202 					RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
203 			sa->flow = rte_flow_create(sa->portid,
204 				&sa->attr, sa->pattern, sa->action, &err);
205 			if (sa->flow == NULL) {
206 				RTE_LOG(ERR, IPSEC,
207 					"Failed to create ipsec flow msg: %s\n",
208 					err.message);
209 				return -1;
210 			}
211 		}
212 	} else {
213 		sa->crypto_session = rte_cryptodev_sym_session_create(
214 				ipsec_ctx->session_pool);
215 		rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
216 				sa->crypto_session, sa->xforms,
217 				ipsec_ctx->session_pool);
218 
219 		rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
220 				&cdev_info);
221 		if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
222 			ret = rte_cryptodev_queue_pair_attach_sym_session(
223 					ipsec_ctx->tbl[cdev_id_qp].id,
224 					ipsec_ctx->tbl[cdev_id_qp].qp,
225 					sa->crypto_session);
226 			if (ret < 0) {
227 				RTE_LOG(ERR, IPSEC,
228 					"Session cannot be attached to qp %u\n",
229 					ipsec_ctx->tbl[cdev_id_qp].qp);
230 				return -1;
231 			}
232 		}
233 	}
234 	sa->cdev_id_qp = cdev_id_qp;
235 
236 	return 0;
237 }
238 
239 static inline void
240 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
241 {
242 	int32_t ret, i;
243 
244 	cqp->buf[cqp->len++] = cop;
245 
246 	if (cqp->len == MAX_PKT_BURST) {
247 		ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
248 				cqp->buf, cqp->len);
249 		if (ret < cqp->len) {
250 			RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
251 					" enqueued %u crypto ops out of %u\n",
252 					 cqp->id, cqp->qp,
253 					 ret, cqp->len);
254 			for (i = ret; i < cqp->len; i++)
255 				rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
256 		}
257 		cqp->in_flight += ret;
258 		cqp->len = 0;
259 	}
260 }
261 
262 static inline void
263 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
264 		struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
265 		uint16_t nb_pkts)
266 {
267 	int32_t ret = 0, i;
268 	struct ipsec_mbuf_metadata *priv;
269 	struct rte_crypto_sym_op *sym_cop;
270 	struct ipsec_sa *sa;
271 	struct cdev_qp *cqp;
272 
273 	for (i = 0; i < nb_pkts; i++) {
274 		if (unlikely(sas[i] == NULL)) {
275 			rte_pktmbuf_free(pkts[i]);
276 			continue;
277 		}
278 
279 		rte_prefetch0(sas[i]);
280 		rte_prefetch0(pkts[i]);
281 
282 		priv = get_priv(pkts[i]);
283 		sa = sas[i];
284 		priv->sa = sa;
285 
286 		switch (sa->type) {
287 		case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
288 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
289 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
290 
291 			rte_prefetch0(&priv->sym_cop);
292 
293 			if ((unlikely(sa->sec_session == NULL)) &&
294 					create_session(ipsec_ctx, sa)) {
295 				rte_pktmbuf_free(pkts[i]);
296 				continue;
297 			}
298 
299 			sym_cop = get_sym_cop(&priv->cop);
300 			sym_cop->m_src = pkts[i];
301 
302 			rte_security_attach_session(&priv->cop,
303 					sa->sec_session);
304 			break;
305 		case RTE_SECURITY_ACTION_TYPE_NONE:
306 
307 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
308 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
309 
310 			rte_prefetch0(&priv->sym_cop);
311 
312 			if ((unlikely(sa->crypto_session == NULL)) &&
313 					create_session(ipsec_ctx, sa)) {
314 				rte_pktmbuf_free(pkts[i]);
315 				continue;
316 			}
317 
318 			rte_crypto_op_attach_sym_session(&priv->cop,
319 					sa->crypto_session);
320 
321 			ret = xform_func(pkts[i], sa, &priv->cop);
322 			if (unlikely(ret)) {
323 				rte_pktmbuf_free(pkts[i]);
324 				continue;
325 			}
326 			break;
327 		case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
328 			break;
329 		case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
330 			priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
331 			priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
332 
333 			rte_prefetch0(&priv->sym_cop);
334 
335 			if ((unlikely(sa->sec_session == NULL)) &&
336 					create_session(ipsec_ctx, sa)) {
337 				rte_pktmbuf_free(pkts[i]);
338 				continue;
339 			}
340 
341 			rte_security_attach_session(&priv->cop,
342 					sa->sec_session);
343 
344 			ret = xform_func(pkts[i], sa, &priv->cop);
345 			if (unlikely(ret)) {
346 				rte_pktmbuf_free(pkts[i]);
347 				continue;
348 			}
349 
350 			cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
351 			cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
352 			if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
353 				rte_security_set_pkt_metadata(
354 						sa->security_ctx,
355 						sa->sec_session, pkts[i], NULL);
356 			continue;
357 		}
358 
359 		RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
360 		enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
361 	}
362 }
363 
364 static inline int
365 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
366 	      struct rte_mbuf *pkts[], uint16_t max_pkts)
367 {
368 	int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
369 	struct ipsec_mbuf_metadata *priv;
370 	struct rte_crypto_op *cops[max_pkts];
371 	struct ipsec_sa *sa;
372 	struct rte_mbuf *pkt;
373 
374 	for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
375 		struct cdev_qp *cqp;
376 
377 		cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
378 		if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
379 			ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
380 
381 		while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
382 			pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt];
383 			rte_prefetch0(pkt);
384 			priv = get_priv(pkt);
385 			sa = priv->sa;
386 			ret = xform_func(pkt, sa, &priv->cop);
387 			if (unlikely(ret)) {
388 				rte_pktmbuf_free(pkt);
389 				continue;
390 			}
391 			pkts[nb_pkts++] = pkt;
392 		}
393 
394 		if (cqp->in_flight == 0)
395 			continue;
396 
397 		nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
398 				cops, max_pkts - nb_pkts);
399 
400 		cqp->in_flight -= nb_cops;
401 
402 		for (j = 0; j < nb_cops; j++) {
403 			pkt = cops[j]->sym->m_src;
404 			rte_prefetch0(pkt);
405 
406 			priv = get_priv(pkt);
407 			sa = priv->sa;
408 
409 			RTE_ASSERT(sa != NULL);
410 
411 			if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
412 				ret = xform_func(pkt, sa, cops[j]);
413 				if (unlikely(ret)) {
414 					rte_pktmbuf_free(pkt);
415 					continue;
416 				}
417 			}
418 			pkts[nb_pkts++] = pkt;
419 		}
420 	}
421 
422 	/* return packets */
423 	return nb_pkts;
424 }
425 
426 uint16_t
427 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
428 		uint16_t nb_pkts, uint16_t len)
429 {
430 	struct ipsec_sa *sas[nb_pkts];
431 
432 	inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
433 
434 	ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
435 
436 	return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
437 }
438 
439 uint16_t
440 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
441 		uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
442 {
443 	struct ipsec_sa *sas[nb_pkts];
444 
445 	outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
446 
447 	ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
448 
449 	return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);
450 }
451