A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
tcp-newreno.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Adrian Sai-wah Tam
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Adrian Sai-wah Tam <adrian.sw.tam@gmail.com>
19  */
20 
21 #define NS_LOG_APPEND_CONTEXT \
22  if (m_node) { std::clog << Simulator::Now ().GetSeconds () << " [node " << m_node->GetId () << "] "; }
23 
24 #include "tcp-newreno.h"
25 #include "ns3/log.h"
26 #include "ns3/trace-source-accessor.h"
27 #include "ns3/simulator.h"
28 #include "ns3/abort.h"
29 #include "ns3/node.h"
30 
31 NS_LOG_COMPONENT_DEFINE ("TcpNewReno");
32 
33 namespace ns3 {
34 
35 NS_OBJECT_ENSURE_REGISTERED (TcpNewReno)
36  ;
37 
38 TypeId
40 {
41  static TypeId tid = TypeId ("ns3::TcpNewReno")
43  .AddConstructor<TcpNewReno> ()
44  .AddAttribute ("ReTxThreshold", "Threshold for fast retransmit",
45  UintegerValue (3),
46  MakeUintegerAccessor (&TcpNewReno::m_retxThresh),
47  MakeUintegerChecker<uint32_t> ())
48  .AddAttribute ("LimitedTransmit", "Enable limited transmit",
49  BooleanValue (false),
50  MakeBooleanAccessor (&TcpNewReno::m_limitedTx),
51  MakeBooleanChecker ())
52  .AddTraceSource ("CongestionWindow",
53  "The TCP connection's congestion window",
55  ;
56  return tid;
57 }
58 
60  : m_retxThresh (3), // mute valgrind, actual value set by the attribute system
61  m_inFastRec (false),
62  m_limitedTx (false) // mute valgrind, actual value set by the attribute system
63 {
64  NS_LOG_FUNCTION (this);
65 }
66 
68  : TcpSocketBase (sock),
69  m_cWnd (sock.m_cWnd),
70  m_ssThresh (sock.m_ssThresh),
71  m_initialCWnd (sock.m_initialCWnd),
72  m_retxThresh (sock.m_retxThresh),
73  m_inFastRec (false),
74  m_limitedTx (sock.m_limitedTx)
75 {
76  NS_LOG_FUNCTION (this);
77  NS_LOG_LOGIC ("Invoked the copy constructor");
78 }
79 
81 {
82 }
83 
84 /* We initialize m_cWnd from this function, after attributes initialized */
85 int
87 {
88  NS_LOG_FUNCTION (this);
89  InitializeCwnd ();
90  return TcpSocketBase::Listen ();
91 }
92 
93 /* We initialize m_cWnd from this function, after attributes initialized */
94 int
96 {
97  NS_LOG_FUNCTION (this << address);
98  InitializeCwnd ();
99  return TcpSocketBase::Connect (address);
100 }
101 
102 /* Limit the size of in-flight data by cwnd and receiver's rxwin */
103 uint32_t
105 {
106  NS_LOG_FUNCTION (this);
107  return std::min (m_rWnd.Get (), m_cWnd.Get ());
108 }
109 
112 {
113  return CopyObject<TcpNewReno> (this);
114 }
115 
116 /* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */
117 void
119 {
120  NS_LOG_FUNCTION (this << seq);
121  NS_LOG_LOGIC ("TcpNewReno receieved ACK for seq " << seq <<
122  " cwnd " << m_cWnd <<
123  " ssthresh " << m_ssThresh);
124 
125  // Check for exit condition of fast recovery
126  if (m_inFastRec && seq < m_recover)
127  { // Partial ACK, partial window deflation (RFC2582 sec.3 bullet #5 paragraph 3)
128  m_cWnd -= seq - m_txBuffer.HeadSequence ();
129  m_cWnd += m_segmentSize; // increase cwnd
130  NS_LOG_INFO ("Partial ACK in fast recovery: cwnd set to " << m_cWnd);
131  TcpSocketBase::NewAck (seq); // update m_nextTxSequence and send new data if allowed by window
132  DoRetransmit (); // Assume the next seq is lost. Retransmit lost packet
133  return;
134  }
135  else if (m_inFastRec && seq >= m_recover)
136  { // Full ACK (RFC2582 sec.3 bullet #5 paragraph 2, option 1)
137  m_cWnd = std::min (m_ssThresh, BytesInFlight () + m_segmentSize);
138  m_inFastRec = false;
139  NS_LOG_INFO ("Received full ACK. Leaving fast recovery with cwnd set to " << m_cWnd);
140  }
141 
142  // Increase of cwnd based on current phase (slow start or congestion avoidance)
143  if (m_cWnd < m_ssThresh)
144  { // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1)
146  NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
147  }
148  else
149  { // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1)
150  // To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd
151  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get ();
152  adder = std::max (1.0, adder);
153  m_cWnd += static_cast<uint32_t> (adder);
154  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
155  }
156 
157  // Complete newAck processing
158  TcpSocketBase::NewAck (seq);
159 }
160 
161 /* Cut cwnd and enter fast recovery mode upon triple dupack */
162 void
163 TcpNewReno::DupAck (const TcpHeader& t, uint32_t count)
164 {
165  NS_LOG_FUNCTION (this << count);
166  if (count == m_retxThresh && !m_inFastRec)
167  { // triple duplicate ack triggers fast retransmit (RFC2582 sec.3 bullet #1)
168  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
171  m_inFastRec = true;
172  NS_LOG_INFO ("Triple dupack. Enter fast recovery mode. Reset cwnd to " << m_cWnd <<
173  ", ssthresh to " << m_ssThresh << " at fast recovery seqnum " << m_recover);
174  DoRetransmit ();
175  }
176  else if (m_inFastRec)
177  { // Increase cwnd for every additional dupack (RFC2582, sec.3 bullet #3)
179  NS_LOG_INFO ("Dupack in fast recovery mode. Increase cwnd to " << m_cWnd);
181  }
183  { // RFC3042 Limited transmit: Send a new packet for each duplicated ACK before fast retransmit
184  NS_LOG_INFO ("Limited transmit");
185  uint32_t sz = SendDataPacket (m_nextTxSequence, m_segmentSize, true);
186  m_nextTxSequence += sz; // Advance next tx sequence
187  };
188 }
189 
190 /* Retransmit timeout */
191 void
193 {
194  NS_LOG_FUNCTION (this);
195  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
196  m_inFastRec = false;
197 
198  // If erroneous timeout in closed/timed-wait state, just return
199  if (m_state == CLOSED || m_state == TIME_WAIT) return;
200  // If all data are received (non-closing socket and nothing to send), just return
201  if (m_state <= ESTABLISHED && m_txBuffer.HeadSequence () >= m_highTxMark) return;
202 
203  // According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight
204  // size and cwnd is set to 1*MSS, then the lost packet is retransmitted and
205  // TCP back to slow start
206  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
208  m_nextTxSequence = m_txBuffer.HeadSequence (); // Restart from highest Ack
209  NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd <<
210  ", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence);
211  m_rtt->IncreaseMultiplier (); // Double the next RTO
212  DoRetransmit (); // Retransmit the packet
213 }
214 
215 void
216 TcpNewReno::SetSegSize (uint32_t size)
217 {
218  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetSegSize() cannot change segment size after connection started.");
219  m_segmentSize = size;
220 }
221 
222 void
223 TcpNewReno::SetSSThresh (uint32_t threshold)
224 {
225  m_ssThresh = threshold;
226 }
227 
228 uint32_t
230 {
231  return m_ssThresh;
232 }
233 
234 void
236 {
237  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetInitialCwnd() cannot change initial cwnd after connection started.");
238  m_initialCWnd = cwnd;
239 }
240 
241 uint32_t
243 {
244  return m_initialCWnd;
245 }
246 
247 void
249 {
250  /*
251  * Initialize congestion window, default to 1 MSS (RFC2001, sec.1) and must
252  * not be larger than 2 MSS (RFC2581, sec.3.1). Both m_initiaCWnd and
253  * m_segmentSize are set by the attribute system in ns3::TcpSocket.
254  */
256 }
257 
258 } // namespace ns3
NS_LOG_COMPONENT_DEFINE("TcpNewReno")
virtual int Listen(void)
Listen for incoming connections.
smart pointer class similar to boost::intrusive_ptr
Definition: ptr.h:59
#define NS_LOG_FUNCTION(parameters)
Definition: log.h:345
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
Definition: tcp-newreno.cc:118
Hold a bool native type.
Definition: boolean.h:38
bool m_limitedTx
perform limited transmit
Definition: tcp-newreno.h:85
uint32_t SendDataPacket(SequenceNumber32 seq, uint32_t maxSize, bool withAck)
Extract at most maxSize bytes from the TxBuffer at sequence seq, add the TCP header, and send to TcpL4Protocol.
uint32_t SizeFromSequence(const SequenceNumber32 &seq) const
Returns the number of bytes from the buffer in the range [seq, tailSequence)
SequenceNumber32 HeadSequence(void) const
Returns the first byte's sequence number.
NS_OBJECT_ENSURE_REGISTERED(NullMessageSimulatorImpl)
virtual int Listen(void)
Listen for incoming connections.
Definition: tcp-newreno.cc:86
virtual uint32_t GetInitialCwnd(void) const
Get the initial Congestion Window.
Definition: tcp-newreno.cc:242
virtual uint32_t Window(void)
Return the max possible number of unacked bytes.
Definition: tcp-newreno.cc:104
#define NS_LOG_INFO(msg)
Definition: log.h:298
uint32_t m_segmentSize
Segment size.
TracedValue< SequenceNumber32 > m_nextTxSequence
Next seqnum to be sent (SND.NXT), ReTx pushes it back.
An implementation of a stream socket using TCP.
Definition: tcp-newreno.h:36
T Get(void) const
Definition: traced-value.h:97
TracedValue< TcpStates_t > m_state
TCP state.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if cond is false.
Definition: abort.h:131
virtual void SetSegSize(uint32_t size)
Set the segment size.
Definition: tcp-newreno.cc:216
a polymophic address class
Definition: address.h:86
TcpTxBuffer m_txBuffer
Tx buffer.
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-newreno.cc:39
virtual void DupAck(const TcpHeader &t, uint32_t count)
Received dupack (duplicate ACK)
Definition: tcp-newreno.cc:163
Hold an unsigned integer type.
Definition: uinteger.h:46
virtual void SetInitialCwnd(uint32_t cwnd)
Set the initial Congestion Window.
Definition: tcp-newreno.cc:235
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
Definition: tcp-newreno.cc:95
TracedValue< uint32_t > m_cWnd
Congestion window.
Definition: tcp-newreno.h:79
A base class for implementation of a stream socket using TCP.
Ptr< RttEstimator > m_rtt
Round trip time estimator.
void InitializeCwnd(void)
Set the congestion window when connection starts.
Definition: tcp-newreno.cc:248
#define NS_LOG_LOGIC(msg)
Definition: log.h:368
bool m_inFastRec
currently in fast recovery
Definition: tcp-newreno.h:84
uint32_t m_ssThresh
Slow Start Threshold.
Definition: tcp-newreno.h:80
uint32_t m_retxThresh
Fast Retransmit threshold.
Definition: tcp-newreno.h:83
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Header for the Transmission Control Protocol.
Definition: tcp-header.h:43
virtual uint32_t BytesInFlight(void)
Return total bytes in flight.
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
virtual void Retransmit(void)
Halving cwnd and call DoRetransmit()
Definition: tcp-newreno.cc:192
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
TracedValue< SequenceNumber32 > m_highTxMark
Highest seqno ever sent, regardless of ReTx.
uint32_t m_initialCWnd
Initial cWnd value.
Definition: tcp-newreno.h:81
virtual uint32_t GetSSThresh(void) const
Get the Slow Start Threshold.
Definition: tcp-newreno.cc:229
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
bool m_connected
Connection established.
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
Definition: tcp-newreno.cc:111
tuple address
Definition: first.py:37
virtual void SetSSThresh(uint32_t threshold)
Set the Slow Start Threshold.
Definition: tcp-newreno.cc:223
TcpNewReno(void)
Create an unbound tcp socket.
Definition: tcp-newreno.cc:59
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:611
bool SendPendingData(bool withAck=false)
Send as much pending data as possible according to the Tx window.
TracedValue< uint32_t > m_rWnd
Flow control window at remote side.
virtual void DoRetransmit(void)
Retransmit the oldest packet.
SequenceNumber32 m_recover
Previous highest Tx seqnum for fast recovery.
Definition: tcp-newreno.h:82
virtual ~TcpNewReno(void)
Definition: tcp-newreno.cc:80