A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
ns3tcp-no-delay-test-suite.cc
Go to the documentation of this file.
1/*
2 * SPDX-License-Identifier: GPL-2.0-only
3 */
4
6
7#include "ns3/abort.h"
8#include "ns3/boolean.h"
9#include "ns3/config.h"
10#include "ns3/csma-helper.h"
11#include "ns3/data-rate.h"
12#include "ns3/inet-socket-address.h"
13#include "ns3/internet-stack-helper.h"
14#include "ns3/ipv4-address-helper.h"
15#include "ns3/ipv4-global-routing-helper.h"
16#include "ns3/log.h"
17#include "ns3/node-container.h"
18#include "ns3/packet-sink-helper.h"
19#include "ns3/pcap-file.h"
20#include "ns3/point-to-point-helper.h"
21#include "ns3/simulator.h"
22#include "ns3/string.h"
23#include "ns3/tcp-socket-factory.h"
24#include "ns3/test.h"
25#include "ns3/uinteger.h"
26
27using namespace ns3;
28
29NS_LOG_COMPONENT_DEFINE("Ns3TcpNoDelayTest");
30
31/**
32 * \ingroup system-tests-tcp
33 *
34 * \brief Tests of Nagle's algorithm and the TCP no delay option.
35 */
37{
38 public:
39 /**
40 * Constructor.
41 *
42 * \param noDelay Enable or disable TCP no delay option.
43 */
44 Ns3TcpNoDelayTestCase(bool noDelay);
45
47 {
48 }
49
50 private:
51 void DoRun() override;
52 bool m_noDelay; //!< Enable or disable TCP no delay option.
53 bool m_writeResults; //!< True if write PCAP files.
54
55 /**
56 * Receive a TCP packet.
57 * \param path The callback context (unused).
58 * \param p The received packet.
59 * \param address The sender's address (unused).
60 */
61 void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
62
63 TestVectors<uint32_t> m_inputs; //!< Sent packets test vector.
64 TestVectors<uint32_t> m_responses; //!< Received packets test vector.
65};
66
68 : TestCase(
69 "Check that ns-3 TCP Nagle's algorithm works correctly and that we can turn it off."),
70 m_noDelay(noDelay),
71 m_writeResults(false)
72{
73}
74
75void
77{
78 m_responses.Add(p->GetSize());
79}
80
81void
83{
84 uint16_t sinkPort = 50000;
85 double sinkStopTime = 8; // sec; will trigger Socket::Close
86 double writerStopTime = 5; // sec; will trigger Socket::Close
87 double simStopTime = 10; // sec
88 Time sinkStopTimeObj = Seconds(sinkStopTime);
89 Time writerStopTimeObj = Seconds(writerStopTime);
90 Time simStopTimeObj = Seconds(simStopTime);
91
94
95 PointToPointHelper pointToPoint;
96 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
97 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
98
99 NetDeviceContainer devices;
100 devices = pointToPoint.Install(n0, n1);
101
102 InternetStackHelper internet;
103 internet.InstallAll();
104
105 Ipv4AddressHelper address;
106 address.SetBase("10.1.1.0", "255.255.255.252");
107 Ipv4InterfaceContainer ifContainer = address.Assign(devices);
108
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",
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 // Enable or disable TCP no delay option
127 Config::SetDefault("ns3::TcpSocket::TcpNoDelay", BooleanValue(m_noDelay));
128 // This test was written with initial window of 1 segment
129 Config::SetDefault("ns3::TcpSocket::InitialCwnd", UintegerValue(1));
130
131 // Connect the socket writer
133
134 // Write 5 packets to get some bytes in flight and some acks going
135 Simulator::Schedule(Seconds(2), &SocketWriter::Write, socketWriter, 2680);
136 m_inputs.Add(536);
137 m_inputs.Add(536);
138 m_inputs.Add(536);
139 m_inputs.Add(536);
140 m_inputs.Add(536);
141
142 // Write one byte after 10 ms to ensure that some data is outstanding
143 // and the window is big enough
144 Simulator::Schedule(Seconds(2.010), &SocketWriter::Write, socketWriter, 1);
145
146 // If Nagle is not enabled, i.e. no delay is on, add an input for a 1-byte
147 // packet to be received
148 if (m_noDelay)
149 {
150 m_inputs.Add(1);
151 }
152
153 // One ms later, write 535 bytes, i.e. one segment size - 1
154 Simulator::Schedule(Seconds(2.012), &SocketWriter::Write, socketWriter, 535);
155
156 // If Nagle is not enabled, add an input for a 535 byte packet,
157 // otherwise, we should get a single "full" packet of 536 bytes
158 if (m_noDelay)
159 {
160 m_inputs.Add(535);
161 }
162 else
163 {
164 m_inputs.Add(536);
165 }
166
167 // Close down the socket
168 Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
169
170 if (m_writeResults)
171 {
172 std::ostringstream oss;
173 if (m_noDelay)
174 {
175 oss << "tcp-no-delay-on-test-case";
176 pointToPoint.EnablePcapAll(oss.str());
177 }
178 else
179 {
180 oss << "tcp-no-delay-off-test-case";
181 pointToPoint.EnablePcapAll(oss.str());
182 }
183 }
184
185 Simulator::Stop(simStopTimeObj);
188
189 // Compare inputs and outputs
192 "Incorrect number of expected receive events");
193 for (uint32_t i = 0; i < m_responses.GetN(); i++)
194 {
195 uint32_t in = m_inputs.Get(i);
196 uint32_t out = m_responses.Get(i);
198 out,
199 "Mismatch: expected " << in << " bytes, got " << out << " bytes");
200 }
201}
202
203/**
204 * \ingroup system-tests-tcp
205 *
206 * TCP Nagle's algorithm and the TCP no delay option TestSuite.
207 */
209{
210 public:
212};
213
215 : TestSuite("ns3-tcp-no-delay", Type::SYSTEM)
216{
217 AddTestCase(new Ns3TcpNoDelayTestCase(true), TestCase::Duration::QUICK);
218 AddTestCase(new Ns3TcpNoDelayTestCase(false), TestCase::Duration::QUICK);
219}
220
221/// Do not forget to allocate an instance of this TestSuite.
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.
void DoRun() override
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:90
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.
AttributeValue implementation for Boolean.
Definition boolean.h:26
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.
static Ipv4Address GetAny()
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
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Build a set of PointToPointNetDevice objects.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
void Connect()
Connect the socket.
void Write(uint32_t numBytes)
Write to the socket.
void Close()
Close the socket.
Hold variables of type string.
Definition string.h:45
encapsulates test code
Definition test.h:1050
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:292
A suite of tests to run.
Definition test.h:1267
Type
Type of test.
Definition test.h:1274
A simple way to store test vectors (for stimulus or from responses)
Definition test.h:1348
T Get(std::size_t i) const
Get the i'th test vector.
Definition test.h:1462
std::size_t GetN() const
Get the total number of test vectors.
Definition test.h:1455
std::size_t Add(T vector)
Definition test.h:1446
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Hold an unsigned integer type.
Definition uinteger.h:34
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
void Connect(std::string path, const CallbackBase &cb)
Definition config.cc:967
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
#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:134
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:684
static Ns3TcpNoDelayTestSuite g_ns3TcpNoDelayTestSuite
Do not forget to allocate an instance of this TestSuite.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44