1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright(c) 2017-2018 Intel Corporation 3 */ 4 #include <rte_malloc.h> 5 #include <rte_hash.h> 6 #include <rte_jhash.h> 7 #include <rte_mbuf.h> 8 #include <rte_cryptodev.h> 9 10 #include "rte_vhost_crypto.h" 11 #include "vhost.h" 12 #include "vhost_user.h" 13 #include "virtio_crypto.h" 14 15 #define INHDR_LEN (sizeof(struct virtio_crypto_inhdr)) 16 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \ 17 sizeof(struct rte_crypto_sym_op)) 18 19 #ifdef RTE_LIBRTE_VHOST_DEBUG 20 #define VC_LOG_ERR(fmt, args...) \ 21 RTE_LOG(ERR, USER1, "[%s] %s() line %u: " fmt "\n", \ 22 "Vhost-Crypto", __func__, __LINE__, ## args) 23 #define VC_LOG_INFO(fmt, args...) \ 24 RTE_LOG(INFO, USER1, "[%s] %s() line %u: " fmt "\n", \ 25 "Vhost-Crypto", __func__, __LINE__, ## args) 26 27 #define VC_LOG_DBG(fmt, args...) \ 28 RTE_LOG(DEBUG, USER1, "[%s] %s() line %u: " fmt "\n", \ 29 "Vhost-Crypto", __func__, __LINE__, ## args) 30 #else 31 #define VC_LOG_ERR(fmt, args...) \ 32 RTE_LOG(ERR, USER1, "[VHOST-Crypto]: " fmt "\n", ## args) 33 #define VC_LOG_INFO(fmt, args...) \ 34 RTE_LOG(INFO, USER1, "[VHOST-Crypto]: " fmt "\n", ## args) 35 #define VC_LOG_DBG(fmt, args...) 36 #endif 37 38 #define VIRTIO_CRYPTO_FEATURES ((1ULL << VIRTIO_F_NOTIFY_ON_EMPTY) | \ 39 (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \ 40 (1ULL << VIRTIO_RING_F_EVENT_IDX) | \ 41 (1ULL << VIRTIO_NET_F_CTRL_VQ) | \ 42 (1ULL << VIRTIO_F_VERSION_1) | \ 43 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) 44 45 #define IOVA_TO_VVA(t, r, a, l, p) \ 46 ((t)(uintptr_t)vhost_iova_to_vva(r->dev, r->vq, a, l, p)) 47 48 /* 49 * vhost_crypto_desc is used to copy original vring_desc to the local buffer 50 * before processing (except the next index). The copy result will be an 51 * array of vhost_crypto_desc elements that follows the sequence of original 52 * vring_desc.next is arranged. 53 */ 54 #define vhost_crypto_desc vring_desc 55 56 static int 57 cipher_algo_transform(uint32_t virtio_cipher_algo, 58 enum rte_crypto_cipher_algorithm *algo) 59 { 60 switch (virtio_cipher_algo) { 61 case VIRTIO_CRYPTO_CIPHER_AES_CBC: 62 *algo = RTE_CRYPTO_CIPHER_AES_CBC; 63 break; 64 case VIRTIO_CRYPTO_CIPHER_AES_CTR: 65 *algo = RTE_CRYPTO_CIPHER_AES_CTR; 66 break; 67 case VIRTIO_CRYPTO_CIPHER_DES_ECB: 68 *algo = -VIRTIO_CRYPTO_NOTSUPP; 69 break; 70 case VIRTIO_CRYPTO_CIPHER_DES_CBC: 71 *algo = RTE_CRYPTO_CIPHER_DES_CBC; 72 break; 73 case VIRTIO_CRYPTO_CIPHER_3DES_ECB: 74 *algo = RTE_CRYPTO_CIPHER_3DES_ECB; 75 break; 76 case VIRTIO_CRYPTO_CIPHER_3DES_CBC: 77 *algo = RTE_CRYPTO_CIPHER_3DES_CBC; 78 break; 79 case VIRTIO_CRYPTO_CIPHER_3DES_CTR: 80 *algo = RTE_CRYPTO_CIPHER_3DES_CTR; 81 break; 82 case VIRTIO_CRYPTO_CIPHER_KASUMI_F8: 83 *algo = RTE_CRYPTO_CIPHER_KASUMI_F8; 84 break; 85 case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2: 86 *algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2; 87 break; 88 case VIRTIO_CRYPTO_CIPHER_AES_F8: 89 *algo = RTE_CRYPTO_CIPHER_AES_F8; 90 break; 91 case VIRTIO_CRYPTO_CIPHER_AES_XTS: 92 *algo = RTE_CRYPTO_CIPHER_AES_XTS; 93 break; 94 case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3: 95 *algo = RTE_CRYPTO_CIPHER_ZUC_EEA3; 96 break; 97 default: 98 return -VIRTIO_CRYPTO_BADMSG; 99 break; 100 } 101 102 return 0; 103 } 104 105 static int 106 auth_algo_transform(uint32_t virtio_auth_algo, 107 enum rte_crypto_auth_algorithm *algo) 108 { 109 switch (virtio_auth_algo) { 110 case VIRTIO_CRYPTO_NO_MAC: 111 *algo = RTE_CRYPTO_AUTH_NULL; 112 break; 113 case VIRTIO_CRYPTO_MAC_HMAC_MD5: 114 *algo = RTE_CRYPTO_AUTH_MD5_HMAC; 115 break; 116 case VIRTIO_CRYPTO_MAC_HMAC_SHA1: 117 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC; 118 break; 119 case VIRTIO_CRYPTO_MAC_HMAC_SHA_224: 120 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC; 121 break; 122 case VIRTIO_CRYPTO_MAC_HMAC_SHA_256: 123 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC; 124 break; 125 case VIRTIO_CRYPTO_MAC_HMAC_SHA_384: 126 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC; 127 break; 128 case VIRTIO_CRYPTO_MAC_HMAC_SHA_512: 129 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC; 130 break; 131 case VIRTIO_CRYPTO_MAC_CMAC_AES: 132 *algo = RTE_CRYPTO_AUTH_AES_CMAC; 133 break; 134 case VIRTIO_CRYPTO_MAC_KASUMI_F9: 135 *algo = RTE_CRYPTO_AUTH_KASUMI_F9; 136 break; 137 case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2: 138 *algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2; 139 break; 140 case VIRTIO_CRYPTO_MAC_GMAC_AES: 141 *algo = RTE_CRYPTO_AUTH_AES_GMAC; 142 break; 143 case VIRTIO_CRYPTO_MAC_CBCMAC_AES: 144 *algo = RTE_CRYPTO_AUTH_AES_CBC_MAC; 145 break; 146 case VIRTIO_CRYPTO_MAC_XCBC_AES: 147 *algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC; 148 break; 149 case VIRTIO_CRYPTO_MAC_CMAC_3DES: 150 case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH: 151 case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9: 152 return -VIRTIO_CRYPTO_NOTSUPP; 153 default: 154 return -VIRTIO_CRYPTO_BADMSG; 155 } 156 157 return 0; 158 } 159 160 static int get_iv_len(enum rte_crypto_cipher_algorithm algo) 161 { 162 int len; 163 164 switch (algo) { 165 case RTE_CRYPTO_CIPHER_3DES_CBC: 166 len = 8; 167 break; 168 case RTE_CRYPTO_CIPHER_3DES_CTR: 169 len = 8; 170 break; 171 case RTE_CRYPTO_CIPHER_3DES_ECB: 172 len = 8; 173 break; 174 case RTE_CRYPTO_CIPHER_AES_CBC: 175 len = 16; 176 break; 177 178 /* TODO: add common algos */ 179 180 default: 181 len = -1; 182 break; 183 } 184 185 return len; 186 } 187 188 /** 189 * vhost_crypto struct is used to maintain a number of virtio_cryptos and 190 * one DPDK crypto device that deals with all crypto workloads. It is declared 191 * here and defined in vhost_crypto.c 192 */ 193 struct vhost_crypto { 194 /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto 195 * session ID. 196 */ 197 struct rte_hash *session_map; 198 struct rte_mempool *mbuf_pool; 199 struct rte_mempool *sess_pool; 200 struct rte_mempool *wb_pool; 201 202 /** DPDK cryptodev ID */ 203 uint8_t cid; 204 uint16_t nb_qps; 205 206 uint64_t last_session_id; 207 208 uint64_t cache_session_id; 209 struct rte_cryptodev_sym_session *cache_session; 210 /** socket id for the device */ 211 int socket_id; 212 213 struct virtio_net *dev; 214 215 uint8_t option; 216 } __rte_cache_aligned; 217 218 struct vhost_crypto_writeback_data { 219 uint8_t *src; 220 uint8_t *dst; 221 uint64_t len; 222 struct vhost_crypto_writeback_data *next; 223 }; 224 225 struct vhost_crypto_data_req { 226 struct vring_desc *head; 227 struct virtio_net *dev; 228 struct virtio_crypto_inhdr *inhdr; 229 struct vhost_virtqueue *vq; 230 struct vhost_crypto_writeback_data *wb; 231 struct rte_mempool *wb_pool; 232 uint16_t desc_idx; 233 uint16_t len; 234 uint16_t zero_copy; 235 }; 236 237 static int 238 transform_cipher_param(struct rte_crypto_sym_xform *xform, 239 VhostUserCryptoSessionParam *param) 240 { 241 int ret; 242 243 ret = cipher_algo_transform(param->cipher_algo, &xform->cipher.algo); 244 if (unlikely(ret < 0)) 245 return ret; 246 247 if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) { 248 VC_LOG_DBG("Invalid cipher key length\n"); 249 return -VIRTIO_CRYPTO_BADMSG; 250 } 251 252 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER; 253 xform->cipher.key.length = param->cipher_key_len; 254 if (xform->cipher.key.length > 0) 255 xform->cipher.key.data = param->cipher_key_buf; 256 if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT) 257 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT; 258 else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT) 259 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT; 260 else { 261 VC_LOG_DBG("Bad operation type"); 262 return -VIRTIO_CRYPTO_BADMSG; 263 } 264 265 ret = get_iv_len(xform->cipher.algo); 266 if (unlikely(ret < 0)) 267 return ret; 268 xform->cipher.iv.length = (uint16_t)ret; 269 xform->cipher.iv.offset = IV_OFFSET; 270 return 0; 271 } 272 273 static int 274 transform_chain_param(struct rte_crypto_sym_xform *xforms, 275 VhostUserCryptoSessionParam *param) 276 { 277 struct rte_crypto_sym_xform *xform_cipher, *xform_auth; 278 int ret; 279 280 switch (param->chaining_dir) { 281 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER: 282 xform_auth = xforms; 283 xform_cipher = xforms->next; 284 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT; 285 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY; 286 break; 287 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH: 288 xform_cipher = xforms; 289 xform_auth = xforms->next; 290 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT; 291 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE; 292 break; 293 default: 294 return -VIRTIO_CRYPTO_BADMSG; 295 } 296 297 /* cipher */ 298 ret = cipher_algo_transform(param->cipher_algo, 299 &xform_cipher->cipher.algo); 300 if (unlikely(ret < 0)) 301 return ret; 302 303 if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) { 304 VC_LOG_DBG("Invalid cipher key length\n"); 305 return -VIRTIO_CRYPTO_BADMSG; 306 } 307 308 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER; 309 xform_cipher->cipher.key.length = param->cipher_key_len; 310 xform_cipher->cipher.key.data = param->cipher_key_buf; 311 ret = get_iv_len(xform_cipher->cipher.algo); 312 if (unlikely(ret < 0)) 313 return ret; 314 xform_cipher->cipher.iv.length = (uint16_t)ret; 315 xform_cipher->cipher.iv.offset = IV_OFFSET; 316 317 /* auth */ 318 xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH; 319 ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo); 320 if (unlikely(ret < 0)) 321 return ret; 322 323 if (param->auth_key_len > VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH) { 324 VC_LOG_DBG("Invalid auth key length\n"); 325 return -VIRTIO_CRYPTO_BADMSG; 326 } 327 328 xform_auth->auth.digest_length = param->digest_len; 329 xform_auth->auth.key.length = param->auth_key_len; 330 xform_auth->auth.key.data = param->auth_key_buf; 331 332 return 0; 333 } 334 335 static void 336 vhost_crypto_create_sess(struct vhost_crypto *vcrypto, 337 VhostUserCryptoSessionParam *sess_param) 338 { 339 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0}; 340 struct rte_cryptodev_sym_session *session; 341 int ret; 342 343 switch (sess_param->op_type) { 344 case VIRTIO_CRYPTO_SYM_OP_NONE: 345 case VIRTIO_CRYPTO_SYM_OP_CIPHER: 346 ret = transform_cipher_param(&xform1, sess_param); 347 if (unlikely(ret)) { 348 VC_LOG_ERR("Error transform session msg (%i)", ret); 349 sess_param->session_id = ret; 350 return; 351 } 352 break; 353 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING: 354 if (unlikely(sess_param->hash_mode != 355 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) { 356 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP; 357 VC_LOG_ERR("Error transform session message (%i)", 358 -VIRTIO_CRYPTO_NOTSUPP); 359 return; 360 } 361 362 xform1.next = &xform2; 363 364 ret = transform_chain_param(&xform1, sess_param); 365 if (unlikely(ret)) { 366 VC_LOG_ERR("Error transform session message (%i)", ret); 367 sess_param->session_id = ret; 368 return; 369 } 370 371 break; 372 default: 373 VC_LOG_ERR("Algorithm not yet supported"); 374 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP; 375 return; 376 } 377 378 session = rte_cryptodev_sym_session_create(vcrypto->cid, &xform1, 379 vcrypto->sess_pool); 380 if (!session) { 381 VC_LOG_ERR("Failed to create session"); 382 sess_param->session_id = -VIRTIO_CRYPTO_ERR; 383 return; 384 } 385 386 /* insert hash to map */ 387 if (rte_hash_add_key_data(vcrypto->session_map, 388 &vcrypto->last_session_id, session) < 0) { 389 VC_LOG_ERR("Failed to insert session to hash table"); 390 391 if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0) 392 VC_LOG_ERR("Failed to free session"); 393 sess_param->session_id = -VIRTIO_CRYPTO_ERR; 394 return; 395 } 396 397 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.", 398 vcrypto->last_session_id, vcrypto->dev->vid); 399 400 sess_param->session_id = vcrypto->last_session_id; 401 vcrypto->last_session_id++; 402 } 403 404 static int 405 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id) 406 { 407 struct rte_cryptodev_sym_session *session; 408 uint64_t sess_id = session_id; 409 int ret; 410 411 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id, 412 (void **)&session); 413 414 if (unlikely(ret < 0)) { 415 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id); 416 return -VIRTIO_CRYPTO_INVSESS; 417 } 418 419 if (rte_cryptodev_sym_session_free(vcrypto->cid, session) < 0) { 420 VC_LOG_DBG("Failed to free session"); 421 return -VIRTIO_CRYPTO_ERR; 422 } 423 424 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) { 425 VC_LOG_DBG("Failed to delete session from hash table."); 426 return -VIRTIO_CRYPTO_ERR; 427 } 428 429 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id, 430 vcrypto->dev->vid); 431 432 return 0; 433 } 434 435 static enum rte_vhost_msg_result 436 vhost_crypto_msg_post_handler(int vid, void *msg) 437 { 438 struct virtio_net *dev = get_device(vid); 439 struct vhost_crypto *vcrypto; 440 struct vhu_msg_context *ctx = msg; 441 enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK; 442 443 if (dev == NULL) { 444 VC_LOG_ERR("Invalid vid %i", vid); 445 return RTE_VHOST_MSG_RESULT_ERR; 446 } 447 448 vcrypto = dev->extern_data; 449 if (vcrypto == NULL) { 450 VC_LOG_ERR("Cannot find required data, is it initialized?"); 451 return RTE_VHOST_MSG_RESULT_ERR; 452 } 453 454 switch (ctx->msg.request.master) { 455 case VHOST_USER_CRYPTO_CREATE_SESS: 456 vhost_crypto_create_sess(vcrypto, 457 &ctx->msg.payload.crypto_session); 458 ctx->fd_num = 0; 459 ret = RTE_VHOST_MSG_RESULT_REPLY; 460 break; 461 case VHOST_USER_CRYPTO_CLOSE_SESS: 462 if (vhost_crypto_close_sess(vcrypto, ctx->msg.payload.u64)) 463 ret = RTE_VHOST_MSG_RESULT_ERR; 464 break; 465 default: 466 ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED; 467 break; 468 } 469 470 return ret; 471 } 472 473 static __rte_always_inline struct vhost_crypto_desc * 474 find_write_desc(struct vhost_crypto_desc *head, struct vhost_crypto_desc *desc, 475 uint32_t max_n_descs) 476 { 477 if (desc < head) 478 return NULL; 479 480 while (desc - head < (int)max_n_descs) { 481 if (desc->flags & VRING_DESC_F_WRITE) 482 return desc; 483 desc++; 484 } 485 486 return NULL; 487 } 488 489 static __rte_always_inline struct virtio_crypto_inhdr * 490 reach_inhdr(struct vhost_crypto_data_req *vc_req, 491 struct vhost_crypto_desc *head, 492 uint32_t max_n_descs) 493 { 494 struct virtio_crypto_inhdr *inhdr; 495 struct vhost_crypto_desc *last = head + (max_n_descs - 1); 496 uint64_t dlen = last->len; 497 498 if (unlikely(dlen != sizeof(*inhdr))) 499 return NULL; 500 501 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, last->addr, 502 &dlen, VHOST_ACCESS_WO); 503 if (unlikely(!inhdr || dlen != last->len)) 504 return NULL; 505 506 return inhdr; 507 } 508 509 static __rte_always_inline int 510 move_desc(struct vhost_crypto_desc *head, 511 struct vhost_crypto_desc **cur_desc, 512 uint32_t size, uint32_t max_n_descs) 513 { 514 struct vhost_crypto_desc *desc = *cur_desc; 515 int left = size - desc->len; 516 517 while (desc->flags & VRING_DESC_F_NEXT && left > 0 && 518 desc >= head && 519 desc - head < (int)max_n_descs) { 520 desc++; 521 left -= desc->len; 522 } 523 524 if (unlikely(left > 0)) 525 return -1; 526 527 if (unlikely(head - desc == (int)max_n_descs)) 528 *cur_desc = NULL; 529 else 530 *cur_desc = desc + 1; 531 532 return 0; 533 } 534 535 static __rte_always_inline void * 536 get_data_ptr(struct vhost_crypto_data_req *vc_req, 537 struct vhost_crypto_desc *cur_desc, 538 uint8_t perm) 539 { 540 void *data; 541 uint64_t dlen = cur_desc->len; 542 543 data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm); 544 if (unlikely(!data || dlen != cur_desc->len)) { 545 VC_LOG_ERR("Failed to map object"); 546 return NULL; 547 } 548 549 return data; 550 } 551 552 static __rte_always_inline uint32_t 553 copy_data_from_desc(void *dst, struct vhost_crypto_data_req *vc_req, 554 struct vhost_crypto_desc *desc, uint32_t size) 555 { 556 uint64_t remain; 557 uint64_t addr; 558 559 remain = RTE_MIN(desc->len, size); 560 addr = desc->addr; 561 do { 562 uint64_t len; 563 void *src; 564 565 len = remain; 566 src = IOVA_TO_VVA(void *, vc_req, addr, &len, VHOST_ACCESS_RO); 567 if (unlikely(src == NULL || len == 0)) 568 return 0; 569 570 rte_memcpy(dst, src, len); 571 remain -= len; 572 /* cast is needed for 32-bit architecture */ 573 dst = RTE_PTR_ADD(dst, (size_t)len); 574 addr += len; 575 } while (unlikely(remain != 0)); 576 577 return RTE_MIN(desc->len, size); 578 } 579 580 581 static __rte_always_inline int 582 copy_data(void *data, struct vhost_crypto_data_req *vc_req, 583 struct vhost_crypto_desc *head, struct vhost_crypto_desc **cur_desc, 584 uint32_t size, uint32_t max_n_descs) 585 { 586 struct vhost_crypto_desc *desc = *cur_desc; 587 uint32_t left = size; 588 589 do { 590 uint32_t copied; 591 592 copied = copy_data_from_desc(data, vc_req, desc, left); 593 if (copied == 0) 594 return -1; 595 left -= copied; 596 data = RTE_PTR_ADD(data, copied); 597 } while (left != 0 && ++desc < head + max_n_descs); 598 599 if (unlikely(left != 0)) 600 return -1; 601 602 if (unlikely(desc == head + max_n_descs)) 603 *cur_desc = NULL; 604 else 605 *cur_desc = desc + 1; 606 607 return 0; 608 } 609 610 static void 611 write_back_data(struct vhost_crypto_data_req *vc_req) 612 { 613 struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last; 614 615 while (wb_data) { 616 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len); 617 memset(wb_data->src, 0, wb_data->len); 618 wb_last = wb_data; 619 wb_data = wb_data->next; 620 rte_mempool_put(vc_req->wb_pool, wb_last); 621 } 622 } 623 624 static void 625 free_wb_data(struct vhost_crypto_writeback_data *wb_data, 626 struct rte_mempool *mp) 627 { 628 while (wb_data->next != NULL) 629 free_wb_data(wb_data->next, mp); 630 631 rte_mempool_put(mp, wb_data); 632 } 633 634 /** 635 * The function will allocate a vhost_crypto_writeback_data linked list 636 * containing the source and destination data pointers for the write back 637 * operation after dequeued from Cryptodev PMD queues. 638 * 639 * @param vc_req 640 * The vhost crypto data request pointer 641 * @param cur_desc 642 * The pointer of the current in use descriptor pointer. The content of 643 * cur_desc is expected to be updated after the function execution. 644 * @param end_wb_data 645 * The last write back data element to be returned. It is used only in cipher 646 * and hash chain operations. 647 * @param src 648 * The source data pointer 649 * @param offset 650 * The offset to both source and destination data. For source data the offset 651 * is the number of bytes between src and start point of cipher operation. For 652 * destination data the offset is the number of bytes from *cur_desc->addr 653 * to the point where the src will be written to. 654 * @param write_back_len 655 * The size of the write back length. 656 * @return 657 * The pointer to the start of the write back data linked list. 658 */ 659 static __rte_always_inline struct vhost_crypto_writeback_data * 660 prepare_write_back_data(struct vhost_crypto_data_req *vc_req, 661 struct vhost_crypto_desc *head_desc, 662 struct vhost_crypto_desc **cur_desc, 663 struct vhost_crypto_writeback_data **end_wb_data, 664 uint8_t *src, 665 uint32_t offset, 666 uint64_t write_back_len, 667 uint32_t max_n_descs) 668 { 669 struct vhost_crypto_writeback_data *wb_data, *head; 670 struct vhost_crypto_desc *desc = *cur_desc; 671 uint64_t dlen; 672 uint8_t *dst; 673 int ret; 674 675 ret = rte_mempool_get(vc_req->wb_pool, (void **)&head); 676 if (unlikely(ret < 0)) { 677 VC_LOG_ERR("no memory"); 678 goto error_exit; 679 } 680 681 wb_data = head; 682 683 if (likely(desc->len > offset)) { 684 wb_data->src = src + offset; 685 dlen = desc->len; 686 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, 687 &dlen, VHOST_ACCESS_RW); 688 if (unlikely(!dst || dlen != desc->len)) { 689 VC_LOG_ERR("Failed to map descriptor"); 690 goto error_exit; 691 } 692 693 wb_data->dst = dst + offset; 694 wb_data->len = RTE_MIN(dlen - offset, write_back_len); 695 write_back_len -= wb_data->len; 696 src += offset + wb_data->len; 697 offset = 0; 698 699 if (unlikely(write_back_len)) { 700 ret = rte_mempool_get(vc_req->wb_pool, 701 (void **)&(wb_data->next)); 702 if (unlikely(ret < 0)) { 703 VC_LOG_ERR("no memory"); 704 goto error_exit; 705 } 706 707 wb_data = wb_data->next; 708 } else 709 wb_data->next = NULL; 710 } else 711 offset -= desc->len; 712 713 while (write_back_len && 714 desc >= head_desc && 715 desc - head_desc < (int)max_n_descs) { 716 desc++; 717 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) { 718 VC_LOG_ERR("incorrect descriptor"); 719 goto error_exit; 720 } 721 722 if (desc->len <= offset) { 723 offset -= desc->len; 724 continue; 725 } 726 727 dlen = desc->len; 728 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen, 729 VHOST_ACCESS_RW) + offset; 730 if (unlikely(dst == NULL || dlen != desc->len)) { 731 VC_LOG_ERR("Failed to map descriptor"); 732 goto error_exit; 733 } 734 735 wb_data->src = src + offset; 736 wb_data->dst = dst; 737 wb_data->len = RTE_MIN(desc->len - offset, write_back_len); 738 write_back_len -= wb_data->len; 739 src += wb_data->len; 740 offset = 0; 741 742 if (write_back_len) { 743 ret = rte_mempool_get(vc_req->wb_pool, 744 (void **)&(wb_data->next)); 745 if (unlikely(ret < 0)) { 746 VC_LOG_ERR("no memory"); 747 goto error_exit; 748 } 749 750 wb_data = wb_data->next; 751 } else 752 wb_data->next = NULL; 753 } 754 755 if (unlikely(desc - head_desc == (int)max_n_descs)) 756 *cur_desc = NULL; 757 else 758 *cur_desc = desc + 1; 759 760 *end_wb_data = wb_data; 761 762 return head; 763 764 error_exit: 765 if (head) 766 free_wb_data(head, vc_req->wb_pool); 767 768 return NULL; 769 } 770 771 static __rte_always_inline uint8_t 772 vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req) 773 { 774 if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) && 775 (req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) && 776 (req->para.dst_data_len >= req->para.src_data_len) && 777 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE))) 778 return VIRTIO_CRYPTO_OK; 779 return VIRTIO_CRYPTO_BADMSG; 780 } 781 782 static __rte_always_inline uint8_t 783 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op, 784 struct vhost_crypto_data_req *vc_req, 785 struct virtio_crypto_cipher_data_req *cipher, 786 struct vhost_crypto_desc *head, 787 uint32_t max_n_descs) 788 { 789 struct vhost_crypto_desc *desc = head; 790 struct vhost_crypto_writeback_data *ewb = NULL; 791 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst; 792 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET); 793 uint8_t ret = vhost_crypto_check_cipher_request(cipher); 794 795 if (unlikely(ret != VIRTIO_CRYPTO_OK)) 796 goto error_exit; 797 798 /* prepare */ 799 /* iv */ 800 if (unlikely(copy_data(iv_data, vc_req, head, &desc, 801 cipher->para.iv_len, max_n_descs))) { 802 VC_LOG_ERR("Incorrect virtio descriptor"); 803 ret = VIRTIO_CRYPTO_BADMSG; 804 goto error_exit; 805 } 806 807 switch (vcrypto->option) { 808 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 809 m_src->data_len = cipher->para.src_data_len; 810 rte_mbuf_iova_set(m_src, 811 gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.src_data_len)); 812 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO); 813 if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) { 814 VC_LOG_ERR("zero_copy may fail due to cross page data"); 815 ret = VIRTIO_CRYPTO_ERR; 816 goto error_exit; 817 } 818 819 if (unlikely(move_desc(head, &desc, cipher->para.src_data_len, 820 max_n_descs) < 0)) { 821 VC_LOG_ERR("Incorrect descriptor"); 822 ret = VIRTIO_CRYPTO_ERR; 823 goto error_exit; 824 } 825 826 break; 827 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 828 vc_req->wb_pool = vcrypto->wb_pool; 829 m_src->data_len = cipher->para.src_data_len; 830 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), 831 vc_req, head, &desc, cipher->para.src_data_len, 832 max_n_descs) < 0)) { 833 VC_LOG_ERR("Incorrect virtio descriptor"); 834 ret = VIRTIO_CRYPTO_BADMSG; 835 goto error_exit; 836 } 837 break; 838 default: 839 ret = VIRTIO_CRYPTO_BADMSG; 840 goto error_exit; 841 } 842 843 /* dst */ 844 desc = find_write_desc(head, desc, max_n_descs); 845 if (unlikely(!desc)) { 846 VC_LOG_ERR("Cannot find write location"); 847 ret = VIRTIO_CRYPTO_BADMSG; 848 goto error_exit; 849 } 850 851 switch (vcrypto->option) { 852 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 853 rte_mbuf_iova_set(m_dst, 854 gpa_to_hpa(vcrypto->dev, desc->addr, cipher->para.dst_data_len)); 855 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW); 856 if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) { 857 VC_LOG_ERR("zero_copy may fail due to cross page data"); 858 ret = VIRTIO_CRYPTO_ERR; 859 goto error_exit; 860 } 861 862 if (unlikely(move_desc(head, &desc, cipher->para.dst_data_len, 863 max_n_descs) < 0)) { 864 VC_LOG_ERR("Incorrect descriptor"); 865 ret = VIRTIO_CRYPTO_ERR; 866 goto error_exit; 867 } 868 869 m_dst->data_len = cipher->para.dst_data_len; 870 break; 871 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 872 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb, 873 rte_pktmbuf_mtod(m_src, uint8_t *), 0, 874 cipher->para.dst_data_len, max_n_descs); 875 if (unlikely(vc_req->wb == NULL)) { 876 ret = VIRTIO_CRYPTO_ERR; 877 goto error_exit; 878 } 879 880 break; 881 default: 882 ret = VIRTIO_CRYPTO_BADMSG; 883 goto error_exit; 884 } 885 886 /* src data */ 887 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 888 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION; 889 890 op->sym->cipher.data.offset = 0; 891 op->sym->cipher.data.length = cipher->para.src_data_len; 892 893 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO); 894 if (unlikely(vc_req->inhdr == NULL)) { 895 ret = VIRTIO_CRYPTO_BADMSG; 896 goto error_exit; 897 } 898 899 vc_req->inhdr->status = VIRTIO_CRYPTO_OK; 900 vc_req->len = cipher->para.dst_data_len + INHDR_LEN; 901 902 return 0; 903 904 error_exit: 905 if (vc_req->wb) 906 free_wb_data(vc_req->wb, vc_req->wb_pool); 907 908 vc_req->len = INHDR_LEN; 909 return ret; 910 } 911 912 static __rte_always_inline uint8_t 913 vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req) 914 { 915 if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) && 916 (req->para.src_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) && 917 (req->para.dst_data_len >= req->para.src_data_len) && 918 (req->para.dst_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) && 919 (req->para.cipher_start_src_offset < 920 VHOST_CRYPTO_MAX_DATA_SIZE) && 921 (req->para.len_to_cipher <= VHOST_CRYPTO_MAX_DATA_SIZE) && 922 (req->para.hash_start_src_offset < 923 VHOST_CRYPTO_MAX_DATA_SIZE) && 924 (req->para.len_to_hash <= VHOST_CRYPTO_MAX_DATA_SIZE) && 925 (req->para.cipher_start_src_offset + req->para.len_to_cipher <= 926 req->para.src_data_len) && 927 (req->para.hash_start_src_offset + req->para.len_to_hash <= 928 req->para.src_data_len) && 929 (req->para.dst_data_len + req->para.hash_result_len <= 930 VHOST_CRYPTO_MAX_DATA_SIZE))) 931 return VIRTIO_CRYPTO_OK; 932 return VIRTIO_CRYPTO_BADMSG; 933 } 934 935 static __rte_always_inline uint8_t 936 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op, 937 struct vhost_crypto_data_req *vc_req, 938 struct virtio_crypto_alg_chain_data_req *chain, 939 struct vhost_crypto_desc *head, 940 uint32_t max_n_descs) 941 { 942 struct vhost_crypto_desc *desc = head, *digest_desc; 943 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL; 944 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst; 945 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET); 946 uint32_t digest_offset; 947 void *digest_addr; 948 uint8_t ret = vhost_crypto_check_chain_request(chain); 949 950 if (unlikely(ret != VIRTIO_CRYPTO_OK)) 951 goto error_exit; 952 953 /* prepare */ 954 /* iv */ 955 if (unlikely(copy_data(iv_data, vc_req, head, &desc, 956 chain->para.iv_len, max_n_descs) < 0)) { 957 VC_LOG_ERR("Incorrect virtio descriptor"); 958 ret = VIRTIO_CRYPTO_BADMSG; 959 goto error_exit; 960 } 961 962 switch (vcrypto->option) { 963 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 964 m_src->data_len = chain->para.src_data_len; 965 m_dst->data_len = chain->para.dst_data_len; 966 967 rte_mbuf_iova_set(m_src, 968 gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.src_data_len)); 969 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO); 970 if (unlikely(rte_mbuf_iova_get(m_src) == 0 || m_src->buf_addr == NULL)) { 971 VC_LOG_ERR("zero_copy may fail due to cross page data"); 972 ret = VIRTIO_CRYPTO_ERR; 973 goto error_exit; 974 } 975 976 if (unlikely(move_desc(head, &desc, chain->para.src_data_len, 977 max_n_descs) < 0)) { 978 VC_LOG_ERR("Incorrect descriptor"); 979 ret = VIRTIO_CRYPTO_ERR; 980 goto error_exit; 981 } 982 break; 983 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 984 vc_req->wb_pool = vcrypto->wb_pool; 985 m_src->data_len = chain->para.src_data_len; 986 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), 987 vc_req, head, &desc, chain->para.src_data_len, 988 max_n_descs) < 0)) { 989 VC_LOG_ERR("Incorrect virtio descriptor"); 990 ret = VIRTIO_CRYPTO_BADMSG; 991 goto error_exit; 992 } 993 994 break; 995 default: 996 ret = VIRTIO_CRYPTO_BADMSG; 997 goto error_exit; 998 } 999 1000 /* dst */ 1001 desc = find_write_desc(head, desc, max_n_descs); 1002 if (unlikely(!desc)) { 1003 VC_LOG_ERR("Cannot find write location"); 1004 ret = VIRTIO_CRYPTO_BADMSG; 1005 goto error_exit; 1006 } 1007 1008 switch (vcrypto->option) { 1009 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 1010 rte_mbuf_iova_set(m_dst, 1011 gpa_to_hpa(vcrypto->dev, desc->addr, chain->para.dst_data_len)); 1012 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW); 1013 if (unlikely(rte_mbuf_iova_get(m_dst) == 0 || m_dst->buf_addr == NULL)) { 1014 VC_LOG_ERR("zero_copy may fail due to cross page data"); 1015 ret = VIRTIO_CRYPTO_ERR; 1016 goto error_exit; 1017 } 1018 1019 if (unlikely(move_desc(vc_req->head, &desc, 1020 chain->para.dst_data_len, max_n_descs) < 0)) { 1021 VC_LOG_ERR("Incorrect descriptor"); 1022 ret = VIRTIO_CRYPTO_ERR; 1023 goto error_exit; 1024 } 1025 1026 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev, 1027 desc->addr, chain->para.hash_result_len); 1028 op->sym->auth.digest.data = get_data_ptr(vc_req, desc, 1029 VHOST_ACCESS_RW); 1030 if (unlikely(op->sym->auth.digest.phys_addr == 0)) { 1031 VC_LOG_ERR("zero_copy may fail due to cross page data"); 1032 ret = VIRTIO_CRYPTO_ERR; 1033 goto error_exit; 1034 } 1035 1036 if (unlikely(move_desc(head, &desc, 1037 chain->para.hash_result_len, 1038 max_n_descs) < 0)) { 1039 VC_LOG_ERR("Incorrect descriptor"); 1040 ret = VIRTIO_CRYPTO_ERR; 1041 goto error_exit; 1042 } 1043 1044 break; 1045 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 1046 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb, 1047 rte_pktmbuf_mtod(m_src, uint8_t *), 1048 chain->para.cipher_start_src_offset, 1049 chain->para.dst_data_len - 1050 chain->para.cipher_start_src_offset, 1051 max_n_descs); 1052 if (unlikely(vc_req->wb == NULL)) { 1053 ret = VIRTIO_CRYPTO_ERR; 1054 goto error_exit; 1055 } 1056 1057 digest_desc = desc; 1058 digest_offset = m_src->data_len; 1059 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *, 1060 digest_offset); 1061 1062 /** create a wb_data for digest */ 1063 ewb->next = prepare_write_back_data(vc_req, head, &desc, 1064 &ewb2, digest_addr, 0, 1065 chain->para.hash_result_len, max_n_descs); 1066 if (unlikely(ewb->next == NULL)) { 1067 ret = VIRTIO_CRYPTO_ERR; 1068 goto error_exit; 1069 } 1070 1071 if (unlikely(copy_data(digest_addr, vc_req, head, &digest_desc, 1072 chain->para.hash_result_len, 1073 max_n_descs) < 0)) { 1074 VC_LOG_ERR("Incorrect virtio descriptor"); 1075 ret = VIRTIO_CRYPTO_BADMSG; 1076 goto error_exit; 1077 } 1078 1079 op->sym->auth.digest.data = digest_addr; 1080 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src, 1081 digest_offset); 1082 break; 1083 default: 1084 ret = VIRTIO_CRYPTO_BADMSG; 1085 goto error_exit; 1086 } 1087 1088 /* record inhdr */ 1089 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO); 1090 if (unlikely(vc_req->inhdr == NULL)) { 1091 ret = VIRTIO_CRYPTO_BADMSG; 1092 goto error_exit; 1093 } 1094 1095 vc_req->inhdr->status = VIRTIO_CRYPTO_OK; 1096 1097 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 1098 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION; 1099 1100 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset; 1101 op->sym->cipher.data.length = chain->para.src_data_len - 1102 chain->para.cipher_start_src_offset; 1103 1104 op->sym->auth.data.offset = chain->para.hash_start_src_offset; 1105 op->sym->auth.data.length = chain->para.len_to_hash; 1106 1107 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len + 1108 INHDR_LEN; 1109 return 0; 1110 1111 error_exit: 1112 if (vc_req->wb) 1113 free_wb_data(vc_req->wb, vc_req->wb_pool); 1114 vc_req->len = INHDR_LEN; 1115 return ret; 1116 } 1117 1118 /** 1119 * Process on descriptor 1120 */ 1121 static __rte_always_inline int 1122 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto, 1123 struct vhost_virtqueue *vq, struct rte_crypto_op *op, 1124 struct vring_desc *head, struct vhost_crypto_desc *descs, 1125 uint16_t desc_idx) 1126 { 1127 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src); 1128 struct rte_cryptodev_sym_session *session; 1129 struct virtio_crypto_op_data_req req; 1130 struct virtio_crypto_inhdr *inhdr; 1131 struct vhost_crypto_desc *desc = descs; 1132 struct vring_desc *src_desc; 1133 uint64_t session_id; 1134 uint64_t dlen; 1135 uint32_t nb_descs = 0, max_n_descs, i; 1136 int err; 1137 1138 vc_req->desc_idx = desc_idx; 1139 vc_req->dev = vcrypto->dev; 1140 vc_req->vq = vq; 1141 1142 if (unlikely((head->flags & VRING_DESC_F_INDIRECT) == 0)) { 1143 VC_LOG_ERR("Invalid descriptor"); 1144 return -1; 1145 } 1146 1147 dlen = head->len; 1148 src_desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr, 1149 &dlen, VHOST_ACCESS_RO); 1150 if (unlikely(!src_desc || dlen != head->len)) { 1151 VC_LOG_ERR("Invalid descriptor"); 1152 return -1; 1153 } 1154 head = src_desc; 1155 1156 nb_descs = max_n_descs = dlen / sizeof(struct vring_desc); 1157 if (unlikely(nb_descs > VHOST_CRYPTO_MAX_N_DESC || nb_descs == 0)) { 1158 err = VIRTIO_CRYPTO_ERR; 1159 VC_LOG_ERR("Cannot process num of descriptors %u", nb_descs); 1160 if (nb_descs > 0) { 1161 struct vring_desc *inhdr_desc = head; 1162 while (inhdr_desc->flags & VRING_DESC_F_NEXT) { 1163 if (inhdr_desc->next >= max_n_descs) 1164 return -1; 1165 inhdr_desc = &head[inhdr_desc->next]; 1166 } 1167 if (inhdr_desc->len != sizeof(*inhdr)) 1168 return -1; 1169 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, 1170 vc_req, inhdr_desc->addr, &dlen, 1171 VHOST_ACCESS_WO); 1172 if (unlikely(!inhdr || dlen != inhdr_desc->len)) 1173 return -1; 1174 inhdr->status = VIRTIO_CRYPTO_ERR; 1175 return -1; 1176 } 1177 } 1178 1179 /* copy descriptors to local variable */ 1180 for (i = 0; i < max_n_descs; i++) { 1181 desc->addr = src_desc->addr; 1182 desc->len = src_desc->len; 1183 desc->flags = src_desc->flags; 1184 desc++; 1185 if (unlikely((src_desc->flags & VRING_DESC_F_NEXT) == 0)) 1186 break; 1187 if (unlikely(src_desc->next >= max_n_descs)) { 1188 err = VIRTIO_CRYPTO_BADMSG; 1189 VC_LOG_ERR("Invalid descriptor"); 1190 goto error_exit; 1191 } 1192 src_desc = &head[src_desc->next]; 1193 } 1194 1195 vc_req->head = head; 1196 vc_req->zero_copy = vcrypto->option; 1197 1198 nb_descs = desc - descs; 1199 desc = descs; 1200 1201 if (unlikely(desc->len < sizeof(req))) { 1202 err = VIRTIO_CRYPTO_BADMSG; 1203 VC_LOG_ERR("Invalid descriptor"); 1204 goto error_exit; 1205 } 1206 1207 if (unlikely(copy_data(&req, vc_req, descs, &desc, sizeof(req), 1208 max_n_descs) < 0)) { 1209 err = VIRTIO_CRYPTO_BADMSG; 1210 VC_LOG_ERR("Invalid descriptor"); 1211 goto error_exit; 1212 } 1213 1214 /* desc is advanced by 1 now */ 1215 max_n_descs -= 1; 1216 1217 switch (req.header.opcode) { 1218 case VIRTIO_CRYPTO_CIPHER_ENCRYPT: 1219 case VIRTIO_CRYPTO_CIPHER_DECRYPT: 1220 session_id = req.header.session_id; 1221 1222 /* one branch to avoid unnecessary table lookup */ 1223 if (vcrypto->cache_session_id != session_id) { 1224 err = rte_hash_lookup_data(vcrypto->session_map, 1225 &session_id, (void **)&session); 1226 if (unlikely(err < 0)) { 1227 err = VIRTIO_CRYPTO_ERR; 1228 VC_LOG_ERR("Failed to find session %"PRIu64, 1229 session_id); 1230 goto error_exit; 1231 } 1232 1233 vcrypto->cache_session = session; 1234 vcrypto->cache_session_id = session_id; 1235 } 1236 1237 session = vcrypto->cache_session; 1238 1239 err = rte_crypto_op_attach_sym_session(op, session); 1240 if (unlikely(err < 0)) { 1241 err = VIRTIO_CRYPTO_ERR; 1242 VC_LOG_ERR("Failed to attach session to op"); 1243 goto error_exit; 1244 } 1245 1246 switch (req.u.sym_req.op_type) { 1247 case VIRTIO_CRYPTO_SYM_OP_NONE: 1248 err = VIRTIO_CRYPTO_NOTSUPP; 1249 break; 1250 case VIRTIO_CRYPTO_SYM_OP_CIPHER: 1251 err = prepare_sym_cipher_op(vcrypto, op, vc_req, 1252 &req.u.sym_req.u.cipher, desc, 1253 max_n_descs); 1254 break; 1255 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING: 1256 err = prepare_sym_chain_op(vcrypto, op, vc_req, 1257 &req.u.sym_req.u.chain, desc, 1258 max_n_descs); 1259 break; 1260 } 1261 if (unlikely(err != 0)) { 1262 VC_LOG_ERR("Failed to process sym request"); 1263 goto error_exit; 1264 } 1265 break; 1266 default: 1267 err = VIRTIO_CRYPTO_ERR; 1268 VC_LOG_ERR("Unsupported symmetric crypto request type %u", 1269 req.header.opcode); 1270 goto error_exit; 1271 } 1272 1273 return 0; 1274 1275 error_exit: 1276 1277 inhdr = reach_inhdr(vc_req, descs, max_n_descs); 1278 if (likely(inhdr != NULL)) 1279 inhdr->status = (uint8_t)err; 1280 1281 return -1; 1282 } 1283 1284 static __rte_always_inline struct vhost_virtqueue * 1285 vhost_crypto_finalize_one_request(struct rte_crypto_op *op, 1286 struct vhost_virtqueue *old_vq) 1287 { 1288 struct rte_mbuf *m_src = op->sym->m_src; 1289 struct rte_mbuf *m_dst = op->sym->m_dst; 1290 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src); 1291 struct vhost_virtqueue *vq; 1292 uint16_t used_idx, desc_idx; 1293 1294 if (unlikely(!vc_req)) { 1295 VC_LOG_ERR("Failed to retrieve vc_req"); 1296 return NULL; 1297 } 1298 vq = vc_req->vq; 1299 used_idx = vc_req->desc_idx; 1300 1301 if (old_vq && (vq != old_vq)) 1302 return vq; 1303 1304 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)) 1305 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR; 1306 else { 1307 if (vc_req->zero_copy == 0) 1308 write_back_data(vc_req); 1309 } 1310 1311 desc_idx = vq->avail->ring[used_idx]; 1312 vq->used->ring[desc_idx].id = vq->avail->ring[desc_idx]; 1313 vq->used->ring[desc_idx].len = vc_req->len; 1314 1315 rte_mempool_put(m_src->pool, (void *)m_src); 1316 1317 if (m_dst) 1318 rte_mempool_put(m_dst->pool, (void *)m_dst); 1319 1320 return vc_req->vq; 1321 } 1322 1323 static __rte_always_inline uint16_t 1324 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops, 1325 uint16_t nb_ops, int *callfd) 1326 { 1327 uint16_t processed = 1; 1328 struct vhost_virtqueue *vq, *tmp_vq; 1329 1330 if (unlikely(nb_ops == 0)) 1331 return 0; 1332 1333 vq = vhost_crypto_finalize_one_request(ops[0], NULL); 1334 if (unlikely(vq == NULL)) 1335 return 0; 1336 tmp_vq = vq; 1337 1338 while ((processed < nb_ops)) { 1339 tmp_vq = vhost_crypto_finalize_one_request(ops[processed], 1340 tmp_vq); 1341 1342 if (unlikely(vq != tmp_vq)) 1343 break; 1344 1345 processed++; 1346 } 1347 1348 *callfd = vq->callfd; 1349 1350 *(volatile uint16_t *)&vq->used->idx += processed; 1351 1352 return processed; 1353 } 1354 1355 int 1356 rte_vhost_crypto_driver_start(const char *path) 1357 { 1358 uint64_t protocol_features; 1359 int ret; 1360 1361 ret = rte_vhost_driver_set_features(path, VIRTIO_CRYPTO_FEATURES); 1362 if (ret) 1363 return -1; 1364 1365 ret = rte_vhost_driver_get_protocol_features(path, &protocol_features); 1366 if (ret) 1367 return -1; 1368 protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_CONFIG); 1369 ret = rte_vhost_driver_set_protocol_features(path, protocol_features); 1370 if (ret) 1371 return -1; 1372 1373 return rte_vhost_driver_start(path); 1374 } 1375 1376 int 1377 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id, 1378 struct rte_mempool *sess_pool, 1379 int socket_id) 1380 { 1381 struct virtio_net *dev = get_device(vid); 1382 struct rte_hash_parameters params = {0}; 1383 struct vhost_crypto *vcrypto; 1384 char name[128]; 1385 int ret; 1386 1387 if (!dev) { 1388 VC_LOG_ERR("Invalid vid %i", vid); 1389 return -EINVAL; 1390 } 1391 1392 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto), 1393 RTE_CACHE_LINE_SIZE, socket_id); 1394 if (!vcrypto) { 1395 VC_LOG_ERR("Insufficient memory"); 1396 return -ENOMEM; 1397 } 1398 1399 vcrypto->sess_pool = sess_pool; 1400 vcrypto->cid = cryptodev_id; 1401 vcrypto->cache_session_id = UINT64_MAX; 1402 vcrypto->last_session_id = 1; 1403 vcrypto->dev = dev; 1404 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE; 1405 1406 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid); 1407 params.name = name; 1408 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES; 1409 params.hash_func = rte_jhash; 1410 params.key_len = sizeof(uint64_t); 1411 params.socket_id = socket_id; 1412 vcrypto->session_map = rte_hash_create(¶ms); 1413 if (!vcrypto->session_map) { 1414 VC_LOG_ERR("Failed to creath session map"); 1415 ret = -ENOMEM; 1416 goto error_exit; 1417 } 1418 1419 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid); 1420 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name, 1421 VHOST_CRYPTO_MBUF_POOL_SIZE, 512, 1422 sizeof(struct vhost_crypto_data_req), 1423 VHOST_CRYPTO_MAX_DATA_SIZE + RTE_PKTMBUF_HEADROOM, 1424 rte_socket_id()); 1425 if (!vcrypto->mbuf_pool) { 1426 VC_LOG_ERR("Failed to creath mbuf pool"); 1427 ret = -ENOMEM; 1428 goto error_exit; 1429 } 1430 1431 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid); 1432 vcrypto->wb_pool = rte_mempool_create(name, 1433 VHOST_CRYPTO_MBUF_POOL_SIZE, 1434 sizeof(struct vhost_crypto_writeback_data), 1435 128, 0, NULL, NULL, NULL, NULL, 1436 rte_socket_id(), 0); 1437 if (!vcrypto->wb_pool) { 1438 VC_LOG_ERR("Failed to creath mempool"); 1439 ret = -ENOMEM; 1440 goto error_exit; 1441 } 1442 1443 dev->extern_data = vcrypto; 1444 dev->extern_ops.pre_msg_handle = NULL; 1445 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler; 1446 1447 return 0; 1448 1449 error_exit: 1450 rte_hash_free(vcrypto->session_map); 1451 rte_mempool_free(vcrypto->mbuf_pool); 1452 1453 rte_free(vcrypto); 1454 1455 return ret; 1456 } 1457 1458 int 1459 rte_vhost_crypto_free(int vid) 1460 { 1461 struct virtio_net *dev = get_device(vid); 1462 struct vhost_crypto *vcrypto; 1463 1464 if (unlikely(dev == NULL)) { 1465 VC_LOG_ERR("Invalid vid %i", vid); 1466 return -EINVAL; 1467 } 1468 1469 vcrypto = dev->extern_data; 1470 if (unlikely(vcrypto == NULL)) { 1471 VC_LOG_ERR("Cannot find required data, is it initialized?"); 1472 return -ENOENT; 1473 } 1474 1475 rte_hash_free(vcrypto->session_map); 1476 rte_mempool_free(vcrypto->mbuf_pool); 1477 rte_mempool_free(vcrypto->wb_pool); 1478 rte_free(vcrypto); 1479 1480 dev->extern_data = NULL; 1481 dev->extern_ops.pre_msg_handle = NULL; 1482 dev->extern_ops.post_msg_handle = NULL; 1483 1484 return 0; 1485 } 1486 1487 int 1488 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option) 1489 { 1490 struct virtio_net *dev = get_device(vid); 1491 struct vhost_crypto *vcrypto; 1492 1493 if (unlikely(dev == NULL)) { 1494 VC_LOG_ERR("Invalid vid %i", vid); 1495 return -EINVAL; 1496 } 1497 1498 if (unlikely((uint32_t)option >= 1499 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) { 1500 VC_LOG_ERR("Invalid option %i", option); 1501 return -EINVAL; 1502 } 1503 1504 vcrypto = (struct vhost_crypto *)dev->extern_data; 1505 if (unlikely(vcrypto == NULL)) { 1506 VC_LOG_ERR("Cannot find required data, is it initialized?"); 1507 return -ENOENT; 1508 } 1509 1510 if (vcrypto->option == (uint8_t)option) 1511 return 0; 1512 1513 if (!(rte_mempool_full(vcrypto->mbuf_pool)) || 1514 !(rte_mempool_full(vcrypto->wb_pool))) { 1515 VC_LOG_ERR("Cannot update zero copy as mempool is not full"); 1516 return -EINVAL; 1517 } 1518 1519 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) { 1520 char name[128]; 1521 1522 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid); 1523 vcrypto->wb_pool = rte_mempool_create(name, 1524 VHOST_CRYPTO_MBUF_POOL_SIZE, 1525 sizeof(struct vhost_crypto_writeback_data), 1526 128, 0, NULL, NULL, NULL, NULL, 1527 rte_socket_id(), 0); 1528 if (!vcrypto->wb_pool) { 1529 VC_LOG_ERR("Failed to creath mbuf pool"); 1530 return -ENOMEM; 1531 } 1532 } else { 1533 rte_mempool_free(vcrypto->wb_pool); 1534 vcrypto->wb_pool = NULL; 1535 } 1536 1537 vcrypto->option = (uint8_t)option; 1538 1539 return 0; 1540 } 1541 1542 uint16_t 1543 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid, 1544 struct rte_crypto_op **ops, uint16_t nb_ops) 1545 { 1546 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2]; 1547 struct vhost_crypto_desc descs[VHOST_CRYPTO_MAX_N_DESC]; 1548 struct virtio_net *dev = get_device(vid); 1549 struct vhost_crypto *vcrypto; 1550 struct vhost_virtqueue *vq; 1551 uint16_t avail_idx; 1552 uint16_t start_idx; 1553 uint16_t count; 1554 uint16_t i = 0; 1555 1556 if (unlikely(dev == NULL)) { 1557 VC_LOG_ERR("Invalid vid %i", vid); 1558 return 0; 1559 } 1560 1561 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) { 1562 VC_LOG_ERR("Invalid qid %u", qid); 1563 return 0; 1564 } 1565 1566 vcrypto = (struct vhost_crypto *)dev->extern_data; 1567 if (unlikely(vcrypto == NULL)) { 1568 VC_LOG_ERR("Cannot find required data, is it initialized?"); 1569 return 0; 1570 } 1571 1572 vq = dev->virtqueue[qid]; 1573 1574 avail_idx = *((volatile uint16_t *)&vq->avail->idx); 1575 start_idx = vq->last_used_idx; 1576 count = avail_idx - start_idx; 1577 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE); 1578 count = RTE_MIN(count, nb_ops); 1579 1580 if (unlikely(count == 0)) 1581 return 0; 1582 1583 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise 1584 * we need only 1 mbuf as src and dst 1585 */ 1586 switch (vcrypto->option) { 1587 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 1588 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, 1589 (void **)mbufs, count * 2) < 0)) { 1590 VC_LOG_ERR("Insufficient memory"); 1591 return 0; 1592 } 1593 1594 for (i = 0; i < count; i++) { 1595 uint16_t used_idx = (start_idx + i) & (vq->size - 1); 1596 uint16_t desc_idx = vq->avail->ring[used_idx]; 1597 struct vring_desc *head = &vq->desc[desc_idx]; 1598 struct rte_crypto_op *op = ops[i]; 1599 1600 op->sym->m_src = mbufs[i * 2]; 1601 op->sym->m_dst = mbufs[i * 2 + 1]; 1602 op->sym->m_src->data_off = 0; 1603 op->sym->m_dst->data_off = 0; 1604 1605 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, 1606 op, head, descs, used_idx) < 0)) 1607 break; 1608 } 1609 1610 if (unlikely(i < count)) 1611 rte_mempool_put_bulk(vcrypto->mbuf_pool, 1612 (void **)&mbufs[i * 2], 1613 (count - i) * 2); 1614 1615 break; 1616 1617 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 1618 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, 1619 (void **)mbufs, count) < 0)) { 1620 VC_LOG_ERR("Insufficient memory"); 1621 return 0; 1622 } 1623 1624 for (i = 0; i < count; i++) { 1625 uint16_t used_idx = (start_idx + i) & (vq->size - 1); 1626 uint16_t desc_idx = vq->avail->ring[used_idx]; 1627 struct vring_desc *head = &vq->desc[desc_idx]; 1628 struct rte_crypto_op *op = ops[i]; 1629 1630 op->sym->m_src = mbufs[i]; 1631 op->sym->m_dst = NULL; 1632 op->sym->m_src->data_off = 0; 1633 1634 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, 1635 op, head, descs, desc_idx) < 0)) 1636 break; 1637 } 1638 1639 if (unlikely(i < count)) 1640 rte_mempool_put_bulk(vcrypto->mbuf_pool, 1641 (void **)&mbufs[i], 1642 count - i); 1643 1644 break; 1645 1646 } 1647 1648 vq->last_used_idx += i; 1649 1650 return i; 1651 } 1652 1653 uint16_t 1654 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops, 1655 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds) 1656 { 1657 struct rte_crypto_op **tmp_ops = ops; 1658 uint16_t count = 0, left = nb_ops; 1659 int callfd; 1660 uint16_t idx = 0; 1661 1662 while (left) { 1663 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left, 1664 &callfd); 1665 if (unlikely(count == 0)) 1666 break; 1667 1668 tmp_ops = &tmp_ops[count]; 1669 left -= count; 1670 1671 callfds[idx++] = callfd; 1672 1673 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) { 1674 VC_LOG_ERR("Too many vqs"); 1675 break; 1676 } 1677 } 1678 1679 *nb_callfds = idx; 1680 1681 return nb_ops - left; 1682 } 1683