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