3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
6 * this software and associated documentation files (the "Software"), to deal in
7 * the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * http://aws.amazon.com/freertos
23 * http://www.FreeRTOS.org
28 * Module which handles the TCP windowing schemes for FreeRTOS-PLUS-TCP
31 #ifndef FREERTOS_TCP_WIN_H
32 #define FREERTOS_TCP_WIN_H
38 extern BaseType_t xTCPWindowLoggingLevel;
40 typedef struct xTCPTimer
45 typedef struct xTCP_SEGMENT
47 uint32_t ulSequenceNumber; /* The sequence number of the first byte in this packet */
48 int32_t lMaxLength; /* Maximum space, number of bytes which can be stored in this segment */
49 int32_t lDataLength; /* Actual number of bytes */
50 int32_t lStreamPos; /* reference to the [t|r]xStream of the socket */
51 TCPTimer_t xTransmitTimer; /* saves a timestamp at the moment this segment gets transmitted (TX only) */
57 ucTransmitCount : 8,/* Number of times the segment has been transmitted, used to calculate the RTT */
58 ucDupAckCount : 8, /* Counts the number of times that a higher segment was ACK'd. After 3 times a Fast Retransmission takes place */
59 bOutstanding : 1, /* It the peer's turn, we're just waiting for an ACK */
60 bAcked : 1, /* This segment has been acknowledged */
61 bIsForRx : 1; /* pdTRUE if segment is used for reception */
65 #if( ipconfigUSE_TCP_WIN != 0 )
66 struct xLIST_ITEM xQueueItem; /* TX only: segments can be linked in one of three queues: xPriorityQueue, xTxQueue, and xWaitQueue */
67 struct xLIST_ITEM xListItem; /* With this item the segment can be connected to a list, depending on who is owning it */
71 typedef struct xTCP_WINSIZE
73 uint32_t ulRxWindowLength;
74 uint32_t ulTxWindowLength;
78 * If TCP time-stamps are being used, they will occupy 12 bytes in
79 * each packet, and thus the message space will become smaller
81 /* Keep this as a multiple of 4 */
82 #if( ipconfigUSE_TCP_WIN == 1 )
83 #define ipSIZE_TCP_OPTIONS 16u
85 #define ipSIZE_TCP_OPTIONS 12u
89 * Every TCP connection owns a TCP window for the administration of all packets
90 * It owns two sets of segment descriptors, incoming and outgoing
92 typedef struct xTCP_WINDOW
99 bHasInit : 1, /* The window structure has been initialised */
100 bSendFullSize : 1, /* May only send packets with a size equal to MSS (for optimisation) */
101 bTimeStamps : 1; /* Socket is supposed to use TCP time-stamps. This depends on the */
102 } bits; /* party which opens the connection */
108 uint32_t ulFirstSequenceNumber; /* Logging & debug: the first segment received/sent in this connection
109 * for Tx: initial send sequence number (ISS)
110 * for Rx: initial receive sequence number (IRS) */
111 uint32_t ulCurrentSequenceNumber;/* Tx/Rx: the oldest sequence number not yet confirmed, also SND.UNA / RCV.NXT
112 * In other words: the sequence number of the left side of the sliding window */
113 uint32_t ulFINSequenceNumber; /* The sequence number which carried the FIN flag */
114 uint32_t ulHighestSequenceNumber;/* Sequence number of the right-most byte + 1 */
116 uint32_t ulOurSequenceNumber; /* The SEQ number we're sending out */
117 uint32_t ulUserDataLength; /* Number of bytes in Rx buffer which may be passed to the user, after having received a 'missing packet' */
118 uint32_t ulNextTxSequenceNumber; /* The sequence number given to the next byte to be added for transmission */
119 int32_t lSRTT; /* Smoothed Round Trip Time, it may increment quickly and it decrements slower */
120 uint8_t ucOptionLength; /* Number of valid bytes in ulOptionsData[] */
121 #if( ipconfigUSE_TCP_WIN == 1 )
122 List_t xPriorityQueue; /* Priority queue: segments which must be sent immediately */
123 List_t xTxQueue; /* Transmit queue: segments queued for transmission */
124 List_t xWaitQueue; /* Waiting queue: outstanding segments */
125 TCPSegment_t *pxHeadSegment; /* points to a segment which has not been transmitted and it's size is still growing (user data being added) */
126 uint32_t ulOptionsData[ipSIZE_TCP_OPTIONS/sizeof(uint32_t)]; /* Contains the options we send out */
127 List_t xTxSegments; /* A linked list of all transmission segments, sorted on sequence number */
128 List_t xRxSegments; /* A linked list of reception segments, order depends on sequence of arrival */
130 /* For tiny TCP, there is only 1 outstanding TX segment */
131 TCPSegment_t xTxSegment; /* Priority queue */
133 uint16_t usOurPortNumber; /* Mostly for debugging/logging: our TCP port number */
134 uint16_t usPeerPortNumber; /* debugging/logging: the peer's TCP port number */
135 uint16_t usMSS; /* Current accepted MSS */
136 uint16_t usMSSInit; /* MSS as configured by the socket owner */
140 /*=============================================================================
142 * Creation and destruction
144 *=============================================================================*/
146 /* Create and initialize a window */
147 void vTCPWindowCreate( TCPWindow_t *pxWindow, uint32_t ulRxWindowLength,
148 uint32_t ulTxWindowLength, uint32_t ulAckNumber, uint32_t ulSequenceNumber, uint32_t ulMSS );
150 /* Destroy a window (always returns NULL)
151 * It will free some resources: a collection of segments */
152 void vTCPWindowDestroy( TCPWindow_t *pxWindow );
154 /* Initialize a window */
155 void vTCPWindowInit( TCPWindow_t *pxWindow, uint32_t ulAckNumber, uint32_t ulSequenceNumber, uint32_t ulMSS );
157 /* Clean up allocated segments. Should only be called when FreeRTOS+TCP will no longer be used. */
158 void vTCPSegmentCleanup( void );
160 /*=============================================================================
164 *=============================================================================*/
166 /* if true may be passed directly to user (segment expected and window is empty)
167 * But pxWindow->ackno should always be used to set "BUF->ackno" */
168 int32_t lTCPWindowRxCheck( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber, uint32_t ulLength, uint32_t ulSpace );
170 /* When lTCPWindowRxCheck returned false, please call store for this unexpected data */
171 BaseType_t xTCPWindowRxStore( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber, uint32_t ulLength );
173 /* This function will be called as soon as a FIN is received. It will return true
174 * if there are no 'open' reception segments */
175 BaseType_t xTCPWindowRxEmpty( TCPWindow_t *pxWindow );
177 /* _HT_ Temporary function for testing/debugging
178 * Not used at this moment */
179 void vTCPWinShowSegments( TCPWindow_t *pxWindow, BaseType_t bForRx );
181 /*=============================================================================
185 *=============================================================================*/
187 /* Adds data to the Tx-window */
188 int32_t lTCPWindowTxAdd( TCPWindow_t *pxWindow, uint32_t ulLength, int32_t lPosition, int32_t lMax );
190 /* Check data to be sent and calculate the time period we may sleep */
191 BaseType_t xTCPWindowTxHasData( TCPWindow_t *pxWindow, uint32_t ulWindowSize, TickType_t *pulDelay );
193 /* See if anything is left to be sent
194 * Function will be called when a FIN has been received. Only when the TX window is clean,
195 * it will return pdTRUE */
196 BaseType_t xTCPWindowTxDone( TCPWindow_t *pxWindow );
198 /* Fetches data to be sent.
199 * apPos will point to a location with the circular data buffer: txStream */
200 uint32_t ulTCPWindowTxGet( TCPWindow_t *pxWindow, uint32_t ulWindowSize, int32_t *plPosition );
202 /* Receive a normal ACK */
203 uint32_t ulTCPWindowTxAck( TCPWindow_t *pxWindow, uint32_t ulSequenceNumber );
205 /* Receive a SACK option */
206 uint32_t ulTCPWindowTxSack( TCPWindow_t *pxWindow, uint32_t ulFirst, uint32_t ulLast );
213 #endif /* FREERTOS_TCP_WIN_H */