1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2020 Intel Corporation
5 #include <netinet/in.h>
6 #include <netinet/ip.h>
8 #include <rte_branch_prediction.h>
10 #include <rte_cryptodev.h>
11 #include <rte_ethdev.h>
15 #include "ipsec-secgw.h"
17 #define SATP_OUT_IPV4(t) \
18 ((((t) & RTE_IPSEC_SATP_MODE_MASK) == RTE_IPSEC_SATP_MODE_TRANS && \
19 (((t) & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4)) || \
20 ((t) & RTE_IPSEC_SATP_MODE_MASK) == RTE_IPSEC_SATP_MODE_TUNLV4)
22 /* helper routine to free bulk of crypto-ops and related packets */
24 free_cops(struct rte_crypto_op *cop[], uint32_t n)
28 for (i = 0; i != n; i++)
29 rte_pktmbuf_free(cop[i]->sym->m_src);
32 /* helper routine to enqueue bulk of crypto ops */
34 enqueue_cop_bulk(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
36 uint32_t i, k, len, n;
41 * if cqp is empty and we have enough ops,
42 * then queue them to the PMD straightway.
44 if (num >= RTE_DIM(cqp->buf) * 3 / 4 && len == 0) {
45 n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cop, num);
47 free_cops(cop + n, num - n);
54 n = RTE_DIM(cqp->buf) - len;
55 n = RTE_MIN(num - k, n);
57 /* put packets into cqp */
58 for (i = 0; i != n; i++)
59 cqp->buf[len + i] = cop[k + i];
64 /* if cqp is full then, enqueue crypto-ops to PMD */
65 if (len == RTE_DIM(cqp->buf)) {
66 n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
69 free_cops(cqp->buf + n, len - n);
80 fill_ipsec_session(struct rte_ipsec_session *ss, struct ipsec_ctx *ctx,
85 /* setup crypto section */
86 if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE ||
87 ss->type == RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) {
88 RTE_ASSERT(ss->crypto.ses == NULL);
89 rc = create_lookaside_session(ctx, sa, ss);
92 /* setup session action type */
93 } else if (ss->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
94 RTE_ASSERT(ss->security.ses == NULL);
95 rc = create_lookaside_session(ctx, sa, ss);
101 rc = rte_ipsec_session_prepare(ss);
103 memset(ss, 0, sizeof(*ss));
109 * group input packets byt the SA they belong to.
112 sa_group(void *sa_ptr[], struct rte_mbuf *pkts[],
113 struct rte_ipsec_group grp[], uint32_t num)
117 void * const nosa = &spi;
121 for (i = 0, n = 0; i != num; i++) {
123 if (sa != sa_ptr[i]) {
124 grp[n].cnt = pkts + i - grp[n].m;
126 grp[n].id.ptr = sa_ptr[i];
132 /* terminate last group */
134 grp[n].cnt = pkts + i - grp[n].m;
142 * helper function, splits processed packets into ipv4/ipv6 traffic.
145 copy_to_trf(struct ipsec_traffic *trf, uint64_t satp, struct rte_mbuf *mb[],
149 struct traffic_type *out;
152 * determine traffic type(ipv4/ipv6) and offset for ACL classify
155 if ((satp & RTE_IPSEC_SATP_DIR_MASK) == RTE_IPSEC_SATP_DIR_IB) {
156 if ((satp & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4) {
158 ofs = offsetof(struct ip, ip_p);
161 ofs = offsetof(struct ip6_hdr, ip6_nxt);
163 } else if (SATP_OUT_IPV4(satp)) {
165 ofs = offsetof(struct ip, ip_p);
168 ofs = offsetof(struct ip6_hdr, ip6_nxt);
171 for (j = 0, s = out->num; j != num; j++) {
172 out->data[s + j] = rte_pktmbuf_mtod_offset(mb[j],
174 out->pkts[s + j] = mb[j];
181 ipsec_prepare_crypto_group(struct ipsec_ctx *ctx, struct ipsec_sa *sa,
182 struct rte_ipsec_session *ips, struct rte_mbuf **m,
186 struct rte_crypto_op *cop[cnt];
188 struct ipsec_mbuf_metadata *priv;
190 cqp = &ctx->tbl[sa->cdev_id_qp];
192 /* for that app each mbuf has it's own crypto op */
193 for (j = 0; j != cnt; j++) {
194 priv = get_priv(m[j]);
197 * this is just to satisfy inbound_sa_check()
198 * should be removed in future.
203 /* prepare and enqueue crypto ops */
204 k = rte_ipsec_pkt_crypto_prepare(ips, m, cop, cnt);
206 enqueue_cop_bulk(cqp, cop, k);
212 * helper routine for inline and cpu(synchronous) processing
213 * this is just to satisfy inbound_sa_check() and get_hop_for_offload_pkt().
214 * Should be removed in future.
217 prep_process_group(void *sa, struct rte_mbuf *mb[], uint32_t cnt)
220 struct ipsec_mbuf_metadata *priv;
222 for (j = 0; j != cnt; j++) {
223 priv = get_priv(mb[j]);
229 * finish processing of packets successfully decrypted by an inline processor
232 ipsec_process_inline_group(struct rte_ipsec_session *ips, void *sa,
233 struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t cnt)
239 satp = rte_ipsec_sa_type(ips->sa);
240 prep_process_group(sa, mb, cnt);
242 k = rte_ipsec_pkt_process(ips, mb, cnt);
243 copy_to_trf(trf, satp, mb, k);
248 * process packets synchronously
251 ipsec_process_cpu_group(struct rte_ipsec_session *ips, void *sa,
252 struct ipsec_traffic *trf, struct rte_mbuf *mb[], uint32_t cnt)
258 satp = rte_ipsec_sa_type(ips->sa);
259 prep_process_group(sa, mb, cnt);
261 k = rte_ipsec_pkt_cpu_prepare(ips, mb, cnt);
262 k = rte_ipsec_pkt_process(ips, mb, k);
263 copy_to_trf(trf, satp, mb, k);
268 * Process ipsec packets.
269 * If packet belong to SA that is subject of inline-crypto,
270 * then process it immediately.
271 * Otherwise do necessary preparations and queue it to related
275 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
279 struct rte_ipsec_group *pg;
280 struct rte_ipsec_session *ips;
281 struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
283 n = sa_group(trf->ipsec.saptr, trf->ipsec.pkts, grp, trf->ipsec.num);
285 for (i = 0; i != n; i++) {
288 sa = ipsec_mask_saptr(pg->id.ptr);
290 /* fallback to cryptodev with RX packets which inline
291 * processor was unable to process
294 ips = (pg->id.val & IPSEC_SA_OFFLOAD_FALLBACK_FLAG) ?
295 ipsec_get_fallback_session(sa) :
296 ipsec_get_primary_session(sa);
298 /* no valid HW session for that SA, try to create one */
299 if (sa == NULL || (ips->crypto.ses == NULL &&
300 fill_ipsec_session(ips, ctx, sa) != 0))
303 /* process packets inline */
306 /* enqueue packets to crypto dev */
307 case RTE_SECURITY_ACTION_TYPE_NONE:
308 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
309 k = ipsec_prepare_crypto_group(ctx, sa, ips,
312 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
313 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
314 k = ipsec_process_inline_group(ips, sa,
315 trf, pg->m, pg->cnt);
317 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO:
318 k = ipsec_process_cpu_group(ips, sa,
319 trf, pg->m, pg->cnt);
326 /* drop packets that cannot be enqueued/processed */
328 free_pkts(pg->m + k, pg->cnt - k);
332 static inline uint32_t
333 cqp_dequeue(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
337 if (cqp->in_flight == 0)
340 n = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, cop, num);
341 RTE_ASSERT(cqp->in_flight >= n);
347 static inline uint32_t
348 ctx_dequeue(struct ipsec_ctx *ctx, struct rte_crypto_op *cop[], uint32_t num)
354 for (i = ctx->last_qp; n != num && i != ctx->nb_qps; i++)
355 n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
357 for (i = 0; n != num && i != ctx->last_qp; i++)
358 n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
365 * dequeue packets from crypto-queues and finalize processing.
368 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
371 uint32_t i, k, n, ng;
372 struct rte_ipsec_session *ss;
373 struct traffic_type *out;
374 struct rte_ipsec_group *pg;
375 struct rte_crypto_op *cop[RTE_DIM(trf->ipsec.pkts)];
376 struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
383 /* dequeue completed crypto-ops */
384 n = ctx_dequeue(ctx, cop, RTE_DIM(cop));
388 /* group them by ipsec session */
389 ng = rte_ipsec_pkt_crypto_group((const struct rte_crypto_op **)
390 (uintptr_t)cop, out->pkts, grp, n);
392 /* process each group of packets */
393 for (i = 0; i != ng; i++) {
397 satp = rte_ipsec_sa_type(ss->sa);
399 k = rte_ipsec_pkt_process(ss, pg->m, pg->cnt);
400 copy_to_trf(trf, satp, pg->m, k);
402 /* free bad packets, if any */
403 free_pkts(pg->m + k, pg->cnt - k);
408 /* we should never have packet with unknown SA here */