A Discrete-Event Network Simulator
API
codel-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) 2012 Andrew McGregor
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  * Codel, the COntrolled DELay Queueing discipline
19  * Based on ns2 simulation code presented by Kathie Nichols
20  *
21  * This port based on linux kernel code by
22  * Authors: Dave Täht <d@taht.net>
23  * Eric Dumazet <edumazet@google.com>
24  *
25  * Ported to ns-3 by: Andrew McGregor <andrewmcgr@gmail.com>
26 */
27 
28 #include "ns3/log.h"
29 #include "ns3/enum.h"
30 #include "ns3/uinteger.h"
31 #include "ns3/abort.h"
32 #include "codel-queue-disc.h"
33 #include "ns3/object-factory.h"
34 #include "ns3/drop-tail-queue.h"
35 
36 namespace ns3 {
37 
38 NS_LOG_COMPONENT_DEFINE ("CoDelQueueDisc");
39 
47 /* borrowed from the linux kernel */
48 static inline uint32_t ReciprocalDivide (uint32_t A, uint32_t R)
49 {
50  return (uint32_t)(((uint64_t)A * R) >> 32);
51 }
52 
53 /* end kernel borrowings */
54 
59 static uint32_t CoDelGetTime (void)
60 {
61  Time time = Simulator::Now ();
62  uint64_t ns = time.GetNanoSeconds ();
63 
64  return static_cast<uint32_t>(ns >> CODEL_SHIFT);
65 }
66 
67 
68 NS_OBJECT_ENSURE_REGISTERED (CoDelQueueDisc);
69 
71 {
72  static TypeId tid = TypeId ("ns3::CoDelQueueDisc")
73  .SetParent<QueueDisc> ()
74  .SetGroupName ("TrafficControl")
75  .AddConstructor<CoDelQueueDisc> ()
76  .AddAttribute ("UseEcn",
77  "True to use ECN (packets are marked instead of being dropped)",
78  BooleanValue (false),
81  .AddAttribute ("MaxSize",
82  "The maximum number of packets/bytes accepted by this queue disc.",
87  .AddAttribute ("MinBytes",
88  "The CoDel algorithm minbytes parameter.",
89  UintegerValue (1500),
91  MakeUintegerChecker<uint32_t> ())
92  .AddAttribute ("Interval",
93  "The CoDel algorithm interval",
94  StringValue ("100ms"),
96  MakeTimeChecker ())
97  .AddAttribute ("Target",
98  "The CoDel algorithm target queue delay",
99  StringValue ("5ms"),
101  MakeTimeChecker ())
102  .AddAttribute ("CeThreshold",
103  "The CoDel CE threshold for marking packets",
104  TimeValue (Time::Max ()),
106  MakeTimeChecker ())
107  .AddTraceSource ("Count",
108  "CoDel count",
110  "ns3::TracedValueCallback::Uint32")
111  .AddTraceSource ("LastCount",
112  "CoDel lastcount",
114  "ns3::TracedValueCallback::Uint32")
115  .AddTraceSource ("DropState",
116  "Dropping state",
118  "ns3::TracedValueCallback::Bool")
119  .AddTraceSource ("DropNext",
120  "Time until next packet drop",
122  "ns3::TracedValueCallback::Uint32")
123  ;
124 
125  return tid;
126 }
127 
130  m_count (0),
131  m_lastCount (0),
132  m_dropping (false),
133  m_recInvSqrt (~0U >> REC_INV_SQRT_SHIFT),
134  m_firstAboveTime (0),
135  m_dropNext (0)
136 {
137  NS_LOG_FUNCTION (this);
138 }
139 
141 {
142  NS_LOG_FUNCTION (this);
143 }
144 
145 uint16_t
146 CoDelQueueDisc::NewtonStep (uint16_t recInvSqrt, uint32_t count)
147 {
149  uint32_t invsqrt = ((uint32_t) recInvSqrt) << REC_INV_SQRT_SHIFT;
150  uint32_t invsqrt2 = ((uint64_t) invsqrt * invsqrt) >> 32;
151  uint64_t val = (3ll << 32) - ((uint64_t) count * invsqrt2);
152 
153  val >>= 2; /* avoid overflow */
154  val = (val * invsqrt) >> (32 - 2 + 1);
155  return static_cast<uint16_t>(val >> REC_INV_SQRT_SHIFT);
156 }
157 
158 uint32_t
159 CoDelQueueDisc::ControlLaw (uint32_t t, uint32_t interval, uint32_t recInvSqrt)
160 {
162  return t + ReciprocalDivide (interval, recInvSqrt << REC_INV_SQRT_SHIFT);
163 }
164 
165 bool
167 {
168  NS_LOG_FUNCTION (this << item);
169 
170  if (GetCurrentSize () + item > GetMaxSize ())
171  {
172  NS_LOG_LOGIC ("Queue full -- dropping pkt");
174  return false;
175  }
176 
177  bool retval = GetInternalQueue (0)->Enqueue (item);
178 
179  // If Queue::Enqueue fails, QueueDisc::DropBeforeEnqueue is called by the
180  // internal queue because QueueDisc::AddInternalQueue sets the trace callback
181 
182  NS_LOG_LOGIC ("Number packets " << GetInternalQueue (0)->GetNPackets ());
183  NS_LOG_LOGIC ("Number bytes " << GetInternalQueue (0)->GetNBytes ());
184 
185  return retval;
186 }
187 
188 bool
190 {
191  NS_LOG_FUNCTION (this);
192  bool okToDrop;
193 
194  if (!item)
195  {
196  m_firstAboveTime = 0;
197  return false;
198  }
199 
200  Time delta = Simulator::Now () - item->GetTimeStamp ();
201  NS_LOG_INFO ("Sojourn time " << delta.ToDouble (Time::MS) << "ms");
202  uint32_t sojournTime = Time2CoDel (delta);
203 
204  if (CoDelTimeBefore (sojournTime, Time2CoDel (m_target))
206  {
207  // went below so we'll stay below for at least q->interval
208  NS_LOG_LOGIC ("Sojourn time is below target or number of bytes in queue is less than minBytes; packet should not be dropped");
209  m_firstAboveTime = 0;
210  return false;
211  }
212  okToDrop = false;
213  if (m_firstAboveTime == 0)
214  {
215  /* just went above from below. If we stay above
216  * for at least q->interval we'll say it's ok to drop
217  */
218  NS_LOG_LOGIC ("Sojourn time has just gone above target from below, need to stay above for at least q->interval before packet can be dropped. ");
220  }
221  else if (CoDelTimeAfter (now, m_firstAboveTime))
222  {
223  NS_LOG_LOGIC ("Sojourn time has been above target for at least q->interval; it's OK to (possibly) drop packet.");
224  okToDrop = true;
225  }
226  return okToDrop;
227 }
228 
231 {
232  NS_LOG_FUNCTION (this);
233 
234  Ptr<QueueDiscItem> item = GetInternalQueue (0)->Dequeue ();
235  if (!item)
236  {
237  // Leave dropping state when queue is empty
238  m_dropping = false;
239  NS_LOG_LOGIC ("Queue empty");
240  return 0;
241  }
242  uint32_t now = CoDelGetTime ();
243 
244  NS_LOG_LOGIC ("Popped " << item);
245  NS_LOG_LOGIC ("Number packets remaining " << GetInternalQueue (0)->GetNPackets ());
246  NS_LOG_LOGIC ("Number bytes remaining " << GetInternalQueue (0)->GetNBytes ());
247 
248  // Determine if item should be dropped
249  bool okToDrop = OkToDrop (item, now);
250 
251  if (m_dropping)
252  { // In the dropping state (sojourn time has gone above target and hasn't come down yet)
253  // Check if we can leave the dropping state or next drop should occur
254  NS_LOG_LOGIC ("In dropping state, check if it's OK to leave or next drop should occur");
255  if (!okToDrop)
256  {
257  /* sojourn time fell below target - leave dropping state */
258  NS_LOG_LOGIC ("Sojourn time goes below target, it's OK to leave dropping state.");
259  m_dropping = false;
260  }
261  else if (CoDelTimeAfterEq (now, m_dropNext))
262  {
263  while (m_dropping && CoDelTimeAfterEq (now, m_dropNext))
264  {
265  ++m_count;
267  // It's time for the next drop. Drop the current packet and
268  // dequeue the next. The dequeue might take us out of dropping
269  // state. If not, schedule the next drop.
270  // A large amount of packets in queue might result in drop
271  // rates so high that the next drop should happen now,
272  // hence the while loop.
273  if (m_useEcn && Mark (item, TARGET_EXCEEDED_MARK))
274  {
275  NS_LOG_LOGIC ("Sojourn time is still above target and it's time for next drop or mark; marking " << item);
276  NS_LOG_LOGIC ("Running ControlLaw for input m_dropNext: " << (double)m_dropNext / 1000000);
278  NS_LOG_LOGIC ("Scheduled next drop at " << (double) m_dropNext / 1000000);
279  goto end;
280  }
281  NS_LOG_LOGIC ("Sojourn time is still above target and it's time for next drop; dropping " << item);
283 
284  item = GetInternalQueue (0)->Dequeue ();
285 
286  if (item)
287  {
288  NS_LOG_LOGIC ("Popped " << item);
289  NS_LOG_LOGIC ("Number packets remaining " << GetInternalQueue (0)->GetNPackets ());
290  NS_LOG_LOGIC ("Number bytes remaining " << GetInternalQueue (0)->GetNBytes ());
291  }
292 
293  if (!OkToDrop (item, now))
294  {
295  /* leave dropping state */
296  NS_LOG_LOGIC ("Leaving dropping state");
297  m_dropping = false;
298  }
299  else
300  {
301  /* schedule the next drop */
302  NS_LOG_LOGIC ("Running ControlLaw for input m_dropNext: " << (double)m_dropNext / 1000000);
304  NS_LOG_LOGIC ("Scheduled next drop at " << (double)m_dropNext / 1000000);
305  }
306  }
307  }
308  }
309  else
310  {
311  // Not in the dropping state
312  // Decide if we have to enter the dropping state and drop the first packet
313  NS_LOG_LOGIC ("Not in dropping state; decide if we have to enter the state and drop the first packet");
314  if (okToDrop)
315  {
316  if (m_useEcn && Mark (item, TARGET_EXCEEDED_MARK))
317  {
318  NS_LOG_LOGIC ("Sojourn time goes above target, marking the first packet " << item << " and entering the dropping state");
319  }
320  else
321  {
322  // Drop the first packet and enter dropping state unless the queue is empty
323  NS_LOG_LOGIC ("Sojourn time goes above target, dropping the first packet " << item << " and entering the dropping state");
325  item = GetInternalQueue (0)->Dequeue ();
326  if (item)
327  {
328  NS_LOG_LOGIC ("Popped " << item);
329  NS_LOG_LOGIC ("Number packets remaining " << GetInternalQueue (0)->GetNPackets ());
330  NS_LOG_LOGIC ("Number bytes remaining " << GetInternalQueue (0)->GetNBytes ());
331  }
332  OkToDrop (item, now);
333  }
334  m_dropping = true;
335  /*
336  * if min went above target close to when we last went below it
337  * assume that the drop rate that controlled the queue on the
338  * last cycle is a good starting point to control it now.
339  */
340  int delta = m_count - m_lastCount;
341  if (delta > 1 && CoDelTimeBefore (now - m_dropNext, 16 * Time2CoDel (m_interval)))
342  {
343  m_count = delta;
345  }
346  else
347  {
348  m_count = 1;
350  }
352  NS_LOG_LOGIC ("Running ControlLaw for input now: " << (double)now);
354  NS_LOG_LOGIC ("Scheduled next drop at " << (double)m_dropNext / 1000000 << " now " << (double)now / 1000000);
355  }
356  }
357  end:
358  uint32_t ldelay = Time2CoDel (Simulator::Now () - item->GetTimeStamp ());
359  if (item && m_useEcn && CoDelTimeAfter (ldelay, Time2CoDel (m_ceThreshold)) && Mark (item, CE_THRESHOLD_EXCEEDED_MARK))
360  {
361  NS_LOG_LOGIC ("Marking due to CeThreshold " << m_ceThreshold.GetSeconds ());
362  }
363  return item;
364 }
365 
366 Time
368 {
369  return m_target;
370 }
371 
372 Time
374 {
375  return m_interval;
376 }
377 
378 uint32_t
380 {
381  return m_dropNext;
382 }
383 
384 bool
385 CoDelQueueDisc::CoDelTimeAfter (uint32_t a, uint32_t b)
386 {
387  return ((int64_t)(a) - (int64_t)(b) > 0);
388 }
389 
390 bool
391 CoDelQueueDisc::CoDelTimeAfterEq (uint32_t a, uint32_t b)
392 {
393  return ((int64_t)(a) - (int64_t)(b) >= 0);
394 }
395 
396 bool
397 CoDelQueueDisc::CoDelTimeBefore (uint32_t a, uint32_t b)
398 {
399  return ((int64_t)(a) - (int64_t)(b) < 0);
400 }
401 
402 bool
403 CoDelQueueDisc::CoDelTimeBeforeEq (uint32_t a, uint32_t b)
404 {
405  return ((int64_t)(a) - (int64_t)(b) <= 0);
406 }
407 
408 uint32_t
410 {
411  return static_cast<uint32_t>(t.GetNanoSeconds () >> CODEL_SHIFT);
412 }
413 
414 bool
416 {
417  NS_LOG_FUNCTION (this);
418  if (GetNQueueDiscClasses () > 0)
419  {
420  NS_LOG_ERROR ("CoDelQueueDisc cannot have classes");
421  return false;
422  }
423 
424  if (GetNPacketFilters () > 0)
425  {
426  NS_LOG_ERROR ("CoDelQueueDisc cannot have packet filters");
427  return false;
428  }
429 
430  if (GetNInternalQueues () == 0)
431  {
432  // add a DropTail queue
434  ("MaxSize", QueueSizeValue (GetMaxSize ())));
435  }
436 
437  if (GetNInternalQueues () != 1)
438  {
439  NS_LOG_ERROR ("CoDelQueueDisc needs 1 internal queue");
440  return false;
441  }
442 
443  return true;
444 }
445 
446 void
448 {
449  NS_LOG_FUNCTION (this);
450 }
451 
452 } // namespace ns3
453 
virtual void InitializeParams(void)
Initialize parameters (if any) before the first packet is enqueued.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
uint32_t GetNPackets(void) const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:440
virtual bool DoEnqueue(Ptr< QueueDiscItem > item)
Add a packet to the queue.
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 "...
static constexpr const char * TARGET_EXCEEDED_DROP
Sojourn time above target.
static uint32_t ControlLaw(uint32_t t, uint32_t interval, uint32_t recInvSqrt)
Determine the time for next drop CoDel control law is t + m_interval/sqrt(m_count).
AttributeValue implementation for Boolean.
Definition: boolean.h:36
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:729
static constexpr const char * CE_THRESHOLD_EXCEEDED_MARK
Sojourn time above CE threshold.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Hold variables of type string.
Definition: string.h:41
uint32_t GetDropNext(void)
Get the time for next packet drop while in the dropping state.
TracedValue< uint32_t > m_count
Number of packets dropped since entering drop state.
bool Mark(Ptr< QueueDiscItem > item, const char *reason)
Marks the given packet and, if successful, updates the counters associated with the given reason...
Definition: queue-disc.cc:818
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
static constexpr const char * OVERLIMIT_DROP
Overlimit dropped packet.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: boolean.h:85
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:361
virtual bool CheckConfig(void)
Check whether the current configuration is correct.
Time GetInterval(void)
Get the interval.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
double ToDouble(enum Unit unit) const
Get the Time value expressed in a particular unit.
Definition: nstime.h:511
bool CoDelTimeAfterEq(uint32_t a, uint32_t b)
Check if CoDel time a is successive or equal to b.
uint32_t GetNBytes(void) const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:447
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:181
static uint16_t NewtonStep(uint16_t recInvSqrt, uint32_t count)
Calculate the reciprocal square root of m_count by using Newton&#39;s method http://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Iterative_methods_for_reciprocal_square_roots m_recInvSqrt (new) = (m_recInvSqrt (old) / 2) * (3 - m_count * m_recInvSqrt^2)
bool CoDelTimeAfter(uint32_t a, uint32_t b)
Check if CoDel time a is successive to b.
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
TracedValue< bool > m_dropping
True if in dropping state.
bool OkToDrop(Ptr< QueueDiscItem > item, uint32_t now)
Determine whether a packet is OK to be dropped.
static Time Max()
Maximum representable Time.
Definition: nstime.h:279
void AddInternalQueue(Ptr< InternalQueue > queue)
Add an internal queue to the tail of the list of queues.
Definition: queue-disc.cc:581
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.
uint32_t m_firstAboveTime
Time to declare sojourn time above target.
AttributeValue implementation for Time.
Definition: nstime.h:1124
bool CoDelTimeBefore(uint32_t a, uint32_t b)
Check if CoDel time a is preceding b.
Ptr< InternalQueue > GetInternalQueue(std::size_t i) const
Get the i-th internal queue.
Definition: queue-disc.cc:601
Hold an unsigned integer type.
Definition: uinteger.h:44
Time m_target
5 ms target queue delay
CoDelQueueDisc()
CoDelQueueDisc Constructor.
Ptr< const AttributeAccessor > MakeQueueSizeAccessor(T1 a1)
Definition: queue-size.h:221
TracedValue< uint32_t > m_lastCount
Last number of packets dropped since entering drop state.
bool m_useEcn
True if ECN is used (packets are marked instead of being dropped)
Time m_interval
100 ms sliding minimum time window width
Introspection did not find any typical Config paths.
Ptr< const AttributeChecker > MakeQueueSizeChecker(void)
Definition: queue-size.cc:29
virtual Ptr< QueueDiscItem > DoDequeue(void)
Remove a packet from queue based on the current state If we are in dropping state, check if we could leave the dropping state or if we should perform next drop If we are not currently in dropping state, check if we need to enter the state and drop the first packet.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint16_t m_recInvSqrt
Reciprocal inverse square root.
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:373
std::size_t GetNQueueDiscClasses(void) const
Get the number of queue disc classes.
Definition: queue-disc.cc:669
#define REC_INV_SQRT_SHIFT
Time GetTarget(void)
Get the target queue delay.
#define DEFAULT_CODEL_LIMIT
A CoDel packet queue disc.
static const int CODEL_SHIFT
Number of bits discarded from the time representation.
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:1125
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
QueueSize GetMaxSize(void) const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:454
uint32_t Time2CoDel(Time t)
Return the unsigned 32-bit integer representation of the input Time object.
QueueDiscSizePolicy
Enumeration of the available policies to handle the queue disc size.
Definition: queue-disc.h:103
static constexpr const char * TARGET_EXCEEDED_MARK
Sojourn time above target.
static TypeId GetTypeId(void)
Get the type ID.
bool CoDelTimeBeforeEq(uint32_t a, uint32_t b)
Check if CoDel time a is preceding or equal to b.
Used by queue discs with single internal queue.
Definition: queue-disc.h:105
uint32_t m_minBytes
Minimum bytes in queue to allow a packet drop.
static uint32_t ReciprocalDivide(uint32_t A, uint32_t R)
Performs a reciprocal divide, similar to the Linux kernel reciprocal_divide function.
std::size_t GetNPacketFilters(void) const
Get the number of packet filters.
Definition: queue-disc.cc:628
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
bool SetMaxSize(QueueSize size)
Set the maximum size of the queue disc.
Definition: queue-disc.cc:482
void DropAfterDequeue(Ptr< const QueueDiscItem > item, const char *reason)
Perform the actions required when the queue disc is notified of a packet dropped after dequeue...
Definition: queue-disc.cc:768
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:449
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
static int64_t CoDelGetTime(void)
Returns the current time translated in CoDel time representation.
millisecond
Definition: nstime.h:115
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
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
Time m_ceThreshold
Threshold above which to CE mark.
TracedValue< uint32_t > m_dropNext
Time to drop next packet.
std::size_t GetNInternalQueues(void) const
Get the number of internal queues.
Definition: queue-disc.cc:608