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 {
49  NS_LOG_FUNCTION (this);
50 }
51 
53 {
54  NS_LOG_FUNCTION (this);
55 }
56 
57 void
58 FqCoDelFlow::SetDeficit (uint32_t deficit)
59 {
60  NS_LOG_FUNCTION (this << deficit);
61  m_deficit = deficit;
62 }
63 
64 int32_t
66 {
67  NS_LOG_FUNCTION (this);
68  return m_deficit;
69 }
70 
71 void
73 {
74  NS_LOG_FUNCTION (this << deficit);
75  m_deficit += deficit;
76 }
77 
78 void
80 {
81  NS_LOG_FUNCTION (this);
82  m_status = status;
83 }
84 
87 {
88  NS_LOG_FUNCTION (this);
89  return m_status;
90 }
91 
92 
94 
96 {
97  static TypeId tid = TypeId ("ns3::FqCoDelQueueDisc")
98  .SetParent<QueueDisc> ()
99  .SetGroupName ("TrafficControl")
100  .AddConstructor<FqCoDelQueueDisc> ()
101  .AddAttribute ("Interval",
102  "The CoDel algorithm interval for each FQCoDel queue",
103  StringValue ("100ms"),
106  .AddAttribute ("Target",
107  "The CoDel algorithm target queue delay for each FQCoDel queue",
108  StringValue ("5ms"),
111  .AddAttribute ("PacketLimit",
112  "The hard limit on the real queue size, measured in packets",
113  UintegerValue (10 * 1024),
116  MakeUintegerChecker<uint32_t> (),
118  "Use the MaxSize attribute instead")
119  .AddAttribute ("MaxSize",
120  "The maximum number of packets accepted by this queue disc",
121  QueueSizeValue (QueueSize ("0p")),
125  .AddAttribute ("Flows",
126  "The number of queues into which the incoming packets are classified",
127  UintegerValue (1024),
129  MakeUintegerChecker<uint32_t> ())
130  .AddAttribute ("DropBatchSize",
131  "The maximum number of packets dropped from the fat flow",
132  UintegerValue (64),
134  MakeUintegerChecker<uint32_t> ())
135  ;
136  return tid;
137 }
138 
141  m_quantum (0)
142 {
143  NS_LOG_FUNCTION (this);
144 }
145 
147 {
148  NS_LOG_FUNCTION (this);
149 }
150 
151 void
153 {
154  NS_LOG_FUNCTION (this << quantum);
155  m_quantum = quantum;
156 }
157 
158 uint32_t
160 {
161  return m_quantum;
162 }
163 
164 bool
166 {
167  NS_LOG_FUNCTION (this << item);
168 
169  int32_t ret = Classify (item);
170 
171  if (ret == PacketFilter::PF_NO_MATCH)
172  {
173  NS_LOG_ERROR ("No filter has been able to classify this packet, drop it.");
175  return false;
176  }
177 
178  uint32_t h = ret % m_flows;
179 
180  Ptr<FqCoDelFlow> flow;
181  if (m_flowsIndices.find (h) == m_flowsIndices.end ())
182  {
183  NS_LOG_DEBUG ("Creating a new flow queue with index " << h);
184  flow = m_flowFactory.Create<FqCoDelFlow> ();
186  qd->Initialize ();
187  flow->SetQueueDisc (qd);
188  AddQueueDiscClass (flow);
189 
191  }
192  else
193  {
194  flow = StaticCast<FqCoDelFlow> (GetQueueDiscClass (m_flowsIndices[h]));
195  }
196 
197  if (flow->GetStatus () == FqCoDelFlow::INACTIVE)
198  {
199  flow->SetStatus (FqCoDelFlow::NEW_FLOW);
200  flow->SetDeficit (m_quantum);
201  m_newFlows.push_back (flow);
202  }
203 
204  flow->GetQueueDisc ()->Enqueue (item);
205 
206  NS_LOG_DEBUG ("Packet enqueued into flow " << h << "; flow index " << m_flowsIndices[h]);
207 
208  if (GetCurrentSize () > GetMaxSize ())
209  {
210  FqCoDelDrop ();
211  }
212 
213  return true;
214 }
215 
218 {
219  NS_LOG_FUNCTION (this);
220 
221  Ptr<FqCoDelFlow> flow;
222  Ptr<QueueDiscItem> item;
223 
224  do
225  {
226  bool found = false;
227 
228  while (!found && !m_newFlows.empty ())
229  {
230  flow = m_newFlows.front ();
231 
232  if (flow->GetDeficit () <= 0)
233  {
234  flow->IncreaseDeficit (m_quantum);
235  flow->SetStatus (FqCoDelFlow::OLD_FLOW);
236  m_oldFlows.push_back (flow);
237  m_newFlows.pop_front ();
238  }
239  else
240  {
241  NS_LOG_DEBUG ("Found a new flow with positive deficit");
242  found = true;
243  }
244  }
245 
246  while (!found && !m_oldFlows.empty ())
247  {
248  flow = m_oldFlows.front ();
249 
250  if (flow->GetDeficit () <= 0)
251  {
252  flow->IncreaseDeficit (m_quantum);
253  m_oldFlows.push_back (flow);
254  m_oldFlows.pop_front ();
255  }
256  else
257  {
258  NS_LOG_DEBUG ("Found an old flow with positive deficit");
259  found = true;
260  }
261  }
262 
263  if (!found)
264  {
265  NS_LOG_DEBUG ("No flow found to dequeue a packet");
266  return 0;
267  }
268 
269  item = flow->GetQueueDisc ()->Dequeue ();
270 
271  if (!item)
272  {
273  NS_LOG_DEBUG ("Could not get a packet from the selected flow queue");
274  if (!m_newFlows.empty ())
275  {
276  flow->SetStatus (FqCoDelFlow::OLD_FLOW);
277  m_oldFlows.push_back (flow);
278  m_newFlows.pop_front ();
279  }
280  else
281  {
282  flow->SetStatus (FqCoDelFlow::INACTIVE);
283  m_oldFlows.pop_front ();
284  }
285  }
286  else
287  {
288  NS_LOG_DEBUG ("Dequeued packet " << item->GetPacket ());
289  }
290  } while (item == 0);
291 
292  flow->IncreaseDeficit (item->GetSize () * -1);
293 
294  return item;
295 }
296 
299 {
300  NS_LOG_FUNCTION (this);
301 
302  return PeekDequeued ();
303 }
304 
305 void
307 {
308  NS_LOG_FUNCTION (this << limit);
310 }
311 
312 uint32_t
314 {
315  NS_LOG_FUNCTION (this);
316  return GetMaxSize ().GetValue ();
317 }
318 
319 bool
321 {
322  NS_LOG_FUNCTION (this);
323  if (GetNQueueDiscClasses () > 0)
324  {
325  NS_LOG_ERROR ("FqCoDelQueueDisc cannot have classes");
326  return false;
327  }
328 
329  if (GetNPacketFilters () == 0)
330  {
331  NS_LOG_ERROR ("FqCoDelQueueDisc needs at least a packet filter");
332  return false;
333  }
334 
335  if (GetNInternalQueues () > 0)
336  {
337  NS_LOG_ERROR ("FqCoDelQueueDisc cannot have internal queues");
338  return false;
339  }
340 
341  return true;
342 }
343 
344 void
346 {
347  NS_LOG_FUNCTION (this);
348 
349  // we are at initialization time. If the user has not set a quantum value,
350  // set the quantum to the MTU of the device
351  if (!m_quantum)
352  {
353  Ptr<NetDevice> device = GetNetDevice ();
354  NS_ASSERT_MSG (device, "Device not set for the queue disc");
355  m_quantum = device->GetMtu ();
356  NS_LOG_DEBUG ("Setting the quantum to the MTU of the device: " << m_quantum);
357  }
358 
359  m_flowFactory.SetTypeId ("ns3::FqCoDelFlow");
360 
361  m_queueDiscFactory.SetTypeId ("ns3::CoDelQueueDisc");
365 }
366 
367 uint32_t
369 {
370  NS_LOG_FUNCTION (this);
371 
372  uint32_t maxBacklog = 0, index = 0;
373  Ptr<QueueDisc> qd;
374 
375  /* Queue is full! Find the fat flow and drop packet(s) from it */
376  for (uint32_t i = 0; i < GetNQueueDiscClasses (); i++)
377  {
378  qd = GetQueueDiscClass (i)->GetQueueDisc ();
379  uint32_t bytes = qd->GetNBytes ();
380  if (bytes > maxBacklog)
381  {
382  maxBacklog = bytes;
383  index = i;
384  }
385  }
386 
387  /* Our goal is to drop half of this fat flow backlog */
388  uint32_t len = 0, count = 0, threshold = maxBacklog >> 1;
389  qd = GetQueueDiscClass (index)->GetQueueDisc ();
390  Ptr<QueueDiscItem> item;
391 
392  do
393  {
394  item = qd->GetInternalQueue (0)->Dequeue ();
396  len += item->GetSize ();
397  } while (++count < m_dropBatchSize && len < threshold);
398 
399  return index;
400 }
401 
402 } // namespace ns3
void SetDeficit(uint32_t deficit)
Set the deficit for this flow.
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
A FqCoDel packet queue disc.
uint32_t GetNQueueDiscClasses(void) const
Get the number of queue disc classes.
Definition: queue-disc.cc:652
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 "...
Class for representing queue sizes.
Definition: queue-size.h:94
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:620
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:704
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
uint32_t GetNBytes(void) const
Get the amount of bytes stored by the queue disc.
Definition: queue-disc.cc:447
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.
uint32_t m_dropBatchSize
Max number of packets dropped from the fat 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:201
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
virtual Ptr< const QueueDiscItem > DoPeek(void)
This function returns a copy of the next packet the queue disc will extract.
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:181
uint32_t GetValue() const
Get the underlying value.
Definition: queue-size.cc:175
FqCoDelFlow()
FqCoDelFlow constructor.
void SetStatus(FlowStatus status)
Set the status for this flow.
A flow queue used by the FqCoDel queue disc.
Ptr< InternalQueue > GetInternalQueue(uint32_t i) const
Get the i-th internal queue.
Definition: queue-disc.cc:587
int32_t m_deficit
the deficit for this flow
uint32_t m_flows
Number of flow queues.
NS_ASSERT_MSG(false,"Ipv4AddressGenerator::MaskToIndex(): Impossible")
uint32_t GetNInternalQueues(void) const
Get the number of internal queues.
Definition: queue-disc.cc:594
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Hold an unsigned integer type.
Definition: uinteger.h:44
FlowStatus GetStatus(void) const
Get the status of this flow.
Use number of packets for queue size.
Definition: queue-size.h:44
virtual bool CheckConfig(void)
Check whether the current configuration is correct.
Ptr< const AttributeAccessor > MakeQueueSizeAccessor(T1 a1)
Definition: queue-size.h:221
Attribute or trace source is deprecated; user is warned.
Definition: type-id.h:72
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:658
uint32_t GetNPacketFilters(void) const
Get the number of packet filters.
Definition: queue-disc.cc:614
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.
int32_t GetDeficit(void) const
Get the deficit for this flow.
uint32_t GetQuantum(void) const
Get the quantum value.
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::string m_interval
CoDel interval attribute.
void SetQuantum(uint32_t quantum)
Set the quantum value.
std::string m_target
CoDel target attribute.
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.
FqCoDelQueueDisc()
FqCoDelQueueDisc constructor.
void Set(std::string name, const AttributeValue &value)
Set an attribute to be set during construction.
virtual void InitializeParams(void)
Initialize parameters (if any) before the first packet is enqueued.
uint32_t GetLimit(void) const
Get the limit of this queue disc.
QueueDiscSizePolicy
Enumeration of the available policies to handle the queue disc size.
Definition: queue-disc.h:103
Ptr< const QueueDiscItem > PeekDequeued(void)
Dequeue a packet and retain it in the queue disc as a requeued packet.
Definition: queue-disc.cc:880
Used by queue discs with multiple internal queues/child queue discs.
Definition: queue-disc.h:107
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
bool SetMaxSize(QueueSize size)
Set the maximum size of the queue disc.
Definition: queue-disc.cc:482
void SetLimit(uint32_t limit)
Set the limit of this queue disc.
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:743
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.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:253
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.
FlowStatus
Used to determine the status of this flow queue.
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:914
Ptr< NetDevice > GetNetDevice(void) const
Get the NetDevice on which this queue discipline is installed.
Definition: queue-disc.cc:546
void Initialize(void)
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:183
Ptr< QueueDiscClass > GetQueueDiscClass(uint32_t i) const
Get the i-th queue disc class.
Definition: queue-disc.cc:645
ObjectFactory m_flowFactory
Factory to create a new flow.
QueueSize GetMaxSize(void) const
Get the maximum size of the queue disc.
Definition: queue-disc.cc:454