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