Skip to contentSkip to conversation

virtio-net: add support for SR-IOV emulation

RFC PATCH Superseded Akihiko Odaki 2 participants 8 messages Mar 5, 2024 – Apr 10, 2024

v3 of 9 Superseded Latest is v9 →

lore
Share & tools
lore
#0 lore raw

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

Based-on: <[email protected]> ("[PATCH v8 00/15] 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 and 3 adds validations.
Patch 4 adds user-created SR-IOV VF infrastructure.
Patch 5 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 6 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 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 (6):
hw/pci: Do not add ROM BAR for SR-IOV VF
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

 include/hw/pci/pci_device.h |   6 +-
 include/hw/pci/pcie_sriov.h |  19 +++
 hw/pci/pci.c                |  70 +++++++----
 hw/pci/pcie_sriov.c         | 299 +++++++++++++++++++++++++++++++++++---------
 hw/virtio/virtio-net-pci.c  |   1 +
 hw/virtio/virtio-pci.c      |   7 ++
 6 files changed, 319 insertions(+), 83 deletions(-)

---
base-commit: 2c4eb0476e461b8a4b2f745d25f987e831c7f640
change-id: 20231202-sriov-9402fb262be8

Best regards,

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

RFC PATCH v3 1/6 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 cb5ac46e9f27..201ff64e11cc 100644
@@ -2359,6 +2359,14 @@ static void pci_add_option_rom(PCIDevice *pdev, bool is_default_rom,
         return;
     }
 
+    if (pci_is_vf(pdev)) {
+        if (pdev->rom_bar != UINT32_MAX) {
+            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.44.0
#2 lore raw

RFC PATCH v3 2/6 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 09a53ed30027..aac12e70f122 100644
@@ -41,6 +41,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.44.0
#3 lore raw

RFC PATCH v3 3/6 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 aac12e70f122..c449ddd0ac39 100644
@@ -41,6 +41,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.44.0
#4 lore raw

RFC PATCH v3 4/6 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 |  19 +++
 hw/pci/pci.c                |  62 ++++++----
 hw/pci/pcie_sriov.c         | 289 +++++++++++++++++++++++++++++++++++---------
 4 files changed, 293 insertions(+), 83 deletions(-)
include/hw/pci/pci_device.h +5 -1
index 6be0f989ebe0..eefd9d9a7b5a 100644
@@ -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
index d576a8c6be19..20626b5605c9 100644
@@ -18,6 +18,7 @@
 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 */
 };
 
 struct PCIESriovVF {
@@ -40,6 +41,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 +38 -24

112 lines not on this page · raw message

hw/pci/pcie_sriov.c +231 -58

347 lines not on this page · raw message

Signature · 1 line
2.44.0
#5 lore raw

RFC PATCH v3 5/6 virtio-pci: Implement SR-IOV PF

In reply to Akihiko Odaki

Allow 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
index 1a7039fb0c68..f6a2dbb3b5e2 100644
@@ -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.44.0
#6 lore raw

RFC PATCH v3 6/6 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 e03543a70a75..dba4987d6e04 100644
@@ -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.44.0
#7 lore raw
In reply to Akihiko Odaki
Quoted 32 lines · Akihiko Odaki

Based-on: <[email protected]> ("[PATCH v8 00/15] 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
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.

QEMU crashed same as RFC v2:

https://lore.kernel.org/all/[email protected]/T/#mb16bdef667a0b0b7c08f487fcd18e8d627694bf6

Quoted 63 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 disables ROM BAR, which virtio-net-pci enables by default, for VFs.
Patch 2 and 3 adds validations.
Patch 4 adds user-created SR-IOV VF infrastructure.
Patch 5 makes virtio-pci work as SR-IOV PF for user-created VFs. Patch 6 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 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 (6):
hw/pci: Do not add ROM BAR for SR-IOV VF
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

  include/hw/pci/pci_device.h |   6 +-
  include/hw/pci/pcie_sriov.h |  19 +++
  hw/pci/pci.c                |  70 +++++++----
  hw/pci/pcie_sriov.c         | 299 +++++++++++++++++++++++++++++++++++---------
  hw/virtio/virtio-net-pci.c  |   1 +
  hw/virtio/virtio-pci.c      |   7 ++
  6 files changed, 319 insertions(+), 83 deletions(-)

---
base-commit: 2c4eb0476e461b8a4b2f745d25f987e831c7f640
change-id: 20231202-sriov-9402fb262be8

Best regards,