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 "hcom_common.h"
14 :
15 : #include "hccp_ctx.h"
16 :
17 : #include "exception_handler.h"
18 :
19 : // 当前复用orion数据结构
20 : #include "rdma_handle_manager.h"
21 : #include "local_ub_rma_buffer.h"
22 : #include "orion_adapter_hccp.h"
23 :
24 : namespace hcomm {
25 :
26 12 : HcclResult CcuCreateJetty(const Hccl::IpAddress &ipAddr, const CcuJettyInfo &jettyInfo,
27 : std::unique_ptr<CcuJetty> &ccuJetty)
28 : {
29 : EXCEPTION_HANDLE_BEGIN
30 :
31 12 : ccuJetty = std::make_unique<CcuJetty>(ipAddr, jettyInfo);
32 12 : CHK_RET(ccuJetty->Init());
33 :
34 0 : EXCEPTION_HANDLE_END
35 12 : return HcclResult::HCCL_SUCCESS;
36 : }
37 :
38 30 : CcuJetty::CcuJetty(const Hccl::IpAddress &ipAddr, const CcuJettyInfo &jettyInfo)
39 30 : : ipAddr_(ipAddr), jettyInfo_(jettyInfo)
40 : {
41 30 : }
42 :
43 14 : HcclResult CcuJetty::Init()
44 : {
45 : EXCEPTION_HANDLE_BEGIN
46 14 : devLogicId_ = HcclGetThreadDeviceId();
47 14 : uint32_t devPhyId{0};
48 14 : Hccl::CqCreateInfo cqInfo{0};
49 14 : CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<uint32_t>(devLogicId_), devPhyId));
50 14 : auto &rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
51 14 : ctxHandle_ = rdmaHandleMgr.GetByIp(devPhyId, ipAddr_);
52 14 : CHK_PRT_RET(!rdmaHandleMgr.IsHandleValid(ctxHandle_),
53 : HCCL_ERROR("[CcuJetty][%s] ctxHandle_[%p] is not valid, "
54 : "RdmaHandleManager may have DeInit this device", __func__, ctxHandle_),
55 : HcclResult::HCCL_E_INTERNAL);
56 14 : const auto _jfcHandle = rdmaHandleMgr.GetJfcHandle(ctxHandle_, cqInfo, Hccl::HrtUbJfcMode::CCU_POLL);
57 14 : const JfcHandle jfcHandle = reinterpret_cast<JfcHandle>(_jfcHandle);
58 14 : const auto &tokenInfo = rdmaHandleMgr.GetTokenIdInfo(ctxHandle_);
59 14 : const auto _tokenIdHandle = tokenInfo.first;
60 14 : const TokenIdHandle tokenIdHandle = reinterpret_cast<TokenIdHandle>(_tokenIdHandle);
61 14 : const auto tokenValue = Hccl::GetUbToken();
62 14 : const auto jettyMode = jettyInfo_.jettyType == CcuJettyType::CCUM_CACHED_JETTY ?
63 14 : HrtJettyMode::CCU_CCUM_CACHE : HrtJettyMode::CCU_TA_CACHE;
64 :
65 14 : inParam_ = HrtRaUbCreateJettyParam{jfcHandle, jfcHandle, tokenValue,
66 14 : tokenIdHandle, jettyMode, jettyInfo_.taJettyId, jettyInfo_.sqBufVa,
67 14 : jettyInfo_.sqBufSize, jettyInfo_.wqeBBStartId, jettyInfo_.sqDepth}; // CTP默认为8s
68 0 : EXCEPTION_HANDLE_END
69 :
70 14 : return HcclResult::HCCL_SUCCESS;
71 : }
72 :
73 7 : HcclResult CcuJetty::SetMappedJettyPriority(uint32_t priority)
74 : {
75 7 : const uint8_t mapped = static_cast<uint8_t>(priority & 0xFU);
76 :
77 7 : if (mappedJettyPrioritySet_ && mappedJettyPriority_ != mapped) {
78 1 : HCCL_ERROR("[CcuJetty][%s] mappedJettyPriority conflict on shared jetty: existing[%u] new[%u] "
79 : "jettyId[%u] isCreated[%d].",
80 : __func__, static_cast<unsigned>(mappedJettyPriority_), static_cast<unsigned>(mapped),
81 : jettyInfo_.taJettyId, static_cast<int>(isCreated_));
82 1 : return HcclResult::HCCL_E_INTERNAL;
83 : }
84 :
85 : // 多 channel 复用:jetty 已 create,qos 已写入 URMA,不可再改 inParam_
86 6 : if (isCreated_) {
87 0 : HCCL_INFO("[CcuJetty][%s] jetty[%u] already created, skip mappedJettyPriority[%u].",
88 : __func__, jettyInfo_.taJettyId, static_cast<unsigned>(mapped));
89 0 : return HcclResult::HCCL_SUCCESS;
90 : }
91 :
92 6 : if (mappedJettyPrioritySet_) {
93 0 : return HcclResult::HCCL_SUCCESS;
94 : }
95 :
96 6 : mappedJettyPriority_ = mapped;
97 6 : mappedJettyPrioritySet_ = true;
98 6 : inParam_.qos = mapped;
99 6 : return HcclResult::HCCL_SUCCESS;
100 : }
101 :
102 30 : CcuJetty::~CcuJetty()
103 : {
104 30 : (void)Clean();
105 30 : }
106 :
107 0 : static HcclResult CheckRequestResult(RequestHandle &reqHandle)
108 : {
109 0 : if (reqHandle == 0) {
110 0 : return HcclResult::HCCL_SUCCESS;
111 : }
112 :
113 0 : RequestResult result = HccpGetAsyncReqResult(reqHandle);
114 0 : if (result == RequestResult::NOT_COMPLETED) {
115 0 : return HcclResult::HCCL_E_AGAIN;
116 : }
117 :
118 0 : if (result != RequestResult::COMPLETED) {
119 0 : HCCL_ERROR("[TpMgr][%s] failed, result[%s] is unexpected.",
120 : __func__, result.Describe().c_str());
121 0 : return HcclResult::HCCL_E_NETWORK;
122 : }
123 :
124 0 : return HcclResult::HCCL_SUCCESS;
125 : }
126 :
127 0 : static HcclResult ParseCreateInfo(const struct QpCreateInfo *infoPtr,
128 : const JettyHandle jettyHandle, HrtRaUbJettyCreatedOutParam &outParam)
129 : {
130 0 : outParam.handle = jettyHandle;
131 0 : auto ret = memcpy_s(outParam.key, HRT_UB_QP_KEY_MAX_LEN,
132 0 : infoPtr->key.value, infoPtr->key.size);
133 0 : if (ret != 0) {
134 0 : HCCL_ERROR("[CcuJetty][%s] create info key memcpy_s failed, ret[%d].",
135 : __func__, ret);
136 0 : return HcclResult::HCCL_E_MEMORY;
137 : }
138 :
139 0 : constexpr uint32_t URMA_TOKEN_ID_RIGHT_SHIFT = 8;
140 :
141 0 : outParam.jettyVa = infoPtr->va;
142 0 : outParam.uasid = infoPtr->ub.uasid;
143 0 : outParam.id = infoPtr->ub.id;
144 0 : outParam.keySize = infoPtr->key.size;
145 0 : outParam.dbVa = infoPtr->ub.dbAddr;
146 0 : outParam.dbTokenId = infoPtr->ub.dbTokenId >> URMA_TOKEN_ID_RIGHT_SHIFT;
147 : // 不提供 tokenValue,不得打印token相关信息
148 0 : return HcclResult::HCCL_SUCCESS;
149 : }
150 :
151 0 : HcclResult CcuJetty::HandleAsyncRequest()
152 : {
153 0 : if (reqHandle_ == 0) {
154 0 : CHK_RET(HccpUbCreateJettyAsync(ctxHandle_, inParam_,
155 : reqDataBuffer_, jettyHandlePtr_, reqHandle_));
156 0 : return HcclResult::HCCL_E_AGAIN; // 首次触发异步接口调用,动作一定未完成
157 : }
158 :
159 0 : auto ret = CheckRequestResult(reqHandle_);
160 0 : if (ret == HcclResult::HCCL_E_AGAIN) {
161 0 : return ret;
162 : }
163 0 : CHK_RET(ret);
164 :
165 : const struct QpCreateInfo *info =
166 0 : reinterpret_cast<const QpCreateInfo *>(reqDataBuffer_.data());
167 0 : const JettyHandle jettyHandle = reinterpret_cast<JettyHandle>(jettyHandlePtr_);
168 0 : return ParseCreateInfo(info, jettyHandle, outParam_);
169 : }
170 :
171 0 : HcclResult CcuJetty::CreateJetty(u8 errTimeout)
172 : {
173 0 : if (isError_) {
174 0 : HCCL_ERROR("[CcuJetty][%s] failed, jetty[%u] is error, "
175 : "refused to create.", __func__, inParam_.jettyId);
176 0 : return HcclResult::HCCL_E_INTERNAL;
177 : }
178 :
179 0 : if (isCreated_) {
180 0 : HCCL_INFO("[CcuJetty][%s] passed, jetty[%u] has been created.",
181 : __func__, inParam_.jettyId);
182 0 : return HcclResult::HCCL_SUCCESS;
183 : }
184 :
185 0 : inParam_.errTimeout = errTimeout;
186 0 : auto ret = HandleAsyncRequest();
187 0 : if (ret == HcclResult::HCCL_SUCCESS) {
188 0 : isCreated_ = true;
189 0 : } else if (ret != HcclResult::HCCL_E_AGAIN) {
190 0 : isError_ = true;
191 : }
192 :
193 0 : return ret;
194 : }
195 :
196 2 : HrtRaUbCreateJettyParam CcuJetty::GetCreateJettyParam() const
197 : {
198 2 : return inParam_;
199 : }
200 :
201 4 : HrtRaUbJettyCreatedOutParam CcuJetty::GetJettyedOutParam() const
202 : {
203 4 : return outParam_;
204 : }
205 :
206 30 : HcclResult CcuJetty::Clean()
207 : {
208 30 : if (isCreated_ && outParam_.handle != 0) {
209 0 : auto jettyHandle = outParam_.handle;
210 0 : outParam_ = {}; // 移动handle并置空,防止二次释放
211 0 : isCreated_ = false;
212 0 : reqHandle_ = 0;
213 0 : jettyHandlePtr_ = nullptr;
214 0 : reqDataBuffer_.clear();
215 :
216 0 : auto ret = RaCtxQpDestroy(jettyHandle);
217 0 : if (ret != 0) {
218 0 : HCCL_ERROR("[CcuJetty][%s] failed, jettyHanlde[0x%llx].",
219 : __func__, jettyHandle);
220 0 : return HcclResult::HCCL_E_NETWORK;
221 : }
222 : }
223 30 : isError_ = false;
224 30 : return HcclResult::HCCL_SUCCESS;
225 : }
226 : } // namespace hcomm
|