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, struct PingInitInfo *initInfo)
17 : {
18 1 : unsigned int phyId = pingHandle->phyId;
19 1 : union OpPingInitData pingData = {0};
20 : int ret;
21 :
22 1 : ret = memcpy_s(&(pingData.txData.attr), sizeof(struct PingInitAttr), initAttr, sizeof(struct PingInitAttr));
23 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]memcpy_s init_attr failed, ret(%d) phyId(%u)", ret, phyId),
24 : -ESAFEFUNC);
25 :
26 1 : ret = RaHdcProcessMsg(RA_RS_PING_INIT, phyId, (char *)&pingData, sizeof(union OpPingInitData));
27 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId),
28 : ret);
29 :
30 1 : ret = memcpy_s(initInfo, sizeof(struct PingInitInfo), &(pingData.rxData.info), sizeof(struct PingInitInfo));
31 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]memcpy_s init_info failed, ret(%d) phyId(%u)", ret, phyId),
32 : -ESAFEFUNC);
33 1 : ret = memcpy_s(&(pingHandle->dev), sizeof(union PingDev), &(initAttr->dev), sizeof(union PingDev));
34 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_ping]memcpy_s dev info failed, ret(%d) phyId(%u)", ret, phyId),
35 : -ESAFEFUNC);
36 1 : pingHandle->devIndex = pingData.rxData.devIndex;
37 :
38 1 : return 0;
39 : }
40 :
41 6 : STATIC void RaHdcPingInitRdev(struct RaRsDevInfo *rdev, unsigned int phyId, unsigned int devIndex)
42 : {
43 6 : rdev->phyId = phyId;
44 6 : rdev->devIndex = devIndex;
45 6 : }
46 :
47 1 : int RaHdcPingTargetAdd(struct RaPingHandle *pingHandle, struct PingTargetInfo target[], uint32_t num)
48 : {
49 1 : unsigned int phyId = pingHandle->phyId;
50 : union OpPingAddData pingData;
51 : unsigned int i;
52 : int ret;
53 :
54 2 : for (i = 0; i < num; i++) {
55 1 : if (pingHandle->protocol == PROTOCOL_RDMA) {
56 1 : CHK_PRT_RETURN(target[i].localInfo.rdma.udpSport > MAX_PORT_NUM,
57 : hccp_err("[add][ra_hdc_ping]udp_sport(%u) invalid, i(%u), phyId(%u)", target[i].localInfo.rdma.udpSport,
58 : i, phyId),
59 : -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), &(target[i]),
65 : sizeof(struct PingTargetInfo));
66 1 : CHK_PRT_RETURN(ret,
67 : hccp_err("[add][ra_hdc_ping]memcpy_s target failed, ret(%d) i(%u) phyId(%u)", ret, i, phyId), -ESAFEFUNC);
68 1 : ret = RaHdcProcessMsg(RA_RS_PING_ADD, phyId, (char *)&pingData, sizeof(union OpPingAddData));
69 1 : CHK_PRT_RETURN(ret,
70 : hccp_err("[add][ra_hdc_ping]ra hdc message process failed ret(%d) i(%u) phyId(%u)", 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)", ret, phyId),
85 : -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)", ret, phyId),
89 : 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) ? RA_MAX_PING_TARGET_NUM
108 : : (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), &(target[j].remoteInfo),
114 : 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)", ret, i, j,
117 : 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, sizeof(union OpPingResultsData));
124 : // caller needs to retry, degrade log level
125 0 : if (ret == -EAGAIN) {
126 0 : hccp_warn("[get][ra_hdc_ping]ra hdc message process unsuccessful, ret(%d) phyId(%u)", ret, phyId);
127 0 : goto out;
128 : }
129 :
130 0 : if (pingData.rxData.num > sendNum) {
131 0 : hccp_err("[get][ra_hdc_ping]rx_data.num[%u] is larger than send_num[%u], ret(%d) phyId(%u)",
132 : pingData.rxData.num, sendNum, ret, phyId);
133 0 : ret = -EINVAL;
134 0 : goto out;
135 : }
136 :
137 : // prepare rx data target
138 0 : for (i = 0; i < pingData.rxData.num; i++) {
139 0 : j = i + completeCnt;
140 0 : ret = memcpy_s(&(target[j].result), sizeof(struct PingResultInfo), &(pingData.rxData.target[i]),
141 : sizeof(struct PingResultInfo));
142 0 : if (ret) {
143 0 : hccp_err("[get][ra_hdc_ping]memcpy_s result failed, ret(%d), i(%u), j(%u), phyId(%u)", ret, i, j,
144 : phyId);
145 0 : ret = -ESAFEFUNC;
146 0 : goto out;
147 : }
148 : }
149 :
150 0 : completeCnt += pingData.rxData.num;
151 0 : if (ret) {
152 0 : hccp_err("[get][ra_hdc_ping]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId);
153 0 : goto out;
154 : }
155 : }
156 :
157 0 : out:
158 0 : *num = completeCnt;
159 :
160 0 : return ret;
161 : }
162 :
163 1 : int RaHdcPingTargetDel(struct RaPingHandle *pingHandle, struct PingTargetCommInfo target[], uint32_t num)
164 : {
165 1 : unsigned int phyId = pingHandle->phyId;
166 : union OpPingDelData pingData;
167 1 : unsigned int completeCnt = 0;
168 1 : unsigned int sendNum = 0;
169 1 : unsigned int i = 0;
170 1 : unsigned int j = 0;
171 : int ret;
172 :
173 2 : while (completeCnt < num) {
174 1 : (void)memset_s(&pingData, sizeof(pingData), 0, sizeof(pingData));
175 1 : RaHdcPingInitRdev(&pingData.txData.rdev, phyId, pingHandle->devIndex);
176 1 : sendNum = ((num - completeCnt) >= RA_MAX_PING_TARGET_NUM) ? RA_MAX_PING_TARGET_NUM : (num - completeCnt);
177 :
178 : // prepare tx data target
179 2 : for (i = 0; i < sendNum; i++) {
180 1 : j = i + completeCnt;
181 1 : ret = memcpy_s(&(pingData.txData.target[i]), sizeof(struct PingTargetCommInfo), &(target[j]),
182 : sizeof(struct PingTargetCommInfo));
183 1 : CHK_PRT_RETURN(ret,
184 : hccp_err("[del][ra_hdc_ping]memcpy_s target failed, ret(%d), i(%u) j(%u), phyId(%u)", ret, i, j, phyId),
185 : -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)", ret, phyId),
191 : 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)", ret, phyId),
208 : 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)", ret, phyId),
223 : ret);
224 :
225 1 : return 0;
226 : }
|