LCOV - code coverage report
Current view: top level - base_comm/resources/endpoints - cpu_urma_endpoint.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 79.2 % 101 80
Test Date: 2026-08-25 19:18:03 Functions: 73.3 % 15 11

            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 "cpu_urma_endpoint.h"
      12              : #include <algorithm>
      13              : #include "endpoint_mgr.h"
      14              : #include "log.h"
      15              : #include "urma_mem.h"
      16              : #include "proc_reged_mem_mgr_cache.h"
      17              : #include "adapter_rts_common.h"
      18              : #include "server_socket_manager.h"
      19              : #include "hccp_peer_manager.h"
      20              : 
      21              : namespace hcomm {
      22           23 : CpuUrmaEndpoint::CpuUrmaEndpoint(const EndpointDesc& endpointDesc) : Endpoint(endpointDesc) {}
      23              : 
      24           46 : CpuUrmaEndpoint::~CpuUrmaEndpoint() noexcept
      25              : {
      26           23 :     std::lock_guard<std::mutex> lock(portMutex_);
      27           23 :     if (dynamicPort_ != HCCL_INVALID_PORT) {
      28            1 :         ServerSocketStopListenImpl(dynamicPort_);
      29              :     }
      30           23 :     dynamicPort_ = HCCL_INVALID_PORT;
      31           46 : }
      32              : 
      33           23 : HcclResult CpuUrmaEndpoint::Init()
      34              : {
      35           23 :     HCCL_INFO("[%s] localEndpoint protocol[%d]", __func__, endpointDesc_.protocol);
      36              : 
      37           23 :     Hccl::IpAddress ipAddr{};
      38           23 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
      39              :     u32 devPhyId;
      40              :     s32 deviceLogicId;
      41           23 :     HcclResult ret = hrtGetDevice(&deviceLogicId);
      42           23 :     if (ret != HCCL_SUCCESS) {
      43            0 :         HCCL_ERROR("call hrtGetDevice failed, deviceLogicId[%d]", deviceLogicId);
      44            0 :         return ret;
      45              :     }
      46           23 :     RaSocketSetWhiteListStatus(1); // PEER模式需要手动开启白名单模式
      47           23 :     Hccl::HccpPeerManager::GetInstance().Init(deviceLogicId);
      48           23 :     ret = hrtGetDevicePhyIdByIndex(static_cast<uint32_t>(deviceLogicId), devPhyId);
      49           23 :     if (ret != HCCL_SUCCESS) {
      50            0 :         HCCL_ERROR("call hrtGetDevicePhyIdByIndex failed, deviceLogicId[%d], devPhyId[%u]", deviceLogicId, devPhyId);
      51            0 :         return ret;
      52              :     }
      53           23 :     auto& rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
      54           23 :     ctxHandle_ = static_cast<void*>(
      55           23 :         rdmaHandleMgr.GetByAddr(devPhyId, Hccl::LinkProtoType::UB, ipAddr, Hccl::PortDeploymentType::HOST_NET));
      56           23 :     CHK_PTR_NULL(ctxHandle_);
      57           23 :     HCCL_INFO(
      58              :         "CpuUrmaEndpoint::%s success, devPhyId[%u], ipAddr[%s], ctxHandle[%p]", __func__, devPhyId,
      59              :         ipAddr.Describe().c_str(), ctxHandle_);
      60              : 
      61           23 :     MemMgrCacheKey key{devPhyId, COMM_PROTOCOL_UB_CTP, ipAddr, LocTypeToPortType(endpointDesc_.loc.locType)};
      62           23 :     auto createMgr = [this]() {
      63           23 :         auto mgr = std::make_shared<UbRegedMemMgr>();
      64           23 :         mgr->rdmaHandle_ = ctxHandle_;
      65           23 :         return mgr;
      66           23 :     };
      67           23 :     CHK_RET(AttachCache(key, createMgr));
      68           23 :     return HCCL_SUCCESS;
      69              : }
      70              : 
      71            1 : HcclResult CpuUrmaEndpoint::ServerSocketListen(const uint32_t port)
      72              : {
      73            1 :     Hccl::IpAddress ipAddr{};
      74            1 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
      75              : 
      76            1 :     s32 devId = 0;
      77            1 :     CHK_RET(hrtGetDevice(&devId));
      78            1 :     u32 devPhyId = 0;
      79            1 :     CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
      80              : 
      81            1 :     Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
      82            1 :     Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
      83              : 
      84            1 :     HCCL_INFO("[CpuUrmaEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devPhyId, ipAddr.Describe().c_str());
      85              : 
      86            1 :     uint32_t requestPort = port;
      87            1 :     CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
      88              :         localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
      89              : 
      90            1 :     return HCCL_SUCCESS;
      91              : }
      92              : 
      93            2 : inline HcclResult CpuUrmaEndpoint::ServerSocketStopListenImpl(const uint32_t port)
      94              : {
      95            2 :     Hccl::IpAddress ipAddr{};
      96            2 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
      97              : 
      98            2 :     s32 devId = 0;
      99            2 :     CHK_RET(hrtGetDevice(&devId));
     100            2 :     u32 devPhyId = 0;
     101            2 :     CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
     102            2 :     Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
     103            2 :     Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
     104            2 :     CHK_RET(ServerSocketManager::GetInstance().ServerSocketStopListen(localPort, Hccl::NicType::HOST_NIC_TYPE, port));
     105              : 
     106            1 :     return HCCL_SUCCESS;
     107              : }
     108              : 
     109            1 : HcclResult CpuUrmaEndpoint::ServerSocketStopListen(const uint32_t port) { return ServerSocketStopListenImpl(port); }
     110              : 
     111            1 : HcclResult CpuUrmaEndpoint::ServerSocketGetListenPort(uint32_t* port)
     112              : {
     113            1 :     std::lock_guard<std::mutex> lock(portMutex_);
     114            1 :     Hccl::IpAddress localIpAddr{};
     115            1 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, localIpAddr));
     116              : 
     117            1 :     s32 deviceId = 0;
     118            1 :     CHK_RET(hrtGetDevice(&deviceId));
     119            1 :     u32 devicePhyId = 0;
     120            1 :     CHK_RET(hrtGetDevicePhyIdByIndex(deviceId, devicePhyId));
     121              : 
     122            1 :     Hccl::DevNetPortType portType = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
     123            1 :     Hccl::PortData portData = Hccl::PortData(devicePhyId, portType, 0, localIpAddr);
     124              : 
     125            1 :     HCCL_INFO(
     126              :         "[CpuUrmaEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devicePhyId, localIpAddr.Describe().c_str());
     127              : 
     128              :     // 已有监听端口则直接返回
     129            1 :     if (dynamicPort_ != HCCL_INVALID_PORT) {
     130            0 :         *port = dynamicPort_;
     131            0 :         HCCL_INFO("[CpuUrmaEndpoint::%s] already listening, return existing port[%u]", __func__, dynamicPort_);
     132            0 :         return HCCL_SUCCESS;
     133              :     }
     134              : 
     135            1 :     uint32_t requestPort = 0;
     136            1 :     CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
     137              :         portData, Hccl::NicType::HOST_NIC_TYPE, devicePhyId, &requestPort));
     138            1 :     if (requestPort == 0 || requestPort == HCCL_INVALID_PORT) {
     139            0 :         HCCL_ERROR("[CpuUrmaEndpoint::%s] get listen port failed, port is invalid.", __func__);
     140            0 :         return HCCL_E_NETWORK;
     141              :     }
     142            1 :     dynamicPort_ = requestPort;
     143            1 :     *port = dynamicPort_;
     144              : 
     145            1 :     return HCCL_SUCCESS;
     146            1 : }
     147              : 
     148            1 : HcclResult CpuUrmaEndpoint::RegisterMemory(HcommMem mem, const char* memTag, void** memHandle)
     149              : {
     150            1 :     CHK_RET(this->regedMemMgr_->RegisterMemory(mem, memTag, memHandle));
     151            1 :     return HCCL_SUCCESS;
     152              : }
     153              : 
     154            1 : HcclResult CpuUrmaEndpoint::UnregisterMemory(void* memHandle)
     155              : {
     156            1 :     CHK_RET(this->regedMemMgr_->UnregisterMemory(memHandle));
     157            1 :     return HCCL_SUCCESS;
     158              : }
     159              : 
     160            0 : HcclResult CpuUrmaEndpoint::MemoryExport(void* memHandle, void** memDesc, uint32_t* memDescLen)
     161              : {
     162            0 :     CHK_RET(this->regedMemMgr_->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
     163            0 :     return HCCL_SUCCESS;
     164              : }
     165              : 
     166            0 : HcclResult CpuUrmaEndpoint::MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem)
     167              : {
     168            0 :     CHK_RET(this->regedMemMgr_->MemoryImport(memDesc, descLen, outMem));
     169            0 :     return HCCL_SUCCESS;
     170              : }
     171              : 
     172            0 : HcclResult CpuUrmaEndpoint::MemoryUnimport(const void* memDesc, uint32_t descLen)
     173              : {
     174            0 :     CHK_RET(this->regedMemMgr_->MemoryUnimport(memDesc, descLen));
     175            0 :     return HCCL_SUCCESS;
     176              : }
     177              : 
     178            0 : HcclResult CpuUrmaEndpoint::GetAllMemHandles(void** memHandles, uint32_t* memHandleNum)
     179              : {
     180            0 :     CHK_RET(this->regedMemMgr_->GetAllMemHandles(memHandles, memHandleNum));
     181            0 :     return HCCL_SUCCESS;
     182              : }
     183              : 
     184              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1