Skip to content

Link protocol

This is the protocol between two qntb devices (qemu/hw/misc/qntb.c). You need it only to write another implementation of an end, for example in a different emulator. It is not visible to guests and may change between versions; HELLO carries the version.

Transport

A UNIX stream socket. One end listens, the other connects. Each end may reconnect after the connection is lost; a new connection starts from HELLO again.

Messages

Every message is a header followed by len payload bytes. All fields are little-endian, structures are packed.

struct hdr { uint32_t type; uint32_t len; };
Type Name Payload Reply
1 HELLO version, mw_count, mw_size[2] (u64), spads, doorbells, flags —
2 HELLO_ACK — —
3 STATE enable, accept —
4 SPAD_WR accept, idx, val —
5 DB_SET accept, pad, bits (u64) —
6 MW_WR id, accept, mw, len, off (u64), then len data bytes RESP if id ≠ 0
7 MW_RD id, accept, mw, len, off (u64) RESP with len bytes
8 SPAD_RD id, idx RESP with 4 bytes
9 SYNC id RESP
10 RESP id, code, then data —
11 PING — —
12 GRANT mw, pad, size (u64), off (u64); a file descriptor —
13 REVOKE mw —

Fields are uint32_t unless marked. Unknown types are ignored. A payload larger than 64 KiB + 64 bytes ends the connection.

Handshake

  1. After connecting, each end sends HELLO (protocol version 2). flags bit 0 says that the end can map memory it is granted.
  2. An end that receives a HELLO with the same version and the same mw_count, window sizes, spads and doorbells answers HELLO_ACK. Otherwise it sets CFG_MISMATCH and does not answer.
  3. An end has a present peer (PEER_PRESENT = 1) once it has both received a matching HELLO and its own HELLO was acknowledged. It then sends STATE.

STATE carries the sender's LINK_CTRL.ENABLE and its acceptance number. An end sends STATE whenever ENABLE changes, after a reset, and when the peer becomes present. The link is up on an end when its own ENABLE is 1 and the peer reported enable = 1.

The acceptance number belongs to the target. It changes when ENABLE goes from 0 to 1 and on every reset. Every operation on the peer (SPAD_WR, DB_SET, MW_WR, MW_RD) carries the last number the sender received. The target drops operations whose number is not its current one, so writes sent before a reset can never land in memory opened after it (SPEC §13.7).

Operations on the peer

The target applies an operation only if its ENABLE is 1, the sender is a present peer, and the acceptance number matches. Memory window operations also need bus mastering on, an active translation, and a range inside it. Only guest RAM is accessed; with MSI forwarding, a 4-byte write that matches an MSI-X entry raises that vector instead.

RESP.code is 0 on success, 3 if the window refused the access, and 4 if the link or acceptance check failed. A refused read returns all ones.

Mapped windows

If the peer set flags bit 0, a target may let it map an inbound window directly. It sends GRANT for window mw with a file descriptor (SCM_RIGHTS, attached to the first byte of the message) and the byte range [off, off + size) of that file that backs the window. It does this only while it would accept memory window operations at all (see above) and only if the whole window is shared, writable RAM.

The receiver maps the range while its own link is up, and stops using it when it receives REVOKE for that window, when its link goes down, or when the connection ends. A receiver handles GRANT and REVOKE as soon as it reads them, before any later message from the same peer. So when the target gets the answer to a SYNC sent after a REVOKE, the mapping is gone.

A receiver that cannot map a grant ignores it and keeps using MW_WR and MW_RD; the target accepts those as before.

Ordering

Each end handles messages strictly in the order they arrive, and answers a request only after it has applied everything that came before it. This is what makes the SYNC round trip a barrier: when its RESP arrives, the peer has applied all earlier messages from this end, and this end has applied everything the peer sent before the RESP.

Liveness

Each end sends PING every 500 ms. An end that has received nothing for 2 seconds closes the connection and reports the peer as gone. A request that gets no answer in that time also closes the connection.