1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2016-2020 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_event_crypto_adapter.h> 10 #include <rte_log.h> 11 #include <rte_crypto.h> 12 #include <rte_security.h> 13 #include <rte_cryptodev.h> 14 #include <rte_ipsec.h> 15 #include <rte_ethdev.h> 16 #include <rte_mbuf.h> 17 #include <rte_hash.h> 18 19 #include "ipsec.h" 20 #include "esp.h" 21 22 static inline void 23 set_ipsec_conf(struct ipsec_sa *sa, struct rte_security_ipsec_xform *ipsec) 24 { 25 if (ipsec->mode == RTE_SECURITY_IPSEC_SA_MODE_TUNNEL) { 26 struct rte_security_ipsec_tunnel_param *tunnel = 27 &ipsec->tunnel; 28 if (IS_IP4_TUNNEL(sa->flags)) { 29 tunnel->type = 30 RTE_SECURITY_IPSEC_TUNNEL_IPV4; 31 tunnel->ipv4.ttl = IPDEFTTL; 32 33 memcpy((uint8_t *)&tunnel->ipv4.src_ip, 34 (uint8_t *)&sa->src.ip.ip4, 4); 35 36 memcpy((uint8_t *)&tunnel->ipv4.dst_ip, 37 (uint8_t *)&sa->dst.ip.ip4, 4); 38 } else if (IS_IP6_TUNNEL(sa->flags)) { 39 tunnel->type = 40 RTE_SECURITY_IPSEC_TUNNEL_IPV6; 41 tunnel->ipv6.hlimit = IPDEFTTL; 42 tunnel->ipv6.dscp = 0; 43 tunnel->ipv6.flabel = 0; 44 45 memcpy((uint8_t *)&tunnel->ipv6.src_addr, 46 (uint8_t *)&sa->src.ip.ip6.ip6_b, 16); 47 48 memcpy((uint8_t *)&tunnel->ipv6.dst_addr, 49 (uint8_t *)&sa->dst.ip.ip6.ip6_b, 16); 50 } 51 /* TODO support for Transport */ 52 } 53 ipsec->replay_win_sz = app_sa_prm.window_size; 54 ipsec->options.esn = app_sa_prm.enable_esn; 55 ipsec->options.udp_encap = sa->udp_encap; 56 } 57 58 int 59 create_lookaside_session(struct ipsec_ctx *ipsec_ctx_lcore[], 60 struct socket_ctx *skt_ctx, const struct eventmode_conf *em_conf, 61 struct ipsec_sa *sa, struct rte_ipsec_session *ips) 62 { 63 uint16_t cdev_id = RTE_CRYPTO_MAX_DEVS; 64 enum rte_crypto_op_sess_type sess_type; 65 struct rte_cryptodev_info cdev_info; 66 enum rte_crypto_op_type op_type; 67 unsigned long cdev_id_qp = 0; 68 struct ipsec_ctx *ipsec_ctx; 69 struct cdev_key key = { 0 }; 70 void *sess = NULL; 71 uint32_t lcore_id; 72 int32_t ret = 0; 73 74 RTE_LCORE_FOREACH(lcore_id) { 75 ipsec_ctx = ipsec_ctx_lcore[lcore_id]; 76 77 /* Core is not bound to any cryptodev, skip it */ 78 if (ipsec_ctx->cdev_map == NULL) 79 continue; 80 81 /* Looking for cryptodev, which can handle this SA */ 82 key.lcore_id = (uint8_t)lcore_id; 83 key.cipher_algo = (uint8_t)sa->cipher_algo; 84 key.auth_algo = (uint8_t)sa->auth_algo; 85 key.aead_algo = (uint8_t)sa->aead_algo; 86 87 ret = rte_hash_lookup_data(ipsec_ctx->cdev_map, &key, 88 (void **)&cdev_id_qp); 89 if (ret == -ENOENT) 90 continue; 91 if (ret < 0) { 92 RTE_LOG(ERR, IPSEC, 93 "No cryptodev: core %u, cipher_algo %u, " 94 "auth_algo %u, aead_algo %u\n", 95 key.lcore_id, 96 key.cipher_algo, 97 key.auth_algo, 98 key.aead_algo); 99 return ret; 100 } 101 102 /* Verify that all cores are using same cryptodev for current 103 * algorithm combination, required by SA. 104 * Current cryptodev mapping process will map SA to the first 105 * cryptodev that matches requirements, so it's a double check, 106 * not an additional restriction. 107 */ 108 if (cdev_id == RTE_CRYPTO_MAX_DEVS) 109 cdev_id = ipsec_ctx->tbl[cdev_id_qp].id; 110 else if (cdev_id != ipsec_ctx->tbl[cdev_id_qp].id) { 111 RTE_LOG(ERR, IPSEC, 112 "SA mapping to multiple cryptodevs is " 113 "not supported!"); 114 return -EINVAL; 115 } 116 117 /* Store per core queue pair information */ 118 sa->cqp[lcore_id] = &ipsec_ctx->tbl[cdev_id_qp]; 119 } 120 if (cdev_id == RTE_CRYPTO_MAX_DEVS) { 121 RTE_LOG(WARNING, IPSEC, "No cores found to handle SA\n"); 122 return 0; 123 } 124 125 RTE_LOG(DEBUG, IPSEC, "Create session for SA spi %u on cryptodev " 126 "%u\n", sa->spi, cdev_id); 127 128 if (ips->type != RTE_SECURITY_ACTION_TYPE_NONE && 129 ips->type != RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) { 130 struct rte_security_session_conf sess_conf = { 131 .action_type = ips->type, 132 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 133 {.ipsec = { 134 .spi = sa->spi, 135 .salt = sa->salt, 136 .options = { 0 }, 137 .replay_win_sz = 0, 138 .direction = sa->direction, 139 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP, 140 .mode = (IS_TUNNEL(sa->flags)) ? 141 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL : 142 RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT, 143 } }, 144 .crypto_xform = sa->xforms, 145 .userdata = NULL, 146 147 }; 148 149 if (ips->type == RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) { 150 struct rte_security_ctx *ctx = (struct rte_security_ctx *) 151 rte_cryptodev_get_sec_ctx( 152 cdev_id); 153 154 /* Set IPsec parameters in conf */ 155 set_ipsec_conf(sa, &(sess_conf.ipsec)); 156 157 ips->security.ses = rte_security_session_create(ctx, 158 &sess_conf, skt_ctx->session_pool); 159 if (ips->security.ses == NULL) { 160 RTE_LOG(ERR, IPSEC, 161 "SEC Session init failed: err: %d\n", ret); 162 return -1; 163 } 164 ips->security.ctx = ctx; 165 166 sess = ips->security.ses; 167 op_type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 168 sess_type = RTE_CRYPTO_OP_SECURITY_SESSION; 169 } else { 170 RTE_LOG(ERR, IPSEC, "Inline not supported\n"); 171 return -1; 172 } 173 } else { 174 if (ips->type == RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO) { 175 struct rte_cryptodev_info info; 176 177 rte_cryptodev_info_get(cdev_id, &info); 178 if (!(info.feature_flags & 179 RTE_CRYPTODEV_FF_SYM_CPU_CRYPTO)) 180 return -ENOTSUP; 181 182 } 183 ips->crypto.dev_id = cdev_id; 184 ips->crypto.ses = rte_cryptodev_sym_session_create(cdev_id, 185 sa->xforms, skt_ctx->session_pool); 186 187 rte_cryptodev_info_get(cdev_id, &cdev_info); 188 } 189 190 /* Setup meta data required by event crypto adapter */ 191 if (em_conf->enable_event_crypto_adapter && sess != NULL) { 192 union rte_event_crypto_metadata m_data; 193 const struct eventdev_params *eventdev_conf; 194 195 eventdev_conf = &(em_conf->eventdev_config[0]); 196 memset(&m_data, 0, sizeof(m_data)); 197 198 /* Fill in response information */ 199 m_data.response_info.sched_type = em_conf->ext_params.sched_type; 200 m_data.response_info.op = RTE_EVENT_OP_NEW; 201 m_data.response_info.queue_id = eventdev_conf->ev_cpt_queue_id; 202 203 /* Fill in request information */ 204 m_data.request_info.cdev_id = cdev_id; 205 m_data.request_info.queue_pair_id = 0; 206 207 /* Attach meta info to session */ 208 rte_cryptodev_session_event_mdata_set(cdev_id, sess, op_type, 209 sess_type, &m_data, sizeof(m_data)); 210 } 211 212 return 0; 213 } 214 215 int 216 create_inline_session(struct socket_ctx *skt_ctx, struct ipsec_sa *sa, 217 struct rte_ipsec_session *ips) 218 { 219 int32_t ret = 0; 220 struct rte_security_ctx *sec_ctx; 221 struct rte_security_session_conf sess_conf = { 222 .action_type = ips->type, 223 .protocol = RTE_SECURITY_PROTOCOL_IPSEC, 224 {.ipsec = { 225 .spi = sa->spi, 226 .salt = sa->salt, 227 .options = { 0 }, 228 .replay_win_sz = 0, 229 .direction = sa->direction, 230 .proto = RTE_SECURITY_IPSEC_SA_PROTO_ESP 231 } }, 232 .crypto_xform = sa->xforms, 233 .userdata = NULL, 234 }; 235 236 if (IS_TRANSPORT(sa->flags)) { 237 sess_conf.ipsec.mode = RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT; 238 if (IS_IP4(sa->flags)) { 239 sess_conf.ipsec.tunnel.type = 240 RTE_SECURITY_IPSEC_TUNNEL_IPV4; 241 242 sess_conf.ipsec.tunnel.ipv4.src_ip.s_addr = 243 sa->src.ip.ip4; 244 sess_conf.ipsec.tunnel.ipv4.dst_ip.s_addr = 245 sa->dst.ip.ip4; 246 } else if (IS_IP6(sa->flags)) { 247 sess_conf.ipsec.tunnel.type = 248 RTE_SECURITY_IPSEC_TUNNEL_IPV6; 249 250 memcpy(sess_conf.ipsec.tunnel.ipv6.src_addr.s6_addr, 251 sa->src.ip.ip6.ip6_b, 16); 252 memcpy(sess_conf.ipsec.tunnel.ipv6.dst_addr.s6_addr, 253 sa->dst.ip.ip6.ip6_b, 16); 254 } 255 } else if (IS_TUNNEL(sa->flags)) { 256 sess_conf.ipsec.mode = RTE_SECURITY_IPSEC_SA_MODE_TUNNEL; 257 258 if (IS_IP4(sa->flags)) { 259 sess_conf.ipsec.tunnel.type = 260 RTE_SECURITY_IPSEC_TUNNEL_IPV4; 261 262 sess_conf.ipsec.tunnel.ipv4.src_ip.s_addr = 263 sa->src.ip.ip4; 264 sess_conf.ipsec.tunnel.ipv4.dst_ip.s_addr = 265 sa->dst.ip.ip4; 266 } else if (IS_IP6(sa->flags)) { 267 sess_conf.ipsec.tunnel.type = 268 RTE_SECURITY_IPSEC_TUNNEL_IPV6; 269 270 memcpy(sess_conf.ipsec.tunnel.ipv6.src_addr.s6_addr, 271 sa->src.ip.ip6.ip6_b, 16); 272 memcpy(sess_conf.ipsec.tunnel.ipv6.dst_addr.s6_addr, 273 sa->dst.ip.ip6.ip6_b, 16); 274 } else { 275 RTE_LOG(ERR, IPSEC, "invalid tunnel type\n"); 276 return -1; 277 } 278 } 279 280 if (sa->udp_encap) { 281 sess_conf.ipsec.options.udp_encap = 1; 282 sess_conf.ipsec.udp.sport = htons(sa->udp.sport); 283 sess_conf.ipsec.udp.dport = htons(sa->udp.dport); 284 } 285 286 if (sa->esn > 0) { 287 sess_conf.ipsec.options.esn = 1; 288 sess_conf.ipsec.esn.value = sa->esn; 289 } 290 291 292 RTE_LOG_DP(DEBUG, IPSEC, "Create session for SA spi %u on port %u\n", 293 sa->spi, sa->portid); 294 295 if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO) { 296 struct rte_flow_error err; 297 const struct rte_security_capability *sec_cap; 298 int ret = 0; 299 300 sec_ctx = (struct rte_security_ctx *) 301 rte_eth_dev_get_sec_ctx( 302 sa->portid); 303 if (sec_ctx == NULL) { 304 RTE_LOG(ERR, IPSEC, 305 " rte_eth_dev_get_sec_ctx failed\n"); 306 return -1; 307 } 308 309 ips->security.ses = rte_security_session_create(sec_ctx, 310 &sess_conf, skt_ctx->session_pool); 311 if (ips->security.ses == NULL) { 312 RTE_LOG(ERR, IPSEC, 313 "SEC Session init failed: err: %d\n", ret); 314 return -1; 315 } 316 317 sec_cap = rte_security_capabilities_get(sec_ctx); 318 319 /* iterate until ESP tunnel*/ 320 while (sec_cap->action != RTE_SECURITY_ACTION_TYPE_NONE) { 321 if (sec_cap->action == ips->type && 322 sec_cap->protocol == 323 RTE_SECURITY_PROTOCOL_IPSEC && 324 sec_cap->ipsec.mode == 325 RTE_SECURITY_IPSEC_SA_MODE_TUNNEL && 326 sec_cap->ipsec.direction == sa->direction) 327 break; 328 sec_cap++; 329 } 330 331 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) { 332 RTE_LOG(ERR, IPSEC, 333 "No suitable security capability found\n"); 334 return -1; 335 } 336 337 ips->security.ol_flags = sec_cap->ol_flags; 338 ips->security.ctx = sec_ctx; 339 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH; 340 341 if (IS_IP6(sa->flags)) { 342 sa->pattern[1].mask = &rte_flow_item_ipv6_mask; 343 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6; 344 sa->pattern[1].spec = &sa->ipv6_spec; 345 346 memcpy(sa->ipv6_spec.hdr.dst_addr, 347 sa->dst.ip.ip6.ip6_b, 16); 348 memcpy(sa->ipv6_spec.hdr.src_addr, 349 sa->src.ip.ip6.ip6_b, 16); 350 } else if (IS_IP4(sa->flags)) { 351 sa->pattern[1].mask = &rte_flow_item_ipv4_mask; 352 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4; 353 sa->pattern[1].spec = &sa->ipv4_spec; 354 355 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4; 356 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4; 357 } 358 359 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi); 360 361 if (sa->udp_encap) { 362 363 sa->udp_spec.hdr.dst_port = 364 rte_cpu_to_be_16(sa->udp.dport); 365 sa->udp_spec.hdr.src_port = 366 rte_cpu_to_be_16(sa->udp.sport); 367 368 sa->pattern[2].mask = &rte_flow_item_udp_mask; 369 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_UDP; 370 sa->pattern[2].spec = &sa->udp_spec; 371 372 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_ESP; 373 sa->pattern[3].spec = &sa->esp_spec; 374 sa->pattern[3].mask = &rte_flow_item_esp_mask; 375 376 sa->pattern[4].type = RTE_FLOW_ITEM_TYPE_END; 377 } else { 378 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP; 379 sa->pattern[2].spec = &sa->esp_spec; 380 sa->pattern[2].mask = &rte_flow_item_esp_mask; 381 382 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END; 383 } 384 385 sa->action[0].type = RTE_FLOW_ACTION_TYPE_SECURITY; 386 sa->action[0].conf = ips->security.ses; 387 388 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END; 389 390 sa->attr.egress = (sa->direction == 391 RTE_SECURITY_IPSEC_SA_DIR_EGRESS); 392 sa->attr.ingress = (sa->direction == 393 RTE_SECURITY_IPSEC_SA_DIR_INGRESS); 394 if (sa->attr.ingress) { 395 uint8_t rss_key[64]; 396 struct rte_eth_rss_conf rss_conf = { 397 .rss_key = rss_key, 398 .rss_key_len = sizeof(rss_key), 399 }; 400 struct rte_eth_dev_info dev_info; 401 uint16_t queue[RTE_MAX_QUEUES_PER_PORT]; 402 struct rte_flow_action_rss action_rss; 403 unsigned int i; 404 unsigned int j; 405 406 /* Don't create flow if default flow is created */ 407 if (flow_info_tbl[sa->portid].rx_def_flow) 408 return 0; 409 410 ret = rte_eth_dev_info_get(sa->portid, &dev_info); 411 if (ret != 0) { 412 RTE_LOG(ERR, IPSEC, 413 "Error during getting device (port %u) info: %s\n", 414 sa->portid, strerror(-ret)); 415 return ret; 416 } 417 418 sa->action[2].type = RTE_FLOW_ACTION_TYPE_END; 419 /* Try RSS. */ 420 sa->action[1].type = RTE_FLOW_ACTION_TYPE_RSS; 421 sa->action[1].conf = &action_rss; 422 ret = rte_eth_dev_rss_hash_conf_get(sa->portid, 423 &rss_conf); 424 if (ret != 0) { 425 RTE_LOG(ERR, IPSEC, 426 "rte_eth_dev_rss_hash_conf_get:ret=%d\n", 427 ret); 428 return -1; 429 } 430 for (i = 0, j = 0; i < dev_info.nb_rx_queues; ++i) 431 queue[j++] = i; 432 433 action_rss = (struct rte_flow_action_rss){ 434 .types = rss_conf.rss_hf, 435 .key_len = rss_conf.rss_key_len, 436 .queue_num = j, 437 .key = rss_key, 438 .queue = queue, 439 }; 440 ret = rte_flow_validate(sa->portid, &sa->attr, 441 sa->pattern, sa->action, 442 &err); 443 if (!ret) 444 goto flow_create; 445 /* Try Queue. */ 446 sa->action[1].type = RTE_FLOW_ACTION_TYPE_QUEUE; 447 sa->action[1].conf = 448 &(struct rte_flow_action_queue){ 449 .index = 0, 450 }; 451 ret = rte_flow_validate(sa->portid, &sa->attr, 452 sa->pattern, sa->action, 453 &err); 454 /* Try End. */ 455 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END; 456 sa->action[1].conf = NULL; 457 ret = rte_flow_validate(sa->portid, &sa->attr, 458 sa->pattern, sa->action, 459 &err); 460 if (ret) 461 goto flow_create_failure; 462 } else if (sa->attr.egress && 463 (ips->security.ol_flags & 464 RTE_SECURITY_TX_HW_TRAILER_OFFLOAD)) { 465 sa->action[1].type = 466 RTE_FLOW_ACTION_TYPE_PASSTHRU; 467 sa->action[2].type = 468 RTE_FLOW_ACTION_TYPE_END; 469 } 470 flow_create: 471 sa->flow = rte_flow_create(sa->portid, 472 &sa->attr, sa->pattern, sa->action, &err); 473 if (sa->flow == NULL) { 474 flow_create_failure: 475 RTE_LOG(ERR, IPSEC, 476 "Failed to create ipsec flow msg: %s\n", 477 err.message); 478 return -1; 479 } 480 } else if (ips->type == RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL) { 481 const struct rte_security_capability *sec_cap; 482 483 sec_ctx = (struct rte_security_ctx *) 484 rte_eth_dev_get_sec_ctx(sa->portid); 485 486 if (sec_ctx == NULL) { 487 RTE_LOG(ERR, IPSEC, 488 "Ethernet device doesn't have security features registered\n"); 489 return -1; 490 } 491 492 /* Set IPsec parameters in conf */ 493 set_ipsec_conf(sa, &(sess_conf.ipsec)); 494 495 /* Save SA as userdata for the security session. When 496 * the packet is received, this userdata will be 497 * retrieved using the metadata from the packet. 498 * 499 * The PMD is expected to set similar metadata for other 500 * operations, like rte_eth_event, which are tied to 501 * security session. In such cases, the userdata could 502 * be obtained to uniquely identify the security 503 * parameters denoted. 504 */ 505 506 sess_conf.userdata = (void *) sa; 507 508 ips->security.ses = rte_security_session_create(sec_ctx, 509 &sess_conf, skt_ctx->session_pool); 510 if (ips->security.ses == NULL) { 511 RTE_LOG(ERR, IPSEC, 512 "SEC Session init failed: err: %d\n", ret); 513 return -1; 514 } 515 516 sec_cap = rte_security_capabilities_get(sec_ctx); 517 if (sec_cap == NULL) { 518 RTE_LOG(ERR, IPSEC, 519 "No capabilities registered\n"); 520 return -1; 521 } 522 523 /* iterate until ESP tunnel*/ 524 while (sec_cap->action != 525 RTE_SECURITY_ACTION_TYPE_NONE) { 526 if (sec_cap->action == ips->type && 527 sec_cap->protocol == 528 RTE_SECURITY_PROTOCOL_IPSEC && 529 sec_cap->ipsec.mode == 530 sess_conf.ipsec.mode && 531 sec_cap->ipsec.direction == sa->direction) 532 break; 533 sec_cap++; 534 } 535 536 if (sec_cap->action == RTE_SECURITY_ACTION_TYPE_NONE) { 537 RTE_LOG(ERR, IPSEC, 538 "No suitable security capability found\n"); 539 return -1; 540 } 541 542 ips->security.ol_flags = sec_cap->ol_flags; 543 ips->security.ctx = sec_ctx; 544 } 545 546 return 0; 547 } 548 549 int 550 create_ipsec_esp_flow(struct ipsec_sa *sa) 551 { 552 int ret = 0; 553 struct rte_flow_error err = {}; 554 if (sa->direction == RTE_SECURITY_IPSEC_SA_DIR_EGRESS) { 555 RTE_LOG(ERR, IPSEC, 556 "No Flow director rule for Egress traffic\n"); 557 return -1; 558 } 559 if (sa->flags == TRANSPORT) { 560 RTE_LOG(ERR, IPSEC, 561 "No Flow director rule for transport mode\n"); 562 return -1; 563 } 564 sa->action[0].type = RTE_FLOW_ACTION_TYPE_QUEUE; 565 sa->pattern[0].type = RTE_FLOW_ITEM_TYPE_ETH; 566 sa->action[0].conf = &(struct rte_flow_action_queue) { 567 .index = sa->fdir_qid, 568 }; 569 sa->attr.egress = 0; 570 sa->attr.ingress = 1; 571 if (IS_IP6(sa->flags)) { 572 sa->pattern[1].mask = &rte_flow_item_ipv6_mask; 573 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV6; 574 sa->pattern[1].spec = &sa->ipv6_spec; 575 memcpy(sa->ipv6_spec.hdr.dst_addr, 576 sa->dst.ip.ip6.ip6_b, sizeof(sa->dst.ip.ip6.ip6_b)); 577 memcpy(sa->ipv6_spec.hdr.src_addr, 578 sa->src.ip.ip6.ip6_b, sizeof(sa->src.ip.ip6.ip6_b)); 579 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP; 580 sa->pattern[2].spec = &sa->esp_spec; 581 sa->pattern[2].mask = &rte_flow_item_esp_mask; 582 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi); 583 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END; 584 } else if (IS_IP4(sa->flags)) { 585 sa->pattern[1].mask = &rte_flow_item_ipv4_mask; 586 sa->pattern[1].type = RTE_FLOW_ITEM_TYPE_IPV4; 587 sa->pattern[1].spec = &sa->ipv4_spec; 588 sa->ipv4_spec.hdr.dst_addr = sa->dst.ip.ip4; 589 sa->ipv4_spec.hdr.src_addr = sa->src.ip.ip4; 590 sa->pattern[2].type = RTE_FLOW_ITEM_TYPE_ESP; 591 sa->pattern[2].spec = &sa->esp_spec; 592 sa->pattern[2].mask = &rte_flow_item_esp_mask; 593 sa->esp_spec.hdr.spi = rte_cpu_to_be_32(sa->spi); 594 sa->pattern[3].type = RTE_FLOW_ITEM_TYPE_END; 595 } 596 sa->action[1].type = RTE_FLOW_ACTION_TYPE_END; 597 598 ret = rte_flow_validate(sa->portid, &sa->attr, sa->pattern, sa->action, 599 &err); 600 if (ret < 0) { 601 RTE_LOG(ERR, IPSEC, "Flow validation failed %s\n", err.message); 602 return ret; 603 } 604 605 sa->flow = rte_flow_create(sa->portid, &sa->attr, sa->pattern, 606 sa->action, &err); 607 if (!sa->flow) { 608 RTE_LOG(ERR, IPSEC, "Flow creation failed %s\n", err.message); 609 return -1; 610 } 611 612 return 0; 613 } 614 615 /* 616 * queue crypto-ops into PMD queue. 617 */ 618 void 619 enqueue_cop_burst(struct cdev_qp *cqp) 620 { 621 uint32_t i, len, ret; 622 623 len = cqp->len; 624 ret = rte_cryptodev_enqueue_burst(cqp->id, cqp->qp, cqp->buf, len); 625 if (ret < len) { 626 RTE_LOG_DP(DEBUG, IPSEC, "Cryptodev %u queue %u:" 627 " enqueued %u crypto ops out of %u\n", 628 cqp->id, cqp->qp, ret, len); 629 /* drop packets that we fail to enqueue */ 630 for (i = ret; i < len; i++) 631 free_pkts(&cqp->buf[i]->sym->m_src, 1); 632 } 633 cqp->in_flight += ret; 634 cqp->len = 0; 635 } 636 637 static inline void 638 enqueue_cop(struct cdev_qp *cqp, struct rte_crypto_op *cop) 639 { 640 cqp->buf[cqp->len++] = cop; 641 642 if (cqp->len == MAX_PKT_BURST) 643 enqueue_cop_burst(cqp); 644 } 645 646 static inline void 647 ipsec_enqueue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 648 struct rte_mbuf *pkts[], void *sas[], 649 uint16_t nb_pkts) 650 { 651 int32_t ret = 0, i; 652 struct ipsec_mbuf_metadata *priv; 653 struct rte_crypto_sym_op *sym_cop; 654 struct ipsec_sa *sa; 655 struct rte_ipsec_session *ips; 656 657 for (i = 0; i < nb_pkts; i++) { 658 if (unlikely(sas[i] == NULL)) { 659 free_pkts(&pkts[i], 1); 660 continue; 661 } 662 663 rte_prefetch0(sas[i]); 664 rte_prefetch0(pkts[i]); 665 666 priv = get_priv(pkts[i]); 667 sa = ipsec_mask_saptr(sas[i]); 668 priv->sa = sa; 669 ips = ipsec_get_primary_session(sa); 670 671 switch (ips->type) { 672 case RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL: 673 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 674 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 675 676 rte_prefetch0(&priv->sym_cop); 677 678 if (unlikely(ips->security.ses == NULL)) { 679 free_pkts(&pkts[i], 1); 680 continue; 681 } 682 683 if (unlikely((pkts[i]->packet_type & 684 (RTE_PTYPE_TUNNEL_MASK | 685 RTE_PTYPE_L4_MASK)) == 686 MBUF_PTYPE_TUNNEL_ESP_IN_UDP && 687 sa->udp_encap != 1)) { 688 free_pkts(&pkts[i], 1); 689 continue; 690 } 691 692 sym_cop = get_sym_cop(&priv->cop); 693 sym_cop->m_src = pkts[i]; 694 695 rte_security_attach_session(&priv->cop, 696 ips->security.ses); 697 break; 698 699 case RTE_SECURITY_ACTION_TYPE_CPU_CRYPTO: 700 RTE_LOG(ERR, IPSEC, "CPU crypto is not supported by the" 701 " legacy mode."); 702 free_pkts(&pkts[i], 1); 703 continue; 704 705 case RTE_SECURITY_ACTION_TYPE_NONE: 706 707 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 708 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 709 710 rte_prefetch0(&priv->sym_cop); 711 712 if (unlikely(ips->crypto.ses == NULL)) { 713 free_pkts(&pkts[i], 1); 714 continue; 715 } 716 717 rte_crypto_op_attach_sym_session(&priv->cop, 718 ips->crypto.ses); 719 720 ret = xform_func(pkts[i], sa, &priv->cop); 721 if (unlikely(ret)) { 722 free_pkts(&pkts[i], 1); 723 continue; 724 } 725 break; 726 case RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL: 727 RTE_ASSERT(ips->security.ses != NULL); 728 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i]; 729 if (ips->security.ol_flags & 730 RTE_SECURITY_TX_OLOAD_NEED_MDATA) 731 rte_security_set_pkt_metadata( 732 ips->security.ctx, ips->security.ses, 733 pkts[i], NULL); 734 continue; 735 case RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO: 736 RTE_ASSERT(ips->security.ses != NULL); 737 priv->cop.type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 738 priv->cop.status = RTE_CRYPTO_OP_STATUS_NOT_PROCESSED; 739 740 rte_prefetch0(&priv->sym_cop); 741 rte_security_attach_session(&priv->cop, 742 ips->security.ses); 743 744 ret = xform_func(pkts[i], sa, &priv->cop); 745 if (unlikely(ret)) { 746 free_pkts(&pkts[i], 1); 747 continue; 748 } 749 750 ipsec_ctx->ol_pkts[ipsec_ctx->ol_pkts_cnt++] = pkts[i]; 751 if (ips->security.ol_flags & 752 RTE_SECURITY_TX_OLOAD_NEED_MDATA) 753 rte_security_set_pkt_metadata( 754 ips->security.ctx, ips->security.ses, 755 pkts[i], NULL); 756 continue; 757 } 758 759 enqueue_cop(sa->cqp[ipsec_ctx->lcore_id], &priv->cop); 760 } 761 } 762 763 static inline int32_t 764 ipsec_inline_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 765 struct rte_mbuf *pkts[], uint16_t max_pkts) 766 { 767 int32_t nb_pkts, ret; 768 struct ipsec_mbuf_metadata *priv; 769 struct ipsec_sa *sa; 770 struct rte_mbuf *pkt; 771 772 nb_pkts = 0; 773 while (ipsec_ctx->ol_pkts_cnt > 0 && nb_pkts < max_pkts) { 774 pkt = ipsec_ctx->ol_pkts[--ipsec_ctx->ol_pkts_cnt]; 775 rte_prefetch0(pkt); 776 priv = get_priv(pkt); 777 sa = priv->sa; 778 ret = xform_func(pkt, sa, &priv->cop); 779 if (unlikely(ret)) { 780 free_pkts(&pkt, 1); 781 continue; 782 } 783 pkts[nb_pkts++] = pkt; 784 } 785 786 return nb_pkts; 787 } 788 789 static inline int 790 ipsec_dequeue(ipsec_xform_fn xform_func, struct ipsec_ctx *ipsec_ctx, 791 struct rte_mbuf *pkts[], uint16_t max_pkts) 792 { 793 int32_t nb_pkts = 0, ret = 0, i, j, nb_cops; 794 struct ipsec_mbuf_metadata *priv; 795 struct rte_crypto_op *cops[max_pkts]; 796 struct ipsec_sa *sa; 797 struct rte_mbuf *pkt; 798 799 for (i = 0; i < ipsec_ctx->nb_qps && nb_pkts < max_pkts; i++) { 800 struct cdev_qp *cqp; 801 802 cqp = &ipsec_ctx->tbl[ipsec_ctx->last_qp++]; 803 if (ipsec_ctx->last_qp == ipsec_ctx->nb_qps) 804 ipsec_ctx->last_qp %= ipsec_ctx->nb_qps; 805 806 if (cqp->in_flight == 0) 807 continue; 808 809 nb_cops = rte_cryptodev_dequeue_burst(cqp->id, cqp->qp, 810 cops, max_pkts - nb_pkts); 811 812 cqp->in_flight -= nb_cops; 813 814 for (j = 0; j < nb_cops; j++) { 815 pkt = cops[j]->sym->m_src; 816 rte_prefetch0(pkt); 817 818 priv = get_priv(pkt); 819 sa = priv->sa; 820 821 RTE_ASSERT(sa != NULL); 822 823 if (ipsec_get_action_type(sa) == 824 RTE_SECURITY_ACTION_TYPE_NONE) { 825 ret = xform_func(pkt, sa, cops[j]); 826 if (unlikely(ret)) { 827 free_pkts(&pkt, 1); 828 continue; 829 } 830 } else if (ipsec_get_action_type(sa) == 831 RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL) { 832 if (cops[j]->status) { 833 free_pkts(&pkt, 1); 834 continue; 835 } 836 } 837 pkts[nb_pkts++] = pkt; 838 } 839 } 840 841 /* return packets */ 842 return nb_pkts; 843 } 844 845 uint16_t 846 ipsec_inbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 847 uint16_t nb_pkts, uint16_t len) 848 { 849 void *sas[nb_pkts]; 850 851 inbound_sa_lookup(ctx->sa_ctx, pkts, sas, nb_pkts); 852 853 ipsec_enqueue(esp_inbound, ctx, pkts, sas, nb_pkts); 854 855 return ipsec_inline_dequeue(esp_inbound_post, ctx, pkts, len); 856 } 857 858 uint16_t 859 ipsec_inbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 860 uint16_t len) 861 { 862 return ipsec_dequeue(esp_inbound_post, ctx, pkts, len); 863 } 864 865 uint16_t 866 ipsec_outbound(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 867 uint32_t sa_idx[], uint16_t nb_pkts, uint16_t len) 868 { 869 void *sas[nb_pkts]; 870 871 outbound_sa_lookup(ctx->sa_ctx, sa_idx, sas, nb_pkts); 872 873 ipsec_enqueue(esp_outbound, ctx, pkts, sas, nb_pkts); 874 875 return ipsec_inline_dequeue(esp_outbound_post, ctx, pkts, len); 876 } 877 878 uint16_t 879 ipsec_outbound_cqp_dequeue(struct ipsec_ctx *ctx, struct rte_mbuf *pkts[], 880 uint16_t len) 881 { 882 return ipsec_dequeue(esp_outbound_post, ctx, pkts, len); 883 } 884