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 AICPUTS_ROCE_ENDPOINT_H
12 : #define AICPUTS_ROCE_ENDPOINT_H
13 :
14 : #include <cstdint>
15 : #include <functional>
16 : #include <memory>
17 : #include <mutex>
18 : #include <string>
19 : #include <unordered_map>
20 : #include <vector>
21 : #include "hccl_mem_defs.h"
22 : #include "endpoint.h"
23 : #include "hccl_socket.h"
24 :
25 : namespace hcomm {
26 : struct AicpuTsListenSocketSlot {
27 : std::shared_ptr<hccl::HcclSocket> socket{};
28 : uint32_t refCount{0U};
29 : };
30 :
31 : struct SocketMapKey {
32 : uint32_t devicePhyId;
33 : uint32_t port;
34 :
35 7 : bool operator==(const SocketMapKey& other) const { return devicePhyId == other.devicePhyId && port == other.port; }
36 : };
37 :
38 : struct SocketMapKeyHash {
39 13 : size_t operator()(const SocketMapKey& k) const
40 : {
41 13 : return std::hash<uint32_t>()(k.devicePhyId) ^ (std::hash<uint32_t>()(k.port) << 1);
42 : }
43 : };
44 :
45 : struct AicpuTsNetDevSlot {
46 : HcclNetDev netDev{nullptr};
47 : uint32_t refCount{0U};
48 : };
49 :
50 : class AicpuTsRoceEndpoint : public Endpoint {
51 : public:
52 : explicit AicpuTsRoceEndpoint(const EndpointDesc& endpointDesc);
53 : ~AicpuTsRoceEndpoint() override;
54 :
55 : HcclResult Init() override;
56 :
57 : HcclResult ServerSocketListen(const uint32_t port) override;
58 :
59 : HcclResult RegisterMemory(HcommMem mem, const char* memTag, void** memHandle) override;
60 : HcclResult UnregisterMemory(void* memHandle) override;
61 : HcclResult MemoryExport(void* memHandle, void** memDesc, uint32_t* memDescLen) override;
62 : HcclResult MemoryImport(const void* memDesc, uint32_t descLen, HcommMem* outMem) override;
63 : HcclResult MemoryUnimport(const void* memDesc, uint32_t descLen) override;
64 : HcclResult GetAllMemHandles(void** memHandles, uint32_t* memHandleNum) override;
65 :
66 3 : HcclNetDev GetNetDev() const { return netDev_; }
67 :
68 : HcclResult GetSocket(uint32_t port, const std::string& tag, std::shared_ptr<hccl::HcclSocket>& outConnected);
69 : HcclResult AcceptDataSocket(
70 : uint32_t port, const std::string& tag, std::shared_ptr<hccl::HcclSocket>& outConnected,
71 : uint32_t acceptTimeoutMs = 0);
72 :
73 : HcclResult AddListenSocketWhiteList(uint32_t port, const std::vector<SocketWlistInfo>& wlistInfos);
74 :
75 : private:
76 : static std::unordered_map<SocketMapKey, AicpuTsListenSocketSlot, SocketMapKeyHash>& GetServerSocketMap();
77 : static std::mutex& ListenSocketMapMutex();
78 : bool ReuseListenSocketIfExist(const SocketMapKey& key, const char* logPrefix);
79 : void ReleaseListenSocketRefs();
80 :
81 : static std::unordered_map<uint32_t, AicpuTsNetDevSlot>& GetNetDevMap();
82 : static std::mutex& NetDevMapMutex();
83 : HcclResult AcquireSharedNetDev(uint32_t devicePhyId, const HcclNetDevInfos& info);
84 : void ReleaseSharedNetDev();
85 : void ReleaseNicSocketHandle(HcclNetDev netDev);
86 : HcclResult AcquireRdmaContext(uint32_t devPhyId, const EndpointDesc& endpointDesc);
87 :
88 : HcclNetDev netDev_{nullptr};
89 : uint32_t netDevRefPhyId_{UINT32_MAX};
90 : std::vector<SocketMapKey> listenRefKeys_{};
91 : bool hasListenSocketRef_{false};
92 : };
93 : } // namespace hcomm
94 : #endif // AICPUTS_ROCE_ENDPOINT_H
|