A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
tcp-bytes-in-flight-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 Natale Patriciello <natale.patriciello@gmail.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8#include "tcp-error-model.h"
9#include "tcp-general-test.h"
10
11#include "ns3/config.h"
12#include "ns3/log.h"
13#include "ns3/node.h"
14
15using namespace ns3;
16
17NS_LOG_COMPONENT_DEFINE("TcpBytesInFlightTestSuite");
18
19/**
20 * \ingroup internet-test
21 *
22 * \brief Check the value of BytesInFlight against a home-made guess
23 *
24 * The guess is made wrt to segments that travel the network; we have,
25 * in theory, the possibility to know the real amount of bytes in flight. However
26 * this value is useless, since the sender bases its guess on the received ACK.
27 *
28 * \see Tx
29 * \see BytesInFlightTrace
30 */
32{
33 public:
34 /**
35 * \brief Constructor.
36 * \param desc Description.
37 * \param toDrop Packets to drop.
38 */
39 TcpBytesInFlightTest(const std::string& desc, std::vector<uint32_t>& toDrop);
40
41 protected:
42 /**
43 * \brief Create a receiver error model.
44 * \returns The receiver error model.
45 */
47 /**
48 * \brief Receive a packet.
49 * \param p The packet.
50 * \param h The TCP header.
51 * \param who Who the socket belongs to (sender or receiver).
52 */
53 void Rx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
54 /**
55 * \brief Transmit a packet.
56 * \param p The packet.
57 * \param h The TCP header.
58 * \param who Who the socket belongs to (sender or receiver).
59 */
60 void Tx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
61 /**
62 * \brief Track the bytes in flight.
63 * \param oldValue previous value.
64 * \param newValue actual value.
65 */
66 void BytesInFlightTrace(uint32_t oldValue, uint32_t newValue) override;
67
68 /**
69 * \brief Called when a packet is dropped.
70 * \param ipH The IP header.
71 * \param tcpH The TCP header.
72 * \param p The packet.
73 */
74 void PktDropped(const Ipv4Header& ipH, const TcpHeader& tcpH, Ptr<const Packet> p);
75
76 /**
77 * \brief Configure the test.
78 */
79 void ConfigureEnvironment() override;
80
81 /**
82 * \brief Do the checks before the RTO expires.
83 * \param tcb The TcpSocketState.
84 * \param who The socket.
85 */
86 void BeforeRTOExpired(const Ptr<const TcpSocketState> tcb, SocketWho who) override;
87
88 /**
89 * \brief Update when RTO expires
90 * \param oldVal old time value
91 * \param newVal new time value
92 */
93 void RTOExpired(Time oldVal, Time newVal);
94
95 /**
96 * \brief Do the final checks.
97 */
98 void FinalChecks() override;
99
100 private:
101 uint32_t m_guessedBytesInFlight; //!< Guessed bytes in flight.
102 uint32_t m_dupAckRecv; //!< Number of DupACKs received.
103 SequenceNumber32 m_lastAckRecv; //!< Last ACK received.
104 SequenceNumber32 m_greatestSeqSent; //!< greatest sequence number sent.
105 std::vector<uint32_t> m_toDrop; //!< List of SequenceNumber to drop
106};
107
108TcpBytesInFlightTest::TcpBytesInFlightTest(const std::string& desc, std::vector<uint32_t>& toDrop)
109 : TcpGeneralTest(desc),
110 m_guessedBytesInFlight(0),
111 m_dupAckRecv(0),
112 m_lastAckRecv(1),
113 m_greatestSeqSent(0),
114 m_toDrop(toDrop)
115{
116}
117
118void
128
131{
133 for (auto it = m_toDrop.begin(); it != m_toDrop.end(); ++it)
134 {
135 m_errorModel->AddSeqToKill(SequenceNumber32(*it));
136 }
137
138 m_errorModel->SetDropCallback(MakeCallback(&TcpBytesInFlightTest::PktDropped, this));
139
140 return m_errorModel;
141}
142
143void
145{
146 NS_LOG_DEBUG("Before RTO for " << who);
147 GetSenderSocket()->TraceConnectWithoutContext(
148 "RTO",
150}
151
152void
154{
155 NS_LOG_DEBUG("RTO expired at " << newVal.GetSeconds());
157}
158
159void
161{
162 NS_LOG_DEBUG("Drop seq= " << tcpH.GetSequenceNumber() << " size " << p->GetSize());
163}
164
165void
167{
168 if (who == RECEIVER)
169 {
170 }
171 else if (who == SENDER)
172 {
173 if (h.GetAckNumber() > m_lastAckRecv)
174 { // New ack
176 NS_LOG_DEBUG("Recv ACK=" << h.GetAckNumber());
177
178 if (m_dupAckRecv > 0)
179 { // Previously we got some ACKs
181 { // This an ACK which acknowledge all the window
182 m_guessedBytesInFlight = 0; // All outstanding data acked
183 diff = 0;
184 m_dupAckRecv = 0;
185 }
186 else
187 {
188 // Partial ACK: Update the dupAck received count
189 m_dupAckRecv -= diff / GetSegSize(SENDER);
190 // During fast recovery the TCP data sender respond to a partial acknowledgment
191 // by inferring that the next in-sequence packet has been lost (RFC5681)
193 }
194 }
195
196 if ((h.GetFlags() & TcpHeader::FIN) != 0 || m_guessedBytesInFlight + 1 == diff)
197 { // received the ACK for the FIN (which includes 1 spurious byte)
198 diff -= 1;
199 }
202 NS_LOG_DEBUG("Update m_guessedBytesInFlight to " << m_guessedBytesInFlight);
203 }
204 else if (h.GetAckNumber() == m_lastAckRecv && m_lastAckRecv != SequenceNumber32(1) &&
205 (h.GetFlags() & TcpHeader::FIN) == 0)
206 {
207 // For each dupack I should guess that a segment has been received
208 // Please do not count FIN and SYN/ACK as dupacks
210 m_dupAckRecv++;
211 // RFC 6675 says after two dupacks, the segment is considered lost
212 if (m_dupAckRecv == 3)
213 {
214 NS_LOG_DEBUG("Loss of a segment detected");
216 }
217 NS_LOG_DEBUG("Dupack received, Update m_guessedBytesInFlight to "
219 }
220 }
221}
222
223void
225{
226 if (who == SENDER)
227 {
228 static SequenceNumber32 retr(0);
229 static uint32_t times = 0;
230
232 { // This is not a retransmission
234 times = 0;
235 }
236
237 if (retr == h.GetSequenceNumber())
238 {
239 ++times;
240 }
241
242 if (times < 2)
243 {
244 // count retransmission only one time
245 m_guessedBytesInFlight += p->GetSize();
246 }
247 retr = h.GetSequenceNumber();
248
249 NS_LOG_DEBUG("TX size=" << p->GetSize() << " seq=" << h.GetSequenceNumber()
250 << " m_guessedBytesInFlight=" << m_guessedBytesInFlight);
251 }
252}
253
254void
256{
257 NS_LOG_DEBUG("Socket BytesInFlight=" << newValue << " mine is=" << m_guessedBytesInFlight);
259 newValue,
260 "At time " << Simulator::Now().GetSeconds()
261 << "; guessed and measured bytes in flight differs");
262}
263
264void
266{
268 0,
269 "Still present bytes in flight at the end of the transmission");
270}
271
272/**
273 * \ingroup internet-test
274 *
275 * \brief TestSuite: Check the value of BytesInFlight against a home-made guess
276 */
278{
279 public:
281 : TestSuite("tcp-bytes-in-flight-test", Type::UNIT)
282 {
283 std::vector<uint32_t> toDrop;
284 AddTestCase(new TcpBytesInFlightTest("BytesInFlight value, no drop", toDrop),
285 TestCase::Duration::QUICK);
286 toDrop.push_back(4001);
287 AddTestCase(new TcpBytesInFlightTest("BytesInFlight value, one drop", toDrop),
288 TestCase::Duration::QUICK);
289 toDrop.push_back(4001);
291 new TcpBytesInFlightTest("BytesInFlight value, two drop of same segment", toDrop),
292 TestCase::Duration::QUICK);
293 toDrop.pop_back();
294 toDrop.push_back(4501);
296 new TcpBytesInFlightTest("BytesInFlight value, two drop of consecutive segments",
297 toDrop),
298 TestCase::Duration::QUICK);
299 }
300};
301
303 g_tcpBytesInFlightTestSuite; //!< Static variable for test initialization
Check the value of BytesInFlight against a home-made guess.
void ConfigureEnvironment() override
Configure the test.
std::vector< uint32_t > m_toDrop
List of SequenceNumber to drop.
void BytesInFlightTrace(uint32_t oldValue, uint32_t newValue) override
Track the bytes in flight.
void FinalChecks() override
Do the final checks.
void BeforeRTOExpired(const Ptr< const TcpSocketState > tcb, SocketWho who) override
Do the checks before the RTO expires.
SequenceNumber32 m_lastAckRecv
Last ACK received.
uint32_t m_guessedBytesInFlight
Guessed bytes in flight.
void Tx(const Ptr< const Packet > p, const TcpHeader &h, SocketWho who) override
Transmit a packet.
void PktDropped(const Ipv4Header &ipH, const TcpHeader &tcpH, Ptr< const Packet > p)
Called when a packet is dropped.
TcpBytesInFlightTest(const std::string &desc, std::vector< uint32_t > &toDrop)
Constructor.
void RTOExpired(Time oldVal, Time newVal)
Update when RTO expires.
uint32_t m_dupAckRecv
Number of DupACKs received.
void Rx(const Ptr< const Packet > p, const TcpHeader &h, SocketWho who) override
Receive a packet.
Ptr< ErrorModel > CreateReceiverErrorModel() override
Create a receiver error model.
SequenceNumber32 m_greatestSeqSent
greatest sequence number sent.
TestSuite: Check the value of BytesInFlight against a home-made guess.
Packet header for IPv4.
Definition ipv4-header.h:23
Smart pointer class similar to boost::intrusive_ptr.
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
General infrastructure for TCP testing.
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.
uint32_t GetSegSize(SocketWho who)
Get the segment size of the node specified.
virtual void ConfigureEnvironment()
Change the configuration of the environment.
Ptr< TcpSocketMsgBase > GetSenderSocket()
Get the pointer to a previously created sender 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
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
uint8_t GetFlags() const
Get the flags.
SequenceNumber32 GetAckNumber() const
Get the ACK number.
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
static constexpr auto UNIT
Definition test.h:1291
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
#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
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
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 TcpBytesInFlightTestSuite g_tcpBytesInFlightTestSuite
Static variable for test initialization.