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 = 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->flow = rte_flow_create(sa->portid,
202 &sa->attr, sa->pattern, sa->action, &err);
203 if (sa->flow == NULL) {
205 "Failed to create ipsec flow msg: %s\n",
211 sa->crypto_session = rte_cryptodev_sym_session_create(
212 ipsec_ctx->session_pool);
213 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
214 sa->crypto_session, sa->xforms,
215 ipsec_ctx->session_pool);
217 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
219 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
220 ret = rte_cryptodev_queue_pair_attach_sym_session(
221 ipsec_ctx->tbl[cdev_id_qp].id,
222 ipsec_ctx->tbl[cdev_id_qp].qp,
226 "Session cannot be attached to qp %u\n",
227 ipsec_ctx->tbl[cdev_id_qp].qp);
232 sa->cdev_id_qp = cdev_id_qp;
238 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
242 cqp->buf[cqp->len++] = cop;
244 if (cqp->len == MAX_PKT_BURST) {
245 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
247 if (ret < cqp->len) {
248 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
249 " enqueued %u crypto ops out of %u\n",
252 for (i = ret; i < cqp->len; i++)
253 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
255 cqp->in_flight += ret;
261 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
262 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
266 struct ipsec_mbuf_metadata *priv;
267 struct rte_crypto_sym_op *sym_cop;
271 for (i = 0; i < nb_pkts; i++) {
272 if (unlikely(sas[i] == NULL)) {
273 rte_pktmbuf_free(pkts[i]);
277 rte_prefetch0(sas[i]);
278 rte_prefetch0(pkts[i]);
280 priv = get_priv(pkts[i]);
285 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
286 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
287 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
289 rte_prefetch0(&priv->sym_cop);
291 if ((unlikely(sa->sec_session == NULL)) &&
292 create_session(ipsec_ctx, sa)) {
293 rte_pktmbuf_free(pkts[i]);
297 sym_cop = get_sym_cop(&priv->cop);
298 sym_cop->m_src = pkts[i];
300 rte_security_attach_session(&priv->cop,
303 case RTE_SECURITY_ACTION_TYPE_NONE:
305 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
306 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
308 rte_prefetch0(&priv->sym_cop);
310 if ((unlikely(sa->crypto_session == NULL)) &&
311 create_session(ipsec_ctx, sa)) {
312 rte_pktmbuf_free(pkts[i]);
316 rte_crypto_op_attach_sym_session(&priv->cop,
319 ret = xform_func(pkts[i], sa, &priv->cop);
321 rte_pktmbuf_free(pkts[i]);
325 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
327 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
328 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
329 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
331 rte_prefetch0(&priv->sym_cop);
333 if ((unlikely(sa->sec_session == NULL)) &&
334 create_session(ipsec_ctx, sa)) {
335 rte_pktmbuf_free(pkts[i]);
339 rte_security_attach_session(&priv->cop,
342 ret = xform_func(pkts[i], sa, &priv->cop);
344 rte_pktmbuf_free(pkts[i]);
348 cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
349 cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
350 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
351 rte_security_set_pkt_metadata(
353 sa->sec_session, pkts[i], NULL);
357 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
358 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
363 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
364 struct rte_mbuf *pkts[], uint16_t max_pkts)
366 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
367 struct ipsec_mbuf_metadata *priv;
368 struct rte_crypto_op *cops[max_pkts];
370 struct rte_mbuf *pkt;
372 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
375 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
376 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
377 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
379 while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
380 pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt];
382 priv = get_priv(pkt);
384 ret = xform_func(pkt, sa, &priv->cop);
386 rte_pktmbuf_free(pkt);
389 pkts[nb_pkts++] = pkt;
392 if (cqp->in_flight == 0)
395 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
396 cops, max_pkts - nb_pkts);
398 cqp->in_flight -= nb_cops;
400 for (j = 0; j < nb_cops; j++) {
401 pkt = cops[j]->sym->m_src;
404 priv = get_priv(pkt);
407 RTE_ASSERT(sa != NULL);
409 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
410 ret = xform_func(pkt, sa, cops[j]);
412 rte_pktmbuf_free(pkt);
416 pkts[nb_pkts++] = pkt;
425 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
426 uint16_t nb_pkts, uint16_t len)
428 struct ipsec_sa *sas[nb_pkts];
430 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
432 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
434 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
438 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
439 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
441 struct ipsec_sa *sas[nb_pkts];
443 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
445 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
447 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);