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 : #ifndef HCCLV2_SOCKET_MANAGER_H
11 : #define HCCLV2_SOCKET_MANAGER_H
12 :
13 : #include <string>
14 : #include <vector>
15 : #include <set>
16 : #include <unordered_map>
17 : #include <memory>
18 :
19 : #include "../../unified_platform/resource/socket/socket.h"
20 : #include "virtual_topo.h"
21 : #include "socket_config.h"
22 : #include "env_func.h"
23 : #include "orion_adapter_hccp.h"
24 :
25 : namespace Hccl {
26 :
27 : using RankIpPortMap = std::unordered_map<u32, std::unordered_map<IpAddress, u32>>;
28 : using RankIpPortMapPtr = std::shared_ptr<RankIpPortMap>;
29 :
30 : class CommunicatorImpl;
31 : class SocketManager {
32 : public:
33 1 : SocketManager() = default;
34 : SocketManager(
35 : const CommunicatorImpl& communicator, u32 localRank, u32 devicePhyId, u32 deviceLogicId,
36 : std::function<shared_ptr<Socket>(
37 : IpAddress& localIpAddress, IpAddress& remoteIpAddress, u32 listenPort, SocketHandle socketHandle,
38 : const std::string& tag, SocketRole socketRole, NicType nicType)>
39 : socketProducer
40 : = nullptr);
41 :
42 : SocketManager(u32 localRank, u32 devicePhyId, u32 deviceLogicId, const std::string& socketTag);
43 :
44 : void
45 : SetDeviceServerListenPortMap(const std::unordered_map<u32, std::unordered_map<IpAddress, u32>>& rankListenPortMap);
46 :
47 : std::unordered_map<u32, std::unordered_map<IpAddress, u32>>
48 : GetSubCommDeviceServerListenPortMap(const std::vector<u32>& rankIds) const;
49 :
50 : u32 GetDeviceListenPort(const u32& rankId, const IpAddress& ipAddress);
51 :
52 : void BatchCreateSockets(const vector<LinkData>& links);
53 : void ServerListen(const SocketConfig& socketConfig);
54 : void ConnectSockets(const SocketConfig& socketConfig);
55 :
56 : void BatchCreateSockets(const SocketConfig& socketConfig);
57 :
58 : void ServerInit(PortData& localPort);
59 :
60 : static void ServerInitAll(NewRankInfo& rankInfo);
61 :
62 : bool ServerDeInit(PortData& localPort) const;
63 :
64 : Socket* CreateConnectedSocket(const SocketConfig& socketConfig);
65 :
66 : Socket* GetConnectedSocket(const SocketConfig& socketConfig) const;
67 :
68 : bool CheckServerPortListening(const PortData& portData, const uint32_t port) const;
69 :
70 : void DestroyAll();
71 :
72 : void AddWhiteList(PortData& localPort, vector<RaSocketWhitelist>& wlistInfoVec) const;
73 :
74 : bool DelWhiteList(PortData& localPort, vector<RaSocketWhitelist>& wlistInfoVec) const;
75 :
76 : ~SocketManager();
77 :
78 : SocketManager(const SocketManager& socketManager) = delete;
79 :
80 : SocketManager& operator=(const SocketManager& socketManager) = delete;
81 :
82 : private:
83 : void PrepareLinkAndServerInit(const SocketConfig& socketConfig);
84 : void BatchServerInit(const vector<LinkData>& links);
85 : void BatchAddWhiteList(const vector<LinkData>& links);
86 : void BatchCreateConnectedSockets(const vector<LinkData>& links);
87 : void AddWhiteList(const SocketConfig& socketConfig);
88 : const CommunicatorImpl* comm;
89 : static std::unordered_map<PortData, shared_ptr<Socket>>& GetServerSocketMap();
90 : u32 localRank;
91 : u32 devicePhyId;
92 : u32 deviceLogicId_;
93 : std::unordered_map<u32, std::unordered_map<IpAddress, u32>> rankListenPortMap_{};
94 : std::function<shared_ptr<Socket>(
95 : IpAddress& localIpAddress, IpAddress& remoteIpAddress, u32 listenPort, SocketHandle socketHandle,
96 : const std::string& tag, SocketRole socketRole, NicType nicType)>
97 : socketProducer
98 12 : = [](IpAddress& localIpAddress, IpAddress& remoteIpAddress, u32 listenPort, SocketHandle socketHandle,
99 : const std::string& tag, SocketRole socketRole, NicType nicType) -> shared_ptr<Socket> {
100 : auto tmpSocket = std::make_shared<Socket>(
101 12 : socketHandle, localIpAddress, listenPort, remoteIpAddress, tag, socketRole, nicType);
102 36 : HCCL_INFO("create socket with role %u", static_cast<u32>(socketRole));
103 12 : return tmpSocket;
104 0 : };
105 :
106 : std::unordered_map<SocketConfig, shared_ptr<Socket>> connectedSocketMap;
107 : std::unordered_map<PortData, vector<RaSocketWhitelist>> socketWlistMap{};
108 :
109 : Socket* GetServerListenSocket(const PortData& localPort) const;
110 : std::set<LinkData> availableLinks;
111 :
112 : std::string socketTag_{};
113 : static std::mutex socketLock;
114 : };
115 :
116 : } // namespace Hccl
117 :
118 : #endif // HCCLV2_SOCKET_MANAGER_H
|