ntb_seg link protocol¶
This page specifies what ntb_seg does on an NTB link, so that the other
end can be something else: another operating system, a bare-metal program or
an FPGA. An implementation that follows it interoperates with ntb_seg; the
Linux module is the reference. The application-level layout that pushseg
puts into the segments is specified separately, in pushseg's
NTB protocol.
Requirement words: must is needed to interoperate; should is what the reference does and what avoids known problems.
1. What each end needs from its NTB¶
Per link, each end uses:
| Resource | Use |
|---|---|
| Memory window 0 | Outbound: the peer's segment. Inbound: our segment |
| Scratchpads 0 to 3 (32 bit) | The handshake (section 3) |
| Doorbells 0 to n−1, n ≥ 2 | The highest one is the control bit; the others belong to the application |
| One peer | One link joins exactly two ends |
The doorbells must be numbered from bit 0 without gaps, and both ends must
have the same n: an end rings the peer's bit n − 1 as the control bit,
taking n from its own NTB. Application bits are 0 … n−2 (with pushseg:
bit q for the peer's partition q).
"Our scratchpad i" is the register the peer writes and we read; "the peer's
scratchpad i" is the one we write and it reads (the Linux NTB API calls them
spad and peer_spad).
2. The segment¶
Each end allocates its local segment at the first link up, when the window's alignment rules are known:
- size: the configured segment size (reference:
seg_size, default 1 MiB), capped by the window's maximum and by 512 MiB, rounded down to the window's size alignment and to the page size; - memory that the NTB can write coherently with the CPU (DMA-coherent, or cached memory on a cache-coherent platform such as x86);
- the inbound translation of window 0 points at it; its address must meet the window's address alignment (the reference fails the setup otherwise and tries again only at the next link event).
The segment is kept until the device goes away; it does not move when the
link goes down and up. A node with several links may put the same local
segment behind every link's inbound window (shared_local): then every peer
writes into one memory. In the reference that segment is one page allocation
(at most 4 MiB on x86), sized by the first link that comes up; a link whose
window cannot take all of it is refused.
The segment size an end announces must not change while the link is up (the reader of the scratchpads relies on it, see section 3).
The remote segment is the peer's local segment, seen through outbound window 0. Its usable size is the size the peer announced, capped by our outbound window size and by 1 GiB; a capped size of 0 means "not ready".
3. Handshake¶
Scratchpad roles (values written by the peer into our scratchpads):
| Scratchpad | Name | Value |
|---|---|---|
| 0 | MAGIC | 0x534547 << 8 | gen ("SEG" and the peer's 8-bit generation); 0 = nothing published |
| 1 | SIZE_LO | low 32 bits of the peer's segment size |
| 2 | SIZE_HI | high 32 bits of the peer's segment size |
| 3 | ACK | the last of our generations the peer has seen; 0xFFFFFFFF = none |
Generation. Each end keeps an 8-bit generation and increments it (modulo 256) each time it sees the link come up. It identifies one link session.
At start (probe) each end clears its own MAGIC (0) and ACK (0xFFFFFFFF).
While the link is up, each end runs this step at every link event, at every control doorbell and at least every 100 ms until it is acknowledged:
- If our MAGIC holds
0x534547xx: read SIZE_LO and SIZE_HI, and writexxinto the peer's ACK. The peer is ready if the size is not 0. - If our ACK is not our current generation, publish: write the peer's
MAGIC = 0, then the peer's SIZE_LO and SIZE_HI, then the peer's MAGIC =
0x53454700 | gen(MAGIC last, so a peer never pairs an old MAGIC with new sizes), then ring the peer's control doorbell; and try again later.
When the link goes down, each end clears its own MAGIC (0) and ACK
(0xFFFFFFFF), so nothing from the old session is acknowledged later.
Restarting an end. An end that restarts (reloads its driver, reboots) must take the link down, so that the peer sees it go down, before it clears its scratchpads and publishes again. The reference does: unloading disables the link. Without that, the peer's ACK still holds the peer's current generation, the peer never publishes again, and the restarted end never sees it ready.
The link is ready (NTB_SEG_PEER_READY) when it is up and the peer's
MAGIC is valid with a size above 0. Only then may the remote segment be
written; stores before that, or after the link went down, are lost.
The reference counts a link generation on each end: it changes whenever "link up", "peer ready" or the remote size changes, as seen when the link state is sampled (at link events, control doorbells and retries). Users of a segment (pushseg) treat every change as the end of everything they knew about the peer. A down-and-up short enough to fall between two samples is not seen as a change: the segments and mappings stay valid, and only stores made while the link was down are lost, as with any outage.
4. Doorbells¶
- The control bit only triggers step 3 above; it carries no data.
- Application bits are delivered to the application as they are: a set bit means "rung at least once since you last looked". Several rings merge.
- A doorbell must not overtake the stores made before it (section 5).
5. Ordering¶
On PCIe, posted writes on one path arrive in order; a read that returns completes after the writes before it. An implementation must provide:
- Ring after stores. Stores to the remote segment issued before a doorbell must reach the peer's memory before the doorbell. With a write-combining mapping, drain the write-combining buffers (a store fence on x86) before ringing.
- Flush. "Everything written so far has arrived" = drain the write-combining buffers, then do a non-posted read that travels the same path across the link as the window writes and wait for its completion. The reference reads the peer's scratchpad 0, which on the NTBs it runs on goes through the link behind those writes; on an NTB where register reads take another path, read from the remote segment instead.
- 8-byte stores. Aligned 8-byte stores to the remote segment must arrive whole (one transaction). Users rely on it for flags and counters.
- DMA. A DMA copy into the remote segment has landed when the engine reports it complete; only then may the writer ring or flush for it.
Reads from the remote segment are slow (non-posted) and not needed by the protocol beyond the flush and the handshake; users should write remotely and read locally.
6. Interoperating with pushseg¶
pushseg's ntb backend uses ntb_seg through its device file, but the
other end only sees memory, doorbells and the handshake above. To be a pushseg
node, an implementation also needs:
- the segment layout and procedures of pushseg's NTB protocol;
- the same segment size as the other nodes (pushseg checks the partition size), and doorbells for every pushseg process per node;
- for a node with several links, one local segment behind all of them.
7. Checking an implementation¶
Run it against ntb_seg in a qemu-ntb VM pair or mesh:
segtool wait-readyon the Linux end must report ready, with the size the other end announced;t-seg.sh's steps (fill, check, DMA, doorbells, reload of one end) are the behaviours to reproduce;- unloading or restarting the other end must make the Linux end report the link not ready, and a restart must complete the handshake again.