LCOV - code coverage report
Current view: top level - legacy/ascend950/unified_platform/resource/net_dev - hccl_net_dev_v2.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 75.8 % 91 69
Test Date: 2026-07-28 12:11:00 Functions: 100.0 % 8 8

            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 "hccl_net_dev_v2.h"
      12              : #include "net_device.h"
      13              : #include "inner_net_dev_manager.h"
      14              : #include "inner_net_dev.h"
      15              : #include "log.h"
      16              : 
      17              : using namespace Hccl;
      18              : /**
      19              :  * @brief 将 HcclNetDevInfos 转换为 NetDevInfo
      20              :  * @param src 输入的 HcclNetDevInfos 结构体
      21              :  * @param[out] dst 输出的 NetDevInfo 结构体
      22              :  * @return HcclResult 转换结果(成功返回HCCL_SUCCESS,失败返回对应错误码)
      23              :  */
      24            7 : static Hccl::LinkProtoType ConvertHcclProtoToLinkProto(HcclProtoType hcclProto)
      25              : {
      26            7 :     switch (hcclProto) {
      27            2 :         case HCCL_PROTO_TYPE_BUS:
      28              :             // 设备间总线直连协议 -> UB(统一总线,涵盖华为UB系列)
      29            2 :             return Hccl::LinkProtoType::UB;
      30            1 :         case HCCL_PROTO_TYPE_TCP:
      31              :             // TCP协议 -> 直接映射
      32            1 :             return Hccl::LinkProtoType::TCP;
      33            0 :         case HCCL_PROTO_TYPE_ROCE:
      34              :             // RoCE(基于RDMA的以太网协议)-> RDMA
      35            0 :             return Hccl::LinkProtoType::RDMA;
      36            4 :         case HCCL_PROTO_TYPE_UBC_CTP:
      37              :         case HCCL_PROTO_TYPE_UBC_TP:
      38              :         case HCCL_PROTO_TYPE_UBG_TP:
      39              :             // 华为统一总线系列协议 -> UB
      40            4 :             return Hccl::LinkProtoType::UB;
      41            0 :         case HCCL_PROTO_TYPE_RESERVED:
      42            0 :             return Hccl::LinkProtoType::HCCS_PCIE;
      43            0 :         default:
      44            0 :             return Hccl::LinkProtoType::HCCS_PCIE;
      45              :     }
      46              : }
      47              : 
      48            7 : static Hccl::PortDeploymentType ConvertDeploymentType(HcclNetDevDeployment type) {
      49            7 :     if(type == HCCL_NETDEV_DEPLOYMENT_DEVICE) {
      50            3 :         return Hccl::PortDeploymentType::DEV_NET;
      51            4 :     } else if(type == HCCL_NETDEV_DEPLOYMENT_HOST) {
      52            3 :         return Hccl::PortDeploymentType::HOST_NET;
      53              :     } else {
      54            1 :         return Hccl::PortDeploymentType::P2P;        
      55              :     }
      56              : }
      57              : 
      58            7 : static HcclResult ConvertToNetDevInfo(const HcclNetDevInfos &src, Hccl::NetDevInfo &dst)
      59              : {
      60            7 :     dst.devId = src.devicePhyId;
      61            7 :     dst.protoType = ConvertHcclProtoToLinkProto(src.addr.protoType);
      62            7 :     dst.type = ConvertDeploymentType(src.netdevDeployment);
      63            7 :     if(dst.type == Hccl::PortDeploymentType::P2P) {
      64            3 :         HCCL_ERROR("Invalid deployment type (devPhyId: %d)", src.devicePhyId);
      65            1 :         return HCCL_E_PARA;
      66              :     }
      67              :     try {
      68            6 :         if (src.addr.type == HCCL_ADDR_TYPE_IP_V4) {
      69              :             // 转换IPv4地址:从 struct in_addr 到 IpAddress
      70            4 :             dst.addr = Hccl::IpAddress(src.addr.addr.s_addr);
      71            2 :         } else if (src.addr.type == HCCL_ADDR_TYPE_IP_V6) {
      72              :             // IPv6地址转换:先将in6_addr转换为字符串,再构造IpAddress
      73              :             char        ipv6Str[INET6_ADDRSTRLEN];
      74            1 :             const char *result = inet_ntop(AF_INET6, &src.addr.addr6, ipv6Str, INET6_ADDRSTRLEN);
      75            1 :             if (result == nullptr) {
      76            0 :                 HCCL_ERROR("Invalid result (devPhyId: %d)", src.devicePhyId);
      77            0 :                 return HCCL_E_PARA;
      78              :             }
      79            2 :             dst.addr = Hccl::IpAddress(ipv6Str, AF_INET6);
      80              :         } else {
      81            3 :             HCCL_ERROR("Invalid address type(devPhyId: %d)", src.devicePhyId);
      82            1 :             return HCCL_E_PARA;
      83              :         }
      84            0 :     } catch (const std::exception &e) {
      85              :         // 捕获IpAddress构造中的异常(如无效地址、不支持的协议族)
      86            0 :         HCCL_ERROR("Failed to convert (devPhyId: %d)", src.devicePhyId);
      87            0 :         return HCCL_E_INTERNAL;
      88            0 :     }
      89              : 
      90            5 :     return HCCL_SUCCESS;
      91              : }
      92              : 
      93            7 : HcclResult HcclNetDevOpenV2(const HcclNetDevInfos *info, HcclNetDev *netDev)
      94              : {
      95            7 :     CHK_PTR_NULL(info);  
      96            7 :     CHK_PTR_NULL(netDev);
      97              : 
      98            7 :     Hccl::NetDevInfo pltInfo;
      99            7 :     HcclResult ret = ConvertToNetDevInfo(*info, pltInfo);
     100            7 :     if (ret != HCCL_SUCCESS) {
     101            6 :         HCCL_ERROR("ConvertToNetDevInfo fail, devPhyId[%d]", info->devicePhyId);
     102            2 :         return ret;
     103              :     }
     104              : 
     105            5 :     Hccl::InnerNetDevManager *netDevMgr = &Hccl::InnerNetDevManager::GetInstance();
     106            5 :     if (netDevMgr == nullptr) {
     107            0 :         HCCL_ERROR("InnerNetDevManager::GetInstance() fail, devPhyId[%d]", info->devicePhyId);
     108            0 :         return HCCL_E_PTR;
     109              :     }
     110              : 
     111            5 :     HcclNetDevice *hcclNetDev = nullptr;
     112            5 :     ret                          = netDevMgr->AddDevice(pltInfo, hcclNetDev);
     113            5 :     if (ret != HCCL_SUCCESS) {
     114            0 :         HCCL_ERROR("AddDevice fail, devPhyId[%d]",  info->devicePhyId);
     115            0 :         return ret;
     116              :     }
     117            5 :     *netDev = static_cast<HcclNetDev>(hcclNetDev);
     118           15 :     HCCL_INFO("HcclNetDevOpenV2: successfully opened netDev[%p]!", *netDev);
     119            5 :     return HCCL_SUCCESS;
     120              : }
     121              : 
     122            5 : HcclResult HcclNetDevCloseV2(HcclNetDev netDev)
     123              : {
     124           15 :     HCCL_INFO("[HcclNetDevCloseV2] netDev[%p].", netDev);
     125            5 :     CHK_PTR_NULL(netDev);
     126            5 :     Hccl::InnerNetDevManager *netDevMgr = &Hccl::InnerNetDevManager::GetInstance();
     127            5 :     CHK_PTR_NULL(netDevMgr);
     128            5 :     return netDevMgr->DeleteDevice(static_cast<HcclNetDevice*>(netDev));
     129              : }
     130              : 
     131            1 : HcclResult HcclNetDevGetAddrV2(const HcclNetDev netDev, HcclAddress *addr)
     132              : {
     133            1 :     CHK_PTR_NULL(netDev);
     134            1 :     CHK_PTR_NULL(addr);
     135              : 
     136            1 :     auto ipAddr = static_cast<HcclNetDevice*>(netDev)->GetNetDevInfo().addr;    
     137              :     CHK_PTR_NULL(&ipAddr);
     138            1 :     if (ipAddr.GetFamily() == AF_INET) {
     139            0 :         addr->type = HCCL_ADDR_TYPE_IP_V4;
     140            0 :         addr->addr = ipAddr.GetBinaryAddress().addr;
     141            0 :         unsigned char *ipv4Addr = reinterpret_cast<unsigned char*>(&addr->addr);
     142            0 :         HCCL_DEBUG("IPv4 Address: %u.%u.%u.%u", ipv4Addr[0], ipv4Addr[1], ipv4Addr[2], ipv4Addr[3]);
     143            1 :     } else if (ipAddr.GetFamily() == AF_INET6) {
     144            1 :         addr->type  = HCCL_ADDR_TYPE_IP_V6;
     145            1 :         addr->addr6 = ipAddr.GetBinaryAddress().addr6;
     146            1 :         unsigned short *ipv6Addr = reinterpret_cast<unsigned short*>(&addr->addr6);
     147            3 :         HCCL_DEBUG("IPv6 Address: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
     148              :                     ipv6Addr[0], ipv6Addr[1], ipv6Addr[2], ipv6Addr[3],
     149              :                     ipv6Addr[4], ipv6Addr[5], ipv6Addr[6], ipv6Addr[7]);
     150              :     } else {        
     151            0 :         HCCL_ERROR("HcclNetDevGetAddrV2 fail, devPhyId[%u]",
     152              :                            static_cast<HcclNetDevice *>(netDev)->GetNetDevInfo().devId);
     153            0 :         return HCCL_E_PARA;
     154              :     }
     155            1 :     return HCCL_SUCCESS;
     156              : }
     157              : 
     158            1 : HcclResult HcclNetDevGetBusAddrV2(HcclDeviceId dstDevId, HcclAddress *busAddr)
     159              : {
     160              :     (void)dstDevId;
     161              :     (void)busAddr;
     162            3 :     HCCL_ERROR("HcclNetDevGetBusAddrV2: failed to get bus address for device[%d], busAddr[%p].", dstDevId, busAddr);
     163            1 :     return HCCL_E_NOT_SUPPORT;
     164              : }
     165              : 
     166            1 : HcclResult HcclNetDevGetNicAddrV2(int32_t devicePhyId, HcclAddress **addr, uint32_t *addrNum)
     167              : {
     168              :     (void)devicePhyId;
     169              :     (void)addr;
     170              :     (void)addrNum;
     171            3 :     HCCL_ERROR("HcclNetDevGetNicAddrV2: failed to get NIC address for devicePhyId[%d], addr[%p], addrNum[%p].",
     172              :                 devicePhyId, addr, addrNum);
     173            1 :     return HCCL_E_NOT_SUPPORT;
     174              : }
        

Generated by: LCOV version 2.0-1