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.h"
33 #include "ns3/simple-channel.h"
34 #include "ns3/drop-tail-queue.h"
35 #include "ns3/net-device-queue-interface.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_TEST_EXPECT_MSG_EQ (ndqi->GetTxQueue (0)->IsStopped (), value, msg);
175 }
176 
177 void
178 TcFlowControlTestCase::CheckPacketsInQueueDisc (Ptr<NetDevice> dev, uint16_t nPackets, const char* msg)
179 {
181  Ptr<QueueDisc> qdisc = tc->GetRootQueueDiscOnDevice (dev);
182  NS_TEST_EXPECT_MSG_EQ (qdisc->GetNPackets (), nPackets, msg);
183 }
184 
185 
186 void
188 {
190  n.Create (2);
191 
192  n.Get (0)->AggregateObject (CreateObject<TrafficControlLayer> ());
193  n.Get (1)->AggregateObject (CreateObject<TrafficControlLayer> ());
194 
195  Ptr<Queue<Packet> > queue;
196 
198  {
199  queue = CreateObjectWithAttributes<DropTailQueue<Packet> > ("MaxSize", StringValue ("5p"));
200  }
201  else
202  {
203  queue = CreateObjectWithAttributes<DropTailQueue<Packet> > ("MaxSize", StringValue ("5000B"));
204  }
205 
206  // link the two nodes
207  Ptr<SimpleNetDevice> txDev, rxDev;
208  txDev = CreateObjectWithAttributes<SimpleNetDevice> ("TxQueue", PointerValue (queue),
209  "DataRate", DataRateValue (DataRate ("1Mb/s")));
210  rxDev = CreateObject<SimpleNetDevice> ();
211  n.Get (0)->AddDevice (txDev);
212  n.Get (1)->AddDevice (rxDev);
213  Ptr<SimpleChannel> channel1 = CreateObject<SimpleChannel> ();
214  txDev->SetChannel (channel1);
215  rxDev->SetChannel (channel1);
216 
217  txDev->SetMtu (2500);
218 
219  TrafficControlHelper tch = TrafficControlHelper::Default ();
220  tch.Install (txDev);
221 
222  // transmit 10 packets at time 0
223  Simulator::Schedule (Time (Seconds (0)), &TcFlowControlTestCase::SendPackets,
224  this, n.Get (0), 10);
225 
227  {
228  /*
229  * When the device queue is in packet mode, all the packets enqueued in the
230  * queue disc are correctly transmitted, even if the device queue is stopped
231  * when the last packet is received from the upper layers
232  */
233 
234  // The transmission of each packet takes 1000B/1Mbps = 8ms
235  // After 1ms, we have 5 packets in the device queue (stopped) and 4 in the queue disc
237  this, txDev, 5, "There must be 5 packets in the device queue after 1ms");
239  this, txDev, true, "The device queue must be stopped after 1ms");
241  this, txDev, 4, "There must be 4 packets in the queue disc after 1ms");
242 
243  // After 9ms, we have 5 packets in the device queue (stopped) and 3 in the queue disc
245  this, txDev, 5, "There must be 5 packets in the device queue after 9ms");
247  this, txDev, true, "The device queue must be stopped after 9ms");
249  this, txDev, 3, "There must be 3 packets in the queue disc after 9ms");
250 
251  // After 17ms, we have 5 packets in the device queue (stopped) and 2 in the queue disc
253  this, txDev, 5, "There must be 5 packets in the device queue after 17ms");
255  this, txDev, true, "The device queue must be stopped after 17ms");
257  this, txDev, 2, "There must be 2 packets in the queue disc after 17ms");
258 
259  // After 25ms, we have 5 packets in the device queue (stopped) and 1 in the queue disc
261  this, txDev, 5, "There must be 5 packets in the device queue after 25ms");
263  this, txDev, true, "The device queue must be stopped after 25ms");
265  this, txDev, 1, "There must be 1 packet in the queue disc after 25ms");
266 
267  // After 33ms, we have 5 packets in the device queue (stopped) and the queue disc is empty
269  this, txDev, 5, "There must be 5 packets in the device queue after 33ms");
271  this, txDev, true, "The device queue must be stopped after 33ms");
273  this, txDev, 0, "The queue disc must be empty after 33ms");
274 
275  // After 41ms, we have 4 packets in the device queue (not stopped) and the queue disc is empty
277  this, txDev, 4, "There must be 4 packets in the device queue after 41ms");
279  this, txDev, false, "The device queue must not be stopped after 41ms");
281  this, txDev, 0, "The queue disc must be empty after 41ms");
282 
283  // After 81ms, all packets must have been transmitted (the device queue and the queue disc are empty)
285  this, txDev, 0, "The device queue must be empty after 81ms");
287  this, txDev, false, "The device queue must not be stopped after 81ms");
289  this, txDev, 0, "The queue disc must be empty after 81ms");
290  }
291  else
292  {
293  /*
294  * When the device queue is in byte mode, all the packets enqueued in the
295  * queue disc are correctly transmitted, even if the device queue is stopped
296  * when the last packet is received from the upper layers
297  */
298 
299  // The transmission of each packet takes 1000B/1Mbps = 8ms
300  // After 1ms, we have 3 packets in the device queue (stopped) and 6 in the queue disc
302  this, txDev, 3, "There must be 3 packets in the device queue after 1ms");
304  this, txDev, true, "The device queue must be stopped after 1ms");
306  this, txDev, 6, "There must be 6 packets in the queue disc after 1ms");
307 
308  // After 9ms, we have 3 packets in the device queue (stopped) and 5 in the queue disc
310  this, txDev, 3, "There must be 3 packets in the device queue after 9ms");
312  this, txDev, true, "The device queue must be stopped after 9ms");
314  this, txDev, 5, "There must be 5 packets in the queue disc after 9ms");
315 
316  // After 17ms, we have 3 packets in the device queue (stopped) and 4 in the queue disc
318  this, txDev, 3, "There must be 3 packets in the device queue after 17ms");
320  this, txDev, true, "The device queue must be stopped after 17ms");
322  this, txDev, 4, "There must be 4 packets in the queue disc after 17ms");
323 
324  // After 25ms, we have 3 packets in the device queue (stopped) and 3 in the queue disc
326  this, txDev, 3, "There must be 3 packets in the device queue after 25ms");
328  this, txDev, true, "The device queue must be stopped after 25ms");
330  this, txDev, 3, "There must be 3 packets in the queue disc after 25ms");
331 
332  // After 33ms, we have 3 packets in the device queue (stopped) and 2 in the queue disc
334  this, txDev, 3, "There must be 3 packets in the device queue after 33ms");
336  this, txDev, true, "The device queue must be stopped after 33ms");
338  this, txDev, 2, "There must be 2 packets in the queue disc after 33ms");
339 
340  // After 41ms, we have 3 packets in the device queue (stopped) and 1 in the queue disc
342  this, txDev, 3, "There must be 3 packets in the device queue after 41ms");
344  this, txDev, true, "The device queue must be stopped after 41ms");
346  this, txDev, 1, "There must be 1 packet in the queue disc after 41ms");
347 
348  // After 49ms, we have 3 packets in the device queue (stopped) and the queue disc is empty
350  this, txDev, 3, "There must be 3 packets in the device queue after 49ms");
352  this, txDev, true, "The device queue must be stopped after 49ms");
354  this, txDev, 0, "The queue disc must be empty after 49ms");
355 
356  // After 57ms, we have 2 packets in the device queue (not stopped) and the queue disc is empty
358  this, txDev, 2, "There must be 2 packets in the device queue after 57ms");
360  this, txDev, false, "The device queue must not be stopped after 57ms");
362  this, txDev, 0, "The queue disc must be empty after 57ms");
363 
364  // After 81ms, all packets must have been transmitted (the device queue and the queue disc are empty)
366  this, txDev, 0, "The device queue must be empty after 81ms");
368  this, txDev, false, "The device queue must not be stopped after 81ms");
370  this, txDev, 0, "The queue disc must be empty after 81ms");
371  }
372 
373  Simulator::Run ();
374  Simulator::Destroy ();
375 }
376 
383 static class TcFlowControlTestSuite : public TestSuite
384 {
385 public:
387  : TestSuite ("tc-flow-control", UNIT)
388  {
390  AddTestCase (new TcFlowControlTestCase (QueueSizeUnit::BYTES), TestCase::QUICK);
391  }
QueueSizeUnit m_type
the test type
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.
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:1022
#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
virtual bool SetMtu(const uint16_t mtu)
Use number of packets for queue size.
Definition: queue-size.h:44
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.
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:1014
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(uint8_t i) const
Get the i-th transmission queue of the device.
Traffic Control Flow Control Test Suite.
This test suite implements a Unit Test.
Definition: test.h:1352
void SendPackets(Ptr< Node > n, uint16_t nPackets)
Instruct a node to send a specified number of packets.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
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.