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 PING_MESH_PUB_H
12 : #define PING_MESH_PUB_H
13 :
14 : #include <map>
15 : #include <thread>
16 : #include <atomic>
17 : #include "hccl_ip_address.h"
18 : #include "hccl_socket.h"
19 : #include "hdc_pub.h"
20 : #include "network/hccp_common.h"
21 : #include "network/hccp_ping.h"
22 : #include "hccn_rping.h"
23 : #include "orion_adapter_hccp.h"
24 : #include "dispatcher_task_types.h"
25 : #include "universal_concurrent_map.h"
26 :
27 : namespace hccl
28 : {
29 : constexpr u32 ONE_MILLISEC = 1000;
30 : constexpr u32 RPING_INTERFACE_OPCODE= 71;
31 : constexpr u32 RPING_INTERFACE_VERSION = 1;
32 : constexpr u32 RPING_PAYLOAD_LEN_MAX = 1500;
33 : constexpr u64 TSD_EXT_PARA_NUM = 2;
34 : constexpr u32 RPING_SERVICE_LEVEL_DEFAULT = 4;
35 : constexpr u32 RPING_TRAFFIC_CLASS_DEFAULT = 132;
36 :
37 : //判断类型相关函数
38 : bool IsSupportHCCLV2(const char *socNamePtr);
39 : HcclResult GetAddrType(u32 *addrtype);
40 : // HCCN接口需要的结构体
41 : struct RpingInput {
42 : HcclIpAddress sip;
43 : HcclIpAddress dip;
44 : int srcPort; // UDP源端口号,用户配置,参与hash选路
45 : int reserved; // 保留字段,用于对齐,暂不使用
46 : int sl; // 指定队列
47 : int tc; // 主要是修改DSCP
48 : int port; // 监听端口
49 : u32 len;
50 : u32 addrType; /* address type, 0: ip, 1: eid */ //todo: 是否要添加需要确定
51 : char payload[RPING_PAYLOAD_LEN_MAX];
52 : };
53 :
54 92 : MAKE_ENUM(HrtNetworkMode, PEER, HDC)
55 :
56 : struct HRaInfo {
57 : HrtNetworkMode mode;
58 : uint32_t phyId;
59 2 : HRaInfo(HrtNetworkMode mode, uint32_t phyId) : mode(mode), phyId(phyId)
60 : {
61 2 : }
62 : };
63 :
64 : struct RpingOutput {
65 : u32 txPkt; // rping发包总数
66 : u32 rxPkt; // rping收包总数
67 : u32 minRTT;
68 : u32 maxRTT;
69 : u32 avgRTT;
70 : u32 state;
71 : u32 reserved[2];
72 : };
73 :
74 : // 需要重填的payload头只有前136字节,后面是固定的
75 : struct RpingPayloadHead {
76 : union {
77 : char srcIp[64]; /* local(client) ip */
78 : char srcEid[16]; /* local(client) eid */
79 : };
80 : union {
81 : char dstIp[64]; /* remote(target) ip */
82 : char dstEid[16]; /* remote(target) eid */
83 : };
84 : u32 payloadLen;
85 : u32 resvd[3];
86 : u64 timestamp[8];
87 : u32 rpingBatchId;
88 : u32 addrType; /* address type, 0: ip, 1: eid */
89 : u8 reserved[40];
90 : };
91 :
92 : union RpingIpHead {
93 : struct {
94 : u32 versionTclassFlow; // 4bit version, 8bit tclass, 20bit flow label
95 : u16 payLen; // ub报文的payload长度
96 : u8 nextHdr; // 下一个头部的类型
97 : u8 hopLimit; // 最大跳数
98 : u8 srcIp[16]; // sip的gid,128bit
99 : u8 dstIp[16]; // dip的gid,128bit
100 : } ipv6;
101 :
102 : struct {
103 : u8 rsvd[20]; // 前20字节为空
104 : u32 verIhlTosTlen; // 4bit version, 4bit IHL, 8bit type of service, 16bit total length
105 : u32 IdFlagsFragOffset; // 16bit identification, 3bit flags, 13bit fragment offset
106 : u8 tol; // time to live
107 : u8 protocol; // 协议类型
108 : u16 headChecksum;
109 : u32 srcIp;
110 : u32 dstIp;
111 : } ipv4;
112 : };
113 :
114 : struct RpingEidHead {
115 : u32 version; // 32bit version
116 : u32 type; // 32bit type
117 : u32 ser_version; // 32bit serversion
118 : u32 padding1; // 32bitpadding
119 : u8 info_size1; // 8bit的info_size
120 : u8 srcEid[16]; // sip的Eid,128bit
121 : u32 uasid1; // 32bit的uasid
122 : u32 jetty_id1; // 32bit的jetty_id值
123 : u32 padding2; // 32bit的padding
124 : u8 resvd[7]; // 56bit的reserved
125 : u32 s_token_value; // 32bit的s_token_value
126 : u32 dst_version; // 32bit的dst_version
127 : u32 padding3; // 32bit的padding
128 : u8 info_size2; // 4bit的info_size
129 : u8 dstEid[16]; // dip的Eid,128bit
130 : u32 uasid2; // 32bit的uasid2
131 : u32 jetty_id2; // 32bit的jetty_id2
132 : u8 reserv[7];
133 : u32 client_jetty_token_value; //32bit的client_jetty_token_value
134 : u64 times[8];
135 : u32 taskId;
136 : u8 reserved[44];
137 :
138 : };
139 :
140 : enum class RpingState {
141 : UNINIT,
142 : INITED,
143 : READY,
144 : RUN,
145 : STOP,
146 : RESERVED
147 : };
148 :
149 : enum class RpingLinkState {
150 : CONNECTED,
151 : CONNECTING,
152 : DISCONNECTED,
153 : TIMEOUT,
154 : ERROR
155 : };
156 :
157 : enum WhiteListStatus {
158 : WHITE_LIST_CLOSE = 0,
159 : WHITE_LIST_OPEN = 1
160 : };
161 :
162 : class PingMesh {
163 : private:
164 : PingInitInfo initInfo_ {}; // 初始化信息
165 : void *pingHandle_ = nullptr; // 记录hccp侧的pingmesh句柄
166 : std::shared_ptr<HcclSocket> socket_ = nullptr; // 记录server端的socket信息,用于建立rdma链路
167 : HcclIpAddress ipAddr_; // 记录device的ip信息
168 : u8 *payload_ = nullptr; // client侧记录的payload信息
169 : RpingState rpingState_ = RpingState::UNINIT; // 记录client状态
170 : int rpingTargetNum_ = 0; // 记录client目标数量
171 : std::mutex socketMapsMtx_;
172 : std::map<std::string, std::shared_ptr<HcclSocket>> socketMaps_; // 记录client端的socket信息
173 : UniversalConcurrentMap<std::string, PingQpInfo> rdmaInfoMaps_; // 记录target的rdma或者ub信息
174 : std::unique_ptr<std::thread> connThread_; // server端等待socket建链的背景线程
175 : HcclNetDevCtx netCtx_ = nullptr; // 记录网络上下文信息
176 : std::shared_ptr<HDCommunicate> hdcD2H_ = nullptr; // 从device侧获取数据的接口
177 : s32 deviceLogicId_ = 0;
178 : u32 devicePhyId_ = 0;
179 : bool isDeinited_ = false;
180 : bool isSocketClosed_ = false;
181 : std::atomic<bool> connThreadStop_{false}; // 侦听的子线程的结束条件
182 : bool isUsePayload_ = false;
183 : UniversalConcurrentMap<std::string, u32> payloadLenMap_; // 记录自定义payload的长度
184 : HccnRpingMode mode = HCCN_RPING_MODE_ROCE; //ROCEorUB,
185 :
186 : HcclResult RpingSendInitInfo(u32 deviceId, u32 port, HcclIpAddress ipAddr, PingInitInfo initInfo,
187 : std::shared_ptr<HcclSocket> socket);
188 : HcclResult RpingRecvTargetInfo(void *clientNetCtx, u32 port, HcclIpAddress ipAddr, PingInitInfo &recvInfo, u32 timeout);
189 : HcclResult StartSocketThread(u32 deviceId, HcclIpAddress ipAddr, u32 port);
190 : HcclResult HccnCloseSubProc(u32 deviceId);
191 : HcclResult HccnRaInit(u32 deviceId);
192 : HcclResult HccnTargetAttrInter(u32 targetNumInter, RpingInput *inputInter, HccnRpingAddTargetConfig *configInter, PingTargetInfo *targetInter);
193 : HcclResult HccnTarRemoveAttrInter(u32 targetNumInter, RpingInput *inputInter, PingTargetCommInfo *targetInter, std::shared_ptr<HcclSocket> &socketInter);
194 : HcclResult RpingResultInfoInit(PingTargetResult *resultInfo,
195 : UniversalConcurrentMap<std::string, PingQpInfo> &rdmaInfoMaps, RpingInput *input, u32 targetNum);
196 : HcclResult HccnSupportedAndGetphyid(u32 deviceId, LinkType netMode);
197 : HcclResult HccnRpingOpenTsd(u32 deviceId, u32 mode, u32 port, u32 nodeNum, u32 bufferSize, u32 sl, u32 tc);
198 : public:
199 : PingMesh();
200 : ~PingMesh();
201 : HcclResult HccnRpingInit(u32 deviceId, u32 mode, HcclIpAddress ipAddr, u32 port, u32 nodeNum, u32 bufferSize,
202 : u32 sl = RPING_SERVICE_LEVEL_DEFAULT, u32 tc = RPING_TRAFFIC_CLASS_DEFAULT);
203 : HcclResult HccnRpingDeinit(u32 deviceId);
204 : HcclResult HccnRpingAddTarget(u32 deviceId, u32 targetNum, RpingInput *input, HccnRpingAddTargetConfig *config);
205 : HcclResult HccnRpingRemoveTarget(u32 deviceId, u32 targetNum, RpingInput *input);
206 : HcclResult HccnRpingGetTarget(u32 deviceId, u32 targetNum, RpingInput *input, int *targetStat);
207 : HcclResult HccnRpingBatchPingStart(u32 deviceId, u32 pktNum, u32 interval, u32 timeout);
208 : HcclResult HccnRpingBatchPingStop(u32 deviceId);
209 : HcclResult HccnRpingGetResult(u32 deviceId, u32 targetNum, RpingInput *input, RpingOutput *output);
210 : HcclResult HccnRpingRefillPayloadHead(u8 *originalHead, u32 payloadNum);
211 : HcclResult HccnRpingRefillUbPayloadHead(u8 *originalHead, u32 payloadNum);
212 : HcclResult HccnRpingGetPayload(u32 deviceId, void **payload, u32 *payloadLen, HccnRpingMode mode);
213 :
214 14 : inline s32 GetDeviceLogicId() {
215 14 : return deviceLogicId_;
216 : }
217 1 : inline HccnRpingMode GetMode() {
218 1 : return mode;
219 : }
220 6 : inline void init(HccnRpingInitAttr* attr) {
221 6 : this->mode = attr->mode;
222 6 : }
223 : };
224 : }
225 : #endif
|