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 TP_MGR_H
12 : #define TP_MGR_H
13 :
14 : #include <cstdint>
15 : #include <functional>
16 : #include <mutex>
17 : #include <vector>
18 : #include <unordered_map>
19 :
20 : #include "hccl_types.h"
21 : #include "hcomm_adapter_hccp.h"
22 : #include "hccp_ctx_tp.h"
23 : #include "orion_adpt_utils.h"
24 :
25 : namespace hcomm {
26 :
27 : // ==================== TA 档位映射表 ====================
28 : // TA 芯片挡位 = hw_value / 8
29 : // 挡位 0 (0-7): 512ms
30 : // 挡位 1 (8-15): 1000ms (1s)
31 : // 挡位 2 (16-23): 8000ms (8s)
32 : // 挡位 3 (24-31): 32000ms (32s)
33 : // 各挡位对应的最小硬件配置值
34 : constexpr uint8_t TA_HW_GEAR0_BASE = 0;
35 : constexpr uint8_t TA_HW_GEAR1_BASE = 8;
36 : constexpr uint8_t TA_HW_GEAR2_BASE = 16;
37 : constexpr uint8_t TA_HW_GEAR3_BASE = 24;
38 :
39 : constexpr uint8_t TA_GEAR_INDEX_0 = 0;
40 : constexpr uint8_t TA_GEAR_INDEX_1 = 1;
41 : constexpr uint8_t TA_GEAR_INDEX_2 = 2;
42 : constexpr uint8_t TA_GEAR_INDEX_3 = 3;
43 :
44 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR0 = 512;
45 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR1 = 1000;
46 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR2 = 8000;
47 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR3 = 32000;
48 :
49 : constexpr uint8_t AT_GEAR_MIN = 0;
50 : constexpr uint8_t AT_GEAR_MAX = 3;
51 : constexpr uint8_t AT_GEAR_DEFAULT = 2;
52 : constexpr uint32_t AT_TIMEOUT_MAP[4] = {16, 128, 1000, 4000};
53 :
54 : /// 与 GetTpInfo / ReleaseTpInfo 中 info、req 两级 map 的 qos 键一致(param.qos 低 8 位)
55 : using QosKey = uint32_t;
56 :
57 : using GetTpInfoParam = struct GetTpInfoParamDef {
58 : CommAddr locAddr{};
59 : CommAddr rmtAddr{};
60 : TpProtocol tpProtocol{TpProtocol::CTP};
61 : /// 参与 TP/SL 分组与缓存键(0–7),连接侧已归一化
62 : uint32_t qos{EnvConfig::UB_QOS_DEFAULT};
63 : /// 非 0 时与 sl_available 推导的 M 取 min 作为可用档位数上限
64 : uint32_t slLevelCount{0};
65 : /// 环回等场景:首 TPID + 掩码内最小 SL
66 : bool loopFirstTpLowestSl{false};
67 : /// 仅 CCU 设备环回 GetTpInfo:与通信域 hcclQos 解耦,SL 来自 GetTpAttr.slBitmap;写回 SL 经 HrtRaSetTpAttrAsync(HDC)
68 : bool ccuLoopbackGetTpInfo{false};
69 :
70 2 : explicit GetTpInfoParamDef() = default;
71 : GetTpInfoParamDef(const CommAddr &locAddr, const CommAddr &rmtAddr, TpProtocol tpProtocol)
72 : : locAddr(locAddr), rmtAddr(rmtAddr), tpProtocol(tpProtocol) {}
73 :
74 999 : std::string Describe() const {
75 999 : Hccl::IpAddress locIpAddr{}, rmtIpAddr{};
76 999 : (void)CommAddrToIpAddress(locAddr, locIpAddr);
77 999 : (void)CommAddrToIpAddress(rmtAddr, rmtIpAddr);
78 : return Hccl::StringFormat(
79 : "RaUbGetTpInfoParam[locAddr=%s, rmtAddr=%s, tpProtocol=%s, qos=%u, loopFirstTpLowestSl=%d, ccuLoop=%d]",
80 2997 : locIpAddr.Describe().c_str(), rmtIpAddr.Describe().c_str(), tpProtocol.Describe().c_str(), qos,
81 3996 : static_cast<int>(loopFirstTpLowestSl), static_cast<int>(ccuLoopbackGetTpInfo));
82 : }
83 : };
84 :
85 : /*
86 : * TP信息,当前申请TpHandle,不感知具体TP信息,当前仅支持TP与CTP
87 : * tpHandle: 对应管控面的TPID与相关资源,URMA通过引用计数管理申请和销毁TP
88 : */
89 : using TpHandle = uint64_t;
90 : struct TpInfo {
91 : TpHandle tpHandle{0};
92 : uint32_t mappedJettyPriority{0};
93 : bool hasMappedJettyPriority{false};
94 :
95 783 : TpInfo() = default;
96 : explicit TpInfo(const TpHandle handle)
97 : : tpHandle(handle) {}
98 : };
99 :
100 : struct TpAttrInfo {
101 : struct TpAttr tpAttr{0};
102 :
103 283 : TpAttrInfo() = default;
104 85 : TpAttrInfo(const struct TpAttr &attr)
105 85 : : tpAttr(attr) {}
106 : };
107 :
108 : using GetTpAttrParam = struct GetTpAttrParamDef {
109 : TpHandle tpHandle{0};
110 : uint32_t attrBitmap{0};
111 :
112 : explicit GetTpAttrParamDef() = default;
113 175 : GetTpAttrParamDef(const TpHandle tpHandle, const uint32_t attrBitmap)
114 175 : : tpHandle(tpHandle), attrBitmap(attrBitmap){};
115 :
116 85 : std::string Describe() const {
117 : return Hccl::StringFormat("GetTpAttrParam[tpHandle=0x%llx, attrBitmap=0x%x]",
118 85 : static_cast<unsigned long long>(tpHandle), attrBitmap);
119 : }
120 : };
121 :
122 :
123 : class TpMgr {
124 : public:
125 : static TpMgr &GetInstance(const uint32_t devicePhyId);
126 : HcclResult GetTpInfo(const GetTpInfoParam ¶m, TpInfo &tpInfo);
127 : HcclResult GetTpAttr(const GetTpAttrParam ¶m, TpAttrInfo &tpAttrInfo, CtxHandle ctxHandle);
128 : // unimport jetty 会 URMA 销毁 tp 资源,hccl 配套删除记录
129 : HcclResult ReleaseTpInfo(const GetTpInfoParam ¶m, const TpInfo &tpInfo);
130 : HcclResult ReleaseTpAttr(const TpHandle tpHandle, const TpAttrInfo &tpAttrInfo);
131 : static HcclResult GetTpTotalTimeout(const TpAttrInfo &tpAttrInfo, uint32_t &tpTimeOutMs);
132 : static uint8_t CalcTaTimeout(const TpAttrInfo &tpAttrInfo);
133 :
134 : private:
135 : struct TpInfoCtx {
136 : TpInfo tpInfo{};
137 : uint32_t useCnt{0};
138 :
139 195 : TpInfoCtx() = default;
140 195 : TpInfoCtx(const TpInfo &info, const uint32_t cnt)
141 195 : : tpInfo(info), useCnt(cnt) {}
142 : };
143 :
144 : struct TpAttrCtx {
145 : TpAttrInfo tpAttrInfo{};
146 : uint32_t useCnt{0};
147 :
148 85 : TpAttrCtx() = default;
149 85 : TpAttrCtx(const TpAttrInfo &info, const uint32_t cnt)
150 85 : : tpAttrInfo(info), useCnt(cnt) {}
151 : };
152 :
153 : /*
154 : * Request上下文,保存查询TP信息相关调用异步接口出参
155 : * handle: 异步接口调用handle,用于查询处理结果
156 : * tpInfoNum: 查询到的TP信息个数,当前为复用TP,只会申请1个
157 : * dataBuffer: 查询到的TP信息数据,原始数据保留缓冲区
158 : */
159 : enum class ReqPhase : uint8_t { WAIT_LIST = 0, WAIT_TP_ATTR = 1 };
160 :
161 : struct RequestCtx {
162 : ReqPhase phase{ReqPhase::WAIT_LIST};
163 : RequestHandle handle{0};
164 : uint32_t tpInfoNum{0};
165 : std::vector<char> dataBuffer;
166 : TpAttr tpAttr{};
167 : uint32_t tpAttrBitmap{0};
168 : };
169 :
170 : struct TpAttrRequestCtx {
171 : RequestHandle handle{0};
172 : struct TpAttr tpAttr{0};
173 : };
174 :
175 : using TpAttrCtxMap = std::unordered_map<TpHandle, TpAttrCtx>;
176 : using TpAttrReqCtxMap = std::unordered_map<TpHandle, TpAttrRequestCtx>;
177 :
178 : /// 三级索引:先按本端 IP,再按对端 IP,最后按 QoS 档(0–7,与 GetTpInfo/TP-SL 策略里用的档位一致)。
179 : using InfoQosMap = std::unordered_map<uint32_t, TpInfoCtx>;
180 : using InfoRmtMap = std::unordered_map<Hccl::IpAddress, InfoQosMap>;
181 : using InfoCtxMap = std::unordered_map<Hccl::IpAddress, InfoRmtMap>;
182 : using ReqQosMap = std::unordered_map<uint32_t, RequestCtx>;
183 : using ReqRmtMap = std::unordered_map<Hccl::IpAddress, ReqQosMap>;
184 : using ReqCtxMap = std::unordered_map<Hccl::IpAddress, ReqRmtMap>;
185 :
186 : private:
187 132 : TpMgr() = default;
188 132 : ~TpMgr() = default;
189 : TpMgr(const TpMgr &that) = delete;
190 : TpMgr &operator=(const TpMgr &that) = delete;
191 :
192 : HcclResult FindAndGetTpInfo(const GetTpInfoParam ¶m, TpInfo &tpInfo);
193 : HcclResult FindAndGetTpAttr(const TpHandle tpHandle, TpAttrInfo &tpAttrInfo);
194 : HcclResult LookupInfoCtxEntry(InfoCtxMap &infoMap, const Hccl::IpAddress &locAddr, const Hccl::IpAddress &rmtAddr,
195 : const QosKey qosKey, InfoCtxMap::iterator &lit, InfoRmtMap::iterator &rit, InfoQosMap::iterator &qosIt) const;
196 : HcclResult PollGetTpInfoReqCtx(std::unique_lock<std::mutex> &reqCtxLock, const GetTpInfoParam ¶m, TpInfo &tpInfo);
197 : HcclResult BeginGetTpInfoListRequest(const GetTpInfoParam ¶m, ReqQosMap &qosMap, const QosKey qosKey);
198 : HcclResult AdvanceGetTpInfoWaitList(const GetTpInfoParam ¶m, RequestCtx &reqCtx, ReqQosMap &qosMap,
199 : const ReqQosMap::iterator it, std::unique_lock<std::mutex> &reqCtxLock, TpInfo &tpInfo);
200 :
201 : HcclResult StartGetTpInfoListRequest(const GetTpInfoParam ¶m, RequestCtx &reqCtx) const;
202 : HcclResult StartGetTpAttrForFirstTp(const GetTpInfoParam ¶m, RequestCtx &reqCtx) const;
203 : HcclResult StartGetTpAttrRequest(const GetTpAttrParam ¶m, TpAttrRequestCtx &reqCtx, CtxHandle ctxHandle) const;
204 : HcclResult BuildTpInfoAndCommitQosAttr(const GetTpInfoParam ¶m, const RequestCtx &reqCtx,
205 : const struct HccpTpInfo *baseInfoPtr, const uint32_t tpListIndex, const uint32_t mappedSl, TpInfo &tpInfo);
206 : HcclResult CommitTpInfoToCache(const GetTpInfoParam ¶m, TpInfo &tpInfo);
207 : HcclResult HandleCompletedRequest(RequestCtx reqCtx, const GetTpInfoParam ¶m, TpInfo &tpInfo);
208 : HcclResult HandleCompletedTpAttrRequest(const TpAttrRequestCtx reqCtx, const TpHandle tpHandle,
209 : TpAttrInfo &tpAttrInfo);
210 :
211 : InfoCtxMap &GetInfoCtxMap(const TpProtocol tpProtocol);
212 : ReqCtxMap &GetReqCtxMap(const TpProtocol tpProtocol);
213 : std::mutex &GetInfoCtxMutex(const TpProtocol tpProtocol);
214 : std::mutex &GetReqCtxMutex(const TpProtocol tpProtocol);
215 :
216 : private:
217 : bool initFlag_{false};
218 : uint32_t devPhyId_{0};
219 :
220 : InfoCtxMap ctpInfoMap_;
221 : ReqCtxMap ctpReqMap_;
222 :
223 : InfoCtxMap rtpInfoMap_;
224 : ReqCtxMap rtpReqMap_;
225 :
226 : InfoCtxMap uboeInfoMap_;
227 : ReqCtxMap uboeReqMap_;
228 :
229 : TpAttrCtxMap tpAttrCtxMap_;
230 : TpAttrReqCtxMap tpAttrReqCtxMap_;
231 :
232 : std::mutex ctpInfoMutex_;
233 : std::mutex ctpReqMutex_;
234 :
235 : std::mutex rtpInfoMutex_;
236 : std::mutex rtpReqMutex_;
237 :
238 : std::mutex uboeInfoMutex_;
239 : std::mutex uboeReqMutex_;
240 :
241 : std::mutex tpAttrCtxMutex_;
242 : std::mutex tpAttrReqMutex_;
243 : };
244 :
245 : } // namespace hcomm
246 :
247 : #endif // TP_MGR_H
|