Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 AICPU_TS_UBOE_UB_RTP_CHANNEL_HELPER_H
12 : #define AICPU_TS_UBOE_UB_RTP_CHANNEL_HELPER_H
13 :
14 : #include <cstdint>
15 : #include <memory>
16 : #include <mutex>
17 : #include <string>
18 : #include <vector>
19 :
20 : #include "channel.h"
21 : #include "aicpu_ts_channel_helper.h"
22 : #include "socket_mgr.h"
23 :
24 : // Orion
25 : #include "../../../../../../src/legacy/ascend950/unified_platform/resource/socket/socket.h"
26 : #include "rma_connection.h"
27 : #include "ub_mem_transport.h"
28 : #include "dev_ub_connection.h"
29 : #include "ub_local_notify.h"
30 : #include "hcomm_adapter_hccp.h"
31 :
32 : namespace hcomm {
33 :
34 : constexpr u32 FINISH_MSG_SIZE = 128;
35 : constexpr char_t FINISH_MSG[FINISH_MSG_SIZE] = "Uboe Comm Pipe ready!";
36 :
37 : class AicpuTsUboeUbRtpChannelHelper : public Channel {
38 : public:
39 : AicpuTsUboeUbRtpChannelHelper(EndpointHandle endpointHandle, const HcommChannelDesc& channelDesc);
40 : virtual ~AicpuTsUboeUbRtpChannelHelper();
41 :
42 : HcclResult GetNotifyNum(uint32_t* notifyNum) const override;
43 : HcclResult GetRemoteMems(uint32_t* memNum, CommMem** remoteMem, char*** memInfos) override;
44 :
45 : HcclResult H2DResPack(std::vector<char>& buffer);
46 0 : const HcommChannelDesc& GetChannelDesc() const override { return channelDesc_; }
47 : virtual HcclResult Clean() override;
48 : virtual HcclResult Resume() override;
49 :
50 : // 数据面接口
51 : HcclResult NotifyRecord(const uint32_t remoteNotifyIdx) override;
52 : HcclResult NotifyWait(const uint32_t localNotifyIdx, const uint32_t timeout) override;
53 : HcclResult WriteWithNotify(void* dst, const void* src, const uint64_t len, uint32_t remoteNotifyIdx) override;
54 : HcclResult Write(void* dst, const void* src, uint64_t len) override;
55 : HcclResult Read(void* dst, const void* src, uint64_t len) override;
56 : HcclResult ChannelFence() override;
57 :
58 0 : AicpuTsChannelHelper* GetAicpuTsHelper() override { return &aicpuTsHelper_; }
59 :
60 : protected:
61 : static constexpr u64 NORMAL_NOTIFY_VAL = 1;
62 :
63 62 : MAKE_ENUM(UboeRmtBufType, NOTIFY, BUFFER)
64 : using RemoteBufferVec = std::vector<std::unique_ptr<Hccl::RemoteUbRmaBuffer>>;
65 : using LocalBufferVec = std::vector<Hccl::LocalUbRmaBuffer*>;
66 :
67 : virtual HcclResult BuildConnection() = 0;
68 : virtual void SendFinish() = 0;
69 : virtual void RecvFinish() = 0;
70 :
71 : HcclResult ParseInputParam();
72 : HcclResult BuildNotify();
73 : HcclResult BuildDrainResource();
74 : HcclResult BuildSocket();
75 : void BuildConn();
76 :
77 : bool IsSocketReady();
78 : bool IsResReady();
79 : bool IsConnsReady();
80 : bool RecvDataProcess();
81 : void SendDataSize();
82 : void RecvDataSize();
83 : void SendExchangeData();
84 : void RecvExchangeData();
85 :
86 : void NotifyVecPack(Hccl::BinaryStream& binaryStream);
87 : void BufferVecPack(Hccl::BinaryStream& binaryStream, std::vector<Hccl::LocalRmaBuffer*>& bufferVec);
88 : void DrainBufferPack(Hccl::BinaryStream& binaryStream);
89 : void ConnVecPack(Hccl::BinaryStream& binaryStream);
90 : void RmtBufferVecUnpackProc(
91 : u32 locNum, Hccl::BinaryStream& binaryStream, RemoteBufferVec& bufferVec, UboeRmtBufType type);
92 : void RmtDrainBufferUnpackProc(Hccl::BinaryStream& binaryStream);
93 : bool ConnVecUnpackProc(Hccl::BinaryStream& binaryStream);
94 :
95 : std::vector<char> GetUniqueIdV2();
96 : std::vector<char> GetNotifyUniqueIds();
97 : std::vector<char> GetRmtBufferUniqueIds(RemoteBufferVec& bufferVec, UboeRmtBufType type) const;
98 : std::vector<char> GetLocBufferUniqueIds(LocalBufferVec& bufferVec, UboeRmtBufType type) const;
99 : std::vector<char> GetSingleRmtBufferUniqueId(u64 addr, u64 size, u32 tokenId, u32 tokenValue, u32 notifyId) const;
100 : std::vector<char> GetSingleLocBufferUniqueId(u64 addr, u64 size, u32 tokenId, u32 tokenValue) const;
101 : std::vector<char> GetDrainUniqueIds() const;
102 : std::vector<char> GetConnUniqueIds();
103 :
104 : // --------------------- 入参 ---------------------
105 : EndpointHandle endpointHandle_;
106 : HcommChannelDesc channelDesc_;
107 :
108 : // --------------------- 转换参数 ---------------------
109 : EndpointDesc localEp_{};
110 : EndpointDesc remoteEp_{};
111 : uint32_t notifyNum_{0};
112 :
113 : // --------------------- 具体成员 ---------------------
114 : Hccl::Socket* socket_{nullptr};
115 : RdmaHandle rdmaHandle_{nullptr};
116 : std::unique_ptr<Hccl::UbMemTransport> memTransport_{nullptr};
117 : Hccl::BaseMemTransport::CommonLocRes commonRes_{};
118 : std::vector<std::unique_ptr<Hccl::DevUbConnection>> connections_{};
119 : std::vector<std::unique_ptr<Hccl::UbLocalNotify>> localNotifies_{};
120 : std::unique_ptr<Hccl::Socket> serverSocket_;
121 :
122 : std::unique_ptr<Hccl::UbLocalNotify> drainNotify_; // 本端drain notify
123 : std::unique_ptr<Hccl::LocalUbRmaBuffer> drainBuffer_; // 本端常量1 buffer
124 : std::unique_ptr<Hccl::RemoteUbRmaBuffer> rmtDrainBuffer_; // 对端常量1 buffer
125 :
126 : ChannelStatus channelStatus{ChannelStatus::INIT};
127 :
128 : u32 bufferNum_{0};
129 : u32 connNum_{0};
130 : u32 recvDataSize_{0};
131 :
132 : RemoteBufferVec rmtNotifyVec_;
133 : RemoteBufferVec rmtBufferVec_;
134 : LocalBufferVec locBufferVec_;
135 : std::vector<char> recvData_{};
136 : std::vector<char> recvFinishMsg_{};
137 : std::vector<char> sendData_{};
138 : std::vector<char> sendFinishMsg_{};
139 : bool isRecvFirst_{false};
140 :
141 : Hccl::IpAddress locAddr_;
142 : Hccl::IpAddress rmtAddr_;
143 :
144 : std::mutex remoteMemsMutex_;
145 : bool cacheValid_ = false;
146 : std::vector<CommMem> remoteUserMems_;
147 : std::vector<std::string> memInfoCopies_;
148 : std::vector<char*> memInfoPointers_;
149 : const Hccl::SocketConfig* socketConfig_{nullptr};
150 : DevBaseAttr devBaseAttr_{};
151 : uint32_t devicePhyId_{};
152 :
153 : private:
154 : AicpuTsChannelHelper aicpuTsHelper_;
155 : };
156 :
157 : } // namespace hcomm
158 :
159 : #endif // AICPU_TS_UBOE_UB_RTP_CHANNEL_HELPER_H
|