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 "hccp_dl.h"
13 : #include "rs.h"
14 : #include "dl_netco_function.h"
15 :
16 : void *gNetcoApiHandle = NULL;
17 : #ifndef CA_CONFIG_LLT
18 : struct RsNetcoOps gNetcoOps;
19 : #else
20 : struct RsNetcoOps gNetcoOps = {
21 : .rsNetcoInit = NetCoInitFactory,
22 : .rsNetcoDeinit = NetCoDestruct,
23 : .rsNetcoEventDispatch = NetCoFdEventDispatch,
24 : .rsNetcoTblAddUpd = NetCoTblAddUpd,
25 : };
26 : #endif
27 :
28 1 : STATIC int RsOpenLibnetCoSo(void)
29 : {
30 : #ifndef CA_CONFIG_LLT
31 : if (gNetcoApiHandle == NULL) {
32 : gNetcoApiHandle = HccpDlopen("libnet_co.so", RTLD_NOW);
33 : if (gNetcoApiHandle != NULL) {
34 : return 0;
35 : }
36 : hccp_err("HccpDlopen[libnet_co.so] failed! errno=%d, dlerror: %s", errno, dlerror());
37 : return -EINVAL;
38 : } else {
39 : hccp_run_info("netco_api dlopen again!");
40 : }
41 : #endif
42 1 : return 0;
43 : }
44 :
45 1 : STATIC void RsCloseNetcoSo(void)
46 : {
47 : #ifndef CA_CONFIG_LLT
48 : if (gNetcoApiHandle != NULL) {
49 : (void)HccpDlclose(gNetcoApiHandle);
50 : gNetcoApiHandle = NULL;
51 : }
52 : #endif
53 1 : return;
54 : }
55 :
56 0 : STATIC int RsNetcoTblApiInit(void)
57 : {
58 : #ifndef CA_CONFIG_LLT
59 : gNetcoOps.rsNetcoInit = (void *(*)(int, NetCoIpPortArg))
60 : HccpDlsym(gNetcoApiHandle, "NetCoInitFactory");
61 : DL_API_RET_IS_NULL_CHECK(gNetcoOps.rsNetcoInit, "NetCoInitFactory");
62 :
63 : gNetcoOps.rsNetcoDeinit = (void (*)(void *))
64 : HccpDlsym(gNetcoApiHandle, "NetCoDestruct");
65 : DL_API_RET_IS_NULL_CHECK(gNetcoOps.rsNetcoDeinit, "NetCoDestruct");
66 :
67 : gNetcoOps.rsNetcoEventDispatch = (unsigned int (*)(void *, int, unsigned int))
68 : HccpDlsym(gNetcoApiHandle, "NetCoFdEventDispatch");
69 : DL_API_RET_IS_NULL_CHECK(gNetcoOps.rsNetcoEventDispatch, "NetCoFdEventDispatch");
70 :
71 : gNetcoOps.rsNetcoTblAddUpd = (int (*)(void *, unsigned int, char *, unsigned int))
72 : HccpDlsym(gNetcoApiHandle, "NetCoTblAddUpd");
73 : DL_API_RET_IS_NULL_CHECK(gNetcoOps.rsNetcoTblAddUpd, "NetCoTblAddUpd");
74 : #endif
75 0 : return 0;
76 : }
77 :
78 1 : STATIC int RsNetcoApiInit(void)
79 : {
80 : int ret;
81 1 : ret = RsOpenLibnetCoSo();
82 1 : CHK_PRT_RETURN(ret != 0, hccp_err("HccpDlopen[libnet_co.so] failed! ret=[%d],"\
83 : "Please check network adapter driver has been installed", ret), ret);
84 :
85 1 : ret = RsNetcoTblApiInit();
86 1 : if (ret != 0) {
87 1 : hccp_err("[rs_netco_tbl_api_init]HccpDlopen failed! ret=[%d]", ret);
88 1 : RsCloseNetcoSo();
89 1 : return ret;
90 : }
91 :
92 0 : return 0;
93 : }
94 :
95 0 : int RsNslbApiInit(void)
96 : {
97 : int ret;
98 :
99 0 : ret = RsNetcoApiInit();
100 0 : CHK_PRT_RETURN(ret, hccp_err("rs_netco_api_init failed! ret=[%d]", ret), ret);
101 :
102 0 : return 0;
103 : }
104 :
105 0 : void RsNslbApiDeinit(void)
106 : {
107 0 : if (gNetcoApiHandle != NULL) {
108 0 : (void)HccpDlclose(gNetcoApiHandle);
109 0 : gNetcoApiHandle = NULL;
110 : }
111 0 : return;
112 : }
113 :
114 1 : void *RsNetcoInit(int epollfd, NetCoIpPortArg ipPortArg)
115 : {
116 1 : if (gNetcoOps.rsNetcoInit == NULL) {
117 : #ifndef CA_CONFIG_LLT
118 : hccp_err("rs_netco_init is null");
119 : return NULL;
120 : #endif
121 : }
122 1 : return gNetcoOps.rsNetcoInit(epollfd, ipPortArg);
123 : }
124 :
125 0 : void RsNetcoDeinit(void *co)
126 : {
127 0 : if (gNetcoOps.rsNetcoDeinit == NULL) {
128 : #ifndef CA_CONFIG_LLT
129 : hccp_err("rs_netco_deinit is null");
130 : return;
131 : #endif
132 : }
133 0 : return gNetcoOps.rsNetcoDeinit(co);
134 : }
135 :
136 1 : unsigned int RsNetcoEventDispatch(void *co, int fd, unsigned int curEvents)
137 : {
138 1 : if (gNetcoOps.rsNetcoEventDispatch == NULL) {
139 : #ifndef CA_CONFIG_LLT
140 : hccp_err("rs_netco_event_dispatch is null");
141 : return EINVAL;
142 : #endif
143 : }
144 1 : return gNetcoOps.rsNetcoEventDispatch(co, fd, curEvents);
145 : }
146 :
147 1 : int RsNetcoTblAddUpd(void *netcoHandle, unsigned int type, char *data, unsigned int dataLen)
148 : {
149 1 : if (gNetcoOps.rsNetcoTblAddUpd == NULL) {
150 : #ifndef CA_CONFIG_LLT
151 : hccp_err("rs_netco_tbl_add_upd is null, type(%u)", type);
152 : return -EINVAL;
153 : #endif
154 : }
155 1 : return gNetcoOps.rsNetcoTblAddUpd(netcoHandle, type, data, dataLen);
156 : }
|