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 <stdlib.h>
12 : #include <string.h>
13 : #include <arpa/inet.h>
14 : #include <unistd.h>
15 : #include <errno.h>
16 : #include "user_log.h"
17 : #include "ra_hdc.h"
18 : #include "securec.h"
19 : #include "ra.h"
20 : #include "ra_comm.h"
21 : #include "ra_rs_err.h"
22 : #include "dl_hal_function.h"
23 : #include "ra_hdc_socket.h"
24 :
25 5 : int RaHdcSocketBatchConnect(unsigned int phyId, struct SocketConnectInfoT conn[], unsigned int num)
26 : {
27 5 : union OpSocketConnectData *socketConnectData = NULL;
28 5 : unsigned int interfaceVersion = 0;
29 : int ret;
30 :
31 5 : socketConnectData = (union OpSocketConnectData *)calloc(sizeof(union OpSocketConnectData), sizeof(char));
32 5 : CHK_PRT_RETURN(socketConnectData == NULL, hccp_err("[batch_connect][ra_hdc_socket]calloc socket_connect_data "
33 : "failed, phyId(%u).", phyId), -ENOMEM);
34 :
35 4 : socketConnectData->txData.num = num;
36 :
37 4 : ret = RaGetSocketConnectInfo(conn, num, socketConnectData->txData.conn, MAX_SOCKET_NUM);
38 4 : if (ret != 0) {
39 1 : hccp_err("[batch_connect][ra_hdc_socket]ra_get_socket_connect_info failed, ret(%d) phyId(%u)", ret, phyId);
40 1 : goto out;
41 : }
42 :
43 : // check opcode version, use port by default
44 3 : ret = RaHdcGetInterfaceVersion(phyId, RA_RS_SOCKET_CONN, &interfaceVersion);
45 3 : if (ret == 0 && interfaceVersion >= RA_RS_SOCKET_CONN_VERSION) {
46 1 : socketConnectData->txData.num |= (1U << SOCKET_USE_PORT_BIT);
47 : }
48 :
49 3 : ret = RaHdcProcessMsg(RA_RS_SOCKET_CONN, phyId, (char *)socketConnectData,
50 : sizeof(union OpSocketConnectData));
51 3 : if (ret != 0) {
52 1 : hccp_err("[batch_connect][ra_hdc_socket]ra hdc message process failed, ret(%d) phyId(%u)", ret, phyId);
53 : }
54 2 : out:
55 4 : free(socketConnectData);
56 4 : socketConnectData = NULL;
57 4 : return ret;
58 : }
59 :
60 7 : int RaHdcSocketListenStart(unsigned int phyId, struct SocketListenInfoT conn[], unsigned int num)
61 : {
62 7 : union OpSocketListenData socketListenData = {0};
63 7 : unsigned int interfaceVersion = 0;
64 : int ret;
65 :
66 7 : socketListenData.txData.num = num;
67 7 : ret = RaGetSocketListenInfo(conn, num, socketListenData.txData.conn, MAX_SOCKET_NUM);
68 7 : CHK_PRT_RETURN(ret != 0, hccp_err("[listen_start][ra_hdc_socket]ra_get_socket_listen_info failed, "
69 : "ret(%d) phyId(%u)", ret, phyId), -EINVAL);
70 :
71 : // check opcode version, use port by default
72 6 : ret = RaHdcGetInterfaceVersion(phyId, RA_RS_SOCKET_LISTEN_START, &interfaceVersion);
73 6 : if (ret == 0 && interfaceVersion >= RA_RS_SOCKET_LISTEN_VERSION) {
74 1 : socketListenData.txData.num |= (1U << SOCKET_USE_PORT_BIT);
75 : }
76 :
77 6 : ret = RaHdcProcessMsg(RA_RS_SOCKET_LISTEN_START, phyId, (char *)&socketListenData,
78 : sizeof(union OpSocketListenData));
79 6 : CHK_PRT_RETURN(ret == -EADDRINUSE, hccp_warn("[listen_start][ra_hdc_socket]ra hdc message process unsuccessful,"
80 : " ret(%d) phyId(%u)", ret, phyId), ret);
81 6 : CHK_PRT_RETURN(ret != 0, hccp_err("[listen_start][ra_hdc_socket]ra hdc message process failed, ret(%d) phyId(%u)",
82 : ret, phyId), ret);
83 :
84 3 : ret = RaGetSocketListenResult(socketListenData.rxData.conn, num, conn, MAX_SOCKET_NUM);
85 3 : CHK_PRT_RETURN(ret != 0, hccp_err("[listen_start][ra_hdc_socket]ra_get_socket_listen_result failed, ret(%d) "
86 : "phyId(%u)", ret, phyId), -EINVAL);
87 :
88 2 : return ret;
89 : }
90 :
91 4 : int RaHdcSocketBatchClose(unsigned int phyId, struct SocketCloseInfoT conn[], unsigned int num)
92 : {
93 4 : union OpSocketCloseData socketCloseData = {0};
94 4 : unsigned int interfaceVersion = 0;
95 : unsigned int i;
96 : int ret;
97 :
98 4 : socketCloseData.txData.num = num;
99 8 : for (i = 0; i < num; i++) {
100 4 : if (conn[i].fdHandle == NULL) {
101 0 : hccp_err("[batch_close][ra_hdc_socket]i(%u), conn fdHandle is null", i);
102 0 : ret = -EINVAL;
103 0 : goto out;
104 : }
105 4 : socketCloseData.txData.conn[i].phyId = phyId;
106 4 : socketCloseData.txData.conn[i].closeFd = ((struct SocketHdcInfo *)conn[i].fdHandle)->fd;
107 : }
108 :
109 : // check opcode version, use RA_RS_GET_SOCKET opcode due to compatibility issue
110 : // use attr disuse_linger of the fist conn as the common attr for all(0 by default)
111 4 : ret = RaHdcGetInterfaceVersion(phyId, RA_RS_GET_SOCKET, &interfaceVersion);
112 4 : if (ret == 0 && interfaceVersion >= RA_RS_GET_SOCKET_VERSION && conn[0].disuseLinger != 0) {
113 0 : socketCloseData.txData.num |= (1U << SOCKET_DISUSE_LINGER_BIT);
114 : }
115 :
116 4 : ret = RaHdcProcessMsg(RA_RS_SOCKET_CLOSE, phyId, (char *)&socketCloseData,
117 : sizeof(union OpSocketCloseData));
118 4 : if (ret) {
119 2 : hccp_err("[batch_close][ra_hdc_socket]ra hdc message process failed, ret(%d) phyId(%u).", ret, phyId);
120 2 : goto out;
121 : }
122 :
123 2 : out:
124 4 : if (ret != (-EAGAIN)) {
125 8 : for (i = 0; i < num; i++) {
126 4 : if (conn[i].fdHandle != NULL) {
127 4 : free(conn[i].fdHandle);
128 4 : conn[i].fdHandle = NULL;
129 : }
130 : }
131 : }
132 4 : return ret;
133 : }
134 :
135 3 : int RaHdcSocketBatchAbort(unsigned int phyId, struct SocketConnectInfoT conn[], unsigned int num)
136 : {
137 3 : union OpSocketConnectData *socketConnectData = NULL;
138 : int ret;
139 :
140 3 : socketConnectData = (union OpSocketConnectData *)calloc(sizeof(union OpSocketConnectData), sizeof(char));
141 3 : CHK_PRT_RETURN(socketConnectData == NULL, hccp_err("[batch_abort][ra_hdc_socket]calloc socket_connect_data "
142 : "failed. phyId(%u).", phyId), -ENOMEM);
143 :
144 2 : socketConnectData->txData.num = num;
145 2 : ret = RaGetSocketConnectInfo(conn, num, socketConnectData->txData.conn, MAX_SOCKET_NUM);
146 2 : if (ret != 0) {
147 1 : hccp_err("[batch_abort][ra_hdc_socket]ra_get_socket_connect_info failed, ret(%d) phyId(%u)", ret, phyId);
148 1 : goto out;
149 : }
150 :
151 1 : ret = RaHdcProcessMsg(RA_RS_SOCKET_ABORT, phyId, (char *)socketConnectData,
152 : sizeof(union OpSocketConnectData));
153 1 : if (ret != 0) {
154 0 : hccp_err("[batch_abort][ra_hdc_socket]ra hdc message process failed, ret(%d) phyId(%u)", ret, phyId);
155 : }
156 1 : out:
157 2 : free(socketConnectData);
158 2 : socketConnectData = NULL;
159 2 : return ret;
160 : }
161 :
162 4 : int RaHdcSocketListenStop(unsigned int phyId, struct SocketListenInfoT conn[], unsigned int num)
163 : {
164 4 : union OpSocketListenData socketListenData = {0};
165 4 : unsigned int interfaceVersion = 0;
166 : int ret;
167 :
168 4 : socketListenData.txData.num = num;
169 :
170 4 : ret = RaGetSocketListenInfo(conn, num, socketListenData.txData.conn, MAX_SOCKET_NUM);
171 4 : CHK_PRT_RETURN(ret, hccp_err("[listen_stop][ra_hdc_socket]ra_hdc_socket_listen_stop memcpy_s failed, ret(%d)"
172 : "phyId(%u).", ret, phyId), -EINVAL);
173 :
174 : // check opcode version, use port by default
175 3 : ret = RaHdcGetInterfaceVersion(phyId, RA_RS_SOCKET_LISTEN_STOP, &interfaceVersion);
176 3 : if (ret == 0 && interfaceVersion >= RA_RS_SOCKET_LISTEN_VERSION) {
177 1 : socketListenData.txData.num |= (1U << SOCKET_USE_PORT_BIT);
178 : }
179 :
180 3 : ret = RaHdcProcessMsg(RA_RS_SOCKET_LISTEN_STOP, phyId, (char *)&socketListenData,
181 : sizeof(union OpSocketListenData));
182 3 : CHK_PRT_RETURN(ret, hccp_err("[listen_stop][ra_hdc_socket]ra hdc message process failed ret(%d) phyId(%u).",
183 : ret, phyId), ret);
184 :
185 2 : return 0;
186 : }
187 :
188 4 : STATIC int RaGetIpAndTagInfo(union OpSocketInfoData *socketInfoData, struct RaSocketHandle *socketHandle,
189 : struct SocketInfoT conn[], unsigned int index)
190 : {
191 : int ret;
192 :
193 4 : ret = memcpy_s(&(socketInfoData->txData.conn[index].localIp), sizeof(union HccpIpAddr),
194 4 : &(socketHandle->rdevInfo.localIp), sizeof(union HccpIpAddr));
195 4 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_ip_and_tag_info]memcpy_s local_ip failed, ret(%d), index(%u)",
196 : ret, index), -ESAFEFUNC);
197 3 : ret = memcpy_s(&(socketInfoData->txData.conn[index].remoteIp), sizeof(union HccpIpAddr),
198 3 : &(conn[index].remoteIp), sizeof(union HccpIpAddr));
199 3 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_ip_and_tag_info]memcpy_s remote_ip failed, ret(%d), index(%u)",
200 : ret, index), -ESAFEFUNC);
201 3 : ret = memcpy_s(socketInfoData->txData.conn[index].tag, sizeof(socketInfoData->txData.conn[index].tag),
202 3 : conn[index].tag, sizeof(conn[index].tag));
203 3 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_ip_and_tag_info]memcpy_s tag failed, ret(%d), index(%u)",
204 : ret, index), -ESAFEFUNC);
205 :
206 3 : return 0;
207 : }
208 :
209 4 : STATIC int RaAssembleSockets(union OpSocketInfoData *socketInfoData, struct SocketInfoT *conn,
210 : unsigned int num, const int sockFd[], size_t sockFdLen)
211 : {
212 : unsigned int i;
213 : int ret;
214 4 : struct RaSocketHandle *socketHandle = NULL;
215 :
216 7 : for (i = 0; (i < num) && (i < sockFdLen); i++) {
217 4 : if (conn[i].fdHandle == NULL) {
218 2 : conn[i].fdHandle = (struct SocketHdcInfo *)calloc(1, sizeof(struct SocketHdcInfo));
219 2 : if (conn[i].fdHandle == NULL) {
220 0 : hccp_err("[assemble][ra_sockets]fd handle calloc failed.");
221 0 : ret = -ENOMEM;
222 0 : goto calloc_err;
223 : }
224 : }
225 4 : ((socketInfoData->txData).conn[i]).fd = sockFd[i];
226 4 : socketHandle = (struct RaSocketHandle *)conn[i].socketHandle;
227 4 : socketInfoData->txData.conn[i].phyId = socketHandle->rdevInfo.phyId;
228 4 : socketInfoData->txData.conn[i].family = socketHandle->rdevInfo.family;
229 4 : ret = RaGetIpAndTagInfo(socketInfoData, socketHandle, conn, i);
230 4 : if (ret) {
231 1 : hccp_err("[assemble][ra_sockets]ra_get_ip_and_tag_info failed, ret(%d)", ret);
232 1 : ret = -EINVAL;
233 1 : goto mem_err;
234 : }
235 : }
236 :
237 3 : return 0;
238 :
239 1 : mem_err:
240 1 : i++;
241 1 : calloc_err:
242 2 : for (; i > 0;) {
243 1 : i--;
244 1 : if (conn[i].fdHandle != NULL) {
245 1 : free(conn[i].fdHandle);
246 1 : conn[i].fdHandle = NULL;
247 : }
248 : }
249 :
250 1 : return ret;
251 : }
252 :
253 1 : static void FreeAssembleSockets(struct SocketInfoT conn[], unsigned int num)
254 : {
255 : unsigned int i;
256 :
257 2 : for (i = 0; i < num; i++) {
258 1 : if (conn[i].fdHandle != NULL) {
259 1 : free(conn[i].fdHandle);
260 1 : conn[i].fdHandle = NULL;
261 : }
262 : }
263 1 : }
264 :
265 6 : int RaHdcSocketSend(unsigned int phyId, const void *handle, const void *data, unsigned long long size)
266 : {
267 6 : union OpSocketSendData *sendDataHead = NULL;
268 : int realSendSize;
269 : int ret;
270 :
271 6 : if (size > SOCKET_SEND_MAXLEN) {
272 2 : size = SOCKET_SEND_MAXLEN;
273 : }
274 6 : sendDataHead = (union OpSocketSendData *)calloc(sizeof(union OpSocketSendData), sizeof(char));
275 6 : CHK_PRT_RETURN(sendDataHead == NULL, hccp_err("[send][ra_hdc_socket]calloc failed, phyId(%u)",
276 : phyId), -ENOMEM);
277 :
278 5 : sendDataHead->txData.fd = (unsigned int)((const struct SocketHdcInfo *)handle)->fd;
279 5 : sendDataHead->txData.sendSize = size;
280 :
281 5 : ret = memcpy_s(sendDataHead->txData.dataSend, SOCKET_SEND_MAXLEN, data, size);
282 5 : if (ret) {
283 1 : hccp_err("[send][ra_hdc_socket]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
284 1 : realSendSize = -ESAFEFUNC;
285 1 : goto out;
286 : }
287 :
288 4 : ret = RaHdcProcessMsg(RA_RS_SOCKET_SEND, phyId, (char *)sendDataHead,
289 : sizeof(union OpSocketSendData));
290 4 : if (ret) {
291 2 : if (ret > 0) {
292 0 : ret = -EINVAL; /* 0:success; ret > 0:failed maybe drv interface return; ret < 0:failed maybe rs return */
293 : }
294 2 : if (ret != -EAGAIN) {
295 1 : hccp_warn("[send][ra_hdc_socket]ra hdc message process unsuccessful, ret(%d) phyId(%u)", ret, phyId);
296 : }
297 2 : realSendSize = ret;
298 2 : goto out;
299 : }
300 :
301 2 : realSendSize = (int)sendDataHead->rxData.realSendSize;
302 :
303 5 : out:
304 5 : free(sendDataHead);
305 5 : sendDataHead = NULL;
306 5 : return realSendSize;
307 : }
308 :
309 8 : int RaHdcSocketRecv(unsigned int phyId, const void *handle, void *data, unsigned long long size)
310 : {
311 8 : union OpSocketRecvData *recvDataHead = NULL;
312 : int realRecvSize;
313 : int ret;
314 :
315 8 : if (size > SOCKET_SEND_MAXLEN) {
316 0 : size = SOCKET_SEND_MAXLEN;
317 : }
318 :
319 8 : recvDataHead = (union OpSocketRecvData *)calloc(size + sizeof(union OpSocketRecvData), sizeof(char));
320 8 : CHK_PRT_RETURN(recvDataHead == NULL, hccp_err("[recv][ra_hdc_socket]calloc failed. phyId(%u)", phyId), -ENOMEM);
321 7 : recvDataHead->txData.fd = (unsigned int)((const struct SocketHdcInfo *)handle)->fd;
322 7 : recvDataHead->txData.recvSize = size;
323 :
324 7 : ret = RaHdcProcessMsg(RA_RS_SOCKET_RECV, phyId, (char *)recvDataHead,
325 : sizeof(union OpSocketRecvData) + size);
326 7 : if (ret) {
327 4 : if (ret > 0) {
328 0 : ret = -EINVAL; /* 0:success; ret > 0:failed maybe drv interface return; ret < 0:failed maybe rs return */
329 : }
330 4 : if (ret != -EAGAIN) {
331 3 : hccp_warn("[recv][ra_hdc_socket]ra hdc message process unsuccessful, ret(%d) phyId(%u)", ret, phyId);
332 : }
333 4 : realRecvSize = ret;
334 4 : goto out;
335 : }
336 :
337 3 : realRecvSize = (int)recvDataHead->rxData.realRecvSize;
338 3 : if (realRecvSize <= 0) {
339 3 : goto out;
340 : } else {
341 0 : ret = memcpy_s(data, size, (char *)recvDataHead + sizeof(union OpSocketRecvData), realRecvSize);
342 0 : if (ret) {
343 0 : hccp_err("[recv][ra_hdc_socket]memcpy_s failed, ret(%d) phyId(%u)", ret, phyId);
344 0 : realRecvSize = -ESAFEFUNC;
345 0 : goto out;
346 : }
347 : }
348 :
349 0 : out:
350 7 : free(recvDataHead);
351 7 : recvDataHead = NULL;
352 7 : return realRecvSize;
353 : }
354 :
355 2 : STATIC int RaGetRecvSockets(union OpSocketInfoData *socketInfoData, struct SocketInfoT conn[],
356 : unsigned int num)
357 : {
358 : unsigned int i;
359 : int realNum;
360 : int ret;
361 2 : struct RaSocketHandle *socketHandle = NULL;
362 :
363 4 : for (i = 0; i < num; i++) {
364 2 : ((struct SocketHdcInfo *)conn[i].fdHandle)->phyId = socketInfoData->rxData.conn[i].phyId;
365 2 : ((struct SocketHdcInfo *)conn[i].fdHandle)->fd = socketInfoData->rxData.conn[i].fd;
366 2 : socketHandle = (struct RaSocketHandle *)conn[i].socketHandle;
367 2 : ((struct SocketHdcInfo *)conn[i].fdHandle)->socketHandle = socketHandle;
368 2 : ret = memcpy_s(&(socketHandle->rdevInfo.localIp), sizeof(union HccpIpAddr),
369 2 : &(socketInfoData->rxData.conn[i].localIp), sizeof(union HccpIpAddr));
370 2 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_recv_sockets]memcpy_s local_ip failed, ret(%d)", ret), -ESAFEFUNC);
371 2 : ret = memcpy_s(&(conn[i].remoteIp), sizeof(union HccpIpAddr), &(socketInfoData->rxData.conn[i].remoteIp),
372 : sizeof(union HccpIpAddr));
373 2 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_recv_sockets]memcpy_s remote_ip failed, ret(%d)", ret), -ESAFEFUNC);
374 2 : conn[i].status = socketInfoData->rxData.conn[i].status;
375 : }
376 :
377 2 : realNum = socketInfoData->rxData.num;
378 2 : return realNum;
379 : }
380 :
381 5 : int RaHdcGetSockets(unsigned int phyId, unsigned int role, struct SocketInfoT conn[], unsigned int num)
382 : {
383 : int ret;
384 5 : int sockFd[MAX_SOCKET_NUM] = {0};
385 : union OpSocketInfoData *socketInfoData;
386 :
387 5 : socketInfoData = (union OpSocketInfoData *)calloc(sizeof(union OpSocketInfoData), sizeof(char));
388 5 : CHK_PRT_RETURN(socketInfoData == NULL, hccp_err("[get][ra_hdc_sockets]socket info data"
389 : "calloc failed phyId(%u)", phyId), -ENOMEM);
390 4 : socketInfoData->txData.num = num;
391 4 : socketInfoData->txData.role = role;
392 :
393 4 : ret = RaAssembleSockets(socketInfoData, conn, num, sockFd, sizeof(sockFd) / sizeof(sockFd[0]));
394 4 : if (ret) {
395 1 : hccp_err("[get][ra_hdc_sockets]assemble sockets error ret(%d) phyId(%u)", ret, phyId);
396 1 : goto out;
397 : }
398 :
399 3 : ret = RaHdcProcessMsg(RA_RS_GET_SOCKET, phyId, (char *)socketInfoData,
400 : sizeof(union OpSocketInfoData));
401 3 : if (ret) {
402 1 : hccp_err("[get][ra_hdc_sockets]ra hdc message process failed ret(%d) phyId(%u)", ret, phyId);
403 1 : ret = -EINVAL; /* !=0 is error situation return negative value, function normal return >=0 */
404 1 : goto err;
405 : }
406 :
407 2 : ret = RaGetRecvSockets(socketInfoData, conn, num);
408 : // no sockets get, free socket info(fd_handle)
409 2 : if (ret == 0) {
410 0 : goto err;
411 : }
412 2 : free(socketInfoData);
413 2 : socketInfoData = NULL;
414 2 : return ret;
415 :
416 1 : err:
417 1 : FreeAssembleSockets(conn, num);
418 2 : out:
419 2 : free(socketInfoData);
420 2 : socketInfoData = NULL;
421 2 : return ret;
422 : }
423 :
424 6 : STATIC int RaHdcGetAllVnic(unsigned int curPhyId, unsigned int *vnicIp, unsigned int num)
425 : {
426 : int ret;
427 : unsigned int logicId, phyId;
428 : unsigned int devNum;
429 : union OpGetVnicIpData vnicIpData;
430 :
431 6 : ret = DlDrvGetDevNum(&devNum);
432 6 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_all_vnic]get dev num failed, ret(%d)", ret), ret);
433 :
434 9 : for (logicId = 0; logicId < devNum; logicId++) {
435 8 : ret = DlDrvDeviceGetPhyIdByIndex(logicId, &phyId);
436 8 : CHK_PRT_RETURN(ret != 0 || phyId >= RA_MAX_PHY_ID_NUM || phyId >= num, hccp_err("[get][ra_hdc_all_vnic]get phy"
437 : "id failed, logicId(%u) ret(%d) phyId(%u) >= %d or >= %u invalid", logicId, ret, phyId,
438 : RA_MAX_PHY_ID_NUM, num), -ENODEV);
439 :
440 8 : vnicIpData.txData.phyId = phyId;
441 8 : ret = RaHdcProcessMsg(RA_RS_GET_VNIC_IP, curPhyId,
442 : (char *)&vnicIpData, sizeof(union OpGetVnicIpData));
443 8 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_all_vnic]ra hdc message process failed ret(%d), phyId(%d),"
444 : "logicId(%d)", ret, phyId, logicId), ret);
445 :
446 4 : vnicIp[phyId] = vnicIpData.rxData.vnicIp;
447 4 : hccp_info("vnic ipaddr:0x%x, get vnicIp:0x%x, phyId:%u, logicId:%u", vnicIpData.rxData.vnicIp,
448 : vnicIp[phyId], phyId, logicId);
449 : }
450 :
451 1 : return 0;
452 : }
453 :
454 2 : int RaHdcGetVnicIpInfosV1(unsigned int phyId, enum IdType type, unsigned int ids[], unsigned int num,
455 : struct IpInfo infos[])
456 : {
457 2 : union OpGetVnicIpInfosDataV1 vnicIpData = {0};
458 2 : unsigned int completeCnt = 0;
459 2 : unsigned int sendNum = 0;
460 : int ret;
461 :
462 2 : while (completeCnt < num) {
463 2 : vnicIpData.txData.phyId = phyId;
464 2 : sendNum = ((num - completeCnt) >= MAX_IP_INFO_NUM_V1) ? MAX_IP_INFO_NUM_V1 : (num - completeCnt);
465 2 : ret = memcpy_s(vnicIpData.txData.ids, sizeof(unsigned int) * MAX_IP_INFO_NUM_V1,
466 2 : &ids[completeCnt], sizeof(unsigned int) * sendNum);
467 2 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][ip_infos]memcpy_s for ids failed ret(%d)", ret), -ESAFEFUNC);
468 2 : vnicIpData.txData.num = sendNum;
469 2 : vnicIpData.txData.type = type;
470 :
471 2 : ret = RaHdcProcessMsg(RA_RS_GET_VNIC_IP_INFOS_V1, phyId, (char *)&vnicIpData,
472 : sizeof(union OpGetVnicIpInfosDataV1));
473 2 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][ip_infos]ra hdc message process failed ret(%d), phyId(%u)",
474 : ret, phyId), ret);
475 :
476 0 : ret = memcpy_s(&infos[completeCnt], sizeof(struct IpInfo) * (num - completeCnt),
477 : vnicIpData.rxData.infos, sizeof(struct IpInfo) * sendNum);
478 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][ip_infos]memcpy_s for ids failed ret(%d)", ret), -ESAFEFUNC);
479 0 : completeCnt += sendNum;
480 : }
481 :
482 0 : return 0;
483 : }
484 :
485 2 : int RaHdcGetVnicIpInfos(unsigned int phyId, enum IdType type, unsigned int ids[], unsigned int num,
486 : struct IpInfo infos[])
487 : {
488 2 : union OpGetVnicIpInfosData vnicIpData = {0};
489 2 : unsigned int interfaceVersion = 0;
490 2 : unsigned int completeCnt = 0;
491 2 : unsigned int sendNum = 0;
492 : int ret;
493 :
494 : // origin procedure for compatibility issue
495 2 : ret = RaHdcGetInterfaceVersion(phyId, RA_RS_GET_VNIC_IP_INFOS, &interfaceVersion);
496 2 : if (ret != 0 || interfaceVersion < RA_RS_GET_VNIC_IP_INFOS_VERSION) {
497 2 : return RaHdcGetVnicIpInfosV1(phyId, type, ids, num, infos);
498 : }
499 :
500 0 : while (completeCnt < num) {
501 0 : vnicIpData.txData.phyId = phyId;
502 0 : sendNum = ((num - completeCnt) >= MAX_IP_INFO_NUM) ? MAX_IP_INFO_NUM : (num - completeCnt);
503 0 : ret = memcpy_s(vnicIpData.txData.ids, sizeof(unsigned int) * MAX_IP_INFO_NUM,
504 0 : &ids[completeCnt], sizeof(unsigned int) * sendNum);
505 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][ip_infos]memcpy_s for ids failed ret(%d)", ret), -ESAFEFUNC);
506 0 : vnicIpData.txData.num = sendNum;
507 0 : vnicIpData.txData.type = type;
508 :
509 0 : ret = RaHdcProcessMsg(RA_RS_GET_VNIC_IP_INFOS, phyId, (char *)&vnicIpData,
510 : sizeof(union OpGetVnicIpInfosData));
511 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][ip_infos]ra hdc message process failed ret(%d), phyId(%u)",
512 : ret, phyId), ret);
513 :
514 0 : ret = memcpy_s(&infos[completeCnt], sizeof(struct IpInfo) * (num - completeCnt),
515 : vnicIpData.rxData.infos, sizeof(struct IpInfo) * sendNum);
516 0 : CHK_PRT_RETURN(ret != 0, hccp_err("[get][ip_infos]memcpy_s for ids failed ret(%d)", ret), -ESAFEFUNC);
517 0 : completeCnt += sendNum;
518 : }
519 :
520 0 : return 0;
521 : }
522 :
523 10 : int RaHdcSocketInit(struct rdev rdevInfo)
524 : {
525 10 : unsigned int vnicIp[RA_MAX_VNIC_NUM] = {0};
526 : union OpSocketInitData socketInitData;
527 10 : unsigned int interfaceVersion = 0;
528 : int ret;
529 :
530 10 : ret = memset_s(&socketInitData, sizeof(socketInitData), 0, sizeof(socketInitData));
531 10 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_socket]memset_s failed ret(%d) phyId(%u)", ret,
532 : rdevInfo.phyId), -ESAFEFUNC);
533 :
534 : // check opcode version, init g_vnics with invalid ip mask 0xFFFFFFFF
535 9 : ret = RaHdcGetInterfaceVersion(rdevInfo.phyId, RA_RS_GET_VNIC_IP_INFOS_V1, &interfaceVersion);
536 9 : if (ret == 0 && interfaceVersion >= RA_RS_GET_VNIC_IP_INFOS_VERSION) {
537 1 : ret = memset_s(vnicIp, sizeof(vnicIp), 0xFFFFFFFF, sizeof(vnicIp));
538 1 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_socket]memset_s failed ret(%d) phyId(%u)", ret,
539 : rdevInfo.phyId), -ESAFEFUNC);
540 : } else {
541 : // origin procedure: init g_vnics with vnic_ip get by phyId
542 8 : ret = RaHdcGetAllVnic(rdevInfo.phyId, vnicIp, RA_MAX_VNIC_NUM);
543 8 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_socket]ra_hdc_get_all_vnic failed ret(%d) phyId(%u)", ret,
544 : rdevInfo.phyId), ret);
545 : }
546 :
547 3 : ret = memcpy_s(&(socketInitData.txData.vnicIp), sizeof(socketInitData.txData.vnicIp),
548 : &vnicIp, sizeof(vnicIp));
549 3 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_socket]memcpy_s for vnic_ip failed ret(%d) phyId(%u)", ret,
550 : rdevInfo.phyId), -ESAFEFUNC);
551 :
552 2 : socketInitData.txData.num = RA_MAX_VNIC_NUM;
553 2 : ret = RaHdcProcessMsg(RA_RS_SOCKET_INIT, rdevInfo.phyId, (char *)&socketInitData,
554 : sizeof(union OpSocketInitData));
555 2 : CHK_PRT_RETURN(ret, hccp_err("[init][ra_hdc_socket]ra hdc message process failed ret(%d) phyId(%u)", ret,
556 : rdevInfo.phyId), ret);
557 :
558 2 : return 0;
559 : }
560 :
561 4 : int RaHdcSocketDeinit(struct rdev rdevInfo)
562 : {
563 : int ret;
564 : union OpSocketDeinitData socketDeinitData;
565 :
566 4 : ret = memset_s(&socketDeinitData, sizeof(socketDeinitData), 0, sizeof(socketDeinitData));
567 4 : CHK_PRT_RETURN(ret, hccp_err("[deinit][ra_hdc_socket]memset_s failed ret(%d) phyId(%u)", ret,
568 : rdevInfo.phyId), -ESAFEFUNC);
569 :
570 3 : ret = memcpy_s(&(socketDeinitData.txData.rdevInfo), sizeof(struct rdev), &rdevInfo, sizeof(struct rdev));
571 3 : CHK_PRT_RETURN(ret, hccp_err("[deinit][ra_hdc_socket]memcpy_s failed ret(%d) phyId(%u)", ret,
572 : rdevInfo.phyId), -ESAFEFUNC);
573 :
574 2 : ret = RaHdcProcessMsg(RA_RS_SOCKET_DEINIT, rdevInfo.phyId, (char *)&socketDeinitData,
575 : sizeof(union OpSocketDeinitData));
576 2 : CHK_PRT_RETURN(ret, hccp_err("[deinit][ra_hdc_socket]ra hdc message process failed ret(%d) phyId(%u)", ret,
577 : rdevInfo.phyId), ret);
578 :
579 1 : return 0;
580 : }
581 :
582 3 : int RaHdcGetIfnum(unsigned int phyId, bool isAll, unsigned int *num)
583 : {
584 3 : union OpIfnumData ifnumData = {0};
585 : int ret;
586 :
587 3 : ifnumData.txData.num = isAll ? RA_RS_GET_ALL_IP_BIT_MASK : 0;
588 3 : ifnumData.txData.phyId = phyId;
589 3 : ret = RaHdcProcessMsg(RA_RS_GET_IFNUM, phyId, (char *)&ifnumData, sizeof(union OpIfnumData));
590 3 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_ifnum]ra hdc message process failed ret(%d) phyId(%u)",
591 : ret, phyId), ret);
592 :
593 2 : *num = ifnumData.rxData.num;
594 :
595 2 : return 0;
596 : }
597 :
598 11 : int RaHdcGetIfaddrs(unsigned int phyId, struct IfaddrInfo ifaddrInfos[], unsigned int *num)
599 : {
600 11 : union OpIfaddrData ifaddrData = {0};
601 : int ret;
602 :
603 11 : ifaddrData.txData.num = *num;
604 11 : ret = memcpy_s(ifaddrData.txData.ifaddrInfos, sizeof(struct IfaddrInfo) * MAX_INTERFACE_NUM, ifaddrInfos,
605 11 : sizeof(struct IfaddrInfo) * (*num));
606 11 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_ifaddrs]memcpy_s failed, ret(%d) phyId(%u)",
607 : ret, phyId), -ESAFEFUNC);
608 :
609 10 : ifaddrData.txData.phyId = phyId;
610 10 : ret = RaHdcProcessMsg(RA_RS_GET_IFADDRS, phyId, (char *)&ifaddrData, sizeof(union OpIfaddrData));
611 10 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_ifaddrs]ra hdc message process failed ret(%d) phyId(%u)",
612 : ret, phyId), ret);
613 :
614 1 : ret = memcpy_s(ifaddrInfos, sizeof(struct IfaddrInfo) * (*num), ifaddrData.rxData.ifaddrInfos,
615 1 : sizeof(struct IfaddrInfo) * (ifaddrData.rxData.num));
616 1 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_ifaddrs]memcpy_s failed, ret(%d) phyId(%u)",
617 : ret, phyId), -ESAFEFUNC);
618 :
619 1 : *num = ifaddrData.rxData.num;
620 1 : return 0;
621 : }
622 :
623 7 : int RaHdcGetIfaddrsV2(unsigned int phyId, bool isAll, struct InterfaceInfo interfaceInfos[], unsigned int *num)
624 : {
625 7 : union OpIfaddrDataV2 ifaddrData = {0};
626 : int ret;
627 :
628 7 : ifaddrData.txData.num = isAll ? (*num | RA_RS_GET_ALL_IP_BIT_MASK) : *num;
629 7 : ret = memcpy_s(ifaddrData.txData.interfaceInfos, sizeof(struct InterfaceInfo) * MAX_INTERFACE_NUM,
630 7 : interfaceInfos, sizeof(struct InterfaceInfo) * (*num));
631 7 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_ifaddrs_v2]memcpy_s tx interface infos failed, ret(%d) phyId(%u)",
632 : ret, phyId), -ESAFEFUNC);
633 :
634 6 : ifaddrData.txData.phyId = phyId;
635 6 : ret = RaHdcProcessMsg(RA_RS_GET_IFADDRS_V2, phyId, (char *)&ifaddrData, sizeof(union OpIfaddrDataV2));
636 6 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_ifaddrs_v2]ra hdc message process failed ret(%d) phyId(%u)",
637 : ret, phyId), ret);
638 :
639 4 : ret = memcpy_s(interfaceInfos, sizeof(struct InterfaceInfo) * (*num), ifaddrData.rxData.interfaceInfos,
640 4 : sizeof(struct InterfaceInfo) * (ifaddrData.rxData.num));
641 4 : CHK_PRT_RETURN(ret, hccp_err("[get][ra_hdc_ifaddrs_v2]memcpy_s rx interface infos failed, ret(%d) phyId(%u)",
642 : ret, phyId), -ESAFEFUNC);
643 :
644 4 : *num = ifaddrData.rxData.num;
645 4 : return 0;
646 : }
647 :
648 16 : static int RaHdcSocketWhiteListOpV1(unsigned int opcode, struct rdev rdevInfo,
649 : struct SocketWlistInfoT whiteList[], unsigned int num)
650 : {
651 16 : union OpWlistData *wlistData = NULL;
652 : int ret;
653 16 : wlistData = (union OpWlistData *)calloc(sizeof(union OpWlistData), sizeof(char));
654 16 : CHK_PRT_RETURN(wlistData == NULL, hccp_err("[op][ra_hdc_socket_white_list]calloc wlist data failed! phyId(%u)",
655 : rdevInfo.phyId), -ENOMEM);
656 14 : wlistData->txData.num = num;
657 14 : ret = memcpy_s(&(wlistData->txData.rdevInfo), sizeof(struct rdev), &(rdevInfo), sizeof(struct rdev));
658 14 : if (ret) {
659 2 : hccp_err("[op][ra_hdc_socket_white_list]memcpy_s for rdev_info failed, ret(%d) phyId(%u)",
660 : ret, rdevInfo.phyId);
661 2 : ret = -ESAFEFUNC;
662 2 : goto out;
663 : }
664 :
665 12 : ret = memcpy_s(wlistData->txData.wlist, sizeof(struct SocketWlistInfoT) * MAX_WLIST_NUM_V1, whiteList,
666 : sizeof(struct SocketWlistInfoT) * num);
667 12 : if (ret) {
668 0 : hccp_err("[op][ra_hdc_socket_white_list]memcpy_s for wlist failed, ret(%d) phyId(%u)", ret, rdevInfo.phyId);
669 0 : ret = -ESAFEFUNC;
670 0 : goto out;
671 : }
672 :
673 12 : ret = RaHdcProcessMsg(opcode, rdevInfo.phyId, (char *)wlistData, sizeof(union OpWlistData));
674 12 : if (ret) {
675 6 : hccp_err("[op][ra_hdc_socket_white_list]ra hdc process msg failed ret(%d) phyId(%u)", ret, rdevInfo.phyId);
676 6 : goto out;
677 : }
678 :
679 6 : out:
680 14 : free(wlistData);
681 14 : wlistData = NULL;
682 14 : return ret;
683 : }
684 :
685 0 : static int RaHdcSocketWhiteListOpV2(unsigned int opcode, struct rdev rdevInfo,
686 : struct SocketWlistInfoT whiteList[], unsigned int num)
687 : {
688 : int ret;
689 0 : union OpWlistDataV2 *wlistData = NULL;
690 :
691 0 : wlistData = (union OpWlistDataV2 *)calloc(sizeof(union OpWlistDataV2), sizeof(char));
692 0 : CHK_PRT_RETURN(wlistData == NULL, hccp_err("[op][ra_hdc_socket_white_list]calloc wlist data failed! phyId(%u)",
693 : rdevInfo.phyId), -ENOMEM);
694 0 : wlistData->txData.num = num;
695 0 : ret = memcpy_s(&(wlistData->txData.rdevInfo), sizeof(struct rdev), &(rdevInfo), sizeof(struct rdev));
696 0 : if (ret) {
697 0 : hccp_err("[op][ra_hdc_socket_white_list]memcpy_s for rdev_info failed, ret(%d) phyId(%u)",
698 : ret, rdevInfo.phyId);
699 0 : ret = -ESAFEFUNC;
700 0 : goto out;
701 : }
702 :
703 0 : ret = memcpy_s(wlistData->txData.wlist, sizeof(struct SocketWlistInfoT) * MAX_WLIST_NUM, whiteList,
704 : sizeof(struct SocketWlistInfoT) * num);
705 0 : if (ret) {
706 0 : hccp_err("[op][ra_hdc_socket_white_list]memcpy_s for wlist failed, ret(%d) phyId(%u)", ret, rdevInfo.phyId);
707 0 : ret = -ESAFEFUNC;
708 0 : goto out;
709 : }
710 :
711 0 : ret = RaHdcProcessMsg(opcode, rdevInfo.phyId, (char *)wlistData, sizeof(union OpWlistDataV2));
712 0 : if (ret) {
713 0 : hccp_err("[op][ra_hdc_socket_white_list]ra hdc process msg failed ret(%d) phyId(%u)", ret, rdevInfo.phyId);
714 0 : goto out;
715 : }
716 :
717 0 : out:
718 0 : free(wlistData);
719 0 : wlistData = NULL;
720 0 : return ret;
721 : }
722 :
723 5 : int RaHdcSocketWhiteListAdd(struct rdev rdevInfo, struct SocketWlistInfoT whiteList[], unsigned int num)
724 : {
725 : int ret;
726 5 : unsigned int interfaceVersion = 0;
727 :
728 5 : ret = RaHdcGetInterfaceVersion(rdevInfo.phyId, RA_RS_WLIST_ADD_V2, &interfaceVersion);
729 5 : if (ret != 0 || interfaceVersion != RA_RS_WLIST_ADD_V2_VERSION) {
730 5 : return RaHdcSocketWhiteListOpV1(RA_RS_WLIST_ADD, rdevInfo, whiteList, num);
731 : }
732 :
733 0 : return RaHdcSocketWhiteListOpV2(RA_RS_WLIST_ADD_V2, rdevInfo, whiteList, num);
734 : }
735 :
736 11 : int RaHdcSocketWhiteListDel(struct rdev rdevInfo, struct SocketWlistInfoT whiteList[], unsigned int num)
737 : {
738 : int ret;
739 11 : unsigned int interfaceVersion = 0;
740 :
741 11 : ret = RaHdcGetInterfaceVersion(rdevInfo.phyId, RA_RS_WLIST_DEL_V2, &interfaceVersion);
742 11 : if (ret != 0 || interfaceVersion != RA_RS_WLIST_DEL_V2_VERSION) {
743 11 : return RaHdcSocketWhiteListOpV1(RA_RS_WLIST_DEL, rdevInfo, whiteList, num);
744 : }
745 :
746 0 : return RaHdcSocketWhiteListOpV2(RA_RS_WLIST_DEL_V2, rdevInfo, whiteList, num);
747 : }
748 :
749 2 : int RaHdcSocketAcceptCreditAdd(unsigned int phyId, struct SocketListenInfoT conn[], unsigned int num,
750 : unsigned int creditLimit)
751 : {
752 2 : union OpAcceptCreditData opData = {0};
753 : int ret;
754 :
755 2 : opData.txData.phyId = phyId;
756 2 : opData.txData.creditLimit = creditLimit;
757 2 : opData.txData.num = num;
758 2 : ret = RaGetSocketListenInfo(conn, num, opData.txData.conn, MAX_SOCKET_NUM);
759 2 : CHK_PRT_RETURN(ret != 0, hccp_err("[set][ra_hdc_socket]ra_get_socket_listen_info failed, ret(%d) phyId(%u)",
760 : ret, phyId), -EINVAL);
761 :
762 1 : ret = RaHdcProcessMsg(RA_RS_ACCEPT_CREDIT_ADD, phyId, (char *)&opData, sizeof(union OpAcceptCreditData));
763 1 : CHK_PRT_RETURN(ret != 0, hccp_err("[set][ra_hdc_socket]ra hdc message process failed, ret(%d) phyId(%u)",
764 : ret, phyId), ret);
765 :
766 0 : return ret;
767 : }
|