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;
118 sa->sec_session = rte_security_session_create(ctx,
119 &sess_conf, ipsec_ctx->session_pool);
120 if (sa->sec_session == NULL) {
122 "SEC Session init failed: err: %d\n", ret);
126 sec_cap = rte_security_capabilities_get(ctx);
128 /* iterate until ESP tunnel*/
129 while (sec_cap->action !=
130 RTE_SECURITY_ACTION_TYPE_NONE) {
132 if (sec_cap->action == sa->type &&
134 RTE_SECURITY_PROTOCOL_IPSEC &&
135 sec_cap->ipsec.mode ==
136 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
137 sec_cap->ipsec.direction == sa->direction)
142 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
144 "No suitable security capability found\n");
148 sa->ol_flags = sec_cap->ol_flags;
149 sa->security_ctx = ctx;
150 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
152 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
153 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
154 if (sa->flags & IP6_TUNNEL) {
155 sa->pattern[1].spec = &sa->ipv6_spec;
156 memcpy(sa->ipv6_spec.hdr.dst_addr,
157 sa->dst.ip.ip6.ip6_b, 16);
158 memcpy(sa->ipv6_spec.hdr.src_addr,
159 sa->src.ip.ip6.ip6_b, 16);
161 sa->pattern[1].spec = &sa->ipv4_spec;
162 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
163 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
166 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
167 sa->pattern[2].spec = &sa->esp_spec;
168 sa->pattern[2].mask = &rte_flow_item_esp_mask;
169 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
171 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
173 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
174 sa->action[0].conf = sa->sec_session;
176 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
178 sa->attr.egress = (sa->direction ==
179 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
180 sa->attr.ingress = (sa->direction ==
181 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
182 if (sa->attr.ingress) {
184 struct rte_eth_rss_conf rss_conf = {
188 struct rte_eth_dev *eth_dev;
190 struct rte_flow_action_rss rss;
192 const struct rte_eth_rss_conf *rss_conf;
194 uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
200 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
202 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
203 sa->action[1].conf = &action_rss;
204 eth_dev = ctx->device;
205 rte_eth_dev_rss_hash_conf_get(sa->portid,
208 i < eth_dev->data->nb_rx_queues; ++i)
209 if (eth_dev->data->rx_queues[i])
210 action_rss.local.queue[j++] = i;
211 action_rss.local.num = j;
212 action_rss.local.rss_conf = &rss_conf;
213 ret = rte_flow_validate(sa->portid, &sa->attr,
214 sa->pattern, sa->action,
219 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
221 &(struct rte_flow_action_queue){
224 ret = rte_flow_validate(sa->portid, &sa->attr,
225 sa->pattern, sa->action,
228 goto flow_create_failure;
229 } else if (sa->attr.egress &&
231 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
233 RTE_FLOW_ACTION_TYPE_PASSTHRU;
235 RTE_FLOW_ACTION_TYPE_END;
238 sa->flow = rte_flow_create(sa->portid,
239 &sa->attr, sa->pattern, sa->action, &err);
240 if (sa->flow == NULL) {
243 "Failed to create ipsec flow msg: %s\n",
247 } else if (sa->type ==
248 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
249 struct rte_security_ctx *ctx =
250 (struct rte_security_ctx *)
251 rte_eth_dev_get_sec_ctx(sa->portid);
252 const struct rte_security_capability *sec_cap;
256 "Ethernet device doesn't have security features registered\n");
260 /* Set IPsec parameters in conf */
261 set_ipsec_conf(sa, &(sess_conf.ipsec));
263 /* Save SA as userdata for the security session. When
264 * the packet is received, this userdata will be
265 * retrieved using the metadata from the packet.
267 * This is required only for inbound SAs.
270 if (sa->direction == RTE_SECURITY_IPSEC_SA_DIR_INGRESS)
271 sess_conf.userdata = (void *) sa;
273 sa->sec_session = rte_security_session_create(ctx,
274 &sess_conf, ipsec_ctx->session_pool);
275 if (sa->sec_session == NULL) {
277 "SEC Session init failed: err: %d\n", ret);
281 sec_cap = rte_security_capabilities_get(ctx);
283 if (sec_cap == NULL) {
285 "No capabilities registered\n");
289 /* iterate until ESP tunnel*/
290 while (sec_cap->action !=
291 RTE_SECURITY_ACTION_TYPE_NONE) {
293 if (sec_cap->action == sa->type &&
295 RTE_SECURITY_PROTOCOL_IPSEC &&
296 sec_cap->ipsec.mode ==
297 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
298 sec_cap->ipsec.direction == sa->direction)
303 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
305 "No suitable security capability found\n");
309 sa->ol_flags = sec_cap->ol_flags;
310 sa->security_ctx = ctx;
313 sa->crypto_session = rte_cryptodev_sym_session_create(
314 ipsec_ctx->session_pool);
315 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
316 sa->crypto_session, sa->xforms,
317 ipsec_ctx->session_pool);
319 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
321 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
322 ret = rte_cryptodev_queue_pair_attach_sym_session(
323 ipsec_ctx->tbl[cdev_id_qp].id,
324 ipsec_ctx->tbl[cdev_id_qp].qp,
328 "Session cannot be attached to qp %u\n",
329 ipsec_ctx->tbl[cdev_id_qp].qp);
334 sa->cdev_id_qp = cdev_id_qp;
340 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
344 cqp->buf[cqp->len++] = cop;
346 if (cqp->len == MAX_PKT_BURST) {
347 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
349 if (ret < cqp->len) {
350 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
351 " enqueued %u crypto ops out of %u\n",
354 for (i = ret; i < cqp->len; i++)
355 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
357 cqp->in_flight += ret;
363 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
364 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
368 struct ipsec_mbuf_metadata *priv;
369 struct rte_crypto_sym_op *sym_cop;
372 for (i = 0; i < nb_pkts; i++) {
373 if (unlikely(sas[i] == NULL)) {
374 rte_pktmbuf_free(pkts[i]);
378 rte_prefetch0(sas[i]);
379 rte_prefetch0(pkts[i]);
381 priv = get_priv(pkts[i]);
386 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
387 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
388 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
390 rte_prefetch0(&priv->sym_cop);
392 if ((unlikely(sa->sec_session == NULL)) &&
393 create_session(ipsec_ctx, sa)) {
394 rte_pktmbuf_free(pkts[i]);
398 sym_cop = get_sym_cop(&priv->cop);
399 sym_cop->m_src = pkts[i];
401 rte_security_attach_session(&priv->cop,
404 case RTE_SECURITY_ACTION_TYPE_NONE:
406 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
407 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
409 rte_prefetch0(&priv->sym_cop);
411 if ((unlikely(sa->crypto_session == NULL)) &&
412 create_session(ipsec_ctx, sa)) {
413 rte_pktmbuf_free(pkts[i]);
417 rte_crypto_op_attach_sym_session(&priv->cop,
420 ret = xform_func(pkts[i], sa, &priv->cop);
422 rte_pktmbuf_free(pkts[i]);
426 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
427 if ((unlikely(sa->sec_session == NULL)) &&
428 create_session(ipsec_ctx, sa)) {
429 rte_pktmbuf_free(pkts[i]);
433 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
434 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
435 rte_security_set_pkt_metadata(
437 sa->sec_session, pkts[i], NULL);
439 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
440 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
441 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
443 rte_prefetch0(&priv->sym_cop);
445 if ((unlikely(sa->sec_session == NULL)) &&
446 create_session(ipsec_ctx, sa)) {
447 rte_pktmbuf_free(pkts[i]);
451 rte_security_attach_session(&priv->cop,
454 ret = xform_func(pkts[i], sa, &priv->cop);
456 rte_pktmbuf_free(pkts[i]);
460 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
461 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
462 rte_security_set_pkt_metadata(
464 sa->sec_session, pkts[i], NULL);
468 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
469 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
474 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
475 struct rte_mbuf *pkts[], uint16_t max_pkts)
477 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
478 struct ipsec_mbuf_metadata *priv;
479 struct rte_crypto_op *cops[max_pkts];
481 struct rte_mbuf *pkt;
483 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts;) {
485 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp];
487 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
488 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
490 priv = get_priv(pkt);
492 ret = xform_func(pkt, sa, &priv->cop);
494 rte_pktmbuf_free(pkt);
497 pkts[nb_pkts++] = pkt;
500 if (cqp->in_flight == 0) {
501 ipsec_ctx->last_qp++;
502 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
503 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
508 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
509 cops, max_pkts - nb_pkts);
511 cqp->in_flight -= nb_cops;
513 for (j = 0; j < nb_cops; j++) {
514 pkt = cops[j]->sym->m_src;
517 priv = get_priv(pkt);
520 RTE_ASSERT(sa != NULL);
522 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
523 ret = xform_func(pkt, sa, cops[j]);
525 rte_pktmbuf_free(pkt);
529 pkts[nb_pkts++] = pkt;
530 if (cqp->in_flight < max_pkts) {
531 ipsec_ctx->last_qp++;
532 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
533 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
544 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
545 uint16_t nb_pkts, uint16_t len)
547 struct ipsec_sa *sas[nb_pkts];
549 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
551 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
553 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
557 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
558 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
560 struct ipsec_sa *sas[nb_pkts];
562 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
564 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
566 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);