A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
traffic-control-helper.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Universita' degli Studi di Napoli Federico II
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Stefano Avallone <stavallo@unina.it>
18  */
19 
20 #include "traffic-control-helper.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/log.h"
24 #include "ns3/net-device-queue-interface.h"
25 #include "ns3/pointer.h"
26 #include "ns3/queue-limits.h"
27 #include "ns3/traffic-control-layer.h"
28 #include "ns3/uinteger.h"
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("TrafficControlHelper");
34 
36  : m_queueDiscFactory(factory)
37 {
38 }
39 
40 void
42 {
43  m_internalQueuesFactory.push_back(factory);
44 }
45 
46 void
48 {
49  m_packetFiltersFactory.push_back(factory);
50 }
51 
52 uint16_t
54 {
55  m_queueDiscClassesFactory.push_back(factory);
56  return static_cast<uint16_t>(m_queueDiscClassesFactory.size() - 1);
57 }
58 
59 void
60 QueueDiscFactory::SetChildQueueDisc(uint16_t classId, uint16_t handle)
61 {
63  "Cannot attach a queue disc to a non existing class");
64  m_classIdChildHandleMap[classId] = handle;
65 }
66 
68 QueueDiscFactory::CreateQueueDisc(const std::vector<Ptr<QueueDisc>>& queueDiscs)
69 {
70  // create the queue disc
72 
73  // create and add the internal queues
74  for (auto i = m_internalQueuesFactory.begin(); i != m_internalQueuesFactory.end(); i++)
75  {
77  }
78 
79  // create and add the packet filters
80  for (auto i = m_packetFiltersFactory.begin(); i != m_packetFiltersFactory.end(); i++)
81  {
82  qd->AddPacketFilter(i->Create<PacketFilter>());
83  }
84 
85  // create and add the queue disc classes
86  for (std::size_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],
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 
104 {
105 }
106 
108 TrafficControlHelper::Default(std::size_t nTxQueues)
109 {
110  NS_LOG_FUNCTION(nTxQueues);
111  NS_ABORT_MSG_IF(nTxQueues == 0, "The device must have at least one queue");
112  TrafficControlHelper helper;
113 
114  if (nTxQueues == 1)
115  {
116  helper.SetRootQueueDisc("ns3::FqCoDelQueueDisc");
117  }
118  else
119  {
120  uint16_t handle = helper.SetRootQueueDisc("ns3::MqQueueDisc");
121  ClassIdList cls = helper.AddQueueDiscClasses(handle, nTxQueues, "ns3::QueueDiscClass");
122  helper.AddChildQueueDiscs(handle, cls, "ns3::FqCoDelQueueDisc");
123  }
124  return helper;
125 }
126 
127 uint16_t
129 {
131  "A root queue disc has been already added to this factory");
132 
133  m_queueDiscFactory.emplace_back(factory);
134  return 0;
135 }
136 
137 void
138 TrafficControlHelper::DoAddInternalQueues(uint16_t handle, uint16_t count, ObjectFactory factory)
139 {
140  NS_ABORT_MSG_IF(handle >= m_queueDiscFactory.size(),
141  "A queue disc with handle " << handle << " does not exist");
142 
143  for (int i = 0; i < count; i++)
144  {
145  m_queueDiscFactory[handle].AddInternalQueue(factory);
146  }
147 }
148 
149 void
151 {
152  NS_ABORT_MSG_IF(handle >= m_queueDiscFactory.size(),
153  "A queue disc with handle " << handle << " does not exist");
154 
155  m_queueDiscFactory[handle].AddPacketFilter(factory);
156 }
157 
159 TrafficControlHelper::DoAddQueueDiscClasses(uint16_t handle, uint16_t count, ObjectFactory factory)
160 {
161  NS_ABORT_MSG_IF(handle >= m_queueDiscFactory.size(),
162  "A queue disc with handle " << handle << " does not exist");
163 
165  uint16_t classId;
166 
167  for (int i = 0; i < count; i++)
168  {
169  classId = m_queueDiscFactory[handle].AddQueueDiscClass(factory);
170  list.push_back(classId);
171  }
172  return list;
173 }
174 
175 uint16_t
176 TrafficControlHelper::DoAddChildQueueDisc(uint16_t handle, uint16_t classId, ObjectFactory factory)
177 {
178  NS_ABORT_MSG_IF(handle >= m_queueDiscFactory.size(),
179  "A queue disc with handle " << handle << " does not exist");
180 
181  auto childHandle = static_cast<uint16_t>(m_queueDiscFactory.size());
182  m_queueDiscFactory.emplace_back(factory);
183  m_queueDiscFactory[handle].SetChildQueueDisc(classId, childHandle);
184 
185  return childHandle;
186 }
187 
190  const TrafficControlHelper::ClassIdList& classes,
191  ObjectFactory factory)
192 {
194  for (auto c = classes.begin(); c != classes.end(); c++)
195  {
196  uint16_t childHandle = DoAddChildQueueDisc(handle, *c, factory);
197  list.push_back(childHandle);
198  }
199  return list;
200 }
201 
204 {
206 
207  // A TrafficControlLayer object is aggregated by the InternetStackHelper, but check
208  // anyway because a queue disc has no effect without a TrafficControlLayer object
209  Ptr<TrafficControlLayer> tc = d->GetNode()->GetObject<TrafficControlLayer>();
210  NS_ASSERT(tc);
211 
212  // Start from an empty vector of queue discs
213  m_queueDiscs.clear();
214  m_queueDiscs.resize(m_queueDiscFactory.size());
215 
216  // Create queue discs (from leaves to root)
217  for (auto i = m_queueDiscFactory.size(); i-- > 0;)
218  {
219  m_queueDiscs[i] = m_queueDiscFactory[i].CreateQueueDisc(m_queueDiscs);
220  }
221 
222  // Set the root queue disc (if any has been created) on the device
223  if (!m_queueDiscs.empty() && m_queueDiscs[0])
224  {
225  tc->SetRootQueueDiscOnDevice(d, m_queueDiscs[0]);
226  container.Add(m_queueDiscs[0]);
227  }
228 
229  // Queue limits objects can only be installed if a netdevice queue interface
230  // has been aggregated to the netdevice. This is normally the case if the
231  // netdevice has been created via helpers. Abort the simulation if not.
233  {
235  NS_ABORT_MSG_IF(!ndqi,
236  "A NetDeviceQueueInterface object has not been"
237  "aggregated to the NetDevice");
238  for (std::size_t i = 0; i < ndqi->GetNTxQueues(); i++)
239  {
241  ndqi->GetTxQueue(i)->SetQueueLimits(ql);
242  }
243  }
244 
245  return container;
246 }
247 
250 {
252 
253  for (auto i = c.Begin(); i != c.End(); ++i)
254  {
255  container.Add(Install(*i));
256  }
257 
258  return container;
259 }
260 
261 void
263 {
264  Ptr<TrafficControlLayer> tc = d->GetNode()->GetObject<TrafficControlLayer>();
265  NS_ASSERT(tc);
266 
267  tc->DeleteRootQueueDiscOnDevice(d);
268  // remove the queue limits objects installed on the device transmission queues
270  // if a queue disc has been installed on the device, a netdevice queue interface
271  // must have been aggregated to the device
272  NS_ASSERT(ndqi);
273  for (std::size_t i = 0; i < ndqi->GetNTxQueues(); i++)
274  {
275  ndqi->GetTxQueue(i)->SetQueueLimits(nullptr);
276  }
277 }
278 
279 void
281 {
282  for (auto i = c.Begin(); i != c.End(); ++i)
283  {
284  Uninstall(*i);
285  }
286 }
287 
288 } // namespace ns3
holds a vector of ns3::NetDevice pointers
Iterator Begin() const
Get an iterator which refers to the first NetDevice in the container.
Iterator End() const
Get an iterator which indicates past-the-last NetDevice in the container.
Network device transmission queue interface.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
TypeId GetTypeId() const
Get the TypeId which will be created by this ObjectFactory.
PacketFilter is the abstract base class for filters used by queue discs to classify packets.
Definition: packet-filter.h:35
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Holds a vector of ns3::QueueDisc pointers.
Ptr< QueueDisc > CreateQueueDisc(const std::vector< Ptr< QueueDisc >> &queueDiscs)
Create a queue disc with the currently stored configuration.
std::vector< ObjectFactory > m_internalQueuesFactory
Vector of factories to create internal queues.
uint16_t AddQueueDiscClass(ObjectFactory factory)
Add a factory to create a queue disc class.
void AddInternalQueue(ObjectFactory factory)
Add a factory to create an internal queue.
void AddPacketFilter(ObjectFactory factory)
Add a factory to create a packet filter.
void SetChildQueueDisc(uint16_t classId, uint16_t handle)
Set the (child) queue disc to attach to a class.
std::map< uint16_t, uint16_t > m_classIdChildHandleMap
Map storing the associations between class IDs and child queue disc handles.
std::vector< ObjectFactory > m_packetFiltersFactory
Vector of factories to create packet filters.
ObjectFactory m_queueDiscFactory
Factory to create this queue disc.
std::vector< ObjectFactory > m_queueDiscClassesFactory
Vector of factories to create queue disc classes.
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:184
void AddInternalQueue(Ptr< InternalQueue > queue)
Add an internal queue to the tail of the list of queues.
Definition: queue-disc.cc:573
void AddQueueDiscClass(Ptr< QueueDiscClass > qdClass)
Add a queue disc class to the tail of the list of classes.
Definition: queue-disc.cc:624
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:604
Template class for packet Queues.
Definition: queue.h:268
Abstract base class for NetDevice queue length controller.
Definition: queue-limits.h:44
Build a set of QueueDisc objects.
std::vector< uint16_t > HandleList
Container type for Handlers.
QueueDiscContainer Install(NetDeviceContainer c)
std::vector< Ptr< QueueDisc > > m_queueDiscs
Vector of all the created queue discs.
uint16_t DoSetRootQueueDisc(ObjectFactory factory)
Actual implementation of the SetRootQueueDisc method.
uint16_t DoAddChildQueueDisc(uint16_t handle, uint16_t classId, ObjectFactory factory)
Actual implementation of the AddChildQueueDisc method.
TrafficControlHelper()
Create a TrafficControlHelper to make life easier when creating QueueDisc objects.
uint16_t SetRootQueueDisc(const std::string &type, Args &&... args)
Helper function used to set a root queue disc of the given type and with the given attributes.
void DoAddInternalQueues(uint16_t handle, uint16_t count, ObjectFactory factory)
Actual implementation of the AddInternalQueues method.
void DoAddPacketFilter(uint16_t handle, ObjectFactory factory)
Actual implementation of the AddPacketFilter method.
ObjectFactory m_queueLimitsFactory
Factory to create a queue limits object.
void Uninstall(NetDeviceContainer c)
ClassIdList DoAddQueueDiscClasses(uint16_t handle, uint16_t count, ObjectFactory factory)
Actual implementation of the AddQueueDiscClasses method.
std::vector< uint16_t > ClassIdList
Container type for Class IDs.
static TrafficControlHelper Default(std::size_t nTxQueues=1)
ClassIdList AddQueueDiscClasses(uint16_t handle, uint16_t count, const std::string &type, Args &&... args)
Helper function used to add the given number of queue disc classes (of the given type and with the gi...
std::vector< QueueDiscFactory > m_queueDiscFactory
QueueDisc factory, stores the configuration of all the queue discs.
HandleList DoAddChildQueueDiscs(uint16_t handle, const ClassIdList &classes, ObjectFactory factory)
Actual implementation of the AddChildQueueDiscs method.
HandleList AddChildQueueDiscs(uint16_t handle, const ClassIdList &classes, const std::string &type, Args &&... args)
Helper function used to attach a child queue disc (of the given type and with the given attributes) t...
The Traffic Control layer aims at introducing an equivalent of the Linux Traffic Control infrastructu...
uint16_t GetUid() const
Get the internal id of this TypeId.
Definition: type-id.cc:1205
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition: assert.h:66
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Every class exported by the ns3 library is enclosed in the ns3 namespace.
#define list