A Discrete-Event Network Simulator
API
pie-queue-disc.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016 NITK Surathkal
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  * Authors: Shravya Ks <shravya.ks0@gmail.com>
19  * Smriti Murali <m.smriti.95@gmail.com>
20  * Mohit P. Tahiliani <tahiliani@nitk.edu.in>
21  */
22 
23 /*
24  * PORT NOTE: This code was ported from ns-2.36rc1 (queue/pie.cc).
25  * Most of the comments are also ported from the same.
26  */
27 
28 #include "ns3/log.h"
29 #include "ns3/enum.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/double.h"
32 #include "ns3/simulator.h"
33 #include "ns3/abort.h"
34 #include "pie-queue-disc.h"
35 #include "ns3/drop-tail-queue.h"
36 #include "ns3/net-device-queue-interface.h"
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("PieQueueDisc");
41 
42 NS_OBJECT_ENSURE_REGISTERED (PieQueueDisc);
43 
45 {
46  static TypeId tid = TypeId ("ns3::PieQueueDisc")
47  .SetParent<QueueDisc> ()
48  .SetGroupName ("TrafficControl")
49  .AddConstructor<PieQueueDisc> ()
50  .AddAttribute ("Mode",
51  "Determines unit for QueueLimit",
54  MakeEnumChecker (QUEUE_DISC_MODE_BYTES, "QUEUE_DISC_MODE_BYTES",
55  QUEUE_DISC_MODE_PACKETS, "QUEUE_DISC_MODE_PACKETS"))
56  .AddAttribute ("MeanPktSize",
57  "Average of packet size",
58  UintegerValue (1000),
60  MakeUintegerChecker<uint32_t> ())
61  .AddAttribute ("A",
62  "Value of alpha",
63  DoubleValue (0.125),
65  MakeDoubleChecker<double> ())
66  .AddAttribute ("B",
67  "Value of beta",
68  DoubleValue (1.25),
70  MakeDoubleChecker<double> ())
71  .AddAttribute ("Tupdate",
72  "Time period to calculate drop probability",
73  TimeValue (Seconds (0.03)),
75  MakeTimeChecker ())
76  .AddAttribute ("Supdate",
77  "Start time of the update timer",
78  TimeValue (Seconds (0)),
80  MakeTimeChecker ())
81  .AddAttribute ("QueueLimit",
82  "Queue limit in bytes/packets",
83  UintegerValue (25),
85  MakeUintegerChecker<uint32_t> ())
86  .AddAttribute ("DequeueThreshold",
87  "Minimum queue size in bytes before dequeue rate is measured",
88  UintegerValue (10000),
90  MakeUintegerChecker<uint32_t> ())
91  .AddAttribute ("QueueDelayReference",
92  "Desired queue delay",
93  TimeValue (Seconds (0.02)),
95  MakeTimeChecker ())
96  .AddAttribute ("MaxBurstAllowance",
97  "Current max burst allowance in seconds before random drop",
98  TimeValue (Seconds (0.1)),
100  MakeTimeChecker ())
101  ;
102 
103  return tid;
104 }
105 
107  : QueueDisc ()
108 {
109  NS_LOG_FUNCTION (this);
110  m_uv = CreateObject<UniformRandomVariable> ();
112 }
113 
115 {
116  NS_LOG_FUNCTION (this);
117 }
118 
119 void
121 {
122  NS_LOG_FUNCTION (this);
123  m_uv = 0;
126 }
127 
128 void
130 {
131  NS_LOG_FUNCTION (this << mode);
132  m_mode = mode;
133 }
134 
137 {
138  NS_LOG_FUNCTION (this);
139  return m_mode;
140 }
141 
142 void
144 {
145  NS_LOG_FUNCTION (this << lim);
146  m_queueLimit = lim;
147 }
148 
149 uint32_t
151 {
152  NS_LOG_FUNCTION (this);
153  if (GetMode () == QUEUE_DISC_MODE_BYTES)
154  {
155  return GetInternalQueue (0)->GetNBytes ();
156  }
157  else if (GetMode () == QUEUE_DISC_MODE_PACKETS)
158  {
159  return GetInternalQueue (0)->GetNPackets ();
160  }
161  else
162  {
163  NS_ABORT_MSG ("Unknown PIE mode.");
164  }
165 }
166 
167 Time
169 {
170  NS_LOG_FUNCTION (this);
171  return m_qDelay;
172 }
173 
174 int64_t
176 {
177  NS_LOG_FUNCTION (this << stream);
178  m_uv->SetStream (stream);
179  return 1;
180 }
181 
182 bool
184 {
185  NS_LOG_FUNCTION (this << item);
186 
187  uint32_t nQueued = GetQueueSize ();
188 
189  if ((GetMode () == QUEUE_DISC_MODE_PACKETS && nQueued >= m_queueLimit)
190  || (GetMode () == QUEUE_DISC_MODE_BYTES && nQueued + item->GetSize () > m_queueLimit))
191  {
192  // Drops due to queue limit: reactive
194  return false;
195  }
196  else if (DropEarly (item, nQueued))
197  {
198  // Early probability drop: proactive
200  return false;
201  }
202 
203  // No drop
204  bool retval = GetInternalQueue (0)->Enqueue (item);
205 
206  // If Queue::Enqueue fails, QueueDisc::DropBeforeEnqueue is called by the
207  // internal queue because QueueDisc::AddInternalQueue sets the trace callback
208 
209  NS_LOG_LOGIC ("\t bytesInQueue " << GetInternalQueue (0)->GetNBytes ());
210  NS_LOG_LOGIC ("\t packetsInQueue " << GetInternalQueue (0)->GetNPackets ());
211 
212  return retval;
213 }
214 
215 void
217 {
218  // Initially queue is empty so variables are initialize to zero except m_dqCount
219  m_inMeasurement = false;
220  m_dqCount = -1;
221  m_dropProb = 0;
222  m_avgDqRate = 0.0;
223  m_dqStart = 0;
225  m_qDelayOld = Time (Seconds (0));
226 }
227 
228 bool PieQueueDisc::DropEarly (Ptr<QueueDiscItem> item, uint32_t qSize)
229 {
230  NS_LOG_FUNCTION (this << item << qSize);
231  if (m_burstAllowance.GetSeconds () > 0)
232  {
233  // If there is still burst_allowance left, skip random early drop.
234  return false;
235  }
236 
237  if (m_burstState == NO_BURST)
238  {
241  }
242 
243  double p = m_dropProb;
244 
245  uint32_t packetSize = item->GetSize ();
246 
247  if (GetMode () == QUEUE_DISC_MODE_BYTES)
248  {
249  p = p * packetSize / m_meanPktSize;
250  }
251  bool earlyDrop = true;
252  double u = m_uv->GetValue ();
253 
254  if ((m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb < 0.2))
255  {
256  return false;
257  }
258  else if (GetMode () == QUEUE_DISC_MODE_BYTES && qSize <= 2 * m_meanPktSize)
259  {
260  return false;
261  }
262  else if (GetMode () == QUEUE_DISC_MODE_PACKETS && qSize <= 2)
263  {
264  return false;
265  }
266 
267  if (u > p)
268  {
269  earlyDrop = false;
270  }
271  if (!earlyDrop)
272  {
273  return false;
274  }
275 
276  return true;
277 }
278 
280 {
281  NS_LOG_FUNCTION (this);
282  Time qDelay;
283  double p = 0.0;
284  bool missingInitFlag = false;
285  if (m_avgDqRate > 0)
286  {
287  qDelay = Time (Seconds (GetInternalQueue (0)->GetNBytes () / m_avgDqRate));
288  }
289  else
290  {
291  qDelay = Time (Seconds (0));
292  missingInitFlag = true;
293  }
294 
295  m_qDelay = qDelay;
296 
297  if (m_burstAllowance.GetSeconds () > 0)
298  {
299  m_dropProb = 0;
300  }
301  else
302  {
303  p = m_a * (qDelay.GetSeconds () - m_qDelayRef.GetSeconds ()) + m_b * (qDelay.GetSeconds () - m_qDelayOld.GetSeconds ());
304  if (m_dropProb < 0.001)
305  {
306  p /= 32;
307  }
308  else if (m_dropProb < 0.01)
309  {
310  p /= 8;
311  }
312  else if (m_dropProb < 0.1)
313  {
314  p /= 2;
315  }
316  else if (m_dropProb < 1)
317  {
318  p /= 0.5;
319  }
320  else if (m_dropProb < 10)
321  {
322  p /= 0.125;
323  }
324  else
325  {
326  p /= 0.03125;
327  }
328  if ((m_dropProb >= 0.1) && (p > 0.02))
329  {
330  p = 0.02;
331  }
332  }
333 
334  p += m_dropProb;
335 
336  // For non-linear drop in prob
337 
338  if (qDelay.GetSeconds () == 0 && m_qDelayOld.GetSeconds () == 0)
339  {
340  p *= 0.98;
341  }
342  else if (qDelay.GetSeconds () > 0.2)
343  {
344  p += 0.02;
345  }
346 
347  m_dropProb = (p > 0) ? p : 0;
349  {
350  m_burstAllowance = Time (Seconds (0));
351  }
352  else
353  {
355  }
356 
357  uint32_t burstResetLimit = BURST_RESET_TIMEOUT / m_tUpdate.GetSeconds ();
358  if ( (qDelay.GetSeconds () < 0.5 * m_qDelayRef.GetSeconds ()) && (m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb == 0) && !missingInitFlag )
359  {
360  m_dqCount = -1;
361  m_avgDqRate = 0.0;
362  }
363  if ( (qDelay.GetSeconds () < 0.5 * m_qDelayRef.GetSeconds ()) && (m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb == 0) && (m_burstAllowance.GetSeconds () == 0))
364  {
366  {
368  m_burstReset = 0;
369  }
370  else if (m_burstState == IN_BURST)
371  {
372  m_burstReset++;
373  if (m_burstReset > burstResetLimit)
374  {
375  m_burstReset = 0;
377  }
378  }
379  }
380  else if (m_burstState == IN_BURST)
381  {
382  m_burstReset = 0;
383  }
384 
385  m_qDelayOld = qDelay;
387 }
388 
391 {
392  NS_LOG_FUNCTION (this);
393 
394  if (GetInternalQueue (0)->IsEmpty ())
395  {
396  NS_LOG_LOGIC ("Queue empty");
397  return 0;
398  }
399 
400  Ptr<QueueDiscItem> item = GetInternalQueue (0)->Dequeue ();
401  double now = Simulator::Now ().GetSeconds ();
402  uint32_t pktSize = item->GetSize ();
403 
404  // if not in a measurement cycle and the queue has built up to dq_threshold,
405  // start the measurement cycle
406 
407  if ( (GetInternalQueue (0)->GetNBytes () >= m_dqThreshold) && (!m_inMeasurement) )
408  {
409  m_dqStart = now;
410  m_dqCount = 0;
411  m_inMeasurement = true;
412  }
413 
414  if (m_inMeasurement)
415  {
416  m_dqCount += pktSize;
417 
418  // done with a measurement cycle
419  if (m_dqCount >= m_dqThreshold)
420  {
421 
422  double tmp = now - m_dqStart;
423 
424  if (tmp > 0)
425  {
426  if (m_avgDqRate == 0)
427  {
428  m_avgDqRate = m_dqCount / tmp;
429  }
430  else
431  {
432  m_avgDqRate = (0.5 * m_avgDqRate) + (0.5 * (m_dqCount / tmp));
433  }
434  }
435 
436  // restart a measurement cycle if there is enough data
437  if (GetInternalQueue (0)->GetNBytes () > m_dqThreshold)
438  {
439  m_dqStart = now;
440  m_dqCount = 0;
441  m_inMeasurement = true;
442  }
443  else
444  {
445  m_dqCount = 0;
446  m_inMeasurement = false;
447  }
448  }
449  }
450 
451  return item;
452 }
453 
456 {
457  NS_LOG_FUNCTION (this);
458  if (GetInternalQueue (0)->IsEmpty ())
459  {
460  NS_LOG_LOGIC ("Queue empty");
461  return 0;
462  }
463 
464  Ptr<const QueueDiscItem> item = GetInternalQueue (0)->Peek ();
465 
466  NS_LOG_LOGIC ("Number packets " << GetInternalQueue (0)->GetNPackets ());
467  NS_LOG_LOGIC ("Number bytes " << GetInternalQueue (0)->GetNBytes ());
468 
469  return item;
470 }
471 
472 bool
474 {
475  NS_LOG_FUNCTION (this);
476  if (GetNQueueDiscClasses () > 0)
477  {
478  NS_LOG_ERROR ("PieQueueDisc cannot have classes");
479  return false;
480  }
481 
482  if (GetNPacketFilters () > 0)
483  {
484  NS_LOG_ERROR ("PieQueueDisc cannot have packet filters");
485  return false;
486  }
487 
488  if (GetNInternalQueues () == 0)
489  {
490  // create a DropTail queue
491  Ptr<InternalQueue> queue = CreateObjectWithAttributes<DropTailQueue<QueueDiscItem> > ("Mode", EnumValue (m_mode));
493  {
494  queue->SetMaxPackets (m_queueLimit);
495  }
496  else
497  {
498  queue->SetMaxBytes (m_queueLimit);
499  }
500  AddInternalQueue (queue);
501  }
502 
503  if (GetNInternalQueues () != 1)
504  {
505  NS_LOG_ERROR ("PieQueueDisc needs 1 internal queue");
506  return false;
507  }
508 
511  {
512  NS_LOG_ERROR ("The mode of the provided queue does not match the mode set on the PieQueueDisc");
513  return false;
514  }
515 
516  if ((m_mode == QUEUE_DISC_MODE_PACKETS && GetInternalQueue (0)->GetMaxPackets () != m_queueLimit)
517  || (m_mode == QUEUE_DISC_MODE_BYTES && GetInternalQueue (0)->GetMaxBytes () != m_queueLimit))
518  {
519  NS_LOG_ERROR ("The size of the internal queue differs from the queue disc limit");
520  return false;
521  }
522 
523  return true;
524 }
525 
526 } //namespace ns3
QueueDiscMode GetMode(void)
Get the operating mode of this queue disc.
uint32_t m_dqCount
Number of bytes departed since current measurement cycle starts.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Time m_tUpdate
Time period after which CalculateP () is called.
uint32_t GetNQueueDiscClasses(void) const
Get the number of queue disc classes.
Definition: queue-disc.cc:557
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
void SetMode(QueueDiscMode mode)
Set the operating mode of this queue disc.
void DropBeforeEnqueue(Ptr< const QueueDiscItem > item, const char *reason)
Perform the actions required when the queue disc is notified of a packet dropped before enqueue...
Definition: queue-disc.cc:609
QueueDiscMode m_mode
Mode (bytes or packets)
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
double m_avgDqRate
Time averaged dequeue rate.
Time m_maxBurst
Maximum burst allowed before random early dropping kicks in.
virtual Ptr< const QueueDiscItem > DoPeek(void) const
This function returns a copy of the next packet the queue disc will extract.
double m_a
Parameter to pie controller.
uint32_t GetNBytes(void) const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:437
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: enum.h:209
static constexpr const char * FORCED_DROP
Drops due to queue limit: reactive.
virtual void InitializeParams(void)
Initialize the queue parameters.
Use number of packets for maximum queue disc size.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t m_burstReset
Used to reset value of burst allowance.
static constexpr const char * UNFORCED_DROP
Early probability drops: proactive.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:719
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:151
Implements PIE Active Queue Management discipline.
Time m_qDelayRef
Desired queue delay.
Use number of packets for maximum queue size.
Definition: queue.h:166
double m_dropProb
Variable used in calculation of drop probability.
uint32_t m_meanPktSize
Average packet size in bytes.
void SetQueueLimit(uint32_t lim)
Set the limit of the queue in bytes or packets.
Ptr< InternalQueue > GetInternalQueue(uint32_t i) const
Get the i-th internal queue.
Definition: queue-disc.cc:492
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
uint32_t GetNInternalQueues(void) const
Get the number of internal queues.
Definition: queue-disc.cc:499
void AddInternalQueue(Ptr< InternalQueue > queue)
Add an internal queue to the tail of the list of queues.
Definition: queue-disc.cc:472
Time GetQueueDelay(void)
Get queue delay.
Hold variables of type enum.
Definition: enum.h:54
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
virtual bool CheckConfig(void)
Check whether the current configuration is correct.
AttributeValue implementation for Time.
Definition: nstime.h:1055
Hold an unsigned integer type.
Definition: uinteger.h:44
Time m_sUpdate
Start time of the update timer.
uint32_t m_dqThreshold
Minimum queue size in bytes before dequeue rate is measured.
#define BURST_RESET_TIMEOUT
virtual void DoDispose(void)
Dispose of the object.
Definition: queue-disc.cc:371
Time m_qDelayOld
Old value of queue delay.
uint32_t GetNPacketFilters(void) const
Get the number of packet filters.
Definition: queue-disc.cc:519
double m_dqStart
Start timestamp of current measurement cycle.
bool DropEarly(Ptr< QueueDiscItem > item, uint32_t qSize)
Check if a packet needs to be dropped due to probability drop.
virtual void DoDispose(void)
Dispose of the object.
QueueDiscMode
Enumeration of the modes supported in the class.
void CalculateP()
Periodically update the drop probability based on the delay samples: not only the current delay sampl...
static void Remove(const EventId &id)
Remove an event from the event list.
Definition: simulator.cc:336
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
uint32_t GetQueueSize(void)
Get the current value of the queue in bytes or packets.
EventId m_rtrsEvent
Event used to decide the decision of interval of drop probability calculation.
Use number of bytes for maximum queue size.
Definition: queue.h:167
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:1056
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
Ptr< UniformRandomVariable > m_uv
Rng stream.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
PieQueueDisc()
PieQueueDisc Constructor.
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: double.h:42
virtual Ptr< QueueDiscItem > DoDequeue(void)
This function actually extracts a packet from the queue disc.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
Time m_qDelay
Current value of queue delay.
static TypeId GetTypeId(void)
Get the type ID.
BurstStateT m_burstState
Used to determine the current state of burst.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
Time m_burstAllowance
Current max burst value in seconds that is allowed before random drops kick in.
virtual ~PieQueueDisc()
PieQueueDisc Destructor.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:253
static const uint32_t packetSize
virtual bool DoEnqueue(Ptr< QueueDiscItem > item)
This function actually enqueues a packet into the queue disc.
uint32_t m_queueLimit
Queue limit in bytes / packets.
Use number of bytes for maximum queue disc size.
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
double m_b
Parameter to pie controller.
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: uinteger.h:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
uint32_t GetNPackets(void) const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:430
bool m_inMeasurement
Indicates whether we are in a measurement cycle.