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