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 *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);
245 if (unlikely(ret < 0))
248 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
249 xform->cipher.algo = (enum rte_crypto_cipher_algorithm)ret;
250 xform->cipher.key.length = param->cipher_key_len;
251 if (xform->cipher.key.length > 0)
252 xform->cipher.key.data = param->cipher_key_buf;
253 if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
254 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
255 else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
256 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
258 VC_LOG_DBG("Bad operation type");
259 return -VIRTIO_CRYPTO_BADMSG;
262 ret = get_iv_len(xform->cipher.algo);
263 if (unlikely(ret < 0))
265 xform->cipher.iv.length = (uint16_t)ret;
266 xform->cipher.iv.offset = IV_OFFSET;
271 transform_chain_param(struct rte_crypto_sym_xform *xforms,
272 VhostUserCryptoSessionParam *param)
274 struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
277 switch (param->chaining_dir) {
278 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
280 xform_cipher = xforms->next;
281 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
282 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
284 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
285 xform_cipher = xforms;
286 xform_auth = xforms->next;
287 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
288 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
291 return -VIRTIO_CRYPTO_BADMSG;
295 ret = cipher_algo_transform(param->cipher_algo);
296 if (unlikely(ret < 0))
298 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
299 xform_cipher->cipher.algo = (enum rte_crypto_cipher_algorithm)ret;
300 xform_cipher->cipher.key.length = param->cipher_key_len;
301 xform_cipher->cipher.key.data = param->cipher_key_buf;
302 ret = get_iv_len(xform_cipher->cipher.algo);
303 if (unlikely(ret < 0))
305 xform_cipher->cipher.iv.length = (uint16_t)ret;
306 xform_cipher->cipher.iv.offset = IV_OFFSET;
309 xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
310 ret = auth_algo_transform(param->hash_algo);
311 if (unlikely(ret < 0))
313 xform_auth->auth.algo = (enum rte_crypto_auth_algorithm)ret;
314 xform_auth->auth.digest_length = param->digest_len;
315 xform_auth->auth.key.length = param->auth_key_len;
316 xform_auth->auth.key.data = param->auth_key_buf;
322 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
323 VhostUserCryptoSessionParam *sess_param)
325 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
326 struct rte_cryptodev_sym_session *session;
329 switch (sess_param->op_type) {
330 case VIRTIO_CRYPTO_SYM_OP_NONE:
331 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
332 ret = transform_cipher_param(&xform1, sess_param);
334 VC_LOG_ERR("Error transform session msg (%i)", ret);
335 sess_param->session_id = ret;
339 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
340 if (unlikely(sess_param->hash_mode !=
341 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
342 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
343 VC_LOG_ERR("Error transform session message (%i)",
344 -VIRTIO_CRYPTO_NOTSUPP);
348 xform1.next = &xform2;
350 ret = transform_chain_param(&xform1, sess_param);
352 VC_LOG_ERR("Error transform session message (%i)", ret);
353 sess_param->session_id = ret;
359 VC_LOG_ERR("Algorithm not yet supported");
360 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
364 session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
366 VC_LOG_ERR("Failed to create session");
367 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
371 if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
372 vcrypto->sess_pool) < 0) {
373 VC_LOG_ERR("Failed to initialize session");
374 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
378 /* insert hash to map */
379 if (rte_hash_add_key_data(vcrypto->session_map,
380 &vcrypto->last_session_id, session) < 0) {
381 VC_LOG_ERR("Failed to insert session to hash table");
383 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
384 VC_LOG_ERR("Failed to clear session");
386 if (rte_cryptodev_sym_session_free(session) < 0)
387 VC_LOG_ERR("Failed to free session");
389 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
393 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
394 vcrypto->last_session_id, vcrypto->dev->vid);
396 sess_param->session_id = vcrypto->last_session_id;
397 vcrypto->last_session_id++;
401 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
403 struct rte_cryptodev_sym_session *session;
404 uint64_t sess_id = session_id;
407 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
410 if (unlikely(ret < 0)) {
411 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
412 return -VIRTIO_CRYPTO_INVSESS;
415 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
416 VC_LOG_DBG("Failed to clear session");
417 return -VIRTIO_CRYPTO_ERR;
420 if (rte_cryptodev_sym_session_free(session) < 0) {
421 VC_LOG_DBG("Failed to free session");
422 return -VIRTIO_CRYPTO_ERR;
425 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
426 VC_LOG_DBG("Failed to delete session from hash table.");
427 return -VIRTIO_CRYPTO_ERR;
430 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
436 static enum vh_result
437 vhost_crypto_msg_post_handler(int vid, void *msg)
439 struct virtio_net *dev = get_device(vid);
440 struct vhost_crypto *vcrypto;
441 VhostUserMsg *vmsg = msg;
442 enum vh_result ret = VH_RESULT_OK;
445 VC_LOG_ERR("Invalid vid %i", vid);
446 return VH_RESULT_ERR;
449 vcrypto = dev->extern_data;
450 if (vcrypto == NULL) {
451 VC_LOG_ERR("Cannot find required data, is it initialized?");
452 return VH_RESULT_ERR;
455 if (vmsg->request.master == VHOST_USER_CRYPTO_CREATE_SESS) {
456 vhost_crypto_create_sess(vcrypto,
457 &vmsg->payload.crypto_session);
459 ret = VH_RESULT_REPLY;
460 } else if (vmsg->request.master == VHOST_USER_CRYPTO_CLOSE_SESS) {
461 if (vhost_crypto_close_sess(vcrypto, vmsg->payload.u64))
468 static __rte_always_inline struct vring_desc *
469 find_write_desc(struct vring_desc *head, struct vring_desc *desc)
471 if (desc->flags & VRING_DESC_F_WRITE)
474 while (desc->flags & VRING_DESC_F_NEXT) {
475 desc = &head[desc->next];
476 if (desc->flags & VRING_DESC_F_WRITE)
483 static struct virtio_crypto_inhdr *
484 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc)
487 struct virtio_crypto_inhdr *inhdr;
489 while (desc->flags & VRING_DESC_F_NEXT)
490 desc = &vc_req->head[desc->next];
493 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
494 &dlen, VHOST_ACCESS_WO);
495 if (unlikely(!inhdr || dlen != desc->len))
501 static __rte_always_inline int
502 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
505 struct vring_desc *desc = *cur_desc;
508 rte_prefetch0(&head[desc->next]);
511 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
512 desc = &head[desc->next];
513 rte_prefetch0(&head[desc->next]);
517 if (unlikely(left > 0))
520 *cur_desc = &head[desc->next];
524 static __rte_always_inline void *
525 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc *cur_desc,
529 uint64_t dlen = cur_desc->len;
531 data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
532 if (unlikely(!data || dlen != cur_desc->len)) {
533 VC_LOG_ERR("Failed to map object");
541 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
542 struct vring_desc **cur_desc, uint32_t size)
544 struct vring_desc *desc = *cur_desc;
545 uint64_t remain, addr, dlen, len;
547 uint8_t *data = dst_data;
551 rte_prefetch0(&vc_req->head[desc->next]);
552 to_copy = RTE_MIN(desc->len, (uint32_t)left);
554 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
556 if (unlikely(!src || !dlen))
559 rte_memcpy((uint8_t *)data, src, dlen);
562 if (unlikely(dlen < to_copy)) {
563 remain = to_copy - dlen;
564 addr = desc->addr + dlen;
568 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
570 if (unlikely(!src || !len)) {
571 VC_LOG_ERR("Failed to map descriptor");
575 rte_memcpy(data, src, len);
584 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
585 desc = &vc_req->head[desc->next];
586 rte_prefetch0(&vc_req->head[desc->next]);
587 to_copy = RTE_MIN(desc->len, (uint32_t)left);
589 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
591 if (unlikely(!src || !dlen)) {
592 VC_LOG_ERR("Failed to map descriptor");
596 rte_memcpy(data, src, dlen);
599 if (unlikely(dlen < to_copy)) {
600 remain = to_copy - dlen;
601 addr = desc->addr + dlen;
605 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
607 if (unlikely(!src || !len)) {
608 VC_LOG_ERR("Failed to map descriptor");
612 rte_memcpy(data, src, len);
622 if (unlikely(left > 0)) {
623 VC_LOG_ERR("Incorrect virtio descriptor");
627 *cur_desc = &vc_req->head[desc->next];
633 write_back_data(struct vhost_crypto_data_req *vc_req)
635 struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
638 rte_prefetch0(wb_data->next);
639 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
641 wb_data = wb_data->next;
642 rte_mempool_put(vc_req->wb_pool, wb_last);
647 free_wb_data(struct vhost_crypto_writeback_data *wb_data,
648 struct rte_mempool *mp)
650 while (wb_data->next != NULL)
651 free_wb_data(wb_data->next, mp);
653 rte_mempool_put(mp, wb_data);
657 * The function will allocate a vhost_crypto_writeback_data linked list
658 * containing the source and destination data pointers for the write back
659 * operation after dequeued from Cryptodev PMD queues.
662 * The vhost crypto data request pointer
664 * The pointer of the current in use descriptor pointer. The content of
665 * cur_desc is expected to be updated after the function execution.
667 * The last write back data element to be returned. It is used only in cipher
668 * and hash chain operations.
670 * The source data pointer
672 * The offset to both source and destination data. For source data the offset
673 * is the number of bytes between src and start point of cipher operation. For
674 * destination data the offset is the number of bytes from *cur_desc->addr
675 * to the point where the src will be written to.
676 * @param write_back_len
677 * The size of the write back length.
679 * The pointer to the start of the write back data linked list.
681 static struct vhost_crypto_writeback_data *
682 prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
683 struct vring_desc **cur_desc,
684 struct vhost_crypto_writeback_data **end_wb_data,
687 uint64_t write_back_len)
689 struct vhost_crypto_writeback_data *wb_data, *head;
690 struct vring_desc *desc = *cur_desc;
695 ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
696 if (unlikely(ret < 0)) {
697 VC_LOG_ERR("no memory");
703 if (likely(desc->len > offset)) {
704 wb_data->src = src + offset;
706 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
707 &dlen, VHOST_ACCESS_RW) + offset;
708 if (unlikely(!dst || dlen != desc->len)) {
709 VC_LOG_ERR("Failed to map descriptor");
714 wb_data->len = desc->len - offset;
715 write_back_len -= wb_data->len;
716 src += offset + wb_data->len;
719 if (unlikely(write_back_len)) {
720 ret = rte_mempool_get(vc_req->wb_pool,
721 (void **)&(wb_data->next));
722 if (unlikely(ret < 0)) {
723 VC_LOG_ERR("no memory");
727 wb_data = wb_data->next;
729 wb_data->next = NULL;
733 while (write_back_len) {
734 desc = &vc_req->head[desc->next];
735 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
736 VC_LOG_ERR("incorrect descriptor");
740 if (desc->len <= offset) {
746 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
747 VHOST_ACCESS_RW) + offset;
748 if (unlikely(dst == NULL || dlen != desc->len)) {
749 VC_LOG_ERR("Failed to map descriptor");
755 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
756 write_back_len -= wb_data->len;
760 if (write_back_len) {
761 ret = rte_mempool_get(vc_req->wb_pool,
762 (void **)&(wb_data->next));
763 if (unlikely(ret < 0)) {
764 VC_LOG_ERR("no memory");
768 wb_data = wb_data->next;
770 wb_data->next = NULL;
773 *cur_desc = &vc_req->head[desc->next];
775 *end_wb_data = wb_data;
781 free_wb_data(head, vc_req->wb_pool);
787 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
788 struct vhost_crypto_data_req *vc_req,
789 struct virtio_crypto_cipher_data_req *cipher,
790 struct vring_desc *cur_desc)
792 struct vring_desc *desc = cur_desc;
793 struct vhost_crypto_writeback_data *ewb = NULL;
794 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
795 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
800 if (unlikely(copy_data(iv_data, vc_req, &desc,
801 cipher->para.iv_len) < 0)) {
802 ret = VIRTIO_CRYPTO_BADMSG;
806 m_src->data_len = cipher->para.src_data_len;
808 switch (vcrypto->option) {
809 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
810 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
811 cipher->para.src_data_len);
812 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
813 if (unlikely(m_src->buf_iova == 0 ||
814 m_src->buf_addr == NULL)) {
815 VC_LOG_ERR("zero_copy may fail due to cross page data");
816 ret = VIRTIO_CRYPTO_ERR;
820 if (unlikely(move_desc(vc_req->head, &desc,
821 cipher->para.src_data_len) < 0)) {
822 VC_LOG_ERR("Incorrect descriptor");
823 ret = VIRTIO_CRYPTO_ERR;
828 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
829 vc_req->wb_pool = vcrypto->wb_pool;
831 if (unlikely(cipher->para.src_data_len >
832 RTE_MBUF_DEFAULT_BUF_SIZE)) {
833 VC_LOG_ERR("Not enough space to do data copy");
834 ret = VIRTIO_CRYPTO_ERR;
837 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
838 vc_req, &desc, cipher->para.src_data_len)
840 ret = VIRTIO_CRYPTO_BADMSG;
845 ret = VIRTIO_CRYPTO_BADMSG;
850 desc = find_write_desc(vc_req->head, desc);
851 if (unlikely(!desc)) {
852 VC_LOG_ERR("Cannot find write location");
853 ret = VIRTIO_CRYPTO_BADMSG;
857 switch (vcrypto->option) {
858 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
859 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
860 desc->addr, cipher->para.dst_data_len);
861 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
862 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
863 VC_LOG_ERR("zero_copy may fail due to cross page data");
864 ret = VIRTIO_CRYPTO_ERR;
868 if (unlikely(move_desc(vc_req->head, &desc,
869 cipher->para.dst_data_len) < 0)) {
870 VC_LOG_ERR("Incorrect descriptor");
871 ret = VIRTIO_CRYPTO_ERR;
875 m_dst->data_len = cipher->para.dst_data_len;
877 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
878 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
879 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
880 cipher->para.dst_data_len);
881 if (unlikely(vc_req->wb == NULL)) {
882 ret = VIRTIO_CRYPTO_ERR;
888 ret = VIRTIO_CRYPTO_BADMSG;
893 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
894 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
896 op->sym->cipher.data.offset = 0;
897 op->sym->cipher.data.length = cipher->para.src_data_len;
899 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
900 if (unlikely(vc_req->inhdr == NULL)) {
901 ret = VIRTIO_CRYPTO_BADMSG;
905 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
906 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
912 free_wb_data(vc_req->wb, vc_req->wb_pool);
914 vc_req->len = INHDR_LEN;
919 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
920 struct vhost_crypto_data_req *vc_req,
921 struct virtio_crypto_alg_chain_data_req *chain,
922 struct vring_desc *cur_desc)
924 struct vring_desc *desc = cur_desc, *digest_desc;
925 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
926 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
927 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
928 uint32_t digest_offset;
934 if (unlikely(copy_data(iv_data, vc_req, &desc,
935 chain->para.iv_len) < 0)) {
936 ret = VIRTIO_CRYPTO_BADMSG;
940 m_src->data_len = chain->para.src_data_len;
942 switch (vcrypto->option) {
943 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
944 m_dst->data_len = chain->para.dst_data_len;
946 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
947 chain->para.src_data_len);
948 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
949 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
950 VC_LOG_ERR("zero_copy may fail due to cross page data");
951 ret = VIRTIO_CRYPTO_ERR;
955 if (unlikely(move_desc(vc_req->head, &desc,
956 chain->para.src_data_len) < 0)) {
957 VC_LOG_ERR("Incorrect descriptor");
958 ret = VIRTIO_CRYPTO_ERR;
962 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
963 vc_req->wb_pool = vcrypto->wb_pool;
965 if (unlikely(chain->para.src_data_len >
966 RTE_MBUF_DEFAULT_BUF_SIZE)) {
967 VC_LOG_ERR("Not enough space to do data copy");
968 ret = VIRTIO_CRYPTO_ERR;
971 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
972 vc_req, &desc, chain->para.src_data_len)) < 0) {
973 ret = VIRTIO_CRYPTO_BADMSG;
979 ret = VIRTIO_CRYPTO_BADMSG;
984 desc = find_write_desc(vc_req->head, desc);
985 if (unlikely(!desc)) {
986 VC_LOG_ERR("Cannot find write location");
987 ret = VIRTIO_CRYPTO_BADMSG;
991 switch (vcrypto->option) {
992 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
993 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
994 desc->addr, chain->para.dst_data_len);
995 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
996 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
997 VC_LOG_ERR("zero_copy may fail due to cross page data");
998 ret = VIRTIO_CRYPTO_ERR;
1002 if (unlikely(move_desc(vc_req->head, &desc,
1003 chain->para.dst_data_len) < 0)) {
1004 VC_LOG_ERR("Incorrect descriptor");
1005 ret = VIRTIO_CRYPTO_ERR;
1009 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1010 desc->addr, chain->para.hash_result_len);
1011 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1013 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1014 VC_LOG_ERR("zero_copy may fail due to cross page data");
1015 ret = VIRTIO_CRYPTO_ERR;
1019 if (unlikely(move_desc(vc_req->head, &desc,
1020 chain->para.hash_result_len) < 0)) {
1021 VC_LOG_ERR("Incorrect descriptor");
1022 ret = VIRTIO_CRYPTO_ERR;
1027 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1028 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1029 rte_pktmbuf_mtod(m_src, uint8_t *),
1030 chain->para.cipher_start_src_offset,
1031 chain->para.dst_data_len -
1032 chain->para.cipher_start_src_offset);
1033 if (unlikely(vc_req->wb == NULL)) {
1034 ret = VIRTIO_CRYPTO_ERR;
1038 digest_offset = m_src->data_len;
1039 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1043 /** create a wb_data for digest */
1044 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1045 digest_addr, 0, chain->para.hash_result_len);
1046 if (unlikely(ewb->next == NULL)) {
1047 ret = VIRTIO_CRYPTO_ERR;
1051 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1052 chain->para.hash_result_len)) < 0) {
1053 ret = VIRTIO_CRYPTO_BADMSG;
1057 op->sym->auth.digest.data = digest_addr;
1058 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1062 ret = VIRTIO_CRYPTO_BADMSG;
1067 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1068 if (unlikely(vc_req->inhdr == NULL)) {
1069 ret = VIRTIO_CRYPTO_BADMSG;
1073 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1075 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1076 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1078 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1079 op->sym->cipher.data.length = chain->para.src_data_len -
1080 chain->para.cipher_start_src_offset;
1082 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1083 op->sym->auth.data.length = chain->para.len_to_hash;
1085 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1091 free_wb_data(vc_req->wb, vc_req->wb_pool);
1092 vc_req->len = INHDR_LEN;
1097 * Process on descriptor
1099 static __rte_always_inline int
1100 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1101 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1102 struct vring_desc *head, uint16_t desc_idx)
1104 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1105 struct rte_cryptodev_sym_session *session;
1106 struct virtio_crypto_op_data_req *req, tmp_req;
1107 struct virtio_crypto_inhdr *inhdr;
1108 struct vring_desc *desc = NULL;
1109 uint64_t session_id;
1113 vc_req->desc_idx = desc_idx;
1114 vc_req->dev = vcrypto->dev;
1117 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1119 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1120 &dlen, VHOST_ACCESS_RO);
1121 if (unlikely(!desc || dlen != head->len))
1129 vc_req->head = head;
1130 vc_req->zero_copy = vcrypto->option;
1132 req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1133 if (unlikely(req == NULL)) {
1134 switch (vcrypto->option) {
1135 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1136 err = VIRTIO_CRYPTO_BADMSG;
1137 VC_LOG_ERR("Invalid descriptor");
1139 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1141 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req))
1143 err = VIRTIO_CRYPTO_BADMSG;
1144 VC_LOG_ERR("Invalid descriptor");
1149 err = VIRTIO_CRYPTO_ERR;
1150 VC_LOG_ERR("Invalid option");
1154 if (unlikely(move_desc(vc_req->head, &desc,
1155 sizeof(*req)) < 0)) {
1156 VC_LOG_ERR("Incorrect descriptor");
1161 switch (req->header.opcode) {
1162 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1163 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1164 session_id = req->header.session_id;
1166 /* one branch to avoid unnecessary table lookup */
1167 if (vcrypto->cache_session_id != session_id) {
1168 err = rte_hash_lookup_data(vcrypto->session_map,
1169 &session_id, (void **)&session);
1170 if (unlikely(err < 0)) {
1171 err = VIRTIO_CRYPTO_ERR;
1172 VC_LOG_ERR("Failed to find session %"PRIu64,
1177 vcrypto->cache_session = session;
1178 vcrypto->cache_session_id = session_id;
1181 session = vcrypto->cache_session;
1183 err = rte_crypto_op_attach_sym_session(op, session);
1184 if (unlikely(err < 0)) {
1185 err = VIRTIO_CRYPTO_ERR;
1186 VC_LOG_ERR("Failed to attach session to op");
1190 switch (req->u.sym_req.op_type) {
1191 case VIRTIO_CRYPTO_SYM_OP_NONE:
1192 err = VIRTIO_CRYPTO_NOTSUPP;
1194 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1195 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1196 &req->u.sym_req.u.cipher, desc);
1198 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1199 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1200 &req->u.sym_req.u.chain, desc);
1203 if (unlikely(err != 0)) {
1204 VC_LOG_ERR("Failed to process sym request");
1209 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1210 req->header.opcode);
1218 inhdr = reach_inhdr(vc_req, desc);
1219 if (likely(inhdr != NULL))
1220 inhdr->status = (uint8_t)err;
1225 static __rte_always_inline struct vhost_virtqueue *
1226 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1227 struct vhost_virtqueue *old_vq)
1229 struct rte_mbuf *m_src = op->sym->m_src;
1230 struct rte_mbuf *m_dst = op->sym->m_dst;
1231 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1234 if (unlikely(!vc_req)) {
1235 VC_LOG_ERR("Failed to retrieve vc_req");
1239 if (old_vq && (vc_req->vq != old_vq))
1242 desc_idx = vc_req->desc_idx;
1244 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1245 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1247 if (vc_req->zero_copy == 0)
1248 write_back_data(vc_req);
1251 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1252 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1254 rte_mempool_put(m_src->pool, (void *)m_src);
1257 rte_mempool_put(m_dst->pool, (void *)m_dst);
1262 static __rte_always_inline uint16_t
1263 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1264 uint16_t nb_ops, int *callfd)
1266 uint16_t processed = 1;
1267 struct vhost_virtqueue *vq, *tmp_vq;
1269 if (unlikely(nb_ops == 0))
1272 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1273 if (unlikely(vq == NULL))
1277 while ((processed < nb_ops)) {
1278 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1281 if (unlikely(vq != tmp_vq))
1287 *callfd = vq->callfd;
1289 *(volatile uint16_t *)&vq->used->idx += processed;
1294 int __rte_experimental
1295 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1296 struct rte_mempool *sess_pool, int socket_id)
1298 struct virtio_net *dev = get_device(vid);
1299 struct rte_hash_parameters params = {0};
1300 struct vhost_crypto *vcrypto;
1305 VC_LOG_ERR("Invalid vid %i", vid);
1309 ret = rte_vhost_driver_set_features(dev->ifname,
1310 VIRTIO_CRYPTO_FEATURES);
1312 VC_LOG_ERR("Error setting features");
1316 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1317 RTE_CACHE_LINE_SIZE, socket_id);
1319 VC_LOG_ERR("Insufficient memory");
1323 vcrypto->sess_pool = sess_pool;
1324 vcrypto->cid = cryptodev_id;
1325 vcrypto->cache_session_id = UINT64_MAX;
1326 vcrypto->last_session_id = 1;
1328 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1330 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1332 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1333 params.hash_func = rte_jhash;
1334 params.key_len = sizeof(uint64_t);
1335 params.socket_id = socket_id;
1336 vcrypto->session_map = rte_hash_create(¶ms);
1337 if (!vcrypto->session_map) {
1338 VC_LOG_ERR("Failed to creath session map");
1343 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1344 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1345 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1346 sizeof(struct vhost_crypto_data_req),
1347 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1349 if (!vcrypto->mbuf_pool) {
1350 VC_LOG_ERR("Failed to creath mbuf pool");
1355 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1356 vcrypto->wb_pool = rte_mempool_create(name,
1357 VHOST_CRYPTO_MBUF_POOL_SIZE,
1358 sizeof(struct vhost_crypto_writeback_data),
1359 128, 0, NULL, NULL, NULL, NULL,
1360 rte_socket_id(), 0);
1361 if (!vcrypto->wb_pool) {
1362 VC_LOG_ERR("Failed to creath mempool");
1367 dev->extern_data = vcrypto;
1368 dev->extern_ops.pre_msg_handle = NULL;
1369 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1374 if (vcrypto->session_map)
1375 rte_hash_free(vcrypto->session_map);
1376 if (vcrypto->mbuf_pool)
1377 rte_mempool_free(vcrypto->mbuf_pool);
1384 int __rte_experimental
1385 rte_vhost_crypto_free(int vid)
1387 struct virtio_net *dev = get_device(vid);
1388 struct vhost_crypto *vcrypto;
1390 if (unlikely(dev == NULL)) {
1391 VC_LOG_ERR("Invalid vid %i", vid);
1395 vcrypto = dev->extern_data;
1396 if (unlikely(vcrypto == NULL)) {
1397 VC_LOG_ERR("Cannot find required data, is it initialized?");
1401 rte_hash_free(vcrypto->session_map);
1402 rte_mempool_free(vcrypto->mbuf_pool);
1403 rte_mempool_free(vcrypto->wb_pool);
1406 dev->extern_data = NULL;
1407 dev->extern_ops.pre_msg_handle = NULL;
1408 dev->extern_ops.post_msg_handle = NULL;
1413 int __rte_experimental
1414 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1416 struct virtio_net *dev = get_device(vid);
1417 struct vhost_crypto *vcrypto;
1419 if (unlikely(dev == NULL)) {
1420 VC_LOG_ERR("Invalid vid %i", vid);
1424 if (unlikely((uint32_t)option >=
1425 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1426 VC_LOG_ERR("Invalid option %i", option);
1430 vcrypto = (struct vhost_crypto *)dev->extern_data;
1431 if (unlikely(vcrypto == NULL)) {
1432 VC_LOG_ERR("Cannot find required data, is it initialized?");
1436 if (vcrypto->option == (uint8_t)option)
1439 if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1440 !(rte_mempool_full(vcrypto->wb_pool))) {
1441 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1445 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1448 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1449 vcrypto->wb_pool = rte_mempool_create(name,
1450 VHOST_CRYPTO_MBUF_POOL_SIZE,
1451 sizeof(struct vhost_crypto_writeback_data),
1452 128, 0, NULL, NULL, NULL, NULL,
1453 rte_socket_id(), 0);
1454 if (!vcrypto->wb_pool) {
1455 VC_LOG_ERR("Failed to creath mbuf pool");
1459 rte_mempool_free(vcrypto->wb_pool);
1460 vcrypto->wb_pool = NULL;
1463 vcrypto->option = (uint8_t)option;
1468 uint16_t __rte_experimental
1469 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1470 struct rte_crypto_op **ops, uint16_t nb_ops)
1472 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1473 struct virtio_net *dev = get_device(vid);
1474 struct vhost_crypto *vcrypto;
1475 struct vhost_virtqueue *vq;
1481 if (unlikely(dev == NULL)) {
1482 VC_LOG_ERR("Invalid vid %i", vid);
1486 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1487 VC_LOG_ERR("Invalid qid %u", qid);
1491 vcrypto = (struct vhost_crypto *)dev->extern_data;
1492 if (unlikely(vcrypto == NULL)) {
1493 VC_LOG_ERR("Cannot find required data, is it initialized?");
1497 vq = dev->virtqueue[qid];
1499 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1500 start_idx = vq->last_used_idx;
1501 count = avail_idx - start_idx;
1502 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1503 count = RTE_MIN(count, nb_ops);
1505 if (unlikely(count == 0))
1508 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1509 * we need only 1 mbuf as src and dst
1511 switch (vcrypto->option) {
1512 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1513 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1514 (void **)mbufs, count * 2) < 0)) {
1515 VC_LOG_ERR("Insufficient memory");
1519 for (i = 0; i < count; i++) {
1520 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1521 uint16_t desc_idx = vq->avail->ring[used_idx];
1522 struct vring_desc *head = &vq->desc[desc_idx];
1523 struct rte_crypto_op *op = ops[i];
1525 op->sym->m_src = mbufs[i * 2];
1526 op->sym->m_dst = mbufs[i * 2 + 1];
1527 op->sym->m_src->data_off = 0;
1528 op->sym->m_dst->data_off = 0;
1530 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1531 op, head, desc_idx)) < 0)
1535 if (unlikely(i < count))
1536 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1537 (void **)&mbufs[i * 2],
1542 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1543 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1544 (void **)mbufs, count) < 0)) {
1545 VC_LOG_ERR("Insufficient memory");
1549 for (i = 0; i < count; i++) {
1550 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1551 uint16_t desc_idx = vq->avail->ring[used_idx];
1552 struct vring_desc *head = &vq->desc[desc_idx];
1553 struct rte_crypto_op *op = ops[i];
1555 op->sym->m_src = mbufs[i];
1556 op->sym->m_dst = NULL;
1557 op->sym->m_src->data_off = 0;
1559 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1560 op, head, desc_idx)) < 0)
1564 if (unlikely(i < count))
1565 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1573 vq->last_used_idx += i;
1578 uint16_t __rte_experimental
1579 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1580 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1582 struct rte_crypto_op **tmp_ops = ops;
1583 uint16_t count = 0, left = nb_ops;
1588 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1590 if (unlikely(count == 0))
1593 tmp_ops = &tmp_ops[count];
1596 callfds[idx++] = callfd;
1598 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1599 VC_LOG_ERR("Too many vqs");
1606 return nb_ops - left;