A Discrete-Event Network Simulator
API
cobalt-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) 2019 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 * Cobalt, the CoDel - BLUE - Alternate Queueing discipline
19 * Based on linux code.
20 *
21 * Ported to ns-3 by: Vignesh Kannan <vignesh2496@gmail.com>
22 * Harsh Lara <harshapplefan@gmail.com>
23 * Jendaipou Palmei <jendaipoupalmei@gmail.com>
24 * Shefali Gupta <shefaligups11@gmail.com>
25 * Mohit P. Tahiliani <tahiliani@nitk.edu.in>
26 */
27
28#include "ns3/log.h"
29#include "ns3/enum.h"
30#include "ns3/uinteger.h"
31#include "ns3/abort.h"
32#include "cobalt-queue-disc.h"
33#include "ns3/object-factory.h"
34#include "ns3/drop-tail-queue.h"
35#include "ns3/net-device-queue-interface.h"
36#include <climits>
37
38
39namespace ns3 {
40
41NS_LOG_COMPONENT_DEFINE ("CobaltQueueDisc");
42
43NS_OBJECT_ENSURE_REGISTERED (CobaltQueueDisc);
44
46{
47 static TypeId tid = TypeId ("ns3::CobaltQueueDisc")
49 .SetGroupName ("TrafficControl")
50 .AddConstructor<CobaltQueueDisc> ()
51 .AddAttribute ("MaxSize",
52 "The maximum number of packets/bytes accepted by this queue disc.",
54 MakeQueueSizeAccessor (&QueueDisc::SetMaxSize,
56 MakeQueueSizeChecker ())
57 .AddAttribute ("Interval",
58 "The Cobalt algorithm interval",
59 StringValue ("100ms"),
62 .AddAttribute ("Target",
63 "The Cobalt algorithm target queue delay",
64 StringValue ("5ms"),
67 .AddAttribute ("UseEcn",
68 "True to use ECN (packets are marked instead of being dropped)",
69 BooleanValue (false),
72 .AddAttribute ("Pdrop",
73 "Marking Probability",
74 DoubleValue (0),
76 MakeDoubleChecker<double> ())
77 .AddAttribute ("Increment",
78 "Pdrop increment value",
79 DoubleValue (1. / 256),
81 MakeDoubleChecker<double> ())
82 .AddAttribute ("Decrement",
83 "Pdrop decrement Value",
84 DoubleValue (1. / 4096),
86 MakeDoubleChecker<double> ())
87 .AddAttribute ("CeThreshold",
88 "The CoDel CE threshold for marking packets",
92 .AddAttribute ("UseL4s",
93 "True to use L4S (only ECT1 packets are marked at CE threshold)",
94 BooleanValue (false),
97 .AddAttribute ("BlueThreshold",
98 "The Threshold after which Blue is enabled",
99 TimeValue (MilliSeconds (400)),
102 .AddTraceSource ("Count",
103 "Cobalt count",
105 "ns3::TracedValueCallback::Uint32")
106 .AddTraceSource ("DropState",
107 "Dropping state",
109 "ns3::TracedValueCallback::Bool")
110 .AddTraceSource ("DropNext",
111 "Time until next packet drop",
113 "ns3::TracedValueCallback::Uint32")
114 ;
115
116 return tid;
117}
118
126/* borrowed from the linux kernel */
128{
129 return (uint32_t)(((uint64_t)A * R) >> 32);
130}
131
136static int64_t CoDelGetTime (void)
137{
138 Time time = Simulator::Now ();
139 int64_t ns = time.GetNanoSeconds ();
140
141 return ns;
142}
143
145 : QueueDisc ()
146{
147 NS_LOG_FUNCTION (this);
149 m_uv = CreateObject<UniformRandomVariable> ();
150}
151
153{
154 return m_pDrop;
155}
156
158{
159 NS_LOG_FUNCTION (this);
160}
161
162int64_t
164{
165 NS_LOG_FUNCTION (this << stream);
166 m_uv->SetStream (stream);
167 return 1;
168}
169
170void
172{
173 // Cobalt parameters
174 NS_LOG_FUNCTION (this);
175 m_recInvSqrtCache[0] = ~0;
176 CacheInit ();
177 m_count = 0;
178 m_dropping = false;
179 m_recInvSqrt = ~0U;
181 m_dropNext = 0;
182}
183
184bool
186{
187 return ((int64_t)(a) - (int64_t)(b) > 0);
188}
189
190bool
192{
193 return ((int64_t)(a) - (int64_t)(b) >= 0);
194}
195
196int64_t
198{
199 return (t.GetNanoSeconds ());
200}
201
202Time
204{
205 return m_target;
206}
207
208Time
210{
211 return m_interval;
212}
213
214int64_t
216{
217 return m_dropNext;
218}
219
220void
222{
223 NS_LOG_FUNCTION (this);
224 uint32_t invsqrt = ((uint32_t) m_recInvSqrt);
225 uint32_t invsqrt2 = ((uint64_t) invsqrt * invsqrt) >> 32;
226 uint64_t val = (3ll << 32) - ((uint64_t) m_count * invsqrt2);
227
228 val >>= 2; /* avoid overflow */
229 val = (val * invsqrt) >> (32 - 2 + 1);
230 m_recInvSqrt = val;
231}
232
233void
235{
236 m_recInvSqrt = ~0U;
238
240 {
241 NewtonStep ();
242 NewtonStep ();
243 NewtonStep ();
244 NewtonStep ();
246 }
247}
248
249void
251{
253 {
255 }
256 else
257 {
258 NewtonStep ();
259 }
260}
261
262int64_t
264{
265 NS_LOG_FUNCTION (this);
267}
268
269void
271{
272 NS_LOG_FUNCTION (this);
273 m_uv = 0;
275}
276
279{
280 NS_LOG_FUNCTION (this);
281 if (GetInternalQueue (0)->IsEmpty ())
282 {
283 NS_LOG_LOGIC ("Queue empty");
284 return 0;
285 }
286
287 Ptr<const QueueDiscItem> item = GetInternalQueue (0)->Peek ();
288
289 NS_LOG_LOGIC ("Number packets " << GetInternalQueue (0)->GetNPackets ());
290 NS_LOG_LOGIC ("Number bytes " << GetInternalQueue (0)->GetNBytes ());
291
292 return item;
293}
294
295bool
297{
298 NS_LOG_FUNCTION (this);
299 if (GetNQueueDiscClasses () > 0)
300 {
301 NS_LOG_ERROR ("CobaltQueueDisc cannot have classes");
302 return false;
303 }
304
305 if (GetNPacketFilters () > 0)
306 {
307 NS_LOG_ERROR ("CobaltQueueDisc cannot have packet filters");
308 return false;
309 }
310
311 if (GetNInternalQueues () == 0)
312 {
313
315 ("MaxSize", QueueSizeValue (GetMaxSize ())));
316 }
317
318
319 if (GetNInternalQueues () != 1)
320 {
321 NS_LOG_ERROR ("CobaltQueueDisc needs 1 internal queue");
322 return false;
323 }
324 return true;
325}
326
327bool
329{
330 NS_LOG_FUNCTION (this << item);
331 Ptr<Packet> p = item->GetPacket ();
332 if (GetCurrentSize () + item > GetMaxSize ())
333 {
334 NS_LOG_LOGIC ("Queue full -- dropping pkt");
335 int64_t now = CoDelGetTime ();
336 // Call this to update Blue's drop probability
337 CobaltQueueFull (now);
339 return false;
340 }
341
342 bool retval = GetInternalQueue (0)->Enqueue (item);
343
344 // If Queue::Enqueue fails, QueueDisc::Drop is called by the internal queue
345 // because QueueDisc::AddInternalQueue sets the drop callback
346
347 NS_LOG_LOGIC ("Number packets " << GetInternalQueue (0)->GetNPackets ());
348 NS_LOG_LOGIC ("Number bytes " << GetInternalQueue (0)->GetNBytes ());
349
350 return retval;
351}
352
355{
356 NS_LOG_FUNCTION (this);
357
358 while (1)
359 {
360 Ptr<QueueDiscItem> item = GetInternalQueue (0)->Dequeue ();
361 if (!item)
362 {
363 // Leave dropping state when queue is empty (derived from Codel)
364 m_dropping = false;
365 NS_LOG_LOGIC ("Queue empty");
366 int64_t now = CoDelGetTime ();
367 // Call this to update Blue's drop probability
368 CobaltQueueEmpty (now);
369 return 0;
370 }
371
372 int64_t now = CoDelGetTime ();
373
374 NS_LOG_LOGIC ("Popped " << item);
375 NS_LOG_LOGIC ("Number packets remaining " << GetInternalQueue (0)->GetNPackets ());
376 NS_LOG_LOGIC ("Number bytes remaining " << GetInternalQueue (0)->GetNBytes ());
377
378 // Determine if item should be dropped
379 // ECN marking happens inside this function, so it need not be done here
380 bool drop = CobaltShouldDrop (item, now);
381
382 if (drop)
383 {
385 }
386 else
387 {
388 return item;
389 }
390 }
391}
392
393// Call this when a packet had to be dropped due to queue overflow.
395{
396 NS_LOG_FUNCTION (this);
397 NS_LOG_LOGIC ("Outside IF block");
399 {
400 NS_LOG_LOGIC ("inside IF block");
401 m_pDrop = std::min (m_pDrop + m_increment, (double)1.0);
403 }
404 m_dropping = true;
405 m_dropNext = now;
406 if (!m_count)
407 {
408 m_count = 1;
409 }
410}
411
412// Call this when the queue was serviced but turned out to be empty.
414{
415 NS_LOG_FUNCTION (this);
417 {
418 m_pDrop = std::max (m_pDrop - m_decrement, (double)0.0);
420 }
421 m_dropping = false;
422
423 if (m_count && CoDelTimeAfterEq ((now - m_dropNext), 0))
424 {
425 m_count--;
426 InvSqrt ();
428 }
429}
430
431// Determines if Cobalt should drop the packet
433{
434 NS_LOG_FUNCTION (this);
435 bool drop = false;
436
437 /* Simplified Codel implementation */
438 Time delta = Simulator::Now () - item->GetTimeStamp ();
439 NS_LOG_INFO ("Sojourn time " << delta.As (Time::S));
440 int64_t sojournTime = Time2CoDel (delta);
441 int64_t schedule = now - m_dropNext;
442 bool over_target = CoDelTimeAfter (sojournTime, Time2CoDel (m_target));
443 bool next_due = m_count && schedule >= 0;
444 bool isMarked = false;
445
446 // If L4S mode is enabled then check if the packet is ECT1 or CE and
447 // if sojourn time is greater than CE threshold then the packet is marked.
448 // If packet is marked succesfully then the CoDel steps can be skipped.
449 if (item && m_useL4s)
450 {
451 uint8_t tosByte = 0;
452 if (item->GetUint8Value (QueueItem::IP_DSFIELD, tosByte) && (((tosByte & 0x3) == 1) || (tosByte & 0x3) == 3))
453 {
454 if ((tosByte & 0x3) == 1)
455 {
456 NS_LOG_DEBUG ("ECT1 packet " << static_cast<uint16_t> (tosByte & 0x3));
457 }
458 else
459 {
460 NS_LOG_DEBUG ("CE packet " << static_cast<uint16_t> (tosByte & 0x3));
461 }
463 {
464 NS_LOG_LOGIC ("Marking due to CeThreshold " << m_ceThreshold.GetSeconds ());
465 }
466 return false;
467 }
468 }
469
470 if (over_target)
471 {
472 if (!m_dropping)
473 {
474 m_dropping = true;
475 m_dropNext = ControlLaw (now);
476 }
477 if (!m_count)
478 {
479 m_count = 1;
480 }
481 }
482 else if (m_dropping)
483 {
484 m_dropping = false;
485 }
486
487 if (next_due && m_dropping)
488 {
489 /* Check for marking possibility only if BLUE decides NOT to drop. */
490 /* Check if router and packet, both have ECN enabled. Only if this is true, mark the packet. */
491 isMarked = (m_useEcn && Mark (item, FORCED_MARK));
492 drop = !isMarked;
493
495
496 InvSqrt ();
498 schedule = now - m_dropNext;
499 }
500 else
501 {
502 while (next_due)
503 {
504 m_count--;
505 InvSqrt ();
507 schedule = now - m_dropNext;
508 next_due = m_count && schedule >= 0;
509 }
510 }
511
512 // If CE threshold is enabled then isMarked flag is used to determine whether
513 // packet is marked and if the packet is marked then a second attempt at marking should be suppressed.
514 // If UseL4S attribute is enabled then ECT0 packets should not be marked.
515 if (!isMarked && !m_useL4s && m_useEcn && CoDelTimeAfter (sojournTime, Time2CoDel (m_ceThreshold)) && Mark (item, CE_THRESHOLD_EXCEEDED_MARK))
516 {
517 NS_LOG_LOGIC ("Marking due to CeThreshold " << m_ceThreshold.GetSeconds ());
518 }
519
520 // Enable Blue Enhancement if sojourn time is greater than blueThreshold and its been m_target time until the last time blue was updated
522 {
523 m_pDrop = std::min (m_pDrop + m_increment, (double)1.0);
525 }
526
527 /* Simple BLUE implementation. Lack of ECN is deliberate. */
528 if (m_pDrop)
529 {
530 double u = m_uv->GetValue ();
531 drop = drop | (u < m_pDrop);
532 }
533
534 /* Overload the drop_next field as an activity timeout */
535 if (!m_count)
536 {
538 }
539 else if (schedule > 0 && !drop)
540 {
541 m_dropNext = now;
542 }
543
544 return drop;
545}
546
547} // namespace ns3
#define min(a, b)
Definition: 80211b.c:42
#define max(a, b)
Definition: 80211b.c:43
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Cobalt packet queue disc.
Time m_ceThreshold
Threshold above which to CE mark.
uint32_t m_recInvSqrtCache[REC_INV_SQRT_CACHE]
Cache to maintain some initial values of InvSqrt.
Time m_target
target queue delay
static constexpr const char * CE_THRESHOLD_EXCEEDED_MARK
Sojourn time above CE threshold.
virtual Ptr< QueueDiscItem > DoDequeue(void)
This function actually extracts a packet from the queue disc.
bool CoDelTimeAfterEq(int64_t a, int64_t b)
Check if CoDel time a is successive or equal to b.
virtual Ptr< const QueueDiscItem > DoPeek(void)
Return a copy of the next packet the queue disc will extract.
double GetPdrop() const
Get the drop probability of Blue.
int64_t AssignStreams(int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
virtual void DoDispose(void)
Dispose of the object.
virtual bool DoEnqueue(Ptr< QueueDiscItem > item)
This function actually enqueues a packet into the queue disc.
int64_t GetDropNext(void) const
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.
virtual bool CheckConfig(void)
Check whether the current configuration is correct.
double m_increment
increment value for marking probability
void CacheInit(void)
There is a big difference in timing between the accurate values placed in the cache and the approxima...
void CobaltQueueFull(int64_t now)
Called when the queue becomes full to alter the drop probabilities of Blue.
Time m_interval
sliding minimum time window width
double m_decrement
decrement value for marking probability
bool CobaltShouldDrop(Ptr< QueueDiscItem > item, int64_t now)
Called to decide whether the current packet should be dropped based on decisions taken by Blue and Co...
void InvSqrt(void)
Updates the inverse square root.
Time GetTarget(void) const
Get the target queue delay.
static constexpr const char * TARGET_EXCEEDED_DROP
Sojourn time above target.
int64_t Time2CoDel(Time t) const
Return the unsigned 32-bit integer representation of the input Time object.
bool CoDelTimeAfter(int64_t a, int64_t b)
Check if CoDel time a is successive to b.
double m_pDrop
Drop Probability.
virtual void InitializeParams(void)
Initialize the queue parameters.
CobaltQueueDisc()
CobaltQueueDisc Constructor.
int64_t ControlLaw(int64_t t)
Determine the time for next drop CoDel control law is t + m_interval/sqrt(m_count).
static constexpr const char * FORCED_MARK
forced marks by Codel on ECN-enabled
virtual ~CobaltQueueDisc()
Destructor.
Time m_blueThreshold
Threshold to enable blue enhancement.
uint32_t m_recInvSqrt
Reciprocal inverse square root.
Ptr< UniformRandomVariable > m_uv
Rng stream.
uint32_t m_lastUpdateTimeBlue
Blue's last update time for drop probability.
static constexpr const char * OVERLIMIT_DROP
Overlimit dropped packet.
Time GetInterval(void) const
Get the interval.
TracedValue< bool > m_dropping
True if in dropping state.
bool m_useEcn
True if ECN is used (packets are marked instead of being dropped)
TracedValue< int64_t > m_dropNext
Time to drop next packet.
void NewtonStep(void)
Calculate the reciprocal square root of m_count by using Newton's method http://en....
void CobaltQueueEmpty(int64_t now)
Called when the queue becomes empty to alter the drop probabilities of Blue.
bool m_useL4s
True if L4S is used (ECT1 packets are marked at CE threshold)
static TypeId GetTypeId(void)
Get the type ID.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
Introspection did not find any typical Config paths.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:181
void AddInternalQueue(Ptr< InternalQueue > queue)
Add an internal queue to the tail of the list of queues.
Definition: queue-disc.cc:579
QueueSize GetCurrentSize(void)
Get the current size of the queue disc in bytes, if operating in bytes mode, or packets,...
Definition: queue-disc.cc:521
uint32_t GetNBytes(void) const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:445
QueueSize GetMaxSize(void) const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:452
Ptr< InternalQueue > GetInternalQueue(std::size_t i) const
Get the i-th internal queue.
Definition: queue-disc.cc:599
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 GetNPacketFilters(void) const
Get the number of packet filters.
Definition: queue-disc.cc:626
uint32_t GetNPackets(void) const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:438
std::size_t GetNQueueDiscClasses(void) const
Get the number of queue disc classes.
Definition: queue-disc.cc:667
bool SetMaxSize(QueueSize size)
Set the maximum size of the queue disc.
Definition: queue-disc.cc:480
virtual void DoDispose(void)
Dispose of the object.
Definition: queue-disc.cc:382
std::size_t GetNInternalQueues(void) const
Get the number of internal queues.
Definition: queue-disc.cc:606
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:816
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:727
Class for representing queue sizes.
Definition: queue-size.h:95
AttributeValue implementation for QueueSize.
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
Hold variables of type string.
Definition: string.h:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
@ S
second
Definition: nstime.h:114
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition: nstime.h:282
TimeWithUnit As(const enum Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition: time.cc:432
int64_t GetNanoSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:391
AttributeValue implementation for Time.
Definition: nstime.h:1308
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
#define DEFAULT_COBALT_LIMIT
#define REC_INV_SQRT_CACHE
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Definition: boolean.h:85
Ptr< const AttributeAccessor > MakeDoubleAccessor(T1 a1)
Definition: double.h:42
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Definition: nstime.h:1309
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#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:281
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:45
@ BYTES
Use number of bytes for queue size.
Definition: queue-size.h:45
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static int64_t CoDelGetTime(void)
Returns the current time translated in CoDel time representation.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:536
static uint32_t ReciprocalDivide(uint32_t A, uint32_t R)
Performs a reciprocal divide, similar to the Linux kernel reciprocal_divide function.