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 "orion_adpt_utils.h"
12 :
13 : // Orion
14 : #include "adapter_rts_common.h"
15 : #include "orion_adapter_hccp.h"
16 : #include "tp_manager.h"
17 : #include "topo_common_types.h"
18 : #include "virtual_topo.h"
19 : #include "hcomm_c_adpt.h"
20 : #include "hcomm_adapter_hccp.h"
21 :
22 : namespace hcomm {
23 :
24 3 : const char* CommAddrTypeToStr(CommAddrType type)
25 : {
26 3 : switch (type) {
27 0 : case COMM_ADDR_TYPE_IP_V4:
28 0 : return "COMM_ADDR_TYPE_IP_V4";
29 0 : case COMM_ADDR_TYPE_IP_V6:
30 0 : return "COMM_ADDR_TYPE_IP_V6";
31 0 : case COMM_ADDR_TYPE_EID:
32 0 : return "COMM_ADDR_TYPE_EID";
33 2 : case COMM_ADDR_TYPE_ID:
34 2 : return "COMM_ADDR_TYPE_ID";
35 0 : case COMM_ADDR_TYPE_RESERVED:
36 0 : return "COMM_ADDR_TYPE_RESERVED";
37 1 : default:
38 1 : return "UNKNOWN_COMM_ADDR_TYPE";
39 : }
40 : }
41 :
42 9097 : HcclResult CommAddrToIpAddress(const CommAddr& commAddr, Hccl::IpAddress& ipAddr)
43 : {
44 9097 : if (commAddr.type != COMM_ADDR_TYPE_IP_V4 && commAddr.type != COMM_ADDR_TYPE_IP_V6
45 4 : && commAddr.type != COMM_ADDR_TYPE_EID) {
46 3 : if (commAddr.type == COMM_ADDR_TYPE_ID || commAddr.type == COMM_ADDR_TYPE_RESERVED) {
47 2 : HCCL_ERROR(
48 : "[%s] failed, comm address type[%d][%s] is not supported.", __func__, commAddr.type,
49 : CommAddrTypeToStr(commAddr.type));
50 : } else {
51 1 : HCCL_ERROR(
52 : "[%s] failed, comm address type[%d][%s] is invalid.", __func__, commAddr.type,
53 : CommAddrTypeToStr(commAddr.type));
54 : }
55 3 : return HCCL_E_NOT_SUPPORT;
56 : }
57 :
58 : Hccl::BinaryAddr binAddr;
59 9094 : int32_t family = AF_INET6;
60 9094 : if (commAddr.type == COMM_ADDR_TYPE_IP_V4) {
61 1560 : binAddr.addr = commAddr.addr;
62 1560 : int32_t family = AF_INET;
63 1560 : ipAddr = Hccl::IpAddress(binAddr, family);
64 1560 : return HCCL_SUCCESS;
65 : }
66 :
67 7534 : if (commAddr.type == COMM_ADDR_TYPE_EID) {
68 1 : Hccl::Eid inputEid;
69 1 : s32 sret = memcpy_s(inputEid.raw, Hccl::URMA_EID_LEN, commAddr.eid, Hccl::URMA_EID_LEN);
70 1 : CHK_PRT_RET(sret != EOK, HCCL_ERROR("memcpy failed, errno[%d]", sret), HCCL_E_MEMORY);
71 1 : ipAddr = Hccl::IpAddress(inputEid);
72 1 : return HCCL_SUCCESS;
73 : }
74 :
75 7533 : binAddr.addr6 = commAddr.addr6;
76 7533 : ipAddr = Hccl::IpAddress(binAddr, family);
77 7533 : return HCCL_SUCCESS;
78 : }
79 :
80 128 : HcclResult IpAddressToCommAddr(const Hccl::IpAddress& ipAddr, CommAddr& commAddr)
81 : {
82 128 : int32_t family = ipAddr.GetFamily();
83 128 : const auto& binAddr = ipAddr.GetBinaryAddress();
84 :
85 128 : if (family == AF_INET) {
86 46 : commAddr.addr = binAddr.addr;
87 46 : commAddr.type = COMM_ADDR_TYPE_IP_V4;
88 46 : return HcclResult::HCCL_SUCCESS;
89 : }
90 :
91 82 : commAddr.addr6 = binAddr.addr6;
92 82 : commAddr.type = COMM_ADDR_TYPE_IP_V6;
93 82 : return HcclResult::HCCL_SUCCESS;
94 : }
95 :
96 103 : HcclResult CommProtocolToLinkProtocol(CommProtocol commProtocol, Hccl::LinkProtocol& linkProtocol)
97 : {
98 103 : switch (commProtocol) {
99 34 : case COMM_PROTOCOL_UB_CTP:
100 34 : linkProtocol = Hccl::LinkProtocol::UB_CTP;
101 34 : break;
102 2 : case COMM_PROTOCOL_UBC_TP:
103 2 : linkProtocol = Hccl::LinkProtocol::UB_TP;
104 2 : break;
105 16 : case COMM_PROTOCOL_ROCE:
106 16 : linkProtocol = Hccl::LinkProtocol::ROCE;
107 16 : break;
108 4 : case COMM_PROTOCOL_HCCS:
109 4 : linkProtocol = Hccl::LinkProtocol::HCCS;
110 4 : break;
111 41 : case COMM_PROTOCOL_UB_MEM:
112 41 : linkProtocol = Hccl::LinkProtocol::UB_MEM;
113 41 : break;
114 0 : case COMM_PROTOCOL_PCIE:
115 0 : linkProtocol = Hccl::LinkProtocol::PCIE;
116 0 : break;
117 0 : case COMM_PROTOCOL_UBOE:
118 0 : linkProtocol = Hccl::LinkProtocol::UBOE;
119 0 : break;
120 5 : case COMM_PROTOCOL_UB_RTP:
121 5 : linkProtocol = Hccl::LinkProtocol::UB_RTP;
122 5 : break;
123 1 : default:
124 1 : HCCL_ERROR("[%s] Invalid CommProtocol[%u]", __func__, commProtocol);
125 1 : return HCCL_E_PARA;
126 : }
127 102 : return HCCL_SUCCESS;
128 : }
129 :
130 7 : HcclResult CommAddrTypeToHcclAddressType(CommAddrType commAddrType, HcclAddressType& hcclAddressType)
131 : {
132 7 : switch (commAddrType) {
133 5 : case COMM_ADDR_TYPE_IP_V4:
134 5 : hcclAddressType = HCCL_ADDR_TYPE_IP_V4;
135 5 : break;
136 1 : case COMM_ADDR_TYPE_IP_V6:
137 1 : hcclAddressType = HCCL_ADDR_TYPE_IP_V6;
138 1 : break;
139 1 : default:
140 1 : HCCL_ERROR("[%s] Invalid CommAddrType[%u]", __func__, commAddrType);
141 1 : return HCCL_E_NOT_FOUND;
142 : }
143 6 : return HCCL_SUCCESS;
144 : }
145 :
146 89 : Hccl::LinkData BuildDefaultLinkData()
147 : {
148 89 : Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::HOST_NET;
149 89 : Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::ROCE;
150 89 : Hccl::IpAddress locAddr;
151 89 : Hccl::IpAddress rmtAddr;
152 89 : uint32_t locDevPhyId = 0;
153 89 : uint32_t rmtDevPhyId = 0;
154 89 : return Hccl::LinkData(portDeploymentType, linkProtocol, locDevPhyId, rmtDevPhyId, locAddr, rmtAddr);
155 : }
156 :
157 : static HcclResult
158 84 : EndpointLocTypeToPortDeploymentType(const EndpointLocType locType, Hccl::PortDeploymentType& deployType)
159 : {
160 84 : switch (locType) {
161 17 : case EndpointLocType::ENDPOINT_LOC_TYPE_HOST:
162 17 : deployType = Hccl::PortDeploymentType::HOST_NET;
163 17 : break;
164 66 : case EndpointLocType::ENDPOINT_LOC_TYPE_DEVICE:
165 66 : deployType = Hccl::PortDeploymentType::DEV_NET;
166 66 : break;
167 1 : default:
168 1 : HCCL_ERROR("[%s] unknown type of EndpointLocType[%d]", __func__, locType);
169 1 : return HcclResult::HCCL_E_PARA;
170 : }
171 :
172 83 : return HcclResult::HCCL_SUCCESS;
173 : }
174 :
175 : HcclResult
176 43 : EndpointDescPairToLinkData(const EndpointDesc& locEp, const EndpointDesc& rmtEp, Hccl::LinkData& linkData, u32 reuseIdx)
177 : {
178 43 : Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::INVALID;
179 43 : CHK_RET(EndpointLocTypeToPortDeploymentType(locEp.loc.locType, portDeploymentType));
180 :
181 42 : Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::INVALID;
182 42 : CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, linkProtocol));
183 :
184 42 : Hccl::IpAddress locAddr{};
185 42 : Hccl::IpAddress rmtAddr{};
186 42 : CHK_RET(CommAddrToIpAddress(locEp.commAddr, locAddr));
187 42 : CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, rmtAddr));
188 :
189 42 : uint32_t locDevPhyId = locEp.loc.device.devPhyId;
190 42 : uint32_t rmtDevPhyId = rmtEp.loc.device.devPhyId;
191 :
192 : // 开源开放架构下comms层级不感知通信域层级的rank信息
193 : // 当前复用orion数据结构故使用devId替换
194 42 : linkData = Hccl::LinkData(portDeploymentType, linkProtocol, locDevPhyId, rmtDevPhyId, locAddr, rmtAddr, reuseIdx);
195 :
196 42 : return HCCL_SUCCESS;
197 : }
198 :
199 41 : HcclResult EndpointDescPairToLinkDataWithRankIds(
200 : const uint32_t myRank, const uint32_t rmtRank, const EndpointDesc& locEp, const EndpointDesc& rmtEp,
201 : Hccl::LinkData& linkData, uint32_t devicePhyId, uint32_t remoteDevicePhyId, u32 reuseIdx)
202 : {
203 41 : Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::INVALID;
204 41 : CHK_RET(EndpointLocTypeToPortDeploymentType(locEp.loc.locType, portDeploymentType));
205 :
206 41 : Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::INVALID;
207 41 : CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, linkProtocol));
208 :
209 41 : Hccl::IpAddress locAddr{};
210 41 : Hccl::IpAddress rmtAddr{};
211 41 : CHK_RET(CommAddrToIpAddress(locEp.commAddr, locAddr));
212 41 : CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, rmtAddr));
213 :
214 : // 临时方案,为支持开源开放与orion通信域混跑,复用orion数据结构,添加rank信息
215 41 : linkData = Hccl::LinkData(
216 41 : portDeploymentType, linkProtocol, myRank, rmtRank, locAddr, rmtAddr, devicePhyId, remoteDevicePhyId, reuseIdx);
217 41 : linkData.UpdateIpAddrWithPCIE();
218 :
219 41 : return HCCL_SUCCESS;
220 : }
221 :
222 13 : HcclResult PrepareUbConnBuildContext(
223 : const EndpointDesc& locEp, const EndpointDesc& rmtEp, const HcommChannelDesc& channelDesc, UbConnBuildContext& ctx)
224 : {
225 13 : CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, ctx.protocol));
226 13 : CHK_RET(CommAddrToIpAddress(locEp.commAddr, ctx.locAddr));
227 13 : CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, ctx.rmtAddr));
228 13 : CHK_RET(hrtGetDevice(&ctx.deviceLogicId));
229 13 : Hccl::TpManager::GetInstance(ctx.deviceLogicId).Init();
230 13 : if (channelDesc.qos > 7U) {
231 0 : HCCL_WARNING(
232 : "[PrepareUbConnBuildContext] invalid channelQos[%u], expect [0, 7], use default qos[%u].", channelDesc.qos,
233 : Hccl::kRaUbGetTpInfoParamDefaultQos);
234 0 : ctx.qosPre = static_cast<u8>(Hccl::kRaUbGetTpInfoParamDefaultQos);
235 : } else {
236 13 : ctx.qosPre = static_cast<u8>(channelDesc.qos);
237 : }
238 13 : ctx.sqDepth = channelDesc.ubAttr.sqDepth;
239 13 : return HCCL_SUCCESS;
240 : }
241 :
242 6 : HcclResult CheckUbSqDepth(const UbConnBuildContext& ctx, const DevBaseAttr& devBaseAttr)
243 : {
244 6 : if (ctx.sqDepth == UB_SQ_DEPTH_NOT_SET) {
245 1 : return HCCL_SUCCESS;
246 : }
247 5 : if (ctx.sqDepth < UB_SQ_DEPTH_MIN || ctx.sqDepth > devBaseAttr.sqMaxDepth) {
248 1 : HCCL_ERROR(
249 : "[%s] invalid ubAttr.sqDepth[%u], aligned range is [%u, %u] (aligned to power-of-two before compared).",
250 : __func__, ctx.sqDepth, UB_SQ_DEPTH_MIN, devBaseAttr.sqMaxDepth);
251 1 : return HCCL_E_PARA;
252 : }
253 4 : return HCCL_SUCCESS;
254 : }
255 :
256 : } // namespace hcomm
|