Add vhost-user-input
v2 cover Add vhost-user-input
Hi,
This is the vhost-user-input part of "[PATCH v6 00/11] vhost-user for input & GPU".
v2:
- build fixes
v1: (changes since original v6 series)
- add "libvhost-user: fix -Waddress-of-packed-member" & "util: simplify unix_listen()"
- use unix_listen()
- build vhost-user-input by default (when applicable)
Marc-André Lureau (6):
libvhost-user: fix -Waddress-of-packed-member
libvhost-user: add PROTOCOL_F_CONFIG if {set,get}_config Add vhost-user-backend
Add vhost-user-input-pci
util: simplify unix_listen()
contrib: add vhost-user-input
include/hw/virtio/virtio-input.h | 14 + include/sysemu/vhost-user-backend.h | 57 ++++ backends/vhost-user.c | 209 +++++++++++++ contrib/libvhost-user/libvhost-user.c | 10 +- contrib/vhost-user-input/main.c | 393 +++++++++++++++++++++++++ hw/input/vhost-user-input.c | 129 ++++++++ hw/virtio/vhost-user-input-pci.c | 53 ++++ util/qemu-sockets.c | 18 +- MAINTAINERS | 4 + Makefile | 11 + Makefile.objs | 1 + backends/Makefile.objs | 2 + contrib/vhost-user-input/Makefile.objs | 1 + hw/input/Kconfig | 5 + hw/input/Makefile.objs | 1 + hw/virtio/Makefile.objs | 1 + 16 files changed, 890 insertions(+), 19 deletions(-) create mode 100644 include/sysemu/vhost-user-backend.h create mode 100644 backends/vhost-user.c create mode 100644 contrib/vhost-user-input/main.c create mode 100644 hw/input/vhost-user-input.c create mode 100644 hw/virtio/vhost-user-input-pci.c create mode 100644 contrib/vhost-user-input/Makefile.objs
Signature · 1 line
2.21.0.777.g83232e3864
PATCH v2 1/6 libvhost-user: fix -Waddress-of-packed-member
/home/elmarco/src/qemu/contrib/libvhost-user/libvhost-user.c: In function ‘vu_set_mem_table_exec_postcopy’:/home/elmarco/src/qemu/contrib/libvhost-user/libvhost-user.c:546:31: warning: taking address of packed member of ‘struct VhostUserMsg’ may result in an unaligned pointer value [-Waddress-of-packed-member]
546 | VhostUserMemory *memory = &vmsg->payload.memory;
| ^~~~~~~~~~~~~~~~~~~~~
/home/elmarco/src/qemu/contrib/libvhost-user/libvhost-user.c: In function ‘vu_set_mem_table_exec’:
/home/elmarco/src/qemu/contrib/libvhost-user/libvhost-user.c:688:31: warning: taking address of packed member of ‘struct VhostUserMsg’ may result in an unaligned pointer value [-Waddress-of-packed-member]
688 | VhostUserMemory *memory = &vmsg->payload.memory;
| ^~~~~~~~~~~~~~~~~~~~~
/home/elmarco/src/qemu/contrib/libvhost-user/libvhost-user.c: In function ‘vu_set_vring_addr_exec’:
/home/elmarco/src/qemu/contrib/libvhost-user/libvhost-user.c:817:36: warning: taking address of packed member of ‘struct VhostUserMsg’ may result in an unaligned pointer value [-Waddress-of-packed-member]
817 | struct vhost_vring_addr *vra = &vmsg->payload.addr;
| ^~~~~~~~~~~~~~~~~~~--- contrib/libvhost-user/libvhost-user.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
contrib/libvhost-user/libvhost-user.c +3 -3
@@ -542,7 +542,7 @@ static bool vu_set_mem_table_exec_postcopy(VuDev *dev, VhostUserMsg *vmsg) {int i; -VhostUserMemory *memory = &vmsg->payload.memory; +VhostUserMemory m = vmsg->payload.memory, *memory = &m; dev->nregions = memory->nregions;DPRINT ("Nregions: %d\n" , memory->nregions);
@@ -684,7 +684,7 @@ static bool vu_set_mem_table_exec(VuDev *dev, VhostUserMsg *vmsg) {int i; -VhostUserMemory *memory = &vmsg->payload.memory; +VhostUserMemory m = vmsg->payload.memory, *memory = &m;for (i =0 ; i < dev->nregions; i++) {VuDevRegion *r = &dev->regions[i];
@@ -813,7 +813,7 @@ vu_set_vring_num_exec(VuDev *dev, VhostUserMsg *vmsg)static bool vu_set_vring_addr_exec (VuDev *dev,VhostUserMsg *vmsg) { -struct vhost_vring_addr *vra = &vmsg->payload.addr; +struct vhost_vring_addr addr = vmsg->payload.addr, *vra = &addr;unsigned int index = vra->index;VuVirtq *vq = &dev->vq[index];
Signature · 1 line
2.21.0.777.g83232e3864
PATCH v2 2/6 libvhost-user: add PROTOCOL_F_CONFIG if {set, get}_config
In reply to Marc-André LureauAdd the config protocol feature bit if the set_config & get_config callbacks are implemented.
--- contrib/libvhost-user/libvhost-user.c | 4 ++++ 1 file changed, 4 insertions(+)
contrib/libvhost-user/libvhost-user.c +4 -0
@@ -1157,6 +1157,10 @@ vu_get_protocol_features_exec(VuDev *dev, VhostUserMsg *vmsg) features |=1ULL <<VHOST_USER_PROTOCOL_F_PAGEFAULT ; } +if (dev->iface->get_config && dev->iface->set_config) { + features |=1ULL <<VHOST_USER_PROTOCOL_F_CONFIG ; + } +if (dev->iface->get_protocol_features) { features |= dev->iface->get_protocol_features (dev); }
Signature · 1 line
2.21.0.777.g83232e3864
PATCH v2 3/6 Add vhost-user-backend
In reply to Marc-André LureauCreate a vhost-user-backend object that holds a connection to a vhost-user backend (or "slave" process) and can be referenced from virtio devices that support it. See later patches for input & gpu usage.
Note: a previous iteration of this object made it user-creatable, and allowed managed sub-process spawning, but that has been dropped for now.
--- include/sysemu/vhost-user-backend.h | 57 ++++++++ backends/vhost-user.c | 209 ++++++++++++++++++++++++++++ MAINTAINERS | 2 + backends/Makefile.objs | 2 + 4 files changed, 270 insertions(+) create mode 100644 include/sysemu/vhost-user-backend.h create mode 100644 backends/vhost-user.c
include/sysemu/vhost-user-backend.h new file +57 -0
@@ -0,0 +1,57 @@ +/* +* QEMU vhost-user backend +* +* Copyright (C) 2018 Red Hat Inc +* +* Authors: +* Marc-André Lureau <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +*/ +#ifndef QEMU_VHOST_USER_BACKEND_H +#define QEMU_VHOST_USER_BACKEND_H + +#include "qom/object.h" +#include "exec/memory.h" +#include "qemu/option.h" +#include "qemu/bitmap.h" +#include "hw/virtio/vhost.h" +#include "hw/virtio/vhost-user.h" +#include "chardev/char-fe.h" +#include "io/channel.h" + +#define TYPE_VHOST_USER_BACKEND "vhost-user-backend" +#define VHOST_USER_BACKEND (obj) \ + OBJECT_CHECK(VhostUserBackend, (obj), TYPE_VHOST_USER_BACKEND) +#define VHOST_USER_BACKEND_GET_CLASS (obj) \ + OBJECT_GET_CLASS(VhostUserBackendClass, (obj), TYPE_VHOST_USER_BACKEND) +#define VHOST_USER_BACKEND_CLASS (klass) \ + OBJECT_CLASS_CHECK(VhostUserBackendClass, (klass), TYPE_VHOST_USER_BACKEND) + +typedef struct VhostUserBackend VhostUserBackend ; +typedef struct VhostUserBackendClass VhostUserBackendClass ; + +struct VhostUserBackendClass { +ObjectClass parent_class; +}; + +struct VhostUserBackend { +/* private */ +Object parent; + +char *chr_name; +CharBackend chr; +VhostUserState vhost_user; +struct vhost_dev dev; +VirtIODevice *vdev; +bool started; +bool completed; +}; + +int vhost_user_backend_dev_init (VhostUserBackend *b,VirtIODevice *vdev, +unsigned nvqs,Error **errp); +void vhost_user_backend_start (VhostUserBackend *b); +void vhost_user_backend_stop (VhostUserBackend *b); + +#endif
backends/vhost-user.c new file +209 -0
209 lines not on this page · raw message
MAINTAINERS +2 -0
@@ -1478,6 +1478,8 @@ F: hw/*/*vhost* F: docs/interop/vhost-user.json F: docs/interop/vhost-user.txt F: contrib/vhost-user-*/ +F: backends/vhost-user.c +F: include/sysemu/vhost-user-backend.h virtio M: Michael S. Tsirkin <[email protected]>
backends/Makefile.objs +2 -0
@@ -14,4 +14,6 @@ common-obj-y += cryptodev-vhost.o common-obj-$(CONFIG_VHOST_CRYPTO) += cryptodev-vhost-user.o endif +common-obj-$(call land,$(CONFIG_VHOST_USER),$(CONFIG_VIRTIO)) += vhost-user.o + common-obj-$(CONFIG_LINUX) += hostmem-memfd.o
Signature · 1 line
2.21.0.777.g83232e3864
PATCH v2 4/6 Add vhost-user-input-pci
In reply to Marc-André LureauAdd a new virtio-input device, which connects to a vhost-user backend.
Instead of reading configuration directly from an input device / evdev (like virtio-input-host), it reads it over vhost-user protocol with {SET,GET}_CONFIG messages. The vhost-user-backend handles the queues & events setup.
--- include/hw/virtio/virtio-input.h | 14 ++++ hw/input/vhost-user-input.c | 129 +++++++++++++++++++++++++++++++ hw/virtio/vhost-user-input-pci.c | 53 +++++++++++++ MAINTAINERS | 1 + hw/input/Kconfig | 5 ++ hw/input/Makefile.objs | 1 + hw/virtio/Makefile.objs | 1 + 7 files changed, 204 insertions(+) create mode 100644 hw/input/vhost-user-input.c create mode 100644 hw/virtio/vhost-user-input-pci.c
include/hw/virtio/virtio-input.h +14 -0
@@ -2,6 +2,7 @@#define QEMU_VIRTIO_INPUT_H #include "ui/input.h" +#include "sysemu/vhost-user-backend.h" /* ----------------------------------------------------------------- */ /* virtio input protocol */
@@ -42,11 +43,18 @@ typedef struct virtio_input_event virtio_input_event;#define VIRTIO_INPUT_HOST_GET_PARENT_CLASS (obj) \ OBJECT_GET_PARENT_CLASS(obj, TYPE_VIRTIO_INPUT_HOST) +#define TYPE_VHOST_USER_INPUT "vhost-user-input" +#define VHOST_USER_INPUT (obj) \ + OBJECT_CHECK(VHostUserInput, (obj), TYPE_VHOST_USER_INPUT) +#define VHOST_USER_INPUT_GET_PARENT_CLASS (obj) \ + OBJECT_GET_PARENT_CLASS(obj, TYPE_VHOST_USER_INPUT) +typedef struct VirtIOInput VirtIOInput ;typedef struct VirtIOInputClass VirtIOInputClass ;typedef struct VirtIOInputConfig VirtIOInputConfig ;typedef struct VirtIOInputHID VirtIOInputHID ;typedef struct VirtIOInputHost VirtIOInputHost ; +typedef struct VHostUserInput VHostUserInput ;struct VirtIOInputConfig {virtio_input_config config;
@@ -98,6 +106,12 @@ struct VirtIOInputHost {int fd; }; +struct VHostUserInput { +VirtIOInput parent_obj; + +VhostUserBackend *vhost; +}; +void virtio_input_send (VirtIOInput *vinput,virtio_input_event *event);void virtio_input_init_config (VirtIOInput *vinput,virtio_input_config *config);
hw/input/vhost-user-input.c new file +129 -0
129 lines not on this page · raw message
hw/virtio/vhost-user-input-pci.c new file +53 -0
53 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -1521,6 +1521,7 @@ L: [email protected] virtio-input M: Gerd Hoffmann <[email protected]> S: Maintained +F: hw/input/vhost-user-input.c F: hw/input/virtio-input*.c F: include/hw/virtio/virtio-input.h
hw/input/Kconfig +5 -0
@@ -29,5 +29,10 @@ config VIRTIO_INPUT_HOST default y depends on VIRTIO && LINUX +config VHOST_USER_INPUT + bool + default y + depends on VIRTIO_INPUT && VHOST_USER + config TSC210X bool
hw/input/Makefile.objs +1 -0
@@ -11,6 +11,7 @@ common-obj-$(CONFIG_VIRTIO_INPUT) += virtio-input.o common-obj-$(CONFIG_VIRTIO_INPUT) += virtio-input-hid.o ifeq ($(CONFIG_LINUX),y) common-obj-$(CONFIG_VIRTIO_INPUT) += virtio-input-host.o +common-obj-$(CONFIG_VHOST_USER_INPUT) += vhost-user-input.o endif obj-$(CONFIG_MILKYMIST) += milkymist-softusb.o
hw/virtio/Makefile.objs +1 -0
@@ -17,6 +17,7 @@ obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock.o ifeq ($(CONFIG_VIRTIO_PCI),y) obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-pci.o obj-$(CONFIG_VHOST_USER_BLK) += vhost-user-blk-pci.o +obj-$(CONFIG_VHOST_USER_INPUT) += vhost-user-input-pci.o obj-$(CONFIG_VHOST_USER_SCSI) += vhost-user-scsi-pci.o obj-$(CONFIG_VHOST_SCSI) += vhost-scsi-pci.o obj-$(CONFIG_VIRTIO_INPUT_HOST) += virtio-input-host-pci.o
Signature · 1 line
2.21.0.777.g83232e3864
Re: [Qemu-devel] [PATCH v2 4/6] Add vhost-user-input-pci
Hi,
Review
Quoted 5 lines
(quoted diff) +5
-0+static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { + .base_name = TYPE_VHOST_USER_INPUT_PCI, + .generic_name = "vhost-user-input-pci", + .transitional_name = "vhost-user-input-pci-transitional", + .non_transitional_name = "vhost-user-input-pci-non-transitional",
virtio-input is virtio 1.0 only, so we don't need these variants. Incremental fix below, if you ack that I can squash it for the pull req. Or you send a v3 (in case you have other changes pending).
cheers,
Gerd
hw/virtio/vhost-user-input-pci.c +2 -4
@@ -13,7 +13,7 @@typedef struct VHostUserInputPCI VHostUserInputPCI ; -#define TYPE_VHOST_USER_INPUT_PCI "vhost-user-input-pci-base" +#define TYPE_VHOST_USER_INPUT_PCI "vhost-user-input-pci"#define VHOST_USER_INPUT_PCI (obj) \ OBJECT_CHECK(VHostUserInputPCI, (obj), TYPE_VHOST_USER_INPUT_PCI)
@@ -37,9 +37,7 @@ static void vhost_user_input_pci_instance_init(Object *obj)static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { .base_name =TYPE_VHOST_USER_INPUT_PCI , - .generic_name ="vhost-user-input-pci" , - .transitional_name ="vhost-user-input-pci-transitional" , - .non_transitional_name ="vhost-user-input-pci-non-transitional" , + .generic_name =TYPE_VHOST_USER_INPUT_PCI , .parent =TYPE_VIRTIO_INPUT_PCI , .instance_size =sizeof (VHostUserInputPCI), .instance_init = vhost_user_input_pci_instance_init,
Re: [Qemu-devel] [PATCH v2 4/6] Add vhost-user-input-pci
Hi
Quoted 9 lines · Gerd Hoffmann
Hi,
Review
Quoted 5 lines
(quoted diff) +5
-0+static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { + .base_name = TYPE_VHOST_USER_INPUT_PCI, + .generic_name = "vhost-user-input-pci", + .transitional_name = "vhost-user-input-pci-transitional", + .non_transitional_name = "vhost-user-input-pci-non-transitional",
Quoted 6 lines
virtio-input is virtio 1.0 only, so we don't need these variants. Incremental fix below, if you ack that I can squash it for the pull req. Or you send a v3 (in case you have other changes pending).
cheers,
Gerd
ack, thanks
Quoted 23 lines
hw/virtio/vhost-user-input-pci.c +2
-4@@ -13,7 +13,7 @@typedef struct VHostUserInputPCI VHostUserInputPCI ; -#define TYPE_VHOST_USER_INPUT_PCI "vhost-user-input-pci-base" +#define TYPE_VHOST_USER_INPUT_PCI "vhost-user-input-pci"#define VHOST_USER_INPUT_PCI (obj) \ OBJECT_CHECK(VHostUserInputPCI, (obj), TYPE_VHOST_USER_INPUT_PCI)@@ -37,9 +37,7 @@ static void vhost_user_input_pci_instance_init(Object *obj)static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { .base_name =TYPE_VHOST_USER_INPUT_PCI , - .generic_name ="vhost-user-input-pci" , - .transitional_name ="vhost-user-input-pci-transitional" , - .non_transitional_name ="vhost-user-input-pci-non-transitional" , + .generic_name =TYPE_VHOST_USER_INPUT_PCI , .parent =TYPE_VIRTIO_INPUT_PCI , .instance_size =sizeof (VHostUserInputPCI), .instance_init = vhost_user_input_pci_instance_init,
Re: [Qemu-devel] [PATCH v2 4/6] Add vhost-user-input-pci
Quoted 12 lines · Marc-André Lureau
Hi
Quoted 9 lines · Gerd Hoffmann
Hi,
Quoted 5 lines
(quoted diff) +5
-0+static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { + .base_name = TYPE_VHOST_USER_INPUT_PCI, + .generic_name = "vhost-user-input-pci", + .transitional_name = "vhost-user-input-pci-transitional", + .non_transitional_name = "vhost-user-input-pci-non-transitional",
Quoted 8 lines
Quoted 6 lines
virtio-input is virtio 1.0 only, so we don't need these variants. Incremental fix below, if you ack that I can squash it for the pull req. Or you send a v3 (in case you have other changes pending).
cheers,
Gerdack, thanks
Spoke too soon, patch failed tests.
New version (with some virtio-input-host fixes on top) pushed to:
https://git.kraxel.org/cgit/qemu/log/?h=testing/vhost-input
Can you please verify?
thanks,
Gerd
Re: [Qemu-devel] [PATCH v2 4/6] Add vhost-user-input-pci
Hi
Quoted 21 lines · Gerd Hoffmann
Quoted 19 lines · Marc-André Lureau
Hi
Quoted 15 lines · Gerd Hoffmann
Hi,
Review
Quoted 5 lines
(quoted diff) +5
-0+static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { + .base_name = TYPE_VHOST_USER_INPUT_PCI, + .generic_name = "vhost-user-input-pci", + .transitional_name = "vhost-user-input-pci-transitional", + .non_transitional_name = "vhost-user-input-pci-non-transitional",virtio-input is virtio 1.0 only, so we don't need these variants. Incremental fix below, if you ack that I can squash it for the pull req. Or you send a v3 (in case you have other changes pending).
cheers,
Gerd
Quoted 8 lines
Quoted 1 line
ack, thanks
Spoke too soon, patch failed tests.
New version (with some virtio-input-host fixes on top) pushed to:https://git.kraxel.org/cgit/qemu/log/?h=testing/vhost-input
Can you please verify?
Looks good, tested successfully.
thanks
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH v2 4/6] Add vhost-user-input-pci
Hi
Quoted 30 lines · Marc-André Lureau
Hi
Quoted 26 lines · Gerd Hoffmann
Quoted 12 lines · Marc-André Lureau
Hi
Quoted 9 lines · Gerd Hoffmann
Hi,
Quoted 5 lines
(quoted diff) +5
-0+static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { + .base_name = TYPE_VHOST_USER_INPUT_PCI, + .generic_name = "vhost-user-input-pci", + .transitional_name = "vhost-user-input-pci-transitional", + .non_transitional_name = "vhost-user-input-pci-non-transitional",Quoted 8 lines
Quoted 6 lines
virtio-input is virtio 1.0 only, so we don't need these variants. Incremental fix below, if you ack that I can squash it for the pull req. Or you send a v3 (in case you have other changes pending).
cheers,
Gerdack, thanks
Spoke too soon, patch failed tests.
New version (with some virtio-input-host fixes on top) pushed to:
Quoted 7 lines
Quoted 3 lines
https://git.kraxel.org/cgit/qemu/log/?h=testing/vhost-input
Can you please verify?
Looks good, tested successfully.
thanks
nack, I tested wrongly, investigating some issue
Signature · 1 line
Marc-André Lureau
Re: [Qemu-devel] [PATCH v2 4/6] Add vhost-user-input-pci
Hi
Quoted 37 lines · Marc-André Lureau
Hi
Quoted 32 lines · Marc-André Lureau
Hi
Quoted 28 lines · Gerd Hoffmann
Quoted 12 lines · Marc-André Lureau
Hi
Quoted 9 lines · Gerd Hoffmann
Hi,
Quoted 5 lines
(quoted diff) +5
-0+static const VirtioPCIDeviceTypeInfo vhost_user_input_pci_info = { + .base_name = TYPE_VHOST_USER_INPUT_PCI, + .generic_name = "vhost-user-input-pci", + .transitional_name = "vhost-user-input-pci-transitional", + .non_transitional_name = "vhost-user-input-pci-non-transitional",Quoted 8 lines
Quoted 6 lines
virtio-input is virtio 1.0 only, so we don't need these variants. Incremental fix below, if you ack that I can squash it for the pull req. Or you send a v3 (in case you have other changes pending).
cheers,
Gerdack, thanks
Spoke too soon, patch failed tests.
New version (with some virtio-input-host fixes on top) pushed to:
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
Can you please verify?
Looks good, tested successfully.
thanksnack, I tested wrongly, investigating some issue
In your git tree, you removed:
- .parent = TYPE_VIRTIO_INPUT_PCI,
from vhost_user_input_pci_info, please add it back
Signature · 1 line
Marc-André Lureau
PATCH v2 5/6 util: simplify unix_listen()
In reply to Marc-André LureauThe only caller of unix_listen() left is qga/channel-posix.c.
There is no need to deal with legacy coma-trailing options ",...".
--- util/qemu-sockets.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-)
util/qemu-sockets.c +2 -16
@@ -966,26 +966,12 @@ static int unix_connect_saddr(UnixSocketAddress *saddr, Error **errp)/* compatibility wrapper */ int unix_listen (const char *str,Error **errp) { -char *path, *optstr; -int sock, len;UnixSocketAddress *saddr; +int sock; saddr =g_new0 (UnixSocketAddress,1 ); - - optstr =strchr (str,',' ); -if (optstr) { - len = optstr - str; -if (len) { - path =g_malloc (len+1 ); -snprintf (path, len+1 ,"%.*s" , len, str); - saddr->path = path; - } - }else { - saddr->path =g_strdup (str); - } - + saddr->path =g_strdup (str); sock =unix_listen_saddr (saddr, errp); -qapi_free_UnixSocketAddress (saddr);return sock; }
Signature · 1 line
2.21.0.777.g83232e3864
Re: [Qemu-devel] [PATCH v2 5/6] util: simplify unix_listen()
Quoted 8 lines · Marc-André Lureau
The only caller of
unix_listen()left isqga/channel-posix.c.There is no need to deal with legacy coma-trailing options ",...".
Signed-off-by Marc-André Lureau <[email protected]>--- util/qemu-sockets.c | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-)
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 :|
PATCH v2 6/6 contrib: add vhost-user-input
In reply to Marc-André LureauAdd a vhost-user input backend example, based on virtio-input-host device. It takes an evdev path as argument, and can be associated with a vhost-user-input device via a UNIX socket:
$ vhost-user-input -p /dev/input/eventX -s /tmp/vui.sock
$ qemu ... -chardev socket,id=vuic,path=/tmp/vui.sock -device vhost-user-input-pci,chardev=vuic
This example is intentionally not included in $TOOLS, and not installed by default.
--- contrib/vhost-user-input/main.c | 393 +++++++++++++++++++++++++ MAINTAINERS | 1 + Makefile | 11 + Makefile.objs | 1 + contrib/vhost-user-input/Makefile.objs | 1 + 5 files changed, 407 insertions(+) create mode 100644 contrib/vhost-user-input/main.c create mode 100644 contrib/vhost-user-input/Makefile.objs
contrib/vhost-user-input/main.c new file +393 -0
@@ -0,0 +1,393 @@ +/* +* This work is licensed under the terms of the GNU GPL, version 2 or +* (at your option) any later version. See the COPYING file in the +* top-level directory. +*/ + +#include "qemu/osdep.h" + +#include <glib.h> +#include <linux/input.h> + +#include "qemu/iov.h" +#include "qemu/bswap.h" +#include "qemu/sockets.h" +#include "contrib/libvhost-user/libvhost-user.h" +#include "contrib/libvhost-user/libvhost-user-glib.h" +#include "standard-headers/linux/virtio_input.h" +#include "qapi/error.h" + +typedef struct virtio_input_event virtio_input_event ; +typedef struct virtio_input_config virtio_input_config ; + +typedef struct VuInput { +VugDev dev; +GSource *evsrc; +int evdevfd; +GArray *config; +virtio_input_config *sel_config; +struct { +virtio_input_event event; +VuVirtqElement *elem; + } *queue; +uint32_t qindex, qsize; +}VuInput ; + +static void vi_input_send (VuInput *vi,struct virtio_input_event *event) +{ +VuDev *dev = &vi->dev.parent; +VuVirtq *vq =vu_get_queue (dev,0 ); +VuVirtqElement *elem; +int i, len; + +/* queue up events ... */ +if (vi->qindex == vi->qsize) { + vi->qsize++; + vi->queue =g_realloc_n (vi->queue, vi->qsize,sizeof (vi->queue[0 ])); + } + vi->queue[vi->qindex++].event = *event; + +/* ... until we see a report sync ... */ +if (event->type !=htole16 (EV_SYN ) || + event->code !=htole16 (SYN_REPORT )) { +return ; + } + +/* ... then check available space ... */ +for (i =0 ; i < vi->qindex; i++) { + elem =vu_queue_pop (dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +while (--i >=0 ) { +vu_queue_unpop (dev, vq, vi->queue[i].elem,0 ); + } + vi->qindex =0 ; +g_warning ("virtio-input queue full" ); +return ; + } + vi->queue[i].elem = elem; + } + +/* ... and finally pass them to the guest */ +for (i =0 ; i < vi->qindex; i++) { + elem = vi->queue[i].elem; + len =iov_from_buf (elem->in_sg, elem->in_num, +0 , &vi->queue[i].event,sizeof (virtio_input_event)); +vu_queue_push (dev, vq, elem, len); +g_free (elem); + } + +vu_queue_notify (&vi->dev.parent, vq); + vi->qindex =0 ; +} + +static void +vi_evdev_watch (VuDev *dev,int condition,void *data) +{ +VuInput *vi = data; +int fd = vi->evdevfd; + +g_debug ("Got evdev condition %x" , condition); + +struct virtio_input_event virtio; +struct input_event evdev; +int rc; + +for (;;) { + rc =read (fd, &evdev,sizeof (evdev)); +if (rc !=sizeof (evdev)) { +break ; + } + +g_debug ("input %d %d %d" , evdev.type, evdev.code, evdev.value); + + virtio.type =htole16 (evdev.type); + virtio.code =htole16 (evdev.code); + virtio.value =htole32 (evdev.value); +vi_input_send (vi, &virtio); + } +} + + +static void vi_handle_status (VuInput *vi,virtio_input_event *event) +{ +struct input_event evdev; +int rc; + +if (gettimeofday (&evdev.time,NULL )) { +perror ("vi_handle_status: gettimeofday" ); +return ; + } + + evdev.type =le16toh (event->type); + evdev.code =le16toh (event->code); + evdev.value =le32toh (event->value); + + rc =write (vi->evdevfd, &evdev,sizeof (evdev)); +if (rc ==-1 ) { +perror ("vi_host_handle_status: write" ); + } +} + +static void vi_handle_sts (VuDev *dev,int qidx) +{ +VuInput *vi =container_of (dev, VuInput, dev.parent); +VuVirtq *vq =vu_get_queue (dev, qidx); +virtio_input_event event; +VuVirtqElement *elem; +int len; + +g_debug ("%s" ,G_STRFUNC ); + +for (;;) { + elem =vu_queue_pop (dev, vq,sizeof (VuVirtqElement)); +if (!elem) { +break ; + } + +memset (&event,0 ,sizeof (event)); + len =iov_to_buf (elem->out_sg, elem->out_num, +0 , &event,sizeof (event)); +vi_handle_status (vi, &event); +vu_queue_push (dev, vq, elem, len); +g_free (elem); + } + +vu_queue_notify (&vi->dev.parent, vq); +} + +static void +vi_panic (VuDev *dev,const char *msg) +{ +g_critical ("%s\n" , msg); +exit (EXIT_FAILURE ); +} + +static void +vi_queue_set_started (VuDev *dev,int qidx,bool started) +{ +VuInput *vi =container_of (dev, VuInput, dev.parent); +VuVirtq *vq =vu_get_queue (dev, qidx); + +g_debug ("queue started %d:%d" , qidx, started); + +if (qidx ==1 ) { +vu_set_queue_handler (dev, vq, started ? vi_handle_sts :NULL ); + } + + started =vu_queue_started (dev,vu_get_queue (dev,0 )) && +vu_queue_started (dev,vu_get_queue (dev,1 )); + +if (started && !vi->evsrc) { + vi->evsrc =vug_source_new (&vi->dev, vi->evdevfd, +G_IO_IN , vi_evdev_watch, vi); + } + +if (!started && vi->evsrc) { +g_source_destroy (vi->evsrc); + vi->evsrc =NULL ; + } +} + +static virtio_input_config * +vi_find_config (VuInput *vi,uint8_t select,uint8_t subsel) +{ +virtio_input_config *cfg; +int i; + +for (i =0 ; i < vi->config->len; i++) { + cfg = &g_array_index (vi->config, virtio_input_config, i); +if (select == cfg->select && subsel == cfg->subsel) { +return cfg; + } + } + +return NULL ; +} + +static int vi_get_config (VuDev *dev,uint8_t *config,uint32_t len) +{ +VuInput *vi =container_of (dev, VuInput, dev.parent); + +g_return_val_if_fail (len <=sizeof (*vi->sel_config),-1 ); + +if (vi->sel_config) { +memcpy (config, vi->sel_config, len); + }else { +memset (config,0 , len); + } + +return 0 ; +} + +static int vi_set_config (VuDev *dev,const uint8_t *data, +uint32_t offset,uint32_t size, +uint32_t flags) +{ +VuInput *vi =container_of (dev, VuInput, dev.parent); +virtio_input_config *config = (virtio_input_config *)data; + + vi->sel_config =vi_find_config (vi, config->select, config->subsel); + +return 0 ; +} + +static const VuDevIface vuiface = { + .queue_set_started = vi_queue_set_started, + .get_config = vi_get_config, + .set_config = vi_set_config, +}; + +static void +vi_bits_config (VuInput *vi,int type,int count) +{ +virtio_input_config bits; +int rc, i, size =0 ; + +memset (&bits,0 ,sizeof (bits)); + rc =ioctl (vi->evdevfd,EVIOCGBIT (type, count /8 ), bits.u.bitmap); +if (rc <0 ) { +return ; + } + +for (i =0 ; i < count /8 ; i++) { +if (bits.u.bitmap[i]) { + size = i +1 ; + } + } +if (size ==0 ) { +return ; + } + + bits.select =VIRTIO_INPUT_CFG_EV_BITS ; + bits.subsel = type; + bits.size = size; +g_array_append_val (vi->config, bits); +} + +static char *opt_evdev; +static int opt_fdnum =-1 ; +static char *opt_socket_path; +static gboolean opt_nograb; +static gboolean opt_print_caps; + +static GOptionEntry entries[] = { + {"print-capabilities" ,'c' ,0 ,G_OPTION_ARG_NONE , &opt_print_caps, +"Print capabilities" ,NULL }, + {"no-grab" ,'n' ,0 ,G_OPTION_ARG_NONE , &opt_nograb, +"Don't grab device" ,NULL }, + {"fd" ,'f' ,0 ,G_OPTION_ARG_INT , &opt_fdnum, +"Use inherited fd socket" ,"FDNUM" }, + {"socket-path" ,'s' ,0 ,G_OPTION_ARG_FILENAME , &opt_socket_path, +"Use UNIX socket path" ,"PATH" }, + {"evdev-path" ,'p' ,0 ,G_OPTION_ARG_FILENAME , &opt_evdev, +"evdev input device path" ,"PATH" }, + {NULL , } +}; + +int +main (int argc,char *argv[]) +{ +GMainLoop *loop =NULL ; +VuInput vi = {0 , }; +int rc, ver, fd; +virtio_input_config id; +struct input_id ids; +GError *error =NULL ; +GOptionContext *context; + + context =g_option_context_new (NULL ); +g_option_context_add_main_entries (context, entries,NULL ); +if (!g_option_context_parse (context, &argc, &argv, &error)) { +g_printerr ("Option parsing failed: %s\n" , error->message); +exit (EXIT_FAILURE ); + } +if (opt_print_caps) { +g_print ("{\n" ); +g_print (" \"type\": \"input\",\n" ); +g_print (" \"features\": [\n" ); +g_print (" \"evdev-path\",\n" ); +g_print (" \"no-grab\"\n" ); +g_print (" ]\n" ); +g_print ("}\n" ); +exit (EXIT_SUCCESS ); + } +if (!opt_evdev) { +g_printerr ("Please specify an evdev path\n" ); +exit (EXIT_FAILURE ); + } +if ((!!opt_socket_path + (opt_fdnum !=-1 )) !=1 ) { +g_printerr ("Please specify either --fd or --socket-path\n" ); +exit (EXIT_FAILURE ); + } + + vi.evdevfd =open (opt_evdev,O_RDWR ); +if (vi.evdevfd <0 ) { +g_printerr ("Failed to open evdev: %s\n" ,g_strerror (errno)); +exit (EXIT_FAILURE ); + } + + rc =ioctl (vi.evdevfd,EVIOCGVERSION , &ver); +if (rc <0 ) { +g_printerr ("%s: is not an evdev device\n" , argv[1 ]); +exit (EXIT_FAILURE ); + } + +if (!opt_nograb) { + rc =ioctl (vi.evdevfd,EVIOCGRAB ,1 ); +if (rc <0 ) { +g_printerr ("Failed to grab device\n" ); +exit (EXIT_FAILURE ); + } + } + + vi.config =g_array_new (false, false,sizeof (virtio_input_config)); +memset (&id,0 ,sizeof (id)); +ioctl (vi.evdevfd,EVIOCGNAME (sizeof (id.u.string) -1 ), id.u.string); + id.select =VIRTIO_INPUT_CFG_ID_NAME ; + id.size =strlen (id.u.string); +g_array_append_val (vi.config, id); + +if (ioctl (vi.evdevfd,EVIOCGID , &ids) ==0 ) { +memset (&id,0 ,sizeof (id)); + id.select =VIRTIO_INPUT_CFG_ID_DEVIDS ; + id.size =sizeof (struct virtio_input_devids ); + id.u.ids.bustype =cpu_to_le16 (ids.bustype); + id.u.ids.vendor =cpu_to_le16 (ids.vendor); + id.u.ids.product =cpu_to_le16 (ids.product); + id.u.ids.version =cpu_to_le16 (ids.version); +g_array_append_val (vi.config, id); + } + +vi_bits_config (&vi,EV_KEY ,KEY_CNT ); +vi_bits_config (&vi,EV_REL ,REL_CNT ); +vi_bits_config (&vi,EV_ABS ,ABS_CNT ); +vi_bits_config (&vi,EV_MSC ,MSC_CNT ); +vi_bits_config (&vi,EV_SW ,SW_CNT ); +g_debug ("config length: %u" , vi.config->len); + +if (opt_socket_path) { +int lsock =unix_listen (opt_socket_path, &error_fatal); + fd =accept (lsock,NULL ,NULL ); +close (lsock); + }else { + fd = opt_fdnum; + } +if (fd ==-1 ) { +g_printerr ("Invalid socket" ); +exit (EXIT_FAILURE ); + } +vug_init (&vi.dev, fd, vi_panic, &vuiface); + + loop =g_main_loop_new (NULL , FALSE); +g_main_loop_run (loop); +g_main_loop_unref (loop); + +vug_deinit (&vi.dev); + +if (vi.evsrc) { +g_source_unref (vi.evsrc); + } +g_array_free (vi.config, TRUE); +g_free (vi.queue); +return 0 ; +}
MAINTAINERS +1 -0
@@ -1524,6 +1524,7 @@ S: Maintained F: hw/input/vhost-user-input.c F: hw/input/virtio-input*.c F: include/hw/virtio/virtio-input.h +F: contrib/vhost-user-input/* virtio-serial M: Amit Shah <[email protected]>
Makefile +11 -0
@@ -406,6 +406,7 @@ dummy := $(call unnest-vars,, \ libvhost-user-obj-y \ vhost-user-scsi-obj-y \ vhost-user-blk-obj-y \ + vhost-user-input-obj-y \ qga-vss-dll-obj-y \ block-obj-y \ block-obj-m \
@@ -615,6 +616,16 @@ rdmacm-mux$(EXESUF): LIBS += "-libumad" rdmacm-mux$(EXESUF): $(rdmacm-mux-obj-y) $(COMMON_LDADDS) $(call LINK, $^) +ifdef CONFIG_VHOST_USER_INPUT +ifdef CONFIG_LINUX +vhost-user-input$(EXESUF): $(vhost-user-input-obj-y) libvhost-user.a libqemuutil.a + $(call LINK, $^) + +# build by default, do not install +all: vhost-user-input$(EXESUF) +endif +endif + module_block.h: $(SRC_PATH)/scripts/modules/module_block.py config-host.mak $(call quiet-command,$(PYTHON) $< $@ \ $(addprefix $(SRC_PATH)/,$(patsubst %.mo,%.c,$(block-obj-m))), \
Makefile.objs +1 -0
@@ -122,6 +122,7 @@ vhost-user-scsi.o-libs := $(LIBISCSI_LIBS) vhost-user-scsi-obj-y = contrib/vhost-user-scsi/ vhost-user-blk-obj-y = contrib/vhost-user-blk/ rdmacm-mux-obj-y = contrib/rdmacm-mux/ +vhost-user-input-obj-y = contrib/vhost-user-input/ ###################################################################### trace-events-subdirs =
contrib/vhost-user-input/Makefile.objs new file +1 -0
@@ -0,0 +1 @@ +vhost-user-input-obj-y = main.o
Signature · 1 line
2.21.0.777.g83232e3864
Quoted 12 lines · Marc-André Lureau
Hi,
This is the vhost-user-input part of "[PATCH v6 00/11] vhost-user for input & GPU".
v2:
- build fixesv1: (changes since original v6 series)
- add "libvhost-user: fix -Waddress-of-packed-member" & "util: simplifyunix_listen()"
- useunix_listen()
- build vhost-user-input by default (when applicable)
Quoted 7 lines
Marc-André Lureau (6):
libvhost-user: fix -Waddress-of-packed-member
libvhost-user: addPROTOCOL_F_CONFIGif {set,get}_config Add vhost-user-backend
Add vhost-user-input-pci
util: simplifyunix_listen()
contrib: add vhost-user-input
OK looks sane.
Gerd, IIUC you are going to merge this right?
If so
thanks!
Quoted 25 lines
include/sysemu/vhost-user-backend.h | 57 ++++ backends/vhost-user.c | 209 +++++++++++++ contrib/libvhost-user/libvhost-user.c | 10 +- contrib/vhost-user-input/main.c | 393 +++++++++++++++++++++++++ hw/input/vhost-user-input.c | 129 ++++++++ hw/virtio/vhost-user-input-pci.c | 53 ++++ util/qemu-sockets.c | 18 +- MAINTAINERS | 4 + Makefile | 11 + Makefile.objs | 1 + backends/Makefile.objs | 2 + contrib/vhost-user-input/Makefile.objs | 1 + hw/input/Kconfig | 5 + hw/input/Makefile.objs | 1 + hw/virtio/Makefile.objs | 1 + 16 files changed, 890 insertions(+), 19 deletions(-) create mode 100644 include/sysemu/vhost-user-backend.h create mode 100644 backends/vhost-user.c create mode 100644 contrib/vhost-user-input/main.c create mode 100644 hw/input/vhost-user-input.c create mode 100644 hw/virtio/vhost-user-input-pci.c create mode 100644 contrib/vhost-user-input/Makefile.objsSignature · 1 line
2.21.0.777.g83232e3864