A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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: Dave Täht <d@taht.net>
22 * Eric Dumazet <edumazet@google.com>
23 *
24 * Ported to ns-3 by: Andrew McGregor <andrewmcgr@gmail.com>
25 */
26
27#include "codel-queue-disc.h"
28
29#include "ns3/abort.h"
30#include "ns3/drop-tail-queue.h"
31#include "ns3/enum.h"
32#include "ns3/log.h"
33#include "ns3/object-factory.h"
34#include "ns3/uinteger.h"
35
36namespace ns3
37{
38
39NS_LOG_COMPONENT_DEFINE("CoDelQueueDisc");
40
48/* borrowed from the linux kernel */
49static inline uint32_t
51{
52 return (uint32_t)(((uint64_t)A * R) >> 32);
53}
54
55/* end kernel borrowings */
56
61static uint32_t
63{
64 Time time = Simulator::Now();
65 uint64_t ns = time.GetNanoSeconds();
66
67 return static_cast<uint32_t>(ns >> CODEL_SHIFT);
68}
69
70NS_OBJECT_ENSURE_REGISTERED(CoDelQueueDisc);
71
72TypeId
74{
75 static TypeId tid =
76 TypeId("ns3::CoDelQueueDisc")
78 .SetGroupName("TrafficControl")
79 .AddConstructor<CoDelQueueDisc>()
80 .AddAttribute("UseEcn",
81 "True to use ECN (packets are marked instead of being dropped)",
82 BooleanValue(false),
85 .AddAttribute("UseL4s",
86 "True to use L4S (only ECT1 packets are marked at CE threshold)",
87 BooleanValue(false),
90 .AddAttribute(
91 "MaxSize",
92 "The maximum number of packets/bytes accepted by this queue disc.",
94 MakeQueueSizeAccessor(&QueueDisc::SetMaxSize, &QueueDisc::GetMaxSize),
95 MakeQueueSizeChecker())
96 .AddAttribute("MinBytes",
97 "The CoDel algorithm minbytes parameter.",
98 UintegerValue(1500),
100 MakeUintegerChecker<uint32_t>())
101 .AddAttribute("Interval",
102 "The CoDel algorithm interval",
103 StringValue("100ms"),
106 .AddAttribute("Target",
107 "The CoDel algorithm target queue delay",
108 StringValue("5ms"),
111 .AddAttribute("CeThreshold",
112 "The CoDel CE threshold for marking packets",
116 .AddTraceSource("Count",
117 "CoDel count",
119 "ns3::TracedValueCallback::Uint32")
120 .AddTraceSource("LastCount",
121 "CoDel lastcount",
123 "ns3::TracedValueCallback::Uint32")
124 .AddTraceSource("DropState",
125 "Dropping state",
127 "ns3::TracedValueCallback::Bool")
128 .AddTraceSource("DropNext",
129 "Time until next packet drop",
131 "ns3::TracedValueCallback::Uint32");
132
133 return tid;
134}
135
138 m_count(0),
139 m_lastCount(0),
140 m_dropping(false),
141 m_recInvSqrt(~0U >> REC_INV_SQRT_SHIFT),
142 m_firstAboveTime(0),
143 m_dropNext(0)
144{
145 NS_LOG_FUNCTION(this);
146}
147
149{
150 NS_LOG_FUNCTION(this);
151}
152
153uint16_t
154CoDelQueueDisc::NewtonStep(uint16_t recInvSqrt, uint32_t count)
155{
157 uint32_t invsqrt = ((uint32_t)recInvSqrt) << REC_INV_SQRT_SHIFT;
158 uint32_t invsqrt2 = ((uint64_t)invsqrt * invsqrt) >> 32;
159 uint64_t val = (3LL << 32) - ((uint64_t)count * invsqrt2);
160
161 val >>= 2; /* avoid overflow */
162 val = (val * invsqrt) >> (32 - 2 + 1);
163 return static_cast<uint16_t>(val >> REC_INV_SQRT_SHIFT);
164}
165
168{
170 return t + ReciprocalDivide(interval, recInvSqrt << REC_INV_SQRT_SHIFT);
171}
172
173bool
175{
176 NS_LOG_FUNCTION(this << item);
177
178 if (GetCurrentSize() + item > GetMaxSize())
179 {
180 NS_LOG_LOGIC("Queue full -- dropping pkt");
182 return false;
183 }
184
185 bool retval = GetInternalQueue(0)->Enqueue(item);
186
187 // If Queue::Enqueue fails, QueueDisc::DropBeforeEnqueue is called by the
188 // internal queue because QueueDisc::AddInternalQueue sets the trace callback
189
190 NS_LOG_LOGIC("Number packets " << GetInternalQueue(0)->GetNPackets());
191 NS_LOG_LOGIC("Number bytes " << GetInternalQueue(0)->GetNBytes());
192
193 return retval;
194}
195
196bool
198{
199 NS_LOG_FUNCTION(this);
200 bool okToDrop;
201
202 if (!item)
203 {
205 return false;
206 }
207
208 Time delta = Simulator::Now() - item->GetTimeStamp();
209 NS_LOG_INFO("Sojourn time " << delta.As(Time::MS));
210 uint32_t sojournTime = Time2CoDel(delta);
211
212 if (CoDelTimeBefore(sojournTime, Time2CoDel(m_target)) ||
214 {
215 // went below so we'll stay below for at least q->interval
216 NS_LOG_LOGIC("Sojourn time is below target or number of bytes in queue is less than "
217 "minBytes; packet should not be dropped");
219 return false;
220 }
221 okToDrop = false;
222 if (m_firstAboveTime == 0)
223 {
224 /* just went above from below. If we stay above
225 * for at least q->interval we'll say it's ok to drop
226 */
227 NS_LOG_LOGIC("Sojourn time has just gone above target from below, need to stay above for "
228 "at least q->interval before packet can be dropped. ");
230 }
231 else if (CoDelTimeAfter(now, m_firstAboveTime))
232 {
233 NS_LOG_LOGIC("Sojourn time has been above target for at least q->interval; it's OK to "
234 "(possibly) drop packet.");
235 okToDrop = true;
236 }
237 return okToDrop;
238}
239
242{
243 NS_LOG_FUNCTION(this);
244
245 Ptr<QueueDiscItem> item = GetInternalQueue(0)->Dequeue();
246 if (!item)
247 {
248 // Leave dropping state when queue is empty
249 m_dropping = false;
250 NS_LOG_LOGIC("Queue empty");
251 return nullptr;
252 }
253 uint32_t ldelay = Time2CoDel(Simulator::Now() - item->GetTimeStamp());
254 if (item && m_useL4s)
255 {
256 uint8_t tosByte = 0;
257 if (item->GetUint8Value(QueueItem::IP_DSFIELD, tosByte) &&
258 (((tosByte & 0x3) == 1) || (tosByte & 0x3) == 3))
259 {
260 if ((tosByte & 0x3) == 1)
261 {
262 NS_LOG_DEBUG("ECT1 packet " << static_cast<uint16_t>(tosByte & 0x3));
263 }
264 else
265 {
266 NS_LOG_DEBUG("CE packet " << static_cast<uint16_t>(tosByte & 0x3));
267 }
268
271 {
272 NS_LOG_LOGIC("Marking due to CeThreshold " << m_ceThreshold.GetSeconds());
273 }
274 return item;
275 }
276 }
277
278 uint32_t now = CoDelGetTime();
279
280 NS_LOG_LOGIC("Popped " << item);
281 NS_LOG_LOGIC("Number packets remaining " << GetInternalQueue(0)->GetNPackets());
282 NS_LOG_LOGIC("Number bytes remaining " << GetInternalQueue(0)->GetNBytes());
283
284 // Determine if item should be dropped
285 bool okToDrop = OkToDrop(item, now);
286 bool isMarked = false;
287
288 if (m_dropping)
289 { // In the dropping state (sojourn time has gone above target and hasn't come down yet)
290 // Check if we can leave the dropping state or next drop should occur
291 NS_LOG_LOGIC("In dropping state, check if it's OK to leave or next drop should occur");
292 if (!okToDrop)
293 {
294 /* sojourn time fell below target - leave dropping state */
295 NS_LOG_LOGIC("Sojourn time goes below target, it's OK to leave dropping state.");
296 m_dropping = false;
297 }
298 else if (CoDelTimeAfterEq(now, m_dropNext))
299 {
300 while (m_dropping && CoDelTimeAfterEq(now, m_dropNext))
301 {
302 ++m_count;
304 // It's time for the next drop. Drop the current packet and
305 // dequeue the next. The dequeue might take us out of dropping
306 // state. If not, schedule the next drop.
307 // A large amount of packets in queue might result in drop
308 // rates so high that the next drop should happen now,
309 // hence the while loop.
310 if (m_useEcn && Mark(item, TARGET_EXCEEDED_MARK))
311 {
312 isMarked = true;
313 NS_LOG_LOGIC("Sojourn time is still above target and it's time for next drop "
314 "or mark; marking "
315 << item);
316 NS_LOG_LOGIC("Running ControlLaw for input m_dropNext: " << (double)m_dropNext /
317 1000000);
319 NS_LOG_LOGIC("Scheduled next drop at " << (double)m_dropNext / 1000000);
320 goto end;
321 }
323 "Sojourn time is still above target and it's time for next drop; dropping "
324 << item);
326
327 item = GetInternalQueue(0)->Dequeue();
328
329 if (item)
330 {
331 NS_LOG_LOGIC("Popped " << item);
332 NS_LOG_LOGIC("Number packets remaining " << GetInternalQueue(0)->GetNPackets());
333 NS_LOG_LOGIC("Number bytes remaining " << GetInternalQueue(0)->GetNBytes());
334 }
335
336 if (!OkToDrop(item, now))
337 {
338 /* leave dropping state */
339 NS_LOG_LOGIC("Leaving dropping state");
340 m_dropping = false;
341 }
342 else
343 {
344 /* schedule the next drop */
345 NS_LOG_LOGIC("Running ControlLaw for input m_dropNext: " << (double)m_dropNext /
346 1000000);
348 NS_LOG_LOGIC("Scheduled next drop at " << (double)m_dropNext / 1000000);
349 }
350 }
351 }
352 }
353 else
354 {
355 // Not in the dropping state
356 // Decide if we have to enter the dropping state and drop the first packet
357 NS_LOG_LOGIC("Not in dropping state; decide if we have to enter the state and drop the "
358 "first packet");
359 if (okToDrop)
360 {
361 if (m_useEcn && Mark(item, TARGET_EXCEEDED_MARK))
362 {
363 isMarked = true;
364 NS_LOG_LOGIC("Sojourn time goes above target, marking the first packet "
365 << item << " and entering the dropping state");
366 }
367 else
368 {
369 // Drop the first packet and enter dropping state unless the queue is empty
370 NS_LOG_LOGIC("Sojourn time goes above target, dropping the first packet "
371 << item << " and entering the dropping state");
373 item = GetInternalQueue(0)->Dequeue();
374 if (item)
375 {
376 NS_LOG_LOGIC("Popped " << item);
377 NS_LOG_LOGIC("Number packets remaining " << GetInternalQueue(0)->GetNPackets());
378 NS_LOG_LOGIC("Number bytes remaining " << GetInternalQueue(0)->GetNBytes());
379 }
380 OkToDrop(item, now);
381 }
382 m_dropping = true;
383 /*
384 * if min went above target close to when we last went below it
385 * assume that the drop rate that controlled the queue on the
386 * last cycle is a good starting point to control it now.
387 */
388 int delta = m_count - m_lastCount;
389 if (delta > 1 && CoDelTimeBefore(now - m_dropNext, 16 * Time2CoDel(m_interval)))
390 {
391 m_count = delta;
393 }
394 else
395 {
396 m_count = 1;
398 }
400 NS_LOG_LOGIC("Running ControlLaw for input now: " << (double)now);
402 NS_LOG_LOGIC("Scheduled next drop at " << (double)m_dropNext / 1000000 << " now "
403 << (double)now / 1000000);
404 }
405 }
406end:
407 ldelay = Time2CoDel(Simulator::Now() - item->GetTimeStamp());
408 // In Linux, this branch of code is executed even if the packet has been marked
409 // according to the target delay above. If the ns-3 code were to do the same here,
410 // it would result in two counts of mark in the queue statistics. Therefore, we
411 // use the isMarked flag to suppress a second attempt at marking.
412 if (!isMarked && item && !m_useL4s && m_useEcn &&
414 {
415 NS_LOG_LOGIC("Marking due to CeThreshold " << m_ceThreshold.GetSeconds());
416 }
417 return item;
418}
419
420Time
422{
423 return m_target;
424}
425
426Time
428{
429 return m_interval;
430}
431
434{
435 return m_dropNext;
436}
437
438bool
440{
441 return ((int64_t)(a) - (int64_t)(b) > 0);
442}
443
444bool
446{
447 return ((int64_t)(a) - (int64_t)(b) >= 0);
448}
449
450bool
452{
453 return ((int64_t)(a) - (int64_t)(b) < 0);
454}
455
456bool
458{
459 return ((int64_t)(a) - (int64_t)(b) <= 0);
460}
461
464{
465 return static_cast<uint32_t>(t.GetNanoSeconds() >> CODEL_SHIFT);
466}
467
468bool
470{
471 NS_LOG_FUNCTION(this);
472 if (GetNQueueDiscClasses() > 0)
473 {
474 NS_LOG_ERROR("CoDelQueueDisc cannot have classes");
475 return false;
476 }
477
478 if (GetNPacketFilters() > 0)
479 {
480 NS_LOG_ERROR("CoDelQueueDisc cannot have packet filters");
481 return false;
482 }
483
484 if (GetNInternalQueues() == 0)
485 {
486 // add a DropTail queue
490 }
491
492 if (GetNInternalQueues() != 1)
493 {
494 NS_LOG_ERROR("CoDelQueueDisc needs 1 internal queue");
495 return false;
496 }
497
498 return true;
499}
500
501void
503{
504 NS_LOG_FUNCTION(this);
505}
506
507} // namespace ns3
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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:78
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:577
QueueSize GetCurrentSize()
Get the current size of the queue disc in bytes, if operating in bytes mode, or packets,...
Definition: queue-disc.cc:519
uint32_t GetNPackets() const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:436
uint32_t GetNBytes() const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:443
Ptr< InternalQueue > GetInternalQueue(std::size_t i) const
Get the i-th internal queue.
Definition: queue-disc.cc:595
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:766
std::size_t GetNQueueDiscClasses() const
Get the number of queue disc classes.
Definition: queue-disc.cc:665
QueueSize GetMaxSize() const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:450
std::size_t GetNPacketFilters() const
Get the number of packet filters.
Definition: queue-disc.cc:622
bool SetMaxSize(QueueSize size)
Set the maximum size of the queue disc.
Definition: queue-disc.cc:478
std::size_t GetNInternalQueues() const
Get the number of internal queues.
Definition: queue-disc.cc:602
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:817
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:726
Class for representing queue sizes.
Definition: queue-size.h:96
AttributeValue implementation for QueueSize.
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
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:417
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:402
@ MS
millisecond
Definition: nstime.h:117
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition: nstime.h:296
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:936
Hold an unsigned integer type.
Definition: uinteger.h:45
#define DEFAULT_CODEL_LIMIT
#define REC_INV_SQRT_SHIFT
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:86
Ptr< const AttributeChecker > MakeBooleanChecker()
Definition: boolean.cc:124
Ptr< const AttributeChecker > MakeTimeChecker()
Helper to make an unbounded Time checker.
Definition: nstime.h:1444
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1424
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Definition: uinteger.h:46
#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.
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.
static uint32_t ReciprocalDivide(uint32_t A, uint32_t R)
Performs a reciprocal divide, similar to the Linux kernel reciprocal_divide function.