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",
55  MakeEnumChecker (QUEUE_DISC_MODE_BYTES, "QUEUE_DISC_MODE_BYTES",
56  QUEUE_DISC_MODE_PACKETS, "QUEUE_DISC_MODE_PACKETS"),
58  "Use the MaxSize attribute instead")
59  .AddAttribute ("MeanPktSize",
60  "Average of packet size",
61  UintegerValue (1000),
63  MakeUintegerChecker<uint32_t> ())
64  .AddAttribute ("A",
65  "Value of alpha",
66  DoubleValue (0.125),
68  MakeDoubleChecker<double> ())
69  .AddAttribute ("B",
70  "Value of beta",
71  DoubleValue (1.25),
73  MakeDoubleChecker<double> ())
74  .AddAttribute ("Tupdate",
75  "Time period to calculate drop probability",
76  TimeValue (Seconds (0.03)),
78  MakeTimeChecker ())
79  .AddAttribute ("Supdate",
80  "Start time of the update timer",
81  TimeValue (Seconds (0)),
83  MakeTimeChecker ())
84  .AddAttribute ("QueueLimit",
85  "Queue limit in bytes/packets",
86  UintegerValue (25),
88  MakeUintegerChecker<uint32_t> (),
90  "Use the MaxSize attribute instead")
91  .AddAttribute ("MaxSize",
92  "The maximum number of packets accepted by this queue disc",
93  QueueSizeValue (QueueSize ("0p")),
97  .AddAttribute ("DequeueThreshold",
98  "Minimum queue size in bytes before dequeue rate is measured",
99  UintegerValue (10000),
101  MakeUintegerChecker<uint32_t> ())
102  .AddAttribute ("QueueDelayReference",
103  "Desired queue delay",
104  TimeValue (Seconds (0.02)),
106  MakeTimeChecker ())
107  .AddAttribute ("MaxBurstAllowance",
108  "Current max burst allowance in seconds before random drop",
109  TimeValue (Seconds (0.1)),
111  MakeTimeChecker ())
112  ;
113 
114  return tid;
115 }
116 
119 {
120  NS_LOG_FUNCTION (this);
121  m_uv = CreateObject<UniformRandomVariable> ();
123 }
124 
126 {
127  NS_LOG_FUNCTION (this);
128 }
129 
130 void
132 {
133  NS_LOG_FUNCTION (this);
134  m_uv = 0;
137 }
138 
139 void
141 {
142  NS_LOG_FUNCTION (this << mode);
143 
144  if (mode == QUEUE_DISC_MODE_BYTES)
145  {
147  }
148  else if (mode == QUEUE_DISC_MODE_PACKETS)
149  {
151  }
152  else
153  {
154  NS_ABORT_MSG ("Unknown queue size unit");
155  }
156 }
157 
160 {
161  NS_LOG_FUNCTION (this);
163 }
164 
165 void
167 {
168  NS_LOG_FUNCTION (this << lim);
169  SetMaxSize (QueueSize (GetMaxSize ().GetUnit (), lim));
170 }
171 
172 uint32_t
174 {
175  NS_LOG_FUNCTION (this);
176  if (GetMode () == QUEUE_DISC_MODE_BYTES)
177  {
178  return GetInternalQueue (0)->GetNBytes ();
179  }
180  else if (GetMode () == QUEUE_DISC_MODE_PACKETS)
181  {
182  return GetInternalQueue (0)->GetNPackets ();
183  }
184  else
185  {
186  NS_ABORT_MSG ("Unknown PIE mode.");
187  }
188 }
189 
190 Time
192 {
193  NS_LOG_FUNCTION (this);
194  return m_qDelay;
195 }
196 
197 int64_t
199 {
200  NS_LOG_FUNCTION (this << stream);
201  m_uv->SetStream (stream);
202  return 1;
203 }
204 
205 bool
207 {
208  NS_LOG_FUNCTION (this << item);
209 
210  QueueSize nQueued = GetCurrentSize ();
211 
212  if (nQueued + item > GetMaxSize ())
213  {
214  // Drops due to queue limit: reactive
216  return false;
217  }
218  else if (DropEarly (item, nQueued.GetValue ()))
219  {
220  // Early probability drop: proactive
222  return false;
223  }
224 
225  // No drop
226  bool retval = GetInternalQueue (0)->Enqueue (item);
227 
228  // If Queue::Enqueue fails, QueueDisc::DropBeforeEnqueue is called by the
229  // internal queue because QueueDisc::AddInternalQueue sets the trace callback
230 
231  NS_LOG_LOGIC ("\t bytesInQueue " << GetInternalQueue (0)->GetNBytes ());
232  NS_LOG_LOGIC ("\t packetsInQueue " << GetInternalQueue (0)->GetNPackets ());
233 
234  return retval;
235 }
236 
237 void
239 {
240  // Initially queue is empty so variables are initialize to zero except m_dqCount
241  m_inMeasurement = false;
243  m_dropProb = 0;
244  m_avgDqRate = 0.0;
245  m_dqStart = 0;
247  m_qDelayOld = Time (Seconds (0));
248 }
249 
250 bool PieQueueDisc::DropEarly (Ptr<QueueDiscItem> item, uint32_t qSize)
251 {
252  NS_LOG_FUNCTION (this << item << qSize);
253  if (m_burstAllowance.GetSeconds () > 0)
254  {
255  // If there is still burst_allowance left, skip random early drop.
256  return false;
257  }
258 
259  if (m_burstState == NO_BURST)
260  {
263  }
264 
265  double p = m_dropProb;
266 
267  uint32_t packetSize = item->GetSize ();
268 
269  if (GetMode () == QUEUE_DISC_MODE_BYTES)
270  {
271  p = p * packetSize / m_meanPktSize;
272  }
273  bool earlyDrop = true;
274  double u = m_uv->GetValue ();
275 
276  if ((m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb < 0.2))
277  {
278  return false;
279  }
280  else if (GetMode () == QUEUE_DISC_MODE_BYTES && qSize <= 2 * m_meanPktSize)
281  {
282  return false;
283  }
284  else if (GetMode () == QUEUE_DISC_MODE_PACKETS && qSize <= 2)
285  {
286  return false;
287  }
288 
289  if (u > p)
290  {
291  earlyDrop = false;
292  }
293  if (!earlyDrop)
294  {
295  return false;
296  }
297 
298  return true;
299 }
300 
302 {
303  NS_LOG_FUNCTION (this);
304  Time qDelay;
305  double p = 0.0;
306  bool missingInitFlag = false;
307  if (m_avgDqRate > 0)
308  {
309  qDelay = Time (Seconds (GetInternalQueue (0)->GetNBytes () / m_avgDqRate));
310  }
311  else
312  {
313  qDelay = Time (Seconds (0));
314  missingInitFlag = true;
315  }
316 
317  m_qDelay = qDelay;
318 
319  if (m_burstAllowance.GetSeconds () > 0)
320  {
321  m_dropProb = 0;
322  }
323  else
324  {
325  p = m_a * (qDelay.GetSeconds () - m_qDelayRef.GetSeconds ()) + m_b * (qDelay.GetSeconds () - m_qDelayOld.GetSeconds ());
326  if (m_dropProb < 0.001)
327  {
328  p /= 32;
329  }
330  else if (m_dropProb < 0.01)
331  {
332  p /= 8;
333  }
334  else if (m_dropProb < 0.1)
335  {
336  p /= 2;
337  }
338  else if (m_dropProb < 1)
339  {
340  p /= 0.5;
341  }
342  else if (m_dropProb < 10)
343  {
344  p /= 0.125;
345  }
346  else
347  {
348  p /= 0.03125;
349  }
350  if ((m_dropProb >= 0.1) && (p > 0.02))
351  {
352  p = 0.02;
353  }
354  }
355 
356  p += m_dropProb;
357 
358  // For non-linear drop in prob
359 
360  if (qDelay.GetSeconds () == 0 && m_qDelayOld.GetSeconds () == 0)
361  {
362  p *= 0.98;
363  }
364  else if (qDelay.GetSeconds () > 0.2)
365  {
366  p += 0.02;
367  }
368 
369  m_dropProb = (p > 0) ? p : 0;
371  {
372  m_burstAllowance = Time (Seconds (0));
373  }
374  else
375  {
377  }
378 
379  uint32_t burstResetLimit = static_cast<uint32_t>(BURST_RESET_TIMEOUT / m_tUpdate.GetSeconds ());
380  if ( (qDelay.GetSeconds () < 0.5 * m_qDelayRef.GetSeconds ()) && (m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb == 0) && !missingInitFlag )
381  {
383  m_avgDqRate = 0.0;
384  }
385  if ( (qDelay.GetSeconds () < 0.5 * m_qDelayRef.GetSeconds ()) && (m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb == 0) && (m_burstAllowance.GetSeconds () == 0))
386  {
388  {
390  m_burstReset = 0;
391  }
392  else if (m_burstState == IN_BURST)
393  {
394  m_burstReset++;
395  if (m_burstReset > burstResetLimit)
396  {
397  m_burstReset = 0;
399  }
400  }
401  }
402  else if (m_burstState == IN_BURST)
403  {
404  m_burstReset = 0;
405  }
406 
407  m_qDelayOld = qDelay;
409 }
410 
413 {
414  NS_LOG_FUNCTION (this);
415 
416  if (GetInternalQueue (0)->IsEmpty ())
417  {
418  NS_LOG_LOGIC ("Queue empty");
419  return 0;
420  }
421 
422  Ptr<QueueDiscItem> item = GetInternalQueue (0)->Dequeue ();
423  double now = Simulator::Now ().GetSeconds ();
424  uint32_t pktSize = item->GetSize ();
425 
426  // if not in a measurement cycle and the queue has built up to dq_threshold,
427  // start the measurement cycle
428 
429  if ( (GetInternalQueue (0)->GetNBytes () >= m_dqThreshold) && (!m_inMeasurement) )
430  {
431  m_dqStart = now;
432  m_dqCount = 0;
433  m_inMeasurement = true;
434  }
435 
436  if (m_inMeasurement)
437  {
438  m_dqCount += pktSize;
439 
440  // done with a measurement cycle
441  if (m_dqCount >= m_dqThreshold)
442  {
443 
444  double tmp = now - m_dqStart;
445 
446  if (tmp > 0)
447  {
448  if (m_avgDqRate == 0)
449  {
450  m_avgDqRate = m_dqCount / tmp;
451  }
452  else
453  {
454  m_avgDqRate = (0.5 * m_avgDqRate) + (0.5 * (m_dqCount / tmp));
455  }
456  }
457 
458  // restart a measurement cycle if there is enough data
459  if (GetInternalQueue (0)->GetNBytes () > m_dqThreshold)
460  {
461  m_dqStart = now;
462  m_dqCount = 0;
463  m_inMeasurement = true;
464  }
465  else
466  {
467  m_dqCount = 0;
468  m_inMeasurement = false;
469  }
470  }
471  }
472 
473  return item;
474 }
475 
478 {
479  NS_LOG_FUNCTION (this);
480 
482 
483  if (!item)
484  {
485  NS_LOG_LOGIC ("Queue empty");
486  return 0;
487  }
488 
489  NS_LOG_LOGIC ("Number packets " << GetInternalQueue (0)->GetNPackets ());
490  NS_LOG_LOGIC ("Number bytes " << GetInternalQueue (0)->GetNBytes ());
491 
492  return item;
493 }
494 
495 bool
497 {
498  NS_LOG_FUNCTION (this);
499  if (GetNQueueDiscClasses () > 0)
500  {
501  NS_LOG_ERROR ("PieQueueDisc cannot have classes");
502  return false;
503  }
504 
505  if (GetNPacketFilters () > 0)
506  {
507  NS_LOG_ERROR ("PieQueueDisc cannot have packet filters");
508  return false;
509  }
510 
511  if (GetNInternalQueues () == 0)
512  {
513  // add a DropTail queue
515  ("MaxSize", QueueSizeValue (GetMaxSize ())));
516  }
517 
518  if (GetNInternalQueues () != 1)
519  {
520  NS_LOG_ERROR ("PieQueueDisc needs 1 internal queue");
521  return false;
522  }
523 
524  return true;
525 }
526 
527 } //namespace ns3
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:652
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.
Class for representing queue sizes.
Definition: queue-size.h:94
#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:704
#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.
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:447
QueueSize GetCurrentSize(void)
Get the current size of the queue disc in bytes, if operating in bytes mode, or packets, otherwise.
Definition: queue-disc.cc:523
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
QueueDiscMode GetMode(void) const
Get the operating mode of this queue disc.
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:733
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:181
Implements PIE Active Queue Management discipline.
Time m_qDelayRef
Desired queue delay.
uint32_t GetValue() const
Get the underlying value.
Definition: queue-size.cc:175
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:587
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:355
uint32_t GetNInternalQueues(void) const
Get the number of internal queues.
Definition: queue-disc.cc:594
void AddInternalQueue(Ptr< InternalQueue > queue)
Add an internal queue to the tail of the list of queues.
Definition: queue-disc.cc:567
Time GetQueueDelay(void)
Get queue delay.
Ptr< T > CreateObjectWithAttributes(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(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
Allocate an Object on the heap and initialize with a set of attributes.
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:1069
Hold an unsigned integer type.
Definition: uinteger.h:44
Use number of packets for queue size.
Definition: queue-size.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
Ptr< const AttributeAccessor > MakeQueueSizeAccessor(T1 a1)
Definition: queue-size.h:221
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:72
virtual void DoDispose(void)
Dispose of the object.
Definition: queue-disc.cc:381
Time m_qDelayOld
Old value of queue delay.
uint32_t GetNPacketFilters(void) const
Get the number of packet filters.
Definition: queue-disc.cc:614
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.
Introspection did not find any typical Config paths.
Ptr< const AttributeChecker > MakeQueueSizeChecker(void)
Definition: queue-size.cc:29
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.
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:1070
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.
QueueDiscSizePolicy
Enumeration of the available policies to handle the queue disc size.
Definition: queue-disc.h:103
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
Ptr< const QueueDiscItem > PeekDequeued(void)
Dequeue a packet and retain it in the queue disc as a requeued packet.
Definition: queue-disc.cc:880
Used by queue discs with single internal queue.
Definition: queue-disc.h:105
virtual Ptr< const QueueDiscItem > DoPeek(void)
This function returns a copy of the next packet the queue disc will extract.
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:1007
Time m_burstAllowance
Current max burst value in seconds that is allowed before random drops kick in.
bool SetMaxSize(QueueSize size)
Set the maximum size of the queue disc.
Definition: queue-disc.cc:482
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.
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
Use number of bytes for queue size.
Definition: queue-size.h:45
uint64_t m_dqCount
Number of bytes departed since current measurement cycle starts.
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
static const uint64_t DQCOUNT_INVALID
Invalid dqCount value.
QueueSize GetMaxSize(void) const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:454
uint32_t GetNPackets(void) const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:440
bool m_inMeasurement
Indicates whether we are in a measurement cycle.