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 <hccl/hccl_types.h>
12 : #include "dlhal_function.h"
13 : #include "dlra_function.h"
14 : #include "sal_pub.h"
15 : #include "adapter_hccp.h"
16 : #include "network_manager_pub.h"
17 : #include "externalinput_pub.h"
18 : #include "hccl_network.h"
19 :
20 : namespace hccl {
21 252 : HcclResult NetDevContext::Init(NicType nicType, s32 devicePhyId, s32 deviceLogicId, HcclIpAddress localIp,
22 : HcclIpAddress backupIp)
23 : {
24 252 : devicePhyId_ = devicePhyId;
25 252 : deviceLogicId_ = deviceLogicId;
26 252 : localIp_ = localIp;
27 252 : backupIp_ = backupIp;
28 252 : nicType_ = nicType;
29 :
30 252 : if (nicType == NicType::VNIC_TYPE || nicType == NicType::DEVICE_NIC_TYPE) {
31 227 : nicDeployment_ = NICDeployment::NIC_DEPLOYMENT_DEVICE;
32 : } else {
33 25 : nicDeployment_ = NICDeployment::NIC_DEPLOYMENT_HOST;
34 : }
35 :
36 252 : if (static_cast<s32>(devicePhyId) == HOST_DEVICE_ID) {
37 0 : deviceLogicId_ = 0;
38 : }
39 :
40 252 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
41 25 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StartHostNet(localIp, hostSocketHandle_));
42 : }
43 :
44 252 : return HCCL_SUCCESS;
45 : }
46 :
47 0 : HcclResult NetDevContext::GetinfoConfig(const HcclNetDevInfos *info) {
48 0 : CHK_PTR_NULL(info);
49 0 : devicePhyId_ = info->devicePhyId;
50 0 : isBackup_ = info->isBackup;
51 0 : u32 deviceLogicId = 0;
52 0 : CHK_RET(hrtGetDeviceIndexByPhyId(devicePhyId_, deviceLogicId));
53 0 : deviceLogicId_ = deviceLogicId;
54 0 : CHK_RET(ConvertIP(info->addr));
55 0 : netDevDeployment_ = info->netdevDeployment;
56 0 : protoType_ = info->addr.protoType;
57 :
58 0 : HCCL_INFO("[NetDevContext][InitV2] netDevDeployment_ = [%u], protoType_ = [%u]", netDevDeployment_, protoType_);
59 0 : return HCCL_SUCCESS;
60 : }
61 :
62 0 : HcclResult NetDevContext::ConvertIP(const HcclAddress address) {
63 0 : s32 family = AF_INET;
64 : HcclInAddr temp;
65 0 : if (address.type == HCCL_ADDR_TYPE_IP_V4) {
66 0 : family = AF_INET;
67 0 : temp.addr = address.addr;
68 0 : } else if (address.type == HCCL_ADDR_TYPE_IP_V6) {
69 0 : family = AF_INET6;
70 0 : temp.addr6 = address.addr6;
71 : } else {
72 0 : HCCL_ERROR("[NetDevContext][InitV2]this addrType [%u] is not supported, please check the configuration.", address.type);
73 0 : return HCCL_E_PARA;
74 : }
75 0 : HcclIpAddress localIptemp(family, temp);
76 0 : localIp_ = localIptemp;
77 0 : return HCCL_SUCCESS;
78 0 : }
79 :
80 : // 初始化进程和设备
81 0 : HcclResult NetDevContext::InitV2(const HcclNetDevInfos *info)
82 : {
83 0 : CHK_PTR_NULL(info);
84 0 : CHK_RET(GetinfoConfig(info)) ;
85 : // 需要保存新的协议类型
86 0 : if (netDevDeployment_ == HcclNetDevDeployment::HCCL_NETDEV_DEPLOYMENT_DEVICE) {
87 0 : switch (protoType_) {
88 0 : case HCCL_PROTO_TYPE_ROCE:
89 : {
90 0 : nicType_ = NicType::DEVICE_NIC_TYPE;
91 0 : nicDeployment_ = NICDeployment::NIC_DEPLOYMENT_DEVICE;
92 0 : bool rdmaFlag = !GetExternalInputHcclIsTcpMode();
93 0 : if (!rdmaFlag) {
94 0 : HCCL_ERROR("[NetDevContext][InitV2]rdmaFlag and protoType are not equal, please check the configuration.");
95 0 : return HCCL_E_PARA;
96 : }
97 : NetworkMode netMode;
98 0 : NetworkManager::GetInstance(deviceLogicId_).GetNetworkMode(netMode);
99 : NotifyTypeT notifyType;
100 0 : NetworkManager::GetInstance(deviceLogicId_).GetNotifyType(notifyType);
101 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).CreateRdmaHandle(localIp_, isBackup_, netMode, notifyType, netDevDeployment_));
102 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).CreateNicSocketHandle(localIp_));
103 0 : NetworkManager::GetInstance(deviceLogicId_).GetRdmaHandleByIpAddr(localIp_, handle_);
104 :
105 0 : CHK_PTR_NULL(handle_);
106 0 : HCCL_INFO("[NetDevContext][InitV2]Deployment is device and proto is roce");
107 0 : break;
108 : }
109 0 : case HCCL_PROTO_TYPE_BUS:
110 : {
111 0 : nicType_ = NicType::VNIC_TYPE;
112 0 : nicDeployment_ = NICDeployment::NIC_DEPLOYMENT_DEVICE;
113 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).CreateVnicSocketHandle(localIp_));
114 0 : RaResourceInfo raResourceInfo;
115 0 : NetworkManager::GetInstance(deviceLogicId_).GetRaResourceInfo(raResourceInfo);
116 0 : IpSocket &sock = raResourceInfo.vnicSocketMap[localIp_];
117 0 : handle_ = sock.nicSocketHandle;
118 0 : CHK_PTR_NULL(handle_);
119 0 : HCCL_INFO("[NetDevContext][InitV2]Deployment is device and proto is bus");
120 0 : break;
121 0 : }
122 :
123 0 : case HCCL_PROTO_TYPE_TCP:
124 : {
125 0 : nicType_ = NicType::DEVICE_NIC_TYPE;
126 0 : nicDeployment_ = NICDeployment::NIC_DEPLOYMENT_DEVICE;
127 0 : bool rdmaFlag = !GetExternalInputHcclIsTcpMode();
128 0 : if (rdmaFlag) {
129 0 : HCCL_ERROR("[NetDevContext][InitV2]rdmaFlag is ERROR, please check the configuration.");
130 0 : return HCCL_E_PARA;
131 : }
132 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).CreateNicSocketHandle(localIp_));
133 0 : NetworkManager::GetInstance(deviceLogicId_).GetNicHandleByIpAddr(localIp_, handle_);
134 0 : CHK_PTR_NULL(handle_);
135 0 : HCCL_INFO("[NetDevContext][InitV2]Deployment is device and proto is tcp");
136 0 : break;
137 : }
138 :
139 0 : default: // 保留
140 0 : HCCL_ERROR("[NetDevContext][DeinitV2]this prototype [%u] is not supported in device mode, please check the configuration.", protoType_);
141 0 : return HCCL_E_NOT_SUPPORT;
142 : }
143 : }
144 0 : else if(netDevDeployment_ == HcclNetDevDeployment::HCCL_NETDEV_DEPLOYMENT_HOST) {
145 0 : switch (protoType_) {
146 0 : case HCCL_PROTO_TYPE_TCP:
147 : {
148 0 : nicType_ = NicType::HOST_NIC_TYPE;
149 0 : nicDeployment_ = NICDeployment::NIC_DEPLOYMENT_HOST;
150 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).CreateHostSocketHandle(localIp_, handle_));
151 0 : RaResourceInfo raResourceInfo;
152 0 : NetworkManager::GetInstance(deviceLogicId_).GetRaResourceInfo(raResourceInfo);
153 0 : IpSocket &sock = raResourceInfo.hostNetSocketMap[localIp_];
154 0 : handle_ = sock.nicSocketHandle;
155 0 : CHK_PTR_NULL(handle_);
156 0 : HCCL_INFO("[NetDevContext][InitV2]Deployment is host and proto is tcp");
157 0 : break;
158 0 : }
159 0 : case HCCL_PROTO_TYPE_ROCE:
160 : {
161 0 : nicType_ = NicType::HOST_NIC_TYPE;
162 0 : nicDeployment_ = NICDeployment::NIC_DEPLOYMENT_HOST;
163 0 : NetworkMode netMode = NETWORK_PEER_ONLINE;
164 0 : NotifyTypeT notifyType = NOTIFY;
165 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).CreateRdmaHandle(localIp_, isBackup_, netMode, notifyType, netDevDeployment_));
166 0 : NetworkManager::GetInstance(deviceLogicId_).GetRdmaHandleByIpAddr(localIp_, handle_);
167 0 : CHK_PTR_NULL(handle_);
168 0 : HCCL_INFO("[NetDevContext][InitV2]Deployment is host and proto is roce");
169 0 : break;
170 : }
171 0 : default: // 保留
172 0 : HCCL_ERROR("[NetDevContext][DeinitV2]this prototype [%u] is not supported in host mode, please check the configuration.", protoType_);
173 0 : return HCCL_E_NOT_SUPPORT;
174 : }
175 : } else {
176 0 : HCCL_ERROR("[NetDevContext][DeinitV2]this Deployment [%u] is not supported, please check the configuration.", netDevDeployment_);
177 0 : return HCCL_E_NOT_SUPPORT;
178 : // 保留
179 : }
180 :
181 0 : return HCCL_SUCCESS;
182 : }
183 :
184 230 : HcclResult NetDevContext::Deinit()
185 : {
186 230 : if (nicDeployment_ == NICDeployment::NIC_DEPLOYMENT_HOST) {
187 25 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StopHostNet(hostSocketHandle_, localIp_));
188 : }
189 230 : return HCCL_SUCCESS;
190 : }
191 :
192 0 : HcclResult NetDevContext::DeinitV2()
193 : {
194 0 : if (netDevDeployment_ == HcclNetDevDeployment::HCCL_NETDEV_DEPLOYMENT_DEVICE) {
195 0 : switch (protoType_) {
196 0 : case HCCL_PROTO_TYPE_ROCE:
197 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StopRdmaHandle(localIp_, netDevDeployment_));
198 0 : break;
199 0 : case HCCL_PROTO_TYPE_BUS:
200 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StopVnicSocketHandle(localIp_));
201 0 : break;
202 0 : case HCCL_PROTO_TYPE_TCP:
203 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StopNicSocketHandle( localIp_));
204 0 : break;
205 0 : default: // 保留
206 0 : HCCL_ERROR("[NetDevContext][DeinitV2]this prototype [%u] is not supported in host mode, please check the configuration.", protoType_);
207 0 : return HCCL_E_NOT_SUPPORT;
208 : }
209 : }
210 0 : else if(netDevDeployment_ == HcclNetDevDeployment::HCCL_NETDEV_DEPLOYMENT_HOST) {
211 0 : switch (protoType_) {
212 0 : case HCCL_PROTO_TYPE_TCP:
213 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StopHostSocketHandle(localIp_));
214 0 : break;
215 0 : case HCCL_PROTO_TYPE_ROCE:
216 0 : CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StopRdmaHandle(localIp_, netDevDeployment_));
217 0 : break;
218 0 : default: // 保留
219 0 : HCCL_ERROR("[NetDevContext][DeinitV2]this prototype [%u] is not supported in device mode, please check the configuration.", protoType_);
220 0 : return HCCL_E_NOT_SUPPORT;
221 : }
222 : } else {
223 0 : HCCL_ERROR("[NetDevContext][DeinitV2]this Deployment [%u] is not supported, please check the configuration.", netDevDeployment_);
224 0 : return HCCL_E_NOT_SUPPORT;
225 : }
226 0 : return HCCL_SUCCESS;
227 : }
228 :
229 13 : void NetDevContext::SetTlsStatus(TlsStatus tlsStatus)
230 : {
231 13 : tlsStatus_ = tlsStatus;
232 13 : HCCL_INFO("[NetDevContext][SetTlsStatus]devicePhyId[%d], set tlsStatus[%d]", devicePhyId_, tlsStatus);
233 13 : return;
234 : }
235 :
236 13 : void NetDevContext::SetIsNotNeedGetTlsStatus(bool isNotNeedGetTlsStatus)
237 : {
238 13 : isNotNeedGetTlsStatus_ = isNotNeedGetTlsStatus;
239 13 : return;
240 : }
241 : }
242 :
243 210 : HcclResult HcclNetInit(NICDeployment nicDeploy, s32 devicePhyId, s32 deviceLogicId, bool enableWhitelistFlag,
244 : bool hasBackup)
245 : {
246 210 : CHK_RET(hccl::DlRaFunction::GetInstance().DlRaFunctionInit());
247 210 : if (nicDeploy == NICDeployment::NIC_DEPLOYMENT_DEVICE) {
248 190 : CHK_RET(hccl::DlHalFunction::GetInstance().DlHalFunctionInit());
249 : bool isHostUseDevNic;
250 189 : CHK_RET(IsHostUseDevNic(isHostUseDevNic));
251 189 : u32 tempDevicePhyId = hasBackup ? static_cast<u32>(devicePhyId) : hccl::DEFAULT_PHY_ID;
252 189 : HCCL_DEBUG("[%s]start NetworkManager Init, deviceLogicId[%u], devicePhyId[%u], nicDeploy[%d], hasBackup[%d],"
253 : " tempDevicePhyId[%u]", __func__, deviceLogicId, devicePhyId, nicDeploy, hasBackup, tempDevicePhyId);
254 189 : CHK_RET(hccl::NetworkManager::GetInstance(deviceLogicId).Init(
255 : NICDeployment::NIC_DEPLOYMENT_DEVICE, enableWhitelistFlag, tempDevicePhyId, isHostUseDevNic, hasBackup));
256 : } else {
257 21 : CHK_RET(hccl::NetworkManager::GetInstance(deviceLogicId).Init(
258 : NICDeployment::NIC_DEPLOYMENT_HOST, enableWhitelistFlag, devicePhyId));
259 : }
260 :
261 209 : return HCCL_SUCCESS;
262 : }
263 :
264 203 : HcclResult HcclNetDeInit(NICDeployment nicDeploy, s32 devicePhyId, s32 deviceLogicId, bool hasBackup)
265 : {
266 203 : CHK_RET(hccl::NetworkManager::GetInstance(deviceLogicId).DeInit(nicDeploy, false, hasBackup));
267 202 : return HCCL_SUCCESS;
268 : }
269 :
270 239 : HcclResult HcclNetOpenDev(
271 : HcclNetDevCtx *netDevCtx, NicType nicType, s32 devicePhyId, s32 deviceLogicId, hccl::HcclIpAddress localIp,
272 : hccl::HcclIpAddress backupIp)
273 : {
274 239 : CHK_PTR_NULL(netDevCtx);
275 :
276 239 : hccl::NetDevContext *pNetDevCtx = new (std::nothrow) hccl::NetDevContext();
277 239 : CHK_PTR_NULL(pNetDevCtx);
278 :
279 239 : HcclResult ret = pNetDevCtx->Init(nicType, devicePhyId, deviceLogicId, localIp, backupIp);
280 239 : if (ret != HCCL_SUCCESS) {
281 1 : HCCL_ERROR("[Init][Port]Init fail. ret[%u]", ret);
282 1 : delete pNetDevCtx;
283 1 : pNetDevCtx = nullptr;
284 1 : return ret;
285 : }
286 :
287 238 : *netDevCtx = pNetDevCtx;
288 :
289 238 : return HCCL_SUCCESS;
290 : }
291 :
292 230 : void HcclNetCloseDev(HcclNetDevCtx netDevCtx)
293 : {
294 230 : if (netDevCtx == nullptr) {
295 0 : HCCL_ERROR("[HcclNetCloseDev] netDevCtx is nullptr");
296 0 : return;
297 : }
298 230 : hccl::NetDevContext* pNetDevCtx = static_cast<hccl::NetDevContext *>(netDevCtx);
299 :
300 230 : HcclResult ret = pNetDevCtx->Deinit();
301 230 : if (ret != HCCL_SUCCESS) {
302 0 : HCCL_ERROR("[DeInit][Port]DeInit fail. ret[%u]", ret);
303 : }
304 :
305 230 : delete pNetDevCtx;
306 : }
307 :
308 87 : HcclResult HcclNetDevGetNicType(HcclNetDevCtx netDevCtx, NicType *nicType)
309 : {
310 87 : CHK_PTR_NULL(netDevCtx);
311 87 : CHK_PTR_NULL(nicType);
312 87 : hccl::NetDevContext* pNetDevCtx = static_cast<hccl::NetDevContext *>(netDevCtx);
313 :
314 87 : *nicType = pNetDevCtx->GetNicType();
315 87 : return HCCL_SUCCESS;
316 : }
317 :
318 290 : HcclResult HcclNetDevGetLocalIp(HcclNetDevCtx netDevCtx, hccl::HcclIpAddress &localIp)
319 : {
320 290 : CHK_PTR_NULL(netDevCtx);
321 290 : hccl::NetDevContext* pNetDevCtx = static_cast<hccl::NetDevContext *>(netDevCtx);
322 :
323 290 : localIp = pNetDevCtx->GetLocalIp();
324 290 : return HCCL_SUCCESS;
325 : }
326 :
327 20 : HcclResult HcclNetDevGetProtoType(HcclNetDevCtx netDevCtx, u32 &proto)
328 : {
329 20 : CHK_PTR_NULL(netDevCtx);
330 20 : hccl::NetDevContext* pNetDevCtx = static_cast<hccl::NetDevContext *>(netDevCtx);
331 :
332 20 : proto = (u32)pNetDevCtx->GetProtoType();
333 20 : return HCCL_SUCCESS;
334 : }
335 :
336 0 : HcclResult HcclNetDevSetProtoType(HcclNetDevCtx netDevCtx, u32 proto)
337 : {
338 0 : CHK_PTR_NULL(netDevCtx);
339 0 : hccl::NetDevContext* pNetDevCtx = static_cast<hccl::NetDevContext *>(netDevCtx);
340 :
341 0 : pNetDevCtx->SetProtoType(proto);
342 0 : return HCCL_SUCCESS;
343 : }
344 :
345 0 : HcclResult HcclNetDevGetPortStatus(HcclNetDevCtx netDevCtx, bool &portStatus)
346 : {
347 0 : CHK_PTR_NULL(netDevCtx);
348 0 : hccl::NetDevContext* pNetDevCtx = static_cast<hccl::NetDevContext *>(netDevCtx);
349 0 : u32 devicePhyId = static_cast<u32>(pNetDevCtx->GetPhyId());
350 0 : RdmaHandle rdmaHandle = nullptr;
351 : enum PortStatus status;
352 0 : CHK_RET(HrtRaRdmaGetHandle(devicePhyId, rdmaHandle));
353 0 : CHK_RET(hrtRaRdevGetPortStatus(rdmaHandle, &status));
354 0 : portStatus = (status == PORT_STATUS_ACTIVE);
355 0 : HCCL_RUN_INFO("[HcclNetDevGetPortStatus]devicePhysicID_[%u], portStatus_[%d]", devicePhyId, portStatus);
356 0 : return HCCL_SUCCESS;
357 : }
358 :
359 38 : HcclResult HcclNetDevGetTlsStatus(HcclNetDevCtx netDevCtx, TlsStatus *tlsStatus)
360 : {
361 38 : CHK_PTR_NULL(netDevCtx);
362 38 : CHK_PTR_NULL(tlsStatus);
363 :
364 38 : hccl::NetDevContext* pNetDevCtx = static_cast<hccl::NetDevContext *>(netDevCtx);
365 38 : std::lock_guard<std::mutex> lock(pNetDevCtx->mu_);
366 : // tls开关状态多个通信域只需要查询一次,后续一直使用第一次查询结果
367 38 : if (pNetDevCtx->IsNotNeedGetTlsStatus()) {
368 25 : *tlsStatus = pNetDevCtx->GettlsStatus();
369 25 : return HCCL_SUCCESS;
370 : }
371 :
372 13 : u32 devicePhyId = static_cast<u32>(pNetDevCtx->GetPhyId());
373 13 : struct RaInfo raInfo = {};
374 13 : raInfo.mode = static_cast<int>(pNetDevCtx->GetNicDeployment());
375 13 : raInfo.phyId = devicePhyId;
376 13 : bool tlsEnable = false;
377 13 : HcclResult ret = HrtRaGetTlsEnable(&raInfo, &tlsEnable);
378 13 : if (ret == HCCL_E_NOT_SUPPORT) {
379 0 : pNetDevCtx->SetTlsStatus(TlsStatus::UNKNOWN);
380 13 : } else if(tlsEnable) {
381 0 : pNetDevCtx->SetTlsStatus(TlsStatus::ENABLE);
382 : } else {
383 13 : pNetDevCtx->SetTlsStatus(TlsStatus::DISABLE);
384 : }
385 13 : *tlsStatus = pNetDevCtx->GettlsStatus();
386 13 : pNetDevCtx->SetIsNotNeedGetTlsStatus(true);
387 13 : return ret;
388 38 : }
|