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 :
12 : #include "server_socket_mgr.h"
13 : #include "hccl_common.h"
14 : #include "exception_handler.h"
15 : #include "orion_adpt_utils.h"
16 : #include "socket_handle_manager.h"
17 :
18 : namespace hcomm {
19 :
20 0 : HcclResult ServerSocketMgr::ListenStart(const uint32_t devPhyId, const CommAddr &commAddr, const Hccl::NicType nicType)
21 : {
22 0 : if (nicType != Hccl::NicType::DEVICE_NIC_TYPE &&
23 0 : nicType != Hccl::NicType::HOST_NIC_TYPE) {
24 0 : HCCL_ERROR("[%s] nicType[%d] is not supported", __func__, nicType); // 枚举用转换吗?
25 0 : return HCCL_E_PARA;
26 : }
27 :
28 0 : auto &socketMgr = ServerSocketMgr::GetInstance(devPhyId);
29 :
30 0 : CHK_RET(socketMgr.ListenStart_(commAddr, nicType));
31 :
32 0 : return HcclResult::HCCL_SUCCESS;
33 : }
34 :
35 3 : ServerSocketMgr &ServerSocketMgr::GetInstance(const uint32_t devicePhyId)
36 : {
37 69 : static ServerSocketMgr socketMgr[MAX_MODULE_DEVICE_NUM + 1];
38 :
39 3 : uint32_t devPhyId = devicePhyId;
40 3 : if (devPhyId >= MAX_MODULE_DEVICE_NUM + 1) {
41 0 : HCCL_WARNING("[%s] Invalid devicePhyId: %u, max allowed: %u, using default index: %u",
42 : __func__, devicePhyId, MAX_MODULE_DEVICE_NUM, MAX_MODULE_DEVICE_NUM);
43 0 : devPhyId = MAX_MODULE_DEVICE_NUM;
44 : }
45 :
46 3 : socketMgr[devPhyId].devPhyId_ = devPhyId;
47 3 : return socketMgr[devPhyId];
48 : }
49 :
50 0 : HcclResult ServerSocketMgr::ListenStart_(const CommAddr &commAddr, const Hccl::NicType nicType)
51 : {
52 0 : std::lock_guard<std::mutex> lock(innerMutex_);
53 0 : Hccl::IpAddress ipAddr{};
54 0 : CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
55 :
56 0 : if (nicType == Hccl::NicType::DEVICE_NIC_TYPE) {
57 0 : auto ipIter = deviceServerSocketMap_.find(ipAddr);
58 0 : if (ipIter != deviceServerSocketMap_.end()) {
59 0 : HCCL_INFO("[ServerSocketMgr][%s] device server socket already created.", __func__);
60 0 : return HcclResult::HCCL_SUCCESS;
61 : }
62 : } else {
63 0 : auto ipIter = hostServerSocketMap_.find(ipAddr);
64 0 : if (ipIter != hostServerSocketMap_.end()) {
65 0 : HCCL_INFO("[ServerSocketMgr][%s] host server socket already created.", __func__);
66 0 : return HcclResult::HCCL_SUCCESS;
67 : }
68 : }
69 :
70 : EXCEPTION_HANDLE_BEGIN
71 0 : const Hccl::DevNetPortType portType = Hccl::DevNetPortType(Hccl::ConnectProtoType::UB); // 不能写死
72 : // todo: 暂时使用devPhyId构造rankId,id存疑?
73 0 : Hccl::PortData localPort = Hccl::PortData(static_cast<Hccl::RankId>(devPhyId_), portType, 0, ipAddr);
74 :
75 0 : HCCL_INFO("[ServerSocketMgr][%s] get socket handle, devPhyId[%u] locAddr[%s].",
76 : __func__, devPhyId_, ipAddr.Describe().c_str());
77 :
78 0 : Hccl::SocketHandle socketHandle = Hccl::SocketHandleManager::GetInstance().Create(devPhyId_, localPort);
79 :
80 0 : std::unique_ptr<Hccl::Socket> serverSocket = nullptr;
81 0 : constexpr uint32_t listenPort = 60001; // 端口号如何处理,可能冲突
82 0 : const std::string tag = "server";
83 0 : constexpr Hccl::SocketRole role = Hccl::SocketRole::SERVER;
84 :
85 0 : HCCL_INFO("[ServerSocketMgr][%s] create server socket, "
86 : "locAddr[%s] rmtAddr[%s] tag[%s] role[%s].",
87 : __func__, ipAddr.Describe().c_str(), ipAddr.Describe().c_str(),
88 : tag.c_str(), role.Describe().c_str());
89 :
90 0 : serverSocket.reset(new (std::nothrow) Hccl::Socket(
91 0 : socketHandle, ipAddr, listenPort, ipAddr, tag, role, nicType));
92 0 : CHK_PTR_NULL(serverSocket);
93 0 : serverSocket->Listen();
94 :
95 0 : if (nicType == Hccl::NicType::DEVICE_NIC_TYPE) {
96 0 : deviceServerSocketMap_[ipAddr] = std::move(serverSocket); // IP校验?
97 : } else {
98 0 : hostServerSocketMap_[ipAddr] = std::move(serverSocket); // IP校验?
99 : }
100 0 : EXCEPTION_HANDLE_END
101 0 : return HcclResult::HCCL_SUCCESS;
102 0 : }
103 :
104 3 : void ServerSocketMgr::DeInit(u32 devPhyId)
105 : {
106 3 : HCCL_INFO("[ServerSocketMgr][%s] DeInit[%u]", __func__, devPhyId);
107 3 : auto &inst = GetInstance(devPhyId);
108 3 : std::lock_guard<std::mutex> lock(inst.innerMutex_);
109 3 : for (auto &it : inst.deviceServerSocketMap_) {
110 0 : if (it.second != nullptr) {
111 0 : it.second->Destroy();
112 0 : it.second.reset();
113 : }
114 : }
115 3 : inst.deviceServerSocketMap_.clear();
116 :
117 3 : for (auto &it : inst.hostServerSocketMap_) {
118 0 : if (it.second != nullptr) {
119 0 : it.second->Destroy();
120 0 : it.second.reset();
121 : }
122 : }
123 3 : inst.hostServerSocketMap_.clear();
124 3 : }
125 :
126 : }; // namespace hcomm
|