A Discrete-Event Network Simulator
API
msdu-aggregator.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 MIRKO BANCHI
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: Mirko Banchi <mk.banchi@gmail.com>
19  * Stefano Avallone <stavallo@unina.it>
20  */
21 
22 #include "ns3/log.h"
23 #include "ns3/packet.h"
24 #include "msdu-aggregator.h"
25 #include "amsdu-subframe-header.h"
26 #include "qos-txop.h"
27 #include "mpdu-aggregator.h"
29 #include "mac-low.h"
30 #include "wifi-phy.h"
31 #include "wifi-net-device.h"
32 #include "ht-capabilities.h"
33 #include "wifi-mac.h"
34 #include "wifi-mac-queue.h"
35 #include "wifi-mac-trailer.h"
36 #include <algorithm>
37 
38 namespace ns3 {
39 
40 NS_LOG_COMPONENT_DEFINE ("MsduAggregator");
41 
42 NS_OBJECT_ENSURE_REGISTERED (MsduAggregator);
43 
44 TypeId
46 {
47  static TypeId tid = TypeId ("ns3::MsduAggregator")
48  .SetParent<Object> ()
49  .SetGroupName ("Wifi")
50  .AddConstructor<MsduAggregator> ()
51  ;
52  return tid;
53 }
54 
56 {
57 }
58 
60 {
61 }
62 
63 void
65 {
66  m_edca = map;
67 }
68 
69 uint16_t
70 MsduAggregator::GetSizeIfAggregated (uint16_t msduSize, uint16_t amsduSize)
71 {
72  NS_LOG_FUNCTION (msduSize << amsduSize);
73 
74  // the size of the A-MSDU subframe header is 14 bytes: DA (6), SA (6) and Length (2)
75  return amsduSize + CalculatePadding (amsduSize) + 14 + msduSize;
76 }
77 
78 void
80  Mac48Address src, Mac48Address dest) const
81 {
82  NS_LOG_FUNCTION (this);
83  NS_ASSERT (amsdu);
84 
85  // pad the previous A-MSDU subframe if the A-MSDU is not empty
86  if (amsdu->GetSize () > 0)
87  {
88  uint8_t padding = CalculatePadding (amsdu->GetSize ());
89 
90  if (padding)
91  {
92  Ptr<Packet> pad = Create<Packet> (padding);
93  amsdu->AddAtEnd (pad);
94  }
95  }
96 
97  // add A-MSDU subframe header and MSDU
99  hdr.SetDestinationAddr (dest);
100  hdr.SetSourceAddr (src);
101  hdr.SetLength (static_cast<uint16_t> (msdu->GetSize ()));
102 
103  Ptr<Packet> tmp = msdu->Copy ();
104  tmp->AddHeader (hdr);
105  amsdu->AddAtEnd (tmp);
106 }
107 
110  WifiTxVector txVector, uint32_t ampduSize,
111  Time ppduDurationLimit) const
112 {
113  NS_LOG_FUNCTION (recipient << +tid << txVector << ampduSize << ppduDurationLimit);
114 
115  /* "The Address 1 field of an MPDU carrying an A-MSDU shall be set to an
116  * individual address or to the GCR concealment address" (Section 10.12
117  * of 802.11-2016)
118  */
119  NS_ABORT_MSG_IF (recipient.IsBroadcast (), "Recipient address is broadcast");
120 
121  Ptr<QosTxop> qosTxop = m_edca.find (QosUtilsMapTidToAc (tid))->second;
122  Ptr<WifiMacQueue> queue = qosTxop->GetWifiMacQueue ();
123  WifiMacQueue::ConstIterator peekedIt = queue->PeekByTidAndAddress (tid, recipient);
124 
125  if (peekedIt == queue->end ())
126  {
127  NS_LOG_DEBUG ("No packet with the given TID and address in the queue");
128  return 0;
129  }
130 
131  /* "A STA shall not transmit an A-MSDU within a QoS Data frame under a block
132  * ack agreement unless the recipient indicates support for A-MSDU by setting
133  * the A-MSDU Supported field to 1 in its BlockAck Parameter Set field of the
134  * ADDBA Response frame" (Section 10.12 of 802.11-2016)
135  */
136  // No check required for now, as we always set the A-MSDU Supported field to 1
137 
138  WifiModulationClass modulation = txVector.GetMode ().GetModulationClass ();
139 
140  // Get the maximum size of the A-MSDU we can send to the recipient
141  uint16_t maxAmsduSize = GetMaxAmsduSize (recipient, tid, modulation);
142 
143  if (maxAmsduSize == 0)
144  {
145  return 0;
146  }
147 
148  Ptr<Packet> amsdu = Create<Packet> ();
149  uint8_t nMsdu = 0;
150  WifiMacHeader header = (*peekedIt)->GetHeader ();
151  Time tstamp = (*peekedIt)->GetTimeStamp ();
152  // We need to keep track of the first MSDU. When it is processed, it is not known
153  // if aggregation will succeed or not.
154  WifiMacQueue::ConstIterator first = peekedIt;
155 
156  // TODO Add support for the Max Number Of MSDUs In A-MSDU field in the Extended
157  // Capabilities element sent by the recipient
158 
159  while (peekedIt != queue->end ())
160  {
161  // check if aggregating the peeked MSDU violates the A-MSDU size limit
162  uint16_t newAmsduSize = GetSizeIfAggregated ((*peekedIt)->GetPacket ()->GetSize (),
163  amsdu->GetSize ());
164 
165  if (newAmsduSize > maxAmsduSize)
166  {
167  NS_LOG_DEBUG ("No other MSDU can be aggregated: maximum A-MSDU size reached");
168  break;
169  }
170 
171  // check if the A-MSDU obtained by aggregating the peeked MSDU violates
172  // the A-MPDU size limit or the PPDU duration limit
173  if (!qosTxop->GetLow ()->IsWithinSizeAndTimeLimits (header.GetSize () + newAmsduSize + WIFI_MAC_FCS_LENGTH,
174  recipient, tid, txVector, ampduSize, ppduDurationLimit))
175  {
176  NS_LOG_DEBUG ("No other MSDU can be aggregated");
177  break;
178  }
179 
180  // We can now safely aggregate the MSDU to the A-MSDU
181  Aggregate ((*peekedIt)->GetPacket (), amsdu,
182  qosTxop->MapSrcAddressForAggregation (header),
183  qosTxop->MapDestAddressForAggregation (header));
184 
185  /* "The expiration of the A-MSDU lifetime timer occurs only when the lifetime
186  * timer of all of the constituent MSDUs of the A-MSDU have expired" (Section
187  * 10.12 of 802.11-2016)
188  */
189  // The timestamp of the A-MSDU is the most recent among those of the MSDUs
190  tstamp = Max (tstamp, (*peekedIt)->GetTimeStamp ());
191 
192  // If it is the first MSDU, move to the next one
193  if (nMsdu == 0)
194  {
195  peekedIt++;
196  }
197  // otherwise, remove it from the queue
198  else
199  {
200  peekedIt = queue->Remove (peekedIt);
201  }
202 
203  nMsdu++;
204 
205  peekedIt = queue->PeekByTidAndAddress (tid, recipient, peekedIt);
206  }
207 
208  if (nMsdu < 2)
209  {
210  NS_LOG_DEBUG ("Aggregation failed (could not aggregate at least two MSDUs)");
211  return 0;
212  }
213 
214  // Aggregation succeeded, we have to remove the first MSDU
215  queue->Remove (first);
216 
217  header.SetQosAmsdu ();
218  header.SetAddr3 (qosTxop->GetLow ()->GetBssid ());
219 
220  return Create<WifiMacQueueItem> (amsdu, header, tstamp);
221 }
222 
223 uint8_t
225 {
226  return (4 - (amsduSize % 4 )) % 4;
227 }
228 
229 uint16_t
231  WifiModulationClass modulation) const
232 {
233  NS_LOG_FUNCTION (this << recipient << +tid << modulation);
234 
235  AcIndex ac = QosUtilsMapTidToAc (tid);
236  Ptr<QosTxop> qosTxop = m_edca.find (ac)->second;
237  Ptr<WifiNetDevice> device = DynamicCast<WifiNetDevice> (qosTxop->GetLow ()->GetPhy ()->GetDevice ());
238  NS_ASSERT (device);
239  Ptr<WifiRemoteStationManager> stationManager = device->GetRemoteStationManager ();
240  NS_ASSERT (stationManager);
241 
242  // Find the A-MSDU max size configured on this device
243  UintegerValue size;
244 
245  switch (ac)
246  {
247  case AC_BE:
248  device->GetMac ()->GetAttribute ("BE_MaxAmsduSize", size);
249  break;
250  case AC_BK:
251  device->GetMac ()->GetAttribute ("BK_MaxAmsduSize", size);
252  break;
253  case AC_VI:
254  device->GetMac ()->GetAttribute ("VI_MaxAmsduSize", size);
255  break;
256  case AC_VO:
257  device->GetMac ()->GetAttribute ("VO_MaxAmsduSize", size);
258  break;
259  default:
260  NS_ABORT_MSG ("Unknown AC " << ac);
261  return 0;
262  }
263 
264  uint16_t maxAmsduSize = size.Get ();
265 
266  if (maxAmsduSize == 0)
267  {
268  NS_LOG_DEBUG ("A-MSDU Aggregation is disabled on this station for AC " << ac);
269  return 0;
270  }
271 
272  // Retrieve the Capabilities elements advertised by the recipient
273  Ptr<const VhtCapabilities> vhtCapabilities = stationManager->GetStationVhtCapabilities (recipient);
274  Ptr<const HtCapabilities> htCapabilities = stationManager->GetStationHtCapabilities (recipient);
275 
276  if (!htCapabilities)
277  {
278  /* "A non-DMG STA shall not transmit an A-MSDU to a STA from which it has
279  * not received a frame containing an HT Capabilities element" (Section
280  * 10.12 of 802.11-2016)
281  */
282  NS_LOG_DEBUG ("A-MSDU Aggregation disabled because the recipient did not"
283  " send an HT Capabilities element");
284  return 0;
285  }
286 
287  // Determine the constraint imposed by the recipient based on the PPDU
288  // format used to transmit the A-MSDU
289  if (modulation == WIFI_MOD_CLASS_HE || modulation == WIFI_MOD_CLASS_VHT)
290  {
291  // the maximum A-MSDU size is indirectly constrained by the maximum MPDU
292  // size supported by the recipient and advertised in the VHT Capabilities
293  // element (see Table 9-19 of 802.11-2016 as amended by 802.11ax)
294  NS_ABORT_MSG_IF (!vhtCapabilities, "VHT Capabilities element not received");
295 
296  maxAmsduSize = std::min (maxAmsduSize, static_cast<uint16_t>(vhtCapabilities->GetMaxMpduLength () - 56));
297  }
298  else if (modulation == WIFI_MOD_CLASS_HT)
299  {
300  // the maximum A-MSDU size is constrained by the maximum A-MSDU size
301  // supported by the recipient and advertised in the HT Capabilities
302  // element (see Table 9-19 of 802.11-2016)
303 
304  maxAmsduSize = std::min (maxAmsduSize, htCapabilities->GetMaxAmsduLength ());
305  }
306  else // non-HT PPDU
307  {
308  // the maximum A-MSDU size is indirectly constrained by the maximum PSDU size
309  // supported by the recipient (see Table 9-19 of 802.11-2016)
310 
311  maxAmsduSize = std::min (maxAmsduSize, static_cast<uint16_t>(3839));
312  }
313 
314  return maxAmsduSize;
315 }
316 
319 {
321  DeaggregatedMsdus set;
322 
324  Ptr<Packet> extractedMsdu = Create<Packet> ();
325  uint32_t maxSize = aggregatedPacket->GetSize ();
326  uint16_t extractedLength;
327  uint8_t padding;
328  uint32_t deserialized = 0;
329 
330  while (deserialized < maxSize)
331  {
332  deserialized += aggregatedPacket->RemoveHeader (hdr);
333  extractedLength = hdr.GetLength ();
334  extractedMsdu = aggregatedPacket->CreateFragment (0, static_cast<uint32_t> (extractedLength));
335  aggregatedPacket->RemoveAtStart (extractedLength);
336  deserialized += extractedLength;
337 
338  padding = (4 - ((extractedLength + 14) % 4 )) % 4;
339 
340  if (padding > 0 && deserialized < maxSize)
341  {
342  aggregatedPacket->RemoveAtStart (padding);
343  deserialized += padding;
344  }
345 
346  std::pair<Ptr<Packet>, AmsduSubframeHeader> packetHdr (extractedMsdu, hdr);
347  set.push_back (packetHdr);
348  }
349  NS_LOG_INFO ("Deaggreated A-MSDU: extracted " << set.size () << " MSDUs");
350  return set;
351 }
352 
353 } //namespace ns3
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
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
uint16_t GetMaxAmsduSize(Mac48Address recipient, uint8_t tid, WifiModulationClass modulation) const
Determine the maximum size for an A-MSDU of the given TID that can be sent to the given receiver when...
#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
#define min(a, b)
Definition: 80211b.c:42
uint32_t GetSize(void) const
Return the size of the WifiMacHeader in octets.
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
static const uint16_t WIFI_MAC_FCS_LENGTH
The length in octects of the IEEE 802.11 MAC FCS field.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
static DeaggregatedMsdus Deaggregate(Ptr< Packet > aggregatedPacket)
#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.
EdcaQueues m_edca
the map of EDCA queues
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
void SetSourceAddr(Mac48Address to)
Set source address function.
Ptr< WifiMacQueueItem > GetNextAmsdu(Mac48Address recipient, uint8_t tid, WifiTxVector txVector, uint32_t ampduSize=0, Time ppduDurationLimit=Seconds(0)) const
Dequeue MSDUs to be transmitted to a given station and belonging to a given TID from the correspondin...
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
void SetEdcaQueues(EdcaQueues map)
Set the map of EDCA queues.
void SetAddr3(Mac48Address address)
Fill the Address 3 field with the given address.
Hold an unsigned integer type.
Definition: uinteger.h:44
int64x64_t Max(const int64x64_t &a, const int64x64_t &b)
Maximum.
Definition: int64x64.h:209
AcIndex QosUtilsMapTidToAc(uint8_t tid)
Maps TID (Traffic ID) to Access classes.
Definition: qos-utils.cc:32
WifiMode GetMode(void) const
static TypeId GetTypeId(void)
Get the type ID.
HT PHY (Clause 20)
Definition: wifi-mode.h:58
static uint16_t GetSizeIfAggregated(uint16_t msduSize, uint16_t amsduSize)
Compute the size of the A-MSDU resulting from the aggregation of an MSDU of size msduSize and an A-MS...
void Aggregate(Ptr< const Packet > msdu, Ptr< Packet > amsdu, Mac48Address src, Mac48Address dest) const
Aggregate an MSDU to an A-MSDU.
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:494
Ptr< const HtCapabilities > GetStationHtCapabilities(Mac48Address from)
Return the HT capabilities sent by the remote station.
void SetLength(uint16_t length)
Set length function.
uint64_t Get(void) const
Definition: uinteger.cc:35
Ptr< MacLow > GetLow(void) const
Return the MacLow associated with this Txop.
Definition: txop.cc:355
std::list< std::pair< Ptr< Packet >, AmsduSubframeHeader > > DeaggregatedMsdus
DeaggregatedMsdus typedef.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
an EUI-48 address
Definition: mac48-address.h:43
Introspection did not find any typical Config paths.
void SetDestinationAddr(Mac48Address to)
Set destination address function.
uint16_t GetLength(void) const
Get length function.
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
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:272
Ptr< const VhtCapabilities > GetStationVhtCapabilities(Mac48Address from)
Return the VHT capabilities sent by the remote station.
static uint8_t CalculatePadding(uint16_t amsduSize)
Calculate how much padding must be added to the end of an A-MSDU of the given size if a new MSDU is a...
Aggregator used to construct A-MSDUs.
A base class which provides memory management and object aggregation.
Definition: object.h:87
Definition: first.py:1
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:915
void SetQosAmsdu(void)
Set that A-MSDU is present.
AcIndex
This enumeration defines the Access Categories as an enumeration with values corresponding to the AC ...
Definition: qos-utils.h:38
HE PHY (Clause 26)
Definition: wifi-mode.h:62
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
Implements the IEEE 802.11 MAC header.
std::map< AcIndex, Ptr< QosTxop > > EdcaQueues
EDCA queues typedef.