Kernel - Slight reworks to timer code
[tpg/acess2.git] / Modules / IPStack / tcp.h
index ed517db..6aa404f 100644 (file)
@@ -6,9 +6,11 @@
 #define _TCP_H_
 
 #include "ipstack.h"
+#include <adt.h>       // tRingBuffer
 
 typedef struct sTCPHeader      tTCPHeader;
 typedef struct sTCPListener    tTCPListener;
+typedef struct sTCPStoredPacket        tTCPStoredPacket;
 typedef struct sTCPConnection  tTCPConnection;
 
 struct sTCPHeader
@@ -18,7 +20,7 @@ struct sTCPHeader
        Uint32  SequenceNumber;
        Uint32  AcknowlegementNumber;
        #if 0
-       struct {
+       struct {        // Lowest to highest
                unsigned Reserved:      4;
                unsigned DataOffset: 4; // Size of the header in 32-bit words
        } __attribute__ ((packed));
@@ -66,35 +68,93 @@ struct sTCPListener
        tInterface      *Interface;     //!< Listening Interface
        tVFS_Node       Node;   //!< Server Directory node
         int    NextID;         //!< Name of the next connection
-       tSpinlock       lConnections;   //!< Spinlock for connections
+       tShortSpinlock  lConnections;   //!< Spinlock for connections
        tTCPConnection  *Connections;   //!< Connections (linked list)
-       volatile tTCPConnection *NewConnections;
+       tTCPConnection  *volatile NewConnections;
+       tTCPConnection  *ConnectionsTail;
+};
+
+struct sTCPStoredPacket
+{
+       struct sTCPStoredPacket *Next;
+       size_t  Length;
+       Uint32  Sequence;
+       Uint8   Data[];
+};
+
+enum eTCPConnectionState
+{
+       TCP_ST_CLOSED,          // 0 - Connection invalid
+       
+       TCP_ST_SYN_SENT,        // 1 - SYN sent by local, waiting for SYN-ACK
+       TCP_ST_SYN_RCVD,        // 2 - SYN recieved, SYN-ACK sent
+       
+       TCP_ST_OPEN,            // 3 - Connection open
+       
+       // Local Close
+       TCP_ST_FIN_WAIT1,       // 4 - FIN sent, waiting for reply (ACK or FIN)
+       TCP_ST_FIN_WAIT2,       // 5 - sent FIN acked, waiting for FIN from peer
+       TCP_ST_CLOSING,         // 6 - Waiting for ACK of FIN (FIN sent and recieved)
+       TCP_ST_TIME_WAIT,       // 7 - Waiting for timeout after local close
+       // Remote close
+       TCP_ST_CLOSE_WAIT,      // 8 - FIN recieved, waiting for user to close (error set, wait for node close)
+       TCP_ST_LAST_ACK,        // 9 - FIN sent and recieved, waiting for ACK
+       TCP_ST_FINISHED         // 10 - Essentially closed, all packets are invalid
 };
 
 struct sTCPConnection
 {
        struct sTCPConnection   *Next;
-        int    State;  //!< Connection state (see ::eTCPConnectionState)
+       enum eTCPConnectionState        State;  //!< Connection state (see ::eTCPConnectionState)
        Uint16  LocalPort;      //!< Local port
        Uint16  RemotePort;     //!< Remote port
        tInterface      *Interface;     //!< Listening Interface
        tVFS_Node       Node;   //!< Node
        
-        int    NextSequenceSend;       //!< Next sequence value for outbound packets
-        int    NextSequenceRcv;        //!< Next expected sequence value for inbound
+       Uint32  NextSequenceSend;       //!< Next sequence value for outbound packets
+       Uint32  NextSequenceRcv;        //!< Next expected sequence value for inbound
        
-        int    nQueuedPackets; //!< Number of packets not ACKed
-       struct {
-                int    Sequence;
-               void    *Data;
-       }       *QueuedPackets; //!< Non-ACKed packets
+       #if 0
+       /**
+        * \brief Non-ACKed packets
+        * \note Ring buffer
+        * \{
+        */
+       tMutex  lNonACKedPackets;
+       tTCPStoredPacket        *SentPackets;   //!< Non-acknowleged packets
+       /**
+        * \}
+        */
+       #endif
        
+       /**
+        * \brief Unread Packets
+        * \note Ring buffer
+        * \{
+        */
+       tMutex  lRecievedPackets;
+       tRingBuffer     *RecievedBuffer;
+       /**
+        * \}
+        */
        
-        int    nFuturePackets; //!< Number of packets recieved that are out of sequence
-       struct {
-                int    SequenceNum;
-               void    *Data;
-       }       **FuturePackets;        //!< Out of sequence packets
+       /**
+        * \brief Out of sequence packets
+        * \note Sorted list to improve times
+        * \todo Convert this to a ring buffer and a bitmap of valid bytes
+        * \{
+        */
+       #if CACHE_FUTURE_PACKETS_OR_BYTES == bytes
+       Uint32  HighestSequenceRcvd;    //!< Highest sequence number (within window) recieved
+       Uint8   *FuturePacketData;      //!< Future packet data (indexed by sequence number)
+       Uint8   *FuturePacketValidBytes;        //!< Valid byte bitmap (WINDOW_SIZE/8 bytes)
+       #else
+       tShortSpinlock  lFuturePackets; //!< Future packets spinlock
+       tTCPStoredPacket        *FuturePackets; //!< Out of sequence packets
+       #endif
+       /**
+        * \}
+        */
        
        union {
                tIPv4   v4;
@@ -103,11 +163,4 @@ struct sTCPConnection
        // Type is determined by LocalInterface->Type
 };
 
-enum eTCPConnectionState
-{
-       TCP_ST_CLOSED,
-       TCP_ST_HALFOPEN,
-       TCP_ST_OPEN
-};
-
 #endif

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