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), phyId_(phyId), devType_(devType)
59 0 : {}
60 0 : NICDeployment GetNicDeployment() const
61 : {
62 0 : return nicDeployment_;
63 : }
64 :
65 0 : DevType GetDevType() const
66 : {
67 0 : return devType_;
68 : }
69 :
70 0 : std::int32_t GetPhyId() const
71 : {
72 0 : return phyId_;
73 : }
74 :
75 : private:
76 : NICDeployment nicDeployment_{NICDeployment::NIC_DEPLOYMENT_RESERVED};
77 : std::int32_t phyId_{-1};
78 : DevType devType_{DevType::DEV_TYPE_COUNT};
79 : };
80 :
81 : struct MulQpInfoCacheBase {
82 0 : virtual ~MulQpInfoCacheBase() = default;
83 : virtual HcclResult Init();
84 0 : virtual bool IsAvailable() const
85 : {
86 0 : return false;
87 : }
88 : void SetMulQpInfoFrom(MUL_QP_FROM mulQpInfoFrom);
89 : MUL_QP_FROM MulQpInfoFrom() const;
90 : // 获取ip->IP port个数
91 : virtual HcclResult GetPortsNumByIpPair(PortNum &portNum, const KeyPair &ipPair = KeyPair()) const;
92 : // 获取ip->ip port个数
93 : virtual HcclResult GetSpecialSourcePortsByIpPair(
94 : MulQpSourcePorts &sourcePorts, const KeyPair &ipPair = KeyPair()) const;
95 :
96 : protected:
97 : HcclResult initStatus_{HcclResult::HCCL_E_RESERVED};
98 : MUL_QP_FROM mulQpInfoFrom_{MUL_QP_FROM::MUL_QP_FROM_UNKNOWN};
99 : };
100 :
101 : struct DevCfgMulQpInfoCache : MulQpInfoCacheBase {
102 : explicit DevCfgMulQpInfoCache(const NICDeployment nicDeployment, const std::int32_t phyId);
103 0 : ~DevCfgMulQpInfoCache() override = default;
104 : HcclResult Init() override;
105 : bool IsAvailable() const override;
106 :
107 : // 获取ip->IP port个数
108 : HcclResult GetPortsNumByIpPair(PortNum &portNum, const KeyPair &ipPair = KeyPair()) const override;
109 : // 获取ip->ip port个数
110 : HcclResult GetSpecialSourcePortsByIpPair(
111 : MulQpSourcePorts &sourcePorts, const KeyPair &ipPair = KeyPair()) const override;
112 :
113 : private:
114 : NICDeployment nicDeployment_{NICDeployment::NIC_DEPLOYMENT_RESERVED};
115 : std::int32_t phyId_{-1};
116 : DevCfgQpInfo cacheInfo_;
117 : };
118 :
119 : struct DevNslbMulQpInfoCache : MulQpInfoCacheBase {
120 : explicit DevNslbMulQpInfoCache(const NICDeployment nicDeployment, const std::int32_t phyId);
121 0 : ~DevNslbMulQpInfoCache() override = default;
122 : HcclResult Init() override;
123 : bool IsAvailable() const override;
124 :
125 : // 获取ip->IP port个数
126 : HcclResult GetPortsNumByIpPair(PortNum &portNum, const KeyPair &ipPair = KeyPair()) const override;
127 : // 获取ip->ip port个数
128 : HcclResult GetSpecialSourcePortsByIpPair(
129 : MulQpSourcePorts &sourcePorts, const KeyPair &ipPair = KeyPair()) const override;
130 :
131 : private:
132 : NICDeployment nicDeployment_{NICDeployment::NIC_DEPLOYMENT_RESERVED};
133 : std::int32_t phyId_{-1};
134 : bool isEnableNslb_{false};
135 : };
136 :
137 : struct EnvConfigPathCache : MulQpInfoCacheBase {
138 : EnvConfigPathCache();
139 0 : ~EnvConfigPathCache() override = default;
140 : HcclResult Init() override;
141 : bool IsAvailable() const override;
142 : HcclResult LoadMultiQpSrcPortFromFile();
143 : static HcclResult GetIpPairFromString(
144 : std::string &s, std::string &ipPair, const std::uint32_t lineCnt, const std::string &lineAvator);
145 :
146 : // 获取ip->IP port个数
147 : HcclResult GetPortsNumByIpPair(PortNum &portNum, const KeyPair &ipPair = KeyPair()) const override;
148 : // 获取ip->ip port个数
149 : HcclResult GetSpecialSourcePortsByIpPair(
150 : MulQpSourcePorts &sourcePorts, const KeyPair &ipPair = KeyPair()) const override;
151 :
152 : private:
153 : EnvConfigPathQpInfo cacheInfo_;
154 : };
155 :
156 : struct EnvPerConnectionQpInfoCache : MulQpInfoCacheBase {
157 : EnvPerConnectionQpInfoCache();
158 0 : ~EnvPerConnectionQpInfoCache() override = default;
159 : HcclResult Init() override;
160 : bool IsAvailable() const override;
161 : // 获取ip->IP port个数
162 : HcclResult GetPortsNumByIpPair(PortNum &portNum, const KeyPair &ipPair = KeyPair()) const override;
163 : // 获取ip->ip port个数
164 : HcclResult GetSpecialSourcePortsByIpPair(
165 : MulQpSourcePorts &sourcePorts, const KeyPair &ipPair = KeyPair()) const override;
166 :
167 : private:
168 : EnvPerConnectionQpInfo cacheInfo_{};
169 : bool isSetEnvPerConnectionQp_{};
170 : };
171 :
172 : class MulQpInfo {
173 : public:
174 0 : explicit MulQpInfo() = default;
175 : virtual ~MulQpInfo();
176 : MulQpInfo(const MulQpInfo &) = delete;
177 : MulQpInfo &operator=(const MulQpInfo &) = delete;
178 : MulQpInfo(const MulQpInfo &&) = delete;
179 : MulQpInfo &operator=(const MulQpInfo &&) = delete;
180 :
181 : HcclResult Init(const InitParams ¶ms);
182 : // 已经调用Init函数
183 : bool IsInitialized();
184 : // 表示多QP信息按序解析正确且存在配置 //否则为多QP未配置 配置错误init就会失败
185 : HcclResult IsEnableMulQp(bool &isEnableMulQp);
186 : HcclResult GetMulQpFromType(MUL_QP_FROM &type);
187 :
188 : // 获取ip->IP port个数
189 : HcclResult GetPortsNumByIpPair(PortNum &portNum, const KeyPair &ipPair = KeyPair());
190 : // 获取ip->ip port个数
191 : HcclResult GetSpecialSourcePortsByIpPair(MulQpSourcePorts &sourcePorts, const KeyPair &ipPair = KeyPair());
192 :
193 : private:
194 : mutable std::mutex initLock_;
195 : HcclResult initStatus_{HcclResult::HCCL_E_RESERVED}; // 解析数据过程未发现配置错误,或者 未/非必须 配置
196 : std::unique_ptr<MulQpInfoCacheBase> config_{nullptr};
197 : };
198 : } // namespace hccl
199 :
200 : namespace std {
201 : template <>
202 : struct hash<const hccl::MUL_QP_FROM> {
203 : std::size_t operator()(const hccl::MUL_QP_FROM &type) const noexcept
204 : {
205 : return static_cast<std::size_t>(type);
206 : }
207 : };
208 :
209 : template <>
210 : struct hash<hccl::MUL_QP_FROM> {
211 : std::size_t operator()(const hccl::MUL_QP_FROM &type) const noexcept
212 : {
213 : return static_cast<std::size_t>(type);
214 : }
215 : };
216 : } // namespace std
217 : #endif // MULTI_QP_INFO_MANAGER_H
|