A Discrete-Event Network Simulator
API
ns3tcp-no-delay-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  */
16 
17 #include "ns3/log.h"
18 #include "ns3/abort.h"
19 #include "ns3/test.h"
20 #include "ns3/pcap-file.h"
21 #include "ns3/config.h"
22 #include "ns3/string.h"
23 #include "ns3/uinteger.h"
24 #include "ns3/boolean.h"
25 #include "ns3/data-rate.h"
26 #include "ns3/inet-socket-address.h"
27 #include "ns3/point-to-point-helper.h"
28 #include "ns3/csma-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/node-container.h"
35 #include "ns3/simulator.h"
36 #include "ns3tcp-socket-writer.h"
37 
38 using namespace ns3;
39 
40 NS_LOG_COMPONENT_DEFINE ("Ns3TcpNoDelayTest");
41 
48 {
49 public:
55  Ns3TcpNoDelayTestCase (bool noDelay);
56  virtual ~Ns3TcpNoDelayTestCase () {}
57 
58 private:
59  virtual void DoRun (void);
60  bool m_noDelay;
62 
69  void SinkRx (std::string path, Ptr<const Packet> p, const Address &address);
70 
73 };
74 
76  : TestCase ("Check that ns-3 TCP Nagle's algorithm works correctly and that we can turn it off."),
77  m_noDelay (noDelay),
78  m_writeResults (false)
79 {
80 }
81 
82 void
84 {
85  m_responses.Add (p->GetSize ());
86 }
87 
88 void
90 {
91  uint16_t sinkPort = 50000;
92  double sinkStopTime = 8; // sec; will trigger Socket::Close
93  double writerStopTime = 5; // sec; will trigger Socket::Close
94  double simStopTime = 10; // sec
95  Time sinkStopTimeObj = Seconds (sinkStopTime);
96  Time writerStopTimeObj = Seconds (writerStopTime);
97  Time simStopTimeObj= Seconds (simStopTime);
98 
99  Ptr<Node> n0 = CreateObject<Node> ();
100  Ptr<Node> n1 = CreateObject<Node> ();
101 
103  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
104  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
105 
107  devices = pointToPoint.Install (n0, n1);
108 
109  InternetStackHelper internet;
110  internet.InstallAll ();
111 
113  address.SetBase ("10.1.1.0", "255.255.255.252");
114  Ipv4InterfaceContainer ifContainer = address.Assign (devices);
115 
116  Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter> ();
117  Address sinkAddress (InetSocketAddress (ifContainer.GetAddress (1), sinkPort));
118  socketWriter->Setup (n0, sinkAddress);
119  n0->AddApplication (socketWriter);
120  socketWriter->SetStartTime (Seconds (0.));
121  socketWriter->SetStopTime (writerStopTimeObj);
122 
123  PacketSinkHelper sink ("ns3::TcpSocketFactory",
124  InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
125  ApplicationContainer apps = sink.Install (n1);
126  // Start the sink application at time zero, and stop it at sinkStopTime
127  apps.Start (Seconds (0.0));
128  apps.Stop (sinkStopTimeObj);
129 
130  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
132 
133  // Enable or disable TCP no delay option
134  Config::SetDefault ("ns3::TcpSocket::TcpNoDelay", BooleanValue (m_noDelay));
135  // This test was written with initial window of 1 segment
136  Config::SetDefault ("ns3::TcpSocket::InitialCwnd", UintegerValue (1));
137 
138  // Connect the socket writer
139  Simulator::Schedule (Seconds (1), &SocketWriter::Connect, socketWriter);
140 
141  // Write 5 packets to get some bytes in flight and some acks going
142  Simulator::Schedule (Seconds (2), &SocketWriter::Write, socketWriter, 2680);
143  m_inputs.Add (536);
144  m_inputs.Add (536);
145  m_inputs.Add (536);
146  m_inputs.Add (536);
147  m_inputs.Add (536);
148 
149  // Write one byte after 10 ms to ensure that some data is outstanding
150  // and the window is big enough
151  Simulator::Schedule (Seconds (2.010), &SocketWriter::Write, socketWriter, 1);
152 
153  // If Nagle is not enabled, i.e. no delay is on, add an input for a 1-byte
154  // packet to be received
155  if (m_noDelay)
156  {
157  m_inputs.Add (1);
158  }
159 
160  // One ms later, write 535 bytes, i.e. one segment size - 1
161  Simulator::Schedule (Seconds (2.012), &SocketWriter::Write, socketWriter, 535);
162 
163  // If Nagle is not enabled, add an input for a 535 byte packet,
164  // otherwise, we should get a single "full" packet of 536 bytes
165  if (m_noDelay)
166  {
167  m_inputs.Add (535);
168  }
169  else
170  {
171  m_inputs.Add (536);
172  }
173 
174  // Close down the socket
175  Simulator::Schedule (writerStopTimeObj, &SocketWriter::Close, socketWriter);
176 
177  if (m_writeResults)
178  {
179  std::ostringstream oss;
180  if (m_noDelay)
181  {
182  oss << "tcp-no-delay-on-test-case";
183  pointToPoint.EnablePcapAll (oss.str ());
184  }
185  else
186  {
187  oss << "tcp-no-delay-off-test-case";
188  pointToPoint.EnablePcapAll (oss.str ());
189  }
190  }
191 
192  Simulator::Stop (simStopTimeObj);
193  Simulator::Run ();
194  Simulator::Destroy ();
195 
196  // Compare inputs and outputs
197  NS_TEST_ASSERT_MSG_EQ (m_inputs.GetN (), m_responses.GetN (), "Incorrect number of expected receive events");
198  for (uint32_t i = 0; i < m_responses.GetN (); i++)
199  {
200  uint32_t in = m_inputs.Get (i);
201  uint32_t out = m_responses.Get (i);
202  NS_TEST_ASSERT_MSG_EQ (in, out, "Mismatch: expected " << in << " bytes, got " << out << " bytes");
203  }
204 }
205 
212 {
213 public:
215 };
216 
218  : TestSuite ("ns3-tcp-no-delay", SYSTEM)
219 {
220  AddTestCase (new Ns3TcpNoDelayTestCase (true), TestCase::QUICK);
221  AddTestCase (new Ns3TcpNoDelayTestCase (false), TestCase::QUICK);
222 }
223 
Tests of Nagle's algorithm and the TCP no delay option.
Ns3TcpNoDelayTestCase(bool noDelay)
Constructor.
bool m_noDelay
Enable or disable TCP no delay option.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Receive a TCP packet.
TestVectors< uint32_t > m_responses
Received packets test vector.
virtual void DoRun(void)
Implementation to actually run this TestCase.
bool m_writeResults
True if write PCAP files.
TestVectors< uint32_t > m_inputs
Sent packets test vector.
TCP Nagle's algorithm and the TCP no delay option TestSuite.
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void InstallAll(void) const
Aggregate IPv4, IPv6, UDP, and TCP stacks to all nodes in the simulation.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:159
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Hold variables of type string.
Definition: string.h:41
encapsulates test code
Definition: test.h:994
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
A suite of tests to run.
Definition: test.h:1188
T Get(std::size_t i) const
Get the i'th test vector.
Definition: test.h:1331
std::size_t GetN(void) const
Get the total number of test vectors.
Definition: test.h:1324
std::size_t Add(T vector)
Definition: test.h:1315
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Hold an unsigned integer type.
Definition: uinteger.h:44
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#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:141
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
pointToPoint
Definition: first.py:35
devices
Definition: first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:1648
static Ns3TcpNoDelayTestSuite g_ns3TcpNoDelayTestSuite
Do not forget to allocate an instance of this TestSuite.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56