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 "wifi-mac-queue-item.h"
30 #include "wifi-mac-queue.h"
31 #include "msdu-aggregator.h"
32 #include "wifi-net-device.h"
33 #include "ns3/ht-capabilities.h"
34 #include "ns3/vht-capabilities.h"
35 #include "ns3/he-capabilities.h"
36 #include "wifi-mac.h"
37 #include "qos-txop.h"
38 #include "ctrl-headers.h"
39 #include "wifi-mac-trailer.h"
40 #include "wifi-tx-parameters.h"
41 
42 NS_LOG_COMPONENT_DEFINE ("MpduAggregator");
43 
44 namespace ns3 {
45 
46 NS_OBJECT_ENSURE_REGISTERED (MpduAggregator);
47 
48 TypeId
50 {
51  static TypeId tid = TypeId ("ns3::MpduAggregator")
52  .SetParent<Object> ()
53  .SetGroupName ("Wifi")
54  .AddConstructor<MpduAggregator> ()
55  ;
56  return tid;
57 }
58 
60 {
61 }
62 
64 {
65 }
66 
67 void
69 {
70  m_mac = 0;
72 }
73 
74 void
76 {
77  NS_LOG_FUNCTION (this << mac);
78  m_mac = mac;
79 }
80 
81 void
83 {
84  NS_LOG_FUNCTION (mpdu << ampdu << isSingle);
85  NS_ASSERT (ampdu);
86  // if isSingle is true, then ampdu must be empty
87  NS_ASSERT (!isSingle || ampdu->GetSize () == 0);
88 
89  // pad the previous A-MPDU subframe if the A-MPDU is not empty
90  if (ampdu->GetSize () > 0)
91  {
92  uint8_t padding = CalculatePadding (ampdu->GetSize ());
93 
94  if (padding)
95  {
96  Ptr<Packet> pad = Create<Packet> (padding);
97  ampdu->AddAtEnd (pad);
98  }
99  }
100 
101  // add MPDU header and trailer
102  Ptr<Packet> tmp = mpdu->GetPacket ()->Copy ();
103  tmp->AddHeader (mpdu->GetHeader ());
104  AddWifiMacTrailer (tmp);
105 
106  // add A-MPDU subframe header and MPDU to the A-MPDU
107  AmpduSubframeHeader hdr = GetAmpduSubframeHeader (static_cast<uint16_t> (tmp->GetSize ()), isSingle);
108 
109  tmp->AddHeader (hdr);
110  ampdu->AddAtEnd (tmp);
111 }
112 
113 uint32_t
114 MpduAggregator::GetSizeIfAggregated (uint32_t mpduSize, uint32_t ampduSize)
115 {
116  NS_LOG_FUNCTION (mpduSize << ampduSize);
117 
118  return ampduSize + CalculatePadding (ampduSize) + 4 + mpduSize;
119 }
120 
121 uint32_t
123  WifiModulationClass modulation) const
124 {
125  NS_LOG_FUNCTION (this << recipient << +tid << modulation);
126 
127  AcIndex ac = QosUtilsMapTidToAc (tid);
128 
129  // Find the A-MPDU max size configured on this device
130  uint32_t maxAmpduSize = m_mac->GetMaxAmpduSize (ac);
131 
132  if (maxAmpduSize == 0)
133  {
134  NS_LOG_DEBUG ("A-MPDU Aggregation is disabled on this station for AC " << ac);
135  return 0;
136  }
137 
138  Ptr<WifiRemoteStationManager> stationManager = m_mac->GetWifiRemoteStationManager ();
139  NS_ASSERT (stationManager);
140 
141  // Retrieve the Capabilities elements advertised by the recipient
142  Ptr<const HeCapabilities> heCapabilities = stationManager->GetStationHeCapabilities (recipient);
143  Ptr<const VhtCapabilities> vhtCapabilities = stationManager->GetStationVhtCapabilities (recipient);
144  Ptr<const HtCapabilities> htCapabilities = stationManager->GetStationHtCapabilities (recipient);
145 
146  // Determine the constraint imposed by the recipient based on the PPDU
147  // format used to transmit the A-MPDU
148  if (modulation == WIFI_MOD_CLASS_HE)
149  {
150  NS_ABORT_MSG_IF (!heCapabilities, "HE Capabilities element not received");
151 
152  maxAmpduSize = std::min (maxAmpduSize, heCapabilities->GetMaxAmpduLength ());
153  }
154  else if (modulation == WIFI_MOD_CLASS_VHT)
155  {
156  NS_ABORT_MSG_IF (!vhtCapabilities, "VHT Capabilities element not received");
157 
158  maxAmpduSize = std::min (maxAmpduSize, vhtCapabilities->GetMaxAmpduLength ());
159  }
160  else if (modulation == WIFI_MOD_CLASS_HT)
161  {
162  NS_ABORT_MSG_IF (!htCapabilities, "HT Capabilities element not received");
163 
164  maxAmpduSize = std::min (maxAmpduSize, htCapabilities->GetMaxAmpduLength ());
165  }
166  else // non-HT PPDU
167  {
168  NS_LOG_DEBUG ("A-MPDU aggregation is not available for non-HT PHYs");
169 
170  maxAmpduSize = 0;
171  }
172 
173  return maxAmpduSize;
174 }
175 
176 uint8_t
178 {
179  return (4 - (ampduSize % 4 )) % 4;
180 }
181 
183 MpduAggregator::GetAmpduSubframeHeader (uint16_t mpduSize, bool isSingle)
184 {
186  hdr.SetLength (mpduSize);
187  if (isSingle)
188  {
189  hdr.SetEof (1);
190  }
191  return hdr;
192 }
193 
194 std::vector<Ptr<WifiMacQueueItem>>
196  Time availableTime) const
197 {
198  NS_LOG_FUNCTION (this << *mpdu << &txParams << availableTime);
199 
200  std::vector<Ptr<WifiMacQueueItem>> mpduList;
201 
202  Mac48Address recipient = mpdu->GetHeader ().GetAddr1 ();
203  NS_ASSERT (mpdu->GetHeader ().IsQosData () && !recipient.IsBroadcast ());
204  uint8_t tid = mpdu->GetHeader ().GetQosTid ();
205 
206  Ptr<QosTxop> qosTxop = m_mac->GetQosTxop (tid);
207  NS_ASSERT (qosTxop != 0);
208 
209  //Have to make sure that the block ack agreement is established and A-MPDU is enabled
210  if (qosTxop->GetBaAgreementEstablished (recipient, tid)
211  && GetMaxAmpduSize (recipient, tid, txParams.m_txVector.GetModulationClass ()) > 0)
212  {
213  /* here is performed MPDU aggregation */
214  Ptr<WifiMacQueueItem> nextMpdu = mpdu;
215 
216  while (nextMpdu != 0)
217  {
218  // if we are here, nextMpdu can be aggregated to the A-MPDU.
219  NS_LOG_DEBUG ("Adding packet with sequence number " << nextMpdu->GetHeader ().GetSequenceNumber ()
220  << " to A-MPDU, packet size = " << nextMpdu->GetSize ()
221  << ", A-MPDU size = " << txParams.GetSize (recipient));
222 
223  mpduList.push_back (nextMpdu);
224 
225  // If allowed by the BA agreement, get the next MPDU
226  Ptr<const WifiMacQueueItem> peekedMpdu;
227  peekedMpdu = qosTxop->PeekNextMpdu (tid, recipient, nextMpdu);
228  nextMpdu = 0;
229 
230  if (peekedMpdu != 0)
231  {
232  // PeekNextMpdu() does not return an MPDU that is beyond the transmit window
233  NS_ASSERT (IsInWindow (peekedMpdu->GetHeader ().GetSequenceNumber (),
234  qosTxop->GetBaStartingSequence (recipient, tid),
235  qosTxop->GetBaBufferSize (recipient, tid)));
236 
237  // get the next MPDU to aggregate, provided that the constraints on size
238  // and duration limit are met. Note that the returned MPDU differs from
239  // the peeked MPDU if A-MSDU aggregation is enabled.
240  NS_LOG_DEBUG ("Trying to aggregate another MPDU");
241  nextMpdu = qosTxop->GetNextMpdu (peekedMpdu, txParams, availableTime, false);
242  }
243  }
244 
245  if (mpduList.size () == 1)
246  {
247  // return an empty vector if it was not possible to aggregate at least two MPDUs
248  mpduList.clear ();
249  }
250  }
251 
252  return mpduList;
253 }
254 
255 } //namespace ns3
#define min(a, b)
Definition: 80211b.c:42
Headers for A-MPDU subframes.
void SetEof(bool eof)
Set the EOF field.
void SetLength(uint16_t length)
Set the length field.
an EUI-48 address
Definition: mac48-address.h:44
bool IsBroadcast(void) const
Aggregator used to construct A-MPDUs.
static uint8_t CalculatePadding(uint32_t ampduSize)
void DoDispose() override
Destructor implementation.
Ptr< WifiMac > m_mac
the MAC of this station
static TypeId GetTypeId(void)
Get the type ID.
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...
static void Aggregate(Ptr< const WifiMacQueueItem > mpdu, Ptr< Packet > ampdu, bool isSingle)
Aggregate an MPDU to an A-MPDU.
static AmpduSubframeHeader GetAmpduSubframeHeader(uint16_t mpduSize, bool isSingle)
Get the A-MPDU subframe header corresponding to the MPDU size and whether the MPDU is a single MPDU.
void SetWifiMac(const Ptr< WifiMac > mac)
Set the MAC layer to use.
std::vector< Ptr< WifiMacQueueItem > > GetNextAmpdu(Ptr< WifiMacQueueItem > mpdu, WifiTxParameters &txParams, Time availableTime) const
Attempt to aggregate other MPDUs to the given MPDU, while meeting the following constraints:
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...
A base class which provides memory management and object aggregation.
Definition: object.h:88
virtual void DoDispose(void)
Destructor implementation.
Definition: object.cc:346
void AddAtEnd(Ptr< const Packet > packet)
Concatenate the input packet at the end of the current packet.
Definition: packet.cc:335
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
uint16_t GetBaBufferSize(Mac48Address address, uint8_t tid) const
Definition: qos-txop.cc:255
bool GetBaAgreementEstablished(Mac48Address address, uint8_t tid) const
Definition: qos-txop.cc:249
Ptr< WifiMacQueueItem > GetNextMpdu(Ptr< const WifiMacQueueItem > peekedItem, WifiTxParameters &txParams, Time availableTime, bool initialFrame)
Prepare the frame to transmit starting from the MPDU that has been previously peeked by calling PeekN...
Definition: qos-txop.cc:443
uint16_t GetBaStartingSequence(Mac48Address address, uint8_t tid) const
Definition: qos-txop.cc:261
Ptr< const WifiMacQueueItem > PeekNextMpdu(uint8_t tid=8, Mac48Address recipient=Mac48Address::GetBroadcast(), Ptr< const WifiMacQueueItem > item=nullptr)
Peek the next frame to transmit to the given receiver and of the given TID from the EDCA queue.
Definition: qos-txop.cc:357
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Ptr< const HtCapabilities > GetStationHtCapabilities(Mac48Address from)
Return the HT capabilities sent by the remote station.
Ptr< const HeCapabilities > GetStationHeCapabilities(Mac48Address from)
Return the HE capabilities sent by the remote station.
Ptr< const VhtCapabilities > GetStationVhtCapabilities(Mac48Address from)
Return the VHT capabilities sent by the remote station.
This class stores the TX parameters (TX vector, protection mechanism, acknowledgment mechanism,...
uint32_t GetSize(Mac48Address receiver) const
Get the size in bytes of the (A-)MPDU addressed to the given receiver.
WifiTxVector m_txVector
TXVECTOR of the frame being prepared.
WifiModulationClass GetModulationClass(void) const
Get the modulation class specified by this TXVECTOR.
#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_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
AcIndex QosUtilsMapTidToAc(uint8_t tid)
Maps TID (Traffic ID) to Access classes.
Definition: qos-utils.cc:126
WifiModulationClass
This enumeration defines the modulation classes per (Table 10-6 "Modulation classes"; IEEE 802....
AcIndex
This enumeration defines the Access Categories as an enumeration with values corresponding to the AC ...
Definition: qos-utils.h:71
@ WIFI_MOD_CLASS_HT
HT (Clause 19)
@ WIFI_MOD_CLASS_VHT
VHT (Clause 22)
@ WIFI_MOD_CLASS_HE
HE (Clause 27)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void AddWifiMacTrailer(Ptr< Packet > packet)
Add FCS trailer to a packet.
Definition: wifi-utils.cc:122
bool IsInWindow(uint16_t seq, uint16_t winstart, uint16_t winsize)
Definition: wifi-utils.cc:116
mac
Definition: third.py:99