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 (IS_IP4_TUNNEL(sa->flags)) {
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);
36 } else if (IS_IP6_TUNNEL(sa->flags)) {
38 RTE_SECURITY_IPSEC_TUNNEL_IPV6;
39 tunnel->ipv6.hlimit = IPDEFTTL;
40 tunnel->ipv6.dscp = 0;
41 tunnel->ipv6.flabel = 0;
43 memcpy((uint8_t *)&tunnel->ipv6.src_addr,
44 (uint8_t *)&sa->src.ip.ip6.ip6_b, 16);
46 memcpy((uint8_t *)&tunnel->ipv6.dst_addr,
47 (uint8_t *)&sa->dst.ip.ip6.ip6_b, 16);
49 /* TODO support for Transport */
51 ipsec->esn_soft_limit = IPSEC_OFFLOAD_ESN_SOFTLIMIT;
55 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
57 struct rte_cryptodev_info cdev_info;
58 unsigned long cdev_id_qp = 0;
60 struct cdev_key key = { 0 };
62 key.lcore_id = (uint8_t)rte_lcore_id();
64 key.cipher_algo = (uint8_t)sa->cipher_algo;
65 key.auth_algo = (uint8_t)sa->auth_algo;
66 key.aead_algo = (uint8_t)sa->aead_algo;
68 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
69 (void **)&cdev_id_qp);
72 "No cryptodev: core %u, cipher_algo %u, "
73 "auth_algo %u, aead_algo %u\n",
81 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
82 "%u qp %u\n", sa->spi,
83 ipsec_ctx->tbl[cdev_id_qp].id,
84 ipsec_ctx->tbl[cdev_id_qp].qp);
86 if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
87 struct rte_security_session_conf sess_conf = {
88 .action_type = sa->type,
89 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
94 .direction = sa->direction,
95 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
96 .mode = (IS_TUNNEL(sa->flags)) ?
97 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
98 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
100 .crypto_xform = sa->xforms,
105 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
106 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
107 rte_cryptodev_get_sec_ctx(
108 ipsec_ctx->tbl[cdev_id_qp].id);
110 /* Set IPsec parameters in conf */
111 set_ipsec_conf(sa, &(sess_conf.ipsec));
113 sa->sec_session = rte_security_session_create(ctx,
114 &sess_conf, ipsec_ctx->session_priv_pool);
115 if (sa->sec_session == NULL) {
117 "SEC Session init failed: err: %d\n", ret);
121 RTE_LOG(ERR, IPSEC, "Inline not supported\n");
125 sa->crypto_session = rte_cryptodev_sym_session_create(
126 ipsec_ctx->session_pool);
127 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
128 sa->crypto_session, sa->xforms,
129 ipsec_ctx->session_priv_pool);
131 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
135 sa->cdev_id_qp = cdev_id_qp;
141 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa)
144 struct rte_security_ctx *sec_ctx;
145 struct rte_security_session_conf sess_conf = {
146 .action_type = sa->type,
147 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
152 .direction = sa->direction,
153 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
154 .mode = (sa->flags == IP4_TUNNEL ||
155 sa->flags == IP6_TUNNEL) ?
156 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
157 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
159 .crypto_xform = sa->xforms,
163 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n",
164 sa->spi, sa->portid);
166 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
167 struct rte_flow_error err;
168 const struct rte_security_capability *sec_cap;
171 sec_ctx = (struct rte_security_ctx *)
172 rte_eth_dev_get_sec_ctx(
174 if (sec_ctx == NULL) {
176 " rte_eth_dev_get_sec_ctx failed\n");
180 sa->sec_session = rte_security_session_create(sec_ctx,
181 &sess_conf, skt_ctx->session_pool);
182 if (sa->sec_session == NULL) {
184 "SEC Session init failed: err: %d\n", ret);
188 sec_cap = rte_security_capabilities_get(sec_ctx);
190 /* iterate until ESP tunnel*/
191 while (sec_cap->action != RTE_SECURITY_ACTION_TYPE_NONE) {
192 if (sec_cap->action == sa->type &&
194 RTE_SECURITY_PROTOCOL_IPSEC &&
195 sec_cap->ipsec.mode ==
196 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
197 sec_cap->ipsec.direction == sa->direction)
202 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
204 "No suitable security capability found\n");
208 sa->ol_flags = sec_cap->ol_flags;
209 sa->security_ctx = sec_ctx;
210 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
212 if (IS_IP6(sa->flags)) {
213 sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
214 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
215 sa->pattern[1].spec = &sa->ipv6_spec;
217 memcpy(sa->ipv6_spec.hdr.dst_addr,
218 sa->dst.ip.ip6.ip6_b, 16);
219 memcpy(sa->ipv6_spec.hdr.src_addr,
220 sa->src.ip.ip6.ip6_b, 16);
221 } else if (IS_IP4(sa->flags)) {
222 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
223 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
224 sa->pattern[1].spec = &sa->ipv4_spec;
226 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
227 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
230 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
231 sa->pattern[2].spec = &sa->esp_spec;
232 sa->pattern[2].mask = &rte_flow_item_esp_mask;
233 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
235 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
237 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
238 sa->action[0].conf = sa->sec_session;
240 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
242 sa->attr.egress = (sa->direction ==
243 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
244 sa->attr.ingress = (sa->direction ==
245 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
246 if (sa->attr.ingress) {
248 struct rte_eth_rss_conf rss_conf = {
252 struct rte_eth_dev_info dev_info;
253 uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
254 struct rte_flow_action_rss action_rss;
258 rte_eth_dev_info_get(sa->portid, &dev_info);
259 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
261 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
262 sa->action[1].conf = &action_rss;
263 ret = rte_eth_dev_rss_hash_conf_get(sa->portid,
267 "rte_eth_dev_rss_hash_conf_get:ret=%d\n",
271 for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i)
274 action_rss = (struct rte_flow_action_rss){
275 .types = rss_conf.rss_hf,
276 .key_len = rss_conf.rss_key_len,
281 ret = rte_flow_validate(sa->portid, &sa->attr,
282 sa->pattern, sa->action,
287 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
289 &(struct rte_flow_action_queue){
292 ret = rte_flow_validate(sa->portid, &sa->attr,
293 sa->pattern, sa->action,
296 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
297 sa->action[1].conf = NULL;
298 ret = rte_flow_validate(sa->portid, &sa->attr,
299 sa->pattern, sa->action,
302 goto flow_create_failure;
303 } else if (sa->attr.egress &&
305 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
307 RTE_FLOW_ACTION_TYPE_PASSTHRU;
309 RTE_FLOW_ACTION_TYPE_END;
312 sa->flow = rte_flow_create(sa->portid,
313 &sa->attr, sa->pattern, sa->action, &err);
314 if (sa->flow == NULL) {
317 "Failed to create ipsec flow msg: %s\n",
321 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
322 const struct rte_security_capability *sec_cap;
324 sec_ctx = (struct rte_security_ctx *)
325 rte_eth_dev_get_sec_ctx(sa->portid);
327 if (sec_ctx == NULL) {
329 "Ethernet device doesn't have security features registered\n");
333 /* Set IPsec parameters in conf */
334 set_ipsec_conf(sa, &(sess_conf.ipsec));
336 /* Save SA as userdata for the security session. When
337 * the packet is received, this userdata will be
338 * retrieved using the metadata from the packet.
340 * The PMD is expected to set similar metadata for other
341 * operations, like rte_eth_event, which are tied to
342 * security session. In such cases, the userdata could
343 * be obtained to uniquely identify the security
344 * parameters denoted.
347 sess_conf.userdata = (void *) sa;
349 sa->sec_session = rte_security_session_create(sec_ctx,
350 &sess_conf, skt_ctx->session_pool);
351 if (sa->sec_session == NULL) {
353 "SEC Session init failed: err: %d\n", ret);
357 sec_cap = rte_security_capabilities_get(sec_ctx);
358 if (sec_cap == NULL) {
360 "No capabilities registered\n");
364 /* iterate until ESP tunnel*/
365 while (sec_cap->action !=
366 RTE_SECURITY_ACTION_TYPE_NONE) {
367 if (sec_cap->action == sa->type &&
369 RTE_SECURITY_PROTOCOL_IPSEC &&
370 sec_cap->ipsec.mode ==
371 sess_conf.ipsec.mode &&
372 sec_cap->ipsec.direction == sa->direction)
377 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
379 "No suitable security capability found\n");
383 sa->ol_flags = sec_cap->ol_flags;
384 sa->security_ctx = sec_ctx;
392 * queue crypto-ops into PMD queue.
395 enqueue_cop_burst(struct cdev_qp *cqp)
397 uint32_t i, len, ret;
400 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
402 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
403 " enqueued %u crypto ops out of %u\n",
404 cqp->id, cqp->qp, ret, len);
405 /* drop packets that we fail to enqueue */
406 for (i = ret; i < len; i++)
407 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
409 cqp->in_flight += ret;
414 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
416 cqp->buf[cqp->len++] = cop;
418 if (cqp->len == MAX_PKT_BURST)
419 enqueue_cop_burst(cqp);
423 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
424 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
428 struct ipsec_mbuf_metadata *priv;
429 struct rte_crypto_sym_op *sym_cop;
432 for (i = 0; i < nb_pkts; i++) {
433 if (unlikely(sas[i] == NULL)) {
434 rte_pktmbuf_free(pkts[i]);
438 rte_prefetch0(sas[i]);
439 rte_prefetch0(pkts[i]);
441 priv = get_priv(pkts[i]);
446 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
447 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
448 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
450 rte_prefetch0(&priv->sym_cop);
452 if ((unlikely(sa->sec_session == NULL)) &&
453 create_lookaside_session(ipsec_ctx, sa)) {
454 rte_pktmbuf_free(pkts[i]);
458 sym_cop = get_sym_cop(&priv->cop);
459 sym_cop->m_src = pkts[i];
461 rte_security_attach_session(&priv->cop,
464 case RTE_SECURITY_ACTION_TYPE_NONE:
466 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
467 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
469 rte_prefetch0(&priv->sym_cop);
471 if ((unlikely(sa->crypto_session == NULL)) &&
472 create_lookaside_session(ipsec_ctx, sa)) {
473 rte_pktmbuf_free(pkts[i]);
477 rte_crypto_op_attach_sym_session(&priv->cop,
480 ret = xform_func(pkts[i], sa, &priv->cop);
482 rte_pktmbuf_free(pkts[i]);
486 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
487 RTE_ASSERT(sa->sec_session != NULL);
488 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
489 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
490 rte_security_set_pkt_metadata(
492 sa->sec_session, pkts[i], NULL);
494 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
495 RTE_ASSERT(sa->sec_session != NULL);
496 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
497 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
499 rte_prefetch0(&priv->sym_cop);
500 rte_security_attach_session(&priv->cop,
503 ret = xform_func(pkts[i], sa, &priv->cop);
505 rte_pktmbuf_free(pkts[i]);
509 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
510 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
511 rte_security_set_pkt_metadata(
513 sa->sec_session, pkts[i], NULL);
517 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
518 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
522 static inline int32_t
523 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
524 struct rte_mbuf *pkts[], uint16_t max_pkts)
526 int32_t nb_pkts, ret;
527 struct ipsec_mbuf_metadata *priv;
529 struct rte_mbuf *pkt;
532 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
533 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
535 priv = get_priv(pkt);
537 ret = xform_func(pkt, sa, &priv->cop);
539 rte_pktmbuf_free(pkt);
542 pkts[nb_pkts++] = pkt;
549 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
550 struct rte_mbuf *pkts[], uint16_t max_pkts)
552 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
553 struct ipsec_mbuf_metadata *priv;
554 struct rte_crypto_op *cops[max_pkts];
556 struct rte_mbuf *pkt;
558 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
561 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
562 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
563 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
565 if (cqp->in_flight == 0)
568 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
569 cops, max_pkts - nb_pkts);
571 cqp->in_flight -= nb_cops;
573 for (j = 0; j < nb_cops; j++) {
574 pkt = cops[j]->sym->m_src;
577 priv = get_priv(pkt);
580 RTE_ASSERT(sa != NULL);
582 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
583 ret = xform_func(pkt, sa, cops[j]);
585 rte_pktmbuf_free(pkt);
588 } else if (sa->type ==
589 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
590 if (cops[j]->status) {
591 rte_pktmbuf_free(pkt);
595 pkts[nb_pkts++] = pkt;
604 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
605 uint16_t nb_pkts, uint16_t len)
607 struct ipsec_sa *sas[nb_pkts];
609 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
611 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
613 return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
617 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
620 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
624 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
625 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
627 struct ipsec_sa *sas[nb_pkts];
629 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
631 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
633 return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
637 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
640 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);