We use an opaque point to the VirtIOCryptoReq which can support different packets based on different algorithms.
Signed-off-by: Gonglei <arei.gong...@huawei.com> --- crypto/cryptodev-gcrypt.c | 2 +- crypto/cryptodev.c | 28 ++++++++++++++++++++++++++-- hw/virtio/virtio-crypto.c | 10 +++++----- include/crypto/cryptodev.h | 13 +++++++------ 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/crypto/cryptodev-gcrypt.c b/crypto/cryptodev-gcrypt.c index 66a0e5e..413afa9 100644 --- a/crypto/cryptodev-gcrypt.c +++ b/crypto/cryptodev-gcrypt.c @@ -266,7 +266,7 @@ static int qcrypto_cryptodev_backend_gcrypt_sym_operation( return -VIRTIO_CRYPTO_OP_ERR; } } - return 0; + return VIRTIO_CRYPTO_OP_OK; } static void qcrypto_cryptodev_backend_gcrypt_cleanup( diff --git a/crypto/cryptodev.c b/crypto/cryptodev.c index 0b3d55e..23c389b 100644 --- a/crypto/cryptodev.c +++ b/crypto/cryptodev.c @@ -30,6 +30,8 @@ #include "qapi-visit.h" #include "qemu/config-file.h" #include "qom/object_interfaces.h" +#include "hw/virtio/virtio-crypto.h" + static QTAILQ_HEAD(, QCryptoCryptoDevBackendClientState) crypto_clients; @@ -103,7 +105,7 @@ int qcrypto_cryptodev_backend_sym_close_session( return -1; } -int qcrypto_cryptodev_backend_sym_operation( +static int qcrypto_cryptodev_backend_sym_operation( QCryptoCryptoDevBackend *backend, QCryptoCryptoDevBackendSymOpInfo *op_info, uint32_t queue_index, Error **errp) @@ -115,7 +117,29 @@ int qcrypto_cryptodev_backend_sym_operation( return bc->do_sym_op(backend, op_info, queue_index, errp); } - return -1; + return -VIRTIO_CRYPTO_OP_ERR; +} + +int qcrypto_cryptodev_backend_crypto_operation( + QCryptoCryptoDevBackend *backend, + void *opaque, + uint32_t queue_index, Error **errp) +{ + VirtIOCryptoReq *req = opaque; + + if (req->flags == QCRYPTO_CRYPTODEV_BACKEND_ALG_SYM) { + QCryptoCryptoDevBackendSymOpInfo *op_info; + op_info = req->u.sym_op_info; + + return qcrypto_cryptodev_backend_sym_operation(backend, + op_info, queue_index, errp); + } else { + error_setg(errp, "Unsupported cryptodev alg type: %" PRIu32 "", + req->flags); + return -VIRTIO_CRYPTO_OP_NOTSUPP; + } + + return -VIRTIO_CRYPTO_OP_ERR; } static void diff --git a/hw/virtio/virtio-crypto.c b/hw/virtio/virtio-crypto.c index a82a06e..bee5a61 100644 --- a/hw/virtio/virtio-crypto.c +++ b/hw/virtio/virtio-crypto.c @@ -670,15 +670,15 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request) request->flags = QCRYPTO_CRYPTODEV_BACKEND_ALG_SYM; request->u.sym_op_info = sym_op_info; request->idata_hva = idata_hva; - ret = qcrypto_cryptodev_backend_sym_operation(vcrypto->cryptodev, - sym_op_info, queue_index, &local_err); + ret = qcrypto_cryptodev_backend_crypto_operation(vcrypto->cryptodev, + request, queue_index, &local_err); if (ret < 0) { - status = VIRTIO_CRYPTO_OP_ERR; + status = -ret; if (local_err) { error_report_err(local_err); } - } else { /* ret >= 0 */ - status = VIRTIO_CRYPTO_OP_OK; + } else { /* ret == VIRTIO_CRYPTO_OP_OK */ + status = ret; } virtio_crypto_req_complete(request, status); virtio_crypto_free_request(request); diff --git a/include/crypto/cryptodev.h b/include/crypto/cryptodev.h index 7fcdc2f..8dbf613 100644 --- a/include/crypto/cryptodev.h +++ b/include/crypto/cryptodev.h @@ -256,20 +256,21 @@ int qcrypto_cryptodev_backend_sym_close_session( uint64_t session_id, Error **errp); /** - * qcrypto_cryptodev_backend_sym_operation: + * qcrypto_cryptodev_backend_crypto_operation: * @backend: the cryptodev backend object - * @op_info: parameters needed by symmetric crypto operation + * @opaque: pointer to a VirtIOCryptoReq object * @queue_index: queue index of cryptodev backend client * @errp: pointer to a NULL-initialized error object * - * Do symmetric crypto operation, such as encryption and + * Do crypto operation, such as encryption and * decryption * - * Returns: 0 on success, or Negative on error + * Returns: VIRTIO_CRYPTO_OP_OK on success, + * or -VIRTIO_CRYPTO_OP_* on error */ -int qcrypto_cryptodev_backend_sym_operation( +int qcrypto_cryptodev_backend_crypto_operation( QCryptoCryptoDevBackend *backend, - QCryptoCryptoDevBackendSymOpInfo *op_info, + void *opaque, uint32_t queue_index, Error **errp); #endif /* QCRYPTO_CRYPTODEV_H */ -- 1.7.12.4