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,
56 struct rte_ipsec_session *ips)
58 struct rte_cryptodev_info cdev_info;
59 unsigned long cdev_id_qp = 0;
61 struct cdev_key key = { 0 };
63 key.lcore_id = (uint8_t)rte_lcore_id();
65 key.cipher_algo = (uint8_t)sa->cipher_algo;
66 key.auth_algo = (uint8_t)sa->auth_algo;
67 key.aead_algo = (uint8_t)sa->aead_algo;
69 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
70 (void **)&cdev_id_qp);
73 "No cryptodev: core %u, cipher_algo %u, "
74 "auth_algo %u, aead_algo %u\n",
82 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
83 "%u qp %u\n", sa->spi,
84 ipsec_ctx->tbl[cdev_id_qp].id,
85 ipsec_ctx->tbl[cdev_id_qp].qp);
87 if (ips->type != RTE_SECURITY_ACTION_TYPE_NONE) {
88 struct rte_security_session_conf sess_conf = {
89 .action_type = ips->type,
90 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
95 .direction = sa->direction,
96 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
97 .mode = (IS_TUNNEL(sa->flags)) ?
98 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
99 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
101 .crypto_xform = sa->xforms,
106 if (ips->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
107 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
108 rte_cryptodev_get_sec_ctx(
109 ipsec_ctx->tbl[cdev_id_qp].id);
111 /* Set IPsec parameters in conf */
112 set_ipsec_conf(sa, &(sess_conf.ipsec));
114 ips->security.ses = rte_security_session_create(ctx,
115 &sess_conf, ipsec_ctx->session_priv_pool);
116 if (ips->security.ses == NULL) {
118 "SEC Session init failed: err: %d\n", ret);
122 RTE_LOG(ERR, IPSEC, "Inline not supported\n");
126 ips->crypto.ses = rte_cryptodev_sym_session_create(
127 ipsec_ctx->session_pool);
128 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
129 ips->crypto.ses, sa->xforms,
130 ipsec_ctx->session_priv_pool);
132 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
136 sa->cdev_id_qp = cdev_id_qp;
142 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa,
143 struct rte_ipsec_session *ips)
146 struct rte_security_ctx *sec_ctx;
147 struct rte_security_session_conf sess_conf = {
148 .action_type = ips->type,
149 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
154 .direction = sa->direction,
155 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
156 .mode = (sa->flags == IP4_TUNNEL ||
157 sa->flags == IP6_TUNNEL) ?
158 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
159 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
161 .crypto_xform = sa->xforms,
165 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n",
166 sa->spi, sa->portid);
168 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
169 struct rte_flow_error err;
170 const struct rte_security_capability *sec_cap;
173 sec_ctx = (struct rte_security_ctx *)
174 rte_eth_dev_get_sec_ctx(
176 if (sec_ctx == NULL) {
178 " rte_eth_dev_get_sec_ctx failed\n");
182 ips->security.ses = rte_security_session_create(sec_ctx,
183 &sess_conf, skt_ctx->session_pool);
184 if (ips->security.ses == NULL) {
186 "SEC Session init failed: err: %d\n", ret);
190 sec_cap = rte_security_capabilities_get(sec_ctx);
192 /* iterate until ESP tunnel*/
193 while (sec_cap->action != RTE_SECURITY_ACTION_TYPE_NONE) {
194 if (sec_cap->action == ips->type &&
196 RTE_SECURITY_PROTOCOL_IPSEC &&
197 sec_cap->ipsec.mode ==
198 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
199 sec_cap->ipsec.direction == sa->direction)
204 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
206 "No suitable security capability found\n");
210 ips->security.ol_flags = sec_cap->ol_flags;
211 ips->security.ctx = sec_ctx;
212 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
214 if (IS_IP6(sa->flags)) {
215 sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
216 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
217 sa->pattern[1].spec = &sa->ipv6_spec;
219 memcpy(sa->ipv6_spec.hdr.dst_addr,
220 sa->dst.ip.ip6.ip6_b, 16);
221 memcpy(sa->ipv6_spec.hdr.src_addr,
222 sa->src.ip.ip6.ip6_b, 16);
223 } else if (IS_IP4(sa->flags)) {
224 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
225 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
226 sa->pattern[1].spec = &sa->ipv4_spec;
228 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
229 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
232 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
233 sa->pattern[2].spec = &sa->esp_spec;
234 sa->pattern[2].mask = &rte_flow_item_esp_mask;
235 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
237 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
239 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
240 sa->action[0].conf = ips->security.ses;
242 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
244 sa->attr.egress = (sa->direction ==
245 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
246 sa->attr.ingress = (sa->direction ==
247 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
248 if (sa->attr.ingress) {
250 struct rte_eth_rss_conf rss_conf = {
254 struct rte_eth_dev_info dev_info;
255 uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
256 struct rte_flow_action_rss action_rss;
260 ret = rte_eth_dev_info_get(sa->portid, &dev_info);
263 "Error during getting device (port %u) info: %s\n",
264 sa->portid, strerror(-ret));
268 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
270 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
271 sa->action[1].conf = &action_rss;
272 ret = rte_eth_dev_rss_hash_conf_get(sa->portid,
276 "rte_eth_dev_rss_hash_conf_get:ret=%d\n",
280 for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i)
283 action_rss = (struct rte_flow_action_rss){
284 .types = rss_conf.rss_hf,
285 .key_len = rss_conf.rss_key_len,
290 ret = rte_flow_validate(sa->portid, &sa->attr,
291 sa->pattern, sa->action,
296 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
298 &(struct rte_flow_action_queue){
301 ret = rte_flow_validate(sa->portid, &sa->attr,
302 sa->pattern, sa->action,
305 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
306 sa->action[1].conf = NULL;
307 ret = rte_flow_validate(sa->portid, &sa->attr,
308 sa->pattern, sa->action,
311 goto flow_create_failure;
312 } else if (sa->attr.egress &&
313 (ips->security.ol_flags &
314 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
316 RTE_FLOW_ACTION_TYPE_PASSTHRU;
318 RTE_FLOW_ACTION_TYPE_END;
321 sa->flow = rte_flow_create(sa->portid,
322 &sa->attr, sa->pattern, sa->action, &err);
323 if (sa->flow == NULL) {
326 "Failed to create ipsec flow msg: %s\n",
330 } else if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
331 const struct rte_security_capability *sec_cap;
333 sec_ctx = (struct rte_security_ctx *)
334 rte_eth_dev_get_sec_ctx(sa->portid);
336 if (sec_ctx == NULL) {
338 "Ethernet device doesn't have security features registered\n");
342 /* Set IPsec parameters in conf */
343 set_ipsec_conf(sa, &(sess_conf.ipsec));
345 /* Save SA as userdata for the security session. When
346 * the packet is received, this userdata will be
347 * retrieved using the metadata from the packet.
349 * The PMD is expected to set similar metadata for other
350 * operations, like rte_eth_event, which are tied to
351 * security session. In such cases, the userdata could
352 * be obtained to uniquely identify the security
353 * parameters denoted.
356 sess_conf.userdata = (void *) sa;
358 ips->security.ses = rte_security_session_create(sec_ctx,
359 &sess_conf, skt_ctx->session_pool);
360 if (ips->security.ses == NULL) {
362 "SEC Session init failed: err: %d\n", ret);
366 sec_cap = rte_security_capabilities_get(sec_ctx);
367 if (sec_cap == NULL) {
369 "No capabilities registered\n");
373 /* iterate until ESP tunnel*/
374 while (sec_cap->action !=
375 RTE_SECURITY_ACTION_TYPE_NONE) {
376 if (sec_cap->action == ips->type &&
378 RTE_SECURITY_PROTOCOL_IPSEC &&
379 sec_cap->ipsec.mode ==
380 sess_conf.ipsec.mode &&
381 sec_cap->ipsec.direction == sa->direction)
386 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
388 "No suitable security capability found\n");
392 ips->security.ol_flags = sec_cap->ol_flags;
393 ips->security.ctx = sec_ctx;
401 * queue crypto-ops into PMD queue.
404 enqueue_cop_burst(struct cdev_qp *cqp)
406 uint32_t i, len, ret;
409 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
411 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
412 " enqueued %u crypto ops out of %u\n",
413 cqp->id, cqp->qp, ret, len);
414 /* drop packets that we fail to enqueue */
415 for (i = ret; i < len; i++)
416 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
418 cqp->in_flight += ret;
423 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
425 cqp->buf[cqp->len++] = cop;
427 if (cqp->len == MAX_PKT_BURST)
428 enqueue_cop_burst(cqp);
432 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
433 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
437 struct ipsec_mbuf_metadata *priv;
438 struct rte_crypto_sym_op *sym_cop;
440 struct rte_ipsec_session *ips;
442 for (i = 0; i < nb_pkts; i++) {
443 if (unlikely(sas[i] == NULL)) {
444 rte_pktmbuf_free(pkts[i]);
448 rte_prefetch0(sas[i]);
449 rte_prefetch0(pkts[i]);
451 priv = get_priv(pkts[i]);
454 ips = ipsec_get_session(sa);
457 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
458 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
459 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
461 rte_prefetch0(&priv->sym_cop);
463 if ((unlikely(ips->security.ses == NULL)) &&
464 create_lookaside_session(ipsec_ctx, sa, ips)) {
465 rte_pktmbuf_free(pkts[i]);
469 sym_cop = get_sym_cop(&priv->cop);
470 sym_cop->m_src = pkts[i];
472 rte_security_attach_session(&priv->cop,
475 case RTE_SECURITY_ACTION_TYPE_NONE:
477 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
478 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
480 rte_prefetch0(&priv->sym_cop);
482 if ((unlikely(ips->crypto.ses == NULL)) &&
483 create_lookaside_session(ipsec_ctx, sa, ips)) {
484 rte_pktmbuf_free(pkts[i]);
488 rte_crypto_op_attach_sym_session(&priv->cop,
491 ret = xform_func(pkts[i], sa, &priv->cop);
493 rte_pktmbuf_free(pkts[i]);
497 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
498 RTE_ASSERT(ips->security.ses != NULL);
499 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
500 if (ips->security.ol_flags &
501 RTE_SECURITY_TX_OLOAD_NEED_MDATA)
502 rte_security_set_pkt_metadata(
503 ips->security.ctx, ips->security.ses,
506 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
507 RTE_ASSERT(ips->security.ses != NULL);
508 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
509 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
511 rte_prefetch0(&priv->sym_cop);
512 rte_security_attach_session(&priv->cop,
515 ret = xform_func(pkts[i], sa, &priv->cop);
517 rte_pktmbuf_free(pkts[i]);
521 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
522 if (ips->security.ol_flags &
523 RTE_SECURITY_TX_OLOAD_NEED_MDATA)
524 rte_security_set_pkt_metadata(
525 ips->security.ctx, ips->security.ses,
530 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
531 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
535 static inline int32_t
536 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
537 struct rte_mbuf *pkts[], uint16_t max_pkts)
539 int32_t nb_pkts, ret;
540 struct ipsec_mbuf_metadata *priv;
542 struct rte_mbuf *pkt;
545 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
546 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
548 priv = get_priv(pkt);
550 ret = xform_func(pkt, sa, &priv->cop);
552 rte_pktmbuf_free(pkt);
555 pkts[nb_pkts++] = pkt;
562 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
563 struct rte_mbuf *pkts[], uint16_t max_pkts)
565 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
566 struct ipsec_mbuf_metadata *priv;
567 struct rte_crypto_op *cops[max_pkts];
569 struct rte_mbuf *pkt;
571 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
574 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
575 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
576 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
578 if (cqp->in_flight == 0)
581 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
582 cops, max_pkts - nb_pkts);
584 cqp->in_flight -= nb_cops;
586 for (j = 0; j < nb_cops; j++) {
587 pkt = cops[j]->sym->m_src;
590 priv = get_priv(pkt);
593 RTE_ASSERT(sa != NULL);
595 if (ipsec_get_action_type(sa) ==
596 RTE_SECURITY_ACTION_TYPE_NONE) {
597 ret = xform_func(pkt, sa, cops[j]);
599 rte_pktmbuf_free(pkt);
602 } else if (ipsec_get_action_type(sa) ==
603 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
604 if (cops[j]->status) {
605 rte_pktmbuf_free(pkt);
609 pkts[nb_pkts++] = pkt;
618 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
619 uint16_t nb_pkts, uint16_t len)
621 struct ipsec_sa *sas[nb_pkts];
623 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
625 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
627 return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
631 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
634 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
638 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
639 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
641 struct ipsec_sa *sas[nb_pkts];
643 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
645 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
647 return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
651 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
654 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);