Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
116 commits
Select commit Hold shift + click to select a range
3688f3c
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
b7be793
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
faf5901
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
3cc0397
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
c58d83c
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
25c02d3
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
6b5404d
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
06c4e75
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
062af8b
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
d7510ef
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
e16e331
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
649e788
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
664a8e2
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
4ad5029
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9bbc21a
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
cf19042
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
0ba7d30
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
0918809
Let a family's round planner drive the lane host with prompt pieces
jayleaton Oct 10, 2026
361f063
Lane host halt: a stop's drain deadline ends the rounds at the next b…
jayleaton Oct 10, 2026
e9f9210
Round planner test: both prompts queue before the host starts, and it…
jayleaton Oct 10, 2026
96ae865
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
df480e0
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
2c4d837
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
55bf0a7
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
9fbc2b3
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
bb53ff4
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
9eb765d
DeepSeek-V4.1 host side: release config, EXL3 pack headers, a rank's …
jayleaton Oct 11, 2026
865335e
DeepSeek-V4.1 weight forms built at load, and prepared weight folders…
jayleaton Oct 11, 2026
7ccfc5c
DeepSeek-V4.1 Engram host side: table layout, n-gram hashes of a wind…
jayleaton Oct 11, 2026
b775815
DeepSeek-V4.1 phase timers: where a serving round's time goes, by nam…
jayleaton Oct 11, 2026
def4859
DeepSeek-V4.1 Engram rows from packed per-rank shards on local NVMe, …
jayleaton Oct 11, 2026
c7d29ea
DeepSeek-V4.1 CUDA kernel sources: CSA2 attention and top-k, mHC, rou…
jayleaton Oct 11, 2026
b17df97
DeepSeek-V4.1 kernel tooling: nvcc in a container for any checkout la…
jayleaton Oct 11, 2026
73be75f
DeepSeek-V4.1 kernel launch bindings and their loader: each kernel's …
jayleaton Oct 11, 2026
e687ad5
DeepSeek-V4.1 device kernel tests: tf-dsv41-test, bit-exact against t…
jayleaton Oct 11, 2026
a7674d2
DeepSeek-V4.1 kernel prototypes: attention merge and phase timing, x3…
jayleaton Oct 11, 2026
c0605f0
DeepSeek-V4.1 device-side load: named weights in device buffers, run-…
jayleaton Oct 11, 2026
46bff5e
DeepSeek-V4.1 block emitter: a decode window as calls (attention, the…
jayleaton Oct 11, 2026
989ca7e
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
05c5213
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a3303c7
CUDA keyed graph cache and device arena, both off until a family inst…
jayleaton Oct 10, 2026
69e8a65
A failed CUDA kernel launch names its kernel and launch configuration…
jayleaton Oct 10, 2026
d7e89df
fix(cuda): correct RDMA VMM capability attribute
jrb-ai Oct 10, 2026
5826f89
Graph cache: a capture is freed until the map holds it, the keep floo…
jayleaton Oct 11, 2026
5806e55
CUDA arena: the address maps are rehashed before tombstones fill them
jayleaton Oct 11, 2026
d335520
Graph cache: TF_GRAPH_CACHE=0 or off runs every step eagerly in any f…
jayleaton Oct 14, 2026
7ff83d8
Tensor parallelism across CUDA processes: bootstrap, NCCL, mailbox an…
jayleaton Oct 10, 2026
9af2ea2
TP tests: a drain on rank 0 then its clean stop ends rank 1's loop at…
jayleaton Oct 10, 2026
7c9a26e
TP RoCE: with no TF_TP_ROCE_HCA, each rank picks the ports on an IPv4…
jayleaton Oct 11, 2026
8ebb0be
TP README: when -Dtp-nvcc, TF_TP_ROCE_HCA, NCCL_IB_HCA, NCCL_SOCKET_I…
jayleaton Oct 11, 2026
e9e0056
TP test runner: a binary built without the mailbox kernel refuses the…
jayleaton Oct 11, 2026
0472cbf
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
jayleaton Oct 11, 2026
9688398
TP RoCE: no RDMA write past 1 MiB, and boot refuses any setting that …
jayleaton Oct 11, 2026
72132f0
TP RoCE: TF_TP_ROCE_CHUNKED_KB gathers strides past the slot in slot-…
jayleaton Oct 11, 2026
e65583c
Server hooks for a model family's own chat rules.
jayleaton Oct 10, 2026
bf19238
TP RoCE: a failed transport halts its QPs, drains them and keeps its …
jayleaton Oct 11, 2026
9a486bb
Server: a stop logs its signal and the open connections when the acce…
jayleaton Oct 10, 2026
beae7e8
TP RoCE: tf-tp-test failfast V transport checks the failed-transport …
jayleaton Oct 11, 2026
467504a
Server: SIGTERM / SIGINT drains before stopping.
jayleaton Oct 10, 2026
602aad5
Fix RoCE test smaps lookup for merged mappings
jayleaton Oct 11, 2026
23bf7d8
Server tests: a stop that lands between a request's count and its dra…
jayleaton Oct 10, 2026
890dfc6
TP RoCE: run_hosts.sh gives NCCL the RoCE picker's ports when neither…
jayleaton Oct 11, 2026
5410eac
Session memory for paged-KV families: page pool, prefix trie, RAM res…
jayleaton Oct 10, 2026
a5b8b35
Server: a stream that has not sent its first token gets keepalives ev…
jayleaton Oct 10, 2026
a3e2fb1
TP NCCL: all_reduce's doc says NCCL adds in its ring's order, so its …
jayleaton Oct 14, 2026
0d0bdb2
Sessions: the page map is rehashed before tombstones fill it
jayleaton Oct 11, 2026
2b3cccf
Server tests: the drain test's reply in progress is held until its dr…
jayleaton Oct 10, 2026
62a75ae
TP: keep Linux-only transport host tests off macOS builds
jayleaton Oct 15, 2026
2cf61d3
Sessions: honor zero residency budgets and reserve uncapped requests
jayleaton Oct 15, 2026
9367784
server: keep shared halt and drain on the existing template path
jayleaton Oct 15, 2026
864fe25
Graph cache: effective settings have one digest for ranks to compare …
jayleaton Oct 16, 2026
e2504f6
TP: bootstrap compares the graph cache's effective settings, so a ran…
jayleaton Oct 16, 2026
dcdc30c
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
368a0d3
Keep the Linux NVMe lane test out of portable host tests
cwschroeder Oct 16, 2026
7a6b32f
server: main's drain stop closes the engine without the closed lane-h…
jayleaton Oct 16, 2026
abcad7d
Merge ashhart/main into stack/02-graph-cache-arena
jayleaton Oct 16, 2026
02d455e
Merge stack/02-graph-cache-arena (with ashhart/main) into stack/03-tp…
jayleaton Oct 16, 2026
9266f3f
Merge stack/03-tp-fabric (with ashhart/main) into stack/04-sessions
jayleaton Oct 16, 2026
001ac2e
Merge stack/04-sessions (with ashhart/main) into stack/07-server
jayleaton Oct 16, 2026
33c0ef6
Merge stack/07-server (sessions, TP, graph cache, current main) into …
jayleaton Oct 16, 2026
af0bc53
Server tests: the drain test imports the named json module
jayleaton Oct 16, 2026
7051a3c
Merge stack/07-server's drain-test json import fix into stack/08a-config
jayleaton Oct 16, 2026
e6df8ad
merge origin/main: the round planner and halt on main's split lane host
jayleaton Oct 16, 2026
4da2618
Merge shared/lane-host-core (#861, on current main) into stack/08a-co…
jayleaton Oct 16, 2026
6b9bede
Merge stack/08a-config (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
f17db02
Merge stack/08b-engram (current main, shared layers, #861) into stack…
jayleaton Oct 16, 2026
0137ac2
Merge stack/08c-kernels (current main, shared layers, #861) into stac…
jayleaton Oct 16, 2026
8900c7c
Sessions: a failed prefix insert leaves the index as it was
jayleaton Oct 17, 2026
3aeebb9
CUDA arena: a reuse whose address-map insertion fails keeps its range…
jayleaton Oct 17, 2026
46161ac
Merge stack/02-graph-cache-arena (the #635 arena fix) into stack/03-t…
jayleaton Oct 17, 2026
78ce706
Merge stack/03-tp-fabric (the #635 arena and #637 prefix fixes) into …
jayleaton Oct 17, 2026
9c5bd60
Merge stack/04-sessions (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
eaa7953
Merge stack/07-server (the #635 arena and #637 prefix fixes) into sta…
jayleaton Oct 17, 2026
4370f91
Merge stack/08a-config (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
a0f9598
Merge stack/08b-engram (the #635 arena and #637 prefix fixes) into st…
jayleaton Oct 17, 2026
98a1695
Merge stack/08c-kernels (the #635 arena and #637 prefix fixes) into s…
jayleaton Oct 17, 2026
88418d8
test(tp): reject malformed runner commands before configuration
cwschroeder Oct 17, 2026
4c69b0d
fix(tp): parse runner commands before opening sessions
cwschroeder Oct 17, 2026
4df28ce
test(tp): retain verbs load failures in HCA diagnostics
cwschroeder Oct 17, 2026
0a80da0
fix(tp): distinguish verbs load errors from absent HCA ports
cwschroeder Oct 17, 2026
999cb03
Merge stack/03-tp-fabric (adopted #636 runner CLI validation and HCA …
jayleaton Oct 18, 2026
5f14a64
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
749240a
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
7122563
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
6390cf1
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
829c4c9
Merge the layer below (adopted #636 runner CLI validation and HCA ver…
jayleaton Oct 18, 2026
cdde1a4
Merge ashhart/main (1.1.0, 76aafcf2) into stack/02-graph-cache-arena
jayleaton Oct 18, 2026
adbdb8a
CUDA launches: a not-ready launch returns NotReady unlogged, as Drive…
jayleaton Oct 18, 2026
3bd6a6a
Merge stack/02-graph-cache-arena (adbdb8ab, main 76aafcf2) into stack…
jayleaton Oct 18, 2026
54141a1
Merge stack/03-tp-fabric (3bd6a6ab, main 76aafcf2) into stack/04-sess…
jayleaton Oct 18, 2026
80d4ed5
Merge stack/04-sessions (54141a1e, main 76aafcf2) into stack/07-server
jayleaton Oct 18, 2026
aebceae
Merge stack/07-server (80d4ed5c, main 76aafcf2) into stack/08a-config
jayleaton Oct 18, 2026
1003f08
Build: DeepSeek-V4.1's host tests on hosts other than Windows, whose …
jayleaton Oct 18, 2026
aa96782
Merge stack/08a-config (1003f08f, main 76aafcf2) into stack/08b-engram
jayleaton Oct 18, 2026
3f758dc
Merge stack/08b-engram (aa967828, main 76aafcf2) into stack/08c-kernels
jayleaton Oct 18, 2026
ea54d42
Merge stack/08c-kernels (3f758dcf, main 76aafcf2) into stack/08e-block
jayleaton Oct 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TP RoCE: every RDMA post is bounds-checked before the verb, and peers…
… with another region layout are refused

roce_guard.stripes computes each HCA's share of a post (its send stripe, the peer's receive range and its flag word)
and refuses with error.OutOfRegion any range past its slot, its flag area or the registered region, before
post_send. The verbs wire and the shm wire both post through it. The shm wire moves to roce_shm.zig and can stripe
like the verbs wire (Options.shm_stripes), with an optional tap that sees every post for tests. The connection record
carries the slot and registered length, so a peer with another layout is refused at connect; its magic is bumped.

Tests: stripes tile the slot over two HCAs and refuse a post past the slot, a third slot, an unpadded or empty post,
no HCA and a short region. On fake verbs a post past its slot reaches no verb, and a full slot stays inside the
region. Over the shm wire striped as two HCAs, 2,000 variable-length gathers stay inside slot and region, bit for bit.
  • Loading branch information
jayleaton committed Oct 11, 2026
commit e16e331a087bb6e60d0d04854a4cdcc7c3f02ed0
60 changes: 12 additions & 48 deletions zig/src/fabric/tp/roce.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ const collective = @import("collective.zig");
const Bootstrap = @import("bootstrap.zig").Bootstrap;
const sock = @import("sock.zig");
const rv = @import("roce_verbs.zig");
const shm = @import("roce_shm.zig");
const guard = @import("roce_guard.zig");
const HostRegister = @import("mailbox.zig").HostRegister;
const posix = std.posix;
const DevicePtr = collective.DevicePtr;
Expand Down Expand Up @@ -71,30 +73,20 @@ pub const Options = struct {
fast: u32 = 0,
/// TF_TP_ROCE_BLOCKS: the largest grid (config.roce_blocks)
blocks: u32 = 16,
};

/// The shm wire: the peer's region is mapped here; a post copies the slot, then stores the flag with release order.
const ShmWire = struct {
peer: []u8,
local: []u8,
slot_bytes: usize,

fn post(w: *ShmWire, slot: u32, seq: u32, padded: u32) void {
const recv = send_off + 2 * w.slot_bytes;
@memcpy(w.peer[recv + slot * w.slot_bytes ..][0..padded], w.local[send_off + slot * w.slot_bytes ..][0..padded]);
const flag: *std.atomic.Value(u32) = @ptrCast(@alignCast(w.peer[flag_off + slot * 2 * flag_stride ..].ptr));
flag.store(seq, .release);
}
/// The shm wire's stripes, as HCAs: its posts take the verbs wire's split and bounds.
shm_stripes: u32 = 1,
/// Tests: sees every shm post's stripes before the copy.
tap: ?*shm.PostTap = null,
};

const Wire = union(WireKind) {
verbs: rv.VerbsWire,
shm: ShmWire,
shm: shm.ShmWire,

fn post(w: *Wire, slot: u32, seq: u32, padded: u32) !void {
switch (w.*) {
.verbs => |*v| try v.post(slot, seq, padded),
.shm => |*s| s.post(slot, seq, padded),
.shm => |*s| try s.post(slot, seq, padded),
}
}

Expand All @@ -108,7 +100,7 @@ const Wire = union(WireKind) {
fn hcas(w: *const Wire) u32 {
return switch (w.*) {
.verbs => |v| v.n_hca,
.shm => 1,
.shm => |s| s.stripes,
};
}
};
Expand Down Expand Up @@ -204,9 +196,10 @@ pub const Roce = struct {
r.wire = .{ .verbs = try rv.VerbsWire.open(o.hcas, r.region, .{ .send = send_off, .recv = send_off + 2 * r.slot_bytes, .flags = flag_off, .slot = r.slot_bytes }, o.traffic_class) };
},
.shm => {
r.mapping = try sharedPair(b, len);
r.mapping = try shm.sharedPair(b, len);
r.region = @alignCast(r.mapping[r.me * len ..][0..len]);
r.wire = .{ .shm = .{ .peer = r.mapping[(1 - r.me) * len ..][0..len], .local = r.region, .slot_bytes = r.slot_bytes } };
const layout: guard.Layout = .{ .send = send_off, .recv = send_off + 2 * r.slot_bytes, .flags = flag_off, .slot = r.slot_bytes, .len = len };
r.wire = .{ .shm = .{ .peer = r.mapping[(1 - r.me) * len ..][0..len], .layout = layout, .local = r.region, .stripes = o.shm_stripes, .tap = o.tap } };
},
}
return true;
Expand Down Expand Up @@ -422,35 +415,6 @@ pub const Roce = struct {
}
};

/// One shared mapping holding both ranks' regions (rank 0 creates and sizes the file before anyone learns its name).
fn sharedPair(b: *Bootstrap, len: usize) ![]align(page) u8 {
var name: [64]u8 = undefined;
var name_len: u8 = 0;
var fd: std.c.fd_t = -1;
defer if (fd >= 0) {
_ = std.c.close(fd);
};
if (b.rank == 0) blk: {
const s = std.fmt.bufPrint(&name, "/dev/shm/tf-tp-roce-{d}-{x}", .{ std.c.getpid(), sock.nowNs() }) catch break :blk;
name[s.len] = 0;
fd = std.c.open(name[0..s.len :0], .{ .ACCMODE = .RDWR, .CREAT = true, .EXCL = true, .CLOEXEC = true }, @as(std.c.mode_t, 0o600));
if (fd >= 0 and std.c.ftruncate(fd, @intCast(2 * len)) == 0) name_len = @intCast(s.len);
}
// an empty name tells the other rank that rank 0 failed (it must not wait for a name that never comes)
try b.broadcast(std.mem.asBytes(&name_len), std.mem.asBytes(&name_len));
if (name_len == 0) return error.SharedMemoryUnavailable;
try b.broadcast(name[0..name_len], name[0..name_len]);
name[name_len] = 0;
if (b.rank != 0) {
fd = std.c.open(name[0..name_len :0], .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0));
if (fd < 0) return error.SharedMemoryUnavailable;
}
const m = try posix.mmap(null, 2 * len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED }, fd, 0);
try b.agree("mapped", "RoCE shm wire");
if (b.rank == 0) _ = std.c.unlink(name[0..name_len :0]);
return m;
}

fn pin(cpu: u32) void {
var set: std.os.linux.cpu_set_t = @splat(0);
set[cpu / @bitSizeOf(usize)] |= @as(usize, 1) << @intCast(cpu % @bitSizeOf(usize));
Expand Down
96 changes: 96 additions & 0 deletions zig/src/fabric/tp/roce_fake.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//! Fake verbs for host tests of VerbsWire: each QP's posted writes counted from the requests, completions that lag.
const std = @import("std");
const verbs = @import("verbs");
const abi = verbs.abi;
const rv = @import("roce_verbs.zig");

pub const slot: u64 = 1 << 20;
pub const send_off = 8192;

/// What the HCAs hold (posted, not completed); every count comes from the work requests themselves.
pub const Fake = struct {
pub var region: []const u8 = &.{};
pub var lag: u32 = 1;
pub var polls: u64 = 0;
pub var queue: [2][512]u64 = undefined;
pub var head: [2]u32 = .{ 0, 0 };
pub var len: [2]u32 = .{ 0, 0 };
pub var inflight: u64 = 0;
pub var max_inflight: u64 = 0;
pub var max_write: u64 = 0;
pub var outside: u64 = 0;
pub var bytes: u64 = 0;
pub var posts: u64 = 0;

pub fn reset(l: u32) void {
lag = l;
polls = 0;
head = .{ 0, 0 };
len = .{ 0, 0 };
inflight = 0;
max_inflight = 0;
max_write = 0;
outside = 0;
bytes = 0;
posts = 0;
}

pub fn postSend(qp: *abi.Qp, wr: *abi.SendWr, _: *?*abi.SendWr) callconv(.c) c_int {
const q = qp.qp_num;
posts += 1;
var sum: u64 = 0;
var at: ?*abi.SendWr = wr;
while (at) |w| : (at = w.next) {
if (w.send_flags & abi.send_inline == 0) {
const sge = w.sg_list[0];
const lo = @intFromPtr(region.ptr);
if (sge.addr < lo + send_off or sge.addr + sge.length > lo + send_off + 2 * slot) outside += 1;
max_write = @max(max_write, sge.length);
sum += sge.length;
}
if (w.send_flags & abi.send_signaled != 0) {
queue[q][(head[q] + len[q]) % 512] = sum;
len[q] += 1;
inflight += sum;
bytes += sum;
max_inflight = @max(max_inflight, inflight);
sum = 0;
}
}
return 0;
}

/// One completion every `lag` polls of a QP's CQ: the HCA finishes writes far slower than they are posted.
pub fn pollCq(cq: *abi.Cq, _: c_int, wc: [*]abi.Wc) callconv(.c) c_int {
const q = cq.handle;
polls += 1;
if (polls % lag != 0 or len[q] == 0) return 0;
inflight -= queue[q][head[q]];
head[q] = (head[q] + 1) % 512;
len[q] -= 1;
wc[0] = std.mem.zeroes(abi.Wc);
wc[0].status = abi.wc_success;
return 1;
}
};

/// Two fake HCAs behind one VerbsWire, connected to a peer whose region has this rank's layout.
pub const Hw = struct {
ctx: [2]abi.Context = undefined,
qp: [2]abi.Qp = undefined,
cq: [2]abi.Cq = undefined,
mr: [2]abi.Mr = undefined,

pub fn wire(hw: *Hw, region: []u8) rv.VerbsWire {
var w: rv.VerbsWire = .{ .v = undefined, .traffic_class = 0, .recv_off = send_off + 2 * slot, .flag_off = 4096, .send_off = send_off, .slot_bytes = slot, .region = region };
for (0..2) |i| {
hw.ctx[i].ops = .{ ._before_poll = @splat(null), .poll_cq = Fake.pollCq, ._before_post = @splat(null), .post_send = Fake.postSend, .post_recv = undefined, ._after = @splat(null) };
hw.qp[i].qp_num = @intCast(i);
hw.cq[i].handle = @intCast(i);
hw.mr[i].lkey = 7;
w.hcas[i] = .{ .ctx = &hw.ctx[i], .pd = undefined, .mr = &hw.mr[i], .cq = &hw.cq[i], .qp = &hw.qp[i], .gid_index = 0, .gid = @splat(0), .mtu = 0, .lid = 0 };
}
w.n_hca = 2;
return w;
}
};
63 changes: 63 additions & 0 deletions zig/src/fabric/tp/roce_guard.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! Every RoCE post's bounds before the verb: each HCA's stripe inside its slot and the registered region.
const std = @import("std");

pub const max_hcas = 2;
pub const flag_stride = 128;

pub const Error = error{OutOfRegion};

/// Both ranks' region layout (roce.zig's): byte offsets and the registered length every post must stay inside.
pub const Layout = struct { send: u64, recv: u64, flags: u64, slot: u64, len: u64 };

/// One HCA's share of a post: its data stripe in this rank's send slot and the peer's receive slot, and its flag word.
pub const Stripe = struct { local: u64, remote: u64, len: u32, flag: u64 };

/// Slot `slot`'s first `padded` bytes striped over `n_hca` HCAs; error.OutOfRegion before any verb past a slot or MR.
pub fn stripes(l: Layout, n_hca: u32, slot: u32, padded: u32, out: *[max_hcas]Stripe) Error!void {
if (n_hca == 0 or n_hca > max_hcas or slot > 1) return error.OutOfRegion;
if (padded == 0 or padded % 16 != 0 or padded > l.slot) return error.OutOfRegion;
const packs = padded / 16;
var offset: u64 = 0;
for (out[0..n_hca], 0..) |*o, i| {
var stripe = packs / n_hca;
if (i < packs % n_hca) stripe += 1;
o.* = .{ .local = l.send + slot * l.slot + offset, .remote = l.recv + slot * l.slot + offset, .len = stripe * 16, .flag = l.flags + (@as(u64, slot) * max_hcas + i) * flag_stride };
if (!within(o.local, o.len, l.send + slot * l.slot, l.slot) or !within(o.remote, o.len, l.recv + slot * l.slot, l.slot)) return error.OutOfRegion;
if (!within(o.flag, 4, l.flags, l.send - l.flags) or !within(o.local, o.len, 0, l.len) or !within(o.remote, o.len, 0, l.len)) return error.OutOfRegion;
offset += o.len;
}
}

fn within(at: u64, len: u64, lo: u64, size: u64) bool {
return at >= lo and at + len <= lo + size;
}

/// roce.zig's layout for a 64 KiB slot: ctrl 4 KiB, flags 4 KiB, send [2][slot], recv [2][slot].
fn testLayout() Layout {
const slot: u64 = 64 << 10;
return .{ .send = 8192, .recv = 8192 + 2 * slot, .flags = 4096, .slot = slot, .len = 8192 + 4 * slot };
}

test "a post striped over two HCAs: the stripes tile the slot's first bytes, each flag on its own word" {
const l = testLayout();
var s: [max_hcas]Stripe = undefined;
try stripes(l, 2, 1, 48, &s);
try std.testing.expectEqual(Stripe{ .local = l.send + l.slot, .remote = l.recv + l.slot, .len = 32, .flag = l.flags + 2 * flag_stride }, s[0]);
try std.testing.expectEqual(Stripe{ .local = l.send + l.slot + 32, .remote = l.recv + l.slot + 32, .len = 16, .flag = l.flags + 3 * flag_stride }, s[1]);
try stripes(l, 2, 1, @intCast(l.slot), &s);
try std.testing.expectEqual(l.recv + 2 * l.slot, s[1].remote + s[1].len);
}

test "a post past its slot, a third slot, an unpadded or empty post, or no HCA is refused" {
const l = testLayout();
var s: [max_hcas]Stripe = undefined;
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 0, @intCast(l.slot + 16), &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 2, 16, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 0, 24, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 2, 0, 0, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 0, 0, 16, &s));
try std.testing.expectError(error.OutOfRegion, stripes(l, 3, 0, 16, &s));
var short = l;
short.len -= 16;
try std.testing.expectError(error.OutOfRegion, stripes(short, 2, 1, @intCast(l.slot), &s));
}
20 changes: 20 additions & 0 deletions zig/src/fabric/tp/roce_post_test.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//! VerbsWire.post on fake verbs: a post that would leave its slot or region is refused before any verb.
const std = @import("std");
const fake = @import("roce_fake.zig");
const Roce = @import("roce.zig").Roce;
const Fake = fake.Fake;

test "a post past its slot is refused before any verb; a full slot posts inside the region" {
const region = try std.testing.allocator.alignedAlloc(u8, .fromByteUnits(4096), Roce.regionBytes(fake.slot));
defer std.testing.allocator.free(region);
Fake.region = region;
Fake.reset(1);
var hw: fake.Hw = .{};
var w = hw.wire(region);
try std.testing.expectError(error.OutOfRegion, w.post(1, 1, fake.slot + 16));
try std.testing.expectError(error.OutOfRegion, w.post(2, 1, 16));
try std.testing.expectEqual(@as(u64, 0), Fake.posts);
try w.post(1, 1, fake.slot);
try std.testing.expectEqual(@as(u64, 2), Fake.posts);
try std.testing.expectEqual(@as(u64, 0), Fake.outside);
}
63 changes: 63 additions & 0 deletions zig/src/fabric/tp/roce_shm.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! The RoCE protocol's shm wire: both ranks' regions in one shared mapping, posts through the guard's stripes.
const std = @import("std");
const Bootstrap = @import("bootstrap.zig").Bootstrap;
const sock = @import("sock.zig");
const guard = @import("roce_guard.zig");
const posix = std.posix;
const page = std.heap.page_size_min;

/// A test's view of each post: slot, padded bytes and the stripes the wire is about to move.
pub const PostTap = struct {
ctx: *anyopaque,
seen: *const fn (ctx: *anyopaque, l: guard.Layout, slot: u32, padded: u32, s: []const guard.Stripe) void,
};

/// The peer's region is mapped here; a post copies each stripe, then stores its flag with release order.
pub const ShmWire = struct {
peer: []u8,
layout: guard.Layout,
local: []u8,
/// Stripes a post makes, as HCAs would: the verbs wire's split, flags and bounds on one host.
stripes: u32 = 1,
tap: ?*PostTap = null,

pub fn post(w: *ShmWire, slot: u32, seq: u32, padded: u32) guard.Error!void {
var plan: [guard.max_hcas]guard.Stripe = undefined;
try guard.stripes(w.layout, w.stripes, slot, padded, &plan);
if (w.tap) |t| t.seen(t.ctx, w.layout, slot, padded, plan[0..w.stripes]);
for (plan[0..w.stripes]) |s| {
@memcpy(w.peer[s.remote..][0..s.len], w.local[s.local..][0..s.len]);
const flag: *std.atomic.Value(u32) = @ptrCast(@alignCast(w.peer[s.flag..].ptr));
flag.store(seq, .release);
}
}
};

/// One shared mapping holding both ranks' regions (rank 0 creates and sizes the file before anyone learns its name).
pub fn sharedPair(b: *Bootstrap, len: usize) ![]align(page) u8 {
var name: [64]u8 = undefined;
var name_len: u8 = 0;
var fd: std.c.fd_t = -1;
defer if (fd >= 0) {
_ = std.c.close(fd);
};
if (b.rank == 0) blk: {
const s = std.fmt.bufPrint(&name, "/dev/shm/tf-tp-roce-{d}-{x}", .{ std.c.getpid(), sock.nowNs() }) catch break :blk;
name[s.len] = 0;
fd = std.c.open(name[0..s.len :0], .{ .ACCMODE = .RDWR, .CREAT = true, .EXCL = true, .CLOEXEC = true }, @as(std.c.mode_t, 0o600));
if (fd >= 0 and std.c.ftruncate(fd, @intCast(2 * len)) == 0) name_len = @intCast(s.len);
}
// an empty name tells the other rank that rank 0 failed (it must not wait for a name that never comes)
try b.broadcast(std.mem.asBytes(&name_len), std.mem.asBytes(&name_len));
if (name_len == 0) return error.SharedMemoryUnavailable;
try b.broadcast(name[0..name_len], name[0..name_len]);
name[name_len] = 0;
if (b.rank != 0) {
fd = std.c.open(name[0..name_len :0], .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(std.c.mode_t, 0));
if (fd < 0) return error.SharedMemoryUnavailable;
}
const m = try posix.mmap(null, 2 * len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED }, fd, 0);
try b.agree("mapped", "RoCE shm wire");
if (b.rank == 0) _ = std.c.unlink(name[0..name_len :0]);
return m;
}
Loading