2016-10-28 11:33:24 +03:00
|
|
|
/*
|
|
|
|
* Virtio crypto Support
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Gonglei <arei.gonglei@huawei.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or
|
|
|
|
* (at your option) any later version. See the COPYING file in the
|
|
|
|
* top-level directory.
|
|
|
|
*/
|
2019-05-23 17:35:07 +03:00
|
|
|
|
2016-10-28 11:33:24 +03:00
|
|
|
#include "qemu/osdep.h"
|
|
|
|
#include "qemu/iov.h"
|
Include qemu/main-loop.h less
In my "build everything" tree, changing qemu/main-loop.h triggers a
recompile of some 5600 out of 6600 objects (not counting tests and
objects that don't depend on qemu/osdep.h). It includes block/aio.h,
which in turn includes qemu/event_notifier.h, qemu/notify.h,
qemu/processor.h, qemu/qsp.h, qemu/queue.h, qemu/thread-posix.h,
qemu/thread.h, qemu/timer.h, and a few more.
Include qemu/main-loop.h only where it's needed. Touching it now
recompiles only some 1700 objects. For block/aio.h and
qemu/event_notifier.h, these numbers drop from 5600 to 2800. For the
others, they shrink only slightly.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Message-Id: <20190812052359.30071-21-armbru@redhat.com>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Tested-by: Philippe Mathieu-Daudé <philmd@redhat.com>
2019-08-12 08:23:50 +03:00
|
|
|
#include "qemu/main-loop.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2016-10-28 11:33:24 +03:00
|
|
|
#include "qapi/error.h"
|
|
|
|
#include "qemu/error-report.h"
|
|
|
|
|
|
|
|
#include "hw/virtio/virtio.h"
|
|
|
|
#include "hw/virtio/virtio-crypto.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2016-10-28 11:33:24 +03:00
|
|
|
#include "standard-headers/linux/virtio_ids.h"
|
2018-03-01 16:46:29 +03:00
|
|
|
#include "sysemu/cryptodev-vhost.h"
|
2016-10-28 11:33:24 +03:00
|
|
|
|
|
|
|
#define VIRTIO_CRYPTO_VM_VERSION 1
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
typedef struct VirtIOCryptoSessionReq {
|
|
|
|
VirtIODevice *vdev;
|
|
|
|
VirtQueue *vq;
|
|
|
|
VirtQueueElement *elem;
|
|
|
|
CryptoDevBackendSessionInfo info;
|
|
|
|
CryptoDevCompletionFunc cb;
|
|
|
|
} VirtIOCryptoSessionReq;
|
|
|
|
|
|
|
|
static void virtio_crypto_free_create_session_req(VirtIOCryptoSessionReq *sreq)
|
|
|
|
{
|
|
|
|
switch (sreq->info.op_code) {
|
|
|
|
case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
|
|
|
|
g_free(sreq->info.u.sym_sess_info.cipher_key);
|
|
|
|
g_free(sreq->info.u.sym_sess_info.auth_key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION:
|
|
|
|
g_free(sreq->info.u.asym_sess_info.key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_HASH_DESTROY_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_MAC_DESTROY_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_AEAD_DESTROY_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error_report("Unknown opcode: %u", sreq->info.op_code);
|
|
|
|
}
|
|
|
|
g_free(sreq);
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
/*
|
|
|
|
* Transfer virtqueue index to crypto queue index.
|
|
|
|
* The control virtqueue is after the data virtqueues
|
|
|
|
* so the input value doesn't need to be adjusted
|
|
|
|
*/
|
|
|
|
static inline int virtio_crypto_vq2q(int queue_index)
|
|
|
|
{
|
|
|
|
return queue_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virtio_crypto_cipher_session_helper(VirtIODevice *vdev,
|
|
|
|
CryptoDevBackendSymSessionInfo *info,
|
|
|
|
struct virtio_crypto_cipher_session_para *cipher_para,
|
|
|
|
struct iovec **iov, unsigned int *out_num)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
unsigned int num = *out_num;
|
|
|
|
|
|
|
|
info->cipher_alg = ldl_le_p(&cipher_para->algo);
|
|
|
|
info->key_len = ldl_le_p(&cipher_para->keylen);
|
|
|
|
info->direction = ldl_le_p(&cipher_para->op);
|
|
|
|
DPRINTF("cipher_alg=%" PRIu32 ", info->direction=%" PRIu32 "\n",
|
|
|
|
info->cipher_alg, info->direction);
|
|
|
|
|
|
|
|
if (info->key_len > vcrypto->conf.max_cipher_key_len) {
|
|
|
|
error_report("virtio-crypto length of cipher key is too big: %u",
|
|
|
|
info->key_len);
|
|
|
|
return -VIRTIO_CRYPTO_ERR;
|
|
|
|
}
|
|
|
|
/* Get cipher key */
|
|
|
|
if (info->key_len > 0) {
|
|
|
|
size_t s;
|
|
|
|
DPRINTF("keylen=%" PRIu32 "\n", info->key_len);
|
|
|
|
|
|
|
|
info->cipher_key = g_malloc(info->key_len);
|
|
|
|
s = iov_to_buf(*iov, num, 0, info->cipher_key, info->key_len);
|
|
|
|
if (unlikely(s != info->key_len)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto cipher key incorrect");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
iov_discard_front(iov, &num, info->key_len);
|
|
|
|
*out_num = num;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
static int
|
2016-10-28 11:33:27 +03:00
|
|
|
virtio_crypto_create_sym_session(VirtIOCrypto *vcrypto,
|
|
|
|
struct virtio_crypto_sym_create_session_req *sess_req,
|
|
|
|
uint32_t queue_id,
|
|
|
|
uint32_t opcode,
|
2022-10-08 11:50:27 +03:00
|
|
|
struct iovec *iov, unsigned int out_num,
|
|
|
|
VirtIOCryptoSessionReq *sreq)
|
2016-10-28 11:33:27 +03:00
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
|
2022-10-08 11:50:27 +03:00
|
|
|
CryptoDevBackendSymSessionInfo *sym_info = &sreq->info.u.sym_sess_info;
|
2016-10-28 11:33:27 +03:00
|
|
|
int queue_index;
|
|
|
|
uint32_t op_type;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
op_type = ldl_le_p(&sess_req->op_type);
|
2022-10-08 11:50:27 +03:00
|
|
|
sreq->info.op_code = opcode;
|
2016-10-28 11:33:27 +03:00
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
sym_info = &sreq->info.u.sym_sess_info;
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_info->op_type = op_type;
|
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
if (op_type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
|
2022-06-11 09:42:43 +03:00
|
|
|
ret = virtio_crypto_cipher_session_helper(vdev, sym_info,
|
2016-10-28 11:33:27 +03:00
|
|
|
&sess_req->u.cipher.para,
|
|
|
|
&iov, &out_num);
|
|
|
|
if (ret < 0) {
|
2022-10-08 11:50:27 +03:00
|
|
|
return ret;
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
} else if (op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
|
|
|
|
size_t s;
|
|
|
|
/* cipher part */
|
2022-06-11 09:42:43 +03:00
|
|
|
ret = virtio_crypto_cipher_session_helper(vdev, sym_info,
|
2016-10-28 11:33:27 +03:00
|
|
|
&sess_req->u.chain.para.cipher_param,
|
|
|
|
&iov, &out_num);
|
|
|
|
if (ret < 0) {
|
2022-10-08 11:50:27 +03:00
|
|
|
return ret;
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
/* hash part */
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_info->alg_chain_order = ldl_le_p(
|
2016-10-28 11:33:27 +03:00
|
|
|
&sess_req->u.chain.para.alg_chain_order);
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_info->add_len = ldl_le_p(&sess_req->u.chain.para.aad_len);
|
|
|
|
sym_info->hash_mode = ldl_le_p(&sess_req->u.chain.para.hash_mode);
|
|
|
|
if (sym_info->hash_mode == VIRTIO_CRYPTO_SYM_HASH_MODE_AUTH) {
|
|
|
|
sym_info->hash_alg =
|
|
|
|
ldl_le_p(&sess_req->u.chain.para.u.mac_param.algo);
|
|
|
|
sym_info->auth_key_len = ldl_le_p(
|
2016-10-28 11:33:27 +03:00
|
|
|
&sess_req->u.chain.para.u.mac_param.auth_key_len);
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_info->hash_result_len = ldl_le_p(
|
2016-10-28 11:33:27 +03:00
|
|
|
&sess_req->u.chain.para.u.mac_param.hash_result_len);
|
2022-06-11 09:42:43 +03:00
|
|
|
if (sym_info->auth_key_len > vcrypto->conf.max_auth_key_len) {
|
2016-10-28 11:33:27 +03:00
|
|
|
error_report("virtio-crypto length of auth key is too big: %u",
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_info->auth_key_len);
|
2022-10-08 11:50:27 +03:00
|
|
|
return -VIRTIO_CRYPTO_ERR;
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
/* get auth key */
|
2022-06-11 09:42:43 +03:00
|
|
|
if (sym_info->auth_key_len > 0) {
|
|
|
|
sym_info->auth_key = g_malloc(sym_info->auth_key_len);
|
|
|
|
s = iov_to_buf(iov, out_num, 0, sym_info->auth_key,
|
|
|
|
sym_info->auth_key_len);
|
|
|
|
if (unlikely(s != sym_info->auth_key_len)) {
|
2016-10-28 11:33:27 +03:00
|
|
|
virtio_error(vdev,
|
|
|
|
"virtio-crypto authenticated key incorrect");
|
2022-10-08 11:50:27 +03:00
|
|
|
return -EFAULT;
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
2022-06-11 09:42:43 +03:00
|
|
|
iov_discard_front(&iov, &out_num, sym_info->auth_key_len);
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
2022-06-11 09:42:43 +03:00
|
|
|
} else if (sym_info->hash_mode == VIRTIO_CRYPTO_SYM_HASH_MODE_PLAIN) {
|
|
|
|
sym_info->hash_alg = ldl_le_p(
|
2016-10-28 11:33:27 +03:00
|
|
|
&sess_req->u.chain.para.u.hash_param.algo);
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_info->hash_result_len = ldl_le_p(
|
2016-10-28 11:33:27 +03:00
|
|
|
&sess_req->u.chain.para.u.hash_param.hash_result_len);
|
|
|
|
} else {
|
|
|
|
/* VIRTIO_CRYPTO_SYM_HASH_MODE_NESTED */
|
|
|
|
error_report("unsupported hash mode");
|
2022-10-08 11:50:27 +03:00
|
|
|
return -VIRTIO_CRYPTO_NOTSUPP;
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* VIRTIO_CRYPTO_SYM_OP_NONE */
|
|
|
|
error_report("unsupported cipher op_type: VIRTIO_CRYPTO_SYM_OP_NONE");
|
2022-10-08 11:50:27 +03:00
|
|
|
return -VIRTIO_CRYPTO_NOTSUPP;
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
queue_index = virtio_crypto_vq2q(queue_id);
|
2022-10-08 11:50:27 +03:00
|
|
|
return cryptodev_backend_create_session(vcrypto->cryptodev, &sreq->info,
|
|
|
|
queue_index, sreq->cb, sreq);
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
static int
|
2022-06-11 09:42:43 +03:00
|
|
|
virtio_crypto_create_asym_session(VirtIOCrypto *vcrypto,
|
|
|
|
struct virtio_crypto_akcipher_create_session_req *sess_req,
|
|
|
|
uint32_t queue_id, uint32_t opcode,
|
2022-10-08 11:50:27 +03:00
|
|
|
struct iovec *iov, unsigned int out_num,
|
|
|
|
VirtIOCryptoSessionReq *sreq)
|
2022-06-11 09:42:43 +03:00
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
|
2022-10-08 11:50:27 +03:00
|
|
|
CryptoDevBackendAsymSessionInfo *asym_info = &sreq->info.u.asym_sess_info;
|
2022-06-11 09:42:43 +03:00
|
|
|
int queue_index;
|
|
|
|
uint32_t algo, keytype, keylen;
|
|
|
|
|
|
|
|
algo = ldl_le_p(&sess_req->para.algo);
|
|
|
|
keytype = ldl_le_p(&sess_req->para.keytype);
|
|
|
|
keylen = ldl_le_p(&sess_req->para.keylen);
|
|
|
|
|
|
|
|
if ((keytype != VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PUBLIC)
|
|
|
|
&& (keytype != VIRTIO_CRYPTO_AKCIPHER_KEY_TYPE_PRIVATE)) {
|
|
|
|
error_report("unsupported asym keytype: %d", keytype);
|
|
|
|
return -VIRTIO_CRYPTO_NOTSUPP;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (keylen) {
|
2022-10-08 11:50:27 +03:00
|
|
|
asym_info->key = g_malloc(keylen);
|
|
|
|
if (iov_to_buf(iov, out_num, 0, asym_info->key, keylen) != keylen) {
|
2022-06-11 09:42:43 +03:00
|
|
|
virtio_error(vdev, "virtio-crypto asym key incorrect");
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
iov_discard_front(&iov, &out_num, keylen);
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
sreq->info.op_code = opcode;
|
|
|
|
asym_info = &sreq->info.u.asym_sess_info;
|
2022-06-11 09:42:43 +03:00
|
|
|
asym_info->algo = algo;
|
|
|
|
asym_info->keytype = keytype;
|
|
|
|
asym_info->keylen = keylen;
|
|
|
|
switch (asym_info->algo) {
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_RSA:
|
|
|
|
asym_info->u.rsa.padding_algo =
|
|
|
|
ldl_le_p(&sess_req->para.u.rsa.padding_algo);
|
|
|
|
asym_info->u.rsa.hash_algo =
|
|
|
|
ldl_le_p(&sess_req->para.u.rsa.hash_algo);
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* TODO DSA&ECDSA handling */
|
|
|
|
|
|
|
|
default:
|
|
|
|
return -VIRTIO_CRYPTO_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
queue_index = virtio_crypto_vq2q(queue_id);
|
2022-10-08 11:50:27 +03:00
|
|
|
return cryptodev_backend_create_session(vcrypto->cryptodev, &sreq->info,
|
|
|
|
queue_index, sreq->cb, sreq);
|
2022-06-11 09:42:43 +03:00
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
static int
|
2016-10-28 11:33:27 +03:00
|
|
|
virtio_crypto_handle_close_session(VirtIOCrypto *vcrypto,
|
|
|
|
struct virtio_crypto_destroy_session_req *close_sess_req,
|
2022-10-08 11:50:27 +03:00
|
|
|
uint32_t queue_id,
|
|
|
|
VirtIOCryptoSessionReq *sreq)
|
2016-10-28 11:33:27 +03:00
|
|
|
{
|
|
|
|
uint64_t session_id;
|
|
|
|
|
|
|
|
session_id = ldq_le_p(&close_sess_req->session_id);
|
|
|
|
DPRINTF("close session, id=%" PRIu64 "\n", session_id);
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
return cryptodev_backend_close_session(
|
|
|
|
vcrypto->cryptodev, session_id, queue_id, sreq->cb, sreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_create_session_completion(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
VirtIOCryptoSessionReq *sreq = (VirtIOCryptoSessionReq *)opaque;
|
|
|
|
VirtQueue *vq = sreq->vq;
|
|
|
|
VirtQueueElement *elem = sreq->elem;
|
|
|
|
VirtIODevice *vdev = sreq->vdev;
|
|
|
|
struct virtio_crypto_session_input input;
|
|
|
|
struct iovec *in_iov = elem->in_sg;
|
|
|
|
unsigned in_num = elem->in_num;
|
|
|
|
size_t s;
|
|
|
|
|
|
|
|
memset(&input, 0, sizeof(input));
|
|
|
|
/* Serious errors, need to reset virtio crypto device */
|
|
|
|
if (ret == -EFAULT) {
|
|
|
|
virtqueue_detach_element(vq, elem, 0);
|
|
|
|
goto out;
|
|
|
|
} else if (ret == -VIRTIO_CRYPTO_NOTSUPP) {
|
|
|
|
stl_le_p(&input.status, VIRTIO_CRYPTO_NOTSUPP);
|
|
|
|
} else if (ret == -VIRTIO_CRYPTO_KEY_REJECTED) {
|
|
|
|
stl_le_p(&input.status, VIRTIO_CRYPTO_KEY_REJECTED);
|
|
|
|
} else if (ret != VIRTIO_CRYPTO_OK) {
|
|
|
|
stl_le_p(&input.status, VIRTIO_CRYPTO_ERR);
|
2016-10-28 11:33:27 +03:00
|
|
|
} else {
|
2022-10-08 11:50:27 +03:00
|
|
|
/* Set the session id */
|
|
|
|
stq_le_p(&input.session_id, sreq->info.session_id);
|
|
|
|
stl_le_p(&input.status, VIRTIO_CRYPTO_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
s = iov_from_buf(in_iov, in_num, 0, &input, sizeof(input));
|
|
|
|
if (unlikely(s != sizeof(input))) {
|
|
|
|
virtio_error(vdev, "virtio-crypto input incorrect");
|
|
|
|
virtqueue_detach_element(vq, elem, 0);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
virtqueue_push(vq, elem, sizeof(input));
|
|
|
|
virtio_notify(vdev, vq);
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_free(elem);
|
|
|
|
virtio_crypto_free_create_session_req(sreq);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_destroy_session_completion(void *opaque, int ret)
|
|
|
|
{
|
|
|
|
VirtIOCryptoSessionReq *sreq = (VirtIOCryptoSessionReq *)opaque;
|
|
|
|
VirtQueue *vq = sreq->vq;
|
|
|
|
VirtQueueElement *elem = sreq->elem;
|
|
|
|
VirtIODevice *vdev = sreq->vdev;
|
|
|
|
struct iovec *in_iov = elem->in_sg;
|
|
|
|
unsigned in_num = elem->in_num;
|
|
|
|
uint8_t status;
|
|
|
|
size_t s;
|
|
|
|
|
|
|
|
if (ret < 0) {
|
2016-10-28 11:33:27 +03:00
|
|
|
status = VIRTIO_CRYPTO_ERR;
|
2022-10-08 11:50:27 +03:00
|
|
|
} else {
|
|
|
|
status = VIRTIO_CRYPTO_OK;
|
|
|
|
}
|
|
|
|
s = iov_from_buf(in_iov, in_num, 0, &status, sizeof(status));
|
|
|
|
if (unlikely(s != sizeof(status))) {
|
|
|
|
virtio_error(vdev, "virtio-crypto status incorrect");
|
|
|
|
virtqueue_detach_element(vq, elem, 0);
|
|
|
|
goto out;
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
2022-10-08 11:50:27 +03:00
|
|
|
virtqueue_push(vq, elem, sizeof(status));
|
|
|
|
virtio_notify(vdev, vq);
|
2016-10-28 11:33:27 +03:00
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
out:
|
|
|
|
g_free(elem);
|
|
|
|
g_free(sreq);
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
struct virtio_crypto_op_ctrl_req ctrl;
|
|
|
|
VirtQueueElement *elem;
|
2022-10-08 11:50:27 +03:00
|
|
|
VirtIOCryptoSessionReq *sreq;
|
2016-10-28 11:33:27 +03:00
|
|
|
unsigned out_num;
|
2022-10-08 11:50:27 +03:00
|
|
|
unsigned in_num;
|
2016-10-28 11:33:27 +03:00
|
|
|
uint32_t queue_id;
|
|
|
|
uint32_t opcode;
|
|
|
|
struct virtio_crypto_session_input input;
|
|
|
|
size_t s;
|
2022-10-08 11:50:27 +03:00
|
|
|
int ret;
|
|
|
|
struct iovec *out_iov;
|
|
|
|
struct iovec *in_iov;
|
2016-10-28 11:33:27 +03:00
|
|
|
|
|
|
|
for (;;) {
|
2020-09-17 12:44:55 +03:00
|
|
|
g_autofree struct iovec *out_iov_copy = NULL;
|
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
|
|
|
|
if (!elem) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (elem->out_num < 1 || elem->in_num < 1) {
|
|
|
|
virtio_error(vdev, "virtio-crypto ctrl missing headers");
|
|
|
|
virtqueue_detach_element(vq, elem, 0);
|
|
|
|
g_free(elem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_num = elem->out_num;
|
2022-05-12 20:57:46 +03:00
|
|
|
out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num);
|
2020-09-17 12:44:55 +03:00
|
|
|
out_iov = out_iov_copy;
|
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
in_num = elem->in_num;
|
|
|
|
in_iov = elem->in_sg;
|
2020-09-17 12:44:55 +03:00
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
if (unlikely(iov_to_buf(out_iov, out_num, 0, &ctrl, sizeof(ctrl))
|
|
|
|
!= sizeof(ctrl))) {
|
|
|
|
virtio_error(vdev, "virtio-crypto request ctrl_hdr too short");
|
|
|
|
virtqueue_detach_element(vq, elem, 0);
|
|
|
|
g_free(elem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iov_discard_front(&out_iov, &out_num, sizeof(ctrl));
|
|
|
|
|
|
|
|
opcode = ldl_le_p(&ctrl.header.opcode);
|
|
|
|
queue_id = ldl_le_p(&ctrl.header.queue_id);
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
sreq = g_new0(VirtIOCryptoSessionReq, 1);
|
|
|
|
sreq->vdev = vdev;
|
|
|
|
sreq->vq = vq;
|
|
|
|
sreq->elem = elem;
|
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
switch (opcode) {
|
|
|
|
case VIRTIO_CRYPTO_CIPHER_CREATE_SESSION:
|
2022-10-08 11:50:27 +03:00
|
|
|
sreq->cb = virtio_crypto_create_session_completion;
|
|
|
|
ret = virtio_crypto_create_sym_session(vcrypto,
|
|
|
|
&ctrl.u.sym_create_session,
|
|
|
|
queue_id, opcode,
|
|
|
|
out_iov, out_num,
|
|
|
|
sreq);
|
|
|
|
if (ret < 0) {
|
|
|
|
virtio_crypto_create_session_completion(sreq, ret);
|
|
|
|
}
|
|
|
|
break;
|
2022-06-11 09:42:43 +03:00
|
|
|
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_CREATE_SESSION:
|
2022-10-08 11:50:27 +03:00
|
|
|
sreq->cb = virtio_crypto_create_session_completion;
|
|
|
|
ret = virtio_crypto_create_asym_session(vcrypto,
|
2022-06-11 09:42:43 +03:00
|
|
|
&ctrl.u.akcipher_create_session,
|
|
|
|
queue_id, opcode,
|
2022-10-08 11:50:27 +03:00
|
|
|
out_iov, out_num,
|
|
|
|
sreq);
|
|
|
|
if (ret < 0) {
|
|
|
|
virtio_crypto_create_session_completion(sreq, ret);
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-06-11 09:42:43 +03:00
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
case VIRTIO_CRYPTO_CIPHER_DESTROY_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_HASH_DESTROY_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_MAC_DESTROY_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_AEAD_DESTROY_SESSION:
|
2022-06-11 09:42:43 +03:00
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_DESTROY_SESSION:
|
2022-10-08 11:50:27 +03:00
|
|
|
sreq->cb = virtio_crypto_destroy_session_completion;
|
|
|
|
ret = virtio_crypto_handle_close_session(vcrypto,
|
|
|
|
&ctrl.u.destroy_session, queue_id,
|
|
|
|
sreq);
|
|
|
|
if (ret < 0) {
|
|
|
|
virtio_crypto_destroy_session_completion(sreq, ret);
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-10-08 11:50:27 +03:00
|
|
|
|
2016-10-28 11:33:27 +03:00
|
|
|
case VIRTIO_CRYPTO_HASH_CREATE_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_MAC_CREATE_SESSION:
|
|
|
|
case VIRTIO_CRYPTO_AEAD_CREATE_SESSION:
|
|
|
|
default:
|
2022-10-08 11:50:27 +03:00
|
|
|
memset(&input, 0, sizeof(input));
|
2016-10-28 11:33:27 +03:00
|
|
|
error_report("virtio-crypto unsupported ctrl opcode: %d", opcode);
|
|
|
|
stl_le_p(&input.status, VIRTIO_CRYPTO_NOTSUPP);
|
|
|
|
s = iov_from_buf(in_iov, in_num, 0, &input, sizeof(input));
|
|
|
|
if (unlikely(s != sizeof(input))) {
|
|
|
|
virtio_error(vdev, "virtio-crypto input incorrect");
|
|
|
|
virtqueue_detach_element(vq, elem, 0);
|
2022-10-08 11:50:27 +03:00
|
|
|
} else {
|
|
|
|
virtqueue_push(vq, elem, sizeof(input));
|
|
|
|
virtio_notify(vdev, vq);
|
2016-10-28 11:33:27 +03:00
|
|
|
}
|
2022-10-08 11:50:27 +03:00
|
|
|
g_free(sreq);
|
|
|
|
g_free(elem);
|
2016-10-28 11:33:27 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
} /* end switch case */
|
|
|
|
|
|
|
|
} /* end for loop */
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:28 +03:00
|
|
|
static void virtio_crypto_init_request(VirtIOCrypto *vcrypto, VirtQueue *vq,
|
|
|
|
VirtIOCryptoReq *req)
|
|
|
|
{
|
|
|
|
req->vcrypto = vcrypto;
|
|
|
|
req->vq = vq;
|
|
|
|
req->in = NULL;
|
|
|
|
req->in_iov = NULL;
|
|
|
|
req->in_num = 0;
|
|
|
|
req->in_len = 0;
|
2023-03-01 13:58:38 +03:00
|
|
|
req->flags = QCRYPTODEV_BACKEND_ALG__MAX;
|
2022-06-11 09:42:43 +03:00
|
|
|
memset(&req->op_info, 0x00, sizeof(req->op_info));
|
2016-10-28 11:33:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_free_request(VirtIOCryptoReq *req)
|
|
|
|
{
|
2022-06-11 09:42:43 +03:00
|
|
|
if (!req) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-01 13:58:38 +03:00
|
|
|
if (req->flags == QCRYPTODEV_BACKEND_ALG_SYM) {
|
2022-06-11 09:42:43 +03:00
|
|
|
size_t max_len;
|
|
|
|
CryptoDevBackendSymOpInfo *op_info = req->op_info.u.sym_op_info;
|
|
|
|
|
2023-05-09 10:53:17 +03:00
|
|
|
if (op_info) {
|
|
|
|
max_len = op_info->iv_len +
|
|
|
|
op_info->aad_len +
|
|
|
|
op_info->src_len +
|
|
|
|
op_info->dst_len +
|
|
|
|
op_info->digest_result_len;
|
|
|
|
|
|
|
|
/* Zeroize and free request data structure */
|
|
|
|
memset(op_info, 0, sizeof(*op_info) + max_len);
|
|
|
|
g_free(op_info);
|
|
|
|
}
|
2023-03-01 13:58:38 +03:00
|
|
|
} else if (req->flags == QCRYPTODEV_BACKEND_ALG_ASYM) {
|
2022-06-11 09:42:43 +03:00
|
|
|
CryptoDevBackendAsymOpInfo *op_info = req->op_info.u.asym_op_info;
|
|
|
|
if (op_info) {
|
|
|
|
g_free(op_info->src);
|
|
|
|
g_free(op_info->dst);
|
|
|
|
memset(op_info, 0, sizeof(*op_info));
|
2016-12-22 06:01:28 +03:00
|
|
|
g_free(op_info);
|
2016-10-28 11:33:28 +03:00
|
|
|
}
|
|
|
|
}
|
2022-06-11 09:42:43 +03:00
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
g_free(req->in_iov);
|
2022-06-11 09:42:43 +03:00
|
|
|
g_free(req);
|
2016-10-28 11:33:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virtio_crypto_sym_input_data_helper(VirtIODevice *vdev,
|
|
|
|
VirtIOCryptoReq *req,
|
|
|
|
uint32_t status,
|
|
|
|
CryptoDevBackendSymOpInfo *sym_op_info)
|
|
|
|
{
|
|
|
|
size_t s, len;
|
2022-10-08 11:50:27 +03:00
|
|
|
struct iovec *in_iov = req->in_iov;
|
2016-10-28 11:33:28 +03:00
|
|
|
|
|
|
|
if (status != VIRTIO_CRYPTO_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-22 06:37:03 +03:00
|
|
|
len = sym_op_info->src_len;
|
2016-10-28 11:33:28 +03:00
|
|
|
/* Save the cipher result */
|
2022-10-08 11:50:27 +03:00
|
|
|
s = iov_from_buf(in_iov, req->in_num, 0, sym_op_info->dst, len);
|
2016-10-28 11:33:28 +03:00
|
|
|
if (s != len) {
|
|
|
|
virtio_error(vdev, "virtio-crypto dest data incorrect");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
iov_discard_front(&in_iov, &req->in_num, len);
|
2016-10-28 11:33:28 +03:00
|
|
|
|
|
|
|
if (sym_op_info->op_type ==
|
|
|
|
VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
|
|
|
|
/* Save the digest result */
|
2022-10-08 11:50:27 +03:00
|
|
|
s = iov_from_buf(in_iov, req->in_num, 0,
|
2016-10-28 11:33:28 +03:00
|
|
|
sym_op_info->digest_result,
|
|
|
|
sym_op_info->digest_result_len);
|
|
|
|
if (s != sym_op_info->digest_result_len) {
|
|
|
|
virtio_error(vdev, "virtio-crypto digest result incorrect");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 09:42:43 +03:00
|
|
|
static void
|
|
|
|
virtio_crypto_akcipher_input_data_helper(VirtIODevice *vdev,
|
|
|
|
VirtIOCryptoReq *req, int32_t status,
|
|
|
|
CryptoDevBackendAsymOpInfo *asym_op_info)
|
|
|
|
{
|
|
|
|
size_t s, len;
|
2022-10-08 11:50:27 +03:00
|
|
|
struct iovec *in_iov = req->in_iov;
|
2022-06-11 09:42:43 +03:00
|
|
|
|
|
|
|
if (status != VIRTIO_CRYPTO_OK) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = asym_op_info->dst_len;
|
|
|
|
if (!len) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
s = iov_from_buf(in_iov, req->in_num, 0, asym_op_info->dst, len);
|
2022-06-11 09:42:43 +03:00
|
|
|
if (s != len) {
|
|
|
|
virtio_error(vdev, "virtio-crypto asym dest data incorrect");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
iov_discard_front(&in_iov, &req->in_num, len);
|
2022-06-11 09:42:43 +03:00
|
|
|
|
|
|
|
/* For akcipher, dst_len may be changed after operation */
|
|
|
|
req->in_len = sizeof(struct virtio_crypto_inhdr) + asym_op_info->dst_len;
|
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
static void virtio_crypto_req_complete(void *opaque, int ret)
|
2016-10-28 11:33:28 +03:00
|
|
|
{
|
2022-10-08 11:50:27 +03:00
|
|
|
VirtIOCryptoReq *req = (VirtIOCryptoReq *)opaque;
|
2016-10-28 11:33:28 +03:00
|
|
|
VirtIOCrypto *vcrypto = req->vcrypto;
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
|
2022-10-08 11:50:27 +03:00
|
|
|
uint8_t status = -ret;
|
2016-10-28 11:33:28 +03:00
|
|
|
|
2023-03-01 13:58:38 +03:00
|
|
|
if (req->flags == QCRYPTODEV_BACKEND_ALG_SYM) {
|
2016-10-28 11:33:28 +03:00
|
|
|
virtio_crypto_sym_input_data_helper(vdev, req, status,
|
2022-06-11 09:42:43 +03:00
|
|
|
req->op_info.u.sym_op_info);
|
2023-03-01 13:58:38 +03:00
|
|
|
} else if (req->flags == QCRYPTODEV_BACKEND_ALG_ASYM) {
|
2022-06-11 09:42:43 +03:00
|
|
|
virtio_crypto_akcipher_input_data_helper(vdev, req, status,
|
|
|
|
req->op_info.u.asym_op_info);
|
2016-10-28 11:33:28 +03:00
|
|
|
}
|
|
|
|
stb_p(&req->in->status, status);
|
|
|
|
virtqueue_push(req->vq, &req->elem, req->in_len);
|
|
|
|
virtio_notify(vdev, req->vq);
|
2022-10-08 11:50:27 +03:00
|
|
|
virtio_crypto_free_request(req);
|
2016-10-28 11:33:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VirtIOCryptoReq *
|
|
|
|
virtio_crypto_get_request(VirtIOCrypto *s, VirtQueue *vq)
|
|
|
|
{
|
|
|
|
VirtIOCryptoReq *req = virtqueue_pop(vq, sizeof(VirtIOCryptoReq));
|
|
|
|
|
|
|
|
if (req) {
|
|
|
|
virtio_crypto_init_request(s, vq, req);
|
|
|
|
}
|
|
|
|
return req;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CryptoDevBackendSymOpInfo *
|
|
|
|
virtio_crypto_sym_op_helper(VirtIODevice *vdev,
|
|
|
|
struct virtio_crypto_cipher_para *cipher_para,
|
|
|
|
struct virtio_crypto_alg_chain_data_para *alg_chain_para,
|
|
|
|
struct iovec *iov, unsigned int out_num)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
CryptoDevBackendSymOpInfo *op_info;
|
|
|
|
uint32_t src_len = 0, dst_len = 0;
|
|
|
|
uint32_t iv_len = 0;
|
|
|
|
uint32_t aad_len = 0, hash_result_len = 0;
|
|
|
|
uint32_t hash_start_src_offset = 0, len_to_hash = 0;
|
|
|
|
uint32_t cipher_start_src_offset = 0, len_to_cipher = 0;
|
|
|
|
|
2017-01-03 09:50:03 +03:00
|
|
|
uint64_t max_len, curr_size = 0;
|
2016-10-28 11:33:28 +03:00
|
|
|
size_t s;
|
|
|
|
|
|
|
|
/* Plain cipher */
|
|
|
|
if (cipher_para) {
|
|
|
|
iv_len = ldl_le_p(&cipher_para->iv_len);
|
|
|
|
src_len = ldl_le_p(&cipher_para->src_data_len);
|
|
|
|
dst_len = ldl_le_p(&cipher_para->dst_data_len);
|
|
|
|
} else if (alg_chain_para) { /* Algorithm chain */
|
|
|
|
iv_len = ldl_le_p(&alg_chain_para->iv_len);
|
|
|
|
src_len = ldl_le_p(&alg_chain_para->src_data_len);
|
|
|
|
dst_len = ldl_le_p(&alg_chain_para->dst_data_len);
|
|
|
|
|
|
|
|
aad_len = ldl_le_p(&alg_chain_para->aad_len);
|
|
|
|
hash_result_len = ldl_le_p(&alg_chain_para->hash_result_len);
|
|
|
|
hash_start_src_offset = ldl_le_p(
|
|
|
|
&alg_chain_para->hash_start_src_offset);
|
|
|
|
cipher_start_src_offset = ldl_le_p(
|
|
|
|
&alg_chain_para->cipher_start_src_offset);
|
|
|
|
len_to_cipher = ldl_le_p(&alg_chain_para->len_to_cipher);
|
|
|
|
len_to_hash = ldl_le_p(&alg_chain_para->len_to_hash);
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-01-03 09:50:03 +03:00
|
|
|
max_len = (uint64_t)iv_len + aad_len + src_len + dst_len + hash_result_len;
|
2016-10-28 11:33:28 +03:00
|
|
|
if (unlikely(max_len > vcrypto->conf.max_size)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto too big length");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
op_info = g_malloc0(sizeof(CryptoDevBackendSymOpInfo) + max_len);
|
|
|
|
op_info->iv_len = iv_len;
|
|
|
|
op_info->src_len = src_len;
|
|
|
|
op_info->dst_len = dst_len;
|
|
|
|
op_info->aad_len = aad_len;
|
|
|
|
op_info->digest_result_len = hash_result_len;
|
|
|
|
op_info->hash_start_src_offset = hash_start_src_offset;
|
|
|
|
op_info->len_to_hash = len_to_hash;
|
|
|
|
op_info->cipher_start_src_offset = cipher_start_src_offset;
|
|
|
|
op_info->len_to_cipher = len_to_cipher;
|
|
|
|
/* Handle the initilization vector */
|
|
|
|
if (op_info->iv_len > 0) {
|
|
|
|
DPRINTF("iv_len=%" PRIu32 "\n", op_info->iv_len);
|
|
|
|
op_info->iv = op_info->data + curr_size;
|
|
|
|
|
|
|
|
s = iov_to_buf(iov, out_num, 0, op_info->iv, op_info->iv_len);
|
|
|
|
if (unlikely(s != op_info->iv_len)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto iv incorrect");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
iov_discard_front(&iov, &out_num, op_info->iv_len);
|
|
|
|
curr_size += op_info->iv_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle additional authentication data if exists */
|
|
|
|
if (op_info->aad_len > 0) {
|
|
|
|
DPRINTF("aad_len=%" PRIu32 "\n", op_info->aad_len);
|
|
|
|
op_info->aad_data = op_info->data + curr_size;
|
|
|
|
|
|
|
|
s = iov_to_buf(iov, out_num, 0, op_info->aad_data, op_info->aad_len);
|
|
|
|
if (unlikely(s != op_info->aad_len)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto additional auth data incorrect");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
iov_discard_front(&iov, &out_num, op_info->aad_len);
|
|
|
|
|
|
|
|
curr_size += op_info->aad_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the source data */
|
|
|
|
if (op_info->src_len > 0) {
|
|
|
|
DPRINTF("src_len=%" PRIu32 "\n", op_info->src_len);
|
|
|
|
op_info->src = op_info->data + curr_size;
|
|
|
|
|
|
|
|
s = iov_to_buf(iov, out_num, 0, op_info->src, op_info->src_len);
|
|
|
|
if (unlikely(s != op_info->src_len)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto source data incorrect");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
iov_discard_front(&iov, &out_num, op_info->src_len);
|
|
|
|
|
|
|
|
curr_size += op_info->src_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the destination data */
|
|
|
|
op_info->dst = op_info->data + curr_size;
|
|
|
|
curr_size += op_info->dst_len;
|
|
|
|
|
|
|
|
DPRINTF("dst_len=%" PRIu32 "\n", op_info->dst_len);
|
|
|
|
|
|
|
|
/* Handle the hash digest result */
|
|
|
|
if (hash_result_len > 0) {
|
|
|
|
DPRINTF("hash_result_len=%" PRIu32 "\n", hash_result_len);
|
|
|
|
op_info->digest_result = op_info->data + curr_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return op_info;
|
|
|
|
|
|
|
|
err:
|
|
|
|
g_free(op_info);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
virtio_crypto_handle_sym_req(VirtIOCrypto *vcrypto,
|
|
|
|
struct virtio_crypto_sym_data_req *req,
|
2022-06-11 09:42:43 +03:00
|
|
|
CryptoDevBackendOpInfo *op_info,
|
2016-10-28 11:33:28 +03:00
|
|
|
struct iovec *iov, unsigned int out_num)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
|
2022-06-11 09:42:43 +03:00
|
|
|
CryptoDevBackendSymOpInfo *sym_op_info;
|
2016-10-28 11:33:28 +03:00
|
|
|
uint32_t op_type;
|
|
|
|
|
|
|
|
op_type = ldl_le_p(&req->op_type);
|
|
|
|
if (op_type == VIRTIO_CRYPTO_SYM_OP_CIPHER) {
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_op_info = virtio_crypto_sym_op_helper(vdev, &req->u.cipher.para,
|
2016-10-28 11:33:28 +03:00
|
|
|
NULL, iov, out_num);
|
2022-06-11 09:42:43 +03:00
|
|
|
if (!sym_op_info) {
|
2016-10-28 11:33:28 +03:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
} else if (op_type == VIRTIO_CRYPTO_SYM_OP_ALGORITHM_CHAINING) {
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_op_info = virtio_crypto_sym_op_helper(vdev, NULL,
|
2016-10-28 11:33:28 +03:00
|
|
|
&req->u.chain.para,
|
|
|
|
iov, out_num);
|
2022-06-11 09:42:43 +03:00
|
|
|
if (!sym_op_info) {
|
2016-10-28 11:33:28 +03:00
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* VIRTIO_CRYPTO_SYM_OP_NONE */
|
|
|
|
error_report("virtio-crypto unsupported cipher type");
|
|
|
|
return -VIRTIO_CRYPTO_NOTSUPP;
|
|
|
|
}
|
|
|
|
|
2022-06-11 09:42:43 +03:00
|
|
|
sym_op_info->op_type = op_type;
|
|
|
|
op_info->u.sym_op_info = sym_op_info;
|
2016-10-28 11:33:28 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-06-11 09:42:43 +03:00
|
|
|
static int
|
|
|
|
virtio_crypto_handle_asym_req(VirtIOCrypto *vcrypto,
|
|
|
|
struct virtio_crypto_akcipher_data_req *req,
|
|
|
|
CryptoDevBackendOpInfo *op_info,
|
|
|
|
struct iovec *iov, unsigned int out_num)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
|
|
|
|
CryptoDevBackendAsymOpInfo *asym_op_info;
|
|
|
|
uint32_t src_len;
|
|
|
|
uint32_t dst_len;
|
|
|
|
uint32_t len;
|
|
|
|
uint8_t *src = NULL;
|
|
|
|
uint8_t *dst = NULL;
|
|
|
|
|
2022-09-23 11:42:54 +03:00
|
|
|
asym_op_info = g_new0(CryptoDevBackendAsymOpInfo, 1);
|
2022-06-11 09:42:43 +03:00
|
|
|
src_len = ldl_le_p(&req->para.src_data_len);
|
|
|
|
dst_len = ldl_le_p(&req->para.dst_data_len);
|
|
|
|
|
|
|
|
if (src_len > 0) {
|
|
|
|
src = g_malloc0(src_len);
|
|
|
|
len = iov_to_buf(iov, out_num, 0, src, src_len);
|
|
|
|
if (unlikely(len != src_len)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto asym src data incorrect"
|
|
|
|
"expected %u, actual %u", src_len, len);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov_discard_front(&iov, &out_num, src_len);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dst_len > 0) {
|
|
|
|
dst = g_malloc0(dst_len);
|
|
|
|
|
|
|
|
if (op_info->op_code == VIRTIO_CRYPTO_AKCIPHER_VERIFY) {
|
|
|
|
len = iov_to_buf(iov, out_num, 0, dst, dst_len);
|
|
|
|
if (unlikely(len != dst_len)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto asym dst data incorrect"
|
|
|
|
"expected %u, actual %u", dst_len, len);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
iov_discard_front(&iov, &out_num, dst_len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
asym_op_info->src_len = src_len;
|
|
|
|
asym_op_info->dst_len = dst_len;
|
|
|
|
asym_op_info->src = src;
|
|
|
|
asym_op_info->dst = dst;
|
|
|
|
op_info->u.asym_op_info = asym_op_info;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
g_free(asym_op_info);
|
|
|
|
g_free(src);
|
|
|
|
g_free(dst);
|
|
|
|
|
|
|
|
return -EFAULT;
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:28 +03:00
|
|
|
static int
|
|
|
|
virtio_crypto_handle_request(VirtIOCryptoReq *request)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = request->vcrypto;
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
|
|
|
|
VirtQueueElement *elem = &request->elem;
|
|
|
|
int queue_index = virtio_crypto_vq2q(virtio_get_queue_index(request->vq));
|
|
|
|
struct virtio_crypto_op_data_req req;
|
|
|
|
int ret;
|
2020-09-17 12:44:55 +03:00
|
|
|
g_autofree struct iovec *in_iov_copy = NULL;
|
|
|
|
g_autofree struct iovec *out_iov_copy = NULL;
|
2016-10-28 11:33:28 +03:00
|
|
|
struct iovec *in_iov;
|
|
|
|
struct iovec *out_iov;
|
|
|
|
unsigned in_num;
|
|
|
|
unsigned out_num;
|
|
|
|
uint32_t opcode;
|
2022-06-11 09:42:43 +03:00
|
|
|
CryptoDevBackendOpInfo *op_info = &request->op_info;
|
2016-10-28 11:33:28 +03:00
|
|
|
|
|
|
|
if (elem->out_num < 1 || elem->in_num < 1) {
|
|
|
|
virtio_error(vdev, "virtio-crypto dataq missing headers");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
out_num = elem->out_num;
|
2022-05-12 20:57:46 +03:00
|
|
|
out_iov_copy = g_memdup2(elem->out_sg, sizeof(out_iov[0]) * out_num);
|
2020-09-17 12:44:55 +03:00
|
|
|
out_iov = out_iov_copy;
|
|
|
|
|
2016-10-28 11:33:28 +03:00
|
|
|
in_num = elem->in_num;
|
2022-05-12 20:57:46 +03:00
|
|
|
in_iov_copy = g_memdup2(elem->in_sg, sizeof(in_iov[0]) * in_num);
|
2020-09-17 12:44:55 +03:00
|
|
|
in_iov = in_iov_copy;
|
|
|
|
|
2016-10-28 11:33:28 +03:00
|
|
|
if (unlikely(iov_to_buf(out_iov, out_num, 0, &req, sizeof(req))
|
|
|
|
!= sizeof(req))) {
|
|
|
|
virtio_error(vdev, "virtio-crypto request outhdr too short");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
iov_discard_front(&out_iov, &out_num, sizeof(req));
|
|
|
|
|
|
|
|
if (in_iov[in_num - 1].iov_len <
|
|
|
|
sizeof(struct virtio_crypto_inhdr)) {
|
|
|
|
virtio_error(vdev, "virtio-crypto request inhdr too short");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* We always touch the last byte, so just see how big in_iov is. */
|
|
|
|
request->in_len = iov_size(in_iov, in_num);
|
|
|
|
request->in = (void *)in_iov[in_num - 1].iov_base
|
|
|
|
+ in_iov[in_num - 1].iov_len
|
|
|
|
- sizeof(struct virtio_crypto_inhdr);
|
|
|
|
iov_discard_back(in_iov, &in_num, sizeof(struct virtio_crypto_inhdr));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The length of operation result, including dest_data
|
|
|
|
* and digest_result if exists.
|
|
|
|
*/
|
|
|
|
request->in_num = in_num;
|
|
|
|
request->in_iov = in_iov;
|
2022-10-08 11:50:27 +03:00
|
|
|
/* now, we free the in_iov_copy inside virtio_crypto_free_request */
|
|
|
|
in_iov_copy = NULL;
|
2016-10-28 11:33:28 +03:00
|
|
|
|
|
|
|
opcode = ldl_le_p(&req.header.opcode);
|
2022-06-11 09:42:43 +03:00
|
|
|
op_info->session_id = ldq_le_p(&req.header.session_id);
|
|
|
|
op_info->op_code = opcode;
|
2023-03-01 13:58:43 +03:00
|
|
|
op_info->queue_index = queue_index;
|
|
|
|
op_info->cb = virtio_crypto_req_complete;
|
|
|
|
op_info->opaque = request;
|
2016-10-28 11:33:28 +03:00
|
|
|
|
|
|
|
switch (opcode) {
|
|
|
|
case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
|
|
|
|
case VIRTIO_CRYPTO_CIPHER_DECRYPT:
|
2023-03-01 13:58:38 +03:00
|
|
|
op_info->algtype = request->flags = QCRYPTODEV_BACKEND_ALG_SYM;
|
2016-10-28 11:33:28 +03:00
|
|
|
ret = virtio_crypto_handle_sym_req(vcrypto,
|
2022-06-11 09:42:43 +03:00
|
|
|
&req.u.sym_req, op_info,
|
|
|
|
out_iov, out_num);
|
|
|
|
goto check_result;
|
|
|
|
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_SIGN:
|
|
|
|
case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
|
2023-03-01 13:58:38 +03:00
|
|
|
op_info->algtype = request->flags = QCRYPTODEV_BACKEND_ALG_ASYM;
|
2022-06-11 09:42:43 +03:00
|
|
|
ret = virtio_crypto_handle_asym_req(vcrypto,
|
|
|
|
&req.u.akcipher_req, op_info,
|
2016-10-28 11:33:28 +03:00
|
|
|
out_iov, out_num);
|
2022-06-11 09:42:43 +03:00
|
|
|
|
|
|
|
check_result:
|
2016-10-28 11:33:28 +03:00
|
|
|
/* Serious errors, need to reset virtio crypto device */
|
|
|
|
if (ret == -EFAULT) {
|
|
|
|
return -1;
|
|
|
|
} else if (ret == -VIRTIO_CRYPTO_NOTSUPP) {
|
2022-10-08 11:50:27 +03:00
|
|
|
virtio_crypto_req_complete(request, -VIRTIO_CRYPTO_NOTSUPP);
|
2016-10-28 11:33:28 +03:00
|
|
|
} else {
|
2016-10-28 11:33:29 +03:00
|
|
|
ret = cryptodev_backend_crypto_operation(vcrypto->cryptodev,
|
2023-03-01 13:58:43 +03:00
|
|
|
op_info);
|
2016-10-28 11:33:28 +03:00
|
|
|
if (ret < 0) {
|
2022-10-08 11:50:27 +03:00
|
|
|
virtio_crypto_req_complete(request, ret);
|
2016-10-28 11:33:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-06-11 09:42:43 +03:00
|
|
|
|
2016-10-28 11:33:28 +03:00
|
|
|
case VIRTIO_CRYPTO_HASH:
|
|
|
|
case VIRTIO_CRYPTO_MAC:
|
|
|
|
case VIRTIO_CRYPTO_AEAD_ENCRYPT:
|
|
|
|
case VIRTIO_CRYPTO_AEAD_DECRYPT:
|
|
|
|
default:
|
|
|
|
error_report("virtio-crypto unsupported dataq opcode: %u",
|
|
|
|
opcode);
|
2022-10-08 11:50:27 +03:00
|
|
|
virtio_crypto_req_complete(request, -VIRTIO_CRYPTO_NOTSUPP);
|
2016-10-28 11:33:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_handle_dataq(VirtIODevice *vdev, VirtQueue *vq)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
VirtIOCryptoReq *req;
|
|
|
|
|
|
|
|
while ((req = virtio_crypto_get_request(vcrypto, vq))) {
|
|
|
|
if (virtio_crypto_handle_request(req) < 0) {
|
|
|
|
virtqueue_detach_element(req->vq, &req->elem, 0);
|
|
|
|
virtio_crypto_free_request(req);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:30 +03:00
|
|
|
static void virtio_crypto_dataq_bh(void *opaque)
|
|
|
|
{
|
|
|
|
VirtIOCryptoQueue *q = opaque;
|
|
|
|
VirtIOCrypto *vcrypto = q->vcrypto;
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
|
|
|
|
|
|
|
|
/* This happens when device was stopped but BH wasn't. */
|
|
|
|
if (!vdev->vm_running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Just in case the driver is not ready on more */
|
|
|
|
if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-11-16 23:17:32 +03:00
|
|
|
for (;;) {
|
|
|
|
virtio_crypto_handle_dataq(vdev, q->dataq);
|
|
|
|
virtio_queue_set_notification(q->dataq, 1);
|
|
|
|
|
|
|
|
/* Are we done or did the guest add more buffers? */
|
|
|
|
if (virtio_queue_empty(q->dataq)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtio_queue_set_notification(q->dataq, 0);
|
|
|
|
}
|
2016-10-28 11:33:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
virtio_crypto_handle_dataq_bh(VirtIODevice *vdev, VirtQueue *vq)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
VirtIOCryptoQueue *q =
|
|
|
|
&vcrypto->vqs[virtio_crypto_vq2q(virtio_get_queue_index(vq))];
|
|
|
|
|
|
|
|
/* This happens when device was stopped but VCPU wasn't. */
|
|
|
|
if (!vdev->vm_running) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
virtio_queue_set_notification(vq, 0);
|
|
|
|
qemu_bh_schedule(q->dataq_bh);
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:24 +03:00
|
|
|
static uint64_t virtio_crypto_get_features(VirtIODevice *vdev,
|
|
|
|
uint64_t features,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
return features;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_reset(VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
/* multiqueue is disabled by default */
|
|
|
|
vcrypto->curr_queues = 1;
|
2016-12-22 06:12:39 +03:00
|
|
|
if (!cryptodev_backend_is_ready(vcrypto->cryptodev)) {
|
2016-10-28 11:33:24 +03:00
|
|
|
vcrypto->status &= ~VIRTIO_CRYPTO_S_HW_READY;
|
|
|
|
} else {
|
|
|
|
vcrypto->status |= VIRTIO_CRYPTO_S_HW_READY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-01 13:58:39 +03:00
|
|
|
static uint32_t virtio_crypto_init_services(uint32_t qservices)
|
|
|
|
{
|
|
|
|
uint32_t vservices = 0;
|
|
|
|
|
|
|
|
if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_CIPHER)) {
|
|
|
|
vservices |= (1 << VIRTIO_CRYPTO_SERVICE_CIPHER);
|
|
|
|
}
|
|
|
|
if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_HASH)) {
|
|
|
|
vservices |= (1 << VIRTIO_CRYPTO_SERVICE_HASH);
|
|
|
|
}
|
|
|
|
if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_MAC)) {
|
|
|
|
vservices |= (1 << VIRTIO_CRYPTO_SERVICE_MAC);
|
|
|
|
}
|
|
|
|
if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_AEAD)) {
|
|
|
|
vservices |= (1 << VIRTIO_CRYPTO_SERVICE_AEAD);
|
|
|
|
}
|
|
|
|
if (qservices & (1 << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER)) {
|
|
|
|
vservices |= (1 << VIRTIO_CRYPTO_SERVICE_AKCIPHER);
|
|
|
|
}
|
|
|
|
|
|
|
|
return vservices;
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:26 +03:00
|
|
|
static void virtio_crypto_init_config(VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
|
2023-03-01 13:58:39 +03:00
|
|
|
vcrypto->conf.crypto_services = virtio_crypto_init_services(
|
|
|
|
vcrypto->conf.cryptodev->conf.crypto_services);
|
2016-10-28 11:33:26 +03:00
|
|
|
vcrypto->conf.cipher_algo_l =
|
|
|
|
vcrypto->conf.cryptodev->conf.cipher_algo_l;
|
|
|
|
vcrypto->conf.cipher_algo_h =
|
|
|
|
vcrypto->conf.cryptodev->conf.cipher_algo_h;
|
|
|
|
vcrypto->conf.hash_algo = vcrypto->conf.cryptodev->conf.hash_algo;
|
|
|
|
vcrypto->conf.mac_algo_l = vcrypto->conf.cryptodev->conf.mac_algo_l;
|
|
|
|
vcrypto->conf.mac_algo_h = vcrypto->conf.cryptodev->conf.mac_algo_h;
|
|
|
|
vcrypto->conf.aead_algo = vcrypto->conf.cryptodev->conf.aead_algo;
|
2022-06-11 09:42:43 +03:00
|
|
|
vcrypto->conf.akcipher_algo = vcrypto->conf.cryptodev->conf.akcipher_algo;
|
2016-10-28 11:33:26 +03:00
|
|
|
vcrypto->conf.max_cipher_key_len =
|
|
|
|
vcrypto->conf.cryptodev->conf.max_cipher_key_len;
|
|
|
|
vcrypto->conf.max_auth_key_len =
|
|
|
|
vcrypto->conf.cryptodev->conf.max_auth_key_len;
|
|
|
|
vcrypto->conf.max_size = vcrypto->conf.cryptodev->conf.max_size;
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:24 +03:00
|
|
|
static void virtio_crypto_device_realize(DeviceState *dev, Error **errp)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
vcrypto->cryptodev = vcrypto->conf.cryptodev;
|
|
|
|
if (vcrypto->cryptodev == NULL) {
|
|
|
|
error_setg(errp, "'cryptodev' parameter expects a valid object");
|
|
|
|
return;
|
2017-07-14 05:14:58 +03:00
|
|
|
} else if (cryptodev_backend_is_used(vcrypto->cryptodev)) {
|
2020-07-14 19:02:00 +03:00
|
|
|
error_setg(errp, "can't use already used cryptodev backend: %s",
|
|
|
|
object_get_canonical_path_component(OBJECT(vcrypto->conf.cryptodev)));
|
2017-07-14 05:14:58 +03:00
|
|
|
return;
|
2016-10-28 11:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
vcrypto->max_queues = MAX(vcrypto->cryptodev->conf.peers.queues, 1);
|
|
|
|
if (vcrypto->max_queues + 1 > VIRTIO_QUEUE_MAX) {
|
|
|
|
error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
|
2016-11-19 22:29:26 +03:00
|
|
|
"must be a positive integer less than %d.",
|
2016-10-28 11:33:24 +03:00
|
|
|
vcrypto->max_queues, VIRTIO_QUEUE_MAX);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:23:18 +03:00
|
|
|
virtio_init(vdev, VIRTIO_ID_CRYPTO, vcrypto->config_size);
|
2016-10-28 11:33:24 +03:00
|
|
|
vcrypto->curr_queues = 1;
|
2022-03-15 17:41:56 +03:00
|
|
|
vcrypto->vqs = g_new0(VirtIOCryptoQueue, vcrypto->max_queues);
|
2016-10-28 11:33:24 +03:00
|
|
|
for (i = 0; i < vcrypto->max_queues; i++) {
|
2016-10-28 11:33:30 +03:00
|
|
|
vcrypto->vqs[i].dataq =
|
|
|
|
virtio_add_queue(vdev, 1024, virtio_crypto_handle_dataq_bh);
|
|
|
|
vcrypto->vqs[i].dataq_bh =
|
2023-04-28 00:10:09 +03:00
|
|
|
qemu_bh_new_guarded(virtio_crypto_dataq_bh, &vcrypto->vqs[i],
|
|
|
|
&dev->mem_reentrancy_guard);
|
2016-10-28 11:33:30 +03:00
|
|
|
vcrypto->vqs[i].vcrypto = vcrypto;
|
2016-10-28 11:33:24 +03:00
|
|
|
}
|
|
|
|
|
2022-10-08 11:50:27 +03:00
|
|
|
vcrypto->ctrl_vq = virtio_add_queue(vdev, 1024, virtio_crypto_handle_ctrl);
|
2016-12-22 06:12:39 +03:00
|
|
|
if (!cryptodev_backend_is_ready(vcrypto->cryptodev)) {
|
2016-10-28 11:33:24 +03:00
|
|
|
vcrypto->status &= ~VIRTIO_CRYPTO_S_HW_READY;
|
|
|
|
} else {
|
|
|
|
vcrypto->status |= VIRTIO_CRYPTO_S_HW_READY;
|
|
|
|
}
|
2016-10-28 11:33:26 +03:00
|
|
|
|
|
|
|
virtio_crypto_init_config(vdev);
|
2016-12-22 06:12:38 +03:00
|
|
|
cryptodev_backend_set_used(vcrypto->cryptodev, true);
|
2016-10-28 11:33:24 +03:00
|
|
|
}
|
|
|
|
|
qdev: Unrealize must not fail
Devices may have component devices and buses.
Device realization may fail. Realization is recursive: a device's
realize() method realizes its components, and device_set_realized()
realizes its buses (which should in turn realize the devices on that
bus, except bus_set_realized() doesn't implement that, yet).
When realization of a component or bus fails, we need to roll back:
unrealize everything we realized so far. If any of these unrealizes
failed, the device would be left in an inconsistent state. Must not
happen.
device_set_realized() lets it happen: it ignores errors in the roll
back code starting at label child_realize_fail.
Since realization is recursive, unrealization must be recursive, too.
But how could a partly failed unrealize be rolled back? We'd have to
re-realize, which can fail. This design is fundamentally broken.
device_set_realized() does not roll back at all. Instead, it keeps
unrealizing, ignoring further errors.
It can screw up even for a device with no buses: if the lone
dc->unrealize() fails, it still unregisters vmstate, and calls
listeners' unrealize() callback.
bus_set_realized() does not roll back either. Instead, it stops
unrealizing.
Fortunately, no unrealize method can fail, as we'll see below.
To fix the design error, drop parameter @errp from all the unrealize
methods.
Any unrealize method that uses @errp now needs an update. This leads
us to unrealize() methods that can fail. Merely passing it to another
unrealize method cannot cause failure, though. Here are the ones that
do other things with @errp:
* virtio_serial_device_unrealize()
Fails when qbus_set_hotplug_handler() fails, but still does all the
other work. On failure, the device would stay realized with its
resources completely gone. Oops. Can't happen, because
qbus_set_hotplug_handler() can't actually fail here. Pass
&error_abort to qbus_set_hotplug_handler() instead.
* hw/ppc/spapr_drc.c's unrealize()
Fails when object_property_del() fails, but all the other work is
already done. On failure, the device would stay realized with its
vmstate registration gone. Oops. Can't happen, because
object_property_del() can't actually fail here. Pass &error_abort
to object_property_del() instead.
* spapr_phb_unrealize()
Fails and bails out when remove_drcs() fails, but other work is
already done. On failure, the device would stay realized with some
of its resources gone. Oops. remove_drcs() fails only when
chassis_from_bus()'s object_property_get_uint() fails, and it can't
here. Pass &error_abort to remove_drcs() instead.
Therefore, no unrealize method can fail before this patch.
device_set_realized()'s recursive unrealization via bus uses
object_property_set_bool(). Can't drop @errp there, so pass
&error_abort.
We similarly unrealize with object_property_set_bool() elsewhere,
always ignoring errors. Pass &error_abort instead.
Several unrealize methods no longer handle errors from other unrealize
methods: virtio_9p_device_unrealize(),
virtio_input_device_unrealize(), scsi_qdev_unrealize(), ...
Much of the deleted error handling looks wrong anyway.
One unrealize methods no longer ignore such errors:
usb_ehci_pci_exit().
Several realize methods no longer ignore errors when rolling back:
v9fs_device_realize_common(), pci_qdev_unrealize(),
spapr_phb_realize(), usb_qdev_realize(), vfio_ccw_realize(),
virtio_device_realize().
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Philippe Mathieu-Daudé <philmd@redhat.com>
Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20200505152926.18877-17-armbru@redhat.com>
2020-05-05 18:29:24 +03:00
|
|
|
static void virtio_crypto_device_unrealize(DeviceState *dev)
|
2016-10-28 11:33:24 +03:00
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(dev);
|
2016-10-28 11:33:30 +03:00
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(dev);
|
|
|
|
VirtIOCryptoQueue *q;
|
|
|
|
int i, max_queues;
|
|
|
|
|
|
|
|
max_queues = vcrypto->multiqueue ? vcrypto->max_queues : 1;
|
|
|
|
for (i = 0; i < max_queues; i++) {
|
2020-02-25 10:55:54 +03:00
|
|
|
virtio_delete_queue(vcrypto->vqs[i].dataq);
|
2016-10-28 11:33:30 +03:00
|
|
|
q = &vcrypto->vqs[i];
|
|
|
|
qemu_bh_delete(q->dataq_bh);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(vcrypto->vqs);
|
2020-02-25 10:55:54 +03:00
|
|
|
virtio_delete_queue(vcrypto->ctrl_vq);
|
2016-10-28 11:33:24 +03:00
|
|
|
|
|
|
|
virtio_cleanup(vdev);
|
2016-12-22 06:12:38 +03:00
|
|
|
cryptodev_backend_set_used(vcrypto->cryptodev, false);
|
2016-10-28 11:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static const VMStateDescription vmstate_virtio_crypto = {
|
|
|
|
.name = "virtio-crypto",
|
2016-10-31 17:08:00 +03:00
|
|
|
.unmigratable = 1,
|
2016-10-28 11:33:24 +03:00
|
|
|
.minimum_version_id = VIRTIO_CRYPTO_VM_VERSION,
|
|
|
|
.version_id = VIRTIO_CRYPTO_VM_VERSION,
|
|
|
|
.fields = (VMStateField[]) {
|
|
|
|
VMSTATE_VIRTIO_DEVICE,
|
|
|
|
VMSTATE_END_OF_LIST()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static Property virtio_crypto_properties[] = {
|
2017-07-14 05:14:58 +03:00
|
|
|
DEFINE_PROP_LINK("cryptodev", VirtIOCrypto, conf.cryptodev,
|
|
|
|
TYPE_CRYPTODEV_BACKEND, CryptoDevBackend *),
|
2016-10-28 11:33:24 +03:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_crypto_get_config(VirtIODevice *vdev, uint8_t *config)
|
|
|
|
{
|
2016-10-28 11:33:26 +03:00
|
|
|
VirtIOCrypto *c = VIRTIO_CRYPTO(vdev);
|
2016-11-26 06:07:55 +03:00
|
|
|
struct virtio_crypto_config crypto_cfg = {};
|
2016-10-28 11:33:26 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Virtio-crypto device conforms to VIRTIO 1.0 which is always LE,
|
|
|
|
* so we can use LE accessors directly.
|
|
|
|
*/
|
|
|
|
stl_le_p(&crypto_cfg.status, c->status);
|
|
|
|
stl_le_p(&crypto_cfg.max_dataqueues, c->max_queues);
|
|
|
|
stl_le_p(&crypto_cfg.crypto_services, c->conf.crypto_services);
|
|
|
|
stl_le_p(&crypto_cfg.cipher_algo_l, c->conf.cipher_algo_l);
|
|
|
|
stl_le_p(&crypto_cfg.cipher_algo_h, c->conf.cipher_algo_h);
|
|
|
|
stl_le_p(&crypto_cfg.hash_algo, c->conf.hash_algo);
|
|
|
|
stl_le_p(&crypto_cfg.mac_algo_l, c->conf.mac_algo_l);
|
|
|
|
stl_le_p(&crypto_cfg.mac_algo_h, c->conf.mac_algo_h);
|
|
|
|
stl_le_p(&crypto_cfg.aead_algo, c->conf.aead_algo);
|
|
|
|
stl_le_p(&crypto_cfg.max_cipher_key_len, c->conf.max_cipher_key_len);
|
|
|
|
stl_le_p(&crypto_cfg.max_auth_key_len, c->conf.max_auth_key_len);
|
|
|
|
stq_le_p(&crypto_cfg.max_size, c->conf.max_size);
|
2022-06-11 09:42:43 +03:00
|
|
|
stl_le_p(&crypto_cfg.akcipher_algo, c->conf.akcipher_algo);
|
2016-10-28 11:33:26 +03:00
|
|
|
|
|
|
|
memcpy(config, &crypto_cfg, c->config_size);
|
2016-10-28 11:33:24 +03:00
|
|
|
}
|
|
|
|
|
2018-03-01 16:46:29 +03:00
|
|
|
static bool virtio_crypto_started(VirtIOCrypto *c, uint8_t status)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(c);
|
|
|
|
return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
|
|
|
|
(c->status & VIRTIO_CRYPTO_S_HW_READY) && vdev->vm_running;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_vhost_status(VirtIOCrypto *c, uint8_t status)
|
|
|
|
{
|
|
|
|
VirtIODevice *vdev = VIRTIO_DEVICE(c);
|
|
|
|
int queues = c->multiqueue ? c->max_queues : 1;
|
|
|
|
CryptoDevBackend *b = c->cryptodev;
|
|
|
|
CryptoDevBackendClient *cc = b->conf.peers.ccs[0];
|
|
|
|
|
|
|
|
if (!cryptodev_get_vhost(cc, b, 0)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((virtio_crypto_started(c, status)) == !!c->vhost_started) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!c->vhost_started) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
c->vhost_started = 1;
|
|
|
|
r = cryptodev_vhost_start(vdev, queues);
|
|
|
|
if (r < 0) {
|
|
|
|
error_report("unable to start vhost crypto: %d: "
|
|
|
|
"falling back on userspace virtio", -r);
|
|
|
|
c->vhost_started = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
cryptodev_vhost_stop(vdev, queues);
|
|
|
|
c->vhost_started = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_set_status(VirtIODevice *vdev, uint8_t status)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
|
|
|
|
virtio_crypto_vhost_status(vcrypto, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_guest_notifier_mask(VirtIODevice *vdev, int idx,
|
|
|
|
bool mask)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
int queue = virtio_crypto_vq2q(idx);
|
|
|
|
|
|
|
|
assert(vcrypto->vhost_started);
|
|
|
|
|
2022-12-22 10:04:42 +03:00
|
|
|
/*
|
|
|
|
* Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
|
|
|
|
* as the Marco of configure interrupt's IDX, If this driver does not
|
|
|
|
* support, the function will return
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (idx == VIRTIO_CONFIG_IRQ_IDX) {
|
|
|
|
return;
|
|
|
|
}
|
2018-03-01 16:46:29 +03:00
|
|
|
cryptodev_vhost_virtqueue_mask(vdev, queue, idx, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool virtio_crypto_guest_notifier_pending(VirtIODevice *vdev, int idx)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
int queue = virtio_crypto_vq2q(idx);
|
|
|
|
|
|
|
|
assert(vcrypto->vhost_started);
|
|
|
|
|
2022-12-22 10:04:42 +03:00
|
|
|
/*
|
|
|
|
* Add the check for configure interrupt, Use VIRTIO_CONFIG_IRQ_IDX -1
|
|
|
|
* as the Marco of configure interrupt's IDX, If this driver does not
|
|
|
|
* support, the function will return
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (idx == VIRTIO_CONFIG_IRQ_IDX) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-01 16:46:29 +03:00
|
|
|
return cryptodev_vhost_virtqueue_pending(vdev, queue, idx);
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:23:19 +03:00
|
|
|
static struct vhost_dev *virtio_crypto_get_vhost(VirtIODevice *vdev)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
|
|
|
|
CryptoDevBackend *b = vcrypto->cryptodev;
|
|
|
|
CryptoDevBackendClient *cc = b->conf.peers.ccs[0];
|
|
|
|
CryptoDevBackendVhost *vhost_crypto = cryptodev_get_vhost(cc, b, 0);
|
|
|
|
return &vhost_crypto->dev;
|
|
|
|
}
|
|
|
|
|
2016-10-28 11:33:24 +03:00
|
|
|
static void virtio_crypto_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
|
|
|
VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
|
|
|
|
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, virtio_crypto_properties);
|
2016-10-28 11:33:24 +03:00
|
|
|
dc->vmsd = &vmstate_virtio_crypto;
|
|
|
|
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
|
|
|
|
vdc->realize = virtio_crypto_device_realize;
|
|
|
|
vdc->unrealize = virtio_crypto_device_unrealize;
|
|
|
|
vdc->get_config = virtio_crypto_get_config;
|
|
|
|
vdc->get_features = virtio_crypto_get_features;
|
|
|
|
vdc->reset = virtio_crypto_reset;
|
2018-03-01 16:46:29 +03:00
|
|
|
vdc->set_status = virtio_crypto_set_status;
|
|
|
|
vdc->guest_notifier_mask = virtio_crypto_guest_notifier_mask;
|
|
|
|
vdc->guest_notifier_pending = virtio_crypto_guest_notifier_pending;
|
2022-04-01 16:23:19 +03:00
|
|
|
vdc->get_vhost = virtio_crypto_get_vhost;
|
2016-10-28 11:33:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void virtio_crypto_instance_init(Object *obj)
|
|
|
|
{
|
|
|
|
VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(obj);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The default config_size is sizeof(struct virtio_crypto_config).
|
|
|
|
* Can be overriden with virtio_crypto_set_config_size.
|
|
|
|
*/
|
|
|
|
vcrypto->config_size = sizeof(struct virtio_crypto_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const TypeInfo virtio_crypto_info = {
|
|
|
|
.name = TYPE_VIRTIO_CRYPTO,
|
|
|
|
.parent = TYPE_VIRTIO_DEVICE,
|
|
|
|
.instance_size = sizeof(VirtIOCrypto),
|
|
|
|
.instance_init = virtio_crypto_instance_init,
|
|
|
|
.class_init = virtio_crypto_class_init,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void virtio_register_types(void)
|
|
|
|
{
|
|
|
|
type_register_static(&virtio_crypto_info);
|
|
|
|
}
|
|
|
|
|
|
|
|
type_init(virtio_register_types)
|