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 <errno.h>
12 : #include <stdio.h>
13 : #include <stdlib.h>
14 : #include <string.h>
15 : #include "securec.h"
16 : #include "user_log.h"
17 : #include "dl_netco_function.h"
18 : #include "file_opt.h"
19 : #include "ra_rs_err.h"
20 : #include "network_comm.h"
21 : #include "rs_adp_nslb.h"
22 :
23 0 : STATIC int RsGetNetcoCfg(unsigned int phyId, NetCoIpPortArg *netcoArg)
24 : {
25 0 : char cfgVal[CFG_VAL_LEN] = {0};
26 0 : bool nslbSupport = false;
27 0 : int ret = 0;
28 :
29 0 : ret = FileReadCfg(NETCO_CFGFILE_PATH, (int)phyId, "udp_port_mode", cfgVal, CFG_VAL_LEN);
30 : // file not exist or item not found, degrade log level
31 0 : CHK_PRT_RETURN(ret == FILE_OPT_INNER_PARAM_ERR || ret == FILE_OPT_SYS_RD_FILE_NOT_FOUND,
32 : hccp_run_warn("file_read_cfg udp_port_mode unsuccessful, ret(%d)", ret), -ENOTSUPP);
33 0 : CHK_PRT_RETURN(ret != 0, hccp_err("file_read_cfg udp_port_mode failed, ret(%d)", ret), ret);
34 :
35 0 : nslbSupport = (strncmp(cfgVal, "nslb_dp", strlen("nslb_dp") + 1) == 0) ? true : false;
36 0 : CHK_PRT_RETURN(!nslbSupport, hccp_run_warn("phy_id(%u) not support nslb", phyId), -ENOTSUPP);
37 :
38 0 : (void)memset_s(cfgVal, CFG_VAL_LEN, 0, CFG_VAL_LEN);
39 0 : ret = FileReadCfg(NETCO_CFGFILE_PATH, (int)phyId, "nslb_dp_listen_port", cfgVal, CFG_VAL_LEN);
40 : // file not exist or item not found, degrade log level
41 0 : CHK_PRT_RETURN(ret == FILE_OPT_INNER_PARAM_ERR || ret == FILE_OPT_SYS_RD_FILE_NOT_FOUND,
42 : hccp_run_warn("file_read_cfg nslb_dp_listen_port unsuccessful, ret(%d)", ret), -ENOTSUPP);
43 0 : CHK_PRT_RETURN(ret != 0, hccp_err("file_read_cfg nslb_dp_listen_port failed, ret(%d)", ret), ret);
44 :
45 0 : netcoArg->listenPort = (unsigned short)strtoul(cfgVal, NULL, NETCO_PORT_NUM_BASE);
46 0 : netcoArg->gatewayPort = netcoArg->listenPort;
47 0 : return 0;
48 : }
49 :
50 0 : STATIC int RsNetcoInitArg(unsigned int phyId, NetCoIpPortArg *netcoArg)
51 : {
52 0 : struct IfaddrInfo ifaddrInfos = {0};
53 0 : unsigned int gwAddr = 0;
54 0 : unsigned int num = 1;
55 0 : int ret = 0;
56 :
57 0 : ret = RsGetNetcoCfg(phyId, netcoArg);
58 0 : CHK_PRT_RETURN(ret == -ENOTSUPP, hccp_run_warn("get netco cfg unsuccessful, ret(%d) phyId(%u)", ret, phyId), ret);
59 0 : CHK_PRT_RETURN(ret != 0, hccp_err("get netco cfg failed, ret(%d) phyId(%u)", ret, phyId), ret);
60 :
61 0 : ret = RsGetIfaddrs(&ifaddrInfos, &num, phyId);
62 0 : CHK_PRT_RETURN(ret != 0 || num != 1, hccp_err("rs get ifaddr failed, ret(%d) or num(%u) != 1", ret, num), -EINVAL);
63 0 : netcoArg->localIp = ntohl(ifaddrInfos.ip.addr.s_addr);
64 :
65 0 : ret = NetGetGatewayAddress(phyId, &gwAddr);
66 0 : CHK_PRT_RETURN(ret != 0, hccp_err("rs get gateway failed ret %d", ret), ret);
67 0 : netcoArg->gatewayIp = ntohl(gwAddr);
68 :
69 : // 0 indicates port will be assigned randomly
70 0 : netcoArg->localNetPort = htons(0);
71 0 : return 0;
72 : }
73 :
74 1 : STATIC int RsNslbNetcoInit(unsigned int phyId, struct RsNslbCb *nslbCb)
75 : {
76 1 : NetCoIpPortArg netcoArg = {0};
77 1 : struct rs_cb *rsCb = NULL;
78 1 : void *netcoCb = NULL;
79 1 : int ret = 0;
80 :
81 1 : ret = RsNetcoInitArg(phyId, &netcoArg);
82 1 : CHK_PRT_RETURN(ret == -ENOTSUPP, hccp_warn("get netco init arg unsuccessful, ret(%d)", ret), ret);
83 1 : CHK_PRT_RETURN(ret != 0, hccp_err("get netco init arg failed, ret(%d)", ret), -EINVAL);
84 :
85 1 : ret = RsNslbApiInit();
86 1 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_nslb_api_init[%d]", ret), ret);
87 :
88 1 : ret = RsGetRsCb(phyId, &rsCb);
89 1 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_get_rs_cb failed, phyId(%u) invalid, ret(%d)", phyId, ret), ret);
90 :
91 1 : netcoCb = RsNetcoInit(rsCb->connCb.epollfd, netcoArg);
92 1 : CHK_PRT_RETURN(netcoCb == NULL, hccp_err("netco init failed"), -EINVAL);
93 :
94 1 : ret = pthread_mutex_init(&nslbCb->mutex, NULL);
95 1 : CHK_PRT_RETURN(ret != 0, hccp_err("rs_nslb mutex_init failed, phyId(%u), ret(%d)", phyId, ret), ret);
96 :
97 1 : nslbCb->netcoCb = netcoCb;
98 1 : nslbCb->initFlag = true;
99 1 : return 0;
100 : }
101 :
102 1 : STATIC void RsNslbNetcoDeinit(struct RsNslbCb *nslbCb)
103 : {
104 1 : RS_PTHREAD_MUTEX_LOCK(&nslbCb->mutex);
105 1 : RsNetcoDeinit(nslbCb->netcoCb);
106 1 : nslbCb->initFlag = false;
107 1 : nslbCb->netcoCb = NULL;
108 :
109 1 : RsNslbApiDeinit();
110 1 : RS_PTHREAD_MUTEX_ULOCK(&nslbCb->mutex);
111 1 : pthread_mutex_destroy(&nslbCb->mutex);
112 1 : return;
113 : }
114 :
115 1 : STATIC int RsNslbNetcoTblRequest(struct RsNslbCb *nslbCb, unsigned int type, char *data, unsigned int dataLen)
116 : {
117 1 : int ret = 0;
118 :
119 1 : RS_PTHREAD_MUTEX_LOCK(&nslbCb->mutex);
120 1 : ret = RsNetcoTblAddUpd(nslbCb->netcoCb, type, data, dataLen);
121 1 : RS_PTHREAD_MUTEX_ULOCK(&nslbCb->mutex);
122 :
123 1 : return ret;
124 : }
125 :
126 1 : int RsNslbNetcoRequest(unsigned int phyId, struct RsNslbCb *nslbCb, unsigned int type, char *data, unsigned int dataLen)
127 : {
128 1 : int ret = 0;
129 :
130 1 : switch (type) {
131 0 : case NETCO_REQ_TYPE_INIT:
132 0 : ret = RsNslbNetcoInit(phyId, nslbCb);
133 0 : CHK_PRT_RETURN(ret == -ENOTSUPP, hccp_warn("netco init unsuccessful ret(%d)", ret), -ENOTSUPP);
134 0 : break;
135 0 : case NETCO_REQ_TYPE_DEINIT:
136 0 : RsNslbNetcoDeinit(nslbCb);
137 0 : break;
138 1 : default:
139 1 : ret = RsNslbNetcoTblRequest(nslbCb, type, data, dataLen);
140 1 : break;
141 : }
142 :
143 1 : ret = (ret > 0) ? -ret : ret;
144 1 : CHK_PRT_RETURN(ret != 0, hccp_err("netco request failed, type(%u) ret(%d)", type, ret), ret);
145 :
146 1 : return 0;
147 : }
148 :
149 2 : int RsEpollNslbEventHandle(struct RsNslbCb *nslbCb, int fd, unsigned int events)
150 : {
151 2 : unsigned int ret = 0;
152 2 : int retVal = 0;
153 :
154 : // netco is not initialized, no epoll event need to handle
155 2 : if (!nslbCb->initFlag) {
156 0 : return -ENODEV;
157 : }
158 :
159 2 : RS_PTHREAD_MUTEX_LOCK(&nslbCb->mutex);
160 2 : ret = RsNetcoEventDispatch(nslbCb->netcoCb, fd, events);
161 2 : retVal = (ret == NET_CO_PROCED) ? (int)ret : -ENODEV;
162 2 : RS_PTHREAD_MUTEX_ULOCK(&nslbCb->mutex);
163 2 : return retVal;
164 : }
|