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 (sa->flags == IP4_TUNNEL) {
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 = (sa->flags == IP4_TUNNEL ||
87 sa->flags == IP6_TUNNEL) ?
88 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
89 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
91 .crypto_xform = sa->xforms,
96 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
97 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
98 rte_cryptodev_get_sec_ctx(
99 ipsec_ctx->tbl[cdev_id_qp].id);
101 /* Set IPsec parameters in conf */
102 set_ipsec_conf(sa, &(sess_conf.ipsec));
104 sa->sec_session = rte_security_session_create(ctx,
105 &sess_conf, ipsec_ctx->session_pool);
106 if (sa->sec_session == NULL) {
108 "SEC Session init failed: err: %d\n", ret);
111 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
112 struct rte_flow_error err;
113 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
114 rte_eth_dev_get_sec_ctx(
116 const struct rte_security_capability *sec_cap;
119 sa->sec_session = rte_security_session_create(ctx,
120 &sess_conf, ipsec_ctx->session_pool);
121 if (sa->sec_session == NULL) {
123 "SEC Session init failed: err: %d\n", ret);
127 sec_cap = rte_security_capabilities_get(ctx);
129 /* iterate until ESP tunnel*/
130 while (sec_cap->action !=
131 RTE_SECURITY_ACTION_TYPE_NONE) {
133 if (sec_cap->action == sa->type &&
135 RTE_SECURITY_PROTOCOL_IPSEC &&
136 sec_cap->ipsec.mode ==
137 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
138 sec_cap->ipsec.direction == sa->direction)
143 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
145 "No suitable security capability found\n");
149 sa->ol_flags = sec_cap->ol_flags;
150 sa->security_ctx = ctx;
151 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
153 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
154 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
155 if (sa->flags & IP6_TUNNEL) {
156 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);
162 sa->pattern[1].spec = &sa->ipv4_spec;
163 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
164 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
167 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
168 sa->pattern[2].spec = &sa->esp_spec;
169 sa->pattern[2].mask = &rte_flow_item_esp_mask;
170 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi);
172 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
174 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
175 sa->action[0].conf = sa->sec_session;
177 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
179 sa->attr.egress = (sa->direction ==
180 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
181 sa->attr.ingress = (sa->direction ==
182 RTE_SECURITY_IPSEC_SA_DIR_INGRESS);
183 if (sa->attr.ingress) {
185 struct rte_eth_rss_conf rss_conf = {
189 struct rte_eth_dev *eth_dev;
191 struct rte_flow_action_rss rss;
193 const struct rte_eth_rss_conf *rss_conf;
195 uint16_t queue[RTE_MAX_QUEUES_PER_PORT];
201 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END;
203 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS;
204 sa->action[1].conf = &action_rss;
205 eth_dev = ctx->device;
206 rte_eth_dev_rss_hash_conf_get(sa->portid,
209 i < eth_dev->data->nb_rx_queues; ++i)
210 if (eth_dev->data->rx_queues[i])
211 action_rss.local.queue[j++] = i;
212 action_rss.local.num = j;
213 action_rss.local.rss_conf = &rss_conf;
214 ret = rte_flow_validate(sa->portid, &sa->attr,
215 sa->pattern, sa->action,
220 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE;
222 &(struct rte_flow_action_queue){
225 ret = rte_flow_validate(sa->portid, &sa->attr,
226 sa->pattern, sa->action,
229 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
230 sa->action[1].conf = NULL;
231 ret = rte_flow_validate(sa->portid, &sa->attr,
232 sa->pattern, sa->action,
235 goto flow_create_failure;
236 } else if (sa->attr.egress &&
238 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) {
240 RTE_FLOW_ACTION_TYPE_PASSTHRU;
242 RTE_FLOW_ACTION_TYPE_END;
245 sa->flow = rte_flow_create(sa->portid,
246 &sa->attr, sa->pattern, sa->action, &err);
247 if (sa->flow == NULL) {
250 "Failed to create ipsec flow msg: %s\n",
254 } else if (sa->type ==
255 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) {
256 struct rte_security_ctx *ctx =
257 (struct rte_security_ctx *)
258 rte_eth_dev_get_sec_ctx(sa->portid);
259 const struct rte_security_capability *sec_cap;
263 "Ethernet device doesn't have security features registered\n");
267 /* Set IPsec parameters in conf */
268 set_ipsec_conf(sa, &(sess_conf.ipsec));
270 /* Save SA as userdata for the security session. When
271 * the packet is received, this userdata will be
272 * retrieved using the metadata from the packet.
274 * The PMD is expected to set similar metadata for other
275 * operations, like rte_eth_event, which are tied to
276 * security session. In such cases, the userdata could
277 * be obtained to uniquely identify the security
278 * parameters denoted.
281 sess_conf.userdata = (void *) sa;
283 sa->sec_session = rte_security_session_create(ctx,
284 &sess_conf, ipsec_ctx->session_pool);
285 if (sa->sec_session == NULL) {
287 "SEC Session init failed: err: %d\n", ret);
291 sec_cap = rte_security_capabilities_get(ctx);
293 if (sec_cap == NULL) {
295 "No capabilities registered\n");
299 /* iterate until ESP tunnel*/
300 while (sec_cap->action !=
301 RTE_SECURITY_ACTION_TYPE_NONE) {
303 if (sec_cap->action == sa->type &&
305 RTE_SECURITY_PROTOCOL_IPSEC &&
306 sec_cap->ipsec.mode ==
307 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
308 sec_cap->ipsec.direction == sa->direction)
313 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
315 "No suitable security capability found\n");
319 sa->ol_flags = sec_cap->ol_flags;
320 sa->security_ctx = ctx;
323 sa->crypto_session = rte_cryptodev_sym_session_create(
324 ipsec_ctx->session_pool);
325 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
326 sa->crypto_session, sa->xforms,
327 ipsec_ctx->session_pool);
329 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
331 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
332 ret = rte_cryptodev_queue_pair_attach_sym_session(
333 ipsec_ctx->tbl[cdev_id_qp].id,
334 ipsec_ctx->tbl[cdev_id_qp].qp,
338 "Session cannot be attached to qp %u\n",
339 ipsec_ctx->tbl[cdev_id_qp].qp);
344 sa->cdev_id_qp = cdev_id_qp;
350 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
354 cqp->buf[cqp->len++] = cop;
356 if (cqp->len == MAX_PKT_BURST) {
357 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
359 if (ret < cqp->len) {
360 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
361 " enqueued %u crypto ops out of %u\n",
364 for (i = ret; i < cqp->len; i++)
365 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
367 cqp->in_flight += ret;
373 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
374 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
378 struct ipsec_mbuf_metadata *priv;
379 struct rte_crypto_sym_op *sym_cop;
382 for (i = 0; i < nb_pkts; i++) {
383 if (unlikely(sas[i] == NULL)) {
384 rte_pktmbuf_free(pkts[i]);
388 rte_prefetch0(sas[i]);
389 rte_prefetch0(pkts[i]);
391 priv = get_priv(pkts[i]);
396 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
397 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
398 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
400 rte_prefetch0(&priv->sym_cop);
402 if ((unlikely(sa->sec_session == NULL)) &&
403 create_session(ipsec_ctx, sa)) {
404 rte_pktmbuf_free(pkts[i]);
408 sym_cop = get_sym_cop(&priv->cop);
409 sym_cop->m_src = pkts[i];
411 rte_security_attach_session(&priv->cop,
414 case RTE_SECURITY_ACTION_TYPE_NONE:
416 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
417 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
419 rte_prefetch0(&priv->sym_cop);
421 if ((unlikely(sa->crypto_session == NULL)) &&
422 create_session(ipsec_ctx, sa)) {
423 rte_pktmbuf_free(pkts[i]);
427 rte_crypto_op_attach_sym_session(&priv->cop,
430 ret = xform_func(pkts[i], sa, &priv->cop);
432 rte_pktmbuf_free(pkts[i]);
436 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
437 if ((unlikely(sa->sec_session == NULL)) &&
438 create_session(ipsec_ctx, sa)) {
439 rte_pktmbuf_free(pkts[i]);
443 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
444 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
445 rte_security_set_pkt_metadata(
447 sa->sec_session, pkts[i], NULL);
449 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
450 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
451 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
453 rte_prefetch0(&priv->sym_cop);
455 if ((unlikely(sa->sec_session == NULL)) &&
456 create_session(ipsec_ctx, sa)) {
457 rte_pktmbuf_free(pkts[i]);
461 rte_security_attach_session(&priv->cop,
464 ret = xform_func(pkts[i], sa, &priv->cop);
466 rte_pktmbuf_free(pkts[i]);
470 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i];
471 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
472 rte_security_set_pkt_metadata(
474 sa->sec_session, pkts[i], NULL);
478 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
479 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
484 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
485 struct rte_mbuf *pkts[], uint16_t max_pkts)
487 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
488 struct ipsec_mbuf_metadata *priv;
489 struct rte_crypto_op *cops[max_pkts];
491 struct rte_mbuf *pkt;
493 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts;) {
495 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp];
497 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
498 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt];
500 priv = get_priv(pkt);
502 ret = xform_func(pkt, sa, &priv->cop);
504 rte_pktmbuf_free(pkt);
507 pkts[nb_pkts++] = pkt;
510 if (cqp->in_flight == 0) {
511 ipsec_ctx->last_qp++;
512 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
513 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
518 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
519 cops, max_pkts - nb_pkts);
521 cqp->in_flight -= nb_cops;
523 for (j = 0; j < nb_cops; j++) {
524 pkt = cops[j]->sym->m_src;
527 priv = get_priv(pkt);
530 RTE_ASSERT(sa != NULL);
532 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
533 ret = xform_func(pkt, sa, cops[j]);
535 rte_pktmbuf_free(pkt);
539 pkts[nb_pkts++] = pkt;
540 if (cqp->in_flight < max_pkts) {
541 ipsec_ctx->last_qp++;
542 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
543 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
554 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
555 uint16_t nb_pkts, uint16_t len)
557 struct ipsec_sa *sas[nb_pkts];
559 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
561 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
563 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
567 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
568 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
570 struct ipsec_sa *sas[nb_pkts];
572 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
574 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
576 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);