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