From: Viresh Kumar <viresh.kumar@linaro.org>
This creates the QEMU side of the vhost-user-gpio device which connects
to the remote daemon. It is based of vhost-user-i2c code.
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
Message-Id: <5390324a748194a21bc99b1538e19761a8c64092.1641987128.git.viresh.kumar@linaro.org>
[AJB: fixes for qtest]
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
v2
- set VIRTIO_F_VERSION_1
- set VHOST_USER_F_PROTOCOL_FEATURES
- terminate feature_bits with VHOST_INVALID_FEATURE_BIT
- ensure vdev->backend_features set
- ensure vhost_dev.acked_features set
---
include/hw/virtio/vhost-user-gpio.h | 35 +++
hw/virtio/vhost-user-gpio.c | 357 ++++++++++++++++++++++++++++
hw/virtio/Kconfig | 5 +
hw/virtio/meson.build | 1 +
4 files changed, 398 insertions(+)
create mode 100644 include/hw/virtio/vhost-user-gpio.h
create mode 100644 hw/virtio/vhost-user-gpio.c
diff --git a/include/hw/virtio/vhost-user-gpio.h b/include/hw/virtio/vhost-user-gpio.h
new file mode 100644
index 0000000000..afeb56f53e
--- /dev/null
+++ b/include/hw/virtio/vhost-user-gpio.h
@@ -0,0 +1,35 @@
+/*
+ * Vhost-user GPIO virtio device
+ *
+ * Copyright (c) 2021 Viresh Kumar <viresh.kumar@linaro.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef _QEMU_VHOST_USER_GPIO_H
+#define _QEMU_VHOST_USER_GPIO_H
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/vhost.h"
+#include "hw/virtio/vhost-user.h"
+#include "standard-headers/linux/virtio_gpio.h"
+#include "chardev/char-fe.h"
+
+#define TYPE_VHOST_USER_GPIO "vhost-user-gpio-device"
+OBJECT_DECLARE_SIMPLE_TYPE(VHostUserGPIO, VHOST_USER_GPIO);
+
+struct VHostUserGPIO {
+ /*< private >*/
+ VirtIODevice parent;
+ CharBackend chardev;
+ struct virtio_gpio_config config;
+ struct vhost_virtqueue *vhost_vq;
+ struct vhost_dev vhost_dev;
+ VhostUserState vhost_user;
+ VirtQueue *command_vq;
+ VirtQueue *interrupt_vq;
+ bool connected;
+ /*< public >*/
+};
+
+#endif /* _QEMU_VHOST_USER_GPIO_H */
diff --git a/hw/virtio/vhost-user-gpio.c b/hw/virtio/vhost-user-gpio.c
new file mode 100644
index 0000000000..87e3976880
--- /dev/null
+++ b/hw/virtio/vhost-user-gpio.c
@@ -0,0 +1,357 @@
+/*
+ * Vhost-user GPIO virtio device
+ *
+ * Copyright (c) 2022 Viresh Kumar <viresh.kumar@linaro.org>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "qemu/osdep.h"
+#include "qapi/error.h"
+#include "hw/qdev-properties.h"
+#include "hw/virtio/virtio-bus.h"
+#include "hw/virtio/vhost-user-gpio.h"
+#include "qemu/error-report.h"
+#include "standard-headers/linux/virtio_ids.h"
+
+/* do no other vhost-user daemons need this? */
+#define VHOST_USER_F_PROTOCOL_FEATURES 30
+
+static const int feature_bits[] = {
+ VIRTIO_F_VERSION_1,
+ VIRTIO_GPIO_F_IRQ,
+ VHOST_USER_F_PROTOCOL_FEATURES,
+ VHOST_INVALID_FEATURE_BIT
+};
+
+static void vu_gpio_get_config(VirtIODevice *vdev, uint8_t *config)
+{
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+
+ memcpy(config, &gpio->config, sizeof(gpio->config));
+}
+
+static int vu_gpio_config_notifier(struct vhost_dev *dev)
+{
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(dev->vdev);
+
+ memcpy(dev->vdev->config, &gpio->config, sizeof(gpio->config));
+ virtio_notify_config(dev->vdev);
+
+ return 0;
+}
+
+const VhostDevConfigOps gpio_ops = {
+ .vhost_dev_config_notifier = vu_gpio_config_notifier,
+};
+
+static int vu_gpio_start(VirtIODevice *vdev)
+{
+ BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+ VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+ int ret, i;
+
+ if (!k->set_guest_notifiers) {
+ error_report("binding does not support guest notifiers");
+ return -ENOSYS;
+ }
+
+ ret = vhost_dev_enable_notifiers(&gpio->vhost_dev, vdev);
+ if (ret < 0) {
+ error_report("Error enabling host notifiers: %d", ret);
+ return ret;
+ }
+
+ ret = k->set_guest_notifiers(qbus->parent, gpio->vhost_dev.nvqs, true);
+ if (ret < 0) {
+ error_report("Error binding guest notifier: %d", ret);
+ goto err_host_notifiers;
+ }
+
+ /*
+ * Before we start up we need to ensure we have the final feature
+ * set needed for the vhost configuration.
+ */
+ vhost_ack_features(&gpio->vhost_dev, feature_bits, vdev->backend_features);
+
+ ret = vhost_dev_start(&gpio->vhost_dev, vdev);
+ if (ret < 0) {
+ error_report("Error starting vhost-user-gpio: %d", ret);
+ goto err_guest_notifiers;
+ }
+
+ /*
+ * guest_notifier_mask/pending not used yet, so just unmask
+ * everything here. virtio-pci will do the right thing by
+ * enabling/disabling irqfd.
+ */
+ for (i = 0; i < gpio->vhost_dev.nvqs; i++) {
+ vhost_virtqueue_mask(&gpio->vhost_dev, vdev, i, false);
+ }
+
+ return 0;
+
+err_guest_notifiers:
+ k->set_guest_notifiers(qbus->parent, gpio->vhost_dev.nvqs, false);
+err_host_notifiers:
+ vhost_dev_disable_notifiers(&gpio->vhost_dev, vdev);
+
+ return ret;
+}
+
+static void vu_gpio_stop(VirtIODevice *vdev)
+{
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+ BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
+ VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
+ int ret;
+
+ if (!k->set_guest_notifiers) {
+ return;
+ }
+
+ vhost_dev_stop(&gpio->vhost_dev, vdev);
+
+ ret = k->set_guest_notifiers(qbus->parent, gpio->vhost_dev.nvqs, false);
+ if (ret < 0) {
+ error_report("vhost guest notifier cleanup failed: %d", ret);
+ return;
+ }
+
+ vhost_dev_disable_notifiers(&gpio->vhost_dev, vdev);
+}
+
+static void vu_gpio_set_status(VirtIODevice *vdev, uint8_t status)
+{
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+ bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
+
+ if (!vdev->vm_running) {
+ should_start = false;
+ }
+
+ if (!gpio->connected) {
+ return;
+ }
+
+ if (gpio->vhost_dev.started == should_start) {
+ return;
+ }
+
+ if (should_start) {
+ if (vu_gpio_start(vdev)) {
+ qemu_chr_fe_disconnect(&gpio->chardev);
+ }
+ } else {
+ vu_gpio_stop(vdev);
+ }
+}
+
+static uint64_t vu_gpio_get_features(VirtIODevice *vdev, uint64_t features,
+ Error **errp)
+{
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+
+ virtio_add_feature(&features, VIRTIO_GPIO_F_IRQ);
+ virtio_add_feature(&features, VIRTIO_F_VERSION_1);
+
+ vdev->backend_features = vhost_get_features(&gpio->vhost_dev, feature_bits,
+ features);
+ return vdev->backend_features;
+}
+
+static void vu_gpio_handle_output(VirtIODevice *vdev, VirtQueue *vq)
+{
+ /*
+ * Not normally called; it's the daemon that handles the queue;
+ * however virtio's cleanup path can call this.
+ */
+}
+
+static void vu_gpio_guest_notifier_mask(VirtIODevice *vdev, int idx, bool mask)
+{
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+
+ vhost_virtqueue_mask(&gpio->vhost_dev, vdev, idx, mask);
+}
+
+static void do_vhost_user_cleanup(VirtIODevice *vdev, VHostUserGPIO *gpio)
+{
+ virtio_delete_queue(gpio->command_vq);
+ virtio_delete_queue(gpio->interrupt_vq);
+ g_free(gpio->vhost_dev.vqs);
+ gpio->vhost_dev.vqs = NULL;
+ virtio_cleanup(vdev);
+ vhost_user_cleanup(&gpio->vhost_user);
+}
+
+static int vu_gpio_connect(DeviceState *dev)
+{
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+ Error *local_err = NULL;
+ int ret;
+
+ if (gpio->connected) {
+ return 0;
+ }
+ gpio->connected = true;
+
+ vhost_dev_set_config_notifier(&gpio->vhost_dev, &gpio_ops);
+
+ ret = vhost_dev_init(&gpio->vhost_dev, &gpio->vhost_user,
+ VHOST_BACKEND_TYPE_USER, 0, &local_err);
+ if (ret < 0) {
+ error_report("vhost-user-gpio: vhost initialization failed: %s",
+ strerror(-ret));
+ return ret;
+ }
+
+ /* restore vhost state */
+ if (virtio_device_started(vdev, vdev->status)) {
+ vu_gpio_start(vdev);
+ }
+
+ return 0;
+}
+
+static void vu_gpio_disconnect(DeviceState *dev)
+{
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+
+ if (!gpio->connected) {
+ return;
+ }
+ gpio->connected = false;
+
+ vu_gpio_stop(vdev);
+ vhost_dev_cleanup(&gpio->vhost_dev);
+}
+
+static void vu_gpio_event(void *opaque, QEMUChrEvent event)
+{
+ DeviceState *dev = opaque;
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev);
+
+ switch (event) {
+ case CHR_EVENT_OPENED:
+ if (vu_gpio_connect(dev) < 0) {
+ qemu_chr_fe_disconnect(&gpio->chardev);
+ return;
+ }
+ break;
+ case CHR_EVENT_CLOSED:
+ vu_gpio_disconnect(dev);
+ break;
+ case CHR_EVENT_BREAK:
+ case CHR_EVENT_MUX_IN:
+ case CHR_EVENT_MUX_OUT:
+ /* Ignore */
+ break;
+ }
+}
+
+static void vu_gpio_device_realize(DeviceState *dev, Error **errp)
+{
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(dev);
+ Error *err = NULL;
+ int ret;
+
+ if (!gpio->chardev.chr) {
+ error_setg(errp, "vhost-user-gpio: chardev is mandatory");
+ return;
+ }
+
+ if (!vhost_user_init(&gpio->vhost_user, &gpio->chardev, errp)) {
+ return;
+ }
+
+ virtio_init(vdev, VIRTIO_ID_GPIO, sizeof(gpio->config));
+
+ gpio->vhost_dev.nvqs = 2;
+ gpio->command_vq = virtio_add_queue(vdev, 256, vu_gpio_handle_output);
+ gpio->interrupt_vq = virtio_add_queue(vdev, 256, vu_gpio_handle_output);
+ gpio->vhost_dev.vqs = g_new0(struct vhost_virtqueue, gpio->vhost_dev.nvqs);
+
+ gpio->connected = false;
+
+ qemu_chr_fe_set_handlers(&gpio->chardev, NULL, NULL, vu_gpio_event, NULL,
+ dev, NULL, true);
+
+reconnect:
+ if (qemu_chr_fe_wait_connected(&gpio->chardev, &err) < 0) {
+ error_report_err(err);
+ do_vhost_user_cleanup(vdev, gpio);
+ return;
+ }
+
+ /* check whether vhost_user_gpio_connect() failed or not */
+ if (!gpio->connected) {
+ goto reconnect;
+ }
+
+ ret = vhost_dev_get_config(&gpio->vhost_dev, (uint8_t *)&gpio->config,
+ sizeof(gpio->config), errp);
+ if (ret < 0) {
+ error_report("vhost-user-gpio: get config failed");
+ goto reconnect;
+ }
+
+ return;
+}
+
+static void vu_gpio_device_unrealize(DeviceState *dev)
+{
+ VirtIODevice *vdev = VIRTIO_DEVICE(dev);
+ VHostUserGPIO *gpio = VHOST_USER_GPIO(dev);
+
+ vu_gpio_set_status(vdev, 0);
+ qemu_chr_fe_set_handlers(&gpio->chardev, NULL, NULL, NULL, NULL, NULL, NULL,
+ false);
+ vhost_dev_cleanup(&gpio->vhost_dev);
+ do_vhost_user_cleanup(vdev, gpio);
+}
+
+static const VMStateDescription vu_gpio_vmstate = {
+ .name = "vhost-user-gpio",
+ .unmigratable = 1,
+};
+
+static Property vu_gpio_properties[] = {
+ DEFINE_PROP_CHR("chardev", VHostUserGPIO, chardev),
+ DEFINE_PROP_END_OF_LIST(),
+};
+
+static void vu_gpio_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+ VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
+
+ device_class_set_props(dc, vu_gpio_properties);
+ dc->vmsd = &vu_gpio_vmstate;
+ set_bit(DEVICE_CATEGORY_INPUT, dc->categories);
+ vdc->realize = vu_gpio_device_realize;
+ vdc->unrealize = vu_gpio_device_unrealize;
+ vdc->get_features = vu_gpio_get_features;
+ vdc->get_config = vu_gpio_get_config;
+ vdc->set_status = vu_gpio_set_status;
+ vdc->guest_notifier_mask = vu_gpio_guest_notifier_mask;
+}
+
+static const TypeInfo vu_gpio_info = {
+ .name = TYPE_VHOST_USER_GPIO,
+ .parent = TYPE_VIRTIO_DEVICE,
+ .instance_size = sizeof(VHostUserGPIO),
+ .class_init = vu_gpio_class_init,
+};
+
+static void vu_gpio_register_types(void)
+{
+ type_register_static(&vu_gpio_info);
+}
+
+type_init(vu_gpio_register_types)
diff --git a/hw/virtio/Kconfig b/hw/virtio/Kconfig
index e9ecae1f50..cbfd8c7173 100644
--- a/hw/virtio/Kconfig
+++ b/hw/virtio/Kconfig
@@ -80,3 +80,8 @@ config VHOST_USER_FS
bool
default y
depends on VIRTIO && VHOST_USER
+
+config VHOST_USER_GPIO
+ bool
+ default y
+ depends on VIRTIO && VHOST_USER
diff --git a/hw/virtio/meson.build b/hw/virtio/meson.build
index 7e8877fd64..33c8e71fab 100644
--- a/hw/virtio/meson.build
+++ b/hw/virtio/meson.build
@@ -29,6 +29,7 @@ virtio_ss.add(when: 'CONFIG_VIRTIO_IOMMU', if_true: files('virtio-iommu.c'))
virtio_ss.add(when: 'CONFIG_VIRTIO_MEM', if_true: files('virtio-mem.c'))
virtio_ss.add(when: 'CONFIG_VHOST_USER_I2C', if_true: files('vhost-user-i2c.c'))
virtio_ss.add(when: 'CONFIG_VHOST_USER_RNG', if_true: files('vhost-user-rng.c'))
+virtio_ss.add(when: 'CONFIG_VHOST_USER_GPIO', if_true: files('vhost-user-gpio.c'))
virtio_pci_ss = ss.source_set()
virtio_pci_ss.add(when: 'CONFIG_VHOST_VSOCK', if_true: files('vhost-vsock-pci.c'))
--
2.30.2
On Tue, May 24, 2022 at 04:40:46PM +0100, Alex Bennée wrote: > +static int vu_gpio_start(VirtIODevice *vdev) > +{ > + BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); > + VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); > + VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); > + int ret, i; > + > + if (!k->set_guest_notifiers) { > + error_report("binding does not support guest notifiers"); > + return -ENOSYS; > + } > + > + ret = vhost_dev_enable_notifiers(&gpio->vhost_dev, vdev); > + if (ret < 0) { > + error_report("Error enabling host notifiers: %d", ret); > + return ret; > + } > + > + ret = k->set_guest_notifiers(qbus->parent, gpio->vhost_dev.nvqs, true); > + if (ret < 0) { > + error_report("Error binding guest notifier: %d", ret); > + goto err_host_notifiers; > + } > + > + /* > + * Before we start up we need to ensure we have the final feature > + * set needed for the vhost configuration. > + */ > + vhost_ack_features(&gpio->vhost_dev, feature_bits, vdev->backend_features); This is doing the feature handling differently from the other vhost-user-* implementations, and it doesn't seem to work for me. Negotiated features (I noticed it with VIRTIO_RING_F_EVENT_IDX) never make it to VHOST_USER_SET_FEATURES. If I change this code to match vhost-user-i2c and the other implementations like in the patch below, it works. The guest is Linux v5.18. The backend uses libvhost-user and is the one posted here (with a few fixes): https://lore.kernel.org/lkml/20220311162445.346685-3-vincent.whitchurch@axis.com/ 8<------- diff --git a/hw/virtio/vhost-user-gpio.c b/hw/virtio/vhost-user-gpio.c index 87e3976880..1dc7af6b03 100644 --- a/hw/virtio/vhost-user-gpio.c +++ b/hw/virtio/vhost-user-gpio.c @@ -73,7 +73,7 @@ static int vu_gpio_start(VirtIODevice *vdev) * Before we start up we need to ensure we have the final feature * set needed for the vhost configuration. */ - vhost_ack_features(&gpio->vhost_dev, feature_bits, vdev->backend_features); + gpio->vhost_dev.acked_features = vdev->guest_features; ret = vhost_dev_start(&gpio->vhost_dev, vdev); if (ret < 0) { @@ -156,9 +156,7 @@ static uint64_t vu_gpio_get_features(VirtIODevice *vdev, uint64_t features, virtio_add_feature(&features, VIRTIO_GPIO_F_IRQ); virtio_add_feature(&features, VIRTIO_F_VERSION_1); - vdev->backend_features = vhost_get_features(&gpio->vhost_dev, feature_bits, - features); - return vdev->backend_features; + return vhost_get_features(&gpio->vhost_dev, feature_bits, features); } static void vu_gpio_handle_output(VirtIODevice *vdev, VirtQueue *vq) -- 2.28.0
Vincent Whitchurch <vincent.whitchurch@axis.com> writes: > On Tue, May 24, 2022 at 04:40:46PM +0100, Alex Bennée wrote: >> +static int vu_gpio_start(VirtIODevice *vdev) >> +{ >> + BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); >> + VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); >> + VHostUserGPIO *gpio = VHOST_USER_GPIO(vdev); >> + int ret, i; >> + >> + if (!k->set_guest_notifiers) { >> + error_report("binding does not support guest notifiers"); >> + return -ENOSYS; >> + } >> + >> + ret = vhost_dev_enable_notifiers(&gpio->vhost_dev, vdev); >> + if (ret < 0) { >> + error_report("Error enabling host notifiers: %d", ret); >> + return ret; >> + } >> + >> + ret = k->set_guest_notifiers(qbus->parent, gpio->vhost_dev.nvqs, true); >> + if (ret < 0) { >> + error_report("Error binding guest notifier: %d", ret); >> + goto err_host_notifiers; >> + } >> + >> + /* >> + * Before we start up we need to ensure we have the final feature >> + * set needed for the vhost configuration. >> + */ >> + vhost_ack_features(&gpio->vhost_dev, feature_bits, vdev->backend_features); > > This is doing the feature handling differently from the other > vhost-user-* implementations, and it doesn't seem to work for me. > Negotiated features (I noticed it with VIRTIO_RING_F_EVENT_IDX) never > make it to VHOST_USER_SET_FEATURES. > > If I change this code to match vhost-user-i2c and the other > implementations like in the patch below, it works. Unfortunately the virtio-i2c backend doesn't need VHOST_USER_F_PROTOCOL_FEATURES which gets squashed with the bellow changes which is the cause of the eventual failure in the qos-test: # Start of read-guest-mem tests vu_net_set_features: 0 ** ERROR:../../tests/qtest/vhost-user-test.c:1031:vu_net_set_features: assertion failed: (msg->payload.u64 & (0x1ULL << VHOST_USER_F_PROTOCOL_FEATURES)) which adds to my confusion about the exact route the negotiation of vhost-user feature bits is meant to make through the code. > > The guest is Linux v5.18. The backend uses libvhost-user and is the one > posted here (with a few fixes): > > https://lore.kernel.org/lkml/20220311162445.346685-3-vincent.whitchurch@axis.com/ > > 8<------- > diff --git a/hw/virtio/vhost-user-gpio.c b/hw/virtio/vhost-user-gpio.c > index 87e3976880..1dc7af6b03 100644 > --- a/hw/virtio/vhost-user-gpio.c > +++ b/hw/virtio/vhost-user-gpio.c > @@ -73,7 +73,7 @@ static int vu_gpio_start(VirtIODevice *vdev) > * Before we start up we need to ensure we have the final feature > * set needed for the vhost configuration. > */ > - vhost_ack_features(&gpio->vhost_dev, feature_bits, vdev->backend_features); > + gpio->vhost_dev.acked_features = vdev->guest_features; > > ret = vhost_dev_start(&gpio->vhost_dev, vdev); > if (ret < 0) { > @@ -156,9 +156,7 @@ static uint64_t vu_gpio_get_features(VirtIODevice *vdev, uint64_t features, > virtio_add_feature(&features, VIRTIO_GPIO_F_IRQ); > virtio_add_feature(&features, VIRTIO_F_VERSION_1); > > - vdev->backend_features = vhost_get_features(&gpio->vhost_dev, feature_bits, > - features); > - return vdev->backend_features; > + return vhost_get_features(&gpio->vhost_dev, feature_bits, features); > } > > static void vu_gpio_handle_output(VirtIODevice *vdev, VirtQueue *vq) -- Alex Bennée
© 2016 - 2025 Red Hat, Inc.