A Discrete-Event Network Simulator
API
traffic-control-helper.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 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  * Author: Stefano Avallone <stavallo@unina.it>
19  */
20 
21 #include "ns3/log.h"
22 #include "ns3/abort.h"
23 #include "ns3/queue-disc.h"
24 #include "ns3/uinteger.h"
25 #include "ns3/pointer.h"
26 #include "ns3/traffic-control-layer.h"
27 #include "traffic-control-helper.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("TrafficControlHelper");
32 
34  : m_queueDiscFactory (factory)
35 {
36 }
37 
38 void
40 {
41  m_internalQueuesFactory.push_back (factory);
42 }
43 
44 void
46 {
47  m_packetFiltersFactory.push_back (factory);
48 }
49 
50 uint16_t
52 {
53  m_queueDiscClassesFactory.push_back (factory);
54  return m_queueDiscClassesFactory.size () - 1;
55 }
56 
57 void
58 QueueDiscFactory::SetChildQueueDisc (uint16_t classId, uint16_t handle)
59 {
60  NS_ABORT_MSG_IF (classId >= m_queueDiscClassesFactory.size (),
61  "Cannot attach a queue disc to a non existing class");
62  m_classIdChildHandleMap[classId] = handle;
63 }
64 
66 QueueDiscFactory::CreateQueueDisc (const std::vector<Ptr<QueueDisc> > & queueDiscs)
67 {
68  // create the queue disc
70 
71  // create and add the internal queues
72  for (std::vector<ObjectFactory>::iterator i = m_internalQueuesFactory.begin ();
73  i != m_internalQueuesFactory.end (); i++ )
74  {
75  qd->AddInternalQueue (i->Create<Queue> ());
76  }
77 
78  // create and add the packet filters
79  for (std::vector<ObjectFactory>::iterator i = m_packetFiltersFactory.begin ();
80  i != m_packetFiltersFactory.end (); i++ )
81  {
82  qd->AddPacketFilter (i->Create<PacketFilter> ());
83  }
84 
85  // create and add the queue disc classes
86  for (uint32_t i = 0; i < m_queueDiscClassesFactory.size (); i++)
87  {
88  // the class ID is given by the index i of the vector
90  "Cannot create a queue disc class with no attached queue disc");
91 
92  uint16_t handle = m_classIdChildHandleMap[i];
93  NS_ABORT_MSG_IF (handle >= queueDiscs.size () || queueDiscs[handle] == 0,
94  "A queue disc with handle " << handle << " has not been created yet");
95 
96  m_queueDiscClassesFactory[i].Set ("QueueDisc", PointerValue (queueDiscs[handle]));
97  qd->AddQueueDiscClass (m_queueDiscClassesFactory[i].Create<QueueDiscClass> ());
98  }
99 
100  return qd;
101 }
102 
103 
105 {
106 }
107 
110 {
111  TrafficControlHelper helper;
112  uint16_t handle = helper.SetRootQueueDisc ("ns3::PfifoFastQueueDisc");
113  helper.AddInternalQueues (handle, 3, "ns3::DropTailQueue", "MaxPackets", UintegerValue (1000));
114  helper.AddPacketFilter (handle, "ns3::PfifoFastIpv4PacketFilter");
115  helper.AddPacketFilter (handle, "ns3::PfifoFastIpv6PacketFilter");
116  return helper;
117 }
118 
119 uint16_t
121  std::string n01, const AttributeValue& v01,
122  std::string n02, const AttributeValue& v02,
123  std::string n03, const AttributeValue& v03,
124  std::string n04, const AttributeValue& v04,
125  std::string n05, const AttributeValue& v05,
126  std::string n06, const AttributeValue& v06,
127  std::string n07, const AttributeValue& v07,
128  std::string n08, const AttributeValue& v08,
129  std::string n09, const AttributeValue& v09,
130  std::string n10, const AttributeValue& v10,
131  std::string n11, const AttributeValue& v11,
132  std::string n12, const AttributeValue& v12,
133  std::string n13, const AttributeValue& v13,
134  std::string n14, const AttributeValue& v14,
135  std::string n15, const AttributeValue& v15)
136 {
137  NS_ABORT_MSG_UNLESS (m_queueDiscFactory.empty (), "A root queue disc has been already added to this factory");
138 
139  ObjectFactory factory;
140  factory.SetTypeId (type);
141  factory.Set (n01, v01);
142  factory.Set (n02, v02);
143  factory.Set (n03, v03);
144  factory.Set (n04, v04);
145  factory.Set (n05, v05);
146  factory.Set (n06, v06);
147  factory.Set (n07, v07);
148  factory.Set (n08, v08);
149  factory.Set (n09, v09);
150  factory.Set (n10, v10);
151  factory.Set (n11, v11);
152  factory.Set (n12, v12);
153  factory.Set (n13, v13);
154  factory.Set (n14, v14);
155  factory.Set (n15, v15);
156 
157  m_queueDiscFactory.push_back (QueueDiscFactory (factory));
158  return 0;
159 }
160 
161 void
162 TrafficControlHelper::AddInternalQueues (uint16_t handle, uint16_t count, std::string type,
163  std::string n01, const AttributeValue& v01,
164  std::string n02, const AttributeValue& v02,
165  std::string n03, const AttributeValue& v03,
166  std::string n04, const AttributeValue& v04,
167  std::string n05, const AttributeValue& v05,
168  std::string n06, const AttributeValue& v06,
169  std::string n07, const AttributeValue& v07,
170  std::string n08, const AttributeValue& v08)
171 {
172  NS_ABORT_MSG_IF (handle >= m_queueDiscFactory.size (), "A queue disc with handle "
173  << handle << " does not exist");
174 
175  ObjectFactory factory;
176  factory.SetTypeId (type);
177  factory.Set (n01, v01);
178  factory.Set (n02, v02);
179  factory.Set (n03, v03);
180  factory.Set (n04, v04);
181  factory.Set (n05, v05);
182  factory.Set (n06, v06);
183  factory.Set (n07, v07);
184  factory.Set (n08, v08);
185 
186  for (int i = 0; i < count; i++)
187  {
188  m_queueDiscFactory[handle].AddInternalQueue (factory);
189  }
190 }
191 
192 void
193 TrafficControlHelper::AddPacketFilter (uint16_t handle, std::string type,
194  std::string n01, const AttributeValue& v01,
195  std::string n02, const AttributeValue& v02,
196  std::string n03, const AttributeValue& v03,
197  std::string n04, const AttributeValue& v04,
198  std::string n05, const AttributeValue& v05,
199  std::string n06, const AttributeValue& v06,
200  std::string n07, const AttributeValue& v07,
201  std::string n08, const AttributeValue& v08)
202 {
203  NS_ABORT_MSG_IF (handle >= m_queueDiscFactory.size (), "A queue disc with handle "
204  << handle << " does not exist");
205 
206  ObjectFactory factory;
207  factory.SetTypeId (type);
208  factory.Set (n01, v01);
209  factory.Set (n02, v02);
210  factory.Set (n03, v03);
211  factory.Set (n04, v04);
212  factory.Set (n05, v05);
213  factory.Set (n06, v06);
214  factory.Set (n07, v07);
215  factory.Set (n08, v08);
216 
217  m_queueDiscFactory[handle].AddPacketFilter (factory);
218 }
219 
221 TrafficControlHelper::AddQueueDiscClasses (uint16_t handle, uint16_t count, std::string type,
222  std::string n01, const AttributeValue& v01,
223  std::string n02, const AttributeValue& v02,
224  std::string n03, const AttributeValue& v03,
225  std::string n04, const AttributeValue& v04,
226  std::string n05, const AttributeValue& v05,
227  std::string n06, const AttributeValue& v06,
228  std::string n07, const AttributeValue& v07,
229  std::string n08, const AttributeValue& v08)
230 {
231  NS_ABORT_MSG_IF (handle >= m_queueDiscFactory.size (), "A queue disc with handle "
232  << handle << " does not exist");
233 
234  ObjectFactory factory;
235  factory.SetTypeId (type);
236  factory.Set (n01, v01);
237  factory.Set (n02, v02);
238  factory.Set (n03, v03);
239  factory.Set (n04, v04);
240  factory.Set (n05, v05);
241  factory.Set (n06, v06);
242  factory.Set (n07, v07);
243  factory.Set (n08, v08);
244 
246  uint16_t classId;
247 
248  for (int i = 0; i < count; i++)
249  {
250  classId = m_queueDiscFactory[handle].AddQueueDiscClass (factory);
251  list.push_back (classId);
252  }
253  return list;
254 }
255 
256 uint16_t
257 TrafficControlHelper::AddChildQueueDisc (uint16_t handle, uint16_t classId, std::string type,
258  std::string n01, const AttributeValue& v01,
259  std::string n02, const AttributeValue& v02,
260  std::string n03, const AttributeValue& v03,
261  std::string n04, const AttributeValue& v04,
262  std::string n05, const AttributeValue& v05,
263  std::string n06, const AttributeValue& v06,
264  std::string n07, const AttributeValue& v07,
265  std::string n08, const AttributeValue& v08,
266  std::string n09, const AttributeValue& v09,
267  std::string n10, const AttributeValue& v10,
268  std::string n11, const AttributeValue& v11,
269  std::string n12, const AttributeValue& v12,
270  std::string n13, const AttributeValue& v13,
271  std::string n14, const AttributeValue& v14,
272  std::string n15, const AttributeValue& v15)
273 {
274  NS_ABORT_MSG_IF (handle >= m_queueDiscFactory.size (), "A queue disc with handle "
275  << handle << " does not exist");
276 
277  ObjectFactory factory;
278  factory.SetTypeId (type);
279  factory.Set (n01, v01);
280  factory.Set (n02, v02);
281  factory.Set (n03, v03);
282  factory.Set (n04, v04);
283  factory.Set (n05, v05);
284  factory.Set (n06, v06);
285  factory.Set (n07, v07);
286  factory.Set (n08, v08);
287  factory.Set (n09, v09);
288  factory.Set (n10, v10);
289  factory.Set (n11, v11);
290  factory.Set (n12, v12);
291  factory.Set (n13, v13);
292  factory.Set (n14, v14);
293  factory.Set (n15, v15);
294 
295  uint16_t childHandle = m_queueDiscFactory.size ();
296  m_queueDiscFactory.push_back (QueueDiscFactory (factory));
297  m_queueDiscFactory[handle].SetChildQueueDisc (classId, childHandle);
298 
299  return childHandle;
300 }
301 
304  std::string type,
305  std::string n01, const AttributeValue& v01,
306  std::string n02, const AttributeValue& v02,
307  std::string n03, const AttributeValue& v03,
308  std::string n04, const AttributeValue& v04,
309  std::string n05, const AttributeValue& v05,
310  std::string n06, const AttributeValue& v06,
311  std::string n07, const AttributeValue& v07,
312  std::string n08, const AttributeValue& v08,
313  std::string n09, const AttributeValue& v09,
314  std::string n10, const AttributeValue& v10,
315  std::string n11, const AttributeValue& v11,
316  std::string n12, const AttributeValue& v12,
317  std::string n13, const AttributeValue& v13,
318  std::string n14, const AttributeValue& v14,
319  std::string n15, const AttributeValue& v15)
320 {
322  for (ClassIdList::const_iterator c = classes.begin (); c != classes.end (); c++)
323  {
324  uint16_t childHandle = AddChildQueueDisc (handle, *c, type, n01, v01, n02, v02, n03, v03,
325  n04, v04, n05, v05, n06, v06, n07, v07, n08, v08, n09, v09,
326  n10, v10, n11, v11, n12, v12, n13, v13, n14, v14, n15, v15);
327  list.push_back (childHandle);
328  }
329  return list;
330 }
331 
334 {
335  QueueDiscContainer container;
336 
337  // A TrafficControlLayer object is aggregated by the InternetStackHelper, but check
338  // anyway because a queue disc has no effect without a TrafficControlLayer object
339  Ptr<TrafficControlLayer> tc = d->GetNode ()->GetObject<TrafficControlLayer> ();
340  NS_ASSERT (tc != 0);
341 
342  // Start from an empty vector of queue discs
343  m_queueDiscs.clear ();
344  m_queueDiscs.resize (m_queueDiscFactory.size ());
345 
346  // Create queue discs (from leaves to root)
347  for (int i = m_queueDiscFactory.size () - 1; i >= 0; i--)
348  {
349  Ptr<QueueDisc> q = m_queueDiscFactory[i].CreateQueueDisc (m_queueDiscs);
350  q->SetNetDevice (d);
351  m_queueDiscs[i] = q;
352  container.Add (q);
353  }
354 
355  // Set the root queue disc on the device
356  tc->SetRootQueueDiscOnDevice (d, m_queueDiscs[0]);
357 
358  return container;
359 }
360 
363 {
364  QueueDiscContainer container;
365 
366  for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
367  {
368  container.Add (Install (*i));
369  }
370 
371  return container;
372 }
373 
374 void
376 {
377  Ptr<TrafficControlLayer> tc = d->GetNode ()->GetObject<TrafficControlLayer> ();
378  NS_ASSERT (tc != 0);
379 
380  tc->DeleteRootQueueDiscOnDevice (d);
381 }
382 
383 void
385 {
386  for (NetDeviceContainer::Iterator i = c.Begin (); i != c.End (); ++i)
387  {
388  Uninstall (*i);
389  }
390 }
391 
392 
393 } // namespace ns3
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
QueueDiscContainer Install(NetDeviceContainer c)
QueueDiscFactory()
Default constructor.
void AddQueueDiscClass(Ptr< QueueDiscClass > qdClass)
Add a queue disc class to the tail of the list of classes.
Definition: queue-disc.cc:352
Traffic control layer definition.
Hold a value for an Attribute.
Definition: attribute.h:68
std::vector< ObjectFactory > m_queueDiscClassesFactory
Vector of factories to create queue disc classes.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#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.
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:205
std::vector< Ptr< QueueDisc > > m_queueDiscs
Vector of all the created queue discs.
void Uninstall(NetDeviceContainer c)
ClassIdList AddQueueDiscClasses(uint16_t handle, uint16_t count, std::string type, std::string n01="", const AttributeValue &v01=EmptyAttributeValue(), std::string n02="", const AttributeValue &v02=EmptyAttributeValue(), std::string n03="", const AttributeValue &v03=EmptyAttributeValue(), std::string n04="", const AttributeValue &v04=EmptyAttributeValue(), std::string n05="", const AttributeValue &v05=EmptyAttributeValue(), std::string n06="", const AttributeValue &v06=EmptyAttributeValue(), std::string n07="", const AttributeValue &v07=EmptyAttributeValue(), std::string n08="", const AttributeValue &v08=EmptyAttributeValue())
Helper function used to add the given number of queue disc classes (of the given type and with the gi...
void AddPacketFilter(uint16_t handle, std::string type, std::string n01="", const AttributeValue &v01=EmptyAttributeValue(), std::string n02="", const AttributeValue &v02=EmptyAttributeValue(), std::string n03="", const AttributeValue &v03=EmptyAttributeValue(), std::string n04="", const AttributeValue &v04=EmptyAttributeValue(), std::string n05="", const AttributeValue &v05=EmptyAttributeValue(), std::string n06="", const AttributeValue &v06=EmptyAttributeValue(), std::string n07="", const AttributeValue &v07=EmptyAttributeValue(), std::string n08="", const AttributeValue &v08=EmptyAttributeValue())
Helper function used to add a packet filter (of the given type and with the given attributes) to the ...
void SetChildQueueDisc(uint16_t classId, uint16_t handle)
Set the (child) queue disc to attach to a class.
Holds a vector of ns3::QueueDisc pointers.
Abstract base class for packet Queues.
Definition: queue.h:44
ObjectFactory m_queueDiscFactory
Factory to create this queue disc.
TrafficControlHelper()
Create a TrafficControlHelper to make life easier when creating QueueDisc objects.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Hold an unsigned integer type.
Definition: uinteger.h:44
void AddInternalQueue(Ptr< Queue > queue)
Add an internal queue to the tail of the list of queues.
Definition: queue-disc.cc:312
holds a vector of ns3::NetDevice pointers
Build a set of QueueDisc objects.
Ptr< QueueDisc > CreateQueueDisc(const std::vector< Ptr< QueueDisc > > &queueDiscs)
Create a queue disc with the currently stored configuration.
#define list
HandleList AddChildQueueDiscs(uint16_t handle, const ClassIdList &classes, std::string type, std::string n01="", const AttributeValue &v01=EmptyAttributeValue(), std::string n02="", const AttributeValue &v02=EmptyAttributeValue(), std::string n03="", const AttributeValue &v03=EmptyAttributeValue(), std::string n04="", const AttributeValue &v04=EmptyAttributeValue(), std::string n05="", const AttributeValue &v05=EmptyAttributeValue(), std::string n06="", const AttributeValue &v06=EmptyAttributeValue(), std::string n07="", const AttributeValue &v07=EmptyAttributeValue(), std::string n08="", const AttributeValue &v08=EmptyAttributeValue(), std::string n09="", const AttributeValue &v09=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue(), std::string n11="", const AttributeValue &v11=EmptyAttributeValue(), std::string n12="", const AttributeValue &v12=EmptyAttributeValue(), std::string n13="", const AttributeValue &v13=EmptyAttributeValue(), std::string n14="", const AttributeValue &v14=EmptyAttributeValue(), std::string n15="", const AttributeValue &v15=EmptyAttributeValue())
Helper function used to attach a child queue disc (of the given type and with the given attributes) t...
uint16_t AddChildQueueDisc(uint16_t handle, uint16_t classId, std::string type, std::string n01="", const AttributeValue &v01=EmptyAttributeValue(), std::string n02="", const AttributeValue &v02=EmptyAttributeValue(), std::string n03="", const AttributeValue &v03=EmptyAttributeValue(), std::string n04="", const AttributeValue &v04=EmptyAttributeValue(), std::string n05="", const AttributeValue &v05=EmptyAttributeValue(), std::string n06="", const AttributeValue &v06=EmptyAttributeValue(), std::string n07="", const AttributeValue &v07=EmptyAttributeValue(), std::string n08="", const AttributeValue &v08=EmptyAttributeValue(), std::string n09="", const AttributeValue &v09=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue(), std::string n11="", const AttributeValue &v11=EmptyAttributeValue(), std::string n12="", const AttributeValue &v12=EmptyAttributeValue(), std::string n13="", const AttributeValue &v13=EmptyAttributeValue(), std::string n14="", const AttributeValue &v14=EmptyAttributeValue(), std::string n15="", const AttributeValue &v15=EmptyAttributeValue())
Helper function used to attach a child queue disc (of the given type and with the given attributes) t...
std::vector< uint16_t > ClassIdList
uint16_t AddQueueDiscClass(ObjectFactory factory)
Add a factory to create a queue disc class.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Hold objects of type Ptr.
Definition: pointer.h:36
uint16_t SetRootQueueDisc(std::string type, std::string n01="", const AttributeValue &v01=EmptyAttributeValue(), std::string n02="", const AttributeValue &v02=EmptyAttributeValue(), std::string n03="", const AttributeValue &v03=EmptyAttributeValue(), std::string n04="", const AttributeValue &v04=EmptyAttributeValue(), std::string n05="", const AttributeValue &v05=EmptyAttributeValue(), std::string n06="", const AttributeValue &v06=EmptyAttributeValue(), std::string n07="", const AttributeValue &v07=EmptyAttributeValue(), std::string n08="", const AttributeValue &v08=EmptyAttributeValue(), std::string n09="", const AttributeValue &v09=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue(), std::string n11="", const AttributeValue &v11=EmptyAttributeValue(), std::string n12="", const AttributeValue &v12=EmptyAttributeValue(), std::string n13="", const AttributeValue &v13=EmptyAttributeValue(), std::string n14="", const AttributeValue &v14=EmptyAttributeValue(), std::string n15="", const AttributeValue &v15=EmptyAttributeValue())
Helper function used to set a root queue disc of the given type and with the given attributes...
std::vector< ObjectFactory > m_internalQueuesFactory
Vector of factories to create internal queues.
void AddInternalQueue(ObjectFactory factory)
Add a factory to create an internal queue.
void AddPacketFilter(ObjectFactory factory)
Add a factory to create a packet filter.
Instantiate subclasses of ns3::Object.
std::vector< ObjectFactory > m_packetFiltersFactory
Vector of factories to create packet filters.
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
std::vector< QueueDiscFactory > m_queueDiscFactory
QueueDisc factory, stores the configuration of all the queue discs.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
void AddInternalQueues(uint16_t handle, uint16_t count, std::string type, std::string n01="", const AttributeValue &v01=EmptyAttributeValue(), std::string n02="", const AttributeValue &v02=EmptyAttributeValue(), std::string n03="", const AttributeValue &v03=EmptyAttributeValue(), std::string n04="", const AttributeValue &v04=EmptyAttributeValue(), std::string n05="", const AttributeValue &v05=EmptyAttributeValue(), std::string n06="", const AttributeValue &v06=EmptyAttributeValue(), std::string n07="", const AttributeValue &v07=EmptyAttributeValue(), std::string n08="", const AttributeValue &v08=EmptyAttributeValue())
Helper function used to add the given number of internal queues (of the given type and with the given...
std::map< uint16_t, uint16_t > m_classIdChildHandleMap
Map storing the associations between class IDs and child queue disc handles.
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
PacketFilter is the abstract base class for filters used by queue discs to classify packets...
Definition: packet-filter.h:34
void AddPacketFilter(Ptr< PacketFilter > filter)
Add a packet filter to the tail of the list of filters used to classify packets.
Definition: queue-disc.cc:332
This class stores object factories required to create a queue disc and all of its components (packet ...
static TrafficControlHelper Default(void)
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
void Add(QueueDiscContainer other)
Append the contents of another QueueDiscContainer to the end of this container.
std::vector< uint16_t > HandleList
void SetNetDevice(Ptr< NetDevice > device)
Set the NetDevice on which this queue discipline is installed.
Definition: queue-disc.cc:284