A Discrete-Event Network Simulator
API
prio-queue-disc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Universita' degli Studi di Napoli Federico II
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Authors: Stefano Avallone <stavallo@unina.it>
18  */
19 
20 #include "prio-queue-disc.h"
21 
22 #include "ns3/log.h"
23 #include "ns3/object-factory.h"
24 #include "ns3/pointer.h"
25 #include "ns3/socket.h"
26 
27 #include <algorithm>
28 #include <iterator>
29 
30 namespace ns3
31 {
32 
33 NS_LOG_COMPONENT_DEFINE("PrioQueueDisc");
34 
35 NS_OBJECT_ENSURE_REGISTERED(PrioQueueDisc);
36 
38 
39 std::ostream&
40 operator<<(std::ostream& os, const Priomap& priomap)
41 {
42  std::copy(priomap.begin(), priomap.end() - 1, std::ostream_iterator<uint16_t>(os, " "));
43  os << priomap.back();
44  return os;
45 }
46 
47 std::istream&
48 operator>>(std::istream& is, Priomap& priomap)
49 {
50  for (int i = 0; i < 16; i++)
51  {
52  if (!(is >> priomap[i]))
53  {
54  NS_FATAL_ERROR("Incomplete priomap specification ("
55  << i << " values provided, 16 required)");
56  }
57  }
58  return is;
59 }
60 
61 TypeId
63 {
64  static TypeId tid =
65  TypeId("ns3::PrioQueueDisc")
67  .SetGroupName("TrafficControl")
68  .AddConstructor<PrioQueueDisc>()
69  .AddAttribute("Priomap",
70  "The priority to band mapping.",
71  PriomapValue(Priomap{{1, 2, 2, 2, 1, 2, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1}}),
72  MakePriomapAccessor(&PrioQueueDisc::m_prio2band),
73  MakePriomapChecker());
74  return tid;
75 }
76 
79 {
80  NS_LOG_FUNCTION(this);
81 }
82 
84 {
85  NS_LOG_FUNCTION(this);
86 }
87 
88 void
89 PrioQueueDisc::SetBandForPriority(uint8_t prio, uint16_t band)
90 {
91  NS_LOG_FUNCTION(this << prio << band);
92 
93  NS_ASSERT_MSG(prio < 16, "Priority must be a value between 0 and 15");
94 
95  m_prio2band[prio] = band;
96 }
97 
98 uint16_t
100 {
101  NS_LOG_FUNCTION(this << prio);
102 
103  NS_ASSERT_MSG(prio < 16, "Priority must be a value between 0 and 15");
104 
105  return m_prio2band[prio];
106 }
107 
108 bool
110 {
111  NS_LOG_FUNCTION(this << item);
112 
113  uint32_t band = m_prio2band[0];
114 
115  int32_t ret = Classify(item);
116 
117  if (ret == PacketFilter::PF_NO_MATCH)
118  {
119  NS_LOG_DEBUG("No filter has been able to classify this packet, using priomap.");
120 
121  SocketPriorityTag priorityTag;
122  if (item->GetPacket()->PeekPacketTag(priorityTag))
123  {
124  band = m_prio2band[priorityTag.GetPriority() & 0x0f];
125  }
126  }
127  else
128  {
129  NS_LOG_DEBUG("Packet filters returned " << ret);
130 
131  if (ret >= 0 && static_cast<uint32_t>(ret) < GetNQueueDiscClasses())
132  {
133  band = ret;
134  }
135  }
136 
137  NS_ASSERT_MSG(band < GetNQueueDiscClasses(), "Selected band out of range");
138  bool retval = GetQueueDiscClass(band)->GetQueueDisc()->Enqueue(item);
139 
140  // If Queue::Enqueue fails, QueueDisc::Drop is called by the child queue disc
141  // because QueueDisc::AddQueueDiscClass sets the drop callback
142 
143  NS_LOG_LOGIC("Number packets band " << band << ": "
144  << GetQueueDiscClass(band)->GetQueueDisc()->GetNPackets());
145 
146  return retval;
147 }
148 
151 {
152  NS_LOG_FUNCTION(this);
153 
154  Ptr<QueueDiscItem> item;
155 
156  for (uint32_t i = 0; i < GetNQueueDiscClasses(); i++)
157  {
158  if ((item = GetQueueDiscClass(i)->GetQueueDisc()->Dequeue()))
159  {
160  NS_LOG_LOGIC("Popped from band " << i << ": " << item);
161  NS_LOG_LOGIC("Number packets band "
162  << i << ": " << GetQueueDiscClass(i)->GetQueueDisc()->GetNPackets());
163  return item;
164  }
165  }
166 
167  NS_LOG_LOGIC("Queue empty");
168  return item;
169 }
170 
173 {
174  NS_LOG_FUNCTION(this);
175 
177 
178  for (uint32_t i = 0; i < GetNQueueDiscClasses(); i++)
179  {
180  if ((item = GetQueueDiscClass(i)->GetQueueDisc()->Peek()))
181  {
182  NS_LOG_LOGIC("Peeked from band " << i << ": " << item);
183  NS_LOG_LOGIC("Number packets band "
184  << i << ": " << GetQueueDiscClass(i)->GetQueueDisc()->GetNPackets());
185  return item;
186  }
187  }
188 
189  NS_LOG_LOGIC("Queue empty");
190  return item;
191 }
192 
193 bool
195 {
196  NS_LOG_FUNCTION(this);
197  if (GetNInternalQueues() > 0)
198  {
199  NS_LOG_ERROR("PrioQueueDisc cannot have internal queues");
200  return false;
201  }
202 
203  if (GetNQueueDiscClasses() == 0)
204  {
205  // create 3 fifo queue discs
206  ObjectFactory factory;
207  factory.SetTypeId("ns3::FifoQueueDisc");
208  for (uint8_t i = 0; i < 2; i++)
209  {
210  Ptr<QueueDisc> qd = factory.Create<QueueDisc>();
211  qd->Initialize();
212  Ptr<QueueDiscClass> c = CreateObject<QueueDiscClass>();
213  c->SetQueueDisc(qd);
215  }
216  }
217 
218  if (GetNQueueDiscClasses() < 2)
219  {
220  NS_LOG_ERROR("PrioQueueDisc needs at least 2 classes");
221  return false;
222  }
223 
224  return true;
225 }
226 
227 void
229 {
230  NS_LOG_FUNCTION(this);
231 }
232 
233 } // namespace ns3
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
void Initialize()
Invoke DoInitialize on all Objects aggregated to this one.
Definition: object.cc:186
static const int PF_NO_MATCH
Standard value used by packet filters to indicate that no match was possible.
Definition: packet-filter.h:49
The Prio qdisc is a simple classful queueing discipline that contains an arbitrary number of classes ...
void SetBandForPriority(uint8_t prio, uint16_t band)
Set the band (class) assigned to packets with specified priority.
~PrioQueueDisc() override
Ptr< const QueueDiscItem > DoPeek() override
Return a copy of the next packet the queue disc will extract.
Ptr< QueueDiscItem > DoDequeue() override
This function actually extracts a packet from the queue disc.
uint16_t GetBandForPriority(uint8_t prio) const
Get the band (class) assigned to packets with specified priority.
bool CheckConfig() override
Check whether the current configuration is correct.
PrioQueueDisc()
PrioQueueDisc constructor.
Priomap m_prio2band
Priority to band mapping.
bool DoEnqueue(Ptr< QueueDiscItem > item) override
This function actually enqueues a packet into the queue disc.
static TypeId GetTypeId()
Get the type ID.
void InitializeParams() override
Initialize parameters (if any) before the first packet is enqueued.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
QueueDisc is an abstract base class providing the interface and implementing the operations common to...
Definition: queue-disc.h:184
void AddQueueDiscClass(Ptr< QueueDiscClass > qdClass)
Add a queue disc class to the tail of the list of classes.
Definition: queue-disc.cc:624
uint32_t GetNPackets() const
Get the number of packets stored by the queue disc.
Definition: queue-disc.cc:432
int32_t Classify(Ptr< QueueDiscItem > item)
Classify a packet by calling the packet filters, one at a time, until either a filter able to classif...
Definition: queue-disc.cc:667
std::size_t GetNQueueDiscClasses() const
Get the number of queue disc classes.
Definition: queue-disc.cc:661
Ptr< QueueDiscClass > GetQueueDiscClass(std::size_t i) const
Get the i-th queue disc class.
Definition: queue-disc.cc:654
std::size_t GetNInternalQueues() const
Get the number of internal queues.
Definition: queue-disc.cc:598
Ptr< QueueDiscItem > Dequeue()
Extract from the queue disc the packet that has been dequeued by calling Peek, if any,...
Definition: queue-disc.cc:886
Ptr< const QueueDiscItem > Peek()
Get a copy of the next packet the queue discipline will extract.
Definition: queue-disc.cc:920
indicates whether the socket has a priority set.
Definition: socket.h:1316
uint8_t GetPriority() const
Get the tag's priority.
Definition: socket.cc:860
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:282
#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:46
QueueDiscSizePolicy
Enumeration of the available policies to handle the queue disc size.
Definition: queue-disc.h:107
@ NO_LIMITS
Used by queue discs with unlimited size.
Definition: queue-disc.h:111
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ATTRIBUTE_HELPER_CPP(ValueClassTest)
std::istream & operator>>(std::istream &is, Angles &a)
Definition: angles.cc:183
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:159
std::array< uint16_t, 16 > Priomap
Priority map.