A Discrete-Event Network Simulator
API
net-device-queue-interface.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2017 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 <stefano.avallone@.unina.it>
19  */
20 #ifndef NET_DEVICE_QUEUE_INTERFACE_H
21 #define NET_DEVICE_QUEUE_INTERFACE_H
22 
23 #include <vector>
24 #include <functional>
25 #include "ns3/callback.h"
26 #include "ns3/object.h"
27 #include "ns3/ptr.h"
28 #include "ns3/log.h"
29 #include "ns3/net-device.h"
30 #include "ns3/object-factory.h"
31 
32 namespace ns3 {
33 
34 class QueueLimits;
35 class NetDeviceQueueInterface;
36 class QueueItem;
37 
38 
56 class NetDeviceQueue : public Object
57 {
58 public:
63  static TypeId GetTypeId (void);
64 
65  NetDeviceQueue ();
66  virtual ~NetDeviceQueue();
67 
72  virtual void Start (void);
73 
78  virtual void Stop (void);
79 
85  virtual void Wake (void);
86 
94  virtual bool IsStopped (void) const;
95 
106 
109 
120  virtual void SetWakeCallback (WakeCallback cb);
121 
126  virtual void NotifyQueuedBytes (uint32_t bytes);
127 
132  virtual void NotifyTransmittedBytes (uint32_t bytes);
133 
137  void ResetQueueLimits ();
138 
144 
150 
162  template <typename QueueType>
163  void PacketEnqueued (QueueType* queue, Ptr<const typename QueueType::ItemType> item);
164 
177  template <typename QueueType>
178  void PacketDequeued (QueueType* queue, Ptr<const typename QueueType::ItemType> item);
179 
192  template <typename QueueType>
193  void PacketDiscarded (QueueType* queue, Ptr<const typename QueueType::ItemType> item);
194 
203  template <typename QueueType>
204  void ConnectQueueTraces (Ptr<QueueType> queue);
205 
206 private:
212 
214 };
215 
216 
231 {
232 public:
237  static TypeId GetTypeId (void);
238 
243  virtual ~NetDeviceQueueInterface ();
244 
253  Ptr<NetDeviceQueue> GetTxQueue (std::size_t i) const;
254 
259  std::size_t GetNTxQueues (void) const;
260 
269  void SetTxQueuesType (TypeId type);
270 
279  void SetNTxQueues (std::size_t numTxQueues);
280 
282  typedef std::function<std::size_t (Ptr<QueueItem>)> SelectQueueCallback;
283 
292 
301 
302 protected:
306  virtual void DoDispose (void);
310  virtual void NotifyNewAggregate (void);
311 
312 private:
314  std::vector< Ptr<NetDeviceQueue> > m_txQueuesVector;
316 };
317 
318 
323 template <typename QueueType>
324 void
326 {
327  NS_ASSERT (queue != 0);
328 
329  queue->TraceConnectWithoutContext ("Enqueue",
330  MakeCallback (&NetDeviceQueue::PacketEnqueued<QueueType>, this)
331  .Bind (PeekPointer (queue)));
332  queue->TraceConnectWithoutContext ("Dequeue",
333  MakeCallback (&NetDeviceQueue::PacketDequeued<QueueType>, this)
334  .Bind (PeekPointer (queue)));
335  queue->TraceConnectWithoutContext ("DropBeforeEnqueue",
336  MakeCallback (&NetDeviceQueue::PacketDiscarded<QueueType>, this)
337  .Bind (PeekPointer (queue)));
338 }
339 
340 template <typename QueueType>
341 void
343 {
344  NS_LOG_FUNCTION (this << queue << item);
345 
346  // Inform BQL
347  NotifyQueuedBytes (item->GetSize ());
348 
349  NS_ASSERT_MSG (m_device, "Aggregated NetDevice not set");
350  Ptr<Packet> p = Create<Packet> (m_device->GetMtu ());
351 
352  // After enqueuing a packet, we need to check whether the queue is able to
353  // store another packet. If not, we stop the queue
354 
355  if (queue->GetCurrentSize () + p > queue->GetMaxSize ())
356  {
357  NS_LOG_DEBUG ("The device queue is being stopped (" << queue->GetCurrentSize ()
358  << " inside)");
359  Stop ();
360  }
361 }
362 
363 template <typename QueueType>
364 void
366 {
367  NS_LOG_FUNCTION (this << queue << item);
368 
369  // Inform BQL
370  NotifyTransmittedBytes (item->GetSize ());
371 
372  NS_ASSERT_MSG (m_device, "Aggregated NetDevice not set");
373  Ptr<Packet> p = Create<Packet> (m_device->GetMtu ());
374 
375  // After dequeuing a packet, if there is room for another packet we
376  // call Wake () that ensures that the queue is not stopped and restarts
377  // the queue disc if the queue was stopped
378 
379  if (queue->GetCurrentSize () + p <= queue->GetMaxSize ())
380  {
381  Wake ();
382  }
383 }
384 
385 template <typename QueueType>
386 void
388 {
389  NS_LOG_FUNCTION (this << queue << item);
390 
391  // This method is called when a packet is discarded before being enqueued in the
392  // device queue, likely because the queue is full. This should not happen if the
393  // device correctly stops the queue. Anyway, stop the tx queue, so that the upper
394  // layers do not send packets until there is room in the queue again.
395 
396  NS_LOG_ERROR ("BUG! No room in the device queue for the received packet! ("
397  << queue->GetCurrentSize () << " inside)");
398 
399  Stop ();
400 }
401 
402 } // namespace ns3
403 
404 #endif /* NET_DEVICE_QUEUE_INTERFACE_H */
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
ns3::NetDeviceQueue::PacketDiscarded
void PacketDiscarded(QueueType *queue, Ptr< const typename QueueType::ItemType > item)
Perform the actions required by flow control and dynamic queue limits when a packet is dropped before...
Definition: net-device-queue-interface.h:387
ns3::NetDeviceQueueInterface::NotifyNewAggregate
virtual void NotifyNewAggregate(void)
Notify that an object was aggregated.
Definition: net-device-queue-interface.cc:236
NS_ASSERT
#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
ns3::NetDeviceQueueInterface::m_txQueues
ObjectFactory m_txQueues
Device transmission queues TypeId.
Definition: net-device-queue-interface.h:313
ns3::NetDeviceQueueInterface::GetTxQueue
Ptr< NetDeviceQueue > GetTxQueue(std::size_t i) const
Get the i-th transmission queue of the device.
Definition: net-device-queue-interface.cc:214
ns3::Callback< void >
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::NetDeviceQueue::m_wakeCallback
WakeCallback m_wakeCallback
Wake callback.
Definition: net-device-queue-interface.h:210
ns3::NetDeviceQueue::SetQueueLimits
void SetQueueLimits(Ptr< QueueLimits > ql)
Set queue limits to this queue.
Definition: net-device-queue-interface.cc:161
ns3::NetDeviceQueue::Wake
virtual void Wake(void)
Called by the device to wake the queue disc associated with this device transmission queue.
Definition: net-device-queue-interface.cc:82
ns3::NetDeviceQueueInterface::SetTxQueuesType
void SetTxQueuesType(TypeId type)
Set the type of device transmission queues to create.
Definition: net-device-queue-interface.cc:249
ns3::NetDeviceQueueInterface::GetSelectQueueCallback
SelectQueueCallback GetSelectQueueCallback(void) const
Get the select queue callback.
Definition: net-device-queue-interface.cc:281
ns3::NetDeviceQueue::NS_LOG_TEMPLATE_DECLARE
NS_LOG_TEMPLATE_DECLARE
redefinition of the log component
Definition: net-device-queue-interface.h:213
ns3::NetDevice::GetMtu
virtual uint16_t GetMtu(void) const =0
ns3::NetDeviceQueue::m_queueLimits
Ptr< QueueLimits > m_queueLimits
Queue limits object.
Definition: net-device-queue-interface.h:209
ns3::PeekPointer
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:415
ns3::NetDeviceQueue::WakeCallback
Callback< void > WakeCallback
Callback invoked by netdevices to wake upper layers.
Definition: net-device-queue-interface.h:108
ns3::NetDeviceQueueInterface::~NetDeviceQueueInterface
virtual ~NetDeviceQueueInterface()
Definition: net-device-queue-interface.cc:208
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::NetDeviceQueueInterface::DoDispose
virtual void DoDispose(void)
Dispose of the object.
Definition: net-device-queue-interface.cc:227
ns3::NetDeviceQueueInterface::NetDeviceQueueInterface
NetDeviceQueueInterface()
Constructor.
Definition: net-device-queue-interface.cc:200
ns3::NetDeviceQueue::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: net-device-queue-interface.cc:33
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
ns3::NetDeviceQueue::IsStopped
virtual bool IsStopped(void) const
Get the status of the device transmission queue.
Definition: net-device-queue-interface.cc:61
ns3::NetDeviceQueue::PacketEnqueued
void PacketEnqueued(QueueType *queue, Ptr< const typename QueueType::ItemType > item)
Perform the actions required by flow control and dynamic queue limits when a packet is enqueued in th...
Definition: net-device-queue-interface.h:342
ns3::NetDeviceQueue::m_device
Ptr< NetDevice > m_device
the netdevice aggregated to the NetDeviceQueueInterface
Definition: net-device-queue-interface.h:211
ns3::NetDeviceQueueInterface::SetNTxQueues
void SetNTxQueues(std::size_t numTxQueues)
Set the number of device transmission queues to create.
Definition: net-device-queue-interface.cc:260
ns3::NetDeviceQueue
Network device transmission queue.
Definition: net-device-queue-interface.h:57
ns3::NetDeviceQueue::ConnectQueueTraces
void ConnectQueueTraces(Ptr< QueueType > queue)
Connect the traced callbacks of a queue to the methods providing support for flow control and dynamic...
Definition: net-device-queue-interface.h:325
ns3::NetDeviceQueue::m_stoppedByQueueLimits
bool m_stoppedByQueueLimits
True if the queue has been stopped by a queue limits object.
Definition: net-device-queue-interface.h:208
ns3::ObjectFactory
Instantiate subclasses of ns3::Object.
Definition: object-factory.h:48
NS_ASSERT_MSG
#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:88
ns3::NetDeviceQueue::PacketDequeued
void PacketDequeued(QueueType *queue, Ptr< const typename QueueType::ItemType > item)
Perform the actions required by flow control and dynamic queue limits when a packet is dequeued (or d...
Definition: net-device-queue-interface.h:365
ns3::NetDeviceQueueInterface::SetSelectQueueCallback
void SetSelectQueueCallback(SelectQueueCallback cb)
Set the select queue callback.
Definition: net-device-queue-interface.cc:275
ns3::NetDeviceQueue::NetDeviceQueue
NetDeviceQueue()
Definition: net-device-queue-interface.cc:43
ns3::NetDeviceQueue::~NetDeviceQueue
virtual ~NetDeviceQueue()
Definition: net-device-queue-interface.cc:51
ns3::MakeCallback
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1642
ns3::NetDeviceQueue::Stop
virtual void Stop(void)
Called by the device to stop this device transmission queue.
Definition: net-device-queue-interface.cc:75
NS_LOG_ERROR
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
ns3::NetDeviceQueueInterface
Network device transmission queue interface.
Definition: net-device-queue-interface.h:231
ns3::NetDeviceQueue::m_stoppedByDevice
bool m_stoppedByDevice
True if the queue has been stopped by the device.
Definition: net-device-queue-interface.h:207
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::NetDeviceQueueInterface::m_txQueuesVector
std::vector< Ptr< NetDeviceQueue > > m_txQueuesVector
Device transmission queues.
Definition: net-device-queue-interface.h:314
ns3::NetDeviceQueueInterface::m_selectQueueCallback
SelectQueueCallback m_selectQueueCallback
Select queue callback.
Definition: net-device-queue-interface.h:315
ns3::NetDeviceQueue::NotifyQueuedBytes
virtual void NotifyQueuedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes queued to the device queue.
Definition: net-device-queue-interface.cc:112
ns3::NetDeviceQueue::SetWakeCallback
virtual void SetWakeCallback(WakeCallback cb)
Set the wake callback.
Definition: net-device-queue-interface.cc:106
ns3::NetDeviceQueueInterface::SelectQueueCallback
std::function< std::size_t(Ptr< QueueItem >)> SelectQueueCallback
Callback invoked to determine the tx queue selected for a given packet.
Definition: net-device-queue-interface.h:282
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::NetDeviceQueue::NotifyAggregatedObject
void NotifyAggregatedObject(Ptr< NetDeviceQueueInterface > ndqi)
Notify this NetDeviceQueue that the NetDeviceQueueInterface was aggregated to an object.
Definition: net-device-queue-interface.cc:97
ns3::NetDeviceQueue::GetQueueLimits
Ptr< QueueLimits > GetQueueLimits()
Get queue limits to this queue.
Definition: net-device-queue-interface.cc:168
ns3::NetDeviceQueue::Start
virtual void Start(void)
Called by the device to start this device transmission queue.
Definition: net-device-queue-interface.cc:68
ns3::NetDeviceQueue::NotifyTransmittedBytes
virtual void NotifyTransmittedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes it is going to transmit.
Definition: net-device-queue-interface.cc:128
ns3::NetDeviceQueueInterface::GetNTxQueues
std::size_t GetNTxQueues(void) const
Get the number of device transmission queues.
Definition: net-device-queue-interface.cc:221
ns3::NetDeviceQueue::ResetQueueLimits
void ResetQueueLimits()
Reset queue limits state.
Definition: net-device-queue-interface.cc:150
ns3::NetDeviceQueueInterface::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: net-device-queue-interface.cc:178