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 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr, 811 cipher->para.src_data_len); 812 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO); 813 if (unlikely(m_src->buf_iova == 0 || 814 m_src->buf_addr == NULL)) { 815 VC_LOG_ERR("zero_copy may fail due to cross page data"); 816 ret = VIRTIO_CRYPTO_ERR; 817 goto error_exit; 818 } 819 820 if (unlikely(move_desc(head, &desc, cipher->para.src_data_len, 821 max_n_descs) < 0)) { 822 VC_LOG_ERR("Incorrect descriptor"); 823 ret = VIRTIO_CRYPTO_ERR; 824 goto error_exit; 825 } 826 827 break; 828 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 829 vc_req->wb_pool = vcrypto->wb_pool; 830 m_src->data_len = cipher->para.src_data_len; 831 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), 832 vc_req, head, &desc, cipher->para.src_data_len, 833 max_n_descs) < 0)) { 834 VC_LOG_ERR("Incorrect virtio descriptor"); 835 ret = VIRTIO_CRYPTO_BADMSG; 836 goto error_exit; 837 } 838 break; 839 default: 840 ret = VIRTIO_CRYPTO_BADMSG; 841 goto error_exit; 842 } 843 844 /* dst */ 845 desc = find_write_desc(head, desc, max_n_descs); 846 if (unlikely(!desc)) { 847 VC_LOG_ERR("Cannot find write location"); 848 ret = VIRTIO_CRYPTO_BADMSG; 849 goto error_exit; 850 } 851 852 switch (vcrypto->option) { 853 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 854 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev, 855 desc->addr, cipher->para.dst_data_len); 856 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW); 857 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) { 858 VC_LOG_ERR("zero_copy may fail due to cross page data"); 859 ret = VIRTIO_CRYPTO_ERR; 860 goto error_exit; 861 } 862 863 if (unlikely(move_desc(head, &desc, cipher->para.dst_data_len, 864 max_n_descs) < 0)) { 865 VC_LOG_ERR("Incorrect descriptor"); 866 ret = VIRTIO_CRYPTO_ERR; 867 goto error_exit; 868 } 869 870 m_dst->data_len = cipher->para.dst_data_len; 871 break; 872 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 873 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb, 874 rte_pktmbuf_mtod(m_src, uint8_t *), 0, 875 cipher->para.dst_data_len, max_n_descs); 876 if (unlikely(vc_req->wb == NULL)) { 877 ret = VIRTIO_CRYPTO_ERR; 878 goto error_exit; 879 } 880 881 break; 882 default: 883 ret = VIRTIO_CRYPTO_BADMSG; 884 goto error_exit; 885 } 886 887 /* src data */ 888 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 889 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION; 890 891 op->sym->cipher.data.offset = 0; 892 op->sym->cipher.data.length = cipher->para.src_data_len; 893 894 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO); 895 if (unlikely(vc_req->inhdr == NULL)) { 896 ret = VIRTIO_CRYPTO_BADMSG; 897 goto error_exit; 898 } 899 900 vc_req->inhdr->status = VIRTIO_CRYPTO_OK; 901 vc_req->len = cipher->para.dst_data_len + INHDR_LEN; 902 903 return 0; 904 905 error_exit: 906 if (vc_req->wb) 907 free_wb_data(vc_req->wb, vc_req->wb_pool); 908 909 vc_req->len = INHDR_LEN; 910 return ret; 911 } 912 913 static __rte_always_inline uint8_t 914 vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req) 915 { 916 if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) && 917 (req->para.src_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) && 918 (req->para.dst_data_len >= req->para.src_data_len) && 919 (req->para.dst_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) && 920 (req->para.cipher_start_src_offset < 921 VHOST_CRYPTO_MAX_DATA_SIZE) && 922 (req->para.len_to_cipher <= VHOST_CRYPTO_MAX_DATA_SIZE) && 923 (req->para.hash_start_src_offset < 924 VHOST_CRYPTO_MAX_DATA_SIZE) && 925 (req->para.len_to_hash <= VHOST_CRYPTO_MAX_DATA_SIZE) && 926 (req->para.cipher_start_src_offset + req->para.len_to_cipher <= 927 req->para.src_data_len) && 928 (req->para.hash_start_src_offset + req->para.len_to_hash <= 929 req->para.src_data_len) && 930 (req->para.dst_data_len + req->para.hash_result_len <= 931 VHOST_CRYPTO_MAX_DATA_SIZE))) 932 return VIRTIO_CRYPTO_OK; 933 return VIRTIO_CRYPTO_BADMSG; 934 } 935 936 static __rte_always_inline uint8_t 937 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op, 938 struct vhost_crypto_data_req *vc_req, 939 struct virtio_crypto_alg_chain_data_req *chain, 940 struct vhost_crypto_desc *head, 941 uint32_t max_n_descs) 942 { 943 struct vhost_crypto_desc *desc = head, *digest_desc; 944 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL; 945 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst; 946 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET); 947 uint32_t digest_offset; 948 void *digest_addr; 949 uint8_t ret = vhost_crypto_check_chain_request(chain); 950 951 if (unlikely(ret != VIRTIO_CRYPTO_OK)) 952 goto error_exit; 953 954 /* prepare */ 955 /* iv */ 956 if (unlikely(copy_data(iv_data, vc_req, head, &desc, 957 chain->para.iv_len, max_n_descs) < 0)) { 958 VC_LOG_ERR("Incorrect virtio descriptor"); 959 ret = VIRTIO_CRYPTO_BADMSG; 960 goto error_exit; 961 } 962 963 switch (vcrypto->option) { 964 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 965 m_src->data_len = chain->para.src_data_len; 966 m_dst->data_len = chain->para.dst_data_len; 967 968 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr, 969 chain->para.src_data_len); 970 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO); 971 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) { 972 VC_LOG_ERR("zero_copy may fail due to cross page data"); 973 ret = VIRTIO_CRYPTO_ERR; 974 goto error_exit; 975 } 976 977 if (unlikely(move_desc(head, &desc, chain->para.src_data_len, 978 max_n_descs) < 0)) { 979 VC_LOG_ERR("Incorrect descriptor"); 980 ret = VIRTIO_CRYPTO_ERR; 981 goto error_exit; 982 } 983 break; 984 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 985 vc_req->wb_pool = vcrypto->wb_pool; 986 m_src->data_len = chain->para.src_data_len; 987 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), 988 vc_req, head, &desc, chain->para.src_data_len, 989 max_n_descs) < 0)) { 990 VC_LOG_ERR("Incorrect virtio descriptor"); 991 ret = VIRTIO_CRYPTO_BADMSG; 992 goto error_exit; 993 } 994 995 break; 996 default: 997 ret = VIRTIO_CRYPTO_BADMSG; 998 goto error_exit; 999 } 1000 1001 /* dst */ 1002 desc = find_write_desc(head, desc, max_n_descs); 1003 if (unlikely(!desc)) { 1004 VC_LOG_ERR("Cannot find write location"); 1005 ret = VIRTIO_CRYPTO_BADMSG; 1006 goto error_exit; 1007 } 1008 1009 switch (vcrypto->option) { 1010 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 1011 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev, 1012 desc->addr, chain->para.dst_data_len); 1013 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW); 1014 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) { 1015 VC_LOG_ERR("zero_copy may fail due to cross page data"); 1016 ret = VIRTIO_CRYPTO_ERR; 1017 goto error_exit; 1018 } 1019 1020 if (unlikely(move_desc(vc_req->head, &desc, 1021 chain->para.dst_data_len, max_n_descs) < 0)) { 1022 VC_LOG_ERR("Incorrect descriptor"); 1023 ret = VIRTIO_CRYPTO_ERR; 1024 goto error_exit; 1025 } 1026 1027 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev, 1028 desc->addr, chain->para.hash_result_len); 1029 op->sym->auth.digest.data = get_data_ptr(vc_req, desc, 1030 VHOST_ACCESS_RW); 1031 if (unlikely(op->sym->auth.digest.phys_addr == 0)) { 1032 VC_LOG_ERR("zero_copy may fail due to cross page data"); 1033 ret = VIRTIO_CRYPTO_ERR; 1034 goto error_exit; 1035 } 1036 1037 if (unlikely(move_desc(head, &desc, 1038 chain->para.hash_result_len, 1039 max_n_descs) < 0)) { 1040 VC_LOG_ERR("Incorrect descriptor"); 1041 ret = VIRTIO_CRYPTO_ERR; 1042 goto error_exit; 1043 } 1044 1045 break; 1046 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 1047 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb, 1048 rte_pktmbuf_mtod(m_src, uint8_t *), 1049 chain->para.cipher_start_src_offset, 1050 chain->para.dst_data_len - 1051 chain->para.cipher_start_src_offset, 1052 max_n_descs); 1053 if (unlikely(vc_req->wb == NULL)) { 1054 ret = VIRTIO_CRYPTO_ERR; 1055 goto error_exit; 1056 } 1057 1058 digest_desc = desc; 1059 digest_offset = m_src->data_len; 1060 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *, 1061 digest_offset); 1062 1063 /** create a wb_data for digest */ 1064 ewb->next = prepare_write_back_data(vc_req, head, &desc, 1065 &ewb2, digest_addr, 0, 1066 chain->para.hash_result_len, max_n_descs); 1067 if (unlikely(ewb->next == NULL)) { 1068 ret = VIRTIO_CRYPTO_ERR; 1069 goto error_exit; 1070 } 1071 1072 if (unlikely(copy_data(digest_addr, vc_req, head, &digest_desc, 1073 chain->para.hash_result_len, 1074 max_n_descs) < 0)) { 1075 VC_LOG_ERR("Incorrect virtio descriptor"); 1076 ret = VIRTIO_CRYPTO_BADMSG; 1077 goto error_exit; 1078 } 1079 1080 op->sym->auth.digest.data = digest_addr; 1081 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src, 1082 digest_offset); 1083 break; 1084 default: 1085 ret = VIRTIO_CRYPTO_BADMSG; 1086 goto error_exit; 1087 } 1088 1089 /* record inhdr */ 1090 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO); 1091 if (unlikely(vc_req->inhdr == NULL)) { 1092 ret = VIRTIO_CRYPTO_BADMSG; 1093 goto error_exit; 1094 } 1095 1096 vc_req->inhdr->status = VIRTIO_CRYPTO_OK; 1097 1098 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC; 1099 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION; 1100 1101 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset; 1102 op->sym->cipher.data.length = chain->para.src_data_len - 1103 chain->para.cipher_start_src_offset; 1104 1105 op->sym->auth.data.offset = chain->para.hash_start_src_offset; 1106 op->sym->auth.data.length = chain->para.len_to_hash; 1107 1108 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len + 1109 INHDR_LEN; 1110 return 0; 1111 1112 error_exit: 1113 if (vc_req->wb) 1114 free_wb_data(vc_req->wb, vc_req->wb_pool); 1115 vc_req->len = INHDR_LEN; 1116 return ret; 1117 } 1118 1119 /** 1120 * Process on descriptor 1121 */ 1122 static __rte_always_inline int 1123 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto, 1124 struct vhost_virtqueue *vq, struct rte_crypto_op *op, 1125 struct vring_desc *head, struct vhost_crypto_desc *descs, 1126 uint16_t desc_idx) 1127 { 1128 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src); 1129 struct rte_cryptodev_sym_session *session; 1130 struct virtio_crypto_op_data_req req; 1131 struct virtio_crypto_inhdr *inhdr; 1132 struct vhost_crypto_desc *desc = descs; 1133 struct vring_desc *src_desc; 1134 uint64_t session_id; 1135 uint64_t dlen; 1136 uint32_t nb_descs = 0, max_n_descs, i; 1137 int err; 1138 1139 vc_req->desc_idx = desc_idx; 1140 vc_req->dev = vcrypto->dev; 1141 vc_req->vq = vq; 1142 1143 if (unlikely((head->flags & VRING_DESC_F_INDIRECT) == 0)) { 1144 VC_LOG_ERR("Invalid descriptor"); 1145 return -1; 1146 } 1147 1148 dlen = head->len; 1149 src_desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr, 1150 &dlen, VHOST_ACCESS_RO); 1151 if (unlikely(!src_desc || dlen != head->len)) { 1152 VC_LOG_ERR("Invalid descriptor"); 1153 return -1; 1154 } 1155 head = src_desc; 1156 1157 nb_descs = max_n_descs = dlen / sizeof(struct vring_desc); 1158 if (unlikely(nb_descs > VHOST_CRYPTO_MAX_N_DESC || nb_descs == 0)) { 1159 err = VIRTIO_CRYPTO_ERR; 1160 VC_LOG_ERR("Cannot process num of descriptors %u", nb_descs); 1161 if (nb_descs > 0) { 1162 struct vring_desc *inhdr_desc = head; 1163 while (inhdr_desc->flags & VRING_DESC_F_NEXT) { 1164 if (inhdr_desc->next >= max_n_descs) 1165 return -1; 1166 inhdr_desc = &head[inhdr_desc->next]; 1167 } 1168 if (inhdr_desc->len != sizeof(*inhdr)) 1169 return -1; 1170 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, 1171 vc_req, inhdr_desc->addr, &dlen, 1172 VHOST_ACCESS_WO); 1173 if (unlikely(!inhdr || dlen != inhdr_desc->len)) 1174 return -1; 1175 inhdr->status = VIRTIO_CRYPTO_ERR; 1176 return -1; 1177 } 1178 } 1179 1180 /* copy descriptors to local variable */ 1181 for (i = 0; i < max_n_descs; i++) { 1182 desc->addr = src_desc->addr; 1183 desc->len = src_desc->len; 1184 desc->flags = src_desc->flags; 1185 desc++; 1186 if (unlikely((src_desc->flags & VRING_DESC_F_NEXT) == 0)) 1187 break; 1188 if (unlikely(src_desc->next >= max_n_descs)) { 1189 err = VIRTIO_CRYPTO_BADMSG; 1190 VC_LOG_ERR("Invalid descriptor"); 1191 goto error_exit; 1192 } 1193 src_desc = &head[src_desc->next]; 1194 } 1195 1196 vc_req->head = head; 1197 vc_req->zero_copy = vcrypto->option; 1198 1199 nb_descs = desc - descs; 1200 desc = descs; 1201 1202 if (unlikely(desc->len < sizeof(req))) { 1203 err = VIRTIO_CRYPTO_BADMSG; 1204 VC_LOG_ERR("Invalid descriptor"); 1205 goto error_exit; 1206 } 1207 1208 if (unlikely(copy_data(&req, vc_req, descs, &desc, sizeof(req), 1209 max_n_descs) < 0)) { 1210 err = VIRTIO_CRYPTO_BADMSG; 1211 VC_LOG_ERR("Invalid descriptor"); 1212 goto error_exit; 1213 } 1214 1215 /* desc is advanced by 1 now */ 1216 max_n_descs -= 1; 1217 1218 switch (req.header.opcode) { 1219 case VIRTIO_CRYPTO_CIPHER_ENCRYPT: 1220 case VIRTIO_CRYPTO_CIPHER_DECRYPT: 1221 session_id = req.header.session_id; 1222 1223 /* one branch to avoid unnecessary table lookup */ 1224 if (vcrypto->cache_session_id != session_id) { 1225 err = rte_hash_lookup_data(vcrypto->session_map, 1226 &session_id, (void **)&session); 1227 if (unlikely(err < 0)) { 1228 err = VIRTIO_CRYPTO_ERR; 1229 VC_LOG_ERR("Failed to find session %"PRIu64, 1230 session_id); 1231 goto error_exit; 1232 } 1233 1234 vcrypto->cache_session = session; 1235 vcrypto->cache_session_id = session_id; 1236 } 1237 1238 session = vcrypto->cache_session; 1239 1240 err = rte_crypto_op_attach_sym_session(op, session); 1241 if (unlikely(err < 0)) { 1242 err = VIRTIO_CRYPTO_ERR; 1243 VC_LOG_ERR("Failed to attach session to op"); 1244 goto error_exit; 1245 } 1246 1247 switch (req.u.sym_req.op_type) { 1248 case VIRTIO_CRYPTO_SYM_OP_NONE: 1249 err = VIRTIO_CRYPTO_NOTSUPP; 1250 break; 1251 case VIRTIO_CRYPTO_SYM_OP_CIPHER: 1252 err = prepare_sym_cipher_op(vcrypto, op, vc_req, 1253 &req.u.sym_req.u.cipher, desc, 1254 max_n_descs); 1255 break; 1256 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING: 1257 err = prepare_sym_chain_op(vcrypto, op, vc_req, 1258 &req.u.sym_req.u.chain, desc, 1259 max_n_descs); 1260 break; 1261 } 1262 if (unlikely(err != 0)) { 1263 VC_LOG_ERR("Failed to process sym request"); 1264 goto error_exit; 1265 } 1266 break; 1267 default: 1268 err = VIRTIO_CRYPTO_ERR; 1269 VC_LOG_ERR("Unsupported symmetric crypto request type %u", 1270 req.header.opcode); 1271 goto error_exit; 1272 } 1273 1274 return 0; 1275 1276 error_exit: 1277 1278 inhdr = reach_inhdr(vc_req, descs, max_n_descs); 1279 if (likely(inhdr != NULL)) 1280 inhdr->status = (uint8_t)err; 1281 1282 return -1; 1283 } 1284 1285 static __rte_always_inline struct vhost_virtqueue * 1286 vhost_crypto_finalize_one_request(struct rte_crypto_op *op, 1287 struct vhost_virtqueue *old_vq) 1288 { 1289 struct rte_mbuf *m_src = op->sym->m_src; 1290 struct rte_mbuf *m_dst = op->sym->m_dst; 1291 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src); 1292 struct vhost_virtqueue *vq; 1293 uint16_t used_idx, desc_idx; 1294 1295 if (unlikely(!vc_req)) { 1296 VC_LOG_ERR("Failed to retrieve vc_req"); 1297 return NULL; 1298 } 1299 vq = vc_req->vq; 1300 used_idx = vc_req->desc_idx; 1301 1302 if (old_vq && (vq != old_vq)) 1303 return vq; 1304 1305 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS)) 1306 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR; 1307 else { 1308 if (vc_req->zero_copy == 0) 1309 write_back_data(vc_req); 1310 } 1311 1312 desc_idx = vq->avail->ring[used_idx]; 1313 vq->used->ring[desc_idx].id = vq->avail->ring[desc_idx]; 1314 vq->used->ring[desc_idx].len = vc_req->len; 1315 1316 rte_mempool_put(m_src->pool, (void *)m_src); 1317 1318 if (m_dst) 1319 rte_mempool_put(m_dst->pool, (void *)m_dst); 1320 1321 return vc_req->vq; 1322 } 1323 1324 static __rte_always_inline uint16_t 1325 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops, 1326 uint16_t nb_ops, int *callfd) 1327 { 1328 uint16_t processed = 1; 1329 struct vhost_virtqueue *vq, *tmp_vq; 1330 1331 if (unlikely(nb_ops == 0)) 1332 return 0; 1333 1334 vq = vhost_crypto_finalize_one_request(ops[0], NULL); 1335 if (unlikely(vq == NULL)) 1336 return 0; 1337 tmp_vq = vq; 1338 1339 while ((processed < nb_ops)) { 1340 tmp_vq = vhost_crypto_finalize_one_request(ops[processed], 1341 tmp_vq); 1342 1343 if (unlikely(vq != tmp_vq)) 1344 break; 1345 1346 processed++; 1347 } 1348 1349 *callfd = vq->callfd; 1350 1351 *(volatile uint16_t *)&vq->used->idx += processed; 1352 1353 return processed; 1354 } 1355 1356 int 1357 rte_vhost_crypto_driver_start(const char *path) 1358 { 1359 uint64_t protocol_features; 1360 int ret; 1361 1362 ret = rte_vhost_driver_set_features(path, VIRTIO_CRYPTO_FEATURES); 1363 if (ret) 1364 return -1; 1365 1366 ret = rte_vhost_driver_get_protocol_features(path, &protocol_features); 1367 if (ret) 1368 return -1; 1369 protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_CONFIG); 1370 ret = rte_vhost_driver_set_protocol_features(path, protocol_features); 1371 if (ret) 1372 return -1; 1373 1374 return rte_vhost_driver_start(path); 1375 } 1376 1377 int 1378 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id, 1379 struct rte_mempool *sess_pool, 1380 int socket_id) 1381 { 1382 struct virtio_net *dev = get_device(vid); 1383 struct rte_hash_parameters params = {0}; 1384 struct vhost_crypto *vcrypto; 1385 char name[128]; 1386 int ret; 1387 1388 if (!dev) { 1389 VC_LOG_ERR("Invalid vid %i", vid); 1390 return -EINVAL; 1391 } 1392 1393 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto), 1394 RTE_CACHE_LINE_SIZE, socket_id); 1395 if (!vcrypto) { 1396 VC_LOG_ERR("Insufficient memory"); 1397 return -ENOMEM; 1398 } 1399 1400 vcrypto->sess_pool = sess_pool; 1401 vcrypto->cid = cryptodev_id; 1402 vcrypto->cache_session_id = UINT64_MAX; 1403 vcrypto->last_session_id = 1; 1404 vcrypto->dev = dev; 1405 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE; 1406 1407 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid); 1408 params.name = name; 1409 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES; 1410 params.hash_func = rte_jhash; 1411 params.key_len = sizeof(uint64_t); 1412 params.socket_id = socket_id; 1413 vcrypto->session_map = rte_hash_create(¶ms); 1414 if (!vcrypto->session_map) { 1415 VC_LOG_ERR("Failed to creath session map"); 1416 ret = -ENOMEM; 1417 goto error_exit; 1418 } 1419 1420 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid); 1421 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name, 1422 VHOST_CRYPTO_MBUF_POOL_SIZE, 512, 1423 sizeof(struct vhost_crypto_data_req), 1424 VHOST_CRYPTO_MAX_DATA_SIZE + RTE_PKTMBUF_HEADROOM, 1425 rte_socket_id()); 1426 if (!vcrypto->mbuf_pool) { 1427 VC_LOG_ERR("Failed to creath mbuf pool"); 1428 ret = -ENOMEM; 1429 goto error_exit; 1430 } 1431 1432 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid); 1433 vcrypto->wb_pool = rte_mempool_create(name, 1434 VHOST_CRYPTO_MBUF_POOL_SIZE, 1435 sizeof(struct vhost_crypto_writeback_data), 1436 128, 0, NULL, NULL, NULL, NULL, 1437 rte_socket_id(), 0); 1438 if (!vcrypto->wb_pool) { 1439 VC_LOG_ERR("Failed to creath mempool"); 1440 ret = -ENOMEM; 1441 goto error_exit; 1442 } 1443 1444 dev->extern_data = vcrypto; 1445 dev->extern_ops.pre_msg_handle = NULL; 1446 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler; 1447 1448 return 0; 1449 1450 error_exit: 1451 rte_hash_free(vcrypto->session_map); 1452 rte_mempool_free(vcrypto->mbuf_pool); 1453 1454 rte_free(vcrypto); 1455 1456 return ret; 1457 } 1458 1459 int 1460 rte_vhost_crypto_free(int vid) 1461 { 1462 struct virtio_net *dev = get_device(vid); 1463 struct vhost_crypto *vcrypto; 1464 1465 if (unlikely(dev == NULL)) { 1466 VC_LOG_ERR("Invalid vid %i", vid); 1467 return -EINVAL; 1468 } 1469 1470 vcrypto = dev->extern_data; 1471 if (unlikely(vcrypto == NULL)) { 1472 VC_LOG_ERR("Cannot find required data, is it initialized?"); 1473 return -ENOENT; 1474 } 1475 1476 rte_hash_free(vcrypto->session_map); 1477 rte_mempool_free(vcrypto->mbuf_pool); 1478 rte_mempool_free(vcrypto->wb_pool); 1479 rte_free(vcrypto); 1480 1481 dev->extern_data = NULL; 1482 dev->extern_ops.pre_msg_handle = NULL; 1483 dev->extern_ops.post_msg_handle = NULL; 1484 1485 return 0; 1486 } 1487 1488 int 1489 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option) 1490 { 1491 struct virtio_net *dev = get_device(vid); 1492 struct vhost_crypto *vcrypto; 1493 1494 if (unlikely(dev == NULL)) { 1495 VC_LOG_ERR("Invalid vid %i", vid); 1496 return -EINVAL; 1497 } 1498 1499 if (unlikely((uint32_t)option >= 1500 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) { 1501 VC_LOG_ERR("Invalid option %i", option); 1502 return -EINVAL; 1503 } 1504 1505 vcrypto = (struct vhost_crypto *)dev->extern_data; 1506 if (unlikely(vcrypto == NULL)) { 1507 VC_LOG_ERR("Cannot find required data, is it initialized?"); 1508 return -ENOENT; 1509 } 1510 1511 if (vcrypto->option == (uint8_t)option) 1512 return 0; 1513 1514 if (!(rte_mempool_full(vcrypto->mbuf_pool)) || 1515 !(rte_mempool_full(vcrypto->wb_pool))) { 1516 VC_LOG_ERR("Cannot update zero copy as mempool is not full"); 1517 return -EINVAL; 1518 } 1519 1520 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) { 1521 char name[128]; 1522 1523 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid); 1524 vcrypto->wb_pool = rte_mempool_create(name, 1525 VHOST_CRYPTO_MBUF_POOL_SIZE, 1526 sizeof(struct vhost_crypto_writeback_data), 1527 128, 0, NULL, NULL, NULL, NULL, 1528 rte_socket_id(), 0); 1529 if (!vcrypto->wb_pool) { 1530 VC_LOG_ERR("Failed to creath mbuf pool"); 1531 return -ENOMEM; 1532 } 1533 } else { 1534 rte_mempool_free(vcrypto->wb_pool); 1535 vcrypto->wb_pool = NULL; 1536 } 1537 1538 vcrypto->option = (uint8_t)option; 1539 1540 return 0; 1541 } 1542 1543 uint16_t 1544 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid, 1545 struct rte_crypto_op **ops, uint16_t nb_ops) 1546 { 1547 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2]; 1548 struct vhost_crypto_desc descs[VHOST_CRYPTO_MAX_N_DESC]; 1549 struct virtio_net *dev = get_device(vid); 1550 struct vhost_crypto *vcrypto; 1551 struct vhost_virtqueue *vq; 1552 uint16_t avail_idx; 1553 uint16_t start_idx; 1554 uint16_t count; 1555 uint16_t i = 0; 1556 1557 if (unlikely(dev == NULL)) { 1558 VC_LOG_ERR("Invalid vid %i", vid); 1559 return 0; 1560 } 1561 1562 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) { 1563 VC_LOG_ERR("Invalid qid %u", qid); 1564 return 0; 1565 } 1566 1567 vcrypto = (struct vhost_crypto *)dev->extern_data; 1568 if (unlikely(vcrypto == NULL)) { 1569 VC_LOG_ERR("Cannot find required data, is it initialized?"); 1570 return 0; 1571 } 1572 1573 vq = dev->virtqueue[qid]; 1574 1575 avail_idx = *((volatile uint16_t *)&vq->avail->idx); 1576 start_idx = vq->last_used_idx; 1577 count = avail_idx - start_idx; 1578 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE); 1579 count = RTE_MIN(count, nb_ops); 1580 1581 if (unlikely(count == 0)) 1582 return 0; 1583 1584 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise 1585 * we need only 1 mbuf as src and dst 1586 */ 1587 switch (vcrypto->option) { 1588 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE: 1589 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, 1590 (void **)mbufs, count * 2) < 0)) { 1591 VC_LOG_ERR("Insufficient memory"); 1592 return 0; 1593 } 1594 1595 for (i = 0; i < count; i++) { 1596 uint16_t used_idx = (start_idx + i) & (vq->size - 1); 1597 uint16_t desc_idx = vq->avail->ring[used_idx]; 1598 struct vring_desc *head = &vq->desc[desc_idx]; 1599 struct rte_crypto_op *op = ops[i]; 1600 1601 op->sym->m_src = mbufs[i * 2]; 1602 op->sym->m_dst = mbufs[i * 2 + 1]; 1603 op->sym->m_src->data_off = 0; 1604 op->sym->m_dst->data_off = 0; 1605 1606 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, 1607 op, head, descs, used_idx) < 0)) 1608 break; 1609 } 1610 1611 if (unlikely(i < count)) 1612 rte_mempool_put_bulk(vcrypto->mbuf_pool, 1613 (void **)&mbufs[i * 2], 1614 (count - i) * 2); 1615 1616 break; 1617 1618 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE: 1619 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, 1620 (void **)mbufs, count) < 0)) { 1621 VC_LOG_ERR("Insufficient memory"); 1622 return 0; 1623 } 1624 1625 for (i = 0; i < count; i++) { 1626 uint16_t used_idx = (start_idx + i) & (vq->size - 1); 1627 uint16_t desc_idx = vq->avail->ring[used_idx]; 1628 struct vring_desc *head = &vq->desc[desc_idx]; 1629 struct rte_crypto_op *op = ops[i]; 1630 1631 op->sym->m_src = mbufs[i]; 1632 op->sym->m_dst = NULL; 1633 op->sym->m_src->data_off = 0; 1634 1635 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, 1636 op, head, descs, desc_idx) < 0)) 1637 break; 1638 } 1639 1640 if (unlikely(i < count)) 1641 rte_mempool_put_bulk(vcrypto->mbuf_pool, 1642 (void **)&mbufs[i], 1643 count - i); 1644 1645 break; 1646 1647 } 1648 1649 vq->last_used_idx += i; 1650 1651 return i; 1652 } 1653 1654 uint16_t 1655 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops, 1656 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds) 1657 { 1658 struct rte_crypto_op **tmp_ops = ops; 1659 uint16_t count = 0, left = nb_ops; 1660 int callfd; 1661 uint16_t idx = 0; 1662 1663 while (left) { 1664 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left, 1665 &callfd); 1666 if (unlikely(count == 0)) 1667 break; 1668 1669 tmp_ops = &tmp_ops[count]; 1670 left -= count; 1671 1672 callfds[idx++] = callfd; 1673 1674 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) { 1675 VC_LOG_ERR("Too many vqs"); 1676 break; 1677 } 1678 } 1679 1680 *nb_callfds = idx; 1681 1682 return nb_ops - left; 1683 } 1684