The idea: move a running VM and pause it only briefly
A cloud provider sometimes has to empty a host: to patch it, to replace a part, or to spread the load. Live migration moves a running virtual machine to another host while it keeps working. Its users notice at most a short pause, the downtime. The hard part is the memory: gigabytes that the guest keeps changing while they are copied.
The page follows the pre-copy design of Clark et al. (NSDI 2005), which QEMU/KVM, Xen and VMware all use, and shows post-copy as the alternative. The canvas draws two KVM hosts joined by a 10 Gbit/s link. Each host shows the guest's 4 GB of memory as 250 squares of 4,000 pages (16 MB each).
What has to move
- Memory: 4 GB here, 1,000,000 pages of 4 KB. This is almost all of the work.
- CPU and device state: registers of each vCPU, timers, the state of the virtual disk and network cards. A few megabytes; the page uses 8 MB.
- The disk: in a cloud it usually sits on shared network storage that both hosts can reach, so it does not move.
- The network address: the VM keeps its MAC and IP address. After the move the target sends a gratuitous ARP (QEMU sends RARP announce packets) so that the switch sends the VM's traffic to the new port. Open TCP connections survive.
Pre-copy rounds and the dirty bitmap
The target starts QEMU with -incoming and waits. The source turns on dirty logging: KVM write-protects the guest's memory, and every write the guest makes sets one bit in a dirty bitmap, one bit per page. Then the rounds begin:
- Round 0 sends every page. The guest keeps running and writing.
- Each later round reads and clears the bitmap and sends only the pages marked in it: the pages written since they were last sent. On the target, the old copies of those pages are stale (yellow on the canvas).
- When the pages left are few enough to send within the downtime budget, the source pauses the guest (stop-and-copy), sends them with the CPU state, and the guest resumes on the target.
Demo: pre-copy converges (database VM). The database writes 400 MB/s (100,000 pages per second) to a working set of 1.2 GB. The link carries 1,000 MB/s, so each round lasts 0.4 times as long as the one before:
| Round | Pages sent | Time | Dirtied meanwhile | Time to send those |
|---|---|---|---|---|
| 0 | 1,000,000 | 4 s | 300,000 | 1.2 s > 300 ms |
| 1 | 300,000 | 1.2 s | 120,000 | 480 ms > 300 ms |
| 2 | 120,000 | 480 ms | 48,000 | 192 ms ≤ 300 ms ✓ |
| stop | 48,000 + 8 MB state | 200 ms | guest paused | downtime 200 ms |
The whole move takes 5.88 s and sends 5,880 MB for a 4 GB VM. The guest is paused for only 200 ms of it. In round 0 the dirtied pages stop at 300,000 although the guest writes for 4 s: it keeps rewriting the same 1.2 GB, its writable working set. Demo: idle web server: one round is enough writes only 20 MB/s, so after round 0 just 20,000 pages are dirty: 88 ms of downtime, 4.088 s in all.
When pre-copy converges, and when it does not
Each round sends what the guest dirtied during the round before. With dirty rate D and link speed B, a round lasts about D / B times as long as the previous one, until the dirtied pages reach the whole working set. So the rounds shrink only while the guest writes slower than the link carries.
Demo: write-heavy VM never converges runs a memory benchmark that writes 1,200 MB/s over a 2.4 GB working set. That is faster than the 1,000 MB/s link, so every round ends with the whole working set dirty again: 600,000 pages, 2.4 s to send. QEMU would keep trying for ever. The page gives up after 6 rounds (Clark et al.'s Xen stopped after 30) and forces the switch: 2.408 s of downtime, 8 times the limit, after 18.408 s and 18,408 MB sent, 4.6 times the VM's memory.
The QEMU/KVM knobs
| Parameter | What it does | On the page |
|---|---|---|
downtime-limit | The longest pause QEMU accepts. It switches when the rest can be sent within it. Default 300 ms. | 300, 100 or 30 ms |
max-bandwidth | Caps the migration stream, so the VM's own traffic still gets through. | 1,000 MB/s on a 10 Gbit/s link |
auto-converge | Slows the guest's vCPUs down step by step (cpu-throttle-initial, cpu-throttle-increment) until it dirties less than the link carries. | not drawn |
postcopy-ram | Allows switching to post-copy (migrate-start-postcopy). | method: post-copy |
A smaller downtime-limit means more rounds. Demo: tighter downtime-limit (30 ms) moves the database VM in 6 rounds instead of 3: downtime 20.3 ms instead of 200 ms, but 6 s and 6,000 MB in all. On the QEMU monitor a migration looks like this:
(target) qemu-system-x86_64 ... -incoming tcp:0:4444
(source) migrate_set_parameter downtime-limit 300
migrate_set_parameter max-bandwidth 1000M
migrate -d tcp:target-host:4444
info migrate
Tools such as libvirt (virsh migrate --live) and OpenStack drive the same commands.
Post-copy: switch first, fetch pages on fault
Post-copy turns the order around. It pauses the guest, sends only the CPU and device state (8 ms), and resumes the guest on the target with none of its memory there. The source then pushes the pages in the background. When the guest touches a page that has not arrived, it takes a page fault over the network: the target (through Linux's userfaultfd) asks the source for that page first, and the vCPU waits for the answer.
Demo: post-copy for the write-heavy VM moves the VM that pre-copy could not:
| Write-heavy VM | Pre-copy | Post-copy |
|---|---|---|
| downtime | 2.408 s ✗ | 8 ms ✓ |
| total time | 18.408 s | 4.008 s |
| data sent | 18,408 MB | 4,008 MB: each page once |
| cost while moving | none: the guest runs at full speed on the source | 101 network faults; the guest waits 2.013 s in all |
Post-copy always finishes, in one pass over memory. Its price is a slow guest right after the switch: every first touch of a missing page costs a network round trip instead of a memory access. In the database VM the guest waits 1.024 s over 51 faults. Many tools start with pre-copy and switch to post-copy only if it does not converge.
When the link fails
The two methods fail very differently. In Demo: link fails during pre-copy the link breaks halfway through round 1. Nothing is lost: the source still holds the whole, current VM, which never stopped. The target throws its copy away, and the downtime is 0. In Demo: link fails during post-copy the link breaks after 1.6 s. The guest already runs on the target, but 150 of its 250 squares are still on the source. Its next fault cannot be served, so QEMU stops it (postcopy-paused). It can resume with migrate-recover when the link comes back; if the source host has died, the VM is lost.
Where the other pages fit
How the guest runs on the CPU, and how KVM and QEMU split the work, is in hypervisor VM exits; dirty logging works by write-protecting guest pages in the EPT shown there. Containers vs VMs compares a VM with a container, and cloud service models shows the IaaS rentals that providers move around like this. Pages and page faults inside one machine are in virtual memory, and the gratuitous ARP in ARP.
What the page leaves out
Real dirty rates change from second to second; the page keeps them fixed and uses decimal toy units (4 GB = 1,000,000 pages of 4 KB). A grid square stands for 4,000 pages; in post-copy a fault fetches the whole square in one 16 ms slot, while QEMU fetches a 4 KB page (or a 2 MB huge page) and prefetches its neighbours. Real QEMU also skips zero pages, can compress or send only changed bytes (xbzrle), uses several streams (multifd), and estimates the bandwidth instead of knowing it. Moving the disk as well (block migration), auto-converge in action, the cost of dirty logging on the guest, and migration between hosts with different CPUs are not shown.