A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ns3tcp-loss-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) 2010 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 <iomanip>
20 
21 #include "ns3/log.h"
22 #include "ns3/abort.h"
23 #include "ns3/test.h"
24 #include "ns3/pcap-file.h"
25 #include "ns3/config.h"
26 #include "ns3/string.h"
27 #include "ns3/uinteger.h"
28 #include "ns3/data-rate.h"
29 #include "ns3/inet-socket-address.h"
30 #include "ns3/point-to-point-helper.h"
31 #include "ns3/internet-stack-helper.h"
32 #include "ns3/ipv4-global-routing-helper.h"
33 #include "ns3/ipv4-address-helper.h"
34 #include "ns3/packet-sink-helper.h"
35 #include "ns3/tcp-socket-factory.h"
36 #include "ns3/node-container.h"
37 #include "ns3/simulator.h"
38 #include "ns3/error-model.h"
39 #include "ns3/pointer.h"
40 #include "ns3tcp-socket-writer.h"
41 #include "ns3/tcp-westwood.h"
42 
43 using namespace ns3;
44 
45 NS_LOG_COMPONENT_DEFINE ("Ns3TcpLossTest");
46 
47 const bool WRITE_VECTORS = false; // set to true to write response vectors
48 const bool WRITE_LOGGING = false; // set to true to write logging
49 const uint32_t PCAP_LINK_TYPE = 1187373557; // Some large random number -- we use to verify data was written by this program
50 const uint32_t PCAP_SNAPLEN = 64; // Don't bother to save much data
51 
52 // ===========================================================================
53 // Tests of TCP implementation loss behavior
54 // ===========================================================================
55 //
56 
58 {
59 public:
61  Ns3TcpLossTestCase (std::string tcpModel, uint32_t testCase);
62  virtual ~Ns3TcpLossTestCase () {}
63 
64 private:
65  virtual void DoSetup (void);
66  virtual void DoRun (void);
67  virtual void DoTeardown (void);
68 
70  std::string m_pcapFilename;
72  uint32_t m_testCase;
73  uint32_t m_totalTxBytes;
74  uint32_t m_currentTxBytes;
79  std::string m_tcpModel;
80 
81  void Ipv4L3Tx (std::string context, Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface);
82  void CwndTracer (uint32_t oldval, uint32_t newval);
83  void WriteUntilBufferFull (Ptr<Socket> localSocket, uint32_t txSpace);
84  void StartFlow (Ptr<Socket> localSocket,
85  Ipv4Address servAddress,
86  uint16_t servPort);
87 
88 };
89 
91  : TestCase ("Check the operation of the TCP state machine for several cases"),
92  m_testCase (0),
93  m_totalTxBytes (200000),
94  m_currentTxBytes (0),
95  m_writeVectors (WRITE_VECTORS),
96  m_writeResults (false),
97  m_writeLogging (WRITE_LOGGING),
98  m_needToClose (true),
99  m_tcpModel ("ns3::TcpWestwood")
100 {
101 }
102 
103 Ns3TcpLossTestCase::Ns3TcpLossTestCase (std::string tcpModel, uint32_t testCase)
104  : TestCase ("Check the behaviour of TCP upon packet losses"),
105  m_testCase (testCase),
106  m_totalTxBytes (200000),
107  m_currentTxBytes (0),
108  m_writeVectors (WRITE_VECTORS),
109  m_writeResults (false),
110  m_writeLogging (WRITE_LOGGING),
111  m_needToClose (true),
112  m_tcpModel (tcpModel)
113 {
114 }
115 
116 void
118 {
119  //
120  // We expect there to be a file called ns3tcp-state-response-vectors.pcap in
121  // response-vectors/ of this directory
122  //
123  std::ostringstream oss;
124  oss << "/response-vectors/ns3tcp-loss-" << m_tcpModel << m_testCase << "-response-vectors.pcap";
126 
127  if (m_writeVectors)
128  {
129  m_pcapFile.Open (m_pcapFilename, std::ios::out|std::ios::binary);
131  }
132  else
133  {
134  m_pcapFile.Open (m_pcapFilename, std::ios::in|std::ios::binary);
135  NS_ABORT_MSG_UNLESS (m_pcapFile.GetDataLinkType () == PCAP_LINK_TYPE, "Wrong response vectors in directory");
136  }
137 }
138 
139 void
141 {
142  m_pcapFile.Close ();
143 }
144 
145 void
146 Ns3TcpLossTestCase::Ipv4L3Tx (std::string context, Ptr<const Packet> packet, Ptr<Ipv4> ipv4, uint32_t interface)
147 {
148  //
149  // We're not testing IP so remove and toss the header. In order to do this,
150  // though, we need to copy the packet since we have a const version.
151  //
152  Ptr<Packet> p = packet->Copy ();
153  Ipv4Header ipHeader;
154  p->RemoveHeader (ipHeader);
155 
156  //
157  // What is left is the TCP header and any data that may be sent. We aren't
158  // sending any TCP data, so we expect what remains is only TCP header, which
159  // is a small thing to save.
160  //
161  if (m_writeVectors)
162  {
163  //
164  // Save the TCP under test response for later testing.
165  //
166  Time tNow = Simulator::Now ();
167  int64_t tMicroSeconds = tNow.GetMicroSeconds ();
168 
169  uint32_t size = p->GetSize ();
170  uint8_t *buf = new uint8_t[size];
171  p->CopyData (buf, size);
172 
173  m_pcapFile.Write (uint32_t (tMicroSeconds / 1000000),
174  uint32_t (tMicroSeconds % 1000000),
175  buf,
176  size);
177  delete [] buf;
178  }
179  else
180  {
181  //
182  // Read the TCP under test expected response from the expected vector
183  // file and see if it still does the right thing.
184  //
185  uint8_t expected[PCAP_SNAPLEN];
186  uint32_t tsSec, tsUsec, inclLen, origLen, readLen;
187  m_pcapFile.Read (expected, sizeof(expected), tsSec, tsUsec, inclLen, origLen, readLen);
188 
189  NS_LOG_DEBUG ("read " << readLen);
190 
191  uint8_t *actual = new uint8_t[readLen];
192  p->CopyData (actual, readLen);
193 
194  uint32_t result = memcmp (actual, expected, readLen);
195 
196  delete [] actual;
197 
198  //
199  // Avoid streams of errors -- only report the first.
200  //
201  if (IsStatusSuccess ())
202  {
203  NS_TEST_EXPECT_MSG_EQ (result, 0, "Expected data comparison error");
204  }
205  }
206 }
207 
208 void
209 Ns3TcpLossTestCase::CwndTracer (uint32_t oldval, uint32_t newval)
210 {
211  if (m_writeLogging)
212  {
213  *(m_osw->GetStream ()) << "Moving cwnd from " << oldval << " to " << newval
214  << " at time " << Simulator::Now ().GetSeconds ()
215  << " seconds" << std::endl;
216  }
217 }
218 
220 // Implementing an "application" to send bytes over a TCP connection
221 void
223 {
225  {
226  uint32_t left = m_totalTxBytes - m_currentTxBytes;
227  uint32_t dataOffset = m_currentTxBytes % 1040;
228  uint32_t toWrite = 1040 - dataOffset;
229  uint32_t txAvail = localSocket->GetTxAvailable ();
230  toWrite = std::min (toWrite, left);
231  toWrite = std::min (toWrite, txAvail);
232  if (txAvail == 0)
233  {
234  return;
235  };
236  if (m_writeLogging)
237  {
238  std::clog << "Submitting " << toWrite
239  << " bytes to TCP socket" << std::endl;
240  }
241  int amountSent = localSocket->Send (0, toWrite, 0);
242  NS_ASSERT (amountSent > 0); // Given GetTxAvailable() non-zero, amountSent should not be zero
243  m_currentTxBytes += amountSent;
244  }
245  if (m_needToClose)
246  {
247  if (m_writeLogging)
248  {
249  std::clog << "Close socket at "
250  << Simulator::Now ().GetSeconds () << std::endl;
251  }
252  localSocket->Close ();
253  m_needToClose = false;
254  }
255 }
256 
257 void
259  Ipv4Address servAddress,
260  uint16_t servPort)
261 {
262  if (m_writeLogging)
263  {
264  std::clog << "Starting flow at time "
265  << Simulator::Now ().GetSeconds () << std::endl;
266  }
267  localSocket->Connect (InetSocketAddress (servAddress, servPort)); // connect
268 
269  // tell the tcp implementation to call WriteUntilBufferFull again
270  // if we blocked and new tx buffer space becomes available
271  localSocket->SetSendCallback (MakeCallback
273  this));
274  WriteUntilBufferFull (localSocket, localSocket->GetTxAvailable ());
275 }
276 
277 void
279 {
280  // Network topology
281  //
282  // 8Mb/s, 0.1ms 0.8Mb/s, 100ms
283  // s1-----------------r1-----------------k1
284  //
285  // Example corresponding to simulations in the paper "Simulation-based
286  // Comparisons of Tahoe, Reno, and SACK TCP
287 
288  std::ostringstream tcpModel;
289  tcpModel << "ns3::Tcp" << m_tcpModel;
290  if (m_tcpModel.compare("WestwoodPlus") == 0)
291  {
292  Config::SetDefault("ns3::TcpL4Protocol::SocketType",
293  TypeIdValue (TcpWestwood::GetTypeId()));
294  Config::SetDefault("ns3::TcpWestwood::ProtocolType",
295  EnumValue(TcpWestwood::WESTWOODPLUS));
296  }
297  else
298  {
299  Config::SetDefault ("ns3::TcpL4Protocol::SocketType",
300  StringValue (tcpModel.str ()));
301  }
302 
303  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (1000));
304  Config::SetDefault ("ns3::TcpSocket::DelAckCount", UintegerValue (1));
305 
306  if (m_writeLogging)
307  {
309  LogComponentEnable ("TcpLossResponse", LOG_LEVEL_ALL);
310  LogComponentEnable ("ErrorModel", LOG_LEVEL_DEBUG);
311  LogComponentEnable ("TcpLossResponse", LOG_LEVEL_ALL);
312  LogComponentEnable ("TcpWestwood", LOG_LEVEL_ALL);
313  LogComponentEnable ("TcpNewReno", LOG_LEVEL_INFO);
314  LogComponentEnable ("TcpReno", LOG_LEVEL_INFO);
315  LogComponentEnable ("TcpTahoe", LOG_LEVEL_INFO);
316  LogComponentEnable ("TcpSocketBase", LOG_LEVEL_INFO);
317  }
318 
320  // Topology construction
321  //
322 
323  // Create three nodes: s1, r1, and k1
324  NodeContainer s1r1;
325  s1r1.Create (2);
326 
327  NodeContainer r1k1;
328  r1k1.Add (s1r1.Get (1));
329  r1k1.Create (1);
330 
331  // Set up TCP/IP stack to all nodes (and create loopback device at device 0)
332  InternetStackHelper internet;
333  internet.InstallAll ();
334 
335  // Connect the nodes
336  PointToPointHelper p2p;
337  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (8000000)));
338  p2p.SetChannelAttribute ("Delay", TimeValue (Seconds (0.0001)));
339  NetDeviceContainer dev0 = p2p.Install (s1r1);
340  p2p.SetDeviceAttribute ("DataRate", DataRateValue (DataRate (800000)));
341  p2p.SetChannelAttribute ("Delay", TimeValue (Seconds (0.1)));
342  NetDeviceContainer dev1 = p2p.Install (r1k1);
343 
344  // Add IP addresses to each network interfaces
345  Ipv4AddressHelper ipv4;
346  ipv4.SetBase ("10.1.3.0", "255.255.255.0");
347  ipv4.Assign (dev0);
348  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
349  Ipv4InterfaceContainer ipInterfs = ipv4.Assign (dev1);
350 
351  // Set up routes to all nodes
352  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
353 
355  // Send 20000 (totalTxBytes) bytes from node s1 to node k1
356  //
357 
358  // Create a packet sink to receive packets on node k1
359  uint16_t servPort = 50000; // Destination port number
360  PacketSinkHelper sink ("ns3::TcpSocketFactory",
361  InetSocketAddress (Ipv4Address::GetAny (), servPort));
362  ApplicationContainer apps = sink.Install (r1k1.Get (1));
363  apps.Start (Seconds (0.0));
364  apps.Stop (Seconds (100.0));
365 
366  // Create a data source to send packets on node s0.
367  // Instead of full application, here use the socket directly by
368  // registering callbacks in function StarFlow().
369  Ptr<Socket> localSocket = Socket::CreateSocket (s1r1.Get (0), TcpSocketFactory::GetTypeId ());
370  localSocket->Bind ();
371  Simulator::ScheduleNow (&Ns3TcpLossTestCase::StartFlow,
372  this,
373  localSocket,
374  ipInterfs.GetAddress (1),
375  servPort);
376 
377  Config::Connect ("/NodeList/0/$ns3::Ipv4L3Protocol/Tx",
379 
381  ("/NodeList/0/$ns3::TcpL4Protocol/SocketList/0/CongestionWindow",
383 
385  // Set up loss model at node k1
386  //
387  std::list<uint32_t> sampleList;
388  switch (m_testCase)
389  {
390  case 0:
391  break;
392  case 1:
393  // Force a loss for 15th data packet. TCP cwnd will be at 14 segments
394  // (14000 bytes) when duplicate acknowledgments start to come.
395  sampleList.push_back (16);
396  break;
397  case 2:
398  sampleList.push_back (16);
399  sampleList.push_back (17);
400  break;
401  case 3:
402  sampleList.push_back (16);
403  sampleList.push_back (17);
404  sampleList.push_back (18);
405  break;
406  case 4:
407  sampleList.push_back (16);
408  sampleList.push_back (17);
409  sampleList.push_back (18);
410  sampleList.push_back (19);
411  break;
412  default:
413  NS_FATAL_ERROR ("Program fatal error: loss value " << m_testCase << " not supported.");
414  break;
415  }
416 
417  Ptr<ReceiveListErrorModel> pem = CreateObject<ReceiveListErrorModel> ();
418  pem->SetList (sampleList);
419  dev1.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (pem));
420 
421  // One can toggle the comment for the following line on or off to see the
422  // effects of finite send buffer modelling. One can also change the size of
423  // that buffer.
424  // localSocket->SetAttribute("SndBufSize", UintegerValue(4096));
425 
426  std::ostringstream oss;
427  oss << "tcp-loss-" << m_tcpModel << m_testCase << "-test-case";
428  if (m_writeResults)
429  {
430  p2p.EnablePcapAll (oss.str ());
431  p2p.EnableAsciiAll (oss.str ());
432  }
433 
434  std::ostringstream oss2;
435  oss2 << "src/test/ns3tcp/Tcp" << m_tcpModel << "." << m_testCase << ".log";
436  AsciiTraceHelper ascii;
437  if (m_writeLogging)
438  {
439  m_osw = ascii.CreateFileStream (oss2.str ());
440  *(m_osw->GetStream ()) << std::setprecision (9) << std::fixed;
441  p2p.EnableAsciiAll (m_osw);
442  }
443 
444  // Finally, set up the simulator to run. The 1000 second hard limit is a
445  // failsafe in case some change above causes the simulation to never end
446  Simulator::Stop (Seconds (1000));
447  Simulator::Run ();
448  Simulator::Destroy ();
449 }
450 
452 {
453 public:
455 };
456 
458  : TestSuite ("ns3-tcp-loss", SYSTEM)
459 {
460  SetDataDir (NS_TEST_SOURCEDIR);
461  Packet::EnablePrinting (); // Enable packet metadata for all test cases
462 
463  AddTestCase (new Ns3TcpLossTestCase ("Tahoe", 0), TestCase::QUICK);
464  AddTestCase (new Ns3TcpLossTestCase ("Tahoe", 1), TestCase::QUICK);
465  AddTestCase (new Ns3TcpLossTestCase ("Tahoe", 2), TestCase::QUICK);
466  AddTestCase (new Ns3TcpLossTestCase ("Tahoe", 3), TestCase::QUICK);
467  AddTestCase (new Ns3TcpLossTestCase ("Tahoe", 4), TestCase::QUICK);
468 
469  AddTestCase (new Ns3TcpLossTestCase ("Reno", 0), TestCase::QUICK);
470  AddTestCase (new Ns3TcpLossTestCase ("Reno", 1), TestCase::QUICK);
471  AddTestCase (new Ns3TcpLossTestCase ("Reno", 2), TestCase::QUICK);
472  AddTestCase (new Ns3TcpLossTestCase ("Reno", 3), TestCase::QUICK);
473  AddTestCase (new Ns3TcpLossTestCase ("Reno", 4), TestCase::QUICK);
474 
475  AddTestCase (new Ns3TcpLossTestCase ("NewReno", 0), TestCase::QUICK);
476  AddTestCase (new Ns3TcpLossTestCase ("NewReno", 1), TestCase::QUICK);
477  AddTestCase (new Ns3TcpLossTestCase ("NewReno", 2), TestCase::QUICK);
478  AddTestCase (new Ns3TcpLossTestCase ("NewReno", 3), TestCase::QUICK);
479  AddTestCase (new Ns3TcpLossTestCase ("NewReno", 4), TestCase::QUICK);
480 
481  AddTestCase (new Ns3TcpLossTestCase ("Westwood", 0), TestCase::QUICK);
482  AddTestCase (new Ns3TcpLossTestCase ("Westwood", 1), TestCase::QUICK);
483  AddTestCase (new Ns3TcpLossTestCase ("Westwood", 2), TestCase::QUICK);
484  AddTestCase (new Ns3TcpLossTestCase ("Westwood", 3), TestCase::QUICK);
485  AddTestCase (new Ns3TcpLossTestCase ("Westwood", 4), TestCase::QUICK);
486 
487  AddTestCase (new Ns3TcpLossTestCase ("WestwoodPlus", 0), TestCase::QUICK);
488  AddTestCase (new Ns3TcpLossTestCase ("WestwoodPlus", 1), TestCase::QUICK);
489  AddTestCase (new Ns3TcpLossTestCase ("WestwoodPlus", 2), TestCase::QUICK);
490  AddTestCase (new Ns3TcpLossTestCase ("WestwoodPlus", 3), TestCase::QUICK);
491  AddTestCase (new Ns3TcpLossTestCase ("WestwoodPlus", 4), TestCase::QUICK);
492 
493 }
494 
496