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 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
17
18#include "ns3/abort.h"
19#include "ns3/boolean.h"
20#include "ns3/config.h"
21#include "ns3/csma-helper.h"
22#include "ns3/data-rate.h"
23#include "ns3/inet-socket-address.h"
24#include "ns3/internet-stack-helper.h"
25#include "ns3/ipv4-address-helper.h"
26#include "ns3/ipv4-global-routing-helper.h"
27#include "ns3/log.h"
28#include "ns3/node-container.h"
29#include "ns3/packet-sink-helper.h"
30#include "ns3/pcap-file.h"
31#include "ns3/point-to-point-helper.h"
32#include "ns3/simulator.h"
33#include "ns3/string.h"
34#include "ns3/tcp-socket-factory.h"
35#include "ns3/test.h"
36#include "ns3/uinteger.h"
37
38using namespace ns3;
39
40NS_LOG_COMPONENT_DEFINE("Ns3TcpNoDelayTest");
41
42/**
43 * \ingroup system-tests-tcp
44 *
45 * \brief Tests of Nagle's algorithm and the TCP no delay option.
46 */
48{
49 public:
50 /**
51 * Constructor.
52 *
53 * \param noDelay Enable or disable TCP no delay option.
54 */
55 Ns3TcpNoDelayTestCase(bool noDelay);
56
58 {
59 }
60
61 private:
62 void DoRun() override;
63 bool m_noDelay; //!< Enable or disable TCP no delay option.
64 bool m_writeResults; //!< True if write PCAP files.
65
66 /**
67 * Receive a TCP packet.
68 * \param path The callback context (unused).
69 * \param p The received packet.
70 * \param address The sender's address (unused).
71 */
72 void SinkRx(std::string path, Ptr<const Packet> p, const Address& address);
73
74 TestVectors<uint32_t> m_inputs; //!< Sent packets test vector.
75 TestVectors<uint32_t> m_responses; //!< Received packets test vector.
76};
77
79 : TestCase(
80 "Check that ns-3 TCP Nagle's algorithm works correctly and that we can turn it off."),
81 m_noDelay(noDelay),
82 m_writeResults(false)
83{
84}
85
86void
88{
89 m_responses.Add(p->GetSize());
90}
91
92void
94{
95 uint16_t sinkPort = 50000;
96 double sinkStopTime = 8; // sec; will trigger Socket::Close
97 double writerStopTime = 5; // sec; will trigger Socket::Close
98 double simStopTime = 10; // sec
99 Time sinkStopTimeObj = Seconds(sinkStopTime);
100 Time writerStopTimeObj = Seconds(writerStopTime);
101 Time simStopTimeObj = Seconds(simStopTime);
102
103 Ptr<Node> n0 = CreateObject<Node>();
104 Ptr<Node> n1 = CreateObject<Node>();
105
106 PointToPointHelper pointToPoint;
107 pointToPoint.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
108 pointToPoint.SetChannelAttribute("Delay", StringValue("2ms"));
109
110 NetDeviceContainer devices;
111 devices = pointToPoint.Install(n0, n1);
112
113 InternetStackHelper internet;
114 internet.InstallAll();
115
116 Ipv4AddressHelper address;
117 address.SetBase("10.1.1.0", "255.255.255.252");
118 Ipv4InterfaceContainer ifContainer = address.Assign(devices);
119
120 Ptr<SocketWriter> socketWriter = CreateObject<SocketWriter>();
121 Address sinkAddress(InetSocketAddress(ifContainer.GetAddress(1), sinkPort));
122 socketWriter->Setup(n0, sinkAddress);
123 n0->AddApplication(socketWriter);
124 socketWriter->SetStartTime(Seconds(0.));
125 socketWriter->SetStopTime(writerStopTimeObj);
126
127 PacketSinkHelper sink("ns3::TcpSocketFactory",
129 ApplicationContainer apps = sink.Install(n1);
130 // Start the sink application at time zero, and stop it at sinkStopTime
131 apps.Start(Seconds(0.0));
132 apps.Stop(sinkStopTimeObj);
133
134 Config::Connect("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx",
136
137 // Enable or disable TCP no delay option
138 Config::SetDefault("ns3::TcpSocket::TcpNoDelay", BooleanValue(m_noDelay));
139 // This test was written with initial window of 1 segment
140 Config::SetDefault("ns3::TcpSocket::InitialCwnd", UintegerValue(1));
141
142 // Connect the socket writer
144
145 // Write 5 packets to get some bytes in flight and some acks going
146 Simulator::Schedule(Seconds(2), &SocketWriter::Write, socketWriter, 2680);
147 m_inputs.Add(536);
148 m_inputs.Add(536);
149 m_inputs.Add(536);
150 m_inputs.Add(536);
151 m_inputs.Add(536);
152
153 // Write one byte after 10 ms to ensure that some data is outstanding
154 // and the window is big enough
155 Simulator::Schedule(Seconds(2.010), &SocketWriter::Write, socketWriter, 1);
156
157 // If Nagle is not enabled, i.e. no delay is on, add an input for a 1-byte
158 // packet to be received
159 if (m_noDelay)
160 {
161 m_inputs.Add(1);
162 }
163
164 // One ms later, write 535 bytes, i.e. one segment size - 1
165 Simulator::Schedule(Seconds(2.012), &SocketWriter::Write, socketWriter, 535);
166
167 // If Nagle is not enabled, add an input for a 535 byte packet,
168 // otherwise, we should get a single "full" packet of 536 bytes
169 if (m_noDelay)
170 {
171 m_inputs.Add(535);
172 }
173 else
174 {
175 m_inputs.Add(536);
176 }
177
178 // Close down the socket
179 Simulator::Schedule(writerStopTimeObj, &SocketWriter::Close, socketWriter);
180
181 if (m_writeResults)
182 {
183 std::ostringstream oss;
184 if (m_noDelay)
185 {
186 oss << "tcp-no-delay-on-test-case";
187 pointToPoint.EnablePcapAll(oss.str());
188 }
189 else
190 {
191 oss << "tcp-no-delay-off-test-case";
192 pointToPoint.EnablePcapAll(oss.str());
193 }
194 }
195
196 Simulator::Stop(simStopTimeObj);
199
200 // Compare inputs and outputs
203 "Incorrect number of expected receive events");
204 for (uint32_t i = 0; i < m_responses.GetN(); i++)
205 {
206 uint32_t in = m_inputs.Get(i);
207 uint32_t out = m_responses.Get(i);
209 out,
210 "Mismatch: expected " << in << " bytes, got " << out << " bytes");
211 }
212}
213
214/**
215 * \ingroup system-tests-tcp
216 *
217 * TCP Nagle's algorithm and the TCP no delay option TestSuite.
218 */
220{
221 public:
223};
224
226 : TestSuite("ns3-tcp-no-delay", Type::SYSTEM)
227{
228 AddTestCase(new Ns3TcpNoDelayTestCase(true), TestCase::Duration::QUICK);
229 AddTestCase(new Ns3TcpNoDelayTestCase(false), TestCase::Duration::QUICK);
230}
231
232/// 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: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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:56
encapsulates test code
Definition: test.h:1061
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:301
A suite of tests to run.
Definition: test.h:1268
Type
Type of test.
Definition: test.h:1275
A simple way to store test vectors (for stimulus or from responses)
Definition: test.h:1342
T Get(std::size_t i) const
Get the i'th test vector.
Definition: test.h:1424
std::size_t GetN() const
Get the total number of test vectors.
Definition: test.h:1417
std::size_t Add(T vector)
Definition: test.h:1408
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Hold an unsigned integer type.
Definition: uinteger.h:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:978
#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:145
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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
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:55