vfio-user client
RFC v3 13/19 vfio-user: pci_user_realize PCI setup
In reply to John JohnsonPCI BARs read from remote device
PCI config reads/writes sent to remote server
--- hw/vfio/pci.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-)
hw/vfio/pci.c +88 -1
@@ -3551,8 +3551,93 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) goto error; } +/* Get a copy of config space */ + ret =VDEV_REGION_READ (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX ,0 , +MIN (pci_config_size (pdev), vdev->config_size), + pdev->config); +if (ret < (int )MIN (pci_config_size (&vdev->pdev), vdev->config_size)) { +error_setg_errno (errp, -ret,"failed to read device config space" ); + goto error; + } + +/* vfio emulates a lot for us, but some bits need extra love */ + vdev->emulated_config_bits =g_malloc0 (vdev->config_size); + +/* QEMU can choose to expose the ROM or not */ +memset (vdev->emulated_config_bits +PCI_ROM_ADDRESS ,0xff ,4 ); +/* QEMU can also add or extend BARs */ +memset (vdev->emulated_config_bits +PCI_BASE_ADDRESS_0 ,0xff ,6 *4 ); + vdev->vendor_id =pci_get_word (pdev->config +PCI_VENDOR_ID ); + vdev->device_id =pci_get_word (pdev->config +PCI_DEVICE_ID ); + +/* QEMU can change multi-function devices to single function, or reverse */ + vdev->emulated_config_bits[PCI_HEADER_TYPE ] = +PCI_HEADER_TYPE_MULTI_FUNCTION ; + +/* Restore or clear multifunction, this is always controlled by QEMU */ +if (vdev->pdev.cap_present &QEMU_PCI_CAP_MULTIFUNCTION ) { + vdev->pdev.config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; + }else { + vdev->pdev.config[PCI_HEADER_TYPE ] &= ~PCI_HEADER_TYPE_MULTI_FUNCTION ; + } + +/* +* Clear host resource mapping info. If we choose not to register a +* BAR, such as might be the case with the option ROM, we can get +* confusing, unwritable, residual addresses from the host here. +*/ +memset (&vdev->pdev.config[PCI_BASE_ADDRESS_0 ],0 ,24 ); +memset (&vdev->pdev.config[PCI_ROM_ADDRESS ],0 ,4 ); + +vfio_pci_size_rom (vdev); + +vfio_bars_prepare (vdev); + +vfio_msix_early_setup (vdev, &err); +if (err) { +error_propagate (errp, err); + goto error; + } + +vfio_bars_register (vdev); + + ret =vfio_add_capabilities (vdev, errp); +if (ret) { + goto out_teardown; + } + +/* QEMU emulates all of MSI & MSIX */ +if (pdev->cap_present &QEMU_PCI_CAP_MSIX ) { +memset (vdev->emulated_config_bits + pdev->msix_cap,0xff , +MSIX_CAP_LENGTH ); + } + +if (pdev->cap_present &QEMU_PCI_CAP_MSI ) { +memset (vdev->emulated_config_bits + pdev->msi_cap,0xff , + vdev->msi_cap_size); + } + +if (vdev->pdev.config[PCI_INTERRUPT_PIN ] !=0 ) { + vdev->intx.mmap_timer =timer_new_ms (QEMU_CLOCK_VIRTUAL , + vfio_intx_mmap_enable, vdev); +pci_device_set_intx_routing_notifier (&vdev->pdev, + vfio_intx_routing_notifier); + vdev->irqchip_change_notifier.notify = vfio_irqchip_change; +kvm_irqchip_add_change_notifier (&vdev->irqchip_change_notifier); + ret =vfio_intx_enable (vdev, errp); +if (ret) { + goto out_deregister; + } + } +return ; +out_deregister: +pci_device_set_intx_routing_notifier (&vdev->pdev,NULL ); +kvm_irqchip_remove_change_notifier (&vdev->irqchip_change_notifier); +out_teardown: +vfio_teardown_msi (vdev); +vfio_bars_exit (vdev); error:vfio_user_disconnect (proxy);error_prepend (errp,VFIO_MSG_PREFIX , vdev->vbasedev.name);
@@ -3565,7 +3650,9 @@ static void vfio_user_instance_finalize(Object *obj)vfio_put_device (vdev); -vfio_user_disconnect (vbasedev->proxy); +if (vbasedev->proxy !=NULL ) { +vfio_user_disconnect (vbasedev->proxy); + } }static Property vfio_user_pci_dev_properties[] = {
Signature · 1 line
1.8.3.1
RFC v3 11/19 vfio-user: get region info
In reply to John Johnson--- hw/vfio/user-protocol.h | 14 ++++++++++++ include/hw/vfio/vfio-common.h | 4 +++- hw/vfio/common.c | 30 +++++++++++++++++++++++++- hw/vfio/user.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-)
hw/vfio/user-protocol.h +14 -0
@@ -95,4 +95,18 @@ typedef struct { uint32_t cap_offset; } 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 */
include/hw/vfio/vfio-common.h +3 -1
@@ -56,6 +56,7 @@ typedef struct VFIORegion {uint32_t nr_mmaps;VFIOMmap *mmaps;uint8_t nr;/* cache the region number for debug */ +int remfd;/* fd if exported from remote process */ } VFIORegion; typedef struct VFIOMigration {
@@ -150,8 +151,9 @@ typedef struct VFIODevice {VFIOMigration *migration;Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking; -struct vfio_region_info **regions;VFIOProxy *proxy; +struct vfio_region_info **regions; +int *regfds; } VFIODevice;struct VFIODeviceOps {
hw/vfio/common.c +29 -1
@@ -40,6 +40,7 @@#include "trace.h" #include "qapi/error.h" #include "migration/migration.h" +#include "hw/vfio/user.h" VFIOGroupList vfio_group_list =QLIST_HEAD_INITIALIZER (vfio_group_list);
@@ -1491,6 +1492,16 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,return true; } +static int vfio_get_region_info_remfd (VFIODevice *vbasedev,int index) +{ +struct vfio_region_info *info; + +if (vbasedev->regions ==NULL || vbasedev->regions[index] ==NULL ) { +vfio_get_region_info (vbasedev, index, &info); + } +return vbasedev->regfds !=NULL ? vbasedev->regfds[index] :-1 ; +} +static int vfio_setup_region_sparse_mmaps (VFIORegion *region,struct vfio_region_info *info) {
@@ -1544,6 +1555,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->size = info->size; region->fd_offset = info->offset; region->nr = index; + region->remfd =vfio_get_region_info_remfd (vbasedev, index);if (region->size) { region->mem =g_new0 (MemoryRegion,1 );
@@ -1587,6 +1599,7 @@ int vfio_region_mmap(VFIORegion *region) {int i, prot =0 ;char *name; +int fd;if (!region->mem) {return 0 ;
@@ -1595,9 +1608,11 @@ int vfio_region_mmap(VFIORegion *region) prot |= region->flags &VFIO_REGION_INFO_FLAG_READ ?PROT_READ :0 ; prot |= region->flags &VFIO_REGION_INFO_FLAG_WRITE ?PROT_WRITE :0 ; + fd = region->remfd !=-1 ? region->remfd : region->vbasedev->fd; +for (i =0 ; i < region->nr_mmaps; i++) { region->mmaps[i].mmap =mmap (NULL , region->mmaps[i].size, prot, -MAP_SHARED , region->vbasedev->fd, +MAP_SHARED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) {
@@ -2379,10 +2394,17 @@ void vfio_put_base_device(VFIODevice *vbasedev)int i;for (i =0 ; i < vbasedev->num_regions; i++) { +if (vbasedev->regfds !=NULL && vbasedev->regfds[i] !=-1 ) { +close (vbasedev->regfds[i]); + }g_free (vbasedev->regions[i]); }g_free (vbasedev->regions); vbasedev->regions =NULL ; +if (vbasedev->regfds !=NULL ) { +g_free (vbasedev->regfds); + vbasedev->regfds =NULL ; + } }if (!vbasedev->group) {
@@ -2405,6 +2427,9 @@ int vfio_get_region_info(VFIODevice *vbasedev, int index,if (vbasedev->regions ==NULL ) { vbasedev->regions = g_new0(struct vfio_region_info *, vbasedev->num_regions); +if (vbasedev->proxy !=NULL ) { + vbasedev->regfds =g_new0 (int, vbasedev->num_regions); + } }/* check cache */ if (vbasedev->regions[index] !=NULL ) {
@@ -2441,6 +2466,9 @@ retry:/* fill cache */ vbasedev->regions[index] =g_malloc0 (argsz);memcpy (vbasedev->regions[index], *info, argsz); +if (vbasedev->regfds !=NULL ) { + vbasedev->regfds[index] = fd; + }return 0 ; }
hw/vfio/user.c +50 -0
63 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 9/19 vfio-user: define socket send functions
In reply to John JohnsonAlso negotiate version with remote server
--- hw/vfio/user-protocol.h | 23 +++ hw/vfio/user.h | 1 + hw/vfio/pci.c | 11 ++ hw/vfio/user.c | 411 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 446 insertions(+)
hw/vfio/user-protocol.h +23 -0
@@ -52,6 +52,29 @@ enum vfio_user_command {#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_MIGR "migration" + +/* "migration" member */ +#define VFIO_USER_CAP_PGSIZE "pgsize" +#define VFIO_USER_DEF_MAX_FDS 8#define VFIO_USER_MAX_MAX_FDS 16
hw/vfio/user.h +1 -0
@@ -81,5 +81,6 @@ void vfio_user_disconnect(VFIOProxy *proxy);void vfio_user_set_handler (VFIODevice *vbasedev,void (*handler)(void *opaque,VFIOUserMsg *msg),void *reqarg); +int vfio_user_validate_version (VFIODevice *vbasedev,Error **errp);#endif /* VFIO_USER_H */
hw/vfio/pci.c +11 -0
@@ -3512,6 +3512,12 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; } +vfio_user_validate_version (vbasedev, &err); +if (err !=NULL ) { +error_propagate (errp, err); + goto error; + } + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); vbasedev->dev =DEVICE (vdev); vbasedev->fd =-1 ;
@@ -3520,6 +3526,11 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) vbasedev->ops = &vfio_user_pci_ops; vbasedev->valid_ops = &vfio_pci_valid_ops; +return ; + +error: +vfio_user_disconnect (proxy); +error_prepend (errp,VFIO_MSG_PREFIX , vdev->vbasedev.name); }static void vfio_user_instance_finalize (Object *obj)
hw/vfio/user.c +411 -0
453 lines not on this page · raw message
Signature · 1 line
1.8.3.1
v3 cover vfio-user client
Hello,
This is the 3rd revision of the vfio-user client implementation.
First of all, thank you for your time reviewing the previous versions.
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.
The libvfio-user project (https://github.com/nutanix/libvfio-user)
can be used by a remote process to handle the protocol to implement the third part.
We also worked on implementing a server with QEMU that is a separate patch series.
Contributors:
John G Johnson <[email protected]>
John Levon <[email protected]>
Thanos Makatos <[email protected]>
Elena Ufimtseva <[email protected]>
Jagannathan Raman <[email protected]>
Changes from v2->v3:
John Johnson (18):
vfio-user: add VFIO base abstract class
Moved common vfio pci cli options to base class
Add container IO ops vector
Added ops vectors to decide to use ioctl() or socket implementation
Add device IO ops vector
Added ops vectors to decide to use ioctl() or socket implementation
Add validation ops vector
Added validation vector to check user replies
vfio-user: Define type vfio_user_pci_dev_info
Added separate VFIO_USER_PCI config element to control whether vfio-user is compiled Fix scalar spelling
vfio-user: connect vfio proxy to remote server
Made socket IO non-blocking
Use g_strdup_printf to save socket name
vfio-user: define socket receive functions
Made socket IO non-blocking
Process inbound commands in main loop thread to avoid BQL interactions with recv Added comment describing inbound command callback usage
Use true/false instead of 1/0 for booleans
vfio-user: define socket send functions
Made socket IO non-blocking
Added version string NULL termination check
vfio-user: get device info
Added ops vectors to decide to use ioctl() or socket implementation Added validation vector to check user replies
vfio-user: get region info
Added ops vectors to decide to use ioctl() or socket implementation Added validation vector to check user replies
Remove merge bug that filled region cache twice
vfio-user: region read/write
Added ops vectors to decide to use ioctl() or socket implementation Added validation vector to check user replies
Made posted write conditional on region not mapped
vfio-user: pci_user_realize PCI setup
Moved common vfio pci cli options to base class
vfio-user: get and set IRQs
Added ops vectors to decide to use ioctl() or socket implementation Added validation vector to check user replies
Fixed %m usage when not using syscall
vfio-user: proxy container connect/disconnect
Added separate VFIO_USER_PCI config element to control whether vfio-user is compiled Use true/false instead of 1/0 for booleans
vfio-user: dma map/unmap operations
Added ops vectors to decide to use ioctl() or socket implementation Use BQL instead of iolock in comments
Fixed %m usage when not using syscall
vfio-user: dma read/write operations
Added header checking before loading DMA message content Added error handling if DMA fails
vfio-user: pci reset
no r3-specific changes
vfio-user: migration support
generic fix: only set qemu file error if there is a file
Thanos Makatos (1):
vfio-user: introduce vfio-user protocol specification
Spec specifies host endiannes instead of always LE Fixed grammar error
docs/devel/index.rst | 1 + docs/devel/vfio-user.rst | 1810 +++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.h | 27 +- hw/vfio/user-protocol.h | 210 +++++ hw/vfio/user.h | 96 +++ include/hw/vfio/vfio-common.h | 95 +++ hw/vfio/common.c | 489 +++++++++-- hw/vfio/migration.c | 34 +- hw/vfio/pci.c | 740 ++++++++++++++--- hw/vfio/user.c | 1559 +++++++++++++++++++++++++++++++++++ MAINTAINERS | 11 + hw/vfio/Kconfig | 10 + hw/vfio/meson.build | 1 + 13 files changed, 4896 insertions(+), 187 deletions(-) create mode 100644 docs/devel/vfio-user.rst create mode 100644 hw/vfio/user-protocol.h create mode 100644 hw/vfio/user.h create mode 100644 hw/vfio/user.c
Signature · 1 line
1.8.3.1
RFC v3 10/19 vfio-user: get device info
--- hw/vfio/user-protocol.h | 13 +++++++++++++ hw/vfio/user.h | 2 ++ hw/vfio/pci.c | 19 +++++++++++++++++++ hw/vfio/user.c | 40 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+)
hw/vfio/user-protocol.h +13 -0
@@ -82,4 +82,17 @@ typedef struct {#define VFIO_USER_MAX_MAX_XFER (64 * 1024 * 1024) +/* +* VFIO_USER_DEVICE_GET_INFO +* imported from struct_device_info +*/ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +uint32_t num_regions; +uint32_t num_irqs; +uint32_t cap_offset; +}VFIOUserDeviceInfo ; +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio/user.h +2 -0
@@ -83,4 +83,6 @@ void vfio_user_set_handler(VFIODevice *vbasedev, void *reqarg);int vfio_user_validate_version (VFIODevice *vbasedev,Error **errp); +extern VFIODevIO vfio_dev_io_sock; +#endif /* VFIO_USER_H */
hw/vfio/pci.c +19 -0
@@ -3484,6 +3484,8 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)VFIODevice *vbasedev = &vdev->vbasedev;SocketAddress addr;VFIOProxy *proxy; +struct vfio_device_info info; +int ret;Error *err =NULL ;/*
@@ -3524,8 +3526,25 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) vbasedev->type =VFIO_DEVICE_TYPE_PCI ; vbasedev->no_mmap = false; vbasedev->ops = &vfio_user_pci_ops; + vbasedev->io_ops = &vfio_dev_io_sock; vbasedev->valid_ops = &vfio_pci_valid_ops; + ret =VDEV_GET_INFO (vbasedev, &info); +if (ret) { +error_setg_errno (errp, -ret,"get info failure" ); + goto error; + } + vbasedev->num_irqs = info.num_irqs; + vbasedev->num_regions = info.num_regions; + vbasedev->flags = info.flags; + vbasedev->reset_works = !!(info.flags &VFIO_DEVICE_FLAGS_RESET ); + +vfio_populate_device (vdev, &err); +if (err) { +error_propagate (errp, err); + goto error; + } +return ; error:
hw/vfio/user.c +40 -0
@@ -905,3 +905,43 @@ int vfio_user_validate_version(VFIODevice *vbasedev, Error **errp)return 0 ; } + +static int vfio_user_get_info (VFIOProxy *proxy,struct vfio_device_info *info) +{ +VFIOUserDeviceInfo msg; + +memset (&msg,0 ,sizeof (msg)); +vfio_user_request_msg (&msg.hdr,VFIO_USER_DEVICE_GET_INFO ,sizeof (msg),0 ); + msg.argsz =sizeof (struct vfio_device_info ); + +vfio_user_send_wait (proxy, &msg.hdr,NULL ,0 , false); +if (msg.hdr.flags &VFIO_USER_ERROR ) { +return -msg.hdr.error_reply; + } + +memcpy (info, &msg.argsz,sizeof (*info)); +return 0 ; +} + + +/* +* Socket-based io_ops +*/ + +static int vfio_user_io_get_info (VFIODevice *vbasedev, +struct vfio_device_info *info) +{ +int ret; + + ret =vfio_user_get_info (vbasedev->proxy, info); +if (ret) { +return ret; + } + +return VDEV_VALID_INFO (vbasedev, info); +} + +VFIODevIO vfio_dev_io_sock = { + .get_info = vfio_user_io_get_info, +}; +
Signature · 1 line
1.8.3.1
RFC v3 2/19 vfio-user: add VFIO base abstract class
In reply to John JohnsonAdd an abstract base class both the kernel driver and user socket implementations can use to share code.
--- hw/vfio/pci.h | 16 +++++++-- hw/vfio/pci.c | 112 +++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 81 insertions(+), 47 deletions(-)
hw/vfio/pci.h +14 -2
@@ -114,8 +114,13 @@ typedef struct VFIOMSIXInfo {unsigned long *pending; } VFIOMSIXInfo; -#define TYPE_VFIO_PCI "vfio-pci" -OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI) +/* +* TYPE_VFIO_PCI_BASE is an abstract type used to share code +* between VFIO implementations that use a kernel driver +* with those that use user sockets. +*/ +#define TYPE_VFIO_PCI_BASE "vfio-pci-base" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI_BASE)struct VFIOPCIDevice {PCIDevice pdev;
@@ -175,6 +180,13 @@ struct VFIOPCIDevice {Notifier irqchip_change_notifier; }; +#define TYPE_VFIO_PCI "vfio-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOKernPCIDevice, VFIO_PCI) + +struct VFIOKernPCIDevice { +VFIOPCIDevice device; +}; +/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ static inline bool vfio_pci_is (VFIOPCIDevice *vdev,uint32_t vendor,uint32_t device) {
hw/vfio/pci.c +67 -45
232 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 4/19 Add device IO ops vector
In reply to John JohnsonUsed for communication with VFIO driver
(prep work for vfio-user, which will communicate over a socket)
--- include/hw/vfio/vfio-common.h | 28 ++++++++ hw/vfio/common.c | 159 ++++++++++++++++++++++++++++++++++++++---- hw/vfio/pci.c | 146 ++++++++++++++++++++++++-------------- 3 files changed, 265 insertions(+), 68 deletions(-)
include/hw/vfio/vfio-common.h +28 -0
@@ -124,6 +124,7 @@ typedef struct VFIOHostDMAWindow { } VFIOHostDMAWindow;typedef struct VFIODeviceOps VFIODeviceOps ; +typedef struct VFIODevIO VFIODevIO ; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next;
@@ -139,12 +140,14 @@ typedef struct VFIODevice {bool ram_block_discard_allowed;bool enable_migration;VFIODeviceOps *ops; +VFIODevIO *io_ops;unsigned int num_irqs;unsigned int num_regions;unsigned int flags;VFIOMigration *migration;Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking; +struct vfio_region_info **regions; } VFIODevice;struct VFIODeviceOps {
@@ -164,6 +167,30 @@ struct VFIODeviceOps {* through ioctl() to the kernel VFIO driver, but vfio-user * can use a socket to a remote process. */ +struct VFIODevIO { +int (*get_info)(VFIODevice *vdev,struct vfio_device_info *info); +int (*get_region_info)(VFIODevice *vdev, +struct vfio_region_info *info,int *fd); +int (*get_irq_info)(VFIODevice *vdev,struct vfio_irq_info *irq); +int (*set_irqs)(VFIODevice *vdev,struct vfio_irq_set *irqs); +int (*region_read)(VFIODevice *vdev,uint8_t nr,off_t off,uint32_t size, +void *data); +int (*region_write)(VFIODevice *vdev,uint8_t nr,off_t off,uint32_t size, +void *data,bool post); +}; + +#define VDEV_GET_INFO (vdev, info) \ + ((vdev)->io_ops->get_info((vdev), (info))) +#define VDEV_GET_REGION_INFO (vdev, info, fd) \ + ((vdev)->io_ops->get_region_info((vdev), (info), (fd))) +#define VDEV_GET_IRQ_INFO (vdev, irq) \ + ((vdev)->io_ops->get_irq_info((vdev), (irq))) +#define VDEV_SET_IRQS (vdev, irqs) \ + ((vdev)->io_ops->set_irqs((vdev), (irqs))) +#define VDEV_REGION_READ (vdev, nr, off, size, data) \ + ((vdev)->io_ops->region_read((vdev), (nr), (off), (size), (data))) +#define VDEV_REGION_WRITE (vdev, nr, off, size, data, post) \ + ((vdev)->io_ops->region_write((vdev), (nr), (off), (size), (data), (post)))struct VFIOContIO { int (*dma_map)(VFIOContainer *container,
@@ -184,6 +211,7 @@ struct VFIOContIO {#define CONT_DIRTY_BITMAP (cont, bitmap, range) \ ((cont)->io_ops->dirty_bitmap((cont), (bitmap), (range))) +extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl;#endif /* CONFIG_LINUX */
hw/vfio/common.c +144 -15
243 lines not on this page · raw message
hw/vfio/pci.c +93 -53
291 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 16/19 vfio-user: dma map/unmap operations
In reply to John Johnson--- hw/vfio/pci.h | 1 + hw/vfio/user-protocol.h | 32 +++++++ hw/vfio/user.h | 1 + include/hw/vfio/vfio-common.h | 4 + hw/vfio/common.c | 76 +++++++++++++--- hw/vfio/pci.c | 4 + hw/vfio/user.c | 206 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 309 insertions(+), 15 deletions(-)
hw/vfio/pci.h +1 -0
@@ -193,6 +193,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name; +bool secure_dma;/* disable shared mem for DMA */ bool send_queued;/* all sends are queued */ bool no_post;/* all regions write are sync */ };
hw/vfio/user-protocol.h +32 -0
@@ -83,6 +83,31 @@ typedef struct {/* +* 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_device_info */
@@ -146,4 +171,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.h +1 -0
@@ -74,6 +74,7 @@ typedef struct VFIOProxy {/* VFIOProxy flags */ #define VFIO_PROXY_CLIENT 0x1 +#define VFIO_PROXY_SECURE 0x2#define VFIO_PROXY_FORCE_QUEUED 0x4#define VFIO_PROXY_NO_POST 0x8
include/hw/vfio/vfio-common.h +4 -0
@@ -90,6 +90,8 @@ typedef struct VFIOContainer {VFIOContIO *io_ops;bool initialized;bool dirty_pages_supported; +bool will_commit; +bool need_map_fd;uint64_t dirty_pgsizes;uint64_t max_dirty_bitmap_size;unsigned long pgsizes;
@@ -210,6 +212,7 @@ struct VFIOContIO {int (*dirty_bitmap)(VFIOContainer *container,struct vfio_iommu_type1_dirty_bitmap *bitmap,struct vfio_iommu_type1_dirty_bitmap_get *range); +void (*wait_commit)(VFIOContainer *container); };#define CONT_DMA_MAP (cont, map, fd, will_commit) \
@@ -218,6 +221,7 @@ struct VFIOContIO { ((cont)->io_ops->dma_unmap((cont), (unmap), (bitmap), (will_commit)))#define CONT_DIRTY_BITMAP (cont, bitmap, range) \ ((cont)->io_ops->dirty_bitmap((cont), (bitmap), (range))) +#define CONT_WAIT_COMMIT (cont) ((cont)->io_ops->wait_commit(cont))extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl;
hw/vfio/common.c +61 -15
207 lines not on this page · raw message
hw/vfio/pci.c +4 -0
@@ -3516,6 +3516,9 @@ 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->secure_dma) { + proxy->flags |=VFIO_PROXY_SECURE ; + }if (udev->send_queued) { proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; }
@@ -3686,6 +3689,7 @@ static void vfio_user_instance_finalize(Object *obj)static Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false),DEFINE_PROP_BOOL ("x-send-queued" , VFIOUserPCIDevice, send_queued, false),DEFINE_PROP_BOOL ("x-no-posted-writes" , VFIOUserPCIDevice, no_post, false),DEFINE_PROP_END_OF_LIST (),
hw/vfio/user.c +206 -0
237 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 7/19 vfio-user: connect vfio proxy to remote server
In reply to John Johnson--- hw/vfio/user.h | 78 +++++++++++++++++++ include/hw/vfio/vfio-common.h | 2 + hw/vfio/pci.c | 20 +++++ hw/vfio/user.c | 170 ++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 4 + hw/vfio/meson.build | 1 + 6 files changed, 275 insertions(+) create mode 100644 hw/vfio/user.h create mode 100644 hw/vfio/user.c
hw/vfio/user.h new file +78 -0
@@ -0,0 +1,78 @@ +#ifndef VFIO_USER_H +#define VFIO_USER_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. +* +*/ + +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 VFIOProxy { +QLIST_ENTRY (VFIOProxy) 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; +}VFIOProxy ; + +/* VFIOProxy flags */ +#define VFIO_PROXY_CLIENT 0x1 + +VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp); +void vfio_user_disconnect (VFIOProxy *proxy); + +#endif /* VFIO_USER_H */
include/hw/vfio/vfio-common.h +2 -0
@@ -76,6 +76,7 @@ typedef struct VFIOAddressSpace {struct VFIOGroup ;typedef struct VFIOContIO VFIOContIO ; +typedef struct VFIOProxy VFIOProxy ; typedef struct VFIOContainer { VFIOAddressSpace *space;
@@ -150,6 +151,7 @@ typedef struct VFIODevice {Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking;struct vfio_region_info **regions; +VFIOProxy *proxy; } VFIODevice;struct VFIODeviceOps {
hw/vfio/pci.c +20 -0
@@ -43,6 +43,7 @@#include "qapi/error.h" #include "migration/blocker.h" #include "migration/qemu-file.h" +#include "hw/vfio/user.h" #define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"
@@ -3476,6 +3477,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)VFIOUserPCIDevice *udev =VFIO_USER_PCI (pdev);VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIODevice *vbasedev = &vdev->vbasedev; +SocketAddress addr; +VFIOProxy *proxy; +Error *err =NULL ;/* * TODO: make option parser understand SocketAddress
@@ -3488,6 +3492,16 @@ 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, &err); +if (!proxy) { +error_setg (errp,"Remote proxy not found" ); +return ; + } + vbasedev->proxy = proxy; + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); vbasedev->dev =DEVICE (vdev); vbasedev->fd =-1 ;
@@ -3500,6 +3514,12 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)static void vfio_user_instance_finalize (Object *obj) { +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); +VFIODevice *vbasedev = &vdev->vbasedev; + +vfio_put_device (vdev); + +vfio_user_disconnect (vbasedev->proxy); }static Property vfio_user_pci_dev_properties[] = {
hw/vfio/user.c new file +170 -0
170 lines not on this page · raw message
MAINTAINERS +4 -0
@@ -1882,8 +1882,12 @@ L: [email protected] vfio-user M: John G Johnson <[email protected]> M: Thanos Makatos <[email protected]> +M: Elena Ufimtseva <[email protected]> +M: Jagannathan Raman <[email protected]> S: Supported F: docs/devel/vfio-user.rst +F: hw/vfio/user.c +F: hw/vfio/user.h vhost M: Michael S. Tsirkin <[email protected]>
hw/vfio/meson.build +1 -0
@@ -9,6 +9,7 @@ vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files( 'pci-quirks.c', 'pci.c', )) +vfio_ss.add(when: 'CONFIG_VFIO_USER', if_true: files('user.c')) vfio_ss.add(when: 'CONFIG_VFIO_CCW', if_true: files('ccw.c')) vfio_ss.add(when: 'CONFIG_VFIO_PLATFORM', if_true: files('platform.c')) vfio_ss.add(when: 'CONFIG_VFIO_XGMAC', if_true: files('calxeda-xgmac.c'))
Signature · 1 line
1.8.3.1
RFC v3 5/19 Add validation ops vector
In reply to John JohnsonValidates cases where the return values aren't fully trusted (prep work for vfio-user, where the return values from the remote process aren't trusted)
--- include/hw/vfio/vfio-common.h | 21 ++++++++++++++ hw/vfio/pci.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+)
include/hw/vfio/vfio-common.h +21 -0
@@ -125,6 +125,7 @@ typedef struct VFIOHostDMAWindow {typedef struct VFIODeviceOps VFIODeviceOps ;typedef struct VFIODevIO VFIODevIO ; +typedef struct VFIOValidOps VFIOValidOps ; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next;
@@ -141,6 +142,7 @@ typedef struct VFIODevice {bool enable_migration;VFIODeviceOps *ops;VFIODevIO *io_ops; +VFIOValidOps *valid_ops;unsigned int num_irqs;unsigned int num_regions;unsigned int flags;
@@ -214,6 +216,25 @@ struct VFIOContIO {extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl; +/* +* This ops vector allows for bus-specific verification +* routines in cases where the server may not be fully +* trusted. +*/ +struct VFIOValidOps { +int (*validate_get_info)(VFIODevice *vdev,struct vfio_device_info *info); +int (*validate_get_region_info)(VFIODevice *vdev, +struct vfio_region_info *info,int *fd); +int (*validate_get_irq_info)(VFIODevice *vdev,struct vfio_irq_info *info); +}; + +#define VDEV_VALID_INFO (vdev, info) \ + ((vdev)->valid_ops->validate_get_info((vdev), (info))) +#define VDEV_VALID_REGION_INFO (vdev, info, fd) \ + ((vdev)->valid_ops->validate_get_region_info((vdev), (info), (fd))) +#define VDEV_VALID_IRQ_INFO (vdev, irq) \ + ((vdev)->valid_ops->validate_get_irq_info((vdev), (irq))) +#endif /* CONFIG_LINUX */ typedef struct VFIOGroup {
hw/vfio/pci.c +67 -0
70 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 14/19 vfio-user: get and set IRQs
In reply to John Johnson--- hw/vfio/user-protocol.h | 25 ++++++++++ hw/vfio/pci.c | 9 +++- hw/vfio/user.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+), 2 deletions(-)
hw/vfio/user-protocol.h +25 -0
@@ -110,6 +110,31 @@ typedef struct { } 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/pci.c +7 -2
@@ -506,7 +506,7 @@ static int vfio_msix_vector_do_use(PCIDevice *pdev, unsigned int nr, vdev->nr_vectors = nr +1 ; ret =vfio_enable_vectors (vdev, true);if (ret) { -error_report ("vfio: failed to enable vectors, %d" , ret); +error_report ("vfio: failed to enable vectors, %s" ,strerror (-ret)); } }else {Error *err =NULL ;
@@ -651,7 +651,8 @@ retry: ret =vfio_enable_vectors (vdev, false);if (ret) {if (ret <0 ) { -error_report ("vfio: Error: Failed to setup MSI fds: %m" ); +error_report ("vfio: Error: Failed to setup MSI fds: %s" , +strerror (-ret)); }else if (ret != vdev->nr_vectors) {error_report ("vfio: Error: Failed to enable %d " "MSI vectors, retry with %d" , vdev->nr_vectors, ret);
@@ -2662,6 +2663,7 @@ static void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp) irq_info.index =VFIO_PCI_ERR_IRQ_INDEX ; ret =VDEV_GET_IRQ_INFO (vbasedev, &irq_info); +if (ret) {/* This can fail for an old kernel or legacy PCI dev */ trace_vfio_populate_device_get_irq_info_failure (strerror (errno));
@@ -3630,6 +3632,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) } } +vfio_register_err_notifier (vdev); +vfio_register_req_notifier (vdev); +return ; out_deregister:
hw/vfio/user.c +128 -0
146 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 6/19 vfio-user: Define type vfio_user_pci_dev_info
In reply to John JohnsonNew class for vfio-user with its class and instance constructors and destructors, and its pci ops.
--- hw/vfio/pci.h | 9 ++++++ hw/vfio/pci.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/Kconfig | 10 ++++++ 3 files changed, 116 insertions(+)
hw/vfio/pci.h +9 -0
@@ -187,6 +187,15 @@ struct VFIOKernPCIDevice {VFIOPCIDevice device; }; +#define TYPE_VFIO_USER_PCI "vfio-user-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) + +struct VFIOUserPCIDevice { +VFIOPCIDevice device; +char *sock_name; +bool secure_dma;/* disable shared mem for DMA */ +}; +/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ static inline bool vfio_pci_is (VFIOPCIDevice *vdev,uint32_t vendor,uint32_t device) {
hw/vfio/pci.c +97 -0
106 lines not on this page · raw message
hw/vfio/Kconfig +10 -0
@@ -2,6 +2,10 @@ config VFIO bool depends on LINUX +config VFIO_USER + bool + depends on VFIO + config VFIO_PCI bool default y
@@ -9,6 +13,12 @@ config VFIO_PCI select EDID depends on LINUX && PCI +config VFIO_USER_PCI + bool + default y + select VFIO_USER + depends on VFIO_PCI + config VFIO_CCW bool default y
Signature · 1 line
1.8.3.1
RFC v3 17/19 vfio-user: dma read/write operations
In reply to John Johnson--- hw/vfio/user-protocol.h | 11 +++++ hw/vfio/user.h | 4 ++ hw/vfio/pci.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/user.c | 78 ++++++++++++++++++++++++++++++----- 4 files changed, 188 insertions(+), 10 deletions(-)
hw/vfio/user-protocol.h +11 -0
@@ -171,6 +171,17 @@ typedef struct {char data[]; } VFIOUserRegionRW; +/* +* 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;
hw/vfio/user.h +4 -0
@@ -80,9 +80,13 @@ typedef struct VFIOProxy {VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp);void vfio_user_disconnect (VFIOProxy *proxy); +uint64_t vfio_user_max_xfer (void );void vfio_user_set_handler (VFIODevice *vbasedev,void (*handler)(void *opaque,VFIOUserMsg *msg),void *reqarg); +void vfio_user_send_reply (VFIOProxy *proxy,VFIOUserHdr *hdr,int size); +void vfio_user_send_error (VFIOProxy *proxy,VFIOUserHdr *hdr,int error); +void vfio_user_putfds (VFIOUserMsg *msg);int vfio_user_validate_version (VFIODevice *vbasedev,Error **errp);extern VFIODevIO vfio_dev_io_sock;
hw/vfio/pci.c +105 -0
116 lines not on this page · raw message
hw/vfio/user.c +68 -10
114 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 12/19 vfio-user: region read/write
In reply to John Johnson--- hw/vfio/pci.h | 1 + hw/vfio/user-protocol.h | 12 +++++ hw/vfio/user.h | 1 + include/hw/vfio/vfio-common.h | 1 + hw/vfio/common.c | 7 ++- hw/vfio/pci.c | 7 +++ hw/vfio/user.c | 101 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 1 deletion(-)
hw/vfio/pci.h +1 -0
@@ -194,6 +194,7 @@ struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name;bool send_queued;/* all sends are queued */ +bool no_post;/* all regions write are sync */ };/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */
hw/vfio/user-protocol.h +12 -0
@@ -109,4 +109,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.h +1 -0
@@ -75,6 +75,7 @@ typedef struct VFIOProxy {/* VFIOProxy flags */ #define VFIO_PROXY_CLIENT 0x1#define VFIO_PROXY_FORCE_QUEUED 0x4 +#define VFIO_PROXY_NO_POST 0x8VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp);void vfio_user_disconnect (VFIOProxy *proxy);
include/hw/vfio/vfio-common.h +1 -0
@@ -56,6 +56,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 */ int remfd;/* fd if exported from remote process */ } VFIORegion;
hw/vfio/common.c +6 -1
@@ -213,6 +213,7 @@ void vfio_region_write(void *opaque, hwaddr addr,uint32_t dword;uint64_t qword; } buf; +bool post = region->post_wr;int ret;switch (size) {
@@ -233,7 +234,11 @@ void vfio_region_write(void *opaque, hwaddr addr,break ; } - ret =VDEV_REGION_WRITE (vbasedev, region->nr, addr, size, &buf, false); +/* read-after-write hazard if guest can directly access region */ +if (region->nr_mmaps) { + post = false; + } + ret =VDEV_REGION_WRITE (vbasedev, region->nr, addr, size, &buf, post);if (ret != size) {const char *err = ret <0 ?strerror (-ret) :"short write" ;
hw/vfio/pci.c +7 -0
@@ -1665,6 +1665,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)
@@ -3513,6 +3516,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)if (udev->send_queued) { proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; } +if (udev->no_post) { + proxy->flags |=VFIO_PROXY_NO_POST ; + }vfio_user_validate_version (vbasedev, &err);if (err !=NULL ) {
@@ -3565,6 +3571,7 @@ static void vfio_user_instance_finalize(Object *obj)static 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_BOOL ("x-no-posted-writes" , VFIOUserPCIDevice, no_post, false),DEFINE_PROP_END_OF_LIST (), };
hw/vfio/user.c +101 -0
127 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 8/19 vfio-user: define socket receive functions
In reply to John JohnsonAdd infrastructure needed to receive incoming messages
--- hw/vfio/pci.h | 2 +- hw/vfio/user-protocol.h | 62 +++++++++ hw/vfio/user.h | 9 +- hw/vfio/pci.c | 12 +- hw/vfio/user.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 1 + 6 files changed, 409 insertions(+), 3 deletions(-) create mode 100644 hw/vfio/user-protocol.h
hw/vfio/pci.h +1 -1
@@ -193,7 +193,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name; -bool secure_dma;/* disable shared mem for DMA */ +bool send_queued;/* all sends are queued */ };/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */
hw/vfio/user-protocol.h new file +62 -0
62 lines not on this page · raw message
hw/vfio/user.h +8 -1
@@ -11,6 +11,8 @@* */ +#include "user-protocol.h" + typedef struct { int send_fds; int recv_fds;
@@ -27,6 +29,7 @@ enum msg_type { typedef struct VFIOUserMsg { QTAILQ_ENTRY(VFIOUserMsg) next; + VFIOUserHdr *hdr; VFIOUserFDs *fds; uint32_t rsize; uint32_t id;
@@ -70,9 +73,13 @@ typedef struct VFIOProxy { } VFIOProxy;/* VFIOProxy flags */ -#define VFIO_PROXY_CLIENT 0x1 +#define VFIO_PROXY_CLIENT 0x1 +#define VFIO_PROXY_FORCE_QUEUED 0x4VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp);void vfio_user_disconnect (VFIOProxy *proxy); +void vfio_user_set_handler (VFIODevice *vbasedev, +void (*handler)(void *opaque,VFIOUserMsg *msg), +void *reqarg);#endif /* VFIO_USER_H */
hw/vfio/pci.c +11 -1
@@ -3448,6 +3448,11 @@ struct VFIOValidOps vfio_pci_valid_ops = {* vfio-user routines. */ +static void vfio_user_pci_process_req (void *opaque,VFIOUserMsg *msg) +{ + +} +/* * Emulated devices don't use host hot reset */
@@ -3501,6 +3506,11 @@ 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); + +if (udev->send_queued) { + proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; + } vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); vbasedev->dev =DEVICE (vdev);
@@ -3524,7 +3534,7 @@ static void vfio_user_instance_finalize(Object *obj)static Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), -DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false), +DEFINE_PROP_BOOL ("x-send-queued" , VFIOUserPCIDevice, send_queued, false),DEFINE_PROP_END_OF_LIST (), };
hw/vfio/user.c +326 -0
370 lines not on this page · raw message
MAINTAINERS +1 -0
@@ -1888,6 +1888,7 @@ S: Supported F: docs/devel/vfio-user.rst F: hw/vfio/user.c F: hw/vfio/user.h +F: hw/vfio/user-protocol.h vhost M: Michael S. Tsirkin <[email protected]>
Signature · 1 line
1.8.3.1
RFC v3 18/19 vfio-user: pci reset
In reply to John Johnson--- hw/vfio/user.h | 1 + hw/vfio/pci.c | 15 +++++++++++++++ hw/vfio/user.c | 12 ++++++++++++ 3 files changed, 28 insertions(+)
hw/vfio/user.h +1 -0
@@ -88,6 +88,7 @@ void vfio_user_send_reply(VFIOProxy *proxy, VFIOUserHdr *hdr, int size);void vfio_user_send_error (VFIOProxy *proxy,VFIOUserHdr *hdr,int error);void vfio_user_putfds (VFIOUserMsg *msg);int vfio_user_validate_version (VFIODevice *vbasedev,Error **errp); +void vfio_user_reset (VFIOProxy *proxy);extern VFIODevIO vfio_dev_io_sock;extern VFIOContIO vfio_cont_io_sock;
hw/vfio/pci.c +15 -0
@@ -3792,6 +3792,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_reset (vbasedev->proxy); + } + +vfio_pci_post_reset (vdev); +} +static Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name),DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false),
@@ -3805,6 +3819,7 @@ static void vfio_user_pci_dev_class_init(ObjectClass *klass, void *data)DeviceClass *dc =DEVICE_CLASS (klass);PCIDeviceClass *pdc =PCI_DEVICE_CLASS (klass); + dc->reset = 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;
hw/vfio/user.c +12 -0
@@ -1386,6 +1386,18 @@ static int vfio_user_region_write(VFIOProxy *proxy, uint8_t index, off_t offset,return ret; } +void vfio_user_reset (VFIOProxy *proxy) +{ +VFIOUserHdr msg; + +vfio_user_request_msg (&msg,VFIO_USER_DEVICE_RESET ,sizeof (msg),0 ); + +vfio_user_send_wait (proxy, &msg,NULL ,0 , false); +if (msg.flags &VFIO_USER_ERROR ) { +error_printf ("reset reply error %d\n" , msg.error_reply); + } +} +/* * Socket-based io_ops
Signature · 1 line
1.8.3.1
RFC v3 3/19 Add container IO ops vector
In reply to John JohnsonUsed for communication with VFIO driver
(prep work for vfio-user, which will communicate over a socket)
--- include/hw/vfio/vfio-common.h | 34 +++++++++++ hw/vfio/common.c | 131 ++++++++++++++++++++++++++++-------------- 2 files changed, 123 insertions(+), 42 deletions(-)
include/hw/vfio/vfio-common.h +34 -0
@@ -75,6 +75,7 @@ typedef struct VFIOAddressSpace { } VFIOAddressSpace;struct VFIOGroup ; +typedef struct VFIOContIO VFIOContIO ; typedef struct VFIOContainer { VFIOAddressSpace *space;
@@ -83,6 +84,7 @@ typedef struct VFIOContainer {MemoryListener prereg_listener;unsigned iommu_type;Error *error; +VFIOContIO *io_ops;bool initialized;bool dirty_pages_supported;uint64_t dirty_pgsizes;
@@ -154,6 +156,38 @@ struct VFIODeviceOps {int (*vfio_load_config)(VFIODevice *vdev,QEMUFile *f); }; +#ifdef CONFIG_LINUX + +/* +* The next 2 ops vectors are how Devices and Containers +* communicate with the server. The default option is +* through ioctl() to the kernel VFIO driver, but vfio-user +* can use a socket to a remote process. +*/ + +struct VFIOContIO { +int (*dma_map)(VFIOContainer *container, +struct vfio_iommu_type1_dma_map *map, +int fd,bool will_commit); +int (*dma_unmap)(VFIOContainer *container, +struct vfio_iommu_type1_dma_unmap *unmap, +struct vfio_bitmap *bitmap,bool will_commit); +int (*dirty_bitmap)(VFIOContainer *container, +struct vfio_iommu_type1_dirty_bitmap *bitmap, +struct vfio_iommu_type1_dirty_bitmap_get *range); +}; + +#define CONT_DMA_MAP (cont, map, fd, will_commit) \ + ((cont)->io_ops->dma_map((cont), (map), (fd), (will_commit))) +#define CONT_DMA_UNMAP (cont, unmap, bitmap, will_commit) \ + ((cont)->io_ops->dma_unmap((cont), (unmap), (bitmap), (will_commit))) +#define CONT_DIRTY_BITMAP (cont, bitmap, range) \ + ((cont)->io_ops->dirty_bitmap((cont), (bitmap), (range))) + +extern VFIOContIO vfio_cont_io_ioctl; + +#endif /* CONFIG_LINUX */ + typedef struct VFIOGroup { int fd; int groupid;
hw/vfio/common.c +89 -42
185 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 1/19 vfio-user: introduce vfio-user protocol specification
In reply to John JohnsonFrom: 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.
This patch is sourced from:
https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/
It has been earlier discussed as an RFC in:
"RFC: use VFIO over a UNIX domain socket to implement device offloading"
--- docs/devel/index.rst | 1 + docs/devel/vfio-user.rst | 1810 ++++++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 6 + 3 files changed, 1817 insertions(+) create mode 100644 docs/devel/vfio-user.rst
docs/devel/index.rst +1 -0
@@ -44,3 +44,4 @@ modifying QEMU's source code. vfio-migration qapi-code-gen writing-qmp-commands + vfio-user
docs/devel/vfio-user.rst new file +1810 -0
1,810 lines not on this page · raw message
MAINTAINERS +6 -0
@@ -1879,6 +1879,12 @@ F: hw/vfio/ap.c F: docs/system/s390x/vfio-ap.rst L: [email protected] +vfio-user +M: John G Johnson <[email protected]> +M: Thanos Makatos <[email protected]> +S: Supported +F: docs/devel/vfio-user.rst + vhost M: Michael S. Tsirkin <[email protected]> S: Supported
Signature · 1 line
1.8.3.1
RFC v3 19/19 vfio-user: migration support
In reply to John Johnsonbug fix: only set qemu file error if there is a file
--- hw/vfio/user-protocol.h | 18 +++++++++++++++++ hw/vfio/migration.c | 34 +++++++++++++++---------------- hw/vfio/pci.c | 7 +++++++ hw/vfio/user.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 17 deletions(-)
hw/vfio/user-protocol.h +18 -0
@@ -182,6 +182,10 @@ typedef struct {char data[]; } VFIOUserDMARW; +/* +* VFIO_USER_DIRTY_PAGES +*/ +/*imported from struct vfio_bitmap */ typedef struct { uint64_t pgsize;
@@ -189,4 +193,18 @@ typedef struct { char data[]; } VFIOUserBitmap; +/* imported from struct vfio_iommu_type1_dirty_bitmap_get */ +typedef struct { +uint64_t iova; +uint64_t size; +VFIOUserBitmap bitmap; +}VFIOUserBitmapRange ; + +/* imported from struct vfio_iommu_type1_dirty_bitmap */ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +}VFIOUserDirtyPages ; +#endif /* VFIO_USER_PROTOCOL_H */
hw/vfio/migration.c +17 -17
@@ -27,6 +27,7 @@#include "pci.h" #include "trace.h" #include "hw/hw.h" +#include "user.h" /* * Flags to be used as unique delimiters for VFIO devices in the migration
@@ -49,11 +50,13 @@ static int64_t bytes_transferred;static inline int vfio_mig_access (VFIODevice *vbasedev,void *val,int count,off_t off,bool iswrite) { +VFIORegion *region = &vbasedev->migration->region;int ret; - ret = iswrite ?pwrite (vbasedev->fd, val, count, off) : -pread (vbasedev->fd, val, count, off); -if (ret < count) { + ret = iswrite ? +VDEV_REGION_WRITE (vbasedev, region->nr, off, count, val, false) : +VDEV_REGION_READ (vbasedev, region->nr, off, count, val); +if (ret < count) {error_report ("vfio_mig_%s %d byte %s: failed at offset 0x%" HWADDR_PRIx", err: %s" , iswrite ?"write" :"read" , count, vbasedev->name, off,strerror (errno));
@@ -111,9 +114,7 @@ static int vfio_migration_set_state(VFIODevice *vbasedev, uint32_t mask, uint32_t value) {VFIOMigration *migration = vbasedev->migration; -VFIORegion *region = &migration->region; -off_t dev_state_off = region->fd_offset + -VFIO_MIG_STRUCT_OFFSET (device_state); +off_t dev_state_off =VFIO_MIG_STRUCT_OFFSET (device_state);uint32_t device_state;int ret;
@@ -201,13 +202,13 @@ static int vfio_save_buffer(QEMUFile *f, VFIODevice *vbasedev, uint64_t *size)int ret; ret =vfio_mig_read (vbasedev, &data_offset,sizeof (data_offset), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (data_offset)); +VFIO_MIG_STRUCT_OFFSET (data_offset));if (ret <0 ) {return ret; } ret =vfio_mig_read (vbasedev, &data_size,sizeof (data_size), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (data_size)); +VFIO_MIG_STRUCT_OFFSET (data_size));if (ret <0 ) {return ret; }
@@ -233,8 +234,7 @@ static int vfio_save_buffer(QEMUFile *f, VFIODevice *vbasedev, uint64_t *size) } buf_allocated = true; - ret =vfio_mig_read (vbasedev, buf, sec_size, - region->fd_offset + data_offset); + ret =vfio_mig_read (vbasedev, buf, sec_size, data_offset);if (ret <0 ) {g_free (buf);return ret;
@@ -269,7 +269,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev, do { ret = vfio_mig_read(vbasedev, &data_offset, sizeof(data_offset), - region->fd_offset + VFIO_MIG_STRUCT_OFFSET(data_offset)); + VFIO_MIG_STRUCT_OFFSET(data_offset)); if (ret < 0) { return ret; }
@@ -309,8 +309,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,qemu_get_buffer (f, buf, sec_size);if (buf_alloc) { - ret =vfio_mig_write (vbasedev, buf, sec_size, - region->fd_offset + data_offset); + ret =vfio_mig_write (vbasedev, buf, sec_size, data_offset);g_free (buf);if (ret <0 ) {
@@ -322,7 +321,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev, } ret =vfio_mig_write (vbasedev, &report_size,sizeof (report_size), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (data_size)); +VFIO_MIG_STRUCT_OFFSET (data_size));if (ret <0 ) {return ret; }
@@ -334,12 +333,11 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,static int vfio_update_pending (VFIODevice *vbasedev) {VFIOMigration *migration = vbasedev->migration; -VFIORegion *region = &migration->region;uint64_t pending_bytes =0 ;int ret; ret =vfio_mig_read (vbasedev, &pending_bytes,sizeof (pending_bytes), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (pending_bytes)); +VFIO_MIG_STRUCT_OFFSET (pending_bytes));if (ret <0 ) { migration->pending_bytes =0 ;return ret;
@@ -744,7 +742,9 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state)*/ error_report ("%s: Failed to set device state 0x%x" , vbasedev->name, (migration->device_state & mask) | value); -qemu_file_set_error (migrate_get_current ()->to_dst_file, ret); +if (value !=0 ) { +qemu_file_set_error (migrate_get_current ()->to_dst_file, ret); + } } vbasedev->migration->vm_running = running; trace_vfio_vmstate_change(vbasedev->name, running, RunState_str(state),
hw/vfio/pci.c +7 -0
@@ -3754,6 +3754,13 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) } } +if (!pdev->failover_pair_id) { + ret =vfio_migration_probe (&vdev->vbasedev, errp); +if (ret) { +error_report ("%s: Migration disabled" , vdev->vbasedev.name); + } + } +vfio_register_err_notifier (vdev);vfio_register_req_notifier (vdev);
hw/vfio/user.c +54 -0
71 lines not on this page · raw message
Signature · 1 line
1.8.3.1
RFC v3 15/19 vfio-user: proxy container connect/disconnect
In reply to John Johnson--- hw/vfio/user.h | 1 + include/hw/vfio/vfio-common.h | 3 ++ hw/vfio/common.c | 98 +++++++++++++++++++++++++++++++++++++++++++ hw/vfio/pci.c | 24 +++++++++++ hw/vfio/user.c | 3 ++ 5 files changed, 129 insertions(+)
hw/vfio/user.h +1 -0
@@ -85,5 +85,6 @@ void vfio_user_set_handler(VFIODevice *vbasedev,int vfio_user_validate_version (VFIODevice *vbasedev,Error **errp);extern VFIODevIO vfio_dev_io_sock; +extern VFIOContIO vfio_cont_io_sock;#endif /* VFIO_USER_H */
include/hw/vfio/vfio-common.h +3 -0
@@ -94,6 +94,7 @@ typedef struct VFIOContainer {uint64_t max_dirty_bitmap_size;unsigned long pgsizes;unsigned int dma_max_mappings; +VFIOProxy *proxy; QLIST_HEAD(, VFIOGuestIOMMU) giommu_list; QLIST_HEAD(, VFIOHostDMAWindow) hostwin_list; QLIST_HEAD(, VFIOGroup) group_list;
@@ -300,6 +301,8 @@ VFIOGroup *vfio_get_group(int groupid, AddressSpace *as, Error **errp);void vfio_put_group (VFIOGroup *group);int vfio_get_device (VFIOGroup *group,const char *name,VFIODevice *vbasedev,Error **errp); +void vfio_connect_proxy (VFIOProxy *proxy,VFIOGroup *group,AddressSpace *as); +void vfio_disconnect_proxy (VFIOGroup *group);extern const MemoryRegionOps vfio_region_ops; typedef QLIST_HEAD(VFIOGroupList, VFIOGroup) VFIOGroupList;
hw/vfio/common.c +98 -0
116 lines not on this page · raw message
hw/vfio/pci.c +24 -0
@@ -3489,6 +3489,7 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)VFIODevice *vbasedev = &vdev->vbasedev;SocketAddress addr;VFIOProxy *proxy; +VFIOGroup *group =NULL ;struct vfio_device_info info;int ret;Error *err =NULL ;
@@ -3537,6 +3538,19 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) vbasedev->io_ops = &vfio_dev_io_sock; vbasedev->valid_ops = &vfio_pci_valid_ops; +/* +* each device gets its own group and container +* make them unrelated to any host IOMMU groupings +*/ + group =g_malloc0 (sizeof (*group)); + group->fd =-1 ; + group->groupid =-1 ; +QLIST_INIT (&group->device_list); +QLIST_INSERT_HEAD (&group->device_list, vbasedev, next); + vbasedev->group = group; + +vfio_connect_proxy (proxy, group,pci_device_iommu_address_space (pdev)); + ret =VDEV_GET_INFO (vbasedev, &info);if (ret) {error_setg_errno (errp, -ret,"get info failure" );
@@ -3644,6 +3658,9 @@ out_teardown:vfio_teardown_msi (vdev);vfio_bars_exit (vdev); error: +if (group !=NULL ) { +vfio_disconnect_proxy (group); + }vfio_user_disconnect (proxy);error_prepend (errp,VFIO_MSG_PREFIX , vdev->vbasedev.name); }
@@ -3652,6 +3669,13 @@ static void vfio_user_instance_finalize(Object *obj) {VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj);VFIODevice *vbasedev = &vdev->vbasedev; +VFIOGroup *group = vbasedev->group; + +if (group !=NULL ) { +vfio_disconnect_proxy (group); +g_free (group); + vbasedev->group =NULL ; + }vfio_put_device (vdev);
hw/vfio/user.c +3 -0
@@ -1224,3 +1224,6 @@ VFIODevIO vfio_dev_io_sock = { .region_write = vfio_user_io_region_write, }; + +VFIOContIO vfio_cont_io_sock = { +};
Signature · 1 line
1.8.3.1
Re: [RFC v3 12/19] vfio-user: region read/write
In reply to John JohnsonQuoted 63 lines · On Mon, 8 Nov 2021 16:46:40 -0800 John Johnson <[email protected]>…
Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/pci.h | 1 + hw/vfio/user-protocol.h | 12 +++++ hw/vfio/user.h | 1 + include/hw/vfio/vfio-common.h | 1 + hw/vfio/common.c | 7 ++- hw/vfio/pci.c | 7 +++ hw/vfio/user.c | 101 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 1 deletion(-)hw/vfio/pci.h +1
-0@@ -194,6 +194,7 @@ struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name;bool send_queued;/* all sends are queued */ +bool no_post;/* all regions write are sync */ };/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ hw/vfio/user-protocol.h +12
-0@@ -109,4 +109,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.h +1
-0@@ -75,6 +75,7 @@ typedef struct VFIOProxy {/* VFIOProxy flags */ #define VFIO_PROXY_CLIENT 0x1#define VFIO_PROXY_FORCE_QUEUED 0x4 +#define VFIO_PROXY_NO_POST 0x8VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp);void vfio_user_disconnect (VFIOProxy *proxy);include/hw/vfio/vfio-common.h +0
-0
include/hw/vfio/vfio-common.h typedef struct VFIORegion { · line 59
Quoted 4 lines
include/hw/vfio/vfio-common.h +1
-0@@ -56,6 +56,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 */
As with the fd in the previous patch, this is where the concept of posted writes should be introduced throughout. Or maybe even better would be to introduce write support without posting and the next patch could expose posted writes. Thanks,
Alex
Quoted 195 lines
include/hw/vfio/vfio-common.h +0
-0int remfd;/* fd if exported from remote process */ } VFIORegion;hw/vfio/common.c +6
-1@@ -213,6 +213,7 @@ void vfio_region_write(void *opaque, hwaddr addr,uint32_t dword;uint64_t qword; } buf; +bool post = region->post_wr;int ret;switch (size) {@@ -233,7 +234,11 @@ void vfio_region_write(void *opaque, hwaddr addr,break ; } - ret =VDEV_REGION_WRITE (vbasedev, region->nr, addr, size, &buf, false); +/* read-after-write hazard if guest can directly access region */ +if (region->nr_mmaps) { + post = false; + } + ret =VDEV_REGION_WRITE (vbasedev, region->nr, addr, size, &buf, post);if (ret != size) {const char *err = ret <0 ?strerror (-ret) :"short write" ;hw/vfio/pci.c +7
-0@@ -1665,6 +1665,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)@@ -3513,6 +3516,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)if (udev->send_queued) { proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; } +if (udev->no_post) { + proxy->flags |=VFIO_PROXY_NO_POST ; + }vfio_user_validate_version (vbasedev, &err);if (err !=NULL ) {@@ -3565,6 +3571,7 @@ static void vfio_user_instance_finalize(Object *obj)static 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_BOOL ("x-no-posted-writes" , VFIOUserPCIDevice, no_post, false),DEFINE_PROP_END_OF_LIST (), };hw/vfio/user.c +101
-0@@ -50,6 +50,8 @@ static void vfio_user_cb(void *opaque);static void vfio_user_request (void *opaque);static int vfio_user_send_queued (VFIOProxy *proxy,VFIOUserMsg *msg); +static void vfio_user_send_async (VFIOProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds);static void vfio_user_send_wait (VFIOProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds,int rsize,bool nobql); static void vfio_user_request_msg(VFIOUserHdr *hdr, uint16_t cmd,@@ -533,6 +535,33 @@ static int vfio_user_send_queued(VFIOProxy *proxy, VFIOUserMsg *msg)return 0 ; } +/* +* async send - msg can be queued, but will be freed when sent +*/ +static void vfio_user_send_async (VFIOProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds) +{ +VFIOUserMsg *msg; +int ret; + +if (!(hdr->flags & (VFIO_USER_NO_REPLY |VFIO_USER_REPLY ))) { +error_printf ("vfio_user_send_async on sync message\n" ); +return ; + } + +QEMU_LOCK_GUARD (&proxy->lock); + + msg =vfio_user_getmsg (proxy, hdr, fds); + msg->id = hdr->id; + msg->rsize =0 ; + msg->type =VFIO_MSG_ASYNC ; + + ret =vfio_user_send_queued (proxy, msg); +if (ret <0 ) { +vfio_user_recycle (proxy, msg); + } +} +static void vfio_user_send_wait (VFIOProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds,int rsize,bool nobql) {@@ -957,6 +986,62 @@ static int vfio_user_get_region_info(VFIOProxy *proxy,return 0 ; } +static int vfio_user_region_read (VFIOProxy *proxy,uint8_t index,off_t offset, +uint32_t count,void *data) +{ + g_autofree VFIOUserRegionRW *msgp = NULL; +int size =sizeof (*msgp) + count; + + msgp =g_malloc0 (size); +vfio_user_request_msg (&msgp->hdr,VFIO_USER_REGION_READ ,sizeof (*msgp),0 ); + msgp->offset = offset; + msgp->region = index; + msgp->count = count; + +vfio_user_send_wait (proxy, &msgp->hdr,NULL , size, false); +if (msgp->hdr.flags &VFIO_USER_ERROR ) { +return -msgp->hdr.error_reply; + }else if (msgp->count > count) { +return -E2BIG ; + }else { +memcpy (data, &msgp->data, msgp->count); + } + +return msgp->count; +} + +static int vfio_user_region_write (VFIOProxy *proxy,uint8_t index,off_t offset, +uint32_t count,void *data,bool post) +{ +VFIOUserRegionRW *msgp =NULL ; +int flags = post ?VFIO_USER_NO_REPLY :0 ; +int size =sizeof (*msgp) + count; +int ret; + + msgp =g_malloc0 (size); +vfio_user_request_msg (&msgp->hdr,VFIO_USER_REGION_WRITE , size, flags); + msgp->offset = offset; + msgp->region = index; + msgp->count = count; +memcpy (&msgp->data, data, count); + +/* async send will free msg after it's sent */ +if (post && !(proxy->flags &VFIO_PROXY_NO_POST )) { +vfio_user_send_async (proxy, &msgp->hdr,NULL ); +return count; + } + +vfio_user_send_wait (proxy, &msgp->hdr,NULL ,0 , false); +if (msgp->hdr.flags &VFIO_USER_ERROR ) { + ret = -msgp->hdr.error_reply; + }else { + ret = count; + } + +g_free (msgp); +return ret; +} +/* * Socket-based io_ops @@ -990,8 +1075,24 @@ static int vfio_user_io_get_region_info(VFIODevice *vbasedev,return VDEV_VALID_REGION_INFO (vbasedev, info, fd); } +static int vfio_user_io_region_read (VFIODevice *vbasedev,uint8_t index, +off_t off,uint32_t size,void *data) +{ +return vfio_user_region_read (vbasedev->proxy, index, off, size, data); +} + +static int vfio_user_io_region_write (VFIODevice *vbasedev,uint8_t index, +off_t off,unsigned size,void *data, +bool post) +{ +return vfio_user_region_write (vbasedev->proxy, index, off, size, data, + post); +} +VFIODevIO vfio_dev_io_sock = { .get_info = vfio_user_io_get_info, .get_region_info = vfio_user_io_get_region_info, + .region_read = vfio_user_io_region_read, + .region_write = vfio_user_io_region_write, };
Re: [RFC v3 11/19] vfio-user: get region info
In reply to John JohnsonQuoted 71 lines · On Mon, 8 Nov 2021 16:46:39 -0800 John Johnson <[email protected]>…
Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/user-protocol.h | 14 ++++++++++++ include/hw/vfio/vfio-common.h | 4 +++- hw/vfio/common.c | 30 +++++++++++++++++++++++++- hw/vfio/user.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-)hw/vfio/user-protocol.h +14
-0@@ -95,4 +95,18 @@ typedef struct { uint32_t cap_offset; } 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 */ include/hw/vfio/vfio-common.h +3
-1@@ -56,6 +56,7 @@ typedef struct VFIORegion {uint32_t nr_mmaps;VFIOMmap *mmaps;uint8_t nr;/* cache the region number for debug */ +int remfd;/* fd if exported from remote process */ } VFIORegion; typedef struct VFIOMigration {@@ -150,8 +151,9 @@ typedef struct VFIODevice {VFIOMigration *migration;Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking; -struct vfio_region_info **regions;VFIOProxy *proxy; +struct vfio_region_info **regions; +int *regfds; } VFIODevice;struct VFIODeviceOps {hw/vfio/common.c +1
-0@@ -40,6 +40,7 @@#include "trace.h" #include "qapi/error.h" #include "migration/migration.h" +#include "hw/vfio/user.h" VFIOGroupList vfio_group_list =QLIST_HEAD_INITIALIZER (vfio_group_list);@@ -1491,6 +1492,16 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,return true; }
hw/vfio/common.c bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, · line 1503
Quoted 10 lines
hw/vfio/common.c +9
-0+static int vfio_get_region_info_remfd (VFIODevice *vbasedev,int index) +{ +struct vfio_region_info *info; + +if (vbasedev->regions ==NULL || vbasedev->regions[index] ==NULL ) { +vfio_get_region_info (vbasedev, index, &info); + } +return vbasedev->regfds !=NULL ? vbasedev->regfds[index] :-1 ; +}
This patch is really obscure due to the region info fd being added many patches ago and only now being used.
Do we really want a parallel array to regions for storing these fds?
Why do we call an array of these fds "regfds" but a single one "remfd"?
Ugh, why do we have both the regfds array and a remfd per VFIORegion?
TBH, I'm still not sure why we're caching region infos at all, this seems to be gratuitously bloated.
hw/vfio/common.c int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, · line 1558
Quoted 9 lines
hw/vfio/common.c +2
-0+static int vfio_setup_region_sparse_mmaps (VFIORegion *region,struct vfio_region_info *info) {@@ -1544,6 +1555,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->size = info->size; region->fd_offset = info->offset; region->nr = index; + region->remfd =vfio_get_region_info_remfd (vbasedev, index);
Why didn't we just get an fd back from vfio_get_region_info() that we could use here?
Quoted 12 lines
hw/vfio/common.c +1
-0if (region->size) { region->mem =g_new0 (MemoryRegion,1 );@@ -1587,6 +1599,7 @@ int vfio_region_mmap(VFIORegion *region) {int i, prot =0 ;char *name; +int fd;if (!region->mem) {return 0 ;
hw/vfio/common.c int vfio_region_mmap(VFIORegion *region) · line 1611
Quoted 4 lines
hw/vfio/common.c +1
-0@@ -1595,9 +1608,11 @@ int vfio_region_mmap(VFIORegion *region) prot |= region->flags &VFIO_REGION_INFO_FLAG_READ ?PROT_READ :0 ; prot |= region->flags &VFIO_REGION_INFO_FLAG_WRITE ?PROT_WRITE :0 ; + fd = region->remfd !=-1 ? region->remfd : region->vbasedev->fd;
Gack, why can't VFIORegion.fd be either the remote process fd or the vbasedevfd to avoid all these switches? Thanks,
Alex
Quoted 115 lines
hw/vfio/common.c +15
-1+for (i =0 ; i < region->nr_mmaps; i++) { region->mmaps[i].mmap =mmap (NULL , region->mmaps[i].size, prot, -MAP_SHARED , region->vbasedev->fd, +MAP_SHARED , fd, region->fd_offset + region->mmaps[i].offset);if (region->mmaps[i].mmap ==MAP_FAILED ) {@@ -2379,10 +2394,17 @@ void vfio_put_base_device(VFIODevice *vbasedev)int i;for (i =0 ; i < vbasedev->num_regions; i++) { +if (vbasedev->regfds !=NULL && vbasedev->regfds[i] !=-1 ) { +close (vbasedev->regfds[i]); + }g_free (vbasedev->regions[i]); }g_free (vbasedev->regions); vbasedev->regions =NULL ; +if (vbasedev->regfds !=NULL ) { +g_free (vbasedev->regfds); + vbasedev->regfds =NULL ; + } }if (!vbasedev->group) {@@ -2405,6 +2427,9 @@ int vfio_get_region_info(VFIODevice *vbasedev, int index,if (vbasedev->regions ==NULL ) { vbasedev->regions = g_new0(struct vfio_region_info *, vbasedev->num_regions); +if (vbasedev->proxy !=NULL ) { + vbasedev->regfds =g_new0 (int, vbasedev->num_regions); + } }/* check cache */ if (vbasedev->regions[index] !=NULL ) {@@ -2441,6 +2466,9 @@ retry:/* fill cache */ vbasedev->regions[index] =g_malloc0 (argsz);memcpy (vbasedev->regions[index], *info, argsz); +if (vbasedev->regfds !=NULL ) { + vbasedev->regfds[index] = fd; + }return 0 ; }hw/vfio/user.c +50
-0@@ -923,6 +923,40 @@ static int vfio_user_get_info(VFIOProxy *proxy, struct vfio_device_info *info)return 0 ; } +static int vfio_user_get_region_info (VFIOProxy *proxy, +struct vfio_region_info *info, +VFIOUserFDs *fds) +{ + g_autofree VFIOUserRegionInfo *msgp = NULL; +uint32_t size; + +/* data returned can be larger than vfio_region_info */ +if (info->argsz <sizeof (*info)) { +error_printf ("vfio_user_get_region_info argsz too small\n" ); +return -EINVAL ; + } +if (fds !=NULL && fds->send_fds !=0 ) { +error_printf ("vfio_user_get_region_info can't send FDs\n" ); +return -EINVAL ; + } + + size = info->argsz +sizeof (VFIOUserHdr); + msgp =g_malloc0 (size); + +vfio_user_request_msg (&msgp->hdr,VFIO_USER_DEVICE_GET_REGION_INFO , +sizeof (*msgp),0 ); + msgp->argsz = info->argsz; + msgp->index = info->index; + +vfio_user_send_wait (proxy, &msgp->hdr, fds, size, false); +if (msgp->hdr.flags &VFIO_USER_ERROR ) { +return -msgp->hdr.error_reply; + } + +memcpy (info, &msgp->argsz, info->argsz); +return 0 ; +} +/* * Socket-based io_ops @@ -941,7 +975,23 @@ static int vfio_user_io_get_info(VFIODevice *vbasedev,return VDEV_VALID_INFO (vbasedev, info); } +static int vfio_user_io_get_region_info (VFIODevice *vbasedev, +struct vfio_region_info *info, +int *fd) +{ +int ret; +VFIOUserFDs fds = {0 ,1 , fd}; + + ret =vfio_user_get_region_info (vbasedev->proxy, info, &fds); +if (ret) { +return ret; + } + +return VDEV_VALID_REGION_INFO (vbasedev, info, fd); +} +VFIODevIO vfio_dev_io_sock = { .get_info = vfio_user_io_get_info, + .get_region_info = vfio_user_io_get_region_info, };
Re: [RFC v3 06/19] vfio-user: Define type vfio_user_pci_dev_info
In reply to John JohnsonQuoted 59 lines · On Mon, 8 Nov 2021 16:46:34 -0800 John Johnson <[email protected]>…
New class for vfio-user with its class and instance constructors and destructors, and its pci ops.
Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/pci.h | 9 ++++++ hw/vfio/pci.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/Kconfig | 10 ++++++ 3 files changed, 116 insertions(+)hw/vfio/pci.h +9
-0@@ -187,6 +187,15 @@ struct VFIOKernPCIDevice {VFIOPCIDevice device; }; +#define TYPE_VFIO_USER_PCI "vfio-user-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) + +struct VFIOUserPCIDevice { +VFIOPCIDevice device; +char *sock_name; +bool secure_dma;/* disable shared mem for DMA */ +}; +/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ static inline bool vfio_pci_is (VFIOPCIDevice *vdev,uint32_t vendor,uint32_t device) {hw/vfio/pci.c +12
-0@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include CONFIG_DEVICES #include <linux/vfio.h> #include <sys/ioctl.h> @@ -3438,3 +3439,99 @@ struct VFIOValidOps vfio_pci_valid_ops = { .validate_get_region_info = vfio_pci_valid_region_info, .validate_get_irq_info = vfio_pci_valid_irq_info, }; + + +#ifdef CONFIG_VFIO_USER_PCI + +/* +* vfio-user routines. +*/ + +/* +* Emulated devices don't use host hot reset +*/
hw/vfio/pci.c struct VFIOValidOps vfio_pci_valid_ops = { · line 3462
Quoted 10 lines
hw/vfio/pci.c +10
-0+static int vfio_user_pci_no_reset (VFIODevice *vbasedev) +{ +error_printf ("vfio-user - no hot reset\n" ); +return 0 ; +} + +static void vfio_user_pci_not_needed (VFIODevice *vbasedev) +{ + vbasedev->needs_reset = false; +}
Seems like we should make some of these optional rather than stubbing dummy functions.
Quoted 23 lines
hw/vfio/pci.c +23
-0+ +static VFIODeviceOps vfio_user_pci_ops = { + .vfio_compute_needs_reset = vfio_user_pci_not_needed, + .vfio_hot_reset_multi = vfio_user_pci_no_reset, + .vfio_eoi = vfio_intx_eoi, + .vfio_get_object = vfio_pci_get_object, + .vfio_save_config = vfio_pci_save_config, + .vfio_load_config = vfio_pci_load_config, +}; + +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; + +/* +* TODO: make option parser understand SocketAddress +* and use that instead of having scalar options +* for each socket type. +*/ +if (!udev->sock_name) {
hw/vfio/pci.c struct VFIOValidOps vfio_pci_valid_ops = { · line 3495
Quoted 10 lines
hw/vfio/pci.c +10
-0+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); + vbasedev->dev =DEVICE (vdev); + vbasedev->fd =-1 ; + vbasedev->type =VFIO_DEVICE_TYPE_PCI ; + vbasedev->no_mmap = false;
Why hard coded rather than a property? This is a useful debugging feature to be able to trap all device accesses. The device should work either way.
hw/vfio/pci.c struct VFIOValidOps vfio_pci_valid_ops = { · line 3507
Quoted 12 lines
hw/vfio/pci.c +12
-0+ vbasedev->ops = &vfio_user_pci_ops; + vbasedev->valid_ops = &vfio_pci_valid_ops; + +} + +static void vfio_user_instance_finalize (Object *obj) +{ +} + +static Property vfio_user_pci_dev_properties[] = { +DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false),
Add this when it means something. Thanks,
Alex
Quoted 58 lines
hw/vfio/pci.c +30
-0+ DEFINE_PROP_END_OF_LIST(), +}; + +static void vfio_user_pci_dev_class_init (ObjectClass *klass,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_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) + +#endif /* VFIO_USER_PCI */ hw/vfio/Kconfig +10
-0@@ -2,6 +2,10 @@ config VFIO bool depends on LINUX +config VFIO_USER + bool + depends on VFIO + config VFIO_PCI bool default y@@ -9,6 +13,12 @@ config VFIO_PCI select EDID depends on LINUX && PCI +config VFIO_USER_PCI + bool + default y + select VFIO_USER + depends on VFIO_PCI + config VFIO_CCW bool default y
Re: [RFC v3 04/19] Add device IO ops vector
In reply to John JohnsonQuoted 41 lines · On Mon, 8 Nov 2021 16:46:32 -0800 John Johnson <[email protected]>…
Used for communication with VFIO driver
(prep work for vfio-user, which will communicate over a socket)Signed-off-by John G Johnson <[email protected]>--- include/hw/vfio/vfio-common.h | 28 ++++++++ hw/vfio/common.c | 159 ++++++++++++++++++++++++++++++++++++++---- hw/vfio/pci.c | 146 ++++++++++++++++++++++++-------------- 3 files changed, 265 insertions(+), 68 deletions(-)include/hw/vfio/vfio-common.h +3
-0@@ -124,6 +124,7 @@ typedef struct VFIOHostDMAWindow { } VFIOHostDMAWindow;typedef struct VFIODeviceOps VFIODeviceOps ; +typedef struct VFIODevIO VFIODevIO ; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next;@@ -139,12 +140,14 @@ typedef struct VFIODevice {bool ram_block_discard_allowed;bool enable_migration;VFIODeviceOps *ops; +VFIODevIO *io_ops;unsigned int num_irqs;unsigned int num_regions;unsigned int flags;VFIOMigration *migration;Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking; +struct vfio_region_info **regions; } VFIODevice;struct VFIODeviceOps {@@ -164,6 +167,30 @@ struct VFIODeviceOps {* through ioctl() to the kernel VFIO driver, but vfio-user * can use a socket to a remote process. */
include/hw/vfio/vfio-common.h struct VFIODeviceOps { · line 179
Quoted 10 lines
include/hw/vfio/vfio-common.h +10
-0+struct VFIODevIO { +int (*get_info)(VFIODevice *vdev,struct vfio_device_info *info); +int (*get_region_info)(VFIODevice *vdev, +struct vfio_region_info *info,int *fd); +int (*get_irq_info)(VFIODevice *vdev,struct vfio_irq_info *irq); +int (*set_irqs)(VFIODevice *vdev,struct vfio_irq_set *irqs); +int (*region_read)(VFIODevice *vdev,uint8_t nr,off_t off,uint32_t size, +void *data); +int (*region_write)(VFIODevice *vdev,uint8_t nr,off_t off,uint32_t size, +void *data,bool post);
The @post arg is not really developed in this patch, it would make review easier to add the arg when it becomes implemented and used.
Quoted 136 lines
include/hw/vfio/vfio-common.h +15
-0+}; + +#define VDEV_GET_INFO (vdev, info) \ + ((vdev)->io_ops->get_info((vdev), (info))) +#define VDEV_GET_REGION_INFO (vdev, info, fd) \ + ((vdev)->io_ops->get_region_info((vdev), (info), (fd))) +#define VDEV_GET_IRQ_INFO (vdev, irq) \ + ((vdev)->io_ops->get_irq_info((vdev), (irq))) +#define VDEV_SET_IRQS (vdev, irqs) \ + ((vdev)->io_ops->set_irqs((vdev), (irqs))) +#define VDEV_REGION_READ (vdev, nr, off, size, data) \ + ((vdev)->io_ops->region_read((vdev), (nr), (off), (size), (data))) +#define VDEV_REGION_WRITE (vdev, nr, off, size, data, post) \ + ((vdev)->io_ops->region_write((vdev), (nr), (off), (size), (data), (post)))struct VFIOContIO { int (*dma_map)(VFIOContainer *container,@@ -184,6 +211,7 @@ struct VFIOContIO {#define CONT_DIRTY_BITMAP (cont, bitmap, range) \ ((cont)->io_ops->dirty_bitmap((cont), (bitmap), (range))) +extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl;#endif /* CONFIG_LINUX */ hw/vfio/common.c +29
-13@@ -70,7 +70,7 @@ void vfio_disable_irqindex(VFIODevice *vbasedev, int index) .count = 0, }; -ioctl (vbasedev->fd,VFIO_DEVICE_SET_IRQS , &irq_set); +VDEV_SET_IRQS (vbasedev, &irq_set); }void vfio_unmask_single_irqindex (VFIODevice *vbasedev,int index)@@ -83,7 +83,7 @@ void vfio_unmask_single_irqindex(VFIODevice *vbasedev, int index) .count = 1, }; -ioctl (vbasedev->fd,VFIO_DEVICE_SET_IRQS , &irq_set); +VDEV_SET_IRQS (vbasedev, &irq_set); }void vfio_mask_single_irqindex (VFIODevice *vbasedev,int index)@@ -96,7 +96,7 @@ void vfio_mask_single_irqindex(VFIODevice *vbasedev, int index) .count = 1, }; -ioctl (vbasedev->fd,VFIO_DEVICE_SET_IRQS , &irq_set); +VDEV_SET_IRQS (vbasedev, &irq_set); }static inline const char *action_to_str (int action)@@ -177,9 +177,7 @@ int vfio_set_irq_signaling(VFIODevice *vbasedev, int index, int subindex, pfd = (int32_t *)&irq_set->data; *pfd = fd; -if (ioctl (vbasedev->fd,VFIO_DEVICE_SET_IRQS , irq_set)) { - ret = -errno; - } + ret =VDEV_SET_IRQS (vbasedev, irq_set);g_free (irq_set);if (!ret) {@@ -214,6 +212,7 @@ void vfio_region_write(void *opaque, hwaddr addr,uint32_t dword;uint64_t qword; } buf; +int ret;switch (size) {case 1 :@@ -233,13 +232,15 @@ void vfio_region_write(void *opaque, hwaddr addr,break ; } -if (pwrite (vbasedev->fd, &buf, size, region->fd_offset + addr) != size) { + ret =VDEV_REGION_WRITE (vbasedev, region->nr, addr, size, &buf, false); +if (ret != size) { +const char *err = ret <0 ?strerror (-ret) :"short write" ; +error_report ("%s(%s:region%d+0x%" HWADDR_PRIx", 0x%" PRIx64 -",%d) failed: %m" , +",%d) failed: %s" , __func__, vbasedev->name, region->nr, - addr, data, size); + addr, data, size, err); } -trace_vfio_region_write (vbasedev->name, region->nr, addr, data, size);/* @@ -265,13 +266,18 @@ uint64_t vfio_region_read(void *opaque,uint64_t qword; } buf;uint64_t data =0 ; +int ret; -if (pread (vbasedev->fd, &buf, size, region->fd_offset + addr) != size) { -error_report ("%s(%s:region%d+0x%" HWADDR_PRIx", %d) failed: %m" , + ret =VDEV_REGION_READ (vbasedev, region->nr, addr, size, &buf); +if (ret != size) { +const char *err = ret <0 ?strerror (-ret) :"short read" ; + +error_report ("%s(%s:region%d+0x%" HWADDR_PRIx", %d) failed: %s" , __func__, vbasedev->name, region->nr, - addr, size); + addr, size, err);return (uint64_t )-1 ; } +switch (size) {case 1 : data = buf.byte;@@ -2369,6 +2375,16 @@ int vfio_get_device(VFIOGroup *group, const char *name,void vfio_put_base_device (VFIODevice *vbasedev) { +if (vbasedev->regions !=NULL ) { +int i; + +for (i =0 ; i < vbasedev->num_regions; i++) { +g_free (vbasedev->regions[i]); + } +g_free (vbasedev->regions); + vbasedev->regions =NULL ; + } +if (!vbasedev->group) {return ; }@@ -2382,6 +2398,21 @@ int vfio_get_region_info(VFIODevice *vbasedev, int index, struct vfio_region_info **info)
hw/vfio/common.c int vfio_get_region_info(VFIODevice *vbasedev, int index, · line 2408
Quoted 10 lines
hw/vfio/common.c +8
-0{size_t argsz =sizeof (struct vfio_region_info ); +int fd =-1 ; +int ret; + +/* create region cache */ +if (vbasedev->regions ==NULL ) { + vbasedev->regions = g_new0(struct vfio_region_info *, + vbasedev->num_regions); + }
Seems like this should have been part of vfio_get_device() since that's where we set num_regions.
hw/vfio/common.c int vfio_get_region_info(VFIODevice *vbasedev, int index, · line 2415
Quoted 7 lines
hw/vfio/common.c +7
-0+/* check cache */ +if (vbasedev->regions[index] !=NULL ) { + *info =g_malloc0 (vbasedev->regions[index]->argsz); +memcpy (*info, vbasedev->regions[index], + vbasedev->regions[index]->argsz); +return 0 ; + }
Why are we calling vfio_get_region_info() enough that we need a cache? This looks like an optimization for something that doesn't exist yet. And if we have a cache, why aren't callers using it directly rather than creating a copy for each caller?
Quoted 13 lines
hw/vfio/common.c +2
-2*info =g_malloc0 (argsz);@@ -2389,19 +2420,28 @@ int vfio_get_region_info(VFIODevice *vbasedev, int index, retry: (*info)->argsz = argsz; -if (ioctl (vbasedev->fd,VFIO_DEVICE_GET_REGION_INFO , *info)) { + ret =VDEV_GET_REGION_INFO (vbasedev, *info, &fd); +if (ret !=0 ) {g_free (*info); *info =NULL ; -return -errno;
hw/vfio/common.c int vfio_get_region_info(VFIODevice *vbasedev, int index, · line 2436
Quoted 10 lines
hw/vfio/common.c +5
-0+return ret; }if ((*info)->argsz > argsz) { argsz = (*info)->argsz; *info =g_realloc (*info, argsz); +if (fd !=-1 ) { +close (fd); + fd =-1 ; + }
Use of fd here is hard to review, it's clearly for some future use case.
Quoted 84 lines
hw/vfio/common.c +73
-0goto retry; } +/* fill cache */ + vbasedev->regions[index] =g_malloc0 (argsz); +memcpy (vbasedev->regions[index], *info, argsz); +return 0 ; }@@ -2554,6 +2594,95 @@ int vfio_eeh_as_op(AddressSpace *as, uint32_t op)* Traditional ioctl() based io_ops */ +static int vfio_io_get_info (VFIODevice *vbasedev,struct vfio_device_info *info) +{ +int ret; + + ret =ioctl (vbasedev->fd,VFIO_DEVICE_GET_INFO , info); +if (ret <0 ) { + ret = -errno; + } + +return ret; +} + +static int vfio_io_get_region_info (VFIODevice *vbasedev, +struct vfio_region_info *info, +int *fd) +{ +int ret; + + *fd =-1 ; + ret =ioctl (vbasedev->fd,VFIO_DEVICE_GET_REGION_INFO , info); +if (ret <0 ) { + ret = -errno; + } + +return ret; +} + +static int vfio_io_get_irq_info (VFIODevice *vbasedev, +struct vfio_irq_info *info) +{ +int ret; + + ret =ioctl (vbasedev->fd,VFIO_DEVICE_GET_IRQ_INFO , info); +if (ret <0 ) { + ret = -errno; + } + +return ret; +} + +static int vfio_io_set_irqs (VFIODevice *vbasedev,struct vfio_irq_set *irqs) +{ +int ret; + + ret =ioctl (vbasedev->fd,VFIO_DEVICE_SET_IRQS , irqs); +if (ret <0 ) { + ret = -errno; + } + +return ret; +} + +static int vfio_io_region_read (VFIODevice *vbasedev,uint8_t index,off_t off, +uint32_t size,void *data) +{ +struct vfio_region_info *info = vbasedev->regions[index]; +int ret; + + ret =pread (vbasedev->fd, data, size, info->offset + off); +if (ret <0 ) { + ret = -errno; + } + +return ret; +} + +static int vfio_io_region_write (VFIODevice *vbasedev,uint8_t index,off_t off, +uint32_t size,void *data,bool post) +{
hw/vfio/common.c int vfio_eeh_as_op(AddressSpace *as, uint32_t op) · line 2675
Quoted 10 lines
hw/vfio/common.c +10
-0+struct vfio_region_info *info = vbasedev->regions[index]; +int ret; + + ret =pwrite (vbasedev->fd, data, size, info->offset + off); +if (ret <0 ) { + ret = -errno; + } + +return ret; +}
Simplify all these with ternaries?
return ret < 0 ? -errno : ret;
Quoted 99 lines
hw/vfio/common.c +10
-0+ +VFIODevIO vfio_dev_io_ioctl = { + .get_info = vfio_io_get_info, + .get_region_info = vfio_io_get_region_info, + .get_irq_info = vfio_io_get_irq_info, + .set_irqs = vfio_io_set_irqs, + .region_read = vfio_io_region_read, + .region_write = vfio_io_region_write, +}; +static int vfio_io_dma_map (VFIOContainer *container,struct vfio_iommu_type1_dma_map *map,int fd,bool will_commit)hw/vfio/pci.c +19
-15@@ -402,7 +402,7 @@ static int vfio_enable_vectors(VFIOPCIDevice *vdev, bool msix) fds[i] = fd; } - ret =ioctl (vdev->vbasedev.fd,VFIO_DEVICE_SET_IRQS , irq_set); + ret =VDEV_SET_IRQS (&vdev->vbasedev, irq_set);g_free (irq_set);@@ -772,14 +772,16 @@ static void vfio_update_msi(VFIOPCIDevice *vdev)static void vfio_pci_load_rom (VFIOPCIDevice *vdev) { +VFIODevice *vbasedev = &vdev->vbasedev;struct vfio_region_info *reg_info;uint64_t size;off_t off =0 ;ssize_t bytes; +int ret; -if (vfio_get_region_info (&vdev->vbasedev, -VFIO_PCI_ROM_REGION_INDEX , ®_info)) { -error_report ("vfio: Error getting ROM info: %m" ); + ret =vfio_get_region_info (vbasedev,VFIO_PCI_ROM_REGION_INDEX , ®_info); +if (ret <0 ) { +error_report ("vfio: Error getting ROM info: %s" ,strerror (-ret));return ; }@@ -806,18 +808,19 @@ static void vfio_pci_load_rom(VFIOPCIDevice *vdev)memset (vdev->rom,0xff , size);while (size) { - bytes =pread (vdev->vbasedev.fd, vdev->rom + off, - size, vdev->rom_offset + off); + bytes =VDEV_REGION_READ (vbasedev,VFIO_PCI_ROM_REGION_INDEX , off, + size, vdev->rom + off);if (bytes ==0 ) {break ; }else if (bytes >0 ) { off += bytes; size -= bytes; }else { -if (errno ==EINTR || errno ==EAGAIN ) { +if (bytes == -EINTR || bytes == -EAGAIN ) {continue ; } -error_report ("vfio: Error reading device ROM: %m" ); +error_report ("vfio: Error reading device ROM: %s" , +strerror (-bytes));break ; } }@@ -905,11 +908,10 @@ static const MemoryRegionOps vfio_rom_ops = {static void vfio_pci_size_rom (VFIOPCIDevice *vdev) { +VFIODevice *vbasedev = &vdev->vbasedev;uint32_t orig, size =cpu_to_le32 ((uint32_t )PCI_ROM_ADDRESS_MASK ); -off_t offset = vdev->config_offset +PCI_ROM_ADDRESS ;DeviceState *dev =DEVICE (vdev);char *name; -int fd = vdev->vbasedev.fd;if (vdev->pdev.romfile || !vdev->pdev.rom_bar) {/* Since pci handles romfile, just print a message and return */ @@ -926,13 +928,23 @@ static void vfio_pci_size_rom(VFIOPCIDevice *vdev)* Use the same size ROM BAR as the physical device. The contents * will get filled in later when the guest tries to read it. */ -if (pread (fd, &orig,4 , offset) !=4 || -pwrite (fd, &size,4 , offset) !=4 || -pread (fd, &size,4 , offset) !=4 || -pwrite (fd, &orig,4 , offset) !=4 ) { -error_report ("%s(%s) failed: %m" , __func__, vdev->vbasedev.name); +#define rom_read (var) VDEV_REGION_READ(vbasedev, \ + VFIO_PCI_CONFIG_REGION_INDEX, \ + PCI_ROM_ADDRESS, 4, (var)) +#define rom_write (var) VDEV_REGION_WRITE(vbasedev, \ + VFIO_PCI_CONFIG_REGION_INDEX, \ + PCI_ROM_ADDRESS, 4, (var), false) +
hw/vfio/pci.c static void vfio_pci_size_rom(VFIOPCIDevice *vdev) · line 947
Quoted 10 lines
hw/vfio/pci.c +8
-0+if (rom_read (&orig) !=4 || +rom_write (&size) !=4 || +rom_read (&size) !=4 || +rom_write (&orig) !=4 ) { + +error_report ("%s(%s) ROM access failed" , __func__, vbasedev->name);return ; } +#undef rom_read +#undef rom_write
Hmm, I think I'd rather see a generic function broken out for this, maybe:
int vfio_pci_size_bar32(VFIODevice *vbasedev, int offset,
uint32_t mask, uint32_t *size)
{
uint32_t orig, val;
int index = VFIO_PCI_CONFIG_REGION_INDEX;
if (VDEV_REGION_READ(vbasedev, index, offset, 4, &orig) != 4 ||
VDEV_REGION_WRITE(vbasedev, index, offset, 4, &mask, false) != 4 ||
VDEV_REGION_READ(vbasedev, index, offset, 4, &val) != 4 ||
VDEV_REGION_WRITE(vbasedev, index, offset, 4, &orig, false) != 4) {
error_report(...
return -1;
}
*size = ~(le32_to_cpu(val) & PCI_ROM_ADDRESS_MASK) + 1;
return 0;
}Really I think it's just trying to hard code VFIO_PCI_CONFIG_REGION_INDEX and PCI_ROM_ADDRESS into the macro calls that demand some simplification, if we use variables it looks fine inline.
We could also re-wrap VDEV_REGION_READ/WRITE in vfio/pci.h to simplify config space access, presumably we'd never have posted writes to config space. Thanks,
Alex
Quoted 216 lines
hw/vfio/pci.c +66
-38size = ~(le32_to_cpu (size) &PCI_ROM_ADDRESS_MASK ) +1 ;@@ -1110,6 +1122,7 @@ static void vfio_sub_page_bar_update_mapping(PCIDevice *pdev, int bar)uint32_t vfio_pci_read_config (PCIDevice *pdev,uint32_t addr,int len) {VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev); +VFIODevice *vbasedev = &vdev->vbasedev;uint32_t emu_bits =0 , emu_val =0 , phys_val =0 , val;memcpy (&emu_bits, vdev->emulated_config_bits + addr, len);@@ -1122,12 +1135,14 @@ uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len)if (~emu_bits & (0xffffffffU >> (32 - len *8 ))) {ssize_t ret; - ret =pread (vdev->vbasedev.fd, &phys_val, len, - vdev->config_offset + addr); + ret =VDEV_REGION_READ (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , addr, + len, &phys_val);if (ret != len) { -error_report ("%s(%s, 0x%x, 0x%x) failed: %m" , - __func__, vdev->vbasedev.name, addr, len); -return -errno; +const char *err = ret <0 ?strerror (-ret) :"short read" ; + +error_report ("%s(%s, 0x%x, 0x%x) failed: %s" , + __func__, vbasedev->name, addr, len, err); +return -1 ; } phys_val =le32_to_cpu (phys_val); }@@ -1143,15 +1158,20 @@ void vfio_pci_write_config(PCIDevice *pdev, uint32_t addr, uint32_t val, int len) { VFIOPCIDevice *vdev = VFIO_PCI_BASE(pdev); +VFIODevice *vbasedev = &vdev->vbasedev;uint32_t val_le =cpu_to_le32 (val); +int ret;trace_vfio_pci_write_config (vdev->vbasedev.name, addr, val, len);/* Write everything to VFIO, let it filter out what we can't write */ -if (pwrite (vdev->vbasedev.fd, &val_le, len, vdev->config_offset + addr) - != len) { -error_report ("%s(%s, 0x%x, 0x%x, 0x%x) failed: %m" , - __func__, vdev->vbasedev.name, addr, val, len); + ret =VDEV_REGION_WRITE (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , addr, + len, &val_le, false); +if (ret != len) { +const char *err = ret <0 ?strerror (-ret) :"short write" ; + +error_report ("%s(%s, 0x%x, 0x%x, 0x%x) failed: %s" , + __func__, vbasedev->name, addr, val, len, err); }/* MSI/MSI-X Enabling/Disabling */ @@ -1239,10 +1259,13 @@ static int vfio_msi_setup(VFIOPCIDevice *vdev, int pos, Error **errp)int ret, entries;Error *err =NULL ; -if (pread (vdev->vbasedev.fd, &ctrl,sizeof (ctrl), - vdev->config_offset + pos +PCI_CAP_FLAGS ) !=sizeof (ctrl)) { -error_setg_errno (errp, errno,"failed reading MSI PCI_CAP_FLAGS" ); -return -errno; + ret =VDEV_REGION_READ (&vdev->vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , + pos +PCI_CAP_FLAGS ,sizeof (ctrl), &ctrl); +if (ret !=sizeof (ctrl)) { +const char *err = ret <0 ?strerror (-ret) :"short read" ; + +error_setg (errp,"failed reading MSI PCI_CAP_FLAGS %s" , err); +return ret; } ctrl =le16_to_cpu (ctrl);@@ -1444,33 +1467,40 @@ static void vfio_pci_relocate_msix(VFIOPCIDevice *vdev, Error **errp)*/ static void vfio_msix_early_setup (VFIOPCIDevice *vdev,Error **errp) { +VFIODevice *vbasedev = &vdev->vbasedev;uint8_t pos;uint16_t ctrl;uint32_t table, pba; -int fd = vdev->vbasedev.fd;VFIOMSIXInfo *msix; +int ret; pos =pci_find_capability (&vdev->pdev,PCI_CAP_ID_MSIX );if (!pos) {return ; } -if (pread (fd, &ctrl,sizeof (ctrl), - vdev->config_offset + pos +PCI_MSIX_FLAGS ) !=sizeof (ctrl)) { -error_setg_errno (errp, errno,"failed to read PCI MSIX FLAGS" ); -return ; + ret =VDEV_REGION_READ (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , + pos +PCI_MSIX_FLAGS ,sizeof (ctrl), &ctrl); +if (ret !=sizeof (ctrl)) { +const char *err = ret <0 ?strerror (-ret) :"short read" ; + +error_setg (errp,"failed to read PCI MSIX FLAGS %s" , err); } -if (pread (fd, &table,sizeof (table), - vdev->config_offset + pos +PCI_MSIX_TABLE ) !=sizeof (table)) { -error_setg_errno (errp, errno,"failed to read PCI MSIX TABLE" ); -return ; + ret =VDEV_REGION_READ (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , + pos +PCI_MSIX_TABLE ,sizeof (table), &table); +if (ret !=sizeof (table)) { +const char *err = ret <0 ?strerror (-ret) :"short read" ; + +error_setg (errp,"failed to read PCI MSIX TABLE %s" , err); } -if (pread (fd, &pba,sizeof (pba), - vdev->config_offset + pos +PCI_MSIX_PBA ) !=sizeof (pba)) { -error_setg_errno (errp, errno,"failed to read PCI MSIX PBA" ); -return ; + ret =VDEV_REGION_READ (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , + pos +PCI_MSIX_PBA ,sizeof (pba), &pba); +if (ret !=sizeof (pba)) { +const char *err = ret <0 ?strerror (-ret) :"short read" ; + +error_setg (errp,"failed to read PCI MSIX PBA %s" , err); } ctrl =le16_to_cpu (ctrl);@@ -1608,7 +1638,6 @@ static void vfio_mmap_set_enabled(VFIOPCIDevice *vdev, bool enabled)static void vfio_bar_prepare (VFIOPCIDevice *vdev,int nr) {VFIOBAR *bar = &vdev->bars[nr]; -uint32_t pci_bar;int ret;@@ -1618,10 +1647,13 @@ static void vfio_bar_prepare(VFIOPCIDevice *vdev, int nr) }/* Determine what type of BAR this is for registration */ - ret =pread (vdev->vbasedev.fd, &pci_bar,sizeof (pci_bar), - vdev->config_offset +PCI_BASE_ADDRESS_0 + (4 * nr)); + ret =VDEV_REGION_READ (&vdev->vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , +PCI_BASE_ADDRESS_0 + (4 * nr), +sizeof (pci_bar), &pci_bar);if (ret !=sizeof (pci_bar)) { -error_report ("vfio: Failed to read BAR %d (%m)" , nr); +const char *err = ret <0 ?strerror (-ret) :"short read" ; + +error_report ("vfio: Failed to read BAR %d (%s)" , nr, err);return ; }@@ -2169,8 +2201,9 @@ static void vfio_pci_pre_reset(VFIOPCIDevice *vdev)static void vfio_pci_post_reset (VFIOPCIDevice *vdev) { +VFIODevice *vbasedev = &vdev->vbasedev;Error *err =NULL ; -int nr; +int ret, nr;vfio_intx_enable (vdev, &err);if (err) {@@ -2178,13 +2211,17 @@ static void vfio_pci_post_reset(VFIOPCIDevice *vdev) }for (nr =0 ; nr <PCI_NUM_REGIONS -1 ; ++nr) { -off_t addr = vdev->config_offset +PCI_BASE_ADDRESS_0 + (4 * nr); +off_t addr =PCI_BASE_ADDRESS_0 + (4 * nr);uint32_t val =0 ;uint32_t len =sizeof (val); -if (pwrite (vdev->vbasedev.fd, &val, len, addr) != len) { -error_report ("%s(%s) reset bar %d failed: %m" , __func__, - vdev->vbasedev.name, nr); + ret =VDEV_REGION_WRITE (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX , addr, + len, &val, false); +if (ret != len) { +const char *err = ret <0 ?strerror (-ret) :"short write" ; + +error_report ("%s(%s) reset bar %d failed: %s" , __func__, + vbasedev->name, nr, err); } }@@ -2619,7 +2656,7 @@ static void vfio_populate_device(VFIOPCIDevice *vdev, Error **errp) irq_info.index =VFIO_PCI_ERR_IRQ_INDEX ; - ret =ioctl (vdev->vbasedev.fd,VFIO_DEVICE_GET_IRQ_INFO , &irq_info); + ret =VDEV_GET_IRQ_INFO (vbasedev, &irq_info);if (ret) {/* This can fail for an old kernel or legacy PCI dev */ trace_vfio_populate_device_get_irq_info_failure (strerror (errno));@@ -2738,8 +2775,10 @@ static void vfio_register_req_notifier(VFIOPCIDevice *vdev)return ; } -if (ioctl (vdev->vbasedev.fd, -VFIO_DEVICE_GET_IRQ_INFO , &irq_info) <0 || irq_info.count <1 ) { +if (VDEV_GET_IRQ_INFO (&vdev->vbasedev, &irq_info) <0 ) { +return ; + } +if (irq_info.count <1 ) {return ; }@@ -2817,6 +2856,7 @@ static void vfio_realize(PCIDevice *pdev, Error **errp) vdev->vbasedev.ops = &vfio_pci_ops; vdev->vbasedev.type =VFIO_DEVICE_TYPE_PCI ; vdev->vbasedev.dev =DEVICE (vdev); + vdev->vbasedev.io_ops = &vfio_dev_io_ioctl; tmp =g_strdup_printf ("%s/iommu_group" , vdev->vbasedev.sysfsdev); len =readlink (tmp, group_path,sizeof (group_path));
Re: [RFC v3 19/19] vfio-user: migration support
In reply to John JohnsonQuoted 1 line · On Mon, 8 Nov 2021 16:46:47 -0800 John Johnson <[email protected]>…
bug fix: only set qemu file error if there is a file
I don't understand this commit log. Is this meant to be a revision log? In general it would be nice to have more detailed commit logs for many of these patches describing any nuances.
Note that the migration uAPI is being reevaluated on the kernel side, so I expect we'll want to hold off on vfio-user beyond minimal stub support. Thanks,
Alex
Quoted 259 lines
Signed-off-by John G Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/user-protocol.h | 18 +++++++++++++++++ hw/vfio/migration.c | 34 +++++++++++++++---------------- hw/vfio/pci.c | 7 +++++++ hw/vfio/user.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 17 deletions(-)hw/vfio/user-protocol.h +18
-0@@ -182,6 +182,10 @@ typedef struct {char data[]; } VFIOUserDMARW; +/* +* VFIO_USER_DIRTY_PAGES +*/ +/*imported from struct vfio_bitmap */ typedef struct { uint64_t pgsize;@@ -189,4 +193,18 @@ typedef struct { char data[]; } VFIOUserBitmap; +/* imported from struct vfio_iommu_type1_dirty_bitmap_get */ +typedef struct { +uint64_t iova; +uint64_t size; +VFIOUserBitmap bitmap; +}VFIOUserBitmapRange ; + +/* imported from struct vfio_iommu_type1_dirty_bitmap */ +typedef struct { +VFIOUserHdr hdr; +uint32_t argsz; +uint32_t flags; +}VFIOUserDirtyPages ; +#endif /* VFIO_USER_PROTOCOL_H */ hw/vfio/migration.c +17
-17@@ -27,6 +27,7 @@#include "pci.h" #include "trace.h" #include "hw/hw.h" +#include "user.h" /* * Flags to be used as unique delimiters for VFIO devices in the migration @@ -49,11 +50,13 @@ static int64_t bytes_transferred;static inline int vfio_mig_access (VFIODevice *vbasedev,void *val,int count,off_t off,bool iswrite) { +VFIORegion *region = &vbasedev->migration->region;int ret; - ret = iswrite ?pwrite (vbasedev->fd, val, count, off) : -pread (vbasedev->fd, val, count, off); -if (ret < count) { + ret = iswrite ? +VDEV_REGION_WRITE (vbasedev, region->nr, off, count, val, false) : +VDEV_REGION_READ (vbasedev, region->nr, off, count, val); +if (ret < count) {error_report ("vfio_mig_%s %d byte %s: failed at offset 0x%" HWADDR_PRIx", err: %s" , iswrite ?"write" :"read" , count, vbasedev->name, off,strerror (errno));@@ -111,9 +114,7 @@ static int vfio_migration_set_state(VFIODevice *vbasedev, uint32_t mask, uint32_t value) {VFIOMigration *migration = vbasedev->migration; -VFIORegion *region = &migration->region; -off_t dev_state_off = region->fd_offset + -VFIO_MIG_STRUCT_OFFSET (device_state); +off_t dev_state_off =VFIO_MIG_STRUCT_OFFSET (device_state);uint32_t device_state;int ret;@@ -201,13 +202,13 @@ static int vfio_save_buffer(QEMUFile *f, VFIODevice *vbasedev, uint64_t *size)int ret; ret =vfio_mig_read (vbasedev, &data_offset,sizeof (data_offset), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (data_offset)); +VFIO_MIG_STRUCT_OFFSET (data_offset));if (ret <0 ) {return ret; } ret =vfio_mig_read (vbasedev, &data_size,sizeof (data_size), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (data_size)); +VFIO_MIG_STRUCT_OFFSET (data_size));if (ret <0 ) {return ret; }@@ -233,8 +234,7 @@ static int vfio_save_buffer(QEMUFile *f, VFIODevice *vbasedev, uint64_t *size) } buf_allocated = true; - ret =vfio_mig_read (vbasedev, buf, sec_size, - region->fd_offset + data_offset); + ret =vfio_mig_read (vbasedev, buf, sec_size, data_offset);if (ret <0 ) {g_free (buf);return ret;@@ -269,7 +269,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev, do { ret = vfio_mig_read(vbasedev, &data_offset, sizeof(data_offset), - region->fd_offset + VFIO_MIG_STRUCT_OFFSET(data_offset)); + VFIO_MIG_STRUCT_OFFSET(data_offset)); if (ret < 0) { return ret; }@@ -309,8 +309,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,qemu_get_buffer (f, buf, sec_size);if (buf_alloc) { - ret =vfio_mig_write (vbasedev, buf, sec_size, - region->fd_offset + data_offset); + ret =vfio_mig_write (vbasedev, buf, sec_size, data_offset);g_free (buf);if (ret <0 ) {@@ -322,7 +321,7 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev, } ret =vfio_mig_write (vbasedev, &report_size,sizeof (report_size), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (data_size)); +VFIO_MIG_STRUCT_OFFSET (data_size));if (ret <0 ) {return ret; }@@ -334,12 +333,11 @@ static int vfio_load_buffer(QEMUFile *f, VFIODevice *vbasedev,static int vfio_update_pending (VFIODevice *vbasedev) {VFIOMigration *migration = vbasedev->migration; -VFIORegion *region = &migration->region;uint64_t pending_bytes =0 ;int ret; ret =vfio_mig_read (vbasedev, &pending_bytes,sizeof (pending_bytes), - region->fd_offset +VFIO_MIG_STRUCT_OFFSET (pending_bytes)); +VFIO_MIG_STRUCT_OFFSET (pending_bytes));if (ret <0 ) { migration->pending_bytes =0 ;return ret;@@ -744,7 +742,9 @@ static void vfio_vmstate_change(void *opaque, bool running, RunState state)*/ error_report ("%s: Failed to set device state 0x%x" , vbasedev->name, (migration->device_state & mask) | value); -qemu_file_set_error (migrate_get_current ()->to_dst_file, ret); +if (value !=0 ) { +qemu_file_set_error (migrate_get_current ()->to_dst_file, ret); + } } vbasedev->migration->vm_running = running; trace_vfio_vmstate_change(vbasedev->name, running, RunState_str(state),hw/vfio/pci.c +7
-0@@ -3754,6 +3754,13 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) } } +if (!pdev->failover_pair_id) { + ret =vfio_migration_probe (&vdev->vbasedev, errp); +if (ret) { +error_report ("%s: Migration disabled" , vdev->vbasedev.name); + } + } +vfio_register_err_notifier (vdev);vfio_register_req_notifier (vdev);hw/vfio/user.c +54
-0@@ -1398,6 +1398,52 @@ void vfio_user_reset(VFIOProxy *proxy) } } +static int vfio_user_dirty_bitmap (VFIOProxy *proxy, +struct vfio_iommu_type1_dirty_bitmap *cmd, +struct vfio_iommu_type1_dirty_bitmap_get + *dbitmap) +{ +g_autofree struct { +VFIOUserDirtyPages msg; +VFIOUserBitmapRange range; + } *msgp =NULL ; +int msize, rsize; + +/* +* If just the command is sent, the returned bitmap isn't needed. +* The bitmap structs are different from the ioctl() versions, +* ioctl() returns the bitmap in a local VA +*/ +if (dbitmap !=NULL ) { + msize =sizeof (*msgp); + rsize = msize + dbitmap->bitmap.size; + msgp =g_malloc0 (rsize); + msgp->range.iova = dbitmap->iova; + msgp->range.size = dbitmap->size; + msgp->range.bitmap.pgsize = dbitmap->bitmap.pgsize; + msgp->range.bitmap.size = dbitmap->bitmap.size; + }else { + msize = rsize =sizeof (VFIOUserDirtyPages); + msgp =g_malloc0 (rsize); + } + +vfio_user_request_msg (&msgp->msg.hdr,VFIO_USER_DIRTY_PAGES , msize,0 ); + msgp->msg.argsz = msize -sizeof (msgp->msg.hdr); + msgp->msg.flags = cmd->flags; + +vfio_user_send_wait (proxy, &msgp->msg.hdr,NULL , rsize, false); +if (msgp->msg.hdr.flags &VFIO_USER_ERROR ) { +return -msgp->msg.hdr.error_reply; + } + +if (dbitmap !=NULL ) { +memcpy (dbitmap->bitmap.data, &msgp->range.bitmap.data, + dbitmap->bitmap.size); + } + +return 0 ; +} +/* * Socket-based io_ops @@ -1493,6 +1539,13 @@ static int vfio_user_io_dma_unmap(VFIOContainer *container,return vfio_user_dma_unmap (container->proxy, unmap, bitmap, will_commit); } +static int vfio_user_io_dirty_bitmap (VFIOContainer *container, +struct vfio_iommu_type1_dirty_bitmap *bitmap, +struct vfio_iommu_type1_dirty_bitmap_get *range) +{ +return vfio_user_dirty_bitmap (container->proxy, bitmap, range); +} +static void vfio_user_io_wait_commit (VFIOContainer *container) {vfio_user_wait_reqs (container->proxy);@@ -1501,5 +1554,6 @@ static void vfio_user_io_wait_commit(VFIOContainer *container)VFIOContIO vfio_cont_io_sock = { .dma_map = vfio_user_io_dma_map, .dma_unmap = vfio_user_io_dma_unmap, + .dirty_bitmap = vfio_user_io_dirty_bitmap, .wait_commit = vfio_user_io_wait_commit, };
Re: [RFC v3 07/19] vfio-user: connect vfio proxy to remote server
In reply to John JohnsonQuoted 158 lines · On Mon, 8 Nov 2021 16:46:35 -0800 John Johnson <[email protected]>…
Signed-off-by John G Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/user.h | 78 +++++++++++++++++++ include/hw/vfio/vfio-common.h | 2 + hw/vfio/pci.c | 20 +++++ hw/vfio/user.c | 170 ++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 4 + hw/vfio/meson.build | 1 + 6 files changed, 275 insertions(+) create mode 100644 hw/vfio/user.h create mode 100644 hw/vfio/user.chw/vfio/user.h new file +78
-0@@ -0,0 +1,78 @@ +#ifndef VFIO_USER_H +#define VFIO_USER_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. +* +*/ + +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 VFIOProxy { +QLIST_ENTRY (VFIOProxy) 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; +}VFIOProxy ; + +/* VFIOProxy flags */ +#define VFIO_PROXY_CLIENT 0x1 + +VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp); +void vfio_user_disconnect (VFIOProxy *proxy); + +#endif /* VFIO_USER_H */ include/hw/vfio/vfio-common.h +2
-0@@ -76,6 +76,7 @@ typedef struct VFIOAddressSpace {struct VFIOGroup ;typedef struct VFIOContIO VFIOContIO ; +typedef struct VFIOProxy VFIOProxy ; typedef struct VFIOContainer { VFIOAddressSpace *space;@@ -150,6 +151,7 @@ typedef struct VFIODevice {Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking;struct vfio_region_info **regions; +VFIOProxy *proxy; } VFIODevice;struct VFIODeviceOps {hw/vfio/pci.c +14
-0@@ -43,6 +43,7 @@#include "qapi/error.h" #include "migration/blocker.h" #include "migration/qemu-file.h" +#include "hw/vfio/user.h" #define TYPE_VFIO_PCI_NOHOTPLUG "vfio-pci-nohotplug"@@ -3476,6 +3477,9 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)VFIOUserPCIDevice *udev =VFIO_USER_PCI (pdev);VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIODevice *vbasedev = &vdev->vbasedev; +SocketAddress addr; +VFIOProxy *proxy; +Error *err =NULL ;/* * TODO: make option parser understand SocketAddress @@ -3488,6 +3492,16 @@ 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, &err); +if (!proxy) { +error_setg (errp,"Remote proxy not found" ); +return ; + } + vbasedev->proxy = proxy; + vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); vbasedev->dev =DEVICE (vdev); vbasedev->fd =-1 ;
hw/vfio/pci.c static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) · line 3520
Quoted 7 lines
hw/vfio/pci.c +4
-0@@ -3500,6 +3514,12 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp)static void vfio_user_instance_finalize (Object *obj) { +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); +VFIODevice *vbasedev = &vdev->vbasedev; + +vfio_put_device (vdev);
This looks suspiciously like the initial function in the previous patch should not have been empty. Thanks,
Alex
Quoted 210 lines
hw/vfio/pci.c +2
-0+ +vfio_user_disconnect (vbasedev->proxy); }static Property vfio_user_pci_dev_properties[] = {hw/vfio/user.c new file +170
-0@@ -0,0 +1,170 @@ +/* +* 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 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#include "qemu/osdep.h" +#include <linux/vfio.h> +#include <sys/ioctl.h> + +#include "qemu/error-report.h" +#include "qapi/error.h" +#include "qemu/main-loop.h" +#include "hw/hw.h" +#include "hw/vfio/vfio-common.h" +#include "hw/vfio/vfio.h" +#include "qemu/sockets.h" +#include "io/channel.h" +#include "io/channel-socket.h" +#include "io/channel-util.h" +#include "sysemu/iothread.h" +#include "user.h" + +static IOThread *vfio_user_iothread; +static void vfio_user_shutdown (VFIOProxy *proxy); + + +/* +* Functions called by main, CPU, or iothread threads +*/ + +static void vfio_user_shutdown (VFIOProxy *proxy) +{ +qio_channel_shutdown (proxy->ioc,QIO_CHANNEL_SHUTDOWN_READ ,NULL ); +qio_channel_set_aio_fd_handler (proxy->ioc, proxy->ctx,NULL ,NULL ,NULL ); +} + + +/* +* Functions only called by iothread +*/ + +static void vfio_user_cb (void *opaque) +{ +VFIOProxy *proxy = opaque; + +QEMU_LOCK_GUARD (&proxy->lock); + + proxy->state =VFIO_PROXY_CLOSED ; +qemu_cond_signal (&proxy->close_cv); +} + + +/* +* Functions called by main or CPU threads +*/ + +static QLIST_HEAD(, VFIOProxy) vfio_user_sockets = +QLIST_HEAD_INITIALIZER (vfio_user_sockets); + +VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp) +{ +VFIOProxy *proxy; +QIOChannelSocket *sioc; +QIOChannel *ioc; +char *sockname; + +if (addr->type !=SOCKET_ADDRESS_TYPE_UNIX ) { +error_setg (errp,"vfio_user_connect - bad address family" ); +return NULL ; + } + sockname = addr->u.q_unix.path; + + sioc =qio_channel_socket_new (); + ioc =QIO_CHANNEL (sioc); +if (qio_channel_socket_connect_sync (sioc, addr, errp)) { +object_unref (OBJECT (ioc)); +return NULL ; + } +qio_channel_set_blocking (ioc, false,NULL ); + + proxy =g_malloc0 (sizeof (VFIOProxy)); + proxy->sockname =g_strdup_printf ("unix:%s" , sockname); + proxy->ioc = ioc; + proxy->flags =VFIO_PROXY_CLIENT ; + proxy->state =VFIO_PROXY_CONNECTED ; + +qemu_mutex_init (&proxy->lock); +qemu_cond_init (&proxy->close_cv); + +if (vfio_user_iothread ==NULL ) { + vfio_user_iothread =iothread_create ("VFIO user" , errp); + } + + proxy->ctx =iothread_get_aio_context (vfio_user_iothread); + +QTAILQ_INIT (&proxy->outgoing); +QTAILQ_INIT (&proxy->incoming); +QTAILQ_INIT (&proxy->free); +QTAILQ_INIT (&proxy->pending); +QLIST_INSERT_HEAD (&vfio_user_sockets, proxy, next); + +return proxy; +} + +void vfio_user_disconnect (VFIOProxy *proxy) +{ +VFIOUserMsg *r1, *r2; + +qemu_mutex_lock (&proxy->lock); + +/* our side is quitting */ +if (proxy->state ==VFIO_PROXY_CONNECTED ) { +vfio_user_shutdown (proxy); +if (!QTAILQ_EMPTY (&proxy->pending)) { +error_printf ("vfio_user_disconnect: outstanding requests\n" ); + } + } +object_unref (OBJECT (proxy->ioc)); + proxy->ioc =NULL ; + + proxy->state =VFIO_PROXY_CLOSING ; + QTAILQ_FOREACH_SAFE(r1, &proxy->outgoing, next, r2) { +qemu_cond_destroy (&r1->cv); +QTAILQ_REMOVE (&proxy->pending, r1, next); +g_free (r1); + } + QTAILQ_FOREACH_SAFE(r1, &proxy->incoming, next, r2) { +qemu_cond_destroy (&r1->cv); +QTAILQ_REMOVE (&proxy->pending, r1, next); +g_free (r1); + } + QTAILQ_FOREACH_SAFE(r1, &proxy->pending, next, r2) { +qemu_cond_destroy (&r1->cv); +QTAILQ_REMOVE (&proxy->pending, r1, next); +g_free (r1); + } + QTAILQ_FOREACH_SAFE(r1, &proxy->free, next, r2) { +qemu_cond_destroy (&r1->cv); +QTAILQ_REMOVE (&proxy->free, r1, next); +g_free (r1); + } + +/* +* Make sure the iothread isn't blocking anywhere +* with a ref to this proxy by waiting for a BH +* handler to run after the proxy fd handlers were +* deleted above. +*/ +aio_bh_schedule_oneshot (proxy->ctx, vfio_user_cb, proxy); +qemu_cond_wait (&proxy->close_cv, &proxy->lock); + +/* we now hold the only ref to proxy */ +qemu_mutex_unlock (&proxy->lock); +qemu_cond_destroy (&proxy->close_cv); +qemu_mutex_destroy (&proxy->lock); + +QLIST_REMOVE (proxy, next); +if (QLIST_EMPTY (&vfio_user_sockets)) { +iothread_destroy (vfio_user_iothread); + vfio_user_iothread =NULL ; + } + +g_free (proxy->sockname); +g_free (proxy); +}MAINTAINERS +4
-0@@ -1882,8 +1882,12 @@ L: [email protected] vfio-user M: John G Johnson <[email protected]> M: Thanos Makatos <[email protected]> +M: Elena Ufimtseva <[email protected]> +M: Jagannathan Raman <[email protected]> S: Supported F: docs/devel/vfio-user.rst +F: hw/vfio/user.c +F: hw/vfio/user.h vhost M: Michael S. Tsirkin <[email protected]>hw/vfio/meson.build +1
-0@@ -9,6 +9,7 @@ vfio_ss.add(when: 'CONFIG_VFIO_PCI', if_true: files( 'pci-quirks.c', 'pci.c', )) +vfio_ss.add(when: 'CONFIG_VFIO_USER', if_true: files('user.c')) vfio_ss.add(when: 'CONFIG_VFIO_CCW', if_true: files('ccw.c')) vfio_ss.add(when: 'CONFIG_VFIO_PLATFORM', if_true: files('platform.c')) vfio_ss.add(when: 'CONFIG_VFIO_XGMAC', if_true: files('calxeda-xgmac.c'))
Re: [RFC v3 02/19] vfio-user: add VFIO base abstract class
In reply to John JohnsonQuoted 229 lines · On Mon, 8 Nov 2021 16:46:30 -0800 John Johnson <[email protected]>…
Add an abstract base class both the kernel driver and user socket implementations can use to share code.
Signed-off-by John G Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/pci.h | 16 +++++++-- hw/vfio/pci.c | 112 +++++++++++++++++++++++++++++++++++----------------------- 2 files changed, 81 insertions(+), 47 deletions(-)hw/vfio/pci.h +14
-2@@ -114,8 +114,13 @@ typedef struct VFIOMSIXInfo {unsigned long *pending; } VFIOMSIXInfo; -#define TYPE_VFIO_PCI "vfio-pci" -OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI) +/* +* TYPE_VFIO_PCI_BASE is an abstract type used to share code +* between VFIO implementations that use a kernel driver +* with those that use user sockets. +*/ +#define TYPE_VFIO_PCI_BASE "vfio-pci-base" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOPCIDevice, VFIO_PCI_BASE)struct VFIOPCIDevice {PCIDevice pdev;@@ -175,6 +180,13 @@ struct VFIOPCIDevice {Notifier irqchip_change_notifier; }; +#define TYPE_VFIO_PCI "vfio-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOKernPCIDevice, VFIO_PCI) + +struct VFIOKernPCIDevice { +VFIOPCIDevice device; +}; +/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ static inline bool vfio_pci_is (VFIOPCIDevice *vdev,uint32_t vendor,uint32_t device) {hw/vfio/pci.c +63
-21@@ -231,7 +231,7 @@ static void vfio_intx_update(VFIOPCIDevice *vdev, PCIINTxRoute *route)static void vfio_intx_routing_notifier (PCIDevice *pdev) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);PCIINTxRoute route;if (vdev->interrupt != VFIO_INT_INTx) {@@ -457,7 +457,7 @@ static void vfio_update_kvm_msi_virq(VFIOMSIVector *vector, MSIMessage msg,static int vfio_msix_vector_do_use (PCIDevice *pdev,unsigned int nr,MSIMessage *msg,IOHandler *handler) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIOMSIVector *vector;int ret;@@ -542,7 +542,7 @@ static int vfio_msix_vector_use(PCIDevice *pdev,static void vfio_msix_vector_release (PCIDevice *pdev,unsigned int nr) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIOMSIVector *vector = &vdev->msi_vectors[nr];trace_vfio_msix_vector_release (vdev->vbasedev.name, nr);@@ -1063,7 +1063,7 @@ static const MemoryRegionOps vfio_vga_ops = {*/ static void vfio_sub_page_bar_update_mapping (PCIDevice *pdev,int bar) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIORegion *region = &vdev->bars[bar].region;MemoryRegion *mmap_mr, *region_mr, *base_mr;PCIIORegion *r;@@ -1109,7 +1109,7 @@ static void vfio_sub_page_bar_update_mapping(PCIDevice *pdev, int bar)*/ uint32_t vfio_pci_read_config (PCIDevice *pdev,uint32_t addr,int len) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);uint32_t emu_bits =0 , emu_val =0 , phys_val =0 , val;memcpy (&emu_bits, vdev->emulated_config_bits + addr, len);@@ -1142,7 +1142,7 @@ uint32_t vfio_pci_read_config(PCIDevice *pdev, uint32_t addr, int len)void vfio_pci_write_config (PCIDevice *pdev,uint32_t addr,uint32_t val,int len) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);uint32_t val_le =cpu_to_le32 (val);trace_vfio_pci_write_config (vdev->vbasedev.name, addr, val, len);@@ -2782,7 +2782,7 @@ static void vfio_unregister_req_notifier(VFIOPCIDevice *vdev)static void vfio_realize (PCIDevice *pdev,Error **errp) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);VFIODevice *vbasedev_iter;VFIOGroup *group;char *tmp, *subsys, group_path[PATH_MAX ], *group_name;@@ -3105,7 +3105,7 @@ error:static void vfio_instance_finalize (Object *obj) { -VFIOPCIDevice *vdev =VFIO_PCI (obj); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj);VFIOGroup *group = vdev->vbasedev.group;vfio_display_finalize (vdev);@@ -3125,7 +3125,7 @@ static void vfio_instance_finalize(Object *obj)static void vfio_exitfn (PCIDevice *pdev) { -VFIOPCIDevice *vdev =VFIO_PCI (pdev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (pdev);vfio_unregister_req_notifier (vdev);vfio_unregister_err_notifier (vdev);@@ -3144,7 +3144,7 @@ static void vfio_exitfn(PCIDevice *pdev)static void vfio_pci_reset (DeviceState *dev) { -VFIOPCIDevice *vdev =VFIO_PCI (dev); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (dev);trace_vfio_pci_reset (vdev->vbasedev.name);@@ -3184,7 +3184,7 @@ post_reset:static void vfio_instance_init (Object *obj) {PCIDevice *pci_dev =PCI_DEVICE (obj); -VFIOPCIDevice *vdev =VFIO_PCI (obj); +VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); device_add_bootindex_property(obj, &vdev->bootindex, "bootindex", NULL,@@ -3201,38 +3201,75 @@ static void vfio_instance_init(Object *obj) pci_dev->cap_present |=QEMU_PCI_CAP_EXPRESS ; } -static Property vfio_pci_dev_properties[] = { -DEFINE_PROP_PCI_HOST_DEVADDR ("host" , VFIOPCIDevice, host), -DEFINE_PROP_STRING ("sysfsdev" , VFIOPCIDevice, vbasedev.sysfsdev), +static Property vfio_pci_base_dev_properties[] = {DEFINE_PROP_ON_OFF_AUTO ("x-pre-copy-dirty-page-tracking" , VFIOPCIDevice, vbasedev.pre_copy_dirty_page_tracking,ON_OFF_AUTO_ON ), +DEFINE_PROP_UINT32 ("x-intx-mmap-timeout-ms" , VFIOPCIDevice, + intx.mmap_timeout,1100 ), +DEFINE_PROP_BOOL ("x-enable-migration" , VFIOPCIDevice, + vbasedev.enable_migration, false), +DEFINE_PROP_BOOL ("x-no-mmap" , VFIOPCIDevice, vbasedev.no_mmap, false), +DEFINE_PROP_BOOL ("x-no-kvm-intx" , VFIOPCIDevice, no_kvm_intx, false), +DEFINE_PROP_BOOL ("x-no-kvm-msi" , VFIOPCIDevice, no_kvm_msi, false), +DEFINE_PROP_BOOL ("x-no-kvm-msix" , VFIOPCIDevice, no_kvm_msix, false), +DEFINE_PROP_BOOL ("x-no-kvm-ioeventfd" , VFIOPCIDevice, no_kvm_ioeventfd, + false), +DEFINE_PROP_BOOL ("x-no-vfio-ioeventfd" , VFIOPCIDevice, no_vfio_ioeventfd, + false), +DEFINE_PROP_OFF_AUTO_PCIBAR ("x-msix-relocation" , VFIOPCIDevice, msix_relo, +OFF_AUTOPCIBAR_OFF ), +/* +* TODO - support passed fds... is this necessary? +* DEFINE_PROP_STRING("vfiofd", VFIOPCIDevice, vfiofd_name), +* DEFINE_PROP_STRING("vfiogroupfd, VFIOPCIDevice, vfiogroupfd_name), +*/ +DEFINE_PROP_END_OF_LIST (), +}; + +static void vfio_pci_base_dev_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); +PCIDeviceClass *pdc =PCI_DEVICE_CLASS (klass); + +device_class_set_props (dc, vfio_pci_base_dev_properties); + dc->desc ="VFIO PCI base device" ; +set_bit (DEVICE_CATEGORY_MISC , dc->categories); + pdc->exit = vfio_exitfn; + pdc->config_read = vfio_pci_read_config; + pdc->config_write = vfio_pci_write_config; +} + +static const TypeInfo vfio_pci_base_dev_info = { + .name =TYPE_VFIO_PCI_BASE , + .parent =TYPE_PCI_DEVICE , + .instance_size =0 , + .abstract = true, + .class_init = vfio_pci_base_dev_class_init, + .interfaces = (InterfaceInfo []) { + {INTERFACE_PCIE_DEVICE }, + {INTERFACE_CONVENTIONAL_PCI_DEVICE }, + { } + }, +}; + +static Property vfio_pci_dev_properties[] = { +DEFINE_PROP_PCI_HOST_DEVADDR ("host" , VFIOPCIDevice, host), +DEFINE_PROP_STRING ("sysfsdev" , VFIOPCIDevice, vbasedev.sysfsdev),DEFINE_PROP_ON_OFF_AUTO ("display" , VFIOPCIDevice, display,ON_OFF_AUTO_OFF ),DEFINE_PROP_UINT32 ("xres" , VFIOPCIDevice, display_xres,0 ),DEFINE_PROP_UINT32 ("yres" , VFIOPCIDevice, display_yres,0 ), -DEFINE_PROP_UINT32 ("x-intx-mmap-timeout-ms" , VFIOPCIDevice, - intx.mmap_timeout,1100 ),DEFINE_PROP_BIT ("x-vga" , VFIOPCIDevice, features,VFIO_FEATURE_ENABLE_VGA_BIT , false),DEFINE_PROP_BIT ("x-req" , VFIOPCIDevice, features,VFIO_FEATURE_ENABLE_REQ_BIT , true),DEFINE_PROP_BIT ("x-igd-opregion" , VFIOPCIDevice, features,VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT , false), -DEFINE_PROP_BOOL ("x-enable-migration" , VFIOPCIDevice, - vbasedev.enable_migration, false), -DEFINE_PROP_BOOL ("x-no-mmap" , VFIOPCIDevice, vbasedev.no_mmap, false),DEFINE_PROP_BOOL ("x-balloon-allowed" , VFIOPCIDevice, vbasedev.ram_block_discard_allowed, false), -DEFINE_PROP_BOOL ("x-no-kvm-intx" , VFIOPCIDevice, no_kvm_intx, false), -DEFINE_PROP_BOOL ("x-no-kvm-msi" , VFIOPCIDevice, no_kvm_msi, false),
hw/vfio/pci.c static void vfio_instance_init(Object *obj) · line 3275
Quoted 10 lines
hw/vfio/pci.c +0
-5-DEFINE_PROP_BOOL ("x-no-kvm-msix" , VFIOPCIDevice, no_kvm_msix, false),DEFINE_PROP_BOOL ("x-no-geforce-quirks" , VFIOPCIDevice, no_geforce_quirks, false), -DEFINE_PROP_BOOL ("x-no-kvm-ioeventfd" , VFIOPCIDevice, no_kvm_ioeventfd, - false), -DEFINE_PROP_BOOL ("x-no-vfio-ioeventfd" , VFIOPCIDevice, no_vfio_ioeventfd, - false),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,
Not sure I understand some of these choices. The kernel interface options like "host" and "sysfsdev" obviously belong here, and it makes sense for quirks related to real hardware to be here, igd, nvidia, gpu direct, etc. But the rest I'm a bit lost. Why would overriding the PCI IDs, disabling ballooning, disabling the device request interrupt, enabling exposing a VGA region, supporting a display, be unique to the kernel interface? Thanks,
Alex
Quoted 57 lines
hw/vfio/pci.c +4
-19@@ -3243,13 +3280,6 @@ static Property vfio_pci_dev_properties[] = {DEFINE_PROP_UNSIGNED_NODEFAULT ("x-nv-gpudirect-clique" , VFIOPCIDevice, nv_gpudirect_clique, qdev_prop_nv_gpudirect_clique, uint8_t), -DEFINE_PROP_OFF_AUTO_PCIBAR ("x-msix-relocation" , VFIOPCIDevice, msix_relo, -OFF_AUTOPCIBAR_OFF ), -/* -* TODO - support passed fds... is this necessary? -* DEFINE_PROP_STRING("vfiofd", VFIOPCIDevice, vfiofd_name), -* DEFINE_PROP_STRING("vfiogroupfd, VFIOPCIDevice, vfiogroupfd_name), -*/ DEFINE_PROP_END_OF_LIST(), };@@ -3261,25 +3291,16 @@ static void vfio_pci_dev_class_init(ObjectClass *klass, void *data) dc->reset = vfio_pci_reset;device_class_set_props (dc, vfio_pci_dev_properties); dc->desc ="VFIO-based PCI device assignment" ; -set_bit (DEVICE_CATEGORY_MISC , dc->categories); pdc->realize = vfio_realize; - pdc->exit = vfio_exitfn; - pdc->config_read = vfio_pci_read_config; - pdc->config_write = vfio_pci_write_config; }static const TypeInfo vfio_pci_dev_info = { .name =TYPE_VFIO_PCI , - .parent =TYPE_PCI_DEVICE , - .instance_size =sizeof (VFIOPCIDevice), + .parent =TYPE_VFIO_PCI_BASE , + .instance_size =sizeof (VFIOKernPCIDevice), .class_init = vfio_pci_dev_class_init, .instance_init = vfio_instance_init, .instance_finalize = vfio_instance_finalize, - .interfaces = (InterfaceInfo []) { - {INTERFACE_PCIE_DEVICE }, - {INTERFACE_CONVENTIONAL_PCI_DEVICE }, - { } - }, };static Property vfio_pci_dev_nohotplug_properties[] = {@@ -3298,12 +3319,13 @@ static void vfio_pci_nohotplug_dev_class_init(ObjectClass *klass, void *data)static const TypeInfo vfio_pci_nohotplug_dev_info = { .name =TYPE_VFIO_PCI_NOHOTPLUG , .parent =TYPE_VFIO_PCI , - .instance_size =sizeof (VFIOPCIDevice), + .instance_size =sizeof (VFIOKernPCIDevice), .class_init = vfio_pci_nohotplug_dev_class_init, };static void register_vfio_pci_dev_type (void ) { +type_register_static (&vfio_pci_base_dev_info);type_register_static (&vfio_pci_dev_info);type_register_static (&vfio_pci_nohotplug_dev_info); }
Re: [RFC v3 08/19] vfio-user: define socket receive functions
In reply to John JohnsonQuoted 20 lines · On Mon, 8 Nov 2021 16:46:36 -0800 John Johnson <[email protected]>…
Add infrastructure needed to receive incoming messages
Signed-off-by John G Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/pci.h | 2 +- hw/vfio/user-protocol.h | 62 +++++++++ hw/vfio/user.h | 9 +- hw/vfio/pci.c | 12 +- hw/vfio/user.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 1 + 6 files changed, 409 insertions(+), 3 deletions(-) create mode 100644 hw/vfio/user-protocol.hhw/vfio/pci.h +0
-0
hw/vfio/pci.h OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) · line 196
Quoted 4 lines
hw/vfio/pci.h +0
-1@@ -193,7 +193,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name; -bool secure_dma;/* disable shared mem for DMA */
Don't introduce it into the series to start with, confusing to review.
Quoted 26 lines
hw/vfio/pci.h +1
-0+bool send_queued;/* all sends are queued */ };/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ hw/vfio/user-protocol.h new file +16
-0@@ -0,0 +1,62 @@ +#ifndef VFIO_USER_PROTOCOL_H +#define VFIO_USER_PROTOCOL_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. +* +* Each message has a standard header that describes the command +* being sent, which is almost always a VFIO ioctl(). +* +* The header may be followed by command-specific data, such as the +* region and offset info for read and write commands.
hw/vfio/user-protocol.h line 26
Quoted 10 lines
hw/vfio/user-protocol.h new file +10
-0+*/ + +typedef struct { +uint16_t id; +uint16_t command; +uint32_t size; +uint32_t flags; +uint32_t error_reply; +}VFIOUserHdr ; +
A comment referencing the doc would probably be a good idea about here.
Quoted 23 lines
hw/vfio/user-protocol.h new file +23
-0+/* VFIOUserHdr commands */ +enum vfio_user_command { +VFIO_USER_VERSION =1 , +VFIO_USER_DMA_MAP =2 , +VFIO_USER_DMA_UNMAP =3 , +VFIO_USER_DEVICE_GET_INFO =4 , +VFIO_USER_DEVICE_GET_REGION_INFO =5 , +VFIO_USER_DEVICE_GET_REGION_IO_FDS =6 , +VFIO_USER_DEVICE_GET_IRQ_INFO =7 , +VFIO_USER_DEVICE_SET_IRQS =8 , +VFIO_USER_REGION_READ =9 , +VFIO_USER_REGION_WRITE =10 , +VFIO_USER_DMA_READ =11 , +VFIO_USER_DMA_WRITE =12 , +VFIO_USER_DEVICE_RESET =13 , +VFIO_USER_DIRTY_PAGES =14 , +VFIO_USER_MAX , +}; + +/* VFIOUserHdr flags */ +#define VFIO_USER_REQUEST 0x0 +#define VFIO_USER_REPLY 0x1 +#define VFIO_USER_TYPE 0xF
hw/vfio/user-protocol.h line 59
Quoted 10 lines
hw/vfio/user-protocol.h new file +10
-0+ +#define VFIO_USER_NO_REPLY 0x10 +#define VFIO_USER_ERROR 0x20 + + +#define VFIO_USER_DEF_MAX_FDS 8 +#define VFIO_USER_MAX_MAX_FDS 16 + +#define VFIO_USER_DEF_MAX_XFER (1024 * 1024) +#define VFIO_USER_MAX_MAX_XFER (64 * 1024 * 1024)
These are essentially magic numbers, some discussion of how these limits are derived would be useful for future contributors, but also only DEV_MAX_XFER is used in this patch and it's confusing why the macro isn't used directly. Most of the logic surrounding these is added in the next patch, so it doesn't really make sense to add them here. Thanks,
Alex
Quoted 468 lines
hw/vfio/user-protocol.h new file +3
-0+ + +#endif /* VFIO_USER_PROTOCOL_H */ hw/vfio/user.h +8
-1@@ -11,6 +11,8 @@* */ +#include "user-protocol.h" + typedef struct { int send_fds; int recv_fds;@@ -27,6 +29,7 @@ enum msg_type { typedef struct VFIOUserMsg { QTAILQ_ENTRY(VFIOUserMsg) next; + VFIOUserHdr *hdr; VFIOUserFDs *fds; uint32_t rsize; uint32_t id;@@ -70,9 +73,13 @@ typedef struct VFIOProxy { } VFIOProxy;/* VFIOProxy flags */ -#define VFIO_PROXY_CLIENT 0x1 +#define VFIO_PROXY_CLIENT 0x1 +#define VFIO_PROXY_FORCE_QUEUED 0x4VFIOProxy *vfio_user_connect_dev (SocketAddress *addr,Error **errp);void vfio_user_disconnect (VFIOProxy *proxy); +void vfio_user_set_handler (VFIODevice *vbasedev, +void (*handler)(void *opaque,VFIOUserMsg *msg), +void *reqarg);#endif /* VFIO_USER_H */ hw/vfio/pci.c +11
-1@@ -3448,6 +3448,11 @@ struct VFIOValidOps vfio_pci_valid_ops = {* vfio-user routines. */ +static void vfio_user_pci_process_req (void *opaque,VFIOUserMsg *msg) +{ + +} +/* * Emulated devices don't use host hot reset */ @@ -3501,6 +3506,11 @@ 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); + +if (udev->send_queued) { + proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; + } vbasedev->name =g_strdup_printf ("VFIO user <%s>" , udev->sock_name); vbasedev->dev =DEVICE (vdev);@@ -3524,7 +3534,7 @@ static void vfio_user_instance_finalize(Object *obj)static Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), -DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false), +DEFINE_PROP_BOOL ("x-send-queued" , VFIOUserPCIDevice, send_queued, false),DEFINE_PROP_END_OF_LIST (), };hw/vfio/user.c +326
-0@@ -25,9 +25,27 @@#include "sysemu/iothread.h" #include "user.h" +static uint64_t max_xfer_size =VFIO_USER_DEF_MAX_XFER ;static IOThread *vfio_user_iothread; +static void vfio_user_shutdown (VFIOProxy *proxy); +static VFIOUserMsg *vfio_user_getmsg (VFIOProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds); +static VFIOUserFDs *vfio_user_getfds (int numfds); +static void vfio_user_recycle (VFIOProxy *proxy,VFIOUserMsg *msg); + +static void vfio_user_recv (void *opaque); +static int vfio_user_recv_one (VFIOProxy *proxy); +static void vfio_user_cb (void *opaque); +static void vfio_user_request (void *opaque); + + +static inline void vfio_user_set_error (VFIOUserHdr *hdr,uint32_t err) +{ + hdr->flags |=VFIO_USER_ERROR ; + hdr->error_reply = err; +}/* * Functions called by main, CPU, or iothread threads @@ -39,11 +57,259 @@ static void vfio_user_shutdown(VFIOProxy *proxy)qio_channel_set_aio_fd_handler (proxy->ioc, proxy->ctx,NULL ,NULL ,NULL ); } +static VFIOUserMsg *vfio_user_getmsg (VFIOProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds) +{ +VFIOUserMsg *msg; + + msg =QTAILQ_FIRST (&proxy->free); +if (msg !=NULL ) { +QTAILQ_REMOVE (&proxy->free, msg, next); + }else { + msg =g_malloc0 (sizeof (*msg)); +qemu_cond_init (&msg->cv); + } + + msg->hdr = hdr; + msg->fds = fds; +return msg; +} + +/* +* Recycle a message list entry to the free list. +*/ +static void vfio_user_recycle (VFIOProxy *proxy,VFIOUserMsg *msg) +{ +if (msg->type ==VFIO_MSG_NONE ) { +error_printf ("vfio_user_recycle - freeing free msg\n" ); +return ; + } + +/* free msg buffer if no one is waiting to consume the reply */ +if (msg->type ==VFIO_MSG_NOWAIT || msg->type ==VFIO_MSG_ASYNC ) { +g_free (msg->hdr); + } + + msg->type =VFIO_MSG_NONE ; + msg->hdr =NULL ; + msg->fds =NULL ; + msg->complete = false; +QTAILQ_INSERT_HEAD (&proxy->free, msg, next); +} + +static VFIOUserFDs *vfio_user_getfds (int numfds) +{ +VFIOUserFDs *fds =g_malloc0 (sizeof (*fds) + (numfds *sizeof (int ))); + + fds->fds = (int *)((char *)fds +sizeof (*fds)); + +return fds; +}/* * Functions only called by iothread */ +static void vfio_user_recv (void *opaque) +{ +VFIOProxy *proxy = opaque; + + +QEMU_LOCK_GUARD (&proxy->lock); + +if (proxy->state ==VFIO_PROXY_CONNECTED ) { +while (vfio_user_recv_one (proxy) ==0 ) { + ; + } + } +} + +/* +* Receive and process one incoming message. +* +* For replies, find matching outgoing request and wake any waiters. +* For requests, queue in incoming list and run request BH. +*/ +static int vfio_user_recv_one (VFIOProxy *proxy) +{ +VFIOUserMsg *msg =NULL ; + g_autofree int *fdp = NULL; +VFIOUserFDs *reqfds; +VFIOUserHdr hdr; +struct iovec iov = { + .iov_base = &hdr, + .iov_len =sizeof (hdr), + }; +bool isreply = false; +int i, ret; +size_t msgleft, numfds =0 ; +char *data =NULL ; +char *buf =NULL ; +Error *local_err =NULL ; + +/* +* Read header +*/ + ret =qio_channel_readv_full (proxy->ioc, &iov,1 , &fdp, &numfds, + &local_err); +if (ret ==QIO_CHANNEL_ERR_BLOCK ) { +return ret; + } +if (ret <=0 ) { +/* read error or other side closed connection */ +if (ret ==0 ) { +error_setg (&local_err,"vfio_user_recv server closed socket" ); + }else { +error_prepend (&local_err,"vfio_user_recv" ); + } + goto fatal; + } +if (ret <sizeof (msg)) { +error_setg (&local_err,"vfio_user_recv short read of header" ); + goto fatal; + } + +/* +* Validate header +*/ +if (hdr.size <sizeof (VFIOUserHdr)) { +error_setg (&local_err,"vfio_user_recv bad header size" ); + goto fatal; + } +switch (hdr.flags &VFIO_USER_TYPE ) { +case VFIO_USER_REQUEST : + isreply = false; +break ; +case VFIO_USER_REPLY : + isreply = true; +break ; +default : +error_setg (&local_err,"vfio_user_recv unknown message type" ); + goto fatal; + } + +/* +* For replies, find the matching pending request. +* For requests, reap incoming FDs. +*/ +if (isreply) { + QTAILQ_FOREACH(msg, &proxy->pending, next) { +if (hdr.id == msg->id) { +break ; + } + } +if (msg ==NULL ) { +error_setg (&local_err,"vfio_user_recv unexpected reply" ); + goto err; + } +QTAILQ_REMOVE (&proxy->pending, msg, next); + +/* +* Process any received FDs +*/ +if (numfds !=0 ) { +if (msg->fds ==NULL || msg->fds->recv_fds < numfds) { +error_setg (&local_err,"vfio_user_recv unexpected FDs" ); + goto err; + } + msg->fds->recv_fds = numfds; +memcpy (msg->fds->fds, fdp, numfds *sizeof (int )); + } + }else { +if (numfds !=0 ) { + reqfds =vfio_user_getfds (numfds); +memcpy (reqfds->fds, fdp, numfds *sizeof (int )); + }else { + reqfds =NULL ; + } + } + +/* +* Put the whole message into a single buffer. +*/ +if (isreply) { +if (hdr.size > msg->rsize) { +error_setg (&local_err, +"vfio_user_recv reply larger than recv buffer" ); + goto err; + } + *msg->hdr = hdr; + data = (char *)msg->hdr +sizeof (hdr); + }else { +if (hdr.size > max_xfer_size) { +error_setg (&local_err,"vfio_user_recv request larger than max" ); + goto err; + } + buf =g_malloc0 (hdr.size); +memcpy (buf, &hdr,sizeof (hdr)); + data = buf +sizeof (hdr); + msg =vfio_user_getmsg (proxy, (VFIOUserHdr *)buf, reqfds); + msg->type =VFIO_MSG_REQ ; + } + + msgleft = hdr.size -sizeof (hdr); +if (msgleft !=0 ) { + ret =qio_channel_read (proxy->ioc, data, msgleft, &local_err); +/* error or would block */ +if (ret <0 ) { + goto fatal; + } +if (ret != msgleft) { +error_setg (&local_err,"vfio_user_recv short read of msg body" ); + goto fatal; + } + } + +/* +* Replies signal a waiter, if none just check for errors +* and free the message buffer. +* +* Requests get queued for the BH. +*/ +if (isreply) { + msg->complete = true; +if (msg->type ==VFIO_MSG_WAIT ) { +qemu_cond_signal (&msg->cv); + }else { +if (hdr.flags &VFIO_USER_ERROR ) { +error_printf ("vfio_user_rcv error reply on async request " ); +error_printf ("command %x error %s\n" , hdr.command, +strerror (hdr.error_reply)); + } +/* youngest nowait msg has been ack'd */ +if (proxy->last_nowait == msg) { + proxy->last_nowait =NULL ; + } +vfio_user_recycle (proxy, msg); + } + }else { +QTAILQ_INSERT_TAIL (&proxy->incoming, msg, next); +qemu_bh_schedule (proxy->req_bh); + } +return 0 ; + +/* +* fatal means the other side closed or we don't trust the stream +* err means this message is corrupt +*/ +fatal: +vfio_user_shutdown (proxy); + proxy->state =VFIO_PROXY_ERROR ; + +err: +for (i =0 ; i < numfds; i++) { +close (fdp[i]); + } +if (isreply && msg !=NULL ) { +/* force an error to keep sending thread from hanging */ +vfio_user_set_error (msg->hdr,EINVAL ); + msg->complete = true; +qemu_cond_signal (&msg->cv); + } +error_report_err (local_err); +return -1 ; +} +static void vfio_user_cb (void *opaque) {VFIOProxy *proxy = opaque;@@ -59,6 +325,51 @@ static void vfio_user_cb(void *opaque)* Functions called by main or CPU threads */ +/* +* Process incoming requests. +* +* The bus-specific callback has the form: +* request(opaque, msg) +* where 'opaque' was specified in vfio_user_set_handler +* and 'msg' is the inbound message. +* +* The callback is responsible for disposing of the message buffer, +* usually by re-using it when calling vfio_send_reply or vfio_send_error, +* both of which free their message buffer when the reply is sent. +* +* If the callback uses a new buffer, it needs to free the old one. +*/ +static void vfio_user_request (void *opaque) +{ +VFIOProxy *proxy = opaque; +VFIOUserMsgQ new, free; +VFIOUserMsg *msg; + +/* reap all incoming */ + WITH_QEMU_LOCK_GUARD(&proxy->lock) { + new = proxy->incoming; +QTAILQ_INIT (&proxy->incoming); + } +QTAILQ_INIT (&free); + +/* process list */ +while (!QTAILQ_EMPTY (&new)) { + msg =QTAILQ_FIRST (&new); +QTAILQ_REMOVE (&new, msg, next); + proxy->request (proxy->req_arg, msg); +QTAILQ_INSERT_HEAD (&free, msg, next); + } + +/* free list */ + WITH_QEMU_LOCK_GUARD(&proxy->lock) { +while (!QTAILQ_EMPTY (&free)) { + msg =QTAILQ_FIRST (&free); +QTAILQ_REMOVE (&free, msg, next); +vfio_user_recycle (proxy, msg); + } + } +} + static QLIST_HEAD(, VFIOProxy) vfio_user_sockets =QLIST_HEAD_INITIALIZER (vfio_user_sockets);@@ -97,6 +408,7 @@ VFIOProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp) } proxy->ctx =iothread_get_aio_context (vfio_user_iothread); + proxy->req_bh =qemu_bh_new (vfio_user_request, proxy);QTAILQ_INIT (&proxy->outgoing);QTAILQ_INIT (&proxy->incoming);@@ -107,6 +419,18 @@ VFIOProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp)return proxy; } +void vfio_user_set_handler (VFIODevice *vbasedev, +void (*handler)(void *opaque,VFIOUserMsg *msg), +void *req_arg) +{ +VFIOProxy *proxy = vbasedev->proxy; + + proxy->request = handler; + proxy->req_arg = req_arg; +qio_channel_set_aio_fd_handler (proxy->ioc, proxy->ctx, + vfio_user_recv,NULL , proxy); +} +void vfio_user_disconnect (VFIOProxy *proxy) {VFIOUserMsg *r1, *r2;@@ -122,6 +446,8 @@ void vfio_user_disconnect(VFIOProxy *proxy) }object_unref (OBJECT (proxy->ioc)); proxy->ioc =NULL ; +qemu_bh_delete (proxy->req_bh); + proxy->req_bh =NULL ; proxy->state =VFIO_PROXY_CLOSING ; QTAILQ_FOREACH_SAFE(r1, &proxy->outgoing, next, r2) {MAINTAINERS +1
-0@@ -1888,6 +1888,7 @@ S: Supported F: docs/devel/vfio-user.rst F: hw/vfio/user.c F: hw/vfio/user.h +F: hw/vfio/user-protocol.h vhost M: Michael S. Tsirkin <[email protected]>
Re: [RFC v3 05/19] Add validation ops vector
In reply to John JohnsonAdd a prefix on Subject: please. Same for previous in series.
Quoted 72 lines · On Mon, 8 Nov 2021 16:46:33 -0800 John Johnson <[email protected]>…
Validates cases where the return values aren't fully trusted (prep work for vfio-user, where the return values from the remote process aren't trusted)
Signed-off-by John G Johnson <[email protected]>--- include/hw/vfio/vfio-common.h | 21 ++++++++++++++ hw/vfio/pci.c | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+)include/hw/vfio/vfio-common.h +21
-0@@ -125,6 +125,7 @@ typedef struct VFIOHostDMAWindow {typedef struct VFIODeviceOps VFIODeviceOps ;typedef struct VFIODevIO VFIODevIO ; +typedef struct VFIOValidOps VFIOValidOps ; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next;@@ -141,6 +142,7 @@ typedef struct VFIODevice {bool enable_migration;VFIODeviceOps *ops;VFIODevIO *io_ops; +VFIOValidOps *valid_ops;unsigned int num_irqs;unsigned int num_regions;unsigned int flags;@@ -214,6 +216,25 @@ struct VFIOContIO {extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl; +/* +* This ops vector allows for bus-specific verification +* routines in cases where the server may not be fully +* trusted. +*/ +struct VFIOValidOps { +int (*validate_get_info)(VFIODevice *vdev,struct vfio_device_info *info); +int (*validate_get_region_info)(VFIODevice *vdev, +struct vfio_region_info *info,int *fd); +int (*validate_get_irq_info)(VFIODevice *vdev,struct vfio_irq_info *info); +}; + +#define VDEV_VALID_INFO (vdev, info) \ + ((vdev)->valid_ops->validate_get_info((vdev), (info))) +#define VDEV_VALID_REGION_INFO (vdev, info, fd) \ + ((vdev)->valid_ops->validate_get_region_info((vdev), (info), (fd))) +#define VDEV_VALID_IRQ_INFO (vdev, irq) \ + ((vdev)->valid_ops->validate_get_irq_info((vdev), (irq))) +#endif /* CONFIG_LINUX */ typedef struct VFIOGroup {hw/vfio/pci.c +8
-0@@ -3371,3 +3371,70 @@ static void register_vfio_pci_dev_type(void) } type_init(register_vfio_pci_dev_type) + + +/* + * PCI validation ops - used when return values need + * validation before use + */ + +static int vfio_pci_valid_info(VFIODevice *vbasedev,
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3391
Quoted 10 lines
hw/vfio/pci.c +10
-0+ struct vfio_device_info *info) +{ +/* must be PCI */ +if ((info->flags &VFIO_DEVICE_FLAGS_PCI ) ==0 ) { +return -EINVAL ; + } +/* only other valid flag is reset */ +if (info->flags & ~(VFIO_DEVICE_FLAGS_PCI |VFIO_DEVICE_FLAGS_RESET )) { +return -EINVAL ; + }
This means QEMU vfio-pci breaks on any extension of the flags field.
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3395
Quoted 4 lines
hw/vfio/pci.c +4
-0+/* account for extra migration region */ +if (info->num_regions >VFIO_PCI_NUM_REGIONS +1 ) { +return -EINVAL ; + }
This is also invalid, there can be device specific regions beyond migration.
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3398
Quoted 3 lines
hw/vfio/pci.c +3
-0+if (info->num_irqs >VFIO_PCI_NUM_IRQS ) { +return -EINVAL ; + }
And device specific IRQs.
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3411
Quoted 13 lines
hw/vfio/pci.c +13
-0+return 0 ; +} + +static int vfio_pci_valid_region_info (VFIODevice *vbasedev, +struct vfio_region_info *info, +int *fd) +{ +if (info->flags & ~(VFIO_REGION_INFO_FLAG_READ | +VFIO_REGION_INFO_FLAG_WRITE | +VFIO_REGION_INFO_FLAG_MMAP | +VFIO_REGION_INFO_FLAG_CAPS )) { +return -EINVAL ; + }
Similarly, this allows zero future extensions. Notice for instance how the CAPS flag was added later as a backwards compatible extension.
Quoted 8 lines
hw/vfio/pci.c +8
-0+if (info->index > vbasedev->num_regions) { +return -EINVAL ; + } +/* cap_offset in valid area */ +if ((info->flags &VFIO_REGION_INFO_FLAG_CAPS ) && + (info->cap_offset <sizeof (*info) || info->cap_offset > info->argsz)) { +return -EINVAL ; + }
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3429
Quoted 10 lines
hw/vfio/pci.c +10
-0+return 0 ; +} + +static int vfio_pci_valid_irq_info (VFIODevice *vbasedev, +struct vfio_irq_info *info) +{ +if (info->flags & ~(VFIO_IRQ_INFO_EVENTFD |VFIO_IRQ_INFO_MASKABLE | +VFIO_IRQ_INFO_AUTOMASKED |VFIO_IRQ_INFO_NORESIZE )) { +return -EINVAL ; + }
Similarly, nak. Thanks,
Alex
Quoted 11 lines
hw/vfio/pci.c +11
-0+if (info->index > vbasedev->num_irqs) { +return -EINVAL ; + } +return 0 ; +} + +struct VFIOValidOps vfio_pci_valid_ops = { + .validate_get_info = vfio_pci_valid_info, + .validate_get_region_info = vfio_pci_valid_region_info, + .validate_get_irq_info = vfio_pci_valid_irq_info, +};
Re: [RFC v3 13/19] vfio-user: pci_user_realize PCI setup
In reply to John JohnsonQuoted 15 lines · On Mon, 8 Nov 2021 16:46:41 -0800 John Johnson <[email protected]>…
PCI BARs read from remote device
PCI config reads/writes sent to remote serverSigned-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>--- hw/vfio/pci.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 88 insertions(+), 1 deletion(-)hw/vfio/pci.c +0
-0
hw/vfio/pci.c static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) · line 3553
Quoted 3 lines
hw/vfio/pci.c +0
-0@@ -3551,8 +3551,93 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) goto error; }
There's a LOT of duplication with the kernel realize function here. Thanks,
Alex
Quoted 101 lines
hw/vfio/pci.c +88
-1+/* Get a copy of config space */ + ret =VDEV_REGION_READ (vbasedev,VFIO_PCI_CONFIG_REGION_INDEX ,0 , +MIN (pci_config_size (pdev), vdev->config_size), + pdev->config); +if (ret < (int )MIN (pci_config_size (&vdev->pdev), vdev->config_size)) { +error_setg_errno (errp, -ret,"failed to read device config space" ); + goto error; + } + +/* vfio emulates a lot for us, but some bits need extra love */ + vdev->emulated_config_bits =g_malloc0 (vdev->config_size); + +/* QEMU can choose to expose the ROM or not */ +memset (vdev->emulated_config_bits +PCI_ROM_ADDRESS ,0xff ,4 ); +/* QEMU can also add or extend BARs */ +memset (vdev->emulated_config_bits +PCI_BASE_ADDRESS_0 ,0xff ,6 *4 ); + vdev->vendor_id =pci_get_word (pdev->config +PCI_VENDOR_ID ); + vdev->device_id =pci_get_word (pdev->config +PCI_DEVICE_ID ); + +/* QEMU can change multi-function devices to single function, or reverse */ + vdev->emulated_config_bits[PCI_HEADER_TYPE ] = +PCI_HEADER_TYPE_MULTI_FUNCTION ; + +/* Restore or clear multifunction, this is always controlled by QEMU */ +if (vdev->pdev.cap_present &QEMU_PCI_CAP_MULTIFUNCTION ) { + vdev->pdev.config[PCI_HEADER_TYPE ] |=PCI_HEADER_TYPE_MULTI_FUNCTION ; + }else { + vdev->pdev.config[PCI_HEADER_TYPE ] &= ~PCI_HEADER_TYPE_MULTI_FUNCTION ; + } + +/* +* Clear host resource mapping info. If we choose not to register a +* BAR, such as might be the case with the option ROM, we can get +* confusing, unwritable, residual addresses from the host here. +*/ +memset (&vdev->pdev.config[PCI_BASE_ADDRESS_0 ],0 ,24 ); +memset (&vdev->pdev.config[PCI_ROM_ADDRESS ],0 ,4 ); + +vfio_pci_size_rom (vdev); + +vfio_bars_prepare (vdev); + +vfio_msix_early_setup (vdev, &err); +if (err) { +error_propagate (errp, err); + goto error; + } + +vfio_bars_register (vdev); + + ret =vfio_add_capabilities (vdev, errp); +if (ret) { + goto out_teardown; + } + +/* QEMU emulates all of MSI & MSIX */ +if (pdev->cap_present &QEMU_PCI_CAP_MSIX ) { +memset (vdev->emulated_config_bits + pdev->msix_cap,0xff , +MSIX_CAP_LENGTH ); + } + +if (pdev->cap_present &QEMU_PCI_CAP_MSI ) { +memset (vdev->emulated_config_bits + pdev->msi_cap,0xff , + vdev->msi_cap_size); + } + +if (vdev->pdev.config[PCI_INTERRUPT_PIN ] !=0 ) { + vdev->intx.mmap_timer =timer_new_ms (QEMU_CLOCK_VIRTUAL , + vfio_intx_mmap_enable, vdev); +pci_device_set_intx_routing_notifier (&vdev->pdev, + vfio_intx_routing_notifier); + vdev->irqchip_change_notifier.notify = vfio_irqchip_change; +kvm_irqchip_add_change_notifier (&vdev->irqchip_change_notifier); + ret =vfio_intx_enable (vdev, errp); +if (ret) { + goto out_deregister; + } + } +return ; +out_deregister: +pci_device_set_intx_routing_notifier (&vdev->pdev,NULL ); +kvm_irqchip_remove_change_notifier (&vdev->irqchip_change_notifier); +out_teardown: +vfio_teardown_msi (vdev); +vfio_bars_exit (vdev); error:vfio_user_disconnect (proxy);error_prepend (errp,VFIO_MSG_PREFIX , vdev->vbasedev.name);@@ -3565,7 +3650,9 @@ static void vfio_user_instance_finalize(Object *obj)vfio_put_device (vdev); -vfio_user_disconnect (vbasedev->proxy); +if (vbasedev->proxy !=NULL ) { +vfio_user_disconnect (vbasedev->proxy); + } }static Property vfio_user_pci_dev_properties[] = {
Re: [RFC v3 16/19] vfio-user: dma map/unmap operations
In reply to John JohnsonQuoted 18 lines · On Mon, 8 Nov 2021 16:46:44 -0800 John Johnson <[email protected]>…
Signed-off-by Jagannathan Raman <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>--- hw/vfio/pci.h | 1 + hw/vfio/user-protocol.h | 32 +++++++ hw/vfio/user.h | 1 + include/hw/vfio/vfio-common.h | 4 + hw/vfio/common.c | 76 +++++++++++++--- hw/vfio/pci.c | 4 + hw/vfio/user.c | 206 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 309 insertions(+), 15 deletions(-)hw/vfio/pci.h +0
-0
hw/vfio/pci.h OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) · line 196
Quoted 4 lines
hw/vfio/pci.h +1
-0@@ -193,6 +193,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name; +bool secure_dma;/* disable shared mem for DMA */
???????????? It's there, it's gone, it's back.
Quoted 68 lines
hw/vfio/pci.h +0
-0bool send_queued;/* all sends are queued */ bool no_post;/* all regions write are sync */ };hw/vfio/user-protocol.h +32
-0@@ -83,6 +83,31 @@ typedef struct {/* +* 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_device_info */ @@ -146,4 +171,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.h +1
-0@@ -74,6 +74,7 @@ typedef struct VFIOProxy {/* VFIOProxy flags */ #define VFIO_PROXY_CLIENT 0x1 +#define VFIO_PROXY_SECURE 0x2#define VFIO_PROXY_FORCE_QUEUED 0x4#define VFIO_PROXY_NO_POST 0x8include/hw/vfio/vfio-common.h +0
-0
include/hw/vfio/vfio-common.h typedef struct VFIOContainer { · line 93
Quoted 4 lines
include/hw/vfio/vfio-common.h +1
-0@@ -90,6 +90,8 @@ typedef struct VFIOContainer {VFIOContIO *io_ops;bool initialized;bool dirty_pages_supported; +bool will_commit;
The entire will_commit concept hidden in the map and unmap operations from many patches ago should be introduced here, or later.
Quoted 44 lines
include/hw/vfio/vfio-common.h +3
-0+bool need_map_fd;uint64_t dirty_pgsizes;uint64_t max_dirty_bitmap_size;unsigned long pgsizes;@@ -210,6 +212,7 @@ struct VFIOContIO {int (*dirty_bitmap)(VFIOContainer *container,struct vfio_iommu_type1_dirty_bitmap *bitmap,struct vfio_iommu_type1_dirty_bitmap_get *range); +void (*wait_commit)(VFIOContainer *container); };#define CONT_DMA_MAP (cont, map, fd, will_commit) \@@ -218,6 +221,7 @@ struct VFIOContIO { ((cont)->io_ops->dma_unmap((cont), (unmap), (bitmap), (will_commit)))#define CONT_DIRTY_BITMAP (cont, bitmap, range) \ ((cont)->io_ops->dirty_bitmap((cont), (bitmap), (range))) +#define CONT_WAIT_COMMIT (cont) ((cont)->io_ops->wait_commit(cont))extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl;hw/vfio/common.c +2
-1@@ -411,6 +411,7 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,struct vfio_iommu_type1_dma_unmap *unmap;struct vfio_bitmap *bitmap;uint64_t pages =REAL_HOST_PAGE_ALIGN (size) / qemu_real_host_page_size; +bool will_commit = container->will_commit;int ret; unmap =g_malloc0 (sizeof (*unmap) +sizeof (*bitmap));@@ -444,7 +445,7 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container, goto unmap_exit; } - ret =CONT_DMA_UNMAP (container, unmap, bitmap, false); + ret =CONT_DMA_UNMAP (container, unmap, bitmap, will_commit);if (!ret) {cpu_physical_memory_set_dirty_lebitmap ((unsigned long *)bitmap->data, iotlb->translated_addr, pages);@@ -471,16 +472,17 @@ static int vfio_dma_unmap(VFIOContainer *container, .iova = iova, .size = size,
hw/vfio/common.c static int vfio_dma_unmap(VFIOContainer *container, · line 482
Quoted 10 lines
hw/vfio/common.c +2
-1}; +bool will_commit = container->will_commit;if (iotlb && container->dirty_pages_supported &&vfio_devices_all_running_and_saving (container)) {return vfio_dma_unmap_bitmap (container, iova, size, iotlb); } -return CONT_DMA_UNMAP (container, &unmap,NULL , false); +return CONT_DMA_UNMAP (container, &unmap,NULL , will_commit);
We're passing the container, why do we need a separate will_commit arg for these?
Quoted 20 lines
hw/vfio/common.c +3
-3} -static int vfio_dma_map (VFIOContainer *container,hwaddr iova, +static int vfio_dma_map (VFIOContainer *container,MemoryRegion *mr,hwaddr iova,ram_addr_t size,void *vaddr,bool readonly) {struct vfio_iommu_type1_dma_map map = {@@ -490,13 +492,23 @@ static int vfio_dma_map(VFIOContainer *container, hwaddr iova, .iova = iova, .size = size, }; -int ret; +int fd, ret; +bool will_commit = container->will_commit;if (!readonly) { map.flags |=VFIO_DMA_MAP_FLAG_WRITE ; } - ret =CONT_DMA_MAP (container, &map,-1 , false);
hw/vfio/common.c static int vfio_dma_map(VFIOContainer *container, hwaddr iova, · line 511
Quoted 10 lines
hw/vfio/common.c +10
-0+if (container->need_map_fd) { + fd =memory_region_get_fd (mr); +if (fd !=-1 ) { + map.vaddr =qemu_ram_block_host_offset (mr->ram_block, vaddr); + } + }else { + fd =-1 ; + } + + ret =CONT_DMA_MAP (container, &map, fd, will_commit);
Why were we even passing a -1 fd previously? Would it make more sense to pass the mr and put this in the user variant .map_dma? We're going to the trouble to pass the mr down this far here. If the map callback handled the above fd and map.vaddr we could also avoid of the need_map_fd flag on the container.
Quoted 78 lines
hw/vfio/common.c +18
-6if (ret <0 ) {error_report ("VFIO_MAP_DMA failed: %s" ,strerror (-ret));@@ -557,7 +569,8 @@ static bool vfio_listener_skipped_section(MemoryRegionSection *section)/* Called with rcu_read_lock held. */ static bool vfio_get_xlat_addr (IOMMUTLBEntry *iotlb,void **vaddr, -ram_addr_t *ram_addr,bool *read_only) +ram_addr_t *ram_addr,bool *read_only, +MemoryRegion **mrp) {MemoryRegion *mr;hwaddr xlat;@@ -638,6 +651,10 @@ static bool vfio_get_xlat_addr(IOMMUTLBEntry *iotlb, void **vaddr, *read_only = !writable || mr->readonly; } +if (mrp !=NULL ) { + *mrp = mr; + } +return true; }@@ -645,6 +662,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) {VFIOGuestIOMMU *giommu =container_of (n, VFIOGuestIOMMU, n);VFIOContainer *container = giommu->container; +MemoryRegion *mr;hwaddr iova = iotlb->iova + giommu->iommu_offset;void *vaddr;int ret;@@ -663,7 +681,7 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)if ((iotlb->perm &IOMMU_RW ) !=IOMMU_NONE ) {bool read_only; -if (!vfio_get_xlat_addr (iotlb, &vaddr,NULL , &read_only)) { +if (!vfio_get_xlat_addr (iotlb, &vaddr,NULL , &read_only, &mr)) { goto out; }/* @@ -673,14 +691,14 @@ static void vfio_iommu_map_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb)* of vaddr will always be there, even if the memory object is * destroyed and its backing memory munmap-ed. */ - ret =vfio_dma_map (container, iova, + ret =vfio_dma_map (container, mr, iova, iotlb->addr_mask +1 , vaddr, read_only);if (ret) {error_report ("vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , +"0x%" HWADDR_PRIx", %p)" , container, iova, - iotlb->addr_mask +1 , vaddr, ret); + iotlb->addr_mask +1 , vaddr); } }else { ret =vfio_dma_unmap (container, iova, iotlb->addr_mask +1 , iotlb);@@ -735,7 +753,7 @@ static int vfio_ram_discard_notify_populate(RamDiscardListener *rdl, section->offset_within_address_space; vaddr =memory_region_get_ram_ptr (section->mr) + start; - ret =vfio_dma_map (vrdl->container, iova, next - start, + ret =vfio_dma_map (vrdl->container, section->mr, iova, next - start, vaddr, section->readonly);if (ret) {/* Rollback */ @@ -843,6 +861,23 @@ static void vfio_unregister_ram_discard_listener(VFIOContainer *container,g_free (vrdl); } +static void vfio_listener_begin (MemoryListener *listener) +{ +VFIOContainer *container =container_of (listener, VFIOContainer, listener); + +/* cannot drop BQL during the transaction, send maps/demaps async */ + container->will_commit = true;
hw/vfio/common.c static void vfio_unregister_ram_discard_listener(VFIOContainer *container, · line 879
Quoted 10 lines
hw/vfio/common.c +10
-0+} + +static void vfio_listener_commit (MemoryListener *listener) +{ +VFIOContainer *container =container_of (listener, VFIOContainer, listener); + +/* wait for any async requests sent during the transaction */ +CONT_WAIT_COMMIT (container); + container->will_commit = false; +}
Not sure I follow the semantics, when would the map/unmap callbacks get called when will_commit is false?
Does it really make sense to have macros for ops that we call in one place?
Quoted 39 lines
hw/vfio/common.c +7
-4+static void vfio_listener_region_add (MemoryListener *listener,MemoryRegionSection *section) {@@ -1035,12 +1070,12 @@ static void vfio_listener_region_add(MemoryListener *listener, } } - ret =vfio_dma_map (container, iova,int128_get64 (llsize), + ret =vfio_dma_map (container, section->mr, iova,int128_get64 (llsize), vaddr, section->readonly);if (ret) {error_setg (&err,"vfio_dma_map(%p, 0x%" HWADDR_PRIx", " -"0x%" HWADDR_PRIx", %p) = %d (%m)" , - container, iova,int128_get64 (llsize), vaddr, ret); +"0x%" HWADDR_PRIx", %p)" , + container, iova,int128_get64 (llsize), vaddr);if (memory_region_is_ram_device (section->mr)) {/* Allow unexpected mappings not to be fatal for RAM devices */ error_report_err (err);@@ -1301,7 +1336,7 @@ static void vfio_iommu_map_dirty_notify(IOMMUNotifier *n, IOMMUTLBEntry *iotlb) }rcu_read_lock (); -if (vfio_get_xlat_addr (iotlb,NULL , &translated_addr,NULL )) { +if (vfio_get_xlat_addr (iotlb,NULL , &translated_addr,NULL ,NULL )) {int ret; ret = vfio_get_dirty_bitmap(container, iova, iotlb->addr_mask + 1,@@ -1418,6 +1453,8 @@ static void vfio_listener_log_sync(MemoryListener *listener, }static const MemoryListener vfio_memory_listener = { + .begin = vfio_listener_begin, + .commit = vfio_listener_commit, .region_add = vfio_listener_region_add, .region_del = vfio_listener_region_del, .log_global_start = vfio_listener_log_global_start,
hw/vfio/common.c int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, · line 1601
Quoted 4 lines
hw/vfio/common.c +1
-0@@ -1561,6 +1598,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;
Should this be in a different patch? It looks unrelated.
Quoted 20 lines
hw/vfio/common.c +2
-0region->remfd =vfio_get_region_info_remfd (vbasedev, index);if (region->size) {@@ -2047,6 +2085,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container->dirty_pages_supported = false; container->dma_max_mappings =0 ; container->io_ops = &vfio_cont_io_ioctl; + container->need_map_fd = false;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list);QLIST_INIT (&container->vrdl_list);@@ -2230,6 +2269,7 @@ void vfio_connect_proxy(VFIOProxy *proxy, VFIOGroup *group, AddressSpace *as) container->space = space; container->fd =-1 ; container->io_ops = &vfio_cont_io_sock; + container->need_map_fd = (proxy->flags &VFIO_PROXY_SECURE ) ==0 ;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list); container->proxy = proxy;
hw/vfio/common.c static int vfio_io_dirty_bitmap(VFIOContainer *container, · line 2926
Quoted 8 lines
hw/vfio/common.c +5
-0@@ -2879,8 +2919,14 @@ static int vfio_io_dirty_bitmap(VFIOContainer *container,return ret; } +static void vfio_io_wait_commit (VFIOContainer *container) +{ +/* ioctl()s are synchronous */ +} +
Maybe these should just be "dma_commit" rather than "wait_commit"? I'd also tend to suggest "async" rather than "will_commit".
Quoted 21 lines
hw/vfio/common.c +1
-0VFIOContIO vfio_cont_io_ioctl = { .dma_map = vfio_io_dma_map, .dma_unmap = vfio_io_dma_unmap, .dirty_bitmap = vfio_io_dirty_bitmap, + .wait_commit = vfio_io_wait_commit, };hw/vfio/pci.c +3
-0@@ -3516,6 +3516,9 @@ 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->secure_dma) { + proxy->flags |=VFIO_PROXY_SECURE ; + }if (udev->send_queued) { proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; }
hw/vfio/pci.c static void vfio_user_instance_finalize(Object *obj) · line 3692
Quoted 4 lines
hw/vfio/pci.c +1
-0@@ -3686,6 +3689,7 @@ static void vfio_user_instance_finalize(Object *obj)static Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false),
"secure_dma" looks entirely compartmentalized that it could be a separate patch. Thanks,
Alex
Quoted 249 lines
hw/vfio/pci.c +0
-0DEFINE_PROP_BOOL ("x-send-queued" , VFIOUserPCIDevice, send_queued, false),DEFINE_PROP_BOOL ("x-no-posted-writes" , VFIOUserPCIDevice, no_post, false), DEFINE_PROP_END_OF_LIST(),hw/vfio/user.c +206
-0@@ -52,8 +52,11 @@ static void vfio_user_request(void *opaque);static int vfio_user_send_queued (VFIOProxy *proxy,VFIOUserMsg *msg);static void vfio_user_send_async (VFIOProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds); +static void vfio_user_send_nowait (VFIOProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds,int rsize);static void vfio_user_send_wait (VFIOProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds,int rsize,bool nobql); +static void vfio_user_wait_reqs (VFIOProxy *proxy);static void vfio_user_request_msg (VFIOUserHdr *hdr,uint16_t cmd,uint32_t size,uint32_t flags);@@ -562,6 +565,36 @@ static void vfio_user_send_async(VFIOProxy *proxy, VFIOUserHdr *hdr, } } +/* +* nowait send - vfio_wait_reqs() can wait for it later +*/ +static void vfio_user_send_nowait (VFIOProxy *proxy,VFIOUserHdr *hdr, +VFIOUserFDs *fds,int rsize) +{ +VFIOUserMsg *msg; +int ret; + +if (hdr->flags &VFIO_USER_NO_REPLY ) { +error_printf ("vfio_user_send_nowait on async message\n" ); +return ; + } + +QEMU_LOCK_GUARD (&proxy->lock); + + msg =vfio_user_getmsg (proxy, hdr, fds); + msg->id = hdr->id; + msg->rsize = rsize ? rsize : hdr->size; + msg->type =VFIO_MSG_NOWAIT ; + + ret =vfio_user_send_queued (proxy, msg); +if (ret <0 ) { +vfio_user_recycle (proxy, msg); +return ; + } + + proxy->last_nowait = msg; +} +static void vfio_user_send_wait (VFIOProxy *proxy,VFIOUserHdr *hdr,VFIOUserFDs *fds,int rsize,bool nobql) {@@ -610,6 +643,56 @@ static void vfio_user_send_wait(VFIOProxy *proxy, VFIOUserHdr *hdr, } } +static void vfio_user_wait_reqs (VFIOProxy *proxy) +{ +VFIOUserMsg *msg; +bool iolock = false; + +/* +* Any DMA map/unmap requests sent in the middle +* of a memory region transaction were sent nowait. +* Wait for them here. +*/ +qemu_mutex_lock (&proxy->lock); +if (proxy->last_nowait !=NULL ) { + iolock =qemu_mutex_iothread_locked (); +if (iolock) { +qemu_mutex_unlock_iothread (); + } + +/* +* Change type to WAIT to wait for reply +*/ + msg = proxy->last_nowait; + msg->type =VFIO_MSG_WAIT ; +while (!msg->complete) { +if (!qemu_cond_timedwait (&msg->cv, &proxy->lock, wait_time)) { +error_printf ("vfio_wait_reqs - timed out\n" ); +break ; + } + } + +if (msg->hdr->flags &VFIO_USER_ERROR ) { +error_printf ("vfio_user_wait_reqs - error reply on async request " ); +error_printf ("command %x error %s\n" , msg->hdr->command, +strerror (msg->hdr->error_reply)); + } + + proxy->last_nowait =NULL ; +/* +* Change type back to NOWAIT to free +*/ + msg->type =VFIO_MSG_NOWAIT ; +vfio_user_recycle (proxy, msg); + } + +/* lock order is BQL->proxy - don't hold proxy when getting BQL */ +qemu_mutex_unlock (&proxy->lock); +if (iolock) { +qemu_mutex_lock_iothread (); + } +} + static QLIST_HEAD(, VFIOProxy) vfio_user_sockets =QLIST_HEAD_INITIALIZER (vfio_user_sockets);@@ -935,6 +1018,102 @@ int vfio_user_validate_version(VFIODevice *vbasedev, Error **errp)return 0 ; } +static int vfio_user_dma_map (VFIOProxy *proxy, +struct vfio_iommu_type1_dma_map *map, +int fd,bool will_commit) +{ +VFIOUserFDs *fds =NULL ; +VFIOUserDMAMap *msgp =g_malloc0 (sizeof (*msgp)); +int ret; + +vfio_user_request_msg (&msgp->hdr,VFIO_USER_DMA_MAP ,sizeof (*msgp),0 ); + msgp->argsz = map->argsz; + msgp->flags = map->flags; + msgp->offset = map->vaddr; + msgp->iova = map->iova; + msgp->size = map->size; + +/* +* The will_commit case sends without blocking or dropping BQL. +* They're later waited for in vfio_send_wait_reqs. +*/ +if (will_commit) { +/* can't use auto variable since we don't block */ +if (fd !=-1 ) { + fds =vfio_user_getfds (1 ); + fds->send_fds =1 ; + fds->fds[0 ] = fd; + } +vfio_user_send_nowait (proxy, &msgp->hdr, fds,0 ); + ret =0 ; + }else { +VFIOUserFDs local_fds = {1 ,0 , &fd }; + + fds = fd !=-1 ? &local_fds :NULL ; +vfio_user_send_wait (proxy, &msgp->hdr, fds,0 , will_commit); + ret = (msgp->hdr.flags &VFIO_USER_ERROR ) ? -msgp->hdr.error_reply :0 ; +g_free (msgp); + } + +return ret; +} + +static int vfio_user_dma_unmap (VFIOProxy *proxy, +struct vfio_iommu_type1_dma_unmap *unmap, +struct vfio_bitmap *bitmap,bool will_commit) +{ +struct { +VFIOUserDMAUnmap msg; +VFIOUserBitmap bitmap; + } *msgp =NULL ; +int msize, rsize; +bool blocking = !will_commit; + +if (bitmap ==NULL && + (unmap->flags &VFIO_DMA_UNMAP_FLAG_GET_DIRTY_BITMAP )) { +error_printf ("vfio_user_dma_unmap mismatched flags and bitmap\n" ); +return -EINVAL ; + } + +/* +* If a dirty bitmap is returned, allocate extra space for it +* and block for reply even in the will_commit case. +* Otherwise, can send the unmap request without waiting. +*/ +if (bitmap !=NULL ) { + blocking = true; + msize =sizeof (*msgp); + rsize = msize + bitmap->size; + msgp =g_malloc0 (rsize); + msgp->bitmap.pgsize = bitmap->pgsize; + msgp->bitmap.size = bitmap->size; + }else { + msize = rsize =sizeof (VFIOUserDMAUnmap); + msgp =g_malloc0 (rsize); + } + +vfio_user_request_msg (&msgp->msg.hdr,VFIO_USER_DMA_UNMAP , msize,0 ); + msgp->msg.argsz = unmap->argsz; + msgp->msg.flags = unmap->flags; + msgp->msg.iova = unmap->iova; + msgp->msg.size = unmap->size; + +if (blocking) { +vfio_user_send_wait (proxy, &msgp->msg.hdr,NULL , rsize, will_commit); +if (msgp->msg.hdr.flags &VFIO_USER_ERROR ) { +return -msgp->msg.hdr.error_reply; + } +if (bitmap !=NULL ) { +memcpy (bitmap->data, &msgp->bitmap.data, bitmap->size); + } +g_free (msgp); + }else { +vfio_user_send_nowait (proxy, &msgp->msg.hdr,NULL , rsize); + } + +return 0 ; +} +static int vfio_user_get_info (VFIOProxy *proxy,struct vfio_device_info *info) {VFIOUserDeviceInfo msg;@@ -1225,5 +1404,32 @@ VFIODevIO vfio_dev_io_sock = { }; +static int vfio_user_io_dma_map (VFIOContainer *container, +struct vfio_iommu_type1_dma_map *map, +int fd,bool will_commit) +{ +if (fd !=-1 ) { +return vfio_user_dma_map (container->proxy, map, fd, will_commit); + }else { + map->vaddr =0 ; +return vfio_user_dma_map (container->proxy, map,-1 , will_commit); + } +} + +static int vfio_user_io_dma_unmap (VFIOContainer *container, +struct vfio_iommu_type1_dma_unmap *unmap, +struct vfio_bitmap *bitmap,bool will_commit) +{ +return vfio_user_dma_unmap (container->proxy, unmap, bitmap, will_commit); +} + +static void vfio_user_io_wait_commit (VFIOContainer *container) +{ +vfio_user_wait_reqs (container->proxy); +} +VFIOContIO vfio_cont_io_sock = { + .dma_map = vfio_user_io_dma_map, + .dma_unmap = vfio_user_io_dma_unmap, + .wait_commit = vfio_user_io_wait_commit, };
Re: [RFC v3 02/19] vfio-user: add VFIO base abstract class
In reply to Alex WilliamsonQuoted 42 lines · Alex Williamson
Quoted 23 lines · On Mon, 8 Nov 2021 16:46:30 -0800 John Johnson <[email protected]>…
hw/vfio/pci.c +4
-7+ +static Property vfio_pci_dev_properties[] = { +DEFINE_PROP_PCI_HOST_DEVADDR ("host" , VFIOPCIDevice, host), +DEFINE_PROP_STRING ("sysfsdev" , VFIOPCIDevice, vbasedev.sysfsdev),DEFINE_PROP_ON_OFF_AUTO ("display" , VFIOPCIDevice, display,ON_OFF_AUTO_OFF ),DEFINE_PROP_UINT32 ("xres" , VFIOPCIDevice, display_xres,0 ),DEFINE_PROP_UINT32 ("yres" , VFIOPCIDevice, display_yres,0 ), -DEFINE_PROP_UINT32 ("x-intx-mmap-timeout-ms" , VFIOPCIDevice, - intx.mmap_timeout,1100 ),DEFINE_PROP_BIT ("x-vga" , VFIOPCIDevice, features,VFIO_FEATURE_ENABLE_VGA_BIT , false),DEFINE_PROP_BIT ("x-req" , VFIOPCIDevice, features,VFIO_FEATURE_ENABLE_REQ_BIT , true),DEFINE_PROP_BIT ("x-igd-opregion" , VFIOPCIDevice, features,VFIO_FEATURE_ENABLE_IGD_OPREGION_BIT , false), -DEFINE_PROP_BOOL ("x-enable-migration" , VFIOPCIDevice, - vbasedev.enable_migration, false), -DEFINE_PROP_BOOL ("x-no-mmap" , VFIOPCIDevice, vbasedev.no_mmap, false),DEFINE_PROP_BOOL ("x-balloon-allowed" , VFIOPCIDevice, vbasedev.ram_block_discard_allowed, false), -DEFINE_PROP_BOOL ("x-no-kvm-intx" , VFIOPCIDevice, no_kvm_intx, false), - DEFINE_PROP_BOOL("x-no-kvm-msi", VFIOPCIDevice, no_kvm_msi, false),hw/vfio/pci.c static void vfio_instance_init(Object *obj) · line 3275
Quoted 10 lines
hw/vfio/pci.c +0
-5-DEFINE_PROP_BOOL ("x-no-kvm-msix" , VFIOPCIDevice, no_kvm_msix, false),DEFINE_PROP_BOOL ("x-no-geforce-quirks" , VFIOPCIDevice, no_geforce_quirks, false), -DEFINE_PROP_BOOL ("x-no-kvm-ioeventfd" , VFIOPCIDevice, no_kvm_ioeventfd, - false), -DEFINE_PROP_BOOL ("x-no-vfio-ioeventfd" , VFIOPCIDevice, no_vfio_ioeventfd, - false),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,
Quoted 8 lines
Not sure I understand some of these choices. The kernel interface options like "host" and "sysfsdev" obviously belong here, and it makes sense for quirks related to real hardware to be here, igd, nvidia, gpu direct, etc. But the rest I'm a bit lost. Why would overriding the PCI IDs, disabling ballooning, disabling the device request interrupt, enabling exposing a VGA region, supporting a display, be unique to the kernel interface? Thanks,
I thought the overrides were workarounds for broken HW. I don’t
see a problem with supporting them; it just seemed unnecessary extra code.
The VGA properties are intentionally deleted, as we do not support
graphics console displays. That could be added as an later RFE if there is interest in libvfio-user based graphics device emulation.
JJ
Re: [RFC v3 05/19] Add validation ops vector
In reply to Alex WilliamsonQuoted 7 lines · Alex Williamson
Add a prefix on Subject: please. Same for previous in series.
ok
Quoted 131 lines
Quoted 72 lines · On Mon, 8 Nov 2021 16:46:33 -0800 John Johnson <[email protected]>…
Validates cases where the return values aren't fully trusted (prep work for vfio-user, where the return values from the remote process aren't trusted)
Signed-off-by John G Johnson <[email protected]>---
include/hw/vfio/vfio-common.h| 21 ++++++++++++++hw/vfio/pci.c| 67 +++++++++++++++++++++++++++++++++++++++++++2 files changed, 88 insertions(+)include/hw/vfio/vfio-common.h +21
-0@@ -125,6 +125,7 @@ typedef struct VFIOHostDMAWindow {typedef struct VFIODeviceOps VFIODeviceOps ;typedef struct VFIODevIO VFIODevIO ; +typedef struct VFIOValidOps VFIOValidOps ; typedef struct VFIODevice { QLIST_ENTRY(VFIODevice) next;@@ -141,6 +142,7 @@ typedef struct VFIODevice {bool enable_migration;VFIODeviceOps *ops;VFIODevIO *io_ops; +VFIOValidOps *valid_ops;unsigned int num_irqs;unsigned int num_regions;unsigned int flags;@@ -214,6 +216,25 @@ struct VFIOContIO {extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl; +/* +* This ops vector allows for bus-specific verification +* routines in cases where the server may not be fully +* trusted. +*/ +struct VFIOValidOps { +int (*validate_get_info)(VFIODevice *vdev,struct vfio_device_info *info); +int (*validate_get_region_info)(VFIODevice *vdev, +struct vfio_region_info *info,int *fd); +int (*validate_get_irq_info)(VFIODevice *vdev,struct vfio_irq_info *info); +}; + +#define VDEV_VALID_INFO (vdev, info) \ + ((vdev)->valid_ops->validate_get_info((vdev), (info))) +#define VDEV_VALID_REGION_INFO (vdev, info, fd) \ + ((vdev)->valid_ops->validate_get_region_info((vdev), (info), (fd))) +#define VDEV_VALID_IRQ_INFO (vdev, irq) \ + ((vdev)->valid_ops->validate_get_irq_info((vdev), (irq))) +#endif /* CONFIG_LINUX */ typedef struct VFIOGroup {hw/vfio/pci.c +8
-0@@ -3371,3 +3371,70 @@ static void register_vfio_pci_dev_type(void) } type_init(register_vfio_pci_dev_type) + + +/* + * PCI validation ops - used when return values need + * validation before use + */ + +static int vfio_pci_valid_info(VFIODevice *vbasedev,hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3391
Quoted 10 lines
hw/vfio/pci.c +10
-0+ struct vfio_device_info *info) +{ +/* must be PCI */ +if ((info->flags &VFIO_DEVICE_FLAGS_PCI ) ==0 ) { +return -EINVAL ; + } +/* only other valid flag is reset */ +if (info->flags & ~(VFIO_DEVICE_FLAGS_PCI |VFIO_DEVICE_FLAGS_RESET )) { +return -EINVAL ; + }This means QEMU vfio-pci breaks on any extension of the flags field.
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3395
Quoted 4 lines
hw/vfio/pci.c +4
-0+/* account for extra migration region */ +if (info->num_regions >VFIO_PCI_NUM_REGIONS +1 ) { +return -EINVAL ; + }This is also invalid, there can be device specific regions beyond migration.
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3398
Quoted 3 lines
hw/vfio/pci.c +3
-0+if (info->num_irqs >VFIO_PCI_NUM_IRQS ) { +return -EINVAL ; + }And device specific IRQs.
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3411
Quoted 13 lines
hw/vfio/pci.c +13
-0+return 0 ; +} + +static int vfio_pci_valid_region_info (VFIODevice *vbasedev, +struct vfio_region_info *info, +int *fd) +{ +if (info->flags & ~(VFIO_REGION_INFO_FLAG_READ | +VFIO_REGION_INFO_FLAG_WRITE | +VFIO_REGION_INFO_FLAG_MMAP | +VFIO_REGION_INFO_FLAG_CAPS )) { +return -EINVAL ; + }Similarly, this allows zero future extensions. Notice for instance how the CAPS flag was added later as a backwards compatible extension.
Quoted 12 lines
hw/vfio/pci.c +12
-0+if (info->index > vbasedev->num_regions) { +return -EINVAL ; + } +/* cap_offset in valid area */ +if ((info->flags &VFIO_REGION_INFO_FLAG_CAPS ) && + (info->cap_offset <sizeof (*info) || info->cap_offset > info->argsz)) { +return -EINVAL ; + } +return 0 ; +} + +static int vfio_pci_valid_irq_info(VFIODevice *vbasedev,
Quoted 9 lines
hw/vfio/pci.c static void register_vfio_pci_dev_type(void) · line 3429
Quoted 6 lines
hw/vfio/pci.c +6
-0+ struct vfio_irq_info *info) +{ +if (info->flags & ~(VFIO_IRQ_INFO_EVENTFD |VFIO_IRQ_INFO_MASKABLE | +VFIO_IRQ_INFO_AUTOMASKED |VFIO_IRQ_INFO_NORESIZE )) { +return -EINVAL ; + }Similarly, nak. Thanks,
The verification routines are to sanitize the data returned when we
don’t trust the server. The kernel driver return values are not verified, as the kernel has much better ways of corrupting the client. When new flags are added, the they’d be added here, too.
I can push them down into the the vfio-user code, but that forces
the vfio-code to change versions or add capabilities whenever a new flag is added.
JJ
Re: [RFC v3 06/19] vfio-user: Define type vfio_user_pci_dev_info
In reply to Alex WilliamsonQuoted 71 lines · Alex Williamson
Quoted 64 lines · On Mon, 8 Nov 2021 16:46:34 -0800 John Johnson <[email protected]>…
New class for vfio-user with its class and instance constructors and destructors, and its pci ops.
Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>---
hw/vfio/pci.h | 9 ++++++ hw/vfio/pci.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ hw/vfio/Kconfig | 10 ++++++ 3 files changed, 116 insertions(+)hw/vfio/pci.h +9
-0@@ -187,6 +187,15 @@ struct VFIOKernPCIDevice {VFIOPCIDevice device; }; +#define TYPE_VFIO_USER_PCI "vfio-user-pci" +OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) + +struct VFIOUserPCIDevice { +VFIOPCIDevice device; +char *sock_name; +bool secure_dma;/* disable shared mem for DMA */ +}; +/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ static inline bool vfio_pci_is (VFIOPCIDevice *vdev,uint32_t vendor,uint32_t device) {hw/vfio/pci.c +17
-0@@ -19,6 +19,7 @@*/ #include "qemu/osdep.h" +#include CONFIG_DEVICES #include <linux/vfio.h> #include <sys/ioctl.h> @@ -3438,3 +3439,99 @@ struct VFIOValidOps vfio_pci_valid_ops = { .validate_get_region_info = vfio_pci_valid_region_info, .validate_get_irq_info = vfio_pci_valid_irq_info, }; + + +#ifdef CONFIG_VFIO_USER_PCI + +/* +* vfio-user routines. +*/ + +/* +* Emulated devices don't use host hot reset +*/ +static int vfio_user_pci_no_reset (VFIODevice *vbasedev) +{ +error_printf ("vfio-user - no hot reset\n" ); +return 0 ; +}
Quoted 9 lines
hw/vfio/pci.c struct VFIOValidOps vfio_pci_valid_ops = { · line 3462
Quoted 5 lines
hw/vfio/pci.c +5
-0+ +static void vfio_user_pci_not_needed (VFIODevice *vbasedev) +{ + vbasedev->needs_reset = false; +}Seems like we should make some of these optional rather than stubbing dummy functions.
ok
Quoted 29 lines
Quoted 29 lines
hw/vfio/pci.c +29
-0+ +static VFIODeviceOps vfio_user_pci_ops = { + .vfio_compute_needs_reset = vfio_user_pci_not_needed, + .vfio_hot_reset_multi = vfio_user_pci_no_reset, + .vfio_eoi = vfio_intx_eoi, + .vfio_get_object = vfio_pci_get_object, + .vfio_save_config = vfio_pci_save_config, + .vfio_load_config = vfio_pci_load_config, +}; + +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; + +/* +* 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);
Quoted 9 lines
hw/vfio/pci.c struct VFIOValidOps vfio_pci_valid_ops = { · line 3495
Quoted 4 lines
hw/vfio/pci.c +4
-0+ vbasedev->dev =DEVICE (vdev); + vbasedev->fd =-1 ; + vbasedev->type =VFIO_DEVICE_TYPE_PCI ; + vbasedev->no_mmap = false;Why hard coded rather than a property? This is a useful debugging feature to be able to trap all device accesses. The device should work either way.
As I said in the earlier property reply, I thought these were
HW workarounds.
Quoted 6 lines
Quoted 6 lines
hw/vfio/pci.c +6
-0+ vbasedev->ops = &vfio_user_pci_ops; + vbasedev->valid_ops = &vfio_pci_valid_ops; + +} + +static void vfio_user_instance_finalize (Object *obj)
Quoted 9 lines
hw/vfio/pci.c struct VFIOValidOps vfio_pci_valid_ops = { · line 3507
Quoted 6 lines
hw/vfio/pci.c +6
-0+{ +} + +static Property vfio_user_pci_dev_properties[] = { +DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false),Add this when it means something. Thanks,
This can be moved to the path that uses ’secure_dma”
JJ
Re: [RFC v3 04/19] Add device IO ops vector
In reply to Alex WilliamsonQuoted 8 lines · Alex Williamson
Quoted 1 line · On Mon, 8 Nov 2021 16:46:32 -0800 John Johnson <[email protected]>…
Quoted 6 lines
include/hw/vfio/vfio-common.h struct VFIODeviceOps { · line 179
Quoted 2 lines
include/hw/vfio/vfio-common.h +2
-0+int (*region_write)(VFIODevice *vdev,uint8_t nr,off_t off,uint32_t size, +void *data,bool post);The @post arg is not really developed in this patch, it would make review easier to add the arg when it becomes implemented and used.
ok
Quoted 25 lines
hw/vfio/common.c int vfio_get_region_info(VFIODevice *vbasedev, int index, · line 2408
Quoted 13 lines
hw/vfio/common.c +8
-0@@ -2382,6 +2398,21 @@ int vfio_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; + +/* create region cache */ +if (vbasedev->regions ==NULL ) { + vbasedev->regions = g_new0(struct vfio_region_info *, + vbasedev->num_regions); + }Seems like this should have been part of
vfio_get_device()since that's where we setnum_regions.hw/vfio/common.c int vfio_get_region_info(VFIODevice *vbasedev, int index, · line 2415
Quoted 7 lines
hw/vfio/common.c +7
-0+/* check cache */ +if (vbasedev->regions[index] !=NULL ) { + *info =g_malloc0 (vbasedev->regions[index]->argsz); +memcpy (*info, vbasedev->regions[index], + vbasedev->regions[index]->argsz); +return 0 ; + }
Quoted 5 lines
Why are we calling
vfio_get_region_info()enough that we need a cache? This looks like an optimization for something that doesn't exist yet. And if we have a cache, why aren't callers using it directly rather than creating a copy for each caller?
The are a couple types of vfio_get_region_info() callers.
One type wants a specific region, which it then uses to fill in it’s own data structures (e.g., vfio_region_setup()) and the other type uses vfio_get_dev_region_info() to loop through all regions looking for a specific one (e.g., for the migration region or for a specifc device to setup its quirks) On top of that, each vfio_get_region_info() will need to call the server twice for regions with capabilities: once to get the proper ‘argsz’ and once to actually get the info.
I thought just having a cache that gets filled once, then
satisfies all later callers was a cleaner idea. It also solved the issue I had that the FD returned isn't used until the container is setup, which would’ve needed another vfio_get_region_info() call.
I can certainly move the cache fill to vfio_get_device()
and remove the copies if you like that idea better.
Quoted 16 lines
Quoted 16 lines
hw/vfio/common.c +0
-0*info =g_malloc0 (argsz);@@ -2389,19 +2420,28 @@ int vfio_get_region_info(VFIODevice *vbasedev, int index,retry:
(*info)->argsz = argsz;hw/vfio/common.c +3
-2-if (ioctl (vbasedev->fd,VFIO_DEVICE_GET_REGION_INFO , *info)) { + ret =VDEV_GET_REGION_INFO (vbasedev, *info, &fd); +if (ret !=0 ) {g_free (*info); *info =NULL ; -return -errno; +return ret; }
Quoted 10 lines
hw/vfio/common.c int vfio_get_region_info(VFIODevice *vbasedev, int index, · line 2436
Quoted 7 lines
if ((*info)->argsz > argsz) { argsz = (*info)->argsz; *info = g_realloc(*info, argsz);hw/vfio/common.c +4
-0+if (fd !=-1 ) { +close (fd); + fd =-1 ; + }Use of fd here is hard to review, it's clearly for some future use case.
The FD code can be moved to a patch where it’s used.
Quoted 18 lines
Quoted 5 lines
hw/vfio/common.c +4
-0+ +static int vfio_io_region_write (VFIODevice *vbasedev,uint8_t index,off_t off, +uint32_t size,void *data,bool post) +{hw/vfio/common.c int vfio_eeh_as_op(AddressSpace *as, uint32_t op) · line 2675
Quoted 10 lines
hw/vfio/common.c +10
-0+struct vfio_region_info *info = vbasedev->regions[index]; +int ret; + + ret =pwrite (vbasedev->fd, data, size, info->offset + off); +if (ret <0 ) { + ret = -errno; + } + +return ret; +}Simplify all these with ternaries?
Quoted 2 lines
return ret < 0 ? -errno : ret;
ok
Quoted 55 lines
Quoted 28 lines
static void
vfio_pci_size_rom(VFIOPCIDevice *vdev){hw/vfio/pci.c +8
-7+VFIODevice *vbasedev = &vdev->vbasedev;uint32_t orig, size =cpu_to_le32 ((uint32_t )PCI_ROM_ADDRESS_MASK ); -off_t offset = vdev->config_offset +PCI_ROM_ADDRESS ;DeviceState *dev =DEVICE (vdev);char *name; -int fd = vdev->vbasedev.fd;if (vdev->pdev.romfile || !vdev->pdev.rom_bar) {/* Since pci handles romfile, just print a message and return */ @@ -926,13 +928,23 @@ static void vfio_pci_size_rom(VFIOPCIDevice *vdev)* Use the same size ROM BAR as the physical device. The contents * will get filled in later when the guest tries to read it. */ -if (pread (fd, &orig,4 , offset) !=4 || -pwrite (fd, &size,4 , offset) !=4 || -pread (fd, &size,4 , offset) !=4 || -pwrite (fd, &orig,4 , offset) !=4 ) { -error_report ("%s(%s) failed: %m" , __func__, vdev->vbasedev.name); +#define rom_read (var) VDEV_REGION_READ(vbasedev, \ + VFIO_PCI_CONFIG_REGION_INDEX, \ + PCI_ROM_ADDRESS, 4, (var)) +#define rom_write (var) VDEV_REGION_WRITE(vbasedev, \ + VFIO_PCI_CONFIG_REGION_INDEX, \ + PCI_ROM_ADDRESS, 4, (var), false) +hw/vfio/pci.c static void vfio_pci_size_rom(VFIOPCIDevice *vdev) · line 947
Quoted 10 lines
hw/vfio/pci.c +8
-0+if (rom_read (&orig) !=4 || +rom_write (&size) !=4 || +rom_read (&size) !=4 || +rom_write (&orig) !=4 ) { + +error_report ("%s(%s) ROM access failed" , __func__, vbasedev->name);return ; } +#undef rom_read +#undef rom_writeHmm, I think I'd rather see a generic function broken out for this, maybe:
int vfio_pci_size_bar32(VFIODevice *vbasedev, int offset,
uint32_t mask, uint32_t *size) { uint32_t orig, val; int index = VFIO_PCI_CONFIG_REGION_INDEX; if (VDEV_REGION_READ(vbasedev, index, offset, 4, &orig) != 4 || VDEV_REGION_WRITE(vbasedev, index, offset, 4, &mask, false) != 4 || VDEV_REGION_READ(vbasedev, index, offset, 4, &val) != 4 || VDEV_REGION_WRITE(vbasedev, index, offset, 4, &orig, false) != 4) { error_report(...
Quoted 8 lines
return -1; } *size = ~(le32_to_cpu(val) & PCI_ROM_ADDRESS_MASK) + 1; return 0; }
ok
Quoted 9 lines
Really I think it's just trying to hard code
VFIO_PCI_CONFIG_REGION_INDEXandPCI_ROM_ADDRESSinto the macro calls that demand some simplification, if we use variables it looks fine inline.We could also re-wrap VDEV_REGION_READ/WRITE in
vfio/pci.hto simplify config space access, presumably we'd never have posted writes to config space. Thanks,
There are multiple places where config space is read or written
in pci.c, so adding an inline or #define for it is a good idea.
JJ
Re: [RFC v3 13/19] vfio-user: pci_user_realize PCI setup
In reply to Alex WilliamsonQuoted 17 lines · Alex Williamson
Quoted 10 lines · On Mon, 8 Nov 2021 16:46:41 -0800 John Johnson <[email protected]>…
PCI BARs read from remote device
PCI config reads/writes sent to remote serverSigned-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>---
hw/vfio/pci.c| 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-1 file changed, 88 insertions(+), 1 deletion(-)
Quoted 12 lines
hw/vfio/pci.c static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) · line 3553
Quoted 8 lines
hw/vfio/pci.c +0
-0@@ -3551,8 +3551,93 @@ static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) goto error; }There's a LOT of duplication with the kernel realize function here. Thanks,
When I add more common properties, there will be even more
duplication, so I’ll add a vfio_realize_common() routine.
JJ
Re: [RFC v3 07/19] vfio-user: connect vfio proxy to remote server
In reply to Alex WilliamsonQuoted 16 lines · Alex Williamson
hw/vfio/pci.c static void vfio_user_pci_realize(PCIDevice *pdev, Error **errp) · line 3520
Quoted 8 lines · On Mon, 8 Nov 2021 16:46:35 -0800 John Johnson <[email protected]>…
static void
vfio_user_instance_finalize(Object *obj){hw/vfio/pci.c +4
-0+VFIOPCIDevice *vdev =VFIO_PCI_BASE (obj); +VFIODevice *vbasedev = &vdev->vbasedev; + +vfio_put_device (vdev);
Quoted 3 lines
This looks suspiciously like the initial function in the previous patch should not have been empty. Thanks,
It can go in the other patch
JJ
Re: [RFC v3 12/19] vfio-user: region read/write
In reply to Alex WilliamsonQuoted 8 lines · Alex Williamson
Quoted 1 line · On Mon, 8 Nov 2021 16:46:40 -0800 John Johnson <[email protected]>…
Quoted 17 lines
include/hw/vfio/vfio-common.h typedef struct VFIORegion { · line 59
Quoted 11 lines
VFIOProxy *vfio_user_connect_dev(SocketAddress *addr, Error **errp); void
vfio_user_disconnect(VFIOProxy *proxy);include/hw/vfio/vfio-common.h +1
-0@@ -56,6 +56,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 */ As with the fd in the previous patch, this is where the concept of posted writes should be introduced throughout. Or maybe even better would be to introduce write support without posting and the next patch could expose posted writes. Thanks,
I can place it with the region write ops patch where it’s used.
JJ
Re: [RFC v3 08/19] vfio-user: define socket receive functions
In reply to Alex WilliamsonQuoted 22 lines · Alex Williamson
Quoted 15 lines · On Mon, 8 Nov 2021 16:46:36 -0800 John Johnson <[email protected]>…
Add infrastructure needed to receive incoming messages
Signed-off-by John G Johnson <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>---
hw/vfio/pci.h | 2 +- hw/vfio/user-protocol.h | 62 +++++++++ hw/vfio/user.h | 9 +- hw/vfio/pci.c | 12 +- hw/vfio/user.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++++ MAINTAINERS | 1 + 6 files changed, 409 insertions(+), 3 deletions(-)create mode 100644
hw/vfio/user-protocol.h
Quoted 12 lines
hw/vfio/pci.h OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) · line 196
Quoted 9 lines
hw/vfio/pci.h +0
-1@@ -193,7 +193,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name; -bool secure_dma;/* disable shared mem for DMA */ Don't introduce it into the series to start with, confusing to review.
ok
Quoted 30 lines
Quoted 30 lines
hw/vfio/pci.h +1
-0+bool send_queued;/* all sends are queued */ };/* Use uin32_t for vendor & device so PCI_ANY_ID expands and cannot match hw */ hw/vfio/user-protocol.h new file +20
-0@@ -0,0 +1,62 @@ +#ifndef VFIO_USER_PROTOCOL_H +#define VFIO_USER_PROTOCOL_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. + * + * Each message has a standard header that describes the command + * being sent, which is almost always a VFIO ioctl(). + * + * The header may be followed by command-specific data, such as the + * region and offset info for read and write commands. + */ + +typedef struct { + uint16_t id;
Quoted 9 lines
hw/vfio/user-protocol.h line 26
Quoted 6 lines
hw/vfio/user-protocol.h new file +6
-0+uint16_t command; +uint32_t size; +uint32_t flags; +uint32_t error_reply; +} VFIOUserHdr; +A comment referencing the doc would probably be a good idea about here.
ok
Quoted 32 lines
Quoted 32 lines
hw/vfio/user-protocol.h new file +32
-0+/* VFIOUserHdr commands */ +enum vfio_user_command { +VFIO_USER_VERSION =1 , +VFIO_USER_DMA_MAP =2 , +VFIO_USER_DMA_UNMAP =3 , +VFIO_USER_DEVICE_GET_INFO =4 , +VFIO_USER_DEVICE_GET_REGION_INFO =5 , +VFIO_USER_DEVICE_GET_REGION_IO_FDS =6 , +VFIO_USER_DEVICE_GET_IRQ_INFO =7 , +VFIO_USER_DEVICE_SET_IRQS =8 , +VFIO_USER_REGION_READ =9 , +VFIO_USER_REGION_WRITE =10 , +VFIO_USER_DMA_READ =11 , +VFIO_USER_DMA_WRITE =12 , +VFIO_USER_DEVICE_RESET =13 , +VFIO_USER_DIRTY_PAGES =14 , +VFIO_USER_MAX , +}; + +/* VFIOUserHdr flags */ +#define VFIO_USER_REQUEST 0x0 +#define VFIO_USER_REPLY 0x1 +#define VFIO_USER_TYPE 0xF + +#define VFIO_USER_NO_REPLY 0x10 +#define VFIO_USER_ERROR 0x20 + + +#define VFIO_USER_DEF_MAX_FDS 8 +#define VFIO_USER_MAX_MAX_FDS 16 + +#define VFIO_USER_DEF_MAX_XFER (1024 * 1024)
Quoted 9 lines
hw/vfio/user-protocol.h line 59
Quoted 1 line
hw/vfio/user-protocol.h new file +1
-0+#define VFIO_USER_MAX_MAX_XFER (64 * 1024 * 1024)These are essentially magic numbers, some discussion of how these limits are derived would be useful for future contributors, but also only
DEV_MAX_XFERis used in this patch and it's confusing why the macro isn't used directly. Most of the logic surrounding these is added in the next patch, so it doesn't really make sense to add them here. Thanks,
ok
JJ
Re: [RFC v3 16/19] vfio-user: dma map/unmap operations
In reply to Alex WilliamsonQuoted 20 lines · Alex Williamson
Quoted 13 lines · On Mon, 8 Nov 2021 16:46:44 -0800 John Johnson <[email protected]>…
Signed-off-by Jagannathan Raman <[email protected]>Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>---
hw/vfio/pci.h | 1 + hw/vfio/user-protocol.h | 32 +++++++ hw/vfio/user.h | 1 + include/hw/vfio/vfio-common.h | 4 + hw/vfio/common.c | 76 +++++++++++++--- hw/vfio/pci.c | 4 + hw/vfio/user.c | 206 ++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 309 insertions(+), 15 deletions(-)
Quoted 12 lines
hw/vfio/pci.h OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI) · line 196
Quoted 9 lines
hw/vfio/pci.h +1
-0@@ -193,6 +193,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(VFIOUserPCIDevice, VFIO_USER_PCI)struct VFIOUserPCIDevice {VFIOPCIDevice device;char *sock_name; +bool secure_dma;/* disable shared mem for DMA */ ???????????? It's there, it's gone, it's back.
This was a merge mistake
Quoted 15 lines
include/hw/vfio/vfio-common.h typedef struct VFIOContainer { · line 93
Quoted 11 lines
include/hw/vfio/vfio-common.h +1
-0@@ -90,6 +90,8 @@ typedef struct VFIOContainer {VFIOContIO *io_ops;bool initialized;bool dirty_pages_supported; +bool will_commit;The entire
will_commitconcept hidden in the map and unmap operations from many patches ago should be introduced here, or later.
ok
Quoted 52 lines
Quoted 52 lines
include/hw/vfio/vfio-common.h +3
-0+bool need_map_fd;uint64_t dirty_pgsizes;uint64_t max_dirty_bitmap_size;unsigned long pgsizes;@@ -210,6 +212,7 @@ struct VFIOContIO {int (*dirty_bitmap)(VFIOContainer *container,struct vfio_iommu_type1_dirty_bitmap *bitmap,struct vfio_iommu_type1_dirty_bitmap_get *range); +void (*wait_commit)(VFIOContainer *container); };#define CONT_DMA_MAP (cont, map, fd, will_commit) \@@ -218,6 +221,7 @@ struct VFIOContIO { ((cont)->io_ops->dma_unmap((cont), (unmap), (bitmap), (will_commit)))#define CONT_DIRTY_BITMAP (cont, bitmap, range) \ ((cont)->io_ops->dirty_bitmap((cont), (bitmap), (range))) +#define CONT_WAIT_COMMIT (cont) ((cont)->io_ops->wait_commit(cont))extern VFIODevIO vfio_dev_io_ioctl;extern VFIOContIO vfio_cont_io_ioctl;hw/vfio/common.c +3
-1@@ -411,6 +411,7 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container,struct vfio_iommu_type1_dma_unmap *unmap;struct vfio_bitmap *bitmap;uint64_t pages =REAL_HOST_PAGE_ALIGN (size) / qemu_real_host_page_size; +bool will_commit = container->will_commit;int ret; unmap =g_malloc0 (sizeof (*unmap) +sizeof (*bitmap));@@ -444,7 +445,7 @@ static int vfio_dma_unmap_bitmap(VFIOContainer *container, goto unmap_exit; } - ret =CONT_DMA_UNMAP (container, unmap, bitmap, false); + ret =CONT_DMA_UNMAP (container, unmap, bitmap, will_commit);if (!ret) {cpu_physical_memory_set_dirty_lebitmap ((unsigned long *)bitmap->data, iotlb->translated_addr, pages);@@ -471,16 +472,17 @@ static int vfio_dma_unmap(VFIOContainer *container, .iova = iova, .size = size, }; +bool will_commit = container->will_commit;if (iotlb && container->dirty_pages_supported &&vfio_devices_all_running_and_saving (container)) {return vfio_dma_unmap_bitmap (container, iova, size, iotlb); }
Quoted 6 lines
hw/vfio/common.c static int vfio_dma_unmap(VFIOContainer *container, · line 482
Quoted 2 lines
hw/vfio/common.c +1
-1-return CONT_DMA_UNMAP (container, &unmap,NULL , false); +return CONT_DMA_UNMAP (container, &unmap,NULL , will_commit);We're passing the container, why do we need a separate
will_commitarg for these?
ok
Quoted 29 lines
Quoted 29 lines
hw/vfio/common.c +11
-3} -static int vfio_dma_map (VFIOContainer *container,hwaddr iova, +static int vfio_dma_map (VFIOContainer *container,MemoryRegion *mr,hwaddr iova,ram_addr_t size,void *vaddr,bool readonly) {struct vfio_iommu_type1_dma_map map = {@@ -490,13 +492,23 @@ static int vfio_dma_map(VFIOContainer *container, hwaddr iova, .iova = iova, .size = size, }; -int ret; +int fd, ret; +bool will_commit = container->will_commit;if (!readonly) { map.flags |=VFIO_DMA_MAP_FLAG_WRITE ; } - ret =CONT_DMA_MAP (container, &map,-1 , false); +if (container->need_map_fd) { + fd =memory_region_get_fd (mr); +if (fd !=-1 ) { + map.vaddr =qemu_ram_block_host_offset (mr->ram_block, vaddr); + } + }else { + fd =-1 ; + }
Quoted 9 lines
hw/vfio/common.c static int vfio_dma_map(VFIOContainer *container, hwaddr iova, · line 511
Quoted 2 lines
hw/vfio/common.c +2
-0+ + ret =CONT_DMA_MAP (container, &map, fd, will_commit);Why were we even passing a -1 fd previously? Would it make more sense to pass the mr and put this in the user variant .map_dma? We're going to the trouble to pass the mr down this far here. If the map callback handled the above fd and map.vaddr we could also avoid of the
need_map_fdflag on the container.
ok
Quoted 13 lines
Quoted 13 lines
hw/vfio/common.c +11
-0+static void vfio_listener_begin (MemoryListener *listener) +{ +VFIOContainer *container =container_of (listener, VFIOContainer, listener); + +/* cannot drop BQL during the transaction, send maps/demaps async */ + container->will_commit = true; +} + +static void vfio_listener_commit (MemoryListener *listener) +{ +VFIOContainer *container =container_of (listener, VFIOContainer, listener);
Quoted 9 lines
hw/vfio/common.c static void vfio_unregister_ram_discard_listener(VFIOContainer *container, · line 879
Quoted 5 lines
hw/vfio/common.c +5
-0+ +/* wait for any async requests sent during the transaction */ +CONT_WAIT_COMMIT (container); + container->will_commit = false; +}Not sure I follow the semantics, when would the map/unmap callbacks get called when
will_commitis false?
If map/unmap is called outside a transaction, it can drop BQL and
wait. For unmap/map inside a transaction, they're sent async, and waited for after the commit (when it’s safe to drop BQL while we wait)
Quoted 3 lines
Does it really make sense to have macros for ops that we call in one place?
I did it this way so all container ops calls are done the same way.
Quoted 6 lines
Quoted 6 lines
\
static const MemoryListenervfio_memory_listener= {hw/vfio/common.c +2
-0+ .begin = vfio_listener_begin, + .commit = vfio_listener_commit, .region_add = vfio_listener_region_add, .region_del = vfio_listener_region_del,
Quoted 9 lines
hw/vfio/common.c int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, · line 1601
Quoted 6 lines
.log_global_start =
vfio_listener_log_global_start,hw/vfio/common.c +1
-0@@ -1561,6 +1598,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;Should this be in a different patch? It looks unrelated.
I think you said as much in the patch that introduced the region write
ops call.
Quoted 23 lines
Quoted 23 lines
hw/vfio/common.c +2
-0region->remfd =vfio_get_region_info_remfd (vbasedev, index);if (region->size) {@@ -2047,6 +2085,7 @@ static int vfio_connect_container(VFIOGroup *group, AddressSpace *as, container->dirty_pages_supported = false; container->dma_max_mappings =0 ; container->io_ops = &vfio_cont_io_ioctl; + container->need_map_fd = false;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list);QLIST_INIT (&container->vrdl_list);@@ -2230,6 +2269,7 @@ void vfio_connect_proxy(VFIOProxy *proxy, VFIOGroup *group, AddressSpace *as) container->space = space; container->fd =-1 ; container->io_ops = &vfio_cont_io_sock; + container->need_map_fd = (proxy->flags &VFIO_PROXY_SECURE ) ==0 ;QLIST_INIT (&container->giommu_list);QLIST_INIT (&container->hostwin_list); container->proxy = proxy;@@ -2879,8 +2919,14 @@ static int vfio_io_dirty_bitmap(VFIOContainer *container,return ret; }
Quoted 9 lines
hw/vfio/common.c static int vfio_io_dirty_bitmap(VFIOContainer *container, · line 2926
Quoted 5 lines
hw/vfio/common.c +5
-0+static void vfio_io_wait_commit (VFIOContainer *container) +{ +/* ioctl()s are synchronous */ +} +Maybe these should just be "
dma_commit" rather than "wait_commit"? I'd also tend to suggest "async" rather than "will_commit".
ok
Quoted 22 lines
Quoted 22 lines
hw/vfio/common.c +1
-0VFIOContIO vfio_cont_io_ioctl = { .dma_map = vfio_io_dma_map, .dma_unmap = vfio_io_dma_unmap, .dirty_bitmap = vfio_io_dirty_bitmap, + .wait_commit = vfio_io_wait_commit, };hw/vfio/pci.c +3
-0@@ -3516,6 +3516,9 @@ 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->secure_dma) { + proxy->flags |=VFIO_PROXY_SECURE ; + }if (udev->send_queued) { proxy->flags |=VFIO_PROXY_FORCE_QUEUED ; }@@ -3686,6 +3689,7 @@ static void vfio_user_instance_finalize(Object *obj)
Quoted 7 lines
hw/vfio/pci.c static void vfio_user_instance_finalize(Object *obj) · line 3692
Quoted 3 lines
hw/vfio/pci.c +1
-0static Property vfio_user_pci_dev_properties[] = {DEFINE_PROP_STRING ("socket" , VFIOUserPCIDevice, sock_name), +DEFINE_PROP_BOOL ("secure-dma" , VFIOUserPCIDevice, secure_dma, false),"
secure_dma" looks entirely compartmentalized that it could be a separate patch. Thanks,
ok
JJ
Re: [RFC v3 11/19] vfio-user: get region info
In reply to Alex WilliamsonQuoted 101 lines · Alex Williamson
Quoted 71 lines · On Mon, 8 Nov 2021 16:46:39 -0800 John Johnson <[email protected]>…
Signed-off-by Elena Ufimtseva <[email protected]>Signed-off-by John G Johnson <[email protected]>Signed-off-by Jagannathan Raman <[email protected]>---
hw/vfio/user-protocol.h | 14 ++++++++++++ include/hw/vfio/vfio-common.h | 4 +++- hw/vfio/common.c | 30 +++++++++++++++++++++++++- hw/vfio/user.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 2 deletions(-)hw/vfio/user-protocol.h +14
-0@@ -95,4 +95,18 @@ typedef struct { uint32_t cap_offset; } 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 */ include/hw/vfio/vfio-common.h +3
-1@@ -56,6 +56,7 @@ typedef struct VFIORegion {uint32_t nr_mmaps;VFIOMmap *mmaps;uint8_t nr;/* cache the region number for debug */ +int remfd;/* fd if exported from remote process */ } VFIORegion; typedef struct VFIOMigration {@@ -150,8 +151,9 @@ typedef struct VFIODevice {VFIOMigration *migration;Error *migration_blocker;OnOffAuto pre_copy_dirty_page_tracking; -struct vfio_region_info **regions;VFIOProxy *proxy; +struct vfio_region_info **regions; +int *regfds; } VFIODevice;struct VFIODeviceOps {hw/vfio/common.c +1
-0@@ -40,6 +40,7 @@#include "trace.h" #include "qapi/error.h" #include "migration/migration.h" +#include "hw/vfio/user.h" VFIOGroupList
vfio_group_list=QLIST_HEAD_INITIALIZER(vfio_group_list);hw/vfio/common.c +0
-0@@ -1491,6 +1492,16 @@ bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info,return true; }hw/vfio/common.c bool vfio_get_info_dma_avail(struct vfio_iommu_type1_info *info, · line 1503
Quoted 10 lines
hw/vfio/common.c +9
-0+static int vfio_get_region_info_remfd (VFIODevice *vbasedev,int index) +{ +struct vfio_region_info *info; + +if (vbasedev->regions ==NULL || vbasedev->regions[index] ==NULL ) { +vfio_get_region_info (vbasedev, index, &info); + } +return vbasedev->regfds !=NULL ? vbasedev->regfds[index] :-1 ; +}This patch is really obscure due to the region info fd being added many patches ago and only now being used.
Do we really want a parallel array to regions for storing these fds?
Why do we call an array of these fds "regfds" but a single one "remfd"?
Ugh, why do we have both the regfds array and a remfd per VFIORegion?
TBH, I'm still not sure why we're caching region infos at all, this seems to be gratuitously bloated.
Quoted 13 lines
hw/vfio/common.c int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, · line 1558
Quoted 9 lines
hw/vfio/common.c +2
-0+static int vfio_setup_region_sparse_mmaps (VFIORegion *region,struct vfio_region_info *info) {@@ -1544,6 +1555,7 @@ int vfio_region_setup(Object *obj, VFIODevice *vbasedev, VFIORegion *region, region->size = info->size; region->fd_offset = info->offset; region->nr = index; + region->remfd =vfio_get_region_info_remfd (vbasedev, index);Why didn't we just get an fd back from
vfio_get_region_info()that we could use here?
As I said in the patch with the cache changes, the purpose of
the cache was to fill it once, and have all the other callers who want region info be satisfied from the cache. This includes the FD used to map device memory exported by the server.
Quoted 15 lines
Quoted 15 lines
hw/vfio/common.c +1
-0if (region->size) { region->mem =g_new0 (MemoryRegion,1 );@@ -1587,6 +1599,7 @@ int vfio_region_mmap(VFIORegion *region) {int i, prot =0 ;char *name; +int fd;if (!region->mem) {return 0 ;@@ -1595,9 +1608,11 @@ int vfio_region_mmap(VFIORegion *region) prot |= region->flags &VFIO_REGION_INFO_FLAG_READ ?PROT_READ :0 ; prot |= region->flags &VFIO_REGION_INFO_FLAG_WRITE ?PROT_WRITE :0 ;
Quoted 5 lines
hw/vfio/common.c int vfio_region_mmap(VFIORegion *region) · line 1611
Quoted 1 line
hw/vfio/common.c +1
-0+ fd = region->remfd !=-1 ? region->remfd : region->vbasedev->fd;Gack, why can't VFIORegion.fd be either the remote process fd or the vbasedevfd to avoid all these switches? Thanks,
That’s a good idea. The container FD can be the vbasedev FD
is the kernel case, and the exported FD in vfio-user.
JJ
Re: [RFC v3 19/19] vfio-user: migration support
In reply to Alex WilliamsonQuoted 10 lines · Alex Williamson
Quoted 1 line · On Mon, 8 Nov 2021 16:46:47 -0800 John Johnson <[email protected]>…
bug fix: only set qemu file error if there is a file
Quoted 8 lines
I don't understand this commit log. Is this meant to be a revision log? In general it would be nice to have more detailed commit logs for many of these patches describing any nuances.
Note that the migration uAPI is being reevaluated on the kernel side, so I expect we'll want to hold off on vfio-user beyond minimal stub support. Thanks,
It was a note that this was a bug found in the vfio code,
and not a vfio-user addition. I can add more commentary to the patches.
JJ