Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 <memory>
11 :
12 : #include "hcomm_c_adpt.h"
13 : #include "hcomm_c_adpt_common.h"
14 : #include "log.h"
15 : #include "endpoint.h"
16 : #include "endpoint_monitor.h"
17 : #include "../hcomm_res_mgr.h"
18 : #include "hcomm_result_defs.h"
19 : #include "param_check_pub.h"
20 : #include "exception_handler.h"
21 : #include "hcom_common.h"
22 : #include "hcomm_res_defs.h"
23 : #ifdef ENABLE_EXPERIMENTAL
24 : #include "nic_plugin_dispatcher.h"
25 : #endif
26 :
27 : using namespace hcomm;
28 :
29 : namespace {
30 70 : HcclResult ValidateEndpointDesc(const EndpointDesc *endpoint, EndpointHandle *endpointHandle)
31 : {
32 70 : CHK_PTR_NULL(endpoint);
33 69 : CHK_PTR_NULL(endpointHandle);
34 68 : if (endpoint->loc.locType != ENDPOINT_LOC_TYPE_DEVICE && endpoint->loc.locType != ENDPOINT_LOC_TYPE_HOST) {
35 0 : HCCL_ERROR("[%s] Only support END_POINT_LOCATION_DEVICE AND END_POINT_LOCATION_HOST, but "
36 : "endpoint->loc.locType is %d",
37 : __func__, endpoint->loc.locType);
38 0 : return HCCL_E_PARA;
39 : }
40 68 : return HCCL_SUCCESS;
41 : }
42 :
43 61 : HcclResult RegisterDeviceEndpointMonitorIfNeeded(const EndpointDesc *endpoint, EndpointHandle handle)
44 : {
45 61 : if ((endpoint->loc.locType != ENDPOINT_LOC_TYPE_DEVICE)
46 55 : || ((endpoint->protocol != COMM_PROTOCOL_UBC_CTP) && (endpoint->protocol != COMM_PROTOCOL_UBC_TP))) {
47 44 : return HCCL_SUCCESS;
48 : }
49 :
50 17 : s32 devLogicIdSigned = HcclGetThreadDeviceId();
51 17 : CHK_PRT_RET(devLogicIdSigned < 0,
52 : HCCL_ERROR("[%s] HcclGetThreadDeviceId failed, ret[%d]", __func__, devLogicIdSigned), HCCL_E_INTERNAL);
53 17 : EndpointMonitor::GetInstance(devLogicIdSigned).RegisterToEndpointMonitor(devLogicIdSigned, handle);
54 17 : return HCCL_SUCCESS;
55 : }
56 :
57 67 : HcclResult CreateBuiltinEndpoint(const EndpointDesc *endpoint, EndpointHandle *endpointHandle)
58 : {
59 67 : CHK_RET(RefreshEndpointContext(*endpoint));
60 67 : std::unique_ptr<Endpoint> endpointPtr = nullptr;
61 67 : HcclResult ret = Endpoint::CreateEndpoint(*endpoint, endpointPtr);
62 67 : if (ret != HCCL_SUCCESS) {
63 2 : HCCL_ERROR("call Endpoint::CreateEndpoint failed");
64 2 : return ret;
65 : }
66 65 : CHK_PTR_NULL(endpointPtr);
67 65 : ret = endpointPtr->Init();
68 65 : if (ret != HCCL_SUCCESS) {
69 4 : HCCL_ERROR("call endpointPtr->Init failed");
70 4 : return ret;
71 : }
72 :
73 61 : const EndpointHandle handle = reinterpret_cast<EndpointHandle>(endpointPtr.get());
74 61 : CHK_PTR_NULL(handle);
75 61 : EXCEPTION_CATCH(GetEndpointMap().AddEndpoint(handle, std::move(endpointPtr)), return HCCL_E_INTERNAL);
76 61 : *endpointHandle = handle;
77 61 : CHK_RET(RegisterDeviceEndpointMonitorIfNeeded(endpoint, handle));
78 61 : HCCL_INFO("[%s] endpointDesc.protocol [%d] and endpointDesc.loc.locType [%d] create endpointHandle [%p] done.",
79 : __func__, endpoint->protocol, endpoint->loc.locType, handle);
80 61 : return HCCL_SUCCESS;
81 67 : }
82 : } // namespace
83 :
84 23 : HcommResult HcommEndpointGet(EndpointHandle endpointHandle, void **endpoint) // 根据endpointHandle返回Endpoint对象指针
85 : {
86 23 : CHK_PTR_NULL(endpoint);
87 : #ifdef ENABLE_EXPERIMENTAL
88 20 : bool handled = false;
89 20 : CHK_RET(static_cast<HcclResult>(PluginEndpointGet(endpointHandle, endpoint, handled)));
90 20 : if (handled) {
91 1 : return HCCL_SUCCESS;
92 : }
93 : #endif
94 :
95 19 : auto it = GetEndpointMap().GetEndpoint(endpointHandle);
96 19 : CHK_PRT_RET(it == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
97 : HCCL_E_NOT_FOUND);
98 :
99 17 : *endpoint = static_cast<void *>(it);
100 17 : HCCL_INFO("[%s] START. endpointHandle[%p] endpoint[%p].", __func__, static_cast<void*>(endpointHandle), static_cast<void*>(endpoint));
101 17 : return HCCL_SUCCESS;
102 : }
103 :
104 70 : HcommResult HcommEndpointCreate(const EndpointDesc *endpoint, EndpointHandle *endpointHandle)
105 : {
106 : EXCEPTION_HANDLE_BEGIN
107 70 : (void) HcommResMgrInit();
108 77 : CHK_RET(ValidateEndpointDesc(endpoint, endpointHandle));
109 : #ifdef ENABLE_EXPERIMENTAL
110 68 : bool pluginHandled = false;
111 68 : CHK_RET(static_cast<HcclResult>(PluginEndpointCreate(endpoint, endpointHandle, pluginHandled)));
112 68 : if (pluginHandled) {
113 1 : HCCL_INFO("[NicPluginDebug][%s] plugin endpoint created, protocol[%d], handle[%p].", __func__,
114 : endpoint->protocol, *endpointHandle);
115 1 : return HCCL_SUCCESS;
116 : }
117 : #endif
118 67 : CHK_RET(CreateBuiltinEndpoint(endpoint, endpointHandle));
119 61 : HcommResMgr::RegisterDeviceResetCallback();
120 0 : EXCEPTION_HANDLE_END
121 61 : return HCCL_SUCCESS;
122 : }
123 :
124 35 : HcommResult HcommEndpointDestroy(EndpointHandle endpointHandle)
125 : {
126 35 : (void)HcommResMgrInit();
127 35 : HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
128 : #ifdef ENABLE_EXPERIMENTAL
129 35 : bool handled = false;
130 35 : CHK_RET(static_cast<HcclResult>(PluginEndpointDestroy(endpointHandle, handled)));
131 35 : if (handled) {
132 1 : return HCCL_SUCCESS;
133 : }
134 : #endif
135 :
136 34 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
137 34 : if (endpoint != nullptr) {
138 32 : CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
139 : }
140 34 : s32 devLogicIdSigned = HcclGetThreadDeviceId();
141 34 : CHK_PRT_RET(devLogicIdSigned < 0,
142 : HCCL_ERROR("[%s] HcclGetThreadDeviceId failed, ret[%d]", __func__, devLogicIdSigned), HCCL_E_INTERNAL);
143 34 : EndpointMonitor::GetInstance(devLogicIdSigned).RemoveEpHandleFromEndpointMonitor(endpointHandle);
144 34 : auto ret = GetEndpointMap().RemoveEndpoint(endpointHandle);
145 34 : CHK_PRT_RET(ret == false, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),
146 : HCCL_E_NOT_FOUND);
147 32 : return HCCL_SUCCESS;
148 : }
149 :
150 12 : HcommResult HcommEndpointStartListen(EndpointHandle endpointHandle, uint32_t port, HcommEndpointListenConfig *config)
151 : {
152 : (void)config;
153 12 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
154 12 : CHK_PRT_RET(endpoint == nullptr,
155 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
156 10 : CHK_RET(endpoint->ServerSocketListen(port));
157 10 : return HCCL_SUCCESS;
158 : }
159 :
160 4 : HcommResult HcommEndpointStopListen(EndpointHandle endpointHandle, uint32_t port)
161 : {
162 4 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
163 4 : CHK_PRT_RET(endpoint == nullptr,
164 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
165 2 : CHK_RET(endpoint->ServerSocketStopListen(port));
166 2 : return HCCL_SUCCESS;
167 : }
168 :
169 6 : HcommResult HcommEndpointGetListenPort(EndpointHandle endpointHandle, uint32_t *port)
170 : {
171 6 : CHK_PTR_NULL(port);
172 4 : (void)HcommResMgrInit();
173 : #ifdef ENABLE_EXPERIMENTAL
174 4 : if (IsPluginEndpoint(endpointHandle)) {
175 1 : return HCCL_E_NOT_SUPPORT;
176 : }
177 : #endif
178 :
179 3 : auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
180 3 : CHK_PRT_RET(endpoint == nullptr,
181 : HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle), HCCL_E_NOT_FOUND);
182 1 : CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
183 1 : return endpoint->ServerSocketGetListenPort(port);
184 : }
185 :
186 1 : HcommResult HcommEndpointCheckFeature(
187 : HcommEndpointFeatureType featureType, const EndpointDesc *endpointDesc, bool *value)
188 : {
189 1 : CHK_PTR_NULL(endpointDesc);
190 1 : CHK_PTR_NULL(value);
191 1 : (void)HcommResMgrInit();
192 :
193 1 : return static_cast<HcommResult>(Endpoint::CheckFeature(*endpointDesc, featureType, *value));
194 : }
|