A Discrete-Event Network Simulator
API
net-device-queue-interface.cc
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 
20 #include "ns3/net-device-queue-interface.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/queue-item.h"
24 #include "ns3/queue-limits.h"
25 #include "ns3/uinteger.h"
26 
27 namespace ns3
28 {
29 
30 NS_LOG_COMPONENT_DEFINE("NetDeviceQueueInterface");
31 
32 TypeId
34 {
35  static TypeId tid = TypeId("ns3::NetDeviceQueue")
36  .SetParent<Object>()
37  .SetGroupName("Network")
38  .AddConstructor<NetDeviceQueue>();
39  return tid;
40 }
41 
43  : m_stoppedByDevice(false),
44  m_stoppedByQueueLimits(false),
45  NS_LOG_TEMPLATE_DEFINE("NetDeviceQueueInterface")
46 {
47  NS_LOG_FUNCTION(this);
48 }
49 
51 {
52  NS_LOG_FUNCTION(this);
53 
54  m_queueLimits = nullptr;
56  m_device = nullptr;
57 }
58 
59 bool
61 {
62  NS_LOG_FUNCTION(this);
64 }
65 
66 void
68 {
69  NS_LOG_FUNCTION(this);
70  m_stoppedByDevice = false;
71 }
72 
73 void
75 {
76  NS_LOG_FUNCTION(this);
77  m_stoppedByDevice = true;
78 }
79 
80 void
82 {
83  NS_LOG_FUNCTION(this);
84 
85  bool wasStoppedByDevice = m_stoppedByDevice;
86  m_stoppedByDevice = false;
87 
88  // Request the queue disc to dequeue a packet
89  if (wasStoppedByDevice && !m_wakeCallback.IsNull())
90  {
92  }
93 }
94 
95 void
97 {
98  NS_LOG_FUNCTION(this << ndqi);
99 
100  m_device = ndqi->GetObject<NetDevice>();
101  NS_ABORT_MSG_IF(!m_device, "No NetDevice object was aggregated to the NetDeviceQueueInterface");
102 }
103 
104 void
106 {
107  m_wakeCallback = cb;
108 }
109 
110 void
112 {
113  NS_LOG_FUNCTION(this << bytes);
114  if (!m_queueLimits)
115  {
116  return;
117  }
118  m_queueLimits->Queued(bytes);
119  if (m_queueLimits->Available() >= 0)
120  {
121  return;
122  }
123  m_stoppedByQueueLimits = true;
124 }
125 
126 void
128 {
129  NS_LOG_FUNCTION(this << bytes);
130  if ((!m_queueLimits) || (!bytes))
131  {
132  return;
133  }
134  m_queueLimits->Completed(bytes);
135  if (m_queueLimits->Available() < 0)
136  {
137  return;
138  }
139  bool wasStoppedByQueueLimits = m_stoppedByQueueLimits;
140  m_stoppedByQueueLimits = false;
141  // Request the queue disc to dequeue a packet
142  if (wasStoppedByQueueLimits && !m_wakeCallback.IsNull())
143  {
144  m_wakeCallback();
145  }
146 }
147 
148 void
150 {
151  NS_LOG_FUNCTION(this);
152  if (!m_queueLimits)
153  {
154  return;
155  }
156  m_queueLimits->Reset();
157 }
158 
159 void
161 {
162  NS_LOG_FUNCTION(this << ql);
163  m_queueLimits = ql;
164 }
165 
168 {
169  NS_LOG_FUNCTION(this);
170  return m_queueLimits;
171 }
172 
174 
175 TypeId
177 {
178  static TypeId tid =
179  TypeId("ns3::NetDeviceQueueInterface")
180  .SetParent<Object>()
181  .SetGroupName("Network")
182  .AddConstructor<NetDeviceQueueInterface>()
183  .AddAttribute("TxQueuesType",
184  "The type of transmission queues to be used",
189  .AddAttribute("NTxQueues",
190  "The number of device transmission queues",
192  UintegerValue(1),
195  MakeUintegerChecker<uint16_t>(1, 65535));
196  return tid;
197 }
198 
200 {
201  NS_LOG_FUNCTION(this);
202 
203  // the default select queue callback returns 0
204  m_selectQueueCallback = [](Ptr<QueueItem> item) { return 0; };
205 }
206 
208 {
209  NS_LOG_FUNCTION(this);
210 }
211 
214 {
215  NS_ASSERT(i < m_txQueuesVector.size());
216  return m_txQueuesVector[i];
217 }
218 
219 std::size_t
221 {
222  return m_txQueuesVector.size();
223 }
224 
225 void
227 {
228  NS_LOG_FUNCTION(this);
229 
230  m_txQueuesVector.clear();
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION(this);
238 
239  // Notify the NetDeviceQueue objects that an object was aggregated
240  for (auto& tx : m_txQueuesVector)
241  {
242  tx->NotifyAggregatedObject(this);
243  }
245 }
246 
247 void
249 {
250  NS_LOG_FUNCTION(this << type);
251 
253  "Cannot call SetTxQueuesType after creating device queues");
254 
257 }
258 
259 void
261 {
262  NS_LOG_FUNCTION(this << numTxQueues);
263  NS_ASSERT(numTxQueues > 0);
264 
266  "Cannot call SetNTxQueues after creating device queues");
267 
268  // create the netdevice queues
269  for (std::size_t i = 0; i < numTxQueues; i++)
270  {
272  }
273 }
274 
275 void
277 {
279 }
280 
283 {
284  return m_selectQueueCallback;
285 }
286 
287 } // namespace ns3
void Nullify()
Discard the implementation, set it to null.
Definition: callback.h:578
bool IsNull() const
Check for null implementation.
Definition: callback.h:572
Network layer to device interface.
Definition: net-device.h:98
Network device transmission queue.
bool m_stoppedByQueueLimits
True if the queue has been stopped by a queue limits object.
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.
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.
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.
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.
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.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
A base class which provides memory management and object aggregation.
Definition: object.h:89
friend class ObjectFactory
Friends.
Definition: object.h:328
virtual void NotifyNewAggregate()
Notify all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:332
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
virtual void DoDispose()
Destructor implementation.
Definition: object.cc:353
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:78
a unique identifier for an interface.
Definition: type-id.h:60
@ ATTR_GET
The attribute can be read.
Definition: type-id.h:65
@ ATTR_CONSTRUCT
The attribute can be written at construction-time.
Definition: type-id.h:67
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:935
AttributeValue implementation for TypeId.
Definition: type-id.h:600
Hold an unsigned integer type.
Definition: uinteger.h:45
#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
Ptr< const AttributeChecker > MakeTypeIdChecker()
Definition: type-id.cc:1254
Ptr< const AttributeAccessor > MakeTypeIdAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: type-id.h:600
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:46
#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_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition: log.h:236
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.