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-08-18 17:47:01 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_UB_CTP:
      37              :         case HCCL_PROTO_TYPE_UBC_TP:
      38              :         case HCCL_PROTO_TYPE_UB_RTP:
      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              : {
      50            7 :     if (type == HCCL_NETDEV_DEPLOYMENT_DEVICE) {
      51            3 :         return Hccl::PortDeploymentType::DEV_NET;
      52            4 :     } else if (type == HCCL_NETDEV_DEPLOYMENT_HOST) {
      53            3 :         return Hccl::PortDeploymentType::HOST_NET;
      54              :     } else {
      55            1 :         return Hccl::PortDeploymentType::P2P;
      56              :     }
      57              : }
      58              : 
      59            7 : static HcclResult ConvertToNetDevInfo(const HcclNetDevInfos& src, Hccl::NetDevInfo& dst)
      60              : {
      61            7 :     dst.devId = src.devicePhyId;
      62            7 :     dst.protoType = ConvertHcclProtoToLinkProto(src.addr.protoType);
      63            7 :     dst.type = ConvertDeploymentType(src.netdevDeployment);
      64            7 :     if (dst.type == Hccl::PortDeploymentType::P2P) {
      65            3 :         HCCL_ERROR("Invalid deployment type (devPhyId: %d)", src.devicePhyId);
      66            1 :         return HCCL_E_PARA;
      67              :     }
      68              :     try {
      69            6 :         if (src.addr.type == HCCL_ADDR_TYPE_IP_V4) {
      70              :             // 转换IPv4地址:从 struct in_addr 到 IpAddress
      71            4 :             dst.addr = Hccl::IpAddress(src.addr.addr.s_addr);
      72            2 :         } else if (src.addr.type == HCCL_ADDR_TYPE_IP_V6) {
      73              :             // IPv6地址转换:先将in6_addr转换为字符串,再构造IpAddress
      74              :             char ipv6Str[INET6_ADDRSTRLEN];
      75            1 :             const char* result = inet_ntop(AF_INET6, &src.addr.addr6, ipv6Str, INET6_ADDRSTRLEN);
      76            1 :             if (result == nullptr) {
      77            0 :                 HCCL_ERROR("Invalid result (devPhyId: %d)", src.devicePhyId);
      78            0 :                 return HCCL_E_PARA;
      79              :             }
      80            2 :             dst.addr = Hccl::IpAddress(ipv6Str, AF_INET6);
      81              :         } else {
      82            3 :             HCCL_ERROR("Invalid address type(devPhyId: %d)", src.devicePhyId);
      83            1 :             return HCCL_E_PARA;
      84              :         }
      85            0 :     } catch (const std::exception& e) {
      86              :         // 捕获IpAddress构造中的异常(如无效地址、不支持的协议族)
      87            0 :         HCCL_ERROR("Failed to convert (devPhyId: %d)", src.devicePhyId);
      88            0 :         return HCCL_E_INTERNAL;
      89            0 :     }
      90              : 
      91            5 :     return HCCL_SUCCESS;
      92              : }
      93              : 
      94            7 : HcclResult HcclNetDevOpenV2(const HcclNetDevInfos* info, HcclNetDev* netDev)
      95              : {
      96            7 :     CHK_PTR_NULL(info);
      97            7 :     CHK_PTR_NULL(netDev);
      98              : 
      99            7 :     Hccl::NetDevInfo pltInfo;
     100            7 :     HcclResult ret = ConvertToNetDevInfo(*info, pltInfo);
     101            7 :     if (ret != HCCL_SUCCESS) {
     102            6 :         HCCL_ERROR("ConvertToNetDevInfo fail, devPhyId[%d]", info->devicePhyId);
     103            2 :         return ret;
     104              :     }
     105              : 
     106            5 :     Hccl::InnerNetDevManager* netDevMgr = &Hccl::InnerNetDevManager::GetInstance();
     107            5 :     if (netDevMgr == nullptr) {
     108            0 :         HCCL_ERROR("InnerNetDevManager::GetInstance() fail, devPhyId[%d]", info->devicePhyId);
     109            0 :         return HCCL_E_PTR;
     110              :     }
     111              : 
     112            5 :     HcclNetDevice* hcclNetDev = nullptr;
     113            5 :     ret = netDevMgr->AddDevice(pltInfo, hcclNetDev);
     114            5 :     if (ret != HCCL_SUCCESS) {
     115            0 :         HCCL_ERROR("AddDevice fail, devPhyId[%d]", info->devicePhyId);
     116            0 :         return ret;
     117              :     }
     118            5 :     *netDev = static_cast<HcclNetDev>(hcclNetDev);
     119           15 :     HCCL_INFO("HcclNetDevOpenV2: successfully opened netDev[%p]!", *netDev);
     120            5 :     return HCCL_SUCCESS;
     121              : }
     122              : 
     123            5 : HcclResult HcclNetDevCloseV2(HcclNetDev netDev)
     124              : {
     125           15 :     HCCL_INFO("[HcclNetDevCloseV2] netDev[%p].", netDev);
     126            5 :     CHK_PTR_NULL(netDev);
     127            5 :     Hccl::InnerNetDevManager* netDevMgr = &Hccl::InnerNetDevManager::GetInstance();
     128            5 :     CHK_PTR_NULL(netDevMgr);
     129            5 :     return netDevMgr->DeleteDevice(static_cast<HcclNetDevice*>(netDev));
     130              : }
     131              : 
     132            1 : HcclResult HcclNetDevGetAddrV2(const HcclNetDev netDev, HcclAddress* addr)
     133              : {
     134            1 :     CHK_PTR_NULL(netDev);
     135            1 :     CHK_PTR_NULL(addr);
     136              : 
     137            1 :     auto ipAddr = static_cast<HcclNetDevice*>(netDev)->GetNetDevInfo().addr;
     138              :     CHK_PTR_NULL(&ipAddr);
     139            1 :     if (ipAddr.GetFamily() == AF_INET) {
     140            0 :         addr->type = HCCL_ADDR_TYPE_IP_V4;
     141            0 :         addr->addr = ipAddr.GetBinaryAddress().addr;
     142            0 :         unsigned char* ipv4Addr = reinterpret_cast<unsigned char*>(&addr->addr);
     143            0 :         HCCL_DEBUG("IPv4 Address: %u.%u.%u.%u", ipv4Addr[0], ipv4Addr[1], ipv4Addr[2], ipv4Addr[3]);
     144            1 :     } else if (ipAddr.GetFamily() == AF_INET6) {
     145            1 :         addr->type = HCCL_ADDR_TYPE_IP_V6;
     146            1 :         addr->addr6 = ipAddr.GetBinaryAddress().addr6;
     147            1 :         unsigned short* ipv6Addr = reinterpret_cast<unsigned short*>(&addr->addr6);
     148            3 :         HCCL_DEBUG(
     149              :             "IPv6 Address: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x", ipv6Addr[0], ipv6Addr[1], ipv6Addr[2], ipv6Addr[3],
     150              :             ipv6Addr[4], ipv6Addr[5], ipv6Addr[6], ipv6Addr[7]);
     151              :     } else {
     152            0 :         HCCL_ERROR(
     153              :             "HcclNetDevGetAddrV2 fail, devPhyId[%u]", static_cast<HcclNetDevice*>(netDev)->GetNetDevInfo().devId);
     154            0 :         return HCCL_E_PARA;
     155              :     }
     156            1 :     return HCCL_SUCCESS;
     157              : }
     158              : 
     159            1 : HcclResult HcclNetDevGetBusAddrV2(HcclDeviceId dstDevId, HcclAddress* busAddr)
     160              : {
     161              :     (void)dstDevId;
     162              :     (void)busAddr;
     163            3 :     HCCL_ERROR("HcclNetDevGetBusAddrV2: failed to get bus address for device[%d], busAddr[%p].", dstDevId, busAddr);
     164            1 :     return HCCL_E_NOT_SUPPORT;
     165              : }
     166              : 
     167            1 : HcclResult HcclNetDevGetNicAddrV2(int32_t devicePhyId, HcclAddress** addr, uint32_t* addrNum)
     168              : {
     169              :     (void)devicePhyId;
     170              :     (void)addr;
     171              :     (void)addrNum;
     172            3 :     HCCL_ERROR(
     173              :         "HcclNetDevGetNicAddrV2: failed to get NIC address for devicePhyId[%d], addr[%p], addrNum[%p].", devicePhyId,
     174              :         addr, addrNum);
     175            1 :     return HCCL_E_NOT_SUPPORT;
     176              : }
        

Generated by: LCOV version 2.0-1