A Discrete-Event Network Simulator
API
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 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("TcpNewReno");
34 
35 NS_OBJECT_ENSURE_REGISTERED (TcpNewReno);
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::TcpNewReno")
42  .AddConstructor<TcpNewReno> ()
43  .AddAttribute ("ReTxThreshold", "Threshold for fast retransmit",
44  UintegerValue (3),
46  MakeUintegerChecker<uint32_t> ())
47  .AddAttribute ("LimitedTransmit", "Enable limited transmit",
48  BooleanValue (false),
51  .AddTraceSource ("CongestionWindow",
52  "The TCP connection's congestion window",
54  "ns3::TracedValue::Uint32Callback")
55  .AddTraceSource ("SlowStartThreshold",
56  "TCP slow start threshold (bytes)",
58  "ns3::TracedValue::Uint32Callback")
59  ;
60  return tid;
61 }
62 
64  : m_retxThresh (3), // mute valgrind, actual value set by the attribute system
65  m_inFastRec (false),
66  m_limitedTx (false) // mute valgrind, actual value set by the attribute system
67 {
68  NS_LOG_FUNCTION (this);
69 }
70 
72  : TcpSocketBase (sock),
73  m_cWnd (sock.m_cWnd),
74  m_ssThresh (sock.m_ssThresh),
75  m_initialCWnd (sock.m_initialCWnd),
76  m_initialSsThresh (sock.m_initialSsThresh),
77  m_retxThresh (sock.m_retxThresh),
78  m_inFastRec (false),
79  m_limitedTx (sock.m_limitedTx)
80 {
81  NS_LOG_FUNCTION (this);
82  NS_LOG_LOGIC ("Invoked the copy constructor");
83 }
84 
86 {
87 }
88 
89 /* We initialize m_cWnd from this function, after attributes initialized */
90 int
92 {
93  NS_LOG_FUNCTION (this);
94  InitializeCwnd ();
95  return TcpSocketBase::Listen ();
96 }
97 
98 /* We initialize m_cWnd from this function, after attributes initialized */
99 int
101 {
102  NS_LOG_FUNCTION (this << address);
103  InitializeCwnd ();
104  return TcpSocketBase::Connect (address);
105 }
106 
107 /* Limit the size of in-flight data by cwnd and receiver's rxwin */
108 uint32_t
110 {
111  NS_LOG_FUNCTION (this);
112  return std::min (m_rWnd.Get (), m_cWnd.Get ());
113 }
114 
117 {
118  return CopyObject<TcpNewReno> (this);
119 }
120 
121 /* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */
122 void
124 {
125  NS_LOG_FUNCTION (this << seq);
126  NS_LOG_LOGIC ("TcpNewReno received ACK for seq " << seq <<
127  " cwnd " << m_cWnd <<
128  " ssthresh " << m_ssThresh);
129 
130  // Check for exit condition of fast recovery
131  if (m_inFastRec && seq < m_recover)
132  { // Partial ACK, partial window deflation (RFC2582 sec.3 bullet #5 paragraph 3)
133  m_cWnd += m_segmentSize - (seq - m_txBuffer->HeadSequence ());
134  NS_LOG_INFO ("Partial ACK for seq " << seq << " in fast recovery: cwnd set to " << m_cWnd);
135  m_txBuffer->DiscardUpTo(seq); //Bug 1850: retransmit before newack
136  DoRetransmit (); // Assume the next seq is lost. Retransmit lost packet
137  TcpSocketBase::NewAck (seq); // update m_nextTxSequence and send new data if allowed by window
138  return;
139  }
140  else if (m_inFastRec && seq >= m_recover)
141  { // Full ACK (RFC2582 sec.3 bullet #5 paragraph 2, option 1)
142  m_cWnd = std::min (m_ssThresh.Get (), BytesInFlight () + m_segmentSize);
143  m_inFastRec = false;
144  NS_LOG_INFO ("Received full ACK for seq " << seq <<". Leaving fast recovery with cwnd set to " << m_cWnd);
145  }
146 
147  // Increase of cwnd based on current phase (slow start or congestion avoidance)
148  if (m_cWnd < m_ssThresh)
149  { // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1)
151  NS_LOG_INFO ("In SlowStart, ACK of seq " << seq << "; update cwnd to " << m_cWnd << "; ssthresh " << m_ssThresh);
152  }
153  else
154  { // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1)
155  // To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd
156  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get ();
157  adder = std::max (1.0, adder);
158  m_cWnd += static_cast<uint32_t> (adder);
159  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
160  }
161 
162  // Complete newAck processing
163  TcpSocketBase::NewAck (seq);
164 }
165 
166 /* Cut cwnd and enter fast recovery mode upon triple dupack */
167 void
168 TcpNewReno::DupAck (const TcpHeader& t, uint32_t count)
169 {
170  NS_LOG_FUNCTION (this << count);
171  if (count == m_retxThresh && !m_inFastRec)
172  { // triple duplicate ack triggers fast retransmit (RFC2582 sec.3 bullet #1)
173  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
176  m_inFastRec = true;
177  NS_LOG_INFO ("Triple dupack. Enter fast recovery mode. Reset cwnd to " << m_cWnd <<
178  ", ssthresh to " << m_ssThresh << " at fast recovery seqnum " << m_recover);
179  DoRetransmit ();
180  }
181  else if (m_inFastRec)
182  { // Increase cwnd for every additional dupack (RFC2582, sec.3 bullet #3)
184  NS_LOG_INFO ("Dupack in fast recovery mode. Increase cwnd to " << m_cWnd);
186  }
187  else if (!m_inFastRec && m_limitedTx && m_txBuffer->SizeFromSequence (m_nextTxSequence) > 0)
188  { // RFC3042 Limited transmit: Send a new packet for each duplicated ACK before fast retransmit
189  NS_LOG_INFO ("Limited transmit");
190  uint32_t sz = SendDataPacket (m_nextTxSequence, m_segmentSize, true);
191  m_nextTxSequence += sz; // Advance next tx sequence
192  };
193 }
194 
195 /* Retransmit timeout */
196 void
198 {
199  NS_LOG_FUNCTION (this);
200  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
201  m_inFastRec = false;
202 
203  // If erroneous timeout in closed/timed-wait state, just return
204  if (m_state == CLOSED || m_state == TIME_WAIT) return;
205  // If all data are received (non-closing socket and nothing to send), just return
206  if (m_state <= ESTABLISHED && m_txBuffer->HeadSequence () >= m_highTxMark) return;
207 
208  // According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight
209  // size and cwnd is set to 1*MSS, then the lost packet is retransmitted and
210  // TCP back to slow start
211  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
213  m_nextTxSequence = m_txBuffer->HeadSequence (); // Restart from highest Ack
214  NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd <<
215  ", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence);
216  DoRetransmit (); // Retransmit the packet
217 }
218 
219 void
220 TcpNewReno::SetSegSize (uint32_t size)
221 {
222  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetSegSize() cannot change segment size after connection started.");
223  m_segmentSize = size;
224 }
225 
226 void
227 TcpNewReno::SetInitialSSThresh (uint32_t threshold)
228 {
229  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetSSThresh() cannot change initial ssThresh after connection started.");
230  m_initialSsThresh = threshold;
231 }
232 
233 uint32_t
235 {
236  return m_initialSsThresh;
237 }
238 
239 void
241 {
242  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpNewReno::SetInitialCwnd() cannot change initial cwnd after connection started.");
243  m_initialCWnd = cwnd;
244 }
245 
246 uint32_t
248 {
249  return m_initialCWnd;
250 }
251 
252 void
254 {
255  /*
256  * Initialize congestion window, default to 1 MSS (RFC2001, sec.1) and must
257  * not be larger than 2 MSS (RFC2581, sec.3.1). Both m_initiaCWnd and
258  * m_segmentSize are set by the attribute system in ns3::TcpSocket.
259  */
262 }
263 
264 } // namespace ns3
virtual int Listen(void)
Listen for incoming connections.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
Definition: tcp-newreno.cc:123
AttributeValue implementation for Boolean.
Definition: boolean.h:34
bool m_limitedTx
perform limited transmit
Definition: tcp-newreno.h:86
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.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:81
uint32_t m_initialSsThresh
Initial Slow Start Threshold value.
Definition: tcp-newreno.h:82
virtual int Listen(void)
Listen for incoming connections.
Definition: tcp-newreno.cc:91
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual uint32_t GetInitialCwnd(void) const
Get the initial Congestion Window.
Definition: tcp-newreno.cc:247
virtual uint32_t Window(void)
Return the max possible number of unacked bytes.
Definition: tcp-newreno.cc:109
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
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
Get the underlying value.
Definition: traced-value.h:186
TracedValue< TcpStates_t > m_state
TCP state.
virtual void SetSegSize(uint32_t size)
Set the segment size.
Definition: tcp-newreno.cc:220
a polymophic address class
Definition: address.h:90
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
TracedValue< uint32_t > m_ssThresh
Slow Start Threshold.
Definition: tcp-newreno.h:80
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-newreno.cc:38
Ptr< TcpTxBuffer > m_txBuffer
Tx buffer.
virtual void DupAck(const TcpHeader &t, uint32_t count)
Received dupack (duplicate ACK)
Definition: tcp-newreno.cc:168
Hold an unsigned integer type.
Definition: uinteger.h:44
virtual void SetInitialCwnd(uint32_t cwnd)
Set the initial Congestion Window.
Definition: tcp-newreno.cc:240
virtual uint32_t GetInitialSSThresh(void) const
Get the initial Slow Start Threshold.
Definition: tcp-newreno.cc:234
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
Definition: tcp-newreno.cc:100
TracedValue< uint32_t > m_cWnd
Congestion window.
Definition: tcp-newreno.h:79
A base class for implementation of a stream socket using TCP.
void InitializeCwnd(void)
Set the congestion window when connection starts.
Definition: tcp-newreno.cc:253
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
bool m_inFastRec
currently in fast recovery
Definition: tcp-newreno.h:85
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t m_retxThresh
Fast Retransmit threshold.
Definition: tcp-newreno.h:84
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
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:197
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
TracedValue< SequenceNumber32 > m_highTxMark
Highest seqno ever sent, regardless of ReTx.
uint32_t m_initialCWnd
Initial cWnd value.
Definition: tcp-newreno.h:81
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
bool m_connected
Connection established.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
Definition: tcp-newreno.cc:116
tuple address
Definition: first.py:37
TcpNewReno(void)
Create an unbound tcp socket.
Definition: tcp-newreno.cc:63
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:51
virtual void SetInitialSSThresh(uint32_t threshold)
Set the initial Slow Start Threshold.
Definition: tcp-newreno.cc:227
TypeId SetParent(TypeId tid)
Definition: type-id.cc:631
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:83
virtual ~TcpNewReno(void)
Definition: tcp-newreno.cc:85