A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tcp-socket-base.h
Go to the documentation of this file.
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 
24 #include <stdint.h>
25 #include <queue>
26 #include "ns3/callback.h"
27 #include "ns3/traced-value.h"
28 #include "ns3/tcp-socket.h"
29 #include "ns3/ptr.h"
30 #include "ns3/ipv4-address.h"
31 #include "ns3/ipv4-header.h"
32 #include "ns3/ipv4-interface.h"
33 #include "ns3/event-id.h"
34 #include "tcp-tx-buffer.h"
35 #include "tcp-rx-buffer.h"
36 #include "rtt-estimator.h"
37 
38 namespace ns3 {
39 
40 class Ipv4EndPoint;
41 class Ipv6EndPoint;
42 class Node;
43 class Packet;
44 class TcpL4Protocol;
45 class TcpHeader;
46 
60 class TcpSocketBase : public TcpSocket
61 {
62 public:
63  static TypeId GetTypeId (void);
67  TcpSocketBase (void);
68 
72  TcpSocketBase (const TcpSocketBase& sock);
73  virtual ~TcpSocketBase (void);
74 
75  // Set associated Node, TcpL4Protocol, RttEstimator to this socket
76  virtual void SetNode (Ptr<Node> node);
77  virtual void SetTcp (Ptr<TcpL4Protocol> tcp);
78  virtual void SetRtt (Ptr<RttEstimator> rtt);
79 
80  // Necessary implementations of null functions from ns3::Socket
81  virtual enum SocketErrno GetErrno (void) const; // returns m_errno
82  virtual enum SocketType GetSocketType (void) const; // returns socket type
83  virtual Ptr<Node> GetNode (void) const; // returns m_node
84  virtual int Bind (void); // Bind a socket by setting up endpoint in TcpL4Protocol
85  virtual int Bind6 (void); // Bind a socket by setting up endpoint in TcpL4Protocol
86  virtual int Bind (const Address &address); // ... endpoint of specific addr or port
87  virtual int Connect (const Address &address); // Setup endpoint and call ProcessAction() to connect
88  virtual int Listen (void); // Verify the socket is in a correct state and call ProcessAction() to listen
89  virtual int Close (void); // Close by app: Kill socket upon tx buffer emptied
90  virtual int ShutdownSend (void); // Assert the m_shutdownSend flag to prevent send to network
91  virtual int ShutdownRecv (void); // Assert the m_shutdownRecv flag to prevent forward to app
92  virtual int Send (Ptr<Packet> p, uint32_t flags); // Call by app to send data to network
93  virtual int SendTo (Ptr<Packet> p, uint32_t flags, const Address &toAddress); // Same as Send(), toAddress is insignificant
94  virtual Ptr<Packet> Recv (uint32_t maxSize, uint32_t flags); // Return a packet to be forwarded to app
95  virtual Ptr<Packet> RecvFrom (uint32_t maxSize, uint32_t flags, Address &fromAddress); // ... and write the remote address at fromAddress
96  virtual uint32_t GetTxAvailable (void) const; // Available Tx buffer size
97  virtual uint32_t GetRxAvailable (void) const; // Available-to-read data size, i.e. value of m_rxAvailable
98  virtual int GetSockName (Address &address) const; // Return local addr:port in address
99  virtual void BindToNetDevice (Ptr<NetDevice> netdevice); // NetDevice with my m_endPoint
100 
101 protected:
102  // Implementing ns3::TcpSocket -- Attribute get/set
103  virtual void SetSndBufSize (uint32_t size);
104  virtual uint32_t GetSndBufSize (void) const;
105  virtual void SetRcvBufSize (uint32_t size);
106  virtual uint32_t GetRcvBufSize (void) const;
107  virtual void SetSegSize (uint32_t size);
108  virtual uint32_t GetSegSize (void) const;
109  virtual void SetSSThresh (uint32_t threshold) = 0;
110  virtual uint32_t GetSSThresh (void) const = 0;
111  virtual void SetInitialCwnd (uint32_t cwnd) = 0;
112  virtual uint32_t GetInitialCwnd (void) const = 0;
113  virtual void SetConnTimeout (Time timeout);
114  virtual Time GetConnTimeout (void) const;
115  virtual void SetConnCount (uint32_t count);
116  virtual uint32_t GetConnCount (void) const;
117  virtual void SetDelAckTimeout (Time timeout);
118  virtual Time GetDelAckTimeout (void) const;
119  virtual void SetDelAckMaxCount (uint32_t count);
120  virtual uint32_t GetDelAckMaxCount (void) const;
121  virtual void SetTcpNoDelay (bool noDelay);
122  virtual bool GetTcpNoDelay (void) const;
123  virtual void SetPersistTimeout (Time timeout);
124  virtual Time GetPersistTimeout (void) const;
125  virtual bool SetAllowBroadcast (bool allowBroadcast);
126  virtual bool GetAllowBroadcast (void) const;
127 
128  // Helper functions: Connection set up
129  int SetupCallback (void); // Common part of the two Bind(), i.e. set callback and remembering local addr:port
130  int DoConnect (void); // Sending a SYN packet to make a connection if the state allows
131  void ConnectionSucceeded (void); // Schedule-friendly wrapper for Socket::NotifyConnectionSucceeded()
132  int SetupEndpoint (void); // Configure m_endpoint for local addr for given remote addr
133  int SetupEndpoint6 (void); // Configure m_endpoint6 for local addr for given remote addr
134  void CompleteFork (Ptr<Packet>, const TcpHeader&, const Address& fromAddress, const Address& toAdress);
135 
136  // Helper functions: Transfer operation
137  void ForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, Ptr<Ipv4Interface> incomingInterface);
138  void ForwardUp6 (Ptr<Packet> packet, Ipv6Address saddr, Ipv6Address daddr, uint16_t port);
139  virtual void DoForwardUp (Ptr<Packet> packet, Ipv4Header header, uint16_t port, Ptr<Ipv4Interface> incomingInterface); //Get a pkt from L3
140  virtual void DoForwardUp (Ptr<Packet> packet, Ipv6Address saddr, Ipv6Address daddr, uint16_t port); // Ipv6 version
141  bool SendPendingData (bool withAck = false); // Send as much as the window allows
142  uint32_t SendDataPacket (SequenceNumber32 seq, uint32_t maxSize, bool withAck); // Send a data packet
143  void SendEmptyPacket (uint8_t flags); // Send a empty packet that carries a flag, e.g. ACK
144  void SendRST (void); // Send reset and tear down this socket
145  bool OutOfRange (SequenceNumber32 head, SequenceNumber32 tail) const; // Check if a sequence number range is within the rx window
146 
147  // Helper functions: Connection close
148  int DoClose (void); // Close a socket by sending RST, FIN, or FIN+ACK, depend on the current state
149  void CloseAndNotify (void); // To CLOSED state, notify upper layer, and deallocate end point
150  void Destroy (void); // Kill this socket by zeroing its attributes
151  void Destroy6 (void); // Kill this socket by zeroing its attributes
152  void DeallocateEndPoint (void); // Deallocate m_endPoint
153  void PeerClose (Ptr<Packet>, const TcpHeader&); // Received a FIN from peer, notify rx buffer
154  void DoPeerClose (void); // FIN is in sequence, notify app and respond with a FIN
155  void CancelAllTimers (void); // Cancel all timer when endpoint is deleted
156  void TimeWait (void); // Move from CLOSING or FIN_WAIT_2 to TIME_WAIT state
157 
158  // State transition functions
159  void ProcessEstablished (Ptr<Packet>, const TcpHeader&); // Received a packet upon ESTABLISHED state
160  void ProcessListen (Ptr<Packet>, const TcpHeader&, const Address&, const Address&); // Process the newly received ACK
161  void ProcessSynSent (Ptr<Packet>, const TcpHeader&); // Received a packet upon SYN_SENT
162  void ProcessSynRcvd (Ptr<Packet>, const TcpHeader&, const Address&, const Address&); // Received a packet upon SYN_RCVD
163  void ProcessWait (Ptr<Packet>, const TcpHeader&); // Received a packet upon CLOSE_WAIT, FIN_WAIT_1, FIN_WAIT_2
164  void ProcessClosing (Ptr<Packet>, const TcpHeader&); // Received a packet upon CLOSING
165  void ProcessLastAck (Ptr<Packet>, const TcpHeader&); // Received a packet upon LAST_ACK
166 
167  // Window management
168  virtual uint32_t UnAckDataCount (void); // Return count of number of unacked bytes
169  virtual uint32_t BytesInFlight (void); // Return total bytes in flight
170  virtual uint32_t Window (void); // Return the max possible number of unacked bytes
171  virtual uint32_t AvailableWindow (void); // Return unfilled portion of window
172  virtual uint16_t AdvertisedWindowSize (void); // The amount of Rx window announced to the peer
173 
174  // Manage data tx/rx
175  virtual Ptr<TcpSocketBase> Fork (void) = 0; // Call CopyObject<> to clone me
176  virtual void ReceivedAck (Ptr<Packet>, const TcpHeader&); // Received an ACK packet
177  virtual void ReceivedData (Ptr<Packet>, const TcpHeader&); // Recv of a data, put into buffer, call L7 to get it if necessary
178  virtual void EstimateRtt (const TcpHeader&); // RTT accounting
179  virtual void NewAck (SequenceNumber32 const& seq); // Update buffers w.r.t. ACK
180  virtual void DupAck (const TcpHeader& t, uint32_t count) = 0; // Received dupack
181  virtual void ReTxTimeout (void); // Call Retransmit() upon RTO event
182  virtual void Retransmit (void); // Halving cwnd and call DoRetransmit()
183  virtual void DelAckTimeout (void); // Action upon delay ACK timeout, i.e. send an ACK
184  virtual void LastAckTimeout (void); // Timeout at LAST_ACK, close the connection
185  virtual void PersistTimeout (void); // Send 1 byte probe to get an updated window size
186  virtual void DoRetransmit (void); // Retransmit the oldest packet
187  virtual void ReadOptions (const TcpHeader&); // Read option from incoming packets
188  virtual void AddOptions (TcpHeader&); // Add option to outgoing packets
189 
190 protected:
191  // Counters and events
192  EventId m_retxEvent; //< Retransmission event
193  EventId m_lastAckEvent; //< Last ACK timeout event
194  EventId m_delAckEvent; //< Delayed ACK timeout event
195  EventId m_persistEvent; //< Persist event: Send 1 byte to probe for a non-zero Rx window
196  EventId m_timewaitEvent; //< TIME_WAIT expiration event: Move this socket to CLOSED state
197  uint32_t m_dupAckCount; //< Dupack counter
198  uint32_t m_delAckCount; //< Delayed ACK counter
199  uint32_t m_delAckMaxCount; //< Number of packet to fire an ACK before delay timeout
200  bool m_noDelay; //< Set to true to disable Nagle's algorithm
201  uint32_t m_cnCount; //< Count of remaining connection retries
202  uint32_t m_cnRetries; //< Number of connection retries before giving up
203  TracedValue<Time> m_rto; //< Retransmit timeout
204  TracedValue<Time> m_lastRtt; //< Last RTT sample collected
205  Time m_delAckTimeout; //< Time to delay an ACK
206  Time m_persistTimeout; //< Time between sending 1-byte probes
207  Time m_cnTimeout; //< Timeout for connection retry
208 
209  // Connections to other layers of TCP/IP
214 
215  // Round trip time estimation
217 
218  // Rx and Tx buffer management
219  TracedValue<SequenceNumber32> m_nextTxSequence; //< Next seqnum to be sent (SND.NXT), ReTx pushes it back
220  TracedValue<SequenceNumber32> m_highTxMark; //< Highest seqno ever sent, regardless of ReTx
221  TcpRxBuffer m_rxBuffer; //< Rx buffer (reordering buffer)
222  TcpTxBuffer m_txBuffer; //< Tx buffer
223 
224  // State-related attributes
226  enum SocketErrno m_errno; //< Socket error code
227  bool m_closeNotified; //< Told app to close socket
228  bool m_closeOnEmpty; //< Close socket upon tx buffer emptied
229  bool m_shutdownSend; //< Send no longer allowed
230  bool m_shutdownRecv; //< Receive no longer allowed
231  bool m_connected; //< Connection established
232  double m_msl; //< Max segment lifetime
233 
234  // Window management
235  uint32_t m_segmentSize; //< Segment size
236  uint16_t m_maxWinSize; //< Maximum window size to advertise
237  TracedValue<uint32_t> m_rWnd; //< Flow control window at remote side
238 };
239 
240 } // namespace ns3
241 
242 #endif /* TCP_SOCKET_BASE_H */