Skip to contentSkip to conversation

virtio-net: add support for SR-IOV emulation

PATCH In review Akihiko Odaki 3 participants 14 messages Mar 14, 2025 – Apr 2, 2025

lore
Share & tools
lore
#0 lore raw

v9 cover virtio-net: add support for SR-IOV emulation

Based-on: <[email protected]> ("[PATCH v18 00/14] hw/pci: SR-IOV related fixes and improvements")

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 disables ROM BAR, which virtio-net-pci enables by default, for VFs.
Patch 2 makes zero stride valid for 1 VF configuration. Patch 3 and 4 adds validations.
Patch 5 adds user-created SR-IOV VF infrastructure.
Patch 6 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 7 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 v9:
- Rebased.
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Rebased.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Removed #include <error-report.h>, which is no longer needed.
- Rebased.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Added ARI extended capability.
- Rebased.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Dropped the RFC tag.
- Fixed device unrealization.
- Rebased.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Added patch "hw/pci: Fix SR-IOV VF number calculation" to fix division by zero reported by Yui Washizu.
- Rebased.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rebased.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Changed to keep VF instances.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (9):
hw/pci: Do not add ROM BAR for SR-IOV VF
hw/pci: Fix SR-IOV VF number calculation
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: Document composable SR-IOV device
pcie_sriov: Make a PCI device with user-created VF ARI-capable

 MAINTAINERS                    |   1 +
 docs/system/index.rst          |   1 +
 docs/system/sriov.rst          |  37 ++++++
 include/hw/pci/pci_device.h    |   6 +-
 include/hw/pci/pcie_sriov.h    |  21 +++
 include/hw/virtio/virtio-pci.h |   1 +
 hw/pci/pci.c                   |  76 +++++++----
 hw/pci/pcie_sriov.c            | 294 +++++++++++++++++++++++++++++++++--------
 hw/virtio/virtio-net-pci.c     |   1 +
 hw/virtio/virtio-pci.c         |  24 +++-
 10 files changed, 378 insertions(+), 84 deletions(-)

---
base-commit: 825b96dbcee23d134b691fc75618b59c5f53da32
change-id: 20231202-sriov-9402fb262be8

Best regards,

Signature · 1 line
Akihiko Odaki <[email protected]>
#1 lore raw

PATCH v9 1/9 hw/pci: Do not add ROM BAR for SR-IOV VF

A SR-IOV VF cannot have a ROM BAR.

---
 hw/pci/pci.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/pci/pci.c +8 -0
index 2844ec55560f..8460b6105338 100644
@@ -2522,6 +2522,14 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
         return;
     }
 
+    if (pci_is_vf(pdev)) {
+        if (pdev->rom_bar > 0) {
+            error_setg(errp, "ROM BAR cannot be enabled for SR-IOV VF");
+        }
+
+        return;
+    }
+
     if (load_file || pdev->romsize == UINT32_MAX) {
         path = qemu_find_file(QEMU_FILE_TYPE_BIOS, pdev->romfile);
         if (path == NULL) {
Signature · 1 line
2.48.1
#2 lore raw

PATCH v9 2/9 hw/pci: Fix SR-IOV VF number calculation

In reply to Akihiko Odaki

pci_config_get_bar_addr() had a division by vf_stride. vf_stride needs to be non-zero when there are multiple VFs, but the specification does not prohibit to make it zero when there is only one VF.

Do not perform the division for the first VF to avoid division by zero.

---
 hw/pci/pci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
hw/pci/pci.c +5 -1
index 8460b6105338..2b1c7d487fc1 100644
@@ -1594,7 +1594,11 @@ static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg,
             pci_get_word(pf->config + sriov_cap + PCI_SRIOV_VF_OFFSET);
         uint16_t vf_stride =
             pci_get_word(pf->config + sriov_cap + PCI_SRIOV_VF_STRIDE);
-        uint32_t vf_num = (d->devfn - (pf->devfn + vf_offset)) / vf_stride;
+        uint32_t vf_num = d->devfn - (pf->devfn + vf_offset);
+
+        if (vf_num) {
+            vf_num /= vf_stride;
+        }
 
         if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
             new_addr = pci_get_quad(pf->config + bar);
Signature · 1 line
2.48.1
#3 lore raw

PATCH v9 3/9 pcie_sriov: Ensure PF and VF are mutually exclusive

In reply to Akihiko Odaki

A 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
index 1eb4358256de..109b2ebcccba 100644
@@ -42,6 +42,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;
+    }
+
     if (total_vfs &&
         (uint32_t)devfn + (uint32_t)(total_vfs - 1) * vf_stride >= PCI_DEVFN_MAX) {
         error_setg(errp, "VF addr overflows");
Signature · 1 line
2.48.1
#4 lore raw

PATCH v9 4/9 pcie_sriov: Check PCI Express for SR-IOV PF

In reply to Akihiko Odaki

SR-IOV requires PCI Express.

---
 hw/pci/pcie_sriov.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/pci/pcie_sriov.c +5 -0
index 109b2ebcccba..a5b546abe8bb 100644
@@ -42,6 +42,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.48.1
#5 lore raw

PATCH v9 5/9 pcie_sriov: Allow user to create SR-IOV device

In reply to Akihiko Odaki

A 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 |  18 +++
 hw/pci/pci.c                |  62 ++++++----
 hw/pci/pcie_sriov.c         | 278 +++++++++++++++++++++++++++++++++++---------
 4 files changed, 286 insertions(+), 78 deletions(-)
include/hw/pci/pci_device.h +5 -1
index 345b12eaac1a..e41d95b0b06f 100644
@@ -38,6 +38,8 @@ struct PCIDeviceClass {
     uint16_t subsystem_id;              /* only for header type = 0 */
 
     const char *romfile;                /* rom bar */
+
+    bool sriov_vf_user_creatable;
 };
 
 enum PCIReqIDType {
@@ -177,6 +179,8 @@ struct PCIDevice {
      * realizing the device.
      */
     uint32_t max_bounce_buffer_size;
+
+    char *sriov_pf;
 };
 
 static inline int pci_intx(PCIDevice *pci_dev)
@@ -209,7 +213,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 +18 -0
index c5d2d318d330..f75b8f22ee92 100644
@@ -18,6 +18,7 @@
 typedef struct PCIESriovPF {
     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 */
 } PCIESriovPF;
 
 typedef struct PCIESriovVF {
@@ -40,6 +41,23 @@ 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: The size of added capability. 0 if the user did not create VFs.
+ *         -1 if failed.
+ */
+int16_t 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 +38 -24

112 lines not on this page · raw message

hw/pci/pcie_sriov.c +225 -53

338 lines not on this page · raw message

Signature · 1 line
2.48.1
#6 lore raw

PATCH v9 6/9 virtio-pci: Implement SR-IOV PF

In reply to Akihiko Odaki

Allow user to attach SR-IOV VF to a virtio-pci PF.

---
 include/hw/virtio/virtio-pci.h |  1 +
 hw/virtio/virtio-pci.c         | 20 +++++++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)
include/hw/virtio/virtio-pci.h +1 -0
index 971c5fabd444..b473274834e9 100644
@@ -155,6 +155,7 @@ struct VirtIOPCIProxy {
     uint32_t modern_io_bar_idx;
     uint32_t modern_mem_bar_idx;
     int config_cap;
+    uint16_t last_pcie_cap_offset;
     uint32_t flags;
     bool disable_modern;
     bool ignore_backend_features;
hw/virtio/virtio-pci.c +15 -5
index 3ca3f849d391..2463bff2ea5b 100644
@@ -1962,6 +1962,7 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
     uint8_t *config;
     uint32_t size;
     VirtIODevice *vdev = virtio_bus_get_device(bus);
+    int16_t res;
 
     /*
      * Virtio capabilities present without
@@ -2109,6 +2110,14 @@ 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);
     }
+
+    res = pcie_sriov_pf_init_from_user_created_vfs(&proxy->pci_dev,
+                                                   proxy->last_pcie_cap_offset,
+                                                   errp);
+    if (res > 0) {
+        proxy->last_pcie_cap_offset += res;
+        virtio_add_feature(&vdev->host_features, VIRTIO_F_SR_IOV);
+    }
 }
 
 static void virtio_pci_device_unplugged(DeviceState *d)
@@ -2199,7 +2208,7 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
 
     if (pcie_port && pci_is_express(pci_dev)) {
         int pos;
-        uint16_t last_pcie_cap_offset = PCI_CONFIG_SPACE_SIZE;
+        proxy->last_pcie_cap_offset = PCI_CONFIG_SPACE_SIZE;
 
         pos = pcie_endpoint_cap_init(pci_dev, 0);
         assert(pos > 0);
@@ -2216,9 +2225,9 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
         pci_set_word(pci_dev->config + pos + PCI_PM_PMC, 0x3);
 
         if (proxy->flags & VIRTIO_PCI_FLAG_AER) {
-            pcie_aer_init(pci_dev, PCI_ERR_VER, last_pcie_cap_offset,
+            pcie_aer_init(pci_dev, PCI_ERR_VER, proxy->last_pcie_cap_offset,
                           PCI_ERR_SIZEOF, NULL);
-            last_pcie_cap_offset += PCI_ERR_SIZEOF;
+            proxy->last_pcie_cap_offset += PCI_ERR_SIZEOF;
         }
 
         if (proxy->flags & VIRTIO_PCI_FLAG_INIT_DEVERR) {
@@ -2243,9 +2252,9 @@ static void virtio_pci_realize(PCIDevice *pci_dev, Error **errp)
         }
 
         if (proxy->flags & VIRTIO_PCI_FLAG_ATS) {
-            pcie_ats_init(pci_dev, last_pcie_cap_offset,
+            pcie_ats_init(pci_dev, proxy->last_pcie_cap_offset,
                           proxy->flags & VIRTIO_PCI_FLAG_ATS_PAGE_ALIGNED);
-            last_pcie_cap_offset += PCI_EXT_CAP_ATS_SIZEOF;
+            proxy->last_pcie_cap_offset += PCI_EXT_CAP_ATS_SIZEOF;
         }
 
         if (proxy->flags & VIRTIO_PCI_FLAG_INIT_FLR) {
@@ -2273,6 +2282,7 @@ static void virtio_pci_exit(PCIDevice *pci_dev)
                      !pci_bus_is_root(pci_get_bus(pci_dev));
     bool modern_pio = proxy->flags & VIRTIO_PCI_FLAG_MODERN_PIO_NOTIFY;
 
+    pcie_sriov_pf_exit(&proxy->pci_dev);
     msix_uninit_exclusive_bar(pci_dev);
     if (proxy->flags & VIRTIO_PCI_FLAG_AER && pcie_port &&
         pci_is_express(pci_dev)) {
Signature · 1 line
2.48.1
#7 lore raw

PATCH v9 7/9 virtio-net: Implement SR-IOV VF

In reply to Akihiko Odaki

A 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
index e18953ad674b..430b727ea3f4 100644
@@ -74,6 +74,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.48.1
#8 lore raw

PATCH v9 8/9 docs: Document composable SR-IOV device

In reply to Akihiko Odaki
---
 MAINTAINERS           |  1 +
 docs/system/index.rst |  1 +
 docs/system/sriov.rst | 36 ++++++++++++++++++++++++++++++++++++
 3 files changed, 38 insertions(+)
MAINTAINERS +1 -0
index 0e5db7a57449..4cde71687645 100644
@@ -2029,6 +2029,7 @@ F: hw/pci-bridge/*
 F: qapi/pci.json
 F: docs/pci*
 F: docs/specs/*pci*
+F: docs/system/sriov.rst
 
 PCIE DOE
 M: Huai-Cheng Kuo <[email protected]>
docs/system/index.rst +1 -0
index c21065e51932..718e9d3c56bb 100644
@@ -39,3 +39,4 @@ or Hypervisor.Framework.
    multi-process
    confidential-guest-support
    vm-templating
+   sriov
docs/system/sriov.rst new file +36 -0
new file mode 100644
index 000000000000..a851a66a4b8b
@@ -0,0 +1,36 @@
+.. SPDX-License-Identifier: GPL-2.0-or-later
+
+Compsable SR-IOV device
+=======================
+
+SR-IOV (Single Root I/O Virtualization) is an optional extended capability of a
+PCI Express device. It allows a single physical function (PF) to appear as
+multiple virtual functions (VFs) for the main purpose of eliminating software
+overhead in I/O from virtual machines.
+
+There are devices with predefined SR-IOV configurations, but it is also possible
+to compose an SR-IOV device yourself. Composing an SR-IOV device is currently
+only supported by virtio-net-pci.
+
+Users can configure an SR-IOV-capable virtio-net device by adding
+virtio-net-pci functions to a bus. Below is a command line example:
+
+.. code-block:: shell
+
+    -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 the user's responsibility to ensure that VFs have
+function numbers larger than one of the PF, and that the function numbers
+have a consistent stride.
+
+You may also need to perform additional steps to activate the SR-IOV feature on
+your guest. For Linux, refer to [1]_.
+
+.. [1] https://docs.kernel.org/PCI/pci-iov-howto.html
Signature · 1 line
2.48.1
#9 lore raw

PATCH v9 9/9 pcie_sriov: Make a PCI device with user-created VF ARI-capable

In reply to Akihiko Odaki
---
 docs/system/sriov.rst       |  3 ++-
 include/hw/pci/pcie_sriov.h |  7 +++++--
 hw/pci/pcie_sriov.c         |  8 +++++++-
 hw/virtio/virtio-pci.c      | 16 ++++++++++------
 4 files changed, 24 insertions(+), 10 deletions(-)
docs/system/sriov.rst +2 -1
index a851a66a4b8b..d12178f3c319 100644
@@ -28,7 +28,8 @@ virtio-net-pci functions to a bus. Below is a command line example:
 The VFs specify the paired PF with ``sriov-pf`` property. The PF must be
 added after all VFs. It is the user's responsibility to ensure that VFs have
 function numbers larger than one of the PF, and that the function numbers
-have a consistent stride.
+have a consistent stride. Both the PF and VFs are ARI-capable so you can have
+255 VFs at maximum.
 
 You may also need to perform additional steps to activate the SR-IOV feature on
 your guest. For Linux, refer to [1]_.
include/hw/pci/pcie_sriov.h +5 -2
index f75b8f22ee92..aeaa38cf3456 100644
@@ -43,12 +43,15 @@ void pcie_sriov_vf_register_bar(PCIDevice *dev, int region_num,
 
 /**
  * pcie_sriov_pf_init_from_user_created_vfs() - Initialize PF with user-created
- *                                              VFs.
+ *                                              VFs, adding ARI to PF
  * @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: The size of added capability. 0 if the user did not create VFs.
+ * Initializes a PF with user-created VFs, adding the ARI extended capability to
+ * the PF. The VFs should call pcie_ari_init() to form an ARI device.
+ *
+ * Return: The size of added capabilities. 0 if the user did not create VFs.
  *         -1 if failed.
  */
 int16_t pcie_sriov_pf_init_from_user_created_vfs(PCIDevice *dev,
hw/pci/pcie_sriov.c +7 -1
index 08f707e847fd..3ad18744f4a8 100644
@@ -245,6 +245,7 @@ int16_t pcie_sriov_pf_init_from_user_created_vfs(PCIDevice *dev,
     PCIDevice **vfs;
     BusState *bus = qdev_get_parent_bus(DEVICE(dev));
     uint16_t ven_id = pci_get_word(dev->config + PCI_VENDOR_ID);
+    uint16_t size = PCI_EXT_CAP_SRIOV_SIZEOF;
     uint16_t vf_dev_id;
     uint16_t vf_offset;
     uint16_t vf_stride;
@@ -311,6 +312,11 @@ int16_t pcie_sriov_pf_init_from_user_created_vfs(PCIDevice *dev,
         return -1;
     }
 
+    if (!pcie_find_capability(dev, PCI_EXT_CAP_ID_ARI)) {
+        pcie_ari_init(dev, offset + size);
+        size += PCI_ARI_SIZEOF;
+    }
+
     for (i = 0; i < pf->len; i++) {
         vfs[i]->exp.sriov_vf.pf = dev;
         vfs[i]->exp.sriov_vf.vf_number = i;
@@ -331,7 +337,7 @@ int16_t pcie_sriov_pf_init_from_user_created_vfs(PCIDevice *dev,
         }
     }
 
-    return PCI_EXT_CAP_SRIOV_SIZEOF;
+    return size;
 }
 
 bool pcie_sriov_register_device(PCIDevice *dev, Error **errp)
hw/virtio/virtio-pci.c +10 -6
index 2463bff2ea5b..58abdda04bc3 100644
@@ -2111,12 +2111,16 @@ static void virtio_pci_device_plugged(DeviceState *d, Error **errp)
                          PCI_BASE_ADDRESS_SPACE_IO, &proxy->bar);
     }
 
-    res = pcie_sriov_pf_init_from_user_created_vfs(&proxy->pci_dev,
-                                                   proxy->last_pcie_cap_offset,
-                                                   errp);
-    if (res > 0) {
-        proxy->last_pcie_cap_offset += res;
-        virtio_add_feature(&vdev->host_features, VIRTIO_F_SR_IOV);
+    if (pci_is_vf(&proxy->pci_dev)) {
+        pcie_ari_init(&proxy->pci_dev, proxy->last_pcie_cap_offset);
+        proxy->last_pcie_cap_offset += PCI_ARI_SIZEOF;
+    } else {
+        res = pcie_sriov_pf_init_from_user_created_vfs(
+            &proxy->pci_dev, proxy->last_pcie_cap_offset, errp);
+        if (res > 0) {
+            proxy->last_pcie_cap_offset += res;
+            virtio_add_feature(&vdev->host_features, VIRTIO_F_SR_IOV);
+        }
     }
 }
 
Signature · 1 line
2.48.1
#10 lore raw
In reply to Akihiko Odaki

I tested the following features with this patch series, and there were not  issues:
- Creation and deletion of VFs
- Communication with an external machine through VFs

Thank you.

Yui

Quoted 137 lines · Akihiko Odaki

Based-on:<[email protected]> ("[PATCH v18 00/14] hw/pci: SR-IOV related fixes and improvements")

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 disables ROM BAR, which virtio-net-pci enables by default, for VFs.
Patch 2 makes zero stride valid for 1 VF configuration. Patch 3 and 4 adds validations.
Patch 5 adds user-created SR-IOV VF infrastructure.
Patch 6 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 7 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 v9:
- Rebased.
- Link to v8:https://lore.kernel.org/r/[email protected]

Changes in v8:
- Rebased.
- Link to v7:https://lore.kernel.org/r/[email protected]

Changes in v7:
- Removed #include <error-report.h>, which is no longer needed.
- Rebased.
- Link to v6:https://lore.kernel.org/r/[email protected]

Changes in v6:
- Added ARI extended capability.
- Rebased.
- Link to v5:https://lore.kernel.org/r/[email protected]

Changes in v5:
- Dropped the RFC tag.
- Fixed device unrealization.
- Rebased.
- Link to v4:https://lore.kernel.org/r/[email protected]

Changes in v4:
- Added patch "hw/pci: Fix SR-IOV VF number calculation" to fix division by zero reported by Yui Washizu.
- Rebased.
- Link to v3:https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rebased.
- Link to v2:https://lore.kernel.org/r/[email protected]

Changes in v2:
- Changed to keep VF instances.
- Link to v1:https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (9):
hw/pci: Do not add ROM BAR for SR-IOV VF
hw/pci: Fix SR-IOV VF number calculation
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: Document composable SR-IOV device
pcie_sriov: Make a PCI device with user-created VF ARI-capable

  MAINTAINERS                    |   1 +
  docs/system/index.rst          |   1 +
  docs/system/sriov.rst          |  37 ++++++
  include/hw/pci/pci_device.h    |   6 +-
  include/hw/pci/pcie_sriov.h    |  21 +++
  include/hw/virtio/virtio-pci.h |   1 +
  hw/pci/pci.c                   |  76 +++++++----
  hw/pci/pcie_sriov.c            | 294 +++++++++++++++++++++++++++++++++--------
  hw/virtio/virtio-net-pci.c     |   1 +
  hw/virtio/virtio-pci.c         |  24 +++-
  10 files changed, 378 insertions(+), 84 deletions(-)

---
base-commit: 825b96dbcee23d134b691fc75618b59c5f53da32
change-id: 20231202-sriov-9402fb262be8

Best regards,

author Tested-by Your Name
#11 lore raw
Quoted 6 lines · Yui Washizu

I tested the following features with this patch series, and there were not  issues:
- Creation and deletion of VFs
- Communication with an external machine through VFs

Thank you.

Can you reply with:

This tag means:
- you have tested changes you found significant,
- you found no problems with them, and
- you are fine to record that in Git commit logs

Pasha Tatashin (who told me they tried the patches), it would be nice if you do the same.

Regards,
Akihiko Odaki

Quoted 152 lines

Yui

Quoted 146 lines · Akihiko Odaki

Based-on:<[email protected]> ("[PATCH v18 00/14] hw/pci: SR-IOV related fixes and improvements")

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 disables ROM BAR, which virtio-net-pci enables by default, for VFs.
Patch 2 makes zero stride valid for 1 VF configuration. Patch 3 and 4 adds validations.
Patch 5 adds user-created SR-IOV VF infrastructure.
Patch 6 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 7 allows user to create SR-IOV VFs with virtio-net-pci.

[1]https://patchew.org/QEMU/1689731808-3009-1-git-send-email- [email protected]/ [2]https://lore.kernel.org/all/5d46f455- [email protected]/

---
Changes in v9:
- Rebased.
- Link to v8:https://lore.kernel.org/r/20250104-sriov- [email protected]

Changes in v8:
- Rebased.
- Link to v7:https://lore.kernel.org/r/20240813-sriov- [email protected]

Changes in v7:
- Removed #include <error-report.h>, which is no longer needed.
- Rebased.
- Link to v6:https://lore.kernel.org/r/20240802-sriov- [email protected]

Changes in v6:
- Added ARI extended capability.
- Rebased.
- Link to v5:https://lore.kernel.org/r/20240715-sriov- [email protected]

Changes in v5:
- Dropped the RFC tag.
- Fixed device unrealization.
- Rebased.
- Link to v4:https://lore.kernel.org/r/20240428-sriov-v4-0- [email protected]

Changes in v4:
- Added patch "hw/pci: Fix SR-IOV VF number calculation" to fix division by zero reported by Yui Washizu.
- Rebased.
- Link to v3:https://lore.kernel.org/r/20240305-sriov-v3-0- [email protected]

Changes in v3:
- Rebased.
- Link to v2:https://lore.kernel.org/r/20231210-sriov-v2-0- [email protected]

Changes in v2:
- Changed to keep VF instances.
- Link to v1:https://lore.kernel.org/r/20231202-sriov- [email protected]

---
Akihiko Odaki (9):
       hw/pci: Do not add ROM BAR for SR-IOV VF
       hw/pci: Fix SR-IOV VF number calculation
       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: Document composable SR-IOV device
       pcie_sriov: Make a PCI device with user-created VF ARI-capable

  MAINTAINERS                    |   1 + docs/system/index.rst          |   1 + docs/system/sriov.rst          |  37 ++++++ include/hw/pci/pci_device.h    |   6 +- include/hw/pci/pcie_sriov.h    |  21 +++ include/hw/virtio/virtio-pci.h |   1 +
  hw/pci/pci.c                   |  76 +++++++---- hw/pci/pcie_sriov.c            | 294 +++++++++++++++++++++++++++++++
++--------
  hw/virtio/virtio-net-pci.c     |   1 + hw/virtio/virtio-pci.c         |  24 +++-

  10 files changed, 378 insertions(+), 84 deletions(-)

---
base-commit: 825b96dbcee23d134b691fc75618b59c5f53da32
change-id: 20231202-sriov-9402fb262be8

Best regards,

Tested-by
#12 lore raw
Quoted 11 lines · Akihiko Odaki
Quoted 6 lines · Yui Washizu

I tested the following features with this patch series, and there were not  issues:
- Creation and deletion of VFs
- Communication with an external machine through VFs

Thank you.

Can you reply with:

OK, Thank you.

I resend it along with the content.

I tested the following features with this patch series, and there were not  issues:
- Creation and deletion of VFs
- Communication with an external machine through VFs

Quoted 165 lines

This tag means:
- you have tested changes you found significant,
- you found no problems with them, and
- you are fine to record that in Git commit logs

Pasha Tatashin (who told me they tried the patches), it would be nice if you do the same.

Regards,
Akihiko Odaki

Quoted 153 lines

Yui

Quoted 147 lines · Akihiko Odaki

Based-on:<[email protected]> ("[PATCH v18 00/14] hw/pci: SR-IOV related fixes and improvements")

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 disables ROM BAR, which virtio-net-pci enables by default, for VFs.
Patch 2 makes zero stride valid for 1 VF configuration. Patch 3 and 4 adds validations.
Patch 5 adds user-created SR-IOV VF infrastructure.
Patch 6 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 7 allows user to create SR-IOV VFs with virtio-net-pci.

[1]https://patchew.org/QEMU/1689731808-3009-1-git-send-email- [email protected]/ [2]https://lore.kernel.org/all/5d46f455- [email protected]/

---
Changes in v9:
- Rebased.
- Link to v8:https://lore.kernel.org/r/20250104-sriov- [email protected]

Changes in v8:
- Rebased.
- Link to v7:https://lore.kernel.org/r/20240813-sriov- [email protected]

Changes in v7:
- Removed #include <error-report.h>, which is no longer needed.
- Rebased.
- Link to v6:https://lore.kernel.org/r/20240802-sriov- [email protected]

Changes in v6:
- Added ARI extended capability.
- Rebased.
- Link to v5:https://lore.kernel.org/r/20240715-sriov- [email protected]

Changes in v5:
- Dropped the RFC tag.
- Fixed device unrealization.
- Rebased.
- Link to v4:https://lore.kernel.org/r/20240428-sriov-v4-0- [email protected]

Changes in v4:
- Added patch "hw/pci: Fix SR-IOV VF number calculation" to fix division
   by zero reported by Yui Washizu.
- Rebased.
- Link to v3:https://lore.kernel.org/r/20240305-sriov-v3-0- [email protected]

Changes in v3:
- Rebased.
- Link to v2:https://lore.kernel.org/r/20231210-sriov-v2-0- [email protected]

Changes in v2:
- Changed to keep VF instances.
- Link to v1:https://lore.kernel.org/r/20231202-sriov- [email protected]

---
Akihiko Odaki (9):
       hw/pci: Do not add ROM BAR for SR-IOV VF
       hw/pci: Fix SR-IOV VF number calculation
       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: Document composable SR-IOV device
       pcie_sriov: Make a PCI device with user-created VF ARI-capable

  MAINTAINERS                    |   1 + docs/system/index.rst          |   1 + docs/system/sriov.rst          |  37 ++++++ include/hw/pci/pci_device.h    |   6 +- include/hw/pci/pcie_sriov.h    |  21 +++ include/hw/virtio/virtio-pci.h |   1 + hw/pci/pci.c                   |  76 +++++++---- hw/pci/pcie_sriov.c            | 294 +++++++++++++++++++++++++++++++ ++-------- hw/virtio/virtio-net-pci.c     |   1 + hw/virtio/virtio-pci.c         |  24 +++-

  10 files changed, 378 insertions(+), 84 deletions(-)

---
base-commit: 825b96dbcee23d134b691fc75618b59c5f53da32
change-id: 20231202-sriov-9402fb262be8

Best regards,

Tested-by on the v9 series
#13 lore raw
In reply to Akihiko Odaki
Quoted 7 lines · Akihiko Odaki

Based-on: <[email protected]> ("[PATCH v18 00/14] hw/pci: SR-IOV related fixes and improvements")

Introduction
------------

Quoted 8 lines

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.

I have been using these patches for live update orchestrator development work. Having virtualized SR-IOV helps tremendously.

Quoted 126 lines

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 disables ROM BAR, which virtio-net-pci enables by default, for VFs.
Patch 2 makes zero stride valid for 1 VF configuration. Patch 3 and 4 adds validations.
Patch 5 adds user-created SR-IOV VF infrastructure.
Patch 6 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 7 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 v9:
- Rebased.
- Link to v8: https://lore.kernel.org/r/[email protected]

Changes in v8:
- Rebased.
- Link to v7: https://lore.kernel.org/r/[email protected]

Changes in v7:
- Removed #include <error-report.h>, which is no longer needed.
- Rebased.
- Link to v6: https://lore.kernel.org/r/[email protected]

Changes in v6:
- Added ARI extended capability.
- Rebased.
- Link to v5: https://lore.kernel.org/r/[email protected]

Changes in v5:
- Dropped the RFC tag.
- Fixed device unrealization.
- Rebased.
- Link to v4: https://lore.kernel.org/r/[email protected]

Changes in v4:
- Added patch "hw/pci: Fix SR-IOV VF number calculation" to fix division by zero reported by Yui Washizu.
- Rebased.
- Link to v3: https://lore.kernel.org/r/[email protected]

Changes in v3:
- Rebased.
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Changed to keep VF instances.
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Akihiko Odaki (9):
hw/pci: Do not add ROM BAR for SR-IOV VF
hw/pci: Fix SR-IOV VF number calculation
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: Document composable SR-IOV device
pcie_sriov: Make a PCI device with user-created VF ARI-capable

 MAINTAINERS                    |   1 +
 docs/system/index.rst          |   1 +
 docs/system/sriov.rst          |  37 ++++++
 include/hw/pci/pci_device.h    |   6 +-
 include/hw/pci/pcie_sriov.h    |  21 +++
 include/hw/virtio/virtio-pci.h |   1 +
 hw/pci/pci.c                   |  76 +++++++----
 hw/pci/pcie_sriov.c            | 294 +++++++++++++++++++++++++++++++++--------
 hw/virtio/virtio-net-pci.c     |   1 +
 hw/virtio/virtio-pci.c         |  24 +++-
 10 files changed, 378 insertions(+), 84 deletions(-)

---
base-commit: 825b96dbcee23d134b691fc75618b59c5f53da32
change-id: 20231202-sriov-9402fb262be8

Best regards,
--
Akihiko Odaki <[email protected]>