From 3736fef17ec2f334a3d2933211e7b540baa265d7 Mon Sep 17 00:00:00 2001 From: Luke Alonso Date: Mon, 30 Nov 2026 09:34:48 -0800 Subject: [PATCH] perf(helios): sync vendored roce_proxy.c to FlashInfer 2a07747b8 (2-step ring2) Why: On 4-rank switchless dual-port RoCE rings (DGX Spark / NVIDIA GB10, SM121), small-message BF16 allreduce previously used the oneshot protocol, which relays across opposite ranks over CPU mailbox hops. FlashInfer's merged `local-inference-lab/flashinfer#192` (`2a07747b8e12e54ec8e8e4bbef34de8586618e68`) adds a 2-step neighbor-only ring allreduce (`KIND_RING2_STEP1` / `KIND_RING2_STEP2`) plus small-message inline WR and ring-relay improvements in `_roce_proxy.c`. Per `docs/roce-ring.md`, Helios's vendored `helios/native/roce_proxy.c` must remain byte-identical to FlashInfer's `_roce_proxy.c` after its provenance header so preparation and serving share the transport protocol. What changed: Re-vendored `helios/native/roce_proxy.c` byte-for-byte (after the 9-line provenance header) from `local-inference-lab/flashinfer` commit `2a07747b8e12e54ec8e8e4bbef34de8586618e68` (`#192`). Relative to Flare `main`, this brings in: - `KIND_RING2_STEP1` (5) and `KIND_RING2_STEP2` (6) direct-neighbor RDMA write handling (`post_direct_striped_batched`, `post_ring2_step`) with batched completion signaling (`RING2_SIGNAL_STRIDE = 16`, `RING2_MAX_OUTSTANDING = 6`, statically verified against `ROCE_SEND_DEPTH` with queue slack for relay ops and ring ACKs). - Small-message and ring-relay transport improvements from FlashInfer `#192`: QP `max_inline_data` raised from 16 to 256 (with fallback to 16 and per-HCA `max_inline` tracking) to enable `IBV_SEND_INLINE` on relay mailbox and `post_striped` payloads `<= max_inline`; `ring_next_from` and precomputed `ring_relays_origin[]` so `ring_service` skips non-relayed origins; `post_op` posting relayed peers before direct peers on rings; and idle-loop polling (`ring_service` every spin when `!ring2_active`, every 16th when `ring2_active`; `drain_cqs` every 16th). - High-capacity CPU pinning (`pin_high_capacity_cpus`, `"flare-roce"` thread name) already merged upstream in `flashinfer#156` (overlaps open `#576`). - Existing control opcodes (0..4) and `ROCE_ABI_VERSION = 14` remain unchanged, preserving full compatibility with existing ABI-14 cores. Claimed benefit and result: Enables native Rust serving (`flare run`) to drive b12x's 2-step RoCE ring allreduce on 4-rank switchless rings without opposite-rank CPU relay hops. Note that an isolated `#643`-only run on a `main`-exported core does not emit kinds 5/6; paired with `local-inference-lab/flashinfer#192` (`2a07747b8`), `local-inference-lab/flashinfer#193`, `local-inference-lab/hyperflare-recipes#112`, and `NVIDIA/hyperflare-inference#644` on 4x DGX Spark TP4 DeepSeek-V4.1-Flash serving (`max_tokens = 256`, 5-prompt suite): - In node-level nsys P=4 traces, `RoceRing2AllReduceLaunch` averages 31.8-33.0 us/call across 88 calls/round (eliminating opposite-rank CPU relay hops on small BF16 allreduces). - Combined-stack matched-depth GPU round times dropped 1.5-2.2 ms across P=1..6 versus pre-existing non-ring2 baseline core `gb10-tp4-202611291503` (P=1: 22.3 -> 20.8 ms, P~4: 34.59 -> 32.36 ms, P~5: 35.92 -> 34.16 ms; 5-prompt mean 74.02 -> 76.11 tok/s under `balanced`, `greedy_code` 74.39 -> 80.89 tok/s). Supporting evidence and limitations: - Byte-parity check: `diff` between `tail -n +10 helios/native/roce_proxy.c` and `flashinfer@2a07747b8:flashinfer/experimental/b12x/comm/roce/_roce_proxy.c` returned 0 bytes diff. - Native RoCE proxy unit tests (`cargo test --locked --release --manifest-path helios/Cargo.toml --features roce --lib --target-dir flare-cli/target execution::roce`): **30 passed, 0 failed** (6 `execution::roce::tests` + 24 `execution::roce_fabric::tests`). - Full crate tests (`cargo test --locked --release` on `helios` with `--features runtime-core,server`, `flare`, `flare-cli`): all passed. - Live 4-rank multi-GPU collective execution verified on 4x DGX Spark dual-port switchless RoCE rings serving both ring2-enabled cores (`gb10-tp4-202611301141`, `gb10-tp4-202611301553`, 57 graphs each) and legacy non-ring2 ABI-14 core (`gb10-tp4-202611291503`). - Limitations / unrun checks: - Keeping `ROCE_ABI_VERSION = 14` preserves compatibility with existing ABI-14 cores, but an older Flare binary without `#643` loading a newer ring2-exported core will connect and fail on replay op 1 with `unknown op kind 5` rather than rejecting at startup. - Collides on `roce_proxy.c` with open `#576` (`pin_high_capacity_cpus` from merged `flashinfer#156`) and `#577` (`RING_WINDOW` / `reserve`); whichever lands second will rebase cleanly against the vendored file. - Non-4-rank ring topologies and non-SM121 RoCE NICs were not benchmarked. Design-Signed-Off-By: Antigravity (Gemini 3.1 Pro); design-review=2e5b --- helios/native/roce_proxy.c | 349 +++++++++++++++++++++++++++++++++---- 1 file changed, 315 insertions(+), 34 deletions(-) diff --git a/helios/native/roce_proxy.c b/helios/native/roce_proxy.c index 997a6f816..be4a21e8d 100644 --- a/helios/native/roce_proxy.c +++ b/helios/native/roce_proxy.c @@ -107,6 +107,13 @@ // ready word of N + 1 also releases chunk c of N, and no peer can begin op N + 2, which would // reuse N's slot, before receiving our N. // +// A 4-rank closed ring can perform a two-step neighbor-only all-reduce over the +// one-shot send/recv/flag slots (ctrl kind[slot] = 5 for step 1, 6 for step 2). +// Each step splits an even-pack payload into two equal halves (lo and hi), writes +// lo directly to one 1-hop neighbor and hi directly to the other with no CPU +// mailbox relay, and alternates which neighbor receives which half between steps +// so every rank obtains the full sum in two direct hops. +// // This file is compiled by b12x.comm.roce._proxy at first use with the host // gcc and libibverbs; it must stay plain C with no CUDA dependency. @@ -163,6 +170,21 @@ _Static_assert(CTRL_CREDIT >= CTRL_READY_VL + ROCE_VARLEN_MAX_CHUNKS && #define KIND_VARLEN 2 #define KIND_REDUCE_SCATTER 3 #define KIND_GATHER_SHARDS 4 +#define KIND_RING2_STEP1 5u +#define KIND_RING2_STEP2 6u +// Two-step ring direct writes signal 1 in RING2_SIGNAL_STRIDE ops per (HCA, peer) +// QP. Capping signaled outstanding ops at RING2_MAX_OUTSTANDING keeps at most +// RING2_MAX_OUTSTANDING * RING2_SIGNAL_STRIDE * 2 + (RING2_SIGNAL_STRIDE - 1) * 2 +// = 222 WRs in flight on a 512-deep send queue, leaving room for up to 85 +// subsequent three-WR relay ops (255 WRs) plus world=4 ring ACKs. +#define RING2_SIGNAL_STRIDE 16u +#define RING2_MAX_OUTSTANDING 6u +_Static_assert((RING2_SIGNAL_STRIDE & (RING2_SIGNAL_STRIDE - 1u)) == 0, + "RING2_SIGNAL_STRIDE must be a power of two"); +_Static_assert(RING2_MAX_OUTSTANDING* RING2_SIGNAL_STRIDE * 2 + (RING2_SIGNAL_STRIDE - 1u) * 2 + + (ROCE_SEND_DEPTH / 6) * 3 <= + ROCE_SEND_DEPTH, + "batched ring2 WRs must fit inside ROCE_SEND_DEPTH with queue slack"); // run_pipe_op phase mask: reduce-scatter chunks, all-gather chunks. #define PIPE_PHASE_RS 1u #define PIPE_PHASE_AG 2u @@ -227,11 +249,13 @@ typedef struct { struct ibv_cq* cq; struct ibv_qp* qp[ROCE_MAX_PEERS][ROCE_MAX_LANES]; uint32_t outstanding[ROCE_MAX_PEERS][ROCE_MAX_LANES]; + uint8_t ring2_unsignaled[ROCE_MAX_PEERS]; uint64_t writes_completed; uint64_t bytes_posted; union ibv_gid gid; uint16_t lid; enum ibv_mtu mtu; + uint32_t max_inline; } roce_hca_t; typedef struct { @@ -285,6 +309,7 @@ typedef struct { uint8_t ring_target[ROCE_MAX_PEERS][ROCE_MAX_RAILS][RING_WINDOW]; uint8_t ring_previous[ROCE_MAX_PEERS][ROCE_MAX_RAILS][RING_WINDOW]; uint64_t ring_inflight[ROCE_MAX_PEERS][ROCE_MAX_RAILS][RING_WINDOW]; + uint8_t ring_relays_origin[ROCE_MAX_PEERS]; uint64_t ring_forwarded; // All-gather relay (roce_set_relay): per peer, the rank that forwards our chunks to it (-1: we // send them); per source, the rank we forward its chunks to (-1: none). @@ -307,14 +332,15 @@ static ring_descriptor_t* ring_outgoing(const roce_ctx_t* c, int origin, int rai return (ring_descriptor_t*)(c->ring + ring_outgoing_offset(c->world, ring_slot(origin, rail, slot))); } -// Next hop toward target: on a line always toward it; on a cycle the shorter way, clockwise on a -// tie. -static int ring_next(const roce_ctx_t* c, int target) { - if (c->ring_open) return target > c->rank ? c->rank + 1 : c->rank - 1; - int clockwise = (target - c->rank + c->world) % c->world; - int counter = (c->rank - target + c->world) % c->world; - return clockwise <= counter ? (c->rank + 1) % c->world : (c->rank + c->world - 1) % c->world; +// Next hop from `from` toward `target`: on a line always toward it; on a cycle the shorter way, +// clockwise on a tie. +static int ring_next_from(const roce_ctx_t* c, int from, int target) { + if (c->ring_open) return target > from ? from + 1 : from - 1; + int clockwise = (target - from + c->world) % c->world; + int counter = (from - target + c->world) % c->world; + return clockwise <= counter ? (from + 1) % c->world : (from + c->world - 1) % c->world; } +static int ring_next(const roce_ctx_t* c, int target) { return ring_next_from(c, c->rank, target); } static int ring_relayed(const roce_ctx_t* c, int peer) { if (c->ring == NULL || peer == c->rank) return 0; if (c->ring_open) return peer != c->rank + 1 && peer != c->rank - 1; @@ -451,6 +477,7 @@ static int open_hca(roce_ctx_t* c, int h, const char* name) { set_err(c, "ibv_create_cq", errno); return -1; } + hca->max_inline = UINT32_MAX; for (int p = 0; p < c->world; p++) { if (p == c->rank) { continue; @@ -464,19 +491,25 @@ static int open_hca(roce_ctx_t* c, int h, const char* name) { attr.cap.max_recv_wr = 1; attr.cap.max_send_sge = 1; attr.cap.max_recv_sge = 1; - attr.cap.max_inline_data = 16; for (int l = 0; l < c->n_lane; l++) { + attr.cap.max_inline_data = 256; hca->qp[p][l] = ibv_create_qp(hca->pd, &attr); + if (hca->qp[p][l] == NULL) { + attr.cap.max_inline_data = 16; + hca->qp[p][l] = ibv_create_qp(hca->pd, &attr); + } if (hca->qp[p][l] == NULL) { set_err(c, "ibv_create_qp", errno); return -1; } if (attr.cap.max_inline_data < 8) { - snprintf(c->err, sizeof(c->err), - "HCA %d granted max_inline_data=%u, need at least 8 bytes", + snprintf(c->err, sizeof(c->err), "HCA %d granted max_inline_data=%u, need at least 8 bytes", h, attr.cap.max_inline_data); return -1; } + if (attr.cap.max_inline_data < hca->max_inline) { + hca->max_inline = attr.cap.max_inline_data; + } struct ibv_qp_attr init; memset(&init, 0, sizeof(init)); init.qp_state = IBV_QPS_INIT; @@ -681,8 +714,8 @@ int roce_connect(roce_ctx_t* c, const void* blobs, uint64_t blobs_len, const int for (int k = 0; k < n_rail; k++) { int h = routes[(p * n_rail + k) * 2]; int r = routes[(p * n_rail + k) * 2 + 1]; - if (h < 0 || h >= c->n_hca || r < 0 || r >= ROCE_MAX_HCAS || - all[p].mtu[r] == 0 || all[p].qp_num[r][c->rank][0] == 0) { + if (h < 0 || h >= c->n_hca || r < 0 || r >= ROCE_MAX_HCAS || all[p].mtu[r] == 0 || + all[p].qp_num[r][c->rank][0] == 0) { snprintf(c->err, sizeof(c->err), "invalid route to rank %d rail %d: %d -> %d", p, k, h, r); return -1; } @@ -696,6 +729,23 @@ int roce_connect(roce_ctx_t* c, const void* blobs, uint64_t blobs_len, const int } } c->n_rail = n_rail; + memset(c->ring_relays_origin, 0, sizeof(c->ring_relays_origin)); + if (c->ring != NULL) { + for (int origin = 0; origin < c->world; origin++) { + if (origin == c->rank) continue; + for (int target = 0; target < c->world; target++) { + if (target == origin || target == c->rank) continue; + int cur = origin; + for (int hop = 0; hop < c->world && cur != target; hop++) { + cur = ring_next_from(c, cur, target); + if (cur == c->rank) { + c->ring_relays_origin[origin] = 1; + break; + } + } + } + } + } for (int p = 0; p < c->world; p++) { if (p == c->rank || ring_relayed(c, p)) { continue; @@ -775,13 +825,10 @@ static int drain_cq(roce_ctx_t* c, int h) { return 0; } -// Wait until lane `lane` of HCA h can take one more stripe to peer p. Direct stripes need two -// requests and relay packets chain three. One sixth of the send queue (85 signaled ops, at most -// 255 chained WRs) leaves room for up to 128 unreserved per-origin ACKs at world=16 plus provider -// SQE overhead. -static int reserve(roce_ctx_t* c, int h, int p, int lane) { +// Wait until lane `lane` of HCA h has fewer than `limit` signaled ops outstanding to peer p. +static int reserve_limit(roce_ctx_t* c, int h, int p, int lane, uint32_t limit) { roce_hca_t* hca = &c->hca[h]; - while (hca->outstanding[p][lane] >= ROCE_SEND_DEPTH / 6) { + while (hca->outstanding[p][lane] >= limit) { if (drain_all(c) != 0) { return -1; } @@ -797,6 +844,14 @@ static int reserve(roce_ctx_t* c, int h, int p, int lane) { return 0; } +// Wait until lane `lane` of HCA h can take one more stripe to peer p. Direct stripes need two +// requests and relay packets chain three. One sixth of the send queue (85 signaled ops, at most +// 255 chained WRs) leaves room for up to 128 unreserved per-origin ACKs at world=16 plus provider +// SQE overhead. +static int reserve(roce_ctx_t* c, int h, int p, int lane) { + return reserve_limit(c, h, p, lane, ROCE_SEND_DEPTH / 6); +} + // Each original sender has RING_WINDOW private mailbox slots per rail at every // relay. It reuses a slot only after the end-to-end ack of the packet that last // used it returns. Different senders never hold each other's mailbox credit, so @@ -853,12 +908,18 @@ static int ring_submit(roce_ctx_t* c, int origin, int rail, int slot, int next, a.sg_list = &data; a.num_sge = 1; a.opcode = IBV_WR_RDMA_WRITE; + if (d->nbytes > 0 && d->nbytes <= c->hca[h].max_inline) { + a.send_flags = IBV_SEND_INLINE; + } a.wr.rdma.remote_addr = mailbox + offsetof(ring_mailbox_t, data); a.wr.rdma.rkey = c->ring_rkey[rail][next]; a.next = &b; b.sg_list = &header; b.num_sge = 1; b.opcode = IBV_WR_RDMA_WRITE; + if (sizeof(*d) - 8 <= c->hca[h].max_inline) { + b.send_flags = IBV_SEND_INLINE; + } b.wr.rdma.remote_addr = mailbox + 8; b.wr.rdma.rkey = c->ring_rkey[rail][next]; b.next = &z; @@ -975,16 +1036,16 @@ static int ring_service_slot(roce_ctx_t* c, int origin, int rail, int slot) { static int ring_service(roce_ctx_t* c) { if (c->ring == NULL) return 0; for (int origin = 0; origin < c->world; origin++) { - if (origin == c->rank) continue; + if (origin == c->rank || !c->ring_relays_origin[origin]) continue; for (int rail = 0; rail < c->n_rail; rail++) { int ready[RING_WINDOW], count = 0; uint64_t serials[RING_WINDOW]; // The slot loads below are not one snapshot: a later packet can land between them. A packet // read here as unarrived but whose successor is read as arrived would be forwarded after it, // and the successor of a stripe's last chunk carries the receiver's readiness flag. The first - // pass bounds the forwarded serials by the newest arrival it observed. Packets from one origin - // on one rail reach this relay through one queue pair in posting order, so every earlier - // packet had landed before that load and the second pass sees each of them. + // pass bounds the forwarded serials by the newest arrival it observed. Packets from one + // origin on one rail reach this relay through one queue pair in posting order, so every + // earlier packet had landed before that load and the second pass sees each of them. uint64_t bound = 0; for (int slot = 0; slot < RING_WINDOW; slot++) { if (c->ring_pending[origin][rail][slot]) { @@ -1120,6 +1181,9 @@ static int post_striped(roce_ctx_t* c, int p, const uint8_t* send, uint64_t remo data_wr.sg_list = &data_sge; data_wr.num_sge = 1; data_wr.opcode = IBV_WR_RDMA_WRITE; + if (stripe_bytes <= hca->max_inline) { + data_wr.send_flags = IBV_SEND_INLINE; + } data_wr.wr.rdma.remote_addr = remote + remote_off + byte_offset; data_wr.wr.rdma.rkey = c->peer_rkey[k][p]; first_wr = &data_wr; @@ -1137,17 +1201,25 @@ static int post_striped(roce_ctx_t* c, int p, const uint8_t* send, uint64_t remo return 0; } -static int drain_all(roce_ctx_t* c) { +static int drain_cqs(roce_ctx_t* c) { for (int h = 0; h < c->n_hca; h++) { if (drain_cq(c, h) != 0) { return -1; } } - return ring_service(c); + return 0; +} + +static int drain_all(roce_ctx_t* c) { + if (ring_service(c) != 0) { + return -1; + } + return drain_cqs(c); } // Post one-shot op seq to every peer: the whole payload, one stripe plus its flag -// write per rail, each on the HCA routed to that peer for the rail. +// write per rail, each on the HCA routed to that peer for the rail. Post relayed +// multi-hop peers first so the critical-path hop starts earliest on a ring. static int post_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes) { if (nbytes == 0 || nbytes % 16 != 0 || nbytes > c->slot_bytes) { snprintf(c->err, sizeof(c->err), @@ -1157,19 +1229,141 @@ static int post_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes) { } uint32_t slot = seq & 1u; const uint8_t* send = c->region + c->send_off + (size_t)slot * c->slot_bytes; - for (int p = 0; p < c->world; p++) { - if (p == c->rank) { - continue; + uint64_t remote_off = c->recv_off + ((uint64_t)c->rank * ROCE_SLOTS + slot) * c->slot_bytes; + uint64_t flag_off = c->flag_off + ((uint64_t)c->rank * ROCE_SLOTS + slot) * (uint64_t)c->n_rail * + ROCE_FLAG_STRIDE; + int passes = c->ring != NULL ? 2 : 1; + for (int pass = 0; pass < passes; pass++) { + int want_relayed = (c->ring != NULL && pass == 0); + for (int p = 0; p < c->world; p++) { + if (p == c->rank) { + continue; + } + if (c->ring != NULL && (ring_relayed(c, p) != 0) != want_relayed) { + continue; + } + if (post_striped(c, p, send, remote_off, nbytes / 16, flag_off, seq, 0) != 0) { + return -1; + } + } + } + c->ops_posted += 1; + return drain_all(c); +} + +// Write `packs` 16-byte packs directly to 1-hop neighbor `p` on lane 0, striped +// across all rails, signaling 1 in RING2_SIGNAL_STRIDE ops per (HCA, peer) QP. +static int post_direct_striped_batched(roce_ctx_t* c, int p, const uint8_t* send, + uint64_t remote_off, uint32_t packs, uint64_t flag_off, + uint32_t seq) { + uint32_t seq_copy = seq; + uint32_t pack_offset = 0; + uint64_t remote = c->peer_addr[p]; + for (int k = 0; k < c->n_rail; k++) { + int h = c->rail_hca[p][k]; + roce_hca_t* hca = &c->hca[h]; + if (reserve_limit(c, h, p, 0, RING2_MAX_OUTSTANDING) != 0) { + return -1; + } + uint32_t stripe_packs = packs / (uint32_t)c->n_rail; + if ((uint32_t)k < packs % (uint32_t)c->n_rail) { + stripe_packs += 1; + } + uint32_t stripe_bytes = stripe_packs * 16; + uint64_t byte_offset = (uint64_t)pack_offset * 16; + int signaled = (hca->ring2_unsignaled[p] == 0); + struct ibv_sge flag_sge = { + .addr = (uint64_t)(uintptr_t)&seq_copy, + .length = 4, + .lkey = 0, + }; + struct ibv_send_wr flag_wr; + memset(&flag_wr, 0, sizeof(flag_wr)); + flag_wr.wr_id = (uint64_t)p; + flag_wr.sg_list = &flag_sge; + flag_wr.num_sge = 1; + flag_wr.opcode = IBV_WR_RDMA_WRITE; + flag_wr.send_flags = IBV_SEND_INLINE | (signaled ? IBV_SEND_SIGNALED : 0); + flag_wr.wr.rdma.remote_addr = remote + flag_off + (uint64_t)k * ROCE_FLAG_STRIDE; + flag_wr.wr.rdma.rkey = c->peer_rkey[k][p]; + + struct ibv_send_wr data_wr; + struct ibv_sge data_sge; + struct ibv_send_wr* first_wr = &flag_wr; + if (stripe_bytes != 0) { + data_sge = (struct ibv_sge){ + .addr = (uint64_t)(uintptr_t)(send + byte_offset), + .length = stripe_bytes, + .lkey = hca->mr->lkey, + }; + memset(&data_wr, 0, sizeof(data_wr)); + data_wr.wr_id = (uint64_t)p; + data_wr.next = &flag_wr; + data_wr.sg_list = &data_sge; + data_wr.num_sge = 1; + data_wr.opcode = IBV_WR_RDMA_WRITE; + if (stripe_bytes <= hca->max_inline) { + data_wr.send_flags = IBV_SEND_INLINE; + } + data_wr.wr.rdma.remote_addr = remote + remote_off + byte_offset; + data_wr.wr.rdma.rkey = c->peer_rkey[k][p]; + first_wr = &data_wr; } - uint64_t remote_off = c->recv_off + ((uint64_t)c->rank * ROCE_SLOTS + slot) * c->slot_bytes; - uint64_t flag_off = c->flag_off + ((uint64_t)c->rank * ROCE_SLOTS + slot) * - (uint64_t)c->n_rail * ROCE_FLAG_STRIDE; - if (post_striped(c, p, send, remote_off, nbytes / 16, flag_off, seq, 0) != 0) { + struct ibv_send_wr* bad = NULL; + int rc = ibv_post_send(hca->qp[p][0], first_wr, &bad); + if (rc != 0) { + set_err(c, "ibv_post_send(ring2)", rc); return -1; } + if (signaled) { + hca->outstanding[p][0] += 1; + } + hca->ring2_unsignaled[p] = + (uint8_t)((hca->ring2_unsignaled[p] + 1u) & (RING2_SIGNAL_STRIDE - 1u)); + hca->bytes_posted += stripe_bytes; + pack_offset += stripe_packs; + } + return 0; +} + +// Post one step (step=0 for KIND_RING2_STEP1, step=1 for KIND_RING2_STEP2) of a +// 4-rank closed-ring two-step split-buffer all-reduce. Both halves go directly +// to 1-hop neighbors with no mailbox relay. +static int post_ring2_step(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, int step) { + if (c->world != 4 || c->ring == NULL || c->ring_open) { + snprintf(c->err, sizeof(c->err), + "RoCE two-step ring op requires a 4-rank closed ring (world=%d, ring=%d, open=%d)", + c->world, c->ring != NULL, c->ring_open); + return -1; + } + if (nbytes == 0 || (nbytes % 32) != 0 || nbytes > c->slot_bytes) { + snprintf(c->err, sizeof(c->err), + "RoCE two-step ring payload size must be a positive multiple of 32 bytes within the " + "slot, got %u", + nbytes); + return -1; + } + uint32_t slot = seq & 1u; + int even = (c->rank & 1) == 0; + int cw = (c->rank + 1) & 3; + int ccw = (c->rank + 3) & 3; + int p_lo = (even ^ step) ? cw : ccw; + int p_hi = (even ^ step) ? ccw : cw; + uint32_t half_bytes = nbytes / 2; + uint32_t half_packs = nbytes / 32; + const uint8_t* send = c->region + c->send_off + (size_t)slot * c->slot_bytes; + uint64_t remote_off = c->recv_off + ((uint64_t)c->rank * ROCE_SLOTS + slot) * c->slot_bytes; + uint64_t flag_off = c->flag_off + ((uint64_t)c->rank * ROCE_SLOTS + slot) * (uint64_t)c->n_rail * + ROCE_FLAG_STRIDE; + if (post_direct_striped_batched(c, p_lo, send, remote_off, half_packs, flag_off, seq) != 0) { + return -1; + } + if (post_direct_striped_batched(c, p_hi, send + half_bytes, remote_off + half_bytes, half_packs, + flag_off, seq) != 0) { + return -1; } c->ops_posted += 1; - return drain_all(c); + return 0; } // First pack of shard j of a total-pack message (the kernel uses the same split). @@ -1502,8 +1696,79 @@ static int run_varlen_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, uint32_t return drain_all(c); } +#ifdef __linux__ +// Restrict the proxy polling thread to high-capacity CPUs within its current +// affinity mask on asymmetric Linux systems (such as ARM big.LITTLE). No-op +// when sysfs cpu_capacity is absent, homogeneous, already restricted to one +// tier, or would collapse below two CPUs. +static void pin_high_capacity_cpus(void) { + cpu_set_t allowed; + CPU_ZERO(&allowed); + if (sched_getaffinity(0, sizeof(allowed), &allowed) != 0) { + return; + } + int cpus[CPU_SETSIZE]; + uint32_t caps[CPU_SETSIZE]; + int count = 0; + uint32_t min_cap = UINT32_MAX; + uint32_t max_cap = 0; + for (int cpu = 0; cpu < CPU_SETSIZE; cpu++) { + if (!CPU_ISSET(cpu, &allowed)) { + continue; + } + char path[128]; + snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/cpu_capacity", cpu); + FILE* f = fopen(path, "r"); + if (f == NULL) { + return; + } + unsigned long val = 0; + int matched = fscanf(f, "%lu", &val); + fclose(f); + if (matched != 1 || val == 0 || val > UINT32_MAX) { + return; + } + cpus[count] = cpu; + caps[count] = (uint32_t)val; + if (caps[count] < min_cap) min_cap = caps[count]; + if (caps[count] > max_cap) max_cap = caps[count]; + count++; + } + if (count < 2 || min_cap == max_cap) { + return; + } + cpu_set_t next; + CPU_ZERO(&next); + int selected = 0; + for (int i = 0; i < count; i++) { + if ((uint64_t)caps[i] * 10 >= (uint64_t)max_cap * 9) { + CPU_SET(cpus[i], &next); + selected++; + } + } + if (selected < 2) { + CPU_ZERO(&next); + selected = 0; + uint64_t sum = (uint64_t)min_cap + (uint64_t)max_cap; + for (int i = 0; i < count; i++) { + if ((uint64_t)caps[i] * 2 > sum) { + CPU_SET(cpus[i], &next); + selected++; + } + } + } + if (selected >= 2 && selected < count) { + (void)sched_setaffinity(0, sizeof(next), &next); + } +} +#endif + static void* proxy_main(void* arg) { roce_ctx_t* c = (roce_ctx_t*)arg; +#ifdef __linux__ + (void)pthread_setname_np(pthread_self(), "flare-roce"); + pin_high_capacity_cpus(); +#endif volatile uint32_t* ctrl = (volatile uint32_t*)(c->region + c->ctrl_off); // Spin while ops are flowing. After ROCE_IDLE_SPINS polls without a // doorbell, request a short nanosleep between polls (the OS decides the @@ -1525,7 +1790,19 @@ static void* proxy_main(void* arg) { } if (seq == c->last_seq) { idle++; - if (idle % 64 == 0) { + if (c->ring != NULL) { + int ring2_active = (c->last_kind == KIND_RING2_STEP1 || c->last_kind == KIND_RING2_STEP2); + if (!ring2_active || (idle & 15u) == 0) { + if (ring_service(c) != 0) { + atomic_store(&c->failed, 1); + return NULL; + } + } + if ((idle & 15u) == 0 && drain_cqs(c) != 0) { + atomic_store(&c->failed, 1); + return NULL; + } + } else if ((idle & 63u) == 0) { if (drain_all(c) != 0) { atomic_store(&c->failed, 1); return NULL; @@ -1557,6 +1834,10 @@ static void* proxy_main(void* arg) { int rc; if (kind == KIND_ONESHOT) { rc = post_op(c, s, nbytes); + } else if (kind == KIND_RING2_STEP1) { + rc = post_ring2_step(c, s, nbytes, 0); + } else if (kind == KIND_RING2_STEP2) { + rc = post_ring2_step(c, s, nbytes, 1); } else if (kind == KIND_TWOSHOT || kind == KIND_REDUCE_SCATTER || kind == KIND_GATHER_SHARDS) { uint32_t phases = kind == KIND_TWOSHOT ? (PIPE_PHASE_RS | PIPE_PHASE_AG)