Tutorial: two VMs talking over NTB¶
This walks through a link between two VMs by hand: start them, load the modules, move data with the CPU and with DMA, and ring doorbells. Then it shows the same from a C program. It takes about fifteen minutes once qemu-ntb is built.
1. Build¶
Get qemu-ntb next to this repository (or set QEMU_NTB to its path) with
its prebuilt bundle: QEMU with the qntb device, the guest kernel,
ntb_hw_qntb, the initramfs and a kit to build modules for the guest
kernel. A source build
works the same way.
git clone https://github.com/oguzhankynk/qemu-ntb ../qemu-ntb
../qemu-ntb/scripts/get-prebuilt.sh
Then build this repository's module and tool:
make -C kmod # ntb_seg.ko, against qemu-ntb's guest kernel (or its kit)
make -C tools # segtool, linked statically
2. Start two VMs¶
The VMs share a directory with the host (/mnt/share inside). Copy the
module and the tool there, then start one VM per terminal:
Q=../qemu-ntb
cp kmod/ntb_seg.ko tools/segtool $Q/driver/ntb_hw_qntb.ko $Q/out/share/
mkdir -p /tmp/ntb
NTB_IMPL=qemu $Q/scripts/run-vm.sh a /tmp/ntb/a.sock # terminal 1
NTB_IMPL=qemu $Q/scripts/run-vm.sh b /tmp/ntb/b.sock # terminal 2
run-vm.sh turns on the device's fast memory windows: the peer's memory is
mapped straight into the guest. Quit a VM with Ctrl-A X.
3. Load the modules¶
In both VMs:
modprobe ntb
insmod /mnt/share/ntb_hw_qntb.ko # the NTB hardware driver
insmod /mnt/share/ntb_seg.ko # this repository's module
cd /mnt/share
./segtool wait-ready && ./segtool info
link up, peer ready, local 1048576, remote 1048576, staging 1048576, doorbells 31, gen 2
Each side now has a 1 MiB local segment the peer can write, and sees
the peer's segment as its remote segment. The size is a module
parameter (seg_size).
4. Move data¶
Write a pattern into the peer's segment with the CPU, and tell it with doorbell 0:
./segtool fill 7 65536 && ./segtool ring 1 # VM a
./segtool wait-db 1 && ./segtool check 7 65536 && echo ok # VM b
fill stores into the mapped window and then flushes: the data is in b's
memory before the doorbell goes out. check reads b's own memory; nothing
crosses the link for it.
The same with DMA (the copy goes from a staging buffer through the device's DMA engine):
./segtool dma-fill 8 1048576 && ./segtool ring 2 # VM a
./segtool wait-db 2 && ./segtool check 8 1048576 && echo ok # VM b
5. Measure¶
./segtool pong 1000 # VM b
./segtool ping 1000 # VM a: round-trip latency through doorbells
./segtool bw cpu 256 # VM a: CPU write rate into b's segment
./segtool bw dma 256 # VM a: the same with DMA
With fast windows the CPU writes run at memory speed; without them
(FAST_MW=off before run-vm.sh) every store is a message and the rate
drops to well under 1 MB/s, while DMA stays fast. See qemu-ntb's
performance page.
6. From a program¶
The tool is a thin layer over the device file. The core of a sender:
#include <fcntl.h>
#include <stdint.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <linux/ntb_seg.h> /* include/uapi/linux/ntb_seg.h */
int fd = open("/dev/ntb_seg0", O_RDWR);
struct ntb_seg_info info;
ioctl(fd, NTB_SEG_IOC_INFO, &info); /* wait for NTB_SEG_PEER_READY */
volatile uint64_t *peer = mmap(NULL, info.remote_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, NTB_SEG_MMAP_REMOTE);
peer[0] = 42; /* lands in the peer's memory */
uint64_t bell = 1;
ioctl(fd, NTB_SEG_IOC_RING, &bell); /* orders the store, then rings */
And a receiver:
volatile uint64_t *mine = mmap(NULL, info.local_size, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, NTB_SEG_MMAP_LOCAL);
struct ntb_seg_event ev;
while (read(fd, &ev, sizeof(ev)) == sizeof(ev)) { /* blocks; poll() works too */
if (ev.type == NTB_SEG_EV_DOORBELL && (ev.value & 1))
printf("got %llu\n", (unsigned long long)mine[0]);
}
Two rules for the remote mapping:
- Write it with plain loads and stores of up to 8 bytes, not with
memcpy(). Without fast windows the mapping is trapped MMIO, and KVM cannot emulate the vector instructionsmemcpy()may use (the program getsSIGILL). - Order before you signal:
NTB_SEG_IOC_RINGandNTB_SEG_IOC_FLUSHorder the earlier stores. A plain store followed by a store to a "ready" flag in the same segment is not enough on all hardware.
The full interface is in the API reference.
7. When the peer goes away¶
Unload ntb_seg in one VM (rmmod ntb_seg) while the other keeps writing.
The writer's stores are dropped; read() reports a link event with
NTB_SEG_PEER_READY cleared. Load the module again and the handshake
repeats; tests/t-seg.sh does exactly this.
8. Run the tests¶
tests/t-gridlane.sh runs gridlane's Gray-Scott demonstrator across the VMs
(see the home page).
tests/t-pushseg.sh runs a ping-pong of pushseg
over its ntb backend; set PUSHSEG to a pushseg checkout.
tests/t-psmulti.sh runs three pushseg processes per VM over the same
link.
MESH=3 scripts/run-test.sh tests/t-mesh-seg.sh runs on three VMs with a
link between every two; see a mesh of VMs.
scripts/run-test.sh tests/t-seg.sh # QEMU device, fast windows
FAST_MW=off scripts/run-test.sh tests/t-seg.sh # message path
NTB_IMPL=ntbd scripts/run-test.sh tests/t-seg.sh
Logs are in qemu-ntb's out/logs/, results in out/share/result-{a,b}.txt.
A test fails on any FAIL line and on kernel warnings (lockdep, KASAN-style
reports, hung tasks) in either guest.