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_security.h>
41 #include <rte_cryptodev.h>
42 #include <rte_ethdev.h>
50 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
52 struct rte_cryptodev_info cdev_info;
53 unsigned long cdev_id_qp = 0;
55 struct cdev_key key = { 0 };
57 key.lcore_id = (uint8_t)rte_lcore_id();
59 key.cipher_algo = (uint8_t)sa->cipher_algo;
60 key.auth_algo = (uint8_t)sa->auth_algo;
61 key.aead_algo = (uint8_t)sa->aead_algo;
63 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
64 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
65 (void **)&cdev_id_qp);
68 "No cryptodev: core %u, cipher_algo %u, "
69 "auth_algo %u, aead_algo %u\n",
78 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
79 "%u qp %u\n", sa->spi,
80 ipsec_ctx->tbl[cdev_id_qp].id,
81 ipsec_ctx->tbl[cdev_id_qp].qp);
83 if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
84 struct rte_security_session_conf sess_conf = {
85 .action_type = sa->type,
86 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
91 .direction = sa->direction,
92 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
93 .mode = (sa->flags == IP4_TUNNEL ||
94 sa->flags == IP6_TUNNEL) ?
95 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
96 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
98 .crypto_xform = sa->xforms
102 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
103 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
104 rte_cryptodev_get_sec_ctx(
105 ipsec_ctx->tbl[cdev_id_qp].id);
107 if (sess_conf.ipsec.mode ==
108 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
109 struct rte_security_ipsec_tunnel_param *tunnel =
110 &sess_conf.ipsec.tunnel;
111 if (sa->flags == IP4_TUNNEL) {
113 RTE_SECURITY_IPSEC_TUNNEL_IPV4;
114 tunnel->ipv4.ttl = IPDEFTTL;
116 memcpy((uint8_t *)&tunnel->ipv4.src_ip,
117 (uint8_t *)&sa->src.ip.ip4, 4);
119 memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
120 (uint8_t *)&sa->dst.ip.ip4, 4);
122 /* TODO support for Transport and IPV6 tunnel */
125 sa->sec_session = rte_security_session_create(ctx,
126 &sess_conf, ipsec_ctx->session_pool);
127 if (sa->sec_session == NULL) {
129 "SEC Session init failed: err: %d\n", ret);
132 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
133 struct rte_flow_error err;
134 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
135 rte_eth_dev_get_sec_ctx(
137 const struct rte_security_capability *sec_cap;
139 sa->sec_session = rte_security_session_create(ctx,
140 &sess_conf, ipsec_ctx->session_pool);
141 if (sa->sec_session == NULL) {
143 "SEC Session init failed: err: %d\n", ret);
147 sec_cap = rte_security_capabilities_get(ctx);
149 /* iterate until ESP tunnel*/
150 while (sec_cap->action !=
151 RTE_SECURITY_ACTION_TYPE_NONE) {
153 if (sec_cap->action == sa->type &&
155 RTE_SECURITY_PROTOCOL_IPSEC &&
156 sec_cap->ipsec.mode ==
157 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
158 sec_cap->ipsec.direction == sa->direction)
163 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
165 "No suitable security capability found\n");
169 sa->ol_flags = sec_cap->ol_flags;
170 sa->security_ctx = ctx;
171 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
173 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
174 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
175 if (sa->flags & IP6_TUNNEL) {
176 sa->pattern[1].spec = &sa->ipv6_spec;
177 memcpy(sa->ipv6_spec.hdr.dst_addr,
178 sa->dst.ip.ip6.ip6_b, 16);
179 memcpy(sa->ipv6_spec.hdr.src_addr,
180 sa->src.ip.ip6.ip6_b, 16);
182 sa->pattern[1].spec = &sa->ipv4_spec;
183 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
184 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
187 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
188 sa->pattern[2].spec = &sa->esp_spec;
189 sa->pattern[2].mask = &rte_flow_item_esp_mask;
190 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
192 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
194 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
195 sa->action[0].conf = sa->sec_session;
197 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
199 sa->attr.egress = (sa->direction ==
200 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
201 sa->attr.ingress = (sa->direction ==
202 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
203 sa->flow = rte_flow_create(sa->portid,
204 &sa->attr, sa->pattern, sa->action, &err);
205 if (sa->flow == NULL) {
207 "Failed to create ipsec flow msg: %s\n",
213 sa->crypto_session = rte_cryptodev_sym_session_create(
214 ipsec_ctx->session_pool);
215 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
216 sa->crypto_session, sa->xforms,
217 ipsec_ctx->session_pool);
219 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
221 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
222 ret = rte_cryptodev_queue_pair_attach_sym_session(
223 ipsec_ctx->tbl[cdev_id_qp].id,
224 ipsec_ctx->tbl[cdev_id_qp].qp,
228 "Session cannot be attached to qp %u\n",
229 ipsec_ctx->tbl[cdev_id_qp].qp);
234 sa->cdev_id_qp = cdev_id_qp;
240 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
244 cqp->buf[cqp->len++] = cop;
246 if (cqp->len == MAX_PKT_BURST) {
247 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
249 if (ret < cqp->len) {
250 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
251 " enqueued %u crypto ops out of %u\n",
254 for (i = ret; i < cqp->len; i++)
255 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
257 cqp->in_flight += ret;
263 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
264 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
268 struct ipsec_mbuf_metadata *priv;
269 struct rte_crypto_sym_op *sym_cop;
273 for (i = 0; i < nb_pkts; i++) {
274 if (unlikely(sas[i] == NULL)) {
275 rte_pktmbuf_free(pkts[i]);
279 rte_prefetch0(sas[i]);
280 rte_prefetch0(pkts[i]);
282 priv = get_priv(pkts[i]);
287 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
288 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
289 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
291 rte_prefetch0(&priv->sym_cop);
293 if ((unlikely(sa->sec_session == NULL)) &&
294 create_session(ipsec_ctx, sa)) {
295 rte_pktmbuf_free(pkts[i]);
299 sym_cop = get_sym_cop(&priv->cop);
300 sym_cop->m_src = pkts[i];
302 rte_security_attach_session(&priv->cop,
305 case RTE_SECURITY_ACTION_TYPE_NONE:
307 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
308 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
310 rte_prefetch0(&priv->sym_cop);
312 if ((unlikely(sa->crypto_session == NULL)) &&
313 create_session(ipsec_ctx, sa)) {
314 rte_pktmbuf_free(pkts[i]);
318 rte_crypto_op_attach_sym_session(&priv->cop,
321 ret = xform_func(pkts[i], sa, &priv->cop);
323 rte_pktmbuf_free(pkts[i]);
327 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
329 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
330 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
331 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
333 rte_prefetch0(&priv->sym_cop);
335 if ((unlikely(sa->sec_session == NULL)) &&
336 create_session(ipsec_ctx, sa)) {
337 rte_pktmbuf_free(pkts[i]);
341 rte_security_attach_session(&priv->cop,
344 ret = xform_func(pkts[i], sa, &priv->cop);
346 rte_pktmbuf_free(pkts[i]);
350 cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
351 cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
352 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
353 rte_security_set_pkt_metadata(
355 sa->sec_session, pkts[i], NULL);
359 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
360 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
365 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
366 struct rte_mbuf *pkts[], uint16_t max_pkts)
368 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
369 struct ipsec_mbuf_metadata *priv;
370 struct rte_crypto_op *cops[max_pkts];
372 struct rte_mbuf *pkt;
374 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
377 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
378 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
379 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
381 while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
382 pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt];
384 priv = get_priv(pkt);
386 ret = xform_func(pkt, sa, &priv->cop);
388 rte_pktmbuf_free(pkt);
391 pkts[nb_pkts++] = pkt;
394 if (cqp->in_flight == 0)
397 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
398 cops, max_pkts - nb_pkts);
400 cqp->in_flight -= nb_cops;
402 for (j = 0; j < nb_cops; j++) {
403 pkt = cops[j]->sym->m_src;
406 priv = get_priv(pkt);
409 RTE_ASSERT(sa != NULL);
411 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
412 ret = xform_func(pkt, sa, cops[j]);
414 rte_pktmbuf_free(pkt);
418 pkts[nb_pkts++] = pkt;
427 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
428 uint16_t nb_pkts, uint16_t len)
430 struct ipsec_sa *sas[nb_pkts];
432 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
434 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
436 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
440 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
441 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
443 struct ipsec_sa *sas[nb_pkts];
445 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
447 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
449 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);