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 #ifndef SPUNGE_H
17 #define SPUNGE_H
18 #include "sockets.h"
19 #include "lf_ring.h"
20 #include "queue.h"
21 #include "hlist.h"
22 #include "log.h"
23 #include "dympool.h"
24 #include "fillp_cookie.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define UDP_HASH_TABLE_SIZE 128
31 
32 #define FILLP_ETH_DEVICE_SIZE 16
33 
34 #define FILLP_MAC_ADDRESS_SIZE 6
35 
36 #define FILLP_INST_UNSEND_BOX_NUM 1
37 
38 #define FILLP_INST_WIFI_INFO_NUM 4
39 
40 #define FILLP_EPOLL_ITEM_INIT_NUM 5
41 #define FILLP_MSG_ITEM_INIT_NUM 10
42 #define FILLP_CONN_ITEM_INIT_NUM 5
43 
44 struct SpungeResConf {
45     FILLP_UINT maxInstNum;
46     FILLP_UINT maxSockNum;
47     FILLP_UINT maxTimerItemNum;
48     FILLP_UINT maxMsgItemNum;
49     FILLP_UINT maxConnNum;
50     FILLP_UINT maxEpollItemNum;
51     FILLP_UINT maxEpollEventNum;
52 };
53 
54 struct SpungePcbList {
55     struct Hlist list;
56 };
57 
58 struct SpungePcbRes {
59     struct SpungePcbList list;
60 };
61 
62 struct SpungePcbhashbucket {
63     struct Hlist list;
64 };
65 
66 struct SpungeServerRateControlItem {
67     FILLP_INT totalWeight;
68     FILLP_UINT32 maxRate;
69 };
70 
71 struct SpungeServerRateControl {
72     FILLP_LLONG lastControlTime;
73     FILLP_INT connectionNum;
74     FILLP_CHAR pad[4];
75     struct SpungeServerRateControlItem send;
76     struct SpungeServerRateControlItem recv;
77 };
78 
79 struct SpungeTokenBucke {
80     FILLP_LLONG lastTime;
81     FILLP_UINT32 rate;         /* kpbs */
82     FILLP_UINT32 tokenCount;   /* bytes */
83     FILLP_UINT32 maxPktSize;   /* bytes */
84     FILLP_ULLONG waitPktCount; /* pkt */
85     struct Hlist tbFpcbLists;
86     struct HlistNode *fpcbCur;
87     struct SpungeInstance *inst;
88     struct FillpTimingWheelTimerNode tockenTimerNode;
89 };
90 
91 struct SpungeInstance {
92     FILLP_LLONG curTime;
93     FILLP_LLONG minSendInterval;
94     FillpQueue *msgBox;
95     DympoolType *msgPool;
96     struct ThreadParam mainThreadParam;
97     struct SpungePcbList pcbList; /* Recrd all connections */
98     struct Hlist osSockist;
99     struct SpungeServerRateControl rateControl;
100     struct FillpTimingWheel timingWheel;
101     FillpQueue *unsendBox[FILLP_INST_UNSEND_BOX_NUM];
102     SYS_ARCH_SEM threadSem;     /* Used when do send */
103     FILLP_BOOL thresdSemInited;
104     struct FillpPcbItem **unsendItem;
105     struct Hlist sendPcbList;
106 
107     FILLP_INT instIndex;
108     FILLP_UINT netMask;
109     FILLP_BOOL hasInited;
110     FILLP_BOOL waitTobeCoreKilled;
111     FILLP_UINT8 srcMac[FILLP_MAC_ADDRESS_SIZE];
112     FILLP_UINT8 destMac[FILLP_MAC_ADDRESS_SIZE];
113     FILLP_UINT8 cleanseDataCtr;
114     FILLP_UINT8 pad[1];
115 
116     FillpMacInfo macInfo;
117 
118     struct FillpTimingWheelTimerNode macTimerNode;
119     struct FillpTimingWheelTimerNode fairTimerNode;
120     FILLP_CHAR *tmpBuf[FILLP_VLEN];
121     struct SpungePcb tempSpcb;
122     struct SpungeTokenBucke stb;
123     SysArchAtomic msgUsingCount;
124 };
125 
126 void SpinstAddToPcbList(struct SpungeInstance *inst, struct HlistNode *node);
127 void SpinstDeleteFromPcbList(struct SpungeInstance *inst, struct HlistNode *node);
128 
129 struct Spunge {
130     struct SpungeResConf resConf;
131     FILLP_UINT insNum;
132     FILLP_BOOL hasInited;
133     FILLP_BOOL hasDeinitBlked;
134     FILLP_UINT8 traceFlag;
135     FILLP_UINT8 pad;
136     void *traceHandle;
137     struct FtSocketTable *sockTable; /* alloc socket source */
138     DympoolType *netPool;
139 
140     DympoolType *epitemPool;    /* epitem */
141     DympoolType *eventpollPool; /* eventpoll */
142 
143     struct SpungeInstance *instPool;
144 };
145 
146 extern struct Spunge *g_spunge;
147 #define SPUNGE_GET_CUR_INSTANCE() (&g_spunge->instPool[0])
148 
149 #ifdef FILLP_LINUX
150 extern FILLP_CHAR *g_ethdevice;
151 #endif
152 #define SPUNGE_STACK_VALID (g_spunge && g_spunge->hasInited)
153 
154 void SpungeEpollEventCallback(struct FtSocket *sock, FILLP_INT event, FILLP_INT count);
155 void SpungeEpollAppRecvOne(struct FtSocket *sock);
156 
157 void SockSetOsSocket(struct FtSocket *ftSock, struct SockOsSocket *osSock);
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif