A Discrete-Event Network Simulator
API
tcp-westwood.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 ResiliNets, ITTC, University of Kansas
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  * Authors: Siddharth Gangadhar <siddharth@ittc.ku.edu>, Truc Anh N. Nguyen <annguyen@ittc.ku.edu>,
19  * and Greeshma Umapathi
20  *
21  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
22  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
23  * Information and Telecommunication Technology Center (ITTC)
24  * and Department of Electrical Engineering and Computer Science
25  * The University of Kansas Lawrence, KS USA.
26  *
27  * Work supported in part by NSF FIND (Future Internet Design) Program
28  * under grant CNS-0626918 (Postmodern Internet Architecture),
29  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
30  * US Department of Defense (DoD), and ITTC at The University of Kansas.
31  */
32 
33 #define NS_LOG_APPEND_CONTEXT \
34  if (m_node) { std::clog << Simulator::Now ().GetSeconds () << " [node " << m_node->GetId () << "] "; }
35 
36 #include "tcp-westwood.h"
37 #include "ns3/log.h"
38 #include "ns3/trace-source-accessor.h"
39 #include "ns3/simulator.h"
40 #include "ns3/abort.h"
41 #include "ns3/node.h"
42 #include "ns3/sequence-number.h"
43 #include "rtt-estimator.h"
44 
45 namespace ns3 {
46 
47 NS_LOG_COMPONENT_DEFINE("TcpWestwood");
48 
49 NS_OBJECT_ENSURE_REGISTERED(TcpWestwood);
50 
51 TypeId
53 {
54  static TypeId tid = TypeId("ns3::TcpWestwood")
56  .SetGroupName ("Internet")
57  .AddConstructor<TcpWestwood>()
58  .AddTraceSource("CongestionWindow", "The TCP connection's congestion window",
60  "ns3::TracedValue::Uint32Callback")
61  .AddTraceSource ("SlowStartThreshold",
62  "TCP slow start threshold (bytes)",
64  "ns3::TracedValue::Uint32Callback")
65  .AddAttribute("FilterType", "Use this to choose no filter or Tustin's approximation filter",
68  .AddAttribute("ProtocolType", "Use this to let the code run as Westwood or WestwoodPlus",
72  .AddTraceSource("EstimatedBW", "The estimated bandwidth",
74  "ns3::TracedValue::DoubleCallback");
75  return tid;
76 }
77 
79  m_inFastRec(false),
80  m_currentBW(0),
81  m_lastSampleBW(0),
82  m_lastBW(0),
83  m_minRtt(0),
84  m_lastAck(0),
85  m_prevAckNo(0),
86  m_accountedFor(0),
87  m_ackedSegments(0),
88  m_IsCount(false)
89 {
90  NS_LOG_FUNCTION (this);
91 }
92 
94  TcpSocketBase(sock),
95  m_cWnd(sock.m_cWnd),
96  m_ssThresh(sock.m_ssThresh),
97  m_initialCWnd(sock.m_initialCWnd),
98  m_initialSsThresh (sock.m_initialSsThresh),
99  m_inFastRec(false),
100  m_currentBW(sock.m_currentBW),
101  m_lastSampleBW(sock.m_lastSampleBW),
102  m_lastBW(sock.m_lastBW),
103  m_minRtt(sock.m_minRtt),
104  m_lastAck(sock.m_lastAck),
105  m_prevAckNo(sock.m_prevAckNo),
106  m_accountedFor(sock.m_accountedFor),
107  m_pType(sock.m_pType),
108  m_fType(sock.m_fType),
109  m_IsCount(sock.m_IsCount)
110 {
111  NS_LOG_FUNCTION (this);
112  NS_LOG_LOGIC ("Invoked the copy constructor");
113  NS_LOG_INFO ("m_minRtt at copy constructor" << m_minRtt);
114 }
115 
117 {
118 }
119 
120 int
122 {
123  NS_LOG_FUNCTION (this);
124  InitializeCwnd();
125  return TcpSocketBase::Listen();
126 }
127 
128 int
130 {
131  NS_LOG_FUNCTION (this << address);
132  InitializeCwnd();
133  return TcpSocketBase::Connect(address);
134 }
135 
136 uint32_t
138 {
139  NS_LOG_FUNCTION (this);
140  return std::min (m_rWnd.Get (), m_cWnd.Get ());
141 }
142 
145 {
146  NS_LOG_FUNCTION (this);
147  return CopyObject<TcpWestwood>(this);
148 }
149 
150 void
152 { // Same as Reno
153  NS_LOG_FUNCTION (this << seq);
154  NS_LOG_LOGIC ("TcpWestwood receieved ACK for seq " << seq <<
155  " cwnd " << m_cWnd <<
156  " ssthresh " << m_ssThresh);
157 
158  // Check for exit condition of fast recovery
159  if (m_inFastRec)
160  {// First new ACK after fast recovery, reset cwnd as in Reno
161  m_cWnd = m_ssThresh;
162  m_inFastRec = false;
163  NS_LOG_INFO ("Reset cwnd to " << m_cWnd);
164  };
165 
166  // Increase of cwnd based on current phase (slow start or congestion avoidance)
167  if (m_cWnd < m_ssThresh)
168  { // Slow start mode, add one segSize to cWnd as in Reno
170  NS_LOG_INFO ("In SlowStart, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
171  }
172  else
173  { // Congestion avoidance mode, increase by (segSize*segSize)/cwnd as in Reno
174  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get();
175  adder = std::max(1.0, adder);
176  m_cWnd += static_cast<uint32_t>(adder);
177  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
178  }
179 
180  // Complete newAck processing
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION (this);
188  int acked = 0;
189  if ((0 != (tcpHeader.GetFlags () & TcpHeader::ACK)) && tcpHeader.GetAckNumber() >= m_prevAckNo)
190  {// It is a duplicate ACK or a new ACK. Old ACK is ignored.
192  {// For Westwood, calculate the number of ACKed segments and estimate the BW
193  acked = CountAck (tcpHeader);
194  EstimateBW (acked, tcpHeader, Time(0));
195  }
197  {// For Weswood+, calculate the number of ACKed segments and update m_ackedSegments
198  if (m_IsCount)
199  {
200  acked = CountAck (tcpHeader);
201  UpdateAckedSegments (acked);
202  }
203  }
204  }
205 
206  TcpSocketBase::ReceivedAck (packet, tcpHeader);
207 }
208 
209 void
210 TcpWestwood::EstimateBW (int acked, const TcpHeader& tcpHeader, Time rtt)
211 {
212  NS_LOG_FUNCTION (this);
214  {
215  // Get the time when the current ACK is received
216  double currentAck = static_cast<double> (Simulator::Now().GetSeconds());
217  // Calculate the BW
218  m_currentBW = acked * m_segmentSize / (currentAck - m_lastAck);
219  // Update the last ACK time
220  m_lastAck = currentAck;
221  }
223  {
224  // Calculate the BW
226  // Reset m_ackedSegments and m_IsCount for the next sampling
227  m_ackedSegments = 0;
228  m_IsCount = false;
229  }
230 
231  // Filter the BW sample
232  Filtering();
233 }
234 
235 int
237 {
238  NS_LOG_FUNCTION (this);
239 
240  // Calculate the number of acknowledged segments based on the received ACK number
241  int cumul_ack = (tcpHeader.GetAckNumber() - m_prevAckNo) / m_segmentSize;
242 
243  if (cumul_ack == 0)
244  {// A DUPACK counts for 1 segment delivered successfully
245  m_accountedFor++;
246  cumul_ack = 1;
247  }
248  if (cumul_ack > 1)
249  {// A delayed ACK or a cumulative ACK after a retransmission
250  // Check how much new data it ACKs
251  if (m_accountedFor >= cumul_ack)
252  {
253  m_accountedFor -= cumul_ack;
254  cumul_ack = 1;
255  }
256  else if (m_accountedFor < cumul_ack)
257  {
258  cumul_ack -= m_accountedFor;
259  m_accountedFor = 0;
260  }
261  }
262 
263  // Update the previous ACK number
264  m_prevAckNo = tcpHeader.GetAckNumber();
265 
266  return cumul_ack;
267 }
268 
269 void
271 {
272  m_ackedSegments += acked;
273 }
274 
275 void
276 TcpWestwood::DupAck (const TcpHeader& header, uint32_t count)
277 {
278  NS_LOG_FUNCTION (this << count << m_cWnd);
279 
280  if (count == 3 && !m_inFastRec)
281  {// Triple duplicate ACK triggers fast retransmit
282  // Adjust cwnd and ssthresh based on the estimated BW
283  m_ssThresh = uint32_t(m_currentBW * static_cast<double> (m_minRtt.GetSeconds()));
284  if (m_cWnd > m_ssThresh)
285  {
286  m_cWnd = m_ssThresh;
287  }
288  m_inFastRec = true;
289  NS_LOG_INFO ("Triple dupack. Enter fast recovery mode. Reset cwnd to " << m_cWnd <<", ssthresh to " << m_ssThresh);
290  DoRetransmit ();
291  }
292  else if (m_inFastRec)
293  {// Increase cwnd for every additional DUPACK as in Reno
295  NS_LOG_INFO ("Dupack in fast recovery mode. Increase cwnd to " << m_cWnd);
297  {
299  }
300  }
301 }
302 
303 void
305 {
306  NS_LOG_FUNCTION (this);
307  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
308  m_inFastRec = false;
309 
310  // If erroneous timeout in closed/timed-wait state, just return
311  if (m_state == CLOSED || m_state == TIME_WAIT)
312  return;
313  // If all data are received, just return
314  if (m_txBuffer->HeadSequence () >= m_nextTxSequence)
315  return;
316 
317  // Upon an RTO, adjust cwnd and ssthresh based on the estimated BW
318  m_ssThresh = std::max (static_cast<double> (2 * m_segmentSize), m_currentBW.Get () * static_cast<double> (m_minRtt.GetSeconds ()));
320 
321  // Restart from highest ACK
322  m_nextTxSequence = m_txBuffer->HeadSequence ();
323  NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd <<
324  ", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence);
325 
326  // Retransmit the packet
327  DoRetransmit ();
328 }
329 
330 void
332 {
334 
335  // Calculate m_lastRtt
336  TcpSocketBase::EstimateRtt (tcpHeader);
337 
338  // Update minRtt
339  if (m_minRtt == Time (0))
340  {
342  }
343  else
344  {
345  if (m_lastRtt < m_minRtt)
346  {
348  }
349  }
350 
351  // For Westwood+, start running a clock on the currently estimated RTT if possible
352  // to trigger a new BW sampling event
354  {
355  if(m_lastRtt != Time (0) && m_state == ESTABLISHED && !m_IsCount)
356  {
357  m_IsCount = true;
360  }
361  }
362 }
363 
364 void
366 {
367  NS_LOG_FUNCTION (this);
368 
369  double alpha = 0.9;
370 
371  if (m_fType == TcpWestwood::NONE)
372  {
373  }
374  else if (m_fType == TcpWestwood::TUSTIN)
375  {
376  double sample_bwe = m_currentBW;
377  m_currentBW = (alpha * m_lastBW) + ((1 - alpha) * ((sample_bwe + m_lastSampleBW) / 2));
378  m_lastSampleBW = sample_bwe;
380  }
381 }
382 
383 void
384 TcpWestwood::SetSegSize (uint32_t size)
385 {
386  NS_ABORT_MSG_UNLESS(m_state == CLOSED, "TcpWestwood::SetSegSize() cannot change segment size after connection started.");
387  m_segmentSize = size;
388 }
389 
390 void
392 {
393  NS_LOG_FUNCTION (this);
394  NS_ABORT_MSG_UNLESS (m_state == CLOSED, "TcpWestwood::SetSSThresh() cannot change initial ssThresh after connection started.");
395  m_initialSsThresh = threshold;
396 }
397 
398 uint32_t
400 {
401  NS_LOG_FUNCTION (this);
402  return m_initialSsThresh;
403 }
404 
405 void
407 {
408  NS_ABORT_MSG_UNLESS(m_state == CLOSED, "TcpWestwood::SetInitialCwnd() cannot change initial cwnd after connection started.");
409  m_initialCWnd = cwnd;
410 }
411 
412 uint32_t
414 {
415  NS_LOG_FUNCTION (this);
416  return m_initialCWnd;
417 }
418 
419 void
421 {
422  NS_LOG_FUNCTION (this);
423  /*
424  * Initialize congestion window, default to 1 MSS (RFC2001, sec.1) and must
425  * not be larger than 2 MSS (RFC2581, sec.3.1). Both m_initiaCWnd and
426  * m_segmentSize are set by the attribute system in ns3::TcpSocket.
427  */
430 }
431 
432 void
433 TcpWestwood::ScaleSsThresh (uint8_t scaleFactor)
434 {
435  m_ssThresh <<= scaleFactor;
436 }
437 
438 
439 } // namespace ns3
void InitializeCwnd(void)
Initialize cwnd at the beginning of a connection.
EventId m_bwEstimateEvent
The BW estimation event for Westwood+.
Definition: tcp-westwood.h:185
int CountAck(const TcpHeader &tcpHeader)
Calculate the number of acknowledged packets upon the receipt of an ACK packet.
virtual int Listen(void)
Listen for incoming connections.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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 "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:161
TracedValue< Time > m_lastRtt
Last RTT sample collected.
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: enum.h:209
SequenceNumber32 GetAckNumber() const
Get the ACK number.
Definition: tcp-header.cc:149
double m_lastBW
Last bandwidth sample after being filtered.
Definition: tcp-westwood.h:175
int m_accountedFor
The number of received DUPACKs.
Definition: tcp-westwood.h:179
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void DupAck(const TcpHeader &t, uint32_t count)
Received dupack (duplicate ACK)
#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.
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
TracedValue< SequenceNumber32 > m_nextTxSequence
Next seqnum to be sent (SND.NXT), ReTx pushes it back.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
virtual void ReceivedAck(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Received an ACK packet.
T Get(void) const
Get the underlying value.
Definition: traced-value.h:186
TracedValue< TcpStates_t > m_state
TCP state.
virtual uint32_t Window(void)
Return the max possible number of unacked bytes.
virtual void Retransmit(void)
Halving cwnd and call DoRetransmit()
virtual int Listen(void)
Listen for incoming connections.
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.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:334
TracedValue< uint32_t > m_cWnd
Congestion window.
Definition: tcp-westwood.h:167
enum FilterType m_fType
0 for none, 1 for Tustin
Definition: tcp-westwood.h:181
virtual void EstimateRtt(const TcpHeader &tcpHeader)
Take into account the packet for RTT estimation.
Hold variables of type enum.
Definition: enum.h:54
Ptr< TcpTxBuffer > m_txBuffer
Tx buffer.
void Filtering(void)
Tustin filter.
virtual uint32_t GetInitialCwnd(void) const
Get the initial Congestion Window.
A base class for implementation of a stream socket using TCP.
bool m_inFastRec
Currently in fast recovery if TRUE.
Definition: tcp-westwood.h:171
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
void UpdateAckedSegments(int acked)
Update the total number of acknowledged packets during the current RTT.
double m_lastAck
The time last ACK was received.
Definition: tcp-westwood.h:177
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Header for the Transmission Control Protocol.
Definition: tcp-header.h:44
SequenceNumber32 m_prevAckNo
Previously received ACK number.
Definition: tcp-westwood.h:178
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-westwood.cc:52
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
TracedValue< uint32_t > m_ssThresh
Slow Start Threshold.
Definition: tcp-westwood.h:168
virtual void SetSegSize(uint32_t size)
Set the segment size.
virtual ~TcpWestwood(void)
virtual void NewAck(SequenceNumber32 const &seq)
Update buffers w.r.t.
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
int m_ackedSegments
The number of segments ACKed between RTTs.
Definition: tcp-westwood.h:183
uint32_t m_initialSsThresh
Initial Slow Start Threshold value.
Definition: tcp-westwood.h:170
virtual void ReceivedAck(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Process the newly received ACK.
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 void SetInitialCwnd(uint32_t cwnd)
Set the initial Congestion Window.
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
double m_lastSampleBW
Last bandwidth sample.
Definition: tcp-westwood.h:174
virtual uint32_t GetInitialSSThresh(void) const
Get the initial Slow Start Threshold.
TracedValue< double > m_currentBW
Current value of the estimated BW.
Definition: tcp-westwood.h:173
bool m_IsCount
Start keeping track of m_ackedSegments for Westwood+ if TRUE.
Definition: tcp-westwood.h:184
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
void EstimateBW(int acked, const TcpHeader &tcpHeader, Time rtt)
Estimate the network's bandwidth.
uint32_t m_initialCWnd
Initial cWnd value.
Definition: tcp-westwood.h:169
Time m_minRtt
Minimum RTT.
Definition: tcp-westwood.h:176
tuple address
Definition: first.py:37
virtual void ScaleSsThresh(uint8_t scaleFactor)
Scale the initial SsThresh value to the correct one.
EventId m_sendPendingDataEvent
micro-delay event to send pending data
virtual int Connect(const Address &address)
Initiate a connection to a remote host.
a unique identifier for an interface.
Definition: type-id.h:57
virtual void EstimateRtt(const TcpHeader &header)
Estimate the RTT, record the minimum value, and run a clock on the RTT to trigger Westwood+ bandwidth...
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 void SetInitialSSThresh(uint32_t threshold)
Set the initial Slow Start Threshold.
An implementation of a stream socket using TCP.
Definition: tcp-westwood.h:62
enum ProtocolType m_pType
0 for Westwood, 1 for Westwood+
Definition: tcp-westwood.h:180