问题原文
原帖链接 · panxl · CC BY-SA 4.0
I am doing a research about QEMU/KVM and Firecracker/KVM. As far as I understood, both Firecracker and QEMU communicates with KVM to eventually benefit hardware assisted virtualization by changing CPU mode to guest to host and vice versa.
In guest mode of the CPU, guest can directly execute even its privileged instructions so why do we also need paravirtualization?
In Firecracker, only 5 devices emulated, such as
- virtio-net,
- virtio-block,
- virtio-vsock and so on.
Even in this minimalist design, we have to put paravirtualization drivers. Can't we just depend on hardware assisted virtualization?
已采纳解答
原帖链接 · Austin Hemmelgarn · CC BY-SA 4.0
Consider just the case of networking.
To actually be useful in most cases, a VM needs to be able to communicate over the network. For that, the guest obviously has to see some sort of network interface. But VT-x and AMD-V and ARM VHE and pretty much all other hardware virtualization implementations don’t provide NICs, they just give you a way to securely segregate and partition out CPU resources. So hardware virtualization does nothing to give you a network interface.
Now, you could pass through a physical network interface from the host system, but that has a number of issues:
- It requires special handling at boot time for the host OS to ensure that it doesn’t actually bind a driver to that interface.
- It requires special support in the hardware to be secure (you need an IOMMU, and the NIC you want to pass through has to support running behind an IOMMU).
- It requires actually having a usable network interface for each VM. This obviously means that anybody on a laptop would be out of luck, but it also means that most large scale VM hosting providers would be out of luck as well (they may be running many dozens of VMs on the same host).
- It makes live migration functionally impossible without RDMA hardware and a lot of additional complexity (just like any other direct device pass-through does).
- In the case of VMs needing to talk to each other, it inherently introduces an external point of failure (because they have to send traffic through a network switch outside of the system).
So you obviously need to emulate a network interface somehow. The obvious choice would be to just pick a commonly used physical NIC and emulate that. But that has it’s own set of issues:
- A lot of what a physical NIC does is rather computationally expensive to emulate. It’s only efficient in a physical implementation because it’s using physical logic and ASICs.
- A majority of the same stuff that’s expensive to emulate isn’t even needed for a VM, but you can’t avoid emulating it because the drivers will expect it to work.
- A lot of additional complexity is needed in the guest drivers to support this stuff that isn’t even giving any real benefit. For perspective, the Intel e1000 (a commonly emulated physical NIC) driver for Linux is about 17k lines of code, while the virtio-net driver is only 4.8k (7.3k if you include the virtio-pci components that it is probably using on x86 systems).
virtio-net solves those issues, it only covers the things that are actually needed to move network packets between the guest OS and the host networking layer, and nothing more. And solving those issues provides a huge performance improvement. I have not tested recently, but the last time I compared it using QEMU, virtio-net provided more than twice the effective bandwidth of an emulated e1000 card, and roughly 1/10th of the latency, all with lower CPU usage on the host side.
The same logic applies for most other devices. Some stuff can be emulated relatively inexpensively or is not performance critical and thus doesn’t need to be efficient (this is why there is no VirtIO watchdog timer for example, it’s not performance critical, and it’s trivial to emulate in most cases), but for most things that don’t fit those criteria there is a paravirt option because the performance difference is huge and the reduced complexity tends to make things more reliable.
And sometimes paravirtualization lets you do things you couldn’t really do with ‘regular’ hardware. VirtIOFS and the VirtIO transport for 9P2000 are prime examples of this, they have no hardware analogues, but provide a reasonably efficient way to share files between the host and guest without needing to emulate a network or a block device.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
