A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-window-update-test-suite.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Gabriel Ferreira <gabrielcarvfer@gmail.com>
7 */
8
9#include "tcp-general-test.h"
10
11#include "ns3/boolean.h"
12#include "ns3/log.h"
13#include "ns3/node.h"
14#include "ns3/nstime.h"
15#include "ns3/simulator.h"
16#include "ns3/tcp-header.h"
17#include "ns3/uinteger.h"
18
19using namespace ns3;
20
21NS_LOG_COMPONENT_DEFINE("TcpWindowUpdateTestSuite");
22
23/**
24 * @ingroup internet-test
25 * @ingroup tests
26 *
27 * @brief Regression test for @issueid{125}.
28 *
29 * @issueid{125}: when the receiving application reads buffered data and
30 * thereby frees up receive-buffer space, TCP must proactively send a
31 * window-update ACK rather than leave a sender stalled on a zero (closed)
32 * window until its persist timer (zero-window probe) fires. This matches
33 * TcpSocketBase::SetRcvBufSize(), which, when the buffer is enlarged while
34 * the connection is established, explicitly transmits an ACK to advertise
35 * the reopened window.
36 *
37 * Scenario:
38 * - The receiver is created with a small RcvBufSize so that, after the sender
39 * fills it, the receiver advertises a window of 0 and the sender stalls.
40 * - The receiving application does NOT read while the buffer fills (the
41 * default ReceivePacket(), which drains the buffer, is overridden to do
42 * nothing).
43 * - At a fixed, deterministic time, well before the sender's persist timer
44 * could fire (the persist timeout is deliberately set very large), the
45 * receiving application reads the entire buffer via Recv(), freeing all
46 * receive-buffer space.
47 *
48 * Correct behaviour (asserted here): immediately after the application read,
49 * and WITHOUT waiting for the sender's persist timer, the receiver transmits
50 * an ACK advertising a non-zero window (a window update).
51 */
53{
54 public:
55 /**
56 * @brief Constructor.
57 * @param desc Test description.
58 */
59 TcpWindowUpdateOnReadTest(const std::string& desc);
60
61 protected:
63 void ReceivePacket(Ptr<Socket> socket) override;
64 void Tx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
65 void ConfigureEnvironment() override;
66 void ConfigureProperties() override;
67 void FinalChecks() override;
68
69 private:
70 /**
71 * @brief Scheduled application read that frees the entire receive buffer.
72 */
73 void AppRead();
74
75 Time m_readTime; //!< Time at which the application reads to free the window.
76 bool m_windowClosed; //!< True once the receiver has advertised a zero window.
77 bool m_appHasRead; //!< True once the scheduled application read has executed.
78 bool m_windowUpdateSent; //!< True if a window-update ACK was sent shortly after the read.
79 Time m_windowUpdateDeadline; //!< Latest time a read-triggered update may legitimately appear.
80};
81
91
92void
94{
96 // Send more data than the receiver buffer can hold so that the window closes.
98 SetAppPktSize(500);
99 SetMTU(500);
102 // TcpGeneralTest runs until the event queue empties. With the fix in place,
103 // the window update revives the sender, which then persist-probes the
104 // (still-closed) window forever because the application reads only once;
105 // bound the run well past the window-update deadline so it terminates.
107}
108
109void
111{
114 // A very large persist timeout guarantees that any window reopening observed
115 // around m_readTime is caused by the application read, not by a zero-window
116 // probe from the sender's persist timer.
117 GetSenderSocket()->SetAttribute("PersistTimeout", TimeValue(Seconds(600)));
118}
119
122{
124
125 // Disable window scaling so that TcpHeader::GetWindowSize() is the advertised
126 // window in bytes and assertions can be made directly on it.
127 socket->SetAttribute("WindowScaling", BooleanValue(false));
128 // Small receive buffer: a single segment. After the sender fills it, the
129 // receiver advertises a window of 0 and the sender stalls.
130 socket->SetAttribute("RcvBufSize", UintegerValue(500));
131
132 // The application reads (and frees the whole buffer) at a fixed, deterministic
133 // time, long before the (600 s) persist timer could fire.
135
136 return socket;
137}
138
139void
141{
142 // Intentionally do NOT read here: we want the receive buffer to fill so the
143 // window closes. The data is drained later, explicitly, in AppRead().
144 NS_LOG_INFO("RECEIVER application notified of data but does not read (window must close)");
145}
146
147void
149{
150 NS_LOG_INFO("RECEIVER application reads to free the receive buffer at "
151 << Simulator::Now().As(Time::S));
152 m_appHasRead = true;
154 // Drain everything currently buffered, freeing receive-buffer space.
155 while (Ptr<Packet> p = socket->Recv(0xffffffff, 0))
156 {
157 if (p->GetSize() == 0)
158 {
159 break;
160 }
161 }
162}
163
164void
166{
167 if (who != RECEIVER)
168 {
169 return;
170 }
171
172 NS_LOG_INFO("RECEIVER TX " << h << " size " << p->GetSize() << " at "
173 << Simulator::Now().As(Time::S));
174
175 // Track that the receiver has advertised a closed (zero) window at least once
176 // before the application read. This anchors the scenario: the window really
177 // did close, so the sender really was stalled.
178 if (!m_appHasRead && (h.GetWindowSize() == 0))
179 {
180 m_windowClosed = true;
181 }
182
183 // A window-update ACK triggered by the application read: an ACK carrying a
184 // non-zero window, emitted shortly after the read and well before the
185 // (600 s) persist timer. With a 500-byte buffer fully drained, the reopened
186 // window is 500.
188 {
189 m_windowUpdateSent = true;
190 }
191}
192
193void
195{
197 true,
198 "Precondition failed: receiver never advertised a zero window, "
199 "so the sender was never stalled");
201 true,
202 "Precondition failed: the scheduled application read did not run");
203 // Reading data that frees receive-buffer space must elicit a window-update
204 // ACK without waiting for the sender persist timer (@issueid{125}).
206 true,
207 "Receiver did not send a window-update ACK after the application "
208 "freed receive-buffer space (issue #125): the only relief is the "
209 "sender persist timer");
210}
211
212/**
213 * @ingroup internet-test
214 * @ingroup tests
215 *
216 * @brief TCP window-update-on-read TestSuite (@issueid{125}).
217 */
219{
220 public:
222 : TestSuite("tcp-window-update", Type::UNIT)
223 {
225 "window update ACK sent when application read frees rx buffer"),
227 }
228};
229
230static TcpWindowUpdateTestSuite g_tcpWindowUpdateTestSuite; //!< Static variable for test init
Regression test for Issue 125.
bool m_appHasRead
True once the scheduled application read has executed.
Ptr< TcpSocketMsgBase > CreateReceiverSocket(Ptr< Node > node) override
Create and install the socket to install on the receiver.
bool m_windowClosed
True once the receiver has advertised a zero window.
Time m_readTime
Time at which the application reads to free the window.
Time m_windowUpdateDeadline
Latest time a read-triggered update may legitimately appear.
void Tx(const Ptr< const Packet > p, const TcpHeader &h, SocketWho who) override
Packet transmitted down to IP layer.
void ConfigureProperties() override
Change the configuration of the socket properties.
TcpWindowUpdateOnReadTest(const std::string &desc)
Constructor.
void ReceivePacket(Ptr< Socket > socket) override
Packet received.
void ConfigureEnvironment() override
Change the configuration of the environment.
void AppRead()
Scheduled application read that frees the entire receive buffer.
void FinalChecks() override
Performs the (eventual) final checks through test asserts.
bool m_windowUpdateSent
True if a window-update ACK was sent shortly after the read.
TCP window-update-on-read TestSuite (Issue 125).
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:191
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
void SetPropagationDelay(Time propDelay)
Propagation delay of the bottleneck link.
void SetAppPktCount(uint32_t pktCount)
Set app packet count.
SocketWho
Used as parameter of methods, specifies on what node the caller is interested (e.g.
@ RECEIVER
Receiver node.
void SetAppPktSize(uint32_t pktSize)
Set app packet size.
void SetInitialCwnd(SocketWho who, uint32_t initialCwnd)
Forcefully set the initial cwnd.
virtual Ptr< TcpSocketMsgBase > CreateReceiverSocket(Ptr< Node > node)
Create and install the socket to install on the receiver.
virtual void ConfigureProperties()
Change the configuration of the socket properties.
void SetMTU(uint32_t mtu)
MTU of the bottleneck link.
virtual void ConfigureEnvironment()
Change the configuration of the environment.
TcpGeneralTest(const std::string &desc)
TcpGeneralTest constructor.
Ptr< TcpSocketMsgBase > GetSenderSocket()
Get the pointer to a previously created sender socket.
Ptr< TcpSocketMsgBase > GetReceiverSocket()
Get the pointer to a previously created receiver socket.
void SetTransmitStart(Time startTime)
Set the initial time at which the application sends the first data packet.
Header for the Transmission Control Protocol.
Definition tcp-header.h:36
uint16_t GetWindowSize() const
Get the window size.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
Type
Type of test.
Definition test.h:1271
@ UNIT
This test suite implements a Unit Test.
Definition test.h:1273
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Simulation virtual time values and global simulation resolution.
Definition nstime.h:95
@ S
second
Definition nstime.h:106
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:274
#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:133
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1330
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TcpWindowUpdateTestSuite g_tcpWindowUpdateTestSuite
Static variable for test init.