LCOV - code coverage report
Current view: top level - base_comm/resources/endpoints - cpu_roce_endpoint.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 61.8 % 110 68
Test Date: 2026-08-04 10:52:23 Functions: 64.7 % 17 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              : #include "endpoint_mgr.h"
      11              : #include "hccl_mem_defs.h"
      12              : #include "cpu_roce_endpoint.h"
      13              : #include "hccl/hccl_res.h"
      14              : #include "log.h"
      15              : #include "roce_mem.h"
      16              : #include "proc_reged_mem_mgr_cache.h"
      17              : #include "host_socket_handle_manager.h"
      18              : #include "adapter_rts_common.h"
      19              : #include "hccp_peer_manager.h"
      20              : #include "server_socket_manager.h"
      21              : #include "hccp.h"
      22              : 
      23              : using Hccl::HcclException;
      24              : using std::string;
      25              : using std::exception;
      26              :  
      27              : namespace hcomm {
      28          116 : CpuRoceEndpoint::CpuRoceEndpoint(const EndpointDesc &endpointDesc)
      29          116 :     : Endpoint(endpointDesc)
      30              : {
      31          116 : }
      32              : 
      33          230 : CpuRoceEndpoint::~CpuRoceEndpoint() noexcept
      34              : {
      35          116 :     std::lock_guard<std::mutex> lock(portMutex_);
      36          116 :     if (dynamicPort_ != HCCL_INVALID_PORT) {
      37            0 :         ServerSocketStopListenImpl(dynamicPort_);
      38              :     }
      39          116 :     dynamicPort_ = HCCL_INVALID_PORT;
      40          116 :     ProcRegedMemMgrCache::GetInstance().Release(cacheKey_);
      41          230 : }
      42              : 
      43          116 : HcclResult CpuRoceEndpoint::Init()
      44              : {
      45          116 :     HCCL_INFO("[%s] localEndpoint protocol[%d]", __func__, endpointDesc_.protocol);
      46              : 
      47          116 :     if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_HOST) {
      48            0 :         HCCL_INFO("[CpuRoceEndpoint][%s] CpuRoceEndpoint not support device", __func__);
      49            0 :         return HCCL_E_NOT_SUPPORT;
      50              :     }
      51          116 :     Hccl::IpAddress ipAddr{};
      52          116 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
      53          116 :     s32 devId = 0;
      54          116 :     CHK_RET(hrtGetDevice(&devId));
      55          116 :     EXCEPTION_CATCH(Hccl::HccpPeerManager::GetInstance().Init(devId), return HCCL_E_INTERNAL);
      56          116 :     u32 devPhyId = 0;
      57          116 :     CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
      58          116 :     auto &rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
      59          116 :     TRY_CATCH_RETURN(ctxHandle_ = static_cast<void *>(
      60              :         rdmaHandleMgr.GetByAddr(devPhyId, Hccl::LinkProtoType::RDMA, ipAddr, Hccl::PortDeploymentType::HOST_NET)));
      61          116 :     CHK_PTR_NULL(ctxHandle_);
      62          115 :     HCCL_INFO("CpuRoceEndpoint::%s success, devPhyId[%u], ipAddr[%s], ctxHandle[%p]",
      63              :         __func__,
      64              :         devPhyId,
      65              :         ipAddr.Describe().c_str(),
      66              :         ctxHandle_);
      67              : 
      68          115 :     cacheKey_ = MemMgrCacheKey{devPhyId, COMM_PROTOCOL_ROCE, ipAddr, LocTypeToPortType(endpointDesc_.loc.locType)};
      69          115 :     auto &cache = ProcRegedMemMgrCache::GetInstance();
      70          118 :     EXCEPTION_CATCH(regedMemMgr_ = cache.GetOrCreate(cacheKey_, [this]() {
      71              :         auto m = std::make_shared<RoceRegedMemMgr>();
      72              :         m->rdmaHandle_ = this->ctxHandle_;
      73              :         return m;
      74              :     }), return HCCL_E_PARA);
      75          115 :     return HCCL_SUCCESS;
      76              : }
      77              : 
      78            4 : HcclResult CpuRoceEndpoint::ServerSocketListen(const uint32_t port)
      79              : {
      80            4 :     Hccl::IpAddress ipAddr{};
      81            4 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
      82              : 
      83            4 :     s32 devId = 0;
      84            4 :     CHK_RET(hrtGetDevice(&devId));
      85            4 :     u32 devPhyId = 0;
      86            4 :     CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
      87              : 
      88            4 :     Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
      89            4 :     Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
      90              : 
      91            4 :     HCCL_INFO("[CpuRoceEndpoint::%s] devicePhyId[%u] ipAddress[%s]",
      92              :         __func__, devPhyId, ipAddr.Describe().c_str());
      93              : 
      94            4 :     uint32_t requestPort = port;
      95            4 :     CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
      96            4 :     return HCCL_SUCCESS;
      97              : }
      98              : 
      99            2 : inline HcclResult CpuRoceEndpoint::ServerSocketStopListenImpl(const uint32_t port)
     100              : {
     101            2 :     Hccl::IpAddress ipAddr{};
     102            2 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
     103              : 
     104            2 :     s32 devId = 0;
     105            2 :     CHK_RET(hrtGetDevice(&devId));
     106            2 :     u32 devPhyId = 0;
     107            2 :     CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
     108              : 
     109            2 :     Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
     110            2 :     Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
     111            2 :     CHK_RET(ServerSocketManager::GetInstance().ServerSocketStopListen(localPort, Hccl::NicType::HOST_NIC_TYPE, port));
     112              : 
     113            2 :     return HCCL_SUCCESS;
     114              : }
     115              :          
     116            2 : HcclResult CpuRoceEndpoint::ServerSocketStopListen(const uint32_t port)
     117              : {
     118            2 :     return ServerSocketStopListenImpl(port);
     119              : }
     120              : 
     121            0 : HcclResult CpuRoceEndpoint::ServerSocketGetListenPort(uint32_t *port)
     122              : {
     123            0 :     std::lock_guard<std::mutex> lock(portMutex_);
     124            0 :     CHK_PTR_NULL(port);
     125            0 :     s32 devId = 0;
     126            0 :     CHK_RET(hrtGetDevice(&devId));
     127            0 :     u32 devPhyId = 0;
     128            0 :     CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
     129              : 
     130            0 :     Hccl::IpAddress ipAddr{};
     131            0 :     CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
     132              : 
     133            0 :     Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
     134            0 :     Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
     135              : 
     136            0 :     HCCL_INFO("[CpuRoceEndpoint::%s] devicePhyId[%u] ipAddress[%s]",
     137              :         __func__, devPhyId, ipAddr.Describe().c_str());
     138              : 
     139              :     // 已有监听端口则直接返回
     140            0 :     if (dynamicPort_ != HCCL_INVALID_PORT) {
     141            0 :         *port = dynamicPort_;
     142            0 :         HCCL_INFO("[CpuRoceEndpoint::%s] already listening, return existing port[%u]", __func__, dynamicPort_);
     143            0 :         return HCCL_SUCCESS; 
     144              :     }
     145            0 :     uint32_t requestPort = 0;
     146            0 :     CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
     147            0 :     if (requestPort == 0 || requestPort == HCCL_INVALID_PORT) {
     148            0 :         HCCL_ERROR("[CpuRoceEndpoint::%s] get listen port failed, port is invalid", __func__);
     149            0 :         return HCCL_E_NETWORK;
     150              :     }
     151            0 :     dynamicPort_ = requestPort;
     152            0 :     *port = dynamicPort_;
     153            0 :     return HCCL_SUCCESS;
     154            0 : }
     155              : 
     156            1 : HcclResult CpuRoceEndpoint::RegisterMemory(HcommMem mem, const char *memTag, void **memHandle)
     157              : {
     158            1 :     CHK_RET(this->regedMemMgr_->RegisterMemory(mem, memTag, memHandle));
     159            0 :     return HCCL_SUCCESS;
     160              : }
     161              : 
     162            2 : HcclResult CpuRoceEndpoint::UnregisterMemory(void* memHandle)
     163              : {
     164            2 :     CHK_RET(this->regedMemMgr_->UnregisterMemory(memHandle));
     165            0 :     return HCCL_SUCCESS;
     166              : }
     167              : 
     168            0 : HcclResult CpuRoceEndpoint::MemoryExport(void *memHandle, void **memDesc, uint32_t *memDescLen)
     169              : {
     170            0 :     CHK_RET(this->regedMemMgr_->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
     171            0 :     return HCCL_SUCCESS;
     172              : }
     173              : 
     174            0 : HcclResult CpuRoceEndpoint::MemoryImport(const void *memDesc, uint32_t descLen, HcommMem *outMem)
     175              : {
     176            0 :     CHK_RET(this->regedMemMgr_->MemoryImport(memDesc, descLen, outMem));
     177            0 :     return HCCL_SUCCESS;
     178              : }
     179              : 
     180            0 : HcclResult CpuRoceEndpoint::MemoryUnimport(const void *memDesc, uint32_t descLen)
     181              : {
     182            0 :     CHK_RET(this->regedMemMgr_->MemoryUnimport(memDesc, descLen));
     183            0 :     return HCCL_SUCCESS;
     184              : }
     185              : 
     186            0 : HcclResult CpuRoceEndpoint::GetAllMemHandles(void **memHandles, uint32_t *memHandleNum)
     187              : {
     188            0 :     CHK_RET(this->regedMemMgr_->GetAllMemHandles(memHandles, memHandleNum));
     189            0 :     return HCCL_SUCCESS;
     190              : }
     191              : 
     192           65 : HcclResult CpuRoceEndpoint::GetCapabilities(Capabilities &caps)
     193              : {
     194           65 :     HCCL_INFO("[CpuRoceEndpoint::%s] START.", __func__);
     195              :     static constexpr uint64_t RDMA_MAX_WR_LENGTH = 1ULL * 1024 * 1024 * 1024; // 单次RDMA操作最大长度1GB
     196           65 :     if (!isCapabilitiesAvailable_) {
     197              :         // 待 HCCP 提供查询设备支持的最大发送消息的接口后,查询设备实际值。
     198           64 :         capabilities_.maxMsgSize = RDMA_MAX_WR_LENGTH;
     199           64 :         uint32_t ret = RaGetLbMax(this->regedMemMgr_->rdmaHandle_, &(capabilities_.lbMax));
     200           64 :         HCCL_DEBUG("[CpuRoceEndpoint::GetCapabilities] lbMax = %d.", capabilities_.lbMax);
     201           64 :         CHK_PRT_RET(ret != 0,
     202              :             HCCL_ERROR("[CpuRoceEndpoint::GetCapabilities][GetLbMax]errNo[0x%016llx] RaGetLbMax fail. "
     203              :             "return[%u], params: rdmaHandle[%p], lbMax[%d]",
     204              :             HCCL_ERROR_CODE(HCCL_E_NETWORK), ret, this->regedMemMgr_->rdmaHandle_, capabilities_.lbMax),
     205              :             HCCL_E_NETWORK);
     206           64 :         isCapabilitiesAvailable_ = true;
     207              :     }
     208           65 :     caps = capabilities_;
     209           65 :     HCCL_INFO("[CpuRoceEndpoint::%s] SUCCESS.", __func__);
     210           65 :     return HCCL_SUCCESS;
     211              : }
     212              : }
        

Generated by: LCOV version 2.0-1