LCOV - code coverage report
Current view: top level - base_comm/resources/endpoints/server_socket - server_socket_mgr.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 24.6 % 61 15
Test Date: 2026-08-18 17:47:01 Functions: 50.0 % 4 2

            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 "server_socket_mgr.h"
      12              : #include "hccl_common.h"
      13              : #include "exception_handler.h"
      14              : #include "orion_adpt_utils.h"
      15              : #include "socket_handle_manager.h"
      16              : 
      17              : namespace hcomm {
      18              : 
      19            0 : HcclResult ServerSocketMgr::ListenStart(const uint32_t devPhyId, const CommAddr& commAddr, const Hccl::NicType nicType)
      20              : {
      21            0 :     if (nicType != Hccl::NicType::DEVICE_NIC_TYPE && nicType != Hccl::NicType::HOST_NIC_TYPE) {
      22            0 :         HCCL_ERROR("[%s] nicType[%d] is not supported", __func__, static_cast<int>(nicType)); // 枚举用转换吗?
      23            0 :         return HCCL_E_PARA;
      24              :     }
      25              : 
      26            0 :     auto& socketMgr = ServerSocketMgr::GetInstance(devPhyId);
      27              : 
      28            0 :     CHK_RET(socketMgr.ListenStart_(commAddr, nicType));
      29              : 
      30            0 :     return HcclResult::HCCL_SUCCESS;
      31              : }
      32              : 
      33            3 : ServerSocketMgr& ServerSocketMgr::GetInstance(const uint32_t devicePhyId)
      34              : {
      35           69 :     static ServerSocketMgr socketMgr[MAX_MODULE_DEVICE_NUM + 1];
      36              : 
      37            3 :     uint32_t devPhyId = devicePhyId;
      38            3 :     if (devPhyId >= MAX_MODULE_DEVICE_NUM + 1) {
      39            0 :         HCCL_WARNING(
      40              :             "[%s] Invalid devicePhyId: %u, max allowed: %u, using default index: %u", __func__, devicePhyId,
      41              :             MAX_MODULE_DEVICE_NUM, MAX_MODULE_DEVICE_NUM);
      42            0 :         devPhyId = MAX_MODULE_DEVICE_NUM;
      43              :     }
      44              : 
      45            3 :     socketMgr[devPhyId].devPhyId_ = devPhyId;
      46            3 :     return socketMgr[devPhyId];
      47              : }
      48              : 
      49            0 : HcclResult ServerSocketMgr::ListenStart_(const CommAddr& commAddr, const Hccl::NicType nicType)
      50              : {
      51            0 :     std::lock_guard<std::mutex> lock(innerMutex_);
      52            0 :     Hccl::IpAddress ipAddr{};
      53            0 :     CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
      54              : 
      55            0 :     if (nicType == Hccl::NicType::DEVICE_NIC_TYPE) {
      56            0 :         auto ipIter = deviceServerSocketMap_.find(ipAddr);
      57            0 :         if (ipIter != deviceServerSocketMap_.end()) {
      58            0 :             HCCL_INFO("[ServerSocketMgr][%s] device server socket already created.", __func__);
      59            0 :             return HcclResult::HCCL_SUCCESS;
      60              :         }
      61              :     } else {
      62            0 :         auto ipIter = hostServerSocketMap_.find(ipAddr);
      63            0 :         if (ipIter != hostServerSocketMap_.end()) {
      64            0 :             HCCL_INFO("[ServerSocketMgr][%s] host server socket already created.", __func__);
      65            0 :             return HcclResult::HCCL_SUCCESS;
      66              :         }
      67              :     }
      68              : 
      69              :     EXCEPTION_HANDLE_BEGIN
      70            0 :     const Hccl::DevNetPortType portType = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB); // 不能写死
      71              :     // todo: 暂时使用devPhyId构造rankId,id存疑?
      72            0 :     Hccl::PortData localPort = Hccl::PortData(static_cast<Hccl::RankId>(devPhyId_), portType, 0, ipAddr);
      73              : 
      74            0 :     HCCL_INFO(
      75              :         "[ServerSocketMgr][%s] get socket handle, devPhyId[%u] locAddr[%s].", __func__, devPhyId_,
      76              :         ipAddr.Describe().c_str());
      77              : 
      78            0 :     Hccl::SocketHandle socketHandle = Hccl::SocketHandleManager::GetInstance().Create(devPhyId_, localPort);
      79              : 
      80            0 :     std::unique_ptr<Hccl::Socket> serverSocket = nullptr;
      81            0 :     constexpr uint32_t listenPort = 60001; // 端口号如何处理,可能冲突
      82            0 :     const std::string tag = "server";
      83            0 :     constexpr Hccl::SocketRole role = Hccl::SocketRole::SERVER;
      84              : 
      85            0 :     HCCL_INFO(
      86              :         "[ServerSocketMgr][%s] create server socket, "
      87              :         "locAddr[%s] rmtAddr[%s] tag[%s] role[%s].",
      88              :         __func__, ipAddr.Describe().c_str(), ipAddr.Describe().c_str(), tag.c_str(), role.Describe().c_str());
      89              : 
      90            0 :     serverSocket.reset(new (std::nothrow) Hccl::Socket(socketHandle, ipAddr, listenPort, ipAddr, tag, role, nicType));
      91            0 :     CHK_PTR_NULL(serverSocket);
      92            0 :     serverSocket->Listen();
      93              : 
      94            0 :     if (nicType == Hccl::NicType::DEVICE_NIC_TYPE) {
      95            0 :         deviceServerSocketMap_[ipAddr] = std::move(serverSocket); // IP校验?
      96              :     } else {
      97            0 :         hostServerSocketMap_[ipAddr] = std::move(serverSocket); // IP校验?
      98              :     }
      99            0 :     EXCEPTION_HANDLE_END
     100            0 :     return HcclResult::HCCL_SUCCESS;
     101            0 : }
     102              : 
     103            3 : void ServerSocketMgr::DeInit(u32 devPhyId)
     104              : {
     105            3 :     HCCL_INFO("[ServerSocketMgr][%s] DeInit[%u]", __func__, devPhyId);
     106            3 :     auto& inst = GetInstance(devPhyId);
     107            3 :     std::lock_guard<std::mutex> lock(inst.innerMutex_);
     108            3 :     for (auto& it : inst.deviceServerSocketMap_) {
     109            0 :         if (it.second != nullptr) {
     110            0 :             it.second->Destroy();
     111            0 :             it.second.reset();
     112              :         }
     113              :     }
     114            3 :     inst.deviceServerSocketMap_.clear();
     115              : 
     116            3 :     for (auto& it : inst.hostServerSocketMap_) {
     117            0 :         if (it.second != nullptr) {
     118            0 :             it.second->Destroy();
     119            0 :             it.second.reset();
     120              :         }
     121              :     }
     122            3 :     inst.hostServerSocketMap_.clear();
     123            3 : }
     124              : 
     125              : }; // namespace hcomm
        

Generated by: LCOV version 2.0-1