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