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 void 21 set_ipsec_conf(struct ipsec_sa *sa, struct rte_security_ipsec_xform *ipsec) 22 { 23 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) { 24 struct rte_security_ipsec_tunnel_param *tunnel = 25 &ipsec->tunnel; 26 if (IS_IP4_TUNNEL(sa->flags)) { 27 tunnel->type = 28 RTE_SECURITY_IPSEC_TUNNEL_IPV4; 29 tunnel->ipv4.ttl = IPDEFTTL; 30 31 memcpy((uint8_t *)&tunnel->ipv4.src_ip, 32 (uint8_t *)&sa->src.ip.ip4, 4); 33 34 memcpy((uint8_t *)&tunnel->ipv4.dst_ip, 35 (uint8_t *)&sa->dst.ip.ip4, 4); 36 } 37 /* TODO support for Transport and IPV6 tunnel */ 38 } 39 ipsec->esn_soft_limit = IPSEC_OFFLOAD_ESN_SOFTLIMIT; 40 } 41 42 int 43 create_session(struct ipsec_ctx *ipsec_ctx, struct ipsec_sa *sa) 44 { 45 struct rte_cryptodev_info cdev_info; 46 unsigned long cdev_id_qp = 0; 47 int32_t ret = 0; 48 struct cdev_key key = { 0 }; 49 50 key.lcore_id = (uint8_t)rte_lcore_id(); 51 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; 55 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); 59 if (ret < 0) { 60 RTE_LOG(ERR, IPSEC, 61 "No cryptodev: core %u, cipher_algo %u, " 62 "auth_algo %u, aead_algo %u\n", 63 key.lcore_id, 64 key.cipher_algo, 65 key.auth_algo, 66 key.aead_algo); 67 return -1; 68 } 69 } 70 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); 75 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, 80 {.ipsec = { 81 .spi = sa->spi, 82 .salt = sa->salt, 83 .options = { 0 }, 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, 89 } }, 90 .crypto_xform = sa->xforms, 91 .userdata = NULL, 92 93 }; 94 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); 99 100 /* Set IPsec parameters in conf */ 101 set_ipsec_conf(sa, &(sess_conf.ipsec)); 102 103 sa->sec_session = rte_security_session_create(ctx, 104 &sess_conf, ipsec_ctx->session_priv_pool); 105 if (sa->sec_session == NULL) { 106 RTE_LOG(ERR, IPSEC, 107 "SEC Session init failed: err: %d\n", ret); 108 return -1; 109 } 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( 114 sa->portid); 115 const struct rte_security_capability *sec_cap; 116 int ret = 0; 117 118 sa->sec_session = rte_security_session_create(ctx, 119 &sess_conf, ipsec_ctx->session_priv_pool); 120 if (sa->sec_session == NULL) { 121 RTE_LOG(ERR, IPSEC, 122 "SEC Session init failed: err: %d\n", ret); 123 return -1; 124 } 125 126 sec_cap = rte_security_capabilities_get(ctx); 127 128 /* iterate until ESP tunnel*/ 129 while (sec_cap->action != 130 RTE_SECURITY_ACTION_TYPE_NONE) { 131 132 if (sec_cap->action == sa->type && 133 sec_cap->protocol == 134 RTE_SECURITY_PROTOCOL_IPSEC && 135 sec_cap->ipsec.mode == 136 sess_conf.ipsec.mode && 137 sec_cap->ipsec.direction == sa->direction) 138 break; 139 sec_cap++; 140 } 141 142 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) { 143 RTE_LOG(ERR, IPSEC, 144 "No suitable security capability found\n"); 145 return -1; 146 } 147 148 sa->ol_flags = sec_cap->ol_flags; 149 sa->security_ctx = ctx; 150 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH; 151 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; 156 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; 165 166 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4; 167 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4; 168 } 169 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); 174 175 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END; 176 177 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY; 178 sa->action[0].conf = sa->sec_session; 179 180 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END; 181 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) { 187 uint8_t rss_key[40]; 188 struct rte_eth_rss_conf rss_conf = { 189 .rss_key = rss_key, 190 .rss_key_len = 40, 191 }; 192 struct rte_eth_dev *eth_dev; 193 uint16_t queue[RTE_MAX_QUEUES_PER_PORT]; 194 struct rte_flow_action_rss action_rss; 195 unsigned int i; 196 unsigned int j; 197 198 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END; 199 /* Try RSS. */ 200 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS; 201 sa->action[1].conf = &action_rss; 202 eth_dev = ctx->device; 203 rte_eth_dev_rss_hash_conf_get(sa->portid, 204 &rss_conf); 205 for (i = 0, j = 0; 206 i < eth_dev->data->nb_rx_queues; ++i) 207 if (eth_dev->data->rx_queues[i]) 208 queue[j++] = i; 209 action_rss = (struct rte_flow_action_rss){ 210 .types = rss_conf.rss_hf, 211 .key_len = rss_conf.rss_key_len, 212 .queue_num = j, 213 .key = rss_key, 214 .queue = queue, 215 }; 216 ret = rte_flow_validate(sa->portid, &sa->attr, 217 sa->pattern, sa->action, 218 &err); 219 if (!ret) 220 goto flow_create; 221 /* Try Queue. */ 222 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE; 223 sa->action[1].conf = 224 &(struct rte_flow_action_queue){ 225 .index = 0, 226 }; 227 ret = rte_flow_validate(sa->portid, &sa->attr, 228 sa->pattern, sa->action, 229 &err); 230 /* Try End. */ 231 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END; 232 sa->action[1].conf = NULL; 233 ret = rte_flow_validate(sa->portid, &sa->attr, 234 sa->pattern, sa->action, 235 &err); 236 if (ret) 237 goto flow_create_failure; 238 } else if (sa->attr.egress && 239 (sa->ol_flags & 240 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) { 241 sa->action[1].type = 242 RTE_FLOW_ACTION_TYPE_PASSTHRU; 243 sa->action[2].type = 244 RTE_FLOW_ACTION_TYPE_END; 245 } 246 flow_create: 247 sa->flow = rte_flow_create(sa->portid, 248 &sa->attr, sa->pattern, sa->action, &err); 249 if (sa->flow == NULL) { 250 flow_create_failure: 251 RTE_LOG(ERR, IPSEC, 252 "Failed to create ipsec flow msg: %s\n", 253 err.message); 254 return -1; 255 } 256 } else if (sa->type == 257 RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) { 258 struct rte_security_ctx *ctx = 259 (struct rte_security_ctx *) 260 rte_eth_dev_get_sec_ctx(sa->portid); 261 const struct rte_security_capability *sec_cap; 262 263 if (ctx == NULL) { 264 RTE_LOG(ERR, IPSEC, 265 "Ethernet device doesn't have security features registered\n"); 266 return -1; 267 } 268 269 /* Set IPsec parameters in conf */ 270 set_ipsec_conf(sa, &(sess_conf.ipsec)); 271 272 /* Save SA as userdata for the security session. When 273 * the packet is received, this userdata will be 274 * retrieved using the metadata from the packet. 275 * 276 * The PMD is expected to set similar metadata for other 277 * operations, like rte_eth_event, which are tied to 278 * security session. In such cases, the userdata could 279 * be obtained to uniquely identify the security 280 * parameters denoted. 281 */ 282 283 sess_conf.userdata = (void *) sa; 284 285 sa->sec_session = rte_security_session_create(ctx, 286 &sess_conf, ipsec_ctx->session_pool); 287 if (sa->sec_session == NULL) { 288 RTE_LOG(ERR, IPSEC, 289 "SEC Session init failed: err: %d\n", ret); 290 return -1; 291 } 292 293 sec_cap = rte_security_capabilities_get(ctx); 294 295 if (sec_cap == NULL) { 296 RTE_LOG(ERR, IPSEC, 297 "No capabilities registered\n"); 298 return -1; 299 } 300 301 /* iterate until ESP tunnel*/ 302 while (sec_cap->action != 303 RTE_SECURITY_ACTION_TYPE_NONE) { 304 305 if (sec_cap->action == sa->type && 306 sec_cap->protocol == 307 RTE_SECURITY_PROTOCOL_IPSEC && 308 sec_cap->ipsec.mode == 309 sess_conf.ipsec.mode && 310 sec_cap->ipsec.direction == sa->direction) 311 break; 312 sec_cap++; 313 } 314 315 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) { 316 RTE_LOG(ERR, IPSEC, 317 "No suitable security capability found\n"); 318 return -1; 319 } 320 321 sa->ol_flags = sec_cap->ol_flags; 322 sa->security_ctx = ctx; 323 } 324 } else { 325 sa->crypto_session = rte_cryptodev_sym_session_create( 326 ipsec_ctx->session_pool); 327 rte_cryptodev_sym_session_init(ipsec_ctx->tbl[cdev_id_qp].id, 328 sa->crypto_session, sa->xforms, 329 ipsec_ctx->session_priv_pool); 330 331 rte_cryptodev_info_get(ipsec_ctx->tbl[cdev_id_qp].id, 332 &cdev_info); 333 } 334 sa->cdev_id_qp = cdev_id_qp; 335 336 return 0; 337 } 338 339 /* 340 * queue crypto-ops into PMD queue. 341 */ 342 void 343 enqueue_cop_burst(struct cdev_qp *cqp) 344 { 345 uint32_t i, len, ret; 346 347 len = cqp->len; 348 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len); 349 if (ret < len) { 350 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:" 351 " enqueued %u crypto ops out of %u\n", 352 cqp->id, cqp->qp, ret, len); 353 /* drop packets that we fail to enqueue */ 354 for (i = ret; i < len; i++) 355 rte_pktmbuf_free(cqp->buf[i]->sym->m_src); 356 } 357 cqp->in_flight += ret; 358 cqp->len = 0; 359 } 360 361 static inline void 362 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop) 363 { 364 cqp->buf[cqp->len++] = cop; 365 366 if (cqp->len == MAX_PKT_BURST) 367 enqueue_cop_burst(cqp); 368 } 369 370 static inline void 371 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 372 struct rte_mbuf *pkts[], struct ipsec_sa *sas[], 373 uint16_t nb_pkts) 374 { 375 int32_t ret = 0, i; 376 struct ipsec_mbuf_metadata *priv; 377 struct rte_crypto_sym_op *sym_cop; 378 struct ipsec_sa *sa; 379 380 for (i = 0; i < nb_pkts; i++) { 381 if (unlikely(sas[i] == NULL)) { 382 rte_pktmbuf_free(pkts[i]); 383 continue; 384 } 385 386 rte_prefetch0(sas[i]); 387 rte_prefetch0(pkts[i]); 388 389 priv = get_priv(pkts[i]); 390 sa = sas[i]; 391 priv->sa = sa; 392 393 switch (sa->type) { 394 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL: 395 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 396 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 397 398 rte_prefetch0(&priv->sym_cop); 399 400 if ((unlikely(sa->sec_session == NULL)) && 401 create_session(ipsec_ctx, sa)) { 402 rte_pktmbuf_free(pkts[i]); 403 continue; 404 } 405 406 sym_cop = get_sym_cop(&priv->cop); 407 sym_cop->m_src = pkts[i]; 408 409 rte_security_attach_session(&priv->cop, 410 sa->sec_session); 411 break; 412 case RTE_SECURITY_ACTION_TYPE_NONE: 413 414 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 415 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 416 417 rte_prefetch0(&priv->sym_cop); 418 419 if ((unlikely(sa->crypto_session == NULL)) && 420 create_session(ipsec_ctx, sa)) { 421 rte_pktmbuf_free(pkts[i]); 422 continue; 423 } 424 425 rte_crypto_op_attach_sym_session(&priv->cop, 426 sa->crypto_session); 427 428 ret = xform_func(pkts[i], sa, &priv->cop); 429 if (unlikely(ret)) { 430 rte_pktmbuf_free(pkts[i]); 431 continue; 432 } 433 break; 434 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL: 435 if ((unlikely(sa->sec_session == NULL)) && 436 create_session(ipsec_ctx, sa)) { 437 rte_pktmbuf_free(pkts[i]); 438 continue; 439 } 440 441 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i]; 442 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA) 443 rte_security_set_pkt_metadata( 444 sa->security_ctx, 445 sa->sec_session, pkts[i], NULL); 446 continue; 447 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO: 448 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 449 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 450 451 rte_prefetch0(&priv->sym_cop); 452 453 if ((unlikely(sa->sec_session == NULL)) && 454 create_session(ipsec_ctx, sa)) { 455 rte_pktmbuf_free(pkts[i]); 456 continue; 457 } 458 459 rte_security_attach_session(&priv->cop, 460 sa->sec_session); 461 462 ret = xform_func(pkts[i], sa, &priv->cop); 463 if (unlikely(ret)) { 464 rte_pktmbuf_free(pkts[i]); 465 continue; 466 } 467 468 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i]; 469 if (sa->ol_flags & RTE_SECURITY_TX_OLOAD_NEED_MDATA) 470 rte_security_set_pkt_metadata( 471 sa->security_ctx, 472 sa->sec_session, pkts[i], NULL); 473 continue; 474 } 475 476 RTE_ASSERT(sa->cdev_id_qp < ipsec_ctx->nb_qps); 477 enqueue_cop(&ipsec_ctx->tbl[sa->cdev_id_qp], &priv->cop); 478 } 479 } 480 481 static inline int32_t 482 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 483 struct rte_mbuf *pkts[], uint16_t max_pkts) 484 { 485 int32_t nb_pkts, ret; 486 struct ipsec_mbuf_metadata *priv; 487 struct ipsec_sa *sa; 488 struct rte_mbuf *pkt; 489 490 nb_pkts = 0; 491 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) { 492 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt]; 493 rte_prefetch0(pkt); 494 priv = get_priv(pkt); 495 sa = priv->sa; 496 ret = xform_func(pkt, sa, &priv->cop); 497 if (unlikely(ret)) { 498 rte_pktmbuf_free(pkt); 499 continue; 500 } 501 pkts[nb_pkts++] = pkt; 502 } 503 504 return nb_pkts; 505 } 506 507 static inline int 508 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 509 struct rte_mbuf *pkts[], uint16_t max_pkts) 510 { 511 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops; 512 struct ipsec_mbuf_metadata *priv; 513 struct rte_crypto_op *cops[max_pkts]; 514 struct ipsec_sa *sa; 515 struct rte_mbuf *pkt; 516 517 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) { 518 struct cdev_qp *cqp; 519 520 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++]; 521 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps) 522 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps; 523 524 if (cqp->in_flight == 0) 525 continue; 526 527 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, 528 cops, max_pkts - nb_pkts); 529 530 cqp->in_flight -= nb_cops; 531 532 for (j = 0; j < nb_cops; j++) { 533 pkt = cops[j]->sym->m_src; 534 rte_prefetch0(pkt); 535 536 priv = get_priv(pkt); 537 sa = priv->sa; 538 539 RTE_ASSERT(sa != NULL); 540 541 if (sa->type == RTE_SECURITY_ACTION_TYPE_NONE) { 542 ret = xform_func(pkt, sa, cops[j]); 543 if (unlikely(ret)) { 544 rte_pktmbuf_free(pkt); 545 continue; 546 } 547 } 548 pkts[nb_pkts++] = pkt; 549 } 550 } 551 552 /* return packets */ 553 return nb_pkts; 554 } 555 556 uint16_t 557 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 558 uint16_t nb_pkts, uint16_t len) 559 { 560 struct ipsec_sa *sas[nb_pkts]; 561 562 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts); 563 564 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts); 565 566 return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len); 567 } 568 569 uint16_t 570 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 571 uint16_t len) 572 { 573 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len); 574 } 575 576 uint16_t 577 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 578 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len) 579 { 580 struct ipsec_sa *sas[nb_pkts]; 581 582 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts); 583 584 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts); 585 586 return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len); 587 } 588 589 uint16_t 590 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 591 uint16_t len) 592 { 593 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len); 594 } 595