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 
80  WifiTxVector txVector, uint32_t ampduSize,
81  Time ppduDurationLimit) const
82 {
83  NS_LOG_FUNCTION (recipient << +tid << txVector << ampduSize << ppduDurationLimit);
84 
85  /* "The Address 1 field of an MPDU carrying an A-MSDU shall be set to an
86  * individual address or to the GCR concealment address" (Section 10.12
87  * of 802.11-2016)
88  */
89  NS_ABORT_MSG_IF (recipient.IsBroadcast (), "Recipient address is broadcast");
90 
91  Ptr<QosTxop> qosTxop = m_edca.find (QosUtilsMapTidToAc (tid))->second;
92  Ptr<WifiMacQueue> queue = qosTxop->GetWifiMacQueue ();
93  WifiMacQueue::ConstIterator peekedIt = queue->PeekByTidAndAddress (tid, recipient);
94 
95  if (peekedIt == queue->end ())
96  {
97  NS_LOG_DEBUG ("No packet with the given TID and address in the queue");
98  return 0;
99  }
100 
101  /* "A STA shall not transmit an A-MSDU within a QoS Data frame under a block
102  * ack agreement unless the recipient indicates support for A-MSDU by setting
103  * the A-MSDU Supported field to 1 in its BlockAck Parameter Set field of the
104  * ADDBA Response frame" (Section 10.12 of 802.11-2016)
105  */
106  // No check required for now, as we always set the A-MSDU Supported field to 1
107 
108  WifiModulationClass modulation = txVector.GetMode ().GetModulationClass ();
109 
110  // Get the maximum size of the A-MSDU we can send to the recipient
111  uint16_t maxAmsduSize = GetMaxAmsduSize (recipient, tid, modulation);
112 
113  if (maxAmsduSize == 0)
114  {
115  return 0;
116  }
117 
118  Ptr<WifiMacQueueItem> amsdu = Create<WifiMacQueueItem> (Create<const Packet> (), (*peekedIt)->GetHeader ());
119  uint8_t nMsdu = 0;
120  // We need to keep track of the first MSDU. When it is processed, it is not known
121  // if aggregation will succeed or not.
122  WifiMacQueue::ConstIterator first = peekedIt;
123 
124  // TODO Add support for the Max Number Of MSDUs In A-MSDU field in the Extended
125  // Capabilities element sent by the recipient
126 
127  while (peekedIt != queue->end ())
128  {
129  // check if aggregating the peeked MSDU violates the A-MSDU size limit
130  uint16_t newAmsduSize = GetSizeIfAggregated ((*peekedIt)->GetPacket ()->GetSize (),
131  amsdu->GetPacket ()->GetSize ());
132 
133  if (newAmsduSize > maxAmsduSize)
134  {
135  NS_LOG_DEBUG ("No other MSDU can be aggregated: maximum A-MSDU size reached");
136  break;
137  }
138 
139  // check if the A-MSDU obtained by aggregating the peeked MSDU violates
140  // the A-MPDU size limit or the PPDU duration limit
141  if (!qosTxop->GetLow ()->IsWithinSizeAndTimeLimits (amsdu->GetHeader ().GetSize () + newAmsduSize
143  recipient, tid, txVector, ampduSize, ppduDurationLimit))
144  {
145  NS_LOG_DEBUG ("No other MSDU can be aggregated");
146  break;
147  }
148 
149  // The MSDU can be aggregated to the A-MSDU.
150  // If it is the first MSDU, move to the next one
151  if (nMsdu == 0)
152  {
153  amsdu = Copy (*peekedIt);
154  peekedIt++;
155  }
156  // otherwise, remove it from the queue
157  else
158  {
159  amsdu->Aggregate (*peekedIt);
160  peekedIt = queue->Remove (peekedIt);
161  }
162 
163  nMsdu++;
164 
165  peekedIt = queue->PeekByTidAndAddress (tid, recipient, peekedIt);
166  }
167 
168  if (nMsdu < 2)
169  {
170  NS_LOG_DEBUG ("Aggregation failed (could not aggregate at least two MSDUs)");
171  return 0;
172  }
173 
174  // Aggregation succeeded, we have to remove the first MSDU
175  queue->Remove (first);
176 
177  return amsdu;
178 }
179 
180 uint8_t
182 {
183  return (4 - (amsduSize % 4 )) % 4;
184 }
185 
186 uint16_t
188  WifiModulationClass modulation) const
189 {
190  NS_LOG_FUNCTION (this << recipient << +tid << modulation);
191 
192  AcIndex ac = QosUtilsMapTidToAc (tid);
193  Ptr<QosTxop> qosTxop = m_edca.find (ac)->second;
194  Ptr<WifiNetDevice> device = DynamicCast<WifiNetDevice> (qosTxop->GetLow ()->GetPhy ()->GetDevice ());
195  NS_ASSERT (device);
196  Ptr<WifiRemoteStationManager> stationManager = device->GetRemoteStationManager ();
197  NS_ASSERT (stationManager);
198 
199  // Find the A-MSDU max size configured on this device
200  UintegerValue size;
201 
202  switch (ac)
203  {
204  case AC_BE:
205  device->GetMac ()->GetAttribute ("BE_MaxAmsduSize", size);
206  break;
207  case AC_BK:
208  device->GetMac ()->GetAttribute ("BK_MaxAmsduSize", size);
209  break;
210  case AC_VI:
211  device->GetMac ()->GetAttribute ("VI_MaxAmsduSize", size);
212  break;
213  case AC_VO:
214  device->GetMac ()->GetAttribute ("VO_MaxAmsduSize", size);
215  break;
216  default:
217  NS_ABORT_MSG ("Unknown AC " << ac);
218  return 0;
219  }
220 
221  uint16_t maxAmsduSize = size.Get ();
222 
223  if (maxAmsduSize == 0)
224  {
225  NS_LOG_DEBUG ("A-MSDU Aggregation is disabled on this station for AC " << ac);
226  return 0;
227  }
228 
229  // Retrieve the Capabilities elements advertised by the recipient
230  Ptr<const VhtCapabilities> vhtCapabilities = stationManager->GetStationVhtCapabilities (recipient);
231  Ptr<const HtCapabilities> htCapabilities = stationManager->GetStationHtCapabilities (recipient);
232 
233  if (!htCapabilities)
234  {
235  /* "A non-DMG STA shall not transmit an A-MSDU to a STA from which it has
236  * not received a frame containing an HT Capabilities element" (Section
237  * 10.12 of 802.11-2016)
238  */
239  NS_LOG_DEBUG ("A-MSDU Aggregation disabled because the recipient did not"
240  " send an HT Capabilities element");
241  return 0;
242  }
243 
244  // Determine the constraint imposed by the recipient based on the PPDU
245  // format used to transmit the A-MSDU
246  if (modulation >= WIFI_MOD_CLASS_VHT)
247  {
248  // the maximum A-MSDU size is indirectly constrained by the maximum MPDU
249  // size supported by the recipient and advertised in the VHT Capabilities
250  // element (see Table 9-19 of 802.11-2016 as amended by 802.11ax)
251  NS_ABORT_MSG_IF (!vhtCapabilities, "VHT Capabilities element not received");
252 
253  maxAmsduSize = std::min (maxAmsduSize, static_cast<uint16_t>(vhtCapabilities->GetMaxMpduLength () - 56));
254  }
255  else if (modulation == WIFI_MOD_CLASS_HT)
256  {
257  // the maximum A-MSDU size is constrained by the maximum A-MSDU size
258  // supported by the recipient and advertised in the HT Capabilities
259  // element (see Table 9-19 of 802.11-2016)
260 
261  maxAmsduSize = std::min (maxAmsduSize, htCapabilities->GetMaxAmsduLength ());
262  }
263  else // non-HT PPDU
264  {
265  // the maximum A-MSDU size is indirectly constrained by the maximum PSDU size
266  // supported by the recipient (see Table 9-19 of 802.11-2016)
267 
268  maxAmsduSize = std::min (maxAmsduSize, static_cast<uint16_t>(3839));
269  }
270 
271  return maxAmsduSize;
272 }
273 
276 {
278  DeaggregatedMsdus set;
279 
281  Ptr<Packet> extractedMsdu = Create<Packet> ();
282  uint32_t maxSize = aggregatedPacket->GetSize ();
283  uint16_t extractedLength;
284  uint8_t padding;
285  uint32_t deserialized = 0;
286 
287  while (deserialized < maxSize)
288  {
289  deserialized += aggregatedPacket->RemoveHeader (hdr);
290  extractedLength = hdr.GetLength ();
291  extractedMsdu = aggregatedPacket->CreateFragment (0, static_cast<uint32_t> (extractedLength));
292  aggregatedPacket->RemoveAtStart (extractedLength);
293  deserialized += extractedLength;
294 
295  padding = (4 - ((extractedLength + 14) % 4 )) % 4;
296 
297  if (padding > 0 && deserialized < maxSize)
298  {
299  aggregatedPacket->RemoveAtStart (padding);
300  deserialized += padding;
301  }
302 
303  std::pair<Ptr<const Packet>, AmsduSubframeHeader> packetHdr (extractedMsdu, hdr);
304  set.push_back (packetHdr);
305  }
306  NS_LOG_INFO ("Deaggreated A-MSDU: extracted " << set.size () << " MSDUs");
307  return set;
308 }
309 
310 } //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
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 "...
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
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:205
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:281
#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
Background.
Definition: qos-utils.h:43
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.
std::list< std::pair< Ptr< const Packet >, AmsduSubframeHeader > > DeaggregatedMsdus
DeaggregatedMsdus typedef.
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
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...
WifiModulationClass GetModulationClass() const
Definition: wifi-mode.cc:463
Ptr< const HtCapabilities > GetStationHtCapabilities(Mac48Address from)
Return the HT capabilities sent by the remote station.
uint64_t Get(void) const
Definition: uinteger.cc:35
Ptr< MacLow > GetLow(void) const
Return the MacLow associated with this Txop.
Definition: txop.cc:366
Every class exported by the ns3 library is enclosed in the ns3 namespace.
an EUI-48 address
Definition: mac48-address.h:43
Headers for A-MSDU subframes.
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:273
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
Ptr< WifiMacQueueItem > GetNextAmsdu(Mac48Address recipient, uint8_t tid, WifiTxVector txVector, uint32_t ampduSize=0, Time ppduDurationLimit=Time::Min()) const
Dequeue MSDUs to be transmitted to a given station and belonging to a given TID from the correspondin...
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:922
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:690
std::map< AcIndex, Ptr< QosTxop > > EdcaQueues
EDCA queues typedef.