A Discrete-Event Network Simulator
API
codel-queue-disc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 Andrew McGregor
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Codel, the COntrolled DELay Queueing discipline
18  * Based on ns2 simulation code presented by Kathie Nichols
19  *
20  * This port based on linux kernel code by
21  * Authors:
22  * 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 "codel-queue-disc.h"
29 
30 #include "ns3/abort.h"
31 #include "ns3/drop-tail-queue.h"
32 #include "ns3/enum.h"
33 #include "ns3/log.h"
34 #include "ns3/object-factory.h"
35 #include "ns3/uinteger.h"
36 
37 namespace ns3
38 {
39 
40 NS_LOG_COMPONENT_DEFINE("CoDelQueueDisc");
41 
49 /* borrowed from the linux kernel */
50 static inline uint32_t
51 ReciprocalDivide(uint32_t A, uint32_t R)
52 {
53  return (uint32_t)(((uint64_t)A * R) >> 32);
54 }
55 
56 /* end kernel borrowings */
57 
62 static uint32_t
64 {
65  Time time = Simulator::Now();
66  uint64_t ns = time.GetNanoSeconds();
67 
68  return static_cast<uint32_t>(ns >> CODEL_SHIFT);
69 }
70 
71 NS_OBJECT_ENSURE_REGISTERED(CoDelQueueDisc);
72 
73 TypeId
75 {
76  static TypeId tid =
77  TypeId("ns3::CoDelQueueDisc")
79  .SetGroupName("TrafficControl")
80  .AddConstructor<CoDelQueueDisc>()
81  .AddAttribute("UseEcn",
82  "True to use ECN (packets are marked instead of being dropped)",
83  BooleanValue(false),
86  .AddAttribute("UseL4s",
87  "True to use L4S (only ECT1 packets are marked at CE threshold)",
88  BooleanValue(false),
91  .AddAttribute(
92  "MaxSize",
93  "The maximum number of packets/bytes accepted by this queue disc.",
94  QueueSizeValue(QueueSize(QueueSizeUnit::BYTES, 1500 * DEFAULT_CODEL_LIMIT)),
95  MakeQueueSizeAccessor(&QueueDisc::SetMaxSize, &QueueDisc::GetMaxSize),
96  MakeQueueSizeChecker())
97  .AddAttribute("MinBytes",
98  "The CoDel algorithm minbytes parameter.",
99  UintegerValue(1500),
101  MakeUintegerChecker<uint32_t>())
102  .AddAttribute("Interval",
103  "The CoDel algorithm interval",
104  StringValue("100ms"),
106  MakeTimeChecker())
107  .AddAttribute("Target",
108  "The CoDel algorithm target queue delay",
109  StringValue("5ms"),
111  MakeTimeChecker())
112  .AddAttribute("CeThreshold",
113  "The CoDel CE threshold for marking packets",
114  TimeValue(Time::Max()),
116  MakeTimeChecker())
117  .AddTraceSource("Count",
118  "CoDel count",
120  "ns3::TracedValueCallback::Uint32")
121  .AddTraceSource("LastCount",
122  "CoDel lastcount",
124  "ns3::TracedValueCallback::Uint32")
125  .AddTraceSource("DropState",
126  "Dropping state",
128  "ns3::TracedValueCallback::Bool")
129  .AddTraceSource("DropNext",
130  "Time until next packet drop",
132  "ns3::TracedValueCallback::Uint32");
133 
134  return tid;
135 }
136 
139  m_count(0),
140  m_lastCount(0),
141  m_dropping(false),
142  m_recInvSqrt(~0U >> REC_INV_SQRT_SHIFT),
143  m_firstAboveTime(0),
144  m_dropNext(0)
145 {
146  NS_LOG_FUNCTION(this);
147 }
148 
150 {
151  NS_LOG_FUNCTION(this);
152 }
153 
154 uint16_t
155 CoDelQueueDisc::NewtonStep(uint16_t recInvSqrt, uint32_t count)
156 {
158  uint32_t invsqrt = ((uint32_t)recInvSqrt) << REC_INV_SQRT_SHIFT;
159  uint32_t invsqrt2 = ((uint64_t)invsqrt * invsqrt) >> 32;
160  uint64_t val = (3LL << 32) - ((uint64_t)count * invsqrt2);
161 
162  val >>= 2; /* avoid overflow */
163  val = (val * invsqrt) >> (32 - 2 + 1);
164  return static_cast<uint16_t>(val >> REC_INV_SQRT_SHIFT);
165 }
166 
167 uint32_t
168 CoDelQueueDisc::ControlLaw(uint32_t t, uint32_t interval, uint32_t recInvSqrt)
169 {
171  return t + ReciprocalDivide(interval, recInvSqrt << REC_INV_SQRT_SHIFT);
172 }
173 
174 bool
176 {
177  NS_LOG_FUNCTION(this << item);
178 
179  if (GetCurrentSize() + item > GetMaxSize())
180  {
181  NS_LOG_LOGIC("Queue full -- dropping pkt");
183  return false;
184  }
185 
186  bool retval = GetInternalQueue(0)->Enqueue(item);
187 
188  // If Queue::Enqueue fails, QueueDisc::DropBeforeEnqueue is called by the
189  // internal queue because QueueDisc::AddInternalQueue sets the trace callback
190 
191  NS_LOG_LOGIC("Number packets " << GetInternalQueue(0)->GetNPackets());
192  NS_LOG_LOGIC("Number bytes " << GetInternalQueue(0)->GetNBytes());
193 
194  return retval;
195 }
196 
197 bool
199 {
200  NS_LOG_FUNCTION(this);
201  bool okToDrop;
202 
203  if (!item)
204  {
205  m_firstAboveTime = 0;
206  return false;
207  }
208 
209  Time delta = Simulator::Now() - item->GetTimeStamp();
210  NS_LOG_INFO("Sojourn time " << delta.As(Time::MS));
211  uint32_t sojournTime = Time2CoDel(delta);
212 
213  if (CoDelTimeBefore(sojournTime, Time2CoDel(m_target)) ||
215  {
216  // went below so we'll stay below for at least q->interval
217  NS_LOG_LOGIC("Sojourn time is below target or number of bytes in queue is less than "
218  "minBytes; packet should not be dropped");
219  m_firstAboveTime = 0;
220  return false;
221  }
222  okToDrop = false;
223  if (m_firstAboveTime == 0)
224  {
225  /* just went above from below. If we stay above
226  * for at least q->interval we'll say it's ok to drop
227  */
228  NS_LOG_LOGIC("Sojourn time has just gone above target from below, need to stay above for "
229  "at least q->interval before packet can be dropped. ");
231  }
232  else if (CoDelTimeAfter(now, m_firstAboveTime))
233  {
234  NS_LOG_LOGIC("Sojourn time has been above target for at least q->interval; it's OK to "
235  "(possibly) drop packet.");
236  okToDrop = true;
237  }
238  return okToDrop;
239 }
240 
243 {
244  NS_LOG_FUNCTION(this);
245 
246  Ptr<QueueDiscItem> item = GetInternalQueue(0)->Dequeue();
247  if (!item)
248  {
249  // Leave dropping state when queue is empty
250  m_dropping = false;
251  NS_LOG_LOGIC("Queue empty");
252  return nullptr;
253  }
254  uint32_t ldelay = Time2CoDel(Simulator::Now() - item->GetTimeStamp());
255  if (item && m_useL4s)
256  {
257  uint8_t tosByte = 0;
258  if (item->GetUint8Value(QueueItem::IP_DSFIELD, tosByte) &&
259  (((tosByte & 0x3) == 1) || (tosByte & 0x3) == 3))
260  {
261  if ((tosByte & 0x3) == 1)
262  {
263  NS_LOG_DEBUG("ECT1 packet " << static_cast<uint16_t>(tosByte & 0x3));
264  }
265  else
266  {
267  NS_LOG_DEBUG("CE packet " << static_cast<uint16_t>(tosByte & 0x3));
268  }
269 
270  if (CoDelTimeAfter(ldelay, Time2CoDel(m_ceThreshold)) &&
272  {
273  NS_LOG_LOGIC("Marking due to CeThreshold " << m_ceThreshold.GetSeconds());
274  }
275  return item;
276  }
277  }
278 
279  uint32_t now = CoDelGetTime();
280 
281  NS_LOG_LOGIC("Popped " << item);
282  NS_LOG_LOGIC("Number packets remaining " << GetInternalQueue(0)->GetNPackets());
283  NS_LOG_LOGIC("Number bytes remaining " << GetInternalQueue(0)->GetNBytes());
284 
285  // Determine if item should be dropped
286  bool okToDrop = OkToDrop(item, now);
287  bool isMarked = false;
288 
289  if (m_dropping)
290  { // In the dropping state (sojourn time has gone above target and hasn't come down yet)
291  // Check if we can leave the dropping state or next drop should occur
292  NS_LOG_LOGIC("In dropping state, check if it's OK to leave or next drop should occur");
293  if (!okToDrop)
294  {
295  /* sojourn time fell below target - leave dropping state */
296  NS_LOG_LOGIC("Sojourn time goes below target, it's OK to leave dropping state.");
297  m_dropping = false;
298  }
299  else if (CoDelTimeAfterEq(now, m_dropNext))
300  {
301  while (m_dropping && CoDelTimeAfterEq(now, m_dropNext))
302  {
303  ++m_count;
305  // It's time for the next drop. Drop the current packet and
306  // dequeue the next. The dequeue might take us out of dropping
307  // state. If not, schedule the next drop.
308  // A large amount of packets in queue might result in drop
309  // rates so high that the next drop should happen now,
310  // hence the while loop.
311  if (m_useEcn && Mark(item, TARGET_EXCEEDED_MARK))
312  {
313  isMarked = true;
314  NS_LOG_LOGIC("Sojourn time is still above target and it's time for next drop "
315  "or mark; marking "
316  << item);
317  NS_LOG_LOGIC("Running ControlLaw for input m_dropNext: " << (double)m_dropNext /
318  1000000);
320  NS_LOG_LOGIC("Scheduled next drop at " << (double)m_dropNext / 1000000);
321  goto end;
322  }
323  NS_LOG_LOGIC(
324  "Sojourn time is still above target and it's time for next drop; dropping "
325  << item);
327 
328  item = GetInternalQueue(0)->Dequeue();
329 
330  if (item)
331  {
332  NS_LOG_LOGIC("Popped " << item);
333  NS_LOG_LOGIC("Number packets remaining " << GetInternalQueue(0)->GetNPackets());
334  NS_LOG_LOGIC("Number bytes remaining " << GetInternalQueue(0)->GetNBytes());
335  }
336 
337  if (!OkToDrop(item, now))
338  {
339  /* leave dropping state */
340  NS_LOG_LOGIC("Leaving dropping state");
341  m_dropping = false;
342  }
343  else
344  {
345  /* schedule the next drop */
346  NS_LOG_LOGIC("Running ControlLaw for input m_dropNext: " << (double)m_dropNext /
347  1000000);
349  NS_LOG_LOGIC("Scheduled next drop at " << (double)m_dropNext / 1000000);
350  }
351  }
352  }
353  }
354  else
355  {
356  // Not in the dropping state
357  // Decide if we have to enter the dropping state and drop the first packet
358  NS_LOG_LOGIC("Not in dropping state; decide if we have to enter the state and drop the "
359  "first packet");
360  if (okToDrop)
361  {
362  if (m_useEcn && Mark(item, TARGET_EXCEEDED_MARK))
363  {
364  isMarked = true;
365  NS_LOG_LOGIC("Sojourn time goes above target, marking the first packet "
366  << item << " and entering the dropping state");
367  }
368  else
369  {
370  // Drop the first packet and enter dropping state unless the queue is empty
371  NS_LOG_LOGIC("Sojourn time goes above target, dropping the first packet "
372  << item << " and entering the dropping state");
374  item = GetInternalQueue(0)->Dequeue();
375  if (item)
376  {
377  NS_LOG_LOGIC("Popped " << item);
378  NS_LOG_LOGIC("Number packets remaining " << GetInternalQueue(0)->GetNPackets());
379  NS_LOG_LOGIC("Number bytes remaining " << GetInternalQueue(0)->GetNBytes());
380  }
381  OkToDrop(item, now);
382  }
383  m_dropping = true;
384  /*
385  * if min went above target close to when we last went below it
386  * assume that the drop rate that controlled the queue on the
387  * last cycle is a good starting point to control it now.
388  */
389  int delta = m_count - m_lastCount;
390  if (delta > 1 && CoDelTimeBefore(now - m_dropNext, 16 * Time2CoDel(m_interval)))
391  {
392  m_count = delta;
394  }
395  else
396  {
397  m_count = 1;
399  }
401  NS_LOG_LOGIC("Running ControlLaw for input now: " << (double)now);
403  NS_LOG_LOGIC("Scheduled next drop at " << (double)m_dropNext / 1000000 << " now "
404  << (double)now / 1000000);
405  }
406  }
407 end:
408  ldelay = Time2CoDel(Simulator::Now() - item->GetTimeStamp());
409  // In Linux, this branch of code is executed even if the packet has been marked
410  // according to the target delay above. If the ns-3 code were to do the same here,
411  // it would result in two counts of mark in the queue statistics. Therefore, we
412  // use the isMarked flag to suppress a second attempt at marking.
413  if (!isMarked && item && !m_useL4s && m_useEcn &&
415  {
416  NS_LOG_LOGIC("Marking due to CeThreshold " << m_ceThreshold.GetSeconds());
417  }
418  return item;
419 }
420 
421 Time
423 {
424  return m_target;
425 }
426 
427 Time
429 {
430  return m_interval;
431 }
432 
433 uint32_t
435 {
436  return m_dropNext;
437 }
438 
439 bool
440 CoDelQueueDisc::CoDelTimeAfter(uint32_t a, uint32_t b)
441 {
442  return ((int64_t)(a) - (int64_t)(b) > 0);
443 }
444 
445 bool
446 CoDelQueueDisc::CoDelTimeAfterEq(uint32_t a, uint32_t b)
447 {
448  return ((int64_t)(a) - (int64_t)(b) >= 0);
449 }
450 
451 bool
452 CoDelQueueDisc::CoDelTimeBefore(uint32_t a, uint32_t b)
453 {
454  return ((int64_t)(a) - (int64_t)(b) < 0);
455 }
456 
457 bool
458 CoDelQueueDisc::CoDelTimeBeforeEq(uint32_t a, uint32_t b)
459 {
460  return ((int64_t)(a) - (int64_t)(b) <= 0);
461 }
462 
463 uint32_t
465 {
466  return static_cast<uint32_t>(t.GetNanoSeconds() >> CODEL_SHIFT);
467 }
468 
469 bool
471 {
472  NS_LOG_FUNCTION(this);
473  if (GetNQueueDiscClasses() > 0)
474  {
475  NS_LOG_ERROR("CoDelQueueDisc cannot have classes");
476  return false;
477  }
478 
479  if (GetNPacketFilters() > 0)
480  {
481  NS_LOG_ERROR("CoDelQueueDisc cannot have packet filters");
482  return false;
483  }
484 
485  if (GetNInternalQueues() == 0)
486  {
487  // add a DropTail queue
490  QueueSizeValue(GetMaxSize())));
491  }
492 
493  if (GetNInternalQueues() != 1)
494  {
495  NS_LOG_ERROR("CoDelQueueDisc needs 1 internal queue");
496  return false;
497  }
498 
499  return true;
500 }
501 
502 void
504 {
505  NS_LOG_FUNCTION(this);
506 }
507 
508 } // namespace ns3
A CoDel packet queue disc.
bool CheckConfig() override
Check whether the current configuration is correct.
uint32_t GetDropNext()
Get the time for next packet drop while in the dropping state.
static uint16_t NewtonStep(uint16_t recInvSqrt, uint32_t count)
Calculate the reciprocal square root of m_count by using Newton's method http://en....
static constexpr const char * OVERLIMIT_DROP
Overlimit dropped packet.
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).
static constexpr const char * TARGET_EXCEEDED_DROP
Sojourn time above target.
uint16_t m_recInvSqrt
Reciprocal inverse square root.
bool CoDelTimeBeforeEq(uint32_t a, uint32_t b)
Check if CoDel time a is preceding or equal to b.
void InitializeParams() override
Initialize parameters (if any) before the first packet is enqueued.
Time m_ceThreshold
Threshold above which to CE mark.
Time GetInterval()
Get the interval.
Ptr< QueueDiscItem > DoDequeue() override
Remove a packet from queue based on the current state If we are in dropping state,...
uint32_t m_minBytes
Minimum bytes in queue to allow a packet drop.
bool CoDelTimeAfterEq(uint32_t a, uint32_t b)
Check if CoDel time a is successive or equal to b.
static constexpr const char * CE_THRESHOLD_EXCEEDED_MARK
Sojourn time above CE threshold.
TracedValue< uint32_t > m_count
Number of packets dropped since entering drop state.
bool m_useL4s
True if L4S is used (ECT1 packets are marked at CE threshold)
bool OkToDrop(Ptr< QueueDiscItem > item, uint32_t now)
Determine whether a packet is OK to be dropped.
TracedValue< uint32_t > m_lastCount
Last number of packets dropped since entering drop state.
Time m_target
5 ms target queue delay
uint32_t Time2CoDel(Time t)
Return the unsigned 32-bit integer representation of the input Time object.
CoDelQueueDisc()
CoDelQueueDisc Constructor.
uint32_t m_firstAboveTime
Time to declare sojourn time above target.
Time GetTarget()
Get the target queue delay.
bool m_useEcn
True if ECN is used (packets are marked instead of being dropped)
TracedValue< bool > m_dropping
True if in dropping state.
bool CoDelTimeAfter(uint32_t a, uint32_t b)
Check if CoDel time a is successive to b.
bool CoDelTimeBefore(uint32_t a, uint32_t b)
Check if CoDel time a is preceding b.
bool DoEnqueue(Ptr< QueueDiscItem > item) override
Add a packet to the queue.
static constexpr const char * TARGET_EXCEEDED_MARK
Sojourn time above target.
Time m_interval
100 ms sliding minimum time window width
TracedValue< uint32_t > m_dropNext
Time to drop next packet.
static TypeId GetTypeId()
Get the type ID.
A FIFO packet queue that drops tail-end packets on overflow.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:184
void AddInternalQueue(Ptr< InternalQueue > queue)
Add an internal queue to the tail of the list of queues.
Definition: queue-disc.cc:573
uint32_t GetNPackets() const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:432
uint32_t GetNBytes() const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:439
Ptr< InternalQueue > GetInternalQueue(std::size_t i) const
Get the i-th internal queue.
Definition: queue-disc.cc:591
QueueSize GetCurrentSize() const
Get the current size of the queue disc in bytes, if operating in bytes mode, or packets,...
Definition: queue-disc.cc:515
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:759
std::size_t GetNQueueDiscClasses() const
Get the number of queue disc classes.
Definition: queue-disc.cc:661
QueueSize GetMaxSize() const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:446
std::size_t GetNPacketFilters() const
Get the number of packet filters.
Definition: queue-disc.cc:618
bool SetMaxSize(QueueSize size)
Set the maximum size of the queue disc.
Definition: queue-disc.cc:474
std::size_t GetNInternalQueues() const
Get the number of internal queues.
Definition: queue-disc.cc:598
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:809
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:720
Class for representing queue sizes.
Definition: queue-size.h:96
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
int64_t GetNanoSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:418
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
@ MS
millisecond
Definition: nstime.h:117
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition: nstime.h:297
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
Hold an unsigned integer type.
Definition: uinteger.h:45
#define DEFAULT_CODEL_LIMIT
#define REC_INV_SQRT_SHIFT
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:46
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
QueueDiscSizePolicy
Enumeration of the available policies to handle the queue disc size.
Definition: queue-disc.h:107
@ SINGLE_INTERNAL_QUEUE
Used by queue discs with single internal queue.
Definition: queue-disc.h:108
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1414
static int64_t CoDelGetTime()
Returns the current time translated in CoDel time representation.
static const int CODEL_SHIFT
Number of bits discarded from the time representation.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
static uint32_t ReciprocalDivide(uint32_t A, uint32_t R)
Performs a reciprocal divide, similar to the Linux kernel reciprocal_divide function.
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46