-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathnet_if.c
8984 lines (7784 loc) · 377 KB
/
net_if.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*********************************************************************************************************
* uC/TCP-IP
* The Embedded TCP/IP Suite
*
* Copyright 2004-2021 Silicon Laboratories Inc. www.silabs.com
*
* SPDX-License-Identifier: APACHE-2.0
*
* This software is subject to an open source license and is distributed by
* Silicon Laboratories Inc. pursuant to the terms of the Apache License,
* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
*
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* NETWORK INTERFACE MANAGEMENT
*
* Filename : net_if.c
* Version : V3.06.01
*********************************************************************************************************
* Note(s) : (1) Network Interface modules located in the following network directory :
*
* (a) \<Network Protocol Suite>\IF\net_if.*
* net_if_*.*
*
* where
* <Network Protocol Suite> directory path for network protocol suite
* net_if.* Generic Network Interface Management
* module files
* net_if_*.* Specific Network Interface(s) module files
*********************************************************************************************************
*/
#define MICRIUM_SOURCE
#define NET_IF_MODULE
/*
*********************************************************************************************************
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*********************************************************************************************************
*/
#include "net_if.h"
#include "../Source/net_cfg_net.h"
#ifdef NET_IPv4_MODULE_EN
#include "../IP/IPv4/net_arp.h"
#include "../IP/IPv4/net_ipv4.h"
#endif
#ifdef NET_IPv6_MODULE_EN
#include "../IP/IPv6/net_ipv6.h"
#include "../IP/IPv6/net_mldp.h"
#endif
#ifdef NET_IF_802x_MODULE_EN
#include "net_if_802x.h"
#endif
#ifdef NET_IF_ETHER_MODULE_EN
#include "net_if_ether.h"
#endif
#ifdef NET_IF_LOOPBACK_MODULE_EN
#include "net_if_loopback.h"
#endif
#ifdef NET_IF_WIFI_MODULE_EN
#include "net_if_wifi.h"
#endif
#ifdef NET_IGMP_MODULE_EN
#include "../IP/IPv4/net_igmp.h"
#endif
#include "../Source/net.h"
#include "../Source/net_udp.h"
#include "../Source/net_tcp.h"
#include "../Source/net_mgr.h"
#include "../Source/net_util.h"
#include <KAL/kal.h>
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*********************************************************************************************************
*/
/* -------------------- TASK NAMES -------------------- */
#define NET_IF_RX_TASK_NAME "Net IF Rx Task"
#define NET_IF_TX_DEALLOC_TASK_NAME "Net IF Tx Dealloc Task"
/* -------------------- OBJ NAMES --------------------- */
#define NET_IF_RX_Q_NAME "Net IF Rx Q"
#define NET_IF_TX_DEALLOC_Q_NAME "Net IF Tx Dealloc Q"
#define NET_IF_TX_SUSPEND_NAME "Net IF Tx Suspend"
#define NET_IF_LINK_SUBSCRIBER "Net IF Link subscriber pool"
#define NET_IF_CFG_NAME "Cfg lock"
#define NET_IF_DEV_TX_RDY_NAME "Net IF Dev Tx Rdy"
#define NET_IF_TX_SUSPEND_TIMEOUT_MIN_MS 0
#define NET_IF_TX_SUSPEND_TIMEOUT_MAX_MS 100
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*********************************************************************************************************
*/
static KAL_TASK_HANDLE NetIF_RxTaskHandle;
static KAL_TASK_HANDLE NetIF_TxDeallocTaskHandle;
static KAL_Q_HANDLE NetIF_RxQ_Handle;
static KAL_Q_HANDLE NetIF_TxQ_Handle;
static NET_IF NetIF_Tbl[NET_IF_NBR_IF_TOT]; /* Net IF tbl. */
static NET_IF_NBR NetIF_NbrBase; /* Net IF tbl base nbr. */
static NET_IF_NBR NetIF_NbrNext; /* Net IF tbl next nbr to cfg. */
static NET_STAT_CTR NetIF_RxTaskPktCtr; /* Net IF rx task q'd pkts ctr. */
static NET_IF_Q_SIZE NetIF_RxQ_SizeCfgd; /* Net IF rx q cfg'd size. */
static NET_IF_Q_SIZE NetIF_RxQ_SizeCfgdRem; /* Net IF rx q cfg'd size rem'ing. */
static NET_BUF *NetIF_TxListHead; /* Ptr to net IF tx list head. */
static NET_BUF *NetIF_TxListTail; /* Ptr to net IF tx list tail. */
static NET_IF_Q_SIZE NetIF_TxDeallocQ_SizeCfgd; /* Net IF tx dealloc cfg'd size. */
static NET_IF_Q_SIZE NetIF_TxDeallocQ_SizeCfgdRem; /* Net IF tx dealloc cfg'd size rem'ing. */
static NET_TMR *NetIF_PhyLinkStateTmr; /* Phy link state tmr. */
static CPU_INT16U NetIF_PhyLinkStateTime_ms; /* Phy link state time (in ms ). */
static NET_TMR_TICK NetIF_PhyLinkStateTime_tick; /* Phy link state time (in ticks). */
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
static NET_TMR *NetIF_PerfMonTmr; /* Perf mon tmr. */
static CPU_INT16U NetIF_PerfMonTime_ms; /* Perf mon time (in ms ). */
static NET_TMR_TICK NetIF_PerfMonTime_tick; /* Perf mon time (in ticks). */
static CPU_BOOLEAN NetIF_CtrsResetEn = DEF_NO; /* Variable added for uC-Probe to reset counters. */
#endif
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*********************************************************************************************************
*/
static void NetIF_InitTaskObj (const NET_TASK_CFG *p_rx_task_cfg,
const NET_TASK_CFG *p_tx_task_cfg,
NET_ERR *p_err);
static void NetIF_ObjInit ( NET_IF *p_if,
NET_ERR *p_err);
static void NetIF_ObjDel ( NET_IF *p_if);
static void NetIF_DevTxRdyWait ( NET_IF *p_if,
NET_ERR *p_err);
/* --------------------- RX FNCTS --------------------- */
static void NetIF_RxTask ( void *p_data);
static void NetIF_RxTaskHandler ( void);
static NET_IF_NBR NetIF_RxTaskWait ( NET_ERR *p_err);
static void NetIF_RxHandler ( NET_IF_NBR if_nbr);
#ifdef NET_LOAD_BAL_MODULE_EN
static void NetIF_RxHandlerLoadBal ( NET_IF *p_if);
#endif
static NET_BUF_SIZE NetIF_RxPkt ( NET_IF *p_if,
NET_ERR *p_err);
#ifdef NET_LOAD_BAL_MODULE_EN
static void NetIF_RxPktDec ( NET_IF *p_if);
#endif
#if ((NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) || \
(NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED))
static void NetIF_RxPktDiscard ( NET_BUF *p_buf,
NET_ERR *p_err);
#endif
static void NetIF_RxQ_SizeCfg ( NET_IF_Q_SIZE size);
/* --------------------- TX FNCTS --------------------- */
static void NetIF_TxDeallocTask ( void *p_data);
static void NetIF_TxDeallocTaskHandler ( void);
/* Wait for dev tx comp signal. */
static CPU_INT08U *NetIF_TxDeallocTaskWait ( NET_ERR *p_err);
static void NetIF_TxDeallocQ_SizeCfg ( NET_IF_Q_SIZE size);
static void NetIF_TxHandler ( NET_BUF *p_buf,
NET_ERR *p_err);
static NET_BUF_SIZE NetIF_TxPkt ( NET_IF *p_if,
NET_BUF *p_buf,
NET_ERR *p_err);
#if ((NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) || \
(NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED))
static void NetIF_TxPktValidate ( NET_IF *p_if,
NET_BUF_HDR *p_buf_hdr,
NET_ERR *p_err);
#endif
static void NetIF_TxPktListDealloc ( CPU_INT08U *p_buf_data);
static NET_BUF *NetIF_TxPktListSrch ( CPU_INT08U *p_buf_data);
static void NetIF_TxPktListInsert ( NET_BUF *p_buf);
static void NetIF_TxPktListRemove ( NET_BUF *p_buf);
static void NetIF_TxPktFree ( NET_BUF *p_buf);
static void NetIF_TxPktDiscard ( NET_BUF *p_buf,
CPU_BOOLEAN inc_ctrs,
NET_ERR *p_err);
#ifdef NET_LOAD_BAL_MODULE_EN
static void NetIF_TxSuspendTimeoutInit ( NET_IF *p_if,
NET_ERR *p_err);
static void NetIF_TxSuspendSignal ( NET_IF *p_if);
static void NetIF_TxSuspendWait ( NET_IF *p_if);
#endif
/* ------------------ HANDLER FNCTS ------------------- */
static void NetIF_BufPoolCfgValidate ( NET_IF_NBR if_nbr,
NET_DEV_CFG *p_dev_cfg,
NET_ERR *p_err);
static void *NetIF_GetDataAlignPtr ( NET_IF_NBR if_nbr,
NET_TRANSACTION transaction,
void *p_data,
NET_ERR *p_err);
static CPU_INT16U NetIF_GetProtocolHdrSize ( NET_IF *p_if,
NET_PROTOCOL_TYPE protocol,
NET_ERR *p_err);
static void NetIF_IO_CtrlHandler ( NET_IF_NBR if_nbr,
CPU_INT08U opt,
void *p_data,
NET_ERR *p_err);
#ifndef NET_CFG_LINK_STATE_POLL_DISABLED
static void NetIF_PhyLinkStateHandler ( void *p_obj);
#endif
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
static void NetIF_PerfMonHandler ( void *p_obj);
#endif
/*
*********************************************************************************************************
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*********************************************************************************************************
*/
#ifdef NET_LOAD_BAL_MODULE_EN
#ifndef NET_IF_CFG_TX_SUSPEND_TIMEOUT_MS
#error "NET_IF_CFG_TX_SUSPEND_TIMEOUT_MS not #define'd in 'net_cfg.h' "
#error " [MUST be >= NET_IF_TX_SUSPEND_TIMEOUT_MIN_MS]"
#error " [ && <= NET_IF_TX_SUSPEND_TIMEOUT_MAX_MS]"
#elif (DEF_CHK_VAL(NET_IF_CFG_TX_SUSPEND_TIMEOUT_MS, \
NET_IF_TX_SUSPEND_TIMEOUT_MIN_MS, \
NET_IF_TX_SUSPEND_TIMEOUT_MAX_MS) != DEF_OK)
#error "NET_IF_CFG_TX_SUSPEND_TIMEOUT_MS illegally #define'd in 'net_cfg.h' "
#error " [MUST be >= NET_IF_TX_SUSPEND_TIMEOUT_MIN_MS]"
#error " [ && <= NET_IF_TX_SUSPEND_TIMEOUT_MAX_MS]"
#endif
#endif
/*
*********************************************************************************************************
* NetIF_Init()
*
* Description : (1) Initialize the Network Interface Management Module :
*
* (a) Perform Network Interface/OS initialization
* (b) Initialize Network Interface table
* (c) Initialize Network Interface counter(s)
* (d) Initialize Network Interface transmit list pointers
* (e) Initialize Network Interface timers
* (f) Initialize Network Interface(s)/Layer(s) :
* (1) (A) Initialize Loopback Interface
* (B) Initialize specific network interface(s)
* (2) Initialize ARP Layer
*
*
* Argument(s) : p_err Pointer to variable that will receive the return error code from this function :
*
* NET_IF_ERR_NONE Network interface module successfully
* initialized.
*
* -------------------- RETURNED BY NetIF_InitTaskObj() : ----------------------
* See NetIF_InitTaskObj() for additional return error codes.
*
* -------------------- RETURNED BY NetIF_Loopback_Init() : --------------------
* See NetIF_Loopback_Init() for additional return error codes.
*
* ---------------------- RETURNED BY NetIF_&&&_Init() : -----------------------
* See specific network interface(s) 'Init()' for additional return error codes.
*
* ------------------------ RETURNED BY NetTmr_Get() : -------------------------
* See NetTmr_Get() for additional return error codes.
*
* ---------------------- RETURNED BY NetStat_CtrInit() : ----------------------
* See NetStat_CtrInit() for additional return error codes.
*
* Return(s) : none.
*
* Caller(s) : Net_Init().
*
* This function is an INTERNAL network protocol suite function & MUST NOT be called by
* application function(s).
*
* Note(s) : (2) The following network interface initialization functions MUST be sequenced as follows :
*
* (a) Net_IF_KAL_Init() MUST precede ALL other network interface initialization functions
* (b) NetIF_&&&_Init()'s MUST follow ALL other network interface initialization functions
*
* (3) Regardless of whether the Loopback interface is enabled or disabled, network interface
* numbers MUST be initialized to reserve the lowest possible network interface number
* ('NET_IF_NBR_LOOPBACK') for the Loopback interface.
*
* NetIF_NbrLoopback
* NetIF_NbrBaseCfgd
*********************************************************************************************************
*/
void NetIF_Init (const NET_TASK_CFG *p_rx_task_cfg,
const NET_TASK_CFG *p_tx_task_cfg,
NET_ERR *p_err)
{
NET_IF *p_if;
NET_IF_NBR if_nbr;
NET_TMR_TICK timeout_tick;
CPU_SR_ALLOC();
/* ---------- PERFORM NET IF/OS INIT ---------- */
NetIF_InitTaskObj(p_rx_task_cfg, p_tx_task_cfg, p_err); /* Create net IF obj(s)/task(s) [see Note #2a]. */
if (*p_err != NET_IF_ERR_NONE) {
return;
}
/* --------------- INIT IF TBL ---------------- */
p_if = &NetIF_Tbl[0];
for (if_nbr = 0u; if_nbr < NET_IF_NBR_IF_TOT; if_nbr++) {
p_if->Nbr = NET_IF_NBR_NONE;
p_if->Type = NET_IF_TYPE_NONE;
p_if->Init = DEF_NO;
p_if->En = DEF_DISABLED;
p_if->Link = NET_IF_LINK_DOWN;
p_if->MTU = 0u;
p_if->IF_API = (void *)0;
p_if->IF_Data = (void *)0;
p_if->Dev_API = (void *)0;
p_if->Dev_Cfg = (void *)0;
p_if->Dev_Data = (void *)0;
p_if->Ext_API = (void *)0;
p_if->Ext_Cfg = (void *)0;
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
p_if->PerfMonState = NET_IF_PERF_MON_STATE_STOP;
p_if->PerfMonTS_Prev_ms = NET_TS_NONE;
#else
(void)&NetIF_NbrBase;
#endif
#ifdef NET_LOAD_BAL_MODULE_EN
NetStat_CtrInit(&p_if->RxPktCtr, p_err);
if (*p_err != NET_STAT_ERR_NONE) {
return;
}
NetStat_CtrInit(&p_if->TxSuspendCtr, p_err);
if (*p_err != NET_STAT_ERR_NONE) {
return;
}
#endif
p_if++;
}
/* Init base/next IF nbrs (see Note #3). */
#if (NET_IF_CFG_LOOPBACK_EN == DEF_ENABLED)
NetIF_NbrBase = NET_IF_NBR_BASE;
NetIF_NbrNext = NET_IF_NBR_LOOPBACK;
#else
NetIF_NbrBase = NET_IF_NBR_BASE_CFGD;
NetIF_NbrNext = NET_IF_NBR_BASE_CFGD;
#endif
/* ------------ INIT NET IF CTR's ------------- */
NetStat_CtrInit(&NetIF_RxTaskPktCtr, p_err);
if (*p_err != NET_STAT_ERR_NONE) {
return;
}
/* ----------- INIT NET IF TX LIST ------------ */
NetIF_TxListHead = DEF_NULL;
NetIF_TxListTail = DEF_NULL;
/* ------------ INIT NET IF TMR's ------------- */
CPU_CRITICAL_ENTER();
timeout_tick = NetIF_PhyLinkStateTime_tick;
CPU_CRITICAL_EXIT();
#ifndef NET_CFG_LINK_STATE_POLL_DISABLED
NetIF_PhyLinkStateTmr = NetTmr_Get((CPU_FNCT_PTR)&NetIF_PhyLinkStateHandler,
(void *) 0,
(NET_TMR_TICK) timeout_tick,
(NET_ERR *) p_err);
if (*p_err != NET_TMR_ERR_NONE) {
return;
}
#endif
(void)&NetIF_PhyLinkStateTmr;
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
CPU_CRITICAL_ENTER();
timeout_tick = NetIF_PerfMonTime_tick;
CPU_CRITICAL_EXIT();
NetIF_PerfMonTmr = NetTmr_Get((CPU_FNCT_PTR)&NetIF_PerfMonHandler,
(void *) 0,
(NET_TMR_TICK) timeout_tick,
(NET_ERR *) p_err);
if (*p_err != NET_TMR_ERR_NONE) {
return;
}
(void)&NetIF_PerfMonTmr;
#endif
/* -------------- INIT NET IF(s) -------------- */
/* See Note #2b. */
#ifdef NET_IF_LOOPBACK_MODULE_EN
NetIF_Loopback_Init(p_err);
if (*p_err != NET_IF_ERR_NONE) {
return;
}
#endif
#ifdef NET_IF_ETHER_MODULE_EN
NetIF_Ether_Init(p_err);
if (*p_err != NET_IF_ERR_NONE) {
return;
}
#endif
#ifdef NET_IF_WIFI_MODULE_EN
NetIF_WiFi_Init(p_err);
if (*p_err != NET_IF_ERR_NONE) {
return;
}
#endif
#ifdef NET_ARP_MODULE_EN
NetARP_Init(p_err);
if (*p_err != NET_ARP_ERR_NONE) {
return;
}
#endif
*p_err = NET_IF_ERR_NONE;
}
/*
*********************************************************************************************************
* NetIF_BufPoolInit()
*
* Description : (1) Create network interface buffer memory pools :
*
* (a) Validate network interface buffer configuration
* (b) Create network buffer memory pools :
* (1) Create receive large buffer pool
* (2) Create transmit large buffer pool
* (3) Create transmit small buffer pool
* (4) Create network buffer header pool
*
*
* Argument(s) : p_if Pointer to network interface.
* ---- Argument validated in NetIF_Add().
*
* p_err Pointer to variable that will receive the return error code from this function :
*
* NET_IF_ERR_NONE Buffer pools successfully created.
* NET_IF_ERR_INVALID_CFG Invalid/NULL API configuration.
* NET_ERR_FAULT_NULL_FNCT Invalid NULL function pointer.
*
* -- RETURNED BY NetIF_BufPoolCfgValidate() : --
* NET_IF_ERR_INVALID_POOL_TYPE Invalid network interface buffer pool type.
* NET_IF_ERR_INVALID_POOL_ADDR Invalid network interface buffer pool address.
* NET_IF_ERR_INVALID_POOL_SIZE Invalid network interface buffer pool size.
* NET_IF_ERR_INVALID_POOL_QTY Invalid network interface buffer pool number
* of buffers configured.
*
* --- RETURNED BY NetBuf_PoolCfgValidate() : ---
* NET_BUF_ERR_INVALID_QTY Invalid number of network buffers configured.
* NET_BUF_ERR_INVALID_SIZE Invalid size of network buffer data areas
* configured.
* NET_BUF_ERR_INVALID_IX Invalid offset from base index into network
* buffer data area configured.
*
* ------ RETURNED BY NetBuf_PoolInit() : -------
* NET_IF_ERR_INVALID_IF Invalid network interface number.
* NET_BUF_ERR_INVALID_POOL_TYPE Invalid network buffer pool type.
* NET_BUF_ERR_POOL_MEM_ALLOC Network buffer pool initialization failed.
*
* Return(s) : none.
*
* Caller(s) : NetIF_Ether_IF_Add(),
* NetIF_WiFi_IF_Add(),
* NetIF_Loopback_IF_Add().
*
* This function is an INTERNAL network protocol suite function & SHOULD NOT be called by
* application function(s) [see also Note #1].
*
* Note(s) : (2) (a) Each added network interfaces MUST decrement its total number of configured receive
* buffers from the remaining network interface receive queue configured size.
*
* (b) Each added network interfaces MUST decrement its total number of configured transmit
* buffers from the remaining network interface transmit deallocation queue configured
* size.
*
* (1) However, since the network loopback interface does NOT deallocate transmit
* packets via the network interface transmit deallocation task (see
* 'net_if_loopback.c NetIF_Loopback_Tx() Note #4'); then the network interface
* transmit deallocation queue size does NOT need to be adjusted by the network
* loopback interface's number of configured transmit buffers.
*
* See also 'NetBuf_PoolCfgValidate() Note #2'.
*
* (3) Each network buffer data area allocates additional octets for its configured offset
* (see 'net_dev_cfg.c EXAMPLE NETWORK INTERFACE / DEVICE CONFIGURATION Note #5'). This
* ensures that each data area's effective, useable size still equals its configured size
* (see 'net_dev_cfg.c EXAMPLE NETWORK INTERFACE / DEVICE CONFIGURATION Note #2').
*********************************************************************************************************
*/
void NetIF_BufPoolInit (NET_IF *p_if,
NET_ERR *p_err)
{
NET_IF_NBR if_nbr;
NET_IF_API *p_if_api;
NET_DEV_CFG *p_dev_cfg;
void *p_addr;
CPU_ADDR size;
CPU_SIZE_T size_buf;
CPU_SIZE_T reqd_octets;
NET_BUF_QTY nbr_bufs_tot;
/* ------------- VALIDATE NET IF BUF CFG -------------- */
if_nbr = (NET_IF_NBR )p_if->Nbr;
p_if_api = (NET_IF_API *)p_if->IF_API;
p_dev_cfg = (NET_DEV_CFG *)p_if->Dev_Cfg;
#if ((NET_ERR_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) || \
(NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED))
if (p_if_api == (NET_IF_API *)0) {
*p_err = NET_IF_ERR_INVALID_CFG;
return;
}
if (p_if_api->BufPoolCfgValidate == (void (*)(NET_IF *,
NET_ERR *))0) {
*p_err = NET_ERR_FAULT_NULL_FNCT;
return;
}
#endif
NetBuf_PoolCfgValidate(p_if->Type, p_dev_cfg, p_err);
if (*p_err != NET_BUF_ERR_NONE) {
return;
}
NetIF_BufPoolCfgValidate(if_nbr, p_dev_cfg, p_err);
if (*p_err != NET_IF_ERR_NONE) {
return;
}
p_if_api->BufPoolCfgValidate(p_if, p_err);
if (*p_err != NET_IF_ERR_NONE) {
return;
}
/* --------- INIT NET IF RX BUF POOL ---------- */
if (p_dev_cfg->RxBufPoolType == NET_IF_MEM_TYPE_MAIN) {
p_addr = (void *)0;
size = (CPU_ADDR)0u;
} else {
p_addr = (void *)p_dev_cfg->MemAddr;
size = (CPU_ADDR)p_dev_cfg->MemSize;
}
if (p_dev_cfg->RxBufLargeNbr > 0) {
size_buf = p_dev_cfg->RxBufLargeSize + /* Inc cfg'd rx buf size by ... */
p_dev_cfg->RxBufIxOffset; /* ... cfg'd rx buf offset (see Note #3). */
NetBuf_PoolInit(if_nbr,
NET_BUF_TYPE_RX_LARGE, /* Create large rx buf data area pool. */
p_addr, /* Create pool in dedicated mem, if avail. */
size, /* Size of dedicated mem, if avail. */
p_dev_cfg->RxBufLargeNbr, /* Nbr of large rx bufs to create. */
size_buf, /* Size of large rx bufs to create. */
p_dev_cfg->RxBufAlignOctets, /* Align large rx bufs to octet boundary. */
&reqd_octets,
p_err);
if (*p_err != NET_BUF_ERR_NONE) {
return;
}
NetIF_RxQ_SizeCfgdRem -= p_dev_cfg->RxBufLargeNbr; /* Dec rem'ing rx Q size by nbr large rx bufs .. */
/* .. (see Note #2a). */
}
/* ------------- INIT NET IF TX BUF POOLS ------------- */
if (p_dev_cfg->TxBufPoolType == NET_IF_MEM_TYPE_MAIN) {
p_addr = (void *)0;
size = (CPU_ADDR)0u;
} else {
p_addr = (void *)p_dev_cfg->MemAddr;
size = (CPU_ADDR)p_dev_cfg->MemSize;
}
if (p_dev_cfg->TxBufLargeNbr > 0) {
size_buf = p_dev_cfg->TxBufLargeSize + /* Inc cfg'd tx buf size by ... */
p_dev_cfg->TxBufIxOffset; /* ... cfg'd tx buf offset (see Note #3). */
NetBuf_PoolInit(if_nbr,
NET_BUF_TYPE_TX_LARGE, /* Create large tx buf data area pool. */
p_addr, /* Create pool in dedicated mem, if avail. */
size, /* Size of dedicated mem, if avail. */
p_dev_cfg->TxBufLargeNbr, /* Nbr of large tx bufs to create. */
size_buf, /* Size of large tx bufs to create. */
p_dev_cfg->TxBufAlignOctets, /* Align large tx bufs to octet boundary. */
&reqd_octets,
p_err);
if (*p_err != NET_BUF_ERR_NONE) {
return;
}
if (if_nbr != NET_IF_NBR_LOOPBACK) { /* For all non-loopback IF's (see Note #2b1),.. */
/* .. dec rem'ing tx dealloc Q size by nbr .. */
/* .. of large tx bufs (see Note #2b). */
NetIF_TxDeallocQ_SizeCfgdRem -= p_dev_cfg->TxBufLargeNbr;
}
}
if (p_dev_cfg->TxBufSmallNbr > 0) {
size_buf = p_dev_cfg->TxBufSmallSize + /* Inc cfg'd tx buf size by ... */
p_dev_cfg->TxBufIxOffset; /* ... cfg'd tx buf offset (see Note #3). */
NetBuf_PoolInit(if_nbr,
NET_BUF_TYPE_TX_SMALL, /* Create small tx buf data area pool. */
p_addr, /* Create pool in dedicated mem, if avail. */
size, /* Size of dedicated mem, if avail. */
p_dev_cfg->TxBufSmallNbr, /* Nbr of small tx bufs to create. */
size_buf, /* Size of small tx bufs to create. */
p_dev_cfg->TxBufAlignOctets, /* Align small tx bufs to octet boundary. */
&reqd_octets,
p_err);
if (*p_err != NET_BUF_ERR_NONE) {
return;
}
if (if_nbr != NET_IF_NBR_LOOPBACK) { /* For all non-loopback IF's (see Note #2b1),.. */
/* .. dec rem'ing tx dealloc Q size by nbr .. */
/* .. of small tx bufs (see Note #2b). */
NetIF_TxDeallocQ_SizeCfgdRem -= p_dev_cfg->TxBufSmallNbr;
}
}
/* --------------- INIT NET IF BUF POOL --------------- */
/* Calc IF's tot nbr net bufs. */
nbr_bufs_tot = p_dev_cfg->RxBufLargeNbr +
p_dev_cfg->TxBufLargeNbr +
p_dev_cfg->TxBufSmallNbr;
if (nbr_bufs_tot > 0) {
NetBuf_PoolInit(if_nbr,
NET_BUF_TYPE_BUF, /* Create net buf pool. */
0, /* Create net bufs from main mem heap. */
0u,
nbr_bufs_tot, /* Nbr of net bufs to create. */
sizeof(NET_BUF), /* Size of net bufs. */
sizeof(CPU_DATA), /* Align net bufs to CPU data word size. */
&reqd_octets,
p_err);
if (*p_err != NET_BUF_ERR_NONE) {
return;
}
}
*p_err = NET_IF_ERR_NONE;
}
/*
*********************************************************************************************************
* NetIF_CfgPhyLinkPeriod()
*
* Description : (1) Configure Network Interface Physical Link State Handler time (i.e. scheduling period) :
*
* (a) Validate desired physical link state handler time
* (b) Configure desired physical link state handler time
*
*
* Argument(s) : time_ms Desired value for Network Interface Physical Link State Handler time
* (in milliseconds).
*
* Return(s) : DEF_OK, Network Interface Physical Link State Handler timeout configured.
*
* DEF_FAIL, otherwise.
*
* Caller(s) : Application.
*
* This function is a network protocol suite application programming interface (API)
* function & MAY be called by application function(s).
*
* Note(s) : (2) Configured time does NOT reschedule the next physical link state handling but
* configures the scheduling of all subsequent physical link state handling.
*
* (3) Configured time converted to 'NET_TMR_TICK' ticks to avoid run-time conversion.
*
* (4) 'NetIF_PhyLinkStateTime' variables MUST ALWAYS be accessed exclusively in critical
* sections.
*********************************************************************************************************
*/
CPU_BOOLEAN NetIF_CfgPhyLinkPeriod (CPU_INT16U time_ms)
{
NET_TMR_TICK time_tick;
CPU_SR_ALLOC();
if (time_ms < NET_IF_PHY_LINK_TIME_MIN_MS) {
return (DEF_FAIL);
}
if (time_ms > NET_IF_PHY_LINK_TIME_MAX_MS) {
return (DEF_FAIL);
}
time_tick = ((NET_TMR_TICK)time_ms * NET_TMR_TIME_TICK_PER_SEC) / DEF_TIME_NBR_mS_PER_SEC;
CPU_CRITICAL_ENTER();
NetIF_PhyLinkStateTime_ms = time_ms;
NetIF_PhyLinkStateTime_tick = time_tick;
CPU_CRITICAL_EXIT();
(void)&NetIF_PhyLinkStateTime_ms;
return (DEF_OK);
}
/*
*********************************************************************************************************
* NetIF_CfgPerfMonPeriod()
*
* Description : (1) Configure Network Interface Performance Monitor Handler time (i.e. scheduling period) :
*
* (a) Validate desired performance monitor handler time
* (b) Configure desired performance monitor handler time
*
*
* Argument(s) : time_ms Desired value for Network Interface Performance Monitor Handler time
* (in milliseconds).
*
* Return(s) : DEF_OK, Network Interface Performance Monitor Handler time configured.
*
* DEF_FAIL, otherwise.
*
* Caller(s) : Application.
*
* This function is a network protocol suite application programming interface (API)
* function & MAY be called by application function(s).
*
* Note(s) : (2) Configured time does NOT reschedule the next performance monitor handling but
* configures the scheduling of all subsequent performance monitor handling.
*
* (3) Configured time converted to 'NET_TMR_TICK' ticks to avoid run-time conversion.
*
* (4) 'NetIF_PerfMonTime' variables MUST ALWAYS be accessed exclusively in critical
* sections.
*********************************************************************************************************
*/
#if (NET_CTR_CFG_STAT_EN == DEF_ENABLED)
CPU_BOOLEAN NetIF_CfgPerfMonPeriod (CPU_INT16U time_ms)
{
NET_TMR_TICK time_tick;
CPU_SR_ALLOC();
if (time_ms < NET_IF_PERF_MON_TIME_MIN_MS) {
return (DEF_FAIL);
}
if (time_ms > NET_IF_PERF_MON_TIME_MAX_MS) {
return (DEF_FAIL);
}
time_tick = ((NET_TMR_TICK)time_ms * NET_TMR_TIME_TICK_PER_SEC) / DEF_TIME_NBR_mS_PER_SEC;
CPU_CRITICAL_ENTER();
NetIF_PerfMonTime_ms = time_ms;
NetIF_PerfMonTime_tick = time_tick;
CPU_CRITICAL_EXIT();
(void)&NetIF_PerfMonTime_ms;
return (DEF_OK);
}
#endif
/*
*********************************************************************************************************
* NetIF_Add()
*
* Description : (1) Add & initialize a specific instance of a network interface :
*
* (a) Acquire network lock
* (b) Validate network interface :
* (1) Validate next network interface available
* (2) Validate network interface parameters
* (c) Initialize network interface :
* (1) Configure network interface parameters
* (2) Initialize network buffers & pools
* (3) Initialize specific network interface & device
* (d) Release network lock
* (e) Return network interface number
* OR
* Null network interface number & error code, on failure
*
*
* Argument(s) : if_api Pointer to specific network interface API.
*
* dev_api Pointer to specific network device driver API.
*
* dev_bsp Pointer to specific network device board-specific API.
*
* dev_cfg Pointer to specific network device hardware configuration.
*
* ext_api Pointer to specific network extension layer API (see Note #4).
*
* ext_cfg Pointer to specific network extension layer configuration (see Note #4).
*
* p_err Pointer to variable that will receive the return error code from this function :
*
* NET_IF_ERR_NONE Network interface successfully added.
* NET_ERR_FAULT_NULL_PTR Argument 'if_api'/'dev_cfg'/'dev_api'/'dev_bsp'
* passed a NULL pointer.
* NET_ERR_FAULT_NULL_FNCT Invalid/NULL network interface function pointer.
* NET_IF_ERR_INVALID_CFG Invalid network interface API configuration.
* NET_IF_ERR_INVALID_IF NO available network interfaces
* (see 'net_cfg_dev.h NET_IF_NBR_IF_TOT').
*
* NET_DEV_ERR_NULL_PTR Argument(s) 'dev_api'/'dev_bsp'/'dev_cfg'
* passed a NULL pointer.
* NET_DEV_ERR_INVALID_CFG Argument(s) 'phy_api'/'phy_cfg'/'phy_data'
* incorrectly configured (see Note #4b).
*
* NET_INIT_ERR_NOT_COMPLETED Network initialization NOT complete.
*
* ------- RETURNED BY NetIF_BufPoolInit() : --------
* NET_IF_ERR_INVALID_POOL_TYPE Invalid network interface buffer pool type.
* NET_IF_ERR_INVALID_POOL_ADDR Invalid network interface buffer pool address.
* NET_IF_ERR_INVALID_POOL_SIZE Invalid network interface buffer pool size.
* NET_IF_ERR_INVALID_POOL_QTY Invalid network interface buffer pool number
* of buffers configured.
*
* NET_BUF_ERR_POOL_MEM_ALLOC Network buffer pool initialization failed.
* NET_BUF_ERR_INVALID_POOL_TYPE Invalid network buffer pool type.
* NET_BUF_ERR_INVALID_QTY Invalid number of network buffers configured.
* NET_BUF_ERR_INVALID_SIZE Invalid size of network buffer data areas
* configured.
* NET_BUF_ERR_INVALID_IX Invalid offset from base index into network
* buffer data area configured.
*
* --------- RETURNED BY 'p_if_api->Add()' : ---------
* NET_ERR_FAULT_MEM_ALLOC Insufficient resources available to add interface.
*
* NET_DEV_ERR_INVALID_CFG Invalid/NULL network device API configuration.
* NET_ERR_FAULT_NULL_FNCT Invalid NULL network device function pointer.
*
* See specific network interface(s) 'Add()' for
* additional return error codes.
*
* ---- RETURNED BY Net_GlobalLockAcquire() : ----
* NET_ERR_FAULT_LOCK_ACQUIRE Network access NOT acquired.
*
* Return(s) : Interface number of the added interface, if NO error(s).
*
* NET_IF_NBR_NONE, otherwise.
*
* Caller(s) : Application.
*
* This function is a network protocol suite application programming interface (API) function
* & MAY be called by application function(s) [see also Note #2].
*
* Note(s) : (2) NetIF_Add() is called by application function(s) & ... :
*
* (a) MUST NOT be called with the global network lock already acquired; ...