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