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 ((1 << VIRTIO_F_NOTIFY_ON_EMPTY) | \
39 (1 << VIRTIO_RING_F_INDIRECT_DESC) | \
40 (1 << VIRTIO_RING_F_EVENT_IDX) | \
41 (1 << VIRTIO_CRYPTO_SERVICE_CIPHER) | \
42 (1 << VIRTIO_CRYPTO_SERVICE_MAC) | \
43 (1 << VIRTIO_NET_F_CTRL_VQ))
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 cipher_algo_transform(uint32_t virtio_cipher_algo)
53 switch (virtio_cipher_algo) {
54 case VIRTIO_CRYPTO_CIPHER_AES_CBC:
55 ret = RTE_CRYPTO_CIPHER_AES_CBC;
57 case VIRTIO_CRYPTO_CIPHER_AES_CTR:
58 ret = RTE_CRYPTO_CIPHER_AES_CTR;
60 case VIRTIO_CRYPTO_CIPHER_DES_ECB:
61 ret = -VIRTIO_CRYPTO_NOTSUPP;
63 case VIRTIO_CRYPTO_CIPHER_DES_CBC:
64 ret = RTE_CRYPTO_CIPHER_DES_CBC;
66 case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
67 ret = RTE_CRYPTO_CIPHER_3DES_ECB;
69 case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
70 ret = RTE_CRYPTO_CIPHER_3DES_CBC;
72 case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
73 ret = RTE_CRYPTO_CIPHER_3DES_CTR;
75 case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
76 ret = RTE_CRYPTO_CIPHER_KASUMI_F8;
78 case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
79 ret = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
81 case VIRTIO_CRYPTO_CIPHER_AES_F8:
82 ret = RTE_CRYPTO_CIPHER_AES_F8;
84 case VIRTIO_CRYPTO_CIPHER_AES_XTS:
85 ret = RTE_CRYPTO_CIPHER_AES_XTS;
87 case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
88 ret = RTE_CRYPTO_CIPHER_ZUC_EEA3;
91 ret = -VIRTIO_CRYPTO_BADMSG;
99 auth_algo_transform(uint32_t virtio_auth_algo)
103 switch (virtio_auth_algo) {
105 case VIRTIO_CRYPTO_NO_MAC:
106 ret = RTE_CRYPTO_AUTH_NULL;
108 case VIRTIO_CRYPTO_MAC_HMAC_MD5:
109 ret = RTE_CRYPTO_AUTH_MD5_HMAC;
111 case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
112 ret = RTE_CRYPTO_AUTH_SHA1_HMAC;
114 case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
115 ret = RTE_CRYPTO_AUTH_SHA224_HMAC;
117 case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
118 ret = RTE_CRYPTO_AUTH_SHA256_HMAC;
120 case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
121 ret = RTE_CRYPTO_AUTH_SHA384_HMAC;
123 case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
124 ret = RTE_CRYPTO_AUTH_SHA512_HMAC;
126 case VIRTIO_CRYPTO_MAC_CMAC_3DES:
127 ret = -VIRTIO_CRYPTO_NOTSUPP;
129 case VIRTIO_CRYPTO_MAC_CMAC_AES:
130 ret = RTE_CRYPTO_AUTH_AES_CMAC;
132 case VIRTIO_CRYPTO_MAC_KASUMI_F9:
133 ret = RTE_CRYPTO_AUTH_KASUMI_F9;
135 case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
136 ret = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
138 case VIRTIO_CRYPTO_MAC_GMAC_AES:
139 ret = RTE_CRYPTO_AUTH_AES_GMAC;
141 case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
142 ret = -VIRTIO_CRYPTO_NOTSUPP;
144 case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
145 ret = RTE_CRYPTO_AUTH_AES_CBC_MAC;
147 case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
148 ret = -VIRTIO_CRYPTO_NOTSUPP;
150 case VIRTIO_CRYPTO_MAC_XCBC_AES:
151 ret = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
154 ret = -VIRTIO_CRYPTO_BADMSG;
161 static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
166 case RTE_CRYPTO_CIPHER_3DES_CBC:
169 case RTE_CRYPTO_CIPHER_3DES_CTR:
172 case RTE_CRYPTO_CIPHER_3DES_ECB:
175 case RTE_CRYPTO_CIPHER_AES_CBC:
179 /* TODO: add common algos */
190 * vhost_crypto struct is used to maintain a number of virtio_cryptos and
191 * one DPDK crypto device that deals with all crypto workloads. It is declared
192 * here and defined in vhost_crypto.c
194 struct vhost_crypto {
195 /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
198 struct rte_hash *session_map;
199 struct rte_mempool *mbuf_pool;
200 struct rte_mempool *sess_pool;
201 struct rte_mempool *sess_priv_pool;
202 struct rte_mempool *wb_pool;
204 /** DPDK cryptodev ID */
208 uint64_t last_session_id;
210 uint64_t cache_session_id;
211 struct rte_cryptodev_sym_session *cache_session;
212 /** socket id for the device */
215 struct virtio_net *dev;
218 } __rte_cache_aligned;
220 struct vhost_crypto_writeback_data {
224 struct vhost_crypto_writeback_data *next;
227 struct vhost_crypto_data_req {
228 struct vring_desc *head;
229 struct virtio_net *dev;
230 struct virtio_crypto_inhdr *inhdr;
231 struct vhost_virtqueue *vq;
232 struct vhost_crypto_writeback_data *wb;
233 struct rte_mempool *wb_pool;
240 transform_cipher_param(struct rte_crypto_sym_xform *xform,
241 VhostUserCryptoSessionParam *param)
245 ret = cipher_algo_transform(param->cipher_algo);
246 if (unlikely(ret < 0))
249 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
250 xform->cipher.algo = (enum rte_crypto_cipher_algorithm)ret;
251 xform->cipher.key.length = param->cipher_key_len;
252 if (xform->cipher.key.length > 0)
253 xform->cipher.key.data = param->cipher_key_buf;
254 if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
255 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
256 else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
257 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
259 VC_LOG_DBG("Bad operation type");
260 return -VIRTIO_CRYPTO_BADMSG;
263 ret = get_iv_len(xform->cipher.algo);
264 if (unlikely(ret < 0))
266 xform->cipher.iv.length = (uint16_t)ret;
267 xform->cipher.iv.offset = IV_OFFSET;
272 transform_chain_param(struct rte_crypto_sym_xform *xforms,
273 VhostUserCryptoSessionParam *param)
275 struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
278 switch (param->chaining_dir) {
279 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
281 xform_cipher = xforms->next;
282 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
283 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
285 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
286 xform_cipher = xforms;
287 xform_auth = xforms->next;
288 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
289 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
292 return -VIRTIO_CRYPTO_BADMSG;
296 ret = cipher_algo_transform(param->cipher_algo);
297 if (unlikely(ret < 0))
299 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
300 xform_cipher->cipher.algo = (enum rte_crypto_cipher_algorithm)ret;
301 xform_cipher->cipher.key.length = param->cipher_key_len;
302 xform_cipher->cipher.key.data = param->cipher_key_buf;
303 ret = get_iv_len(xform_cipher->cipher.algo);
304 if (unlikely(ret < 0))
306 xform_cipher->cipher.iv.length = (uint16_t)ret;
307 xform_cipher->cipher.iv.offset = IV_OFFSET;
310 xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
311 ret = auth_algo_transform(param->hash_algo);
312 if (unlikely(ret < 0))
314 xform_auth->auth.algo = (enum rte_crypto_auth_algorithm)ret;
315 xform_auth->auth.digest_length = param->digest_len;
316 xform_auth->auth.key.length = param->auth_key_len;
317 xform_auth->auth.key.data = param->auth_key_buf;
323 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
324 VhostUserCryptoSessionParam *sess_param)
326 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
327 struct rte_cryptodev_sym_session *session;
330 switch (sess_param->op_type) {
331 case VIRTIO_CRYPTO_SYM_OP_NONE:
332 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
333 ret = transform_cipher_param(&xform1, sess_param);
335 VC_LOG_ERR("Error transform session msg (%i)", ret);
336 sess_param->session_id = ret;
340 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
341 if (unlikely(sess_param->hash_mode !=
342 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
343 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
344 VC_LOG_ERR("Error transform session message (%i)",
345 -VIRTIO_CRYPTO_NOTSUPP);
349 xform1.next = &xform2;
351 ret = transform_chain_param(&xform1, sess_param);
353 VC_LOG_ERR("Error transform session message (%i)", ret);
354 sess_param->session_id = ret;
360 VC_LOG_ERR("Algorithm not yet supported");
361 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
365 session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
367 VC_LOG_ERR("Failed to create session");
368 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
372 if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
373 vcrypto->sess_priv_pool) < 0) {
374 VC_LOG_ERR("Failed to initialize session");
375 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
379 /* insert hash to map */
380 if (rte_hash_add_key_data(vcrypto->session_map,
381 &vcrypto->last_session_id, session) < 0) {
382 VC_LOG_ERR("Failed to insert session to hash table");
384 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
385 VC_LOG_ERR("Failed to clear session");
387 if (rte_cryptodev_sym_session_free(session) < 0)
388 VC_LOG_ERR("Failed to free session");
390 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
394 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
395 vcrypto->last_session_id, vcrypto->dev->vid);
397 sess_param->session_id = vcrypto->last_session_id;
398 vcrypto->last_session_id++;
402 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
404 struct rte_cryptodev_sym_session *session;
405 uint64_t sess_id = session_id;
408 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
411 if (unlikely(ret < 0)) {
412 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
413 return -VIRTIO_CRYPTO_INVSESS;
416 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
417 VC_LOG_DBG("Failed to clear session");
418 return -VIRTIO_CRYPTO_ERR;
421 if (rte_cryptodev_sym_session_free(session) < 0) {
422 VC_LOG_DBG("Failed to free session");
423 return -VIRTIO_CRYPTO_ERR;
426 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
427 VC_LOG_DBG("Failed to delete session from hash table.");
428 return -VIRTIO_CRYPTO_ERR;
431 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
437 static enum vh_result
438 vhost_crypto_msg_post_handler(int vid, void *msg)
440 struct virtio_net *dev = get_device(vid);
441 struct vhost_crypto *vcrypto;
442 VhostUserMsg *vmsg = msg;
443 enum vh_result ret = VH_RESULT_OK;
446 VC_LOG_ERR("Invalid vid %i", vid);
447 return VH_RESULT_ERR;
450 vcrypto = dev->extern_data;
451 if (vcrypto == NULL) {
452 VC_LOG_ERR("Cannot find required data, is it initialized?");
453 return VH_RESULT_ERR;
456 if (vmsg->request.master == VHOST_USER_CRYPTO_CREATE_SESS) {
457 vhost_crypto_create_sess(vcrypto,
458 &vmsg->payload.crypto_session);
460 ret = VH_RESULT_REPLY;
461 } else if (vmsg->request.master == VHOST_USER_CRYPTO_CLOSE_SESS) {
462 if (vhost_crypto_close_sess(vcrypto, vmsg->payload.u64))
469 static __rte_always_inline struct vring_desc *
470 find_write_desc(struct vring_desc *head, struct vring_desc *desc,
473 if (desc->flags & VRING_DESC_F_WRITE)
476 while (desc->flags & VRING_DESC_F_NEXT) {
477 if (unlikely(*nb_descs == 0))
481 desc = &head[desc->next];
482 if (desc->flags & VRING_DESC_F_WRITE)
489 static struct virtio_crypto_inhdr *
490 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc,
494 struct virtio_crypto_inhdr *inhdr;
496 while (desc->flags & VRING_DESC_F_NEXT) {
497 if (unlikely(*nb_descs == 0))
500 desc = &vc_req->head[desc->next];
504 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
505 &dlen, VHOST_ACCESS_WO);
506 if (unlikely(!inhdr || dlen != desc->len))
512 static __rte_always_inline int
513 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
514 uint32_t size, uint32_t *nb_descs)
516 struct vring_desc *desc = *cur_desc;
517 int left = size - desc->len;
519 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
521 if (unlikely(*nb_descs == 0))
524 desc = &head[desc->next];
525 rte_prefetch0(&head[desc->next]);
529 if (unlikely(left > 0))
532 if (unlikely(*nb_descs == 0))
535 *cur_desc = &head[desc->next];
539 static __rte_always_inline void *
540 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc *cur_desc,
544 uint64_t dlen = cur_desc->len;
546 data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
547 if (unlikely(!data || dlen != cur_desc->len)) {
548 VC_LOG_ERR("Failed to map object");
556 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
557 struct vring_desc **cur_desc, uint32_t size, uint32_t *nb_descs)
559 struct vring_desc *desc = *cur_desc;
560 uint64_t remain, addr, dlen, len;
562 uint8_t *data = dst_data;
566 to_copy = RTE_MIN(desc->len, (uint32_t)left);
568 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
570 if (unlikely(!src || !dlen))
573 rte_memcpy((uint8_t *)data, src, dlen);
576 if (unlikely(dlen < to_copy)) {
577 remain = to_copy - dlen;
578 addr = desc->addr + dlen;
582 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
584 if (unlikely(!src || !len)) {
585 VC_LOG_ERR("Failed to map descriptor");
589 rte_memcpy(data, src, len);
598 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
599 if (unlikely(*nb_descs == 0)) {
600 VC_LOG_ERR("Invalid descriptors");
605 desc = &vc_req->head[desc->next];
606 rte_prefetch0(&vc_req->head[desc->next]);
607 to_copy = RTE_MIN(desc->len, (uint32_t)left);
609 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
611 if (unlikely(!src || !dlen)) {
612 VC_LOG_ERR("Failed to map descriptor");
616 rte_memcpy(data, src, dlen);
619 if (unlikely(dlen < to_copy)) {
620 remain = to_copy - dlen;
621 addr = desc->addr + dlen;
625 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
627 if (unlikely(!src || !len)) {
628 VC_LOG_ERR("Failed to map descriptor");
632 rte_memcpy(data, src, len);
642 if (unlikely(left > 0)) {
643 VC_LOG_ERR("Incorrect virtio descriptor");
647 if (unlikely(*nb_descs == 0))
650 *cur_desc = &vc_req->head[desc->next];
656 write_back_data(struct vhost_crypto_data_req *vc_req)
658 struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
661 rte_prefetch0(wb_data->next);
662 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
664 wb_data = wb_data->next;
665 rte_mempool_put(vc_req->wb_pool, wb_last);
670 free_wb_data(struct vhost_crypto_writeback_data *wb_data,
671 struct rte_mempool *mp)
673 while (wb_data->next != NULL)
674 free_wb_data(wb_data->next, mp);
676 rte_mempool_put(mp, wb_data);
680 * The function will allocate a vhost_crypto_writeback_data linked list
681 * containing the source and destination data pointers for the write back
682 * operation after dequeued from Cryptodev PMD queues.
685 * The vhost crypto data request pointer
687 * The pointer of the current in use descriptor pointer. The content of
688 * cur_desc is expected to be updated after the function execution.
690 * The last write back data element to be returned. It is used only in cipher
691 * and hash chain operations.
693 * The source data pointer
695 * The offset to both source and destination data. For source data the offset
696 * is the number of bytes between src and start point of cipher operation. For
697 * destination data the offset is the number of bytes from *cur_desc->addr
698 * to the point where the src will be written to.
699 * @param write_back_len
700 * The size of the write back length.
702 * The pointer to the start of the write back data linked list.
704 static struct vhost_crypto_writeback_data *
705 prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
706 struct vring_desc **cur_desc,
707 struct vhost_crypto_writeback_data **end_wb_data,
710 uint64_t write_back_len,
713 struct vhost_crypto_writeback_data *wb_data, *head;
714 struct vring_desc *desc = *cur_desc;
719 ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
720 if (unlikely(ret < 0)) {
721 VC_LOG_ERR("no memory");
727 if (likely(desc->len > offset)) {
728 wb_data->src = src + offset;
730 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
731 &dlen, VHOST_ACCESS_RW) + offset;
732 if (unlikely(!dst || dlen != desc->len)) {
733 VC_LOG_ERR("Failed to map descriptor");
738 wb_data->len = desc->len - offset;
739 write_back_len -= wb_data->len;
740 src += offset + wb_data->len;
743 if (unlikely(write_back_len)) {
744 ret = rte_mempool_get(vc_req->wb_pool,
745 (void **)&(wb_data->next));
746 if (unlikely(ret < 0)) {
747 VC_LOG_ERR("no memory");
751 wb_data = wb_data->next;
753 wb_data->next = NULL;
757 while (write_back_len) {
758 if (unlikely(*nb_descs == 0)) {
759 VC_LOG_ERR("Invalid descriptors");
764 desc = &vc_req->head[desc->next];
765 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
766 VC_LOG_ERR("incorrect descriptor");
770 if (desc->len <= offset) {
776 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
777 VHOST_ACCESS_RW) + offset;
778 if (unlikely(dst == NULL || dlen != desc->len)) {
779 VC_LOG_ERR("Failed to map descriptor");
785 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
786 write_back_len -= wb_data->len;
790 if (write_back_len) {
791 ret = rte_mempool_get(vc_req->wb_pool,
792 (void **)&(wb_data->next));
793 if (unlikely(ret < 0)) {
794 VC_LOG_ERR("no memory");
798 wb_data = wb_data->next;
800 wb_data->next = NULL;
803 if (unlikely(*nb_descs == 0))
806 *cur_desc = &vc_req->head[desc->next];
808 *end_wb_data = wb_data;
814 free_wb_data(head, vc_req->wb_pool);
820 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
821 struct vhost_crypto_data_req *vc_req,
822 struct virtio_crypto_cipher_data_req *cipher,
823 struct vring_desc *cur_desc,
826 struct vring_desc *desc = cur_desc;
827 struct vhost_crypto_writeback_data *ewb = NULL;
828 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
829 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
834 if (unlikely(copy_data(iv_data, vc_req, &desc, cipher->para.iv_len,
836 ret = VIRTIO_CRYPTO_BADMSG;
840 m_src->data_len = cipher->para.src_data_len;
842 switch (vcrypto->option) {
843 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
844 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
845 cipher->para.src_data_len);
846 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
847 if (unlikely(m_src->buf_iova == 0 ||
848 m_src->buf_addr == NULL)) {
849 VC_LOG_ERR("zero_copy may fail due to cross page data");
850 ret = VIRTIO_CRYPTO_ERR;
854 if (unlikely(move_desc(vc_req->head, &desc,
855 cipher->para.src_data_len, nb_descs) < 0)) {
856 VC_LOG_ERR("Incorrect descriptor");
857 ret = VIRTIO_CRYPTO_ERR;
862 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
863 vc_req->wb_pool = vcrypto->wb_pool;
865 if (unlikely(cipher->para.src_data_len >
866 RTE_MBUF_DEFAULT_BUF_SIZE)) {
867 VC_LOG_ERR("Not enough space to do data copy");
868 ret = VIRTIO_CRYPTO_ERR;
871 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
872 vc_req, &desc, cipher->para.src_data_len,
874 ret = VIRTIO_CRYPTO_BADMSG;
879 ret = VIRTIO_CRYPTO_BADMSG;
884 desc = find_write_desc(vc_req->head, desc, nb_descs);
885 if (unlikely(!desc)) {
886 VC_LOG_ERR("Cannot find write location");
887 ret = VIRTIO_CRYPTO_BADMSG;
891 switch (vcrypto->option) {
892 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
893 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
894 desc->addr, cipher->para.dst_data_len);
895 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
896 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
897 VC_LOG_ERR("zero_copy may fail due to cross page data");
898 ret = VIRTIO_CRYPTO_ERR;
902 if (unlikely(move_desc(vc_req->head, &desc,
903 cipher->para.dst_data_len, nb_descs) < 0)) {
904 VC_LOG_ERR("Incorrect descriptor");
905 ret = VIRTIO_CRYPTO_ERR;
909 m_dst->data_len = cipher->para.dst_data_len;
911 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
912 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
913 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
914 cipher->para.dst_data_len, nb_descs);
915 if (unlikely(vc_req->wb == NULL)) {
916 ret = VIRTIO_CRYPTO_ERR;
922 ret = VIRTIO_CRYPTO_BADMSG;
927 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
928 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
930 op->sym->cipher.data.offset = 0;
931 op->sym->cipher.data.length = cipher->para.src_data_len;
933 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
934 if (unlikely(vc_req->inhdr == NULL)) {
935 ret = VIRTIO_CRYPTO_BADMSG;
939 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
940 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
946 free_wb_data(vc_req->wb, vc_req->wb_pool);
948 vc_req->len = INHDR_LEN;
953 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
954 struct vhost_crypto_data_req *vc_req,
955 struct virtio_crypto_alg_chain_data_req *chain,
956 struct vring_desc *cur_desc,
959 struct vring_desc *desc = cur_desc, *digest_desc;
960 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
961 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
962 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
963 uint32_t digest_offset;
969 if (unlikely(copy_data(iv_data, vc_req, &desc,
970 chain->para.iv_len, nb_descs) < 0)) {
971 ret = VIRTIO_CRYPTO_BADMSG;
975 m_src->data_len = chain->para.src_data_len;
977 switch (vcrypto->option) {
978 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
979 m_dst->data_len = chain->para.dst_data_len;
981 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
982 chain->para.src_data_len);
983 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
984 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
985 VC_LOG_ERR("zero_copy may fail due to cross page data");
986 ret = VIRTIO_CRYPTO_ERR;
990 if (unlikely(move_desc(vc_req->head, &desc,
991 chain->para.src_data_len, nb_descs) < 0)) {
992 VC_LOG_ERR("Incorrect descriptor");
993 ret = VIRTIO_CRYPTO_ERR;
997 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
998 vc_req->wb_pool = vcrypto->wb_pool;
1000 if (unlikely(chain->para.src_data_len >
1001 RTE_MBUF_DEFAULT_BUF_SIZE)) {
1002 VC_LOG_ERR("Not enough space to do data copy");
1003 ret = VIRTIO_CRYPTO_ERR;
1006 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1007 vc_req, &desc, chain->para.src_data_len,
1009 ret = VIRTIO_CRYPTO_BADMSG;
1015 ret = VIRTIO_CRYPTO_BADMSG;
1020 desc = find_write_desc(vc_req->head, desc, nb_descs);
1021 if (unlikely(!desc)) {
1022 VC_LOG_ERR("Cannot find write location");
1023 ret = VIRTIO_CRYPTO_BADMSG;
1027 switch (vcrypto->option) {
1028 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1029 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
1030 desc->addr, chain->para.dst_data_len);
1031 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1032 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
1033 VC_LOG_ERR("zero_copy may fail due to cross page data");
1034 ret = VIRTIO_CRYPTO_ERR;
1038 if (unlikely(move_desc(vc_req->head, &desc,
1039 chain->para.dst_data_len, nb_descs) < 0)) {
1040 VC_LOG_ERR("Incorrect descriptor");
1041 ret = VIRTIO_CRYPTO_ERR;
1045 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1046 desc->addr, chain->para.hash_result_len);
1047 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1049 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1050 VC_LOG_ERR("zero_copy may fail due to cross page data");
1051 ret = VIRTIO_CRYPTO_ERR;
1055 if (unlikely(move_desc(vc_req->head, &desc,
1056 chain->para.hash_result_len, nb_descs) < 0)) {
1057 VC_LOG_ERR("Incorrect descriptor");
1058 ret = VIRTIO_CRYPTO_ERR;
1063 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1064 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1065 rte_pktmbuf_mtod(m_src, uint8_t *),
1066 chain->para.cipher_start_src_offset,
1067 chain->para.dst_data_len -
1068 chain->para.cipher_start_src_offset, nb_descs);
1069 if (unlikely(vc_req->wb == NULL)) {
1070 ret = VIRTIO_CRYPTO_ERR;
1074 digest_offset = m_src->data_len;
1075 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1079 /** create a wb_data for digest */
1080 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1081 digest_addr, 0, chain->para.hash_result_len,
1083 if (unlikely(ewb->next == NULL)) {
1084 ret = VIRTIO_CRYPTO_ERR;
1088 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1089 chain->para.hash_result_len, nb_descs)) < 0) {
1090 ret = VIRTIO_CRYPTO_BADMSG;
1094 op->sym->auth.digest.data = digest_addr;
1095 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1099 ret = VIRTIO_CRYPTO_BADMSG;
1104 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1105 if (unlikely(vc_req->inhdr == NULL)) {
1106 ret = VIRTIO_CRYPTO_BADMSG;
1110 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1112 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1113 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1115 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1116 op->sym->cipher.data.length = chain->para.src_data_len -
1117 chain->para.cipher_start_src_offset;
1119 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1120 op->sym->auth.data.length = chain->para.len_to_hash;
1122 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1128 free_wb_data(vc_req->wb, vc_req->wb_pool);
1129 vc_req->len = INHDR_LEN;
1134 * Process on descriptor
1136 static __rte_always_inline int
1137 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1138 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1139 struct vring_desc *head, uint16_t desc_idx)
1141 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1142 struct rte_cryptodev_sym_session *session;
1143 struct virtio_crypto_op_data_req *req, tmp_req;
1144 struct virtio_crypto_inhdr *inhdr;
1145 struct vring_desc *desc = NULL;
1146 uint64_t session_id;
1148 uint32_t nb_descs = vq->size;
1151 vc_req->desc_idx = desc_idx;
1152 vc_req->dev = vcrypto->dev;
1155 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1157 nb_descs = dlen / sizeof(struct vring_desc);
1158 /* drop invalid descriptors */
1159 if (unlikely(nb_descs > vq->size))
1161 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1162 &dlen, VHOST_ACCESS_RO);
1163 if (unlikely(!desc || dlen != head->len))
1171 vc_req->head = head;
1172 vc_req->zero_copy = vcrypto->option;
1174 req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1175 if (unlikely(req == NULL)) {
1176 switch (vcrypto->option) {
1177 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1178 err = VIRTIO_CRYPTO_BADMSG;
1179 VC_LOG_ERR("Invalid descriptor");
1181 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1183 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req),
1185 err = VIRTIO_CRYPTO_BADMSG;
1186 VC_LOG_ERR("Invalid descriptor");
1191 err = VIRTIO_CRYPTO_ERR;
1192 VC_LOG_ERR("Invalid option");
1196 if (unlikely(move_desc(vc_req->head, &desc,
1197 sizeof(*req), &nb_descs) < 0)) {
1198 VC_LOG_ERR("Incorrect descriptor");
1203 switch (req->header.opcode) {
1204 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1205 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1206 session_id = req->header.session_id;
1208 /* one branch to avoid unnecessary table lookup */
1209 if (vcrypto->cache_session_id != session_id) {
1210 err = rte_hash_lookup_data(vcrypto->session_map,
1211 &session_id, (void **)&session);
1212 if (unlikely(err < 0)) {
1213 err = VIRTIO_CRYPTO_ERR;
1214 VC_LOG_ERR("Failed to find session %"PRIu64,
1219 vcrypto->cache_session = session;
1220 vcrypto->cache_session_id = session_id;
1223 session = vcrypto->cache_session;
1225 err = rte_crypto_op_attach_sym_session(op, session);
1226 if (unlikely(err < 0)) {
1227 err = VIRTIO_CRYPTO_ERR;
1228 VC_LOG_ERR("Failed to attach session to op");
1232 switch (req->u.sym_req.op_type) {
1233 case VIRTIO_CRYPTO_SYM_OP_NONE:
1234 err = VIRTIO_CRYPTO_NOTSUPP;
1236 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1237 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1238 &req->u.sym_req.u.cipher, desc,
1241 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1242 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1243 &req->u.sym_req.u.chain, desc,
1247 if (unlikely(err != 0)) {
1248 VC_LOG_ERR("Failed to process sym request");
1253 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1254 req->header.opcode);
1262 inhdr = reach_inhdr(vc_req, desc, &nb_descs);
1263 if (likely(inhdr != NULL))
1264 inhdr->status = (uint8_t)err;
1269 static __rte_always_inline struct vhost_virtqueue *
1270 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1271 struct vhost_virtqueue *old_vq)
1273 struct rte_mbuf *m_src = op->sym->m_src;
1274 struct rte_mbuf *m_dst = op->sym->m_dst;
1275 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1278 if (unlikely(!vc_req)) {
1279 VC_LOG_ERR("Failed to retrieve vc_req");
1283 if (old_vq && (vc_req->vq != old_vq))
1286 desc_idx = vc_req->desc_idx;
1288 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1289 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1291 if (vc_req->zero_copy == 0)
1292 write_back_data(vc_req);
1295 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1296 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1298 rte_mempool_put(m_src->pool, (void *)m_src);
1301 rte_mempool_put(m_dst->pool, (void *)m_dst);
1306 static __rte_always_inline uint16_t
1307 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1308 uint16_t nb_ops, int *callfd)
1310 uint16_t processed = 1;
1311 struct vhost_virtqueue *vq, *tmp_vq;
1313 if (unlikely(nb_ops == 0))
1316 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1317 if (unlikely(vq == NULL))
1321 while ((processed < nb_ops)) {
1322 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1325 if (unlikely(vq != tmp_vq))
1331 *callfd = vq->callfd;
1333 *(volatile uint16_t *)&vq->used->idx += processed;
1338 int __rte_experimental
1339 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1340 struct rte_mempool *sess_pool,
1341 struct rte_mempool *sess_priv_pool,
1344 struct virtio_net *dev = get_device(vid);
1345 struct rte_hash_parameters params = {0};
1346 struct vhost_crypto *vcrypto;
1351 VC_LOG_ERR("Invalid vid %i", vid);
1355 ret = rte_vhost_driver_set_features(dev->ifname,
1356 VIRTIO_CRYPTO_FEATURES);
1358 VC_LOG_ERR("Error setting features");
1362 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1363 RTE_CACHE_LINE_SIZE, socket_id);
1365 VC_LOG_ERR("Insufficient memory");
1369 vcrypto->sess_pool = sess_pool;
1370 vcrypto->sess_priv_pool = sess_priv_pool;
1371 vcrypto->cid = cryptodev_id;
1372 vcrypto->cache_session_id = UINT64_MAX;
1373 vcrypto->last_session_id = 1;
1375 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1377 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1379 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1380 params.hash_func = rte_jhash;
1381 params.key_len = sizeof(uint64_t);
1382 params.socket_id = socket_id;
1383 vcrypto->session_map = rte_hash_create(¶ms);
1384 if (!vcrypto->session_map) {
1385 VC_LOG_ERR("Failed to creath session map");
1390 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1391 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1392 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1393 sizeof(struct vhost_crypto_data_req),
1394 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1396 if (!vcrypto->mbuf_pool) {
1397 VC_LOG_ERR("Failed to creath mbuf pool");
1402 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1403 vcrypto->wb_pool = rte_mempool_create(name,
1404 VHOST_CRYPTO_MBUF_POOL_SIZE,
1405 sizeof(struct vhost_crypto_writeback_data),
1406 128, 0, NULL, NULL, NULL, NULL,
1407 rte_socket_id(), 0);
1408 if (!vcrypto->wb_pool) {
1409 VC_LOG_ERR("Failed to creath mempool");
1414 dev->extern_data = vcrypto;
1415 dev->extern_ops.pre_msg_handle = NULL;
1416 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1421 if (vcrypto->session_map)
1422 rte_hash_free(vcrypto->session_map);
1423 if (vcrypto->mbuf_pool)
1424 rte_mempool_free(vcrypto->mbuf_pool);
1431 int __rte_experimental
1432 rte_vhost_crypto_free(int vid)
1434 struct virtio_net *dev = get_device(vid);
1435 struct vhost_crypto *vcrypto;
1437 if (unlikely(dev == NULL)) {
1438 VC_LOG_ERR("Invalid vid %i", vid);
1442 vcrypto = dev->extern_data;
1443 if (unlikely(vcrypto == NULL)) {
1444 VC_LOG_ERR("Cannot find required data, is it initialized?");
1448 rte_hash_free(vcrypto->session_map);
1449 rte_mempool_free(vcrypto->mbuf_pool);
1450 rte_mempool_free(vcrypto->wb_pool);
1453 dev->extern_data = NULL;
1454 dev->extern_ops.pre_msg_handle = NULL;
1455 dev->extern_ops.post_msg_handle = NULL;
1460 int __rte_experimental
1461 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1463 struct virtio_net *dev = get_device(vid);
1464 struct vhost_crypto *vcrypto;
1466 if (unlikely(dev == NULL)) {
1467 VC_LOG_ERR("Invalid vid %i", vid);
1471 if (unlikely((uint32_t)option >=
1472 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1473 VC_LOG_ERR("Invalid option %i", option);
1477 vcrypto = (struct vhost_crypto *)dev->extern_data;
1478 if (unlikely(vcrypto == NULL)) {
1479 VC_LOG_ERR("Cannot find required data, is it initialized?");
1483 if (vcrypto->option == (uint8_t)option)
1486 if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1487 !(rte_mempool_full(vcrypto->wb_pool))) {
1488 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1492 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1495 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1496 vcrypto->wb_pool = rte_mempool_create(name,
1497 VHOST_CRYPTO_MBUF_POOL_SIZE,
1498 sizeof(struct vhost_crypto_writeback_data),
1499 128, 0, NULL, NULL, NULL, NULL,
1500 rte_socket_id(), 0);
1501 if (!vcrypto->wb_pool) {
1502 VC_LOG_ERR("Failed to creath mbuf pool");
1506 rte_mempool_free(vcrypto->wb_pool);
1507 vcrypto->wb_pool = NULL;
1510 vcrypto->option = (uint8_t)option;
1515 uint16_t __rte_experimental
1516 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1517 struct rte_crypto_op **ops, uint16_t nb_ops)
1519 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1520 struct virtio_net *dev = get_device(vid);
1521 struct vhost_crypto *vcrypto;
1522 struct vhost_virtqueue *vq;
1528 if (unlikely(dev == NULL)) {
1529 VC_LOG_ERR("Invalid vid %i", vid);
1533 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1534 VC_LOG_ERR("Invalid qid %u", qid);
1538 vcrypto = (struct vhost_crypto *)dev->extern_data;
1539 if (unlikely(vcrypto == NULL)) {
1540 VC_LOG_ERR("Cannot find required data, is it initialized?");
1544 vq = dev->virtqueue[qid];
1546 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1547 start_idx = vq->last_used_idx;
1548 count = avail_idx - start_idx;
1549 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1550 count = RTE_MIN(count, nb_ops);
1552 if (unlikely(count == 0))
1555 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1556 * we need only 1 mbuf as src and dst
1558 switch (vcrypto->option) {
1559 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1560 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1561 (void **)mbufs, count * 2) < 0)) {
1562 VC_LOG_ERR("Insufficient memory");
1566 for (i = 0; i < count; i++) {
1567 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1568 uint16_t desc_idx = vq->avail->ring[used_idx];
1569 struct vring_desc *head = &vq->desc[desc_idx];
1570 struct rte_crypto_op *op = ops[i];
1572 op->sym->m_src = mbufs[i * 2];
1573 op->sym->m_dst = mbufs[i * 2 + 1];
1574 op->sym->m_src->data_off = 0;
1575 op->sym->m_dst->data_off = 0;
1577 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1578 op, head, desc_idx)) < 0)
1582 if (unlikely(i < count))
1583 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1584 (void **)&mbufs[i * 2],
1589 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1590 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1591 (void **)mbufs, count) < 0)) {
1592 VC_LOG_ERR("Insufficient memory");
1596 for (i = 0; i < count; i++) {
1597 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1598 uint16_t desc_idx = vq->avail->ring[used_idx];
1599 struct vring_desc *head = &vq->desc[desc_idx];
1600 struct rte_crypto_op *op = ops[i];
1602 op->sym->m_src = mbufs[i];
1603 op->sym->m_dst = NULL;
1604 op->sym->m_src->data_off = 0;
1606 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1607 op, head, desc_idx)) < 0)
1611 if (unlikely(i < count))
1612 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1620 vq->last_used_idx += i;
1625 uint16_t __rte_experimental
1626 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1627 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1629 struct rte_crypto_op **tmp_ops = ops;
1630 uint16_t count = 0, left = nb_ops;
1635 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1637 if (unlikely(count == 0))
1640 tmp_ops = &tmp_ops[count];
1643 callfds[idx++] = callfd;
1645 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1646 VC_LOG_ERR("Too many vqs");
1653 return nb_ops - left;