Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "hccl_mem_defs.h"
11 : #include "aicputs_hccs_endpoint.h"
12 : #include "log.h"
13 : #include "net_dev/global_net_dev_manager.h"
14 : #include "hccs_reged_mem_mgr.h"
15 :
16 : using namespace hccl;
17 :
18 : namespace hcomm {
19 14 : AicpuTsHccsEndpoint::AicpuTsHccsEndpoint(const EndpointDesc &endpointDesc)
20 14 : : Endpoint(endpointDesc)
21 : {
22 14 : }
23 :
24 28 : AicpuTsHccsEndpoint::~AicpuTsHccsEndpoint()
25 : {
26 : try {
27 14 : (void)ServerSocketStopListenImpl(serverPort_);
28 0 : } catch (...) { }
29 :
30 :
31 14 : if (regedMemMgr_ != nullptr) {
32 14 : regedMemMgr_ = nullptr;
33 : }
34 :
35 : try {
36 14 : if (netDevCtx_ != nullptr) {
37 14 : (void)hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId).UnRefNetDevCtx(
38 14 : NicType::VNIC_TYPE, devIpAddr_, serverPort_);
39 14 : netDevCtx_ = nullptr;
40 : }
41 0 : } catch (...) { }
42 28 : }
43 :
44 14 : HcclResult AicpuTsHccsEndpoint::Init()
45 : {
46 14 : HCCL_INFO("[%s]localEndpoint protocol[%d], type[%u], id[%u] locType[%u], devPhyId[%u], serverIdx[%u], "
47 : "superDevId[%u], superPodIdx[%u]",
48 : __func__, endpointDesc_.protocol, endpointDesc_.commAddr.type, endpointDesc_.commAddr.id,
49 : endpointDesc_.loc.locType, endpointDesc_.loc.device.devPhyId, endpointDesc_.loc.device.serverIdx,
50 : endpointDesc_.loc.device.superDevId, endpointDesc_.loc.device.superPodIdx);
51 :
52 14 : if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_DEVICE) {
53 0 : HCCL_INFO("[AicpuTsHccsEndpoint][%s] AicpuTsHccsEndpoint not support host", __func__);
54 0 : return HCCL_E_NOT_SUPPORT;
55 : }
56 :
57 14 : u32 devPhyId = endpointDesc_.loc.device.devPhyId;
58 14 : uint32_t superDevId = endpointDesc_.loc.device.superDevId;
59 14 : CHK_RET(GlobalNetDevMgr::GetDeviceVnicIP(devPhyId, superDevId, devIpAddr_));
60 14 : HCCL_INFO("[AicpuTsHccsEndpoint]devPhyId[%u] superDevId[%u] devIpAddr_[%s] ",
61 : devPhyId, superDevId, devIpAddr_.GetReadableAddress());
62 :
63 14 : CHK_RET(hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId).RefNetDevCtx(
64 : NicType::VNIC_TYPE, devIpAddr_, serverPort_, netDevCtx_));
65 14 : EXCEPTION_CATCH(regedMemMgr_ = std::make_shared<HccsRegedMemMgr>(netDevCtx_), return HCCL_E_PARA);
66 14 : return HCCL_SUCCESS;
67 : }
68 :
69 0 : HcclResult AicpuTsHccsEndpoint::ServerSocketListen(const uint32_t port)
70 : {
71 0 : CHK_RET(hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId).ServerInit(serverPort_));
72 0 : serverListened_ = true;
73 0 : return HCCL_SUCCESS;
74 : }
75 :
76 14 : inline HcclResult AicpuTsHccsEndpoint::ServerSocketStopListenImpl(const uint32_t port)
77 : {
78 14 : if (serverListened_) {
79 0 : CHK_RET(hccl::GlobalNetDevMgr::GetInstance(endpointDesc_.loc.device.devPhyId).ServerDeInit(port));
80 0 : serverListened_ = false;
81 : }
82 :
83 14 : return HCCL_SUCCESS;
84 : }
85 :
86 0 : HcclResult AicpuTsHccsEndpoint::ServerSocketStopListen(const uint32_t port)
87 : {
88 0 : return ServerSocketStopListenImpl(port);
89 : }
90 :
91 13 : HcclResult AicpuTsHccsEndpoint::RegisterMemory(HcommMem mem, const char *memTag, void **memHandle)
92 : {
93 13 : CHK_RET(GetRegedMemMgr()->RegisterMemory(mem, memTag, memHandle));
94 10 : return HCCL_SUCCESS;
95 : }
96 :
97 13 : HcclResult AicpuTsHccsEndpoint::UnregisterMemory(void* memHandle)
98 : {
99 13 : CHK_RET(GetRegedMemMgr()->UnregisterMemory(memHandle));
100 10 : return HCCL_SUCCESS;
101 : }
102 :
103 2 : HcclResult AicpuTsHccsEndpoint::MemoryExport(void *memHandle, void **memDesc, uint32_t *memDescLen)
104 : {
105 2 : CHK_RET(GetRegedMemMgr()->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
106 2 : return HCCL_SUCCESS;
107 : }
108 :
109 2 : HcclResult AicpuTsHccsEndpoint::MemoryImport(const void *memDesc, uint32_t descLen, HcommMem *outMem)
110 : {
111 2 : CHK_RET(GetRegedMemMgr()->MemoryImport(memDesc, descLen, outMem));
112 2 : return HCCL_SUCCESS;
113 : }
114 :
115 2 : HcclResult AicpuTsHccsEndpoint::MemoryUnimport(const void *memDesc, uint32_t descLen)
116 : {
117 2 : CHK_RET(GetRegedMemMgr()->MemoryUnimport(memDesc, descLen));
118 2 : return HCCL_SUCCESS;
119 : }
120 :
121 0 : HcclResult AicpuTsHccsEndpoint::GetAllMemHandles(void **memHandles, uint32_t *memHandleNum)
122 : {
123 0 : CHK_RET(GetRegedMemMgr()->GetAllMemHandles(memHandles, memHandleNum));
124 0 : return HCCL_SUCCESS;
125 : }
126 :
127 2 : HcclResult AicpuTsHccsEndpoint::MemoryGrant(const HcommMemGrantInfo *remoteGrantInfo)
128 : {
129 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
130 2 : CHK_PTR_NULL(mgr);
131 2 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
132 2 : CHK_RET(hccsRegedMemMgr->MemoryGrant(remoteGrantInfo));
133 2 : return HCCL_SUCCESS;
134 2 : }
135 :
136 2 : HcclResult AicpuTsHccsEndpoint::MemoryEnableP2P(const EndpointDesc &remoteEndpointDesc)
137 : {
138 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
139 2 : CHK_PTR_NULL(mgr);
140 2 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
141 2 : CHK_RET(hccsRegedMemMgr->MemoryEnableP2P(GetEndpointDesc(), remoteEndpointDesc));
142 2 : return HCCL_SUCCESS;
143 2 : }
144 :
145 2 : HcclResult AicpuTsHccsEndpoint::MemoryDisableP2P(const EndpointDesc &remoteEndpointDesc)
146 : {
147 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
148 2 : CHK_PTR_NULL(mgr);
149 2 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
150 2 : CHK_RET(hccsRegedMemMgr->MemoryDisableP2P(GetEndpointDesc(), remoteEndpointDesc));
151 2 : return HCCL_SUCCESS;
152 2 : }
153 :
154 2 : HcclResult AicpuTsHccsEndpoint::MemoryOpenRemoteIpc()
155 : {
156 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
157 2 : CHK_PTR_NULL(mgr);
158 2 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
159 2 : CHK_RET(hccsRegedMemMgr->MemoryOpenRemoteIpc());
160 2 : return HCCL_SUCCESS;
161 2 : }
162 :
163 2 : HcclResult AicpuTsHccsEndpoint::MemoryCloseRemoteIpc()
164 : {
165 2 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
166 2 : CHK_PTR_NULL(mgr);
167 2 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
168 2 : CHK_RET(hccsRegedMemMgr->MemoryCloseRemoteIpc());
169 2 : return HCCL_SUCCESS;
170 2 : }
171 :
172 0 : HcclResult AicpuTsHccsEndpoint::GetRemoteIpcRmaBuffer(std::vector<CommMem> &remoteIpcRmaBufferVec)
173 : {
174 0 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
175 0 : CHK_PTR_NULL(mgr);
176 0 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
177 0 : CHK_RET(hccsRegedMemMgr->GetRemoteIpcRmaBuffer(remoteIpcRmaBufferVec));
178 0 : return HCCL_SUCCESS;
179 0 : }
180 :
181 0 : HcclResult AicpuTsHccsEndpoint::GetRemoteIpcRmaBufferEx(std::vector<HcclMemEx> &remoteIpcRmaBufferVecEx)
182 : {
183 0 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
184 0 : CHK_PTR_NULL(mgr);
185 0 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
186 0 : CHK_RET(hccsRegedMemMgr->GetRemoteIpcRmaBufferEx(remoteIpcRmaBufferVecEx));
187 0 : return HCCL_SUCCESS;
188 0 : }
189 :
190 0 : HcclResult AicpuTsHccsEndpoint::GetLocalIpcRmaBufferEx(std::vector<HcclMemEx> &localIpcRmaBufferVecEx)
191 : {
192 0 : std::shared_ptr<RegedMemMgr> mgr = GetRegedMemMgr();
193 0 : CHK_PTR_NULL(mgr);
194 0 : HccsRegedMemMgr *hccsRegedMemMgr = (HccsRegedMemMgr *)mgr.get();
195 0 : CHK_RET(hccsRegedMemMgr->GetLocalIpcRmaBufferEx(localIpcRmaBufferVecEx));
196 0 : return HCCL_SUCCESS;
197 0 : }
198 : }
|