A Discrete-Event Network Simulator
API
wifi-mac-queue.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005, 2009 INRIA
4  * Copyright (c) 2009 MIRKO BANCHI
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation;
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * Mirko Banchi <mk.banchi@gmail.com>
21  */
22 
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "ns3/uinteger.h"
26 #include "ns3/enum.h"
27 #include "wifi-mac-queue.h"
29 
30 namespace ns3 {
31 
32 NS_OBJECT_ENSURE_REGISTERED (WifiMacQueue);
33 
35  const WifiMacHeader &hdr,
36  Time tstamp)
37  : packet (packet),
38  hdr (hdr),
39  tstamp (tstamp)
40 {
41 }
42 
43 TypeId
45 {
46  static TypeId tid = TypeId ("ns3::WifiMacQueue")
47  .SetParent<Object> ()
48  .SetGroupName ("Wifi")
49  .AddConstructor<WifiMacQueue> ()
50  .AddAttribute ("MaxPacketNumber", "If a packet arrives when there are already this number of packets, it is dropped.",
51  UintegerValue (400),
53  MakeUintegerChecker<uint32_t> ())
54  .AddAttribute ("MaxDelay", "If a packet stays longer than this delay in the queue, it is dropped.",
55  TimeValue (MilliSeconds (500.0)),
57  MakeTimeChecker ())
58  .AddAttribute ("DropPolicy", "Upon enqueue with full queue, drop oldest (DropOldest) or newest (DropNewest) packet",
62  WifiMacQueue::DROP_NEWEST, "DropNewest"))
63  ;
64  return tid;
65 }
66 
68  : m_size (0)
69 {
70 }
71 
73 {
74  Flush ();
75 }
76 
77 void
78 WifiMacQueue::SetMaxSize (uint32_t maxSize)
79 {
80  m_maxSize = maxSize;
81 }
82 
83 void
85 {
86  m_maxDelay = delay;
87 }
88 
89 uint32_t
91 {
92  return m_maxSize;
93 }
94 
95 Time
97 {
98  return m_maxDelay;
99 }
100 
101 void
103 {
104  Cleanup ();
105  if (m_size == m_maxSize)
106  {
107  if (m_dropPolicy == DROP_NEWEST)
108  {
109  return;
110  }
111  else if (m_dropPolicy == DROP_OLDEST)
112  {
113  m_queue.pop_front ();
114  m_size--;
115  }
116  }
117  Time now = Simulator::Now ();
118  m_queue.push_back (Item (packet, hdr, now));
119  m_size++;
120 }
121 
122 void
124 {
125  if (m_queue.empty ())
126  {
127  return;
128  }
129 
130  Time now = Simulator::Now ();
131  uint32_t n = 0;
132  for (PacketQueueI i = m_queue.begin (); i != m_queue.end (); )
133  {
134  if (i->tstamp + m_maxDelay > now)
135  {
136  i++;
137  }
138  else
139  {
140  i = m_queue.erase (i);
141  n++;
142  }
143  }
144  m_size -= n;
145 }
146 
149 {
150  Cleanup ();
151  if (!m_queue.empty ())
152  {
153  Item i = m_queue.front ();
154  m_queue.pop_front ();
155  m_size--;
156  *hdr = i.hdr;
157  return i.packet;
158  }
159  return 0;
160 }
161 
164 {
165  Cleanup ();
166  if (!m_queue.empty ())
167  {
168  Item i = m_queue.front ();
169  *hdr = i.hdr;
170  return i.packet;
171  }
172  return 0;
173 }
174 
178 {
179  Cleanup ();
180  Ptr<const Packet> packet = 0;
181  if (!m_queue.empty ())
182  {
183  PacketQueueI it;
184  for (it = m_queue.begin (); it != m_queue.end (); ++it)
185  {
186  if (it->hdr.IsQosData ())
187  {
188  if (GetAddressForPacket (type, it) == dest
189  && it->hdr.GetQosTid () == tid)
190  {
191  packet = it->packet;
192  *hdr = it->hdr;
193  m_queue.erase (it);
194  m_size--;
195  break;
196  }
197  }
198  }
199  }
200  return packet;
201 }
202 
205  WifiMacHeader::AddressType type, Mac48Address dest, Time *timestamp)
206 {
207  Cleanup ();
208  if (!m_queue.empty ())
209  {
210  PacketQueueI it;
211  for (it = m_queue.begin (); it != m_queue.end (); ++it)
212  {
213  if (it->hdr.IsQosData ())
214  {
215  if (GetAddressForPacket (type, it) == dest
216  && it->hdr.GetQosTid () == tid)
217  {
218  *hdr = it->hdr;
219  *timestamp = it->tstamp;
220  return it->packet;
221  }
222  }
223  }
224  }
225  return 0;
226 }
227 
228 bool
230 {
231  Cleanup ();
232  return m_queue.empty ();
233 }
234 
235 uint32_t
237 {
238  Cleanup ();
239  return m_size;
240 }
241 
242 void
244 {
245  m_queue.erase (m_queue.begin (), m_queue.end ());
246  m_size = 0;
247 }
248 
251 {
252  if (type == WifiMacHeader::ADDR1)
253  {
254  return it->hdr.GetAddr1 ();
255  }
256  if (type == WifiMacHeader::ADDR2)
257  {
258  return it->hdr.GetAddr2 ();
259  }
260  if (type == WifiMacHeader::ADDR3)
261  {
262  return it->hdr.GetAddr3 ();
263  }
264  return 0;
265 }
266 
267 bool
269 {
270  PacketQueueI it = m_queue.begin ();
271  for (; it != m_queue.end (); it++)
272  {
273  if (it->packet == packet)
274  {
275  m_queue.erase (it);
276  m_size--;
277  return true;
278  }
279  }
280  return false;
281 }
282 
283 void
285 {
286  Cleanup ();
287  if (m_size == m_maxSize)
288  {
289  return;
290  }
291  Time now = Simulator::Now ();
292  m_queue.push_front (Item (packet, hdr, now));
293  m_size++;
294 }
295 
296 uint32_t
298  Mac48Address addr)
299 {
300  Cleanup ();
301  uint32_t nPackets = 0;
302  if (!m_queue.empty ())
303  {
304  PacketQueueI it;
305  for (it = m_queue.begin (); it != m_queue.end (); it++)
306  {
307  if (GetAddressForPacket (type, it) == addr)
308  {
309  if (it->hdr.IsQosData () && it->hdr.GetQosTid () == tid)
310  {
311  nPackets++;
312  }
313  }
314  }
315  }
316  return nPackets;
317 }
318 
321  const QosBlockedDestinations *blockedPackets)
322 {
323  Cleanup ();
324  Ptr<const Packet> packet = 0;
325  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
326  {
327  if (!it->hdr.IsQosData ()
328  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
329  {
330  *hdr = it->hdr;
331  timestamp = it->tstamp;
332  packet = it->packet;
333  m_queue.erase (it);
334  m_size--;
335  return packet;
336  }
337  }
338  return packet;
339 }
340 
343  const QosBlockedDestinations *blockedPackets)
344 {
345  Cleanup ();
346  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
347  {
348  if (!it->hdr.IsQosData ()
349  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
350  {
351  *hdr = it->hdr;
352  timestamp = it->tstamp;
353  return it->packet;
354  }
355  }
356  return 0;
357 }
358 
359 } //namespace ns3
Time m_maxDelay
Time to live for packets in the queue.
Keep track of destination address - TID pairs that are waiting for a block ACK response.
uint32_t GetSize(void)
Return the current queue size.
WifiMacHeader hdr
Wifi MAC header associated with the packet.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void SetMaxDelay(Time delay)
Set the maximum delay before the packet is discarded.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
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
Ptr< const Packet > packet
Actual packet.
void Flush(void)
Flush the queue.
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
virtual void Cleanup(void)
Clean up the queue by removing packets that exceeded the maximum delay.
uint32_t GetNPacketsByTidAndAddress(uint8_t tid, WifiMacHeader::AddressType type, Mac48Address addr)
Returns number of QoS packets having tid equals to tid and address specified by type equals to addr...
bool Remove(Ptr< const Packet > packet)
If exists, removes packet from queue and returns true.
void SetMaxSize(uint32_t maxSize)
Set the maximum queue size.
Ptr< const Packet > DequeueFirstAvailable(WifiMacHeader *hdr, Time &tStamp, const QosBlockedDestinations *blockedPackets)
Returns first available packet for transmission.
Ptr< const Packet > DequeueByTidAndAddress(WifiMacHeader *hdr, uint8_t tid, WifiMacHeader::AddressType type, Mac48Address addr)
Searchs and returns, if is present in this queue, first packet having address indicated by type equal...
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:446
Hold variables of type enum.
Definition: enum.h:54
AddressType
Address types.
AttributeValue implementation for Time.
Definition: nstime.h:957
bool IsEmpty(void)
Return if the queue is empty.
Hold an unsigned integer type.
Definition: uinteger.h:44
static TypeId GetTypeId(void)
uint32_t GetMaxSize(void) const
Return the maximum queue size.
Ptr< const Packet > Dequeue(WifiMacHeader *hdr)
Dequeue the packet in the front of the queue.
uint32_t m_size
Current queue size.
Ptr< const Packet > Peek(WifiMacHeader *hdr)
Peek the packet in the front of the queue.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const Packet > PeekByTidAndAddress(WifiMacHeader *hdr, uint8_t tid, WifiMacHeader::AddressType type, Mac48Address addr, Time *timestamp)
Searchs and returns, if is present in this queue, first packet having address indicated by type equal...
void PushFront(Ptr< const Packet > packet, const WifiMacHeader &hdr)
Enqueue the given packet and its corresponding WifiMacHeader at the front of the queue.
an EUI-48 address
Definition: mac48-address.h:43
Ptr< const AttributeAccessor > MakeTimeAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method...
Definition: nstime.h:958
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
This queue implements the timeout procedure described in (Section 9.19.2.6 "Retransmit procedures" pa...
Mac48Address GetAddressForPacket(enum WifiMacHeader::AddressType type, PacketQueueI it)
Return the appropriate address for the given packet (given by PacketQueue iterator).
enum DropPolicy m_dropPolicy
Drop behavior of queue.
Ptr< const Packet > PeekFirstAvailable(WifiMacHeader *hdr, Time &tStamp, const QosBlockedDestinations *blockedPackets)
Returns first available packet for transmission.
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
void Enqueue(Ptr< const Packet > packet, const WifiMacHeader &hdr)
Enqueue the given packet and its corresponding WifiMacHeader at the end of the queue.
Time GetMaxDelay(void) const
Return the maximum delay before the packet is discarded.
A base class which provides memory management and object aggregation.
Definition: object.h:87
A struct that holds information about a packet for putting in a packet queue.
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
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904
std::list< struct Item >::iterator PacketQueueI
typedef for packet (struct Item) queue iterator.
Implements the IEEE 802.11 MAC header.
PacketQueue m_queue
Packet (struct Item) queue.
uint32_t m_maxSize
Queue capacity.
Item(Ptr< const Packet > packet, const WifiMacHeader &hdr, Time tstamp)
Create a struct with the given parameters.
bool IsBlocked(Mac48Address dest, uint8_t tid) const
Check if the given destination address and TID are blocked from sending (e.g.