chenBright commented on code in PR #3720:
URL: https://github.com/apache/brpc/pull/3720#discussion_r4508931457
##########
src/brpc/rdma/rdma_endpoint.cpp:
##########
@@ -1367,505 +727,47 @@ int RdmaEndpoint::StartCqEvents() {
return -1;
}
- if (_cq_sid != INVALID_SOCKET_ID) {
- // Already started.
- return 0;
- }
- if (_resource == nullptr) {
- if (BAIDU_UNLIKELY(g_skip_rdma_init)) {
- // For UT: AllocateResources() succeeds without allocating
anything.
+ if (!_connection) {
+ if (g_skip_rdma_init) {
return 0;
}
-
- LOG(WARNING) << "No RDMA resource to start CQ events on, " << *_socket;
- errno = ERDMA;
+ errno = ENOTCONN;
return -1;
}
-
- SocketOptions options;
- options.user = this;
- options.keytable_pool = _socket->_keytable_pool;
- if (!FLAGS_rdma_use_polling) {
- options.fd = _resource->comp_channel->fd;
- options.on_edge_triggered_events = PollCq;
- }
- if (Socket::Create(options, &_cq_sid) < 0) {
- PLOG(WARNING) << "Fail to create socket for cq";
- return -1;
- }
-
- if (FLAGS_rdma_use_polling) {
- PollerAddCqSid();
- }
-
- return 0;
+ return _connection->EnableReceive();
}
int RdmaEndpoint::BringUpQp(const ParsedHello& remote, bool is_server) {
- // MTU negotiation (server side): compute min(local, client) and store it
+ // MTU negotiation (server side): compute the minimum MTU and store it
// so FillLocalRdmaHello can advertise the negotiated value in the server
- // hello reply. This is done before the g_skip_rdma_init early-return so
- // that UT (which skips real QP bring-up) still sets _outgoing_mtu.
- if (is_server && remote.path_mtu.has_value()) {
+ // hello reply. This is done before the `g_skip_rdma_init` early return so
+ // that UT (which skips real QP bring-up) still sets `_outgoing_mtu`.
+ if (is_server == true && remote.path_mtu.has_value() == true) {
uint32_t local_mtu = GetRdmaActiveMtu();
_outgoing_mtu = std::min(local_mtu, *remote.path_mtu);
}
if (BAIDU_UNLIKELY(g_skip_rdma_init)) {
- // For UT
return 0;
}
-
- ibv_qp_attr attr;
-
- attr.qp_state = IBV_QPS_INIT;
- attr.pkey_index = 0; // TODO: support more pkey use in future
- attr.port_num = GetRdmaPortNum();
- attr.qp_access_flags = IBV_ACCESS_REMOTE_WRITE;
- int err = IbvModifyQp(_resource->qp, &attr, (ibv_qp_attr_mask)(
- IBV_QP_STATE |
- IBV_QP_PKEY_INDEX |
- IBV_QP_PORT |
- IBV_QP_ACCESS_FLAGS));
- if (err != 0) {
- LOG(WARNING) << "Fail to modify QP from RESET to INIT: " <<
berror(err);
- return -1;
- }
-
- // ECE negotiation, done while the QP is in INIT state (must be set
- // before the RTR transition).
- //
- // End-to-end model:
- // Server: `remote->ece' is the client's queried ECE; set it here,
- // then after RTS we query the reduced/negotiated ECE and
- // send it back in the server hello.
- // Client: `remote->ece' is the server's reduced ECE;
- // just set it here.
- bool use_ece = true;
- if (IbvSetEce != nullptr && remote.ece.has_value()) {
- ibv_ece ece = *remote.ece;
- int err = IbvSetEce(_resource->qp, &ece);
- if (err != 0) {
- use_ece = false;
- LOG(WARNING) << "Fail to IbvSetEce, continue without ECE: "
- << berror(err);
- }
- }
-
- if (PostRecv(_rq_size, true) < 0) {
- PLOG(WARNING) << "Fail to post recv wr";
- return -1;
- }
-
- attr.qp_state = IBV_QPS_RTR;
- // MTU negotiation: use the peer-advertised MTU if available, otherwise
- // fall back to the legacy default (IBV_MTU_1024).
- // Server side: _outgoing_mtu was already computed at function entry;
- // reuse it for the QP path_mtu attribute.
- // Client side: remote.path_mtu is already the server's negotiated
- // min(local, client) and we use it as-is.
- uint32_t negotiated_mtu =
- std::min(static_cast<uint32_t>(IBV_MTU_1024), GetRdmaActiveMtu());
- if (is_server && _outgoing_mtu.has_value()) {
- negotiated_mtu = *_outgoing_mtu;
- } else if (!is_server && remote.path_mtu.has_value()) {
- negotiated_mtu = *remote.path_mtu;
- }
- attr.path_mtu = static_cast<ibv_mtu>(negotiated_mtu);
- attr.ah_attr.grh.dgid = remote.gid;
- attr.ah_attr.grh.flow_label = 0;
- attr.ah_attr.grh.sgid_index = GetRdmaGidIndex();
- attr.ah_attr.grh.hop_limit = MAX_HOP_LIMIT;
- attr.ah_attr.grh.traffic_class = 0;
- attr.ah_attr.dlid = remote.lid;
- attr.ah_attr.sl = 0;
- attr.ah_attr.src_path_bits = 0;
- attr.ah_attr.static_rate = 0;
- attr.ah_attr.is_global = 1;
- attr.ah_attr.port_num = GetRdmaPortNum();
- attr.dest_qp_num = remote.qp_num;
- attr.rq_psn = 0;
- attr.max_dest_rd_atomic = 0;
- attr.min_rnr_timer = 0; // We do not allow rnr error
- err = IbvModifyQp(_resource->qp, &attr, (ibv_qp_attr_mask)(
- IBV_QP_STATE |
- IBV_QP_PATH_MTU |
- IBV_QP_MIN_RNR_TIMER |
- IBV_QP_AV |
- IBV_QP_MAX_DEST_RD_ATOMIC |
- IBV_QP_DEST_QPN |
- IBV_QP_RQ_PSN));
- if (err != 0) {
- LOG(WARNING) << "Fail to modify QP from INIT to RTR: " << berror(err);
- return -1;
- }
-
- attr.qp_state = IBV_QPS_RTS;
- attr.timeout = TIMEOUT;
- attr.retry_cnt = RETRY_CNT;
- attr.rnr_retry = 0; // We do not allow rnr error
- attr.sq_psn = 0;
- attr.max_rd_atomic = 0;
- err = IbvModifyQp(_resource->qp, &attr, (ibv_qp_attr_mask)(
- IBV_QP_STATE |
- IBV_QP_RNR_RETRY |
- IBV_QP_RETRY_CNT |
- IBV_QP_TIMEOUT |
- IBV_QP_SQ_PSN |
- IBV_QP_MAX_QP_RD_ATOMIC));
- if (err != 0) {
- LOG(WARNING) << "Fail to modify QP from RTR to RTS: " << berror(err);
- return -1;
- }
-
- // On the server side, now that the QP reached RTS, query the
reduced/negotiated
- // ECE (the subset of enhancements supported by both peers) so it can be
returned
- // to the client in the server hello.
- if (is_server && use_ece && IbvQueryEce != nullptr &&
remote.ece.has_value()) {
- ibv_ece ece;
- int qerr = IbvQueryEce(_resource->qp, &ece);
- if (qerr == 0) {
- _outgoing_ece = ece;
- } else {
- LOG(WARNING) << "Fail to IbvQueryEce(negotiated), "
- "continue without ECE: " << berror(qerr);
- }
- }
-
- return 0;
-}
-
-static void DeallocateCq(ibv_cq* cq) {
- if (nullptr == cq) {
- return;
- }
-
- int err = IbvDestroyCq(cq);
- LOG_IF(WARNING, 0 != err) << "Fail to destroy CQ: " << berror(err);
-}
-
-static int DrainCq(ibv_cq* cq) {
- if (nullptr == cq) {
- return 0;
- }
-
- ibv_wc wc;
- int ret;
- do {
- ret = ibv_poll_cq(cq, 1, &wc);
- } while (ret > 0);
-
- LOG_IF(ERROR, ret < 0) << "drain CQ failed: " << ret;
- return ret;
-}
-
-void RdmaEndpoint::DeallocateResources() {
- if (_resource == nullptr) {
- return;
- }
- if (FLAGS_rdma_use_polling) {
- PollerRemoveCqSid();
- }
- bool move_to_rdma_resource_list = false;
- if (_sq_size <= FLAGS_rdma_prepared_qp_size &&
- _rq_size <= FLAGS_rdma_prepared_qp_size &&
- FLAGS_rdma_prepared_qp_cnt > 0) {
- ibv_qp_attr attr;
- attr.qp_state = IBV_QPS_RESET;
- if (IbvModifyQp(_resource->qp, &attr, IBV_QP_STATE) == 0) {
- move_to_rdma_resource_list = true;
- }
- }
-
- if (nullptr != _resource->send_cq) {
- IbvAckCqEvents(_resource->send_cq, _send_cq_events);
- }
- if (nullptr != _resource->recv_cq) {
- IbvAckCqEvents(_resource->recv_cq, _recv_cq_events);
- }
-
- bool remove_consumer = true;
-_reclaim:
- if (!move_to_rdma_resource_list) {
- if (_resource->qp != nullptr) {
- int err = IbvDestroyQp(_resource->qp);
- LOG_IF(WARNING, 0 != err) << "Fail to destroy QP: " << berror(err);
- _resource->qp = nullptr;
- }
-
- DeallocateCq(_resource->polling_cq);
- DeallocateCq(_resource->send_cq);
- DeallocateCq(_resource->recv_cq);
-
- if (_resource->comp_channel != nullptr) {
- if (_cq_sid != INVALID_SOCKET_ID) {
- // Destroy send_comp_channel will destroy this fd,
- // so that we should remove it from epoll fd first
- int fd = _resource->comp_channel->fd;
- GetGlobalEventDispatcher(fd,
_socket->_io_event.bthread_tag()).RemoveConsumer(fd);
- remove_consumer = false;
- }
- int err = IbvDestroyCompChannel(_resource->comp_channel);
- LOG_IF(WARNING, 0 != err) << "Fail to destroy CQ channel: " <<
berror(err);
- }
-
- _resource->polling_cq = nullptr;
- _resource->send_cq = nullptr;
- _resource->recv_cq = nullptr;
- _resource->comp_channel = nullptr;
- delete _resource;
- _resource = nullptr;
- }
-
- if (INVALID_SOCKET_ID != _cq_sid) {
- SocketUniquePtr s;
- if (Socket::Address(_cq_sid, &s) == 0) {
- if (remove_consumer) {
- s->_io_event.RemoveConsumer(s->_fd);
- }
- s->_user = nullptr; // Do not release user (this RdmaEndpoint).
- s->_fd = -1; // Already remove fd from epoll fd.
- s->SetFailed();
- }
- }
-
- if (move_to_rdma_resource_list) {
- // When a QP is moved to the RESET state, all associated send and
- // receive queues are flushed, meaning any outstanding WRs are
effectively
- // abandoned by the hardware.
- //
- // However, the CQ associated with that QP is *not* cleared
automatically,
- // meaning that it will still contain entries for WRs that completed
before
- // the reset.
- //
- // The application should finish polling the CQ to remove these
obsolete
- // entries before reusing the QP.
- int ret = DrainCq(_resource->polling_cq);
- ret += DrainCq(_resource->send_cq);
- ret += DrainCq(_resource->recv_cq);
- if (ret < 0) {
- move_to_rdma_resource_list = false;
- goto _reclaim;
- }
-
- {
- BAIDU_SCOPED_LOCK(*g_rdma_resource_mutex);
- _resource->next = g_rdma_resource_list;
- g_rdma_resource_list = _resource;
- }
- _resource = nullptr;
- }
-
- // Detach everything from this endpoint so that the function is
- // idempotent: it is called both when the endpoint is reset/destroyed
- // and when AllocateResources() fails halfway.
- _cq_sid = INVALID_SOCKET_ID;
- _send_cq_events = 0;
- _recv_cq_events = 0;
+ return _connection->Activate(remote, is_server, &_outgoing_ece);
}
-static const int MAX_CQ_EVENTS = 128;
-
-int RdmaEndpoint::GetAndAckEvents(SocketUniquePtr& s) {
- void* context = nullptr;
- ibv_cq* cq = nullptr;
- while (true) {
- if (IbvGetCqEvent(_resource->comp_channel, &cq, &context) != 0) {
- if (errno != EAGAIN) {
- const int saved_errno = errno;
- PLOG(ERROR) << "Fail to get cq event from " <<
s->description();
- s->SetFailed(saved_errno, "Fail to get cq event from %s: %s",
- s->description().c_str(), berror(saved_errno));
- return -1;
- }
- break;
- }
- if (cq == _resource->send_cq) {
- ++_send_cq_events;
- } else if (cq == _resource->recv_cq) {
- ++_recv_cq_events;
- } else {
- // Unexpected CQ event that does not belong to
- // this endpoint's send/recv CQs.
- LOG(WARNING) << "Unexpected CQ event from cq=" << cq
- << " of " << s->description();
- // Acknowledge this single event immediately
- // to avoid leaking unacknowledged events.
- IbvAckCqEvents(cq, 1);
- }
- }
- if (_send_cq_events >= MAX_CQ_EVENTS) {
- IbvAckCqEvents(_resource->send_cq, _send_cq_events);
- _send_cq_events = 0;
- }
- if (_recv_cq_events >= MAX_CQ_EVENTS) {
- IbvAckCqEvents(_resource->recv_cq, _recv_cq_events);
- _recv_cq_events = 0;
- }
- return 0;
+bool RdmaEndpoint::OwnsConnection(const RdmaConnection* connection) const {
+ return connection && _connection.get() == connection;
Review Comment:
These accesses cannot overlap under the current Socket lifetime rules.
Delivery callbacks retain a `SocketUniquePtr`, while recycling and health-check
reset wait for those references to drain. Handshake fallback detaches the
connection before CQ delivery is enabled.
In a3b3a4a, I added comments to `OwnsConnection()` and `Reset()` documenting
this lifetime requirement. The synchronization comes from the Socket lifecycle,
rather than from `shared_ptr` itself.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]