1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci
16060ff233Sopenharmony_ci#ifndef FILLP_SOCKETS_H
17060ff233Sopenharmony_ci#define FILLP_SOCKETS_H
18060ff233Sopenharmony_ci#include "opt.h"
19060ff233Sopenharmony_ci#include "queue.h"
20060ff233Sopenharmony_ci#include "epoll.h"
21060ff233Sopenharmony_ci#include "hlist.h"
22060ff233Sopenharmony_ci#include "net.h"
23060ff233Sopenharmony_ci
24060ff233Sopenharmony_ci#ifdef FILLP_LINUX
25060ff233Sopenharmony_ci#include <errno.h>
26060ff233Sopenharmony_ci#elif defined(FILLP_WIN32)
27060ff233Sopenharmony_ci#include <Winsock2.h>
28060ff233Sopenharmony_ci#endif
29060ff233Sopenharmony_ci
30060ff233Sopenharmony_ci#ifdef __cplusplus
31060ff233Sopenharmony_ciextern "C" {
32060ff233Sopenharmony_ci#endif
33060ff233Sopenharmony_ci
34060ff233Sopenharmony_cienum SockAllockState {
35060ff233Sopenharmony_ci    SOCK_ALLOC_STATE_FREE,
36060ff233Sopenharmony_ci    SOCK_ALLOC_STATE_ERR,
37060ff233Sopenharmony_ci
38060ff233Sopenharmony_ci    SOCK_ALLOC_STATE_WAIT_TO_CLOSE, /* app has called SockClose */
39060ff233Sopenharmony_ci    SOCK_ALLOC_STATE_COMM,
40060ff233Sopenharmony_ci    SOCK_ALLOC_STATE_EPOLL,
41060ff233Sopenharmony_ci    SOCK_ALLOC_STATE_EPOLL_TO_CLOSE
42060ff233Sopenharmony_ci};
43060ff233Sopenharmony_ci
44060ff233Sopenharmony_cistruct GlobalAppUdpRes {
45060ff233Sopenharmony_ci    FILLP_UINT32 txBurst; /* max pkt number to send each cycle */
46060ff233Sopenharmony_ci#ifdef FILLP_64BIT_ALIGN
47060ff233Sopenharmony_ci    FILLP_UINT32 reserve;
48060ff233Sopenharmony_ci#endif
49060ff233Sopenharmony_ci};
50060ff233Sopenharmony_ci
51060ff233Sopenharmony_cistruct GlobalAppCommon {
52060ff233Sopenharmony_ci    FILLP_UINT32 keepAliveTime; /* ms */
53060ff233Sopenharmony_ci    FILLP_UINT32 maxServerAllowSendCache;
54060ff233Sopenharmony_ci    FILLP_UINT32 maxServerAllowRecvCache;
55060ff233Sopenharmony_ci    FILLP_UINT32 udpSendBufSize;
56060ff233Sopenharmony_ci    FILLP_UINT32 recvBufSize;
57060ff233Sopenharmony_ci    FILLP_UINT32 disconnectRetryTimeout; /* Testability addition in the code it is 100. */
58060ff233Sopenharmony_ci    FILLP_UINT32 sendCache;              /* size of send cache */
59060ff233Sopenharmony_ci    FILLP_UINT32 recvCache;              /* size of recv cache  */
60060ff233Sopenharmony_ci    FILLP_UINT32 connectTimeout;         /* seconds */
61060ff233Sopenharmony_ci    FILLP_UINT16 reserve;                /* Now not used, need to remove it */
62060ff233Sopenharmony_ci    FILLP_UINT16 connRetryTimeout;       /* Testability addition in the code it is 10. */
63060ff233Sopenharmony_ci    FILLP_BOOL enableNackDelay;
64060ff233Sopenharmony_ci    FILLP_BOOL enlargePackIntervalFlag;
65060ff233Sopenharmony_ci    FILLP_BOOL enableDateOptTimestamp;
66060ff233Sopenharmony_ci    FILLP_UCHAR pad[3];
67060ff233Sopenharmony_ci    FILLP_LLONG nackDelayTimeout;
68060ff233Sopenharmony_ci    FILLP_UINT32 fcStasticsInterval;
69060ff233Sopenharmony_ci};
70060ff233Sopenharmony_ci
71060ff233Sopenharmony_cistruct GlobalAppFlowControl {
72060ff233Sopenharmony_ci    FILLP_UINT32 maxRate; /* maximum data sending rate */
73060ff233Sopenharmony_ci    FILLP_UINT32 maxRecvRate;
74060ff233Sopenharmony_ci    FILLP_UINT32 oppositeSetRate; /* Only for Server */
75060ff233Sopenharmony_ci    FILLP_UINT32 packInterval;    /* us */
76060ff233Sopenharmony_ci    FILLP_UINT16 pktSize;         /* default pkt size to cal flow rate */
77060ff233Sopenharmony_ci    FILLP_BOOL slowStart;
78060ff233Sopenharmony_ci    FILLP_BOOL constRateEnbale;
79060ff233Sopenharmony_ci    FILLP_UCHAR reserve[4];
80060ff233Sopenharmony_ci};
81060ff233Sopenharmony_ci
82060ff233Sopenharmony_cistruct GlobalAppResource {
83060ff233Sopenharmony_ci    struct GlobalAppUdpRes udp;
84060ff233Sopenharmony_ci    struct GlobalAppCommon common;
85060ff233Sopenharmony_ci    struct GlobalAppFlowControl flowControl;
86060ff233Sopenharmony_ci};
87060ff233Sopenharmony_ci
88060ff233Sopenharmony_ci#define MAX_SPUNGE_TYPE_NUM 24 /* Check with SpungeMsgType it shopuld be more than max elements in this enum */
89060ff233Sopenharmony_ci
90060ff233Sopenharmony_cistruct FtSocket {
91060ff233Sopenharmony_ci    FILLP_INT index;      /* index of table */
92060ff233Sopenharmony_ci    FILLP_INT allocState; /* socket has been allocState */
93060ff233Sopenharmony_ci    struct FtNetconn *netconn;
94060ff233Sopenharmony_ci    /* These following members are used for connection and referenced by FtNetconn */
95060ff233Sopenharmony_ci    FILLP_INT coreErrType[MAX_SPUNGE_TYPE_NUM];
96060ff233Sopenharmony_ci
97060ff233Sopenharmony_ci    void *recvPktBuf;
98060ff233Sopenharmony_ci    struct SpungeInstance *inst;
99060ff233Sopenharmony_ci    void *traceHandle; /* Handle provided by FillpTrace callback */
100060ff233Sopenharmony_ci
101060ff233Sopenharmony_ci    struct HlistNode listenNode;
102060ff233Sopenharmony_ci    SYS_ARCH_SEM acceptSem;
103060ff233Sopenharmony_ci    FillpQueue *acceptBox;
104060ff233Sopenharmony_ci    FILLP_INT listenBacklog;
105060ff233Sopenharmony_ci
106060ff233Sopenharmony_ci    FILLP_UINT32 errEvent;
107060ff233Sopenharmony_ci    struct EventPoll *eventEpoll;
108060ff233Sopenharmony_ci    SysArchAtomic rcvEvent;
109060ff233Sopenharmony_ci    SysArchAtomic sendEvent;
110060ff233Sopenharmony_ci    SysArchAtomic sendEventCount;
111060ff233Sopenharmony_ci    SysArchAtomic epollWaiting;
112060ff233Sopenharmony_ci
113060ff233Sopenharmony_ci    struct Hlist epTaskList;
114060ff233Sopenharmony_ci    SYS_ARCH_SEM epollTaskListLock;
115060ff233Sopenharmony_ci
116060ff233Sopenharmony_ci    /* It means, that A ft-socket can be registered up to 10 epoll instances, not
117060ff233Sopenharmony_ci       more than that. This value is compile config controlled, App can
118060ff233Sopenharmony_ci       increase the number if expects more epoll instances for its user application
119060ff233Sopenharmony_ci    */
120060ff233Sopenharmony_ci    FILLP_INT associatedEpollInstanceArr[FILLP_NUM_OF_EPOLL_INSTANCE_SUPPORTED];
121060ff233Sopenharmony_ci    FILLP_UINT32 associatedEpollInstanceIdx;
122060ff233Sopenharmony_ci
123060ff233Sopenharmony_ci    FILLP_UINT32 offset;
124060ff233Sopenharmony_ci    FILLP_UINT16 dataOptionFlag;
125060ff233Sopenharmony_ci    FILLP_UINT16 dataOptionSize;
126060ff233Sopenharmony_ci    FILLP_LLONG jitter;
127060ff233Sopenharmony_ci    FILLP_LLONG transmit;
128060ff233Sopenharmony_ci
129060ff233Sopenharmony_ci    FILLP_UINT16 flags;
130060ff233Sopenharmony_ci    FILLP_UINT16 sockAddrType;
131060ff233Sopenharmony_ci    FILLP_INT socketType;     // get from SockSocket
132060ff233Sopenharmony_ci    FILLP_INT socketProtocol; // get from SockSocket
133060ff233Sopenharmony_ci
134060ff233Sopenharmony_ci    FILLP_BOOL isListenSock;
135060ff233Sopenharmony_ci    FILLP_BOOL isSockBind;
136060ff233Sopenharmony_ci    FILLP_BOOL lingering;
137060ff233Sopenharmony_ci    FILLP_UINT8 traceFlag; /* Flag for enable indication User/Network */
138060ff233Sopenharmony_ci    FILLP_INT freeTimeCount;
139060ff233Sopenharmony_ci    FILLP_INT err;
140060ff233Sopenharmony_ci
141060ff233Sopenharmony_ci    SYS_ARCH_SEM connBlockSem;     /* Used when do connect */
142060ff233Sopenharmony_ci    SYS_ARCH_RW_SEM sockConnSem;   /* Used to protect socket resource not freed */
143060ff233Sopenharmony_ci    SYS_ARCH_SEM sockCloseProtect; /* To make sure that only one close message posted to fillp thread */
144060ff233Sopenharmony_ci    struct GlobalAppResource resConf; /* Total size is 15 * sizeof uint32 */
145060ff233Sopenharmony_ci    struct linger fillpLinger;
146060ff233Sopenharmony_ci    FILLP_INT directlySend; /* directly send packet in the app thread instead of in the main thread */
147060ff233Sopenharmony_ci};
148060ff233Sopenharmony_ci
149060ff233Sopenharmony_ci#define FILLP_SOCK_SET_ERR(sk, e) ((sk)->err = (e))
150060ff233Sopenharmony_ci
151060ff233Sopenharmony_cistatic __inline struct FtSocket *SockEntryListenSocket(struct HlistNode *node)
152060ff233Sopenharmony_ci{
153060ff233Sopenharmony_ci    return (struct FtSocket *)((char *)(node) - (uintptr_t)(&(((struct FtSocket *)0)->listenNode)));
154060ff233Sopenharmony_ci}
155060ff233Sopenharmony_ci
156060ff233Sopenharmony_ci#define SOCK_GET_SENDPKTPOOL(_sock) ((_sock)->netconn->pcb->fpcb.send.itemPool)
157060ff233Sopenharmony_ci#define SOCK_GET_SENDBOX(_sock) ((_sock)->netconn->pcb->fpcb.send.unsendBox)
158060ff233Sopenharmony_ci#define SOCK_GET_RECVBOX(_sock) ((_sock)->netconn->pcb->fpcb.recv.recvBox)
159060ff233Sopenharmony_ci#define SOCK_GET_PKTSIZE(_sock) ((_sock)->netconn->pcb->fpcb.pktSize)
160060ff233Sopenharmony_ci
161060ff233Sopenharmony_ci#define SOCK_CONN_TRY_RDLOCK(_sock) SYS_ARCH_RWSEM_TRYRDWAIT(&(_sock)->sockConnSem)
162060ff233Sopenharmony_ci#define SOCK_CONN_UNLOCK_RD(_sock) SYS_ARCH_RWSEM_RDPOST(&(_sock)->sockConnSem)
163060ff233Sopenharmony_ci
164060ff233Sopenharmony_ci#define SOCK_CONN_TRY_LOCK_CLOSE(_sock) SYS_ARCH_SEM_TRYWAIT(&(_sock)->sockCloseProtect)
165060ff233Sopenharmony_ci#define SOCK_CONN_UNLOCK_CLOSE(_sock) SYS_ARCH_SEM_POST(&(_sock)->sockCloseProtect)
166060ff233Sopenharmony_ci
167060ff233Sopenharmony_ci#define SOCK_GET_SENDSEM(_sock) ((_sock)->netconn->pcb->fpcb.send.sendSem)
168060ff233Sopenharmony_ci#define SOCK_GET_RECVSEM(_sock) ((_sock)->netconn->pcb->fpcb.recv.recvSem)
169060ff233Sopenharmony_ci
170060ff233Sopenharmony_ci#define SOCK_WAIT_SENDSEM(_sock) SYS_ARCH_SEM_WAIT(&SOCK_GET_SENDSEM(_sock))
171060ff233Sopenharmony_ci#define SOCK_POST_SENDSEM(_sock) SYS_ARCH_SEM_POST(&SOCK_GET_SENDSEM(_sock))
172060ff233Sopenharmony_ci#define SOCK_TRYWAIT_SENDSEM(_sock) SYS_ARCH_SEM_TRYWAIT(&SOCK_GET_SENDSEM(_sock))
173060ff233Sopenharmony_ci
174060ff233Sopenharmony_ci#define SOCK_WAIT_RECVSEM(_sock) SYS_ARCH_SEM_WAIT(&SOCK_GET_RECVSEM(_sock))
175060ff233Sopenharmony_ci#define SOCK_POST_RECVSEM(_sock) SYS_ARCH_SEM_POST(&SOCK_GET_RECVSEM(_sock))
176060ff233Sopenharmony_ci#define SOCK_TRYWAIT_RECVSEM(_sock) SYS_ARCH_SEM_TRYWAIT(&SOCK_GET_RECVSEM(_sock))
177060ff233Sopenharmony_ci
178060ff233Sopenharmony_ci#ifdef FILLP_LINUX
179060ff233Sopenharmony_ci#if defined(FILLP_LW_LITEOS)
180060ff233Sopenharmony_ci#define SOCK_SEND_CPU_PAUSE() FILLP_SLEEP_MS(10)
181060ff233Sopenharmony_ci#else
182060ff233Sopenharmony_ci#define SOCK_SEND_CPU_PAUSE() (void)FILLP_USLEEP(FILLP_CPU_PAUSE_TIME)
183060ff233Sopenharmony_ci#endif
184060ff233Sopenharmony_ci#else
185060ff233Sopenharmony_ci#define SOCK_SEND_CPU_PAUSE() FILLP_SLEEP_MS(1)
186060ff233Sopenharmony_ci#endif
187060ff233Sopenharmony_ci
188060ff233Sopenharmony_ci#if defined(FILLP_LW_LITEOS)
189060ff233Sopenharmony_ci#define SOCK_RECV_CPU_PAUSE() FILLP_SLEEP_MS(10)
190060ff233Sopenharmony_ci#else
191060ff233Sopenharmony_ci#define SOCK_RECV_CPU_PAUSE() FILLP_SLEEP_MS(1)
192060ff233Sopenharmony_ci#endif
193060ff233Sopenharmony_ci
194060ff233Sopenharmony_ci#ifdef FILLP_LINUX
195060ff233Sopenharmony_ci#define EPOLL_CPU_PAUSE() (void)FILLP_USLEEP(FILLP_CPU_PAUSE_TIME)
196060ff233Sopenharmony_ci#else
197060ff233Sopenharmony_ci#define EPOLL_CPU_PAUSE() FILLP_SLEEP_MS(1)
198060ff233Sopenharmony_ci#endif
199060ff233Sopenharmony_ci
200060ff233Sopenharmony_cistruct FtSocketTable {
201060ff233Sopenharmony_ci    FillpQueue *freeQueqe;
202060ff233Sopenharmony_ci    struct FtSocket **sockPool;
203060ff233Sopenharmony_ci    FILLP_INT size;
204060ff233Sopenharmony_ci    SysArchAtomic used;
205060ff233Sopenharmony_ci};
206060ff233Sopenharmony_ci
207060ff233Sopenharmony_ci#ifdef FILLP_LINUX
208060ff233Sopenharmony_ci#define SET_ERRNO(_errno) (errno = (_errno))
209060ff233Sopenharmony_ci#elif defined(FILLP_WIN32)
210060ff233Sopenharmony_ci#define SET_ERRNO(_errno) WSASetLastError(_errno)
211060ff233Sopenharmony_ci#endif
212060ff233Sopenharmony_ci
213060ff233Sopenharmony_ci#ifdef FILLP_LINUX
214060ff233Sopenharmony_ci#define FT_OS_GET_ERRNO errno
215060ff233Sopenharmony_ci#elif defined(FILLP_WIN32)
216060ff233Sopenharmony_ci#define FT_OS_GET_ERRNO WSAGetLastError()
217060ff233Sopenharmony_ci#endif
218060ff233Sopenharmony_ci
219060ff233Sopenharmony_ci/* Should this netconn avoid blocking? */
220060ff233Sopenharmony_ci#define SOCK_FLAG_NON_BLOCKING 0x0001
221060ff233Sopenharmony_ci
222060ff233Sopenharmony_ci/* Get the blocking status of netconn calls (@todo: write/send is missing) */
223060ff233Sopenharmony_ci#define SOCK_IS_NONBLOCKING(sock) (((sock)->flags & SOCK_FLAG_NON_BLOCKING) != 0)
224060ff233Sopenharmony_ci#define SOCK_IS_BLOCKING(sock) (((sock)->flags & SOCK_FLAG_NON_BLOCKING) == 0)
225060ff233Sopenharmony_ci
226060ff233Sopenharmony_ci/* Set the blocking status of FtSocket
227060ff233Sopenharmony_ci
228060ff233Sopenharmony_ci    The flag (which contains the socket options is in FtSocket and NOT in netconn CB)
229060ff233Sopenharmony_ci    is in FtSocket structure, this is because: Application can set the socket to
230060ff233Sopenharmony_ci    nonblock just after calling FtSocket (before ft_connet/FtAccept), but the
231060ff233Sopenharmony_ci    netconn CB will be available only during FtConnect/FtAccept.
232060ff233Sopenharmony_ci*/
233060ff233Sopenharmony_civoid SockSetNonblocking(struct FtSocket *sock, FILLP_INT val);
234060ff233Sopenharmony_ci
235060ff233Sopenharmony_cistruct NackDelayCfg {
236060ff233Sopenharmony_ci    FILLP_INT sockIndex;
237060ff233Sopenharmony_ci    FILLP_UINT32 nackCfgVal;
238060ff233Sopenharmony_ci    FILLP_LLONG nackDelayTimeout;
239060ff233Sopenharmony_ci};
240060ff233Sopenharmony_ci
241060ff233Sopenharmony_ciFILLP_INT SysArchSetSockRcvbuf(FILLP_INT sock, FILLP_UINT size);
242060ff233Sopenharmony_ciFILLP_INT SysArchSetSockSndbuf(FILLP_INT sock, FILLP_UINT size);
243060ff233Sopenharmony_ciFILLP_INT SysArchSetSockBlocking(FILLP_INT sock, FILLP_BOOL blocking);
244060ff233Sopenharmony_ciFILLP_INT SysSetThreadName(FILLP_CHAR *name, FILLP_UINT16 nameLen);
245060ff233Sopenharmony_ci
246060ff233Sopenharmony_ci#ifdef __cplusplus
247060ff233Sopenharmony_ci}
248060ff233Sopenharmony_ci#endif
249060ff233Sopenharmony_ci
250060ff233Sopenharmony_ci#endif /* FILLP_SOCKETS_H */