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 "rdma_handle_manager.h"
12 : #include <mutex>
13 :
14 : #include "socket_handle_manager.h"
15 : #include "orion_adapter_rts.h"
16 : #include "orion_adapter_hccp.h"
17 : #include "log.h"
18 : #include "tokenInfo_manager.h"
19 :
20 : namespace Hccl {
21 :
22 3 : RdmaHandleManager::RdmaHandleManager()
23 : {
24 3 : rdmaHandleMap.resize(MAX_DEVICE_NUM);
25 198 : for (u32 i = 0; i < rdmaHandleMap.size(); ++i) {
26 195 : rdmaHandleMap[i].resize(LINK_PROTO_TYPE_NUM);
27 : }
28 3 : }
29 :
30 3 : RdmaHandleManager::~RdmaHandleManager()
31 : {
32 3 : DECTOR_TRY_CATCH("RdmaHandleManager", DestroyAll());
33 3 : }
34 :
35 3505 : RdmaHandleManager &RdmaHandleManager::GetInstance()
36 : {
37 3505 : static RdmaHandleManager rdmaHandleManager;
38 3505 : return rdmaHandleManager;
39 : }
40 :
41 1 : RdmaHandle RdmaHandleManager::Create(u32 devPhyId, const PortData &localPort)
42 : {
43 1 : RaInterface intf{};
44 1 : intf.address = localPort.GetAddr();
45 1 : intf.phyId = devPhyId;
46 :
47 3 : HCCL_INFO("RdmaHandleManager::Create, devPhyId[%u], localPort[%s]", devPhyId, localPort.Describe().c_str());
48 :
49 1 : HrtNetworkMode netMode = HrtNetworkMode::HDC;
50 1 : if (localPort.GetType() == PortDeploymentType::HOST_NET) {
51 0 : netMode = HrtNetworkMode::PEER;
52 : }
53 :
54 1 : RdmaHandle rdmaHandle = HrtRaRdmaInit(netMode, intf);
55 1 : rdmaHandleMap[devPhyId][localPort.GetProto()][localPort.GetAddr()] = rdmaHandle;
56 1 : netWorkModeMap[rdmaHandle] = netMode;
57 1 : activeHandles_.insert(rdmaHandle);
58 1 : return rdmaHandle;
59 : }
60 :
61 3 : RdmaHandle RdmaHandleManager::Create(u32 devPhyId, const LinkProtoType &localProtocolType,
62 : const IpAddress &localIp, PortDeploymentType type)
63 : {
64 3 : RaInterface intf{};
65 3 : intf.address = localIp;
66 3 : intf.phyId = devPhyId;
67 :
68 3 : HCCL_INFO("RdmaHandleManager::Create, devPhyId[%u], LinkProtoType[%s], localIp[%s]",
69 : devPhyId, localProtocolType.Describe().c_str(), localIp.GetIpStr().c_str());
70 :
71 3 : HrtNetworkMode netMode = HrtNetworkMode::HDC;
72 3 : if (type == PortDeploymentType::HOST_NET) {
73 3 : netMode = HrtNetworkMode::PEER;
74 : }
75 3 : RdmaHandle rdmaHandle = HrtRaRdmaInit(netMode, intf);
76 3 : rdmaHandleMap[devPhyId][localProtocolType][localIp] = rdmaHandle;
77 3 : netWorkModeMap[rdmaHandle] = netMode;
78 3 : tokenInfoMap[rdmaHandle] = std::make_unique<TokenInfoManager>(devPhyId, rdmaHandle);
79 3 : activeHandles_.insert(rdmaHandle);
80 3 : return rdmaHandle;
81 : }
82 :
83 60 : RdmaHandle RdmaHandleManager::Get(u32 devPhyId, const PortData &localPort, LinkProtocol linkProtocol)
84 : {
85 60 : std::lock_guard<std::mutex> lock(managerMutex);
86 :
87 60 : LinkProtoType localProto = localPort.GetProto();
88 60 : if (devPhyId > rdmaHandleMap.size() - 1 || localProto == LinkProtoType::HCCS_PCIE) {
89 0 : return nullptr;
90 : }
91 :
92 60 : IpAddress localIp = localPort.GetAddr();
93 60 : if (linkProtocol == LinkProtocol::UBOE) {
94 0 : IpAddress eidAddress;
95 0 : UboeIpv4ToEid(localIp, eidAddress, devPhyId);
96 0 : localIp = eidAddress;
97 : }
98 :
99 60 : RdmaHandle res = rdmaHandleMap[devPhyId][localProto][localIp];
100 60 : if (res == nullptr) {
101 7 : if (localProto == LinkProtoType::RDMA) {
102 1 : res = Create(devPhyId, localPort);
103 6 : } else if (localProto == LinkProtoType::UB) {
104 6 : HrtRaUbCtxInitParam in(HrtNetworkMode::HDC, devPhyId, localIp);
105 6 : res = HrtRaUbCtxInit(in);
106 6 : rdmaHandleMap[devPhyId][localProto][localIp] = res;
107 6 : tokenInfoMap[res] = std::make_unique<TokenInfoManager>(devPhyId, res);
108 6 : activeHandles_.insert(res);
109 18 : HCCL_INFO("Create one rdmahandle [%p], devPhyId [%u], portAddr [%s]",
110 : res, devPhyId, localPort.GetAddr().Describe().c_str());
111 : }
112 : }
113 :
114 180 : HCCL_INFO("[RdmaHandleManager::Get] one rdmahandle [%p], devPhyId [%u], portAddr [%s]",
115 : res, devPhyId, localPort.GetAddr().Describe().c_str());
116 60 : return res;
117 60 : }
118 :
119 109 : RdmaHandle RdmaHandleManager::GetByAddr(u32 devPhyId, const LinkProtoType &localProtocolType,
120 : IpAddress &localIp, PortDeploymentType type)
121 : {
122 109 : std::lock_guard<std::mutex> lock(managerMutex);
123 :
124 109 : if (devPhyId > rdmaHandleMap.size() - 1 || localProtocolType == LinkProtoType::HCCS_PCIE) {
125 0 : return nullptr;
126 : }
127 :
128 109 : RdmaHandle res = rdmaHandleMap[devPhyId][localProtocolType][localIp];
129 109 : if (res == nullptr) {
130 3 : if (localProtocolType == LinkProtoType::RDMA) {
131 3 : res = Create(devPhyId, localProtocolType, localIp, type);
132 0 : } else if (localProtocolType == LinkProtoType::UB) {
133 0 : HrtNetworkMode mode = (type == Hccl::PortDeploymentType::DEV_NET) ? HrtNetworkMode::HDC : HrtNetworkMode::PEER;
134 0 : HrtRaUbCtxInitParam in(mode, devPhyId, localIp);
135 0 : res = HrtRaUbCtxInit(in);
136 0 : rdmaHandleMap[devPhyId][localProtocolType][localIp] = res;
137 0 : tokenInfoMap[res] = std::make_unique<TokenInfoManager>(devPhyId, res);
138 0 : activeHandles_.insert(res);
139 0 : HCCL_INFO("Create one rdmahandle [%p], devPhyId [%u], portAddr [%s]",
140 : res, devPhyId, localIp.Describe().c_str());
141 : }
142 : }
143 109 : HCCL_INFO("[RdmaHandleManager::GetByAddr] one rdmahandle [%p], devPhyId [%u], portAddr [%s]",
144 : res, devPhyId, localIp.Describe().c_str());
145 109 : return res;
146 109 : }
147 :
148 46 : RdmaHandle RdmaHandleManager::GetByIp(u32 devPhyId, const IpAddress &localIp)
149 : {
150 46 : std::lock_guard<std::mutex> lock(managerMutex);
151 :
152 46 : if (devPhyId > rdmaHandleMap.size() - 1) {
153 0 : HCCL_ERROR("[RdmaHandleManager][GetByIp]devPhyId[%u] is invalid, "
154 : "should be less than [%zu]", devPhyId, rdmaHandleMap.size());
155 0 : return nullptr;
156 : }
157 :
158 : // only support UB type
159 46 : RdmaHandle res = rdmaHandleMap[devPhyId][LinkProtoType::UB][localIp];
160 46 : if (res == nullptr) {
161 19 : HrtRaUbCtxInitParam in(HrtNetworkMode::HDC, devPhyId, localIp);
162 19 : res = HrtRaUbCtxInit(in);
163 19 : rdmaHandleMap[devPhyId][LinkProtoType::UB][localIp] = res;
164 19 : tokenInfoMap[res] = std::make_unique<TokenInfoManager>(devPhyId, res);
165 19 : activeHandles_.insert(res);
166 53 : HCCL_INFO("Create one rdmahandle [%p], devPhyId [%u], ipAddr [%s]",
167 : res, devPhyId, localIp.Describe().c_str());
168 : }
169 :
170 46 : return res;
171 46 : }
172 :
173 1131 : bool RdmaHandleManager::FindCachedJfcHandle(RdmaHandle rdmaHandle, HrtUbJfcMode jfcMode,
174 : JfcHandle &handle, CqCreateInfo &cqInfo)
175 : {
176 1131 : auto outerIt = jfcHandleMap.find(rdmaHandle);
177 1131 : if (outerIt == jfcHandleMap.end()) {
178 10 : return false;
179 : }
180 1121 : auto innerIt = outerIt->second.find(jfcMode);
181 1121 : if (innerIt == outerIt->second.end()) {
182 1 : return false;
183 : }
184 1120 : handle = innerIt->second;
185 1120 : cqInfo = cqInfoMap[innerIt->second];
186 1120 : return true;
187 : }
188 :
189 1130 : JfcHandle RdmaHandleManager::GetJfcHandle(RdmaHandle rdmaHandle, CqCreateInfo& cqInfo, HrtUbJfcMode jfcMode)
190 : {
191 1130 : std::lock_guard<std::mutex> lock(managerMutex);
192 :
193 1130 : if (rdmaHandle == nullptr) {
194 1 : THROW<InvalidParamsException>("[RdmaHandleManager][GetJfcHandle]rdmaHandle is nullptr, please check input.");
195 : }
196 :
197 1129 : if (jfcMode != HrtUbJfcMode::STARS_POLL && jfcMode != HrtUbJfcMode::CCU_POLL && jfcMode != HrtUbJfcMode::NORMAL) {
198 1 : THROW<InvalidParamsException>("[RdmaHandleManager][GetJfcHandle]jfcMode[%s] is not STARS_POLL or CCU_POLL or NORMAL, "
199 3 : "please check input.", jfcMode.Describe().c_str());
200 : }
201 :
202 1128 : JfcHandle cachedHandle{};
203 1128 : if (FindCachedJfcHandle(rdmaHandle, jfcMode, cachedHandle, cqInfo)) {
204 1119 : return cachedHandle;
205 : }
206 9 : JfcHandle newHandle = HrtRaUbCreateJfc(rdmaHandle, cqInfo, jfcMode);
207 9 : JfcHandle &ref = jfcHandleMap[rdmaHandle][jfcMode]; // 引用缓存
208 9 : ref = newHandle;
209 9 : cqInfoMap[ref] = cqInfo;
210 9 : return ref;
211 1130 : }
212 :
213 0 : JfcHandle RdmaHandleManager::GetJfcHandleAndCqInfo(RdmaHandle rdmaHandle, CqCreateInfo& cqInfo, HrtUbJfcMode jfcMode)
214 : {
215 0 : std::lock_guard<std::mutex> lock(managerMutex);
216 :
217 0 : if (rdmaHandle == nullptr) {
218 0 : THROW<InvalidParamsException>("[RdmaHandleManager][GetJfcHandleAndCqInfo]rdmaHandle is nullptr, please check input.");
219 : }
220 :
221 0 : if (jfcMode != HrtUbJfcMode::USER_CTL) {
222 0 : THROW<InvalidParamsException>("[RdmaHandleManager][GetJfcHandleAndCqInfo]jfcMode[%s] is not USER_CTL, "
223 0 : "please check input.", jfcMode.Describe().c_str());
224 : }
225 :
226 0 : JfcHandle cachedHandle{};
227 0 : if (FindCachedJfcHandle(rdmaHandle, jfcMode, cachedHandle, cqInfo)) {
228 0 : return cachedHandle;
229 : }
230 0 : JfcHandle newHandle = HrtRaUbCreateJfcUserCtl(rdmaHandle, cqInfo);
231 0 : JfcHandle &ref = jfcHandleMap[rdmaHandle][jfcMode]; // 引用缓存
232 0 : ref = newHandle;
233 0 : cqInfoMap[ref] = cqInfo;
234 0 : return ref;
235 0 : }
236 :
237 153 : std::pair<uint32_t, uint32_t> RdmaHandleManager::GetDieAndFuncId(RdmaHandle rdmaHandle)
238 : {
239 153 : std::lock_guard<std::mutex> lock(managerMutex);
240 :
241 153 : if (rdmaHandle == nullptr) {
242 1 : THROW<InvalidParamsException>("[RdmaHandleManager][GetDieAndFuncId]rdmaHandle is nullptr, please check input.");
243 : }
244 :
245 152 : if (DieAndFuncIdMap.find(rdmaHandle) != DieAndFuncIdMap.end()) {
246 148 : return DieAndFuncIdMap[rdmaHandle];
247 : }
248 :
249 4 : DieAndFuncIdMap[rdmaHandle] = HraGetDieAndFuncId(rdmaHandle);
250 4 : return DieAndFuncIdMap[rdmaHandle];
251 153 : }
252 :
253 24 : bool RdmaHandleManager::GetRtpEnable(RdmaHandle rdmaHandle)
254 : {
255 24 : std::lock_guard<std::mutex> lock(managerMutex);
256 :
257 24 : if (rdmaHandle == nullptr) {
258 0 : THROW<InvalidParamsException>("[RdmaHandleManager][GetRtpEnable]rdmaHandle is nullptr, please check input.");
259 : }
260 :
261 24 : if (RtpEnableMap.find(rdmaHandle) != RtpEnableMap.end()) {
262 23 : return RtpEnableMap[rdmaHandle];
263 : }
264 :
265 1 : RtpEnableMap[rdmaHandle] = HraGetRtpEnable(rdmaHandle);
266 3 : HCCL_RUN_INFO("[%s] GetRtpEnable return[%d]", __func__, RtpEnableMap[rdmaHandle]);
267 1 : return RtpEnableMap[rdmaHandle];
268 24 : }
269 :
270 140 : std::pair<TokenIdHandle, uint32_t> RdmaHandleManager::GetTokenIdInfo(RdmaHandle rdmaHandle, const BufferKey<uintptr_t, u64> &bufKey)
271 : {
272 140 : std::lock_guard<std::mutex> lock(managerMutex);
273 :
274 140 : if (rdmaHandle == nullptr) {
275 1 : THROW<InvalidParamsException>("[RdmaHandleManager::%s]rdmaHandle is nullptr, please check input.", __func__);
276 : }
277 :
278 139 : if (tokenInfoMap.find(rdmaHandle) == tokenInfoMap.end()) {
279 1 : THROW<InvalidParamsException>("[RdmaHandleManager::%s]tokenInfoManager is nullptr, please check.", __func__);
280 : }
281 :
282 138 : std::pair<TokenIdHandle, uint32_t> tokenInfo = tokenInfoMap[rdmaHandle]->GetTokenInfo(bufKey);
283 :
284 414 : HCCL_INFO("[RdmaHandleManager::%s] Addr[%llu] Size[%llu] rdmahandle[%p]", __func__, bufKey.Addr(), bufKey.Size(), rdmaHandle);
285 138 : return tokenInfo;
286 140 : }
287 :
288 111 : void RdmaHandleManager::PutTokenIdInfo(RdmaHandle rdmaHandle, const BufferKey<uintptr_t, u64> &bufKey,
289 : TokenIdHandle tokenIdHandle)
290 : {
291 111 : std::lock_guard<std::mutex> lock(managerMutex);
292 :
293 111 : if (rdmaHandle == nullptr) {
294 0 : HCCL_WARNING("[RdmaHandleManager::%s]rdmaHandle is nullptr", __func__);
295 0 : return;
296 : }
297 :
298 111 : if (tokenInfoMap.find(rdmaHandle) == tokenInfoMap.end()) {
299 0 : HCCL_WARNING("[RdmaHandleManager::%s]tokenInfoManager is nullptr", __func__);
300 0 : return;
301 : }
302 :
303 111 : tokenInfoMap[rdmaHandle]->PutTokenInfo(bufKey, tokenIdHandle);
304 333 : HCCL_DEBUG("[RdmaHandleManager::%s] Addr[%llu] Size[%llu] rdmahandle[%p]",
305 : __func__, bufKey.Addr(), bufKey.Size(), rdmaHandle);
306 111 : }
307 :
308 : constexpr u32 UB_HANDLE_INDEX = 3;
309 : constexpr u32 RDMA_HANDLE_INDEX = 2;
310 :
311 4 : void RdmaHandleManager::DestroyAll()
312 : {
313 4 : if(destroyed.load()) {
314 1 : return;
315 : }
316 3 : destroyed.store(true);
317 :
318 3 : std::lock_guard<std::mutex> lock(managerMutex);
319 5 : HCCL_INFO("[RdmaHandleManager::%s] destroy all", __func__);
320 :
321 3 : activeHandles_.clear();
322 :
323 7 : for (auto &handleIter : jfcHandleMap) {
324 8 : for (auto &modeIter : handleIter.second) {
325 4 : DECTOR_TRY_CATCH("jfc handle destroy", HrtRaUbDestroyJfc(handleIter.first, modeIter.second));
326 : }
327 : }
328 :
329 198 : for (u32 i = 0; i < rdmaHandleMap.size(); ++i) {
330 975 : for (u32 j = 0; j < rdmaHandleMap[i].size(); ++j) {
331 805 : for (auto &handleIter : rdmaHandleMap[i][j]) {
332 25 : if (j == RDMA_HANDLE_INDEX && handleIter.second != nullptr) {
333 2 : DECTOR_TRY_CATCH("rdma handle deinit", HrtRaRdmaDeInit(handleIter.second, netWorkModeMap[handleIter.second]));
334 : }
335 25 : if (j == UB_HANDLE_INDEX && handleIter.second != nullptr) {
336 23 : if (tokenInfoMap[handleIter.second] != nullptr) {
337 23 : DECTOR_TRY_CATCH("token id handle destroy", tokenInfoMap[handleIter.second]->Destroy());
338 : }
339 23 : DECTOR_TRY_CATCH("ub handle destroy", HrtRaUbCtxDestroy(handleIter.second));
340 : }
341 : }
342 780 : rdmaHandleMap[i][j].clear();
343 : }
344 : }
345 :
346 3 : rdmaHandleMap.clear();
347 3 : DieAndFuncIdMap.clear();
348 3 : RtpEnableMap.clear();
349 3 : jfcHandleMap.clear();
350 3 : cqInfoMap.clear();
351 3 : netWorkModeMap.clear();
352 3 : }
353 :
354 0 : HcclResult GetEidByAnyEidInfo(s32 deviceLogicId, const HrtDevEidInfo& eidInfo, const IpAddress& ipV4Address, IpAddress& eidAddress)
355 : {
356 : // 根据eidInfo初始化rdmaHandle
357 0 : HrtRaUbCtxInitParam in(HrtNetworkMode::HDC, HrtGetDevicePhyIdByIndex(deviceLogicId), eidInfo.ipAddress);
358 0 : RdmaHandle rdmaHandle = HrtRaUbCtxInit(in);
359 :
360 : // 调用ra_get_eid_by_ip转换ipAddress为eid
361 0 : vector<IpAddress> eidAddrList{};
362 0 : CHK_RET(HrtRaGetEidByIp(rdmaHandle, {ipV4Address}, eidAddrList));
363 0 : if (eidAddrList.empty()) {
364 0 : HCCL_WARNING("[RdmaHandleManager::%s] Get Eid failed, deviceLogicId=%d, ipV4Address=%s",
365 : __func__, deviceLogicId, ipV4Address.Describe().c_str());
366 0 : return HCCL_E_NOT_FOUND;
367 : }
368 0 : eidAddress = eidAddrList.front();
369 0 : return HCCL_SUCCESS;
370 0 : }
371 :
372 : /* 将IPV4转为EID
373 : 1、基于IPV4 IpAddress查询uboeIpv4EidMap,如果存在直接返回
374 : 2、不存在时,调用hccp接口根据IPV4 IpAddress查询EID,并将其保存到uboeIpv4EidMap中
375 : */
376 11 : void RdmaHandleManager::UboeIpv4ToEid(const IpAddress& ipV4Address, IpAddress& eidAddress, u32 devPhyId)
377 : {
378 : // 如果无法查询设备是否为uboe设备,直接退出
379 11 : if (HrtGetUboeFlagEnable(devPhyId) != HCCL_SUCCESS) {
380 11 : return;
381 : }
382 0 : HCCL_INFO("[UboeIpv4ToEid] begin, ipV4Address[%s]", ipV4Address.Describe().c_str());
383 0 : auto it = uboeIpv4EidMap.find(ipV4Address);
384 0 : if (it != uboeIpv4EidMap.end()) {
385 0 : eidAddress = it->second;
386 0 : HCCL_INFO("[UboeIpv4ToEid] uboeIpv4EidMap find, eidAddress[%s]", it->second.Describe().c_str());
387 0 : return;
388 : }
389 :
390 0 : s32 deviceLogicId = HrtGetDevice();
391 0 : HRaInfo info(HrtNetworkMode::HDC, HrtGetDevicePhyIdByIndex(deviceLogicId));
392 0 : vector<HrtDevEidInfo> eidInfoList = HrtRaGetDevEidInfoList(info);
393 0 : if (eidInfoList.empty()) {
394 0 : HCCL_WARNING("[RdmaHandleManager::%s] Get EidInfoList empty, deviceLogicId=%d", __func__, deviceLogicId);
395 0 : return;
396 : }
397 0 : HCCL_INFO("[RdmaHandleManager::%s] Get EidInfo success, deviceLogicId=%d, eidInfo size=%u",
398 : __func__, deviceLogicId, eidInfoList.size());
399 :
400 0 : for (const auto& eidInfo : eidInfoList) {
401 0 : if (HrtCheckUboeSupported(eidInfo.devFeature) &&
402 0 : GetEidByAnyEidInfo(deviceLogicId, eidInfo, ipV4Address, eidAddress) == HCCL_SUCCESS) {
403 : // 存储eid到AddressInfo
404 0 : HCCL_INFO("[UboeIpv4ToEid] success, eidAddress[%s]", eidAddress.Describe().c_str());
405 0 : uboeIpv4EidMap.insert(std::make_pair(ipV4Address, eidAddress));
406 0 : return;
407 : }
408 : }
409 0 : HCCL_WARNING("[RdmaHandleManager::%s] Get EidInfo failed, deviceLogicId=%d", __func__, deviceLogicId);
410 0 : }
411 :
412 0 : HcclResult RdmaHandleManager::GetEidByIpv4Addr(const IpAddress& addr, IpAddress& eidAddr)
413 : {
414 0 : auto it = uboeIpv4EidMap.find(addr);
415 0 : if (it == uboeIpv4EidMap.end()) {
416 0 : HCCL_WARNING("[RdmaHandleManager::%s] Find Eid failed, addr[%s]", __func__, addr.Describe().c_str());
417 0 : return HCCL_E_PARA;
418 : }
419 0 : eidAddr = it->second;
420 0 : return HCCL_SUCCESS;
421 : }
422 :
423 11 : void RdmaHandleManager::CollectHandlesToCleanup(u32 devPhyId, std::vector<HandleInfo> &handlesToCleanup)
424 : {
425 55 : for(u32 j = 0; j < rdmaHandleMap[devPhyId].size(); ++j) {
426 58 : for (auto &handleIter : rdmaHandleMap[devPhyId][j]) {
427 14 : if (handleIter.second != nullptr) {
428 14 : handlesToCleanup.push_back({handleIter.second, j});
429 : }
430 : }
431 44 : rdmaHandleMap[devPhyId][j].clear();
432 : }
433 11 : }
434 :
435 14 : void RdmaHandleManager::CleanupJfcHandles(RdmaHandle handle)
436 : {
437 14 : auto jfcIt = jfcHandleMap.find(handle);
438 14 : if (jfcIt != jfcHandleMap.end()) {
439 8 : for (auto &modeIter : jfcIt->second) {
440 4 : DECTOR_TRY_CATCH("jfc handle destroy", HrtRaUbDestroyJfc(handle, modeIter.second));
441 4 : cqInfoMap.erase(modeIter.second);
442 : }
443 4 : jfcHandleMap.erase(jfcIt);
444 : }
445 14 : }
446 :
447 5 : void RdmaHandleManager::CleanupRdmaHandleEntry(RdmaHandle handle)
448 : {
449 5 : auto modeIt = netWorkModeMap.find(handle);
450 5 : if (modeIt != netWorkModeMap.end()) {
451 5 : DECTOR_TRY_CATCH("rdma handle deinit", HrtRaRdmaDeInit(handle, modeIt->second));
452 5 : netWorkModeMap.erase(modeIt);
453 : }
454 5 : }
455 :
456 9 : void RdmaHandleManager::CleanupUbHandleEntry(RdmaHandle handle)
457 : {
458 9 : auto tokenIt = tokenInfoMap.find(handle);
459 9 : if (tokenIt != tokenInfoMap.end()) {
460 4 : if (tokenIt->second != nullptr) {
461 2 : DECTOR_TRY_CATCH("token id handle destroy", tokenIt->second->Destroy());
462 : }
463 4 : DECTOR_TRY_CATCH("ub handle destroy", HrtRaUbCtxDestroy(handle));
464 4 : tokenInfoMap.erase(tokenIt);
465 : } else {
466 5 : DECTOR_TRY_CATCH("ub handle destroy", HrtRaUbCtxDestroy(handle));
467 : }
468 9 : }
469 :
470 14 : void RdmaHandleManager::CleanupAuxiliaryMaps(RdmaHandle handle)
471 : {
472 14 : DieAndFuncIdMap.erase(handle);
473 14 : RtpEnableMap.erase(handle);
474 14 : }
475 :
476 14 : void RdmaHandleManager::CleanupSingleHandle(const HandleInfo &info)
477 : {
478 14 : RdmaHandle handle = info.handle;
479 14 : CleanupJfcHandles(handle);
480 14 : if(info.protoIndex == RDMA_HANDLE_INDEX) {
481 5 : CleanupRdmaHandleEntry(handle);
482 : }
483 14 : if (info.protoIndex == UB_HANDLE_INDEX) {
484 9 : CleanupUbHandleEntry(handle);
485 : }
486 14 : CleanupAuxiliaryMaps(handle);
487 14 : }
488 :
489 13 : void RdmaHandleManager::DeInit(u32 devPhyId)
490 : {
491 13 : if(destroyed.load()) {
492 2 : return;
493 : }
494 12 : HCCL_INFO("[RdmaHandleManager][%s] DeInit[%u]", __func__, devPhyId);
495 :
496 12 : std::lock_guard<std::mutex> lock(managerMutex);
497 12 : if (devPhyId >= rdmaHandleMap.size()) {
498 1 : HCCL_INFO("[RdmaHandleManager][%s] devPhyId[%u] is out of range", __func__, devPhyId);
499 1 : return;
500 : }
501 :
502 11 : std::vector<HandleInfo> handlesToCleanup;
503 11 : CollectHandlesToCleanup(devPhyId, handlesToCleanup);
504 :
505 25 : for (auto &info : handlesToCleanup) {
506 14 : activeHandles_.erase(info.handle);
507 : }
508 :
509 25 : for(auto &info : handlesToCleanup) {
510 14 : CleanupSingleHandle(info);
511 : }
512 12 : }
513 :
514 18 : bool RdmaHandleManager::IsHandleValid(RdmaHandle handle)
515 : {
516 18 : if (handle == nullptr) {
517 1 : return false;
518 : }
519 17 : std::lock_guard<std::mutex> lock(managerMutex);
520 17 : if (destroyed.load()) {
521 1 : return false;
522 : }
523 16 : return activeHandles_.find(handle) != activeHandles_.end();
524 17 : }
525 :
526 : } // namespace Hccl
|