4 * Copyright(c) 2016-2017 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
37 #include <rte_branch_prediction.h>
39 #include <rte_crypto.h>
40 #include <rte_cryptodev.h>
48 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
50 struct rte_cryptodev_info cdev_info;
51 unsigned long cdev_id_qp = 0;
53 struct cdev_key key = { 0 };
55 key.lcore_id = (uint8_t)rte_lcore_id();
57 key.cipher_algo = (uint8_t)sa->cipher_algo;
58 key.auth_algo = (uint8_t)sa->auth_algo;
60 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
61 (void **)&cdev_id_qp);
63 RTE_LOG(ERR, IPSEC, "No cryptodev: core %u, cipher_algo %u, "
64 "auth_algo %u\n", key.lcore_id, key.cipher_algo,
69 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
70 "%u qp %u\n", sa->spi,
71 ipsec_ctx->tbl[cdev_id_qp].id,
72 ipsec_ctx->tbl[cdev_id_qp].qp);
74 sa->crypto_session = rte_cryptodev_sym_session_create(
75 ipsec_ctx->session_pool);
76 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
77 sa->crypto_session, sa->xforms,
78 ipsec_ctx->session_pool);
80 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id, &cdev_info);
81 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
82 ret = rte_cryptodev_queue_pair_attach_sym_session(
83 ipsec_ctx->tbl[cdev_id_qp].id,
84 ipsec_ctx->tbl[cdev_id_qp].qp,
88 "Session cannot be attached to qp %u ",
89 ipsec_ctx->tbl[cdev_id_qp].qp);
93 sa->cdev_id_qp = cdev_id_qp;
99 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
103 cqp->buf[cqp->len++] = cop;
105 if (cqp->len == MAX_PKT_BURST) {
106 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
108 if (ret < cqp->len) {
109 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
110 " enqueued %u crypto ops out of %u\n",
113 for (i = ret; i < cqp->len; i++)
114 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
116 cqp->in_flight += ret;
122 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
123 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
127 struct ipsec_mbuf_metadata *priv;
130 for (i = 0; i < nb_pkts; i++) {
131 if (unlikely(sas[i] == NULL)) {
132 rte_pktmbuf_free(pkts[i]);
136 rte_prefetch0(sas[i]);
137 rte_prefetch0(pkts[i]);
139 priv = get_priv(pkts[i]);
143 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
144 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
146 rte_prefetch0(&priv->sym_cop);
148 if ((unlikely(sa->crypto_session == NULL)) &&
149 create_session(ipsec_ctx, sa)) {
150 rte_pktmbuf_free(pkts[i]);
154 rte_crypto_op_attach_sym_session(&priv->cop,
157 ret = xform_func(pkts[i], sa, &priv->cop);
159 rte_pktmbuf_free(pkts[i]);
163 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
164 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
169 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
170 struct rte_mbuf *pkts[], uint16_t max_pkts)
172 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
173 struct ipsec_mbuf_metadata *priv;
174 struct rte_crypto_op *cops[max_pkts];
176 struct rte_mbuf *pkt;
178 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
181 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
182 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
183 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
185 if (cqp->in_flight == 0)
188 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
189 cops, max_pkts - nb_pkts);
191 cqp->in_flight -= nb_cops;
193 for (j = 0; j < nb_cops; j++) {
194 pkt = cops[j]->sym->m_src;
197 priv = get_priv(pkt);
200 RTE_ASSERT(sa != NULL);
202 ret = xform_func(pkt, sa, cops[j]);
204 rte_pktmbuf_free(pkt);
206 pkts[nb_pkts++] = pkt;
215 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
216 uint16_t nb_pkts, uint16_t len)
218 struct ipsec_sa *sas[nb_pkts];
220 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
222 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
224 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
228 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
229 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
231 struct ipsec_sa *sas[nb_pkts];
233 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
235 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
237 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);