A Discrete-Event Network Simulator
API
nsctcp-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 
20 #include "ns3/log.h"
21 #include "ns3/abort.h"
22 #include "ns3/test.h"
23 #include "ns3/pcap-file.h"
24 #include "ns3/config.h"
25 #include "ns3/string.h"
26 #include "ns3/uinteger.h"
27 #include "ns3/data-rate.h"
28 #include "ns3/inet-socket-address.h"
29 #include "ns3/point-to-point-helper.h"
30 #include "ns3/internet-stack-helper.h"
31 #include "ns3/ipv4-global-routing-helper.h"
32 #include "ns3/ipv4-address-helper.h"
33 #include "ns3/packet-sink-helper.h"
34 #include "ns3/tcp-socket-factory.h"
35 #include "ns3/node-container.h"
36 #include "ns3/simulator.h"
37 #include "ns3/error-model.h"
38 #include "ns3/pointer.h"
39 #include "../ns3tcp/ns3tcp-socket-writer.h"
40 
41 using namespace ns3;
42 
43 NS_LOG_COMPONENT_DEFINE ("NscTcpLossTest");
44 
45 // ===========================================================================
46 // Tests of TCP implementation loss behavior
47 // ===========================================================================
48 //
49 
51 {
52 public:
54  virtual ~NscTcpLossTestCase1 () {}
55 
56 private:
57  virtual void DoRun (void);
59 
60  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
61 
64 };
65 
67  : TestCase ("Check that nsc TCP survives loss of first two SYNs"),
68  m_writeResults (false)
69 {
70 }
71 
72 void
74 {
75  m_responses.Add (p->GetSize ());
76 }
77 
78 void
80 {
81  uint16_t sinkPort = 50000;
82  double sinkStopTime = 40; // sec; will trigger Socket::Close
83  double writerStopTime = 30; // sec; will trigger Socket::Close
84  double simStopTime = 60; // sec
85  Time sinkStopTimeObj = Seconds (sinkStopTime);
86  Time writerStopTimeObj = Seconds (writerStopTime);
87  Time simStopTimeObj= Seconds (simStopTime);
88  // This test was written with initial window of 1 segment
89  Config::SetDefault ("ns3::TcpSocket::InitialCwnd", UintegerValue (1));
90 
91  Ptr<Node> n0 = CreateObject<Node> ();
92  Ptr<Node> n1 = CreateObject<Node> ();
93 
95  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
96  pointToPoint.SetChannelAttribute ("Delay", StringValue ("200ms"));
97 
99  devices = pointToPoint.Install (n0, n1);
100 
101  InternetStackHelper internet;
102  internet.SetTcp ("ns3::NscTcpL4Protocol", "Library", StringValue ("liblinux2.6.26.so"));
103  internet.InstallAll ();
104 
106  address.SetBase ("10.1.1.0", "255.255.255.252");
107  Ipv4InterfaceContainer ifContainer = address.Assign (devices);
108 
109  Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
110  Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
111  socketWriter->Setup (n0, sinkAddress);
112  n0->AddApplication (socketWriter);
113  socketWriter->SetStartTime (Seconds (0.));
114  socketWriter->SetStopTime (writerStopTimeObj);
115 
116  PacketSinkHelper sink ("ns3::TcpSocketFactory",
117  InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
118  ApplicationContainer apps = sink.Install (n1);
119  // Start the sink application at time zero, and stop it at sinkStopTime
120  apps.Start (Seconds (0.0));
121  apps.Stop (sinkStopTimeObj);
122 
123  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
125 
126  Simulator::Schedule (Seconds (2), &SocketWriter::Connect, socketWriter);
127  Simulator::Schedule (Seconds (10), &SocketWriter::Write, socketWriter, 500);
128  m_inputs.Add (500);
129  Simulator::Schedule (writerStopTimeObj, &SocketWriter::Close, socketWriter);
130 
131  std::list<uint32_t> sampleList;
132  // Lose first two SYNs
133  sampleList.push_back (0);
134  sampleList.push_back (1);
135  // This time, we'll explicitly create the error model we want
136  Ptr<ReceiveListErrorModel> pem = CreateObject<ReceiveListErrorModel> ();
137  pem->SetList (sampleList);
138  devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (pem));
139 
140  if (m_writeResults)
141  {
142  pointToPoint.EnablePcapAll ("nsc-tcp-loss-test-case-1");
143  pointToPoint.EnableAsciiAll ("nsc-tcp-loss-test-case-1");
144  }
145 
146  Simulator::Stop (simStopTimeObj);
147  Simulator::Run ();
148  Simulator::Destroy ();
149 
150  // Compare inputs and outputs
151  NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
152  for (uint32_t i = 0; i < m_responses.GetN (); i++)
153  {
154  uint32_t in = m_inputs.Get (i);
155  uint32_t out = m_responses.Get (i);
156  NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes");
157  }
158 }
159 
161 {
162 public:
164  virtual ~NscTcpLossTestCase2 () {}
165 
166 private:
167  virtual void DoRun (void);
169 
170  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
171 
174 };
175 
177  : TestCase ("Check that nsc TCP survives loss of first data packet"),
178  m_writeResults (false)
179 {
180 }
181 
182 void
184 {
185  m_responses.Add (p->GetSize ());
186 }
187 
188 void
190 {
191  uint16_t sinkPort = 50000;
192  double sinkStopTime = 40; // sec; will trigger Socket::Close
193  double writerStopTime = 12; // sec; will trigger Socket::Close
194  double simStopTime = 60; // sec
195  Time sinkStopTimeObj = Seconds (sinkStopTime);
196  Time writerStopTimeObj = Seconds (writerStopTime);
197  Time simStopTimeObj= Seconds (simStopTime);
198  // This test was written with initial window of 1 segment
199  Config::SetDefault ("ns3::TcpSocket::InitialCwnd", UintegerValue (1));
200 
201  Ptr<Node> n0 = CreateObject<Node> ();
202  Ptr<Node> n1 = CreateObject<Node> ();
203 
205  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
206  pointToPoint.SetChannelAttribute ("Delay", StringValue ("200ms"));
207 
209  devices = pointToPoint.Install (n0, n1);
210 
211  InternetStackHelper internet;
212  internet.SetTcp ("ns3::NscTcpL4Protocol", "Library", StringValue ("liblinux2.6.26.so"));
213  internet.InstallAll ();
214 
216  address.SetBase ("10.1.1.0", "255.255.255.252");
217  Ipv4InterfaceContainer ifContainer = address.Assign (devices);
218 
219  Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
220  Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
221  socketWriter->Setup (n0, sinkAddress);
222  n0->AddApplication (socketWriter);
223  socketWriter->SetStartTime (Seconds (0.));
224  socketWriter->SetStopTime (writerStopTimeObj);
225 
226  PacketSinkHelper sink ("ns3::TcpSocketFactory",
227  InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
228  ApplicationContainer apps = sink.Install (n1);
229  // Start the sink application at time zero, and stop it at sinkStopTime
230  apps.Start (Seconds (0.0));
231  apps.Stop (sinkStopTimeObj);
232 
233  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
235 
236  Simulator::Schedule (Seconds (2), &SocketWriter::Connect, socketWriter);
237  Simulator::Schedule (Seconds (10), &SocketWriter::Write, socketWriter, 500);
238  m_inputs.Add (500);
239  Simulator::Schedule (writerStopTimeObj, &SocketWriter::Close, socketWriter);
240 
241  std::list<uint32_t> sampleList;
242  // Lose first data segment
243  sampleList.push_back (2);
244  // This time, we'll explicitly create the error model we want
245  Ptr<ReceiveListErrorModel> pem = CreateObject<ReceiveListErrorModel> ();
246  pem->SetList (sampleList);
247  devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (pem));
248 
249  if (m_writeResults)
250  {
251  pointToPoint.EnablePcapAll ("nsc-tcp-loss-test-case-2");
252  pointToPoint.EnableAsciiAll ("nsc-tcp-loss-test-case-2");
253  }
254 
255  Simulator::Stop (simStopTimeObj);
256  Simulator::Run ();
257  Simulator::Destroy ();
258 
259  // Compare inputs and outputs
260  NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
261  for (uint32_t i = 0; i < m_responses.GetN (); i++)
262  {
263  uint32_t in = m_inputs.Get (i);
264  uint32_t out = m_responses.Get (i);
265  NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes");
266  }
267 }
268 
270 {
271 public:
273 };
274 
276  : TestSuite ("nsc-tcp-loss", SYSTEM)
277 {
278  AddTestCase (new NscTcpLossTestCase1, TestCase::QUICK);
279  AddTestCase (new NscTcpLossTestCase2, TestCase::QUICK);
280 }
281 
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:159
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:75
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
an Inet address class
TestVectors< uint32_t > m_responses
virtual void DoRun(void)
Implementation to actually run this TestCase.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Hold variables of type string.
Definition: string.h:41
A suite of tests to run.
Definition: test.h:1343
virtual void DoRun(void)
Implementation to actually run this TestCase.
TestVectors< uint32_t > m_inputs
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
aggregate IP/TCP/UDP functionality to existing Nodes.
T Get(std::size_t i) const
Get the i&#39;th test vector.
Definition: test.h:1483
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
Build a set of PointToPointNetDevice objects.
encapsulates test code
Definition: test.h:1153
a polymophic address class
Definition: address.h:90
std::size_t GetN(void) const
Get the total number of test vectors.
Definition: test.h:1476
void Setup(Ptr< Node > node, Address peer)
TestVectors< uint32_t > m_inputs
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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:166
pointToPoint
Definition: first.py:35
holds a vector of ns3::NetDevice pointers
TestVectors< uint32_t > m_responses
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:918
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
address
Definition: first.py:44
void SetTcp(std::string tid)
set the Tcp stack which will not need any other parameter.
void SetList(const std::list< uint32_t > &packetlist)
Definition: error-model.cc:520
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
std::size_t Add(T vector)
Definition: test.h:1467
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
devices
Definition: first.py:39
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
static NscTcpLossTestSuite nscTcpLossTestSuite
void SetStartTime(Time start)
Specify application start time.
Definition: application.cc:69
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642