Merged
feat​:host网卡插件机制框架 #4257
gxdjsntAdd on Jul 30
feat​:host网卡插件机制框架 #4257
Merged
gxdjsntAdd on Jul 30
Total: 31 files changed+2672-1978
@@ -49,6 +49,14 @@ enum class HcommChannelKind : uint32_t {
49 AIV_URMA = 8U,49 AIV_URMA = 8U,
50};50};
51 51 
52+// HcommChannelGetStatus 出参状态码
53+enum HcommChannelLinkStatus : int32_t {
54+ HCOMM_CHANNEL_STATUS_READY = 0,
55+ HCOMM_CHANNEL_STATUS_CONNECTING = 1,
56+ HCOMM_CHANNEL_STATUS_FAILED = 2,
57+ HCOMM_CHANNEL_STATUS_TIMEOUT = 3,
58+};
59+ 
52/**60/**
53 * @note 职责:一个EndPointPair上的建立的通信通道的C++抽象接口类声明。61 * @note 职责:一个EndPointPair上的建立的通信通道的C++抽象接口类声明。
54 * 管理该通信通道Channel对上的同步信号Notify、通信队列(如qp、jetty等)等资源管理,负责建立连接,以及注册内存、同步信号等的交换。62 * 管理该通信通道Channel对上的同步信号Notify、通信队列(如qp、jetty等)等资源管理,负责建立连接,以及注册内存、同步信号等的交换。
@@ -28,10 +28,58 @@ public:
28 using EndpointT = typename Traits::EndpointT;28 using EndpointT = typename Traits::EndpointT;
29 using ChannelT = typename Traits::ChannelT;29 using ChannelT = typename Traits::ChannelT;
30 30 
31- static int32_t InitEndpoint(void* endpointCtx)31+ // ==================== Endpoint Ops ====================
32+ 
33+ static int32_t InitEndpoint(void* ctx)
32 {34 {
33- CHK_PTR_NULL(endpointCtx);35+ CHK_PTR_NULL(ctx);
34- return static_cast<EndpointT*>(endpointCtx)->Init();36+ return static_cast<EndpointT*>(ctx)->Init();
37+ }
38+ 
39+ static int32_t DestroyEndpoint(void* ctx)
40+ {
41+ CHK_PTR_NULL(ctx);
42+ delete static_cast<EndpointT*>(ctx);
43+ return HCCL_SUCCESS;
44+ }
45+ 
46+ static int32_t RegisterMemory(void* ctx, const CommMem* mem, const char* tag, void** handle)
47+ {
48+ CHK_PTR_NULL(ctx);
49+ CHK_PTR_NULL(mem);
50+ CHK_PTR_NULL(handle);
51+ return static_cast<HcclResult>(static_cast<EndpointT*>(ctx)->RegisterMemory(*mem, tag, handle));
52+ }
53+ 
54+ static int32_t UnregisterMemory(void* ctx, void* handle)
55+ {
56+ CHK_PTR_NULL(ctx);
57+ return static_cast<HcclResult>(static_cast<EndpointT*>(ctx)->UnregisterMemory(handle));
58+ }
59+ 
60+ static int32_t MemoryExport(void* ctx, void* handle, void** desc, uint32_t* descLen)
61+ {
62+ CHK_PTR_NULL(ctx);
63+ return static_cast<HcclResult>(static_cast<EndpointT*>(ctx)->MemoryExport(handle, desc, descLen));
64+ }
65+ 
66+ static int32_t MemoryImport(void* ctx, const void* desc, uint32_t descLen, CommMem* outMem)
67+ {
68+ CHK_PTR_NULL(ctx);
69+ return static_cast<HcclResult>(static_cast<EndpointT*>(ctx)->MemoryImport(desc, descLen, outMem));
70+ }
71+ 
72+ static int32_t MemoryUnimport(void* ctx, const void* desc, uint32_t descLen)
73+ {
74+ CHK_PTR_NULL(ctx);
75+ return static_cast<HcclResult>(static_cast<EndpointT*>(ctx)->MemoryUnimport(desc, descLen));
76+ }
77+ 
78+ static int32_t GetListenPort(void* ctx, uint32_t* port)
79+ {
80+ CHK_PTR_NULL(ctx);
81+ CHK_PTR_NULL(port);
82+ return static_cast<HcclResult>(static_cast<EndpointT*>(ctx)->ServerSocketGetListenPort(port));
35 }83 }
36 84 
37 static HcommResult CreateEndpoint(const EndpointDesc* endpoint, void** endpointCtx)85 static HcommResult CreateEndpoint(const EndpointDesc* endpoint, void** endpointCtx)
@@ -44,44 +92,178 @@ public:
44 return HCCL_SUCCESS;92 return HCCL_SUCCESS;
45 }93 }
46 94 
47- static void DestroyEndpoint(void* endpointCtx) { delete static_cast<EndpointT*>(endpointCtx); }95+ // ==================== Channel Ops ====================
48 96 
49- static HcommResult MemReg(void* endpointCtx, const CommMem* mem, const char* memTag, void** memHandle)97+ static int32_t InitChannel(void* ctx)
50 {98 {
51- CHK_PTR_NULL(endpointCtx);99+ CHK_PTR_NULL(ctx);
52- CHK_PTR_NULL(mem);100+ return static_cast<ChannelT*>(ctx)->Init();
53- CHK_PTR_NULL(memHandle);
54- return static_cast<EndpointT*>(endpointCtx)->RegisterMemory(*mem, memTag, memHandle);
55 }101 }
56 102 
57- static HcommResult MemUnreg(void* endpointCtx, void* memHandle)103+ static int32_t DestroyChannel(void* ctx)
58 {104 {
59- CHK_PTR_NULL(endpointCtx);105+ CHK_PTR_NULL(ctx);
60- return static_cast<EndpointT*>(endpointCtx)->UnregisterMemory(memHandle);106+ delete static_cast<ChannelT*>(ctx);
107+ return HCCL_SUCCESS;
61 }108 }
62 109 
63- static HcommResult MemExport(void* endpointCtx, void* memHandle, void** memDesc, uint32_t* memDescLen)110+ static int32_t GetStatus(void* ctx, int32_t* status)
64 {111 {
65- CHK_PTR_NULL(endpointCtx);112+ CHK_PTR_NULL(ctx);
66- return static_cast<EndpointT*>(endpointCtx)->MemoryExport(memHandle, memDesc, memDescLen);113+ CHK_PTR_NULL(status);
114+ auto channelStatus = static_cast<ChannelT*>(ctx)->GetStatus();
115+ 
116+ switch (channelStatus) {
117+ case ChannelStatus::FAILED:
118+ *status = HCOMM_CHANNEL_STATUS_FAILED;
119+ break;
120+ case ChannelStatus::SOCKET_TIMEOUT:
121+ *status = HCOMM_CHANNEL_STATUS_TIMEOUT;
122+ break;
123+ case ChannelStatus::READY:
124+ *status = HCOMM_CHANNEL_STATUS_READY;
125+ break;
126+ default:
127+ *status = HCOMM_CHANNEL_STATUS_CONNECTING;
128+ break;
129+ }
130+ 
131+ return HCCL_SUCCESS;
67 }132 }
68 133 
69- static HcommResult MemImport(void* endpointCtx, const void* memDesc, uint32_t descLen, CommMem* outMem)134+ // write
135+ static int32_t WriteNbi(void* ctx, void* dst, const void* src, uint64_t len)
70 {136 {
71- CHK_PTR_NULL(endpointCtx);137+ CHK_PTR_NULL(ctx);
72- return static_cast<EndpointT*>(endpointCtx)->MemoryImport(memDesc, descLen, outMem);138+ return static_cast<ChannelT*>(ctx)->Write(dst, src, len);
73 }139 }
74 140 
75- static HcommResult MemUnimport(void* endpointCtx, const void* memDesc, uint32_t descLen)141+ static int32_t WriteNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len)
76 {142 {
77- CHK_PTR_NULL(endpointCtx);143+ (void)thread;
78- return static_cast<EndpointT*>(endpointCtx)->MemoryUnimport(memDesc, descLen);144+ CHK_PTR_NULL(ctx);
145+ return static_cast<ChannelT*>(ctx)->Write(dst, src, len);
79 }146 }
80 147 
81- static int32_t InitChannel(void* channelCtx)148+ // writeWithNotify
149+ static int32_t WriteWithNotifyNbi(void* ctx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx)
82 {150 {
83- CHK_PTR_NULL(channelCtx);151+ CHK_PTR_NULL(ctx);
84- return static_cast<ChannelT*>(channelCtx)->Init();152+ return static_cast<ChannelT*>(ctx)->WriteWithNotify(dst, src, len, remoteNotifyIdx);
153+ }
154+ 
155+ static int32_t WriteWithNotifyNbiOnThread(
156+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx)
157+ {
158+ (void)thread;
159+ CHK_PTR_NULL(ctx);
160+ return static_cast<ChannelT*>(ctx)->WriteWithNotify(dst, src, len, remoteNotifyIdx);
161+ }
162+ 
163+ // writeReduce
164+ 
165+ // read
166+ static int32_t ReadNbi(void* ctx, void* dst, const void* src, uint64_t len)
167+ {
168+ CHK_PTR_NULL(ctx);
169+ return static_cast<ChannelT*>(ctx)->Read(dst, src, len);
170+ }
171+ 
172+ static int32_t ReadNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len)
173+ {
174+ (void)thread;
175+ CHK_PTR_NULL(ctx);
176+ return static_cast<ChannelT*>(ctx)->Read(dst, src, len);
177+ }
178+ 
179+ // notify
180+ static int32_t NotifyRecord(void* ctx, uint32_t remoteNotifyIdx)
181+ {
182+ CHK_PTR_NULL(ctx);
183+ return static_cast<ChannelT*>(ctx)->NotifyRecord(remoteNotifyIdx);
184+ }
185+ 
186+ static int32_t NotifyRecordOnThread(void* ctx, ThreadHandle thread, uint32_t remoteNotifyIdx)
187+ {
188+ (void)thread;
189+ CHK_PTR_NULL(ctx);
190+ return static_cast<ChannelT*>(ctx)->NotifyRecord(remoteNotifyIdx);
191+ }
192+ 
193+ static int32_t NotifyWait(void* ctx, uint32_t localNotifyIdx, uint32_t timeout)
194+ {
195+ CHK_PTR_NULL(ctx);
196+ return static_cast<ChannelT*>(ctx)->NotifyWait(localNotifyIdx, timeout);
197+ }
198+ 
199+ static int32_t NotifyWaitOnThread(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeout)
200+ {
201+ (void)thread;
202+ CHK_PTR_NULL(ctx);
203+ return static_cast<ChannelT*>(ctx)->NotifyWait(localNotifyIdx, timeout);
204+ }
205+ 
206+ // batch / fence / drain
207+ 
208+ static int32_t Fence(void* ctx)
209+ {
210+ CHK_PTR_NULL(ctx);
211+ return static_cast<ChannelT*>(ctx)->ChannelFence();
212+ }
213+ 
214+ static int32_t FenceOnThread(void* ctx, ThreadHandle thread)
215+ {
216+ (void)thread;
217+ CHK_PTR_NULL(ctx);
218+ return static_cast<ChannelT*>(ctx)->ChannelFence();
219+ }
220+ 
221+ // ==================== Ops Tables ====================
222+ 
223+ static HcommNicEndpointOps EndpointOps()
224+ {
225+ return {
226+ {HCOMM_NIC_ENDPOINT_OPS_VERSION, HCOMM_NIC_ENDPOINT_OPS_MAGIC_WORD, sizeof(HcommNicEndpointOps), 0},
227+ InitEndpoint, // init
228+ DestroyEndpoint, // destroy
229+ RegisterMemory, // registerMemory
230+ UnregisterMemory, // unregisterMemory
231+ MemoryExport, // memoryExport
232+ MemoryImport, // memoryImport
233+ MemoryUnimport, // memoryUnimport
234+ GetListenPort, // getListenPort
235+ };
236+ }
237+ 
238+ static HcommNicChannelOps ChannelOps()
239+ {
240+ return {
241+ {HCOMM_NIC_CHANNEL_OPS_VERSION, HCOMM_NIC_CHANNEL_OPS_MAGIC_WORD, sizeof(HcommNicChannelOps), 0},
242+ InitChannel, // init
243+ DestroyChannel, // destroy
244+ GetStatus, // getStatus
245+ WriteNbi, // writeNbi
246+ WriteNbiOnThread, // writeNbiOnThread
247+ nullptr, // writeOnThread
248+ WriteWithNotifyNbi, // writeWithNotifyNbi
249+ WriteWithNotifyNbiOnThread, // writeWithNotifyNbiOnThread
250+ nullptr, // writeWithNotifyOnThread
251+ nullptr, // writeReduceOnThread
252+ nullptr, // writeReduceWithNotifyOnThread
253+ ReadNbi, // readNbi
254+ ReadNbiOnThread, // readNbiOnThread
255+ nullptr, // readOnThread
256+ nullptr, // readReduceOnThread
257+ NotifyRecord, // notifyRecord
258+ NotifyRecordOnThread, // notifyRecordOnThread
259+ NotifyWait, // notifyWait
260+ NotifyWaitOnThread, // notifyWaitOnThread
261+ nullptr, // notifyWaitOnThreadWithDefaultTimeout
262+ nullptr, // batchTransferOnThread
263+ Fence, // fence
264+ FenceOnThread, // fenceOnThread
265+ nullptr, // drainOnThread
266+ };
85 }267 }
86 268 
87 static HcommResult CreateChannel(void* endpointCtx, const HcommChannelDesc* channelDesc, void** channelCtx)269 static HcommResult CreateChannel(void* endpointCtx, const HcommChannelDesc* channelDesc, void** channelCtx)
@@ -96,104 +278,7 @@ public:
96 return HCCL_SUCCESS;278 return HCCL_SUCCESS;
97 }279 }
98 280 
99- static void DestroyChannel(void* channelCtx) { delete static_cast<ChannelT*>(channelCtx); }281+ // ==================== Export Helpers ====================
100- 
101- static HcommResult GetStatus(void* channelCtx, int32_t* status)
102- {
103- CHK_PTR_NULL(channelCtx);
104- CHK_PTR_NULL(status);
105- auto channelStatus = static_cast<ChannelT*>(channelCtx)->GetStatus();
106- *status = (channelStatus == ChannelStatus::READY) ? 0 : 1;
107- if (channelStatus == ChannelStatus::FAILED || channelStatus == ChannelStatus::SOCKET_TIMEOUT) {
108- return HCCL_E_NETWORK;
109- }
110- return HCCL_SUCCESS;
111- }
112- 
113- static HcommResult GetNotifyNum(void* channelCtx, uint32_t* notifyNum)
114- {
115- CHK_PTR_NULL(channelCtx);
116- return static_cast<ChannelT*>(channelCtx)->GetNotifyNum(notifyNum);
117- }
118- 
119- static HcommResult GetUserRemoteMem(void* channelCtx, CommMem** remoteMem, char*** memTags, uint32_t* memNum)
120- {
121- return Traits::GetUserRemoteMem(static_cast<ChannelT*>(channelCtx), remoteMem, memTags, memNum);
122- }
123- 
124- static HcommResult UpdateMemInfo(void* channelCtx, HcommMemHandle* memHandles, uint32_t memHandleNum)
125- {
126- CHK_PTR_NULL(channelCtx);
127- return static_cast<ChannelT*>(channelCtx)->UpdateMemInfo(memHandles, memHandleNum);
128- }
129- 
130- static HcommResult NotifyRecord(void* channelCtx, uint32_t remoteNotifyIdx)
131- {
132- CHK_PTR_NULL(channelCtx);
133- return static_cast<ChannelT*>(channelCtx)->NotifyRecord(remoteNotifyIdx);
134- }
135- 
136- static HcommResult NotifyWait(void* channelCtx, uint32_t localNotifyIdx, uint32_t timeout)
137- {
138- CHK_PTR_NULL(channelCtx);
139- return static_cast<ChannelT*>(channelCtx)->NotifyWait(localNotifyIdx, timeout);
140- }
141- 
142- static HcommResult
143- WriteWithNotify(void* channelCtx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx)
144- {
145- CHK_PTR_NULL(channelCtx);
146- return static_cast<ChannelT*>(channelCtx)->WriteWithNotify(dst, src, len, remoteNotifyIdx);
147- }
148- 
149- static HcommResult Write(void* channelCtx, void* dst, const void* src, uint64_t len)
150- {
151- CHK_PTR_NULL(channelCtx);
152- return static_cast<ChannelT*>(channelCtx)->Write(dst, src, len);
153- }
154- 
155- static HcommResult Read(void* channelCtx, void* dst, const void* src, uint64_t len)
156- {
157- CHK_PTR_NULL(channelCtx);
158- return static_cast<ChannelT*>(channelCtx)->Read(dst, src, len);
159- }
160- 
161- static HcommResult Fence(void* channelCtx)
162- {
163- CHK_PTR_NULL(channelCtx);
164- return static_cast<ChannelT*>(channelCtx)->ChannelFence();
165- }
166- 
167- static HcommNicEndpointOps EndpointOps()
168- {
169- return {
170- {HCOMM_NIC_ENDPOINT_OPS_VERSION, HCOMM_NIC_ENDPOINT_OPS_MAGIC_WORD, sizeof(HcommNicEndpointOps), 0},
171- InitEndpoint,
172- MemReg,
173- MemUnreg,
174- MemExport,
175- MemImport,
176- MemUnimport,
177- DestroyEndpoint};
178- }
179- 
180- static HcommNicChannelOps ChannelOps()
181- {
182- return {
183- {HCOMM_NIC_CHANNEL_OPS_VERSION, HCOMM_NIC_CHANNEL_OPS_MAGIC_WORD, sizeof(HcommNicChannelOps), 0},
184- InitChannel,
185- GetNotifyNum,
186- Write,
187- WriteWithNotify,
188- Read,
189- NotifyRecord,
190- NotifyWait,
191- Fence,
192- DestroyChannel,
193- GetStatus,
194- UpdateMemInfo,
195- GetUserRemoteMem};
196- }
197 282 
198 static int32_t CreateEndpointExport(283 static int32_t CreateEndpointExport(
199 const EndpointDesc* endpointDesc, void** outCtx, HcommNicEndpointOps** outOps, HcommNicEndpointOps* endpointOps)284 const EndpointDesc* endpointDesc, void** outCtx, HcommNicEndpointOps** outOps, HcommNicEndpointOps* endpointOps)
@@ -21,9 +21,7 @@ set(src_list
21 hccl_comm_socket_c_adpt.cc21 hccl_comm_socket_c_adpt.cc
22)22)
23 23 
24-if(ENABLE_EXPERIMENTAL)24+add_subdirectory(nic_plugin)
25- add_subdirectory(nic_plugin)
26-endif()
27 25
28if(TARGET hcomm)26if(TARGET hcomm)
29 target_sources(hcomm PRIVATE27 target_sources(hcomm PRIVATE
@@ -35,12 +33,10 @@ if(TARGET hcomm)
35 ${PROJECT_SOURCE_DIR}/src/base_comm/resources/endpoint_pairs/channels/aiv33 ${PROJECT_SOURCE_DIR}/src/base_comm/resources/endpoint_pairs/channels/aiv
36 ${PROJECT_SOURCE_DIR}/src/base_comm/resources/endpoints/dfx34 ${PROJECT_SOURCE_DIR}/src/base_comm/resources/endpoints/dfx
37 )35 )
38- if(ENABLE_EXPERIMENTAL)36+ target_compile_definitions(hcomm PRIVATE ENABLE_EXPERIMENTAL)
39- target_compile_definitions(hcomm PRIVATE ENABLE_EXPERIMENTAL)37+ target_include_directories(hcomm PRIVATE
40- target_include_directories(hcomm PRIVATE38+ ${CMAKE_CURRENT_SOURCE_DIR}/nic_plugin
41- ${CMAKE_CURRENT_SOURCE_DIR}/nic_plugin39+ )
42- )
43- endif()
44endif()40endif()
45 41 
46if(TARGET ccl_kernel)42if(TARGET ccl_kernel)
@@ -53,5 +49,6 @@ if(TARGET ccl_kernel)
53 )49 )
54 target_include_directories(ccl_kernel PRIVATE50 target_include_directories(ccl_kernel PRIVATE
55 ${CMAKE_CURRENT_SOURCE_DIR}51 ${CMAKE_CURRENT_SOURCE_DIR}
52+ ${CMAKE_CURRENT_SOURCE_DIR}/nic_plugin
56 )53 )
57endif()54endif()
@@ -17,16 +17,12 @@
17#include "host/host_cpu_roce_channel.h"17#include "host/host_cpu_roce_channel.h"
18#include "hccl_comm_pub.h"18#include "hccl_comm_pub.h"
19#include "op_base.h"19#include "op_base.h"
20-#include "dlprof_function.h"
21#include "hcclCommOp.h"20#include "hcclCommOp.h"
22#include "adapter_prof.h"21#include "adapter_prof.h"
23#include "hccl_diag.h"22#include "hccl_diag.h"
24#include "exception_handler.h"23#include "exception_handler.h"
25-#include "task_info.h"
26#include "task_param.h"24#include "task_param.h"
27-#ifdef ENABLE_EXPERIMENTAL25+#include "nic_plugin_manager.h"
28-#include "nic_plugin_dispatcher.h"
29-#endif
30 26 
31using namespace hccl;27using namespace hccl;
32thread_local LaunchContext g_threadLaunchCtx;28thread_local LaunchContext g_threadLaunchCtx;
@@ -20,15 +20,95 @@
20#include "comm_engine_utils.h"20#include "comm_engine_utils.h"
21#include "channel_process.h"21#include "channel_process.h"
22#include "aicpu_ts_channel_helper.h"22#include "aicpu_ts_channel_helper.h"
23-#ifdef ENABLE_EXPERIMENTAL
24-#include "nic_plugin_dispatcher.h"
25-#endif
26#include "channel_config.h"23#include "channel_config.h"
27#include "shared_jetty_mgr.h"24#include "shared_jetty_mgr.h"
28#include "endpoint.h"25#include "endpoint.h"
26+#include "builtin_endpoint_ops.h"
27+#include "nic_plugin_holder.h"
28+#include "nic_plugin_manager.h"
29 29 
30using namespace hcomm;30using namespace hcomm;
31 31 
32+namespace {
33+void DestroyPluginCtx(HcommNicChannelOps* ops, void* pluginCtx)
34+{
35+ if (ops != nullptr && ops->destroy != nullptr) {
36+ int32_t ret = ops->destroy(pluginCtx);
37+ if (ret != HCCL_SUCCESS) {
38+ HCCL_WARNING("[%s] plugin channel destroy failed, ret[%d].", __func__, ret);
39+ }
40+ }
41+}
42+ 
43+void RollbackPluginChannels(ChannelHandle* channels, uint32_t count)
44+{
45+ for (uint32_t i = 0; i < count; ++i) {
46+ if (channels[i] == 0)
47+ continue;
48+ auto* ch = CHANNEL_FROM_HANDLE(channels[i]);
49+ if (ch != nullptr) {
50+ HcclResult ret = ChannelProcess::RemovePluginChannelFromMap(reinterpret_cast<ChannelHandle>(ch));
51+ if (ret != HCCL_SUCCESS) {
52+ HCCL_WARNING(
53+ "[%s] plugin channel not found in map during rollback, handle[0x%llx], ret[%d].", __func__,
54+ channels[i], ret);
55+ }
56+ }
57+ channels[i] = 0;
58+ }
59+}
60+ 
61+HcommResult CreateOnePluginChannel(
62+ const NicPluginEntry* entry, void* epCtx, HcommChannelDesc* channelDesc, ChannelHandle* outChannel)
63+{
64+ *outChannel = 0;
65+ 
66+ void* pluginCtx = nullptr;
67+ HcommNicChannelOps* pluginOps = nullptr;
68+ HcommResult ret = static_cast<HcommResult>(entry->createChannel(epCtx, channelDesc, &pluginCtx, &pluginOps));
69+ CHK_PRT_RET(
70+ (ret != HCCL_SUCCESS), HCCL_ERROR("[NicPlugin][%s] createChannel failed, ret[%d].", __func__, ret), ret);
71+ 
72+ if (!ValidateChannelOps(pluginOps)) {
73+ HCCL_ERROR("[NicPlugin][%s] invalid channel ops.", __func__);
74+ DestroyPluginCtx(pluginOps, pluginCtx);
75+ return HCCL_E_INTERNAL;
76+ }
77+ 
78+ HcommNicChannelOps* filledOps = nullptr;
79+ ret = FillDefaultChannelOps(pluginOps, &filledOps);
80+ if (ret != HCCL_SUCCESS) {
81+ HCCL_ERROR("[NicPlugin][%s] FillDefaultChannelOps failed, ret[%d].", __func__, ret);
82+ DestroyPluginCtx(pluginOps, pluginCtx);
83+ return ret;
84+ }
85+ 
86+ ret = static_cast<HcommResult>(filledOps->init(pluginCtx));
87+ if (ret != HCCL_SUCCESS) {
88+ int32_t destroyRet = filledOps->destroy(pluginCtx);
89+ if (destroyRet != HCCL_SUCCESS) {
90+ HCCL_WARNING("[%s] plugin channel destroy failed after init failure, ret[%d].", __func__, destroyRet);
91+ }
92+ delete filledOps;
93+ HCCL_ERROR("[NicPlugin][%s] plugin channel init failed, ret[%d].", __func__, ret);
94+ return ret;
95+ }
96+ 
97+ auto holder = std::make_shared<hcomm::PluginChannelHolder>(entry);
98+ holder->SetNicChannelCtx(filledOps, pluginCtx);
99+ ChannelHandle handle = reinterpret_cast<ChannelHandle>(holder.get());
100+ 
101+ ret = static_cast<HcommResult>(ChannelProcess::InsertPluginChannelToMap(handle, std::move(holder)));
102+ CHK_PRT_RET(
103+ (ret != HCCL_SUCCESS), HCCL_ERROR("[NicPlugin][%s] InsertChannelToMap failed, ret[%d].", __func__, ret), ret);
104+ 
105+ *outChannel = MAKE_PLUGIN_CH_HANDLE(handle);
106+ HCCL_INFO("[%s] plugin channel created, handle[0x%llx].", __func__, handle);
107+ return HCCL_SUCCESS;
108+}
109+ 
110+} // namespace
111+ 
32HcommResult CheckUbAttr(HcommChannelDesc& channelDesc, CommEngine engine)112HcommResult CheckUbAttr(HcommChannelDesc& channelDesc, CommEngine engine)
33{113{
34 if (channelDesc.remoteEndpoint.protocol != COMM_PROTOCOL_UBC_TP114 if (channelDesc.remoteEndpoint.protocol != COMM_PROTOCOL_UBC_TP
@@ -243,17 +323,29 @@ HcommResult HcommChannelUpdateMemInfo(HcommMemHandle* memHandles, uint32_t memHa
243{323{
244 CHK_PTR_NULL(memHandles);324 CHK_PTR_NULL(memHandles);
245 CHK_PRT_RET((memHandleNum == 0), HCCL_ERROR("[%s]Invalid memHandleNum, memHandleNum is 0.", __func__), HCCL_E_PARA);325 CHK_PRT_RET((memHandleNum == 0), HCCL_ERROR("[%s]Invalid memHandleNum, memHandleNum is 0.", __func__), HCCL_E_PARA);
246-#ifdef ENABLE_EXPERIMENTAL
247- bool handled = false;
248- CHK_RET(static_cast<HcclResult>(PluginChannelUpdateMemInfo(channelHandle, memHandles, memHandleNum, handled)));
249- if (handled) {
250- return HCCL_SUCCESS;
251- }
252-#endif
253- 
254 return ChannelProcess::ChannelUpdateMemInfo(memHandles, memHandleNum, channelHandle);326 return ChannelProcess::ChannelUpdateMemInfo(memHandles, memHandleNum, channelHandle);
255}327}
256 328 
329+HcommResult CreatePluginChannels(
330+ hcomm::Endpoint* endpoint, HcommChannelDesc* channelDescs, uint32_t channelNum, ChannelHandle* channels)
331+{
332+ auto* epHolder = dynamic_cast<hcomm::PluginEndpointHolder*>(endpoint);
333+ CHK_PTR_NULL(epHolder);
334+ const NicPluginEntry* entry = epHolder->GetPluginEntry();
335+ CHK_PTR_NULL(entry);
336+ void* epCtx = endpoint->GetNicCtx();
337+ 
338+ for (uint32_t idx = 0; idx < channelNum; ++idx) {
339+ HcommResult ret = CreateOnePluginChannel(entry, epCtx, &channelDescs[idx], &channels[idx]);
340+ if (ret != HCCL_SUCCESS) {
341+ (void)RollbackPluginChannels(channels, idx);
342+ return ret;
343+ }
344+ }
345+ 
346+ return HCCL_SUCCESS;
347+}
348+ 
257HcommResult HcommChannelCreate(349HcommResult HcommChannelCreate(
258 EndpointHandle endpointHandle, CommEngine engine, HcommChannelDesc* channelDescs, uint32_t channelNum,350 EndpointHandle endpointHandle, CommEngine engine, HcommChannelDesc* channelDescs, uint32_t channelNum,
259 ChannelHandle* channels)351 ChannelHandle* channels)
@@ -266,23 +358,19 @@ HcommResult HcommChannelCreate(
266 HCCL_INFO(358 HCCL_INFO(
267 "[%s] START. endpointHandle[0x%llx], engine[%s], channelNum[%u].", __func__, endpointHandle,359 "[%s] START. endpointHandle[0x%llx], engine[%s], channelNum[%u].", __func__, endpointHandle,
268 GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), channelNum);360 GetEnumToString(GetCommEngineStatusStrMap(), engine).c_str(), channelNum);
361+ 
362+ std::vector<HcommChannelDesc> channelDescFinals;
363+ CHK_RET(static_cast<HcclResult>(NormalizeHcommChannelDescs(channelDescs, channelNum, channelDescFinals, engine)));
269 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);364 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
365+ if (endpoint != nullptr && endpoint->GetNicOps() != nullptr && endpoint->GetNicOps() != &g_BuiltinEndpointOps) {
366+ CHK_RET(
367+ static_cast<HcclResult>(CreatePluginChannels(endpoint, channelDescFinals.data(), channelNum, channels)));
368+ return HCCL_SUCCESS;
369+ }
370+ (void)HcommResMgrInit();
270 if (endpoint != nullptr) {371 if (endpoint != nullptr) {
271 CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));372 CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
272 }373 }
273- (void)HcommResMgrInit();
274- std::vector<HcommChannelDesc> channelDescFinals;
275- CHK_RET(static_cast<HcclResult>(NormalizeHcommChannelDescs(channelDescs, channelNum, channelDescFinals, engine)));
276- 
277-#ifdef ENABLE_EXPERIMENTAL
278- bool pluginHandled = false;
279- CHK_RET(static_cast<HcclResult>(
280- PluginChannelCreate(endpointHandle, engine, channelDescFinals.data(), channelNum, channels, pluginHandled)));
281- if (pluginHandled) {
282- return HCCL_SUCCESS;
283- }
284-#endif
285- 
286 std::vector<ChannelHandle> hostChannelHandles(channelNum);374 std::vector<ChannelHandle> hostChannelHandles(channelNum);
287 ChannelHandle* targetChannels = hostChannelHandles.data();375 ChannelHandle* targetChannels = hostChannelHandles.data();
288 376 
@@ -297,13 +385,6 @@ HcommResult HcommChannelCreate(
297HcommResult HcommChannelGet(ChannelHandle channelHandle, void** channel)385HcommResult HcommChannelGet(ChannelHandle channelHandle, void** channel)
298{386{
299 CHK_PTR_NULL(channel);387 CHK_PTR_NULL(channel);
300-#ifdef ENABLE_EXPERIMENTAL
301- bool handled = false;
302- CHK_RET(static_cast<HcclResult>(PluginChannelGet(channelHandle, channel, handled)));
303- if (handled) {
304- return HCCL_SUCCESS;
305- }
306-#endif
307 return ChannelProcess::ChannelGet(channelHandle, channel);388 return ChannelProcess::ChannelGet(channelHandle, channel);
308}389}
309 390 
@@ -312,63 +393,47 @@ HcommResult HcommChannelGetStatus(const ChannelHandle* channelList, uint32_t lis
312 CHK_PTR_NULL(channelList);393 CHK_PTR_NULL(channelList);
313 CHK_PTR_NULL(statusList);394 CHK_PTR_NULL(statusList);
314 CHK_PRT_RET((listNum == 0), HCCL_ERROR("[%s]Invalid listNum, listNum[%u]", __func__, listNum), HCCL_E_PARA);395 CHK_PRT_RET((listNum == 0), HCCL_ERROR("[%s]Invalid listNum, listNum[%u]", __func__, listNum), HCCL_E_PARA);
315- (void)HcommResMgrInit();396+ 
316-#ifdef ENABLE_EXPERIMENTAL397+ if (IS_PLUGIN_HANDLE(channelList[0])) {
严正行

[MEDIUM] HcommChannelGetStatus 和 HcommChannelDestroy 仅检查 channelList[0]/channels[0] 判断是否为 plugin channel

问题分析:

  • 第 352 行:if (IS_PLUGIN_HANDLE(channelList[0])) 判断第一个 channel,若为 plugin 则全部走 plugin 路径
  • 第 418 行同理
  • 若 channelList 混合 plugin channel 和 builtin channel,plugin 路径会对 builtin channel 调 CHANNEL_FROM_HANDLE + GetNicOps(),得到错误指针
  • 虽然当前同一批 channel 通常来自同一 endpoint,但此假设未显式验证

建议修复: 逐个检查每个 channel 的 IS_PLUGIN_HANDLE,或至少在注释中明确说明同一批 channel 必须全为 plugin 或全为 builtin 的契约。

likedislike
gxdjsnt
gxdjsnt
Aug 8 Comments:
likedislike
317- bool allHandled = true;398+ for (uint32_t i = 0; i < listNum; i++) {
318- for (uint32_t i = 0; i < listNum; i++) {399+ auto* ch = CHANNEL_FROM_HANDLE(channelList[i]);
319- bool handled = false;400+ CHK_PTR_NULL(ch);
320- CHK_RET(static_cast<HcclResult>(PluginChannelGetStatus(channelList[i], &statusList[i], handled)));401+ int32_t status = 0;
321- if (!handled) {402+ HcommResult ret = static_cast<HcommResult>(ch->GetNicOps()->getStatus(ch->GetNicCtx(), &status));
322- allHandled = false;403+ if (ret != HCCL_SUCCESS) {
404+ HCCL_ERROR("[%s] plugin getStatus failed, idx[%u], ret[%d].", __func__, i, ret);
405+ return ret;
406+ }
407+ statusList[i] = status;
408+ }
409+ return HCCL_SUCCESS;
410+ } else {
411+ (void)HcommResMgrInit();
412+ std::vector<CommEngine> engines;
413+ std::vector<HcommChannelDesc> channelDescFinals;
414+ std::vector<ChannelStatus> internalStatus(listNum);
415+ HcclResult ret
416+ = ChannelProcess::GetChannelsInfo(channelList, listNum, engines, channelDescFinals, internalStatus);
417+ if (ret != HCCL_SUCCESS) {
418+ HCCL_ERROR("[%s] GetChannelsInfo failed, ret[%d]", __func__, ret);
419+ return HCCL_E_INTERNAL;
420+ }
421+ ret = ChannelProcess::HandleStatusByEngine(
422+ channelList, listNum, engines, channelDescFinals, internalStatus, statusList);
423+ if (ret != HCCL_SUCCESS) {
424+ HCCL_ERROR("[%s] HandleStatusByEngine failed, ret[%d]", __func__, ret);
425+ return HCCL_E_INTERNAL;
323 }426 }
324- }
325- if (allHandled) {
326 return HCCL_SUCCESS;427 return HCCL_SUCCESS;
327 }428 }
328-#endif
329- 
330- std::vector<CommEngine> engines;
331- std::vector<HcommChannelDesc> channelDescFinals;
332- std::vector<ChannelStatus> internalStatus(listNum);
333- HcclResult ret = ChannelProcess::GetChannelsInfo(channelList, listNum, engines, channelDescFinals, internalStatus);
334- if (ret != HCCL_SUCCESS) {
335- HCCL_ERROR("[%s] GetChannelsInfo failed, ret[%d]", __func__, ret);
336- return HCCL_E_INTERNAL;
337- }
338- ret = ChannelProcess::HandleStatusByEngine(
339- channelList, listNum, engines, channelDescFinals, internalStatus, statusList);
340- if (ret != HCCL_SUCCESS) {
341- HCCL_ERROR("[%s] HandleStatusByEngine failed, ret[%d]", __func__, ret);
342- return HCCL_E_INTERNAL;
343- }
344- return HCCL_SUCCESS;
345}429}
346 430 
347HcommResult HcommChannelGetNotifyNum(ChannelHandle channelHandle, uint32_t* notifyNum)431HcommResult HcommChannelGetNotifyNum(ChannelHandle channelHandle, uint32_t* notifyNum)
348{432{
349 CHK_PTR_NULL(notifyNum);433 CHK_PTR_NULL(notifyNum);
350-#ifdef ENABLE_EXPERIMENTAL
351- bool handled = false;
352- CHK_RET(static_cast<HcclResult>(PluginChannelGetNotifyNum(channelHandle, notifyNum, handled)));
353- if (handled) {
354- return HCCL_SUCCESS;
355- }
356-#endif
357 return ChannelProcess::ChannelGetNotifyNum(channelHandle, notifyNum);434 return ChannelProcess::ChannelGetNotifyNum(channelHandle, notifyNum);
358}435}
359 436 
360-#ifdef ENABLE_EXPERIMENTAL
361-static void UnregisterPluginChannels(std::vector<ChannelHandle>& pluginChannels)
362-{
363- // plugin channel 虽由 PluginChannelDestroy 销毁,但创建时同样经 RegisterChannels 注册,
364- // 需在此统一注销,否则 CheckEndpointDestroy 会因残留记录阻止 Endpoint 销毁。
365- // 无论 PluginChannelDestroy 是否失败,已收集的 pluginChannels 都需注销。
366- if (!pluginChannels.empty()) {
367- (void)hcomm::SharedJettyMgr::GetInstance().UnregisterChannels(pluginChannels.data(), pluginChannels.size());
368- }
369-}
370-#endif
371- 
372static HcclResult DestroyBuiltinChannels(std::vector<ChannelHandle>& builtinChannels)437static HcclResult DestroyBuiltinChannels(std::vector<ChannelHandle>& builtinChannels)
373{438{
374 // 即使 plugin channel 销毁失败,也需继续销毁 builtin channel,避免 RDMA/jetty 资源泄漏439 // 即使 plugin channel 销毁失败,也需继续销毁 builtin channel,避免 RDMA/jetty 资源泄漏
@@ -392,55 +457,27 @@ static HcclResult DestroyBuiltinChannels(std::vector<ChannelHandle>& builtinChan
392HcommResult HcommChannelDestroy(const ChannelHandle* channels, uint32_t channelNum)457HcommResult HcommChannelDestroy(const ChannelHandle* channels, uint32_t channelNum)
393{458{
394 CHK_PTR_NULL(channels);459 CHK_PTR_NULL(channels);
395- (void)HcommResMgrInit();
396 CHK_PRT_RET(460 CHK_PRT_RET(
397 (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);461 (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
462+ if (IS_PLUGIN_HANDLE(channels[0])) {
463+ for (uint32_t idx = 0; idx < channelNum; ++idx) {
464+ auto* ch = CHANNEL_FROM_HANDLE(channels[idx]);
465+ HcclResult ret = ChannelProcess::RemovePluginChannelFromMap(reinterpret_cast<ChannelHandle>(ch));
466+ if (ret != HCCL_SUCCESS) {
467+ HCCL_WARNING(
468+ "[%s] plugin channel not found in map during destroy, handle[0x%llx], ret[%d].", __func__,
469+ channels[idx], ret);
470+ }
471+ }
472+ return HCCL_SUCCESS;
473+ }
474+ (void)HcommResMgrInit();
398 std::vector<ChannelHandle> builtinChannels;475 std::vector<ChannelHandle> builtinChannels;
399 builtinChannels.reserve(channelNum);476 builtinChannels.reserve(channelNum);
400-#ifdef ENABLE_EXPERIMENTAL
401- std::vector<ChannelHandle> pluginChannels;
402- pluginChannels.reserve(channelNum);
403- HcclResult pluginRet = HCCL_SUCCESS;
404- for (uint32_t idx = 0; idx < channelNum; ++idx) {
405- bool handled = false;
406- HcclResult curRet = static_cast<HcclResult>(PluginChannelDestroy(channels[idx], handled));
407- if (curRet != HCCL_SUCCESS) {
408- HCCL_ERROR(
409- "[%s] PluginChannelDestroy failed, idx[%u], ret[%d], handled[%d].", __func__, idx, curRet,
410- static_cast<int>(handled));
411- pluginRet = curRet;
412- // 不 break,继续处理剩余 channel,确保全部得到销毁/注销
413- if (handled) {
414- // plugin 已认领 channel 所有权,即使返回错误也由 plugin 负责清理,
415- // 不再走 builtin 路径以免 double-free,但仍需注销 SharedJettyMgr 记录
416- pluginChannels.push_back(channels[idx]);
417- continue;
418- }
419- // handled=false 契约保证 plugin 未修改 channel 状态且应返回 HCCL_SUCCESS;
420- // 此分支为防御性兜底(当前实现不可达),仍按 builtin 路径销毁避免资源泄漏。
421- HCCL_WARNING(
422- "[%s] PluginChannelDestroy returned error with handled=false, idx[%u], ret[%d]. "
423- "Falling back to builtin destroy per contract.",
424- __func__, idx, curRet);
425- } else if (handled) {
426- pluginChannels.push_back(channels[idx]);
427- continue;
428- }
429- // 非 plugin channel 或 plugin 未认领,按 builtin 路径处理
430- builtinChannels.push_back(channels[idx]);
431- }
432- UnregisterPluginChannels(pluginChannels);
433- HcclResult builtinRet = DestroyBuiltinChannels(builtinChannels);
434- if (pluginRet != HCCL_SUCCESS) {
435- return pluginRet;
436- }
437- return builtinRet;
438-#else
439 for (uint32_t idx = 0; idx < channelNum; ++idx) {477 for (uint32_t idx = 0; idx < channelNum; ++idx) {
440 builtinChannels.push_back(channels[idx]);478 builtinChannels.push_back(channels[idx]);
441 }479 }
442 return static_cast<HcommResult>(DestroyBuiltinChannels(builtinChannels));480 return static_cast<HcommResult>(DestroyBuiltinChannels(builtinChannels));
443-#endif
444}481}
445 482 
446HcommResult HcommChannelConfigCreate(HcommChannelConfig* config)483HcommResult HcommChannelConfigCreate(HcommChannelConfig* config)
@@ -478,27 +515,6 @@ static HcclResult ValidateSharedQueueConfig(const std::vector<HcommChannelDesc>&
478 return HCCL_SUCCESS;515 return HCCL_SUCCESS;
479}516}
480 517 
481-#ifdef ENABLE_EXPERIMENTAL
482-static HcclResult
483-RegisterSharedQueuePluginChannels(EndpointHandle endpointHandle, uint32_t channelNum, ChannelHandle* channels)
484-{
485- // plugin channel 不经过 Channel::BuildConnection,无法走共享 jetty 复用路径。
486- // 但仍需注册到 SharedJettyMgr 以便 CheckEndpointDestroy 校验,确保 plugin channel
487- // 销毁后 endpoint 才能销毁(plugin channel 与 endpoint 也有资源依赖关系)。
488- HcclResult regRet = hcomm::SharedJettyMgr::GetInstance().RegisterChannels(endpointHandle, channels, channelNum);
489- if (regRet != HCCL_SUCCESS) {
490- HCCL_ERROR("[%s] failed to register shared jetty channels (plugin), ret[%d].", __func__, regRet);
491- // 注册失败需销毁已创建的 plugin channel,避免泄漏
492- for (uint32_t i = 0; i < channelNum; ++i) {
493- bool handled = false;
494- (void)static_cast<HcclResult>(PluginChannelDestroy(channels[i], handled));
495- }
496- return regRet;
497- }
498- return HCCL_SUCCESS;
499-}
500-#endif
501- 
502static HcclResult CreateAndRegisterSharedQueueBuiltinChannels(518static HcclResult CreateAndRegisterSharedQueueBuiltinChannels(
503 EndpointHandle endpointHandle, CommEngine engine, HcommChannelDesc* channelDescFinals, uint32_t channelNum,519 EndpointHandle endpointHandle, CommEngine engine, HcommChannelDesc* channelDescFinals, uint32_t channelNum,
504 ChannelHandle* channels)520 ChannelHandle* channels)
@@ -571,15 +587,6 @@ HcommResult HcommChannelCreateWithConfig(
571 // NormalizeHcommChannelDescs 内部已调 CheckUbAttr,此处仅补共享模式专有校验587 // NormalizeHcommChannelDescs 内部已调 CheckUbAttr,此处仅补共享模式专有校验
572 CHK_RET(ValidateSharedQueueConfig(channelDescFinals));588 CHK_RET(ValidateSharedQueueConfig(channelDescFinals));
573 589 
574-#ifdef ENABLE_EXPERIMENTAL
575- bool pluginHandled = false;
576- CHK_RET(static_cast<HcclResult>(
577- PluginChannelCreate(endpointHandle, engine, channelDescFinals.data(), channelNum, channels, pluginHandled)));
578- if (pluginHandled) {
579- return static_cast<HcommResult>(RegisterSharedQueuePluginChannels(endpointHandle, channelNum, channels));
580- }
581-#endif
582- 
583 HcclResult ret = CreateAndRegisterSharedQueueBuiltinChannels(590 HcclResult ret = CreateAndRegisterSharedQueueBuiltinChannels(
584 endpointHandle, engine, channelDescFinals.data(), channelNum, channels);591 endpointHandle, engine, channelDescFinals.data(), channelNum, channels);
585 if (ret != HCCL_SUCCESS) {592 if (ret != HCCL_SUCCESS) {
@@ -596,13 +603,6 @@ HcommChannelGetRemoteMems(ChannelHandle channelHandle, uint32_t* memNum, CommMem
596 CHK_PTR_NULL(remoteMem);603 CHK_PTR_NULL(remoteMem);
597 CHK_PTR_NULL(memNum);604 CHK_PTR_NULL(memNum);
598 CHK_PTR_NULL(memInfos);605 CHK_PTR_NULL(memInfos);
599-#ifdef ENABLE_EXPERIMENTAL
600- bool handled = false;
601- CHK_RET(static_cast<HcclResult>(PluginChannelGetRemoteMems(channelHandle, memNum, remoteMem, memInfos, handled)));
602- if (handled) {
603- return HCCL_SUCCESS;
604- }
605-#endif
606 606 
607 return ChannelProcess::ChannelGetRemoteMems(channelHandle, memNum, remoteMem, memInfos);607 return ChannelProcess::ChannelGetRemoteMems(channelHandle, memNum, remoteMem, memInfos);
608}608}
@@ -23,9 +23,9 @@
23#include "channel_config.h"23#include "channel_config.h"
24#include "shared_jetty_mgr.h"24#include "shared_jetty_mgr.h"
25#include "endpoint.h"25#include "endpoint.h"
26-#ifdef ENABLE_EXPERIMENTAL26+#include "builtin_endpoint_ops.h"
27-#include "nic_plugin_dispatcher.h"27+#include "nic_plugin_holder.h"
28-#endif28+#include "nic_plugin_manager.h"
29 29 
30using namespace hcomm;30using namespace hcomm;
31 31 
@@ -59,6 +59,58 @@ HcclResult RegisterDeviceEndpointMonitorIfNeeded(const EndpointDesc* endpoint, E
59 return HCCL_SUCCESS;59 return HCCL_SUCCESS;
60}60}
61 61 
62+HcommResult CreatePluginEndpointHolder(
63+ const EndpointDesc* endpoint, const NicPluginEntry* pluginEntry, EndpointHandle* endpointHandle)
64+{
65+ CHK_PTR_NULL(endpoint);
66+ CHK_PTR_NULL(pluginEntry);
67+ CHK_PTR_NULL(endpointHandle);
68+ void* pluginCtx = nullptr;
69+ HcommNicEndpointOps* pluginOps = nullptr;
70+ HcommResult ret = HCCL_SUCCESS;
71+ ret = static_cast<HcommResult>(pluginEntry->createEndpoint(endpoint, &pluginCtx, &pluginOps));
72+ CHK_PRT_RET(
73+ ret != HCCL_SUCCESS,
74+ HCCL_ERROR(
75+ "[NicPlugin][%s] plugin createEndpoint failed, ret[%d], protocol[%d].", __func__, ret, endpoint->protocol),
76+ ret);
77+ 
78+ CHK_PRT_RET(
79+ !ValidateEndpointOps(pluginOps),
80+ HCCL_ERROR(
81+ "[NicPlugin][%s] plugin endpoint ops validation failed, protocol[%d].", __func__, endpoint->protocol),
82+ HCCL_E_PARA);
83+ 
84+ // 确保HcommNicEndpointOps各接口非空实现,后续调用处无需校验
85+ HcommNicEndpointOps* pluginHolderOps = nullptr;
86+ ret = FillDefaultEndpointOps(pluginOps, &pluginHolderOps);
87+ CHK_PRT_RET(
88+ ret != HCCL_SUCCESS,
89+ HCCL_ERROR(
90+ "[NicPlugin][%s] FillDefaultEndpointOps failed, ret[%d], protocol[%d].", __func__, ret, endpoint->protocol),
91+ ret);
92+ 
93+ ret = static_cast<HcommResult>(pluginHolderOps->init(pluginCtx));
94+ if (ret != HCCL_SUCCESS) {
95+ int32_t destroyRet = pluginHolderOps->destroy(pluginCtx);
96+ if (destroyRet != HCCL_SUCCESS) {
97+ HCCL_WARNING("[%s] plugin endpoint destroy failed after init failure, ret[%d].", __func__, destroyRet);
98+ }
99+ delete pluginHolderOps;
100+ HCCL_ERROR("[NicPlugin][%s] plugin endpoint init failed, ret[%d].", __func__, ret);
101+ return ret;
102+ }
103+ 
104+ auto holder = std::make_unique<PluginEndpointHolder>(*endpoint, pluginEntry);
105+ holder->SetNicEndpointCtx(pluginHolderOps, pluginCtx);
106+ const EndpointHandle handle = reinterpret_cast<EndpointHandle>(holder.get());
107+ EXCEPTION_CATCH(GetEndpointMap().AddEndpoint(handle, std::move(holder)), return HCCL_E_INTERNAL);
108+ *endpointHandle = handle;
109+ HCCL_INFO(
110+ "[NicPlugin][%s] plugin endpoint created, protocol[%d], handle[%p].", __func__, endpoint->protocol, handle);
111+ return HCCL_SUCCESS;
112+}
113+ 
62HcclResult CreateBuiltinEndpoint(const EndpointDesc* endpoint, EndpointHandle* endpointHandle)114HcclResult CreateBuiltinEndpoint(const EndpointDesc* endpoint, EndpointHandle* endpointHandle)
63{115{
64 CHK_RET(RefreshEndpointContext(*endpoint));116 CHK_RET(RefreshEndpointContext(*endpoint));
@@ -75,6 +127,8 @@ HcclResult CreateBuiltinEndpoint(const EndpointDesc* endpoint, EndpointHandle* e
75 return ret;127 return ret;
76 }128 }
77 129 
130+ endpointPtr->SetNicEndpointCtx(&g_BuiltinEndpointOps, endpointPtr.get());
131+ 
78 const EndpointHandle handle = reinterpret_cast<EndpointHandle>(endpointPtr.get());132 const EndpointHandle handle = reinterpret_cast<EndpointHandle>(endpointPtr.get());
79 CHK_PTR_NULL(handle);133 CHK_PTR_NULL(handle);
80 EXCEPTION_CATCH(GetEndpointMap().AddEndpoint(handle, std::move(endpointPtr)), return HCCL_E_INTERNAL);134 EXCEPTION_CATCH(GetEndpointMap().AddEndpoint(handle, std::move(endpointPtr)), return HCCL_E_INTERNAL);
@@ -90,13 +144,6 @@ HcclResult CreateBuiltinEndpoint(const EndpointDesc* endpoint, EndpointHandle* e
90HcommResult HcommEndpointGet(EndpointHandle endpointHandle, void** endpoint) // 根据endpointHandle返回Endpoint对象指针144HcommResult HcommEndpointGet(EndpointHandle endpointHandle, void** endpoint) // 根据endpointHandle返回Endpoint对象指针
91{145{
92 CHK_PTR_NULL(endpoint);146 CHK_PTR_NULL(endpoint);
93-#ifdef ENABLE_EXPERIMENTAL
94- bool handled = false;
95- CHK_RET(static_cast<HcclResult>(PluginEndpointGet(endpointHandle, endpoint, handled)));
96- if (handled) {
97- return HCCL_SUCCESS;
98- }
99-#endif
100 147 
101 auto it = GetEndpointMap().GetEndpoint(endpointHandle);148 auto it = GetEndpointMap().GetEndpoint(endpointHandle);
102 CHK_PRT_RET(149 CHK_PRT_RET(
@@ -112,18 +159,15 @@ HcommResult HcommEndpointGet(EndpointHandle endpointHandle, void** endpoint) //
112 159 
113HcommResult HcommEndpointCreate(const EndpointDesc* endpoint, EndpointHandle* endpointHandle)160HcommResult HcommEndpointCreate(const EndpointDesc* endpoint, EndpointHandle* endpointHandle)
114{161{
115- EXCEPTION_HANDLE_BEGIN(void) HcommResMgrInit();162+ EXCEPTION_HANDLE_BEGIN
116 CHK_RET(ValidateEndpointDesc(endpoint, endpointHandle));163 CHK_RET(ValidateEndpointDesc(endpoint, endpointHandle));
117-#ifdef ENABLE_EXPERIMENTAL164+ if (endpoint->loc.locType == ENDPOINT_LOC_TYPE_HOST) {
118- bool pluginHandled = false;165+ const NicPluginEntry* pluginEntry = FindHostNicPlugin(endpoint->protocol);
119- CHK_RET(static_cast<HcclResult>(PluginEndpointCreate(endpoint, endpointHandle, pluginHandled)));166+ if (pluginEntry != nullptr) {
120- if (pluginHandled) {167+ return CreatePluginEndpointHolder(endpoint, pluginEntry, endpointHandle);
121- HCCL_INFO(168+ }
122- "[NicPluginDebug][%s] plugin endpoint created, protocol[%d], handle[%p].", __func__, endpoint->protocol,
123- *endpointHandle);
124- return HCCL_SUCCESS;
125 }169 }
126-#endif170+ (void)HcommResMgrInit();
127 CHK_RET(CreateBuiltinEndpoint(endpoint, endpointHandle));171 CHK_RET(CreateBuiltinEndpoint(endpoint, endpointHandle));
128 HcommResMgr::RegisterDeviceResetCallback();172 HcommResMgr::RegisterDeviceResetCallback();
129 EXCEPTION_HANDLE_END173 EXCEPTION_HANDLE_END
@@ -132,26 +176,24 @@ HcommResult HcommEndpointCreate(const EndpointDesc* endpoint, EndpointHandle* en
132 176 
133HcommResult HcommEndpointDestroy(EndpointHandle endpointHandle)177HcommResult HcommEndpointDestroy(EndpointHandle endpointHandle)
134{178{
135- (void)HcommResMgrInit();
136 HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);179 HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
137- 180+ auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
138- // 无论 plugin 还是 builtin 路径, 均需校验共享 jetty channel 是否已全部销毁,181+ if (endpoint != nullptr && endpoint->GetNicOps() != nullptr && endpoint->GetNicOps() != &g_BuiltinEndpointOps) {
139- // 否则残留 channel 持有的 jetty 引用会在 endpoint 销毁后成为悬空引用。182+ HCCL_INFO("[NicPlugin][%s] destroy plugin endpoint.", __func__);
183+ auto ret = GetEndpointMap().RemoveEndpoint(endpointHandle);
184+ CHK_PRT_RET(
185+ ret == false, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),
186+ HCCL_E_NOT_FOUND);
187+ return HCCL_SUCCESS;
188+ }
189+ (void)HcommResMgrInit();
190+ // 需校验共享 jetty channel 是否已全部销毁,否则残留 channel 持有的 jetty 引用会在 endpoint 销毁后成为悬空引用。
140 HcclResult jettyRet = hcomm::SharedJettyMgr::GetInstance().CheckEndpointDestroy(endpointHandle);191 HcclResult jettyRet = hcomm::SharedJettyMgr::GetInstance().CheckEndpointDestroy(endpointHandle);
141 CHK_PRT_RET(192 CHK_PRT_RET(
142 jettyRet != HCCL_SUCCESS,193 jettyRet != HCCL_SUCCESS,
143 HCCL_ERROR(194 HCCL_ERROR(
144 "[%s] cannot destroy endpointHandle[0x%llx], shared jetty channels still exist.", __func__, endpointHandle),195 "[%s] cannot destroy endpointHandle[0x%llx], shared jetty channels still exist.", __func__, endpointHandle),
145 jettyRet);196 jettyRet);
146-#ifdef ENABLE_EXPERIMENTAL
147- bool handled = false;
148- CHK_RET(static_cast<HcclResult>(PluginEndpointDestroy(endpointHandle, handled)));
149- if (handled) {
150- return HCCL_SUCCESS;
151- }
152-#endif
153- 
154- auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
155 if (endpoint != nullptr) {197 if (endpoint != nullptr) {
156 CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));198 CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));
157 }199 }
@@ -190,20 +232,11 @@ HcommResult HcommEndpointStopListen(EndpointHandle endpointHandle, uint32_t port
190 232 
191HcommResult HcommEndpointGetListenPort(EndpointHandle endpointHandle, uint32_t* port)233HcommResult HcommEndpointGetListenPort(EndpointHandle endpointHandle, uint32_t* port)
192{234{
193- CHK_PTR_NULL(port);
194- (void)HcommResMgrInit();
195-#ifdef ENABLE_EXPERIMENTAL
196- if (IsPluginEndpoint(endpointHandle)) {
197- return HCCL_E_NOT_SUPPORT;
198- }
199-#endif
200- 
201 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);235 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
202 CHK_PRT_RET(236 CHK_PRT_RET(
203 endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),237 endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
204 HCCL_E_NOT_FOUND);238 HCCL_E_NOT_FOUND);
205- CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));239+ return static_cast<HcclResult>(endpoint->GetNicOps()->getListenPort(endpoint->GetNicCtx(), port));
206- return endpoint->ServerSocketGetListenPort(port);
207}240}
208 241 
209HcommResult242HcommResult
@@ -17,134 +17,61 @@
17#include "hcomm_res_defs.h"17#include "hcomm_res_defs.h"
18#include "hcomm_res.h"18#include "hcomm_res.h"
19#include "hcomm_mem_alloc.h"19#include "hcomm_mem_alloc.h"
20-#ifdef ENABLE_EXPERIMENTAL
21-#include "nic_plugin_dispatcher.h"
22-#endif
23 20 
24using namespace hcomm;21using namespace hcomm;
25 22 
26HcommResult23HcommResult
27HcommMemReg(EndpointHandle endpointHandle, const char* memTag, const CommMem* mem, HcommMemHandle* memHandle)24HcommMemReg(EndpointHandle endpointHandle, const char* memTag, const CommMem* mem, HcommMemHandle* memHandle)
28{25{
29- CHK_PTR_NULL(memHandle);
30- EXCEPTION_HANDLE_BEGIN
31- CHK_PTR_NULL(mem);
32- CHK_PTR_NULL(memHandle);
33- (void)HcommResMgrInit();
34- HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
35-#ifdef ENABLE_EXPERIMENTAL
36- bool handled = false;
37- CHK_RET(static_cast<HcclResult>(PluginMemReg(endpointHandle, memTag, mem, memHandle, handled)));
38- if (handled) {
39- return HCCL_SUCCESS;
40- }
41-#endif
42- 
43 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);26 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
44 CHK_PRT_RET(27 CHK_PRT_RET(
45- endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),28+ endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
46 HCCL_E_NOT_FOUND);29 HCCL_E_NOT_FOUND);
47- CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));30+ CHK_RET(static_cast<HcclResult>(endpoint->GetNicOps()->registerMemory(
严正行

[HIGH] HcommMemReg 在 #ifdef ENABLE_EXPERIMENTAL 路径缺少 CHK_PTR_NULL(mem) 和 CHK_PTR_NULL(memHandle) 参数校验

问题分析:

  • 第 26-32 行 EXPERIMENTAL 路径直接调 endpoint->GetNicOps()->registerMemory(endpoint->GetNicCtx(), mem, memTag, reinterpret_cast<void **>(memHandle))
  • 缺少 CHK_PTR_NULL(mem) 和 CHK_PTR_NULL(memHandle) 检查(非 EXPERIMENTAL 路径第 34-35 行有)
  • 若 mem 为 nullptr,插件 registerMemory 可能解引用 nullptr 崩溃
  • 若 memHandle 为 nullptr,传给插件写入会崩溃

建议修复: 在 EXPERIMENTAL 路径补充 CHK_PTR_NULL(mem) 和 CHK_PTR_NULL(memHandle)。同理 HcommMemExport/HcommMemImport/HcommMemUnimport 的 EXPERIMENTAL 路径也缺少对应参数校验。

likedislike
gxdjsnt
gxdjsnt
Aug 8 Comments:
likedislike
48- CHK_RET(endpoint->RegisterMemory(*mem, memTag, reinterpret_cast<void**>(memHandle)));31+ endpoint->GetNicCtx(), mem, memTag, reinterpret_cast<void**>(memHandle))));
49- EXCEPTION_HANDLE_END
50 return HCCL_SUCCESS;32 return HCCL_SUCCESS;
51}33}
52 34 
53HcommResult HcommMemUnreg(EndpointHandle endpointHandle, HcommMemHandle memHandle)35HcommResult HcommMemUnreg(EndpointHandle endpointHandle, HcommMemHandle memHandle)
54{36{
55- CHK_PTR_NULL(memHandle);
56- (void)HcommResMgrInit();
57- EXCEPTION_HANDLE_BEGIN
58- HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
59-#ifdef ENABLE_EXPERIMENTAL
60- bool handled = false;
61- CHK_RET(static_cast<HcclResult>(PluginMemUnreg(endpointHandle, memHandle, handled)));
62- if (handled) {
63- return HCCL_SUCCESS;
64- }
65-#endif
66- 
67 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);37 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
68 CHK_PRT_RET(38 CHK_PRT_RET(
69- endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),39+ endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
70 HCCL_E_NOT_FOUND);40 HCCL_E_NOT_FOUND);
71- CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));41+ CHK_RET(static_cast<HcclResult>(endpoint->GetNicOps()->unregisterMemory(endpoint->GetNicCtx(), memHandle)));
72- CHK_RET(endpoint->UnregisterMemory(memHandle));
73- EXCEPTION_HANDLE_END
74 return HCCL_SUCCESS;42 return HCCL_SUCCESS;
75}43}
76 44 
77HcommResult45HcommResult
78HcommMemExport(EndpointHandle endpointHandle, HcommMemHandle memHandle, void** memDesc, uint32_t* memDescLen)46HcommMemExport(EndpointHandle endpointHandle, HcommMemHandle memHandle, void** memDesc, uint32_t* memDescLen)
79{47{
80- CHK_PTR_NULL(memHandle);
81- CHK_PTR_NULL(memDesc);
82- CHK_PTR_NULL(memDescLen);
83- (void)HcommResMgrInit();
84- HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
85-#ifdef ENABLE_EXPERIMENTAL
86- bool handled = false;
87- CHK_RET(static_cast<HcclResult>(PluginMemExport(endpointHandle, memHandle, memDesc, memDescLen, handled)));
88- if (handled) {
89- return HCCL_SUCCESS;
90- }
91-#endif
92- 
93 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);48 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
94 CHK_PRT_RET(49 CHK_PRT_RET(
95- endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),50+ endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
96 HCCL_E_NOT_FOUND);51 HCCL_E_NOT_FOUND);
97- CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));52+ CHK_RET(static_cast<HcclResult>(
98- CHK_RET(endpoint->MemoryExport(memHandle, memDesc, memDescLen));53+ endpoint->GetNicOps()->memoryExport(endpoint->GetNicCtx(), memHandle, memDesc, memDescLen)));
99 return HCCL_SUCCESS;54 return HCCL_SUCCESS;
100}55}
101 56 
102HcommResult HcommMemImport(EndpointHandle endpointHandle, const void* memDesc, uint32_t descLen, CommMem* outMem)57HcommResult HcommMemImport(EndpointHandle endpointHandle, const void* memDesc, uint32_t descLen, CommMem* outMem)
103{58{
104- CHK_PTR_NULL(memDesc);
105- CHK_PTR_NULL(outMem);
106- CHK_PRT_RET(descLen == 0, HCCL_ERROR("[%s] descLen[0] is invalid", __func__), HCCL_E_PARA);
107- (void)HcommResMgrInit();
108- HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
109-#ifdef ENABLE_EXPERIMENTAL
110- bool handled = false;
111- CHK_RET(static_cast<HcclResult>(PluginMemImport(endpointHandle, memDesc, descLen, outMem, handled)));
112- if (handled) {
113- return HCCL_SUCCESS;
114- }
115-#endif
116- 
117 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);59 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
118 CHK_PRT_RET(60 CHK_PRT_RET(
119- endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),61+ endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
120 HCCL_E_NOT_FOUND);62 HCCL_E_NOT_FOUND);
121- CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));63+ CHK_RET(
122- CHK_PTR_NULL(outMem);64+ static_cast<HcclResult>(endpoint->GetNicOps()->memoryImport(endpoint->GetNicCtx(), memDesc, descLen, outMem)));
123- CommMem importedMem{};
124- CHK_RET(endpoint->MemoryImport(memDesc, descLen, &importedMem));
125- *outMem = importedMem;
126 return HCCL_SUCCESS;65 return HCCL_SUCCESS;
127}66}
128 67 
129HcommResult HcommMemUnimport(EndpointHandle endpointHandle, const void* memDesc, uint32_t descLen)68HcommResult HcommMemUnimport(EndpointHandle endpointHandle, const void* memDesc, uint32_t descLen)
130{69{
131- CHK_PTR_NULL(memDesc);
132- (void)HcommResMgrInit();
133- HCCL_INFO("[%s] START. endpointHandle[0x%llx].", __func__, endpointHandle);
134-#ifdef ENABLE_EXPERIMENTAL
135- bool handled = false;
136- CHK_RET(static_cast<HcclResult>(PluginMemUnimport(endpointHandle, memDesc, descLen, handled)));
137- if (handled) {
138- return HCCL_SUCCESS;
139- }
140-#endif
141- 
142 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);70 auto endpoint = GetEndpointMap().GetEndpoint(endpointHandle);
143 CHK_PRT_RET(71 CHK_PRT_RET(
144- endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[0x%llx]", __func__, endpointHandle),72+ endpoint == nullptr, HCCL_ERROR("[%s] endpoint not found, endpointHandle[%p]", __func__, endpointHandle),
145 HCCL_E_NOT_FOUND);73 HCCL_E_NOT_FOUND);
146- CHK_RET(RefreshEndpointContext(endpoint->GetEndpointDesc()));74+ CHK_RET(static_cast<HcclResult>(endpoint->GetNicOps()->memoryUnimport(endpoint->GetNicCtx(), memDesc, descLen)));
147- CHK_RET(endpoint->MemoryUnimport(memDesc, descLen));
148 return HCCL_SUCCESS;75 return HCCL_SUCCESS;
149}76}
150 77 
@@ -9,7 +9,6 @@
9# -----------------------------------------------------------------------------------------------------------9# -----------------------------------------------------------------------------------------------------------
10set(src_list10set(src_list
11 nic_plugin_manager.cc11 nic_plugin_manager.cc
12- nic_plugin_dispatcher.cc
13)12)
14 13 
15if(TARGET hcomm)14if(TARGET hcomm)
@@ -31,28 +31,57 @@ extern "C" {
31typedef struct {31typedef struct {
32 CommAbiHeader header;32 CommAbiHeader header;
33 int32_t (*init)(void* ctx);33 int32_t (*init)(void* ctx);
34+ int32_t (*destroy)(void* ctx);
35+ 
34 int32_t (*registerMemory)(void* ctx, const CommMem* mem, const char* tag, void** handle);36 int32_t (*registerMemory)(void* ctx, const CommMem* mem, const char* tag, void** handle);
35 int32_t (*unregisterMemory)(void* ctx, void* handle);37 int32_t (*unregisterMemory)(void* ctx, void* handle);
36 int32_t (*memoryExport)(void* ctx, void* handle, void** desc, uint32_t* descLen);38 int32_t (*memoryExport)(void* ctx, void* handle, void** desc, uint32_t* descLen);
37 int32_t (*memoryImport)(void* ctx, const void* desc, uint32_t descLen, CommMem* outMem);39 int32_t (*memoryImport)(void* ctx, const void* desc, uint32_t descLen, CommMem* outMem);
38 int32_t (*memoryUnimport)(void* ctx, const void* desc, uint32_t descLen);40 int32_t (*memoryUnimport)(void* ctx, const void* desc, uint32_t descLen);
39- void (*destroy)(void* ctx);41+ int32_t (*getListenPort)(void* ctx, uint32_t* port);
40} HcommNicEndpointOps;42} HcommNicEndpointOps;
41 43 
42typedef struct {44typedef struct {
43 CommAbiHeader header;45 CommAbiHeader header;
44 int32_t (*init)(void* ctx);46 int32_t (*init)(void* ctx);
45- int32_t (*getNotifyNum)(void* ctx, uint32_t* num);47+ int32_t (*destroy)(void* ctx);
46- int32_t (*writeNbi)(void* ctx, void* dst, const void* src, uint64_t len);48+ 
47- int32_t (*writeWithNotifyNbi)(void* ctx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
48- int32_t (*readNbi)(void* ctx, void* dst, const void* src, uint64_t len);
49- int32_t (*notifyRecord)(void* ctx, uint32_t remoteNotifyIdx);
50- int32_t (*notifyWait)(void* ctx, uint32_t localNotifyIdx, uint32_t timeout);
51- int32_t (*fence)(void* ctx);
52- void (*destroy)(void* ctx);
53 int32_t (*getStatus)(void* ctx, int32_t* status);49 int32_t (*getStatus)(void* ctx, int32_t* status);
54- int32_t (*updateMemInfo)(void* ctx, HcommMemHandle* memHandles, uint32_t memHandleNum);50+ 
55- int32_t (*getUserRemoteMem)(void* ctx, CommMem** mem, char*** tags, uint32_t* num);51+ int32_t (*writeNbi)(void* ctx, void* dst, const void* src, uint64_t len);
52+ int32_t (*writeNbiOnThread)(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
53+ int32_t (*writeOnThread)(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
54+ int32_t (*writeWithNotifyNbi)(void* ctx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
55+ int32_t (*writeWithNotifyNbiOnThread)(
56+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
57+ int32_t (*writeWithNotifyOnThread)(
58+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
59+ int32_t (*writeReduceOnThread)(
60+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
61+ HcommReduceOp reduceOp);
62+ int32_t (*writeReduceWithNotifyOnThread)(
63+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
64+ HcommReduceOp reduceOp, uint32_t remoteNotifyIdx);
65+ 
66+ int32_t (*readNbi)(void* ctx, void* dst, const void* src, uint64_t len);
67+ int32_t (*readNbiOnThread)(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
68+ int32_t (*readOnThread)(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
69+ int32_t (*readReduceOnThread)(
70+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
71+ HcommReduceOp reduceOp);
72+ 
73+ int32_t (*notifyRecord)(void* ctx, uint32_t remoteNotifyIdx);
74+ int32_t (*notifyRecordOnThread)(void* ctx, ThreadHandle thread, uint32_t remoteNotifyIdx);
75+ int32_t (*notifyWait)(void* ctx, uint32_t localNotifyIdx, uint32_t timeOut);
76+ int32_t (*notifyWaitOnThread)(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeOut);
77+ int32_t (*notifyWaitOnThreadWithDefaultTimeout)(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx);
78+ 
79+ int32_t (*batchTransferOnThread)(
80+ void* ctx, ThreadHandle thread, const HcommBatchTransferDesc* transferDescs, uint32_t transferDescNum);
81+ 
82+ int32_t (*fence)(void* ctx);
83+ int32_t (*fenceOnThread)(void* ctx, ThreadHandle thread);
84+ int32_t (*drainOnThread)(void* ctx, ThreadHandle thread);
56} HcommNicChannelOps;85} HcommNicChannelOps;
57 86 
58typedef struct {87typedef struct {
@@ -1,442 +0,0 @@
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- 
11-#include "nic_plugin_dispatcher.h"
12- 
13-#include <chrono>
14-#include <cstddef>
15-#include <vector>
16- 
17-#include "env_config/env_config.h"
18-#include "nic_plugin_manager.h"
19-#include "param_check_pub.h"
20- 
21-namespace hcomm {
22-namespace {
23- PluginEndpointCtx* GetPluginEndpointCtx(EndpointHandle handle, bool& handled)
24- {
25- handled = IsPluginEndpoint(handle);
26- return handled ? PLUGIN_EP_CTX(handle) : nullptr;
27- }
28- 
29- PluginChannelCtx* GetPluginChannelCtx(ChannelHandle handle, bool& handled)
30- {
31- handled = IsPluginChannel(handle);
32- return handled ? PLUGIN_CH_CTX(handle) : nullptr;
33- }
34- 
35- void DestroyCreatedPluginChannels(ChannelHandle* channels, uint32_t channelNum)
36- {
37- if (channels == nullptr) {
38- return;
39- }
40- for (uint32_t idx = 0; idx < channelNum; ++idx) {
41- if (channels[idx] == 0) {
42- continue;
43- }
44- bool handled = false;
45- (void)PluginChannelDestroy(channels[idx], handled);
46- channels[idx] = 0;
47- }
48- }
49- 
50- HcommResult ConnectPluginChannels(ChannelHandle* channels, uint32_t channelNum)
51- {
52- CHK_PTR_NULL(channels);
53- CHK_PRT_RET(
54- (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
55- 
56- const auto timeout = std::chrono::seconds(Hccl::EnvConfig::GetInstance().GetSocketConfig().GetLinkTimeOut());
57- const auto startTime = std::chrono::steady_clock::now();
58- std::vector<int32_t> statusVec(channelNum, 1);
59- 
60- while (true) {
61- for (uint32_t idx = 0; idx < channelNum; ++idx) {
62- bool handled = false;
63- HcommResult ret = PluginChannelGetStatus(channels[idx], &statusVec[idx], handled);
64- if (ret != HCCL_SUCCESS) {
65- HCCL_ERROR("[%s] PluginChannelGetStatus failed, ret[%d].", __func__, ret);
66- return ret;
67- }
68- CHK_PRT_RET(
69- !handled, HCCL_ERROR("[%s] channel[%u] is not plugin channel.", __func__, idx), HCCL_E_PARA);
70- }
71- 
72- bool allReady = true;
73- for (uint32_t idx = 0; idx < channelNum; ++idx) {
74- if (statusVec[idx] != 0) {
75- allReady = false;
76- break;
77- }
78- }
79- if (allReady) {
80- HCCL_INFO("[%s] SUCCESS.", __func__);
81- return HCCL_SUCCESS;
82- }
83- 
84- if ((std::chrono::steady_clock::now() - startTime) >= timeout) {
85- HCCL_ERROR("[%s] plugin channel connect timeout.", __func__);
86- return HCCL_E_TIMEOUT;
87- }
88- }
89- }
90- 
91-#define CHK_PLUGIN_ENDPOINT_OP(ctx, op) \
92- do { \
93- CHK_PTR_NULL(ctx); \
94- CHK_PTR_NULL((ctx)->ops); \
95- if (!IsEndpointOpAvailable((ctx)->ops, offsetof(HcommNicEndpointOps, op), sizeof((ctx)->ops->op))) { \
96- return UnsupportedPluginOp(__func__); \
97- } \
98- if ((ctx)->ops->op == nullptr) { \
99- return UnsupportedPluginOp(__func__); \
100- } \
101- } while (0)
102- 
103-#define CHK_PLUGIN_CHANNEL_OP(ctx, op) \
104- do { \
105- CHK_PTR_NULL(ctx); \
106- CHK_PTR_NULL((ctx)->ops); \
107- if (!IsChannelOpAvailable((ctx)->ops, offsetof(HcommNicChannelOps, op), sizeof((ctx)->ops->op))) { \
108- return UnsupportedPluginOp(__func__); \
109- } \
110- if ((ctx)->ops->op == nullptr) { \
111- return UnsupportedPluginOp(__func__); \
112- } \
113- } while (0)
114- 
115-#define CHK_PLUGIN_CHANNEL_OP_NAME(ctx, op, opName) \
116- do { \
117- CHK_PTR_NULL(ctx); \
118- CHK_PTR_NULL((ctx)->ops); \
119- if (!IsChannelOpAvailable((ctx)->ops, offsetof(HcommNicChannelOps, op), sizeof((ctx)->ops->op))) { \
120- return UnsupportedPluginOp(opName); \
121- } \
122- if ((ctx)->ops->op == nullptr) { \
123- return UnsupportedPluginOp(opName); \
124- } \
125- } while (0)
126- 
127-#define DISPATCH_PLUGIN_ENDPOINT_OP(handle, handled, op, args) \
128- do { \
129- PluginEndpointCtx* ctx = GetPluginEndpointCtx((handle), (handled)); \
130- if (!(handled)) { \
131- return HCCL_SUCCESS; \
132- } \
133- CHK_PLUGIN_ENDPOINT_OP(ctx, op); \
134- return ctx->ops->op args; \
135- } while (0)
136- 
137-#define DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, op, args) \
138- do { \
139- PluginChannelCtx* ctx = GetPluginChannelCtx((handle), (handled)); \
140- if (!(handled)) { \
141- return HCCL_SUCCESS; \
142- } \
143- CHK_PLUGIN_CHANNEL_OP(ctx, op); \
144- return ctx->ops->op args; \
145- } while (0)
146- 
147-#define DISPATCH_PLUGIN_CHANNEL_OP_NAME(handle, handled, op, opName, args) \
148- do { \
149- PluginChannelCtx* ctx = GetPluginChannelCtx((handle), (handled)); \
150- if (!(handled)) { \
151- return HCCL_SUCCESS; \
152- } \
153- CHK_PLUGIN_CHANNEL_OP_NAME(ctx, op, opName); \
154- return ctx->ops->op args; \
155- } while (0)
156- 
157- HcommResult UnsupportedPluginChannelOp(ChannelHandle handle, bool& handled, const char* opName)
158- {
159- handled = IsPluginChannel(handle);
160- if (!handled) {
161- return HCCL_SUCCESS;
162- }
163- return UnsupportedPluginOp(opName);
164- }
165- 
166- HcommResult UnsupportedPluginChannelReduceOp(
167- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
168- HcommReduceOp reduceOp, bool& handled, const char* opName)
169- {
170- (void)thread;
171- (void)dst;
172- (void)src;
173- (void)count;
174- (void)dataType;
175- (void)reduceOp;
176- return UnsupportedPluginChannelOp(handle, handled, opName);
177- }
178- 
179- HcommResult DispatchPluginChannelWriteNbi(
180- ChannelHandle handle, void* dst, const void* src, uint64_t len, bool& handled, const char* opName)
181- {
182- DISPATCH_PLUGIN_CHANNEL_OP_NAME(handle, handled, writeNbi, opName, (ctx->ctx, dst, src, len));
183- }
184- 
185- HcommResult DispatchPluginChannelWriteWithNotifyNbi(
186- ChannelHandle handle, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx, bool& handled,
187- const char* opName)
188- {
189- DISPATCH_PLUGIN_CHANNEL_OP_NAME(
190- handle, handled, writeWithNotifyNbi, opName, (ctx->ctx, dst, src, len, remoteNotifyIdx));
191- }
192- 
193- HcommResult DispatchPluginChannelReadNbi(
194- ChannelHandle handle, void* dst, const void* src, uint64_t len, bool& handled, const char* opName)
195- {
196- DISPATCH_PLUGIN_CHANNEL_OP_NAME(handle, handled, readNbi, opName, (ctx->ctx, dst, src, len));
197- }
198-} // namespace
199- 
200-bool IsPluginEndpoint(EndpointHandle handle) { return IS_PLUGIN_HANDLE(handle); }
201- 
202-bool IsPluginChannel(ChannelHandle handle) { return IS_PLUGIN_HANDLE(handle); }
203- 
204-HcommResult PluginEndpointCreate(const EndpointDesc* endpoint, EndpointHandle* endpointHandle, bool& handled)
205-{
206- handled = false;
207- CHK_PTR_NULL(endpoint);
208- CHK_PTR_NULL(endpointHandle);
209- if (endpoint->loc.locType != ENDPOINT_LOC_TYPE_HOST || FindHostNicPlugin(endpoint->protocol) == nullptr) {
210- return HCCL_SUCCESS;
211- }
212- handled = true;
213- return CreatePluginEndpoint(endpoint, endpointHandle);
214-}
215- 
216-HcommResult PluginEndpointGet(EndpointHandle handle, void** endpoint, bool& handled)
217-{
218- PluginEndpointCtx* ctx = GetPluginEndpointCtx(handle, handled);
219- if (!handled) {
220- return HCCL_SUCCESS;
221- }
222- CHK_PTR_NULL(ctx);
223- *endpoint = ctx->ctx;
224- return HCCL_SUCCESS;
225-}
226- 
227-HcommResult PluginEndpointDestroy(EndpointHandle handle, bool& handled)
228-{
229- handled = IsPluginEndpoint(handle);
230- return handled ? DestroyPluginEndpoint(handle) : HCCL_SUCCESS;
231-}
232- 
233-HcommResult
234-PluginMemReg(EndpointHandle handle, const char* memTag, const CommMem* mem, HcommMemHandle* memHandle, bool& handled)
235-{
236- DISPATCH_PLUGIN_ENDPOINT_OP(
237- handle, handled, registerMemory, (ctx->ctx, mem, memTag, reinterpret_cast<void**>(memHandle)));
238-}
239- 
240-HcommResult PluginMemUnreg(EndpointHandle handle, HcommMemHandle memHandle, bool& handled)
241-{
242- DISPATCH_PLUGIN_ENDPOINT_OP(handle, handled, unregisterMemory, (ctx->ctx, memHandle));
243-}
244- 
245-HcommResult
246-PluginMemExport(EndpointHandle handle, HcommMemHandle memHandle, void** memDesc, uint32_t* memDescLen, bool& handled)
247-{
248- DISPATCH_PLUGIN_ENDPOINT_OP(handle, handled, memoryExport, (ctx->ctx, memHandle, memDesc, memDescLen));
249-}
250- 
251-HcommResult
252-PluginMemImport(EndpointHandle handle, const void* memDesc, uint32_t descLen, CommMem* outMem, bool& handled)
253-{
254- DISPATCH_PLUGIN_ENDPOINT_OP(handle, handled, memoryImport, (ctx->ctx, memDesc, descLen, outMem));
255-}
256- 
257-HcommResult PluginMemUnimport(EndpointHandle handle, const void* memDesc, uint32_t descLen, bool& handled)
258-{
259- DISPATCH_PLUGIN_ENDPOINT_OP(handle, handled, memoryUnimport, (ctx->ctx, memDesc, descLen));
260-}
261- 
262-HcommResult
263-PluginChannelCreate(EndpointHandle endpointHandle, const HcommChannelDesc* channelDesc, ChannelHandle* channelHandle)
264-{
265- return CreatePluginChannel(endpointHandle, channelDesc, channelHandle);
266-}
267- 
268-HcommResult PluginChannelCreate(
269- EndpointHandle endpointHandle, CommEngine engine, const HcommChannelDesc* channelDescs, uint32_t channelNum,
270- ChannelHandle* channels, bool& handled)
271-{
272- handled = IsPluginEndpoint(endpointHandle);
273- if (!handled) {
274- return HCCL_SUCCESS;
275- }
276- CHK_PTR_NULL(channelDescs);
277- CHK_PTR_NULL(channels);
278- CHK_PRT_RET(
279- (channelNum == 0), HCCL_ERROR("[%s]Invalid channelNum, channelNum[%u]", __func__, channelNum), HCCL_E_PARA);
280- CHK_PRT_RET(
281- engine != COMM_ENGINE_CPU,
282- HCCL_ERROR("[%s] nic plugin endpoint only supports COMM_ENGINE_CPU, engine[%d].", __func__, engine),
283- HCCL_E_NOT_SUPPORT);
284- 
285- for (uint32_t idx = 0; idx < channelNum; ++idx) {
286- HcommResult ret = PluginChannelCreate(endpointHandle, &channelDescs[idx], &channels[idx]);
287- if (ret != HCCL_SUCCESS) {
288- DestroyCreatedPluginChannels(channels, idx);
289- return ret;
290- }
291- }
292- 
293- HcommResult ret = ConnectPluginChannels(channels, channelNum);
294- if (ret != HCCL_SUCCESS) {
295- DestroyCreatedPluginChannels(channels, channelNum);
296- return ret;
297- }
298- return HCCL_SUCCESS;
299-}
300- 
301-HcommResult PluginChannelGet(ChannelHandle handle, void** channel, bool& handled)
302-{
303- PluginChannelCtx* ctx = GetPluginChannelCtx(handle, handled);
304- if (!handled) {
305- return HCCL_SUCCESS;
306- }
307- CHK_PTR_NULL(ctx);
308- *channel = ctx->ctx;
309- return HCCL_SUCCESS;
310-}
311- 
312-HcommResult PluginChannelGetStatus(ChannelHandle handle, int32_t* status, bool& handled)
313-{
314- DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, getStatus, (ctx->ctx, status));
315-}
316- 
317-HcommResult PluginChannelGetNotifyNum(ChannelHandle handle, uint32_t* notifyNum, bool& handled)
318-{
319- DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, getNotifyNum, (ctx->ctx, notifyNum));
320-}
321- 
322-HcommResult PluginChannelDestroy(ChannelHandle handle, bool& handled)
323-{
324- handled = IsPluginChannel(handle);
325- return handled ? DestroyPluginChannel(handle) : HCCL_SUCCESS;
326-}
327- 
328-HcommResult
329-PluginChannelUpdateMemInfo(ChannelHandle handle, HcommMemHandle* memHandles, uint32_t memHandleNum, bool& handled)
330-{
331- DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, updateMemInfo, (ctx->ctx, memHandles, memHandleNum));
332-}
333- 
334-HcommResult
335-PluginChannelGetRemoteMems(ChannelHandle handle, uint32_t* memNum, CommMem** remoteMem, char*** memInfos, bool& handled)
336-{
337- DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, getUserRemoteMem, (ctx->ctx, remoteMem, memInfos, memNum));
338-}
339- 
340-HcommResult
341-PluginChannelWrite(ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled)
342-{
343- (void)thread;
344- return DispatchPluginChannelWriteNbi(handle, dst, src, len, handled, __func__);
345-}
346- 
347-HcommResult PluginChannelBatchTransfer(
348- ChannelHandle handle, ThreadHandle thread, const HcommBatchTransferDesc* transferDescs, uint32_t transferDescNum,
349- bool& handled)
350-{
351- (void)thread;
352- (void)transferDescs;
353- (void)transferDescNum;
354- return UnsupportedPluginChannelOp(handle, handled, __func__);
355-}
356- 
357-HcommResult PluginChannelWriteReduce(
358- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
359- HcommReduceOp reduceOp, bool& handled)
360-{
361- return UnsupportedPluginChannelReduceOp(handle, thread, dst, src, count, dataType, reduceOp, handled, __func__);
362-}
363- 
364-HcommResult PluginChannelWriteWithNotify(
365- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx,
366- bool& handled)
367-{
368- (void)thread;
369- return DispatchPluginChannelWriteWithNotifyNbi(handle, dst, src, len, remoteNotifyIdx, handled, __func__);
370-}
371- 
372-HcommResult PluginChannelWriteReduceWithNotify(
373- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
374- HcommReduceOp reduceOp, uint32_t remoteNotifyIdx, bool& handled)
375-{
376- (void)thread;
377- (void)dst;
378- (void)src;
379- (void)count;
380- (void)dataType;
381- (void)reduceOp;
382- (void)remoteNotifyIdx;
383- return UnsupportedPluginChannelOp(handle, handled, __func__);
384-}
385- 
386-HcommResult
387-PluginChannelRead(ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled)
388-{
389- (void)thread;
390- return DispatchPluginChannelReadNbi(handle, dst, src, len, handled, __func__);
391-}
392- 
393-HcommResult PluginChannelReadReduce(
394- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
395- HcommReduceOp reduceOp, bool& handled)
396-{
397- return UnsupportedPluginChannelReduceOp(handle, thread, dst, src, count, dataType, reduceOp, handled, __func__);
398-}
399- 
400-HcommResult PluginChannelWriteNbi(
401- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled)
402-{
403- (void)thread;
404- return DispatchPluginChannelWriteNbi(handle, dst, src, len, handled, __func__);
405-}
406- 
407-HcommResult PluginChannelWriteWithNotifyNbi(
408- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx,
409- bool& handled)
410-{
411- (void)thread;
412- return DispatchPluginChannelWriteWithNotifyNbi(handle, dst, src, len, remoteNotifyIdx, handled, __func__);
413-}
414- 
415-HcommResult
416-PluginChannelReadNbi(ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled)
417-{
418- (void)thread;
419- return DispatchPluginChannelReadNbi(handle, dst, src, len, handled, __func__);
420-}
421- 
422-HcommResult
423-PluginChannelNotifyRecord(ChannelHandle handle, ThreadHandle thread, uint32_t remoteNotifyIdx, bool& handled)
424-{
425- (void)thread;
426- DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, notifyRecord, (ctx->ctx, remoteNotifyIdx));
427-}
428- 
429-HcommResult PluginChannelNotifyWait(
430- ChannelHandle handle, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeOut, bool& handled)
431-{
432- (void)thread;
433- DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, notifyWait, (ctx->ctx, localNotifyIdx, timeOut));
434-}
435- 
436-HcommResult PluginChannelFence(ChannelHandle handle, ThreadHandle thread, bool& handled)
437-{
438- (void)thread;
439- DISPATCH_PLUGIN_CHANNEL_OP(handle, handled, fence, (ctx->ctx));
440-}
441- 
442-} // namespace hcomm
@@ -1,93 +0,0 @@
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- 
11-#ifndef HCOMM_NIC_PLUGIN_DISPATCHER_H
12-#define HCOMM_NIC_PLUGIN_DISPATCHER_H
13- 
14-#include "hcomm_c_adpt.h"
15-#include "hcomm_primitives.h"
16- 
17-namespace hcomm {
18- 
19-bool IsPluginEndpoint(EndpointHandle handle);
20-bool IsPluginChannel(ChannelHandle handle);
21- 
22-HcommResult PluginEndpointCreate(const EndpointDesc* endpoint, EndpointHandle* endpointHandle, bool& handled);
23-HcommResult PluginEndpointGet(EndpointHandle handle, void** endpoint, bool& handled);
24-HcommResult PluginEndpointDestroy(EndpointHandle handle, bool& handled);
25- 
26-HcommResult
27-PluginMemReg(EndpointHandle handle, const char* memTag, const CommMem* mem, HcommMemHandle* memHandle, bool& handled);
28-HcommResult PluginMemUnreg(EndpointHandle handle, HcommMemHandle memHandle, bool& handled);
29-HcommResult
30-PluginMemExport(EndpointHandle handle, HcommMemHandle memHandle, void** memDesc, uint32_t* memDescLen, bool& handled);
31-HcommResult
32-PluginMemImport(EndpointHandle handle, const void* memDesc, uint32_t descLen, CommMem* outMem, bool& handled);
33-HcommResult PluginMemUnimport(EndpointHandle handle, const void* memDesc, uint32_t descLen, bool& handled);
34- 
35-HcommResult
36-PluginChannelCreate(EndpointHandle endpointHandle, const HcommChannelDesc* channelDesc, ChannelHandle* channelHandle);
37-HcommResult PluginChannelCreate(
38- EndpointHandle endpointHandle, CommEngine engine, const HcommChannelDesc* channelDescs, uint32_t channelNum,
39- ChannelHandle* channels, bool& handled);
40-HcommResult PluginChannelGet(ChannelHandle handle, void** channel, bool& handled);
41-HcommResult PluginChannelGetStatus(ChannelHandle handle, int32_t* status, bool& handled);
42-HcommResult PluginChannelGetNotifyNum(ChannelHandle handle, uint32_t* notifyNum, bool& handled);
43-/**
44- * @brief 尝试销毁 plugin channel。
45- * @param[in] handle channel 句柄
46- * @param[out] handled 是否由 plugin 认领。
47- * handled=false:非 plugin channel,保证返回 HCCL_SUCCESS 且不修改 channel 任何状态,
48- * 调用方可安全走 builtin 销毁路径,无 double-free/UAF 风险。
49- * handled=true:plugin 已认领所有权(无论返回成功或失败),由 plugin 负责清理,
50- * 调用方不可再走 builtin 路径。
51- * @return handled=true 时为 plugin 销毁结果;handled=false 时恒为 HCCL_SUCCESS。
52- */
53-HcommResult PluginChannelDestroy(ChannelHandle handle, bool& handled);
54-HcommResult
55-PluginChannelUpdateMemInfo(ChannelHandle handle, HcommMemHandle* memHandles, uint32_t memHandleNum, bool& handled);
56-HcommResult PluginChannelGetRemoteMems(
57- ChannelHandle handle, uint32_t* memNum, CommMem** remoteMem, char*** memInfos, bool& handled);
58- 
59-HcommResult
60-PluginChannelWrite(ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled);
61-HcommResult PluginChannelBatchTransfer(
62- ChannelHandle handle, ThreadHandle thread, const HcommBatchTransferDesc* transferDescs, uint32_t transferDescNum,
63- bool& handled);
64-HcommResult PluginChannelWriteReduce(
65- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
66- HcommReduceOp reduceOp, bool& handled);
67-HcommResult PluginChannelWriteWithNotify(
68- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx,
69- bool& handled);
70-HcommResult PluginChannelWriteReduceWithNotify(
71- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
72- HcommReduceOp reduceOp, uint32_t remoteNotifyIdx, bool& handled);
73-HcommResult
74-PluginChannelRead(ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled);
75-HcommResult PluginChannelReadReduce(
76- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
77- HcommReduceOp reduceOp, bool& handled);
78-HcommResult PluginChannelWriteNbi(
79- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled);
80-HcommResult PluginChannelWriteWithNotifyNbi(
81- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx,
82- bool& handled);
83-HcommResult PluginChannelReadNbi(
84- ChannelHandle handle, ThreadHandle thread, void* dst, const void* src, uint64_t len, bool& handled);
85-HcommResult
86-PluginChannelNotifyRecord(ChannelHandle handle, ThreadHandle thread, uint32_t remoteNotifyIdx, bool& handled);
87-HcommResult PluginChannelNotifyWait(
88- ChannelHandle handle, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeOut, bool& handled);
89-HcommResult PluginChannelFence(ChannelHandle handle, ThreadHandle thread, bool& handled);
90- 
91-} // namespace hcomm
92- 
93-#endif // HCOMM_NIC_PLUGIN_DISPATCHER_H
@@ -0,0 +1,166 @@
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+#ifndef HCOMM_NIC_PLUGIN_HOLDER_H
11+#define HCOMM_NIC_PLUGIN_HOLDER_H
12+ 
13+#include "endpoint.h"
14+#include "channel.h"
15+#include "nic_plugin_manager.h"
16+ 
17+namespace hcomm {
18+ 
19+template <typename Ops>
20+void DestroyNicPluginOpsAndCtx(Ops*& nicOps, void* nicCtx)
21+{
22+ if (nicOps != nullptr) {
23+ if (nicOps->destroy != nullptr) {
24+ int32_t ret = nicOps->destroy(nicCtx);
25+ if (ret != HCCL_SUCCESS) {
26+ HCCL_WARNING("[%s] plugin destroy failed, ret[%d].", __func__, ret);
27+ }
28+ }
29+ delete nicOps;
30+ nicOps = nullptr;
31+ }
32+}
33+ 
34+/**
35+ * @note 职责:NIC插件Endpoint/Channel占位子类,仅用于在EndpointMap、ChannelMap中占位,
36+ * 接口业务功能由NIC插件实现。
37+ */
38+class PluginEndpointHolder : public Endpoint {
39+public:
40+ explicit PluginEndpointHolder(const EndpointDesc& endpointDesc, const NicPluginEntry* pluginEntry)
41+ : Endpoint(endpointDesc),
42+ pluginEntry_(pluginEntry)
43+ {}
44+ ~PluginEndpointHolder() override { DestroyNicPluginOpsAndCtx(nicOps_, nicCtx_); }
45+ 
46+ const NicPluginEntry* GetPluginEntry() const { return pluginEntry_; }
47+ 
48+ HcclResult Init() override { return HCCL_E_NOT_SUPPORT; }
49+ HcclResult ServerSocketListen(const uint32_t port) override
50+ {
51+ (void)port;
52+ return HCCL_E_NOT_SUPPORT;
53+ }
54+ HcclResult RegisterMemory(HcommMem mem, const char* memTag, void** memHandle) override
55+ {
56+ (void)mem;
57+ (void)memTag;
58+ (void)memHandle;
59+ return HCCL_E_NOT_SUPPORT;
60+ }
61+ HcclResult UnregisterMemory(void* memHandle) override
62+ {
63+ (void)memHandle;
64+ return HCCL_E_NOT_SUPPORT;
65+ }
66+ HcclResult MemoryExport(void* memHandle, void** memDesc, uint32_t* memDescLen) override
67+ {
68+ (void)memHandle;
69+ (void)memDesc;
70+ (void)memDescLen;
71+ return HCCL_E_NOT_SUPPORT;
72+ }
73+ HcclResult MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem) override
74+ {
75+ (void)memDesc;
76+ (void)descLen;
77+ (void)outMem;
78+ return HCCL_E_NOT_SUPPORT;
79+ }
80+ HcclResult MemoryUnimport(const void* memDesc, uint32_t descLen) override
81+ {
82+ (void)memDesc;
83+ (void)descLen;
84+ return HCCL_E_NOT_SUPPORT;
85+ }
86+ HcclResult GetAllMemHandles(void** memHandles, uint32_t* memHandleNum) override
87+ {
88+ (void)memHandles;
89+ (void)memHandleNum;
90+ return HCCL_E_NOT_SUPPORT;
91+ }
92+ 
93+private:
94+ const NicPluginEntry* pluginEntry_;
95+};
96+ 
97+class PluginChannelHolder : public Channel {
98+public:
99+ explicit PluginChannelHolder(const NicPluginEntry* pluginEntry) : pluginEntry_(pluginEntry) {}
100+ ~PluginChannelHolder() override { DestroyNicPluginOpsAndCtx(nicOps_, nicCtx_); }
101+ 
102+ const NicPluginEntry* GetPluginEntry() const { return pluginEntry_; }
103+ 
104+ HcclResult Init() override { return HCCL_E_NOT_SUPPORT; }
105+ HcclResult GetNotifyNum(uint32_t* notifyNum) const override
106+ {
107+ (void)notifyNum;
108+ return HCCL_E_NOT_SUPPORT;
109+ }
110+ HcclResult GetRemoteMems(uint32_t* memNum, CommMem** remoteMem, char*** memInfos) override
111+ {
112+ (void)memNum;
113+ (void)remoteMem;
114+ (void)memInfos;
115+ return HCCL_E_NOT_SUPPORT;
116+ }
117+ ChannelStatus GetStatus() override { return ChannelStatus::FAILED; }
118+ HcclResult Clean() override { return HCCL_E_NOT_SUPPORT; }
119+ HcclResult Resume() override { return HCCL_E_NOT_SUPPORT; }
120+ HcclResult NotifyRecord(const uint32_t remoteNotifyIdx) override
121+ {
122+ (void)remoteNotifyIdx;
123+ return HCCL_E_NOT_SUPPORT;
124+ }
125+ HcclResult NotifyWait(const uint32_t localNotifyIdx, const uint32_t timeout) override
126+ {
127+ (void)localNotifyIdx;
128+ (void)timeout;
129+ return HCCL_E_NOT_SUPPORT;
130+ }
131+ HcclResult WriteWithNotify(void* dst, const void* src, const uint64_t len, uint32_t remoteNotifyIdx) override
132+ {
133+ (void)dst;
134+ (void)src;
135+ (void)len;
136+ (void)remoteNotifyIdx;
137+ return HCCL_E_NOT_SUPPORT;
138+ }
139+ HcclResult Write(void* dst, const void* src, uint64_t len) override
140+ {
141+ (void)dst;
142+ (void)src;
143+ (void)len;
144+ return HCCL_E_NOT_SUPPORT;
145+ }
146+ HcclResult Read(void* dst, const void* src, uint64_t len) override
147+ {
148+ (void)dst;
149+ (void)src;
150+ (void)len;
151+ return HCCL_E_NOT_SUPPORT;
152+ }
153+ HcclResult ChannelFence() override { return HCCL_E_NOT_SUPPORT; }
154+ const HcommChannelDesc& GetChannelDesc() const override
155+ {
156+ static const HcommChannelDesc kEmptyDesc{};
157+ return kEmptyDesc;
158+ }
159+ 
160+private:
161+ const NicPluginEntry* pluginEntry_;
162+};
163+ 
164+} // namespace hcomm
165+ 
166+#endif // HCOMM_NIC_PLUGIN_HOLDER_H
@@ -77,26 +77,6 @@ namespace {
77 return true;77 return true;
78 }78 }
79 79 
80- bool ValidateEndpointOps(const HcommNicEndpointOps* ops)
81- {
82- return ops != nullptr
83- && IsOpsHeaderValid(
84- ops->header, HCOMM_NIC_ENDPOINT_OPS_MAGIC_WORD, HCOMM_NIC_ENDPOINT_OPS_VERSION, "endpoint ops");
85- }
86- 
87- bool ValidateChannelOps(const HcommNicChannelOps* ops)
88- {
89- return ops != nullptr
90- && IsOpsHeaderValid(
91- ops->header, HCOMM_NIC_CHANNEL_OPS_MAGIC_WORD, HCOMM_NIC_CHANNEL_OPS_VERSION, "channel ops");
92- }
93- 
94- template <typename PluginOps>
95- bool IsPluginOpAvailable(const PluginOps* ops, size_t opOffset, size_t opSize)
96- {
97- return ops != nullptr && ops->header.size >= opOffset + opSize;
98- }
99- 
100 void RegisterPluginProtocols(const NicPluginEntry* plugin)80 void RegisterPluginProtocols(const NicPluginEntry* plugin)
101 {81 {
102 auto& protocolPlugins = ProtocolPlugins();82 auto& protocolPlugins = ProtocolPlugins();
@@ -26,31 +26,15 @@ struct NicPluginEntry {
26 HcommNicPluginCreateChannelFunc createChannel;26 HcommNicPluginCreateChannelFunc createChannel;
27};27};
28 28 
29-struct PluginEndpointCtx {
30- HcommNicEndpointOps* ops;
31- void* ctx;
32- const NicPluginEntry* entry;
33-};
34- 
35-struct PluginChannelCtx {
36- HcommNicChannelOps* ops;
37- void* ctx;
38- const NicPluginEntry* entry;
39-};
40- 
41constexpr uintptr_t HCOMM_PLUGIN_HANDLE_FLAG = (static_cast<uintptr_t>(1) << 63);29constexpr uintptr_t HCOMM_PLUGIN_HANDLE_FLAG = (static_cast<uintptr_t>(1) << 63);
30+constexpr int32_t COMM_PROTOCOL_CUSTOM_BASE = 1000;
42 31 
43#define IS_PLUGIN_HANDLE(h) ((((uintptr_t)(h)) & ::hcomm::HCOMM_PLUGIN_HANDLE_FLAG) != 0)32#define IS_PLUGIN_HANDLE(h) ((((uintptr_t)(h)) & ::hcomm::HCOMM_PLUGIN_HANDLE_FLAG) != 0)
44-#define PLUGIN_EP_CTX(h) \
45- (reinterpret_cast<::hcomm::PluginEndpointCtx*>( \
46- (reinterpret_cast<uintptr_t>(h)) & ~::hcomm::HCOMM_PLUGIN_HANDLE_FLAG))
47-#define PLUGIN_CH_CTX(h) \
48- (reinterpret_cast<::hcomm::PluginChannelCtx*>( \
49- (static_cast<uint64_t>(h)) & ~static_cast<uint64_t>(::hcomm::HCOMM_PLUGIN_HANDLE_FLAG)))
50-#define MAKE_PLUGIN_EP_HANDLE(p) \
51- (reinterpret_cast<EndpointHandle>((reinterpret_cast<uintptr_t>(p)) | ::hcomm::HCOMM_PLUGIN_HANDLE_FLAG))
52#define MAKE_PLUGIN_CH_HANDLE(p) \33#define MAKE_PLUGIN_CH_HANDLE(p) \
53 (static_cast<ChannelHandle>((reinterpret_cast<uintptr_t>(p)) | ::hcomm::HCOMM_PLUGIN_HANDLE_FLAG))34 (static_cast<ChannelHandle>((reinterpret_cast<uintptr_t>(p)) | ::hcomm::HCOMM_PLUGIN_HANDLE_FLAG))
35+#define CHANNEL_FROM_HANDLE(h) \
36+ (reinterpret_cast<::hcomm::Channel*>( \
37+ (static_cast<uintptr_t>(h)) & ~static_cast<uintptr_t>(::hcomm::HCOMM_PLUGIN_HANDLE_FLAG)))
54 38 
55void LoadAllNicPlugins();39void LoadAllNicPlugins();
56const NicPluginEntry* FindHostNicPlugin(CommProtocol protocol);40const NicPluginEntry* FindHostNicPlugin(CommProtocol protocol);
@@ -58,15 +42,119 @@ bool ValidatePluginInfo(
58 const char* soPath, const HcommNicPluginInfo* info, HcommNicPluginCreateEndpointFunc createEndpoint,42 const char* soPath, const HcommNicPluginInfo* info, HcommNicPluginCreateEndpointFunc createEndpoint,
59 HcommNicPluginCreateChannelFunc createChannel);43 HcommNicPluginCreateChannelFunc createChannel);
60 44 
61-HcommResult CreatePluginEndpoint(const EndpointDesc* endpoint, EndpointHandle* endpointHandle);45+HcommResult FillDefaultEndpointOps(const HcommNicEndpointOps* src, HcommNicEndpointOps** outOps);
62-HcommResult DestroyPluginEndpoint(EndpointHandle endpointHandle);
63-HcommResult
64-CreatePluginChannel(EndpointHandle endpointHandle, const HcommChannelDesc* channelDesc, ChannelHandle* channelHandle);
65-HcommResult DestroyPluginChannel(ChannelHandle channelHandle);
66 46 
67-HcommResult UnsupportedPluginOp(const char* opName);47+int32_t DefaultEndpointInit(void* ctx);
68-bool IsEndpointOpAvailable(const HcommNicEndpointOps* ops, size_t opOffset, size_t opSize);48+int32_t DefaultEndpointRegisterMemory(void* ctx, const CommMem* mem, const char* tag, void** handle);
69-bool IsChannelOpAvailable(const HcommNicChannelOps* ops, size_t opOffset, size_t opSize);49+int32_t DefaultEndpointUnregisterMemory(void* ctx, void* handle);
50+int32_t DefaultEndpointMemoryExport(void* ctx, void* handle, void** desc, uint32_t* descLen);
51+int32_t DefaultEndpointMemoryImport(void* ctx, const void* desc, uint32_t descLen, CommMem* outMem);
52+int32_t DefaultEndpointMemoryUnimport(void* ctx, const void* desc, uint32_t descLen);
53+int32_t DefaultEndpointGetListenPort(void* ctx, uint32_t* port);
54+ 
55+// HcommNicEndpointOps 新增成员时,在此追加一行 F(op字段名, 默认实现函数) 即可,destroy要求插件必须实现
56+#define FOR_EACH_ENDPOINT_OP_DEFAULT(F) \
57+ F(init, DefaultEndpointInit) \
58+ F(registerMemory, DefaultEndpointRegisterMemory) \
59+ F(unregisterMemory, DefaultEndpointUnregisterMemory) \
60+ F(memoryExport, DefaultEndpointMemoryExport) \
61+ F(memoryImport, DefaultEndpointMemoryImport) \
62+ F(memoryUnimport, DefaultEndpointMemoryUnimport) \
63+ F(getListenPort, DefaultEndpointGetListenPort)
64+ 
65+// 供 FOR_EACH_ENDPOINT_OP_DEFAULT 使用的填充器
66+#define FILL_ENDPOINT_OP_DEFAULT(op, defaultFunc) \
67+ if (!IsPluginOpAvailable(dst, offsetof(HcommNicEndpointOps, op), sizeof(dst->op)) || dst->op == nullptr) { \
68+ dst->op = defaultFunc; \
69+ }
70+ 
71+HcommResult FillDefaultChannelOps(const HcommNicChannelOps* src, HcommNicChannelOps** outOps);
72+ 
73+int32_t DefaultChannelInit(void* ctx);
74+int32_t DefaultChannelGetStatus(void* ctx, int32_t* status);
75+int32_t DefaultChannelWriteNbi(void* ctx, void* dst, const void* src, uint64_t len);
76+int32_t DefaultChannelWriteNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
77+int32_t DefaultChannelWriteOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
78+int32_t DefaultChannelWriteWithNotifyNbi(void* ctx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
79+int32_t DefaultChannelWriteWithNotifyNbiOnThread(
80+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
81+int32_t DefaultChannelWriteWithNotifyOnThread(
82+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx);
83+int32_t DefaultChannelWriteReduceOnThread(
84+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
85+ HcommReduceOp reduceOp);
86+int32_t DefaultChannelWriteReduceWithNotifyOnThread(
87+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
88+ HcommReduceOp reduceOp, uint32_t remoteNotifyIdx);
89+int32_t DefaultChannelReadNbi(void* ctx, void* dst, const void* src, uint64_t len);
90+int32_t DefaultChannelReadNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
91+int32_t DefaultChannelReadOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len);
92+int32_t DefaultChannelReadReduceOnThread(
93+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t count, HcommDataType dataType,
94+ HcommReduceOp reduceOp);
95+int32_t DefaultChannelNotifyRecord(void* ctx, uint32_t remoteNotifyIdx);
96+int32_t DefaultChannelNotifyRecordOnThread(void* ctx, ThreadHandle thread, uint32_t remoteNotifyIdx);
97+int32_t DefaultChannelNotifyWait(void* ctx, uint32_t localNotifyIdx, uint32_t timeOut);
98+int32_t DefaultChannelNotifyWaitOnThread(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeOut);
99+int32_t DefaultChannelNotifyWaitOnThreadWithDefaultTimeout(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx);
100+int32_t DefaultChannelBatchTransferOnThread(
101+ void* ctx, ThreadHandle thread, const HcommBatchTransferDesc* transferDescs, uint32_t transferDescNum);
102+int32_t DefaultChannelFence(void* ctx);
103+int32_t DefaultChannelFenceOnThread(void* ctx, ThreadHandle thread);
104+int32_t DefaultChannelDrainOnThread(void* ctx, ThreadHandle thread);
105+ 
106+// HcommNicChannelOps 新增成员时,在此追加一行 F(op字段名, 默认实现函数) 即可,destroy要求插件必须实现
107+#define FOR_EACH_CHANNEL_OP_DEFAULT(F) \
108+ F(init, DefaultChannelInit) \
109+ F(getStatus, DefaultChannelGetStatus) \
110+ F(writeNbi, DefaultChannelWriteNbi) \
111+ F(writeNbiOnThread, DefaultChannelWriteNbiOnThread) \
112+ F(writeOnThread, DefaultChannelWriteOnThread) \
113+ F(writeWithNotifyNbi, DefaultChannelWriteWithNotifyNbi) \
114+ F(writeWithNotifyNbiOnThread, DefaultChannelWriteWithNotifyNbiOnThread) \
115+ F(writeWithNotifyOnThread, DefaultChannelWriteWithNotifyOnThread) \
116+ F(writeReduceOnThread, DefaultChannelWriteReduceOnThread) \
117+ F(writeReduceWithNotifyOnThread, DefaultChannelWriteReduceWithNotifyOnThread) \
118+ F(readNbi, DefaultChannelReadNbi) \
119+ F(readNbiOnThread, DefaultChannelReadNbiOnThread) \
120+ F(readOnThread, DefaultChannelReadOnThread) \
121+ F(readReduceOnThread, DefaultChannelReadReduceOnThread) \
122+ F(notifyRecord, DefaultChannelNotifyRecord) \
123+ F(notifyRecordOnThread, DefaultChannelNotifyRecordOnThread) \
124+ F(notifyWait, DefaultChannelNotifyWait) \
125+ F(notifyWaitOnThread, DefaultChannelNotifyWaitOnThread) \
126+ F(notifyWaitOnThreadWithDefaultTimeout, DefaultChannelNotifyWaitOnThreadWithDefaultTimeout) \
127+ F(batchTransferOnThread, DefaultChannelBatchTransferOnThread) \
128+ F(fence, DefaultChannelFence) \
129+ F(fenceOnThread, DefaultChannelFenceOnThread) \
130+ F(drainOnThread, DefaultChannelDrainOnThread)
131+ 
132+// 供 FOR_EACH_CHANNEL_OP_DEFAULT 使用的填充器
133+#define FILL_CHANNEL_OP_DEFAULT(op, defaultFunc) \
134+ if (!IsPluginOpAvailable(dst, offsetof(HcommNicChannelOps, op), sizeof(dst->op)) || dst->op == nullptr) { \
135+ dst->op = defaultFunc; \
136+ }
137+ 
138+bool ValidateEndpointOps(const HcommNicEndpointOps* ops);
139+bool ValidateChannelOps(const HcommNicChannelOps* ops);
140+ 
141+template <typename PluginOps>
142+bool IsPluginOpAvailable(const PluginOps* ops, size_t opOffset, size_t opSize)
143+{
144+ return ops != nullptr && ops->header.size >= opOffset + opSize;
145+}
146+ 
147+template <typename PluginOps>
148+void DestroyPluginCtx(PluginOps* ops, void* pluginCtx)
149+{
150+ if (ops != nullptr && IsPluginOpAvailable(ops, offsetof(PluginOps, destroy), sizeof(ops->destroy))
151+ && ops->destroy != nullptr) {
152+ int32_t ret = ops->destroy(pluginCtx);
153+ if (ret != HCCL_SUCCESS) {
154+ HCCL_WARNING("[%s] plugin destroy failed, ret[%d].", __func__, ret);
155+ }
156+ }
157+}
70 158 
71} // namespace hcomm159} // namespace hcomm
72 160 
@@ -0,0 +1,256 @@
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+ 
11+#ifndef BUILTIN_CHANNEL_OPS_H
12+#define BUILTIN_CHANNEL_OPS_H
13+ 
14+#include "hcomm_nic_plugin.h"
15+#include "nic_plugin_manager.h"
16+#include "channel.h"
17+#include "channel_process.h"
18+#include "dtype_common.h"
19+// CreateBuiltinChannel 已调用 channelPtr->Init(),此处 no-op,此接口内置流程暂未调用。
20+inline int32_t BuiltinChannelInit(void* ctx)
21+{
22+ (void)ctx;
23+ return HCCL_SUCCESS;
24+}
25+ 
26+// channel 生命周期由 g_ChannelMap 的 unique_ptr 管理,此处 no-op,此接口内置流程暂未调用。
27+inline int32_t BuiltinChannelDestroy(void* ctx)
28+{
29+ (void)ctx;
30+ return HCCL_SUCCESS;
31+}
32+ 
33+inline int32_t BuiltinGetStatus(void* ctx, int32_t* status)
34+{
35+ auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
36+ CHK_PTR_NULL(channelPtr);
37+ auto channelStatus = channelPtr->GetStatus();
38+ switch (channelStatus) {
39+ case hcomm::ChannelStatus::FAILED:
40+ *status = hcomm::HCOMM_CHANNEL_STATUS_FAILED;
41+ break;
42+ case hcomm::ChannelStatus::SOCKET_TIMEOUT:
43+ *status = hcomm::HCOMM_CHANNEL_STATUS_TIMEOUT;
44+ break;
45+ case hcomm::ChannelStatus::READY:
46+ *status = hcomm::HCOMM_CHANNEL_STATUS_READY;
47+ break;
48+ default:
49+ *status = hcomm::HCOMM_CHANNEL_STATUS_CONNECTING;
50+ break;
51+ }
52+ return HCCL_SUCCESS;
53+}
54+ 
55+template <typename Op>
56+inline int32_t BuiltinRwNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, Op&& op)
57+{
58+ HCCL_INFO(
59+ "[%s] START. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu].", __func__, thread, ctx, dst,
60+ src, len);
61+ 
62+ (void)thread;
63+ CHK_PTR_NULL(src);
64+ CHK_PTR_NULL(dst);
65+ 
66+ HcclResult ret = HCCL_SUCCESS;
67+ DevType devType;
68+ CHK_RET(hrtGetDeviceType(devType));
69+ if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960) {
70+ auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
71+ CHK_PTR_NULL(channelPtr);
72+ ret = op(channelPtr, dst, src, len);
73+ } else {
74+ ret = HCCL_E_NOT_SUPPORT;
75+ }
76+ CHK_PRT_RET(
77+ ret != HCCL_SUCCESS,
78+ HCCL_ERROR(
79+ "[%s] FAIL. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu].", __func__, thread, ctx,
80+ dst, src, len),
81+ ret);
82+ HCCL_INFO("[%s] SUCCESS.", __func__);
83+ return HCCL_SUCCESS;
84+}
85+ 
86+inline int32_t BuiltinWriteNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len)
87+{
88+ return BuiltinRwNbiOnThread(ctx, thread, dst, src, len, [](hcomm::Channel* ch, void* d, const void* s, uint64_t l) {
89+ return ch->Write(d, s, l);
90+ });
91+}
92+ 
93+inline int32_t BuiltinWriteNbi(void* ctx, void* dst, const void* src, uint64_t len)
94+{
95+ return BuiltinWriteNbiOnThread(ctx, 0, dst, src, len);
96+}
97+ 
98+inline int32_t BuiltinWriteWithNotifyNbiOnThread(
99+ void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx)
100+{
101+ HCCL_INFO(
102+ "[%s] START. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu], remoteNotifyIdx[%u].",
103+ __func__, thread, ctx, dst, src, len, remoteNotifyIdx);
104+ 
105+ (void)thread;
106+ CHK_PTR_NULL(src);
107+ CHK_PTR_NULL(dst);
108+ HcclResult ret = HCCL_SUCCESS;
109+ DevType devType;
110+ CHK_RET(hrtGetDeviceType(devType));
111+ if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960 || thread == 0) {
112+ auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
113+ CHK_PTR_NULL(channelPtr);
114+ ret = channelPtr->WriteWithNotify(dst, src, len, remoteNotifyIdx);
115+ } else {
116+ ret = HCCL_E_NOT_SUPPORT;
117+ }
118+ CHK_PRT_RET(
119+ ret != HCCL_SUCCESS,
120+ HCCL_ERROR(
121+ "[%s] FAIL. thread[0x%llx], channel[0x%llx], dst[0x%llx], src[0x%llx], len[%llu], remoteNotifyIdx[%u].",
122+ __func__, thread, ctx, dst, src, len, remoteNotifyIdx),
123+ ret);
124+ HCCL_INFO("[%s] SUCCESS.", __func__);
125+ return HCCL_SUCCESS;
126+}
127+ 
128+inline int32_t BuiltinWriteWithNotifyNbi(void* ctx, void* dst, const void* src, uint64_t len, uint32_t remoteNotifyIdx)
129+{
130+ return BuiltinWriteWithNotifyNbiOnThread(ctx, 0, dst, src, len, remoteNotifyIdx);
131+}
132+ 
133+// 供A5/A6接口BuiltinNotifyWait调用(原流程HcommChannelNotifyWait->HcommChannelNotifyWaitOnThread),A2/A3内置不走该接口
134+inline int32_t BuiltinNotifyWaitOnThread(void* ctx, ThreadHandle thread, uint32_t localNotifyIdx, uint32_t timeOut)
135+{
136+ (void)thread;
137+ HCCL_INFO(
138+ "[%s] START. thread[0x%llx], channel[0x%llx], localNotifyIdx[%u], timeOut[%u].", __func__, thread, ctx,
139+ localNotifyIdx, timeOut);
140+ auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
141+ CHK_PTR_NULL(channelPtr);
142+ HcclResult ret = channelPtr->NotifyWait(localNotifyIdx, timeOut);
143+ CHK_PRT_RET(
144+ ret != HCCL_SUCCESS,
145+ HCCL_ERROR(
146+ "[%s] FAIL. thread[0x%llx], channel[0x%llx], localNotifyIdx[%u], timeOut[%u].", __func__, thread, ctx,
147+ localNotifyIdx, timeOut),
148+ ret);
149+ HCCL_INFO("[%s] SUCCESS.", __func__);
150+ return HCCL_SUCCESS;
151+}
152+ 
153+inline int32_t BuiltinNotifyWait(void* ctx, uint32_t localNotifyIdx, uint32_t timeOut)
154+{
155+ DevType devType;
156+ CHK_RET(hrtGetDeviceType(devType));
157+ if (devType != DevType::DEV_TYPE_950 && devType != DevType::DEV_TYPE_960) {
158+ return HCCL_E_NOT_SUPPORT;
159+ }
160+ return BuiltinNotifyWaitOnThread(ctx, 0, localNotifyIdx, timeOut);
161+}
162+ 
163+// 供A5/A6接口BuiltinNotifyRecord调用(原流程HcommChannelNotifyRecord->HcommChannelNotifyRecordOnThread),A2/A3内置不走该接口
164+inline int32_t BuiltinNotifyRecordOnThread(void* ctx, ThreadHandle thread, uint32_t remoteNotifyIdx)
165+{
166+ (void)thread;
167+ HCCL_INFO(
168+ "[%s] START. thread[0x%llx], channel[0x%llx], remoteNotifyIdx[%u].", __func__, thread, ctx, remoteNotifyIdx);
169+ auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
170+ CHK_PTR_NULL(channelPtr);
171+ HcclResult ret = channelPtr->NotifyRecord(remoteNotifyIdx);
172+ CHK_PRT_RET(
173+ ret != HCCL_SUCCESS,
174+ HCCL_ERROR(
175+ "[%s] FAIL. thread[0x%llx], channel[0x%llx], remoteNotifyIdx[%u].", __func__, thread, ctx, remoteNotifyIdx),
176+ ret);
177+ HCCL_INFO("[%s] SUCCESS.", __func__);
178+ return HCCL_SUCCESS;
179+}
180+ 
181+inline int32_t BuiltinNotifyRecord(void* ctx, uint32_t remoteNotifyIdx)
182+{
183+ DevType devType;
184+ CHK_RET(hrtGetDeviceType(devType));
185+ if (devType != DevType::DEV_TYPE_950 && devType != DevType::DEV_TYPE_960) {
186+ return HCCL_E_NOT_SUPPORT;
187+ }
188+ return BuiltinNotifyRecordOnThread(ctx, 0, remoteNotifyIdx);
189+}
190+ 
191+inline int32_t BuiltinReadNbiOnThread(void* ctx, ThreadHandle thread, void* dst, const void* src, uint64_t len)
192+{
193+ return BuiltinRwNbiOnThread(ctx, thread, dst, src, len, [](hcomm::Channel* ch, void* d, const void* s, uint64_t l) {
194+ return ch->Read(d, s, l);
195+ });
196+}
197+ 
198+inline int32_t BuiltinReadNbi(void* ctx, void* dst, const void* src, uint64_t len)
199+{
200+ return BuiltinReadNbiOnThread(ctx, 0, dst, src, len);
201+}
202+ 
203+inline int32_t BuiltinFenceOnThread(void* ctx, ThreadHandle thread)
204+{
205+ HCCL_INFO("[%s] START. thread[0x%llx], channel[0x%llx].", __func__, thread, ctx);
206+ 
207+ (void)thread;
208+ HcclResult ret = HCCL_SUCCESS;
209+ DevType devType;
210+ CHK_RET(hrtGetDeviceType(devType));
211+ if (devType == DevType::DEV_TYPE_950 || devType == DevType::DEV_TYPE_960 || thread == 0) {
212+ auto* const channelPtr = reinterpret_cast<hcomm::Channel*>(ctx);
213+ CHK_PTR_NULL(channelPtr);
214+ ret = channelPtr->ChannelFence();
215+ } else {
216+ ret = HCCL_E_NOT_SUPPORT;
217+ }
218+ CHK_PRT_RET(
219+ ret != HCCL_SUCCESS, HCCL_ERROR("[%s] FAIL. thread[0x%llx], channel[0x%llx].", __func__, thread, ctx), ret);
220+ HCCL_INFO("[%s] SUCCESS.", __func__);
221+ return HCCL_SUCCESS;
222+}
223+ 
224+inline int32_t BuiltinFence(void* ctx) { return BuiltinFenceOnThread(ctx, 0); }
225+ 
226+// 以下接口内置不支持,复用 nic_plugin_manager.cc 中的 DefaultChannel* 默认实现
227+ 
228+inline HcommNicChannelOps g_BuiltinChannelOps = {
229+ {HCOMM_NIC_CHANNEL_OPS_VERSION, HCOMM_NIC_CHANNEL_OPS_MAGIC_WORD, sizeof(HcommNicChannelOps), 0},
230+ BuiltinChannelInit, // init
231+ BuiltinChannelDestroy, // destroy
232+ BuiltinGetStatus, // getStatus
233+ BuiltinWriteNbi, // writeNbi
234+ BuiltinWriteNbiOnThread, // writeNbiOnThread
235+ hcomm::DefaultChannelWriteOnThread, // writeOnThread
236+ BuiltinWriteWithNotifyNbi, // writeWithNotifyNbi
237+ BuiltinWriteWithNotifyNbiOnThread, // writeWithNotifyNbiOnThread
238+ hcomm::DefaultChannelWriteWithNotifyOnThread, // writeWithNotifyOnThread
239+ hcomm::DefaultChannelWriteReduceOnThread, // writeReduceOnThread
240+ hcomm::DefaultChannelWriteReduceWithNotifyOnThread, // writeReduceWithNotifyOnThread
241+ BuiltinReadNbi, // readNbi
242+ BuiltinReadNbiOnThread, // readNbiOnThread
243+ hcomm::DefaultChannelReadOnThread, // readOnThread
244+ hcomm::DefaultChannelReadReduceOnThread, // readReduceOnThread
245+ BuiltinNotifyRecord, // notifyRecord
246+ BuiltinNotifyRecordOnThread, // notifyRecordOnThread
247+ BuiltinNotifyWait, // notifyWait
248+ BuiltinNotifyWaitOnThread, // notifyWaitOnThread
249+ hcomm::DefaultChannelNotifyWaitOnThreadWithDefaultTimeout, // notifyWaitOnThreadWithDefaultTimeout
250+ hcomm::DefaultChannelBatchTransferOnThread, // batchTransferOnThread
251+ BuiltinFence, // fence
252+ BuiltinFenceOnThread, // fenceOnThread
253+ hcomm::DefaultChannelDrainOnThread, // drainOnThread
254+};
255+ 
256+#endif // BUILTIN_CHANNEL_OPS_H
@@ -23,6 +23,8 @@
23#include <vector>23#include <vector>
24#include "enum_factory.h"24#include "enum_factory.h"
25 25 
26+#include "hcomm_nic_plugin.h"
27+ 
26// Orion28// Orion
27#include "transport_status.h"29#include "transport_status.h"
28#include "ip_address.h"30#include "ip_address.h"
@@ -119,6 +121,15 @@ public:
119 virtual HcclResult Read(void* dst, const void* src, uint64_t len) = 0;121 virtual HcclResult Read(void* dst, const void* src, uint64_t len) = 0;
120 virtual HcclResult ChannelFence() = 0;122 virtual HcclResult ChannelFence() = 0;
121 123 
124+ // ------------------ NIC插件相关 ------------------
125+ void SetNicChannelCtx(HcommNicChannelOps* nicOps, void* nicCtx)
126+ {
127+ nicOps_ = nicOps;
128+ nicCtx_ = nicCtx;
129+ }
130+ HcommNicChannelOps* GetNicOps() const { return nicOps_; }
131+ void* GetNicCtx() const { return nicCtx_; }
132+ 
122 // ------------------ 工具方法 ------------------133 // ------------------ 工具方法 ------------------
123 static ChannelStatus TransportStatusToChannelStatus(Hccl::TransportStatus ts);134 static ChannelStatus TransportStatusToChannelStatus(Hccl::TransportStatus ts);
124 135 
@@ -146,6 +157,9 @@ protected:
146 std::vector<std::shared_ptr<hccl::DeviceMem>> ptrArrayDevMems_{};157 std::vector<std::shared_ptr<hccl::DeviceMem>> ptrArrayDevMems_{};
147 bool deviceEntityReady_{false};158 bool deviceEntityReady_{false};
148 bool isSharedJetty_{false};159 bool isSharedJetty_{false};
160+ 
161+ HcommNicChannelOps* nicOps_{nullptr};
162+ void* nicCtx_{nullptr};
149};163};
150 164 
151} // namespace hcomm165} // namespace hcomm
@@ -31,6 +31,7 @@
31#include "mem_device_pub.h"31#include "mem_device_pub.h"
32#include "comm_engine_utils.h"32#include "comm_engine_utils.h"
33#include "comm_configer.h"33#include "comm_configer.h"
34+#include "builtin_channel_ops.h"
34 35 
35namespace hcomm {36namespace hcomm {
36 37 
@@ -96,6 +97,8 @@ HcclResult ChannelProcess::CreateChannelsLoop(
96 CHK_RET_UNAVAIL(Channel::CreateChannel(endpointHandle, engine, channelDescs[i], tmpPtr, isSharedQueue));97 CHK_RET_UNAVAIL(Channel::CreateChannel(endpointHandle, engine, channelDescs[i], tmpPtr, isSharedQueue));
97 CHK_SMART_PTR_NULL(tmpPtr);98 CHK_SMART_PTR_NULL(tmpPtr);
98 99 
100+ tmpPtr->SetNicChannelCtx(&g_BuiltinChannelOps, tmpPtr.get());
101+ 
99 ChannelHandle handle = reinterpret_cast<ChannelHandle>(tmpPtr.get());102 ChannelHandle handle = reinterpret_cast<ChannelHandle>(tmpPtr.get());
100 outHandles[i] = handle;103 outHandles[i] = handle;
101 HCCL_INFO("%s deviceId[%d], handle[0x%llx], ptr[%p]", __func__, deviceId, handle, tmpPtr.get());104 HCCL_INFO("%s deviceId[%d], handle[0x%llx], ptr[%p]", __func__, deviceId, handle, tmpPtr.get());
@@ -123,6 +126,35 @@ HcclResult ChannelProcess::CreateChannelsLoop(
123 return HCCL_SUCCESS;126 return HCCL_SUCCESS;
124}127}
125 128 
129+HcclResult ChannelProcess::InsertPluginChannelToMap(ChannelHandle handle, std::shared_ptr<Channel> channelPtr)
130+{
131+ std::lock_guard<std::mutex> lock(g_ChannelMapMtx);
132+ if (g_ChannelMap.find(handle) != g_ChannelMap.end()) {
133+ HCCL_ERROR("[NicPlugin][%s] channel handle already exists [0x%llx] in ChannelMap", __func__, handle);
134+ return HCCL_E_INTERNAL;
135+ }
136+ 
137+ g_ChannelMap.emplace(handle, std::move(channelPtr));
138+ HCCL_INFO("[NicPlugin][%s] plugin channel inserted, handle[0x%llx].", __func__, handle);
139+ 
140+ return HCCL_SUCCESS;
141+}
142+ 
143+HcclResult ChannelProcess::RemovePluginChannelFromMap(ChannelHandle handle)
144+{
145+ std::lock_guard<std::mutex> lock(g_ChannelMapMtx);
146+ 
147+ auto iter = g_ChannelMap.find(handle);
148+ if (iter == g_ChannelMap.end()) {
149+ HCCL_ERROR("[%s] channel not found in g_ChannelMap, handle[0x%llx].", __func__, handle);
150+ return HCCL_E_NOT_FOUND;
151+ }
152+ g_ChannelMap.erase(iter);
153+ 
154+ HCCL_INFO("[%s] unregister plugin channel, handle[0x%llx].", __func__, handle);
155+ return HCCL_SUCCESS;
156+}
157+ 
126HcclResult158HcclResult
127ChannelProcess::ChannelUpdateMemInfo(HcommMemHandle* memHandles, uint32_t memHandleNum, ChannelHandle channelHandle)159ChannelProcess::ChannelUpdateMemInfo(HcommMemHandle* memHandles, uint32_t memHandleNum, ChannelHandle channelHandle)
128{160{
@@ -95,6 +95,8 @@ public:
95 static HcclResult LaunchChannelKernel(95 static HcclResult LaunchChannelKernel(
96 ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,96 ChannelHandle* channelHandles, ChannelHandle* hostChannelHandles, HcommChannelDesc* hcommDesc, uint32_t listNum,
97 aclrtBinHandle binHandle);97 aclrtBinHandle binHandle);
98+ static HcclResult InsertPluginChannelToMap(ChannelHandle handle, std::shared_ptr<Channel> channelPtr);
99+ static HcclResult RemovePluginChannelFromMap(ChannelHandle handle);
98 100 
99private:101private:
100 template <typename Func>102 template <typename Func>