1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2017-2018 Intel Corporation
4 #include <rte_malloc.h>
8 #include <rte_cryptodev.h>
10 #include "rte_vhost_crypto.h"
12 #include "vhost_user.h"
13 #include "virtio_crypto.h"
15 #define INHDR_LEN (sizeof(struct virtio_crypto_inhdr))
16 #define IV_OFFSET (sizeof(struct rte_crypto_op) + \
17 sizeof(struct rte_crypto_sym_op))
19 #ifdef RTE_LIBRTE_VHOST_DEBUG
20 #define VC_LOG_ERR(fmt, args...) \
21 RTE_LOG(ERR, USER1, "[%s] %s() line %u: " fmt "\n", \
22 "Vhost-Crypto", __func__, __LINE__, ## args)
23 #define VC_LOG_INFO(fmt, args...) \
24 RTE_LOG(INFO, USER1, "[%s] %s() line %u: " fmt "\n", \
25 "Vhost-Crypto", __func__, __LINE__, ## args)
27 #define VC_LOG_DBG(fmt, args...) \
28 RTE_LOG(DEBUG, USER1, "[%s] %s() line %u: " fmt "\n", \
29 "Vhost-Crypto", __func__, __LINE__, ## args)
31 #define VC_LOG_ERR(fmt, args...) \
32 RTE_LOG(ERR, USER1, "[VHOST-Crypto]: " fmt "\n", ## args)
33 #define VC_LOG_INFO(fmt, args...) \
34 RTE_LOG(INFO, USER1, "[VHOST-Crypto]: " fmt "\n", ## args)
35 #define VC_LOG_DBG(fmt, args...)
38 #define VIRTIO_CRYPTO_FEATURES ((1 << VIRTIO_F_NOTIFY_ON_EMPTY) | \
39 (1 << VIRTIO_RING_F_INDIRECT_DESC) | \
40 (1 << VIRTIO_RING_F_EVENT_IDX) | \
41 (1 << VIRTIO_CRYPTO_SERVICE_CIPHER) | \
42 (1 << VIRTIO_CRYPTO_SERVICE_MAC) | \
43 (1 << VIRTIO_NET_F_CTRL_VQ))
45 #define IOVA_TO_VVA(t, r, a, l, p) \
46 ((t)(uintptr_t)vhost_iova_to_vva(r->dev, r->vq, a, l, p))
49 cipher_algo_transform(uint32_t virtio_cipher_algo)
53 switch (virtio_cipher_algo) {
54 case VIRTIO_CRYPTO_CIPHER_AES_CBC:
55 ret = RTE_CRYPTO_CIPHER_AES_CBC;
57 case VIRTIO_CRYPTO_CIPHER_AES_CTR:
58 ret = RTE_CRYPTO_CIPHER_AES_CTR;
60 case VIRTIO_CRYPTO_CIPHER_DES_ECB:
61 ret = -VIRTIO_CRYPTO_NOTSUPP;
63 case VIRTIO_CRYPTO_CIPHER_DES_CBC:
64 ret = RTE_CRYPTO_CIPHER_DES_CBC;
66 case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
67 ret = RTE_CRYPTO_CIPHER_3DES_ECB;
69 case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
70 ret = RTE_CRYPTO_CIPHER_3DES_CBC;
72 case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
73 ret = RTE_CRYPTO_CIPHER_3DES_CTR;
75 case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
76 ret = RTE_CRYPTO_CIPHER_KASUMI_F8;
78 case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
79 ret = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
81 case VIRTIO_CRYPTO_CIPHER_AES_F8:
82 ret = RTE_CRYPTO_CIPHER_AES_F8;
84 case VIRTIO_CRYPTO_CIPHER_AES_XTS:
85 ret = RTE_CRYPTO_CIPHER_AES_XTS;
87 case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
88 ret = RTE_CRYPTO_CIPHER_ZUC_EEA3;
91 ret = -VIRTIO_CRYPTO_BADMSG;
99 auth_algo_transform(uint32_t virtio_auth_algo)
103 switch (virtio_auth_algo) {
105 case VIRTIO_CRYPTO_NO_MAC:
106 ret = RTE_CRYPTO_AUTH_NULL;
108 case VIRTIO_CRYPTO_MAC_HMAC_MD5:
109 ret = RTE_CRYPTO_AUTH_MD5_HMAC;
111 case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
112 ret = RTE_CRYPTO_AUTH_SHA1_HMAC;
114 case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
115 ret = RTE_CRYPTO_AUTH_SHA224_HMAC;
117 case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
118 ret = RTE_CRYPTO_AUTH_SHA256_HMAC;
120 case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
121 ret = RTE_CRYPTO_AUTH_SHA384_HMAC;
123 case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
124 ret = RTE_CRYPTO_AUTH_SHA512_HMAC;
126 case VIRTIO_CRYPTO_MAC_CMAC_3DES:
127 ret = -VIRTIO_CRYPTO_NOTSUPP;
129 case VIRTIO_CRYPTO_MAC_CMAC_AES:
130 ret = RTE_CRYPTO_AUTH_AES_CMAC;
132 case VIRTIO_CRYPTO_MAC_KASUMI_F9:
133 ret = RTE_CRYPTO_AUTH_KASUMI_F9;
135 case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
136 ret = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
138 case VIRTIO_CRYPTO_MAC_GMAC_AES:
139 ret = RTE_CRYPTO_AUTH_AES_GMAC;
141 case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
142 ret = -VIRTIO_CRYPTO_NOTSUPP;
144 case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
145 ret = RTE_CRYPTO_AUTH_AES_CBC_MAC;
147 case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
148 ret = -VIRTIO_CRYPTO_NOTSUPP;
150 case VIRTIO_CRYPTO_MAC_XCBC_AES:
151 ret = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
154 ret = -VIRTIO_CRYPTO_BADMSG;
161 static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
166 case RTE_CRYPTO_CIPHER_3DES_CBC:
169 case RTE_CRYPTO_CIPHER_3DES_CTR:
172 case RTE_CRYPTO_CIPHER_3DES_ECB:
175 case RTE_CRYPTO_CIPHER_AES_CBC:
179 /* TODO: add common algos */
190 * vhost_crypto struct is used to maintain a number of virtio_cryptos and
191 * one DPDK crypto device that deals with all crypto workloads. It is declared
192 * here and defined in vhost_crypto.c
194 struct vhost_crypto {
195 /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
198 struct rte_hash *session_map;
199 struct rte_mempool *mbuf_pool;
200 struct rte_mempool *sess_pool;
202 /** DPDK cryptodev ID */
206 uint64_t last_session_id;
208 uint64_t cache_session_id;
209 struct rte_cryptodev_sym_session *cache_session;
210 /** socket id for the device */
213 struct virtio_net *dev;
216 } __rte_cache_aligned;
218 struct vhost_crypto_data_req {
219 struct vring_desc *head;
220 struct virtio_net *dev;
221 struct virtio_crypto_inhdr *inhdr;
222 struct vhost_virtqueue *vq;
223 struct vring_desc *wb_desc;
231 transform_cipher_param(struct rte_crypto_sym_xform *xform,
232 VhostUserCryptoSessionParam *param)
236 ret = cipher_algo_transform(param->cipher_algo);
237 if (unlikely(ret < 0))
240 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
241 xform->cipher.algo = (uint32_t)ret;
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 if (unlikely(ret < 0))
290 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
291 xform_cipher->cipher.algo = (uint32_t)ret;
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);
303 if (unlikely(ret < 0))
305 xform_auth->auth.algo = (uint32_t)ret;
306 xform_auth->auth.digest_length = param->digest_len;
307 xform_auth->auth.key.length = param->auth_key_len;
308 xform_auth->auth.key.data = param->auth_key_buf;
314 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
315 VhostUserCryptoSessionParam *sess_param)
317 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
318 struct rte_cryptodev_sym_session *session;
321 switch (sess_param->op_type) {
322 case VIRTIO_CRYPTO_SYM_OP_NONE:
323 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
324 ret = transform_cipher_param(&xform1, sess_param);
326 VC_LOG_ERR("Error transform session msg (%i)", ret);
327 sess_param->session_id = ret;
331 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
332 if (unlikely(sess_param->hash_mode !=
333 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
334 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
335 VC_LOG_ERR("Error transform session message (%i)",
336 -VIRTIO_CRYPTO_NOTSUPP);
340 xform1.next = &xform2;
342 ret = transform_chain_param(&xform1, sess_param);
344 VC_LOG_ERR("Error transform session message (%i)", ret);
345 sess_param->session_id = ret;
351 VC_LOG_ERR("Algorithm not yet supported");
352 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
356 session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
358 VC_LOG_ERR("Failed to create session");
359 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
363 if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
364 vcrypto->sess_pool) < 0) {
365 VC_LOG_ERR("Failed to initialize session");
366 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
370 /* insert hash to map */
371 if (rte_hash_add_key_data(vcrypto->session_map,
372 &vcrypto->last_session_id, session) < 0) {
373 VC_LOG_ERR("Failed to insert session to hash table");
375 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
376 VC_LOG_ERR("Failed to clear session");
378 if (rte_cryptodev_sym_session_free(session) < 0)
379 VC_LOG_ERR("Failed to free session");
381 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
385 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
386 vcrypto->last_session_id, vcrypto->dev->vid);
388 sess_param->session_id = vcrypto->last_session_id;
389 vcrypto->last_session_id++;
393 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
395 struct rte_cryptodev_sym_session *session;
396 uint64_t sess_id = session_id;
399 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
402 if (unlikely(ret < 0)) {
403 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
404 return -VIRTIO_CRYPTO_INVSESS;
407 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
408 VC_LOG_DBG("Failed to clear session");
409 return -VIRTIO_CRYPTO_ERR;
412 if (rte_cryptodev_sym_session_free(session) < 0) {
413 VC_LOG_DBG("Failed to free session");
414 return -VIRTIO_CRYPTO_ERR;
417 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
418 VC_LOG_DBG("Failed to delete session from hash table.");
419 return -VIRTIO_CRYPTO_ERR;
422 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
428 static enum vh_result
429 vhost_crypto_msg_post_handler(int vid, void *msg)
431 struct virtio_net *dev = get_device(vid);
432 struct vhost_crypto *vcrypto;
433 VhostUserMsg *vmsg = msg;
434 enum vh_result ret = VH_RESULT_OK;
437 VC_LOG_ERR("Invalid vid %i", vid);
438 return VH_RESULT_ERR;
441 vcrypto = dev->extern_data;
442 if (vcrypto == NULL) {
443 VC_LOG_ERR("Cannot find required data, is it initialized?");
444 return VH_RESULT_ERR;
447 if (vmsg->request.master == VHOST_USER_CRYPTO_CREATE_SESS) {
448 vhost_crypto_create_sess(vcrypto,
449 &vmsg->payload.crypto_session);
450 ret = VH_RESULT_REPLY;
451 } else if (vmsg->request.master == VHOST_USER_CRYPTO_CLOSE_SESS) {
452 if (vhost_crypto_close_sess(vcrypto, vmsg->payload.u64))
459 static __rte_always_inline struct vring_desc *
460 find_write_desc(struct vring_desc *head, struct vring_desc *desc)
462 if (desc->flags & VRING_DESC_F_WRITE)
465 while (desc->flags & VRING_DESC_F_NEXT) {
466 desc = &head[desc->next];
467 if (desc->flags & VRING_DESC_F_WRITE)
474 static struct virtio_crypto_inhdr *
475 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc)
478 struct virtio_crypto_inhdr *inhdr;
480 while (desc->flags & VRING_DESC_F_NEXT)
481 desc = &vc_req->head[desc->next];
484 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
485 &dlen, VHOST_ACCESS_WO);
486 if (unlikely(!inhdr || dlen != desc->len))
492 static __rte_always_inline int
493 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
496 struct vring_desc *desc = *cur_desc;
499 rte_prefetch0(&head[desc->next]);
502 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
503 desc = &head[desc->next];
504 rte_prefetch0(&head[desc->next]);
508 if (unlikely(left > 0)) {
509 VC_LOG_ERR("Incorrect virtio descriptor");
513 *cur_desc = &head[desc->next];
518 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
519 struct vring_desc **cur_desc, uint32_t size)
521 struct vring_desc *desc = *cur_desc;
522 uint64_t remain, addr, dlen, len;
524 uint8_t *data = dst_data;
528 rte_prefetch0(&vc_req->head[desc->next]);
529 to_copy = RTE_MIN(desc->len, (uint32_t)left);
531 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
533 if (unlikely(!src || !dlen)) {
534 VC_LOG_ERR("Failed to map descriptor");
538 rte_memcpy((uint8_t *)data, src, dlen);
541 if (unlikely(dlen < to_copy)) {
542 remain = to_copy - dlen;
543 addr = desc->addr + dlen;
547 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
549 if (unlikely(!src || !len)) {
550 VC_LOG_ERR("Failed to map descriptor");
554 rte_memcpy(data, src, len);
563 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
564 desc = &vc_req->head[desc->next];
565 rte_prefetch0(&vc_req->head[desc->next]);
566 to_copy = RTE_MIN(desc->len, (uint32_t)left);
568 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
570 if (unlikely(!src || !dlen)) {
571 VC_LOG_ERR("Failed to map descriptor");
575 rte_memcpy(data, src, dlen);
578 if (unlikely(dlen < to_copy)) {
579 remain = to_copy - dlen;
580 addr = desc->addr + dlen;
584 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
586 if (unlikely(!src || !len)) {
587 VC_LOG_ERR("Failed to map descriptor");
591 rte_memcpy(data, src, len);
601 if (unlikely(left > 0)) {
602 VC_LOG_ERR("Incorrect virtio descriptor");
606 *cur_desc = &vc_req->head[desc->next];
611 static __rte_always_inline void *
612 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc **cur_desc,
613 uint32_t size, uint8_t perm)
616 uint64_t dlen = (*cur_desc)->len;
618 data = IOVA_TO_VVA(void *, vc_req, (*cur_desc)->addr, &dlen, perm);
619 if (unlikely(!data || dlen != (*cur_desc)->len)) {
620 VC_LOG_ERR("Failed to map object");
624 if (unlikely(move_desc(vc_req->head, cur_desc, size) < 0))
631 write_back_data(struct rte_crypto_op *op, struct vhost_crypto_data_req *vc_req)
633 struct rte_mbuf *mbuf = op->sym->m_dst;
634 struct vring_desc *head = vc_req->head;
635 struct vring_desc *desc = vc_req->wb_desc;
636 int left = vc_req->wb_len;
638 uint8_t *src_data = mbuf->buf_addr, *dst;
641 rte_prefetch0(&head[desc->next]);
642 to_write = RTE_MIN(desc->len, (uint32_t)left);
644 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
646 if (unlikely(!dst || dlen != desc->len)) {
647 VC_LOG_ERR("Failed to map descriptor");
651 rte_memcpy(dst, src_data, to_write);
653 src_data += to_write;
655 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
656 desc = &head[desc->next];
657 rte_prefetch0(&head[desc->next]);
658 to_write = RTE_MIN(desc->len, (uint32_t)left);
660 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
662 if (unlikely(!dst || dlen != desc->len)) {
663 VC_LOG_ERR("Failed to map descriptor");
667 rte_memcpy(dst, src_data, to_write);
669 src_data += to_write;
672 if (unlikely(left < 0)) {
673 VC_LOG_ERR("Incorrect virtio descriptor");
681 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
682 struct vhost_crypto_data_req *vc_req,
683 struct virtio_crypto_cipher_data_req *cipher,
684 struct vring_desc *cur_desc)
686 struct vring_desc *desc = cur_desc;
687 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
688 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
693 if (unlikely(copy_data(iv_data, vc_req, &desc,
694 cipher->para.iv_len) < 0)) {
695 ret = VIRTIO_CRYPTO_BADMSG;
699 m_src->data_len = cipher->para.src_data_len;
701 switch (vcrypto->option) {
702 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
703 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
704 cipher->para.src_data_len);
705 m_src->buf_addr = get_data_ptr(vc_req, &desc,
706 cipher->para.src_data_len, VHOST_ACCESS_RO);
707 if (unlikely(m_src->buf_iova == 0 ||
708 m_src->buf_addr == NULL)) {
709 VC_LOG_ERR("zero_copy may fail due to cross page data");
710 ret = VIRTIO_CRYPTO_ERR;
714 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
715 if (unlikely(cipher->para.src_data_len >
716 RTE_MBUF_DEFAULT_BUF_SIZE)) {
717 VC_LOG_ERR("Not enough space to do data copy");
718 ret = VIRTIO_CRYPTO_ERR;
721 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
722 vc_req, &desc, cipher->para.src_data_len)
724 ret = VIRTIO_CRYPTO_BADMSG;
729 ret = VIRTIO_CRYPTO_BADMSG;
734 desc = find_write_desc(vc_req->head, desc);
735 if (unlikely(!desc)) {
736 VC_LOG_ERR("Cannot find write location");
737 ret = VIRTIO_CRYPTO_BADMSG;
741 switch (vcrypto->option) {
742 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
743 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
744 desc->addr, cipher->para.dst_data_len);
745 m_dst->buf_addr = get_data_ptr(vc_req, &desc,
746 cipher->para.dst_data_len, VHOST_ACCESS_RW);
747 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
748 VC_LOG_ERR("zero_copy may fail due to cross page data");
749 ret = VIRTIO_CRYPTO_ERR;
753 m_dst->data_len = cipher->para.dst_data_len;
755 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
756 vc_req->wb_desc = desc;
757 vc_req->wb_len = cipher->para.dst_data_len;
758 if (unlikely(move_desc(vc_req->head, &desc,
759 vc_req->wb_len) < 0)) {
760 ret = VIRTIO_CRYPTO_ERR;
765 ret = VIRTIO_CRYPTO_BADMSG;
770 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
771 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
773 op->sym->cipher.data.offset = 0;
774 op->sym->cipher.data.length = cipher->para.src_data_len;
776 vc_req->inhdr = get_data_ptr(vc_req, &desc, INHDR_LEN, VHOST_ACCESS_WO);
777 if (unlikely(vc_req->inhdr == NULL)) {
778 ret = VIRTIO_CRYPTO_BADMSG;
782 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
783 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
788 vc_req->len = INHDR_LEN;
793 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
794 struct vhost_crypto_data_req *vc_req,
795 struct virtio_crypto_alg_chain_data_req *chain,
796 struct vring_desc *cur_desc)
798 struct vring_desc *desc = cur_desc;
799 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
800 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
801 uint32_t digest_offset;
807 if (unlikely(copy_data(iv_data, vc_req, &desc,
808 chain->para.iv_len) < 0)) {
809 ret = VIRTIO_CRYPTO_BADMSG;
813 m_src->data_len = chain->para.src_data_len;
814 m_dst->data_len = chain->para.dst_data_len;
816 switch (vcrypto->option) {
817 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
818 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
819 chain->para.src_data_len);
820 m_src->buf_addr = get_data_ptr(vc_req, &desc,
821 chain->para.src_data_len, VHOST_ACCESS_RO);
822 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
823 VC_LOG_ERR("zero_copy may fail due to cross page data");
824 ret = VIRTIO_CRYPTO_ERR;
828 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
829 if (unlikely(chain->para.src_data_len >
830 RTE_MBUF_DEFAULT_BUF_SIZE)) {
831 VC_LOG_ERR("Not enough space to do data copy");
832 ret = VIRTIO_CRYPTO_ERR;
835 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
836 vc_req, &desc, chain->para.src_data_len)) < 0) {
837 ret = VIRTIO_CRYPTO_BADMSG;
842 ret = VIRTIO_CRYPTO_BADMSG;
847 desc = find_write_desc(vc_req->head, desc);
848 if (unlikely(!desc)) {
849 VC_LOG_ERR("Cannot find write location");
850 ret = VIRTIO_CRYPTO_BADMSG;
854 switch (vcrypto->option) {
855 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
856 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
857 desc->addr, chain->para.dst_data_len);
858 m_dst->buf_addr = get_data_ptr(vc_req, &desc,
859 chain->para.dst_data_len, VHOST_ACCESS_RW);
860 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
861 VC_LOG_ERR("zero_copy may fail due to cross page data");
862 ret = VIRTIO_CRYPTO_ERR;
866 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
867 desc->addr, chain->para.hash_result_len);
868 op->sym->auth.digest.data = get_data_ptr(vc_req, &desc,
869 chain->para.hash_result_len, VHOST_ACCESS_RW);
870 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
871 VC_LOG_ERR("zero_copy may fail due to cross page data");
872 ret = VIRTIO_CRYPTO_ERR;
876 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
877 digest_offset = m_dst->data_len;
878 digest_addr = rte_pktmbuf_mtod_offset(m_dst, void *,
881 vc_req->wb_desc = desc;
882 vc_req->wb_len = m_dst->data_len + chain->para.hash_result_len;
884 if (unlikely(move_desc(vc_req->head, &desc,
885 chain->para.dst_data_len) < 0)) {
886 ret = VIRTIO_CRYPTO_BADMSG;
890 if (unlikely(copy_data(digest_addr, vc_req, &desc,
891 chain->para.hash_result_len)) < 0) {
892 ret = VIRTIO_CRYPTO_BADMSG;
896 op->sym->auth.digest.data = digest_addr;
897 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_dst,
901 ret = VIRTIO_CRYPTO_BADMSG;
906 vc_req->inhdr = get_data_ptr(vc_req, &desc, INHDR_LEN, VHOST_ACCESS_WO);
907 if (unlikely(vc_req->inhdr == NULL)) {
908 ret = VIRTIO_CRYPTO_BADMSG;
912 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
914 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
915 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
917 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
918 op->sym->cipher.data.length = chain->para.src_data_len -
919 chain->para.cipher_start_src_offset;
921 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
922 op->sym->auth.data.length = chain->para.len_to_hash;
924 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
929 vc_req->len = INHDR_LEN;
934 * Process on descriptor
936 static __rte_always_inline int
937 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
938 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
939 struct vring_desc *head, uint16_t desc_idx)
941 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
942 struct rte_cryptodev_sym_session *session;
943 struct virtio_crypto_op_data_req *req, tmp_req;
944 struct virtio_crypto_inhdr *inhdr;
945 struct vring_desc *desc = NULL;
950 vc_req->desc_idx = desc_idx;
951 vc_req->dev = vcrypto->dev;
954 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
956 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
957 &dlen, VHOST_ACCESS_RO);
958 if (unlikely(!desc || dlen != head->len))
967 vc_req->zero_copy = vcrypto->option;
969 req = get_data_ptr(vc_req, &desc, sizeof(*req), VHOST_ACCESS_RO);
970 if (unlikely(req == NULL)) {
971 switch (vcrypto->option) {
972 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
973 err = VIRTIO_CRYPTO_BADMSG;
974 VC_LOG_ERR("Invalid descriptor");
976 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
978 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req))
980 err = VIRTIO_CRYPTO_BADMSG;
981 VC_LOG_ERR("Invalid descriptor");
986 err = VIRTIO_CRYPTO_ERR;
987 VC_LOG_ERR("Invalid option");
992 switch (req->header.opcode) {
993 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
994 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
995 session_id = req->header.session_id;
997 /* one branch to avoid unnecessary table lookup */
998 if (vcrypto->cache_session_id != session_id) {
999 err = rte_hash_lookup_data(vcrypto->session_map,
1000 &session_id, (void **)&session);
1001 if (unlikely(err < 0)) {
1002 err = VIRTIO_CRYPTO_ERR;
1003 VC_LOG_ERR("Failed to find session %"PRIu64,
1008 vcrypto->cache_session = session;
1009 vcrypto->cache_session_id = session_id;
1012 session = vcrypto->cache_session;
1014 err = rte_crypto_op_attach_sym_session(op, session);
1015 if (unlikely(err < 0)) {
1016 err = VIRTIO_CRYPTO_ERR;
1017 VC_LOG_ERR("Failed to attach session to op");
1021 switch (req->u.sym_req.op_type) {
1022 case VIRTIO_CRYPTO_SYM_OP_NONE:
1023 err = VIRTIO_CRYPTO_NOTSUPP;
1025 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1026 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1027 &req->u.sym_req.u.cipher, desc);
1029 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1030 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1031 &req->u.sym_req.u.chain, desc);
1034 if (unlikely(err != 0)) {
1035 VC_LOG_ERR("Failed to process sym request");
1040 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1041 req->header.opcode);
1049 inhdr = reach_inhdr(vc_req, desc);
1050 if (likely(inhdr != NULL))
1051 inhdr->status = (uint8_t)err;
1056 static __rte_always_inline struct vhost_virtqueue *
1057 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1058 struct vhost_virtqueue *old_vq)
1060 struct rte_mbuf *m_src = op->sym->m_src;
1061 struct rte_mbuf *m_dst = op->sym->m_dst;
1062 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1066 if (unlikely(!vc_req)) {
1067 VC_LOG_ERR("Failed to retrieve vc_req");
1071 if (old_vq && (vc_req->vq != old_vq))
1074 desc_idx = vc_req->desc_idx;
1076 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1077 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1079 if (vc_req->zero_copy == 0) {
1080 ret = write_back_data(op, vc_req);
1081 if (unlikely(ret != 0))
1082 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1086 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1087 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1089 rte_mempool_put(m_dst->pool, (void *)m_dst);
1090 rte_mempool_put(m_src->pool, (void *)m_src);
1095 static __rte_always_inline uint16_t
1096 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1097 uint16_t nb_ops, int *callfd)
1099 uint16_t processed = 1;
1100 struct vhost_virtqueue *vq, *tmp_vq;
1102 if (unlikely(nb_ops == 0))
1105 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1106 if (unlikely(vq == NULL))
1110 while ((processed < nb_ops)) {
1111 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1114 if (unlikely(vq != tmp_vq))
1120 *callfd = vq->callfd;
1122 *(volatile uint16_t *)&vq->used->idx += processed;
1127 int __rte_experimental
1128 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1129 struct rte_mempool *sess_pool, int socket_id)
1131 struct virtio_net *dev = get_device(vid);
1132 struct rte_hash_parameters params = {0};
1133 struct vhost_crypto *vcrypto;
1138 VC_LOG_ERR("Invalid vid %i", vid);
1142 ret = rte_vhost_driver_set_features(dev->ifname,
1143 VIRTIO_CRYPTO_FEATURES);
1145 VC_LOG_ERR("Error setting features");
1149 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1150 RTE_CACHE_LINE_SIZE, socket_id);
1152 VC_LOG_ERR("Insufficient memory");
1156 vcrypto->sess_pool = sess_pool;
1157 vcrypto->cid = cryptodev_id;
1158 vcrypto->cache_session_id = UINT64_MAX;
1159 vcrypto->last_session_id = 1;
1161 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1163 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1165 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1166 params.hash_func = rte_jhash;
1167 params.key_len = sizeof(uint64_t);
1168 params.socket_id = socket_id;
1169 vcrypto->session_map = rte_hash_create(¶ms);
1170 if (!vcrypto->session_map) {
1171 VC_LOG_ERR("Failed to creath session map");
1176 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1177 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1178 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1179 sizeof(struct vhost_crypto_data_req),
1180 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1182 if (!vcrypto->mbuf_pool) {
1183 VC_LOG_ERR("Failed to creath mbuf pool");
1188 dev->extern_data = vcrypto;
1189 dev->extern_ops.pre_msg_handle = NULL;
1190 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1195 if (vcrypto->session_map)
1196 rte_hash_free(vcrypto->session_map);
1197 if (vcrypto->mbuf_pool)
1198 rte_mempool_free(vcrypto->mbuf_pool);
1205 int __rte_experimental
1206 rte_vhost_crypto_free(int vid)
1208 struct virtio_net *dev = get_device(vid);
1209 struct vhost_crypto *vcrypto;
1211 if (unlikely(dev == NULL)) {
1212 VC_LOG_ERR("Invalid vid %i", vid);
1216 vcrypto = dev->extern_data;
1217 if (unlikely(vcrypto == NULL)) {
1218 VC_LOG_ERR("Cannot find required data, is it initialized?");
1222 rte_hash_free(vcrypto->session_map);
1223 rte_mempool_free(vcrypto->mbuf_pool);
1226 dev->extern_data = NULL;
1227 dev->extern_ops.pre_msg_handle = NULL;
1228 dev->extern_ops.post_msg_handle = NULL;
1233 int __rte_experimental
1234 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1236 struct virtio_net *dev = get_device(vid);
1237 struct vhost_crypto *vcrypto;
1239 if (unlikely(dev == NULL)) {
1240 VC_LOG_ERR("Invalid vid %i", vid);
1244 if (unlikely((uint32_t)option >=
1245 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1246 VC_LOG_ERR("Invalid option %i", option);
1250 vcrypto = (struct vhost_crypto *)dev->extern_data;
1251 if (unlikely(vcrypto == NULL)) {
1252 VC_LOG_ERR("Cannot find required data, is it initialized?");
1256 if (vcrypto->option == (uint8_t)option)
1259 if (!(rte_mempool_full(vcrypto->mbuf_pool))) {
1260 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1264 vcrypto->option = (uint8_t)option;
1269 uint16_t __rte_experimental
1270 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1271 struct rte_crypto_op **ops, uint16_t nb_ops)
1273 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1274 struct virtio_net *dev = get_device(vid);
1275 struct vhost_crypto *vcrypto;
1276 struct vhost_virtqueue *vq;
1283 if (unlikely(dev == NULL)) {
1284 VC_LOG_ERR("Invalid vid %i", vid);
1288 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1289 VC_LOG_ERR("Invalid qid %u", qid);
1293 vcrypto = (struct vhost_crypto *)dev->extern_data;
1294 if (unlikely(vcrypto == NULL)) {
1295 VC_LOG_ERR("Cannot find required data, is it initialized?");
1299 vq = dev->virtqueue[qid];
1301 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1302 start_idx = vq->last_used_idx;
1303 count = avail_idx - start_idx;
1304 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1305 count = RTE_MIN(count, nb_ops);
1307 if (unlikely(count == 0))
1310 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1311 * we need only 1 mbuf as src and dst
1313 required = count * 2;
1314 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, (void **)mbufs,
1316 VC_LOG_ERR("Insufficient memory");
1320 for (i = 0; i < count; i++) {
1321 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1322 uint16_t desc_idx = vq->avail->ring[used_idx];
1323 struct vring_desc *head = &vq->desc[desc_idx];
1324 struct rte_crypto_op *op = ops[i];
1326 op->sym->m_src = mbufs[i * 2];
1327 op->sym->m_dst = mbufs[i * 2 + 1];
1328 op->sym->m_src->data_off = 0;
1329 op->sym->m_dst->data_off = 0;
1331 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, op, head,
1336 vq->last_used_idx += i;
1341 uint16_t __rte_experimental
1342 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1343 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1345 struct rte_crypto_op **tmp_ops = ops;
1346 uint16_t count = 0, left = nb_ops;
1351 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1353 if (unlikely(count == 0))
1356 tmp_ops = &tmp_ops[count];
1359 callfds[idx++] = callfd;
1361 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1362 VC_LOG_ERR("Too many vqs");
1369 return nb_ops - left;