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_crypto.h>
11 #include <rte_security.h>
12 #include <rte_cryptodev.h>
13 #include <rte_ethdev.h>
21 set_ipsec_conf(struct ipsec_sa *sa, struct rte_security_ipsec_xform *ipsec)
23 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
24 struct rte_security_ipsec_tunnel_param *tunnel =
26 if (sa->flags == IP4_TUNNEL) {
28 RTE_SECURITY_IPSEC_TUNNEL_IPV4;
29 tunnel->ipv4.ttl = IPDEFTTL;
31 memcpy((uint8_t *)&tunnel->ipv4.src_ip,
32 (uint8_t *)&sa->src.ip.ip4, 4);
34 memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
35 (uint8_t *)&sa->dst.ip.ip4, 4);
37 /* TODO support for Transport and IPV6 tunnel */
42 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
44 struct rte_cryptodev_info cdev_info;
45 unsigned long cdev_id_qp = 0;
47 struct cdev_key key = { 0 };
49 key.lcore_id = (uint8_t)rte_lcore_id();
51 key.cipher_algo = (uint8_t)sa->cipher_algo;
52 key.auth_algo = (uint8_t)sa->auth_algo;
53 key.aead_algo = (uint8_t)sa->aead_algo;
55 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
56 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
57 (void **)&cdev_id_qp);
60 "No cryptodev: core %u, cipher_algo %u, "
61 "auth_algo %u, aead_algo %u\n",
70 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
71 "%u qp %u\n", sa->spi,
72 ipsec_ctx->tbl[cdev_id_qp].id,
73 ipsec_ctx->tbl[cdev_id_qp].qp);
75 if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
76 struct rte_security_session_conf sess_conf = {
77 .action_type = sa->type,
78 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
83 .direction = sa->direction,
84 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
85 .mode = (sa->flags == IP4_TUNNEL ||
86 sa->flags == IP6_TUNNEL) ?
87 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
88 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
90 .crypto_xform = sa->xforms,
95 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
96 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
97 rte_cryptodev_get_sec_ctx(
98 ipsec_ctx->tbl[cdev_id_qp].id);
100 /* Set IPsec parameters in conf */
101 set_ipsec_conf(sa, &(sess_conf.ipsec));
103 sa->sec_session = rte_security_session_create(ctx,
104 &sess_conf, ipsec_ctx->session_pool);
105 if (sa->sec_session == NULL) {
107 "SEC Session init failed: err: %d\n", ret);
110 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
111 struct rte_flow_error err;
112 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
113 rte_eth_dev_get_sec_ctx(
115 const struct rte_security_capability *sec_cap;
117 sa->sec_session = rte_security_session_create(ctx,
118 &sess_conf, ipsec_ctx->session_pool);
119 if (sa->sec_session == NULL) {
121 "SEC Session init failed: err: %d\n", ret);
125 sec_cap = rte_security_capabilities_get(ctx);
127 /* iterate until ESP tunnel*/
128 while (sec_cap->action !=
129 RTE_SECURITY_ACTION_TYPE_NONE) {
131 if (sec_cap->action == sa->type &&
133 RTE_SECURITY_PROTOCOL_IPSEC &&
134 sec_cap->ipsec.mode ==
135 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
136 sec_cap->ipsec.direction == sa->direction)
141 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
143 "No suitable security capability found\n");
147 sa->ol_flags = sec_cap->ol_flags;
148 sa->security_ctx = ctx;
149 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
151 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
152 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
153 if (sa->flags & IP6_TUNNEL) {
154 sa->pattern[1].spec = &sa->ipv6_spec;
155 memcpy(sa->ipv6_spec.hdr.dst_addr,
156 sa->dst.ip.ip6.ip6_b, 16);
157 memcpy(sa->ipv6_spec.hdr.src_addr,
158 sa->src.ip.ip6.ip6_b, 16);
160 sa->pattern[1].spec = &sa->ipv4_spec;
161 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
162 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
165 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
166 sa->pattern[2].spec = &sa->esp_spec;
167 sa->pattern[2].mask = &rte_flow_item_esp_mask;
168 sa->esp_spec.hdr.spi = sa->spi;
170 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
172 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
173 sa->action[0].conf = sa->sec_session;
175 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
177 sa->attr.egress = (sa->direction ==
178 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
179 sa->attr.ingress = (sa->direction ==
180 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
181 sa->flow = rte_flow_create(sa->portid,
182 &sa->attr, sa->pattern, sa->action, &err);
183 if (sa->flow == NULL) {
185 "Failed to create ipsec flow msg: %s\n",
189 } else if (sa->type ==
190 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
191 struct rte_security_ctx *ctx =
192 (struct rte_security_ctx *)
193 rte_eth_dev_get_sec_ctx(sa->portid);
194 const struct rte_security_capability *sec_cap;
198 "Ethernet device doesn't have security features registered\n");
202 /* Set IPsec parameters in conf */
203 set_ipsec_conf(sa, &(sess_conf.ipsec));
205 /* Save SA as userdata for the security session. When
206 * the packet is received, this userdata will be
207 * retrieved using the metadata from the packet.
209 * This is required only for inbound SAs.
212 if (sa->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
213 sess_conf.userdata = (void *) sa;
215 sa->sec_session = rte_security_session_create(ctx,
216 &sess_conf, ipsec_ctx->session_pool);
217 if (sa->sec_session == NULL) {
219 "SEC Session init failed: err: %d\n", ret);
223 sec_cap = rte_security_capabilities_get(ctx);
225 if (sec_cap == NULL) {
227 "No capabilities registered\n");
231 /* iterate until ESP tunnel*/
232 while (sec_cap->action !=
233 RTE_SECURITY_ACTION_TYPE_NONE) {
235 if (sec_cap->action == sa->type &&
237 RTE_SECURITY_PROTOCOL_IPSEC &&
238 sec_cap->ipsec.mode ==
239 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
240 sec_cap->ipsec.direction == sa->direction)
245 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
247 "No suitable security capability found\n");
251 sa->ol_flags = sec_cap->ol_flags;
252 sa->security_ctx = ctx;
255 sa->crypto_session = rte_cryptodev_sym_session_create(
256 ipsec_ctx->session_pool);
257 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
258 sa->crypto_session, sa->xforms,
259 ipsec_ctx->session_pool);
261 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
263 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
264 ret = rte_cryptodev_queue_pair_attach_sym_session(
265 ipsec_ctx->tbl[cdev_id_qp].id,
266 ipsec_ctx->tbl[cdev_id_qp].qp,
270 "Session cannot be attached to qp %u\n",
271 ipsec_ctx->tbl[cdev_id_qp].qp);
276 sa->cdev_id_qp = cdev_id_qp;
282 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
286 cqp->buf[cqp->len++] = cop;
288 if (cqp->len == MAX_PKT_BURST) {
289 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
291 if (ret < cqp->len) {
292 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
293 " enqueued %u crypto ops out of %u\n",
296 for (i = ret; i < cqp->len; i++)
297 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
299 cqp->in_flight += ret;
305 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
306 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
310 struct ipsec_mbuf_metadata *priv;
311 struct rte_crypto_sym_op *sym_cop;
315 for (i = 0; i < nb_pkts; i++) {
316 if (unlikely(sas[i] == NULL)) {
317 rte_pktmbuf_free(pkts[i]);
321 rte_prefetch0(sas[i]);
322 rte_prefetch0(pkts[i]);
324 priv = get_priv(pkts[i]);
329 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
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 sym_cop = get_sym_cop(&priv->cop);
342 sym_cop->m_src = pkts[i];
344 rte_security_attach_session(&priv->cop,
347 case RTE_SECURITY_ACTION_TYPE_NONE:
349 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
350 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
352 rte_prefetch0(&priv->sym_cop);
354 if ((unlikely(sa->crypto_session == NULL)) &&
355 create_session(ipsec_ctx, sa)) {
356 rte_pktmbuf_free(pkts[i]);
360 rte_crypto_op_attach_sym_session(&priv->cop,
363 ret = xform_func(pkts[i], sa, &priv->cop);
365 rte_pktmbuf_free(pkts[i]);
369 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
370 if ((unlikely(sa->sec_session == NULL)) &&
371 create_session(ipsec_ctx, sa)) {
372 rte_pktmbuf_free(pkts[i]);
376 cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
377 cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
378 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
379 rte_security_set_pkt_metadata(
381 sa->sec_session, pkts[i], NULL);
383 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
384 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
385 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
387 rte_prefetch0(&priv->sym_cop);
389 if ((unlikely(sa->sec_session == NULL)) &&
390 create_session(ipsec_ctx, sa)) {
391 rte_pktmbuf_free(pkts[i]);
395 rte_security_attach_session(&priv->cop,
398 ret = xform_func(pkts[i], sa, &priv->cop);
400 rte_pktmbuf_free(pkts[i]);
404 cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
405 cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
406 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
407 rte_security_set_pkt_metadata(
409 sa->sec_session, pkts[i], NULL);
413 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
414 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
419 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
420 struct rte_mbuf *pkts[], uint16_t max_pkts)
422 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
423 struct ipsec_mbuf_metadata *priv;
424 struct rte_crypto_op *cops[max_pkts];
426 struct rte_mbuf *pkt;
428 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts;) {
431 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp];
433 while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
434 pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt];
436 priv = get_priv(pkt);
438 ret = xform_func(pkt, sa, &priv->cop);
440 rte_pktmbuf_free(pkt);
443 pkts[nb_pkts++] = pkt;
446 if (cqp->in_flight == 0) {
447 ipsec_ctx->last_qp++;
448 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
449 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
454 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
455 cops, max_pkts - nb_pkts);
457 cqp->in_flight -= nb_cops;
459 for (j = 0; j < nb_cops; j++) {
460 pkt = cops[j]->sym->m_src;
463 priv = get_priv(pkt);
466 RTE_ASSERT(sa != NULL);
468 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
469 ret = xform_func(pkt, sa, cops[j]);
471 rte_pktmbuf_free(pkt);
475 pkts[nb_pkts++] = pkt;
476 if (cqp->in_flight < max_pkts) {
477 ipsec_ctx->last_qp++;
478 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
479 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
490 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
491 uint16_t nb_pkts, uint16_t len)
493 struct ipsec_sa *sas[nb_pkts];
495 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
497 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
499 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
503 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
504 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
506 struct ipsec_sa *sas[nb_pkts];
508 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
510 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
512 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);