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 2802 : RdmaHandleManager &RdmaHandleManager::GetInstance()
36 : {
37 2802 : static RdmaHandleManager rdmaHandleManager;
38 2802 : 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 1022 : bool RdmaHandleManager::FindCachedJfcHandle(RdmaHandle rdmaHandle, HrtUbJfcMode jfcMode,
174 : JfcHandle &handle, CqCreateInfo &cqInfo)
175 : {
176 1022 : auto outerIt = jfcHandleMap.find(rdmaHandle);
177 1022 : if (outerIt == jfcHandleMap.end()) {
178 10 : return false;
179 : }
180 1012 : auto innerIt = outerIt->second.find(jfcMode);
181 1012 : if (innerIt == outerIt->second.end()) {
182 1 : return false;
183 : }
184 1011 : handle = innerIt->second;
185 1011 : cqInfo = cqInfoMap[innerIt->second];
186 1011 : return true;
187 : }
188 :
189 1021 : JfcHandle RdmaHandleManager::GetJfcHandle(RdmaHandle rdmaHandle, CqCreateInfo& cqInfo, HrtUbJfcMode jfcMode)
190 : {
191 1021 : std::lock_guard<std::mutex> lock(managerMutex);
192 :
193 1021 : if (rdmaHandle == nullptr) {
194 1 : THROW<InvalidParamsException>("[RdmaHandleManager][GetJfcHandle]rdmaHandle is nullptr, please check input.");
195 : }
196 :
197 1020 : 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 1019 : JfcHandle cachedHandle{};
203 1019 : if (FindCachedJfcHandle(rdmaHandle, jfcMode, cachedHandle, cqInfo)) {
204 1010 : 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 1021 : }
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 152 : std::pair<uint32_t, uint32_t> RdmaHandleManager::GetDieAndFuncId(RdmaHandle rdmaHandle)
238 : {
239 152 : std::lock_guard<std::mutex> lock(managerMutex);
240 :
241 152 : if (rdmaHandle == nullptr) {
242 1 : THROW<InvalidParamsException>("[RdmaHandleManager][GetDieAndFuncId]rdmaHandle is nullptr, please check input.");
243 : }
244 :
245 151 : if (DieAndFuncIdMap.find(rdmaHandle) != DieAndFuncIdMap.end()) {
246 147 : return DieAndFuncIdMap[rdmaHandle];
247 : }
248 :
249 4 : DieAndFuncIdMap[rdmaHandle] = HraGetDieAndFuncId(rdmaHandle);
250 4 : return DieAndFuncIdMap[rdmaHandle];
251 152 : }
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 231 : std::pair<TokenIdHandle, uint32_t> RdmaHandleManager::GetTokenIdInfo(RdmaHandle rdmaHandle, const BufferKey<uintptr_t, u64> &bufKey)
271 : {
272 231 : std::lock_guard<std::mutex> lock(managerMutex);
273 :
274 231 : if (rdmaHandle == nullptr) {
275 1 : THROW<InvalidParamsException>("[RdmaHandleManager::%s]rdmaHandle is nullptr, please check input.", __func__);
276 : }
277 :
278 230 : if (tokenInfoMap.find(rdmaHandle) == tokenInfoMap.end()) {
279 1 : THROW<InvalidParamsException>("[RdmaHandleManager::%s]tokenInfoManager is nullptr, please check.", __func__);
280 : }
281 :
282 229 : std::pair<TokenIdHandle, uint32_t> tokenInfo = tokenInfoMap[rdmaHandle]->GetTokenInfo(bufKey);
283 :
284 687 : HCCL_INFO("[RdmaHandleManager::%s] Addr[%llu] Size[%llu] rdmahandle[%p]", __func__, bufKey.Addr(), bufKey.Size(), rdmaHandle);
285 229 : return tokenInfo;
286 231 : }
287 :
288 : constexpr u32 UB_HANDLE_INDEX = 3;
289 : constexpr u32 RDMA_HANDLE_INDEX = 2;
290 :
291 4 : void RdmaHandleManager::DestroyAll()
292 : {
293 4 : if(destroyed.load()) {
294 1 : return;
295 : }
296 3 : destroyed.store(true);
297 :
298 3 : std::lock_guard<std::mutex> lock(managerMutex);
299 5 : HCCL_INFO("[RdmaHandleManager::%s] destroy all", __func__);
300 :
301 3 : activeHandles_.clear();
302 :
303 7 : for (auto &handleIter : jfcHandleMap) {
304 8 : for (auto &modeIter : handleIter.second) {
305 4 : DECTOR_TRY_CATCH("jfc handle destroy", HrtRaUbDestroyJfc(handleIter.first, modeIter.second));
306 : }
307 : }
308 :
309 198 : for (u32 i = 0; i < rdmaHandleMap.size(); ++i) {
310 975 : for (u32 j = 0; j < rdmaHandleMap[i].size(); ++j) {
311 805 : for (auto &handleIter : rdmaHandleMap[i][j]) {
312 25 : if (j == RDMA_HANDLE_INDEX && handleIter.second != nullptr) {
313 2 : DECTOR_TRY_CATCH("rdma handle deinit", HrtRaRdmaDeInit(handleIter.second, netWorkModeMap[handleIter.second]));
314 : }
315 25 : if (j == UB_HANDLE_INDEX && handleIter.second != nullptr) {
316 23 : if (tokenInfoMap[handleIter.second] != nullptr) {
317 23 : DECTOR_TRY_CATCH("token id handle destroy", tokenInfoMap[handleIter.second]->Destroy());
318 : }
319 23 : DECTOR_TRY_CATCH("ub handle destroy", HrtRaUbCtxDestroy(handleIter.second));
320 : }
321 : }
322 780 : rdmaHandleMap[i][j].clear();
323 : }
324 : }
325 :
326 3 : rdmaHandleMap.clear();
327 3 : DieAndFuncIdMap.clear();
328 3 : RtpEnableMap.clear();
329 3 : jfcHandleMap.clear();
330 3 : cqInfoMap.clear();
331 3 : netWorkModeMap.clear();
332 3 : }
333 :
334 0 : HcclResult GetEidByAnyEidInfo(s32 deviceLogicId, const HrtDevEidInfo& eidInfo, const IpAddress& ipV4Address, IpAddress& eidAddress)
335 : {
336 : // 根据eidInfo初始化rdmaHandle
337 0 : HrtRaUbCtxInitParam in(HrtNetworkMode::HDC, HrtGetDevicePhyIdByIndex(deviceLogicId), eidInfo.ipAddress);
338 0 : RdmaHandle rdmaHandle = HrtRaUbCtxInit(in);
339 :
340 : // 调用ra_get_eid_by_ip转换ipAddress为eid
341 0 : vector<IpAddress> eidAddrList{};
342 0 : CHK_RET(HrtRaGetEidByIp(rdmaHandle, {ipV4Address}, eidAddrList));
343 0 : if (eidAddrList.empty()) {
344 0 : HCCL_WARNING("[RdmaHandleManager::%s] Get Eid failed, deviceLogicId=%d, ipV4Address=%s",
345 : __func__, deviceLogicId, ipV4Address.Describe().c_str());
346 0 : return HCCL_E_NOT_FOUND;
347 : }
348 0 : eidAddress = eidAddrList.front();
349 0 : return HCCL_SUCCESS;
350 0 : }
351 :
352 : /* 将IPV4转为EID
353 : 1、基于IPV4 IpAddress查询uboeIpv4EidMap,如果存在直接返回
354 : 2、不存在时,调用hccp接口根据IPV4 IpAddress查询EID,并将其保存到uboeIpv4EidMap中
355 : */
356 11 : void RdmaHandleManager::UboeIpv4ToEid(const IpAddress& ipV4Address, IpAddress& eidAddress, u32 devPhyId)
357 : {
358 : // 如果无法查询设备是否为uboe设备,直接退出
359 11 : if (HrtGetUboeFlagEnable(devPhyId) != HCCL_SUCCESS) {
360 11 : return;
361 : }
362 0 : HCCL_INFO("[UboeIpv4ToEid] begin, ipV4Address[%s]", ipV4Address.Describe().c_str());
363 0 : auto it = uboeIpv4EidMap.find(ipV4Address);
364 0 : if (it != uboeIpv4EidMap.end()) {
365 0 : eidAddress = it->second;
366 0 : HCCL_INFO("[UboeIpv4ToEid] uboeIpv4EidMap find, eidAddress[%s]", it->second.Describe().c_str());
367 0 : return;
368 : }
369 :
370 0 : s32 deviceLogicId = HrtGetDevice();
371 0 : HRaInfo info(HrtNetworkMode::HDC, HrtGetDevicePhyIdByIndex(deviceLogicId));
372 0 : vector<HrtDevEidInfo> eidInfoList = HrtRaGetDevEidInfoList(info);
373 0 : if (eidInfoList.empty()) {
374 0 : HCCL_WARNING("[RdmaHandleManager::%s] Get EidInfoList empty, deviceLogicId=%d", __func__, deviceLogicId);
375 0 : return;
376 : }
377 0 : HCCL_INFO("[RdmaHandleManager::%s] Get EidInfo success, deviceLogicId=%d, eidInfo size=%u",
378 : __func__, deviceLogicId, eidInfoList.size());
379 :
380 0 : for (const auto& eidInfo : eidInfoList) {
381 0 : if (HrtCheckUboeSupported(eidInfo.devFeature) &&
382 0 : GetEidByAnyEidInfo(deviceLogicId, eidInfo, ipV4Address, eidAddress) == HCCL_SUCCESS) {
383 : // 存储eid到AddressInfo
384 0 : HCCL_INFO("[UboeIpv4ToEid] success, eidAddress[%s]", eidAddress.Describe().c_str());
385 0 : uboeIpv4EidMap.insert(std::make_pair(ipV4Address, eidAddress));
386 0 : return;
387 : }
388 : }
389 0 : HCCL_WARNING("[RdmaHandleManager::%s] Get EidInfo failed, deviceLogicId=%d", __func__, deviceLogicId);
390 0 : }
391 :
392 0 : HcclResult RdmaHandleManager::GetEidByIpv4Addr(const IpAddress& addr, IpAddress& eidAddr)
393 : {
394 0 : auto it = uboeIpv4EidMap.find(addr);
395 0 : if (it == uboeIpv4EidMap.end()) {
396 0 : HCCL_WARNING("[RdmaHandleManager::%s] Find Eid failed, addr[%s]", __func__, addr.Describe().c_str());
397 0 : return HCCL_E_PARA;
398 : }
399 0 : eidAddr = it->second;
400 0 : return HCCL_SUCCESS;
401 : }
402 :
403 11 : void RdmaHandleManager::CollectHandlesToCleanup(u32 devPhyId, std::vector<HandleInfo> &handlesToCleanup)
404 : {
405 55 : for(u32 j = 0; j < rdmaHandleMap[devPhyId].size(); ++j) {
406 58 : for (auto &handleIter : rdmaHandleMap[devPhyId][j]) {
407 14 : if (handleIter.second != nullptr) {
408 14 : handlesToCleanup.push_back({handleIter.second, j});
409 : }
410 : }
411 44 : rdmaHandleMap[devPhyId][j].clear();
412 : }
413 11 : }
414 :
415 14 : void RdmaHandleManager::CleanupJfcHandles(RdmaHandle handle)
416 : {
417 14 : auto jfcIt = jfcHandleMap.find(handle);
418 14 : if (jfcIt != jfcHandleMap.end()) {
419 8 : for (auto &modeIter : jfcIt->second) {
420 4 : DECTOR_TRY_CATCH("jfc handle destroy", HrtRaUbDestroyJfc(handle, modeIter.second));
421 4 : cqInfoMap.erase(modeIter.second);
422 : }
423 4 : jfcHandleMap.erase(jfcIt);
424 : }
425 14 : }
426 :
427 5 : void RdmaHandleManager::CleanupRdmaHandleEntry(RdmaHandle handle)
428 : {
429 5 : auto modeIt = netWorkModeMap.find(handle);
430 5 : if (modeIt != netWorkModeMap.end()) {
431 5 : DECTOR_TRY_CATCH("rdma handle deinit", HrtRaRdmaDeInit(handle, modeIt->second));
432 5 : netWorkModeMap.erase(modeIt);
433 : }
434 5 : }
435 :
436 9 : void RdmaHandleManager::CleanupUbHandleEntry(RdmaHandle handle)
437 : {
438 9 : auto tokenIt = tokenInfoMap.find(handle);
439 9 : if (tokenIt != tokenInfoMap.end()) {
440 4 : if (tokenIt->second != nullptr) {
441 2 : DECTOR_TRY_CATCH("token id handle destroy", tokenIt->second->Destroy());
442 : }
443 4 : DECTOR_TRY_CATCH("ub handle destroy", HrtRaUbCtxDestroy(handle));
444 4 : tokenInfoMap.erase(tokenIt);
445 : } else {
446 5 : DECTOR_TRY_CATCH("ub handle destroy", HrtRaUbCtxDestroy(handle));
447 : }
448 9 : }
449 :
450 14 : void RdmaHandleManager::CleanupAuxiliaryMaps(RdmaHandle handle)
451 : {
452 14 : DieAndFuncIdMap.erase(handle);
453 14 : RtpEnableMap.erase(handle);
454 14 : }
455 :
456 14 : void RdmaHandleManager::CleanupSingleHandle(const HandleInfo &info)
457 : {
458 14 : RdmaHandle handle = info.handle;
459 14 : CleanupJfcHandles(handle);
460 14 : if(info.protoIndex == RDMA_HANDLE_INDEX) {
461 5 : CleanupRdmaHandleEntry(handle);
462 : }
463 14 : if (info.protoIndex == UB_HANDLE_INDEX) {
464 9 : CleanupUbHandleEntry(handle);
465 : }
466 14 : CleanupAuxiliaryMaps(handle);
467 14 : }
468 :
469 13 : void RdmaHandleManager::DeInit(u32 devPhyId)
470 : {
471 13 : if(destroyed.load()) {
472 2 : return;
473 : }
474 12 : HCCL_INFO("[RdmaHandleManager][%s] DeInit[%u]", __func__, devPhyId);
475 :
476 12 : std::lock_guard<std::mutex> lock(managerMutex);
477 12 : if (devPhyId >= rdmaHandleMap.size()) {
478 1 : HCCL_INFO("[RdmaHandleManager][%s] devPhyId[%u] is out of range", __func__, devPhyId);
479 1 : return;
480 : }
481 :
482 11 : std::vector<HandleInfo> handlesToCleanup;
483 11 : CollectHandlesToCleanup(devPhyId, handlesToCleanup);
484 :
485 25 : for (auto &info : handlesToCleanup) {
486 14 : activeHandles_.erase(info.handle);
487 : }
488 :
489 25 : for(auto &info : handlesToCleanup) {
490 14 : CleanupSingleHandle(info);
491 : }
492 12 : }
493 :
494 18 : bool RdmaHandleManager::IsHandleValid(RdmaHandle handle)
495 : {
496 18 : if (handle == nullptr) {
497 1 : return false;
498 : }
499 17 : std::lock_guard<std::mutex> lock(managerMutex);
500 17 : if (destroyed.load()) {
501 1 : return false;
502 : }
503 16 : return activeHandles_.find(handle) != activeHandles_.end();
504 17 : }
505 :
506 : } // namespace Hccl
|