A Discrete-Event Network Simulator
API
tcp-veno.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-veno.h"
28 #include "ns3/tcp-socket-base.h"
29 #include "ns3/log.h"
30 
31 namespace ns3 {
32 
33 NS_LOG_COMPONENT_DEFINE ("TcpVeno");
35 
36 TypeId
38 {
39  static TypeId tid = TypeId ("ns3::TcpVeno")
41  .AddConstructor<TcpVeno> ()
42  .SetGroupName ("Internet")
43  .AddAttribute ("Beta", "Threshold for congestion detection",
44  UintegerValue (3),
46  MakeUintegerChecker<uint32_t> ())
47  ;
48  return tid;
49 }
50 
52  : TcpNewReno (),
53  m_baseRtt (Time::Max ()),
54  m_minRtt (Time::Max ()),
55  m_cntRtt (0),
56  m_doingVenoNow (true),
57  m_diff (0),
58  m_inc (true),
59  m_ackCnt (0),
60  m_beta (6)
61 {
62  NS_LOG_FUNCTION (this);
63 }
64 
66  : TcpNewReno (sock),
67  m_baseRtt (sock.m_baseRtt),
68  m_minRtt (sock.m_minRtt),
69  m_cntRtt (sock.m_cntRtt),
70  m_doingVenoNow (true),
71  m_diff (0),
72  m_inc (true),
73  m_ackCnt (sock.m_ackCnt),
74  m_beta (sock.m_beta)
75 {
76  NS_LOG_FUNCTION (this);
77 }
78 
80 {
81  NS_LOG_FUNCTION (this);
82 }
83 
86 {
87  return CopyObject<TcpVeno> (this);
88 }
89 
90 void
91 TcpVeno::PktsAcked (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked,
92  const Time& rtt)
93 {
94  NS_LOG_FUNCTION (this << tcb << segmentsAcked << rtt);
95 
96  if (rtt.IsZero ())
97  {
98  return;
99  }
100 
101  m_minRtt = std::min (m_minRtt, rtt);
102  NS_LOG_DEBUG ("Updated m_minRtt= " << m_minRtt);
103 
104 
105  m_baseRtt = std::min (m_baseRtt, rtt);
106  NS_LOG_DEBUG ("Updated m_baseRtt= " << m_baseRtt);
107 
108  // Update RTT counter
109  m_cntRtt++;
110  NS_LOG_DEBUG ("Updated m_cntRtt= " << m_cntRtt);
111 }
112 
113 void
115 {
116  NS_LOG_FUNCTION (this);
117 
118  m_doingVenoNow = true;
119  m_minRtt = Time::Max ();
120 }
121 
122 void
124 {
125  NS_LOG_FUNCTION (this);
126 
127  m_doingVenoNow = false;
128 }
129 
130 void
132  const TcpSocketState::TcpCongState_t newState)
133 {
134  NS_LOG_FUNCTION (this << tcb << newState);
135  if (newState == TcpSocketState::CA_OPEN)
136  {
137  EnableVeno ();
138  NS_LOG_LOGIC ("Veno is now on.");
139  }
140  else
141  {
142  DisableVeno ();
143  NS_LOG_LOGIC ("Veno is turned off.");
144  }
145 }
146 
147 void
148 TcpVeno::IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
149 {
150  NS_LOG_FUNCTION (this << tcb << segmentsAcked);
151 
152  // Always calculate m_diff, even if we are not doing Veno now
153  uint32_t targetCwnd;
154  uint32_t segCwnd = tcb->GetCwndInSegments ();
155 
156  /*
157  * Calculate the cwnd we should have. baseRtt is the minimum RTT
158  * per-connection, minRtt is the minimum RTT in this window
159  *
160  * little trick:
161  * desidered throughput is currentCwnd * baseRtt
162  * target cwnd is throughput / minRtt
163  */
164  double tmp = m_baseRtt.GetSeconds () / m_minRtt.GetSeconds ();
165  targetCwnd = segCwnd * tmp;
166  NS_LOG_DEBUG ("Calculated targetCwnd = " << targetCwnd);
167  NS_ASSERT (segCwnd >= targetCwnd); // implies baseRtt <= minRtt
168 
169  // Calculate the difference between actual and target cwnd
170  m_diff = segCwnd - targetCwnd;
171  NS_LOG_DEBUG ("Calculated m_diff = " << m_diff);
172 
173  if (!m_doingVenoNow)
174  {
175  // If Veno is not on, we follow NewReno algorithm
176  NS_LOG_LOGIC ("Veno is not turned on, we follow NewReno algorithm.");
177  TcpNewReno::IncreaseWindow (tcb, segmentsAcked);
178  return;
179  }
180 
181  // We do the Veno calculations only if we got enough RTT samples
182  if (m_cntRtt <= 2)
183  { // We do not have enough RTT samples, so we should behave like NewReno
184  NS_LOG_LOGIC ("We do not have enough RTT samples to perform Veno "
185  "calculations, we behave like NewReno.");
186  TcpNewReno::IncreaseWindow (tcb, segmentsAcked);
187  }
188  else
189  {
190  NS_LOG_LOGIC ("We have enough RTT samples to perform Veno calculations.");
191 
192  if (tcb->m_cWnd < tcb->m_ssThresh)
193  { // Slow start mode. Veno employs same slow start algorithm as NewReno's.
194  NS_LOG_LOGIC ("We are in slow start, behave like NewReno.");
195  segmentsAcked = TcpNewReno::SlowStart (tcb, segmentsAcked);
196  }
197  else
198  { // Congestion avoidance mode
199  NS_LOG_LOGIC ("We are in congestion avoidance, execute Veno additive "
200  "increase algo.");
201 
202  if (m_diff < m_beta)
203  {
204  // Available bandwidth is not fully utilized,
205  // increase cwnd by 1 every RTT
206  NS_LOG_LOGIC ("Available bandwidth not fully utilized, increase "
207  "cwnd by 1 every RTT");
208  TcpNewReno::CongestionAvoidance (tcb, segmentsAcked);
209  }
210  else
211  {
212  // Available bandwidth is fully utilized,
213  // increase cwnd by 1 every other RTT
214  NS_LOG_LOGIC ("Available bandwidth fully utilized, increase cwnd "
215  "by 1 every other RTT");
216  if (m_inc)
217  {
218  TcpNewReno::CongestionAvoidance (tcb, segmentsAcked);
219  m_inc = false;
220  }
221  else
222  {
223  m_inc = true;
224  }
225  }
226  }
227  }
228 
229  // Reset cntRtt & minRtt every RTT
230  m_cntRtt = 0;
231  m_minRtt = Time::Max ();
232 }
233 
234 std::string
236 {
237  return "TcpVeno";
238 }
239 
240 uint32_t
242  uint32_t bytesInFlight)
243 {
244  NS_LOG_FUNCTION (this << tcb << bytesInFlight);
245 
246  if (m_diff < m_beta)
247  {
248  // random loss due to bit errors is most likely to have occurred,
249  // we cut cwnd by 1/5
250  NS_LOG_LOGIC ("Random loss is most likely to have occurred, "
251  "cwnd is reduced by 1/5");
252  static double tmp = 4.0/5.0;
253  return std::max (static_cast<uint32_t> (bytesInFlight * tmp),
254  2 * tcb->m_segmentSize);
255  }
256  else
257  {
258  // congestion-based loss is most likely to have occurred,
259  // we reduce cwnd by 1/2 as in NewReno
260  NS_LOG_LOGIC ("Congestive loss is most likely to have occurred, "
261  "cwnd is halved");
262  return TcpNewReno::GetSsThresh (tcb, bytesInFlight);
263  }
264 }
265 
266 } // namespace ns3
virtual Ptr< TcpCongestionOps > Fork()
Copy the congestion control algorithm across socket.
Definition: tcp-veno.cc:85
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
bool IsZero(void) const
Definition: nstime.h:274
virtual uint32_t GetSsThresh(Ptr< const TcpSocketState > tcb, uint32_t bytesInFlight)
Get slow start threshold during Veno multiplicative-decrease phase.
Definition: tcp-veno.cc:241
TcpVeno(void)
Create an unbound tcp socket.
Definition: tcp-veno.cc:51
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
void DisableVeno()
Turn off Veno.
Definition: tcp-veno.cc:123
virtual void IncreaseWindow(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
Try to increase the cWnd following the NewReno specification.
The NewReno implementation.
An implementation of TCP Veno.
Definition: tcp-veno.h:69
bool m_inc
If true, cwnd needs to be incremented.
Definition: tcp-veno.h:167
uint32_t m_beta
Threshold for congestion detection.
Definition: tcp-veno.h:169
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
bool m_doingVenoNow
If true, do Veno for this RTT.
Definition: tcp-veno.h:165
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t m_diff
Difference between expected and actual throughput.
Definition: tcp-veno.h:166
virtual std::string GetName() const
Get the name of the congestion control algorithm.
Definition: tcp-veno.cc:235
int64x64_t Max(const int64x64_t &a, const int64x64_t &b)
Maximum.
Definition: int64x64.h:209
virtual uint32_t GetSsThresh(Ptr< const TcpSocketState > tcb, uint32_t bytesInFlight)
Get the slow start threshold after a loss event.
#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.
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 Veno depending on the congestion state.
Definition: tcp-veno.cc:131
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-veno.cc:37
virtual void PktsAcked(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked, const Time &rtt)
Perform RTT sampling needed to execute Veno algorithm.
Definition: tcp-veno.cc:91
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time m_baseRtt
Minimum of all RTT measurements seen during connection.
Definition: tcp-veno.h:162
uint32_t m_cntRtt
Number of RTT measurements during last RTT.
Definition: tcp-veno.h:164
virtual void IncreaseWindow(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
Adjust cwnd following Veno additive increase algorithm.
Definition: tcp-veno.cc:148
virtual void CongestionAvoidance(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
NewReno congestion avoidance.
virtual ~TcpVeno(void)
Definition: tcp-veno.cc:79
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
Time m_minRtt
Minimum of RTTs measured within last RTT.
Definition: tcp-veno.h:163
void EnableVeno()
Enable Veno algorithm to start Veno sampling.
Definition: tcp-veno.cc:114