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 "endpoint_mgr.h"
11 : #include "hccl_mem_defs.h"
12 : #include "cpu_roce_endpoint.h"
13 : #include "hccl/hccl_res.h"
14 : #include "log.h"
15 : #include "roce_mem.h"
16 : #include "proc_reged_mem_mgr_cache.h"
17 : #include "host_socket_handle_manager.h"
18 : #include "adapter_rts_common.h"
19 : #include "hccp_peer_manager.h"
20 : #include "server_socket_manager.h"
21 : #include "hccp.h"
22 :
23 : using Hccl::HcclException;
24 : using std::exception;
25 : using std::string;
26 :
27 : namespace hcomm {
28 132 : CpuRoceEndpoint::CpuRoceEndpoint(const EndpointDesc& endpointDesc) : Endpoint(endpointDesc) {}
29 :
30 262 : CpuRoceEndpoint::~CpuRoceEndpoint() noexcept
31 : {
32 132 : std::lock_guard<std::mutex> lock(portMutex_);
33 132 : if (dynamicPort_ != HCCL_INVALID_PORT) {
34 0 : ServerSocketStopListenImpl(dynamicPort_);
35 : }
36 132 : dynamicPort_ = HCCL_INVALID_PORT;
37 132 : ProcRegedMemMgrCache::GetInstance().Release(cacheKey_);
38 262 : }
39 :
40 132 : HcclResult CpuRoceEndpoint::Init()
41 : {
42 132 : HCCL_INFO("[%s] localEndpoint protocol[%d]", __func__, endpointDesc_.protocol);
43 :
44 132 : if (endpointDesc_.loc.locType != ENDPOINT_LOC_TYPE_HOST) {
45 0 : HCCL_INFO("[CpuRoceEndpoint][%s] CpuRoceEndpoint not support device", __func__);
46 0 : return HCCL_E_NOT_SUPPORT;
47 : }
48 132 : Hccl::IpAddress ipAddr{};
49 132 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
50 132 : s32 devId = 0;
51 132 : CHK_RET(hrtGetDevice(&devId));
52 132 : EXCEPTION_CATCH(Hccl::HccpPeerManager::GetInstance().Init(devId), return HCCL_E_INTERNAL);
53 132 : u32 devPhyId = 0;
54 132 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
55 132 : auto& rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
56 132 : TRY_CATCH_RETURN(
57 : ctxHandle_ = static_cast<void*>(
58 : rdmaHandleMgr.GetByAddr(devPhyId, Hccl::LinkProtoType::RDMA, ipAddr, Hccl::PortDeploymentType::HOST_NET)));
59 132 : CHK_PTR_NULL(ctxHandle_);
60 131 : HCCL_INFO(
61 : "CpuRoceEndpoint::%s success, devPhyId[%u], ipAddr[%s], ctxHandle[%p]", __func__, devPhyId,
62 : ipAddr.Describe().c_str(), ctxHandle_);
63 :
64 131 : cacheKey_ = MemMgrCacheKey{devPhyId, COMM_PROTOCOL_ROCE, ipAddr, LocTypeToPortType(endpointDesc_.loc.locType)};
65 131 : auto& cache = ProcRegedMemMgrCache::GetInstance();
66 145 : EXCEPTION_CATCH(
67 : regedMemMgr_ = cache.GetOrCreate(
68 : cacheKey_,
69 : [this]() {
70 : auto m = std::make_shared<RoceRegedMemMgr>();
71 : m->rdmaHandle_ = this->ctxHandle_;
72 : return m;
73 : }),
74 : return HCCL_E_PARA);
75 131 : return HCCL_SUCCESS;
76 : }
77 :
78 4 : HcclResult CpuRoceEndpoint::ServerSocketListen(const uint32_t port)
79 : {
80 4 : Hccl::IpAddress ipAddr{};
81 4 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
82 :
83 4 : s32 devId = 0;
84 4 : CHK_RET(hrtGetDevice(&devId));
85 4 : u32 devPhyId = 0;
86 4 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
87 :
88 4 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
89 4 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
90 :
91 4 : HCCL_INFO("[CpuRoceEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devPhyId, ipAddr.Describe().c_str());
92 :
93 4 : uint32_t requestPort = port;
94 4 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
95 : localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
96 4 : return HCCL_SUCCESS;
97 : }
98 :
99 2 : inline HcclResult CpuRoceEndpoint::ServerSocketStopListenImpl(const uint32_t port)
100 : {
101 2 : Hccl::IpAddress ipAddr{};
102 2 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
103 :
104 2 : s32 devId = 0;
105 2 : CHK_RET(hrtGetDevice(&devId));
106 2 : u32 devPhyId = 0;
107 2 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
108 :
109 2 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
110 2 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
111 2 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStopListen(localPort, Hccl::NicType::HOST_NIC_TYPE, port));
112 :
113 2 : return HCCL_SUCCESS;
114 : }
115 :
116 2 : HcclResult CpuRoceEndpoint::ServerSocketStopListen(const uint32_t port) { return ServerSocketStopListenImpl(port); }
117 :
118 0 : HcclResult CpuRoceEndpoint::ServerSocketGetListenPort(uint32_t* port)
119 : {
120 0 : std::lock_guard<std::mutex> lock(portMutex_);
121 0 : CHK_PTR_NULL(port);
122 0 : s32 devId = 0;
123 0 : CHK_RET(hrtGetDevice(&devId));
124 0 : u32 devPhyId = 0;
125 0 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
126 :
127 0 : Hccl::IpAddress ipAddr{};
128 0 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
129 :
130 0 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::RDMA);
131 0 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
132 :
133 0 : HCCL_INFO("[CpuRoceEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devPhyId, ipAddr.Describe().c_str());
134 :
135 : // 已有监听端口则直接返回
136 0 : if (dynamicPort_ != HCCL_INVALID_PORT) {
137 0 : *port = dynamicPort_;
138 0 : HCCL_INFO("[CpuRoceEndpoint::%s] already listening, return existing port[%u]", __func__, dynamicPort_);
139 0 : return HCCL_SUCCESS;
140 : }
141 0 : uint32_t requestPort = 0;
142 0 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
143 : localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
144 0 : if (requestPort == 0 || requestPort == HCCL_INVALID_PORT) {
145 0 : HCCL_ERROR("[CpuRoceEndpoint::%s] get listen port failed, port is invalid", __func__);
146 0 : return HCCL_E_NETWORK;
147 : }
148 0 : dynamicPort_ = requestPort;
149 0 : *port = dynamicPort_;
150 0 : return HCCL_SUCCESS;
151 0 : }
152 :
153 1 : HcclResult CpuRoceEndpoint::RegisterMemory(HcommMem mem, const char* memTag, void** memHandle)
154 : {
155 1 : CHK_RET(this->regedMemMgr_->RegisterMemory(mem, memTag, memHandle));
156 0 : return HCCL_SUCCESS;
157 : }
158 :
159 2 : HcclResult CpuRoceEndpoint::UnregisterMemory(void* memHandle)
160 : {
161 2 : CHK_RET(this->regedMemMgr_->UnregisterMemory(memHandle));
162 0 : return HCCL_SUCCESS;
163 : }
164 :
165 0 : HcclResult CpuRoceEndpoint::MemoryExport(void* memHandle, void** memDesc, uint32_t* memDescLen)
166 : {
167 0 : CHK_RET(this->regedMemMgr_->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
168 0 : return HCCL_SUCCESS;
169 : }
170 :
171 0 : HcclResult CpuRoceEndpoint::MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem)
172 : {
173 0 : CHK_RET(this->regedMemMgr_->MemoryImport(memDesc, descLen, outMem));
174 0 : return HCCL_SUCCESS;
175 : }
176 :
177 0 : HcclResult CpuRoceEndpoint::MemoryUnimport(const void* memDesc, uint32_t descLen)
178 : {
179 0 : CHK_RET(this->regedMemMgr_->MemoryUnimport(memDesc, descLen));
180 0 : return HCCL_SUCCESS;
181 : }
182 :
183 0 : HcclResult CpuRoceEndpoint::GetAllMemHandles(void** memHandles, uint32_t* memHandleNum)
184 : {
185 0 : CHK_RET(this->regedMemMgr_->GetAllMemHandles(memHandles, memHandleNum));
186 0 : return HCCL_SUCCESS;
187 : }
188 :
189 65 : HcclResult CpuRoceEndpoint::GetCapabilities(Capabilities& caps)
190 : {
191 65 : HCCL_INFO("[CpuRoceEndpoint::%s] START.", __func__);
192 : static constexpr uint64_t RDMA_MAX_WR_LENGTH = 1ULL * 1024 * 1024 * 1024; // 单次RDMA操作最大长度1GB
193 65 : if (!isCapabilitiesAvailable_) {
194 : // 待 HCCP 提供查询设备支持的最大发送消息的接口后,查询设备实际值。
195 64 : capabilities_.maxMsgSize = RDMA_MAX_WR_LENGTH;
196 64 : uint32_t ret = RaGetLbMax(this->regedMemMgr_->rdmaHandle_, &(capabilities_.lbMax));
197 64 : HCCL_DEBUG("[CpuRoceEndpoint::GetCapabilities] lbMax = %d.", capabilities_.lbMax);
198 64 : CHK_PRT_RET(
199 : ret != 0,
200 : HCCL_ERROR(
201 : "[CpuRoceEndpoint::GetCapabilities][GetLbMax]errNo[0x%016llx] RaGetLbMax fail. "
202 : "return[%u], params: rdmaHandle[%p], lbMax[%d]",
203 : HCCL_ERROR_CODE(HCCL_E_NETWORK), ret, this->regedMemMgr_->rdmaHandle_, capabilities_.lbMax),
204 : HCCL_E_NETWORK);
205 64 : isCapabilitiesAvailable_ = true;
206 : }
207 65 : caps = capabilities_;
208 65 : HCCL_INFO("[CpuRoceEndpoint::%s] SUCCESS.", __func__);
209 65 : return HCCL_SUCCESS;
210 : }
211 : } // namespace hcomm
|