-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFreeRTOS_TCP_WIN.c
1980 lines (1638 loc) · 63.7 KB
/
FreeRTOS_TCP_WIN.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
/*
* FreeRTOS+TCP Labs Build 150406 (C) 2015 Real Time Engineers ltd.
* Authors include Hein Tibosch and Richard Barry
*
*******************************************************************************
***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
*** ***
*** ***
*** FREERTOS+TCP IS STILL IN THE LAB: ***
*** ***
*** This product is functional and is already being used in commercial ***
*** products. Be aware however that we are still refining its design, ***
*** the source code does not yet fully conform to the strict coding and ***
*** style standards mandated by Real Time Engineers ltd., and the ***
*** documentation and testing is not necessarily complete. ***
*** ***
*** PLEASE REPORT EXPERIENCES USING THE SUPPORT RESOURCES FOUND ON THE ***
*** URL: http://www.FreeRTOS.org/contact Active early adopters may, at ***
*** the sole discretion of Real Time Engineers Ltd., be offered versions ***
*** under a license other than that described below. ***
*** ***
*** ***
***** NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ******* NOTE ***
*******************************************************************************
*
* - Open source licensing -
* While FreeRTOS+TCP is in the lab it is provided only under version two of the
* GNU General Public License (GPL) (which is different to the standard FreeRTOS
* license). FreeRTOS+TCP is free to download, use and distribute under the
* terms of that license provided the copyright notice and this text are not
* altered or removed from the source files. The GPL V2 text is available on
* the gnu.org web site, and on the following
* URL: http://www.FreeRTOS.org/gpl-2.0.txt. Active early adopters may, and
* solely at the discretion of Real Time Engineers Ltd., be offered versions
* under a license other then the GPL.
*
* FreeRTOS+TCP is distributed in the hope that it will be useful. You cannot
* use FreeRTOS+TCP unless you agree that you use the software 'as is'.
* FreeRTOS+TCP is provided WITHOUT ANY WARRANTY; without even the implied
* warranties of NON-INFRINGEMENT, MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. Real Time Engineers Ltd. disclaims all conditions and terms, be they
* implied, expressed, or statutory.
*
* 1 tab == 4 spaces!
*
* http://www.FreeRTOS.org
* http://www.FreeRTOS.org/plus
* http://www.FreeRTOS.org/labs
*
*/
/*
* FreeRTOS_TCP_WIN.c
* Module which handles the TCP windowing schemes for FreeRTOS+TCP. Many
* functions have two versions - one for FreeRTOS+TCP (full) and one for
* FreeRTOS+TCP (lite).
*
* In this module all ports and IP addresses and sequence numbers are
* being stored in host byte-order.
*/
/* Standard includes. */
#include <stdint.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
/* FreeRTOS+TCP includes. */
#include "FreeRTOS_UDP_IP.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_Sockets.h"
#include "FreeRTOS_IP_Private.h"
#include "NetworkBufferManagement.h"
#include "FreeRTOS_TCP_WIN.h"
/* Constants used for Smoothed Round Trip Time (SRTT). */
#define winSRTT_INCREMENT_NEW 2
#define winSRTT_INCREMENT_CURRENT 6
#define winSRTT_DECREMENT_NEW 1
#define winSRTT_DECREMENT_CURRENT 7
#define winSRTT_CAP_mS 50
#if( ipconfigUSE_TCP_WIN == 1 )
#define xTCPWindowRxNew( pxWindow, ulSequenceNumber, lCount ) xTCPWindowNew( pxWindow, ulSequenceNumber, lCount, pdTRUE )
#define xTCPWindowTxNew( pxWindow, ulSequenceNumber, lCount ) xTCPWindowNew( pxWindow, ulSequenceNumber, lCount, pdFALSE )
/* The code to send a single Selective ACK (SACK):
* NOP (0x01), NOP (0x01), SACK (0x05), LEN (0x0a),
* followed by a lower and a higher sequence number,
* where LEN is 2 + 2*4 = 10 bytes. */
#define OPTION_CODE_SINGLE_SACK ( 0x0101050a )
/* Normal retransmission:
* A packet will be retransmitted after a Retransmit Time-Out (RTO).
* Fast retransmission:
* When 3 packets with a higher sequence number have been acknowledged
* by the peer, it is very unlikely a current packet will ever arrive.
* It will be retransmitted far before the RTO.
*/
#define DUPLICATE_ACKS_BEFORE_FAST_RETRANSMIT ( 3 )
/* If there have been several retransmissions (4), decrease the
* size of the transmission window to at most 2 times MSS.
*/
#define MAX_TRANSMIT_COUNT_USING_LARGE_WINDOW ( 4 )
#endif /* configUSE_TCP_WIN */
/*-----------------------------------------------------------*/
extern void vListInsertGeneric( List_t * const pxList, ListItem_t * const pxNewListItem, MiniListItem_t * const pxWhere );
/*
* All TCP sockets share a pool of segment descriptors (TCPSegment_t)
* Available descriptors are stored in the 'xSegmentList'
* When a socket owns a descriptor, it will either be stored in
* 'xTxSegments' or 'xRxSegments'
* As soon as a package has been confirmed, the descriptor will be returned
* to the segment pool
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static BaseType_t prvCreateSectors( void );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* Find a segment with a given sequence number in the list of received
* segments: 'pxWindow->xRxSegments'.
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowRxFind( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* Allocate a new segment
* The socket will borrow all segments from a common pool: 'xSegmentList',
* which is a list of 'TCPSegment_t'
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowNew( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber, int32_t lCount, BaseType_t xIsForRx );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/* When the peer has a close request (FIN flag), the driver will check if
* there are missing packets in the Rx-queue
* It will accept the closure of the connection if both conditions are true:
* - the Rx-queue is empty
* - we've ACK'd the highest Rx sequence number seen
*/
#if( ipconfigUSE_TCP_WIN == 1 )
BaseType_t xTCPWindowRxEmpty( TCPWindow_t *pxWindow );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* Detaches and returns the head of a queue
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowGetHead( List_t *pxList );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* Returns the head of a queue but it won't be detached
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowPeekHead( List_t *pxList );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* Free entry pxSegment because it's not used anymore
* The ownership will be passed back to the segment pool
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static void vTCPWindowFree( TCPSegment_t *pxSegment );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* A segment has been received with sequence number 'ulSequenceNumber', where
* 'ulCurrentSequenceNumber == ulSequenceNumber', which means that exactly this
* segment was expected. xTCPWindowRxConfirm() will check if there is already
* another segment with a sequence number between (ulSequenceNumber) and
* (ulSequenceNumber+xLength). Normally none will be found, because the next Rx
* segment should have a sequence number equal to '(ulSequenceNumber+xLength)'.
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowRxConfirm( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber, uint32_t ulLength );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* FreeRTOS+TCP stores data in circular buffers. Calculate the next position to
* store.
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static int32_t lTCPIncrementTxPosition( int32_t lPosition, int32_t lMax, int32_t lCount );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* This function will look if there is new transmission data. It will return
* true if there is data to be sent.
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static BaseType_t prvTCPWindowTxHasSpace( TCPWindow_t *pxWindow, uint32_t ulWindowSize );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* An acknowledge was received. See if some outstanding data may be removed
* from the transmission queue(s).
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static uint32_t prvTCPWindowTxCheckAck( TCPWindow_t *pxWindow, uint32_t ulFirst, uint32_t ulLast );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*
* A higher Tx block has been acknowledged. Now iterate through the xWaitQueue
* to find a possible condition for a FAST retransmission.
*/
#if( ipconfigUSE_TCP_WIN == 1 )
static uint32_t prvTCPWindowFastRetransmit( TCPWindow_t *pxWindow, uint32_t ulFirst );
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
/* TCP segement pool. */
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPSegments = NULL;
#endif /* ipconfigUSE_TCP_WIN == 1 */
/* List of free TCP segments. */
#if( ipconfigUSE_TCP_WIN == 1 )
static List_t xSegmentList;
#endif
/* Logging verbosity level. */
BaseType_t xTCPWindowLoggingLevel = 0;
/* Some 32-bit arithmetic: comparing sequence numbers */
static portINLINE BaseType_t xSequenceLessThanOrEqual( uint32_t a, uint32_t b )
{
/* Test if a <= b
Return true if the unsigned subtraction of (b-a) doesn't generate an
arithmetic overflow. */
return ( ( b - a ) & 0x80000000U ) == 0;
}
/*-----------------------------------------------------------*/
static portINLINE BaseType_t xSequenceLessThan( uint32_t a, uint32_t b )
{
/* Test if a < b */
return ( ( b - a - 1 ) & 0x80000000U ) == 0;
}
/*-----------------------------------------------------------*/
static portINLINE BaseType_t xSequenceGreaterThan( uint32_t a, uint32_t b )
{
/* Test if a > b */
return ( ( a - b - 1 ) & 0x80000000U ) == 0;
}
/*-----------------------------------------------------------*/
static portINLINE BaseType_t xSequenceGreaterThanOrEqual( uint32_t a, uint32_t b )
{
/* Test if a >= b */
return ( ( a - b ) & 0x80000000U ) == 0;
}
/*-----------------------------------------------------------*/
static portINLINE void vListInsertFifo( List_t * const pxList, ListItem_t * const pxNewListItem )
{
vListInsertGeneric( pxList, pxNewListItem, &pxList->xListEnd );
}
/*-----------------------------------------------------------*/
static portINLINE void vListInsertStack( List_t * const pxList, ListItem_t * const pxNewListItem )
{
vListInsertGeneric( pxList, pxNewListItem, (MiniListItem_t *)pxList->xListEnd.pxNext );
}
/*-----------------------------------------------------------*/
static portINLINE void vTCPTimerSet( TcpTimer_t *pxTimer )
{
pxTimer->ulBorn = xTaskGetTickCount ( );
}
/*-----------------------------------------------------------*/
static portINLINE uint32_t ulTimerGetAge( TcpTimer_t *pxTimer )
{
return ( ( xTaskGetTickCount() - pxTimer->ulBorn ) * portTICK_PERIOD_MS );
}
/*-----------------------------------------------------------*/
/* _HT_ GCC (using the settings that I'm using) checks for every public function if it is
preceded by a prototype. Later this prototype will be located in list.h? */
extern void vListInsertGeneric( List_t * const pxList, ListItem_t * const pxNewListItem, MiniListItem_t * const pxWhere );
void vListInsertGeneric( List_t * const pxList, ListItem_t * const pxNewListItem, MiniListItem_t * const pxWhere )
{
/* Insert a new list item into pxList, it does not sort the list,
but it puts the item just before xListEnd, so it will be the last item
returned by listGET_HEAD_ENTRY() */
pxNewListItem->pxNext = (struct xLIST_ITEM * configLIST_VOLATILE)pxWhere;
pxNewListItem->pxPrevious = pxWhere->pxPrevious;
pxWhere->pxPrevious->pxNext = pxNewListItem;
pxWhere->pxPrevious = pxNewListItem;
/* Remember which list the item is in. */
pxNewListItem->pvContainer = ( void * ) pxList;
( pxList->uxNumberOfItems )++;
}
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
static BaseType_t prvCreateSectors( void )
{
BaseType_t xIndex, xReturn;
/* Allocate space for 'xTCPSegments' and store them in 'xSegmentList'. */
vListInitialise( &xSegmentList );
xTCPSegments = ( TCPSegment_t * ) pvPortMallocLarge( ipconfigTCP_WIN_SEG_COUNT * sizeof( xTCPSegments[ 0 ] ) );
if( xTCPSegments == NULL )
{
FreeRTOS_debug_printf( ( "prvCreateSectors: malloc %lu failed\n",
ipconfigTCP_WIN_SEG_COUNT * sizeof( xTCPSegments[ 0 ] ) ) );
xReturn = pdFAIL;
}
else
{
/* Clear the allocated space. */
memset( xTCPSegments, '\0', ipconfigTCP_WIN_SEG_COUNT * sizeof( xTCPSegments[ 0 ] ) );
for( xIndex = 0; xIndex < ipconfigTCP_WIN_SEG_COUNT; xIndex++ )
{
/* Could call vListInitialiseItem here but all data has been
nulled already. Set the owner to a segment descriptor. */
listSET_LIST_ITEM_OWNER( &( xTCPSegments[ xIndex ].xListItem ), ( void* ) &( xTCPSegments[ xIndex ] ) );
listSET_LIST_ITEM_OWNER( &( xTCPSegments[ xIndex ].xQueueItem ), ( void* ) &( xTCPSegments[ xIndex ] ) );
/* And add it to the pool of available segments */
vListInsertFifo( &xSegmentList, &( xTCPSegments[xIndex].xListItem ) );
}
xReturn = pdPASS;
}
return xReturn;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowRxFind( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber )
{
const ListItem_t *pxIterator;
const MiniListItem_t* pxEnd;
TCPSegment_t *pxSegment, *pxReturn = NULL;
/* Find a segment with a given sequence number in the list of received
segments. */
pxEnd = ( const MiniListItem_t* )listGET_END_MARKER( &pxWindow->xRxSegments );
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( pxEnd );
pxIterator != ( const ListItem_t * ) pxEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
pxSegment = ( TCPSegment_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
if( pxSegment->ulSequenceNumber == ulSequenceNumber )
{
pxReturn = pxSegment;
break;
}
}
return pxReturn;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowNew( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber, int32_t lCount, BaseType_t xIsForRx )
{
TCPSegment_t *pxSegment;
ListItem_t * pxItem;
configASSERT( listLIST_IS_EMPTY( &xSegmentList ) == pdFALSE );
/* Allocate a new segment. The socket will borrow all segments from a
common pool: 'xSegmentList', which is a list of 'TCPSegment_t' */
if( listLIST_IS_EMPTY( &xSegmentList ) != pdFALSE )
{
/* If the TCP-stack runs out of segments, you might consider
increasing 'ipconfigTCP_WIN_SEG_COUNT'. */
FreeRTOS_debug_printf( ( "xTCPWindow%cxNew: Error: all segments occupied\n", xIsForRx ? 'R' : 'T' ) );
pxSegment = NULL;
}
else
{
/* Pop the item at the head of the list. Semaphore protection is
not required as only the IP task will call these functions. */
pxItem = ( ListItem_t * ) listGET_HEAD_ENTRY( &xSegmentList );
pxSegment = ( TCPSegment_t * ) listGET_LIST_ITEM_OWNER( pxItem );
configASSERT( pxItem != NULL );
configASSERT( pxSegment != NULL );
/* Remove the item from xSegmentList. */
uxListRemove( pxItem );
/* Add it to either the connections' Rx or Tx queue. */
vListInsertFifo( xIsForRx ? &pxWindow->xRxSegments : &pxWindow->xTxSegments, pxItem );
/* And set the segment's timer to zero */
vTCPTimerSet( &pxSegment->xTransmitTimer );
pxSegment->u.ulFlags = 0;
pxSegment->u.bits.bIsForRx = ( xIsForRx != 0 );
pxSegment->lMaxLength = lCount;
pxSegment->lDataLength = lCount;
pxSegment->ulSequenceNumber = ulSequenceNumber;
}
return pxSegment;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
BaseType_t xTCPWindowRxEmpty( TCPWindow_t *pxWindow )
{
BaseType_t xReturn;
/* When the peer has a close request (FIN flag), the driver will check
if there are missing packets in the Rx-queue. It will accept the
closure of the connection if both conditions are true:
- the Rx-queue is empty
- the highest Rx sequence number has been ACK'ed */
if( listLIST_IS_EMPTY( ( &pxWindow->xRxSegments ) ) == pdFALSE )
{
/* Rx data has been stored while earlier packets were missing. */
xReturn = pdFALSE;
}
else if( xSequenceGreaterThanOrEqual( pxWindow->rx.ulCurrentSequenceNumber, pxWindow->rx.ulHighestSequenceNumber ) != pdFALSE )
{
/* No Rx packets are being stored and the highest sequence number
that has been received has been ACKed. */
xReturn = pdTRUE;
}
else
{
FreeRTOS_debug_printf( ( "xTCPWindowRxEmpty: cur %lu highest %lu (empty)\n",
( pxWindow->rx.ulCurrentSequenceNumber - pxWindow->rx.ulFirstSequenceNumber ),
( pxWindow->rx.ulHighestSequenceNumber - pxWindow->rx.ulFirstSequenceNumber ) ) );
xReturn = pdFALSE;
}
return xReturn;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowGetHead( List_t *pxList )
{
TCPSegment_t *pxSegment;
ListItem_t * pxItem;
/* Detaches and returns the head of a queue. */
if( listLIST_IS_EMPTY( pxList ) != pdFALSE )
{
pxSegment = NULL;
}
else
{
pxItem = ( ListItem_t * ) listGET_HEAD_ENTRY( pxList );
pxSegment = ( TCPSegment_t * ) listGET_LIST_ITEM_OWNER( pxItem );
uxListRemove( pxItem );
}
return pxSegment;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowPeekHead( List_t *pxList )
{
ListItem_t *pxItem;
TCPSegment_t *pxReturn;
/* Returns the head of a queue but it won't be detached. */
if( listLIST_IS_EMPTY( pxList ) != pdFALSE )
{
pxReturn = NULL;
}
else
{
pxItem = ( ListItem_t * ) listGET_HEAD_ENTRY( pxList );
pxReturn = ( TCPSegment_t * ) listGET_LIST_ITEM_OWNER( pxItem );
}
return pxReturn;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
static void vTCPWindowFree( TCPSegment_t *pxSegment )
{
/* Free entry pxSegment because it's not used any more. The ownership
will be passed back to the segment pool.
Unlink it from one of the queues, if any. */
if( listLIST_ITEM_CONTAINER( &( pxSegment->xQueueItem ) ) != NULL )
{
uxListRemove( &( pxSegment->xQueueItem ) );
}
pxSegment->ulSequenceNumber = 0;
pxSegment->lDataLength = 0;
pxSegment->u.ulFlags = 0;
/* Take it out of xRxSegments/xTxSegments */
if( listLIST_ITEM_CONTAINER( &( pxSegment->xListItem ) ) != NULL )
{
uxListRemove( &( pxSegment->xListItem ) );
}
/* Return it to xSegmentList */
vListInsertFifo( &xSegmentList, &( pxSegment->xListItem ) );
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
void vTCPWindowDestroy( TCPWindow_t *pxWindow )
{
List_t * pxSegments;
BaseType_t xRound;
TCPSegment_t *pxSegment;
/* Destroy a window. A TCP window doesn't serve any more. Return all
owned segments to the pool. In order to save code, it will make 2 rounds,
one to remove the segments from xRxSegments, and a second round to clear
xTxSegments*/
for( xRound = 0; xRound < 2; xRound++ )
{
if( xRound != 0 )
{
pxSegments = &( pxWindow->xRxSegments );
}
else
{
pxSegments = &( pxWindow->xTxSegments );
}
if( listLIST_IS_INITIALISED( pxSegments ) != pdFALSE )
{
while( listCURRENT_LIST_LENGTH( pxSegments ) > 0U )
{
pxSegment = ( TCPSegment_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxSegments );
vTCPWindowFree( pxSegment );
}
}
}
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
void vTCPWindowCreate( TCPWindow_t *pxWindow, uint32_t ulRxWindowLength,
uint32_t ulTxWindowLength, uint32_t ulAckNumber, uint32_t ulSequenceNumber, uint32_t ulMSS )
{
/* Create and initialize a window. */
#if( ipconfigUSE_TCP_WIN == 1 )
{
if( xTCPSegments == NULL )
{
prvCreateSectors();
}
vListInitialise( &pxWindow->xTxSegments );
vListInitialise( &pxWindow->xRxSegments );
vListInitialise( &pxWindow->xPriorityQueue ); /* Priority queue: segments which must be sent immediately */
vListInitialise( &pxWindow->xTxQueue ); /* Transmit queue: segments queued for transmission */
vListInitialise( &pxWindow->xWaitQueue ); /* Waiting queue: outstanding segments */
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
if( xTCPWindowLoggingLevel != 0 )
{
FreeRTOS_debug_printf( ( "vTCPWindowCreate: for WinLen = Rx/Tx: %lu/%lu\n",
ulRxWindowLength, ulTxWindowLength ) );
}
pxWindow->xSize.ulRxWindowLength = ulRxWindowLength;
pxWindow->xSize.ulTxWindowLength = ulTxWindowLength;
vTCPWindowInit( pxWindow, ulAckNumber, ulSequenceNumber, ulMSS );
}
/*-----------------------------------------------------------*/
void vTCPWindowInit( TCPWindow_t *pxWindow, uint32_t ulAckNumber, uint32_t ulSequenceNumber, uint32_t ulMSS )
{
const int32_t l500ms = 500;
pxWindow->u.ulFlags = 0;
pxWindow->u.bits.bHasInit = pdTRUE;
if( ulMSS != 0 )
{
if( pxWindow->usMSSInit != 0 )
{
pxWindow->usMSSInit = ( uint16_t ) ulMSS;
}
if( ( ulMSS < ( uint32_t ) pxWindow->usMSS ) || ( pxWindow->usMSS == 0 ) )
{
pxWindow->xSize.ulRxWindowLength = ( pxWindow->xSize.ulRxWindowLength / ulMSS ) * ulMSS;
pxWindow->usMSS = ( uint16_t ) ulMSS;
}
}
#if( ipconfigUSE_TCP_WIN == 0 )
{
pxWindow->xTxSegment.lMaxLength = pxWindow->usMSS;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*Start with a timeout of 2 * 500 ms (1 sec). */
pxWindow->lSRTT = l500ms;
/* Just for logging, to print relative sequence numbers. */
pxWindow->rx.ulFirstSequenceNumber = ulAckNumber;
/* The segment asked for in the next transmission. */
pxWindow->rx.ulCurrentSequenceNumber = ulAckNumber;
/* The right-hand side of the receive window. */
pxWindow->rx.ulHighestSequenceNumber = ulAckNumber;
pxWindow->tx.ulFirstSequenceNumber = ulSequenceNumber;
/* The segment asked for in next transmission. */
pxWindow->tx.ulCurrentSequenceNumber = ulSequenceNumber;
/* The sequence number given to the next outgoing byte to be added is
maintained by lTCPWindowTxAdd(). */
pxWindow->ulNextTxSequenceNumber = ulSequenceNumber;
/* The right-hand side of the transmit window. */
pxWindow->tx.ulHighestSequenceNumber = ulSequenceNumber;
pxWindow->ulOurSequenceNumber = ulSequenceNumber;
}
/*-----------------------------------------------------------*/
/*=============================================================================
*
* ###### # #
* # # # #
* # # # #
* # # ####
* ###### ##
* # ## ####
* # # # #
* # # # #
* ### ## # #
* Rx functions
*
*=============================================================================*/
#if( ipconfigUSE_TCP_WIN == 1 )
static TCPSegment_t *xTCPWindowRxConfirm( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber, uint32_t ulLength )
{
TCPSegment_t *pxBest = NULL;
const ListItem_t *pxIterator;
uint32_t ulNextSequenceNumber = ulSequenceNumber + ulLength;
const MiniListItem_t* pxEnd = ( const MiniListItem_t* ) listGET_END_MARKER( &pxWindow->xRxSegments );
TCPSegment_t *pxSegment;
/* A segment has been received with sequence number 'ulSequenceNumber',
where 'ulCurrentSequenceNumber == ulSequenceNumber', which means that
exactly this segment was expected. xTCPWindowRxConfirm() will check if
there is already another segment with a sequence number between (ulSequenceNumber)
and (ulSequenceNumber+ulLength). Normally none will be found, because
the next RX segment should have a sequence number equal to
'(ulSequenceNumber+ulLength)'. */
/* Iterate through all RX segments that are stored: */
for( pxIterator = ( const ListItem_t * ) listGET_NEXT( pxEnd );
pxIterator != ( const ListItem_t * ) pxEnd;
pxIterator = ( const ListItem_t * ) listGET_NEXT( pxIterator ) )
{
pxSegment = ( TCPSegment_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
/* And see if there is a segment for which:
'ulSequenceNumber' <= 'pxSegment->ulSequenceNumber' < 'ulNextSequenceNumber'
If there are more matching segments, the one with the lowest sequence number
shall be taken */
if( ( xSequenceGreaterThanOrEqual( pxSegment->ulSequenceNumber, ulSequenceNumber ) != 0 ) &&
( xSequenceLessThan( pxSegment->ulSequenceNumber, ulNextSequenceNumber ) != 0 ) )
{
if( ( pxBest == NULL ) || ( xSequenceLessThan( pxSegment->ulSequenceNumber, pxBest->ulSequenceNumber ) != 0 ) )
{
pxBest = pxSegment;
}
}
}
if( ( pxBest != NULL ) &&
( ( pxBest->ulSequenceNumber != ulSequenceNumber ) || ( pxBest->lDataLength != ( int32_t ) ulLength ) ) )
{
FreeRTOS_flush_logging();
FreeRTOS_debug_printf( ( "xTCPWindowRxConfirm[%u]: search %lu (+%ld=%lu) found %lu (+%ld=%lu)\n",
pxWindow->usPeerPortNumber,
ulSequenceNumber - pxWindow->rx.ulFirstSequenceNumber,
ulLength,
ulSequenceNumber + ulLength - pxWindow->rx.ulFirstSequenceNumber,
pxBest->ulSequenceNumber - pxWindow->rx.ulFirstSequenceNumber,
pxBest->lDataLength,
pxBest->ulSequenceNumber + ( ( uint32_t ) pxBest->lDataLength ) - pxWindow->rx.ulFirstSequenceNumber ) );
}
return pxBest;
}
#endif /* ipconfgiUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
int32_t lTCPWindowRxCheck( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber, uint32_t ulLength, uint32_t ulSpace )
{
uint32_t ulCurrentSequenceNumber, ulLast, ulSavedSequenceNumber;
int32_t lReturn, lDistance;
TCPSegment_t *pxFound;
/* If lTCPWindowRxCheck( ) returns == 0, the packet will be passed
directly to user (segment is expected). If it returns a positive
number, an earlier packet is missing, but this packet may be stored.
If negative, the packet has already been stored, or it is out-of-order,
or there is not enough space.
As a side-effect, pxWindow->ulUserDataLength will get set to non-zero,
if more Rx data may be passed to the user after this packet. */
ulCurrentSequenceNumber = pxWindow->rx.ulCurrentSequenceNumber;
/* For Selective Ack (SACK), used when out-of-sequence data come in. */
pxWindow->ucOptionLength = 0;
/* Non-zero if TCP-windows contains data which must be popped. */
pxWindow->ulUserDataLength = 0;
if( ulCurrentSequenceNumber == ulSequenceNumber )
{
/* This is the packet with the lowest sequence number we're waiting
for. It can be passed directly to the rx stream. */
if( ulLength > ulSpace )
{
FreeRTOS_debug_printf( ( "lTCPWindowRxCheck: Refuse %lu bytes, due to lack of space (%lu)\n", ulLength, ulSpace ) );
lReturn = -1;
}
else
{
ulCurrentSequenceNumber += ulLength;
if( listCURRENT_LIST_LENGTH( &( pxWindow->xRxSegments ) ) != 0 )
{
ulSavedSequenceNumber = ulCurrentSequenceNumber;
/* See if (part of) this segment has been stored already,
but this rarely happens. */
pxFound = xTCPWindowRxConfirm( pxWindow, ulSequenceNumber, ulLength );
if( pxFound != NULL )
{
ulCurrentSequenceNumber = pxFound->ulSequenceNumber + ( ( uint32_t ) pxFound->lDataLength );
/* Remove it because it will be passed to user directly. */
vTCPWindowFree( pxFound );
}
/* Check for following segments that are already in the
queue and increment ulCurrentSequenceNumber. */
while( ( pxFound = xTCPWindowRxFind( pxWindow, ulCurrentSequenceNumber ) ) != NULL )
{
ulCurrentSequenceNumber += ( uint32_t ) pxFound->lDataLength;
/* As all packet below this one have been passed to the
user it can be discarded. */
vTCPWindowFree( pxFound );
}
if( ulSavedSequenceNumber != ulCurrentSequenceNumber )
{
/* After the current data-package, there is more data
to be popped. */
pxWindow->ulUserDataLength = ulCurrentSequenceNumber - ulSavedSequenceNumber;
if( xTCPWindowLoggingLevel >= 1 )
{
FreeRTOS_debug_printf( ( "lTCPWindowRxCheck[%d,%d]: retran %lu (Found %lu bytes at %lu cnt %ld)\n",
pxWindow->usPeerPortNumber, pxWindow->usOurPortNumber,
ulSequenceNumber - pxWindow->rx.ulFirstSequenceNumber,
pxWindow->ulUserDataLength,
ulSavedSequenceNumber - pxWindow->rx.ulFirstSequenceNumber,
listCURRENT_LIST_LENGTH( &pxWindow->xRxSegments ) ) );
}
}
}
pxWindow->rx.ulCurrentSequenceNumber = ulCurrentSequenceNumber;
/* Packet was expected, may be passed directly to the socket
buffer or application. Store the packet at offset 0. */
lReturn = 0;
}
}
else if( ulCurrentSequenceNumber == ( ulSequenceNumber + 1 ) )
{
/* Looks like a TCP keep-alive message. Do not accept/store Rx data
ulUserDataLength = 0. Not packet out-of-sync. Just reply to it. */
lReturn = -1;
}
else
{
/* The packet is not the one expected. See if it falls within the Rx
window so it can be stored. */
/* An "out-of-sequence" segment was received, must have missed one.
Prepare a SACK (Selective ACK). */
ulLast = ulSequenceNumber + ulLength;
lDistance = ( int32_t ) ( ulLast - ulCurrentSequenceNumber );
if( lDistance <= 0 )
{
/* An earlier has been received, must be a retransmission of a
packet that has been accepted already. No need to send out a
Selective ACK (SACK). */
lReturn = -1;
}
else if( lDistance > ( int32_t ) ulSpace )
{
/* The new segment is ahead of rx.ulCurrentSequenceNumber. The
sequence number of this packet is too far ahead, ignore it. */
FreeRTOS_debug_printf( ( "lTCPWindowRxCheck: Refuse %lu+%lu bytes, due to lack of space (%lu)\n", lDistance, ulLength, ulSpace ) );
lReturn = -1;
}
else
{
/* See if there is more data in a contiguous block to make the
SACK describe a longer range of data. */
/* TODO: SACK's may also be delayed for a short period
* This is useful because subsequent packets will be SACK'd with
* single one message
*/
while( ( pxFound = xTCPWindowRxFind( pxWindow, ulLast ) ) != NULL )
{
ulLast += ( uint32_t ) pxFound->lDataLength;
}
if( xTCPWindowLoggingLevel >= 1 )
{
FreeRTOS_debug_printf( ( "lTCPWindowRxCheck[%d,%d]: seqnr %lu exp %lu (dist %ld) SACK to %lu\n",
pxWindow->usPeerPortNumber, pxWindow->usOurPortNumber,
ulSequenceNumber - pxWindow->rx.ulFirstSequenceNumber,
ulCurrentSequenceNumber - pxWindow->rx.ulFirstSequenceNumber,
( BaseType_t ) ( ulSequenceNumber - ulCurrentSequenceNumber ), /* want this signed */
ulLast - pxWindow->rx.ulFirstSequenceNumber ) );
}
/* Now prepare the SACK message. */
pxWindow->ulOptionsData[0] = FreeRTOS_htonl( OPTION_CODE_SINGLE_SACK );
/* First sequence number that we received. */
pxWindow->ulOptionsData[1] = FreeRTOS_htonl( ulSequenceNumber );
/* Last + 1 */
pxWindow->ulOptionsData[2] = FreeRTOS_htonl( ulLast );
/* Which make 12 (3*4) option bytes. */
pxWindow->ucOptionLength = 3 * sizeof( pxWindow->ulOptionsData[ 0 ] );
pxFound = xTCPWindowRxFind( pxWindow, ulSequenceNumber );
if( pxFound != NULL )
{
/* This out-of-sequence packet has been received for a
second time. It is already stored but do send a SACK
again. */
lReturn = -1;
}
else
{
pxFound = xTCPWindowRxNew( pxWindow, ulSequenceNumber, ( int32_t ) ulLength );
if( pxFound == NULL )
{
/* Can not send a SACK, because the segment cannot be
stored. */
pxWindow->ucOptionLength = 0;
/* Needs to be stored but there is no segment
available. */
lReturn = -1;
}
else
{
if( xTCPWindowLoggingLevel != 0 )
{
FreeRTOS_debug_printf( ( "lTCPWindowRxCheck[%u,%u]: seqnr %lu (cnt %lu)\n",
pxWindow->usPeerPortNumber, pxWindow->usOurPortNumber, ulSequenceNumber - pxWindow->rx.ulFirstSequenceNumber,
listCURRENT_LIST_LENGTH( &pxWindow->xRxSegments ) ) );
FreeRTOS_flush_logging( );
}
/* Return a positive value. The packet may be accepted
and stored but an earlier packet is still missing. */
lReturn = ( int32_t ) ( ulSequenceNumber - ulCurrentSequenceNumber );
}
}
}
}
return lReturn;
}
#endif /* ipconfgiUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
/*=============================================================================
*
* ######### # #
* # # # # #
* # # #
* # ####
* # ##
* # ####
* # # #
* # # #
* ##### # #
*
* Tx functions
*
*=============================================================================*/
#if( ipconfigUSE_TCP_WIN == 1 )
static int32_t lTCPIncrementTxPosition( int32_t lPosition, int32_t lMax, int32_t lCount )
{
/* +TCP stores data in circular buffers. Calculate the next position to
store. */
lPosition += lCount;
if( lPosition >= lMax )
{
lPosition -= lMax;
}
return lPosition;
}
#endif /* ipconfigUSE_TCP_WIN == 1 */
/*-----------------------------------------------------------*/
#if( ipconfigUSE_TCP_WIN == 1 )
int32_t lTCPWindowTxAdd( TCPWindow_t *pxWindow, uint32_t ulLength, int32_t lPosition, int32_t lMax )
{
int32_t lBytesLeft = ( int32_t ) ulLength, lToWrite;
int32_t lDone = 0;
TCPSegment_t *pxSegment = pxWindow->pxHeadSegment;
/* Puts a message in the Tx-window (after buffer size has been
verified). */
if( pxSegment != NULL )
{