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 : #ifndef SERVER_SOCKET_MANAGER_H
12 : #define SERVER_SOCKET_MANAGER_H
13 : #include <mutex>
14 : #include <unordered_map>
15 : #include "hccl/hccl_res.h"
16 : // Orion
17 : #include "socket/socket.h"
18 : #include "virtual_topo.h"
19 : #include "socket_config.h"
20 : #include "socket_manager.h"
21 : #include "orion_adapter_rts.h"
22 : #include "socket_handle_manager.h"
23 : #include "host_socket_handle_manager.h"
24 :
25 : namespace hcomm {
26 :
27 : class ServerSocketManager {
28 : public:
29 89 : static ServerSocketManager& GetInstance()
30 : {
31 89 : static ServerSocketManager instance;
32 89 : return instance;
33 : }
34 : ServerSocketManager(const ServerSocketManager&) = delete;
35 : ServerSocketManager& operator=(const ServerSocketManager&) = delete;
36 : ServerSocketManager(ServerSocketManager&&) = delete;
37 : ServerSocketManager& operator=(ServerSocketManager&&) = delete;
38 :
39 1 : ~ServerSocketManager() {};
40 :
41 : HcclResult ServerSocketStartListen(
42 : const Hccl::PortData& localPort, const Hccl::NicType nicType, const uint32_t devPhyId, uint32_t* port);
43 : HcclResult
44 : ServerSocketStopListen(const Hccl::PortData& localPort, const Hccl::NicType nicType, const uint32_t port);
45 : void DeInit(u32 devPhyId);
46 :
47 : private:
48 1 : ServerSocketManager()
49 1 : {
50 : // Ensure that (Host)SocketHandleManager destruct after the destructionon of device/host ServerSocketMap_.
51 1 : (void)Hccl::SocketHandleManager::GetInstance();
52 1 : (void)Hccl::HostSocketHandleManager::GetInstance();
53 1 : };
54 : HcclResult DeviceSocketListen(const Hccl::PortData& localPort, const uint32_t devPhyId, uint32_t* port);
55 : HcclResult HostSocketListen(const Hccl::PortData& localPort, const uint32_t devPhyId, uint32_t* port);
56 : HcclResult DeviceSocketStopListen(const Hccl::PortData& localPort, const uint32_t port);
57 : HcclResult HostSocketStopListen(const Hccl::PortData& localPort, const uint32_t port);
58 : void DeInitDeviceSockets(u32 devPhyId);
59 : void DeInitHostSockets(u32 devPhyId);
60 :
61 : // PortData : {ListenPort : {Socket, Count}}
62 : std::unordered_map<Hccl::PortData, std::unordered_map<uint32_t, std::pair<std::unique_ptr<Hccl::Socket>, uint32_t>>>
63 : deviceServerSocketMap_{};
64 : std::unordered_map<Hccl::PortData, std::unordered_map<uint32_t, std::pair<std::unique_ptr<Hccl::Socket>, uint32_t>>>
65 : hostServerSocketMap_{};
66 : std::unique_ptr<Hccl::SocketManager> socketMgrCompat_;
67 : std::mutex hostMutex_{};
68 : std::mutex deviceMutex_{};
69 : };
70 :
71 : } // namespace hcomm
72 :
73 : #endif // SERVER_SOCKET_MGR_H
|