LCOV - code coverage report
Current view: top level - coll_communicator_mgr/common/loggers - comm_addr_logger.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.5 % 61 54
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 7 7

            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 "comm_addr_logger.h"
      12              : #include "hccl_comm_pub.h"
      13              : #include <arpa/inet.h>
      14              : #include <array>
      15              : #include <securec.h>
      16              : 
      17              : namespace hcomm {
      18              : namespace logger {
      19              : 
      20            5 :     std::string CommAddrLogger::GetTypeString(CommAddrType type)
      21              :     {
      22            5 :         switch (type) {
      23            1 :             case COMM_ADDR_TYPE_IP_V4:
      24            2 :                 return "IPv4";
      25            1 :             case COMM_ADDR_TYPE_IP_V6:
      26            2 :                 return "IPv6";
      27            1 :             case COMM_ADDR_TYPE_ID:
      28            2 :                 return "ID";
      29            1 :             case COMM_ADDR_TYPE_EID:
      30            2 :                 return "EID";
      31            1 :             default:
      32            1 :                 return "Unknown(" + std::to_string(type) + ")";
      33              :         }
      34              :     }
      35              : 
      36           54 :     std::string CommAddrLogger::ConvertIPv4(const struct in_addr& addr)
      37              :     {
      38              :         std::array<char, INET_ADDRSTRLEN> buffer;
      39          108 :         const char* result = inet_ntop(AF_INET, &addr, buffer.data(), buffer.size());
      40           54 :         if (result == nullptr) {
      41            0 :             HCCL_ERROR("[%s] inet_ntop failed for IPv4 address", __func__);
      42            0 :             return "conversion failed";
      43              :         }
      44          108 :         return std::string(result);
      45              :     }
      46              : 
      47            2 :     std::string CommAddrLogger::ConvertIPv6(const struct in6_addr& addr6)
      48              :     {
      49              :         std::array<char, INET6_ADDRSTRLEN> buffer;
      50            4 :         const char* result = inet_ntop(AF_INET6, &addr6, buffer.data(), buffer.size());
      51            2 :         if (result == nullptr) {
      52            0 :             HCCL_ERROR("[%s] inet_ntop failed for IPv6 address", __func__);
      53            0 :             return "conversion failed";
      54              :         }
      55            4 :         return std::string(result);
      56              :     }
      57              : 
      58            2 :     std::string CommAddrLogger::ConvertID(uint32_t id) { return "id:0x" + std::to_string(id); }
      59              : 
      60           56 :     std::string CommAddrLogger::ConvertEID(const uint8_t eid[16])
      61              :     {
      62              :         // 模仿 Eid::Describe 的格式
      63              :         // 输出:eid[xxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxx]
      64              :         // 其中前 8 字节是 subnetPrefix,后 8 字节是 interfaceId(网络字节序)
      65              : 
      66              :         // 从字节数组中提取两个 64 位整数(网络字节序)
      67           56 :         uint64_t subnetPrefix = 0;
      68           56 :         uint64_t interfaceId = 0;
      69              : 
      70              :         // 使用 memcpy 避免对齐问题
      71           56 :         (void)memcpy_s(&subnetPrefix, sizeof(subnetPrefix), eid, sizeof(subnetPrefix));
      72           56 :         (void)memcpy_s(&interfaceId, sizeof(interfaceId), eid + 8, sizeof(interfaceId));
      73              : 
      74              :         // 转换字节序(网络字节序 -> 主机字节序)
      75           56 :         subnetPrefix = be64toh(subnetPrefix);
      76           56 :         interfaceId = be64toh(interfaceId);
      77              : 
      78              :         char buffer[64];
      79           56 :         int32_t ret = snprintf_s(
      80              :             buffer, sizeof(buffer), sizeof(buffer) - 1, "eid[%016llx:%016llx]",
      81              :             static_cast<unsigned long long>(subnetPrefix), static_cast<unsigned long long>(interfaceId));
      82           56 :         if (ret < 0) {
      83            0 :             HCCL_ERROR("[%s] snprintf_s failed for EID", __func__);
      84            0 :             return "conversion failed";
      85              :         }
      86          112 :         return std::string(buffer);
      87              :     }
      88              : 
      89           57 :     std::string CommAddrLogger::ToString(const CommAddr& commAddr)
      90              :     {
      91              :         // 模仿 IpAddress::Describe 的格式
      92              :         // 输出:IpAddress[eid[xxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxx], AF=v4/v6, addr=xxx.xxx.xxx.xxx, scopeId=0x0]
      93           57 :         std::string desc = "IpAddress[";
      94              : 
      95           57 :         switch (commAddr.type) {
      96           53 :             case COMM_ADDR_TYPE_IP_V4:
      97           53 :                 desc += ConvertEID(commAddr.eid) + ", AF=v4, addr=" + ConvertIPv4(commAddr.addr) + "]";
      98           53 :                 break;
      99            1 :             case COMM_ADDR_TYPE_IP_V6:
     100            1 :                 desc += ConvertEID(commAddr.eid) + ", AF=v6, addr=" + ConvertIPv6(commAddr.addr6) + ", scopeId=0x0]";
     101            1 :                 break;
     102            1 :             case COMM_ADDR_TYPE_ID:
     103            1 :                 desc += "id:" + ConvertID(commAddr.id) + "]";
     104            1 :                 break;
     105            1 :             case COMM_ADDR_TYPE_EID:
     106            1 :                 desc += ConvertEID(commAddr.eid) + "]";
     107            1 :                 break;
     108            1 :             default:
     109            1 :                 desc += "Unknown]";
     110            1 :                 break;
     111              :         }
     112              : 
     113           57 :         return desc;
     114            0 :     }
     115              : 
     116           12 :     void CommAddrLogger::Print(uint32_t idx, const char* endpointName, const CommAddr& commAddr)
     117              :     {
     118           12 :         std::string addrStr = ToString(commAddr);
     119           12 :         HCCL_INFO(
     120              :             "[%s] channelDescs[%u] %s commAddr: type[%d], addr[%s]", __func__, idx, endpointName, commAddr.type,
     121              :             addrStr.c_str());
     122           12 :     }
     123              : 
     124              : } // namespace logger
     125              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1