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_lookaside_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 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",
69 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
70 "%u qp %u\n", sa->spi,
71 ipsec_ctx->tbl[cdev_id_qp].id,
72 ipsec_ctx->tbl[cdev_id_qp].qp);
74 if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
75 struct rte_security_session_conf sess_conf = {
76 .action_type = sa->type,
77 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
82 .direction = sa->direction,
83 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
84 .mode = (IS_TUNNEL(sa->flags)) ?
85 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
86 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
88 .crypto_xform = sa->xforms,
93 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
94 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
95 rte_cryptodev_get_sec_ctx(
96 ipsec_ctx->tbl[cdev_id_qp].id);
98 /* Set IPsec parameters in conf */
99 set_ipsec_conf(sa, &(sess_conf.ipsec));
101 sa->sec_session = rte_security_session_create(ctx,
102 &sess_conf, ipsec_ctx->session_priv_pool);
103 if (sa->sec_session == NULL) {
105 "SEC Session init failed: err: %d\n", ret);
109 RTE_LOG(ERR, IPSEC, "Inline not supported\n");
113 sa->crypto_session = rte_cryptodev_sym_session_create(
114 ipsec_ctx->session_pool);
115 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
116 sa->crypto_session, sa->xforms,
117 ipsec_ctx->session_priv_pool);
119 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
123 sa->cdev_id_qp = cdev_id_qp;
129 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa)
132 struct rte_security_ctx *sec_ctx;
133 struct rte_security_session_conf sess_conf = {
134 .action_type = sa->type,
135 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
140 .direction = sa->direction,
141 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
142 .mode = (sa->flags == IP4_TUNNEL ||
143 sa->flags == IP6_TUNNEL) ?
144 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
145 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
147 .crypto_xform = sa->xforms,
151 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n",
152 sa->spi, sa->portid);
154 if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
155 struct rte_flow_error err;
156 const struct rte_security_capability *sec_cap;
159 sec_ctx = (struct rte_security_ctx *)
160 rte_eth_dev_get_sec_ctx(
162 if (sec_ctx == NULL) {
164 " rte_eth_dev_get_sec_ctx failed\n");
168 sa->sec_session = rte_security_session_create(sec_ctx,
169 &sess_conf, skt_ctx->session_pool);
170 if (sa->sec_session == NULL) {
172 "SEC Session init failed: err: %d\n", ret);
176 sec_cap = rte_security_capabilities_get(sec_ctx);
178 /* iterate until ESP tunnel*/
179 while (sec_cap->action != RTE_SECURITY_ACTION_TYPE_NONE) {
180 if (sec_cap->action == sa->type &&
182 RTE_SECURITY_PROTOCOL_IPSEC &&
183 sec_cap->ipsec.mode ==
184 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
185 sec_cap->ipsec.direction == sa->direction)
190 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
192 "No suitable security capability found\n");
196 sa->ol_flags = sec_cap->ol_flags;
197 sa->security_ctx = sec_ctx;
198 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
200 if (IS_IP6(sa->flags)) {
201 sa->pattern[1].mask = &rte_flow_item_ipv6_mask;
202 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6;
203 sa->pattern[1].spec = &sa->ipv6_spec;
205 memcpy(sa->ipv6_spec.hdr.dst_addr,
206 sa->dst.ip.ip6.ip6_b, 16);
207 memcpy(sa->ipv6_spec.hdr.src_addr,
208 sa->src.ip.ip6.ip6_b, 16);
209 } else if (IS_IP4(sa->flags)) {
210 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
211 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
212 sa->pattern[1].spec = &sa->ipv4_spec;
214 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
215 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
218 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
219 sa->pattern[2].spec = &sa->esp_spec;
220 sa->pattern[2].mask = &rte_flow_item_esp_mask;
221 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
223 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
225 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
226 sa->action[0].conf = sa->sec_session;
228 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
230 sa->attr.egress = (sa->direction ==
231 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
232 sa->attr.ingress = (sa->direction ==
233 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
234 if (sa->attr.ingress) {
236 struct rte_eth_rss_conf rss_conf = {
240 struct rte_eth_dev_info dev_info;
241 uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
242 struct rte_flow_action_rss action_rss;
246 rte_eth_dev_info_get(sa->portid, &dev_info);
247 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
249 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
250 sa->action[1].conf = &action_rss;
251 rte_eth_dev_rss_hash_conf_get(sa->portid, &rss_conf);
252 for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i)
255 action_rss = (struct rte_flow_action_rss){
256 .types = rss_conf.rss_hf,
257 .key_len = rss_conf.rss_key_len,
262 ret = rte_flow_validate(sa->portid, &sa->attr,
263 sa->pattern, sa->action,
268 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
270 &(struct rte_flow_action_queue){
273 ret = rte_flow_validate(sa->portid, &sa->attr,
274 sa->pattern, sa->action,
277 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
278 sa->action[1].conf = NULL;
279 ret = rte_flow_validate(sa->portid, &sa->attr,
280 sa->pattern, sa->action,
283 goto flow_create_failure;
284 } else if (sa->attr.egress &&
286 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
288 RTE_FLOW_ACTION_TYPE_PASSTHRU;
290 RTE_FLOW_ACTION_TYPE_END;
293 sa->flow = rte_flow_create(sa->portid,
294 &sa->attr, sa->pattern, sa->action, &err);
295 if (sa->flow == NULL) {
298 "Failed to create ipsec flow msg: %s\n",
302 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
303 const struct rte_security_capability *sec_cap;
305 sec_ctx = (struct rte_security_ctx *)
306 rte_eth_dev_get_sec_ctx(sa->portid);
308 if (sec_ctx == NULL) {
310 "Ethernet device doesn't have security features registered\n");
314 /* Set IPsec parameters in conf */
315 set_ipsec_conf(sa, &(sess_conf.ipsec));
317 /* Save SA as userdata for the security session. When
318 * the packet is received, this userdata will be
319 * retrieved using the metadata from the packet.
321 * The PMD is expected to set similar metadata for other
322 * operations, like rte_eth_event, which are tied to
323 * security session. In such cases, the userdata could
324 * be obtained to uniquely identify the security
325 * parameters denoted.
328 sess_conf.userdata = (void *) sa;
330 sa->sec_session = rte_security_session_create(sec_ctx,
331 &sess_conf, skt_ctx->session_pool);
332 if (sa->sec_session == NULL) {
334 "SEC Session init failed: err: %d\n", ret);
338 sec_cap = rte_security_capabilities_get(sec_ctx);
339 if (sec_cap == NULL) {
341 "No capabilities registered\n");
345 /* iterate until ESP tunnel*/
346 while (sec_cap->action !=
347 RTE_SECURITY_ACTION_TYPE_NONE) {
348 if (sec_cap->action == sa->type &&
350 RTE_SECURITY_PROTOCOL_IPSEC &&
351 sec_cap->ipsec.mode ==
352 sess_conf.ipsec.mode &&
353 sec_cap->ipsec.direction == sa->direction)
358 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
360 "No suitable security capability found\n");
364 sa->ol_flags = sec_cap->ol_flags;
365 sa->security_ctx = sec_ctx;
373 * queue crypto-ops into PMD queue.
376 enqueue_cop_burst(struct cdev_qp *cqp)
378 uint32_t i, len, ret;
381 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len);
383 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
384 " enqueued %u crypto ops out of %u\n",
385 cqp->id, cqp->qp, ret, len);
386 /* drop packets that we fail to enqueue */
387 for (i = ret; i < len; i++)
388 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
390 cqp->in_flight += ret;
395 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
397 cqp->buf[cqp->len++] = cop;
399 if (cqp->len == MAX_PKT_BURST)
400 enqueue_cop_burst(cqp);
404 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
405 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
409 struct ipsec_mbuf_metadata *priv;
410 struct rte_crypto_sym_op *sym_cop;
413 for (i = 0; i < nb_pkts; i++) {
414 if (unlikely(sas[i] == NULL)) {
415 rte_pktmbuf_free(pkts[i]);
419 rte_prefetch0(sas[i]);
420 rte_prefetch0(pkts[i]);
422 priv = get_priv(pkts[i]);
427 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
428 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
429 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
431 rte_prefetch0(&priv->sym_cop);
433 if ((unlikely(sa->sec_session == NULL)) &&
434 create_lookaside_session(ipsec_ctx, sa)) {
435 rte_pktmbuf_free(pkts[i]);
439 sym_cop = get_sym_cop(&priv->cop);
440 sym_cop->m_src = pkts[i];
442 rte_security_attach_session(&priv->cop,
445 case RTE_SECURITY_ACTION_TYPE_NONE:
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->crypto_session == NULL)) &&
453 create_lookaside_session(ipsec_ctx, sa)) {
454 rte_pktmbuf_free(pkts[i]);
458 rte_crypto_op_attach_sym_session(&priv->cop,
461 ret = xform_func(pkts[i], sa, &priv->cop);
463 rte_pktmbuf_free(pkts[i]);
467 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
468 RTE_ASSERT(sa->sec_session != NULL);
469 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
470 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
471 rte_security_set_pkt_metadata(
473 sa->sec_session, pkts[i], NULL);
475 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
476 RTE_ASSERT(sa->sec_session != NULL);
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);
481 rte_security_attach_session(&priv->cop,
484 ret = xform_func(pkts[i], sa, &priv->cop);
486 rte_pktmbuf_free(pkts[i]);
490 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
491 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
492 rte_security_set_pkt_metadata(
494 sa->sec_session, pkts[i], NULL);
498 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
499 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
503 static inline int32_t
504 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
505 struct rte_mbuf *pkts[], uint16_t max_pkts)
507 int32_t nb_pkts, ret;
508 struct ipsec_mbuf_metadata *priv;
510 struct rte_mbuf *pkt;
513 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
514 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
516 priv = get_priv(pkt);
518 ret = xform_func(pkt, sa, &priv->cop);
520 rte_pktmbuf_free(pkt);
523 pkts[nb_pkts++] = pkt;
530 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
531 struct rte_mbuf *pkts[], uint16_t max_pkts)
533 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
534 struct ipsec_mbuf_metadata *priv;
535 struct rte_crypto_op *cops[max_pkts];
537 struct rte_mbuf *pkt;
539 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
542 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
543 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
544 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
546 if (cqp->in_flight == 0)
549 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
550 cops, max_pkts - nb_pkts);
552 cqp->in_flight -= nb_cops;
554 for (j = 0; j < nb_cops; j++) {
555 pkt = cops[j]->sym->m_src;
558 priv = get_priv(pkt);
561 RTE_ASSERT(sa != NULL);
563 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
564 ret = xform_func(pkt, sa, cops[j]);
566 rte_pktmbuf_free(pkt);
570 pkts[nb_pkts++] = pkt;
579 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
580 uint16_t nb_pkts, uint16_t len)
582 struct ipsec_sa *sas[nb_pkts];
584 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
586 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
588 return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len);
592 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
595 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
599 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
600 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
602 struct ipsec_sa *sas[nb_pkts];
604 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
606 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
608 return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len);
612 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
615 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);