Skip to content
Merged
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
21 changes: 11 additions & 10 deletions docs/cn/rdma.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ RDMA 通过 verbs API 传输数据,复用 bRPC 的 Socket 接口。默认每

bRPC 使用 RDMA RC 模式,每个 `RdmaEndpoint` 对应一个 QP。建连时先通过 TCP 交换 GID、QPN 等参数,再建立 RDMA 连接,这个握手过程复用了 bRPC 的 AppConnect 逻辑。RDMA 连接建立后,原来的 TCP 连接保持 EST 状态,不再传输业务数据。TCP 连接断开时,对应的 RDMA 连接也会置错。

客户端每次新建逻辑连接都先使用 v3 握手,无需配置版本。如果服务端在返回 v3 标识之前断开或重置连接,或返回 v2 标识,客户端会新建 TCP 连接并用 v2 重试一次。TCP 建连、握手及重连共用原始连接截止时间。已经识别为 v3 的报文损坏、握手超时或 v2 重试失败时,连接失败;服务端正常要求回退 TCP 时,客户端继续使用当前连接。
客户端每次新建逻辑连接都先使用 v3 握手。如果首次尝试在收到 v3 标识前遇到对端断开、重置连接或返回 v2 标识,客户端会新建 TCP 连接并使用 v2 专用 RQ。每次逻辑建连最多发送两轮 hello,共用原始连接截止时间。v3 报文截断、解析错误或握手超时会导致建连失败。

`RqConnection` 使用 IOBuf 支持零拷贝收发。发送时,`_sbuf` 保存数据块的引用,直到确认发送完成后才释放。接收时,预先分配的 IOBuf 块由 `_rbuf` 持有,并通过接收请求提交给网卡。每个接收块的大小固定,默认使用 8 KiB 的块。单个发送工作请求(WR)的数据长度不能超过对端接收块的有效容量,较大的 RPC 按此上限分片。

Expand All @@ -65,7 +65,7 @@ SRQ(Shared Receive Queue)让多个连接共享接收队列和预分配的缓

每个 bthread tag 下默认有 4 个 worker,按需创建,连接分配到其中一个 worker。每个 worker 管理一个 SRQ,收到事件时由 bthread 处理队列,不占用专门的常驻线程。

`RdmaEndpoint` 通过 [RdmaConnection 接口](../../src/brpc/rdma/rdma_connection.h) 选择 `RqConnection`(专用 RQ/CQ)或 `SrqConnection`(共享 SRQ/CQ),两条路径都通过 `RdmaEndpoint::Deliver` 将数据交给 InputMessenger 解析。开启 SRQ 时跳过专用 RQ 模式的 QP 预建。
`RdmaEndpoint` 通过 [RdmaConnection 接口](../../src/brpc/rdma/rdma_connection.h) 选择 `RqConnection`(专用 RQ/CQ)或 `SrqConnection`(共享 SRQ/CQ),两条路径都通过 `RdmaEndpoint::Deliver` 将数据交给 InputMessenger 解析。

## 适用场景

Expand All @@ -82,12 +82,12 @@ SRQ 适用于海量连接、稀疏请求的 RPC 场景。例如,客户端需
## 限制

- **连接间有资源竞争**:同一 worker 的连接共用接收队列、TX CQ 额度和 CQ 处理能力。大量连接同时发送时,可能需要等待发送额度,或因对端暂无接收请求而触发 RNR 重试。TX CQ 额度目前不保证在连接间严格公平分配。共享 CQ 或 SRQ 的致命错误会影响该 worker 上的所有连接。
- **仅支持事件模式和 v3 握手**:不能同时开启 `rdma_use_srq` 与 `rdma_use_polling`。网卡及驱动需要支持 RC、SRQ 和配置的队列容量,bRPC 通过 verbs 查询设备能力,不限制网卡名称。
- **握手兼容性**:先升级服务端,再开启客户端的 v3/SRQ。SRQ 服务端仍可接受专用 RQ 连接。若对端支持 v3 但未启用 SRQ,则回退到 TCP,不会改用专用 RQ。不支持 v3 的旧服务端无法保证回退。
- **本地条件**。SRQ 需要 v3 握手、事件模式以及足够的网卡和驱动资源。使用 polling 模式时,即使客户端优先使用 SRQ,首轮 hello 也直接请求专用 RQ。
- **握手回退**。若客户端在发送 hello 前无法创建 SRQ,会在原 TCP 连接上尝试专用 RQ。v3 服务端仅在拒绝本轮握手时设置 `declined=true`,并将必填参数清零,以便旧客户端识别。若应答中没有该字段,客户端也会将必填参数全零,或专用 RQ 参数合法且 `qp_num=0` 的历史应答识别为拒绝。收到拒绝报文、收到有效的专用 RQ 应答,或本地 SRQ QP 激活失败时,客户端发送 `ACK=0` 结束本轮握手。本地专用 QP 分配成功后,客户端在新连接上交换 v3 专用 RQ 参数。如果分配失败,就在原连接上使用 TCP。如果第二轮也被拒绝,则在第二条连接上使用 TCP。仅支持 TCP 的服务端返回相同的拒绝报文,因此启用 SRQ 的客户端可能多建一条连接。

## 使用方法

启用 RDMA 后,两端设置 `rdma_use_srq=true`,客户端设置 `rdma_client_handshake_version=3`,并保持 `rdma_use_polling=false`。开关说明见 [SRQ 开关与参数](#srq-开关与参数)。
启用 RDMA 后,在客户端设置 `rdma_connect_srq=true` 即可优先请求 SRQ。两端均使用事件模式(默认)。服务端默认接受 SRQ 请求,按需创建或复用 worker。如需拒绝新的 SRQ 请求,在服务端设置 `rdma_accept_srq=false`。参见 [SRQ 开关与参数](#srq-开关与参数)。

## 容量规划

Expand All @@ -108,12 +108,12 @@ SRQ 适用于海量连接、稀疏请求的 RPC 场景。例如,客户端需

可配置参数如下:
* `rdma_trace_verbose`:打印 RDMA 建连日志,默认 `false`。
* rdma_client_handshake_version: 已废弃,仅为兼容旧启动参数保留。该值不再参与版本选择;客户端始终先尝试 v3,并在对端拒绝 v3 后重连并用 v2 重试一次。
* `rdma_client_handshake_version` 已废弃,保留以兼容旧启动参数。客户端始终先尝试 v3。
* `rdma_recv_zerocopy`:启用接收零拷贝,默认 `true`。
* `rdma_zerocopy_min_size`:接收零拷贝的最小片段大小,默认 512 B。
* `rdma_recv_block_type`:接收缓冲区的块类型,可选 `default`(8 KiB)、`large`(64 KiB)或 `huge`(2 MiB),默认 `default`。
* `rdma_prepared_qp_size`:预先创建的 QP 队列大小,默认 128。
* `rdma_prepared_qp_cnt`:预先创建的 QP 数量,默认 1024。
* `rdma_prepared_qp_cnt` 控制**每个进程**预建的专用 QP 数量,默认 1024。事件模式下,每个条目占用 1 个 QP、2 个 CQ 和 1 个 completion channel fd。默认资源池合计占用 1024 个 QP、2048 个 CQ 和 1024 个 fd。polling 模式下,每个条目占用 1 个 QP 和 1 个收发共用的 CQ,completion channel fd 开销为零。此前通过启用 SRQ 跳过专用 QP 预建的部署,可在升级时将该参数设为 0,继续按需分配专用 QP。
* `rdma_max_sge`:每个发送请求允许的最大 SGE 数,默认 0,表示使用设备支持的上限。
* `rdma_sq_size`:SQ 大小,默认 128。
* `rdma_rq_size`:RQ 大小,默认 128。
Expand All @@ -129,14 +129,15 @@ SRQ 适用于海量连接、稀疏请求的 RPC 场景。例如,客户端需
* `rdma_use_polling`:启用 RDMA 轮询模式,默认 `false`。
* `rdma_poller_num`:轮询器数量,默认 1。
* `rdma_poller_yield`:轮询器是否主动让出 CPU,默认 `false`。
* `rdma_client_handshake_version`:客户端 RDMA 握手版本,默认 2。使用 SRQ 时设为 3,服务端根据收到的报文识别版本。
* `event_dispatcher_edisp_unsched`:控制 EventDispatcher 是否不可被调度,默认 `false`。设为 `true` 时不可被调度。
* `rdma_disable_bthread`:禁用 bthread,默认 `false`。

## SRQ 开关与参数

以下为 SRQ 专用配置,在进程启动时设置。除另有说明外,容量均按**每个 worker**计算:
* `rdma_use_srq`:SRQ 开关,默认 `false`。需两端启用并使用 v3 握手,仅开启此项而保留默认 v2 握手仍会使用专用 RQ。
以下 SRQ 配置在进程启动时设置,`rdma_accept_srq` 也支持运行时修改。除另有说明外,容量均按**每个 worker**计算。

* `rdma_connect_srq` 控制出站连接请求的接收队列,默认 `false`(专用 RQ)。设为 `true` 时优先请求 SRQ。
* `rdma_accept_srq` 控制整个进程对新入站 SRQ 握手的准入,默认 `true`。设为 `false` 后,服务端在分配 SRQ 资源前静默回复 `declined=true`。v2 和 v3 的专用 RQ 请求仍按原流程协商。已有 worker 和接收池继续供已建立的 SRQ 连接使用。服务端在每次新的入站 SRQ 握手时读取一次开关。若 `immutable_flags=false`,可通过 `/flags` 在线修改。
* `rdma_srq_workers`:每个 bthread tag 的 worker 数量,默认 4。
* `rdma_srq_depth`:SRQ 中接收请求(WR)的目标数量,也用于配置 RX CQ 容量,默认 2048。
* `rdma_srq_pool_blocks`:接收内存池的块数上限,包含供网卡接收、空闲和上层持有的块,默认 16384。
Expand Down
20 changes: 10 additions & 10 deletions docs/en/rdma.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The dedicated RQ implementation precreates a batch of QPs and CQs for new connec

bRPC uses RDMA RC mode, with one QP per `RdmaEndpoint`. Connection setup first exchanges parameters such as GID and QPN over TCP, then establishes the RDMA connection. This handshake reuses bRPC's AppConnect mechanism. After RDMA is established, the TCP connection remains in the EST state but no longer carries application data. Closing the TCP connection also fails the corresponding RDMA connection.

Each new logical connection starts with handshake v3, with no version configuration required. If the server closes or resets the connection before returning v3 magic, or replies with v2 magic, the client reconnects and retries v2 once. TCP connection establishment, the handshake, and reconnection share the original connection deadline. Malformed replies identified as v3, handshake timeouts, and failed v2 retries fail the connection. A normal server request to fall back to TCP continues on the existing connection.
Each new logical connection starts with handshake v3. If the server closes or resets the first connection before returning v3 magic, or replies with v2 magic, the client reconnects with v2 and a dedicated RQ. Each logical connection uses at most two hello exchanges, both under the original connection deadline. A truncated v3 frame, a parse error, or a handshake timeout fails the connection.

`RqConnection` uses IOBuf to support zero-copy sends and receives. On send, `_sbuf` retains references to data blocks until completion is confirmed. On receive, `_rbuf` holds preallocated IOBuf blocks, which are submitted to the NIC through receive requests. Receive blocks have a fixed size, using 8 KiB blocks by default. A single send work request (WR) cannot exceed the usable capacity of the peer's receive block. Larger RPCs are split into fragments that fit this limit.

Expand All @@ -64,7 +64,7 @@ SRQ (Shared Receive Queue) lets multiple connections share a receive queue and p

Each bthread tag has four workers by default, created on demand, and each connection is assigned to one worker. A worker manages one SRQ and processes its queues in a bthread when events arrive. It does not occupy a dedicated persistent thread.

`RdmaEndpoint` selects `RqConnection` (dedicated RQ/CQ) or `SrqConnection` (shared SRQ/CQ) through the [RdmaConnection interface](../../src/brpc/rdma/rdma_connection.h). Both pass data to InputMessenger through `RdmaEndpoint::Deliver`. Enabling SRQ skips QP precreation for the dedicated RQ mode.
`RdmaEndpoint` selects `RqConnection` (dedicated RQ/CQ) or `SrqConnection` (shared SRQ/CQ) through the [RdmaConnection interface](../../src/brpc/rdma/rdma_connection.h). Both pass data to InputMessenger through `RdmaEndpoint::Deliver`.

## Use Cases

Expand All @@ -81,12 +81,12 @@ For sustained high concurrency on a single connection, or a few continuously bus
## Limitations

- **Contention between connections**: Connections on a worker share receive capacity, TX CQ credits, and CQ processing capacity. Concurrent bursts may wait for send credits or trigger RNR retries when the peer has no receive request available. TX CQ credit allocation does not currently guarantee strict fairness between connections. A fatal error in a shared CQ or SRQ affects every connection on that worker.
- **Event mode and v3 handshake only**: `rdma_use_srq` and `rdma_use_polling` cannot both be enabled. The NIC and driver must support RC, SRQ, and the configured queue capacities. bRPC queries device capabilities through verbs and does not restrict device names.
- **Handshake compatibility**: Upgrade servers before enabling v3 and SRQ on clients. An SRQ server still accepts dedicated RQ connections. If a peer supports v3 but has SRQ disabled, the connection falls back to TCP, not dedicated RQ. Fallback is not guaranteed with older servers that do not support v3.
- **Local requirements.** SRQ requires the v3 handshake, event mode, and sufficient NIC and driver resources. In polling mode the client requests a dedicated RQ in its first hello, even when it prefers SRQ.
- **Handshake fallback.** If the client cannot allocate an SRQ QP before its first hello, it tries a dedicated RQ on the original TCP connection. A v3 server sets `declined=true` only when refusing the handshake and zeroes the required fields for older clients. When the field is absent, the client recognizes a legacy refusal if the required fields are all zero or contain valid dedicated RQ parameters with `qp_num=0`. On a refusal, on a valid dedicated RQ reply, or when local SRQ activation fails, the client sends `ACK=0` to finish the exchange. It then allocates a dedicated QP and reconnects with v3. If allocation fails, the client uses TCP on the original connection. If the second exchange is declined, it uses TCP on the second connection. TCP-only servers send the same refusal, so clients that request SRQ may open one extra connection.

## Usage

With RDMA enabled, set `rdma_use_srq=true` on both peers, set `rdma_client_handshake_version=3` on the client, and keep `rdma_use_polling=false`. See [SRQ Flags and Parameters](#srq-flags-and-parameters).
With RDMA enabled, set `rdma_connect_srq=true` on the client to request SRQ first. Both peers use event mode (the default). The server accepts SRQ requests by default and creates or reuses a worker on demand. To decline new SRQ requests, set `rdma_accept_srq=false` on the server. See [SRQ Flags and Parameters](#srq-flags-and-parameters).

## Capacity Planning

Expand All @@ -107,12 +107,12 @@ When tuning these parameters:

Configurable parameters:
* `rdma_trace_verbose`: Log RDMA connection setup details. Default: `false`.
* rdma_client_handshake_version: deprecated and ignored. Retained for compatibility with existing command-line arguments. Clients always start with v3 and reconnect to retry v2 once if the peer rejects v3.
* `rdma_client_handshake_version` is deprecated and retained for command-line compatibility. Clients always start with v3.
* `rdma_recv_zerocopy`: Enable zero-copy receives. Default: `true`.
* `rdma_zerocopy_min_size`: Minimum fragment size for zero-copy receives. Default: 512 B.
* `rdma_recv_block_type`: Receive buffer block type. Choices are `default` (8 KiB), `large` (64 KiB), and `huge` (2 MiB). Default: `default`.
* `rdma_prepared_qp_size`: Queue size of precreated QPs. Default: 128.
* `rdma_prepared_qp_cnt`: Number of precreated QPs. Default: 1024.
* `rdma_prepared_qp_cnt` sets the number of dedicated QPs precreated **per process**. The default is 1024. In event mode each entry uses one QP, two CQs, and one completion-channel fd. At the default count, the pool therefore uses 1024 QPs, 2048 CQs, and 1024 fds. In polling mode each entry uses one QP and one CQ shared by send and receive, with zero completion-channel fds. To keep allocating dedicated QPs on demand when upgrading a deployment that previously skipped precreation by enabling SRQ, set this flag to 0.
* `rdma_max_sge`: Maximum number of SGEs per send request. Default: 0, meaning the device's supported limit.
* `rdma_sq_size`: SQ size. Default: 128.
* `rdma_rq_size`: RQ size. Default: 128.
Expand All @@ -128,15 +128,15 @@ Configurable parameters:
* `rdma_use_polling`: Enable RDMA polling mode. Default: `false`.
* `rdma_poller_num`: Number of pollers. Default: 1.
* `rdma_poller_yield`: Whether pollers voluntarily yield the CPU. Default: `false`.
* `rdma_client_handshake_version`: Client RDMA handshake version. Default: 2. Set to 3 for SRQ. The server detects the version from the received message.
* `event_dispatcher_edisp_unsched`: Control whether EventDispatcher is unschedulable. Default: `false`. Set to `true` to make it unschedulable.
* `rdma_disable_bthread`: Disable bthread. Default: `false`.

## SRQ Flags and Parameters

These settings are specific to SRQ and should be configured at process startup. Unless otherwise stated, capacities are **per worker**:
Configure the SRQ settings at process startup. You can also change `rdma_accept_srq` at runtime. Unless otherwise stated, capacities are **per worker**.

* `rdma_use_srq`: Enable SRQ. Default: `false`. Both peers must enable it and use the v3 handshake. Enabling this flag while keeping the default v2 handshake still uses dedicated RQ.
* `rdma_connect_srq` selects the receive queue requested on outgoing connections. The default is `false` (dedicated RQ). Set it to `true` to prefer SRQ.
* `rdma_accept_srq` controls SRQ admission for new incoming handshakes across the process. The default is `true`. Set it to `false` to silently reply with `declined=true` before allocating SRQ resources. The server continues to accept dedicated RQ requests over v2 and v3. Existing workers and receive pools remain available to established SRQ connections. The server reads this flag once per new incoming SRQ handshake and accepts runtime updates through `/flags` when `immutable_flags=false`.
* `rdma_srq_workers`: Number of workers per bthread tag. Default: 4.
* `rdma_srq_depth`: Target number of receive WRs posted to the SRQ. Also sets the requested RX CQ capacity. Default: 2048.
* `rdma_srq_pool_blocks`: Maximum number of receive pool blocks, including blocks used for NIC receives, free blocks, and blocks held by the application. Default: 16384.
Expand Down
Loading
Loading