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