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 :
11 : #include "cpu_urma_endpoint.h"
12 : #include <algorithm>
13 : #include "endpoint_mgr.h"
14 : #include "log.h"
15 : #include "urma_mem.h"
16 : #include "proc_reged_mem_mgr_cache.h"
17 : #include "adapter_rts_common.h"
18 : #include "server_socket_manager.h"
19 : #include "hccp_peer_manager.h"
20 :
21 : namespace hcomm {
22 23 : CpuUrmaEndpoint::CpuUrmaEndpoint(const EndpointDesc& endpointDesc) : Endpoint(endpointDesc) {}
23 :
24 46 : CpuUrmaEndpoint::~CpuUrmaEndpoint() noexcept
25 : {
26 23 : std::lock_guard<std::mutex> lock(portMutex_);
27 23 : if (dynamicPort_ != HCCL_INVALID_PORT) {
28 1 : ServerSocketStopListenImpl(dynamicPort_);
29 : }
30 23 : dynamicPort_ = HCCL_INVALID_PORT;
31 23 : ProcRegedMemMgrCache::GetInstance().Release(cacheKey_);
32 46 : }
33 :
34 23 : HcclResult CpuUrmaEndpoint::Init()
35 : {
36 23 : HCCL_INFO("[%s] localEndpoint protocol[%d]", __func__, endpointDesc_.protocol);
37 :
38 23 : Hccl::IpAddress ipAddr{};
39 23 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
40 : u32 devPhyId;
41 : s32 deviceLogicId;
42 23 : HcclResult ret = hrtGetDevice(&deviceLogicId);
43 23 : if (ret != HCCL_SUCCESS) {
44 0 : HCCL_ERROR("call hrtGetDevice failed, deviceLogicId[%d]", deviceLogicId);
45 0 : return ret;
46 : }
47 23 : RaSocketSetWhiteListStatus(1); // PEER模式需要手动开启白名单模式
48 23 : Hccl::HccpPeerManager::GetInstance().Init(deviceLogicId);
49 23 : ret = hrtGetDevicePhyIdByIndex(static_cast<uint32_t>(deviceLogicId), devPhyId);
50 23 : if (ret != HCCL_SUCCESS) {
51 0 : HCCL_ERROR("call hrtGetDevicePhyIdByIndex failed, deviceLogicId[%d], devPhyId[%u]", deviceLogicId, devPhyId);
52 0 : return ret;
53 : }
54 23 : auto& rdmaHandleMgr = Hccl::RdmaHandleManager::GetInstance();
55 23 : ctxHandle_ = static_cast<void*>(
56 23 : rdmaHandleMgr.GetByAddr(devPhyId, Hccl::LinkProtoType::UB, ipAddr, Hccl::PortDeploymentType::HOST_NET));
57 23 : CHK_PTR_NULL(ctxHandle_);
58 23 : HCCL_INFO(
59 : "CpuUrmaEndpoint::%s success, devPhyId[%u], ipAddr[%s], ctxHandle[%p]", __func__, devPhyId,
60 : ipAddr.Describe().c_str(), ctxHandle_);
61 :
62 23 : cacheKey_ = MemMgrCacheKey{devPhyId, COMM_PROTOCOL_UB_CTP, ipAddr, LocTypeToPortType(endpointDesc_.loc.locType)};
63 23 : auto& cache = ProcRegedMemMgrCache::GetInstance();
64 46 : EXCEPTION_CATCH(
65 : regedMemMgr_ = cache.GetOrCreate(
66 : cacheKey_,
67 : [this]() {
68 : auto m = std::make_shared<UbRegedMemMgr>();
69 : m->rdmaHandle_ = this->ctxHandle_;
70 : return m;
71 : }),
72 : return HCCL_E_PARA);
73 23 : return HCCL_SUCCESS;
74 : }
75 :
76 1 : HcclResult CpuUrmaEndpoint::ServerSocketListen(const uint32_t port)
77 : {
78 1 : Hccl::IpAddress ipAddr{};
79 1 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
80 :
81 1 : s32 devId = 0;
82 1 : CHK_RET(hrtGetDevice(&devId));
83 1 : u32 devPhyId = 0;
84 1 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
85 :
86 1 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
87 1 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
88 :
89 1 : HCCL_INFO("[CpuUrmaEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devPhyId, ipAddr.Describe().c_str());
90 :
91 1 : uint32_t requestPort = port;
92 1 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
93 : localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
94 :
95 1 : return HCCL_SUCCESS;
96 : }
97 :
98 2 : inline HcclResult CpuUrmaEndpoint::ServerSocketStopListenImpl(const uint32_t port)
99 : {
100 2 : Hccl::IpAddress ipAddr{};
101 2 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
102 :
103 2 : s32 devId = 0;
104 2 : CHK_RET(hrtGetDevice(&devId));
105 2 : u32 devPhyId = 0;
106 2 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
107 2 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
108 2 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
109 2 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStopListen(localPort, Hccl::NicType::HOST_NIC_TYPE, port));
110 :
111 1 : return HCCL_SUCCESS;
112 : }
113 :
114 1 : HcclResult CpuUrmaEndpoint::ServerSocketStopListen(const uint32_t port) { return ServerSocketStopListenImpl(port); }
115 :
116 1 : HcclResult CpuUrmaEndpoint::ServerSocketGetListenPort(uint32_t* port)
117 : {
118 1 : std::lock_guard<std::mutex> lock(portMutex_);
119 1 : Hccl::IpAddress localIpAddr{};
120 1 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, localIpAddr));
121 :
122 1 : s32 deviceId = 0;
123 1 : CHK_RET(hrtGetDevice(&deviceId));
124 1 : u32 devicePhyId = 0;
125 1 : CHK_RET(hrtGetDevicePhyIdByIndex(deviceId, devicePhyId));
126 :
127 1 : Hccl::DevNetPortType portType = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
128 1 : Hccl::PortData portData = Hccl::PortData(devicePhyId, portType, 0, localIpAddr);
129 :
130 1 : HCCL_INFO(
131 : "[CpuUrmaEndpoint::%s] devicePhyId[%u] ipAddress[%s]", __func__, devicePhyId, localIpAddr.Describe().c_str());
132 :
133 : // 已有监听端口则直接返回
134 1 : if (dynamicPort_ != HCCL_INVALID_PORT) {
135 0 : *port = dynamicPort_;
136 0 : HCCL_INFO("[CpuUrmaEndpoint::%s] already listening, return existing port[%u]", __func__, dynamicPort_);
137 0 : return HCCL_SUCCESS;
138 : }
139 :
140 1 : uint32_t requestPort = 0;
141 1 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(
142 : portData, Hccl::NicType::HOST_NIC_TYPE, devicePhyId, &requestPort));
143 1 : if (requestPort == 0 || requestPort == HCCL_INVALID_PORT) {
144 0 : HCCL_ERROR("[CpuUrmaEndpoint::%s] get listen port failed, port is invalid.", __func__);
145 0 : return HCCL_E_NETWORK;
146 : }
147 1 : dynamicPort_ = requestPort;
148 1 : *port = dynamicPort_;
149 :
150 1 : return HCCL_SUCCESS;
151 1 : }
152 :
153 1 : HcclResult CpuUrmaEndpoint::RegisterMemory(HcommMem mem, const char* memTag, void** memHandle)
154 : {
155 1 : CHK_RET(this->regedMemMgr_->RegisterMemory(mem, memTag, memHandle));
156 1 : return HCCL_SUCCESS;
157 : }
158 :
159 1 : HcclResult CpuUrmaEndpoint::UnregisterMemory(void* memHandle)
160 : {
161 1 : CHK_RET(this->regedMemMgr_->UnregisterMemory(memHandle));
162 1 : return HCCL_SUCCESS;
163 : }
164 :
165 0 : HcclResult CpuUrmaEndpoint::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 CpuUrmaEndpoint::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 CpuUrmaEndpoint::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 CpuUrmaEndpoint::GetAllMemHandles(void** memHandles, uint32_t* memHandleNum)
184 : {
185 0 : CHK_RET(this->regedMemMgr_->GetAllMemHandles(memHandles, memHandleNum));
186 0 : return HCCL_SUCCESS;
187 : }
188 :
189 : } // namespace hcomm
|