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-vegas.h"
32 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE ("TcpVegasTestSuite");
36 
37 /**
38  * \brief TcpVegas congestion control algorithm test
39  */
40 class TcpVegasTest : public TestCase
41 {
42 public:
43   /**
44    * \brief Constructor.
45    * \param cWnd Congestion window.
46    * \param segmentSize Segment size.
47    * \param ssThresh Slow Start Threshold.
48    * \param rtt The RTT.
49    * \param segmentsAcked Number of segments ACKed.
50    * \param nextTxSeq Next Tx sequence number.
51    * \param lastAckedSeq Last ACKed sequence number.
52    * \param name Test description.
53    */
54   TcpVegasTest (uint32_t cWnd,
55                 uint32_t segmentSize,
56                 uint32_t ssThresh,
57                 Time rtt,
58                 uint32_t segmentsAcked,
59                 SequenceNumber32 nextTxSeq,
60                 SequenceNumber32 lastAckedSeq,
61                 const std::string &name);
62 
63 private:
64   virtual void DoRun (void);
65   /**
66    * \brief Increases the TCP window.
67    * \param cong The congestion control.
68    */
69   void IncreaseWindow (Ptr<TcpVegas> cong);
70   /**
71    * brief Get and check the SSH threshold.
72    * \param cong The congestion control.
73    */
74   void GetSsThresh (Ptr<TcpVegas> cong);
75 
76   uint32_t m_cWnd;        //!< Congestion window.
77   uint32_t m_segmentSize; //!< Segment size.
78   uint32_t m_ssThresh;    //!< Slow Start Threshold.
79   Time m_rtt;             //!< RTT.
80   uint32_t m_segmentsAcked; //!< Number of segments ACKed.
81   SequenceNumber32 m_nextTxSeq; //!< Next Tx sequence number.
82   SequenceNumber32 m_lastAckedSeq;  //!< Last ACKed sequence number.
83 
84   Ptr<TcpSocketState> m_state;  //!< TCP socket state.
85 };
86 
TcpVegasTest(uint32_t cWnd,uint32_t segmentSize,uint32_t ssThresh,Time rtt,uint32_t segmentsAcked,SequenceNumber32 nextTxSeq,SequenceNumber32 lastAckedSeq,const std::string & name)87 TcpVegasTest::TcpVegasTest (uint32_t cWnd,
88                             uint32_t segmentSize,
89                             uint32_t ssThresh,
90                             Time rtt,
91                             uint32_t segmentsAcked,
92                             SequenceNumber32 nextTxSeq,
93                             SequenceNumber32 lastAckedSeq,
94                             const std::string &name)
95   : TestCase (name),
96     m_cWnd (cWnd),
97     m_segmentSize (segmentSize),
98     m_ssThresh (ssThresh),
99     m_rtt (rtt),
100     m_segmentsAcked (segmentsAcked),
101     m_nextTxSeq (nextTxSeq),
102     m_lastAckedSeq (lastAckedSeq)
103 {
104 }
105 
106 void
DoRun()107 TcpVegasTest::DoRun ()
108 {
109   m_state = CreateObject<TcpSocketState> ();
110 
111   m_state->m_cWnd = m_cWnd;
112   m_state->m_segmentSize = m_segmentSize;
113   m_state->m_ssThresh = m_ssThresh;
114   m_state->m_nextTxSequence = m_nextTxSeq;
115   m_state->m_lastAckedSeq = m_lastAckedSeq;
116   m_state->m_minRtt = m_rtt;
117 
118   Ptr<TcpVegas> cong = CreateObject <TcpVegas> ();
119 
120   // Set baseRtt to 100 ms
121   cong->PktsAcked (m_state, m_segmentsAcked, MilliSeconds (100));
122 
123   // Re-set Vegas to assign a new value of minRtt
124   cong->CongestionStateSet (m_state, TcpSocketState::CA_OPEN);
125   cong->PktsAcked (m_state, m_segmentsAcked, m_rtt);
126 
127   // 2 more calls to PktsAcked to increment cntRtt beyond 2
128   cong->PktsAcked (m_state, m_segmentsAcked, m_rtt);
129   cong->PktsAcked (m_state, m_segmentsAcked, m_rtt);
130 
131   // Update cwnd using Vegas algorithm
132   cong->IncreaseWindow (m_state, m_segmentsAcked);
133 
134   // Our calculation of cwnd
135   IncreaseWindow (cong);
136 
137   NS_TEST_ASSERT_MSG_EQ (m_state->m_cWnd.Get (), m_cWnd,
138                          "CWnd has not updated correctly");
139   NS_TEST_ASSERT_MSG_EQ (m_state->m_ssThresh.Get (), m_ssThresh,
140                          "SsThresh has not updated correctly");
141 }
142 
143 void
IncreaseWindow(Ptr<TcpVegas> cong)144 TcpVegasTest::IncreaseWindow (Ptr<TcpVegas> cong)
145 {
146   Time baseRtt = MilliSeconds (100);
147   uint32_t segCwnd = m_cWnd / m_segmentSize;
148 
149   // Calculate expected throughput
150   uint64_t expectedCwnd;
151   expectedCwnd = (uint64_t) segCwnd * (double) baseRtt.GetMilliSeconds () / (double) m_rtt.GetMilliSeconds ();
152 
153   // Calculate the difference between actual and expected throughput
154   uint32_t diff;
155   diff = segCwnd - expectedCwnd;
156 
157   // Get the alpha,beta, and gamma attributes
158   UintegerValue alpha, beta, gamma;
159   cong->GetAttribute ("Alpha", alpha);
160   cong->GetAttribute ("Beta", beta);
161   cong->GetAttribute ("Gamma", gamma);
162 
163   if (diff > gamma.Get () && (m_cWnd < m_ssThresh))
164     { // Change from slow-start to linear increase/decrease mode
165       segCwnd = std::min (segCwnd, (uint32_t) expectedCwnd + 1);
166       m_cWnd = segCwnd * m_segmentSize;
167       GetSsThresh (cong);
168     }
169   else if (m_cWnd < m_ssThresh)
170     { // Execute Reno slow start
171       if (m_segmentsAcked >= 1)
172         {
173           m_cWnd += m_segmentSize;
174           m_segmentsAcked--;
175         }
176     }
177   else
178     { // Linear increase/decrease mode
179       if (diff > beta.Get ())
180         {
181           m_cWnd = (segCwnd - 1) * m_segmentSize;
182           GetSsThresh (cong);
183         }
184       else if (diff < alpha.Get ())
185         {
186           m_cWnd = (segCwnd + 1) * m_segmentSize;
187         }
188       else
189         {
190         }
191     }
192   m_ssThresh = std::max (m_ssThresh, 3 * m_cWnd / 4);
193 }
194 
195 void
GetSsThresh(Ptr<TcpVegas> cong)196 TcpVegasTest::GetSsThresh (Ptr<TcpVegas> cong)
197 {
198   m_ssThresh = std::max (std::min (m_ssThresh, m_cWnd - m_segmentSize), 2 * m_segmentSize);
199 }
200 
201 
202 /**
203  * \ingroup internet-test
204  * \ingroup tests
205  *
206  * \brief TCP Vegas TestSuite
207  */
208 class TcpVegasTestSuite : public TestSuite
209 {
210 public:
TcpVegasTestSuite()211   TcpVegasTestSuite () : TestSuite ("tcp-vegas-test", UNIT)
212   {
213     AddTestCase (new TcpVegasTest (38 * 1446, 1446, 40 * 1446, MilliSeconds (106), 1, SequenceNumber32 (2893), SequenceNumber32 (5785),
214                                    "Vegas test on cWnd and ssThresh when in slow start and diff > gamma"),
215                  TestCase::QUICK);
216     AddTestCase (new TcpVegasTest (5 * 536, 536, 10 * 536, MilliSeconds (118), 1, SequenceNumber32 (3216), SequenceNumber32 (3753),
217                                    "Vegas test on cWnd and ssThresh when in slow start and diff < gamma"),
218                  TestCase::QUICK);
219     AddTestCase (new TcpVegasTest (60 * 346, 346, 40 * 346, MilliSeconds (206), 1, SequenceNumber32 (20761), SequenceNumber32 (21107),
220                                    "Vegas test on cWnd and ssThresh when diff > beta"),
221                  TestCase::QUICK);
222     AddTestCase (new TcpVegasTest (15 * 1446, 1446, 10 * 1446, MilliSeconds (106), 1, SequenceNumber32 (21691), SequenceNumber32 (24583),
223                                    "Vegas test on cWnd and ssThresh when diff < alpha"),
224                  TestCase::QUICK);
225     AddTestCase (new TcpVegasTest (20 * 746, 746, 10 * 746, MilliSeconds (109), 1, SequenceNumber32 (14921), SequenceNumber32 (15667),
226                                    "Vegas test on cWnd and ssThresh when alpha <= diff <= beta"),
227                  TestCase::QUICK);
228   }
229 };
230 
231 static TcpVegasTestSuite g_tcpVegasTest; //!< Static variable for test initialization
232