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