Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 24 additions & 5 deletions flashinfer/experimental/b12x/comm/roce/_roce_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ _Static_assert(CTRL_CREDIT >= CTRL_READY_VL + ROCE_VARLEN_MAX_CHUNKS &&
// The startup region, sized for the world, holds per (origin, rail, slot):
// mailboxes, then 8-byte acknowledgment words, then outgoing descriptors.
#define RING_CHUNK_BYTES 65536
#define RING_WINDOW 8
#define RING_WINDOW 16
#define RING_COMPLETION (UINT64_C(1) << 63)
_Static_assert((ROCE_SEND_DEPTH / 8) * 3 + ROCE_MAX_PEERS * RING_WINDOW <= ROCE_SEND_DEPTH,
"send queue depth must hold reserved chains plus all per-origin ring ACKs");
typedef struct {
uint64_t serial, target, offset, flag_offset, flag_value, nbytes, previous;
uint8_t padding[72];
Expand Down Expand Up @@ -209,6 +211,8 @@ typedef struct {
uint32_t mtu[ROCE_MAX_HCAS];
uint32_t qp_num[ROCE_MAX_HCAS][ROCE_MAX_PEERS][ROCE_MAX_LANES];
uint32_t lanes;
uint32_t ring_window;
uint64_t ring_bytes;
} roce_blob_t;

typedef struct {
Expand Down Expand Up @@ -568,6 +572,8 @@ int roce_local_blob(roce_ctx_t* c, void* out, uint64_t out_len) {
blob.region_addr = (uint64_t)(uintptr_t)c->region;
blob.ring_addr = (uint64_t)(uintptr_t)c->ring;
blob.lanes = (uint32_t)c->n_lane;
blob.ring_window = c->ring == NULL ? 0 : (uint32_t)RING_WINDOW;
blob.ring_bytes = c->ring == NULL ? 0 : (uint64_t)ring_bytes(c->world);
for (int h = 0; h < c->n_hca; h++) {
blob.rkey[h] = c->hca[h].mr->rkey;
blob.ring_rkey[h] = c->ring == NULL ? 0 : c->ring_mr[h]->rkey;
Expand Down Expand Up @@ -645,6 +651,8 @@ int roce_connect(roce_ctx_t* c, const void* blobs, uint64_t blobs_len, const int
snprintf(c->err, sizeof(c->err), "invalid rail count %d", n_rail);
return -1;
}
const uint32_t expected_ring_window = c->ring == NULL ? 0 : (uint32_t)RING_WINDOW;
const uint64_t expected_ring_bytes = c->ring == NULL ? 0 : (uint64_t)ring_bytes(c->world);
const roce_blob_t* all = (const roce_blob_t*)blobs;
for (int p = 0; p < c->world; p++) {
if ((all[p].ring_addr != 0) != (c->ring != NULL)) {
Expand All @@ -656,6 +664,13 @@ int roce_connect(roce_ctx_t* c, const void* blobs, uint64_t blobs_len, const int
p, all[p].lanes, c->n_lane);
return -1;
}
if (all[p].ring_window != expected_ring_window || all[p].ring_bytes != expected_ring_bytes) {
snprintf(c->err, sizeof(c->err),
"rank %d uses ring window %u (%llu bytes) where this rank uses %u (%llu bytes)", p,
all[p].ring_window, (unsigned long long)all[p].ring_bytes, expected_ring_window,
(unsigned long long)expected_ring_bytes);
return -1;
}
c->peer_addr[p] = all[p].region_addr;
c->ring_addr[p] = all[p].ring_addr;
}
Expand Down Expand Up @@ -760,11 +775,12 @@ static int drain_cq(roce_ctx_t* c, int h) {
}

// Wait until lane `lane` of HCA h can take one more stripe to peer p. Direct stripes need two
// requests and relay packets need three. A quarter of the queue leaves room
// for provider overhead and per-origin acks posted while draining completions.
// requests and relay packets chain three. One eighth of the send queue (64 signaled ops, at most
// 192 chained WRs) leaves room for up to 256 unreserved per-origin ACKs at world=16 with
// RING_WINDOW=16 plus provider SQE overhead.
static int reserve(roce_ctx_t* c, int h, int p, int lane) {
roce_hca_t* hca = &c->hca[h];
while (hca->outstanding[p][lane] >= ROCE_SEND_DEPTH / 4) {
while (hca->outstanding[p][lane] >= ROCE_SEND_DEPTH / 8) {
if (drain_all(c) != 0) {
return -1;
}
Expand Down Expand Up @@ -899,7 +915,7 @@ static int ring_service_slot(roce_ctx_t* c, int origin, int rail, int slot) {
c->ring_inflight[origin][rail][slot] = serial;
int next = ring_next(c, target);
int h = c->rail_hca[next][rail];
if (c->hca[h].outstanding[next][0] >= ROCE_SEND_DEPTH / 4) return 1;
if (c->hca[h].outstanding[next][0] >= ROCE_SEND_DEPTH / 8) return 1;
if (next != target) {
if (ring_submit(c, origin, rail, slot, next, box->data, c->ring_mr[h]->lkey, d) != 0) return -1;
*pending = 2;
Expand Down Expand Up @@ -1347,6 +1363,9 @@ static int run_pipe_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, uint32_t ch
}
}
if (posted) {
if (c->ring != NULL && drain_all(c) != 0) {
return -1;
}
idle = 0;
continue;
}
Expand Down
68 changes: 68 additions & 0 deletions tests/experimental/b12x/comm/test_roce_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,71 @@ def test_layout_places_variable_length_area_after_two_shot():
for bad in (1000, (1 << 32) + 4096):
with pytest.raises(ValueError):
Layout(4, 2 << 20, 0, bad)


def test_ring_blob_handshake_rejects_window_and_size_mismatch():
"""Ring connect rejects mismatched RING_WINDOW or ring region sizes while keeping ABI 14."""
import ctypes
import mmap
import struct
from pathlib import Path

from b12x.comm.roce._proxy import ABI_VERSION, BLOB_STRUCT_ERR, Layout, Proxy, load

try:
lib = load()
except RuntimeError as exc: # no C compiler or libibverbs headers
pytest.skip(str(exc))

assert lib.roce_abi_version() == ABI_VERSION == 14
blob_bytes = int(lib.roce_blob_bytes())
assert blob_bytes == 1176

ib_root = Path("/sys/class/infiniband")
active_hca = None
if ib_root.is_dir():
for dev in sorted(ib_root.iterdir()):
state = dev / "ports" / "1" / "state"
try:
if "ACTIVE" in state.read_text():
active_hca = dev.name
break
except OSError:
continue
if active_hca is None:
return

layout = Layout(2, 4096)
memory = mmap.mmap(-1, layout.total_bytes)
buf = (ctypes.c_char * layout.total_bytes).from_buffer(memory)
proxy = Proxy(
world_size=2,
rank=0,
hca_names=(active_hca,),
gid_index=0,
region_ptr=ctypes.addressof(buf),
region_bytes=layout.total_bytes,
slot_bytes=4096,
ring=True,
)
try:
local = proxy.local_blob()
assert len(local) == blob_bytes
ring_window, ring_bytes = struct.unpack_from("<IQ", local, blob_bytes - 12)
assert ring_window == 16
assert ring_bytes == 2 * 2 * 16 * (128 + 65536 + 8 + 128)

with pytest.raises(RuntimeError, match=BLOB_STRUCT_ERR):
proxy.connect([local, local[:-8]], [[], [(0, 0)]])

bad_window = local[:-12] + struct.pack("<IQ", 8, ring_bytes)
with pytest.raises(RuntimeError, match="uses ring window 8"):
proxy.connect([local, bad_window], [[], [(0, 0)]])

bad_bytes = local[:-12] + struct.pack("<IQ", ring_window, ring_bytes // 2)
with pytest.raises(RuntimeError, match="uses ring window 16"):
proxy.connect([local, bad_bytes], [[], [(0, 0)]])
finally:
proxy.close()
del buf
memory.close()
Loading