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,
429 vhost_crypto_msg_post_handler(int vid, void *msg, uint32_t *require_reply)
431 struct virtio_net *dev = get_device(vid);
432 struct vhost_crypto *vcrypto;
433 VhostUserMsg *vmsg = msg;
436 if (dev == NULL || require_reply == NULL) {
437 VC_LOG_ERR("Invalid vid %i", vid);
441 vcrypto = dev->extern_data;
442 if (vcrypto == NULL) {
443 VC_LOG_ERR("Cannot find required data, is it initialized?");
449 if (vmsg->request.master == VHOST_USER_CRYPTO_CREATE_SESS) {
450 vhost_crypto_create_sess(vcrypto,
451 &vmsg->payload.crypto_session);
453 } else if (vmsg->request.master == VHOST_USER_CRYPTO_CLOSE_SESS)
454 ret = vhost_crypto_close_sess(vcrypto, vmsg->payload.u64);
461 static __rte_always_inline struct vring_desc *
462 find_write_desc(struct vring_desc *head, struct vring_desc *desc)
464 if (desc->flags & VRING_DESC_F_WRITE)
467 while (desc->flags & VRING_DESC_F_NEXT) {
468 desc = &head[desc->next];
469 if (desc->flags & VRING_DESC_F_WRITE)
476 static struct virtio_crypto_inhdr *
477 reach_inhdr(struct vhost_crypto_data_req *vc_req, struct vring_desc *desc)
480 struct virtio_crypto_inhdr *inhdr;
482 while (desc->flags & VRING_DESC_F_NEXT)
483 desc = &vc_req->head[desc->next];
486 inhdr = IOVA_TO_VVA(struct virtio_crypto_inhdr *, vc_req, desc->addr,
487 &dlen, VHOST_ACCESS_WO);
488 if (unlikely(!inhdr || dlen != desc->len))
494 static __rte_always_inline int
495 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
498 struct vring_desc *desc = *cur_desc;
501 rte_prefetch0(&head[desc->next]);
504 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
505 desc = &head[desc->next];
506 rte_prefetch0(&head[desc->next]);
510 if (unlikely(left > 0)) {
511 VC_LOG_ERR("Incorrect virtio descriptor");
515 *cur_desc = &head[desc->next];
520 copy_data(void *dst_data, struct vhost_crypto_data_req *vc_req,
521 struct vring_desc **cur_desc, uint32_t size)
523 struct vring_desc *desc = *cur_desc;
524 uint64_t remain, addr, dlen, len;
526 uint8_t *data = dst_data;
530 rte_prefetch0(&vc_req->head[desc->next]);
531 to_copy = RTE_MIN(desc->len, (uint32_t)left);
533 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
535 if (unlikely(!src || !dlen)) {
536 VC_LOG_ERR("Failed to map descriptor");
540 rte_memcpy((uint8_t *)data, src, dlen);
543 if (unlikely(dlen < to_copy)) {
544 remain = to_copy - dlen;
545 addr = desc->addr + dlen;
549 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
551 if (unlikely(!src || !len)) {
552 VC_LOG_ERR("Failed to map descriptor");
556 rte_memcpy(data, src, len);
565 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
566 desc = &vc_req->head[desc->next];
567 rte_prefetch0(&vc_req->head[desc->next]);
568 to_copy = RTE_MIN(desc->len, (uint32_t)left);
570 src = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
572 if (unlikely(!src || !dlen)) {
573 VC_LOG_ERR("Failed to map descriptor");
577 rte_memcpy(data, src, dlen);
580 if (unlikely(dlen < to_copy)) {
581 remain = to_copy - dlen;
582 addr = desc->addr + dlen;
586 src = IOVA_TO_VVA(uint8_t *, vc_req, addr, &len,
588 if (unlikely(!src || !len)) {
589 VC_LOG_ERR("Failed to map descriptor");
593 rte_memcpy(data, src, len);
603 if (unlikely(left > 0)) {
604 VC_LOG_ERR("Incorrect virtio descriptor");
608 *cur_desc = &vc_req->head[desc->next];
613 static __rte_always_inline void *
614 get_data_ptr(struct vhost_crypto_data_req *vc_req, struct vring_desc **cur_desc,
615 uint32_t size, uint8_t perm)
618 uint64_t dlen = (*cur_desc)->len;
620 data = IOVA_TO_VVA(void *, vc_req, (*cur_desc)->addr, &dlen, perm);
621 if (unlikely(!data || dlen != (*cur_desc)->len)) {
622 VC_LOG_ERR("Failed to map object");
626 if (unlikely(move_desc(vc_req->head, cur_desc, size) < 0))
633 write_back_data(struct rte_crypto_op *op, struct vhost_crypto_data_req *vc_req)
635 struct rte_mbuf *mbuf = op->sym->m_dst;
636 struct vring_desc *head = vc_req->head;
637 struct vring_desc *desc = vc_req->wb_desc;
638 int left = vc_req->wb_len;
640 uint8_t *src_data = mbuf->buf_addr, *dst;
643 rte_prefetch0(&head[desc->next]);
644 to_write = RTE_MIN(desc->len, (uint32_t)left);
646 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
648 if (unlikely(!dst || dlen != desc->len)) {
649 VC_LOG_ERR("Failed to map descriptor");
653 rte_memcpy(dst, src_data, to_write);
655 src_data += to_write;
657 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
658 desc = &head[desc->next];
659 rte_prefetch0(&head[desc->next]);
660 to_write = RTE_MIN(desc->len, (uint32_t)left);
662 dst = IOVA_TO_VVA(uint8_t *, vc_req, desc->addr, &dlen,
664 if (unlikely(!dst || dlen != desc->len)) {
665 VC_LOG_ERR("Failed to map descriptor");
669 rte_memcpy(dst, src_data, to_write);
671 src_data += to_write;
674 if (unlikely(left < 0)) {
675 VC_LOG_ERR("Incorrect virtio descriptor");
683 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
684 struct vhost_crypto_data_req *vc_req,
685 struct virtio_crypto_cipher_data_req *cipher,
686 struct vring_desc *cur_desc)
688 struct vring_desc *desc = cur_desc;
689 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
690 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
695 if (unlikely(copy_data(iv_data, vc_req, &desc,
696 cipher->para.iv_len) < 0)) {
697 ret = VIRTIO_CRYPTO_BADMSG;
701 m_src->data_len = cipher->para.src_data_len;
703 switch (vcrypto->option) {
704 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
705 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
706 cipher->para.src_data_len);
707 m_src->buf_addr = get_data_ptr(vc_req, &desc,
708 cipher->para.src_data_len, VHOST_ACCESS_RO);
709 if (unlikely(m_src->buf_iova == 0 ||
710 m_src->buf_addr == NULL)) {
711 VC_LOG_ERR("zero_copy may fail due to cross page data");
712 ret = VIRTIO_CRYPTO_ERR;
716 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
717 if (unlikely(cipher->para.src_data_len >
718 RTE_MBUF_DEFAULT_BUF_SIZE)) {
719 VC_LOG_ERR("Not enough space to do data copy");
720 ret = VIRTIO_CRYPTO_ERR;
723 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
724 vc_req, &desc, cipher->para.src_data_len)
726 ret = VIRTIO_CRYPTO_BADMSG;
731 ret = VIRTIO_CRYPTO_BADMSG;
736 desc = find_write_desc(vc_req->head, desc);
737 if (unlikely(!desc)) {
738 VC_LOG_ERR("Cannot find write location");
739 ret = VIRTIO_CRYPTO_BADMSG;
743 switch (vcrypto->option) {
744 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
745 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
746 desc->addr, cipher->para.dst_data_len);
747 m_dst->buf_addr = get_data_ptr(vc_req, &desc,
748 cipher->para.dst_data_len, VHOST_ACCESS_RW);
749 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
750 VC_LOG_ERR("zero_copy may fail due to cross page data");
751 ret = VIRTIO_CRYPTO_ERR;
755 m_dst->data_len = cipher->para.dst_data_len;
757 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
758 vc_req->wb_desc = desc;
759 vc_req->wb_len = cipher->para.dst_data_len;
760 if (unlikely(move_desc(vc_req->head, &desc,
761 vc_req->wb_len) < 0)) {
762 ret = VIRTIO_CRYPTO_ERR;
767 ret = VIRTIO_CRYPTO_BADMSG;
772 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
773 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
775 op->sym->cipher.data.offset = 0;
776 op->sym->cipher.data.length = cipher->para.src_data_len;
778 vc_req->inhdr = get_data_ptr(vc_req, &desc, INHDR_LEN, VHOST_ACCESS_WO);
779 if (unlikely(vc_req->inhdr == NULL)) {
780 ret = VIRTIO_CRYPTO_BADMSG;
784 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
785 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
790 vc_req->len = INHDR_LEN;
795 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
796 struct vhost_crypto_data_req *vc_req,
797 struct virtio_crypto_alg_chain_data_req *chain,
798 struct vring_desc *cur_desc)
800 struct vring_desc *desc = cur_desc;
801 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
802 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
803 uint32_t digest_offset;
809 if (unlikely(copy_data(iv_data, vc_req, &desc,
810 chain->para.iv_len) < 0)) {
811 ret = VIRTIO_CRYPTO_BADMSG;
815 m_src->data_len = chain->para.src_data_len;
816 m_dst->data_len = chain->para.dst_data_len;
818 switch (vcrypto->option) {
819 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
820 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
821 chain->para.src_data_len);
822 m_src->buf_addr = get_data_ptr(vc_req, &desc,
823 chain->para.src_data_len, VHOST_ACCESS_RO);
824 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
825 VC_LOG_ERR("zero_copy may fail due to cross page data");
826 ret = VIRTIO_CRYPTO_ERR;
830 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
831 if (unlikely(chain->para.src_data_len >
832 RTE_MBUF_DEFAULT_BUF_SIZE)) {
833 VC_LOG_ERR("Not enough space to do data copy");
834 ret = VIRTIO_CRYPTO_ERR;
837 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *),
838 vc_req, &desc, chain->para.src_data_len)) < 0) {
839 ret = VIRTIO_CRYPTO_BADMSG;
844 ret = VIRTIO_CRYPTO_BADMSG;
849 desc = find_write_desc(vc_req->head, desc);
850 if (unlikely(!desc)) {
851 VC_LOG_ERR("Cannot find write location");
852 ret = VIRTIO_CRYPTO_BADMSG;
856 switch (vcrypto->option) {
857 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
858 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
859 desc->addr, chain->para.dst_data_len);
860 m_dst->buf_addr = get_data_ptr(vc_req, &desc,
861 chain->para.dst_data_len, VHOST_ACCESS_RW);
862 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
863 VC_LOG_ERR("zero_copy may fail due to cross page data");
864 ret = VIRTIO_CRYPTO_ERR;
868 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
869 desc->addr, chain->para.hash_result_len);
870 op->sym->auth.digest.data = get_data_ptr(vc_req, &desc,
871 chain->para.hash_result_len, VHOST_ACCESS_RW);
872 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
873 VC_LOG_ERR("zero_copy may fail due to cross page data");
874 ret = VIRTIO_CRYPTO_ERR;
878 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
879 digest_offset = m_dst->data_len;
880 digest_addr = rte_pktmbuf_mtod_offset(m_dst, void *,
883 vc_req->wb_desc = desc;
884 vc_req->wb_len = m_dst->data_len + chain->para.hash_result_len;
886 if (unlikely(move_desc(vc_req->head, &desc,
887 chain->para.dst_data_len) < 0)) {
888 ret = VIRTIO_CRYPTO_BADMSG;
892 if (unlikely(copy_data(digest_addr, vc_req, &desc,
893 chain->para.hash_result_len)) < 0) {
894 ret = VIRTIO_CRYPTO_BADMSG;
898 op->sym->auth.digest.data = digest_addr;
899 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_dst,
903 ret = VIRTIO_CRYPTO_BADMSG;
908 vc_req->inhdr = get_data_ptr(vc_req, &desc, INHDR_LEN, VHOST_ACCESS_WO);
909 if (unlikely(vc_req->inhdr == NULL)) {
910 ret = VIRTIO_CRYPTO_BADMSG;
914 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
916 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
917 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
919 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
920 op->sym->cipher.data.length = chain->para.src_data_len -
921 chain->para.cipher_start_src_offset;
923 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
924 op->sym->auth.data.length = chain->para.len_to_hash;
926 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
931 vc_req->len = INHDR_LEN;
936 * Process on descriptor
938 static __rte_always_inline int
939 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
940 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
941 struct vring_desc *head, uint16_t desc_idx)
943 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(op->sym->m_src);
944 struct rte_cryptodev_sym_session *session;
945 struct virtio_crypto_op_data_req *req, tmp_req;
946 struct virtio_crypto_inhdr *inhdr;
947 struct vring_desc *desc = NULL;
952 vc_req->desc_idx = desc_idx;
953 vc_req->dev = vcrypto->dev;
956 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
958 desc = IOVA_TO_VVA(struct vring_desc *, vc_req, head->addr,
959 &dlen, VHOST_ACCESS_RO);
960 if (unlikely(!desc || dlen != head->len))
969 vc_req->zero_copy = vcrypto->option;
971 req = get_data_ptr(vc_req, &desc, sizeof(*req), VHOST_ACCESS_RO);
972 if (unlikely(req == NULL)) {
973 switch (vcrypto->option) {
974 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
975 err = VIRTIO_CRYPTO_BADMSG;
976 VC_LOG_ERR("Invalid descriptor");
978 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
980 if (unlikely(copy_data(req, vc_req, &desc, sizeof(*req))
982 err = VIRTIO_CRYPTO_BADMSG;
983 VC_LOG_ERR("Invalid descriptor");
988 err = VIRTIO_CRYPTO_ERR;
989 VC_LOG_ERR("Invalid option");
994 switch (req->header.opcode) {
995 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
996 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
997 session_id = req->header.session_id;
999 /* one branch to avoid unnecessary table lookup */
1000 if (vcrypto->cache_session_id != session_id) {
1001 err = rte_hash_lookup_data(vcrypto->session_map,
1002 &session_id, (void **)&session);
1003 if (unlikely(err < 0)) {
1004 err = VIRTIO_CRYPTO_ERR;
1005 VC_LOG_ERR("Failed to find session %"PRIu64,
1010 vcrypto->cache_session = session;
1011 vcrypto->cache_session_id = session_id;
1014 session = vcrypto->cache_session;
1016 err = rte_crypto_op_attach_sym_session(op, session);
1017 if (unlikely(err < 0)) {
1018 err = VIRTIO_CRYPTO_ERR;
1019 VC_LOG_ERR("Failed to attach session to op");
1023 switch (req->u.sym_req.op_type) {
1024 case VIRTIO_CRYPTO_SYM_OP_NONE:
1025 err = VIRTIO_CRYPTO_NOTSUPP;
1027 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
1028 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
1029 &req->u.sym_req.u.cipher, desc);
1031 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
1032 err = prepare_sym_chain_op(vcrypto, op, vc_req,
1033 &req->u.sym_req.u.chain, desc);
1036 if (unlikely(err != 0)) {
1037 VC_LOG_ERR("Failed to process sym request");
1042 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
1043 req->header.opcode);
1051 inhdr = reach_inhdr(vc_req, desc);
1052 if (likely(inhdr != NULL))
1053 inhdr->status = (uint8_t)err;
1058 static __rte_always_inline struct vhost_virtqueue *
1059 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
1060 struct vhost_virtqueue *old_vq)
1062 struct rte_mbuf *m_src = op->sym->m_src;
1063 struct rte_mbuf *m_dst = op->sym->m_dst;
1064 struct vhost_crypto_data_req *vc_req = rte_mbuf_to_priv(m_src);
1068 if (unlikely(!vc_req)) {
1069 VC_LOG_ERR("Failed to retrieve vc_req");
1073 if (old_vq && (vc_req->vq != old_vq))
1076 desc_idx = vc_req->desc_idx;
1078 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
1079 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1081 if (vc_req->zero_copy == 0) {
1082 ret = write_back_data(op, vc_req);
1083 if (unlikely(ret != 0))
1084 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
1088 vc_req->vq->used->ring[desc_idx].id = desc_idx;
1089 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1091 rte_mempool_put(m_dst->pool, (void *)m_dst);
1092 rte_mempool_put(m_src->pool, (void *)m_src);
1097 static __rte_always_inline uint16_t
1098 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1099 uint16_t nb_ops, int *callfd)
1101 uint16_t processed = 1;
1102 struct vhost_virtqueue *vq, *tmp_vq;
1104 if (unlikely(nb_ops == 0))
1107 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1108 if (unlikely(vq == NULL))
1112 while ((processed < nb_ops)) {
1113 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1116 if (unlikely(vq != tmp_vq))
1122 *callfd = vq->callfd;
1124 *(volatile uint16_t *)&vq->used->idx += processed;
1129 int __rte_experimental
1130 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1131 struct rte_mempool *sess_pool, int socket_id)
1133 struct virtio_net *dev = get_device(vid);
1134 struct rte_hash_parameters params = {0};
1135 struct vhost_crypto *vcrypto;
1140 VC_LOG_ERR("Invalid vid %i", vid);
1144 ret = rte_vhost_driver_set_features(dev->ifname,
1145 VIRTIO_CRYPTO_FEATURES);
1147 VC_LOG_ERR("Error setting features");
1151 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1152 RTE_CACHE_LINE_SIZE, socket_id);
1154 VC_LOG_ERR("Insufficient memory");
1158 vcrypto->sess_pool = sess_pool;
1159 vcrypto->cid = cryptodev_id;
1160 vcrypto->cache_session_id = UINT64_MAX;
1161 vcrypto->last_session_id = 1;
1163 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1165 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1167 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1168 params.hash_func = rte_jhash;
1169 params.key_len = sizeof(uint64_t);
1170 params.socket_id = socket_id;
1171 vcrypto->session_map = rte_hash_create(¶ms);
1172 if (!vcrypto->session_map) {
1173 VC_LOG_ERR("Failed to creath session map");
1178 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1179 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1180 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1181 sizeof(struct vhost_crypto_data_req),
1182 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1184 if (!vcrypto->mbuf_pool) {
1185 VC_LOG_ERR("Failed to creath mbuf pool");
1190 dev->extern_data = vcrypto;
1191 dev->extern_ops.pre_msg_handle = NULL;
1192 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1197 if (vcrypto->session_map)
1198 rte_hash_free(vcrypto->session_map);
1199 if (vcrypto->mbuf_pool)
1200 rte_mempool_free(vcrypto->mbuf_pool);
1207 int __rte_experimental
1208 rte_vhost_crypto_free(int vid)
1210 struct virtio_net *dev = get_device(vid);
1211 struct vhost_crypto *vcrypto;
1213 if (unlikely(dev == NULL)) {
1214 VC_LOG_ERR("Invalid vid %i", vid);
1218 vcrypto = dev->extern_data;
1219 if (unlikely(vcrypto == NULL)) {
1220 VC_LOG_ERR("Cannot find required data, is it initialized?");
1224 rte_hash_free(vcrypto->session_map);
1225 rte_mempool_free(vcrypto->mbuf_pool);
1228 dev->extern_data = NULL;
1229 dev->extern_ops.pre_msg_handle = NULL;
1230 dev->extern_ops.post_msg_handle = NULL;
1235 int __rte_experimental
1236 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1238 struct virtio_net *dev = get_device(vid);
1239 struct vhost_crypto *vcrypto;
1241 if (unlikely(dev == NULL)) {
1242 VC_LOG_ERR("Invalid vid %i", vid);
1246 if (unlikely((uint32_t)option >=
1247 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1248 VC_LOG_ERR("Invalid option %i", option);
1252 vcrypto = (struct vhost_crypto *)dev->extern_data;
1253 if (unlikely(vcrypto == NULL)) {
1254 VC_LOG_ERR("Cannot find required data, is it initialized?");
1258 if (vcrypto->option == (uint8_t)option)
1261 if (!(rte_mempool_full(vcrypto->mbuf_pool))) {
1262 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1266 vcrypto->option = (uint8_t)option;
1271 uint16_t __rte_experimental
1272 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1273 struct rte_crypto_op **ops, uint16_t nb_ops)
1275 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1276 struct virtio_net *dev = get_device(vid);
1277 struct vhost_crypto *vcrypto;
1278 struct vhost_virtqueue *vq;
1285 if (unlikely(dev == NULL)) {
1286 VC_LOG_ERR("Invalid vid %i", vid);
1290 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1291 VC_LOG_ERR("Invalid qid %u", qid);
1295 vcrypto = (struct vhost_crypto *)dev->extern_data;
1296 if (unlikely(vcrypto == NULL)) {
1297 VC_LOG_ERR("Cannot find required data, is it initialized?");
1301 vq = dev->virtqueue[qid];
1303 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1304 start_idx = vq->last_used_idx;
1305 count = avail_idx - start_idx;
1306 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1307 count = RTE_MIN(count, nb_ops);
1309 if (unlikely(count == 0))
1312 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1313 * we need only 1 mbuf as src and dst
1315 required = count * 2;
1316 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, (void **)mbufs,
1318 VC_LOG_ERR("Insufficient memory");
1322 for (i = 0; i < count; i++) {
1323 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1324 uint16_t desc_idx = vq->avail->ring[used_idx];
1325 struct vring_desc *head = &vq->desc[desc_idx];
1326 struct rte_crypto_op *op = ops[i];
1328 op->sym->m_src = mbufs[i * 2];
1329 op->sym->m_dst = mbufs[i * 2 + 1];
1330 op->sym->m_src->data_off = 0;
1331 op->sym->m_dst->data_off = 0;
1333 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, op, head,
1338 vq->last_used_idx += i;
1343 uint16_t __rte_experimental
1344 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1345 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1347 struct rte_crypto_op **tmp_ops = ops;
1348 uint16_t count = 0, left = nb_ops;
1353 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1355 if (unlikely(count == 0))
1358 tmp_ops = &tmp_ops[count];
1361 callfds[idx++] = callfd;
1363 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1364 VC_LOG_ERR("Too many vqs");
1371 return nb_ops - left;