Merged
feat​:host网卡插件机制框架 #4257
gxdjsntAdd on Jul 30
feat​:host网卡插件机制框架 #4257
Merged
gxdjsntAdd on Jul 30
gxdjsnt
gxdjsnt
Jul 30

描述

为 HCOMM 增加 Host 网卡(NIC)插件机制,允许开发者以独立 .so 文件形式替换内置协议实现或拓展全新通信协议,无需修改 HCOMM 原始代码。
采用 C 函数指针表(ops table)嵌入 Channel / Endpoint 基类,创建时填入对应的 ops 表 + ctx 上下文,调用时通过 C 函数指针直接分派。

核心思路:在 Channel / Endpoint 基类中嵌入两个字段 —— nicOps_(ops 表指针)和 nicCtx_(ctx 参数)。创建时填入,运行时统一走 C 函数指针调用。内置实现通过 g_BuiltinChannelOps / g_BuiltinEndpointOps 包装函数表接入同一机制,插件实现通过 dlopen 加载的Endpont和Channel创建时填入。

对原内置接口分发的变化:
(1)enpoint和channel基类新增了nicOps和nicCtx用于实现和插件等同的数据面和控制面接口分发。
(2)在内置enpoint和channel创建时会赋值nicOps和nicCtx,nicCtx是对象本身,nicOps是包装的内置接口实现。
(3)nicOps使用全局变量,enpoint使用g_BuiltinEndpointOps,channel使用g_BuiltinChannelOps。
(4)原cpu_primitives_c_adpt.cc和hcomm_mem_c_adpt.cc中数据面和控制面接口实现挪到了builtin_channel_ops.h和builtin_endpoint_ops.h中。

变更类型

请选择本次引入的变更类型:

  • [x ] ✨ 新特性

关联的Issue

NA

测试

已完成的测试用例和场景:

  1. 内置场景 A5 host网卡通信
  2. 内置场景A2 host网卡通信
    3、内置场景有无插件机制性能对比
    4、插件场景hixl单边通信

补充的UT用例:
1、插件场景业务分发(插件接口实现,插件接口未实现)
2、内置场景业务分发

文档更新

NA

合入检查

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for gxdjsnt's contribution.)
gxdjsntgxdjsnt
Jul 30 Created pull request, commit 56a56e0d
atomgit-bot
atomgit-bot
Jul 30 Comments:

变更摘要

该 PR 实现了 host 网卡插件机制框架,核心目标是将内置实现和第三方插件实现统一到一套标准的 ops 函数表(HcommNicEndpointOps / HcommNicChannelOps)中进行分发。通过在 Endpoint 和 Channel 基类中新增 nicOps_ / nicCtx_ 成员,调用方统一通过 GetNicOps()->someOp(GetNicCtx(), ...) 方式执行操作,消除了旧有的 bool &handled 判断分发的模式。同时大幅扩展了 Channel ops 接口(增加 OnThread、reduce、batch、drain 等变体),引入默认实现填充机制,并移除了 nic_plugin_dispatcher 中旧的分离式分发代码。

主要改动

  • 统一分发机制:在 Endpoint(endpoint.h)和 Channel(channel.h)基类中新增 nicOps_、nicCtx_ 成员及 SetNicEndpointCtx/SetNicChannelCtx、GetNicOps/GetNicCtx 方法;内置实现通过 kBuiltinEndpointOps(builtin_endpoint_ops.h)和 kBuiltinChannelOps(builtin_channel_ops.h)注入,插件实现通过 CreatePluginEndpointHolder/CreatePluginChannels 注入。

  • 新增 PluginEndpointHolder / PluginChannelHolder 占位类:新增 nic_plugin_holder.h,定义 PluginEndpointHolder(继承 Endpoint)和 PluginChannelHolder(继承 Channel),在 EndpointMap/ChannelMap 中占位,所有虚函数重写返回 HCCL_E_NOT_SUPPORT,实际业务逻辑完全由插件 ops 表提供。

  • Ops 结构体大幅扩展:HcommNicChannelOps(hcomm_nic_plugin.h)新增 writeOnThread、writeNbiOnThread、readOnThread、readNbiOnThread、writeReduceOnThread、readReduceOnThread、writeReduceWithNotifyOnThread、batchTransferOnThread、fenceOnThread、drainOnThread、notifyRecordOnThread、notifyWaitOnThread、notifyWaitOnThreadWithDefaultTimeout 等字段;HcommNicEndpointOps 新增 getListenPort 字段,并调整了字段顺序。

  • 默认实现填充机制:FillDefaultEndpointOps / FillDefaultChannelOps(nic_plugin_manager.h/cc)将插件提供的 ops 复制后,通过宏 FOR_EACH_ENDPOINT_OP_DEFAULT / FOR_EACH_CHANNEL_OP_DEFAULT 遍历,自动将 nullptr 字段替换为 Default* 函数(记录 warning 日志并返回 HCCL_E_NOT_SUPPORT),destroy 必须由插件实现。

  • 移除旧 dispatcher:nic_plugin_dispatcher.cc 删除了约 390 行代码,移除了所有 PluginChannel* / PluginEndpoint* 函数、PluginEndpointCtx / PluginChannelCtx 结构体、PLUGIN_EP_CTX / PLUGIN_CH_CTX / MAKE_PLUGIN_EP_HANDLE 宏,以及 bool &handled 分发模式。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 30 Comments:

代码审查

Now I have completed the review. Let me provide the closing summary.


审查总结

按优先级统计

  • P0: 1 个 — CreatePluginChannels 中 InsertChannelToMap 失败路径上的 double-free + use-after-free
  • P1: 0 个
  • P2: 1 个 — HcommChannelNotifyWaitOnThread 实验路径遗漏 DEV_TYPE_960
  • P3: 0 个

整体风险评估

中高风险。P0 的 double-free 是一个明确的内存安全漏洞,会在 InsertChannelToMap 因 handle 冲突失败时触发(例如同一设备上重复创建相同 handle 的 channel)。P2 的 DEV_TYPE_960 遗漏会导致该设备类型在 ENABLE_EXPERIMENTAL 路径上 NotifyWait 行为回退,可能引发通信正确性问题。

各文件审查确认

文件 结果
experimental/base_comm/channel/channel.h 无问题(新增 HcommChannelLinkStatus 枚举和 HcommChannelKind 值)
experimental/base_comm/nic_plugin/nic_plugin_ops.h 无问题(重构 ops 表,GetStatus 返回码变更是设计意图)
src/base_comm/primitives/api_c_adpt/CMakeLists.txt 无问题(始终启用 ENABLE_EXPERIMENTAL 和 nic_plugin 子目录)
src/base_comm/primitives/api_c_adpt/cpu_primitives_c_adpt.cc P2: 第 733 行遗漏 DEV_TYPE_960
src/base_comm/primitives/api_c_adpt/hcomm_c_adpt.cc P0: 第 713-718 行 double-free
src/base_comm/primitives/api_c_adpt/nic_plugin/hcomm_nic_plugin.h 无问题(C ABI 结构体重构,新增 getListenPort 等字段)
src/base_comm/primitives/api_c_adpt/nic_plugin/nic_plugin_dispatcher.cc 无问题(清空,仅保留命名空间占位)
src/base_comm/primitives/api_c_adpt/nic_plugin/nic_plugin_dispatcher.h 无问题(清空声明)
src/base_comm/primitives/api_c_adpt/nic_plugin/nic_plugin_holder.h 无问题(新增 PluginEndpointHolder / PluginChannelHolder 占位类)
src/base_comm/primitives/api_c_adpt/nic_plugin/nic_plugin_manager.cc 无问题(新增 FillDefault 函数、默认实现)
src/base_comm/primitives/api_c_adpt/nic_plugin/nic_plugin_manager.h 无问题(新增宏、模板函数声明)
src/base_comm/resources/endpoint_pairs/channels/builtin_channel_ops.h 无问题(新增内置 channel ops 表)
src/base_comm/resources/endpoint_pairs/channels/channel.h 无问题(新增 nicOps_/nicCtx_ 成员和访问器)
src/base_comm/resources/endpoint_pairs/channels/channel_process.cc 无问题(新增 InsertChannelToMap / RemoveChannelFromMap)
src/base_comm/resources/endpoint_pairs/channels/channel_process.h 无问题(新增方法声明)
src/base_comm/resources/endpoints/builtin_endpoint_ops.h 无问题(新增内置 endpoint ops 表)
src/base_comm/resources/endpoints/endpoint.h 无问题(新增 nicOps_/nicCtx_ 成员和访问器)
test/ut/.../ut_cpu_HcommChannelFenceOnThread.cc 无问题(SetUp 中设置 builtinChannelOps)
test/ut/.../ut_cpu_HcommChannelNotifyWaitOnThread.cc 无问题
test/ut/.../ut_cpu_HcommPluginChannelOps.cc 无问题(重构测试覆盖新 ops)
test/ut/.../ut_cpu_HcommReadNbiOnThread.cc 无问题
test/ut/.../ut_cpu_HcommWriteNbiOnThread.cc 无问题
test/ut/.../ut_cpu_HcommWriteWithNotifyNbiOnThread.cc 无问题
test/ut/.../ut_hcomm_c_adpt.cc 无问题(适配新 API 的测试更新)
test/ut/.../ut_hcomm_endpoint_test.cc 无问题(新增 endpoint 统一分发测试)
test/ut/.../ut_hcomm_mem_test.cc 无问题(适配重排序后错误码变化)
test/ut/.../ut_cpu_roce_endpoint.cc 无问题(适配新路径的测试更新)
类型 数量
🔴 阻塞 1
🟡 建议 3

⛔ 需要修改

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 30 Added label: cann-cla/yes
CANN-robotCANN-robotCollaborators
Jul 30 Added label: stat/needs-squash
335 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 12 Added label: approved
philophiloCollaborators
Aug 12 Resolved final issue
yanglianxiang
yanglianxiangCollaborators
Aug 12 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Aug 12 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 12 Merged pull request