A Discrete-Event Network Simulator
API
mpdu-aggregator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Ghada Badawy <gbadawy@gmail.com>
19  * Stefano Avallone <stavallo@unina.it>
20  */
21 
22 #include "ns3/log.h"
23 #include "ns3/packet.h"
24 #include "mpdu-aggregator.h"
25 #include "ampdu-subframe-header.h"
26 #include "wifi-phy.h"
27 #include "wifi-tx-vector.h"
29 #include "mac-low.h"
30 #include "wifi-mac-queue-item.h"
31 #include "wifi-mac-queue.h"
32 #include "msdu-aggregator.h"
33 #include "wifi-net-device.h"
34 #include "ht-capabilities.h"
35 #include "vht-capabilities.h"
36 #include "he-capabilities.h"
37 #include "wifi-mac.h"
38 #include "ctrl-headers.h"
39 #include "wifi-mac-trailer.h"
40 
41 NS_LOG_COMPONENT_DEFINE ("MpduAggregator");
42 
43 namespace ns3 {
44 
45 NS_OBJECT_ENSURE_REGISTERED (MpduAggregator);
46 
47 TypeId
49 {
50  static TypeId tid = TypeId ("ns3::MpduAggregator")
51  .SetParent<Object> ()
52  .SetGroupName ("Wifi")
53  .AddConstructor<MpduAggregator> ()
54  ;
55  return tid;
56 }
57 
59 {
60 }
61 
63 {
64 }
65 
66 void
68 {
69  m_edca = edcaQueues;
70 }
71 
72 void
74 {
75  NS_LOG_FUNCTION (mpdu << ampdu << isSingle);
76  NS_ASSERT (ampdu);
77  // if isSingle is true, then ampdu must be empty
78  NS_ASSERT (!isSingle || ampdu->GetSize () == 0);
79 
80  // pad the previous A-MPDU subframe if the A-MPDU is not empty
81  if (ampdu->GetSize () > 0)
82  {
83  uint8_t padding = CalculatePadding (ampdu->GetSize ());
84 
85  if (padding)
86  {
87  Ptr<Packet> pad = Create<Packet> (padding);
88  ampdu->AddAtEnd (pad);
89  }
90  }
91 
92  // add MPDU header and trailer
93  Ptr<Packet> tmp = mpdu->GetPacket ()->Copy ();
94  tmp->AddHeader (mpdu->GetHeader ());
95  AddWifiMacTrailer (tmp);
96 
97  // add A-MPDU subframe header and MPDU to the A-MPDU
99  hdr.SetLength (static_cast<uint16_t> (tmp->GetSize ()));
100  if (isSingle)
101  {
102  hdr.SetEof (1);
103  }
104 
105  tmp->AddHeader (hdr);
106  ampdu->AddAtEnd (tmp);
107 }
108 
109 void
110 MpduAggregator::AddHeaderAndPad (Ptr<Packet> mpdu, bool last, bool isSingleMpdu)
111 {
112  NS_LOG_FUNCTION (mpdu << last << isSingleMpdu);
113  AmpduSubframeHeader currentHdr;
114 
115  //This is called to prepare packets from the aggregate queue to be sent so no need to check total size since it has already been
116  //done before when deciding how many packets to add to the queue
117  currentHdr.SetLength (static_cast<uint16_t> (mpdu->GetSize ()));
118  if (isSingleMpdu)
119  {
120  currentHdr.SetEof (1);
121  }
122 
123  mpdu->AddHeader (currentHdr);
124  uint32_t padding = CalculatePadding (mpdu->GetSize ());
125 
126  if (padding && !last)
127  {
128  Ptr<Packet> pad = Create<Packet> (padding);
129  mpdu->AddAtEnd (pad);
130  }
131 }
132 
133 uint32_t
134 MpduAggregator::GetSizeIfAggregated (uint32_t mpduSize, uint32_t ampduSize)
135 {
136  NS_LOG_FUNCTION (mpduSize << ampduSize);
137 
138  return ampduSize + CalculatePadding (ampduSize) + 4 + mpduSize;
139 }
140 
141 uint32_t
143  WifiModulationClass modulation) const
144 {
145  NS_LOG_FUNCTION (this << recipient << +tid << modulation);
146 
147  AcIndex ac = QosUtilsMapTidToAc (tid);
148  Ptr<QosTxop> qosTxop = m_edca.find (ac)->second;
149  Ptr<WifiNetDevice> device = DynamicCast<WifiNetDevice> (qosTxop->GetLow ()->GetPhy ()->GetDevice ());
150  NS_ASSERT (device);
151  Ptr<WifiRemoteStationManager> stationManager = device->GetRemoteStationManager ();
152  NS_ASSERT (stationManager);
153 
154  // Find the A-MPDU max size configured on this device
155  UintegerValue size;
156 
157  switch (ac)
158  {
159  case AC_BE:
160  device->GetMac ()->GetAttribute ("BE_MaxAmpduSize", size);
161  break;
162  case AC_BK:
163  device->GetMac ()->GetAttribute ("BK_MaxAmpduSize", size);
164  break;
165  case AC_VI:
166  device->GetMac ()->GetAttribute ("VI_MaxAmpduSize", size);
167  break;
168  case AC_VO:
169  device->GetMac ()->GetAttribute ("VO_MaxAmpduSize", size);
170  break;
171  default:
172  NS_ABORT_MSG ("Unknown AC " << ac);
173  return 0;
174  }
175 
176  uint32_t maxAmpduSize = size.Get ();
177 
178  if (maxAmpduSize == 0)
179  {
180  NS_LOG_DEBUG ("A-MPDU Aggregation is disabled on this station for AC " << ac);
181  return 0;
182  }
183 
184  // Retrieve the Capabilities elements advertised by the recipient
185  Ptr<const HeCapabilities> heCapabilities = stationManager->GetStationHeCapabilities (recipient);
186  Ptr<const VhtCapabilities> vhtCapabilities = stationManager->GetStationVhtCapabilities (recipient);
187  Ptr<const HtCapabilities> htCapabilities = stationManager->GetStationHtCapabilities (recipient);
188 
189  // Determine the constraint imposed by the recipient based on the PPDU
190  // format used to transmit the A-MPDU
191  if (modulation == WIFI_MOD_CLASS_HE)
192  {
193  NS_ABORT_MSG_IF (!heCapabilities, "HE Capabilities element not received");
194 
195  maxAmpduSize = std::min (maxAmpduSize, heCapabilities->GetMaxAmpduLength ());
196  }
197  else if (modulation == WIFI_MOD_CLASS_VHT)
198  {
199  NS_ABORT_MSG_IF (!vhtCapabilities, "VHT Capabilities element not received");
200 
201  maxAmpduSize = std::min (maxAmpduSize, vhtCapabilities->GetMaxAmpduLength ());
202  }
203  else if (modulation == WIFI_MOD_CLASS_HT)
204  {
205  NS_ABORT_MSG_IF (!htCapabilities, "HT Capabilities element not received");
206 
207  maxAmpduSize = std::min (maxAmpduSize, htCapabilities->GetMaxAmpduLength ());
208  }
209  else // non-HT PPDU
210  {
211  NS_LOG_DEBUG ("A-MPDU aggregation is not available for non-HT PHYs");
212 
213  maxAmpduSize = 0;
214  }
215 
216  return maxAmpduSize;
217 }
218 
219 uint8_t
221 {
222  return (4 - (ampduSize % 4 )) % 4;
223 }
224 
227 {
229  DeaggregatedMpdus set;
230 
232  Ptr<Packet> extractedMpdu = Create<Packet> ();
233  uint32_t maxSize = aggregatedPacket->GetSize ();
234  uint16_t extractedLength;
235  uint32_t padding;
236  uint32_t deserialized = 0;
237 
238  while (deserialized < maxSize)
239  {
240  deserialized += aggregatedPacket->RemoveHeader (hdr);
241  extractedLength = hdr.GetLength ();
242  extractedMpdu = aggregatedPacket->CreateFragment (0, static_cast<uint32_t> (extractedLength));
243  aggregatedPacket->RemoveAtStart (extractedLength);
244  deserialized += extractedLength;
245 
246  padding = (4 - (extractedLength % 4 )) % 4;
247 
248  if (padding > 0 && deserialized < maxSize)
249  {
250  aggregatedPacket->RemoveAtStart (padding);
251  deserialized += padding;
252  }
253 
254  std::pair<Ptr<Packet>, AmpduSubframeHeader> packetHdr (extractedMpdu, hdr);
255  set.push_back (packetHdr);
256  }
257  NS_LOG_INFO ("Deaggreated A-MPDU: extracted " << set.size () << " MPDUs");
258  return set;
259 }
260 
261 std::list<Ptr<const Packet>>
263 {
264  NS_LOG_FUNCTION (aggregatedPacket);
265  std::list<Ptr<const Packet> > ampduSubframes;
266 
268  uint32_t maxSize = aggregatedPacket->GetSize ();
269  uint16_t bytesToExtract;
270  uint32_t padding;
271  uint32_t deserialized = 0;
272 
273  Ptr<Packet> tempPacket = Create<Packet> ();
274  while (deserialized < maxSize)
275  {
276  tempPacket = aggregatedPacket->CreateFragment (deserialized, hdr.GetSerializedSize ());
277  bytesToExtract = tempPacket->PeekHeader (hdr);
278  bytesToExtract += hdr.GetLength ();
279 
280  padding = (4 - (hdr.GetLength () % 4 )) % 4;
281  if (padding > 0 && (deserialized + bytesToExtract) < maxSize)
282  {
283  bytesToExtract += padding;
284  }
285 
286  ampduSubframes.push_back (aggregatedPacket->CreateFragment (deserialized , bytesToExtract));
287  deserialized += bytesToExtract;
288  }
289  NS_LOG_INFO ("Peek A-MPDU subframes of A-MPDU: peeked " << ampduSubframes.size () << " A-MPDU subrames");
290  return ampduSubframes;
291 }
292 
293 std::list<Ptr<const Packet>>
295 {
296  NS_LOG_FUNCTION (aggregatedPacket);
297  std::list<Ptr<const Packet> > ampduSubframes = PeekAmpduSubframes (aggregatedPacket);
298  std::list<Ptr<const Packet> > mpdus;
299 
300  for (const auto& subframe : ampduSubframes)
301  {
302  mpdus.push_back (PeekMpduInAmpduSubframe (subframe));
303  }
304  NS_LOG_INFO ("Peek MPDUs of A-MPDU: peeked " << mpdus.size () << " MPDUs");
305  return mpdus;
306 }
307 
310 {
311  NS_LOG_FUNCTION (ampduSubframe);
313  uint32_t headerSize = ampduSubframe->PeekHeader (hdr);
314  return ampduSubframe->CreateFragment (headerSize , hdr.GetLength ());
315 }
316 
317 std::vector<Ptr<WifiMacQueueItem>>
319  Time ppduDurationLimit) const
320 {
321  NS_LOG_FUNCTION (this << *mpdu << ppduDurationLimit);
322  std::vector<Ptr<WifiMacQueueItem>> mpduList;
323  Mac48Address recipient = mpdu->GetHeader ().GetAddr1 ();
324 
325  NS_ASSERT (mpdu->GetHeader ().IsQosData () && !recipient.IsBroadcast ());
326 
327  uint8_t tid = GetTid (mpdu->GetPacket (), mpdu->GetHeader ());
328  auto edcaIt = m_edca.find (QosUtilsMapTidToAc (tid));
329  NS_ASSERT (edcaIt != m_edca.end ());
330 
331  WifiModulationClass modulation = txVector.GetMode ().GetModulationClass ();
332  uint32_t maxAmpduSize = GetMaxAmpduSize (recipient, tid, modulation);
333 
334  if (maxAmpduSize == 0)
335  {
336  NS_LOG_DEBUG ("A-MPDU aggregation disabled");
337  return mpduList;
338  }
339 
340  //Have to make sure that the block ACK agreement is established before sending an A-MPDU
341  if (edcaIt->second->GetBaAgreementEstablished (recipient, tid))
342  {
343  /* here is performed mpdu aggregation */
344  uint16_t startingSequenceNumber = edcaIt->second->GetBaStartingSequence (recipient, tid);
345  Ptr<WifiMacQueueItem> nextMpdu;
346  uint16_t maxMpdus = edcaIt->second->GetBaBufferSize (recipient, tid);
347  uint32_t currentAmpduSize = 0;
348 
349  // check if the received MPDU meets the size and duration constraints
350  if (edcaIt->second->GetLow ()->IsWithinSizeAndTimeLimits (mpdu, txVector, 0, ppduDurationLimit))
351  {
352  // mpdu can be aggregated
353  nextMpdu = Copy (mpdu);
354  }
355 
356  while (nextMpdu != 0)
357  {
358  /* if we are here, nextMpdu can be aggregated to the A-MPDU.
359  * nextMpdu may be any of the following:
360  * (a) an A-MSDU (with all the constituent MSDUs dequeued from
361  * the EDCA queue)
362  * (b) an MSDU dequeued from the EDCA queue
363  * (c) a retransmitted MSDU or A-MSDU dequeued from the BA Manager queue
364  * (d) an MPDU that was aggregated in an A-MPDU which was not
365  * transmitted (e.g., because the RTS/CTS exchange failed)
366  */
367 
368  currentAmpduSize = GetSizeIfAggregated (nextMpdu->GetSize (), currentAmpduSize);
369 
370  NS_LOG_DEBUG ("Adding packet with sequence number " << nextMpdu->GetHeader ().GetSequenceNumber ()
371  << " to A-MPDU, packet size = " << nextMpdu->GetSize ()
372  << ", A-MPDU size = " << currentAmpduSize);
373 
374  // Always use the Normal Ack policy (Implicit Block Ack), for now
375  nextMpdu->GetHeader ().SetQosAckPolicy (WifiMacHeader::NORMAL_ACK);
376 
377  mpduList.push_back (nextMpdu);
378 
379  // If allowed by the BA agreement, get the next MPDU
380  nextMpdu = 0;
381 
382  Ptr<const WifiMacQueueItem> peekedMpdu;
383  peekedMpdu = edcaIt->second->PeekNextFrame (tid, recipient);
384  if (peekedMpdu != 0)
385  {
386  uint16_t currentSequenceNumber = peekedMpdu->GetHeader ().GetSequenceNumber ();
387 
388  if (IsInWindow (currentSequenceNumber, startingSequenceNumber, maxMpdus))
389  {
390  // dequeue the frame if constraints on size and duration limit are met.
391  // Note that the dequeued MPDU differs from the peeked MPDU if A-MSDU
392  // aggregation is performed during the dequeue
393  NS_LOG_DEBUG ("Trying to aggregate another MPDU");
394  nextMpdu = edcaIt->second->DequeuePeekedFrame (peekedMpdu, txVector, true,
395  currentAmpduSize, ppduDurationLimit);
396  }
397  }
398  }
399  if (mpduList.size () == 1)
400  {
401  // return an empty vector if it was not possible to aggregate at least two MPDUs
402  mpduList.clear ();
403  }
404  }
405  return mpduList;
406 }
407 
408 } //namespace ns3
Aggregator used to construct A-MPDUs.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
bool IsBroadcast(void) const
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
void AddWifiMacTrailer(Ptr< Packet > packet)
Add FCS trailer to a packet.
Definition: wifi-utils.cc:221
#define min(a, b)
Definition: 80211b.c:42
static Ptr< const Packet > PeekMpduInAmpduSubframe(Ptr< const Packet > ampduSubframe)
Peeks the MPDU contained in the A-MPDU subframe.
Ptr< Packet > CreateFragment(uint32_t start, uint32_t length) const
Create a new packet which contains a fragment of the original packet.
Definition: packet.cc:227
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
static DeaggregatedMpdus Deaggregate(Ptr< Packet > aggregatedPacket)
Deaggregates an A-MPDU by removing the A-MPDU subframe header and padding.
std::vector< Ptr< WifiMacQueueItem > > GetNextAmpdu(Ptr< const WifiMacQueueItem > mpdu, WifiTxVector txVector, Time ppduDurationLimit=Seconds(0)) const
Attempt to aggregate other MPDUs to the given MPDU, while meeting the following constraints: ...
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:280
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
VHT PHY (Clause 22)
Definition: wifi-mode.h:60
Video.
Definition: qos-utils.h:45
Voice.
Definition: qos-utils.h:47
Best Effort.
Definition: qos-utils.h:41
static uint32_t GetSizeIfAggregated(uint32_t mpduSize, uint32_t ampduSize)
Compute the size of the A-MPDU resulting from the aggregation of an MPDU of size mpduSize and an A-MP...
static void AddHeaderAndPad(Ptr< Packet > mpdu, bool last, bool isSingleMpdu)
Background.
Definition: qos-utils.h:43
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:362
Ptr< WifiRemoteStationManager > GetRemoteStationManager(void) const
static std::list< Ptr< const Packet > > PeekMpdus(Ptr< const Packet > aggregatedPacket)
Peeks the MPDUs of the provided A-MPDU.
Hold an unsigned integer type.
Definition: uinteger.h:44
AcIndex QosUtilsMapTidToAc(uint8_t tid)
Maps TID (Traffic ID) to Access classes.
Definition: qos-utils.cc:32
WifiMode GetMode(void) const
Introspection did not find any typical Config paths.
HT PHY (Clause 20)
Definition: wifi-mode.h:58
uint32_t PeekHeader(Header &header) const
Deserialize but does not remove the header from the internal buffer.
Definition: packet.cc:290
Ptr< const HeCapabilities > GetStationHeCapabilities(Mac48Address from)
Return the HE capabilities sent by the remote station.
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:494
Ptr< const HtCapabilities > GetStationHtCapabilities(Mac48Address from)
Return the HT capabilities sent by the remote station.
static void Aggregate(Ptr< const WifiMacQueueItem > mpdu, Ptr< Packet > ampdu, bool isSingle)
Aggregate an MPDU to an A-MPDU.
void SetEof(bool eof)
Set the EOF field.
uint64_t Get(void) const
Definition: uinteger.cc:35
void SetLength(uint16_t length)
Set the length field.
Ptr< MacLow > GetLow(void) const
Return the MacLow associated with this Txop.
Definition: txop.cc:355
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static uint8_t CalculatePadding(uint32_t ampduSize)
bool IsInWindow(uint16_t seq, uint16_t winstart, uint16_t winsize)
Definition: wifi-utils.cc:215
an EUI-48 address
Definition: mac48-address.h:43
uint8_t GetTid(Ptr< const Packet > packet, const WifiMacHeader hdr)
Extraction operator for TypeId.
Definition: qos-utils.cc:99
uint32_t GetSerializedSize(void) const
Ptr< WifiMac > GetMac(void) const
WifiModulationClass
This enumeration defines the modulation classes per (Table 9-4 "Modulation classes"; IEEE 802...
Definition: wifi-mode.h:36
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
uint32_t GetMaxAmpduSize(Mac48Address recipient, uint8_t tid, WifiModulationClass modulation) const
Determine the maximum size for an A-MPDU of the given TID that can be sent to the given receiver when...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
static TypeId GetTypeId(void)
Get the type ID.
Ptr< const VhtCapabilities > GetStationVhtCapabilities(Mac48Address from)
Return the VHT capabilities sent by the remote station.
uint16_t GetLength(void) const
Return the length field.
std::list< std::pair< Ptr< Packet >, AmpduSubframeHeader > > DeaggregatedMpdus
A list of deaggregated packets and their A-MPDU subframe headers.
std::map< AcIndex, Ptr< QosTxop > > EdcaQueues
EDCA queues typedef.
static std::list< Ptr< const Packet > > PeekAmpduSubframes(Ptr< const Packet > aggregatedPacket)
Peeks the A-MPDU subframes of the provided A-MPDU.
A base class which provides memory management and object aggregation.
Definition: object.h:87
EdcaQueues m_edca
the map of EDCA queues
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
AcIndex
This enumeration defines the Access Categories as an enumeration with values corresponding to the AC ...
Definition: qos-utils.h:38
Ptr< T > Copy(Ptr< T > object)
Return a deep copy of a Ptr.
Definition: ptr.h:688
HE PHY (Clause 26)
Definition: wifi-mode.h:62
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
void SetEdcaQueues(EdcaQueues edcaQueues)
Set the map of EDCA queues.