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 "base_config_legacy.h"
12 : #include <fstream>
13 : #include <algorithm>
14 : #include <array>
15 : #include "log.h"
16 :
17 : namespace Hccl {
18 :
19 : // EnvHostNicConfig
20 :
21 10 : void EnvHostNicConfig::Parse()
22 : {
23 10 : hcclIfIp.Parse();
24 14 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_IF_IP set by %s to [%s]", hcclIfIp.GetSource(), GetControlIfIp().Describe().c_str());
25 :
26 10 : hcclIfBasePort.Parse();
27 14 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_IF_BASE_PORT set by %s to [%u]", hcclIfBasePort.GetSource(), GetIfBasePort());
28 :
29 10 : hcclSocketIfName.Parse();
30 14 : HCCL_RUN_INFO(
31 : "[HCCL_ENV] HCCL_SOCKET_IFNAME set by %s to [%s]", hcclSocketIfName.GetSource(),
32 : GetSocketIfName().configIfNameStr.c_str());
33 :
34 10 : whitelistDisable.Parse();
35 14 : HCCL_RUN_INFO(
36 : "[HCCL_ENV] HCCL_WHITELIST_DISABLE set by %s to [%d]", whitelistDisable.GetSource(), whitelistDisable.Get());
37 :
38 10 : if (!whitelistDisable.Get()) {
39 1 : hcclWhiteListFile.Parse();
40 3 : HCCL_RUN_INFO(
41 : "[HCCL_ENV] HCCL_WHITELIST_FILE set by %s to [%s]", hcclWhiteListFile.GetSource(),
42 : GetWhiteListFile().c_str());
43 : }
44 :
45 10 : hcclHostSocketPortRange.Parse();
46 10 : std::ostringstream hosrPortRangeOss;
47 11 : for (auto range : GetHostSocketPortRange()) {
48 1 : hosrPortRangeOss << "[" << range.min << ", " << range.max << "]";
49 : }
50 14 : HCCL_RUN_INFO(
51 : "[HCCL_ENV] HCCL_HOST_SOCKET_PORT_RANGE set by %s to %s", hcclHostSocketPortRange.GetSource(),
52 : hosrPortRangeOss.str().c_str());
53 :
54 10 : hcclDeviceSocketPortRange.Parse();
55 10 : std::ostringstream devicePortRangeOss;
56 10 : for (auto range : GetDeviceSocketPortRange()) {
57 0 : devicePortRangeOss << "[" << range.min << ", " << range.max << "]";
58 : }
59 14 : HCCL_RUN_INFO(
60 : "[HCCL_ENV] HCCL_NPU_SOCKET_PORT_RANGE set by %s to %s", hcclDeviceSocketPortRange.GetSource(),
61 : devicePortRangeOss.str().c_str());
62 10 : }
63 :
64 18 : const IpAddress& EnvHostNicConfig::GetControlIfIp() const { return hcclIfIp.Get(); }
65 :
66 45 : u32 EnvHostNicConfig::GetIfBasePort() const { return hcclIfBasePort.Get(); }
67 :
68 19 : const SocketIfName& EnvHostNicConfig::GetSocketIfName() const { return hcclSocketIfName.Get(); }
69 :
70 42 : bool EnvHostNicConfig::GetWhitelistDisable() const { return whitelistDisable.Get(); }
71 :
72 2 : const std::string& EnvHostNicConfig::GetWhiteListFile() const { return hcclWhiteListFile.Get(); }
73 :
74 48 : const std::vector<SocketPortRange>& EnvHostNicConfig::GetHostSocketPortRange() const
75 : {
76 48 : return hcclHostSocketPortRange.Get();
77 : }
78 :
79 29 : const std::vector<SocketPortRange>& EnvHostNicConfig::GetDeviceSocketPortRange() const
80 : {
81 29 : return hcclDeviceSocketPortRange.Get();
82 : }
83 :
84 : // EnvSocketConfig
85 :
86 10 : void EnvSocketConfig::Parse()
87 : {
88 10 : hcclSocketFamily.Parse();
89 14 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_SOCKET_FAMILY set by %s to [%d]", hcclSocketFamily.GetSource(), GetSocketFamily());
90 :
91 10 : linkTimeOut.Parse();
92 14 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_CONNECT_TIMEOUT set by %s to [%d]s", linkTimeOut.GetSource(), GetLinkTimeOut());
93 10 : }
94 :
95 17 : s32 EnvSocketConfig::GetSocketFamily() const { return hcclSocketFamily.Get(); }
96 :
97 60 : s32 EnvSocketConfig::GetLinkTimeOut() const { return linkTimeOut.Get(); }
98 :
99 : // EnvRtsConfig
100 :
101 10 : void EnvRtsConfig::Parse()
102 : {
103 10 : execTimeOut.Parse();
104 10 : aivExecTimeOut.Parse();
105 14 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_EXEC_TIMEOUT set by %s to [%u]s", execTimeOut.GetSource(), GetExecTimeOut());
106 10 : }
107 :
108 264 : u32 EnvRtsConfig::GetExecTimeOut() const { return execTimeOut.Get(); }
109 :
110 1 : double EnvRtsConfig::GetAivExecTimeOut() const { return aivExecTimeOut.Get(); }
111 :
112 : // EnvRdmaConfig
113 :
114 23 : void EnvRdmaConfig::Parse()
115 : {
116 23 : rdmaTrafficClass.Parse();
117 53 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_RDMA_TC set by %s to [%u]", rdmaTrafficClass.GetSource(), GetRdmaTrafficClass());
118 :
119 23 : rdmaServerLevel.Parse();
120 53 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_RDMA_SL set by %s to [%u]", rdmaServerLevel.GetSource(), GetRdmaServerLevel());
121 :
122 23 : rdmaTimeOut.Parse();
123 53 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_RDMA_TIMEOUT set by %s to [%u]", rdmaTimeOut.GetSource(), GetRdmaTimeOut());
124 :
125 23 : rdmaRetryCnt.Parse();
126 53 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_RDMA_RETRY_CNT set by %s to [%u]", rdmaRetryCnt.GetSource(), GetRdmaRetryCnt());
127 :
128 23 : queueNum.Parse();
129 50 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_RDMA_QPS_PER_CONNECTION set by %s to [%u]", queueNum.GetSource(), GetRdmaQueueNum());
130 :
131 22 : multiQpThreshold.Parse();
132 47 : HCCL_RUN_INFO(
133 : "[HCCL_ENV] HCCL_MULTI_QP_THRESHOLD set by %s to [%u]B", multiQpThreshold.GetSource(),
134 : GetRdmaMultiQpThreshold());
135 :
136 21 : qpPortConfigPath.Parse();
137 41 : HCCL_RUN_INFO(
138 : "[HCCL_ENV] HCCL_RDMA_QP_PORT_CONFIG_PATH set by %s to [%s]", qpPortConfigPath.GetSource(),
139 : qpPortConfigPath.Get().c_str());
140 19 : ParseMultiQpSrcPortConfig();
141 19 : }
142 :
143 6 : HcclResult EnvRdmaConfig::OpenMultiQpConfigFile(std::ifstream& inFile)
144 : {
145 6 : std::string fileStr = qpPortConfigPath.Get() + "/MultiQpSrcPort.cfg";
146 6 : std::array<char, PATH_MAX> realFile{};
147 6 : if (realpath(fileStr.c_str(), realFile.data()) == nullptr) {
148 0 : HCCL_ERROR("[EnvRdmaConfig][OpenMultiQpConfigFile] config file path[%s] is invalid.", fileStr.c_str());
149 0 : return HCCL_E_PARA;
150 : }
151 :
152 6 : inFile.open(realFile.data(), std::ifstream::in);
153 6 : if (!inFile) {
154 0 : HCCL_ERROR("[EnvRdmaConfig][OpenMultiQpConfigFile] open config file[%s] failed.", realFile.data());
155 0 : return HCCL_E_PARA;
156 : }
157 24 : HCCL_INFO("[EnvRdmaConfig][OpenMultiQpConfigFile] open config file[%s] success.", realFile.data());
158 6 : return HCCL_SUCCESS;
159 6 : }
160 :
161 6 : HcclResult EnvRdmaConfig::ParseConfigContent(std::ifstream& inFile, MultiQpSrcPortConfig& config)
162 : {
163 6 : config.configDirPath = qpPortConfigPath.Get();
164 6 : u32 lineCnt = 1;
165 6 : std::string line;
166 131079 : while (std::getline(inFile, line)) {
167 131076 : std::string lineAvator = line;
168 131076 : line.erase(std::remove(line.begin(), line.end(), ' '), line.end());
169 131076 : line.erase(std::remove(line.begin(), line.end(), '\t'), line.end());
170 131076 : auto hashPos = line.find('#');
171 131076 : std::string lineInfo = (hashPos != std::string::npos) ? line.substr(0, hashPos) : line;
172 131076 : if (lineInfo.empty()) {
173 131070 : lineCnt++;
174 131070 : continue;
175 : }
176 :
177 6 : std::string ipPairKey, portPart;
178 6 : HcclResult ret = ParseLineToIpPairAndPortPart(lineInfo, lineCnt, lineAvator, ipPairKey, portPart);
179 6 : if (ret != HCCL_SUCCESS) {
180 1 : return ret;
181 : }
182 5 : std::vector<std::uint16_t> ports;
183 5 : ret = ParseSrcPortsFromPortPart(portPart, lineCnt, lineAvator, ports);
184 5 : if (ret != HCCL_SUCCESS) {
185 1 : return ret;
186 : }
187 4 : CHK_PRT_RET(
188 : config.ipPairToPorts.find(ipPairKey) != config.ipPairToPorts.end(),
189 : HCCL_ERROR(
190 : "[MulQpInfo][ParseSrcPortsFromString][line: %u] ip pair[%s] already exists.[%s]", lineCnt,
191 : ipPairKey.c_str(), lineAvator.c_str()),
192 : HCCL_E_PARA);
193 4 : config.ipPairToPorts[ipPairKey] = ports;
194 4 : if (lineCnt >= MultiQpSrcPortConfig::CONFIG_FILE_LINE_MAX) {
195 3 : HCCL_RUN_INFO("config file is too large, stop parsing at line %u.", lineCnt);
196 1 : break;
197 : }
198 3 : lineCnt++;
199 262157 : }
200 4 : return HCCL_SUCCESS;
201 6 : }
202 :
203 19 : void EnvRdmaConfig::ParseMultiQpSrcPortConfig()
204 : {
205 19 : if (!qpPortConfigPath.IsSetByEnvironment() || qpPortConfigPath.Get().empty()) {
206 13 : return;
207 : }
208 : try {
209 6 : std::ifstream inFile;
210 6 : CHK_PRT_THROW(
211 : OpenMultiQpConfigFile(inFile) != HCCL_SUCCESS,
212 : HCCL_ERROR("[EnvRdmaConfig][Parse] open multi qp src port config file failed."), InvalidParamsException,
213 : "open config file fail.");
214 :
215 6 : MultiQpSrcPortConfig config;
216 16 : CHK_PRT_THROW(
217 : ParseConfigContent(inFile, config) != HCCL_SUCCESS,
218 : HCCL_ERROR("[EnvRdmaConfig][Parse] parse multi qp src port config content failed."), InvalidParamsException,
219 : "parse config content fail.");
220 :
221 4 : inFile.close();
222 4 : multiQpSrcPortConfig_ = config;
223 4 : LogMultiQpSrcPortConfig();
224 10 : } catch (const HcclException& e) {
225 6 : HCCL_ERROR("[EnvRdmaConfig][Parse] LoadMultiQpSrcPortConfig failed: %s", e.what());
226 2 : } catch (const std::exception& e) {
227 0 : HCCL_ERROR("[EnvRdmaConfig][Parse] LoadMultiQpSrcPortConfig failed: %s", e.what());
228 0 : }
229 : }
230 :
231 6 : HcclResult EnvRdmaConfig::ParseLineToIpPairAndPortPart(
232 : const std::string& lineInfo, u32 lineCnt, const std::string& lineAvator, std::string& ipPairKey,
233 : std::string& portPart)
234 : {
235 6 : auto eqPos = lineInfo.find('=');
236 9 : CHK_PRT_RET(
237 : eqPos == std::string::npos || eqPos == 0 || eqPos == lineInfo.size() - 1,
238 : HCCL_ERROR(
239 : "[MulQpInfo][ParseSrcPortsFromString][line: %u]Expected format: "
240 : "'srcIP,dstIP=srcPort0,srcPort1,...'.[%s]",
241 : lineCnt, lineAvator.c_str()),
242 : HCCL_E_PARA);
243 5 : CHK_PRT_RET(
244 : lineInfo.find('=', eqPos + 1) != std::string::npos,
245 : HCCL_ERROR(
246 : "[MulQpInfo][ParseSrcPortsFromString][line: %u]Expected format: "
247 : "'srcIP,dstIP=srcPort0,srcPort1,...'.[%s]",
248 : lineCnt, lineAvator.c_str()),
249 : HCCL_E_PARA);
250 5 : std::string ipPart = lineInfo.substr(0, eqPos);
251 5 : portPart = lineInfo.substr(eqPos + 1);
252 :
253 5 : auto commaPos = ipPart.find(',');
254 5 : CHK_PRT_RET(
255 : commaPos == std::string::npos,
256 : HCCL_ERROR(
257 : "[MulQpInfo][ParseSrcPortsFromString][line: %u] IP pair format error, "
258 : "expected 'srcIP,dstIP'.[%s]",
259 : lineCnt, lineAvator.c_str()),
260 : HCCL_E_PARA);
261 5 : CHK_PRT_RET(
262 : ipPart.find(',', commaPos + 1) != std::string::npos,
263 : HCCL_ERROR(
264 : "[MulQpInfo][ParseSrcPortsFromString][line: %u] IP pair format error, "
265 : "expected 'srcIP,dstIP'.[%s]",
266 : lineCnt, lineAvator.c_str()),
267 : HCCL_E_PARA);
268 :
269 5 : IpAddress srcIpAddr, dstIpAddr;
270 : try {
271 5 : srcIpAddr = IpAddress(ipPart.substr(0, commaPos));
272 5 : dstIpAddr = IpAddress(ipPart.substr(commaPos + 1));
273 0 : } catch (const InvalidParamsException& e) {
274 0 : HCCL_ERROR(
275 : "[MulQpInfo][ParseSrcPortsFromString][line: %u] IP format error: %s.[%s]", lineCnt, e.what(),
276 : lineAvator.c_str());
277 0 : return HCCL_E_PARA;
278 0 : }
279 :
280 5 : ipPairKey = srcIpAddr.GetIpStr() + "," + dstIpAddr.GetIpStr();
281 5 : return HCCL_SUCCESS;
282 5 : }
283 :
284 5 : HcclResult EnvRdmaConfig::ParseSrcPortsFromPortPart(
285 : const std::string& portPart, u32 lineCnt, const std::string& lineAvator, std::vector<std::uint16_t>& ports)
286 : {
287 5 : std::size_t start = 0;
288 : while (true) {
289 69 : auto p = portPart.find(',', start);
290 69 : std::string token = (p != std::string::npos) ? portPart.substr(start, p - start) : portPart.substr(start);
291 :
292 69 : CHK_PRT_RET(
293 : token.empty(),
294 : HCCL_ERROR(
295 : "[MulQpInfo][ParseSrcPortsFromString][line: %u]src port[%s] is empty.[%s]", lineCnt, token.c_str(),
296 : lineAvator.c_str()),
297 : HCCL_E_PARA);
298 :
299 69 : unsigned long val = 0;
300 : try {
301 69 : std::size_t parsePos = 0;
302 69 : val = std::stoul(token, &parsePos, 0);
303 69 : if (parsePos != token.size()) {
304 0 : HCCL_ERROR(
305 : "[MulQpInfo][ParseSrcPortsFromString][line: %u] port[%s] is not a valid integer.[%s]", lineCnt,
306 : token.c_str(), lineAvator.c_str());
307 0 : return HCCL_E_PARA;
308 : }
309 0 : } catch (...) {
310 0 : HCCL_ERROR(
311 : "[MulQpInfo][ParseSrcPortsFromString][line: %u] port[%s] is not a valid integer.[%s]", lineCnt,
312 : token.c_str(), lineAvator.c_str());
313 0 : return HCCL_E_PARA;
314 0 : }
315 :
316 69 : CHK_PRT_RET(
317 : val == 0 || val > MultiQpSrcPortConfig::CONFIG_SRC_PORT_ID_MAX,
318 : HCCL_ERROR(
319 : "[MulQpInfo][ParseSrcPortsFromString][line: %u]src port[%s] "
320 : "should be within the range of[1, %u].[%s]",
321 : lineCnt, token.c_str(), MultiQpSrcPortConfig::CONFIG_SRC_PORT_ID_MAX, lineAvator.c_str()),
322 : HCCL_E_PARA);
323 :
324 69 : ports.emplace_back(static_cast<std::uint16_t>(val));
325 69 : if (p == std::string::npos)
326 5 : break;
327 64 : start = p + 1;
328 133 : }
329 :
330 8 : CHK_PRT_RET(
331 : ports.size() > MultiQpSrcPortConfig::CONFIG_SRC_PORT_NUM_MAX || ports.empty(),
332 : HCCL_ERROR(
333 : "[MulQpInfo][ParseSrcPortsFromString][line: %u]config ports num[%zu] more than the "
334 : "threshold[%u].[%s]",
335 : lineCnt, ports.size(), MultiQpSrcPortConfig::CONFIG_SRC_PORT_NUM_MAX, lineAvator.c_str()),
336 : HCCL_E_PARA);
337 :
338 4 : return HCCL_SUCCESS;
339 : }
340 :
341 4 : void EnvRdmaConfig::LogMultiQpSrcPortConfig() const
342 : {
343 8 : for (const auto& entry : multiQpSrcPortConfig_.ipPairToPorts) {
344 4 : std::string portsStr;
345 40 : for (size_t j = 0; j < entry.second.size(); j++) {
346 36 : if (j > 0)
347 32 : portsStr += ",";
348 36 : portsStr += std::to_string(entry.second[j]);
349 : }
350 12 : HCCL_RUN_INFO("[HCCL_ENV] MultiQpSrcPort: ipPair[%s] -> ports[%s]", entry.first.c_str(), portsStr.c_str());
351 4 : }
352 4 : }
353 :
354 34 : u32 EnvRdmaConfig::GetRdmaTrafficClass() const { return rdmaTrafficClass.Get(); }
355 :
356 34 : u32 EnvRdmaConfig::GetRdmaServerLevel() const { return rdmaServerLevel.Get(); }
357 :
358 24 : u32 EnvRdmaConfig::GetRdmaTimeOut() const { return rdmaTimeOut.Get(); }
359 :
360 24 : u32 EnvRdmaConfig::GetRdmaRetryCnt() const { return rdmaRetryCnt.Get(); }
361 :
362 23 : u32 EnvRdmaConfig::GetRdmaQueueNum() const { return queueNum.Get(); }
363 :
364 26 : u32 EnvRdmaConfig::GetRdmaMultiQpThreshold() const { return multiQpThreshold.Get(); }
365 :
366 22 : const MultiQpSrcPortConfig& EnvRdmaConfig::GetMultiQpSrcPortConfig() const { return multiQpSrcPortConfig_; }
367 :
368 : // EnvAlgoConfig
369 :
370 11 : void EnvAlgoConfig::Parse()
371 : {
372 : // Hcomm中不再解析HCCL_ALGO配置,因为没有实际使用
373 11 : primQueueGenName.Parse();
374 15 : HCCL_RUN_INFO(
375 : "[HCCL_ENV] PRIM_QUEUE_GEN_NAME set by %s to [%s]", primQueueGenName.GetSource(),
376 : GetPrimQueueGenName().c_str());
377 :
378 11 : bufferSize.Parse();
379 15 : HCCL_RUN_INFO("[HCCL_ENV] HCCL_BUFFSIZE set by %s to [%llu]B", bufferSize.GetSource(), GetBuffSize());
380 :
381 11 : hcclAccelerator_.Parse();
382 15 : HCCL_RUN_INFO(
383 : "[HCCL_ENV] HCCL_OP_EXPANSION_MODE set by %s to [%s]", hcclAccelerator_.GetSource(),
384 : GetHcclAccelerator().Describe().c_str());
385 11 : }
386 :
387 12 : const std::string& EnvAlgoConfig::GetPrimQueueGenName() const { return primQueueGenName.Get(); }
388 :
389 2 : const std::map<OpType, std::vector<HcclAlgoType>> EnvAlgoConfig::GetAlgoConfig() const
390 : {
391 : // hcomm不支持HCCL_ALGO解析, 返回空
392 2 : return {};
393 : }
394 :
395 21 : u64 EnvAlgoConfig::GetBuffSize() const { return bufferSize.Get(); }
396 :
397 27 : HcclAccelerator EnvAlgoConfig::GetHcclAccelerator() const { return hcclAccelerator_.Get(); }
398 :
399 : // EnvLogConfig
400 10 : void EnvLogConfig::Parse()
401 : {
402 10 : entryLogEnable.Parse();
403 14 : HCCL_RUN_INFO(
404 : "[HCCL_ENV] HCCL_ENTRY_LOG_ENABLE set by %s to [%d]", entryLogEnable.GetSource(), GetEntryLogEnable());
405 :
406 10 : cannVersion.Parse();
407 14 : HCCL_RUN_INFO("[HCCL_ENV] LD_LIBRARY_PATH set by %s to [%s]", cannVersion.GetSource(), GetCannVersion().c_str());
408 :
409 10 : dfsConfig.Parse();
410 14 : HCCL_RUN_INFO(
411 : "[HCCL_ENV] HCCL_DFS_CONFIG task_exception set by %s to [%d], cluster_heartbeat set by %s to [%d], "
412 : "rankConsistentState set by %s to [%d]",
413 : dfsConfig.GetSource(), GetDfsConfig().taskExceptionEnable, dfsConfig.GetSource(),
414 : GetDfsConfig().clusterHeartBeatEnable, dfsConfig.GetSource(), GetDfsConfig().rankConsistentState);
415 10 : }
416 :
417 150 : bool EnvLogConfig::GetEntryLogEnable() const { return entryLogEnable.Get(); }
418 :
419 11 : const std::string& EnvLogConfig::GetCannVersion() const { return cannVersion.Get(); }
420 :
421 261 : const DfsConfig& EnvLogConfig::GetDfsConfig() const { return dfsConfig.Get(); }
422 :
423 : // EnvDetourConfig
424 :
425 10 : void EnvDetourConfig::Parse()
426 : {
427 10 : detourType.Parse();
428 14 : HCCL_RUN_INFO(
429 : "[HCCL_ENV] HCCL_DETOUR set by %s to [%s]", detourType.GetSource(), GetDetourType().Describe().c_str());
430 10 : }
431 :
432 261 : HcclDetourType EnvDetourConfig::GetDetourType() const { return detourType.Get(); }
433 :
434 : } // namespace Hccl
|