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 PREEMPT_SOCKET_MANAGER_H
12 : #define 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/hccl_types.h>
22 : #include "hccl/base.h"
23 : #include "hccl_common.h"
24 : #include "hccl_socket.h"
25 : #include "env_config.h"
26 :
27 : namespace hccl {
28 :
29 : struct EnumClassHash
30 : {
31 : template<typename T>
32 412 : std::size_t operator()(T t) const
33 : {
34 412 : 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 : HcclResult ListenPreempt(const std::shared_ptr<HcclSocket> &listenSocket,
49 : const std::vector<HcclSocketPortRange> &portRange, u32 &usePort);
50 : // 释放一个已抢占的端口
51 : HcclResult Release(const std::shared_ptr<HcclSocket> &listenSocket);
52 :
53 : private:
54 : explicit PreemptPortManager();
55 :
56 : HcclResult PreemptPortInRange(IpPortRef& portRef, const std::shared_ptr<HcclSocket> &listenSocket,
57 : NICDeployment nicDeploy, const std::vector<HcclSocketPortRange> &portRange, u32 &usePort);
58 : HcclResult ReleasePreempt(IpPortRef& portRef, const std::shared_ptr<HcclSocket> &listenSocket,
59 : NICDeployment nicDeploy);
60 :
61 : bool IsAlreadyListening(const IpPortRef& ipPortRef, const std::string &ipAddr, const u32 port);
62 : std::string GetRangeStr(const std::vector<HcclSocketPortRange> &portRangeVec);
63 :
64 : static bool initialized;
65 : s32 deviceLogicId_;
66 : u32 devicePhyId_;
67 :
68 : std::mutex preemptMutex_;
69 :
70 : // 不同类型网卡上抢占的Socket
71 : std::unordered_map<NICDeployment, IpPortRef, EnumClassHash> preemptSockets_;
72 : };
73 : }
74 : #endif // PREEMPT_SOCKET_MANAGER_H
|