LCOV - code coverage report
Current view: top level - base_comm/resources/hccp/rdma_agent/hdc - ra_hdc_socket.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.3 % 407 343
Test Date: 2026-08-18 17:47:01 Functions: 96.0 % 25 24

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

Generated by: LCOV version 2.0-1