Skip to content

ntb-lab

Use a PCIe NTB link from ordinary programs. ntb_seg is a small Linux kernel module that gives user space shared memory with the other host (both ways), doorbells and the NTB's DMA engine, through one device file. This repository also has two Linux VMs on a virtual NTB to try it in, a tutorial, and tests that run in both VMs at once.

release docs license

int fd = open("/dev/ntb_seg0", O_RDWR);
volatile uint64_t *peer = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED,
                               fd, NTB_SEG_MMAP_REMOTE);     /* the other host's memory */
peer[0] = 42;                                                /* a plain store lands there */
uint64_t bell = 1;
ioctl(fd, NTB_SEG_IOC_RING, &bell);                          /* and wakes it up */

On the other host, read() on the same device returns the doorbell, and the value is in its own memory (NTB_SEG_MMAP_LOCAL), read with a plain load.

flowchart LR
    subgraph A["VM a"]
        direction TB
        a1["your program<br/>mmap /dev/ntb_seg0"] --- a2["ntb_seg"] --- a3["ntb_hw_qntb"]
    end
    subgraph B["VM b"]
        direction TB
        b1["your program<br/>mmap /dev/ntb_seg0"] --- b2["ntb_seg"] --- b3["ntb_hw_qntb"]
    end
    a3 <-->|qntb device in QEMU| b3

The virtual NTB comes from qemu-ntb. ntb_seg uses only the Linux NTB API, so the same module and programs are meant for real NTB hardware too (not yet tried on one).

The other end of a link does not have to be Linux: the link protocol specifies what ntb_seg does on the link (handshake, windows, doorbells, ordering), and pushseg's NTB protocol what pushseg keeps in the segments, for a second implementation.

Quick start

You need a qemu-ntb checkout next to this one, with its prebuilt bundle (QEMU with the device, the guest kernel, ntb_hw_qntb and a kit to build modules for that kernel) or a source build. On an x86-64 Linux host with gcc, make and a static C library:

git clone https://github.com/oguzhankynk/qemu-ntb ../qemu-ntb
../qemu-ntb/scripts/get-prebuilt.sh                             # nothing to build
scripts/run-test.sh tests/t-seg.sh                              # both VMs, all checks

run-test.sh builds the module and segtool, starts two VMs through the qemu-ntb harness and runs the test in both. FAST_MW=off uses the slower message path of the device, NTB_IMPL=ntbd the vfio-user prototype.

What is here

Path Contents
kmod/ntb_seg.c The kernel module: /dev/ntb_segN per NTB device
include/uapi/linux/ntb_seg.h Its user-space interface (ioctls, mmap offsets, events)
tools/segtool.c A command-line tool on top of it, also used by the tests
tests/ Tests that run in both VMs at once
docs/ Tutorial, API reference and link protocol

Status

All tests pass: tests/t-seg.sh, t-pushseg.sh, t-psmulti.sh and t-gridlane.sh on two VMs, and the mesh tests below on three (the gridlane tests need the QEMU device). t-seg.sh runs on all three: the QEMU device with fast windows, without them, and ntbd. It checks the handshake, CPU and DMA transfers in both directions with data verification, doorbells, round-trip latency, throughput, and the peer unloading and reloading the module while the other side keeps writing.

pushseg over NTB

pushseg has an ntb backend on top of ntb_seg. tests/t-pushseg.sh runs its ping-pong example between the two VMs:

PUSHSEG=/path/to/pushseg scripts/run-test.sh tests/t-pushseg.sh
Setup Round trip (median) 1 MiB writes
QEMU device, fast windows 0.16 µs 2.7 GB/s
QEMU device, message path (dma_threshold = 0) 105 µs 470 MB/s
ntbd (dma_threshold = 0) 60 µs 2.0 GB/s

Both VMs run on one laptop, so with fast windows the "link" is shared memory; real NTB hardware adds its PCIe latency.

Several processes per node can share the link: set processes in [backend.ntb] (the same on both nodes) and each process takes a partition of the node's segment. tests/t-psmulti.sh runs three processes in each VM; every one sends 2000 notifications with data to each of the three on the other side at once and checks what it receives, then all six start again, and once more with segtool events reading the device as a seventh reader (ntb_seg gives every open file every event). It passes on the QEMU device with and without fast windows and on ntbd.

gridlane over NTB

gridlane's Gray-Scott demonstrator runs across the two VMs. Its data plane between the worker applications (halo exchange, snapshots, reductions) uses pushseg's ntb backend; the control plane and the coordinator use TCP over ntb_netdev on a second NTB link between the same VMs. tests/t-gridlane.sh runs it once with the data plane on NTB and once with it on TCP, and the host checks both against the Python reference:

LINKS=2 GRIDLANE=/path/to/gridlane PUSHSEG=/path/to/pushseg \
    scripts/run-test.sh tests/t-gridlane.sh

128 × 128 cells, 400 steps, 2 applications × 4 workers (6 simulation, 2 analysis), one laptop:

Setup Data plane on NTB (ntb) Data plane on TCP (ntb_netdev)
QEMU device, fast windows 83 ms (halo exchange 26 ms) 1315 ms (1111 ms)
QEMU device, message path 1115 ms (924 ms) 11310 ms (10382 ms)

Both columns use the same virtual NTB; the TCP column goes through ntb_transport without DMA. Every run agrees with the reference.

A mesh of VMs

With qemu-ntb's run-mesh.sh, three (or more) VMs get an NTB link between every two: each VM has one NTB device per peer, the way a node sees an NTB switch with one device per peer or a set of two-port NTB cards. ntb_seg with shared_local=1 gives all of a node's devices one local segment, and pushseg's ntb backend uses all links at once, finding by itself which node is behind which link.

MESH=3 scripts/run-test.sh tests/t-mesh-seg.sh
MESH=3 PUSHSEG=/path/to/pushseg scripts/run-test.sh tests/t-mesh-pushseg.sh
MESH=3 MESH_NET=1 GRIDLANE=/path/to/gridlane PUSHSEG=/path/to/pushseg \
    scripts/run-test.sh tests/t-mesh-gridlane.sh
  • t-mesh-seg.sh: every VM writes into every peer with the CPU and with DMA; the owner finds all writers' data in its one segment; one VM reloads the module and the links between the others do not notice.
  • t-mesh-pushseg.sh: two pushseg processes per VM, each sending notifications with data to all four processes on the other VMs; then a and c keep talking while b reloads ntb_seg (their traffic does not notice), and everyone again after the reload.
  • t-mesh-gridlane.sh: the Gray-Scott demonstrator on three VMs (3 targets × 1 application × 4 workers), the data plane between the workers on the three NTB links, the control plane on a separate virtual network (MESH_NET). It agrees with the reference.

The first two pass on the QEMU device with and without fast windows and on ntbd; the gridlane test runs on the QEMU device. Gray-Scott, 128 × 128 cells, 400 steps, 12 workers:

Setup Data plane on NTB Data plane on TCP (virtio-net)
QEMU device, fast windows 108 ms 402 ms
QEMU device, message path 4449 ms 334 ms

On the message path every store into a window is a message between the two QEMUs, so a plain virtual network is faster; with fast windows the NTB data plane is about four times faster.

Project What it is
qemu-ntb The virtual PCIe NTB for QEMU these VMs use; runs the Linux NTB stack unmodified
pushseg Push-only data transfer between processes and machines; its ntb backend runs on ntb_seg
gridlane Layered data distribution for parallel programs; runs over NTB through pushseg

License

The kernel module and its header are GPL-2.0-only (the header with the Linux syscall note); segtool, scripts and docs are MIT. See LICENSING.md.