A Discrete-Event Network Simulator
API
queue.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 University of Washington
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 
19 #include "ns3/log.h"
20 #include "ns3/abort.h"
21 #include "ns3/enum.h"
22 #include "ns3/uinteger.h"
23 #include "ns3/trace-source-accessor.h"
24 #include "queue.h"
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("Queue");
29 
31 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::Queue")
36  .SetParent<Object> ()
37  .SetGroupName ("Network")
38  .AddAttribute ("Mode",
39  "Whether to use bytes (see MaxBytes) or packets (see MaxPackets) as the maximum queue size metric.",
43  MakeEnumChecker (QUEUE_MODE_BYTES, "QUEUE_MODE_BYTES",
44  QUEUE_MODE_PACKETS, "QUEUE_MODE_PACKETS"))
45  .AddAttribute ("MaxPackets",
46  "The maximum number of packets accepted by this queue.",
47  UintegerValue (100),
50  MakeUintegerChecker<uint32_t> ())
51  .AddAttribute ("MaxBytes",
52  "The maximum number of bytes accepted by this queue.",
53  UintegerValue (100 * 65535),
56  MakeUintegerChecker<uint32_t> ())
57  .AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
59  "ns3::Packet::TracedCallback")
60  .AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
62  "ns3::Packet::TracedCallback")
63  .AddTraceSource ("Drop", "Drop a packet (for whatever reason).",
65  "ns3::Packet::TracedCallback")
66  .AddTraceSource ("PacketsInQueue",
67  "Number of packets currently stored in the queue",
69  "ns3::TracedValueCallback::Uint32")
70  .AddTraceSource ("BytesInQueue",
71  "Number of bytes currently stored in the queue",
73  "ns3::TracedValueCallback::Uint32")
74  ;
75  return tid;
76 }
77 
79  m_nBytes (0),
80  m_nTotalReceivedBytes (0),
81  m_nPackets (0),
82  m_nTotalReceivedPackets (0),
83  m_nTotalDroppedBytes (0),
84  m_nTotalDroppedPackets (0),
85  m_mode (QUEUE_MODE_PACKETS)
86 {
87  NS_LOG_FUNCTION (this);
88 }
89 
91 {
92  NS_LOG_FUNCTION (this);
93 }
94 
95 
96 bool
98 {
99  NS_LOG_FUNCTION (this << item);
100 
102  {
103  NS_LOG_LOGIC ("Queue full (at max packets) -- dropping pkt");
104  Drop (item);
105  return false;
106  }
107 
108  if (m_mode == QUEUE_MODE_BYTES && (m_nBytes.Get () + item->GetPacketSize () > m_maxBytes))
109  {
110  NS_LOG_LOGIC ("Queue full (packet would exceed max bytes) -- dropping pkt");
111  Drop (item);
112  return false;
113  }
114 
115  //
116  // If DoEnqueue fails, Queue::Drop is called by the subclass
117  //
118  bool retval = DoEnqueue (item);
119  if (retval)
120  {
121  NS_LOG_LOGIC ("m_traceEnqueue (p)");
122  m_traceEnqueue (item->GetPacket ());
123 
124  uint32_t size = item->GetPacketSize ();
125  m_nBytes += size;
126  m_nTotalReceivedBytes += size;
127 
128  m_nPackets++;
130  }
131  return retval;
132 }
133 
136 {
137  NS_LOG_FUNCTION (this);
138 
139  if (m_nPackets.Get () == 0)
140  {
141  NS_LOG_LOGIC ("Queue empty");
142  return 0;
143  }
144 
145  Ptr<QueueItem> item = DoDequeue ();
146 
147  if (item != 0)
148  {
149  NS_ASSERT (m_nBytes.Get () >= item->GetPacketSize ());
150  NS_ASSERT (m_nPackets.Get () > 0);
151 
152  m_nBytes -= item->GetPacketSize ();
153  m_nPackets--;
154 
155  NS_LOG_LOGIC ("m_traceDequeue (packet)");
156  m_traceDequeue (item->GetPacket ());
157  }
158  return item;
159 }
160 
163 {
164  NS_LOG_FUNCTION (this);
165 
166  if (m_nPackets.Get () == 0)
167  {
168  NS_LOG_LOGIC ("Queue empty");
169  return 0;
170  }
171 
172  Ptr<QueueItem> item = DoRemove ();
173 
174  if (item != 0)
175  {
176  NS_ASSERT (m_nBytes.Get () >= item->GetPacketSize ());
177  NS_ASSERT (m_nPackets.Get () > 0);
178 
179  m_nBytes -= item->GetPacketSize ();
180  m_nPackets--;
181 
182  Drop (item);
183  }
184  return item;
185 }
186 
187 void
189 {
190  NS_LOG_FUNCTION (this);
191  while (!IsEmpty ())
192  {
193  Dequeue ();
194  }
195 }
196 
198 Queue::Peek (void) const
199 {
200  NS_LOG_FUNCTION (this);
201 
202  if (m_nPackets.Get () == 0)
203  {
204  NS_LOG_LOGIC ("Queue empty");
205  return 0;
206  }
207 
208  return DoPeek ();
209 }
210 
211 
212 uint32_t
213 Queue::GetNPackets (void) const
214 {
215  NS_LOG_FUNCTION (this);
216  NS_LOG_LOGIC ("returns " << m_nPackets);
217  return m_nPackets;
218 }
219 
220 uint32_t
221 Queue::GetNBytes (void) const
222 {
223  NS_LOG_FUNCTION (this);
224  NS_LOG_LOGIC (" returns " << m_nBytes);
225  return m_nBytes;
226 }
227 
228 bool
229 Queue::IsEmpty (void) const
230 {
231  NS_LOG_FUNCTION (this);
232  NS_LOG_LOGIC ("returns " << (m_nPackets.Get () == 0));
233  return m_nPackets.Get () == 0;
234 }
235 
236 uint32_t
238 {
239  NS_LOG_FUNCTION (this);
240  NS_LOG_LOGIC ("returns " << m_nTotalReceivedBytes);
241  return m_nTotalReceivedBytes;
242 }
243 
244 uint32_t
246 {
247  NS_LOG_FUNCTION (this);
248  NS_LOG_LOGIC ("returns " << m_nTotalReceivedPackets);
250 }
251 
252 uint32_t
254 {
255  NS_LOG_FUNCTION (this);
256  NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytes);
257  return m_nTotalDroppedBytes;
258 }
259 
260 uint32_t
262 {
263  NS_LOG_FUNCTION (this);
264  NS_LOG_LOGIC ("returns " << m_nTotalDroppedPackets);
265  return m_nTotalDroppedPackets;
266 }
267 
268 void
270 {
271  NS_LOG_FUNCTION (this);
276 }
277 
278 void
280 {
281  NS_LOG_FUNCTION (this << mode);
282 
283  if (mode == QUEUE_MODE_BYTES && m_mode == QUEUE_MODE_PACKETS)
284  {
285  NS_ABORT_MSG_IF (m_nPackets.Get () != 0,
286  "Cannot change queue mode in a queue with packets.");
287  }
288  else if (mode == QUEUE_MODE_PACKETS && m_mode == QUEUE_MODE_BYTES)
289  {
290  NS_ABORT_MSG_IF (m_nBytes.Get () != 0,
291  "Cannot change queue mode in a queue with packets.");
292  }
293 
294  m_mode = mode;
295 }
296 
298 Queue::GetMode (void) const
299 {
300  NS_LOG_FUNCTION (this);
301  return m_mode;
302 }
303 
304 void
305 Queue::SetMaxPackets (uint32_t maxPackets)
306 {
307  NS_LOG_FUNCTION (this << maxPackets);
308 
309  if (m_mode == QUEUE_MODE_PACKETS)
310  {
311  NS_ABORT_MSG_IF (maxPackets < m_nPackets.Get (),
312  "The new queue size cannot be less than the number of currently stored packets.");
313  }
314 
315  m_maxPackets = maxPackets;
316 }
317 
318 uint32_t
320 {
321  NS_LOG_FUNCTION (this);
322  return m_maxPackets;
323 }
324 
325 void
326 Queue::SetMaxBytes (uint32_t maxBytes)
327 {
328  NS_LOG_FUNCTION (this << maxBytes);
329 
330  if (m_mode == QUEUE_MODE_BYTES)
331  {
332  NS_ABORT_MSG_IF (maxBytes < m_nBytes.Get (),
333  "The new queue size cannot be less than the amount of bytes of currently stored packets.");
334  }
335 
336  m_maxBytes = maxBytes;
337 }
338 
339 uint32_t
340 Queue::GetMaxBytes (void) const
341 {
342  NS_LOG_FUNCTION (this);
343  return m_maxBytes;
344 }
345 
346 void
348 {
349  m_dropCallback = cb;
350 }
351 
352 void
354 {
355  NS_LOG_FUNCTION (this << item);
356 
357  if (!m_dropCallback.IsNull ())
358  {
359  m_dropCallback (item);
360  }
361 }
362 
363 void
365 {
366  NS_LOG_FUNCTION (this << item);
367 
369  m_nTotalDroppedBytes += item->GetPacketSize ();
370 
371  NS_LOG_LOGIC ("m_traceDrop (p)");
372  m_traceDrop (item->GetPacket ());
373  NotifyDrop (item);
374 }
375 
376 } // namespace ns3
void NotifyDrop(Ptr< QueueItem > item)
Notification of a packet drop.
Definition: queue.cc:353
virtual bool DoEnqueue(Ptr< QueueItem > item)=0
Push an item in the queue.
QueueMode m_mode
queue mode (packets or bytes limited)
Definition: queue.h:266
Queue()
Definition: queue.cc:78
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 "...
uint32_t m_nTotalDroppedBytes
Total dropped bytes.
Definition: queue.h:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
void SetMaxBytes(uint32_t maxBytes)
Set the maximum amount of bytes that can be stored in this queue.
Definition: queue.cc:326
Use number of bytes for maximum queue size.
Definition: queue.h:133
bool IsEmpty(void) const
Definition: queue.cc:229
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: enum.h:209
uint32_t GetTotalDroppedPackets(void) const
Definition: queue.cc:261
TracedValue< uint32_t > m_nPackets
Number of packets in the queue.
Definition: queue.h:259
uint32_t m_nTotalReceivedBytes
Total received bytes.
Definition: queue.h:258
bool IsNull(void) const
Check for null implementation.
Definition: callback.h:1270
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
virtual void SetDropCallback(DropCallback cb)
Set the drop callback.
Definition: queue.cc:347
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes...
Definition: queue.cc:269
T Get(void) const
Get the underlying value.
Definition: traced-value.h:218
uint32_t m_maxPackets
max packets in the queue
Definition: queue.h:264
virtual Ptr< const QueueItem > DoPeek(void) const =0
Peek the front item in the queue.
TracedValue< uint32_t > m_nBytes
Number of bytes in the queue.
Definition: queue.h:257
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
virtual Ptr< QueueItem > DoRemove(void)=0
Pull the item to drop from the queue.
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:237
QueueMode
Enumeration of the modes supported in the class.
Definition: queue.h:130
Hold variables of type enum.
Definition: enum.h:54
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.cc:33
void Drop(Ptr< QueueItem > item)
Drop a packet.
Definition: queue.cc:364
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t GetMaxPackets(void) const
Definition: queue.cc:319
virtual ~Queue()
Definition: queue.cc:90
Ptr< QueueItem > Remove(void)
Remove an item from the front of the Queue, counting it as dropped.
Definition: queue.cc:162
uint32_t GetNPackets(void) const
Definition: queue.cc:213
uint32_t GetMaxBytes(void) const
Definition: queue.cc:340
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
TracedCallback< Ptr< const Packet > > m_traceDequeue
Traced callback: fired when a packet is dequeued.
Definition: queue.h:253
Queue::QueueMode GetMode(void) const
Get the encapsulation mode of this device.
Definition: queue.cc:298
TracedCallback< Ptr< const Packet > > m_traceDrop
Traced callback: fired when a packet is dropped.
Definition: queue.h:255
uint32_t m_maxBytes
max bytes in the queue
Definition: queue.h:265
uint32_t GetNBytes(void) const
Definition: queue.cc:221
DropCallback m_dropCallback
drop callback
Definition: queue.h:267
void SetMode(Queue::QueueMode mode)
Set the operating mode of this device.
Definition: queue.cc:279
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
uint32_t GetTotalReceivedPackets(void) const
Definition: queue.cc:245
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
virtual Ptr< QueueItem > DoDequeue(void)=0
Pull the item to dequeue from the queue.
uint32_t m_nTotalReceivedPackets
Total received packets.
Definition: queue.h:260
void SetMaxPackets(uint32_t maxPackets)
Set the maximum amount of packets that can be stored in this queue.
Definition: queue.cc:305
uint32_t m_nTotalDroppedPackets
Total dropped packets.
Definition: queue.h:262
void DequeueAll(void)
Flush the queue.
Definition: queue.cc:188
TracedCallback< Ptr< const Packet > > m_traceEnqueue
Traced callback: fired when a packet is enqueued.
Definition: queue.h:251
Ptr< const QueueItem > Peek(void) const
Get a copy of the item at the front of the queue without removing it.
Definition: queue.cc:198
Use number of packets for maximum queue size.
Definition: queue.h:132
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:253
A base class which provides memory management and object aggregation.
Definition: object.h:87
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
bool Enqueue(Ptr< QueueItem > item)
Place a queue item into the rear of the Queue.
Definition: queue.cc:97
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
Ptr< QueueItem > Dequeue(void)
Remove an item from the front of the Queue, counting it as dequeued.
Definition: queue.cc:135