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 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
242 xform->cipher.key.length = param->cipher_key_len;
243 if (xform->cipher.key.length > 0)
244 xform->cipher.key.data = param->cipher_key_buf;
245 if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
246 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
247 else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
248 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
250 VC_LOG_DBG("Bad operation type");
251 return -VIRTIO_CRYPTO_BADMSG;
254 ret = get_iv_len(xform->cipher.algo);
255 if (unlikely(ret < 0))
257 xform->cipher.iv.length = (uint16_t)ret;
258 xform->cipher.iv.offset = IV_OFFSET;
263 transform_chain_param(struct rte_crypto_sym_xform *xforms,
264 VhostUserCryptoSessionParam *param)
266 struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
269 switch (param->chaining_dir) {
270 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
272 xform_cipher = xforms->next;
273 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
274 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
276 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
277 xform_cipher = xforms;
278 xform_auth = xforms->next;
279 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
280 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
283 return -VIRTIO_CRYPTO_BADMSG;
287 ret = cipher_algo_transform(param->cipher_algo,
288 &xform_cipher->cipher.algo);
289 if (unlikely(ret < 0))
291 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
292 xform_cipher->cipher.key.length = param->cipher_key_len;
293 xform_cipher->cipher.key.data = param->cipher_key_buf;
294 ret = get_iv_len(xform_cipher->cipher.algo);
295 if (unlikely(ret < 0))
297 xform_cipher->cipher.iv.length = (uint16_t)ret;
298 xform_cipher->cipher.iv.offset = IV_OFFSET;
301 xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
302 ret = auth_algo_transform(param->hash_algo, &xform_auth->auth.algo);
303 if (unlikely(ret < 0))
305 xform_auth->auth.digest_length = param->digest_len;
306 xform_auth->auth.key.length = param->auth_key_len;
307 xform_auth->auth.key.data = param->auth_key_buf;
313 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
314 VhostUserCryptoSessionParam *sess_param)
316 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
317 struct rte_cryptodev_sym_session *session;
320 switch (sess_param->op_type) {
321 case VIRTIO_CRYPTO_SYM_OP_NONE:
322 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
323 ret = transform_cipher_param(&xform1, sess_param);
325 VC_LOG_ERR("Error transform session msg (%i)", ret);
326 sess_param->session_id = ret;
330 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
331 if (unlikely(sess_param->hash_mode !=
332 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
333 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
334 VC_LOG_ERR("Error transform session message (%i)",
335 -VIRTIO_CRYPTO_NOTSUPP);
339 xform1.next = &xform2;
341 ret = transform_chain_param(&xform1, sess_param);
343 VC_LOG_ERR("Error transform session message (%i)", ret);
344 sess_param->session_id = ret;
350 VC_LOG_ERR("Algorithm not yet supported");
351 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
355 session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
357 VC_LOG_ERR("Failed to create session");
358 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
362 if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
363 vcrypto->sess_priv_pool) < 0) {
364 VC_LOG_ERR("Failed to initialize session");
365 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
369 /* insert hash to map */
370 if (rte_hash_add_key_data(vcrypto->session_map,
371 &vcrypto->last_session_id, session) < 0) {
372 VC_LOG_ERR("Failed to insert session to hash table");
374 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
375 VC_LOG_ERR("Failed to clear session");
377 if (rte_cryptodev_sym_session_free(session) < 0)
378 VC_LOG_ERR("Failed to free session");
380 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
384 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
385 vcrypto->last_session_id, vcrypto->dev->vid);
387 sess_param->session_id = vcrypto->last_session_id;
388 vcrypto->last_session_id++;
392 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
394 struct rte_cryptodev_sym_session *session;
395 uint64_t sess_id = session_id;
398 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
401 if (unlikely(ret < 0)) {
402 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
403 return -VIRTIO_CRYPTO_INVSESS;
406 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
407 VC_LOG_DBG("Failed to clear session");
408 return -VIRTIO_CRYPTO_ERR;
411 if (rte_cryptodev_sym_session_free(session) < 0) {
412 VC_LOG_DBG("Failed to free session");
413 return -VIRTIO_CRYPTO_ERR;
416 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
417 VC_LOG_DBG("Failed to delete session from hash table.");
418 return -VIRTIO_CRYPTO_ERR;
421 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
427 static enum rte_vhost_msg_result
428 vhost_crypto_msg_post_handler(int vid, void *msg)
430 struct virtio_net *dev = get_device(vid);
431 struct vhost_crypto *vcrypto;
432 VhostUserMsg *vmsg = msg;
433 enum rte_vhost_msg_result ret = RTE_VHOST_MSG_RESULT_OK;
436 VC_LOG_ERR("Invalid vid %i", vid);
437 return RTE_VHOST_MSG_RESULT_ERR;
440 vcrypto = dev->extern_data;
441 if (vcrypto == NULL) {
442 VC_LOG_ERR("Cannot find required data, is it initialized?");
443 return RTE_VHOST_MSG_RESULT_ERR;
446 switch (vmsg->request.master) {
447 case VHOST_USER_CRYPTO_CREATE_SESS:
448 vhost_crypto_create_sess(vcrypto,
449 &vmsg->payload.crypto_session);
451 ret = RTE_VHOST_MSG_RESULT_REPLY;
453 case VHOST_USER_CRYPTO_CLOSE_SESS:
454 if (vhost_crypto_close_sess(vcrypto, vmsg->payload.u64))
455 ret = RTE_VHOST_MSG_RESULT_ERR;
458 ret = RTE_VHOST_MSG_RESULT_NOT_HANDLED;
465 static __rte_always_inline struct vring_desc *
466 find_write_desc(struct vring_desc *head, struct vring_desc *desc,
467 uint32_t *nb_descs, uint32_t vq_size)
469 if (desc->flags & VRING_DESC_F_WRITE)
472 while (desc->flags & VRING_DESC_F_NEXT) {
473 if (unlikely(*nb_descs == 0 || desc->next >= vq_size))
477 desc = &head[desc->next];
478 if (desc->flags & VRING_DESC_F_WRITE)
485 static struct virtio_crypto_inhdr *
486 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc,
487 uint32_t *nb_descs, uint32_t vq_size)
490 struct virtio_crypto_inhdr *inhdr;
492 while (desc->flags & VRING_DESC_F_NEXT) {
493 if (unlikely(*nb_descs == 0 || desc->next >= vq_size))
496 desc = &vc_req->head[desc->next];
500 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
501 &dlen, VHOST_ACCESS_WO);
502 if (unlikely(!inhdr || dlen != desc->len))
508 static __rte_always_inline int
509 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
510 uint32_t size, uint32_t *nb_descs, uint32_t vq_size)
512 struct vring_desc *desc = *cur_desc;
513 int left = size - desc->len;
515 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
517 if (unlikely(*nb_descs == 0 || desc->next >= vq_size))
520 desc = &head[desc->next];
521 rte_prefetch0(&head[desc->next]);
525 if (unlikely(left > 0))
528 if (unlikely(*nb_descs == 0))
531 if (unlikely(desc->next >= vq_size))
533 *cur_desc = &head[desc->next];
539 static __rte_always_inline void *
540 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc *cur_desc,
544 uint64_t dlen = cur_desc->len;
546 data = IOVA_TO_VVA(void *, vc_req, cur_desc->addr, &dlen, perm);
547 if (unlikely(!data || dlen != cur_desc->len)) {
548 VC_LOG_ERR("Failed to map object");
556 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
557 struct vring_desc **cur_desc, uint32_t size,
558 uint32_t *nb_descs, uint32_t vq_size)
560 struct vring_desc *desc = *cur_desc;
561 uint64_t remain, addr, dlen, len;
563 uint8_t *data = dst_data;
567 to_copy = RTE_MIN(desc->len, (uint32_t)left);
569 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
571 if (unlikely(!src || !dlen))
574 rte_memcpy((uint8_t *)data, src, dlen);
577 if (unlikely(dlen < to_copy)) {
578 remain = to_copy - dlen;
579 addr = desc->addr + dlen;
583 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
585 if (unlikely(!src || !len)) {
586 VC_LOG_ERR("Failed to map descriptor");
590 rte_memcpy(data, src, len);
599 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
600 if (unlikely(*nb_descs == 0 || desc->next >= vq_size)) {
601 VC_LOG_ERR("Invalid descriptors");
606 desc = &vc_req->head[desc->next];
607 rte_prefetch0(&vc_req->head[desc->next]);
608 to_copy = RTE_MIN(desc->len, (uint32_t)left);
610 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
612 if (unlikely(!src || !dlen)) {
613 VC_LOG_ERR("Failed to map descriptor");
617 rte_memcpy(data, src, dlen);
620 if (unlikely(dlen < to_copy)) {
621 remain = to_copy - dlen;
622 addr = desc->addr + dlen;
626 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
628 if (unlikely(!src || !len)) {
629 VC_LOG_ERR("Failed to map descriptor");
633 rte_memcpy(data, src, len);
643 if (unlikely(left > 0)) {
644 VC_LOG_ERR("Incorrect virtio descriptor");
648 if (unlikely(*nb_descs == 0))
651 if (unlikely(desc->next >= vq_size))
653 *cur_desc = &vc_req->head[desc->next];
660 write_back_data(struct vhost_crypto_data_req *vc_req)
662 struct vhost_crypto_writeback_data *wb_data = vc_req->wb, *wb_last;
665 rte_memcpy(wb_data->dst, wb_data->src, wb_data->len);
667 wb_data = wb_data->next;
668 rte_mempool_put(vc_req->wb_pool, wb_last);
673 free_wb_data(struct vhost_crypto_writeback_data *wb_data,
674 struct rte_mempool *mp)
676 while (wb_data->next != NULL)
677 free_wb_data(wb_data->next, mp);
679 rte_mempool_put(mp, wb_data);
683 * The function will allocate a vhost_crypto_writeback_data linked list
684 * containing the source and destination data pointers for the write back
685 * operation after dequeued from Cryptodev PMD queues.
688 * The vhost crypto data request pointer
690 * The pointer of the current in use descriptor pointer. The content of
691 * cur_desc is expected to be updated after the function execution.
693 * The last write back data element to be returned. It is used only in cipher
694 * and hash chain operations.
696 * The source data pointer
698 * The offset to both source and destination data. For source data the offset
699 * is the number of bytes between src and start point of cipher operation. For
700 * destination data the offset is the number of bytes from *cur_desc->addr
701 * to the point where the src will be written to.
702 * @param write_back_len
703 * The size of the write back length.
705 * The pointer to the start of the write back data linked list.
707 static struct vhost_crypto_writeback_data *
708 prepare_write_back_data(struct vhost_crypto_data_req *vc_req,
709 struct vring_desc **cur_desc,
710 struct vhost_crypto_writeback_data **end_wb_data,
713 uint64_t write_back_len,
714 uint32_t *nb_descs, uint32_t vq_size)
716 struct vhost_crypto_writeback_data *wb_data, *head;
717 struct vring_desc *desc = *cur_desc;
722 ret = rte_mempool_get(vc_req->wb_pool, (void **)&head);
723 if (unlikely(ret < 0)) {
724 VC_LOG_ERR("no memory");
730 if (likely(desc->len > offset)) {
731 wb_data->src = src + offset;
733 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr,
734 &dlen, VHOST_ACCESS_RW) + offset;
735 if (unlikely(!dst || dlen != desc->len)) {
736 VC_LOG_ERR("Failed to map descriptor");
741 wb_data->len = desc->len - offset;
742 write_back_len -= wb_data->len;
743 src += offset + wb_data->len;
746 if (unlikely(write_back_len)) {
747 ret = rte_mempool_get(vc_req->wb_pool,
748 (void **)&(wb_data->next));
749 if (unlikely(ret < 0)) {
750 VC_LOG_ERR("no memory");
754 wb_data = wb_data->next;
756 wb_data->next = NULL;
760 while (write_back_len) {
761 if (unlikely(*nb_descs == 0 || desc->next >= vq_size)) {
762 VC_LOG_ERR("Invalid descriptors");
767 desc = &vc_req->head[desc->next];
768 if (unlikely(!(desc->flags & VRING_DESC_F_WRITE))) {
769 VC_LOG_ERR("incorrect descriptor");
773 if (desc->len <= offset) {
779 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
780 VHOST_ACCESS_RW) + offset;
781 if (unlikely(dst == NULL || dlen != desc->len)) {
782 VC_LOG_ERR("Failed to map descriptor");
788 wb_data->len = RTE_MIN(desc->len - offset, write_back_len);
789 write_back_len -= wb_data->len;
793 if (write_back_len) {
794 ret = rte_mempool_get(vc_req->wb_pool,
795 (void **)&(wb_data->next));
796 if (unlikely(ret < 0)) {
797 VC_LOG_ERR("no memory");
801 wb_data = wb_data->next;
803 wb_data->next = NULL;
806 if (unlikely(*nb_descs == 0))
809 if (unlikely(desc->next >= vq_size))
811 *cur_desc = &vc_req->head[desc->next];
814 *end_wb_data = wb_data;
820 free_wb_data(head, vc_req->wb_pool);
826 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
827 struct vhost_crypto_data_req *vc_req,
828 struct virtio_crypto_cipher_data_req *cipher,
829 struct vring_desc *cur_desc,
830 uint32_t *nb_descs, uint32_t vq_size)
832 struct vring_desc *desc = cur_desc;
833 struct vhost_crypto_writeback_data *ewb = NULL;
834 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
835 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
840 if (unlikely(copy_data(iv_data, vc_req, &desc, cipher->para.iv_len,
841 nb_descs, vq_size) < 0)) {
842 ret = VIRTIO_CRYPTO_BADMSG;
846 m_src->data_len = cipher->para.src_data_len;
848 switch (vcrypto->option) {
849 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
850 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
851 cipher->para.src_data_len);
852 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
853 if (unlikely(m_src->buf_iova == 0 ||
854 m_src->buf_addr == NULL)) {
855 VC_LOG_ERR("zero_copy may fail due to cross page data");
856 ret = VIRTIO_CRYPTO_ERR;
860 if (unlikely(move_desc(vc_req->head, &desc,
861 cipher->para.src_data_len, nb_descs,
863 VC_LOG_ERR("Incorrect descriptor");
864 ret = VIRTIO_CRYPTO_ERR;
869 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
870 vc_req->wb_pool = vcrypto->wb_pool;
872 if (unlikely(cipher->para.src_data_len >
873 RTE_MBUF_DEFAULT_BUF_SIZE)) {
874 VC_LOG_ERR("Not enough space to do data copy");
875 ret = VIRTIO_CRYPTO_ERR;
878 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
879 vc_req, &desc, cipher->para.src_data_len,
880 nb_descs, vq_size) < 0)) {
881 ret = VIRTIO_CRYPTO_BADMSG;
886 ret = VIRTIO_CRYPTO_BADMSG;
891 desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
892 if (unlikely(!desc)) {
893 VC_LOG_ERR("Cannot find write location");
894 ret = VIRTIO_CRYPTO_BADMSG;
898 switch (vcrypto->option) {
899 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
900 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
901 desc->addr, cipher->para.dst_data_len);
902 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
903 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
904 VC_LOG_ERR("zero_copy may fail due to cross page data");
905 ret = VIRTIO_CRYPTO_ERR;
909 if (unlikely(move_desc(vc_req->head, &desc,
910 cipher->para.dst_data_len,
911 nb_descs, vq_size) < 0)) {
912 VC_LOG_ERR("Incorrect descriptor");
913 ret = VIRTIO_CRYPTO_ERR;
917 m_dst->data_len = cipher->para.dst_data_len;
919 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
920 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
921 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
922 cipher->para.dst_data_len, nb_descs, vq_size);
923 if (unlikely(vc_req->wb == NULL)) {
924 ret = VIRTIO_CRYPTO_ERR;
930 ret = VIRTIO_CRYPTO_BADMSG;
935 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
936 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
938 op->sym->cipher.data.offset = 0;
939 op->sym->cipher.data.length = cipher->para.src_data_len;
941 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
942 if (unlikely(vc_req->inhdr == NULL)) {
943 ret = VIRTIO_CRYPTO_BADMSG;
947 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
948 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
954 free_wb_data(vc_req->wb, vc_req->wb_pool);
956 vc_req->len = INHDR_LEN;
961 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
962 struct vhost_crypto_data_req *vc_req,
963 struct virtio_crypto_alg_chain_data_req *chain,
964 struct vring_desc *cur_desc,
965 uint32_t *nb_descs, uint32_t vq_size)
967 struct vring_desc *desc = cur_desc, *digest_desc;
968 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
969 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
970 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
971 uint32_t digest_offset;
977 if (unlikely(copy_data(iv_data, vc_req, &desc,
978 chain->para.iv_len, nb_descs, vq_size) < 0)) {
979 ret = VIRTIO_CRYPTO_BADMSG;
983 m_src->data_len = chain->para.src_data_len;
985 switch (vcrypto->option) {
986 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
987 m_dst->data_len = chain->para.dst_data_len;
989 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
990 chain->para.src_data_len);
991 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
992 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
993 VC_LOG_ERR("zero_copy may fail due to cross page data");
994 ret = VIRTIO_CRYPTO_ERR;
998 if (unlikely(move_desc(vc_req->head, &desc,
999 chain->para.src_data_len,
1000 nb_descs, vq_size) < 0)) {
1001 VC_LOG_ERR("Incorrect descriptor");
1002 ret = VIRTIO_CRYPTO_ERR;
1006 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1007 vc_req->wb_pool = vcrypto->wb_pool;
1009 if (unlikely(chain->para.src_data_len >
1010 RTE_MBUF_DEFAULT_BUF_SIZE)) {
1011 VC_LOG_ERR("Not enough space to do data copy");
1012 ret = VIRTIO_CRYPTO_ERR;
1015 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1016 vc_req, &desc, chain->para.src_data_len,
1017 nb_descs, vq_size) < 0)) {
1018 ret = VIRTIO_CRYPTO_BADMSG;
1024 ret = VIRTIO_CRYPTO_BADMSG;
1029 desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
1030 if (unlikely(!desc)) {
1031 VC_LOG_ERR("Cannot find write location");
1032 ret = VIRTIO_CRYPTO_BADMSG;
1036 switch (vcrypto->option) {
1037 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1038 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
1039 desc->addr, chain->para.dst_data_len);
1040 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1041 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
1042 VC_LOG_ERR("zero_copy may fail due to cross page data");
1043 ret = VIRTIO_CRYPTO_ERR;
1047 if (unlikely(move_desc(vc_req->head, &desc,
1048 chain->para.dst_data_len,
1049 nb_descs, vq_size) < 0)) {
1050 VC_LOG_ERR("Incorrect descriptor");
1051 ret = VIRTIO_CRYPTO_ERR;
1055 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1056 desc->addr, chain->para.hash_result_len);
1057 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1059 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
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.hash_result_len,
1067 nb_descs, vq_size) < 0)) {
1068 VC_LOG_ERR("Incorrect descriptor");
1069 ret = VIRTIO_CRYPTO_ERR;
1074 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1075 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1076 rte_pktmbuf_mtod(m_src, uint8_t *),
1077 chain->para.cipher_start_src_offset,
1078 chain->para.dst_data_len -
1079 chain->para.cipher_start_src_offset,
1081 if (unlikely(vc_req->wb == NULL)) {
1082 ret = VIRTIO_CRYPTO_ERR;
1086 digest_offset = m_src->data_len;
1087 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1091 /** create a wb_data for digest */
1092 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1093 digest_addr, 0, chain->para.hash_result_len,
1095 if (unlikely(ewb->next == NULL)) {
1096 ret = VIRTIO_CRYPTO_ERR;
1100 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1101 chain->para.hash_result_len,
1102 nb_descs, vq_size) < 0)) {
1103 ret = VIRTIO_CRYPTO_BADMSG;
1107 op->sym->auth.digest.data = digest_addr;
1108 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1112 ret = VIRTIO_CRYPTO_BADMSG;
1117 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1118 if (unlikely(vc_req->inhdr == NULL)) {
1119 ret = VIRTIO_CRYPTO_BADMSG;
1123 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1125 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1126 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1128 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1129 op->sym->cipher.data.length = chain->para.src_data_len -
1130 chain->para.cipher_start_src_offset;
1132 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1133 op->sym->auth.data.length = chain->para.len_to_hash;
1135 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1141 free_wb_data(vc_req->wb, vc_req->wb_pool);
1142 vc_req->len = INHDR_LEN;
1147 * Process on descriptor
1149 static __rte_always_inline int
1150 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1151 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1152 struct vring_desc *head, uint16_t desc_idx)
1154 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1155 struct rte_cryptodev_sym_session *session;
1156 struct virtio_crypto_op_data_req *req, tmp_req;
1157 struct virtio_crypto_inhdr *inhdr;
1158 struct vring_desc *desc = NULL;
1159 uint64_t session_id;
1161 uint32_t nb_descs = vq->size;
1164 vc_req->desc_idx = desc_idx;
1165 vc_req->dev = vcrypto->dev;
1168 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1170 nb_descs = dlen / sizeof(struct vring_desc);
1171 /* drop invalid descriptors */
1172 if (unlikely(nb_descs > vq->size))
1174 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1175 &dlen, VHOST_ACCESS_RO);
1176 if (unlikely(!desc || dlen != head->len))
1184 vc_req->head = head;
1185 vc_req->zero_copy = vcrypto->option;
1187 req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1188 if (unlikely(req == NULL)) {
1189 switch (vcrypto->option) {
1190 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1191 err = VIRTIO_CRYPTO_BADMSG;
1192 VC_LOG_ERR("Invalid descriptor");
1194 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1196 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req),
1197 &nb_descs, vq->size) < 0)) {
1198 err = VIRTIO_CRYPTO_BADMSG;
1199 VC_LOG_ERR("Invalid descriptor");
1204 err = VIRTIO_CRYPTO_ERR;
1205 VC_LOG_ERR("Invalid option");
1209 if (unlikely(move_desc(vc_req->head, &desc,
1210 sizeof(*req), &nb_descs, vq->size) < 0)) {
1211 VC_LOG_ERR("Incorrect descriptor");
1216 switch (req->header.opcode) {
1217 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1218 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1219 session_id = req->header.session_id;
1221 /* one branch to avoid unnecessary table lookup */
1222 if (vcrypto->cache_session_id != session_id) {
1223 err = rte_hash_lookup_data(vcrypto->session_map,
1224 &session_id, (void **)&session);
1225 if (unlikely(err < 0)) {
1226 err = VIRTIO_CRYPTO_ERR;
1227 VC_LOG_ERR("Failed to find session %"PRIu64,
1232 vcrypto->cache_session = session;
1233 vcrypto->cache_session_id = session_id;
1236 session = vcrypto->cache_session;
1238 err = rte_crypto_op_attach_sym_session(op, session);
1239 if (unlikely(err < 0)) {
1240 err = VIRTIO_CRYPTO_ERR;
1241 VC_LOG_ERR("Failed to attach session to op");
1245 switch (req->u.sym_req.op_type) {
1246 case VIRTIO_CRYPTO_SYM_OP_NONE:
1247 err = VIRTIO_CRYPTO_NOTSUPP;
1249 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1250 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1251 &req->u.sym_req.u.cipher, desc,
1252 &nb_descs, vq->size);
1254 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1255 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1256 &req->u.sym_req.u.chain, desc,
1257 &nb_descs, vq->size);
1260 if (unlikely(err != 0)) {
1261 VC_LOG_ERR("Failed to process sym request");
1266 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1267 req->header.opcode);
1275 inhdr = reach_inhdr(vc_req, desc, &nb_descs, vq->size);
1276 if (likely(inhdr != NULL))
1277 inhdr->status = (uint8_t)err;
1282 static __rte_always_inline struct vhost_virtqueue *
1283 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1284 struct vhost_virtqueue *old_vq)
1286 struct rte_mbuf *m_src = op->sym->m_src;
1287 struct rte_mbuf *m_dst = op->sym->m_dst;
1288 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1291 if (unlikely(!vc_req)) {
1292 VC_LOG_ERR("Failed to retrieve vc_req");
1296 if (old_vq && (vc_req->vq != old_vq))
1299 desc_idx = vc_req->desc_idx;
1301 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1302 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1304 if (vc_req->zero_copy == 0)
1305 write_back_data(vc_req);
1308 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1309 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1311 rte_mempool_put(m_src->pool, (void *)m_src);
1314 rte_mempool_put(m_dst->pool, (void *)m_dst);
1319 static __rte_always_inline uint16_t
1320 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1321 uint16_t nb_ops, int *callfd)
1323 uint16_t processed = 1;
1324 struct vhost_virtqueue *vq, *tmp_vq;
1326 if (unlikely(nb_ops == 0))
1329 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1330 if (unlikely(vq == NULL))
1334 while ((processed < nb_ops)) {
1335 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1338 if (unlikely(vq != tmp_vq))
1344 *callfd = vq->callfd;
1346 *(volatile uint16_t *)&vq->used->idx += processed;
1352 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1353 struct rte_mempool *sess_pool,
1354 struct rte_mempool *sess_priv_pool,
1357 struct virtio_net *dev = get_device(vid);
1358 struct rte_hash_parameters params = {0};
1359 struct vhost_crypto *vcrypto;
1364 VC_LOG_ERR("Invalid vid %i", vid);
1368 ret = rte_vhost_driver_set_features(dev->ifname,
1369 VIRTIO_CRYPTO_FEATURES);
1371 VC_LOG_ERR("Error setting features");
1375 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1376 RTE_CACHE_LINE_SIZE, socket_id);
1378 VC_LOG_ERR("Insufficient memory");
1382 vcrypto->sess_pool = sess_pool;
1383 vcrypto->sess_priv_pool = sess_priv_pool;
1384 vcrypto->cid = cryptodev_id;
1385 vcrypto->cache_session_id = UINT64_MAX;
1386 vcrypto->last_session_id = 1;
1388 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1390 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1392 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1393 params.hash_func = rte_jhash;
1394 params.key_len = sizeof(uint64_t);
1395 params.socket_id = socket_id;
1396 vcrypto->session_map = rte_hash_create(¶ms);
1397 if (!vcrypto->session_map) {
1398 VC_LOG_ERR("Failed to creath session map");
1403 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1404 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1405 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1406 sizeof(struct vhost_crypto_data_req),
1407 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1409 if (!vcrypto->mbuf_pool) {
1410 VC_LOG_ERR("Failed to creath mbuf pool");
1415 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1416 vcrypto->wb_pool = rte_mempool_create(name,
1417 VHOST_CRYPTO_MBUF_POOL_SIZE,
1418 sizeof(struct vhost_crypto_writeback_data),
1419 128, 0, NULL, NULL, NULL, NULL,
1420 rte_socket_id(), 0);
1421 if (!vcrypto->wb_pool) {
1422 VC_LOG_ERR("Failed to creath mempool");
1427 dev->extern_data = vcrypto;
1428 dev->extern_ops.pre_msg_handle = NULL;
1429 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1434 if (vcrypto->session_map)
1435 rte_hash_free(vcrypto->session_map);
1436 if (vcrypto->mbuf_pool)
1437 rte_mempool_free(vcrypto->mbuf_pool);
1445 rte_vhost_crypto_free(int vid)
1447 struct virtio_net *dev = get_device(vid);
1448 struct vhost_crypto *vcrypto;
1450 if (unlikely(dev == NULL)) {
1451 VC_LOG_ERR("Invalid vid %i", vid);
1455 vcrypto = dev->extern_data;
1456 if (unlikely(vcrypto == NULL)) {
1457 VC_LOG_ERR("Cannot find required data, is it initialized?");
1461 rte_hash_free(vcrypto->session_map);
1462 rte_mempool_free(vcrypto->mbuf_pool);
1463 rte_mempool_free(vcrypto->wb_pool);
1466 dev->extern_data = NULL;
1467 dev->extern_ops.pre_msg_handle = NULL;
1468 dev->extern_ops.post_msg_handle = NULL;
1474 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1476 struct virtio_net *dev = get_device(vid);
1477 struct vhost_crypto *vcrypto;
1479 if (unlikely(dev == NULL)) {
1480 VC_LOG_ERR("Invalid vid %i", vid);
1484 if (unlikely((uint32_t)option >=
1485 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1486 VC_LOG_ERR("Invalid option %i", option);
1490 vcrypto = (struct vhost_crypto *)dev->extern_data;
1491 if (unlikely(vcrypto == NULL)) {
1492 VC_LOG_ERR("Cannot find required data, is it initialized?");
1496 if (vcrypto->option == (uint8_t)option)
1499 if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1500 !(rte_mempool_full(vcrypto->wb_pool))) {
1501 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1505 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1508 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1509 vcrypto->wb_pool = rte_mempool_create(name,
1510 VHOST_CRYPTO_MBUF_POOL_SIZE,
1511 sizeof(struct vhost_crypto_writeback_data),
1512 128, 0, NULL, NULL, NULL, NULL,
1513 rte_socket_id(), 0);
1514 if (!vcrypto->wb_pool) {
1515 VC_LOG_ERR("Failed to creath mbuf pool");
1519 rte_mempool_free(vcrypto->wb_pool);
1520 vcrypto->wb_pool = NULL;
1523 vcrypto->option = (uint8_t)option;
1529 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1530 struct rte_crypto_op **ops, uint16_t nb_ops)
1532 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1533 struct virtio_net *dev = get_device(vid);
1534 struct vhost_crypto *vcrypto;
1535 struct vhost_virtqueue *vq;
1541 if (unlikely(dev == NULL)) {
1542 VC_LOG_ERR("Invalid vid %i", vid);
1546 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1547 VC_LOG_ERR("Invalid qid %u", qid);
1551 vcrypto = (struct vhost_crypto *)dev->extern_data;
1552 if (unlikely(vcrypto == NULL)) {
1553 VC_LOG_ERR("Cannot find required data, is it initialized?");
1557 vq = dev->virtqueue[qid];
1559 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1560 start_idx = vq->last_used_idx;
1561 count = avail_idx - start_idx;
1562 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1563 count = RTE_MIN(count, nb_ops);
1565 if (unlikely(count == 0))
1568 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1569 * we need only 1 mbuf as src and dst
1571 switch (vcrypto->option) {
1572 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1573 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1574 (void **)mbufs, count * 2) < 0)) {
1575 VC_LOG_ERR("Insufficient memory");
1579 for (i = 0; i < count; i++) {
1580 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1581 uint16_t desc_idx = vq->avail->ring[used_idx];
1582 struct vring_desc *head = &vq->desc[desc_idx];
1583 struct rte_crypto_op *op = ops[i];
1585 op->sym->m_src = mbufs[i * 2];
1586 op->sym->m_dst = mbufs[i * 2 + 1];
1587 op->sym->m_src->data_off = 0;
1588 op->sym->m_dst->data_off = 0;
1590 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1591 op, head, desc_idx) < 0))
1595 if (unlikely(i < count))
1596 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1597 (void **)&mbufs[i * 2],
1602 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1603 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1604 (void **)mbufs, count) < 0)) {
1605 VC_LOG_ERR("Insufficient memory");
1609 for (i = 0; i < count; i++) {
1610 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1611 uint16_t desc_idx = vq->avail->ring[used_idx];
1612 struct vring_desc *head = &vq->desc[desc_idx];
1613 struct rte_crypto_op *op = ops[i];
1615 op->sym->m_src = mbufs[i];
1616 op->sym->m_dst = NULL;
1617 op->sym->m_src->data_off = 0;
1619 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1620 op, head, desc_idx) < 0))
1624 if (unlikely(i < count))
1625 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1633 vq->last_used_idx += i;
1639 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1640 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1642 struct rte_crypto_op **tmp_ops = ops;
1643 uint16_t count = 0, left = nb_ops;
1648 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1650 if (unlikely(count == 0))
1653 tmp_ops = &tmp_ops[count];
1656 callfds[idx++] = callfd;
1658 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1659 VC_LOG_ERR("Too many vqs");
1666 return nb_ops - left;