1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
4 #include <rte_malloc.h>
8 #include <rte_cryptodev.h>
10 #include "rte_vhost_crypto.h"
12 #include "vhost_user.h"
13 #include "virtio_crypto.h"
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))
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)
27 #define VC_LOG_DBG(fmt, args...) \
28 RTE_LOG(DEBUG, USER1, "[%s] %s() line %u: " fmt "\n", \
29 "Vhost-Crypto", __func__, __LINE__, ## args)
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...)
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))
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))
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.
54 #define vhost_crypto_desc vring_desc
57 cipher_algo_transform(uint32_t virtio_cipher_algo,
58 enum rte_crypto_cipher_algorithm *algo)
60 switch (virtio_cipher_algo) {
61 case VIRTIO_CRYPTO_CIPHER_AES_CBC:
62 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
64 case VIRTIO_CRYPTO_CIPHER_AES_CTR:
65 *algo = RTE_CRYPTO_CIPHER_AES_CTR;
67 case VIRTIO_CRYPTO_CIPHER_DES_ECB:
68 *algo = -VIRTIO_CRYPTO_NOTSUPP;
70 case VIRTIO_CRYPTO_CIPHER_DES_CBC:
71 *algo = RTE_CRYPTO_CIPHER_DES_CBC;
73 case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
74 *algo = RTE_CRYPTO_CIPHER_3DES_ECB;
76 case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
77 *algo = RTE_CRYPTO_CIPHER_3DES_CBC;
79 case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
80 *algo = RTE_CRYPTO_CIPHER_3DES_CTR;
82 case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
83 *algo = RTE_CRYPTO_CIPHER_KASUMI_F8;
85 case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
86 *algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
88 case VIRTIO_CRYPTO_CIPHER_AES_F8:
89 *algo = RTE_CRYPTO_CIPHER_AES_F8;
91 case VIRTIO_CRYPTO_CIPHER_AES_XTS:
92 *algo = RTE_CRYPTO_CIPHER_AES_XTS;
94 case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
95 *algo = RTE_CRYPTO_CIPHER_ZUC_EEA3;
98 return -VIRTIO_CRYPTO_BADMSG;
106 auth_algo_transform(uint32_t virtio_auth_algo,
107 enum rte_crypto_auth_algorithm *algo)
109 switch (virtio_auth_algo) {
110 case VIRTIO_CRYPTO_NO_MAC:
111 *algo = RTE_CRYPTO_AUTH_NULL;
113 case VIRTIO_CRYPTO_MAC_HMAC_MD5:
114 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
116 case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
117 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
119 case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
120 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
122 case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
123 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
125 case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
126 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
128 case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
129 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
131 case VIRTIO_CRYPTO_MAC_CMAC_AES:
132 *algo = RTE_CRYPTO_AUTH_AES_CMAC;
134 case VIRTIO_CRYPTO_MAC_KASUMI_F9:
135 *algo = RTE_CRYPTO_AUTH_KASUMI_F9;
137 case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
138 *algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
140 case VIRTIO_CRYPTO_MAC_GMAC_AES:
141 *algo = RTE_CRYPTO_AUTH_AES_GMAC;
143 case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
144 *algo = RTE_CRYPTO_AUTH_AES_CBC_MAC;
146 case VIRTIO_CRYPTO_MAC_XCBC_AES:
147 *algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
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;
154 return -VIRTIO_CRYPTO_BADMSG;
160 static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
165 case RTE_CRYPTO_CIPHER_3DES_CBC:
168 case RTE_CRYPTO_CIPHER_3DES_CTR:
171 case RTE_CRYPTO_CIPHER_3DES_ECB:
174 case RTE_CRYPTO_CIPHER_AES_CBC:
178 /* TODO: add common algos */
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
193 struct vhost_crypto {
194 /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
197 struct rte_hash *session_map;
198 struct rte_mempool *mbuf_pool;
199 struct rte_mempool *sess_pool;
200 struct rte_mempool *sess_priv_pool;
201 struct rte_mempool *wb_pool;
203 /** DPDK cryptodev ID */
207 uint64_t last_session_id;
209 uint64_t cache_session_id;
210 struct rte_cryptodev_sym_session *cache_session;
211 /** socket id for the device */
214 struct virtio_net *dev;
217 } __rte_cache_aligned;
219 struct vhost_crypto_writeback_data {
223 struct vhost_crypto_writeback_data *next;
226 struct vhost_crypto_data_req {
227 struct vring_desc *head;
228 struct virtio_net *dev;
229 struct virtio_crypto_inhdr *inhdr;
230 struct vhost_virtqueue *vq;
231 struct vhost_crypto_writeback_data *wb;
232 struct rte_mempool *wb_pool;
239 transform_cipher_param(struct rte_crypto_sym_xform *xform,
240 VhostUserCryptoSessionParam *param)
244 ret = cipher_algo_transform(param->cipher_algo, &xform->cipher.algo);
245 if (unlikely(ret < 0))
248 if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
249 VC_LOG_DBG("Invalid cipher key length\n");
250 return -VIRTIO_CRYPTO_BADMSG;
253 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
254 xform->cipher.key.length = param->cipher_key_len;
255 if (xform->cipher.key.length > 0)
256 xform->cipher.key.data = param->cipher_key_buf;
257 if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
258 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
259 else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
260 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
262 VC_LOG_DBG("Bad operation type");
263 return -VIRTIO_CRYPTO_BADMSG;
266 ret = get_iv_len(xform->cipher.algo);
267 if (unlikely(ret < 0))
269 xform->cipher.iv.length = (uint16_t)ret;
270 xform->cipher.iv.offset = IV_OFFSET;
275 transform_chain_param(struct rte_crypto_sym_xform *xforms,
276 VhostUserCryptoSessionParam *param)
278 struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
281 switch (param->chaining_dir) {
282 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
284 xform_cipher = xforms->next;
285 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
286 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
288 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
289 xform_cipher = xforms;
290 xform_auth = xforms->next;
291 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
292 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
295 return -VIRTIO_CRYPTO_BADMSG;
299 ret = cipher_algo_transform(param->cipher_algo,
300 &xform_cipher->cipher.algo);
301 if (unlikely(ret < 0))
304 if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
305 VC_LOG_DBG("Invalid cipher key length\n");
306 return -VIRTIO_CRYPTO_BADMSG;
309 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
310 xform_cipher->cipher.key.length = param->cipher_key_len;
311 xform_cipher->cipher.key.data = param->cipher_key_buf;
312 ret = get_iv_len(xform_cipher->cipher.algo);
313 if (unlikely(ret < 0))
315 xform_cipher->cipher.iv.length = (uint16_t)ret;
316 xform_cipher->cipher.iv.offset = IV_OFFSET;
319 xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
320 ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo);
321 if (unlikely(ret < 0))
324 if (param->auth_key_len > VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH) {
325 VC_LOG_DBG("Invalid auth key length\n");
326 return -VIRTIO_CRYPTO_BADMSG;
329 xform_auth->auth.digest_length = param->digest_len;
330 xform_auth->auth.key.length = param->auth_key_len;
331 xform_auth->auth.key.data = param->auth_key_buf;
337 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
338 VhostUserCryptoSessionParam *sess_param)
340 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
341 struct rte_cryptodev_sym_session *session;
344 switch (sess_param->op_type) {
345 case VIRTIO_CRYPTO_SYM_OP_NONE:
346 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
347 ret = transform_cipher_param(&xform1, sess_param);
349 VC_LOG_ERR("Error transform session msg (%i)", ret);
350 sess_param->session_id = ret;
354 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
355 if (unlikely(sess_param->hash_mode !=
356 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
357 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
358 VC_LOG_ERR("Error transform session message (%i)",
359 -VIRTIO_CRYPTO_NOTSUPP);
363 xform1.next = &xform2;
365 ret = transform_chain_param(&xform1, sess_param);
367 VC_LOG_ERR("Error transform session message (%i)", ret);
368 sess_param->session_id = ret;
374 VC_LOG_ERR("Algorithm not yet supported");
375 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
379 session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
381 VC_LOG_ERR("Failed to create session");
382 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
386 if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
387 vcrypto->sess_priv_pool) < 0) {
388 VC_LOG_ERR("Failed to initialize session");
389 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
393 /* insert hash to map */
394 if (rte_hash_add_key_data(vcrypto->session_map,
395 &vcrypto->last_session_id, session) < 0) {
396 VC_LOG_ERR("Failed to insert session to hash table");
398 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
399 VC_LOG_ERR("Failed to clear session");
401 if (rte_cryptodev_sym_session_free(session) < 0)
402 VC_LOG_ERR("Failed to free session");
404 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
408 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
409 vcrypto->last_session_id, vcrypto->dev->vid);
411 sess_param->session_id = vcrypto->last_session_id;
412 vcrypto->last_session_id++;
416 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
418 struct rte_cryptodev_sym_session *session;
419 uint64_t sess_id = session_id;
422 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
425 if (unlikely(ret < 0)) {
426 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
427 return -VIRTIO_CRYPTO_INVSESS;
430 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
431 VC_LOG_DBG("Failed to clear session");
432 return -VIRTIO_CRYPTO_ERR;
435 if (rte_cryptodev_sym_session_free(session) < 0) {
436 VC_LOG_DBG("Failed to free session");
437 return -VIRTIO_CRYPTO_ERR;
440 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
441 VC_LOG_DBG("Failed to delete session from hash table.");
442 return -VIRTIO_CRYPTO_ERR;
445 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
451 static enum rte_vhost_msg_result
452 vhost_crypto_msg_post_handler(int vid, void *msg)
454 struct virtio_net *dev = get_device(vid);
455 struct vhost_crypto *vcrypto;
456 VhostUserMsg *vmsg = msg;
457 enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK;
460 VC_LOG_ERR("Invalid vid %i", vid);
461 return RTE_VHOST_MSG_RESULT_ERR;
464 vcrypto = dev->extern_data;
465 if (vcrypto == NULL) {
466 VC_LOG_ERR("Cannot find required data, is it initialized?");
467 return RTE_VHOST_MSG_RESULT_ERR;
470 switch (vmsg->request.master) {
471 case VHOST_USER_CRYPTO_CREATE_SESS:
472 vhost_crypto_create_sess(vcrypto,
473 &vmsg->payload.crypto_session);
475 ret = RTE_VHOST_MSG_RESULT_REPLY;
477 case VHOST_USER_CRYPTO_CLOSE_SESS:
478 if (vhost_crypto_close_sess(vcrypto, vmsg->payload.u64))
479 ret = RTE_VHOST_MSG_RESULT_ERR;
482 ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED;
489 static __rte_always_inline struct vhost_crypto_desc *
490 find_write_desc(struct vhost_crypto_desc *head, struct vhost_crypto_desc *desc,
491 uint32_t max_n_descs)
496 while (desc - head < (int)max_n_descs) {
497 if (desc->flags & VRING_DESC_F_WRITE)
505 static __rte_always_inline struct virtio_crypto_inhdr *
506 reach_inhdr(struct vhost_crypto_data_req *vc_req,
507 struct vhost_crypto_desc *head,
508 uint32_t max_n_descs)
510 struct virtio_crypto_inhdr *inhdr;
511 struct vhost_crypto_desc *last = head + (max_n_descs - 1);
512 uint64_t dlen = last->len;
514 if (unlikely(dlen != sizeof(*inhdr)))
517 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, last->addr,
518 &dlen, VHOST_ACCESS_WO);
519 if (unlikely(!inhdr || dlen != last->len))
525 static __rte_always_inline int
526 move_desc(struct vhost_crypto_desc *head,
527 struct vhost_crypto_desc **cur_desc,
528 uint32_t size, uint32_t max_n_descs)
530 struct vhost_crypto_desc *desc = *cur_desc;
531 int left = size - desc->len;
533 while (desc->flags & VRING_DESC_F_NEXT && left > 0 &&
535 desc - head < (int)max_n_descs) {
540 if (unlikely(left > 0))
543 if (unlikely(head - desc == (int)max_n_descs))
546 *cur_desc = desc + 1;
551 static __rte_always_inline void *
552 get_data_ptr(struct vhost_crypto_data_req *vc_req,
553 struct vhost_crypto_desc *cur_desc,
557 uint64_t dlen = cur_desc->len;
559 data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
560 if (unlikely(!data || dlen != cur_desc->len)) {
561 VC_LOG_ERR("Failed to map object");
568 static __rte_always_inline int
569 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
570 struct vhost_crypto_desc *head,
571 struct vhost_crypto_desc **cur_desc,
572 uint32_t size, uint32_t max_n_descs)
574 struct vhost_crypto_desc *desc = *cur_desc;
575 uint64_t remain, addr, dlen, len;
577 uint8_t *data = dst_data;
581 to_copy = RTE_MIN(desc->len, (uint32_t)left);
583 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
585 if (unlikely(!src || !dlen))
588 rte_memcpy((uint8_t *)data, src, dlen);
591 if (unlikely(dlen < to_copy)) {
592 remain = to_copy - dlen;
593 addr = desc->addr + dlen;
597 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
599 if (unlikely(!src || !len)) {
600 VC_LOG_ERR("Failed to map descriptor");
604 rte_memcpy(data, src, len);
613 while (desc >= head && desc - head < (int)max_n_descs && left) {
615 to_copy = RTE_MIN(desc->len, (uint32_t)left);
617 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
619 if (unlikely(!src || !dlen)) {
620 VC_LOG_ERR("Failed to map descriptor");
624 rte_memcpy(data, src, dlen);
627 if (unlikely(dlen < to_copy)) {
628 remain = to_copy - dlen;
629 addr = desc->addr + dlen;
633 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
635 if (unlikely(!src || !len)) {
636 VC_LOG_ERR("Failed to map descriptor");
640 rte_memcpy(data, src, len);
650 if (unlikely(left > 0)) {
651 VC_LOG_ERR("Incorrect virtio descriptor");
655 if (unlikely(desc - head == (int)max_n_descs))
658 *cur_desc = desc + 1;
664 write_back_data(struct vhost_crypto_data_req *vc_req)
666 struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
669 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
670 memset(wb_data->src, 0, wb_data->len);
672 wb_data = wb_data->next;
673 rte_mempool_put(vc_req->wb_pool, wb_last);
678 free_wb_data(struct vhost_crypto_writeback_data *wb_data,
679 struct rte_mempool *mp)
681 while (wb_data->next != NULL)
682 free_wb_data(wb_data->next, mp);
684 rte_mempool_put(mp, wb_data);
688 * The function will allocate a vhost_crypto_writeback_data linked list
689 * containing the source and destination data pointers for the write back
690 * operation after dequeued from Cryptodev PMD queues.
693 * The vhost crypto data request pointer
695 * The pointer of the current in use descriptor pointer. The content of
696 * cur_desc is expected to be updated after the function execution.
698 * The last write back data element to be returned. It is used only in cipher
699 * and hash chain operations.
701 * The source data pointer
703 * The offset to both source and destination data. For source data the offset
704 * is the number of bytes between src and start point of cipher operation. For
705 * destination data the offset is the number of bytes from *cur_desc->addr
706 * to the point where the src will be written to.
707 * @param write_back_len
708 * The size of the write back length.
710 * The pointer to the start of the write back data linked list.
712 static __rte_always_inline struct vhost_crypto_writeback_data *
713 prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
714 struct vhost_crypto_desc *head_desc,
715 struct vhost_crypto_desc **cur_desc,
716 struct vhost_crypto_writeback_data **end_wb_data,
719 uint64_t write_back_len,
720 uint32_t max_n_descs)
722 struct vhost_crypto_writeback_data *wb_data, *head;
723 struct vhost_crypto_desc *desc = *cur_desc;
728 ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
729 if (unlikely(ret < 0)) {
730 VC_LOG_ERR("no memory");
736 if (likely(desc->len > offset)) {
737 wb_data->src = src + offset;
739 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
740 &dlen, VHOST_ACCESS_RW);
741 if (unlikely(!dst || dlen != desc->len)) {
742 VC_LOG_ERR("Failed to map descriptor");
746 wb_data->dst = dst + offset;
747 wb_data->len = RTE_MIN(dlen - offset, write_back_len);
748 write_back_len -= wb_data->len;
749 src += offset + wb_data->len;
752 if (unlikely(write_back_len)) {
753 ret = rte_mempool_get(vc_req->wb_pool,
754 (void **)&(wb_data->next));
755 if (unlikely(ret < 0)) {
756 VC_LOG_ERR("no memory");
760 wb_data = wb_data->next;
762 wb_data->next = NULL;
766 while (write_back_len &&
768 desc - head_desc < (int)max_n_descs) {
770 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
771 VC_LOG_ERR("incorrect descriptor");
775 if (desc->len <= offset) {
781 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
782 VHOST_ACCESS_RW) + offset;
783 if (unlikely(dst == NULL || dlen != desc->len)) {
784 VC_LOG_ERR("Failed to map descriptor");
788 wb_data->src = src + offset;
790 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
791 write_back_len -= wb_data->len;
795 if (write_back_len) {
796 ret = rte_mempool_get(vc_req->wb_pool,
797 (void **)&(wb_data->next));
798 if (unlikely(ret < 0)) {
799 VC_LOG_ERR("no memory");
803 wb_data = wb_data->next;
805 wb_data->next = NULL;
808 if (unlikely(desc - head_desc == (int)max_n_descs))
811 *cur_desc = desc + 1;
813 *end_wb_data = wb_data;
819 free_wb_data(head, vc_req->wb_pool);
824 static __rte_always_inline uint8_t
825 vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req)
827 if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
828 (req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) &&
829 (req->para.dst_data_len >= req->para.src_data_len) &&
830 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE)))
831 return VIRTIO_CRYPTO_OK;
832 return VIRTIO_CRYPTO_BADMSG;
835 static __rte_always_inline uint8_t
836 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
837 struct vhost_crypto_data_req *vc_req,
838 struct virtio_crypto_cipher_data_req *cipher,
839 struct vhost_crypto_desc *head,
840 uint32_t max_n_descs)
842 struct vhost_crypto_desc *desc = head;
843 struct vhost_crypto_writeback_data *ewb = NULL;
844 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
845 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
846 uint8_t ret = vhost_crypto_check_cipher_request(cipher);
848 if (unlikely(ret != VIRTIO_CRYPTO_OK))
853 if (unlikely(copy_data(iv_data, vc_req, head, &desc,
854 cipher->para.iv_len, max_n_descs))) {
855 ret = VIRTIO_CRYPTO_BADMSG;
859 switch (vcrypto->option) {
860 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
861 m_src->data_len = cipher->para.src_data_len;
862 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
863 cipher->para.src_data_len);
864 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
865 if (unlikely(m_src->buf_iova == 0 ||
866 m_src->buf_addr == NULL)) {
867 VC_LOG_ERR("zero_copy may fail due to cross page data");
868 ret = VIRTIO_CRYPTO_ERR;
872 if (unlikely(move_desc(head, &desc, cipher->para.src_data_len,
874 VC_LOG_ERR("Incorrect descriptor");
875 ret = VIRTIO_CRYPTO_ERR;
880 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
881 vc_req->wb_pool = vcrypto->wb_pool;
882 m_src->data_len = cipher->para.src_data_len;
883 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
884 vc_req, head, &desc, cipher->para.src_data_len,
886 ret = VIRTIO_CRYPTO_BADMSG;
891 ret = VIRTIO_CRYPTO_BADMSG;
896 desc = find_write_desc(head, desc, max_n_descs);
897 if (unlikely(!desc)) {
898 VC_LOG_ERR("Cannot find write location");
899 ret = VIRTIO_CRYPTO_BADMSG;
903 switch (vcrypto->option) {
904 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
905 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
906 desc->addr, cipher->para.dst_data_len);
907 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
908 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
909 VC_LOG_ERR("zero_copy may fail due to cross page data");
910 ret = VIRTIO_CRYPTO_ERR;
914 if (unlikely(move_desc(head, &desc, cipher->para.dst_data_len,
916 VC_LOG_ERR("Incorrect descriptor");
917 ret = VIRTIO_CRYPTO_ERR;
921 m_dst->data_len = cipher->para.dst_data_len;
923 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
924 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb,
925 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
926 cipher->para.dst_data_len, max_n_descs);
927 if (unlikely(vc_req->wb == NULL)) {
928 ret = VIRTIO_CRYPTO_ERR;
934 ret = VIRTIO_CRYPTO_BADMSG;
939 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
940 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
942 op->sym->cipher.data.offset = 0;
943 op->sym->cipher.data.length = cipher->para.src_data_len;
945 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
946 if (unlikely(vc_req->inhdr == NULL)) {
947 ret = VIRTIO_CRYPTO_BADMSG;
951 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
952 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
958 free_wb_data(vc_req->wb, vc_req->wb_pool);
960 vc_req->len = INHDR_LEN;
964 static __rte_always_inline uint8_t
965 vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req)
967 if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
968 (req->para.src_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
969 (req->para.dst_data_len >= req->para.src_data_len) &&
970 (req->para.dst_data_len <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
971 (req->para.cipher_start_src_offset <
972 VHOST_CRYPTO_MAX_DATA_SIZE) &&
973 (req->para.len_to_cipher <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
974 (req->para.hash_start_src_offset <
975 VHOST_CRYPTO_MAX_DATA_SIZE) &&
976 (req->para.len_to_hash <= VHOST_CRYPTO_MAX_DATA_SIZE) &&
977 (req->para.cipher_start_src_offset + req->para.len_to_cipher <=
978 req->para.src_data_len) &&
979 (req->para.hash_start_src_offset + req->para.len_to_hash <=
980 req->para.src_data_len) &&
981 (req->para.dst_data_len + req->para.hash_result_len <=
982 VHOST_CRYPTO_MAX_DATA_SIZE)))
983 return VIRTIO_CRYPTO_OK;
984 return VIRTIO_CRYPTO_BADMSG;
987 static __rte_always_inline uint8_t
988 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
989 struct vhost_crypto_data_req *vc_req,
990 struct virtio_crypto_alg_chain_data_req *chain,
991 struct vhost_crypto_desc *head,
992 uint32_t max_n_descs)
994 struct vhost_crypto_desc *desc = head, *digest_desc;
995 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
996 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
997 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
998 uint32_t digest_offset;
1000 uint8_t ret = vhost_crypto_check_chain_request(chain);
1002 if (unlikely(ret != VIRTIO_CRYPTO_OK))
1007 if (unlikely(copy_data(iv_data, vc_req, head, &desc,
1008 chain->para.iv_len, max_n_descs) < 0)) {
1009 ret = VIRTIO_CRYPTO_BADMSG;
1013 switch (vcrypto->option) {
1014 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1015 m_src->data_len = chain->para.src_data_len;
1016 m_dst->data_len = chain->para.dst_data_len;
1018 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
1019 chain->para.src_data_len);
1020 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1021 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
1022 VC_LOG_ERR("zero_copy may fail due to cross page data");
1023 ret = VIRTIO_CRYPTO_ERR;
1027 if (unlikely(move_desc(head, &desc, chain->para.src_data_len,
1028 max_n_descs) < 0)) {
1029 VC_LOG_ERR("Incorrect descriptor");
1030 ret = VIRTIO_CRYPTO_ERR;
1034 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1035 vc_req->wb_pool = vcrypto->wb_pool;
1036 m_src->data_len = chain->para.src_data_len;
1037 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1038 vc_req, head, &desc, chain->para.src_data_len,
1039 max_n_descs) < 0)) {
1040 ret = VIRTIO_CRYPTO_BADMSG;
1046 ret = VIRTIO_CRYPTO_BADMSG;
1051 desc = find_write_desc(head, desc, max_n_descs);
1052 if (unlikely(!desc)) {
1053 VC_LOG_ERR("Cannot find write location");
1054 ret = VIRTIO_CRYPTO_BADMSG;
1058 switch (vcrypto->option) {
1059 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1060 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
1061 desc->addr, chain->para.dst_data_len);
1062 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1063 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
1064 VC_LOG_ERR("zero_copy may fail due to cross page data");
1065 ret = VIRTIO_CRYPTO_ERR;
1069 if (unlikely(move_desc(vc_req->head, &desc,
1070 chain->para.dst_data_len, max_n_descs) < 0)) {
1071 VC_LOG_ERR("Incorrect descriptor");
1072 ret = VIRTIO_CRYPTO_ERR;
1076 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1077 desc->addr, chain->para.hash_result_len);
1078 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1080 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1081 VC_LOG_ERR("zero_copy may fail due to cross page data");
1082 ret = VIRTIO_CRYPTO_ERR;
1086 if (unlikely(move_desc(head, &desc,
1087 chain->para.hash_result_len,
1088 max_n_descs) < 0)) {
1089 VC_LOG_ERR("Incorrect descriptor");
1090 ret = VIRTIO_CRYPTO_ERR;
1095 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1096 vc_req->wb = prepare_write_back_data(vc_req, head, &desc, &ewb,
1097 rte_pktmbuf_mtod(m_src, uint8_t *),
1098 chain->para.cipher_start_src_offset,
1099 chain->para.dst_data_len -
1100 chain->para.cipher_start_src_offset,
1102 if (unlikely(vc_req->wb == NULL)) {
1103 ret = VIRTIO_CRYPTO_ERR;
1108 digest_offset = m_src->data_len;
1109 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1112 /** create a wb_data for digest */
1113 ewb->next = prepare_write_back_data(vc_req, head, &desc,
1114 &ewb2, digest_addr, 0,
1115 chain->para.hash_result_len, max_n_descs);
1116 if (unlikely(ewb->next == NULL)) {
1117 ret = VIRTIO_CRYPTO_ERR;
1121 if (unlikely(copy_data(digest_addr, vc_req, head, &digest_desc,
1122 chain->para.hash_result_len,
1123 max_n_descs) < 0)) {
1124 ret = VIRTIO_CRYPTO_BADMSG;
1128 op->sym->auth.digest.data = digest_addr;
1129 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1133 ret = VIRTIO_CRYPTO_BADMSG;
1138 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1139 if (unlikely(vc_req->inhdr == NULL)) {
1140 ret = VIRTIO_CRYPTO_BADMSG;
1144 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1146 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1147 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1149 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1150 op->sym->cipher.data.length = chain->para.src_data_len -
1151 chain->para.cipher_start_src_offset;
1153 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1154 op->sym->auth.data.length = chain->para.len_to_hash;
1156 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1162 free_wb_data(vc_req->wb, vc_req->wb_pool);
1163 vc_req->len = INHDR_LEN;
1168 * Process on descriptor
1170 static __rte_always_inline int
1171 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1172 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1173 struct vring_desc *head, struct vhost_crypto_desc *descs,
1176 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1177 struct rte_cryptodev_sym_session *session;
1178 struct virtio_crypto_op_data_req req;
1179 struct virtio_crypto_inhdr *inhdr;
1180 struct vhost_crypto_desc *desc = descs;
1181 struct vring_desc *src_desc;
1182 uint64_t session_id;
1184 uint32_t nb_descs = 0, max_n_descs, i;
1187 vc_req->desc_idx = desc_idx;
1188 vc_req->dev = vcrypto->dev;
1191 if (unlikely((head->flags & VRING_DESC_F_INDIRECT) == 0)) {
1192 VC_LOG_ERR("Invalid descriptor");
1197 src_desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1198 &dlen, VHOST_ACCESS_RO);
1199 if (unlikely(!src_desc || dlen != head->len)) {
1200 VC_LOG_ERR("Invalid descriptor");
1205 nb_descs = max_n_descs = dlen / sizeof(struct vring_desc);
1206 if (unlikely(nb_descs > VHOST_CRYPTO_MAX_N_DESC || nb_descs == 0)) {
1207 err = VIRTIO_CRYPTO_ERR;
1208 VC_LOG_ERR("Cannot process num of descriptors %u", nb_descs);
1210 struct vring_desc *inhdr_desc = head;
1211 while (inhdr_desc->flags & VRING_DESC_F_NEXT) {
1212 if (inhdr_desc->next >= max_n_descs)
1214 inhdr_desc = &head[inhdr_desc->next];
1216 if (inhdr_desc->len != sizeof(*inhdr))
1218 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *,
1219 vc_req, inhdr_desc->addr, &dlen,
1221 if (unlikely(!inhdr || dlen != inhdr_desc->len))
1223 inhdr->status = VIRTIO_CRYPTO_ERR;
1228 /* copy descriptors to local variable */
1229 for (i = 0; i < max_n_descs; i++) {
1230 desc->addr = src_desc->addr;
1231 desc->len = src_desc->len;
1232 desc->flags = src_desc->flags;
1234 if (unlikely((src_desc->flags & VRING_DESC_F_NEXT) == 0))
1236 if (unlikely(src_desc->next >= max_n_descs)) {
1237 err = VIRTIO_CRYPTO_BADMSG;
1238 VC_LOG_ERR("Invalid descriptor");
1241 src_desc = &head[src_desc->next];
1244 vc_req->head = head;
1245 vc_req->zero_copy = vcrypto->option;
1247 nb_descs = desc - descs;
1250 if (unlikely(desc->len < sizeof(req))) {
1251 err = VIRTIO_CRYPTO_BADMSG;
1252 VC_LOG_ERR("Invalid descriptor");
1256 if (unlikely(copy_data(&req, vc_req, descs, &desc, sizeof(req),
1257 max_n_descs) < 0)) {
1258 err = VIRTIO_CRYPTO_BADMSG;
1259 VC_LOG_ERR("Invalid descriptor");
1263 /* desc is advanced by 1 now */
1266 switch (req.header.opcode) {
1267 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1268 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1269 session_id = req.header.session_id;
1271 /* one branch to avoid unnecessary table lookup */
1272 if (vcrypto->cache_session_id != session_id) {
1273 err = rte_hash_lookup_data(vcrypto->session_map,
1274 &session_id, (void **)&session);
1275 if (unlikely(err < 0)) {
1276 err = VIRTIO_CRYPTO_ERR;
1277 VC_LOG_ERR("Failed to find session %"PRIu64,
1282 vcrypto->cache_session = session;
1283 vcrypto->cache_session_id = session_id;
1286 session = vcrypto->cache_session;
1288 err = rte_crypto_op_attach_sym_session(op, session);
1289 if (unlikely(err < 0)) {
1290 err = VIRTIO_CRYPTO_ERR;
1291 VC_LOG_ERR("Failed to attach session to op");
1295 switch (req.u.sym_req.op_type) {
1296 case VIRTIO_CRYPTO_SYM_OP_NONE:
1297 err = VIRTIO_CRYPTO_NOTSUPP;
1299 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1300 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1301 &req.u.sym_req.u.cipher, desc,
1304 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1305 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1306 &req.u.sym_req.u.chain, desc,
1310 if (unlikely(err != 0)) {
1311 VC_LOG_ERR("Failed to process sym request");
1316 err = VIRTIO_CRYPTO_ERR;
1317 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1326 inhdr = reach_inhdr(vc_req, descs, max_n_descs);
1327 if (likely(inhdr != NULL))
1328 inhdr->status = (uint8_t)err;
1333 static __rte_always_inline struct vhost_virtqueue *
1334 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1335 struct vhost_virtqueue *old_vq)
1337 struct rte_mbuf *m_src = op->sym->m_src;
1338 struct rte_mbuf *m_dst = op->sym->m_dst;
1339 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1340 struct vhost_virtqueue *vq;
1341 uint16_t used_idx, desc_idx;
1343 if (unlikely(!vc_req)) {
1344 VC_LOG_ERR("Failed to retrieve vc_req");
1348 used_idx = vc_req->desc_idx;
1350 if (old_vq && (vq != old_vq))
1353 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1354 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1356 if (vc_req->zero_copy == 0)
1357 write_back_data(vc_req);
1360 desc_idx = vq->avail->ring[used_idx];
1361 vq->used->ring[desc_idx].id = vq->avail->ring[desc_idx];
1362 vq->used->ring[desc_idx].len = vc_req->len;
1364 rte_mempool_put(m_src->pool, (void *)m_src);
1367 rte_mempool_put(m_dst->pool, (void *)m_dst);
1372 static __rte_always_inline uint16_t
1373 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1374 uint16_t nb_ops, int *callfd)
1376 uint16_t processed = 1;
1377 struct vhost_virtqueue *vq, *tmp_vq;
1379 if (unlikely(nb_ops == 0))
1382 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1383 if (unlikely(vq == NULL))
1387 while ((processed < nb_ops)) {
1388 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1391 if (unlikely(vq != tmp_vq))
1397 *callfd = vq->callfd;
1399 *(volatile uint16_t *)&vq->used->idx += processed;
1405 rte_vhost_crypto_driver_start(const char *path)
1407 uint64_t protocol_features;
1410 ret = rte_vhost_driver_set_features(path, VIRTIO_CRYPTO_FEATURES);
1414 ret = rte_vhost_driver_get_protocol_features(path, &protocol_features);
1417 protocol_features |= (1ULL << VHOST_USER_PROTOCOL_F_CONFIG);
1418 ret = rte_vhost_driver_set_protocol_features(path, protocol_features);
1422 return rte_vhost_driver_start(path);
1426 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1427 struct rte_mempool *sess_pool,
1428 struct rte_mempool *sess_priv_pool,
1431 struct virtio_net *dev = get_device(vid);
1432 struct rte_hash_parameters params = {0};
1433 struct vhost_crypto *vcrypto;
1438 VC_LOG_ERR("Invalid vid %i", vid);
1442 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1443 RTE_CACHE_LINE_SIZE, socket_id);
1445 VC_LOG_ERR("Insufficient memory");
1449 vcrypto->sess_pool = sess_pool;
1450 vcrypto->sess_priv_pool = sess_priv_pool;
1451 vcrypto->cid = cryptodev_id;
1452 vcrypto->cache_session_id = UINT64_MAX;
1453 vcrypto->last_session_id = 1;
1455 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1457 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1459 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1460 params.hash_func = rte_jhash;
1461 params.key_len = sizeof(uint64_t);
1462 params.socket_id = socket_id;
1463 vcrypto->session_map = rte_hash_create(¶ms);
1464 if (!vcrypto->session_map) {
1465 VC_LOG_ERR("Failed to creath session map");
1470 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1471 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1472 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1473 sizeof(struct vhost_crypto_data_req),
1474 VHOST_CRYPTO_MAX_DATA_SIZE + RTE_PKTMBUF_HEADROOM,
1476 if (!vcrypto->mbuf_pool) {
1477 VC_LOG_ERR("Failed to creath mbuf pool");
1482 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1483 vcrypto->wb_pool = rte_mempool_create(name,
1484 VHOST_CRYPTO_MBUF_POOL_SIZE,
1485 sizeof(struct vhost_crypto_writeback_data),
1486 128, 0, NULL, NULL, NULL, NULL,
1487 rte_socket_id(), 0);
1488 if (!vcrypto->wb_pool) {
1489 VC_LOG_ERR("Failed to creath mempool");
1494 dev->extern_data = vcrypto;
1495 dev->extern_ops.pre_msg_handle = NULL;
1496 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1501 if (vcrypto->session_map)
1502 rte_hash_free(vcrypto->session_map);
1503 if (vcrypto->mbuf_pool)
1504 rte_mempool_free(vcrypto->mbuf_pool);
1512 rte_vhost_crypto_free(int vid)
1514 struct virtio_net *dev = get_device(vid);
1515 struct vhost_crypto *vcrypto;
1517 if (unlikely(dev == NULL)) {
1518 VC_LOG_ERR("Invalid vid %i", vid);
1522 vcrypto = dev->extern_data;
1523 if (unlikely(vcrypto == NULL)) {
1524 VC_LOG_ERR("Cannot find required data, is it initialized?");
1528 rte_hash_free(vcrypto->session_map);
1529 rte_mempool_free(vcrypto->mbuf_pool);
1530 rte_mempool_free(vcrypto->wb_pool);
1533 dev->extern_data = NULL;
1534 dev->extern_ops.pre_msg_handle = NULL;
1535 dev->extern_ops.post_msg_handle = NULL;
1541 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1543 struct virtio_net *dev = get_device(vid);
1544 struct vhost_crypto *vcrypto;
1546 if (unlikely(dev == NULL)) {
1547 VC_LOG_ERR("Invalid vid %i", vid);
1551 if (unlikely((uint32_t)option >=
1552 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1553 VC_LOG_ERR("Invalid option %i", option);
1557 vcrypto = (struct vhost_crypto *)dev->extern_data;
1558 if (unlikely(vcrypto == NULL)) {
1559 VC_LOG_ERR("Cannot find required data, is it initialized?");
1563 if (vcrypto->option == (uint8_t)option)
1566 if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1567 !(rte_mempool_full(vcrypto->wb_pool))) {
1568 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1572 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1575 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1576 vcrypto->wb_pool = rte_mempool_create(name,
1577 VHOST_CRYPTO_MBUF_POOL_SIZE,
1578 sizeof(struct vhost_crypto_writeback_data),
1579 128, 0, NULL, NULL, NULL, NULL,
1580 rte_socket_id(), 0);
1581 if (!vcrypto->wb_pool) {
1582 VC_LOG_ERR("Failed to creath mbuf pool");
1586 rte_mempool_free(vcrypto->wb_pool);
1587 vcrypto->wb_pool = NULL;
1590 vcrypto->option = (uint8_t)option;
1596 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1597 struct rte_crypto_op **ops, uint16_t nb_ops)
1599 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1600 struct vhost_crypto_desc descs[VHOST_CRYPTO_MAX_N_DESC];
1601 struct virtio_net *dev = get_device(vid);
1602 struct vhost_crypto *vcrypto;
1603 struct vhost_virtqueue *vq;
1609 if (unlikely(dev == NULL)) {
1610 VC_LOG_ERR("Invalid vid %i", vid);
1614 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1615 VC_LOG_ERR("Invalid qid %u", qid);
1619 vcrypto = (struct vhost_crypto *)dev->extern_data;
1620 if (unlikely(vcrypto == NULL)) {
1621 VC_LOG_ERR("Cannot find required data, is it initialized?");
1625 vq = dev->virtqueue[qid];
1627 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1628 start_idx = vq->last_used_idx;
1629 count = avail_idx - start_idx;
1630 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1631 count = RTE_MIN(count, nb_ops);
1633 if (unlikely(count == 0))
1636 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1637 * we need only 1 mbuf as src and dst
1639 switch (vcrypto->option) {
1640 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1641 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1642 (void **)mbufs, count * 2) < 0)) {
1643 VC_LOG_ERR("Insufficient memory");
1647 for (i = 0; i < count; i++) {
1648 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1649 uint16_t desc_idx = vq->avail->ring[used_idx];
1650 struct vring_desc *head = &vq->desc[desc_idx];
1651 struct rte_crypto_op *op = ops[i];
1653 op->sym->m_src = mbufs[i * 2];
1654 op->sym->m_dst = mbufs[i * 2 + 1];
1655 op->sym->m_src->data_off = 0;
1656 op->sym->m_dst->data_off = 0;
1658 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1659 op, head, descs, used_idx) < 0))
1663 if (unlikely(i < count))
1664 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1665 (void **)&mbufs[i * 2],
1670 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1671 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1672 (void **)mbufs, count) < 0)) {
1673 VC_LOG_ERR("Insufficient memory");
1677 for (i = 0; i < count; i++) {
1678 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1679 uint16_t desc_idx = vq->avail->ring[used_idx];
1680 struct vring_desc *head = &vq->desc[desc_idx];
1681 struct rte_crypto_op *op = ops[i];
1683 op->sym->m_src = mbufs[i];
1684 op->sym->m_dst = NULL;
1685 op->sym->m_src->data_off = 0;
1687 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1688 op, head, descs, desc_idx) < 0))
1692 if (unlikely(i < count))
1693 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1701 vq->last_used_idx += i;
1707 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1708 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1710 struct rte_crypto_op **tmp_ops = ops;
1711 uint16_t count = 0, left = nb_ops;
1716 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1718 if (unlikely(count == 0))
1721 tmp_ops = &tmp_ops[count];
1724 callfds[idx++] = callfd;
1726 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1727 VC_LOG_ERR("Too many vqs");
1734 return nb_ops - left;