LCOV - code coverage report
Current view: top level - base_comm/resources/endpoints/net_dev - global_net_dev_manager.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 76.6 % 252 193
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 17 17

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

Generated by: LCOV version 2.0-1