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  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20  * Author: Mirko Banchi <mk.banchi@gmail.com>
21  */
22 
23 #include "ns3/simulator.h"
24 #include "ns3/packet.h"
25 #include "ns3/uinteger.h"
26 
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 (Seconds (10.0)),
57  MakeTimeChecker ())
58  ;
59  return tid;
60 }
61 
63  : m_size (0)
64 {
65 }
66 
68 {
69  Flush ();
70 }
71 
72 void
73 WifiMacQueue::SetMaxSize (uint32_t maxSize)
74 {
75  m_maxSize = maxSize;
76 }
77 
78 void
80 {
81  m_maxDelay = delay;
82 }
83 
84 uint32_t
86 {
87  return m_maxSize;
88 }
89 
90 Time
92 {
93  return m_maxDelay;
94 }
95 
96 void
98 {
99  Cleanup ();
100  if (m_size == m_maxSize)
101  {
102  return;
103  }
104  Time now = Simulator::Now ();
105  m_queue.push_back (Item (packet, hdr, now));
106  m_size++;
107 }
108 
109 void
111 {
112  if (m_queue.empty ())
113  {
114  return;
115  }
116 
117  Time now = Simulator::Now ();
118  uint32_t n = 0;
119  for (PacketQueueI i = m_queue.begin (); i != m_queue.end ();)
120  {
121  if (i->tstamp + m_maxDelay > now)
122  {
123  i++;
124  }
125  else
126  {
127  i = m_queue.erase (i);
128  n++;
129  }
130  }
131  m_size -= n;
132 }
133 
136 {
137  Cleanup ();
138  if (!m_queue.empty ())
139  {
140  Item i = m_queue.front ();
141  m_queue.pop_front ();
142  m_size--;
143  *hdr = i.hdr;
144  return i.packet;
145  }
146  return 0;
147 }
148 
151 {
152  Cleanup ();
153  if (!m_queue.empty ())
154  {
155  Item i = m_queue.front ();
156  *hdr = i.hdr;
157  return i.packet;
158  }
159  return 0;
160 }
161 
165 {
166  Cleanup ();
167  Ptr<const Packet> packet = 0;
168  if (!m_queue.empty ())
169  {
170  PacketQueueI it;
171  for (it = m_queue.begin (); it != m_queue.end (); ++it)
172  {
173  if (it->hdr.IsQosData ())
174  {
175  if (GetAddressForPacket (type, it) == dest
176  && it->hdr.GetQosTid () == tid)
177  {
178  packet = it->packet;
179  *hdr = it->hdr;
180  m_queue.erase (it);
181  m_size--;
182  break;
183  }
184  }
185  }
186  }
187  return packet;
188 }
189 
192  WifiMacHeader::AddressType type, Mac48Address dest, Time *timestamp)
193 {
194  Cleanup ();
195  if (!m_queue.empty ())
196  {
197  PacketQueueI it;
198  for (it = m_queue.begin (); it != m_queue.end (); ++it)
199  {
200  if (it->hdr.IsQosData ())
201  {
202  if (GetAddressForPacket (type, it) == dest
203  && it->hdr.GetQosTid () == tid)
204  {
205  *hdr = it->hdr;
206  *timestamp=it->tstamp;
207  return it->packet;
208  }
209  }
210  }
211  }
212  return 0;
213 }
214 
215 bool
217 {
218  Cleanup ();
219  return m_queue.empty ();
220 }
221 
222 uint32_t
224 {
225  return m_size;
226 }
227 
228 void
230 {
231  m_queue.erase (m_queue.begin (), m_queue.end ());
232  m_size = 0;
233 }
234 
237 {
238  if (type == WifiMacHeader::ADDR1)
239  {
240  return it->hdr.GetAddr1 ();
241  }
242  if (type == WifiMacHeader::ADDR2)
243  {
244  return it->hdr.GetAddr2 ();
245  }
246  if (type == WifiMacHeader::ADDR3)
247  {
248  return it->hdr.GetAddr3 ();
249  }
250  return 0;
251 }
252 
253 bool
255 {
256  PacketQueueI it = m_queue.begin ();
257  for (; it != m_queue.end (); it++)
258  {
259  if (it->packet == packet)
260  {
261  m_queue.erase (it);
262  m_size--;
263  return true;
264  }
265  }
266  return false;
267 }
268 
269 void
271 {
272  Cleanup ();
273  if (m_size == m_maxSize)
274  {
275  return;
276  }
277  Time now = Simulator::Now ();
278  m_queue.push_front (Item (packet, hdr, now));
279  m_size++;
280 }
281 
282 uint32_t
284  Mac48Address addr)
285 {
286  Cleanup ();
287  uint32_t nPackets = 0;
288  if (!m_queue.empty ())
289  {
290  PacketQueueI it;
291  for (it = m_queue.begin (); it != m_queue.end (); it++)
292  {
293  if (GetAddressForPacket (type, it) == addr)
294  {
295  if (it->hdr.IsQosData () && it->hdr.GetQosTid () == tid)
296  {
297  nPackets++;
298  }
299  }
300  }
301  }
302  return nPackets;
303 }
304 
307  const QosBlockedDestinations *blockedPackets)
308 {
309  Cleanup ();
310  Ptr<const Packet> packet = 0;
311  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
312  {
313  if (!it->hdr.IsQosData ()
314  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
315  {
316  *hdr = it->hdr;
317  timestamp = it->tstamp;
318  packet = it->packet;
319  m_queue.erase (it);
320  m_size--;
321  return packet;
322  }
323  }
324  return packet;
325 }
326 
329  const QosBlockedDestinations *blockedPackets)
330 {
331  Cleanup ();
332  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
333  {
334  if (!it->hdr.IsQosData ()
335  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
336  {
337  *hdr = it->hdr;
338  timestamp = it->tstamp;
339  return it->packet;
340  }
341  }
342  return 0;
343 }
344 
345 } // 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 Packet > packet
Actual packet.
void Flush(void)
Flush the queue.
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
AddressType
Address types.
AttributeValue implementation for Time.
Definition: nstime.h:928
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:929
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
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).
Ptr< const Packet > PeekFirstAvailable(WifiMacHeader *hdr, Time &tStamp, const QosBlockedDestinations *blockedPackets)
Returns first available packet for transmission.
void Enqueue(Ptr< const Packet > packet, const WifiMacHeader &hdr)
Enqueue the given packet and its corresponding WifiMacHeader at the end of the queue.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:866
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:57
TypeId SetParent(TypeId tid)
Definition: type-id.cc:638
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.