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 "urma_endpoint.h"
12 : #include <algorithm>
13 : #include "endpoint_mgr.h"
14 : #include "log.h"
15 : #include "hccl/hccl_res.h"
16 : #include "urma_mem.h"
17 : #include "proc_reged_mem_mgr_cache.h"
18 : #include "adapter_rts_common.h"
19 : #include "server_socket_manager.h"
20 : #include "ra_rs_comm.h"
21 :
22 : namespace hcomm {
23 :
24 36 : UrmaEndpoint::UrmaEndpoint(const EndpointDesc& endpointDesc) : Endpoint(endpointDesc) {}
25 :
26 72 : UrmaEndpoint::~UrmaEndpoint() noexcept
27 : {
28 36 : std::lock_guard<std::mutex> lock(portMutex_);
29 36 : if (dynamicPort_ != HCCL_INVALID_PORT) {
30 2 : ServerSocketStopListenImpl(dynamicPort_);
31 : }
32 36 : dynamicPort_ = HCCL_INVALID_PORT;
33 72 : }
34 :
35 20 : HcclResult UrmaEndpoint::Init()
36 : {
37 20 : HCCL_INFO("[%s] localEndpoint protocol[%d]", __func__, endpointDesc_.protocol);
38 :
39 20 : if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_DEVICE) {
40 0 : HCCL_ERROR(
41 : "[UrmaEndpoint][%s] endpointDesc.loc.locType[%d] only support ENDPOINT_LOC_TYPE_DEVICE", __func__,
42 : endpointDesc_.loc.locType);
43 0 : return HCCL_E_PARA;
44 : }
45 :
46 20 : Hccl::IpAddress ipAddr{};
47 20 : HcclResult ret = CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr);
48 20 : if (ret != HCCL_SUCCESS) {
49 0 : HCCL_ERROR("call CommAddrToIpAddress failed");
50 0 : return ret;
51 : }
52 :
53 : u32 devPhyId;
54 : s32 deviceLogicId;
55 20 : ret = hrtGetDevice(&deviceLogicId);
56 20 : if (ret != HCCL_SUCCESS) {
57 0 : HCCL_ERROR("call hrtGetDevice failed, deviceLogicId[%d]", deviceLogicId);
58 0 : return ret;
59 : }
60 20 : Hccl::HccpHdcManager::GetInstance().Init(deviceLogicId);
61 20 : ret = hrtGetDevicePhyIdByIndex(static_cast<uint32_t>(deviceLogicId), devPhyId);
62 20 : if (ret != HCCL_SUCCESS) {
63 0 : HCCL_ERROR("call hrtGetDevicePhyIdByIndex failed, deviceLogicId[%d], devPhyId[%u]", deviceLogicId, devPhyId);
64 0 : return ret;
65 : }
66 :
67 20 : if (endpointDesc_.loc.device.devPhyId != devPhyId) {
68 4 : HCCL_WARNING(
69 : "[UrmaEndpoint][%s] endpointDesc.loc.device.devPhyId[%u] incorrect", __func__,
70 : endpointDesc_.loc.device.devPhyId);
71 : endpointDesc_.loc.device.devPhyId
72 4 : = devPhyId; // 当前endpointDesc.loc.device.devPhyId不准,暂时由查询的devPhyId赋值
73 : }
74 :
75 20 : auto& rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
76 20 : EXCEPTION_CATCH(
77 : ctxHandle_ = static_cast<void*>(rdmaHandleMgr.GetByIp(endpointDesc_.loc.device.devPhyId, ipAddr)),
78 : return HCCL_E_PARA);
79 19 : CHK_PTR_NULL(ctxHandle_);
80 19 : HCCL_INFO(
81 : "%s success, devPhyId[%u], ipAddr[%s], ctxHandle[%p]", __func__, devPhyId, ipAddr.Describe().c_str(),
82 : ctxHandle_);
83 :
84 19 : MemMgrCacheKey key{devPhyId, COMM_PROTOCOL_UB_CTP, ipAddr, LocTypeToPortType(endpointDesc_.loc.locType)};
85 19 : auto createMgr = [this]() {
86 19 : auto mgr = std::make_shared<UbRegedMemMgr>();
87 19 : mgr->rdmaHandle_ = ctxHandle_;
88 19 : return mgr;
89 19 : };
90 19 : CHK_RET(AttachCache(key, createMgr));
91 :
92 : // ccu模式专用的资源分配器
93 19 : ccuChannelCtxPool_.reset(new (std::nothrow) CcuChannelCtxPool(deviceLogicId));
94 19 : CHK_PTR_NULL(ccuChannelCtxPool_);
95 :
96 19 : return HcclResult::HCCL_SUCCESS;
97 : }
98 :
99 3 : HcclResult UrmaEndpoint::ServerSocketListen(const uint32_t port)
100 : {
101 3 : if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_DEVICE) {
102 1 : HCCL_INFO(
103 : "[UrmaEndpoint][%s] endpointDesc.loc.locType[%d] skip create ServerSocket", __func__,
104 : endpointDesc_.loc.locType);
105 1 : return HCCL_SUCCESS;
106 : }
107 :
108 2 : Hccl::IpAddress ipaddr{};
109 2 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipaddr));
110 :
111 2 : HCCL_INFO("[UrmaEndpoint][%s] endpointDesc_.loc.device.devPhyId %u", __func__, endpointDesc_.loc.device.devPhyId);
112 :
113 2 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
114 : Hccl::PortData localPort
115 2 : = Hccl::PortData(static_cast<Hccl::RankId>(endpointDesc_.loc.device.devPhyId), type, 0, ipaddr);
116 :
117 2 : HCCL_INFO(
118 : "[UrmaEndpoint][%s] devicePhyId[%u] localPort[%s]", __func__, endpointDesc_.loc.device.devPhyId,
119 : localPort.Describe().c_str());
120 2 : uint32_t requestPort = port;
121 2 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
122 : localPort, Hccl::NicType::DEVICE_NIC_TYPE, endpointDesc_.loc.device.devPhyId, &requestPort));
123 1 : return HCCL_SUCCESS;
124 : }
125 :
126 2 : inline HcclResult UrmaEndpoint::ServerSocketStopListenImpl(const uint32_t port)
127 : {
128 2 : Hccl::IpAddress ipAddr{};
129 2 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
130 2 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
131 : Hccl::PortData localPort
132 2 : = Hccl::PortData(static_cast<Hccl::RankId>(endpointDesc_.loc.device.devPhyId), type, 0, ipAddr);
133 :
134 2 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStopListen(localPort, Hccl::NicType::DEVICE_NIC_TYPE, port));
135 0 : return HCCL_SUCCESS;
136 : }
137 :
138 0 : HcclResult UrmaEndpoint::ServerSocketStopListen(const uint32_t port) { return ServerSocketStopListenImpl(port); }
139 :
140 6 : HcclResult UrmaEndpoint::ServerSocketGetListenPort(uint32_t* port)
141 : {
142 6 : std::lock_guard<std::mutex> lock(portMutex_);
143 6 : if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_DEVICE) {
144 1 : HCCL_INFO(
145 : "[UrmaEndpoint][%s] endpointDesc.loc.locType[%d] skip create ServerSocket", __func__,
146 : endpointDesc_.loc.locType);
147 1 : return HCCL_SUCCESS;
148 : }
149 :
150 5 : HCCL_INFO("[UrmaEndpoint][%s] endpointDesc_.loc.device.devPhyId %u", __func__, endpointDesc_.loc.device.devPhyId);
151 5 : Hccl::IpAddress ipaddr{};
152 5 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipaddr));
153 :
154 5 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
155 : Hccl::PortData localPort
156 5 : = Hccl::PortData(static_cast<Hccl::RankId>(endpointDesc_.loc.device.devPhyId), type, 0, ipaddr);
157 :
158 5 : HCCL_INFO(
159 : "[UrmaEndpoint][%s] devicePhyId[%u] localPort[%s]", __func__, endpointDesc_.loc.device.devPhyId,
160 : localPort.Describe().c_str());
161 :
162 : // 已有监听端口则直接返回
163 5 : if (dynamicPort_ != HCCL_INVALID_PORT) {
164 1 : *port = dynamicPort_;
165 1 : HCCL_INFO("[UrmaEndpoint::%s] already listening, return existing port[%u]", __func__, dynamicPort_);
166 1 : return HCCL_SUCCESS;
167 : }
168 4 : uint32_t requestPort = 0;
169 4 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
170 : localPort, Hccl::NicType::DEVICE_NIC_TYPE, endpointDesc_.loc.device.devPhyId, &requestPort));
171 3 : if (requestPort == 0 || requestPort == HCCL_INVALID_PORT) {
172 2 : HCCL_ERROR("[UrmaEndpoint::%s] get listen port failed, port is invalid.", __func__);
173 2 : return HCCL_E_NETWORK;
174 : }
175 1 : dynamicPort_ = requestPort;
176 1 : *port = dynamicPort_;
177 1 : return HCCL_SUCCESS;
178 6 : }
179 :
180 0 : HcclResult UrmaEndpoint::RegisterMemory(HcommMem mem, const char* memTag, void** memHandle)
181 : {
182 0 : CHK_RET(this->regedMemMgr_->RegisterMemory(mem, memTag, memHandle));
183 0 : return HCCL_SUCCESS;
184 : }
185 :
186 0 : HcclResult UrmaEndpoint::UnregisterMemory(void* memHandle)
187 : {
188 0 : CHK_RET(this->regedMemMgr_->UnregisterMemory(memHandle));
189 0 : return HCCL_SUCCESS;
190 : }
191 :
192 0 : HcclResult UrmaEndpoint::MemoryExport(void* memHandle, void** memDesc, uint32_t* memDescLen)
193 : {
194 0 : CHK_RET(this->regedMemMgr_->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
195 0 : return HCCL_SUCCESS;
196 : }
197 :
198 0 : HcclResult UrmaEndpoint::MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem)
199 : {
200 0 : CHK_RET(this->regedMemMgr_->MemoryImport(memDesc, descLen, outMem));
201 0 : return HCCL_SUCCESS;
202 : }
203 :
204 0 : HcclResult UrmaEndpoint::MemoryUnimport(const void* memDesc, uint32_t descLen)
205 : {
206 0 : CHK_RET(this->regedMemMgr_->MemoryUnimport(memDesc, descLen));
207 0 : return HCCL_SUCCESS;
208 : }
209 :
210 0 : HcclResult UrmaEndpoint::GetAllMemHandles(void** memHandles, uint32_t* memHandleNum)
211 : {
212 0 : CHK_RET(this->regedMemMgr_->GetAllMemHandles(memHandles, memHandleNum));
213 0 : return HCCL_SUCCESS;
214 : }
215 :
216 30 : CcuChannelCtxPool* UrmaEndpoint::GetCcuChannelCtxPool() { return ccuChannelCtxPool_.get(); }
217 :
218 4 : HcclResult UrmaEndpoint::GetAsyncEvents(uint32_t devPhyId, struct AsyncEvent events[], uint32_t& num)
219 : {
220 4 : uint32_t interfaceVersion{0};
221 :
222 : int ret;
223 : // 对RaCtxGetAsyncEvents接口的版本检验
224 4 : ret = RaGetInterfaceVersion(devPhyId, RA_RS_CTX_GET_ASYNC_EVENTS, &interfaceVersion);
225 4 : if (ret != 0) {
226 0 : HCCL_ERROR(
227 : "[%s] devPhyId[%u] epHandle[%p] RaGetInterfaceVersion failed, ret[%d]", __func__, devPhyId, this, ret);
228 0 : return HCCL_E_INTERNAL;
229 : }
230 :
231 4 : if (interfaceVersion <= 1) {
232 1 : HCCL_ERROR(
233 : "[%s] devPhyId[%u] epHandle[%p] version[%u] not support", __func__, devPhyId, this, interfaceVersion);
234 1 : return HCCL_E_NOT_SUPPORT;
235 : }
236 :
237 3 : if (!IsCtxHandleValid()) {
238 1 : HCCL_ERROR(
239 : "[%s] devPhyId[%u] ctxHandle_[%p] has been invalidated, "
240 : "RdmaHandleManager may have DeInit this device",
241 : __func__, devPhyId, ctxHandle_);
242 1 : return HCCL_E_INTERNAL;
243 : }
244 :
245 2 : ret = RaCtxGetAsyncEvents(ctxHandle_, events, &num);
246 2 : if (ret != 0) {
247 1 : HCCL_ERROR(
248 : "[%s] devPhyId[%u] epHandle[%p] RaCtxGetAsyncEvents failed, ctxHandle[%p] ret[%d]", __func__, devPhyId,
249 : this, (void*)ctxHandle_, ret);
250 1 : return HCCL_E_INTERNAL;
251 : }
252 1 : return HCCL_SUCCESS;
253 : }
254 :
255 : } // namespace hcomm
|