1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2016-2017 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>
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)
22 /* helper routine to free bulk of packets */
24 free_pkts(struct rte_mbuf *mb[], uint32_t n)
28 for (i = 0; i != n; i++)
29 rte_pktmbuf_free(mb[i]);
32 /* helper routine to free bulk of crypto-ops and related packets */
34 free_cops(struct rte_crypto_op *cop[], uint32_t n)
38 for (i = 0; i != n; i++)
39 rte_pktmbuf_free(cop[i]->sym->m_src);
42 /* helper routine to enqueue bulk of crypto ops */
44 enqueue_cop_bulk(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
46 uint32_t i, k, len, n;
51 * if cqp is empty and we have enough ops,
52 * then queue them to the PMD straightway.
54 if (num >= RTE_DIM(cqp->buf) * 3 / 4 && len == 0) {
55 n = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cop, num);
57 free_cops(cop + n, num - n);
64 n = RTE_DIM(cqp->buf) - len;
65 n = RTE_MIN(num - k, n);
67 /* put packets into cqp */
68 for (i = 0; i != n; i++)
69 cqp->buf[len + i] = cop[k + i];
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,
79 free_cops(cqp->buf + n, len - n);
90 fill_ipsec_session(struct rte_ipsec_session *ss, struct ipsec_ctx *ctx,
95 /* setup crypto section */
96 if (ss->type == RTE_SECURITY_ACTION_TYPE_NONE) {
97 if (sa->crypto_session == NULL) {
98 rc = create_session(ctx, sa);
102 ss->crypto.ses = sa->crypto_session;
103 /* setup session action type */
105 if (sa->sec_session == NULL) {
106 rc = create_session(ctx, sa);
110 ss->security.ses = sa->sec_session;
111 ss->security.ctx = sa->security_ctx;
112 ss->security.ol_flags = sa->ol_flags;
115 rc = rte_ipsec_session_prepare(ss);
117 memset(ss, 0, sizeof(*ss));
123 * group input packets byt the SA they belong to.
126 sa_group(struct ipsec_sa *sa_ptr[], struct rte_mbuf *pkts[],
127 struct rte_ipsec_group grp[], uint32_t num)
131 void * const nosa = &spi;
134 for (i = 0, n = 0; i != num; i++) {
136 if (sa != sa_ptr[i]) {
137 grp[n].cnt = pkts + i - grp[n].m;
139 grp[n].id.ptr = sa_ptr[i];
145 /* terminate last group */
147 grp[n].cnt = pkts + i - grp[n].m;
155 * helper function, splits processed packets into ipv4/ipv6 traffic.
158 copy_to_trf(struct ipsec_traffic *trf, uint64_t satp, struct rte_mbuf *mb[],
162 struct traffic_type *out;
165 * determine traffic type(ipv4/ipv6) and offset for ACL classify
168 if ((satp & RTE_IPSEC_SATP_DIR_MASK) == RTE_IPSEC_SATP_DIR_IB) {
169 if ((satp & RTE_IPSEC_SATP_IPV_MASK) == RTE_IPSEC_SATP_IPV4) {
171 ofs = offsetof(struct ip, ip_p);
174 ofs = offsetof(struct ip6_hdr, ip6_nxt);
176 } else if (SATP_OUT_IPV4(satp)) {
178 ofs = offsetof(struct ip, ip_p);
181 ofs = offsetof(struct ip6_hdr, ip6_nxt);
184 for (j = 0, s = out->num; j != num; j++) {
185 out->data[s + j] = rte_pktmbuf_mtod_offset(mb[j],
187 out->pkts[s + j] = mb[j];
194 * Process ipsec packets.
195 * If packet belong to SA that is subject of inline-crypto,
196 * then process it immediately.
197 * Otherwise do necessary preparations and queue it to related
201 ipsec_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
206 struct ipsec_mbuf_metadata *priv;
207 struct rte_ipsec_group *pg;
208 struct rte_ipsec_session *ips;
210 struct rte_crypto_op *cop[RTE_DIM(trf->ipsec.pkts)];
211 struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
213 n = sa_group(trf->ipsec.saptr, trf->ipsec.pkts, grp, trf->ipsec.num);
215 for (i = 0; i != n; i++) {
220 /* no valid SA found */
225 satp = rte_ipsec_sa_type(ips->sa);
227 /* no valid HW session for that SA, try to create one */
228 if (ips->crypto.ses == NULL &&
229 fill_ipsec_session(ips, ctx, sa) != 0)
232 /* process packets inline */
233 else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO ||
235 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
238 * This is just to satisfy inbound_sa_check()
239 * and get_hop_for_offload_pkt().
240 * Should be removed in future.
242 for (j = 0; j != pg->cnt; j++) {
243 priv = get_priv(pg->m[j]);
247 k = rte_ipsec_pkt_process(ips, pg->m, pg->cnt);
248 copy_to_trf(trf, satp, pg->m, k);
250 /* enqueue packets to crypto dev */
253 cqp = &ctx->tbl[sa->cdev_id_qp];
255 /* for that app each mbuf has it's own crypto op */
256 for (j = 0; j != pg->cnt; j++) {
257 priv = get_priv(pg->m[j]);
260 * this is just to satisfy inbound_sa_check()
261 * should be removed in future.
266 /* prepare and enqueue crypto ops */
267 k = rte_ipsec_pkt_crypto_prepare(ips, pg->m, cop,
270 enqueue_cop_bulk(cqp, cop, k);
273 /* drop packets that cannot be enqueued/processed */
275 free_pkts(pg->m + k, pg->cnt - k);
279 static inline uint32_t
280 cqp_dequeue(struct cdev_qp *cqp, struct rte_crypto_op *cop[], uint32_t num)
284 if (cqp->in_flight == 0)
287 n = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, cop, num);
288 RTE_ASSERT(cqp->in_flight >= n);
294 static inline uint32_t
295 ctx_dequeue(struct ipsec_ctx *ctx, struct rte_crypto_op *cop[], uint32_t num)
301 for (i = ctx->last_qp; n != num && i != ctx->nb_qps; i++)
302 n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
304 for (i = 0; n != num && i != ctx->last_qp; i++)
305 n += cqp_dequeue(ctx->tbl + i, cop + n, num - n);
312 * dequeue packets from crypto-queues and finalize processing.
315 ipsec_cqp_process(struct ipsec_ctx *ctx, struct ipsec_traffic *trf)
318 uint32_t i, k, n, ng;
319 struct rte_ipsec_session *ss;
320 struct traffic_type *out;
321 struct rte_ipsec_group *pg;
322 struct rte_crypto_op *cop[RTE_DIM(trf->ipsec.pkts)];
323 struct rte_ipsec_group grp[RTE_DIM(trf->ipsec.pkts)];
330 /* dequeue completed crypto-ops */
331 n = ctx_dequeue(ctx, cop, RTE_DIM(cop));
335 /* group them by ipsec session */
336 ng = rte_ipsec_pkt_crypto_group((const struct rte_crypto_op **)
337 (uintptr_t)cop, out->pkts, grp, n);
339 /* process each group of packets */
340 for (i = 0; i != ng; i++) {
344 satp = rte_ipsec_sa_type(ss->sa);
346 k = rte_ipsec_pkt_process(ss, pg->m, pg->cnt);
347 copy_to_trf(trf, satp, pg->m, k);
349 /* free bad packets, if any */
350 free_pkts(pg->m + k, pg->cnt - k);
355 /* we should never have packet with unknown SA here */