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 HCCLV2_TP_MANAGER_H
12 : #define HCCLV2_TP_MANAGER_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 "ip_address.h"
22 : #include "orion_adapter_hccp.h"
23 :
24 : namespace Hccl {
25 :
26 : constexpr uint8_t TA_GEAR_INDEX_0 = 0;
27 : constexpr uint8_t TA_GEAR_INDEX_1 = 1;
28 : constexpr uint8_t TA_GEAR_INDEX_2 = 2;
29 : constexpr uint8_t TA_GEAR_INDEX_3 = 3;
30 :
31 : constexpr uint8_t TA_HW_GEAR0_BASE = 0;
32 : constexpr uint8_t TA_HW_GEAR1_BASE = 8;
33 : constexpr uint8_t TA_HW_GEAR2_BASE = 16;
34 : constexpr uint8_t TA_HW_GEAR3_BASE = 24;
35 :
36 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR0 = 512;
37 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR1 = 4000;
38 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR2 = 8000;
39 : static constexpr uint32_t TA_TIMEOUT_MS_GEAR3 = 32000;
40 :
41 : constexpr uint8_t AT_GEAR_MIN = 0;
42 : constexpr uint8_t AT_GEAR_MAX = 3;
43 : constexpr uint8_t AT_GEAR_DEFAULT = 2;
44 : constexpr uint32_t AT_TIMEOUT_MAP[4] = {16, 128, 1000, 4000};
45 :
46 : /// 与 GetTpInfo / ReleaseTpInfo 中 info、req 两级 map 的 qos 键一致(param.qos 低 8 位)
47 : using QosKey = uint32_t;
48 :
49 : /*
50 : * TP信息,当前申请TpHandle,不感知具体TP信息,当前仅支持TP与CTP
51 : * tpHandle: 对应管控面的TPID与相关资源,URMA通过引用计数管理申请和销毁TP
52 : */
53 : using TpHandle = uint64_t;
54 : struct TpInfo {
55 : TpHandle tpHandle{0};
56 : uint32_t mappedJettyPriority{0};
57 : bool hasMappedJettyPriority{false};
58 :
59 531 : TpInfo() = default;
60 : TpInfo(const TpHandle handle) : tpHandle(handle) {}
61 : };
62 :
63 : struct TpAttrInfo {
64 : struct TpAttr tpAttr {};
65 :
66 20 : TpAttrInfo() = default;
67 4 : TpAttrInfo(const struct TpAttr& attr) : tpAttr(attr) {}
68 : };
69 :
70 : struct GetTpAttrParam {
71 : TpHandle tpHandle{0};
72 : uint32_t attrBitmap{0};
73 :
74 : GetTpAttrParam() = default;
75 14 : GetTpAttrParam(const TpHandle handle, const uint32_t bitmap) : tpHandle(handle), attrBitmap(bitmap) {}
76 :
77 4 : std::string Describe() const
78 : {
79 4 : return Hccl::StringFormat("GetTpAttrParam[tpHandle=0x%llx, attrBitmap=0x%x]", tpHandle, attrBitmap);
80 : }
81 : };
82 :
83 : class TpManager {
84 : public:
85 : static TpManager& GetInstance(const int32_t deviceLogicId);
86 : void Init();
87 : /// `isSync==true`:同步路径(HOST_NET ctx + HrtRa* 同步接口),一次返回 SUCCESS。
88 : /// `isSync==false`:异步三阶段,轮询返回 HCCL_E_AGAIN。
89 : HcclResult GetTpInfo(const RaUbGetTpInfoParam& param, TpInfo& tpInfo, bool isSync = false);
90 : // unimport jetty 会 URMA 销毁 tp 资源,hccl 配套删除记录
91 : HcclResult ReleaseTpInfo(const RaUbGetTpInfoParam& param, const TpInfo& tpInfo);
92 : HcclResult GetTpAttr(const GetTpAttrParam& param, TpAttrInfo& tpAttrInfo, RdmaHandle rdmaHandle);
93 : HcclResult ReleaseTpAttr(const TpHandle tpHandle, const TpAttrInfo& tpAttrInfo);
94 :
95 : static HcclResult GetTpTotalTimeout(const TpAttrInfo& tpAttrInfo, uint32_t& tpTimeOutMs);
96 : static uint32_t TaHwValueToMs(uint8_t hwValue);
97 : static uint8_t FindMinTaHwValue(uint32_t tpTotalTimeoutMs);
98 :
99 : /// 哨兵值:调用方未传入 TA 超时值,CalcTaTimeout 将回退到协议默认值
100 : static constexpr uint8_t TA_TIMEOUT_NOT_SET = 0xFFU;
101 :
102 : /// 计算 Jetty 异步创建超时挡位(hw_value, 0-31)
103 : /// 入参 taTimeOut 由调用方按协议从环境变量获取后传入;TA_TIMEOUT_NOT_SET 表示未传入,使用协议默认值
104 : /// 入参 tpTimeOutMs 由 GetTpTotalTimeout 计算得到,用于环境变量时间 < TP 总超时时的自动升挡
105 : static uint8_t CalcTaTimeout(TpProtocol tpProtocol, uint8_t taTimeOut, uint32_t tpTimeOutMs);
106 :
107 : private:
108 : bool initFlag{false};
109 : uint32_t devLogicId{0};
110 : uint32_t devPhyId{0};
111 :
112 : struct TpInfoCtx {
113 : TpInfo tpInfo{};
114 : uint32_t useCnt{0};
115 :
116 17 : TpInfoCtx() = default;
117 17 : TpInfoCtx(const TpInfo& info, const uint32_t cnt) : tpInfo(info), useCnt(cnt) {}
118 : };
119 :
120 : struct TpAttrCtx {
121 : TpAttrInfo tpAttrInfo{};
122 : uint32_t useCnt{0};
123 :
124 4 : TpAttrCtx() = default;
125 4 : TpAttrCtx(const TpAttrInfo& info, const uint32_t cnt) : tpAttrInfo(info), useCnt(cnt) {}
126 : };
127 :
128 : /*
129 : * Request上下文,保存查询TP信息相关调用异步接口出参
130 : * handle: 异步接口调用handle,用于查询处理结果
131 : * tpInfoNum: 查询到的TP信息个数,当前为复用TP,只会申请1个
132 : * dataBuffer: 查询到的TP信息数据,原始数据保留缓冲区
133 : */
134 : struct RequestCtx {
135 : enum class ReqPhase : uint8_t { WAIT_LIST = 0, WAIT_TP_ATTR = 1 };
136 : ReqPhase phase{ReqPhase::WAIT_LIST};
137 : RequestHandle handle{0};
138 : uint32_t tpInfoNum{0};
139 : std::vector<char_t> dataBuffer;
140 : TpAttr tpAttr{};
141 : uint32_t tpAttrBitmap{0};
142 : };
143 :
144 : struct TpAttrRequestCtx {
145 : RequestHandle handle{0};
146 : struct TpAttr tpAttr {};
147 : uint32_t attrBitmap{0};
148 : };
149 :
150 : /// 三级索引:先按本端 IP,再按对端 IP,最后按 QoS 键(`QosKey`:`param.qos & 0xFF`,与 next `TpMgr` 一致)。
151 : using InfoQosMap = std::unordered_map<uint32_t, TpInfoCtx>;
152 : using InfoRmtMap = std::unordered_map<IpAddress, InfoQosMap>;
153 : using InfoCtxMap = std::unordered_map<IpAddress, InfoRmtMap>;
154 : using ReqQosMap = std::unordered_map<uint32_t, RequestCtx>;
155 : using ReqRmtMap = std::unordered_map<IpAddress, ReqQosMap>;
156 : using ReqCtxMap = std::unordered_map<IpAddress, ReqRmtMap>;
157 :
158 : using TpAttrCtxMap = std::unordered_map<TpHandle, TpAttrCtx>;
159 : using TpAttrReqCtxMap = std::unordered_map<TpHandle, TpAttrRequestCtx>;
160 :
161 : InfoCtxMap ctpInfoMap;
162 : ReqCtxMap ctpReqMap;
163 :
164 : InfoCtxMap tpInfoMap;
165 : ReqCtxMap tpReqMap;
166 :
167 : InfoCtxMap uboeInfoMap;
168 : ReqCtxMap uboeReqMap;
169 :
170 : InfoCtxMap ubRtpInfoMap;
171 : ReqCtxMap ubRtpReqMap;
172 :
173 : TpAttrCtxMap tpAttrCtxMap;
174 : TpAttrReqCtxMap tpAttrReqCtxMap;
175 :
176 : std::mutex ctpInfoMutex;
177 : std::mutex ctpReqMutex;
178 :
179 : std::mutex tpInfoMutex;
180 : std::mutex tpReqMutex;
181 :
182 : std::mutex uboeInfoMutex;
183 : std::mutex uboeReqMutex;
184 :
185 : std::mutex ubRtpInfoMutex;
186 : std::mutex ubRtpReqMutex;
187 :
188 : std::mutex tpAttrCtxMutex;
189 : std::mutex tpAttrReqMutex;
190 :
191 67 : TpManager() = default;
192 67 : ~TpManager() = default;
193 : TpManager(const TpManager& that) = delete;
194 : TpManager& operator=(const TpManager& that) = delete;
195 :
196 : HcclResult FindAndGetTpInfo(const RaUbGetTpInfoParam& param, TpInfo& tpInfo);
197 : HcclResult RunSyncGetTpInfo(const RaUbGetTpInfoParam& param, TpInfo& tpInfo);
198 : HcclResult RunAsyncGetTpInfo(const RaUbGetTpInfoParam& param, TpInfo& tpInfo);
199 : HcclResult StoreTpInfoResult(const RaUbGetTpInfoParam& param, TpInfo& tpInfo);
200 : HcclResult SyncGetFirstTpAttrForSlPolicy(
201 : const RaUbGetTpInfoParam& param, uint64_t firstTpHandle, TpAttr& tpAttr, uint32_t& attrBitmap) const;
202 : HcclResult AdvanceDeviceWaitListPhase(
203 : const RaUbGetTpInfoParam& param, RequestCtx& reqCtx, ReqQosMap& qosReqMap, ReqQosMap::iterator it,
204 : std::unique_lock<std::mutex>& reqCtxLock, TpInfo& tpInfo);
205 : void StartGetTpInfoListRequest(const RaUbGetTpInfoParam& param, RequestCtx& reqCtx, bool isSync) const;
206 : HcclResult FindAndGetTpAttr(const TpHandle tpHandle, TpAttrInfo& tpAttrInfo);
207 : HcclResult
208 : StartGetTpAttrRequest(const GetTpAttrParam& param, TpAttrRequestCtx& reqCtx, RdmaHandle rdmaHandle) const;
209 : HcclResult
210 : HandleCompletedTpAttrRequest(const TpAttrRequestCtx reqCtx, const TpHandle tpHandle, TpAttrInfo& tpAttrInfo);
211 :
212 : void StartGetTpAttrForFirstTpDevice(const RaUbGetTpInfoParam& param, RequestCtx& reqCtx) const;
213 : HcclResult
214 : HandleCompletedRequest(const RequestCtx reqCtx, const RaUbGetTpInfoParam& param, TpInfo& tpInfo, bool withSlPolicy);
215 : HcclResult
216 : MapTpInfoFromTpAttr(const RaUbGetTpInfoParam& param, const RequestCtx& reqCtx, TpInfo& outTpInfo, bool isSync);
217 :
218 : bool CheckRequestResult(RequestHandle& reqHandle) const;
219 : InfoCtxMap& GetInfoCtxMap(const TpProtocol tpProtocol);
220 : ReqCtxMap& GetReqCtxMap(const TpProtocol tpProtocol);
221 : std::mutex& GetInfoCtxMutex(const TpProtocol tpProtocol);
222 : std::mutex& GetReqCtxMutex(const TpProtocol tpProtocol);
223 : };
224 :
225 : /// UbConnection 释放 TpInfo:Release 键须与 GetTpInfo 时的业务 QoS 一致
226 : void ReleaseUbConnectionTp(
227 : int32_t devLogicId, const IpAddress& locAddr, const IpAddress& rmtAddr, TpProtocol tpProtocol, TpInfo& tpInfo,
228 : uint32_t requestQos);
229 :
230 : } // namespace Hccl
231 :
232 : #endif // HCCLV2_TP_MANAGER_H
|