architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · qemu code overview...

23
QEMU Code Overview Architecture & internals tour Stefan Hajnoczi <[email protected]>

Upload: dangxuyen

Post on 06-Feb-2018

253 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi

QEMU Code OverviewArchitecture & internals tour

Stefan Hajnoczi <[email protected]>

Page 2: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi

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

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

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

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

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

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

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

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

ArchitectureHow it fits together

Page 11: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi

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

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

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

Device emulationImplementing guest hardware

Page 15: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi

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

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

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

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

DevelopmentContributing to QEMU

Page 20: Architecture & internals tour - vmsplice.netstefan/qemu-code-overview.pdf · QEMU Code Overview Architecture & internals tour Stefan Hajnoczi

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

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

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

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