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  .AddAttribute("FilterType", "Use this to choose no filter or Tustin's approximation filter",
61  .AddAttribute("ProtocolType", "Use this to let the code run as Westwood or WestwoodPlus",
65  .AddTraceSource("EstimatedBW", "The estimated bandwidth",
67  "ns3::TracedValueCallback::Double");
68  return tid;
69 }
70 
72  m_inFastRec(false),
73  m_currentBW(0),
74  m_lastSampleBW(0),
75  m_lastBW(0),
76  m_minRtt(0),
77  m_lastAck(0),
78  m_prevAckNo(0),
79  m_accountedFor(0),
80  m_ackedSegments(0),
81  m_IsCount(false)
82 {
83  NS_LOG_FUNCTION (this);
84 }
85 
87  TcpSocketBase(sock),
88  m_inFastRec(false),
89  m_currentBW(sock.m_currentBW),
90  m_lastSampleBW(sock.m_lastSampleBW),
91  m_lastBW(sock.m_lastBW),
92  m_minRtt(sock.m_minRtt),
93  m_lastAck(sock.m_lastAck),
94  m_prevAckNo(sock.m_prevAckNo),
95  m_accountedFor(sock.m_accountedFor),
96  m_pType(sock.m_pType),
97  m_fType(sock.m_fType),
98  m_IsCount(sock.m_IsCount)
99 {
100  NS_LOG_FUNCTION (this);
101  NS_LOG_LOGIC ("Invoked the copy constructor");
102  NS_LOG_INFO ("m_minRtt at copy constructor" << m_minRtt);
103 }
104 
106 {
107 }
108 
111 {
112  NS_LOG_FUNCTION (this);
113  return CopyObject<TcpWestwood>(this);
114 }
115 
116 void
118 { // Same as Reno
119  NS_LOG_FUNCTION (this << seq);
120  NS_LOG_LOGIC ("TcpWestwood receieved ACK for seq " << seq <<
121  " cwnd " << m_cWnd <<
122  " ssthresh " << m_ssThresh);
123 
124  // Check for exit condition of fast recovery
125  if (m_inFastRec)
126  {// First new ACK after fast recovery, reset cwnd as in Reno
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 as in Reno
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 as in Reno
140  double adder = static_cast<double> (m_segmentSize * m_segmentSize) / m_cWnd.Get();
141  adder = std::max(1.0, adder);
142  m_cWnd += static_cast<uint32_t>(adder);
143  NS_LOG_INFO ("In CongAvoid, updated to cwnd " << m_cWnd << " ssthresh " << m_ssThresh);
144  }
145 
146  // Complete newAck processing
148 }
149 
150 void
152 {
153  NS_LOG_FUNCTION (this);
154  int acked = 0;
155  if ((0 != (tcpHeader.GetFlags () & TcpHeader::ACK)) && tcpHeader.GetAckNumber() >= m_prevAckNo)
156  {// It is a duplicate ACK or a new ACK. Old ACK is ignored.
158  {// For Westwood, calculate the number of ACKed segments and estimate the BW
159  acked = CountAck (tcpHeader);
160  EstimateBW (acked, tcpHeader, Time(0));
161  }
163  {// For Weswood+, calculate the number of ACKed segments and update m_ackedSegments
164  if (m_IsCount)
165  {
166  acked = CountAck (tcpHeader);
167  UpdateAckedSegments (acked);
168  }
169  }
170  }
171 
172  TcpSocketBase::ReceivedAck (packet, tcpHeader);
173 }
174 
175 void
176 TcpWestwood::EstimateBW (int acked, const TcpHeader& tcpHeader, Time rtt)
177 {
178  NS_LOG_FUNCTION (this);
180  {
181  // Get the time when the current ACK is received
182  double currentAck = static_cast<double> (Simulator::Now().GetSeconds());
183  // Calculate the BW
184  m_currentBW = acked * m_segmentSize / (currentAck - m_lastAck);
185  // Update the last ACK time
186  m_lastAck = currentAck;
187  }
189  {
190  // Calculate the BW
192  // Reset m_ackedSegments and m_IsCount for the next sampling
193  m_ackedSegments = 0;
194  m_IsCount = false;
195  }
196 
197  // Filter the BW sample
198  Filtering();
199 }
200 
201 int
203 {
204  NS_LOG_FUNCTION (this);
205 
206  // Calculate the number of acknowledged segments based on the received ACK number
207  int cumul_ack = (tcpHeader.GetAckNumber() - m_prevAckNo) / m_segmentSize;
208 
209  if (cumul_ack == 0)
210  {// A DUPACK counts for 1 segment delivered successfully
211  m_accountedFor++;
212  cumul_ack = 1;
213  }
214  if (cumul_ack > 1)
215  {// A delayed ACK or a cumulative ACK after a retransmission
216  // Check how much new data it ACKs
217  if (m_accountedFor >= cumul_ack)
218  {
219  m_accountedFor -= cumul_ack;
220  cumul_ack = 1;
221  }
222  else if (m_accountedFor < cumul_ack)
223  {
224  cumul_ack -= m_accountedFor;
225  m_accountedFor = 0;
226  }
227  }
228 
229  // Update the previous ACK number
230  m_prevAckNo = tcpHeader.GetAckNumber();
231 
232  return cumul_ack;
233 }
234 
235 void
237 {
238  m_ackedSegments += acked;
239 }
240 
241 void
242 TcpWestwood::DupAck (const TcpHeader& header, uint32_t count)
243 {
244  NS_LOG_FUNCTION (this << count << m_cWnd);
245 
246  if (count == 3 && !m_inFastRec)
247  {// Triple duplicate ACK triggers fast retransmit
248  // Adjust cwnd and ssthresh based on the estimated BW
249  m_ssThresh = uint32_t(m_currentBW * static_cast<double> (m_minRtt.GetSeconds()));
250  if (m_cWnd > m_ssThresh)
251  {
252  m_cWnd = m_ssThresh;
253  }
254  m_inFastRec = true;
255  NS_LOG_INFO ("Triple dupack. Enter fast recovery mode. Reset cwnd to " << m_cWnd <<", ssthresh to " << m_ssThresh);
256  DoRetransmit ();
257  }
258  else if (m_inFastRec)
259  {// Increase cwnd for every additional DUPACK as in Reno
261  NS_LOG_INFO ("Dupack in fast recovery mode. Increase cwnd to " << m_cWnd);
263  {
265  }
266  }
267 }
268 
269 void
271 {
272  NS_LOG_FUNCTION (this);
273  NS_LOG_LOGIC (this << " ReTxTimeout Expired at time " << Simulator::Now ().GetSeconds ());
274  m_inFastRec = false;
275 
276  // If erroneous timeout in closed/timed-wait state, just return
277  if (m_state == CLOSED || m_state == TIME_WAIT)
278  return;
279  // If all data are received, just return
280  if (m_txBuffer->HeadSequence () >= m_nextTxSequence)
281  return;
282 
283  // Upon an RTO, adjust cwnd and ssthresh based on the estimated BW
284  m_ssThresh = std::max (static_cast<double> (2 * m_segmentSize), m_currentBW.Get () * static_cast<double> (m_minRtt.GetSeconds ()));
286 
287  // Restart from highest ACK
288  m_nextTxSequence = m_txBuffer->HeadSequence ();
289  NS_LOG_INFO ("RTO. Reset cwnd to " << m_cWnd <<
290  ", ssthresh to " << m_ssThresh << ", restart from seqnum " << m_nextTxSequence);
291 
292  // Retransmit the packet
293  DoRetransmit ();
294 }
295 
296 void
298 {
300 
301  // Calculate m_lastRtt
302  TcpSocketBase::EstimateRtt (tcpHeader);
303 
304  // Update minRtt
305  if (m_minRtt == Time (0))
306  {
308  }
309  else
310  {
311  if (m_lastRtt < m_minRtt)
312  {
314  }
315  }
316 
317  // For Westwood+, start running a clock on the currently estimated RTT if possible
318  // to trigger a new BW sampling event
320  {
321  if(m_lastRtt != Time (0) && m_state == ESTABLISHED && !m_IsCount)
322  {
323  m_IsCount = true;
326  }
327  }
328 }
329 
330 void
332 {
333  NS_LOG_FUNCTION (this);
334 
335  double alpha = 0.9;
336 
337  if (m_fType == TcpWestwood::NONE)
338  {
339  }
340  else if (m_fType == TcpWestwood::TUSTIN)
341  {
342  double sample_bwe = m_currentBW;
343  m_currentBW = (alpha * m_lastBW) + ((1 - alpha) * ((sample_bwe + m_lastSampleBW) / 2));
344  m_lastSampleBW = sample_bwe;
346  }
347 }
348 
349 } // namespace ns3
EventId m_bwEstimateEvent
The BW estimation event for Westwood+.
Definition: tcp-westwood.h:163
int CountAck(const TcpHeader &tcpHeader)
Calculate the number of acknowledged packets upon the receipt of an ACK packet.
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
Connection established.
Definition: tcp-socket.h:70
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:153
Socket is finished.
Definition: tcp-socket.h:65
int m_accountedFor
The number of received DUPACKs.
Definition: tcp-westwood.h:157
TracedValue< uint32_t > m_cWnd
Congestion window.
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
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:719
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.
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:217
TracedValue< TcpStates_t > m_state
TCP state.
virtual void Retransmit(void)
Halving cwnd and call DoRetransmit()
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:341
enum FilterType m_fType
0 for none, 1 for Tustin
Definition: tcp-westwood.h:159
virtual void EstimateRtt(const TcpHeader &tcpHeader)
Take into account the packet for RTT estimation.
Hold variables of type enum.
Definition: enum.h:54
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1216
Ptr< TcpTxBuffer > m_txBuffer
Tx buffer.
void Filtering(void)
Tustin filter.
A base class for implementation of a stream socket using TCP.
bool m_inFastRec
Currently in fast recovery if TRUE.
Definition: tcp-westwood.h:149
#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:155
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:156
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
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:161
virtual void ReceivedAck(Ptr< Packet > packet, const TcpHeader &tcpHeader)
Process the newly received ACK.
bool m_connected
Connection established.
virtual Ptr< TcpSocketBase > Fork(void)
Call CopyObject<> to clone me.
double m_lastSampleBW
Last bandwidth sample.
Definition: tcp-westwood.h:152
TracedValue< double > m_currentBW
Current value of the estimated BW.
Definition: tcp-westwood.h:151
Timeout to catch resent junk before entering closed, can only be entered from FIN_WAIT2 or CLOSING...
Definition: tcp-socket.h:82
bool m_IsCount
Start keeping track of m_ackedSegments for Westwood+ if TRUE.
Definition: tcp-westwood.h:162
TracedValue< uint32_t > m_ssThresh
Slow start threshold.
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.
Time m_minRtt
Minimum RTT.
Definition: tcp-westwood.h:154
EventId m_sendPendingDataEvent
micro-delay event to send pending data
a unique identifier for an interface.
Definition: type-id.h:58
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)
Set the parent TypeId.
Definition: type-id.cc:826
bool SendPendingData(bool withAck=false)
Send as much pending data as possible according to the Tx window.
virtual void DoRetransmit(void)
Retransmit the oldest packet.
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:158