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);
37 /* TODO support for Transport and IPV6 tunnel */
39 ipsec->esn_soft_limit = IPSEC_OFFLOAD_ESN_SOFTLIMIT;
43 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
45 struct rte_cryptodev_info cdev_info;
46 unsigned long cdev_id_qp = 0;
48 struct cdev_key key = { 0 };
50 key.lcore_id = (uint8_t)rte_lcore_id();
52 key.cipher_algo = (uint8_t)sa->cipher_algo;
53 key.auth_algo = (uint8_t)sa->auth_algo;
54 key.aead_algo = (uint8_t)sa->aead_algo;
56 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
57 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
58 (void **)&cdev_id_qp);
61 "No cryptodev: core %u, cipher_algo %u, "
62 "auth_algo %u, aead_algo %u\n",
71 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
72 "%u qp %u\n", sa->spi,
73 ipsec_ctx->tbl[cdev_id_qp].id,
74 ipsec_ctx->tbl[cdev_id_qp].qp);
76 if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
77 struct rte_security_session_conf sess_conf = {
78 .action_type = sa->type,
79 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
84 .direction = sa->direction,
85 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
86 .mode = (IS_TUNNEL(sa->flags)) ?
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_priv_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_priv_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 sess_conf.ipsec.mode &&
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 if (IS_IP6(sa->flags)) {
153 sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
154 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
155 sa->pattern[1].spec = &sa->ipv6_spec;
157 memcpy(sa->ipv6_spec.hdr.dst_addr,
158 sa->dst.ip.ip6.ip6_b, 16);
159 memcpy(sa->ipv6_spec.hdr.src_addr,
160 sa->src.ip.ip6.ip6_b, 16);
161 } else if (IS_IP4(sa->flags)) {
162 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
163 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
164 sa->pattern[1].spec = &sa->ipv4_spec;
166 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
167 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
170 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
171 sa->pattern[2].spec = &sa->esp_spec;
172 sa->pattern[2].mask = &rte_flow_item_esp_mask;
173 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
175 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
177 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
178 sa->action[0].conf = sa->sec_session;
180 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
182 sa->attr.egress = (sa->direction ==
183 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
184 sa->attr.ingress = (sa->direction ==
185 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
186 if (sa->attr.ingress) {
188 struct rte_eth_rss_conf rss_conf = {
192 struct rte_eth_dev_info dev_info;
193 uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
194 struct rte_flow_action_rss action_rss;
198 rte_eth_dev_info_get(sa->portid, &dev_info);
199 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
201 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
202 sa->action[1].conf = &action_rss;
203 rte_eth_dev_rss_hash_conf_get(sa->portid,
206 i < dev_info.nb_rx_queues; ++i)
208 action_rss = (struct rte_flow_action_rss){
209 .types = rss_conf.rss_hf,
210 .key_len = rss_conf.rss_key_len,
215 ret = rte_flow_validate(sa->portid, &sa->attr,
216 sa->pattern, sa->action,
221 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
223 &(struct rte_flow_action_queue){
226 ret = rte_flow_validate(sa->portid, &sa->attr,
227 sa->pattern, sa->action,
230 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
231 sa->action[1].conf = NULL;
232 ret = rte_flow_validate(sa->portid, &sa->attr,
233 sa->pattern, sa->action,
236 goto flow_create_failure;
237 } else if (sa->attr.egress &&
239 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
241 RTE_FLOW_ACTION_TYPE_PASSTHRU;
243 RTE_FLOW_ACTION_TYPE_END;
246 sa->flow = rte_flow_create(sa->portid,
247 &sa->attr, sa->pattern, sa->action, &err);
248 if (sa->flow == NULL) {
251 "Failed to create ipsec flow msg: %s\n",
255 } else if (sa->type ==
256 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
257 struct rte_security_ctx *ctx =
258 (struct rte_security_ctx *)
259 rte_eth_dev_get_sec_ctx(sa->portid);
260 const struct rte_security_capability *sec_cap;
264 "Ethernet device doesn't have security features registered\n");
268 /* Set IPsec parameters in conf */
269 set_ipsec_conf(sa, &(sess_conf.ipsec));
271 /* Save SA as userdata for the security session. When
272 * the packet is received, this userdata will be
273 * retrieved using the metadata from the packet.
275 * The PMD is expected to set similar metadata for other
276 * operations, like rte_eth_event, which are tied to
277 * security session. In such cases, the userdata could
278 * be obtained to uniquely identify the security
279 * parameters denoted.
282 sess_conf.userdata = (void *) sa;
284 sa->sec_session = rte_security_session_create(ctx,
285 &sess_conf, ipsec_ctx->session_pool);
286 if (sa->sec_session == NULL) {
288 "SEC Session init failed: err: %d\n", ret);
292 sec_cap = rte_security_capabilities_get(ctx);
294 if (sec_cap == NULL) {
296 "No capabilities registered\n");
300 /* iterate until ESP tunnel*/
301 while (sec_cap->action !=
302 RTE_SECURITY_ACTION_TYPE_NONE) {
304 if (sec_cap->action == sa->type &&
306 RTE_SECURITY_PROTOCOL_IPSEC &&
307 sec_cap->ipsec.mode ==
308 sess_conf.ipsec.mode &&
309 sec_cap->ipsec.direction == sa->direction)
314 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
316 "No suitable security capability found\n");
320 sa->ol_flags = sec_cap->ol_flags;
321 sa->security_ctx = ctx;
324 sa->crypto_session = rte_cryptodev_sym_session_create(
325 ipsec_ctx->session_pool);
326 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
327 sa->crypto_session, sa->xforms,
328 ipsec_ctx->session_priv_pool);
330 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
333 sa->cdev_id_qp = cdev_id_qp;
339 * queue crypto-ops into PMD queue.
342 enqueue_cop_burst(struct cdev_qp *cqp)
344 uint32_t i, len, ret;
347 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
349 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
350 " enqueued %u crypto ops out of %u\n",
351 cqp->id, cqp->qp, ret, len);
352 /* drop packets that we fail to enqueue */
353 for (i = ret; i < len; i++)
354 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
356 cqp->in_flight += ret;
361 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
363 cqp->buf[cqp->len++] = cop;
365 if (cqp->len == MAX_PKT_BURST)
366 enqueue_cop_burst(cqp);
370 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
371 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
375 struct ipsec_mbuf_metadata *priv;
376 struct rte_crypto_sym_op *sym_cop;
379 for (i = 0; i < nb_pkts; i++) {
380 if (unlikely(sas[i] == NULL)) {
381 rte_pktmbuf_free(pkts[i]);
385 rte_prefetch0(sas[i]);
386 rte_prefetch0(pkts[i]);
388 priv = get_priv(pkts[i]);
393 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
394 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
395 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
397 rte_prefetch0(&priv->sym_cop);
399 if ((unlikely(sa->sec_session == NULL)) &&
400 create_session(ipsec_ctx, sa)) {
401 rte_pktmbuf_free(pkts[i]);
405 sym_cop = get_sym_cop(&priv->cop);
406 sym_cop->m_src = pkts[i];
408 rte_security_attach_session(&priv->cop,
411 case RTE_SECURITY_ACTION_TYPE_NONE:
413 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
414 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
416 rte_prefetch0(&priv->sym_cop);
418 if ((unlikely(sa->crypto_session == NULL)) &&
419 create_session(ipsec_ctx, sa)) {
420 rte_pktmbuf_free(pkts[i]);
424 rte_crypto_op_attach_sym_session(&priv->cop,
427 ret = xform_func(pkts[i], sa, &priv->cop);
429 rte_pktmbuf_free(pkts[i]);
433 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
434 if ((unlikely(sa->sec_session == NULL)) &&
435 create_session(ipsec_ctx, sa)) {
436 rte_pktmbuf_free(pkts[i]);
440 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
441 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
442 rte_security_set_pkt_metadata(
444 sa->sec_session, pkts[i], NULL);
446 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
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_session(ipsec_ctx, sa)) {
454 rte_pktmbuf_free(pkts[i]);
458 rte_security_attach_session(&priv->cop,
461 ret = xform_func(pkts[i], sa, &priv->cop);
463 rte_pktmbuf_free(pkts[i]);
467 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
468 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
469 rte_security_set_pkt_metadata(
471 sa->sec_session, pkts[i], NULL);
475 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
476 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
480 static inline int32_t
481 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
482 struct rte_mbuf *pkts[], uint16_t max_pkts)
484 int32_t nb_pkts, ret;
485 struct ipsec_mbuf_metadata *priv;
487 struct rte_mbuf *pkt;
490 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
491 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
493 priv = get_priv(pkt);
495 ret = xform_func(pkt, sa, &priv->cop);
497 rte_pktmbuf_free(pkt);
500 pkts[nb_pkts++] = pkt;
507 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
508 struct rte_mbuf *pkts[], uint16_t max_pkts)
510 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
511 struct ipsec_mbuf_metadata *priv;
512 struct rte_crypto_op *cops[max_pkts];
514 struct rte_mbuf *pkt;
516 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
519 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
520 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
521 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
523 if (cqp->in_flight == 0)
526 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
527 cops, max_pkts - nb_pkts);
529 cqp->in_flight -= nb_cops;
531 for (j = 0; j < nb_cops; j++) {
532 pkt = cops[j]->sym->m_src;
535 priv = get_priv(pkt);
538 RTE_ASSERT(sa != NULL);
540 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
541 ret = xform_func(pkt, sa, cops[j]);
543 rte_pktmbuf_free(pkt);
547 pkts[nb_pkts++] = pkt;
556 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
557 uint16_t nb_pkts, uint16_t len)
559 struct ipsec_sa *sas[nb_pkts];
561 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
563 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
565 return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
569 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
572 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
576 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
577 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
579 struct ipsec_sa *sas[nb_pkts];
581 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
583 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
585 return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
589 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
592 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);