Skip to content

The QEMU device

qntb is a PCI device built into QEMU. Each VM gets one function; two functions connect over a UNIX socket and form one NTB link. The guest sees the device described in the specification, so the same Linux driver works with it and with ntbd.

The device is not part of upstream QEMU. This repository ships the source (qemu/hw/misc/qntb.c) and a small build patch; scripts/build-qemu.sh builds a QEMU with it (see Getting started).

Connecting two VMs

One end listens, the other connects. The listening end is end A (LINK_STATUS.SIDE = 0).

# VM A
qemu-system-x86_64 ... -device qntb,socket=/tmp/ntb.sock,server=on
# VM B
qemu-system-x86_64 ... -device qntb,socket=/tmp/ntb.sock

The order does not matter: the connecting end retries until the listening end is up. Both ends can also live in one VM (loopback, used by the Linux selftest):

qemu-system-x86_64 ... \
    -device qntb,socket=/tmp/ntb.sock,server=on \
    -device qntb,socket=/tmp/ntb.sock

Fast memory windows

By default every access through a memory window is a message, which is slow. For speed, turn on fast-mw on both ends, give each VM shared RAM and run with KVM:

qemu-system-x86_64 -accel kvm ... \
    -object memory-backend-memfd,id=ram0,size=1G,share=on \
    -machine memory-backend=ram0 -m 1G \
    -device qntb,socket=/run/ntb/link.sock,server=on,fast-mw=on

Each QEMU then receives a file descriptor for the other one's guest RAM, so only use it between QEMUs that trust each other, and keep the socket in a directory only they can reach. Without shared RAM (or with TCG) the device falls back to messages. See How it works.

Properties

Property Default Meaning
socket (required) Path of the UNIX socket
server off on: listen on socket (end A); off: connect to it (end B)
mw-count 2 Memory windows, 1 or 2
mw0-size, mw1-size 4M, 1M Window sizes, powers of two up to 1G
spads 16 Scratchpads, 9–128
doorbells 32 Doorbell bits, 4–64
db-vectors 4 MSI-X vectors for doorbells
msix-spare 4 Extra MSI-X vectors for ntb_msi (used when msi-fwd=on)
dma-channels 4 DMA channels, 0–8
msi-fwd on Inbound MSI forwarding (SPEC §7.3), needed by Linux ntb_msi
fast-mw off Map the peer's RAM behind the memory windows (needs KVM and shared RAM)

mw-count, the window sizes, spads and doorbells must be equal on both ends. If they differ, LINK_STATUS.CFG_MISMATCH is set and the link stays down. The other properties may differ.

  • If the peer QEMU exits or is killed, the link goes down at once. When a new peer connects, it comes up again once both drivers enable it.
  • If the peer stops answering (stopped with SIGSTOP, hung, or stuck in a debugger), the link goes down after 2 seconds without any message from it. Both ends send a keepalive every 500 ms.
  • A VM reset or the RESET register resets that end only; the peer sees the link go down.

Migration

Live migration is blocked: migrate fails with qntb: migration of an NTB link is not supported, and QEMU refuses to start with -only-migratable.

How it works

The device talks to its peer with small messages (the link protocol). Writes to the peer (spads, doorbells, memory window writes) are posted: they are queued and the vCPU continues. Reads from the peer wait for an answer. Every read of the control BAR first exchanges a SYNC message with the peer; this gives the barrier the specification relies on (SPEC §4): after a BAR0 read returns, everything this end sent before has reached the peer.

Four threads per device do the work outside the vCPUs:

Thread Job
rx Owns the connection, reads messages, answers SYNC and keepalives
apply Applies the peer's operations to this VM (memory, spads, doorbells)
tx Writes queued messages to the socket
dma Runs the DMA channels

None of them needs the QEMU big lock, so two QEMUs that wait for each other cannot deadlock. Guest memory is accessed directly; interrupts are raised from the main loop.

The fast memory window path

On the message path every store into a memory window is a VM exit and a message. With fast-mw=on the device instead maps the peer's memory behind the window:

  1. When the target opens a window (XLAT_CTRL.ENABLE = 1) onto shared guest RAM, it sends the other end a GRANT with the RAM's file descriptor and the offset of the window in it.
  2. The other end maps that part of the file over its memory window BAR. From then on, guest stores and the DMA engine write straight into the peer's RAM.
  3. When the target closes the window, disables its link, resets, or turns off bus mastering, it sends REVOKE. The other end replaces the mapping at once, without waiting for the main loop. The target's next BAR0 read returns only after that, so a close takes effect exactly as SPEC §7.2 requires.

An end also drops its mappings at once when it gives up on the connection, and while it is too busy to read new messages (it could miss a REVOKE). Writes to spads and doorbells wait for the peer while a window is mapped, so that they stay in order with direct stores that follow them.

Windows that do not point at shared RAM (MMIO, the MSI address used by ntb_msi, ROM) stay on the message path. The test suite covers both paths (FAST_MW=off for the message path).

Performance

Measured with the test suite on a laptop (KVM, debug guest kernel); see Performance for the numbers and how they were taken.

Limitations

  • x86-64 hosts only (the protocol uses host byte order).
  • With fast-mw=on, the two QEMU processes must trust each other: each one receives a file descriptor for the other's guest RAM. Guests cannot use this; they only reach the windows the peer opened.
  • With fast-mw=on, do not give the sending VM a VFIO device: VFIO maps the window into the IOMMU, and a passed-through device could keep writing there after a close.
  • With fast-mw=on, if the peer QEMU process is frozen (SIGSTOP, a debugger), a close cannot be confirmed. The target gives up after 2 seconds and drops the link; the frozen process could still store into the old window for a moment after it resumes, until it notices the connection is gone.
  • A window is resolved when it opens. If the target's RAM layout changes later (memory hot-unplug), the old mapping stays until the window closes.
  • No guest IOMMU: DMA and window accesses go to guest physical addresses.
  • Two ends per link; no multi-port topologies.