Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview...
Transcript of Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview...
![Page 2: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/2.jpg)
Covered topics
Enough details about QEMU to:● Understand how components fit together● Build and start contributing● Debug and troubleshoot
Too little time to step through source code,follow code references if you want to know more
![Page 3: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/3.jpg)
What is QEMU?
Emulates x86, ARM, PowerPC, and other machines
Used for virtualization with KVM and Xen
Written in C, runs on POSIX and Windows hosts
Code at qemu-project.org under GPLv2
![Page 4: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/4.jpg)
External interfacesInteracting with the outside world
![Page 5: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/5.jpg)
Command-line options
Guest is defined on command-line:qemu -m 1024 \ -machine accel=kvm \ -hda web-server.img
man qemu for most options
See qemu-options.hx and vl.c:main() for implementation
![Page 6: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/6.jpg)
QMP monitor
JSON RPC-like API for managing QEMU:● Hotplug devices● Stop/continue guest● Query device information● etc
Write custom scripts with QMP/qmp.py
See qapi-schema.json and QMP/
![Page 7: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/7.jpg)
HMP monitor
Text-based interface for managing QEMU
Superseded by QMP but handy for interactive sessions
See hmp-commands.hx
![Page 8: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/8.jpg)
User interfaces
Remote UIs include VNC and SPICE
Local UIs include GTK and SDL
See ui/
![Page 9: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/9.jpg)
Logging
Errors and warnings go to the monitor, if currently running a command
Otherwise they are printed to stderr
![Page 10: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/10.jpg)
ArchitectureHow it fits together
![Page 11: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/11.jpg)
QEMU process model
Host Kernel
Guest RAM
QEMU
QEMU is a userspace process
QEMU owns guest RAM
Each KVM vCPU is a thread
Host kernel scheduler decides when QEMU and vCPUs run
Can use ps(1), nice(1), cgroups
![Page 12: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/12.jpg)
Main loop
QEMU is event-driven, has async APIs for:● File descriptor is readable or writeable● Timer expiration● Deferred workGlobal mutex protects QEMU code● No need to synchronize explicitly● Gradually being removed to improve
scalability
See include/qemu/main-loop.h
![Page 13: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/13.jpg)
Architecture summary
Main loop● Monitor● UI● Host I/O
completion● Deferred
work● Timers
Host kernelKVM, host I/O, scheduling, resource limits
vCPU #0● Run guest
code● Device
emulation
vCPU #1● Run guest
code● Device
emulation
![Page 14: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/14.jpg)
Device emulationImplementing guest hardware
![Page 15: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/15.jpg)
Hardware emulation model
Accelerators run guest code:● KVM uses hardware assist (VMX/SVM)● TCG does binary translation
Devices implement guest hardware:● See hw/ for code● List available devices: qemu -device \?
![Page 16: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/16.jpg)
KVM accelerator pseudo-codeopen("/dev/kvm")ioctl(KVM_CREATE_VM)
ioctl(KVM_CREATE_VCPU)for (;;) { ioctl(KVM_RUN) switch (exit_reason) { case KVM_EXIT_IO: /* ... */ case KVM_EXIT_HLT: /* ... */ }}
![Page 17: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/17.jpg)
Guest/host device split
Guest devices simulate real hardware● Net example: e1000 PCI adapter● Disk example: virtio-blk device
Host devices implement I/O● Net example: tap device● Disk example: GlusterFS backend
This allows flexible guest/host device pairing
![Page 18: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/18.jpg)
Guest device emulation
Devices have memory or I/O regionsMust implement read/write handler functions
Devices can raise interrupts to notify guest
Inspect devices using info qtree
Inspect memory regions using info mtree
![Page 19: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/19.jpg)
DevelopmentContributing to QEMU
![Page 20: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/20.jpg)
git clone git://git.qemu-project.org/qemu.git
![Page 21: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/21.jpg)
Build process
./configure shell script detects library dependencies
Check ./configure output to confirm optional features are enabled
Only build x86_64 guest support with --target-list=x86_64-softmmu
![Page 22: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/22.jpg)
Contributing
Specifications and documentation, see docs/
Read CODING_STYLE and HACKING
Use scripts/checkpatch.pl to scan your patches
More info:http://qemu-project.org/Contribute/SubmitAPatch
![Page 23: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi](https://reader031.fdocuments.in/reader031/viewer/2022021421/5a79a51d7f8b9a9e0c8b61c1/html5/thumbnails/23.jpg)
Where to find out more
More QEMU architecture overview on my blog:http://goo.gl/sdaVV
Read the code, documentation is sparse
Mailing list: [email protected]: #qemu on irc.oftc.net