xref: /dpdk/examples/ipsec-secgw/ipsec_process.c (revision 4a67af84)
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_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 
22 /* helper routine to free bulk of packets */
23 static inline void
24 free_pkts(struct rte_mbuf *mb[], uint32_t n)
25 {
26 	uint32_t i;
27 
28 	for (i = 0; i != n; i++)
29 		rte_pktmbuf_free(mb[i]);
30 }
31 
32 /* helper routine to free bulk of crypto-ops and related packets */
33 static inline void
34 free_cops(struct rte_crypto_op *cop[], uint32_t n)
35 {
36 	uint32_t i;
37 
38 	for (i = 0; i != n; i++)
39 		rte_pktmbuf_free(cop[i]->sym->m_src);
40 }
41 
42 /* helper routine to enqueue bulk of crypto ops */
43 static inline void
44 enqueue_cop_bulk(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
45 {
46 	uint32_t i, k, len, n;
47 
48 	len = cqp->len;
49 
50 	/*
51 	 * if cqp is empty and we have enough ops,
52 	 * then queue them to the PMD straightway.
53 	 */
54 	if (num >= RTE_DIM(cqp->buf) * 3 / 4 && len == 0) {
55 		n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cop, num);
56 		cqp->in_flight += n;
57 		free_cops(cop + n, num - n);
58 		return;
59 	}
60 
61 	k = 0;
62 
63 	do {
64 		n = RTE_DIM(cqp->buf) - len;
65 		n = RTE_MIN(num - k, n);
66 
67 		/* put packets into cqp */
68 		for (i = 0; i != n; i++)
69 			cqp->buf[len + i] = cop[k + i];
70 
71 		len += n;
72 		k += n;
73 
74 		/* if cqp is full then, enqueue crypto-ops to PMD */
75 		if (len == RTE_DIM(cqp->buf)) {
76 			n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
77 					cqp->buf, len);
78 			cqp->in_flight += n;
79 			free_cops(cqp->buf + n, len - n);
80 			len = 0;
81 		}
82 
83 
84 	} while (k != num);
85 
86 	cqp->len = len;
87 }
88 
89 static inline int
90 fill_ipsec_session(struct rte_ipsec_session *ss, struct ipsec_ctx *ctx,
91 	struct ipsec_sa *sa)
92 {
93 	int32_t rc;
94 
95 	/* setup crypto section */
96 	if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE) {
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(struct ipsec_sa *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 /*
189  * Process ipsec packets.
190  * If packet belong to SA that is subject of inline-crypto,
191  * then process it immediately.
192  * Otherwise do necessary preparations and queue it to related
193  * crypto-dev queue.
194  */
195 void
196 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
197 {
198 	uint64_t satp;
199 	uint32_t i, j, k, n;
200 	struct ipsec_sa *sa;
201 	struct ipsec_mbuf_metadata *priv;
202 	struct rte_ipsec_group *pg;
203 	struct rte_ipsec_session *ips;
204 	struct cdev_qp *cqp;
205 	struct rte_crypto_op *cop[RTE_DIM(trf->ipsec.pkts)];
206 	struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
207 
208 	n = sa_group(trf->ipsec.saptr, trf->ipsec.pkts, grp, trf->ipsec.num);
209 
210 	for (i = 0; i != n; i++) {
211 
212 		pg = grp + i;
213 		sa = pg->id.ptr;
214 
215 		ips = ipsec_get_session(sa);
216 
217 		/* no valid HW session for that SA, try to create one */
218 		if (sa == NULL || (ips->crypto.ses == NULL &&
219 				fill_ipsec_session(ips, ctx, sa) != 0))
220 			k = 0;
221 
222 		/* process packets inline */
223 		else if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
224 				ips->type ==
225 				RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
226 
227 			satp = rte_ipsec_sa_type(ips->sa);
228 
229 			/*
230 			 * This is just to satisfy inbound_sa_check()
231 			 * and get_hop_for_offload_pkt().
232 			 * Should be removed in future.
233 			 */
234 			for (j = 0; j != pg->cnt; j++) {
235 				priv = get_priv(pg->m[j]);
236 				priv->sa = sa;
237 			}
238 
239 			k = rte_ipsec_pkt_process(ips, pg->m, pg->cnt);
240 			copy_to_trf(trf, satp, pg->m, k);
241 
242 		/* enqueue packets to crypto dev */
243 		} else {
244 
245 			cqp = &ctx->tbl[sa->cdev_id_qp];
246 
247 			/* for that app each mbuf has it's own crypto op */
248 			for (j = 0; j != pg->cnt; j++) {
249 				priv = get_priv(pg->m[j]);
250 				cop[j] = &priv->cop;
251 				/*
252 				 * this is just to satisfy inbound_sa_check()
253 				 * should be removed in future.
254 				 */
255 				priv->sa = sa;
256 			}
257 
258 			/* prepare and enqueue crypto ops */
259 			k = rte_ipsec_pkt_crypto_prepare(ips, pg->m, cop,
260 				pg->cnt);
261 			if (k != 0)
262 				enqueue_cop_bulk(cqp, cop, k);
263 		}
264 
265 		/* drop packets that cannot be enqueued/processed */
266 		if (k != pg->cnt)
267 			free_pkts(pg->m + k, pg->cnt - k);
268 	}
269 }
270 
271 static inline uint32_t
272 cqp_dequeue(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
273 {
274 	uint32_t n;
275 
276 	if (cqp->in_flight == 0)
277 		return 0;
278 
279 	n = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, cop, num);
280 	RTE_ASSERT(cqp->in_flight >= n);
281 	cqp->in_flight -= n;
282 
283 	return n;
284 }
285 
286 static inline uint32_t
287 ctx_dequeue(struct ipsec_ctx *ctx, struct rte_crypto_op *cop[], uint32_t num)
288 {
289 	uint32_t i, n;
290 
291 	n = 0;
292 
293 	for (i = ctx->last_qp; n != num && i != ctx->nb_qps; i++)
294 		n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
295 
296 	for (i = 0; n != num && i != ctx->last_qp; i++)
297 		n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
298 
299 	ctx->last_qp = i;
300 	return n;
301 }
302 
303 /*
304  * dequeue packets from crypto-queues and finalize processing.
305  */
306 void
307 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
308 {
309 	uint64_t satp;
310 	uint32_t i, k, n, ng;
311 	struct rte_ipsec_session *ss;
312 	struct traffic_type *out;
313 	struct rte_ipsec_group *pg;
314 	struct rte_crypto_op *cop[RTE_DIM(trf->ipsec.pkts)];
315 	struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
316 
317 	trf->ip4.num = 0;
318 	trf->ip6.num = 0;
319 
320 	out = &trf->ipsec;
321 
322 	/* dequeue completed crypto-ops */
323 	n = ctx_dequeue(ctx, cop, RTE_DIM(cop));
324 	if (n == 0)
325 		return;
326 
327 	/* group them by ipsec session */
328 	ng = rte_ipsec_pkt_crypto_group((const struct rte_crypto_op **)
329 		(uintptr_t)cop, out->pkts, grp, n);
330 
331 	/* process each group of packets */
332 	for (i = 0; i != ng; i++) {
333 
334 		pg = grp + i;
335 		ss = pg->id.ptr;
336 		satp = rte_ipsec_sa_type(ss->sa);
337 
338 		k = rte_ipsec_pkt_process(ss, pg->m, pg->cnt);
339 		copy_to_trf(trf, satp, pg->m, k);
340 
341 		/* free bad packets, if any */
342 		free_pkts(pg->m + k, pg->cnt - k);
343 
344 		n -= pg->cnt;
345 	}
346 
347 	/* we should never have packet with unknown SA here */
348 	RTE_VERIFY(n == 0);
349 }
350