A Discrete-Event Network Simulator
API
tcp-veno-test.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 "ns3/test.h"
28 #include "ns3/log.h"
29 #include "ns3/tcp-congestion-ops.h"
30 #include "ns3/tcp-socket-base.h"
31 #include "ns3/tcp-veno.h"
32 
33 namespace ns3 {
34 
35 NS_LOG_COMPONENT_DEFINE ("TcpVenoTestSuite");
36 
37 static uint32_t
38 NewReno_SlowStart (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
39 {
40  if (segmentsAcked >= 1)
41  {
42  tcb->m_cWnd += tcb->m_segmentSize;
43  return segmentsAcked - 1;
44  }
45 
46  return 0;
47 }
48 
49 static void
50 NewReno_CongestionAvoidance (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
51 {
52  if (segmentsAcked > 0)
53  {
54  double adder = static_cast<double> (tcb->m_segmentSize * tcb->m_segmentSize) / tcb->m_cWnd.Get ();
55  adder = std::max (1.0, adder);
56  tcb->m_cWnd += static_cast<uint32_t> (adder);
57  }
58 }
59 
60 static void
61 NewReno_IncreaseWindow (Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
62 {
63  if (tcb->m_cWnd < tcb->m_ssThresh)
64  {
65  segmentsAcked = NewReno_SlowStart (tcb, segmentsAcked);
66  }
67 
68  if (tcb->m_cWnd >= tcb->m_ssThresh)
69  {
70  NewReno_CongestionAvoidance (tcb, segmentsAcked);
71  }
72 }
73 
77 class TcpVenoTest : public TestCase
78 {
79 public:
80  TcpVenoTest (uint32_t cWnd,
81  uint32_t segmentSize,
82  uint32_t ssThresh,
83  Time rtt,
84  uint32_t segmentsAcked,
85  uint32_t numRtt,
86  const std::string &name);
87 
88 private:
89  virtual void DoRun (void);
90  void AdditiveIncrease (Ptr<TcpSocketState> state, uint32_t diff, UintegerValue beta);
91  uint32_t MultiplicativeDecrease (uint32_t diff, const UintegerValue &beta, uint32_t bytesInFlight);
92 
93  uint32_t m_cWnd;
94  uint32_t m_segmentSize;
95  uint32_t m_ssThresh;
97  uint32_t m_segmentsAcked;
98  uint32_t m_numRtt;
99  bool m_inc;
101 };
102 
104  uint32_t segmentSize,
105  uint32_t ssThresh,
106  Time rtt,
107  uint32_t segmentsAcked,
108  uint32_t numRtt,
109  const std::string &name)
110  : TestCase (name),
111  m_cWnd (cWnd),
112  m_segmentSize (segmentSize),
113  m_ssThresh (ssThresh),
114  m_rtt (rtt),
115  m_segmentsAcked (segmentsAcked),
116  m_numRtt (numRtt),
117  m_inc (true)
118 {
119 }
120 
121 void
123 {
124  m_state = CreateObject<TcpSocketState> ();
125 
126  m_state->m_cWnd = m_cWnd;
127  m_state->m_segmentSize = m_segmentSize;
128  m_state->m_ssThresh = m_ssThresh;
129 
130  Ptr<TcpVeno> cong = CreateObject <TcpVeno> ();
131 
132  // Set baseRtt to 100 ms
133  Time baseRtt = MilliSeconds (100);
134  cong->PktsAcked (m_state, m_segmentsAcked, baseRtt);
135 
136  // Re-set Veno to assign a new value of minRtt
137  cong->CongestionStateSet (m_state, TcpSocketState::CA_OPEN);
138 
139  uint32_t segCwnd = m_cWnd / m_segmentSize;
140 
141  // Calculate expected throughput
142  uint32_t expectedCwnd;
143  double tmp = baseRtt.GetSeconds () / m_rtt.GetSeconds ();
144  expectedCwnd = segCwnd * tmp;
145 
146  // Calculate the difference between actual and expected throughput
147  uint32_t diff;
148  diff = segCwnd - expectedCwnd;
149 
150  // Get the beta attribute
151  UintegerValue beta;
152  cong->GetAttribute ("Beta", beta);
153 
154  uint32_t cntRtt = 0;
155 
156  TcpSocketState state;
157  state.m_cWnd = m_cWnd;
158  state.m_ssThresh = m_ssThresh;
160 
161  while (m_numRtt != 0)
162  {
163  // Update cwnd using Veno's additive increase algorithm
164  cong->PktsAcked (m_state, m_segmentsAcked, m_rtt);
165  cong->IncreaseWindow (m_state, m_segmentsAcked);
166 
167  // The first round the internal m_diff of cong will be 4, just like us
168  if (cntRtt == 0)
169  {
170  // Update ssthresh using Veno's multiplicative decrease algorithm
171  uint32_t ssThresh = cong->GetSsThresh (m_state, m_state->m_cWnd);
172 
173  // Our calculation of ssthresh
174  uint32_t calculatedSsThresh = MultiplicativeDecrease (diff, beta, m_state->m_cWnd.Get ());
175 
176  NS_TEST_ASSERT_MSG_EQ (ssThresh, calculatedSsThresh,
177  "Veno has not decremented cWnd correctly based on its"
178  "multiplicative decrease algo.");
179  }
180 
181  // Our calculation of cwnd
182  if (cntRtt <= 2)
183  {
184  NewReno_IncreaseWindow (&state, 1);
185  }
186  else
187  {
188  AdditiveIncrease (&state, diff, beta);
189  }
190 
191  NS_TEST_ASSERT_MSG_EQ (m_state->m_cWnd.Get (), state.m_cWnd.Get (),
192  "CWnd has not updated correctly based on Veno linear increase algorithm");
193  m_numRtt--;
194  cntRtt++;
195  }
196 }
197 
198 void
200 {
201  if (m_cWnd < m_ssThresh)
202  { // Slow start
203  NewReno_SlowStart (state, 1);
204  }
205  else
206  { // Congestion avoidance
207  if (diff < beta.Get ())
208  { // Increase cwnd by 1 every RTT when bandwidth is not fully utilized
209  NewReno_CongestionAvoidance (state, 1);
210  }
211  else
212  { // Increase cwnd by 1 every other RTT when bandwidth is fully utilized
213  if (m_inc)
214  {
215  NewReno_CongestionAvoidance (state, 1);
216  m_inc = false;
217  }
218  else
219  {
220  m_inc = true;
221  }
222  }
223  }
224 }
225 
226 uint32_t
228  uint32_t bytesInFlight)
229 {
230  uint32_t calculatedSsThresh;
231  if (diff < beta.Get ())
232  {
233  static double tmp = 4.0 / 5.0;
234  calculatedSsThresh = std::max (2 * m_segmentSize, static_cast<uint32_t> (bytesInFlight * tmp));
235  }
236  else
237  {
238  calculatedSsThresh = std::max (2 * m_segmentSize, bytesInFlight / 2);
239  }
240  return calculatedSsThresh;
241 }
242 
243 
244 // -------------------------------------------------------------------
245 static class TcpVenoTestSuite : public TestSuite
246 {
247 public:
248  TcpVenoTestSuite () : TestSuite ("tcp-veno-test", UNIT)
249  {
250  AddTestCase (new TcpVenoTest (38 * 1446, 1446, 40 * 1446, MilliSeconds (100), 1, 1,
251  "Veno test on cWnd in slow start and non-congestive loss"),
253  AddTestCase (new TcpVenoTest (30 * 536, 536, 20 * 536, MilliSeconds (106), 1, 1,
254  "Veno test on cWnd with diff < beta"),
256  AddTestCase (new TcpVenoTest (60 * 536, 536, 40 * 536, MilliSeconds (106), 1, 3,
257  "Veno increment test on cWnd with diff > beta"),
259  }
260 } g_tcpVenoTest;
261 
262 } // namespace ns3
uint32_t MultiplicativeDecrease(uint32_t diff, const UintegerValue &beta, uint32_t bytesInFlight)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
Normal state, no dubious events.
Fast test.
Definition: test.h:1152
A suite of tests to run.
Definition: test.h:1333
static void NewReno_IncreaseWindow(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
static void NewReno_CongestionAvoidance(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
uint32_t m_segmentSize
Segment size.
uint32_t m_segmentSize
Data structure that records the congestion state of a connection.
encapsulates test code
Definition: test.h:1147
This test suite implements a Unit Test.
Definition: test.h:1343
T Get(void) const
Get the underlying value.
Definition: traced-value.h:218
Ptr< TcpSocketState > m_state
uint64_t Get(void) const
Definition: uinteger.cc:35
Testing the additive increase and multiplicative decrease of TcpVeno.
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
void AddTestCase(TestCase *testCase, enum TestDuration duration)
Add an individual child TestCase to this test suite.
Definition: test.cc:298
Hold an unsigned integer type.
Definition: uinteger.h:44
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
Definition: test.h:161
TracedValue< uint32_t > m_ssThresh
Slow start threshold.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t m_segmentsAcked
void AdditiveIncrease(Ptr< TcpSocketState > state, uint32_t diff, UintegerValue beta)
static uint32_t NewReno_SlowStart(Ptr< TcpSocketState > tcb, uint32_t segmentsAcked)
TcpVenoTest(uint32_t cWnd, uint32_t segmentSize, uint32_t ssThresh, Time rtt, uint32_t segmentsAcked, uint32_t numRtt, const std::string &name)
TracedValue< uint32_t > m_cWnd
Congestion window.
virtual void DoRun(void)
Implementation to actually run this TestCase.
ns3::TcpVenoTestSuite g_tcpVenoTest