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 "cpu_urma_endpoint.h"
11 : #include <algorithm>
12 : #include "endpoint_mgr.h"
13 : #include "log.h"
14 : #include "urma_mem.h"
15 : #include "adapter_rts_common.h"
16 : #include "server_socket_manager.h"
17 : #include "hccp_peer_manager.h"
18 :
19 : namespace hcomm {
20 23 : CpuUrmaEndpoint::CpuUrmaEndpoint(const EndpointDesc &endpointDesc)
21 23 : : Endpoint(endpointDesc)
22 : {
23 23 : }
24 :
25 46 : CpuUrmaEndpoint::~CpuUrmaEndpoint() noexcept
26 : {
27 23 : std::lock_guard<std::mutex> lock(portMutex_);
28 23 : if (dynamicPort_ != HCCL_INVALID_PORT) {
29 1 : ServerSocketStopListenImpl(dynamicPort_);
30 : }
31 23 : dynamicPort_ = HCCL_INVALID_PORT;
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("CpuUrmaEndpoint::%s success, devId[%u], ipAddr[%s], ctxHandle[%p]",
59 : __func__,
60 : devPhyId,
61 : ipAddr.Describe().c_str(),
62 : ctxHandle_);
63 :
64 23 : EXCEPTION_CATCH(regedMemMgr_ = std::make_unique<UbRegedMemMgr>(), return HCCL_E_PARA);
65 23 : this->regedMemMgr_->rdmaHandle_ = this->ctxHandle_;
66 23 : return HCCL_SUCCESS;
67 : }
68 :
69 1 : HcclResult CpuUrmaEndpoint::ServerSocketListen(const uint32_t port)
70 : {
71 1 : Hccl::IpAddress ipAddr{};
72 1 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
73 :
74 1 : s32 devId = 0;
75 1 : CHK_RET(hrtGetDevice(&devId));
76 1 : u32 devPhyId = 0;
77 1 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
78 :
79 1 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
80 1 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
81 :
82 1 : HCCL_INFO("[CpuUrmaEndpoint::%s] devicePhyId[%u] ipAddress[%s]",
83 : __func__, devPhyId, ipAddr.Describe().c_str());
84 :
85 1 : uint32_t requestPort = port;
86 1 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(localPort, Hccl::NicType::HOST_NIC_TYPE, devPhyId, &requestPort));
87 :
88 1 : return HCCL_SUCCESS;
89 : }
90 :
91 2 : inline HcclResult CpuUrmaEndpoint::ServerSocketStopListenImpl(const uint32_t port)
92 : {
93 2 : Hccl::IpAddress ipAddr{};
94 2 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, ipAddr));
95 :
96 2 : s32 devId = 0;
97 2 : CHK_RET(hrtGetDevice(&devId));
98 2 : u32 devPhyId = 0;
99 2 : CHK_RET(hrtGetDevicePhyIdByIndex(devId, devPhyId));
100 2 : Hccl::DevNetPortType type = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
101 2 : Hccl::PortData localPort = Hccl::PortData(devPhyId, type, 0, ipAddr);
102 2 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStopListen(localPort, Hccl::NicType::HOST_NIC_TYPE, port));
103 :
104 1 : return HCCL_SUCCESS;
105 : }
106 :
107 1 : HcclResult CpuUrmaEndpoint::ServerSocketStopListen(const uint32_t port)
108 : {
109 1 : return ServerSocketStopListenImpl(port);
110 : }
111 :
112 1 : HcclResult CpuUrmaEndpoint::ServerSocketGetListenPort(uint32_t *port)
113 : {
114 1 : std::lock_guard<std::mutex> lock(portMutex_);
115 1 : Hccl::IpAddress localIpAddr{};
116 1 : CHK_RET(CommAddrToIpAddress(endpointDesc_.commAddr, localIpAddr));
117 :
118 1 : s32 deviceId = 0;
119 1 : CHK_RET(hrtGetDevice(&deviceId));
120 1 : u32 devicePhyId = 0;
121 1 : CHK_RET(hrtGetDevicePhyIdByIndex(deviceId, devicePhyId));
122 :
123 1 : Hccl::DevNetPortType portType = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB);
124 1 : Hccl::PortData portData = Hccl::PortData(devicePhyId, portType, 0, localIpAddr);
125 :
126 1 : HCCL_INFO("[CpuUrmaEndpoint::%s] devicePhyId[%u] ipAddress[%s]",
127 : __func__, devicePhyId, localIpAddr.Describe().c_str());
128 :
129 : // 已有监听端口则直接返回
130 1 : if (dynamicPort_ != HCCL_INVALID_PORT) {
131 0 : *port = dynamicPort_;
132 0 : HCCL_INFO("[CpuUrmaEndpoint::%s] already listening, return existing port[%u]", __func__, dynamicPort_);
133 0 : return HCCL_SUCCESS;
134 : }
135 :
136 1 : uint32_t requestPort = 0;
137 1 : CHK_RET(ServerSocketManager::GetInstance().ServerSocketStartListen(portData, Hccl::NicType::HOST_NIC_TYPE, devicePhyId, &requestPort));
138 1 : if (requestPort == 0 || requestPort == HCCL_INVALID_PORT) {
139 0 : HCCL_ERROR("[CpuUrmaEndpoint::%s] get listen port failed, port is invalid.", __func__);
140 0 : return HCCL_E_NETWORK;
141 : }
142 1 : dynamicPort_ = requestPort;
143 1 : *port = dynamicPort_;
144 :
145 1 : return HCCL_SUCCESS;
146 1 : }
147 :
148 1 : HcclResult CpuUrmaEndpoint::RegisterMemory(HcommMem mem, const char *memTag, void **memHandle)
149 : {
150 1 : CHK_RET(this->regedMemMgr_->RegisterMemory(mem, memTag, memHandle));
151 1 : return HCCL_SUCCESS;
152 : }
153 :
154 1 : HcclResult CpuUrmaEndpoint::UnregisterMemory(void* memHandle)
155 : {
156 1 : CHK_RET(this->regedMemMgr_->UnregisterMemory(memHandle));
157 1 : return HCCL_SUCCESS;
158 : }
159 :
160 0 : HcclResult CpuUrmaEndpoint::MemoryExport(void *memHandle, void **memDesc, uint32_t *memDescLen)
161 : {
162 0 : CHK_RET(this->regedMemMgr_->MemoryExport(this->endpointDesc_, memHandle, memDesc, memDescLen));
163 0 : return HCCL_SUCCESS;
164 : }
165 :
166 0 : HcclResult CpuUrmaEndpoint::MemoryImport(const void *memDesc, uint32_t descLen, HcommMem *outMem)
167 : {
168 0 : CHK_RET(this->regedMemMgr_->MemoryImport(memDesc, descLen, outMem));
169 0 : return HCCL_SUCCESS;
170 : }
171 :
172 0 : HcclResult CpuUrmaEndpoint::MemoryUnimport(const void *memDesc, uint32_t descLen)
173 : {
174 0 : CHK_RET(this->regedMemMgr_->MemoryUnimport(memDesc, descLen));
175 0 : return HCCL_SUCCESS;
176 : }
177 :
178 0 : HcclResult CpuUrmaEndpoint::GetAllMemHandles(void **memHandles, uint32_t *memHandleNum)
179 : {
180 0 : CHK_RET(this->regedMemMgr_->GetAllMemHandles(memHandles, memHandleNum));
181 0 : return HCCL_SUCCESS;
182 : }
183 :
184 : }
|