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