Technical Blog

Linux networking internals: small packets, VFIO, OVS-DPDK and SR-IOV

Almost all the work we followed in part 3 is done once per packet, no matter how big the packet is. With small packets, and with VMs, that cost adds up quickly, and the usual answer is to take the host kernel out of the data path.

Small packets#

Packet size is a trade-off. Small packets get through each hop faster, so latency is lower, but you need a lot more of them to move the same data, so throughput goes down. NFV and VoIP like small packets. Jumbo frames go the other way, with better throughput but more time to send each packet. Usually you want something in between, where packet size doesn't hurt latency too much and you can still fill a 10 gig link.

A 64-byte packet costs about as much to push through the stack as a 1500-byte one. A 10G link full of 64-byte frames carries 14.88 million packets a second, which leaves roughly 67 nanoseconds for each packet. With small packets you want the network stack involved as little as possible.

VMs make it worse#

A host with NIC, driver, TX/RX rings and packet buffers in kernel space, and a VM in user space that has its own complete network stack and application
In a VM, a packet goes through the host's stack and then the guest's stack.

Most deployments are virtualized now (OpenStack, VMware and so on), and every VM runs its own network stack. A packet arriving at the server goes through the host's driver and stack, then the virtual switch, and then the guest's whole stack before it reaches the application. Both stacks run for every packet, and small packets multiply that cost.

Getting line rate into a VM with small packets is really hard. A few years ago I tried to fill a VM's 10G link with 64-byte packets and never got there. Inside the VM we topped out at 2 to 3 Gbit/s.

Traditional OpenStack VM networking: the VM's vNIC connects through tap, qbr, qvb/qvo to br-int (VLAN ID) and br-tun; on the right, QEMU with the guest virtio driver, vhost-net, tun/tap, Linux bridge, veth and the OVS forwarding plane in kernel space, then the NIC driver and NIC
A VM's network in OpenStack: the logical layout on the left, where each piece runs on the right.

On a typical OpenStack compute node with OVS, the VM's vNIC is plugged into a tap device, which goes into a Linux bridge (qbr) where the iptables security groups live. A veth pair (qvb/qvo) connects that bridge to the OVS integration bridge (br-int), a patch port goes from there to br-tun for VXLAN, and then the packet leaves through the NIC driver. Underneath, the guest's virtio driver hands the traffic to vhost-net in the host kernel, and from there it goes through tun/tap, the host network stack, the bridge module, the veth pair and the OVS kernel datapath. That's a lot of hops for every packet. (I went through this path hop by hop in How packets actually flow in OpenStack.)

The host still needs its own network stack for its own traffic. For the VMs' traffic, though, running a full second stack on the host doesn't buy you much, which is why people bypass it.

VFIO and the IOMMU#

Two layouts: on the right, the traditional one with the driver, TX/RX rings and packet buffers in kernel space; on the left, only VFIO in kernel space, with TX/RX rings and packet buffers in user space
Right: the normal layout. Left: with VFIO, the rings and buffers move to user space.

Bypassing the host stack means moving the NIC into user space. After that the host can't see the NIC anymore, and whatever owns it in user space has to do the driver's job. It has to talk to the NIC's registers, handle its interrupts and set up memory for it to DMA into.

The DMA part is the risky one. User space isn't privileged, and a process that can point a NIC's DMA engine anywhere it likes can overwrite the kernel. VFIO and the IOMMU are there to prevent that. Someone once described VFIO to me as poking a hole in the kernel, which is about right.

VFIO (vfio-pci) is a passthrough-style driver that lets a user-space process own a device. It maps the device's registers into the process, maps memory for DMA and delivers interrupts as file descriptors. The IOMMU (Intel VT-d or AMD-Vi) is the hardware that makes this safe. It translates the addresses the device uses and only lets the device reach memory that has been mapped for it. The older UIO did the same job without any of that protection.

# enable the IOMMU on the kernel command line (Intel):  intel_iommu=on iommu=pt
dmesg | grep -iE 'DMAR|IOMMU'
ls /sys/kernel/iommu_groups/

# give a NIC to VFIO (it disappears from the host)
modprobe vfio-pci
dpdk-devbind.py --status
dpdk-devbind.py --bind=vfio-pci 0000:3b:00.0

VFIO only gets the NIC into user space. Something there still has to drive it and do what the network stack used to do, and you can't give every VM its own physical NIC anyway. The two common ways to handle that are OVS-DPDK and SR-IOV.

DPDK and OVS-DPDK#

DPDK (the Data Plane Development Kit) started at Intel. It's a framework for fast packet processing in user space. Its poll-mode drivers (PMDs) run on dedicated cores and poll the NIC's rings all the time instead of waiting for interrupts. Packet buffers live in huge pages, so there are far fewer TLB misses, and the kernel isn't in the data path at all, so there are no syscalls and no skbs. The downside is that the PMD cores run at 100% even when the link is idle. And since the kernel doesn't know the NIC exists anymore, ip link, iptables and a plain tcpdump can't see it.

DPDK-enabled Neutron OVS deployment: the VM's vNIC attaches to br-int through vhost-user, br-int patches to br-tun, and br-phy holds the dpdk port. On the right, QEMU's guest virtio driver connects to vhost-user in ovs-vswitchd, whose PMD threads drive the dpdk NIC through VFIO
OVS-DPDK in Neutron: the guest talks to ovs-vswitchd over vhost-user, and PMD threads drive the NIC through VFIO.

OVS-DPDK is Open vSwitch with its datapath moved into user space, inside ovs-vswitchd. QEMU talks to it over vhost-user (shared memory between the guest and ovs-vswitchd) instead of vhost-net and a tap device, and the PMD threads move packets between the VMs and the physical NIC. Compared to the earlier diagram, the whole kernel half is gone. You can push much smaller packets with much lower latency, and it works for anything attached to OVS, not only VMs. A minimal setup looks like this:

ovs-vsctl set Open_vSwitch . other_config:dpdk-init=true
ovs-vsctl set Open_vSwitch . other_config:dpdk-socket-mem="1024,1024"
ovs-vsctl set Open_vSwitch . other_config:pmd-cpu-mask=0x6

ovs-vsctl add-br br-phy -- set bridge br-phy datapath_type=netdev
ovs-vsctl add-port br-phy dpdk0 -- set Interface dpdk0 type=dpdk \
    options:dpdk-devargs=0000:3b:00.0

ovs-appctl dpif-netdev/pmd-rxq-show
ovs-appctl dpif-netdev/pmd-stats-show

PMDs are pinned one per core, so as you add VMs and traffic you'll probably have to give OVS more cores.

SR-IOV#

Two VMs, each with a vNIC wired directly to one of several virtual functions (VF) on a single NIC
SR-IOV: each VM gets its own virtual function on the NIC.

SR-IOV does the virtualization on the NIC itself. One NIC shows up as a physical function (PF), which the host driver manages, plus a number of virtual functions (VFs). Each VF is passed through to a VM (with VFIO, so the IOMMU still protects the host), and the guest loads a normal VF driver for it. There's no host stack and no OVS in the path.

cat /sys/class/net/eth0/device/sriov_totalvfs
echo 8 > /sys/class/net/eth0/device/sriov_numvfs
ip link show eth0                                # vf 0..7 with MAC, VLAN, spoof checking
ip link set eth0 vf 0 vlan 100 spoofchk on

In OpenStack that's a port with vnic_type=direct (I wrote about the Nova side in PCI passthrough: Type-PF, Type-VF and Type-PCI). What you give up is flexibility. The VM is tied to the hardware, which makes live migration harder, and security groups and everything else OVS does are no longer in the path. The number of VFs also limits how many VMs can use the card. The Intel NICs I used had 64 per port, and newer cards support a lot more.

Side by side. Left: QEMU guest virtio driver to vhost-user in ovs-vswitchd, PMD threads, VFIO in kernel space and the DPDK NIC. Right: QEMU guest VF driver directly to an SR-IOV VF, with the PF driver remaining in kernel space
OVS-DPDK on the left, SR-IOV on the right.

To compare the two: with OVS-DPDK, VFIO gives the whole NIC to user space and OVS splits it between the VMs in software. With SR-IOV, the PF stays with the host and only the VFs go to the guests, which see what looks like a normal NIC and drive it themselves. If you need switching on the host, or you're writing your own packet-processing application, you'd normally go with DPDK or OVS-DPDK. If you just want your VMs to go fast, VFs are the simpler option.