xref: /dpdk/examples/ipsec-secgw/ipsec_process.c (revision f69ed1044230c218c9afd8f1b47b6fe6aa1eeec5)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2016-2020 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_cryptodev.h>
11 #include <rte_ethdev.h>
12 #include <rte_mbuf.h>
13 
14 #include "ipsec.h"
15 
16 #define SATP_OUT_IPV4(t)	\
17 	((((t) & RTE_IPSEC_SATP_MODE_MASK) == RTE_IPSEC_SATP_MODE_TRANS && \
18 	(((t) & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4)) || \
19 	((t) & RTE_IPSEC_SATP_MODE_MASK) == RTE_IPSEC_SATP_MODE_TUNLV4)
20 
21 /* helper routine to free bulk of packets */
22 static inline void
23 free_pkts(struct rte_mbuf *mb[], uint32_t n)
24 {
25 	uint32_t i;
26 
27 	for (i = 0; i != n; i++)
28 		rte_pktmbuf_free(mb[i]);
29 }
30 
31 /* helper routine to free bulk of crypto-ops and related packets */
32 static inline void
33 free_cops(struct rte_crypto_op *cop[], uint32_t n)
34 {
35 	uint32_t i;
36 
37 	for (i = 0; i != n; i++)
38 		rte_pktmbuf_free(cop[i]->sym->m_src);
39 }
40 
41 /* helper routine to enqueue bulk of crypto ops */
42 static inline void
43 enqueue_cop_bulk(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
44 {
45 	uint32_t i, k, len, n;
46 
47 	len = cqp->len;
48 
49 	/*
50 	 * if cqp is empty and we have enough ops,
51 	 * then queue them to the PMD straightway.
52 	 */
53 	if (num >= RTE_DIM(cqp->buf) * 3 / 4 && len == 0) {
54 		n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cop, num);
55 		cqp->in_flight += n;
56 		free_cops(cop + n, num - n);
57 		return;
58 	}
59 
60 	k = 0;
61 
62 	do {
63 		n = RTE_DIM(cqp->buf) - len;
64 		n = RTE_MIN(num - k, n);
65 
66 		/* put packets into cqp */
67 		for (i = 0; i != n; i++)
68 			cqp->buf[len + i] = cop[k + i];
69 
70 		len += n;
71 		k += n;
72 
73 		/* if cqp is full then, enqueue crypto-ops to PMD */
74 		if (len == RTE_DIM(cqp->buf)) {
75 			n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
76 					cqp->buf, len);
77 			cqp->in_flight += n;
78 			free_cops(cqp->buf + n, len - n);
79 			len = 0;
80 		}
81 
82 
83 	} while (k != num);
84 
85 	cqp->len = len;
86 }
87 
88 static inline int
89 fill_ipsec_session(struct rte_ipsec_session *ss, struct ipsec_ctx *ctx,
90 	struct ipsec_sa *sa)
91 {
92 	int32_t rc;
93 
94 	/* setup crypto section */
95 	if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE ||
96 			ss->type == RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) {
97 		RTE_ASSERT(ss->crypto.ses == NULL);
98 		rc = create_lookaside_session(ctx, sa, ss);
99 		if (rc != 0)
100 			return rc;
101 	/* setup session action type */
102 	} else if (ss->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
103 		RTE_ASSERT(ss->security.ses == NULL);
104 		rc = create_lookaside_session(ctx, sa, ss);
105 		if (rc != 0)
106 			return rc;
107 	} else
108 		RTE_ASSERT(0);
109 
110 	rc = rte_ipsec_session_prepare(ss);
111 	if (rc != 0)
112 		memset(ss, 0, sizeof(*ss));
113 
114 	return rc;
115 }
116 
117 /*
118  * group input packets byt the SA they belong to.
119  */
120 static uint32_t
121 sa_group(void *sa_ptr[], struct rte_mbuf *pkts[],
122 	struct rte_ipsec_group grp[], uint32_t num)
123 {
124 	uint32_t i, n, spi;
125 	void *sa;
126 	void * const nosa = &spi;
127 
128 	sa = nosa;
129 	for (i = 0, n = 0; i != num; i++) {
130 
131 		if (sa != sa_ptr[i]) {
132 			grp[n].cnt = pkts + i - grp[n].m;
133 			n += (sa != nosa);
134 			grp[n].id.ptr = sa_ptr[i];
135 			grp[n].m = pkts + i;
136 			sa = sa_ptr[i];
137 		}
138 	}
139 
140 	/* terminate last group */
141 	if (sa != nosa) {
142 		grp[n].cnt = pkts + i - grp[n].m;
143 		n++;
144 	}
145 
146 	return n;
147 }
148 
149 /*
150  * helper function, splits processed packets into ipv4/ipv6 traffic.
151  */
152 static inline void
153 copy_to_trf(struct ipsec_traffic *trf, uint64_t satp, struct rte_mbuf *mb[],
154 	uint32_t num)
155 {
156 	uint32_t j, ofs, s;
157 	struct traffic_type *out;
158 
159 	/*
160 	 * determine traffic type(ipv4/ipv6) and offset for ACL classify
161 	 * based on SA type
162 	 */
163 	if ((satp & RTE_IPSEC_SATP_DIR_MASK) == RTE_IPSEC_SATP_DIR_IB) {
164 		if ((satp & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4) {
165 			out = &trf->ip4;
166 			ofs = offsetof(struct ip, ip_p);
167 		} else {
168 			out = &trf->ip6;
169 			ofs = offsetof(struct ip6_hdr, ip6_nxt);
170 		}
171 	} else if (SATP_OUT_IPV4(satp)) {
172 		out = &trf->ip4;
173 		ofs = offsetof(struct ip, ip_p);
174 	} else {
175 		out = &trf->ip6;
176 		ofs = offsetof(struct ip6_hdr, ip6_nxt);
177 	}
178 
179 	for (j = 0, s = out->num; j != num; j++) {
180 		out->data[s + j] = rte_pktmbuf_mtod_offset(mb[j],
181 				void *, ofs);
182 		out->pkts[s + j] = mb[j];
183 	}
184 
185 	out->num += num;
186 }
187 
188 static uint32_t
189 ipsec_prepare_crypto_group(struct ipsec_ctx *ctx, struct ipsec_sa *sa,
190 		struct rte_ipsec_session *ips, struct rte_mbuf **m,
191 		unsigned int cnt)
192 {
193 	struct cdev_qp *cqp;
194 	struct rte_crypto_op *cop[cnt];
195 	uint32_t j, k;
196 	struct ipsec_mbuf_metadata *priv;
197 
198 	cqp = &ctx->tbl[sa->cdev_id_qp];
199 
200 	/* for that app each mbuf has it's own crypto op */
201 	for (j = 0; j != cnt; j++) {
202 		priv = get_priv(m[j]);
203 		cop[j] = &priv->cop;
204 		/*
205 		 * this is just to satisfy inbound_sa_check()
206 		 * should be removed in future.
207 		 */
208 		priv->sa = sa;
209 	}
210 
211 	/* prepare and enqueue crypto ops */
212 	k = rte_ipsec_pkt_crypto_prepare(ips, m, cop, cnt);
213 	if (k != 0)
214 		enqueue_cop_bulk(cqp, cop, k);
215 
216 	return k;
217 }
218 
219 /*
220  * helper routine for inline and cpu(synchronous) processing
221  * this is just to satisfy inbound_sa_check() and get_hop_for_offload_pkt().
222  * Should be removed in future.
223  */
224 static inline void
225 prep_process_group(void *sa, struct rte_mbuf *mb[], uint32_t cnt)
226 {
227 	uint32_t j;
228 	struct ipsec_mbuf_metadata *priv;
229 
230 	for (j = 0; j != cnt; j++) {
231 		priv = get_priv(mb[j]);
232 		priv->sa = sa;
233 	}
234 }
235 
236 /*
237  * finish processing of packets successfully decrypted by an inline processor
238  */
239 static uint32_t
240 ipsec_process_inline_group(struct rte_ipsec_session *ips, void *sa,
241 	struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t cnt)
242 {
243 	uint64_t satp;
244 	uint32_t k;
245 
246 	/* get SA type */
247 	satp = rte_ipsec_sa_type(ips->sa);
248 	prep_process_group(sa, mb, cnt);
249 
250 	k = rte_ipsec_pkt_process(ips, mb, cnt);
251 	copy_to_trf(trf, satp, mb, k);
252 	return k;
253 }
254 
255 /*
256  * process packets synchronously
257  */
258 static uint32_t
259 ipsec_process_cpu_group(struct rte_ipsec_session *ips, void *sa,
260 	struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t cnt)
261 {
262 	uint64_t satp;
263 	uint32_t k;
264 
265 	/* get SA type */
266 	satp = rte_ipsec_sa_type(ips->sa);
267 	prep_process_group(sa, mb, cnt);
268 
269 	k = rte_ipsec_pkt_cpu_prepare(ips, mb, cnt);
270 	k = rte_ipsec_pkt_process(ips, mb, k);
271 	copy_to_trf(trf, satp, mb, k);
272 	return k;
273 }
274 
275 /*
276  * Process ipsec packets.
277  * If packet belong to SA that is subject of inline-crypto,
278  * then process it immediately.
279  * Otherwise do necessary preparations and queue it to related
280  * crypto-dev queue.
281  */
282 void
283 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
284 {
285 	uint32_t i, k, n;
286 	struct ipsec_sa *sa;
287 	struct rte_ipsec_group *pg;
288 	struct rte_ipsec_session *ips;
289 	struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
290 
291 	n = sa_group(trf->ipsec.saptr, trf->ipsec.pkts, grp, trf->ipsec.num);
292 
293 	for (i = 0; i != n; i++) {
294 
295 		pg = grp + i;
296 		sa = ipsec_mask_saptr(pg->id.ptr);
297 
298 		/* fallback to cryptodev with RX packets which inline
299 		 * processor was unable to process
300 		 */
301 		if (sa != NULL)
302 			ips = (pg->id.val & IPSEC_SA_OFFLOAD_FALLBACK_FLAG) ?
303 				ipsec_get_fallback_session(sa) :
304 				ipsec_get_primary_session(sa);
305 
306 		/* no valid HW session for that SA, try to create one */
307 		if (sa == NULL || (ips->crypto.ses == NULL &&
308 				fill_ipsec_session(ips, ctx, sa) != 0))
309 			k = 0;
310 
311 		/* process packets inline */
312 		else {
313 			switch (ips->type) {
314 			/* enqueue packets to crypto dev */
315 			case RTE_SECURITY_ACTION_TYPE_NONE:
316 			case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
317 				k = ipsec_prepare_crypto_group(ctx, sa, ips,
318 					pg->m, pg->cnt);
319 				break;
320 			case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
321 			case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
322 				k = ipsec_process_inline_group(ips, sa,
323 					trf, pg->m, pg->cnt);
324 				break;
325 			case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
326 				k = ipsec_process_cpu_group(ips, sa,
327 					trf, pg->m, pg->cnt);
328 				break;
329 			default:
330 				k = 0;
331 			}
332 		}
333 
334 		/* drop packets that cannot be enqueued/processed */
335 		if (k != pg->cnt)
336 			free_pkts(pg->m + k, pg->cnt - k);
337 	}
338 }
339 
340 static inline uint32_t
341 cqp_dequeue(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
342 {
343 	uint32_t n;
344 
345 	if (cqp->in_flight == 0)
346 		return 0;
347 
348 	n = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, cop, num);
349 	RTE_ASSERT(cqp->in_flight >= n);
350 	cqp->in_flight -= n;
351 
352 	return n;
353 }
354 
355 static inline uint32_t
356 ctx_dequeue(struct ipsec_ctx *ctx, struct rte_crypto_op *cop[], uint32_t num)
357 {
358 	uint32_t i, n;
359 
360 	n = 0;
361 
362 	for (i = ctx->last_qp; n != num && i != ctx->nb_qps; i++)
363 		n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
364 
365 	for (i = 0; n != num && i != ctx->last_qp; i++)
366 		n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
367 
368 	ctx->last_qp = i;
369 	return n;
370 }
371 
372 /*
373  * dequeue packets from crypto-queues and finalize processing.
374  */
375 void
376 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
377 {
378 	uint64_t satp;
379 	uint32_t i, k, n, ng;
380 	struct rte_ipsec_session *ss;
381 	struct traffic_type *out;
382 	struct rte_ipsec_group *pg;
383 	struct rte_crypto_op *cop[RTE_DIM(trf->ipsec.pkts)];
384 	struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
385 
386 	trf->ip4.num = 0;
387 	trf->ip6.num = 0;
388 
389 	out = &trf->ipsec;
390 
391 	/* dequeue completed crypto-ops */
392 	n = ctx_dequeue(ctx, cop, RTE_DIM(cop));
393 	if (n == 0)
394 		return;
395 
396 	/* group them by ipsec session */
397 	ng = rte_ipsec_pkt_crypto_group((const struct rte_crypto_op **)
398 		(uintptr_t)cop, out->pkts, grp, n);
399 
400 	/* process each group of packets */
401 	for (i = 0; i != ng; i++) {
402 
403 		pg = grp + i;
404 		ss = pg->id.ptr;
405 		satp = rte_ipsec_sa_type(ss->sa);
406 
407 		k = rte_ipsec_pkt_process(ss, pg->m, pg->cnt);
408 		copy_to_trf(trf, satp, pg->m, k);
409 
410 		/* free bad packets, if any */
411 		free_pkts(pg->m + k, pg->cnt - k);
412 
413 		n -= pg->cnt;
414 	}
415 
416 	/* we should never have packet with unknown SA here */
417 	RTE_VERIFY(n == 0);
418 }
419