SR-IOV Explained: Virtual Functions in PCI Drivers
How SR-IOV splits one PCIe device into virtual functions on Linux 6.12, how to enable them from sysfs, and how a PF driver supports sriov_configure.

A network card in a virtualization host has a problem. Giving every virtual machine an emulated NIC costs CPU for each packet, and giving each VM a whole physical card does not scale. SR-IOV, Single Root I/O Virtualization, solves it in hardware: one physical PCIe device presents itself as a few full-featured physical functions and many lightweight virtual functions, each with its own PCI address, BARs, queues and interrupts. A VM gets a virtual function through VFIO and drives it directly, with the IOMMU keeping it isolated. This post explains the model, shows how to create virtual functions from sysfs on Linux 6.12, and walks the driver side: the sriov_configure callback and what the PF driver must still do for its children. It builds on VFIO and the IOMMU.
The specification is the SR-IOV specification from PCI-SIG, and the kernel's guide is the SR-IOV HOWTO.
Physical and virtual functions
An SR-IOV device has two kinds of function.
A physical function (PF) is a normal PCI function with the SR-IOV extended capability in its configuration space. It is the one that controls the device: it can configure the resources shared by all functions, enable or disable the virtual functions, and set policy for each one, such as its MAC address, VLAN or rate limit. The PF driver is usually the vendor's full driver and runs on the host.
A virtual function (VF) is a lightweight function that shares the hardware with the PF. It has its own configuration space, its own BARs, its own set of queues and its own interrupt vectors, but only the registers needed for data transfer. It cannot change global device settings. It has a different device ID from the PF, such as 1520 for an Intel I350 VF, and the host or a guest binds a small driver to it.
The capability has a few fields that fix how many VFs can exist and where they appear:
| Field | Meaning |
|---|---|
TotalVFs | The maximum number of VFs the device supports |
NumVFs | The number currently enabled |
First VF Offset | The routing ID of VF 0, relative to the PF's |
VF Stride | The routing ID distance between consecutive VFs |
The offset and stride let the VFs get PCI addresses with no extra bus numbers in the hardware: VF n has the routing ID of the PF plus First VF Offset plus n times VF Stride. A PF at 02:00.0 with an offset of 128 and a stride of 4 has its first VF at 02:10.0, its second at 02:10.4, then 02:11.0, and so on. The kernel may need to allocate extra bus numbers for a large VF count, which is why the capability also reports its requirements to the bus assignment code.
Enable virtual functions
Everything is controlled through two files in the PF's sysfs directory:
cat /sys/bus/pci/devices/0000:02:00.0/sriov_totalvfs
echo 4 | sudo tee /sys/bus/pci/devices/0000:02:00.0/sriov_numvfs
lspci -nn | grep -i "virtual function"7
4
02:10.0 Ethernet controller [0200]: Intel Corporation I350 Ethernet Controller Virtual Function [8086:1520] (rev 01)
02:10.4 Ethernet controller [0200]: Intel Corporation I350 Ethernet Controller Virtual Function [8086:1520] (rev 01)
02:11.0 Ethernet controller [0200]: Intel Corporation I350 Ethernet Controller Virtual Function [8086:1520] (rev 01)
02:11.4 Ethernet controller [0200]: Intel Corporation I350 Ethernet Controller Virtual Function [8086:1520] (rev 01)The device supports at most 7 VFs, and the second line is tee echoing the 4 that was written. The four new functions appear on the bus with their own addresses and the VF device ID. Each is a regular PCI device in the kernel, with its own lspci entry and its own entry under /sys/bus/pci/devices/. To change the count you must first write 0, since the kernel refuses to go from one non-zero count to another:
echo 0 | sudo tee /sys/bus/pci/devices/0000:02:00.0/sriov_numvfsDisabling VFs while a VM is using one tears it away from the guest, so shut the guests down or detach the devices first.
From the host, the PF's driver can configure a VF. For a network PF, the ip tool uses the netlink VF interface:
sudo ip link set dev eth1 vf 0 mac 02:00:00:00:00:10
sudo ip link set dev eth1 vf 0 vlan 100
ip link show dev eth13: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:1b:21:aa:bb:cc brd ff:ff:ff:ff:ff:ff
vf 0 link/ether 02:00:00:00:00:10 brd ff:ff:ff:ff:ff:ff, vlan 100, spoof checking on, link-state auto, trust off
vf 1 link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff, spoof checking on, link-state auto, trust offSetting the MAC and VLAN on the PF side is the security point of SR-IOV: the guest's VF driver cannot override a MAC or VLAN that the host assigned, and spoof checking drops frames with a source address the host did not allow.
The PF driver's role
The core of SR-IOV in a driver is one callback and two functions. The PCI core exposes the sriov_numvfs file for every PF whose driver implements sriov_configure:
static int sample_sriov_configure(struct pci_dev *pdev, int num_vfs)
{
int ret;
if (num_vfs == 0) {
if (pci_vfs_assigned(pdev)) {
dev_warn(&pdev->dev, "VFs are assigned to guests\n");
return -EPERM;
}
pci_disable_sriov(pdev);
sample_free_vf_resources(pdev);
return 0;
}
ret = sample_alloc_vf_resources(pdev, num_vfs);
if (ret)
return ret;
ret = pci_enable_sriov(pdev, num_vfs);
if (ret) {
sample_free_vf_resources(pdev);
return ret;
}
return num_vfs;
}
static struct pci_driver sample_driver = {
.name = "sample_pf",
.id_table = sample_ids,
.probe = sample_probe,
.remove = sample_remove,
.sriov_configure = sample_sriov_configure,
};The contract matters. The callback returns the number of VFs enabled on success, which becomes the value sriov_numvfs reports, and zero when it disabled them. pci_enable_sriov(pdev, num_vfs) creates the VF devices: it programs the capability, makes the VFs appear on the bus and registers them, so the kernel's PCI core probes each VF like any other device. It fails if num_vfs exceeds TotalVFs, or if the bus numbering cannot fit them. pci_disable_sriov() removes them.
Before disabling, check pci_vfs_assigned(), which reports whether any VF is currently attached to a VM through VFIO. Pulling a VF out from under a running guest crashes the guest's driver, so a conscientious PF driver refuses with -EPERM, as above. What the driver allocates for its VFs depends on the hardware: queue pairs and interrupt vectors are split between the PF and its children, and the PF driver programs that split before enabling.
Remove must also tidy up. A PF driver's remove should call pci_disable_sriov() if VFs are still enabled, since the VFs cannot exist without a PF. The core does call the disable for you in some paths, but an explicit call is clearer.
The VF side
A VF is bound to a driver like any other PCI device. For a network VF, the vendor ships a small driver, for example igbvf for Intel I350, that has no way to change global state and talks to the PF over a mailbox for the few operations that need the host's consent, such as joining a multicast group or requesting a MAC. The VF driver checks that its device is a VF at probe time. In its own code, pdev->is_virtfn is true for a VF, which lets a shared driver adjust behavior.
For assignment to a VM, the host unbinds the VF from its driver and binds it to vfio-pci, as in the VFIO post. The guest then loads the VF driver on the passed-through device and sees what looks like a dedicated network card, without the emulation cost. Each VF is usually in its own IOMMU group on hardware that supports ACS properly, which is the reason SR-IOV is sold on isolation.
Limits and trade-offs
SR-IOV moves work into hardware, and it has costs. The host loses visibility into a VF's traffic, so features that rely on the host switching packets, such as software firewalls and live migration, need either hardware support or a fallback path. Live migration of a guest with a VF is hard because the device state is in hardware, which the hypervisor cannot copy.
The number of VFs is limited by TotalVFs and by the device's internal resources, such as queues and interrupts, and each VF takes a little of both. A device with 64 VFs divides its queues 64 ways, and a heavy guest sees what its share allows. Memory use grows too, since each VF has its own BAR windows, and the kernel needs enough PCI address space for all of them. If the firmware did not reserve that space, enabling VFs fails with a log line about not enough MMIO resources, and the fix is a firmware setting that enables SR-IOV with a large BAR region or a boot parameter that lets the kernel reassign resources.


