• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "res.h"
17 #include "spunge.h"
18 #include "spunge_core.h"
19 #include "socket_common.h"
20 #include "fillp_flow_control.h"
21 #include "net.h"
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
NetconnFreeOsSocket(struct SockOsSocket * osSock,struct SpungeInstance * curInst)27 static void NetconnFreeOsSocket(struct SockOsSocket *osSock, struct SpungeInstance *curInst)
28 {
29     if ((osSock == FILLP_NULL_PTR) || (curInst == FILLP_NULL_PTR)) {
30         /* No need to prin the error log in this case, because the param 'ftSock->osSocket'
31            can be set to NULL only in this function below, so printing log here is
32            not useful
33         */
34         return;
35     }
36 
37     osSock->reference--;
38     if (osSock->reference <= 0) {
39         if (OS_SOCK_OPS_FUNC_VALID(osSock, destroySysIoSocket)) {
40             (void)osSock->ioSock->ops->destroySysIoSocket(osSock->ioSock);
41         }
42         HlistDelete(&curInst->osSockist, &osSock->osListNode);
43         SpungeFree(osSock, SPUNGE_ALLOC_TYPE_CALLOC);
44     }
45     return;
46 }
47 
48 
NetconnSetSock(struct FtSocket * sock,struct FtNetconn * conn)49 void NetconnSetSock(struct FtSocket *sock, struct FtNetconn *conn)
50 {
51     sock->netconn = conn;
52     conn->sock = (void *)sock;
53     return;
54 }
55 
NetconnSetSendCacheSize(struct FtNetconn * conn,FILLP_UINT32 cacheSize)56 void NetconnSetSendCacheSize(struct FtNetconn *conn, FILLP_UINT32 cacheSize)
57 {
58     SpungePcbSetSendCacheSize(conn->pcb, cacheSize);
59 }
NetconnSetRecvCacheSize(struct FtNetconn * conn,FILLP_UINT32 cacheSize)60 void NetconnSetRecvCacheSize(struct FtNetconn *conn, FILLP_UINT32 cacheSize)
61 {
62     SpungePcbSetRecvCacheSize(conn->pcb, cacheSize);
63 }
NetconnSetPktSize(struct FtNetconn * conn,FILLP_UINT32 pktSize)64 void NetconnSetPktSize(struct FtNetconn *conn, FILLP_UINT32 pktSize)
65 {
66     SpungePcbSetPktSize(conn->pcb, pktSize);
67 }
68 
NetconnSetOpersiteRate(struct FtNetconn * conn,FILLP_UINT32 rate)69 void NetconnSetOpersiteRate(struct FtNetconn *conn, FILLP_UINT32 rate)
70 {
71     SpungePcbSetOppositeRate(conn->pcb, rate);
72 }
73 
NetconnSetSlowStart(struct FtNetconn * conn,FILLP_BOOL slowStart)74 void NetconnSetSlowStart(struct FtNetconn *conn, FILLP_BOOL slowStart)
75 {
76     SpungePcbSetSlowStart(conn->pcb, slowStart);
77 }
78 
NetconnSetPackInterval(struct FtNetconn * conn,FILLP_UINT32 interval)79 void NetconnSetPackInterval(struct FtNetconn *conn, FILLP_UINT32 interval)
80 {
81     SpungePcbSetPackInterval(conn->pcb, interval);
82 }
83 
NetconnSetLocalPort(struct FtNetconn * conn,FILLP_INT port)84 void NetconnSetLocalPort(struct FtNetconn *conn, FILLP_INT port)
85 {
86     SpungePcbSetLocalPort(conn->pcb, port);
87 }
88 
NetconnSetAddrType(struct FtNetconn * conn,FILLP_UINT16 addrType)89 void NetconnSetAddrType(struct FtNetconn *conn, FILLP_UINT16 addrType)
90 {
91     SpungePcbSetAddrType(conn->pcb, addrType);
92 }
93 
NetconnSetDirectlySend(struct FtNetconn * conn,FILLP_INT directlySend)94 void NetconnSetDirectlySend(struct FtNetconn *conn, FILLP_INT directlySend)
95 {
96     SpungePcbSetDirectlySend(conn->pcb, directlySend);
97 }
98 
NetconnSetConnectTimeout(struct FtNetconn * conn,FILLP_LLONG timeoutUs)99 void NetconnSetConnectTimeout(struct FtNetconn *conn, FILLP_LLONG timeoutUs)
100 {
101     conn->connTimeout = SYS_ARCH_GET_CUR_TIME_LONGLONG() + timeoutUs;
102 }
103 
NetconnIsConnectTimeout(struct FtNetconn * conn)104 FILLP_BOOL NetconnIsConnectTimeout(struct FtNetconn *conn)
105 {
106     return conn->connTimeout <= SYS_ARCH_GET_CUR_TIME_LONGLONG();
107 }
108 
FillpNetconnAlloc(FILLP_UINT16 domain,struct SpungeInstance * inst)109 struct FtNetconn *FillpNetconnAlloc(FILLP_UINT16 domain, struct SpungeInstance *inst)
110 {
111     struct FtNetconn *conn = FILLP_NULL_PTR;
112     FILLP_INT ret;
113 
114     FILLP_UNUSED_PARA(domain);
115 
116     ret = DympAlloc(g_spunge->netPool, (void **)&conn, FILLP_FALSE);
117     if (conn == FILLP_NULL_PTR) {
118         FILLP_LOGERR("Failed to allocate the netconn connection, Ret=%d", ret);
119         return FILLP_NULL_PTR;
120     }
121 
122     (void)memset_s(conn, sizeof(struct FtNetconn), 0, sizeof(struct FtNetconn));
123 
124     FillpNetconnSetState(conn, CONN_STATE_IDLE);
125 
126     conn->pcb = SpungePcbNew(conn, inst);
127     if (conn->pcb == FILLP_NULL_PTR) {
128         FILLP_LOGERR("alloc spunge_pcb fail");
129 
130         DympFree(conn);
131         return FILLP_NULL_PTR;
132     }
133 
134     conn->pcb->conn = conn;
135 
136     conn->clientFourHandshakeState = FILLP_CLIENT_FOUR_HANDSHAKE_STATE_INITIAL;
137 
138     conn->closeSet = 0;   /* Application calls close() function */
139     conn->shutdownRdSet = 0; /* Application called shutdown(sock, RD) */
140     conn->shutdownWrSet = 0;   /* Application called shutdown(sock, WR) */
141     conn->peerRdSet = 0;       /* Peer notify that it won't read anything */
142     conn->peerWrSet = 0;       /* Peer notify that it won't send anything */
143     conn->sendBufRunOut = 0;    /* Send buffer has run out */
144     conn->flagsReverse = 0;
145     conn->calcRttDuringConnect = 0;
146 #ifdef FILLP_LINUX
147     conn->iovCount = 0;
148 #endif
149     return conn;
150 }
151 
FillpNetconnDestroy(struct FtNetconn * conn)152 void FillpNetconnDestroy(struct FtNetconn *conn)
153 {
154     int i;
155     if (conn == FILLP_NULL_PTR) {
156         FILLP_LOGERR("FillpNetconnDestroy: Invalid paramaters passed\r\n");
157 
158         return;
159     }
160 
161     FillpDisableConnRetryCheckTimer(&conn->pcb->fpcb);
162 
163     conn->clientFourHandshakeState = FILLP_CLIENT_FOUR_HANDSHAKE_STATE_INITIAL;
164 
165     SpungePcbRemove(conn->pcb);
166     conn->pcb = FILLP_NULL_PTR;
167 
168     for (i = 0; i < MAX_SPUNGEINSTANCE_NUM; i++) {
169         if (conn->osSocket[i] != FILLP_NULL_PTR) {
170             NetconnFreeOsSocket(conn->osSocket[i], SPUNGE_GET_CUR_INSTANCE());
171             conn->osSocket[i] = FILLP_NULL_PTR;
172         }
173     }
174 
175     DympFree(conn);
176     return;
177 }
178 
FillpErrIsFatal(FILLP_INT err)179 static FILLP_BOOL FillpErrIsFatal(FILLP_INT err)
180 {
181     FILLP_BOOL isFatal;
182     switch (err) {
183         case ERR_OK:
184         case FILLP_ERR_ISCONN:
185         case FILLP_ERR_EALREADY:
186         case ERR_NOBUFS:
187         case ERR_EINPROGRESS:
188         case ERR_CONN_TIMEOUT:
189         case ERR_NORES:
190             isFatal = FILLP_FALSE;
191             break;
192         default:
193             isFatal = FILLP_TRUE;
194             break;
195     }
196 
197     return isFatal;
198 }
199 
FillpErrToErrno(FILLP_INT err)200 FILLP_INT FillpErrToErrno(FILLP_INT err)
201 {
202     switch (err) {
203         case ERR_OK:
204             return FILLP_OK;
205         case FILLP_ERR_ISCONN:
206             return FILLP_EISCONN;
207         case FILLP_ERR_EALREADY:
208             return FILLP_EALREADY;
209         case ERR_NOBUFS:
210             return FILLP_ENOBUFS;
211         case ERR_EINPROGRESS:
212             return FILLP_EINPROGRESS;
213         case ERR_PARAM:
214             return FILLP_EINVAL;
215         case ERR_CONN_TIMEOUT:
216             return FILLP_ETIMEDOUT;
217         case ERR_WRONGSTATE:
218             return FILLP_EINVAL;
219         case ERR_NO_SOCK:
220             return FILLP_ENOTSOCK;
221         case ERR_FAILURE:
222             return FILLP_EFAULT;
223         case ERR_NORES:
224             return FILLP_ENOMEM;
225         case ERR_NO_SYS_SOCK:
226             return FILLP_EFAULT;
227         case ERR_NO_REBIND:
228             return FILLP_EINVAL;
229         case ERR_SOCK_BIND:
230             return FILLP_EINVAL;
231         case ERR_REMOTE_REJECT_OR_CLOSE:
232             return FILLP_ECONNRESET;
233         case ERR_CONNREFUSED:
234             return FILLP_ECONNREFUSED;
235         default:
236             return FILLP_EFAULT;
237     }
238 }
239 
FillpNetconnSetSafeErr(struct FtNetconn * conn,FILLP_INT err)240 void FillpNetconnSetSafeErr(struct FtNetconn *conn, FILLP_INT err)
241 {
242     if (!FillpErrIsFatal(conn->lastErr)) {
243         conn->lastErr = err;
244     }
245 }
246 
FillpNetconnSetState(struct FtNetconn * conn,FILLP_UINT8 state)247 void FillpNetconnSetState(struct FtNetconn *conn, FILLP_UINT8 state)
248 {
249     conn->state = state;
250     FILLP_LOGINF("Set conn state:%u", state);
251 
252     if (state == CONN_STATE_CONNECTED) {
253         FillpEnablePackTimer(&conn->pcb->fpcb);
254         FillpEnableFcTimer(&conn->pcb->fpcb);
255 
256         FillpEnableKeepAliveTimer(&conn->pcb->fpcb);
257         conn->pcb->fpcb.statistics.keepAlive.lastRecvTime = conn->pcb->fpcb.pcbInst->curTime;
258 
259         if (g_resource.common.outOfOrderCacheEnable &&
260             (g_appResource.common.enableNackDelay == FILLP_FALSE)) {
261             conn->pcb->fpcb.dataBurstTimerNode.interval =
262                 (FILLP_UINT32)(g_resource.common.recvCachePktNumBufferTimeout * FILLP_ONE_SECOND);
263             FillpEnableDataBurstTimer(&conn->pcb->fpcb);
264         }
265 
266         SpungeTokenBucketAddFpcb(&conn->pcb->fpcb);
267     }
268 
269     if (state == CONN_STATE_CLOSED) {
270         if (conn->pcb != FILLP_NULL_PTR) {
271             SpungeTokenBucketDelFpcb(&conn->pcb->fpcb);
272         }
273     }
274     return;
275 }
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 
281