LCOV - code coverage report
Current view: top level - base_comm/resources/southbound_adpt - hcomm_adapter_hccp.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 65.6 % 346 227
Test Date: 2026-08-29 17:38:31 Functions: 85.0 % 20 17

            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              : #include "hcomm_adapter_hccp.h"
      12              : 
      13              : #include <algorithm>
      14              : 
      15              : #include "securec.h"
      16              : #include "log.h"
      17              : #include "orion_adpt_utils.h"
      18              : #include "hccp_tlv.h"
      19              : #include "hccp_common.h"
      20              : 
      21              : #include "hccp_async.h"
      22              : #include "hccp_async_ctx.h"
      23              : #include "enum_factory.h"
      24              : 
      25              : #include "hccp_tlv_hdc_manager.h"
      26              : #include "exception_handler.h"
      27              : #include "hccp_ctx_dfx.h"
      28              : 
      29              : namespace hcomm {
      30              : 
      31          478 : HcclResult IpAddressToHccpEid(const Hccl::IpAddress& ipAddr, Eid& eid)
      32              : {
      33          478 :     HCCL_INFO("EID ipAddr[%s]", ipAddr.Describe().c_str());
      34          478 :     int32_t sRet = memcpy_s(eid.raw, sizeof(eid.raw), ipAddr.GetEid().raw, sizeof(ipAddr.GetEid().raw));
      35          478 :     if (sRet != EOK) {
      36            0 :         HCCL_ERROR("[%s] memcpy failed[%d].", __func__, sRet);
      37            0 :         return HcclResult::HCCL_E_MEMORY;
      38              :     }
      39          478 :     HCCL_INFO(
      40              :         "[IpAddressToHccpEid] hccpEid.in6.subnetPrefix[%016llx], hccpEid.in6.interfaceId[%016llx]",
      41              :         eid.in6.subnetPrefix, eid.in6.interfaceId);
      42          478 :     return HcclResult::HCCL_SUCCESS;
      43              : }
      44              : 
      45          214 : HcclResult IpAddressToReverseHcclEid(const Hccl::IpAddress& ipAddr, Hccl::Eid& eid)
      46              : {
      47          214 :     HCCL_INFO("EID ipAddr[%s]", ipAddr.Describe().c_str());
      48          214 :     int32_t sRet = memcpy_s(eid.raw, sizeof(eid.raw), ipAddr.GetReverseEid().raw, sizeof(ipAddr.GetReverseEid().raw));
      49          214 :     if (sRet != EOK) {
      50            0 :         HCCL_ERROR("[%s] memcpy failed[%d].", __func__, sRet);
      51            0 :         return HcclResult::HCCL_E_MEMORY;
      52              :     }
      53          214 :     HCCL_INFO(
      54              :         "[IpAddressToHccpEid] hccpEid.in6.subnetPrefix[%016llx], hccpEid.in6.interfaceId[%016llx]",
      55              :         eid.in6.subnetPrefix, eid.in6.interfaceId);
      56          214 :     return HcclResult::HCCL_SUCCESS;
      57              : }
      58              : 
      59           81 : inline Hccl::IpAddress HccpEidToIpAddress(Eid& hccpEid)
      60              : {
      61           81 :     Hccl::Eid eid{};
      62           81 :     HCCL_INFO(
      63              :         "[HccpEidToIpAddress] hccpEid.in6.subnetPrefix[%016llx], hccpEid.in6.interfaceId[%016llx]",
      64              :         hccpEid.in6.subnetPrefix, hccpEid.in6.interfaceId);
      65           81 :     s32 sRet = memcpy_s(eid.raw, sizeof(eid.raw), hccpEid.raw, sizeof(hccpEid.raw));
      66           81 :     if (sRet != EOK) {
      67            0 :         HCCL_ERROR("failed to change eid to ip");
      68            0 :         return Hccl::IpAddress{}; // 暂时不处理
      69              :     }
      70           81 :     return Hccl::IpAddress(eid);
      71              : }
      72              : 
      73           29 : HcclResult RaGetDevEidInfos(const RaInfo& raInfo, std::vector<DevEidInfo>& devEidInfos)
      74              : {
      75           29 :     uint32_t num = 0;
      76           58 :     int32_t ret = RaGetDevEidInfoNum(raInfo, &num);
      77           29 :     if (ret != 0) {
      78            0 :         HCCL_ERROR("call RaGetDevEidInfoNum failed, error code =%d.", ret);
      79            0 :         return HcclResult::HCCL_E_NETWORK;
      80              :     }
      81              : 
      82          110 :     struct HccpDevEidInfo infoList[num] = {};
      83           29 :     ret = RaGetDevEidInfoList(raInfo, infoList, &num);
      84           29 :     if (ret != 0) {
      85            0 :         HCCL_ERROR("call RaGetDevEidInfoList failed, error code =%d.", ret);
      86            0 :         return HcclResult::HCCL_E_NETWORK;
      87              :     }
      88              : 
      89           29 :     devEidInfos.resize(num);
      90          110 :     for (uint32_t i = 0; i < num; i++) {
      91           81 :         devEidInfos[i].name = (infoList[i].name);
      92           81 :         Hccl::IpAddress ipAddr = HccpEidToIpAddress(infoList[i].eid);
      93           81 :         CHK_RET(IpAddressToCommAddr(ipAddr, devEidInfos[i].commAddr));
      94           81 :         devEidInfos[i].type = infoList[i].type;
      95           81 :         devEidInfos[i].eidIndex = infoList[i].eidIndex;
      96           81 :         devEidInfos[i].dieId = infoList[i].dieId;
      97           81 :         devEidInfos[i].chipId = infoList[i].chipId;
      98           81 :         devEidInfos[i].funcId = infoList[i].funcId;
      99           81 :         devEidInfos[i].devFeature = infoList[i].devFeature;
     100              :     }
     101              : 
     102           29 :     return HcclResult::HCCL_SUCCESS;
     103           29 : }
     104              : 
     105          585 : RequestResult HccpGetAsyncReqResult(RequestHandle& reqHandle)
     106              : {
     107          585 :     if (reqHandle == 0) {
     108            0 :         HCCL_ERROR("[%s] failed, reqHandle is 0.", __func__);
     109            0 :         return RequestResult::INVALID_PARA;
     110              :     }
     111              : 
     112          585 :     int reqResult = 0;
     113          585 :     int32_t ret = RaGetAsyncReqResult(reinterpret_cast<void*>(reqHandle), &reqResult);
     114              :     // 返回 OTHERS_EAGAIN 代表查询到异步任务未完成,需要重新查询,此时保留handle
     115          585 :     if (ret == OTHERS_EAGAIN) {
     116            0 :         return RequestResult::NOT_COMPLETED;
     117              :     }
     118              : 
     119              :     // 返回码非0代表调用查询接口失败,当前仅入参错误时触发
     120          585 :     if (ret != 0) {
     121            0 :         HCCL_ERROR(
     122              :             "[%s] failed to get asynchronous request result[%d], "
     123              :             "reqhandle[%llx].",
     124              :             __func__, ret, reqHandle);
     125            0 :         return RequestResult::GET_REQ_RESULT_FAILED;
     126              :     }
     127              : 
     128          585 :     RequestHandle tmpReqHandle = reqHandle;
     129              :     // 返回码为 0 时,reqResult为异步任务完成结果,0代表成功,其他值代表失败
     130              :     // SOCK_EAGAIN 为 socket 类执行结果,代表 socket 接口失败需要重试
     131          585 :     if (reqResult == SOCK_EAGAIN) {
     132            0 :         return RequestResult::SOCK_E_AGAIN;
     133              :     }
     134              : 
     135          585 :     if (reqResult != 0) {
     136            0 :         HCCL_ERROR(
     137              :             "[%s] failed, the asynchronous request "
     138              :             "error[%d], reqhandle[%llx].",
     139              :             __func__, reqResult, tmpReqHandle);
     140            0 :         return RequestResult::ASYNC_REQUEST_FAILED;
     141              :     }
     142              : 
     143          585 :     return RequestResult::COMPLETED;
     144              : }
     145              : 
     146              : const std::map<HrtTransportMode, TransportModeT> HRT_TRANSPORT_MODE_MAP
     147              :     = {{HrtTransportMode::RM, TransportModeT::CONN_RM}};
     148              : const std::map<HrtJettyMode, JettyMode> HRT_JETTY_MODE_MAP
     149              :     = {{HrtJettyMode::STANDARD, JettyMode::JETTY_MODE_URMA_NORMAL},
     150              :        {HrtJettyMode::HOST_OFFLOAD, JettyMode::JETTY_MODE_USER_CTL_NORMAL},
     151              :        {HrtJettyMode::HOST_OPBASE, JettyMode::JETTY_MODE_USER_CTL_NORMAL},
     152              :        {HrtJettyMode::DEV_USED, JettyMode::JETTY_MODE_USER_CTL_NORMAL},
     153              :        {HrtJettyMode::CACHE_LOCK_DWQE, JettyMode::JETTY_MODE_CACHE_LOCK_DWQE},
     154              :        {HrtJettyMode::CCU_CCUM_CACHE, JettyMode::JETTY_MODE_CCU},
     155              :        {HrtJettyMode::CCU_TA_CACHE, JettyMode::JETTY_MODE_CCU_TA_CACHE}};
     156              : 
     157              : constexpr uint8_t RNR_RETRY = 7;
     158              : constexpr uint32_t RQ_DEPTH = 256;
     159              : 
     160              : HcclResult
     161          214 : HccpUbCreateJetty(const CtxHandle ctxHandle, const HrtRaUbCreateJettyParam& in, HrtRaUbJettyCreatedOutParam& out)
     162              : {
     163          214 :     struct QpCreateAttr attr {};
     164          214 :     attr.scqHandle = reinterpret_cast<void*>(in.sjfcHandle);
     165          214 :     attr.rcqHandle = reinterpret_cast<void*>(in.rjfcHandle);
     166          214 :     attr.srqHandle = reinterpret_cast<void*>(in.sjfcHandle);
     167          214 :     attr.rqDepth = RQ_DEPTH;
     168          214 :     attr.sqDepth = in.sqDepth;
     169          214 :     attr.transportMode = HRT_TRANSPORT_MODE_MAP.at(in.transMode);
     170          214 :     attr.ub.mode = HRT_JETTY_MODE_MAP.at(in.jettyMode);
     171              : 
     172          214 :     attr.ub.tokenValue = in.tokenValue;
     173          214 :     attr.ub.tokenIdHandle = reinterpret_cast<void*>(in.tokenIdHandle);
     174          214 :     attr.ub.flag.value = 0;
     175              :     /* errTime配置值:0-31
     176              :        0-7代表芯片配置值b00:512ms
     177              :        8-15代表芯片配置值b01:4s
     178              :        16-23代表芯片配置值b10:8s
     179              :        24-31代表芯片配置值b11:32s
     180              :     */
     181          214 :     attr.ub.errTimeout = in.errTimeout;
     182          214 :     attr.ub.priority = static_cast<uint8_t>(in.qos & 0xFU);
     183          214 :     attr.ub.rnrRetry = RNR_RETRY;
     184          214 :     attr.ub.flag.bs.shareJfr = 1;
     185          214 :     attr.ub.jettyId = in.jettyId;
     186              :     // 在continue模式下+配置了wqe的fence标记,并且远端有一些权限校验错误/内存异常错误,硬件会直接挂死
     187              :     // jfs_flag 的 error_suspend 设置为 1,
     188          214 :     attr.ub.jfsFlag.bs.errorSuspend = 1;
     189              : 
     190          214 :     if (in.jettyMode == HrtJettyMode::CCU_TA_CACHE) {
     191           38 :         attr.ub.tokenValue = in.tokenValue;
     192           38 :         attr.ub.taCacheMode.lockFlag = true;
     193           38 :         attr.ub.taCacheMode.sqeBufIdx = in.sqeBufIndex;
     194              :     } else {
     195          176 :         attr.ub.extMode.sqebbNum = in.sqDepth;
     196              :     }
     197              : 
     198          214 :     if (in.jettyMode == HrtJettyMode::HOST_OFFLOAD) {
     199            0 :         attr.ub.extMode.piType = 1;
     200          214 :     } else if (in.jettyMode == HrtJettyMode::CCU_CCUM_CACHE) {
     201          176 :         attr.ub.tokenValue = in.tokenValue;
     202          176 :         attr.ub.extMode.cstmFlag.bs.sqCstm = 1;
     203          176 :         attr.ub.extMode.sq.buffSize = in.sqBufSize;
     204          176 :         attr.ub.extMode.sq.buffVa = in.sqBufVa;
     205           38 :     } else if (in.jettyMode == HrtJettyMode::DEV_USED) {
     206            0 :         attr.ub.extMode.cstmFlag.bs.sqCstm = 1;
     207            0 :         attr.ub.extMode.sq.buffSize = in.sqBufSize;
     208            0 :         attr.ub.extMode.sq.buffVa = in.sqBufVa;
     209              :     }
     210              : 
     211              :     // 其他Mode暂时不需要额外更新特定字段
     212          214 :     HCCL_INFO(
     213              :         "Create jetty, input params: attr.ub.jettyId[%u], attr.rqDepth[%u], "
     214              :         "attr.sqDepth[%u], attr.transportMode[%d], attr.ub.mode[%d], "
     215              :         "attr.ub.extMode.sqebbNum[%u], attr.ub.extMode.sq.buffVa[%llx], "
     216              :         "attr.ub.extMode.sq.buffSize[%u], attr.ub.extMode.piType[%u], "
     217              :         "attr.ub.priority[%u], timeout[%u], attr.ub.taCacheMode.sqeBufIdx[%u].",
     218              :         attr.ub.jettyId, attr.rqDepth, attr.sqDepth, attr.transportMode, attr.ub.mode, attr.ub.extMode.sqebbNum,
     219              :         attr.ub.extMode.sq.buffVa, attr.ub.extMode.sq.buffSize, attr.ub.extMode.piType, attr.ub.priority,
     220              :         attr.ub.errTimeout, attr.ub.taCacheMode.sqeBufIdx);
     221              : 
     222          214 :     struct QpCreateInfo info {};
     223          214 :     void* qpHandle = nullptr;
     224          214 :     int32_t ret = RaCtxQpCreate(ctxHandle, &attr, &info, &qpHandle);
     225          214 :     if (ret != 0) {
     226            0 :         HCCL_ERROR(
     227              :             "[%s] failed, ctxHandle[%p] jetty_id[%u] JettyMode[%s] "
     228              :             "sqDepth[%u] sq.buffVa[%llx] sq.buffSize[%u].",
     229              :             __func__, ctxHandle, attr.ub.jettyId, in.jettyMode.Describe().c_str(), attr.sqDepth,
     230              :             attr.ub.extMode.sq.buffVa, attr.ub.extMode.sq.buffSize);
     231            0 :         return HcclResult::HCCL_E_NETWORK;
     232              :     }
     233              : 
     234              :     // 适配URMA,直接组装WQE的TOKENID需要进行移位,包括CCU与AICPU
     235          214 :     constexpr u32 URMA_TOKEN_ID_RIGHT_SHIFT = 8;
     236              : 
     237          214 :     out.handle = reinterpret_cast<JettyHandle>(qpHandle);
     238          214 :     out.id = info.ub.id;
     239          214 :     out.uasid = info.ub.uasid;
     240          214 :     out.jettyVa = info.va;
     241          214 :     out.dbVa = info.ub.dbAddr;
     242          214 :     out.dbTokenId = info.ub.dbTokenId >> URMA_TOKEN_ID_RIGHT_SHIFT;
     243              : 
     244          214 :     int32_t sRet = memcpy_s(out.key, sizeof(out.key), info.key.value, info.key.size);
     245          214 :     if (sRet != 0) {
     246            0 :         HCCL_ERROR("[%s] failed, memcpy failed[%d].", __func__, sRet);
     247            0 :         return HcclResult::HCCL_E_MEMORY;
     248              :     }
     249          214 :     out.keySize = info.key.size;
     250          214 :     attr.ub.tokenValue = 0; // 清理栈中的敏感信息
     251          214 :     HCCL_INFO("[%s], output params: out.id[%u], out.dbVa[%llx]", __func__, out.id, out.dbVa);
     252              : 
     253          214 :     return HcclResult::HCCL_SUCCESS;
     254              : }
     255              : 
     256            0 : HcclResult HccpUbCreateJettyAsync(
     257              :     const CtxHandle ctxhandle, const HrtRaUbCreateJettyParam& in, std::vector<char>& out, void*& jettyHandle,
     258              :     RequestHandle& reqHandle)
     259              : {
     260            0 :     struct QpCreateAttr attr {};
     261            0 :     attr.scqHandle = reinterpret_cast<void*>(in.sjfcHandle);
     262            0 :     attr.rcqHandle = reinterpret_cast<void*>(in.rjfcHandle);
     263            0 :     attr.srqHandle = reinterpret_cast<void*>(in.sjfcHandle);
     264            0 :     attr.rqDepth = RQ_DEPTH;
     265            0 :     attr.sqDepth = in.sqDepth;
     266            0 :     attr.transportMode = HRT_TRANSPORT_MODE_MAP.at(in.transMode);
     267            0 :     attr.ub.mode = HRT_JETTY_MODE_MAP.at(in.jettyMode);
     268              : 
     269            0 :     attr.ub.tokenValue = in.tokenValue;
     270            0 :     attr.ub.tokenIdHandle = reinterpret_cast<void*>(in.tokenIdHandle);
     271            0 :     attr.ub.flag.value = 0;
     272              :     /* errTime配置值:0-31
     273              :        0-7代表芯片配置值b00:512ms
     274              :        8-15代表芯片配置值b01:4s
     275              :        16-23代表芯片配置值b10:8s
     276              :        24-31代表芯片配置值b11:32s
     277              :     */
     278            0 :     attr.ub.errTimeout = in.errTimeout;
     279            0 :     attr.ub.priority = static_cast<uint8_t>(in.qos & 0xFU);
     280            0 :     attr.ub.rnrRetry = RNR_RETRY;
     281            0 :     attr.ub.flag.bs.shareJfr = 1;
     282            0 :     attr.ub.jettyId = in.jettyId;
     283              :     // 在continue模式下+配置了wqe的fence标记,并且远端有一些权限校验错误/内存异常错误,硬件会直接挂死
     284              :     // jfs_flag 的 error_suspend 设置为 1,
     285            0 :     attr.ub.jfsFlag.bs.errorSuspend = 1;
     286              : 
     287            0 :     if (in.jettyMode == HrtJettyMode::CCU_TA_CACHE) {
     288            0 :         attr.ub.tokenValue = in.tokenValue;
     289            0 :         attr.ub.taCacheMode.lockFlag = true;
     290            0 :         attr.ub.taCacheMode.sqeBufIdx = in.sqeBufIndex;
     291              :     } else {
     292            0 :         attr.ub.extMode.sqebbNum = in.sqDepth;
     293              :     }
     294              : 
     295            0 :     if (in.jettyMode == HrtJettyMode::HOST_OFFLOAD) {
     296            0 :         attr.ub.extMode.piType = 1;
     297            0 :     } else if (in.jettyMode == HrtJettyMode::CCU_CCUM_CACHE) {
     298            0 :         attr.ub.tokenValue = in.tokenValue;
     299            0 :         attr.ub.extMode.cstmFlag.bs.sqCstm = 1;
     300            0 :         attr.ub.extMode.sq.buffSize = in.sqBufSize;
     301            0 :         attr.ub.extMode.sq.buffVa = in.sqBufVa;
     302            0 :     } else if (in.jettyMode == HrtJettyMode::DEV_USED) {
     303            0 :         attr.ub.extMode.cstmFlag.bs.sqCstm = 1;
     304            0 :         attr.ub.extMode.sq.buffSize = in.sqBufSize;
     305            0 :         attr.ub.extMode.sq.buffVa = in.sqBufVa;
     306              :     }
     307              : 
     308              :     // 其他Mode暂时不需要额外更新特定字段
     309            0 :     HCCL_INFO(
     310              :         "Create jetty, input params: attr.ub.jettyId[%u], attr.rqDepth[%u], "
     311              :         "attr.sqDepth[%u], attr.transportMode[%d], attr.ub.mode[%d], "
     312              :         "attr.ub.extMode.sqebbNum[%u], attr.ub.extMode.sq.buffVa[%llx], "
     313              :         "attr.ub.extMode.sq.buffSize[%u], attr.ub.extMode.piType[%u], "
     314              :         "attr.ub.priority[%u], timeout[%u], attr.ub.taCacheMode.sqeBufIdx[%u].",
     315              :         attr.ub.jettyId, attr.rqDepth, attr.sqDepth, attr.transportMode, attr.ub.mode, attr.ub.extMode.sqebbNum,
     316              :         attr.ub.extMode.sq.buffVa, attr.ub.extMode.sq.buffSize, attr.ub.extMode.piType, attr.ub.priority,
     317              :         attr.ub.errTimeout, attr.ub.taCacheMode.sqeBufIdx);
     318              : 
     319            0 :     void* raReqHandle = nullptr;
     320            0 :     out.resize(sizeof(QpCreateInfo));
     321              :     s32 ret
     322            0 :         = RaCtxQpCreateAsync(ctxhandle, &attr, reinterpret_cast<QpCreateInfo*>(out.data()), &jettyHandle, &raReqHandle);
     323            0 :     if (ret != 0 || !raReqHandle) {
     324            0 :         HCCL_ERROR(
     325              :             "[%s] failed, call interface error[%d], raReqHandle[%p], "
     326              :             "ctxHanlde[%p].",
     327              :             __func__, ret, raReqHandle, ctxhandle);
     328            0 :         return HcclResult::HCCL_E_NETWORK;
     329              :     }
     330            0 :     attr.ub.tokenValue = 0; // 清理栈中的token信息
     331            0 :     HCCL_INFO("[%s] ok, get handle[%llu].", __func__, reinterpret_cast<RequestHandle>(raReqHandle));
     332            0 :     reqHandle = reinterpret_cast<RequestHandle>(raReqHandle);
     333            0 :     return HcclResult::HCCL_SUCCESS;
     334              : }
     335              : 
     336          214 : static HcclResult ImportJetty(
     337              :     const CtxHandle ctxHandle, u8* key, const u32 keyLen, const u32 tokenValue, const JettyImportExpCfg& cfg,
     338              :     const JettyImportMode mode, const TpProtocol protocol, HrtRaUbJettyImportedOutParam& out)
     339              : {
     340          214 :     if (mode == JettyImportMode::JETTY_IMPORT_MODE_NORMAL) {
     341            0 :         HCCL_ERROR("[%s] currently not support JETTY_IMPORT_MODE_NORMAL.", __func__);
     342            0 :         return HcclResult::HCCL_E_NOT_SUPPORT;
     343              :     }
     344              : 
     345          214 :     if (protocol != TpProtocol::RTP && protocol != TpProtocol::CTP) {
     346            0 :         HCCL_ERROR("[%s] failed, tp protocol[%s] is not expected.", __func__, protocol.Describe().c_str());
     347            0 :         return HcclResult::HCCL_E_NOT_SUPPORT;
     348              :     }
     349              : 
     350          214 :     struct QpImportInfoT info {};
     351          214 :     int res = memcpy_s(info.in.key.value, sizeof(info.in.key.value), key, keyLen);
     352          214 :     if (res != 0) {
     353            0 :         HCCL_ERROR("[%s] memcpy_s failed, ret = %d", __func__, res);
     354            0 :         return HcclResult::HCCL_E_MEMORY;
     355              :     }
     356          214 :     info.in.key.size = keyLen;
     357              : 
     358          214 :     info.in.ub.mode = mode;
     359          214 :     info.in.ub.tokenValue = tokenValue;
     360          214 :     info.in.ub.policy = JettyGrpPolicy::JETTY_GRP_POLICY_RR;
     361          214 :     info.in.ub.type = TargetType::TARGET_TYPE_JETTY;
     362              : 
     363          214 :     info.in.ub.flag.value = 0;
     364          214 :     info.in.ub.flag.bs.tokenPolicy = TOKEN_POLICY_PLAIN_TEXT;
     365              : 
     366          214 :     info.in.ub.expImportCfg = cfg;
     367              :     // tp_type: 0->RTP, 1->CTP
     368          214 :     info.in.ub.tpType = protocol == TpProtocol::RTP ? 0 : 1;
     369              : 
     370          214 :     void* remQpHandle = nullptr;
     371          214 :     int32_t ret = RaCtxQpImport(ctxHandle, &info, &remQpHandle);
     372          214 :     if (ret != 0) {
     373            0 :         HCCL_ERROR(
     374              :             "[%s] failed, ctxHandle[%p] loc tp handle[%llx] "
     375              :             "rmt tp handle[%llx] loc tag[%llu] loc psn[%u] rmt psn[%u]"
     376              :             "protocol[%s].",
     377              :             __func__, ctxHandle, cfg.tpHandle, cfg.peerTpHandle, cfg.tag, cfg.txPsn, cfg.rxPsn,
     378              :             protocol.Describe().c_str());
     379            0 :         return HcclResult::HCCL_E_NETWORK;
     380              :     }
     381              : 
     382          214 :     out.handle = reinterpret_cast<TargetJettyHandle>(remQpHandle);
     383          214 :     out.targetJettyVa = info.out.ub.tjettyHandle;
     384          214 :     out.tpn = info.out.ub.tpn;
     385          214 :     info.in.ub.tokenValue = 0; // 清理栈中的敏感信息
     386          214 :     return HcclResult::HCCL_SUCCESS;
     387              : }
     388              : 
     389          214 : static struct JettyImportExpCfg GetTpImportCfg(const JettyImportCfg& jettyImportCfg)
     390              : {
     391          214 :     struct JettyImportExpCfg cfg = {};
     392              : 
     393          214 :     cfg.tpHandle = jettyImportCfg.localTpHandle;
     394          214 :     cfg.peerTpHandle = jettyImportCfg.remoteTpHandle;
     395          214 :     cfg.tag = jettyImportCfg.localTag;
     396          214 :     cfg.txPsn = jettyImportCfg.localPsn;
     397          214 :     cfg.rxPsn = jettyImportCfg.remotePsn;
     398              : 
     399          214 :     return cfg;
     400              : }
     401              : 
     402          214 : HcclResult HccpUbTpImportJetty(
     403              :     const CtxHandle ctxHandle, u8* key, const u32 keyLen, const u32 tokenValue, const JettyImportCfg& jettyImportCfg,
     404              :     HrtRaUbJettyImportedOutParam& out)
     405              : {
     406          214 :     struct JettyImportExpCfg cfg = GetTpImportCfg(jettyImportCfg);
     407          214 :     const auto mode = JettyImportMode::JETTY_IMPORT_MODE_EXP;
     408          428 :     return ImportJetty(ctxHandle, key, keyLen, tokenValue, cfg, mode, jettyImportCfg.protocol, out);
     409              : }
     410              : 
     411            0 : static HcclResult ImportJettyAsync(
     412              :     CtxHandle ctxHandle, const HccpUbJettyImportedInParam& in, std::vector<char>& out, void*& remQpHandle,
     413              :     const JettyImportExpCfg& cfg, JettyImportMode mode, TpProtocol protocol, RequestHandle& reqHandle)
     414              : {
     415            0 :     if (mode == JettyImportMode::JETTY_IMPORT_MODE_NORMAL) {
     416            0 :         HCCL_ERROR("[%s] currently not support JETTY_IMPORT_MODE_NORMAL.", __func__);
     417            0 :         return HcclResult::HCCL_E_NOT_SUPPORT;
     418              :     }
     419              : 
     420            0 :     out.resize(sizeof(QpImportInfoT));
     421            0 :     struct QpImportInfoT* info = reinterpret_cast<QpImportInfoT*>(out.data());
     422              : 
     423            0 :     s32 ret = memcpy_s(info->in.key.value, sizeof(info->in.key.value), in.key, in.keyLen);
     424            0 :     if (ret != 0) {
     425            0 :         HCCL_ERROR("[%s] memcpy_s failed, ret=%d.", __func__, ret);
     426            0 :         return HcclResult::HCCL_E_MEMORY;
     427              :     }
     428              : 
     429            0 :     info->in.key.size = in.keyLen;
     430            0 :     info->in.ub.mode = mode;
     431            0 :     info->in.ub.tokenValue = in.tokenValue;
     432            0 :     info->in.ub.policy = JettyGrpPolicy::JETTY_GRP_POLICY_RR;
     433            0 :     info->in.ub.type = TargetType::TARGET_TYPE_JETTY;
     434              : 
     435            0 :     info->in.ub.flag.value = 0;
     436            0 :     info->in.ub.flag.bs.tokenPolicy = TOKEN_POLICY_PLAIN_TEXT;
     437              : 
     438            0 :     info->in.ub.expImportCfg = cfg;
     439              : 
     440            0 :     if (protocol != TpProtocol::RTP && protocol != TpProtocol::CTP) {
     441            0 :         HCCL_ERROR("[%s] failed, tp protocol[%s] is not expected.", __func__, protocol.Describe().c_str());
     442            0 :         return HcclResult::HCCL_E_PARA;
     443              :     }
     444              :     // tp_type: 0->RTP, 1->CTP
     445            0 :     info->in.ub.tpType = protocol == TpProtocol::RTP ? 0 : 1;
     446              : 
     447            0 :     void* raReqHandle = nullptr;
     448            0 :     ret = RaCtxQpImportAsync(ctxHandle, info, &remQpHandle, &raReqHandle);
     449            0 :     if (ret != 0 || !raReqHandle) {
     450            0 :         HCCL_ERROR(
     451              :             "[%s] failed, call interface error[%d] raReqHandle[%p], "
     452              :             "ctxHandle[%p].",
     453              :             __func__, ret, raReqHandle, ctxHandle);
     454            0 :         return HcclResult::HCCL_E_NETWORK;
     455              :     }
     456            0 :     info->in.ub.tokenValue = 0;
     457            0 :     HCCL_INFO("[%s] ok, get handle[%llu]", __func__, reinterpret_cast<RequestHandle>(raReqHandle));
     458            0 :     reqHandle = reinterpret_cast<RequestHandle>(raReqHandle);
     459            0 :     return HcclResult::HCCL_SUCCESS;
     460              : }
     461              : 
     462            0 : HcclResult HccpUbTpImportJettyAsync(
     463              :     const CtxHandle ctxHandle, const HccpUbJettyImportedInParam& in, std::vector<char>& out, void*& remQpHandle,
     464              :     RequestHandle& reqHandle)
     465              : {
     466            0 :     struct JettyImportExpCfg cfg = GetTpImportCfg(in.jettyImportCfg);
     467            0 :     const auto mode = JettyImportMode::JETTY_IMPORT_MODE_EXP;
     468            0 :     return ImportJettyAsync(ctxHandle, in, out, remQpHandle, cfg, mode, in.jettyImportCfg.protocol, reqHandle);
     469              : }
     470              : 
     471        28326 : HcclResult HccpRaTlvCcuCustomChannel(int32_t devLogicId, void* customIn, void* customOut)
     472              : {
     473              :     // 当前复用legacy流程单例,后续需整改
     474              :     EXCEPTION_HANDLE_BEGIN
     475        28326 :     auto tlvHandle = Hccl::HccpTlvHdcManager::GetInstance().GetTlvHandle(devLogicId);
     476        28326 :     CHK_RET(HccpRaTlvRequestForCustomChannel(tlvHandle, MSG_TYPE_CCU_DISPATCH_CMD, customIn, customOut));
     477            0 :     EXCEPTION_HANDLE_END
     478        28326 :     return HcclResult::HCCL_SUCCESS;
     479              : }
     480              : 
     481        28354 : HcclResult HccpRaTlvRequestForCustomChannel(void* tlvHandle, unsigned int msgType, void* customIn, void* customOut)
     482              : {
     483        28354 :     CHK_PTR_NULL(tlvHandle);
     484        28353 :     CHK_PTR_NULL(customIn);
     485        28352 :     CHK_PTR_NULL(customOut);
     486              : 
     487        28351 :     struct TlvMsg sendMsg {};
     488        28351 :     sendMsg.type = msgType;
     489        28351 :     sendMsg.length = sizeof(CustomChanInfoIn);
     490        28351 :     sendMsg.data = static_cast<char*>(customIn);
     491              : 
     492        28351 :     struct TlvMsg recvMsg {};
     493        28351 :     recvMsg.type = msgType;
     494        28351 :     recvMsg.length = sizeof(CustomChanInfoOut);
     495        28351 :     recvMsg.data = static_cast<char*>(customOut);
     496              : 
     497        28351 :     int ret = RaTlvRequest(tlvHandle, TLV_MODULE_TYPE_CCU, &sendMsg, &recvMsg);
     498        28351 :     if (ret != 0) {
     499            1 :         HCCL_ERROR("[%s] RaTlvRequest fail, tlvHandle[%p], ret[%d]", __func__, tlvHandle, ret);
     500            1 :         return HCCL_E_NETWORK;
     501              :     }
     502        28350 :     return HCCL_SUCCESS;
     503              : }
     504              : 
     505            3 : HcclResult HccpBatchQueryJettyStatus(
     506              :     const CtxHandle ctxHandle, const std::vector<JettyHandle>& jettyHandles, std::vector<JettyStatus>& jettyAttrs,
     507              :     u32& num)
     508              : {
     509            3 :     CHK_PTR_NULL(ctxHandle);
     510            3 :     if (jettyHandles.size() != num) {
     511            1 :         HCCL_ERROR("jettyHandles size[%zu] not equal to num[%u]", jettyHandles.size(), num);
     512            1 :         return HCCL_E_PARA;
     513              :     }
     514            4 :     std::vector<struct JettyAttr> raJettyAttrs(MAX_JETTY_QUERY_NUM);
     515            2 :     std::vector<void*> qp_handle(jettyHandles.size());
     516            3 :     for (size_t i = 0; i < jettyHandles.size(); ++i) {
     517            1 :         qp_handle[i] = reinterpret_cast<void*>(jettyHandles[i]);
     518              :     }
     519            2 :     auto ret = RaCtxQpQueryBatch(qp_handle.data(), raJettyAttrs.data(), &num);
     520            2 :     if (ret != 0) {
     521            0 :         HCCL_ERROR("RaBatchQueryJettyAttr failed.ret[%d]", ret);
     522            0 :         return HCCL_E_NETWORK;
     523              :     }
     524            2 :     if (num != jettyHandles.size()) {
     525            0 :         HCCL_ERROR("jettyAttrs num[%zu] not equal to input jettyHandles size[%zu]", num, jettyHandles.size());
     526            0 :         return HCCL_E_PARA;
     527              :     }
     528              : 
     529            3 :     for (u32 i = 0; i < num; i++) {
     530            1 :         JettyStatus jettyStatus = static_cast<JettyStatus::Value>(static_cast<int>(raJettyAttrs[i].state));
     531            1 :         jettyAttrs.push_back(jettyStatus);
     532              :     }
     533            2 :     return HCCL_SUCCESS;
     534            2 : }
     535              : 
     536            3 : HcclResult HccpGetUboeFlagEnable(const u32 devPhyId)
     537              : {
     538            3 :     u32 uboeVersion = 0;
     539            3 :     s32 versionRet = RaGetInterfaceVersion(devPhyId, GET_UBOE_FLAG_ENABLE_OPCODE, &uboeVersion);
     540            3 :     CHK_PRT_RET(
     541              :         versionRet != 0,
     542              :         HCCL_ERROR("[%s] RaGetInterfaceVersion failed, devPhyId=%u, versionRet=%d", __func__, devPhyId, versionRet),
     543              :         HCCL_E_INTERNAL);
     544            2 :     CHK_PRT_RET(
     545              :         uboeVersion < GET_UBOE_FLAG_ENABLE_VERSION,
     546              :         HCCL_ERROR(
     547              :             "[%s] this package does not support to get uboe flag, "
     548              :             "please change new package. uboeVersion[%u].",
     549              :             __func__, uboeVersion),
     550              :         HCCL_E_NOT_SUPPORT);
     551            1 :     return HCCL_SUCCESS;
     552              : }
     553              : 
     554            2 : HcclResult HccpGetIpByEid(void* ctxHandle, const CommAddr& eidAddr, CommAddr& ipAddr)
     555              : {
     556            2 :     ipAddr = {};
     557            2 :     ipAddr.type = COMM_ADDR_TYPE_RESERVED;
     558            2 :     CHK_PTR_NULL(ctxHandle);
     559            2 :     CHK_PRT_RET(
     560              :         eidAddr.type != COMM_ADDR_TYPE_EID,
     561              :         HCCL_ERROR("[%s] invalid address type[%d], expected COMM_ADDR_TYPE_EID.", __func__, eidAddr.type), HCCL_E_PARA);
     562              : 
     563            2 :     union HccpEid hccpEid {};
     564            2 :     CHK_SAFETY_FUNC_RET(memcpy_s(hccpEid.raw, sizeof(hccpEid.raw), eidAddr.eid, sizeof(eidAddr.eid)));
     565              : 
     566            2 :     struct IpInfo ipInfo {};
     567            2 :     uint32_t num = 1U;
     568            2 :     const int32_t ret = RaGetIpByEid(ctxHandle, &hccpEid, &ipInfo, &num);
     569            2 :     CHK_PRT_RET(
     570              :         ret != 0, HCCL_ERROR("[%s] RaGetIpByEid failed, ctxHandle[%p], ret[%d].", __func__, ctxHandle, ret),
     571              :         HCCL_E_NETWORK);
     572            1 :     CHK_PRT_RET(
     573              :         num == 0, HCCL_ERROR("[%s] RaGetIpByEid returned no IPv4 address, ctxHandle[%p].", __func__, ctxHandle),
     574              :         HCCL_E_NOT_FOUND);
     575            1 :     CHK_PRT_RET(
     576              :         num != 1U,
     577              :         HCCL_ERROR("[%s] RaGetIpByEid returned unexpected address count[%u], ctxHandle[%p].", __func__, num, ctxHandle),
     578              :         HCCL_E_INTERNAL);
     579            1 :     CHK_PRT_RET(
     580              :         ipInfo.family != AF_INET,
     581              :         HCCL_ERROR(
     582              :             "[%s] RaGetIpByEid returned unsupported address family[%d], expected AF_INET.", __func__, ipInfo.family),
     583              :         HCCL_E_NOT_SUPPORT);
     584              : 
     585            1 :     ipAddr.type = COMM_ADDR_TYPE_IP_V4;
     586            1 :     ipAddr.addr = ipInfo.ip.addr;
     587            1 :     HCCL_INFO(
     588              :         "[%s] query UBoE IPv4 success, ctxHandle[%p], IPv4[0x%08x].", __func__, ctxHandle, ntohl(ipAddr.addr.s_addr));
     589            1 :     return HCCL_SUCCESS;
     590              : }
     591              : 
     592           15 : HcclResult HccpRaGetDevBaseAttr(void* ctxHandle, struct DevBaseAttr* attr)
     593              : {
     594           15 :     int ret = RaGetDevBaseAttr(ctxHandle, attr);
     595           15 :     if (ret != 0) {
     596            1 :         HCCL_ERROR("[%s] RaGetDevBaseAttr failed, ctxHandle[%p], attr[%p], ret[%d]", __func__, ctxHandle, attr, ret);
     597            1 :         return HCCL_E_NETWORK;
     598              :     }
     599           14 :     HCCL_INFO(
     600              :         "HccpRaGetDevBaseAttr success, sqMaxDepth[%u], rqMaxDepth[%u], sqMaxSge[%u], rqMaxSge[%u], maxReadSize[%u], "
     601              :         "maxWriteSize[%u]",
     602              :         attr->sqMaxDepth, attr->rqMaxDepth, attr->sqMaxSge, attr->rqMaxSge, attr->maxReadSize, attr->maxWriteSize);
     603           14 :     return HCCL_SUCCESS;
     604              : }
     605              : 
     606            4 : HcclResult HccpGetCtpEnable(void* ctxHandle, bool& ctpEnable)
     607              : {
     608            4 :     ctpEnable = false;
     609            4 :     CHK_PTR_NULL(ctxHandle);
     610            3 :     DevBaseAttr attr{};
     611            3 :     CHK_RET(HccpRaGetDevBaseAttr(ctxHandle, &attr));
     612              : 
     613           19 :     for (uint32_t i = 0; i < MAX_PRIORITY_CNT; ++i) {
     614           18 :         if (attr.ub.priorityInfo[i].tpType.bs.ctp == 1) {
     615            1 :             ctpEnable = true;
     616            1 :             break;
     617              :         }
     618              :     }
     619            2 :     HCCL_INFO("[%s] ctxHandle[%p], ctpEnable[%d].", __func__, ctxHandle, ctpEnable);
     620            2 :     return HCCL_SUCCESS;
     621              : }
     622              : 
     623            5 : HcclResult HrtRaDumpJettyContext(JettyHandle jettyHandle, u32 jettyId)
     624              : {
     625            5 :     CHK_PTR_NULL(jettyHandle);
     626              : 
     627            4 :     uint8_t context[CONTEXT_MAX_LEN] = {0};
     628            4 :     unsigned int len = CONTEXT_MAX_LEN;
     629            4 :     int ret = RaCtxGetJettyContext(static_cast<void*>(jettyHandle), context, &len);
     630              : 
     631            4 :     CHK_PRT_RET(
     632              :         ret != 0,
     633              :         HCCL_ERROR(
     634              :             "[HrtRaDumpJettyContext] RaCtxGetJettyContext failed, "
     635              :             "jettyId[%u], ret=%d",
     636              :             jettyId, ret),
     637              :         HCCL_E_INTERNAL);
     638              : 
     639            3 :     CHK_PRT_RET(
     640              :         len == 0 || len > CONTEXT_MAX_LEN,
     641              :         HCCL_ERROR("[HrtRaDumpJettyContext] invalid context len=%u, jettyId[%u]", len, jettyId), HCCL_E_INTERNAL);
     642              : 
     643            1 :     constexpr u32 bytesPerLine = 64;
     644            5 :     for (u32 offset = 0; offset < len; offset += bytesPerLine) {
     645            4 :         u32 bytesThisLine = std::min(len - offset, bytesPerLine);
     646              : 
     647            4 :         char hexBuf[bytesPerLine * 2 + 1] = {0};
     648          260 :         for (u32 i = 0; i < bytesThisLine; i++) {
     649          256 :             int sret = snprintf_s(hexBuf + i * 2, sizeof(hexBuf) - i * 2, 2U, "%02x", context[offset + i]);
     650          256 :             CHK_PRT_RET(
     651              :                 sret <= 0,
     652              :                 HCCL_ERROR(
     653              :                     "[HrtRaDumpJettyContext] snprintf_s failed, dest[%p], destMax[%zu], count[%u], "
     654              :                     "contextVal[%u], ret[%d].",
     655              :                     static_cast<void*>(hexBuf + i * 2), sizeof(hexBuf) - i * 2, 2U, context[offset + i], sret),
     656              :                 HCCL_E_INTERNAL);
     657              :         }
     658              : 
     659            4 :         HCCL_ERROR("[HrtRaDumpJettyContext] jettyId=%u, len=%u, JettyContext:%s", jettyId, len, hexBuf);
     660              :     }
     661            1 :     return HCCL_SUCCESS;
     662              : }
     663              : 
     664              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1