A Discrete-Event Network Simulator
API
traffic-control-layer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 Natale Patriciello <natale.patriciello@gmail.com>
4  * 2016 Stefano Avallone <stavallo@unina.it>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */
19 
20 #include "traffic-control-layer.h"
21 #include "ns3/log.h"
22 #include "ns3/object-map.h"
23 #include "ns3/packet.h"
24 #include "ns3/socket.h"
25 #include "ns3/queue-disc.h"
26 
27 namespace ns3 {
28 
29 NS_LOG_COMPONENT_DEFINE ("TrafficControlLayer");
30 
31 NS_OBJECT_ENSURE_REGISTERED (TrafficControlLayer);
32 
33 TypeId
35 {
36  static TypeId tid = TypeId ("ns3::TrafficControlLayer")
37  .SetParent<Object> ()
38  .SetGroupName ("TrafficControl")
39  .AddConstructor<TrafficControlLayer> ()
40  .AddAttribute ("RootQueueDiscList", "The list of root queue discs associated to this Traffic Control layer.",
41  ObjectMapValue (),
44  MakeObjectMapChecker<QueueDisc> ())
45  ;
46  return tid;
47 }
48 
49 TypeId
51 {
52  return GetTypeId ();
53 }
54 
56  : Object ()
57 {
59 }
60 
61 void
63 {
64  NS_LOG_FUNCTION (this);
65  m_node = 0;
66  m_handlers.clear ();
67  m_netDevices.clear ();
69 }
70 
71 void
73 {
74  NS_LOG_FUNCTION (this);
75  std::map<Ptr<NetDevice>, NetDeviceInfo>::iterator ndi;
76  for (ndi = m_netDevices.begin (); ndi != m_netDevices.end (); ndi++)
77  {
78  if (ndi->second.rootQueueDisc)
79  {
80  Ptr<NetDeviceQueueInterface> devQueueIface = ndi->second.ndqi;
81  NS_ASSERT (devQueueIface);
82 
83  // set the wake callbacks on netdevice queues
84  if (ndi->second.rootQueueDisc->GetWakeMode () == QueueDisc::WAKE_ROOT)
85  {
86  for (uint32_t i = 0; i < devQueueIface->GetNTxQueues (); i++)
87  {
88  devQueueIface->GetTxQueue (i)->SetWakeCallback (MakeCallback (&QueueDisc::Run, ndi->second.rootQueueDisc));
89  ndi->second.queueDiscsToWake.push_back (ndi->second.rootQueueDisc);
90  }
91  }
92  else if (ndi->second.rootQueueDisc->GetWakeMode () == QueueDisc::WAKE_CHILD)
93  {
94  NS_ASSERT_MSG (ndi->second.rootQueueDisc->GetNQueueDiscClasses () == devQueueIface->GetNTxQueues (),
95  "The number of child queue discs does not match the number of netdevice queues");
96  for (uint32_t i = 0; i < devQueueIface->GetNTxQueues (); i++)
97  {
98  devQueueIface->GetTxQueue (i)->SetWakeCallback (MakeCallback (&QueueDisc::Run,
99  ndi->second.rootQueueDisc->GetQueueDiscClass (i)->GetQueueDisc ()));
100  ndi->second.queueDiscsToWake.push_back (ndi->second.rootQueueDisc->GetQueueDiscClass (i)->GetQueueDisc ());
101  }
102  }
103 
104  // initialize the queue disc
105  ndi->second.rootQueueDisc->Initialize ();
106  }
107  }
109 }
110 
111 void
113 {
114  NS_LOG_FUNCTION (this << device);
115 
116  // ensure this setup is done just once. SetupDevice is called by Ipv4L3Protocol
117  // and Ipv6L3Protocol when they add an interface, thus it might be called twice
118  // in case of dual stack nodes. Also, SetupDevice might be called twice if the
119  // tc helper is invoked (to install a queue disc) before the creation of the
120  // Ipv{4,6}Interface, since SetRootQueueDiscOnDevice calls SetupDevice
121  if (device->GetObject<NetDeviceQueueInterface> ())
122  {
123  NS_LOG_DEBUG ("The setup for this device has been already done.");
124  return;
125  }
126 
127  // create a NetDeviceQueueInterface object and aggregate it to the device
128  Ptr<NetDeviceQueueInterface> devQueueIface = CreateObject<NetDeviceQueueInterface> ();
129  device->AggregateObject (devQueueIface);
130 
131  // multi-queue devices must set the number of transmission queues in their
132  // NotifyNewAggregate method. Since we have just aggregated the netdevice
133  // queue interface to the device, we can create the transmission queues
134  devQueueIface->CreateTxQueues ();
135 
136  // devices can set a select queue callback in their NotifyNewAggregate method
137  SelectQueueCallback cb = devQueueIface->GetSelectQueueCallback ();
138 
139  // create an entry in the m_netDevices map for this device
140  NS_ASSERT_MSG (m_netDevices.find (device) == m_netDevices.end (), "This is a bug,"
141  << " SetupDevice only can insert an entry in the m_netDevices map");
142 
143  NetDeviceInfo entry = {0, devQueueIface, QueueDiscVector (), cb};
144  m_netDevices[device] = entry;
145 }
146 
147 void
149  uint16_t protocolType, Ptr<NetDevice> device)
150 {
151  NS_LOG_FUNCTION (this << protocolType << device);
152 
153  struct ProtocolHandlerEntry entry;
154  entry.handler = handler;
155  entry.protocol = protocolType;
156  entry.device = device;
157  entry.promiscuous = false;
158 
159  m_handlers.push_back (entry);
160 
161  NS_LOG_DEBUG ("Handler for NetDevice: " << device << " registered for protocol " <<
162  protocolType << ".");
163 }
164 
165 void
167 {
168  NS_LOG_FUNCTION (this << device << qDisc);
169 
170  std::map<Ptr<NetDevice>, NetDeviceInfo>::iterator ndi = m_netDevices.find (device);
171 
172  if (ndi == m_netDevices.end ())
173  {
174  // SetupDevice has not been called yet. This may happen when the tc helper is
175  // invoked (to install a queue disc) before the creation of the Ipv{4,6}Interface.
176  // Since queue discs require that a netdevice queue interface is aggregated
177  // to the device, call SetupDevice
178  SetupDevice (device);
179  ndi = m_netDevices.find (device);
180  NS_ASSERT (ndi != m_netDevices.end ());
181  }
182 
183  NS_ASSERT_MSG (ndi->second.rootQueueDisc == 0, "Cannot install a root queue disc on a "
184  << "device already having one. Delete the existing queue disc first.");
185  ndi->second.rootQueueDisc = qDisc;
186 }
187 
190 {
191  NS_LOG_FUNCTION (this << device);
192 
193  std::map<Ptr<NetDevice>, NetDeviceInfo>::const_iterator ndi = m_netDevices.find (device);
194 
195  if (ndi == m_netDevices.end ())
196  {
197  return 0;
198  }
199  return ndi->second.rootQueueDisc;
200 }
201 
204 {
205  NS_LOG_FUNCTION (this << index);
206  return GetRootQueueDiscOnDevice (m_node->GetDevice (index));
207 }
208 
209 void
211 {
212  NS_LOG_FUNCTION (this << device);
213 
214  std::map<Ptr<NetDevice>, NetDeviceInfo>::iterator ndi = m_netDevices.find (device);
215 
216  NS_ASSERT_MSG (ndi != m_netDevices.end () && ndi->second.rootQueueDisc != 0, "No root queue disc"
217  << " installed on device " << device);
218 
219  // remove the root queue disc
220  ndi->second.rootQueueDisc = 0;
221  ndi->second.queueDiscsToWake.clear ();
222 }
223 
224 void
226 {
227  NS_LOG_FUNCTION (this << node);
228  m_node = node;
229 }
230 
231 void
233 {
234  NS_LOG_FUNCTION (this);
235  if (m_node == 0)
236  {
237  Ptr<Node> node = this->GetObject<Node> ();
238  //verify that it's a valid node and that
239  //the node was not set before
240  if (node != 0)
241  {
242  this->SetNode (node);
243  }
244  }
246 }
247 
248 uint32_t
250 {
251  return m_node->GetNDevices ();
252 }
253 
254 
255 void
257  uint16_t protocol, const Address &from, const Address &to,
258  NetDevice::PacketType packetType)
259 {
260  NS_LOG_FUNCTION (this << device << p << protocol << from << to << packetType);
261 
262  bool found = false;
263 
264  for (ProtocolHandlerList::iterator i = m_handlers.begin ();
265  i != m_handlers.end (); i++)
266  {
267  if (i->device == 0
268  || (i->device != 0 && i->device == device))
269  {
270  if (i->protocol == 0
271  || i->protocol == protocol)
272  {
273  NS_LOG_DEBUG ("Found handler for packet " << p << ", protocol " <<
274  protocol << " and NetDevice " << device <<
275  ". Send packet up");
276  i->handler (device, p, protocol, from, to, packetType);
277  found = true;
278  }
279  }
280  }
281 
282  if (! found)
283  {
284  NS_FATAL_ERROR ("Handler for protocol " << p << " and device " << device <<
285  " not found. It isn't forwarded up; it dies here.");
286  }
287 }
288 
289 void
291 {
292  NS_LOG_FUNCTION (this << device << item);
293 
294  NS_LOG_DEBUG ("Send packet to device " << device << " protocol number " <<
295  item->GetProtocol ());
296 
297  std::map<Ptr<NetDevice>, NetDeviceInfo>::iterator ndi = m_netDevices.find (device);
298  NS_ASSERT (ndi != m_netDevices.end ());
299  Ptr<NetDeviceQueueInterface> devQueueIface = ndi->second.ndqi;
300  NS_ASSERT (devQueueIface);
301 
302  // determine the transmission queue of the device where the packet will be enqueued
303  uint8_t txq = 0;
304  if (devQueueIface->GetNTxQueues () > 1)
305  {
306  if (!ndi->second.selectQueueCallback.IsNull ())
307  {
308  txq = ndi->second.selectQueueCallback (item);
309  }
310  // otherwise, Linux determines the queue index by using a hash function
311  // and associates such index to the socket which the packet belongs to,
312  // so that subsequent packets of the same socket will be mapped to the
313  // same tx queue (__netdev_pick_tx function in net/core/dev.c). It is
314  // pointless to implement this in ns-3 because currently the multi-queue
315  // devices provide a select queue callback
316  }
317 
318  NS_ASSERT (txq < devQueueIface->GetNTxQueues ());
319 
320  if (ndi->second.rootQueueDisc == 0)
321  {
322  // The device has no attached queue disc, thus add the header to the packet and
323  // send it directly to the device if the selected queue is not stopped
324  if (!devQueueIface->GetTxQueue (txq)->IsStopped ())
325  {
326  item->AddHeader ();
327  // a single queue device makes no use of the priority tag
328  if (devQueueIface->GetNTxQueues () == 1)
329  {
330  SocketPriorityTag priorityTag;
331  item->GetPacket ()->RemovePacketTag (priorityTag);
332  }
333  device->Send (item->GetPacket (), item->GetAddress (), item->GetProtocol ());
334  }
335  }
336  else
337  {
338  // Enqueue the packet in the queue disc associated with the netdevice queue
339  // selected for the packet and try to dequeue packets from such queue disc
340  item->SetTxQueueIndex (txq);
341 
342  Ptr<QueueDisc> qDisc = ndi->second.queueDiscsToWake[txq];
343  NS_ASSERT (qDisc);
344  qDisc->Enqueue (item);
345  qDisc->Run ();
346  }
347 }
348 
349 } // namespace ns3
virtual void DeleteRootQueueDiscOnDevice(Ptr< NetDevice > device)
This method can be used to remove the root queue disc (and associated filters, classes and queues) in...
virtual void DoInitialize(void)
Initialize() implementation.
Definition: object.cc:353
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 "...
virtual void SetRootQueueDiscOnDevice(Ptr< NetDevice > device, Ptr< QueueDisc > qDisc)
This method can be used to set the root queue disc installed on a device.
bool Enqueue(Ptr< QueueDiscItem > item)
Pass a packet to store to the queue discipline.
Definition: queue-disc.cc:451
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Introspection did not find any typical Config paths.
ProtocolHandlerList m_handlers
List of upper-layer handlers.
bool promiscuous
true if it is a promiscuous handler
virtual void DoInitialize(void)
Initialize() implementation.
#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
PacketType
Packet types are used as they are in Linux.
Definition: net-device.h:606
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
virtual void Receive(Ptr< NetDevice > device, Ptr< const Packet > p, uint16_t protocol, const Address &from, const Address &to, NetDevice::PacketType packetType)
Called by NetDevices, incoming packet.
uint16_t protocol
the protocol number
a polymophic address class
Definition: address.h:90
void SetNode(Ptr< Node > node)
Set node associated with this stack.
indicates whether the socket has a priority set.
Definition: socket.h:1304
void Run(void)
Modelled after the Linux function __qdisc_run (net/sched/sch_generic.c) Dequeues multiple packets...
Definition: queue-disc.cc:494
Ptr< NetDevice > GetDevice(uint32_t index) const
Retrieve the index-th NetDevice associated to this node.
Definition: node.cc:142
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Network device transmission queue interface.
Definition: net-device.h:262
std::vector< Ptr< QueueDisc > > QueueDiscVector
Typedef for queue disc vector.
Ptr< NetDevice > device
the NetDevice
uint32_t GetNDevices(void) const
Definition: node.cc:150
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static TypeId GetTypeId(void)
Get the type ID.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:90
virtual void Send(Ptr< NetDevice > device, Ptr< QueueDiscItem > item)
Called from upper layer to queue a packet for the transmission.
Ptr< Node > m_node
The node this TrafficControlLayer object is aggregated to.
virtual Ptr< QueueDisc > GetRootQueueDiscOnDevice(Ptr< NetDevice > device) const
This method can be used to get the root queue disc installed on a device.
std::map< Ptr< NetDevice >, NetDeviceInfo > m_netDevices
Map storing the required information for each device with a queue disc installed. ...
Protocol handler entry.
virtual void SetupDevice(Ptr< NetDevice > device)
Perform the operations that the traffic control layer needs to do when an IPv4/v6 interface is added ...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
uint32_t GetNDevices(void) const
Required by the object map accessor.
Information to store for each device.
Ptr< const AttributeAccessor > MakeObjectMapAccessor(U T::*memberVariable)
MakeAccessorHelper implementation for ObjectVector.
Definition: object-map.h:80
Node::ProtocolHandler handler
the protocol handler
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual void NotifyNewAggregate(void)
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:325
virtual TypeId GetInstanceTypeId(void) const
Get the type ID for the instance.
Container for a set of ns3::Object pointers.
virtual void DoDispose(void)
Destructor implementation.
a unique identifier for an interface.
Definition: type-id.h:58
void RegisterProtocolHandler(Node::ProtocolHandler handler, uint16_t protocolType, Ptr< NetDevice > device)
Register an IN handler.
Ptr< QueueDisc > GetRootQueueDiscOnDeviceByIndex(uint32_t index) const
Required by the object map accessor.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904