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