LCOV - code coverage report
Current view: top level - base_comm/common - tp_mgr.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 21 21
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 13 13

            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 经
      68              :     /// HrtRaSetTpAttrAsync(HDC)
      69              :     bool ccuLoopbackGetTpInfo{false};
      70              : 
      71            2 :     explicit GetTpInfoParamDef() = default;
      72              :     GetTpInfoParamDef(const CommAddr& locAddr, const CommAddr& rmtAddr, TpProtocol tpProtocol)
      73              :         : locAddr(locAddr),
      74              :           rmtAddr(rmtAddr),
      75              :           tpProtocol(tpProtocol)
      76              :     {}
      77              : 
      78         1149 :     std::string Describe() const
      79              :     {
      80         1149 :         Hccl::IpAddress locIpAddr{}, rmtIpAddr{};
      81         1149 :         (void)CommAddrToIpAddress(locAddr, locIpAddr);
      82         1149 :         (void)CommAddrToIpAddress(rmtAddr, rmtIpAddr);
      83              :         return Hccl::StringFormat(
      84              :             "RaUbGetTpInfoParam[locAddr=%s, rmtAddr=%s, tpProtocol=%s, qos=%u, loopFirstTpLowestSl=%d, ccuLoop=%d]",
      85         3447 :             locIpAddr.Describe().c_str(), rmtIpAddr.Describe().c_str(), tpProtocol.Describe().c_str(), qos,
      86         4596 :             static_cast<int>(loopFirstTpLowestSl), static_cast<int>(ccuLoopbackGetTpInfo));
      87              :     }
      88              : };
      89              : 
      90              : /*
      91              :  * TP信息,当前申请TpHandle,不感知具体TP信息,当前仅支持TP与CTP
      92              :  * tpHandle: 对应管控面的TPID与相关资源,URMA通过引用计数管理申请和销毁TP
      93              :  */
      94              : using TpHandle = uint64_t;
      95              : struct TpInfo {
      96              :     TpHandle tpHandle{0};
      97              :     uint32_t mappedJettyPriority{0};
      98              :     bool hasMappedJettyPriority{false};
      99              : 
     100          903 :     TpInfo() = default;
     101              :     explicit TpInfo(const TpHandle handle) : tpHandle(handle) {}
     102              : };
     103              : 
     104              : struct TpAttrInfo {
     105              :     struct TpAttr tpAttr {};
     106              : 
     107          328 :     TpAttrInfo() = default;
     108          100 :     TpAttrInfo(const struct TpAttr& attr) : tpAttr(attr) {}
     109              : };
     110              : 
     111              : using GetTpAttrParam = struct GetTpAttrParamDef {
     112              :     TpHandle tpHandle{0};
     113              :     uint32_t attrBitmap{0};
     114              : 
     115              :     explicit GetTpAttrParamDef() = default;
     116          205 :     GetTpAttrParamDef(const TpHandle tpHandle, const uint32_t attrBitmap)
     117          205 :         : tpHandle(tpHandle),
     118          205 :           attrBitmap(attrBitmap) {};
     119              : 
     120          100 :     std::string Describe() const
     121              :     {
     122              :         return Hccl::StringFormat(
     123          100 :             "GetTpAttrParam[tpHandle=0x%llx, attrBitmap=0x%x]", static_cast<unsigned long long>(tpHandle), attrBitmap);
     124              :     }
     125              : };
     126              : 
     127              : class TpMgr {
     128              : public:
     129              :     static TpMgr& GetInstance(const uint32_t devicePhyId);
     130              :     HcclResult GetTpInfo(const GetTpInfoParam& param, TpInfo& tpInfo);
     131              :     HcclResult GetTpAttr(const GetTpAttrParam& param, TpAttrInfo& tpAttrInfo, CtxHandle ctxHandle);
     132              :     // unimport jetty 会 URMA 销毁 tp 资源,hccl 配套删除记录
     133              :     HcclResult ReleaseTpInfo(const GetTpInfoParam& param, const TpInfo& tpInfo);
     134              :     HcclResult ReleaseTpAttr(const TpHandle tpHandle, const TpAttrInfo& tpAttrInfo);
     135              :     static HcclResult GetTpTotalTimeout(const TpAttrInfo& tpAttrInfo, uint32_t& tpTimeOutMs);
     136              :     static uint8_t CalcTaTimeout(const TpAttrInfo& tpAttrInfo);
     137              : 
     138              : private:
     139              :     struct TpInfoCtx {
     140              :         TpInfo tpInfo{};
     141              :         uint32_t useCnt{0};
     142              : 
     143          225 :         TpInfoCtx() = default;
     144          225 :         TpInfoCtx(const TpInfo& info, const uint32_t cnt) : tpInfo(info), useCnt(cnt) {}
     145              :     };
     146              : 
     147              :     struct TpAttrCtx {
     148              :         TpAttrInfo tpAttrInfo{};
     149              :         uint32_t useCnt{0};
     150              : 
     151          100 :         TpAttrCtx() = default;
     152          100 :         TpAttrCtx(const TpAttrInfo& info, const uint32_t cnt) : tpAttrInfo(info), useCnt(cnt) {}
     153              :     };
     154              : 
     155              :     /*
     156              :      * Request上下文,保存查询TP信息相关调用异步接口出参
     157              :      * handle: 异步接口调用handle,用于查询处理结果
     158              :      * tpInfoNum: 查询到的TP信息个数,当前为复用TP,只会申请1个
     159              :      * dataBuffer: 查询到的TP信息数据,原始数据保留缓冲区
     160              :      */
     161              :     enum class ReqPhase : uint8_t { WAIT_LIST = 0, WAIT_TP_ATTR = 1 };
     162              : 
     163              :     struct RequestCtx {
     164              :         ReqPhase phase{ReqPhase::WAIT_LIST};
     165              :         RequestHandle handle{0};
     166              :         uint32_t tpInfoNum{0};
     167              :         std::vector<char> dataBuffer;
     168              :         TpAttr tpAttr{};
     169              :         uint32_t tpAttrBitmap{0};
     170              :     };
     171              : 
     172              :     struct TpAttrRequestCtx {
     173              :         RequestHandle handle{0};
     174              :         struct TpAttr tpAttr {};
     175              :     };
     176              : 
     177              :     using TpAttrCtxMap = std::unordered_map<TpHandle, TpAttrCtx>;
     178              :     using TpAttrReqCtxMap = std::unordered_map<TpHandle, TpAttrRequestCtx>;
     179              : 
     180              :     /// 三级索引:先按本端 IP,再按对端 IP,最后按 QoS 档(0–7,与 GetTpInfo/TP-SL 策略里用的档位一致)。
     181              :     using InfoQosMap = std::unordered_map<uint32_t, TpInfoCtx>;
     182              :     using InfoRmtMap = std::unordered_map<Hccl::IpAddress, InfoQosMap>;
     183              :     using InfoCtxMap = std::unordered_map<Hccl::IpAddress, InfoRmtMap>;
     184              :     using ReqQosMap = std::unordered_map<uint32_t, RequestCtx>;
     185              :     using ReqRmtMap = std::unordered_map<Hccl::IpAddress, ReqQosMap>;
     186              :     using ReqCtxMap = std::unordered_map<Hccl::IpAddress, ReqRmtMap>;
     187              : 
     188              : private:
     189          132 :     TpMgr() = default;
     190          132 :     ~TpMgr() = default;
     191              :     TpMgr(const TpMgr& that) = delete;
     192              :     TpMgr& operator=(const TpMgr& that) = delete;
     193              : 
     194              :     HcclResult FindAndGetTpInfo(const GetTpInfoParam& param, TpInfo& tpInfo);
     195              :     HcclResult FindAndGetTpAttr(const TpHandle tpHandle, TpAttrInfo& tpAttrInfo);
     196              :     HcclResult LookupInfoCtxEntry(
     197              :         InfoCtxMap& infoMap, const Hccl::IpAddress& locAddr, const Hccl::IpAddress& rmtAddr, const QosKey qosKey,
     198              :         InfoCtxMap::iterator& lit, InfoRmtMap::iterator& rit, InfoQosMap::iterator& qosIt) const;
     199              :     HcclResult
     200              :     PollGetTpInfoReqCtx(std::unique_lock<std::mutex>& reqCtxLock, const GetTpInfoParam& param, TpInfo& tpInfo);
     201              :     HcclResult BeginGetTpInfoListRequest(const GetTpInfoParam& param, ReqQosMap& qosMap, const QosKey qosKey);
     202              :     HcclResult AdvanceGetTpInfoWaitList(
     203              :         const GetTpInfoParam& param, RequestCtx& reqCtx, ReqQosMap& qosMap, const ReqQosMap::iterator it,
     204              :         std::unique_lock<std::mutex>& reqCtxLock, TpInfo& tpInfo);
     205              : 
     206              :     HcclResult StartGetTpInfoListRequest(const GetTpInfoParam& param, RequestCtx& reqCtx) const;
     207              :     HcclResult StartGetTpAttrForFirstTp(const GetTpInfoParam& param, RequestCtx& reqCtx) const;
     208              :     HcclResult StartGetTpAttrRequest(const GetTpAttrParam& param, TpAttrRequestCtx& reqCtx, CtxHandle ctxHandle) const;
     209              :     HcclResult BuildTpInfoAndCommitQosAttr(
     210              :         const GetTpInfoParam& param, const RequestCtx& reqCtx, const struct HccpTpInfo* baseInfoPtr,
     211              :         const uint32_t tpListIndex, const uint32_t mappedSl, TpInfo& tpInfo);
     212              :     HcclResult CommitTpInfoToCache(const GetTpInfoParam& param, TpInfo& tpInfo);
     213              :     HcclResult HandleCompletedRequest(RequestCtx reqCtx, const GetTpInfoParam& param, TpInfo& tpInfo);
     214              :     HcclResult
     215              :     HandleCompletedTpAttrRequest(const TpAttrRequestCtx reqCtx, const TpHandle tpHandle, TpAttrInfo& tpAttrInfo);
     216              : 
     217              :     InfoCtxMap& GetInfoCtxMap(const TpProtocol tpProtocol);
     218              :     ReqCtxMap& GetReqCtxMap(const TpProtocol tpProtocol);
     219              :     std::mutex& GetInfoCtxMutex(const TpProtocol tpProtocol);
     220              :     std::mutex& GetReqCtxMutex(const TpProtocol tpProtocol);
     221              : 
     222              : private:
     223              :     bool initFlag_{false};
     224              :     uint32_t devPhyId_{0};
     225              : 
     226              :     InfoCtxMap ctpInfoMap_;
     227              :     ReqCtxMap ctpReqMap_;
     228              : 
     229              :     InfoCtxMap rtpInfoMap_;
     230              :     ReqCtxMap rtpReqMap_;
     231              : 
     232              :     InfoCtxMap uboeInfoMap_;
     233              :     ReqCtxMap uboeReqMap_;
     234              : 
     235              :     TpAttrCtxMap tpAttrCtxMap_;
     236              :     TpAttrReqCtxMap tpAttrReqCtxMap_;
     237              : 
     238              :     std::mutex ctpInfoMutex_;
     239              :     std::mutex ctpReqMutex_;
     240              : 
     241              :     std::mutex rtpInfoMutex_;
     242              :     std::mutex rtpReqMutex_;
     243              : 
     244              :     std::mutex uboeInfoMutex_;
     245              :     std::mutex uboeReqMutex_;
     246              : 
     247              :     std::mutex tpAttrCtxMutex_;
     248              :     std::mutex tpAttrReqMutex_;
     249              : };
     250              : 
     251              : } // namespace hcomm
     252              : 
     253              : #endif // TP_MGR_H
        

Generated by: LCOV version 2.0-1