vfio-user client
v3 cover vfio-user client
The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.
The vfio-user protocol allows for implementing (PCI) devices in another userspace process; SPDK is one example, which includes a virtual NVMe implementation.
The vfio-user framework consists of 3 parts:
1) The VFIO user protocol specification.
2) A client - the VFIO device in QEMU that encapsulates VFIO messages and sends them to the server.
3) A server - a remote process that emulates a device.
This patchset implements parts 1 and 2.
It has been tested against libvfio-user test servers as well as SPDK.
A previous version of this series can be found at
https://lore.kernel.org/qemu-devel/[email protected]/
Changes since v2:
- rebase to current master
- code review feedback for region fd change
thanks
john
John Levon (22):
vfio: export PCI helpers needed for vfio-user
vfio: enable per-IRQ MSI-X masking
vfio: add per-region fd support
vfio: mark posted writes in region write callbacks
vfio-user: add vfio-user class and container
vfio-user: connect vfio proxy to remote server
vfio-user: implement message receive infrastructure
vfio-user: implement message send infrastructure
vfio-user: implement VFIO_USER_DEVICE_GET_INFO
vfio-user: implement VFIO_USER_DEVICE_GET_REGION_INFO
vfio-user: implement VFIO_USER_REGION_READ/WRITE
vfio-user: set up PCI in vfio_user_pci_realize()
vfio-user: implement VFIO_USER_DEVICE_GET/SET_IRQ*
vfio-user: forward MSI-X PBA BAR accesses to server
vfio-user: set up container access to the proxy
vfio-user: implement VFIO_USER_DEVICE_RESET
vfio-user: implement VFIO_USER_DMA_MAP/UNMAP
vfio-user: implement VFIO_USER_DMA_READ/WRITE
vfio-user: add 'x-msg-timeout' option
vfio-user: support posted writes
vfio-user: add coalesced posted writes
docs: add vfio-user documentation
Thanos Makatos (1):
vfio-user: introduce vfio-user protocol specification
MAINTAINERS | 10 +- docs/devel/index-internals.rst | 1 + docs/devel/vfio-user.rst | 1522 +++++++++++++++++++++++++ docs/system/device-emulation.rst | 1 + docs/system/devices/vfio-user.rst | 24 + meson.build | 1 + hw/vfio-user/container.h | 27 + hw/vfio-user/device.h | 27 + hw/vfio-user/protocol.h | 245 ++++ hw/vfio-user/proxy.h | 137 +++ hw/vfio-user/trace.h | 1 + hw/vfio/pci.h | 13 + include/hw/vfio/vfio-container-base.h | 1 + include/hw/vfio/vfio-device.h | 15 +- include/hw/vfio/vfio-region.h | 1 + hw/vfio-user/container.c | 348 ++++++ hw/vfio-user/device.c | 389 +++++++ hw/vfio-user/pci.c | 438 +++++++ hw/vfio-user/proxy.c | 1314 +++++++++++++++++++++ hw/vfio/device.c | 58 +- hw/vfio/pci.c | 99 +- hw/vfio/region.c | 12 +- hw/meson.build | 1 + hw/vfio-user/meson.build | 11 + hw/vfio-user/trace-events | 18 + hw/vfio/trace-events | 6 +- meson_options.txt | 2 + scripts/meson-buildoptions.sh | 4 + 28 files changed, 4679 insertions(+), 47 deletions(-) create mode 100644 docs/devel/vfio-user.rst create mode 100644 docs/system/devices/vfio-user.rst create mode 100644 hw/vfio-user/container.h create mode 100644 hw/vfio-user/device.h create mode 100644 hw/vfio-user/protocol.h create mode 100644 hw/vfio-user/proxy.h create mode 100644 hw/vfio-user/trace.h create mode 100644 hw/vfio-user/container.c create mode 100644 hw/vfio-user/device.c create mode 100644 hw/vfio-user/pci.c create mode 100644 hw/vfio-user/proxy.c create mode 100644 hw/vfio-user/meson.build create mode 100644 hw/vfio-user/trace-events
Signature · 1 line
2.43.0
PATCH v3 1/23 vfio: export PCI helpers needed for vfio-user
The vfio-user code will need to re-use various parts of the vfio PCI code. Export them in hw/vfio/pci.h, and rename them to the vfio_pci_* namespace.
--- hw/vfio/pci.h | 11 ++++++++++ hw/vfio/pci.c | 48 ++++++++++++++++++++++---------------------- hw/vfio/trace-events | 6 +++--- 3 files changed, 38 insertions(+), 27 deletions(-)
hw/vfio/pci.h +11 -0
@@ -248,4 +248,15 @@ void vfio_display_finalize(VFIOPCIDevice *vdev);extern const VMStateDescription vfio_display_vmstate; +void vfio_pci_bars_exit (VFIOPCIDevice *vdev); +bool vfio_pci_add_capabilities (VFIOPCIDevice *vdev,Error **errp); +bool vfio_pci_config_setup (VFIOPCIDevice *vdev,Error **errp); +bool vfio_pci_interrupt_setup (VFIOPCIDevice *vdev,Error **errp); +void vfio_pci_intx_eoi (VFIODevice *vbasedev); +void vfio_pci_put_device (VFIOPCIDevice *vdev); +bool vfio_pci_populate_device (VFIOPCIDevice *vdev,Error **errp); +void vfio_pci_register_err_notifier (VFIOPCIDevice *vdev); +void vfio_pci_register_req_notifier (VFIOPCIDevice *vdev); +void vfio_pci_teardown_msi (VFIOPCIDevice *vdev); +#endif /* HW_VFIO_VFIO_PCI_H */
hw/vfio/pci.c +24 -24
175 lines not on this page · raw message
hw/vfio/trace-events +3 -3
@@ -2,7 +2,7 @@ # pci.c vfio_intx_interrupt(const char *name, char line) " (%s) Pin %c" -vfio_intx_eoi(const char *name) " (%s) EOI" +vfio_pci_intx_eoi(const char *name) " (%s) EOI" vfio_intx_enable_kvm(const char *name) " (%s) KVM INTx accel enabled" vfio_intx_disable_kvm(const char *name) " (%s) KVM INTx accel disabled" vfio_intx_update(const char *name, int new_irq, int target_irq) " (%s) IRQ moved %d -> %d"
@@ -35,8 +35,8 @@ vfio_pci_hot_reset(const char *name, const char *type) " (%s) %s" vfio_pci_hot_reset_has_dep_devices(const char *name) "%s: hot reset dependent devices:" vfio_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int group_id) "\t%04x:%02x:%02x.%x group %d" vfio_pci_hot_reset_result(const char *name, const char *result) "%s hot reset: %s" -vfio_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx" -vfio_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s" +vfio_pci_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx" +vfio_pci_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s" vfio_mdev(const char *name, bool is_mdev) " (%s) is_mdev %d" vfio_add_ext_cap_dropped(const char *name, uint16_t cap, uint16_t offset) "%s 0x%x@0x%x" vfio_pci_reset(const char *name) " (%s)"
Signature · 1 line
2.43.0
Re: [PATCH v3 01/23] vfio: export PCI helpers needed for vfio-user
Quoted 5 lines · John Levon
The vfio-user code will need to re-use various parts of the vfio PCI code. Export them in
hw/vfio/pci.h, and rename them to the vfio_pci_* namespace.Signed-off-by John Levon <[email protected]>
Thanks,
C.
Quoted 250 lines
--- hw/vfio/pci.h | 11 ++++++++++ hw/vfio/pci.c | 48 ++++++++++++++++++++++---------------------- hw/vfio/trace-events | 6 +++--- 3 files changed, 38 insertions(+), 27 deletions(-)hw/vfio/pci.h +11
-0@@ -248,4 +248,15 @@ void vfio_display_finalize(VFIOPCIDevice *vdev);extern const VMStateDescription vfio_display_vmstate; +void vfio_pci_bars_exit (VFIOPCIDevice *vdev); +bool vfio_pci_add_capabilities (VFIOPCIDevice *vdev,Error **errp); +bool vfio_pci_config_setup (VFIOPCIDevice *vdev,Error **errp); +bool vfio_pci_interrupt_setup (VFIOPCIDevice *vdev,Error **errp); +void vfio_pci_intx_eoi (VFIODevice *vbasedev); +void vfio_pci_put_device (VFIOPCIDevice *vdev); +bool vfio_pci_populate_device (VFIOPCIDevice *vdev,Error **errp); +void vfio_pci_register_err_notifier (VFIOPCIDevice *vdev); +void vfio_pci_register_req_notifier (VFIOPCIDevice *vdev); +void vfio_pci_teardown_msi (VFIOPCIDevice *vdev); +#endif /* HW_VFIO_VFIO_PCI_H */ hw/vfio/pci.c +24
-24@@ -103,7 +103,7 @@ static void vfio_intx_interrupt(void *opaque) } } -static void vfio_intx_eoi (VFIODevice *vbasedev) +void vfio_pci_intx_eoi (VFIODevice *vbasedev) {VFIOPCIDevice *vdev =container_of (vbasedev, VFIOPCIDevice, vbasedev);@@ -111,7 +111,7 @@ static void vfio_intx_eoi(VFIODevice *vbasedev)return ; } -trace_vfio_intx_eoi (vbasedev->name); +trace_vfio_pci_intx_eoi (vbasedev->name); vdev->intx.pending = false;pci_irq_deassert (&vdev->pdev);@@ -236,7 +236,7 @@ static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route) }/* Re-enable the interrupt in cased we missed an EOI */ -vfio_intx_eoi (&vdev->vbasedev); +vfio_pci_intx_eoi (&vdev->vbasedev); }static void vfio_intx_routing_notifier (PCIDevice *pdev)@@ -1743,7 +1743,7 @@ static bool vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp)return true; } -static void vfio_teardown_msi (VFIOPCIDevice *vdev) +void vfio_pci_teardown_msi (VFIOPCIDevice *vdev) {msi_uninit (&vdev->pdev);@@ -1839,7 +1839,7 @@ static void vfio_bars_register(VFIOPCIDevice *vdev) } } -static void vfio_bars_exit (VFIOPCIDevice *vdev) +void vfio_pci_bars_exit (VFIOPCIDevice *vdev) {int i;@@ -2430,7 +2430,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)g_free (config); } -static bool vfio_add_capabilities (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_add_capabilities (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev = &vdev->pdev;@@ -2706,7 +2706,7 @@ static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)static VFIODeviceOps vfio_pci_ops = { .vfio_compute_needs_reset = vfio_pci_compute_needs_reset, .vfio_hot_reset_multi = vfio_pci_hot_reset_multi, - .vfio_eoi = vfio_intx_eoi, + .vfio_eoi = vfio_pci_intx_eoi, .vfio_get_object = vfio_pci_get_object, .vfio_save_config = vfio_pci_save_config, .vfio_load_config = vfio_pci_load_config,@@ -2777,7 +2777,7 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)return true; } -static bool vfio_populate_device (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_populate_device (VFIOPCIDevice *vdev,Error **errp) {VFIODevice *vbasedev = &vdev->vbasedev;struct vfio_region_info *reg_info =NULL ;@@ -2823,7 +2823,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)return false; } -trace_vfio_populate_device_config (vdev->vbasedev.name, +trace_vfio_pci_populate_device_config (vdev->vbasedev.name, (unsigned long )reg_info->size, (unsigned long )reg_info->offset, (unsigned long )reg_info->flags);@@ -2845,7 +2845,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp) ret =vfio_device_get_irq_info (vbasedev,VFIO_PCI_ERR_IRQ_INDEX , &irq_info);if (ret) {/* This can fail for an old kernel or legacy PCI dev */ -trace_vfio_populate_device_get_irq_info_failure (strerror (-ret)); +trace_vfio_pci_populate_device_get_irq_info_failure (strerror (-ret)); }else if (irq_info.count ==1 ) { vdev->pci_aer = true; }else {@@ -2857,7 +2857,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)return true; } -static void vfio_pci_put_device (VFIOPCIDevice *vdev) +void vfio_pci_put_device (VFIOPCIDevice *vdev) {vfio_display_finalize (vdev);vfio_bars_finalize (vdev);@@ -2905,7 +2905,7 @@ static void vfio_err_notifier_handler(void *opaque)* and continue after disabling error recovery support for the * device. */ -static void vfio_register_err_notifier (VFIOPCIDevice *vdev) +void vfio_pci_register_err_notifier (VFIOPCIDevice *vdev) {Error *err =NULL ;int32_t fd;@@ -2964,7 +2964,7 @@ static void vfio_req_notifier_handler(void *opaque) } } -static void vfio_register_req_notifier (VFIOPCIDevice *vdev) +void vfio_pci_register_req_notifier (VFIOPCIDevice *vdev) {struct vfio_irq_info irq_info;Error *err =NULL ;@@ -3018,7 +3018,7 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) vdev->req_enabled = false; } -static bool vfio_pci_config_setup (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_config_setup (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev = &vdev->pdev;VFIODevice *vbasedev = &vdev->vbasedev;@@ -3124,7 +3124,7 @@ static bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)return true; } -static bool vfio_interrupt_setup (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_interrupt_setup (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev = &vdev->pdev;@@ -3214,7 +3214,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp) goto error; } -if (!vfio_populate_device (vdev, errp)) { +if (!vfio_pci_populate_device (vdev, errp)) { goto error; }@@ -3228,7 +3228,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp) goto out_teardown; } -if (!vfio_add_capabilities (vdev, errp)) { +if (!vfio_pci_add_capabilities (vdev, errp)) { goto out_unset_idev; }@@ -3244,7 +3244,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)vfio_bar_quirk_setup (vdev, i); } -if (!vfio_interrupt_setup (vdev, errp)) { +if (!vfio_pci_interrupt_setup (vdev, errp)) { goto out_unset_idev; }@@ -3288,8 +3288,8 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp) } } -vfio_register_err_notifier (vdev); -vfio_register_req_notifier (vdev); +vfio_pci_register_err_notifier (vdev); +vfio_pci_register_req_notifier (vdev);vfio_setup_resetfn_quirk (vdev);return ;@@ -3310,8 +3310,8 @@ out_unset_idev:pci_device_unset_iommu_device (pdev); } out_teardown: -vfio_teardown_msi (vdev); -vfio_bars_exit (vdev); +vfio_pci_teardown_msi (vdev); +vfio_pci_bars_exit (vdev); error:error_prepend (errp,VFIO_MSG_PREFIX , vbasedev->name); }@@ -3338,9 +3338,9 @@ static void vfio_exitfn(PCIDevice *pdev)if (vdev->intx.mmap_timer) {timer_free (vdev->intx.mmap_timer); } -vfio_teardown_msi (vdev); +vfio_pci_teardown_msi (vdev);vfio_pci_disable_rp_atomics (vdev); -vfio_bars_exit (vdev); +vfio_pci_bars_exit (vdev);vfio_migration_exit (vbasedev);if (!vbasedev->mdev) {pci_device_unset_iommu_device (pdev);hw/vfio/trace-events +3
-3@@ -2,7 +2,7 @@ # pci.c vfio_intx_interrupt(const char *name, char line) " (%s) Pin %c" -vfio_intx_eoi(const char *name) " (%s) EOI" +vfio_pci_intx_eoi(const char *name) " (%s) EOI" vfio_intx_enable_kvm(const char *name) " (%s) KVM INTx accel enabled" vfio_intx_disable_kvm(const char *name) " (%s) KVM INTx accel disabled" vfio_intx_update(const char *name, int new_irq, int target_irq) " (%s) IRQ moved %d -> %d"@@ -35,8 +35,8 @@ vfio_pci_hot_reset(const char *name, const char *type) " (%s) %s" vfio_pci_hot_reset_has_dep_devices(const char *name) "%s: hot reset dependent devices:" vfio_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int group_id) "\t%04x:%02x:%02x.%x group %d" vfio_pci_hot_reset_result(const char *name, const char *result) "%s hot reset: %s" -vfio_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx" -vfio_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s" +vfio_pci_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx" +vfio_pci_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s" vfio_mdev(const char *name, bool is_mdev) " (%s) is_mdev %d" vfio_add_ext_cap_dropped(const char *name, uint16_t cap, uint16_t offset) "%s 0x%x@0x%x" vfio_pci_reset(const char *name) " (%s)"
Re: [PATCH v3 01/23] vfio: export PCI helpers needed for vfio-user
In reply to John LevonQuoted 245 lines · John Levon
The vfio-user code will need to re-use various parts of the vfio PCI code. Export them in
hw/vfio/pci.h, and rename them to the vfio_pci_* namespace.Signed-off-by John Levon <[email protected]>--- hw/vfio/pci.h | 11 ++++++++++ hw/vfio/pci.c | 48 ++++++++++++++++++++++---------------------- hw/vfio/trace-events | 6 +++--- 3 files changed, 38 insertions(+), 27 deletions(-)hw/vfio/pci.h +11
-0@@ -248,4 +248,15 @@ void vfio_display_finalize(VFIOPCIDevice *vdev);extern const VMStateDescription vfio_display_vmstate; +void vfio_pci_bars_exit (VFIOPCIDevice *vdev); +bool vfio_pci_add_capabilities (VFIOPCIDevice *vdev,Error **errp); +bool vfio_pci_config_setup (VFIOPCIDevice *vdev,Error **errp); +bool vfio_pci_interrupt_setup (VFIOPCIDevice *vdev,Error **errp); +void vfio_pci_intx_eoi (VFIODevice *vbasedev); +void vfio_pci_put_device (VFIOPCIDevice *vdev); +bool vfio_pci_populate_device (VFIOPCIDevice *vdev,Error **errp); +void vfio_pci_register_err_notifier (VFIOPCIDevice *vdev); +void vfio_pci_register_req_notifier (VFIOPCIDevice *vdev); +void vfio_pci_teardown_msi (VFIOPCIDevice *vdev); +#endif /* HW_VFIO_VFIO_PCI_H */ hw/vfio/pci.c +24
-24@@ -103,7 +103,7 @@ static void vfio_intx_interrupt(void *opaque) } } -static void vfio_intx_eoi (VFIODevice *vbasedev) +void vfio_pci_intx_eoi (VFIODevice *vbasedev) {VFIOPCIDevice *vdev =container_of (vbasedev, VFIOPCIDevice, vbasedev);@@ -111,7 +111,7 @@ static void vfio_intx_eoi(VFIODevice *vbasedev)return ; } -trace_vfio_intx_eoi (vbasedev->name); +trace_vfio_pci_intx_eoi (vbasedev->name); vdev->intx.pending = false;pci_irq_deassert (&vdev->pdev);@@ -236,7 +236,7 @@ static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route) }/* Re-enable the interrupt in cased we missed an EOI */ -vfio_intx_eoi (&vdev->vbasedev); +vfio_pci_intx_eoi (&vdev->vbasedev); }static void vfio_intx_routing_notifier (PCIDevice *pdev)@@ -1743,7 +1743,7 @@ static bool vfio_msix_setup(VFIOPCIDevice *vdev, int pos, Error **errp)return true; } -static void vfio_teardown_msi (VFIOPCIDevice *vdev) +void vfio_pci_teardown_msi (VFIOPCIDevice *vdev) {msi_uninit (&vdev->pdev);@@ -1839,7 +1839,7 @@ static void vfio_bars_register(VFIOPCIDevice *vdev) } } -static void vfio_bars_exit (VFIOPCIDevice *vdev) +void vfio_pci_bars_exit (VFIOPCIDevice *vdev) {int i;@@ -2430,7 +2430,7 @@ static void vfio_add_ext_cap(VFIOPCIDevice *vdev)g_free (config); } -static bool vfio_add_capabilities (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_add_capabilities (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev = &vdev->pdev;@@ -2706,7 +2706,7 @@ static int vfio_pci_load_config(VFIODevice *vbasedev, QEMUFile *f)static VFIODeviceOps vfio_pci_ops = { .vfio_compute_needs_reset = vfio_pci_compute_needs_reset, .vfio_hot_reset_multi = vfio_pci_hot_reset_multi, - .vfio_eoi = vfio_intx_eoi, + .vfio_eoi = vfio_pci_intx_eoi, .vfio_get_object = vfio_pci_get_object, .vfio_save_config = vfio_pci_save_config, .vfio_load_config = vfio_pci_load_config,@@ -2777,7 +2777,7 @@ bool vfio_populate_vga(VFIOPCIDevice *vdev, Error **errp)return true; } -static bool vfio_populate_device (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_populate_device (VFIOPCIDevice *vdev,Error **errp) {VFIODevice *vbasedev = &vdev->vbasedev;struct vfio_region_info *reg_info =NULL ;@@ -2823,7 +2823,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)return false; } -trace_vfio_populate_device_config (vdev->vbasedev.name, +trace_vfio_pci_populate_device_config (vdev->vbasedev.name, (unsigned long )reg_info->size, (unsigned long )reg_info->offset, (unsigned long )reg_info->flags);@@ -2845,7 +2845,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp) ret =vfio_device_get_irq_info (vbasedev,VFIO_PCI_ERR_IRQ_INDEX , &irq_info);if (ret) {/* This can fail for an old kernel or legacy PCI dev */ -trace_vfio_populate_device_get_irq_info_failure (strerror (-ret)); +trace_vfio_pci_populate_device_get_irq_info_failure (strerror (-ret)); }else if (irq_info.count ==1 ) { vdev->pci_aer = true; }else {@@ -2857,7 +2857,7 @@ static bool vfio_populate_device(VFIOPCIDevice *vdev, Error **errp)return true; } -static void vfio_pci_put_device (VFIOPCIDevice *vdev) +void vfio_pci_put_device (VFIOPCIDevice *vdev) {vfio_display_finalize (vdev);vfio_bars_finalize (vdev);@@ -2905,7 +2905,7 @@ static void vfio_err_notifier_handler(void *opaque)* and continue after disabling error recovery support for the * device. */ -static void vfio_register_err_notifier (VFIOPCIDevice *vdev) +void vfio_pci_register_err_notifier (VFIOPCIDevice *vdev) {Error *err =NULL ;int32_t fd;@@ -2964,7 +2964,7 @@ static void vfio_req_notifier_handler(void *opaque) } } -static void vfio_register_req_notifier (VFIOPCIDevice *vdev) +void vfio_pci_register_req_notifier (VFIOPCIDevice *vdev) {struct vfio_irq_info irq_info;Error *err =NULL ;@@ -3018,7 +3018,7 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev) vdev->req_enabled = false; } -static bool vfio_pci_config_setup (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_config_setup (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev = &vdev->pdev;VFIODevice *vbasedev = &vdev->vbasedev;@@ -3124,7 +3124,7 @@ static bool vfio_pci_config_setup(VFIOPCIDevice *vdev, Error **errp)return true; } -static bool vfio_interrupt_setup (VFIOPCIDevice *vdev,Error **errp) +bool vfio_pci_interrupt_setup (VFIOPCIDevice *vdev,Error **errp) {PCIDevice *pdev = &vdev->pdev;@@ -3214,7 +3214,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp) goto error; } -if (!vfio_populate_device (vdev, errp)) { +if (!vfio_pci_populate_device (vdev, errp)) { goto error; }@@ -3228,7 +3228,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp) goto out_teardown; } -if (!vfio_add_capabilities (vdev, errp)) { +if (!vfio_pci_add_capabilities (vdev, errp)) { goto out_unset_idev; }@@ -3244,7 +3244,7 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp)vfio_bar_quirk_setup (vdev, i); } -if (!vfio_interrupt_setup (vdev, errp)) { +if (!vfio_pci_interrupt_setup (vdev, errp)) { goto out_unset_idev; }@@ -3288,8 +3288,8 @@ static void vfio_pci_realize(PCIDevice *pdev, Error **errp) } } -vfio_register_err_notifier (vdev); -vfio_register_req_notifier (vdev); +vfio_pci_register_err_notifier (vdev); +vfio_pci_register_req_notifier (vdev);vfio_setup_resetfn_quirk (vdev);return ;@@ -3310,8 +3310,8 @@ out_unset_idev:pci_device_unset_iommu_device (pdev); } out_teardown: -vfio_teardown_msi (vdev); -vfio_bars_exit (vdev); +vfio_pci_teardown_msi (vdev); +vfio_pci_bars_exit (vdev); error:error_prepend (errp,VFIO_MSG_PREFIX , vbasedev->name); }@@ -3338,9 +3338,9 @@ static void vfio_exitfn(PCIDevice *pdev)if (vdev->intx.mmap_timer) {timer_free (vdev->intx.mmap_timer); } -vfio_teardown_msi (vdev); +vfio_pci_teardown_msi (vdev);vfio_pci_disable_rp_atomics (vdev); -vfio_bars_exit (vdev); +vfio_pci_bars_exit (vdev);vfio_migration_exit (vbasedev);if (!vbasedev->mdev) {pci_device_unset_iommu_device (pdev);hw/vfio/trace-events +1
-1@@ -2,7 +2,7 @@ # pci.c vfio_intx_interrupt(const char *name, char line) " (%s) Pin %c" -vfio_intx_eoi(const char *name) " (%s) EOI" +vfio_pci_intx_eoi(const char *name) " (%s) EOI" vfio_intx_enable_kvm(const char *name) " (%s) KVM INTx accel enabled" vfio_intx_disable_kvm(const char *name) " (%s) KVM INTx accel disabled" vfio_intx_update(const char *name, int new_irq, int target_irq) " (%s) IRQ moved %d -> %d"
hw/vfio/trace-events vfio_pci_hot_reset(const char *name, const char *type) " (%s) %s" · line 42
Quoted 10 lines
hw/vfio/trace-events +2
-2@@ -35,8 +35,8 @@ vfio_pci_hot_reset(const char *name, const char *type) " (%s) %s" vfio_pci_hot_reset_has_dep_devices(const char *name) "%s: hot reset dependent devices:" vfio_pci_hot_reset_dep_devices(int domain, int bus, int slot, int function, int group_id) "\t%04x:%02x:%02x.%x group %d" vfio_pci_hot_reset_result(const char *name, const char *result) "%s hot reset: %s" -vfio_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx" -vfio_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s" +vfio_pci_populate_device_config(const char *name, unsigned long size, unsigned long offset, unsigned long flags) "Device '%s' config: size: 0x%lx, offset: 0x%lx, flags: 0x%lx" +vfio_pci_populate_device_get_irq_info_failure(const char *errstr) "VFIO_DEVICE_GET_IRQ_INFO failure: %s" vfio_mdev(const char *name, bool is_mdev) " (%s) is_mdev %d" vfio_add_ext_cap_dropped(const char *name, uint16_t cap, uint16_t offset) "%s 0x%x@0x%x" vfio_pci_reset(const char *name) " (%s)"
ATB,
Mark.
Cédric Le Goater
AppliedRe: [PATCH v3 01/23] vfio: export PCI helpers needed for vfio-user
In reply to John LevonQuoted 10 lines · John Levon
The vfio-user code will need to re-use various parts of the vfio PCI code. Export them in
hw/vfio/pci.h, and rename them to the vfio_pci_* namespace.Signed-off-by John Levon <[email protected]>--- hw/vfio/pci.h | 11 ++++++++++ hw/vfio/pci.c | 48 ++++++++++++++++++++++---------------------- hw/vfio/trace-events | 6 +++--- 3 files changed, 38 insertions(+), 27 deletions(-)
Applied to vfio-next.
Thanks,
C.
PATCH v3 2/23 vfio: enable per-IRQ MSI-X masking
In reply to John LevonIf VFIO_IRQ_INFO_MASKABLE is set for VFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.
--- hw/vfio/pci.h | 1 + include/hw/vfio/vfio-device.h | 2 ++ hw/vfio/device.c | 26 ++++++++++++++++++++ hw/vfio/pci.c | 46 ++++++++++++++++++++++++++++++----- 4 files changed, 69 insertions(+), 6 deletions(-)
hw/vfio/pci.h +1 -0
@@ -191,6 +191,7 @@ struct VFIOPCIDevice {bool defer_kvm_irq_routing;bool clear_parent_atomics_on_exit;bool skip_vsc_check; +bool can_mask_msix;VFIODisplay *dpy;Notifier irqchip_change_notifier; };
include/hw/vfio/vfio-device.h +2 -0
@@ -133,7 +133,9 @@ struct VFIODeviceOps { (ret < 0 ? strerror(-ret) : "short write")void vfio_device_irq_disable (VFIODevice *vbasedev,int index); +void vfio_device_irq_unmask_single (VFIODevice *vbasedev,int index,int irq);void vfio_device_irq_unmask (VFIODevice *vbasedev,int index); +void vfio_device_irq_mask_single (VFIODevice *vbasedev,int index,int irq);void vfio_device_irq_mask (VFIODevice *vbasedev,int index);bool vfio_device_irq_set_signaling (VFIODevice *vbasedev,int index,int subindex,int action,int fd,Error **errp);
hw/vfio/device.c +26 -0
@@ -85,6 +85,19 @@ void vfio_device_irq_disable(VFIODevice *vbasedev, int index) vbasedev->io_ops->set_irqs (vbasedev, &irq_set); } +void vfio_device_irq_unmask_single (VFIODevice *vbasedev,int index,int irq) +{ +struct vfio_irq_set irq_set = { + .argsz =sizeof (irq_set), + .flags =VFIO_IRQ_SET_DATA_NONE |VFIO_IRQ_SET_ACTION_UNMASK , + .index = index, + .start = irq, + .count =1 , + }; + + vbasedev->io_ops->set_irqs (vbasedev, &irq_set); +} +void vfio_device_irq_unmask (VFIODevice *vbasedev,int index) {struct vfio_irq_set irq_set = {
@@ -98,6 +111,19 @@ void vfio_device_irq_unmask(VFIODevice *vbasedev, int index) vbasedev->io_ops->set_irqs (vbasedev, &irq_set); } +void vfio_device_irq_mask_single (VFIODevice *vbasedev,int index,int irq) +{ +struct vfio_irq_set irq_set = { + .argsz =sizeof (irq_set), + .flags =VFIO_IRQ_SET_DATA_NONE |VFIO_IRQ_SET_ACTION_MASK , + .index = index, + .start = irq, + .count =1 , + }; + + vbasedev->io_ops->set_irqs (vbasedev, &irq_set); +} +void vfio_device_irq_mask (VFIODevice *vbasedev,int index) {struct vfio_irq_set irq_set = {
hw/vfio/pci.c +40 -6
89 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 02/23] vfio: enable per-IRQ MSI-X masking
Quoted 8 lines · John Levon
If
VFIO_IRQ_INFO_MASKABLEis set forVFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.Originally-by John Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>Signed-off-by John Levon <[email protected]>
Quoted 6 lines
--- hw/vfio/pci.h | 1 + include/hw/vfio/vfio-device.h | 2 ++ hw/vfio/device.c | 26 ++++++++++++++++++++ hw/vfio/pci.c | 46 ++++++++++++++++++++++++++++++----- 4 files changed, 69 insertions(+), 6 deletions(-)
I find these changes difficult to understand. Can you split them a bit more ?
Thanks,
C.
Quoted 171 lines
hw/vfio/pci.h +1
-0@@ -191,6 +191,7 @@ struct VFIOPCIDevice {bool defer_kvm_irq_routing;bool clear_parent_atomics_on_exit;bool skip_vsc_check; +bool can_mask_msix;VFIODisplay *dpy;Notifier irqchip_change_notifier; };include/hw/vfio/vfio-device.h +2
-0@@ -133,7 +133,9 @@ struct VFIODeviceOps { (ret < 0 ? strerror(-ret) : "short write")void vfio_device_irq_disable (VFIODevice *vbasedev,int index); +void vfio_device_irq_unmask_single (VFIODevice *vbasedev,int index,int irq);void vfio_device_irq_unmask (VFIODevice *vbasedev,int index); +void vfio_device_irq_mask_single (VFIODevice *vbasedev,int index,int irq);void vfio_device_irq_mask (VFIODevice *vbasedev,int index);bool vfio_device_irq_set_signaling (VFIODevice *vbasedev,int index,int subindex,int action,int fd,Error **errp);hw/vfio/device.c +26
-0@@ -85,6 +85,19 @@ void vfio_device_irq_disable(VFIODevice *vbasedev, int index) vbasedev->io_ops->set_irqs (vbasedev, &irq_set); } +void vfio_device_irq_unmask_single (VFIODevice *vbasedev,int index,int irq) +{ +struct vfio_irq_set irq_set = { + .argsz =sizeof (irq_set), + .flags =VFIO_IRQ_SET_DATA_NONE |VFIO_IRQ_SET_ACTION_UNMASK , + .index = index, + .start = irq, + .count =1 , + }; + + vbasedev->io_ops->set_irqs (vbasedev, &irq_set); +} +void vfio_device_irq_unmask (VFIODevice *vbasedev,int index) {struct vfio_irq_set irq_set = {@@ -98,6 +111,19 @@ void vfio_device_irq_unmask(VFIODevice *vbasedev, int index) vbasedev->io_ops->set_irqs (vbasedev, &irq_set); } +void vfio_device_irq_mask_single (VFIODevice *vbasedev,int index,int irq) +{ +struct vfio_irq_set irq_set = { + .argsz =sizeof (irq_set), + .flags =VFIO_IRQ_SET_DATA_NONE |VFIO_IRQ_SET_ACTION_MASK , + .index = index, + .start = irq, + .count =1 , + }; + + vbasedev->io_ops->set_irqs (vbasedev, &irq_set); +} +void vfio_device_irq_mask (VFIODevice *vbasedev,int index) {struct vfio_irq_set irq_set = {hw/vfio/pci.c +40
-6@@ -535,6 +535,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, {VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIOMSIVector *vector; +bool new_vec = false;int ret;bool resizing = !!(vdev->nr_vectors < nr +1 );@@ -575,6 +576,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,kvm_irqchip_commit_route_changes (&vfio_route_change);vfio_connect_kvm_msi_virq (vector); } + new_vec = true; } }@@ -584,6 +586,9 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr,* in use, so we shutdown and incrementally increase them as needed. * nr_vectors represents the total number of vectors allocated. * +* Otherwise, unmask the vector if the vector is already setup (and we can +* do so) or send the fd if not. +* * When dynamic allocation is supported, let the host only allocate * and enable a vector when it is in use in guest. nr_vectors represents * the upper bound of vectors being enabled (but not all of the ranges @@ -594,13 +599,20 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, }if (!vdev->defer_kvm_irq_routing) { -if (vdev->msix->noresize && resizing) { -vfio_device_irq_disable (&vdev->vbasedev,VFIO_PCI_MSIX_IRQ_INDEX ); - ret =vfio_enable_vectors (vdev, true); -if (ret) { -error_report ("vfio: failed to enable vectors, %s" , -strerror (-ret)); +if (resizing) { +if (vdev->msix->noresize) { +vfio_device_irq_disable (&vdev->vbasedev, +VFIO_PCI_MSIX_IRQ_INDEX ); + ret =vfio_enable_vectors (vdev, true); +if (ret) { +error_report ("vfio: failed to enable vectors, %d" , ret); + } + }else { +set_irq_signalling (&vdev->vbasedev, vector, nr); } + }else if (vdev->can_mask_msix && !new_vec) { +vfio_device_irq_unmask_single (&vdev->vbasedev, +VFIO_PCI_MSIX_IRQ_INDEX , nr); }else {set_irq_signalling (&vdev->vbasedev, vector, nr); }@@ -630,6 +642,13 @@ static void vfio_msix_vector_release(PCIDevice *pdev, unsigned int nr)trace_vfio_msix_vector_release (vdev->vbasedev.name, nr); +/* just mask vector if peer supports it */ +if (vdev->can_mask_msix) { +vfio_device_irq_mask_single (&vdev->vbasedev,VFIO_PCI_MSIX_IRQ_INDEX , + nr); +return ; + } +/* * There are still old guests that mask and unmask vectors on every * interrupt. If we're using QEMU bypass with a KVM irqfd, leave all of @@ -702,6 +721,13 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)error_report ("vfio: failed to enable vectors, %s" ,strerror (-ret)); } + }else if (vdev->can_mask_msix) { +/* +* If we can use single irq masking, send an invalid fd on vector 0 +* to enable MSI-X without any vectors enabled. +*/ +vfio_device_irq_set_signaling (&vdev->vbasedev,VFIO_PCI_MSIX_IRQ_INDEX , +0 ,VFIO_IRQ_SET_ACTION_TRIGGER ,-1 ,NULL ); }else {/* * Some communication channels between VF & PF or PF & fw rely on the @@ -2842,6 +2868,14 @@ bool vfio_pci_populate_device(VFIOPCIDevice *vdev, Error **errp) } } + ret =vfio_device_get_irq_info (vbasedev,VFIO_PCI_MSIX_IRQ_INDEX , + &irq_info); +if (ret ==0 && (irq_info.flags &VFIO_IRQ_INFO_MASKABLE )) { + vdev->can_mask_msix = true; + }else { + vdev->can_mask_msix = false; + } + ret =vfio_device_get_irq_info (vbasedev,VFIO_PCI_ERR_IRQ_INDEX , &irq_info);if (ret) {/* This can fail for an old kernel or legacy PCI dev */
Re: [PATCH v3 02/23] vfio: enable per-IRQ MSI-X masking
In reply to John LevonQuoted 3 lines · John Levon
If
VFIO_IRQ_INFO_MASKABLEis set forVFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.
I'm just going to drop this patch. Neither vfio nor libvfio-user (including qemu-as-server) report MASKABLE for MSI-X anyway, so it doesn't seem relevant.
I'm not sure if Oracle had some other use case in mind or had previously tested it somehow.
Furthermore, this:
Quoted 4 lines
hw/vfio/pci.c +0
-0@@ -702,6 +721,13 @@ static void vfio_msix_enable(VFIOPCIDevice *vdev)error_report ("vfio: failed to enable vectors, %s" ,strerror (-ret)); }
hw/vfio/pci.c static void vfio_msix_enable(VFIOPCIDevice *vdev) · line 733
Quoted 10 lines
hw/vfio/pci.c +7
-0+ }else if (vdev->can_mask_msix) { +/* +* If we can use single irq masking, send an invalid fd on vector 0 +* to enable MSI-X without any vectors enabled. +*/ +vfio_device_irq_set_signaling (&vdev->vbasedev,VFIO_PCI_MSIX_IRQ_INDEX , +0 ,VFIO_IRQ_SET_ACTION_TRIGGER ,-1 ,NULL ); }else {/* * Some communication channels between VF & PF or PF & fw rely on the
Seems odd as it doesn't pass DATA_EVENTFD, unlike the vfio_enable_msix_no_vec() below it; I have no idea why the difference or if it makes sense, but it doesn't seem so.
regards
john
Re: [PATCH v3 02/23] vfio: enable per-IRQ MSI-X masking
Quoted 8 lines · John Levon
Quoted 3 lines · John Levon
If
VFIO_IRQ_INFO_MASKABLEis set forVFIO_PCI_MSIX_IRQ_INDEX, record this in ->can_mask_msix, and use it to individually mask MSI-X interrupts as needed.I'm just going to drop this patch. Neither vfio nor libvfio-user (including qemu-as-server) report MASKABLE for MSI-X anyway, so it doesn't seem relevant.
ok. Dropped.
Thanks,
C.
PATCH v3 3/23 vfio: add per-region fd support
In reply to John LevonFor vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this, and use the correct fd in vfio_region_mmap().
--- include/hw/vfio/vfio-device.h | 7 +++++-- hw/vfio/device.c | 29 +++++++++++++++++++++++++---- hw/vfio/region.c | 9 +++++++-- 3 files changed, 37 insertions(+), 8 deletions(-)
include/hw/vfio/vfio-device.h +5 -2
@@ -66,6 +66,7 @@ typedef struct VFIODevice {OnOffAuto enable_migration;OnOffAuto migration_multifd_transfer;bool migration_events; +bool use_region_fds;VFIODeviceOps *ops;VFIODeviceIOOps *io_ops;unsigned int num_irqs;
@@ -84,6 +85,7 @@ typedef struct VFIODevice {VFIOIOASHwpt *hwpt;QLIST_ENTRY (VFIODevice) hwpt_next;struct vfio_region_info **reginfo; +int *region_fds; } VFIODevice;struct VFIODeviceOps {
@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {/** * @get_region_info * -* Fill in @info with information on the region given by @info->index. +* Fill in @info (and optionally @fd) with information on the region given +* by @info->index. */ int (*get_region_info)(VFIODevice *vdev, -struct vfio_region_info *info); +struct vfio_region_info *info,int *fd);/** * @get_irq_info
hw/vfio/device.c +25 -4
@@ -226,6 +226,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, struct vfio_region_info **info) {size_t argsz =sizeof (struct vfio_region_info ); +int fd =-1 ;int ret;/* check cache */
@@ -240,7 +241,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, retry: (*info)->argsz = argsz; - ret = vbasedev->io_ops->get_region_info (vbasedev, *info); + ret = vbasedev->io_ops->get_region_info (vbasedev, *info, &fd);if (ret !=0 ) {g_free (*info); *info =NULL ;
@@ -251,11 +252,19 @@ retry: argsz = (*info)->argsz; *info =g_realloc (*info, argsz); +if (fd !=-1 ) { +close (fd); + fd =-1 ; + } + goto retry; }/* fill cache */ vbasedev->reginfo[index] = *info; +if (vbasedev->region_fds !=NULL ) { + vbasedev->region_fds[index] = fd; + }return 0 ; }
@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, vbasedev->io_ops = &vfio_device_io_ops_ioctl; vbasedev->dev = dev; vbasedev->fd =-1 ; + vbasedev->use_region_fds = false; vbasedev->ram_block_discard_allowed = ram_discard; }
@@ -470,6 +480,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer, vbasedev->reginfo = g_new0(struct vfio_region_info *, vbasedev->num_regions); +if (vbasedev->use_region_fds) { + vbasedev->region_fds =g_new0 (int, vbasedev->num_regions); + } }void vfio_device_unprepare (VFIODevice *vbasedev)
@@ -478,9 +491,14 @@ void vfio_device_unprepare(VFIODevice *vbasedev)for (i =0 ; i < vbasedev->num_regions; i++) {g_free (vbasedev->reginfo[i]); +if (vbasedev->region_fds !=NULL && vbasedev->region_fds[i] !=-1 ) { +close (vbasedev->region_fds[i]); + } + } -g_free (vbasedev->reginfo); - vbasedev->reginfo =NULL ; + +g_clear_pointer (&vbasedev->reginfo, g_free); +g_clear_pointer (&vbasedev->region_fds, g_free);QLIST_REMOVE (vbasedev, container_next);QLIST_REMOVE (vbasedev, global_next);
@@ -502,10 +520,13 @@ static int vfio_device_io_device_feature(VFIODevice *vbasedev, }static int vfio_device_io_get_region_info (VFIODevice *vbasedev, -struct vfio_region_info *info) +struct vfio_region_info *info, +int *fd) {int ret; + *fd =-1 ; + ret =ioctl (vbasedev->fd,VFIO_DEVICE_GET_REGION_INFO , info);return ret <0 ? -errno : ret;
hw/vfio/region.c +7 -2
@@ -241,6 +241,7 @@ int vfio_region_mmap(VFIORegion *region) {int i, ret, prot =0 ;char *name; +int fd;if (!region->mem) {return 0 ;
@@ -271,14 +272,18 @@ int vfio_region_mmap(VFIORegion *region) goto no_mmap; } +/* Use the per-region fd if set, or the shared fd. */ + fd = region->vbasedev->region_fds ? + region->vbasedev->region_fds[region->nr] : + region->vbasedev->fd, + map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align);munmap (map_base, map_align - map_base);munmap (map_align + region->mmaps[i].size, align - (map_align - map_base)); region->mmaps[i].mmap =mmap (map_align, region->mmaps[i].size, prot, -MAP_SHARED |MAP_FIXED , - region->vbasedev->fd, +MAP_SHARED |MAP_FIXED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) {
Signature · 1 line
2.43.0
Re: [PATCH v3 03/23] vfio: add per-region fd support
Quoted 32 lines · John Levon
For vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this, and use the correct fd in
vfio_region_mmap().Signed-off-by John Levon <[email protected]>--- include/hw/vfio/vfio-device.h | 7 +++++-- hw/vfio/device.c | 29 +++++++++++++++++++++++++---- hw/vfio/region.c | 9 +++++++-- 3 files changed, 37 insertions(+), 8 deletions(-)include/hw/vfio/vfio-device.h +2
-0@@ -66,6 +66,7 @@ typedef struct VFIODevice {OnOffAuto enable_migration;OnOffAuto migration_multifd_transfer;bool migration_events; +bool use_region_fds;VFIODeviceOps *ops;VFIODeviceIOOps *io_ops;unsigned int num_irqs;@@ -84,6 +85,7 @@ typedef struct VFIODevice {VFIOIOASHwpt *hwpt;QLIST_ENTRY (VFIODevice) hwpt_next;struct vfio_region_info **reginfo; +int *region_fds; } VFIODevice;struct VFIODeviceOps {
include/hw/vfio/vfio-device.h struct VFIODeviceIOOps { · line 179
Quoted 7 lines
include/hw/vfio/vfio-device.h +2
-1@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {/** * @get_region_info * -* Fill in @info with information on the region given by @info->index. +* Fill in @info (and optionally @fd) with information on the region given +* by @info->index. */
Could you please update the whole documentation of the VFIODeviceIOOps struct and document each parameter ?
Quoted 48 lines
include/hw/vfio/vfio-device.h +1
-1int (*get_region_info)(VFIODevice *vdev, -struct vfio_region_info *info); +struct vfio_region_info *info,int *fd);/** * @get_irq_info hw/vfio/device.c +10
-1@@ -226,6 +226,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, struct vfio_region_info **info) {size_t argsz =sizeof (struct vfio_region_info ); +int fd =-1 ;int ret;/* check cache */ @@ -240,7 +241,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, retry: (*info)->argsz = argsz; - ret = vbasedev->io_ops->get_region_info (vbasedev, *info); + ret = vbasedev->io_ops->get_region_info (vbasedev, *info, &fd);if (ret !=0 ) {g_free (*info); *info =NULL ;@@ -251,11 +252,19 @@ retry: argsz = (*info)->argsz; *info =g_realloc (*info, argsz); +if (fd !=-1 ) { +close (fd); + fd =-1 ; + } + goto retry; }/* fill cache */ vbasedev->reginfo[index] = *info; +if (vbasedev->region_fds !=NULL ) { + vbasedev->region_fds[index] = fd; + }return 0 ; }
hw/vfio/device.c void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, · line 372
Quoted 4 lines
hw/vfio/device.c +1
-0@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, vbasedev->io_ops = &vfio_device_io_ops_ioctl; vbasedev->dev = dev; vbasedev->fd =-1 ; + vbasedev->use_region_fds = false;
why not extend vfio_device_init() with a 'region_fd_cache' bool parameter instead ? and avoid the extra VFIODevice attribute.
Quoted 14 lines
hw/vfio/device.c +3
-0vbasedev->ram_block_discard_allowed = ram_discard; }@@ -470,6 +480,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer, vbasedev->reginfo = g_new0(struct vfio_region_info *, vbasedev->num_regions); +if (vbasedev->use_region_fds) { + vbasedev->region_fds =g_new0 (int, vbasedev->num_regions); + } }void vfio_device_unprepare (VFIODevice *vbasedev)
hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 497
Quoted 7 lines
hw/vfio/device.c +4
-0@@ -478,9 +491,14 @@ void vfio_device_unprepare(VFIODevice *vbasedev)for (i =0 ; i < vbasedev->num_regions; i++) {g_free (vbasedev->reginfo[i]); +if (vbasedev->region_fds !=NULL && vbasedev->region_fds[i] !=-1 ) { +close (vbasedev->region_fds[i]); + } +
Adding a small helper to retrieve the region fd from the cache, would hide some of the implementation details here and below in vfio_region_mmap()
hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 500
Quoted 5 lines
hw/vfio/device.c +2
-2} -g_free (vbasedev->reginfo); - vbasedev->reginfo =NULL ; + +g_clear_pointer (&vbasedev->reginfo, g_free);
unrelated change.
Thanks,
C.
Quoted 52 lines
hw/vfio/device.c +5
-1+g_clear_pointer (&vbasedev->region_fds, g_free);QLIST_REMOVE (vbasedev, container_next);QLIST_REMOVE (vbasedev, global_next);@@ -502,10 +520,13 @@ static int vfio_device_io_device_feature(VFIODevice *vbasedev, }static int vfio_device_io_get_region_info (VFIODevice *vbasedev, -struct vfio_region_info *info) +struct vfio_region_info *info, +int *fd) {int ret; + *fd =-1 ; + ret =ioctl (vbasedev->fd,VFIO_DEVICE_GET_REGION_INFO , info);return ret <0 ? -errno : ret;hw/vfio/region.c +7
-2@@ -241,6 +241,7 @@ int vfio_region_mmap(VFIORegion *region) {int i, ret, prot =0 ;char *name; +int fd;if (!region->mem) {return 0 ;@@ -271,14 +272,18 @@ int vfio_region_mmap(VFIORegion *region) goto no_mmap; } +/* Use the per-region fd if set, or the shared fd. */ + fd = region->vbasedev->region_fds ? + region->vbasedev->region_fds[region->nr] : + region->vbasedev->fd, + map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align);munmap (map_base, map_align - map_base);munmap (map_align + region->mmaps[i].size, align - (map_align - map_base)); region->mmaps[i].mmap =mmap (map_align, region->mmaps[i].size, prot, -MAP_SHARED |MAP_FIXED , - region->vbasedev->fd, +MAP_SHARED |MAP_FIXED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) {
Re: [PATCH v3 03/23] vfio: add per-region fd support
Quoted 5 lines · Cédric Le Goater
Quoted 4 lines · John Levon
For vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this, and use the correct fd in
vfio_region_mmap().
Quoted 11 lines
include/hw/vfio/vfio-device.h struct VFIODeviceIOOps { · line 179
Quoted 8 lines
include/hw/vfio/vfio-device.h +2
-1@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {/** * @get_region_info * -* Fill in @info with information on the region given by @info->index. +* Fill in @info (and optionally @fd) with information on the region given +* by @info->index. */ Could you please update the whole documentation of the VFIODeviceIOOps struct and document each parameter ?
Sorry, not sure what you're asking for. This struct was fully documented in 38bf025d ("vfio: add device IO ops vector")
and its subsequent patches.
Quoted 8 lines
hw/vfio/device.c void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, · line 372
Quoted 5 lines
hw/vfio/device.c +1
-0@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, vbasedev->io_ops = &vfio_device_io_ops_ioctl; vbasedev->dev = dev; vbasedev->fd =-1 ; + vbasedev->use_region_fds = false;why not extend
vfio_device_init()with a 'region_fd_cache' bool parameter instead ?
I could do, but you previously asked me not to add an "io_ops" parameter to this function and instead directly change it here - why is this parameter different?
Quoted 1 line
and avoid the extra VFIODevice attribute.
I don't get this - we still need to record the boolean in the vbasedev.
regards
john
Re: [PATCH v3 03/23] vfio: add per-region fd support
Quoted 19 lines · John Levon
Quoted 5 lines · Cédric Le Goater
Quoted 4 lines · John Levon
For vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this, and use the correct fd in
vfio_region_mmap().Quoted 11 lines
include/hw/vfio/vfio-device.h struct VFIODeviceIOOps { · line 179
Quoted 8 lines
include/hw/vfio/vfio-device.h +2
-1@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {/** * @get_region_info * -* Fill in @info with information on the region given by @info->index. +* Fill in @info (and optionally @fd) with information on the region given +* by @info->index. */ Could you please update the whole documentation of the VFIODeviceIOOps struct and document each parameter ?
Quoted 3 lines
Sorry, not sure what you're asking for. This struct was fully documented in
38bf025d("vfio: add device IO ops vector")
and its subsequent patches.
yes. See the formatting of VFIODeviceOps :
/**
* @vfio_save_config
*
* Save device config state
*
* @vdev: #VFIODevice for which to save the config
* @f: #QEMUFile where to send the data
* @errp: pointer to Error*, to store an error if it happens. *
* Returns zero to indicate success and negative for error */
int (*vfio_save_config)(VFIODevice *vdev, QEMUFile *f, Error **errp);
No big deal. It can come later.
Quoted 11 lines
Quoted 8 lines
hw/vfio/device.c void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, · line 372
Quoted 5 lines
hw/vfio/device.c +1
-0@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, vbasedev->io_ops = &vfio_device_io_ops_ioctl; vbasedev->dev = dev; vbasedev->fd =-1 ; + vbasedev->use_region_fds = false;why not extend
vfio_device_init()with a 'region_fd_cache' bool parameter instead ?I could do, but you previously asked me not to add an "
io_ops" parameter to this function and instead directly change it here - why is this parameter different?
Because I thought we could allocate vbasedev->region_fds[] directly under vfio_device_init().
Quoted 3 lines
Quoted 1 line
and avoid the extra VFIODevice attribute.
I don't get this - we still need to record the boolean in the vbasedev.
but we can't because we don't have the vbasedev->num_regions value yet. So forget this proposal.
That said, there are a few contortions around the instance_init() handler, vfio_device_init() and the realize() handler that I find confusing. I will see if it can be improved when the code is stable again.
Thanks,
C.
Re: [PATCH v3 03/23] vfio: add per-region fd support
In reply to Cédric Le GoaterQuoted 11 lines · Cédric Le Goater
hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 496
Quoted 7 lines
hw/vfio/device.c +4
-0@@ -478,9 +491,14 @@ void vfio_device_unprepare(VFIODevice *vbasedev)for (i =0 ; i < vbasedev->num_regions; i++) {g_free (vbasedev->reginfo[i]); +if (vbasedev->region_fds !=NULL && vbasedev->region_fds[i] !=-1 ) { +close (vbasedev->region_fds[i]); + } +Adding a small helper to retrieve the region fd from the cache, would hide some of the implementation details here and below in
vfio_region_mmap()
I can add a region_get_fd() helper, but I can't use it here - we should only close the fd if it's a per-region fd. I'll make this call a region_cleanup() or similar.
regards
john
Re: [PATCH v3 03/23] vfio: add per-region fd support
In reply to Cédric Le GoaterQuoted 10 lines · Cédric Le Goater
hw/vfio/device.c void vfio_device_unprepare(VFIODevice *vbasedev) · line 495
Quoted 6 lines
hw/vfio/device.c +3
-0@@ -478,9 +491,14 @@ void vfio_device_unprepare(VFIODevice *vbasedev)for (i =0 ; i < vbasedev->num_regions; i++) {g_free (vbasedev->reginfo[i]); +if (vbasedev->region_fds !=NULL && vbasedev->region_fds[i] !=-1 ) { +close (vbasedev->region_fds[i]); + }Adding a small helper to retrieve the region fd from the cache, would hide some of the implementation details here and below in
vfio_region_mmap()
I've added vfio_device_get_region_fd() - used by vfio_region_mmap(); now the existence of ->region_fds is private to device.c, so I think that's better.
regards
john
Re: [PATCH v3 03/23] vfio: add per-region fd support
In reply to John LevonQuoted 149 lines · John Levon
For vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this, and use the correct fd in
vfio_region_mmap().Signed-off-by John Levon <[email protected]>--- include/hw/vfio/vfio-device.h | 7 +++++-- hw/vfio/device.c | 29 +++++++++++++++++++++++++---- hw/vfio/region.c | 9 +++++++-- 3 files changed, 37 insertions(+), 8 deletions(-)include/hw/vfio/vfio-device.h +5
-2@@ -66,6 +66,7 @@ typedef struct VFIODevice {OnOffAuto enable_migration;OnOffAuto migration_multifd_transfer;bool migration_events; +bool use_region_fds;VFIODeviceOps *ops;VFIODeviceIOOps *io_ops;unsigned int num_irqs;@@ -84,6 +85,7 @@ typedef struct VFIODevice {VFIOIOASHwpt *hwpt;QLIST_ENTRY (VFIODevice) hwpt_next;struct vfio_region_info **reginfo; +int *region_fds; } VFIODevice;struct VFIODeviceOps {@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {/** * @get_region_info * -* Fill in @info with information on the region given by @info->index. +* Fill in @info (and optionally @fd) with information on the region given +* by @info->index. */ int (*get_region_info)(VFIODevice *vdev, -struct vfio_region_info *info); +struct vfio_region_info *info,int *fd);/** * @get_irq_info hw/vfio/device.c +25
-4@@ -226,6 +226,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, struct vfio_region_info **info) {size_t argsz =sizeof (struct vfio_region_info ); +int fd =-1 ;int ret;/* check cache */ @@ -240,7 +241,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, retry: (*info)->argsz = argsz; - ret = vbasedev->io_ops->get_region_info (vbasedev, *info); + ret = vbasedev->io_ops->get_region_info (vbasedev, *info, &fd);if (ret !=0 ) {g_free (*info); *info =NULL ;@@ -251,11 +252,19 @@ retry: argsz = (*info)->argsz; *info =g_realloc (*info, argsz); +if (fd !=-1 ) { +close (fd); + fd =-1 ; + } + goto retry; }/* fill cache */ vbasedev->reginfo[index] = *info; +if (vbasedev->region_fds !=NULL ) { + vbasedev->region_fds[index] = fd; + }return 0 ; }@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, vbasedev->io_ops = &vfio_device_io_ops_ioctl; vbasedev->dev = dev; vbasedev->fd =-1 ; + vbasedev->use_region_fds = false; vbasedev->ram_block_discard_allowed = ram_discard; }@@ -470,6 +480,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer, vbasedev->reginfo = g_new0(struct vfio_region_info *, vbasedev->num_regions); +if (vbasedev->use_region_fds) { + vbasedev->region_fds =g_new0 (int, vbasedev->num_regions); + } }void vfio_device_unprepare (VFIODevice *vbasedev)@@ -478,9 +491,14 @@ void vfio_device_unprepare(VFIODevice *vbasedev)for (i =0 ; i < vbasedev->num_regions; i++) {g_free (vbasedev->reginfo[i]); +if (vbasedev->region_fds !=NULL && vbasedev->region_fds[i] !=-1 ) { +close (vbasedev->region_fds[i]); + } + } -g_free (vbasedev->reginfo); - vbasedev->reginfo =NULL ; + +g_clear_pointer (&vbasedev->reginfo, g_free); +g_clear_pointer (&vbasedev->region_fds, g_free);QLIST_REMOVE (vbasedev, container_next);QLIST_REMOVE (vbasedev, global_next);@@ -502,10 +520,13 @@ static int vfio_device_io_device_feature(VFIODevice *vbasedev, }static int vfio_device_io_get_region_info (VFIODevice *vbasedev, -struct vfio_region_info *info) +struct vfio_region_info *info, +int *fd) {int ret; + *fd =-1 ; + ret =ioctl (vbasedev->fd,VFIO_DEVICE_GET_REGION_INFO , info);return ret <0 ? -errno : ret;hw/vfio/region.c +1
-0@@ -241,6 +241,7 @@ int vfio_region_mmap(VFIORegion *region) {int i, ret, prot =0 ;char *name; +int fd;if (!region->mem) {return 0 ;
hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 279
Quoted 8 lines
hw/vfio/region.c +5
-0@@ -271,14 +272,18 @@ int vfio_region_mmap(VFIORegion *region) goto no_mmap; } +/* Use the per-region fd if set, or the shared fd. */ + fd = region->vbasedev->region_fds ? + region->vbasedev->region_fds[region->nr] : + region->vbasedev->fd, +
This feels a bit odd: if you're asking to map a VFIORegion then shouldn't that contain all the information required for the mapping, including the fd?
(goes and looks)
It appears that there is already a fd field in VFIORegion so is there a reason why we can't use this instead of adding region_fds to vbasedev?
hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 289
Quoted 12 lines
hw/vfio/region.c +1
-2map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align);munmap (map_base, map_align - map_base);munmap (map_align + region->mmaps[i].size, align - (map_align - map_base)); region->mmaps[i].mmap =mmap (map_align, region->mmaps[i].size, prot, -MAP_SHARED |MAP_FIXED , - region->vbasedev->fd, +MAP_SHARED |MAP_FIXED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) {
ATB,
Mark.
Re: [PATCH v3 03/23] vfio: add per-region fd support
Quoted 6 lines · Mark Cave-Ayland
hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 279
Quoted 5 lines
hw/vfio/region.c +5
-0+/* Use the per-region fd if set, or the shared fd. */ + fd = region->vbasedev->region_fds ? + region->vbasedev->region_fds[region->nr] : + region->vbasedev->fd, +
Quoted 7 lines
This feels a bit odd: if you're asking to map a VFIORegion then shouldn't that contain all the information required for the mapping, including the fd?
(goes and looks)
It appears that there is already a fd field in VFIORegion so is there a reason why we can't use this instead of adding
region_fdsto vbasedev?
region->fd was in earlier patchsets, but dropped as requested by Cédric. So we must look it up here.
regards
john
Re: [PATCH v3 03/23] vfio: add per-region fd support
Quoted 11 lines · John Levon
Quoted 9 lines · Mark Cave-Ayland
hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 279
Quoted 5 lines
hw/vfio/region.c +5
-0+/* Use the per-region fd if set, or the shared fd. */ + fd = region->vbasedev->region_fds ? + region->vbasedev->region_fds[region->nr] : + region->vbasedev->fd, +This feels a bit odd: if you're asking to map a VFIORegion then shouldn't that contain all the information required for the mapping, including the fd?
Quoted 7 lines
Quoted 4 lines
(goes and looks)
It appears that there is already a fd field in VFIORegion so is there a reason why we can't use this instead of adding
region_fdsto vbasedev?
region->fdwas in earlier patchsets, but dropped as requested by Cédric. So we must look it up here.
yes. It didn't seem necessary to store the same information under multiple structs.
C.
Re: [PATCH v3 03/23] vfio: add per-region fd support
Quoted 14 lines · Cédric Le Goater
Quoted 13 lines · John Levon
Quoted 11 lines · Mark Cave-Ayland
hw/vfio/region.c int vfio_region_mmap(VFIORegion *region) · line 279
Quoted 5 lines
hw/vfio/region.c +5
-0+/* Use the per-region fd if set, or the shared fd. */ + fd = region->vbasedev->region_fds ? + region->vbasedev->region_fds[region->nr] : + region->vbasedev->fd, +This feels a bit odd: if you're asking to map a VFIORegion then shouldn't that contain all the information required for the mapping, including the fd?
(goes and looks)
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
It appears that there is already a fd field in VFIORegion so is there a reason why we can't use this instead of adding
region_fdsto vbasedev?
region->fdwas in earlier patchsets, but dropped as requested by Cédric. So we must look it up here.yes. It didn't seem necessary to store the same information under multiple structs.
And the reason we have ->region_fds at all rather than just storing directly in region->fd is that vfio_device_get_region_info() doesn't take a VFIORegion argument, as it's used in other contexts too.
regards
john
Re: [PATCH v3 03/23] vfio: add per-region fd support
In reply to John LevonQuoted 5 lines · John Levon
For vfio-user, each region has its own fd rather than sharing vbasedev's. Add the necessary plumbing to support this, and use the correct fd in
vfio_region_mmap().Signed-off-by John Levon <[email protected]>
Applied to vfio-next.
Thanks,
C.
Quoted 164 lines
--- include/hw/vfio/vfio-device.h | 7 +++++-- hw/vfio/device.c | 29 +++++++++++++++++++++++++---- hw/vfio/region.c | 9 +++++++-- 3 files changed, 37 insertions(+), 8 deletions(-)include/hw/vfio/vfio-device.h +5
-2@@ -66,6 +66,7 @@ typedef struct VFIODevice {OnOffAuto enable_migration;OnOffAuto migration_multifd_transfer;bool migration_events; +bool use_region_fds;VFIODeviceOps *ops;VFIODeviceIOOps *io_ops;unsigned int num_irqs;@@ -84,6 +85,7 @@ typedef struct VFIODevice {VFIOIOASHwpt *hwpt;QLIST_ENTRY (VFIODevice) hwpt_next;struct vfio_region_info **reginfo; +int *region_fds; } VFIODevice;struct VFIODeviceOps {@@ -172,10 +174,11 @@ struct VFIODeviceIOOps {/** * @get_region_info * -* Fill in @info with information on the region given by @info->index. +* Fill in @info (and optionally @fd) with information on the region given +* by @info->index. */ int (*get_region_info)(VFIODevice *vdev, -struct vfio_region_info *info); +struct vfio_region_info *info,int *fd);/** * @get_irq_info hw/vfio/device.c +25
-4@@ -226,6 +226,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, struct vfio_region_info **info) {size_t argsz =sizeof (struct vfio_region_info ); +int fd =-1 ;int ret;/* check cache */ @@ -240,7 +241,7 @@ int vfio_device_get_region_info(VFIODevice *vbasedev, int index, retry: (*info)->argsz = argsz; - ret = vbasedev->io_ops->get_region_info (vbasedev, *info); + ret = vbasedev->io_ops->get_region_info (vbasedev, *info, &fd);if (ret !=0 ) {g_free (*info); *info =NULL ;@@ -251,11 +252,19 @@ retry: argsz = (*info)->argsz; *info =g_realloc (*info, argsz); +if (fd !=-1 ) { +close (fd); + fd =-1 ; + } + goto retry; }/* fill cache */ vbasedev->reginfo[index] = *info; +if (vbasedev->region_fds !=NULL ) { + vbasedev->region_fds[index] = fd; + }return 0 ; }@@ -360,6 +369,7 @@ void vfio_device_init(VFIODevice *vbasedev, int type, VFIODeviceOps *ops, vbasedev->io_ops = &vfio_device_io_ops_ioctl; vbasedev->dev = dev; vbasedev->fd =-1 ; + vbasedev->use_region_fds = false; vbasedev->ram_block_discard_allowed = ram_discard; }@@ -470,6 +480,9 @@ void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer, vbasedev->reginfo = g_new0(struct vfio_region_info *, vbasedev->num_regions); +if (vbasedev->use_region_fds) { + vbasedev->region_fds =g_new0 (int, vbasedev->num_regions); + } }void vfio_device_unprepare (VFIODevice *vbasedev)@@ -478,9 +491,14 @@ void vfio_device_unprepare(VFIODevice *vbasedev)for (i =0 ; i < vbasedev->num_regions; i++) {g_free (vbasedev->reginfo[i]); +if (vbasedev->region_fds !=NULL && vbasedev->region_fds[i] !=-1 ) { +close (vbasedev->region_fds[i]); + } + } -g_free (vbasedev->reginfo); - vbasedev->reginfo =NULL ; + +g_clear_pointer (&vbasedev->reginfo, g_free); +g_clear_pointer (&vbasedev->region_fds, g_free);QLIST_REMOVE (vbasedev, container_next);QLIST_REMOVE (vbasedev, global_next);@@ -502,10 +520,13 @@ static int vfio_device_io_device_feature(VFIODevice *vbasedev, }static int vfio_device_io_get_region_info (VFIODevice *vbasedev, -struct vfio_region_info *info) +struct vfio_region_info *info, +int *fd) {int ret; + *fd =-1 ; + ret =ioctl (vbasedev->fd,VFIO_DEVICE_GET_REGION_INFO , info);return ret <0 ? -errno : ret;hw/vfio/region.c +7
-2@@ -241,6 +241,7 @@ int vfio_region_mmap(VFIORegion *region) {int i, ret, prot =0 ;char *name; +int fd;if (!region->mem) {return 0 ;@@ -271,14 +272,18 @@ int vfio_region_mmap(VFIORegion *region) goto no_mmap; } +/* Use the per-region fd if set, or the shared fd. */ + fd = region->vbasedev->region_fds ? + region->vbasedev->region_fds[region->nr] : + region->vbasedev->fd, + map_align = (void *)ROUND_UP ((uintptr_t )map_base, (uintptr_t )align);munmap (map_base, map_align - map_base);munmap (map_align + region->mmaps[i].size, align - (map_align - map_base)); region->mmaps[i].mmap =mmap (map_align, region->mmaps[i].size, prot, -MAP_SHARED |MAP_FIXED , - region->vbasedev->fd, +MAP_SHARED |MAP_FIXED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) {
PATCH v3 4/23 vfio: mark posted writes in region write callbacks
In reply to John LevonFor vfio-user, the region write implementation needs to know if the write is posted; add the necessary plumbing to support this.
--- include/hw/vfio/vfio-device.h | 4 ++-- include/hw/vfio/vfio-region.h | 1 + hw/vfio/device.c | 3 ++- hw/vfio/pci.c | 5 ++++- hw/vfio/region.c | 3 ++- 5 files changed, 11 insertions(+), 5 deletions(-)
include/hw/vfio/vfio-device.h +2 -2
@@ -207,10 +207,10 @@ struct VFIODeviceIOOps {* @region_write * * Write @size bytes to the region @nr at offset @off from the buffer -* @data. +* @data; if @post, the write is posted. */ int (*region_write)(VFIODevice *vdev,uint8_t nr,off_t off,uint32_t size, -void *data); +void *data,bool post); }; void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,
include/hw/vfio/vfio-region.h +1 -0
@@ -29,6 +29,7 @@ typedef struct VFIORegion {uint32_t nr_mmaps;VFIOMmap *mmaps;uint8_t nr;/* cache the region number for debug */ +bool post_wr;/* writes can be posted */ } VFIORegion;
hw/vfio/device.c +2 -1
@@ -569,7 +569,8 @@ static int vfio_device_io_region_read(VFIODevice *vbasedev, uint8_t index, }static int vfio_device_io_region_write (VFIODevice *vbasedev,uint8_t index, -off_t off,uint32_t size,void *data) +off_t off,uint32_t size,void *data, +bool post) {struct vfio_region_info *info;int ret;
hw/vfio/pci.c +4 -1
@@ -1015,7 +1015,7 @@ static int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset, {return vdev->vbasedev.io_ops->region_write (&vdev->vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , - offset, size, data); + offset, size, data, false); }static uint64_t vfio_rom_read (void *opaque,hwaddr addr,unsigned size)
@@ -1819,6 +1819,9 @@ static void vfio_bar_prepare(VFIOPCIDevice *vdev, int nr) bar->type = pci_bar & (bar->ioport ? ~PCI_BASE_ADDRESS_IO_MASK : ~PCI_BASE_ADDRESS_MEM_MASK ); bar->size = bar->region.size; + +/* IO regions are sync, memory can be async */ + bar->region.post_wr = (bar->ioport ==0 ); }static void vfio_bars_prepare (VFIOPCIDevice *vdev)
hw/vfio/region.c +2 -1
@@ -66,7 +66,7 @@ void vfio_region_write(void *opaque, hwaddr addr, } ret = vbasedev->io_ops->region_write (vbasedev, region->nr, - addr, size, &buf); + addr, size, &buf, region->post_wr);if (ret != size) { error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64 ",%d) failed: %s",
@@ -200,6 +200,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->size = info->size; region->fd_offset = info->offset; region->nr = index; + region->post_wr = false;if (region->size) { region->mem =g_new0 (MemoryRegion,1 );
Signature · 1 line
2.43.0
Cédric Le Goater
AppliedRe: [PATCH v3 04/23] vfio: mark posted writes in region write callbacks
Quoted 5 lines · John Levon
For vfio-user, the region write implementation needs to know if the write is posted; add the necessary plumbing to support this.
Signed-off-by John Levon <[email protected]>Reviewed-by Cédric Le Goater <[email protected]>
Applied to vfio-next.
Thanks,
C.
Quoted 95 lines
--- include/hw/vfio/vfio-device.h | 4 ++-- include/hw/vfio/vfio-region.h | 1 + hw/vfio/device.c | 3 ++- hw/vfio/pci.c | 5 ++++- hw/vfio/region.c | 3 ++- 5 files changed, 11 insertions(+), 5 deletions(-)include/hw/vfio/vfio-device.h +2
-2@@ -207,10 +207,10 @@ struct VFIODeviceIOOps {* @region_write * * Write @size bytes to the region @nr at offset @off from the buffer -* @data. +* @data; if @post, the write is posted. */ int (*region_write)(VFIODevice *vdev,uint8_t nr,off_t off,uint32_t size, -void *data); +void *data,bool post); }; void vfio_device_prepare(VFIODevice *vbasedev, VFIOContainerBase *bcontainer,include/hw/vfio/vfio-region.h +1
-0@@ -29,6 +29,7 @@ typedef struct VFIORegion {uint32_t nr_mmaps;VFIOMmap *mmaps;uint8_t nr;/* cache the region number for debug */ +bool post_wr;/* writes can be posted */ } VFIORegion;hw/vfio/device.c +2
-1@@ -569,7 +569,8 @@ static int vfio_device_io_region_read(VFIODevice *vbasedev, uint8_t index, }static int vfio_device_io_region_write (VFIODevice *vbasedev,uint8_t index, -off_t off,uint32_t size,void *data) +off_t off,uint32_t size,void *data, +bool post) {struct vfio_region_info *info;int ret;hw/vfio/pci.c +4
-1@@ -1015,7 +1015,7 @@ static int vfio_pci_config_space_write(VFIOPCIDevice *vdev, off_t offset, {return vdev->vbasedev.io_ops->region_write (&vdev->vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , - offset, size, data); + offset, size, data, false); }static uint64_t vfio_rom_read (void *opaque,hwaddr addr,unsigned size)@@ -1819,6 +1819,9 @@ static void vfio_bar_prepare(VFIOPCIDevice *vdev, int nr) bar->type = pci_bar & (bar->ioport ? ~PCI_BASE_ADDRESS_IO_MASK : ~PCI_BASE_ADDRESS_MEM_MASK ); bar->size = bar->region.size; + +/* IO regions are sync, memory can be async */ + bar->region.post_wr = (bar->ioport ==0 ); }static void vfio_bars_prepare (VFIOPCIDevice *vdev)hw/vfio/region.c +2
-1@@ -66,7 +66,7 @@ void vfio_region_write(void *opaque, hwaddr addr, } ret = vbasedev->io_ops->region_write (vbasedev, region->nr, - addr, size, &buf); + addr, size, &buf, region->post_wr);if (ret != size) { error_report("%s(%s:region%d+0x%"HWADDR_PRIx", 0x%"PRIx64 ",%d) failed: %s",@@ -200,6 +200,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->size = info->size; region->fd_offset = info->offset; region->nr = index; + region->post_wr = false;if (region->size) { region->mem =g_new0 (MemoryRegion,1 );
PATCH v3 5/23 vfio-user: introduce vfio-user protocol specification
In reply to John LevonFrom: Thanos Makatos <[email protected]>
This patch introduces the vfio-user protocol specification (formerly known as VFIO-over-socket), which is designed to allow devices to be emulated outside QEMU, in a separate process. vfio-user reuses the existing VFIO defines, structs and concepts.
It has been earlier discussed as an RFC in:
"RFC: use VFIO over a UNIX domain socket to implement device offloading"
--- MAINTAINERS | 8 +- docs/devel/index-internals.rst | 1 + docs/devel/vfio-user.rst | 1522 ++++++++++++++++++++++++++++++++ 3 files changed, 1530 insertions(+), 1 deletion(-) create mode 100644 docs/devel/vfio-user.rst
MAINTAINERS +7 -1
@@ -4241,13 +4241,19 @@ F: hw/remote/proxy-memory-listener.c F: include/hw/remote/proxy-memory-listener.h F: hw/remote/iohub.c F: include/hw/remote/iohub.h -F: subprojects/libvfio-user F: hw/remote/vfio-user-obj.c F: include/hw/remote/vfio-user-obj.h F: hw/remote/iommu.c F: include/hw/remote/iommu.h F: tests/functional/test_multiprocess.py +VFIO-USER: +M: John Levon <[email protected]> +M: Thanos Makatos <[email protected]> +S: Supported +F: docs/devel/vfio-user.rst +F: subprojects/libvfio-user + EBPF: M: Jason Wang <[email protected]> R: Andrew Melnychenko <[email protected]>
docs/devel/index-internals.rst +1 -0
@@ -22,6 +22,7 @@ Details about QEMU's various subsystems including how to add features to them. tracing uefi-vars vfio-iommufd + vfio-user writing-monitor-commands virtio-backends crypto
docs/devel/vfio-user.rst new file +1522 -0
1,522 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 05/23] vfio-user: introduce vfio-user protocol specification
Quoted 10 lines · John Levon
From: Thanos Makatos <[email protected]>
This patch introduces the vfio-user protocol specification (formerly known as VFIO-over-socket), which is designed to allow devices to be emulated outside QEMU, in a separate process. vfio-user reuses the existing VFIO defines, structs and concepts.
It has been earlier discussed as an RFC in:
"RFC: use VFIO over a UNIX domain socket to implement device offloading"
Quoted 6 lines
Signed-off-by Thanos Makatos <[email protected]>Signed-off-by John Levon <[email protected]>--- MAINTAINERS | 8 +- docs/devel/index-internals.rst | 1 + docs/devel/vfio-user.rst | 1522 ++++++++++++++++++++++++++++++++
I think this file should be split in several smaller files. The protocol specification part belongs to "System Emulation Management and Interoperability" section.
We already have a docs/system/devices/vfio-user.rst which needs some care. Tbh, this is a bit confusing.
We lack a device example (and functional tests)
Quoted 44 lines
3 files changed, 1530 insertions(+), 1 deletion(-)create mode 100644
docs/devel/vfio-user.rst> diff --git a/MAINTAINERS b/MAINTAINERS
indexaa6763077e..dbc393989a100644MAINTAINERS +7
-1@@ -4241,13 +4241,19 @@ F: hw/remote/proxy-memory-listener.c F: include/hw/remote/proxy-memory-listener.h F: hw/remote/iohub.c F: include/hw/remote/iohub.h -F: subprojects/libvfio-user F: hw/remote/vfio-user-obj.c F: include/hw/remote/vfio-user-obj.h F: hw/remote/iommu.c F: include/hw/remote/iommu.h F: tests/functional/test_multiprocess.py +VFIO-USER: +M: John Levon <[email protected]> +M: Thanos Makatos <[email protected]> +S: Supported +F: docs/devel/vfio-user.rst +F: subprojects/libvfio-user + EBPF: M: Jason Wang <[email protected]> R: Andrew Melnychenko <[email protected]>docs/devel/index-internals.rst +1
-0@@ -22,6 +22,7 @@ Details about QEMU's various subsystems including how to add features to them. tracing uefi-vars vfio-iommufd + vfio-user writing-monitor-commands virtio-backends cryptodocs/devel/vfio-user.rst new file +0
-0
docs/devel/vfio-user.rst line 4
Quoted 4 lines
docs/devel/vfio-user.rst new file +4
-0@@ -0,0 +1,1522 @@ +.. include:: <isonum.txt> +******************************** +vfio-user Protocol Specification +********************************
An '*' line defines a chapter. I think we want a section instead. Please use an '=' line.
docs/devel/vfio-user.rst line 8
Quoted 4 lines
docs/devel/vfio-user.rst new file +4
-0+ +-------------- +Version_ 0.9.1 +--------------
Are you planning to have a subsection per version ?
Also, the version is not consistent with the code :
hw/vfio-user/protocol.h:#define VFIO_USER_MAJOR_VER 0
hw/vfio-user/protocol.h:#define VFIO_USER_MINOR_VER 0
Should this version information move to the "Protocol specification" subsection below ?
Quoted 1,514 lines
docs/devel/vfio-user.rst new file +1514
-0+ +.. contents:: Table of Contents + +Introduction +============ +vfio-user is a protocol that allows a device to be emulated in a separate +process outside of a Virtual Machine Monitor (VMM). vfio-user devices consist +of a generic VFIO device type, living inside the VMM, which we call the client, +and the core device implementation, living outside the VMM, which we call the +server. + +The vfio-user specification is partly based on the +`Linux VFIO ioctl interface <https://www.kernel.org/doc/html/latest/driver-api/vfio.html>`_. + +VFIO is a mature and stable API, backed by an extensively used framework. The +existing VFIO client implementation in QEMU (``qemu/hw/vfio/``) can be largely +re-used, though there is nothing in this specification that requires that +particular implementation. None of the VFIO kernel modules are required for +supporting the protocol, on either the client or server side. Some source +definitions in VFIO are re-used for vfio-user. + +The main idea is to allow a virtual device to function in a separate process in +the same host over a UNIX domain socket. A UNIX domain socket (``AF_UNIX``) is +chosen because file descriptors can be trivially sent over it, which in turn +allows: + +* Sharing of client memory for DMA with the server. +* Sharing of server memory with the client for fast MMIO. +* Efficient sharing of eventfd's for triggering interrupts. + +Other socket types could be used which allow the server to run in a separate +guest in the same host (``AF_VSOCK``) or remotely (``AF_INET``). Theoretically +the underlying transport does not necessarily have to be a socket, however we do +not examine such alternatives. In this protocol version we focus on using a UNIX +domain socket and introduce basic support for the other two types of sockets +without considering performance implications. + +While passing of file descriptors is desirable for performance reasons, support +is not necessary for either the client or the server in order to implement the +protocol. There is always an in-band, message-passing fall back mechanism. + +Overview +======== + +VFIO is a framework that allows a physical device to be securely passed through +to a user space process; the device-specific kernel driver does not drive the +device at all. Typically, the user space process is a VMM and the device is +passed through to it in order to achieve high performance. VFIO provides an API +and the required functionality in the kernel. QEMU has adopted VFIO to allow a +guest to directly access physical devices, instead of emulating them in +software. + +vfio-user reuses the core VFIO concepts defined in its API, but implements them +as messages to be sent over a socket. It does not change the kernel-based VFIO +in any way, in fact none of the VFIO kernel modules need to be loaded to use +vfio-user. It is also possible for the client to concurrently use the current +kernel-based VFIO for one device, and vfio-user for another device. + +VFIO Device Model +----------------- + +A device under VFIO presents a standard interface to the user process. Many of +the VFIO operations in the existing interface use the ``ioctl()`` system call, and +references to the existing interface are called the ``ioctl()`` implementation in +this document. + +The following sections describe the set of messages that implement the vfio-user +interface over a socket. In many cases, the messages are analogous to data +structures used in the ``ioctl()`` implementation. Messages derived from the +``ioctl()`` will have a name derived from the ``ioctl()`` command name. E.g., the +``VFIO_DEVICE_GET_INFO`` ``ioctl()`` command becomes a +``VFIO_USER_DEVICE_GET_INFO`` message. The purpose of this reuse is to share as +much code as feasible with the ``ioctl()`` implementation``. + +Connection Initiation +^^^^^^^^^^^^^^^^^^^^^ + +After the client connects to the server, the initial client message is +``VFIO_USER_VERSION`` to propose a protocol version and set of capabilities to +apply to the session. The server replies with a compatible version and set of +capabilities it supports, or closes the connection if it cannot support the +advertised version. + +Device Information +^^^^^^^^^^^^^^^^^^ + +The client uses a ``VFIO_USER_DEVICE_GET_INFO`` message to query the server for +information about the device. This information includes: + +* The device type and whether it supports reset (``VFIO_DEVICE_FLAGS_``), +* the number of device regions, and +* the device presents to the client the number of interrupt types the device + supports. + +Region Information +^^^^^^^^^^^^^^^^^^ + +The client uses ``VFIO_USER_DEVICE_GET_REGION_INFO`` messages to query the +server for information about the device's regions. This information describes: + +* Read and write permissions, whether it can be memory mapped, and whether it + supports additional capabilities (``VFIO_REGION_INFO_CAP_``). +* Region index, size, and offset. + +When a device region can be mapped by the client, the server provides a file +descriptor which the client can ``mmap()``. The server is responsible for +polling for client updates to memory mapped regions. + +Region Capabilities +""""""""""""""""""" + +Some regions have additional capabilities that cannot be described adequately +by the region info data structure. These capabilities are returned in the +region info reply in a list similar to PCI capabilities in a PCI device's +configuration space. + +Sparse Regions +"""""""""""""" +A region can be memory-mappable in whole or in part. When only a subset of a +region can be mapped by the client, a ``VFIO_REGION_INFO_CAP_SPARSE_MMAP`` +capability is included in the region info reply. This capability describes +which portions can be mapped by the client. + +.. Note:: + For example, in a virtual NVMe controller, sparse regions can be used so + that accesses to the NVMe registers (found in the beginning of BAR0) are + trapped (an infrequent event), while allowing direct access to the doorbells + (an extremely frequent event as every I/O submission requires a write to + BAR0), found in the next page after the NVMe registers in BAR0. + +Device-Specific Regions +""""""""""""""""""""""" + +A device can define regions additional to the standard ones (e.g. PCI indexes +0-8). This is achieved by including a ``VFIO_REGION_INFO_CAP_TYPE`` capability +in the region info reply of a device-specific region. Such regions are reflected +in ``struct vfio_user_device_info.num_regions``. Thus, for PCI devices this +value can be equal to, or higher than, ``VFIO_PCI_NUM_REGIONS``. + +Region I/O via file descriptors +------------------------------- + +For unmapped regions, region I/O from the client is done via +``VFIO_USER_REGION_READ/WRITE``. As an optimization, ioeventfds or ioregionfds +may be configured for sub-regions of some regions. A client may request +information on these sub-regions via ``VFIO_USER_DEVICE_GET_REGION_IO_FDS``; by +configuring the returned file descriptors as ioeventfds or ioregionfds, the +server can be directly notified of I/O (for example, by KVM) without taking a +trip through the client. + +Interrupts +^^^^^^^^^^ + +The client uses ``VFIO_USER_DEVICE_GET_IRQ_INFO`` messages to query the server +for the device's interrupt types. The interrupt types are specific to the bus +the device is attached to, and the client is expected to know the capabilities +of each interrupt type. The server can signal an interrupt by directly injecting +interrupts into the guest via an event file descriptor. The client configures +how the server signals an interrupt with ``VFIO_USER_SET_IRQS`` messages. + +Device Read and Write +^^^^^^^^^^^^^^^^^^^^^ + +When the guest executes load or store operations to an unmapped device region, +the client forwards these operations to the server with +``VFIO_USER_REGION_READ`` or ``VFIO_USER_REGION_WRITE`` messages. The server +will reply with data from the device on read operations or an acknowledgement on +write operations. See `Read and Write Operations`_. + +Client memory access +-------------------- + +The client uses ``VFIO_USER_DMA_MAP`` and ``VFIO_USER_DMA_UNMAP`` messages to +inform the server of the valid DMA ranges that the server can access on behalf +of a device (typically, VM guest memory). DMA memory may be accessed by the +server via ``VFIO_USER_DMA_READ`` and ``VFIO_USER_DMA_WRITE`` messages over the +socket. In this case, the "DMA" part of the naming is a misnomer. + +Actual direct memory access of client memory from the server is possible if the +client provides file descriptors the server can ``mmap()``. Note that ``mmap()`` +privileges cannot be revoked by the client, therefore file descriptors should +only be exported in environments where the client trusts the server not to +corrupt guest memory. + +See `Read and Write Operations`_. + +Client/server interactions +========================== + +Socket +------ + +A server can serve: + +1) one or more clients, and/or +2) one or more virtual devices, belonging to one or more clients. + +The current protocol specification requires a dedicated socket per +client/server connection. It is a server-side implementation detail whether a +single server handles multiple virtual devices from the same or multiple +clients. The location of the socket is implementation-specific. Multiplexing +clients, devices, and servers over the same socket is not supported in this +version of the protocol. + +Authentication +-------------- + +For ``AF_UNIX``, we rely on OS mandatory access controls on the socket files, +therefore it is up to the management layer to set up the socket as required. +Socket types that span guests or hosts will require a proper authentication +mechanism. Defining that mechanism is deferred to a future version of the +protocol. + +Command Concurrency +------------------- + +A client may pipeline multiple commands without waiting for previous command +replies. The server will process commands in the order they are received. A +consequence of this is if a client issues a command with the *No_reply* bit, +then subsequently issues a command without *No_reply*, the older command will +have been processed before the reply to the younger command is sent by the +server. The client must be aware of the device's capability to process +concurrent commands if pipelining is used. For example, pipelining allows +multiple client threads to concurrently access device regions; the client must +ensure these accesses obey device semantics. + +An example is a frame buffer device, where the device may allow concurrent +access to different areas of video memory, but may have indeterminate behavior +if concurrent accesses are performed to command or status registers. + +Note that unrelated messages sent from the server to the client can appear in +between a client to server request/reply and vice versa. + +Implementers should be prepared for certain commands to exhibit potentially +unbounded latencies. For example, ``VFIO_USER_DEVICE_RESET`` may take an +arbitrarily long time to complete; clients should take care not to block +unnecessarily. + +Socket Disconnection Behavior +----------------------------- +The server and the client can disconnect from each other, either intentionally +or unexpectedly. Both the client and the server need to know how to handle such +events. + +Server Disconnection +^^^^^^^^^^^^^^^^^^^^ +A server disconnecting from the client may indicate that: + +1) A virtual device has been restarted, either intentionally (e.g. because of a + device update) or unintentionally (e.g. because of a crash). +2) A virtual device has been shut down with no intention to be restarted. + +It is impossible for the client to know whether or not a failure is +intermittent or innocuous and should be retried, therefore the client should +reset the VFIO device when it detects the socket has been disconnected. +Error recovery will be driven by the guest's device error handling +behavior. + +Client Disconnection +^^^^^^^^^^^^^^^^^^^^ +The client disconnecting from the server primarily means that the client +has exited. Currently, this means that the guest is shut down so the device is +no longer needed therefore the server can automatically exit. However, there +can be cases where a client disconnection should not result in a server exit: + +1) A single server serving multiple clients. +2) A multi-process QEMU upgrading itself step by step, which is not yet + implemented. + +Therefore in order for the protocol to be forward compatible, the server should +respond to a client disconnection as follows: + + - all client memory regions are unmapped and cleaned up (including closing any + passed file descriptors) + - all IRQ file descriptors passed from the old client are closed + - the device state should otherwise be retained + +The expectation is that when a client reconnects, it will re-establish IRQ and +client memory mappings. + +If anything happens to the client (such as qemu really did exit), the control +stack will know about it and can clean up resources accordingly. + +Security Considerations +----------------------- + +Speaking generally, vfio-user clients should not trust servers, and vice versa. +Standard tools and mechanisms should be used on both sides to validate input and +prevent against denial of service scenarios, buffer overflow, etc. + +Request Retry and Response Timeout +---------------------------------- +A failed command is a command that has been successfully sent and has been +responded to with an error code. Failure to send the command in the first place +(e.g. because the socket is disconnected) is a different type of error examined +earlier in the disconnect section. + +.. Note:: + QEMU's VFIO retries certain operations if they fail. While this makes sense + for real HW, we don't know for sure whether it makes sense for virtual + devices. + +Defining a retry and timeout scheme is deferred to a future version of the +protocol. + +Message sizes +------------- + +Some requests have an ``argsz`` field. In a request, it defines the maximum +expected reply payload size, which should be at least the size of the fixed +reply payload headers defined here. The *request* payload size is defined by the +usual ``msg_size`` field in the header, not the ``argsz`` field. + +In a reply, the server sets ``argsz`` field to the size needed for a full +payload size. This may be less than the requested maximum size. This may be +larger than the requested maximum size: in that case, the full payload is not +included in the reply, but the ``argsz`` field in the reply indicates the needed +size, allowing a client to allocate a larger buffer for holding the reply before +trying again. + +In addition, during negotiation (see `Version`_), the client and server may +each specify a ``max_data_xfer_size`` value; this defines the maximum data that +may be read or written via one of the ``VFIO_USER_DMA/REGION_READ/WRITE`` +messages; see `Read and Write Operations`_. + +Protocol Specification +====================== + +To distinguish from the base VFIO symbols, all vfio-user symbols are prefixed +with ``vfio_user`` or ``VFIO_USER``. In this revision, all data is in the +endianness of the host system, although this may be relaxed in future +revisions in cases where the client and server run on different hosts +with different endianness. + +Unless otherwise specified, all sizes should be presumed to be in bytes. + +.. _Commands: + +Commands +-------- +The following table lists the VFIO message command IDs, and whether the +message command is sent from the client or the server. + +====================================== ========= ================= +Name Command Request Direction +====================================== ========= ================= +``VFIO_USER_VERSION`` 1 client -> server +``VFIO_USER_DMA_MAP`` 2 client -> server +``VFIO_USER_DMA_UNMAP`` 3 client -> server +``VFIO_USER_DEVICE_GET_INFO`` 4 client -> server +``VFIO_USER_DEVICE_GET_REGION_INFO`` 5 client -> server +``VFIO_USER_DEVICE_GET_REGION_IO_FDS`` 6 client -> server +``VFIO_USER_DEVICE_GET_IRQ_INFO`` 7 client -> server +``VFIO_USER_DEVICE_SET_IRQS`` 8 client -> server +``VFIO_USER_REGION_READ`` 9 client -> server +``VFIO_USER_REGION_WRITE`` 10 client -> server +``VFIO_USER_DMA_READ`` 11 server -> client +``VFIO_USER_DMA_WRITE`` 12 server -> client +``VFIO_USER_DEVICE_RESET`` 13 client -> server +``VFIO_USER_REGION_WRITE_MULTI`` 15 client -> server +====================================== ========= ================= + +Header +------ + +All messages, both command messages and reply messages, are preceded by a +16-byte header that contains basic information about the message. The header is +followed by message-specific data described in the sections below. + ++----------------+--------+-------------+ +| Name | Offset | Size | ++================+========+=============+ +| Message ID | 0 | 2 | ++----------------+--------+-------------+ +| Command | 2 | 2 | ++----------------+--------+-------------+ +| Message size | 4 | 4 | ++----------------+--------+-------------+ +| Flags | 8 | 4 | ++----------------+--------+-------------+ +| | +-----+------------+ | +| | | Bit | Definition | | +| | +=====+============+ | +| | | 0-3 | Type | | +| | +-----+------------+ | +| | | 4 | No_reply | | +| | +-----+------------+ | +| | | 5 | Error | | +| | +-----+------------+ | ++----------------+--------+-------------+ +| Error | 12 | 4 | ++----------------+--------+-------------+ +| <message data> | 16 | variable | ++----------------+--------+-------------+ + +* *Message ID* identifies the message, and is echoed in the command's reply + message. Message IDs belong entirely to the sender, can be re-used (even + concurrently) and the receiver must not make any assumptions about their + uniqueness. +* *Command* specifies the command to be executed, listed in Commands_. It is + also set in the reply header. +* *Message size* contains the size of the entire message, including the header. +* *Flags* contains attributes of the message: + + * The *Type* bits indicate the message type. + + * *Command* (value 0x0) indicates a command message. + * *Reply* (value 0x1) indicates a reply message acknowledging a previous + command with the same message ID. + * *No_reply* in a command message indicates that no reply is needed for this + command. This is commonly used when multiple commands are sent, and only + the last needs acknowledgement. + * *Error* in a reply message indicates the command being acknowledged had + an error. In this case, the *Error* field will be valid. + +* *Error* in a reply message is an optional UNIX errno value. It may be zero + even if the Error bit is set in Flags. It is reserved in a command message. + +Each command message in Commands_ must be replied to with a reply message, +unless the message sets the *No_Reply* bit. The reply consists of the header +with the *Reply* bit set, plus any additional data. + +If an error occurs, the reply message must only include the reply header. + +As the header is standard in both requests and replies, it is not included in +the command-specific specifications below; each message definition should be +appended to the standard header, and the offsets are given from the end of the +standard header. + +``VFIO_USER_VERSION`` +--------------------- + +.. _Version: + +This is the initial message sent by the client after the socket connection is +established; the same format is used for the server's reply. + +Upon establishing a connection, the client must send a ``VFIO_USER_VERSION`` +message proposing a protocol version and a set of capabilities. The server +compares these with the versions and capabilities it supports and sends a +``VFIO_USER_VERSION`` reply according to the following rules. + +* The major version in the reply must be the same as proposed. If the client + does not support the proposed major, it closes the connection. +* The minor version in the reply must be equal to or less than the minor + version proposed. +* The capability list must be a subset of those proposed. If the server + requires a capability the client did not include, it closes the connection. + +The protocol major version will only change when incompatible protocol changes +are made, such as changing the message format. The minor version may change +when compatible changes are made, such as adding new messages or capabilities, +Both the client and server must support all minor versions less than the +maximum minor version it supports. E.g., an implementation that supports +version 1.3 must also support 1.0 through 1.2. + +When making a change to this specification, the protocol version number must +be included in the form "added in version X.Y" + +Request +^^^^^^^ + +============== ====== ==== +Name Offset Size +============== ====== ==== +version major 0 2 +version minor 2 2 +version data 4 variable (including terminating NUL). Optional. +============== ====== ==== + +The version data is an optional UTF-8 encoded JSON byte array with the following +format: + ++--------------+--------+-----------------------------------+ +| Name | Type | Description | ++==============+========+===================================+ +| capabilities | object | Contains common capabilities that | +| | | the sender supports. Optional. | ++--------------+--------+-----------------------------------+ + +Capabilities: + ++--------------------+---------+------------------------------------------------+ +| Name | Type | Description | ++====================+=========+================================================+ +| max_msg_fds | number | Maximum number of file descriptors that can be | +| | | received by the sender in one message. | +| | | Optional. If not specified then the receiver | +| | | must assume a value of ``1``. | ++--------------------+---------+------------------------------------------------+ +| max_data_xfer_size | number | Maximum ``count`` for data transfer messages; | +| | | see `Read and Write Operations`_. Optional, | +| | | with a default value of 1048576 bytes. | ++--------------------+---------+------------------------------------------------+ +| pgsizes | number | Page sizes supported in DMA map operations | +| | | or'ed together. Optional, with a default value | +| | | of supporting only 4k pages. | ++--------------------+---------+------------------------------------------------+ +| max_dma_maps | number | Maximum number DMA map windows that can be | +| | | valid simultaneously. Optional, with a | +| | | value of 65535 (64k-1). | ++--------------------+---------+------------------------------------------------+ +| migration | object | Migration capability parameters. If missing | +| | | then migration is not supported by the sender. | ++--------------------+---------+------------------------------------------------+ +| write_multiple | boolean | ``VFIO_USER_REGION_WRITE_MULTI`` messages | +| | | are supported if the value is ``true``. | ++--------------------+---------+------------------------------------------------+ + +The migration capability contains the following name/value pairs: + ++-----------------+--------+--------------------------------------------------+ +| Name | Type | Description | ++=================+========+==================================================+ +| pgsize | number | Page size of dirty pages bitmap. The smallest | +| | | between the client and the server is used. | ++-----------------+--------+--------------------------------------------------+ +| max_bitmap_size | number | Maximum bitmap size in ``VFIO_USER_DIRTY_PAGES`` | +| | | and ``VFIO_DMA_UNMAP`` messages. Optional, | +| | | with a default value of 256MB. | ++-----------------+--------+--------------------------------------------------+ + +Reply +^^^^^ + +The same message format is used in the server's reply with the semantics +described above. + +``VFIO_USER_DMA_MAP`` +--------------------- + +This command message is sent by the client to the server to inform it of the +memory regions the server can access. It must be sent before the server can +perform any DMA to the client. It is normally sent directly after the version +handshake is completed, but may also occur when memory is added to the client, +or if the client uses a vIOMMU. + +Request +^^^^^^^ + +The request payload for this message is a structure of the following format: + ++-------------+--------+-------------+ +| Name | Offset | Size | ++=============+========+=============+ +| argsz | 0 | 4 | ++-------------+--------+-------------+ +| flags | 4 | 4 | ++-------------+--------+-------------+ +| | +-----+------------+ | +| | | Bit | Definition | | +| | +=====+============+ | +| | | 0 | readable | | +| | +-----+------------+ | +| | | 1 | writeable | | +| | +-----+------------+ | ++-------------+--------+-------------+ +| offset | 8 | 8 | ++-------------+--------+-------------+ +| address | 16 | 8 | ++-------------+--------+-------------+ +| size | 24 | 8 | ++-------------+--------+-------------+ + +* *argsz* is the size of the above structure. Note there is no reply payload, + so this field differs from other message types. +* *flags* contains the following region attributes: + + * *readable* indicates that the region can be read from. + + * *writeable* indicates that the region can be written to. + +* *offset* is the file offset of the region with respect to the associated file + descriptor, or zero if the region is not mappable +* *address* is the base DMA address of the region. +* *size* is the size of the region. + +This structure is 32 bytes in size, so the message size is 16 + 32 bytes. + +If the DMA region being added can be directly mapped by the server, a file +descriptor must be sent as part of the message meta-data. The region can be +mapped via the mmap() system call. On ``AF_UNIX`` sockets, the file descriptor +must be passed as ``SCM_RIGHTS`` type ancillary data. Otherwise, if the DMA +region cannot be directly mapped by the server, no file descriptor must be sent +as part of the message meta-data and the DMA region can be accessed by the +server using ``VFIO_USER_DMA_READ`` and ``VFIO_USER_DMA_WRITE`` messages, +explained in `Read and Write Operations`_. A command to map over an existing +region must be failed by the server with ``EEXIST`` set in error field in the +reply. + +Reply +^^^^^ + +There is no payload in the reply message. + +``VFIO_USER_DMA_UNMAP`` +----------------------- + +This command message is sent by the client to the server to inform it that a +DMA region, previously made available via a ``VFIO_USER_DMA_MAP`` command +message, is no longer available for DMA. It typically occurs when memory is +subtracted from the client or if the client uses a vIOMMU. The DMA region is +described by the following structure: + +Request +^^^^^^^ + +The request payload for this message is a structure of the following format: + ++--------------+--------+------------------------+ +| Name | Offset | Size | ++==============+========+========================+ +| argsz | 0 | 4 | ++--------------+--------+------------------------+ +| flags | 4 | 4 | ++--------------+--------+------------------------+ +| address | 8 | 8 | ++--------------+--------+------------------------+ +| size | 16 | 8 | ++--------------+--------+------------------------+ + +* *argsz* is the maximum size of the reply payload. +* *flags* is unused in this version. +* *address* is the base DMA address of the DMA region. +* *size* is the size of the DMA region. + +The address and size of the DMA region being unmapped must match exactly a +previous mapping. + +Reply +^^^^^ + +Upon receiving a ``VFIO_USER_DMA_UNMAP`` command, if the file descriptor is +mapped then the server must release all references to that DMA region before +replying, which potentially includes in-flight DMA transactions. + +The server responds with the original DMA entry in the request. + + +``VFIO_USER_DEVICE_GET_INFO`` +----------------------------- + +This command message is sent by the client to the server to query for basic +information about the device. + +Request +^^^^^^^ + ++-------------+--------+--------------------------+ +| Name | Offset | Size | ++=============+========+==========================+ +| argsz | 0 | 4 | ++-------------+--------+--------------------------+ +| flags | 4 | 4 | ++-------------+--------+--------------------------+ +| | +-----+-------------------------+ | +| | | Bit | Definition | | +| | +=====+=========================+ | +| | | 0 | VFIO_DEVICE_FLAGS_RESET | | +| | +-----+-------------------------+ | +| | | 1 | VFIO_DEVICE_FLAGS_PCI | | +| | +-----+-------------------------+ | ++-------------+--------+--------------------------+ +| num_regions | 8 | 4 | ++-------------+--------+--------------------------+ +| num_irqs | 12 | 4 | ++-------------+--------+--------------------------+ + +* *argsz* is the maximum size of the reply payload +* all other fields must be zero. + +Reply +^^^^^ + ++-------------+--------+--------------------------+ +| Name | Offset | Size | ++=============+========+==========================+ +| argsz | 0 | 4 | ++-------------+--------+--------------------------+ +| flags | 4 | 4 | ++-------------+--------+--------------------------+ +| | +-----+-------------------------+ | +| | | Bit | Definition | | +| | +=====+=========================+ | +| | | 0 | VFIO_DEVICE_FLAGS_RESET | | +| | +-----+-------------------------+ | +| | | 1 | VFIO_DEVICE_FLAGS_PCI | | +| | +-----+-------------------------+ | ++-------------+--------+--------------------------+ +| num_regions | 8 | 4 | ++-------------+--------+--------------------------+ +| num_irqs | 12 | 4 | ++-------------+--------+--------------------------+ + +* *argsz* is the size required for the full reply payload (16 bytes today) +* *flags* contains the following device attributes. + + * ``VFIO_DEVICE_FLAGS_RESET`` indicates that the device supports the + ``VFIO_USER_DEVICE_RESET`` message. + * ``VFIO_DEVICE_FLAGS_PCI`` indicates that the device is a PCI device. + +* *num_regions* is the number of memory regions that the device exposes. +* *num_irqs* is the number of distinct interrupt types that the device supports. + +This version of the protocol only supports PCI devices. Additional devices may +be supported in future versions. + +``VFIO_USER_DEVICE_GET_REGION_INFO`` +------------------------------------ + +This command message is sent by the client to the server to query for +information about device regions. The VFIO region info structure is defined in +``<linux/vfio.h>`` (``struct vfio_region_info``). + +Request +^^^^^^^ + ++------------+--------+------------------------------+ +| Name | Offset | Size | ++============+========+==============================+ +| argsz | 0 | 4 | ++------------+--------+------------------------------+ +| flags | 4 | 4 | ++------------+--------+------------------------------+ +| index | 8 | 4 | ++------------+--------+------------------------------+ +| cap_offset | 12 | 4 | ++------------+--------+------------------------------+ +| size | 16 | 8 | ++------------+--------+------------------------------+ +| offset | 24 | 8 | ++------------+--------+------------------------------+ + +* *argsz* the maximum size of the reply payload +* *index* is the index of memory region being queried, it is the only field + that is required to be set in the command message. +* all other fields must be zero. + +Reply +^^^^^ + ++------------+--------+------------------------------+ +| Name | Offset | Size | ++============+========+==============================+ +| argsz | 0 | 4 | ++------------+--------+------------------------------+ +| flags | 4 | 4 | ++------------+--------+------------------------------+ +| | +-----+-----------------------------+ | +| | | Bit | Definition | | +| | +=====+=============================+ | +| | | 0 | VFIO_REGION_INFO_FLAG_READ | | +| | +-----+-----------------------------+ | +| | | 1 | VFIO_REGION_INFO_FLAG_WRITE | | +| | +-----+-----------------------------+ | +| | | 2 | VFIO_REGION_INFO_FLAG_MMAP | | +| | +-----+-----------------------------+ | +| | | 3 | VFIO_REGION_INFO_FLAG_CAPS | | +| | +-----+-----------------------------+ | ++------------+--------+------------------------------+ ++------------+--------+------------------------------+ +| index | 8 | 4 | ++------------+--------+------------------------------+ +| cap_offset | 12 | 4 | ++------------+--------+------------------------------+ +| size | 16 | 8 | ++------------+--------+------------------------------+ +| offset | 24 | 8 | ++------------+--------+------------------------------+ + +* *argsz* is the size required for the full reply payload (region info structure + plus the size of any region capabilities) +* *flags* are attributes of the region: + + * ``VFIO_REGION_INFO_FLAG_READ`` allows client read access to the region. + * ``VFIO_REGION_INFO_FLAG_WRITE`` allows client write access to the region. + * ``VFIO_REGION_INFO_FLAG_MMAP`` specifies the client can mmap() the region. + When this flag is set, the reply will include a file descriptor in its + meta-data. On ``AF_UNIX`` sockets, the file descriptors will be passed as + ``SCM_RIGHTS`` type ancillary data. + * ``VFIO_REGION_INFO_FLAG_CAPS`` indicates additional capabilities found in the + reply. + +* *index* is the index of memory region being queried, it is the only field + that is required to be set in the command message. +* *cap_offset* describes where additional region capabilities can be found. + cap_offset is relative to the beginning of the VFIO region info structure. + The data structure it points is a VFIO cap header defined in + ``<linux/vfio.h>``. +* *size* is the size of the region. +* *offset* is the offset that should be given to the mmap() system call for + regions with the MMAP attribute. It is also used as the base offset when + mapping a VFIO sparse mmap area, described below. + +VFIO region capabilities +"""""""""""""""""""""""" + +The VFIO region information can also include a capabilities list. This list is +similar to a PCI capability list - each entry has a common header that +identifies a capability and where the next capability in the list can be found. +The VFIO capability header format is defined in ``<linux/vfio.h>`` (``struct +vfio_info_cap_header``). + +VFIO cap header format +"""""""""""""""""""""" + ++---------+--------+------+ +| Name | Offset | Size | ++=========+========+======+ +| id | 0 | 2 | ++---------+--------+------+ +| version | 2 | 2 | ++---------+--------+------+ +| next | 4 | 4 | ++---------+--------+------+ + +* *id* is the capability identity. +* *version* is a capability-specific version number. +* *next* specifies the offset of the next capability in the capability list. It + is relative to the beginning of the VFIO region info structure. + +VFIO sparse mmap cap header +""""""""""""""""""""""""""" + ++------------------+----------------------------------+ +| Name | Value | ++==================+==================================+ +| id | VFIO_REGION_INFO_CAP_SPARSE_MMAP | ++------------------+----------------------------------+ +| version | 0x1 | ++------------------+----------------------------------+ +| next | <next> | ++------------------+----------------------------------+ +| sparse mmap info | VFIO region info sparse mmap | ++------------------+----------------------------------+ + +This capability is defined when only a subrange of the region supports +direct access by the client via mmap(). The VFIO sparse mmap area is defined in +``<linux/vfio.h>`` (``struct vfio_region_sparse_mmap_area`` and ``struct +vfio_region_info_cap_sparse_mmap``). + +VFIO region info cap sparse mmap +"""""""""""""""""""""""""""""""" + ++----------+--------+------+ +| Name | Offset | Size | ++==========+========+======+ +| nr_areas | 0 | 4 | ++----------+--------+------+ +| reserved | 4 | 4 | ++----------+--------+------+ +| offset | 8 | 8 | ++----------+--------+------+ +| size | 16 | 8 | ++----------+--------+------+ +| ... | | | ++----------+--------+------+ + +* *nr_areas* is the number of sparse mmap areas in the region. +* *offset* and size describe a single area that can be mapped by the client. + There will be *nr_areas* pairs of offset and size. The offset will be added to + the base offset given in the ``VFIO_USER_DEVICE_GET_REGION_INFO`` to form the + offset argument of the subsequent mmap() call. + +The VFIO sparse mmap area is defined in ``<linux/vfio.h>`` (``struct +vfio_region_info_cap_sparse_mmap``). + + +``VFIO_USER_DEVICE_GET_REGION_IO_FDS`` +-------------------------------------- + +Clients can access regions via ``VFIO_USER_REGION_READ/WRITE`` or, if provided, by +``mmap()`` of a file descriptor provided by the server. + +``VFIO_USER_DEVICE_GET_REGION_IO_FDS`` provides an alternative access mechanism via +file descriptors. This is an optional feature intended for performance +improvements where an underlying sub-system (such as KVM) supports communication +across such file descriptors to the vfio-user server, without needing to +round-trip through the client. + +The server returns an array of sub-regions for the requested region. Each +sub-region describes a span (offset and size) of a region, along with the +requested file descriptor notification mechanism to use. Each sub-region in the +response message may choose to use a different method, as defined below. The +two mechanisms supported in this specification are ioeventfds and ioregionfds. + +The server in addition returns a file descriptor in the ancillary data; clients +are expected to configure each sub-region's file descriptor with the requested +notification method. For example, a client could configure KVM with the +requested ioeventfd via a ``KVM_IOEVENTFD`` ``ioctl()``. + +Request +^^^^^^^ + ++-------------+--------+------+ +| Name | Offset | Size | ++=============+========+======+ +| argsz | 0 | 4 | ++-------------+--------+------+ +| flags | 4 | 4 | ++-------------+--------+------+ +| index | 8 | 4 | ++-------------+--------+------+ +| count | 12 | 4 | ++-------------+--------+------+ + +* *argsz* the maximum size of the reply payload +* *index* is the index of memory region being queried +* all other fields must be zero + +The client must set ``flags`` to zero and specify the region being queried in +the ``index``. + +Reply +^^^^^ + ++-------------+--------+------+ +| Name | Offset | Size | ++=============+========+======+ +| argsz | 0 | 4 | ++-------------+--------+------+ +| flags | 4 | 4 | ++-------------+--------+------+ +| index | 8 | 4 | ++-------------+--------+------+ +| count | 12 | 4 | ++-------------+--------+------+ +| sub-regions | 16 | ... | ++-------------+--------+------+ + +* *argsz* is the size of the region IO FD info structure plus the + total size of the sub-region array. Thus, each array entry "i" is at offset + i * ((argsz - 32) / count). Note that currently this is 40 bytes for both IO + FD types, but this is not to be relied on. As elsewhere, this indicates the + full reply payload size needed. +* *flags* must be zero +* *index* is the index of memory region being queried +* *count* is the number of sub-regions in the array +* *sub-regions* is the array of Sub-Region IO FD info structures + +The reply message will additionally include at least one file descriptor in the +ancillary data. Note that more than one sub-region may share the same file +descriptor. + +Note that it is the client's responsibility to verify the requested values (for +example, that the requested offset does not exceed the region's bounds). + +Each sub-region given in the response has one of two possible structures, +depending whether *type* is ``VFIO_USER_IO_FD_TYPE_IOEVENTFD`` or +``VFIO_USER_IO_FD_TYPE_IOREGIONFD``: + +Sub-Region IO FD info format (ioeventfd) +"""""""""""""""""""""""""""""""""""""""" + ++-----------+--------+------+ +| Name | Offset | Size | ++===========+========+======+ +| offset | 0 | 8 | ++-----------+--------+------+ +| size | 8 | 8 | ++-----------+--------+------+ +| fd_index | 16 | 4 | ++-----------+--------+------+ +| type | 20 | 4 | ++-----------+--------+------+ +| flags | 24 | 4 | ++-----------+--------+------+ +| padding | 28 | 4 | ++-----------+--------+------+ +| datamatch | 32 | 8 | ++-----------+--------+------+ + +* *offset* is the offset of the start of the sub-region within the region + requested ("physical address offset" for the region) +* *size* is the length of the sub-region. This may be zero if the access size is + not relevant, which may allow for optimizations +* *fd_index* is the index in the ancillary data of the FD to use for ioeventfd + notification; it may be shared. +* *type* is ``VFIO_USER_IO_FD_TYPE_IOEVENTFD`` +* *flags* is any of: + + * ``KVM_IOEVENTFD_FLAG_DATAMATCH`` + * ``KVM_IOEVENTFD_FLAG_PIO`` + * ``KVM_IOEVENTFD_FLAG_VIRTIO_CCW_NOTIFY`` (FIXME: makes sense?) + +* *datamatch* is the datamatch value if needed + +See https://www.kernel.org/doc/Documentation/virtual/kvm/api.txt, *4.59 +KVM_IOEVENTFD* for further context on the ioeventfd-specific fields. + +Sub-Region IO FD info format (ioregionfd) +""""""""""""""""""""""""""""""""""""""""" + ++-----------+--------+------+ +| Name | Offset | Size | ++===========+========+======+ +| offset | 0 | 8 | ++-----------+--------+------+ +| size | 8 | 8 | ++-----------+--------+------+ +| fd_index | 16 | 4 | ++-----------+--------+------+ +| type | 20 | 4 | ++-----------+--------+------+ +| flags | 24 | 4 | ++-----------+--------+------+ +| padding | 28 | 4 | ++-----------+--------+------+ +| user_data | 32 | 8 | ++-----------+--------+------+ + +* *offset* is the offset of the start of the sub-region within the region + requested ("physical address offset" for the region) +* *size* is the length of the sub-region. This may be zero if the access size is + not relevant, which may allow for optimizations; ``KVM_IOREGION_POSTED_WRITES`` + must be set in *flags* in this case +* *fd_index* is the index in the ancillary data of the FD to use for ioregionfd + messages; it may be shared +* *type* is ``VFIO_USER_IO_FD_TYPE_IOREGIONFD`` +* *flags* is any of: + + * ``KVM_IOREGION_PIO`` + * ``KVM_IOREGION_POSTED_WRITES`` + +* *user_data* is an opaque value passed back to the server via a message on the + file descriptor + +For further information on the ioregionfd-specific fields, see: +https://lore.kernel.org/kvm/[email protected]/ + +(FIXME: update with final API docs.) + +``VFIO_USER_DEVICE_GET_IRQ_INFO`` +--------------------------------- + +This command message is sent by the client to the server to query for +information about device interrupt types. The VFIO IRQ info structure is +defined in ``<linux/vfio.h>`` (``struct vfio_irq_info``). + +Request +^^^^^^^ + ++-------+--------+---------------------------+ +| Name | Offset | Size | ++=======+========+===========================+ +| argsz | 0 | 4 | ++-------+--------+---------------------------+ +| flags | 4 | 4 | ++-------+--------+---------------------------+ +| | +-----+--------------------------+ | +| | | Bit | Definition | | +| | +=====+==========================+ | +| | | 0 | VFIO_IRQ_INFO_EVENTFD | | +| | +-----+--------------------------+ | +| | | 1 | VFIO_IRQ_INFO_MASKABLE | | +| | +-----+--------------------------+ | +| | | 2 | VFIO_IRQ_INFO_AUTOMASKED | | +| | +-----+--------------------------+ | +| | | 3 | VFIO_IRQ_INFO_NORESIZE | | +| | +-----+--------------------------+ | ++-------+--------+---------------------------+ +| index | 8 | 4 | ++-------+--------+---------------------------+ +| count | 12 | 4 | ++-------+--------+---------------------------+ + +* *argsz* is the maximum size of the reply payload (16 bytes today) +* index is the index of IRQ type being queried (e.g. ``VFIO_PCI_MSIX_IRQ_INDEX``) +* all other fields must be zero + +Reply +^^^^^ + ++-------+--------+---------------------------+ +| Name | Offset | Size | ++=======+========+===========================+ +| argsz | 0 | 4 | ++-------+--------+---------------------------+ +| flags | 4 | 4 | ++-------+--------+---------------------------+ +| | +-----+--------------------------+ | +| | | Bit | Definition | | +| | +=====+==========================+ | +| | | 0 | VFIO_IRQ_INFO_EVENTFD | | +| | +-----+--------------------------+ | +| | | 1 | VFIO_IRQ_INFO_MASKABLE | | +| | +-----+--------------------------+ | +| | | 2 | VFIO_IRQ_INFO_AUTOMASKED | | +| | +-----+--------------------------+ | +| | | 3 | VFIO_IRQ_INFO_NORESIZE | | +| | +-----+--------------------------+ | ++-------+--------+---------------------------+ +| index | 8 | 4 | ++-------+--------+---------------------------+ +| count | 12 | 4 | ++-------+--------+---------------------------+ + +* *argsz* is the size required for the full reply payload (16 bytes today) +* *flags* defines IRQ attributes: + + * ``VFIO_IRQ_INFO_EVENTFD`` indicates the IRQ type can support server eventfd + signalling. + * ``VFIO_IRQ_INFO_MASKABLE`` indicates that the IRQ type supports the ``MASK`` + and ``UNMASK`` actions in a ``VFIO_USER_DEVICE_SET_IRQS`` message. + * ``VFIO_IRQ_INFO_AUTOMASKED`` indicates the IRQ type masks itself after being + triggered, and the client must send an ``UNMASK`` action to receive new + interrupts. + * ``VFIO_IRQ_INFO_NORESIZE`` indicates ``VFIO_USER_SET_IRQS`` operations setup + interrupts as a set, and new sub-indexes cannot be enabled without disabling + the entire type. +* index is the index of IRQ type being queried +* count describes the number of interrupts of the queried type. + +``VFIO_USER_DEVICE_SET_IRQS`` +----------------------------- + +This command message is sent by the client to the server to set actions for +device interrupt types. The VFIO IRQ set structure is defined in +``<linux/vfio.h>`` (``struct vfio_irq_set``). + +Request +^^^^^^^ + ++-------+--------+------------------------------+ +| Name | Offset | Size | ++=======+========+==============================+ +| argsz | 0 | 4 | ++-------+--------+------------------------------+ +| flags | 4 | 4 | ++-------+--------+------------------------------+ +| | +-----+-----------------------------+ | +| | | Bit | Definition | | +| | +=====+=============================+ | +| | | 0 | VFIO_IRQ_SET_DATA_NONE | | +| | +-----+-----------------------------+ | +| | | 1 | VFIO_IRQ_SET_DATA_BOOL | | +| | +-----+-----------------------------+ | +| | | 2 | VFIO_IRQ_SET_DATA_EVENTFD | | +| | +-----+-----------------------------+ | +| | | 3 | VFIO_IRQ_SET_ACTION_MASK | | +| | +-----+-----------------------------+ | +| | | 4 | VFIO_IRQ_SET_ACTION_UNMASK | | +| | +-----+-----------------------------+ | +| | | 5 | VFIO_IRQ_SET_ACTION_TRIGGER | | +| | +-----+-----------------------------+ | ++-------+--------+------------------------------+ +| index | 8 | 4 | ++-------+--------+------------------------------+ +| start | 12 | 4 | ++-------+--------+------------------------------+ +| count | 16 | 4 | ++-------+--------+------------------------------+ +| data | 20 | variable | ++-------+--------+------------------------------+ + +* *argsz* is the size of the VFIO IRQ set request payload, including any *data* + field. Note there is no reply payload, so this field differs from other + message types. +* *flags* defines the action performed on the interrupt range. The ``DATA`` + flags describe the data field sent in the message; the ``ACTION`` flags + describe the action to be performed. The flags are mutually exclusive for + both sets. + + * ``VFIO_IRQ_SET_DATA_NONE`` indicates there is no data field in the command. + The action is performed unconditionally. + * ``VFIO_IRQ_SET_DATA_BOOL`` indicates the data field is an array of boolean + bytes. The action is performed if the corresponding boolean is true. + * ``VFIO_IRQ_SET_DATA_EVENTFD`` indicates an array of event file descriptors + was sent in the message meta-data. These descriptors will be signalled when + the action defined by the action flags occurs. In ``AF_UNIX`` sockets, the + descriptors are sent as ``SCM_RIGHTS`` type ancillary data. + If no file descriptors are provided, this de-assigns the specified + previously configured interrupts. + * ``VFIO_IRQ_SET_ACTION_MASK`` indicates a masking event. It can be used with + ``VFIO_IRQ_SET_DATA_BOOL`` or ``VFIO_IRQ_SET_DATA_NONE`` to mask an interrupt, + or with ``VFIO_IRQ_SET_DATA_EVENTFD`` to generate an event when the guest masks + the interrupt. + * ``VFIO_IRQ_SET_ACTION_UNMASK`` indicates an unmasking event. It can be used + with ``VFIO_IRQ_SET_DATA_BOOL`` or ``VFIO_IRQ_SET_DATA_NONE`` to unmask an + interrupt, or with ``VFIO_IRQ_SET_DATA_EVENTFD`` to generate an event when the + guest unmasks the interrupt. + * ``VFIO_IRQ_SET_ACTION_TRIGGER`` indicates a triggering event. It can be used + with ``VFIO_IRQ_SET_DATA_BOOL`` or ``VFIO_IRQ_SET_DATA_NONE`` to trigger an + interrupt, or with ``VFIO_IRQ_SET_DATA_EVENTFD`` to generate an event when the + server triggers the interrupt. + +* *index* is the index of IRQ type being setup. +* *start* is the start of the sub-index being set. +* *count* describes the number of sub-indexes being set. As a special case, a + count (and start) of 0, with data flags of ``VFIO_IRQ_SET_DATA_NONE`` disables + all interrupts of the index. +* *data* is an optional field included when the + ``VFIO_IRQ_SET_DATA_BOOL`` flag is present. It contains an array of booleans + that specify whether the action is to be performed on the corresponding + index. It's used when the action is only performed on a subset of the range + specified. + +Not all interrupt types support every combination of data and action flags. +The client must know the capabilities of the device and IRQ index before it +sends a ``VFIO_USER_DEVICE_SET_IRQ`` message. + +In typical operation, a specific IRQ may operate as follows: + +1. The client sends a ``VFIO_USER_DEVICE_SET_IRQ`` message with + ``flags=(VFIO_IRQ_SET_DATA_EVENTFD|VFIO_IRQ_SET_ACTION_TRIGGER)`` along + with an eventfd. This associates the IRQ with a particular eventfd on the + server side. + +#. The client may send a ``VFIO_USER_DEVICE_SET_IRQ`` message with + ``flags=(VFIO_IRQ_SET_DATA_EVENTFD|VFIO_IRQ_SET_ACTION_MASK/UNMASK)`` along + with another eventfd. This associates the given eventfd with the + mask/unmask state on the server side. + +#. The server may trigger the IRQ by writing 1 to the eventfd. + +#. The server may mask/unmask an IRQ which will write 1 to the corresponding + mask/unmask eventfd, if there is one. + +5. A client may trigger a device IRQ itself, by sending a + ``VFIO_USER_DEVICE_SET_IRQ`` message with + ``flags=(VFIO_IRQ_SET_DATA_NONE/BOOL|VFIO_IRQ_SET_ACTION_TRIGGER)``. + +6. A client may mask or unmask the IRQ, by sending a + ``VFIO_USER_DEVICE_SET_IRQ`` message with + ``flags=(VFIO_IRQ_SET_DATA_NONE/BOOL|VFIO_IRQ_SET_ACTION_MASK/UNMASK)``. + +Reply +^^^^^ + +There is no payload in the reply. + +.. _Read and Write Operations: + +Note that all of these operations must be supported by the client and/or server, +even if the corresponding memory or device region has been shared as mappable. + +The ``count`` field must not exceed the value of ``max_data_xfer_size`` of the +peer, for both reads and writes. + +``VFIO_USER_REGION_READ`` +------------------------- + +If a device region is not mappable, it's not directly accessible by the client +via ``mmap()`` of the underlying file descriptor. In this case, a client can +read from a device region with this message. + +Request +^^^^^^^ + ++--------+--------+----------+ +| Name | Offset | Size | ++========+========+==========+ +| offset | 0 | 8 | ++--------+--------+----------+ +| region | 8 | 4 | ++--------+--------+----------+ +| count | 12 | 4 | ++--------+--------+----------+ + +* *offset* into the region being accessed. +* *region* is the index of the region being accessed. +* *count* is the size of the data to be transferred. + +Reply +^^^^^ + ++--------+--------+----------+ +| Name | Offset | Size | ++========+========+==========+ +| offset | 0 | 8 | ++--------+--------+----------+ +| region | 8 | 4 | ++--------+--------+----------+ +| count | 12 | 4 | ++--------+--------+----------+ +| data | 16 | variable | ++--------+--------+----------+ + +* *offset* into the region accessed. +* *region* is the index of the region accessed. +* *count* is the size of the data transferred. +* *data* is the data that was read from the device region. + +``VFIO_USER_REGION_WRITE`` +-------------------------- + +If a device region is not mappable, it's not directly accessible by the client +via mmap() of the underlying fd. In this case, a client can write to a device +region with this message. + +Request +^^^^^^^ + ++--------+--------+----------+ +| Name | Offset | Size | ++========+========+==========+ +| offset | 0 | 8 | ++--------+--------+----------+ +| region | 8 | 4 | ++--------+--------+----------+ +| count | 12 | 4 | ++--------+--------+----------+ +| data | 16 | variable | ++--------+--------+----------+ + +* *offset* into the region being accessed. +* *region* is the index of the region being accessed. +* *count* is the size of the data to be transferred. +* *data* is the data to write + +Reply +^^^^^ + ++--------+--------+----------+ +| Name | Offset | Size | ++========+========+==========+ +| offset | 0 | 8 | ++--------+--------+----------+ +| region | 8 | 4 | ++--------+--------+----------+ +| count | 12 | 4 | ++--------+--------+----------+ + +* *offset* into the region accessed. +* *region* is the index of the region accessed. +* *count* is the size of the data transferred. + +``VFIO_USER_DMA_READ`` +----------------------- + +If the client has not shared mappable memory, the server can use this message to +read from guest memory. + +Request +^^^^^^^ + ++---------+--------+----------+ +| Name | Offset | Size | ++=========+========+==========+ +| address | 0 | 8 | ++---------+--------+----------+ +| count | 8 | 8 | ++---------+--------+----------+ + +* *address* is the client DMA memory address being accessed. This address must have + been previously exported to the server with a ``VFIO_USER_DMA_MAP`` message. +* *count* is the size of the data to be transferred. + +Reply +^^^^^ + ++---------+--------+----------+ +| Name | Offset | Size | ++=========+========+==========+ +| address | 0 | 8 | ++---------+--------+----------+ +| count | 8 | 8 | ++---------+--------+----------+ +| data | 16 | variable | ++---------+--------+----------+ + +* *address* is the client DMA memory address being accessed. +* *count* is the size of the data transferred. +* *data* is the data read. + +``VFIO_USER_DMA_WRITE`` +----------------------- + +If the client has not shared mappable memory, the server can use this message to +write to guest memory. + +Request +^^^^^^^ + ++---------+--------+----------+ +| Name | Offset | Size | ++=========+========+==========+ +| address | 0 | 8 | ++---------+--------+----------+ +| count | 8 | 8 | ++---------+--------+----------+ +| data | 16 | variable | ++---------+--------+----------+ + +* *address* is the client DMA memory address being accessed. This address must have + been previously exported to the server with a ``VFIO_USER_DMA_MAP`` message. +* *count* is the size of the data to be transferred. +* *data* is the data to write + +Reply +^^^^^ + ++---------+--------+----------+ +| Name | Offset | Size | ++=========+========+==========+ +| address | 0 | 8 | ++---------+--------+----------+ +| count | 8 | 4 | ++---------+--------+----------+ + +* *address* is the client DMA memory address being accessed. +* *count* is the size of the data transferred. + +``VFIO_USER_DEVICE_RESET`` +-------------------------- + +This command message is sent from the client to the server to reset the device. +Neither the request or reply have a payload. + +``VFIO_USER_REGION_WRITE_MULTI`` +-------------------------------- + +This message can be used to coalesce multiple device write operations +into a single messgage. It is only used as an optimization when the +outgoing message queue is relatively full. + +Request +^^^^^^^ + ++---------+--------+----------+ +| Name | Offset | Size | ++=========+========+==========+ +| wr_cnt | 0 | 8 | ++---------+--------+----------+ +| wrs | 8 | variable | ++---------+--------+----------+ + +* *wr_cnt* is the number of device writes coalesced in the message +* *wrs* is an array of device writes defined below + +Single Device Write Format +"""""""""""""""""""""""""" + ++--------+--------+----------+ +| Name | Offset | Size | ++========+========+==========+ +| offset | 0 | 8 | ++--------+--------+----------+ +| region | 8 | 4 | ++--------+--------+----------+ +| count | 12 | 4 | ++--------+--------+----------+ +| data | 16 | 8 | ++--------+--------+----------+ + +* *offset* into the region being accessed. +* *region* is the index of the region being accessed. +* *count* is the size of the data to be transferred. This format can + only describe writes of 8 bytes or less. +* *data* is the data to write. + +Reply +^^^^^ + ++---------+--------+----------+ +| Name | Offset | Size | ++=========+========+==========+ +| wr_cnt | 0 | 8 | ++---------+--------+----------+ + +* *wr_cnt* is the number of device writes completed. + + +Appendices +========== + +Unused VFIO ``ioctl()`` commands +-------------------------------- + +The following VFIO commands do not have an equivalent vfio-user command: + +* ``VFIO_GET_API_VERSION`` +* ``VFIO_CHECK_EXTENSION`` +* ``VFIO_SET_IOMMU`` +* ``VFIO_GROUP_GET_STATUS`` +* ``VFIO_GROUP_SET_CONTAINER`` +* ``VFIO_GROUP_UNSET_CONTAINER`` +* ``VFIO_GROUP_GET_DEVICE_FD`` +* ``VFIO_IOMMU_GET_INFO`` + +However, once support for live migration for VFIO devices is finalized some +of the above commands may have to be handled by the client in their +corresponding vfio-user form. This will be addressed in a future protocol +version. + +VFIO groups and containers +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The current VFIO implementation includes group and container idioms that +describe how a device relates to the host IOMMU. In the vfio-user +implementation, the IOMMU is implemented in SW by the client, and is not +visible to the server. The simplest idea would be that the client put each +device into its own group and container. + +Backend Program Conventions +--------------------------- + +vfio-user backend program conventions are based on the vhost-user ones. + +* The backend program must not daemonize itself. +* No assumptions must be made as to what access the backend program has on the + system. +* File descriptors 0, 1 and 2 must exist, must have regular + stdin/stdout/stderr semantics, and can be redirected. +* The backend program must honor the SIGTERM signal. +* The backend program must accept the following commands line options: + + * ``--socket-path=PATH``: path to UNIX domain socket, + * ``--fd=FDNUM``: file descriptor for UNIX domain socket, incompatible with + ``--socket-path`` +* The backend program must be accompanied with a JSON file stored under + ``/usr/share/vfio-user``. + +TODO add schema similar to docs/interop/vhost-user.json.
Re: [PATCH v3 05/23] vfio-user: introduce vfio-user protocol specification
Quoted 5 lines · Cédric Le Goater
Quoted 1 line
docs/devel/vfio-user.rst | 1522 ++++++++++++++++++++++++++++++++I think this file should be split in several smaller files. The protocol specification part belongs to "System Emulation Management and Interoperability" section.
I can move it to interop/ sure. Why must it be split up ? It's nearest analogue, vhost-user.rst, is one file, and I think that's better for a protocol specification.
Quoted 2 lines
We already have a
docs/system/devices/vfio-user.rstwhich needs some care.
My series adds this file. This is the user docs, including a device example, so independent of the protocol specification.
Please let me know what care it needs!
Quoted 1 line
(and functional tests)
Mark is working on them, there are some difficulties...
regards
john
Re: [PATCH v3 05/23] vfio-user: introduce vfio-user protocol specification
Quoted 12 lines · John Levon
Quoted 5 lines · Cédric Le Goater
Quoted 1 line
docs/devel/vfio-user.rst | 1522 ++++++++++++++++++++++++++++++++I think this file should be split in several smaller files. The protocol specification part belongs to "System Emulation Management and Interoperability" section.
I can move it to interop/ sure. Why must it be split up ? It's nearest analogue,
vhost-user.rst, is one file, and I think that's better for a protocol specification.
Quoted 5 lines
Quoted 2 lines
We already have a
docs/system/devices/vfio-user.rstwhich needs some care.My series adds this file. This is the user docs, including a device example, so independent of the protocol specification.
ah yes. The QEMU documentation is a bit messy. Not your fault.
Quoted 1 line
Please let me know what care it needs!
sure.
Quoted 3 lines
Quoted 1 line
(and functional tests)
Mark is working on them, there are some difficulties...
Which kind ? This is important to have and I will be reluctant to merge any support without tests. It is OK to merge a dummy device implementation for the tests since they provide a good example.
C.
Re: [PATCH v3 05/23] vfio-user: introduce vfio-user protocol specification
Quoted 28 lines · Cédric Le Goater
Quoted 13 lines · John Levon
Quoted 6 lines · Cédric Le Goater
Quoted 2 lines
docs/devel/vfio-user.rst | 1522 +++++++++++++++++++++++++++
+++++I think this file should be split in several smaller files. The protocol specification part belongs to "System Emulation Management and Interoperability" section.
I can move it to interop/ sure. Why must it be split up ? It's nearest analogue,
vhost-user.rst, is one file, and I think that's better for a protocol specification.Quoted 5 lines
Quoted 2 lines
We already have a
docs/system/devices/vfio-user.rstwhich needs some care.My series adds this file. This is the user docs, including a device example, so independent of the protocol specification.
ah yes. The QEMU documentation is a bit messy. Not your fault.
Quoted 1 line
Please let me know what care it needs!
sure.
Quoted 2 lines
Quoted 1 line
(and functional tests)
Quoted 7 lines
Quoted 1 line
Mark is working on them, there are some difficulties...
Which kind ? This is important to have and I will be reluctant to merge any support without tests. It is OK to merge a dummy device implementation for the tests since they provide a good example.
C.
I have a bootable test image available for testing basic functionality, however there are a few issues with the functional test framework: in particular the test framework doesn't currently allow logging of multiple VM consoles correctly.
I am continuing to work on this in the background, and hope to be able to provide an integrated functional test soon.
ATB,
Mark.
Re: [PATCH v3 05/23] vfio-user: introduce vfio-user protocol specification
Quoted 31 lines · Mark Cave-Ayland
Quoted 29 lines · Cédric Le Goater
Quoted 12 lines · John Levon
Quoted 5 lines · Cédric Le Goater
Quoted 1 line
docs/devel/vfio-user.rst | 1522 +++++++++++++++++++++++++++ +++++
I think this file should be split in several smaller files. The protocol specification part belongs to "System Emulation Management and Interoperability" section.
I can move it to interop/ sure. Why must it be split up ? It's nearest analogue,
vhost-user.rst, is one file, and I think that's better for a protocol specification.Quoted 5 lines
Quoted 2 lines
We already have a
docs/system/devices/vfio-user.rstwhich needs some care.My series adds this file. This is the user docs, including a device example, so independent of the protocol specification.
ah yes. The QEMU documentation is a bit messy. Not your fault.
Quoted 1 line
Please let me know what care it needs!
sure.
Quoted 3 lines
Quoted 1 line
(and functional tests)
Mark is working on them, there are some difficulties...
Quoted 7 lines
Quoted 5 lines
Which kind ? This is important to have and I will be reluctant to merge any support without tests. It is OK to merge a dummy device implementation for the tests since they provide a good example.
C.
I have a bootable test image available for testing basic functionality, however there are a few issues with the functional test framework: in particular the test framework doesn't currently allow logging of multiple VM consoles correctly.
What about test_aarch64_aspeed_ast2700fc.py which boots multiple SoCs ?
Quoted 1 line
I am continuing to work on this in the background, and hope to be able to provide an integrated functional test soon.
Nice ! That would be great.
Thanks Mark,
C.
Re: [PATCH v3 05/23] vfio-user: introduce vfio-user protocol specification
In reply to Cédric Le GoaterQuoted 7 lines · Cédric Le Goater
docs/devel/vfio-user.rst line 8
Quoted 4 lines
docs/devel/vfio-user.rst new file +4
-0+ +-------------- +Version_ 0.9.1 +--------------Are you planning to have a subsection per version ?
I've removed this, as it's not useful/meaningful at this point in time.
regards
john
Re: [PATCH v3 05/23] vfio-user: introduce vfio-user protocol specification
In reply to John LevonQuoted 7 lines · John Levon
From: Thanos Makatos <[email protected]>
This patch introduces the vfio-user protocol specification (formerly known as VFIO-over-socket), which is designed to allow devices to be emulated outside QEMU, in a separate process. vfio-user reuses the existing VFIO defines, structs and concepts.
Quoted 5 lines
It has been earlier discussed as an RFC in:
"RFC: use VFIO over a UNIX domain socket to implement device offloading"Signed-off-by Thanos Makatos <[email protected]>Signed-off-by John Levon <[email protected]>
please put this documentation change at the end in v4.
Thanks,
C.
PATCH v3 6/23 vfio-user: add vfio-user class and container
In reply to John LevonIntroduce basic plumbing for vfio-user behind a new --enable-vfio-user-client option.
We introduce VFIOUserContainer in hw/vfio-user/container.c, which is a container type for the "IOMMU" type "vfio-iommu-user", and share some common container code from hw/vfio/container.c.
Add hw/vfio-user/pci.c for instantiating VFIOUserPCIDevice objects, sharing some common code from hw/vfio/pci.c.
--- MAINTAINERS | 2 + hw/vfio-user/container.h | 25 +++ include/hw/vfio/vfio-container-base.h | 1 + hw/vfio-user/container.c | 212 ++++++++++++++++++++++++++ hw/vfio-user/pci.c | 155 +++++++++++++++++++ hw/meson.build | 1 + hw/vfio-user/meson.build | 9 ++ meson_options.txt | 2 + scripts/meson-buildoptions.sh | 4 + 9 files changed, 411 insertions(+) create mode 100644 hw/vfio-user/container.h create mode 100644 hw/vfio-user/container.c create mode 100644 hw/vfio-user/pci.c create mode 100644 hw/vfio-user/meson.build
MAINTAINERS +2 -0
@@ -4252,6 +4252,8 @@ M: John Levon <[email protected]> M: Thanos Makatos <[email protected]> S: Supported F: docs/devel/vfio-user.rst +F: hw/vfio-user/* +F: include/hw/vfio-user/* F: subprojects/libvfio-user EBPF:
hw/vfio-user/container.h new file +25 -0
@@ -0,0 +1,25 @@ +/* +* vfio-user specific definitions. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VFIO_USER_CONTAINER_H +#define HW_VFIO_USER_CONTAINER_H + +#include <inttypes.h> +#include <stdbool.h> + +#include "hw/vfio/vfio-container-base.h" + +/* MMU container sub-class for vfio-user. */ +typedef struct VFIOUserContainer { +VFIOContainerBase bcontainer; +}VFIOUserContainer ; + +OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserContainer,VFIO_IOMMU_USER ); + +#endif /* HW_VFIO_USER_CONTAINER_H */
include/hw/vfio/vfio-container-base.h +1 -0
@@ -109,6 +109,7 @@ vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer)#define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"#define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"#define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd" +#define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user"OBJECT_DECLARE_TYPE (VFIOContainerBase, VFIOIOMMUClass,VFIO_IOMMU )
hw/vfio-user/container.c new file +212 -0
212 lines not on this page · raw message
hw/vfio-user/pci.c new file +155 -0
155 lines not on this page · raw message
hw/meson.build +1 -0
@@ -39,6 +39,7 @@ subdir('uefi') subdir('ufs') subdir('usb') subdir('vfio') +subdir('vfio-user') subdir('virtio') subdir('vmapple') subdir('watchdog')
hw/vfio-user/meson.build new file +9 -0
@@ -0,0 +1,9 @@ +vfio_user_ss = ss.source_set() +vfio_user_ss.add(files( + 'container.c', + 'pci.c', +)) + +if get_option('vfio_user_client').enabled() + specific_ss.add_all(vfio_user_ss) +endif
meson_options.txt +2 -0
@@ -109,6 +109,8 @@ option('multiprocess', type: 'feature', value: 'auto', description: 'Out of process device emulation support') option('relocatable', type : 'boolean', value : true, description: 'toggle relocatable install') +option('vfio_user_client', type: 'feature', value: 'disabled', + description: 'vfio-user client support') option('vfio_user_server', type: 'feature', value: 'disabled', description: 'vfio-user server support') option('dbus_display', type: 'feature', value: 'auto',
scripts/meson-buildoptions.sh +4 -0
@@ -201,6 +201,8 @@ meson_options_help() { printf "%s\n" ' vdi vdi image format support' printf "%s\n" ' vduse-blk-export' printf "%s\n" ' VDUSE block export support' + printf "%s\n" ' vfio-user-client' + printf "%s\n" ' vfio-user client support' printf "%s\n" ' vfio-user-server' printf "%s\n" ' vfio-user server support' printf "%s\n" ' vhdx vhdx image format support'
@@ -529,6 +531,8 @@ _meson_option_parse() { --disable-vdi) printf "%s" -Dvdi=disabled ;; --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;; --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;; + --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;; + --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;; --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;; --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;; --enable-vhdx) printf "%s" -Dvhdx=enabled ;;
Signature · 1 line
2.43.0
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
Quoted 49 lines · John Levon
Introduce basic plumbing for vfio-user behind a new --enable-vfio-user-client option.
We introduce VFIOUserContainer in
hw/vfio-user/container.c, which is a container type for the "IOMMU" type "vfio-iommu-user", and share some common container code fromhw/vfio/container.c.Add
hw/vfio-user/pci.cfor instantiating VFIOUserPCIDevice objects, sharing some common code fromhw/vfio/pci.c.Originally-by John Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>Signed-off-by John Levon <[email protected]>--- MAINTAINERS | 2 + hw/vfio-user/container.h | 25 +++ include/hw/vfio/vfio-container-base.h | 1 + hw/vfio-user/container.c | 212 ++++++++++++++++++++++++++ hw/vfio-user/pci.c | 155 +++++++++++++++++++ hw/meson.build | 1 + hw/vfio-user/meson.build | 9 ++ meson_options.txt | 2 + scripts/meson-buildoptions.sh | 4 + 9 files changed, 411 insertions(+)create mode 100644
hw/vfio-user/container.h
create mode 100644hw/vfio-user/container.c
create mode 100644hw/vfio-user/pci.c
create mode 100644 hw/vfio-user/meson.buildMAINTAINERS +2
-0@@ -4252,6 +4252,8 @@ M: John Levon <[email protected]> M: Thanos Makatos <[email protected]> S: Supported F: docs/devel/vfio-user.rst +F: hw/vfio-user/* +F: include/hw/vfio-user/* F: subprojects/libvfio-user EBPF:hw/vfio-user/container.h new file +0
-0
hw/vfio-user/container.h line 5
Quoted 5 lines
hw/vfio-user/container.h new file +5
-0@@ -0,0 +1,25 @@ +/* +* vfio-user specific definitions. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory.
Please drop the license boiler plate. This is redundant with SPDX.
Quoted 463 lines
hw/vfio-user/container.h new file +18
-0+* SPDX-License-Identifier: GPL-2.0-or-later + */> + +#ifndef HW_VFIO_USER_CONTAINER_H +#define HW_VFIO_USER_CONTAINER_H + +#include <inttypes.h> +#include <stdbool.h> + +#include "hw/vfio/vfio-container-base.h" + +/* MMU container sub-class for vfio-user. */ +typedef struct VFIOUserContainer { +VFIOContainerBase bcontainer; +}VFIOUserContainer ; + +OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserContainer,VFIO_IOMMU_USER ); + +#endif /* HW_VFIO_USER_CONTAINER_H */ include/hw/vfio/vfio-container-base.h +1
-0@@ -109,6 +109,7 @@ vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer)#define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"#define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"#define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd" +#define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user"OBJECT_DECLARE_TYPE (VFIOContainerBase, VFIOIOMMUClass,VFIO_IOMMU )hw/vfio-user/container.c new file +212
-0@@ -0,0 +1,212 @@ +/* +* Container for vfio-user IOMMU type: rather than communicating with the kernel +* vfio driver, we communicate over a socket to a server using the vfio-user +* protocol. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include <linux/vfio.h> +#include "qemu/osdep.h" + +#include "hw/vfio-user/container.h" +#include "hw/vfio/vfio-cpr.h" +#include "hw/vfio/vfio-device.h" +#include "hw/vfio/vfio-listener.h" +#include "qapi/error.h" +#include "trace.h" + +static int vfio_user_dma_unmap (const VFIOContainerBase *bcontainer, +hwaddr iova,ram_addr_t size, +IOMMUTLBEntry *iotlb,bool unmap_all) +{ +return -ENOTSUP ; +} + +static int vfio_user_dma_map (const VFIOContainerBase *bcontainer,hwaddr iova, +ram_addr_t size,void *vaddr,bool readonly, +MemoryRegion *mrp) +{ +return -ENOTSUP ; +} + +static int +vfio_user_set_dirty_page_tracking (const VFIOContainerBase *bcontainer, +bool start,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static int vfio_user_query_dirty_bitmap (const VFIOContainerBase *bcontainer, +VFIOBitmap *vbmap,hwaddr iova, +hwaddr size,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static bool vfio_user_setup (VFIOContainerBase *bcontainer,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static VFIOUserContainer *vfio_user_create_container (Error **errp) +{ +VFIOUserContainer *container; + + container =VFIO_IOMMU_USER (object_new (TYPE_VFIO_IOMMU_USER )); +return container; +} + +/* +* Try to mirror vfio_container_connect() as much as possible. +*/ +static VFIOUserContainer * +vfio_user_container_connect (AddressSpace *as,Error **errp) +{ +VFIOContainerBase *bcontainer; +VFIOUserContainer *container; +VFIOAddressSpace *space; +VFIOIOMMUClass *vioc; + + space =vfio_address_space_get (as); + + container =vfio_user_create_container (errp); +if (!container) { + goto put_space_exit; + } + + bcontainer = &container->bcontainer; + +if (!vfio_cpr_register_container (bcontainer, errp)) { + goto free_container_exit; + } + + vioc =VFIO_IOMMU_GET_CLASS (bcontainer); +assert (vioc->setup); + +if (!vioc->setup (bcontainer, errp)) { + goto unregister_container_exit; + } + +vfio_address_space_insert (space, bcontainer); + +if (!vfio_listener_register (bcontainer, errp)) { + goto listener_release_exit; + } + + bcontainer->initialized = true; + +return container; + +listener_release_exit: +vfio_listener_unregister (bcontainer); +if (vioc->release) { + vioc->release (bcontainer); + } + +unregister_container_exit: +vfio_cpr_unregister_container (bcontainer); + +free_container_exit: +object_unref (container); + +put_space_exit: +vfio_address_space_put (space); + +return NULL ; +} + +static void vfio_user_container_disconnect (VFIOUserContainer *container) +{ +VFIOContainerBase *bcontainer = &container->bcontainer; +VFIOIOMMUClass *vioc =VFIO_IOMMU_GET_CLASS (bcontainer); + +vfio_listener_unregister (bcontainer); +if (vioc->release) { + vioc->release (bcontainer); + } + +VFIOAddressSpace *space = bcontainer->space; + +vfio_cpr_unregister_container (bcontainer); +object_unref (container); + +vfio_address_space_put (space); +} + +static bool vfio_user_device_get (VFIOUserContainer *container, +VFIODevice *vbasedev,Error **errp) +{ +struct vfio_device_info info = {0 }; + + vbasedev->fd =-1 ; + +vfio_device_prepare (vbasedev, &container->bcontainer, &info); + +return true; +} + +/* +* vfio_user_device_attach: attach a device to a new container. +*/ +static bool vfio_user_device_attach (const char *name,VFIODevice *vbasedev, +AddressSpace *as,Error **errp) +{ +VFIOUserContainer *container; + + container =vfio_user_container_connect (as, errp); +if (container ==NULL ) { +error_prepend (errp,"failed to connect proxy" ); +return false; + } + +return vfio_user_device_get (container, vbasedev, errp); +} + +static void vfio_user_device_detach (VFIODevice *vbasedev) +{ +VFIOUserContainer *container =container_of (vbasedev->bcontainer, + VFIOUserContainer, bcontainer); + +vfio_device_unprepare (vbasedev); + +vfio_user_container_disconnect (container); +} + +static int vfio_user_pci_hot_reset (VFIODevice *vbasedev,bool single) +{ +/* ->needs_reset is always false for vfio-user. */ +return 0 ; +} + +static void vfio_iommu_user_class_init (ObjectClass *klass,const void *data) +{ +VFIOIOMMUClass *vioc =VFIO_IOMMU_CLASS (klass); + + vioc->setup = vfio_user_setup; + vioc->dma_map = vfio_user_dma_map; + vioc->dma_unmap = vfio_user_dma_unmap; + vioc->attach_device = vfio_user_device_attach; + vioc->detach_device = vfio_user_device_detach; + vioc->set_dirty_page_tracking = vfio_user_set_dirty_page_tracking; + vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap; + vioc->pci_hot_reset = vfio_user_pci_hot_reset; +}; + +static const TypeInfo types[] = { + { + .name =TYPE_VFIO_IOMMU_USER , + .parent =TYPE_VFIO_IOMMU , + .instance_size =sizeof (VFIOUserContainer), + .class_init = vfio_iommu_user_class_init, + }, +}; + +DEFINE_TYPES (types)hw/vfio-user/pci.c new file +155
-0@@ -0,0 +1,155 @@ +/* +* vfio PCI device over a UNIX socket. +* +* Copyright © 2018, 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include "qemu/osdep.h" + +#include "hw/qdev-properties.h" +#include "hw/vfio/pci.h" + +#define TYPE_VFIO_USER_PCI "vfio-user-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) + +struct VFIOUserPCIDevice { +VFIOPCIDevice device; +char *sock_name; +}; + +/* +* Emulated devices don't use host hot reset +*/ +static void vfio_user_compute_needs_reset (VFIODevice *vbasedev) +{ + vbasedev->needs_reset = false; +} + +static Object *vfio_user_pci_get_object (VFIODevice *vbasedev) +{ +VFIOUserPCIDevice *vdev =container_of (vbasedev, VFIOUserPCIDevice, + device.vbasedev); + +return OBJECT (vdev); +} + +static VFIODeviceOps vfio_user_pci_ops = { + .vfio_compute_needs_reset = vfio_user_compute_needs_reset, + .vfio_eoi = vfio_pci_intx_eoi, + .vfio_get_object = vfio_user_pci_get_object, +/* No live migration support yet. */ + .vfio_save_config =NULL , + .vfio_load_config =NULL , +}; + +static void vfio_user_pci_realize (PCIDevice *pdev,Error **errp) +{ +ERRP_GUARD (); +VFIOUserPCIDevice *udev =VFIO_USER_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev); +VFIODevice *vbasedev = &vdev->vbasedev; +AddressSpace *as; + +/* +* TODO: make option parser understand SocketAddress +* and use that instead of having scalar options +* for each socket type. +*/ +if (!udev->sock_name) { +error_setg (errp,"No socket specified" ); +error_append_hint (errp,"Use -device vfio-user-pci,socket=<name>\n" ); +return ; + } + + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); + +/* +* vfio-user devices are effectively mdevs (don't use a host iommu). +*/ + vbasedev->mdev = true; + + as =pci_device_iommu_address_space (pdev); +if (!vfio_device_attach_by_iommu_type (TYPE_VFIO_IOMMU_USER , + vbasedev->name, vbasedev, + as, errp)) { +error_prepend (errp,VFIO_MSG_PREFIX , vbasedev->name); +return ; + } +} + +static void vfio_user_instance_init (Object *obj) +{ +PCIDevice *pci_dev =PCI_DEVICE (obj); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); +VFIODevice *vbasedev = &vdev->vbasedev; + +device_add_bootindex_property (obj, &vdev->bootindex, +"bootindex" ,NULL , + &pci_dev->qdev); + vdev->host.domain = ~0U ; + vdev->host.bus = ~0U ; + vdev->host.slot = ~0U ; + vdev->host.function = ~0U ; + +vfio_device_init (vbasedev,VFIO_DEVICE_TYPE_PCI , &vfio_user_pci_ops, +DEVICE (vdev), false); + + vdev->nv_gpudirect_clique =0xFF ; + +/* +* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command +* line, therefore, no need to wait to realize like other devices. +*/ + pci_dev->cap_present |=QEMU_PCI_CAP_EXPRESS ; +} + +static void vfio_user_instance_finalize (Object *obj) +{ +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); + +vfio_pci_put_device (vdev); +} + +static const Property vfio_user_pci_dev_properties[] = { +DEFINE_PROP_UINT32 ("x-pci-vendor-id" , VFIOPCIDevice, + vendor_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-device-id" , VFIOPCIDevice, + device_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-sub-vendor-id" , VFIOPCIDevice, + sub_vendor_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-sub-device-id" , VFIOPCIDevice, + sub_device_id,PCI_ANY_ID ), +DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +}; + +static void vfio_user_pci_dev_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *pdc =PCI_DEVICE_CLASS (klass); + +device_class_set_props (dc, vfio_user_pci_dev_properties); + dc->desc ="VFIO over socket PCI device assignment" ; + pdc->realize = vfio_user_pci_realize; +} + +static const TypeInfo vfio_user_pci_dev_info = { + .name =TYPE_VFIO_USER_PCI , + .parent =TYPE_VFIO_PCI_BASE , + .instance_size =sizeof (VFIOUserPCIDevice), + .class_init = vfio_user_pci_dev_class_init, + .instance_init = vfio_user_instance_init, + .instance_finalize = vfio_user_instance_finalize, +}; + +static void register_vfio_user_dev_type (void ) +{ +type_register_static (&vfio_user_pci_dev_info); +} + +type_init (register_vfio_user_dev_type)hw/meson.build +1
-0@@ -39,6 +39,7 @@ subdir('uefi') subdir('ufs') subdir('usb') subdir('vfio') +subdir('vfio-user') subdir('virtio') subdir('vmapple') subdir('watchdog')hw/vfio-user/meson.build new file +9
-0@@ -0,0 +1,9 @@ +vfio_user_ss = ss.source_set() +vfio_user_ss.add(files( + 'container.c', + 'pci.c', +)) + +if get_option('vfio_user_client').enabled() + specific_ss.add_all(vfio_user_ss) +endifmeson_options.txt +2
-0@@ -109,6 +109,8 @@ option('multiprocess', type: 'feature', value: 'auto', description: 'Out of process device emulation support') option('relocatable', type : 'boolean', value : true, description: 'toggle relocatable install') +option('vfio_user_client', type: 'feature', value: 'disabled', + description: 'vfio-user client support') option('vfio_user_server', type: 'feature', value: 'disabled', description: 'vfio-user server support') option('dbus_display', type: 'feature', value: 'auto',scripts/meson-buildoptions.sh +2
-0@@ -201,6 +201,8 @@ meson_options_help() { printf "%s\n" ' vdi vdi image format support' printf "%s\n" ' vduse-blk-export' printf "%s\n" ' VDUSE block export support' + printf "%s\n" ' vfio-user-client' + printf "%s\n" ' vfio-user client support' printf "%s\n" ' vfio-user-server' printf "%s\n" ' vfio-user server support' printf "%s\n" ' vhdx vhdx image format support'
scripts/meson-buildoptions.sh _meson_option_parse() { · line 538
Quoted 8 lines
scripts/meson-buildoptions.sh +2
-0@@ -529,6 +531,8 @@ _meson_option_parse() { --disable-vdi) printf "%s" -Dvdi=disabled ;; --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;; --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;; + --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;; + --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;; --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;; --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;; --enable-vhdx) printf "%s" -Dvhdx=enabled ;;
can't we simply have a CONFIG option and select the device on platforms supporting it ?
Thanks,
C.
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
Quoted 4 lines · Cédric Le Goater
Quoted 4 lines
scripts/meson-buildoptions.sh +0
-0@@ -529,6 +531,8 @@ _meson_option_parse() { --disable-vdi) printf "%s" -Dvdi=disabled ;; --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;; --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;;
Quoted 8 lines
scripts/meson-buildoptions.sh _meson_option_parse() { · line 538
Quoted 5 lines
scripts/meson-buildoptions.sh +2
-0+ --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;; + --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;; --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;; --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;; --enable-vhdx) printf "%s" -Dvhdx=enabled ;;can't we simply have a CONFIG option and select the device on platforms supporting it ?
You mean always build vfio-user client rather than optionally? Why would it be different from other optional components?
AFAIK all platforms (at least in theory) would support it.
regards
john
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
Quoted 15 lines · John Levon
Quoted 4 lines · Cédric Le Goater
Quoted 4 lines
scripts/meson-buildoptions.sh +0
-0@@ -529,6 +531,8 @@ _meson_option_parse() { --disable-vdi) printf "%s" -Dvdi=disabled ;; --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;; --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;;Quoted 8 lines
scripts/meson-buildoptions.sh _meson_option_parse() { · line 538
Quoted 5 lines
scripts/meson-buildoptions.sh +2
-0+ --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;; + --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;; --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;; --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;; --enable-vhdx) printf "%s" -Dvhdx=enabled ;;can't we simply have a CONFIG option and select the device on platforms supporting it ?
Quoted 2 lines
You mean always build vfio-user client rather than optionally? Why would it be different from other optional components?
why would it be optional ? I don't see any dependency for vfio-user-client. vfio-user-server depends on libvfio-user [1]. Not vfio-user-client. Should it ?
Does this mean we are abandoning libvfio-user ? Sorry I am not familiar with this framework.
Quoted 1 line
AFAIK all platforms (at least in theory) would support it.
So what would be the reason for not compiling it ? It is not different from VFIO AFAICT.
Thanks,
C.
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
Quoted 9 lines · Cédric Le Goater
Quoted 5 lines
Quoted 2 lines
can't we simply have a CONFIG option and select the device on platforms supporting it ?
You mean always build vfio-user client rather than optionally? Why would it be different from other optional components?
why would it be optional ? I don't see any dependency for vfio-user-client. vfio-user-server depends on libvfio-user [1]. Not vfio-user-client. Should it ?
libvfio-user is for the server side, you are correct that the client does not depend on it at all. (It's still actively in use.)
Quoted 4 lines
Quoted 1 line
AFAIK all platforms (at least in theory) would support it.
So what would be the reason for not compiling it ? It is not different from VFIO AFAICT.
OK, thanks. I simply didn't know qemu policy on building things in by default. Of course I would be very happy to see it built into every qemu :)
regards
john
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
In reply to John LevonQuoted 35 lines · John Levon
Introduce basic plumbing for vfio-user behind a new --enable-vfio-user-client option.
We introduce VFIOUserContainer in
hw/vfio-user/container.c, which is a container type for the "IOMMU" type "vfio-iommu-user", and share some common container code fromhw/vfio/container.c.Add
hw/vfio-user/pci.cfor instantiating VFIOUserPCIDevice objects, sharing some common code fromhw/vfio/pci.c.Originally-by John Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>Signed-off-by John Levon <[email protected]>--- MAINTAINERS | 2 + hw/vfio-user/container.h | 25 +++ include/hw/vfio/vfio-container-base.h | 1 + hw/vfio-user/container.c | 212 ++++++++++++++++++++++++++ hw/vfio-user/pci.c | 155 +++++++++++++++++++ hw/meson.build | 1 + hw/vfio-user/meson.build | 9 ++ meson_options.txt | 2 + scripts/meson-buildoptions.sh | 4 + 9 files changed, 411 insertions(+)create mode 100644
hw/vfio-user/container.h
create mode 100644hw/vfio-user/container.c
create mode 100644hw/vfio-user/pci.c
create mode 100644 hw/vfio-user/meson.buildMAINTAINERS +0
-0
MAINTAINERS M: John Levon <[email protected]> · line 4257
Quoted 6 lines
MAINTAINERS +2
-0@@ -4252,6 +4252,8 @@ M: John Levon <[email protected]> M: Thanos Makatos <[email protected]> S: Supported F: docs/devel/vfio-user.rst +F: hw/vfio-user/* +F: include/hw/vfio-user/* F: subprojects/libvfio-user
Question: how do you see the division between hw/vfio and hw/vfio-user? My initial feeling is that there is substantial sharing between the two, in which case I'd expect the files to be in hw/vfio as e.g. hw/vfio/container-user.c etc. instead of its own directory.
Cédric, what are your thoughts here?
Quoted 11 lines
MAINTAINERS +0
-0EBPF:hw/vfio-user/container.h new file +4
-0@@ -0,0 +1,25 @@ +/* +* vfio-user specific definitions. +* +* This work is licensed under the terms of the GNU GPL, version 2. See
hw/vfio-user/container.h line 14
Quoted 10 lines
hw/vfio-user/container.h new file +10
-0+* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VFIO_USER_CONTAINER_H +#define HW_VFIO_USER_CONTAINER_H + +#include <inttypes.h> +#include <stdbool.h>
These shouldn't be included directly: you probably just want:
#include "qemu/osdep.h"
instead.
hw/vfio-user/container.h line 23
Quoted 8 lines
hw/vfio-user/container.h new file +8
-0+#include "hw/vfio/vfio-container-base.h" + +/* MMU container sub-class for vfio-user. */ +typedef struct VFIOUserContainer { +VFIOContainerBase bcontainer; +}VFIOUserContainer ; + +OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserContainer,VFIO_IOMMU_USER );
As per the documentation at https://qemu-project.gitlab.io/qemu/devel/style.html#qemu-object-model-declarations the parent object should always be named parent_obj and struct shouldn't have a typedef i.e.
/* MMU container sub-class for vfio-user. */
struct VFIOUserContainer {
VFIOContainerBase parent_obj;
};OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserContainer, VFIO_IOMMU_USER);
Quoted 94 lines
hw/vfio-user/container.h new file +1
-0+#endif /* HW_VFIO_USER_CONTAINER_H */ include/hw/vfio/vfio-container-base.h +1
-0@@ -109,6 +109,7 @@ vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer)#define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"#define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"#define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd" +#define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user"OBJECT_DECLARE_TYPE (VFIOContainerBase, VFIOIOMMUClass,VFIO_IOMMU )hw/vfio-user/container.c new file +75
-0@@ -0,0 +1,212 @@ +/* +* Container for vfio-user IOMMU type: rather than communicating with the kernel +* vfio driver, we communicate over a socket to a server using the vfio-user +* protocol. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include <linux/vfio.h> +#include "qemu/osdep.h" + +#include "hw/vfio-user/container.h" +#include "hw/vfio/vfio-cpr.h" +#include "hw/vfio/vfio-device.h" +#include "hw/vfio/vfio-listener.h" +#include "qapi/error.h" +#include "trace.h" + +static int vfio_user_dma_unmap (const VFIOContainerBase *bcontainer, +hwaddr iova,ram_addr_t size, +IOMMUTLBEntry *iotlb,bool unmap_all) +{ +return -ENOTSUP ; +} + +static int vfio_user_dma_map (const VFIOContainerBase *bcontainer,hwaddr iova, +ram_addr_t size,void *vaddr,bool readonly, +MemoryRegion *mrp) +{ +return -ENOTSUP ; +} + +static int +vfio_user_set_dirty_page_tracking (const VFIOContainerBase *bcontainer, +bool start,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static int vfio_user_query_dirty_bitmap (const VFIOContainerBase *bcontainer, +VFIOBitmap *vbmap,hwaddr iova, +hwaddr size,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static bool vfio_user_setup (VFIOContainerBase *bcontainer,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static VFIOUserContainer *vfio_user_create_container (Error **errp) +{ +VFIOUserContainer *container; + + container =VFIO_IOMMU_USER (object_new (TYPE_VFIO_IOMMU_USER )); +return container; +} + +/* +* Try to mirror vfio_container_connect() as much as possible. +*/ +static VFIOUserContainer * +vfio_user_container_connect (AddressSpace *as,Error **errp) +{ +VFIOContainerBase *bcontainer; +VFIOUserContainer *container; +VFIOAddressSpace *space;
hw/vfio-user/container.c line 85
Quoted 10 lines
hw/vfio-user/container.c new file +10
-0+VFIOIOMMUClass *vioc; + + space =vfio_address_space_get (as); + + container =vfio_user_create_container (errp); +if (!container) { + goto put_space_exit; + } + + bcontainer = &container->bcontainer;
References to the object hierarchy should always be done with the automatically generated QOM cast macros since they are easier to read, and also ensure type safety e.g.:
bcontainer = VFIO_IOMMU(container);
Quoted 32 lines
hw/vfio-user/container.c new file +32
-0+if (!vfio_cpr_register_container (bcontainer, errp)) { + goto free_container_exit; + } + + vioc =VFIO_IOMMU_GET_CLASS (bcontainer); +assert (vioc->setup); + +if (!vioc->setup (bcontainer, errp)) { + goto unregister_container_exit; + } + +vfio_address_space_insert (space, bcontainer); + +if (!vfio_listener_register (bcontainer, errp)) { + goto listener_release_exit; + } + + bcontainer->initialized = true; + +return container; + +listener_release_exit: +vfio_listener_unregister (bcontainer); +if (vioc->release) { + vioc->release (bcontainer); + } + +unregister_container_exit: +vfio_cpr_unregister_container (bcontainer); + +free_container_exit: +object_unref (container);
hw/vfio-user/container.c line 128
Quoted 10 lines
hw/vfio-user/container.c new file +10
-0+ +put_space_exit: +vfio_address_space_put (space); + +return NULL ; +} + +static void vfio_user_container_disconnect (VFIOUserContainer *container) +{ +VFIOContainerBase *bcontainer = &container->bcontainer;
Use a QOM cast here.
Quoted 13 lines
hw/vfio-user/container.c new file +13
-0+VFIOIOMMUClass *vioc =VFIO_IOMMU_GET_CLASS (bcontainer); + +vfio_listener_unregister (bcontainer); +if (vioc->release) { + vioc->release (bcontainer); + } + +VFIOAddressSpace *space = bcontainer->space; + +vfio_cpr_unregister_container (bcontainer); +object_unref (container); + +vfio_address_space_put (space);
hw/vfio-user/container.c line 151
Quoted 10 lines
hw/vfio-user/container.c new file +10
-0+} + +static bool vfio_user_device_get (VFIOUserContainer *container, +VFIODevice *vbasedev,Error **errp) +{ +struct vfio_device_info info = {0 }; + + vbasedev->fd =-1 ; + +vfio_device_prepare (vbasedev, &container->bcontainer, &info);
Use a QOM cast here.
Quoted 80 lines
hw/vfio-user/container.c new file +60
-0+return true; +} + +/* +* vfio_user_device_attach: attach a device to a new container. +*/ +static bool vfio_user_device_attach (const char *name,VFIODevice *vbasedev, +AddressSpace *as,Error **errp) +{ +VFIOUserContainer *container; + + container =vfio_user_container_connect (as, errp); +if (container ==NULL ) { +error_prepend (errp,"failed to connect proxy" ); +return false; + } + +return vfio_user_device_get (container, vbasedev, errp); +} + +static void vfio_user_device_detach (VFIODevice *vbasedev) +{ +VFIOUserContainer *container =container_of (vbasedev->bcontainer, + VFIOUserContainer, bcontainer); + +vfio_device_unprepare (vbasedev); + +vfio_user_container_disconnect (container); +} + +static int vfio_user_pci_hot_reset (VFIODevice *vbasedev,bool single) +{ +/* ->needs_reset is always false for vfio-user. */ +return 0 ; +} + +static void vfio_iommu_user_class_init (ObjectClass *klass,const void *data) +{ +VFIOIOMMUClass *vioc =VFIO_IOMMU_CLASS (klass); + + vioc->setup = vfio_user_setup; + vioc->dma_map = vfio_user_dma_map; + vioc->dma_unmap = vfio_user_dma_unmap; + vioc->attach_device = vfio_user_device_attach; + vioc->detach_device = vfio_user_device_detach; + vioc->set_dirty_page_tracking = vfio_user_set_dirty_page_tracking; + vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap; + vioc->pci_hot_reset = vfio_user_pci_hot_reset; +}; + +static const TypeInfo types[] = { + { + .name =TYPE_VFIO_IOMMU_USER , + .parent =TYPE_VFIO_IOMMU , + .instance_size =sizeof (VFIOUserContainer), + .class_init = vfio_iommu_user_class_init, + }, +}; + +DEFINE_TYPES (types)hw/vfio-user/pci.c new file +14
-0@@ -0,0 +1,155 @@ +/* +* vfio PCI device over a UNIX socket. +* +* Copyright © 2018, 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include "qemu/osdep.h" +
hw/vfio-user/pci.c line 24
Quoted 10 lines
hw/vfio-user/pci.c new file +10
-0+#include "hw/qdev-properties.h" +#include "hw/vfio/pci.h" + +#define TYPE_VFIO_USER_PCI "vfio-user-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) + +struct VFIOUserPCIDevice { +VFIOPCIDevice device; +char *sock_name; +};
Again as per the documentation link above, device should be called parent_obj plus there should be a empty line between parent_obj and the other members i.e.
struct VFIOUserPCIDevice {
VFIOPCIDevice parent_obj;
char *sock_name;
}Note that by using QOM casts the name of the parent object member is not exposed to the remainder of the code.
hw/vfio-user/pci.c line 37
Quoted 12 lines
hw/vfio-user/pci.c new file +12
-0+/* +* Emulated devices don't use host hot reset +*/ +static void vfio_user_compute_needs_reset (VFIODevice *vbasedev) +{ + vbasedev->needs_reset = false; +} + +static Object *vfio_user_pci_get_object (VFIODevice *vbasedev) +{ +VFIOUserPCIDevice *vdev =container_of (vbasedev, VFIOUserPCIDevice, + device.vbasedev);
Hmmm. Not for this series but this may suggest VFIODevice should be QOMified.
Quoted 106 lines
hw/vfio-user/pci.c new file +105
-0+return OBJECT (vdev); +} > +static VFIODeviceOps vfio_user_pci_ops = { + .vfio_compute_needs_reset = vfio_user_compute_needs_reset, + .vfio_eoi = vfio_pci_intx_eoi, + .vfio_get_object = vfio_user_pci_get_object, +/* No live migration support yet. */ + .vfio_save_config =NULL , + .vfio_load_config =NULL , +}; + +static void vfio_user_pci_realize (PCIDevice *pdev,Error **errp) +{ +ERRP_GUARD (); +VFIOUserPCIDevice *udev =VFIO_USER_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev); +VFIODevice *vbasedev = &vdev->vbasedev; +AddressSpace *as; + +/* +* TODO: make option parser understand SocketAddress +* and use that instead of having scalar options +* for each socket type. +*/ +if (!udev->sock_name) { +error_setg (errp,"No socket specified" ); +error_append_hint (errp,"Use -device vfio-user-pci,socket=<name>\n" ); +return ; + } + + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); + +/* +* vfio-user devices are effectively mdevs (don't use a host iommu). +*/ + vbasedev->mdev = true; + + as =pci_device_iommu_address_space (pdev); +if (!vfio_device_attach_by_iommu_type (TYPE_VFIO_IOMMU_USER , + vbasedev->name, vbasedev, + as, errp)) { +error_prepend (errp,VFIO_MSG_PREFIX , vbasedev->name); +return ; + } +} + +static void vfio_user_instance_init (Object *obj) +{ +PCIDevice *pci_dev =PCI_DEVICE (obj); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); +VFIODevice *vbasedev = &vdev->vbasedev; + +device_add_bootindex_property (obj, &vdev->bootindex, +"bootindex" ,NULL , + &pci_dev->qdev); + vdev->host.domain = ~0U ; + vdev->host.bus = ~0U ; + vdev->host.slot = ~0U ; + vdev->host.function = ~0U ; + +vfio_device_init (vbasedev,VFIO_DEVICE_TYPE_PCI , &vfio_user_pci_ops, +DEVICE (vdev), false); + + vdev->nv_gpudirect_clique =0xFF ; + +/* +* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command +* line, therefore, no need to wait to realize like other devices. +*/ + pci_dev->cap_present |=QEMU_PCI_CAP_EXPRESS ; +} + +static void vfio_user_instance_finalize (Object *obj) +{ +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); + +vfio_pci_put_device (vdev); +} + +static const Property vfio_user_pci_dev_properties[] = { +DEFINE_PROP_UINT32 ("x-pci-vendor-id" , VFIOPCIDevice, + vendor_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-device-id" , VFIOPCIDevice, + device_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-sub-vendor-id" , VFIOPCIDevice, + sub_vendor_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-sub-device-id" , VFIOPCIDevice, + sub_device_id,PCI_ANY_ID ), +DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +}; + +static void vfio_user_pci_dev_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *pdc =PCI_DEVICE_CLASS (klass); + +device_class_set_props (dc, vfio_user_pci_dev_properties); + dc->desc ="VFIO over socket PCI device assignment" ; + pdc->realize = vfio_user_pci_realize; +} + +static const TypeInfo vfio_user_pci_dev_info = { + .name =TYPE_VFIO_USER_PCI , + .parent =TYPE_VFIO_PCI_BASE , + .instance_size =sizeof (VFIOUserPCIDevice), + .class_init = vfio_user_pci_dev_class_init,
hw/vfio-user/pci.c line 155
Quoted 10 lines
hw/vfio-user/pci.c new file +10
-0+ .instance_init = vfio_user_instance_init, + .instance_finalize = vfio_user_instance_finalize, +}; + +static void register_vfio_user_dev_type (void ) +{ +type_register_static (&vfio_user_pci_dev_info); +} + +type_init (register_vfio_user_dev_type)
Use DEFINE_TYPES as you've already done above instead of type_init() here.
Quoted 54 lines
hw/meson.build +1
-0@@ -39,6 +39,7 @@ subdir('uefi') subdir('ufs') subdir('usb') subdir('vfio') +subdir('vfio-user') subdir('virtio') subdir('vmapple') subdir('watchdog')hw/vfio-user/meson.build new file +9
-0@@ -0,0 +1,9 @@ +vfio_user_ss = ss.source_set() +vfio_user_ss.add(files( + 'container.c', + 'pci.c', +)) + +if get_option('vfio_user_client').enabled() + specific_ss.add_all(vfio_user_ss) +endifmeson_options.txt +2
-0@@ -109,6 +109,8 @@ option('multiprocess', type: 'feature', value: 'auto', description: 'Out of process device emulation support') option('relocatable', type : 'boolean', value : true, description: 'toggle relocatable install') +option('vfio_user_client', type: 'feature', value: 'disabled', + description: 'vfio-user client support') option('vfio_user_server', type: 'feature', value: 'disabled', description: 'vfio-user server support') option('dbus_display', type: 'feature', value: 'auto',scripts/meson-buildoptions.sh +2
-0@@ -201,6 +201,8 @@ meson_options_help() { printf "%s\n" ' vdi vdi image format support' printf "%s\n" ' vduse-blk-export' printf "%s\n" ' VDUSE block export support' + printf "%s\n" ' vfio-user-client' + printf "%s\n" ' vfio-user client support' printf "%s\n" ' vfio-user-server' printf "%s\n" ' vfio-user server support' printf "%s\n" ' vhdx vhdx image format support'
scripts/meson-buildoptions.sh _meson_option_parse() { · line 538
Quoted 8 lines
scripts/meson-buildoptions.sh +2
-0@@ -529,6 +531,8 @@ _meson_option_parse() { --disable-vdi) printf "%s" -Dvdi=disabled ;; --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;; --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;; + --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;; + --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;; --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;; --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;; --enable-vhdx) printf "%s" -Dvhdx=enabled ;;
ATB,
Mark.
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
Quoted 4 lines · Mark Cave-Ayland
Question: how do you see the division between hw/vfio and hw/vfio-user? My initial feeling is that there is substantial sharing between the two, in which case I'd expect the files to be in hw/vfio as e.g.
hw/vfio/container-user.cetc. instead of its own directory.
That was also in the earlier patchsets! Cédric asked for hw/vfio-user - and I think I actually prefer it myself. The amount we export from hw/vfio is actually fairly minimal (now).
Quoted 13 lines
hw/vfio-user/container.h line 23
Quoted 8 lines
hw/vfio-user/container.h new file +8
-0+#include "hw/vfio/vfio-container-base.h" + +/* MMU container sub-class for vfio-user. */ +typedef struct VFIOUserContainer { +VFIOContainerBase bcontainer; +}VFIOUserContainer ; + +OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserContainer,VFIO_IOMMU_USER );As per the documentation at https://qemu-project.gitlab.io/qemu/devel/style.html#qemu-object-model-declarations
the parent object should always be namedparent_objand struct shouldn't
have a typedef i.e.
Quoted 6 lines
/* MMU container sub-class for vfio-user. */
struct VFIOUserContainer { VFIOContainerBase parent_obj; };
OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserContainer, VFIO_IOMMU_USER);
I don't think I want to diverge from VFIOContainer here, though, right?
Quoted 21 lines
Quoted 6 lines
hw/vfio-user/container.c new file +6
-0+static VFIOUserContainer * +vfio_user_container_connect (AddressSpace *as,Error **errp) +{ +VFIOContainerBase *bcontainer; +VFIOUserContainer *container; +VFIOAddressSpace *space;hw/vfio-user/container.c line 85
Quoted 10 lines
hw/vfio-user/container.c new file +10
-0+VFIOIOMMUClass *vioc; + + space =vfio_address_space_get (as); + + container =vfio_user_create_container (errp); +if (!container) { + goto put_space_exit; + } + + bcontainer = &container->bcontainer;References to the object hierarchy should always be done with the automatically generated QOM cast macros since they are easier to read, and also ensure type safety e.g.:
Quoted 1 line
bcontainer = VFIO_IOMMU(container);
Ditto. Sounds like a fine future cleanup that should be applied to all the code, but for now, and for review reasons, I'd prefer to be "the same" as hw/vfio/
Quoted 9 lines
hw/vfio-user/pci.c line 24
Quoted 4 lines
hw/vfio-user/pci.c new file +4
-0+struct VFIOUserPCIDevice { +VFIOPCIDevice device; +char *sock_name; +};Again as per the documentation link above, device should be called
parent_objplus there should be a empty line betweenparent_objand the other members i.e.
Quoted 8 lines
struct VFIOUserPCIDevice { VFIOPCIDevice parent_obj; char *sock_name; }Note that by using QOM casts the name of the parent object member is not exposed to the remainder of the code.
I can make this change, though, as there's no vfio equivalent, if Cédric thinks I should too.
Quoted 8 lines
hw/vfio-user/pci.c line 155
Quoted 6 lines
hw/vfio-user/pci.c new file +6
-0+static void register_vfio_user_dev_type (void ) +{ +type_register_static (&vfio_user_pci_dev_info); +} + +type_init (register_vfio_user_dev_type)Use
DEFINE_TYPESas you've already done above instead oftype_init()here.
Again, same as hw/vfio/pci.c
regards
john
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
Quoted 10 lines · John Levon
Quoted 4 lines · Mark Cave-Ayland
Question: how do you see the division between hw/vfio and hw/vfio-user? My initial feeling is that there is substantial sharing between the two, in which case I'd expect the files to be in hw/vfio as e.g.
hw/vfio/container-user.cetc. instead of its own directory.That was also in the earlier patchsets! Cédric asked for hw/vfio-user - and I think I actually prefer it myself. The amount we export from hw/vfio is actually fairly minimal (now).
yes. It looks much better. The interfaces between the core VFIO framework and the new vfio-user-pci device are clear. This is easier for maintenance too.
Thanks,
C.
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
In reply to Mark Cave-AylandQuoted 46 lines · Mark Cave-Ayland
Quoted 40 lines · John Levon
Introduce basic plumbing for vfio-user behind a new --enable-vfio-user-client option.
We introduce VFIOUserContainer in
hw/vfio-user/container.c, which is a container type for the "IOMMU" type "vfio-iommu-user", and share some common container code fromhw/vfio/container.c.Add
hw/vfio-user/pci.cfor instantiating VFIOUserPCIDevice objects, sharing some common code fromhw/vfio/pci.c.Originally-by John Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>Signed-off-by John Levon <[email protected]>---
MAINTAINERS | 2 + hw/vfio-user/container.h | 25 +++include/hw/vfio/vfio-container-base.h| 1 + hw/vfio-user/container.c | 212 ++++++++++++++++++++++++++ hw/vfio-user/pci.c | 155 +++++++++++++++++++ hw/meson.build | 1 + hw/vfio-user/meson.build | 9 ++ meson_options.txt | 2 + scripts/meson-buildoptions.sh | 4 +9 files changed, 411 insertions(+)create mode 100644
hw/vfio-user/container.h
create mode 100644hw/vfio-user/container.c
create mode 100644hw/vfio-user/pci.c
create mode 100644 hw/vfio-user/meson.buildMAINTAINERS +0
-0@@ -4252,6 +4252,8 @@ M: John Levon <[email protected]>M: Thanos Makatos <[email protected]>
S: Supported
F:docs/devel/vfio-user.rstMAINTAINERS +2
-0+F: hw/vfio-user/* +F: include/hw/vfio-user/*Quoted 1 line
F: subprojects/libvfio-user
Question: how do you see the division between hw/vfio and hw/vfio-user? My initial feeling is that there is substantial sharing between the two, in which case I'd expect the files to be in hw/vfio as e.g.
hw/vfio/container-user.cetc. instead of its own directory.
Quoted 1 line
Cédric, what are your thoughts here?
The vfio devices are very dependent on the bus they depend on. My idea is to keep under hw/vfio the initial implementation of the vfio-pci device and the common services for other "complex" vfio devices like vfio-user-pci. Which give us :
hw/vfio/* 17323 lines hw/vfio-user/* 2955 lines
vfio-ap and vfio-ccw are small but I might still move them under s390x. vfio-platform will be removed in the next QEMU cycle.
Also, util/vfio-helpers.c, on which is built the nvme BlockDriver, should be merged under the common services, so under hw/vfio.
Thanks,
C.
Quoted 528 lines
Quoted 12 lines
EBPF:
hw/vfio-user/container.h new file +4
-0@@ -0,0 +1,25 @@ +/* +* vfio-user specific definitions. +* +* This work is licensed under the terms of the GNU GPL, version 2. See hw/vfio-user/container.h line 14
Quoted 10 lines
hw/vfio-user/container.h new file +10
-0+* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VFIO_USER_CONTAINER_H +#define HW_VFIO_USER_CONTAINER_H + +#include <inttypes.h> +#include <stdbool.h> These shouldn't be included directly: you probably just want:
#include "
qemu/osdep.h"instead.
hw/vfio-user/container.h line 23
Quoted 8 lines
hw/vfio-user/container.h new file +8
-0+#include "hw/vfio/vfio-container-base.h" + +/* MMU container sub-class for vfio-user. */ +typedef struct VFIOUserContainer { + VFIOContainerBase bcontainer; +}VFIOUserContainer ; + +OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserContainer,VFIO_IOMMU_USER );As per the documentation at https://qemu-project.gitlab.io/qemu/devel/style.html#qemu-object-model-declarations the parent object should always be named
parent_objand struct shouldn't have a typedef i.e./* MMU container sub-class for vfio-user. */
struct VFIOUserContainer {
VFIOContainerBaseparent_obj;
};
OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserContainer, VFIO_IOMMU_USER);Quoted 92 lines
hw/vfio-user/container.h new file +1
-0+#endif /* HW_VFIO_USER_CONTAINER_H */ include/hw/vfio/vfio-container-base.h +1
-0@@ -109,6 +109,7 @@ vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer) #define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy" #define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr" #define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd" +#define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user" OBJECT_DECLARE_TYPE(VFIOContainerBase, VFIOIOMMUClass, VFIO_IOMMU)hw/vfio-user/container.c new file +75
-0@@ -0,0 +1,212 @@ +/* +* Container for vfio-user IOMMU type: rather than communicating with the kernel +* vfio driver, we communicate over a socket to a server using the vfio-user +* protocol. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include <linux/vfio.h> +#include "qemu/osdep.h" + +#include "hw/vfio-user/container.h" +#include "hw/vfio/vfio-cpr.h" +#include "hw/vfio/vfio-device.h" +#include "hw/vfio/vfio-listener.h" +#include "qapi/error.h" +#include "trace.h" + +static int vfio_user_dma_unmap (const VFIOContainerBase *bcontainer, + hwaddr iova, ram_addr_t size, + IOMMUTLBEntry *iotlb, bool unmap_all) +{ + return -ENOTSUP; +} + +static int vfio_user_dma_map (const VFIOContainerBase *bcontainer,hwaddr iova, + ram_addr_t size, void *vaddr, bool readonly, + MemoryRegion *mrp) +{ + return -ENOTSUP; +} + +static int +vfio_user_set_dirty_page_tracking (const VFIOContainerBase *bcontainer, + bool start, Error **errp) +{ + error_setg_errno(errp, ENOTSUP, "Not supported"); + return -ENOTSUP; +} + +static int vfio_user_query_dirty_bitmap (const VFIOContainerBase *bcontainer, + VFIOBitmap *vbmap, hwaddr iova, + hwaddr size, Error **errp) +{ + error_setg_errno(errp, ENOTSUP, "Not supported"); + return -ENOTSUP; +} + +static bool vfio_user_setup (VFIOContainerBase *bcontainer,Error **errp) +{ + error_setg_errno(errp, ENOTSUP, "Not supported"); + return -ENOTSUP; +} + +static VFIOUserContainer *vfio_user_create_container (Error **errp) +{ + VFIOUserContainer *container; + + container = VFIO_IOMMU_USER(object_new(TYPE_VFIO_IOMMU_USER)); + return container; +} + +/* +* Try to mirror vfio_container_connect() as much as possible. +*/ +static VFIOUserContainer * +vfio_user_container_connect (AddressSpace *as,Error **errp) +{ + VFIOContainerBase *bcontainer; + VFIOUserContainer *container; + VFIOAddressSpace *space;hw/vfio-user/container.c line 85
Quoted 10 lines
hw/vfio-user/container.c new file +10
-0+ VFIOIOMMUClass *vioc; + + space = vfio_address_space_get(as); + + container = vfio_user_create_container(errp); + if (!container) { + goto put_space_exit; + } + + bcontainer = &container->bcontainer;References to the object hierarchy should always be done with the automatically generated QOM cast macros since they are easier to read, and also ensure type safety e.g.:
bcontainer =
VFIO_IOMMU(container);Quoted 32 lines
hw/vfio-user/container.c new file +32
-0+ if (!vfio_cpr_register_container(bcontainer, errp)) { + goto free_container_exit; + } + + vioc = VFIO_IOMMU_GET_CLASS(bcontainer); + assert(vioc->setup); + + if (!vioc->setup(bcontainer, errp)) { + goto unregister_container_exit; + } + + vfio_address_space_insert(space, bcontainer); + + if (!vfio_listener_register(bcontainer, errp)) { + goto listener_release_exit; + } + + bcontainer->initialized = true; + + return container; + +listener_release_exit: + vfio_listener_unregister(bcontainer); + if (vioc->release) { + vioc->release(bcontainer); + } + +unregister_container_exit: + vfio_cpr_unregister_container(bcontainer); + +free_container_exit: + object_unref(container);hw/vfio-user/container.c line 128
Quoted 10 lines
hw/vfio-user/container.c new file +10
-0+ +put_space_exit: + vfio_address_space_put(space); + + return NULL; +} + +static void vfio_user_container_disconnect (VFIOUserContainer *container) +{ + VFIOContainerBase *bcontainer = &container->bcontainer;Use a QOM cast here.
Quoted 13 lines
hw/vfio-user/container.c new file +13
-0+ VFIOIOMMUClass *vioc = VFIO_IOMMU_GET_CLASS(bcontainer); + + vfio_listener_unregister(bcontainer); + if (vioc->release) { + vioc->release(bcontainer); + } + + VFIOAddressSpace *space = bcontainer->space; + + vfio_cpr_unregister_container(bcontainer); + object_unref(container); + + vfio_address_space_put(space);hw/vfio-user/container.c line 151
Quoted 10 lines
hw/vfio-user/container.c new file +10
-0+} + +static bool vfio_user_device_get (VFIOUserContainer *container, + VFIODevice *vbasedev, Error **errp) +{ + struct vfio_device_info info = { 0 }; + + vbasedev->fd = -1; + + vfio_device_prepare(vbasedev, &container->bcontainer, &info);Use a QOM cast here.
Quoted 80 lines
hw/vfio-user/container.c new file +60
-0+ return true; +} + +/* +* vfio_user_device_attach: attach a device to a new container. +*/ +static bool vfio_user_device_attach (const char *name,VFIODevice *vbasedev, + AddressSpace *as, Error **errp) +{ + VFIOUserContainer *container; + + container = vfio_user_container_connect(as, errp); + if (container == NULL) { + error_prepend(errp, "failed to connect proxy"); + return false; + } + + return vfio_user_device_get(container, vbasedev, errp); +} + +static void vfio_user_device_detach (VFIODevice *vbasedev) +{ + VFIOUserContainer *container = container_of(vbasedev->bcontainer, + VFIOUserContainer, bcontainer); + + vfio_device_unprepare(vbasedev); + + vfio_user_container_disconnect(container); +} + +static int vfio_user_pci_hot_reset (VFIODevice *vbasedev,bool single) +{ + /* ->needs_reset is always false for vfio-user. */ + return 0; +} + +static void vfio_iommu_user_class_init (ObjectClass *klass,const void *data) +{ + VFIOIOMMUClass *vioc = VFIO_IOMMU_CLASS(klass); + + vioc->setup = vfio_user_setup; + vioc->dma_map = vfio_user_dma_map; + vioc->dma_unmap = vfio_user_dma_unmap; + vioc->attach_device = vfio_user_device_attach; + vioc->detach_device = vfio_user_device_detach; + vioc->set_dirty_page_tracking = vfio_user_set_dirty_page_tracking; + vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap; + vioc->pci_hot_reset = vfio_user_pci_hot_reset; +}; + +static const TypeInfo types[] = { + { + .name = TYPE_VFIO_IOMMU_USER, + .parent = TYPE_VFIO_IOMMU, + .instance_size = sizeof(VFIOUserContainer), + .class_init = vfio_iommu_user_class_init, + }, +}; + +DEFINE_TYPES (types)hw/vfio-user/pci.c new file +14
-0@@ -0,0 +1,155 @@ +/* +* vfio PCI device over a UNIX socket. +* +* Copyright © 2018, 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include "qemu/osdep.h" +hw/vfio-user/pci.c line 24
Quoted 10 lines
hw/vfio-user/pci.c new file +10
-0+#include "hw/qdev-properties.h" +#include "hw/vfio/pci.h" + +#define TYPE_VFIO_USER_PCI "vfio-user-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) + +struct VFIOUserPCIDevice { + VFIOPCIDevice device; + char *sock_name; +};Again as per the documentation link above, device should be called
parent_objplus there should be a empty line betweenparent_objand the other members i.e.struct VFIOUserPCIDevice {
VFIOPCIDeviceparent_obj;char
*sock_name;
}Note that by using QOM casts the name of the parent object member is not exposed to the remainder of the code.
hw/vfio-user/pci.c line 37
Quoted 12 lines
hw/vfio-user/pci.c new file +12
-0+/* +* Emulated devices don't use host hot reset +*/ +static void vfio_user_compute_needs_reset (VFIODevice *vbasedev) +{ + vbasedev->needs_reset = false; +} + +static Object *vfio_user_pci_get_object (VFIODevice *vbasedev) +{ + VFIOUserPCIDevice *vdev = container_of(vbasedev, VFIOUserPCIDevice, + device.vbasedev);Hmmm. Not for this series but this may suggest VFIODevice should be QOMified.
Quoted 106 lines
hw/vfio-user/pci.c new file +2
-0+ return OBJECT(vdev); +}> +static VFIODeviceOps
vfio_user_pci_ops= {
+ .vfio_compute_needs_reset =vfio_user_compute_needs_reset, + .vfio_eoi =vfio_pci_intx_eoi,
+ .vfio_get_object =vfio_user_pci_get_object,
+ /* No live migration support yet. */
+ .vfio_save_config = NULL,
+ .vfio_load_config = NULL,hw/vfio-user/pci.c new file +97
-0+}; + +static void vfio_user_pci_realize (PCIDevice *pdev,Error **errp) +{ + ERRP_GUARD(); + VFIOUserPCIDevice *udev = VFIO_USER_PCI(pdev); + VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev); + VFIODevice *vbasedev = &vdev->vbasedev; + AddressSpace *as; + + /* + * TODO: make option parser understand SocketAddress + * and use that instead of having scalar options + * for each socket type. + */ + if (!udev->sock_name) { + error_setg(errp, "No socket specified"); + error_append_hint(errp, "Use -device vfio-user-pci,socket=<name>\n"); + return; + } + + vbasedev->name = g_strdup_printf("VFIO user <%s>", udev->sock_name); + + /* + * vfio-user devices are effectively mdevs (don't use a host iommu). + */ + vbasedev->mdev = true; + + as = pci_device_iommu_address_space(pdev); + if (!vfio_device_attach_by_iommu_type(TYPE_VFIO_IOMMU_USER, + vbasedev->name, vbasedev, + as, errp)) { + error_prepend(errp, VFIO_MSG_PREFIX, vbasedev->name); + return; + } +} + +static void vfio_user_instance_init (Object *obj) +{ + PCIDevice *pci_dev = PCI_DEVICE(obj); + VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj); + VFIODevice *vbasedev = &vdev->vbasedev; + + device_add_bootindex_property(obj, &vdev->bootindex, + "bootindex", NULL, + &pci_dev->qdev); + vdev->host.domain = ~0U; + vdev->host.bus = ~0U; + vdev->host.slot = ~0U; + vdev->host.function = ~0U; + + vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_PCI, &vfio_user_pci_ops, + DEVICE(vdev), false); + + vdev->nv_gpudirect_clique = 0xFF; + + /* + * QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command + * line, therefore, no need to wait to realize like other devices. + */ + pci_dev->cap_present |= QEMU_PCI_CAP_EXPRESS; +} + +static void vfio_user_instance_finalize (Object *obj) +{ + VFIOPCIDevice *vdev = VFIO_PCI_BASE(obj); + + vfio_pci_put_device(vdev); +} + +static const Property vfio_user_pci_dev_properties[] = { + DEFINE_PROP_UINT32("x-pci-vendor-id", VFIOPCIDevice, + vendor_id, PCI_ANY_ID), + DEFINE_PROP_UINT32("x-pci-device-id", VFIOPCIDevice, + device_id, PCI_ANY_ID), + DEFINE_PROP_UINT32("x-pci-sub-vendor-id", VFIOPCIDevice, + sub_vendor_id, PCI_ANY_ID), + DEFINE_PROP_UINT32("x-pci-sub-device-id", VFIOPCIDevice, + sub_device_id, PCI_ANY_ID), + DEFINE_PROP_STRING("socket", VFIOUserPCIDevice, sock_name), +}; + +static void vfio_user_pci_dev_class_init (ObjectClass *klass,const void *data) +{ + DeviceClass *dc = DEVICE_CLASS(klass); + PCIDeviceClass *pdc = PCI_DEVICE_CLASS(klass); + + device_class_set_props(dc, vfio_user_pci_dev_properties); + dc->desc = "VFIO over socket PCI device assignment"; + pdc->realize = vfio_user_pci_realize; +} + +static const TypeInfo vfio_user_pci_dev_info = { + .name = TYPE_VFIO_USER_PCI, + .parent = TYPE_VFIO_PCI_BASE, + .instance_size = sizeof(VFIOUserPCIDevice), + .class_init = vfio_user_pci_dev_class_init,hw/vfio-user/pci.c line 155
Quoted 10 lines
hw/vfio-user/pci.c new file +10
-0+ .instance_init = vfio_user_instance_init, + .instance_finalize = vfio_user_instance_finalize, +}; + +static void register_vfio_user_dev_type (void ) +{ + type_register_static(&vfio_user_pci_dev_info); +} + +type_init (register_vfio_user_dev_type)Use
DEFINE_TYPESas you've already done above instead oftype_init()here.Quoted 54 lines
hw/meson.build +1
-0@@ -39,6 +39,7 @@ subdir('uefi') subdir('ufs') subdir('usb') subdir('vfio') +subdir('vfio-user') subdir('virtio') subdir('vmapple') subdir('watchdog')hw/vfio-user/meson.build new file +9
-0@@ -0,0 +1,9 @@ +vfio_user_ss = ss.source_set() +vfio_user_ss.add(files( + 'container.c', + 'pci.c', +)) + +if get_option('vfio_user_client').enabled() + specific_ss.add_all(vfio_user_ss) +endifmeson_options.txt +2
-0@@ -109,6 +109,8 @@ option('multiprocess', type: 'feature', value: 'auto', description: 'Out of process device emulation support') option('relocatable', type : 'boolean', value : true, description: 'toggle relocatable install') +option('vfio_user_client', type: 'feature', value: 'disabled', + description: 'vfio-user client support') option('vfio_user_server', type: 'feature', value: 'disabled', description: 'vfio-user server support') option('dbus_display', type: 'feature', value: 'auto',scripts/meson-buildoptions.sh +0
-0@@ -201,6 +201,8 @@ meson_options_help() {printf "%s\n" ' vdi vdi image format support' printf "%s\n" ' vduse-blk-export'
printf "%s\n" ' VDUSE block export support' + printf "%s\n" ' vfio-user-client'
+ printf "%s\n" ' vfio-user client support' printf "%s\n" ' vfio-user-server'
printf "%s\n" ' vfio-user server support' printf "%s\n" ' vhdx vhdx image format support'scripts/meson-buildoptions.sh _meson_option_parse() { · line 538
Quoted 8 lines
scripts/meson-buildoptions.sh +2
-0@@ -529,6 +531,8 @@ _meson_option_parse() { --disable-vdi) printf "%s" -Dvdi=disabled ;; --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;; --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;; + --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;; + --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;; --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;; --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;; --enable-vhdx) printf "%s" -Dvhdx=enabled ;;ATB,
Mark.
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
In reply to John LevonQuoted 362 lines · John Levon
Introduce basic plumbing for vfio-user behind a new --enable-vfio-user-client option.
We introduce VFIOUserContainer in
hw/vfio-user/container.c, which is a container type for the "IOMMU" type "vfio-iommu-user", and share some common container code fromhw/vfio/container.c.Add
hw/vfio-user/pci.cfor instantiating VFIOUserPCIDevice objects, sharing some common code fromhw/vfio/pci.c.Originally-by John Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>Signed-off-by John Levon <[email protected]>--- MAINTAINERS | 2 + hw/vfio-user/container.h | 25 +++ include/hw/vfio/vfio-container-base.h | 1 + hw/vfio-user/container.c | 212 ++++++++++++++++++++++++++ hw/vfio-user/pci.c | 155 +++++++++++++++++++ hw/meson.build | 1 + hw/vfio-user/meson.build | 9 ++ meson_options.txt | 2 + scripts/meson-buildoptions.sh | 4 + 9 files changed, 411 insertions(+)create mode 100644
hw/vfio-user/container.h
create mode 100644hw/vfio-user/container.c
create mode 100644hw/vfio-user/pci.c
create mode 100644 hw/vfio-user/meson.buildMAINTAINERS +2
-0@@ -4252,6 +4252,8 @@ M: John Levon <[email protected]> M: Thanos Makatos <[email protected]> S: Supported F: docs/devel/vfio-user.rst +F: hw/vfio-user/* +F: include/hw/vfio-user/* F: subprojects/libvfio-user EBPF:hw/vfio-user/container.h new file +25
-0@@ -0,0 +1,25 @@ +/* +* vfio-user specific definitions. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#ifndef HW_VFIO_USER_CONTAINER_H +#define HW_VFIO_USER_CONTAINER_H + +#include <inttypes.h> +#include <stdbool.h> + +#include "hw/vfio/vfio-container-base.h" + +/* MMU container sub-class for vfio-user. */ +typedef struct VFIOUserContainer { +VFIOContainerBase bcontainer; +}VFIOUserContainer ; + +OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserContainer,VFIO_IOMMU_USER ); + +#endif /* HW_VFIO_USER_CONTAINER_H */ include/hw/vfio/vfio-container-base.h +1
-0@@ -109,6 +109,7 @@ vfio_container_get_page_size_mask(const VFIOContainerBase *bcontainer)#define TYPE_VFIO_IOMMU_LEGACY TYPE_VFIO_IOMMU "-legacy"#define TYPE_VFIO_IOMMU_SPAPR TYPE_VFIO_IOMMU "-spapr"#define TYPE_VFIO_IOMMU_IOMMUFD TYPE_VFIO_IOMMU "-iommufd" +#define TYPE_VFIO_IOMMU_USER TYPE_VFIO_IOMMU "-user"OBJECT_DECLARE_TYPE (VFIOContainerBase, VFIOIOMMUClass,VFIO_IOMMU )hw/vfio-user/container.c new file +212
-0@@ -0,0 +1,212 @@ +/* +* Container for vfio-user IOMMU type: rather than communicating with the kernel +* vfio driver, we communicate over a socket to a server using the vfio-user +* protocol. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include <linux/vfio.h> +#include "qemu/osdep.h" + +#include "hw/vfio-user/container.h" +#include "hw/vfio/vfio-cpr.h" +#include "hw/vfio/vfio-device.h" +#include "hw/vfio/vfio-listener.h" +#include "qapi/error.h" +#include "trace.h" + +static int vfio_user_dma_unmap (const VFIOContainerBase *bcontainer, +hwaddr iova,ram_addr_t size, +IOMMUTLBEntry *iotlb,bool unmap_all) +{ +return -ENOTSUP ; +} + +static int vfio_user_dma_map (const VFIOContainerBase *bcontainer,hwaddr iova, +ram_addr_t size,void *vaddr,bool readonly, +MemoryRegion *mrp) +{ +return -ENOTSUP ; +} + +static int +vfio_user_set_dirty_page_tracking (const VFIOContainerBase *bcontainer, +bool start,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static int vfio_user_query_dirty_bitmap (const VFIOContainerBase *bcontainer, +VFIOBitmap *vbmap,hwaddr iova, +hwaddr size,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static bool vfio_user_setup (VFIOContainerBase *bcontainer,Error **errp) +{ +error_setg_errno (errp,ENOTSUP ,"Not supported" ); +return -ENOTSUP ; +} + +static VFIOUserContainer *vfio_user_create_container (Error **errp) +{ +VFIOUserContainer *container; + + container =VFIO_IOMMU_USER (object_new (TYPE_VFIO_IOMMU_USER )); +return container; +} + +/* +* Try to mirror vfio_container_connect() as much as possible. +*/ +static VFIOUserContainer * +vfio_user_container_connect (AddressSpace *as,Error **errp) +{ +VFIOContainerBase *bcontainer; +VFIOUserContainer *container; +VFIOAddressSpace *space; +VFIOIOMMUClass *vioc; + + space =vfio_address_space_get (as); + + container =vfio_user_create_container (errp); +if (!container) { + goto put_space_exit; + } + + bcontainer = &container->bcontainer; + +if (!vfio_cpr_register_container (bcontainer, errp)) { + goto free_container_exit; + } + + vioc =VFIO_IOMMU_GET_CLASS (bcontainer); +assert (vioc->setup); + +if (!vioc->setup (bcontainer, errp)) { + goto unregister_container_exit; + } + +vfio_address_space_insert (space, bcontainer); + +if (!vfio_listener_register (bcontainer, errp)) { + goto listener_release_exit; + } + + bcontainer->initialized = true; + +return container; + +listener_release_exit: +vfio_listener_unregister (bcontainer); +if (vioc->release) { + vioc->release (bcontainer); + } + +unregister_container_exit: +vfio_cpr_unregister_container (bcontainer); + +free_container_exit: +object_unref (container); + +put_space_exit: +vfio_address_space_put (space); + +return NULL ; +} + +static void vfio_user_container_disconnect (VFIOUserContainer *container) +{ +VFIOContainerBase *bcontainer = &container->bcontainer; +VFIOIOMMUClass *vioc =VFIO_IOMMU_GET_CLASS (bcontainer); + +vfio_listener_unregister (bcontainer); +if (vioc->release) { + vioc->release (bcontainer); + } + +VFIOAddressSpace *space = bcontainer->space; + +vfio_cpr_unregister_container (bcontainer); +object_unref (container); + +vfio_address_space_put (space); +} + +static bool vfio_user_device_get (VFIOUserContainer *container, +VFIODevice *vbasedev,Error **errp) +{ +struct vfio_device_info info = {0 }; + + vbasedev->fd =-1 ; + +vfio_device_prepare (vbasedev, &container->bcontainer, &info); + +return true; +} + +/* +* vfio_user_device_attach: attach a device to a new container. +*/ +static bool vfio_user_device_attach (const char *name,VFIODevice *vbasedev, +AddressSpace *as,Error **errp) +{ +VFIOUserContainer *container; + + container =vfio_user_container_connect (as, errp); +if (container ==NULL ) { +error_prepend (errp,"failed to connect proxy" ); +return false; + } + +return vfio_user_device_get (container, vbasedev, errp); +} + +static void vfio_user_device_detach (VFIODevice *vbasedev) +{ +VFIOUserContainer *container =container_of (vbasedev->bcontainer, + VFIOUserContainer, bcontainer); + +vfio_device_unprepare (vbasedev); + +vfio_user_container_disconnect (container); +} + +static int vfio_user_pci_hot_reset (VFIODevice *vbasedev,bool single) +{ +/* ->needs_reset is always false for vfio-user. */ +return 0 ; +} + +static void vfio_iommu_user_class_init (ObjectClass *klass,const void *data) +{ +VFIOIOMMUClass *vioc =VFIO_IOMMU_CLASS (klass); + + vioc->setup = vfio_user_setup; + vioc->dma_map = vfio_user_dma_map; + vioc->dma_unmap = vfio_user_dma_unmap; + vioc->attach_device = vfio_user_device_attach; + vioc->detach_device = vfio_user_device_detach; + vioc->set_dirty_page_tracking = vfio_user_set_dirty_page_tracking; + vioc->query_dirty_bitmap = vfio_user_query_dirty_bitmap; + vioc->pci_hot_reset = vfio_user_pci_hot_reset; +}; + +static const TypeInfo types[] = { + { + .name =TYPE_VFIO_IOMMU_USER , + .parent =TYPE_VFIO_IOMMU , + .instance_size =sizeof (VFIOUserContainer), + .class_init = vfio_iommu_user_class_init, + }, +}; + +DEFINE_TYPES (types)hw/vfio-user/pci.c new file +52
-0@@ -0,0 +1,155 @@ +/* +* vfio PCI device over a UNIX socket. +* +* Copyright © 2018, 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include <sys/ioctl.h> +#include "qemu/osdep.h" + +#include "hw/qdev-properties.h" +#include "hw/vfio/pci.h" + +#define TYPE_VFIO_USER_PCI "vfio-user-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) + +struct VFIOUserPCIDevice { +VFIOPCIDevice device; +char *sock_name; +}; + +/* +* Emulated devices don't use host hot reset +*/ +static void vfio_user_compute_needs_reset (VFIODevice *vbasedev) +{ + vbasedev->needs_reset = false; +} + +static Object *vfio_user_pci_get_object (VFIODevice *vbasedev) +{ +VFIOUserPCIDevice *vdev =container_of (vbasedev, VFIOUserPCIDevice, + device.vbasedev); + +return OBJECT (vdev); +} + +static VFIODeviceOps vfio_user_pci_ops = { + .vfio_compute_needs_reset = vfio_user_compute_needs_reset, + .vfio_eoi = vfio_pci_intx_eoi, + .vfio_get_object = vfio_user_pci_get_object, +/* No live migration support yet. */ + .vfio_save_config =NULL , + .vfio_load_config =NULL , +}; + +static void vfio_user_pci_realize (PCIDevice *pdev,Error **errp) +{
hw/vfio-user/pci.c line 62
Quoted 10 lines
hw/vfio-user/pci.c new file +10
-0+ERRP_GUARD (); +VFIOUserPCIDevice *udev =VFIO_USER_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev); +VFIODevice *vbasedev = &vdev->vbasedev; +AddressSpace *as; + +/* +* TODO: make option parser understand SocketAddress +* and use that instead of having scalar options +* for each socket type.
Please take a look at vfu_object_class_init() to handle the "socket" property.
C.
Quoted 155 lines
hw/vfio-user/pci.c new file +93
-0+*/ +if (!udev->sock_name) { +error_setg (errp,"No socket specified" ); +error_append_hint (errp,"Use -device vfio-user-pci,socket=<name>\n" ); +return ; + } + + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); + +/* +* vfio-user devices are effectively mdevs (don't use a host iommu). +*/ + vbasedev->mdev = true; + + as =pci_device_iommu_address_space (pdev); +if (!vfio_device_attach_by_iommu_type (TYPE_VFIO_IOMMU_USER , + vbasedev->name, vbasedev, + as, errp)) { +error_prepend (errp,VFIO_MSG_PREFIX , vbasedev->name); +return ; + } +} + +static void vfio_user_instance_init (Object *obj) +{ +PCIDevice *pci_dev =PCI_DEVICE (obj); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); +VFIODevice *vbasedev = &vdev->vbasedev; + +device_add_bootindex_property (obj, &vdev->bootindex, +"bootindex" ,NULL , + &pci_dev->qdev); + vdev->host.domain = ~0U ; + vdev->host.bus = ~0U ; + vdev->host.slot = ~0U ; + vdev->host.function = ~0U ; + +vfio_device_init (vbasedev,VFIO_DEVICE_TYPE_PCI , &vfio_user_pci_ops, +DEVICE (vdev), false); + + vdev->nv_gpudirect_clique =0xFF ; + +/* +* QEMU_PCI_CAP_EXPRESS initialization does not depend on QEMU command +* line, therefore, no need to wait to realize like other devices. +*/ + pci_dev->cap_present |=QEMU_PCI_CAP_EXPRESS ; +} + +static void vfio_user_instance_finalize (Object *obj) +{ +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); + +vfio_pci_put_device (vdev); +} + +static const Property vfio_user_pci_dev_properties[] = { +DEFINE_PROP_UINT32 ("x-pci-vendor-id" , VFIOPCIDevice, + vendor_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-device-id" , VFIOPCIDevice, + device_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-sub-vendor-id" , VFIOPCIDevice, + sub_vendor_id,PCI_ANY_ID ), +DEFINE_PROP_UINT32 ("x-pci-sub-device-id" , VFIOPCIDevice, + sub_device_id,PCI_ANY_ID ), +DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +}; + +static void vfio_user_pci_dev_class_init (ObjectClass *klass,const void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *pdc =PCI_DEVICE_CLASS (klass); + +device_class_set_props (dc, vfio_user_pci_dev_properties); + dc->desc ="VFIO over socket PCI device assignment" ; + pdc->realize = vfio_user_pci_realize; +} + +static const TypeInfo vfio_user_pci_dev_info = { + .name =TYPE_VFIO_USER_PCI , + .parent =TYPE_VFIO_PCI_BASE , + .instance_size =sizeof (VFIOUserPCIDevice), + .class_init = vfio_user_pci_dev_class_init, + .instance_init = vfio_user_instance_init, + .instance_finalize = vfio_user_instance_finalize, +}; + +static void register_vfio_user_dev_type (void ) +{ +type_register_static (&vfio_user_pci_dev_info); +} + +type_init (register_vfio_user_dev_type)hw/meson.build +1
-0@@ -39,6 +39,7 @@ subdir('uefi') subdir('ufs') subdir('usb') subdir('vfio') +subdir('vfio-user') subdir('virtio') subdir('vmapple') subdir('watchdog')hw/vfio-user/meson.build new file +9
-0@@ -0,0 +1,9 @@ +vfio_user_ss = ss.source_set() +vfio_user_ss.add(files( + 'container.c', + 'pci.c', +)) + +if get_option('vfio_user_client').enabled() + specific_ss.add_all(vfio_user_ss) +endifmeson_options.txt +2
-0@@ -109,6 +109,8 @@ option('multiprocess', type: 'feature', value: 'auto', description: 'Out of process device emulation support') option('relocatable', type : 'boolean', value : true, description: 'toggle relocatable install') +option('vfio_user_client', type: 'feature', value: 'disabled', + description: 'vfio-user client support') option('vfio_user_server', type: 'feature', value: 'disabled', description: 'vfio-user server support') option('dbus_display', type: 'feature', value: 'auto',scripts/meson-buildoptions.sh +4
-0@@ -201,6 +201,8 @@ meson_options_help() { printf "%s\n" ' vdi vdi image format support' printf "%s\n" ' vduse-blk-export' printf "%s\n" ' VDUSE block export support' + printf "%s\n" ' vfio-user-client' + printf "%s\n" ' vfio-user client support' printf "%s\n" ' vfio-user-server' printf "%s\n" ' vfio-user server support' printf "%s\n" ' vhdx vhdx image format support'@@ -529,6 +531,8 @@ _meson_option_parse() { --disable-vdi) printf "%s" -Dvdi=disabled ;; --enable-vduse-blk-export) printf "%s" -Dvduse_blk_export=enabled ;; --disable-vduse-blk-export) printf "%s" -Dvduse_blk_export=disabled ;; + --enable-vfio-user-client) printf "%s" -Dvfio_user_client=enabled ;; + --disable-vfio-user-client) printf "%s" -Dvfio_user_client=disabled ;; --enable-vfio-user-server) printf "%s" -Dvfio_user_server=enabled ;; --disable-vfio-user-server) printf "%s" -Dvfio_user_server=disabled ;; --enable-vhdx) printf "%s" -Dvhdx=enabled ;;
Re: [PATCH v3 06/23] vfio-user: add vfio-user class and container
Quoted 7 lines · Cédric Le Goater
Quoted 7 lines
hw/vfio-user/pci.c new file +7
-0+static void vfio_user_pci_realize (PCIDevice *pdev,Error **errp) +{ +ERRP_GUARD (); +VFIOUserPCIDevice *udev =VFIO_USER_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev); +VFIODevice *vbasedev = &vdev->vbasedev; +AddressSpace *as;
Quoted 8 lines
hw/vfio-user/pci.c line 62
Quoted 5 lines
hw/vfio-user/pci.c new file +5
-0+ +/* +* TODO: make option parser understand SocketAddress +* and use that instead of having scalar options +* for each socket type. Please take a look at
vfu_object_class_init()to handle the "socket" property.
Thanks for the pointer: done. I had to switch to using JSON-style device specification for this though, so updated docs too.
regards
john
PATCH v3 7/23 vfio-user: connect vfio proxy to remote server
In reply to John LevonIntroduce the vfio-user "proxy": this is the client code responsible for sending and receiving vfio-user messages across the control socket.
The new files hw/vfio-user/proxy.[ch] contain some basic plumbing for managing the proxy; initialize the proxy during realization of the VFIOUserPCIDevice instance.
--- hw/vfio-user/proxy.h | 82 +++++++++++++++++ include/hw/vfio/vfio-device.h | 2 + hw/vfio-user/pci.c | 17 ++++ hw/vfio-user/proxy.c | 165 ++++++++++++++++++++++++++++++++++ hw/vfio-user/meson.build | 1 + 5 files changed, 267 insertions(+) create mode 100644 hw/vfio-user/proxy.h create mode 100644 hw/vfio-user/proxy.c
hw/vfio-user/proxy.h new file +82 -0
@@ -0,0 +1,82 @@ +#ifndef VFIO_USER_PROXY_H +#define VFIO_USER_PROXY_H + +/* +* vfio protocol over a UNIX socket. +* +* Copyright © 2018, 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "io/channel.h" +#include "io/channel-socket.h" + +typedef struct { +int send_fds; +int recv_fds; +int *fds; +}VFIOUserFDs ; + +enum msg_type { +VFIO_MSG_NONE , +VFIO_MSG_ASYNC , +VFIO_MSG_WAIT , +VFIO_MSG_NOWAIT , +VFIO_MSG_REQ , +}; + +typedef struct VFIOUserMsg { +QTAILQ_ENTRY (VFIOUserMsg) next; +VFIOUserFDs *fds; +uint32_t rsize; +uint32_t id; +QemuCond cv; +bool complete; +enum msg_type type; +}VFIOUserMsg ; + + +enum proxy_state { +VFIO_PROXY_CONNECTED =1 , +VFIO_PROXY_ERROR =2 , +VFIO_PROXY_CLOSING =3 , +VFIO_PROXY_CLOSED =4 , +}; + +typedef QTAILQ_HEAD(VFIOUserMsgQ, VFIOUserMsg) VFIOUserMsgQ; + +typedef struct VFIOUserProxy { +QLIST_ENTRY (VFIOUserProxy) next; +char *sockname; +struct QIOChannel *ioc; +void (*request)(void *opaque,VFIOUserMsg *msg); +void *req_arg; +int flags; +QemuCond close_cv; +AioContext *ctx; +QEMUBH *req_bh; + +/* +* above only changed when BQL is held +* below are protected by per-proxy lock +*/ +QemuMutex lock; +VFIOUserMsgQ free; +VFIOUserMsgQ pending; +VFIOUserMsgQ incoming; +VFIOUserMsgQ outgoing; +VFIOUserMsg *last_nowait; +enum proxy_state state; +}VFIOUserProxy ; + +/* VFIOProxy flags */ +#define VFIO_PROXY_CLIENT 0x1 + +VFIOUserProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp); +void vfio_user_disconnect (VFIOUserProxy *proxy); + +#endif /* VFIO_USER_PROXY_H */
include/hw/vfio/vfio-device.h +2 -0
@@ -46,6 +46,7 @@ typedef struct VFIOMigration VFIOMigration;typedef struct IOMMUFDBackend IOMMUFDBackend ;typedef struct VFIOIOASHwpt VFIOIOASHwpt ; +typedef struct VFIOUserProxy VFIOUserProxy ; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next;
@@ -86,6 +87,7 @@ typedef struct VFIODevice {QLIST_ENTRY (VFIODevice) hwpt_next;struct vfio_region_info **reginfo;int *region_fds; +VFIOUserProxy *proxy; } VFIODevice;struct VFIODeviceOps {
hw/vfio-user/pci.c +17 -0
@@ -14,6 +14,7 @@#include "hw/qdev-properties.h" #include "hw/vfio/pci.h" +#include "hw/vfio-user/proxy.h" #define TYPE_VFIO_USER_PCI "vfio-user-pci"OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserPCIDevice,VFIO_USER_PCI )
@@ -55,6 +56,8 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIODevice *vbasedev = &vdev->vbasedev;AddressSpace *as; +SocketAddress addr; +VFIOUserProxy *proxy;/* * TODO: make option parser understand SocketAddress
@@ -67,6 +70,15 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)return ; } +memset (&addr,0 ,sizeof (addr)); + addr.type =SOCKET_ADDRESS_TYPE_UNIX ; + addr.u.q_unix.path = udev->sock_name; + proxy =vfio_user_connect_dev (&addr, errp); +if (!proxy) { +return ; + } + vbasedev->proxy = proxy; + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name);/*
@@ -112,8 +124,13 @@ static void vfio_user_instance_init(Object *obj)static void vfio_user_instance_finalize (Object *obj) {VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); +VFIODevice *vbasedev = &vdev->vbasedev;vfio_pci_put_device (vdev); + +if (vbasedev->proxy !=NULL ) { +vfio_user_disconnect (vbasedev->proxy); + } }static const Property vfio_user_pci_dev_properties[] = {
hw/vfio-user/proxy.c new file +165 -0
165 lines not on this page · raw message
hw/vfio-user/meson.build +1 -0
@@ -2,6 +2,7 @@ vfio_user_ss = ss.source_set() vfio_user_ss.add(files( 'container.c', 'pci.c', + 'proxy.c', )) if get_option('vfio_user_client').enabled()
Signature · 1 line
2.43.0
PATCH v3 8/23 vfio-user: implement message receive infrastructure
In reply to John LevonAdd the basic implementation for receiving vfio-user messages from the control socket.
--- meson.build | 1 + hw/vfio-user/protocol.h | 56 ++++++ hw/vfio-user/proxy.h | 11 + hw/vfio-user/trace.h | 1 + hw/vfio-user/pci.c | 11 + hw/vfio-user/proxy.c | 409 ++++++++++++++++++++++++++++++++++++++ hw/vfio-user/trace-events | 6 + 7 files changed, 495 insertions(+) create mode 100644 hw/vfio-user/protocol.h create mode 100644 hw/vfio-user/trace.h create mode 100644 hw/vfio-user/trace-events
meson.build +1 -0
@@ -3679,6 +3679,7 @@ if have_system 'hw/ufs', 'hw/usb', 'hw/vfio', + 'hw/vfio-user', 'hw/virtio', 'hw/vmapple', 'hw/watchdog',
hw/vfio-user/protocol.h new file +56 -0
56 lines not on this page · raw message
hw/vfio-user/proxy.h +11 -0
@@ -15,6 +15,9 @@#include "io/channel.h" #include "io/channel-socket.h" +#include "qemu/sockets.h" +#include "hw/vfio-user/protocol.h" + typedef struct { int send_fds; int recv_fds;
@@ -31,6 +34,7 @@ enum msg_type { typedef struct VFIOUserMsg { QTAILQ_ENTRY(VFIOUserMsg) next; + VFIOUserHdr *hdr; VFIOUserFDs *fds; uint32_t rsize; uint32_t id;
@@ -70,13 +74,20 @@ typedef struct VFIOUserProxy {VFIOUserMsgQ incoming;VFIOUserMsgQ outgoing;VFIOUserMsg *last_nowait; +VFIOUserMsg *part_recv; +size_t recv_left; enum proxy_state state; } VFIOUserProxy;/* VFIOProxy flags */ #define VFIO_PROXY_CLIENT 0x1 +typedef struct VFIODevice VFIODevice ; +VFIOUserProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp);void vfio_user_disconnect (VFIOUserProxy *proxy); +void vfio_user_set_handler (VFIODevice *vbasedev, +void (*handler)(void *opaque,VFIOUserMsg *msg), +void *reqarg);#endif /* VFIO_USER_PROXY_H */
hw/vfio-user/trace.h new file +1 -0
@@ -0,0 +1 @@ +#include "trace/trace-hw_vfio_user.h"
hw/vfio-user/pci.c +11 -0
@@ -24,6 +24,16 @@ struct VFIOUserPCIDevice {char *sock_name; }; +/* +* Incoming request message callback. +* +* Runs off main loop, so BQL held. +*/ +static void vfio_user_pci_process_req (void *opaque,VFIOUserMsg *msg) +{ + +} +/* * Emulated devices don't use host hot reset */
@@ -78,6 +88,7 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)return ; } vbasedev->proxy = proxy; +vfio_user_set_handler (vbasedev, vfio_user_pci_process_req, vdev); vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name);
hw/vfio-user/proxy.c +409 -0
458 lines not on this page · raw message
hw/vfio-user/trace-events new file +6 -0
@@ -0,0 +1,6 @@ +# See docs/devel/tracing.rst for syntax documentation. + +# common.c +vfio_user_recv_hdr(const char *name, uint16_t id, uint16_t cmd, uint32_t size, uint32_t flags) " (%s) id 0x%x cmd 0x%x size 0x%x flags 0x%x" +vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x" +vfio_user_recv_request(uint16_t cmd) " command 0x%x"
Signature · 1 line
2.43.0
PATCH v3 9/23 vfio-user: implement message send infrastructure
In reply to John LevonAdd plumbing for sending vfio-user messages on the control socket. Add initial version negotation on connection.
--- hw/vfio-user/protocol.h | 62 +++++ hw/vfio-user/proxy.h | 9 + hw/vfio-user/pci.c | 18 +- hw/vfio-user/proxy.c | 494 ++++++++++++++++++++++++++++++++++++++ hw/vfio-user/trace-events | 2 + 5 files changed, 583 insertions(+), 2 deletions(-)
hw/vfio-user/protocol.h +62 -0
@@ -53,4 +53,66 @@ enum vfio_user_command {#define VFIO_USER_NO_REPLY 0x10#define VFIO_USER_ERROR 0x20 + +/* +* VFIO_USER_VERSION +*/ +typedef struct { +VFIOUserHdr hdr; +uint16_t major; +uint16_t minor; +char capabilities[]; +}VFIOUserVersion ; + +#define VFIO_USER_MAJOR_VER 0 +#define VFIO_USER_MINOR_VER 0 + +#define VFIO_USER_CAP "capabilities" + +/* "capabilities" members */ +#define VFIO_USER_CAP_MAX_FDS "max_msg_fds" +#define VFIO_USER_CAP_MAX_XFER "max_data_xfer_size" +#define VFIO_USER_CAP_PGSIZES "pgsizes" +#define VFIO_USER_CAP_MAP_MAX "max_dma_maps" +#define VFIO_USER_CAP_MIGR "migration" + +/* "migration" members */ +#define VFIO_USER_CAP_PGSIZE "pgsize" +#define VFIO_USER_CAP_MAX_BITMAP "max_bitmap_size" + +/* +* Max FDs mainly comes into play when a device supports multiple interrupts +* where each ones uses an eventfd to inject it into the guest. +* It is clamped by the the number of FDs the qio channel supports in a +* single message. +*/ +#define VFIO_USER_DEF_MAX_FDS 8 +#define VFIO_USER_MAX_MAX_FDS 16 + +/* +* Max transfer limits the amount of data in region and DMA messages. +* Region R/W will be very small (limited by how much a single instruction +* can process) so just use a reasonable limit here. +*/ +#define VFIO_USER_DEF_MAX_XFER (1024 * 1024) +#define VFIO_USER_MAX_MAX_XFER (64 * 1024 * 1024) + +/* +* Default pagesizes supported is 4k. +*/ +#define VFIO_USER_DEF_PGSIZE 4096 + +/* +* Default max number of DMA mappings is stolen from the +* linux kernel "dma_entry_limit" +*/ +#define VFIO_USER_DEF_MAP_MAX 65535 + +/* +* Default max bitmap size is also take from the linux kernel, +* where usage of signed ints limits the VA range to 2^31 bytes. +* Dividing that by the number of bits per byte yields 256MB +*/ +#define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024) +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +9 -0
@@ -40,6 +40,7 @@ typedef struct VFIOUserMsg {uint32_t id;QemuCond cv;bool complete; +bool pending;enum msg_type type; } VFIOUserMsg;
@@ -59,6 +60,12 @@ typedef struct VFIOUserProxy {struct QIOChannel *ioc;void (*request)(void *opaque,VFIOUserMsg *msg);void *req_arg; +uint64_t max_xfer_size; +uint64_t max_send_fds; +uint64_t max_dma; +uint64_t dma_pgsizes; +uint64_t max_bitmap; +uint64_t migr_pgsize;int flags;QemuCond close_cv;AioContext *ctx;
@@ -81,6 +88,7 @@ typedef struct VFIOUserProxy {/* VFIOProxy flags */ #define VFIO_PROXY_CLIENT 0x1 +#define VFIO_PROXY_FORCE_QUEUED 0x4typedef struct VFIODevice VFIODevice ;
@@ -89,5 +97,6 @@ void vfio_user_disconnect(VFIOUserProxy *proxy);void vfio_user_set_handler (VFIODevice *vbasedev,void (*handler)(void *opaque,VFIOUserMsg *msg),void *reqarg); +bool vfio_user_validate_version (VFIOUserProxy *proxy,Error **errp);#endif /* VFIO_USER_PROXY_H */
hw/vfio-user/pci.c +16 -2
@@ -22,6 +22,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name; +bool send_queued;/* all sends are queued */ };/*
@@ -90,6 +91,14 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) vbasedev->proxy = proxy;vfio_user_set_handler (vbasedev, vfio_user_pci_process_req, vdev); +if (udev->send_queued) { + proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; + } + +if (!vfio_user_validate_version (proxy, errp)) { + goto error; + } + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name);/*
@@ -101,9 +110,13 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)if (!vfio_device_attach_by_iommu_type (TYPE_VFIO_IOMMU_USER , vbasedev->name, vbasedev, as, errp)) { -error_prepend (errp,VFIO_MSG_PREFIX , vbasedev->name); -return ; + goto error; } + +return ; + +error: +error_prepend (errp,VFIO_MSG_PREFIX , vdev->vbasedev.name); }static void vfio_user_instance_init (Object *obj)
@@ -154,6 +167,7 @@ static const Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_UINT32 ("x-pci-sub-device-id" , VFIOPCIDevice, sub_device_id,PCI_ANY_ID ),DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), + DEFINE_PROP_BOOL("x-send-queued", VFIOUserPCIDevice, send_queued, false), };static void vfio_user_pci_dev_class_init (ObjectClass *klass,const void *data)
hw/vfio-user/proxy.c +494 -0
550 lines not on this page · raw message
hw/vfio-user/trace-events +2 -0
@@ -4,3 +4,5 @@ vfio_user_recv_hdr(const char *name, uint16_t id, uint16_t cmd, uint32_t size, uint32_t flags) " (%s) id 0x%x cmd 0x%x size 0x%x flags 0x%x" vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x" vfio_user_recv_request(uint16_t cmd) " command 0x%x" +vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x" +vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s"
Signature · 1 line
2.43.0
PATCH v3 10/23 vfio-user: implement VFIO_USER_DEVICE_GET_INFO
In reply to John LevonAdd support for getting basic device information.
--- hw/vfio-user/device.h | 23 ++++++++++++++++++ hw/vfio-user/protocol.h | 12 ++++++++++ hw/vfio-user/proxy.h | 7 ++++++ hw/vfio-user/container.c | 10 +++++++- hw/vfio-user/device.c | 50 +++++++++++++++++++++++++++++++++++++++ hw/vfio-user/proxy.c | 12 ++++------ hw/vfio-user/meson.build | 1 + hw/vfio-user/trace-events | 1 + 8 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 hw/vfio-user/device.h create mode 100644 hw/vfio-user/device.c
hw/vfio-user/device.h new file +23 -0
@@ -0,0 +1,23 @@ +#ifndef VFIO_USER_DEVICE_H +#define VFIO_USER_DEVICE_H + +/* +* vfio protocol over a UNIX socket device handling. +* +* Copyright © 2018, 2021 Oracle and/or its affiliates. +* +* This work is licensed under the terms of the GNU GPL, version 2. See +* the COPYING file in the top-level directory. +* +* SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#include "qemu/osdep.h" +#include "linux/vfio.h" + +#include "hw/vfio-user/proxy.h" + +int vfio_user_get_device_info (VFIOUserProxy *proxy, +struct vfio_device_info *info); + +#endif /* VFIO_USER_DEVICE_H */
hw/vfio-user/protocol.h +12 -0
@@ -115,4 +115,16 @@ typedef struct {*/ #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024) +/* +* VFIO_USER_DEVICE_GET_INFO +* imported from struct vfio_device_info +*/ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +uint32_t num_regions; +uint32_t num_irqs; +}VFIOUserDeviceInfo ; +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +7 -0
@@ -15,7 +15,9 @@#include "io/channel.h" #include "io/channel-socket.h" +#include "qemu/queue.h" #include "qemu/sockets.h" +#include "qemu/thread.h" #include "hw/vfio-user/protocol.h" typedef struct {
@@ -99,4 +101,9 @@ void vfio_user_set_handler(VFIODevice *vbasedev, void *reqarg);bool vfio_user_validate_version (VFIOUserProxy *proxy,Error **errp); +void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd, +uint32_t size,uint32_t flags); +void vfio_user_send_wait (VFIOUserProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds,int rsize); +#endif /* VFIO_USER_PROXY_H */
hw/vfio-user/container.c +9 -1
@@ -14,6 +14,7 @@#include "qemu/osdep.h" #include "hw/vfio-user/container.h" +#include "hw/vfio-user/device.h" #include "hw/vfio/vfio-cpr.h" #include "hw/vfio/vfio-device.h" #include "hw/vfio/vfio-listener.h"
@@ -144,7 +145,14 @@ static void vfio_user_container_disconnect(VFIOUserContainer *container)static bool vfio_user_device_get (VFIOUserContainer *container,VFIODevice *vbasedev,Error **errp) { -struct vfio_device_info info = {0 }; +struct vfio_device_info info = { .argsz =sizeof (info) }; +int ret; + + ret =vfio_user_get_device_info (vbasedev->proxy, &info); +if (ret) { +error_setg_errno (errp, -ret,"get info failure" ); +return ret; + } vbasedev->fd =-1 ;
hw/vfio-user/device.c new file +50 -0
50 lines not on this page · raw message
hw/vfio-user/proxy.c +4 -8
@@ -42,10 +42,6 @@ static void vfio_user_cb(void *opaque);static void vfio_user_request (void *opaque);static int vfio_user_send_queued (VFIOUserProxy *proxy,VFIOUserMsg *msg); -static void vfio_user_send_wait (VFIOUserProxy *proxy,VFIOUserHdr *hdr, -VFIOUserFDs *fds,int rsize); -static void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd, -uint32_t size,uint32_t flags);static inline void vfio_user_set_error (VFIOUserHdr *hdr,uint32_t err) {
@@ -613,8 +609,8 @@ static int vfio_user_send_queued(VFIOUserProxy *proxy, VFIOUserMsg *msg)return 0 ; } -static void vfio_user_send_wait (VFIOUserProxy *proxy,VFIOUserHdr *hdr, -VFIOUserFDs *fds,int rsize) +void vfio_user_send_wait (VFIOUserProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds,int rsize) {VFIOUserMsg *msg;int ret;
@@ -785,8 +781,8 @@ void vfio_user_disconnect(VFIOUserProxy *proxy)g_free (proxy); } -static void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd, -uint32_t size,uint32_t flags) +void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd, +uint32_t size,uint32_t flags) {static uint16_t next_id;
hw/vfio-user/meson.build +1 -0
@@ -1,6 +1,7 @@ vfio_user_ss = ss.source_set() vfio_user_ss.add(files( 'container.c', + 'device.c', 'pci.c', 'proxy.c', ))
hw/vfio-user/trace-events +1 -0
@@ -6,3 +6,4 @@ vfio_user_recv_read(uint16_t id, int read) " id 0x%x read 0x%x" vfio_user_recv_request(uint16_t cmd) " command 0x%x" vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x" vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s" +vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d"
Signature · 1 line
2.43.0
PATCH v3 11/23 vfio-user: implement VFIO_USER_DEVICE_GET_REGION_INFO
In reply to John LevonAdd support for getting region info for vfio-user. As vfio-user has one fd per region, enable ->use_region_fds.
--- hw/vfio-user/device.h | 2 ++ hw/vfio-user/protocol.h | 14 ++++++++ hw/vfio-user/proxy.h | 1 + hw/vfio-user/device.c | 68 +++++++++++++++++++++++++++++++++++++++ hw/vfio-user/pci.c | 11 +++++++ hw/vfio-user/trace-events | 1 + 6 files changed, 97 insertions(+)
hw/vfio-user/device.h +2 -0
@@ -20,4 +20,6 @@int vfio_user_get_device_info (VFIOUserProxy *proxy,struct vfio_device_info *info); +extern VFIODeviceIOOps vfio_user_device_io_ops_sock; +#endif /* VFIO_USER_DEVICE_H */
hw/vfio-user/protocol.h +14 -0
@@ -127,4 +127,18 @@ typedef struct { uint32_t num_irqs; } VFIOUserDeviceInfo; +/* +* VFIO_USER_DEVICE_GET_REGION_INFO +* imported from struct vfio_region_info +*/ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +uint32_t index; +uint32_t cap_offset; +uint64_t size; +uint64_t offset; +}VFIOUserRegionInfo ; +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +1 -0
@@ -18,6 +18,7 @@#include "qemu/queue.h" #include "qemu/sockets.h" #include "qemu/thread.h" +#include "hw/vfio/vfio-device.h" #include "hw/vfio-user/protocol.h" typedef struct {
hw/vfio-user/device.c +68 -0
71 lines not on this page · raw message
hw/vfio-user/pci.c +11 -0
@@ -14,6 +14,7 @@#include "hw/qdev-properties.h" #include "hw/vfio/pci.h" +#include "hw/vfio-user/device.h" #include "hw/vfio-user/proxy.h" #define TYPE_VFIO_USER_PCI "vfio-user-pci"
@@ -101,11 +102,21 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); +/* +* Use socket-based device I/O instead of vfio kernel driver. +*/ + vbasedev->io_ops = &vfio_user_device_io_ops_sock; +/* * vfio-user devices are effectively mdevs (don't use a host iommu). */ vbasedev->mdev = true; +/* +* Enable per-region fds. +*/ + vbasedev->use_region_fds = true; + as =pci_device_iommu_address_space (pdev); if (!vfio_device_attach_by_iommu_type(TYPE_VFIO_IOMMU_USER, vbasedev->name, vbasedev,
hw/vfio-user/trace-events +1 -0
@@ -7,3 +7,4 @@ vfio_user_recv_request(uint16_t cmd) " command 0x%x" vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x" vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s" vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d" +vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index %d flags 0x%x size 0x%"PRIx64
Signature · 1 line
2.43.0
PATCH v3 12/23 vfio-user: implement VFIO_USER_REGION_READ/WRITE
In reply to John Levon--- hw/vfio-user/protocol.h | 12 +++++++ hw/vfio-user/device.c | 67 +++++++++++++++++++++++++++++++++++++++ hw/vfio-user/trace-events | 1 + 3 files changed, 80 insertions(+)
hw/vfio-user/protocol.h +12 -0
@@ -141,4 +141,16 @@ typedef struct { uint64_t offset; } VFIOUserRegionInfo; +/* +* VFIO_USER_REGION_READ +* VFIO_USER_REGION_WRITE +*/ +typedef struct { +VFIOUserHdr hdr; +uint64_t offset; +uint32_t region; +uint32_t count; +char data[]; +}VFIOUserRegionRW ; +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/device.c +67 -0
76 lines not on this page · raw message
hw/vfio-user/trace-events +1 -0
@@ -8,3 +8,4 @@ vfio_user_send_write(uint16_t id, int wrote) " id 0x%x wrote 0x%x" vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d minor %d caps: %s" vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d" vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index %d flags 0x%x size 0x%"PRIx64 +vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d"
Signature · 1 line
2.43.0
PATCH v3 13/23 vfio-user: set up PCI in vfio_user_pci_realize()
In reply to John LevonRe-use PCI setup functions from hw/vfio/pci.c to realize the vfio-user PCI device.
--- hw/vfio-user/pci.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+)
hw/vfio-user/pci.c +29 -0
@@ -124,10 +124,39 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) goto error; } +if (!vfio_pci_populate_device (vdev, errp)) { + goto error; + } + +if (!vfio_pci_config_setup (vdev, errp)) { + goto error; + } + +/* +* vfio_pci_config_setup will have registered the device's BARs +* and setup any MSIX BARs, so errors after it succeeds must +* use out_teardown +*/ + +if (!vfio_pci_add_capabilities (vdev, errp)) { + goto out_teardown; + } + +if (!vfio_pci_interrupt_setup (vdev, errp)) { + goto out_teardown; + } + +vfio_pci_register_err_notifier (vdev); +vfio_pci_register_req_notifier (vdev); +return ; +out_teardown: +vfio_pci_teardown_msi (vdev); +vfio_pci_bars_exit (vdev); error:error_prepend (errp,VFIO_MSG_PREFIX , vdev->vbasedev.name); +vfio_pci_put_device (vdev); }static void vfio_user_instance_init (Object *obj)
Signature · 1 line
2.43.0
PATCH v3 14/23 vfio-user: implement VFIO_USER_DEVICE_GET/SET_IRQ*
In reply to John LevonIRQ setup uses the same semantics as the traditional vfio path, but we need to share the corresponding file descriptors with the server as necessary.
--- hw/vfio-user/protocol.h | 25 ++++++++ hw/vfio-user/device.c | 121 ++++++++++++++++++++++++++++++++++++++ hw/vfio-user/trace-events | 2 + 3 files changed, 148 insertions(+)
hw/vfio-user/protocol.h +25 -0
@@ -141,6 +141,31 @@ typedef struct {uint64_t offset; } VFIOUserRegionInfo; +/* +* VFIO_USER_DEVICE_GET_IRQ_INFO +* imported from struct vfio_irq_info +*/ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +uint32_t index; +uint32_t count; +}VFIOUserIRQInfo ; + +/* +* VFIO_USER_DEVICE_SET_IRQS +* imported from struct vfio_irq_set +*/ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +uint32_t index; +uint32_t start; +uint32_t count; +}VFIOUserIRQSet ; +/* * VFIO_USER_REGION_READ * VFIO_USER_REGION_WRITE
hw/vfio-user/device.c +121 -0
133 lines not on this page · raw message
hw/vfio-user/trace-events +2 -0
@@ -9,3 +9,5 @@ vfio_user_version(uint16_t major, uint16_t minor, const char *caps) " major %d m vfio_user_get_info(uint32_t nregions, uint32_t nirqs) " #regions %d #irqs %d" vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index %d flags 0x%x size 0x%"PRIx64 vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d" +vfio_user_get_irq_info(uint32_t index, uint32_t flags, uint32_t count) " index %d flags 0x%x count %d" +vfio_user_set_irqs(uint32_t index, uint32_t start, uint32_t count, uint32_t flags) " index %d start %d count %d flags 0x%x"
Signature · 1 line
2.43.0
PATCH v3 15/23 vfio-user: forward MSI-X PBA BAR accesses to server
In reply to John LevonFor vfio-user, the server holds the pending IRQ state; set up an I/O region for the MSI-X PBA so we can ask the server for this state on a PBA read.
--- hw/vfio/pci.h | 1 + hw/vfio-user/pci.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+)
hw/vfio/pci.h +1 -0
@@ -116,6 +116,7 @@ typedef struct VFIOMSIXInfo {uint32_t pba_offset;unsigned long *pending;bool noresize; +MemoryRegion *pba_region; } VFIOMSIXInfo;/*
hw/vfio-user/pci.c +64 -0
82 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 16/23 vfio-user: set up container access to the proxy
In reply to John LevonThe user container will shortly need access to the underlying vfio-user proxy; set this up.
--- hw/vfio-user/container.h | 2 ++ hw/vfio-user/container.c | 43 +++++++++++++++++++++++++++++++--------- 2 files changed, 36 insertions(+), 9 deletions(-)
hw/vfio-user/container.h +2 -0
@@ -14,10 +14,12 @@#include <stdbool.h> #include "hw/vfio/vfio-container-base.h" +#include "hw/vfio-user/proxy.h" /* MMU container sub-class for vfio-user. */ typedef struct VFIOUserContainer {VFIOContainerBase bcontainer; +VFIOUserProxy *proxy; }VFIOUserContainer ;OBJECT_DECLARE_SIMPLE_TYPE (VFIOUserContainer,VFIO_IOMMU_USER );
hw/vfio-user/container.c +34 -9
103 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 17/23 vfio-user: implement VFIO_USER_DEVICE_RESET
In reply to John LevonHook this call up to the legacy reset handler for vfio-user-pci.
--- hw/vfio-user/device.h | 2 ++ hw/vfio-user/device.c | 12 ++++++++++++ hw/vfio-user/pci.c | 15 +++++++++++++++ 3 files changed, 29 insertions(+)
hw/vfio-user/device.h +2 -0
@@ -20,6 +20,8 @@int vfio_user_get_device_info (VFIOUserProxy *proxy,struct vfio_device_info *info); +void vfio_user_device_reset (VFIOUserProxy *proxy); +extern VFIODeviceIOOps vfio_user_device_io_ops_sock;#endif /* VFIO_USER_DEVICE_H */
hw/vfio-user/device.c +12 -0
@@ -49,6 +49,18 @@ int vfio_user_get_device_info(VFIOUserProxy *proxy,return 0 ; } +void vfio_user_device_reset (VFIOUserProxy *proxy) +{ +VFIOUserHdr msg; + +vfio_user_request_msg (&msg,VFIO_USER_DEVICE_RESET ,sizeof (msg),0 ); + +vfio_user_send_wait (proxy, &msg,NULL ,0 ); +if (msg.flags &VFIO_USER_ERROR ) { +error_printf ("reset reply error %d\n" , msg.error_reply); + } +} +static int vfio_user_get_region_info (VFIOUserProxy *proxy,struct vfio_region_info *info,VFIOUserFDs *fds)
hw/vfio-user/pci.c +15 -0
@@ -261,6 +261,20 @@ static void vfio_user_instance_finalize(Object *obj) } } +static void vfio_user_pci_reset (DeviceState *dev) +{ +VFIOPCIDevice *vdev =VFIO_PCI_BASE (dev); +VFIODevice *vbasedev = &vdev->vbasedev; + +vfio_pci_pre_reset (vdev); + +if (vbasedev->reset_works) { +vfio_user_device_reset (vbasedev->proxy); + } + +vfio_pci_post_reset (vdev); +} +static const Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_UINT32 ("x-pci-vendor-id" , VFIOPCIDevice, vendor_id,PCI_ANY_ID ),
@@ -279,6 +293,7 @@ static void vfio_user_pci_dev_class_init(ObjectClass *klass, const void *data)DeviceClass *dc =DEVICE_CLASS (klass);PCIDeviceClass *pdc =PCI_DEVICE_CLASS (klass); +device_class_set_legacy_reset (dc, vfio_user_pci_reset);device_class_set_props (dc, vfio_user_pci_dev_properties); dc->desc ="VFIO over socket PCI device assignment" ; pdc->realize = vfio_user_pci_realize;
Signature · 1 line
2.43.0
PATCH v3 18/23 vfio-user: implement VFIO_USER_DMA_MAP/UNMAP
In reply to John LevonFrom: John Levon <[email protected]>
When the vfio-user container gets mapping updates, share them with the vfio-user by sending a message; this can include the region fd, allowing the server to directly mmap() the region as needed.
For performance, we only wait for the message responses when we're doing with a series of updates via the listener_commit() callback.
--- hw/vfio-user/protocol.h | 32 ++++++++++++ hw/vfio-user/proxy.h | 6 +++ hw/vfio-user/container.c | 107 +++++++++++++++++++++++++++++++++++++- hw/vfio-user/proxy.c | 77 ++++++++++++++++++++++++++- hw/vfio-user/trace-events | 4 ++ 5 files changed, 222 insertions(+), 4 deletions(-)
hw/vfio-user/protocol.h +32 -0
@@ -115,6 +115,31 @@ typedef struct {*/ #define VFIO_USER_DEF_MAX_BITMAP (256 * 1024 * 1024) +/* +* VFIO_USER_DMA_MAP +* imported from struct vfio_iommu_type1_dma_map +*/ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +uint64_t offset;/* FD offset */ +uint64_t iova; +uint64_t size; +}VFIOUserDMAMap ; + +/* +* VFIO_USER_DMA_UNMAP +* imported from struct vfio_iommu_type1_dma_unmap +*/ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +uint64_t iova; +uint64_t size; +}VFIOUserDMAUnmap ; +/* * VFIO_USER_DEVICE_GET_INFO * imported from struct vfio_device_info
@@ -178,4 +203,11 @@ typedef struct { char data[]; } VFIOUserRegionRW; +/*imported from struct vfio_bitmap */ +typedef struct { +uint64_t pgsize; +uint64_t size; +char data[]; +}VFIOUserBitmap ; +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +6 -0
@@ -73,6 +73,7 @@ typedef struct VFIOUserProxy {QemuCond close_cv;AioContext *ctx;QEMUBH *req_bh; +bool async_ops;/* * above only changed when BQL is held
@@ -102,9 +103,14 @@ void vfio_user_set_handler(VFIODevice *vbasedev, void *reqarg);bool vfio_user_validate_version (VFIOUserProxy *proxy,Error **errp); +VFIOUserFDs *vfio_user_getfds (int numfds); +void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd,uint32_t size,uint32_t flags); +void vfio_user_wait_reqs (VFIOUserProxy *proxy);void vfio_user_send_wait (VFIOUserProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds,int rsize); +void vfio_user_send_nowait (VFIOUserProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds,int rsize);#endif /* VFIO_USER_PROXY_H */
hw/vfio-user/container.c +105 -2
129 lines not on this page · raw message
hw/vfio-user/proxy.c +75 -2
101 lines not on this page · raw message
hw/vfio-user/trace-events +4 -0
@@ -11,3 +11,7 @@ vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d" vfio_user_get_irq_info(uint32_t index, uint32_t flags, uint32_t count) " index %d flags 0x%x count %d" vfio_user_set_irqs(uint32_t index, uint32_t start, uint32_t count, uint32_t flags) " index %d start %d count %d flags 0x%x" + +# container.c +vfio_user_dma_map(uint64_t iova, uint64_t size, uint64_t off, uint32_t flags, bool async_ops) " iova 0x%"PRIx64" size 0x%"PRIx64" off 0x%"PRIx64" flags 0x%x async_ops %d" +vfio_user_dma_unmap(uint64_t iova, uint64_t size, uint32_t flags, bool async_ops) " iova 0x%"PRIx64" size 0x%"PRIx64" flags 0x%x async_ops %d"
Signature · 1 line
2.43.0
PATCH v3 19/23 vfio-user: implement VFIO_USER_DMA_READ/WRITE
In reply to John LevonUnlike most other messages, this is a server->client message, for when a server wants to do "DMA"; this is slow, so normally the server has memory directly mapped instead.
--- hw/vfio-user/protocol.h | 13 ++++- hw/vfio-user/proxy.h | 3 ++ hw/vfio-user/pci.c | 111 ++++++++++++++++++++++++++++++++++++++++ hw/vfio-user/proxy.c | 84 ++++++++++++++++++++++++++++++ 4 files changed, 210 insertions(+), 1 deletion(-)
hw/vfio-user/protocol.h +12 -1
@@ -203,7 +203,18 @@ typedef struct {char data[]; } VFIOUserRegionRW; -/*imported from struct vfio_bitmap */ +/* +* VFIO_USER_DMA_READ +* VFIO_USER_DMA_WRITE +*/ +typedef struct { +VFIOUserHdr hdr; +uint64_t offset; +uint32_t count; +char data[]; +}VFIOUserDMARW ; + +/* imported from struct vfio_bitmap */ typedef struct { uint64_t pgsize; uint64_t size;
hw/vfio-user/proxy.h +3 -0
@@ -104,6 +104,7 @@ void vfio_user_set_handler(VFIODevice *vbasedev,bool vfio_user_validate_version (VFIOUserProxy *proxy,Error **errp);VFIOUserFDs *vfio_user_getfds (int numfds); +void vfio_user_putfds (VFIOUserMsg *msg);void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd,uint32_t size,uint32_t flags);
@@ -112,5 +113,7 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr, VFIOUserFDs *fds, int rsize);void vfio_user_send_nowait (VFIOUserProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds,int rsize); +void vfio_user_send_reply (VFIOUserProxy *proxy,VFIOUserHdr *hdr,int size); +void vfio_user_send_error (VFIOUserProxy *proxy,VFIOUserHdr *hdr,int error);#endif /* VFIO_USER_PROXY_H */
hw/vfio-user/pci.c +111 -0
130 lines not on this page · raw message
hw/vfio-user/proxy.c +84 -0
102 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH v3 20/23 vfio-user: add 'x-msg-timeout' option
In reply to John LevonBy default, the vfio-user subsystem will wait 5 seconds for a message reply from the server. Add an option to allow this to be configurable.
--- hw/vfio-user/proxy.h | 1 + hw/vfio-user/pci.c | 5 +++++ hw/vfio-user/proxy.c | 7 ++++--- 3 files changed, 10 insertions(+), 3 deletions(-)
hw/vfio-user/proxy.h +1 -0
@@ -70,6 +70,7 @@ typedef struct VFIOUserProxy {uint64_t max_bitmap;uint64_t migr_pgsize;int flags; +uint32_t wait_time;QemuCond close_cv;AioContext *ctx;QEMUBH *req_bh;
hw/vfio-user/pci.c +5 -0
@@ -25,6 +25,7 @@ struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name;bool send_queued;/* all sends are queued */ +uint32_t wait_time;/* timeout for message replies */ };/*
@@ -263,6 +264,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; } +/* user specified or 5 sec default */ + proxy->wait_time = udev->wait_time; +if (!vfio_user_validate_version (proxy, errp)) { goto error; }
@@ -397,6 +401,7 @@ static const Property vfio_user_pci_dev_properties[] = { sub_device_id, PCI_ANY_ID),DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name),DEFINE_PROP_BOOL ("x-send-queued" , VFIOUserPCIDevice, send_queued, false), + DEFINE_PROP_UINT32("x-msg-timeout", VFIOUserPCIDevice, wait_time, 5000), };static void vfio_user_pci_dev_class_init (ObjectClass *klass,const void *data)
hw/vfio-user/proxy.c +4 -3
@@ -24,7 +24,6 @@#include "qemu/main-loop.h" #include "system/iothread.h" -static int wait_time =5000 ;/* wait up to 5 sec for busy servers */ static IOThread *vfio_user_iothread;static void vfio_user_shutdown (VFIOUserProxy *proxy);
@@ -665,7 +664,8 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,if (ret ==0 ) {while (!msg->complete) { -if (!qemu_cond_timedwait (&msg->cv, &proxy->lock, wait_time)) { +if (!qemu_cond_timedwait (&msg->cv, &proxy->lock, + proxy->wait_time)) {VFIOUserMsgQ *list; list = msg->pending ? &proxy->pending : &proxy->outgoing;
@@ -725,7 +725,8 @@ void vfio_user_wait_reqs(VFIOUserProxy *proxy) msg->type =VFIO_MSG_WAIT ; proxy->last_nowait =NULL ;while (!msg->complete) { -if (!qemu_cond_timedwait (&msg->cv, &proxy->lock, wait_time)) { +if (!qemu_cond_timedwait (&msg->cv, &proxy->lock, + proxy->wait_time)) {VFIOUserMsgQ *list; list = msg->pending ? &proxy->pending : &proxy->outgoing;
Signature · 1 line
2.43.0
PATCH v3 21/23 vfio-user: support posted writes
In reply to John LevonSupport an asynchronous send of a vfio-user socket message (no wait for a reply) when the write is posted. This is only safe when no regions are mappable by the VM. Add an option to explicitly disable this as well.
--- hw/vfio-user/proxy.h | 5 +++++ hw/vfio-user/device.c | 37 ++++++++++++++++++++++++++++++++++--- hw/vfio-user/pci.c | 6 ++++++ hw/vfio-user/proxy.c | 12 ++++++++++-- 4 files changed, 55 insertions(+), 5 deletions(-)
hw/vfio-user/proxy.h +5 -0
@@ -94,6 +94,7 @@ typedef struct VFIOUserProxy {/* VFIOProxy flags */ #define VFIO_PROXY_CLIENT 0x1#define VFIO_PROXY_FORCE_QUEUED 0x4 +#define VFIO_PROXY_NO_POST 0x8typedef struct VFIODevice VFIODevice ;
@@ -107,6 +108,8 @@ bool vfio_user_validate_version(VFIOUserProxy *proxy, Error **errp);VFIOUserFDs *vfio_user_getfds (int numfds);void vfio_user_putfds (VFIOUserMsg *msg); +void vfio_user_disable_posted_writes (VFIOUserProxy *proxy); +void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd,uint32_t size,uint32_t flags);void vfio_user_wait_reqs (VFIOUserProxy *proxy);
@@ -114,6 +117,8 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr, VFIOUserFDs *fds, int rsize);void vfio_user_send_nowait (VFIOUserProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds,int rsize); +void vfio_user_send_async (VFIOUserProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds);void vfio_user_send_reply (VFIOUserProxy *proxy,VFIOUserHdr *hdr,int size);void vfio_user_send_error (VFIOUserProxy *proxy,VFIOUserHdr *hdr,int error);
hw/vfio-user/device.c +34 -3
@@ -93,10 +93,21 @@ static int vfio_user_get_region_info(VFIOUserProxy *proxy,trace_vfio_user_get_region_info (msgp->index, msgp->flags, msgp->size);memcpy (info, &msgp->argsz, info->argsz); + +/* +* If at least one region is directly mapped into the VM, then we can no +* longer rely on the sequential nature of vfio-user request handling to +* ensure that posted writes are completed before a subsequent read. In this +* case, disable posted write support. This is a per-device property, not +* per-region. +*/ +if (info->flags &VFIO_REGION_INFO_FLAG_MMAP ) { +vfio_user_disable_posted_writes (proxy); + } +return 0 ; } -static int vfio_user_device_io_get_region_info (VFIODevice *vbasedev,struct vfio_region_info *info,int *fd)
@@ -272,6 +283,12 @@ static int vfio_user_device_io_region_read(VFIODevice *vbasedev, uint8_t index,return msgp->count; } +/* +* If this is a posted write, and VFIO_PROXY_NO_POST is not set, then we are OK +* to send the write to the socket without waiting for the server's reply: +* a subsequent read (of any region) will not pass the posted write, as all +* messages are handled sequentially. +*/ static int vfio_user_device_io_region_write (VFIODevice *vbasedev,uint8_t index,off_t off,unsigned count,void *data,bool post)
@@ -279,21 +296,35 @@ static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,VFIOUserRegionRW *msgp =NULL ;VFIOUserProxy *proxy = vbasedev->proxy;int size =sizeof (*msgp) + count; +int flags =0 ;int ret;if (count > proxy->max_xfer_size) {return -EINVAL ; } +if (proxy->flags &VFIO_PROXY_NO_POST ) { + post = false; + } + +if (post) { + flags |=VFIO_USER_NO_REPLY ; + } + msgp =g_malloc0 (size); -vfio_user_request_msg (&msgp->hdr,VFIO_USER_REGION_WRITE , size,0 ); +vfio_user_request_msg (&msgp->hdr,VFIO_USER_REGION_WRITE , size, flags); msgp->offset = off; msgp->region = index; msgp->count = count;memcpy (&msgp->data, data, count);trace_vfio_user_region_rw (msgp->region, msgp->offset, msgp->count); -/* Ignore post: all writes are synchronous/non-posted. */ +/* async send will free msg after it's sent */ +if (post) { +vfio_user_send_async (proxy, &msgp->hdr,NULL ); +return count; + } +vfio_user_send_wait (proxy, &msgp->hdr,NULL ,0 );if (msgp->hdr.flags &VFIO_USER_ERROR ) { ret = -msgp->hdr.error_reply;
hw/vfio-user/pci.c +6 -0
@@ -26,6 +26,7 @@ struct VFIOUserPCIDevice {char *sock_name;bool send_queued;/* all sends are queued */ uint32_t wait_time;/* timeout for message replies */ +bool no_post;/* all region writes are sync */ };/*
@@ -264,6 +265,10 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; } +if (udev->no_post) { + proxy->flags |=VFIO_PROXY_NO_POST ; + } +/* user specified or 5 sec default */ proxy->wait_time = udev->wait_time;
@@ -402,6 +407,7 @@ static const Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name),DEFINE_PROP_BOOL ("x-send-queued" , VFIOUserPCIDevice, send_queued, false),DEFINE_PROP_UINT32 ("x-msg-timeout" , VFIOUserPCIDevice, wait_time,5000 ), + DEFINE_PROP_BOOL("x-no-posted-writes", VFIOUserPCIDevice, no_post, false), };static void vfio_user_pci_dev_class_init (ObjectClass *klass,const void *data)
hw/vfio-user/proxy.c +10 -2
@@ -683,8 +683,8 @@ void vfio_user_send_wait(VFIOUserProxy *proxy, VFIOUserHdr *hdr,/* * async send - msg can be queued, but will be freed when sent */ -static void vfio_user_send_async (VFIOUserProxy *proxy,VFIOUserHdr *hdr, -VFIOUserFDs *fds) +void vfio_user_send_async (VFIOUserProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds) {VFIOUserMsg *msg;int ret;
@@ -805,6 +805,14 @@ void vfio_user_putfds(VFIOUserMsg *msg) msg->fds =NULL ; } +void +vfio_user_disable_posted_writes (VFIOUserProxy *proxy) +{ + WITH_QEMU_LOCK_GUARD(&proxy->lock) { + proxy->flags |=VFIO_PROXY_NO_POST ; + } +} + static QLIST_HEAD(, VFIOUserProxy) vfio_user_sockets =QLIST_HEAD_INITIALIZER (vfio_user_sockets);
Signature · 1 line
2.43.0
PATCH v3 22/23 vfio-user: add coalesced posted writes
In reply to John LevonAdd new message to send multiple writes to server in a single message. Prevents the outgoing queue from overflowing when a long latency operation is followed by a series of posted writes.
--- hw/vfio-user/protocol.h | 21 ++++++++++ hw/vfio-user/proxy.h | 12 ++++++ hw/vfio-user/device.c | 40 +++++++++++++++++++ hw/vfio-user/proxy.c | 84 +++++++++++++++++++++++++++++++++++++++ hw/vfio-user/trace-events | 1 + 5 files changed, 158 insertions(+)
hw/vfio-user/protocol.h +21 -0
@@ -42,6 +42,7 @@ enum vfio_user_command {VFIO_USER_DMA_WRITE =12 ,VFIO_USER_DEVICE_RESET =13 ,VFIO_USER_DIRTY_PAGES =14 , +VFIO_USER_REGION_WRITE_MULTI =15 , VFIO_USER_MAX, };
@@ -75,6 +76,7 @@ typedef struct {#define VFIO_USER_CAP_PGSIZES "pgsizes"#define VFIO_USER_CAP_MAP_MAX "max_dma_maps"#define VFIO_USER_CAP_MIGR "migration" +#define VFIO_USER_CAP_MULTI "write_multiple"/* "migration" members */ #define VFIO_USER_CAP_PGSIZE "pgsize"
@@ -221,4 +223,23 @@ typedef struct { char data[]; } VFIOUserBitmap; +/* +* VFIO_USER_REGION_WRITE_MULTI +*/ +#define VFIO_USER_MULTI_DATA 8 +#define VFIO_USER_MULTI_MAX 200 + +typedef struct { +uint64_t offset; +uint32_t region; +uint32_t count; +char data[VFIO_USER_MULTI_DATA ]; +}VFIOUserWROne ; + +typedef struct { +VFIOUserHdr hdr; +uint64_t wr_cnt; +VFIOUserWROne wrs[VFIO_USER_MULTI_MAX ]; +}VFIOUserWRMulti ; +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio-user/proxy.h +12 -0
@@ -88,6 +88,8 @@ typedef struct VFIOUserProxy {VFIOUserMsg *last_nowait;VFIOUserMsg *part_recv;size_t recv_left; +VFIOUserWRMulti *wr_multi; +int num_outgoing;enum proxy_state state; } VFIOUserProxy;
@@ -95,6 +97,11 @@ typedef struct VFIOUserProxy {#define VFIO_PROXY_CLIENT 0x1#define VFIO_PROXY_FORCE_QUEUED 0x4#define VFIO_PROXY_NO_POST 0x8 +#define VFIO_PROXY_USE_MULTI 0x16 + +/* coalescing high and low water marks for VFIOProxy num_outgoing */ +#define VFIO_USER_OUT_HIGH 1024 +#define VFIO_USER_OUT_LOW 128typedef struct VFIODevice VFIODevice ;
@@ -122,4 +129,9 @@ void vfio_user_send_async(VFIOUserProxy *proxy, VFIOUserHdr *hdr,void vfio_user_send_reply (VFIOUserProxy *proxy,VFIOUserHdr *hdr,int size);void vfio_user_send_error (VFIOUserProxy *proxy,VFIOUserHdr *hdr,int error); +void vfio_user_flush_multi (VFIOUserProxy *proxy); +void vfio_user_create_multi (VFIOUserProxy *proxy); +void vfio_user_add_multi (VFIOUserProxy *proxy,uint8_t index, +off_t offset,uint32_t count,void *data); +#endif /* VFIO_USER_PROXY_H */
hw/vfio-user/device.c +40 -0
@@ -10,6 +10,8 @@*/ #include "qemu/osdep.h" +#include "qemu/lockable.h" +#include "qemu/thread.h" #include "hw/vfio-user/device.h" #include "hw/vfio-user/trace.h"
@@ -296,6 +298,7 @@ static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index,VFIOUserRegionRW *msgp =NULL ;VFIOUserProxy *proxy = vbasedev->proxy;int size =sizeof (*msgp) + count; +bool can_multi;int flags =0 ;int ret;
@@ -311,6 +314,43 @@ static int vfio_user_device_io_region_write(VFIODevice *vbasedev, uint8_t index, flags |=VFIO_USER_NO_REPLY ; } +/* write eligible to be in a WRITE_MULTI msg ? */ + can_multi = (proxy->flags &VFIO_PROXY_USE_MULTI ) && post && + count <=VFIO_USER_MULTI_DATA ; + +/* +* This should be a rare case, so first check without the lock, +* if we're wrong, vfio_send_queued() will flush any posted writes +* we missed here +*/ +if (proxy->wr_multi !=NULL || + (proxy->num_outgoing >VFIO_USER_OUT_HIGH && can_multi)) { + +/* +* re-check with lock +* +* if already building a WRITE_MULTI msg, +* add this one if possible else flush pending before +* sending the current one +* +* else if outgoing queue is over the highwater, +* start a new WRITE_MULTI message +*/ + WITH_QEMU_LOCK_GUARD(&proxy->lock) { +if (proxy->wr_multi !=NULL ) { +if (can_multi) { +vfio_user_add_multi (proxy, index, off, count, data); +return count; + } +vfio_user_flush_multi (proxy); + }else if (proxy->num_outgoing >VFIO_USER_OUT_HIGH && can_multi) { +vfio_user_create_multi (proxy); +vfio_user_add_multi (proxy, index, off, count, data); +return count; + } + } + } + msgp =g_malloc0 (size);vfio_user_request_msg (&msgp->hdr,VFIO_USER_REGION_WRITE , size, flags); msgp->offset = off;
hw/vfio-user/proxy.c +84 -0
146 lines not on this page · raw message
hw/vfio-user/trace-events +1 -0
@@ -11,6 +11,7 @@ vfio_user_get_region_info(uint32_t index, uint32_t flags, uint64_t size) " index vfio_user_region_rw(uint32_t region, uint64_t off, uint32_t count) " region %d offset 0x%"PRIx64" count %d" vfio_user_get_irq_info(uint32_t index, uint32_t flags, uint32_t count) " index %d flags 0x%x count %d" vfio_user_set_irqs(uint32_t index, uint32_t start, uint32_t count, uint32_t flags) " index %d start %d count %d flags 0x%x" +vfio_user_wrmulti(const char *s, uint64_t wr_cnt) " %s count 0x%"PRIx64 # container.c vfio_user_dma_map(uint64_t iova, uint64_t size, uint64_t off, uint32_t flags, bool async_ops) " iova 0x%"PRIx64" size 0x%"PRIx64" off 0x%"PRIx64" flags 0x%x async_ops %d"
Signature · 1 line
2.43.0
PATCH v3 23/23 docs: add vfio-user documentation
In reply to John LevonAdd some basic documentation on vfio-user usage.
--- docs/system/device-emulation.rst | 1 + docs/system/devices/vfio-user.rst | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 docs/system/devices/vfio-user.rst
docs/system/device-emulation.rst +1 -0
@@ -85,6 +85,7 @@ Emulated Devices devices/can.rst devices/ccid.rst devices/cxl.rst + devices/vfio-user.rst devices/ivshmem.rst devices/ivshmem-flat.rst devices/keyboard.rst
docs/system/devices/vfio-user.rst new file +24 -0
@@ -0,0 +1,24 @@ +========= +vfio-user +========= + +QEMU includes a ``vfio-user`` client. The ``vfio-user`` specification allows for +implementing (PCI) devices in userspace outside of QEMU; it is similar to +``vhost-user`` in this respect (see :doc:`vhost-user`), but can emulate arbitrary +PCI devices, not just ``virtio``. Whereas ``vfio`` is handled by the host +kernel, ``vfio-user``, while similar in implementation, is handled entirely in +userspace. + +For example, SPDK includes a virtual PCI NVMe controller implementation; by +setting up a ``vfio-user`` UNIX socket between QEMU and SPDK, a VM can send NVMe +I/O to the SPDK process. + +Presuming a suitable ``vfio-user`` server has opened a socket at +``/tmp/vfio-user.sock``, a device can be configured with for example: + +.. code-block:: console + +-device vfio-user-pci,socket=/tmp/vfio-user.sock,bus=pcie.0,addr=0x9 + +See `libvfio-user <https://github.com/nutanix/libvfio-user/>`_ for further +information.
Signature · 1 line
2.43.0
John,
Quoted 13 lines · John Levon
The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.
The vfio-user protocol allows for implementing (PCI) devices in another userspace process; SPDK is one example, which includes a virtual NVMe implementation.
The vfio-user framework consists of 3 parts:
1) The VFIO user protocol specification.
2) A client - the VFIO device in QEMU that encapsulates VFIO messages and sends them to the server.
3) A server - a remote process that emulates a device.
Quoted 3 lines
This patchset implements parts 1 and 2.
It has been tested against libvfio-user test servers as well as SPDK.
Is libvfio-user a server-only library ? It seems a bit odd not to use it on the client side. Anyhow,
We have now merged upstream all the prerequisite changes for vfio-user. This gives us about 1 month to fix regressions and merge the model for the vfio-user-pci device, its documentation and tests.
I *really* would prefer if we had functional tests although I don't how complex it would be. At least an initial framework would be good to have. More sophisticated scenarios, such as mappings and interrupts, can come later.
Thanks,
C.
Quoted 14 lines · Cédric Le Goater
Quoted 13 lines · John Levon
The series contains an implement of a vfio-user client in QEMU, along with a few more preparatory patches.
The vfio-user protocol allows for implementing (PCI) devices in another userspace process; SPDK is one example, which includes a virtual NVMe implementation.
The vfio-user framework consists of 3 parts:
1) The VFIO user protocol specification.
2) A client - the VFIO device in QEMU that encapsulates VFIO messages and sends them to the server.
3) A server - a remote process that emulates a device.
Quoted 5 lines
Quoted 3 lines
This patchset implements parts 1 and 2.
It has been tested against libvfio-user test servers as well as SPDK.
Is libvfio-user a server-only library ?
Yes.
Quoted 1 line
It seems a bit odd not to use it on the client side.
I wasn't around for any original discussion here, so don't have context, but there are two clients I know of today: qemu and cloud-hypervisor. The latter is in Rust, and the former is opinionated enough that I'm not sure the message handling part (the only useful bit of the library here) would be a good match.
Also I rather like having more than one implementation of a protocol, anyway.
Quoted 1 line
I *really* would prefer if we had functional tests although I don't how
I agree it makes sense to have a working functional test before merging.
In the meantime, I would like to continue with review of the main chunk - as you said, there's a lot of code. I've not made substantial changes since v3 (only the comments so far), so I'm happy to continue with v3 review, or if you prefer, I can send out v4 against master. Please let me know.
regards
john
Hi John,
Quoted 3 lines
Quoted 1 line
I *really* would prefer if we had functional tests although I don't how
I agree it makes sense to have a working functional test before merging.
Any progress ?
Quoted 4 lines
In the meantime, I would like to continue with review of the main chunk - as you said, there's a lot of code. I've not made substantial changes since v3 (only the comments so far), so I'm happy to continue with v3 review, or if you prefer, I can send out v4 against master. Please let me know.
I doubt we will have much more reviews on the vfio-user part. Can you please send v4 ?
Thanks,
C.
Quoted 7 lines · Cédric Le Goater
Hi John,
Quoted 3 lines
Quoted 1 line
I *really* would prefer if we had functional tests although I don't how
I agree it makes sense to have a working functional test before merging.
Any progress ?
I've had to look at some other bits and pieces this week, but the basic status is sadly still unchanged - I have a test with 2 VMs, a client and server, that works fine when run by hand yet hangs when run within the functional test framework.
I do now have a VM with lots of extra debugging installed, but figuring out why I see these hangs is proving to be quite time intensive. If we could point you towards a manual image and some scripts, would that be sufficient for now?
Quoted 7 lines
Quoted 4 lines
In the meantime, I would like to continue with review of the main chunk - as you said, there's a lot of code. I've not made substantial changes since v3 (only the comments so far), so I'm happy to continue with v3 review, or if you prefer, I can send out v4 against master. Please let me know.
I doubt we will have much more reviews on the vfio-user part. Can you please send v4 ?
ATB,
Mark.
Hello Mark,
Quoted 6 lines · Mark Cave-Ayland
Quoted 4 lines · Cédric Le Goater
Hi John,
Quoted 2 lines
Quoted 1 line
I *really* would prefer if we had functional tests although I don't how
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
I agree it makes sense to have a working functional test before merging.
Any progress ?
I've had to look at some other bits and pieces this week, but the basic status is sadly still unchanged - I have a test with 2 VMs, a client and server, that works fine when run by hand yet hangs when run within the functional test framework.
I do now have a VM with lots of extra debugging installed, but figuring out why I see these hangs is proving to be quite time intensive. If we could point you towards a manual image and some scripts, would that be sufficient for now?
Before merging, I would like to be able to experiment a minimum.
Does a dummy device (server side) implementation exist ? and a GH repo I could pull the code from.
John,
How do you test ?
Thanks,
C.
Quoted 8 lines · Cédric Le Goater
Before merging, I would like to be able to experiment a minimum.
Does a dummy device (server side) implementation exist ? and a GH repo I could pull the code from.
John,
How do you test ?
I use these scripts for smoke-testing:
https://github.com/jlevon/grot/blob/master/tests/vfio-user/run.sh
is easiest - you just need to compile libvfio-user and qemu and you should be good if you have a suitable Linux image.
See https://github.com/nutanix/libvfio-user/blob/master/docs/qemu.md
if you need an image that has the relevant kernel module, and for some basic testing instructions.
https://github.com/jlevon/grot/blob/master/tests/vfio-user/spdk-run.sh
is a more complete test - covers mmaps both ways etc. You can test this by interacting with the vNVMe device inside your VM.
See https://github.com/nutanix/libvfio-user/blob/master/docs/spdk.md
The qemu and live migration sections there are obviously stale.
Please let me know if you hit problems.
regards
john
Hello,
Quoted 9 lines · John Levon
Quoted 7 lines · Cédric Le Goater
Before merging, I would like to be able to experiment a minimum.
Does a dummy device (server side) implementation exist ? and a GH repo I could pull the code from.
John,
Quoted 7 lines
Quoted 1 line
How do you test ?
I use these scripts for smoke-testing:
https://github.com/jlevon/grot/blob/master/tests/vfio-user/run.sh
is easiest - you just need to compile libvfio-user and qemu and you should be good if you have a suitable Linux image.
PCI enumeration work but the kernel module won't load on ubuntu 24.04 and 25.04 :
root@ubuntu:~# lspci -s 00:0d.0 -k -vv 00:0d.0 Non-VGA unclassified device: ACCES I/O Products, Inc. PCI-IDIO-16 Isolated Digital Input / FET Output Card Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV+ VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx- Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx- Interrupt: pin A routed to IRQ 21 Region 2: I/O ports at 6000 [size=256] Kernel modules: gpio_pci_idio_16
root@ubuntu:~# dmesg | grep 0000:00:0d.0 [ 0.381550] pci 0000:00:0d.0: [494f:0dc8] type 00 class 0x000000 conventional PCI endpoint [ 0.382696] pci 0000:00:0d.0: BAR 2 [io 0x6000-0x60ff] [ 4.017654] pci-idio-16 0000:00:0d.0: error -EINVAL: Unable to initialize register map [ 4.027063] pci-idio-16 0000:00:0d.0: probe with driver pci-idio-16 failed with error -22
Any idea why ?
Quoted 10 lines
See https://github.com/nutanix/libvfio-user/blob/master/docs/qemu.md
if you need an image that has the relevant kernel module, and for some basic testing instructions.https://github.com/jlevon/grot/blob/master/tests/vfio-user/spdk-run.sh
is a more complete test - covers mmaps both ways etc. You can test this by interacting with the vNVMe device inside your VM.See https://github.com/nutanix/libvfio-user/blob/master/docs/spdk.md
I didn't try SPDK yet.
Thanks,
C.
Quoted 1 line · Cédric Le Goater
PCI enumeration work but the kernel module won't load on ubuntu 24.04
Sorry, I forgot - there's a kernel bug in the Ubuntu LTS kernel for this driver. Mark has a fix - Mark, can you share?
regards
john
Quoted 6 lines · John Levon
Quoted 1 line · Cédric Le Goater
PCI enumeration work but the kernel module won't load on ubuntu 24.04
Sorry, I forgot - there's a kernel bug in the Ubuntu LTS kernel for this driver. Mark has a fix - Mark, can you share?
I did have a bit of a dig into this a few weeks ago, and I think there are at least 2 bugs related to recent changes to the kernel regmap code. I was able to persuade the module to load with a couple of hacks, but I think there was still something else required to get the basic test up and running...
ATB,
Mark.
Quoted 32 lines · Cédric Le Goater
Hello,
Quoted 7 lines · John Levon
Quoted 5 lines · Cédric Le Goater
Before merging, I would like to be able to experiment a minimum.
Does a dummy device (server side) implementation exist ? and a GH repo I could pull the code from.
Quoted 8 lines
Quoted 3 lines
John,
How do you test ?
I use these scripts for smoke-testing:
https://urldefense.proofpoint.com/v2/url? u=https-3A__github.com_jlevon_grot_blob_master_tests_vfio-2Duser_run.sh&d=DwIDaQ&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=V-sTFYIBuNsRDS6PAkkLHCjd5V2lvtIYfbhCV4BsWHxVWzLB_Dj21kOCZ_00B8wZ&s=ZZtYgH13tGNzKPUfKumHJno3qZjfUTg5z6GMJ_9o5so&e= is easiest - you just need to compile libvfio-user and qemu and you should be good if you have a suitable Linux image.
PCI enumeration work but the kernel module won't load on ubuntu 24.04 and 25.04 :
root@ubuntu:~# lspci -s 00:0d.0 -k -vv 00:0d.0 Non-VGA unclassified device: ACCES I/O Products, Inc. PCI- IDIO-16 Isolated Digital Input / FET Output Card Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV+ VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx- Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx- Interrupt: pin A routed to IRQ 21 Region 2: I/O ports at 6000 [size=256] Kernel modules: gpio_pci_idio_16
Quoted 7 lines
root@ubuntu:~# dmesg | grep 0000:00:0d.0 [ 0.381550] pci 0000:00:0d.0: [494f:0dc8] type 00 class 0x000000 conventional PCI endpoint [ 0.382696] pci 0000:00:0d.0: BAR 2 [io 0x6000-0x60ff] [ 4.017654] pci-idio-16 0000:00:0d.0: error -EINVAL: Unable to initialize register map [ 4.027063] pci-idio-16 0000:00:0d.0: probe with driver pci-idio-16 failed with error -22Any idea why ?
Annoyingly this is because the gpio_pci_idio_16 module is currently broken upstream :( The last LTS I was able to able to successfully test here was 6.1, and so that's what I've used to generate my functional test images.
ATB,
Mark.
Quoted 38 lines · Mark Cave-Ayland
Quoted 36 lines · Cédric Le Goater
Hello,
Quoted 7 lines · John Levon
Quoted 5 lines · Cédric Le Goater
Before merging, I would like to be able to experiment a minimum.
Does a dummy device (server side) implementation exist ? and a GH repo I could pull the code from.
Quoted 8 lines
Quoted 3 lines
John,
How do you test ?
I use these scripts for smoke-testing:
https://urldefense.proofpoint.com/v2/url? u=https-3A__github.com_jlevon_grot_blob_master_tests_vfio-2Duser_run.sh&d=DwIDaQ&c=s883GpUCOChKOHiocYtGcg&r=c23RpsaH4D2MKyD3EPJTDa0BAxz6tV8aUJqVSoytEiY&m=V-sTFYIBuNsRDS6PAkkLHCjd5V2lvtIYfbhCV4BsWHxVWzLB_Dj21kOCZ_00B8wZ&s=ZZtYgH13tGNzKPUfKumHJno3qZjfUTg5z6GMJ_9o5so&e= is easiest - you just need to compile libvfio-user and qemu and you should be good if you have a suitable Linux image.
PCI enumeration work but the kernel module won't load on ubuntu 24.04 and 25.04 :
root@ubuntu:~# lspci -s 00:0d.0 -k -vv 00:0d.0 Non-VGA unclassified device: ACCES I/O Products, Inc. PCI- IDIO-16 Isolated Digital Input / FET Output Card Control: I/O+ Mem+ BusMaster- SpecCycle- MemWINV+ VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx- Status: Cap- 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx- Interrupt: pin A routed to IRQ 21 Region 2: I/O ports at 6000 [size=256] Kernel modules: gpio_pci_idio_16root@ubuntu:~# dmesg | grep 0000:00:0d.0 [ 0.381550] pci 0000:00:0d.0: [494f:0dc8] type 00 class 0x000000 conventional PCI endpoint [ 0.382696] pci 0000:00:0d.0: BAR 2 [io 0x6000-0x60ff] [ 4.017654] pci-idio-16 0000:00:0d.0: error -EINVAL: Unable to initialize register map [ 4.027063] pci-idio-16 0000:00:0d.0: probe with driver pci-idio-16 failed with error -22
Quoted 3 lines
Quoted 1 line
Any idea why ?
Annoyingly this is because the
gpio_pci_idio_16module is currently broken upstream :(
Is is a known issue ? the gpio_pci_idio_16 module seems well maintained.
Quoted 1 line
The last LTS I was able to able to successfully test here was 6.1, and so that's what I've used to generate my functional test images.
All seems to work fine with the debian-12-nocloud-amd64.qcow2 imageand the gpio-pci-idio-16 kmod is even provided by default.
It would be nice to be able generate some interrupts
Thanks,
C.