A Discrete-Event Network Simulator
API
fq-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) 2016 Universita' degli Studi di Napoli Federico II
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: Pasquale Imputato <p.imputato@gmail.com>
19  * Stefano Avallone <stefano.avallone@unina.it>
20 */
21 
22 #include "ns3/log.h"
23 #include "ns3/string.h"
24 #include "ns3/queue.h"
25 #include "fq-codel-queue-disc.h"
26 #include "codel-queue-disc.h"
27 #include "ns3/net-device-queue-interface.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("FqCoDelQueueDisc");
32 
33 NS_OBJECT_ENSURE_REGISTERED (FqCoDelFlow);
34 
36 {
37  static TypeId tid = TypeId ("ns3::FqCoDelFlow")
39  .SetGroupName ("TrafficControl")
40  .AddConstructor<FqCoDelFlow> ()
41  ;
42  return tid;
43 }
44 
46  : m_deficit (0),
47  m_status (INACTIVE),
48  m_index (0)
49 {
50  NS_LOG_FUNCTION (this);
51 }
52 
54 {
55  NS_LOG_FUNCTION (this);
56 }
57 
58 void
59 FqCoDelFlow::SetDeficit (uint32_t deficit)
60 {
61  NS_LOG_FUNCTION (this << deficit);
62  m_deficit = deficit;
63 }
64 
65 int32_t
67 {
68  NS_LOG_FUNCTION (this);
69  return m_deficit;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION (this << deficit);
76  m_deficit += deficit;
77 }
78 
79 void
81 {
82  NS_LOG_FUNCTION (this);
83  m_status = status;
84 }
85 
88 {
89  NS_LOG_FUNCTION (this);
90  return m_status;
91 }
92 
93 void
94 FqCoDelFlow::SetIndex (uint32_t index)
95 {
96  NS_LOG_FUNCTION (this);
97  m_index = index;
98 }
99 
100 uint32_t
102 {
103  return m_index;
104 }
105 
106 
108 
110 {
111  static TypeId tid = TypeId ("ns3::FqCoDelQueueDisc")
112  .SetParent<QueueDisc> ()
113  .SetGroupName ("TrafficControl")
114  .AddConstructor<FqCoDelQueueDisc> ()
115  .AddAttribute ("UseEcn",
116  "True to use ECN (packets are marked instead of being dropped)",
117  BooleanValue (true),
120  .AddAttribute ("Interval",
121  "The CoDel algorithm interval for each FQCoDel queue",
122  StringValue ("100ms"),
125  .AddAttribute ("Target",
126  "The CoDel algorithm target queue delay for each FQCoDel queue",
127  StringValue ("5ms"),
130  .AddAttribute ("MaxSize",
131  "The maximum number of packets accepted by this queue disc",
132  QueueSizeValue (QueueSize ("10240p")),
136  .AddAttribute ("Flows",
137  "The number of queues into which the incoming packets are classified",
138  UintegerValue (1024),
140  MakeUintegerChecker<uint32_t> ())
141  .AddAttribute ("DropBatchSize",
142  "The maximum number of packets dropped from the fat flow",
143  UintegerValue (64),
145  MakeUintegerChecker<uint32_t> ())
146  .AddAttribute ("Perturbation",
147  "The salt used as an additional input to the hash function used to classify packets",
148  UintegerValue (0),
150  MakeUintegerChecker<uint32_t> ())
151  .AddAttribute ("CeThreshold",
152  "The FqCoDel CE threshold for marking packets",
153  TimeValue (Time::Max ()),
155  MakeTimeChecker ())
156  .AddAttribute ("EnableSetAssociativeHash",
157  "Enable/Disable Set Associative Hash",
158  BooleanValue (false),
161  .AddAttribute ("SetWays",
162  "The size of a set of queues (used by set associative hash)",
163  UintegerValue (8),
165  MakeUintegerChecker<uint32_t> ())
166  .AddAttribute ("UseL4s",
167  "True to use L4S (only ECT1 packets are marked at CE threshold)",
168  BooleanValue (false),
171  ;
172  return tid;
173 }
174 
177  m_quantum (0)
178 {
179  NS_LOG_FUNCTION (this);
180 }
181 
183 {
184  NS_LOG_FUNCTION (this);
185 }
186 
187 void
189 {
190  NS_LOG_FUNCTION (this << quantum);
191  m_quantum = quantum;
192 }
193 
194 uint32_t
196 {
197  return m_quantum;
198 }
199 
200 uint32_t
202 {
203  NS_LOG_FUNCTION (this << flowHash);
204 
205  uint32_t h = (flowHash % m_flows);
206  uint32_t innerHash = h % m_setWays;
207  uint32_t outerHash = h - innerHash;
208 
209  for (uint32_t i = outerHash; i < outerHash + m_setWays; i++)
210  {
211  auto it = m_flowsIndices.find (i);
212 
213  if (it == m_flowsIndices.end ()
214  || (m_tags.find (i) != m_tags.end () && m_tags[i] == flowHash)
215  || StaticCast<FqCoDelFlow> (GetQueueDiscClass (it->second))->GetStatus () == FqCoDelFlow::INACTIVE)
216  {
217  // this queue has not been created yet or is associated with this flow
218  // or is inactive, hence we can use it
219  m_tags[i] = flowHash;
220  return i;
221  }
222  }
223 
224  // all the queues of the set are used. Use the first queue of the set
225  m_tags[outerHash] = flowHash;
226  return outerHash;
227 }
228 
229 bool
231 {
232  NS_LOG_FUNCTION (this << item);
233 
234  uint32_t flowHash, h;
235 
236  if (GetNPacketFilters () == 0)
237  {
238  flowHash = item->Hash (m_perturbation);
239  }
240  else
241  {
242  int32_t ret = Classify (item);
243 
244  if (ret != PacketFilter::PF_NO_MATCH)
245  {
246  flowHash = static_cast<uint32_t> (ret);
247  }
248  else
249  {
250  NS_LOG_ERROR ("No filter has been able to classify this packet, drop it.");
252  return false;
253  }
254  }
255 
257  {
258  h = SetAssociativeHash (flowHash);
259  }
260  else
261  {
262  h = flowHash % m_flows;
263  }
264 
265  Ptr<FqCoDelFlow> flow;
266  if (m_flowsIndices.find (h) == m_flowsIndices.end ())
267  {
268  NS_LOG_DEBUG ("Creating a new flow queue with index " << h);
269  flow = m_flowFactory.Create<FqCoDelFlow> ();
271  // If CoDel, Set values of CoDelQueueDisc to match this QueueDisc
272  Ptr<CoDelQueueDisc> codel = qd->GetObject<CoDelQueueDisc> ();
273  if (codel)
274  {
275  codel->SetAttribute ("UseEcn", BooleanValue (m_useEcn));
276  codel->SetAttribute ("CeThreshold", TimeValue (m_ceThreshold));
277  codel->SetAttribute ("UseL4s", BooleanValue (m_useL4s));
278  }
279  qd->Initialize ();
280  flow->SetQueueDisc (qd);
281  flow->SetIndex (h);
282  AddQueueDiscClass (flow);
283 
285  }
286  else
287  {
288  flow = StaticCast<FqCoDelFlow> (GetQueueDiscClass (m_flowsIndices[h]));
289  }
290 
291  if (flow->GetStatus () == FqCoDelFlow::INACTIVE)
292  {
293  flow->SetStatus (FqCoDelFlow::NEW_FLOW);
294  flow->SetDeficit (m_quantum);
295  m_newFlows.push_back (flow);
296  }
297 
298  flow->GetQueueDisc ()->Enqueue (item);
299 
300  NS_LOG_DEBUG ("Packet enqueued into flow " << h << "; flow index " << m_flowsIndices[h]);
301 
302  if (GetCurrentSize () > GetMaxSize ())
303  {
304  NS_LOG_DEBUG ("Overload; enter FqCodelDrop ()");
305  FqCoDelDrop ();
306  }
307 
308  return true;
309 }
310 
313 {
314  NS_LOG_FUNCTION (this);
315 
316  Ptr<FqCoDelFlow> flow;
317  Ptr<QueueDiscItem> item;
318 
319  do
320  {
321  bool found = false;
322 
323  while (!found && !m_newFlows.empty ())
324  {
325  flow = m_newFlows.front ();
326 
327  if (flow->GetDeficit () <= 0)
328  {
329  NS_LOG_DEBUG ("Increase deficit for new flow index " << flow->GetIndex ());
330  flow->IncreaseDeficit (m_quantum);
331  flow->SetStatus (FqCoDelFlow::OLD_FLOW);
332  m_oldFlows.push_back (flow);
333  m_newFlows.pop_front ();
334  }
335  else
336  {
337  NS_LOG_DEBUG ("Found a new flow " << flow->GetIndex () << " with positive deficit");
338  found = true;
339  }
340  }
341 
342  while (!found && !m_oldFlows.empty ())
343  {
344  flow = m_oldFlows.front ();
345 
346  if (flow->GetDeficit () <= 0)
347  {
348  NS_LOG_DEBUG ("Increase deficit for old flow index " << flow->GetIndex ());
349  flow->IncreaseDeficit (m_quantum);
350  m_oldFlows.push_back (flow);
351  m_oldFlows.pop_front ();
352  }
353  else
354  {
355  NS_LOG_DEBUG ("Found an old flow " << flow->GetIndex () << " with positive deficit");
356  found = true;
357  }
358  }
359 
360  if (!found)
361  {
362  NS_LOG_DEBUG ("No flow found to dequeue a packet");
363  return 0;
364  }
365 
366  item = flow->GetQueueDisc ()->Dequeue ();
367 
368  if (!item)
369  {
370  NS_LOG_DEBUG ("Could not get a packet from the selected flow queue");
371  if (!m_newFlows.empty ())
372  {
373  flow->SetStatus (FqCoDelFlow::OLD_FLOW);
374  m_oldFlows.push_back (flow);
375  m_newFlows.pop_front ();
376  }
377  else
378  {
379  flow->SetStatus (FqCoDelFlow::INACTIVE);
380  m_oldFlows.pop_front ();
381  }
382  }
383  else
384  {
385  NS_LOG_DEBUG ("Dequeued packet " << item->GetPacket ());
386  }
387  } while (item == 0);
388 
389  flow->IncreaseDeficit (item->GetSize () * -1);
390 
391  return item;
392 }
393 
394 bool
396 {
397  NS_LOG_FUNCTION (this);
398  if (GetNQueueDiscClasses () > 0)
399  {
400  NS_LOG_ERROR ("FqCoDelQueueDisc cannot have classes");
401  return false;
402  }
403 
404  if (GetNInternalQueues () > 0)
405  {
406  NS_LOG_ERROR ("FqCoDelQueueDisc cannot have internal queues");
407  return false;
408  }
409 
410  // we are at initialization time. If the user has not set a quantum value,
411  // set the quantum to the MTU of the device (if any)
412  if (!m_quantum)
413  {
415  Ptr<NetDevice> dev;
416  // if the NetDeviceQueueInterface object is aggregated to a
417  // NetDevice, get the MTU of such NetDevice
418  if (ndqi && (dev = ndqi->GetObject<NetDevice> ()))
419  {
420  m_quantum = dev->GetMtu ();
421  NS_LOG_DEBUG ("Setting the quantum to the MTU of the device: " << m_quantum);
422  }
423 
424  if (!m_quantum)
425  {
426  NS_LOG_ERROR ("The quantum parameter cannot be null");
427  return false;
428  }
429  }
430 
432  {
433  NS_LOG_ERROR ("The number of queues must be an integer multiple of the size "
434  "of the set of queues used by set associative hash");
435  return false;
436  }
437 
438  if (m_useL4s)
439  {
440  NS_ABORT_MSG_IF (m_ceThreshold == Time::Max(), "CE threshold not set");
441  if (m_useEcn == false)
442  {
443  NS_LOG_WARN ("Enabling ECN as L4S mode is enabled");
444  }
445  }
446  return true;
447 }
448 
449 void
451 {
452  NS_LOG_FUNCTION (this);
453 
454  m_flowFactory.SetTypeId ("ns3::FqCoDelFlow");
455 
456  m_queueDiscFactory.SetTypeId ("ns3::CoDelQueueDisc");
460 }
461 
462 uint32_t
464 {
465  NS_LOG_FUNCTION (this);
466 
467  uint32_t maxBacklog = 0, index = 0;
468  Ptr<QueueDisc> qd;
469 
470  /* Queue is full! Find the fat flow and drop packet(s) from it */
471  for (uint32_t i = 0; i < GetNQueueDiscClasses (); i++)
472  {
473  qd = GetQueueDiscClass (i)->GetQueueDisc ();
474  uint32_t bytes = qd->GetNBytes ();
475  if (bytes > maxBacklog)
476  {
477  maxBacklog = bytes;
478  index = i;
479  }
480  }
481 
482  /* Our goal is to drop half of this fat flow backlog */
483  uint32_t len = 0, count = 0, threshold = maxBacklog >> 1;
484  qd = GetQueueDiscClass (index)->GetQueueDisc ();
485  Ptr<QueueDiscItem> item;
486 
487  do
488  {
489  NS_LOG_DEBUG ("Drop packet (overflow); count: " << count << " len: " << len << " threshold: " << threshold);
490  item = qd->GetInternalQueue (0)->Dequeue ();
492  len += item->GetSize ();
493  } while (++count < m_dropBatchSize && len < threshold);
494 
495  return index;
496 }
497 
498 } // namespace ns3
499 
void SetDeficit(uint32_t deficit)
Set the deficit for this flow.
A FqCoDel packet queue disc.
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 "...
AttributeValue implementation for Boolean.
Definition: boolean.h:36
Class for representing queue sizes.
Definition: queue-size.h:94
FlowStatus
Used to determine the status of this flow queue.
uint32_t m_quantum
Deficit assigned to flows at each round.
void AddQueueDiscClass(Ptr< QueueDiscClass > qdClass)
Add a queue disc class to the tail of the list of classes.
Definition: queue-disc.cc:634
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 const int PF_NO_MATCH
Standard value used by packet filters to indicate that no match was possible.
Definition: packet-filter.h:48
#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
QueueSizeUnit
Enumeration of the operating modes of queues.
Definition: queue-size.h:42
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
uint32_t FqCoDelDrop(void)
Drop a packet from the head of the queue with the largest current byte count.
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
uint32_t m_dropBatchSize
Max number of packets dropped from the fat flow.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
int32_t GetDeficit(void) const
Get the deficit for this flow.
static TypeId GetTypeId(void)
Get the type ID.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
uint32_t GetNBytes(void) const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:447
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:181
uint32_t m_setWays
size of a set of queues (used by set associative hash)
bool m_useEcn
True if ECN is used (packets are marked instead of being dropped)
bool m_useL4s
True if L4S is used (ECT1 packets are marked at CE threshold)
Ptr< NetDeviceQueueInterface > GetNetDeviceQueueInterface(void) const
Definition: queue-disc.cc:546
FqCoDelFlow()
FqCoDelFlow constructor.
void SetStatus(FlowStatus status)
Set the status for this flow.
A flow queue used by the FqCoDel queue disc.
static Time Max()
Maximum representable Time Not to be confused with Max(Time,Time).
Definition: nstime.h:283
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
int32_t m_deficit
the deficit for this flow
uint32_t m_flows
Number of flow queues.
bool m_enableSetAssociativeHash
whether to enable set associative hash
AttributeValue implementation for Time.
Definition: nstime.h:1353
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
uint32_t SetAssociativeHash(uint32_t flowHash)
Compute the index of the queue for the flow having the given flowHash, according to the set associati...
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
Use number of packets for queue size.
Definition: queue-size.h:44
uint32_t GetQuantum(void) const
Get the quantum value.
uint32_t m_perturbation
hash perturbation value
virtual bool CheckConfig(void)
Check whether the current configuration is correct.
Ptr< const AttributeAccessor > MakeQueueSizeAccessor(T1 a1)
Definition: queue-size.h:221
int32_t Classify(Ptr< QueueDiscItem > item)
Classify a packet by calling the packet filters, one at a time, until either a filter able to classif...
Definition: queue-disc.cc:675
uint32_t GetIndex(void) const
Get the index of this flow.
uint32_t m_index
the index for this flow
Ptr< QueueDiscClass > GetQueueDiscClass(std::size_t i) const
Get the i-th queue disc class.
Definition: queue-disc.cc:662
Time m_ceThreshold
Threshold above which to CE mark.
static constexpr const char * UNCLASSIFIED_DROP
No packet filter able to classify packet.
QueueDiscClass is the base class for classes that are included in a queue disc.
Definition: queue-disc.h:49
Ptr< const AttributeChecker > MakeQueueSizeChecker(void)
Definition: queue-size.cc:29
std::list< Ptr< FqCoDelFlow > > m_newFlows
The list of new flows.
virtual Ptr< QueueDiscItem > DoDequeue(void)
This function actually extracts a packet from the queue disc.
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:669
std::string m_interval
CoDel interval attribute.
void SetQuantum(uint32_t quantum)
Set the quantum value.
std::string m_target
CoDel target attribute.
std::map< uint32_t, uint32_t > m_tags
Tags used by set associative hash.
ObjectFactory m_queueDiscFactory
Factory to create a new queue.
static TypeId GetTypeId(void)
Get the type ID.
std::map< uint32_t, uint32_t > m_flowsIndices
Map with the index of class for each flow.
void SetIndex(uint32_t index)
Set the index for this flow.
FqCoDelQueueDisc()
FqCoDelQueueDisc constructor.
A CoDel packet queue disc.
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:1354
virtual void InitializeParams(void)
Initialize parameters (if any) before the first packet is enqueued.
QueueSize GetMaxSize(void) const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:454
QueueDiscSizePolicy
Enumeration of the available policies to handle the queue disc size.
Definition: queue-disc.h:103
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
Used by queue discs with multiple internal queues/child queue discs.
Definition: queue-disc.h:107
Network layer to device interface.
Definition: net-device.h:95
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
std::size_t GetNPacketFilters(void) const
Get the number of packet filters.
Definition: queue-disc.cc:628
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
FlowStatus GetStatus(void) const
Get the status of this flow.
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
std::list< Ptr< FqCoDelFlow > > m_oldFlows
The list of old flows.
virtual bool DoEnqueue(Ptr< QueueDiscItem > item)
This function actually enqueues a packet into the queue disc.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:533
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
static constexpr const char * OVERLIMIT_DROP
Overlimit dropped packets.
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: string.h:42
void IncreaseDeficit(int32_t deficit)
Increase the deficit for this flow.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
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
FlowStatus m_status
the status of this flow
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
Inactive Period or unslotted CSMA-CA.
Definition: lr-wpan-mac.h:93
ObjectFactory m_flowFactory
Factory to create a new flow.
std::size_t GetNInternalQueues(void) const
Get the number of internal queues.
Definition: queue-disc.cc:608