A Discrete-Event Network Simulator
API
tc-flow-control-test-suite.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2017 Universita' degli Studi di Napoli Federico II
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Stefano Avallone <stavallo@unina.it>
19  *
20  */
21 
22 #include "ns3/test.h"
23 #include "ns3/uinteger.h"
24 #include "ns3/pointer.h"
25 #include "ns3/string.h"
26 #include "ns3/double.h"
27 #include "ns3/log.h"
28 #include "ns3/simulator.h"
29 #include "ns3/node-container.h"
30 #include "ns3/traffic-control-layer.h"
31 #include "ns3/traffic-control-helper.h"
32 #include "ns3/simple-net-device-helper.h"
33 #include "ns3/data-rate.h"
34 #include "ns3/net-device-queue-interface.h"
35 #include "ns3/queue.h"
36 #include "ns3/config.h"
37 
38 using namespace ns3;
39 
47 public:
54  virtual ~QueueDiscTestItem ();
55  virtual void AddHeader (void);
56  virtual bool Mark(void);
57 
58 private:
70  QueueDiscTestItem &operator = (const QueueDiscTestItem &);
71 };
72 
74  : QueueDiscItem (p, Mac48Address (), 0)
75 {
76 }
77 
79 {
80 }
81 
82 void
84 {
85 }
86 
87 bool
89 {
90  return false;
91 }
92 
100 {
101 public:
108  virtual ~TcFlowControlTestCase ();
109 private:
110  virtual void DoRun (void);
116  void SendPackets (Ptr<Node> n, uint16_t nPackets);
123  void CheckPacketsInDeviceQueue (Ptr<NetDevice> dev, uint16_t nPackets, const char* msg);
130  void CheckDeviceQueueStopped (Ptr<NetDevice> dev, bool value, const char* msg);
137  void CheckPacketsInQueueDisc (Ptr<NetDevice> dev, uint16_t nPackets, const char* msg);
139 };
140 
142  : TestCase ("Test the operation of the flow control mechanism"),
143  m_type (tt)
144 {
145 }
146 
148 {
149 }
150 
151 void
153 {
154  Ptr<TrafficControlLayer> tc = n->GetObject<TrafficControlLayer> ();
155  for (uint16_t i = 0; i < nPackets; i++)
156  {
157  tc->Send (n->GetDevice (0), Create<QueueDiscTestItem> (Create<Packet> (1000)));
158  }
159 }
160 
161 void
162 TcFlowControlTestCase::CheckPacketsInDeviceQueue (Ptr<NetDevice> dev, uint16_t nPackets, const char* msg)
163 {
164  PointerValue ptr;
165  dev->GetAttributeFailSafe ("TxQueue", ptr);
166  Ptr<Queue<Packet> > queue = ptr.Get<Queue<Packet> > ();
167  NS_TEST_EXPECT_MSG_EQ (queue->GetNPackets (), nPackets, msg);
168 }
169 
170 void
172 {
174  NS_ASSERT_MSG (ndqi, "A device queue interface has not been aggregated to the device");
175  NS_TEST_EXPECT_MSG_EQ (ndqi->GetTxQueue (0)->IsStopped (), value, msg);
176 }
177 
178 void
179 TcFlowControlTestCase::CheckPacketsInQueueDisc (Ptr<NetDevice> dev, uint16_t nPackets, const char* msg)
180 {
182  Ptr<QueueDisc> qdisc = tc->GetRootQueueDiscOnDevice (dev);
183  NS_TEST_EXPECT_MSG_EQ (qdisc->GetNPackets (), nPackets, msg);
184 }
185 
186 
187 void
189 {
191  n.Create (2);
192 
193  n.Get (0)->AggregateObject (CreateObject<TrafficControlLayer> ());
194  n.Get (1)->AggregateObject (CreateObject<TrafficControlLayer> ());
195 
196  SimpleNetDeviceHelper simple;
197 
198  NetDeviceContainer rxDevC = simple.Install (n.Get (1));
199 
200  simple.SetDeviceAttribute ("DataRate", DataRateValue (DataRate ("1Mb/s")));
201  simple.SetQueue ("ns3::DropTailQueue", "MaxSize",
202  StringValue (m_type == QueueSizeUnit::PACKETS ? "5p" : "5000B"));
203 
204  Ptr<NetDevice> txDev;
205  txDev = simple.Install (n.Get (0), DynamicCast<SimpleChannel> (rxDevC.Get (0)->GetChannel ())).Get (0);
206  txDev->SetMtu (2500);
207 
208  TrafficControlHelper tch = TrafficControlHelper::Default ();
209  tch.Install (txDev);
210 
211  // transmit 10 packets at time 0
212  Simulator::Schedule (Time (Seconds (0)), &TcFlowControlTestCase::SendPackets,
213  this, n.Get (0), 10);
214 
216  {
217  /*
218  * When the device queue is in packet mode, all the packets enqueued in the
219  * queue disc are correctly transmitted, even if the device queue is stopped
220  * when the last packet is received from the upper layers
221  */
222 
223  // The transmission of each packet takes 1000B/1Mbps = 8ms
224  // After 1ms, we have 5 packets in the device queue (stopped) and 4 in the queue disc
226  this, txDev, 5, "There must be 5 packets in the device queue after 1ms");
228  this, txDev, true, "The device queue must be stopped after 1ms");
230  this, txDev, 4, "There must be 4 packets in the queue disc after 1ms");
231 
232  // After 9ms, we have 5 packets in the device queue (stopped) and 3 in the queue disc
234  this, txDev, 5, "There must be 5 packets in the device queue after 9ms");
236  this, txDev, true, "The device queue must be stopped after 9ms");
238  this, txDev, 3, "There must be 3 packets in the queue disc after 9ms");
239 
240  // After 17ms, we have 5 packets in the device queue (stopped) and 2 in the queue disc
242  this, txDev, 5, "There must be 5 packets in the device queue after 17ms");
244  this, txDev, true, "The device queue must be stopped after 17ms");
246  this, txDev, 2, "There must be 2 packets in the queue disc after 17ms");
247 
248  // After 25ms, we have 5 packets in the device queue (stopped) and 1 in the queue disc
250  this, txDev, 5, "There must be 5 packets in the device queue after 25ms");
252  this, txDev, true, "The device queue must be stopped after 25ms");
254  this, txDev, 1, "There must be 1 packet in the queue disc after 25ms");
255 
256  // After 33ms, we have 5 packets in the device queue (stopped) and the queue disc is empty
258  this, txDev, 5, "There must be 5 packets in the device queue after 33ms");
260  this, txDev, true, "The device queue must be stopped after 33ms");
262  this, txDev, 0, "The queue disc must be empty after 33ms");
263 
264  // After 41ms, we have 4 packets in the device queue (not stopped) and the queue disc is empty
266  this, txDev, 4, "There must be 4 packets in the device queue after 41ms");
268  this, txDev, false, "The device queue must not be stopped after 41ms");
270  this, txDev, 0, "The queue disc must be empty after 41ms");
271 
272  // After 81ms, all packets must have been transmitted (the device queue and the queue disc are empty)
274  this, txDev, 0, "The device queue must be empty after 81ms");
276  this, txDev, false, "The device queue must not be stopped after 81ms");
278  this, txDev, 0, "The queue disc must be empty after 81ms");
279  }
280  else
281  {
282  /*
283  * When the device queue is in byte mode, all the packets enqueued in the
284  * queue disc are correctly transmitted, even if the device queue is stopped
285  * when the last packet is received from the upper layers
286  */
287 
288  // The transmission of each packet takes 1000B/1Mbps = 8ms
289  // After 1ms, we have 3 packets in the device queue (stopped) and 6 in the queue disc
291  this, txDev, 3, "There must be 3 packets in the device queue after 1ms");
293  this, txDev, true, "The device queue must be stopped after 1ms");
295  this, txDev, 6, "There must be 6 packets in the queue disc after 1ms");
296 
297  // After 9ms, we have 3 packets in the device queue (stopped) and 5 in the queue disc
299  this, txDev, 3, "There must be 3 packets in the device queue after 9ms");
301  this, txDev, true, "The device queue must be stopped after 9ms");
303  this, txDev, 5, "There must be 5 packets in the queue disc after 9ms");
304 
305  // After 17ms, we have 3 packets in the device queue (stopped) and 4 in the queue disc
307  this, txDev, 3, "There must be 3 packets in the device queue after 17ms");
309  this, txDev, true, "The device queue must be stopped after 17ms");
311  this, txDev, 4, "There must be 4 packets in the queue disc after 17ms");
312 
313  // After 25ms, we have 3 packets in the device queue (stopped) and 3 in the queue disc
315  this, txDev, 3, "There must be 3 packets in the device queue after 25ms");
317  this, txDev, true, "The device queue must be stopped after 25ms");
319  this, txDev, 3, "There must be 3 packets in the queue disc after 25ms");
320 
321  // After 33ms, we have 3 packets in the device queue (stopped) and 2 in the queue disc
323  this, txDev, 3, "There must be 3 packets in the device queue after 33ms");
325  this, txDev, true, "The device queue must be stopped after 33ms");
327  this, txDev, 2, "There must be 2 packets in the queue disc after 33ms");
328 
329  // After 41ms, we have 3 packets in the device queue (stopped) and 1 in the queue disc
331  this, txDev, 3, "There must be 3 packets in the device queue after 41ms");
333  this, txDev, true, "The device queue must be stopped after 41ms");
335  this, txDev, 1, "There must be 1 packet in the queue disc after 41ms");
336 
337  // After 49ms, we have 3 packets in the device queue (stopped) and the queue disc is empty
339  this, txDev, 3, "There must be 3 packets in the device queue after 49ms");
341  this, txDev, true, "The device queue must be stopped after 49ms");
343  this, txDev, 0, "The queue disc must be empty after 49ms");
344 
345  // After 57ms, we have 2 packets in the device queue (not stopped) and the queue disc is empty
347  this, txDev, 2, "There must be 2 packets in the device queue after 57ms");
349  this, txDev, false, "The device queue must not be stopped after 57ms");
351  this, txDev, 0, "The queue disc must be empty after 57ms");
352 
353  // After 81ms, all packets must have been transmitted (the device queue and the queue disc are empty)
355  this, txDev, 0, "The device queue must be empty after 81ms");
357  this, txDev, false, "The device queue must not be stopped after 81ms");
359  this, txDev, 0, "The queue disc must be empty after 81ms");
360  }
361 
362  Simulator::Run ();
363  Simulator::Destroy ();
364 }
365 
372 static class TcFlowControlTestSuite : public TestSuite
373 {
374 public:
376  : TestSuite ("tc-flow-control", UNIT)
377  {
379  AddTestCase (new TcFlowControlTestCase (QueueSizeUnit::BYTES), TestCase::QUICK);
380  }
QueueSizeUnit m_type
the test type
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
QueueDiscContainer Install(NetDeviceContainer c)
void CheckPacketsInDeviceQueue(Ptr< NetDevice > dev, uint16_t nPackets, const char *msg)
Check if the device queue stores the expected number of packets.
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
Hold variables of type string.
Definition: string.h:41
Introspection did not find any typical Config paths.
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition: queue-size.h:42
A suite of tests to run.
Definition: test.h:1342
virtual Ptr< Node > GetNode(void) const =0
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition: test.h:285
QueueDiscItem is the abstract base class for items that are stored in a queue disc.
Definition: queue-item.h:148
encapsulates test code
Definition: test.h:1155
bool GetAttributeFailSafe(std::string name, AttributeValue &value) const
Get the value of an attribute without raising erros.
Definition: object-base.cc:258
void CheckPacketsInQueueDisc(Ptr< NetDevice > dev, uint16_t nPackets, const char *msg)
Check if the queue disc stores the expected number of packets.
Class for representing data rates.
Definition: data-rate.h:88
void AddTestCase(TestCase *testCase, TestDuration duration=QUICK)
Add an individual child TestCase to this test suite.
Definition: test.cc:299
Use number of packets for queue size.
Definition: queue-size.h:44
holds a vector of ns3::NetDevice pointers
void SetDeviceAttribute(std::string n1, const AttributeValue &v1)
Build a set of QueueDisc objects.
Network device transmission queue interface.
void CheckDeviceQueueStopped(Ptr< NetDevice > dev, bool value, const char *msg)
Check if the device queue is in the expected status (stopped or not)
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Hold objects of type Ptr<T>.
Definition: pointer.h:36
virtual void AddHeader(void)
Add the header to the packet.
an EUI-48 address
Definition: mac48-address.h:43
TcFlowControlTestCase(QueueSizeUnit tt)
Constructor.
void SetQueue(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue())
Each net device must have a queue to pass packets through.
virtual void Send(Ptr< NetDevice > device, Ptr< QueueDiscItem > item)
Called from upper layer to queue a packet for the transmission.
virtual bool Mark(void)
Marks the packet as a substitute for dropping it, such as for Explicit Congestion Notification...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
Ptr< T > Get(void) const
Definition: pointer.h:194
TcFlowControlTestSuite g_tcFlowControlTestSuite
the test suite
virtual void DoRun(void)
Implementation to actually run this TestCase.
Queue Disc Test Item.
Traffic Control Flow Control Test Case.
Ptr< NetDeviceQueue > GetTxQueue(std::size_t i) const
Get the i-th transmission queue of the device.
build a set of SimpleNetDevice objects
Traffic Control Flow Control Test Suite.
This test suite implements a Unit Test.
Definition: test.h:1351
virtual bool SetMtu(const uint16_t mtu)=0
void SendPackets(Ptr< Node > n, uint16_t nPackets)
Instruct a node to send a specified number of packets.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::SimpleChannel with the attributes configured by SimpleNetDeviceHelper::Se...
Use number of bytes for queue size.
Definition: queue-size.h:45
virtual Ptr< QueueDisc > GetRootQueueDiscOnDevice(Ptr< NetDevice > device) const
This method can be used to get the root queue disc installed on a device.