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 GPA_TO_VVA(t, m, a) ((t)(uintptr_t)rte_vhost_gpa_to_vva(m, a))
48 cipher_algo_transform(uint32_t virtio_cipher_algo)
52 switch (virtio_cipher_algo) {
53 case VIRTIO_CRYPTO_CIPHER_AES_CBC:
54 ret = RTE_CRYPTO_CIPHER_AES_CBC;
56 case VIRTIO_CRYPTO_CIPHER_AES_CTR:
57 ret = RTE_CRYPTO_CIPHER_AES_CTR;
59 case VIRTIO_CRYPTO_CIPHER_DES_ECB:
60 ret = -VIRTIO_CRYPTO_NOTSUPP;
62 case VIRTIO_CRYPTO_CIPHER_DES_CBC:
63 ret = RTE_CRYPTO_CIPHER_DES_CBC;
65 case VIRTIO_CRYPTO_CIPHER_3DES_ECB:
66 ret = RTE_CRYPTO_CIPHER_3DES_ECB;
68 case VIRTIO_CRYPTO_CIPHER_3DES_CBC:
69 ret = RTE_CRYPTO_CIPHER_3DES_CBC;
71 case VIRTIO_CRYPTO_CIPHER_3DES_CTR:
72 ret = RTE_CRYPTO_CIPHER_3DES_CTR;
74 case VIRTIO_CRYPTO_CIPHER_KASUMI_F8:
75 ret = RTE_CRYPTO_CIPHER_KASUMI_F8;
77 case VIRTIO_CRYPTO_CIPHER_SNOW3G_UEA2:
78 ret = RTE_CRYPTO_CIPHER_SNOW3G_UEA2;
80 case VIRTIO_CRYPTO_CIPHER_AES_F8:
81 ret = RTE_CRYPTO_CIPHER_AES_F8;
83 case VIRTIO_CRYPTO_CIPHER_AES_XTS:
84 ret = RTE_CRYPTO_CIPHER_AES_XTS;
86 case VIRTIO_CRYPTO_CIPHER_ZUC_EEA3:
87 ret = RTE_CRYPTO_CIPHER_ZUC_EEA3;
90 ret = -VIRTIO_CRYPTO_BADMSG;
98 auth_algo_transform(uint32_t virtio_auth_algo)
102 switch (virtio_auth_algo) {
104 case VIRTIO_CRYPTO_NO_MAC:
105 ret = RTE_CRYPTO_AUTH_NULL;
107 case VIRTIO_CRYPTO_MAC_HMAC_MD5:
108 ret = RTE_CRYPTO_AUTH_MD5_HMAC;
110 case VIRTIO_CRYPTO_MAC_HMAC_SHA1:
111 ret = RTE_CRYPTO_AUTH_SHA1_HMAC;
113 case VIRTIO_CRYPTO_MAC_HMAC_SHA_224:
114 ret = RTE_CRYPTO_AUTH_SHA224_HMAC;
116 case VIRTIO_CRYPTO_MAC_HMAC_SHA_256:
117 ret = RTE_CRYPTO_AUTH_SHA256_HMAC;
119 case VIRTIO_CRYPTO_MAC_HMAC_SHA_384:
120 ret = RTE_CRYPTO_AUTH_SHA384_HMAC;
122 case VIRTIO_CRYPTO_MAC_HMAC_SHA_512:
123 ret = RTE_CRYPTO_AUTH_SHA512_HMAC;
125 case VIRTIO_CRYPTO_MAC_CMAC_3DES:
126 ret = -VIRTIO_CRYPTO_NOTSUPP;
128 case VIRTIO_CRYPTO_MAC_CMAC_AES:
129 ret = RTE_CRYPTO_AUTH_AES_CMAC;
131 case VIRTIO_CRYPTO_MAC_KASUMI_F9:
132 ret = RTE_CRYPTO_AUTH_KASUMI_F9;
134 case VIRTIO_CRYPTO_MAC_SNOW3G_UIA2:
135 ret = RTE_CRYPTO_AUTH_SNOW3G_UIA2;
137 case VIRTIO_CRYPTO_MAC_GMAC_AES:
138 ret = RTE_CRYPTO_AUTH_AES_GMAC;
140 case VIRTIO_CRYPTO_MAC_GMAC_TWOFISH:
141 ret = -VIRTIO_CRYPTO_NOTSUPP;
143 case VIRTIO_CRYPTO_MAC_CBCMAC_AES:
144 ret = RTE_CRYPTO_AUTH_AES_CBC_MAC;
146 case VIRTIO_CRYPTO_MAC_CBCMAC_KASUMI_F9:
147 ret = -VIRTIO_CRYPTO_NOTSUPP;
149 case VIRTIO_CRYPTO_MAC_XCBC_AES:
150 ret = RTE_CRYPTO_AUTH_AES_XCBC_MAC;
153 ret = -VIRTIO_CRYPTO_BADMSG;
160 static int get_iv_len(enum rte_crypto_cipher_algorithm algo)
165 case RTE_CRYPTO_CIPHER_3DES_CBC:
168 case RTE_CRYPTO_CIPHER_3DES_CTR:
171 case RTE_CRYPTO_CIPHER_3DES_ECB:
174 case RTE_CRYPTO_CIPHER_AES_CBC:
178 /* TODO: add common algos */
189 * vhost_crypto struct is used to maintain a number of virtio_cryptos and
190 * one DPDK crypto device that deals with all crypto workloads. It is declared
191 * here and defined in vhost_crypto.c
193 struct vhost_crypto {
194 /** Used to lookup DPDK Cryptodev Session based on VIRTIO crypto
197 struct rte_hash *session_map;
198 struct rte_mempool *mbuf_pool;
199 struct rte_mempool *sess_pool;
201 /** DPDK cryptodev ID */
205 uint64_t last_session_id;
207 uint64_t cache_session_id;
208 struct rte_cryptodev_sym_session *cache_session;
209 /** socket id for the device */
212 struct virtio_net *dev;
215 } __rte_cache_aligned;
217 struct vhost_crypto_data_req {
218 struct vring_desc *head;
219 struct rte_vhost_memory *mem;
220 struct virtio_crypto_inhdr *inhdr;
221 struct vhost_virtqueue *vq;
222 struct vring_desc *wb_desc;
230 transform_cipher_param(struct rte_crypto_sym_xform *xform,
231 VhostUserCryptoSessionParam *param)
235 ret = cipher_algo_transform(param->cipher_algo);
236 if (unlikely(ret < 0))
239 xform->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
240 xform->cipher.algo = (uint32_t)ret;
241 xform->cipher.key.length = param->cipher_key_len;
242 if (xform->cipher.key.length > 0)
243 xform->cipher.key.data = param->cipher_key_buf;
244 if (param->dir == VIRTIO_CRYPTO_OP_ENCRYPT)
245 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
246 else if (param->dir == VIRTIO_CRYPTO_OP_DECRYPT)
247 xform->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
249 VC_LOG_DBG("Bad operation type");
250 return -VIRTIO_CRYPTO_BADMSG;
253 ret = get_iv_len(xform->cipher.algo);
254 if (unlikely(ret < 0))
256 xform->cipher.iv.length = (uint16_t)ret;
257 xform->cipher.iv.offset = IV_OFFSET;
262 transform_chain_param(struct rte_crypto_sym_xform *xforms,
263 VhostUserCryptoSessionParam *param)
265 struct rte_crypto_sym_xform *xform_cipher, *xform_auth;
268 switch (param->chaining_dir) {
269 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_HASH_THEN_CIPHER:
271 xform_cipher = xforms->next;
272 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_DECRYPT;
273 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_VERIFY;
275 case VIRTIO_CRYPTO_SYM_ALG_CHAIN_ORDER_CIPHER_THEN_HASH:
276 xform_cipher = xforms;
277 xform_auth = xforms->next;
278 xform_cipher->cipher.op = RTE_CRYPTO_CIPHER_OP_ENCRYPT;
279 xform_auth->auth.op = RTE_CRYPTO_AUTH_OP_GENERATE;
282 return -VIRTIO_CRYPTO_BADMSG;
286 ret = cipher_algo_transform(param->cipher_algo);
287 if (unlikely(ret < 0))
289 xform_cipher->type = RTE_CRYPTO_SYM_XFORM_CIPHER;
290 xform_cipher->cipher.algo = (uint32_t)ret;
291 xform_cipher->cipher.key.length = param->cipher_key_len;
292 xform_cipher->cipher.key.data = param->cipher_key_buf;
293 ret = get_iv_len(xform_cipher->cipher.algo);
294 if (unlikely(ret < 0))
296 xform_cipher->cipher.iv.length = (uint16_t)ret;
297 xform_cipher->cipher.iv.offset = IV_OFFSET;
300 xform_auth->type = RTE_CRYPTO_SYM_XFORM_AUTH;
301 ret = auth_algo_transform(param->hash_algo);
302 if (unlikely(ret < 0))
304 xform_auth->auth.algo = (uint32_t)ret;
305 xform_auth->auth.digest_length = param->digest_len;
306 xform_auth->auth.key.length = param->auth_key_len;
307 xform_auth->auth.key.data = param->auth_key_buf;
313 vhost_crypto_create_sess(struct vhost_crypto *vcrypto,
314 VhostUserCryptoSessionParam *sess_param)
316 struct rte_crypto_sym_xform xform1 = {0}, xform2 = {0};
317 struct rte_cryptodev_sym_session *session;
320 switch (sess_param->op_type) {
321 case VIRTIO_CRYPTO_SYM_OP_NONE:
322 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
323 ret = transform_cipher_param(&xform1, sess_param);
325 VC_LOG_ERR("Error transform session msg (%i)", ret);
326 sess_param->session_id = ret;
330 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
331 if (unlikely(sess_param->hash_mode !=
332 VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH)) {
333 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
334 VC_LOG_ERR("Error transform session message (%i)",
335 -VIRTIO_CRYPTO_NOTSUPP);
339 xform1.next = &xform2;
341 ret = transform_chain_param(&xform1, sess_param);
343 VC_LOG_ERR("Error transform session message (%i)", ret);
344 sess_param->session_id = ret;
350 VC_LOG_ERR("Algorithm not yet supported");
351 sess_param->session_id = -VIRTIO_CRYPTO_NOTSUPP;
355 session = rte_cryptodev_sym_session_create(vcrypto->sess_pool);
357 VC_LOG_ERR("Failed to create session");
358 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
362 if (rte_cryptodev_sym_session_init(vcrypto->cid, session, &xform1,
363 vcrypto->sess_pool) < 0) {
364 VC_LOG_ERR("Failed to initialize session");
365 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
369 /* insert hash to map */
370 if (rte_hash_add_key_data(vcrypto->session_map,
371 &vcrypto->last_session_id, session) < 0) {
372 VC_LOG_ERR("Failed to insert session to hash table");
374 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0)
375 VC_LOG_ERR("Failed to clear session");
377 if (rte_cryptodev_sym_session_free(session) < 0)
378 VC_LOG_ERR("Failed to free session");
380 sess_param->session_id = -VIRTIO_CRYPTO_ERR;
384 VC_LOG_INFO("Session %"PRIu64" created for vdev %i.",
385 vcrypto->last_session_id, vcrypto->dev->vid);
387 sess_param->session_id = vcrypto->last_session_id;
388 vcrypto->last_session_id++;
392 vhost_crypto_close_sess(struct vhost_crypto *vcrypto, uint64_t session_id)
394 struct rte_cryptodev_sym_session *session;
395 uint64_t sess_id = session_id;
398 ret = rte_hash_lookup_data(vcrypto->session_map, &sess_id,
401 if (unlikely(ret < 0)) {
402 VC_LOG_ERR("Failed to delete session %"PRIu64".", session_id);
403 return -VIRTIO_CRYPTO_INVSESS;
406 if (rte_cryptodev_sym_session_clear(vcrypto->cid, session) < 0) {
407 VC_LOG_DBG("Failed to clear session");
408 return -VIRTIO_CRYPTO_ERR;
411 if (rte_cryptodev_sym_session_free(session) < 0) {
412 VC_LOG_DBG("Failed to free session");
413 return -VIRTIO_CRYPTO_ERR;
416 if (rte_hash_del_key(vcrypto->session_map, &sess_id) < 0) {
417 VC_LOG_DBG("Failed to delete session from hash table.");
418 return -VIRTIO_CRYPTO_ERR;
421 VC_LOG_INFO("Session %"PRIu64" deleted for vdev %i.", sess_id,
428 vhost_crypto_msg_post_handler(int vid, void *msg, uint32_t *require_reply)
430 struct virtio_net *dev = get_device(vid);
431 struct vhost_crypto *vcrypto;
432 VhostUserMsg *vmsg = msg;
435 if (dev == NULL || require_reply == NULL) {
436 VC_LOG_ERR("Invalid vid %i", vid);
440 vcrypto = dev->extern_data;
441 if (vcrypto == NULL) {
442 VC_LOG_ERR("Cannot find required data, is it initialized?");
448 if (vmsg->request.master == VHOST_USER_CRYPTO_CREATE_SESS) {
449 vhost_crypto_create_sess(vcrypto,
450 &vmsg->payload.crypto_session);
452 } else if (vmsg->request.master == VHOST_USER_CRYPTO_CLOSE_SESS)
453 ret = vhost_crypto_close_sess(vcrypto, vmsg->payload.u64);
460 static __rte_always_inline struct vring_desc *
461 find_write_desc(struct vring_desc *head, struct vring_desc *desc)
463 if (desc->flags & VRING_DESC_F_WRITE)
466 while (desc->flags & VRING_DESC_F_NEXT) {
467 desc = &head[desc->next];
468 if (desc->flags & VRING_DESC_F_WRITE)
475 static struct virtio_crypto_inhdr *
476 reach_inhdr(struct vring_desc *head, struct rte_vhost_memory *mem,
477 struct vring_desc *desc)
479 while (desc->flags & VRING_DESC_F_NEXT)
480 desc = &head[desc->next];
482 return GPA_TO_VVA(struct virtio_crypto_inhdr *, mem, desc->addr);
485 static __rte_always_inline int
486 move_desc(struct vring_desc *head, struct vring_desc **cur_desc,
489 struct vring_desc *desc = *cur_desc;
492 rte_prefetch0(&head[desc->next]);
495 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
496 desc = &head[desc->next];
497 rte_prefetch0(&head[desc->next]);
501 if (unlikely(left < 0)) {
502 VC_LOG_ERR("Incorrect virtio descriptor");
506 *cur_desc = &head[desc->next];
511 copy_data(void *dst_data, struct vring_desc *head, struct rte_vhost_memory *mem,
512 struct vring_desc **cur_desc, uint32_t size)
514 struct vring_desc *desc = *cur_desc;
516 uint8_t *data = dst_data;
520 rte_prefetch0(&head[desc->next]);
521 to_copy = RTE_MIN(desc->len, (uint32_t)left);
522 src = GPA_TO_VVA(uint8_t *, mem, desc->addr);
523 rte_memcpy((uint8_t *)data, src, to_copy);
526 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
527 desc = &head[desc->next];
528 rte_prefetch0(&head[desc->next]);
529 to_copy = RTE_MIN(desc->len, (uint32_t)left);
530 src = GPA_TO_VVA(uint8_t *, mem, desc->addr);
531 rte_memcpy(data + size - left, src, to_copy);
535 if (unlikely(left < 0)) {
536 VC_LOG_ERR("Incorrect virtio descriptor");
540 *cur_desc = &head[desc->next];
545 static __rte_always_inline void *
546 get_data_ptr(struct vring_desc *head, struct rte_vhost_memory *mem,
547 struct vring_desc **cur_desc, uint32_t size)
551 data = GPA_TO_VVA(void *, mem, (*cur_desc)->addr);
552 if (unlikely(!data)) {
553 VC_LOG_ERR("Failed to get object");
557 if (unlikely(move_desc(head, cur_desc, size) < 0))
564 write_back_data(struct rte_crypto_op *op, struct vhost_crypto_data_req *vc_req)
566 struct rte_mbuf *mbuf = op->sym->m_dst;
567 struct vring_desc *head = vc_req->head;
568 struct rte_vhost_memory *mem = vc_req->mem;
569 struct vring_desc *desc = vc_req->wb_desc;
570 int left = vc_req->wb_len;
572 uint8_t *src_data = mbuf->buf_addr, *dst;
574 rte_prefetch0(&head[desc->next]);
575 to_write = RTE_MIN(desc->len, (uint32_t)left);
576 dst = GPA_TO_VVA(uint8_t *, mem, desc->addr);
577 rte_memcpy(dst, src_data, to_write);
579 src_data += to_write;
581 while ((desc->flags & VRING_DESC_F_NEXT) && left > 0) {
582 desc = &head[desc->next];
583 rte_prefetch0(&head[desc->next]);
584 to_write = RTE_MIN(desc->len, (uint32_t)left);
585 dst = GPA_TO_VVA(uint8_t *, mem, desc->addr);
586 rte_memcpy(dst, src_data, to_write);
588 src_data += to_write;
591 if (unlikely(left < 0)) {
592 VC_LOG_ERR("Incorrect virtio descriptor");
600 prepare_sym_cipher_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
601 struct vhost_crypto_data_req *vc_req,
602 struct virtio_crypto_cipher_data_req *cipher,
603 struct vring_desc *cur_desc)
605 struct vring_desc *head = vc_req->head;
606 struct vring_desc *desc = cur_desc;
607 struct rte_vhost_memory *mem = vc_req->mem;
608 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
609 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
614 if (unlikely(copy_data(iv_data, head, mem, &desc,
615 cipher->para.iv_len) < 0)) {
616 ret = VIRTIO_CRYPTO_BADMSG;
620 m_src->data_len = cipher->para.src_data_len;
622 switch (vcrypto->option) {
623 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
624 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
625 cipher->para.src_data_len);
626 m_src->buf_addr = get_data_ptr(head, mem, &desc,
627 cipher->para.src_data_len);
628 if (unlikely(m_src->buf_iova == 0 ||
629 m_src->buf_addr == NULL)) {
630 VC_LOG_ERR("zero_copy may fail due to cross page data");
631 ret = VIRTIO_CRYPTO_ERR;
635 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
636 if (unlikely(cipher->para.src_data_len >
637 RTE_MBUF_DEFAULT_BUF_SIZE)) {
638 VC_LOG_ERR("Not enough space to do data copy");
639 ret = VIRTIO_CRYPTO_ERR;
642 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), head,
643 mem, &desc, cipher->para.src_data_len))
645 ret = VIRTIO_CRYPTO_BADMSG;
650 ret = VIRTIO_CRYPTO_BADMSG;
655 desc = find_write_desc(head, desc);
656 if (unlikely(!desc)) {
657 VC_LOG_ERR("Cannot find write location");
658 ret = VIRTIO_CRYPTO_BADMSG;
662 switch (vcrypto->option) {
663 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
664 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
665 desc->addr, cipher->para.dst_data_len);
666 m_dst->buf_addr = get_data_ptr(head, mem, &desc,
667 cipher->para.dst_data_len);
668 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
669 VC_LOG_ERR("zero_copy may fail due to cross page data");
670 ret = VIRTIO_CRYPTO_ERR;
674 m_dst->data_len = cipher->para.dst_data_len;
676 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
677 vc_req->wb_desc = desc;
678 vc_req->wb_len = cipher->para.dst_data_len;
679 if (unlikely(move_desc(head, &desc, vc_req->wb_len) < 0)) {
680 ret = VIRTIO_CRYPTO_ERR;
685 ret = VIRTIO_CRYPTO_BADMSG;
690 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
691 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
693 op->sym->cipher.data.offset = 0;
694 op->sym->cipher.data.length = cipher->para.src_data_len;
696 vc_req->inhdr = get_data_ptr(head, mem, &desc, INHDR_LEN);
697 if (unlikely(vc_req->inhdr == NULL)) {
698 ret = VIRTIO_CRYPTO_BADMSG;
702 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
703 vc_req->len = cipher->para.dst_data_len + INHDR_LEN;
708 vc_req->len = INHDR_LEN;
713 prepare_sym_chain_op(struct vhost_crypto *vcrypto, struct rte_crypto_op *op,
714 struct vhost_crypto_data_req *vc_req,
715 struct virtio_crypto_alg_chain_data_req *chain,
716 struct vring_desc *cur_desc)
718 struct vring_desc *head = vc_req->head;
719 struct vring_desc *desc = cur_desc;
720 struct rte_vhost_memory *mem = vc_req->mem;
721 struct rte_mbuf *m_src = op->sym->m_src, *m_dst = op->sym->m_dst;
722 uint8_t *iv_data = rte_crypto_op_ctod_offset(op, uint8_t *, IV_OFFSET);
723 uint32_t digest_offset;
729 if (unlikely(copy_data(iv_data, head, mem, &desc,
730 chain->para.iv_len) < 0)) {
731 ret = VIRTIO_CRYPTO_BADMSG;
735 m_src->data_len = chain->para.src_data_len;
736 m_dst->data_len = chain->para.dst_data_len;
738 switch (vcrypto->option) {
739 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
740 m_src->buf_iova = gpa_to_hpa(vcrypto->dev, desc->addr,
741 chain->para.src_data_len);
742 m_src->buf_addr = get_data_ptr(head, mem, &desc,
743 chain->para.src_data_len);
744 if (unlikely(m_src->buf_iova == 0 || m_src->buf_addr == NULL)) {
745 VC_LOG_ERR("zero_copy may fail due to cross page data");
746 ret = VIRTIO_CRYPTO_ERR;
750 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
751 if (unlikely(chain->para.src_data_len >
752 RTE_MBUF_DEFAULT_BUF_SIZE)) {
753 VC_LOG_ERR("Not enough space to do data copy");
754 ret = VIRTIO_CRYPTO_ERR;
757 if (unlikely(copy_data(rte_pktmbuf_mtod(m_src, uint8_t *), head,
758 mem, &desc, chain->para.src_data_len)) < 0) {
759 ret = VIRTIO_CRYPTO_BADMSG;
764 ret = VIRTIO_CRYPTO_BADMSG;
769 desc = find_write_desc(head, desc);
770 if (unlikely(!desc)) {
771 VC_LOG_ERR("Cannot find write location");
772 ret = VIRTIO_CRYPTO_BADMSG;
776 switch (vcrypto->option) {
777 case RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE:
778 m_dst->buf_iova = gpa_to_hpa(vcrypto->dev,
779 desc->addr, chain->para.dst_data_len);
780 m_dst->buf_addr = get_data_ptr(head, mem, &desc,
781 chain->para.dst_data_len);
782 if (unlikely(m_dst->buf_iova == 0 || m_dst->buf_addr == NULL)) {
783 VC_LOG_ERR("zero_copy may fail due to cross page data");
784 ret = VIRTIO_CRYPTO_ERR;
788 op->sym->auth.digest.phys_addr = gpa_to_hpa(vcrypto->dev,
789 desc->addr, chain->para.hash_result_len);
790 op->sym->auth.digest.data = get_data_ptr(head, mem, &desc,
791 chain->para.hash_result_len);
792 if (unlikely(op->sym->auth.digest.phys_addr == 0)) {
793 VC_LOG_ERR("zero_copy may fail due to cross page data");
794 ret = VIRTIO_CRYPTO_ERR;
798 case RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE:
799 digest_offset = m_dst->data_len;
800 digest_addr = rte_pktmbuf_mtod_offset(m_dst, void *,
803 vc_req->wb_desc = desc;
804 vc_req->wb_len = m_dst->data_len + chain->para.hash_result_len;
806 if (unlikely(move_desc(head, &desc,
807 chain->para.dst_data_len) < 0)) {
808 ret = VIRTIO_CRYPTO_BADMSG;
812 if (unlikely(copy_data(digest_addr, head, mem, &desc,
813 chain->para.hash_result_len)) < 0) {
814 ret = VIRTIO_CRYPTO_BADMSG;
818 op->sym->auth.digest.data = digest_addr;
819 op->sym->auth.digest.phys_addr = rte_pktmbuf_iova_offset(m_dst,
821 if (unlikely(move_desc(head, &desc,
822 chain->para.hash_result_len) < 0)) {
823 ret = VIRTIO_CRYPTO_ERR;
828 ret = VIRTIO_CRYPTO_BADMSG;
833 vc_req->inhdr = get_data_ptr(head, mem, &desc, INHDR_LEN);
834 if (unlikely(vc_req->inhdr == NULL)) {
835 ret = VIRTIO_CRYPTO_BADMSG;
839 vc_req->inhdr->status = VIRTIO_CRYPTO_OK;
841 op->type = RTE_CRYPTO_OP_TYPE_SYMMETRIC;
842 op->sess_type = RTE_CRYPTO_OP_WITH_SESSION;
844 op->sym->cipher.data.offset = chain->para.cipher_start_src_offset;
845 op->sym->cipher.data.length = chain->para.src_data_len -
846 chain->para.cipher_start_src_offset;
848 op->sym->auth.data.offset = chain->para.hash_start_src_offset;
849 op->sym->auth.data.length = chain->para.len_to_hash;
851 vc_req->len = chain->para.dst_data_len + chain->para.hash_result_len +
856 vc_req->len = INHDR_LEN;
861 * Process on descriptor
863 static __rte_always_inline int
864 vhost_crypto_process_one_req(struct vhost_crypto *vcrypto,
865 struct vhost_virtqueue *vq, struct rte_crypto_op *op,
866 struct vring_desc *head, uint16_t desc_idx,
867 struct rte_vhost_memory *mem)
869 struct vhost_crypto_data_req *vc_req = RTE_PTR_ADD(op->sym->m_src,
870 sizeof(struct rte_mbuf));
871 struct rte_cryptodev_sym_session *session;
872 struct virtio_crypto_op_data_req *req;
873 struct virtio_crypto_inhdr *inhdr;
874 struct vring_desc *desc = NULL;
878 vc_req->desc_idx = desc_idx;
880 if (likely(head->flags & VRING_DESC_F_INDIRECT)) {
881 head = GPA_TO_VVA(struct vring_desc *, mem, head->addr);
893 vc_req->zero_copy = vcrypto->option;
895 req = get_data_ptr(head, mem, &desc, sizeof(*req));
896 if (unlikely(req == NULL)) {
897 err = VIRTIO_CRYPTO_ERR;
898 VC_LOG_ERR("Invalid descriptor");
902 switch (req->header.opcode) {
903 case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
904 case VIRTIO_CRYPTO_CIPHER_DECRYPT:
905 session_id = req->header.session_id;
907 /* one branch to avoid unnecessary table lookup */
908 if (vcrypto->cache_session_id != session_id) {
909 err = rte_hash_lookup_data(vcrypto->session_map,
910 &session_id, (void **)&session);
911 if (unlikely(err < 0)) {
912 err = VIRTIO_CRYPTO_ERR;
913 VC_LOG_ERR("Failed to find session %"PRIu64,
918 vcrypto->cache_session = session;
919 vcrypto->cache_session_id = session_id;
922 session = vcrypto->cache_session;
924 err = rte_crypto_op_attach_sym_session(op, session);
925 if (unlikely(err < 0)) {
926 err = VIRTIO_CRYPTO_ERR;
927 VC_LOG_ERR("Failed to attach session to op");
931 switch (req->u.sym_req.op_type) {
932 case VIRTIO_CRYPTO_SYM_OP_NONE:
933 err = VIRTIO_CRYPTO_NOTSUPP;
935 case VIRTIO_CRYPTO_SYM_OP_CIPHER:
936 err = prepare_sym_cipher_op(vcrypto, op, vc_req,
937 &req->u.sym_req.u.cipher, desc);
939 case VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING:
940 err = prepare_sym_chain_op(vcrypto, op, vc_req,
941 &req->u.sym_req.u.chain, desc);
944 if (unlikely(err != 0)) {
945 VC_LOG_ERR("Failed to process sym request");
950 VC_LOG_ERR("Unsupported symmetric crypto request type %u",
959 inhdr = reach_inhdr(head, mem, desc);
960 if (likely(inhdr != NULL))
961 inhdr->status = (uint8_t)err;
966 static __rte_always_inline struct vhost_virtqueue *
967 vhost_crypto_finalize_one_request(struct rte_crypto_op *op,
968 struct vhost_virtqueue *old_vq)
970 struct rte_mbuf *m_src = op->sym->m_src;
971 struct rte_mbuf *m_dst = op->sym->m_dst;
972 struct vhost_crypto_data_req *vc_req = RTE_PTR_ADD(m_src,
973 sizeof(struct rte_mbuf));
977 if (unlikely(!vc_req)) {
978 VC_LOG_ERR("Failed to retrieve vc_req");
982 if (old_vq && (vc_req->vq != old_vq))
985 desc_idx = vc_req->desc_idx;
987 if (unlikely(op->status != RTE_CRYPTO_OP_STATUS_SUCCESS))
988 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
990 if (vc_req->zero_copy == 0) {
991 ret = write_back_data(op, vc_req);
992 if (unlikely(ret != 0))
993 vc_req->inhdr->status = VIRTIO_CRYPTO_ERR;
997 vc_req->vq->used->ring[desc_idx].id = desc_idx;
998 vc_req->vq->used->ring[desc_idx].len = vc_req->len;
1000 rte_mempool_put(m_dst->pool, (void *)m_dst);
1001 rte_mempool_put(m_src->pool, (void *)m_src);
1006 static __rte_always_inline uint16_t
1007 vhost_crypto_complete_one_vm_requests(struct rte_crypto_op **ops,
1008 uint16_t nb_ops, int *callfd)
1010 uint16_t processed = 1;
1011 struct vhost_virtqueue *vq, *tmp_vq;
1013 if (unlikely(nb_ops == 0))
1016 vq = vhost_crypto_finalize_one_request(ops[0], NULL);
1017 if (unlikely(vq == NULL))
1021 while ((processed < nb_ops)) {
1022 tmp_vq = vhost_crypto_finalize_one_request(ops[processed],
1025 if (unlikely(vq != tmp_vq))
1031 *callfd = vq->callfd;
1033 *(volatile uint16_t *)&vq->used->idx += processed;
1038 int __rte_experimental
1039 rte_vhost_crypto_create(int vid, uint8_t cryptodev_id,
1040 struct rte_mempool *sess_pool, int socket_id)
1042 struct virtio_net *dev = get_device(vid);
1043 struct rte_hash_parameters params = {0};
1044 struct vhost_crypto *vcrypto;
1049 VC_LOG_ERR("Invalid vid %i", vid);
1053 ret = rte_vhost_driver_set_features(dev->ifname,
1054 VIRTIO_CRYPTO_FEATURES);
1056 VC_LOG_ERR("Error setting features");
1060 vcrypto = rte_zmalloc_socket(NULL, sizeof(*vcrypto),
1061 RTE_CACHE_LINE_SIZE, socket_id);
1063 VC_LOG_ERR("Insufficient memory");
1067 vcrypto->sess_pool = sess_pool;
1068 vcrypto->cid = cryptodev_id;
1069 vcrypto->cache_session_id = UINT64_MAX;
1070 vcrypto->last_session_id = 1;
1072 vcrypto->option = RTE_VHOST_CRYPTO_ZERO_COPY_DISABLE;
1074 snprintf(name, 127, "HASH_VHOST_CRYPT_%u", (uint32_t)vid);
1076 params.entries = VHOST_CRYPTO_SESSION_MAP_ENTRIES;
1077 params.hash_func = rte_jhash;
1078 params.key_len = sizeof(uint64_t);
1079 params.socket_id = socket_id;
1080 vcrypto->session_map = rte_hash_create(¶ms);
1081 if (!vcrypto->session_map) {
1082 VC_LOG_ERR("Failed to creath session map");
1087 snprintf(name, 127, "MBUF_POOL_VM_%u", (uint32_t)vid);
1088 vcrypto->mbuf_pool = rte_pktmbuf_pool_create(name,
1089 VHOST_CRYPTO_MBUF_POOL_SIZE, 512,
1090 sizeof(struct vhost_crypto_data_req),
1091 RTE_MBUF_DEFAULT_DATAROOM * 2 + RTE_PKTMBUF_HEADROOM,
1093 if (!vcrypto->mbuf_pool) {
1094 VC_LOG_ERR("Failed to creath mbuf pool");
1099 dev->extern_data = vcrypto;
1100 dev->extern_ops.pre_msg_handle = NULL;
1101 dev->extern_ops.post_msg_handle = vhost_crypto_msg_post_handler;
1106 if (vcrypto->session_map)
1107 rte_hash_free(vcrypto->session_map);
1108 if (vcrypto->mbuf_pool)
1109 rte_mempool_free(vcrypto->mbuf_pool);
1116 int __rte_experimental
1117 rte_vhost_crypto_free(int vid)
1119 struct virtio_net *dev = get_device(vid);
1120 struct vhost_crypto *vcrypto;
1122 if (unlikely(dev == NULL)) {
1123 VC_LOG_ERR("Invalid vid %i", vid);
1127 vcrypto = dev->extern_data;
1128 if (unlikely(vcrypto == NULL)) {
1129 VC_LOG_ERR("Cannot find required data, is it initialized?");
1133 rte_hash_free(vcrypto->session_map);
1134 rte_mempool_free(vcrypto->mbuf_pool);
1137 dev->extern_data = NULL;
1138 dev->extern_ops.pre_msg_handle = NULL;
1139 dev->extern_ops.post_msg_handle = NULL;
1144 int __rte_experimental
1145 rte_vhost_crypto_set_zero_copy(int vid, enum rte_vhost_crypto_zero_copy option)
1147 struct virtio_net *dev = get_device(vid);
1148 struct vhost_crypto *vcrypto;
1150 if (unlikely(dev == NULL)) {
1151 VC_LOG_ERR("Invalid vid %i", vid);
1155 if (unlikely(option < 0 || option >=
1156 RTE_VHOST_CRYPTO_MAX_ZERO_COPY_OPTIONS)) {
1157 VC_LOG_ERR("Invalid option %i", option);
1161 vcrypto = (struct vhost_crypto *)dev->extern_data;
1162 if (unlikely(vcrypto == NULL)) {
1163 VC_LOG_ERR("Cannot find required data, is it initialized?");
1167 if (vcrypto->option == (uint8_t)option)
1170 if (!(rte_mempool_full(vcrypto->mbuf_pool))) {
1171 VC_LOG_ERR("Cannot update zero copy as mempool is not full");
1175 vcrypto->option = (uint8_t)option;
1180 uint16_t __rte_experimental
1181 rte_vhost_crypto_fetch_requests(int vid, uint32_t qid,
1182 struct rte_crypto_op **ops, uint16_t nb_ops)
1184 struct rte_mbuf *mbufs[VHOST_CRYPTO_MAX_BURST_SIZE * 2];
1185 struct virtio_net *dev = get_device(vid);
1186 struct rte_vhost_memory *mem;
1187 struct vhost_crypto *vcrypto;
1188 struct vhost_virtqueue *vq;
1195 if (unlikely(dev == NULL)) {
1196 VC_LOG_ERR("Invalid vid %i", vid);
1200 if (unlikely(qid >= VHOST_MAX_QUEUE_PAIRS)) {
1201 VC_LOG_ERR("Invalid qid %u", qid);
1205 vcrypto = (struct vhost_crypto *)dev->extern_data;
1206 if (unlikely(vcrypto == NULL)) {
1207 VC_LOG_ERR("Cannot find required data, is it initialized?");
1211 vq = dev->virtqueue[qid];
1214 avail_idx = *((volatile uint16_t *)&vq->avail->idx);
1215 start_idx = vq->last_used_idx;
1216 count = avail_idx - start_idx;
1217 count = RTE_MIN(count, VHOST_CRYPTO_MAX_BURST_SIZE);
1218 count = RTE_MIN(count, nb_ops);
1220 if (unlikely(count == 0))
1223 /* for zero copy, we need 2 empty mbufs for src and dst, otherwise
1224 * we need only 1 mbuf as src and dst
1226 required = count * 2;
1227 if (unlikely(rte_mempool_get_bulk(vcrypto->mbuf_pool, (void **)mbufs,
1229 VC_LOG_ERR("Insufficient memory");
1233 for (i = 0; i < count; i++) {
1234 uint16_t used_idx = (start_idx + i) & (vq->size - 1);
1235 uint16_t desc_idx = vq->avail->ring[used_idx];
1236 struct vring_desc *head = &vq->desc[desc_idx];
1237 struct rte_crypto_op *op = ops[i];
1239 op->sym->m_src = mbufs[i * 2];
1240 op->sym->m_dst = mbufs[i * 2 + 1];
1241 op->sym->m_src->data_off = 0;
1242 op->sym->m_dst->data_off = 0;
1244 if (unlikely(vhost_crypto_process_one_req(vcrypto, vq, op, head,
1245 desc_idx, mem)) < 0)
1249 vq->last_used_idx += i;
1254 uint16_t __rte_experimental
1255 rte_vhost_crypto_finalize_requests(struct rte_crypto_op **ops,
1256 uint16_t nb_ops, int *callfds, uint16_t *nb_callfds)
1258 struct rte_crypto_op **tmp_ops = ops;
1259 uint16_t count = 0, left = nb_ops;
1264 count = vhost_crypto_complete_one_vm_requests(tmp_ops, left,
1266 if (unlikely(count == 0))
1269 tmp_ops = &tmp_ops[count];
1272 callfds[idx++] = callfd;
1274 if (unlikely(idx >= VIRTIO_CRYPTO_MAX_NUM_BURST_VQS)) {
1275 VC_LOG_ERR("Too many vqs");
1282 return nb_ops - left;