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