hw/display: add vhost-user-media device
cover hw/display: add vhost-user-media device
This series introduces the QEMU frontend for the virtio-media device, a VirtIO device that exposes V4L2 video device functionality to the guest over the virtio-media protocol, as specified in the VirtIO specification v1.4, section 5.22 [1].
Note on patch 1
---------------
Patch 1 adds a temporary virtio-media device ID to QEMU's copy of the Linux standard headers. It is included to unblock testing but is marked "DO NOT MERGE": it will be superseded once the corresponding kernel-side patch [2] is merged upstream and QEMU's headers are updated to match. Patches 2 and 3 depend on patch 1 for the VIRTIO_ID_MEDIA definition.
Overview
--------
Patch 2 introduces the vhost-user-media device frontend as defined by the specification.
Patch 3 adds a 4 GiB shared memory PCI BAR (BAR 2) used by the guest driver as a DMA cache for video buffer exchange with the host backend. The region is exposed to the guest through a VirtIO shared memory capability.
Testing
-------
The series was tested with:
Guest driver: the virtio-media out-of-tree Linux kernel driver [3]. An upstream series for this driver has also been posted [4].
Host backend: the rust-vmm vhost-device-media backend [5]:
cargo run -- -s /path/to/media.sock -d /dev/video0 --backend v4l2-proxy
Full QEMU invocation:
qemu-system-x86_64 \
-chardev socket,path=/tmp/media.sock,id=media \
-device vhost-user-media-pci,chardev=media,id=media \
-object memory-backend-memfd,id=mem,size=4G,share=on \
-numa node,memdev=mem[1] https://docs.oasis-open.org/virtio/virtio/v1.4/cs01/virtio-v1.4-cs01.html
[2] https://lore.kernel.org/all/[email protected]/
[3] https://github.com/chromeos/virtio-media/tree/main/driver
[4] https://lore.kernel.org/all/[email protected]/
[5] https://github.com/rust-vmm/vhost-device/pull/944
Albert Esteve (3):
virtio_ids: Add ID for virtio media
hw/display: add vhost-user-media device hw/display/vhost-user-media: add shared memory cache BAR
hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-media-pci.c | 105 ++++++ hw/display/vhost-user-media.c | 397 ++++++++++++++++++++ hw/virtio/virtio.c | 3 +- include/hw/virtio/vhost-user-media.h | 47 +++ include/standard-headers/linux/virtio_ids.h | 1 + 7 files changed, 560 insertions(+), 1 deletion(-) create mode 100644 hw/display/vhost-user-media-pci.c create mode 100644 hw/display/vhost-user-media.c create mode 100644 include/hw/virtio/vhost-user-media.h
Signature · 1 line
2.54.0
PATCH 1/3 virtio_ids: Add ID for virtio media
Temporary commit. DO NOT MERGE.
While we wait for the patch [1] to get merged upstream, manually update and add the virtio-media ID to the kernel header for testing purposes.
[1] https://lore.kernel.org/all/[email protected]/
--- include/standard-headers/linux/virtio_ids.h | 1 + 1 file changed, 1 insertion(+)
include/standard-headers/linux/virtio_ids.h +1 -0
@@ -69,6 +69,7 @@#define VIRTIO_ID_BT 40/* virtio bluetooth */ #define VIRTIO_ID_GPIO 41/* virtio gpio */ #define VIRTIO_ID_SPI 45/* virtio spi */ +#define VIRTIO_ID_MEDIA 48/* virtio media */ /* * Virtio Transitional IDs
Signature · 1 line
2.54.0
PATCH 2/3 hw/display: add vhost-user-media device
In reply to Albert EsteveAdd the QEMU side of the vhost-user-media device, which connects to a virtio-media vhost-user backend daemon implementing the V4L2 API over the virtio-media protocol.
The virtio-media device is specified in the VirtIO specification v1.4, section 5.22:
https://docs.oasis-open.org/virtio/virtio/v1.4/cs01/virtio-v1.4-cs01.html
Tested with the rust-vmm vhost-device-media backend [1]:
cargo run -- -s /path/to/media.sock -d /dev/video0 --backend v4l2-proxy
Example invocation:
qemu-system-x86_64 \
-chardev socket,path=/tmp/media.sock,id=media \
-device vhost-user-media-pci,chardev=media,id=media[1] https://github.com/rust-vmm/vhost-device/pull/944
--- hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-media-pci.c | 77 ++++++ hw/display/vhost-user-media.c | 393 +++++++++++++++++++++++++++ hw/virtio/virtio.c | 3 +- include/hw/virtio/vhost-user-media.h | 47 ++++ 6 files changed, 527 insertions(+), 1 deletion(-) create mode 100644 hw/display/vhost-user-media-pci.c create mode 100644 hw/display/vhost-user-media.c create mode 100644 include/hw/virtio/vhost-user-media.h
hw/display/Kconfig +5 -0
@@ -120,6 +120,11 @@ config VHOST_USER_VGA default y depends on VIRTIO_VGA && VHOST_USER_GPU +config VHOST_USER_MEDIA + bool + default y + depends on VIRTIO && VHOST_USER + config DPCD bool select AUX
hw/display/meson.build +3 -0
@@ -37,6 +37,9 @@ system_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c')) system_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c')) system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-dmabuf.c')) system_ss.add(when: 'CONFIG_DM163', if_true: files('dm163.c')) +system_ss.add(when: 'CONFIG_VHOST_USER_MEDIA', if_true: files('vhost-user-media.c')) +system_ss.add(when: ['CONFIG_VHOST_USER_MEDIA', 'CONFIG_VIRTIO_PCI' ], + if_true: files('vhost-user-media-pci.c')) stub_ss.add([files('acpi-vga-stub.c'), pixman]) if (config_all_devices.has_key('CONFIG_VGA_CIRRUS') or
hw/display/vhost-user-media-pci.c new file +77 -0
77 lines not on this page · raw message
hw/display/vhost-user-media.c new file +393 -0
393 lines not on this page · raw message
hw/virtio/virtio.c +2 -1
@@ -198,7 +198,8 @@ const char *virtio_device_names[] = { [VIRTIO_ID_AUDIO_POLICY] = "virtio-audio-pol", [VIRTIO_ID_BT] = "virtio-bluetooth", [VIRTIO_ID_GPIO] = "virtio-gpio", - [VIRTIO_ID_SPI] = "virtio-spi" + [VIRTIO_ID_SPI] = "virtio-spi", + [VIRTIO_ID_MEDIA] = "virtio-media", };static const char *virtio_id_to_name (uint16_t device_id)
include/hw/virtio/vhost-user-media.h new file +47 -0
47 lines not on this page · raw message
Signature · 1 line
2.54.0
Quoted 69 lines · Albert Esteve
Add the QEMU side of the vhost-user-media device, which connects to a virtio-media vhost-user backend daemon implementing the V4L2 API over the virtio-media protocol.
The virtio-media device is specified in the VirtIO specification v1.4, section 5.22:
https://docs.oasis-open.org/virtio/virtio/v1.4/cs01/virtio-v1.4-cs01.htmlTested with the rust-vmm vhost-device-media backend [1]:
cargo run -- -s /path/to/media.sock -d /dev/video0 --backend v4l2-proxyExample invocation:
qemu-system-x86_64 \ -chardev socket,path=/tmp/media.sock,id=media \ -device vhost-user-media-pci,chardev=media,id=media[1] https://github.com/rust-vmm/vhost-device/pull/944
Signed-off-by Albert Esteve <[email protected]>--- hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-media-pci.c | 77 ++++++ hw/display/vhost-user-media.c | 393 +++++++++++++++++++++++++++ hw/virtio/virtio.c | 3 +- include/hw/virtio/vhost-user-media.h | 47 ++++ 6 files changed, 527 insertions(+), 1 deletion(-) create mode 100644 hw/display/vhost-user-media-pci.c create mode 100644 hw/display/vhost-user-media.c create mode 100644 include/hw/virtio/vhost-user-media.hhw/display/Kconfig +5
-0@@ -120,6 +120,11 @@ config VHOST_USER_VGA default y depends on VIRTIO_VGA && VHOST_USER_GPU +config VHOST_USER_MEDIA + bool + default y + depends on VIRTIO && VHOST_USER + config DPCD bool select AUXhw/display/meson.build +3
-0@@ -37,6 +37,9 @@ system_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c')) system_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c')) system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-dmabuf.c')) system_ss.add(when: 'CONFIG_DM163', if_true: files('dm163.c')) +system_ss.add(when: 'CONFIG_VHOST_USER_MEDIA', if_true: files('vhost-user-media.c')) +system_ss.add(when: ['CONFIG_VHOST_USER_MEDIA', 'CONFIG_VIRTIO_PCI' ], + if_true: files('vhost-user-media-pci.c')) stub_ss.add([files('acpi-vga-stub.c'), pixman]) if (config_all_devices.has_key('CONFIG_VGA_CIRRUS') orhw/display/vhost-user-media-pci.c new file +0
-0
hw/display/vhost-user-media-pci.c line 4
Quoted 4 lines
hw/display/vhost-user-media-pci.c new file +4
-0@@ -0,0 +1,77 @@ +/* +* Vhost-user MEDIA virtio device PCI glue +* +* Copyright Red Hat, Inc. 2024
Have you really been hanging on to this new code for 2 years before posting ? Should this be 2026 ?
Quoted 11 lines
hw/display/vhost-user-media-pci.c new file +4
-0+* Authors: Albert Esteve <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ hw/display/vhost-user-media.c new file +0
-0
hw/display/vhost-user-media.c line 4
Quoted 4 lines
hw/display/vhost-user-media.c new file +4
-0@@ -0,0 +1,393 @@ +/* +* Vhost-user Media device +* +* Copyright Red Hat, Inc. 2024
Same Q
Quoted 17 lines
hw/display/vhost-user-media.c new file +10
-0+* +* This is the boilerplate for instantiating a vhost-user device +* implementing a virtio-media device. +* +* Authors: +* Albert Esteve <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +include/hw/virtio/vhost-user-media.h new file +0
-0
include/hw/virtio/vhost-user-media.h line 4
Quoted 4 lines
include/hw/virtio/vhost-user-media.h new file +4
-0@@ -0,0 +1,47 @@ +/* +* vhost-user-media virtio device +* +* Copyright Red Hat, Inc. 2024
Again
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
Quoted 63 lines · Daniel P. Berrangé
Quoted 61 lines · Albert Esteve
Add the QEMU side of the vhost-user-media device, which connects to a virtio-media vhost-user backend daemon implementing the V4L2 API over the virtio-media protocol.
The virtio-media device is specified in the VirtIO specification v1.4, section 5.22:
https://docs.oasis-open.org/virtio/virtio/v1.4/cs01/virtio-v1.4-cs01.htmlTested with the rust-vmm vhost-device-media backend [1]:
cargo run -- -s /path/to/media.sock -d /dev/video0 --backend v4l2-proxyExample invocation:
qemu-system-x86_64 \ -chardev socket,path=/tmp/media.sock,id=media \ -device vhost-user-media-pci,chardev=media,id=media[1] https://github.com/rust-vmm/vhost-device/pull/944
Signed-off-by Albert Esteve <[email protected]>--- hw/display/Kconfig | 5 + hw/display/meson.build | 3 + hw/display/vhost-user-media-pci.c | 77 ++++++ hw/display/vhost-user-media.c | 393 +++++++++++++++++++++++++++ hw/virtio/virtio.c | 3 +- include/hw/virtio/vhost-user-media.h | 47 ++++ 6 files changed, 527 insertions(+), 1 deletion(-) create mode 100644 hw/display/vhost-user-media-pci.c create mode 100644 hw/display/vhost-user-media.c create mode 100644 include/hw/virtio/vhost-user-media.hhw/display/Kconfig +5
-0@@ -120,6 +120,11 @@ config VHOST_USER_VGA default y depends on VIRTIO_VGA && VHOST_USER_GPU +config VHOST_USER_MEDIA + bool + default y + depends on VIRTIO && VHOST_USER + config DPCD bool select AUXhw/display/meson.build +3
-0@@ -37,6 +37,9 @@ system_ss.add(when: 'CONFIG_NEXTCUBE', if_true: files('next-fb.c')) system_ss.add(when: 'CONFIG_VGA', if_true: files('vga.c')) system_ss.add(when: 'CONFIG_VIRTIO', if_true: files('virtio-dmabuf.c')) system_ss.add(when: 'CONFIG_DM163', if_true: files('dm163.c')) +system_ss.add(when: 'CONFIG_VHOST_USER_MEDIA', if_true: files('vhost-user-media.c')) +system_ss.add(when: ['CONFIG_VHOST_USER_MEDIA', 'CONFIG_VIRTIO_PCI' ], + if_true: files('vhost-user-media-pci.c'))
Quoted 15 lines
hw/display/vhost-user-media-pci.c line 4
Quoted 12 lines
hw/display/meson.build +0
-0stub_ss.add([files('acpi-vga-stub.c'), pixman]) if (config_all_devices.has_key('CONFIG_VGA_CIRRUS') orhw/display/vhost-user-media-pci.c new file +4
-0@@ -0,0 +1,77 @@ +/* +* Vhost-user MEDIA virtio device PCI glue +* +* Copyright Red Hat, Inc. 2024 Have you really been hanging on to this new code for 2 years before posting ? Should this be 2026 ?
Yup! Rebasing/updating has been painful a few times. But it was waiting for the specs to land, and the shared memory mapping patch. And other developments in parallel (e.g., virtio_ids.h kernel header is still in the process).
But to your point, yes, I will update the copyright in the next version. It makes sense to match the copyright year to when it is upstreamed.
BR,
Albert.
Quoted 51 lines
Quoted 12 lines
hw/display/vhost-user-media-pci.c new file +4
-0+* Authors: Albert Esteve <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ hw/display/vhost-user-media.c new file +0
-0hw/display/vhost-user-media.c line 4
Quoted 4 lines
hw/display/vhost-user-media.c new file +4
-0@@ -0,0 +1,393 @@ +/* +* Vhost-user Media device +* +* Copyright Red Hat, Inc. 2024 Same Q
Quoted 17 lines
hw/display/vhost-user-media.c new file +10
-0+* +* This is the boilerplate for instantiating a vhost-user device +* implementing a virtio-media device. +* +* Authors: +* Albert Esteve <[email protected]> +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ +include/hw/virtio/vhost-user-media.h new file +0
-0include/hw/virtio/vhost-user-media.h line 4
Quoted 4 lines
include/hw/virtio/vhost-user-media.h new file +4
-0@@ -0,0 +1,47 @@ +/* +* vhost-user-media virtio device +* +* Copyright Red Hat, Inc. 2024 Again
With regards,
Daniel
--|: https://berrange.com ~~ https://hachyderm.io/@berrange :| |: https://libvirt.org ~~ https://entangle-photo.org :| |: https://pixelfed.art/berrange ~~ https://fstop138.berrange.com :|
PATCH 3/3 hw/display/vhost-user-media: add shared memory cache BAR
In reply to Albert EsteveRegister a 4 GiB shared memory PCI BAR (BAR 2) so the guest driver can use it as a DMA cache for video buffers. The memory region is allocated on device realize and exposed to the guest through a VirtIO shared memory capability (shmem ID 0).
The host must be started with a memfd-backed memory object covering at least the shared region size, e.g.:
-object memory-backend-memfd,id=mem,size=4G,share=on \
-numa node,memdev=mem--- hw/display/vhost-user-media-pci.c | 28 ++++++++++++++++++++++++++++ hw/display/vhost-user-media.c | 4 ++++ 2 files changed, 32 insertions(+)
hw/display/vhost-user-media-pci.c +28 -0
@@ -12,12 +12,18 @@#include "hw/virtio/vhost-user-media.h" #include "hw/virtio/virtio-pci.h" +/* BAR 2 is used for the shared memory cache region exposed to the guest */ +#define VIRTIO_MEDIA_PCI_CACHE_BAR 2 + +#define VIRTIO_MEDIA_PCI_SHMCAP_ID_CACHE 0 +#define TYPE_VHOST_USER_MEDIA_PCI "vhost-user-media-pci-base" OBJECT_DECLARE_SIMPLE_TYPE(VHostUserMEDIAPCI, VHOST_USER_MEDIA_PCI)struct VHostUserMEDIAPCI {VirtIOPCIProxy parent_obj;VHostUserMEDIA vdev; +MemoryRegion cachebar; };static const Property vumedia_pci_properties[] = {
@@ -31,12 +37,34 @@ static void vumedia_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) {VHostUserMEDIAPCI *dev =VHOST_USER_MEDIA_PCI (vpci_dev);DeviceState *dev_state =DEVICE (&dev->vdev); +VirtIODevice *vdev =VIRTIO_DEVICE (dev_state); +VirtioSharedMemory *shmem; +uint64_t cache_size;if (vpci_dev->nvectors ==DEV_NVECTORS_UNSPECIFIED ) { vpci_dev->nvectors =1 ; }qdev_realize (dev_state,BUS (&vpci_dev->bus), errp); +if (*errp) { +return ; + } + + shmem =QSIMPLEQ_LAST (&vdev->shmem_list, VirtioSharedMemory, entry); + cache_size =memory_region_size (&shmem->mr); + +memory_region_init (&dev->cachebar,OBJECT (vpci_dev), +"vhost-media-pci-cachebar" , cache_size); +memory_region_add_subregion (&dev->cachebar,0 , &shmem->mr); +virtio_pci_add_shm_cap (vpci_dev,VIRTIO_MEDIA_PCI_CACHE_BAR ,0 , + cache_size,VIRTIO_MEDIA_PCI_SHMCAP_ID_CACHE ); + +/* After 'realized' so the memory region exists */ +pci_register_bar (&vpci_dev->pci_dev,VIRTIO_MEDIA_PCI_CACHE_BAR , +PCI_BASE_ADDRESS_SPACE_MEMORY | +PCI_BASE_ADDRESS_MEM_PREFETCH | +PCI_BASE_ADDRESS_MEM_TYPE_64 , + &dev->cachebar); }static void vumedia_pci_class_init (ObjectClass *klass,const void *data)
hw/display/vhost-user-media.c +4 -0
@@ -20,6 +20,8 @@#include "hw/virtio/virtio-bus.h" #include "hw/virtio/vhost-user-media.h" +#define CACHE_SIZE (1ull << 32) +static const int feature_bits[] = {VIRTIO_F_RING_RESET ,VHOST_INVALID_FEATURE_BIT
@@ -288,6 +290,8 @@ static void vhost_user_media_device_realize(DeviceState *dev, Error **errp)virtio_init (vdev,VIRTIO_ID_MEDIA ,sizeof (struct virtio_media_config )); +virtio_new_shmem_region (vdev,0 ,CACHE_SIZE ); +/* one command queue and one event queue */ media->vhost_dev.nvqs =2 ; media->vhost_dev.vqs = g_new0(struct vhost_virtqueue,
Signature · 1 line
2.54.0
Tested with vhost-device-media (v4l2-proxy backend) on Fedora 43 guest (kernel 6.17.1), proxying a host integrated camera. Device detected and frame capture works via v4l2-ctl --stream-mmap.