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