Getting started¶
Without building¶
The quickest way is the container image, which has everything prebuilt:
docker run --rm -it --device /dev/kvm ghcr.io/oguzhankynk/qemu-ntb
It opens two VMs in tmux with ntb_netdev between them (see
demo below for what to try). Without /dev/kvm the VMs run
under TCG, QEMU's CPU emulation.
In a checkout, scripts/get-prebuilt.sh downloads the same bundle from the
GitHub release (and checks its SHA-256 sum) and unpacks it where a source
build would put things:
| Path | Contents |
|---|---|
third_party/install-qntb/ |
QEMU 11.1.3 with qntb (x86-64 system emulator) |
third_party/build/linux/arch/x86/boot/bzImage |
guest kernel 7.3.6 |
third_party/kbuild/ |
kit to build modules for that kernel (make -C third_party/kbuild M=...) |
out/initramfs.cpio.gz |
guest initramfs (busybox, the kernel's NTB modules) |
driver/ntb_hw_qntb.ko |
the guest driver |
Then scripts/demo.sh, scripts/run-pair.sh and scripts/run-single.sh
work with the QEMU device; driver/ and tests/kmod/ build against the
kit. run-all.sh and ntbd still need a source build. The bundle is
built and tested by .github/workflows/release.yml.
From source¶
This section builds everything from source and brings up a link between two
VMs. It assumes an x86-64 Linux host with KVM (/dev/kvm). It was written on
Ubuntu 22.04 (also under WSL2) with GCC 11.
What gets built¶
| Part | Where | Needed for |
|---|---|---|
QEMU 11.1.3 with the qntb device |
qemu/, scripts/build-qemu.sh |
the QEMU device |
| QEMU 11.1.3 and libvfio-user | third_party/ |
the ntbd prototype only |
ntbd |
ntbd/ |
the prototype device (vfio-user server) |
| Guest kernel 7.3.6 with the NTB modules | third_party/build/linux |
the guests |
ntb_hw_qntb |
driver/ |
the guests |
If you only want the QEMU device, you can skip libvfio-user and ntbd.
Sources¶
Everything third-party lives under third_party/. Source trees there are
never modified; builds are out of tree.
| Component | Version | Verification |
|---|---|---|
| QEMU | 11.1.3 | qemu-11.1.3.tar.xz.sig, key CEAC C9E1 5534 EBAB B82D 3FA0 3353 C9CE F108 B584 (Michael Roth). sha256 5a8289c7940dc77d3873c81a6c29ba3afe1446fc9d36e77fa1658cdbecb84b22 |
| Linux (guest) | 7.3.6 | kernel.org sha256sums.asc, key B886 8C80 BA62 A1FF FAF5 FDA9 632D 3A06 589D A6B1 (checksum autosigner). sha256 8183d475a76a0faffa92d6cd500af421f589dfd0300a0f1060bb875e11d45723 |
| libvfio-user | git 1380c47c14632afc180168f995e72470ad323aba |
GitHub nutanix/libvfio-user, commit pinned (no signed releases) |
| meson | 1.12.1 | pip, in third_party/tools/venv |
scripts/fetch-sources.sh downloads both, checks the sums above and
unpacks QEMU to third_party/src/qemu-11.1.3 and Linux to
third_party/src/linux-7.3.6.
System packages: libglib2.0-dev libpixman-1-dev libjson-c-dev libslirp-dev
libcmocka-dev busybox-static.
Build¶
TP=third_party
# QEMU with the qntb device -> third_party/install-qntb
scripts/build-qemu.sh
# Guest kernel: defconfig + kvm_guest.config + NTB and debug options
# (scripts/build-kernel.sh does these four commands)
make -C $TP/src/linux-7.3.6 O=$PWD/$TP/build/linux defconfig kvm_guest.config
$TP/src/linux-7.3.6/scripts/config --file $TP/build/linux/.config \
-e PCI_MSI -e DEBUG_FS -e DMADEVICES -e DMA_ENGINE \
-m NTB -m NTB_TRANSPORT -m NTB_NETDEV -e NTB_MSI -m NTB_MSI_TEST \
-m NTB_PERF -m NTB_TOOL -m NTB_PINGPONG -e DYNAMIC_DEBUG -e IKCONFIG -e IKCONFIG_PROC \
-e DEBUG_KERNEL -e DEBUG_ATOMIC_SLEEP -e PROVE_LOCKING -e DEBUG_SPINLOCK \
-e DEBUG_MUTEXES -e DEBUG_OBJECTS -e DEBUG_LIST -e DEBUG_SHIRQ
make -C $TP/src/linux-7.3.6 O=$PWD/$TP/build/linux olddefconfig
make -C $TP/src/linux-7.3.6 O=$PWD/$TP/build/linux -j8 bzImage modules
# Driver, test modules and the guest initramfs
make -C driver
make -C tests/kmod
scripts/mkinitramfs.sh # out/initramfs.cpio.gz
build-qemu.sh makes a hard-linked copy of the QEMU source
(third_party/src/qemu-11.1.3-qntb), adds qntb.c, applies
qemu/qntb-build.patch (two lines in hw/misc/Kconfig and meson.build)
and builds it. Run it again after changing qntb.c; it rebuilds only what
changed.
For the ntbd prototype¶
mkdir -p $TP/build/qemu && cd $TP/build/qemu
../../src/qemu-11.1.3/configure --prefix=$PWD/../../install \
--target-list=x86_64-softmmu --enable-kvm --enable-slirp --disable-docs --disable-werror
ninja && ninja install && cd -
# The debug build of libvfio-user uses -Werror, and GCC 11 reports a false
# maybe-uninitialized in samples/client.c, hence debug=false with -g.
$TP/tools/venv/bin/meson setup $TP/build/libvfio-user $TP/src/libvfio-user \
--prefix=$PWD/$TP/install -Ddebug=false -Doptimization=2 -Dc_args=-g -Ddebug-logs=enabled
ninja -C $TP/build/libvfio-user install
make -C ntbd
First link¶
Start two VMs in two terminals. run-vm.sh uses the kernel and initramfs
built above, and NTB_IMPL=qemu selects the QEMU device. a.sock and
b.sock in the same directory mean the two ends of one link.
NTB_IMPL=qemu scripts/run-vm.sh a /tmp/ntb/a.sock # terminal 1
NTB_IMPL=qemu scripts/run-vm.sh b /tmp/ntb/b.sock # terminal 2
Create /tmp/ntb first, and copy the driver into the shared directory:
cp driver/ntb_hw_qntb.ko out/share/. Quit a VM with Ctrl-A X.
In each guest, load the driver and ntb_tool, and enable the link:
modprobe ntb
insmod /mnt/share/ntb_hw_qntb.ko
modprobe ntb_tool
cd /sys/kernel/debug/ntb_tool/0000:00:04.0
echo Y > link
cat peer0/link # Y once both sides have enabled it
Now write a scratchpad in one VM and read it in the other:
echo 0x1234 > peer0/spad0 # VM a
cat spad0 # VM b: 0x1234
For an interactive two-VM demo with networking over the link, run
NTB_IMPL=qemu scripts/demo.sh (needs tmux).
Running VMs by hand¶
Without the helper scripts, a VM needs only the device:
qemu-system-x86_64 -machine q35,accel=kvm -cpu host -m 1G \
-kernel bzImage -initrd initramfs.cpio.gz -append console=ttyS0 -nographic \
-device qntb,socket=/tmp/ntb.sock,server=on # the other VM: no server=on
See The QEMU device for all properties.
Helper scripts¶
run-vm.sh takes these environment variables: NTB_IMPL (qemu or ntbd),
QNTB_OPTS (extra device properties, e.g. msi-fwd=off), KERNEL,
INITRD (another kernel or initramfs, e.g. a KASAN build), KAPPEND (extra
kernel command line), MEM, and NO_SHARE=1 (no 9p share; 9p blocks
migration). mkinitramfs.sh takes KBUILD (kernel build directory) and
INITRD_NAME.
The guest init reads qntb.autorun=<test> (a test from the share),
qntb.run=<path> (a script inside the initramfs) and qntb.setup= from the
kernel command line. out/share on the host is /mnt/share in the guest.
run-vm.sh gives guests shared RAM (memory-backend-memfd,share=on): ntbd
needs it to reach guest memory, and the QEMU device uses it for the fast
memory window path.