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 HCCL_PREEMPT_SOCKET_MANAGER_H
12 : #define HCCL_PREEMPT_SOCKET_MANAGER_H
13 :
14 : #include <cstddef>
15 : #include <memory>
16 : #include <mutex>
17 : #include <string>
18 : #include <unordered_map>
19 : #include <utility>
20 : #include <vector>
21 : #include "hccl/base.h"
22 : #include "orion_adapter_hccp.h"
23 : #include "env_func.h"
24 : #include "socket.h"
25 : #include "referenced.h"
26 :
27 : namespace Hccl {
28 :
29 : struct EnumClassHash {
30 : template <typename T>
31 135 : std::size_t operator()(T t) const
32 : {
33 135 : return static_cast<std::size_t>(t);
34 : }
35 : };
36 :
37 : // 以IP和port的粒度管理已经抢占的port的计数器
38 : using IpPortRef = std::unordered_map<std::string, std::pair<u32, Referenced>>;
39 :
40 : class PreemptPortManager {
41 : public:
42 : ~PreemptPortManager();
43 :
44 : static PreemptPortManager& GetInstance(s32 deviceLogicId);
45 :
46 : // 尝试在给定范围内抢占一个端口
47 : void ListenPreempt(
48 : const std::shared_ptr<Socket>& listenSocket, const std::vector<SocketPortRange>& portRange, u32& usePort);
49 : // 释放一个已抢占的端口
50 : void Release(const std::shared_ptr<Socket>& listenSocket);
51 :
52 : private:
53 : explicit PreemptPortManager();
54 :
55 : void PreemptPortInRange(
56 : const std::shared_ptr<Socket>& listenSocket, HrtNetworkMode netMode,
57 : const std::vector<SocketPortRange>& portRange, u32& usePort);
58 : void ReleasePreempt(IpPortRef& portRef, const std::shared_ptr<Socket>& listenSocket, HrtNetworkMode netMode);
59 :
60 : bool IsAlreadyListening(const IpPortRef& ipPortRef, const std::string& ipAddr, const u32 port);
61 : std::string GetRangeStr(const std::vector<SocketPortRange>& portRangeVec);
62 :
63 : static bool initialized;
64 : s32 deviceLogicId_;
65 : u32 devicePhyId_;
66 : std::mutex preemptMutex_;
67 :
68 : // 不同类型网卡上抢占的Socket
69 : std::unordered_map<HrtNetworkMode, IpPortRef, EnumClassHash> preemptSockets_;
70 : };
71 : } // namespace Hccl
72 : #endif // HCCL_PREEMPT_SOCKET_MANAGER_H
|