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 7819 : HcclResult CommAddrToIpAddress(const CommAddr &commAddr, Hccl::IpAddress &ipAddr)
40 : {
41 7819 : 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 7816 : int32_t family = AF_INET6;
54 7816 : if (commAddr.type == COMM_ADDR_TYPE_IP_V4) {
55 1440 : binAddr.addr = commAddr.addr;
56 1440 : int32_t family = AF_INET;
57 1440 : ipAddr = Hccl::IpAddress(binAddr, family);
58 1440 : return HCCL_SUCCESS;
59 : }
60 :
61 6376 : 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, errno[%d]", sret), HCCL_E_MEMORY);
65 1 : ipAddr = Hccl::IpAddress(inputEid);
66 1 : return HCCL_SUCCESS;
67 : }
68 :
69 6375 : binAddr.addr6 = commAddr.addr6;
70 6375 : ipAddr = Hccl::IpAddress(binAddr, family);
71 6375 : return HCCL_SUCCESS;
72 : }
73 :
74 98 : HcclResult IpAddressToCommAddr(const Hccl::IpAddress &ipAddr, CommAddr &commAddr)
75 : {
76 98 : int32_t family = ipAddr.GetFamily();
77 98 : const auto &binAddr = ipAddr.GetBinaryAddress();
78 :
79 98 : if (family == AF_INET) {
80 46 : commAddr.addr = binAddr.addr;
81 46 : commAddr.type = COMM_ADDR_TYPE_IP_V4;
82 46 : return HcclResult::HCCL_SUCCESS;
83 : }
84 :
85 52 : commAddr.addr6 = binAddr.addr6;
86 52 : commAddr.type = COMM_ADDR_TYPE_IP_V6;
87 52 : return HcclResult::HCCL_SUCCESS;
88 : }
89 :
90 71 : HcclResult CommProtocolToLinkProtocol(CommProtocol commProtocol, Hccl::LinkProtocol &linkProtocol)
91 : {
92 71 : switch (commProtocol) {
93 33 : case COMM_PROTOCOL_UBC_CTP:
94 33 : linkProtocol = Hccl::LinkProtocol::UB_CTP;
95 33 : 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 4 : case COMM_PROTOCOL_HCCS:
103 4 : linkProtocol = Hccl::LinkProtocol::HCCS;
104 4 : 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 3 : case COMM_PROTOCOL_UBG:
115 3 : linkProtocol = Hccl::LinkProtocol::UBG;
116 3 : break;
117 1 : default:
118 1 : HCCL_ERROR("[%s] Invalid CommProtocol[%u]", __func__, commProtocol);
119 1 : return HCCL_E_PARA;
120 : }
121 70 : 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] Invalid CommAddrType[%u]", __func__, commAddrType);
135 1 : return HCCL_E_NOT_FOUND;
136 : }
137 6 : return HCCL_SUCCESS;
138 : }
139 :
140 61 : Hccl::LinkData BuildDefaultLinkData()
141 : {
142 61 : Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::HOST_NET;
143 61 : Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::ROCE;
144 61 : Hccl::IpAddress locAddr;
145 61 : Hccl::IpAddress rmtAddr;
146 61 : uint32_t locDevPhyId = 0;
147 61 : uint32_t rmtDevPhyId = 0;
148 : return Hccl::LinkData(
149 : portDeploymentType,
150 : linkProtocol,
151 : locDevPhyId, rmtDevPhyId,
152 : locAddr, rmtAddr
153 61 : );
154 : }
155 :
156 56 : static HcclResult EndpointLocTypeToPortDeploymentType(const EndpointLocType locType,
157 : Hccl::PortDeploymentType &deployType)
158 : {
159 56 : switch (locType) {
160 17 : case EndpointLocType::ENDPOINT_LOC_TYPE_HOST:
161 17 : deployType = Hccl::PortDeploymentType::HOST_NET;
162 17 : break;
163 38 : case EndpointLocType::ENDPOINT_LOC_TYPE_DEVICE:
164 38 : deployType = Hccl::PortDeploymentType::DEV_NET;
165 38 : 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 55 : return HcclResult::HCCL_SUCCESS;
172 : }
173 :
174 43 : HcclResult EndpointDescPairToLinkData(const EndpointDesc &locEp, const EndpointDesc &rmtEp,
175 : Hccl::LinkData &linkData, u32 reuseIdx)
176 : {
177 43 : Hccl::PortDeploymentType portDeploymentType = Hccl::PortDeploymentType::INVALID;
178 43 : CHK_RET(EndpointLocTypeToPortDeploymentType(locEp.loc.locType, portDeploymentType));
179 :
180 42 : Hccl::LinkProtocol linkProtocol = Hccl::LinkProtocol::INVALID;
181 42 : CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, linkProtocol));
182 :
183 42 : Hccl::IpAddress locAddr{};
184 42 : Hccl::IpAddress rmtAddr{};
185 42 : CHK_RET(CommAddrToIpAddress(locEp.commAddr, locAddr));
186 42 : CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, rmtAddr));
187 :
188 42 : uint32_t locDevPhyId = locEp.loc.device.devPhyId;
189 42 : uint32_t rmtDevPhyId = rmtEp.loc.device.devPhyId;
190 :
191 : // 开源开放架构下comms层级不感知通信域层级的rank信息
192 : // 当前复用orion数据结构故使用devId替换
193 42 : linkData = Hccl::LinkData(
194 : portDeploymentType,
195 : linkProtocol,
196 : locDevPhyId, rmtDevPhyId,
197 : locAddr, rmtAddr, reuseIdx
198 42 : );
199 :
200 42 : 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 9 : HcclResult PrepareUbConnBuildContext(const EndpointDesc &locEp, const EndpointDesc &rmtEp, uint32_t channelQos,
231 : UbConnBuildContext &ctx)
232 : {
233 9 : CHK_RET(CommProtocolToLinkProtocol(locEp.protocol, ctx.protocol));
234 9 : CHK_RET(CommAddrToIpAddress(locEp.commAddr, ctx.locAddr));
235 9 : CHK_RET(CommAddrToIpAddress(rmtEp.commAddr, ctx.rmtAddr));
236 9 : CHK_RET(hrtGetDevice(&ctx.deviceLogicId));
237 9 : Hccl::TpManager::GetInstance(ctx.deviceLogicId).Init();
238 9 : 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 9 : ctx.qosPre = static_cast<u8>(channelQos);
244 : }
245 9 : return HCCL_SUCCESS;
246 : }
247 :
248 : } // namespace hcomm
|