ntb_seg API¶
ntb_seg creates /dev/ntb_segN for every NTB device it binds to. The
interface is defined in include/uapi/linux/ntb_seg.h; this page explains
it.
Model¶
Each side has one local segment: memory in its own RAM that the peer can write through the NTB. Each side sees the peer's local segment as its remote segment. There is no protocol on top: what the bytes mean, how the memory is divided and who writes where is up to the application.
The link is ready for use when both modules are loaded and the NTB link is
up. The modules then exchange their segment sizes (four scratchpads and the
highest doorbell bit are used for that, with retries until the peer
acknowledges) and set NTB_SEG_PEER_READY.
Module parameters¶
| Parameter | Default | Meaning |
|---|---|---|
seg_size |
1 MiB | Local segment size; capped by the memory window size |
staging_size |
1 MiB | DMA staging buffer; 0 disables DMA |
shared_local |
0 | 1: every NTB device of the node gets the same local segment (one per node instead of one per device); for a node with several links. At most 4 MiB (one page allocation); a device whose window cannot take it is refused |
mmap¶
| Offset | Region | Notes |
|---|---|---|
NTB_SEG_MMAP_LOCAL |
local segment | DMA-coherent memory (cached on x86). Available once the link has been up once |
NTB_SEG_MMAP_REMOTE |
remote segment | The outbound memory window, write-combining. Usable when PEER_READY; stores are dropped while it is not |
NTB_SEG_MMAP_STAGING |
DMA staging buffer | Source for NTB_SEG_IOC_DMA_WRITE |
Map each region from its start, at most its size, with MAP_SHARED. The
offsets fit a 32-bit off_t; regions are limited to 512 MiB (local,
staging) and 1 GiB (remote). The module cannot be unloaded while a file is
open. If the NTB device itself goes away (driver unbind, hot unplug), the
remote mapping is torn down and touching it raises SIGBUS; the local
segment and the staging buffer stay valid memory until the last file is
closed, but the peer can no longer write them.
ioctls¶
| ioctl | Argument | Effect |
|---|---|---|
NTB_SEG_IOC_INFO |
struct ntb_seg_info * |
Sizes, flags, doorbell count, link_gen |
NTB_SEG_IOC_RING |
__u64 * bits |
Orders earlier stores, then rings the peer's doorbells. Only bits below db_count |
NTB_SEG_IOC_FLUSH |
— | Makes earlier stores to the remote segment visible there (a non-posted read across the link) |
NTB_SEG_IOC_DMA_WRITE |
struct ntb_seg_dma * |
Queues a copy from the staging buffer into the remote segment and returns |
NTB_SEG_IOC_DMA_WAIT |
— | Waits for every copy queued through this file descriptor; -EIO if one failed |
Errors: ENODEV after the device is gone, ENOTCONN for DMA while the
peer is not ready, EOPNOTSUPP for DMA without an engine, EINVAL for
ranges outside a region.
Events¶
read() returns one struct ntb_seg_event at a time and blocks unless the
file is non-blocking; poll() reports POLLIN when an event is waiting and
POLLHUP once the device is gone (then read() returns 0).
| Type | value |
|---|---|
NTB_SEG_EV_DOORBELL |
The doorbell bits the peer rang since the last such event |
NTB_SEG_EV_LINK |
ntb_seg_info.flags after a link or readiness change |
Doorbells from several rings are merged into one event, like hardware doorbells: a bit says "at least once since you last looked".
Every open file of a device gets every event, so several processes can
share it (NTB_SEG_SHARED_EVENTS in flags says so). Events that arrive
while no file is open, and those the last file to close left unread, wait
for the next one to open; so one tool after another (segtool ring, then
segtool wait-db) loses nothing. The segments and the
staging buffer are shared: the processes divide them among themselves, as
pushseg's ntb backend does with its processes option.
Several links¶
A node with several NTB links (a mesh, or an NTB switch with one device per
peer) has one /dev/ntb_segN per link. Each has its own remote segment,
doorbells and DMA staging buffer. With shared_local=1 they all have one
local segment (NTB_SEG_SHARED_LOCAL in flags): every peer writes into the
same memory, so an application can let any peer write anywhere in it.
Mapping NTB_SEG_MMAP_LOCAL through any of the devices gives that memory.
Each link comes up, goes down and counts its link_gen on its own. Which
device leads to which peer: /sys/class/misc/ntb_segN/device is the NTB
device, whose parent is the PCI function.
Ordering¶
- Stores to the remote segment followed by
NTB_SEG_IOC_RING: the peer sees the data when it sees the doorbell. - DMA: data of a copy is in the peer's memory when
NTB_SEG_IOC_DMA_WAITreturns. Ring the doorbell after that. - Stores to the remote segment from several threads have no order with each other unless the application provides one.
Requirements¶
ntb_seg binds to an NTB device with one peer, at least one memory window,
four scratchpads and two doorbell bits (numbered from 0, without gaps). DMA needs a dmaengine channel
provided by the NTB device itself (ntb_hw_qntb has one); without it the
DMA ioctls return EOPNOTSUPP.