IPStack/tcp - Added deferred ACK support
[tpg/acess2.git] / KernelLand / Modules / IPStack / tcp.h
1 /*
2  * Acess2 IP Stack
3  * - TCP Definitions
4  */
5 #ifndef _TCP_H_
6 #define _TCP_H_
7
8 #include "ipstack.h"
9 #include <adt.h>        // tRingBuffer
10 #include <timers.h>     // tTimer
11
12 typedef struct sTCPHeader       tTCPHeader;
13 typedef struct sTCPListener     tTCPListener;
14 typedef struct sTCPStoredPacket tTCPStoredPacket;
15 typedef struct sTCPConnection   tTCPConnection;
16
17 struct sTCPHeader
18 {
19         Uint16  SourcePort;
20         Uint16  DestPort;
21         Uint32  SequenceNumber;
22         Uint32  AcknowlegementNumber;
23         #if 0
24         struct {        // Lowest to highest
25                 unsigned Reserved:      4;
26                 unsigned DataOffset: 4; // Size of the header in 32-bit words
27         } __attribute__ ((packed));
28         #else
29         Uint8   DataOffset;
30         #endif
31         #if 0
32         struct {        // Lowest to Highest
33                 unsigned FIN:   1;      // Last packet
34                 unsigned SYN:   1;      // Synchronise Sequence Numbers
35                 unsigned RST:   1;      // Reset Connection
36                 unsigned PSH:   1;      // Push Function
37                 unsigned ACK:   1;      // Acknowlegement field is significant
38                 unsigned URG:   1;      // Urgent pointer is significant
39                 unsigned ECE:   1;      // ECN-Echo
40                 unsigned CWR:   1;      // Congestion Window Reduced
41         } __attribute__ ((packed)) Flags;
42         #else
43         Uint8   Flags;
44         #endif
45         Uint16  WindowSize;
46         
47         Uint16  Checksum;
48         Uint16  UrgentPointer;
49         
50         Uint8   Options[];
51 } __attribute__ ((packed));
52
53 enum eTCPFlags
54 {
55         TCP_FLAG_FIN    = 0x01,
56         TCP_FLAG_SYN    = 0x02,
57         TCP_FLAG_RST    = 0x04,
58         TCP_FLAG_PSH    = 0x08,
59         TCP_FLAG_ACK    = 0x10,
60         TCP_FLAG_URG    = 0x20,
61         TCP_FLAG_ECE    = 0x40,
62         TCP_FLAG_CWR    = 0x80
63 };
64
65 struct sTCPListener
66 {
67         struct sTCPListener     *Next;  //!< Next server in the list
68         Uint16  Port;           //!< Listening port (0 disables the server)
69         tInterface      *Interface;     //!< Listening Interface
70         tVFS_Node       Node;   //!< Server Directory node
71          int    NextID;         //!< Name of the next connection
72         tShortSpinlock  lConnections;   //!< Spinlock for connections
73         tTCPConnection  *Connections;   //!< Connections (linked list)
74         tTCPConnection  *volatile NewConnections;
75         tTCPConnection  *ConnectionsTail;
76 };
77
78 struct sTCPStoredPacket
79 {
80         struct sTCPStoredPacket *Next;
81         size_t  Length;
82         Uint32  Sequence;
83         Uint8   Data[];
84 };
85
86 enum eTCPConnectionState
87 {
88         TCP_ST_CLOSED,          // 0 - Connection invalid
89         
90         TCP_ST_SYN_SENT,        // 1 - SYN sent by local, waiting for SYN-ACK
91         TCP_ST_SYN_RCVD,        // 2 - SYN recieved, SYN-ACK sent
92         
93         TCP_ST_OPEN,            // 3 - Connection open
94         
95         // Local Close
96         TCP_ST_FIN_WAIT1,       // 4 - FIN sent, waiting for reply (ACK or FIN)
97         TCP_ST_FIN_WAIT2,       // 5 - sent FIN acked, waiting for FIN from peer
98         TCP_ST_CLOSING,         // 6 - Waiting for ACK of FIN (FIN sent and recieved)
99         TCP_ST_TIME_WAIT,       // 7 - Waiting for timeout after local close
100         // Remote close
101         TCP_ST_CLOSE_WAIT,      // 8 - FIN recieved, waiting for user to close (error set, wait for node close)
102         TCP_ST_LAST_ACK,        // 9 - FIN sent and recieved, waiting for ACK
103         TCP_ST_FINISHED         // 10 - Essentially closed, all packets are invalid
104 };
105
106 struct sTCPConnection
107 {
108         struct sTCPConnection   *Next;
109         enum eTCPConnectionState        State;  //!< Connection state (see ::eTCPConnectionState)
110         Uint16  LocalPort;      //!< Local port
111         Uint16  RemotePort;     //!< Remote port
112         tInterface      *Interface;     //!< Listening Interface
113         tVFS_Node       Node;   //!< Node
114         
115         Uint32  NextSequenceSend;       //!< Next sequence value for outbound packets
116         Uint32  NextSequenceRcv;        //!< Next expected sequence value for inbound
117
118         // Deferred ACK handling
119         Uint32  LastACKSequence;
120         tTimer  *DeferredACKTimer;      
121
122         #if 0
123         /**
124          * \brief Non-ACKed packets
125          * \note Ring buffer
126          * \{
127          */
128         tMutex  lNonACKedPackets;
129         tTCPStoredPacket        *SentPackets;   //!< Non-acknowleged packets
130         /**
131          * \}
132          */
133         #endif
134         
135         /**
136          * \brief Unread Packets
137          * \note Ring buffer
138          * \{
139          */
140         tMutex  lRecievedPackets;
141         tRingBuffer     *RecievedBuffer;
142         /**
143          * \}
144          */
145         
146         /**
147          * \brief Out of sequence packets
148          * \note Sorted list to improve times
149          * \todo Convert this to a ring buffer and a bitmap of valid bytes
150          * \{
151          */
152         #if CACHE_FUTURE_PACKETS_OR_BYTES == bytes
153         Uint32  HighestSequenceRcvd;    //!< Highest sequence number (within window) recieved
154         Uint8   *FuturePacketData;      //!< Future packet data (indexed by sequence number)
155         Uint8   *FuturePacketValidBytes;        //!< Valid byte bitmap (WINDOW_SIZE/8 bytes)
156         #else
157         tShortSpinlock  lFuturePackets; //!< Future packets spinlock
158         tTCPStoredPacket        *FuturePackets; //!< Out of sequence packets
159         #endif
160         /**
161          * \}
162          */
163         
164         union {
165                 tIPv4   v4;
166                 tIPv6   v6;
167         } RemoteIP;     //!< Remote IP Address
168         // Type is determined by LocalInterface->Type
169 };
170
171 #endif

UCC git Repository :: git.ucc.asn.au