Skip to contentSkip to conversation

Refactor cryptodev

PATCH Superseded zhenwei pi 2 participants 12 messages Nov 11, 2022 – Nov 16, 2022 Arm/Amlogic Meson SoC support

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

zhenwei pi

author
#0 lore raw

cover Refactor cryptodev

The main changes in this series:
- introduce cryptodev.json to describe the attributes of crypto device, then drop duplicated type declare, remove some virtio related dependence.
- add statistics: OPS and bandwidth.
- add QMP command: query-cryptodev
- add HMP info command: cryptodev
- misc fix: detect akcipher capability instead of exposing akcipher service unconditionally.

Zhenwei Pi (8):
cryptodev: Introduce cryptodev.json
cryptodev: Remove 'name' & 'model' fields
cryptodev: Introduce cryptodev alg type in QAPI
cryptodev: Introduce server type in QAPI
cryptodev: Introduce 'query-cryptodev' QMP command
cryptodev: Support statistics
cryptodev-builtin: Detect akcipher capability
hmp: add cryptodev info command

 MAINTAINERS                     |   1 +
 backends/cryptodev-builtin.c    |  46 +++++++---
 backends/cryptodev-lkcf.c       |  11 +--
 backends/cryptodev-vhost-user.c |  13 ++-
 backends/cryptodev-vhost.c      |   4 +-
 backends/cryptodev.c            | 101 ++++++++++++++++++----
 hmp-commands-info.hx            |  14 ++++
 hw/virtio/virtio-crypto.c       |  41 +++++++--
 include/monitor/hmp.h           |   1 +
 include/sysemu/cryptodev.h      |  61 ++++++++------
 monitor/hmp-cmds.c              |  36 ++++++++
 qapi/cryptodev.json             | 144 ++++++++++++++++++++++++++++++++
 qapi/meson.build                |   1 +
 qapi/qapi-schema.json           |   1 +
 14 files changed, 401 insertions(+), 74 deletions(-)
 create mode 100644 qapi/cryptodev.json
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH 1/8 cryptodev: Introduce cryptodev.json

Introduce QCryptodevBackendType in cryptodev.json, also apply this to related codes. Then we can drop 'enum CryptoDevBackendOptionsType'.

---
 MAINTAINERS                     |  1 +
 backends/cryptodev-builtin.c    |  2 +-
 backends/cryptodev-lkcf.c       |  2 +-
 backends/cryptodev-vhost-user.c |  4 ++--
 backends/cryptodev-vhost.c      |  4 ++--
 include/sysemu/cryptodev.h      | 11 ++---------
 qapi/cryptodev.json             | 20 ++++++++++++++++++++
 qapi/meson.build                |  1 +
 qapi/qapi-schema.json           |  1 +
 9 files changed, 31 insertions(+), 15 deletions(-)
 create mode 100644 qapi/cryptodev.json
MAINTAINERS +1 -0
index caba73ec41..3f698cb0e9 100644
@@ -2830,6 +2830,7 @@ M: Gonglei <[email protected]>
 S: Maintained
 F: include/sysemu/cryptodev*.h
 F: backends/cryptodev*.c
+F: qapi/cryptodev.json
 
 Python library
 M: John Snow <[email protected]>
backends/cryptodev-builtin.c +1 -1
index cda6ca3b71..8c7c10847d 100644
@@ -76,7 +76,7 @@ static void cryptodev_builtin_init(
               "cryptodev-builtin", NULL);
     cc->info_str = g_strdup_printf("cryptodev-builtin0");
     cc->queue_index = 0;
-    cc->type = CRYPTODEV_BACKEND_TYPE_BUILTIN;
+    cc->type = QCRYPTODEV_BACKEND_TYPE_BUILTIN;
     backend->conf.peers.ccs[0] = cc;
 
     backend->conf.crypto_services =
backends/cryptodev-lkcf.c +1 -1
index 133bd706a4..91e02c0df9 100644
@@ -226,7 +226,7 @@ static void cryptodev_lkcf_init(CryptoDevBackend *backend, Error **errp)
     cc = cryptodev_backend_new_client("cryptodev-lkcf", NULL);
     cc->info_str = g_strdup_printf("cryptodev-lkcf0");
     cc->queue_index = 0;
-    cc->type = CRYPTODEV_BACKEND_TYPE_LKCF;
+    cc->type = QCRYPTODEV_BACKEND_TYPE_LKCF;
     backend->conf.peers.ccs[0] = cc;
 
     backend->conf.crypto_services =
backends/cryptodev-vhost-user.c +2 -2
index ab3028e045..c165a1b1d6 100644
@@ -67,7 +67,7 @@ cryptodev_vhost_user_get_vhost(
 {
     CryptoDevBackendVhostUser *s =
                       CRYPTODEV_BACKEND_VHOST_USER(b);
-    assert(cc->type == CRYPTODEV_BACKEND_TYPE_VHOST_USER);
+    assert(cc->type == QCRYPTODEV_BACKEND_TYPE_VHOST_USER);
     assert(queue < MAX_CRYPTO_QUEUE_NUM);
 
     return s->vhost_crypto[queue];
@@ -203,7 +203,7 @@ static void cryptodev_vhost_user_init(
         cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
                                        i, chr->label);
         cc->queue_index = i;
-        cc->type = CRYPTODEV_BACKEND_TYPE_VHOST_USER;
+        cc->type = QCRYPTODEV_BACKEND_TYPE_VHOST_USER;
 
         backend->conf.peers.ccs[i] = cc;
 
backends/cryptodev-vhost.c +2 -2
index bc13e466b4..0715014552 100644
@@ -128,7 +128,7 @@ cryptodev_get_vhost(CryptoDevBackendClient *cc,
 
     switch (cc->type) {
 #if defined(CONFIG_VHOST_USER) && defined(CONFIG_LINUX)
-    case CRYPTODEV_BACKEND_TYPE_VHOST_USER:
+    case QCRYPTODEV_BACKEND_TYPE_VHOST_USER:
         vhost_crypto = cryptodev_vhost_user_get_vhost(cc, b, queue);
         break;
 #endif
@@ -196,7 +196,7 @@ int cryptodev_vhost_start(VirtIODevice *dev, int total_queues)
          * because vhost user doesn't interrupt masking/unmasking
          * properly.
          */
-        if (cc->type == CRYPTODEV_BACKEND_TYPE_VHOST_USER) {
+        if (cc->type == QCRYPTODEV_BACKEND_TYPE_VHOST_USER) {
             dev->use_guest_notifier_mask = false;
         }
      }
include/sysemu/cryptodev.h +2 -9
index cf9b3f07fe..8d2adda974 100644
@@ -25,6 +25,7 @@
 
 #include "qemu/queue.h"
 #include "qom/object.h"
+#include "qapi/qapi-types-cryptodev.h"
 
 /**
  * CryptoDevBackend:
@@ -215,16 +216,8 @@ struct CryptoDevBackendClass {
                  void *opaque);
 };
 
-typedef enum CryptoDevBackendOptionsType {
-    CRYPTODEV_BACKEND_TYPE_NONE = 0,
-    CRYPTODEV_BACKEND_TYPE_BUILTIN = 1,
-    CRYPTODEV_BACKEND_TYPE_VHOST_USER = 2,
-    CRYPTODEV_BACKEND_TYPE_LKCF = 3,
-    CRYPTODEV_BACKEND_TYPE__MAX,
-} CryptoDevBackendOptionsType;
-
 struct CryptoDevBackendClient {
-    CryptoDevBackendOptionsType type;
+    QCryptodevBackendType type;
     char *model;
     char *name;
     char *info_str;
qapi/cryptodev.json new file +20 -0
new file mode 100644
index 0000000000..b65edbe183
@@ -0,0 +1,20 @@
+# -*- Mode: Python -*-
+# vim: filetype=python
+#
+# This work is licensed under the terms of the GNU GPL, version 2 or later.
+# See the COPYING file in the top-level directory.
+
+##
+# @QCryptodevBackendType:
+#
+# The crypto device backend type
+#
+# @builtin: the QEMU builtin support
+# @vhost-user: vhost-user
+# @lkcf: Linux kernel cryptographic framework
+#
+# Since: 8.0
+##
+{ 'enum': 'QCryptodevBackendType',
+  'prefix': 'QCRYPTODEV_BACKEND_TYPE',
+  'data': ['builtin', 'vhost-user', 'lkcf']}
qapi/meson.build +1 -0
index 9a36c15c04..b5069f34f8 100644
@@ -56,6 +56,7 @@ if have_system
   qapi_all_modules += [
     'acpi',
     'audio',
+    'cryptodev',
     'qdev',
     'pci',
     'rdma',
qapi/qapi-schema.json +1 -0
index f000b90744..1e923945db 100644
@@ -95,3 +95,4 @@
 { 'include': 'pci.json' }
 { 'include': 'stats.json' }
 { 'include': 'virtio.json' }
+{ 'include': 'cryptodev.json' }
Signature · 1 line
2.20.1
author
#2 lore raw

PATCH 2/8 cryptodev: Remove 'name' & 'model' fields

In reply to zhenwei pi

We have already used qapi to generate crypto device types, this allows to convert type to a string 'model', so the 'model' field is not needed.

And the 'name' field is not used by any backend driver, drop it.

---
 backends/cryptodev-builtin.c    |  3 +--
 backends/cryptodev-lkcf.c       |  2 +-
 backends/cryptodev-vhost-user.c |  3 +--
 backends/cryptodev.c            | 11 +----------
 include/sysemu/cryptodev.h      | 12 +++---------
 5 files changed, 7 insertions(+), 24 deletions(-)
backends/cryptodev-builtin.c +1 -2
index 8c7c10847d..08895271eb 100644
@@ -72,8 +72,7 @@ static void cryptodev_builtin_init(
         return;
     }
 
-    cc = cryptodev_backend_new_client(
-              "cryptodev-builtin", NULL);
+    cc = cryptodev_backend_new_client();
     cc->info_str = g_strdup_printf("cryptodev-builtin0");
     cc->queue_index = 0;
     cc->type = QCRYPTODEV_BACKEND_TYPE_BUILTIN;
backends/cryptodev-lkcf.c +1 -1
index 91e02c0df9..de3d1867c5 100644
@@ -223,7 +223,7 @@ static void cryptodev_lkcf_init(CryptoDevBackend *backend, Error **errp)
         return;
     }
 
-    cc = cryptodev_backend_new_client("cryptodev-lkcf", NULL);
+    cc = cryptodev_backend_new_client();
     cc->info_str = g_strdup_printf("cryptodev-lkcf0");
     cc->queue_index = 0;
     cc->type = QCRYPTODEV_BACKEND_TYPE_LKCF;
backends/cryptodev-vhost-user.c +1 -2
index c165a1b1d6..580bd1abb0 100644
@@ -198,8 +198,7 @@ static void cryptodev_vhost_user_init(
     s->opened = true;
 
     for (i = 0; i < queues; i++) {
-        cc = cryptodev_backend_new_client(
-                  "cryptodev-vhost-user", NULL);
+        cc = cryptodev_backend_new_client();
         cc->info_str = g_strdup_printf("cryptodev-vhost-user%zu to %s ",
                                        i, chr->label);
         cc->queue_index = i;
backends/cryptodev.c +1 -10
index 54ee8c81f5..81941af816 100644
@@ -34,18 +34,11 @@
 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
 
 
-CryptoDevBackendClient *
-cryptodev_backend_new_client(const char *model,
-                                    const char *name)
+CryptoDevBackendClient *cryptodev_backend_new_client(void)
 {
     CryptoDevBackendClient *cc;
 
     cc = g_new0(CryptoDevBackendClient, 1);
-    cc->model = g_strdup(model);
-    if (name) {
-        cc->name = g_strdup(name);
-    }
-
     QTAILQ_INSERT_TAIL(&crypto_clients, cc, next);
 
     return cc;
@@ -55,8 +48,6 @@ void cryptodev_backend_free_client(
                   CryptoDevBackendClient *cc)
 {
     QTAILQ_REMOVE(&crypto_clients, cc, next);
-    g_free(cc->name);
-    g_free(cc->model);
     g_free(cc->info_str);
     g_free(cc);
 }
include/sysemu/cryptodev.h +3 -9
index 8d2adda974..af152d09db 100644
@@ -218,8 +218,6 @@ struct CryptoDevBackendClass {
 
 struct CryptoDevBackendClient {
     QCryptodevBackendType type;
-    char *model;
-    char *name;
     char *info_str;
     unsigned int queue_index;
     int vring_enable;
@@ -264,11 +262,8 @@ struct CryptoDevBackend {
 
 /**
  * cryptodev_backend_new_client:
- * @model: the cryptodev backend model
- * @name: the cryptodev backend name, can be NULL
  *
- * Creates a new cryptodev backend client object
- * with the @name in the model @model.
+ * Creates a new cryptodev backend client object.
  *
  * The returned object must be released with
  * cryptodev_backend_free_client() when no
@@ -276,9 +271,8 @@ struct CryptoDevBackend {
  *
  * Returns: a new cryptodev backend client object
  */
-CryptoDevBackendClient *
-cryptodev_backend_new_client(const char *model,
-                                    const char *name);
+CryptoDevBackendClient *cryptodev_backend_new_client(void);
+
 /**
  * cryptodev_backend_free_client:
  * @cc: the cryptodev backend client object
Signature · 1 line
2.20.1
author
#3 lore raw

PATCH 3/8 cryptodev: Introduce cryptodev alg type in QAPI

In reply to zhenwei pi

Introduce cryptodev alg type in cryptodev.json, then apply this to related codes, and drop 'enum CryptoDevBackendAlgType'.

There are two options:
1, { 'enum': 'QCryptodevBackendAlgType',
'prefix': 'CRYPTODEV_BACKEND_ALG',
'data': ['sym', 'asym']}
Then we can keep 'CRYPTODEV_BACKEND_ALG_SYM' and avoid lots of changes.
2, changes in this patch(with prefix 'QCRYPTODEV_BACKEND_ALG').

To avoid breaking the rule of QAPI, use 2 here.

---
 backends/cryptodev-builtin.c |  6 +++---
 backends/cryptodev-lkcf.c    |  4 ++--
 backends/cryptodev.c         |  6 +++---
 hw/virtio/virtio-crypto.c    | 14 +++++++-------
 include/sysemu/cryptodev.h   |  8 +-------
 qapi/cryptodev.json          | 14 ++++++++++++++
 6 files changed, 30 insertions(+), 22 deletions(-)
backends/cryptodev-builtin.c +3 -3
index 08895271eb..5fb7b8f43f 100644
@@ -537,7 +537,7 @@ static int cryptodev_builtin_operation(
     CryptoDevBackendBuiltinSession *sess;
     CryptoDevBackendSymOpInfo *sym_op_info;
     CryptoDevBackendAsymOpInfo *asym_op_info;
-    enum CryptoDevBackendAlgType algtype = op_info->algtype;
+    enum QCryptodevBackendAlgType algtype = op_info->algtype;
     int status = -VIRTIO_CRYPTO_ERR;
     Error *local_error = NULL;
 
@@ -549,11 +549,11 @@ static int cryptodev_builtin_operation(
     }
 
     sess = builtin->sessions[op_info->session_id];
-    if (algtype == CRYPTODEV_BACKEND_ALG_SYM) {
+    if (algtype == QCRYPTODEV_BACKEND_ALG_SYM) {
         sym_op_info = op_info->u.sym_op_info;
         status = cryptodev_builtin_sym_operation(sess, sym_op_info,
                                                  &local_error);
-    } else if (algtype == CRYPTODEV_BACKEND_ALG_ASYM) {
+    } else if (algtype == QCRYPTODEV_BACKEND_ALG_ASYM) {
         asym_op_info = op_info->u.asym_op_info;
         status = cryptodev_builtin_asym_operation(sess, op_info->op_code,
                                                   asym_op_info, &local_error);
backends/cryptodev-lkcf.c +2 -2
index de3d1867c5..919bf05b75 100644
@@ -477,7 +477,7 @@ static int cryptodev_lkcf_operation(
     CryptoDevBackendLKCF *lkcf =
         CRYPTODEV_BACKEND_LKCF(backend);
     CryptoDevBackendLKCFSession *sess;
-    enum CryptoDevBackendAlgType algtype = op_info->algtype;
+    enum QCryptodevBackendAlgType algtype = op_info->algtype;
     CryptoDevLKCFTask *task;
 
     if (op_info->session_id >= MAX_SESSIONS ||
@@ -488,7 +488,7 @@ static int cryptodev_lkcf_operation(
     }
 
     sess = lkcf->sess[op_info->session_id];
-    if (algtype != CRYPTODEV_BACKEND_ALG_ASYM) {
+    if (algtype != QCRYPTODEV_BACKEND_ALG_ASYM) {
         error_report("algtype not supported: %u", algtype);
         return -VIRTIO_CRYPTO_NOTSUPP;
     }
backends/cryptodev.c +3 -3
index 81941af816..d3caded920 100644
@@ -120,10 +120,10 @@ int cryptodev_backend_crypto_operation(
 {
     VirtIOCryptoReq *req = opaque1;
     CryptoDevBackendOpInfo *op_info = &req->op_info;
-    enum CryptoDevBackendAlgType algtype = req->flags;
+    enum QCryptodevBackendAlgType algtype = req->flags;
 
-    if ((algtype != CRYPTODEV_BACKEND_ALG_SYM)
-        && (algtype != CRYPTODEV_BACKEND_ALG_ASYM)) {
+    if ((algtype != QCRYPTODEV_BACKEND_ALG_SYM)
+        && (algtype != QCRYPTODEV_BACKEND_ALG_ASYM)) {
         error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
         return -VIRTIO_CRYPTO_NOTSUPP;
     }
hw/virtio/virtio-crypto.c +7 -7
index 97da74e719..e0a7e2cbd8 100644
@@ -462,7 +462,7 @@ static void virtio_crypto_init_request(VirtIOCrypto *vcrypto, VirtQueue *vq,
     req->in_iov = NULL;
     req->in_num = 0;
     req->in_len = 0;
-    req->flags = CRYPTODEV_BACKEND_ALG__MAX;
+    req->flags = QCRYPTODEV_BACKEND_ALG__MAX;
     memset(&req->op_info, 0x00, sizeof(req->op_info));
 }
 
@@ -472,7 +472,7 @@ static void virtio_crypto_free_request(VirtIOCryptoReq *req)
         return;
     }
 
-    if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
+    if (req->flags == QCRYPTODEV_BACKEND_ALG_SYM) {
         size_t max_len;
         CryptoDevBackendSymOpInfo *op_info = req->op_info.u.sym_op_info;
 
@@ -485,7 +485,7 @@ static void virtio_crypto_free_request(VirtIOCryptoReq *req)
         /* Zeroize and free request data structure */
         memset(op_info, 0, sizeof(*op_info) + max_len);
         g_free(op_info);
-    } else if (req->flags == CRYPTODEV_BACKEND_ALG_ASYM) {
+    } else if (req->flags == QCRYPTODEV_BACKEND_ALG_ASYM) {
         CryptoDevBackendAsymOpInfo *op_info = req->op_info.u.asym_op_info;
         if (op_info) {
             g_free(op_info->src);
@@ -570,10 +570,10 @@ static void virtio_crypto_req_complete(void *opaque, int ret)
     VirtIODevice *vdev = VIRTIO_DEVICE(vcrypto);
     uint8_t status = -ret;
 
-    if (req->flags == CRYPTODEV_BACKEND_ALG_SYM) {
+    if (req->flags == QCRYPTODEV_BACKEND_ALG_SYM) {
         virtio_crypto_sym_input_data_helper(vdev, req, status,
                                             req->op_info.u.sym_op_info);
-    } else if (req->flags == CRYPTODEV_BACKEND_ALG_ASYM) {
+    } else if (req->flags == QCRYPTODEV_BACKEND_ALG_ASYM) {
         virtio_crypto_akcipher_input_data_helper(vdev, req, status,
                                              req->op_info.u.asym_op_info);
     }
@@ -875,7 +875,7 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request)
     switch (opcode) {
     case VIRTIO_CRYPTO_CIPHER_ENCRYPT:
     case VIRTIO_CRYPTO_CIPHER_DECRYPT:
-        op_info->algtype = request->flags = CRYPTODEV_BACKEND_ALG_SYM;
+        op_info->algtype = request->flags = QCRYPTODEV_BACKEND_ALG_SYM;
         ret = virtio_crypto_handle_sym_req(vcrypto,
                          &req.u.sym_req, op_info,
                          out_iov, out_num);
@@ -885,7 +885,7 @@ virtio_crypto_handle_request(VirtIOCryptoReq *request)
     case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
     case VIRTIO_CRYPTO_AKCIPHER_SIGN:
     case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
-        op_info->algtype = request->flags = CRYPTODEV_BACKEND_ALG_ASYM;
+        op_info->algtype = request->flags = QCRYPTODEV_BACKEND_ALG_ASYM;
         ret = virtio_crypto_handle_asym_req(vcrypto,
                          &req.u.akcipher_req, op_info,
                          out_iov, out_num);
include/sysemu/cryptodev.h +1 -7
index af152d09db..f68a4baf13 100644
@@ -49,12 +49,6 @@ typedef struct CryptoDevBackendPeers CryptoDevBackendPeers;
 typedef struct CryptoDevBackendClient
                      CryptoDevBackendClient;
 
-enum CryptoDevBackendAlgType {
-    CRYPTODEV_BACKEND_ALG_SYM,
-    CRYPTODEV_BACKEND_ALG_ASYM,
-    CRYPTODEV_BACKEND_ALG__MAX,
-};
-
 /**
  * CryptoDevBackendSymSessionInfo:
  *
@@ -181,7 +175,7 @@ typedef struct CryptoDevBackendAsymOpInfo {
 } CryptoDevBackendAsymOpInfo;
 
 typedef struct CryptoDevBackendOpInfo {
-    enum CryptoDevBackendAlgType algtype;
+    enum QCryptodevBackendAlgType algtype;
     uint32_t op_code;
     uint64_t session_id;
     union {
qapi/cryptodev.json +14 -0
index b65edbe183..ebb6852035 100644
@@ -4,6 +4,20 @@
 # This work is licensed under the terms of the GNU GPL, version 2 or later.
 # See the COPYING file in the top-level directory.
 
+##
+# @QCryptodevBackendAlgType:
+#
+# The supported algorithm types of a crypto device.
+#
+# @sym: symmetric encryption
+# @asym: asymmetric Encryption
+#
+# Since: 8.0
+##
+{ 'enum': 'QCryptodevBackendAlgType',
+  'prefix': 'QCRYPTODEV_BACKEND_ALG',
+  'data': ['sym', 'asym']}
+
 ##
 # @QCryptodevBackendType:
 #
Signature · 1 line
2.20.1
author
#4 lore raw

PATCH 4/8 cryptodev: Introduce server type in QAPI

In reply to zhenwei pi

Introduce cryptodev service type in cryptodev.json, then apply this to related codes. Now we can remove VIRTIO_CRYPTO_SERVICE_xxx dependence from QEMU cryptodev.

---
 backends/cryptodev-builtin.c    |  8 ++++----
 backends/cryptodev-lkcf.c       |  2 +-
 backends/cryptodev-vhost-user.c |  6 +++---
 hw/virtio/virtio-crypto.c       | 27 +++++++++++++++++++++++++--
 qapi/cryptodev.json             | 11 +++++++++++
 5 files changed, 44 insertions(+), 10 deletions(-)
backends/cryptodev-builtin.c +4 -4
index 5fb7b8f43f..4987abb7d6 100644
@@ -79,10 +79,10 @@ static void cryptodev_builtin_init(
     backend->conf.peers.ccs[0] = cc;
 
     backend->conf.crypto_services =
-                         1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
-                         1u << VIRTIO_CRYPTO_SERVICE_HASH |
-                         1u << VIRTIO_CRYPTO_SERVICE_MAC |
-                         1u << VIRTIO_CRYPTO_SERVICE_AKCIPHER;
+                         1u << QCRYPTODEV_BACKEND_SERVICE_CIPHER |
+                         1u << QCRYPTODEV_BACKEND_SERVICE_HASH |
+                         1u << QCRYPTODEV_BACKEND_SERVICE_MAC |
+                         1u << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER;
     backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
     backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
     backend->conf.akcipher_algo = 1u << VIRTIO_CRYPTO_AKCIPHER_RSA;
backends/cryptodev-lkcf.c +1 -1
index 919bf05b75..31ec712849 100644
@@ -230,7 +230,7 @@ static void cryptodev_lkcf_init(CryptoDevBackend *backend, Error **errp)
     backend->conf.peers.ccs[0] = cc;
 
     backend->conf.crypto_services =
-        1u << VIRTIO_CRYPTO_SERVICE_AKCIPHER;
+        1u << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER;
     backend->conf.akcipher_algo = 1u << VIRTIO_CRYPTO_AKCIPHER_RSA;
     lkcf->running = true;
 
backends/cryptodev-vhost-user.c +3 -3
index 580bd1abb0..b1d9eb735f 100644
@@ -221,9 +221,9 @@ static void cryptodev_vhost_user_init(
                      cryptodev_vhost_user_event, NULL, s, NULL, true);
 
     backend->conf.crypto_services =
-                         1u << VIRTIO_CRYPTO_SERVICE_CIPHER |
-                         1u << VIRTIO_CRYPTO_SERVICE_HASH |
-                         1u << VIRTIO_CRYPTO_SERVICE_MAC;
+                         1u << QCRYPTODEV_BACKEND_SERVICE_CIPHER |
+                         1u << QCRYPTODEV_BACKEND_SERVICE_HASH |
+                         1u << QCRYPTODEV_BACKEND_SERVICE_MAC;
     backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
     backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
 
hw/virtio/virtio-crypto.c +25 -2
index e0a7e2cbd8..87d9582bc1 100644
@@ -997,12 +997,35 @@ static void virtio_crypto_reset(VirtIODevice *vdev)
     }
 }
 
+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;
+}
+
 static void virtio_crypto_init_config(VirtIODevice *vdev)
 {
     VirtIOCrypto *vcrypto = VIRTIO_CRYPTO(vdev);
 
-    vcrypto->conf.crypto_services =
-                     vcrypto->conf.cryptodev->conf.crypto_services;
+    vcrypto->conf.crypto_services = virtio_crypto_init_services(
+                     vcrypto->conf.cryptodev->conf.crypto_services);
     vcrypto->conf.cipher_algo_l =
                      vcrypto->conf.cryptodev->conf.cipher_algo_l;
     vcrypto->conf.cipher_algo_h =
qapi/cryptodev.json +11 -0
index ebb6852035..8732a30524 100644
@@ -18,6 +18,17 @@
   'prefix': 'QCRYPTODEV_BACKEND_ALG',
   'data': ['sym', 'asym']}
 
+##
+# @QCryptodevBackendServiceType:
+#
+# The supported service types of a crypto device.
+#
+# Since: 8.0
+##
+{ 'enum': 'QCryptodevBackendServiceType',
+  'prefix': 'QCRYPTODEV_BACKEND_SERVICE',
+  'data': ['cipher', 'hash', 'mac', 'aead', 'akcipher']}
+
 ##
 # @QCryptodevBackendType:
 #
Signature · 1 line
2.20.1
author
#5 lore raw

PATCH 5/8 cryptodev: Introduce 'query-cryptodev' QMP command

In reply to zhenwei pi

Now we have a QMP command to query crypto devices:
virsh qemu-monitor-command vm '{"execute": "query-cryptodev"}' | jq {
"return": [

    {
      "service": [
        "akcipher",
        "mac",
        "hash",
        "cipher"
      ],
      "id": "cryptodev1",
      "client": [
        {
          "queue": 0,
          "type": "builtin",
          "info": "cryptodev-builtin0"
        }
      ]
    },
    {
      "service": [
        "akcipher"
      ],
      "id": "cryptodev0",
      "client": [
        {
          "queue": 0,
          "type": "lkcf",
          "info": "cryptodev-lkcf0"
        }
      ]
    }

],
"id": "libvirt-415"
}

---
 backends/cryptodev.c | 49 ++++++++++++++++++++++++++++++++++++++++++++
 qapi/cryptodev.json  | 43 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)
backends/cryptodev.c +49 -0
index d3caded920..bf2f3234c9 100644
@@ -24,6 +24,7 @@
 #include "qemu/osdep.h"
 #include "sysemu/cryptodev.h"
 #include "qapi/error.h"
+#include "qapi/qapi-commands-cryptodev.h"
 #include "qapi/visitor.h"
 #include "qemu/config-file.h"
 #include "qemu/error-report.h"
@@ -33,6 +34,54 @@
 
 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
 
+static int qmp_query_cryptodev_foreach(Object *obj, void *data)
+{
+    CryptoDevBackend *backend;
+    CryptodevInfoList **infolist = data;
+    uint32_t services;
+
+    if (!object_dynamic_cast(obj, TYPE_CRYPTODEV_BACKEND)) {
+        return 0;
+    }
+
+    CryptodevInfo *info = g_new0(CryptodevInfo, 1);
+    info->id = g_strdup(object_get_canonical_path_component(obj));
+
+    backend = CRYPTODEV_BACKEND(obj);
+    services = backend->conf.crypto_services;
+    for (uint32_t i = 0; i < QCRYPTODEV_BACKEND_SERVICE__MAX; i++) {
+        if (services & (1 << i)) {
+            QAPI_LIST_PREPEND(info->service, i);
+        }
+    }
+
+    for (uint32_t i = 0; i < backend->conf.peers.queues; i++) {
+        CryptoDevBackendClient *cc = backend->conf.peers.ccs[i];
+        CryptodevBackendClient *client = g_new0(CryptodevBackendClient, 1);
+
+        client->queue = cc->queue_index;
+        client->type = cc->type;
+        if (cc->info_str) {
+            client->has_info = true;
+            client->info = strdup(cc->info_str);
+        }
+        QAPI_LIST_PREPEND(info->client, client);
+    }
+
+    QAPI_LIST_PREPEND(*infolist, info);
+
+    return 0;
+}
+
+CryptodevInfoList *qmp_query_cryptodev(Error **errp)
+{
+    CryptodevInfoList *list = NULL;
+    Object *objs = container_get(object_get_root(), "/objects");
+
+    object_child_foreach(objs, qmp_query_cryptodev_foreach, &list);
+
+    return list;
+}
 
 CryptoDevBackendClient *cryptodev_backend_new_client(void)
 {
qapi/cryptodev.json +43 -0

46 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#6 lore raw

PATCH 6/8 cryptodev: Support statistics

In reply to zhenwei pi

Introduce cryptodev statistics in QAPI, and record OPS/Bandwidth for each crypto device.

Example of this feature:
virsh qemu-monitor-command vm '{"execute": "query-cryptodev"}' | jq {
"return": [

    {
      "service": [
        "akcipher",
        "mac",
        "hash",
        "cipher"
      ],
      "asym-stat": {
        "encrypt-ops": 0,
        "verify-bytes": 0,
        "sign-ops": 0,
        "verify-ops": 0,
        "sign-bytes": 0,
        "decrypt-bytes": 0,
        "decrypt-ops": 0,
        "encrypt-bytes": 0
      },
      "sym-stat": {
        "encrypt-ops": 40,
        "decrypt-bytes": 5376,
        "decrypt-ops": 40,
        "encrypt-bytes": 5376
      },
      "id": "cryptodev1",
      "client": [
        {
          "queue": 0,
          "type": "builtin",
          "info": "cryptodev-builtin0"
        }
      ]
    },
    {
      "service": [
        "akcipher"
      ],
      "asym-stat": {
        "encrypt-ops": 54,
        "verify-bytes": 8704,
        "sign-ops": 17,
        "verify-ops": 34,
        "sign-bytes": 340,
        "decrypt-bytes": 9215,
        "decrypt-ops": 36,
        "encrypt-bytes": 13294
      },
      "id": "cryptodev0",
      "client": [
        {
          "queue": 0,
          "type": "lkcf",
          "info": "cryptodev-lkcf0"
        }
      ]
    }

],
"id": "libvirt-424"
}

---
 backends/cryptodev-builtin.c |  8 ++++-
 backends/cryptodev-lkcf.c    |  1 +
 backends/cryptodev.c         | 37 +++++++++++++++++++++--
 include/sysemu/cryptodev.h   | 30 +++++++++++++++++++
 qapi/cryptodev.json          | 58 +++++++++++++++++++++++++++++++++++-
 5 files changed, 130 insertions(+), 4 deletions(-)
backends/cryptodev-builtin.c +7 -1
index 4987abb7d6..a548c66e39 100644
@@ -95,6 +95,9 @@ static void cryptodev_builtin_init(
     backend->conf.max_cipher_key_len = CRYPTODEV_BUITLIN_MAX_CIPHER_KEY_LEN;
     backend->conf.max_auth_key_len = CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN;
 
+    backend->sym_stat = g_new0(QCryptodevBackendSymStat, 1);
+    backend->asym_stat = g_new0(QCryptodevBackendAsymStat, 1);
+
     cryptodev_backend_set_ready(backend, true);
 }
 
@@ -433,6 +436,7 @@ static int cryptodev_builtin_close_session(
 }
 
 static int cryptodev_builtin_sym_operation(
+                 CryptoDevBackend *backend,
                  CryptoDevBackendBuiltinSession *sess,
                  CryptoDevBackendSymOpInfo *op_info, Error **errp)
 {
@@ -458,12 +462,14 @@ static int cryptodev_builtin_sym_operation(
         if (ret < 0) {
             return -VIRTIO_CRYPTO_ERR;
         }
+        QCryptodevSymStatIncEncrypt(backend, op_info->src_len);
     } else {
         ret = qcrypto_cipher_decrypt(sess->cipher, op_info->src,
                                      op_info->dst, op_info->src_len, errp);
         if (ret < 0) {
             return -VIRTIO_CRYPTO_ERR;
         }
+        QCryptodevSymStatIncDecrypt(backend, op_info->src_len);
     }
 
     return VIRTIO_CRYPTO_OK;
@@ -551,7 +557,7 @@ static int cryptodev_builtin_operation(
     sess = builtin->sessions[op_info->session_id];
     if (algtype == QCRYPTODEV_BACKEND_ALG_SYM) {
         sym_op_info = op_info->u.sym_op_info;
-        status = cryptodev_builtin_sym_operation(sess, sym_op_info,
+        status = cryptodev_builtin_sym_operation(backend, sess, sym_op_info,
                                                  &local_error);
     } else if (algtype == QCRYPTODEV_BACKEND_ALG_ASYM) {
         asym_op_info = op_info->u.asym_op_info;
backends/cryptodev-lkcf.c +1 -0
index 31ec712849..609a39bb7e 100644
@@ -233,6 +233,7 @@ static void cryptodev_lkcf_init(CryptoDevBackend *backend, Error **errp)
         1u << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER;
     backend->conf.akcipher_algo = 1u << VIRTIO_CRYPTO_AKCIPHER_RSA;
     lkcf->running = true;
+    backend->asym_stat = g_new0(QCryptodevBackendAsymStat, 1);
 
     QSIMPLEQ_INIT(&lkcf->requests);
     QSIMPLEQ_INIT(&lkcf->responses);
backends/cryptodev.c +35 -2
index bf2f3234c9..b93b1d25f2 100644
@@ -48,6 +48,18 @@ static int qmp_query_cryptodev_foreach(Object *obj, void *data)
     info->id = g_strdup(object_get_canonical_path_component(obj));
 
     backend = CRYPTODEV_BACKEND(obj);
+    if (backend->sym_stat) {
+        info->has_sym_stat = true;
+        info->sym_stat = g_memdup2(backend->sym_stat,
+                                sizeof(QCryptodevBackendSymStat));
+    }
+
+    if (backend->asym_stat) {
+        info->has_asym_stat = true;
+        info->asym_stat = g_memdup2(backend->asym_stat,
+                                sizeof(QCryptodevBackendAsymStat));
+    }
+
     services = backend->conf.crypto_services;
     for (uint32_t i = 0; i < QCRYPTODEV_BACKEND_SERVICE__MAX; i++) {
         if (services & (1 << i)) {
@@ -111,6 +123,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(
@@ -171,8 +186,26 @@ int cryptodev_backend_crypto_operation(
     CryptoDevBackendOpInfo *op_info = &req->op_info;
     enum QCryptodevBackendAlgType algtype = req->flags;
 
-    if ((algtype != QCRYPTODEV_BACKEND_ALG_SYM)
-        && (algtype != QCRYPTODEV_BACKEND_ALG_ASYM)) {
+    /* symmetric statistics need to be recorded in driver */
+    if (algtype == QCRYPTODEV_BACKEND_ALG_ASYM) {
+        CryptoDevBackendAsymOpInfo *asym_op_info = op_info->u.asym_op_info;
+        switch (op_info->op_code) {
+        case VIRTIO_CRYPTO_AKCIPHER_ENCRYPT:
+            QCryptodevAsymStatIncEncrypt(backend, asym_op_info->src_len);
+            break;
+        case VIRTIO_CRYPTO_AKCIPHER_DECRYPT:
+            QCryptodevAsymStatIncDecrypt(backend, asym_op_info->src_len);
+            break;
+        case VIRTIO_CRYPTO_AKCIPHER_SIGN:
+            QCryptodevAsymStatIncSign(backend, asym_op_info->src_len);
+            break;
+        case VIRTIO_CRYPTO_AKCIPHER_VERIFY:
+            QCryptodevAsymStatIncVerify(backend, asym_op_info->src_len);
+            break;
+        default:
+            return -VIRTIO_CRYPTO_NOTSUPP;
+        }
+    } else if (algtype != QCRYPTODEV_BACKEND_ALG_SYM) {
         error_report("Unsupported cryptodev alg type: %" PRIu32 "", algtype);
         return -VIRTIO_CRYPTO_NOTSUPP;
     }
include/sysemu/cryptodev.h +30 -0
index f68a4baf13..c154c52039 100644
@@ -252,8 +252,38 @@ 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:
  *
qapi/cryptodev.json +57 -1

70 lines not on this page · raw message

Signature · 1 line
2.20.1
author
#7 lore raw

PATCH 7/8 cryptodev-builtin: Detect akcipher capability

In reply to zhenwei pi

Rather than exposing akcipher service/RSA algorithm to virtio crypto device unconditionally, detect akcipher capability from akcipher crypto framework. This avoids unsuccessful requests.

---
 backends/cryptodev-builtin.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
backends/cryptodev-builtin.c +21 -4
index a548c66e39..9e94d2bfcc 100644
@@ -59,6 +59,25 @@ struct CryptoDevBackendBuiltin {
     CryptoDevBackendBuiltinSession *sessions[MAX_NUM_SESSIONS];
 };
 
+static void cryptodev_builtin_init_akcipher(CryptoDevBackend *backend)
+{
+    QCryptoAkCipherOptions opts;
+    bool supported = false;
+
+    opts.alg = QCRYPTO_AKCIPHER_ALG_RSA;
+    opts.u.rsa.padding_alg = QCRYPTO_RSA_PADDING_ALG_RAW;
+    if (qcrypto_akcipher_supports(&opts)) {
+        backend->conf.crypto_services |=
+                     (1u << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER);
+        backend->conf.akcipher_algo = 1u << VIRTIO_CRYPTO_AKCIPHER_RSA;
+        supported = true;
+    }
+
+    if (supported) {
+        backend->asym_stat = g_new0(QCryptodevBackendAsymStat, 1);
+    }
+}
+
 static void cryptodev_builtin_init(
              CryptoDevBackend *backend, Error **errp)
 {
@@ -81,11 +100,9 @@ static void cryptodev_builtin_init(
     backend->conf.crypto_services =
                          1u << QCRYPTODEV_BACKEND_SERVICE_CIPHER |
                          1u << QCRYPTODEV_BACKEND_SERVICE_HASH |
-                         1u << QCRYPTODEV_BACKEND_SERVICE_MAC |
-                         1u << QCRYPTODEV_BACKEND_SERVICE_AKCIPHER;
+                         1u << QCRYPTODEV_BACKEND_SERVICE_MAC;
     backend->conf.cipher_algo_l = 1u << VIRTIO_CRYPTO_CIPHER_AES_CBC;
     backend->conf.hash_algo = 1u << VIRTIO_CRYPTO_HASH_SHA1;
-    backend->conf.akcipher_algo = 1u << VIRTIO_CRYPTO_AKCIPHER_RSA;
     /*
      * Set the Maximum length of crypto request.
      * Why this value? Just avoid to overflow when
@@ -96,7 +113,7 @@ static void cryptodev_builtin_init(
     backend->conf.max_auth_key_len = CRYPTODEV_BUITLIN_MAX_AUTH_KEY_LEN;
 
     backend->sym_stat = g_new0(QCryptodevBackendSymStat, 1);
-    backend->asym_stat = g_new0(QCryptodevBackendAsymStat, 1);
+    cryptodev_builtin_init_akcipher(backend);
 
     cryptodev_backend_set_ready(backend, true);
 }
Signature · 1 line
2.20.1
author
#8 lore raw

PATCH 8/8 hmp: add cryptodev info command

In reply to zhenwei pi

Example of this command:

 # virsh qemu-monitor-command vm --hmp info cryptodev
cryptodev1: service=[akcipher|mac|hash|cipher]
    queue 0: type=builtin
cryptodev0: service=[akcipher]
    queue 0: type=lkcf
---
 hmp-commands-info.hx  | 14 ++++++++++++++
 include/monitor/hmp.h |  1 +
 monitor/hmp-cmds.c    | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)
hmp-commands-info.hx +14 -0
index 754b1e8408..47d63d26db 100644
@@ -993,3 +993,17 @@ SRST
   ``info virtio-queue-element`` *path* *queue* [*index*]
     Display element of a given virtio queue
 ERST
+
+    {
+        .name       = "cryptodev",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show the crypto devices",
+        .cmd        = hmp_info_cryptodev,
+        .flags      = "p",
+    },
+
+SRST
+  ``info cryptodev``
+    Show the crypto devices.
+ERST
include/monitor/hmp.h +1 -0
index dfbc0c9a2f..b6b2b49202 100644
@@ -143,5 +143,6 @@ void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict);
 void hmp_human_readable_text_helper(Monitor *mon,
                                     HumanReadableText *(*qmp_handler)(Error **));
 void hmp_info_stats(Monitor *mon, const QDict *qdict);
+void hmp_info_cryptodev(Monitor *mon, const QDict *qdict);
 
 #endif
monitor/hmp-cmds.c +36 -0
index 01b789a79e..3f1054aa1e 100644
@@ -33,6 +33,7 @@
 #include "qapi/qapi-commands-block.h"
 #include "qapi/qapi-commands-char.h"
 #include "qapi/qapi-commands-control.h"
+#include "qapi/qapi-commands-cryptodev.h"
 #include "qapi/qapi-commands-machine.h"
 #include "qapi/qapi-commands-migration.h"
 #include "qapi/qapi-commands-misc.h"
@@ -2761,3 +2762,38 @@ void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict)
 
     qapi_free_VirtioQueueElement(e);
 }
+
+void hmp_info_cryptodev(Monitor *mon, const QDict *qdict)
+{
+    CryptodevInfoList *info_list;
+    CryptodevInfo *info;
+    QCryptodevBackendServiceTypeList *service_list;
+    CryptodevBackendClientList *client_list;
+    CryptodevBackendClient *client;
+    char services[128] = {};
+    int len;
+
+    info_list = qmp_query_cryptodev(NULL);
+    for ( ; info_list; info_list = info_list->next) {
+        info = info_list->value;
+
+        service_list = info->service;
+        for (len = 0; service_list; service_list = service_list->next) {
+            len += snprintf(services + len, sizeof(services) - len, "%s|",
+                QCryptodevBackendServiceType_str(service_list->value));
+        }
+        if (len) {
+            services[len - 1] = '\0'; /* strip last char '|' */
+        }
+        monitor_printf(mon, "%s: service=[%s]\n", info->id, services);
+
+        client_list = info->client;
+        for ( ; client_list; client_list = client_list->next) {
+            client = client_list->value;
+            monitor_printf(mon, "    queue %ld: type=%s\n", client->queue,
+                          QCryptodevBackendType_str(client->type));
+        }
+    }
+
+    qapi_free_CryptodevInfoList(info_list);
+}
Signature · 1 line
2.20.1
#9 lore raw

Re: [PATCH for 8.0 8/8] hmp: add cryptodev info command

Quoted 62 lines · * zhenwei pi ([email protected]) wrote:

Example of this command:

 # virsh qemu-monitor-command vm --hmp info cryptodev
cryptodev1: service=[akcipher|mac|hash|cipher]
    queue 0: type=builtin
cryptodev0: service=[akcipher]
    queue 0: type=lkcf
---
 hmp-commands-info.hx  | 14 ++++++++++++++
 include/monitor/hmp.h |  1 +
 monitor/hmp-cmds.c    | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+)
hmp-commands-info.hx +14 -0
index 754b1e8408..47d63d26db 100644
@@ -993,3 +993,17 @@ SRST
   ``info virtio-queue-element`` *path* *queue* [*index*]
     Display element of a given virtio queue
 ERST
+
+    {
+        .name       = "cryptodev",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show the crypto devices",
+        .cmd        = hmp_info_cryptodev,
+        .flags      = "p",
+    },
+
+SRST
+  ``info cryptodev``
+    Show the crypto devices.
+ERST
include/monitor/hmp.h +1 -0
index dfbc0c9a2f..b6b2b49202 100644
@@ -143,5 +143,6 @@ void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict);
 void hmp_human_readable_text_helper(Monitor *mon,
                                     HumanReadableText *(*qmp_handler)(Error **));
 void hmp_info_stats(Monitor *mon, const QDict *qdict);
+void hmp_info_cryptodev(Monitor *mon, const QDict *qdict);
 
 #endif
monitor/hmp-cmds.c +1 -0
index 01b789a79e..3f1054aa1e 100644
@@ -33,6 +33,7 @@
 #include "qapi/qapi-commands-block.h"
 #include "qapi/qapi-commands-char.h"
 #include "qapi/qapi-commands-control.h"
+#include "qapi/qapi-commands-cryptodev.h"
 #include "qapi/qapi-commands-machine.h"
 #include "qapi/qapi-commands-migration.h"
 #include "qapi/qapi-commands-misc.h"
@@ -2761,3 +2762,38 @@ void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict)
 
     qapi_free_VirtioQueueElement(e);

monitor/hmp-cmds.c void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict) · line 2773

Quoted 10 lines
monitor/hmp-cmds.c +9 -0
 }
+
+void hmp_info_cryptodev(Monitor *mon, const QDict *qdict)
+{
+    CryptodevInfoList *info_list;
+    CryptodevInfo *info;
+    QCryptodevBackendServiceTypeList *service_list;
+    CryptodevBackendClientList *client_list;
+    CryptodevBackendClient *client;
+    char services[128] = {};

I'd rather avoid magic length buffers; the magic is always the wrong number!

monitor/hmp-cmds.c void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict) · line 2777

Quoted 4 lines
monitor/hmp-cmds.c +4 -0
+    int len;
+
+    info_list = qmp_query_cryptodev(NULL);
+    for ( ; info_list; info_list = info_list->next) {

maybe:

 for ( info_list = qmp_query_cryptodev(NULL);
       info_list;
       info_list = info_list->next) {

monitor/hmp-cmds.c void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict) · line 2783

Quoted 6 lines
monitor/hmp-cmds.c +6 -0
+        info = info_list->value;
+
+        service_list = info->service;
+        for (len = 0; service_list; service_list = service_list->next) {
+            len += snprintf(services + len, sizeof(services) - len, "%s|",
+                QCryptodevBackendServiceType_str(service_list->value));

Consider using a dynamically allocated string and then just using g_strconcat or g_strjoin() to glue them all together.

    new_services = g_strjoin("|", services,  NULL);   ?
    g_free(services);
    services = new_services;

Maybe?

Quoted 19 lines
monitor/hmp-cmds.c +16 -0
+        }
+        if (len) {
+            services[len - 1] = '\0'; /* strip last char '|' */
+        }
+        monitor_printf(mon, "%s: service=[%s]\n", info->id, services);
+
+        client_list = info->client;
+        for ( ; client_list; client_list = client_list->next) {
+            client = client_list->value;
+            monitor_printf(mon, "    queue %ld: type=%s\n", client->queue,
+                          QCryptodevBackendType_str(client->type));
+        }
+    }
+
+    qapi_free_CryptodevInfoList(info_list);
+}
Signature · 1 line
2.20.1
Signature · 1 line
Dr. David Alan Gilbert / [email protected] / Manchester, UK
author
#10 lore raw

Re: Re: [PATCH for 8.0 8/8] hmp: add cryptodev info command

Quoted 94 lines · Dr. David Alan Gilbert
Quoted 62 lines · * zhenwei pi ([email protected]) wrote:

Example of this command:

  # virsh qemu-monitor-command vm --hmp info cryptodev
cryptodev1: service=[akcipher|mac|hash|cipher]
     queue 0: type=builtin
cryptodev0: service=[akcipher]
     queue 0: type=lkcf
---
  hmp-commands-info.hx  | 14 ++++++++++++++
  include/monitor/hmp.h |  1 +
  monitor/hmp-cmds.c    | 36 ++++++++++++++++++++++++++++++++++++
  3 files changed, 51 insertions(+)
hmp-commands-info.hx +14 -0
index 754b1e8408..47d63d26db 100644
@@ -993,3 +993,17 @@ SRST
    ``info virtio-queue-element`` *path* *queue* [*index*]
      Display element of a given virtio queue
  ERST
+
+    {
+        .name       = "cryptodev",
+        .args_type  = "",
+        .params     = "",
+        .help       = "show the crypto devices",
+        .cmd        = hmp_info_cryptodev,
+        .flags      = "p",
+    },
+
+SRST
+  ``info cryptodev``
+    Show the crypto devices.
+ERST
include/monitor/hmp.h +1 -0
index dfbc0c9a2f..b6b2b49202 100644
@@ -143,5 +143,6 @@ void hmp_info_vcpu_dirty_limit(Monitor *mon, const QDict *qdict);
  void hmp_human_readable_text_helper(Monitor *mon,
                                      HumanReadableText *(*qmp_handler)(Error **));
  void hmp_info_stats(Monitor *mon, const QDict *qdict);
+void hmp_info_cryptodev(Monitor *mon, const QDict *qdict);
 
  #endif
monitor/hmp-cmds.c +1 -0
index 01b789a79e..3f1054aa1e 100644
@@ -33,6 +33,7 @@
  #include "qapi/qapi-commands-block.h"
  #include "qapi/qapi-commands-char.h"
  #include "qapi/qapi-commands-control.h"
+#include "qapi/qapi-commands-cryptodev.h"
  #include "qapi/qapi-commands-machine.h"
  #include "qapi/qapi-commands-migration.h"
  #include "qapi/qapi-commands-misc.h"
@@ -2761,3 +2762,38 @@ void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict)
 
      qapi_free_VirtioQueueElement(e);

monitor/hmp-cmds.c void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict) · line 2773

Quoted 10 lines
monitor/hmp-cmds.c +9 -0
  }
+
+void hmp_info_cryptodev(Monitor *mon, const QDict *qdict)
+{
+    CryptodevInfoList *info_list;
+    CryptodevInfo *info;
+    QCryptodevBackendServiceTypeList *service_list;
+    CryptodevBackendClientList *client_list;
+    CryptodevBackendClient *client;
+    char services[128] = {};

I'd rather avoid magic length buffers; the magic is always the wrong number!

monitor/hmp-cmds.c void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict) · line 2777

Quoted 4 lines
monitor/hmp-cmds.c +4 -0
+    int len;
+
+    info_list = qmp_query_cryptodev(NULL);
+    for ( ; info_list; info_list = info_list->next) {

maybe:

  for ( info_list = qmp_query_cryptodev(NULL);
        info_list;
        info_list = info_list->next) {

monitor/hmp-cmds.c void hmp_virtio_queue_element(Monitor *mon, const QDict *qdict) · line 2783

Quoted 6 lines
monitor/hmp-cmds.c +6 -0
+        info = info_list->value;
+
+        service_list = info->service;
+        for (len = 0; service_list; service_list = service_list->next) {
+            len += snprintf(services + len, sizeof(services) - len, "%s|",
+                QCryptodevBackendServiceType_str(service_list->value));
Quoted 9 lines

Consider using a dynamically allocated string and then just using g_strconcat or g_strjoin() to glue them all together.

     new_services = g_strjoin("|", services,  NULL);   ?
     g_free(services);
     services = new_services;

Maybe?

Hi, I'll fix these in the next version, thanks!

Quoted 20 lines
Quoted 20 lines
monitor/hmp-cmds.c +16 -0
+        }
+        if (len) {
+            services[len - 1] = '\0'; /* strip last char '|' */
+        }
+        monitor_printf(mon, "%s: service=[%s]\n", info->id, services);
+
+        client_list = info->client;
+        for ( ; client_list; client_list = client_list->next) {
+            client = client_list->value;
+            monitor_printf(mon, "    queue %ld: type=%s\n", client->queue,
+                          QCryptodevBackendType_str(client->type));
+        }
+    }
+
+    qapi_free_CryptodevInfoList(info_list);
+}
Signature · 1 line
2.20.1
Signature · 1 line
zhenwei pi
author
#11 lore raw

PING: [PATCH for 8.0 0/8] Refactor cryptodev

In reply to zhenwei pi

Hi, Lei

Dr. David Alan Gilbert has already reviewed the hmp part, could you please review the cryptodev/virtio-crypto part?

I volunteer to co-maintain the cryptodev part, I'd like to add myself as cryptodev maintainer in the next version, do you have any suggestion?

Quoted 36 lines · zhenwei pi

The main changes in this series:
- introduce cryptodev.json to describe the attributes of crypto device, then drop duplicated type declare, remove some virtio related dependence.
- add statistics: OPS and bandwidth.
- add QMP command: query-cryptodev
- add HMP info command: cryptodev
- misc fix: detect akcipher capability instead of exposing akcipher service unconditionally.

Zhenwei Pi (8):
cryptodev: Introduce cryptodev.json
cryptodev: Remove 'name' & 'model' fields
cryptodev: Introduce cryptodev alg type in QAPI
cryptodev: Introduce server type in QAPI
cryptodev: Introduce 'query-cryptodev' QMP command
cryptodev: Support statistics
cryptodev-builtin: Detect akcipher capability
hmp: add cryptodev info command

  MAINTAINERS                     |   1 +
  backends/cryptodev-builtin.c    |  46 +++++++---
  backends/cryptodev-lkcf.c       |  11 +--
  backends/cryptodev-vhost-user.c |  13 ++-
  backends/cryptodev-vhost.c      |   4 +-
  backends/cryptodev.c            | 101 ++++++++++++++++++----
  hmp-commands-info.hx            |  14 ++++
  hw/virtio/virtio-crypto.c       |  41 +++++++--
  include/monitor/hmp.h           |   1 +
  include/sysemu/cryptodev.h      |  61 ++++++++------
  monitor/hmp-cmds.c              |  36 ++++++++
  qapi/cryptodev.json             | 144 ++++++++++++++++++++++++++++++++
  qapi/meson.build                |   1 +
  qapi/qapi-schema.json           |   1 +
  14 files changed, 401 insertions(+), 74 deletions(-)

create mode 100644 qapi/cryptodev.json

Signature · 1 line
zhenwei pi