A Discrete-Event Network Simulator
API
net-device-queue-interface.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 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 <stefano.avallone@.unina.it>
18 */
19#ifndef NET_DEVICE_QUEUE_INTERFACE_H
20#define NET_DEVICE_QUEUE_INTERFACE_H
21
22#include "ns3/callback.h"
23#include "ns3/log.h"
24#include "ns3/net-device.h"
25#include "ns3/object-factory.h"
26#include "ns3/object.h"
27#include "ns3/ptr.h"
28
29#include <functional>
30#include <vector>
31
32namespace ns3
33{
34
35class QueueLimits;
36class NetDeviceQueueInterface;
37class QueueItem;
38
56class NetDeviceQueue : public Object
57{
58 public:
63 static TypeId GetTypeId();
64
66 ~NetDeviceQueue() override;
67
72 virtual void Start();
73
78 virtual void Stop();
79
85 virtual void Wake();
86
94 virtual bool IsStopped() 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>
205
206 private:
212
214};
215
230{
231 public:
236 static TypeId GetTypeId();
237
242 ~NetDeviceQueueInterface() override;
243
252 Ptr<NetDeviceQueue> GetTxQueue(std::size_t i) const;
253
258 std::size_t GetNTxQueues() const;
259
269
278 void SetNTxQueues(std::size_t numTxQueues);
279
281 typedef std::function<std::size_t(Ptr<QueueItem>)> SelectQueueCallback;
282
291
300
301 protected:
305 void DoDispose() override;
309 void NotifyNewAggregate() override;
310
311 private:
313 std::vector<Ptr<NetDeviceQueue>> m_txQueuesVector;
315};
316
321template <typename QueueType>
322void
324{
325 NS_ASSERT(queue);
326
327 queue->TraceConnectWithoutContext(
328 "Enqueue",
329 MakeCallback(&NetDeviceQueue::PacketEnqueued<QueueType>, this).Bind(PeekPointer(queue)));
330 queue->TraceConnectWithoutContext(
331 "Dequeue",
332 MakeCallback(&NetDeviceQueue::PacketDequeued<QueueType>, this).Bind(PeekPointer(queue)));
333 queue->TraceConnectWithoutContext(
334 "DropBeforeEnqueue",
335 MakeCallback(&NetDeviceQueue::PacketDiscarded<QueueType>, this).Bind(PeekPointer(queue)));
336}
337
338template <typename QueueType>
339void
341{
342 NS_LOG_FUNCTION(this << queue << item);
343
344 // Inform BQL
345 NotifyQueuedBytes(item->GetSize());
346
347 NS_ASSERT_MSG(m_device, "Aggregated NetDevice not set");
348 // After enqueuing a packet, we need to check whether the queue is able to
349 // store another packet. If not, we stop the queue
350
351 if (queue->WouldOverflow(1, m_device->GetMtu()))
352 {
353 NS_LOG_DEBUG("The device queue is being stopped (" << queue->GetCurrentSize()
354 << " inside)");
355 Stop();
356 }
357}
358
359template <typename QueueType>
360void
362{
363 NS_LOG_FUNCTION(this << queue << item);
364
365 // Inform BQL
366 NotifyTransmittedBytes(item->GetSize());
367
368 NS_ASSERT_MSG(m_device, "Aggregated NetDevice not set");
369 // After dequeuing a packet, if there is room for another packet we
370 // call Wake () that ensures that the queue is not stopped and restarts
371 // the queue disc if the queue was stopped
372
373 if (!queue->WouldOverflow(1, m_device->GetMtu()))
374 {
375 Wake();
376 }
377}
378
379template <typename QueueType>
380void
382{
383 NS_LOG_FUNCTION(this << queue << item);
384
385 // This method is called when a packet is discarded before being enqueued in the
386 // device queue, likely because the queue is full. This should not happen if the
387 // device correctly stops the queue. Anyway, stop the tx queue, so that the upper
388 // layers do not send packets until there is room in the queue again.
389
390 NS_LOG_ERROR("BUG! No room in the device queue for the received packet! ("
391 << queue->GetCurrentSize() << " inside)");
392
393 Stop();
394}
395
396} // namespace ns3
397
398#endif /* NET_DEVICE_QUEUE_INTERFACE_H */
Network device transmission queue.
bool m_stoppedByQueueLimits
True if the queue has been stopped by a queue limits object.
void ConnectQueueTraces(Ptr< QueueType > queue)
Connect the traced callbacks of a queue to the methods providing support for flow control and dynamic...
virtual void Stop()
Called by the device to stop this device transmission queue.
void NotifyAggregatedObject(Ptr< NetDeviceQueueInterface > ndqi)
Notify this NetDeviceQueue that the NetDeviceQueueInterface was aggregated to an object.
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...
Ptr< QueueLimits > GetQueueLimits()
Get queue limits to this queue.
Ptr< QueueLimits > m_queueLimits
Queue limits object.
virtual void Wake()
Called by the device to wake the queue disc associated with this device transmission queue.
virtual bool IsStopped() const
Get the status of the device transmission queue.
void SetQueueLimits(Ptr< QueueLimits > ql)
Set queue limits to this queue.
NS_LOG_TEMPLATE_DECLARE
redefinition of the log component
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...
void ResetQueueLimits()
Reset queue limits state.
virtual void Start()
Called by the device to start this device transmission queue.
virtual void NotifyTransmittedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes it is going to transmit.
virtual void NotifyQueuedBytes(uint32_t bytes)
Called by the netdevice to report the number of bytes queued to the device queue.
Ptr< NetDevice > m_device
the netdevice aggregated to the NetDeviceQueueInterface
WakeCallback m_wakeCallback
Wake callback.
Callback< void > WakeCallback
Callback invoked by netdevices to wake upper layers.
bool m_stoppedByDevice
True if the queue has been stopped by the device.
static TypeId GetTypeId()
Get the type ID.
virtual void SetWakeCallback(WakeCallback cb)
Set the wake callback.
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...
Network device transmission queue interface.
SelectQueueCallback m_selectQueueCallback
Select queue callback.
std::vector< Ptr< NetDeviceQueue > > m_txQueuesVector
Device transmission queues.
std::size_t GetNTxQueues() const
Get the number of device transmission queues.
Ptr< NetDeviceQueue > GetTxQueue(std::size_t i) const
Get the i-th transmission queue of the device.
std::function< std::size_t(Ptr< QueueItem >)> SelectQueueCallback
Callback invoked to determine the tx queue selected for a given packet.
ObjectFactory m_txQueues
Device transmission queues TypeId.
void SetSelectQueueCallback(SelectQueueCallback cb)
Set the select queue callback.
void DoDispose() override
Dispose of the object.
SelectQueueCallback GetSelectQueueCallback() const
Get the select queue callback.
void SetTxQueuesType(TypeId type)
Set the type of device transmission queues to create.
void SetNTxQueues(std::size_t numTxQueues)
Set the number of device transmission queues to create.
void NotifyNewAggregate() override
Notify that an object was aggregated.
static TypeId GetTypeId()
Get the type ID.
Instantiate subclasses of ns3::Object.
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
a unique identifier for an interface.
Definition: type-id.h:60
#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_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:86
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#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.
U * PeekPointer(const Ptr< U > &p)
Definition: ptr.h:488
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:691