diff mbox series

[v3,05/12] cryptodev: Introduce 'query-cryptodev' QMP command

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

Commit Message

zhenwei pi Jan. 28, 2023, 3:56 a.m. UTC
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"
        }
      ]
    },
    {
      "service": [
        "akcipher"
      ],
      "id": "cryptodev0",
      "client": [
        {
          "queue": 0,
          "type": "lkcf"
        }
      ]
    }
  ],
  "id": "libvirt-417"
}

Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
---
 backends/cryptodev.c | 45 ++++++++++++++++++++++++++++++++++++++++++++
 qapi/cryptodev.json  | 44 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)

Comments

Michael S. Tsirkin Jan. 28, 2023, 11:30 a.m. UTC | #1
On Sat, Jan 28, 2023 at 11:56:26AM +0800, zhenwei pi wrote:
> 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"
>         }
>       ]
>     },
>     {
>       "service": [
>         "akcipher"
>       ],
>       "id": "cryptodev0",
>       "client": [
>         {
>           "queue": 0,
>           "type": "lkcf"
>         }
>       ]
>     }
>   ],
>   "id": "libvirt-417"
> }
> 
> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
> ---
>  backends/cryptodev.c | 45 ++++++++++++++++++++++++++++++++++++++++++++
>  qapi/cryptodev.json  | 44 +++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 89 insertions(+)
> 
> diff --git a/backends/cryptodev.c b/backends/cryptodev.c
> index c2a053db0e..d51eeb5ee4 100644
> --- a/backends/cryptodev.c
> +++ b/backends/cryptodev.c
> @@ -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,50 @@
>  
>  static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
>  
> +static int qmp_query_cryptodev_foreach(Object *obj, void *data)
> +{
> +    CryptoDevBackend *backend;
> +    CryptodevInfoList **infolist = data;
> +    uint32_t services, i;
> +
> +    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 (i = 0; i < QCRYPTODEV_BACKEND_SERVICE__MAX; i++) {
> +        if (services & (1 << i)) {
> +            QAPI_LIST_PREPEND(info->service, i);
> +        }
> +    }
> +
> +    for (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;
> +        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)
>  {
> diff --git a/qapi/cryptodev.json b/qapi/cryptodev.json
> index 8732a30524..940078ace0 100644
> --- a/qapi/cryptodev.json
> +++ b/qapi/cryptodev.json
> @@ -43,3 +43,47 @@
>  { 'enum': 'QCryptodevBackendType',
>    'prefix': 'QCRYPTODEV_BACKEND_TYPE',
>    'data': ['builtin', 'vhost-user', 'lkcf']}
> +
> +##
> +# @CryptodevBackendClient:
> +#
> +# Information about a queue of crypto device.
> +#
> +# @queue: the queue index of the crypto device
> +#
> +# @type: the type of the crypto device
> +#
> +# Since: 8.0
> +##
> +{ 'struct': 'CryptodevBackendClient',
> +  'data': { 'queue': 'uint32',
> +            'type': 'QCryptodevBackendType' } }
> +
> +##
> +# @CryptodevInfo:
> +#
> +# Information about a crypto device.
> +#
> +# @id: the id of the crypto device
> +#
> +# @service: supported service types of a crypto device
> +#
> +# @client: the additional infomation of the crypto device
> +#
> +# Since: 8.0
> +##
> +{ 'struct': 'CryptodevInfo',
> +  'data': { 'id': 'str',
> +            'service': ['QCryptodevBackendServiceType'],
> +            'client': ['CryptodevBackendClient'] } }

So we end up with both CryptodevBackendClient and
CryptoDevBackendClient. Please don't do this.


> +
> +##
> +# @query-cryptodev:
> +#
> +# Returns information about current crypto devices.
> +#
> +# Returns: a list of @CryptodevInfo
> +#
> +# Since: 8.0
> +##
> +{ 'command': 'query-cryptodev', 'returns': ['CryptodevInfo']}
> -- 
> 2.34.1
zhenwei pi Jan. 28, 2023, 12:50 p.m. UTC | #2
On 1/28/23 19:30, Michael S. Tsirkin wrote:
> On Sat, Jan 28, 2023 at 11:56:26AM +0800, zhenwei pi wrote:
>> 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"
>>          }
>>        ]
>>      },
>>      {
>>        "service": [
>>          "akcipher"
>>        ],
>>        "id": "cryptodev0",
>>        "client": [
>>          {
>>            "queue": 0,
>>            "type": "lkcf"
>>          }
>>        ]
>>      }
>>    ],
>>    "id": "libvirt-417"
>> }
>>
>> Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
>> ---
>>   backends/cryptodev.c | 45 ++++++++++++++++++++++++++++++++++++++++++++
>>   qapi/cryptodev.json  | 44 +++++++++++++++++++++++++++++++++++++++++++
>>   2 files changed, 89 insertions(+)
>>
>> diff --git a/backends/cryptodev.c b/backends/cryptodev.c
>> index c2a053db0e..d51eeb5ee4 100644
>> --- a/backends/cryptodev.c
>> +++ b/backends/cryptodev.c
>> @@ -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,50 @@
>>   
>>   static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
>>   
>> +static int qmp_query_cryptodev_foreach(Object *obj, void *data)
>> +{
>> +    CryptoDevBackend *backend;
>> +    CryptodevInfoList **infolist = data;
>> +    uint32_t services, i;
>> +
>> +    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 (i = 0; i < QCRYPTODEV_BACKEND_SERVICE__MAX; i++) {
>> +        if (services & (1 << i)) {
>> +            QAPI_LIST_PREPEND(info->service, i);
>> +        }
>> +    }
>> +
>> +    for (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;
>> +        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)
>>   {
>> diff --git a/qapi/cryptodev.json b/qapi/cryptodev.json
>> index 8732a30524..940078ace0 100644
>> --- a/qapi/cryptodev.json
>> +++ b/qapi/cryptodev.json
>> @@ -43,3 +43,47 @@
>>   { 'enum': 'QCryptodevBackendType',
>>     'prefix': 'QCRYPTODEV_BACKEND_TYPE',
>>     'data': ['builtin', 'vhost-user', 'lkcf']}
>> +
>> +##
>> +# @CryptodevBackendClient:
>> +#
>> +# Information about a queue of crypto device.
>> +#
>> +# @queue: the queue index of the crypto device
>> +#
>> +# @type: the type of the crypto device
>> +#
>> +# Since: 8.0
>> +##
>> +{ 'struct': 'CryptodevBackendClient',
>> +  'data': { 'queue': 'uint32',
>> +            'type': 'QCryptodevBackendType' } }
>> +
>> +##
>> +# @CryptodevInfo:
>> +#
>> +# Information about a crypto device.
>> +#
>> +# @id: the id of the crypto device
>> +#
>> +# @service: supported service types of a crypto device
>> +#
>> +# @client: the additional infomation of the crypto device
>> +#
>> +# Since: 8.0
>> +##
>> +{ 'struct': 'CryptodevInfo',
>> +  'data': { 'id': 'str',
>> +            'service': ['QCryptodevBackendServiceType'],
>> +            'client': ['CryptodevBackendClient'] } }
> 
> So we end up with both CryptodevBackendClient and
> CryptoDevBackendClient. Please don't do this.
> 
>

Sorry, my fault, they should be *QCryptodevBackendClient* and 
*QCryptodevInfo^.

>> +
>> +##
>> +# @query-cryptodev:
>> +#
>> +# Returns information about current crypto devices.
>> +#
>> +# Returns: a list of @CryptodevInfo
>> +#
>> +# Since: 8.0
>> +##
>> +{ 'command': 'query-cryptodev', 'returns': ['CryptodevInfo']}
>> -- 
>> 2.34.1
>
diff mbox series

Patch

diff --git a/backends/cryptodev.c b/backends/cryptodev.c
index c2a053db0e..d51eeb5ee4 100644
--- a/backends/cryptodev.c
+++ b/backends/cryptodev.c
@@ -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,50 @@ 
 
 static QTAILQ_HEAD(, CryptoDevBackendClient) crypto_clients;
 
+static int qmp_query_cryptodev_foreach(Object *obj, void *data)
+{
+    CryptoDevBackend *backend;
+    CryptodevInfoList **infolist = data;
+    uint32_t services, i;
+
+    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 (i = 0; i < QCRYPTODEV_BACKEND_SERVICE__MAX; i++) {
+        if (services & (1 << i)) {
+            QAPI_LIST_PREPEND(info->service, i);
+        }
+    }
+
+    for (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;
+        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)
 {
diff --git a/qapi/cryptodev.json b/qapi/cryptodev.json
index 8732a30524..940078ace0 100644
--- a/qapi/cryptodev.json
+++ b/qapi/cryptodev.json
@@ -43,3 +43,47 @@ 
 { 'enum': 'QCryptodevBackendType',
   'prefix': 'QCRYPTODEV_BACKEND_TYPE',
   'data': ['builtin', 'vhost-user', 'lkcf']}
+
+##
+# @CryptodevBackendClient:
+#
+# Information about a queue of crypto device.
+#
+# @queue: the queue index of the crypto device
+#
+# @type: the type of the crypto device
+#
+# Since: 8.0
+##
+{ 'struct': 'CryptodevBackendClient',
+  'data': { 'queue': 'uint32',
+            'type': 'QCryptodevBackendType' } }
+
+##
+# @CryptodevInfo:
+#
+# Information about a crypto device.
+#
+# @id: the id of the crypto device
+#
+# @service: supported service types of a crypto device
+#
+# @client: the additional infomation of the crypto device
+#
+# Since: 8.0
+##
+{ 'struct': 'CryptodevInfo',
+  'data': { 'id': 'str',
+            'service': ['QCryptodevBackendServiceType'],
+            'client': ['CryptodevBackendClient'] } }
+
+##
+# @query-cryptodev:
+#
+# Returns information about current crypto devices.
+#
+# Returns: a list of @CryptodevInfo
+#
+# Since: 8.0
+##
+{ 'command': 'query-cryptodev', 'returns': ['CryptodevInfo']}