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 "global_net_dev_manager.h"
12 : #include <string>
13 : #include "hccl_mem.h"
14 : // for hccl_network.h
15 : #include "hccl_network.h"
16 : #include "network_manager_pub.h"
17 : #include "dlhal_function.h"
18 : #include "dlra_function.h"
19 :
20 : using namespace hccl;
21 :
22 : namespace hccl {
23 : std::map<PortInfo, std::pair<NicType, HcclNetDevCtx>> GlobalNetDevMgr::netDevCtxMap_;
24 : std::map<PortInfo, Referenced> GlobalNetDevMgr::netDevCtxRefMap_;
25 : std::mutex GlobalNetDevMgr::netDevCtxMtx_;
26 : bool GlobalNetDevMgr::isDlRaInited_{false};
27 :
28 : std::map<PortInfo, std::shared_ptr<HcclSocket>> GlobalNetDevMgr::serverSocketMap_;
29 : std::map<PortInfo, Referenced> GlobalNetDevMgr::serverSocketRefMap_;
30 : std::mutex GlobalNetDevMgr::serverMapMutex_;
31 :
32 : // reserve 1 instance for invalid deviceid and host
33 : static GlobalNetDevMgr netDevMgrInstance[MAX_MODULE_DEVICE_NUM + 1];
34 2838 : GlobalNetDevMgr::~GlobalNetDevMgr()
35 : {
36 2838 : HCCL_INFO("[GlobalNetDevMgr][%s] start.", __func__);
37 2838 : if (isInited_) {
38 1 : UnInit();
39 : }
40 2838 : HCCL_INFO("[GlobalNetDevMgr][%s] end.", __func__);
41 2838 : }
42 :
43 36 : GlobalNetDevMgr& GlobalNetDevMgr::GetInstance(u32 devicePhyId)
44 : {
45 : u32 deviceLogicId;
46 36 : HcclResult hcclRet = hrtGetDeviceIndexByPhyId(devicePhyId, deviceLogicId);
47 36 : if (hcclRet != HCCL_SUCCESS) {
48 0 : HCCL_RUN_WARNING("GlobalNetDevMgr::GetInstance hrtGetDeviceIndexByPhyId failed, ret[%d], "
49 : "return reserve instance", hcclRet);
50 0 : return netDevMgrInstance[MAX_MODULE_DEVICE_NUM];
51 : }
52 :
53 36 : if (deviceLogicId >= MAX_MODULE_DEVICE_NUM) {
54 0 : HCCL_RUN_WARNING("[Get][Instance]deviceLogicId[%u] is invalid, return reserve instance", deviceLogicId);
55 0 : return netDevMgrInstance[MAX_MODULE_DEVICE_NUM];
56 : }
57 :
58 36 : if (!netDevMgrInstance[deviceLogicId].isInited_) {
59 12 : hcclRet = Init(devicePhyId, deviceLogicId);
60 12 : if (hcclRet != HCCL_SUCCESS) {
61 0 : HCCL_RUN_WARNING("[Get][Instance]Init deviceLogicId[%u]fail, return reserve instance", deviceLogicId);
62 0 : return netDevMgrInstance[MAX_MODULE_DEVICE_NUM];
63 : }
64 : }
65 :
66 36 : HCCL_DEBUG("GlobalNetDevMgr::GetInstance deviceLogicId[%u], devicePhyId[%u] done.", deviceLogicId, devicePhyId);
67 36 : return netDevMgrInstance[deviceLogicId];
68 : }
69 :
70 12 : HcclResult GlobalNetDevMgr::Init(u32 devicePhyId, u32 deviceLogicId)
71 : {
72 : // init after get the lock
73 12 : std::unique_lock<std::mutex> lock(netDevCtxMtx_);
74 12 : if (netDevMgrInstance[deviceLogicId].isInited_) {
75 0 : return HCCL_SUCCESS;
76 : }
77 :
78 12 : if (!isDlRaInited_) {
79 1 : CHK_RET(hccl::DlRaFunction::GetInstance().DlRaFunctionInit());
80 1 : CHK_RET(hccl::DlHalFunction::GetInstance().DlHalFunctionInit());
81 1 : isDlRaInited_ = true;
82 : }
83 :
84 : // need to check again
85 12 : if (netDevMgrInstance[deviceLogicId].isInited_) {
86 0 : HCCL_INFO("[GlobalNetDevMgr][Init]Has been inited. devicePhyId[%u], deviceLogicId[%u]",
87 : devicePhyId, deviceLogicId);
88 0 : return HCCL_SUCCESS;
89 : }
90 :
91 12 : netDevMgrInstance[deviceLogicId].devicePhyId_ = devicePhyId;
92 12 : netDevMgrInstance[deviceLogicId].deviceLogicId_ = deviceLogicId;
93 12 : CHK_RET(HcclNetInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId, static_cast<u32>(deviceLogicId), false));
94 12 : netDevMgrInstance[deviceLogicId].isInited_ = true;
95 12 : HCCL_INFO("[GlobalNetDevMgr][Init]Init success, devicePhyId[%u], deviceLogicId[%u]",
96 : devicePhyId, deviceLogicId);
97 12 : return HCCL_SUCCESS;
98 12 : }
99 :
100 12 : void GlobalNetDevMgr::UnInit()
101 : {
102 12 : if (!isInited_) {
103 0 : HCCL_INFO(
104 : "[GlobalNetDevMgr][UnInit]has been deinited. devicePhyId[%u], deviceLogicId[%d]",
105 : devicePhyId_, deviceLogicId_);
106 0 : return;
107 : }
108 :
109 12 : (void)HcclNetDeInit(NICDeployment::NIC_DEPLOYMENT_DEVICE, devicePhyId_, static_cast<u32>(deviceLogicId_));
110 12 : netDevCtx_ = nullptr;
111 12 : isInited_ = false;
112 12 : HCCL_INFO("[GlobalNetDevMgr][UnInit]UnInit success. devicePhyId[%u], deviceLogicId[%d]",
113 : devicePhyId_, deviceLogicId_);
114 : }
115 :
116 0 : HcclResult GlobalNetDevMgr::GetDeviceVnicIP(u32 devicePhyId, u32 superDeviceId, hccl::HcclIpAddress &vnicIP)
117 : {
118 : s32 localDeviceLogicId;
119 : u32 localDeviceId;
120 0 : CHK_RET(hrtGetDevice(&localDeviceLogicId));
121 0 : CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<u32>(localDeviceLogicId), localDeviceId));
122 :
123 : // 先创建进程
124 : bool isHostUseDevNic;
125 0 : CHK_RET(IsHostUseDevNic(isHostUseDevNic));
126 0 : u32 tempDevicePhyId = hccl::DEFAULT_PHY_ID;
127 0 : HCCL_DEBUG("[GlobalNetDevMgr][%s]GetDeviceVnicIP, deviceLogicId[%d], devicePhyId[%u], "
128 : "nicDeploy[%d], hasBackup[%d], tempDevicePhyId[%u]",
129 : __func__,
130 : localDeviceLogicId,
131 : devicePhyId,
132 : static_cast<int>(NICDeployment::NIC_DEPLOYMENT_DEVICE),
133 : false,
134 : tempDevicePhyId);
135 0 : CHK_RET(hccl::NetworkManager::GetInstance(localDeviceLogicId)
136 : .InitV2(NICDeployment::NIC_DEPLOYMENT_DEVICE, false, tempDevicePhyId, isHostUseDevNic));
137 :
138 : // 参考 Heartbeat::GetConnInfo
139 : // hccl::HcclIpAddress vnicIP(localDeviceId);
140 0 : if (superDeviceId != SUPER_DEVICE_ID_INVALID) {
141 0 : CHK_RET(hrtRaGetSingleSocketVnicIpInfo(localDeviceId, DeviceIdType::DEVICE_ID_TYPE_SDID,
142 : superDeviceId, vnicIP));
143 : } else {
144 0 : CHK_RET(hrtRaGetSingleSocketVnicIpInfo(localDeviceId, DeviceIdType::DEVICE_ID_TYPE_PHY_ID,
145 : devicePhyId, vnicIP));
146 : }
147 :
148 0 : HCCL_INFO("[GlobalNetDevMgr][GetDeviceVnicIP] vnicIP [%s] for devicePhyId[%u], superDeviceId[%u]",
149 : vnicIP.GetReadableAddress(), devicePhyId, superDeviceId);
150 :
151 : // 销毁进程
152 0 : CHK_RET(hccl::NetworkManager::GetInstance(localDeviceLogicId)
153 : .DeInitV2(NICDeployment::NIC_DEPLOYMENT_DEVICE, false, false));
154 0 : return HCCL_SUCCESS;
155 : }
156 :
157 14 : HcclResult GlobalNetDevMgr::RefNetDevCtx(NicType nicType, const HcclIpAddress &ipAddr, u32 port,
158 : HcclNetDevCtx &netDevCtx)
159 : {
160 14 : HCCL_INFO("[GlobalNetDevMgr][RefNetDevCtx] nicType[%d], ip[%s]", static_cast<int>(nicType), ipAddr.GetReadableAddress());
161 14 : std::lock_guard<std::mutex> lock(netDevCtxMtx_);
162 :
163 : // 进程粒度open dev,如果已open,直接复用
164 14 : PortInfo portInfo(ipAddr, port);
165 14 : if (netDevCtxMap_.find(portInfo) != netDevCtxMap_.end()) {
166 0 : netDevCtx = netDevCtxMap_[portInfo].second;
167 0 : CHK_PTR_NULL(netDevCtx);
168 :
169 0 : auto &netDevCtxRef = netDevCtxRefMap_[portInfo];
170 0 : netDevCtxRef.Ref();
171 0 : netDevCtx_ = netDevCtx;
172 :
173 0 : HCCL_INFO(
174 : "[GlobalNetDevMgr][RefNetDevCtx] nicType[%d] ip[%s] has been Ref.",
175 : static_cast<int>(nicType), ipAddr.GetReadableAddress());
176 0 : return HCCL_SUCCESS;
177 : }
178 :
179 : HcclNetDevCtx tempNetDevCtx;
180 14 : CHK_RET(HcclNetOpenDev(&tempNetDevCtx, nicType, devicePhyId_, deviceLogicId_, ipAddr));
181 14 : CHK_PTR_NULL(tempNetDevCtx);
182 :
183 : try {
184 14 : netDevCtxMap_.insert(std::make_pair(portInfo, std::make_pair(nicType, tempNetDevCtx)));
185 0 : } catch (...) {
186 0 : (void)HcclNetCloseDev(tempNetDevCtx);
187 0 : return HCCL_E_MEMORY;
188 0 : }
189 :
190 14 : Referenced ref;
191 14 : ref.Ref();
192 : try {
193 14 : netDevCtxRefMap_.insert(std::make_pair(portInfo, ref));
194 0 : } catch (...) {
195 0 : netDevCtxMap_.erase(portInfo);
196 0 : (void)HcclNetCloseDev(tempNetDevCtx);
197 0 : return HCCL_E_MEMORY;
198 0 : }
199 :
200 14 : netDevCtx = tempNetDevCtx;
201 14 : netDevCtx_ = netDevCtx;
202 14 : HCCL_INFO(
203 : "[GlobalNetDevMgr][RefNetDevCtx] nicType[%d] ip[%s] has been Init.", static_cast<int>(nicType), ipAddr.GetReadableAddress());
204 14 : return HCCL_SUCCESS;
205 14 : }
206 :
207 14 : HcclResult GlobalNetDevMgr::UnRefNetDevCtx(NicType nicType, const HcclIpAddress &ipAddr, u32 port)
208 : {
209 14 : HCCL_INFO("[GlobalNetDevMgr][UnRefNetDevCtx] nicType[%d], ip[%s]", static_cast<int>(nicType), ipAddr.GetReadableAddress());
210 :
211 14 : std::lock_guard<std::mutex> lock(netDevCtxMtx_);
212 :
213 : HcclNetDevCtx netDevCtx;
214 14 : PortInfo portInfo(ipAddr, port);
215 14 : if (netDevCtxMap_.find(portInfo) != netDevCtxMap_.end()) {
216 14 : netDevCtx = netDevCtxMap_[portInfo].second;
217 14 : CHK_PTR_NULL(netDevCtx);
218 :
219 14 : auto &netDevCtxRef = netDevCtxRefMap_[portInfo];
220 14 : netDevCtxRef.Unref();
221 14 : HCCL_INFO(
222 : "[GlobalNetDevMgr][UnRefNetDevCtx] nicType[%d] ip[%s] has been UnRef.",
223 : static_cast<int>(nicType), ipAddr.GetReadableAddress());
224 :
225 14 : if (netDevCtxRef.Count() == 0) {
226 14 : netDevCtxMap_.erase(portInfo);
227 14 : netDevCtxRefMap_.erase(portInfo);
228 14 : HcclNetCloseDev(netDevCtx);
229 14 : HCCL_INFO(
230 : "[GlobalNetDevMgr][UnRefNetDevCtx] nicType[%d] ip[%s] has been Deinit.",
231 : static_cast<int>(nicType), ipAddr.GetReadableAddress());
232 : }
233 : }
234 :
235 14 : if (netDevCtxMap_.empty()) {
236 11 : UnInit();
237 : }
238 14 : return HCCL_SUCCESS;
239 14 : }
240 :
241 2 : HcclResult GlobalNetDevMgr::ServerInit(u32 port)
242 : {
243 2 : HcclIpAddress localIp{0};
244 2 : CHK_RET(HcclNetDevGetLocalIp(netDevCtx_, localIp));
245 2 : HCCL_INFO("[GlobalNetDevMgr][ServerInit]ip[%s] port[%u]", localIp.GetReadableAddress(), port);
246 2 : PortInfo portInfo(localIp, port);
247 :
248 2 : std::unique_lock<std::mutex> lock(serverMapMutex_);
249 2 : auto serverSocketInMap = serverSocketMap_.find(portInfo);
250 2 : if (serverSocketInMap != serverSocketMap_.end()) {
251 0 : auto &serverSocketRef = serverSocketRefMap_[portInfo];
252 0 : serverSocketRef.Ref();
253 0 : HCCL_INFO("[GlobalNetDevMgr][ServerInit]ip[%s] port[%u] inited", localIp.GetReadableAddress(), port);
254 0 : return HCCL_SUCCESS;
255 : }
256 :
257 2 : std::shared_ptr<HcclSocket> tempSocket;
258 2 : EXCEPTION_CATCH((tempSocket = std::make_shared<HcclSocket>(
259 : netDevCtx_, port)), return HCCL_E_PTR);
260 2 : CHK_RET(tempSocket->Init());
261 2 : CHK_RET(tempSocket->Listen());
262 2 : serverSocketMap_.insert(std::make_pair(portInfo, tempSocket));
263 :
264 2 : Referenced ref;
265 2 : ref.Ref();
266 2 : serverSocketRefMap_.insert(std::make_pair(portInfo, ref));
267 2 : HCCL_INFO("[GlobalNetDevMgr][ServerInit]ip[%s] port[%u] init done", localIp.GetReadableAddress(), port);
268 2 : return HCCL_SUCCESS;
269 2 : }
270 :
271 2 : HcclResult GlobalNetDevMgr::ServerDeInit(u32 port)
272 : {
273 2 : HcclIpAddress localIp{0};
274 2 : CHK_RET(HcclNetDevGetLocalIp(netDevCtx_, localIp));
275 2 : CHK_RET(ServerDeInit(localIp, port));
276 :
277 2 : return HCCL_SUCCESS;
278 2 : }
279 :
280 2 : HcclResult GlobalNetDevMgr::ServerDeInit(const HcclIpAddress& localIp, u32 port)
281 : {
282 2 : PortInfo portInfo(localIp, port);
283 :
284 2 : std::unique_lock<std::mutex> lock(serverMapMutex_);
285 2 : auto res = serverSocketMap_.find(portInfo);
286 2 : if (res == serverSocketMap_.end()) {
287 0 : HCCL_INFO("[DeInit][Server]ip[%s] port[%u] not found", localIp.GetReadableAddress(), port);
288 0 : return HCCL_SUCCESS;
289 : }
290 :
291 2 : auto &serverSocketRef = serverSocketRefMap_[portInfo];
292 2 : serverSocketRef.Unref();
293 :
294 2 : HCCL_INFO("[DeInit][Server]ip[%s] port[%u] serverSocketRef.Count() = %d", localIp.GetReadableAddress(),
295 : port, serverSocketRef.Count());
296 2 : if (serverSocketRef.Count() == 0) {
297 2 : HCCL_INFO("[DeInit][Server]ip[%s] port[%u]", localIp.GetReadableAddress(), port);
298 2 : serverSocketMap_[portInfo]->DeInit();
299 2 : serverSocketMap_.erase(portInfo);
300 2 : serverSocketRefMap_.erase(portInfo);
301 : }
302 :
303 2 : return HCCL_SUCCESS;
304 2 : }
305 :
306 1 : HcclResult GlobalNetDevMgr::AddListenSocketWhiteList(const HcclIpAddress& localIp, uint32_t port,
307 : const std::vector<SocketWlistInfo> &wlistInfos)
308 : {
309 1 : if (wlistInfos.empty()) {
310 0 : HCCL_ERROR("[GlobalNetDevMgr][%s] empty whitelist", __func__);
311 0 : return HCCL_E_PARA;
312 : }
313 :
314 1 : PortInfo portInfo(localIp, port);
315 1 : auto it = serverSocketMap_.find(portInfo);
316 1 : if (it == serverSocketMap_.end() || it->second == nullptr) {
317 0 : HCCL_ERROR("[GlobalNetDevMgr][%s] no listen socket for port[%u]", __func__, port);
318 0 : return HCCL_E_NOT_FOUND;
319 : }
320 1 : std::vector<SocketWlistInfo> mutableCopy = wlistInfos;
321 1 : return it->second->AddWhiteList(mutableCopy);
322 1 : }
323 :
324 1 : HcclResult GlobalNetDevMgr::AcceptDataSocket(const HcclIpAddress& localIp, uint32_t port, const std::string &tag,
325 : std::shared_ptr<hccl::HcclSocket> &outConnected, uint32_t acceptTimeoutMs)
326 : {
327 1 : PortInfo portInfo(localIp, port);
328 1 : auto it = serverSocketMap_.find(portInfo);
329 1 : if (it == serverSocketMap_.end() || it->second == nullptr) {
330 0 : HCCL_ERROR("[GlobalNetDevMgr][%s] no listen socket for port[%u]", __func__, port);
331 0 : return HCCL_E_NOT_FOUND;
332 : }
333 1 : return it->second->Accept(tag, outConnected, acceptTimeoutMs);
334 1 : }
335 :
336 1 : HcclResult GlobalNetDevMgr::WaitClientSocketLinkEstablished(const std::shared_ptr<hccl::HcclSocket> &socket,
337 : s32 timeoutSec)
338 : {
339 1 : CHK_SMART_PTR_NULL(socket);
340 1 : u32 pollCount = 0;
341 1 : const auto startTime = std::chrono::steady_clock::now();
342 1 : const auto timeout = std::chrono::seconds(timeoutSec > 0 ? timeoutSec : GetExternalInputHcclLinkTimeOut());
343 1 : HCCL_DEBUG("[GlobalNetDevMgr][client][WaitLink] waiting for socket link up...");
344 : while (true) {
345 1 : if ((std::chrono::steady_clock::now() - startTime) >= timeout) {
346 0 : HCCL_ERROR("[GlobalNetDevMgr][client][WaitLink] wait socket establish timeout, timeout[%lld s]",
347 : static_cast<long long>(timeout.count()));
348 0 : socket->SetStatus(hccl::HcclSocketStatus::SOCKET_TIMEOUT);
349 0 : return HCCL_E_TIMEOUT;
350 : }
351 1 : const hccl::HcclSocketStatus status = socket->GetStatus();
352 1 : if (status == hccl::HcclSocketStatus::SOCKET_OK) {
353 1 : HCCL_DEBUG("[GlobalNetDevMgr][client][WaitLink] socket established. localIp[%s], remoteIp[%s]",
354 : socket->GetLocalIp().GetReadableIP(), socket->GetRemoteIp().GetReadableIP());
355 1 : return HCCL_SUCCESS;
356 : }
357 0 : if (status == hccl::HcclSocketStatus::SOCKET_CONNECTING) {
358 0 : SaluSleep(ONE_MILLISECOND_OF_USLEEP);
359 0 : if (pollCount % 50U == 0U) {
360 0 : HCCL_DEBUG("[GlobalNetDevMgr][client][WaitLink] socket is connecting");
361 : }
362 0 : ++pollCount;
363 0 : continue;
364 : }
365 0 : if (status == hccl::HcclSocketStatus::SOCKET_TIMEOUT) {
366 0 : return HCCL_E_TIMEOUT;
367 : }
368 0 : socket->SetStatus(hccl::HcclSocketStatus::SOCKET_ERROR);
369 0 : return HCCL_E_TCP_CONNECT;
370 0 : }
371 : }
372 :
373 2 : void GlobalNetDevMgr::MakeSocketTag(hccl::HcclIpAddress tagServerIp, uint32_t tagServerPort,
374 : hccl::HcclIpAddress tagClientIp, std::string &socketTag)
375 : {
376 8 : socketTag = tagServerIp.GetReadableIP() + std::string(":") + std::to_string(tagServerPort) +
377 8 : std::string(":") + tagClientIp.GetReadableIP();
378 2 : }
379 :
380 1 : HcclResult GlobalNetDevMgr::ConnectToServer(uint32_t localPort,
381 : hccl::HcclIpAddress remoteIp, uint32_t remotePort, std::string &socketTag,
382 : std::shared_ptr<hccl::HcclSocket> &socket)
383 : {
384 1 : HCCL_INFO("[GlobalNetDevMgr]ConnectToServer start");
385 :
386 1 : auto *netDevCtxPtr = static_cast<hccl::NetDevContext *>(netDevCtx_);
387 1 : hccl::HcclIpAddress localIpAddr = netDevCtxPtr->GetLocalIp();
388 :
389 1 : HCCL_INFO("[GlobalNetDevMgr]ConnectToServer localIp[%s] localPort[%u] remoteIp[%s] remotePort[%u] socketTag[%s]",
390 : localIpAddr.GetReadableIP(), localPort, remoteIp.GetReadableIP(), remotePort, socketTag.c_str());
391 :
392 1 : HCCL_INFO("[GlobalNetDevMgr][client] ConnectToServer connect to server");
393 1 : std::shared_ptr<hccl::HcclSocket> socketTemp = nullptr;
394 1 : EXCEPTION_CATCH(socketTemp = std::make_shared<hccl::HcclSocket>(socketTag, netDevCtx_, remoteIp, remotePort,
395 : hccl::HcclSocketRole::SOCKET_ROLE_CLIENT),
396 : return HCCL_E_PTR);
397 1 : CHK_SMART_PTR_NULL(socketTemp);
398 1 : CHK_RET(socketTemp->Init());
399 1 : CHK_RET(socketTemp->Connect());
400 1 : HcclResult waitRet = WaitClientSocketLinkEstablished(socketTemp, 0);
401 1 : if (waitRet != HCCL_SUCCESS) {
402 0 : socketTemp->Close();
403 0 : return waitRet;
404 : }
405 :
406 1 : socket = socketTemp;
407 1 : HCCL_INFO("[GlobalNetDevMgr]ConnectToServer done localPort[%u] remotePort[%u]",
408 : socket->GetLocalPort(), socket->GetRemotePort());
409 :
410 1 : return HCCL_SUCCESS;
411 1 : }
412 :
413 1 : HcclResult GlobalNetDevMgr::AcceptClient(uint32_t localPort,
414 : hccl::HcclIpAddress remoteIp, std::string &socketTag, std::shared_ptr<hccl::HcclSocket> &socket)
415 : {
416 1 : HCCL_INFO("[GlobalNetDevMgr]AcceptClient start");
417 :
418 1 : auto *netDevCtxPtr = static_cast<hccl::NetDevContext *>(netDevCtx_);
419 1 : hccl::HcclIpAddress localIpAddr = netDevCtxPtr->GetLocalIp();
420 :
421 1 : HCCL_INFO("[GlobalNetDevMgr]AcceptClient localIp[%s] localPort[%u] remoteIp[%s] socketTag[%s]",
422 : localIpAddr.GetReadableIP(), localPort, remoteIp.GetReadableIP(), socketTag.c_str());
423 :
424 1 : HCCL_INFO("[GlobalNetDevMgr][server] AcceptClient listen and accept");
425 1 : SocketWlistInfo wlistEntry{};
426 1 : wlistEntry.connLimit = 1U;
427 1 : const auto bin = remoteIp.GetBinaryAddress();
428 1 : wlistEntry.remoteIp.addr = bin.addr;
429 1 : wlistEntry.remoteIp.addr6 = bin.addr6;
430 1 : s32 mw = memcpy_s(wlistEntry.tag, sizeof(wlistEntry.tag), socketTag.c_str(), socketTag.size() + 1U);
431 1 : CHK_PRT_RET(mw != EOK, HCCL_ERROR("[GlobalNetDevMgr]memcpy_s whitelist tag failed"),
432 : HCCL_E_MEMORY);
433 2 : const std::vector<SocketWlistInfo> wlistVec = { wlistEntry };
434 1 : CHK_RET(AddListenSocketWhiteList(localIpAddr, localPort, wlistVec));
435 :
436 1 : std::shared_ptr<hccl::HcclSocket> socketTemp = nullptr;
437 1 : CHK_RET(AcceptDataSocket(localIpAddr, localPort, socketTag, socketTemp, 0));
438 1 : CHK_SMART_PTR_NULL(socketTemp);
439 :
440 1 : socket = socketTemp;
441 1 : HCCL_INFO("[GlobalNetDevMgr]AcceptClient done localPort[%u] remotePort[%u]",
442 : socket->GetLocalPort(), socket->GetRemotePort());
443 :
444 1 : return HCCL_SUCCESS;
445 1 : }
446 :
447 2 : void GlobalNetDevMgr::CloseSocket(std::shared_ptr<hccl::HcclSocket> &socket)
448 : {
449 2 : if (socket != nullptr) {
450 2 : socket->Close();
451 2 : socket = nullptr;
452 : }
453 2 : }
454 : } // namespace hccl
|