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