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 "qos-txop.h"
27 #include "ns3/ht-capabilities.h"
28 #include "regular-wifi-mac.h"
29 #include "wifi-mac-queue.h"
30 #include "wifi-mac-trailer.h"
31 #include "ns3/ht-frame-exchange-manager.h"
32 #include "wifi-tx-parameters.h"
33 #include <algorithm>
34 
35 namespace ns3 {
36 
37 NS_LOG_COMPONENT_DEFINE ("MsduAggregator");
38 
39 NS_OBJECT_ENSURE_REGISTERED (MsduAggregator);
40 
41 TypeId
43 {
44  static TypeId tid = TypeId ("ns3::MsduAggregator")
45  .SetParent<Object> ()
46  .SetGroupName ("Wifi")
47  .AddConstructor<MsduAggregator> ()
48  ;
49  return tid;
50 }
51 
53 {
54 }
55 
57 {
58 }
59 
60 void
62 {
63  m_mac = 0;
64  m_htFem = 0;
66 }
67 
68 void
70 {
71  NS_LOG_FUNCTION (this << mac);
72  m_mac = mac;
73  m_htFem = DynamicCast<HtFrameExchangeManager> (m_mac->GetFrameExchangeManager ());
74 }
75 
76 uint16_t
77 MsduAggregator::GetSizeIfAggregated (uint16_t msduSize, uint16_t amsduSize)
78 {
79  NS_LOG_FUNCTION (msduSize << amsduSize);
80 
81  // the size of the A-MSDU subframe header is 14 bytes: DA (6), SA (6) and Length (2)
82  return amsduSize + CalculatePadding (amsduSize) + 14 + msduSize;
83 }
84 
87  Time availableTime, WifiMacQueueItem::ConstIterator& queueIt) const
88 {
89  NS_LOG_FUNCTION (this << *peekedItem << &txParams << availableTime);
90 
91  Ptr<WifiMacQueue> queue = m_mac->GetTxopQueue (peekedItem->GetQueueAc ());
92  auto it = peekedItem->GetQueueIterator ();
93  NS_ASSERT ((*it)->GetPacket () == peekedItem->GetPacket ());
94 
95  uint8_t tid = peekedItem->GetHeader ().GetQosTid ();
96  Mac48Address recipient = peekedItem->GetHeader ().GetAddr1 ();
97 
98  /* "The Address 1 field of an MPDU carrying an A-MSDU shall be set to an
99  * individual address or to the GCR concealment address" (Section 10.12
100  * of 802.11-2016)
101  */
102  NS_ABORT_MSG_IF (recipient.IsBroadcast (), "Recipient address is broadcast");
103 
104  /* "A STA shall not transmit an A-MSDU within a QoS Data frame under a block
105  * ack agreement unless the recipient indicates support for A-MSDU by setting
106  * the A-MSDU Supported field to 1 in its BlockAck Parameter Set field of the
107  * ADDBA Response frame" (Section 10.12 of 802.11-2016)
108  */
109  // No check required for now, as we always set the A-MSDU Supported field to 1
110 
111  // TODO Add support for the Max Number Of MSDUs In A-MSDU field in the Extended
112  // Capabilities element sent by the recipient
113 
114  NS_ASSERT (m_htFem != 0);
115 
116  if (GetMaxAmsduSize (recipient, tid, txParams.m_txVector.GetModulationClass ()) == 0)
117  {
118  NS_LOG_DEBUG ("A-MSDU aggregation disabled");
119  return nullptr;
120  }
121 
122  Ptr<WifiMacQueueItem> amsdu = *it; // amsdu points to the peeked MPDU, but it's non-const
123  uint8_t nMsdu = 1;
124 
125  it++;
126 
127  while ((it = queue->PeekByTidAndAddress (tid, recipient, it)) != queue->end ()
128  && m_htFem->TryAggregateMsdu (*it, txParams, availableTime))
129  {
130  // dequeue the MSDU being aggregated and advance the current iterator
131  // before it is invalidated
132  Ptr<WifiMacQueueItem> msdu = *it++;
133  queue->DequeueIfQueued (msdu);
134 
135  auto pos = std::next (amsdu->GetQueueIterator ());
136  queue->DequeueIfQueued (amsdu);
137 
138  amsdu->Aggregate (msdu);
139  bool ret = queue->Insert (pos, amsdu);
140  // The size of a WifiMacQueue is measured as number of packets. We dequeued
141  // two packets, so there is certainly room for inserting one packet
142  NS_ABORT_IF (!ret);
143 
144  nMsdu++;
145  }
146 
147  if (nMsdu == 1)
148  {
149  NS_LOG_DEBUG ("Aggregation failed (could not aggregate at least two MSDUs)");
150  return nullptr;
151  }
152 
153  // Aggregation succeeded
154  queueIt = it;
155 
156  return amsdu;
157 }
158 
159 uint8_t
161 {
162  return (4 - (amsduSize % 4 )) % 4;
163 }
164 
165 uint16_t
167  WifiModulationClass modulation) const
168 {
169  NS_LOG_FUNCTION (this << recipient << +tid << modulation);
170 
171  AcIndex ac = QosUtilsMapTidToAc (tid);
172 
173  // Find the A-MSDU max size configured on this device
174  uint16_t maxAmsduSize = m_mac->GetMaxAmsduSize (ac);
175 
176  if (maxAmsduSize == 0)
177  {
178  NS_LOG_DEBUG ("A-MSDU Aggregation is disabled on this station for AC " << ac);
179  return 0;
180  }
181 
182  Ptr<WifiRemoteStationManager> stationManager = m_mac->GetWifiRemoteStationManager ();
183  NS_ASSERT (stationManager);
184 
185  // Retrieve the Capabilities elements advertised by the recipient
186  Ptr<const VhtCapabilities> vhtCapabilities = stationManager->GetStationVhtCapabilities (recipient);
187  Ptr<const HtCapabilities> htCapabilities = stationManager->GetStationHtCapabilities (recipient);
188 
189  if (!htCapabilities)
190  {
191  /* "A non-DMG STA shall not transmit an A-MSDU to a STA from which it has
192  * not received a frame containing an HT Capabilities element" (Section
193  * 10.12 of 802.11-2016)
194  */
195  NS_LOG_DEBUG ("A-MSDU Aggregation disabled because the recipient did not"
196  " send an HT Capabilities element");
197  return 0;
198  }
199 
200  // Determine the constraint imposed by the recipient based on the PPDU
201  // format used to transmit the A-MSDU
202  if (modulation >= WIFI_MOD_CLASS_VHT)
203  {
204  // the maximum A-MSDU size is indirectly constrained by the maximum MPDU
205  // size supported by the recipient and advertised in the VHT Capabilities
206  // element (see Table 9-19 of 802.11-2016 as amended by 802.11ax)
207  NS_ABORT_MSG_IF (!vhtCapabilities, "VHT Capabilities element not received");
208 
209  maxAmsduSize = std::min (maxAmsduSize, static_cast<uint16_t>(vhtCapabilities->GetMaxMpduLength () - 56));
210  }
211  else if (modulation == WIFI_MOD_CLASS_HT)
212  {
213  // the maximum A-MSDU size is constrained by the maximum A-MSDU size
214  // supported by the recipient and advertised in the HT Capabilities
215  // element (see Table 9-19 of 802.11-2016)
216 
217  maxAmsduSize = std::min (maxAmsduSize, htCapabilities->GetMaxAmsduLength ());
218  }
219  else // non-HT PPDU
220  {
221  // the maximum A-MSDU size is indirectly constrained by the maximum PSDU size
222  // supported by the recipient (see Table 9-19 of 802.11-2016)
223 
224  maxAmsduSize = std::min (maxAmsduSize, static_cast<uint16_t>(3839));
225  }
226 
227  return maxAmsduSize;
228 }
229 
232 {
235 
237  Ptr<Packet> extractedMsdu = Create<Packet> ();
238  uint32_t maxSize = aggregatedPacket->GetSize ();
239  uint16_t extractedLength;
240  uint8_t padding;
241  uint32_t deserialized = 0;
242 
243  while (deserialized < maxSize)
244  {
245  deserialized += aggregatedPacket->RemoveHeader (hdr);
246  extractedLength = hdr.GetLength ();
247  extractedMsdu = aggregatedPacket->CreateFragment (0, static_cast<uint32_t> (extractedLength));
248  aggregatedPacket->RemoveAtStart (extractedLength);
249  deserialized += extractedLength;
250 
251  padding = (4 - ((extractedLength + 14) % 4 )) % 4;
252 
253  if (padding > 0 && deserialized < maxSize)
254  {
255  aggregatedPacket->RemoveAtStart (padding);
256  deserialized += padding;
257  }
258 
259  std::pair<Ptr<const Packet>, AmsduSubframeHeader> packetHdr (extractedMsdu, hdr);
260  set.push_back (packetHdr);
261  }
262  NS_LOG_INFO ("Deaggreated A-MSDU: extracted " << set.size () << " MSDUs");
263  return set;
264 }
265 
266 } //namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::MsduAggregator::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: msdu-aggregator.cc:42
ns3::WifiMacQueueItem::Aggregate
void Aggregate(Ptr< const WifiMacQueueItem > msdu)
Aggregate the MSDU contained in the given MPDU to this MPDU (thus constituting an A-MSDU).
Definition: wifi-mac-queue-item.cc:116
NS_OBJECT_ENSURE_REGISTERED
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
NS_ASSERT
#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
ns3::MsduAggregator::CalculatePadding
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...
Definition: msdu-aggregator.cc:160
min
#define min(a, b)
Definition: 80211b.c:42
regular-wifi-mac.h
ns3::AmsduSubframeHeader::GetLength
uint16_t GetLength(void) const
Get length function.
Definition: amsdu-subframe-header.cc:115
ns3::Packet::GetSize
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
ns3::Packet::CreateFragment
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
wifi-tx-parameters.h
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::MsduAggregator::m_htFem
Ptr< HtFrameExchangeManager > m_htFem
the HT Frame Exchange Manager of this station
Definition: msdu-aggregator.h:149
ns3::WIFI_MOD_CLASS_HT
@ WIFI_MOD_CLASS_HT
HT (Clause 19)
Definition: wifi-phy-common.h:130
ns3::MsduAggregator::DoDispose
void DoDispose() override
Destructor implementation.
Definition: msdu-aggregator.cc:61
ns3::MsduAggregator::~MsduAggregator
virtual ~MsduAggregator()
Definition: msdu-aggregator.cc:56
qos-txop.h
wifi-remote-station-manager.h
ns3::WifiMacQueueItem::DeaggregatedMsdus
std::list< std::pair< Ptr< const Packet >, AmsduSubframeHeader > > DeaggregatedMsdus
DeaggregatedMsdus typedef.
Definition: wifi-mac-queue-item.h:126
ns3::AmsduSubframeHeader
Headers for A-MSDU subframes.
Definition: amsdu-subframe-header.h:34
ns3::Mac48Address
an EUI-48 address
Definition: mac48-address.h:44
third.mac
mac
Definition: third.py:99
ns3::WifiTxParameters
This class stores the TX parameters (TX vector, protection mechanism, acknowledgment mechanism,...
Definition: wifi-tx-parameters.h:45
ns3::TypeId::SetParent
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:923
wifi-mac-queue.h
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::WifiMacQueueItem::ConstIterator
std::list< Ptr< WifiMacQueueItem > >::const_iterator ConstIterator
Const iterator typedef.
Definition: wifi-mac-queue-item.h:144
ns3::WifiRemoteStationManager::GetStationHtCapabilities
Ptr< const HtCapabilities > GetStationHtCapabilities(Mac48Address from)
Return the HT capabilities sent by the remote station.
Definition: wifi-remote-station-manager.cc:1418
ns3::WifiTxVector::GetModulationClass
WifiModulationClass GetModulationClass(void) const
Get the modulation class specified by this TXVECTOR.
Definition: wifi-tx-vector.cc:128
ns3::Packet::RemoveAtStart
void RemoveAtStart(uint32_t size)
Remove size bytes from the start of the current packet.
Definition: packet.cc:362
ns3::Object
A base class which provides memory management and object aggregation.
Definition: object.h:88
NS_LOG_INFO
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
ns3::QosUtilsMapTidToAc
AcIndex QosUtilsMapTidToAc(uint8_t tid)
Maps TID (Traffic ID) to Access classes.
Definition: qos-utils.cc:126
ns3::Mac48Address::IsBroadcast
bool IsBroadcast(void) const
Definition: mac48-address.cc:158
ns3::Packet::RemoveHeader
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
NS_ABORT_MSG_IF
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
ns3::MsduAggregator::GetNextAmsdu
Ptr< WifiMacQueueItem > GetNextAmsdu(Ptr< const WifiMacQueueItem > peekedItem, WifiTxParameters &txParams, Time availableTime, WifiMacQueueItem::ConstIterator &queueIt) const
Attempt to aggregate other MSDUs to the given A-MSDU while meeting the following constraints:
Definition: msdu-aggregator.cc:86
ns3::WifiRemoteStationManager::GetStationVhtCapabilities
Ptr< const VhtCapabilities > GetStationVhtCapabilities(Mac48Address from)
Return the VHT capabilities sent by the remote station.
Definition: wifi-remote-station-manager.cc:1424
msdu-aggregator.h
NS_ABORT_IF
#define NS_ABORT_IF(cond)
Abnormal program termination if a condition is true.
Definition: abort.h:77
ns3::MsduAggregator::m_mac
Ptr< RegularWifiMac > m_mac
the MAC of this station
Definition: msdu-aggregator.h:148
NS_LOG_FUNCTION_NOARGS
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
Definition: log-macros-enabled.h:209
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::WifiTxParameters::m_txVector
WifiTxVector m_txVector
TXVECTOR of the frame being prepared.
Definition: wifi-tx-parameters.h:62
ns3::MsduAggregator::GetSizeIfAggregated
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...
Definition: msdu-aggregator.cc:77
ns3::MsduAggregator::Deaggregate
static WifiMacQueueItem::DeaggregatedMsdus Deaggregate(Ptr< Packet > aggregatedPacket)
Definition: msdu-aggregator.cc:231
ns3::MsduAggregator
Aggregator used to construct A-MSDUs.
Definition: msdu-aggregator.h:45
ns3::AcIndex
AcIndex
This enumeration defines the Access Categories as an enumeration with values corresponding to the AC ...
Definition: qos-utils.h:71
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::MsduAggregator::SetWifiMac
void SetWifiMac(const Ptr< RegularWifiMac > mac)
Set the MAC layer to use.
Definition: msdu-aggregator.cc:69
ns3::WifiMacQueueItem::GetQueueIterator
ConstIterator GetQueueIterator(void) const
Get a const iterator pointing to the position of the MPDU in the queue.
Definition: wifi-mac-queue-item.cc:218
ns3::WIFI_MOD_CLASS_VHT
@ WIFI_MOD_CLASS_VHT
VHT (Clause 22)
Definition: wifi-phy-common.h:131
ns3::MsduAggregator::GetMaxAmsduSize
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...
Definition: msdu-aggregator.cc:166
wifi-mac-trailer.h
ns3::Object::DoDispose
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
ns3::WifiModulationClass
WifiModulationClass
This enumeration defines the modulation classes per (Table 10-6 "Modulation classes"; IEEE 802....
Definition: wifi-phy-common.h:122
ns3::MsduAggregator::MsduAggregator
MsduAggregator()
Definition: msdu-aggregator.cc:52