PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/src/internet/model/tcp-socket-base.h

https://github.com/scarletttu/ns-3-codel-dev
C Header | 242 lines | 166 code | 22 blank | 54 comment | 0 complexity | 7e701dc756f84a8d2616edc3844b80dc MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2007 Georgia Tech Research Corporation
  4. * Copyright (c) 2010 Adrian Sai-wah Tam
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation;
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
  20. */
  21. #ifndef TCP_SOCKET_BASE_H
  22. #define TCP_SOCKET_BASE_H
  23. #include <stdint.h>
  24. #include <queue>
  25. #include "ns3/callback.h"
  26. #include "ns3/traced-value.h"
  27. #include "ns3/tcp-socket.h"
  28. #include "ns3/ptr.h"
  29. #include "ns3/ipv4-address.h"
  30. #include "ns3/ipv4-header.h"
  31. #include "ns3/ipv4-interface.h"
  32. #include "ns3/event-id.h"
  33. #include "tcp-tx-buffer.h"
  34. #include "tcp-rx-buffer.h"
  35. #include "rtt-estimator.h"
  36. namespace ns3 {
  37. class Ipv4EndPoint;
  38. class Ipv6EndPoint;
  39. class Node;
  40. class Packet;
  41. class TcpL4Protocol;
  42. class TcpHeader;
  43. /**
  44. * \ingroup socket
  45. * \ingroup tcp
  46. *
  47. * \brief A base class for implementation of a stream socket using TCP.
  48. *
  49. * This class contains the essential components of TCP, as well as a sockets
  50. * interface for upper layers to call. This serves as a base for other TCP
  51. * functions where the sliding window mechanism is handled here. This class
  52. * provides connection orientation and sliding window flow control. Part of
  53. * this class is modified from the original NS-3 TCP socket implementation
  54. * (TcpSocketImpl) by Raj Bhattacharjea <raj.b@gatech.edu> of Georgia Tech.
  55. */
  56. class TcpSocketBase : public TcpSocket
  57. {
  58. public:
  59. static TypeId GetTypeId (void);
  60. /**
  61. * Create an unbound TCP socket
  62. */
  63. TcpSocketBase (void);
  64. /**
  65. * Clone a TCP socket, for use upon receiving a connection request in LISTEN state
  66. */
  67. TcpSocketBase (const TcpSocketBase& sock);
  68. virtual ~TcpSocketBase (void);
  69. // Set associated Node, TcpL4Protocol, RttEstimator to this socket
  70. virtual void SetNode (Ptr<Node> node);
  71. virtual void SetTcp (Ptr<TcpL4Protocol> tcp);
  72. virtual void SetRtt (Ptr<RttEstimator> rtt);
  73. // Necessary implementations of null functions from ns3::Socket
  74. virtual enum SocketErrno GetErrno (void) const; // returns m_errno
  75. virtual enum SocketType GetSocketType (void) const; // returns socket type
  76. virtual Ptr<Node> GetNode (void) const; // returns m_node
  77. virtual int Bind (void); // Bind a socket by setting up endpoint in TcpL4Protocol
  78. virtual int Bind6 (void); // Bind a socket by setting up endpoint in TcpL4Protocol
  79. virtual int Bind (const Address &address); // ... endpoint of specific addr or port
  80. virtual int Connect (const Address &address); // Setup endpoint and call ProcessAction() to connect
  81. virtual int Listen (void); // Verify the socket is in a correct state and call ProcessAction() to listen
  82. virtual int Close (void); // Close by app: Kill socket upon tx buffer emptied
  83. virtual int ShutdownSend (void); // Assert the m_shutdownSend flag to prevent send to network
  84. virtual int ShutdownRecv (void); // Assert the m_shutdownRecv flag to prevent forward to app
  85. virtual int Send (Ptr<Packet> p, uint32_t flags); // Call by app to send data to network
  86. virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address &toAddress); // Same as Send(), toAddress is insignificant
  87. virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags); // Return a packet to be forwarded to app
  88. virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags, Address &fromAddress); // ... and write the remote address at fromAddress
  89. virtual uint32_t GetTxAvailable (void) const; // Available Tx buffer size
  90. virtual uint32_t GetRxAvailable (void) const; // Available-to-read data size, i.e. value of m_rxAvailable
  91. virtual int GetSockName (Address &address) const; // Return local addr:port in address
  92. virtual void BindToNetDevice (Ptr<NetDevice> netdevice); // NetDevice with my m_endPoint
  93. protected:
  94. // Implementing ns3::TcpSocket -- Attribute get/set
  95. virtual void SetSndBufSize (uint32_t size);
  96. virtual uint32_t GetSndBufSize (void) const;
  97. virtual void SetRcvBufSize (uint32_t size);
  98. virtual uint32_t GetRcvBufSize (void) const;
  99. virtual void SetSegSize (uint32_t size);
  100. virtual uint32_t GetSegSize (void) const;
  101. virtual void SetSSThresh (uint32_t threshold) = 0;
  102. virtual uint32_t GetSSThresh (void) const = 0;
  103. virtual void SetInitialCwnd (uint32_t cwnd) = 0;
  104. virtual uint32_t GetInitialCwnd (void) const = 0;
  105. virtual void SetConnTimeout (Time timeout);
  106. virtual Time GetConnTimeout (void) const;
  107. virtual void SetConnCount (uint32_t count);
  108. virtual uint32_t GetConnCount (void) const;
  109. virtual void SetDelAckTimeout (Time timeout);
  110. virtual Time GetDelAckTimeout (void) const;
  111. virtual void SetDelAckMaxCount (uint32_t count);
  112. virtual uint32_t GetDelAckMaxCount (void) const;
  113. virtual void SetTcpNoDelay (bool noDelay);
  114. virtual bool GetTcpNoDelay (void) const;
  115. virtual void SetPersistTimeout (Time timeout);
  116. virtual Time GetPersistTimeout (void) const;
  117. virtual bool SetAllowBroadcast (bool allowBroadcast);
  118. virtual bool GetAllowBroadcast (void) const;
  119. // Helper functions: Connection set up
  120. int SetupCallback (void); // Common part of the two Bind(), i.e. set callback and remembering local addr:port
  121. int DoConnect (void); // Sending a SYN packet to make a connection if the state allows
  122. void ConnectionSucceeded (void); // Schedule-friendly wrapper for Socket::NotifyConnectionSucceeded()
  123. int SetupEndpoint (void); // Configure m_endpoint for local addr for given remote addr
  124. int SetupEndpoint6 (void); // Configure m_endpoint6 for local addr for given remote addr
  125. void CompleteFork (Ptr<Packet>, const TcpHeader&, const Address& fromAddress, const Address& toAdress);
  126. // Helper functions: Transfer operation
  127. void ForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, Ptr<Ipv4Interface> incomingInterface);
  128. void ForwardUp6 (Ptr<Packet> packet, Ipv6Address saddr, Ipv6Address daddr, uint16_t port);
  129. virtual void DoForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, Ptr<Ipv4Interface> incomingInterface); //Get a pkt from L3
  130. virtual void DoForwardUp (Ptr<Packet> packet, Ipv6Address saddr, Ipv6Address daddr, uint16_t port); // Ipv6 version
  131. bool SendPendingData (bool withAck = false); // Send as much as the window allows
  132. uint32_t SendDataPacket (SequenceNumber32 seq, uint32_t maxSize, bool withAck); // Send a data packet
  133. void SendEmptyPacket (uint8_t flags); // Send a empty packet that carries a flag, e.g. ACK
  134. void SendRST (void); // Send reset and tear down this socket
  135. bool OutOfRange (SequenceNumber32 head, SequenceNumber32 tail) const; // Check if a sequence number range is within the rx window
  136. // Helper functions: Connection close
  137. int DoClose (void); // Close a socket by sending RST, FIN, or FIN+ACK, depend on the current state
  138. void CloseAndNotify (void); // To CLOSED state, notify upper layer, and deallocate end point
  139. void Destroy (void); // Kill this socket by zeroing its attributes
  140. void Destroy6 (void); // Kill this socket by zeroing its attributes
  141. void DeallocateEndPoint (void); // Deallocate m_endPoint
  142. void PeerClose (Ptr<Packet>, const TcpHeader&); // Received a FIN from peer, notify rx buffer
  143. void DoPeerClose (void); // FIN is in sequence, notify app and respond with a FIN
  144. void CancelAllTimers (void); // Cancel all timer when endpoint is deleted
  145. void TimeWait (void); // Move from CLOSING or FIN_WAIT_2 to TIME_WAIT state
  146. // State transition functions
  147. void ProcessEstablished (Ptr<Packet>, const TcpHeader&); // Received a packet upon ESTABLISHED state
  148. void ProcessListen (Ptr<Packet>, const TcpHeader&, const Address&, const Address&); // Process the newly received ACK
  149. void ProcessSynSent (Ptr<Packet>, const TcpHeader&); // Received a packet upon SYN_SENT
  150. void ProcessSynRcvd (Ptr<Packet>, const TcpHeader&, const Address&, const Address&); // Received a packet upon SYN_RCVD
  151. void ProcessWait (Ptr<Packet>, const TcpHeader&); // Received a packet upon CLOSE_WAIT, FIN_WAIT_1, FIN_WAIT_2
  152. void ProcessClosing (Ptr<Packet>, const TcpHeader&); // Received a packet upon CLOSING
  153. void ProcessLastAck (Ptr<Packet>, const TcpHeader&); // Received a packet upon LAST_ACK
  154. // Window management
  155. virtual uint32_t UnAckDataCount (void); // Return count of number of unacked bytes
  156. virtual uint32_t BytesInFlight (void); // Return total bytes in flight
  157. virtual uint32_t Window (void); // Return the max possible number of unacked bytes
  158. virtual uint32_t AvailableWindow (void); // Return unfilled portion of window
  159. virtual uint16_t AdvertisedWindowSize (void); // The amount of Rx window announced to the peer
  160. // Manage data tx/rx
  161. virtual Ptr<TcpSocketBase> Fork (void) = 0; // Call CopyObject<> to clone me
  162. virtual void ReceivedAck (Ptr<Packet>, const TcpHeader&); // Received an ACK packet
  163. virtual void ReceivedData (Ptr<Packet>, const TcpHeader&); // Recv of a data, put into buffer, call L7 to get it if necessary
  164. virtual void EstimateRtt (const TcpHeader&); // RTT accounting
  165. virtual void NewAck (SequenceNumber32 const& seq); // Update buffers w.r.t. ACK
  166. virtual void DupAck (const TcpHeader& t, uint32_t count) = 0; // Received dupack
  167. virtual void ReTxTimeout (void); // Call Retransmit() upon RTO event
  168. virtual void Retransmit (void); // Halving cwnd and call DoRetransmit()
  169. virtual void DelAckTimeout (void); // Action upon delay ACK timeout, i.e. send an ACK
  170. virtual void LastAckTimeout (void); // Timeout at LAST_ACK, close the connection
  171. virtual void PersistTimeout (void); // Send 1 byte probe to get an updated window size
  172. virtual void DoRetransmit (void); // Retransmit the oldest packet
  173. virtual void ReadOptions (const TcpHeader&); // Read option from incoming packets
  174. virtual void AddOptions (TcpHeader&); // Add option to outgoing packets
  175. protected:
  176. // Counters and events
  177. EventId m_retxEvent; //< Retransmission event
  178. EventId m_lastAckEvent; //< Last ACK timeout event
  179. EventId m_delAckEvent; //< Delayed ACK timeout event
  180. EventId m_persistEvent; //< Persist event: Send 1 byte to probe for a non-zero Rx window
  181. EventId m_timewaitEvent; //< TIME_WAIT expiration event: Move this socket to CLOSED state
  182. uint32_t m_dupAckCount; //< Dupack counter
  183. uint32_t m_delAckCount; //< Delayed ACK counter
  184. uint32_t m_delAckMaxCount; //< Number of packet to fire an ACK before delay timeout
  185. bool m_noDelay; //< Set to true to disable Nagle's algorithm
  186. uint32_t m_cnCount; //< Count of remaining connection retries
  187. uint32_t m_cnRetries; //< Number of connection retries before giving up
  188. TracedValue<Time> m_rto; //< Retransmit timeout
  189. TracedValue<Time> m_lastRtt; //< Last RTT sample collected
  190. Time m_delAckTimeout; //< Time to delay an ACK
  191. Time m_persistTimeout; //< Time between sending 1-byte probes
  192. Time m_cnTimeout; //< Timeout for connection retry
  193. // Connections to other layers of TCP/IP
  194. Ipv4EndPoint* m_endPoint;
  195. Ipv6EndPoint* m_endPoint6;
  196. Ptr<Node> m_node;
  197. Ptr<TcpL4Protocol> m_tcp;
  198. // Round trip time estimation
  199. Ptr<RttEstimator> m_rtt;
  200. // Rx and Tx buffer management
  201. TracedValue<SequenceNumber32> m_nextTxSequence; //< Next seqnum to be sent (SND.NXT), ReTx pushes it back
  202. TracedValue<SequenceNumber32> m_highTxMark; //< Highest seqno ever sent, regardless of ReTx
  203. TcpRxBuffer m_rxBuffer; //< Rx buffer (reordering buffer)
  204. TcpTxBuffer m_txBuffer; //< Tx buffer
  205. // State-related attributes
  206. TracedValue<TcpStates_t> m_state; //< TCP state
  207. enum SocketErrno m_errno; //< Socket error code
  208. bool m_closeNotified; //< Told app to close socket
  209. bool m_closeOnEmpty; //< Close socket upon tx buffer emptied
  210. bool m_shutdownSend; //< Send no longer allowed
  211. bool m_shutdownRecv; //< Receive no longer allowed
  212. bool m_connected; //< Connection established
  213. double m_msl; //< Max segment lifetime
  214. // Window management
  215. uint32_t m_segmentSize; //< Segment size
  216. uint16_t m_maxWinSize; //< Maximum window size to advertise
  217. TracedValue<uint32_t> m_rWnd; //< Flow control window at remote side
  218. };
  219. } // namespace ns3
  220. #endif /* TCP_SOCKET_BASE_H */