A Discrete-Event Network Simulator
API
tcp-vegas.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 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  * Author: Truc Anh N. Nguyen <annguyen@ittc.ku.edu>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  */
26 
27 #include "tcp-vegas.h"
28 #include "tcp-socket-state.h"
29 
30 #include "ns3/log.h"
31 
32 namespace ns3 {
33 
34 NS_LOG_COMPONENT_DEFINE ("TcpVegas");
36 
37 TypeId
39 {
40  static TypeId tid = TypeId ("ns3::TcpVegas")
42  .AddConstructor<TcpVegas> ()
43  .SetGroupName ("Internet")
44  .AddAttribute ("Alpha", "Lower bound of packets in network",
45  UintegerValue (2),
47  MakeUintegerChecker<uint32_t> ())
48  .AddAttribute ("Beta", "Upper bound of packets in network",
49  UintegerValue (4),
51  MakeUintegerChecker<uint32_t> ())
52  .AddAttribute ("Gamma", "Limit on increase",
53  UintegerValue (1),
55  MakeUintegerChecker<uint32_t> ())
56  ;
57  return tid;
58 }
59 
61  : TcpNewReno (),
62  m_alpha (2),
63  m_beta (4),
64  m_gamma (1),
65  m_baseRtt (Time::Max ()),
66  m_minRtt (Time::Max ()),
67  m_cntRtt (0),
68  m_doingVegasNow (true),
69  m_begSndNxt (0)
70 {
71  NS_LOG_FUNCTION (this);
72 }
73 
75  : TcpNewReno (sock),
76  m_alpha (sock.m_alpha),
77  m_beta (sock.m_beta),
78  m_gamma (sock.m_gamma),
79  m_baseRtt (sock.m_baseRtt),
80  m_minRtt (sock.m_minRtt),
81  m_cntRtt (sock.m_cntRtt),
82  m_doingVegasNow (true),
83  m_begSndNxt (0)
84 {
85  NS_LOG_FUNCTION (this);
86 }
87 
89 {
90  NS_LOG_FUNCTION (this);
91 }
92 
95 {
96  return CopyObject<TcpVegas> (this);
97 }
98 
99 void
100 TcpVegas::PktsAcked (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked,
101  const Time& rtt)
102 {
103  NS_LOG_FUNCTION (this << tcb << segmentsAcked << rtt);
104 
105  if (rtt.IsZero ())
106  {
107  return;
108  }
109 
110  m_minRtt = std::min (m_minRtt, rtt);
111  NS_LOG_DEBUG ("Updated m_minRtt = " << m_minRtt);
112 
113  m_baseRtt = std::min (m_baseRtt, rtt);
114  NS_LOG_DEBUG ("Updated m_baseRtt = " << m_baseRtt);
115 
116  // Update RTT counter
117  m_cntRtt++;
118  NS_LOG_DEBUG ("Updated m_cntRtt = " << m_cntRtt);
119 }
120 
121 void
123 {
124  NS_LOG_FUNCTION (this << tcb);
125 
126  m_doingVegasNow = true;
128  m_cntRtt = 0;
129  m_minRtt = Time::Max ();
130 }
131 
132 void
134 {
135  NS_LOG_FUNCTION (this);
136 
137  m_doingVegasNow = false;
138 }
139 
140 void
142  const TcpSocketState::TcpCongState_t newState)
143 {
144  NS_LOG_FUNCTION (this << tcb << newState);
145  if (newState == TcpSocketState::CA_OPEN)
146  {
147  EnableVegas (tcb);
148  }
149  else
150  {
151  DisableVegas ();
152  }
153 }
154 
155 void
156 TcpVegas::IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
157 {
158  NS_LOG_FUNCTION (this << tcb << segmentsAcked);
159 
160  if (!m_doingVegasNow)
161  {
162  // If Vegas is not on, we follow NewReno algorithm
163  NS_LOG_LOGIC ("Vegas is not turned on, we follow NewReno algorithm.");
164  TcpNewReno::IncreaseWindow (tcb, segmentsAcked);
165  return;
166  }
167 
168  if (tcb->m_lastAckedSeq >= m_begSndNxt)
169  { // A Vegas cycle has finished, we do Vegas cwnd adjustment every RTT.
170 
171  NS_LOG_LOGIC ("A Vegas cycle has finished, we adjust cwnd once per RTT.");
172 
173  // Save the current right edge for next Vegas cycle
175 
176  /*
177  * We perform Vegas calculations only if we got enough RTT samples to
178  * insure that at least 1 of those samples wasn't from a delayed ACK.
179  */
180  if (m_cntRtt <= 2)
181  { // We do not have enough RTT samples, so we should behave like Reno
182  NS_LOG_LOGIC ("We do not have enough RTT samples to do Vegas, so we behave like NewReno.");
183  TcpNewReno::IncreaseWindow (tcb, segmentsAcked);
184  }
185  else
186  {
187  NS_LOG_LOGIC ("We have enough RTT samples to perform Vegas calculations");
188  /*
189  * We have enough RTT samples to perform Vegas algorithm.
190  * Now we need to determine if cwnd should be increased or decreased
191  * based on the calculated difference between the expected rate and actual sending
192  * rate and the predefined thresholds (alpha, beta, and gamma).
193  */
194  uint32_t diff;
195  uint32_t targetCwnd;
196  uint32_t segCwnd = tcb->GetCwndInSegments ();
197 
198  /*
199  * Calculate the cwnd we should have. baseRtt is the minimum RTT
200  * per-connection, minRtt is the minimum RTT in this window
201  *
202  * little trick:
203  * desidered throughput is currentCwnd * baseRtt
204  * target cwnd is throughput / minRtt
205  */
206  double tmp = m_baseRtt.GetSeconds () / m_minRtt.GetSeconds ();
207  targetCwnd = static_cast<uint32_t> (segCwnd * tmp);
208  NS_LOG_DEBUG ("Calculated targetCwnd = " << targetCwnd);
209  NS_ASSERT (segCwnd >= targetCwnd); // implies baseRtt <= minRtt
210 
211  /*
212  * Calculate the difference between the expected cWnd and
213  * the actual cWnd
214  */
215  diff = segCwnd - targetCwnd;
216  NS_LOG_DEBUG ("Calculated diff = " << diff);
217 
218  if (diff > m_gamma && (tcb->m_cWnd < tcb->m_ssThresh))
219  {
220  /*
221  * We are going too fast. We need to slow down and change from
222  * slow-start to linear increase/decrease mode by setting cwnd
223  * to target cwnd. We add 1 because of the integer truncation.
224  */
225  NS_LOG_LOGIC ("We are going too fast. We need to slow down and "
226  "change to linear increase/decrease mode.");
227  segCwnd = std::min (segCwnd, targetCwnd + 1);
228  tcb->m_cWnd = segCwnd * tcb->m_segmentSize;
229  tcb->m_ssThresh = GetSsThresh (tcb, 0);
230  NS_LOG_DEBUG ("Updated cwnd = " << tcb->m_cWnd <<
231  " ssthresh=" << tcb->m_ssThresh);
232  }
233  else if (tcb->m_cWnd < tcb->m_ssThresh)
234  { // Slow start mode
235  NS_LOG_LOGIC ("We are in slow start and diff < m_gamma, so we "
236  "follow NewReno slow start");
237  TcpNewReno::SlowStart (tcb, segmentsAcked);
238  }
239  else
240  { // Linear increase/decrease mode
241  NS_LOG_LOGIC ("We are in linear increase/decrease mode");
242  if (diff > m_beta)
243  {
244  // We are going too fast, so we slow down
245  NS_LOG_LOGIC ("We are going too fast, so we slow down by decrementing cwnd");
246  segCwnd--;
247  tcb->m_cWnd = segCwnd * tcb->m_segmentSize;
248  tcb->m_ssThresh = GetSsThresh (tcb, 0);
249  NS_LOG_DEBUG ("Updated cwnd = " << tcb->m_cWnd <<
250  " ssthresh=" << tcb->m_ssThresh);
251  }
252  else if (diff < m_alpha)
253  {
254  // We are going too slow (having too little data in the network),
255  // so we speed up.
256  NS_LOG_LOGIC ("We are going too slow, so we speed up by incrementing cwnd");
257  segCwnd++;
258  tcb->m_cWnd = segCwnd * tcb->m_segmentSize;
259  NS_LOG_DEBUG ("Updated cwnd = " << tcb->m_cWnd <<
260  " ssthresh=" << tcb->m_ssThresh);
261  }
262  else
263  {
264  // We are going at the right speed
265  NS_LOG_LOGIC ("We are sending at the right speed");
266  }
267  }
268  tcb->m_ssThresh = std::max (tcb->m_ssThresh, 3 * tcb->m_cWnd / 4);
269  NS_LOG_DEBUG ("Updated ssThresh = " << tcb->m_ssThresh);
270  }
271 
272  // Reset cntRtt & minRtt every RTT
273  m_cntRtt = 0;
274  m_minRtt = Time::Max ();
275  }
276  else if (tcb->m_cWnd < tcb->m_ssThresh)
277  {
278  TcpNewReno::SlowStart (tcb, segmentsAcked);
279  }
280 }
281 
282 std::string
284 {
285  return "TcpVegas";
286 }
287 
288 uint32_t
290  uint32_t bytesInFlight)
291 {
292  NS_LOG_FUNCTION (this << tcb << bytesInFlight);
293  return std::max (std::min (tcb->m_ssThresh.Get (), tcb->m_cWnd.Get () - tcb->m_segmentSize), 2 * tcb->m_segmentSize);
294 }
295 
296 } // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
The NewReno implementation.
virtual void IncreaseWindow(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
Try to increase the cWnd following the NewReno specification.
virtual uint32_t SlowStart(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
Tcp NewReno slow start algorithm.
uint32_t m_segmentSize
Segment size.
uint32_t GetCwndInSegments() const
Get cwnd in segments rather than bytes.
TcpCongState_t
Definition of the Congestion state machine.
@ CA_OPEN
Normal state, no dubious events.
SequenceNumber32 m_lastAckedSeq
Last sequence ACKed.
TracedValue< uint32_t > m_cWnd
Congestion window.
TracedValue< SequenceNumber32 > m_nextTxSequence
Next seqnum to be sent (SND.NXT), ReTx pushes it back.
TracedValue< uint32_t > m_ssThresh
Slow start threshold.
An implementation of TCP Vegas.
Definition: tcp-vegas.h:66
virtual std::string GetName() const
Get the name of the congestion control algorithm.
Definition: tcp-vegas.cc:283
virtual void CongestionStateSet(Ptr< TcpSocketState > tcb, const TcpSocketState::TcpCongState_t newState)
Enable/disable Vegas algorithm depending on the congestion state.
Definition: tcp-vegas.cc:141
virtual ~TcpVegas(void)
Definition: tcp-vegas.cc:88
void DisableVegas()
Stop taking Vegas samples.
Definition: tcp-vegas.cc:133
uint32_t m_cntRtt
Number of RTT measurements during last RTT.
Definition: tcp-vegas.h:164
TcpVegas(void)
Create an unbound tcp socket.
Definition: tcp-vegas.cc:60
uint32_t m_alpha
Alpha threshold, lower bound of packets in network.
Definition: tcp-vegas.h:159
uint32_t m_beta
Beta threshold, upper bound of packets in network.
Definition: tcp-vegas.h:160
bool m_doingVegasNow
If true, do Vegas for this RTT.
Definition: tcp-vegas.h:165
void EnableVegas(Ptr< TcpSocketState > tcb)
Enable Vegas algorithm to start taking Vegas samples.
Definition: tcp-vegas.cc:122
Time m_minRtt
Minimum of all RTT measurements within last RTT.
Definition: tcp-vegas.h:163
virtual void PktsAcked(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked, const Time &rtt)
Compute RTTs needed to execute Vegas algorithm.
Definition: tcp-vegas.cc:100
Time m_baseRtt
Minimum of all Vegas RTT measurements seen during connection.
Definition: tcp-vegas.h:162
uint32_t m_gamma
Gamma threshold, limit on increase.
Definition: tcp-vegas.h:161
virtual void IncreaseWindow(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
Adjust cwnd following Vegas linear increase/decrease algorithm.
Definition: tcp-vegas.cc:156
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-vegas.cc:38
virtual uint32_t GetSsThresh(Ptr< const TcpSocketState > tcb, uint32_t bytesInFlight)
Get slow start threshold following Vegas principle.
Definition: tcp-vegas.cc:289
virtual Ptr< TcpCongestionOps > Fork()
Copy the congestion control algorithm across sockets.
Definition: tcp-vegas.cc:94
SequenceNumber32 m_begSndNxt
Right edge during last RTT.
Definition: tcp-vegas.h:166
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
bool IsZero(void) const
Exactly equivalent to t == 0.
Definition: nstime.h:300
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition: nstime.h:282
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:67
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
int64x64_t Max(const int64x64_t &a, const int64x64_t &b)
Maximum.
Definition: int64x64.h:230
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.