LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/topo/topo_addr_info/src - topo_addr_info.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 60.6 % 66 40
Test Date: 2026-07-28 12:11:00 Functions: 100.0 % 5 5

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2026 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 "topo_addr_info.h"
      12              : #include <stdint.h>
      13              : #include <stdlib.h>
      14              : #include <syslog.h>
      15              : #include <sys/stat.h>
      16              : #include "hal.h"
      17              : #include "topo.h"
      18              : #include "topo_addr_info_perf.h"
      19              : #include "product_card.h"
      20              : #include "product_server.h"
      21              : #include "product_pod.h"
      22              : 
      23              : #define MAX_DUMP_FILE_LEN (256)
      24              : #define DEFAULT_RANKINFO_FILE_PATH "/etc/hccl_rootinfo.json"
      25              : #define DEFAULT_RANKINFO_SIZE (4096)
      26              : 
      27              : typedef int (*GetSizeFuncType)(size_t* size);
      28              : 
      29              : typedef int (*GetRootinfoFuncType)(int npu_id, unsigned int mainboard_id, void *buf, size_t* len);
      30              : 
      31              : typedef struct {
      32              :     uint32_t mainboard_id;
      33              :     GetSizeFuncType get_size_func;
      34              : } GetSizeFuncTable;
      35              : 
      36              : typedef struct {
      37              :    uint32_t mainboard_id;
      38              :    GetRootinfoFuncType get_rootinfo_func;
      39              : } GetRootinfoFuncTable;
      40              : 
      41              : static GetSizeFuncTable g_get_size_func_table[] = {
      42              :     {MAIN_BOARD_ID_CARD_NOMESH, GetCardRankInfoLen},
      43              :     {MAIN_BOARD_ID_CARD_2PMESH, GetCardRankInfoLen},
      44              :     {MAIN_BOARD_ID_CARD_4PMESH, GetCardRankInfoLen},
      45              :     {MAIN_BOARD_ID_SERVER_8PMESH, ServerGetRootinfoLen},
      46              :     {MAIN_BOARD_ID_SERVER_TYPE1, ServerGetRootinfoLen},
      47              :     {MAIN_BOARD_ID_SERVER_8PMESH_UBOE, ServerGetRootinfoLen},
      48              :     {MAIN_BOARD_ID_SERVER_8PMESH_NOSP, ServerGetRootinfoLen},
      49              :     {MAIN_BOARD_ID_SERVER_8PMESH_NOSP_UBOE, ServerGetRootinfoLen},
      50              :     {MAIN_BOARD_ID_SERVER_UBX, ServerGetRootinfoLen},
      51              :     {MAIN_BOARD_ID_POD, PodGetRootinfoLen},
      52              :     {MAIN_BOARD_ID_POD_2D, PodGetRootinfoLen},
      53              : };
      54              : 
      55              : static GetRootinfoFuncTable g_get_rootinfo_func_table[] = {
      56              :     {MAIN_BOARD_ID_CARD_NOMESH, GetCardRankInfo},
      57              :     {MAIN_BOARD_ID_CARD_2PMESH, GetCardRankInfo},
      58              :     {MAIN_BOARD_ID_CARD_4PMESH, GetCardRankInfo},
      59              :     {MAIN_BOARD_ID_SERVER_8PMESH, ServerGetRootinfo},
      60              :     {MAIN_BOARD_ID_SERVER_TYPE1, ServerGetRootinfo},
      61              :     {MAIN_BOARD_ID_SERVER_8PMESH_UBOE, ServerGetRootinfo},
      62              :     {MAIN_BOARD_ID_SERVER_8PMESH_NOSP, ServerGetRootinfo},
      63              :     {MAIN_BOARD_ID_SERVER_8PMESH_NOSP_UBOE, ServerGetRootinfo},
      64              :     {MAIN_BOARD_ID_SERVER_UBX, ServerGetRootinfo},
      65              :     {MAIN_BOARD_ID_POD, PodGetRootinfo},
      66              :     {MAIN_BOARD_ID_POD_2D, PodGetRootinfo},
      67              : };
      68              : 
      69              : 
      70            3 : int TopoAddrInfoGetSize(int phyId, size_t* size)
      71              : {
      72            3 :     if (size == NULL) {
      73            0 :         return -1;
      74              :     }
      75              : 
      76              :     struct stat st;
      77            3 :     if (stat(DEFAULT_RANKINFO_FILE_PATH, &st) == 0) {
      78            0 :         (*size) = st.st_size;
      79            0 :         return 0;
      80              :     }
      81              : 
      82            3 :     uint32_t mainboard_id = 0;
      83            3 :     int ret = hal_get_mainboard_id(phyId, &mainboard_id);
      84            3 :     if (ret != 0) {
      85            0 :         return ret;
      86              :     }
      87              : 
      88              :     // 从g_get_size_func_table中查找对应的函数
      89           24 :     for (size_t i = 0; i < sizeof(g_get_size_func_table) / sizeof(g_get_size_func_table[0]); i++) {
      90           23 :         if (g_get_size_func_table[i].mainboard_id == mainboard_id) {
      91            2 :             return g_get_size_func_table[i].get_size_func(size);
      92              :         }
      93              :     }
      94            1 :     (*size) = DEFAULT_RANKINFO_SIZE;
      95            1 :     return 0;
      96              : }
      97              : 
      98            7 : static int PassThroughTopoFilePath(char* filePath, size_t bufSize)
      99              : {
     100            7 :     return GetTopoFilePathFromFile("/etc/hccl_rootinfo.json", filePath, bufSize);
     101              : }
     102              : 
     103              : /**
     104              :  *  获取拓扑文件路径
     105              :  */
     106            7 : int TopoAddrInfoGetTopoFilePath(int phyId, char* filePath, size_t bufSize)
     107              : {
     108            7 :     if (filePath == NULL) {
     109            0 :         return -1;
     110              :     }
     111              :     // 优先从/etc/hccl_rootinfo.json中读取
     112            7 :     int ret = PassThroughTopoFilePath(filePath, bufSize);
     113            7 :     if (ret == 0) {
     114            0 :         return ret;
     115              :     }
     116            7 :     uint32_t mainboard_id = 0;
     117            7 :     ret = hal_get_mainboard_id(phyId, &mainboard_id);
     118            7 :     if (ret != 0) {
     119            7 :         return ret;
     120              :     }
     121              :     struct dcmi_spod_info spod_info;
     122            0 :     ret = hal_get_spod_info(phyId, &spod_info);
     123            0 :     if (ret != 0) {
     124            0 :         return TopoGetFilePath(mainboard_id, TOPO_TYPE_IGNORE, filePath, bufSize);
     125              :     }
     126            0 :     return TopoGetFilePath(mainboard_id, spod_info.super_pod_type, filePath, bufSize);
     127              : }
     128              : 
     129          330 : static int PassThrough(char *rankInfo, size_t *bufSize)
     130              : {
     131          330 :     FILE *fp = fopen(DEFAULT_RANKINFO_FILE_PATH, "r");
     132          330 :     if (fp == NULL) {
     133          330 :         return -1;
     134              :     }
     135              :     struct stat stat;
     136            0 :     fstat(fileno(fp), &stat);
     137            0 :     if ((size_t)stat.st_size > (*bufSize)) {
     138            0 :         fclose(fp);
     139            0 :         return -1;
     140              :     }
     141            0 :     int ret = fread(rankInfo, 1, stat.st_size, fp);
     142            0 :     if (ret < 0) {
     143            0 :         fclose(fp);
     144            0 :         return -1;
     145              :     }
     146            0 :     *bufSize = (size_t)stat.st_size;
     147            0 :     return 0;
     148              : }
     149              : 
     150          330 : int TopoAddrInfoGet(int phyId, char* rankInfo, size_t *bufSize)
     151              : {
     152          330 :     TopoLogInit();  /* 懒初始化日志,全部下游共用 */
     153          330 :     TOPO_PERF_BEGIN(TopoAddrInfoGet);
     154          330 :     if (rankInfo == NULL || bufSize == NULL) {
     155            0 :         TOPO_PERF_END(TopoAddrInfoGet);
     156            0 :         return -1;
     157              :     }
     158              :     // 优先读取/etc/hccl_rootinfo.json中内容
     159          330 :     if (PassThrough(rankInfo, bufSize) == 0) {
     160            0 :         TOPO_PERF_END(TopoAddrInfoGet);
     161            0 :         return 0;
     162              :     }
     163              :     // 若/etc/hccl_rootinfo.json中无内容,根据mainboard_id生成rootinfo
     164          330 :     uint32_t mainboard_id = 0;
     165          330 :     int ret = hal_get_mainboard_id(phyId, &mainboard_id);
     166          330 :     if (ret != 0) {
     167            0 :         TOPO_PERF_END(TopoAddrInfoGet);
     168            0 :         return ret;
     169              :     }
     170              : 
     171          330 :     ret = -1;
     172         2008 :     for (size_t i = 0; i < sizeof(g_get_rootinfo_func_table)/sizeof(GetRootinfoFuncTable); ++i) {
     173         2008 :         if (g_get_rootinfo_func_table[i].mainboard_id == mainboard_id) {
     174          330 :             ret = g_get_rootinfo_func_table[i].get_rootinfo_func(phyId, mainboard_id, rankInfo, bufSize);
     175          330 :             break;
     176              :         }
     177              :     }
     178          330 :     TOPO_PERF_END(TopoAddrInfoGet);
     179          330 :     return ret;
     180              : }
        

Generated by: LCOV version 2.0-1