A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ns3tcp-cwnd-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
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 
19 #include "ns3/log.h"
20 #include "ns3/abort.h"
21 #include "ns3/test.h"
22 #include "ns3/pcap-file.h"
23 #include "ns3/config.h"
24 #include "ns3/string.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/data-rate.h"
27 #include "ns3/inet-socket-address.h"
28 #include "ns3/point-to-point-helper.h"
29 #include "ns3/internet-stack-helper.h"
30 #include "ns3/ipv4-global-routing-helper.h"
31 #include "ns3/ipv4-address-helper.h"
32 #include "ns3/packet-sink-helper.h"
33 #include "ns3/tcp-socket-factory.h"
34 #include "ns3/simulator.h"
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE ("Ns3CwndTest");
39 
40 // ===========================================================================
41 // This is a simple test to demonstrate how a known good model (a reference
42 // implementation) may be used to test another model without resorting to
43 // storing stimulus or response vectors.
44 //
45 // Node zero contains the model under test, in this case the ns-3 TCP
46 // implementation. Node one contains the reference implementation that we
47 // assume will generate good test vectors for us. In this case, a Linux
48 // TCP implementation is used to stimulate the ns-3 TCP model with what we
49 // assume are perfectly good packets. We watch the ns-3 implementation to
50 // see what it does in the presence of these assumed good stimuli.
51 //
52 // The test is arranged as a typical ns-3 script, but we use the trace system
53 // to peek into the running system and monitor the ns-3 TCP.
54 //
55 // The topology is just two nodes communicating over a point-to-point network.
56 // The point-to-point network is chosen because it is simple and allows us to
57 // easily generate pcap traces we can use to separately verify that the ns-3
58 // implementation is responding correctly. Once the oopration is verified, we
59 // enter a list of responses that capture the response succinctly.
60 //
61 // node 0 node 1
62 // +----------------+ +----------------+
63 // | ns-3 TCP | | Linux TCP |
64 // +----------------+ +----------------+
65 // | 10.1.1.1 | | 10.1.1.2 |
66 // +----------------+ +----------------+
67 // | point-to-point | | point-to-point |
68 // +----------------+ +----------------+
69 // | |
70 // +---------------------+
71 // 5 Mbps, 2 ms
72 //
73 // ===========================================================================
74 //
75 class SimpleSource : public Application
76 {
77 public:
78 
79  SimpleSource ();
80  virtual ~SimpleSource();
81 
82  void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
83 
84 private:
85  virtual void StartApplication (void);
86  virtual void StopApplication (void);
87 
88  void ScheduleTx (void);
89  void SendPacket (void);
90 
93  uint32_t m_packetSize;
94  uint32_t m_nPackets;
97  bool m_running;
98  uint32_t m_packetsSent;
99 };
100 
102  : m_socket (0),
103  m_peer (),
104  m_packetSize (0),
105  m_nPackets (0),
106  m_dataRate (0),
107  m_sendEvent (),
108  m_running (false),
109  m_packetsSent (0)
110 {
111 }
112 
114 {
115  m_socket = 0;
116 }
117 
118 void
119 SimpleSource::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
120 {
121  m_socket = socket;
122  m_peer = address;
123  m_packetSize = packetSize;
124  m_nPackets = nPackets;
125  m_dataRate = dataRate;
126 }
127 
128 void
130 {
131  m_running = true;
132  m_packetsSent = 0;
133  m_socket->Bind ();
135  SendPacket ();
136 }
137 
138 void
140 {
141  m_running = false;
142 
143  if (m_sendEvent.IsRunning ())
144  {
145  Simulator::Cancel (m_sendEvent);
146  }
147 
148  if (m_socket)
149  {
150  m_socket->Close ();
151  }
152 }
153 
154 void
156 {
157  Ptr<Packet> packet = Create<Packet> (m_packetSize);
158  m_socket->Send (packet);
159 
160  if (++m_packetsSent < m_nPackets)
161  {
162  ScheduleTx ();
163  }
164 }
165 
166 void
168 {
169  if (m_running)
170  {
171  Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
172  m_sendEvent = Simulator::Schedule (tNext, &SimpleSource::SendPacket, this);
173  }
174 }
175 
177 {
178 public:
180  virtual ~Ns3TcpCwndTestCase1 ();
181 
182 private:
183  virtual void DoRun (void);
185 
186  class CwndEvent {
187 public:
188  uint32_t m_oldCwnd;
189  uint32_t m_newCwnd;
190  };
191 
193 
194  void CwndChange (uint32_t oldCwnd, uint32_t newCwnd);
195 };
196 
198  : TestCase ("Check to see that the ns-3 TCP congestion window works as expected against liblinux2.6.26.so"),
199  m_writeResults (false)
200 {
201 }
202 
204 {
205 }
206 
207 void
208 Ns3TcpCwndTestCase1::CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
209 {
210  CwndEvent event;
211 
212  event.m_oldCwnd = oldCwnd;
213  event.m_newCwnd = newCwnd;
214 
215  m_responses.Add (event);
216 }
217 
218 void
220 {
221  //
222  // Just create two nodes. One (node zero) will be the node with the TCP
223  // under test which is the ns-3 TCP implementation. The other node (node
224  // one) will be the node with the reference implementation we use to drive
225  // the tests.
226  //
227  NodeContainer nodes;
228  nodes.Create (2);
229 
230  //
231  // For this test we'll use a point-to-point net device. It's not as simple
232  // as a simple-net-device, but it provides nice places to hook trace events
233  // so we can see what's moving between our nodes.
234  //
235  PointToPointHelper pointToPoint;
236  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
237  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
238 
239  //
240  // Install the point-to-point devices on both nodes and connec them up.
241  //
242  NetDeviceContainer devices;
243  devices = pointToPoint.Install (nodes);
244 
245  //
246  // Install two variants of the internet stack. The first, on node zero
247  // uses the TCP under test, which is the default ns-3 TCP implementation.
248  //
249  InternetStackHelper stack;
250  stack.Install (nodes.Get (0));
251 
252  //
253  // The other node, node one, is going to be set up to use a Linux TCP
254  // implementation that we consider a known good TCP.
255  //
256  std::string nscStack = "liblinux2.6.26.so";
257  stack.SetTcp ("ns3::NscTcpL4Protocol", "Library", StringValue ("liblinux2.6.26.so"));
258  stack.Install (nodes.Get (1));
259 
260  //
261  // Assign the address 10.1.1.1 to the TCP implementation under test (index
262  // zero) and 10.1.1.2 to the reference implementation (index one).
263  //
264  Ipv4AddressHelper address;
265  address.SetBase ("10.1.1.0", "255.255.255.252");
266  Ipv4InterfaceContainer interfaces = address.Assign (devices);
267 
268  //
269  // We need a place to send our TCP data on the node with the reference TCP
270  // implementation. We aren't really concerned about what happens there, so
271  // just create a sink.
272  //
273  uint16_t sinkPort = 8080;
274  Address sinkAddress (InetSocketAddress (interfaces.GetAddress (1), sinkPort));
275  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
276  ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
277  sinkApps.Start (Seconds (0.));
278  sinkApps.Stop (Seconds (1.1));
279 
280  //
281  // We want to look at changes in the ns-3 TCP congestion window. The
282  // congestion window is flow clontrol imposed by the sender, so we need
283  // to crank up a flow from the ns-3 TCP node to the NSC TCP node and hook the
284  // CongestionWindow attribute on the socket. Normally one would use an on-off
285  // application to generate a flow, but this has a couple of problems. First,
286  // the socket of the on-off application is not created until Application Start
287  // time, so we wouldn't be able to hook the socket now at configuration time.
288  // Second, even if we could arrange a call after start time, the socket is not
289  // public.
290  //
291  // So, we can cook up a simple version of the on-off application that does what
292  // we want. On the plus side we don't need all of the complexity of the on-off
293  // application. On the minus side, we don't have a helper, so we have to get
294  // a little more involved in the details, but this is trivial.
295  //
296  // So first, we create a socket and do the trace connect on it; then we pass this
297  // socket into the constructor of our simple application which we then install
298  // in the node with the ns-3 TCP.
299  //
300  Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (nodes.Get (0), TcpSocketFactory::GetTypeId ());
301  ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeCallback (&Ns3TcpCwndTestCase1::CwndChange, this));
302 
303  Ptr<SimpleSource> app = CreateObject<SimpleSource> ();
304  app->Setup (ns3TcpSocket, sinkAddress, 1040, 10, DataRate ("5Mbps"));
305  nodes.Get (0)->AddApplication (app);
306  app->SetStartTime (Seconds (1.));
307  app->SetStopTime (Seconds (1.1));
308 
309  //
310  // The idea here is that someone will look very closely at the all of the
311  // communications between the reference TCP and the TCP under test in this
312  // simulation and determine that all of the responses are correct. We expect
313  // that this means generating a pcap trace file from the point-to-point link
314  // and examining the packets closely using tcpdump, wireshark or some such
315  // program. So we provide the ability to generate a pcap trace of the
316  // test execution for your perusal.
317  //
318  // Once the validation test is determined to be running exactly as exptected,
319  // the set of congestion window changes is collected and hard coded into the
320  // test results which will then be checked during the actual execution of the
321  // test.
322  //
323 
324  if (m_writeResults)
325  {
326  pointToPoint.EnablePcapAll ("tcp-cwnd");
327  }
328 
329  Simulator::Stop (Seconds (2));
330  Simulator::Run ();
331  Simulator::Destroy ();
332 
333  //
334  // As new acks are received by the TCP under test, the congestion window
335  // should be opened up by one segment (MSS bytes) each time. This should
336  // trigger a congestion window change event which we hooked and saved above.
337  // We should now be able to look through the saved response vectors and follow
338  // the congestion window as it opens up when the ns-3 TCP under test
339  // transmits its bits
340  //
341  // From inspecting the results, we know that we should see N_EVENTS congestion
342  // window change events. The window should expand N_EVENTS - 1 times (each
343  // time by MSS bytes) until it gets to its largest value. Then the application
344  // sending stops and the window should be slammed shut, with the last event
345  // reflecting the change from LARGEST_CWND back to MSS
346  //
347  const uint32_t MSS = 536;
348  const uint32_t N_EVENTS = 21;
349 
350  CwndEvent event;
351 
352  NS_TEST_ASSERT_MSG_EQ (m_responses.GetN (), N_EVENTS, "Unexpectedly low number of cwnd change events");
353 
354 
355  // Ignore the first event logged (i=0) when m_cWnd goes from 0 to MSS bytes
356  for (uint32_t i = 1, from = MSS, to = MSS * 2; i < N_EVENTS; ++i, from += MSS, to += MSS)
357  {
358  event = m_responses.Get (i);
359  NS_TEST_ASSERT_MSG_EQ (event.m_oldCwnd, from, "Wrong old cwnd value in cwnd change event " << i);
360  NS_TEST_ASSERT_MSG_EQ (event.m_newCwnd, to, "Wrong new cwnd value in cwnd change event " << i);
361  }
362 }
363 
364 
365 // ===========================================================================
366 // Test case for cwnd changes due to out-of-order packets. A bottleneck
367 // link is created, and a limited droptail queue is used in order to
368 // force dropped packets, resulting in out-of-order packet delivery.
369 // This out-of-order delivery will result in a different congestion
370 // window behavior than testcase 1. Specifically, duplicate ACKs
371 // are encountered.
372 //
373 // Network topology
374 //
375 // 1Mb/s, 10ms 100kb/s, 10ms 1Mb/s, 10ms
376 // n0--------------n1-----------------n2---------------n3
377 //
378 // ===========================================================================
380 {
381 public:
383  virtual ~Ns3TcpCwndTestCase2 ();
384 
385 private:
386  virtual void DoRun (void);
388 
389  class CwndEvent {
390 public:
391  uint32_t m_oldCwnd;
392  uint32_t m_newCwnd;
393  };
394 
396 
397  void CwndChange (uint32_t oldCwnd, uint32_t newCwnd);
398 };
399 
401  : TestCase ("Check to see that the ns-3 TCP congestion window works as expected for out-of-order packet delivery"),
402  m_writeResults (false)
403 {
404 }
405 
407 {
408 }
409 
410 void
411 Ns3TcpCwndTestCase2::CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
412 {
413  CwndEvent event;
414 
415  event.m_oldCwnd = oldCwnd;
416  event.m_newCwnd = newCwnd;
417 
418  m_responses.Add (event);
419 }
420 
421 void
423 {
424  // Set up some default values for the simulation.
425  Config::SetDefault ("ns3::DropTailQueue::MaxPackets", UintegerValue (4));
426 
427  NodeContainer n0n1;
428  n0n1.Create (2);
429 
431  n1n2.Add (n0n1.Get (1));
432  n1n2.Create (1);
433 
435  n2n3.Add (n1n2.Get (1));
436  n2n3.Create (1);
437 
438  PointToPointHelper p2p1;
439  p2p1.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (1000000)));
440  p2p1.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10)));
441  PointToPointHelper p2p2;
442  p2p2.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (100000)));
443  p2p2.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (10)));
444 
445  // And then install devices and channels connecting our topology.
446  NetDeviceContainer dev0 = p2p1.Install (n0n1);
447  NetDeviceContainer dev1 = p2p2.Install (n1n2);
448  NetDeviceContainer dev2 = p2p1.Install (n2n3);
449 
450  // Now add ip/tcp stack to all nodes.
451  InternetStackHelper internet;
452  internet.InstallAll ();
453 
454  // Later, we add IP addresses.
455  Ipv4AddressHelper ipv4;
456  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
457  ipv4.Assign (dev0);
458  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
459  ipv4.Assign (dev1);
460  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
461  Ipv4InterfaceContainer ipInterfs = ipv4.Assign (dev2);
462 
463  // and setup ip routing tables to get total ip-level connectivity.
464  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
465 
466  // Set up the apps
467  uint16_t servPort = 50000;
468 
469  // Create a packet sink to receive these packets on n3
470  PacketSinkHelper sink ("ns3::TcpSocketFactory",
471  InetSocketAddress (Ipv4Address::GetAny (), servPort));
472 
473  ApplicationContainer apps = sink.Install (n2n3.Get (1));
474  apps.Start (Seconds (0.0));
475  apps.Stop (Seconds (5.4));
476 
477  // Create the socket for n0
478  Address sinkAddress (InetSocketAddress (ipInterfs.GetAddress (1), servPort));
479  Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (n0n1.Get (0), TcpSocketFactory::GetTypeId ());
480  ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeCallback (&Ns3TcpCwndTestCase2::CwndChange, this));
481 
482  // Create and start the app for n0
483  Ptr<SimpleSource> app = CreateObject<SimpleSource> ();
484  app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000, DataRate ("1Mbps"));
485  n0n1.Get (0)->AddApplication (app);
486  app->SetStartTime (Seconds (1.0));
487  app->SetStopTime (Seconds (5.4));
488 
489  if (m_writeResults)
490  {
491  // Write a pcap for tcp cwnd testcase with out-of-order delivery
492  PointToPointHelper pointToPoint;
493  pointToPoint.EnablePcapAll ("tcp-cwnd-ood");
494  }
495 
496  // Finally, set up the simulator to run.
497  Simulator::Stop (Seconds (5.4));
498  Simulator::Run ();
499  Simulator::Destroy ();
500 
501  //
502  // As new acks are received by the TCP under test, the congestion window
503  // should be opened up by one segment (MSS bytes) each time. This should
504  // trigger a congestion window change event which we hooked and saved above.
505  // We should now be able to look through the saved response vectors and follow
506  // the congestion window as it opens up when the ns-3 TCP under test
507  // transmits its bits
508  //
509  // From inspecting the results, we know that we should see N_EVENTS congestion
510  // window change events. On the tenth change event, the window should
511  // be cut from 5360 to 4288 due to 3 dup acks (NewReno behavior is to
512  // cut in half, and then add 3 segments (5360/2 + 3*536 = 4288)
513  // It should then increment cwnd by one segment per ack throughout
514  // the fast recovery phase. The trace shows that three segments are lost
515  // within the fast recovery window (with sequence numbers starting at
516  // 9113, 10721, and 12329). This last segment (12329) is not recovered
517  // by a fast retransmit and consequently, a coarse timeout is taken and
518  // cwnd is reset to MSS at event index 32. It slow starts again, and takes
519  // another fast retransmit at index 42.
520  //
521  const uint32_t MSS = 536;
522  const uint32_t N_EVENTS = 44;
523 
524  CwndEvent event;
525 
526  NS_TEST_ASSERT_MSG_EQ (m_responses.GetN (), N_EVENTS, "Unexpected number of cwnd change events");
527 
528  // Ignore the first event logged (i=0) when m_cWnd goes from 0 to MSS bytes
529  for (uint32_t i = 1, from = MSS, to = MSS * 2; i < 10; ++i, from += MSS, to += MSS)
530  {
531  event = m_responses.Get (i);
532  NS_TEST_ASSERT_MSG_EQ (event.m_oldCwnd, from, "Wrong old cwnd value in cwnd change event " << i);
533  NS_TEST_ASSERT_MSG_EQ (event.m_newCwnd, to, "Wrong new cwnd value in cwnd change event " << i);
534  }
535 
536  // Cwnd should be back to (10/2 + 3) = 8*MSS
537  event = m_responses.Get (10);
538  NS_TEST_ASSERT_MSG_EQ (event.m_newCwnd, 8*MSS, "Wrong new cwnd value in cwnd change event " << 10);
539 
540  // Fast recovery
541  for (uint32_t i = 11, from = 8*MSS, to = 9 * MSS; i < 32; ++i, from += MSS, to += MSS)
542  {
543  event = m_responses.Get (i);
544  NS_TEST_ASSERT_MSG_EQ (event.m_oldCwnd, from, "Wrong old cwnd value in cwnd change event " << i);
545  NS_TEST_ASSERT_MSG_EQ (event.m_newCwnd, to, "Wrong new cwnd value in cwnd change event " << i);
546  }
547 
548  // Slow start again after coarse timeout
549  for (uint32_t i = 33, from = MSS, to = MSS * 2; i < 42; ++i, from += MSS, to += MSS)
550  {
551  event = m_responses.Get (i);
552  NS_TEST_ASSERT_MSG_EQ (event.m_oldCwnd, from, "Wrong old cwnd value in cwnd change event " << i);
553  NS_TEST_ASSERT_MSG_EQ (event.m_newCwnd, to, "Wrong new cwnd value in cwnd change event " << i);
554  }
555 
556  // Fast retransmit again; cwnd should be back to 8*MSS
557  event = m_responses.Get (42);
558  NS_TEST_ASSERT_MSG_EQ (event.m_newCwnd, 8*MSS, "Wrong new cwnd value in cwnd change event " << 42);
559 }
560 
562 {
563 public:
565 };
566 
568  : TestSuite ("ns3-tcp-cwnd", SYSTEM)
569 {
571 // AddTestCase (new Ns3TcpCwndTestCase2);
572 }
573