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 "wifi-mac-queue.h"
28 
29 namespace ns3 {
30 
31 NS_OBJECT_ENSURE_REGISTERED (WifiMacQueue);
32 
34  const WifiMacHeader &hdr,
35  Time tstamp)
36  : packet (packet),
37  hdr (hdr),
38  tstamp (tstamp)
39 {
40 }
41 
42 TypeId
44 {
45  static TypeId tid = TypeId ("ns3::WifiMacQueue")
46  .SetParent<Object> ()
47  .SetGroupName ("Wifi")
48  .AddConstructor<WifiMacQueue> ()
49  .AddAttribute ("MaxPacketNumber", "If a packet arrives when there are already this number of packets, it is dropped.",
50  UintegerValue (400),
52  MakeUintegerChecker<uint32_t> ())
53  .AddAttribute ("MaxDelay", "If a packet stays longer than this delay in the queue, it is dropped.",
54  TimeValue (MilliSeconds (500.0)),
56  MakeTimeChecker ())
57  ;
58  return tid;
59 }
60 
62  : m_size (0)
63 {
64 }
65 
67 {
68  Flush ();
69 }
70 
71 void
72 WifiMacQueue::SetMaxSize (uint32_t maxSize)
73 {
74  m_maxSize = maxSize;
75 }
76 
77 void
79 {
80  m_maxDelay = delay;
81 }
82 
83 uint32_t
85 {
86  return m_maxSize;
87 }
88 
89 Time
91 {
92  return m_maxDelay;
93 }
94 
95 void
97 {
98  Cleanup ();
99  if (m_size == m_maxSize)
100  {
101  return;
102  }
103  Time now = Simulator::Now ();
104  m_queue.push_back (Item (packet, hdr, now));
105  m_size++;
106 }
107 
108 void
110 {
111  if (m_queue.empty ())
112  {
113  return;
114  }
115 
116  Time now = Simulator::Now ();
117  uint32_t n = 0;
118  for (PacketQueueI i = m_queue.begin (); i != m_queue.end (); )
119  {
120  if (i->tstamp + m_maxDelay > now)
121  {
122  i++;
123  }
124  else
125  {
126  i = m_queue.erase (i);
127  n++;
128  }
129  }
130  m_size -= n;
131 }
132 
135 {
136  Cleanup ();
137  if (!m_queue.empty ())
138  {
139  Item i = m_queue.front ();
140  m_queue.pop_front ();
141  m_size--;
142  *hdr = i.hdr;
143  return i.packet;
144  }
145  return 0;
146 }
147 
150 {
151  Cleanup ();
152  if (!m_queue.empty ())
153  {
154  Item i = m_queue.front ();
155  *hdr = i.hdr;
156  return i.packet;
157  }
158  return 0;
159 }
160 
164 {
165  Cleanup ();
166  Ptr<const Packet> packet = 0;
167  if (!m_queue.empty ())
168  {
169  PacketQueueI it;
170  for (it = m_queue.begin (); it != m_queue.end (); ++it)
171  {
172  if (it->hdr.IsQosData ())
173  {
174  if (GetAddressForPacket (type, it) == dest
175  && it->hdr.GetQosTid () == tid)
176  {
177  packet = it->packet;
178  *hdr = it->hdr;
179  m_queue.erase (it);
180  m_size--;
181  break;
182  }
183  }
184  }
185  }
186  return packet;
187 }
188 
191  WifiMacHeader::AddressType type, Mac48Address dest, Time *timestamp)
192 {
193  Cleanup ();
194  if (!m_queue.empty ())
195  {
196  PacketQueueI it;
197  for (it = m_queue.begin (); it != m_queue.end (); ++it)
198  {
199  if (it->hdr.IsQosData ())
200  {
201  if (GetAddressForPacket (type, it) == dest
202  && it->hdr.GetQosTid () == tid)
203  {
204  *hdr = it->hdr;
205  *timestamp = it->tstamp;
206  return it->packet;
207  }
208  }
209  }
210  }
211  return 0;
212 }
213 
214 bool
216 {
217  Cleanup ();
218  return m_queue.empty ();
219 }
220 
221 uint32_t
223 {
224  Cleanup ();
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.
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
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: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 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:826
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.