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 
31 namespace ns3 {
32 
33 class QueueLimits;
34 class NetDeviceQueueInterface;
35 class QueueItem;
36 
37 
55 class NetDeviceQueue : public SimpleRefCount<NetDeviceQueue>
56 {
57 public:
58  NetDeviceQueue ();
59  virtual ~NetDeviceQueue();
60 
65  virtual void Start (void);
66 
71  virtual void Stop (void);
72 
78  virtual void Wake (void);
79 
87  bool IsStopped (void) const;
88 
99 
102 
113  virtual void SetWakeCallback (WakeCallback cb);
114 
119  void NotifyQueuedBytes (uint32_t bytes);
120 
125  void NotifyTransmittedBytes (uint32_t bytes);
126 
130  void ResetQueueLimits ();
131 
137 
143 
155  template <typename QueueType>
156  void PacketEnqueued (QueueType* queue, Ptr<const typename QueueType::ItemType> item);
157 
170  template <typename QueueType>
171  void PacketDequeued (QueueType* queue, Ptr<const typename QueueType::ItemType> item);
172 
185  template <typename QueueType>
186  void PacketDiscarded (QueueType* queue, Ptr<const typename QueueType::ItemType> item);
187 
196  template <typename QueueType>
197  void ConnectQueueTraces (Ptr<QueueType> queue);
198 
199 private:
205 
207 };
208 
209 
224 {
225 public:
230  static TypeId GetTypeId (void);
231 
236  virtual ~NetDeviceQueueInterface ();
237 
246  Ptr<NetDeviceQueue> GetTxQueue (std::size_t i) const;
247 
252  std::size_t GetNTxQueues (void) const;
253 
262  void SetNTxQueues (std::size_t numTxQueues);
263 
265  typedef std::function<std::size_t (Ptr<QueueItem>)> SelectQueueCallback;
266 
275 
284 
285 protected:
289  virtual void DoDispose (void);
293  virtual void NotifyNewAggregate (void);
294 
295 private:
296  std::vector< Ptr<NetDeviceQueue> > m_txQueuesVector;
298 };
299 
300 
305 template <typename QueueType>
306 void
308 {
309  NS_ASSERT (queue != 0);
310 
311  queue->TraceConnectWithoutContext ("Enqueue",
312  MakeCallback (&NetDeviceQueue::PacketEnqueued<QueueType>, this)
313  .Bind (PeekPointer (queue)));
314  queue->TraceConnectWithoutContext ("Dequeue",
315  MakeCallback (&NetDeviceQueue::PacketDequeued<QueueType>, this)
316  .Bind (PeekPointer (queue)));
317  queue->TraceConnectWithoutContext ("DropBeforeEnqueue",
318  MakeCallback (&NetDeviceQueue::PacketDiscarded<QueueType>, this)
319  .Bind (PeekPointer (queue)));
320 }
321 
322 template <typename QueueType>
323 void
325 {
326  NS_LOG_FUNCTION (this << queue << item);
327 
328  // Inform BQL
329  NotifyQueuedBytes (item->GetSize ());
330 
331  NS_ASSERT_MSG (m_device, "Aggregated NetDevice not set");
332  Ptr<Packet> p = Create<Packet> (m_device->GetMtu ());
333 
334  // After enqueuing a packet, we need to check whether the queue is able to
335  // store another packet. If not, we stop the queue
336 
337  if (queue->GetCurrentSize () + p > queue->GetMaxSize ())
338  {
339  NS_LOG_DEBUG ("The device queue is being stopped (" << queue->GetCurrentSize ()
340  << " inside)");
341  Stop ();
342  }
343 }
344 
345 template <typename QueueType>
346 void
348 {
349  NS_LOG_FUNCTION (this << queue << item);
350 
351  // Inform BQL
352  NotifyTransmittedBytes (item->GetSize ());
353 
354  NS_ASSERT_MSG (m_device, "Aggregated NetDevice not set");
355  Ptr<Packet> p = Create<Packet> (m_device->GetMtu ());
356 
357  // After dequeuing a packet, if there is room for another packet we
358  // call Wake () that ensures that the queue is not stopped and restarts
359  // the queue disc if the queue was stopped
360 
361  if (queue->GetCurrentSize () + p <= queue->GetMaxSize ())
362  {
363  Wake ();
364  }
365 }
366 
367 template <typename QueueType>
368 void
370 {
371  NS_LOG_FUNCTION (this << queue << item);
372 
373  // This method is called when a packet is discarded before being enqueued in the
374  // device queue, likely because the queue is full. This should not happen if the
375  // device correctly stops the queue. Anyway, stop the tx queue, so that the upper
376  // layers do not send packets until there is room in the queue again.
377 
378  NS_LOG_ERROR ("BUG! No room in the device queue for the received packet! ("
379  << queue->GetCurrentSize () << " inside)");
380 
381  Stop ();
382 }
383 
384 } // namespace ns3
385 
386 #endif /* NET_DEVICE_QUEUE_INTERFACE_H */
void ConnectQueueTraces(Ptr< QueueType > queue)
Connect the traced callbacks of a queue to the methods providing support for flow control and dynamic...
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 "...
NS_ASSERT_MSG(false, "Ipv4AddressGenerator::MaskToIndex(): Impossible")
bool m_stoppedByDevice
True if the queue has been stopped by the device.
void NotifyQueuedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes queued to the device queue. ...
bool m_stoppedByQueueLimits
True if the queue has been stopped by a queue limits object.
#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
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:564
WakeCallback m_wakeCallback
Wake callback.
void SetSelectQueueCallback(SelectQueueCallback cb)
Set the select queue callback.
NS_LOG_TEMPLATE_DECLARE
redefinition of the log component
void NotifyAggregatedObject(Ptr< NetDeviceQueueInterface > ndqi)
Notify this NetDeviceQueue that the NetDeviceQueueInterface was aggregated to an object.
bool IsStopped(void) const
Get the status of the device transmission queue.
std::size_t GetNTxQueues(void) const
Get the number of device transmission queues.
Callback< void > WakeCallback
Callback invoked by netdevices to wake upper layers.
virtual void Start(void)
Called by the device to start this device transmission queue.
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...
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...
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...
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
static TypeId GetTypeId(void)
Get the type ID.
SelectQueueCallback m_selectQueueCallback
Select queue callback.
Network device transmission queue interface.
SelectQueueCallback GetSelectQueueCallback(void) const
Get the select queue callback.
void ResetQueueLimits()
Reset queue limits state.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Stop(void)
Called by the device to stop this device transmission queue.
std::vector< Ptr< NetDeviceQueue > > m_txQueuesVector
Device transmission queues.
virtual void DoDispose(void)
Dispose of the object.
void SetQueueLimits(Ptr< QueueLimits > ql)
Set queue limits to this queue.
std::function< std::size_t(Ptr< QueueItem >)> SelectQueueCallback
Callback invoked to determine the tx queue selected for a given packet.
void NotifyTransmittedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes it is going to transmit.
Ptr< QueueLimits > m_queueLimits
Queue limits object.
virtual void Wake(void)
Called by the device to wake the queue disc associated with this device transmission queue...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
Ptr< NetDevice > m_device
the netdevice aggregated to the NetDeviceQueueInterface
Ptr< QueueLimits > GetQueueLimits()
Get queue limits to this queue.
virtual void SetWakeCallback(WakeCallback cb)
Set the wake callback.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:256
Ptr< NetDeviceQueue > GetTxQueue(std::size_t i) const
Get the i-th transmission queue of the device.
A base class which provides memory management and object aggregation.
Definition: object.h:87
virtual void NotifyNewAggregate(void)
Notify that an object was aggregated.
void SetNTxQueues(std::size_t numTxQueues)
Set the number of device transmission queues to create.
Network device transmission queue.
A template-based reference counting class.
a unique identifier for an interface.
Definition: type-id.h:58