You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "zchuango (via GitHub)" <gi...@apache.org> on 2026/08/07 06:18:53 UTC
[PR] feat: add URMA transport support (brpc)
zchuango opened a new pull request, #3428:
URL: https://github.com/apache/brpc/pull/3428
### What problem does this PR solve?
Issue Number: Related to #3401
Problem Summary:
brpc does not currently provide a remote-memory transport based on the UMDK
URMA API.
This PR adds URMA (Unified Remote Memory Access) as an optional transport in
the existing `Transport` framework. TCP is used for connection establishment
and URMA capability/resource negotiation. After both peers successfully
negotiate URMA support, the data path switches to URMA. When URMA is
unavailable or negotiation does not succeed, the connection falls back to TCP.
The initial implementation supports the `baidu_std` protocol only and is
disabled by default.
### What is changed and the side effects?
Changed:
- Add `SOCKET_MODE_URMA` and `UrmaTransport`, and integrate them with the
existing transport, socket, and message-processing framework.
- Add `UrmaEndpoint` for URMA resource management, SEND/RECV processing,
completion handling, and credit-based flow control.
- Add TCP-based URMA capability and resource negotiation, including v2 binary
and v3 protobuf handshake formats, with TCP fallback.
- Add a registered buffer pool for IOBuf blocks and APIs for explicit user
memory registration.
- Support completion-event mode and optional busy-polling mode.
- Support the UMDK bonding extension when the corresponding provider extension
is available.
- Add optional URMA build integration for CMake, Make, and Bazel, together with
a link-time URMA mock for build and test environments without `liburma` or
URMA hardware.
- Add URMA unit tests, English and Chinese documentation, and an
`urma_performance` example.
Side effects:
- Performance effects:
- URMA is optional and disabled by default. Existing socket modes and their
default behavior remain unchanged.
- Enabling URMA maps and registers a configurable IOBuf buffer pool. The
default configuration is 65,536 buffers of 8 KiB each (512 MiB in total).
- Busy-polling mode may increase CPU usage because completion pollers
actively poll the JFC.
- URMA performance depends on the provider, driver, hardware, and polling
configuration. This PR does not claim a specific hardware performance
improvement.
- Breaking backward compatibility:
- No intended breaking changes.
- `SOCKET_MODE_URMA` and the URMA memory-registration APIs are additive.
Existing socket modes and their default behavior remain unchanged.
---
### Check List:
- Please make sure your changes are compilable.
- When providing us with a new feature, it is best to add related tests.
- Please follow [Contributor Covenant Code of Conduct](https://github.com/apache/brpc/blob/master/CODE_OF_CONDUCT.md).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3958358536
##########
BUILD.bazel:
##########
@@ -547,12 +561,20 @@ cc_library(
"src/brpc/policy/thrift_protocol.cpp",
"src/brpc/event_dispatcher_epoll.cpp",
"src/brpc/event_dispatcher_kqueue.cpp",
+ "src/brpc/urma/*.cpp",
+ "src/brpc/urma/**/*.cpp",
]) + select({
"//bazel/config:brpc_with_thrift": glob([
"src/brpc/thrift*.cpp",
"src/brpc/**/thrift*.cpp",
]),
"//conditions:default": [],
+ }) + select({
+ "//bazel/config:brpc_with_urma_use_real": URMA_SRCS,
+ "//bazel/config:brpc_with_urma": URMA_SRCS + [
+ "src/brpc/urma/mock_urma.cpp",
+ ],
+ "//conditions:default": [],
}),
Review Comment:
URMA sources are included unconditionally via the base `glob([... 'src/brpc/urma/*.cpp', 'src/brpc/urma/**/*.cpp', ...])` and then included again via the URMA `select(...)`, which can cause duplicate-source errors and also defeats the intent to include `mock_urma.cpp` only in mock builds (and exclude it when linking the real `-lurma`). Remove URMA globs from the base `glob(...)` and rely solely on the `select(...)` (or exclude the URMA paths from the base glob) so URMA sources (and especially `mock_urma.cpp`) are only compiled in the intended configurations.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5240785830
The common handshake abstraction is being discussed in:#3432
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5293264010
@dwh110 Thanks for the review and suggestion. I have noted this concern and will evaluate the proposed changes. I will update the PR once the necessary modifications are implemented.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3958980567
##########
src/brpc/input_messenger_processor.cpp:
##########
@@ -15,279 +15,286 @@
// specific language governing permissions and limitations
// under the License.
-#include "butil/logging.h"
-#include "butil/binary_printer.h"
-#include "bthread/unstable.h"
-#include "brpc/options.pb.h"
-#include "brpc/transport.h"
#include "brpc/input_messenger_processor.h"
#include "brpc/input_messenger.h"
+#include "brpc/options.pb.h"
+#include "brpc/transport.h"
+#include "bthread/unstable.h"
+#include "butil/binary_printer.h"
+#include "butil/logging.h"
+
namespace brpc {
DECLARE_uint64(max_body_size);
-const size_t MSG_SIZE_WINDOW = 10; // Take last so many message into stat.
+const size_t MSG_SIZE_WINDOW = 10; // Take last so many message into stat.
const size_t MIN_ONCE_READ = 4096;
const size_t MAX_ONCE_READ = 524288;
-static const char* StreamTypeName(InputMessengerProcessor::StreamType type) {
- switch (type) {
- case InputMessengerProcessor::STREAM_NONE: return "none";
- case InputMessengerProcessor::STREAM_TCP_FD: return "tcp_fd";
- case InputMessengerProcessor::STREAM_RDMA_QP: return "rdma_qp";
- }
- return "unknown";
+static const char *StreamTypeName(InputMessengerProcessor::StreamType type) {
+ switch (type) {
+ case InputMessengerProcessor::STREAM_NONE:
+ return "none";
+ case InputMessengerProcessor::STREAM_TCP_FD:
+ return "tcp_fd";
+ case InputMessengerProcessor::STREAM_RDMA_QP:
+ return "rdma_qp";
+ case InputMessengerProcessor::STREAM_URMA_JETTY:
+ return "urma_jetty";
+ }
+ return "unknown";
}
-InputMessengerProcessor::ParsingStreamGuard::ParsingStreamGuard(Socket* socket, StreamType type)
+InputMessengerProcessor::ParsingStreamGuard::ParsingStreamGuard(Socket *socket,
Review Comment:
don't change these formats
##########
src/brpc/input_messenger.cpp:
##########
@@ -15,35 +15,33 @@
// specific language governing permissions and limitations
// under the License.
-
-#include <gflags/gflags.h>
-#include "butil/fd_guard.h" // fd_guard
-#include "butil/logging.h" // CHECK
-#include "butil/time.h" // cpuwide_time_us
-#include "butil/fd_utility.h" // make_non_blocking
-#include "bthread/bthread.h" // bthread_start_background
-#include "bthread/unstable.h" // bthread_flush
-#include "bvar/bvar.h" // bvar::Adder
-#include "brpc/options.pb.h" // ProtocolType
-#include "brpc/reloadable_flags.h" // BRPC_VALIDATE_GFLAG
-#include "brpc/protocol.h" // ListProtocols
-#include "brpc/rdma/rdma_endpoint.h"
#include "brpc/input_messenger.h"
+#include "brpc/options.pb.h" // ProtocolType
+#include "brpc/protocol.h" // ListProtocols
+#include "brpc/rdma/rdma_endpoint.h"
+#include "brpc/reloadable_flags.h" // BRPC_VALIDATE_GFLAG
#include "brpc/transport_factory.h"
+#include "bthread/bthread.h" // bthread_start_background
+#include "bthread/unstable.h" // bthread_flush
+#include "butil/fd_guard.h" // fd_guard
+#include "butil/fd_utility.h" // make_non_blocking
+#include "butil/logging.h" // CHECK
+#include "butil/time.h" // cpuwide_time_us
+#include "bvar/bvar.h" // bvar::Adder
+#include <gflags/gflags.h>
+
namespace brpc {
-InputMessenger* g_messenger = nullptr;
+InputMessenger *g_messenger = nullptr;
Review Comment:
There's no need to change these formats
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3946011940
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1846 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
+ if (nr == 0) {
+ errno = EEOF;
+ return -1;
+ }
+ received += nr;
+ }
+ return 0;
+}
+
+void UrmaEndpoint::PushBackToReadBuf(const void* data, size_t len) {
+ _socket->_read_buf.append(data, len);
+}
+
+int UrmaEndpoint::WriteToFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t written = 0;
+ while (written < len) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = write(fd, p + written, len - written);
+ if (nw >= 0) {
+ written += nw;
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+// ============================================================================
+// Hello builders / parsers.
+// ============================================================================
+
+void UrmaEndpoint::MakeLocalParsedHello(ParsedHello* out) const {
+ *out = ParsedHello{}; // value-initialize (avoids memset on non-trivial type)
+ out->buffer_size = static_cast<uint32_t>(GetUrmaRecvBlockSize());
+ out->recv_buffer_cnt = _rq_size - 1;
+ if (_resource && _resource->jetty) {
+ out->jetty_id = _resource->jetty->jetty_id.id;
+ out->uasid = _resource->jetty->jetty_id.uasid;
+ const urma_eid_t* local_eid = GetUrmaLocalEid();
+ const uint8_t* advertised_eid =
+ local_eid != nullptr
+ ? local_eid->raw
+ : _resource->jetty->jetty_id.eid.raw;
+ std::memcpy(out->eid, advertised_eid, 16);
+ }
+ out->tp_type = static_cast<uint8_t>(URMA_CTP);
+ // Pool segment: flatten g_pool_seg's seg fields.
+ urma_target_seg_t* pool = GetPoolSegFor(nullptr);
+ if (pool) {
+ std::memcpy(out->seg_eid, pool->seg.ubva.eid.raw, 16);
+ out->seg_uasid = pool->seg.ubva.uasid;
+ out->seg_va = pool->seg.ubva.va;
+ out->seg_len = pool->seg.len;
+ out->seg_token_id = pool->seg.token_id;
+ }
+}
+
+void UrmaEndpoint::FillLocalHelloV2(v2_wire::HelloMessage* out) const {
+ *out = v2_wire::HelloMessage{}; // value-initialize
+ out->msg_len = v2_wire::HELLO_PACKET_LEN;
+ out->hello_ver = v2_wire::HELLO_V2_VERSION;
+ out->impl_ver = v2_wire::IMPL_V2_VERSION;
+ ParsedHello p;
+ MakeLocalParsedHello(&p);
+ out->buffer_size = p.buffer_size;
+ out->recv_buffer_cnt = p.recv_buffer_cnt;
+ out->jetty_id = p.jetty_id;
+ std::memcpy(out->eid, p.eid, 16);
+ out->uasid = p.uasid;
+ out->tp_type = p.tp_type;
+ std::memcpy(out->seg_eid, p.seg_eid, 16);
+ out->seg_uasid = p.seg_uasid;
+ out->seg_va = p.seg_va;
+ out->seg_len = p.seg_len;
+ out->seg_token_id = p.seg_token_id;
+}
+
+void UrmaEndpoint::FillLocalHelloV3(UrmaHello* out) const {
+ ParsedHello p;
+ MakeLocalParsedHello(&p);
+ out->set_buffer_size(p.buffer_size);
+ out->set_recv_buffer_cnt(p.recv_buffer_cnt);
+ out->set_jetty_id(p.jetty_id);
+ out->set_eid(p.eid, 16);
+ out->set_uasid(p.uasid);
+ out->set_tp_type(p.tp_type);
+ out->set_seg_eid(p.seg_eid, 16);
+ out->set_seg_uasid(p.seg_uasid);
+ out->set_seg_va(p.seg_va);
+ out->set_seg_len(p.seg_len);
+ out->set_seg_token_id(p.seg_token_id);
+}
+
+int UrmaEndpoint::WriteHelloV3(const UrmaHello& msg) {
+ butil::IOBuf packet;
+ packet.append("URM3", 4);
+ std::string body;
+ if (!msg.SerializeToString(&body)) {
+ LOG(ERROR) << "Fail to serialize UrmaHello";
+ return -1;
+ }
+ uint32_t pb_size_be = butil::HostToNet32(static_cast<uint32_t>(body.size()));
+ packet.append(&pb_size_be, sizeof(pb_size_be));
+ packet.append(body);
+ return WriteToFd(packet);
+}
+
+int UrmaEndpoint::WriteToFd(butil::IOBuf& data) {
+ // Write out the IOBuf in a single WriteToFd-style loop.
+ while (!data.empty()) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = data.cut_into_file_descriptor(fd);
+ if (nw >= 0) {
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int UrmaEndpoint::ReadAndParseHelloV3(ParsedHello* out, bool* negotiated) {
+ *negotiated = false;
+ uint32_t pb_size_be = 0;
+ if (ReadFromFd(&pb_size_be, sizeof(pb_size_be)) < 0) {
+ return -1;
+ }
+ const uint32_t pb_size = butil::NetToHost32(pb_size_be);
+ if (pb_size == 0 || pb_size > 4096) {
+ return 0;
+ }
+ std::string body(pb_size, '\0');
+ if (ReadFromFd(&body[0], pb_size) < 0) {
+ return -1;
+ }
+ UrmaHello msg;
+ if (!msg.ParseFromArray(body.data(), static_cast<int>(body.size()))) {
+ return 0;
+ }
+ if (msg.eid().size() != 16 || msg.seg_eid().size() != 16) {
+ return 0;
+ }
+ out->buffer_size = msg.buffer_size();
+ out->recv_buffer_cnt = msg.recv_buffer_cnt();
+ out->jetty_id = msg.jetty_id();
+ std::memcpy(out->eid, msg.eid().data(), 16);
+ out->uasid = msg.uasid();
+ out->tp_type = static_cast<uint8_t>(msg.tp_type());
+ std::memcpy(out->seg_eid, msg.seg_eid().data(), 16);
+ out->seg_uasid = msg.seg_uasid();
+ out->seg_va = msg.seg_va();
+ out->seg_len = msg.seg_len();
+ out->seg_token_id = msg.seg_token_id();
+ if (!ValidHello(*out)) {
+ return 0;
+ }
+ *negotiated = true;
+ return 0;
+}
+
+// ============================================================================
+// Allocate / deallocate per-connection resources.
+// ============================================================================
+
+int UrmaEndpoint::AllocateResources() {
+ if (_resource) {
+ return 0;
+ }
+ urma_context_t* ctx = GetUrmaContext();
+ if (!ctx) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ _resource = new (std::nothrow) UrmaResource();
+ if (!_resource) {
+ return -1;
+ }
+
+ // Try the prepared pool first (sized sq/rq match).
+ if (_sq_size <= static_cast<uint16_t>(FLAGS_urma_sq_size) &&
+ _rq_size <= static_cast<uint16_t>(FLAGS_urma_rq_size)) {
+ BAIDU_SCOPED_LOCK(g_prepared_mutex);
+ if (g_prepared_list) {
+ UrmaResource* next = g_prepared_list->next;
+ delete _resource;
+ _resource = g_prepared_list;
+ g_prepared_list = next;
+ _resource->next = nullptr;
+ --g_prepared_cnt;
+ }
+ }
+
+ if (!_resource->jfc) {
+ // The SDK requires every JFC to reference a JFCE. Polling mode does
+ // not arm or consume it, but still supplies the required object.
+ _resource->jfce = urma_create_jfce(ctx);
+ if (!_resource->jfce ||
+ (!FLAGS_urma_use_polling && _resource->jfce->fd < 0)) {
+ LOG(ERROR) << "Fail to create a usable URMA JFCE";
+ errno = ENODEV;
+ return -1;
+ }
+
+ urma_jfc_cfg_t jfc_cfg{};
+ jfc_cfg.depth = static_cast<uint32_t>(_sq_size + _rq_size);
+ jfc_cfg.jfce = _resource->jfce;
+ _resource->jfc = urma_create_jfc(ctx, &jfc_cfg);
+ if (!_resource->jfc) {
+ PLOG(ERROR) << "urma_create_jfc";
+ return -1;
+ }
+
+ urma_jfr_cfg_t jfr_cfg{};
+ jfr_cfg.depth = static_cast<uint32_t>(_rq_size);
+ jfr_cfg.trans_mode = URMA_TM_RM;
+ jfr_cfg.max_sge = 1;
+ jfr_cfg.min_rnr_timer = URMA_TYPICAL_MIN_RNR_TIMER;
+ jfr_cfg.jfc = _resource->jfc;
+ _resource->jfr = urma_create_jfr(ctx, &jfr_cfg);
+ if (!_resource->jfr) {
+ PLOG(ERROR) << "urma_create_jfr";
+ return -1;
+ }
+
+ urma_jetty_cfg_t jetty_cfg{};
+ jetty_cfg.flag.bs.share_jfr = 1;
+ jetty_cfg.jfs_cfg.depth = static_cast<uint32_t>(_sq_size);
+ jetty_cfg.jfs_cfg.trans_mode = URMA_TM_RM;
+ jetty_cfg.jfs_cfg.priority = GetUrmaJettyPriority();
+ jetty_cfg.jfs_cfg.max_sge =
+ static_cast<uint8_t>(GetUrmaMaxSge());
+ jetty_cfg.jfs_cfg.rnr_retry = URMA_TYPICAL_RNR_RETRY;
+ jetty_cfg.jfs_cfg.err_timeout = URMA_TYPICAL_ERR_TIMEOUT;
+ jetty_cfg.jfs_cfg.jfc = _resource->jfc;
+ jetty_cfg.shared.jfr = _resource->jfr;
+ jetty_cfg.shared.jfc = _resource->jfc;
+ _resource->jetty = urma_create_jetty(ctx, &jetty_cfg);
+ if (!_resource->jetty) {
+ PLOG(ERROR) << "urma_create_jetty";
+ return -1;
+ }
+ }
+
+ _sbuf.resize(_sq_size - RESERVED_WR_NUM);
+ _rbuf.resize(_rq_size);
+ _rbuf_data.resize(_rq_size, nullptr);
+
+ // Wrap the JFCE fd in a brpc Socket so PollCq is driven by epoll.
+ if (!FLAGS_urma_use_polling) {
+ if (!_resource->jfce || _resource->jfce->fd < 0) {
+ LOG(ERROR) << "Prepared URMA resource has no usable JFCE";
+ errno = ENODEV;
+ return -1;
+ }
+ if (ReqNotifyCq() != 0) {
+ return -1;
+ }
+ SocketOptions options;
+ options.user = this;
+ options.keytable_pool = _socket->keytable_pool();
+ options.fd = _resource->jfce->fd;
+ options.on_edge_triggered_events = PollCq;
+ if (Socket::Create(options, &_cq_sid) < 0) {
+ PLOG(ERROR) << "Fail to create CQ socket";
+ return -1;
+ }
+ } else {
+ // Polling mode: synthetic carrier socket (no fd).
+ SocketOptions options;
+ options.user = this;
+ options.keytable_pool = _socket->keytable_pool();
+ options.on_edge_triggered_events = PollCq;
+ if (Socket::Create(options, &_cq_sid) < 0) {
+ PLOG(ERROR) << "Fail to create CQ socket (polling)";
+ return -1;
+ }
+ PollerAddCqSid();
+ }
+ return 0;
+}
+
+void UrmaEndpoint::DeallocateResources() {
+ if (!_resource) {
+ return;
+ }
+
+ if (FLAGS_urma_use_polling) {
+ PollerRemoveCqSid();
+ }
+
+ // Tear down the CQ socket so the EventDispatcher stops calling PollCq.
+ if (_cq_sid != INVALID_SOCKET_ID) {
+ SocketUniquePtr s;
+ if (Socket::Address(_cq_sid, &s) == 0) {
+ if (s->fd() >= 0) {
+ s->_io_event.RemoveConsumer(s->_fd);
+ }
+ s->_user = nullptr; // Do not release user (this UrmaEndpoint).
+ s->_fd = -1; // Already removed fd from epoll.
+ s->SetFailed();
+ }
+ _cq_sid = INVALID_SOCKET_ID;
+ }
+
+ // Reusing a Jetty requires a driver-supported RESET plus a complete JFC
+ // drain. Until that lifecycle is implemented, prepared resources are
+ // one-shot: they accelerate connection setup but are destroyed on close.
+ delete _resource;
+ _resource = nullptr;
+}
+
+// ============================================================================
+// ImportPeer: the critical import_seg-before-import_jetty sequence.
+// ============================================================================
+
+int UrmaEndpoint::ImportPeer(const ParsedHello& peer) {
+ urma_context_t* ctx = GetUrmaContext();
+ if (!ctx) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ // 1. urma_import_seg FIRST so the kernel establishes TP routing for the
+ // remote EID. Without this the first SEND is rejected by hardware with
+ // URMA_CR_RNR_RETRY_CNT_EXC_ERR.
+ urma_seg_t peer_seg{};
+ std::memcpy(peer_seg.ubva.eid.raw, peer.seg_eid, 16);
+ peer_seg.ubva.uasid = peer.seg_uasid;
+ peer_seg.ubva.va = peer.seg_va;
+ peer_seg.len = peer.seg_len;
+ peer_seg.token_id = peer.seg_token_id;
+ urma_token_t seg_token{};
+ urma_import_seg_flag_t seg_flag{};
+ seg_flag.bs.cacheable = URMA_NON_CACHEABLE;
+ seg_flag.bs.access = URMA_ACCESS_READ | URMA_ACCESS_WRITE | URMA_ACCESS_ATOMIC;
+ seg_flag.bs.mapping = URMA_SEG_NOMAP;
+ _resource->remote_seg = urma_import_seg(ctx, &peer_seg, &seg_token, 0, seg_flag);
+ if (!_resource->remote_seg) {
+ PLOG(ERROR) << "urma_import_seg failed";
+ return -1;
+ }
+
+ // 2. urma_import_jetty.
+ urma_rjetty_t remote{};
+ std::memcpy(remote.jetty_id.eid.raw, peer.eid, 16);
+ remote.jetty_id.uasid = peer.uasid;
+ remote.jetty_id.id = peer.jetty_id;
+ remote.trans_mode = URMA_TM_RM;
+ remote.type = URMA_JETTY;
+ if (peer.tp_type > static_cast<uint8_t>(URMA_UTP)) {
+ errno = EPROTO;
+ return -1;
+ }
+ remote.tp_type = static_cast<urma_tp_type_t>(peer.tp_type);
+
+ urma_token_t token{};
+ const bool use_bonding_extension =
+ IsUrmaBondingDevice() && remote.trans_mode == URMA_TM_RM;
+ errno = 0;
+ if (use_bonding_extension) {
+#if BRPC_URMA_HAS_BONDING_EXT
+ // The bonding provider needs the local jetty to associate its send
+ // path with the imported target. A plain import may return success
+ // without setting that association, leaving traffic one-way only.
+ bondp_rjetty_t bonding_remote{};
+ bonding_remote.base = remote;
+ bonding_remote.base.flag.bs.has_drv_ext = 1;
+ bonding_remote.jetty = _resource->jetty;
+ _resource->remote_jetty =
+ urma_import_jetty(ctx, &bonding_remote.base, &token);
+#else
+ LOG(ERROR) << "Bonding remote jetty import requires provider header "
+ "urma_ubagg.h";
+ errno = ENOTSUP;
+#endif
+ } else {
+ _resource->remote_jetty = urma_import_jetty(ctx, &remote, &token);
+ }
+ if (!_resource->remote_jetty) {
+ if (errno == 0) {
+ errno = EIO;
+ }
+ char remote_eid[URMA_EID_STR_LEN + 1] = {};
+ std::snprintf(remote_eid, sizeof(remote_eid), EID_FMT,
+ EID_RAW_ARGS(peer.eid));
+ PLOG(ERROR) << "urma_import_jetty failed"
+ << " remote_eid=" << remote_eid
+ << " remote_uasid=" << peer.uasid
+ << " remote_jetty_id=" << peer.jetty_id
+ << " trans_mode=" << remote.trans_mode
+ << " tp_type=" << remote.tp_type
+ << " bonding_extension=" << use_bonding_extension;
+ return -1;
+ }
+ return 0;
+}
+
+// ============================================================================
+// Send / recv data path.
+// ============================================================================
+
+// Private IOBuf accessor mirroring RdmaIOBuf: reach into IOBuf block refs to
+// build a urma_sge_t directly, without memcpy.
+class UrmaIOBuf : private butil::IOBuf {
+ friend class ::brpc::urma::UrmaEndpoint;
+public:
+ using butil::IOBuf::_ref_num;
+ using butil::IOBuf::_ref_at;
+ using butil::IOBuf::fetch1;
+ using butil::IOBuf::get_first_data_meta;
+ using butil::IOBuf::cutn;
+ // Build the SGE for the current head block.
+ // Returns bytes added, or -1 (errno set).
+ ssize_t cut_into_sglist(urma_sge_t* sglist, size_t* sge_index,
+ butil::IOBuf* to, size_t max_sge,
+ size_t max_len) {
+ size_t len = 0;
+ while (*sge_index < max_sge && len < max_len && _ref_num() != 0) {
+ butil::IOBuf::BlockRef const& r = _ref_at(0);
+ const void* start = fetch1();
+ urma_target_seg_t* tseg =
+ GetPoolSegFor(const_cast<void*>(start));
+ if (!tseg) {
+ // User-registered memory: look up the seg handle.
+ uint64_t meta = get_first_data_meta();
+ if (meta != 0) {
+ tseg = reinterpret_cast<urma_target_seg_t*>(
+ static_cast<uintptr_t>(meta));
+ }
+ }
+ if (!tseg) {
+ errno = ERDMAMEM;
+ return -1;
+ }
+ size_t this_len = r.length;
+ if (len + this_len > max_len) {
+ this_len = max_len - len;
+ }
+ sglist[*sge_index].addr = reinterpret_cast<uint64_t>(start);
+ sglist[*sge_index].len = static_cast<uint32_t>(this_len);
+ sglist[*sge_index].tseg = tseg;
+ cutn(to, this_len);
+ len += this_len;
+ (*sge_index)++;
+ }
+ return static_cast<ssize_t>(len);
+ }
+};
+
+ssize_t UrmaEndpoint::CutFromIOBufList(butil::IOBuf** from, size_t ndata) {
+ if (!_resource || !_resource->jetty || !_resource->remote_jetty) {
+ errno = ENOTCONN;
+ return -1;
+ }
+ int max_sge = GetUrmaMaxSge();
+ if (max_sge < 1) {
+ max_sge = 1;
+ }
+
+ urma_sge_t* sglist = static_cast<urma_sge_t*>(
+ alloca(sizeof(urma_sge_t) * max_sge));
+ if (!sglist) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ size_t current = 0;
+ ssize_t total_len = 0;
+ while (current < ndata) {
+ uint16_t remote_wnd = _remote_rq_window_size.load(butil::memory_order_relaxed);
+ uint16_t sq_wnd = _sq_window_size.load(butil::memory_order_relaxed);
+ if (remote_wnd == 0 || sq_wnd == 0) {
+ if (total_len > 0) {
+ break;
+ }
+ errno = EAGAIN;
+ return -1;
+ }
+ butil::IOBuf* to = &_sbuf[_sq_current];
+ size_t sge_index = 0;
+ size_t this_len = 0;
+ size_t max_len = _remote_recv_block_size > 0
+ ? _remote_recv_block_size
+ : GetUrmaRecvBlockSize();
+ while (sge_index < static_cast<size_t>(max_sge) &&
+ this_len < max_len && current < ndata) {
+ auto* data = reinterpret_cast<UrmaIOBuf*>(from[current]);
+ if (data->empty()) {
+ ++current;
+ continue;
+ }
+ ssize_t n = data->cut_into_sglist(sglist, &sge_index, to,
+ max_sge, max_len - this_len);
+ if (n < 0) {
+ return -1;
+ }
+ this_len += n;
+ }
+ if (sge_index == 0) {
+ break;
+ }
+
+ urma_sg_t sg{sglist, static_cast<uint32_t>(sge_index)};
+ urma_jfs_wr_t wr{};
+ std::memset(&wr, 0, sizeof(wr));
+ // Send payload with URMA_OPC_SEND. Receive credits are flushed
+ // separately by SendImm() after SendAck() reaches its threshold.
+ // Piggybacking credits turns every payload into SEND_IMM and can
+ // produce asymmetric completions with the bonding provider.
+ wr.opcode = URMA_OPC_SEND;
+ wr.flag.bs.complete_enable = 1;
+ wr.tjetty = _resource->remote_jetty;
+ wr.send.src = sg;
+ wr.user_ctx = 1;
+ urma_jfs_wr_t* bad_wr = nullptr;
+ const uint16_t sq_slot = _sq_current;
+ const uint32_t local_jetty_id = _resource->jetty->jetty_id.id;
+ const uint32_t remote_jetty_id = _resource->remote_jetty->id.id;
+
+ // Reserve both credits before making the WR visible to the provider.
+ // In polling mode a completion (and even the peer's receive-credit
+ // ACK) can be processed by another thread before post_send returns.
+ // Decrementing after post therefore creates a transient capacity + 1
+ // window and makes the strict credit check tear down a healthy
+ // connection.
+ _remote_rq_window_size.fetch_sub(1, butil::memory_order_relaxed);
+ _sq_window_size.fetch_sub(1, butil::memory_order_relaxed);
+ int rc = urma_post_jetty_send_wr(_resource->jetty, &wr, &bad_wr);
+ if (rc != URMA_SUCCESS) {
+ const int provider_errno = errno;
+ _remote_rq_window_size.fetch_add(1, butil::memory_order_relaxed);
+ _sq_window_size.fetch_add(1, butil::memory_order_relaxed);
+ LOG(WARNING) << "urma_post_jetty_send_wr failed: " << rc
+ << ", provider_errno=" << provider_errno
+ << " (" << berror(provider_errno) << ')'
+ << ", bad_wr=" << static_cast<const void*>(bad_wr)
+ << ", bad_is_current=" << (bad_wr == &wr)
+ << ", sq_slot=" << sq_slot
+ << ", local_jetty_id=" << local_jetty_id
+ << ", remote_jetty_id=" << remote_jetty_id
+ << ", state=" << GetStateStr()
+ << ", sq_window=" << sq_wnd
+ << ", remote_rq_window=" << remote_wnd
+ << ", num_sge=" << sge_index
+ << ", configured_max_sge=" << GetUrmaMaxSge()
+ << ", payload_size=" << this_len
+ << " on " << _socket->description();
+ errno = rc;
+ return -1;
+ }
+ _sq_current = (_sq_current + 1) % (_sq_size - RESERVED_WR_NUM);
+ total_len += static_cast<ssize_t>(this_len);
+ }
+ return total_len;
+}
+
+bool UrmaEndpoint::IsWritable() const {
+ return _remote_rq_window_size.load(butil::memory_order_relaxed) > 0 &&
+ _sq_window_size.load(butil::memory_order_relaxed) > 0;
+}
+
+// ============================================================================
+// Recv path.
+// ============================================================================
+
+int UrmaEndpoint::DoPostRecv(void* block, size_t block_size) {
+ urma_target_seg_t* tseg = GetPoolSegFor(block);
+ if (!tseg) {
+ errno = ERDMAMEM;
+ return -1;
+ }
+ urma_sge_t sge{reinterpret_cast<uint64_t>(block),
+ static_cast<uint32_t>(block_size), tseg, nullptr};
+ urma_sg_t sg{&sge, 1};
+ urma_jfr_wr_t wr{sg, 0, nullptr};
+ urma_jfr_wr_t* bad = nullptr;
+ // Use the shared-JFR path on every device, including bonding. The bonding
+ // provider owns physical receive scheduling for the JFR; a local
+ // jetty-to-target association is not part of the RM receive API.
+ const urma_status_t status =
+ urma_post_jfr_wr(_resource->jfr, &wr, &bad);
+ if (status != URMA_SUCCESS) {
+ LOG(WARNING) << "Failed to post URMA receive WR: status=" << status
+ << " bonding=" << IsUrmaBondingDevice()
+ << " bad_wr=" << static_cast<const void*>(bad)
+ << " bad_is_current=" << (bad == &wr)
+ << " local_jetty_id=" << _resource->jetty->jetty_id.id
+ << " provider_associated_remote="
+ << static_cast<const void*>(
+ _resource->jetty->remote_jetty)
+ << " state=" << GetStateStr()
+ << " on " << _socket->description();
+ errno = status;
+ return -1;
+ }
+ return 0;
+}
+
+int UrmaEndpoint::PostRecv(uint32_t num, bool zerocopy) {
+ for (uint32_t i = 0; i < num; ++i) {
+ size_t block_size = GetUrmaRecvBlockSize();
+ if (zerocopy) {
+ _rbuf[_rq_received].clear();
+ butil::IOBufAsZeroCopyOutputStream zcis(
+ &_rbuf[_rq_received], block_size + IOBUF_BLOCK_HEADER_LEN);
+ void* data = nullptr;
+ int size = 0;
+ if (!zcis.Next(&data, &size) || !data ||
+ size < static_cast<int>(block_size)) {
+ errno = ENOMEM;
+ return -1;
+ }
+ _rbuf_data[_rq_received] = data;
+ if (DoPostRecv(data, block_size) < 0) {
+ return -1;
+ }
+ } else {
+ if (_rbuf_data[_rq_received] == nullptr) {
+ _rbuf[_rq_received].clear();
+ butil::IOBufAsZeroCopyOutputStream zcos(
+ &_rbuf[_rq_received],
+ block_size + IOBUF_BLOCK_HEADER_LEN);
+ void* data = nullptr;
+ int size = 0;
+ if (!zcos.Next(&data, &size) || !data ||
+ size < static_cast<int>(block_size)) {
+ errno = ENOMEM;
+ return -1;
+ }
+ _rbuf_data[_rq_received] = data;
+ }
+ if (DoPostRecv(_rbuf_data[_rq_received], block_size) < 0) {
+ return -1;
+ }
+ }
+ _rq_received = (_rq_received + 1) % _rq_size;
+ }
+ return 0;
+}
+
+int UrmaEndpoint::SendImm(uint32_t imm) {
+ if (imm == 0) {
+ return 0;
+ }
+ if (!_resource || !_resource->jetty || !_resource->remote_jetty) {
+ errno = ENOTCONN;
+ return -1;
+ }
+ if (_sq_imm_window_size == 0) {
+ errno = EAGAIN;
+ return -1;
+ }
+ // Empty-payload SEND_IMM flushes peer-side receive credit. Connection
+ // lifetime is owned by the TCP fd, so this is not an EOF marker.
+ urma_jfs_wr_t wr{};
+ std::memset(&wr, 0, sizeof(wr));
+ wr.opcode = URMA_OPC_SEND_IMM;
+ wr.flag.bs.complete_enable = 1;
+ wr.flag.bs.solicited_enable = 1;
+ wr.tjetty = _resource->remote_jetty;
+ wr.send.imm_data = imm;
+ wr.user_ctx = 0; // 0 == pure ack (HandleCompletion reuses budget).
+ urma_jfs_wr_t* bad = nullptr;
+ // Reserve the ACK-only SQ slot before posting for the same reason as the
+ // data windows in CutFromIOBufList: polling may observe its completion as
+ // soon as the provider accepts the WR.
+ --_sq_imm_window_size;
+ const urma_status_t status =
+ urma_post_jetty_send_wr(_resource->jetty, &wr, &bad);
+ if (status != URMA_SUCCESS) {
+ const int provider_errno = errno;
+ ++_sq_imm_window_size;
+ _new_rq_wrs.fetch_add(imm, butil::memory_order_relaxed);
+ LOG(WARNING) << "Failed to post URMA credit ACK: status=" << status
+ << " provider_errno=" << provider_errno
+ << " (" << berror(provider_errno) << ')'
+ << " bad_wr=" << static_cast<const void*>(bad)
+ << " bad_is_current=" << (bad == &wr)
+ << " imm=" << imm
+ << " local_jetty_id="
+ << _resource->jetty->jetty_id.id
+ << " remote_jetty_id="
+ << _resource->remote_jetty->id.id
+ << " state=" << GetStateStr()
+ << " on " << _socket->description();
+ errno = status;
+ return -1;
+ }
+ return 0;
+}
+
+int UrmaEndpoint::SendAck(int num) {
+ const uint16_t old =
+ _new_rq_wrs.fetch_add(num, butil::memory_order_relaxed);
+ if (old + num > _remote_window_capacity / 2 &&
+ _sq_imm_window_size > 0) {
+ return SendImm(_new_rq_wrs.exchange(0, butil::memory_order_relaxed));
+ }
+ return 0;
+}
+
+ssize_t UrmaEndpoint::HandleCompletion(const urma_cr_t& cr) {
+ bool zerocopy = FLAGS_urma_recv_zerocopy;
+ if (cr.status != URMA_CR_SUCCESS) {
+ LOG(WARNING) << "URMA completion failed, status=" << cr.status;
+ errno = EIO;
+ return -1;
+ }
+ if (cr.flag.bs.s_r == 0) {
+ // Send completion: reclaim SQ window and wake the writer.
+ if (cr.user_ctx == 0) {
+ // Pure-ack WR: just replenish the imm budget.
+ if (_sq_imm_window_size >= RESERVED_WR_NUM) {
+ LOG(WARNING)
+ << "URMA credit-ACK completion exceeds reserved SQ "
+ "window: current="
+ << _sq_imm_window_size
+ << " capacity=" << RESERVED_WR_NUM
+ << " on " << _socket->description();
+ errno = EPROTO;
+ return -1;
+ }
+ _sq_imm_window_size += 1;
+ SendAck(0);
+ return 0;
+ }
+ uint16_t wnd = 1; // We signal every WR (complete_enable=1).
+ uint16_t old =
+ _sq_window_size.load(butil::memory_order_relaxed);
+ while (true) {
+ if (old >= _local_window_capacity) {
+ LOG(WARNING)
+ << "URMA send completion exceeds SQ window: old=" << old
+ << " increment=" << wnd
+ << " capacity=" << _local_window_capacity
+ << " user_ctx=" << cr.user_ctx
+ << " on " << _socket->description();
+ errno = EPROTO;
+ return -1;
+ }
+ if (_sq_window_size.compare_exchange_weak(
+ old, static_cast<uint16_t>(old + wnd),
+ butil::memory_order_relaxed)) {
+ break;
+ }
+ }
+ for (uint16_t i = 0; i < wnd; ++i) {
+ _sbuf[_sq_sent].clear();
+ _sq_sent = (_sq_sent + 1) % (_sq_size - RESERVED_WR_NUM);
+ }
+ butil::subtle::MemoryBarrier();
+ if (_remote_rq_window_size.load(butil::memory_order_relaxed) >=
+ _local_window_capacity / 8) {
+ _socket->WakeAsEpollOut();
+ }
+ return 0;
+ }
+ // Recv completion.
+ if (cr.opcode == URMA_CR_OPC_SEND_WITH_IMM && cr.imm_data > 0) {
+ if (cr.imm_data > _local_window_capacity) {
+ LOG(WARNING) << "Invalid URMA receive credit: " << cr.imm_data;
+ errno = EPROTO;
+ return -1;
+ }
+ const uint16_t acks = static_cast<uint16_t>(cr.imm_data);
+ uint16_t old =
+ _remote_rq_window_size.load(butil::memory_order_relaxed);
+ while (true) {
+ if (old > _local_window_capacity - acks) {
+ LOG(WARNING)
+ << "URMA receive credit exceeds window: old=" << old
+ << " credit=" << acks
+ << " capacity=" << _local_window_capacity
+ << " imm=" << cr.imm_data
+ << " remote_window_capacity="
+ << _remote_window_capacity
+ << " on " << _socket->description();
+ errno = EPROTO;
+ return -1;
+ }
+ if (_remote_rq_window_size.compare_exchange_weak(
+ old, static_cast<uint16_t>(old + acks),
+ butil::memory_order_relaxed)) {
+ break;
+ }
+ }
+ if (_sq_window_size.load(butil::memory_order_relaxed) > 0) {
+ _socket->WakeAsEpollOut();
+ }
+ } else if (cr.completion_len == 0) {
+ LOG(WARNING) << "Zero-length URMA receive without immediate credit";
+ errno = EPROTO;
+ return -1;
+ }
+ if (cr.completion_len > GetUrmaRecvBlockSize()) {
+ LOG(WARNING) << "URMA completion exceeds receive buffer: "
+ << cr.completion_len;
+ errno = EPROTO;
+ return -1;
+ }
+ if (cr.completion_len < static_cast<uint32_t>(FLAGS_urma_zerocopy_min_size)) {
+ zerocopy = false;
+ }
+ if (zerocopy) {
+ _rbuf[_rq_received].cutn(&_socket->_read_buf, cr.completion_len);
+ } else {
+ _socket->_read_buf.append(_rbuf_data[_rq_received], cr.completion_len);
+ }
+ if (PostRecv(1, zerocopy) < 0) {
+ return -1;
+ }
+ if (cr.completion_len > 0) {
+ SendAck(1);
+ }
+ return static_cast<ssize_t>(cr.completion_len);
+}
+
+void UrmaEndpoint::DispatchReceivedBytes(SocketUniquePtr& s, ssize_t bytes) {
+ int64_t pending = _pending_received_bytes.load(butil::memory_order_relaxed);
+ if (bytes > 0) {
+ pending = _pending_received_bytes.fetch_add(
+ bytes, butil::memory_order_acq_rel) + bytes;
+ }
+
+ const State state = _state.load(butil::memory_order_acquire);
+ if (state != ESTABLISHED) {
+ return;
+ }
+
+ // PollCq and the handshake bthread can both reach this method when the
+ // state changes to ESTABLISHED. Serialize them so each byte added to
+ // _socket->_read_buf is reported to InputMessenger exactly once.
+ std::unique_lock<butil::Mutex> dispatch_lock(_dispatch_mutex);
+ if (_state.load(butil::memory_order_acquire) != ESTABLISHED) {
+ return;
+ }
+ pending = _pending_received_bytes.exchange(
+ 0, butil::memory_order_acq_rel);
+ if (pending <= 0 || s->Failed()) {
+ return;
+ }
+
+ auto* messenger = static_cast<InputMessenger*>(s->user());
+ if (!messenger) {
+ LOG(ERROR) << "URMA socket has no InputMessenger: "
+ << s->description();
+ return;
+ }
+
+ const int64_t received_us = butil::cpuwide_time_us();
+ const int64_t base_realtime = butil::gettimeofday_us() - received_us;
+ InputMessageClosure last_msg;
+ messenger->ProcessNewMessage(s.get(), static_cast<ssize_t>(pending),
+ false, received_us, base_realtime, last_msg);
+}
+
+void UrmaEndpoint::PollCq(Socket* m) {
+ auto* ep = static_cast<UrmaEndpoint*>(m->user());
+ if (!ep || !ep->_resource || !ep->_resource->jfc) {
+ return;
+ }
+ SocketUniquePtr s;
+ if (Socket::Address(ep->_socket->id(), &s) != 0) {
+ return;
+ }
+ if (s->Failed()) {
+ return;
+ }
+
+ const bool event_mode = !FLAGS_urma_use_polling;
+ int progress = Socket::PROGRESS_INIT;
+ while (true) {
+ urma_jfc_t* event_jfc = nullptr;
+ if (event_mode) {
+ const int event_count = ep->WaitCqEvent(s, &event_jfc);
+ if (event_count < 0) {
+ return;
+ }
+ if (event_count == 0) {
+ if (!m->MoreReadEvents(&progress)) {
+ return;
+ }
+ continue;
+ }
+ }
+
+ ssize_t bytes = 0;
+ auto drain_cq = [&]() -> int {
+ while (true) {
+ const int n =
+ std::max(1, std::min<int>(FLAGS_urma_cqe_poll_once, 32));
+ urma_cr_t crs[32];
+ const int cnt =
+ urma_poll_jfc(ep->_resource->jfc, n, crs);
+ if (cnt < 0) {
+ return EIO;
+ }
+ if (cnt == 0) {
+ return 0;
+ }
+ for (int i = 0; i < cnt; ++i) {
+ if (s->Failed()) {
+ return ECANCELED;
+ }
+ const ssize_t nr = ep->HandleCompletion(crs[i]);
+ if (nr < 0) {
+ return errno ? errno : EIO;
+ }
+ bytes += nr;
+ }
+ }
+ };
+
+ int completion_error = drain_cq();
Review Comment:
This is mainly an optimization for `event_mode`. Once we get an event notification, we drain all currently available events in one go, which helps reduce event handling latency.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3959426189
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+ _input_processor.Init(s, InputMessengerProcessor::STREAM_URMA_JETTY);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _input_processor.Reset();
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
Review Comment:
`ReadFromFd` treats `EINTR` as a hard failure. `read(2)` can return `-1` with `errno=EINTR`, in which case the correct behavior is to retry the syscall. Handle `EINTR` by continuing the loop (similar to how `EAGAIN/EWOULDBLOCK` are retried).
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+ _input_processor.Init(s, InputMessengerProcessor::STREAM_URMA_JETTY);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _input_processor.Reset();
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
+ if (nr == 0) {
+ errno = EEOF;
+ return -1;
+ }
+ received += nr;
+ }
+ return 0;
+}
+
+void UrmaEndpoint::PushBackToReadBuf(const void* data, size_t len) {
+ _socket->fd_input_processor().read_buf().append(data, len);
+}
+
+int UrmaEndpoint::WriteToFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t written = 0;
+ while (written < len) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = write(fd, p + written, len - written);
+ if (nw >= 0) {
+ written += nw;
+ continue;
+ }
Review Comment:
`WriteToFd(void*, size_t)` treats `EINTR` as a hard error. `write(2)` can return `-1` with `errno=EINTR`; the loop should retry in that case. Add an `errno == EINTR` branch that continues without failing.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "dwh110 (via GitHub)" <gi...@apache.org>.
dwh110 commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3782745389
##########
src/brpc/urma/mock_urma.cpp:
##########
@@ -0,0 +1,713 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Link-time mock of the URMA user-level C API, modeled on Mooncake's
+// mock_urma.cpp. It compiles against upstream UMDK headers and is linked into
+// brpc when liburma.so is unavailable. It lets CI run UrmaTransport unit tests
+// without URMA hardware.
+//
+// Design (same as Mooncake):
+// - Link-time substitution: the symbols are literally named urma_create_jfc
+// etc.; the linker picks this TU when liburma is absent.
+// - Per-object state is held in anonymous-namespace maps keyed on the opaque
+// pointer returned to the caller. Membership is checked on delete so misuse
+// returns URMA_EINVAL rather than crashes.
+// - Completion path: posted recv WRs remain pending until a SEND targets the
+// corresponding mock jetty. Payload and immediate data are copied into the
+// remote recv WR and both local-send and remote-recv completions are queued.
+// - Device-name contract: device->name == "mock_urma_device" so tests can
+// match it with --urma_device=mock_urma_device.
+
+#if BRPC_WITH_URMA
+
+#include "urma_api.h"
+
+#include <algorithm>
+#include <atomic>
+#include <cerrno>
+#include <cstring>
+#include <deque>
+#include <map>
+#include <mutex>
+#include <shared_mutex>
+#include <sys/eventfd.h>
+#include <unistd.h>
+#include <vector>
+
+namespace {
+
+struct JfcState {
+ std::mutex mutex;
+ std::deque<urma_cr_t> completions;
+ bool event_pending{false};
+};
+
+struct PendingRecv {
+ uint64_t addr;
+ uint32_t len;
+ uint64_t user_ctx;
+};
+
+std::shared_mutex g_rw_mutex;
+bool initialized = false;
+std::vector<urma_device_t *> device_list;
+std::map<urma_context_t *, int> context_map;
+std::map<urma_jfce_t *, int> jfce_map;
+std::map<urma_jfc_t *, JfcState *> jfc_state_map;
+std::map<urma_jfr_t *, int> jfr_map;
+// Side-table: JFR -> the JFC it was created with (used to route recv
+// completions to the right JfcState, since the JFR is an opaque handle).
+std::map<urma_jfr_t *, urma_jfc_t *> jfr_jfc_map;
+std::map<urma_jfr_t *, std::deque<PendingRecv>> jfr_recv_map;
+std::map<urma_target_seg_t *, int> seg_map;
+std::map<urma_jetty_t *, int> jetty_map;
+std::map<uint32_t, urma_jetty_t *> jetty_id_map;
+std::map<urma_target_jetty_t *, int> target_jetty_map;
+std::atomic<uint32_t> next_jetty_id{1};
+
+void PushCompletion(urma_jfc_t* jfc, JfcState* state,
+ const urma_cr_t& completion) {
+ bool signal = false;
+ {
+ std::lock_guard<std::mutex> lock(state->mutex);
+ state->completions.push_back(completion);
+ if (!state->event_pending) {
+ state->event_pending = true;
+ signal = true;
+ }
+ }
+ if (signal && jfc && jfc->jfc_cfg.jfce &&
+ jfc->jfc_cfg.jfce->fd >= 0) {
+ uint64_t one = 1;
+ (void)write(jfc->jfc_cfg.jfce->fd, &one, sizeof(one));
+ }
+}
+
+urma_device_attr_t mock_device_attr = {
+ .guid = {.raw = {10}},
+ .dev_cap = {},
+ .port_cnt = 1,
+ .port_attr = {{.max_mtu = URMA_MTU_4096,
+ .state = URMA_PORT_ACTIVE,
+ .active_width = URMA_LINK_X1,
+ .active_speed = URMA_SP_100G,
+ .active_mtu = URMA_MTU_4096}},
+ .reserved_jetty_id_min = 0,
+ .reserved_jetty_id_max = 1024};
+
+urma_eid_info_t mock_eid_info = {
+ .eid = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
+ 0x0C, 0x0D, 0x0E, 0x0F, 0x10}},
+ .eid_index = 0};
+
+} // namespace
+
+extern "C" {
+
+urma_status_t urma_init(urma_init_attr_t *init_attr) {
Review Comment:
The mock functionality lacks an independent feature switch, which can break normal URMA functionality.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Gzure (via GitHub)" <gi...@apache.org>.
Gzure commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r4013432694
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+ _input_processor.Init(s, InputMessengerProcessor::STREAM_URMA_JETTY);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _input_processor.Reset();
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
+ if (nr == 0) {
+ errno = EEOF;
+ return -1;
+ }
+ received += nr;
+ }
+ return 0;
+}
+
+void UrmaEndpoint::PushBackToReadBuf(const void* data, size_t len) {
+ _socket->fd_input_processor().read_buf().append(data, len);
+}
+
+int UrmaEndpoint::WriteToFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t written = 0;
+ while (written < len) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = write(fd, p + written, len - written);
+ if (nw >= 0) {
+ written += nw;
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+// ============================================================================
+// Hello builders / parsers.
+// ============================================================================
+
+void UrmaEndpoint::MakeLocalParsedHello(ParsedHello* out) const {
+ *out = ParsedHello{}; // value-initialize (avoids memset on non-trivial type)
+ out->buffer_size = static_cast<uint32_t>(GetUrmaRecvBlockSize());
+ out->recv_buffer_cnt = _rq_size - 1;
+ if (_resource && _resource->jetty) {
+ out->jetty_id = _resource->jetty->jetty_id.id;
+ out->uasid = _resource->jetty->jetty_id.uasid;
+ const urma_eid_t* local_eid = GetUrmaLocalEid();
+ const uint8_t* advertised_eid =
+ local_eid != nullptr
+ ? local_eid->raw
+ : _resource->jetty->jetty_id.eid.raw;
+ std::memcpy(out->eid, advertised_eid, 16);
+ }
+ out->tp_type = static_cast<uint8_t>(URMA_CTP);
+ // Pool segment: flatten g_pool_seg's seg fields.
+ urma_target_seg_t* pool = GetPoolSegFor(nullptr);
+ if (pool) {
+ std::memcpy(out->seg_eid, pool->seg.ubva.eid.raw, 16);
+ out->seg_uasid = pool->seg.ubva.uasid;
+ out->seg_va = pool->seg.ubva.va;
+ out->seg_len = pool->seg.len;
+ out->seg_token_id = pool->seg.token_id;
+ }
+}
+
+void UrmaEndpoint::FillLocalHelloV2(v2_wire::HelloMessage* out) const {
Review Comment:
Urma supports v2 and v3 to remain consistent with RDMA. If v2 is confirmed to no longer support new transports, only the implementation of v3 can be retained.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3950264271
##########
test/brpc_urma_unittest.cpp:
##########
@@ -0,0 +1,614 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <cstring>
+#include <limits>
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+
+#if BRPC_WITH_URMA
+#include "butil/atomicops.h"
+#include "butil/sys_byteorder.h"
+#include "urma_api.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "urma_types.h"
+
+using namespace brpc;
+
+namespace brpc {
+namespace urma {
+
+DECLARE_int32(urma_client_handshake_version);
+extern bool g_skip_urma_init;
+extern butil::atomic<bool> g_urma_available;
+
+} // namespace urma
+} // namespace brpc
+
+// ---------------------------------------------------------------------------
+// v2 binary HelloMessage: serialize + deserialize round-trips.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, v2_serialize_deserialize_roundtrip) {
+ urma::v2_wire::HelloMessage m;
+ m.msg_len = urma::v2_wire::HELLO_PACKET_LEN;
+ m.hello_ver = urma::v2_wire::HELLO_V2_VERSION;
+ m.impl_ver = urma::v2_wire::IMPL_V2_VERSION;
+ m.buffer_size = 8192;
+ m.recv_buffer_cnt = 127;
+ m.jetty_id = 0x12345678;
+ for (int i = 0; i < 16; ++i) {
+ m.eid[i] = static_cast<uint8_t>(i + 1);
+ }
+ m.uasid = 0xdeadbeef;
+ m.tp_type = 1; // URMA_CTP
+ for (int i = 0; i < 16; ++i) {
+ m.seg_eid[i] = static_cast<uint8_t>(16 - i);
+ }
+ m.seg_uasid = 0xcafebabe;
+ m.seg_va = 0x1122334455667788ULL;
+ m.seg_len = 1ULL << 20;
+ m.seg_token_id = 0x42424242;
+
+ uint8_t buf[urma::v2_wire::HELLO_BODY_LEN];
+ m.Serialize(buf);
+
+ urma::v2_wire::HelloMessage m2;
+ m2.Deserialize(buf);
+ EXPECT_EQ(m.msg_len, m2.msg_len);
+ EXPECT_EQ(m.hello_ver, m2.hello_ver);
+ EXPECT_EQ(m.impl_ver, m2.impl_ver);
+ EXPECT_EQ(m.buffer_size, m2.buffer_size);
+ EXPECT_EQ(m.recv_buffer_cnt, m2.recv_buffer_cnt);
+ EXPECT_EQ(m.jetty_id, m2.jetty_id);
+ EXPECT_EQ(0, memcmp(m.eid, m2.eid, 16));
+ EXPECT_EQ(m.uasid, m2.uasid);
+ EXPECT_EQ(m.tp_type, m2.tp_type);
+ EXPECT_EQ(0, memcmp(m.seg_eid, m2.seg_eid, 16));
+ EXPECT_EQ(m.seg_uasid, m2.seg_uasid);
+ EXPECT_EQ(m.seg_va, m2.seg_va);
+ EXPECT_EQ(m.seg_len, m2.seg_len);
+ EXPECT_EQ(m.seg_token_id, m2.seg_token_id);
+}
+
+// ---------------------------------------------------------------------------
+// v2 packet on the wire: "URMA" magic + body.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, v2_packet_magic_is_urma) {
+ EXPECT_EQ(4u, urma::v2_wire::MAGIC_STR_LEN);
+ char magic[4] = {'U', 'R', 'M', 'A'};
+ EXPECT_EQ(0, memcmp(magic, "URMA", 4));
+ EXPECT_EQ(4u + 82u, urma::v2_wire::HELLO_PACKET_LEN);
+}
+
+// ---------------------------------------------------------------------------
+// v3 protobuf UrmaHello: serialize + parse round-trips.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, v3_protobuf_roundtrip) {
+ urma::UrmaHello msg;
+ msg.set_buffer_size(8192);
+ msg.set_recv_buffer_cnt(127);
+ msg.set_jetty_id(0x12345678);
+ uint8_t eid[16];
+ for (int i = 0; i < 16; ++i) {
+ eid[i] = static_cast<uint8_t>(i + 1);
+ }
+ msg.set_eid(eid, 16);
+ msg.set_uasid(0xdeadbeef);
+ msg.set_tp_type(1);
+ uint8_t seg_eid[16];
+ for (int i = 0; i < 16; ++i) {
+ seg_eid[i] = static_cast<uint8_t>(16 - i);
+ }
+ msg.set_seg_eid(seg_eid, 16);
+ msg.set_seg_uasid(0xcafebabe);
+ msg.set_seg_va(0x1122334455667788ULL);
+ msg.set_seg_len(1ULL << 20);
+ msg.set_seg_token_id(0x42424242);
+
+ std::string body;
+ ASSERT_TRUE(msg.SerializeToString(&body));
+ urma::UrmaHello msg2;
+ ASSERT_TRUE(msg2.ParseFromString(body));
+ EXPECT_EQ(msg.buffer_size(), msg2.buffer_size());
+ EXPECT_EQ(msg.recv_buffer_cnt(), msg2.recv_buffer_cnt());
+ EXPECT_EQ(msg.jetty_id(), msg2.jetty_id());
+ EXPECT_EQ(16, msg2.eid().size());
+ EXPECT_EQ(0, memcmp(msg.eid().data(), msg2.eid().data(), 16));
+ EXPECT_EQ(msg.uasid(), msg2.uasid());
+ EXPECT_EQ(msg.tp_type(), msg2.tp_type());
+ EXPECT_EQ(16, msg2.seg_eid().size());
+ EXPECT_EQ(msg.seg_uasid(), msg2.seg_uasid());
+ EXPECT_EQ(msg.seg_va(), msg2.seg_va());
+ EXPECT_EQ(msg.seg_len(), msg2.seg_len());
+ EXPECT_EQ(msg.seg_token_id(), msg2.seg_token_id());
+}
+
+// ---------------------------------------------------------------------------
+// CreateServerHandshakeByMagic dispatches on the magic bytes.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, server_handshake_factory_dispatches_on_magic) {
+ // We cannot fully exercise the server handshake without a real socket +
+ // endpoint, but we can verify the factory returns the right protocol
+ // version for each magic, and nullptr for an unknown magic.
+ uint8_t magic_v2[4] = {'U', 'R', 'M', 'A'};
+ uint8_t magic_v3[4] = {'U', 'R', 'M', '3'};
+ uint8_t magic_bad[4] = {'P', 'R', 'P', 'C'};
+
+ // v2 magic -> protocol version 2
+ urma::UrmaHandshake* hs2 =
+ urma::CreateServerHandshakeByMagic(nullptr, magic_v2);
+ // Note: the factory dereferences the endpoint only inside SendLocalHello /
+ // ReceiveAndParseRemoteHello; passing nullptr is safe for the version query.
+ // (We delete immediately to avoid touching the endpoint.)
+ if (hs2) {
+ EXPECT_EQ(2, hs2->ProtocolVersion());
+ delete hs2;
+ }
+ // v3 magic -> protocol version 3
+ urma::UrmaHandshake* hs3 =
+ urma::CreateServerHandshakeByMagic(nullptr, magic_v3);
+ if (hs3) {
+ EXPECT_EQ(3, hs3->ProtocolVersion());
+ delete hs3;
+ }
+ // unknown magic -> nullptr (caller falls back to TCP)
+ urma::UrmaHandshake* hsb =
+ urma::CreateServerHandshakeByMagic(nullptr, magic_bad);
+ EXPECT_EQ(nullptr, hsb);
+}
+
+// ---------------------------------------------------------------------------
+// CreateClientHandshake picks the version from the gflag.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, client_handshake_factory_respects_flag) {
+ const int saved = urma::FLAGS_urma_client_handshake_version;
+
+ urma::FLAGS_urma_client_handshake_version = 2;
+ urma::UrmaHandshake* hs2 = urma::CreateClientHandshake(nullptr);
+ if (hs2) {
+ EXPECT_EQ(2, hs2->ProtocolVersion());
+ delete hs2;
+ }
+
+ urma::FLAGS_urma_client_handshake_version = 3;
+ urma::UrmaHandshake* hs3 = urma::CreateClientHandshake(nullptr);
+ if (hs3) {
+ EXPECT_EQ(3, hs3->ProtocolVersion());
+ delete hs3;
+ }
+
+ urma::FLAGS_urma_client_handshake_version = saved;
+}
Review Comment:
This test mutates a gflag (FLAGS_urma_client_handshake_version) and restores it manually at the end. Using GFLAGS_NAMESPACE::FlagSaver is the established pattern in this repo to guarantee restoration even on early test exit/failure, and avoids leaking flag state into subsequent tests.
##########
src/brpc/urma_transport.cpp:
##########
@@ -0,0 +1,248 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma_transport.h"
+
+#if BRPC_WITH_URMA
+
+#include <gflags/gflags.h>
+
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "bthread/bthread.h"
+#include "bthread/types.h"
+
+#include "brpc/event_dispatcher.h"
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/tcp_transport.h"
+#include "brpc/urma/urma_endpoint.h"
+#include "brpc/urma/urma_helper.h"
+
+namespace brpc {
+
+// Defined in urma_helper.cpp.
+DECLARE_bool(urma_use_polling);
+DECLARE_bool(urma_disable_bthread);
+
+void UrmaTransport::Init(Socket* socket, const SocketOptions& options) {
+ CHECK(_urma_ep == nullptr);
+ if (options.socket_mode == SOCKET_MODE_URMA) {
+ _urma_ep = new (std::nothrow) urma::UrmaEndpoint(socket);
+ if (!_urma_ep) {
+ const int saved_errno = errno;
+ PLOG(ERROR) << "Fail to create UrmaEndpoint";
+ socket->SetFailed(saved_errno, "Fail to create UrmaEndpoint: %s",
+ berror(saved_errno));
+ }
+ _urma_state = URMA_UNKNOWN;
+ } else {
+ _urma_state = URMA_OFF;
+ socket->_socket_mode = SOCKET_MODE_TCP;
+ }
+ _socket = socket;
+ _default_connect = options.app_connect;
+ _on_edge_trigger = options.on_edge_triggered_events;
+ if (options.need_on_edge_trigger && _on_edge_trigger == nullptr) {
+ _on_edge_trigger = urma::UrmaEndpoint::OnNewDataFromTcp;
+ }
+ _tcp_transport = std::make_shared<TcpTransport>();
+ _tcp_transport->Init(socket, options);
+}
+
+void UrmaTransport::Release() {
+ if (_urma_ep) {
+ delete _urma_ep;
+ _urma_ep = nullptr;
+ }
+}
+
+int UrmaTransport::Reset(int32_t /*expected_nref*/) {
+ if (_urma_ep) {
+ _urma_ep->Reset();
+ }
+ _urma_state = URMA_UNKNOWN;
+ return 0;
+}
+
+std::shared_ptr<AppConnect> UrmaTransport::Connect() {
+ if (_default_connect == nullptr) {
+ return std::make_shared<urma::UrmaConnect>();
+ }
+ return _default_connect;
+}
+
+int UrmaTransport::CutFromIOBuf(butil::IOBuf* buf) {
+ if (_urma_ep &&
+ _urma_state.load(butil::memory_order_acquire) != URMA_OFF) {
+ butil::IOBuf* data_arr[1] = {buf};
+ return _urma_ep->CutFromIOBufList(data_arr, 1);
+ } else {
+ return _tcp_transport->CutFromIOBuf(buf);
+ }
+}
Review Comment:
CutFromIOBuf() routes data to UrmaEndpoint while _urma_state is URMA_UNKNOWN. In that state, UrmaEndpoint::CutFromIOBufList() can return ENOTCONN because resources/remote_jetty are not established yet. This should mirror RdmaTransport and only use the URMA data path after negotiation resolves to URMA_ON; otherwise fall back to TCP until the handshake completes.
This issue also appears on line 99 of the same file.
##########
src/brpc/input_messenger.cpp:
##########
@@ -56,294 +54,291 @@ BRPC_VALIDATE_GFLAG(log_connection_close, PassValidate);
DEFINE_bool(socket_keepalive, false,
"Enable keepalive of sockets if this value is true");
-DEFINE_int32(socket_keepalive_idle_s, -1,
- "Set idle time for socket keepalive in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_idle_s, -1,
+ "Set idle time for socket keepalive in seconds if this value is positive");
-DEFINE_int32(socket_keepalive_interval_s, -1,
- "Set interval between keepalives in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_interval_s, -1,
+ "Set interval between keepalives in seconds if this value is positive");
DEFINE_int32(socket_keepalive_count, -1,
"Set number of keepalives before death if this value is positive");
-DEFINE_int32(socket_tcp_user_timeout_ms, -1,
- "If this value is positive, set number of milliseconds that transmitted "
- "data may remain unacknowledged, or bufferred data may remain untransmitted "
- "(due to zero window size) before TCP will forcibly close the corresponding "
- "connection and return ETIMEDOUT to the application. Only linux supports "
- "TCP_USER_TIMEOUT.");
+DEFINE_int32(
+ socket_tcp_user_timeout_ms, -1,
+ "If this value is positive, set number of milliseconds that transmitted "
+ "data may remain unacknowledged, or bufferred data may remain "
+ "untransmitted "
+ "(due to zero window size) before TCP will forcibly close the "
+ "corresponding "
+ "connection and return ETIMEDOUT to the application. Only linux supports "
+ "TCP_USER_TIMEOUT.");
DECLARE_bool(usercode_in_pthread);
DECLARE_bool(usercode_in_coroutine);
-void* ProcessInputMessage(void* void_arg) {
- InputMessageBase* msg = static_cast<InputMessageBase*>(void_arg);
- msg->_process(msg);
- return nullptr;
+void *ProcessInputMessage(void *void_arg) {
+ InputMessageBase *msg = static_cast<InputMessageBase *>(void_arg);
+ msg->_process(msg);
+ return nullptr;
}
struct RunLastMessage {
- inline void operator()(InputMessageBase* last_msg) {
- ProcessInputMessage(last_msg);
- }
+ inline void operator()(InputMessageBase *last_msg) {
+ ProcessInputMessage(last_msg);
+ }
};
InputMessageClosure::~InputMessageClosure() noexcept(false) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
}
-void InputMessageClosure::reset(InputMessageBase* m) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
- _msg = m;
+void InputMessageClosure::reset(InputMessageBase *m) {
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
+ _msg = m;
}
-void InputMessenger::OnNewMessages(Socket* m) {
- // Notes:
- // - If the socket has only one message, the message will be parsed and
- // processed in this bthread. nova-pbrpc and http works in this way.
- // - If the socket has several messages, all messages will be parsed (
- // meaning cutting from butil::IOBuf. serializing from protobuf is part of
- // "process") in this bthread. All messages except the last one will be
- // processed in separate bthreads. To minimize the overhead, scheduling
- // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
- // - Verify will always be called in this bthread at most once and before
- // any process.
- InputMessengerProcessor& processor = m->fd_input_processor();
- int progress = Socket::PROGRESS_INIT;
-
- // Notice that all *return* no matter successful or not will run last
- // message, even if the socket is about to be closed. This should be
- // OK in most cases.
- InputMessageClosure last_msg;
- bool read_eof = false;
- while (!read_eof) {
- const int64_t received_us = butil::cpuwide_time_us();
- const int64_t base_realtime = butil::gettimeofday_us() - received_us;
-
- // Read.
- const ssize_t nr = m->DoRead(&processor.read_buf(),
- processor.OnceReadSize());
- if (nr <= 0) {
- if (0 == nr) {
- // Set `read_eof' flag and proceed to feed EOF into `Protocol'
- // (implied by an empty processor.read_buf()), which may produce
- // a new `InputMessageBase' under some protocols such as HTTP
- LOG_IF(WARNING, FLAGS_log_connection_close) << *m << " was closed by remote side";
- read_eof = true;
- } else if (errno != EAGAIN) {
- if (errno == EINTR) {
- continue; // just retry
- }
- const int saved_errno = errno;
- PLOG(WARNING) << "Fail to read from " << *m;
- m->SetFailed(saved_errno, "Fail to read from %s: %s",
- m->description().c_str(), berror(saved_errno));
- return;
- } else if (!m->MoreReadEvents(&progress)) {
- return;
- } else { // new events during processing
- continue;
- }
- }
-
- if (processor.ProcessNewMessage(nr, read_eof, received_us,
- base_realtime, last_msg) < 0) {
- return;
+void InputMessenger::OnNewMessages(Socket *m) {
+ // Notes:
+ // - If the socket has only one message, the message will be parsed and
+ // processed in this bthread. nova-pbrpc and http works in this way.
+ // - If the socket has several messages, all messages will be parsed (
+ // meaning cutting from butil::IOBuf. serializing from protobuf is part of
+ // "process") in this bthread. All messages except the last one will be
+ // processed in separate bthreads. To minimize the overhead, scheduling
+ // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
+ // - Verify will always be called in this bthread at most once and before
+ // any process.
+ InputMessengerProcessor &processor = m->fd_input_processor();
+ int progress = Socket::PROGRESS_INIT;
+
+ // Notice that all *return* no matter successful or not will run last
+ // message, even if the socket is about to be closed. This should be
+ // OK in most cases.
+ InputMessageClosure last_msg;
+ bool read_eof = false;
+ while (!read_eof) {
+ const int64_t received_us = butil::cpuwide_time_us();
+ const int64_t base_realtime = butil::gettimeofday_us() - received_us;
+
+ // Read.
+ const ssize_t nr =
+ m->DoRead(&processor.read_buf(), processor.OnceReadSize());
+ if (nr <= 0) {
+ if (0 == nr) {
+ // Set `read_eof' flag and proceed to feed EOF into `Protocol'
+ // (implied by an empty processor.read_buf()), which may produce
+ // a new `InputMessageBase' under some protocols such as HTTP
+ LOG_IF(WARNING, FLAGS_log_connection_close)
+ << *m << " was closed by remote side";
+ read_eof = true;
+ } else if (errno != EAGAIN) {
+ if (errno == EINTR) {
+ continue; // just retry
}
+ const int saved_errno = errno;
+ PLOG(WARNING) << "Fail to read from " << *m;
+ m->SetFailed(saved_errno, "Fail to read from %s: %s",
+ m->description().c_str(), berror(saved_errno));
+ return;
+ } else if (!m->MoreReadEvents(&progress)) {
+ return;
+ } else { // new events during processing
+ continue;
+ }
}
- if (read_eof) {
- m->SetEOF();
+ if (processor.ProcessNewMessage(nr, read_eof, received_us, base_realtime,
+ last_msg) < 0) {
+ return;
}
+ }
+
+ if (read_eof) {
+ m->SetEOF();
+ }
}
InputMessenger::InputMessenger(size_t capacity)
- : _handlers(nullptr)
- , _max_index(-1)
- , _non_protocol(false)
- , _capacity(capacity) {
-}
+ : _handlers(nullptr), _max_index(-1), _non_protocol(false),
+ _capacity(capacity) {}
InputMessenger::~InputMessenger() {
- delete[] _handlers;
- _handlers = nullptr;
- _max_index.store(-1, butil::memory_order_relaxed);
- _capacity = 0;
+ delete[] _handlers;
+ _handlers = nullptr;
+ _max_index.store(-1, butil::memory_order_relaxed);
+ _capacity = 0;
}
-int InputMessenger::AddHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = false;
- }
- if (_non_protocol) {
- CHECK(false) << "AddNonProtocolHandler was invoked";
- return -1;
- }
- ProtocolType type = FindProtocolOfHandler(handler);
- if (type == PROTOCOL_UNKNOWN) {
- CHECK(false) << "Adding a handler which doesn't belong to any protocol";
- return -1;
- }
- const int index = type;
- if (index >= (int)_capacity) {
- LOG(FATAL) << "Can't add more handlers than " << _capacity;
- return -1;
- }
- if (_handlers[index].parse == nullptr) {
- // The same protocol might be added more than twice
- _handlers[index] = handler;
- } else if (_handlers[index].parse != handler.parse
- || _handlers[index].process != handler.process) {
- CHECK(_handlers[index].parse == handler.parse);
- CHECK(_handlers[index].process == handler.process);
- return -1;
- }
- if (index > _max_index.load(butil::memory_order_relaxed)) {
- _max_index.store(index, butil::memory_order_release);
- }
- return 0;
-}
-
-int InputMessenger::AddNonProtocolHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = true;
- }
- if (!_non_protocol) {
- CHECK(false) << "AddHandler was invoked";
- return -1;
- }
- const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+int InputMessenger::AddHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = false;
+ }
+ if (_non_protocol) {
+ CHECK(false) << "AddNonProtocolHandler was invoked";
+ return -1;
+ }
+ ProtocolType type = FindProtocolOfHandler(handler);
+ if (type == PROTOCOL_UNKNOWN) {
+ CHECK(false) << "Adding a handler which doesn't belong to any protocol";
+ return -1;
+ }
+ const int index = type;
+ if (index >= (int)_capacity) {
+ LOG(FATAL) << "Can't add more handlers than " << _capacity;
+ return -1;
+ }
+ if (_handlers[index].parse == nullptr) {
+ // The same protocol might be added more than twice
_handlers[index] = handler;
+ } else if (_handlers[index].parse != handler.parse ||
+ _handlers[index].process != handler.process) {
+ CHECK(_handlers[index].parse == handler.parse);
+ CHECK(_handlers[index].process == handler.process);
+ return -1;
+ }
+ if (index > _max_index.load(butil::memory_order_relaxed)) {
_max_index.store(index, butil::memory_order_release);
- return 0;
+ }
+ return 0;
}
-int InputMessenger::Create(const butil::EndPoint& remote_side,
- time_t health_check_interval_s,
- SocketId* id) {
- SocketOptions options;
- options.remote_side = remote_side;
- options.user = this;
- options.on_edge_triggered_events = OnNewMessages;
- options.health_check_interval_s = health_check_interval_s;
- if (FLAGS_socket_keepalive) {
- options.keepalive_options = std::make_shared<SocketKeepaliveOptions>();
- options.keepalive_options->keepalive_idle_s
- = FLAGS_socket_keepalive_idle_s;
- options.keepalive_options->keepalive_interval_s
- = FLAGS_socket_keepalive_interval_s;
- options.keepalive_options->keepalive_count
- = FLAGS_socket_keepalive_count;
- }
- options.tcp_user_timeout_ms = FLAGS_socket_tcp_user_timeout_ms;
- return Socket::Create(options, id);
+int InputMessenger::AddNonProtocolHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = true;
+ }
+ if (!_non_protocol) {
+ CHECK(false) << "AddHandler was invoked";
+ return -1;
+ }
+ const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+ _handlers[index] = handler;
+ _max_index.store(index, butil::memory_order_release);
+ return 0;
Review Comment:
AddNonProtocolHandler() increments _max_index and writes into _handlers without checking that the new index is still within _capacity. This can write past the allocated handler array if enough non-protocol handlers are registered.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "dwh110 (via GitHub)" <gi...@apache.org>.
dwh110 commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3930753680
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1846 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
+ if (nr == 0) {
+ errno = EEOF;
+ return -1;
+ }
+ received += nr;
+ }
+ return 0;
+}
+
+void UrmaEndpoint::PushBackToReadBuf(const void* data, size_t len) {
+ _socket->_read_buf.append(data, len);
+}
+
+int UrmaEndpoint::WriteToFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t written = 0;
+ while (written < len) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = write(fd, p + written, len - written);
+ if (nw >= 0) {
+ written += nw;
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+// ============================================================================
+// Hello builders / parsers.
+// ============================================================================
+
+void UrmaEndpoint::MakeLocalParsedHello(ParsedHello* out) const {
+ *out = ParsedHello{}; // value-initialize (avoids memset on non-trivial type)
+ out->buffer_size = static_cast<uint32_t>(GetUrmaRecvBlockSize());
+ out->recv_buffer_cnt = _rq_size - 1;
+ if (_resource && _resource->jetty) {
+ out->jetty_id = _resource->jetty->jetty_id.id;
+ out->uasid = _resource->jetty->jetty_id.uasid;
+ const urma_eid_t* local_eid = GetUrmaLocalEid();
+ const uint8_t* advertised_eid =
+ local_eid != nullptr
+ ? local_eid->raw
+ : _resource->jetty->jetty_id.eid.raw;
+ std::memcpy(out->eid, advertised_eid, 16);
+ }
+ out->tp_type = static_cast<uint8_t>(URMA_CTP);
+ // Pool segment: flatten g_pool_seg's seg fields.
+ urma_target_seg_t* pool = GetPoolSegFor(nullptr);
+ if (pool) {
+ std::memcpy(out->seg_eid, pool->seg.ubva.eid.raw, 16);
+ out->seg_uasid = pool->seg.ubva.uasid;
+ out->seg_va = pool->seg.ubva.va;
+ out->seg_len = pool->seg.len;
+ out->seg_token_id = pool->seg.token_id;
+ }
+}
+
+void UrmaEndpoint::FillLocalHelloV2(v2_wire::HelloMessage* out) const {
+ *out = v2_wire::HelloMessage{}; // value-initialize
+ out->msg_len = v2_wire::HELLO_PACKET_LEN;
+ out->hello_ver = v2_wire::HELLO_V2_VERSION;
+ out->impl_ver = v2_wire::IMPL_V2_VERSION;
+ ParsedHello p;
+ MakeLocalParsedHello(&p);
+ out->buffer_size = p.buffer_size;
+ out->recv_buffer_cnt = p.recv_buffer_cnt;
+ out->jetty_id = p.jetty_id;
+ std::memcpy(out->eid, p.eid, 16);
+ out->uasid = p.uasid;
+ out->tp_type = p.tp_type;
+ std::memcpy(out->seg_eid, p.seg_eid, 16);
+ out->seg_uasid = p.seg_uasid;
+ out->seg_va = p.seg_va;
+ out->seg_len = p.seg_len;
+ out->seg_token_id = p.seg_token_id;
+}
+
+void UrmaEndpoint::FillLocalHelloV3(UrmaHello* out) const {
+ ParsedHello p;
+ MakeLocalParsedHello(&p);
+ out->set_buffer_size(p.buffer_size);
+ out->set_recv_buffer_cnt(p.recv_buffer_cnt);
+ out->set_jetty_id(p.jetty_id);
+ out->set_eid(p.eid, 16);
+ out->set_uasid(p.uasid);
+ out->set_tp_type(p.tp_type);
+ out->set_seg_eid(p.seg_eid, 16);
+ out->set_seg_uasid(p.seg_uasid);
+ out->set_seg_va(p.seg_va);
+ out->set_seg_len(p.seg_len);
+ out->set_seg_token_id(p.seg_token_id);
+}
+
+int UrmaEndpoint::WriteHelloV3(const UrmaHello& msg) {
+ butil::IOBuf packet;
+ packet.append("URM3", 4);
+ std::string body;
+ if (!msg.SerializeToString(&body)) {
+ LOG(ERROR) << "Fail to serialize UrmaHello";
+ return -1;
+ }
+ uint32_t pb_size_be = butil::HostToNet32(static_cast<uint32_t>(body.size()));
+ packet.append(&pb_size_be, sizeof(pb_size_be));
+ packet.append(body);
+ return WriteToFd(packet);
+}
+
+int UrmaEndpoint::WriteToFd(butil::IOBuf& data) {
+ // Write out the IOBuf in a single WriteToFd-style loop.
+ while (!data.empty()) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = data.cut_into_file_descriptor(fd);
+ if (nw >= 0) {
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+int UrmaEndpoint::ReadAndParseHelloV3(ParsedHello* out, bool* negotiated) {
+ *negotiated = false;
+ uint32_t pb_size_be = 0;
+ if (ReadFromFd(&pb_size_be, sizeof(pb_size_be)) < 0) {
+ return -1;
+ }
+ const uint32_t pb_size = butil::NetToHost32(pb_size_be);
+ if (pb_size == 0 || pb_size > 4096) {
+ return 0;
+ }
+ std::string body(pb_size, '\0');
+ if (ReadFromFd(&body[0], pb_size) < 0) {
+ return -1;
+ }
+ UrmaHello msg;
+ if (!msg.ParseFromArray(body.data(), static_cast<int>(body.size()))) {
+ return 0;
+ }
+ if (msg.eid().size() != 16 || msg.seg_eid().size() != 16) {
+ return 0;
+ }
+ out->buffer_size = msg.buffer_size();
+ out->recv_buffer_cnt = msg.recv_buffer_cnt();
+ out->jetty_id = msg.jetty_id();
+ std::memcpy(out->eid, msg.eid().data(), 16);
+ out->uasid = msg.uasid();
+ out->tp_type = static_cast<uint8_t>(msg.tp_type());
+ std::memcpy(out->seg_eid, msg.seg_eid().data(), 16);
+ out->seg_uasid = msg.seg_uasid();
+ out->seg_va = msg.seg_va();
+ out->seg_len = msg.seg_len();
+ out->seg_token_id = msg.seg_token_id();
+ if (!ValidHello(*out)) {
+ return 0;
+ }
+ *negotiated = true;
+ return 0;
+}
+
+// ============================================================================
+// Allocate / deallocate per-connection resources.
+// ============================================================================
+
+int UrmaEndpoint::AllocateResources() {
+ if (_resource) {
+ return 0;
+ }
+ urma_context_t* ctx = GetUrmaContext();
+ if (!ctx) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ _resource = new (std::nothrow) UrmaResource();
+ if (!_resource) {
+ return -1;
+ }
+
+ // Try the prepared pool first (sized sq/rq match).
+ if (_sq_size <= static_cast<uint16_t>(FLAGS_urma_sq_size) &&
+ _rq_size <= static_cast<uint16_t>(FLAGS_urma_rq_size)) {
+ BAIDU_SCOPED_LOCK(g_prepared_mutex);
+ if (g_prepared_list) {
+ UrmaResource* next = g_prepared_list->next;
+ delete _resource;
+ _resource = g_prepared_list;
+ g_prepared_list = next;
+ _resource->next = nullptr;
+ --g_prepared_cnt;
+ }
+ }
+
+ if (!_resource->jfc) {
+ // The SDK requires every JFC to reference a JFCE. Polling mode does
+ // not arm or consume it, but still supplies the required object.
+ _resource->jfce = urma_create_jfce(ctx);
+ if (!_resource->jfce ||
+ (!FLAGS_urma_use_polling && _resource->jfce->fd < 0)) {
+ LOG(ERROR) << "Fail to create a usable URMA JFCE";
+ errno = ENODEV;
+ return -1;
+ }
+
+ urma_jfc_cfg_t jfc_cfg{};
+ jfc_cfg.depth = static_cast<uint32_t>(_sq_size + _rq_size);
+ jfc_cfg.jfce = _resource->jfce;
+ _resource->jfc = urma_create_jfc(ctx, &jfc_cfg);
+ if (!_resource->jfc) {
+ PLOG(ERROR) << "urma_create_jfc";
+ return -1;
+ }
+
+ urma_jfr_cfg_t jfr_cfg{};
+ jfr_cfg.depth = static_cast<uint32_t>(_rq_size);
+ jfr_cfg.trans_mode = URMA_TM_RM;
+ jfr_cfg.max_sge = 1;
+ jfr_cfg.min_rnr_timer = URMA_TYPICAL_MIN_RNR_TIMER;
+ jfr_cfg.jfc = _resource->jfc;
+ _resource->jfr = urma_create_jfr(ctx, &jfr_cfg);
+ if (!_resource->jfr) {
+ PLOG(ERROR) << "urma_create_jfr";
+ return -1;
+ }
+
+ urma_jetty_cfg_t jetty_cfg{};
+ jetty_cfg.flag.bs.share_jfr = 1;
+ jetty_cfg.jfs_cfg.depth = static_cast<uint32_t>(_sq_size);
+ jetty_cfg.jfs_cfg.trans_mode = URMA_TM_RM;
+ jetty_cfg.jfs_cfg.priority = GetUrmaJettyPriority();
+ jetty_cfg.jfs_cfg.max_sge =
+ static_cast<uint8_t>(GetUrmaMaxSge());
+ jetty_cfg.jfs_cfg.rnr_retry = URMA_TYPICAL_RNR_RETRY;
+ jetty_cfg.jfs_cfg.err_timeout = URMA_TYPICAL_ERR_TIMEOUT;
+ jetty_cfg.jfs_cfg.jfc = _resource->jfc;
+ jetty_cfg.shared.jfr = _resource->jfr;
+ jetty_cfg.shared.jfc = _resource->jfc;
+ _resource->jetty = urma_create_jetty(ctx, &jetty_cfg);
+ if (!_resource->jetty) {
+ PLOG(ERROR) << "urma_create_jetty";
+ return -1;
+ }
+ }
+
+ _sbuf.resize(_sq_size - RESERVED_WR_NUM);
+ _rbuf.resize(_rq_size);
+ _rbuf_data.resize(_rq_size, nullptr);
+
+ // Wrap the JFCE fd in a brpc Socket so PollCq is driven by epoll.
+ if (!FLAGS_urma_use_polling) {
+ if (!_resource->jfce || _resource->jfce->fd < 0) {
+ LOG(ERROR) << "Prepared URMA resource has no usable JFCE";
+ errno = ENODEV;
+ return -1;
+ }
+ if (ReqNotifyCq() != 0) {
+ return -1;
+ }
+ SocketOptions options;
+ options.user = this;
+ options.keytable_pool = _socket->keytable_pool();
+ options.fd = _resource->jfce->fd;
+ options.on_edge_triggered_events = PollCq;
+ if (Socket::Create(options, &_cq_sid) < 0) {
+ PLOG(ERROR) << "Fail to create CQ socket";
+ return -1;
+ }
+ } else {
+ // Polling mode: synthetic carrier socket (no fd).
+ SocketOptions options;
+ options.user = this;
+ options.keytable_pool = _socket->keytable_pool();
+ options.on_edge_triggered_events = PollCq;
+ if (Socket::Create(options, &_cq_sid) < 0) {
+ PLOG(ERROR) << "Fail to create CQ socket (polling)";
+ return -1;
+ }
+ PollerAddCqSid();
+ }
+ return 0;
+}
+
+void UrmaEndpoint::DeallocateResources() {
+ if (!_resource) {
+ return;
+ }
+
+ if (FLAGS_urma_use_polling) {
+ PollerRemoveCqSid();
+ }
+
+ // Tear down the CQ socket so the EventDispatcher stops calling PollCq.
+ if (_cq_sid != INVALID_SOCKET_ID) {
+ SocketUniquePtr s;
+ if (Socket::Address(_cq_sid, &s) == 0) {
+ if (s->fd() >= 0) {
+ s->_io_event.RemoveConsumer(s->_fd);
+ }
+ s->_user = nullptr; // Do not release user (this UrmaEndpoint).
+ s->_fd = -1; // Already removed fd from epoll.
+ s->SetFailed();
+ }
+ _cq_sid = INVALID_SOCKET_ID;
+ }
+
+ // Reusing a Jetty requires a driver-supported RESET plus a complete JFC
+ // drain. Until that lifecycle is implemented, prepared resources are
+ // one-shot: they accelerate connection setup but are destroyed on close.
+ delete _resource;
+ _resource = nullptr;
+}
+
+// ============================================================================
+// ImportPeer: the critical import_seg-before-import_jetty sequence.
+// ============================================================================
+
+int UrmaEndpoint::ImportPeer(const ParsedHello& peer) {
+ urma_context_t* ctx = GetUrmaContext();
+ if (!ctx) {
+ errno = ENODEV;
+ return -1;
+ }
+
+ // 1. urma_import_seg FIRST so the kernel establishes TP routing for the
+ // remote EID. Without this the first SEND is rejected by hardware with
+ // URMA_CR_RNR_RETRY_CNT_EXC_ERR.
+ urma_seg_t peer_seg{};
+ std::memcpy(peer_seg.ubva.eid.raw, peer.seg_eid, 16);
+ peer_seg.ubva.uasid = peer.seg_uasid;
+ peer_seg.ubva.va = peer.seg_va;
+ peer_seg.len = peer.seg_len;
+ peer_seg.token_id = peer.seg_token_id;
+ urma_token_t seg_token{};
+ urma_import_seg_flag_t seg_flag{};
+ seg_flag.bs.cacheable = URMA_NON_CACHEABLE;
+ seg_flag.bs.access = URMA_ACCESS_READ | URMA_ACCESS_WRITE | URMA_ACCESS_ATOMIC;
+ seg_flag.bs.mapping = URMA_SEG_NOMAP;
+ _resource->remote_seg = urma_import_seg(ctx, &peer_seg, &seg_token, 0, seg_flag);
+ if (!_resource->remote_seg) {
+ PLOG(ERROR) << "urma_import_seg failed";
+ return -1;
+ }
+
+ // 2. urma_import_jetty.
+ urma_rjetty_t remote{};
+ std::memcpy(remote.jetty_id.eid.raw, peer.eid, 16);
+ remote.jetty_id.uasid = peer.uasid;
+ remote.jetty_id.id = peer.jetty_id;
+ remote.trans_mode = URMA_TM_RM;
+ remote.type = URMA_JETTY;
+ if (peer.tp_type > static_cast<uint8_t>(URMA_UTP)) {
+ errno = EPROTO;
+ return -1;
+ }
+ remote.tp_type = static_cast<urma_tp_type_t>(peer.tp_type);
+
+ urma_token_t token{};
+ const bool use_bonding_extension =
+ IsUrmaBondingDevice() && remote.trans_mode == URMA_TM_RM;
+ errno = 0;
+ if (use_bonding_extension) {
+#if BRPC_URMA_HAS_BONDING_EXT
+ // The bonding provider needs the local jetty to associate its send
+ // path with the imported target. A plain import may return success
+ // without setting that association, leaving traffic one-way only.
+ bondp_rjetty_t bonding_remote{};
+ bonding_remote.base = remote;
+ bonding_remote.base.flag.bs.has_drv_ext = 1;
+ bonding_remote.jetty = _resource->jetty;
+ _resource->remote_jetty =
+ urma_import_jetty(ctx, &bonding_remote.base, &token);
+#else
+ LOG(ERROR) << "Bonding remote jetty import requires provider header "
+ "urma_ubagg.h";
+ errno = ENOTSUP;
+#endif
+ } else {
+ _resource->remote_jetty = urma_import_jetty(ctx, &remote, &token);
+ }
+ if (!_resource->remote_jetty) {
+ if (errno == 0) {
+ errno = EIO;
+ }
+ char remote_eid[URMA_EID_STR_LEN + 1] = {};
+ std::snprintf(remote_eid, sizeof(remote_eid), EID_FMT,
+ EID_RAW_ARGS(peer.eid));
+ PLOG(ERROR) << "urma_import_jetty failed"
+ << " remote_eid=" << remote_eid
+ << " remote_uasid=" << peer.uasid
+ << " remote_jetty_id=" << peer.jetty_id
+ << " trans_mode=" << remote.trans_mode
+ << " tp_type=" << remote.tp_type
+ << " bonding_extension=" << use_bonding_extension;
+ return -1;
+ }
+ return 0;
+}
+
+// ============================================================================
+// Send / recv data path.
+// ============================================================================
+
+// Private IOBuf accessor mirroring RdmaIOBuf: reach into IOBuf block refs to
+// build a urma_sge_t directly, without memcpy.
+class UrmaIOBuf : private butil::IOBuf {
+ friend class ::brpc::urma::UrmaEndpoint;
+public:
+ using butil::IOBuf::_ref_num;
+ using butil::IOBuf::_ref_at;
+ using butil::IOBuf::fetch1;
+ using butil::IOBuf::get_first_data_meta;
+ using butil::IOBuf::cutn;
+ // Build the SGE for the current head block.
+ // Returns bytes added, or -1 (errno set).
+ ssize_t cut_into_sglist(urma_sge_t* sglist, size_t* sge_index,
+ butil::IOBuf* to, size_t max_sge,
+ size_t max_len) {
+ size_t len = 0;
+ while (*sge_index < max_sge && len < max_len && _ref_num() != 0) {
+ butil::IOBuf::BlockRef const& r = _ref_at(0);
+ const void* start = fetch1();
+ urma_target_seg_t* tseg =
+ GetPoolSegFor(const_cast<void*>(start));
+ if (!tseg) {
+ // User-registered memory: look up the seg handle.
+ uint64_t meta = get_first_data_meta();
+ if (meta != 0) {
+ tseg = reinterpret_cast<urma_target_seg_t*>(
+ static_cast<uintptr_t>(meta));
+ }
+ }
+ if (!tseg) {
+ errno = ERDMAMEM;
+ return -1;
+ }
+ size_t this_len = r.length;
+ if (len + this_len > max_len) {
+ this_len = max_len - len;
+ }
+ sglist[*sge_index].addr = reinterpret_cast<uint64_t>(start);
+ sglist[*sge_index].len = static_cast<uint32_t>(this_len);
+ sglist[*sge_index].tseg = tseg;
+ cutn(to, this_len);
+ len += this_len;
+ (*sge_index)++;
+ }
+ return static_cast<ssize_t>(len);
+ }
+};
+
+ssize_t UrmaEndpoint::CutFromIOBufList(butil::IOBuf** from, size_t ndata) {
+ if (!_resource || !_resource->jetty || !_resource->remote_jetty) {
+ errno = ENOTCONN;
+ return -1;
+ }
+ int max_sge = GetUrmaMaxSge();
+ if (max_sge < 1) {
+ max_sge = 1;
+ }
+
+ urma_sge_t* sglist = static_cast<urma_sge_t*>(
+ alloca(sizeof(urma_sge_t) * max_sge));
+ if (!sglist) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ size_t current = 0;
+ ssize_t total_len = 0;
+ while (current < ndata) {
+ uint16_t remote_wnd = _remote_rq_window_size.load(butil::memory_order_relaxed);
+ uint16_t sq_wnd = _sq_window_size.load(butil::memory_order_relaxed);
+ if (remote_wnd == 0 || sq_wnd == 0) {
+ if (total_len > 0) {
+ break;
+ }
+ errno = EAGAIN;
+ return -1;
+ }
+ butil::IOBuf* to = &_sbuf[_sq_current];
+ size_t sge_index = 0;
+ size_t this_len = 0;
+ size_t max_len = _remote_recv_block_size > 0
+ ? _remote_recv_block_size
+ : GetUrmaRecvBlockSize();
+ while (sge_index < static_cast<size_t>(max_sge) &&
+ this_len < max_len && current < ndata) {
+ auto* data = reinterpret_cast<UrmaIOBuf*>(from[current]);
+ if (data->empty()) {
+ ++current;
+ continue;
+ }
+ ssize_t n = data->cut_into_sglist(sglist, &sge_index, to,
+ max_sge, max_len - this_len);
+ if (n < 0) {
+ return -1;
+ }
+ this_len += n;
+ }
+ if (sge_index == 0) {
+ break;
+ }
+
+ urma_sg_t sg{sglist, static_cast<uint32_t>(sge_index)};
+ urma_jfs_wr_t wr{};
+ std::memset(&wr, 0, sizeof(wr));
+ // Send payload with URMA_OPC_SEND. Receive credits are flushed
+ // separately by SendImm() after SendAck() reaches its threshold.
+ // Piggybacking credits turns every payload into SEND_IMM and can
+ // produce asymmetric completions with the bonding provider.
+ wr.opcode = URMA_OPC_SEND;
+ wr.flag.bs.complete_enable = 1;
+ wr.tjetty = _resource->remote_jetty;
+ wr.send.src = sg;
+ wr.user_ctx = 1;
+ urma_jfs_wr_t* bad_wr = nullptr;
+ const uint16_t sq_slot = _sq_current;
+ const uint32_t local_jetty_id = _resource->jetty->jetty_id.id;
+ const uint32_t remote_jetty_id = _resource->remote_jetty->id.id;
+
+ // Reserve both credits before making the WR visible to the provider.
+ // In polling mode a completion (and even the peer's receive-credit
+ // ACK) can be processed by another thread before post_send returns.
+ // Decrementing after post therefore creates a transient capacity + 1
+ // window and makes the strict credit check tear down a healthy
+ // connection.
+ _remote_rq_window_size.fetch_sub(1, butil::memory_order_relaxed);
+ _sq_window_size.fetch_sub(1, butil::memory_order_relaxed);
+ int rc = urma_post_jetty_send_wr(_resource->jetty, &wr, &bad_wr);
+ if (rc != URMA_SUCCESS) {
+ const int provider_errno = errno;
+ _remote_rq_window_size.fetch_add(1, butil::memory_order_relaxed);
+ _sq_window_size.fetch_add(1, butil::memory_order_relaxed);
+ LOG(WARNING) << "urma_post_jetty_send_wr failed: " << rc
+ << ", provider_errno=" << provider_errno
+ << " (" << berror(provider_errno) << ')'
+ << ", bad_wr=" << static_cast<const void*>(bad_wr)
+ << ", bad_is_current=" << (bad_wr == &wr)
+ << ", sq_slot=" << sq_slot
+ << ", local_jetty_id=" << local_jetty_id
+ << ", remote_jetty_id=" << remote_jetty_id
+ << ", state=" << GetStateStr()
+ << ", sq_window=" << sq_wnd
+ << ", remote_rq_window=" << remote_wnd
+ << ", num_sge=" << sge_index
+ << ", configured_max_sge=" << GetUrmaMaxSge()
+ << ", payload_size=" << this_len
+ << " on " << _socket->description();
+ errno = rc;
+ return -1;
+ }
+ _sq_current = (_sq_current + 1) % (_sq_size - RESERVED_WR_NUM);
+ total_len += static_cast<ssize_t>(this_len);
+ }
+ return total_len;
+}
+
+bool UrmaEndpoint::IsWritable() const {
+ return _remote_rq_window_size.load(butil::memory_order_relaxed) > 0 &&
+ _sq_window_size.load(butil::memory_order_relaxed) > 0;
+}
+
+// ============================================================================
+// Recv path.
+// ============================================================================
+
+int UrmaEndpoint::DoPostRecv(void* block, size_t block_size) {
+ urma_target_seg_t* tseg = GetPoolSegFor(block);
+ if (!tseg) {
+ errno = ERDMAMEM;
+ return -1;
+ }
+ urma_sge_t sge{reinterpret_cast<uint64_t>(block),
+ static_cast<uint32_t>(block_size), tseg, nullptr};
+ urma_sg_t sg{&sge, 1};
+ urma_jfr_wr_t wr{sg, 0, nullptr};
+ urma_jfr_wr_t* bad = nullptr;
+ // Use the shared-JFR path on every device, including bonding. The bonding
+ // provider owns physical receive scheduling for the JFR; a local
+ // jetty-to-target association is not part of the RM receive API.
+ const urma_status_t status =
+ urma_post_jfr_wr(_resource->jfr, &wr, &bad);
+ if (status != URMA_SUCCESS) {
+ LOG(WARNING) << "Failed to post URMA receive WR: status=" << status
+ << " bonding=" << IsUrmaBondingDevice()
+ << " bad_wr=" << static_cast<const void*>(bad)
+ << " bad_is_current=" << (bad == &wr)
+ << " local_jetty_id=" << _resource->jetty->jetty_id.id
+ << " provider_associated_remote="
+ << static_cast<const void*>(
+ _resource->jetty->remote_jetty)
+ << " state=" << GetStateStr()
+ << " on " << _socket->description();
+ errno = status;
+ return -1;
+ }
+ return 0;
+}
+
+int UrmaEndpoint::PostRecv(uint32_t num, bool zerocopy) {
+ for (uint32_t i = 0; i < num; ++i) {
+ size_t block_size = GetUrmaRecvBlockSize();
+ if (zerocopy) {
+ _rbuf[_rq_received].clear();
+ butil::IOBufAsZeroCopyOutputStream zcis(
+ &_rbuf[_rq_received], block_size + IOBUF_BLOCK_HEADER_LEN);
+ void* data = nullptr;
+ int size = 0;
+ if (!zcis.Next(&data, &size) || !data ||
+ size < static_cast<int>(block_size)) {
+ errno = ENOMEM;
+ return -1;
+ }
+ _rbuf_data[_rq_received] = data;
+ if (DoPostRecv(data, block_size) < 0) {
+ return -1;
+ }
+ } else {
+ if (_rbuf_data[_rq_received] == nullptr) {
+ _rbuf[_rq_received].clear();
+ butil::IOBufAsZeroCopyOutputStream zcos(
+ &_rbuf[_rq_received],
+ block_size + IOBUF_BLOCK_HEADER_LEN);
+ void* data = nullptr;
+ int size = 0;
+ if (!zcos.Next(&data, &size) || !data ||
+ size < static_cast<int>(block_size)) {
+ errno = ENOMEM;
+ return -1;
+ }
+ _rbuf_data[_rq_received] = data;
+ }
+ if (DoPostRecv(_rbuf_data[_rq_received], block_size) < 0) {
+ return -1;
+ }
+ }
+ _rq_received = (_rq_received + 1) % _rq_size;
+ }
+ return 0;
+}
+
+int UrmaEndpoint::SendImm(uint32_t imm) {
+ if (imm == 0) {
+ return 0;
+ }
+ if (!_resource || !_resource->jetty || !_resource->remote_jetty) {
+ errno = ENOTCONN;
+ return -1;
+ }
+ if (_sq_imm_window_size == 0) {
+ errno = EAGAIN;
+ return -1;
+ }
+ // Empty-payload SEND_IMM flushes peer-side receive credit. Connection
+ // lifetime is owned by the TCP fd, so this is not an EOF marker.
+ urma_jfs_wr_t wr{};
+ std::memset(&wr, 0, sizeof(wr));
+ wr.opcode = URMA_OPC_SEND_IMM;
+ wr.flag.bs.complete_enable = 1;
+ wr.flag.bs.solicited_enable = 1;
+ wr.tjetty = _resource->remote_jetty;
+ wr.send.imm_data = imm;
+ wr.user_ctx = 0; // 0 == pure ack (HandleCompletion reuses budget).
+ urma_jfs_wr_t* bad = nullptr;
+ // Reserve the ACK-only SQ slot before posting for the same reason as the
+ // data windows in CutFromIOBufList: polling may observe its completion as
+ // soon as the provider accepts the WR.
+ --_sq_imm_window_size;
+ const urma_status_t status =
+ urma_post_jetty_send_wr(_resource->jetty, &wr, &bad);
+ if (status != URMA_SUCCESS) {
+ const int provider_errno = errno;
+ ++_sq_imm_window_size;
+ _new_rq_wrs.fetch_add(imm, butil::memory_order_relaxed);
+ LOG(WARNING) << "Failed to post URMA credit ACK: status=" << status
+ << " provider_errno=" << provider_errno
+ << " (" << berror(provider_errno) << ')'
+ << " bad_wr=" << static_cast<const void*>(bad)
+ << " bad_is_current=" << (bad == &wr)
+ << " imm=" << imm
+ << " local_jetty_id="
+ << _resource->jetty->jetty_id.id
+ << " remote_jetty_id="
+ << _resource->remote_jetty->id.id
+ << " state=" << GetStateStr()
+ << " on " << _socket->description();
+ errno = status;
+ return -1;
+ }
+ return 0;
+}
+
+int UrmaEndpoint::SendAck(int num) {
+ const uint16_t old =
+ _new_rq_wrs.fetch_add(num, butil::memory_order_relaxed);
+ if (old + num > _remote_window_capacity / 2 &&
+ _sq_imm_window_size > 0) {
+ return SendImm(_new_rq_wrs.exchange(0, butil::memory_order_relaxed));
+ }
+ return 0;
+}
+
+ssize_t UrmaEndpoint::HandleCompletion(const urma_cr_t& cr) {
+ bool zerocopy = FLAGS_urma_recv_zerocopy;
+ if (cr.status != URMA_CR_SUCCESS) {
+ LOG(WARNING) << "URMA completion failed, status=" << cr.status;
+ errno = EIO;
+ return -1;
+ }
+ if (cr.flag.bs.s_r == 0) {
+ // Send completion: reclaim SQ window and wake the writer.
+ if (cr.user_ctx == 0) {
+ // Pure-ack WR: just replenish the imm budget.
+ if (_sq_imm_window_size >= RESERVED_WR_NUM) {
+ LOG(WARNING)
+ << "URMA credit-ACK completion exceeds reserved SQ "
+ "window: current="
+ << _sq_imm_window_size
+ << " capacity=" << RESERVED_WR_NUM
+ << " on " << _socket->description();
+ errno = EPROTO;
+ return -1;
+ }
+ _sq_imm_window_size += 1;
+ SendAck(0);
+ return 0;
+ }
+ uint16_t wnd = 1; // We signal every WR (complete_enable=1).
+ uint16_t old =
+ _sq_window_size.load(butil::memory_order_relaxed);
+ while (true) {
+ if (old >= _local_window_capacity) {
+ LOG(WARNING)
+ << "URMA send completion exceeds SQ window: old=" << old
+ << " increment=" << wnd
+ << " capacity=" << _local_window_capacity
+ << " user_ctx=" << cr.user_ctx
+ << " on " << _socket->description();
+ errno = EPROTO;
+ return -1;
+ }
+ if (_sq_window_size.compare_exchange_weak(
+ old, static_cast<uint16_t>(old + wnd),
+ butil::memory_order_relaxed)) {
+ break;
+ }
+ }
+ for (uint16_t i = 0; i < wnd; ++i) {
+ _sbuf[_sq_sent].clear();
+ _sq_sent = (_sq_sent + 1) % (_sq_size - RESERVED_WR_NUM);
+ }
+ butil::subtle::MemoryBarrier();
+ if (_remote_rq_window_size.load(butil::memory_order_relaxed) >=
+ _local_window_capacity / 8) {
+ _socket->WakeAsEpollOut();
+ }
+ return 0;
+ }
+ // Recv completion.
+ if (cr.opcode == URMA_CR_OPC_SEND_WITH_IMM && cr.imm_data > 0) {
+ if (cr.imm_data > _local_window_capacity) {
+ LOG(WARNING) << "Invalid URMA receive credit: " << cr.imm_data;
+ errno = EPROTO;
+ return -1;
+ }
+ const uint16_t acks = static_cast<uint16_t>(cr.imm_data);
+ uint16_t old =
+ _remote_rq_window_size.load(butil::memory_order_relaxed);
+ while (true) {
+ if (old > _local_window_capacity - acks) {
+ LOG(WARNING)
+ << "URMA receive credit exceeds window: old=" << old
+ << " credit=" << acks
+ << " capacity=" << _local_window_capacity
+ << " imm=" << cr.imm_data
+ << " remote_window_capacity="
+ << _remote_window_capacity
+ << " on " << _socket->description();
+ errno = EPROTO;
+ return -1;
+ }
+ if (_remote_rq_window_size.compare_exchange_weak(
+ old, static_cast<uint16_t>(old + acks),
+ butil::memory_order_relaxed)) {
+ break;
+ }
+ }
+ if (_sq_window_size.load(butil::memory_order_relaxed) > 0) {
+ _socket->WakeAsEpollOut();
+ }
+ } else if (cr.completion_len == 0) {
+ LOG(WARNING) << "Zero-length URMA receive without immediate credit";
+ errno = EPROTO;
+ return -1;
+ }
+ if (cr.completion_len > GetUrmaRecvBlockSize()) {
+ LOG(WARNING) << "URMA completion exceeds receive buffer: "
+ << cr.completion_len;
+ errno = EPROTO;
+ return -1;
+ }
+ if (cr.completion_len < static_cast<uint32_t>(FLAGS_urma_zerocopy_min_size)) {
+ zerocopy = false;
+ }
+ if (zerocopy) {
+ _rbuf[_rq_received].cutn(&_socket->_read_buf, cr.completion_len);
+ } else {
+ _socket->_read_buf.append(_rbuf_data[_rq_received], cr.completion_len);
+ }
+ if (PostRecv(1, zerocopy) < 0) {
+ return -1;
+ }
+ if (cr.completion_len > 0) {
+ SendAck(1);
+ }
+ return static_cast<ssize_t>(cr.completion_len);
+}
+
+void UrmaEndpoint::DispatchReceivedBytes(SocketUniquePtr& s, ssize_t bytes) {
+ int64_t pending = _pending_received_bytes.load(butil::memory_order_relaxed);
+ if (bytes > 0) {
+ pending = _pending_received_bytes.fetch_add(
+ bytes, butil::memory_order_acq_rel) + bytes;
+ }
+
+ const State state = _state.load(butil::memory_order_acquire);
+ if (state != ESTABLISHED) {
+ return;
+ }
+
+ // PollCq and the handshake bthread can both reach this method when the
+ // state changes to ESTABLISHED. Serialize them so each byte added to
+ // _socket->_read_buf is reported to InputMessenger exactly once.
+ std::unique_lock<butil::Mutex> dispatch_lock(_dispatch_mutex);
+ if (_state.load(butil::memory_order_acquire) != ESTABLISHED) {
+ return;
+ }
+ pending = _pending_received_bytes.exchange(
+ 0, butil::memory_order_acq_rel);
+ if (pending <= 0 || s->Failed()) {
+ return;
+ }
+
+ auto* messenger = static_cast<InputMessenger*>(s->user());
+ if (!messenger) {
+ LOG(ERROR) << "URMA socket has no InputMessenger: "
+ << s->description();
+ return;
+ }
+
+ const int64_t received_us = butil::cpuwide_time_us();
+ const int64_t base_realtime = butil::gettimeofday_us() - received_us;
+ InputMessageClosure last_msg;
+ messenger->ProcessNewMessage(s.get(), static_cast<ssize_t>(pending),
+ false, received_us, base_realtime, last_msg);
+}
+
+void UrmaEndpoint::PollCq(Socket* m) {
+ auto* ep = static_cast<UrmaEndpoint*>(m->user());
+ if (!ep || !ep->_resource || !ep->_resource->jfc) {
+ return;
+ }
+ SocketUniquePtr s;
+ if (Socket::Address(ep->_socket->id(), &s) != 0) {
+ return;
+ }
+ if (s->Failed()) {
+ return;
+ }
+
+ const bool event_mode = !FLAGS_urma_use_polling;
+ int progress = Socket::PROGRESS_INIT;
+ while (true) {
+ urma_jfc_t* event_jfc = nullptr;
+ if (event_mode) {
+ const int event_count = ep->WaitCqEvent(s, &event_jfc);
+ if (event_count < 0) {
+ return;
+ }
+ if (event_count == 0) {
+ if (!m->MoreReadEvents(&progress)) {
+ return;
+ }
+ continue;
+ }
+ }
+
+ ssize_t bytes = 0;
+ auto drain_cq = [&]() -> int {
+ while (true) {
+ const int n =
+ std::max(1, std::min<int>(FLAGS_urma_cqe_poll_once, 32));
+ urma_cr_t crs[32];
+ const int cnt =
+ urma_poll_jfc(ep->_resource->jfc, n, crs);
+ if (cnt < 0) {
+ return EIO;
+ }
+ if (cnt == 0) {
+ return 0;
+ }
+ for (int i = 0; i < cnt; ++i) {
+ if (s->Failed()) {
+ return ECANCELED;
+ }
+ const ssize_t nr = ep->HandleCompletion(crs[i]);
+ if (nr < 0) {
+ return errno ? errno : EIO;
+ }
+ bytes += nr;
+ }
+ }
+ };
+
+ int completion_error = drain_cq();
Review Comment:
Should it be activated only in polling mode, not in event_mode?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3871826836
##########
docs/cn/urma.md:
##########
@@ -0,0 +1,149 @@
+# UrmaTransport:基于 URMA 的远程内存 RPC
+
+UrmaTransport 是使用 openEuler
+[URMA](https://atomgit.com/openeuler/umdk)(Unified Remote Memory Access)SDK
+实现的传输层。它是
+[#3217](https://github.com/apache/brpc/discussions/3217) 中提出的路线 B,
+与基于 OBMM 的 UBRing 传输(路线 A,
+[#3226](https://github.com/apache/brpc/issues/3226))互补,承担大包/跨节点
+高吞吐场景,共同构成路线 C(双后端)。
+
+## 技术背景
+
+URMA 在 UMDK 支持的设备上提供 verbs 风格接口。当前实现创建可靠消息
+(`URMA_TM_RM`)Jetty,并使用 CTP 传输路径;通过
+`urma_post_jetty_send_wr` 提交发送 WR,通过 `urma_post_jfr_wr` 提交接收
+WR。完成事件既可由 JFC 忙轮询获取,也可通过 JFCE 事件 fd 获取。
+
+## 编译配置
+
+### CMake 编译
+
+```bash
+# 带 URMA 支持编译 brpc
+cmake -B build -DWITH_URMA=ON
+make -C build -j$(nproc)
+
+# 编译 urma_performance 示例
+cd example/urma_performance
+cmake -B build
+make -C build -j$(nproc)
+```
+
+`WITH_URMA=ON` 使用上游 UMDK 头文件进行编译。CMake 优先使用系统安装的
+SDK;找不到头文件时,会参照 Mooncake 的 mock 构建方式下载固定版本的
+UMDK,可通过 `DOWNLOAD_URMA_HEADERS=OFF` 禁止下载。找到 `liburma` 时使用
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3952292767
##########
src/brpc/urma/urma_helper.cpp:
##########
@@ -0,0 +1,787 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_helper.h"
+
+#if BRPC_WITH_URMA
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <atomic>
+#include <new>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/containers/flat_map.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/scoped_lock.h"
+#include "butil/synchronization/lock.h"
+
+#include "urma_api.h"
+#include "urma_types.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_endpoint.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace butil {
+namespace iobuf {
+// declared in iobuf.cpp
+extern void* (*blockmem_allocate)(size_t);
+extern void (*blockmem_deallocate)(void*);
+}
+}
+
+namespace brpc {
+namespace urma {
+
+DEFINE_bool(urma_use_polling, false,
+ "Use busy polling to poll JFC, instead of event mode");
+DEFINE_int32(urma_poller_num, 1,
+ "Number of poller bthreads per bthread tag (polling mode only)");
+DEFINE_bool(urma_disable_bthread, false,
+ "Run the message-processing callback inline (no bthread spawned)");
+
+DEFINE_int32(urma_sq_size, 128,
+ "Depth of the local send jetty (JFS). [16, 4096]");
+DEFINE_int32(urma_rq_size, 128,
+ "Depth of the local recv jetty (JFR). [16, 4096]");
+DEFINE_int32(urma_cqe_poll_once, 32,
+ "Max completion entries polled per urma_poll_jfc call");
+DEFINE_bool(urma_recv_zerocopy, true,
+ "Use zero-copy for receives larger than --urma_zerocopy_min_size");
+DEFINE_int32(urma_zerocopy_min_size, 512,
+ "Receives smaller than this many bytes are copied (not zero-copy)");
+
+DEFINE_string(urma_device, "",
+ "The name of the URMA device to use. Empty means the first one.");
+DEFINE_int32(urma_max_sge, 0,
+ "Max SGEs per WR. 0 means the device maximum.");
+DEFINE_int32(urma_bonding_mode, 0,
+ "Bonding mode for bonding devices: 0=standalone, "
+ "1=active-backup, 2=balance.");
+DEFINE_int32(urma_bonding_level, 0,
+ "Bonding level for bonding devices: 0=IODIE, 1=port.");
+DEFINE_int32(urma_prepared_jetty_cnt, 8,
+ "Requested number of pre-allocated Jetty+CQ sets for fast "
+ "connect; capped automatically according to RLIMIT_NOFILE");
+
+DEFINE_int32(urma_buffer_size, 8 * 1024,
+ "Per-buffer size in the URMA buffer pool (bytes). "
+ "Must match IOBuf block size to keep zero-copy working.");
+DEFINE_int32(urma_buffer_count, 65536,
+ "Number of buffers in the URMA buffer pool.");
+
+DEFINE_bool(urma_poller_yield, false,
+ "Yield (bthread_yield) in the busy poll loop to let other "
+ "bthreads run");
+
+
+// Set to true to skip real URMA hardware initialization (unit tests). When
+// true, GlobalUrmaInitializeOrDie() returns without touching liburma and the
+// endpoint builds its state machine without posting real WRs.
+bool g_skip_urma_init = false;
+butil::atomic<bool> g_urma_available(false);
+
+// ============================================================================
+// Global URMA state (single device / single context chosen at init time).
+// ============================================================================
+
+static urma_device_t* g_device = nullptr;
+static urma_context_t* g_context = nullptr;
+static urma_eid_t g_local_eid{};
+static bool g_has_local_eid = false;
+static urma_device_attr_t g_device_attr{};
+static int g_max_sge = 1;
+static size_t g_recv_block_size = 8 * 1024;
+static bool g_is_bonding_device = false;
+// Prefer the device capability table and retain priority 6 as a compatibility
+// fallback for CTP providers that do not report a priority.
+static uint8_t g_jetty_priority = 6;
+// urma_init/urma_uninit manage process-global liburma state. Only uninitialize
+// it when this helper performed the successful initialization; URMA_EEXIST
+// means another component owns that state.
+static bool g_owns_urma_init = false;
+
+// The single registered segment backing the buffer pool. The whole pool is
+// one urma_register_seg call, sliced into fixed-size buffers. urma_target_seg_t
+// is the per-buffer handle carried by urma_sge_t.tseg on the send/recv path.
+static urma_target_seg_t* g_pool_seg = nullptr;
+static void* g_pool_base = nullptr;
+static size_t g_pool_size = 0;
+static size_t g_pool_buffer_size = 0;
+
+// User-registered segments (RegisterMemoryForUrma). Keyed by buffer address.
+struct UserSeg {
+ urma_target_seg_t* tseg = nullptr;
+ void* base = nullptr;
+ size_t len = 0;
+};
+static butil::FlatMap<void*, UserSeg>* g_user_segs = nullptr;
+static butil::Mutex* g_user_segs_lock = nullptr;
+
+// Original IOBuf allocator (saved so we can restore it on release).
+static void* (*g_mem_alloc_orig)(size_t) = nullptr;
+static void (*g_mem_dealloc_orig)(void*) = nullptr;
+static size_t g_default_block_size_orig = 0;
+
+namespace {
+
+// Round up to the page size.
+size_t PageSize() {
+ long ps = sysconf(_SC_PAGESIZE);
+ return ps > 0 ? static_cast<size_t>(ps) : 4096;
+}
+
+size_t AlignUp(size_t v, size_t align) {
+ return (v + align - 1) / align * align;
+}
+
+bool IsBondingDeviceName(const char* name) {
+ return name != nullptr && strncmp(name, "bonding", 7) == 0;
+}
+
+union urma_tp_type_en TpTypeCapability(urma_tp_type_t tp_type) {
+ union urma_tp_type_en capability{};
+ switch (tp_type) {
+ case URMA_RTP:
+ capability.bs.rtp = 1;
+ break;
+ case URMA_CTP:
+ capability.bs.ctp = 1;
+ break;
+ case URMA_UTP:
+ capability.bs.utp = 1;
+ break;
+ }
+ return capability;
+}
+
+bool ConfigureBondingMode(const std::string& device_name) {
+ const char* context_device_name =
+ g_context != nullptr && g_context->dev != nullptr
+ ? g_context->dev->name
+ : nullptr;
+ g_is_bonding_device =
+ IsBondingDeviceName(device_name.c_str()) ||
+ IsBondingDeviceName(context_device_name);
+ if (!g_is_bonding_device) {
+ return true;
+ }
+
+#if BRPC_URMA_HAS_BONDING_EXT
+ if (FLAGS_urma_bonding_mode < 0 ||
+ FLAGS_urma_bonding_mode >= BONDP_BONDING_MODE_MAX ||
+ FLAGS_urma_bonding_level < 0 ||
+ FLAGS_urma_bonding_level >= BONDP_BONDING_LEVEL_MAX) {
+ LOG(ERROR) << "Invalid URMA bonding configuration: mode="
+ << FLAGS_urma_bonding_mode
+ << " level=" << FLAGS_urma_bonding_level;
+ errno = EINVAL;
+ return false;
+ }
+
+ bondp_set_bonding_mode_in_t bond_in{};
+ bond_in.bonding_mode =
+ static_cast<bondp_bonding_mode_t>(FLAGS_urma_bonding_mode);
+ bond_in.bonding_level =
+ static_cast<bondp_bonding_level_t>(FLAGS_urma_bonding_level);
+
+ urma_user_ctl_in_t ctl_in{};
+ ctl_in.addr = reinterpret_cast<uint64_t>(&bond_in);
+ ctl_in.len = static_cast<uint32_t>(sizeof(bond_in));
+ ctl_in.opcode = BONDP_USER_CTL_SET_BONDING_MODE;
+ urma_user_ctl_out_t ctl_out{};
+ const urma_status_t status = urma_user_ctl(g_context, &ctl_in, &ctl_out);
+ if (status != URMA_SUCCESS) {
+ LOG(ERROR) << "urma_user_ctl(SET_BONDING_MODE) failed: status="
+ << status << " device=" << device_name
+ << " mode=" << FLAGS_urma_bonding_mode
+ << " level=" << FLAGS_urma_bonding_level
+ << ". It must run before segment/JFC/JFR creation";
+ errno = status > 0 ? status : EIO;
+ return false;
+ }
+ return true;
+#else
+ LOG(ERROR) << "URMA bonding device " << device_name
+ << " requires provider header urma_ubagg.h";
+ errno = ENOTSUP;
+ return false;
+#endif
+}
+
+} // namespace
+
+// ============================================================================
+// Buffer pool: one registered segment, sliced into fixed-size buffers.
+// ============================================================================
+
+namespace {
+
+// Shard the free list to reduce contention between allocator threads.
+constexpr size_t kShardCount = 64;
+struct BufferPool {
+ butil::Mutex mutexes[kShardCount];
+ std::vector<void*> free_lists[kShardCount];
+ std::vector<char> in_use; // 0/1 per buffer
+ butil::atomic<size_t> outstanding{0};
+
+ size_t buffer_count() const {
+ return in_use.size();
+ }
+};
+BufferPool* g_pool = nullptr;
+
+size_t ShardFor(void* buf) {
+ auto* base = static_cast<char*>(buf);
+ auto offset = static_cast<size_t>(base - static_cast<char*>(g_pool_base));
+ auto idx = offset / g_pool_buffer_size;
+ return idx % kShardCount;
+}
+
+size_t PreferredShard() {
+ // Hash the current thread id across shards. pthread_self() returns an
+ // opaque pthread_t; cast through uintptr_t to get a hashable value.
+ auto tid = static_cast<uintptr_t>(reinterpret_cast<uintptr_t>(pthread_self()));
+ return tid % kShardCount;
Review Comment:
PreferredShard() uses reinterpret_cast<uintptr_t>(pthread_self()), which is not a valid cast on common Linux libpthread where pthread_t is an integral type (this will not compile). For Linux builds, a simple static_cast to uintptr_t is sufficient for sharding.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5439989543
@wwbmmm @chenBright @dwh110
I have addressed the main review feedback:
- Added Bazel support for disabling UMDK header downloads.
- Added explicit mock controls for CMake, Make, and Bazel.
- Ensured real liburma builds exclude the mock implementation.
- Updated the documentation and pinned the UMDK dependency.
- Verified the Bazel build and all 61 test targets.
Please take another look when available. Kindly manually verify any potential issue before posting a review comment. please feel free ping me anytime.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5727709021
@wwbmmm @chenBright We would like to merge #3428 first so we can continue adapting the URMA handshake to the common handshake framework in #3535. If there are no further blocking concerns with the current version, I’ll go ahead and merge #3428.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Gzure (via GitHub)" <gi...@apache.org>.
Gzure commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r4023678416
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+ _input_processor.Init(s, InputMessengerProcessor::STREAM_URMA_JETTY);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _input_processor.Reset();
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
+ if (nr == 0) {
+ errno = EEOF;
+ return -1;
+ }
+ received += nr;
+ }
+ return 0;
+}
+
+void UrmaEndpoint::PushBackToReadBuf(const void* data, size_t len) {
+ _socket->fd_input_processor().read_buf().append(data, len);
+}
+
+int UrmaEndpoint::WriteToFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t written = 0;
+ while (written < len) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = write(fd, p + written, len - written);
+ if (nw >= 0) {
+ written += nw;
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+// ============================================================================
+// Hello builders / parsers.
+// ============================================================================
+
+void UrmaEndpoint::MakeLocalParsedHello(ParsedHello* out) const {
+ *out = ParsedHello{}; // value-initialize (avoids memset on non-trivial type)
+ out->buffer_size = static_cast<uint32_t>(GetUrmaRecvBlockSize());
+ out->recv_buffer_cnt = _rq_size - 1;
+ if (_resource && _resource->jetty) {
+ out->jetty_id = _resource->jetty->jetty_id.id;
+ out->uasid = _resource->jetty->jetty_id.uasid;
+ const urma_eid_t* local_eid = GetUrmaLocalEid();
+ const uint8_t* advertised_eid =
+ local_eid != nullptr
+ ? local_eid->raw
+ : _resource->jetty->jetty_id.eid.raw;
+ std::memcpy(out->eid, advertised_eid, 16);
+ }
+ out->tp_type = static_cast<uint8_t>(URMA_CTP);
+ // Pool segment: flatten g_pool_seg's seg fields.
+ urma_target_seg_t* pool = GetPoolSegFor(nullptr);
+ if (pool) {
+ std::memcpy(out->seg_eid, pool->seg.ubva.eid.raw, 16);
+ out->seg_uasid = pool->seg.ubva.uasid;
+ out->seg_va = pool->seg.ubva.va;
+ out->seg_len = pool->seg.len;
+ out->seg_token_id = pool->seg.token_id;
+ }
+}
+
+void UrmaEndpoint::FillLocalHelloV2(v2_wire::HelloMessage* out) const {
Review Comment:
> I don't think it's necessary to support v2 for the new transports.
I have deleted the urma v2 related code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3964067186
##########
src/brpc/urma/urma_helper.cpp:
##########
@@ -0,0 +1,787 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_helper.h"
+
+#if BRPC_WITH_URMA
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <atomic>
+#include <functional>
+#include <new>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/containers/flat_map.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/scoped_lock.h"
+#include "butil/synchronization/lock.h"
+
+#include "urma_api.h"
+#include "urma_types.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_endpoint.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace butil {
+namespace iobuf {
+// declared in iobuf.cpp
+extern void* (*blockmem_allocate)(size_t);
+extern void (*blockmem_deallocate)(void*);
+}
+}
+
+namespace brpc {
+namespace urma {
+
+DEFINE_bool(urma_use_polling, false,
+ "Use busy polling to poll JFC, instead of event mode");
+DEFINE_int32(urma_poller_num, 1,
+ "Number of poller bthreads per bthread tag (polling mode only)");
+DEFINE_bool(urma_disable_bthread, false,
+ "Run the message-processing callback inline (no bthread spawned)");
+
+DEFINE_int32(urma_sq_size, 128,
+ "Depth of the local send jetty (JFS). [16, 4096]");
+DEFINE_int32(urma_rq_size, 128,
+ "Depth of the local recv jetty (JFR). [16, 4096]");
+DEFINE_int32(urma_cqe_poll_once, 32,
+ "Max completion entries polled per urma_poll_jfc call");
+DEFINE_bool(urma_recv_zerocopy, true,
+ "Use zero-copy for receives larger than --urma_zerocopy_min_size");
+DEFINE_int32(urma_zerocopy_min_size, 512,
+ "Receives smaller than this many bytes are copied (not zero-copy)");
+
+DEFINE_string(urma_device, "",
+ "The name of the URMA device to use. Empty means the first one.");
+DEFINE_int32(urma_max_sge, 0,
+ "Max SGEs per WR. 0 means the device maximum.");
+DEFINE_int32(urma_bonding_mode, 0,
+ "Bonding mode for bonding devices: 0=standalone, "
+ "1=active-backup, 2=balance.");
+DEFINE_int32(urma_bonding_level, 0,
+ "Bonding level for bonding devices: 0=IODIE, 1=port.");
+DEFINE_int32(urma_prepared_jetty_cnt, 8,
+ "Requested number of pre-allocated Jetty+CQ sets for fast "
+ "connect; capped automatically according to RLIMIT_NOFILE");
+
+DEFINE_int32(urma_buffer_size, 8 * 1024,
+ "Per-buffer size in the URMA buffer pool (bytes). "
+ "Must match IOBuf block size to keep zero-copy working.");
+DEFINE_int32(urma_buffer_count, 65536,
+ "Number of buffers in the URMA buffer pool.");
+
+DEFINE_bool(urma_poller_yield, false,
+ "Yield (bthread_yield) in the busy poll loop to let other "
+ "bthreads run");
+
+
+// Set to true to skip real URMA hardware initialization (unit tests). When
+// true, GlobalUrmaInitializeOrDie() returns without touching liburma and the
+// endpoint builds its state machine without posting real WRs.
+bool g_skip_urma_init = false;
+butil::atomic<bool> g_urma_available(false);
+
+// ============================================================================
+// Global URMA state (single device / single context chosen at init time).
+// ============================================================================
+
+static urma_device_t* g_device = nullptr;
+static urma_context_t* g_context = nullptr;
+static urma_eid_t g_local_eid{};
+static bool g_has_local_eid = false;
+static urma_device_attr_t g_device_attr{};
+static int g_max_sge = 1;
+static size_t g_recv_block_size = 8 * 1024;
+static bool g_is_bonding_device = false;
+// Prefer the device capability table and retain priority 6 as a compatibility
+// fallback for CTP providers that do not report a priority.
+static uint8_t g_jetty_priority = 6;
+// urma_init/urma_uninit manage process-global liburma state. Only uninitialize
+// it when this helper performed the successful initialization; URMA_EEXIST
+// means another component owns that state.
+static bool g_owns_urma_init = false;
+
+// The single registered segment backing the buffer pool. The whole pool is
+// one urma_register_seg call, sliced into fixed-size buffers. urma_target_seg_t
+// is the per-buffer handle carried by urma_sge_t.tseg on the send/recv path.
+static urma_target_seg_t* g_pool_seg = nullptr;
+static void* g_pool_base = nullptr;
+static size_t g_pool_size = 0;
+static size_t g_pool_buffer_size = 0;
+
+// User-registered segments (RegisterMemoryForUrma). Keyed by buffer address.
+struct UserSeg {
+ urma_target_seg_t* tseg = nullptr;
+ void* base = nullptr;
+ size_t len = 0;
+};
+static butil::FlatMap<void*, UserSeg>* g_user_segs = nullptr;
+static butil::Mutex* g_user_segs_lock = nullptr;
+
+// Original IOBuf allocator (saved so we can restore it on release).
+static void* (*g_mem_alloc_orig)(size_t) = nullptr;
+static void (*g_mem_dealloc_orig)(void*) = nullptr;
+static size_t g_default_block_size_orig = 0;
+
+namespace {
+
+// Round up to the page size.
+size_t PageSize() {
+ long ps = sysconf(_SC_PAGESIZE);
+ return ps > 0 ? static_cast<size_t>(ps) : 4096;
+}
+
+size_t AlignUp(size_t v, size_t align) {
+ return (v + align - 1) / align * align;
+}
+
+bool IsBondingDeviceName(const char* name) {
+ return name != nullptr && strncmp(name, "bonding", 7) == 0;
+}
+
+union urma_tp_type_en TpTypeCapability(urma_tp_type_t tp_type) {
+ union urma_tp_type_en capability{};
+ switch (tp_type) {
+ case URMA_RTP:
+ capability.bs.rtp = 1;
+ break;
+ case URMA_CTP:
+ capability.bs.ctp = 1;
+ break;
+ case URMA_UTP:
+ capability.bs.utp = 1;
+ break;
+ }
+ return capability;
+}
+
+bool ConfigureBondingMode(const std::string& device_name) {
+ const char* context_device_name =
+ g_context != nullptr && g_context->dev != nullptr
+ ? g_context->dev->name
+ : nullptr;
+ g_is_bonding_device =
+ IsBondingDeviceName(device_name.c_str()) ||
+ IsBondingDeviceName(context_device_name);
+ if (!g_is_bonding_device) {
+ return true;
+ }
+
+#if BRPC_URMA_HAS_BONDING_EXT
+ if (FLAGS_urma_bonding_mode < 0 ||
+ FLAGS_urma_bonding_mode >= BONDP_BONDING_MODE_MAX ||
+ FLAGS_urma_bonding_level < 0 ||
+ FLAGS_urma_bonding_level >= BONDP_BONDING_LEVEL_MAX) {
+ LOG(ERROR) << "Invalid URMA bonding configuration: mode="
+ << FLAGS_urma_bonding_mode
+ << " level=" << FLAGS_urma_bonding_level;
+ errno = EINVAL;
+ return false;
+ }
+
+ bondp_set_bonding_mode_in_t bond_in{};
+ bond_in.bonding_mode =
+ static_cast<bondp_bonding_mode_t>(FLAGS_urma_bonding_mode);
+ bond_in.bonding_level =
+ static_cast<bondp_bonding_level_t>(FLAGS_urma_bonding_level);
+
+ urma_user_ctl_in_t ctl_in{};
+ ctl_in.addr = reinterpret_cast<uint64_t>(&bond_in);
+ ctl_in.len = static_cast<uint32_t>(sizeof(bond_in));
+ ctl_in.opcode = BONDP_USER_CTL_SET_BONDING_MODE;
+ urma_user_ctl_out_t ctl_out{};
+ const urma_status_t status = urma_user_ctl(g_context, &ctl_in, &ctl_out);
+ if (status != URMA_SUCCESS) {
+ LOG(ERROR) << "urma_user_ctl(SET_BONDING_MODE) failed: status="
+ << status << " device=" << device_name
+ << " mode=" << FLAGS_urma_bonding_mode
+ << " level=" << FLAGS_urma_bonding_level
+ << ". It must run before segment/JFC/JFR creation";
+ errno = status > 0 ? status : EIO;
+ return false;
+ }
+ return true;
+#else
+ LOG(ERROR) << "URMA bonding device " << device_name
+ << " requires provider header urma_ubagg.h";
+ errno = ENOTSUP;
+ return false;
+#endif
+}
+
+} // namespace
+
+// ============================================================================
+// Buffer pool: one registered segment, sliced into fixed-size buffers.
+// ============================================================================
+
+namespace {
+
+// Shard the free list to reduce contention between allocator threads.
+constexpr size_t kShardCount = 64;
+struct BufferPool {
+ butil::Mutex mutexes[kShardCount];
+ std::vector<void*> free_lists[kShardCount];
+ std::vector<char> in_use; // 0/1 per buffer
+ butil::atomic<size_t> outstanding{0};
+
+ size_t buffer_count() const {
+ return in_use.size();
+ }
+};
+BufferPool* g_pool = nullptr;
+
+size_t ShardFor(void* buf) {
+ auto* base = static_cast<char*>(buf);
+ auto offset = static_cast<size_t>(base - static_cast<char*>(g_pool_base));
+ auto idx = offset / g_pool_buffer_size;
+ return idx % kShardCount;
+}
+
+size_t PreferredShard() {
+ // pthread_t is an integer on Linux and a pointer on macOS. Hash its native
+ // representation instead of assuming either form with a cast.
+ return std::hash<pthread_t>()(pthread_self()) % kShardCount;
+}
+
+void* PoolAllocate(size_t size) {
+ if (BAIDU_UNLIKELY(g_skip_urma_init)) {
+ return g_mem_alloc_orig ? g_mem_alloc_orig(size) : malloc(size);
+ }
+ // Only serve the configured buffer size; callers always ask for that.
+ if (size > g_pool_buffer_size) {
+ // Larger than a single buffer -- fall back to the system allocator.
+ return g_mem_alloc_orig ? g_mem_alloc_orig(size) : malloc(size);
+ }
+ auto start = PreferredShard();
+ for (size_t i = 0; i < kShardCount; ++i) {
+ auto shard = (start + i) % kShardCount;
+ BAIDU_SCOPED_LOCK(g_pool->mutexes[shard]);
+ auto& fl = g_pool->free_lists[shard];
+ if (fl.empty()) {
+ continue;
+ }
+ void* buf = fl.back();
+ fl.pop_back();
+ auto idx = (static_cast<char*>(buf) -
+ static_cast<char*>(g_pool_base)) / g_pool_buffer_size;
+ if (idx < g_pool->in_use.size()) {
+ g_pool->in_use[idx] = 1;
+ }
+ g_pool->outstanding.fetch_add(1, butil::memory_order_relaxed);
+ return buf;
+ }
+ LOG_EVERY_SECOND(WARNING)
+ << "URMA buffer pool exhausted; falling back to malloc";
+ return g_mem_alloc_orig ? g_mem_alloc_orig(size) : malloc(size);
+}
+
+void PoolDeallocate(void* buf) {
+ if (BAIDU_UNLIKELY(g_skip_urma_init)) {
+ if (g_mem_dealloc_orig) {
+ g_mem_dealloc_orig(buf);
+ } else {
+ free(buf);
+ }
+ return;
+ }
+ auto* base = static_cast<char*>(g_pool_base);
+ auto* p = static_cast<char*>(buf);
+ if (!base || p < base || p >= base + g_pool_size ||
+ static_cast<size_t>(p - base) % g_pool_buffer_size != 0) {
+ // Not a pool buffer -- hand back to the original allocator.
+ if (g_mem_dealloc_orig) {
+ g_mem_dealloc_orig(buf);
+ } else {
+ free(buf);
+ }
+ return;
+ }
+ auto shard = ShardFor(buf);
+ BAIDU_SCOPED_LOCK(g_pool->mutexes[shard]);
+ auto idx = static_cast<size_t>(p - base) / g_pool_buffer_size;
+ if (idx < g_pool->in_use.size()) {
+ if (!g_pool->in_use[idx]) {
+ LOG(WARNING) << "double-free of URMA pool buffer " << buf;
+ return;
+ }
+ g_pool->in_use[idx] = 0;
+ }
+ g_pool->free_lists[shard].push_back(buf);
+ if (g_pool->outstanding.load(butil::memory_order_relaxed) > 0) {
+ g_pool->outstanding.fetch_sub(1, butil::memory_order_relaxed);
+ }
+}
+
+// Register the pool: mmap one large region and urma_register_seg it.
+bool InitPool() {
+ if (g_pool_buffer_size == 0 || g_pool == nullptr) {
+ return false;
+ }
+ size_t count = g_pool->buffer_count();
+ if (count == 0) {
+ return false;
+ }
+ size_t raw = g_pool_buffer_size * count;
+ size_t page = PageSize();
+ g_pool_size = AlignUp(raw, page);
+
+ g_pool_base = mmap(nullptr, g_pool_size, PROT_READ | PROT_WRITE,
+ MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (g_pool_base == MAP_FAILED) {
+ PLOG(WARNING) << "Fail to mmap URMA buffer pool";
+ g_pool_base = nullptr;
+ return false;
+ }
+
+ urma_reg_seg_flag_t flag{};
+ flag.bs.token_policy = URMA_TOKEN_NONE;
+ flag.bs.cacheable = URMA_NON_CACHEABLE;
+ flag.bs.access = URMA_ACCESS_READ | URMA_ACCESS_WRITE | URMA_ACCESS_ATOMIC;
+
+ urma_seg_cfg_t cfg{};
+ cfg.va = reinterpret_cast<uint64_t>(g_pool_base);
+ cfg.len = g_pool_size;
+ cfg.token_id = nullptr;
+ cfg.token_value = {};
+ cfg.flag = flag;
+ cfg.user_ctx = reinterpret_cast<uint64_t>(g_pool_base);
+ cfg.iova = 0;
+
+ errno = 0;
+ g_pool_seg = urma_register_seg(g_context, &cfg);
+ if (!g_pool_seg) {
+ PLOG(WARNING) << "Fail to urma_register_seg";
+ munmap(g_pool_base, g_pool_size);
+ g_pool_base = nullptr;
+ return false;
+ }
+ g_pool->in_use.assign(count, 0);
+ for (size_t i = 0; i < count; ++i) {
+ auto shard = i % kShardCount;
+ g_pool->free_lists[shard].push_back(
+ static_cast<char*>(g_pool_base) + i * g_pool_buffer_size);
+ }
+ return true;
+}
+
+} // namespace
+
+// Exposed to urma_endpoint.cpp: return the per-buffer target_seg pointer.
+// All pool buffers share the same segment, so we return g_pool_seg for any
+// pool address; the per-WR length selects the slice.
+urma_target_seg_t* GetPoolSegFor(void* buf) {
+ if (g_skip_urma_init || !g_pool_seg || !g_pool_base) {
+ return nullptr;
+ }
+ if (buf == nullptr) {
+ return g_pool_seg;
+ }
+ const uintptr_t base = reinterpret_cast<uintptr_t>(g_pool_base);
+ const uintptr_t p = reinterpret_cast<uintptr_t>(buf);
+ if (p >= base && p - base < g_pool_size) {
+ return g_pool_seg;
+ }
+ return nullptr;
+}
+
+static void GlobalRelease() {
+ g_urma_available.store(false, butil::memory_order_release);
+ if (g_mem_alloc_orig) {
+ butil::iobuf::blockmem_allocate = g_mem_alloc_orig;
+ g_mem_alloc_orig = nullptr;
+ }
+ if (g_mem_dealloc_orig) {
+ butil::iobuf::blockmem_deallocate = g_mem_dealloc_orig;
+ g_mem_dealloc_orig = nullptr;
+ }
+ if (g_default_block_size_orig != 0) {
+ butil::SetDefaultBlockSize(g_default_block_size_orig);
+ g_default_block_size_orig = 0;
+ }
+ UrmaEndpoint::GlobalRelease();
+ if (g_pool_seg) {
+ urma_unregister_seg(g_pool_seg);
+ g_pool_seg = nullptr;
+ }
+ if (g_pool_base) {
+ munmap(g_pool_base, g_pool_size);
+ g_pool_base = nullptr;
+ g_pool_size = 0;
+ }
+ delete g_pool;
+ g_pool = nullptr;
+ delete g_user_segs;
+ g_user_segs = nullptr;
+ delete g_user_segs_lock;
+ g_user_segs_lock = nullptr;
+ if (g_context) {
+ urma_delete_context(g_context);
+ g_context = nullptr;
+ }
+ g_local_eid = urma_eid_t{};
+ g_has_local_eid = false;
+ g_is_bonding_device = false;
+ g_jetty_priority = 6;
+ if (g_owns_urma_init) {
+ const urma_status_t status = urma_uninit();
+ if (status != URMA_SUCCESS) {
+ LOG(WARNING) << "Fail to urma_uninit: " << status;
+ }
+ g_owns_urma_init = false;
+ }
+ g_device = nullptr;
+}
+
+// ============================================================================
+// Global initialization.
+// ============================================================================
+
+static bool GlobalUrmaInitializeImpl() {
+ if (BAIDU_UNLIKELY(g_skip_urma_init)) {
+ g_urma_available.store(true, butil::memory_order_release);
+ return true;
+ }
+ if (FLAGS_urma_sq_size < 16 || FLAGS_urma_sq_size > 4096 ||
+ FLAGS_urma_rq_size < 16 || FLAGS_urma_rq_size > 4096 ||
+ FLAGS_urma_buffer_size < 1024 || FLAGS_urma_buffer_count <= 0 ||
+ FLAGS_urma_poller_num <= 0) {
+ LOG(ERROR) << "Invalid URMA queue, buffer, or poller configuration";
+ errno = EINVAL;
+ return false;
+ }
+
+ urma_init_attr_t init_attr{};
+ const urma_status_t status = urma_init(&init_attr);
+ if (status != URMA_SUCCESS && status != URMA_EEXIST) {
+ if (status == URMA_FAIL) {
+ LOG(ERROR) << "Fail to urma_init: " << status
+ << " (URMA_FAIL). liburma returns URMA_FAIL when it "
+ "cannot load a provider, or when URMA was already "
+ "initialized by another component. Verify readable "
+ "provider libraries under /usr/lib64/urma, loaded "
+ "URMA kernel drivers, and that urma_init is called "
+ "only once per process";
+ } else {
+ LOG(ERROR) << "Fail to urma_init: " << status;
+ }
+ return false;
+ }
+ g_owns_urma_init = (status == URMA_SUCCESS);
+
+ int num_devices = 0;
+ urma_device_t** devices = urma_get_device_list(&num_devices);
+ if (!devices || num_devices <= 0) {
+ LOG(ERROR) << "No URMA device found";
+ urma_free_device_list(devices);
+ return false;
+ }
+ urma_device_t* found = nullptr;
+ for (int i = 0; i < num_devices; ++i) {
+ if (FLAGS_urma_device.empty() ||
+ std::string(devices[i]->name) == FLAGS_urma_device) {
+ found = devices[i];
+ break;
+ }
+ }
+ if (!found) {
+ LOG(ERROR) << "URMA device not found: " << FLAGS_urma_device;
+ urma_free_device_list(devices);
+ return false;
+ }
+ g_device = found;
+
+ const std::string device_name = found->name;
+ if (urma_query_device(found, &g_device_attr) != URMA_SUCCESS) {
+ LOG(ERROR) << "Fail to urma_query_device";
+ urma_free_device_list(devices);
+ g_device = nullptr;
+ return false;
+ }
+ const int ctp_priority =
+ FindUrmaPriorityForTpType(g_device_attr, URMA_CTP);
+ if (ctp_priority >= 0) {
+ g_jetty_priority = static_cast<uint8_t>(ctp_priority);
+ } else {
+ LOG(WARNING) << "URMA device does not report a CTP priority; "
+ "falling back to compatibility priority "
+ << static_cast<unsigned>(g_jetty_priority);
+ }
+ uint32_t eid_cnt = 0;
+ urma_eid_info_t* eids = urma_get_eid_list(found, &eid_cnt);
+ if (!eids || eid_cnt == 0) {
+ LOG(ERROR) << "Fail to urma_get_eid_list";
+ urma_free_eid_list(eids);
+ urma_free_device_list(devices);
+ g_device = nullptr;
+ return false;
+ }
+ // Retain the exact EID used to create the context and advertise it in the
+ // handshake. This matters for a bonding virtual device whose jetty can
+ // carry a provider-selected physical EID.
+ g_local_eid = eids[0].eid;
+ g_has_local_eid = true;
+ g_context = urma_create_context(found, eids[0].eid_index);
+ urma_free_eid_list(eids);
+ urma_free_device_list(devices);
+ g_device = nullptr;
+ if (!g_context) {
+ LOG(ERROR) << "Fail to urma_create_context";
+ return false;
+ }
+ // The bonding provider only accepts SET_BONDING_MODE while the context
+ // has no dependent resource. This must precede register_seg/JFC/JFR.
+ if (!ConfigureBondingMode(device_name)) {
+ return false;
+ }
+ uint32_t device_max_sge = g_device_attr.dev_cap.max_jfs_sge;
+ if (device_max_sge == 0) {
+ device_max_sge = 1;
+ }
+ // urma_jfs_cfg_t::max_sge is uint8_t.
+ if (device_max_sge > 255) {
+ device_max_sge = 255;
+ }
+ g_max_sge = static_cast<int>(device_max_sge);
+ if (FLAGS_urma_max_sge > 0) {
+ if (FLAGS_urma_max_sge > g_max_sge) {
+ LOG(WARNING) << "Cap urma_max_sge from " << FLAGS_urma_max_sge
+ << " to device/config limit " << g_max_sge;
+ } else {
+ g_max_sge = FLAGS_urma_max_sge;
+ }
+ }
+ g_recv_block_size =
+ static_cast<size_t>(FLAGS_urma_buffer_size) -
+ sizeof(butil::IOBuf::Block);
+
+ // User-segment table.
+ g_user_segs_lock = new (std::nothrow) butil::Mutex;
+ g_user_segs = new (std::nothrow) butil::FlatMap<void*, UserSeg>();
+ if (!g_user_segs_lock || !g_user_segs ||
+ g_user_segs->init(65536) < 0) {
+ LOG(ERROR) << "Fail to init g_user_segs";
+ return false;
+ }
+
+ // Buffer pool.
+ g_pool = new BufferPool();
+ g_pool_buffer_size = static_cast<size_t>(FLAGS_urma_buffer_size);
+ // Resize the pool's per-shard vectors to hold the configured count.
+ size_t count = static_cast<size_t>(FLAGS_urma_buffer_count);
+ g_pool->in_use.assign(count, 0);
+ for (size_t s = 0; s < kShardCount; ++s) {
+ g_pool->free_lists[s].reserve(count / kShardCount + 1);
+ }
+ if (!InitPool()) {
+ LOG(ERROR) << "Fail to init URMA buffer pool";
+ return false;
+ }
+
+ // Hijack IOBuf allocation so every IOBuf block is backed by a registered
+ // segment. This makes the send path trivial: any IOBuf can be posted
+ // directly as an urma_sge_t pointing at g_pool_seg.
+ g_mem_alloc_orig = butil::iobuf::blockmem_allocate;
+ g_mem_dealloc_orig = butil::iobuf::blockmem_deallocate;
+ g_default_block_size_orig = butil::GetDefaultBlockSize();
+ butil::iobuf::blockmem_allocate = PoolAllocate;
+ butil::iobuf::blockmem_deallocate = PoolDeallocate;
+ butil::SetDefaultBlockSize(g_pool_buffer_size);
+
+ if (UrmaEndpoint::GlobalInitialize() != 0) {
+ LOG(ERROR) << "Fail to initialize URMA endpoint resources";
+ return false;
+ }
+
+ g_urma_available.store(true, butil::memory_order_release);
+ // Do not register GlobalRelease with atexit. IOBuf keeps blocks in
+ // thread-local chains whose destructors may run after atexit handlers.
+ // Unmapping the registered pool here would leave those TLS chains
+ // pointing into unmapped memory. The process reclaims global URMA
+ // resources on exit; GlobalRelease remains available for init rollback.
+ LOG(INFO) << "URMA initialized: device=" << device_name
+ << " bonding=" << g_is_bonding_device
+ << " max_sge=" << g_max_sge
+ << " buffer_size=" << g_pool_buffer_size
+ << " buffer_count=" << g_pool->buffer_count();
+ return true;
+}
+
+static butil::atomic<int> g_init_once{0};
+static butil::Mutex g_init_mutex;
+
+void GlobalUrmaInitializeOrDie() {
+ int expected = 0;
+ if (g_init_once.load(butil::memory_order_acquire) == 2) {
+ return;
+ }
+ if (g_init_once.compare_exchange_strong(expected, 1,
+ butil::memory_order_acq_rel)) {
+ BAIDU_SCOPED_LOCK(g_init_mutex);
+ if (!GlobalUrmaInitializeImpl()) {
+ LOG(WARNING) << "URMA initialization failed; falling back to TCP";
+ GlobalRelease();
+ }
+ g_init_once.store(2, butil::memory_order_release);
+ } else {
+ // Wait for the other thread to finish init.
+ while (g_init_once.load(butil::memory_order_acquire) != 2) {
+ // spin briefly
+ }
+ }
Review Comment:
The wait-for-initialization path uses a tight busy-spin loop. If URMA init is slow (device enumeration, mmap/register_seg, etc.), other threads can burn CPU cores until init completes. Consider yielding/sleeping briefly in the loop to reduce contention and avoid impacting latency-sensitive threads.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5461426421
@wwbmmm We plan to handle #3432 in a follow-up PR.
We're currently testing the common handshake approach internally with RDMA and UBShm. For URMA, we'd like to get this PR merged first, since the handshake work will depend on how this PR progresses.
Once this is merged, we plan to move the URMA handshake to the common handshake framework as well.
If there are any other concerns with this PR, feel free to ping me.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5570561459
@wwbmmm Thanks for pointing this out. Addressed in commit 07c4661d.
The Bazel source selection has been restructured:
- All src/brpc/urma/*.cpp files are excluded from the base source glob.
- URMA_SRCS explicitly excludes mock_urma.cpp.
- brpc_with_urma_use_real includes only the non-mock URMA sources and links liburma.
- The regular brpc_with_urma configuration appends mock_urma.cpp exactly once.
- URMA implementation sources are no longer compiled when URMA is disabled.
This now matches the CMake and Make behavior and prevents mock urma_* symbols from conflicting with the real liburma. bazel query successfully loaded and expanded the updated source selection.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5585250485
@wwbmmm Addressed the URMA-related Copilot review findings in fe6ea6f3:
- Route writes through URMA only after negotiation reaches URMA_ON; TCP remains in use while the state is UNKNOWN.
- Replace the non-portable pthread_t reinterpret cast with std::hash<pthread_t>.
- Use GFLAGS_NAMESPACE::FlagSaver in the handshake-version test.
- Adapt UrmaEndpoint to the current per-stream InputMessengerProcessor model: URMA completions use a dedicated STREAM_URMA_JETTY processor, while fallback bytes return to the TCP fd processor.
- Clarify the dedicated transport poller comment for RDMA/UBRING/URMA.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r4013458054
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
Review Comment:
ok
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5594791033
@wwbmmm I just noticed a conflict between the format and master of these two files: `input_messenger.cpp` and `input_messenger_processor.cpp`. Now has been fixed. Please review them again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3951529670
##########
src/brpc/input_messenger.cpp:
##########
@@ -56,294 +54,291 @@ BRPC_VALIDATE_GFLAG(log_connection_close, PassValidate);
DEFINE_bool(socket_keepalive, false,
"Enable keepalive of sockets if this value is true");
-DEFINE_int32(socket_keepalive_idle_s, -1,
- "Set idle time for socket keepalive in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_idle_s, -1,
+ "Set idle time for socket keepalive in seconds if this value is positive");
-DEFINE_int32(socket_keepalive_interval_s, -1,
- "Set interval between keepalives in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_interval_s, -1,
+ "Set interval between keepalives in seconds if this value is positive");
DEFINE_int32(socket_keepalive_count, -1,
"Set number of keepalives before death if this value is positive");
-DEFINE_int32(socket_tcp_user_timeout_ms, -1,
- "If this value is positive, set number of milliseconds that transmitted "
- "data may remain unacknowledged, or bufferred data may remain untransmitted "
- "(due to zero window size) before TCP will forcibly close the corresponding "
- "connection and return ETIMEDOUT to the application. Only linux supports "
- "TCP_USER_TIMEOUT.");
+DEFINE_int32(
+ socket_tcp_user_timeout_ms, -1,
+ "If this value is positive, set number of milliseconds that transmitted "
+ "data may remain unacknowledged, or bufferred data may remain "
+ "untransmitted "
+ "(due to zero window size) before TCP will forcibly close the "
+ "corresponding "
+ "connection and return ETIMEDOUT to the application. Only linux supports "
+ "TCP_USER_TIMEOUT.");
DECLARE_bool(usercode_in_pthread);
DECLARE_bool(usercode_in_coroutine);
-void* ProcessInputMessage(void* void_arg) {
- InputMessageBase* msg = static_cast<InputMessageBase*>(void_arg);
- msg->_process(msg);
- return nullptr;
+void *ProcessInputMessage(void *void_arg) {
+ InputMessageBase *msg = static_cast<InputMessageBase *>(void_arg);
+ msg->_process(msg);
+ return nullptr;
}
struct RunLastMessage {
- inline void operator()(InputMessageBase* last_msg) {
- ProcessInputMessage(last_msg);
- }
+ inline void operator()(InputMessageBase *last_msg) {
+ ProcessInputMessage(last_msg);
+ }
};
InputMessageClosure::~InputMessageClosure() noexcept(false) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
}
-void InputMessageClosure::reset(InputMessageBase* m) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
- _msg = m;
+void InputMessageClosure::reset(InputMessageBase *m) {
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
+ _msg = m;
}
-void InputMessenger::OnNewMessages(Socket* m) {
- // Notes:
- // - If the socket has only one message, the message will be parsed and
- // processed in this bthread. nova-pbrpc and http works in this way.
- // - If the socket has several messages, all messages will be parsed (
- // meaning cutting from butil::IOBuf. serializing from protobuf is part of
- // "process") in this bthread. All messages except the last one will be
- // processed in separate bthreads. To minimize the overhead, scheduling
- // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
- // - Verify will always be called in this bthread at most once and before
- // any process.
- InputMessengerProcessor& processor = m->fd_input_processor();
- int progress = Socket::PROGRESS_INIT;
-
- // Notice that all *return* no matter successful or not will run last
- // message, even if the socket is about to be closed. This should be
- // OK in most cases.
- InputMessageClosure last_msg;
- bool read_eof = false;
- while (!read_eof) {
- const int64_t received_us = butil::cpuwide_time_us();
- const int64_t base_realtime = butil::gettimeofday_us() - received_us;
-
- // Read.
- const ssize_t nr = m->DoRead(&processor.read_buf(),
- processor.OnceReadSize());
- if (nr <= 0) {
- if (0 == nr) {
- // Set `read_eof' flag and proceed to feed EOF into `Protocol'
- // (implied by an empty processor.read_buf()), which may produce
- // a new `InputMessageBase' under some protocols such as HTTP
- LOG_IF(WARNING, FLAGS_log_connection_close) << *m << " was closed by remote side";
- read_eof = true;
- } else if (errno != EAGAIN) {
- if (errno == EINTR) {
- continue; // just retry
- }
- const int saved_errno = errno;
- PLOG(WARNING) << "Fail to read from " << *m;
- m->SetFailed(saved_errno, "Fail to read from %s: %s",
- m->description().c_str(), berror(saved_errno));
- return;
- } else if (!m->MoreReadEvents(&progress)) {
- return;
- } else { // new events during processing
- continue;
- }
- }
-
- if (processor.ProcessNewMessage(nr, read_eof, received_us,
- base_realtime, last_msg) < 0) {
- return;
+void InputMessenger::OnNewMessages(Socket *m) {
+ // Notes:
+ // - If the socket has only one message, the message will be parsed and
+ // processed in this bthread. nova-pbrpc and http works in this way.
+ // - If the socket has several messages, all messages will be parsed (
+ // meaning cutting from butil::IOBuf. serializing from protobuf is part of
+ // "process") in this bthread. All messages except the last one will be
+ // processed in separate bthreads. To minimize the overhead, scheduling
+ // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
+ // - Verify will always be called in this bthread at most once and before
+ // any process.
+ InputMessengerProcessor &processor = m->fd_input_processor();
+ int progress = Socket::PROGRESS_INIT;
+
+ // Notice that all *return* no matter successful or not will run last
+ // message, even if the socket is about to be closed. This should be
+ // OK in most cases.
+ InputMessageClosure last_msg;
+ bool read_eof = false;
+ while (!read_eof) {
+ const int64_t received_us = butil::cpuwide_time_us();
+ const int64_t base_realtime = butil::gettimeofday_us() - received_us;
+
+ // Read.
+ const ssize_t nr =
+ m->DoRead(&processor.read_buf(), processor.OnceReadSize());
+ if (nr <= 0) {
+ if (0 == nr) {
+ // Set `read_eof' flag and proceed to feed EOF into `Protocol'
+ // (implied by an empty processor.read_buf()), which may produce
+ // a new `InputMessageBase' under some protocols such as HTTP
+ LOG_IF(WARNING, FLAGS_log_connection_close)
+ << *m << " was closed by remote side";
+ read_eof = true;
+ } else if (errno != EAGAIN) {
+ if (errno == EINTR) {
+ continue; // just retry
}
+ const int saved_errno = errno;
+ PLOG(WARNING) << "Fail to read from " << *m;
+ m->SetFailed(saved_errno, "Fail to read from %s: %s",
+ m->description().c_str(), berror(saved_errno));
+ return;
+ } else if (!m->MoreReadEvents(&progress)) {
+ return;
+ } else { // new events during processing
+ continue;
+ }
}
- if (read_eof) {
- m->SetEOF();
+ if (processor.ProcessNewMessage(nr, read_eof, received_us, base_realtime,
+ last_msg) < 0) {
+ return;
}
+ }
+
+ if (read_eof) {
+ m->SetEOF();
+ }
}
InputMessenger::InputMessenger(size_t capacity)
- : _handlers(nullptr)
- , _max_index(-1)
- , _non_protocol(false)
- , _capacity(capacity) {
-}
+ : _handlers(nullptr), _max_index(-1), _non_protocol(false),
+ _capacity(capacity) {}
InputMessenger::~InputMessenger() {
- delete[] _handlers;
- _handlers = nullptr;
- _max_index.store(-1, butil::memory_order_relaxed);
- _capacity = 0;
+ delete[] _handlers;
+ _handlers = nullptr;
+ _max_index.store(-1, butil::memory_order_relaxed);
+ _capacity = 0;
}
-int InputMessenger::AddHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = false;
- }
- if (_non_protocol) {
- CHECK(false) << "AddNonProtocolHandler was invoked";
- return -1;
- }
- ProtocolType type = FindProtocolOfHandler(handler);
- if (type == PROTOCOL_UNKNOWN) {
- CHECK(false) << "Adding a handler which doesn't belong to any protocol";
- return -1;
- }
- const int index = type;
- if (index >= (int)_capacity) {
- LOG(FATAL) << "Can't add more handlers than " << _capacity;
- return -1;
- }
- if (_handlers[index].parse == nullptr) {
- // The same protocol might be added more than twice
- _handlers[index] = handler;
- } else if (_handlers[index].parse != handler.parse
- || _handlers[index].process != handler.process) {
- CHECK(_handlers[index].parse == handler.parse);
- CHECK(_handlers[index].process == handler.process);
- return -1;
- }
- if (index > _max_index.load(butil::memory_order_relaxed)) {
- _max_index.store(index, butil::memory_order_release);
- }
- return 0;
-}
-
-int InputMessenger::AddNonProtocolHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = true;
- }
- if (!_non_protocol) {
- CHECK(false) << "AddHandler was invoked";
- return -1;
- }
- const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+int InputMessenger::AddHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = false;
+ }
+ if (_non_protocol) {
+ CHECK(false) << "AddNonProtocolHandler was invoked";
+ return -1;
+ }
+ ProtocolType type = FindProtocolOfHandler(handler);
+ if (type == PROTOCOL_UNKNOWN) {
+ CHECK(false) << "Adding a handler which doesn't belong to any protocol";
+ return -1;
+ }
+ const int index = type;
+ if (index >= (int)_capacity) {
+ LOG(FATAL) << "Can't add more handlers than " << _capacity;
+ return -1;
+ }
+ if (_handlers[index].parse == nullptr) {
+ // The same protocol might be added more than twice
_handlers[index] = handler;
+ } else if (_handlers[index].parse != handler.parse ||
+ _handlers[index].process != handler.process) {
+ CHECK(_handlers[index].parse == handler.parse);
+ CHECK(_handlers[index].process == handler.process);
+ return -1;
+ }
+ if (index > _max_index.load(butil::memory_order_relaxed)) {
_max_index.store(index, butil::memory_order_release);
- return 0;
+ }
+ return 0;
}
-int InputMessenger::Create(const butil::EndPoint& remote_side,
- time_t health_check_interval_s,
- SocketId* id) {
- SocketOptions options;
- options.remote_side = remote_side;
- options.user = this;
- options.on_edge_triggered_events = OnNewMessages;
- options.health_check_interval_s = health_check_interval_s;
- if (FLAGS_socket_keepalive) {
- options.keepalive_options = std::make_shared<SocketKeepaliveOptions>();
- options.keepalive_options->keepalive_idle_s
- = FLAGS_socket_keepalive_idle_s;
- options.keepalive_options->keepalive_interval_s
- = FLAGS_socket_keepalive_interval_s;
- options.keepalive_options->keepalive_count
- = FLAGS_socket_keepalive_count;
- }
- options.tcp_user_timeout_ms = FLAGS_socket_tcp_user_timeout_ms;
- return Socket::Create(options, id);
+int InputMessenger::AddNonProtocolHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = true;
+ }
+ if (!_non_protocol) {
+ CHECK(false) << "AddHandler was invoked";
+ return -1;
+ }
+ const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+ _handlers[index] = handler;
+ _max_index.store(index, butil::memory_order_release);
+ return 0;
}
Review Comment:
`AddNonProtocolHandler` no longer checks `index` against `_capacity`, so `_handlers[index] = handler;` can write out-of-bounds and corrupt memory. Add the same capacity guard used in `AddHandler` (and fail fast with a clear log / return value) before indexing into `_handlers`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3959030168
##########
src/brpc/input_messenger.cpp:
##########
@@ -56,294 +54,291 @@ BRPC_VALIDATE_GFLAG(log_connection_close, PassValidate);
DEFINE_bool(socket_keepalive, false,
"Enable keepalive of sockets if this value is true");
-DEFINE_int32(socket_keepalive_idle_s, -1,
- "Set idle time for socket keepalive in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_idle_s, -1,
+ "Set idle time for socket keepalive in seconds if this value is positive");
-DEFINE_int32(socket_keepalive_interval_s, -1,
- "Set interval between keepalives in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_interval_s, -1,
+ "Set interval between keepalives in seconds if this value is positive");
DEFINE_int32(socket_keepalive_count, -1,
"Set number of keepalives before death if this value is positive");
-DEFINE_int32(socket_tcp_user_timeout_ms, -1,
- "If this value is positive, set number of milliseconds that transmitted "
- "data may remain unacknowledged, or bufferred data may remain untransmitted "
- "(due to zero window size) before TCP will forcibly close the corresponding "
- "connection and return ETIMEDOUT to the application. Only linux supports "
- "TCP_USER_TIMEOUT.");
+DEFINE_int32(
+ socket_tcp_user_timeout_ms, -1,
+ "If this value is positive, set number of milliseconds that transmitted "
+ "data may remain unacknowledged, or bufferred data may remain "
+ "untransmitted "
+ "(due to zero window size) before TCP will forcibly close the "
+ "corresponding "
+ "connection and return ETIMEDOUT to the application. Only linux supports "
+ "TCP_USER_TIMEOUT.");
DECLARE_bool(usercode_in_pthread);
DECLARE_bool(usercode_in_coroutine);
-void* ProcessInputMessage(void* void_arg) {
- InputMessageBase* msg = static_cast<InputMessageBase*>(void_arg);
- msg->_process(msg);
- return nullptr;
+void *ProcessInputMessage(void *void_arg) {
+ InputMessageBase *msg = static_cast<InputMessageBase *>(void_arg);
+ msg->_process(msg);
+ return nullptr;
}
struct RunLastMessage {
- inline void operator()(InputMessageBase* last_msg) {
- ProcessInputMessage(last_msg);
- }
+ inline void operator()(InputMessageBase *last_msg) {
+ ProcessInputMessage(last_msg);
+ }
};
InputMessageClosure::~InputMessageClosure() noexcept(false) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
}
-void InputMessageClosure::reset(InputMessageBase* m) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
- _msg = m;
+void InputMessageClosure::reset(InputMessageBase *m) {
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
+ _msg = m;
}
-void InputMessenger::OnNewMessages(Socket* m) {
- // Notes:
- // - If the socket has only one message, the message will be parsed and
- // processed in this bthread. nova-pbrpc and http works in this way.
- // - If the socket has several messages, all messages will be parsed (
- // meaning cutting from butil::IOBuf. serializing from protobuf is part of
- // "process") in this bthread. All messages except the last one will be
- // processed in separate bthreads. To minimize the overhead, scheduling
- // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
- // - Verify will always be called in this bthread at most once and before
- // any process.
- InputMessengerProcessor& processor = m->fd_input_processor();
- int progress = Socket::PROGRESS_INIT;
-
- // Notice that all *return* no matter successful or not will run last
- // message, even if the socket is about to be closed. This should be
- // OK in most cases.
- InputMessageClosure last_msg;
- bool read_eof = false;
- while (!read_eof) {
- const int64_t received_us = butil::cpuwide_time_us();
- const int64_t base_realtime = butil::gettimeofday_us() - received_us;
-
- // Read.
- const ssize_t nr = m->DoRead(&processor.read_buf(),
- processor.OnceReadSize());
- if (nr <= 0) {
- if (0 == nr) {
- // Set `read_eof' flag and proceed to feed EOF into `Protocol'
- // (implied by an empty processor.read_buf()), which may produce
- // a new `InputMessageBase' under some protocols such as HTTP
- LOG_IF(WARNING, FLAGS_log_connection_close) << *m << " was closed by remote side";
- read_eof = true;
- } else if (errno != EAGAIN) {
- if (errno == EINTR) {
- continue; // just retry
- }
- const int saved_errno = errno;
- PLOG(WARNING) << "Fail to read from " << *m;
- m->SetFailed(saved_errno, "Fail to read from %s: %s",
- m->description().c_str(), berror(saved_errno));
- return;
- } else if (!m->MoreReadEvents(&progress)) {
- return;
- } else { // new events during processing
- continue;
- }
- }
-
- if (processor.ProcessNewMessage(nr, read_eof, received_us,
- base_realtime, last_msg) < 0) {
- return;
+void InputMessenger::OnNewMessages(Socket *m) {
+ // Notes:
+ // - If the socket has only one message, the message will be parsed and
+ // processed in this bthread. nova-pbrpc and http works in this way.
+ // - If the socket has several messages, all messages will be parsed (
+ // meaning cutting from butil::IOBuf. serializing from protobuf is part of
+ // "process") in this bthread. All messages except the last one will be
+ // processed in separate bthreads. To minimize the overhead, scheduling
+ // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
+ // - Verify will always be called in this bthread at most once and before
+ // any process.
+ InputMessengerProcessor &processor = m->fd_input_processor();
+ int progress = Socket::PROGRESS_INIT;
+
+ // Notice that all *return* no matter successful or not will run last
+ // message, even if the socket is about to be closed. This should be
+ // OK in most cases.
+ InputMessageClosure last_msg;
+ bool read_eof = false;
+ while (!read_eof) {
+ const int64_t received_us = butil::cpuwide_time_us();
+ const int64_t base_realtime = butil::gettimeofday_us() - received_us;
+
+ // Read.
+ const ssize_t nr =
+ m->DoRead(&processor.read_buf(), processor.OnceReadSize());
+ if (nr <= 0) {
+ if (0 == nr) {
+ // Set `read_eof' flag and proceed to feed EOF into `Protocol'
+ // (implied by an empty processor.read_buf()), which may produce
+ // a new `InputMessageBase' under some protocols such as HTTP
+ LOG_IF(WARNING, FLAGS_log_connection_close)
+ << *m << " was closed by remote side";
+ read_eof = true;
+ } else if (errno != EAGAIN) {
+ if (errno == EINTR) {
+ continue; // just retry
}
+ const int saved_errno = errno;
+ PLOG(WARNING) << "Fail to read from " << *m;
+ m->SetFailed(saved_errno, "Fail to read from %s: %s",
+ m->description().c_str(), berror(saved_errno));
+ return;
+ } else if (!m->MoreReadEvents(&progress)) {
+ return;
+ } else { // new events during processing
+ continue;
+ }
}
- if (read_eof) {
- m->SetEOF();
+ if (processor.ProcessNewMessage(nr, read_eof, received_us, base_realtime,
+ last_msg) < 0) {
+ return;
}
+ }
+
+ if (read_eof) {
+ m->SetEOF();
+ }
}
InputMessenger::InputMessenger(size_t capacity)
- : _handlers(nullptr)
- , _max_index(-1)
- , _non_protocol(false)
- , _capacity(capacity) {
-}
+ : _handlers(nullptr), _max_index(-1), _non_protocol(false),
+ _capacity(capacity) {}
InputMessenger::~InputMessenger() {
- delete[] _handlers;
- _handlers = nullptr;
- _max_index.store(-1, butil::memory_order_relaxed);
- _capacity = 0;
+ delete[] _handlers;
+ _handlers = nullptr;
+ _max_index.store(-1, butil::memory_order_relaxed);
+ _capacity = 0;
}
-int InputMessenger::AddHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = false;
- }
- if (_non_protocol) {
- CHECK(false) << "AddNonProtocolHandler was invoked";
- return -1;
- }
- ProtocolType type = FindProtocolOfHandler(handler);
- if (type == PROTOCOL_UNKNOWN) {
- CHECK(false) << "Adding a handler which doesn't belong to any protocol";
- return -1;
- }
- const int index = type;
- if (index >= (int)_capacity) {
- LOG(FATAL) << "Can't add more handlers than " << _capacity;
- return -1;
- }
- if (_handlers[index].parse == nullptr) {
- // The same protocol might be added more than twice
- _handlers[index] = handler;
- } else if (_handlers[index].parse != handler.parse
- || _handlers[index].process != handler.process) {
- CHECK(_handlers[index].parse == handler.parse);
- CHECK(_handlers[index].process == handler.process);
- return -1;
- }
- if (index > _max_index.load(butil::memory_order_relaxed)) {
- _max_index.store(index, butil::memory_order_release);
- }
- return 0;
-}
-
-int InputMessenger::AddNonProtocolHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = true;
- }
- if (!_non_protocol) {
- CHECK(false) << "AddHandler was invoked";
- return -1;
- }
- const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+int InputMessenger::AddHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = false;
+ }
+ if (_non_protocol) {
+ CHECK(false) << "AddNonProtocolHandler was invoked";
+ return -1;
+ }
+ ProtocolType type = FindProtocolOfHandler(handler);
+ if (type == PROTOCOL_UNKNOWN) {
+ CHECK(false) << "Adding a handler which doesn't belong to any protocol";
+ return -1;
+ }
+ const int index = type;
+ if (index >= (int)_capacity) {
+ LOG(FATAL) << "Can't add more handlers than " << _capacity;
+ return -1;
+ }
+ if (_handlers[index].parse == nullptr) {
+ // The same protocol might be added more than twice
_handlers[index] = handler;
+ } else if (_handlers[index].parse != handler.parse ||
+ _handlers[index].process != handler.process) {
+ CHECK(_handlers[index].parse == handler.parse);
+ CHECK(_handlers[index].process == handler.process);
+ return -1;
+ }
+ if (index > _max_index.load(butil::memory_order_relaxed)) {
_max_index.store(index, butil::memory_order_release);
- return 0;
+ }
+ return 0;
}
-int InputMessenger::Create(const butil::EndPoint& remote_side,
- time_t health_check_interval_s,
- SocketId* id) {
- SocketOptions options;
- options.remote_side = remote_side;
- options.user = this;
- options.on_edge_triggered_events = OnNewMessages;
- options.health_check_interval_s = health_check_interval_s;
- if (FLAGS_socket_keepalive) {
- options.keepalive_options = std::make_shared<SocketKeepaliveOptions>();
- options.keepalive_options->keepalive_idle_s
- = FLAGS_socket_keepalive_idle_s;
- options.keepalive_options->keepalive_interval_s
- = FLAGS_socket_keepalive_interval_s;
- options.keepalive_options->keepalive_count
- = FLAGS_socket_keepalive_count;
- }
- options.tcp_user_timeout_ms = FLAGS_socket_tcp_user_timeout_ms;
- return Socket::Create(options, id);
+int InputMessenger::AddNonProtocolHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = true;
+ }
+ if (!_non_protocol) {
+ CHECK(false) << "AddHandler was invoked";
+ return -1;
+ }
+ const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+ _handlers[index] = handler;
+ _max_index.store(index, butil::memory_order_release);
+ return 0;
}
Review Comment:
AddNonProtocolHandler no longer bounds-checks `index` against `_capacity`, which can cause an out-of-bounds write to `_handlers` when many non-protocol handlers are registered (or when `_max_index` is already at capacity-1). Restore the capacity guard (and keep behavior consistent with AddHandler) before writing to `_handlers[index]`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3960505065
##########
src/brpc/input_messenger_processor.cpp:
##########
@@ -15,279 +15,285 @@
// specific language governing permissions and limitations
// under the License.
-#include "butil/logging.h"
-#include "butil/binary_printer.h"
-#include "bthread/unstable.h"
-#include "brpc/options.pb.h"
-#include "brpc/transport.h"
#include "brpc/input_messenger_processor.h"
#include "brpc/input_messenger.h"
+#include "brpc/options.pb.h"
+#include "brpc/transport.h"
+#include "bthread/unstable.h"
+#include "butil/binary_printer.h"
+#include "butil/logging.h"
+
namespace brpc {
DECLARE_uint64(max_body_size);
-const size_t MSG_SIZE_WINDOW = 10; // Take last so many message into stat.
+const size_t MSG_SIZE_WINDOW = 10; // Take last so many message into stat.
const size_t MIN_ONCE_READ = 4096;
const size_t MAX_ONCE_READ = 524288;
-static const char* StreamTypeName(InputMessengerProcessor::StreamType type) {
- switch (type) {
- case InputMessengerProcessor::STREAM_NONE: return "none";
- case InputMessengerProcessor::STREAM_TCP_FD: return "tcp_fd";
- case InputMessengerProcessor::STREAM_RDMA_QP: return "rdma_qp";
- }
- return "unknown";
+static const char *StreamTypeName(InputMessengerProcessor::StreamType type) {
+ switch (type) {
+ case InputMessengerProcessor::STREAM_NONE:
+ return "none";
+ case InputMessengerProcessor::STREAM_TCP_FD:
+ return "tcp_fd";
+ case InputMessengerProcessor::STREAM_RDMA_QP:
+ return "rdma_qp";
+ case InputMessengerProcessor::STREAM_URMA_JETTY:
+ return "urma_jetty";
+ }
+ return "unknown";
}
InputMessengerProcessor::ParsingStreamGuard::ParsingStreamGuard(Socket* socket, StreamType type)
: _socket(socket) {
- CHECK(socket != nullptr)
- << "Parsing through a processor that was never Init()ed, socket is NULL";
- CHECK_NE(STREAM_NONE, type)
- << "Parsing through a processor that was never Init()ed, " << *socket;
- CHECK_EQ(STREAM_NONE, socket->parsing_stream_type())
- << "Two input streams of " << *socket << " are parsing at the same time: "
- << StreamTypeName(socket->parsing_stream_type()) << " and "
- << StreamTypeName(type);
- socket->set_parsing_stream_type(type);
+ CHECK(socket != nullptr)
+ << "Parsing through a processor that was never Init()ed, socket is NULL";
+ CHECK_NE(STREAM_NONE, type)
+ << "Parsing through a processor that was never Init()ed, " << *socket;
+ CHECK_EQ(STREAM_NONE, socket->parsing_stream_type())
+ << "Two input streams of " << *socket << " are parsing at the same time: "
+ << StreamTypeName(socket->parsing_stream_type()) << " and "
+ << StreamTypeName(type);
+ socket->set_parsing_stream_type(type);
}
InputMessengerProcessor::ParsingStreamGuard::~ParsingStreamGuard() {
- _socket->set_parsing_stream_type(STREAM_NONE);
+ _socket->set_parsing_stream_type(STREAM_NONE);
}
-ParseResult InputMessengerProcessor::CutInputMessage(InputMessenger* messenger,
- size_t* index, bool read_eof) {
- ParsingStreamGuard parsing_stream_guard(_socket, _stream_type);
- InputMessageHandler* handlers = messenger->_handlers;
- int preferred = _socket->preferred_index();
- int max_index = (int)messenger->_max_index.load(butil::memory_order_acquire);
- // Try preferred handler first. The preferred_index is set on last
- // selection or by client.
- if (preferred >= 0 && preferred <= max_index
- && handlers[preferred].parse != nullptr) {
- int cur_index = preferred;
- do {
- ParseResult result =
- handlers[cur_index].parse(&_read_buf, _socket, read_eof,
- handlers[cur_index].arg);
- if (result.is_ok() ||
- result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
- _socket->set_preferred_index(cur_index);
- *index = cur_index;
- return result;
- } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
- // Critical error, return directly.
- LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
- << "A message from " << _socket->remote_side()
- << "(protocol=" << handlers[cur_index].name
- << ") is bigger than " << FLAGS_max_body_size
- << " bytes, the connection will be closed."
- " Set max_body_size to allow bigger messages";
- return result;
- }
+ParseResult InputMessengerProcessor::CutInputMessage(InputMessenger *messenger,
+ size_t *index,
+ bool read_eof) {
+ ParsingStreamGuard parsing_stream_guard(_socket, _stream_type);
+ InputMessageHandler *handlers = messenger->_handlers;
+ int preferred = _socket->preferred_index();
+ int max_index = (int)messenger->_max_index.load(butil::memory_order_acquire);
+ // Try preferred handler first. The preferred_index is set on last
+ // selection or by client.
+ if (preferred >= 0 && preferred <= max_index &&
+ handlers[preferred].parse != nullptr) {
+ int cur_index = preferred;
+ do {
+ ParseResult result = handlers[cur_index].parse(
+ &_read_buf, _socket, read_eof, handlers[cur_index].arg);
+ if (result.is_ok() || result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
+ _socket->set_preferred_index(cur_index);
+ *index = cur_index;
+ return result;
+ } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
+ // Critical error, return directly.
+ LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
+ << "A message from " << _socket->remote_side()
+ << "(protocol=" << handlers[cur_index].name << ") is bigger than "
+ << FLAGS_max_body_size
+ << " bytes, the connection will be closed."
+ " Set max_body_size to allow bigger messages";
+ return result;
+ }
- if (_socket->CreatedByConnect()) {
- if((ProtocolType)cur_index == PROTOCOL_BAIDU_STD && cur_index == preferred) {
- // baidu_std may fall to streaming_rpc.
- cur_index = (int)PROTOCOL_STREAMING_RPC;
- continue;
- } else if((ProtocolType)cur_index == PROTOCOL_STREAMING_RPC &&
- cur_index == preferred) {
- // streaming_rpc may fall to baidu_std.
- cur_index = (int)PROTOCOL_BAIDU_STD;
- continue;
- } else {
- // The protocol is fixed at client-side, no need to try others.
- LOG(ERROR) << "Fail to parse response from " << _socket->remote_side()
- << " by " << handlers[preferred].name
- << " at client-side";
- return MakeParseError(PARSE_ERROR_ABSOLUTELY_WRONG);
- }
- } else {
- // Try other protocols.
- //
- // A handler may lean on this: returning PARSE_ERROR_NOT_ENOUGH_DATA
- // above keeps `preferred_index' pinned on it, TRY_OTHERS here gives
- // it up. RdmaEndpoint::ExecuteServerHandshake() pins itself that way
- // to keep the last handshake read from being taken for a protocol
- // detection. See the tail of its phase 2 before changing what
- // happens to `preferred_index' here.
- break;
- }
- } while (true);
- // Clear context before trying next protocol which probably has
- // an incompatible context with the current one.
- if (_socket->parsing_context()) {
- _socket->reset_parsing_context(nullptr);
- }
- _socket->set_preferred_index(-1);
- }
- for (int i = 0; i <= max_index; ++i) {
- if (i == preferred || handlers[i].parse == nullptr) {
- // Don't try preferred handler(already tried) or invalid handler
- continue;
- }
- ParseResult result = handlers[i].parse(&_read_buf, _socket, read_eof, handlers[i].arg);
- if (result.is_ok() ||
- result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
- _socket->set_preferred_index(i);
- *index = i;
- return result;
- } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
- // Critical error, return directly.
- LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
- << "A message from " << _socket->remote_side()
- << "(protocol=" << handlers[i].name
- << ") is bigger than " << FLAGS_max_body_size
- << " bytes, the connection will be closed."
- " Set max_body_size to allow bigger messages";
- return result;
- }
- // Clear context before trying next protocol which definitely has
- // an incompatible context with the current one.
- if (_socket->parsing_context()) {
- _socket->reset_parsing_context(nullptr);
+ if (_socket->CreatedByConnect()) {
+ if ((ProtocolType)cur_index == PROTOCOL_BAIDU_STD &&
+ cur_index == preferred) {
+ // baidu_std may fall to streaming_rpc.
+ cur_index = (int)PROTOCOL_STREAMING_RPC;
+ continue;
+ } else if ((ProtocolType)cur_index == PROTOCOL_STREAMING_RPC &&
+ cur_index == preferred) {
+ // streaming_rpc may fall to baidu_std.
+ cur_index = (int)PROTOCOL_BAIDU_STD;
+ continue;
+ } else {
+ // The protocol is fixed at client-side, no need to try others.
+ LOG(ERROR) << "Fail to parse response from " << _socket->remote_side()
+ << " by " << handlers[preferred].name << " at client-side";
+ return MakeParseError(PARSE_ERROR_ABSOLUTELY_WRONG);
}
+ } else {
// Try other protocols.
+ //
+ // A handler may lean on this: returning PARSE_ERROR_NOT_ENOUGH_DATA
+ // above keeps `preferred_index' pinned on it, TRY_OTHERS here gives
+ // it up. RdmaEndpoint::ExecuteServerHandshake() pins itself that way
+ // to keep the last handshake read from being taken for a protocol
+ // detection. See the tail of its phase 2 before changing what
+ // happens to `preferred_index' here.
+ break;
+ }
+ } while (true);
+ // Clear context before trying next protocol which probably has
+ // an incompatible context with the current one.
+ if (_socket->parsing_context()) {
+ _socket->reset_parsing_context(nullptr);
+ }
+ _socket->set_preferred_index(-1);
+ }
+ for (int i = 0; i <= max_index; ++i) {
+ if (i == preferred || handlers[i].parse == nullptr) {
+ // Don't try preferred handler(already tried) or invalid handler
+ continue;
}
- return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+ ParseResult result =
+ handlers[i].parse(&_read_buf, _socket, read_eof, handlers[i].arg);
+ if (result.is_ok() || result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
+ _socket->set_preferred_index(i);
+ *index = i;
+ return result;
+ } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
+ // Critical error, return directly.
+ LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
+ << "A message from " << _socket->remote_side()
+ << "(protocol=" << handlers[i].name << ") is bigger than "
+ << FLAGS_max_body_size
+ << " bytes, the connection will be closed."
+ " Set max_body_size to allow bigger messages";
+ return result;
+ }
+ // Clear context before trying next protocol which definitely has
+ // an incompatible context with the current one.
+ if (_socket->parsing_context()) {
+ _socket->reset_parsing_context(nullptr);
+ }
+ // Try other protocols.
+ }
+ return MakeParseError(PARSE_ERROR_TRY_OTHERS);
}
size_t InputMessengerProcessor::OnceReadSize() const {
- size_t once_read = _avg_msg_size * 16;
- if (once_read < MIN_ONCE_READ) {
- once_read = MIN_ONCE_READ;
- } else if (once_read > MAX_ONCE_READ) {
- once_read = MAX_ONCE_READ;
- }
- return once_read;
+ size_t once_read = _avg_msg_size * 16;
+ if (once_read < MIN_ONCE_READ) {
+ once_read = MIN_ONCE_READ;
+ } else if (once_read > MAX_ONCE_READ) {
+ once_read = MAX_ONCE_READ;
+ }
+ return once_read;
}
void InputMessengerProcessor::Reset() {
- _read_buf.clear();
- _last_msg_size = 0;
- _avg_msg_size = 0;
+ _read_buf.clear();
+ _last_msg_size = 0;
+ _avg_msg_size = 0;
}
int InputMessengerProcessor::ProcessNewMessage(ssize_t bytes, bool read_eof,
uint64_t received_us,
uint64_t base_realtime,
- InputMessageClosure& last_msg) {
- auto messenger = static_cast<InputMessenger*>(_socket->user());
- const InputMessageHandler* handlers = messenger->_handlers;
- _socket->AddInputBytes(bytes);
+ InputMessageClosure &last_msg) {
+ auto messenger = static_cast<InputMessenger *>(_socket->user());
+ const InputMessageHandler *handlers = messenger->_handlers;
+ _socket->AddInputBytes(bytes);
- // Avoid this socket to be closed due to idle_timeout_s
- _socket->_last_readtime_us.store(received_us, butil::memory_order_relaxed);
+ // Avoid this socket to be closed due to idle_timeout_s
+ _socket->_last_readtime_us.store(received_us, butil::memory_order_relaxed);
- size_t last_size = _read_buf.length();
- int num_bthread_created = 0;
- while (true) {
- size_t index = 8888;
- ParseResult pr = CutInputMessage(messenger, &index, read_eof);
- if (!pr.is_ok()) {
- if (pr.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
- // incomplete message, re-read.
- // However, some buffer may have been consumed
- // under protocols like HTTP. Record this size
- _last_msg_size += (last_size - _read_buf.length());
- break;
- } else if (pr.error() == PARSE_ERROR_TRY_OTHERS) {
- LOG(WARNING) << "Close " << *_socket << " due to unknown message: "
- << butil::ToPrintable(_read_buf);
- _socket->SetFailed(EINVAL, "Close %s due to unknown message",
- _socket->description().c_str());
- return -1;
- } else {
- LOG(WARNING) << "Close " << *_socket << ": " << pr.error_str();
- _socket->SetFailed(EINVAL, "Close %s: %s",
- _socket->description().c_str(), pr.error_str());
- return -1;
- }
- }
+ size_t last_size = _read_buf.length();
+ int num_bthread_created = 0;
+ while (true) {
+ size_t index = 8888;
+ ParseResult pr = CutInputMessage(messenger, &index, read_eof);
+ if (!pr.is_ok()) {
+ if (pr.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
+ // incomplete message, re-read.
+ // However, some buffer may have been consumed
+ // under protocols like HTTP. Record this size
+ _last_msg_size += (last_size - _read_buf.length());
+ break;
+ } else if (pr.error() == PARSE_ERROR_TRY_OTHERS) {
+ LOG(WARNING) << "Close " << *_socket << " due to unknown message: "
+ << butil::ToPrintable(_read_buf);
+ _socket->SetFailed(EINVAL, "Close %s due to unknown message",
+ _socket->description().c_str());
+ return -1;
+ } else {
+ LOG(WARNING) << "Close " << *_socket << ": " << pr.error_str();
+ _socket->SetFailed(EINVAL, "Close %s: %s",
+ _socket->description().c_str(), pr.error_str());
+ return -1;
+ }
+ }
- _socket->AddInputMessages(1);
- // Calculate average size of messages
- const size_t cur_size = _read_buf.length();
- if (cur_size == 0) {
- // _read_buf is consumed, it's good timing to return blocks
- // cached internally back to TLS, otherwise the memory is not
- // reused until next message arrives which is quite uncertain
- // in situations that most connections are idle.
- _read_buf.return_cached_blocks();
- }
- _last_msg_size += (last_size - cur_size);
- last_size = cur_size;
- const size_t old_avg = _avg_msg_size;
- if (old_avg != 0) {
- _avg_msg_size = (old_avg * (MSG_SIZE_WINDOW - 1) + _last_msg_size) / MSG_SIZE_WINDOW;
- } else {
- _avg_msg_size = _last_msg_size;
- }
- _last_msg_size = 0;
+ _socket->AddInputMessages(1);
+ // Calculate average size of messages
+ const size_t cur_size = _read_buf.length();
+ if (cur_size == 0) {
+ // _read_buf is consumed, it's good timing to return blocks
+ // cached internally back to TLS, otherwise the memory is not
+ // reused until next message arrives which is quite uncertain
+ // in situations that most connections are idle.
+ _read_buf.return_cached_blocks();
+ }
+ _last_msg_size += (last_size - cur_size);
+ last_size = cur_size;
+ const size_t old_avg = _avg_msg_size;
+ if (old_avg != 0) {
+ _avg_msg_size =
+ (old_avg * (MSG_SIZE_WINDOW - 1) + _last_msg_size) / MSG_SIZE_WINDOW;
+ } else {
+ _avg_msg_size = _last_msg_size;
+ }
+ _last_msg_size = 0;
- if (pr.message() == nullptr) { // the Process() step can be skipped.
- continue;
- }
- pr.message()->_received_us = received_us;
- pr.message()->_base_real_us = base_realtime;
+ if (pr.message() == nullptr) { // the Process() step can be skipped.
+ continue;
+ }
+ pr.message()->_received_us = received_us;
+ pr.message()->_base_real_us = base_realtime;
- // This unique_ptr prevents msg to be lost before transfering
- // ownership to last_msg
- DestroyingPtr<InputMessageBase> msg(pr.message());
- _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
- if (handlers[index].process == nullptr) {
- LOG(ERROR) << "process of index=" << index << " is NULL";
- continue;
- }
- _socket->ReAddress(&msg->_socket);
- _socket->PostponeEOF();
- msg->_process = handlers[index].process;
- msg->_arg = handlers[index].arg;
+ // This unique_ptr prevents msg to be lost before transfering
+ // ownership to last_msg
+ DestroyingPtr<InputMessageBase> msg(pr.message());
+ _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
+ if (handlers[index].process == nullptr) {
+ LOG(ERROR) << "process of index=" << index << " is NULL";
+ continue;
+ }
+ _socket->ReAddress(&msg->_socket);
+ _socket->PostponeEOF();
+ msg->_process = handlers[index].process;
+ msg->_arg = handlers[index].arg;
- if (handlers[index].verify != nullptr) {
- int auth_error = 0;
- if (0 == _socket->FightAuthentication(&auth_error)) {
- // Get the right to authenticate
- if (handlers[index].verify(msg.get())) {
- _socket->SetAuthentication(0);
- } else {
- _socket->SetAuthentication(ERPCAUTH);
- LOG(WARNING) << "Fail to authenticate " << *_socket;
- _socket->SetFailed(ERPCAUTH, "Fail to authenticate %s",
- _socket->description().c_str());
- return -1;
- }
- } else {
- LOG_IF(FATAL, auth_error != 0) <<
- "Impossible! Socket should have been "
- "destroyed when authentication failed";
- }
- }
- if (!_socket->is_read_progressive()) {
- // Transfer ownership to last_msg
- last_msg.reset(msg.release());
+ if (handlers[index].verify != nullptr) {
+ int auth_error = 0;
+ if (0 == _socket->FightAuthentication(&auth_error)) {
+ // Get the right to authenticate
+ if (handlers[index].verify(msg.get())) {
+ _socket->SetAuthentication(0);
} else {
- last_msg.reset(msg.release());
- _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
- bthread_flush();
- num_bthread_created = 0;
+ _socket->SetAuthentication(ERPCAUTH);
+ LOG(WARNING) << "Fail to authenticate " << *_socket;
+ _socket->SetFailed(ERPCAUTH, "Fail to authenticate %s",
+ _socket->description().c_str());
+ return -1;
}
+ } else {
+ LOG_IF(FATAL, auth_error != 0)
+ << "Impossible! Socket should have been "
+ "destroyed when authentication failed";
+ }
}
- // In RDMA polling mode, all messages must be executed in a new bthread and
- // not in the bthread where the polling bthread is located, because the
- // method for processing messages may call synchronization primitives,
- // causing the polling bthread to be scheduled out.
- if (_socket->_socket_mode == SOCKET_MODE_RDMA ||
- _socket->_socket_mode == SOCKET_MODE_UBRING) {
- _socket->_transport->QueueMessage(last_msg, &num_bthread_created, true);
- }
- if (num_bthread_created) {
- bthread_flush();
+ if (!_socket->is_read_progressive()) {
+ // Transfer ownership to last_msg
+ last_msg.reset(msg.release());
+ } else {
+ last_msg.reset(msg.release());
+ _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
+ bthread_flush();
+ num_bthread_created = 0;
}
- return 0;
+ }
+ // On dedicated transport pollers (RDMA, UBRING, and URMA), all messages must
+ // be executed in a new bthread. Processing user code on the poller may call
+ // synchronization primitives and prevent it from draining more events.
+ if (_socket->_socket_mode == SOCKET_MODE_RDMA ||
+ _socket->_socket_mode == SOCKET_MODE_UBRING ||
+ _socket->_socket_mode == SOCKET_MODE_URMA) {
Review Comment:
This comment states that URMA always behaves like a dedicated poller that must not run user code inline, but `UrmaTransport::QueueMessage` currently returns early when `--urma_use_polling=false`, causing the last message to run inline via `InputMessageClosure` destruction. Either narrow the comment/condition to URMA polling mode only, or adjust URMA’s queuing behavior so it matches the stated constraint.
##########
src/brpc/input_messenger.cpp:
##########
@@ -56,294 +54,291 @@ BRPC_VALIDATE_GFLAG(log_connection_close, PassValidate);
DEFINE_bool(socket_keepalive, false,
"Enable keepalive of sockets if this value is true");
-DEFINE_int32(socket_keepalive_idle_s, -1,
- "Set idle time for socket keepalive in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_idle_s, -1,
+ "Set idle time for socket keepalive in seconds if this value is positive");
-DEFINE_int32(socket_keepalive_interval_s, -1,
- "Set interval between keepalives in seconds if this value is positive");
+DEFINE_int32(
+ socket_keepalive_interval_s, -1,
+ "Set interval between keepalives in seconds if this value is positive");
DEFINE_int32(socket_keepalive_count, -1,
"Set number of keepalives before death if this value is positive");
-DEFINE_int32(socket_tcp_user_timeout_ms, -1,
- "If this value is positive, set number of milliseconds that transmitted "
- "data may remain unacknowledged, or bufferred data may remain untransmitted "
- "(due to zero window size) before TCP will forcibly close the corresponding "
- "connection and return ETIMEDOUT to the application. Only linux supports "
- "TCP_USER_TIMEOUT.");
+DEFINE_int32(
+ socket_tcp_user_timeout_ms, -1,
+ "If this value is positive, set number of milliseconds that transmitted "
+ "data may remain unacknowledged, or bufferred data may remain "
+ "untransmitted "
+ "(due to zero window size) before TCP will forcibly close the "
+ "corresponding "
+ "connection and return ETIMEDOUT to the application. Only linux supports "
+ "TCP_USER_TIMEOUT.");
DECLARE_bool(usercode_in_pthread);
DECLARE_bool(usercode_in_coroutine);
-void* ProcessInputMessage(void* void_arg) {
- InputMessageBase* msg = static_cast<InputMessageBase*>(void_arg);
- msg->_process(msg);
- return nullptr;
+void *ProcessInputMessage(void *void_arg) {
+ InputMessageBase *msg = static_cast<InputMessageBase *>(void_arg);
+ msg->_process(msg);
+ return nullptr;
}
struct RunLastMessage {
- inline void operator()(InputMessageBase* last_msg) {
- ProcessInputMessage(last_msg);
- }
+ inline void operator()(InputMessageBase *last_msg) {
+ ProcessInputMessage(last_msg);
+ }
};
InputMessageClosure::~InputMessageClosure() noexcept(false) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
}
-void InputMessageClosure::reset(InputMessageBase* m) {
- if (_msg) {
- ProcessInputMessage(_msg);
- }
- _msg = m;
+void InputMessageClosure::reset(InputMessageBase *m) {
+ if (_msg) {
+ ProcessInputMessage(_msg);
+ }
+ _msg = m;
}
-void InputMessenger::OnNewMessages(Socket* m) {
- // Notes:
- // - If the socket has only one message, the message will be parsed and
- // processed in this bthread. nova-pbrpc and http works in this way.
- // - If the socket has several messages, all messages will be parsed (
- // meaning cutting from butil::IOBuf. serializing from protobuf is part of
- // "process") in this bthread. All messages except the last one will be
- // processed in separate bthreads. To minimize the overhead, scheduling
- // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
- // - Verify will always be called in this bthread at most once and before
- // any process.
- InputMessengerProcessor& processor = m->fd_input_processor();
- int progress = Socket::PROGRESS_INIT;
-
- // Notice that all *return* no matter successful or not will run last
- // message, even if the socket is about to be closed. This should be
- // OK in most cases.
- InputMessageClosure last_msg;
- bool read_eof = false;
- while (!read_eof) {
- const int64_t received_us = butil::cpuwide_time_us();
- const int64_t base_realtime = butil::gettimeofday_us() - received_us;
-
- // Read.
- const ssize_t nr = m->DoRead(&processor.read_buf(),
- processor.OnceReadSize());
- if (nr <= 0) {
- if (0 == nr) {
- // Set `read_eof' flag and proceed to feed EOF into `Protocol'
- // (implied by an empty processor.read_buf()), which may produce
- // a new `InputMessageBase' under some protocols such as HTTP
- LOG_IF(WARNING, FLAGS_log_connection_close) << *m << " was closed by remote side";
- read_eof = true;
- } else if (errno != EAGAIN) {
- if (errno == EINTR) {
- continue; // just retry
- }
- const int saved_errno = errno;
- PLOG(WARNING) << "Fail to read from " << *m;
- m->SetFailed(saved_errno, "Fail to read from %s: %s",
- m->description().c_str(), berror(saved_errno));
- return;
- } else if (!m->MoreReadEvents(&progress)) {
- return;
- } else { // new events during processing
- continue;
- }
- }
-
- if (processor.ProcessNewMessage(nr, read_eof, received_us,
- base_realtime, last_msg) < 0) {
- return;
+void InputMessenger::OnNewMessages(Socket *m) {
+ // Notes:
+ // - If the socket has only one message, the message will be parsed and
+ // processed in this bthread. nova-pbrpc and http works in this way.
+ // - If the socket has several messages, all messages will be parsed (
+ // meaning cutting from butil::IOBuf. serializing from protobuf is part of
+ // "process") in this bthread. All messages except the last one will be
+ // processed in separate bthreads. To minimize the overhead, scheduling
+ // is batched(notice the BTHREAD_NOSIGNAL and bthread_flush).
+ // - Verify will always be called in this bthread at most once and before
+ // any process.
+ InputMessengerProcessor &processor = m->fd_input_processor();
+ int progress = Socket::PROGRESS_INIT;
+
+ // Notice that all *return* no matter successful or not will run last
+ // message, even if the socket is about to be closed. This should be
+ // OK in most cases.
+ InputMessageClosure last_msg;
+ bool read_eof = false;
+ while (!read_eof) {
+ const int64_t received_us = butil::cpuwide_time_us();
+ const int64_t base_realtime = butil::gettimeofday_us() - received_us;
+
+ // Read.
+ const ssize_t nr =
+ m->DoRead(&processor.read_buf(), processor.OnceReadSize());
+ if (nr <= 0) {
+ if (0 == nr) {
+ // Set `read_eof' flag and proceed to feed EOF into `Protocol'
+ // (implied by an empty processor.read_buf()), which may produce
+ // a new `InputMessageBase' under some protocols such as HTTP
+ LOG_IF(WARNING, FLAGS_log_connection_close)
+ << *m << " was closed by remote side";
+ read_eof = true;
+ } else if (errno != EAGAIN) {
+ if (errno == EINTR) {
+ continue; // just retry
}
+ const int saved_errno = errno;
+ PLOG(WARNING) << "Fail to read from " << *m;
+ m->SetFailed(saved_errno, "Fail to read from %s: %s",
+ m->description().c_str(), berror(saved_errno));
+ return;
+ } else if (!m->MoreReadEvents(&progress)) {
+ return;
+ } else { // new events during processing
+ continue;
+ }
}
- if (read_eof) {
- m->SetEOF();
+ if (processor.ProcessNewMessage(nr, read_eof, received_us, base_realtime,
+ last_msg) < 0) {
+ return;
}
+ }
+
+ if (read_eof) {
+ m->SetEOF();
+ }
}
InputMessenger::InputMessenger(size_t capacity)
- : _handlers(nullptr)
- , _max_index(-1)
- , _non_protocol(false)
- , _capacity(capacity) {
-}
+ : _handlers(nullptr), _max_index(-1), _non_protocol(false),
+ _capacity(capacity) {}
InputMessenger::~InputMessenger() {
- delete[] _handlers;
- _handlers = nullptr;
- _max_index.store(-1, butil::memory_order_relaxed);
- _capacity = 0;
+ delete[] _handlers;
+ _handlers = nullptr;
+ _max_index.store(-1, butil::memory_order_relaxed);
+ _capacity = 0;
}
-int InputMessenger::AddHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = false;
- }
- if (_non_protocol) {
- CHECK(false) << "AddNonProtocolHandler was invoked";
- return -1;
- }
- ProtocolType type = FindProtocolOfHandler(handler);
- if (type == PROTOCOL_UNKNOWN) {
- CHECK(false) << "Adding a handler which doesn't belong to any protocol";
- return -1;
- }
- const int index = type;
- if (index >= (int)_capacity) {
- LOG(FATAL) << "Can't add more handlers than " << _capacity;
- return -1;
- }
- if (_handlers[index].parse == nullptr) {
- // The same protocol might be added more than twice
- _handlers[index] = handler;
- } else if (_handlers[index].parse != handler.parse
- || _handlers[index].process != handler.process) {
- CHECK(_handlers[index].parse == handler.parse);
- CHECK(_handlers[index].process == handler.process);
- return -1;
- }
- if (index > _max_index.load(butil::memory_order_relaxed)) {
- _max_index.store(index, butil::memory_order_release);
- }
- return 0;
-}
-
-int InputMessenger::AddNonProtocolHandler(const InputMessageHandler& handler) {
- if (handler.parse == nullptr || handler.process == nullptr
- || handler.name == nullptr) {
- CHECK(false) << "Invalid argument";
- return -1;
- }
- BAIDU_SCOPED_LOCK(_add_handler_mutex);
- if (nullptr == _handlers) {
- _handlers = new InputMessageHandler[_capacity];
- memset(_handlers, 0, sizeof(*_handlers) * _capacity);
- _non_protocol = true;
- }
- if (!_non_protocol) {
- CHECK(false) << "AddHandler was invoked";
- return -1;
- }
- const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+int InputMessenger::AddHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = false;
+ }
+ if (_non_protocol) {
+ CHECK(false) << "AddNonProtocolHandler was invoked";
+ return -1;
+ }
+ ProtocolType type = FindProtocolOfHandler(handler);
+ if (type == PROTOCOL_UNKNOWN) {
+ CHECK(false) << "Adding a handler which doesn't belong to any protocol";
+ return -1;
+ }
+ const int index = type;
+ if (index >= (int)_capacity) {
+ LOG(FATAL) << "Can't add more handlers than " << _capacity;
+ return -1;
+ }
+ if (_handlers[index].parse == nullptr) {
+ // The same protocol might be added more than twice
_handlers[index] = handler;
+ } else if (_handlers[index].parse != handler.parse ||
+ _handlers[index].process != handler.process) {
+ CHECK(_handlers[index].parse == handler.parse);
+ CHECK(_handlers[index].process == handler.process);
+ return -1;
+ }
+ if (index > _max_index.load(butil::memory_order_relaxed)) {
_max_index.store(index, butil::memory_order_release);
- return 0;
+ }
+ return 0;
}
-int InputMessenger::Create(const butil::EndPoint& remote_side,
- time_t health_check_interval_s,
- SocketId* id) {
- SocketOptions options;
- options.remote_side = remote_side;
- options.user = this;
- options.on_edge_triggered_events = OnNewMessages;
- options.health_check_interval_s = health_check_interval_s;
- if (FLAGS_socket_keepalive) {
- options.keepalive_options = std::make_shared<SocketKeepaliveOptions>();
- options.keepalive_options->keepalive_idle_s
- = FLAGS_socket_keepalive_idle_s;
- options.keepalive_options->keepalive_interval_s
- = FLAGS_socket_keepalive_interval_s;
- options.keepalive_options->keepalive_count
- = FLAGS_socket_keepalive_count;
- }
- options.tcp_user_timeout_ms = FLAGS_socket_tcp_user_timeout_ms;
- return Socket::Create(options, id);
+int InputMessenger::AddNonProtocolHandler(const InputMessageHandler &handler) {
+ if (handler.parse == nullptr || handler.process == nullptr ||
+ handler.name == nullptr) {
+ CHECK(false) << "Invalid argument";
+ return -1;
+ }
+ BAIDU_SCOPED_LOCK(_add_handler_mutex);
+ if (nullptr == _handlers) {
+ _handlers = new InputMessageHandler[_capacity];
+ memset(_handlers, 0, sizeof(*_handlers) * _capacity);
+ _non_protocol = true;
+ }
+ if (!_non_protocol) {
+ CHECK(false) << "AddHandler was invoked";
+ return -1;
+ }
+ const int index = _max_index.load(butil::memory_order_relaxed) + 1;
+ _handlers[index] = handler;
+ _max_index.store(index, butil::memory_order_release);
+ return 0;
}
Review Comment:
AddNonProtocolHandler writes `_handlers[index]` without checking `index < _capacity`, which can cause out-of-bounds writes when multiple non-protocol handlers are added. Add the same capacity guard used in `AddHandler` (e.g., fatal/return error when `index >= (int)_capacity`).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r4001849912
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
Review Comment:
#include "brpc/urma/urma_api.h"
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+ _input_processor.Init(s, InputMessengerProcessor::STREAM_URMA_JETTY);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _input_processor.Reset();
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
+ if (nr == 0) {
+ errno = EEOF;
+ return -1;
+ }
+ received += nr;
+ }
+ return 0;
+}
+
+void UrmaEndpoint::PushBackToReadBuf(const void* data, size_t len) {
+ _socket->fd_input_processor().read_buf().append(data, len);
+}
+
+int UrmaEndpoint::WriteToFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t written = 0;
+ while (written < len) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = write(fd, p + written, len - written);
+ if (nw >= 0) {
+ written += nw;
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+// ============================================================================
+// Hello builders / parsers.
+// ============================================================================
+
+void UrmaEndpoint::MakeLocalParsedHello(ParsedHello* out) const {
+ *out = ParsedHello{}; // value-initialize (avoids memset on non-trivial type)
+ out->buffer_size = static_cast<uint32_t>(GetUrmaRecvBlockSize());
+ out->recv_buffer_cnt = _rq_size - 1;
+ if (_resource && _resource->jetty) {
+ out->jetty_id = _resource->jetty->jetty_id.id;
+ out->uasid = _resource->jetty->jetty_id.uasid;
+ const urma_eid_t* local_eid = GetUrmaLocalEid();
+ const uint8_t* advertised_eid =
+ local_eid != nullptr
+ ? local_eid->raw
+ : _resource->jetty->jetty_id.eid.raw;
+ std::memcpy(out->eid, advertised_eid, 16);
+ }
+ out->tp_type = static_cast<uint8_t>(URMA_CTP);
+ // Pool segment: flatten g_pool_seg's seg fields.
+ urma_target_seg_t* pool = GetPoolSegFor(nullptr);
+ if (pool) {
+ std::memcpy(out->seg_eid, pool->seg.ubva.eid.raw, 16);
+ out->seg_uasid = pool->seg.ubva.uasid;
+ out->seg_va = pool->seg.ubva.va;
+ out->seg_len = pool->seg.len;
+ out->seg_token_id = pool->seg.token_id;
+ }
+}
+
+void UrmaEndpoint::FillLocalHelloV2(v2_wire::HelloMessage* out) const {
Review Comment:
Why are there two versions (v2 and v3) of the new transport?
##########
src/brpc/urma/urma_helper.h:
##########
@@ -0,0 +1,119 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#ifndef BRPC_URMA_HELPER_H
+#define BRPC_URMA_HELPER_H
+
+#include <cstddef>
+#include <cstdint>
+#include <functional>
+#include <string>
+
+#include "bthread/types.h"
+#include "butil/atomicops.h"
+
+#if BRPC_WITH_URMA
+
+#include "urma_api.h"
+#include "urma_types.h"
Review Comment:
#include "brpc/urma/urma_api.h"
#include "brpc/urma/urma_types.h"
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5235154759
> I'm not familiar with URMA, but it seems that the code in is similar to , is that anything we can reuse?`src/brpc/urma``src/brpc/rdma`
Thanks for pointing this out. The URMA implementation was developed following the overall logic of the existing RDMA transport, while the underlying implementation details are different. We’re reviewing the two implementations now to see whether there are common parts that can be reused or factored out.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3950806406
##########
src/brpc/input_messenger_processor.cpp:
##########
@@ -15,279 +15,285 @@
// specific language governing permissions and limitations
// under the License.
-#include "butil/logging.h"
-#include "butil/binary_printer.h"
-#include "bthread/unstable.h"
-#include "brpc/options.pb.h"
-#include "brpc/transport.h"
#include "brpc/input_messenger_processor.h"
#include "brpc/input_messenger.h"
+#include "brpc/options.pb.h"
+#include "brpc/transport.h"
+#include "bthread/unstable.h"
+#include "butil/binary_printer.h"
+#include "butil/logging.h"
+
namespace brpc {
DECLARE_uint64(max_body_size);
-const size_t MSG_SIZE_WINDOW = 10; // Take last so many message into stat.
+const size_t MSG_SIZE_WINDOW = 10; // Take last so many message into stat.
const size_t MIN_ONCE_READ = 4096;
const size_t MAX_ONCE_READ = 524288;
-static const char* StreamTypeName(InputMessengerProcessor::StreamType type) {
- switch (type) {
- case InputMessengerProcessor::STREAM_NONE: return "none";
- case InputMessengerProcessor::STREAM_TCP_FD: return "tcp_fd";
- case InputMessengerProcessor::STREAM_RDMA_QP: return "rdma_qp";
- }
- return "unknown";
+static const char *StreamTypeName(InputMessengerProcessor::StreamType type) {
+ switch (type) {
+ case InputMessengerProcessor::STREAM_NONE:
+ return "none";
+ case InputMessengerProcessor::STREAM_TCP_FD:
+ return "tcp_fd";
+ case InputMessengerProcessor::STREAM_RDMA_QP:
+ return "rdma_qp";
+ }
+ return "unknown";
}
-InputMessengerProcessor::ParsingStreamGuard::ParsingStreamGuard(Socket* socket, StreamType type)
+InputMessengerProcessor::ParsingStreamGuard::ParsingStreamGuard(Socket *socket,
+ StreamType type)
: _socket(socket) {
- CHECK(socket != nullptr)
- << "Parsing through a processor that was never Init()ed, socket is NULL";
- CHECK_NE(STREAM_NONE, type)
- << "Parsing through a processor that was never Init()ed, " << *socket;
- CHECK_EQ(STREAM_NONE, socket->parsing_stream_type())
- << "Two input streams of " << *socket << " are parsing at the same time: "
- << StreamTypeName(socket->parsing_stream_type()) << " and "
- << StreamTypeName(type);
- socket->set_parsing_stream_type(type);
+ CHECK(socket != nullptr)
+ << "Parsing through a processor that was never Init()ed, socket is NULL";
+ CHECK_NE(STREAM_NONE, type)
+ << "Parsing through a processor that was never Init()ed, " << *socket;
+ CHECK_EQ(STREAM_NONE, socket->parsing_stream_type())
+ << "Two input streams of " << *socket << " are parsing at the same time: "
+ << StreamTypeName(socket->parsing_stream_type()) << " and "
+ << StreamTypeName(type);
+ socket->set_parsing_stream_type(type);
}
InputMessengerProcessor::ParsingStreamGuard::~ParsingStreamGuard() {
- _socket->set_parsing_stream_type(STREAM_NONE);
+ _socket->set_parsing_stream_type(STREAM_NONE);
}
-ParseResult InputMessengerProcessor::CutInputMessage(InputMessenger* messenger,
- size_t* index, bool read_eof) {
- ParsingStreamGuard parsing_stream_guard(_socket, _stream_type);
- InputMessageHandler* handlers = messenger->_handlers;
- int preferred = _socket->preferred_index();
- int max_index = (int)messenger->_max_index.load(butil::memory_order_acquire);
- // Try preferred handler first. The preferred_index is set on last
- // selection or by client.
- if (preferred >= 0 && preferred <= max_index
- && handlers[preferred].parse != nullptr) {
- int cur_index = preferred;
- do {
- ParseResult result =
- handlers[cur_index].parse(&_read_buf, _socket, read_eof,
- handlers[cur_index].arg);
- if (result.is_ok() ||
- result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
- _socket->set_preferred_index(cur_index);
- *index = cur_index;
- return result;
- } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
- // Critical error, return directly.
- LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
- << "A message from " << _socket->remote_side()
- << "(protocol=" << handlers[cur_index].name
- << ") is bigger than " << FLAGS_max_body_size
- << " bytes, the connection will be closed."
- " Set max_body_size to allow bigger messages";
- return result;
- }
+ParseResult InputMessengerProcessor::CutInputMessage(InputMessenger *messenger,
+ size_t *index,
+ bool read_eof) {
+ ParsingStreamGuard parsing_stream_guard(_socket, _stream_type);
+ InputMessageHandler *handlers = messenger->_handlers;
+ int preferred = _socket->preferred_index();
+ int max_index = (int)messenger->_max_index.load(butil::memory_order_acquire);
+ // Try preferred handler first. The preferred_index is set on last
+ // selection or by client.
+ if (preferred >= 0 && preferred <= max_index &&
+ handlers[preferred].parse != nullptr) {
+ int cur_index = preferred;
+ do {
+ ParseResult result = handlers[cur_index].parse(
+ &_read_buf, _socket, read_eof, handlers[cur_index].arg);
+ if (result.is_ok() || result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
+ _socket->set_preferred_index(cur_index);
+ *index = cur_index;
+ return result;
+ } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
+ // Critical error, return directly.
+ LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
+ << "A message from " << _socket->remote_side()
+ << "(protocol=" << handlers[cur_index].name << ") is bigger than "
+ << FLAGS_max_body_size
+ << " bytes, the connection will be closed."
+ " Set max_body_size to allow bigger messages";
+ return result;
+ }
- if (_socket->CreatedByConnect()) {
- if((ProtocolType)cur_index == PROTOCOL_BAIDU_STD && cur_index == preferred) {
- // baidu_std may fall to streaming_rpc.
- cur_index = (int)PROTOCOL_STREAMING_RPC;
- continue;
- } else if((ProtocolType)cur_index == PROTOCOL_STREAMING_RPC &&
- cur_index == preferred) {
- // streaming_rpc may fall to baidu_std.
- cur_index = (int)PROTOCOL_BAIDU_STD;
- continue;
- } else {
- // The protocol is fixed at client-side, no need to try others.
- LOG(ERROR) << "Fail to parse response from " << _socket->remote_side()
- << " by " << handlers[preferred].name
- << " at client-side";
- return MakeParseError(PARSE_ERROR_ABSOLUTELY_WRONG);
- }
- } else {
- // Try other protocols.
- //
- // A handler may lean on this: returning PARSE_ERROR_NOT_ENOUGH_DATA
- // above keeps `preferred_index' pinned on it, TRY_OTHERS here gives
- // it up. RdmaEndpoint::ExecuteServerHandshake() pins itself that way
- // to keep the last handshake read from being taken for a protocol
- // detection. See the tail of its phase 2 before changing what
- // happens to `preferred_index' here.
- break;
- }
- } while (true);
- // Clear context before trying next protocol which probably has
- // an incompatible context with the current one.
- if (_socket->parsing_context()) {
- _socket->reset_parsing_context(nullptr);
- }
- _socket->set_preferred_index(-1);
- }
- for (int i = 0; i <= max_index; ++i) {
- if (i == preferred || handlers[i].parse == nullptr) {
- // Don't try preferred handler(already tried) or invalid handler
- continue;
- }
- ParseResult result = handlers[i].parse(&_read_buf, _socket, read_eof, handlers[i].arg);
- if (result.is_ok() ||
- result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
- _socket->set_preferred_index(i);
- *index = i;
- return result;
- } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
- // Critical error, return directly.
- LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
- << "A message from " << _socket->remote_side()
- << "(protocol=" << handlers[i].name
- << ") is bigger than " << FLAGS_max_body_size
- << " bytes, the connection will be closed."
- " Set max_body_size to allow bigger messages";
- return result;
- }
- // Clear context before trying next protocol which definitely has
- // an incompatible context with the current one.
- if (_socket->parsing_context()) {
- _socket->reset_parsing_context(nullptr);
+ if (_socket->CreatedByConnect()) {
+ if ((ProtocolType)cur_index == PROTOCOL_BAIDU_STD &&
+ cur_index == preferred) {
+ // baidu_std may fall to streaming_rpc.
+ cur_index = (int)PROTOCOL_STREAMING_RPC;
+ continue;
+ } else if ((ProtocolType)cur_index == PROTOCOL_STREAMING_RPC &&
+ cur_index == preferred) {
+ // streaming_rpc may fall to baidu_std.
+ cur_index = (int)PROTOCOL_BAIDU_STD;
+ continue;
+ } else {
+ // The protocol is fixed at client-side, no need to try others.
+ LOG(ERROR) << "Fail to parse response from " << _socket->remote_side()
+ << " by " << handlers[preferred].name << " at client-side";
+ return MakeParseError(PARSE_ERROR_ABSOLUTELY_WRONG);
}
+ } else {
// Try other protocols.
+ //
+ // A handler may lean on this: returning PARSE_ERROR_NOT_ENOUGH_DATA
+ // above keeps `preferred_index' pinned on it, TRY_OTHERS here gives
+ // it up. RdmaEndpoint::ExecuteServerHandshake() pins itself that way
+ // to keep the last handshake read from being taken for a protocol
+ // detection. See the tail of its phase 2 before changing what
+ // happens to `preferred_index' here.
+ break;
+ }
+ } while (true);
+ // Clear context before trying next protocol which probably has
+ // an incompatible context with the current one.
+ if (_socket->parsing_context()) {
+ _socket->reset_parsing_context(nullptr);
+ }
+ _socket->set_preferred_index(-1);
+ }
+ for (int i = 0; i <= max_index; ++i) {
+ if (i == preferred || handlers[i].parse == nullptr) {
+ // Don't try preferred handler(already tried) or invalid handler
+ continue;
}
- return MakeParseError(PARSE_ERROR_TRY_OTHERS);
+ ParseResult result =
+ handlers[i].parse(&_read_buf, _socket, read_eof, handlers[i].arg);
+ if (result.is_ok() || result.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
+ _socket->set_preferred_index(i);
+ *index = i;
+ return result;
+ } else if (result.error() != PARSE_ERROR_TRY_OTHERS) {
+ // Critical error, return directly.
+ LOG_IF(ERROR, result.error() == PARSE_ERROR_TOO_BIG_DATA)
+ << "A message from " << _socket->remote_side()
+ << "(protocol=" << handlers[i].name << ") is bigger than "
+ << FLAGS_max_body_size
+ << " bytes, the connection will be closed."
+ " Set max_body_size to allow bigger messages";
+ return result;
+ }
+ // Clear context before trying next protocol which definitely has
+ // an incompatible context with the current one.
+ if (_socket->parsing_context()) {
+ _socket->reset_parsing_context(nullptr);
+ }
+ // Try other protocols.
+ }
+ return MakeParseError(PARSE_ERROR_TRY_OTHERS);
}
size_t InputMessengerProcessor::OnceReadSize() const {
- size_t once_read = _avg_msg_size * 16;
- if (once_read < MIN_ONCE_READ) {
- once_read = MIN_ONCE_READ;
- } else if (once_read > MAX_ONCE_READ) {
- once_read = MAX_ONCE_READ;
- }
- return once_read;
+ size_t once_read = _avg_msg_size * 16;
+ if (once_read < MIN_ONCE_READ) {
+ once_read = MIN_ONCE_READ;
+ } else if (once_read > MAX_ONCE_READ) {
+ once_read = MAX_ONCE_READ;
+ }
+ return once_read;
}
void InputMessengerProcessor::Reset() {
- _read_buf.clear();
- _last_msg_size = 0;
- _avg_msg_size = 0;
+ _read_buf.clear();
+ _last_msg_size = 0;
+ _avg_msg_size = 0;
}
int InputMessengerProcessor::ProcessNewMessage(ssize_t bytes, bool read_eof,
uint64_t received_us,
uint64_t base_realtime,
- InputMessageClosure& last_msg) {
- auto messenger = static_cast<InputMessenger*>(_socket->user());
- const InputMessageHandler* handlers = messenger->_handlers;
- _socket->AddInputBytes(bytes);
+ InputMessageClosure &last_msg) {
+ auto messenger = static_cast<InputMessenger *>(_socket->user());
+ const InputMessageHandler *handlers = messenger->_handlers;
+ _socket->AddInputBytes(bytes);
- // Avoid this socket to be closed due to idle_timeout_s
- _socket->_last_readtime_us.store(received_us, butil::memory_order_relaxed);
+ // Avoid this socket to be closed due to idle_timeout_s
+ _socket->_last_readtime_us.store(received_us, butil::memory_order_relaxed);
- size_t last_size = _read_buf.length();
- int num_bthread_created = 0;
- while (true) {
- size_t index = 8888;
- ParseResult pr = CutInputMessage(messenger, &index, read_eof);
- if (!pr.is_ok()) {
- if (pr.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
- // incomplete message, re-read.
- // However, some buffer may have been consumed
- // under protocols like HTTP. Record this size
- _last_msg_size += (last_size - _read_buf.length());
- break;
- } else if (pr.error() == PARSE_ERROR_TRY_OTHERS) {
- LOG(WARNING) << "Close " << *_socket << " due to unknown message: "
- << butil::ToPrintable(_read_buf);
- _socket->SetFailed(EINVAL, "Close %s due to unknown message",
- _socket->description().c_str());
- return -1;
- } else {
- LOG(WARNING) << "Close " << *_socket << ": " << pr.error_str();
- _socket->SetFailed(EINVAL, "Close %s: %s",
- _socket->description().c_str(), pr.error_str());
- return -1;
- }
- }
+ size_t last_size = _read_buf.length();
+ int num_bthread_created = 0;
+ while (true) {
+ size_t index = 8888;
+ ParseResult pr = CutInputMessage(messenger, &index, read_eof);
+ if (!pr.is_ok()) {
+ if (pr.error() == PARSE_ERROR_NOT_ENOUGH_DATA) {
+ // incomplete message, re-read.
+ // However, some buffer may have been consumed
+ // under protocols like HTTP. Record this size
+ _last_msg_size += (last_size - _read_buf.length());
+ break;
+ } else if (pr.error() == PARSE_ERROR_TRY_OTHERS) {
+ LOG(WARNING) << "Close " << *_socket << " due to unknown message: "
+ << butil::ToPrintable(_read_buf);
+ _socket->SetFailed(EINVAL, "Close %s due to unknown message",
+ _socket->description().c_str());
+ return -1;
+ } else {
+ LOG(WARNING) << "Close " << *_socket << ": " << pr.error_str();
+ _socket->SetFailed(EINVAL, "Close %s: %s",
+ _socket->description().c_str(), pr.error_str());
+ return -1;
+ }
+ }
- _socket->AddInputMessages(1);
- // Calculate average size of messages
- const size_t cur_size = _read_buf.length();
- if (cur_size == 0) {
- // _read_buf is consumed, it's good timing to return blocks
- // cached internally back to TLS, otherwise the memory is not
- // reused until next message arrives which is quite uncertain
- // in situations that most connections are idle.
- _read_buf.return_cached_blocks();
- }
- _last_msg_size += (last_size - cur_size);
- last_size = cur_size;
- const size_t old_avg = _avg_msg_size;
- if (old_avg != 0) {
- _avg_msg_size = (old_avg * (MSG_SIZE_WINDOW - 1) + _last_msg_size) / MSG_SIZE_WINDOW;
- } else {
- _avg_msg_size = _last_msg_size;
- }
- _last_msg_size = 0;
+ _socket->AddInputMessages(1);
+ // Calculate average size of messages
+ const size_t cur_size = _read_buf.length();
+ if (cur_size == 0) {
+ // _read_buf is consumed, it's good timing to return blocks
+ // cached internally back to TLS, otherwise the memory is not
+ // reused until next message arrives which is quite uncertain
+ // in situations that most connections are idle.
+ _read_buf.return_cached_blocks();
+ }
+ _last_msg_size += (last_size - cur_size);
+ last_size = cur_size;
+ const size_t old_avg = _avg_msg_size;
+ if (old_avg != 0) {
+ _avg_msg_size =
+ (old_avg * (MSG_SIZE_WINDOW - 1) + _last_msg_size) / MSG_SIZE_WINDOW;
+ } else {
+ _avg_msg_size = _last_msg_size;
+ }
+ _last_msg_size = 0;
- if (pr.message() == nullptr) { // the Process() step can be skipped.
- continue;
- }
- pr.message()->_received_us = received_us;
- pr.message()->_base_real_us = base_realtime;
+ if (pr.message() == nullptr) { // the Process() step can be skipped.
+ continue;
+ }
+ pr.message()->_received_us = received_us;
+ pr.message()->_base_real_us = base_realtime;
- // This unique_ptr prevents msg to be lost before transfering
- // ownership to last_msg
- DestroyingPtr<InputMessageBase> msg(pr.message());
- _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
- if (handlers[index].process == nullptr) {
- LOG(ERROR) << "process of index=" << index << " is NULL";
- continue;
- }
- _socket->ReAddress(&msg->_socket);
- _socket->PostponeEOF();
- msg->_process = handlers[index].process;
- msg->_arg = handlers[index].arg;
+ // This unique_ptr prevents msg to be lost before transfering
+ // ownership to last_msg
+ DestroyingPtr<InputMessageBase> msg(pr.message());
+ _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
+ if (handlers[index].process == nullptr) {
+ LOG(ERROR) << "process of index=" << index << " is NULL";
+ continue;
+ }
+ _socket->ReAddress(&msg->_socket);
+ _socket->PostponeEOF();
+ msg->_process = handlers[index].process;
+ msg->_arg = handlers[index].arg;
- if (handlers[index].verify != nullptr) {
- int auth_error = 0;
- if (0 == _socket->FightAuthentication(&auth_error)) {
- // Get the right to authenticate
- if (handlers[index].verify(msg.get())) {
- _socket->SetAuthentication(0);
- } else {
- _socket->SetAuthentication(ERPCAUTH);
- LOG(WARNING) << "Fail to authenticate " << *_socket;
- _socket->SetFailed(ERPCAUTH, "Fail to authenticate %s",
- _socket->description().c_str());
- return -1;
- }
- } else {
- LOG_IF(FATAL, auth_error != 0) <<
- "Impossible! Socket should have been "
- "destroyed when authentication failed";
- }
- }
- if (!_socket->is_read_progressive()) {
- // Transfer ownership to last_msg
- last_msg.reset(msg.release());
+ if (handlers[index].verify != nullptr) {
+ int auth_error = 0;
+ if (0 == _socket->FightAuthentication(&auth_error)) {
+ // Get the right to authenticate
+ if (handlers[index].verify(msg.get())) {
+ _socket->SetAuthentication(0);
} else {
- last_msg.reset(msg.release());
- _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
- bthread_flush();
- num_bthread_created = 0;
+ _socket->SetAuthentication(ERPCAUTH);
+ LOG(WARNING) << "Fail to authenticate " << *_socket;
+ _socket->SetFailed(ERPCAUTH, "Fail to authenticate %s",
+ _socket->description().c_str());
+ return -1;
}
+ } else {
+ LOG_IF(FATAL, auth_error != 0)
+ << "Impossible! Socket should have been "
+ "destroyed when authentication failed";
+ }
}
- // In RDMA polling mode, all messages must be executed in a new bthread and
- // not in the bthread where the polling bthread is located, because the
- // method for processing messages may call synchronization primitives,
- // causing the polling bthread to be scheduled out.
- if (_socket->_socket_mode == SOCKET_MODE_RDMA ||
- _socket->_socket_mode == SOCKET_MODE_UBRING) {
- _socket->_transport->QueueMessage(last_msg, &num_bthread_created, true);
- }
- if (num_bthread_created) {
- bthread_flush();
+ if (!_socket->is_read_progressive()) {
+ // Transfer ownership to last_msg
+ last_msg.reset(msg.release());
+ } else {
+ last_msg.reset(msg.release());
+ _socket->_transport->QueueMessage(last_msg, &num_bthread_created, false);
+ bthread_flush();
+ num_bthread_created = 0;
}
- return 0;
+ }
+ // In RDMA polling mode, all messages must be executed in a new bthread and
+ // not in the bthread where the polling bthread is located, because the
+ // method for processing messages may call synchronization primitives,
+ // causing the polling bthread to be scheduled out.
Review Comment:
The comment now says “In RDMA polling mode” but the condition includes `SOCKET_MODE_UBRING` and `SOCKET_MODE_URMA` as well. Please update the comment to reflect the actual behavior (e.g., “In polling-mode transports (RDMA/UBRING/URMA) ...” or clarify why URMA also requires this even when not polling).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5460960780
Will this issue : https://github.com/apache/brpc/discussions/3432 resolved in this PR, or in further PR?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "dwh110 (via GitHub)" <gi...@apache.org>.
dwh110 commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3781084110
##########
docs/cn/urma.md:
##########
@@ -0,0 +1,149 @@
+# UrmaTransport:基于 URMA 的远程内存 RPC
+
+UrmaTransport 是使用 openEuler
+[URMA](https://atomgit.com/openeuler/umdk)(Unified Remote Memory Access)SDK
+实现的传输层。它是
+[#3217](https://github.com/apache/brpc/discussions/3217) 中提出的路线 B,
+与基于 OBMM 的 UBRing 传输(路线 A,
+[#3226](https://github.com/apache/brpc/issues/3226))互补,承担大包/跨节点
+高吞吐场景,共同构成路线 C(双后端)。
+
+## 技术背景
+
+URMA 在 UMDK 支持的设备上提供 verbs 风格接口。当前实现创建可靠消息
+(`URMA_TM_RM`)Jetty,并使用 CTP 传输路径;通过
+`urma_post_jetty_send_wr` 提交发送 WR,通过 `urma_post_jfr_wr` 提交接收
+WR。完成事件既可由 JFC 忙轮询获取,也可通过 JFCE 事件 fd 获取。
+
+## 编译配置
+
+### CMake 编译
+
+```bash
+# 带 URMA 支持编译 brpc
+cmake -B build -DWITH_URMA=ON
+make -C build -j$(nproc)
+
+# 编译 urma_performance 示例
+cd example/urma_performance
+cmake -B build
+make -C build -j$(nproc)
+```
+
+`WITH_URMA=ON` 使用上游 UMDK 头文件进行编译。CMake 优先使用系统安装的
+SDK;找不到头文件时,会参照 Mooncake 的 mock 构建方式下载固定版本的
+UMDK,可通过 `DOWNLOAD_URMA_HEADERS=OFF` 禁止下载。找到 `liburma` 时使用
Review Comment:
DOWNLOAD_URMA_HEADERS lacks Bazel build support
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Gzure (via GitHub)" <gi...@apache.org>.
Gzure commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r4013411476
##########
src/brpc/urma/urma_helper.h:
##########
@@ -0,0 +1,119 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#ifndef BRPC_URMA_HELPER_H
+#define BRPC_URMA_HELPER_H
+
+#include <cstddef>
+#include <cstdint>
+#include <functional>
+#include <string>
+
+#include "bthread/types.h"
+#include "butil/atomicops.h"
+
+#if BRPC_WITH_URMA
+
+#include "urma_api.h"
+#include "urma_types.h"
Review Comment:
The Urma header file dependency environment has the urma-devel package installed, but the Urma header files are not placed in brpc.
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
Review Comment:
The Urma header file dependency environment has the urma-devel package installed, but the Urma header files are not placed in brpc.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r4013456302
##########
src/brpc/urma/urma_endpoint.cpp:
##########
@@ -0,0 +1,1849 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_endpoint.h"
+
+#if BRPC_WITH_URMA
+
+#include <sys/resource.h>
+#include <unistd.h>
+
+#include <algorithm>
+#include <cstdio>
+#include <cstring>
+#include <memory>
+#include <mutex>
+#include <string>
+#include <unordered_set>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
+#include "butil/macros.h"
+#include "butil/sys_byteorder.h"
+#include "butil/time.h"
+#include "bthread/bthread.h"
+#include "bthread/butex.h"
+
+#include "urma_api.h"
+
+#include "brpc/input_messenger.h"
+#include "brpc/socket.h"
+#include "brpc/urma/urma_bonding.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "brpc/urma_transport.h"
+
+DECLARE_int32(task_group_ntags);
+
+namespace brpc {
+namespace urma {
+
+// Flags used here are declared in urma_endpoint.h (urma_use_polling,
+// urma_poller_num, urma_disable_bthread). Declare the rest here.
+DECLARE_int32(urma_sq_size);
+DECLARE_int32(urma_rq_size);
+DECLARE_int32(urma_cqe_poll_once);
+DECLARE_bool(urma_recv_zerocopy);
+DECLARE_int32(urma_zerocopy_min_size);
+DECLARE_int32(urma_prepared_jetty_cnt);
+DECLARE_bool(urma_poller_yield);
+
+// ---- Constants shared with the handshake module ----
+static const int WAIT_TIMEOUT_MS = 50;
+static const size_t HELLO_ACK_LEN = 4;
+static const uint32_t HELLO_ACK_URMA_OK = 0x1;
+static const size_t IOBUF_BLOCK_HEADER_LEN = sizeof(butil::IOBuf::Block);
+
+// ---- Globals: prepared jetty pool + poller groups ----
+struct PreparedJetty {
+ UrmaResource* res;
+};
+static butil::Mutex g_prepared_mutex;
+static UrmaResource* g_prepared_list = nullptr; // singly-linked
+static int g_prepared_cnt = 0;
+
+static int PreparedJettyCount() {
+ const int requested =
+ std::max(0, std::min(FLAGS_urma_prepared_jetty_cnt, 1024));
+ if (requested == 0) {
+ return 0;
+ }
+
+ struct rlimit nofile;
+ if (getrlimit(RLIMIT_NOFILE, &nofile) != 0 ||
+ nofile.rlim_cur == RLIM_INFINITY) {
+ return requested;
+ }
+
+ // In event mode each prepared JFCE consumes a file descriptor. Keep room
+ // for one TCP fd per future URMA connection and for brpc/system internals.
+ static const rlim_t kReservedFdCount = 64;
+ const rlim_t max_prepared =
+ nofile.rlim_cur > kReservedFdCount
+ ? (nofile.rlim_cur - kReservedFdCount) / 2
+ : 0;
+ if (max_prepared >= static_cast<rlim_t>(requested)) {
+ return requested;
+ }
+
+ LOG(WARNING) << "Cap URMA prepared jetty count from " << requested
+ << " to " << max_prepared
+ << " due to RLIMIT_NOFILE=" << nofile.rlim_cur;
+ return static_cast<int>(max_prepared);
+}
+
+std::vector<UrmaEndpoint::PollerGroup> UrmaEndpoint::_poller_groups;
+
+// ============================================================================
+// UrmaResource lifecycle.
+// ============================================================================
+
+UrmaResource::~UrmaResource() {
+ if (remote_jetty) {
+ urma_unimport_jetty(remote_jetty);
+ }
+ if (remote_seg) {
+ urma_unimport_seg(remote_seg);
+ }
+ if (jetty) {
+ urma_delete_jetty(jetty);
+ }
+ if (jfr) {
+ urma_delete_jfr(jfr);
+ }
+ if (jfc) {
+ urma_delete_jfc(jfc);
+ }
+ if (jfce) {
+ urma_delete_jfce(jfce);
+ }
+}
+
+// ============================================================================
+// Constructor / destructor / Reset.
+// ============================================================================
+
+UrmaEndpoint::UrmaEndpoint(Socket* s)
+ : _socket(s),
+ _state(UNINIT),
+ _handshake_version(0),
+ _resource(nullptr) {
+ _sq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_sq_size))));
+ _rq_size = static_cast<uint16_t>(
+ std::max(16, std::min(4096, static_cast<int>(FLAGS_urma_rq_size))));
+ _read_butex = bthread::butex_create_checked<butil::atomic<int>>();
+ _read_butex->store(0, butil::memory_order_relaxed);
+ _input_processor.Init(s, InputMessengerProcessor::STREAM_URMA_JETTY);
+}
+
+UrmaEndpoint::~UrmaEndpoint() {
+ DeallocateResources();
+ if (_read_butex) {
+ bthread::butex_destroy(_read_butex);
+ _read_butex = nullptr;
+ }
+}
+
+void UrmaEndpoint::Reset() {
+ DeallocateResources();
+ _state = UNINIT;
+ _handshake_version = 0;
+ _remote_recv_block_size = 0;
+ _local_window_capacity = 0;
+ _remote_window_capacity = 0;
+ _remote_rq_window_size.store(0, butil::memory_order_relaxed);
+ _sq_window_size.store(0, butil::memory_order_relaxed);
+ _new_rq_wrs.store(0, butil::memory_order_relaxed);
+ _sq_imm_window_size = 0;
+ _sq_current = 0;
+ _sq_sent = 0;
+ _rq_received = 0;
+ _pending_received_bytes.store(0, butil::memory_order_relaxed);
+ _input_processor.Reset();
+ _sbuf.clear();
+ _rbuf.clear();
+ _rbuf_data.clear();
+ _read_butex->store(0, butil::memory_order_relaxed);
+}
+
+// ============================================================================
+// Handshake IO helpers (ReadFromFd / WriteToFd / PushBackToReadBuf).
+// Modeled on RdmaEndpoint::ReadFromFdLoop / WriteToFdLoop.
+// ============================================================================
+
+int UrmaEndpoint::ReadFromFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t received = 0;
+ while (received < len) {
+ const int expected_val = _read_butex->load(butil::memory_order_acquire);
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nr = read(fd, p + received, len - received);
+ if (nr < 0) {
+ if (errno == EAGAIN || errno == EWOULDBLOCK) {
+ int rc = bthread::butex_wait(_read_butex, expected_val, &duetime);
+ if (rc < 0 && errno != EWOULDBLOCK && errno != ETIMEDOUT) {
+ return -1;
+ }
+ continue;
+ }
+ return -1;
+ }
+ if (nr == 0) {
+ errno = EEOF;
+ return -1;
+ }
+ received += nr;
+ }
+ return 0;
+}
+
+void UrmaEndpoint::PushBackToReadBuf(const void* data, size_t len) {
+ _socket->fd_input_processor().read_buf().append(data, len);
+}
+
+int UrmaEndpoint::WriteToFd(void* data, size_t len) {
+ char* p = static_cast<char*>(data);
+ size_t written = 0;
+ while (written < len) {
+ const timespec duetime = butil::milliseconds_from_now(WAIT_TIMEOUT_MS);
+ const int fd = _socket->fd();
+ const ssize_t nw = write(fd, p + written, len - written);
+ if (nw >= 0) {
+ written += nw;
+ continue;
+ }
+ if (errno != EAGAIN && errno != EWOULDBLOCK) {
+ return -1;
+ }
+ if (_socket->WaitEpollOut(fd, true, &duetime) != 0 && errno != ETIMEDOUT) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
+// ============================================================================
+// Hello builders / parsers.
+// ============================================================================
+
+void UrmaEndpoint::MakeLocalParsedHello(ParsedHello* out) const {
+ *out = ParsedHello{}; // value-initialize (avoids memset on non-trivial type)
+ out->buffer_size = static_cast<uint32_t>(GetUrmaRecvBlockSize());
+ out->recv_buffer_cnt = _rq_size - 1;
+ if (_resource && _resource->jetty) {
+ out->jetty_id = _resource->jetty->jetty_id.id;
+ out->uasid = _resource->jetty->jetty_id.uasid;
+ const urma_eid_t* local_eid = GetUrmaLocalEid();
+ const uint8_t* advertised_eid =
+ local_eid != nullptr
+ ? local_eid->raw
+ : _resource->jetty->jetty_id.eid.raw;
+ std::memcpy(out->eid, advertised_eid, 16);
+ }
+ out->tp_type = static_cast<uint8_t>(URMA_CTP);
+ // Pool segment: flatten g_pool_seg's seg fields.
+ urma_target_seg_t* pool = GetPoolSegFor(nullptr);
+ if (pool) {
+ std::memcpy(out->seg_eid, pool->seg.ubva.eid.raw, 16);
+ out->seg_uasid = pool->seg.ubva.uasid;
+ out->seg_va = pool->seg.ubva.va;
+ out->seg_len = pool->seg.len;
+ out->seg_token_id = pool->seg.token_id;
+ }
+}
+
+void UrmaEndpoint::FillLocalHelloV2(v2_wire::HelloMessage* out) const {
Review Comment:
I don't think it's necessary to support v2 for the new transports.
##########
src/brpc/urma/urma_helper.h:
##########
@@ -0,0 +1,119 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#ifndef BRPC_URMA_HELPER_H
+#define BRPC_URMA_HELPER_H
+
+#include <cstddef>
+#include <cstdint>
+#include <functional>
+#include <string>
+
+#include "bthread/types.h"
+#include "butil/atomicops.h"
+
+#if BRPC_WITH_URMA
+
+#include "urma_api.h"
+#include "urma_types.h"
Review Comment:
ok
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango merged PR #3428:
URL: https://github.com/apache/brpc/pull/3428
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3871821777
##########
docs/cn/urma.md:
##########
@@ -0,0 +1,149 @@
+# UrmaTransport:基于 URMA 的远程内存 RPC
+
+UrmaTransport 是使用 openEuler
+[URMA](https://atomgit.com/openeuler/umdk)(Unified Remote Memory Access)SDK
+实现的传输层。它是
+[#3217](https://github.com/apache/brpc/discussions/3217) 中提出的路线 B,
+与基于 OBMM 的 UBRing 传输(路线 A,
+[#3226](https://github.com/apache/brpc/issues/3226))互补,承担大包/跨节点
+高吞吐场景,共同构成路线 C(双后端)。
+
+## 技术背景
+
+URMA 在 UMDK 支持的设备上提供 verbs 风格接口。当前实现创建可靠消息
+(`URMA_TM_RM`)Jetty,并使用 CTP 传输路径;通过
+`urma_post_jetty_send_wr` 提交发送 WR,通过 `urma_post_jfr_wr` 提交接收
+WR。完成事件既可由 JFC 忙轮询获取,也可通过 JFCE 事件 fd 获取。
+
+## 编译配置
+
+### CMake 编译
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "zchuango (via GitHub)" <gi...@apache.org>.
zchuango commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3872164969
##########
src/brpc/urma/mock_urma.cpp:
##########
@@ -0,0 +1,713 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// Link-time mock of the URMA user-level C API, modeled on Mooncake's
+// mock_urma.cpp. It compiles against upstream UMDK headers and is linked into
+// brpc when liburma.so is unavailable. It lets CI run UrmaTransport unit tests
+// without URMA hardware.
+//
+// Design (same as Mooncake):
+// - Link-time substitution: the symbols are literally named urma_create_jfc
+// etc.; the linker picks this TU when liburma is absent.
+// - Per-object state is held in anonymous-namespace maps keyed on the opaque
+// pointer returned to the caller. Membership is checked on delete so misuse
+// returns URMA_EINVAL rather than crashes.
+// - Completion path: posted recv WRs remain pending until a SEND targets the
+// corresponding mock jetty. Payload and immediate data are copied into the
+// remote recv WR and both local-send and remote-recv completions are queued.
+// - Device-name contract: device->name == "mock_urma_device" so tests can
+// match it with --urma_device=mock_urma_device.
+
+#if BRPC_WITH_URMA
+
+#include "urma_api.h"
+
+#include <algorithm>
+#include <atomic>
+#include <cerrno>
+#include <cstring>
+#include <deque>
+#include <map>
+#include <mutex>
+#include <shared_mutex>
+#include <sys/eventfd.h>
+#include <unistd.h>
+#include <vector>
+
+namespace {
+
+struct JfcState {
+ std::mutex mutex;
+ std::deque<urma_cr_t> completions;
+ bool event_pending{false};
+};
+
+struct PendingRecv {
+ uint64_t addr;
+ uint32_t len;
+ uint64_t user_ctx;
+};
+
+std::shared_mutex g_rw_mutex;
+bool initialized = false;
+std::vector<urma_device_t *> device_list;
+std::map<urma_context_t *, int> context_map;
+std::map<urma_jfce_t *, int> jfce_map;
+std::map<urma_jfc_t *, JfcState *> jfc_state_map;
+std::map<urma_jfr_t *, int> jfr_map;
+// Side-table: JFR -> the JFC it was created with (used to route recv
+// completions to the right JfcState, since the JFR is an opaque handle).
+std::map<urma_jfr_t *, urma_jfc_t *> jfr_jfc_map;
+std::map<urma_jfr_t *, std::deque<PendingRecv>> jfr_recv_map;
+std::map<urma_target_seg_t *, int> seg_map;
+std::map<urma_jetty_t *, int> jetty_map;
+std::map<uint32_t, urma_jetty_t *> jetty_id_map;
+std::map<urma_target_jetty_t *, int> target_jetty_map;
+std::atomic<uint32_t> next_jetty_id{1};
+
+void PushCompletion(urma_jfc_t* jfc, JfcState* state,
+ const urma_cr_t& completion) {
+ bool signal = false;
+ {
+ std::lock_guard<std::mutex> lock(state->mutex);
+ state->completions.push_back(completion);
+ if (!state->event_pending) {
+ state->event_pending = true;
+ signal = true;
+ }
+ }
+ if (signal && jfc && jfc->jfc_cfg.jfce &&
+ jfc->jfc_cfg.jfce->fd >= 0) {
+ uint64_t one = 1;
+ (void)write(jfc->jfc_cfg.jfce->fd, &one, sizeof(one));
+ }
+}
+
+urma_device_attr_t mock_device_attr = {
+ .guid = {.raw = {10}},
+ .dev_cap = {},
+ .port_cnt = 1,
+ .port_attr = {{.max_mtu = URMA_MTU_4096,
+ .state = URMA_PORT_ACTIVE,
+ .active_width = URMA_LINK_X1,
+ .active_speed = URMA_SP_100G,
+ .active_mtu = URMA_MTU_4096}},
+ .reserved_jetty_id_min = 0,
+ .reserved_jetty_id_max = 1024};
+
+urma_eid_info_t mock_eid_info = {
+ .eid = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
+ 0x0C, 0x0D, 0x0E, 0x0F, 0x10}},
+ .eid_index = 0};
+
+} // namespace
+
+extern "C" {
+
+urma_status_t urma_init(urma_init_attr_t *init_attr) {
Review Comment:
@dwh110 done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "dwh110 (via GitHub)" <gi...@apache.org>.
dwh110 commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3780899026
##########
docs/cn/urma.md:
##########
@@ -0,0 +1,149 @@
+# UrmaTransport:基于 URMA 的远程内存 RPC
+
+UrmaTransport 是使用 openEuler
+[URMA](https://atomgit.com/openeuler/umdk)(Unified Remote Memory Access)SDK
+实现的传输层。它是
+[#3217](https://github.com/apache/brpc/discussions/3217) 中提出的路线 B,
+与基于 OBMM 的 UBRing 传输(路线 A,
+[#3226](https://github.com/apache/brpc/issues/3226))互补,承担大包/跨节点
+高吞吐场景,共同构成路线 C(双后端)。
+
+## 技术背景
+
+URMA 在 UMDK 支持的设备上提供 verbs 风格接口。当前实现创建可靠消息
+(`URMA_TM_RM`)Jetty,并使用 CTP 传输路径;通过
+`urma_post_jetty_send_wr` 提交发送 WR,通过 `urma_post_jfr_wr` 提交接收
+WR。完成事件既可由 JFC 忙轮询获取,也可通过 JFCE 事件 fd 获取。
+
+## 编译配置
+
+### CMake 编译
Review Comment:
Bazel‑specific build instructions may be added.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5568481358
Overall the new UrmaTransport subsystem is well-structured and defensively written (handshake parsing bounds-checks, flow-control credits with overflow validation, robust TCP fallback). One concern in the Bazel build is worth addressing before merge.
- `BUILD.bazel:0` The cc_library `srcs` list appends `src/brpc/urma/mock_urma.cpp` unconditionally (in the base source list) and again in the `brpc_with_urma` select. Unlike the Makefile (`URMA_USE_MOCK=0` filters it out) and CMakeLists (`list(REMOVE_ITEM BRPC_SOURCES ... mock_urma.cpp )` when `NOT URMA_USE_MOCK_ENABLED`), the `brpc_with_urma_use_real` select maps to `[]` which does NOT remove the mock translation unit. Since mock_urma.cpp is guarded by `#if BRPC_WITH_URMA`, a real-liburma Bazel build (`--define=BRPC_WITH_URMA=true --define=BRPC_URMA_USE_MOCK=false -lurma`) would still compile and link the `urma_*` mock symbols and clash with the real `liburma`. Please exclude `mock_urma.cpp` under `brpc_with_urma_use_real` (e.g. via a `select` that maps the real case to an empty srcs list), matching the CMake/Make real-library behavior; also note the base list already has it so adding it again in `brpc_with_urma` is redundant.
---
🤖 This reply was automatically generated by brpc-oncall
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #3428:
URL: https://github.com/apache/brpc/pull/3428#discussion_r3743002447
##########
src/brpc/urma/urma_helper.cpp:
##########
@@ -0,0 +1,788 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "brpc/urma/urma_helper.h"
+
+#if BRPC_WITH_URMA
+
+#include <errno.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+#include <atomic>
+#include <new>
+#include <utility>
+#include <vector>
+
+#include <gflags/gflags.h>
+
+#include "butil/atomicops.h"
+#include "butil/containers/flat_map.h"
+#include "butil/iobuf.h"
+#include "butil/logging.h"
Review Comment:
This file hard-codes the IOBuf block header length as 32 bytes, but the actual overhead is sizeof(butil::IOBuf::Block). Hard-coding makes URMA buffer sizing fragile (e.g., if IOBuf::Block layout changes or on non-64bit builds). Consider including the inline definition so the size can be derived from sizeof(IOBuf::Block).
This issue also appears on line 103 of the same file.
##########
test/brpc_urma_unittest.cpp:
##########
@@ -0,0 +1,609 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include <cstring>
+#include <gtest/gtest.h>
+#include <gflags/gflags.h>
+
+#if BRPC_WITH_URMA
+#include "butil/atomicops.h"
+#include "butil/sys_byteorder.h"
+#include "urma_api.h"
+#include "brpc/urma/urma_handshake.h"
+#include "brpc/urma/urma_handshake.pb.h"
+#include "brpc/urma/urma_helper.h"
+#include "urma_types.h"
+
+using namespace brpc;
+
+namespace brpc {
+namespace urma {
+
+DECLARE_int32(urma_client_handshake_version);
+extern bool g_skip_urma_init;
+extern butil::atomic<bool> g_urma_available;
+
+} // namespace urma
+} // namespace brpc
+
+// ---------------------------------------------------------------------------
+// v2 binary HelloMessage: serialize + deserialize round-trips.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, v2_serialize_deserialize_roundtrip) {
+ urma::v2_wire::HelloMessage m;
+ m.msg_len = urma::v2_wire::HELLO_PACKET_LEN;
+ m.hello_ver = urma::v2_wire::HELLO_V2_VERSION;
+ m.impl_ver = urma::v2_wire::IMPL_V2_VERSION;
+ m.buffer_size = 8192;
+ m.recv_buffer_cnt = 127;
+ m.jetty_id = 0x12345678;
+ for (int i = 0; i < 16; ++i) {
+ m.eid[i] = static_cast<uint8_t>(i + 1);
+ }
+ m.uasid = 0xdeadbeef;
+ m.tp_type = 1; // URMA_CTP
+ for (int i = 0; i < 16; ++i) {
+ m.seg_eid[i] = static_cast<uint8_t>(16 - i);
+ }
+ m.seg_uasid = 0xcafebabe;
+ m.seg_va = 0x1122334455667788ULL;
+ m.seg_len = 1ULL << 20;
+ m.seg_token_id = 0x42424242;
+
+ uint8_t buf[urma::v2_wire::HELLO_BODY_LEN];
+ m.Serialize(buf);
+
+ urma::v2_wire::HelloMessage m2;
+ m2.Deserialize(buf);
+ EXPECT_EQ(m.msg_len, m2.msg_len);
+ EXPECT_EQ(m.hello_ver, m2.hello_ver);
+ EXPECT_EQ(m.impl_ver, m2.impl_ver);
+ EXPECT_EQ(m.buffer_size, m2.buffer_size);
+ EXPECT_EQ(m.recv_buffer_cnt, m2.recv_buffer_cnt);
+ EXPECT_EQ(m.jetty_id, m2.jetty_id);
+ EXPECT_EQ(0, memcmp(m.eid, m2.eid, 16));
+ EXPECT_EQ(m.uasid, m2.uasid);
+ EXPECT_EQ(m.tp_type, m2.tp_type);
+ EXPECT_EQ(0, memcmp(m.seg_eid, m2.seg_eid, 16));
+ EXPECT_EQ(m.seg_uasid, m2.seg_uasid);
+ EXPECT_EQ(m.seg_va, m2.seg_va);
+ EXPECT_EQ(m.seg_len, m2.seg_len);
+ EXPECT_EQ(m.seg_token_id, m2.seg_token_id);
+}
+
+// ---------------------------------------------------------------------------
+// v2 packet on the wire: "URMA" magic + body.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, v2_packet_magic_is_urma) {
+ EXPECT_EQ(4u, urma::v2_wire::MAGIC_STR_LEN);
+ char magic[4] = {'U', 'R', 'M', 'A'};
+ EXPECT_EQ(0, memcmp(magic, "URMA", 4));
+ EXPECT_EQ(4u + 82u, urma::v2_wire::HELLO_PACKET_LEN);
+}
+
+// ---------------------------------------------------------------------------
+// v3 protobuf UrmaHello: serialize + parse round-trips.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, v3_protobuf_roundtrip) {
+ urma::UrmaHello msg;
+ msg.set_buffer_size(8192);
+ msg.set_recv_buffer_cnt(127);
+ msg.set_jetty_id(0x12345678);
+ uint8_t eid[16];
+ for (int i = 0; i < 16; ++i) {
+ eid[i] = static_cast<uint8_t>(i + 1);
+ }
+ msg.set_eid(eid, 16);
+ msg.set_uasid(0xdeadbeef);
+ msg.set_tp_type(1);
+ uint8_t seg_eid[16];
+ for (int i = 0; i < 16; ++i) {
+ seg_eid[i] = static_cast<uint8_t>(16 - i);
+ }
+ msg.set_seg_eid(seg_eid, 16);
+ msg.set_seg_uasid(0xcafebabe);
+ msg.set_seg_va(0x1122334455667788ULL);
+ msg.set_seg_len(1ULL << 20);
+ msg.set_seg_token_id(0x42424242);
+
+ std::string body;
+ ASSERT_TRUE(msg.SerializeToString(&body));
+ urma::UrmaHello msg2;
+ ASSERT_TRUE(msg2.ParseFromString(body));
+ EXPECT_EQ(msg.buffer_size(), msg2.buffer_size());
+ EXPECT_EQ(msg.recv_buffer_cnt(), msg2.recv_buffer_cnt());
+ EXPECT_EQ(msg.jetty_id(), msg2.jetty_id());
+ EXPECT_EQ(16, msg2.eid().size());
+ EXPECT_EQ(0, memcmp(msg.eid().data(), msg2.eid().data(), 16));
+ EXPECT_EQ(msg.uasid(), msg2.uasid());
+ EXPECT_EQ(msg.tp_type(), msg2.tp_type());
+ EXPECT_EQ(16, msg2.seg_eid().size());
+ EXPECT_EQ(msg.seg_uasid(), msg2.seg_uasid());
+ EXPECT_EQ(msg.seg_va(), msg2.seg_va());
+ EXPECT_EQ(msg.seg_len(), msg2.seg_len());
+ EXPECT_EQ(msg.seg_token_id(), msg2.seg_token_id());
+}
+
+// ---------------------------------------------------------------------------
+// CreateServerHandshakeByMagic dispatches on the magic bytes.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, server_handshake_factory_dispatches_on_magic) {
+ // We cannot fully exercise the server handshake without a real socket +
+ // endpoint, but we can verify the factory returns the right protocol
+ // version for each magic, and nullptr for an unknown magic.
+ uint8_t magic_v2[4] = {'U', 'R', 'M', 'A'};
+ uint8_t magic_v3[4] = {'U', 'R', 'M', '3'};
+ uint8_t magic_bad[4] = {'P', 'R', 'P', 'C'};
+
+ // v2 magic -> protocol version 2
+ urma::UrmaHandshake* hs2 =
+ urma::CreateServerHandshakeByMagic(nullptr, magic_v2);
+ // Note: the factory dereferences the endpoint only inside SendLocalHello /
+ // ReceiveAndParseRemoteHello; passing nullptr is safe for the version query.
+ // (We delete immediately to avoid touching the endpoint.)
+ if (hs2) {
+ EXPECT_EQ(2, hs2->ProtocolVersion());
+ delete hs2;
+ }
+ // v3 magic -> protocol version 3
+ urma::UrmaHandshake* hs3 =
+ urma::CreateServerHandshakeByMagic(nullptr, magic_v3);
+ if (hs3) {
+ EXPECT_EQ(3, hs3->ProtocolVersion());
+ delete hs3;
+ }
+ // unknown magic -> nullptr (caller falls back to TCP)
+ urma::UrmaHandshake* hsb =
+ urma::CreateServerHandshakeByMagic(nullptr, magic_bad);
+ EXPECT_EQ(nullptr, hsb);
+}
+
+// ---------------------------------------------------------------------------
+// CreateClientHandshake picks the version from the gflag.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, client_handshake_factory_respects_flag) {
+ const int saved = urma::FLAGS_urma_client_handshake_version;
+
+ urma::FLAGS_urma_client_handshake_version = 2;
+ urma::UrmaHandshake* hs2 = urma::CreateClientHandshake(nullptr);
+ if (hs2) {
+ EXPECT_EQ(2, hs2->ProtocolVersion());
+ delete hs2;
+ }
+
+ urma::FLAGS_urma_client_handshake_version = 3;
+ urma::UrmaHandshake* hs3 = urma::CreateClientHandshake(nullptr);
+ if (hs3) {
+ EXPECT_EQ(3, hs3->ProtocolVersion());
+ delete hs3;
+ }
+
+ urma::FLAGS_urma_client_handshake_version = saved;
+}
+
+// ---------------------------------------------------------------------------
+// 4-byte ACK: HELLO_ACK_URMA_OK bit.
+// ---------------------------------------------------------------------------
+TEST(UrmaHandshakeTest, ack_bit_is_rdma_ok) {
Review Comment:
The test case name refers to RDMA even though it is validating URMA ACK semantics, which makes the test suite misleading and harder to search/maintain. Rename the test to use "urma" consistently.
##########
WORKSPACE:
##########
@@ -279,6 +279,13 @@ http_archive(
urls = ["https://archive.apache.org/dist/thrift/0.15.0/thrift-0.15.0.tar.gz"],
)
+git_repository(
+ name = "umdk",
+ build_file = "//bazel/third_party/umdk:umdk.BUILD",
+ remote = "https://atomgit.com/openeuler/umdk.git",
+ tag = "v26.06.0_CAM",
+)
Review Comment:
This Bazel dependency is pinned by a movable tag (git_repository + tag). For hermetic/reproducible builds it should be pinned to an immutable commit SHA (like boringssl above) so the same BRPC revision always resolves the same UMDK sources.
##########
MODULE.bazel:
##########
@@ -52,3 +52,14 @@ git_override(
remote = 'https://github.com/hedronvision/bazel-compile-commands-extractor.git',
commit = '1e08f8e0507b6b6b1f4416a9a22cf5c28beaba93', # Jun 28, 2024
)
+
+git_repository = use_repo_rule(
+ '@bazel_tools//tools/build_defs/repo:git.bzl',
+ 'git_repository',
+)
+git_repository(
+ name = 'umdk',
+ build_file = '//bazel/third_party/umdk:umdk.BUILD',
+ remote = 'https://atomgit.com/openeuler/umdk.git',
+ tag = 'v26.06.0_CAM',
+)
Review Comment:
MODULE.bazel also pulls UMDK via a movable tag. Pinning to a commit (or another immutable reference) would make bzlmod resolution reproducible across time and mirrors.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5230305836
I'm not familiar with URMA, but it seems that the code in `src/brpc/urma` is similar to `src/brpc/rdma`, is that anything we can reuse?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] feat: add URMA transport support (brpc)
Posted by "wwbmmm (via GitHub)" <gi...@apache.org>.
wwbmmm commented on PR #3428:
URL: https://github.com/apache/brpc/pull/3428#issuecomment-5469061189
LGTM
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org