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 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa)
23 struct rte_cryptodev_info cdev_info;
24 unsigned long cdev_id_qp = 0;
26 struct cdev_key key = { 0 };
28 key.lcore_id = (uint8_t)rte_lcore_id();
30 key.cipher_algo = (uint8_t)sa->cipher_algo;
31 key.auth_algo = (uint8_t)sa->auth_algo;
32 key.aead_algo = (uint8_t)sa->aead_algo;
34 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
35 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key,
36 (void **)&cdev_id_qp);
39 "No cryptodev: core %u, cipher_algo %u, "
40 "auth_algo %u, aead_algo %u\n",
49 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev "
50 "%u qp %u\n", sa->spi,
51 ipsec_ctx->tbl[cdev_id_qp].id,
52 ipsec_ctx->tbl[cdev_id_qp].qp);
54 if (sa->type != RTE_SECURITY_ACTION_TYPE_NONE) {
55 struct rte_security_session_conf sess_conf = {
56 .action_type = sa->type,
57 .protocol = RTE_SECURITY_PROTOCOL_IPSEC,
62 .direction = sa->direction,
63 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP,
64 .mode = (sa->flags == IP4_TUNNEL ||
65 sa->flags == IP6_TUNNEL) ?
66 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL :
67 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT,
69 .crypto_xform = sa->xforms
73 if (sa->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) {
74 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
75 rte_cryptodev_get_sec_ctx(
76 ipsec_ctx->tbl[cdev_id_qp].id);
78 if (sess_conf.ipsec.mode ==
79 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) {
80 struct rte_security_ipsec_tunnel_param *tunnel =
81 &sess_conf.ipsec.tunnel;
82 if (sa->flags == IP4_TUNNEL) {
84 RTE_SECURITY_IPSEC_TUNNEL_IPV4;
85 tunnel->ipv4.ttl = IPDEFTTL;
87 memcpy((uint8_t *)&tunnel->ipv4.src_ip,
88 (uint8_t *)&sa->src.ip.ip4, 4);
90 memcpy((uint8_t *)&tunnel->ipv4.dst_ip,
91 (uint8_t *)&sa->dst.ip.ip4, 4);
93 /* TODO support for Transport and IPV6 tunnel */
96 sa->sec_session = rte_security_session_create(ctx,
97 &sess_conf, ipsec_ctx->session_pool);
98 if (sa->sec_session == NULL) {
100 "SEC Session init failed: err: %d\n", ret);
103 } else if (sa->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) {
104 struct rte_flow_error err;
105 struct rte_security_ctx *ctx = (struct rte_security_ctx *)
106 rte_eth_dev_get_sec_ctx(
108 const struct rte_security_capability *sec_cap;
110 sa->sec_session = rte_security_session_create(ctx,
111 &sess_conf, ipsec_ctx->session_pool);
112 if (sa->sec_session == NULL) {
114 "SEC Session init failed: err: %d\n", ret);
118 sec_cap = rte_security_capabilities_get(ctx);
120 /* iterate until ESP tunnel*/
121 while (sec_cap->action !=
122 RTE_SECURITY_ACTION_TYPE_NONE) {
124 if (sec_cap->action == sa->type &&
126 RTE_SECURITY_PROTOCOL_IPSEC &&
127 sec_cap->ipsec.mode ==
128 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL &&
129 sec_cap->ipsec.direction == sa->direction)
134 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) {
136 "No suitable security capability found\n");
140 sa->ol_flags = sec_cap->ol_flags;
141 sa->security_ctx = ctx;
142 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH;
144 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4;
145 sa->pattern[1].mask = &rte_flow_item_ipv4_mask;
146 if (sa->flags & IP6_TUNNEL) {
147 sa->pattern[1].spec = &sa->ipv6_spec;
148 memcpy(sa->ipv6_spec.hdr.dst_addr,
149 sa->dst.ip.ip6.ip6_b, 16);
150 memcpy(sa->ipv6_spec.hdr.src_addr,
151 sa->src.ip.ip6.ip6_b, 16);
153 sa->pattern[1].spec = &sa->ipv4_spec;
154 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4;
155 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4;
158 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP;
159 sa->pattern[2].spec = &sa->esp_spec;
160 sa->pattern[2].mask = &rte_flow_item_esp_mask;
161 sa->esp_spec.hdr.spi = sa->spi;
163 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END;
165 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY;
166 sa->action[0].conf = sa->sec_session;
168 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END;
170 sa->attr.egress = (sa->direction ==
171 RTE_SECURITY_IPSEC_SA_DIR_EGRESS);
172 sa->flow = rte_flow_create(sa->portid,
173 &sa->attr, sa->pattern, sa->action, &err);
174 if (sa->flow == NULL) {
176 "Failed to create ipsec flow msg: %s\n",
182 sa->crypto_session = rte_cryptodev_sym_session_create(
183 ipsec_ctx->session_pool);
184 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id,
185 sa->crypto_session, sa->xforms,
186 ipsec_ctx->session_pool);
188 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id,
190 if (cdev_info.sym.max_nb_sessions_per_qp > 0) {
191 ret = rte_cryptodev_queue_pair_attach_sym_session(
192 ipsec_ctx->tbl[cdev_id_qp].id,
193 ipsec_ctx->tbl[cdev_id_qp].qp,
197 "Session cannot be attached to qp %u\n",
198 ipsec_ctx->tbl[cdev_id_qp].qp);
203 sa->cdev_id_qp = cdev_id_qp;
209 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop)
213 cqp->buf[cqp->len++] = cop;
215 if (cqp->len == MAX_PKT_BURST) {
216 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp,
218 if (ret < cqp->len) {
219 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:"
220 " enqueued %u crypto ops out of %u\n",
223 for (i = ret; i < cqp->len; i++)
224 rte_pktmbuf_free(cqp->buf[i]->sym->m_src);
226 cqp->in_flight += ret;
232 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
233 struct rte_mbuf *pkts[], struct ipsec_sa *sas[],
237 struct ipsec_mbuf_metadata *priv;
238 struct rte_crypto_sym_op *sym_cop;
242 for (i = 0; i < nb_pkts; i++) {
243 if (unlikely(sas[i] == NULL)) {
244 rte_pktmbuf_free(pkts[i]);
248 rte_prefetch0(sas[i]);
249 rte_prefetch0(pkts[i]);
251 priv = get_priv(pkts[i]);
256 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL:
257 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
258 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
260 rte_prefetch0(&priv->sym_cop);
262 if ((unlikely(sa->sec_session == NULL)) &&
263 create_session(ipsec_ctx, sa)) {
264 rte_pktmbuf_free(pkts[i]);
268 sym_cop = get_sym_cop(&priv->cop);
269 sym_cop->m_src = pkts[i];
271 rte_security_attach_session(&priv->cop,
274 case RTE_SECURITY_ACTION_TYPE_NONE:
276 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
277 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
279 rte_prefetch0(&priv->sym_cop);
281 if ((unlikely(sa->crypto_session == NULL)) &&
282 create_session(ipsec_ctx, sa)) {
283 rte_pktmbuf_free(pkts[i]);
287 rte_crypto_op_attach_sym_session(&priv->cop,
290 ret = xform_func(pkts[i], sa, &priv->cop);
292 rte_pktmbuf_free(pkts[i]);
296 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL:
298 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO:
299 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
300 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED;
302 rte_prefetch0(&priv->sym_cop);
304 if ((unlikely(sa->sec_session == NULL)) &&
305 create_session(ipsec_ctx, sa)) {
306 rte_pktmbuf_free(pkts[i]);
310 rte_security_attach_session(&priv->cop,
313 ret = xform_func(pkts[i], sa, &priv->cop);
315 rte_pktmbuf_free(pkts[i]);
319 cqp = &ipsec_ctx->tbl[sa->cdev_id_qp];
320 cqp->ol_pkts[cqp->ol_pkts_cnt++] = pkts[i];
321 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA)
322 rte_security_set_pkt_metadata(
324 sa->sec_session, pkts[i], NULL);
328 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps);
329 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop);
334 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx,
335 struct rte_mbuf *pkts[], uint16_t max_pkts)
337 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops;
338 struct ipsec_mbuf_metadata *priv;
339 struct rte_crypto_op *cops[max_pkts];
341 struct rte_mbuf *pkt;
343 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) {
346 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++];
347 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps)
348 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps;
350 while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) {
351 pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt];
353 priv = get_priv(pkt);
355 ret = xform_func(pkt, sa, &priv->cop);
357 rte_pktmbuf_free(pkt);
360 pkts[nb_pkts++] = pkt;
363 if (cqp->in_flight == 0)
366 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp,
367 cops, max_pkts - nb_pkts);
369 cqp->in_flight -= nb_cops;
371 for (j = 0; j < nb_cops; j++) {
372 pkt = cops[j]->sym->m_src;
375 priv = get_priv(pkt);
378 RTE_ASSERT(sa != NULL);
380 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) {
381 ret = xform_func(pkt, sa, cops[j]);
383 rte_pktmbuf_free(pkt);
387 pkts[nb_pkts++] = pkt;
396 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
397 uint16_t nb_pkts, uint16_t len)
399 struct ipsec_sa *sas[nb_pkts];
401 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts);
403 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts);
405 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len);
409 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[],
410 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len)
412 struct ipsec_sa *sas[nb_pkts];
414 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts);
416 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts);
418 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len);