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 : #ifndef MULTI_QP_INFO_MANAGER_H
12 : #define MULTI_QP_INFO_MANAGER_H
13 : #include "hccl_common.h"
14 : #include <cstdint>
15 : #include <memory>
16 : #include <mutex>
17 : #include <unordered_map>
18 : #include <vector>
19 : #include <utility>
20 : #include "hccl_ip_address.h"
21 :
22 : namespace hccl {
23 : constexpr u32 MULTI_QP_CONFIG_SUB_STRING_NUM = 2; // 配置信息格式为"sip,dip=sport0,sport1,...", 因此会被=分为两个子串
24 : constexpr u32 MULTI_QP_CONFIG_IP_NUM = 2; // 有两个ip,分别为源ip和目的ip
25 : constexpr u32 MULTI_QP_CONFIG_IP_PAIR_SHIFT_NUM = 32;
26 : constexpr u32 MULTI_QP_CONFIG_FILE_LINE_MAX = 128 * 1024; // 配置文件最多只能配置128k行有效内容
27 : constexpr u32 MULTI_QP_CONFIG_SRC_PORT_NUM_MAX = 32; // 一对ip对最多配置32个源端口号
28 : constexpr u32 MULTI_QP_CONFIG_SRC_PORT_ID_MAX = 65535;
29 : enum class MUL_QP_FROM : std::uint8_t {
30 : MUL_QP_FROM_DEV_CFG,
31 : MUL_QP_FROM_DEV_NSLB,
32 : MUL_QP_FROM_ENV_PORT_CONFIG_PATH,
33 : MUL_QP_FROM_ENV_PER_CONNECTION,
34 : MUL_QP_FROM_UNKNOWN
35 : };
36 :
37 : using SourceIpAddress = HcclIpAddress;
38 : using DstIpAddress = HcclIpAddress;
39 : using KeyPair = std::pair<SourceIpAddress, DstIpAddress>; // srcIp,DstIp
40 : struct HcclIpAddressPairHash {
41 : std::size_t operator()(const std::pair<SourceIpAddress, DstIpAddress>& p) const
42 : {
43 : const std::size_t h1 = std::hash<const char*>{}(p.first.GetReadableAddress());
44 : const std::size_t h2 = std::hash<const char*>{}(p.second.GetReadableAddress());
45 : return h1 ^ (h2 << 1);
46 : }
47 : };
48 : using Port = std::uint16_t;
49 : using MulQpSourcePorts = std::vector<Port>;
50 : using DevCfgQpInfo = MulQpSourcePorts;
51 : using DevNslbPort = MulQpSourcePorts;
52 : using EnvConfigPathQpInfo = std::unordered_map<std::string, MulQpSourcePorts>;
53 : using EnvPerConnectionQpInfo = std::uint16_t;
54 : using PortNum = std::uint32_t;
55 :
56 : struct InitParams {
57 0 : explicit InitParams(const NICDeployment nicDeployment, const std::int32_t phyId, const DevType devType)
58 0 : : nicDeployment_(nicDeployment),
59 0 : phyId_(phyId),
60 0 : devType_(devType)
61 0 : {}
62 0 : NICDeployment GetNicDeployment() const { return nicDeployment_; }
63 :
64 0 : DevType GetDevType() const { return devType_; }
65 :
66 0 : std::int32_t GetPhyId() const { return phyId_; }
67 :
68 : private:
69 : NICDeployment nicDeployment_{NICDeployment::NIC_DEPLOYMENT_RESERVED};
70 : std::int32_t phyId_{-1};
71 : DevType devType_{DevType::DEV_TYPE_COUNT};
72 : };
73 :
74 : struct MulQpInfoCacheBase {
75 0 : virtual ~MulQpInfoCacheBase() = default;
76 : virtual HcclResult Init();
77 0 : virtual bool IsAvailable() const { return false; }
78 : void SetMulQpInfoFrom(MUL_QP_FROM mulQpInfoFrom);
79 : MUL_QP_FROM MulQpInfoFrom() const;
80 : // 获取ip->IP port个数
81 : virtual HcclResult GetPortsNumByIpPair(PortNum& portNum, const KeyPair& ipPair = KeyPair()) const;
82 : // 获取ip->ip port个数
83 : virtual HcclResult
84 : GetSpecialSourcePortsByIpPair(MulQpSourcePorts& sourcePorts, const KeyPair& ipPair = KeyPair()) const;
85 :
86 : protected:
87 : HcclResult initStatus_{HcclResult::HCCL_E_RESERVED};
88 : MUL_QP_FROM mulQpInfoFrom_{MUL_QP_FROM::MUL_QP_FROM_UNKNOWN};
89 : };
90 :
91 : struct DevCfgMulQpInfoCache : MulQpInfoCacheBase {
92 : explicit DevCfgMulQpInfoCache(const NICDeployment nicDeployment, const std::int32_t phyId);
93 0 : ~DevCfgMulQpInfoCache() override = default;
94 : HcclResult Init() override;
95 : bool IsAvailable() const override;
96 :
97 : // 获取ip->IP port个数
98 : HcclResult GetPortsNumByIpPair(PortNum& portNum, const KeyPair& ipPair = KeyPair()) const override;
99 : // 获取ip->ip port个数
100 : HcclResult
101 : GetSpecialSourcePortsByIpPair(MulQpSourcePorts& sourcePorts, const KeyPair& ipPair = KeyPair()) const override;
102 :
103 : private:
104 : NICDeployment nicDeployment_{NICDeployment::NIC_DEPLOYMENT_RESERVED};
105 : std::int32_t phyId_{-1};
106 : DevCfgQpInfo cacheInfo_;
107 : };
108 :
109 : struct DevNslbMulQpInfoCache : MulQpInfoCacheBase {
110 : explicit DevNslbMulQpInfoCache(const NICDeployment nicDeployment, const std::int32_t phyId);
111 0 : ~DevNslbMulQpInfoCache() override = default;
112 : HcclResult Init() override;
113 : bool IsAvailable() const override;
114 :
115 : // 获取ip->IP port个数
116 : HcclResult GetPortsNumByIpPair(PortNum& portNum, const KeyPair& ipPair = KeyPair()) const override;
117 : // 获取ip->ip port个数
118 : HcclResult
119 : GetSpecialSourcePortsByIpPair(MulQpSourcePorts& sourcePorts, const KeyPair& ipPair = KeyPair()) const override;
120 :
121 : private:
122 : NICDeployment nicDeployment_{NICDeployment::NIC_DEPLOYMENT_RESERVED};
123 : std::int32_t phyId_{-1};
124 : bool isEnableNslb_{false};
125 : };
126 :
127 : struct EnvConfigPathCache : MulQpInfoCacheBase {
128 : EnvConfigPathCache();
129 0 : ~EnvConfigPathCache() override = default;
130 : HcclResult Init() override;
131 : bool IsAvailable() const override;
132 : HcclResult LoadMultiQpSrcPortFromFile();
133 : static HcclResult GetIpPairFromString(
134 : std::string& s, std::string& ipPair, const std::uint32_t lineCnt, const std::string& lineAvator);
135 :
136 : // 获取ip->IP port个数
137 : HcclResult GetPortsNumByIpPair(PortNum& portNum, const KeyPair& ipPair = KeyPair()) const override;
138 : // 获取ip->ip port个数
139 : HcclResult
140 : GetSpecialSourcePortsByIpPair(MulQpSourcePorts& sourcePorts, const KeyPair& ipPair = KeyPair()) const override;
141 :
142 : private:
143 : EnvConfigPathQpInfo cacheInfo_;
144 : };
145 :
146 : struct EnvPerConnectionQpInfoCache : MulQpInfoCacheBase {
147 : EnvPerConnectionQpInfoCache();
148 0 : ~EnvPerConnectionQpInfoCache() override = default;
149 : HcclResult Init() override;
150 : bool IsAvailable() const override;
151 : // 获取ip->IP port个数
152 : HcclResult GetPortsNumByIpPair(PortNum& portNum, const KeyPair& ipPair = KeyPair()) const override;
153 : // 获取ip->ip port个数
154 : HcclResult
155 : GetSpecialSourcePortsByIpPair(MulQpSourcePorts& sourcePorts, const KeyPair& ipPair = KeyPair()) const override;
156 :
157 : private:
158 : EnvPerConnectionQpInfo cacheInfo_{};
159 : bool isSetEnvPerConnectionQp_{};
160 : };
161 :
162 : class MulQpInfo {
163 : public:
164 0 : explicit MulQpInfo() = default;
165 : virtual ~MulQpInfo();
166 : MulQpInfo(const MulQpInfo&) = delete;
167 : MulQpInfo& operator=(const MulQpInfo&) = delete;
168 : MulQpInfo(const MulQpInfo&&) = delete;
169 : MulQpInfo& operator=(const MulQpInfo&&) = delete;
170 :
171 : HcclResult Init(const InitParams& params);
172 : // 已经调用Init函数
173 : bool IsInitialized();
174 : // 表示多QP信息按序解析正确且存在配置 //否则为多QP未配置 配置错误init就会失败
175 : HcclResult IsEnableMulQp(bool& isEnableMulQp);
176 : HcclResult GetMulQpFromType(MUL_QP_FROM& type);
177 :
178 : // 获取ip->IP port个数
179 : HcclResult GetPortsNumByIpPair(PortNum& portNum, const KeyPair& ipPair = KeyPair());
180 : // 获取ip->ip port个数
181 : HcclResult GetSpecialSourcePortsByIpPair(MulQpSourcePorts& sourcePorts, const KeyPair& ipPair = KeyPair());
182 :
183 : private:
184 : mutable std::mutex initLock_;
185 : HcclResult initStatus_{HcclResult::HCCL_E_RESERVED}; // 解析数据过程未发现配置错误,或者 未/非必须 配置
186 : std::unique_ptr<MulQpInfoCacheBase> config_{nullptr};
187 : };
188 : } // namespace hccl
189 :
190 : namespace std {
191 : template <>
192 : struct hash<const hccl::MUL_QP_FROM> {
193 : std::size_t operator()(const hccl::MUL_QP_FROM& type) const noexcept { return static_cast<std::size_t>(type); }
194 : };
195 :
196 : template <>
197 : struct hash<hccl::MUL_QP_FROM> {
198 : std::size_t operator()(const hccl::MUL_QP_FROM& type) const noexcept { return static_cast<std::size_t>(type); }
199 : };
200 : } // namespace std
201 : #endif // MULTI_QP_INFO_MANAGER_H
|