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 : {
31 : template<typename T>
32 135 : std::size_t operator()(T t) const
33 : {
34 135 : return static_cast<std::size_t>(t);
35 : }
36 : };
37 :
38 : // 以IP和port的粒度管理已经抢占的port的计数器
39 : using IpPortRef = std::unordered_map<std::string, std::pair<u32, Referenced>>;
40 :
41 : class PreemptPortManager {
42 : public:
43 : ~PreemptPortManager();
44 :
45 : static PreemptPortManager& GetInstance(s32 deviceLogicId);
46 :
47 : // 尝试在给定范围内抢占一个端口
48 : void ListenPreempt(const std::shared_ptr<Socket> &listenSocket,
49 : const std::vector<SocketPortRange> &portRange, u32 &usePort);
50 : // 释放一个已抢占的端口
51 : void Release(const std::shared_ptr<Socket> &listenSocket);
52 :
53 : private:
54 : explicit PreemptPortManager();
55 :
56 : void PreemptPortInRange(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,
59 : HrtNetworkMode netMode);
60 :
61 : bool IsAlreadyListening(const IpPortRef& ipPortRef, const std::string &ipAddr, const u32 port);
62 : std::string GetRangeStr(const std::vector<SocketPortRange> &portRangeVec);
63 :
64 : static bool initialized;
65 : s32 deviceLogicId_;
66 : u32 devicePhyId_;
67 : std::mutex preemptMutex_;
68 :
69 : // 不同类型网卡上抢占的Socket
70 : std::unordered_map<HrtNetworkMode, IpPortRef, EnumClassHash> preemptSockets_;
71 : };
72 : }
73 : #endif // HCCL_PREEMPT_SOCKET_MANAGER_H
|