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  ;
167  return tid;
168 }
169 
172  m_quantum (0)
173 {
174  NS_LOG_FUNCTION (this);
175 }
176 
178 {
179  NS_LOG_FUNCTION (this);
180 }
181 
182 void
184 {
185  NS_LOG_FUNCTION (this << quantum);
186  m_quantum = quantum;
187 }
188 
189 uint32_t
191 {
192  return m_quantum;
193 }
194 
195 uint32_t
197 {
198  NS_LOG_FUNCTION (this << flowHash);
199 
200  uint32_t h = (flowHash % m_flows);
201  uint32_t innerHash = h % m_setWays;
202  uint32_t outerHash = h - innerHash;
203 
204  for (uint32_t i = outerHash; i < outerHash + m_setWays; i++)
205  {
206  auto it = m_flowsIndices.find (i);
207 
208  if (it == m_flowsIndices.end ()
209  || (m_tags.find (i) != m_tags.end () && m_tags[i] == flowHash)
210  || StaticCast<FqCoDelFlow> (GetQueueDiscClass (it->second))->GetStatus () == FqCoDelFlow::INACTIVE)
211  {
212  // this queue has not been created yet or is associated with this flow
213  // or is inactive, hence we can use it
214  m_tags[i] = flowHash;
215  return i;
216  }
217  }
218 
219  // all the queues of the set are used. Use the first queue of the set
220  m_tags[outerHash] = flowHash;
221  return outerHash;
222 }
223 
224 bool
226 {
227  NS_LOG_FUNCTION (this << item);
228 
229  uint32_t flowHash, h;
230 
231  if (GetNPacketFilters () == 0)
232  {
233  flowHash = item->Hash (m_perturbation);
234  }
235  else
236  {
237  int32_t ret = Classify (item);
238 
239  if (ret != PacketFilter::PF_NO_MATCH)
240  {
241  flowHash = static_cast<uint32_t> (ret);
242  }
243  else
244  {
245  NS_LOG_ERROR ("No filter has been able to classify this packet, drop it.");
247  return false;
248  }
249  }
250 
252  {
253  h = SetAssociativeHash (flowHash);
254  }
255  else
256  {
257  h = flowHash % m_flows;
258  }
259 
260  Ptr<FqCoDelFlow> flow;
261  if (m_flowsIndices.find (h) == m_flowsIndices.end ())
262  {
263  NS_LOG_DEBUG ("Creating a new flow queue with index " << h);
264  flow = m_flowFactory.Create<FqCoDelFlow> ();
266  // If CoDel, Set values of CoDelQueueDisc to match this QueueDisc
267  Ptr<CoDelQueueDisc> codel = qd->GetObject<CoDelQueueDisc> ();
268  if (codel)
269  {
270  codel->SetAttribute ("UseEcn", BooleanValue (m_useEcn));
271  codel->SetAttribute ("CeThreshold", TimeValue (m_ceThreshold));
272  }
273  qd->Initialize ();
274  flow->SetQueueDisc (qd);
275  flow->SetIndex (h);
276  AddQueueDiscClass (flow);
277 
279  }
280  else
281  {
282  flow = StaticCast<FqCoDelFlow> (GetQueueDiscClass (m_flowsIndices[h]));
283  }
284 
285  if (flow->GetStatus () == FqCoDelFlow::INACTIVE)
286  {
287  flow->SetStatus (FqCoDelFlow::NEW_FLOW);
288  flow->SetDeficit (m_quantum);
289  m_newFlows.push_back (flow);
290  }
291 
292  flow->GetQueueDisc ()->Enqueue (item);
293 
294  NS_LOG_DEBUG ("Packet enqueued into flow " << h << "; flow index " << m_flowsIndices[h]);
295 
296  if (GetCurrentSize () > GetMaxSize ())
297  {
298  NS_LOG_DEBUG ("Overload; enter FqCodelDrop ()");
299  FqCoDelDrop ();
300  }
301 
302  return true;
303 }
304 
307 {
308  NS_LOG_FUNCTION (this);
309 
310  Ptr<FqCoDelFlow> flow;
311  Ptr<QueueDiscItem> item;
312 
313  do
314  {
315  bool found = false;
316 
317  while (!found && !m_newFlows.empty ())
318  {
319  flow = m_newFlows.front ();
320 
321  if (flow->GetDeficit () <= 0)
322  {
323  NS_LOG_DEBUG ("Increase deficit for new flow index " << flow->GetIndex ());
324  flow->IncreaseDeficit (m_quantum);
325  flow->SetStatus (FqCoDelFlow::OLD_FLOW);
326  m_oldFlows.push_back (flow);
327  m_newFlows.pop_front ();
328  }
329  else
330  {
331  NS_LOG_DEBUG ("Found a new flow " << flow->GetIndex () << " with positive deficit");
332  found = true;
333  }
334  }
335 
336  while (!found && !m_oldFlows.empty ())
337  {
338  flow = m_oldFlows.front ();
339 
340  if (flow->GetDeficit () <= 0)
341  {
342  NS_LOG_DEBUG ("Increase deficit for old flow index " << flow->GetIndex ());
343  flow->IncreaseDeficit (m_quantum);
344  m_oldFlows.push_back (flow);
345  m_oldFlows.pop_front ();
346  }
347  else
348  {
349  NS_LOG_DEBUG ("Found an old flow " << flow->GetIndex () << " with positive deficit");
350  found = true;
351  }
352  }
353 
354  if (!found)
355  {
356  NS_LOG_DEBUG ("No flow found to dequeue a packet");
357  return 0;
358  }
359 
360  item = flow->GetQueueDisc ()->Dequeue ();
361 
362  if (!item)
363  {
364  NS_LOG_DEBUG ("Could not get a packet from the selected flow queue");
365  if (!m_newFlows.empty ())
366  {
367  flow->SetStatus (FqCoDelFlow::OLD_FLOW);
368  m_oldFlows.push_back (flow);
369  m_newFlows.pop_front ();
370  }
371  else
372  {
373  flow->SetStatus (FqCoDelFlow::INACTIVE);
374  m_oldFlows.pop_front ();
375  }
376  }
377  else
378  {
379  NS_LOG_DEBUG ("Dequeued packet " << item->GetPacket ());
380  }
381  } while (item == 0);
382 
383  flow->IncreaseDeficit (item->GetSize () * -1);
384 
385  return item;
386 }
387 
388 bool
390 {
391  NS_LOG_FUNCTION (this);
392  if (GetNQueueDiscClasses () > 0)
393  {
394  NS_LOG_ERROR ("FqCoDelQueueDisc cannot have classes");
395  return false;
396  }
397 
398  if (GetNInternalQueues () > 0)
399  {
400  NS_LOG_ERROR ("FqCoDelQueueDisc cannot have internal queues");
401  return false;
402  }
403 
404  // we are at initialization time. If the user has not set a quantum value,
405  // set the quantum to the MTU of the device (if any)
406  if (!m_quantum)
407  {
409  Ptr<NetDevice> dev;
410  // if the NetDeviceQueueInterface object is aggregated to a
411  // NetDevice, get the MTU of such NetDevice
412  if (ndqi && (dev = ndqi->GetObject<NetDevice> ()))
413  {
414  m_quantum = dev->GetMtu ();
415  NS_LOG_DEBUG ("Setting the quantum to the MTU of the device: " << m_quantum);
416  }
417 
418  if (!m_quantum)
419  {
420  NS_LOG_ERROR ("The quantum parameter cannot be null");
421  return false;
422  }
423  }
424 
426  {
427  NS_LOG_ERROR ("The number of queues must be an integer multiple of the size "
428  "of the set of queues used by set associative hash");
429  return false;
430  }
431 
432  return true;
433 }
434 
435 void
437 {
438  NS_LOG_FUNCTION (this);
439 
440  m_flowFactory.SetTypeId ("ns3::FqCoDelFlow");
441 
442  m_queueDiscFactory.SetTypeId ("ns3::CoDelQueueDisc");
446 }
447 
448 uint32_t
450 {
451  NS_LOG_FUNCTION (this);
452 
453  uint32_t maxBacklog = 0, index = 0;
454  Ptr<QueueDisc> qd;
455 
456  /* Queue is full! Find the fat flow and drop packet(s) from it */
457  for (uint32_t i = 0; i < GetNQueueDiscClasses (); i++)
458  {
459  qd = GetQueueDiscClass (i)->GetQueueDisc ();
460  uint32_t bytes = qd->GetNBytes ();
461  if (bytes > maxBacklog)
462  {
463  maxBacklog = bytes;
464  index = i;
465  }
466  }
467 
468  /* Our goal is to drop half of this fat flow backlog */
469  uint32_t len = 0, count = 0, threshold = maxBacklog >> 1;
470  qd = GetQueueDiscClass (index)->GetQueueDisc ();
471  Ptr<QueueDiscItem> item;
472 
473  do
474  {
475  NS_LOG_DEBUG ("Drop packet (overflow); count: " << count << " len: " << len << " threshold: " << threshold);
476  item = qd->GetInternalQueue (0)->Dequeue ();
478  len += item->GetSize ();
479  } while (++count < m_dropBatchSize && len < threshold);
480 
481  return index;
482 }
483 
484 } // namespace ns3
485 
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.
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)
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.
Definition: nstime.h:279
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:1124
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.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
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:1125
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
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
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:449
#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