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