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)
472 if (desc->flags & VRING_DESC_F_WRITE)
475 while (desc->flags & VRING_DESC_F_NEXT) {
476 desc = &head[desc->next];
477 if (desc->flags & VRING_DESC_F_WRITE)
484 static struct virtio_crypto_inhdr *
485 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc)
488 struct virtio_crypto_inhdr *inhdr;
490 while (desc->flags & VRING_DESC_F_NEXT)
491 desc = &vc_req->head[desc->next];
494 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
495 &dlen, VHOST_ACCESS_WO);
496 if (unlikely(!inhdr || dlen != desc->len))
502 static __rte_always_inline int
503 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
506 struct vring_desc *desc = *cur_desc;
509 rte_prefetch0(&head[desc->next]);
512 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
513 desc = &head[desc->next];
514 rte_prefetch0(&head[desc->next]);
518 if (unlikely(left > 0))
521 *cur_desc = &head[desc->next];
525 static __rte_always_inline void *
526 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc *cur_desc,
530 uint64_t dlen = cur_desc->len;
532 data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
533 if (unlikely(!data || dlen != cur_desc->len)) {
534 VC_LOG_ERR("Failed to map object");
542 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
543 struct vring_desc **cur_desc, uint32_t size)
545 struct vring_desc *desc = *cur_desc;
546 uint64_t remain, addr, dlen, len;
548 uint8_t *data = dst_data;
552 rte_prefetch0(&vc_req->head[desc->next]);
553 to_copy = RTE_MIN(desc->len, (uint32_t)left);
555 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
557 if (unlikely(!src || !dlen))
560 rte_memcpy((uint8_t *)data, src, dlen);
563 if (unlikely(dlen < to_copy)) {
564 remain = to_copy - dlen;
565 addr = desc->addr + dlen;
569 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
571 if (unlikely(!src || !len)) {
572 VC_LOG_ERR("Failed to map descriptor");
576 rte_memcpy(data, src, len);
585 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
586 desc = &vc_req->head[desc->next];
587 rte_prefetch0(&vc_req->head[desc->next]);
588 to_copy = RTE_MIN(desc->len, (uint32_t)left);
590 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
592 if (unlikely(!src || !dlen)) {
593 VC_LOG_ERR("Failed to map descriptor");
597 rte_memcpy(data, src, dlen);
600 if (unlikely(dlen < to_copy)) {
601 remain = to_copy - dlen;
602 addr = desc->addr + dlen;
606 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
608 if (unlikely(!src || !len)) {
609 VC_LOG_ERR("Failed to map descriptor");
613 rte_memcpy(data, src, len);
623 if (unlikely(left > 0)) {
624 VC_LOG_ERR("Incorrect virtio descriptor");
628 *cur_desc = &vc_req->head[desc->next];
634 write_back_data(struct vhost_crypto_data_req *vc_req)
636 struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
639 rte_prefetch0(wb_data->next);
640 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
642 wb_data = wb_data->next;
643 rte_mempool_put(vc_req->wb_pool, wb_last);
648 free_wb_data(struct vhost_crypto_writeback_data *wb_data,
649 struct rte_mempool *mp)
651 while (wb_data->next != NULL)
652 free_wb_data(wb_data->next, mp);
654 rte_mempool_put(mp, wb_data);
658 * The function will allocate a vhost_crypto_writeback_data linked list
659 * containing the source and destination data pointers for the write back
660 * operation after dequeued from Cryptodev PMD queues.
663 * The vhost crypto data request pointer
665 * The pointer of the current in use descriptor pointer. The content of
666 * cur_desc is expected to be updated after the function execution.
668 * The last write back data element to be returned. It is used only in cipher
669 * and hash chain operations.
671 * The source data pointer
673 * The offset to both source and destination data. For source data the offset
674 * is the number of bytes between src and start point of cipher operation. For
675 * destination data the offset is the number of bytes from *cur_desc->addr
676 * to the point where the src will be written to.
677 * @param write_back_len
678 * The size of the write back length.
680 * The pointer to the start of the write back data linked list.
682 static struct vhost_crypto_writeback_data *
683 prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
684 struct vring_desc **cur_desc,
685 struct vhost_crypto_writeback_data **end_wb_data,
688 uint64_t write_back_len)
690 struct vhost_crypto_writeback_data *wb_data, *head;
691 struct vring_desc *desc = *cur_desc;
696 ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
697 if (unlikely(ret < 0)) {
698 VC_LOG_ERR("no memory");
704 if (likely(desc->len > offset)) {
705 wb_data->src = src + offset;
707 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
708 &dlen, VHOST_ACCESS_RW) + offset;
709 if (unlikely(!dst || dlen != desc->len)) {
710 VC_LOG_ERR("Failed to map descriptor");
715 wb_data->len = desc->len - offset;
716 write_back_len -= wb_data->len;
717 src += offset + wb_data->len;
720 if (unlikely(write_back_len)) {
721 ret = rte_mempool_get(vc_req->wb_pool,
722 (void **)&(wb_data->next));
723 if (unlikely(ret < 0)) {
724 VC_LOG_ERR("no memory");
728 wb_data = wb_data->next;
730 wb_data->next = NULL;
734 while (write_back_len) {
735 desc = &vc_req->head[desc->next];
736 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
737 VC_LOG_ERR("incorrect descriptor");
741 if (desc->len <= offset) {
747 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
748 VHOST_ACCESS_RW) + offset;
749 if (unlikely(dst == NULL || dlen != desc->len)) {
750 VC_LOG_ERR("Failed to map descriptor");
756 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
757 write_back_len -= wb_data->len;
761 if (write_back_len) {
762 ret = rte_mempool_get(vc_req->wb_pool,
763 (void **)&(wb_data->next));
764 if (unlikely(ret < 0)) {
765 VC_LOG_ERR("no memory");
769 wb_data = wb_data->next;
771 wb_data->next = NULL;
774 *cur_desc = &vc_req->head[desc->next];
776 *end_wb_data = wb_data;
782 free_wb_data(head, vc_req->wb_pool);
788 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
789 struct vhost_crypto_data_req *vc_req,
790 struct virtio_crypto_cipher_data_req *cipher,
791 struct vring_desc *cur_desc)
793 struct vring_desc *desc = cur_desc;
794 struct vhost_crypto_writeback_data *ewb = NULL;
795 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
796 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
801 if (unlikely(copy_data(iv_data, vc_req, &desc,
802 cipher->para.iv_len) < 0)) {
803 ret = VIRTIO_CRYPTO_BADMSG;
807 m_src->data_len = cipher->para.src_data_len;
809 switch (vcrypto->option) {
810 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
811 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
812 cipher->para.src_data_len);
813 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
814 if (unlikely(m_src->buf_iova == 0 ||
815 m_src->buf_addr == NULL)) {
816 VC_LOG_ERR("zero_copy may fail due to cross page data");
817 ret = VIRTIO_CRYPTO_ERR;
821 if (unlikely(move_desc(vc_req->head, &desc,
822 cipher->para.src_data_len) < 0)) {
823 VC_LOG_ERR("Incorrect descriptor");
824 ret = VIRTIO_CRYPTO_ERR;
829 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
830 vc_req->wb_pool = vcrypto->wb_pool;
832 if (unlikely(cipher->para.src_data_len >
833 RTE_MBUF_DEFAULT_BUF_SIZE)) {
834 VC_LOG_ERR("Not enough space to do data copy");
835 ret = VIRTIO_CRYPTO_ERR;
838 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
839 vc_req, &desc, cipher->para.src_data_len)
841 ret = VIRTIO_CRYPTO_BADMSG;
846 ret = VIRTIO_CRYPTO_BADMSG;
851 desc = find_write_desc(vc_req->head, desc);
852 if (unlikely(!desc)) {
853 VC_LOG_ERR("Cannot find write location");
854 ret = VIRTIO_CRYPTO_BADMSG;
858 switch (vcrypto->option) {
859 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
860 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
861 desc->addr, cipher->para.dst_data_len);
862 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
863 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
864 VC_LOG_ERR("zero_copy may fail due to cross page data");
865 ret = VIRTIO_CRYPTO_ERR;
869 if (unlikely(move_desc(vc_req->head, &desc,
870 cipher->para.dst_data_len) < 0)) {
871 VC_LOG_ERR("Incorrect descriptor");
872 ret = VIRTIO_CRYPTO_ERR;
876 m_dst->data_len = cipher->para.dst_data_len;
878 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
879 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
880 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
881 cipher->para.dst_data_len);
882 if (unlikely(vc_req->wb == NULL)) {
883 ret = VIRTIO_CRYPTO_ERR;
889 ret = VIRTIO_CRYPTO_BADMSG;
894 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
895 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
897 op->sym->cipher.data.offset = 0;
898 op->sym->cipher.data.length = cipher->para.src_data_len;
900 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
901 if (unlikely(vc_req->inhdr == NULL)) {
902 ret = VIRTIO_CRYPTO_BADMSG;
906 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
907 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
913 free_wb_data(vc_req->wb, vc_req->wb_pool);
915 vc_req->len = INHDR_LEN;
920 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
921 struct vhost_crypto_data_req *vc_req,
922 struct virtio_crypto_alg_chain_data_req *chain,
923 struct vring_desc *cur_desc)
925 struct vring_desc *desc = cur_desc, *digest_desc;
926 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
927 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
928 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
929 uint32_t digest_offset;
935 if (unlikely(copy_data(iv_data, vc_req, &desc,
936 chain->para.iv_len) < 0)) {
937 ret = VIRTIO_CRYPTO_BADMSG;
941 m_src->data_len = chain->para.src_data_len;
943 switch (vcrypto->option) {
944 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
945 m_dst->data_len = chain->para.dst_data_len;
947 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
948 chain->para.src_data_len);
949 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
950 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
951 VC_LOG_ERR("zero_copy may fail due to cross page data");
952 ret = VIRTIO_CRYPTO_ERR;
956 if (unlikely(move_desc(vc_req->head, &desc,
957 chain->para.src_data_len) < 0)) {
958 VC_LOG_ERR("Incorrect descriptor");
959 ret = VIRTIO_CRYPTO_ERR;
963 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
964 vc_req->wb_pool = vcrypto->wb_pool;
966 if (unlikely(chain->para.src_data_len >
967 RTE_MBUF_DEFAULT_BUF_SIZE)) {
968 VC_LOG_ERR("Not enough space to do data copy");
969 ret = VIRTIO_CRYPTO_ERR;
972 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
973 vc_req, &desc, chain->para.src_data_len)) < 0) {
974 ret = VIRTIO_CRYPTO_BADMSG;
980 ret = VIRTIO_CRYPTO_BADMSG;
985 desc = find_write_desc(vc_req->head, desc);
986 if (unlikely(!desc)) {
987 VC_LOG_ERR("Cannot find write location");
988 ret = VIRTIO_CRYPTO_BADMSG;
992 switch (vcrypto->option) {
993 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
994 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
995 desc->addr, chain->para.dst_data_len);
996 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
997 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
998 VC_LOG_ERR("zero_copy may fail due to cross page data");
999 ret = VIRTIO_CRYPTO_ERR;
1003 if (unlikely(move_desc(vc_req->head, &desc,
1004 chain->para.dst_data_len) < 0)) {
1005 VC_LOG_ERR("Incorrect descriptor");
1006 ret = VIRTIO_CRYPTO_ERR;
1010 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1011 desc->addr, chain->para.hash_result_len);
1012 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1014 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1015 VC_LOG_ERR("zero_copy may fail due to cross page data");
1016 ret = VIRTIO_CRYPTO_ERR;
1020 if (unlikely(move_desc(vc_req->head, &desc,
1021 chain->para.hash_result_len) < 0)) {
1022 VC_LOG_ERR("Incorrect descriptor");
1023 ret = VIRTIO_CRYPTO_ERR;
1028 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1029 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1030 rte_pktmbuf_mtod(m_src, uint8_t *),
1031 chain->para.cipher_start_src_offset,
1032 chain->para.dst_data_len -
1033 chain->para.cipher_start_src_offset);
1034 if (unlikely(vc_req->wb == NULL)) {
1035 ret = VIRTIO_CRYPTO_ERR;
1039 digest_offset = m_src->data_len;
1040 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1044 /** create a wb_data for digest */
1045 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1046 digest_addr, 0, chain->para.hash_result_len);
1047 if (unlikely(ewb->next == NULL)) {
1048 ret = VIRTIO_CRYPTO_ERR;
1052 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1053 chain->para.hash_result_len)) < 0) {
1054 ret = VIRTIO_CRYPTO_BADMSG;
1058 op->sym->auth.digest.data = digest_addr;
1059 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1063 ret = VIRTIO_CRYPTO_BADMSG;
1068 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1069 if (unlikely(vc_req->inhdr == NULL)) {
1070 ret = VIRTIO_CRYPTO_BADMSG;
1074 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1076 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1077 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1079 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1080 op->sym->cipher.data.length = chain->para.src_data_len -
1081 chain->para.cipher_start_src_offset;
1083 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1084 op->sym->auth.data.length = chain->para.len_to_hash;
1086 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1092 free_wb_data(vc_req->wb, vc_req->wb_pool);
1093 vc_req->len = INHDR_LEN;
1098 * Process on descriptor
1100 static __rte_always_inline int
1101 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1102 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1103 struct vring_desc *head, uint16_t desc_idx)
1105 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1106 struct rte_cryptodev_sym_session *session;
1107 struct virtio_crypto_op_data_req *req, tmp_req;
1108 struct virtio_crypto_inhdr *inhdr;
1109 struct vring_desc *desc = NULL;
1110 uint64_t session_id;
1114 vc_req->desc_idx = desc_idx;
1115 vc_req->dev = vcrypto->dev;
1118 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1120 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1121 &dlen, VHOST_ACCESS_RO);
1122 if (unlikely(!desc || dlen != head->len))
1130 vc_req->head = head;
1131 vc_req->zero_copy = vcrypto->option;
1133 req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1134 if (unlikely(req == NULL)) {
1135 switch (vcrypto->option) {
1136 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1137 err = VIRTIO_CRYPTO_BADMSG;
1138 VC_LOG_ERR("Invalid descriptor");
1140 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1142 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req))
1144 err = VIRTIO_CRYPTO_BADMSG;
1145 VC_LOG_ERR("Invalid descriptor");
1150 err = VIRTIO_CRYPTO_ERR;
1151 VC_LOG_ERR("Invalid option");
1155 if (unlikely(move_desc(vc_req->head, &desc,
1156 sizeof(*req)) < 0)) {
1157 VC_LOG_ERR("Incorrect descriptor");
1162 switch (req->header.opcode) {
1163 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1164 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1165 session_id = req->header.session_id;
1167 /* one branch to avoid unnecessary table lookup */
1168 if (vcrypto->cache_session_id != session_id) {
1169 err = rte_hash_lookup_data(vcrypto->session_map,
1170 &session_id, (void **)&session);
1171 if (unlikely(err < 0)) {
1172 err = VIRTIO_CRYPTO_ERR;
1173 VC_LOG_ERR("Failed to find session %"PRIu64,
1178 vcrypto->cache_session = session;
1179 vcrypto->cache_session_id = session_id;
1182 session = vcrypto->cache_session;
1184 err = rte_crypto_op_attach_sym_session(op, session);
1185 if (unlikely(err < 0)) {
1186 err = VIRTIO_CRYPTO_ERR;
1187 VC_LOG_ERR("Failed to attach session to op");
1191 switch (req->u.sym_req.op_type) {
1192 case VIRTIO_CRYPTO_SYM_OP_NONE:
1193 err = VIRTIO_CRYPTO_NOTSUPP;
1195 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1196 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1197 &req->u.sym_req.u.cipher, desc);
1199 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1200 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1201 &req->u.sym_req.u.chain, desc);
1204 if (unlikely(err != 0)) {
1205 VC_LOG_ERR("Failed to process sym request");
1210 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1211 req->header.opcode);
1219 inhdr = reach_inhdr(vc_req, desc);
1220 if (likely(inhdr != NULL))
1221 inhdr->status = (uint8_t)err;
1226 static __rte_always_inline struct vhost_virtqueue *
1227 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1228 struct vhost_virtqueue *old_vq)
1230 struct rte_mbuf *m_src = op->sym->m_src;
1231 struct rte_mbuf *m_dst = op->sym->m_dst;
1232 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1235 if (unlikely(!vc_req)) {
1236 VC_LOG_ERR("Failed to retrieve vc_req");
1240 if (old_vq && (vc_req->vq != old_vq))
1243 desc_idx = vc_req->desc_idx;
1245 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1246 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1248 if (vc_req->zero_copy == 0)
1249 write_back_data(vc_req);
1252 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1253 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1255 rte_mempool_put(m_src->pool, (void *)m_src);
1258 rte_mempool_put(m_dst->pool, (void *)m_dst);
1263 static __rte_always_inline uint16_t
1264 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1265 uint16_t nb_ops, int *callfd)
1267 uint16_t processed = 1;
1268 struct vhost_virtqueue *vq, *tmp_vq;
1270 if (unlikely(nb_ops == 0))
1273 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1274 if (unlikely(vq == NULL))
1278 while ((processed < nb_ops)) {
1279 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1282 if (unlikely(vq != tmp_vq))
1288 *callfd = vq->callfd;
1290 *(volatile uint16_t *)&vq->used->idx += processed;
1295 int __rte_experimental
1296 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1297 struct rte_mempool *sess_pool,
1298 struct rte_mempool *sess_priv_pool,
1301 struct virtio_net *dev = get_device(vid);
1302 struct rte_hash_parameters params = {0};
1303 struct vhost_crypto *vcrypto;
1308 VC_LOG_ERR("Invalid vid %i", vid);
1312 ret = rte_vhost_driver_set_features(dev->ifname,
1313 VIRTIO_CRYPTO_FEATURES);
1315 VC_LOG_ERR("Error setting features");
1319 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1320 RTE_CACHE_LINE_SIZE, socket_id);
1322 VC_LOG_ERR("Insufficient memory");
1326 vcrypto->sess_pool = sess_pool;
1327 vcrypto->sess_priv_pool = sess_priv_pool;
1328 vcrypto->cid = cryptodev_id;
1329 vcrypto->cache_session_id = UINT64_MAX;
1330 vcrypto->last_session_id = 1;
1332 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1334 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1336 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1337 params.hash_func = rte_jhash;
1338 params.key_len = sizeof(uint64_t);
1339 params.socket_id = socket_id;
1340 vcrypto->session_map = rte_hash_create(¶ms);
1341 if (!vcrypto->session_map) {
1342 VC_LOG_ERR("Failed to creath session map");
1347 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1348 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1349 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1350 sizeof(struct vhost_crypto_data_req),
1351 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1353 if (!vcrypto->mbuf_pool) {
1354 VC_LOG_ERR("Failed to creath mbuf pool");
1359 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1360 vcrypto->wb_pool = rte_mempool_create(name,
1361 VHOST_CRYPTO_MBUF_POOL_SIZE,
1362 sizeof(struct vhost_crypto_writeback_data),
1363 128, 0, NULL, NULL, NULL, NULL,
1364 rte_socket_id(), 0);
1365 if (!vcrypto->wb_pool) {
1366 VC_LOG_ERR("Failed to creath mempool");
1371 dev->extern_data = vcrypto;
1372 dev->extern_ops.pre_msg_handle = NULL;
1373 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1378 if (vcrypto->session_map)
1379 rte_hash_free(vcrypto->session_map);
1380 if (vcrypto->mbuf_pool)
1381 rte_mempool_free(vcrypto->mbuf_pool);
1388 int __rte_experimental
1389 rte_vhost_crypto_free(int vid)
1391 struct virtio_net *dev = get_device(vid);
1392 struct vhost_crypto *vcrypto;
1394 if (unlikely(dev == NULL)) {
1395 VC_LOG_ERR("Invalid vid %i", vid);
1399 vcrypto = dev->extern_data;
1400 if (unlikely(vcrypto == NULL)) {
1401 VC_LOG_ERR("Cannot find required data, is it initialized?");
1405 rte_hash_free(vcrypto->session_map);
1406 rte_mempool_free(vcrypto->mbuf_pool);
1407 rte_mempool_free(vcrypto->wb_pool);
1410 dev->extern_data = NULL;
1411 dev->extern_ops.pre_msg_handle = NULL;
1412 dev->extern_ops.post_msg_handle = NULL;
1417 int __rte_experimental
1418 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1420 struct virtio_net *dev = get_device(vid);
1421 struct vhost_crypto *vcrypto;
1423 if (unlikely(dev == NULL)) {
1424 VC_LOG_ERR("Invalid vid %i", vid);
1428 if (unlikely((uint32_t)option >=
1429 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1430 VC_LOG_ERR("Invalid option %i", option);
1434 vcrypto = (struct vhost_crypto *)dev->extern_data;
1435 if (unlikely(vcrypto == NULL)) {
1436 VC_LOG_ERR("Cannot find required data, is it initialized?");
1440 if (vcrypto->option == (uint8_t)option)
1443 if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1444 !(rte_mempool_full(vcrypto->wb_pool))) {
1445 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1449 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1452 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1453 vcrypto->wb_pool = rte_mempool_create(name,
1454 VHOST_CRYPTO_MBUF_POOL_SIZE,
1455 sizeof(struct vhost_crypto_writeback_data),
1456 128, 0, NULL, NULL, NULL, NULL,
1457 rte_socket_id(), 0);
1458 if (!vcrypto->wb_pool) {
1459 VC_LOG_ERR("Failed to creath mbuf pool");
1463 rte_mempool_free(vcrypto->wb_pool);
1464 vcrypto->wb_pool = NULL;
1467 vcrypto->option = (uint8_t)option;
1472 uint16_t __rte_experimental
1473 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1474 struct rte_crypto_op **ops, uint16_t nb_ops)
1476 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1477 struct virtio_net *dev = get_device(vid);
1478 struct vhost_crypto *vcrypto;
1479 struct vhost_virtqueue *vq;
1485 if (unlikely(dev == NULL)) {
1486 VC_LOG_ERR("Invalid vid %i", vid);
1490 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1491 VC_LOG_ERR("Invalid qid %u", qid);
1495 vcrypto = (struct vhost_crypto *)dev->extern_data;
1496 if (unlikely(vcrypto == NULL)) {
1497 VC_LOG_ERR("Cannot find required data, is it initialized?");
1501 vq = dev->virtqueue[qid];
1503 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1504 start_idx = vq->last_used_idx;
1505 count = avail_idx - start_idx;
1506 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1507 count = RTE_MIN(count, nb_ops);
1509 if (unlikely(count == 0))
1512 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1513 * we need only 1 mbuf as src and dst
1515 switch (vcrypto->option) {
1516 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1517 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1518 (void **)mbufs, count * 2) < 0)) {
1519 VC_LOG_ERR("Insufficient memory");
1523 for (i = 0; i < count; i++) {
1524 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1525 uint16_t desc_idx = vq->avail->ring[used_idx];
1526 struct vring_desc *head = &vq->desc[desc_idx];
1527 struct rte_crypto_op *op = ops[i];
1529 op->sym->m_src = mbufs[i * 2];
1530 op->sym->m_dst = mbufs[i * 2 + 1];
1531 op->sym->m_src->data_off = 0;
1532 op->sym->m_dst->data_off = 0;
1534 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1535 op, head, desc_idx)) < 0)
1539 if (unlikely(i < count))
1540 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1541 (void **)&mbufs[i * 2],
1546 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1547 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1548 (void **)mbufs, count) < 0)) {
1549 VC_LOG_ERR("Insufficient memory");
1553 for (i = 0; i < count; i++) {
1554 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1555 uint16_t desc_idx = vq->avail->ring[used_idx];
1556 struct vring_desc *head = &vq->desc[desc_idx];
1557 struct rte_crypto_op *op = ops[i];
1559 op->sym->m_src = mbufs[i];
1560 op->sym->m_dst = NULL;
1561 op->sym->m_src->data_off = 0;
1563 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1564 op, head, desc_idx)) < 0)
1568 if (unlikely(i < count))
1569 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1577 vq->last_used_idx += i;
1582 uint16_t __rte_experimental
1583 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1584 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1586 struct rte_crypto_op **tmp_ops = ops;
1587 uint16_t count = 0, left = nb_ops;
1592 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1594 if (unlikely(count == 0))
1597 tmp_ops = &tmp_ops[count];
1600 callfds[idx++] = callfd;
1602 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1603 VC_LOG_ERR("Too many vqs");
1610 return nb_ops - left;