A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  .AddConstructor<WifiMacQueue> ()
49  .AddAttribute ("MaxPacketNumber", "If a packet arrives when there are already this number of packets, it is dropped.",
50  UintegerValue (400),
51  MakeUintegerAccessor (&WifiMacQueue::m_maxSize),
52  MakeUintegerChecker<uint32_t> ())
53  .AddAttribute ("MaxDelay", "If a packet stays longer than this delay in the queue, it is dropped.",
54  TimeValue (Seconds (10.0)),
55  MakeTimeAccessor (&WifiMacQueue::m_maxDelay),
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 
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  return it->packet;
206  }
207  }
208  }
209  }
210  return 0;
211 }
212 
213 bool
215 {
216  Cleanup ();
217  return m_queue.empty ();
218 }
219 
220 uint32_t
222 {
223  return m_size;
224 }
225 
226 void
228 {
229  m_queue.erase (m_queue.begin (), m_queue.end ());
230  m_size = 0;
231 }
232 
235 {
236  if (type == WifiMacHeader::ADDR1)
237  {
238  return it->hdr.GetAddr1 ();
239  }
240  if (type == WifiMacHeader::ADDR2)
241  {
242  return it->hdr.GetAddr2 ();
243  }
244  if (type == WifiMacHeader::ADDR3)
245  {
246  return it->hdr.GetAddr3 ();
247  }
248  return 0;
249 }
250 
251 bool
253 {
254  PacketQueueI it = m_queue.begin ();
255  for (; it != m_queue.end (); it++)
256  {
257  if (it->packet == packet)
258  {
259  m_queue.erase (it);
260  m_size--;
261  return true;
262  }
263  }
264  return false;
265 }
266 
267 void
269 {
270  Cleanup ();
271  if (m_size == m_maxSize)
272  {
273  return;
274  }
275  Time now = Simulator::Now ();
276  m_queue.push_front (Item (packet, hdr, now));
277  m_size++;
278 }
279 
280 uint32_t
282  Mac48Address addr)
283 {
284  Cleanup ();
285  uint32_t nPackets = 0;
286  if (!m_queue.empty ())
287  {
288  PacketQueueI it;
289  for (it = m_queue.begin (); it != m_queue.end (); it++)
290  {
291  if (GetAddressForPacket (type, it) == addr)
292  {
293  if (it->hdr.IsQosData () && it->hdr.GetQosTid () == tid)
294  {
295  nPackets++;
296  }
297  }
298  }
299  }
300  return nPackets;
301 }
302 
305  const QosBlockedDestinations *blockedPackets)
306 {
307  Cleanup ();
308  Ptr<const Packet> packet = 0;
309  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
310  {
311  if (!it->hdr.IsQosData ()
312  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
313  {
314  *hdr = it->hdr;
315  timestamp = it->tstamp;
316  packet = it->packet;
317  m_queue.erase (it);
318  m_size--;
319  return packet;
320  }
321  }
322  return packet;
323 }
324 
327  const QosBlockedDestinations *blockedPackets)
328 {
329  Cleanup ();
330  for (PacketQueueI it = m_queue.begin (); it != m_queue.end (); it++)
331  {
332  if (!it->hdr.IsQosData ()
333  || !blockedPackets->IsBlocked (it->hdr.GetAddr1 (), it->hdr.GetQosTid ()))
334  {
335  *hdr = it->hdr;
336  timestamp = it->tstamp;
337  return it->packet;
338  }
339  }
340  return 0;
341 }
342 
343 } // 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:79
void SetMaxDelay(Time delay)
Set the maximum delay before the packet is discarded.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register the class in the ns-3 factory.
Definition: object-base.h:38
Ptr< const Packet > packet
Actual packet.
void Flush(void)
Flush the queue.
Ptr< const Packet > PeekByTidAndAddress(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...
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.
AddressType
Address types.
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...
hold objects of type ns3::Time
Definition: nstime.h:1008
bool IsEmpty(void)
Return if the queue is empty.
Hold an unsigned integer type.
Definition: uinteger.h:46
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.
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:41
static Time Now(void)
Return the "current simulation time".
Definition: simulator.cc:180
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.
Ptr< const AttributeChecker > MakeTimeChecker(const Time min, const Time max)
Helper to make a Time checker with bounded range.
Definition: time.cc:441
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:64
A struct that holds information about a packet for putting in a packet queue.
a unique identifier for an interface.
Definition: type-id.h:49
TypeId SetParent(TypeId tid)
Definition: type-id.cc:610
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.