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 "fillp_stack_config.h"
17 #include "fillp_stack_app_config_in.h"
18 #include "fillp_stack_config_in.h"
19 #include "res.h"
20 #include "spunge.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
FtSetCopyPreinitConfigs(IN FILLP_CONST FillpGlobalPreinitExtConfigsSt * globalPreinitConfig)26 static FILLP_INT FtSetCopyPreinitConfigs(IN FILLP_CONST FillpGlobalPreinitExtConfigsSt *globalPreinitConfig)
27 {
28     if ((g_spunge != FILLP_NULL_PTR) && (g_spunge->hasInited == FILLP_TRUE)) {
29         FILLP_LOGERR("Cannot Set value after stack initialization!!!");
30         return ERR_FAILURE;
31     }
32 
33     g_resource.pktLossThresHoldMax = globalPreinitConfig->pktLossThresHoldMax;
34     g_resource.timingWheelAccuracy = globalPreinitConfig->timingWheelAccuracy;
35     g_resource.maximalAckNumLimit = globalPreinitConfig->maximalAckNumLimit;
36     g_resource.sendOneAckNum = globalPreinitConfig->sendOneAckNum;
37     g_resource.cpuPauseTime = globalPreinitConfig->cpuPauseTime;
38     g_resource.retransmitCmpTime = globalPreinitConfig->retransmitCmpTime;
39     g_resource.minRate = globalPreinitConfig->minRate;
40     g_resource.minPackInterval = globalPreinitConfig->minPackInterval;
41     g_resource.unsendBoxLoopCheckBurst = globalPreinitConfig->unsendBoxLoopCheckBurst;
42     g_resource.instUnsendBoxSize = globalPreinitConfig->instUnsendBoxSize;
43     g_resource.nackRetryLen = globalPreinitConfig->nackRetryLen;
44 
45     return FILLP_SUCCESS;
46 }
47 
FtGetCopyPreinitConfigs(IN FillpGlobalPreinitExtConfigsSt * globalPreinitConfig)48 static void FtGetCopyPreinitConfigs(IN FillpGlobalPreinitExtConfigsSt *globalPreinitConfig)
49 {
50     globalPreinitConfig->pktLossThresHoldMax = g_resource.pktLossThresHoldMax;
51     globalPreinitConfig->timingWheelAccuracy = g_resource.timingWheelAccuracy;
52     globalPreinitConfig->maximalAckNumLimit = g_resource.maximalAckNumLimit;
53     globalPreinitConfig->sendOneAckNum = g_resource.sendOneAckNum;
54     globalPreinitConfig->cpuPauseTime = g_resource.cpuPauseTime;
55     globalPreinitConfig->retransmitCmpTime = g_resource.retransmitCmpTime;
56     globalPreinitConfig->minRate = g_resource.minRate;
57     globalPreinitConfig->minPackInterval = g_resource.minPackInterval;
58     globalPreinitConfig->unsendBoxLoopCheckBurst = g_resource.unsendBoxLoopCheckBurst;
59     globalPreinitConfig->instUnsendBoxSize = g_resource.instUnsendBoxSize;
60     globalPreinitConfig->nackRetryLen = g_resource.nackRetryLen;
61 
62     return;
63 }
64 
65 
66 /*******************************************************************
67   Function      : FtInitConfigSet
68   Description   : Api is used to set all the FILLP global stack configuration. please refer FillpGlobalConfigsSt
69               for parameter details.
70   Calls         :
71   Called By     :
72   Input         : structure of type FILLP_GLOBAL_RESOURCE
73   Output        :
74   Return        : FILLP_UINT32 SUCCESS/FAILURE
75   Others        :
76 ********************************************************************/
FtInitConfigSet(IN FILLP_CONST FillpGlobalConfigsSt * globalResource)77 static FILLP_INT32 FtInitConfigSet(IN FILLP_CONST FillpGlobalConfigsSt *globalResource)
78 {
79     FILLP_INT32 ret;
80 
81     if ((g_spunge != FILLP_NULL_PTR) && (g_spunge->hasInited == FILLP_TRUE)) {
82         FILLP_LOGERR("Cannot Set value after stack initialization!!!");
83         return ERR_FAILURE;
84     }
85 
86     ret = FtValidateConfigParams(globalResource);
87     if (ret == ERR_FAILURE) {
88         return ERR_PARAM;
89     }
90 
91     g_resource.udp.rxBurst = globalResource->udp.rxBurst;
92 
93     g_resource.common.maxSockNum = globalResource->common.maxSockNum;
94     g_resource.common.maxConnNum = globalResource->common.maxConnectionNum;
95     g_resource.common.fullCpuEnable = globalResource->common.fullCpu;
96     g_resource.common.recvCachePktNumBufferSize = globalResource->common.recvCachePktNumBufferSize;
97     g_resource.common.outOfOrderCacheEnable = globalResource->common.outOfOrderCacheFeature;
98     g_resource.common.recvCachePktNumBufferTimeout = globalResource->timers.recvCachePktNumBufferTimeout;
99     /* Currently this will not be allowed to configure and is always 1. */
100     g_resource.common.maxInstNum = FILLP_DEFAULT_INST_NUM;
101 
102     g_resource.flowControl.initialRate = globalResource->flowControl.initialRate;
103 #ifdef FILLP_SERVER_SUPPORT
104     g_resource.flowControl.oppositeSetPercentage = globalResource->flowControl.oppositeSetPercentage;
105     g_resource.flowControl.maxRecvRate = globalResource->flowControl.maxRecvRate;
106 #endif
107     g_resource.flowControl.maxRate = globalResource->flowControl.maxRate;
108 
109     g_resource.flowControl.nackRepeatTimes = globalResource->flowControl.nackRepeatTimes;
110     g_resource.flowControl.pktLossAllow = globalResource->flowControl.pktLossAllow;
111     g_resource.flowControl.fcAlg = globalResource->flowControl.fcAlg;
112     g_resource.flowControl.maxRatePercentage = globalResource->flowControl.maxRatePercentage;
113     g_resource.flowControl.supportFairness = globalResource->flowControl.supportFairness;
114 
115     return ERR_OK;
116 }
117 
118 
119 /*******************************************************************
120   Function      : FtInitConfigGet
121   Description   : Api is used to Querry the existing configuration values for all the
122               FILLP global stack configurations. please refer FillpGlobalConfigsSt
123               for parameter details.
124   Calls         :
125   Called By     :
126   Input         : structure of type FILLP_GLOBAL_RESOURCE
127   Output        : updated globalResource with current configuration values.
128   Return        : FILLP_UINT32 SUCCESS/FAILURE
129   Others        :
130 ********************************************************************/
FtInitConfigGet(IO FillpGlobalConfigsSt * globalResource)131 static FILLP_INT32 FtInitConfigGet(IO FillpGlobalConfigsSt *globalResource)
132 {
133     globalResource->udp.rxBurst = (FILLP_UINT16)g_resource.udp.rxBurst;
134     globalResource->common.maxSockNum = g_resource.common.maxSockNum;
135     globalResource->common.maxConnectionNum = g_resource.common.maxConnNum;
136     globalResource->common.fullCpu = g_resource.common.fullCpuEnable;
137     globalResource->common.recvCachePktNumBufferSize = g_resource.common.recvCachePktNumBufferSize;
138     globalResource->common.outOfOrderCacheFeature = g_resource.common.outOfOrderCacheEnable;
139     globalResource->timers.recvCachePktNumBufferTimeout = g_resource.common.recvCachePktNumBufferTimeout;
140 
141     globalResource->flowControl.maxRate = g_resource.flowControl.maxRate;
142 #ifdef FILLP_SERVER_SUPPORT
143     globalResource->flowControl.maxRecvRate = g_resource.flowControl.maxRecvRate;
144     globalResource->flowControl.oppositeSetPercentage = g_resource.flowControl.oppositeSetPercentage;
145 #endif
146     globalResource->flowControl.nackRepeatTimes = g_resource.flowControl.nackRepeatTimes;
147     globalResource->flowControl.pktLossAllow = g_resource.flowControl.pktLossAllow;
148     globalResource->flowControl.fcAlg = g_resource.flowControl.fcAlg;
149 
150     globalResource->flowControl.initialRate = g_resource.flowControl.initialRate;
151     globalResource->flowControl.maxRatePercentage = g_resource.flowControl.maxRatePercentage;
152     globalResource->flowControl.supportFairness = g_resource.flowControl.supportFairness;
153 
154     return ERR_OK;
155 }
156 
FtGetConfigStackHalf1(IN FILLP_UINT32 name,IO void * value)157 static FILLP_INT32 FtGetConfigStackHalf1(IN FILLP_UINT32 name, IO void *value)
158 {
159     switch (name) {
160         case FT_CONF_RECV_CACHE_PKT_NUM_BUFF_SIZE:
161             *(FILLP_UINT32 *)value = g_resource.common.recvCachePktNumBufferSize;
162             break;
163 
164         case FT_CONF_RX_BURST:
165             *(FILLP_UINT16 *)value = g_resource.udp.rxBurst;
166             break;
167 
168         case FT_CONF_OUT_OF_ORDER_CATCHE_FEATURE:
169             *(FILLP_BOOL *)value = g_resource.common.outOfOrderCacheEnable;
170             break;
171 
172         case FT_CONF_CPU_CORE_USE:
173             *(FILLP_UINT8 *)value = g_resource.common.cpuCoreUse;
174             break;
175 
176         /* FLOW CONTROL */
177         case FT_CONF_MAX_SOCK_NUM:
178             *(FILLP_UINT16 *)value = g_resource.common.maxSockNum;
179             break;
180 
181         case FT_CONF_MAX_CONNECTION_NUM:
182             *(FILLP_UINT16 *)value = g_resource.common.maxConnNum;
183             break;
184 
185         case FT_CONF_FULL_CPU:
186             *(FILLP_BOOL *)value = g_resource.common.fullCpuEnable;
187             break;
188 
189         case FT_CONF_OPPOSITE_SET_PERCENTAGE:
190 #ifdef FILLP_SERVER_SUPPORT
191             *(FILLP_UINT16 *)value = g_resource.flowControl.oppositeSetPercentage;
192 #else
193             FILLP_LOGERR("Server feature Not enabled :"
194                          "FT_CONF_OPPOSITE_SET_PERCENTAGE is server only option so cannot GET !!!");
195             return ERR_FEATURE_MACRO_NOT_ENABLED;
196 #endif
197             break;
198 
199         case FT_CONF_NACK_REPEAT_TIMES:
200             *(FILLP_UINT16 *)value = g_resource.flowControl.nackRepeatTimes;
201             break;
202 
203         case FT_CONF_ALG:
204             *(FILLP_UINT8 *) value = g_resource.flowControl.fcAlg;
205             break;
206 
207         case FT_CONF_PACKET_LOSS_ALLOWED:
208             *(FILLP_UINT16 *)value = g_resource.flowControl.pktLossAllow;
209             break;
210 
211         default:
212             return ERR_PARAM;
213     }
214 
215     return ERR_OK;
216 }
217 
FtGetConfigStackHalf2(IN FILLP_UINT32 name,IO void * value)218 static FILLP_INT32 FtGetConfigStackHalf2(IN FILLP_UINT32 name, IO void *value)
219 {
220     switch (name) {
221         case FT_CONF_SUPPORT_FAIRNESS:
222             *(FILLP_UINT8 *)value = g_resource.flowControl.supportFairness;
223             break;
224 
225         case FT_CONF_MAX_RATE_PERCENTAGE:
226             *(FILLP_UINT16 *)value = g_resource.flowControl.maxRatePercentage;
227             break;
228 
229         case FT_CONF_INITIAL_RATE:
230             *(FILLP_UINT32 *)value = g_resource.flowControl.initialRate;
231             break;
232         case FT_CONF_CORE_MAX_RATE:
233             *(FILLP_UINT32 *)value = g_resource.flowControl.maxRate;
234             break;
235         case FT_CONF_CORE_MAX_RECV_RATE:
236 #ifdef FILLP_SERVER_SUPPORT
237             *(FILLP_UINT32 *)value = g_resource.flowControl.maxRecvRate;
238             break;
239 #else
240             FILLP_LOGERR("Server feature Not enabled :"
241                          "FT_CONF_CORE_MAX_RECV_RATE is server only option so cannot GET !!!");
242             return ERR_FEATURE_MACRO_NOT_ENABLED;
243 #endif
244 
245         case FT_CONF_TIMER_RECV_CACHE_PKT_NUMBUFF:
246             *(FILLP_UINT16 *)value = g_resource.common.recvCachePktNumBufferTimeout;
247             break;
248 
249         case FT_CONF_BFULL_CPU_USE_THRESHOLD_RATE:
250             *(FILLP_UINT32 *)value = g_resource.fullCpuUseThresholdRate;
251             break;
252 
253         case FT_CONF_STACK_CORE_LIMIT_RATE:
254             *(FILLP_UINT32 *)value = g_resource.flowControl.limitRate;
255             break;
256 
257         case FT_CONF_STACK_CORE_SEND_CACHE:
258             *(FILLP_UINT32 *)value = g_resource.common.sendCache;
259             break;
260 
261         case FT_CONF_STACK_CORE_RECV_CACHE:
262             *(FILLP_UINT32 *)value = g_resource.common.recvCache;
263             break;
264 
265         default:
266             return ERR_PARAM;
267     }
268 
269     return ERR_OK;
270 }
271 
272 /*******************************************************************
273   Function      : FtGetConfigStack
274   Description   : Api is used to get Individual FILLP stack configuration
275               item.
276   Calls         :
277   Called By     :
278   Input         : FILLP_UINT32    name : Name of the config item to querry
279                       (FILLP_CONFIG_LISTENUM)
280             void *value  : FILLP will store the current value for the config item.
281 
282             void *param  : this is optional. only required for config items
283             which requires additional information to get the configuration value.
284                 for ex:
285                 For SOCKET option this will store the Socket index.
286   Output        :
287   Return        : FILLP_UINT32 SUCCESS/FAILURE
288   Others        :
289 ********************************************************************/
FtGetConfigStack(IN FILLP_UINT32 name,IO void * value,IN FILLP_CONST void * param)290 static FILLP_INT32 FtGetConfigStack(
291     IN FILLP_UINT32 name,
292     IO void *value,
293     IN FILLP_CONST void *param)
294 {
295     FILLP_INT32 ret;
296 
297     FILLP_UNUSED_PARA(param);
298 
299     ret = FtGetConfigStackHalf1(name, value);
300     if (ret != ERR_PARAM) {
301         return ret;
302     }
303 
304     ret = FtGetConfigStackHalf2(name, value);
305     if (ret != ERR_PARAM) {
306         return ret;
307     }
308 
309     FILLP_LOGERR("invalid name %u!!!", name);
310 
311     return ERR_PARAM;
312 }
313 
FtSetConfigStackHalf1(IN FILLP_UINT32 name,IN FILLP_CONST void * value)314 static FILLP_INT32 FtSetConfigStackHalf1(
315     IN FILLP_UINT32 name,
316     IN FILLP_CONST void *value)
317 {
318     switch (name) {
319         case FT_CONF_RX_BURST:
320             return FtConfigSetRxBurst(value);
321 
322         case FT_CONF_RECV_CACHE_PKT_NUM_BUFF_SIZE:
323             return FtConfigSetRecvCachePktNumBufferSize(value);
324 
325         case FT_CONF_OUT_OF_ORDER_CATCHE_FEATURE:
326             return FtConfigSetOutOfOrderCacheFeature(value);
327 
328         case FT_CONF_CPU_CORE_USE:
329             return FtConfigSetCpuCoreUse(value);
330 
331         /* FLOW CONTROL */
332         case FT_CONF_MAX_SOCK_NUM:
333             return FtConfigSetMaxSockNum(value);
334 
335         case FT_CONF_MAX_CONNECTION_NUM:
336             return FtConfigSetMaxConnectionNum(value);
337 
338         case FT_CONF_FULL_CPU:
339             return FtConfigSetFullCpu(value);
340 
341         case FT_CONF_OPPOSITE_SET_PERCENTAGE:
342 #ifdef FILLP_SERVER_SUPPORT
343             return FtConfigSetOppositeSetPercentage(value);
344 #else
345             FILLP_LOGERR("Server feature Not enabled :"
346                          "FT_CONF_OPPOSITE_SET_PERCENTAGE is server only option so cannot SET !!!");
347             return ERR_FEATURE_MACRO_NOT_ENABLED;
348 #endif
349 
350         case FT_CONF_NACK_REPEAT_TIMES:
351             return FtConfigSetNackRepeatTimes(value);
352 
353         case FT_CONF_ALG:
354             return FtConfigSetAlg(value);
355 
356         case FT_CONF_PACKET_LOSS_ALLOWED:
357             return FtConfigSetPktLossAllow(value);
358 
359         case FT_CONF_SUPPORT_FAIRNESS:
360             return FtConfigSetSupportFairness(value);
361 
362         default:
363             return -1;
364     }
365 }
366 
FtSetConfigStackHalf2(IN FILLP_UINT32 name,IN FILLP_CONST void * value)367 static FILLP_INT32 FtSetConfigStackHalf2(
368     IN FILLP_UINT32 name,
369     IN FILLP_CONST void *value)
370 {
371     switch (name) {
372         case FT_CONF_INITIAL_RATE:
373             return FtConfigSetInitialRate(value);
374 
375         case FT_CONF_MAX_RATE_PERCENTAGE:
376             return FtConfigSetMaxRatePercentage(value);
377 
378         case FT_CONF_CORE_MAX_RATE:
379             return FtConfigSetMaxRate(value);
380 
381         case FT_CONF_CORE_MAX_RECV_RATE:
382 #ifdef FILLP_SERVER_SUPPORT
383             return FtConfigSetMaxRecvRate(value);
384 #else
385             FILLP_LOGERR("Server feature Not enabled :"
386                          "FT_CONF_CORE_MAX_RECV_RATE is server only option so cannot SET!!!");
387             return ERR_FEATURE_MACRO_NOT_ENABLED;
388 #endif
389         case FT_CONF_TIMER_RECV_CACHE_PKT_NUMBUFF: {
390             FILLP_UINT16 usTempTimerValue = *(FILLP_UINT16 *)value;
391             if ((usTempTimerValue < FILLP_MIN_TIMER_RECV_CACHE_PKT_NUMBUFF) ||
392                 (usTempTimerValue > FILLP_MAX_TIMER_RECV_CACHE_PKT_NUMBUFF)) {
393                 FILLP_LOGERR("recvCachePktNumBufferTimeout timer %u is invalid !!!", usTempTimerValue);
394                 return ERR_FAILURE;
395             }
396 
397             g_resource.common.recvCachePktNumBufferTimeout = usTempTimerValue;
398             return FILLP_SUCCESS;
399         }
400 
401         case FT_CONF_BFULL_CPU_USE_THRESHOLD_RATE:
402             return FtConfigSetFullCpuUseThresholdRate(value);
403 
404         case FT_CONF_STACK_CORE_LIMIT_RATE:
405             return FtConfigSetLimitRate(value);
406 
407         case FT_CONF_STACK_CORE_SEND_CACHE:
408             return FtConfigSetSendCache(value);
409 
410         case FT_CONF_STACK_CORE_RECV_CACHE:
411             return FtConfigSetRecvCache(value);
412 
413         default:
414             return -1;
415     }
416 }
417 
FtSetConfigStack(IN FILLP_UINT32 name,IN FILLP_CONST void * value,IN FILLP_CONST void * param)418 static FILLP_INT32 FtSetConfigStack(
419     IN FILLP_UINT32 name,
420     IN FILLP_CONST void *value,
421     IN FILLP_CONST void *param)
422 {
423     FILLP_INT32 ret;
424 
425     FILLP_UNUSED_PARA(param);
426     FILLP_LOGINF("name:%u", name);
427 
428     ret = FtSetConfigStackHalf1(name, value);
429     if (ret != -1) {
430         return ret;
431     }
432 
433     ret = FtSetConfigStackHalf2(name, value);
434     if (ret != -1) {
435         return ret;
436     }
437 
438     FILLP_LOGERR("invalid name %u!!!", name);
439     return ERR_FAILURE;
440 }
441 
FtConfigGet(IN FILLP_UINT32 name,IO void * value,IN FILLP_CONST void * param)442 FILLP_INT32 FtConfigGet(IN FILLP_UINT32 name,
443     IO void *value, IN FILLP_CONST void *param)
444 {
445     if (value == FILLP_NULL_PTR) {
446         FILLP_LOGERR("value is NULL!!!");
447         return ERR_NULLPTR;
448     }
449 
450     if (name == FT_CONF_INIT_APP) {
451         if (param == FILLP_NULL_PTR) {
452             FILLP_LOGERR("Parameter Error!");
453             return ERR_NULLPTR;
454         }
455         return FtAppInitConfigGet((FillpAppGlobalConfigsSt *)value, *((FILLP_INT *)param));
456     } else if (name == FT_CONF_INIT_STACK) {
457         return FtInitConfigGet((FillpGlobalConfigsSt *)value);
458     } else if (name == FT_CONF_INIT_STACK_EXT) {
459         FtGetCopyPreinitConfigs((FillpGlobalPreinitExtConfigsSt *)value);
460         return FILLP_SUCCESS;
461     } else if (name < FT_CONF_APP_CONFIG_BOUNDARY) {
462         return FtGetConfigApp(name, value, param);
463     } else {
464         return FtGetConfigStack(name, value, param);
465     }
466 }
467 
FtConfigSet(IN FILLP_UINT32 name,IN FILLP_CONST void * value,IN FILLP_CONST void * param)468 FILLP_INT32 FtConfigSet(IN FILLP_UINT32 name, IN FILLP_CONST void *value,
469     IN FILLP_CONST void *param)
470 {
471     if (value == FILLP_NULL_PTR) {
472         FILLP_LOGERR("value is NULL!!!");
473         return ERR_NULLPTR;
474     }
475 
476     if (name == FT_CONF_INIT_APP) {
477         if (param == FILLP_NULL_PTR) {
478             FILLP_LOGERR("Parameter Error!");
479             return ERR_NULLPTR;
480         }
481 
482         return FtAppInitConfigSet((FILLP_CONST FillpAppGlobalConfigsSt *)value, *((FILLP_INT *)param));
483     } else if (name == FT_CONF_INIT_STACK) {
484         return FtInitConfigSet((FILLP_CONST FillpGlobalConfigsSt *)value);
485     } else if (name == FT_CONF_INIT_STACK_EXT) {
486         return FtSetCopyPreinitConfigs((FILLP_CONST FillpGlobalPreinitExtConfigsSt *)value);
487     } else if (name < FT_CONF_APP_CONFIG_BOUNDARY) {
488         return FtSetConfigApp(name, value, param);
489     } else {
490         return FtSetConfigStack(name, value, param);
491     }
492 }
493 
494 #ifdef __cplusplus
495 }
496 #endif
497 
498