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