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;
52 ipsec->replay_win_sz = app_sa_prm.window_size;
53 ipsec->options.esn = app_sa_prm.enable_esn;
57 create_lookaside_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa,
58 struct rte_ipsec_session *ips)
60 struct rte_cryptodev_info cdev_info;
61 unsigned long cdev_id_qp = 0;
63 struct cdev_key key = { 0 };
65 key.lcore_id = (uint8_t)rte_lcore_id();
67 key.cipher_algo = (uint8_t)sa->cipher_algo;
68 key.auth_algo = (uint8_t)sa->auth_algo;
69 key.aead_algo = (uint8_t)sa->aead_algo;
71 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
72 (void **)&cdev_id_qp);
75 "No cryptodev: core %u, cipher_algo %u, "
76 "auth_algo %u, aead_algo %u\n",
84 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
85 "%u qp %u\n", sa->spi,
86 ipsec_ctx->tbl[cdev_id_qp].id,
87 ipsec_ctx->tbl[cdev_id_qp].qp);
89 if (ips->type != RTE_SECURITY_ACTION_TYPE_NONE) {
90 struct rte_security_session_conf sess_conf = {
91 .action_type = ips->type,
92 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
98 .direction = sa->direction,
99 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
100 .mode = (IS_TUNNEL(sa->flags)) ?
101 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
102 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
104 .crypto_xform = sa->xforms,
109 if (ips->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
110 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
111 rte_cryptodev_get_sec_ctx(
112 ipsec_ctx->tbl[cdev_id_qp].id);
114 /* Set IPsec parameters in conf */
115 set_ipsec_conf(sa, &(sess_conf.ipsec));
117 ips->security.ses = rte_security_session_create(ctx,
118 &sess_conf, ipsec_ctx->session_priv_pool);
119 if (ips->security.ses == NULL) {
121 "SEC Session init failed: err: %d\n", ret);
125 RTE_LOG(ERR, IPSEC, "Inline not supported\n");
129 ips->crypto.ses = rte_cryptodev_sym_session_create(
130 ipsec_ctx->session_pool);
131 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
132 ips->crypto.ses, sa->xforms,
133 ipsec_ctx->session_priv_pool);
135 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
139 sa->cdev_id_qp = cdev_id_qp;
145 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa,
146 struct rte_ipsec_session *ips)
149 struct rte_security_ctx *sec_ctx;
150 struct rte_security_session_conf sess_conf = {
151 .action_type = ips->type,
152 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
158 .direction = sa->direction,
159 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
160 .mode = (sa->flags == IP4_TUNNEL ||
161 sa->flags == IP6_TUNNEL) ?
162 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
163 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
165 .crypto_xform = sa->xforms,
169 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n",
170 sa->spi, sa->portid);
172 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
173 struct rte_flow_error err;
174 const struct rte_security_capability *sec_cap;
177 sec_ctx = (struct rte_security_ctx *)
178 rte_eth_dev_get_sec_ctx(
180 if (sec_ctx == NULL) {
182 " rte_eth_dev_get_sec_ctx failed\n");
186 ips->security.ses = rte_security_session_create(sec_ctx,
187 &sess_conf, skt_ctx->session_pool);
188 if (ips->security.ses == NULL) {
190 "SEC Session init failed: err: %d\n", ret);
194 sec_cap = rte_security_capabilities_get(sec_ctx);
196 /* iterate until ESP tunnel*/
197 while (sec_cap->action != RTE_SECURITY_ACTION_TYPE_NONE) {
198 if (sec_cap->action == ips->type &&
200 RTE_SECURITY_PROTOCOL_IPSEC &&
201 sec_cap->ipsec.mode ==
202 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
203 sec_cap->ipsec.direction == sa->direction)
208 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
210 "No suitable security capability found\n");
214 ips->security.ol_flags = sec_cap->ol_flags;
215 ips->security.ctx = sec_ctx;
216 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
218 if (IS_IP6(sa->flags)) {
219 sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
220 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
221 sa->pattern[1].spec = &sa->ipv6_spec;
223 memcpy(sa->ipv6_spec.hdr.dst_addr,
224 sa->dst.ip.ip6.ip6_b, 16);
225 memcpy(sa->ipv6_spec.hdr.src_addr,
226 sa->src.ip.ip6.ip6_b, 16);
227 } else if (IS_IP4(sa->flags)) {
228 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
229 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
230 sa->pattern[1].spec = &sa->ipv4_spec;
232 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
233 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
236 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
237 sa->pattern[2].spec = &sa->esp_spec;
238 sa->pattern[2].mask = &rte_flow_item_esp_mask;
239 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
241 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
243 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
244 sa->action[0].conf = ips->security.ses;
246 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
248 sa->attr.egress = (sa->direction ==
249 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
250 sa->attr.ingress = (sa->direction ==
251 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
252 if (sa->attr.ingress) {
254 struct rte_eth_rss_conf rss_conf = {
258 struct rte_eth_dev_info dev_info;
259 uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
260 struct rte_flow_action_rss action_rss;
264 ret = rte_eth_dev_info_get(sa->portid, &dev_info);
267 "Error during getting device (port %u) info: %s\n",
268 sa->portid, strerror(-ret));
272 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
274 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
275 sa->action[1].conf = &action_rss;
276 ret = rte_eth_dev_rss_hash_conf_get(sa->portid,
280 "rte_eth_dev_rss_hash_conf_get:ret=%d\n",
284 for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i)
287 action_rss = (struct rte_flow_action_rss){
288 .types = rss_conf.rss_hf,
289 .key_len = rss_conf.rss_key_len,
294 ret = rte_flow_validate(sa->portid, &sa->attr,
295 sa->pattern, sa->action,
300 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
302 &(struct rte_flow_action_queue){
305 ret = rte_flow_validate(sa->portid, &sa->attr,
306 sa->pattern, sa->action,
309 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
310 sa->action[1].conf = NULL;
311 ret = rte_flow_validate(sa->portid, &sa->attr,
312 sa->pattern, sa->action,
315 goto flow_create_failure;
316 } else if (sa->attr.egress &&
317 (ips->security.ol_flags &
318 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
320 RTE_FLOW_ACTION_TYPE_PASSTHRU;
322 RTE_FLOW_ACTION_TYPE_END;
325 sa->flow = rte_flow_create(sa->portid,
326 &sa->attr, sa->pattern, sa->action, &err);
327 if (sa->flow == NULL) {
330 "Failed to create ipsec flow msg: %s\n",
334 } else if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
335 const struct rte_security_capability *sec_cap;
337 sec_ctx = (struct rte_security_ctx *)
338 rte_eth_dev_get_sec_ctx(sa->portid);
340 if (sec_ctx == NULL) {
342 "Ethernet device doesn't have security features registered\n");
346 /* Set IPsec parameters in conf */
347 set_ipsec_conf(sa, &(sess_conf.ipsec));
349 /* Save SA as userdata for the security session. When
350 * the packet is received, this userdata will be
351 * retrieved using the metadata from the packet.
353 * The PMD is expected to set similar metadata for other
354 * operations, like rte_eth_event, which are tied to
355 * security session. In such cases, the userdata could
356 * be obtained to uniquely identify the security
357 * parameters denoted.
360 sess_conf.userdata = (void *) sa;
362 ips->security.ses = rte_security_session_create(sec_ctx,
363 &sess_conf, skt_ctx->session_pool);
364 if (ips->security.ses == NULL) {
366 "SEC Session init failed: err: %d\n", ret);
370 sec_cap = rte_security_capabilities_get(sec_ctx);
371 if (sec_cap == NULL) {
373 "No capabilities registered\n");
377 /* iterate until ESP tunnel*/
378 while (sec_cap->action !=
379 RTE_SECURITY_ACTION_TYPE_NONE) {
380 if (sec_cap->action == ips->type &&
382 RTE_SECURITY_PROTOCOL_IPSEC &&
383 sec_cap->ipsec.mode ==
384 sess_conf.ipsec.mode &&
385 sec_cap->ipsec.direction == sa->direction)
390 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
392 "No suitable security capability found\n");
396 ips->security.ol_flags = sec_cap->ol_flags;
397 ips->security.ctx = sec_ctx;
405 * queue crypto-ops into PMD queue.
408 enqueue_cop_burst(struct cdev_qp *cqp)
410 uint32_t i, len, ret;
413 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
415 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
416 " enqueued %u crypto ops out of %u\n",
417 cqp->id, cqp->qp, ret, len);
418 /* drop packets that we fail to enqueue */
419 for (i = ret; i < len; i++)
420 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
422 cqp->in_flight += ret;
427 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
429 cqp->buf[cqp->len++] = cop;
431 if (cqp->len == MAX_PKT_BURST)
432 enqueue_cop_burst(cqp);
436 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
437 struct rte_mbuf *pkts[], void *sas[],
441 struct ipsec_mbuf_metadata *priv;
442 struct rte_crypto_sym_op *sym_cop;
444 struct rte_ipsec_session *ips;
446 for (i = 0; i < nb_pkts; i++) {
447 if (unlikely(sas[i] == NULL)) {
448 rte_pktmbuf_free(pkts[i]);
452 rte_prefetch0(sas[i]);
453 rte_prefetch0(pkts[i]);
455 priv = get_priv(pkts[i]);
456 sa = ipsec_mask_saptr(sas[i]);
458 ips = ipsec_get_primary_session(sa);
461 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
462 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
463 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
465 rte_prefetch0(&priv->sym_cop);
467 if ((unlikely(ips->security.ses == NULL)) &&
468 create_lookaside_session(ipsec_ctx, sa, ips)) {
469 rte_pktmbuf_free(pkts[i]);
473 sym_cop = get_sym_cop(&priv->cop);
474 sym_cop->m_src = pkts[i];
476 rte_security_attach_session(&priv->cop,
479 case RTE_SECURITY_ACTION_TYPE_NONE:
481 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
482 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
484 rte_prefetch0(&priv->sym_cop);
486 if ((unlikely(ips->crypto.ses == NULL)) &&
487 create_lookaside_session(ipsec_ctx, sa, ips)) {
488 rte_pktmbuf_free(pkts[i]);
492 rte_crypto_op_attach_sym_session(&priv->cop,
495 ret = xform_func(pkts[i], sa, &priv->cop);
497 rte_pktmbuf_free(pkts[i]);
501 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
502 RTE_ASSERT(ips->security.ses != NULL);
503 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
504 if (ips->security.ol_flags &
505 RTE_SECURITY_TX_OLOAD_NEED_MDATA)
506 rte_security_set_pkt_metadata(
507 ips->security.ctx, ips->security.ses,
510 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
511 RTE_ASSERT(ips->security.ses != NULL);
512 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
513 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
515 rte_prefetch0(&priv->sym_cop);
516 rte_security_attach_session(&priv->cop,
519 ret = xform_func(pkts[i], sa, &priv->cop);
521 rte_pktmbuf_free(pkts[i]);
525 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
526 if (ips->security.ol_flags &
527 RTE_SECURITY_TX_OLOAD_NEED_MDATA)
528 rte_security_set_pkt_metadata(
529 ips->security.ctx, ips->security.ses,
534 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
535 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
539 static inline int32_t
540 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
541 struct rte_mbuf *pkts[], uint16_t max_pkts)
543 int32_t nb_pkts, ret;
544 struct ipsec_mbuf_metadata *priv;
546 struct rte_mbuf *pkt;
549 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
550 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
552 priv = get_priv(pkt);
554 ret = xform_func(pkt, sa, &priv->cop);
556 rte_pktmbuf_free(pkt);
559 pkts[nb_pkts++] = pkt;
566 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
567 struct rte_mbuf *pkts[], uint16_t max_pkts)
569 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
570 struct ipsec_mbuf_metadata *priv;
571 struct rte_crypto_op *cops[max_pkts];
573 struct rte_mbuf *pkt;
575 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
578 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
579 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
580 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
582 if (cqp->in_flight == 0)
585 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
586 cops, max_pkts - nb_pkts);
588 cqp->in_flight -= nb_cops;
590 for (j = 0; j < nb_cops; j++) {
591 pkt = cops[j]->sym->m_src;
594 priv = get_priv(pkt);
597 RTE_ASSERT(sa != NULL);
599 if (ipsec_get_action_type(sa) ==
600 RTE_SECURITY_ACTION_TYPE_NONE) {
601 ret = xform_func(pkt, sa, cops[j]);
603 rte_pktmbuf_free(pkt);
606 } else if (ipsec_get_action_type(sa) ==
607 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
608 if (cops[j]->status) {
609 rte_pktmbuf_free(pkt);
613 pkts[nb_pkts++] = pkt;
622 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
623 uint16_t nb_pkts, uint16_t len)
627 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
629 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
631 return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
635 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
638 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
642 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
643 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
647 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
649 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
651 return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
655 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
658 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);