diff mbox series

[v5,09/12] cryptodev: Account statistics

Message ID 20230301025124.3605557-10-pizhenwei@bytedance.com (mailing list archive)
State New, archived
Headers show
Series Refactor cryptodev | expand

Commit Message

zhenwei pi March 1, 2023, 2:51 a.m. UTC
Account OPS/BPS for crypto device, this will be used for 'query-stats'
QEMU monitor command and QoS in the next step.

Note that a crypto device may support symmetric mode, asymmetric mode,
both symmetric and asymmetric mode. So we use two structure to
describe the statistics of a crypto device.

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
 backends/cryptodev.c       | 68 +++++++++++++++++++++++++++++++++++---
 include/sysemu/cryptodev.h | 31 +++++++++++++++++
 qapi/cryptodev.json        | 54 ++++++++++++++++++++++++++++++
 3 files changed, 148 insertions(+), 5 deletions(-)

Comments

Daniel P. Berrangé March 1, 2023, 9:44 a.m. UTC | #1
On Wed, Mar 01, 2023 at 10:51:21AM +0800, zhenwei pi wrote:
> Account OPS/BPS for crypto device, this will be used for 'query-stats'
> QEMU monitor command and QoS in the next step.
> 
> Note that a crypto device may support symmetric mode, asymmetric mode,
> both symmetric and asymmetric mode. So we use two structure to
> describe the statistics of a crypto device.
> 
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
> ---
>  backends/cryptodev.c       | 68 +++++++++++++++++++++++++++++++++++---
>  include/sysemu/cryptodev.h | 31 +++++++++++++++++
>  qapi/cryptodev.json        | 54 ++++++++++++++++++++++++++++++
>  3 files changed, 148 insertions(+), 5 deletions(-)
> 
> diff --git a/backends/cryptodev.c b/backends/cryptodev.c
> index ba7b0bc770..cc824e9665 100644
> --- a/backends/cryptodev.c
> +++ b/backends/cryptodev.c
> @@ -107,6 +107,9 @@ void cryptodev_backend_cleanup(
>      if (bc->cleanup) {
>          bc->cleanup(backend, errp);
>      }
> +
> +    g_free(backend->sym_stat);
> +    g_free(backend->asym_stat);
>  }
>  
>  int cryptodev_backend_create_session(
> @@ -154,16 +157,61 @@ static int cryptodev_backend_operation(
>      return -VIRTIO_CRYPTO_NOTSUPP;
>  }
>  
> +static int cryptodev_backend_account(CryptoDevBackend *backend,
> +                 CryptoDevBackendOpInfo *op_info)
> +{
> +    enum QCryptodevBackendAlgType algtype = op_info->algtype;
> +    int len;
> +
> +    if (algtype == QCRYPTODEV_BACKEND_ALG_ASYM) {
> +        CryptoDevBackendAsymOpInfo *asym_op_info = op_info->u.asym_op_info;
> +        len = asym_op_info->src_len;
> +        switch (op_info->op_code) {
> +        case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
> +            QCryptodevAsymStatIncEncrypt(backend, len);
> +            break;
> +        case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
> +            QCryptodevAsymStatIncDecrypt(backend, len);
> +            break;
> +        case VIRTIO_CRYPTO_AKCIPHER_SIGN:
> +            QCryptodevAsymStatIncSign(backend, len);
> +            break;
> +        case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
> +            QCryptodevAsymStatIncVerify(backend, len);
> +            break;
> +        default:
> +            return -VIRTIO_CRYPTO_NOTSUPP;
> +        }
> +    } else if (algtype == QCRYPTODEV_BACKEND_ALG_SYM) {
> +        CryptoDevBackendSymOpInfo *sym_op_info = op_info->u.sym_op_info;
> +        len = sym_op_info->src_len;
> +        switch (op_info->op_code) {
> +        case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
> +            QCryptodevSymStatIncEncrypt(backend, len);
> +            break;
> +        case VIRTIO_CRYPTO_CIPHER_DECRYPT:
> +            QCryptodevSymStatIncDecrypt(backend, len);
> +            break;
> +        default:
> +            return -VIRTIO_CRYPTO_NOTSUPP;
> +        }
> +    } else {
> +        error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
> +        return -VIRTIO_CRYPTO_NOTSUPP;
> +    }
> +
> +    return len;
> +}
> +
>  int cryptodev_backend_crypto_operation(
>                   CryptoDevBackend *backend,
>                   CryptoDevBackendOpInfo *op_info)
>  {
> -    QCryptodevBackendAlgType algtype = op_info->algtype;
> +    int ret;
>  
> -    if ((algtype != QCRYPTODEV_BACKEND_ALG_SYM)
> -        && (algtype != QCRYPTODEV_BACKEND_ALG_ASYM)) {
> -        error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
> -        return -VIRTIO_CRYPTO_NOTSUPP;
> +    ret = cryptodev_backend_account(backend, op_info);
> +    if (ret < 0) {
> +        return ret;
>      }
>  
>      return cryptodev_backend_operation(backend, op_info);
> @@ -202,10 +250,20 @@ cryptodev_backend_complete(UserCreatable *uc, Error **errp)
>  {
>      CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
>      CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
> +    uint32_t services;
>  
>      if (bc->init) {
>          bc->init(backend, errp);
>      }
> +
> +    services = backend->conf.crypto_services;
> +    if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_CIPHER)) {
> +        backend->sym_stat = g_new0(QCryptodevBackendSymStat, 1);
> +    }
> +
> +    if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER)) {
> +        backend->asym_stat = g_new0(QCryptodevBackendAsymStat, 1);
> +    }
>  }
>  
>  void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
> diff --git a/include/sysemu/cryptodev.h b/include/sysemu/cryptodev.h
> index 048a627035..15e8c04dcf 100644
> --- a/include/sysemu/cryptodev.h
> +++ b/include/sysemu/cryptodev.h
> @@ -253,8 +253,39 @@ struct CryptoDevBackend {
>      /* Tag the cryptodev backend is used by virtio-crypto or not */
>      bool is_used;
>      CryptoDevBackendConf conf;
> +    QCryptodevBackendSymStat *sym_stat;
> +    QCryptodevBackendAsymStat *asym_stat;
>  };
>  
> +#define QCryptodevSymStatInc(be, op, bytes) do { \
> +   be->sym_stat->op##_bytes += (bytes); \
> +   be->sym_stat->op##_ops += 1; \
> +} while (/*CONSTCOND*/0)
> +
> +#define QCryptodevSymStatIncEncrypt(be, bytes) \
> +            QCryptodevSymStatInc(be, encrypt, bytes)
> +
> +#define QCryptodevSymStatIncDecrypt(be, bytes) \
> +            QCryptodevSymStatInc(be, decrypt, bytes)
> +
> +#define QCryptodevAsymStatInc(be, op, bytes) do { \
> +    be->asym_stat->op##_bytes += (bytes); \
> +    be->asym_stat->op##_ops += 1; \
> +} while (/*CONSTCOND*/0)
> +
> +#define QCryptodevAsymStatIncEncrypt(be, bytes) \
> +            QCryptodevAsymStatInc(be, encrypt, bytes)
> +
> +#define QCryptodevAsymStatIncDecrypt(be, bytes) \
> +            QCryptodevAsymStatInc(be, decrypt, bytes)
> +
> +#define QCryptodevAsymStatIncSign(be, bytes) \
> +            QCryptodevAsymStatInc(be, sign, bytes)
> +
> +#define QCryptodevAsymStatIncVerify(be, bytes) \
> +            QCryptodevAsymStatInc(be, verify, bytes)
> +
> +
>  /**
>   * cryptodev_backend_new_client:
>   *
> diff --git a/qapi/cryptodev.json b/qapi/cryptodev.json
> index f33f96a692..54d7f9cb58 100644
> --- a/qapi/cryptodev.json
> +++ b/qapi/cryptodev.json
> @@ -87,3 +87,57 @@
>  # Since: 8.0
>  ##
>  { 'command': 'query-cryptodev', 'returns': ['QCryptodevInfo']}
> +
> +##
> +# @QCryptodevBackendSymStat:
> +#
> +# The statistics of symmetric operation.
> +#
> +# @encrypt-ops: the operations of symmetric encryption
> +#
> +# @decrypt-ops: the operations of symmetric decryption
> +#
> +# @encrypt-bytes: the bytes of symmetric encryption
> +#
> +# @decrypt-bytes: the bytes of symmetric decryption
> +#
> +# Since: 8.0
> +##
> +{ 'struct': 'QCryptodevBackendSymStat',
> +  'data': { 'encrypt-ops': 'int',
> +            'decrypt-ops': 'int',
> +            'encrypt-bytes': 'int',
> +            'decrypt-bytes': 'int' } }
> +
> +##
> +# @QCryptodevBackendAsymStat:
> +#
> +# The statistics of asymmetric operation.
> +#
> +# @encrypt-ops: the operations of asymmetric encryption
> +#
> +# @decrypt-ops: the operations of asymmetric decryption
> +#
> +# @sign-ops: the operations of asymmetric signature
> +#
> +# @verify-ops: the operations of asymmetric verification
> +#
> +# @encrypt-bytes: the bytes of asymmetric encryption
> +#
> +# @decrypt-bytes: the bytes of asymmetric decryption
> +#
> +# @sign-bytes: the bytes of asymmetric signature
> +#
> +# @verify-bytes: the bytes of asymmetric verification
> +#
> +# Since: 8.0
> +##
> +{ 'struct': 'QCryptodevBackendAsymStat',
> +  'data': { 'encrypt-ops': 'int',
> +            'decrypt-ops': 'int',
> +            'sign-ops': 'int',
> +            'verify-ops': 'int',
> +            'encrypt-bytes': 'int',
> +            'decrypt-bytes': 'int',
> +            'sign-bytes': 'int',
> +            'verify-bytes': 'int' } }

AFAICT, these two structs are no longer used in QAPI since the switch
to using query-stats. IOW this has become just an indirect way to
declare a C struct for private use in the C code.

As such, I'd suggest that this QAPI addition be removed, and just
declare a normal C struct directly in the code which needs it.

With regards,
Daniel
zhenwei pi March 1, 2023, 10:20 a.m. UTC | #2
On 3/1/23 17:44, Daniel P. Berrangé wrote:
> On Wed, Mar 01, 2023 at 10:51:21AM +0800, zhenwei pi wrote:
>> Account OPS/BPS for crypto device, this will be used for 'query-stats'
>> QEMU monitor command and QoS in the next step.
>>
>> Note that a crypto device may support symmetric mode, asymmetric mode,
>> both symmetric and asymmetric mode. So we use two structure to
>> describe the statistics of a crypto device.
>>
>> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
>> ---
>>   backends/cryptodev.c       | 68 +++++++++++++++++++++++++++++++++++---
>>   include/sysemu/cryptodev.h | 31 +++++++++++++++++
>>   qapi/cryptodev.json        | 54 ++++++++++++++++++++++++++++++
>>   3 files changed, 148 insertions(+), 5 deletions(-)
>>
>> diff --git a/backends/cryptodev.c b/backends/cryptodev.c
>> index ba7b0bc770..cc824e9665 100644
>> --- a/backends/cryptodev.c
>> +++ b/backends/cryptodev.c
>> @@ -107,6 +107,9 @@ void cryptodev_backend_cleanup(
>>       if (bc->cleanup) {
>>           bc->cleanup(backend, errp);
>>       }
>> +
>> +    g_free(backend->sym_stat);
>> +    g_free(backend->asym_stat);
>>   }
>>   
>>   int cryptodev_backend_create_session(
>> @@ -154,16 +157,61 @@ static int cryptodev_backend_operation(
>>       return -VIRTIO_CRYPTO_NOTSUPP;
>>   }
>>   
>> +static int cryptodev_backend_account(CryptoDevBackend *backend,
>> +                 CryptoDevBackendOpInfo *op_info)
>> +{
>> +    enum QCryptodevBackendAlgType algtype = op_info->algtype;
>> +    int len;
>> +
>> +    if (algtype == QCRYPTODEV_BACKEND_ALG_ASYM) {
>> +        CryptoDevBackendAsymOpInfo *asym_op_info = op_info->u.asym_op_info;
>> +        len = asym_op_info->src_len;
>> +        switch (op_info->op_code) {
>> +        case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
>> +            QCryptodevAsymStatIncEncrypt(backend, len);
>> +            break;
>> +        case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
>> +            QCryptodevAsymStatIncDecrypt(backend, len);
>> +            break;
>> +        case VIRTIO_CRYPTO_AKCIPHER_SIGN:
>> +            QCryptodevAsymStatIncSign(backend, len);
>> +            break;
>> +        case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
>> +            QCryptodevAsymStatIncVerify(backend, len);
>> +            break;
>> +        default:
>> +            return -VIRTIO_CRYPTO_NOTSUPP;
>> +        }
>> +    } else if (algtype == QCRYPTODEV_BACKEND_ALG_SYM) {
>> +        CryptoDevBackendSymOpInfo *sym_op_info = op_info->u.sym_op_info;
>> +        len = sym_op_info->src_len;
>> +        switch (op_info->op_code) {
>> +        case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
>> +            QCryptodevSymStatIncEncrypt(backend, len);
>> +            break;
>> +        case VIRTIO_CRYPTO_CIPHER_DECRYPT:
>> +            QCryptodevSymStatIncDecrypt(backend, len);
>> +            break;
>> +        default:
>> +            return -VIRTIO_CRYPTO_NOTSUPP;
>> +        }
>> +    } else {
>> +        error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
>> +        return -VIRTIO_CRYPTO_NOTSUPP;
>> +    }
>> +
>> +    return len;
>> +}
>> +
>>   int cryptodev_backend_crypto_operation(
>>                    CryptoDevBackend *backend,
>>                    CryptoDevBackendOpInfo *op_info)
>>   {
>> -    QCryptodevBackendAlgType algtype = op_info->algtype;
>> +    int ret;
>>   
>> -    if ((algtype != QCRYPTODEV_BACKEND_ALG_SYM)
>> -        && (algtype != QCRYPTODEV_BACKEND_ALG_ASYM)) {
>> -        error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
>> -        return -VIRTIO_CRYPTO_NOTSUPP;
>> +    ret = cryptodev_backend_account(backend, op_info);
>> +    if (ret < 0) {
>> +        return ret;
>>       }
>>   
>>       return cryptodev_backend_operation(backend, op_info);
>> @@ -202,10 +250,20 @@ cryptodev_backend_complete(UserCreatable *uc, Error **errp)
>>   {
>>       CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
>>       CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
>> +    uint32_t services;
>>   
>>       if (bc->init) {
>>           bc->init(backend, errp);
>>       }
>> +
>> +    services = backend->conf.crypto_services;
>> +    if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_CIPHER)) {
>> +        backend->sym_stat = g_new0(QCryptodevBackendSymStat, 1);
>> +    }
>> +
>> +    if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER)) {
>> +        backend->asym_stat = g_new0(QCryptodevBackendAsymStat, 1);
>> +    }
>>   }
>>   
>>   void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
>> diff --git a/include/sysemu/cryptodev.h b/include/sysemu/cryptodev.h
>> index 048a627035..15e8c04dcf 100644
>> --- a/include/sysemu/cryptodev.h
>> +++ b/include/sysemu/cryptodev.h
>> @@ -253,8 +253,39 @@ struct CryptoDevBackend {
>>       /* Tag the cryptodev backend is used by virtio-crypto or not */
>>       bool is_used;
>>       CryptoDevBackendConf conf;
>> +    QCryptodevBackendSymStat *sym_stat;
>> +    QCryptodevBackendAsymStat *asym_stat;
>>   };
>>   
>> +#define QCryptodevSymStatInc(be, op, bytes) do { \
>> +   be->sym_stat->op##_bytes += (bytes); \
>> +   be->sym_stat->op##_ops += 1; \
>> +} while (/*CONSTCOND*/0)
>> +
>> +#define QCryptodevSymStatIncEncrypt(be, bytes) \
>> +            QCryptodevSymStatInc(be, encrypt, bytes)
>> +
>> +#define QCryptodevSymStatIncDecrypt(be, bytes) \
>> +            QCryptodevSymStatInc(be, decrypt, bytes)
>> +
>> +#define QCryptodevAsymStatInc(be, op, bytes) do { \
>> +    be->asym_stat->op##_bytes += (bytes); \
>> +    be->asym_stat->op##_ops += 1; \
>> +} while (/*CONSTCOND*/0)
>> +
>> +#define QCryptodevAsymStatIncEncrypt(be, bytes) \
>> +            QCryptodevAsymStatInc(be, encrypt, bytes)
>> +
>> +#define QCryptodevAsymStatIncDecrypt(be, bytes) \
>> +            QCryptodevAsymStatInc(be, decrypt, bytes)
>> +
>> +#define QCryptodevAsymStatIncSign(be, bytes) \
>> +            QCryptodevAsymStatInc(be, sign, bytes)
>> +
>> +#define QCryptodevAsymStatIncVerify(be, bytes) \
>> +            QCryptodevAsymStatInc(be, verify, bytes)
>> +
>> +
>>   /**
>>    * cryptodev_backend_new_client:
>>    *
>> diff --git a/qapi/cryptodev.json b/qapi/cryptodev.json
>> index f33f96a692..54d7f9cb58 100644
>> --- a/qapi/cryptodev.json
>> +++ b/qapi/cryptodev.json
>> @@ -87,3 +87,57 @@
>>   # Since: 8.0
>>   ##
>>   { 'command': 'query-cryptodev', 'returns': ['QCryptodevInfo']}
>> +
>> +##
>> +# @QCryptodevBackendSymStat:
>> +#
>> +# The statistics of symmetric operation.
>> +#
>> +# @encrypt-ops: the operations of symmetric encryption
>> +#
>> +# @decrypt-ops: the operations of symmetric decryption
>> +#
>> +# @encrypt-bytes: the bytes of symmetric encryption
>> +#
>> +# @decrypt-bytes: the bytes of symmetric decryption
>> +#
>> +# Since: 8.0
>> +##
>> +{ 'struct': 'QCryptodevBackendSymStat',
>> +  'data': { 'encrypt-ops': 'int',
>> +            'decrypt-ops': 'int',
>> +            'encrypt-bytes': 'int',
>> +            'decrypt-bytes': 'int' } }
>> +
>> +##
>> +# @QCryptodevBackendAsymStat:
>> +#
>> +# The statistics of asymmetric operation.
>> +#
>> +# @encrypt-ops: the operations of asymmetric encryption
>> +#
>> +# @decrypt-ops: the operations of asymmetric decryption
>> +#
>> +# @sign-ops: the operations of asymmetric signature
>> +#
>> +# @verify-ops: the operations of asymmetric verification
>> +#
>> +# @encrypt-bytes: the bytes of asymmetric encryption
>> +#
>> +# @decrypt-bytes: the bytes of asymmetric decryption
>> +#
>> +# @sign-bytes: the bytes of asymmetric signature
>> +#
>> +# @verify-bytes: the bytes of asymmetric verification
>> +#
>> +# Since: 8.0
>> +##
>> +{ 'struct': 'QCryptodevBackendAsymStat',
>> +  'data': { 'encrypt-ops': 'int',
>> +            'decrypt-ops': 'int',
>> +            'sign-ops': 'int',
>> +            'verify-ops': 'int',
>> +            'encrypt-bytes': 'int',
>> +            'decrypt-bytes': 'int',
>> +            'sign-bytes': 'int',
>> +            'verify-bytes': 'int' } }
> 
> AFAICT, these two structs are no longer used in QAPI since the switch
> to using query-stats. IOW this has become just an indirect way to
> declare a C struct for private use in the C code.
> 
> As such, I'd suggest that this QAPI addition be removed, and just
> declare a normal C struct directly in the code which needs it.
> 
> With regards,
> Daniel


OK, I'll fix this in the next version. Thanks!
diff mbox series

Patch

diff --git a/backends/cryptodev.c b/backends/cryptodev.c
index ba7b0bc770..cc824e9665 100644
--- a/backends/cryptodev.c
+++ b/backends/cryptodev.c
@@ -107,6 +107,9 @@  void cryptodev_backend_cleanup(
     if (bc->cleanup) {
         bc->cleanup(backend, errp);
     }
+
+    g_free(backend->sym_stat);
+    g_free(backend->asym_stat);
 }
 
 int cryptodev_backend_create_session(
@@ -154,16 +157,61 @@  static int cryptodev_backend_operation(
     return -VIRTIO_CRYPTO_NOTSUPP;
 }
 
+static int cryptodev_backend_account(CryptoDevBackend *backend,
+                 CryptoDevBackendOpInfo *op_info)
+{
+    enum QCryptodevBackendAlgType algtype = op_info->algtype;
+    int len;
+
+    if (algtype == QCRYPTODEV_BACKEND_ALG_ASYM) {
+        CryptoDevBackendAsymOpInfo *asym_op_info = op_info->u.asym_op_info;
+        len = asym_op_info->src_len;
+        switch (op_info->op_code) {
+        case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
+            QCryptodevAsymStatIncEncrypt(backend, len);
+            break;
+        case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
+            QCryptodevAsymStatIncDecrypt(backend, len);
+            break;
+        case VIRTIO_CRYPTO_AKCIPHER_SIGN:
+            QCryptodevAsymStatIncSign(backend, len);
+            break;
+        case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
+            QCryptodevAsymStatIncVerify(backend, len);
+            break;
+        default:
+            return -VIRTIO_CRYPTO_NOTSUPP;
+        }
+    } else if (algtype == QCRYPTODEV_BACKEND_ALG_SYM) {
+        CryptoDevBackendSymOpInfo *sym_op_info = op_info->u.sym_op_info;
+        len = sym_op_info->src_len;
+        switch (op_info->op_code) {
+        case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
+            QCryptodevSymStatIncEncrypt(backend, len);
+            break;
+        case VIRTIO_CRYPTO_CIPHER_DECRYPT:
+            QCryptodevSymStatIncDecrypt(backend, len);
+            break;
+        default:
+            return -VIRTIO_CRYPTO_NOTSUPP;
+        }
+    } else {
+        error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
+        return -VIRTIO_CRYPTO_NOTSUPP;
+    }
+
+    return len;
+}
+
 int cryptodev_backend_crypto_operation(
                  CryptoDevBackend *backend,
                  CryptoDevBackendOpInfo *op_info)
 {
-    QCryptodevBackendAlgType algtype = op_info->algtype;
+    int ret;
 
-    if ((algtype != QCRYPTODEV_BACKEND_ALG_SYM)
-        && (algtype != QCRYPTODEV_BACKEND_ALG_ASYM)) {
-        error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
-        return -VIRTIO_CRYPTO_NOTSUPP;
+    ret = cryptodev_backend_account(backend, op_info);
+    if (ret < 0) {
+        return ret;
     }
 
     return cryptodev_backend_operation(backend, op_info);
@@ -202,10 +250,20 @@  cryptodev_backend_complete(UserCreatable *uc, Error **errp)
 {
     CryptoDevBackend *backend = CRYPTODEV_BACKEND(uc);
     CryptoDevBackendClass *bc = CRYPTODEV_BACKEND_GET_CLASS(uc);
+    uint32_t services;
 
     if (bc->init) {
         bc->init(backend, errp);
     }
+
+    services = backend->conf.crypto_services;
+    if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_CIPHER)) {
+        backend->sym_stat = g_new0(QCryptodevBackendSymStat, 1);
+    }
+
+    if (services & (1 << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER)) {
+        backend->asym_stat = g_new0(QCryptodevBackendAsymStat, 1);
+    }
 }
 
 void cryptodev_backend_set_used(CryptoDevBackend *backend, bool used)
diff --git a/include/sysemu/cryptodev.h b/include/sysemu/cryptodev.h
index 048a627035..15e8c04dcf 100644
--- a/include/sysemu/cryptodev.h
+++ b/include/sysemu/cryptodev.h
@@ -253,8 +253,39 @@  struct CryptoDevBackend {
     /* Tag the cryptodev backend is used by virtio-crypto or not */
     bool is_used;
     CryptoDevBackendConf conf;
+    QCryptodevBackendSymStat *sym_stat;
+    QCryptodevBackendAsymStat *asym_stat;
 };
 
+#define QCryptodevSymStatInc(be, op, bytes) do { \
+   be->sym_stat->op##_bytes += (bytes); \
+   be->sym_stat->op##_ops += 1; \
+} while (/*CONSTCOND*/0)
+
+#define QCryptodevSymStatIncEncrypt(be, bytes) \
+            QCryptodevSymStatInc(be, encrypt, bytes)
+
+#define QCryptodevSymStatIncDecrypt(be, bytes) \
+            QCryptodevSymStatInc(be, decrypt, bytes)
+
+#define QCryptodevAsymStatInc(be, op, bytes) do { \
+    be->asym_stat->op##_bytes += (bytes); \
+    be->asym_stat->op##_ops += 1; \
+} while (/*CONSTCOND*/0)
+
+#define QCryptodevAsymStatIncEncrypt(be, bytes) \
+            QCryptodevAsymStatInc(be, encrypt, bytes)
+
+#define QCryptodevAsymStatIncDecrypt(be, bytes) \
+            QCryptodevAsymStatInc(be, decrypt, bytes)
+
+#define QCryptodevAsymStatIncSign(be, bytes) \
+            QCryptodevAsymStatInc(be, sign, bytes)
+
+#define QCryptodevAsymStatIncVerify(be, bytes) \
+            QCryptodevAsymStatInc(be, verify, bytes)
+
+
 /**
  * cryptodev_backend_new_client:
  *
diff --git a/qapi/cryptodev.json b/qapi/cryptodev.json
index f33f96a692..54d7f9cb58 100644
--- a/qapi/cryptodev.json
+++ b/qapi/cryptodev.json
@@ -87,3 +87,57 @@ 
 # Since: 8.0
 ##
 { 'command': 'query-cryptodev', 'returns': ['QCryptodevInfo']}
+
+##
+# @QCryptodevBackendSymStat:
+#
+# The statistics of symmetric operation.
+#
+# @encrypt-ops: the operations of symmetric encryption
+#
+# @decrypt-ops: the operations of symmetric decryption
+#
+# @encrypt-bytes: the bytes of symmetric encryption
+#
+# @decrypt-bytes: the bytes of symmetric decryption
+#
+# Since: 8.0
+##
+{ 'struct': 'QCryptodevBackendSymStat',
+  'data': { 'encrypt-ops': 'int',
+            'decrypt-ops': 'int',
+            'encrypt-bytes': 'int',
+            'decrypt-bytes': 'int' } }
+
+##
+# @QCryptodevBackendAsymStat:
+#
+# The statistics of asymmetric operation.
+#
+# @encrypt-ops: the operations of asymmetric encryption
+#
+# @decrypt-ops: the operations of asymmetric decryption
+#
+# @sign-ops: the operations of asymmetric signature
+#
+# @verify-ops: the operations of asymmetric verification
+#
+# @encrypt-bytes: the bytes of asymmetric encryption
+#
+# @decrypt-bytes: the bytes of asymmetric decryption
+#
+# @sign-bytes: the bytes of asymmetric signature
+#
+# @verify-bytes: the bytes of asymmetric verification
+#
+# Since: 8.0
+##
+{ 'struct': 'QCryptodevBackendAsymStat',
+  'data': { 'encrypt-ops': 'int',
+            'decrypt-ops': 'int',
+            'sign-ops': 'int',
+            'verify-ops': 'int',
+            'encrypt-bytes': 'int',
+            'decrypt-bytes': 'int',
+            'sign-bytes': 'int',
+            'verify-bytes': 'int' } }