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