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  Ptr<Packet> p = item->GetPacket ();
101 
103  {
104  NS_LOG_LOGIC ("Queue full (at max packets) -- dropping pkt");
105  Drop (p);
106  return false;
107  }
108 
109  if (m_mode == QUEUE_MODE_BYTES && (m_nBytes.Get () + item->GetPacketSize () > m_maxBytes))
110  {
111  NS_LOG_LOGIC ("Queue full (packet would exceed max bytes) -- dropping pkt");
112  Drop (p);
113  return false;
114  }
115 
116  //
117  // If DoEnqueue fails, Queue::Drop is called by the subclass
118  //
119  bool retval = DoEnqueue (item);
120  if (retval)
121  {
122  NS_LOG_LOGIC ("m_traceEnqueue (p)");
123  m_traceEnqueue (item->GetPacket ());
124 
125  uint32_t size = item->GetPacketSize ();
126  m_nBytes += size;
127  m_nTotalReceivedBytes += size;
128 
129  m_nPackets++;
131  }
132  return retval;
133 }
134 
137 {
138  NS_LOG_FUNCTION (this);
139 
140  if (m_nPackets.Get () == 0)
141  {
142  NS_LOG_LOGIC ("Queue empty");
143  return 0;
144  }
145 
146  Ptr<QueueItem> item = DoDequeue ();
147 
148  if (item != 0)
149  {
150  NS_ASSERT (m_nBytes.Get () >= item->GetPacketSize ());
151  NS_ASSERT (m_nPackets.Get () > 0);
152 
153  m_nBytes -= item->GetPacketSize ();
154  m_nPackets--;
155 
156  NS_LOG_LOGIC ("m_traceDequeue (packet)");
157  m_traceDequeue (item->GetPacket ());
158  }
159  return item;
160 }
161 
162 void
164 {
165  NS_LOG_FUNCTION (this);
166  while (!IsEmpty ())
167  {
168  Dequeue ();
169  }
170 }
171 
173 Queue::Peek (void) const
174 {
175  NS_LOG_FUNCTION (this);
176 
177  if (m_nPackets.Get () == 0)
178  {
179  NS_LOG_LOGIC ("Queue empty");
180  return 0;
181  }
182 
183  return DoPeek ();
184 }
185 
186 
187 uint32_t
188 Queue::GetNPackets (void) const
189 {
190  NS_LOG_FUNCTION (this);
191  NS_LOG_LOGIC ("returns " << m_nPackets);
192  return m_nPackets;
193 }
194 
195 uint32_t
196 Queue::GetNBytes (void) const
197 {
198  NS_LOG_FUNCTION (this);
199  NS_LOG_LOGIC (" returns " << m_nBytes);
200  return m_nBytes;
201 }
202 
203 bool
204 Queue::IsEmpty (void) const
205 {
206  NS_LOG_FUNCTION (this);
207  NS_LOG_LOGIC ("returns " << (m_nPackets.Get () == 0));
208  return m_nPackets.Get () == 0;
209 }
210 
211 uint32_t
213 {
214  NS_LOG_FUNCTION (this);
215  NS_LOG_LOGIC ("returns " << m_nTotalReceivedBytes);
216  return m_nTotalReceivedBytes;
217 }
218 
219 uint32_t
221 {
222  NS_LOG_FUNCTION (this);
223  NS_LOG_LOGIC ("returns " << m_nTotalReceivedPackets);
225 }
226 
227 uint32_t
229 {
230  NS_LOG_FUNCTION (this);
231  NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytes);
232  return m_nTotalDroppedBytes;
233 }
234 
235 uint32_t
237 {
238  NS_LOG_FUNCTION (this);
239  NS_LOG_LOGIC ("returns " << m_nTotalDroppedPackets);
240  return m_nTotalDroppedPackets;
241 }
242 
243 void
245 {
246  NS_LOG_FUNCTION (this);
251 }
252 
253 void
255 {
256  NS_LOG_FUNCTION (this << mode);
257 
258  if (mode == QUEUE_MODE_BYTES && m_mode == QUEUE_MODE_PACKETS)
259  {
260  NS_ABORT_MSG_IF (m_nPackets.Get () != 0,
261  "Cannot change queue mode in a queue with packets.");
262  }
263  else if (mode == QUEUE_MODE_PACKETS && m_mode == QUEUE_MODE_BYTES)
264  {
265  NS_ABORT_MSG_IF (m_nBytes.Get () != 0,
266  "Cannot change queue mode in a queue with packets.");
267  }
268 
269  m_mode = mode;
270 }
271 
273 Queue::GetMode (void) const
274 {
275  NS_LOG_FUNCTION (this);
276  return m_mode;
277 }
278 
279 void
280 Queue::SetMaxPackets (uint32_t maxPackets)
281 {
282  NS_LOG_FUNCTION (this << maxPackets);
283 
284  if (m_mode == QUEUE_MODE_PACKETS)
285  {
286  NS_ABORT_MSG_IF (maxPackets < m_nPackets.Get (),
287  "The new queue size cannot be less than the number of currently stored packets.");
288  }
289 
290  m_maxPackets = maxPackets;
291 }
292 
293 uint32_t
295 {
296  NS_LOG_FUNCTION (this);
297  return m_maxPackets;
298 }
299 
300 void
301 Queue::SetMaxBytes (uint32_t maxBytes)
302 {
303  NS_LOG_FUNCTION (this << maxBytes);
304 
305  if (m_mode == QUEUE_MODE_BYTES)
306  {
307  NS_ABORT_MSG_IF (maxBytes < m_nBytes.Get (),
308  "The new queue size cannot be less than the amount of bytes of currently stored packets.");
309  }
310 
311  m_maxBytes = maxBytes;
312 }
313 
314 uint32_t
315 Queue::GetMaxBytes (void) const
316 {
317  NS_LOG_FUNCTION (this);
318  return m_maxBytes;
319 }
320 
321 void
323 {
324  NS_LOG_FUNCTION (this << p);
325 
327  m_nTotalDroppedBytes += p->GetSize ();
328 
329  NS_LOG_LOGIC ("m_traceDrop (p)");
330  m_traceDrop (p);
331 }
332 
333 } // namespace ns3
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:238
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:233
#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:301
Use number of bytes for maximum queue size.
Definition: queue.h:128
bool IsEmpty(void) const
Definition: queue.cc:204
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:236
TracedValue< uint32_t > m_nPackets
Number of packets in the queue.
Definition: queue.h:231
uint32_t m_nTotalReceivedBytes
Total received bytes.
Definition: queue.h:230
#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
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:786
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes...
Definition: queue.cc:244
T Get(void) const
Get the underlying value.
Definition: traced-value.h:217
uint32_t m_maxPackets
max packets in the queue
Definition: queue.h:236
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:229
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:212
QueueMode
Enumeration of the modes supported in the class.
Definition: queue.h:125
Hold variables of type enum.
Definition: enum.h:54
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.cc:33
Hold an unsigned integer type.
Definition: uinteger.h:44
uint32_t GetMaxPackets(void) const
Definition: queue.cc:294
virtual ~Queue()
Definition: queue.cc:90
uint32_t GetNPackets(void) const
Definition: queue.cc:188
uint32_t GetMaxBytes(void) const
Definition: queue.cc:315
#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:225
Queue::QueueMode GetMode(void) const
Get the encapsulation mode of this device.
Definition: queue.cc:273
TracedCallback< Ptr< const Packet > > m_traceDrop
Traced callback: fired when a packet is dropped.
Definition: queue.h:227
uint32_t m_maxBytes
max bytes in the queue
Definition: queue.h:237
uint32_t GetNBytes(void) const
Definition: queue.cc:196
void Drop(Ptr< Packet > p)
Drop a packet.
Definition: queue.cc:322
void SetMode(Queue::QueueMode mode)
Set the operating mode of this device.
Definition: queue.cc:254
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:220
#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 an item from the queue.
uint32_t m_nTotalReceivedPackets
Total received packets.
Definition: queue.h:232
void SetMaxPackets(uint32_t maxPackets)
Set the maximum amount of packets that can be stored in this queue.
Definition: queue.cc:280
uint32_t m_nTotalDroppedPackets
Total dropped packets.
Definition: queue.h:234
void DequeueAll(void)
Flush the queue.
Definition: queue.cc:163
TracedCallback< Ptr< const Packet > > m_traceEnqueue
Traced callback: fired when a packet is enqueued.
Definition: queue.h:223
Ptr< const QueueItem > Peek(void) const
Get a copy of the item at the front of the queue without removing it.
Definition: queue.cc:173
Use number of packets for maximum queue size.
Definition: queue.h:127
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:228
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:826
Ptr< QueueItem > Dequeue(void)
Remove an item from the front of the Queue.
Definition: queue.cc:136