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) | \
44 (1 << VHOST_USER_PROTOCOL_F_CONFIG))
46 #define IOVA_TO_VVA(t, r, a, l, p) \
47 ((t)(uintptr_t)vhost_iova_to_vva(r->dev, r->vq, a, l, p))
50 cipher_algo_transform(uint32_t virtio_cipher_algo,
51 enum rte_crypto_cipher_algorithm *algo)
53 switch (virtio_cipher_algo) {
54 case VIRTIO_CRYPTO_CIPHER_AES_CBC:
55 *algo = RTE_CRYPTO_CIPHER_AES_CBC;
57 case VIRTIO_CRYPTO_CIPHER_AES_CTR:
58 *algo = RTE_CRYPTO_CIPHER_AES_CTR;
60 case VIRTIO_CRYPTO_CIPHER_DES_ECB:
61 *algo = -VIRTIO_CRYPTO_NOTSUPP;
63 case VIRTIO_CRYPTO_CIPHER_DES_CBC:
64 *algo = RTE_CRYPTO_CIPHER_DES_CBC;
66 case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
67 *algo = RTE_CRYPTO_CIPHER_3DES_ECB;
69 case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
70 *algo = RTE_CRYPTO_CIPHER_3DES_CBC;
72 case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
73 *algo = RTE_CRYPTO_CIPHER_3DES_CTR;
75 case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
76 *algo = RTE_CRYPTO_CIPHER_KASUMI_F8;
78 case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
79 *algo = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
81 case VIRTIO_CRYPTO_CIPHER_AES_F8:
82 *algo = RTE_CRYPTO_CIPHER_AES_F8;
84 case VIRTIO_CRYPTO_CIPHER_AES_XTS:
85 *algo = RTE_CRYPTO_CIPHER_AES_XTS;
87 case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
88 *algo = RTE_CRYPTO_CIPHER_ZUC_EEA3;
91 return -VIRTIO_CRYPTO_BADMSG;
99 auth_algo_transform(uint32_t virtio_auth_algo,
100 enum rte_crypto_auth_algorithm *algo)
102 switch (virtio_auth_algo) {
103 case VIRTIO_CRYPTO_NO_MAC:
104 *algo = RTE_CRYPTO_AUTH_NULL;
106 case VIRTIO_CRYPTO_MAC_HMAC_MD5:
107 *algo = RTE_CRYPTO_AUTH_MD5_HMAC;
109 case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
110 *algo = RTE_CRYPTO_AUTH_SHA1_HMAC;
112 case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
113 *algo = RTE_CRYPTO_AUTH_SHA224_HMAC;
115 case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
116 *algo = RTE_CRYPTO_AUTH_SHA256_HMAC;
118 case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
119 *algo = RTE_CRYPTO_AUTH_SHA384_HMAC;
121 case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
122 *algo = RTE_CRYPTO_AUTH_SHA512_HMAC;
124 case VIRTIO_CRYPTO_MAC_CMAC_AES:
125 *algo = RTE_CRYPTO_AUTH_AES_CMAC;
127 case VIRTIO_CRYPTO_MAC_KASUMI_F9:
128 *algo = RTE_CRYPTO_AUTH_KASUMI_F9;
130 case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
131 *algo = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
133 case VIRTIO_CRYPTO_MAC_GMAC_AES:
134 *algo = RTE_CRYPTO_AUTH_AES_GMAC;
136 case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
137 *algo = RTE_CRYPTO_AUTH_AES_CBC_MAC;
139 case VIRTIO_CRYPTO_MAC_XCBC_AES:
140 *algo = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
142 case VIRTIO_CRYPTO_MAC_CMAC_3DES:
143 case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
144 case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
145 return -VIRTIO_CRYPTO_NOTSUPP;
147 return -VIRTIO_CRYPTO_BADMSG;
153 static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
158 case RTE_CRYPTO_CIPHER_3DES_CBC:
161 case RTE_CRYPTO_CIPHER_3DES_CTR:
164 case RTE_CRYPTO_CIPHER_3DES_ECB:
167 case RTE_CRYPTO_CIPHER_AES_CBC:
171 /* TODO: add common algos */
182 * vhost_crypto struct is used to maintain a number of virtio_cryptos and
183 * one DPDK crypto device that deals with all crypto workloads. It is declared
184 * here and defined in vhost_crypto.c
186 struct vhost_crypto {
187 /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
190 struct rte_hash *session_map;
191 struct rte_mempool *mbuf_pool;
192 struct rte_mempool *sess_pool;
193 struct rte_mempool *sess_priv_pool;
194 struct rte_mempool *wb_pool;
196 /** DPDK cryptodev ID */
200 uint64_t last_session_id;
202 uint64_t cache_session_id;
203 struct rte_cryptodev_sym_session *cache_session;
204 /** socket id for the device */
207 struct virtio_net *dev;
210 } __rte_cache_aligned;
212 struct vhost_crypto_writeback_data {
216 struct vhost_crypto_writeback_data *next;
219 struct vhost_crypto_data_req {
220 struct vring_desc *head;
221 struct virtio_net *dev;
222 struct virtio_crypto_inhdr *inhdr;
223 struct vhost_virtqueue *vq;
224 struct vhost_crypto_writeback_data *wb;
225 struct rte_mempool *wb_pool;
232 transform_cipher_param(struct rte_crypto_sym_xform *xform,
233 VhostUserCryptoSessionParam *param)
237 ret = cipher_algo_transform(param->cipher_algo, &xform->cipher.algo);
238 if (unlikely(ret < 0))
241 if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
242 VC_LOG_DBG("Invalid cipher key length\n");
243 return -VIRTIO_CRYPTO_BADMSG;
246 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
247 xform->cipher.key.length = param->cipher_key_len;
248 if (xform->cipher.key.length > 0)
249 xform->cipher.key.data = param->cipher_key_buf;
250 if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
251 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
252 else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
253 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
255 VC_LOG_DBG("Bad operation type");
256 return -VIRTIO_CRYPTO_BADMSG;
259 ret = get_iv_len(xform->cipher.algo);
260 if (unlikely(ret < 0))
262 xform->cipher.iv.length = (uint16_t)ret;
263 xform->cipher.iv.offset = IV_OFFSET;
268 transform_chain_param(struct rte_crypto_sym_xform *xforms,
269 VhostUserCryptoSessionParam *param)
271 struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
274 switch (param->chaining_dir) {
275 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
277 xform_cipher = xforms->next;
278 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
279 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
281 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
282 xform_cipher = xforms;
283 xform_auth = xforms->next;
284 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
285 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
288 return -VIRTIO_CRYPTO_BADMSG;
292 ret = cipher_algo_transform(param->cipher_algo,
293 &xform_cipher->cipher.algo);
294 if (unlikely(ret < 0))
297 if (param->cipher_key_len > VHOST_USER_CRYPTO_MAX_CIPHER_KEY_LENGTH) {
298 VC_LOG_DBG("Invalid cipher key length\n");
299 return -VIRTIO_CRYPTO_BADMSG;
302 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
303 xform_cipher->cipher.key.length = param->cipher_key_len;
304 xform_cipher->cipher.key.data = param->cipher_key_buf;
305 ret = get_iv_len(xform_cipher->cipher.algo);
306 if (unlikely(ret < 0))
308 xform_cipher->cipher.iv.length = (uint16_t)ret;
309 xform_cipher->cipher.iv.offset = IV_OFFSET;
312 xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
313 ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo);
314 if (unlikely(ret < 0))
317 if (param->auth_key_len > VHOST_USER_CRYPTO_MAX_HMAC_KEY_LENGTH) {
318 VC_LOG_DBG("Invalid auth key length\n");
319 return -VIRTIO_CRYPTO_BADMSG;
322 xform_auth->auth.digest_length = param->digest_len;
323 xform_auth->auth.key.length = param->auth_key_len;
324 xform_auth->auth.key.data = param->auth_key_buf;
330 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
331 VhostUserCryptoSessionParam *sess_param)
333 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
334 struct rte_cryptodev_sym_session *session;
337 switch (sess_param->op_type) {
338 case VIRTIO_CRYPTO_SYM_OP_NONE:
339 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
340 ret = transform_cipher_param(&xform1, sess_param);
342 VC_LOG_ERR("Error transform session msg (%i)", ret);
343 sess_param->session_id = ret;
347 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
348 if (unlikely(sess_param->hash_mode !=
349 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
350 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
351 VC_LOG_ERR("Error transform session message (%i)",
352 -VIRTIO_CRYPTO_NOTSUPP);
356 xform1.next = &xform2;
358 ret = transform_chain_param(&xform1, sess_param);
360 VC_LOG_ERR("Error transform session message (%i)", ret);
361 sess_param->session_id = ret;
367 VC_LOG_ERR("Algorithm not yet supported");
368 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
372 session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
374 VC_LOG_ERR("Failed to create session");
375 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
379 if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
380 vcrypto->sess_priv_pool) < 0) {
381 VC_LOG_ERR("Failed to initialize session");
382 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
386 /* insert hash to map */
387 if (rte_hash_add_key_data(vcrypto->session_map,
388 &vcrypto->last_session_id, session) < 0) {
389 VC_LOG_ERR("Failed to insert session to hash table");
391 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
392 VC_LOG_ERR("Failed to clear session");
394 if (rte_cryptodev_sym_session_free(session) < 0)
395 VC_LOG_ERR("Failed to free session");
397 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
401 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
402 vcrypto->last_session_id, vcrypto->dev->vid);
404 sess_param->session_id = vcrypto->last_session_id;
405 vcrypto->last_session_id++;
409 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
411 struct rte_cryptodev_sym_session *session;
412 uint64_t sess_id = session_id;
415 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
418 if (unlikely(ret < 0)) {
419 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
420 return -VIRTIO_CRYPTO_INVSESS;
423 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
424 VC_LOG_DBG("Failed to clear session");
425 return -VIRTIO_CRYPTO_ERR;
428 if (rte_cryptodev_sym_session_free(session) < 0) {
429 VC_LOG_DBG("Failed to free session");
430 return -VIRTIO_CRYPTO_ERR;
433 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
434 VC_LOG_DBG("Failed to delete session from hash table.");
435 return -VIRTIO_CRYPTO_ERR;
438 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
444 static enum rte_vhost_msg_result
445 vhost_crypto_msg_post_handler(int vid, void *msg)
447 struct virtio_net *dev = get_device(vid);
448 struct vhost_crypto *vcrypto;
449 VhostUserMsg *vmsg = msg;
450 enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK;
453 VC_LOG_ERR("Invalid vid %i", vid);
454 return RTE_VHOST_MSG_RESULT_ERR;
457 vcrypto = dev->extern_data;
458 if (vcrypto == NULL) {
459 VC_LOG_ERR("Cannot find required data, is it initialized?");
460 return RTE_VHOST_MSG_RESULT_ERR;
463 switch (vmsg->request.master) {
464 case VHOST_USER_CRYPTO_CREATE_SESS:
465 vhost_crypto_create_sess(vcrypto,
466 &vmsg->payload.crypto_session);
468 ret = RTE_VHOST_MSG_RESULT_REPLY;
470 case VHOST_USER_CRYPTO_CLOSE_SESS:
471 if (vhost_crypto_close_sess(vcrypto, vmsg->payload.u64))
472 ret = RTE_VHOST_MSG_RESULT_ERR;
475 ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED;
482 static __rte_always_inline struct vring_desc *
483 find_write_desc(struct vring_desc *head, struct vring_desc *desc,
484 uint32_t *nb_descs, uint32_t vq_size)
486 if (desc->flags & VRING_DESC_F_WRITE)
489 while (desc->flags & VRING_DESC_F_NEXT) {
490 if (unlikely(*nb_descs == 0 || desc->next >= vq_size))
494 desc = &head[desc->next];
495 if (desc->flags & VRING_DESC_F_WRITE)
502 static struct virtio_crypto_inhdr *
503 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc,
504 uint32_t *nb_descs, uint32_t vq_size)
507 struct virtio_crypto_inhdr *inhdr;
509 while (desc->flags & VRING_DESC_F_NEXT) {
510 if (unlikely(*nb_descs == 0 || desc->next >= vq_size))
513 desc = &vc_req->head[desc->next];
517 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
518 &dlen, VHOST_ACCESS_WO);
519 if (unlikely(!inhdr || dlen != desc->len))
525 static __rte_always_inline int
526 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
527 uint32_t size, uint32_t *nb_descs, uint32_t vq_size)
529 struct vring_desc *desc = *cur_desc;
530 int left = size - desc->len;
532 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
533 if (unlikely(*nb_descs == 0 || desc->next >= vq_size))
536 desc = &head[desc->next];
537 rte_prefetch0(&head[desc->next]);
543 if (unlikely(left > 0))
546 if (unlikely(*nb_descs == 0))
549 if (unlikely(desc->next >= vq_size))
551 *cur_desc = &head[desc->next];
557 static __rte_always_inline void *
558 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc *cur_desc,
562 uint64_t dlen = cur_desc->len;
564 data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
565 if (unlikely(!data || dlen != cur_desc->len)) {
566 VC_LOG_ERR("Failed to map object");
574 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
575 struct vring_desc **cur_desc, uint32_t size,
576 uint32_t *nb_descs, uint32_t vq_size)
578 struct vring_desc *desc = *cur_desc;
579 uint64_t remain, addr, dlen, len;
581 uint8_t *data = dst_data;
585 to_copy = RTE_MIN(desc->len, (uint32_t)left);
587 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
589 if (unlikely(!src || !dlen))
592 rte_memcpy((uint8_t *)data, src, dlen);
595 if (unlikely(dlen < to_copy)) {
596 remain = to_copy - dlen;
597 addr = desc->addr + dlen;
601 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
603 if (unlikely(!src || !len)) {
604 VC_LOG_ERR("Failed to map descriptor");
608 rte_memcpy(data, src, len);
617 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
618 if (unlikely(*nb_descs == 0 || desc->next >= vq_size)) {
619 VC_LOG_ERR("Invalid descriptors");
624 desc = &vc_req->head[desc->next];
625 rte_prefetch0(&vc_req->head[desc->next]);
626 to_copy = RTE_MIN(desc->len, (uint32_t)left);
628 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
630 if (unlikely(!src || !dlen)) {
631 VC_LOG_ERR("Failed to map descriptor");
635 rte_memcpy(data, src, dlen);
638 if (unlikely(dlen < to_copy)) {
639 remain = to_copy - dlen;
640 addr = desc->addr + dlen;
644 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
646 if (unlikely(!src || !len)) {
647 VC_LOG_ERR("Failed to map descriptor");
651 rte_memcpy(data, src, len);
661 if (unlikely(left > 0)) {
662 VC_LOG_ERR("Incorrect virtio descriptor");
666 if (unlikely(*nb_descs == 0))
669 if (unlikely(desc->next >= vq_size))
671 *cur_desc = &vc_req->head[desc->next];
678 write_back_data(struct vhost_crypto_data_req *vc_req)
680 struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
683 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
685 wb_data = wb_data->next;
686 rte_mempool_put(vc_req->wb_pool, wb_last);
691 free_wb_data(struct vhost_crypto_writeback_data *wb_data,
692 struct rte_mempool *mp)
694 while (wb_data->next != NULL)
695 free_wb_data(wb_data->next, mp);
697 rte_mempool_put(mp, wb_data);
701 * The function will allocate a vhost_crypto_writeback_data linked list
702 * containing the source and destination data pointers for the write back
703 * operation after dequeued from Cryptodev PMD queues.
706 * The vhost crypto data request pointer
708 * The pointer of the current in use descriptor pointer. The content of
709 * cur_desc is expected to be updated after the function execution.
711 * The last write back data element to be returned. It is used only in cipher
712 * and hash chain operations.
714 * The source data pointer
716 * The offset to both source and destination data. For source data the offset
717 * is the number of bytes between src and start point of cipher operation. For
718 * destination data the offset is the number of bytes from *cur_desc->addr
719 * to the point where the src will be written to.
720 * @param write_back_len
721 * The size of the write back length.
723 * The pointer to the start of the write back data linked list.
725 static struct vhost_crypto_writeback_data *
726 prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
727 struct vring_desc **cur_desc,
728 struct vhost_crypto_writeback_data **end_wb_data,
731 uint64_t write_back_len,
732 uint32_t *nb_descs, uint32_t vq_size)
734 struct vhost_crypto_writeback_data *wb_data, *head;
735 struct vring_desc *desc = *cur_desc;
740 ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
741 if (unlikely(ret < 0)) {
742 VC_LOG_ERR("no memory");
748 if (likely(desc->len > offset)) {
749 wb_data->src = src + offset;
751 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
752 &dlen, VHOST_ACCESS_RW) + offset;
753 if (unlikely(!dst || dlen != desc->len)) {
754 VC_LOG_ERR("Failed to map descriptor");
759 wb_data->len = desc->len - offset;
760 write_back_len -= wb_data->len;
761 src += offset + wb_data->len;
764 if (unlikely(write_back_len)) {
765 ret = rte_mempool_get(vc_req->wb_pool,
766 (void **)&(wb_data->next));
767 if (unlikely(ret < 0)) {
768 VC_LOG_ERR("no memory");
772 wb_data = wb_data->next;
774 wb_data->next = NULL;
778 while (write_back_len) {
779 if (unlikely(*nb_descs == 0 || desc->next >= vq_size)) {
780 VC_LOG_ERR("Invalid descriptors");
785 desc = &vc_req->head[desc->next];
786 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
787 VC_LOG_ERR("incorrect descriptor");
791 if (desc->len <= offset) {
797 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
798 VHOST_ACCESS_RW) + offset;
799 if (unlikely(dst == NULL || dlen != desc->len)) {
800 VC_LOG_ERR("Failed to map descriptor");
806 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
807 write_back_len -= wb_data->len;
811 if (write_back_len) {
812 ret = rte_mempool_get(vc_req->wb_pool,
813 (void **)&(wb_data->next));
814 if (unlikely(ret < 0)) {
815 VC_LOG_ERR("no memory");
819 wb_data = wb_data->next;
821 wb_data->next = NULL;
824 if (unlikely(*nb_descs == 0))
827 if (unlikely(desc->next >= vq_size))
829 *cur_desc = &vc_req->head[desc->next];
832 *end_wb_data = wb_data;
838 free_wb_data(head, vc_req->wb_pool);
844 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
845 struct vhost_crypto_data_req *vc_req,
846 struct virtio_crypto_cipher_data_req *cipher,
847 struct vring_desc *cur_desc,
848 uint32_t *nb_descs, uint32_t vq_size)
850 struct vring_desc *desc = cur_desc;
851 struct vhost_crypto_writeback_data *ewb = NULL;
852 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
853 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
858 if (unlikely(copy_data(iv_data, vc_req, &desc, cipher->para.iv_len,
859 nb_descs, vq_size) < 0)) {
860 ret = VIRTIO_CRYPTO_BADMSG;
864 m_src->data_len = cipher->para.src_data_len;
866 switch (vcrypto->option) {
867 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
868 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
869 cipher->para.src_data_len);
870 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
871 if (unlikely(m_src->buf_iova == 0 ||
872 m_src->buf_addr == NULL)) {
873 VC_LOG_ERR("zero_copy may fail due to cross page data");
874 ret = VIRTIO_CRYPTO_ERR;
878 if (unlikely(move_desc(vc_req->head, &desc,
879 cipher->para.src_data_len, nb_descs,
881 VC_LOG_ERR("Incorrect descriptor");
882 ret = VIRTIO_CRYPTO_ERR;
887 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
888 vc_req->wb_pool = vcrypto->wb_pool;
890 if (unlikely(cipher->para.src_data_len >
891 RTE_MBUF_DEFAULT_BUF_SIZE)) {
892 VC_LOG_ERR("Not enough space to do data copy");
893 ret = VIRTIO_CRYPTO_ERR;
896 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
897 vc_req, &desc, cipher->para.src_data_len,
898 nb_descs, vq_size) < 0)) {
899 ret = VIRTIO_CRYPTO_BADMSG;
904 ret = VIRTIO_CRYPTO_BADMSG;
909 desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
910 if (unlikely(!desc)) {
911 VC_LOG_ERR("Cannot find write location");
912 ret = VIRTIO_CRYPTO_BADMSG;
916 switch (vcrypto->option) {
917 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
918 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
919 desc->addr, cipher->para.dst_data_len);
920 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
921 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
922 VC_LOG_ERR("zero_copy may fail due to cross page data");
923 ret = VIRTIO_CRYPTO_ERR;
927 if (unlikely(move_desc(vc_req->head, &desc,
928 cipher->para.dst_data_len,
929 nb_descs, vq_size) < 0)) {
930 VC_LOG_ERR("Incorrect descriptor");
931 ret = VIRTIO_CRYPTO_ERR;
935 m_dst->data_len = cipher->para.dst_data_len;
937 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
938 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
939 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
940 cipher->para.dst_data_len, nb_descs, vq_size);
941 if (unlikely(vc_req->wb == NULL)) {
942 ret = VIRTIO_CRYPTO_ERR;
948 ret = VIRTIO_CRYPTO_BADMSG;
953 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
954 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
956 op->sym->cipher.data.offset = 0;
957 op->sym->cipher.data.length = cipher->para.src_data_len;
959 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
960 if (unlikely(vc_req->inhdr == NULL)) {
961 ret = VIRTIO_CRYPTO_BADMSG;
965 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
966 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
972 free_wb_data(vc_req->wb, vc_req->wb_pool);
974 vc_req->len = INHDR_LEN;
979 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
980 struct vhost_crypto_data_req *vc_req,
981 struct virtio_crypto_alg_chain_data_req *chain,
982 struct vring_desc *cur_desc,
983 uint32_t *nb_descs, uint32_t vq_size)
985 struct vring_desc *desc = cur_desc, *digest_desc;
986 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
987 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
988 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
989 uint32_t digest_offset;
995 if (unlikely(copy_data(iv_data, vc_req, &desc,
996 chain->para.iv_len, nb_descs, vq_size) < 0)) {
997 ret = VIRTIO_CRYPTO_BADMSG;
1001 m_src->data_len = chain->para.src_data_len;
1003 switch (vcrypto->option) {
1004 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1005 m_dst->data_len = chain->para.dst_data_len;
1007 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
1008 chain->para.src_data_len);
1009 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1010 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
1011 VC_LOG_ERR("zero_copy may fail due to cross page data");
1012 ret = VIRTIO_CRYPTO_ERR;
1016 if (unlikely(move_desc(vc_req->head, &desc,
1017 chain->para.src_data_len,
1018 nb_descs, vq_size) < 0)) {
1019 VC_LOG_ERR("Incorrect descriptor");
1020 ret = VIRTIO_CRYPTO_ERR;
1024 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1025 vc_req->wb_pool = vcrypto->wb_pool;
1027 if (unlikely(chain->para.src_data_len >
1028 RTE_MBUF_DEFAULT_BUF_SIZE)) {
1029 VC_LOG_ERR("Not enough space to do data copy");
1030 ret = VIRTIO_CRYPTO_ERR;
1033 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1034 vc_req, &desc, chain->para.src_data_len,
1035 nb_descs, vq_size) < 0)) {
1036 ret = VIRTIO_CRYPTO_BADMSG;
1042 ret = VIRTIO_CRYPTO_BADMSG;
1047 desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
1048 if (unlikely(!desc)) {
1049 VC_LOG_ERR("Cannot find write location");
1050 ret = VIRTIO_CRYPTO_BADMSG;
1054 switch (vcrypto->option) {
1055 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1056 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
1057 desc->addr, chain->para.dst_data_len);
1058 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1059 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
1060 VC_LOG_ERR("zero_copy may fail due to cross page data");
1061 ret = VIRTIO_CRYPTO_ERR;
1065 if (unlikely(move_desc(vc_req->head, &desc,
1066 chain->para.dst_data_len,
1067 nb_descs, vq_size) < 0)) {
1068 VC_LOG_ERR("Incorrect descriptor");
1069 ret = VIRTIO_CRYPTO_ERR;
1073 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1074 desc->addr, chain->para.hash_result_len);
1075 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1077 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1078 VC_LOG_ERR("zero_copy may fail due to cross page data");
1079 ret = VIRTIO_CRYPTO_ERR;
1083 if (unlikely(move_desc(vc_req->head, &desc,
1084 chain->para.hash_result_len,
1085 nb_descs, vq_size) < 0)) {
1086 VC_LOG_ERR("Incorrect descriptor");
1087 ret = VIRTIO_CRYPTO_ERR;
1092 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1093 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1094 rte_pktmbuf_mtod(m_src, uint8_t *),
1095 chain->para.cipher_start_src_offset,
1096 chain->para.dst_data_len -
1097 chain->para.cipher_start_src_offset,
1099 if (unlikely(vc_req->wb == NULL)) {
1100 ret = VIRTIO_CRYPTO_ERR;
1104 digest_offset = m_src->data_len;
1105 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1109 /** create a wb_data for digest */
1110 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1111 digest_addr, 0, chain->para.hash_result_len,
1113 if (unlikely(ewb->next == NULL)) {
1114 ret = VIRTIO_CRYPTO_ERR;
1118 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1119 chain->para.hash_result_len,
1120 nb_descs, vq_size) < 0)) {
1121 ret = VIRTIO_CRYPTO_BADMSG;
1125 op->sym->auth.digest.data = digest_addr;
1126 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1130 ret = VIRTIO_CRYPTO_BADMSG;
1135 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1136 if (unlikely(vc_req->inhdr == NULL)) {
1137 ret = VIRTIO_CRYPTO_BADMSG;
1141 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1143 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1144 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1146 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1147 op->sym->cipher.data.length = chain->para.src_data_len -
1148 chain->para.cipher_start_src_offset;
1150 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1151 op->sym->auth.data.length = chain->para.len_to_hash;
1153 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1159 free_wb_data(vc_req->wb, vc_req->wb_pool);
1160 vc_req->len = INHDR_LEN;
1165 * Process on descriptor
1167 static __rte_always_inline int
1168 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1169 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1170 struct vring_desc *head, uint16_t desc_idx)
1172 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1173 struct rte_cryptodev_sym_session *session;
1174 struct virtio_crypto_op_data_req *req, tmp_req;
1175 struct virtio_crypto_inhdr *inhdr;
1176 struct vring_desc *desc = NULL;
1177 uint64_t session_id;
1179 uint32_t nb_descs = vq->size;
1182 vc_req->desc_idx = desc_idx;
1183 vc_req->dev = vcrypto->dev;
1186 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1188 nb_descs = dlen / sizeof(struct vring_desc);
1189 /* drop invalid descriptors */
1190 if (unlikely(nb_descs > vq->size))
1192 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1193 &dlen, VHOST_ACCESS_RO);
1194 if (unlikely(!desc || dlen != head->len))
1202 vc_req->head = head;
1203 vc_req->zero_copy = vcrypto->option;
1205 req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1206 if (unlikely(req == NULL)) {
1207 switch (vcrypto->option) {
1208 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1209 err = VIRTIO_CRYPTO_BADMSG;
1210 VC_LOG_ERR("Invalid descriptor");
1212 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1214 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req),
1215 &nb_descs, vq->size) < 0)) {
1216 err = VIRTIO_CRYPTO_BADMSG;
1217 VC_LOG_ERR("Invalid descriptor");
1222 err = VIRTIO_CRYPTO_ERR;
1223 VC_LOG_ERR("Invalid option");
1227 if (unlikely(move_desc(vc_req->head, &desc,
1228 sizeof(*req), &nb_descs, vq->size) < 0)) {
1229 VC_LOG_ERR("Incorrect descriptor");
1234 switch (req->header.opcode) {
1235 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1236 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1237 session_id = req->header.session_id;
1239 /* one branch to avoid unnecessary table lookup */
1240 if (vcrypto->cache_session_id != session_id) {
1241 err = rte_hash_lookup_data(vcrypto->session_map,
1242 &session_id, (void **)&session);
1243 if (unlikely(err < 0)) {
1244 err = VIRTIO_CRYPTO_ERR;
1245 VC_LOG_ERR("Failed to find session %"PRIu64,
1250 vcrypto->cache_session = session;
1251 vcrypto->cache_session_id = session_id;
1254 session = vcrypto->cache_session;
1256 err = rte_crypto_op_attach_sym_session(op, session);
1257 if (unlikely(err < 0)) {
1258 err = VIRTIO_CRYPTO_ERR;
1259 VC_LOG_ERR("Failed to attach session to op");
1263 switch (req->u.sym_req.op_type) {
1264 case VIRTIO_CRYPTO_SYM_OP_NONE:
1265 err = VIRTIO_CRYPTO_NOTSUPP;
1267 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1268 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1269 &req->u.sym_req.u.cipher, desc,
1270 &nb_descs, vq->size);
1272 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1273 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1274 &req->u.sym_req.u.chain, desc,
1275 &nb_descs, vq->size);
1278 if (unlikely(err != 0)) {
1279 VC_LOG_ERR("Failed to process sym request");
1284 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1285 req->header.opcode);
1293 inhdr = reach_inhdr(vc_req, desc, &nb_descs, vq->size);
1294 if (likely(inhdr != NULL))
1295 inhdr->status = (uint8_t)err;
1300 static __rte_always_inline struct vhost_virtqueue *
1301 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1302 struct vhost_virtqueue *old_vq)
1304 struct rte_mbuf *m_src = op->sym->m_src;
1305 struct rte_mbuf *m_dst = op->sym->m_dst;
1306 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1309 if (unlikely(!vc_req)) {
1310 VC_LOG_ERR("Failed to retrieve vc_req");
1314 if (old_vq && (vc_req->vq != old_vq))
1317 desc_idx = vc_req->desc_idx;
1319 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1320 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1322 if (vc_req->zero_copy == 0)
1323 write_back_data(vc_req);
1326 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1327 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1329 rte_mempool_put(m_src->pool, (void *)m_src);
1332 rte_mempool_put(m_dst->pool, (void *)m_dst);
1337 static __rte_always_inline uint16_t
1338 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1339 uint16_t nb_ops, int *callfd)
1341 uint16_t processed = 1;
1342 struct vhost_virtqueue *vq, *tmp_vq;
1344 if (unlikely(nb_ops == 0))
1347 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1348 if (unlikely(vq == NULL))
1352 while ((processed < nb_ops)) {
1353 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1356 if (unlikely(vq != tmp_vq))
1362 *callfd = vq->callfd;
1364 *(volatile uint16_t *)&vq->used->idx += processed;
1370 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1371 struct rte_mempool *sess_pool,
1372 struct rte_mempool *sess_priv_pool,
1375 struct virtio_net *dev = get_device(vid);
1376 struct rte_hash_parameters params = {0};
1377 struct vhost_crypto *vcrypto;
1382 VC_LOG_ERR("Invalid vid %i", vid);
1386 ret = rte_vhost_driver_set_features(dev->ifname,
1387 VIRTIO_CRYPTO_FEATURES);
1389 VC_LOG_ERR("Error setting features");
1393 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1394 RTE_CACHE_LINE_SIZE, socket_id);
1396 VC_LOG_ERR("Insufficient memory");
1400 vcrypto->sess_pool = sess_pool;
1401 vcrypto->sess_priv_pool = sess_priv_pool;
1402 vcrypto->cid = cryptodev_id;
1403 vcrypto->cache_session_id = UINT64_MAX;
1404 vcrypto->last_session_id = 1;
1406 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1408 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1410 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1411 params.hash_func = rte_jhash;
1412 params.key_len = sizeof(uint64_t);
1413 params.socket_id = socket_id;
1414 vcrypto->session_map = rte_hash_create(¶ms);
1415 if (!vcrypto->session_map) {
1416 VC_LOG_ERR("Failed to creath session map");
1421 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1422 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1423 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1424 sizeof(struct vhost_crypto_data_req),
1425 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1427 if (!vcrypto->mbuf_pool) {
1428 VC_LOG_ERR("Failed to creath mbuf pool");
1433 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1434 vcrypto->wb_pool = rte_mempool_create(name,
1435 VHOST_CRYPTO_MBUF_POOL_SIZE,
1436 sizeof(struct vhost_crypto_writeback_data),
1437 128, 0, NULL, NULL, NULL, NULL,
1438 rte_socket_id(), 0);
1439 if (!vcrypto->wb_pool) {
1440 VC_LOG_ERR("Failed to creath mempool");
1445 dev->extern_data = vcrypto;
1446 dev->extern_ops.pre_msg_handle = NULL;
1447 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1452 if (vcrypto->session_map)
1453 rte_hash_free(vcrypto->session_map);
1454 if (vcrypto->mbuf_pool)
1455 rte_mempool_free(vcrypto->mbuf_pool);
1463 rte_vhost_crypto_free(int vid)
1465 struct virtio_net *dev = get_device(vid);
1466 struct vhost_crypto *vcrypto;
1468 if (unlikely(dev == NULL)) {
1469 VC_LOG_ERR("Invalid vid %i", vid);
1473 vcrypto = dev->extern_data;
1474 if (unlikely(vcrypto == NULL)) {
1475 VC_LOG_ERR("Cannot find required data, is it initialized?");
1479 rte_hash_free(vcrypto->session_map);
1480 rte_mempool_free(vcrypto->mbuf_pool);
1481 rte_mempool_free(vcrypto->wb_pool);
1484 dev->extern_data = NULL;
1485 dev->extern_ops.pre_msg_handle = NULL;
1486 dev->extern_ops.post_msg_handle = NULL;
1492 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1494 struct virtio_net *dev = get_device(vid);
1495 struct vhost_crypto *vcrypto;
1497 if (unlikely(dev == NULL)) {
1498 VC_LOG_ERR("Invalid vid %i", vid);
1502 if (unlikely((uint32_t)option >=
1503 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1504 VC_LOG_ERR("Invalid option %i", option);
1508 vcrypto = (struct vhost_crypto *)dev->extern_data;
1509 if (unlikely(vcrypto == NULL)) {
1510 VC_LOG_ERR("Cannot find required data, is it initialized?");
1514 if (vcrypto->option == (uint8_t)option)
1517 if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1518 !(rte_mempool_full(vcrypto->wb_pool))) {
1519 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1523 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1526 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1527 vcrypto->wb_pool = rte_mempool_create(name,
1528 VHOST_CRYPTO_MBUF_POOL_SIZE,
1529 sizeof(struct vhost_crypto_writeback_data),
1530 128, 0, NULL, NULL, NULL, NULL,
1531 rte_socket_id(), 0);
1532 if (!vcrypto->wb_pool) {
1533 VC_LOG_ERR("Failed to creath mbuf pool");
1537 rte_mempool_free(vcrypto->wb_pool);
1538 vcrypto->wb_pool = NULL;
1541 vcrypto->option = (uint8_t)option;
1547 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1548 struct rte_crypto_op **ops, uint16_t nb_ops)
1550 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1551 struct virtio_net *dev = get_device(vid);
1552 struct vhost_crypto *vcrypto;
1553 struct vhost_virtqueue *vq;
1559 if (unlikely(dev == NULL)) {
1560 VC_LOG_ERR("Invalid vid %i", vid);
1564 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1565 VC_LOG_ERR("Invalid qid %u", qid);
1569 vcrypto = (struct vhost_crypto *)dev->extern_data;
1570 if (unlikely(vcrypto == NULL)) {
1571 VC_LOG_ERR("Cannot find required data, is it initialized?");
1575 vq = dev->virtqueue[qid];
1577 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1578 start_idx = vq->last_used_idx;
1579 count = avail_idx - start_idx;
1580 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1581 count = RTE_MIN(count, nb_ops);
1583 if (unlikely(count == 0))
1586 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1587 * we need only 1 mbuf as src and dst
1589 switch (vcrypto->option) {
1590 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1591 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1592 (void **)mbufs, count * 2) < 0)) {
1593 VC_LOG_ERR("Insufficient memory");
1597 for (i = 0; i < count; i++) {
1598 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1599 uint16_t desc_idx = vq->avail->ring[used_idx];
1600 struct vring_desc *head = &vq->desc[desc_idx];
1601 struct rte_crypto_op *op = ops[i];
1603 op->sym->m_src = mbufs[i * 2];
1604 op->sym->m_dst = mbufs[i * 2 + 1];
1605 op->sym->m_src->data_off = 0;
1606 op->sym->m_dst->data_off = 0;
1608 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1609 op, head, desc_idx) < 0))
1613 if (unlikely(i < count))
1614 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1615 (void **)&mbufs[i * 2],
1620 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1621 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1622 (void **)mbufs, count) < 0)) {
1623 VC_LOG_ERR("Insufficient memory");
1627 for (i = 0; i < count; i++) {
1628 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1629 uint16_t desc_idx = vq->avail->ring[used_idx];
1630 struct vring_desc *head = &vq->desc[desc_idx];
1631 struct rte_crypto_op *op = ops[i];
1633 op->sym->m_src = mbufs[i];
1634 op->sym->m_dst = NULL;
1635 op->sym->m_src->data_off = 0;
1637 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1638 op, head, desc_idx) < 0))
1642 if (unlikely(i < count))
1643 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1651 vq->last_used_idx += i;
1657 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1658 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1660 struct rte_crypto_op **tmp_ops = ops;
1661 uint16_t count = 0, left = nb_ops;
1666 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1668 if (unlikely(count == 0))
1671 tmp_ops = &tmp_ops[count];
1674 callfds[idx++] = callfd;
1676 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1677 VC_LOG_ERR("Too many vqs");
1684 return nb_ops - left;