qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org
Cc: "Gonglei (Arei)" <arei.gonglei@huawei.com>,
	"Paolo Bonzini" <pbonzini@redhat.com>,
	"Michael S. Tsirkin" <mst@redhat.com>,
	virtio-fs@redhat.com,
	"Erik Schilling" <erik.schilling@linaro.org>,
	"Alex Bennée" <alex.bennee@linaro.org>,
	"Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Eduardo Habkost" <eduardo@habkost.net>,
	"Stefan Hajnoczi" <stefanha@redhat.com>,
	"Eric Blake" <eblake@redhat.com>,
	"Daniel P. Berrangé" <berrange@redhat.com>,
	"Jason Wang" <jasowang@redhat.com>,
	"Viresh Kumar" <viresh.kumar@linaro.org>,
	"Mathieu Poirier" <mathieu.poirier@linaro.org>,
	"Gerd Hoffmann" <kraxel@redhat.com>,
	"Markus Armbruster" <armbru@redhat.com>
Subject: [PATCH 10/12] hw/virtio: add config support to vhost-user-device
Date: Fri, 14 Apr 2023 17:04:31 +0100	[thread overview]
Message-ID: <20230414160433.2096866-11-alex.bennee@linaro.org> (raw)
In-Reply-To: <20230414160433.2096866-1-alex.bennee@linaro.org>

To use the generic device the user will need to provide the config
region size via the command line. We also add a notifier so the guest
can be pinged if the remote daemon updates the config.

With these changes:

  -device vhost-user-device-pci,virtio-id=41,num_vqs=2,config_size=8

is equivalent to:

  -device vhost-user-gpio-pci

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 include/hw/virtio/vhost-user-device.h |  1 +
 hw/virtio/vhost-user-device.c         | 58 ++++++++++++++++++++++++++-
 2 files changed, 58 insertions(+), 1 deletion(-)

diff --git a/include/hw/virtio/vhost-user-device.h b/include/hw/virtio/vhost-user-device.h
index 8d77f06721..cb98e0dcaa 100644
--- a/include/hw/virtio/vhost-user-device.h
+++ b/include/hw/virtio/vhost-user-device.h
@@ -21,6 +21,7 @@ struct VHostUserDevice {
     CharBackend chardev;
     uint16_t virtio_id;
     uint32_t num_vqs;
+    uint32_t config_size;
     /* State tracking */
     VhostUserState vhost_user;
     struct vhost_virtqueue *vhost_vq;
diff --git a/hw/virtio/vhost-user-device.c b/hw/virtio/vhost-user-device.c
index bfbf3b29cb..977cfed247 100644
--- a/hw/virtio/vhost-user-device.c
+++ b/hw/virtio/vhost-user-device.c
@@ -117,6 +117,42 @@ static uint64_t vud_get_features(VirtIODevice *vdev,
     return vud->vhost_dev.features & ~(1ULL << VHOST_USER_F_PROTOCOL_FEATURES);
 }
 
+/*
+ * To handle VirtIO config we need to know the size of the config
+ * space. We don't cache the config but re-fetch it from the guest
+ * every time in case something has changed.
+ */
+static void vud_get_config(VirtIODevice *vdev, uint8_t *config)
+{
+    VHostUserDevice *vud = VHOST_USER_DEVICE(vdev);
+    Error *local_err = NULL;
+
+    /*
+     * There will have been a warning during vhost_dev_init, but lets
+     * assert here as nothing will go right now.
+     */
+    g_assert(vud->config_size && vud->vhost_user.supports_config == true);
+
+    if (vhost_dev_get_config(&vud->vhost_dev, config,
+                             vud->config_size, &local_err)) {
+        error_report_err(local_err);
+    }
+}
+
+/*
+ * When the daemon signals an update to the config we just need to
+ * signal the guest as we re-read the config on demand above.
+ */
+static int vud_config_notifier(struct vhost_dev *dev)
+{
+    virtio_notify_config(dev->vdev);
+    return 0;
+}
+
+const VhostDevConfigOps vud_config_ops = {
+    .vhost_dev_config_notifier = vud_config_notifier,
+};
+
 static void vud_handle_output(VirtIODevice *vdev, VirtQueue *vq)
 {
     /*
@@ -141,12 +177,21 @@ static int vud_connect(DeviceState *dev)
 {
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
     VHostUserDevice *vud = VHOST_USER_DEVICE(vdev);
+    struct vhost_dev *vhost_dev = &vud->vhost_dev;
 
     if (vud->connected) {
         return 0;
     }
     vud->connected = true;
 
+    /*
+     * If we support VHOST_USER_GET_CONFIG we must enable the notifier
+     * so we can ping the guest when it updates.
+     */
+    if (vud->vhost_user.supports_config) {
+        vhost_dev_set_config_notifier(vhost_dev, &vud_config_ops);
+    }
+
     /* restore vhost state */
     if (virtio_device_started(vdev, vdev->status)) {
         vud_start(vdev);
@@ -214,11 +259,20 @@ static void vud_device_realize(DeviceState *dev, Error **errp)
         vud->num_vqs = 1; /* reasonable default? */
     }
 
+    /*
+     * We can't handle config requests unless we know the size of the
+     * config region, specialisations of the vhost-user-device will be
+     * able to set this.
+     */
+    if (vud->config_size) {
+        vud->vhost_user.supports_config = true;
+    }
+
     if (!vhost_user_init(&vud->vhost_user, &vud->chardev, errp)) {
         return;
     }
 
-    virtio_init(vdev, vud->virtio_id, 0);
+    virtio_init(vdev, vud->virtio_id, vud->config_size);
 
     /*
      * Disable guest notifiers, by default all notifications will be via the
@@ -271,6 +325,7 @@ static Property vud_properties[] = {
     DEFINE_PROP_CHR("chardev", VHostUserDevice, chardev),
     DEFINE_PROP_UINT16("virtio-id", VHostUserDevice, virtio_id, 0),
     DEFINE_PROP_UINT32("num_vqs", VHostUserDevice, num_vqs, 1),
+    DEFINE_PROP_UINT32("config_size", VHostUserDevice, config_size, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
@@ -285,6 +340,7 @@ static void vud_class_init(ObjectClass *klass, void *data)
     vdc->realize = vud_device_realize;
     vdc->unrealize = vud_device_unrealize;
     vdc->get_features = vud_get_features;
+    vdc->get_config = vud_get_config;
     vdc->set_status = vud_set_status;
 }
 
-- 
2.39.2



  parent reply	other threads:[~2023-04-14 16:08 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-14 16:04 [PATCH 00/12] virtio: add vhost-user-generic and reduce copy and paste Alex Bennée
2023-04-14 16:04 ` [PATCH 01/12] hw/virtio: fix typo in VIRTIO_CONFIG_IRQ_IDX comments Alex Bennée
2023-04-14 16:04 ` [PATCH 02/12] include/hw/virtio: document virtio_notify_config Alex Bennée
2023-04-14 16:04 ` [PATCH 03/12] include/hw/virtio: add kerneldoc for virtio_init Alex Bennée
2023-04-14 16:04 ` [PATCH 04/12] include/hw/virtio: document some more usage of notifiers Alex Bennée
2023-04-14 16:04 ` [PATCH 05/12] virtio: add generic vhost-user-device Alex Bennée
2023-04-14 16:04 ` [PATCH 06/12] virtio: add PCI stub for vhost-user-device Alex Bennée
2023-04-18  8:28   ` Erik Schilling
2023-04-14 16:04 ` [PATCH 07/12] include: attempt to document device_class_set_props Alex Bennée
2023-04-14 16:04 ` [PATCH 08/12] qom: allow for properties to become "fixed" Alex Bennée
2023-04-17 10:02   ` Markus Armbruster
2023-04-17 11:26     ` Alex Bennée
2023-04-17 12:04       ` Peter Maydell
2023-04-14 16:04 ` [PATCH 09/12] hw/virtio: derive vhost-user-rng from vhost-user-device Alex Bennée
2023-04-14 16:04 ` Alex Bennée [this message]
2023-04-14 16:04 ` [PATCH 11/12] hw/virtio: derive vhost-user-gpio from vhost-user-device (!BROKEN) Alex Bennée
2023-04-14 16:04 ` [PATCH 12/12] docs/system: add a basic enumeration of vhost-user devices Alex Bennée
2023-04-17  4:26 ` [PATCH 00/12] virtio: add vhost-user-generic and reduce copy and paste Viresh Kumar
2023-04-17  8:42   ` Alex Bennée
2023-04-17 12:32 ` Stefan Hajnoczi
2023-04-17 16:14   ` Alex Bennée
2023-04-18 16:04     ` Stefan Hajnoczi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230414160433.2096866-11-alex.bennee@linaro.org \
    --to=alex.bennee@linaro.org \
    --cc=arei.gonglei@huawei.com \
    --cc=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=eblake@redhat.com \
    --cc=eduardo@habkost.net \
    --cc=erik.schilling@linaro.org \
    --cc=jasowang@redhat.com \
    --cc=kraxel@redhat.com \
    --cc=marcandre.lureau@redhat.com \
    --cc=mathieu.poirier@linaro.org \
    --cc=mst@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=viresh.kumar@linaro.org \
    --cc=virtio-fs@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).