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 ("MeanPktSize",
51  "Average of packet size",
52  UintegerValue (1000),
54  MakeUintegerChecker<uint32_t> ())
55  .AddAttribute ("A",
56  "Value of alpha",
57  DoubleValue (0.125),
59  MakeDoubleChecker<double> ())
60  .AddAttribute ("B",
61  "Value of beta",
62  DoubleValue (1.25),
64  MakeDoubleChecker<double> ())
65  .AddAttribute ("Tupdate",
66  "Time period to calculate drop probability",
67  TimeValue (Seconds (0.03)),
69  MakeTimeChecker ())
70  .AddAttribute ("Supdate",
71  "Start time of the update timer",
72  TimeValue (Seconds (0)),
74  MakeTimeChecker ())
75  .AddAttribute ("MaxSize",
76  "The maximum number of packets accepted by this queue disc",
77  QueueSizeValue (QueueSize ("25p")),
81  .AddAttribute ("DequeueThreshold",
82  "Minimum queue size in bytes before dequeue rate is measured",
83  UintegerValue (10000),
85  MakeUintegerChecker<uint32_t> ())
86  .AddAttribute ("QueueDelayReference",
87  "Desired queue delay",
88  TimeValue (Seconds (0.02)),
90  MakeTimeChecker ())
91  .AddAttribute ("MaxBurstAllowance",
92  "Current max burst allowance in seconds before random drop",
93  TimeValue (Seconds (0.1)),
95  MakeTimeChecker ())
96  ;
97 
98  return tid;
99 }
100 
103 {
104  NS_LOG_FUNCTION (this);
105  m_uv = CreateObject<UniformRandomVariable> ();
107 }
108 
110 {
111  NS_LOG_FUNCTION (this);
112 }
113 
114 void
116 {
117  NS_LOG_FUNCTION (this);
118  m_uv = 0;
121 }
122 
123 Time
125 {
126  NS_LOG_FUNCTION (this);
127  return m_qDelay;
128 }
129 
130 int64_t
132 {
133  NS_LOG_FUNCTION (this << stream);
134  m_uv->SetStream (stream);
135  return 1;
136 }
137 
138 bool
140 {
141  NS_LOG_FUNCTION (this << item);
142 
143  QueueSize nQueued = GetCurrentSize ();
144 
145  if (nQueued + item > GetMaxSize ())
146  {
147  // Drops due to queue limit: reactive
149  return false;
150  }
151  else if (DropEarly (item, nQueued.GetValue ()))
152  {
153  // Early probability drop: proactive
155  return false;
156  }
157 
158  // No drop
159  bool retval = GetInternalQueue (0)->Enqueue (item);
160 
161  // If Queue::Enqueue fails, QueueDisc::DropBeforeEnqueue is called by the
162  // internal queue because QueueDisc::AddInternalQueue sets the trace callback
163 
164  NS_LOG_LOGIC ("\t bytesInQueue " << GetInternalQueue (0)->GetNBytes ());
165  NS_LOG_LOGIC ("\t packetsInQueue " << GetInternalQueue (0)->GetNPackets ());
166 
167  return retval;
168 }
169 
170 void
172 {
173  // Initially queue is empty so variables are initialize to zero except m_dqCount
174  m_inMeasurement = false;
176  m_dropProb = 0;
177  m_avgDqRate = 0.0;
178  m_dqStart = 0;
180  m_qDelayOld = Time (Seconds (0));
181 }
182 
183 bool PieQueueDisc::DropEarly (Ptr<QueueDiscItem> item, uint32_t qSize)
184 {
185  NS_LOG_FUNCTION (this << item << qSize);
186  if (m_burstAllowance.GetSeconds () > 0)
187  {
188  // If there is still burst_allowance left, skip random early drop.
189  return false;
190  }
191 
192  if (m_burstState == NO_BURST)
193  {
196  }
197 
198  double p = m_dropProb;
199 
200  uint32_t packetSize = item->GetSize ();
201 
202  if (GetMaxSize ().GetUnit () == QueueSizeUnit::BYTES)
203  {
204  p = p * packetSize / m_meanPktSize;
205  }
206  bool earlyDrop = true;
207  double u = m_uv->GetValue ();
208 
209  if ((m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb < 0.2))
210  {
211  return false;
212  }
213  else if (GetMaxSize ().GetUnit () == QueueSizeUnit::BYTES && qSize <= 2 * m_meanPktSize)
214  {
215  return false;
216  }
217  else if (GetMaxSize ().GetUnit () == QueueSizeUnit::PACKETS && qSize <= 2)
218  {
219  return false;
220  }
221 
222  if (u > p)
223  {
224  earlyDrop = false;
225  }
226  if (!earlyDrop)
227  {
228  return false;
229  }
230 
231  return true;
232 }
233 
235 {
236  NS_LOG_FUNCTION (this);
237  Time qDelay;
238  double p = 0.0;
239  bool missingInitFlag = false;
240  if (m_avgDqRate > 0)
241  {
242  qDelay = Time (Seconds (GetInternalQueue (0)->GetNBytes () / m_avgDqRate));
243  }
244  else
245  {
246  qDelay = Time (Seconds (0));
247  missingInitFlag = true;
248  }
249 
250  m_qDelay = qDelay;
251 
252  if (m_burstAllowance.GetSeconds () > 0)
253  {
254  m_dropProb = 0;
255  }
256  else
257  {
258  p = m_a * (qDelay.GetSeconds () - m_qDelayRef.GetSeconds ()) + m_b * (qDelay.GetSeconds () - m_qDelayOld.GetSeconds ());
259  if (m_dropProb < 0.001)
260  {
261  p /= 32;
262  }
263  else if (m_dropProb < 0.01)
264  {
265  p /= 8;
266  }
267  else if (m_dropProb < 0.1)
268  {
269  p /= 2;
270  }
271  else if (m_dropProb < 1)
272  {
273  p /= 0.5;
274  }
275  else if (m_dropProb < 10)
276  {
277  p /= 0.125;
278  }
279  else
280  {
281  p /= 0.03125;
282  }
283  if ((m_dropProb >= 0.1) && (p > 0.02))
284  {
285  p = 0.02;
286  }
287  }
288 
289  p += m_dropProb;
290 
291  // For non-linear drop in prob
292 
293  if (qDelay.GetSeconds () == 0 && m_qDelayOld.GetSeconds () == 0)
294  {
295  p *= 0.98;
296  }
297  else if (qDelay.GetSeconds () > 0.2)
298  {
299  p += 0.02;
300  }
301 
302  m_dropProb = (p > 0) ? p : 0;
304  {
305  m_burstAllowance = Time (Seconds (0));
306  }
307  else
308  {
310  }
311 
312  uint32_t burstResetLimit = static_cast<uint32_t>(BURST_RESET_TIMEOUT / m_tUpdate.GetSeconds ());
313  if ( (qDelay.GetSeconds () < 0.5 * m_qDelayRef.GetSeconds ()) && (m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb == 0) && !missingInitFlag )
314  {
316  m_avgDqRate = 0.0;
317  }
318  if ( (qDelay.GetSeconds () < 0.5 * m_qDelayRef.GetSeconds ()) && (m_qDelayOld.GetSeconds () < (0.5 * m_qDelayRef.GetSeconds ())) && (m_dropProb == 0) && (m_burstAllowance.GetSeconds () == 0))
319  {
321  {
323  m_burstReset = 0;
324  }
325  else if (m_burstState == IN_BURST)
326  {
327  m_burstReset++;
328  if (m_burstReset > burstResetLimit)
329  {
330  m_burstReset = 0;
332  }
333  }
334  }
335  else if (m_burstState == IN_BURST)
336  {
337  m_burstReset = 0;
338  }
339 
340  m_qDelayOld = qDelay;
342 }
343 
346 {
347  NS_LOG_FUNCTION (this);
348 
349  if (GetInternalQueue (0)->IsEmpty ())
350  {
351  NS_LOG_LOGIC ("Queue empty");
352  return 0;
353  }
354 
355  Ptr<QueueDiscItem> item = GetInternalQueue (0)->Dequeue ();
356  double now = Simulator::Now ().GetSeconds ();
357  uint32_t pktSize = item->GetSize ();
358 
359  // if not in a measurement cycle and the queue has built up to dq_threshold,
360  // start the measurement cycle
361 
362  if ( (GetInternalQueue (0)->GetNBytes () >= m_dqThreshold) && (!m_inMeasurement) )
363  {
364  m_dqStart = now;
365  m_dqCount = 0;
366  m_inMeasurement = true;
367  }
368 
369  if (m_inMeasurement)
370  {
371  m_dqCount += pktSize;
372 
373  // done with a measurement cycle
374  if (m_dqCount >= m_dqThreshold)
375  {
376 
377  double tmp = now - m_dqStart;
378 
379  if (tmp > 0)
380  {
381  if (m_avgDqRate == 0)
382  {
383  m_avgDqRate = m_dqCount / tmp;
384  }
385  else
386  {
387  m_avgDqRate = (0.5 * m_avgDqRate) + (0.5 * (m_dqCount / tmp));
388  }
389  }
390 
391  // restart a measurement cycle if there is enough data
392  if (GetInternalQueue (0)->GetNBytes () > m_dqThreshold)
393  {
394  m_dqStart = now;
395  m_dqCount = 0;
396  m_inMeasurement = true;
397  }
398  else
399  {
400  m_dqCount = 0;
401  m_inMeasurement = false;
402  }
403  }
404  }
405 
406  return item;
407 }
408 
409 bool
411 {
412  NS_LOG_FUNCTION (this);
413  if (GetNQueueDiscClasses () > 0)
414  {
415  NS_LOG_ERROR ("PieQueueDisc cannot have classes");
416  return false;
417  }
418 
419  if (GetNPacketFilters () > 0)
420  {
421  NS_LOG_ERROR ("PieQueueDisc cannot have packet filters");
422  return false;
423  }
424 
425  if (GetNInternalQueues () == 0)
426  {
427  // add a DropTail queue
429  ("MaxSize", QueueSizeValue (GetMaxSize ())));
430  }
431 
432  if (GetNInternalQueues () != 1)
433  {
434  NS_LOG_ERROR ("PieQueueDisc needs 1 internal queue");
435  return false;
436  }
437 
438  return true;
439 }
440 
441 } //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 GetNPackets(void) const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:440
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
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:712
#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.
uint32_t GetValue() const
Get the underlying value.
Definition: queue-size.cc:175
double m_a
Parameter to pie controller.
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
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
static constexpr const char * FORCED_DROP
Drops due to queue limit: reactive.
virtual void InitializeParams(void)
Initialize the queue parameters.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
uint32_t GetNBytes(void) const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:447
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:740
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:182
Implements PIE Active Queue Management discipline.
Time m_qDelayRef
Desired queue delay.
double m_dropProb
Variable used in calculation of drop probability.
uint32_t m_meanPktSize
Average packet size in bytes.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
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.
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
virtual bool CheckConfig(void)
Check whether the current configuration is correct.
AttributeValue implementation for Time.
Definition: nstime.h:1076
Ptr< InternalQueue > GetInternalQueue(std::size_t i) const
Get the i-th internal queue.
Definition: queue-disc.cc:587
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
virtual void DoDispose(void)
Dispose of the object.
Definition: queue-disc.cc:381
Time m_qDelayOld
Old value of queue delay.
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
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.
std::size_t GetNQueueDiscClasses(void) const
Get the number of queue disc classes.
Definition: queue-disc.cc:652
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
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:1077
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.
QueueSize GetMaxSize(void) const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:454
QueueSizeUnit GetUnit() const
Get the underlying unit.
Definition: queue-size.cc:169
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:104
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model...
Used by queue discs with single internal queue.
Definition: queue-disc.h:106
Time m_qDelay
Current value of queue delay.
static TypeId GetTypeId(void)
Get the type ID.
std::size_t GetNPacketFilters(void) const
Get the number of packet filters.
Definition: queue-disc.cc:614
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:1014
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:254
static const uint32_t packetSize
virtual bool DoEnqueue(Ptr< QueueDiscItem > item)
This function actually enqueues a packet into the queue disc.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
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:915
static const uint64_t DQCOUNT_INVALID
Invalid dqCount value.
bool m_inMeasurement
Indicates whether we are in a measurement cycle.
std::size_t GetNInternalQueues(void) const
Get the number of internal queues.
Definition: queue-disc.cc:594