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 : #include "securec.h"
12 : #include "user_log.h"
13 : #include "ra_rs_err.h"
14 : #include "ra_hdc_ping.h"
15 :
16 1 : int RaHdcPingInit(struct RaPingHandle *pingHandle, struct PingInitAttr *initAttr,
17 : struct PingInitInfo *initInfo)
18 : {
19 1 : unsigned int phyId = pingHandle->phyId;
20 1 : union OpPingInitData pingData = { 0 };
21 : int ret;
22 :
23 1 : ret = memcpy_s(&(pingData.txData.attr), sizeof(struct PingInitAttr), initAttr, sizeof(struct PingInitAttr));
24 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]memcpy_s init_attr failed, ret(%d) phyId(%u)",
25 : ret, phyId), -ESAFEFUNC);
26 :
27 1 : ret = RaHdcProcessMsg(RA_RS_PING_INIT, phyId, (char *)&pingData, sizeof(union OpPingInitData));
28 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)",
29 : ret, phyId), ret);
30 :
31 1 : ret = memcpy_s(initInfo, sizeof(struct PingInitInfo), &(pingData.rxData.info), sizeof(struct PingInitInfo));
32 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]memcpy_s init_info failed, ret(%d) phyId(%u)",
33 : ret, phyId), -ESAFEFUNC);
34 1 : ret = memcpy_s(&(pingHandle->dev), sizeof(union PingDev), &(initAttr->dev), sizeof(union PingDev));
35 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]memcpy_s dev info failed, ret(%d) phyId(%u)",
36 : ret, phyId), -ESAFEFUNC);
37 1 : pingHandle->devIndex = pingData.rxData.devIndex;
38 :
39 1 : return 0;
40 : }
41 :
42 6 : STATIC void RaHdcPingInitRdev(struct RaRsDevInfo *rdev, unsigned int phyId, unsigned int devIndex)
43 : {
44 6 : rdev->phyId = phyId;
45 6 : rdev->devIndex = devIndex;
46 6 : }
47 :
48 1 : int RaHdcPingTargetAdd(struct RaPingHandle *pingHandle, struct PingTargetInfo target[], uint32_t num)
49 : {
50 1 : unsigned int phyId = pingHandle->phyId;
51 : union OpPingAddData pingData;
52 : unsigned int i;
53 : int ret;
54 :
55 2 : for (i = 0; i < num; i++) {
56 1 : if (pingHandle->protocol == PROTOCOL_RDMA) {
57 1 : CHK_PRT_RETURN(target[i].localInfo.rdma.udpSport > MAX_PORT_NUM,
58 : hccp_err("[add][ra_hdc_ping]udp_sport(%u) invalid, i(%u), phyId(%u)",
59 : target[i].localInfo.rdma.udpSport, i, phyId), -EINVAL);
60 : }
61 :
62 1 : (void)memset_s(&pingData, sizeof(pingData), 0, sizeof(pingData));
63 1 : RaHdcPingInitRdev(&pingData.txData.rdev, phyId, pingHandle->devIndex);
64 1 : ret = memcpy_s(&(pingData.txData.target), sizeof(struct PingTargetInfo),
65 1 : &(target[i]), sizeof(struct PingTargetInfo));
66 1 : CHK_PRT_RETURN(ret, hccp_err("[add][ra_hdc_ping]memcpy_s target failed, ret(%d) i(%u) phyId(%u)",
67 : ret, i, phyId), -ESAFEFUNC);
68 1 : ret = RaHdcProcessMsg(RA_RS_PING_ADD, phyId, (char *)&pingData, sizeof(union OpPingAddData));
69 1 : CHK_PRT_RETURN(ret, hccp_err("[add][ra_hdc_ping]ra hdc message process failed ret(%d) i(%u) phyId(%u)",
70 : ret, i, phyId), ret);
71 : }
72 :
73 1 : return 0;
74 : }
75 :
76 2 : int RaHdcPingTaskStart(struct RaPingHandle *pingHandle, struct PingTaskAttr *attr)
77 : {
78 2 : union OpPingStartData pingData = { 0 };
79 2 : unsigned int phyId = pingHandle->phyId;
80 : int ret;
81 :
82 2 : RaHdcPingInitRdev(&pingData.txData.rdev, phyId, pingHandle->devIndex);
83 2 : ret = memcpy_s(&(pingData.txData.attr), sizeof(struct PingTaskAttr), attr, sizeof(struct PingTaskAttr));
84 2 : CHK_PRT_RETURN(ret, hccp_err("[start][ra_hdc_ping]memcpy_s attr failed, ret(%d), phyId(%u)",
85 : ret, phyId), -ESAFEFUNC);
86 :
87 2 : ret = RaHdcProcessMsg(RA_RS_PING_START, phyId, (char *)&pingData, sizeof(union OpPingStartData));
88 2 : CHK_PRT_RETURN(ret, hccp_err("[start][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)",
89 : ret, phyId), ret);
90 1 : return 0;
91 : }
92 :
93 0 : int RaHdcPingGetResults(struct RaPingHandle *pingHandle, struct PingTargetResult target[], uint32_t *num)
94 : {
95 0 : unsigned int phyId = pingHandle->phyId;
96 : union OpPingResultsData pingData;
97 0 : unsigned int totalNum = *num;
98 0 : unsigned int completeCnt = 0;
99 0 : unsigned int sendNum = 0;
100 0 : unsigned int i = 0;
101 0 : unsigned int j = 0;
102 0 : int ret = 0;
103 :
104 0 : while (completeCnt < totalNum) {
105 0 : (void)memset_s(&pingData, sizeof(pingData), 0, sizeof(pingData));
106 0 : RaHdcPingInitRdev(&pingData.txData.rdev, phyId, pingHandle->devIndex);
107 0 : sendNum = ((totalNum - completeCnt) >= RA_MAX_PING_TARGET_NUM) ?
108 : RA_MAX_PING_TARGET_NUM : (totalNum - completeCnt);
109 :
110 : // prepare tx data target
111 0 : for (i = 0; i < sendNum; i++) {
112 0 : j = i + completeCnt;
113 0 : ret = memcpy_s(&(pingData.txData.target[i]), sizeof(struct PingTargetCommInfo),
114 0 : &(target[j].remoteInfo), sizeof(struct PingTargetCommInfo));
115 0 : if (ret) {
116 0 : hccp_err("[get][ra_hdc_ping]memcpy_s remote_info failed, ret(%d), i(%u), j(%u), phyId(%u)",
117 : ret, i, j, phyId);
118 0 : goto out;
119 : }
120 : }
121 0 : pingData.txData.num = sendNum;
122 :
123 0 : ret = RaHdcProcessMsg(RA_RS_PING_GET_RESULTS, phyId, (char *)&pingData,
124 : sizeof(union OpPingResultsData));
125 : // caller needs to retry, degrade log level
126 0 : if (ret == -EAGAIN) {
127 0 : hccp_warn("[get][ra_hdc_ping]ra hdc message process unsuccessful, ret(%d) phyId(%u)", ret, phyId);
128 0 : goto out;
129 : }
130 :
131 0 : if (pingData.rxData.num > sendNum) {
132 0 : hccp_err("[get][ra_hdc_ping]rx_data.num[%u] is larger than send_num[%u], ret(%d) phyId(%u)",
133 : pingData.rxData.num, sendNum, ret, phyId);
134 0 : ret = -EINVAL;
135 0 : goto out;
136 : }
137 :
138 : // prepare rx data target
139 0 : for (i = 0; i < pingData.rxData.num; i++) {
140 0 : j = i + completeCnt;
141 0 : ret = memcpy_s(&(target[j].result), sizeof(struct PingResultInfo),
142 0 : &(pingData.rxData.target[i]), sizeof(struct PingResultInfo));
143 0 : if (ret) {
144 0 : hccp_err("[get][ra_hdc_ping]memcpy_s result failed, ret(%d), i(%u), j(%u), phyId(%u)",
145 : ret, i, j, phyId);
146 0 : ret = -ESAFEFUNC;
147 0 : goto out;
148 : }
149 : }
150 :
151 0 : completeCnt += pingData.rxData.num;
152 0 : if (ret) {
153 0 : hccp_err("[get][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId);
154 0 : goto out;
155 : }
156 : }
157 :
158 0 : out:
159 0 : *num = completeCnt;
160 :
161 0 : return ret;
162 : }
163 :
164 1 : int RaHdcPingTargetDel(struct RaPingHandle *pingHandle, struct PingTargetCommInfo target[], uint32_t num)
165 : {
166 1 : unsigned int phyId = pingHandle->phyId;
167 : union OpPingDelData pingData;
168 1 : unsigned int completeCnt = 0;
169 1 : unsigned int sendNum = 0;
170 1 : unsigned int i = 0;
171 1 : unsigned int j = 0;
172 : int ret;
173 :
174 2 : while (completeCnt < num) {
175 1 : (void)memset_s(&pingData, sizeof(pingData), 0, sizeof(pingData));
176 1 : RaHdcPingInitRdev(&pingData.txData.rdev, phyId, pingHandle->devIndex);
177 1 : sendNum = ((num - completeCnt) >= RA_MAX_PING_TARGET_NUM) ? RA_MAX_PING_TARGET_NUM : (num - completeCnt);
178 :
179 : // prepare tx data target
180 2 : for (i = 0; i < sendNum; i++) {
181 1 : j = i + completeCnt;
182 1 : ret = memcpy_s(&(pingData.txData.target[i]), sizeof(struct PingTargetCommInfo),
183 1 : &(target[j]), sizeof(struct PingTargetCommInfo));
184 1 : CHK_PRT_RETURN(ret, hccp_err("[del][ra_hdc_ping]memcpy_s target failed, ret(%d), i(%u) j(%u), phyId(%u)",
185 : ret, i, j, phyId), -ESAFEFUNC);
186 : }
187 1 : pingData.txData.num = sendNum;
188 :
189 1 : ret = RaHdcProcessMsg(RA_RS_PING_DEL, phyId, (char *)&pingData, sizeof(union OpPingDelData));
190 1 : CHK_PRT_RETURN(ret, hccp_err("[del][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)",
191 : ret, phyId), ret);
192 1 : completeCnt += sendNum;
193 : }
194 :
195 1 : return 0;
196 : }
197 :
198 1 : int RaHdcPingTaskStop(struct RaPingHandle *pingHandle)
199 : {
200 1 : union OpPingStopData pingData = { 0 };
201 1 : unsigned int phyId = pingHandle->phyId;
202 : int ret;
203 :
204 1 : RaHdcPingInitRdev(&pingData.txData.rdev, phyId, pingHandle->devIndex);
205 :
206 1 : ret = RaHdcProcessMsg(RA_RS_PING_STOP, phyId, (char *)&pingData, sizeof(union OpPingStopData));
207 1 : CHK_PRT_RETURN(ret, hccp_err("[stop][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)",
208 : ret, phyId), ret);
209 :
210 1 : return 0;
211 : }
212 :
213 1 : int RaHdcPingDeinit(struct RaPingHandle *pingHandle)
214 : {
215 1 : union OpPingDeinitData pingData = { 0 };
216 1 : unsigned int phyId = pingHandle->phyId;
217 : int ret;
218 :
219 1 : RaHdcPingInitRdev(&pingData.txData.rdev, phyId, pingHandle->devIndex);
220 :
221 1 : ret = RaHdcProcessMsg(RA_RS_PING_DEINIT, phyId, (char *)&pingData, sizeof(union OpPingDeinitData));
222 1 : CHK_PRT_RETURN(ret, hccp_err("[deinit][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)",
223 : ret, phyId), ret);
224 :
225 1 : return 0;
226 : }
|