2007-02-15 16:52:39 -08:00
|
|
|
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2007 University of Washington
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
|
* published by the Free Software Foundation;
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
*/
|
|
|
|
|
|
2007-09-13 21:36:32 -07:00
|
|
|
#include "ns3/log.h"
|
2016-03-08 10:45:36 -08:00
|
|
|
#include "ns3/abort.h"
|
|
|
|
|
#include "ns3/enum.h"
|
|
|
|
|
#include "ns3/uinteger.h"
|
2008-03-02 22:08:17 +01:00
|
|
|
#include "ns3/trace-source-accessor.h"
|
2007-05-13 09:35:03 +02:00
|
|
|
#include "queue.h"
|
2007-02-15 16:52:39 -08:00
|
|
|
|
2007-02-16 09:37:35 +01:00
|
|
|
namespace ns3 {
|
2007-02-15 16:52:39 -08:00
|
|
|
|
2014-09-26 15:51:00 -07:00
|
|
|
NS_LOG_COMPONENT_DEFINE ("Queue");
|
|
|
|
|
|
2014-03-05 17:06:59 -08:00
|
|
|
NS_OBJECT_ENSURE_REGISTERED (Queue);
|
2007-05-11 19:15:28 +02:00
|
|
|
|
2008-01-15 12:36:22 +01:00
|
|
|
TypeId
|
2008-01-15 12:43:07 +01:00
|
|
|
Queue::GetTypeId (void)
|
2008-01-02 10:33:39 +01:00
|
|
|
{
|
2008-03-13 12:56:49 -07:00
|
|
|
static TypeId tid = TypeId ("ns3::Queue")
|
2008-03-02 21:57:51 +01:00
|
|
|
.SetParent<Object> ()
|
2016-03-08 10:45:36 -08:00
|
|
|
.SetGroupName ("Network")
|
|
|
|
|
.AddAttribute ("Mode",
|
|
|
|
|
"Whether to use bytes (see MaxBytes) or packets (see MaxPackets) as the maximum queue size metric.",
|
|
|
|
|
EnumValue (QUEUE_MODE_PACKETS),
|
|
|
|
|
MakeEnumAccessor (&Queue::SetMode,
|
|
|
|
|
&Queue::GetMode),
|
|
|
|
|
MakeEnumChecker (QUEUE_MODE_BYTES, "QUEUE_MODE_BYTES",
|
|
|
|
|
QUEUE_MODE_PACKETS, "QUEUE_MODE_PACKETS"))
|
|
|
|
|
.AddAttribute ("MaxPackets",
|
|
|
|
|
"The maximum number of packets accepted by this queue.",
|
|
|
|
|
UintegerValue (100),
|
|
|
|
|
MakeUintegerAccessor (&Queue::SetMaxPackets,
|
|
|
|
|
&Queue::GetMaxPackets),
|
|
|
|
|
MakeUintegerChecker<uint32_t> ())
|
|
|
|
|
.AddAttribute ("MaxBytes",
|
|
|
|
|
"The maximum number of bytes accepted by this queue.",
|
|
|
|
|
UintegerValue (100 * 65535),
|
|
|
|
|
MakeUintegerAccessor (&Queue::SetMaxBytes,
|
|
|
|
|
&Queue::GetMaxBytes),
|
|
|
|
|
MakeUintegerChecker<uint32_t> ())
|
2008-04-14 16:06:44 -07:00
|
|
|
.AddTraceSource ("Enqueue", "Enqueue a packet in the queue.",
|
2014-10-03 02:24:42 -07:00
|
|
|
MakeTraceSourceAccessor (&Queue::m_traceEnqueue),
|
|
|
|
|
"ns3::Packet::TracedCallback")
|
2008-04-14 16:06:44 -07:00
|
|
|
.AddTraceSource ("Dequeue", "Dequeue a packet from the queue.",
|
2014-10-03 02:24:42 -07:00
|
|
|
MakeTraceSourceAccessor (&Queue::m_traceDequeue),
|
|
|
|
|
"ns3::Packet::TracedCallback")
|
2016-03-08 10:45:36 -08:00
|
|
|
.AddTraceSource ("Drop", "Drop a packet (for whatever reason).",
|
2014-10-03 02:24:42 -07:00
|
|
|
MakeTraceSourceAccessor (&Queue::m_traceDrop),
|
|
|
|
|
"ns3::Packet::TracedCallback")
|
2016-03-08 10:45:36 -08:00
|
|
|
.AddTraceSource ("PacketsInQueue",
|
|
|
|
|
"Number of packets currently stored in the queue",
|
|
|
|
|
MakeTraceSourceAccessor (&Queue::m_nPackets),
|
|
|
|
|
"ns3::TracedValueCallback::Uint32")
|
|
|
|
|
.AddTraceSource ("BytesInQueue",
|
|
|
|
|
"Number of bytes currently stored in the queue",
|
|
|
|
|
MakeTraceSourceAccessor (&Queue::m_nBytes),
|
|
|
|
|
"ns3::TracedValueCallback::Uint32")
|
2011-05-13 14:57:43 -04:00
|
|
|
;
|
2008-01-15 12:44:09 +01:00
|
|
|
return tid;
|
2008-01-02 10:33:39 +01:00
|
|
|
}
|
|
|
|
|
|
2007-03-18 14:06:51 -07:00
|
|
|
Queue::Queue() :
|
2011-05-22 23:18:47 -07:00
|
|
|
m_nBytes (0),
|
|
|
|
|
m_nTotalReceivedBytes (0),
|
|
|
|
|
m_nPackets (0),
|
|
|
|
|
m_nTotalReceivedPackets (0),
|
|
|
|
|
m_nTotalDroppedBytes (0),
|
2016-03-08 10:45:36 -08:00
|
|
|
m_nTotalDroppedPackets (0),
|
|
|
|
|
m_mode (QUEUE_MODE_PACKETS)
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Queue::~Queue()
|
|
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
|
|
|
|
|
2007-03-18 14:06:51 -07:00
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
bool
|
2016-03-08 10:44:03 -08:00
|
|
|
Queue::Enqueue (Ptr<QueueItem> item)
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2016-03-08 10:44:03 -08:00
|
|
|
NS_LOG_FUNCTION (this << item);
|
2016-03-08 10:45:36 -08:00
|
|
|
|
|
|
|
|
if (m_mode == QUEUE_MODE_PACKETS && (m_nPackets.Get () >= m_maxPackets))
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_LOGIC ("Queue full (at max packets) -- dropping pkt");
|
2016-05-11 12:10:02 +02:00
|
|
|
Drop (item);
|
2016-03-08 10:45:36 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_mode == QUEUE_MODE_BYTES && (m_nBytes.Get () + item->GetPacketSize () > m_maxBytes))
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_LOGIC ("Queue full (packet would exceed max bytes) -- dropping pkt");
|
2016-05-11 12:10:02 +02:00
|
|
|
Drop (item);
|
2016-03-08 10:45:36 -08:00
|
|
|
return false;
|
|
|
|
|
}
|
2007-02-15 16:52:39 -08:00
|
|
|
|
2010-05-17 07:29:43 -07:00
|
|
|
//
|
|
|
|
|
// If DoEnqueue fails, Queue::Drop is called by the subclass
|
|
|
|
|
//
|
2016-03-08 10:44:03 -08:00
|
|
|
bool retval = DoEnqueue (item);
|
2007-02-15 16:52:39 -08:00
|
|
|
if (retval)
|
|
|
|
|
{
|
2010-05-17 07:29:43 -07:00
|
|
|
NS_LOG_LOGIC ("m_traceEnqueue (p)");
|
2016-03-08 10:44:03 -08:00
|
|
|
m_traceEnqueue (item->GetPacket ());
|
2010-05-17 07:29:43 -07:00
|
|
|
|
2016-03-08 10:44:03 -08:00
|
|
|
uint32_t size = item->GetPacketSize ();
|
2010-01-05 12:27:50 -08:00
|
|
|
m_nBytes += size;
|
|
|
|
|
m_nTotalReceivedBytes += size;
|
2011-05-13 14:57:43 -04:00
|
|
|
|
2007-02-15 16:52:39 -08:00
|
|
|
m_nPackets++;
|
2010-01-05 12:27:50 -08:00
|
|
|
m_nTotalReceivedPackets++;
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
|
|
|
|
return retval;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 10:44:03 -08:00
|
|
|
Ptr<QueueItem>
|
2007-10-01 14:15:56 +02:00
|
|
|
Queue::Dequeue (void)
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-02-15 16:52:39 -08:00
|
|
|
|
2016-03-08 10:45:36 -08:00
|
|
|
if (m_nPackets.Get () == 0)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_LOGIC ("Queue empty");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 10:44:03 -08:00
|
|
|
Ptr<QueueItem> item = DoDequeue ();
|
2007-02-15 16:52:39 -08:00
|
|
|
|
2016-03-08 10:44:03 -08:00
|
|
|
if (item != 0)
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2016-03-08 10:45:36 -08:00
|
|
|
NS_ASSERT (m_nBytes.Get () >= item->GetPacketSize ());
|
|
|
|
|
NS_ASSERT (m_nPackets.Get () > 0);
|
2009-02-24 08:29:36 +01:00
|
|
|
|
2016-03-08 10:44:03 -08:00
|
|
|
m_nBytes -= item->GetPacketSize ();
|
2007-02-15 16:52:39 -08:00
|
|
|
m_nPackets--;
|
|
|
|
|
|
2011-05-22 23:18:47 -07:00
|
|
|
NS_LOG_LOGIC ("m_traceDequeue (packet)");
|
2016-03-08 10:45:36 -08:00
|
|
|
m_traceDequeue (item->GetPacket ());
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
2016-03-08 10:44:03 -08:00
|
|
|
return item;
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
void
|
2007-03-07 22:26:20 -08:00
|
|
|
Queue::DequeueAll (void)
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2008-06-04 09:22:37 -07:00
|
|
|
NS_LOG_FUNCTION (this);
|
|
|
|
|
while (!IsEmpty ())
|
|
|
|
|
{
|
|
|
|
|
Dequeue ();
|
|
|
|
|
}
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
|
|
|
|
|
2016-03-08 10:44:03 -08:00
|
|
|
Ptr<const QueueItem>
|
2007-11-22 10:30:29 +01:00
|
|
|
Queue::Peek (void) const
|
2007-03-27 13:04:11 -07:00
|
|
|
{
|
2008-04-15 15:10:53 -07:00
|
|
|
NS_LOG_FUNCTION (this);
|
2016-03-08 10:45:36 -08:00
|
|
|
|
|
|
|
|
if (m_nPackets.Get () == 0)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_LOGIC ("Queue empty");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-01 14:15:56 +02:00
|
|
|
return DoPeek ();
|
2007-03-27 13:04:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
uint32_t
|
2007-11-15 11:41:42 +00:00
|
|
|
Queue::GetNPackets (void) const
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-09-13 21:36:32 -07:00
|
|
|
NS_LOG_LOGIC ("returns " << m_nPackets);
|
2007-02-15 16:52:39 -08:00
|
|
|
return m_nPackets;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
uint32_t
|
2007-11-15 11:41:42 +00:00
|
|
|
Queue::GetNBytes (void) const
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-09-13 21:36:32 -07:00
|
|
|
NS_LOG_LOGIC (" returns " << m_nBytes);
|
2007-02-15 16:52:39 -08:00
|
|
|
return m_nBytes;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
bool
|
2007-11-15 11:41:42 +00:00
|
|
|
Queue::IsEmpty (void) const
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2016-03-08 10:45:36 -08:00
|
|
|
NS_LOG_LOGIC ("returns " << (m_nPackets.Get () == 0));
|
|
|
|
|
return m_nPackets.Get () == 0;
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
uint32_t
|
2007-11-15 11:41:42 +00:00
|
|
|
Queue::GetTotalReceivedBytes (void) const
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2011-05-22 23:18:47 -07:00
|
|
|
NS_LOG_LOGIC ("returns " << m_nTotalReceivedBytes);
|
2007-02-15 16:52:39 -08:00
|
|
|
return m_nTotalReceivedBytes;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
uint32_t
|
2007-11-15 11:41:42 +00:00
|
|
|
Queue::GetTotalReceivedPackets (void) const
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-09-13 21:36:32 -07:00
|
|
|
NS_LOG_LOGIC ("returns " << m_nTotalReceivedPackets);
|
2007-02-15 16:52:39 -08:00
|
|
|
return m_nTotalReceivedPackets;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
uint32_t
|
2007-11-15 11:41:42 +00:00
|
|
|
Queue:: GetTotalDroppedBytes (void) const
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-09-13 21:36:32 -07:00
|
|
|
NS_LOG_LOGIC ("returns " << m_nTotalDroppedBytes);
|
2007-02-15 16:52:39 -08:00
|
|
|
return m_nTotalDroppedBytes;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
uint32_t
|
2007-11-15 11:41:42 +00:00
|
|
|
Queue::GetTotalDroppedPackets (void) const
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2011-05-22 23:18:47 -07:00
|
|
|
NS_LOG_LOGIC ("returns " << m_nTotalDroppedPackets);
|
2007-02-15 16:52:39 -08:00
|
|
|
return m_nTotalDroppedPackets;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 17:06:19 +01:00
|
|
|
void
|
2007-02-15 16:52:39 -08:00
|
|
|
Queue::ResetStatistics (void)
|
|
|
|
|
{
|
2012-11-16 22:27:18 +01:00
|
|
|
NS_LOG_FUNCTION (this);
|
2007-02-15 16:52:39 -08:00
|
|
|
m_nTotalReceivedBytes = 0;
|
|
|
|
|
m_nTotalReceivedPackets = 0;
|
|
|
|
|
m_nTotalDroppedBytes = 0;
|
|
|
|
|
m_nTotalDroppedPackets = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-08 10:45:36 -08:00
|
|
|
void
|
|
|
|
|
Queue::SetMode (Queue::QueueMode mode)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this << mode);
|
|
|
|
|
|
|
|
|
|
if (mode == QUEUE_MODE_BYTES && m_mode == QUEUE_MODE_PACKETS)
|
|
|
|
|
{
|
|
|
|
|
NS_ABORT_MSG_IF (m_nPackets.Get () != 0,
|
|
|
|
|
"Cannot change queue mode in a queue with packets.");
|
|
|
|
|
}
|
|
|
|
|
else if (mode == QUEUE_MODE_PACKETS && m_mode == QUEUE_MODE_BYTES)
|
|
|
|
|
{
|
|
|
|
|
NS_ABORT_MSG_IF (m_nBytes.Get () != 0,
|
|
|
|
|
"Cannot change queue mode in a queue with packets.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_mode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Queue::QueueMode
|
|
|
|
|
Queue::GetMode (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this);
|
|
|
|
|
return m_mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Queue::SetMaxPackets (uint32_t maxPackets)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this << maxPackets);
|
|
|
|
|
|
|
|
|
|
if (m_mode == QUEUE_MODE_PACKETS)
|
|
|
|
|
{
|
|
|
|
|
NS_ABORT_MSG_IF (maxPackets < m_nPackets.Get (),
|
|
|
|
|
"The new queue size cannot be less than the number of currently stored packets.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_maxPackets = maxPackets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
Queue::GetMaxPackets (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this);
|
|
|
|
|
return m_maxPackets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Queue::SetMaxBytes (uint32_t maxBytes)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this << maxBytes);
|
|
|
|
|
|
|
|
|
|
if (m_mode == QUEUE_MODE_BYTES)
|
|
|
|
|
{
|
|
|
|
|
NS_ABORT_MSG_IF (maxBytes < m_nBytes.Get (),
|
|
|
|
|
"The new queue size cannot be less than the amount of bytes of currently stored packets.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_maxBytes = maxBytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
|
Queue::GetMaxBytes (void) const
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this);
|
|
|
|
|
return m_maxBytes;
|
|
|
|
|
}
|
|
|
|
|
|
2007-02-21 16:30:08 +01:00
|
|
|
void
|
2016-05-11 12:10:02 +02:00
|
|
|
Queue::SetDropCallback (DropCallback cb)
|
2007-02-15 16:52:39 -08:00
|
|
|
{
|
2016-05-11 12:10:02 +02:00
|
|
|
m_dropCallback = cb;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Queue::NotifyDrop (Ptr<QueueItem> item)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this << item);
|
|
|
|
|
|
|
|
|
|
if (!m_dropCallback.IsNull ())
|
|
|
|
|
{
|
|
|
|
|
m_dropCallback (item);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
Queue::Drop (Ptr<QueueItem> item)
|
|
|
|
|
{
|
|
|
|
|
NS_LOG_FUNCTION (this << item);
|
2007-02-15 16:52:39 -08:00
|
|
|
|
|
|
|
|
m_nTotalDroppedPackets++;
|
2016-05-11 12:10:02 +02:00
|
|
|
m_nTotalDroppedBytes += item->GetPacketSize ();
|
2007-02-15 16:52:39 -08:00
|
|
|
|
2007-09-13 21:36:32 -07:00
|
|
|
NS_LOG_LOGIC ("m_traceDrop (p)");
|
2016-05-11 12:10:02 +02:00
|
|
|
m_traceDrop (item->GetPacket ());
|
|
|
|
|
NotifyDrop (item);
|
2007-02-15 16:52:39 -08:00
|
|
|
}
|
|
|
|
|
|
2008-03-02 21:57:51 +01:00
|
|
|
} // namespace ns3
|