A Discrete-Event Network Simulator
API
queue.h
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 // The queue base class has a limit on its size, in terms of number of
20 // packets or number of bytes depending on the operating mode.
21 // The base class implements tracing and basic statistics calculations.
22 
23 #ifndef QUEUE_H
24 #define QUEUE_H
25 
26 #include "ns3/packet.h"
27 #include "ns3/object.h"
28 #include "ns3/traced-callback.h"
29 #include "ns3/traced-value.h"
30 #include "ns3/unused.h"
31 #include "ns3/log.h"
32 #include <string>
33 #include <sstream>
34 #include <list>
35 
36 namespace ns3 {
37 
50 class QueueBase : public Object
51 {
52 public:
57  static TypeId GetTypeId (void);
58 
59  QueueBase ();
60  virtual ~QueueBase ();
61 
81  static void AppendItemTypeIfNotPresent (std::string& typeId, const std::string& itemType);
82 
86  bool IsEmpty (void) const;
87 
91  uint32_t GetNPackets (void) const;
92 
96  uint32_t GetNBytes (void) const;
97 
103  uint32_t GetTotalReceivedBytes (void) const;
104 
110  uint32_t GetTotalReceivedPackets (void) const;
111 
117  uint32_t GetTotalDroppedBytes (void) const;
118 
124  uint32_t GetTotalDroppedBytesBeforeEnqueue (void) const;
125 
131  uint32_t GetTotalDroppedBytesAfterDequeue (void) const;
132 
138  uint32_t GetTotalDroppedPackets (void) const;
139 
145  uint32_t GetTotalDroppedPacketsBeforeEnqueue (void) const;
146 
152  uint32_t GetTotalDroppedPacketsAfterDequeue (void) const;
153 
158  void ResetStatistics (void);
159 
165  {
168  };
169 
175  void SetMode (QueueBase::QueueMode mode);
176 
182  QueueBase::QueueMode GetMode (void) const;
183 
189  void SetMaxPackets (uint32_t maxPackets);
190 
194  uint32_t GetMaxPackets (void) const;
195 
201  void SetMaxBytes (uint32_t maxBytes);
202 
206  uint32_t GetMaxBytes (void) const;
207 
208 #if 0
209  // average calculation requires keeping around
210  // a buffer with the date of arrival of past received packets
211  // which are within the average window
212  // so, it is quite costly to do it all the time.
213  // Hence, it is disabled by default and must be explicitly
214  // enabled with this method which specifies the size
215  // of the average window in time units.
216  void EnableRunningAverage (Time averageWindow);
217  void DisableRunningAverage (void);
218  // average
219  double GetQueueSizeAverage (void);
220  double GetReceivedBytesPerSecondAverage (void);
221  double GetReceivedPacketsPerSecondAverage (void);
222  double GetDroppedBytesPerSecondAverage (void);
223  double GetDroppedPacketsPerSecondAverage (void);
224  // variance
225  double GetQueueSizeVariance (void);
226  double GetReceivedBytesPerSecondVariance (void);
227  double GetReceivedPacketsPerSecondVariance (void);
228  double GetDroppedBytesPerSecondVariance (void);
229  double GetDroppedPacketsPerSecondVariance (void);
230 #endif
231 
232 private:
243 
244  uint32_t m_maxPackets;
245  uint32_t m_maxBytes;
247 
249  template <typename Item>
250  friend class Queue;
251 };
252 
253 
277 template <typename Item>
278 class Queue : public QueueBase
279 {
280 public:
285  static TypeId GetTypeId (void);
286 
287  Queue ();
288  virtual ~Queue ();
289 
295  virtual bool Enqueue (Ptr<Item> item) = 0;
296 
302  virtual Ptr<Item> Dequeue (void) = 0;
303 
309  virtual Ptr<Item> Remove (void) = 0;
310 
316  virtual Ptr<const Item> Peek (void) const = 0;
317 
321  void Flush (void);
322 
323 protected:
324 
326  typedef typename std::list<Ptr<Item> >::const_iterator ConstIterator;
327 
342  ConstIterator Head (void) const;
343 
358  ConstIterator Tail (void) const;
359 
366  bool DoEnqueue (ConstIterator pos, Ptr<Item> item);
367 
373  Ptr<Item> DoDequeue (ConstIterator pos);
374 
380  Ptr<Item> DoRemove (ConstIterator pos);
381 
387  Ptr<const Item> DoPeek (ConstIterator pos) const;
388 
397  void DropBeforeEnqueue (Ptr<Item> item);
398 
407  void DropAfterDequeue (Ptr<Item> item);
408 
409 private:
410  std::list<Ptr<Item> > m_packets;
412 
423 };
424 
425 
430 template <typename Item>
431 TypeId
433 {
434  std::string name = GetTypeParamName<Queue<Item> > ();
435  static TypeId tid = TypeId (("ns3::Queue<" + name + ">").c_str ())
436  .SetParent<QueueBase> ()
437  .SetGroupName ("Network")
438  .AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
440  "ns3::" + name + "::TracedCallback")
441  .AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
443  "ns3::" + name + "::TracedCallback")
444  .AddTraceSource ("Drop", "Drop a packet (for whatever reason).",
446  "ns3::" + name + "::TracedCallback")
447  .AddTraceSource ("DropBeforeEnqueue", "Drop a packet before enqueue.",
449  "ns3::" + name + "::TracedCallback")
450  .AddTraceSource ("DropAfterDequeue", "Drop a packet after dequeue.",
452  "ns3::" + name + "::TracedCallback")
453  ;
454  return tid;
455 }
456 
457 template <typename Item>
459  : NS_LOG_TEMPLATE_DEFINE ("Queue")
460 {
461 }
462 
463 template <typename Item>
465 {
466 }
467 
468 template <typename Item>
469 bool
471 {
472  NS_LOG_FUNCTION (this << item);
473 
474  if (m_mode == QUEUE_MODE_PACKETS && (m_nPackets.Get () >= m_maxPackets))
475  {
476  NS_LOG_LOGIC ("Queue full (at max packets) -- dropping pkt");
477  DropBeforeEnqueue (item);
478  return false;
479  }
480 
481  if (m_mode == QUEUE_MODE_BYTES && (m_nBytes.Get () + item->GetSize () > m_maxBytes))
482  {
483  NS_LOG_LOGIC ("Queue full (packet would exceed max bytes) -- dropping pkt");
484  DropBeforeEnqueue (item);
485  return false;
486  }
487 
488  m_packets.insert (pos, item);
489 
490  uint32_t size = item->GetSize ();
491  m_nBytes += size;
492  m_nTotalReceivedBytes += size;
493 
494  m_nPackets++;
495  m_nTotalReceivedPackets++;
496 
497  NS_LOG_LOGIC ("m_traceEnqueue (p)");
498  m_traceEnqueue (item);
499 
500  return true;
501 }
502 
503 template <typename Item>
504 Ptr<Item>
506 {
507  NS_LOG_FUNCTION (this);
508 
509  if (m_nPackets.Get () == 0)
510  {
511  NS_LOG_LOGIC ("Queue empty");
512  return 0;
513  }
514 
515  Ptr<Item> item = *pos;
516  m_packets.erase (pos);
517 
518  if (item != 0)
519  {
520  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
521  NS_ASSERT (m_nPackets.Get () > 0);
522 
523  m_nBytes -= item->GetSize ();
524  m_nPackets--;
525 
526  NS_LOG_LOGIC ("m_traceDequeue (p)");
527  m_traceDequeue (item);
528  }
529  return item;
530 }
531 
532 template <typename Item>
533 Ptr<Item>
535 {
536  NS_LOG_FUNCTION (this);
537 
538  if (m_nPackets.Get () == 0)
539  {
540  NS_LOG_LOGIC ("Queue empty");
541  return 0;
542  }
543 
544  Ptr<Item> item = *pos;
545  m_packets.erase (pos);
546 
547  if (item != 0)
548  {
549  NS_ASSERT (m_nBytes.Get () >= item->GetSize ());
550  NS_ASSERT (m_nPackets.Get () > 0);
551 
552  m_nBytes -= item->GetSize ();
553  m_nPackets--;
554 
555  // packets are first dequeued and then dropped
556  NS_LOG_LOGIC ("m_traceDequeue (p)");
557  m_traceDequeue (item);
558 
559  DropAfterDequeue (item);
560  }
561  return item;
562 }
563 
564 template <typename Item>
565 void
567 {
568  NS_LOG_FUNCTION (this);
569  while (!IsEmpty ())
570  {
571  Remove ();
572  }
573 }
574 
575 template <typename Item>
578 {
579  NS_LOG_FUNCTION (this);
580 
581  if (m_nPackets.Get () == 0)
582  {
583  NS_LOG_LOGIC ("Queue empty");
584  return 0;
585  }
586 
587  return *pos;
588 }
589 
590 template <typename Item>
592 {
593  return m_packets.cbegin ();
594 }
595 
596 template <typename Item>
598 {
599  return m_packets.cend ();
600 }
601 
602 template <typename Item>
603 void
605 {
606  NS_LOG_FUNCTION (this << item);
607 
608  m_nTotalDroppedPackets++;
609  m_nTotalDroppedPacketsBeforeEnqueue++;
610  m_nTotalDroppedBytes += item->GetSize ();
611  m_nTotalDroppedBytesBeforeEnqueue += item->GetSize ();
612 
613  NS_LOG_LOGIC ("m_traceDropBeforeEnqueue (p)");
614  m_traceDrop (item);
615  m_traceDropBeforeEnqueue (item);
616 }
617 
618 template <typename Item>
619 void
621 {
622  NS_LOG_FUNCTION (this << item);
623 
624  m_nTotalDroppedPackets++;
625  m_nTotalDroppedPacketsAfterDequeue++;
626  m_nTotalDroppedBytes += item->GetSize ();
627  m_nTotalDroppedBytesAfterDequeue += item->GetSize ();
628 
629  NS_LOG_LOGIC ("m_traceDropAfterDequeue (p)");
630  m_traceDrop (item);
631  m_traceDropAfterDequeue (item);
632 }
633 
634 } // namespace ns3
635 
636 #endif /* QUEUE_H */
uint32_t m_nTotalDroppedPacketsBeforeEnqueue
Total dropped packets before enqueue.
Definition: queue.h:241
uint32_t m_nTotalReceivedPackets
Total received packets.
Definition: queue.h:236
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
uint32_t m_nTotalDroppedPackets
Total dropped packets.
Definition: queue.h:240
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
ConstIterator Head(void) const
Get a const iterator which refers to the first item in the queue.
Definition: queue.h:591
TracedCallback< Ptr< const Item > > m_traceDequeue
Traced callback: fired when a packet is dequeued.
Definition: queue.h:416
uint32_t m_maxBytes
max bytes in the queue
Definition: queue.h:245
NS_LOG_TEMPLATE_DECLARE
the log component
Definition: queue.h:411
Forward calls to a chain of Callback.
void Flush(void)
Flush the queue.
Definition: queue.h:566
uint32_t m_nTotalDroppedBytes
Total dropped bytes.
Definition: queue.h:237
#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
uint32_t m_nTotalDroppedPacketsAfterDequeue
Total dropped packets after dequeue.
Definition: queue.h:242
uint32_t m_nTotalDroppedBytesAfterDequeue
Total dropped bytes after dequeue.
Definition: queue.h:239
Ptr< Item > DoRemove(ConstIterator pos)
Pull the item to drop from the queue.
Definition: queue.h:534
uint32_t GetTotalDroppedPacketsBeforeEnqueue(void) const
Definition: queue.cc:172
TracedCallback< Ptr< const Item > > m_traceDrop
Traced callback: fired when a packet is dropped.
Definition: queue.h:418
QueueBase::QueueMode GetMode(void) const
Get the operating mode of this device.
Definition: queue.cc:221
QueueMode m_mode
queue mode (packets or bytes)
Definition: queue.h:246
TracedValue< uint32_t > m_nPackets
Number of packets in the queue.
Definition: queue.h:235
static void AppendItemTypeIfNotPresent(std::string &typeId, const std::string &itemType)
Append the item type to the provided type ID if the latter does not end with '>'. ...
Definition: queue.cc:91
Use number of packets for maximum queue size.
Definition: queue.h:166
virtual ~QueueBase()
Definition: queue.cc:85
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
Template class for packet Queues.
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.h:432
TracedCallback< Ptr< const Item > > m_traceDropAfterDequeue
Traced callback: fired when a packet is dropped after dequeue.
Definition: queue.h:422
uint32_t GetNBytes(void) const
Definition: queue.cc:116
uint32_t m_maxPackets
max packets in the queue
Definition: queue.h:244
void SetMode(QueueBase::QueueMode mode)
Set the operating mode of this device.
Definition: queue.cc:202
uint32_t m_nTotalDroppedBytesBeforeEnqueue
Total dropped bytes before enqueue.
Definition: queue.h:238
#define NS_LOG_TEMPLATE_DEFINE(name)
Initialize a reference to a Log component.
Definition: log.h:235
TracedCallback< Ptr< const Item > > m_traceEnqueue
Traced callback: fired when a packet is enqueued.
Definition: queue.h:414
void SetMaxPackets(uint32_t maxPackets)
Set the maximum amount of packets that can be stored in this queue.
Definition: queue.cc:228
void DropAfterDequeue(Ptr< Item > item)
Drop a packet after dequeue.
Definition: queue.h:620
uint32_t GetMaxPackets(void) const
Definition: queue.cc:242
ConstIterator Tail(void) const
Get a const iterator which indicates past-the-last item in the queue.
Definition: queue.h:597
uint32_t GetTotalDroppedBytes(void) const
Definition: queue.cc:140
uint32_t m_nTotalReceivedBytes
Total received bytes.
Definition: queue.h:234
void ResetStatistics(void)
Resets the counts for dropped packets, dropped bytes, received packets, and received bytes...
Definition: queue.cc:188
virtual bool Enqueue(Ptr< Item > item)=0
Place an item into the Queue (each subclass defines the position)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual Ptr< Item > Remove(void)=0
Remove an item from the Queue (each subclass defines the position), counting it as dropped...
uint32_t GetTotalDroppedBytesAfterDequeue(void) const
Definition: queue.cc:156
Queue()
Definition: queue.h:458
void SetMaxBytes(uint32_t maxBytes)
Set the maximum amount of bytes that can be stored in this queue.
Definition: queue.cc:249
Use number of bytes for maximum queue size.
Definition: queue.h:167
TracedCallback< Ptr< const Item > > m_traceDropBeforeEnqueue
Traced callback: fired when a packet is dropped before enqueue.
Definition: queue.h:420
uint32_t GetTotalReceivedPackets(void) const
Definition: queue.cc:132
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
uint32_t GetMaxBytes(void) const
Definition: queue.cc:263
Abstract base class for packet Queues.
Definition: queue.h:50
virtual Ptr< Item > Dequeue(void)=0
Remove an item from the Queue (each subclass defines the position), counting it as dequeued...
Ptr< const Item > DoPeek(ConstIterator pos) const
Peek the front item in the queue.
Definition: queue.h:577
void DropBeforeEnqueue(Ptr< Item > item)
Drop a packet before enqueue.
Definition: queue.h:604
bool IsEmpty(void) const
Definition: queue.cc:100
uint32_t GetNPackets(void) const
Definition: queue.cc:108
uint32_t GetTotalDroppedPacketsAfterDequeue(void) const
Definition: queue.cc:180
uint32_t GetTotalDroppedPackets(void) const
Definition: queue.cc:164
virtual Ptr< const Item > Peek(void) const =0
Get a copy of an item in the queue (each subclass defines the position) without removing it...
A base class which provides memory management and object aggregation.
Definition: object.h:87
QueueMode
Enumeration of the modes supported in the class.
Definition: queue.h:164
Ptr< Item > DoDequeue(ConstIterator pos)
Pull the item to dequeue from the queue.
Definition: queue.h:505
a unique identifier for an interface.
Definition: type-id.h:58
uint32_t GetTotalReceivedBytes(void) const
Definition: queue.cc:124
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
bool DoEnqueue(ConstIterator pos, Ptr< Item > item)
Push an item in the queue.
Definition: queue.h:470
std::list< Ptr< Item > >::const_iterator ConstIterator
Const iterator.
Definition: queue.h:326
std::list< Ptr< Item > > m_packets
the items in the queue
Definition: queue.h:410
static TypeId GetTypeId(void)
Get the type ID.
Definition: queue.cc:33
uint32_t GetTotalDroppedBytesBeforeEnqueue(void) const
Definition: queue.cc:148
virtual ~Queue()
Definition: queue.h:464
TracedValue< uint32_t > m_nBytes
Number of bytes in the queue.
Definition: queue.h:233