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);
753 if (unlikely(!dst || dlen != desc->len)) {
754 VC_LOG_ERR("Failed to map descriptor");
758 wb_data->dst = dst + offset;
759 wb_data->len = RTE_MIN(dlen - offset, write_back_len);
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");
804 wb_data->src = src + offset;
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);
843 static __rte_always_inline uint8_t
844 vhost_crypto_check_cipher_request(struct virtio_crypto_cipher_data_req *req)
846 if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
847 (req->para.src_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE) &&
848 (req->para.dst_data_len >= req->para.src_data_len) &&
849 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_BUF_SIZE)))
850 return VIRTIO_CRYPTO_OK;
851 return VIRTIO_CRYPTO_BADMSG;
855 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
856 struct vhost_crypto_data_req *vc_req,
857 struct virtio_crypto_cipher_data_req *cipher,
858 struct vring_desc *cur_desc,
859 uint32_t *nb_descs, uint32_t vq_size)
861 struct vring_desc *desc = cur_desc;
862 struct vhost_crypto_writeback_data *ewb = NULL;
863 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
864 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
865 uint8_t ret = vhost_crypto_check_cipher_request(cipher);
867 if (unlikely(ret != VIRTIO_CRYPTO_OK))
872 if (unlikely(copy_data(iv_data, vc_req, &desc, cipher->para.iv_len,
873 nb_descs, vq_size) < 0)) {
874 ret = VIRTIO_CRYPTO_BADMSG;
878 switch (vcrypto->option) {
879 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
880 m_src->data_len = cipher->para.src_data_len;
881 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
882 cipher->para.src_data_len);
883 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
884 if (unlikely(m_src->buf_iova == 0 ||
885 m_src->buf_addr == NULL)) {
886 VC_LOG_ERR("zero_copy may fail due to cross page data");
887 ret = VIRTIO_CRYPTO_ERR;
891 if (unlikely(move_desc(vc_req->head, &desc,
892 cipher->para.src_data_len, nb_descs,
894 VC_LOG_ERR("Incorrect descriptor");
895 ret = VIRTIO_CRYPTO_ERR;
900 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
901 vc_req->wb_pool = vcrypto->wb_pool;
902 m_src->data_len = cipher->para.src_data_len;
903 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
904 vc_req, &desc, cipher->para.src_data_len,
905 nb_descs, vq_size) < 0)) {
906 ret = VIRTIO_CRYPTO_BADMSG;
911 ret = VIRTIO_CRYPTO_BADMSG;
916 desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
917 if (unlikely(!desc)) {
918 VC_LOG_ERR("Cannot find write location");
919 ret = VIRTIO_CRYPTO_BADMSG;
923 switch (vcrypto->option) {
924 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
925 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
926 desc->addr, cipher->para.dst_data_len);
927 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
928 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
929 VC_LOG_ERR("zero_copy may fail due to cross page data");
930 ret = VIRTIO_CRYPTO_ERR;
934 if (unlikely(move_desc(vc_req->head, &desc,
935 cipher->para.dst_data_len,
936 nb_descs, vq_size) < 0)) {
937 VC_LOG_ERR("Incorrect descriptor");
938 ret = VIRTIO_CRYPTO_ERR;
942 m_dst->data_len = cipher->para.dst_data_len;
944 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
945 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
946 rte_pktmbuf_mtod(m_src, uint8_t *), 0,
947 cipher->para.dst_data_len, nb_descs, vq_size);
948 if (unlikely(vc_req->wb == NULL)) {
949 ret = VIRTIO_CRYPTO_ERR;
955 ret = VIRTIO_CRYPTO_BADMSG;
960 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
961 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
963 op->sym->cipher.data.offset = 0;
964 op->sym->cipher.data.length = cipher->para.src_data_len;
966 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
967 if (unlikely(vc_req->inhdr == NULL)) {
968 ret = VIRTIO_CRYPTO_BADMSG;
972 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
973 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
979 free_wb_data(vc_req->wb, vc_req->wb_pool);
981 vc_req->len = INHDR_LEN;
985 static __rte_always_inline uint8_t
986 vhost_crypto_check_chain_request(struct virtio_crypto_alg_chain_data_req *req)
988 if (likely((req->para.iv_len <= VHOST_CRYPTO_MAX_IV_LEN) &&
989 (req->para.src_data_len <= RTE_MBUF_DEFAULT_DATAROOM) &&
990 (req->para.dst_data_len >= req->para.src_data_len) &&
991 (req->para.dst_data_len <= RTE_MBUF_DEFAULT_DATAROOM) &&
992 (req->para.cipher_start_src_offset <
993 RTE_MBUF_DEFAULT_DATAROOM) &&
994 (req->para.len_to_cipher < RTE_MBUF_DEFAULT_DATAROOM) &&
995 (req->para.hash_start_src_offset <
996 RTE_MBUF_DEFAULT_DATAROOM) &&
997 (req->para.len_to_hash < RTE_MBUF_DEFAULT_DATAROOM) &&
998 (req->para.cipher_start_src_offset + req->para.len_to_cipher <=
999 req->para.src_data_len) &&
1000 (req->para.hash_start_src_offset + req->para.len_to_hash <=
1001 req->para.src_data_len) &&
1002 (req->para.dst_data_len + req->para.hash_result_len <=
1003 RTE_MBUF_DEFAULT_DATAROOM)))
1004 return VIRTIO_CRYPTO_OK;
1005 return VIRTIO_CRYPTO_BADMSG;
1009 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
1010 struct vhost_crypto_data_req *vc_req,
1011 struct virtio_crypto_alg_chain_data_req *chain,
1012 struct vring_desc *cur_desc,
1013 uint32_t *nb_descs, uint32_t vq_size)
1015 struct vring_desc *desc = cur_desc, *digest_desc;
1016 struct vhost_crypto_writeback_data *ewb = NULL, *ewb2 = NULL;
1017 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
1018 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
1019 uint32_t digest_offset;
1021 uint8_t ret = vhost_crypto_check_chain_request(chain);
1023 if (unlikely(ret != VIRTIO_CRYPTO_OK))
1028 if (unlikely(copy_data(iv_data, vc_req, &desc,
1029 chain->para.iv_len, nb_descs, vq_size) < 0)) {
1030 ret = VIRTIO_CRYPTO_BADMSG;
1034 switch (vcrypto->option) {
1035 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1036 m_src->data_len = chain->para.src_data_len;
1037 m_dst->data_len = chain->para.dst_data_len;
1039 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
1040 chain->para.src_data_len);
1041 m_src->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1042 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
1043 VC_LOG_ERR("zero_copy may fail due to cross page data");
1044 ret = VIRTIO_CRYPTO_ERR;
1048 if (unlikely(move_desc(vc_req->head, &desc,
1049 chain->para.src_data_len,
1050 nb_descs, vq_size) < 0)) {
1051 VC_LOG_ERR("Incorrect descriptor");
1052 ret = VIRTIO_CRYPTO_ERR;
1056 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1057 vc_req->wb_pool = vcrypto->wb_pool;
1058 m_src->data_len = chain->para.src_data_len;
1059 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
1060 vc_req, &desc, chain->para.src_data_len,
1061 nb_descs, vq_size) < 0)) {
1062 ret = VIRTIO_CRYPTO_BADMSG;
1068 ret = VIRTIO_CRYPTO_BADMSG;
1073 desc = find_write_desc(vc_req->head, desc, nb_descs, vq_size);
1074 if (unlikely(!desc)) {
1075 VC_LOG_ERR("Cannot find write location");
1076 ret = VIRTIO_CRYPTO_BADMSG;
1080 switch (vcrypto->option) {
1081 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1082 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
1083 desc->addr, chain->para.dst_data_len);
1084 m_dst->buf_addr = get_data_ptr(vc_req, desc, VHOST_ACCESS_RW);
1085 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
1086 VC_LOG_ERR("zero_copy may fail due to cross page data");
1087 ret = VIRTIO_CRYPTO_ERR;
1091 if (unlikely(move_desc(vc_req->head, &desc,
1092 chain->para.dst_data_len,
1093 nb_descs, vq_size) < 0)) {
1094 VC_LOG_ERR("Incorrect descriptor");
1095 ret = VIRTIO_CRYPTO_ERR;
1099 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
1100 desc->addr, chain->para.hash_result_len);
1101 op->sym->auth.digest.data = get_data_ptr(vc_req, desc,
1103 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
1104 VC_LOG_ERR("zero_copy may fail due to cross page data");
1105 ret = VIRTIO_CRYPTO_ERR;
1109 if (unlikely(move_desc(vc_req->head, &desc,
1110 chain->para.hash_result_len,
1111 nb_descs, vq_size) < 0)) {
1112 VC_LOG_ERR("Incorrect descriptor");
1113 ret = VIRTIO_CRYPTO_ERR;
1118 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1119 vc_req->wb = prepare_write_back_data(vc_req, &desc, &ewb,
1120 rte_pktmbuf_mtod(m_src, uint8_t *),
1121 chain->para.cipher_start_src_offset,
1122 chain->para.dst_data_len -
1123 chain->para.cipher_start_src_offset,
1125 if (unlikely(vc_req->wb == NULL)) {
1126 ret = VIRTIO_CRYPTO_ERR;
1130 digest_offset = m_src->data_len;
1131 digest_addr = rte_pktmbuf_mtod_offset(m_src, void *,
1135 /** create a wb_data for digest */
1136 ewb->next = prepare_write_back_data(vc_req, &desc, &ewb2,
1137 digest_addr, 0, chain->para.hash_result_len,
1139 if (unlikely(ewb->next == NULL)) {
1140 ret = VIRTIO_CRYPTO_ERR;
1144 if (unlikely(copy_data(digest_addr, vc_req, &digest_desc,
1145 chain->para.hash_result_len,
1146 nb_descs, vq_size) < 0)) {
1147 ret = VIRTIO_CRYPTO_BADMSG;
1151 op->sym->auth.digest.data = digest_addr;
1152 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_src,
1156 ret = VIRTIO_CRYPTO_BADMSG;
1161 vc_req->inhdr = get_data_ptr(vc_req, desc, VHOST_ACCESS_WO);
1162 if (unlikely(vc_req->inhdr == NULL)) {
1163 ret = VIRTIO_CRYPTO_BADMSG;
1167 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
1169 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
1170 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
1172 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
1173 op->sym->cipher.data.length = chain->para.src_data_len -
1174 chain->para.cipher_start_src_offset;
1176 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
1177 op->sym->auth.data.length = chain->para.len_to_hash;
1179 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
1185 free_wb_data(vc_req->wb, vc_req->wb_pool);
1186 vc_req->len = INHDR_LEN;
1191 * Process on descriptor
1193 static __rte_always_inline int
1194 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
1195 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
1196 struct vring_desc *head, uint16_t desc_idx)
1198 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
1199 struct rte_cryptodev_sym_session *session;
1200 struct virtio_crypto_op_data_req *req, tmp_req;
1201 struct virtio_crypto_inhdr *inhdr;
1202 struct vring_desc *desc = NULL;
1203 uint64_t session_id;
1205 uint32_t nb_descs = vq->size;
1208 vc_req->desc_idx = desc_idx;
1209 vc_req->dev = vcrypto->dev;
1212 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
1214 nb_descs = dlen / sizeof(struct vring_desc);
1215 /* drop invalid descriptors */
1216 if (unlikely(nb_descs > vq->size))
1218 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
1219 &dlen, VHOST_ACCESS_RO);
1220 if (unlikely(!desc || dlen != head->len))
1228 vc_req->head = head;
1229 vc_req->zero_copy = vcrypto->option;
1231 req = get_data_ptr(vc_req, desc, VHOST_ACCESS_RO);
1232 if (unlikely(req == NULL)) {
1233 switch (vcrypto->option) {
1234 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1235 err = VIRTIO_CRYPTO_BADMSG;
1236 VC_LOG_ERR("Invalid descriptor");
1238 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1240 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req),
1241 &nb_descs, vq->size) < 0)) {
1242 err = VIRTIO_CRYPTO_BADMSG;
1243 VC_LOG_ERR("Invalid descriptor");
1248 err = VIRTIO_CRYPTO_ERR;
1249 VC_LOG_ERR("Invalid option");
1253 if (unlikely(move_desc(vc_req->head, &desc,
1254 sizeof(*req), &nb_descs, vq->size) < 0)) {
1255 VC_LOG_ERR("Incorrect descriptor");
1260 switch (req->header.opcode) {
1261 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
1262 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
1263 session_id = req->header.session_id;
1265 /* one branch to avoid unnecessary table lookup */
1266 if (vcrypto->cache_session_id != session_id) {
1267 err = rte_hash_lookup_data(vcrypto->session_map,
1268 &session_id, (void **)&session);
1269 if (unlikely(err < 0)) {
1270 err = VIRTIO_CRYPTO_ERR;
1271 VC_LOG_ERR("Failed to find session %"PRIu64,
1276 vcrypto->cache_session = session;
1277 vcrypto->cache_session_id = session_id;
1280 session = vcrypto->cache_session;
1282 err = rte_crypto_op_attach_sym_session(op, session);
1283 if (unlikely(err < 0)) {
1284 err = VIRTIO_CRYPTO_ERR;
1285 VC_LOG_ERR("Failed to attach session to op");
1289 switch (req->u.sym_req.op_type) {
1290 case VIRTIO_CRYPTO_SYM_OP_NONE:
1291 err = VIRTIO_CRYPTO_NOTSUPP;
1293 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1294 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1295 &req->u.sym_req.u.cipher, desc,
1296 &nb_descs, vq->size);
1298 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1299 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1300 &req->u.sym_req.u.chain, desc,
1301 &nb_descs, vq->size);
1304 if (unlikely(err != 0)) {
1305 VC_LOG_ERR("Failed to process sym request");
1310 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1311 req->header.opcode);
1319 inhdr = reach_inhdr(vc_req, desc, &nb_descs, vq->size);
1320 if (likely(inhdr != NULL))
1321 inhdr->status = (uint8_t)err;
1326 static __rte_always_inline struct vhost_virtqueue *
1327 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1328 struct vhost_virtqueue *old_vq)
1330 struct rte_mbuf *m_src = op->sym->m_src;
1331 struct rte_mbuf *m_dst = op->sym->m_dst;
1332 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1335 if (unlikely(!vc_req)) {
1336 VC_LOG_ERR("Failed to retrieve vc_req");
1340 if (old_vq && (vc_req->vq != old_vq))
1343 desc_idx = vc_req->desc_idx;
1345 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1346 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1348 if (vc_req->zero_copy == 0)
1349 write_back_data(vc_req);
1352 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1353 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1355 rte_mempool_put(m_src->pool, (void *)m_src);
1358 rte_mempool_put(m_dst->pool, (void *)m_dst);
1363 static __rte_always_inline uint16_t
1364 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1365 uint16_t nb_ops, int *callfd)
1367 uint16_t processed = 1;
1368 struct vhost_virtqueue *vq, *tmp_vq;
1370 if (unlikely(nb_ops == 0))
1373 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1374 if (unlikely(vq == NULL))
1378 while ((processed < nb_ops)) {
1379 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1382 if (unlikely(vq != tmp_vq))
1388 *callfd = vq->callfd;
1390 *(volatile uint16_t *)&vq->used->idx += processed;
1396 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1397 struct rte_mempool *sess_pool,
1398 struct rte_mempool *sess_priv_pool,
1401 struct virtio_net *dev = get_device(vid);
1402 struct rte_hash_parameters params = {0};
1403 struct vhost_crypto *vcrypto;
1408 VC_LOG_ERR("Invalid vid %i", vid);
1412 ret = rte_vhost_driver_set_features(dev->ifname,
1413 VIRTIO_CRYPTO_FEATURES);
1415 VC_LOG_ERR("Error setting features");
1419 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1420 RTE_CACHE_LINE_SIZE, socket_id);
1422 VC_LOG_ERR("Insufficient memory");
1426 vcrypto->sess_pool = sess_pool;
1427 vcrypto->sess_priv_pool = sess_priv_pool;
1428 vcrypto->cid = cryptodev_id;
1429 vcrypto->cache_session_id = UINT64_MAX;
1430 vcrypto->last_session_id = 1;
1432 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1434 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1436 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1437 params.hash_func = rte_jhash;
1438 params.key_len = sizeof(uint64_t);
1439 params.socket_id = socket_id;
1440 vcrypto->session_map = rte_hash_create(¶ms);
1441 if (!vcrypto->session_map) {
1442 VC_LOG_ERR("Failed to creath session map");
1447 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1448 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1449 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1450 sizeof(struct vhost_crypto_data_req),
1451 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1453 if (!vcrypto->mbuf_pool) {
1454 VC_LOG_ERR("Failed to creath mbuf pool");
1459 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1460 vcrypto->wb_pool = rte_mempool_create(name,
1461 VHOST_CRYPTO_MBUF_POOL_SIZE,
1462 sizeof(struct vhost_crypto_writeback_data),
1463 128, 0, NULL, NULL, NULL, NULL,
1464 rte_socket_id(), 0);
1465 if (!vcrypto->wb_pool) {
1466 VC_LOG_ERR("Failed to creath mempool");
1471 dev->extern_data = vcrypto;
1472 dev->extern_ops.pre_msg_handle = NULL;
1473 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1478 if (vcrypto->session_map)
1479 rte_hash_free(vcrypto->session_map);
1480 if (vcrypto->mbuf_pool)
1481 rte_mempool_free(vcrypto->mbuf_pool);
1489 rte_vhost_crypto_free(int vid)
1491 struct virtio_net *dev = get_device(vid);
1492 struct vhost_crypto *vcrypto;
1494 if (unlikely(dev == NULL)) {
1495 VC_LOG_ERR("Invalid vid %i", vid);
1499 vcrypto = dev->extern_data;
1500 if (unlikely(vcrypto == NULL)) {
1501 VC_LOG_ERR("Cannot find required data, is it initialized?");
1505 rte_hash_free(vcrypto->session_map);
1506 rte_mempool_free(vcrypto->mbuf_pool);
1507 rte_mempool_free(vcrypto->wb_pool);
1510 dev->extern_data = NULL;
1511 dev->extern_ops.pre_msg_handle = NULL;
1512 dev->extern_ops.post_msg_handle = NULL;
1518 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1520 struct virtio_net *dev = get_device(vid);
1521 struct vhost_crypto *vcrypto;
1523 if (unlikely(dev == NULL)) {
1524 VC_LOG_ERR("Invalid vid %i", vid);
1528 if (unlikely((uint32_t)option >=
1529 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1530 VC_LOG_ERR("Invalid option %i", option);
1534 vcrypto = (struct vhost_crypto *)dev->extern_data;
1535 if (unlikely(vcrypto == NULL)) {
1536 VC_LOG_ERR("Cannot find required data, is it initialized?");
1540 if (vcrypto->option == (uint8_t)option)
1543 if (!(rte_mempool_full(vcrypto->mbuf_pool)) ||
1544 !(rte_mempool_full(vcrypto->wb_pool))) {
1545 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1549 if (option == RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE) {
1552 snprintf(name, 127, "WB_POOL_VM_%u", (uint32_t)vid);
1553 vcrypto->wb_pool = rte_mempool_create(name,
1554 VHOST_CRYPTO_MBUF_POOL_SIZE,
1555 sizeof(struct vhost_crypto_writeback_data),
1556 128, 0, NULL, NULL, NULL, NULL,
1557 rte_socket_id(), 0);
1558 if (!vcrypto->wb_pool) {
1559 VC_LOG_ERR("Failed to creath mbuf pool");
1563 rte_mempool_free(vcrypto->wb_pool);
1564 vcrypto->wb_pool = NULL;
1567 vcrypto->option = (uint8_t)option;
1573 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1574 struct rte_crypto_op **ops, uint16_t nb_ops)
1576 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1577 struct virtio_net *dev = get_device(vid);
1578 struct vhost_crypto *vcrypto;
1579 struct vhost_virtqueue *vq;
1585 if (unlikely(dev == NULL)) {
1586 VC_LOG_ERR("Invalid vid %i", vid);
1590 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1591 VC_LOG_ERR("Invalid qid %u", qid);
1595 vcrypto = (struct vhost_crypto *)dev->extern_data;
1596 if (unlikely(vcrypto == NULL)) {
1597 VC_LOG_ERR("Cannot find required data, is it initialized?");
1601 vq = dev->virtqueue[qid];
1603 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1604 start_idx = vq->last_used_idx;
1605 count = avail_idx - start_idx;
1606 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1607 count = RTE_MIN(count, nb_ops);
1609 if (unlikely(count == 0))
1612 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1613 * we need only 1 mbuf as src and dst
1615 switch (vcrypto->option) {
1616 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
1617 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1618 (void **)mbufs, count * 2) < 0)) {
1619 VC_LOG_ERR("Insufficient memory");
1623 for (i = 0; i < count; i++) {
1624 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1625 uint16_t desc_idx = vq->avail->ring[used_idx];
1626 struct vring_desc *head = &vq->desc[desc_idx];
1627 struct rte_crypto_op *op = ops[i];
1629 op->sym->m_src = mbufs[i * 2];
1630 op->sym->m_dst = mbufs[i * 2 + 1];
1631 op->sym->m_src->data_off = 0;
1632 op->sym->m_dst->data_off = 0;
1634 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1635 op, head, desc_idx) < 0))
1639 if (unlikely(i < count))
1640 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1641 (void **)&mbufs[i * 2],
1646 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
1647 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool,
1648 (void **)mbufs, count) < 0)) {
1649 VC_LOG_ERR("Insufficient memory");
1653 for (i = 0; i < count; i++) {
1654 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1655 uint16_t desc_idx = vq->avail->ring[used_idx];
1656 struct vring_desc *head = &vq->desc[desc_idx];
1657 struct rte_crypto_op *op = ops[i];
1659 op->sym->m_src = mbufs[i];
1660 op->sym->m_dst = NULL;
1661 op->sym->m_src->data_off = 0;
1663 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq,
1664 op, head, desc_idx) < 0))
1668 if (unlikely(i < count))
1669 rte_mempool_put_bulk(vcrypto->mbuf_pool,
1677 vq->last_used_idx += i;
1683 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1684 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1686 struct rte_crypto_op **tmp_ops = ops;
1687 uint16_t count = 0, left = nb_ops;
1692 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1694 if (unlikely(count == 0))
1697 tmp_ops = &tmp_ops[count];
1700 callfds[idx++] = callfd;
1702 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1703 VC_LOG_ERR("Too many vqs");
1710 return nb_ops - left;