Skip to contentSkip to conversation

qmp: remove virtio_list, search QOM tree instead

PATCH Superseded Jonah Palmer 2 participants 4 messages May 16, 2023 – Jun 5, 2023

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 qmp: remove virtio_list, search QOM tree instead

The virtio_list duplicates information about virtio devices that already exist in the QOM composition tree. Instead of creating this list of realized virtio devices, search the QOM composition tree instead.

This patch modifies the QMP command qmp_x_query_virtio to instead search the partial paths of '/machine/peripheral/' & '/machine/peripheral-anon/' in the QOM composition tree for virtio devices.

A device is found to be a valid virtio device if (1) it has a canonical path ending with 'virtio-backend' and (2) the device has been realized.

---
 hw/virtio/virtio-qmp.c | 121 ++++++++++++++++++++++++++---------------
 hw/virtio/virtio-qmp.h |   8 +--
 hw/virtio/virtio.c     |   6 --
 3 files changed, 77 insertions(+), 58 deletions(-)
hw/virtio/virtio-qmp.c +76 -45
index b70148aba9..3afc3a8ece 100644
@@ -666,68 +666,99 @@ VirtioDeviceFeatures *qmp_decode_features(uint16_t device_id, uint64_t bitmap)
 VirtioInfoList *qmp_x_query_virtio(Error **errp)
 {
     VirtioInfoList *list = NULL;
-    VirtioInfoList *node;
-    VirtIODevice *vdev;
 
-    QTAILQ_FOREACH(vdev, &virtio_list, next) {
-        DeviceState *dev = DEVICE(vdev);
-        Error *err = NULL;
-        QObject *obj = qmp_qom_get(dev->canonical_path, "realized", &err);
-
-        if (err == NULL) {
-            GString *is_realized = qobject_to_json_pretty(obj, true);
-            /* virtio device is NOT realized, remove it from list */
-            if (!strncmp(is_realized->str, "false", 4)) {
-                QTAILQ_REMOVE(&virtio_list, vdev, next);
-            } else {
+    /* Query the QOM composition tree for virtio devices */
+    qmp_set_virtio_device_list("/machine/peripheral/", &list);
+    qmp_set_virtio_device_list("/machine/peripheral-anon/", &list);
+    if (list == NULL) {
+        error_setg(errp, "No virtio devices found");
+        return NULL;
+    }
+    return list;
+}
+
+/* qmp_set_virtio_device_list:
+ * @ppath: An incomplete peripheral path to search from.
+ * @list: A list of realized virtio devices.
+ * Searches a given incomplete peripheral path (e.g. '/machine/peripheral/'
+ * or '/machine/peripheral-anon/') for realized virtio devices and adds them
+ * to a given list of virtio devices.
+ */
+void qmp_set_virtio_device_list(const char *ppath, VirtioInfoList **list)
+{
+    ObjectPropertyInfoList *plist;
+    VirtioInfoList *node;
+    Error *err = NULL;
+
+    /* Search an incomplete path for virtio devices */
+    plist = qmp_qom_list(ppath, &err);
+    if (err == NULL) {
+        ObjectPropertyInfoList *start = plist;
+        while (plist != NULL) {
+            ObjectPropertyInfo *value = plist->value;
+            GString *path = g_string_new(ppath);
+            g_string_append(path, value->name);
+            g_string_append(path, "/virtio-backend");
+
+            /* Determine if full path is a realized virtio device */
+            VirtIODevice *vdev = qmp_find_virtio_device(path->str);
+            if (vdev != NULL) {
                 node = g_new0(VirtioInfoList, 1);
                 node->value = g_new(VirtioInfo, 1);
-                node->value->path = g_strdup(dev->canonical_path);
+                node->value->path = g_strdup(path->str);
                 node->value->name = g_strdup(vdev->name);
-                QAPI_LIST_PREPEND(list, node->value);
+                QAPI_LIST_PREPEND(*list, node->value);
             }
-           g_string_free(is_realized, true);
+            g_string_free(path, true);
+            plist = plist->next;
         }
-        qobject_unref(obj);
+        qapi_free_ObjectPropertyInfoList(start);
     }
-
-    return list;
 }
 
 VirtIODevice *qmp_find_virtio_device(const char *path)
 {
-    VirtIODevice *vdev;
-
-    QTAILQ_FOREACH(vdev, &virtio_list, next) {
-        DeviceState *dev = DEVICE(vdev);
-
-        if (strcmp(dev->canonical_path, path) != 0) {
-            continue;
+    Error *err = NULL;
+    char *basename;
+
+    /* Append 'virtio-backend' to path if needed */
+    basename = g_path_get_basename(path);
+    if (strcmp(basename, "virtio-backend")) {
+        GString *temp = g_string_new(path);
+        char *last = strrchr(path, '/');
+        if (g_strcmp0(last, "/")) {
+            g_string_append(temp, "/virtio-backend");
+        } else {
+            g_string_append(temp, "virtio-backend");
         }
+        path = g_strdup(temp->str);
+        g_string_free(temp, true);
+    }
 
-        Error *err = NULL;
-        QObject *obj = qmp_qom_get(dev->canonical_path, "realized", &err);
-        if (err == NULL) {
-            GString *is_realized = qobject_to_json_pretty(obj, true);
-            /* virtio device is NOT realized, remove it from list */
-            if (!strncmp(is_realized->str, "false", 4)) {
-                g_string_free(is_realized, true);
-                qobject_unref(obj);
-                QTAILQ_REMOVE(&virtio_list, vdev, next);
-                return NULL;
-            }
+    /* Verify the virtio device is realized */
+    QObject *qobj = qmp_qom_get(path, "realized", &err);
+    if (err == NULL) {
+        GString *is_realized = qobject_to_json_pretty(qobj, true);
+        if (!strncmp(is_realized->str, "false", 4)) {
             g_string_free(is_realized, true);
-        } else {
-            /* virtio device doesn't exist in QOM tree */
-            QTAILQ_REMOVE(&virtio_list, vdev, next);
-            qobject_unref(obj);
+            qobject_unref(qobj);
             return NULL;
         }
-        /* device exists in QOM tree & is realized */
-        qobject_unref(obj);
-        return vdev;
+        g_string_free(is_realized, true);
+    } else {
+        qobject_unref(qobj);
+        return NULL;
+    }
+    qobject_unref(qobj);
+
+    /* Get VirtIODevice object */
+    Object *obj = object_resolve_path(path, NULL);
+    if (!obj) {
+        object_unref(obj);
+        return NULL;
     }
-    return NULL;
+    VirtIODevice *vdev = VIRTIO_DEVICE(obj);
+    return vdev;
 }
 
 VirtioStatus *qmp_x_query_virtio_status(const char *path, Error **errp)
hw/virtio/virtio-qmp.h +1 -7
index 8af5f5e65a..4b2b7875b4 100644
@@ -15,13 +15,7 @@
 #include "hw/virtio/virtio.h"
 #include "hw/virtio/vhost.h"
 
-#include "qemu/queue.h"
-
-typedef QTAILQ_HEAD(QmpVirtIODeviceList, VirtIODevice) QmpVirtIODeviceList;
-
-/* QAPI list of realized VirtIODevices */
-extern QmpVirtIODeviceList virtio_list;
-
+void qmp_set_virtio_device_list(const char *ppath, VirtioInfoList **list);
 VirtIODevice *qmp_find_virtio_device(const char *path);
 VirtioDeviceStatus *qmp_decode_status(uint8_t bitmap);
 VhostDeviceProtocols *qmp_decode_protocols(uint64_t bitmap);
hw/virtio/virtio.c +0 -6
index 272d930721..a7fa807464 100644
@@ -45,8 +45,6 @@
 #include "standard-headers/linux/virtio_mem.h"
 #include "standard-headers/linux/virtio_vsock.h"
 
-QmpVirtIODeviceList virtio_list;
-
 /*
  * Maximum size of virtio device config space
  */
@@ -3619,7 +3617,6 @@ static void virtio_device_realize(DeviceState *dev, Error **errp)
     vdev->listener.commit = virtio_memory_listener_commit;
     vdev->listener.name = "virtio";
     memory_listener_register(&vdev->listener, vdev->dma_as);
-    QTAILQ_INSERT_TAIL(&virtio_list, vdev, next);
 }
 
 static void virtio_device_unrealize(DeviceState *dev)
@@ -3634,7 +3631,6 @@ static void virtio_device_unrealize(DeviceState *dev)
         vdc->unrealize(dev);
     }
 
-    QTAILQ_REMOVE(&virtio_list, vdev, next);
     g_free(vdev->bus_name);
     vdev->bus_name = NULL;
 }
@@ -3808,8 +3804,6 @@ static void virtio_device_class_init(ObjectClass *klass, void *data)
     vdc->stop_ioeventfd = virtio_device_stop_ioeventfd_impl;
 
     vdc->legacy_features |= VIRTIO_LEGACY_FEATURES;
-
-    QTAILQ_INIT(&virtio_list);
 }
 
 bool virtio_device_ioeventfd_enabled(VirtIODevice *vdev)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/2 qmp: update virtio feature maps, vhost-user-gpio instrospection

Add new virtio transport feature to transport feature map:
- VIRTIO_F_RING_RESET

Add new vhost-user protocol feature to vhost-user protocol feature map and enumeration:
- VHOST_USER_PROTOCOL_F_STATUS

Add new virtio device features for several virtio devices to their respective feature mappings:
- virtio-blk: VIRTIO_BLK_F_SECURE_ERASE VIRTIO_BLK_F_ZONED
- virtio-net: VIRTIO_NET_F_NOTF_COAL VIRTIO_NET_F_GUEST_USO4 VIRTIO_NET_F_GUEST_USO6 VIRTIO_NET_F_HOST_USO
- virtio/vhost-user-gpio: VIRTIO_GPIO_F_IRQ VHOST_F_LOG_ALL VHOST_USER_F_PROTOCOL_FEATURES
- virtio-bt: VIRTIO_BT_F_VND_HCI
VIRTIO_BT_F_MSFT_EXT
VIRTIO_BT_F_AOSP_EXT
VIRTIO_BT_F_CONFIG_V2
- virtio-scmi: VIRTIO_SCMI_F_P2A_CHANNELS VIRTIO_SCMI_F_SHARED_MEMORY

Add support for introspection on vhost-user-gpio devices.

---
 hw/virtio/vhost-user-gpio.c |  7 ++++
 hw/virtio/virtio-qmp.c      | 81 +++++++++++++++++++++++++++++++++++--
 2 files changed, 85 insertions(+), 3 deletions(-)
hw/virtio/vhost-user-gpio.c +7 -0
index d6927b610a..e88ca5370f 100644
@@ -205,6 +205,12 @@ static void vu_gpio_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
     vhost_virtqueue_mask(&gpio->vhost_dev, vdev, idx, mask);
 }
 
+static struct vhost_dev *vu_gpio_get_vhost(VirtIODevice *vdev)
+{
+    VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+    return &gpio->vhost_dev;
+}
+
 static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserGPIO *gpio)
 {
     virtio_delete_queue(gpio->command_vq);
@@ -413,6 +419,7 @@ static void vu_gpio_class_init(ObjectClass *klass, void *data)
     vdc->get_config = vu_gpio_get_config;
     vdc->set_status = vu_gpio_set_status;
     vdc->guest_notifier_mask = vu_gpio_guest_notifier_mask;
+    vdc->get_vhost = vu_gpio_get_vhost;
 }
 
 static const TypeInfo vu_gpio_info = {
hw/virtio/virtio-qmp.c +78 -3

138 lines not on this page · raw message

Signature · 1 line
2.31.1
#2 lore raw
In reply to Jonah Palmer
Quoted 11 lines · Jonah Palmer

The virtio_list duplicates information about virtio devices that already exist in the QOM composition tree. Instead of creating this list of realized virtio devices, search the QOM composition tree instead.

This patch modifies the QMP command qmp_x_query_virtio to instead search the partial paths of '/machine/peripheral/' & '/machine/peripheral-anon/' in the QOM composition tree for virtio devices.

A device is found to be a valid virtio device if (1) it has a canonical path ending with 'virtio-backend' and (2) the device has been realized.

Checking the path suffix feels pretty undesirable to me when we could be doing a QOM class check

if (object_dynamic_cast(obj, TYPE_VIRTIO_DEVICE))

     ...

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#3 lore raw
Quoted 15 lines · Daniel P. Berrangé
Quoted 11 lines · Jonah Palmer

The virtio_list duplicates information about virtio devices that already exist in the QOM composition tree. Instead of creating this list of realized virtio devices, search the QOM composition tree instead.

This patch modifies the QMP command qmp_x_query_virtio to instead search the partial paths of '/machine/peripheral/' & '/machine/peripheral-anon/' in the QOM composition tree for virtio devices.

A device is found to be a valid virtio device if (1) it has a canonical path ending with 'virtio-backend' and (2) the device has been realized.

Checking the path suffix feels pretty undesirable to me when we could be doing a QOM class check

Quoted 6 lines

if (object_dynamic_cast(obj, TYPE_VIRTIO_DEVICE))
...

With regards,
Daniel

Ah, yes, you're right. This is a much better solution. I will do this instead of the hacky string manipulation (which also felt undesirable to me).

Jonah