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  */
20 
21 #include "ns3/log.h"
22 #include "ns3/uinteger.h"
23 #include "mpdu-aggregator.h"
24 
25 NS_LOG_COMPONENT_DEFINE ("MpduAggregator");
26 
27 namespace ns3 {
28 
29 NS_OBJECT_ENSURE_REGISTERED (MpduAggregator);
30 
31 TypeId
33 {
34  static TypeId tid = TypeId ("ns3::MpduAggregator")
35  .SetParent<Object> ()
36  .SetGroupName ("Wifi")
37  .AddConstructor<MpduAggregator> ()
38  ;
39  return tid;
40 }
41 
43 {
44 }
45 
47 {
48 }
49 
50 void
52 {
53  m_maxAmpduLength = maxSize;
54 }
55 
56 uint16_t
58 {
59  return m_maxAmpduLength;
60 }
61 
62 bool
64 {
65  NS_LOG_FUNCTION (this);
66  Ptr<Packet> currentPacket;
67  AmpduSubframeHeader currentHdr;
68 
69  uint8_t padding = CalculatePadding (aggregatedPacket);
70  uint32_t actualSize = aggregatedPacket->GetSize ();
71 
72  if ((4 + packet->GetSize () + actualSize + padding) <= m_maxAmpduLength)
73  {
74  if (padding)
75  {
76  Ptr<Packet> pad = Create<Packet> (padding);
77  aggregatedPacket->AddAtEnd (pad);
78  }
79  currentHdr.SetCrc (1);
80  currentHdr.SetSig ();
81  currentHdr.SetLength (packet->GetSize ());
82  currentPacket = packet->Copy ();
83 
84  currentPacket->AddHeader (currentHdr);
85  aggregatedPacket->AddAtEnd (currentPacket);
86  return true;
87  }
88  return false;
89 }
90 
91 void
93 {
94  NS_LOG_FUNCTION (this);
95  Ptr<Packet> currentPacket;
96  AmpduSubframeHeader currentHdr;
97 
98  uint8_t padding = CalculatePadding (aggregatedPacket);
99  if (padding)
100  {
101  Ptr<Packet> pad = Create<Packet> (padding);
102  aggregatedPacket->AddAtEnd (pad);
103  }
104 
105  currentHdr.SetEof (1);
106  currentHdr.SetCrc (1);
107  currentHdr.SetSig ();
108  currentHdr.SetLength (packet->GetSize ());
109  currentPacket = packet->Copy ();
110 
111  currentPacket->AddHeader (currentHdr);
112  aggregatedPacket->AddAtEnd (currentPacket);
113 }
114 
115 void
116 MpduAggregator::AddHeaderAndPad (Ptr<Packet> packet, bool last, bool isSingleMpdu) const
117 {
118  NS_LOG_FUNCTION (this);
119  AmpduSubframeHeader currentHdr;
120 
121  //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
122  //done before when deciding how many packets to add to the queue
123  currentHdr.SetCrc (1);
124  currentHdr.SetSig ();
125  currentHdr.SetLength (packet->GetSize ());
126  if (isSingleMpdu)
127  {
128  currentHdr.SetEof (1);
129  }
130 
131  packet->AddHeader (currentHdr);
132  uint32_t padding = CalculatePadding (packet);
133 
134  if (padding && !last)
135  {
136  Ptr<Packet> pad = Create<Packet> (padding);
137  packet->AddAtEnd (pad);
138  }
139 }
140 
141 bool
142 MpduAggregator::CanBeAggregated (uint32_t packetSize, Ptr<Packet> aggregatedPacket, uint8_t blockAckSize) const
143 {
144  uint8_t padding = CalculatePadding (aggregatedPacket);
145  uint32_t actualSize = aggregatedPacket->GetSize ();
146  if (blockAckSize > 0)
147  {
148  blockAckSize = blockAckSize + 4 + padding;
149  }
150  if ((4 + packetSize + actualSize + padding + blockAckSize) <= m_maxAmpduLength)
151  {
152  return true;
153  }
154  else
155  {
156  return false;
157  }
158 }
159 
160 uint8_t
162 {
163  return (4 - (packet->GetSize () % 4 )) % 4;
164 }
165 
168 {
170  DeaggregatedMpdus set;
171 
173  Ptr<Packet> extractedMpdu = Create<Packet> ();
174  uint32_t maxSize = aggregatedPacket->GetSize ();
175  uint16_t extractedLength;
176  uint32_t padding;
177  uint32_t deserialized = 0;
178 
179  while (deserialized < maxSize)
180  {
181  deserialized += aggregatedPacket->RemoveHeader (hdr);
182  extractedLength = hdr.GetLength ();
183  extractedMpdu = aggregatedPacket->CreateFragment (0, static_cast<uint32_t> (extractedLength));
184  aggregatedPacket->RemoveAtStart (extractedLength);
185  deserialized += extractedLength;
186 
187  padding = (4 - (extractedLength % 4 )) % 4;
188 
189  if (padding > 0 && deserialized < maxSize)
190  {
191  aggregatedPacket->RemoveAtStart (padding);
192  deserialized += padding;
193  }
194 
195  std::pair<Ptr<Packet>, AmpduSubframeHeader> packetHdr (extractedMpdu, hdr);
196  set.push_back (packetHdr);
197  }
198  NS_LOG_INFO ("Deaggreated A-MPDU: extracted " << set.size () << " MPDUs");
199  return set;
200 }
201 
202 } //namespace ns3
uint16_t GetLength(void) const
Return the length field.
Aggregator used to construct A-MPDUs.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
void SetSig()
Set the SIG field.
#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
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:821
static DeaggregatedMpdus Deaggregate(Ptr< Packet > aggregatedPacket)
Deaggregates an A-MPDU by removing the A-MPDU subframe header and padding.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:277
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
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
void AggregateSingleMpdu(Ptr< const Packet > packet, Ptr< Packet > aggregatedPacket) const
bool Aggregate(Ptr< const Packet > packet, Ptr< Packet > aggregatedPacket) const
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
void SetCrc(uint8_t crc)
Set the CRC field.
void AddHeaderAndPad(Ptr< Packet > packet, bool last, bool isSingleMpdu) const
Introspection did not find any typical Config paths.
void SetEof(bool eof)
Set the EOF field.
Ptr< Packet > Copy(void) const
performs a COW copy of the packet.
Definition: packet.cc:121
void SetLength(uint16_t length)
Set the length field.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetMaxAmpduSize(uint16_t maxSize)
Sets the maximum A-MPDU size in bytes.
bool CanBeAggregated(uint32_t packetSize, Ptr< Packet > aggregatedPacket, uint8_t blockAckSize) const
static TypeId GetTypeId(void)
Get the type ID.
uint16_t GetMaxAmpduSize(void) const
Returns the maximum A-MPDU size in bytes.
std::list< std::pair< Ptr< Packet >, AmpduSubframeHeader > > DeaggregatedMpdus
A list of deaggregated packets and their A-MPDU subframe headers.
uint8_t CalculatePadding(Ptr< const Packet > packet) const
static const uint32_t packetSize
A base class which provides memory management and object aggregation.
Definition: object.h:87
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:914
void AddHeader(const Header &header)
Add header to this packet.
Definition: packet.cc:256
uint16_t m_maxAmpduLength
Maximum length in bytes of A-MPDUs.