Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "endpoint_mgr.h"
12 : #include "hccl_mem_defs.h"
13 : #include "cpu_roce_endpoint.h"
14 : #include "hccl/hccl_res.h"
15 : #include "log.h"
16 : #include "roce_mem.h"
17 : #include "proc_reged_mem_mgr_cache.h"
18 : #include "host_socket_handle_manager.h"
19 : #include "adapter_rts_common.h"
20 : #include "hccp_peer_manager.h"
21 : #include "server_socket_manager.h"
22 : #include "hccp.h"
23 :
24 : using Hccl::HcclException;
25 : using std::exception;
26 : using std::string;
27 :
28 : namespace hcomm {
29 131 : CpuRoceEndpoint::CpuRoceEndpoint(const EndpointDesc& endpointDesc) : Endpoint(endpointDesc) {}
30 :
31 260 : CpuRoceEndpoint::~CpuRoceEndpoint() noexcept
32 : {
33 131 : std::lock_guard<std::mutex> lock(portMutex_);
34 131 : if (dynamicPort_ != HCCL_INVALID_PORT) {
35 0 : ServerSocketStopListenImpl(dynamicPort_);
36 : }
37 131 : dynamicPort_ = HCCL_INVALID_PORT;
38 260 : }
39 :
40 131 : HcclResult CpuRoceEndpoint::Init()
41 : {
42 131 : HCCL_INFO("[%s] localEndpoint protocol[%d]", __func__, endpointDesc_.protocol);
43 :
44 131 : if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_HOST) {
45 0 : HCCL_INFO("[CpuRoceEndpoint][%s] CpuRoceEndpoint not support device", __func__);
46 0 : return HCCL_E_NOT_SUPPORT;
47 : }
48 131 : Hccl::IpAddress ipAddr{};
49 131 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
50 131 : s32 devId = 0;
51 131 : CHK_RET(hrtGetDevice(&devId));
52 131 : EXCEPTION_CATCH(Hccl::HccpPeerManager::GetInstance().Init(devId), return HCCL_E_INTERNAL);
53 131 : u32 devPhyId = 0;
54 131 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
55 131 : auto& rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
56 131 : TRY_CATCH_RETURN(
57 : ctxHandle_ = static_cast<void*>(
58 : rdmaHandleMgr.GetByAddr(devPhyId, Hccl::LinkProtoType::RDMA, ipAddr, Hccl::PortDeploymentType::HOST_NET)));
59 131 : CHK_PTR_NULL(ctxHandle_);
60 130 : HCCL_INFO(
61 : "CpuRoceEndpoint::%s success, devPhyId[%u], ipAddr[%s], ctxHandle[%p]", __func__, devPhyId,
62 : ipAddr.Describe().c_str(), ctxHandle_);
63 :
64 130 : MemMgrCacheKey key{devPhyId, COMM_PROTOCOL_ROCE, ipAddr, LocTypeToPortType(endpointDesc_.loc.locType)};
65 14 : auto createMgr = [this]() {
66 14 : auto mgr = std::make_shared<RoceRegedMemMgr>();
67 14 : mgr->rdmaHandle_ = ctxHandle_;
68 14 : return mgr;
69 130 : };
70 130 : CHK_RET(AttachCache(key, createMgr));
71 130 : return HCCL_SUCCESS;
72 : }
73 :
74 4 : HcclResult CpuRoceEndpoint::ServerSocketListen(const uint32_t port)
75 : {
76 4 : Hccl::IpAddress ipAddr{};
77 4 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
78 :
79 4 : s32 devId = 0;
80 4 : CHK_RET(hrtGetDevice(&devId));
81 4 : u32 devPhyId = 0;
82 4 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
83 :
84 4 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
85 4 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
86 :
87 4 : HCCL_INFO("[CpuRoceEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devPhyId, ipAddr.Describe().c_str());
88 :
89 4 : uint32_t requestPort = port;
90 4 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
91 : localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
92 4 : return HCCL_SUCCESS;
93 : }
94 :
95 2 : inline HcclResult CpuRoceEndpoint::ServerSocketStopListenImpl(const uint32_t port)
96 : {
97 2 : Hccl::IpAddress ipAddr{};
98 2 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
99 :
100 2 : s32 devId = 0;
101 2 : CHK_RET(hrtGetDevice(&devId));
102 2 : u32 devPhyId = 0;
103 2 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
104 :
105 2 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
106 2 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
107 2 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStopListen(localPort, Hccl::NicType::HOST_NIC_TYPE, port));
108 :
109 2 : return HCCL_SUCCESS;
110 : }
111 :
112 2 : HcclResult CpuRoceEndpoint::ServerSocketStopListen(const uint32_t port) { return ServerSocketStopListenImpl(port); }
113 :
114 0 : HcclResult CpuRoceEndpoint::ServerSocketGetListenPort(uint32_t* port)
115 : {
116 0 : std::lock_guard<std::mutex> lock(portMutex_);
117 0 : CHK_PTR_NULL(port);
118 0 : s32 devId = 0;
119 0 : CHK_RET(hrtGetDevice(&devId));
120 0 : u32 devPhyId = 0;
121 0 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
122 :
123 0 : Hccl::IpAddress ipAddr{};
124 0 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
125 :
126 0 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
127 0 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
128 :
129 0 : HCCL_INFO("[CpuRoceEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devPhyId, ipAddr.Describe().c_str());
130 :
131 : // 已有监听端口则直接返回
132 0 : if (dynamicPort_ != HCCL_INVALID_PORT) {
133 0 : *port = dynamicPort_;
134 0 : HCCL_INFO("[CpuRoceEndpoint::%s] already listening, return existing port[%u]", __func__, dynamicPort_);
135 0 : return HCCL_SUCCESS;
136 : }
137 0 : uint32_t requestPort = 0;
138 0 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
139 : localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
140 0 : if (requestPort == 0 || requestPort == HCCL_INVALID_PORT) {
141 0 : HCCL_ERROR("[CpuRoceEndpoint::%s] get listen port failed, port is invalid", __func__);
142 0 : return HCCL_E_NETWORK;
143 : }
144 0 : dynamicPort_ = requestPort;
145 0 : *port = dynamicPort_;
146 0 : return HCCL_SUCCESS;
147 0 : }
148 :
149 1 : HcclResult CpuRoceEndpoint::RegisterMemory(HcommMem mem, const char* memTag, void** memHandle)
150 : {
151 1 : CHK_RET(this->regedMemMgr_->RegisterMemory(mem, memTag, memHandle));
152 0 : return HCCL_SUCCESS;
153 : }
154 :
155 2 : HcclResult CpuRoceEndpoint::UnregisterMemory(void* memHandle)
156 : {
157 2 : CHK_RET(this->regedMemMgr_->UnregisterMemory(memHandle));
158 0 : return HCCL_SUCCESS;
159 : }
160 :
161 0 : HcclResult CpuRoceEndpoint::MemoryExport(void* memHandle, void** memDesc, uint32_t* memDescLen)
162 : {
163 0 : CHK_RET(this->regedMemMgr_->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
164 0 : return HCCL_SUCCESS;
165 : }
166 :
167 0 : HcclResult CpuRoceEndpoint::MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem)
168 : {
169 0 : CHK_RET(this->regedMemMgr_->MemoryImport(memDesc, descLen, outMem));
170 0 : return HCCL_SUCCESS;
171 : }
172 :
173 0 : HcclResult CpuRoceEndpoint::MemoryUnimport(const void* memDesc, uint32_t descLen)
174 : {
175 0 : CHK_RET(this->regedMemMgr_->MemoryUnimport(memDesc, descLen));
176 0 : return HCCL_SUCCESS;
177 : }
178 :
179 0 : HcclResult CpuRoceEndpoint::GetAllMemHandles(void** memHandles, uint32_t* memHandleNum)
180 : {
181 0 : CHK_RET(this->regedMemMgr_->GetAllMemHandles(memHandles, memHandleNum));
182 0 : return HCCL_SUCCESS;
183 : }
184 :
185 65 : HcclResult CpuRoceEndpoint::GetCapabilities(Capabilities& caps)
186 : {
187 65 : HCCL_INFO("[CpuRoceEndpoint::%s] START.", __func__);
188 : static constexpr uint64_t RDMA_MAX_WR_LENGTH = 1ULL * 1024 * 1024 * 1024; // 单次RDMA操作最大长度1GB
189 65 : if (!isCapabilitiesAvailable_) {
190 : // 待 HCCP 提供查询设备支持的最大发送消息的接口后,查询设备实际值。
191 64 : capabilities_.maxMsgSize = RDMA_MAX_WR_LENGTH;
192 64 : uint32_t ret = RaGetLbMax(this->regedMemMgr_->rdmaHandle_, &(capabilities_.lbMax));
193 64 : HCCL_DEBUG("[CpuRoceEndpoint::GetCapabilities] lbMax = %d.", capabilities_.lbMax);
194 64 : CHK_PRT_RET(
195 : ret != 0,
196 : HCCL_ERROR(
197 : "[CpuRoceEndpoint::GetCapabilities][GetLbMax]errNo[0x%016llx] RaGetLbMax fail. "
198 : "return[%u], params: rdmaHandle[%p], lbMax[%d]",
199 : HCCL_ERROR_CODE(HCCL_E_NETWORK), ret, this->regedMemMgr_->rdmaHandle_, capabilities_.lbMax),
200 : HCCL_E_NETWORK);
201 64 : isCapabilitiesAvailable_ = true;
202 : }
203 65 : caps = capabilities_;
204 65 : HCCL_INFO("[CpuRoceEndpoint::%s] SUCCESS.", __func__);
205 65 : return HCCL_SUCCESS;
206 : }
207 : } // namespace hcomm
|