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 : }
30 :
31 1 : HcclWhitelist::~HcclWhitelist()
32 : {
33 1 : std::unique_lock<std::mutex> lock(whiteListsMutex_);
34 1 : whiteLists_.clear();
35 1 : }
36 :
37 2 : HcclResult HcclWhitelist::GetHostWhiteList(std::vector<HcclIpAddress>& whiteList)
38 : {
39 2 : std::unique_lock<std::mutex> lock(whiteListsMutex_);
40 2 : whiteList.clear();
41 2 : auto iter = whiteLists_.find(HcclWhiteListType::HCCL_WHITELIST_HOST);
42 2 : if (iter == whiteLists_.end()) {
43 0 : HCCL_INFO("GetHostWhiteList: white list is empty.");
44 0 : return HCCL_SUCCESS;
45 : }
46 2 : whiteList = whiteLists_[HcclWhiteListType::HCCL_WHITELIST_HOST];
47 2 : HCCL_INFO("GetHostWhiteList: whitelist length is %zu.", whiteList.size());
48 2 : return HCCL_SUCCESS;
49 2 : }
50 :
51 2 : HcclResult HcclWhitelist::LoadConfigFile(const std::string& realName)
52 : {
53 2 : CHK_PRT_RET(realName.empty(), HCCL_ERROR("[Load][ConfigFile]whitelist file path is nullptr."), HCCL_E_PARA);
54 2 : std::unique_lock<std::mutex> lock(whiteListsMutex_);
55 2 : whiteLists_.clear();
56 :
57 2 : nlohmann::json fileContent;
58 2 : std::ifstream infile(realName.c_str(), std::ifstream::in);
59 2 : if (!infile) {
60 0 : HCCL_ERROR("[Load][ConfigFile]open file %s failed", realName.c_str());
61 0 : return HCCL_E_INTERNAL;
62 : } else {
63 2 : fileContent.clear();
64 : try {
65 2 : infile >> fileContent; // 将文件内容读取到json对象内
66 0 : } catch (...) {
67 0 : HCCL_ERROR("[Load][ConfigFile]errNo[0x%016llx] load file[%s] to json fail. please check json file format.",
68 : HCOM_ERROR_CODE(HCCL_E_INTERNAL), realName.c_str());
69 0 : infile.close();
70 0 : return HCCL_E_INTERNAL;
71 0 : }
72 : }
73 2 : infile.close();
74 :
75 2 : nlohmann::json hostWhitelist;
76 4 : CHK_RET(JsonUtils::GetJsonProperty(fileContent, "host_ip", hostWhitelist));
77 :
78 4 : for (auto& ipJson : hostWhitelist) {
79 2 : std::string ipStr;
80 : try {
81 2 : ipStr = ipJson.get<std::string>();
82 0 : } catch (...) {
83 0 : HCCL_ERROR("[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 : }
|