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_whitelist.h"
12 : #include <fstream>
13 : #include <string>
14 : #include <map>
15 : #include <hccl/base.h>
16 :
17 : #include "comm.h"
18 : #include "json_utils.h"
19 :
20 : namespace hccl {
21 4 : HcclWhitelist& HcclWhitelist::GetInstance()
22 : {
23 4 : static HcclWhitelist wl;
24 4 : return wl;
25 : }
26 :
27 1 : HcclWhitelist::HcclWhitelist() {}
28 :
29 1 : HcclWhitelist::~HcclWhitelist()
30 : {
31 1 : std::unique_lock<std::mutex> lock(whiteListsMutex_);
32 1 : whiteLists_.clear();
33 1 : }
34 :
35 2 : HcclResult HcclWhitelist::GetHostWhiteList(std::vector<HcclIpAddress>& whiteList)
36 : {
37 2 : std::unique_lock<std::mutex> lock(whiteListsMutex_);
38 2 : whiteList.clear();
39 2 : auto iter = whiteLists_.find(HcclWhiteListType::HCCL_WHITELIST_HOST);
40 2 : if (iter == whiteLists_.end()) {
41 0 : HCCL_INFO("GetHostWhiteList: white list is empty.");
42 0 : return HCCL_SUCCESS;
43 : }
44 2 : whiteList = whiteLists_[HcclWhiteListType::HCCL_WHITELIST_HOST];
45 2 : HCCL_INFO("GetHostWhiteList: whitelist length is %zu.", whiteList.size());
46 2 : return HCCL_SUCCESS;
47 2 : }
48 :
49 2 : HcclResult HcclWhitelist::LoadConfigFile(const std::string& realName)
50 : {
51 2 : CHK_PRT_RET(realName.empty(), HCCL_ERROR("[Load][ConfigFile]whitelist file path is nullptr."), HCCL_E_PARA);
52 2 : std::unique_lock<std::mutex> lock(whiteListsMutex_);
53 2 : whiteLists_.clear();
54 :
55 2 : nlohmann::json fileContent;
56 2 : std::ifstream infile(realName.c_str(), std::ifstream::in);
57 2 : if (!infile) {
58 0 : HCCL_ERROR("[Load][ConfigFile]open file %s failed", realName.c_str());
59 0 : return HCCL_E_INTERNAL;
60 : } else {
61 2 : fileContent.clear();
62 : try {
63 2 : infile >> fileContent; // 将文件内容读取到json对象内
64 0 : } catch (...) {
65 0 : HCCL_ERROR(
66 : "[Load][ConfigFile]errNo[0x%016llx] load file[%s] to json fail. please check json file format.",
67 : HCOM_ERROR_CODE(HCCL_E_INTERNAL), realName.c_str());
68 0 : infile.close();
69 0 : return HCCL_E_INTERNAL;
70 0 : }
71 : }
72 2 : infile.close();
73 :
74 2 : nlohmann::json hostWhitelist;
75 4 : CHK_RET(JsonUtils::GetJsonProperty(fileContent, "host_ip", hostWhitelist));
76 :
77 4 : for (auto& ipJson : hostWhitelist) {
78 2 : std::string ipStr;
79 : try {
80 2 : ipStr = ipJson.get<std::string>();
81 0 : } catch (...) {
82 0 : HCCL_ERROR(
83 : "[Load][ConfigFile]errNo[0x%016llx]get ipStr from ipJson failed, please check host white list",
84 : HCOM_ERROR_CODE(HCCL_E_INTERNAL));
85 0 : return HCCL_E_PARA;
86 0 : }
87 2 : HcclIpAddress ip(ipStr);
88 2 : CHK_PRT_RET(ip.IsInvalid(), HCCL_ERROR("string[%s] is invalid ip", ipStr.c_str()), HCCL_E_PARA);
89 2 : whiteLists_[HcclWhiteListType::HCCL_WHITELIST_HOST].push_back(ip);
90 2 : }
91 2 : return HCCL_SUCCESS;
92 2 : }
93 : } // namespace hccl
|