LCOV - code coverage report
Current view: top level - base_comm/common - orion_adpt_utils.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.2 % 153 135
Test Date: 2026-08-17 10:19:35 Functions: 100.0 % 11 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 "orion_adpt_utils.h"
      11              : 
      12              : // Orion
      13              : #include "adapter_rts_common.h"
      14              : #include "orion_adapter_hccp.h"
      15              : #include "tp_manager.h"
      16              : #include "topo_common_types.h"
      17              : #include "virtual_topo.h"
      18              : #include "hcomm_c_adpt.h"
      19              : #include "hcomm_adapter_hccp.h"
      20              : 
      21              : namespace hcomm {
      22              : 
      23            3 : const char* CommAddrTypeToStr(CommAddrType type)
      24              : {
      25            3 :     switch (type) {
      26            0 :         case COMM_ADDR_TYPE_IP_V4:
      27            0 :             return "COMM_ADDR_TYPE_IP_V4";
      28            0 :         case COMM_ADDR_TYPE_IP_V6:
      29            0 :             return "COMM_ADDR_TYPE_IP_V6";
      30            0 :         case COMM_ADDR_TYPE_EID:
      31            0 :             return "COMM_ADDR_TYPE_EID";
      32            2 :         case COMM_ADDR_TYPE_ID:
      33            2 :             return "COMM_ADDR_TYPE_ID";
      34            0 :         case COMM_ADDR_TYPE_RESERVED:
      35            0 :             return "COMM_ADDR_TYPE_RESERVED";
      36            1 :         default:
      37            1 :             return "UNKNOWN_COMM_ADDR_TYPE";
      38              :     }
      39              : }
      40              : 
      41         9095 : HcclResult CommAddrToIpAddress(const CommAddr& commAddr, Hccl::IpAddress& ipAddr)
      42              : {
      43         9095 :     if (commAddr.type != COMM_ADDR_TYPE_IP_V4 && commAddr.type != COMM_ADDR_TYPE_IP_V6
      44            4 :         && commAddr.type != COMM_ADDR_TYPE_EID) {
      45            3 :         if (commAddr.type == COMM_ADDR_TYPE_ID || commAddr.type == COMM_ADDR_TYPE_RESERVED) {
      46            2 :             HCCL_ERROR(
      47              :                 "[%s] failed, comm address type[%d][%s] is not supported.", __func__, commAddr.type,
      48              :                 CommAddrTypeToStr(commAddr.type));
      49              :         } else {
      50            1 :             HCCL_ERROR(
      51              :                 "[%s] failed, comm address type[%d][%s] is invalid.", __func__, commAddr.type,
      52              :                 CommAddrTypeToStr(commAddr.type));
      53              :         }
      54            3 :         return HCCL_E_NOT_SUPPORT;
      55              :     }
      56              : 
      57              :     Hccl::BinaryAddr binAddr;
      58         9092 :     int32_t family = AF_INET6;
      59         9092 :     if (commAddr.type == COMM_ADDR_TYPE_IP_V4) {
      60         1558 :         binAddr.addr = commAddr.addr;
      61         1558 :         int32_t family = AF_INET;
      62         1558 :         ipAddr = Hccl::IpAddress(binAddr, family);
      63         1558 :         return HCCL_SUCCESS;
      64              :     }
      65              : 
      66         7534 :     if (commAddr.type == COMM_ADDR_TYPE_EID) {
      67            1 :         Hccl::Eid inputEid;
      68            1 :         s32 sret = memcpy_s(inputEid.raw, Hccl::URMA_EID_LEN, commAddr.eid, Hccl::URMA_EID_LEN);
      69            1 :         CHK_PRT_RET(sret != EOK, HCCL_ERROR("memcpy failed, errno[%d]", sret), HCCL_E_MEMORY);
      70            1 :         ipAddr = Hccl::IpAddress(inputEid);
      71            1 :         return HCCL_SUCCESS;
      72              :     }
      73              : 
      74         7533 :     binAddr.addr6 = commAddr.addr6;
      75         7533 :     ipAddr = Hccl::IpAddress(binAddr, family);
      76         7533 :     return HCCL_SUCCESS;
      77              : }
      78              : 
      79          128 : HcclResult IpAddressToCommAddr(const Hccl::IpAddress& ipAddr, CommAddr& commAddr)
      80              : {
      81          128 :     int32_t family = ipAddr.GetFamily();
      82          128 :     const auto& binAddr = ipAddr.GetBinaryAddress();
      83              : 
      84          128 :     if (family == AF_INET) {
      85           46 :         commAddr.addr = binAddr.addr;
      86           46 :         commAddr.type = COMM_ADDR_TYPE_IP_V4;
      87           46 :         return HcclResult::HCCL_SUCCESS;
      88              :     }
      89              : 
      90           82 :     commAddr.addr6 = binAddr.addr6;
      91           82 :     commAddr.type = COMM_ADDR_TYPE_IP_V6;
      92           82 :     return HcclResult::HCCL_SUCCESS;
      93              : }
      94              : 
      95          102 : HcclResult CommProtocolToLinkProtocol(CommProtocol commProtocol, Hccl::LinkProtocol& linkProtocol)
      96              : {
      97          102 :     switch (commProtocol) {
      98           34 :         case COMM_PROTOCOL_UBC_CTP:
      99           34 :             linkProtocol = Hccl::LinkProtocol::UB_CTP;
     100           34 :             break;
     101            2 :         case COMM_PROTOCOL_UBC_TP:
     102            2 :             linkProtocol = Hccl::LinkProtocol::UB_TP;
     103            2 :             break;
     104           16 :         case COMM_PROTOCOL_ROCE:
     105           16 :             linkProtocol = Hccl::LinkProtocol::ROCE;
     106           16 :             break;
     107            4 :         case COMM_PROTOCOL_HCCS:
     108            4 :             linkProtocol = Hccl::LinkProtocol::HCCS;
     109            4 :             break;
     110           41 :         case COMM_PROTOCOL_UB_MEM:
     111           41 :             linkProtocol = Hccl::LinkProtocol::UB_MEM;
     112           41 :             break;
     113            0 :         case COMM_PROTOCOL_PCIE:
     114            0 :             linkProtocol = Hccl::LinkProtocol::PCIE;
     115            0 :             break;
     116            0 :         case COMM_PROTOCOL_UBOE:
     117            0 :             linkProtocol = Hccl::LinkProtocol::UBOE;
     118            0 :             break;
     119            4 :         case COMM_PROTOCOL_UBG:
     120            4 :             linkProtocol = Hccl::LinkProtocol::UBG;
     121            4 :             break;
     122            1 :         default:
     123            1 :             HCCL_ERROR("[%s] Invalid CommProtocol[%u]", __func__, commProtocol);
     124            1 :             return HCCL_E_PARA;
     125              :     }
     126          101 :     return HCCL_SUCCESS;
     127              : }
     128              : 
     129            7 : HcclResult CommAddrTypeToHcclAddressType(CommAddrType commAddrType, HcclAddressType& hcclAddressType)
     130              : {
     131            7 :     switch (commAddrType) {
     132            5 :         case COMM_ADDR_TYPE_IP_V4:
     133            5 :             hcclAddressType = HCCL_ADDR_TYPE_IP_V4;
     134            5 :             break;
     135            1 :         case COMM_ADDR_TYPE_IP_V6:
     136            1 :             hcclAddressType = HCCL_ADDR_TYPE_IP_V6;
     137            1 :             break;
     138            1 :         default:
     139            1 :             HCCL_ERROR("[%s] Invalid CommAddrType[%u]", __func__, commAddrType);
     140            1 :             return HCCL_E_NOT_FOUND;
     141              :     }
     142            6 :     return HCCL_SUCCESS;
     143              : }
     144              : 
     145           89 : Hccl::LinkData BuildDefaultLinkData()
     146              : {
     147           89 :     Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::HOST_NET;
     148           89 :     Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::ROCE;
     149           89 :     Hccl::IpAddress locAddr;
     150           89 :     Hccl::IpAddress rmtAddr;
     151           89 :     uint32_t locDevPhyId = 0;
     152           89 :     uint32_t rmtDevPhyId = 0;
     153           89 :     return Hccl::LinkData(portDeploymentType, linkProtocol, locDevPhyId, rmtDevPhyId, locAddr, rmtAddr);
     154              : }
     155              : 
     156              : static HcclResult
     157           84 : EndpointLocTypeToPortDeploymentType(const EndpointLocType locType, Hccl::PortDeploymentType& deployType)
     158              : {
     159           84 :     switch (locType) {
     160           17 :         case EndpointLocType::ENDPOINT_LOC_TYPE_HOST:
     161           17 :             deployType = Hccl::PortDeploymentType::HOST_NET;
     162           17 :             break;
     163           66 :         case EndpointLocType::ENDPOINT_LOC_TYPE_DEVICE:
     164           66 :             deployType = Hccl::PortDeploymentType::DEV_NET;
     165           66 :             break;
     166            1 :         default:
     167            1 :             HCCL_ERROR("[%s] unknown type of EndpointLocType[%d]", __func__, locType);
     168            1 :             return HcclResult::HCCL_E_PARA;
     169              :     }
     170              : 
     171           83 :     return HcclResult::HCCL_SUCCESS;
     172              : }
     173              : 
     174              : HcclResult
     175           43 : EndpointDescPairToLinkData(const EndpointDesc& locEp, const EndpointDesc& rmtEp, Hccl::LinkData& linkData, u32 reuseIdx)
     176              : {
     177           43 :     Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::INVALID;
     178           43 :     CHK_RET(EndpointLocTypeToPortDeploymentType(locEp.loc.locType, portDeploymentType));
     179              : 
     180           42 :     Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::INVALID;
     181           42 :     CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, linkProtocol));
     182              : 
     183           42 :     Hccl::IpAddress locAddr{};
     184           42 :     Hccl::IpAddress rmtAddr{};
     185           42 :     CHK_RET(CommAddrToIpAddress(locEp.commAddr, locAddr));
     186           42 :     CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, rmtAddr));
     187              : 
     188           42 :     uint32_t locDevPhyId = locEp.loc.device.devPhyId;
     189           42 :     uint32_t rmtDevPhyId = rmtEp.loc.device.devPhyId;
     190              : 
     191              :     // 开源开放架构下comms层级不感知通信域层级的rank信息
     192              :     // 当前复用orion数据结构故使用devId替换
     193           42 :     linkData = Hccl::LinkData(portDeploymentType, linkProtocol, locDevPhyId, rmtDevPhyId, locAddr, rmtAddr, reuseIdx);
     194              : 
     195           42 :     return HCCL_SUCCESS;
     196              : }
     197              : 
     198           41 : HcclResult EndpointDescPairToLinkDataWithRankIds(
     199              :     const uint32_t myRank, const uint32_t rmtRank, const EndpointDesc& locEp, const EndpointDesc& rmtEp,
     200              :     Hccl::LinkData& linkData, uint32_t devicePhyId, uint32_t remoteDevicePhyId, u32 reuseIdx)
     201              : {
     202           41 :     Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::INVALID;
     203           41 :     CHK_RET(EndpointLocTypeToPortDeploymentType(locEp.loc.locType, portDeploymentType));
     204              : 
     205           41 :     Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::INVALID;
     206           41 :     CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, linkProtocol));
     207              : 
     208           41 :     Hccl::IpAddress locAddr{};
     209           41 :     Hccl::IpAddress rmtAddr{};
     210           41 :     CHK_RET(CommAddrToIpAddress(locEp.commAddr, locAddr));
     211           41 :     CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, rmtAddr));
     212              : 
     213              :     // 临时方案,为支持开源开放与orion通信域混跑,复用orion数据结构,添加rank信息
     214           41 :     linkData = Hccl::LinkData(
     215           41 :         portDeploymentType, linkProtocol, myRank, rmtRank, locAddr, rmtAddr, devicePhyId, remoteDevicePhyId, reuseIdx);
     216           41 :     linkData.UpdateIpAddrWithPCIE();
     217              : 
     218           41 :     return HCCL_SUCCESS;
     219              : }
     220              : 
     221           13 : HcclResult PrepareUbConnBuildContext(
     222              :     const EndpointDesc& locEp, const EndpointDesc& rmtEp, const HcommChannelDesc& channelDesc, UbConnBuildContext& ctx)
     223              : {
     224           13 :     CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, ctx.protocol));
     225           13 :     CHK_RET(CommAddrToIpAddress(locEp.commAddr, ctx.locAddr));
     226           13 :     CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, ctx.rmtAddr));
     227           13 :     CHK_RET(hrtGetDevice(&ctx.deviceLogicId));
     228           13 :     Hccl::TpManager::GetInstance(ctx.deviceLogicId).Init();
     229           13 :     if (channelDesc.qos > 7U) {
     230            0 :         HCCL_WARNING(
     231              :             "[PrepareUbConnBuildContext] invalid channelQos[%u], expect [0, 7], use default qos[%u].", channelDesc.qos,
     232              :             Hccl::kRaUbGetTpInfoParamDefaultQos);
     233            0 :         ctx.qosPre = static_cast<u8>(Hccl::kRaUbGetTpInfoParamDefaultQos);
     234              :     } else {
     235           13 :         ctx.qosPre = static_cast<u8>(channelDesc.qos);
     236              :     }
     237           13 :     ctx.sqDepth = channelDesc.ubAttr.sqDepth;
     238           13 :     return HCCL_SUCCESS;
     239              : }
     240              : 
     241            4 : HcclResult CheckUbSqDepth(const UbConnBuildContext& ctx, const DevBaseAttr& devBaseAttr)
     242              : {
     243            4 :     if (ctx.sqDepth == UB_SQ_DEPTH_NOT_SET) {
     244            1 :         return HCCL_SUCCESS;
     245              :     }
     246            3 :     if (ctx.sqDepth < 1 || ctx.sqDepth > devBaseAttr.sqMaxDepth) {
     247            0 :         HCCL_ERROR(
     248              :             "[%s] invalid ubAttr.sqDepth[%u], aligned range is [%u, %u] (aligned to power-of-two before compared).",
     249              :             __func__, ctx.sqDepth, 1, devBaseAttr.sqMaxDepth);
     250            0 :         return HCCL_E_PARA;
     251              :     }
     252            3 :     return HCCL_SUCCESS;
     253              : }
     254              : 
     255              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1