A Discrete-Event Network Simulator
API
net-device-queue-interface.cc
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 
21 #include "ns3/abort.h"
22 #include "ns3/queue-limits.h"
23 #include "ns3/net-device-queue-interface.h"
24 #include "ns3/simulator.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/queue-item.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("NetDeviceQueueInterface");
31 
33  : m_stoppedByDevice (false),
34  m_stoppedByQueueLimits (false),
35  NS_LOG_TEMPLATE_DEFINE ("NetDeviceQueueInterface")
36 {
37  NS_LOG_FUNCTION (this);
38 }
39 
41 {
42  NS_LOG_FUNCTION (this);
43 
44  m_queueLimits = 0;
46  m_device = 0;
47 }
48 
49 bool
51 {
52  NS_LOG_FUNCTION (this);
54 }
55 
56 void
58 {
59  NS_LOG_FUNCTION (this);
60  m_stoppedByDevice = false;
61 }
62 
63 void
65 {
66  NS_LOG_FUNCTION (this);
67  m_stoppedByDevice = true;
68 }
69 
70 void
72 {
73  NS_LOG_FUNCTION (this);
74 
75  bool wasStoppedByDevice = m_stoppedByDevice;
76  m_stoppedByDevice = false;
77 
78  // Request the queue disc to dequeue a packet
79  if (wasStoppedByDevice && !m_wakeCallback.IsNull ())
80  {
82  }
83 }
84 
85 void
87 {
88  NS_LOG_FUNCTION (this << ndqi);
89 
90  m_device = ndqi->GetObject<NetDevice> ();
91  NS_ABORT_MSG_IF (!m_device, "No NetDevice object was aggregated to the NetDeviceQueueInterface");
92 }
93 
94 void
96 {
97  m_wakeCallback = cb;
98 }
99 
100 void
102 {
103  NS_LOG_FUNCTION (this << bytes);
104  if (!m_queueLimits)
105  {
106  return;
107  }
108  m_queueLimits->Queued (bytes);
109  if (m_queueLimits->Available () >= 0)
110  {
111  return;
112  }
113  m_stoppedByQueueLimits = true;
114 }
115 
116 void
118 {
119  NS_LOG_FUNCTION (this << bytes);
120  if ((!m_queueLimits) || (!bytes))
121  {
122  return;
123  }
124  m_queueLimits->Completed (bytes);
125  if (m_queueLimits->Available () < 0)
126  {
127  return;
128  }
129  bool wasStoppedByQueueLimits = m_stoppedByQueueLimits;
130  m_stoppedByQueueLimits = false;
131  // Request the queue disc to dequeue a packet
132  if (wasStoppedByQueueLimits && !m_wakeCallback.IsNull ())
133  {
135  }
136 }
137 
138 void
140 {
141  NS_LOG_FUNCTION (this);
142  if (!m_queueLimits)
143  {
144  return;
145  }
146  m_queueLimits->Reset ();
147 }
148 
149 void
151 {
152  NS_LOG_FUNCTION (this << ql);
153  m_queueLimits = ql;
154 }
155 
158 {
159  NS_LOG_FUNCTION (this);
160  return m_queueLimits;
161 }
162 
163 
165 
166 TypeId
168 {
169  static TypeId tid = TypeId ("ns3::NetDeviceQueueInterface")
170  .SetParent<Object> ()
171  .SetGroupName("Network")
172  .AddConstructor<NetDeviceQueueInterface> ()
173  .AddAttribute ("NTxQueues", "The number of device transmission queues",
175  UintegerValue (1),
178  MakeUintegerChecker<uint16_t> (1, 65535))
179  ;
180  return tid;
181 }
182 
184 {
185  NS_LOG_FUNCTION (this);
186 
187  // the default select queue callback returns 0
188  m_selectQueueCallback = [] (Ptr<QueueItem> item) { return 0; };
189 }
190 
192 {
193  NS_LOG_FUNCTION (this);
194 }
195 
198 {
199  NS_ASSERT (i < m_txQueuesVector.size ());
200  return m_txQueuesVector[i];
201 }
202 
203 std::size_t
205 {
206  return m_txQueuesVector.size ();
207 }
208 
209 void
211 {
212  NS_LOG_FUNCTION (this);
213 
214  m_txQueuesVector.clear ();
216 }
217 
218 void
220 {
221  NS_LOG_FUNCTION (this);
222 
223  // Notify the NetDeviceQueue objects that an object was aggregated
224  for (auto& tx : m_txQueuesVector)
225  {
226  tx->NotifyAggregatedObject (this);
227  }
229 }
230 
231 void
232 NetDeviceQueueInterface::SetNTxQueues (std::size_t numTxQueues)
233 {
234  NS_LOG_FUNCTION (this << numTxQueues);
235  NS_ASSERT (numTxQueues > 0);
236 
237  NS_ABORT_MSG_IF (!m_txQueuesVector.empty (), "Cannot call SetNTxQueues after creating device queues");
238 
239  // create the netdevice queues
240  for (std::size_t i = 0; i < numTxQueues; i++)
241  {
242  m_txQueuesVector.push_back (Create<NetDeviceQueue> ());
243  }
244 }
245 
246 void
248 {
250 }
251 
254 {
255  return m_selectQueueCallback;
256 }
257 
258 } // namespace ns3
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 "...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
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
WakeCallback m_wakeCallback
Wake callback.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
void SetSelectQueueCallback(SelectQueueCallback cb)
Set the select queue callback.
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
The attribute can be written at construction-time.
Definition: type-id.h:65
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.
The attribute can be read.
Definition: type-id.h:63
virtual void Start(void)
Called by the device to start this device transmission queue.
#define NS_LOG_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition: log.h:238
Hold an unsigned integer type.
Definition: uinteger.h:44
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.
static EventId ScheduleNow(MEM mem_ptr, OBJ obj)
Schedule an event to expire Now.
Definition: simulator.h:1578
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.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
virtual void Wake(void)
Called by the device to wake the queue disc associated with this device transmission queue...
Network layer to device interface.
Definition: net-device.h:95
void Nullify(void)
Discard the implementation, set it to null.
Definition: callback.h:1274
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.
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 all Objects aggregated to this one of a new Object being aggregated.
Definition: object.cc:325
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.
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
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:45
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915