A Discrete-Event Network Simulator
API
tcp-reno.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-reno.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 ("TcpReno");
34 
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::TcpReno")
42  .SetGroupName ("Internet")
43  .AddConstructor<TcpReno> ()
44  .AddAttribute ("ReTxThreshold", "Threshold for fast retransmit",
45  UintegerValue (3),
47  MakeUintegerChecker<uint32_t> ())
48  .AddTraceSource ("CongestionWindow",
49  "The TCP connection's congestion window",
51  "ns3::TracedValue::Uint32Callback")
52  .AddTraceSource ("SlowStartThreshold",
53  "TCP slow start threshold (bytes)",
55  "ns3::TracedValue::Uint32Callback")
56  ;
57  return tid;
58 }
59 
60 TcpReno::TcpReno (void) : m_retxThresh (3), m_inFastRec (false)
61 {
62  NS_LOG_FUNCTION (this);
63 }
64 
66  : TcpSocketBase (sock),
67  m_cWnd (sock.m_cWnd),
68  m_ssThresh (sock.m_ssThresh),
69  m_initialCWnd (sock.m_initialCWnd),
70  m_initialSsThresh (sock.m_initialSsThresh),
71  m_retxThresh (sock.m_retxThresh),
72  m_inFastRec (false)
73 {
74  NS_LOG_FUNCTION (this);
75  NS_LOG_LOGIC ("Invoked the copy constructor");
76 }
77 
79 {
80 }
81 
82 /* We initialize m_cWnd from this function, after attributes initialized */
83 int
85 {
86  NS_LOG_FUNCTION (this);
87  InitializeCwnd ();
88  return TcpSocketBase::Listen ();
89 }
90 
91 /* We initialize m_cWnd from this function, after attributes initialized */
92 int
94 {
95  NS_LOG_FUNCTION (this << address);
96  InitializeCwnd ();
97  return TcpSocketBase::Connect (address);
98 }
99 
100 /* Limit the size of in-flight data by cwnd and receiver's rxwin */
101 uint32_t
103 {
104  NS_LOG_FUNCTION (this);
105  return std::min (m_rWnd.Get (), m_cWnd.Get ());
106 }
107 
110 {
111  return CopyObject<TcpReno> (this);
112 }
113 
114 /* New ACK (up to seqnum seq) received. Increase cwnd and call TcpSocketBase::NewAck() */
115 void
117 {
118  NS_LOG_FUNCTION (this << seq);
119  NS_LOG_LOGIC ("TcpReno receieved ACK for seq " << seq <<
120  " cwnd " << m_cWnd <<
121  " ssthresh " << m_ssThresh);
122 
123  // Check for exit condition of fast recovery
124  if (m_inFastRec)
125  { // RFC2001, sec.4; RFC2581, sec.3.2
126  // First new ACK after fast recovery: reset cwnd
127  m_cWnd = m_ssThresh;
128  m_inFastRec = false;
129  NS_LOG_INFO ("Reset cwnd to " << m_cWnd);
130  };
131 
132  // Increase of cwnd based on current phase (slow start or congestion avoidance)
133  if (m_cWnd < m_ssThresh)
134  { // Slow start mode, add one segSize to cWnd. Default m_ssThresh is 65535. (RFC2001, sec.1)
136  NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
137  }
138  else
139  { // Congestion avoidance mode, increase by (segSize*segSize)/cwnd. (RFC2581, sec.3.1)
140  // To increase cwnd for one segSize per RTT, it should be (ackBytes*segSize)/cwnd
141  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get ();
142  adder = std::max (1.0, adder);
143  m_cWnd += static_cast<uint32_t> (adder);
144  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
145  }
146 
147  // Complete newAck processing
148  TcpSocketBase::NewAck (seq);
149 }
150 
151 // Fast recovery and fast retransmit
152 void
153 TcpReno::DupAck (const TcpHeader& t, uint32_t count)
154 {
155  NS_LOG_FUNCTION (this << "t " << count);
156  if (count == m_retxThresh && !m_inFastRec)
157  { // triple duplicate ack triggers fast retransmit (RFC2581, sec.3.2)
158  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
160  m_inFastRec = true;
161  NS_LOG_INFO ("Triple dupack. Reset cwnd to " << m_cWnd << ", ssthresh to " << m_ssThresh);
162  DoRetransmit ();
163  }
164  else if (m_inFastRec)
165  { // In fast recovery, inc cwnd for every additional dupack (RFC2581, sec.3.2)
167  NS_LOG_INFO ("Increased cwnd to " << m_cWnd);
169  {
171  }
172  };
173 }
174 
175 // Retransmit timeout
177 {
178  NS_LOG_FUNCTION (this);
179  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
180  m_inFastRec = false;
181 
182  // If erroneous timeout in closed/timed-wait state, just return
183  if (m_state == CLOSED || m_state == TIME_WAIT) return;
184  // If all data are received (non-closing socket and nothing to send), just return
185  if (m_state <= ESTABLISHED && m_txBuffer->HeadSequence () >= m_highTxMark) return;
186 
187  // According to RFC2581 sec.3.1, upon RTO, ssthresh is set to half of flight
188  // size and cwnd is set to 1*MSS, then the lost packet is retransmitted and
189  // TCP back to slow start
190  m_ssThresh = std::max (2 * m_segmentSize, BytesInFlight () / 2);
192  m_nextTxSequence = m_txBuffer->HeadSequence (); // Restart from highest Ack
193  NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd <<
194  ", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence);
195  DoRetransmit (); // Retransmit the packet
196 }
197 
198 void
199 TcpReno::SetSegSize (uint32_t size)
200 {
201  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetSegSize() cannot change segment size after connection started.");
202  m_segmentSize = size;
203 }
204 
205 void
206 TcpReno::SetInitialSSThresh (uint32_t threshold)
207 {
208  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetSSThresh() cannot change initial ssThresh after connection started.");
209  m_initialSsThresh = threshold;
210 }
211 
212 uint32_t
214 {
215  return m_initialSsThresh;
216 }
217 
218 void
219 TcpReno::SetInitialCwnd (uint32_t cwnd)
220 {
221  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpReno::SetInitialCwnd() cannot change initial cwnd after connection started.");
222  m_initialCWnd = cwnd;
223 }
224 
225 uint32_t
227 {
228  return m_initialCWnd;
229 }
230 
231 void
233 {
234  /*
235  * Initialize congestion window, default to 1 MSS (RFC2001, sec.1) and must
236  * not be larger than 2 MSS (RFC2581, sec.3.1). Both m_initiaCWnd and
237  * m_segmentSize are set by the attribute system in ns3::TcpSocket.
238  */
241 }
242 
243 void
244 TcpReno::ScaleSsThresh (uint8_t scaleFactor)
245 {
246  m_ssThresh <<= scaleFactor;
247 }
248 
249 
250 } // 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 "...
TracedValue< uint32_t > m_cWnd
Congestion window.
Definition: tcp-reno.h:83
virtual void Retransmit(void)
Halving cwnd and call DoRetransmit()
Definition: tcp-reno.cc:176
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
virtual void ScaleSsThresh(uint8_t scaleFactor)
Scale the initial SsThresh value to the correct one.
Definition: tcp-reno.cc:244
TracedValue< uint32_t > m_ssThresh
Slow Start Threshold.
Definition: tcp-reno.h:84
virtual void SetSegSize(uint32_t size)
Set the segment size.
Definition: tcp-reno.cc:199
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
Definition: tcp-reno.cc:109
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t m_initialCWnd
Initial cWnd value.
Definition: tcp-reno.h:85
virtual int Listen(void)
Listen for incoming connections.
Definition: tcp-reno.cc:84
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:244
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:65
uint32_t m_segmentSize
Segment size.
virtual void NewAck(const SequenceNumber32 &seq)
Update buffers w.r.t.
Definition: tcp-reno.cc:116
TracedValue< SequenceNumber32 > m_nextTxSequence
Next seqnum to be sent (SND.NXT), ReTx pushes it back.
T Get(void) const
Get the underlying value.
Definition: traced-value.h:186
TracedValue< TcpStates_t > m_state
TCP state.
virtual void SetInitialCwnd(uint32_t cwnd)
Set the initial Congestion Window.
Definition: tcp-reno.cc:219
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.
virtual uint32_t GetInitialSSThresh(void) const
Get the initial Slow Start Threshold.
Definition: tcp-reno.cc:213
An implementation of a stream socket using TCP.
Definition: tcp-reno.h:38
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
Definition: tcp-reno.cc:93
bool m_inFastRec
currently in fast recovery
Definition: tcp-reno.h:88
Ptr< TcpTxBuffer > m_txBuffer
Tx buffer.
virtual void DupAck(const TcpHeader &t, uint32_t count)
Received dupack (duplicate ACK)
Definition: tcp-reno.cc:153
Hold an unsigned integer type.
Definition: uinteger.h:44
A base class for implementation of a stream socket using TCP.
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
virtual ~TcpReno(void)
Definition: tcp-reno.cc:78
virtual void SetInitialSSThresh(uint32_t threshold)
Set the initial Slow Start Threshold.
Definition: tcp-reno.cc:206
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
virtual uint32_t GetInitialCwnd(void) const
Get the initial Congestion Window.
Definition: tcp-reno.cc:226
uint32_t m_retxThresh
Fast Retransmit threshold.
Definition: tcp-reno.h:87
virtual uint32_t BytesInFlight(void)
Return total bytes in flight.
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-reno.cc:38
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
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.
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
tuple address
Definition: first.py:37
EventId m_sendPendingDataEvent
micro-delay event to send pending data
uint32_t m_initialSsThresh
Initial Slow Start Threshold value.
Definition: tcp-reno.h:86
TcpReno(void)
Create an unbound tcp socket.
Definition: tcp-reno.cc:60
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
bool SendPendingData(bool withAck=false)
Send as much pending data as possible according to the Tx window.
TracedValue< uint32_t > m_rWnd
Receiver window (RCV.WND in RFC793)
virtual void DoRetransmit(void)
Retransmit the oldest packet.
virtual uint32_t Window(void)
Return the max possible number of unacked bytes.
Definition: tcp-reno.cc:102
void InitializeCwnd(void)
Set the congestion window when connection starts.
Definition: tcp-reno.cc:232