1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2017 Intel Corporation 3 */ 4 #include <sys/types.h> 5 #include <netinet/in.h> 6 #include <netinet/ip.h> 7 8 #include <rte_branch_prediction.h> 9 #include <rte_log.h> 10 #include <rte_crypto.h> 11 #include <rte_security.h> 12 #include <rte_cryptodev.h> 13 #include <rte_ethdev.h> 14 #include <rte_mbuf.h> 15 #include <rte_hash.h> 16 17 #include "ipsec.h" 18 #include "esp.h" 19 20 static inline int 21 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa) 22 { 23 struct rte_cryptodev_info cdev_info; 24 unsigned long cdev_id_qp = 0; 25 int32_t ret = 0; 26 struct cdev_key key = { 0 }; 27 28 key.lcore_id = (uint8_t)rte_lcore_id(); 29 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; 33 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); 37 if (ret < 0) { 38 RTE_LOG(ERR, IPSEC, 39 "No cryptodev: core %u, cipher_algo %u, " 40 "auth_algo %u, aead_algo %u\n", 41 key.lcore_id, 42 key.cipher_algo, 43 key.auth_algo, 44 key.aead_algo); 45 return -1; 46 } 47 } 48 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); 53 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, 58 {.ipsec = { 59 .spi = sa->spi, 60 .salt = sa->salt, 61 .options = { 0 }, 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, 68 } }, 69 .crypto_xform = sa->xforms 70 71 }; 72 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); 77 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) { 83 tunnel->type = 84 RTE_SECURITY_IPSEC_TUNNEL_IPV4; 85 tunnel->ipv4.ttl = IPDEFTTL; 86 87 memcpy((uint8_t *)&tunnel->ipv4.src_ip, 88 (uint8_t *)&sa->src.ip.ip4, 4); 89 90 memcpy((uint8_t *)&tunnel->ipv4.dst_ip, 91 (uint8_t *)&sa->dst.ip.ip4, 4); 92 } 93 /* TODO support for Transport and IPV6 tunnel */ 94 } 95 96 sa->sec_session = rte_security_session_create(ctx, 97 &sess_conf, ipsec_ctx->session_pool); 98 if (sa->sec_session == NULL) { 99 RTE_LOG(ERR, IPSEC, 100 "SEC Session init failed: err: %d\n", ret); 101 return -1; 102 } 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( 107 sa->portid); 108 const struct rte_security_capability *sec_cap; 109 110 sa->sec_session = rte_security_session_create(ctx, 111 &sess_conf, ipsec_ctx->session_pool); 112 if (sa->sec_session == NULL) { 113 RTE_LOG(ERR, IPSEC, 114 "SEC Session init failed: err: %d\n", ret); 115 return -1; 116 } 117 118 sec_cap = rte_security_capabilities_get(ctx); 119 120 /* iterate until ESP tunnel*/ 121 while (sec_cap->action != 122 RTE_SECURITY_ACTION_TYPE_NONE) { 123 124 if (sec_cap->action == sa->type && 125 sec_cap->protocol == 126 RTE_SECURITY_PROTOCOL_IPSEC && 127 sec_cap->ipsec.mode == 128 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL && 129 sec_cap->ipsec.direction == sa->direction) 130 break; 131 sec_cap++; 132 } 133 134 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) { 135 RTE_LOG(ERR, IPSEC, 136 "No suitable security capability found\n"); 137 return -1; 138 } 139 140 sa->ol_flags = sec_cap->ol_flags; 141 sa->security_ctx = ctx; 142 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH; 143 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); 152 } else { 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; 156 } 157 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; 162 163 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END; 164 165 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY; 166 sa->action[0].conf = sa->sec_session; 167 168 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END; 169 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) { 175 RTE_LOG(ERR, IPSEC, 176 "Failed to create ipsec flow msg: %s\n", 177 err.message); 178 return -1; 179 } 180 } 181 } else { 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); 187 188 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id, 189 &cdev_info); 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, 194 sa->crypto_session); 195 if (ret < 0) { 196 RTE_LOG(ERR, IPSEC, 197 "Session cannot be attached to qp %u\n", 198 ipsec_ctx->tbl[cdev_id_qp].qp); 199 return -1; 200 } 201 } 202 } 203 sa->cdev_id_qp = cdev_id_qp; 204 205 return 0; 206 } 207 208 static inline void 209 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop) 210 { 211 int32_t ret, i; 212 213 cqp->buf[cqp->len++] = cop; 214 215 if (cqp->len == MAX_PKT_BURST) { 216 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, 217 cqp->buf, cqp->len); 218 if (ret < cqp->len) { 219 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:" 220 " enqueued %u crypto ops out of %u\n", 221 cqp->id, cqp->qp, 222 ret, cqp->len); 223 for (i = ret; i < cqp->len; i++) 224 rte_pktmbuf_free(cqp->buf[i]->sym->m_src); 225 } 226 cqp->in_flight += ret; 227 cqp->len = 0; 228 } 229 } 230 231 static inline void 232 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 233 struct rte_mbuf *pkts[], struct ipsec_sa *sas[], 234 uint16_t nb_pkts) 235 { 236 int32_t ret = 0, i; 237 struct ipsec_mbuf_metadata *priv; 238 struct rte_crypto_sym_op *sym_cop; 239 struct ipsec_sa *sa; 240 struct cdev_qp *cqp; 241 242 for (i = 0; i < nb_pkts; i++) { 243 if (unlikely(sas[i] == NULL)) { 244 rte_pktmbuf_free(pkts[i]); 245 continue; 246 } 247 248 rte_prefetch0(sas[i]); 249 rte_prefetch0(pkts[i]); 250 251 priv = get_priv(pkts[i]); 252 sa = sas[i]; 253 priv->sa = sa; 254 255 switch (sa->type) { 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; 259 260 rte_prefetch0(&priv->sym_cop); 261 262 if ((unlikely(sa->sec_session == NULL)) && 263 create_session(ipsec_ctx, sa)) { 264 rte_pktmbuf_free(pkts[i]); 265 continue; 266 } 267 268 sym_cop = get_sym_cop(&priv->cop); 269 sym_cop->m_src = pkts[i]; 270 271 rte_security_attach_session(&priv->cop, 272 sa->sec_session); 273 break; 274 case RTE_SECURITY_ACTION_TYPE_NONE: 275 276 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 277 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 278 279 rte_prefetch0(&priv->sym_cop); 280 281 if ((unlikely(sa->crypto_session == NULL)) && 282 create_session(ipsec_ctx, sa)) { 283 rte_pktmbuf_free(pkts[i]); 284 continue; 285 } 286 287 rte_crypto_op_attach_sym_session(&priv->cop, 288 sa->crypto_session); 289 290 ret = xform_func(pkts[i], sa, &priv->cop); 291 if (unlikely(ret)) { 292 rte_pktmbuf_free(pkts[i]); 293 continue; 294 } 295 break; 296 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL: 297 break; 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; 301 302 rte_prefetch0(&priv->sym_cop); 303 304 if ((unlikely(sa->sec_session == NULL)) && 305 create_session(ipsec_ctx, sa)) { 306 rte_pktmbuf_free(pkts[i]); 307 continue; 308 } 309 310 rte_security_attach_session(&priv->cop, 311 sa->sec_session); 312 313 ret = xform_func(pkts[i], sa, &priv->cop); 314 if (unlikely(ret)) { 315 rte_pktmbuf_free(pkts[i]); 316 continue; 317 } 318 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( 323 sa->security_ctx, 324 sa->sec_session, pkts[i], NULL); 325 continue; 326 } 327 328 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps); 329 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop); 330 } 331 } 332 333 static inline int 334 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 335 struct rte_mbuf *pkts[], uint16_t max_pkts) 336 { 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]; 340 struct ipsec_sa *sa; 341 struct rte_mbuf *pkt; 342 343 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) { 344 struct cdev_qp *cqp; 345 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; 349 350 while (cqp->ol_pkts_cnt > 0 && nb_pkts < max_pkts) { 351 pkt = cqp->ol_pkts[--cqp->ol_pkts_cnt]; 352 rte_prefetch0(pkt); 353 priv = get_priv(pkt); 354 sa = priv->sa; 355 ret = xform_func(pkt, sa, &priv->cop); 356 if (unlikely(ret)) { 357 rte_pktmbuf_free(pkt); 358 continue; 359 } 360 pkts[nb_pkts++] = pkt; 361 } 362 363 if (cqp->in_flight == 0) 364 continue; 365 366 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, 367 cops, max_pkts - nb_pkts); 368 369 cqp->in_flight -= nb_cops; 370 371 for (j = 0; j < nb_cops; j++) { 372 pkt = cops[j]->sym->m_src; 373 rte_prefetch0(pkt); 374 375 priv = get_priv(pkt); 376 sa = priv->sa; 377 378 RTE_ASSERT(sa != NULL); 379 380 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) { 381 ret = xform_func(pkt, sa, cops[j]); 382 if (unlikely(ret)) { 383 rte_pktmbuf_free(pkt); 384 continue; 385 } 386 } 387 pkts[nb_pkts++] = pkt; 388 } 389 } 390 391 /* return packets */ 392 return nb_pkts; 393 } 394 395 uint16_t 396 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 397 uint16_t nb_pkts, uint16_t len) 398 { 399 struct ipsec_sa *sas[nb_pkts]; 400 401 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts); 402 403 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts); 404 405 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len); 406 } 407 408 uint16_t 409 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 410 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len) 411 { 412 struct ipsec_sa *sas[nb_pkts]; 413 414 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts); 415 416 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts); 417 418 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len); 419 } 420