Part 2 of 6 · 2 min read
Device models
Think of three ways to give a visitor a telephone. The first is a toy telephone that looks like the real thing, and an operator in the next room answers every button press. The second is a plain intercom with a short list of messages that both sides agree on. The third is a real telephone line given to the visitor alone. The analogy stops in one place: the visitor with the real line can dial anyone, and a guest with a real device can also read other memory unless a protection step stops it.
In plain words: There are three models.
| Model | How the guest's driver talks | Speed | Sharing |
|---|---|---|---|
| Emulation | The guest uses the driver for a real old card (an IDE disk, an e1000 network card). Each register access traps and the hypervisor pretends to be the card | Slow: many exits | Many guests share one real device |
| Paravirtual (virtio) | The guest uses a driver written for virtual devices. It talks through shared memory queues and one notification | Fast: few exits | Many guests share one real device |
| Passthrough | The guest gets the real device, and its driver talks to the hardware | Near native | One guest per device (or per slice, with SR-IOV) |
Emulation needs no change in the guest, and it is the slowest. Virtio needs a driver in the guest, which every modern Linux has. Passthrough needs hardware help: an IOMMU (a memory management unit for devices) that limits where the device may do DMA (Level 8) so that a guest's device cannot write another guest's memory. SR-IOV lets one physical device present itself as many small virtual functions, one for each guest.
A worked example. To read one 4 KiB block, an emulated IDE disk takes about 4 trapped register writes (sector, count, command, and one to start the DMA) and then an interrupt. Each write is a VM exit. A virtio-blk driver writes the request into a shared queue and sends one notification, which is one exit. So the same read costs 4 exits under emulation and 1 under virtio, and the next section shows how batching reduces the 1 as well.
The key lines. The model for the system disk and network of almost every cloud guest is virtio. Passthrough is used for the few devices that must be fast, such as a GPU or a network card of 100 Gbit/s.
Paravirtual device
A paravirtual device is a virtual device whose interface is designed for virtual use, so that its guest driver and the hypervisor exchange requests through shared memory with few traps, in place of imitating the registers of a real card.
Example: virtio_blk is the driver of the virtual disks of the test machine.
Check yourself: 1. Why is an emulated device slow? 2. What does virtio need that emulation does not? 3. What does an IOMMU protect against in passthrough?