A Discrete-Event Network Simulator
API
ns3tcp-socket-test-suite.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */
17 
18 #include "ns3tcp-socket-writer.h"
19 
20 #include "ns3/abort.h"
21 #include "ns3/config.h"
22 #include "ns3/csma-helper.h"
23 #include "ns3/data-rate.h"
24 #include "ns3/inet-socket-address.h"
25 #include "ns3/internet-stack-helper.h"
26 #include "ns3/ipv4-address-helper.h"
27 #include "ns3/ipv4-global-routing-helper.h"
28 #include "ns3/log.h"
29 #include "ns3/node-container.h"
30 #include "ns3/packet-sink-helper.h"
31 #include "ns3/pcap-file.h"
32 #include "ns3/point-to-point-helper.h"
33 #include "ns3/simulator.h"
34 #include "ns3/string.h"
35 #include "ns3/tcp-socket-factory.h"
36 #include "ns3/test.h"
37 #include "ns3/uinteger.h"
38 
39 using namespace ns3;
40 
41 NS_LOG_COMPONENT_DEFINE("Ns3SocketTest");
42 
50 {
51  public:
53 
55  {
56  }
57 
58  private:
59  void DoRun() override;
61 
68  void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
69 
72 };
73 
75  : TestCase("Check that ns-3 TCP successfully transfers an application data write of various "
76  "sizes (point-to-point)"),
77  m_writeResults(false)
78 {
79 }
80 
81 void
83 {
84  m_responses.Add(p->GetSize());
85 }
86 
87 void
89 {
90  uint16_t sinkPort = 50000;
91  double sinkStopTime = 40; // sec; will trigger Socket::Close
92  double writerStopTime = 30; // sec; will trigger Socket::Close
93  double simStopTime = 60; // sec
94  Time sinkStopTimeObj = Seconds(sinkStopTime);
95  Time writerStopTimeObj = Seconds(writerStopTime);
96  Time simStopTimeObj = Seconds(simStopTime);
97 
98  Ptr<Node> n0 = CreateObject<Node>();
99  Ptr<Node> n1 = CreateObject<Node>();
100 
102  pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
103  pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
104 
106  devices = pointToPoint.Install(n0, n1);
107 
109  internet.InstallAll();
110 
112  address.SetBase("10.1.1.0", "255.255.255.252");
113  Ipv4InterfaceContainer ifContainer = address.Assign(devices);
114 
115  Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter>();
116  Address sinkAddress(InetSocketAddress(ifContainer.GetAddress(1), sinkPort));
117  socketWriter->Setup(n0, sinkAddress);
118  n0->AddApplication(socketWriter);
119  socketWriter->SetStartTime(Seconds(0.));
120  socketWriter->SetStopTime(writerStopTimeObj);
121 
122  PacketSinkHelper sink("ns3::TcpSocketFactory",
123  InetSocketAddress(Ipv4Address::GetAny(), sinkPort));
124  ApplicationContainer apps = sink.Install(n1);
125  // Start the sink application at time zero, and stop it at sinkStopTime
126  apps.Start(Seconds(0.0));
127  apps.Stop(sinkStopTimeObj);
128 
129  Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
131 
132  Simulator::Schedule(Seconds(2), &SocketWriter::Connect, socketWriter);
133  // Send 1, 10, 100, 1000 bytes
134  Simulator::Schedule(Seconds(10), &SocketWriter::Write, socketWriter, 1);
135  m_inputs.Add(1);
136  Simulator::Schedule(Seconds(12), &SocketWriter::Write, socketWriter, 10);
137  m_inputs.Add(10);
138  Simulator::Schedule(Seconds(14), &SocketWriter::Write, socketWriter, 100);
139  m_inputs.Add(100);
140  Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1000);
141  m_inputs.Add(536);
142  m_inputs.Add(464); // ns-3 TCP default segment size of 536
143  Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
144 
145  if (m_writeResults)
146  {
147  pointToPoint.EnablePcapAll("tcp-socket-test-case-1");
148  }
149 
150  Simulator::Stop(simStopTimeObj);
151  Simulator::Run();
152  Simulator::Destroy();
153 
154  // Compare inputs and outputs
156  m_responses.GetN(),
157  "Incorrect number of expected receive events");
158  for (uint32_t i = 0; i < m_responses.GetN(); i++)
159  {
160  uint32_t in = m_inputs.Get(i);
161  uint32_t out = m_responses.Get(i);
163  out,
164  "Mismatch: expected " << in << " bytes, got " << out << " bytes");
165  }
166 }
167 
175 {
176  public:
178 
180  {
181  }
182 
183  private:
184  void DoRun() override;
186 
193  void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
194 
197 };
198 
200  : TestCase("Check to see that ns-3 TCP successfully transfers an application data write of "
201  "various sizes (CSMA)"),
202  m_writeResults(false)
203 {
204 }
205 
206 void
208 {
209  m_responses.Add(p->GetSize());
210 }
211 
212 void
214 {
215  uint16_t sinkPort = 50000;
216  double sinkStopTime = 40; // sec; will trigger Socket::Close
217  double writerStopTime = 30; // sec; will trigger Socket::Close
218  double simStopTime = 60; // sec
219  Time sinkStopTimeObj = Seconds(sinkStopTime);
220  Time writerStopTimeObj = Seconds(writerStopTime);
221  Time simStopTimeObj = Seconds(simStopTime);
222 
223  Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(1000));
224 
226  nodes.Create(2);
227  Ptr<Node> n0 = nodes.Get(0);
228  Ptr<Node> n1 = nodes.Get(1);
229 
231  csma.SetChannelAttribute("DataRate", StringValue("5Mbps"));
232  csma.SetChannelAttribute("Delay", StringValue("2ms"));
233 
235  devices = csma.Install(nodes);
236 
238  internet.InstallAll();
239 
241  address.SetBase("10.1.1.0", "255.255.255.252");
242  Ipv4InterfaceContainer ifContainer = address.Assign(devices);
243 
244  Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter>();
245  Address sinkAddress(InetSocketAddress(ifContainer.GetAddress(1), sinkPort));
246  socketWriter->Setup(n0, sinkAddress);
247  n0->AddApplication(socketWriter);
248  socketWriter->SetStartTime(Seconds(0.));
249  socketWriter->SetStopTime(writerStopTimeObj);
250 
251  PacketSinkHelper sink("ns3::TcpSocketFactory",
252  InetSocketAddress(Ipv4Address::GetAny(), sinkPort));
253  ApplicationContainer apps = sink.Install(n1);
254  // Start the sink application at time zero, and stop it at sinkStopTime
255  apps.Start(Seconds(0.0));
256  apps.Stop(sinkStopTimeObj);
257 
258  Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
260 
261  Simulator::Schedule(Seconds(2), &SocketWriter::Connect, socketWriter);
262  // Send 1, 10, 100, 1000 bytes
263  // PointToPoint default MTU is 576 bytes, which leaves 536 bytes for TCP
264  Simulator::Schedule(Seconds(10), &SocketWriter::Write, socketWriter, 1);
265  m_inputs.Add(1);
266  Simulator::Schedule(Seconds(12), &SocketWriter::Write, socketWriter, 10);
267  m_inputs.Add(10);
268  Simulator::Schedule(Seconds(14), &SocketWriter::Write, socketWriter, 100);
269  m_inputs.Add(100);
270  Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1000);
271  m_inputs.Add(1000);
272  // Next packet will fragment
273  Simulator::Schedule(Seconds(16), &SocketWriter::Write, socketWriter, 1001);
274  m_inputs.Add(1000);
275  m_inputs.Add(1);
276  Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
277 
278  if (m_writeResults)
279  {
280  csma.EnablePcapAll("tcp-socket-test-case-2", false);
281  }
282  Simulator::Stop(simStopTimeObj);
283  Simulator::Run();
284  Simulator::Destroy();
285 
286  // Compare inputs and outputs
288  m_responses.GetN(),
289  "Incorrect number of expected receive events");
290  for (uint32_t i = 0; i < m_responses.GetN(); i++)
291  {
292  uint32_t in = m_inputs.Get(i);
293  uint32_t out = m_responses.Get(i);
295  out,
296  "Mismatch: expected " << in << " bytes, got " << out << " bytes");
297  }
298 }
299 
306 {
307  public:
309 };
310 
312  : TestSuite("ns3-tcp-socket", SYSTEM)
313 {
314  AddTestCase(new Ns3TcpSocketTestCaseP2P, TestCase::QUICK);
315  AddTestCase(new Ns3TcpSocketTestCaseCsma, TestCase::QUICK);
316 }
317 
Tests of TCP implementations from the application/socket perspective using CSMA links.
TestVectors< uint32_t > m_inputs
Sent packets test vector.
TestVectors< uint32_t > m_responses
Received packets test vector.
bool m_writeResults
True if write PCAP files.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Receive a TCP packet.
void DoRun() override
Implementation to actually run this TestCase.
Tests of TCP implementations from the application/socket perspective using point-to-point links.
TestVectors< uint32_t > m_responses
Received packets test vector.
TestVectors< uint32_t > m_inputs
Sent packets test vector.
void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Receive a TCP packet.
void DoRun() override
Implementation to actually run this TestCase.
bool m_writeResults
True if write PCAP files.
TCP implementations from the application/socket perspective TestSuite.
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
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
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
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:56
encapsulates test code
Definition: test.h:1060
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1256
T Get(std::size_t i) const
Get the i'th test vector.
Definition: test.h:1401
std::size_t GetN() const
Get the total number of test vectors.
Definition: test.h:1394
std::size_t Add(T vector)
Definition: test.h:1385
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Hold an unsigned integer type.
Definition: uinteger.h:45
static void SinkRx(std::string path, Ptr< const Packet > p, const Address &address)
Rx sink.
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:974
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:144
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
address
Definition: first.py:47
pointToPoint
Definition: first.py:38
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
csma
Definition: second.py:63
static Ns3TcpSocketTestSuite g_ns3TcpSocketTestSuite
Do not forget to allocate an instance of this TestSuite.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55