virtio-net: add support for SR-IOV emulation
v2 cover virtio-net: add support for SR-IOV emulation
Introduction
------------
This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------
A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o
-netdev user,id=p -netdev user,id=q
-device pcie-root-port,id=b
-device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=f
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
Keeping VF instances
--------------------
A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
As an strategy alternative to runtime realization/unrealization, this series proposes to reuse the code to power down PCI Express devices. When a PCI Express device is powered down, it will be hidden from the guest but will be kept realized. This effectively implements the behavior we need for the SR-IOV emulation.
Summary
-------
Patch [1, 5] refactors the PCI infrastructure code. Patch [6, 10] adds user-created SR-IOV VF infrastructure. Patch 11 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 12 allows user to create SR-IOV VFs with virtio-net-pci.
[1] https://patchew.org/QEMU/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/
---
Changes in v2:
- Changed to keep VF instances.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Akihiko Odaki (12):
hw/pci: Initialize PCI multifunction after realization hw/pci: Determine if rombar is explicitly enabled hw/pci: Do not add ROM BAR for SR-IOV VF
vfio: Avoid inspecting option QDict for rombar hw/qdev: Remove opts member
pcie_sriov: Reuse SR-IOV VF device instances
pcie_sriov: Release VFs failed to realize
pcie_sriov: Ensure PF and VF are mutually exclusive
pcie_sriov: Check PCI Express for SR-IOV PF
pcie_sriov: Allow user to create SR-IOV device
virtio-pci: Implement SR-IOV PF
virtio-net: Implement SR-IOV VF
docs/pcie_sriov.txt | 8 +- include/hw/pci/pci.h | 2 +- include/hw/pci/pci_device.h | 13 +- include/hw/pci/pcie_sriov.h | 25 ++- include/hw/qdev-core.h | 4 - hw/core/qdev.c | 1 - hw/net/igb.c | 3 +- hw/nvme/ctrl.c | 3 +- hw/pci/pci.c | 98 +++++++----- hw/pci/pci_host.c | 4 +- hw/pci/pcie.c | 4 +- hw/pci/pcie_sriov.c | 360 +++++++++++++++++++++++++++++++++----------- hw/vfio/pci.c | 3 +- hw/virtio/virtio-net-pci.c | 1 + hw/virtio/virtio-pci.c | 7 + system/qdev-monitor.c | 12 +- 16 files changed, 395 insertions(+), 153 deletions(-)
---
base-commit: 4705fc0c8511d073bee4751c3c974aab2b10a970
change-id: 20231202-sriov-9402fb262be8
Best regards,
Signature · 1 line
Akihiko Odaki <[email protected]>
RFC PATCH v2 1/12 hw/pci: Initialize PCI multifunction after realization
The device realization code may enable PCI multifunction for SR-IOV.
--- hw/pci/pci.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
hw/pci/pci.c +9 -8
@@ -940,8 +940,9 @@ static void pci_init_mask_bridge(PCIDevice *d) PCI_PREF_RANGE_TYPE_MASK); } -static void pci_init_multifunction (PCIBus *bus,PCIDevice *dev,Error **errp) +static void pci_init_multifunction (PCIDevice *dev,Error **errp) { +PCIBus *bus =pci_get_bus (dev);uint8_t slot =PCI_SLOT (dev->devfn);uint8_t func;
@@ -1140,7 +1141,6 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,PCIDeviceClass *pc =PCI_DEVICE_GET_CLASS (pci_dev);PCIConfigReadFunc *config_read = pc->config_read;PCIConfigWriteFunc *config_write = pc->config_write; -Error *local_err =NULL ;DeviceState *dev =DEVICE (pci_dev);PCIBus *bus =pci_get_bus (pci_dev);bool is_bridge =IS_PCI_BRIDGE (pci_dev);
@@ -1233,12 +1233,6 @@ static PCIDevice *do_pci_register_device(PCIDevice *pci_dev,if (is_bridge) {pci_init_mask_bridge (pci_dev); } -pci_init_multifunction (bus, pci_dev, &local_err); -if (local_err) { -error_propagate (errp, local_err); -do_pci_unregister_device (pci_dev); -return NULL ; - }if (!config_read) config_read = pci_default_read_config;
@@ -2122,6 +2116,13 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp) } } +pci_init_multifunction (pci_dev, &local_err); +if (local_err) { +error_propagate (errp, local_err); +pci_qdev_unrealize (DEVICE (pci_dev)); +return ; + } +/* * A PCIe Downstream Port that do not have ARI Forwarding enabled must * associate only Device 0 with the device attached to the bus
Signature · 1 line
2.43.0
Re: [PATCH RFC v2 01/12] hw/pci: Initialize PCI multifunction after realization
Quoted 6 lines · Akihiko Odaki
The device realization code may enable PCI multifunction for SR-IOV.
Signed-off-by Akihiko Odaki <[email protected]>--- hw/pci/pci.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
RFC PATCH v2 2/12 hw/pci: Determine if rombar is explicitly enabled
In reply to Akihiko Odakivfio determines if rombar is explicitly enabled by inspecting QDict. Inspecting QDict is not nice because QDict is untyped and depends on the details on the external interface. Add an infrastructure to determine if rombar is explicitly enabled to hw/pci. PCIDevice::rom_bar is changed to have -1 by the default to tell rombar is explicitly enabled. It is consistent with other properties like addr and romsize.
--- include/hw/pci/pci_device.h | 5 +++++ hw/pci/pci.c | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-)
include/hw/pci/pci_device.h +5 -0
@@ -205,6 +205,11 @@ static inline uint16_t pci_get_bdf(PCIDevice *dev)return PCI_BUILD_BDF (pci_bus_num (pci_get_bus (dev)), dev->devfn); } +static inline bool pci_rom_bar_explicitly_enabled (PCIDevice *dev) +{ +return dev->rom_bar && dev->rom_bar !=-1 ; +} +uint16_t pci_requester_id (PCIDevice *dev);/* DMA access functions */
hw/pci/pci.c +1 -1
@@ -71,7 +71,7 @@ static Property pci_props[] = {DEFINE_PROP_PCI_DEVFN ("addr" , PCIDevice, devfn,-1 ),DEFINE_PROP_STRING ("romfile" , PCIDevice, romfile),DEFINE_PROP_UINT32 ("romsize" , PCIDevice, romsize,-1 ), -DEFINE_PROP_UINT32 ("rombar" , PCIDevice, rom_bar,1 ), +DEFINE_PROP_UINT32 ("rombar" , PCIDevice, rom_bar,-1 ),DEFINE_PROP_BIT ("multifunction" , PCIDevice, cap_present,QEMU_PCI_CAP_MULTIFUNCTION_BITNR , false), DEFINE_PROP_BIT("x-pcie-lnksta-dllla", PCIDevice, cap_present,
Signature · 1 line
2.43.0
RFC PATCH v2 3/12 hw/pci: Do not add ROM BAR for SR-IOV VF
In reply to Akihiko OdakiA SR-IOV VF cannot have a ROM BAR.
--- hw/pci/pci.c | 8 ++++++++ 1 file changed, 8 insertions(+)
hw/pci/pci.c +8 -0
@@ -2377,6 +2377,14 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,return ; } +if (pci_is_vf (pdev)) { +if (pdev->rom_bar && pdev->rom_bar !=-1 ) { +error_setg (errp,"ROM BAR cannot be enabled for SR-IOV VF" ); + } + +return ; + } +if (load_file || pdev->romsize ==-1 ) { path =qemu_find_file (QEMU_FILE_TYPE_BIOS , pdev->romfile);if (path ==NULL ) {
Signature · 1 line
2.43.0
RFC PATCH v2 4/12 vfio: Avoid inspecting option QDict for rombar
In reply to Akihiko OdakiUse pci_rom_bar_explicitly_enabled() to determine if rombar is explicitly enabled.
--- hw/vfio/pci.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
hw/vfio/pci.c +1 -2
@@ -1008,7 +1008,6 @@ static void vfio_pci_size_rom(VFIOPCIDevice *vdev) {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;
@@ -1042,7 +1041,7 @@ static void vfio_pci_size_rom(VFIOPCIDevice *vdev) }if (vfio_opt_rom_in_denylist (vdev)) { -if (dev->opts &&qdict_haskey (dev->opts,"rombar" )) { +if (pci_rom_bar_explicitly_enabled (&vdev->pdev)) {warn_report ("Device at %s is known to cause system instability" " issues during option rom execution" , vdev->vbasedev.name);
Signature · 1 line
2.43.0
RFC PATCH v2 5/12 hw/qdev: Remove opts member
In reply to Akihiko OdakiIt is no longer used.
--- include/hw/qdev-core.h | 4 ---- hw/core/qdev.c | 1 - system/qdev-monitor.c | 12 +++++++----- 3 files changed, 7 insertions(+), 10 deletions(-)
include/hw/qdev-core.h +0 -4
@@ -237,10 +237,6 @@ struct DeviceState {* @pending_deleted_expires_ms: optional timeout for deletion events */ int64_t pending_deleted_expires_ms; -/** -* @opts: QDict of options for the device -*/ -QDict *opts;/** * @hotplugged: was device added after PHASE_MACHINE_READY? */
hw/core/qdev.c +0 -1
@@ -706,7 +706,6 @@ static void device_finalize(Object *obj) dev->canonical_path =NULL ; } -qobject_unref (dev->opts);g_free (dev->id); }
system/qdev-monitor.c +7 -5
@@ -625,6 +625,7 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,char *id;DeviceState *dev =NULL ;BusState *bus =NULL ; +QDict *properties; driver =qdict_get_try_str (opts,"driver" );if (!driver) {
@@ -705,13 +706,14 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts, }/* set properties */ - dev->opts =qdict_clone_shallow (opts); -qdict_del (dev->opts,"driver" ); -qdict_del (dev->opts,"bus" ); -qdict_del (dev->opts,"id" ); + properties =qdict_clone_shallow (opts); +qdict_del (properties,"driver" ); +qdict_del (properties,"bus" ); +qdict_del (properties,"id" ); -object_set_properties_from_keyval (&dev->parent_obj, dev->opts, from_json, +object_set_properties_from_keyval (&dev->parent_obj, properties, from_json, errp); +qobject_unref (properties);if (*errp) { goto err_del_dev; }
Signature · 1 line
2.43.0
Re: [PATCH RFC v2 05/12] hw/qdev: Remove opts member
Quoted 1 line · Akihiko Odaki
It is no longer used.
Since commit f3558b1b76 ("qdev: Base object creation on QDict rather than QemuOpts")?
Quoted 67 lines
Signed-off-by Akihiko Odaki <[email protected]>--- include/hw/qdev-core.h | 4 ---- hw/core/qdev.c | 1 - system/qdev-monitor.c | 12 +++++++----- 3 files changed, 7 insertions(+), 10 deletions(-)include/hw/qdev-core.h +0
-4@@ -237,10 +237,6 @@ struct DeviceState {* @pending_deleted_expires_ms: optional timeout for deletion events */ int64_t pending_deleted_expires_ms; -/** -* @opts: QDict of options for the device -*/ -QDict *opts;/** * @hotplugged: was device added after PHASE_MACHINE_READY? */ hw/core/qdev.c +0
-1@@ -706,7 +706,6 @@ static void device_finalize(Object *obj) dev->canonical_path =NULL ; } -qobject_unref (dev->opts);g_free (dev->id); }system/qdev-monitor.c +7
-5@@ -625,6 +625,7 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts,char *id;DeviceState *dev =NULL ;BusState *bus =NULL ; +QDict *properties; driver =qdict_get_try_str (opts,"driver" );if (!driver) {@@ -705,13 +706,14 @@ DeviceState *qdev_device_add_from_qdict(const QDict *opts, }/* set properties */ - dev->opts =qdict_clone_shallow (opts); -qdict_del (dev->opts,"driver" ); -qdict_del (dev->opts,"bus" ); -qdict_del (dev->opts,"id" ); + properties =qdict_clone_shallow (opts); +qdict_del (properties,"driver" ); +qdict_del (properties,"bus" ); +qdict_del (properties,"id" ); -object_set_properties_from_keyval (&dev->parent_obj, dev->opts, from_json, +object_set_properties_from_keyval (&dev->parent_obj, properties, from_json, errp); +qobject_unref (properties);if (*errp) { goto err_del_dev; }
Re: [PATCH RFC v2 05/12] hw/qdev: Remove opts member
Quoted 5 lines · Philippe Mathieu-Daudé
Quoted 1 line · Akihiko Odaki
It is no longer used.
Since commit
f3558b1b76("qdev: Base object creation on QDict rather than QemuOpts")?
One usage still remains and it will be removed with an earlier patch, "[PATCH RFC v2 04/12] vfio: Avoid inspecting option QDict for rombar".
RFC PATCH v2 6/12 pcie_sriov: Reuse SR-IOV VF device instances
In reply to Akihiko OdakiDisable SR-IOV VF devices by reusing code to power down PCI devices instead of removing them when the guest requests to disable VFs. This allows to realize devices and report VF realization errors at PF realization time.
--- docs/pcie_sriov.txt | 8 ++-- include/hw/pci/pci.h | 2 +- include/hw/pci/pci_device.h | 2 +- include/hw/pci/pcie_sriov.h | 6 +-- hw/net/igb.c | 3 +- hw/nvme/ctrl.c | 3 +- hw/pci/pci.c | 18 ++++---- hw/pci/pci_host.c | 4 +- hw/pci/pcie.c | 4 +- hw/pci/pcie_sriov.c | 105 +++++++++++++++++++++----------------------- 10 files changed, 79 insertions(+), 76 deletions(-)
docs/pcie_sriov.txt +5 -3
@@ -52,9 +52,11 @@ setting up a BAR for a VF. ... /* Add and initialize the SR/IOV capability */ - pcie_sriov_pf_init(d, 0x200, "your_virtual_dev", - vf_devid, initial_vfs, total_vfs, - fun_offset, stride); + if (!pcie_sriov_pf_init(d, 0x200, "your_virtual_dev", + vf_devid, initial_vfs, total_vfs, + fun_offset, stride, errp)) { + return; + } /* Set up individual VF BARs (parameters as for normal BARs) */ pcie_sriov_pf_init_vf_bar( ... )
include/hw/pci/pci.h +1 -1
@@ -643,6 +643,6 @@ static inline void pci_irq_pulse(PCIDevice *pci_dev) }MSIMessage pci_get_msi_message (PCIDevice *dev,int vector); -void pci_set_power (PCIDevice *pci_dev,bool state); +void pci_set_enabled (PCIDevice *pci_dev,bool state);#endif
include/hw/pci/pci_device.h +1 -1
@@ -56,7 +56,7 @@ typedef struct PCIReqIDCache PCIReqIDCache;struct PCIDevice {DeviceState qdev;bool partially_hotplugged; -bool has_power; +bool is_enabled;/* PCI config space */ uint8_t *config;
include/hw/pci/pcie_sriov.h +3 -3
@@ -18,7 +18,6 @@struct PCIESriovPF {uint16_t num_vfs;/* Number of virtual functions created */ uint8_t vf_bar_type[PCI_NUM_REGIONS ];/* Store type for each VF bar */ -const char *vfname;/* Reference to the device type used for the VFs */ PCIDevice **vf;/* Pointer to an array of num_vfs VF devices */ };
@@ -27,10 +26,11 @@ struct PCIESriovVF {uint16_t vf_number;/* Logical VF number of this function */ }; -void pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset, +bool pcie_sriov_pf_init (PCIDevice *dev,uint16_t offset,const char *vfname,uint16_t vf_dev_id,uint16_t init_vfs,uint16_t total_vfs, -uint16_t vf_offset,uint16_t vf_stride); +uint16_t vf_offset,uint16_t vf_stride, +Error **errp);void pcie_sriov_pf_exit (PCIDevice *dev);/* Set up a VF bar in the SR/IOV bar area */
hw/net/igb.c +2 -1
@@ -449,7 +449,8 @@ static void igb_pci_realize(PCIDevice *pci_dev, Error **errp)pcie_sriov_pf_init (pci_dev,IGB_CAP_SRIOV_OFFSET ,TYPE_IGBVF ,IGB_82576_VF_DEV_ID ,IGB_MAX_VF_FUNCTIONS ,IGB_MAX_VF_FUNCTIONS , -IGB_VF_OFFSET ,IGB_VF_STRIDE ); +IGB_VF_OFFSET ,IGB_VF_STRIDE , + &error_abort); pcie_sriov_pf_init_vf_bar(pci_dev, IGBVF_MMIO_BAR_IDX, PCI_BASE_ADDRESS_MEM_TYPE_64 | PCI_BASE_ADDRESS_MEM_PREFETCH,
hw/nvme/ctrl.c +2 -1
@@ -8042,7 +8042,8 @@ static void nvme_init_sriov(NvmeCtrl *n, PCIDevice *pci_dev, uint16_t offset)pcie_sriov_pf_init (pci_dev, offset,"nvme" , vf_dev_id, n->params.sriov_max_vfs, n->params.sriov_max_vfs, -NVME_VF_OFFSET ,NVME_VF_STRIDE ); +NVME_VF_OFFSET ,NVME_VF_STRIDE , + &error_abort);pcie_sriov_pf_init_vf_bar (pci_dev,0 ,PCI_BASE_ADDRESS_SPACE_MEMORY |PCI_BASE_ADDRESS_MEM_TYPE_64 , bar_size);
hw/pci/pci.c +10 -8
@@ -1516,7 +1516,7 @@ static void pci_update_mappings(PCIDevice *d)continue ; new_addr =pci_bar_address (d, i, r->type, r->size); -if (!d->has_power) { +if (!d->is_enabled) { new_addr =PCI_BAR_UNMAPPED ; }
@@ -1604,7 +1604,7 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, intpci_update_irq_disabled (d, was_irq_disabled);memory_region_set_enabled (&d->bus_master_enable_region, (pci_get_word (d->config +PCI_COMMAND ) - &PCI_COMMAND_MASTER ) && d->has_power); + &PCI_COMMAND_MASTER ) && d->is_enabled); }msi_write_config (d, addr, val_in, l);
@@ -2180,7 +2180,9 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)return ; } -pci_set_power (pci_dev, true); +if (!pci_is_vf (pci_dev)) { +pci_set_enabled (pci_dev, true); + } pci_dev->msi_trigger = pci_msi_trigger; }
@@ -2844,18 +2846,18 @@ MSIMessage pci_get_msi_message(PCIDevice *dev, int vector)return msg; } -void pci_set_power (PCIDevice *d,bool state) +void pci_set_enabled (PCIDevice *d,bool state) { -if (d->has_power == state) { +if (d->is_enabled == state) {return ; } - d->has_power = state; + d->is_enabled = state;pci_update_mappings (d);memory_region_set_enabled (&d->bus_master_enable_region, (pci_get_word (d->config +PCI_COMMAND ) - &PCI_COMMAND_MASTER ) && d->has_power); -if (!d->has_power) { + &PCI_COMMAND_MASTER ) && d->is_enabled); +if (!d->is_enabled) {pci_device_reset (d); } }
hw/pci/pci_host.c +2 -2
@@ -86,7 +86,7 @@ void pci_host_config_write_common(PCIDevice *pci_dev, uint32_t addr,* allowing direct removal of unexposed functions. */ if ((pci_dev->qdev.hotplugged && !pci_get_function_0 (pci_dev)) || - !pci_dev->has_power ||is_pci_dev_ejected (pci_dev)) { + !pci_dev->is_enabled ||is_pci_dev_ejected (pci_dev)) {return ; }
@@ -111,7 +111,7 @@ uint32_t pci_host_config_read_common(PCIDevice *pci_dev, uint32_t addr,* allowing direct removal of unexposed functions. */ if ((pci_dev->qdev.hotplugged && !pci_get_function_0 (pci_dev)) || - !pci_dev->has_power ||is_pci_dev_ejected (pci_dev)) { + !pci_dev->is_enabled ||is_pci_dev_ejected (pci_dev)) {return ~0x0 ; }
hw/pci/pcie.c +3 -1
@@ -394,7 +394,9 @@ static void pcie_set_power_device(PCIBus *bus, PCIDevice *dev, void *opaque) {bool *power = opaque; -pci_set_power (dev, *power); +if (!pci_is_vf (dev)) { +pci_set_enabled (dev, *power); + } }static void pcie_cap_update_power (PCIDevice *hotplug_dev)
hw/pci/pcie_sriov.c +50 -55
156 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v2 7/12 pcie_sriov: Release VFs failed to realize
In reply to Akihiko OdakiRelease VFs failed to realize just as we do in unregister_vfs().
--- hw/pci/pcie_sriov.c | 2 ++ 1 file changed, 2 insertions(+)
hw/pci/pcie_sriov.c +2 -0
@@ -92,6 +92,8 @@ bool pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset, vf->exp.sriov_vf.vf_number = i;if (!qdev_realize (&vf->qdev, bus, errp)) { +object_unparent (OBJECT (vf)); +object_unref (vf);unrealize_vfs (dev, i);return false; }
Signature · 1 line
2.43.0
RFC PATCH v2 8/12 pcie_sriov: Ensure PF and VF are mutually exclusive
In reply to Akihiko OdakiA device cannot be a SR-IOV PF and a VF at the same time.
--- hw/pci/pcie_sriov.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/pci/pcie_sriov.c +5 -0
@@ -46,6 +46,11 @@ bool pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset,uint8_t *cfg = dev->config + offset;uint8_t *wmask; +if (pci_is_vf (dev)) { +error_setg (errp,"a device cannot be a SR-IOV PF and a VF at the same time" ); +return false; + } +pcie_add_capability (dev,PCI_EXT_CAP_ID_SRIOV ,1 , offset,PCI_EXT_CAP_SRIOV_SIZEOF ); dev->exp.sriov_cap = offset;
Signature · 1 line
2.43.0
RFC PATCH v2 9/12 pcie_sriov: Check PCI Express for SR-IOV PF
In reply to Akihiko OdakiSR-IOV requires PCI Express.
--- hw/pci/pcie_sriov.c | 5 +++++ 1 file changed, 5 insertions(+)
hw/pci/pcie_sriov.c +5 -0
@@ -46,6 +46,11 @@ bool pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset,uint8_t *cfg = dev->config + offset;uint8_t *wmask; +if (!pci_is_express (dev)) { +error_setg (errp,"PCI Express is required for SR-IOV PF" ); +return false; + } +if (pci_is_vf (dev)) {error_setg (errp,"a device cannot be a SR-IOV PF and a VF at the same time" );return false;
Signature · 1 line
2.43.0
RFC PATCH v2 10/12 pcie_sriov: Allow user to create SR-IOV device
In reply to Akihiko OdakiA user can create a SR-IOV device by specifying the PF with the sriov-pf property of the VFs. The VFs must be added before the PF.
A user-creatable VF must have PCIDeviceClass::sriov_vf_user_creatable set. Such a VF cannot refer to the PF because it is created before the PF.
A PF that user-creatable VFs can be attached calls pcie_sriov_pf_init_from_user_created_vfs() during realization and pcie_sriov_pf_exit() when exiting.
--- include/hw/pci/pci_device.h | 6 +- include/hw/pci/pcie_sriov.h | 19 +++ hw/pci/pci.c | 53 ++++++--- hw/pci/pcie_sriov.c | 279 +++++++++++++++++++++++++++++++++++--------- 4 files changed, 283 insertions(+), 74 deletions(-)
include/hw/pci/pci_device.h +5 -1
@@ -37,6 +37,8 @@ struct PCIDeviceClass {uint16_t subsystem_id;/* only for header type = 0 */ const char *romfile;/* rom bar */ + +bool sriov_vf_user_creatable; };enum PCIReqIDType {
@@ -160,6 +162,8 @@ struct PCIDevice {/* ID of standby device in net_failover pair */ char *failover_pair_id;uint32_t acpi_index; + +char *sriov_pf; };static inline int pci_intx (PCIDevice *pci_dev)
@@ -192,7 +196,7 @@ static inline int pci_is_express_downstream_port(const PCIDevice *d)static inline int pci_is_vf (const PCIDevice *d) { -return d->exp.sriov_vf.pf !=NULL ; +return d->sriov_pf || d->exp.sriov_vf.pf !=NULL ; }static inline uint32_t pci_config_size (const PCIDevice *d)
include/hw/pci/pcie_sriov.h +19 -0
@@ -19,6 +19,7 @@ struct PCIESriovPF {uint16_t num_vfs;/* Number of virtual functions created */ uint8_t vf_bar_type[PCI_NUM_REGIONS ];/* Store type for each VF bar */ PCIDevice **vf;/* Pointer to an array of num_vfs VF devices */ +bool vf_user_created;/* If VFs are created by user */ };struct PCIESriovVF {
@@ -41,6 +42,24 @@ void pcie_sriov_pf_init_vf_bar(PCIDevice *dev, int region_num,void pcie_sriov_vf_register_bar (PCIDevice *dev,int region_num,MemoryRegion *memory); +/** +* pcie_sriov_pf_init_from_user_created_vfs() - Initialize PF with user-created +* VFs. +* @dev: A PCIe device being realized. +* @offset: The offset of the SR-IOV capability. +* @errp: pointer to Error*, to store an error if it happens. +* +* Return: +* * true - @dev is initialized as a PCIe SR-IOV PF. +* * false - @dev is not initialized because there is no SR-IOV VFs or an error +* occurred. +*/ +bool pcie_sriov_pf_init_from_user_created_vfs (PCIDevice *dev,uint16_t offset, +Error **errp); + +bool pcie_sriov_register_device (PCIDevice *dev,Error **errp); +void pcie_sriov_unregister_device (PCIDevice *dev); +/* * Default (minimal) page size support values * as required by the SR/IOV standard:
hw/pci/pci.c +35 -18
97 lines not on this page · raw message
hw/pci/pcie_sriov.c +224 -55
328 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v2 11/12 virtio-pci: Implement SR-IOV PF
In reply to Akihiko OdakiAllow user to attach SR-IOV VF to a virtio-pci PF.
--- hw/virtio/virtio-pci.c | 7 +++++++ 1 file changed, 7 insertions(+)
hw/virtio/virtio-pci.c +7 -0
@@ -2075,6 +2075,12 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)pci_register_bar (&proxy->pci_dev, proxy->legacy_io_bar_idx,PCI_BASE_ADDRESS_SPACE_IO , &proxy->bar); } + +if (pcie_sriov_pf_init_from_user_created_vfs (&proxy->pci_dev, +PCI_CONFIG_SPACE_SIZE , + errp)) { +virtio_add_feature (&vdev->host_features,VIRTIO_F_SR_IOV ); + } }static void virtio_pci_device_unplugged (DeviceState *d)
@@ -2083,6 +2089,7 @@ static void virtio_pci_device_unplugged(DeviceState *d)bool modern =virtio_pci_modern (proxy);bool modern_pio = proxy->flags &VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY ; +pcie_sriov_pf_exit (&proxy->pci_dev);virtio_pci_stop_ioeventfd (proxy);if (modern) {
Signature · 1 line
2.43.0
RFC PATCH v2 12/12 virtio-net: Implement SR-IOV VF
In reply to Akihiko OdakiA virtio-net device can be added as a SR-IOV VF to another virtio-pci device that will be the PF.
--- hw/virtio/virtio-net-pci.c | 1 + 1 file changed, 1 insertion(+)
hw/virtio/virtio-net-pci.c +1 -0
@@ -75,6 +75,7 @@ static void virtio_net_pci_class_init(ObjectClass *klass, void *data) k->device_id =PCI_DEVICE_ID_VIRTIO_NET ; k->revision =VIRTIO_PCI_ABI_VERSION ; k->class_id =PCI_CLASS_NETWORK_ETHERNET ; + k->sriov_vf_user_creatable = true;set_bit (DEVICE_CATEGORY_NETWORK , dc->categories);device_class_set_props (dc, virtio_net_properties); vpciklass->realize = virtio_net_pci_realize;
Signature · 1 line
2.43.0
Quoted 30 lines · Akihiko Odaki
Introduction
------------This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o
-netdev user,id=p -netdev user,id=q
-device pcie-root-port,id=b
-device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=f
Quoted 4 lines
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
This seems not user friendly. Any reason we can't just allow user to specify the stride here?
Btw, I vaguely remember qemu allows the params to be accepted as a list. If this is true, we can accept a list of netdev here?
Quoted 11 lines
Keeping VF instances
--------------------A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
Could we store the device options in the PF?
Thanks
Quoted 65 lines
As an strategy alternative to runtime realization/unrealization, this series proposes to reuse the code to power down PCI Express devices. When a PCI Express device is powered down, it will be hidden from the guest but will be kept realized. This effectively implements the behavior we need for the SR-IOV emulation.
Summary
-------Patch [1, 5] refactors the PCI infrastructure code. Patch [6, 10] adds user-created SR-IOV VF infrastructure. Patch 11 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 12 allows user to create SR-IOV VFs with virtio-net-pci.
[1] https://patchew.org/QEMU/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Co-developed-by Yui Washizu <[email protected]>Signed-off-by Akihiko Odaki <[email protected]>---
Changes in v2:
- Changed to keep VF instances.
- Link to v1: https://lore.kernel.org/r/[email protected]---
Akihiko Odaki (12):
hw/pci: Initialize PCI multifunction after realization hw/pci: Determine if rombar is explicitly enabled hw/pci: Do not add ROM BAR for SR-IOV VF
vfio: Avoid inspecting option QDict for rombar hw/qdev: Remove opts member
pcie_sriov: Reuse SR-IOV VF device instances
pcie_sriov: Release VFs failed to realize
pcie_sriov: Ensure PF and VF are mutually exclusive
pcie_sriov: Check PCI Express for SR-IOV PF
pcie_sriov: Allow user to create SR-IOV device
virtio-pci: Implement SR-IOV PF
virtio-net: Implement SR-IOV VFdocs/pcie_sriov.txt | 8 +- include/hw/pci/pci.h | 2 +- include/hw/pci/pci_device.h | 13 +- include/hw/pci/pcie_sriov.h | 25 ++- include/hw/qdev-core.h | 4 - hw/core/qdev.c | 1 - hw/net/igb.c | 3 +- hw/nvme/ctrl.c | 3 +- hw/pci/pci.c | 98 +++++++----- hw/pci/pci_host.c | 4 +- hw/pci/pcie.c | 4 +- hw/pci/pcie_sriov.c | 360 +++++++++++++++++++++++++++++++++----------- hw/vfio/pci.c | 3 +- hw/virtio/virtio-net-pci.c | 1 + hw/virtio/virtio-pci.c | 7 + system/qdev-monitor.c | 12 +- 16 files changed, 395 insertions(+), 153 deletions(-)---
base-commit:4705fc0c8511d073bee4751c3c974aab2b10a970
change-id: 20231202-sriov-9402fb262be8Best regards,
--
Akihiko Odaki <[email protected]>
Quoted 31 lines · Jason Wang
Quoted 30 lines · Akihiko Odaki
Introduction
------------This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o -netdev user,id=p -netdev user,id=q -device pcie-root-port,id=b -device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=f
Quoted 7 lines
Quoted 4 lines
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
This seems not user friendly. Any reason we can't just allow user to specify the stride here?
It should be possible to assign addr automatically without requiring user to specify the stride. I'll try that in the next version.
Quoted 3 lines
Btw, I vaguely remember qemu allows the params to be accepted as a list. If this is true, we can accept a list of netdev here?
Yes, rocker does that. But the problem is not just about getting parameters needed for VFs, which I forgot to mention in the cover letter and will explain below.
Quoted 13 lines
Quoted 5 lines
Keeping VF instances
--------------------Quoted 7 lines
A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
Quoted 1 line
Could we store the device options in the PF?
I wrote it's to store the device options, but the problem is actually more about realizing VFs at runtime instead of at the initialization time.
Realizing VFs at runtime have two major problems. One is that it delays the validations of options; invalid options will be noticed when the guest requests to realize VFs. netdevs also warn that they are not used at initialization time, not knowing that they will be used by VFs later. References to other QEMU objects in the option may also die before VFs are realized.
The other problem is that QEMU cannot interact with the unrealized VFs. For example, if you type "device_add virtio-net-pci,id=vf,sriov-pf=pf" in HMP, you will expect "device_del vf" works, but it's hard to implement such behaviors with unrealized VFs.
I was first going to compromise and allow such quirky behaviors, but I realized such a compromise is unnecessary if we reuse the PCI power down logic so I wrote v2.
Regards,
Akihiko Odaki
Quoted 65 lines · Akihiko Odaki
Quoted 31 lines · Jason Wang
Quoted 30 lines · Akihiko Odaki
Introduction
------------This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o -netdev user,id=p -netdev user,id=q -device pcie-root-port,id=b -device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=fQuoted 7 lines
Quoted 4 lines
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
This seems not user friendly. Any reason we can't just allow user to specify the stride here?
It should be possible to assign addr automatically without requiring user to specify the stride. I'll try that in the next version.
Quoted 3 lines
Btw, I vaguely remember qemu allows the params to be accepted as a list. If this is true, we can accept a list of netdev here?
Yes, rocker does that. But the problem is not just about getting parameters needed for VFs, which I forgot to mention in the cover letter and will explain below.
Quoted 13 lines
Quoted 12 lines
Keeping VF instances
--------------------A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
Quoted 8 lines
Quoted 1 line
Could we store the device options in the PF?
I wrote it's to store the device options, but the problem is actually more about realizing VFs at runtime instead of at the initialization time.
Realizing VFs at runtime have two major problems. One is that it delays the validations of options; invalid options will be noticed when the guest requests to realize VFs.
If PCI spec allows the failure when creating VF, then it should not be a problem.
Quoted 2 lines
netdevs also warn that they are not used
at initialization time, not knowing that they will be used by VFs later.
We could invent things to calm down this false positive.
Quoted 2 lines
References to other QEMU objects in the option may also die before VFs are realized.
Is there any other thing than netdev we need to consider?
Quoted 5 lines
The other problem is that QEMU cannot interact with the unrealized VFs. For example, if you type "
device_addvirtio-net-pci,id=vf,sriov-pf=pf" in HMP, you will expect "device_delvf" works, but it's hard to implement such behaviors with unrealized VFs.
I think hotplug can only be done at PF level if we do that.
Quoted 4 lines
I was first going to compromise and allow such quirky behaviors, but I realized such a compromise is unnecessary if we reuse the PCI power down logic so I wrote v2.
Haven't checked the code, but anything related to the PM here?
Thanks
Quoted 4 lines
Regards,
Akihiko Odaki
Quoted 71 lines · Jason Wang
Quoted 70 lines · Akihiko Odaki
Quoted 31 lines · Jason Wang
Quoted 30 lines · Akihiko Odaki
Introduction
------------This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o -netdev user,id=p -netdev user,id=q -device pcie-root-port,id=b -device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=fQuoted 7 lines
Quoted 4 lines
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
This seems not user friendly. Any reason we can't just allow user to specify the stride here?
It should be possible to assign addr automatically without requiring user to specify the stride. I'll try that in the next version.
Quoted 3 lines
Btw, I vaguely remember qemu allows the params to be accepted as a list. If this is true, we can accept a list of netdev here?
Yes, rocker does that. But the problem is not just about getting parameters needed for VFs, which I forgot to mention in the cover letter and will explain below.
Quoted 13 lines
Quoted 5 lines
Keeping VF instances
--------------------Quoted 7 lines
A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
Quoted 1 line
Could we store the device options in the PF?
I wrote it's to store the device options, but the problem is actually more about realizing VFs at runtime instead of at the initialization time.
Quoted 6 lines
Quoted 3 lines
Realizing VFs at runtime have two major problems. One is that it delays the validations of options; invalid options will be noticed when the guest requests to realize VFs.
If PCI spec allows the failure when creating VF, then it should not be a problem.
I doubt the spec cares such a failure at all. VF enablement should always work for a real hardware. It's neither user-friendly to tell configuration errors at runtime.
Quoted 10 lines
Quoted 3 lines
netdevs also warn that they are not used
at initialization time, not knowing that they will be used by VFs later.We could invent things to calm down this false positive.
Quoted 2 lines
References to other QEMU objects in the option may also die before VFs are realized.
Is there any other thing than netdev we need to consider?
You will also want to set a distinct mac for each VF. Other properties does not matter much in my opinion.
Quoted 8 lines
Quoted 6 lines
The other problem is that QEMU cannot interact with the unrealized VFs. For example, if you type "
device_addvirtio-net-pci,id=vf,sriov-pf=pf" in HMP, you will expect "device_delvf" works, but it's hard to implement such behaviors with unrealized VFs.I think hotplug can only be done at PF level if we do that.
Assuming you mean to let netdev and mac accept arrays, yes.
Quoted 7 lines
Quoted 5 lines
I was first going to compromise and allow such quirky behaviors, but I realized such a compromise is unnecessary if we reuse the PCI power down logic so I wrote v2.
Haven't checked the code, but anything related to the PM here?
You mean power management? We don't have to care about PCI power down for VFs because powering down a SR-IOV PCI device will reset it and thus disable its VFs.
Regards,
Akihiko Odaki
Quoted 77 lines · Akihiko Odaki
Quoted 75 lines · Jason Wang
Quoted 73 lines · Akihiko Odaki
Quoted 31 lines · Jason Wang
Quoted 30 lines · Akihiko Odaki
Introduction
------------This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o -netdev user,id=p -netdev user,id=q -device pcie-root-port,id=b -device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=fQuoted 7 lines
Quoted 4 lines
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
This seems not user friendly. Any reason we can't just allow user to specify the stride here?
It should be possible to assign addr automatically without requiring user to specify the stride. I'll try that in the next version.
Quoted 3 lines
Btw, I vaguely remember qemu allows the params to be accepted as a list. If this is true, we can accept a list of netdev here?
Yes, rocker does that. But the problem is not just about getting parameters needed for VFs, which I forgot to mention in the cover letter and will explain below.
Quoted 13 lines
Quoted 5 lines
Keeping VF instances
--------------------Quoted 7 lines
A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
Quoted 1 line
Could we store the device options in the PF?
I wrote it's to store the device options, but the problem is actually more about realizing VFs at runtime instead of at the initialization time.
Realizing VFs at runtime have two major problems. One is that it delays the validations of options; invalid options will be noticed when the guest requests to realize VFs.
Quoted 6 lines
Quoted 2 lines
If PCI spec allows the failure when creating VF, then it should not be a problem.
I doubt the spec cares such a failure at all. VF enablement should always work for a real hardware. It's neither user-friendly to tell configuration errors at runtime.
I'm not sure which options we should care about? Did you mean netdev options or the virtio-net specific ones?
If VF stick to the same options as PF (except for the SRIOV), it should be validated during the PF initialization.
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
netdevs also warn that they are not used
at initialization time, not knowing that they will be used by VFs later.We could invent things to calm down this false positive.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
References to other QEMU objects in the option may also die before VFs are realized.
Is there any other thing than netdev we need to consider?
You will also want to set a distinct mac for each VF. Other properties does not matter much in my opinion.
Qemu doesn't check mac duplication now. So it's up to the mgmt layer.
Quoted 18 lines
Quoted 9 lines
Quoted 7 lines
The other problem is that QEMU cannot interact with the unrealized VFs. For example, if you type "
device_addvirtio-net-pci,id=vf,sriov-pf=pf" in HMP, you will expect "device_delvf" works, but it's hard to implement such behaviors with unrealized VFs.I think hotplug can only be done at PF level if we do that.
Assuming you mean to let netdev and mac accept arrays, yes.
Quoted 6 lines
Quoted 5 lines
I was first going to compromise and allow such quirky behaviors, but I realized such a compromise is unnecessary if we reuse the PCI power down logic so I wrote v2.
Quoted 5 lines
Quoted 1 line
Haven't checked the code, but anything related to the PM here?
You mean power management? We don't have to care about PCI power down for VFs because powering down a SR-IOV PCI device will reset it and thus disable its VFs.
Ok.
Thanks
Quoted 4 lines
Regards,
Akihiko Odaki
Quoted 85 lines · Jason Wang
Quoted 77 lines · Akihiko Odaki
Quoted 75 lines · Jason Wang
Quoted 73 lines · Akihiko Odaki
Quoted 31 lines · Jason Wang
Quoted 30 lines · Akihiko Odaki
Introduction
------------This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o -netdev user,id=p -netdev user,id=q -device pcie-root-port,id=b -device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=fQuoted 7 lines
Quoted 4 lines
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
This seems not user friendly. Any reason we can't just allow user to specify the stride here?
It should be possible to assign addr automatically without requiring user to specify the stride. I'll try that in the next version.
Quoted 3 lines
Btw, I vaguely remember qemu allows the params to be accepted as a list. If this is true, we can accept a list of netdev here?
Yes, rocker does that. But the problem is not just about getting parameters needed for VFs, which I forgot to mention in the cover letter and will explain below.
Quoted 13 lines
Quoted 5 lines
Keeping VF instances
--------------------Quoted 7 lines
A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
Quoted 1 line
Could we store the device options in the PF?
I wrote it's to store the device options, but the problem is actually more about realizing VFs at runtime instead of at the initialization time.
Realizing VFs at runtime have two major problems. One is that it delays the validations of options; invalid options will be noticed when the guest requests to realize VFs.
Quoted 6 lines
Quoted 2 lines
If PCI spec allows the failure when creating VF, then it should not be a problem.
I doubt the spec cares such a failure at all. VF enablement should always work for a real hardware. It's neither user-friendly to tell configuration errors at runtime.
Quoted 5 lines
I'm not sure which options we should care about? Did you mean netdev options or the virtio-net specific ones?
If VF stick to the same options as PF (except for the SRIOV), it should be validated during the PF initialization.
I'm aware that it's necessary to validate netdev options and PCI function numbers (a.k.a. addr/devfn). I'm not sure if the other options may result in an invalid VF configuration.
That said, I think it's better to let the VF realization code validate the configuration at PF realization - it's less error-prone and potentially requires less code. It also benefits existing SR-IOV devices (igb and nvme) so I'm going to push that change forward whether it will be needed for virtio-net SR-IOV emulation.
Assuming the change to realize VFs early is going to happen for igb and nvme, most of the changes *only* needed by virtio-net SR-IOV emulation is done by:
patch 10 "pcie_sriov: Allow user to create SR-IOV device".
Quoted 11 lines
Quoted 11 lines
Quoted 11 lines
Quoted 5 lines
netdevs also warn that they are not used
at initialization time, not knowing that they will be used by VFs later.We could invent things to calm down this false positive.
Quoted 2 lines
References to other QEMU objects in the option may also die before VFs are realized.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Is there any other thing than netdev we need to consider?
You will also want to set a distinct mac for each VF. Other properties does not matter much in my opinion.
Qemu doesn't check mac duplication now. So it's up to the mgmt layer.
Right. mac is not important; it's just nice to have.
Regards,
Akihiko Odaki
Quoted 29 lines · Akihiko Odaki
Introduction
------------This series is based on the RFC series submitted by Yui Washizu[1]. See also [2] for the context.
This series enables SR-IOV emulation for virtio-net. It is useful to test SR-IOV support on the guest, or to expose several vDPA devices in a VM. vDPA devices can also provide L2 switching feature for offloading though it is out of scope to allow the guest to configure such a feature.
The PF side code resides in virtio-pci. The VF side code resides in the PCI common infrastructure, but it is restricted to work only for virtio-net-pci because of lack of validation.
User Interface
--------------A user can configure a SR-IOV capable virtio-net device by adding virtio-net-pci functions to a bus. Below is a command line example:
-netdev user,id=n -netdev user,id=o -netdev user,id=p -netdev user,id=q -device pcie-root-port,id=b -device virtio-net-pci,bus=b,addr=0x0.0x3,netdev=q,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x2,netdev=p,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=f
Quoted 4 lines
The VFs specify the paired PF with "sriov-pf" property. The PF must be added after all VFs. It is user's responsibility to ensure that VFs have function numbers larger than one of the PF, and the function numbers have a consistent stride.
I attempted to create a VF on a VM using your patches and tested the creation of one VF on the VM.
I initiated QEMU with the following commands:
-netdev tap,id=n,vhost=on -netdev tap,id=o,vhost=on -device pcie-root-port,id=b
-device virtio-net-pci,bus=b,addr=0x0.0x1,netdev=o,sriov-pf=f -device virtio-net-pci,bus=b,addr=0x0.0x0,netdev=n,id=f
However, when creating just one VF,
QEMU crashed at hw/pci/pci.c:1443.
---uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride;
---
The error output was:
'CPU 1/KVM trap divide error ip:... sp:... error:0 in kvm-qemu.'
Interestingly, when creating two VFs, QEMU did not crash.
Quoted 73 lines
Keeping VF instances
--------------------A problem with SR-IOV emulation is that it needs to hotplug the VFs as the guest requests. Previously, this behavior was implemented by realizing and unrealizing VFs at runtime. However, this strategy does not work well for the proposed virtio-net emulation; in this proposal, device options passed in the command line must be maintained as VFs are hotplugged, but they are consumed when the machine starts and not available after that, which makes realizing VFs at runtime impossible.
As an strategy alternative to runtime realization/unrealization, this series proposes to reuse the code to power down PCI Express devices. When a PCI Express device is powered down, it will be hidden from the guest but will be kept realized. This effectively implements the behavior we need for the SR-IOV emulation.
Summary
-------Patch [1, 5] refactors the PCI infrastructure code. Patch [6, 10] adds user-created SR-IOV VF infrastructure. Patch 11 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 12 allows user to create SR-IOV VFs with virtio-net-pci.
[1] https://patchew.org/QEMU/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/Co-developed-by Yui Washizu <[email protected]>Signed-off-by Akihiko Odaki <[email protected]>---
Changes in v2:
- Changed to keep VF instances.
- Link to v1: https://lore.kernel.org/r/[email protected]---
Akihiko Odaki (12):
hw/pci: Initialize PCI multifunction after realization hw/pci: Determine if rombar is explicitly enabled hw/pci: Do not add ROM BAR for SR-IOV VF
vfio: Avoid inspecting option QDict for rombar hw/qdev: Remove opts member
pcie_sriov: Reuse SR-IOV VF device instances
pcie_sriov: Release VFs failed to realize
pcie_sriov: Ensure PF and VF are mutually exclusive
pcie_sriov: Check PCI Express for SR-IOV PF
pcie_sriov: Allow user to create SR-IOV device
virtio-pci: Implement SR-IOV PF
virtio-net: Implement SR-IOV VFdocs/pcie_sriov.txt | 8 +- include/hw/pci/pci.h | 2 +- include/hw/pci/pci_device.h | 13 +- include/hw/pci/pcie_sriov.h | 25 ++- include/hw/qdev-core.h | 4 - hw/core/qdev.c | 1 - hw/net/igb.c | 3 +- hw/nvme/ctrl.c | 3 +- hw/pci/pci.c | 98 +++++++----- hw/pci/pci_host.c | 4 +- hw/pci/pcie.c | 4 +- hw/pci/pcie_sriov.c | 360 +++++++++++++++++++++++++++++++++----------- hw/vfio/pci.c | 3 +- hw/virtio/virtio-net-pci.c | 1 + hw/virtio/virtio-pci.c | 7 + system/qdev-monitor.c | 12 +- 16 files changed, 395 insertions(+), 153 deletions(-)---
base-commit:4705fc0c8511d073bee4751c3c974aab2b10a970
change-id: 20231202-sriov-9402fb262be8Best regards,