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: 64.5 % 344 222
Test Date: 2026-08-18 17:47:01 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          476 : HcclResult IpAddressToHccpEid(const Hccl::IpAddress& ipAddr, Eid& eid)
      32              : {
      33          476 :     HCCL_INFO("EID ipAddr[%s]", ipAddr.Describe().c_str());
      34          476 :     int32_t sRet = memcpy_s(eid.raw, sizeof(eid.raw), ipAddr.GetEid().raw, sizeof(ipAddr.GetEid().raw));
      35          476 :     if (sRet != EOK) {
      36            0 :         HCCL_ERROR("[%s] memcpy failed[%d].", __func__, sRet);
      37            0 :         return HcclResult::HCCL_E_MEMORY;
      38              :     }
      39          476 :     HCCL_INFO(
      40              :         "[IpAddressToHccpEid] hccpEid.in6.subnetPrefix[%016llx], hccpEid.in6.interfaceId[%016llx]",
      41              :         eid.in6.subnetPrefix, eid.in6.interfaceId);
      42          476 :     return HcclResult::HCCL_SUCCESS;
      43              : }
      44              : 
      45          200 : HcclResult IpAddressToReverseHcclEid(const Hccl::IpAddress& ipAddr, Hccl::Eid& eid)
      46              : {
      47          200 :     HCCL_INFO("EID ipAddr[%s]", ipAddr.Describe().c_str());
      48          200 :     int32_t sRet = memcpy_s(eid.raw, sizeof(eid.raw), ipAddr.GetReverseEid().raw, sizeof(ipAddr.GetReverseEid().raw));
      49          200 :     if (sRet != EOK) {
      50            0 :         HCCL_ERROR("[%s] memcpy failed[%d].", __func__, sRet);
      51            0 :         return HcclResult::HCCL_E_MEMORY;
      52              :     }
      53          200 :     HCCL_INFO(
      54              :         "[IpAddressToHccpEid] hccpEid.in6.subnetPrefix[%016llx], hccpEid.in6.interfaceId[%016llx]",
      55              :         eid.in6.subnetPrefix, eid.in6.interfaceId);
      56          200 :     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          552 : RequestResult HccpGetAsyncReqResult(RequestHandle& reqHandle)
     106              : {
     107          552 :     if (reqHandle == 0) {
     108            0 :         HCCL_ERROR("[%s] failed, reqHandle is 0.", __func__);
     109            0 :         return RequestResult::INVALID_PARA;
     110              :     }
     111              : 
     112          552 :     int reqResult = 0;
     113          552 :     int32_t ret = RaGetAsyncReqResult(reinterpret_cast<void*>(reqHandle), &reqResult);
     114              :     // 返回 OTHERS_EAGAIN 代表查询到异步任务未完成,需要重新查询,此时保留handle
     115          552 :     if (ret == OTHERS_EAGAIN) {
     116            0 :         return RequestResult::NOT_COMPLETED;
     117              :     }
     118              : 
     119              :     // 返回码非0代表调用查询接口失败,当前仅入参错误时触发
     120          552 :     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          552 :     RequestHandle tmpReqHandle = reqHandle;
     129              :     // 返回码为 0 时,reqResult为异步任务完成结果,0代表成功,其他值代表失败
     130              :     // SOCK_EAGAIN 为 socket 类执行结果,代表 socket 接口失败需要重试
     131          552 :     if (reqResult == SOCK_EAGAIN) {
     132            0 :         return RequestResult::SOCK_E_AGAIN;
     133              :     }
     134              : 
     135          552 :     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          552 :     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          200 : HccpUbCreateJetty(const CtxHandle ctxHandle, const HrtRaUbCreateJettyParam& in, HrtRaUbJettyCreatedOutParam& out)
     162              : {
     163          200 :     struct QpCreateAttr attr {};
     164          200 :     attr.scqHandle = reinterpret_cast<void*>(in.sjfcHandle);
     165          200 :     attr.rcqHandle = reinterpret_cast<void*>(in.rjfcHandle);
     166          200 :     attr.srqHandle = reinterpret_cast<void*>(in.sjfcHandle);
     167          200 :     attr.rqDepth = RQ_DEPTH;
     168          200 :     attr.sqDepth = in.sqDepth;
     169          200 :     attr.transportMode = HRT_TRANSPORT_MODE_MAP.at(in.transMode);
     170          200 :     attr.ub.mode = HRT_JETTY_MODE_MAP.at(in.jettyMode);
     171              : 
     172          200 :     attr.ub.tokenValue = in.tokenValue;
     173          200 :     attr.ub.tokenIdHandle = reinterpret_cast<void*>(in.tokenIdHandle);
     174          200 :     attr.ub.flag.value = 0;
     175              :     /* errTime配置值:0-31
     176              :        0-7代表芯片配置值b00:512ms
     177              :        8-15代表芯片配置值b01:1s
     178              :        16-23代表芯片配置值b10:8s
     179              :        24-31代表芯片配置值b11:32s
     180              :     */
     181          200 :     attr.ub.errTimeout = in.errTimeout;
     182          200 :     attr.ub.priority = static_cast<uint8_t>(in.qos & 0xFU);
     183          200 :     attr.ub.rnrRetry = RNR_RETRY;
     184          200 :     attr.ub.flag.bs.shareJfr = 1;
     185          200 :     attr.ub.jettyId = in.jettyId;
     186              :     // 在continue模式下+配置了wqe的fence标记,并且远端有一些权限校验错误/内存异常错误,硬件会直接挂死
     187              :     // jfs_flag 的 error_suspend 设置为 1,
     188          200 :     attr.ub.jfsFlag.bs.errorSuspend = 1;
     189              : 
     190          200 :     if (in.jettyMode == HrtJettyMode::CCU_TA_CACHE) {
     191           30 :         attr.ub.tokenValue = in.tokenValue;
     192           30 :         attr.ub.taCacheMode.lockFlag = true;
     193           30 :         attr.ub.taCacheMode.sqeBufIdx = in.sqeBufIndex;
     194              :     } else {
     195          170 :         attr.ub.extMode.sqebbNum = in.sqDepth;
     196              :     }
     197              : 
     198          200 :     if (in.jettyMode == HrtJettyMode::HOST_OFFLOAD) {
     199            0 :         attr.ub.extMode.piType = 1;
     200          200 :     } else if (in.jettyMode == HrtJettyMode::CCU_CCUM_CACHE) {
     201          170 :         attr.ub.tokenValue = in.tokenValue;
     202          170 :         attr.ub.extMode.cstmFlag.bs.sqCstm = 1;
     203          170 :         attr.ub.extMode.sq.buffSize = in.sqBufSize;
     204          170 :         attr.ub.extMode.sq.buffVa = in.sqBufVa;
     205           30 :     } 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          200 :     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          200 :     struct QpCreateInfo info {};
     223          200 :     void* qpHandle = nullptr;
     224          200 :     int32_t ret = RaCtxQpCreate(ctxHandle, &attr, &info, &qpHandle);
     225          200 :     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          200 :     constexpr u32 URMA_TOKEN_ID_RIGHT_SHIFT = 8;
     236              : 
     237          200 :     out.handle = reinterpret_cast<JettyHandle>(qpHandle);
     238          200 :     out.id = info.ub.id;
     239          200 :     out.uasid = info.ub.uasid;
     240          200 :     out.jettyVa = info.va;
     241          200 :     out.dbVa = info.ub.dbAddr;
     242          200 :     out.dbTokenId = info.ub.dbTokenId >> URMA_TOKEN_ID_RIGHT_SHIFT;
     243              : 
     244          200 :     int32_t sRet = memcpy_s(out.key, sizeof(out.key), info.key.value, info.key.size);
     245          200 :     if (sRet != 0) {
     246            0 :         HCCL_ERROR("[%s] failed, memcpy failed[%d].", __func__, sRet);
     247            0 :         return HcclResult::HCCL_E_MEMORY;
     248              :     }
     249          200 :     out.keySize = info.key.size;
     250          200 :     attr.ub.tokenValue = 0; // 清理栈中的敏感信息
     251          200 :     HCCL_INFO("[%s], output params: out.id[%u], out.dbVa[%llx]", __func__, out.id, out.dbVa);
     252              : 
     253          200 :     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:128ms
     274              :        8-15代表芯片配置值b01:1s
     275              :        16-23代表芯片配置值b10:8s
     276              :        24-31代表芯片配置值b11:64s
     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          200 : 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          200 :     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          200 :     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          200 :     struct QpImportInfoT info {};
     351          200 :     int res = memcpy_s(info.in.key.value, sizeof(info.in.key.value), key, keyLen);
     352          200 :     if (res != 0) {
     353            0 :         HCCL_ERROR("[%s] memcpy_s failed, ret = %d", __func__, res);
     354            0 :         return HcclResult::HCCL_E_MEMORY;
     355              :     }
     356          200 :     info.in.key.size = keyLen;
     357              : 
     358          200 :     info.in.ub.mode = mode;
     359          200 :     info.in.ub.tokenValue = tokenValue;
     360          200 :     info.in.ub.policy = JettyGrpPolicy::JETTY_GRP_POLICY_RR;
     361          200 :     info.in.ub.type = TargetType::TARGET_TYPE_JETTY;
     362              : 
     363          200 :     info.in.ub.flag.value = 0;
     364          200 :     info.in.ub.flag.bs.tokenPolicy = TOKEN_POLICY_PLAIN_TEXT;
     365              : 
     366          200 :     info.in.ub.expImportCfg = cfg;
     367              :     // tp_type: 0->RTP, 1->CTP
     368          200 :     info.in.ub.tpType = protocol == TpProtocol::RTP ? 0 : 1;
     369              : 
     370          200 :     void* remQpHandle = nullptr;
     371          200 :     int32_t ret = RaCtxQpImport(ctxHandle, &info, &remQpHandle);
     372          200 :     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          200 :     out.handle = reinterpret_cast<TargetJettyHandle>(remQpHandle);
     383          200 :     out.targetJettyVa = info.out.ub.tjettyHandle;
     384          200 :     out.tpn = info.out.ub.tpn;
     385          200 :     info.in.ub.tokenValue = 0; // 清理栈中的敏感信息
     386          200 :     return HcclResult::HCCL_SUCCESS;
     387              : }
     388              : 
     389          200 : static struct JettyImportExpCfg GetTpImportCfg(const JettyImportCfg& jettyImportCfg)
     390              : {
     391          200 :     struct JettyImportExpCfg cfg = {};
     392              : 
     393          200 :     cfg.tpHandle = jettyImportCfg.localTpHandle;
     394          200 :     cfg.peerTpHandle = jettyImportCfg.remoteTpHandle;
     395          200 :     cfg.tag = jettyImportCfg.localTag;
     396          200 :     cfg.txPsn = jettyImportCfg.localPsn;
     397          200 :     cfg.rxPsn = jettyImportCfg.remotePsn;
     398              : 
     399          200 :     return cfg;
     400              : }
     401              : 
     402          200 : HcclResult HccpUbTpImportJetty(
     403              :     const CtxHandle ctxHandle, u8* key, const u32 keyLen, const u32 tokenValue, const JettyImportCfg& jettyImportCfg,
     404              :     HrtRaUbJettyImportedOutParam& out)
     405              : {
     406          200 :     struct JettyImportExpCfg cfg = GetTpImportCfg(jettyImportCfg);
     407          200 :     const auto mode = JettyImportMode::JETTY_IMPORT_MODE_EXP;
     408          400 :     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          875 : HcclResult HccpRaTlvCcuCustomChannel(int32_t devLogicId, void* customIn, void* customOut)
     472              : {
     473              :     // 当前复用legacy流程单例,后续需整改
     474              :     EXCEPTION_HANDLE_BEGIN
     475          875 :     auto tlvHandle = Hccl::HccpTlvHdcManager::GetInstance().GetTlvHandle(devLogicId);
     476          875 :     CHK_RET(HccpRaTlvRequestForCustomChannel(tlvHandle, MSG_TYPE_CCU_DISPATCH_CMD, customIn, customOut));
     477            0 :     EXCEPTION_HANDLE_END
     478          875 :     return HcclResult::HCCL_SUCCESS;
     479              : }
     480              : 
     481          903 : HcclResult HccpRaTlvRequestForCustomChannel(void* tlvHandle, unsigned int msgType, void* customIn, void* customOut)
     482              : {
     483          903 :     CHK_PTR_NULL(tlvHandle);
     484          902 :     CHK_PTR_NULL(customIn);
     485          901 :     CHK_PTR_NULL(customOut);
     486              : 
     487          900 :     struct TlvMsg sendMsg {};
     488          900 :     sendMsg.type = msgType;
     489          900 :     sendMsg.length = sizeof(CustomChanInfoIn);
     490          900 :     sendMsg.data = static_cast<char*>(customIn);
     491              : 
     492          900 :     struct TlvMsg recvMsg {};
     493          900 :     recvMsg.type = msgType;
     494          900 :     recvMsg.length = sizeof(CustomChanInfoOut);
     495          900 :     recvMsg.data = static_cast<char*>(customOut);
     496              : 
     497          900 :     int ret = RaTlvRequest(tlvHandle, TLV_MODULE_TYPE_CCU, &sendMsg, &recvMsg);
     498          900 :     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          899 :     return HCCL_SUCCESS;
     503              : }
     504              : 
     505              : HcclResult
     506            2 : RaBatchQueryJettyStatus(const std::vector<JettyHandle>& jettyHandles, std::vector<JettyStatus>& jettyAttrs, u32& num)
     507              : {
     508            2 :     if (jettyHandles.size() != num) {
     509            1 :         HCCL_ERROR("jettyHandles size[%zu] not equal to num[%u]", jettyHandles.size(), num);
     510            1 :         return HCCL_E_PARA;
     511              :     }
     512            2 :     std::vector<struct JettyAttr> raJettyAttrs(MAX_JETTY_QUERY_NUM);
     513            1 :     std::vector<void*> qp_handle(jettyHandles.size());
     514            1 :     for (size_t i = 0; i < jettyHandles.size(); ++i) {
     515            0 :         qp_handle[i] = reinterpret_cast<void*>(jettyHandles[i]);
     516              :     }
     517            1 :     auto ret = RaCtxQpQueryBatch(qp_handle.data(), raJettyAttrs.data(), &num);
     518            1 :     if (ret != 0) {
     519            0 :         HCCL_ERROR("RaBatchQueryJettyAttr failed.ret[%d]", ret);
     520            0 :         return HCCL_E_NETWORK;
     521              :     }
     522            1 :     if (num != jettyHandles.size()) {
     523            0 :         HCCL_ERROR("jettyAttrs num[%zu] not equal to input jettyHandles size[%zu]", num, jettyHandles.size());
     524            0 :         return HCCL_E_PARA;
     525              :     }
     526              : 
     527            1 :     for (u32 i = 0; i < num; i++) {
     528            0 :         JettyStatus jettyStatus = static_cast<JettyStatus::Value>(static_cast<int>(raJettyAttrs[i].state));
     529            0 :         jettyAttrs.push_back(jettyStatus);
     530              :     }
     531            1 :     return HCCL_SUCCESS;
     532            1 : }
     533              : 
     534            3 : HcclResult HccpGetUboeFlagEnable(const u32 devPhyId)
     535              : {
     536            3 :     u32 uboeVersion = 0;
     537            3 :     s32 versionRet = RaGetInterfaceVersion(devPhyId, GET_UBOE_FLAG_ENABLE_OPCODE, &uboeVersion);
     538            3 :     CHK_PRT_RET(
     539              :         versionRet != 0,
     540              :         HCCL_ERROR("[%s] RaGetInterfaceVersion failed, devPhyId=%u, versionRet=%d", __func__, devPhyId, versionRet),
     541              :         HCCL_E_INTERNAL);
     542            2 :     CHK_PRT_RET(
     543              :         uboeVersion < GET_UBOE_FLAG_ENABLE_VERSION,
     544              :         HCCL_ERROR(
     545              :             "[%s] this package does not support to get uboe flag, "
     546              :             "please change new package. uboeVersion[%u].",
     547              :             __func__, uboeVersion),
     548              :         HCCL_E_NOT_SUPPORT);
     549            1 :     return HCCL_SUCCESS;
     550              : }
     551              : 
     552            2 : HcclResult HccpGetIpByEid(void* ctxHandle, const CommAddr& eidAddr, CommAddr& ipAddr)
     553              : {
     554            2 :     ipAddr = {};
     555            2 :     ipAddr.type = COMM_ADDR_TYPE_RESERVED;
     556            2 :     CHK_PTR_NULL(ctxHandle);
     557            2 :     CHK_PRT_RET(
     558              :         eidAddr.type != COMM_ADDR_TYPE_EID,
     559              :         HCCL_ERROR("[%s] invalid address type[%d], expected COMM_ADDR_TYPE_EID.", __func__, eidAddr.type), HCCL_E_PARA);
     560              : 
     561            2 :     union HccpEid hccpEid {};
     562            2 :     CHK_SAFETY_FUNC_RET(memcpy_s(hccpEid.raw, sizeof(hccpEid.raw), eidAddr.eid, sizeof(eidAddr.eid)));
     563              : 
     564            2 :     struct IpInfo ipInfo {};
     565            2 :     uint32_t num = 1U;
     566            2 :     const int32_t ret = RaGetIpByEid(ctxHandle, &hccpEid, &ipInfo, &num);
     567            2 :     CHK_PRT_RET(
     568              :         ret != 0, HCCL_ERROR("[%s] RaGetIpByEid failed, ctxHandle[%p], ret[%d].", __func__, ctxHandle, ret),
     569              :         HCCL_E_NETWORK);
     570            1 :     CHK_PRT_RET(
     571              :         num == 0, HCCL_ERROR("[%s] RaGetIpByEid returned no IPv4 address, ctxHandle[%p].", __func__, ctxHandle),
     572              :         HCCL_E_NOT_FOUND);
     573            1 :     CHK_PRT_RET(
     574              :         num != 1U,
     575              :         HCCL_ERROR("[%s] RaGetIpByEid returned unexpected address count[%u], ctxHandle[%p].", __func__, num, ctxHandle),
     576              :         HCCL_E_INTERNAL);
     577            1 :     CHK_PRT_RET(
     578              :         ipInfo.family != AF_INET,
     579              :         HCCL_ERROR(
     580              :             "[%s] RaGetIpByEid returned unsupported address family[%d], expected AF_INET.", __func__, ipInfo.family),
     581              :         HCCL_E_NOT_SUPPORT);
     582              : 
     583            1 :     ipAddr.type = COMM_ADDR_TYPE_IP_V4;
     584            1 :     ipAddr.addr = ipInfo.ip.addr;
     585            1 :     HCCL_INFO(
     586              :         "[%s] query UBoE IPv4 success, ctxHandle[%p], IPv4[0x%08x].", __func__, ctxHandle, ntohl(ipAddr.addr.s_addr));
     587            1 :     return HCCL_SUCCESS;
     588              : }
     589              : 
     590           15 : HcclResult HccpRaGetDevBaseAttr(void* ctxHandle, struct DevBaseAttr* attr)
     591              : {
     592           15 :     int ret = RaGetDevBaseAttr(ctxHandle, attr);
     593           15 :     if (ret != 0) {
     594            1 :         HCCL_ERROR("[%s] RaGetDevBaseAttr failed, ctxHandle[%p], attr[%p], ret[%d]", __func__, ctxHandle, attr, ret);
     595            1 :         return HCCL_E_NETWORK;
     596              :     }
     597           14 :     HCCL_INFO(
     598              :         "HccpRaGetDevBaseAttr success, sqMaxDepth[%u], rqMaxDepth[%u], sqMaxSge[%u], rqMaxSge[%u], maxReadSize[%u], "
     599              :         "maxWriteSize[%u]",
     600              :         attr->sqMaxDepth, attr->rqMaxDepth, attr->sqMaxSge, attr->rqMaxSge, attr->maxReadSize, attr->maxWriteSize);
     601           14 :     return HCCL_SUCCESS;
     602              : }
     603              : 
     604            4 : HcclResult HccpGetCtpEnable(void* ctxHandle, bool& ctpEnable)
     605              : {
     606            4 :     ctpEnable = false;
     607            4 :     CHK_PTR_NULL(ctxHandle);
     608            3 :     DevBaseAttr attr{};
     609            3 :     CHK_RET(HccpRaGetDevBaseAttr(ctxHandle, &attr));
     610              : 
     611           19 :     for (uint32_t i = 0; i < MAX_PRIORITY_CNT; ++i) {
     612           18 :         if (attr.ub.priorityInfo[i].tpType.bs.ctp == 1) {
     613            1 :             ctpEnable = true;
     614            1 :             break;
     615              :         }
     616              :     }
     617            2 :     HCCL_INFO("[%s] ctxHandle[%p], ctpEnable[%d].", __func__, ctxHandle, ctpEnable);
     618            2 :     return HCCL_SUCCESS;
     619              : }
     620              : 
     621            5 : HcclResult HrtRaDumpJettyContext(JettyHandle jettyHandle, u32 jettyId)
     622              : {
     623            5 :     CHK_PTR_NULL(jettyHandle);
     624              : 
     625            4 :     uint8_t context[CONTEXT_MAX_LEN] = {0};
     626            4 :     unsigned int len = CONTEXT_MAX_LEN;
     627            4 :     int ret = RaCtxGetJettyContext(static_cast<void*>(jettyHandle), context, &len);
     628              : 
     629            4 :     CHK_PRT_RET(
     630              :         ret != 0,
     631              :         HCCL_ERROR(
     632              :             "[HrtRaDumpJettyContext] RaCtxGetJettyContext failed, "
     633              :             "jettyId[%u], ret=%d",
     634              :             jettyId, ret),
     635              :         HCCL_E_INTERNAL);
     636              : 
     637            3 :     CHK_PRT_RET(
     638              :         len == 0 || len > CONTEXT_MAX_LEN,
     639              :         HCCL_ERROR("[HrtRaDumpJettyContext] invalid context len=%u, jettyId[%u]", len, jettyId), HCCL_E_INTERNAL);
     640              : 
     641            1 :     constexpr u32 bytesPerLine = 64;
     642            5 :     for (u32 offset = 0; offset < len; offset += bytesPerLine) {
     643            4 :         u32 bytesThisLine = std::min(len - offset, bytesPerLine);
     644              : 
     645            4 :         char hexBuf[bytesPerLine * 2 + 1] = {0};
     646          260 :         for (u32 i = 0; i < bytesThisLine; i++) {
     647          256 :             snprintf_s(hexBuf + i * 2, sizeof(hexBuf) - i * 2, 2U, "%02x", context[offset + i]);
     648              :         }
     649              : 
     650            4 :         HCCL_ERROR("[HrtRaDumpJettyContext] jettyId=%u, len=%u, JettyContext:%s", jettyId, len, hexBuf);
     651              :     }
     652            1 :     return HCCL_SUCCESS;
     653              : }
     654              : 
     655              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1