What is virtio, and why is it faster than emulating a real device?
Short answer: Virtio is a standard for paravirtual devices: instead of emulating a real network card or disk controller register by register, the guest uses a simple driver that puts requests into shared-memory ring buffers (virtqueues) and notifies the host once per batch. This avoids most VM exits. Common devices are virtio-net, virtio-blk, virtio-scsi, virtio-gpu, virtio-console, virtio-rng and virtio-vsock.
How a virtqueue works
- A descriptor table of buffers (guest physical address, length, flags), an available ring where the guest posts descriptor indexes, and a used ring where the device returns completed ones (the "split" layout; virtio 1.1 adds a "packed" layout).
- The guest adds buffers, then kicks the device with one MMIO/PCI write (one exit, or none if the device is polling).
- The device processes many buffers, writes them to the used ring and raises one interrupt.
- Both sides can suppress notifications while busy, so under load there are very few exits per packet.
Why emulated hardware is slow: a real NIC driver performs many register reads and writes per packet; each is an MMIO exit costing microseconds. Virtio was designed for virtualization, so it needs very few.
Making it faster still
- ioeventfd: the guest's kick write is turned into an eventfd signal inside KVM, without returning to QEMU's vCPU thread.
- irqfd: the device raises the guest interrupt by writing an eventfd that KVM injects directly.
- vhost: the data path runs in a host kernel thread (
vhost-net) or another process (vhost-user, e.g. with DPDK), so packets do not go through QEMU at all. - vDPA: real hardware that speaks the virtio ring format, so the guest's virtio driver drives hardware directly.
Transports: virtio-pci on PC-like machines and the ARM virt PCIe bus, virtio-mmio on simple embedded boards (described in the device tree), and virtio-ccw on s390.
Embedded and automotive: virtio is the basis of the VirtIO-based sharing of GPUs, audio, cameras and sensors between VMs in automotive hypervisors, standardised so the same guest drivers work on different hypervisors.