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 "ccu_jetty.h"
12 :
13 : #include "hccp_ctx.h"
14 : #include "rdma_handle_manager.h"
15 : #include "local_ub_rma_buffer.h"
16 :
17 : namespace Hccl {
18 :
19 14 : HcclResult CcuCreateJetty(const IpAddress &ipAddr, const CcuJettyInfo &jettyInfo,
20 : std::unique_ptr<CcuJetty> &ccuJetty)
21 : {
22 14 : TRY_CATCH_RETURN(
23 : ccuJetty = std::make_unique<CcuJetty>(ipAddr, jettyInfo);
24 : );
25 14 : return HcclResult::HCCL_SUCCESS;
26 : }
27 :
28 808 : CcuJetty::CcuJetty(const IpAddress &ipAddr, const CcuJettyInfo &jettyInfo)
29 808 : : ipAddr_(ipAddr), jettyInfo_(jettyInfo)
30 : {
31 808 : devLogicId_ = HrtGetDevice();
32 808 : Hccl::CqCreateInfo cqInfo{0};
33 808 : uint32_t devPhyId = HrtGetDevicePhyIdByIndex(devLogicId_);
34 808 : auto &rdmaHandleMgr = RdmaHandleManager::GetInstance();
35 808 : rdmaHandle_ = rdmaHandleMgr.GetByIp(devPhyId, ipAddr);
36 808 : const auto jfcHandle = rdmaHandleMgr.GetJfcHandle(rdmaHandle_, cqInfo, HrtUbJfcMode::CCU_POLL);
37 808 : const auto &tokenInfo = rdmaHandleMgr.GetTokenIdInfo(rdmaHandle_);
38 808 : const auto tokenIdHandle = tokenInfo.first;
39 808 : const auto tokenValue = GetUbToken();
40 808 : const auto jettyMode = HrtJettyMode::CCU_CCUM_CACHE; // 当前仅支持该模式
41 :
42 808 : inParam_ = HrtRaUbCreateJettyParam{jfcHandle, jfcHandle, tokenValue,
43 808 : tokenIdHandle, jettyMode, jettyInfo.taJettyId, jettyInfo.sqBufVa,
44 808 : jettyInfo.sqBufSize, jettyInfo.wqeBBStartId, jettyInfo.sqDepth};
45 808 : }
46 :
47 808 : CcuJetty::~CcuJetty()
48 : {
49 808 : DECTOR_TRY_CATCH("CcuJetty", {
50 : if (isCreated_ && outParam_.handle != 0) {
51 : HrtRaUbDestroyJetty(outParam_.handle);
52 : }
53 : });
54 808 : }
55 :
56 1 : bool CheckRequestResult(RequestHandle &reqHandle)
57 : {
58 1 : if (reqHandle == 0) {
59 0 : return true;
60 : }
61 :
62 1 : ReqHandleResult result = HrtRaGetAsyncReqResult(reqHandle);
63 1 : if (result == ReqHandleResult::NOT_COMPLETED) {
64 0 : return false;
65 : }
66 :
67 1 : if (result != ReqHandleResult::COMPLETED) {
68 0 : THROW<InternalException>("[CcuJetty][%s] failed, result[%s] is unexpected.",
69 0 : __func__, result.Describe().c_str());
70 : }
71 :
72 1 : return true;
73 : }
74 :
75 1 : static HcclResult ParseCreateInfo(const struct QpCreateInfo *infoPtr,
76 : const JettyHandle jettyHandle, HrtRaUbJettyCreatedOutParam &outParam)
77 : {
78 1 : outParam.handle = jettyHandle;
79 2 : auto ret = memcpy_s(outParam.key, HRT_UB_QP_KEY_MAX_LEN,
80 1 : infoPtr->key.value, infoPtr->key.size);
81 1 : if (ret != 0) {
82 0 : HCCL_ERROR("[CcuJetty][%s] create info key memcpy_s failed, ret[%d].",
83 : __func__, ret);
84 0 : return HcclResult::HCCL_E_MEMORY;
85 : }
86 1 : outParam.jettyVa = infoPtr->va;
87 1 : outParam.uasid = infoPtr->ub.uasid;
88 1 : outParam.id = infoPtr->ub.id;
89 1 : outParam.keySize = infoPtr->key.size;
90 1 : outParam.dbVa = infoPtr->ub.dbAddr;
91 1 : outParam.dbTokenId = infoPtr->ub.dbTokenId >> URMA_TOKEN_ID_RIGHT_SHIFT;
92 : // 不提供 tokenValue,不得打印token相关信息
93 1 : return HcclResult::HCCL_SUCCESS;
94 : }
95 :
96 3 : HcclResult CcuJetty::HandleAsyncRequest()
97 : {
98 6 : TRY_CATCH_RETURN(
99 : if (reqHandle_ == 0) {
100 : reqHandle_ = RaUbCreateJettyAsync(rdmaHandle_, inParam_, reqDataBuffer_, jettyHandlePtr_);
101 : return HcclResult::HCCL_E_AGAIN;
102 : }
103 :
104 : if (!CheckRequestResult(reqHandle_)) {
105 : return HcclResult::HCCL_E_AGAIN;
106 : };
107 : );
108 :
109 : const struct QpCreateInfo *info =
110 1 : reinterpret_cast<const QpCreateInfo *>(reqDataBuffer_.data());
111 1 : const JettyHandle jettyHandle = reinterpret_cast<JettyHandle>(jettyHandlePtr_);
112 1 : return ParseCreateInfo(info, jettyHandle, outParam_);
113 : }
114 :
115 5 : HcclResult CcuJetty::CreateJetty()
116 : {
117 5 : if (isError_) {
118 3 : HCCL_ERROR("[CcuJetty][%s] failed, jetty[%u] is error, "
119 : "refused to create.", __func__, inParam_.jettyId);
120 1 : return HcclResult::HCCL_E_INTERNAL;
121 : }
122 :
123 4 : if (isCreated_) {
124 3 : HCCL_INFO("[CcuJetty][%s] passed, jetty[%u] has been created.",
125 : __func__, inParam_.jettyId);
126 1 : return HcclResult::HCCL_SUCCESS;
127 : }
128 :
129 3 : auto ret = HandleAsyncRequest();
130 3 : if (ret == HcclResult::HCCL_SUCCESS) {
131 1 : isCreated_ = true;
132 2 : } else if (ret != HcclResult::HCCL_E_AGAIN) {
133 1 : isError_ = true;
134 : }
135 :
136 3 : return ret;
137 : }
138 :
139 40 : HrtRaUbCreateJettyParam CcuJetty::GetCreateJettyParam() const
140 : {
141 40 : return inParam_;
142 : }
143 :
144 52 : HrtRaUbJettyCreatedOutParam CcuJetty::GetJettyedOutParam() const
145 : {
146 52 : return outParam_;
147 : }
148 :
149 24 : void CcuJetty::GetJettyInfo(ConnJettyInfo& connJettyInfo)
150 : {
151 24 : if (isCreated_ && outParam_.handle != 0) {
152 14 : connJettyInfo.localJetty = outParam_.handle;
153 : }
154 24 : }
155 :
156 26 : HcclResult CcuJetty::Clean()
157 : {
158 26 : TRY_CATCH_RETURN(
159 : if (isCreated_ && outParam_.handle != 0) {
160 : isCreated_ = false;
161 : reqHandle_ = 0;
162 : jettyHandlePtr_ = nullptr;
163 : reqDataBuffer_.clear();
164 : }
165 : isError_ = false;);
166 26 : return HcclResult::HCCL_SUCCESS;
167 : }
168 : } // namespace Hccl
|