A Discrete-Event Network Simulator
API
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  * 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 
19 #include "tcp-error-model.h"
20 #include "tcp-general-test.h"
21 
22 #include "ns3/config.h"
23 #include "ns3/log.h"
24 #include "ns3/node.h"
25 
26 using namespace ns3;
27 
28 NS_LOG_COMPONENT_DEFINE("TcpBytesInFlightTestSuite");
29 
43 {
44  public:
50  TcpBytesInFlightTest(const std::string& desc, std::vector<uint32_t>& toDrop);
51 
52  protected:
57  Ptr<ErrorModel> CreateReceiverErrorModel() override;
64  void Rx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
71  void Tx(const Ptr<const Packet> p, const TcpHeader& h, SocketWho who) override;
77  void BytesInFlightTrace(uint32_t oldValue, uint32_t newValue) override;
78 
85  void PktDropped(const Ipv4Header& ipH, const TcpHeader& tcpH, Ptr<const Packet> p);
86 
90  void ConfigureEnvironment() override;
91 
97  void BeforeRTOExpired(const Ptr<const TcpSocketState> tcb, SocketWho who) override;
98 
104  void RTOExpired(Time oldVal, Time newVal);
105 
109  void FinalChecks() override;
110 
111  private:
113  uint32_t m_dupAckRecv;
116  std::vector<uint32_t> m_toDrop;
117 };
118 
119 TcpBytesInFlightTest::TcpBytesInFlightTest(const std::string& desc, std::vector<uint32_t>& toDrop)
120  : TcpGeneralTest(desc),
121  m_guessedBytesInFlight(0),
122  m_dupAckRecv(0),
123  m_lastAckRecv(1),
124  m_greatestSeqSent(0),
125  m_toDrop(toDrop)
126 {
127 }
128 
129 void
131 {
132  TcpGeneralTest::ConfigureEnvironment();
133  SetAppPktCount(30);
136 
137  Config::SetDefault("ns3::TcpSocketBase::Sack", BooleanValue(false));
138 }
139 
142 {
143  Ptr<TcpSeqErrorModel> m_errorModel = CreateObject<TcpSeqErrorModel>();
144  for (auto it = m_toDrop.begin(); it != m_toDrop.end(); ++it)
145  {
146  m_errorModel->AddSeqToKill(SequenceNumber32(*it));
147  }
148 
150 
151  return m_errorModel;
152 }
153 
154 void
156 {
157  NS_LOG_DEBUG("Before RTO for " << who);
158  GetSenderSocket()->TraceConnectWithoutContext(
159  "RTO",
161 }
162 
163 void
165 {
166  NS_LOG_DEBUG("RTO expired at " << newVal.GetSeconds());
168 }
169 
170 void
172 {
173  NS_LOG_DEBUG("Drop seq= " << tcpH.GetSequenceNumber() << " size " << p->GetSize());
174 }
175 
176 void
178 {
179  if (who == RECEIVER)
180  {
181  }
182  else if (who == SENDER)
183  {
184  if (h.GetAckNumber() > m_lastAckRecv)
185  { // New ack
186  uint32_t diff = h.GetAckNumber() - m_lastAckRecv;
187  NS_LOG_DEBUG("Recv ACK=" << h.GetAckNumber());
188 
189  if (m_dupAckRecv > 0)
190  { // Previously we got some ACKs
191  if (h.GetAckNumber() >= m_greatestSeqSent)
192  { // This an ACK which acknowledge all the window
193  m_guessedBytesInFlight = 0; // All outstanding data acked
194  diff = 0;
195  m_dupAckRecv = 0;
196  }
197  else
198  {
199  // Partial ACK: Update the dupAck received count
200  m_dupAckRecv -= diff / GetSegSize(SENDER);
201  // During fast recovery the TCP data sender respond to a partial acknowledgment
202  // by inferring that the next in-sequence packet has been lost (RFC5681)
204  }
205  }
206 
207  if ((h.GetFlags() & TcpHeader::FIN) != 0 || m_guessedBytesInFlight + 1 == diff)
208  { // received the ACK for the FIN (which includes 1 spurious byte)
209  diff -= 1;
210  }
211  m_guessedBytesInFlight -= diff;
213  NS_LOG_DEBUG("Update m_guessedBytesInFlight to " << m_guessedBytesInFlight);
214  }
215  else if (h.GetAckNumber() == m_lastAckRecv && m_lastAckRecv != SequenceNumber32(1) &&
216  (h.GetFlags() & TcpHeader::FIN) == 0)
217  {
218  // For each dupack I should guess that a segment has been received
219  // Please do not count FIN and SYN/ACK as dupacks
221  m_dupAckRecv++;
222  // RFC 6675 says after two dupacks, the segment is considered lost
223  if (m_dupAckRecv == 3)
224  {
225  NS_LOG_DEBUG("Loss of a segment detected");
227  }
228  NS_LOG_DEBUG("Dupack received, Update m_guessedBytesInFlight to "
230  }
231  }
232 }
233 
234 void
236 {
237  if (who == SENDER)
238  {
239  static SequenceNumber32 retr(0);
240  static uint32_t times = 0;
241 
243  { // This is not a retransmission
245  times = 0;
246  }
247 
248  if (retr == h.GetSequenceNumber())
249  {
250  ++times;
251  }
252 
253  if (times < 2)
254  {
255  // count retransmission only one time
257  }
258  retr = h.GetSequenceNumber();
259 
260  NS_LOG_DEBUG("TX size=" << p->GetSize() << " seq=" << h.GetSequenceNumber()
261  << " m_guessedBytesInFlight=" << m_guessedBytesInFlight);
262  }
263 }
264 
265 void
266 TcpBytesInFlightTest::BytesInFlightTrace(uint32_t oldValue, uint32_t newValue)
267 {
268  NS_LOG_DEBUG("Socket BytesInFlight=" << newValue << " mine is=" << m_guessedBytesInFlight);
270  newValue,
271  "At time " << Simulator::Now().GetSeconds()
272  << "; guessed and measured bytes in flight differs");
273 }
274 
275 void
277 {
279  0,
280  "Still present bytes in flight at the end of the transmission");
281 }
282 
289 {
290  public:
292  : TestSuite("tcp-bytes-in-flight-test", UNIT)
293  {
294  std::vector<uint32_t> toDrop;
295  AddTestCase(new TcpBytesInFlightTest("BytesInFlight value, no drop", toDrop),
296  TestCase::QUICK);
297  toDrop.push_back(4001);
298  AddTestCase(new TcpBytesInFlightTest("BytesInFlight value, one drop", toDrop),
299  TestCase::QUICK);
300  toDrop.push_back(4001);
301  AddTestCase(
302  new TcpBytesInFlightTest("BytesInFlight value, two drop of same segment", toDrop),
303  TestCase::QUICK);
304  toDrop.pop_back();
305  toDrop.push_back(4501);
306  AddTestCase(
307  new TcpBytesInFlightTest("BytesInFlight value, two drop of consecutive segments",
308  toDrop),
309  TestCase::QUICK);
310  }
311 };
312 
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:34
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
void SetDropCallback(Callback< void, const Ipv4Header &, const TcpHeader &, Ptr< const Packet >> cb)
Set the drop callback.
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.
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:47
SequenceNumber32 GetSequenceNumber() const
Get the sequence number.
Definition: tcp-header.cc:118
uint8_t GetFlags() const
Get the flags.
Definition: tcp-header.cc:148
SequenceNumber32 GetAckNumber() const
Get the ACK number.
Definition: tcp-header.cc:124
void AddSeqToKill(const SequenceNumber32 &seq)
Add the sequence number to the list of segments to be killed.
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
@ UNIT
This test suite implements a Unit Test.
Definition: test.h:1265
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
SequenceNumber< uint32_t, int32_t > SequenceNumber32
32 bit Sequence number.
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:305
#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
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
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 TcpBytesInFlightTestSuite g_tcpBytesInFlightTestSuite
Static variable for test initialization.