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/packet.h"
23 #include "mpdu-aggregator.h"
24 #include "ampdu-subframe-header.h"
25 
26 NS_LOG_COMPONENT_DEFINE ("MpduAggregator");
27 
28 namespace ns3 {
29 
30 NS_OBJECT_ENSURE_REGISTERED (MpduAggregator);
31 
32 TypeId
34 {
35  static TypeId tid = TypeId ("ns3::MpduAggregator")
36  .SetParent<Object> ()
37  .SetGroupName ("Wifi")
38  .AddConstructor<MpduAggregator> ()
39  ;
40  return tid;
41 }
42 
44 {
45 }
46 
48 {
49 }
50 
51 void
53 {
54  m_maxAmpduLength = maxSize;
55 }
56 
57 uint16_t
59 {
60  return m_maxAmpduLength;
61 }
62 
63 bool
65 {
66  NS_LOG_FUNCTION (this);
67  Ptr<Packet> currentPacket;
68  AmpduSubframeHeader currentHdr;
69 
70  uint8_t padding = CalculatePadding (aggregatedPacket);
71  uint32_t actualSize = aggregatedPacket->GetSize ();
72 
73  if ((4 + packet->GetSize () + actualSize + padding) <= GetMaxAmpduSize ())
74  {
75  if (padding)
76  {
77  Ptr<Packet> pad = Create<Packet> (padding);
78  aggregatedPacket->AddAtEnd (pad);
79  }
80  currentHdr.SetLength (static_cast<uint16_t> (packet->GetSize ()));
81  currentPacket = packet->Copy ();
82 
83  currentPacket->AddHeader (currentHdr);
84  aggregatedPacket->AddAtEnd (currentPacket);
85  return true;
86  }
87  return false;
88 }
89 
90 void
92 {
93  NS_LOG_FUNCTION (this);
94  Ptr<Packet> currentPacket;
95  AmpduSubframeHeader currentHdr;
96 
97  uint8_t padding = CalculatePadding (aggregatedPacket);
98  if (padding)
99  {
100  Ptr<Packet> pad = Create<Packet> (padding);
101  aggregatedPacket->AddAtEnd (pad);
102  }
103 
104  currentHdr.SetEof (1);
105  currentHdr.SetLength (static_cast<uint16_t> (packet->GetSize ()));
106  currentPacket = packet->Copy ();
107 
108  currentPacket->AddHeader (currentHdr);
109  aggregatedPacket->AddAtEnd (currentPacket);
110 }
111 
112 void
113 MpduAggregator::AddHeaderAndPad (Ptr<Packet> packet, bool last, bool isSingleMpdu) const
114 {
115  NS_LOG_FUNCTION (this);
116  AmpduSubframeHeader currentHdr;
117 
118  //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
119  //done before when deciding how many packets to add to the queue
120  currentHdr.SetLength (static_cast<uint16_t> (packet->GetSize ()));
121  if (isSingleMpdu)
122  {
123  currentHdr.SetEof (1);
124  }
125 
126  packet->AddHeader (currentHdr);
127  uint32_t padding = CalculatePadding (packet);
128 
129  if (padding && !last)
130  {
131  Ptr<Packet> pad = Create<Packet> (padding);
132  packet->AddAtEnd (pad);
133  }
134 }
135 
136 bool
137 MpduAggregator::CanBeAggregated (uint32_t packetSize, Ptr<Packet> aggregatedPacket, uint8_t blockAckSize) const
138 {
139  uint8_t padding = CalculatePadding (aggregatedPacket);
140  uint32_t actualSize = aggregatedPacket->GetSize ();
141  if (blockAckSize > 0)
142  {
143  blockAckSize = blockAckSize + 4 + padding;
144  }
145  if ((4 + packetSize + actualSize + padding + blockAckSize) <= GetMaxAmpduSize ())
146  {
147  return true;
148  }
149  else
150  {
151  return false;
152  }
153 }
154 
155 uint8_t
157 {
158  return (4 - (packet->GetSize () % 4 )) % 4;
159 }
160 
163 {
165  DeaggregatedMpdus set;
166 
168  Ptr<Packet> extractedMpdu = Create<Packet> ();
169  uint32_t maxSize = aggregatedPacket->GetSize ();
170  uint16_t extractedLength;
171  uint32_t padding;
172  uint32_t deserialized = 0;
173 
174  while (deserialized < maxSize)
175  {
176  deserialized += aggregatedPacket->RemoveHeader (hdr);
177  extractedLength = hdr.GetLength ();
178  extractedMpdu = aggregatedPacket->CreateFragment (0, static_cast<uint32_t> (extractedLength));
179  aggregatedPacket->RemoveAtStart (extractedLength);
180  deserialized += extractedLength;
181 
182  padding = (4 - (extractedLength % 4 )) % 4;
183 
184  if (padding > 0 && deserialized < maxSize)
185  {
186  aggregatedPacket->RemoveAtStart (padding);
187  deserialized += padding;
188  }
189 
190  std::pair<Ptr<Packet>, AmpduSubframeHeader> packetHdr (extractedMpdu, hdr);
191  set.push_back (packetHdr);
192  }
193  NS_LOG_INFO ("Deaggreated A-MPDU: extracted " << set.size () << " MPDUs");
194  return set;
195 }
196 
197 } //namespace ns3
Aggregator used to construct A-MPDUs.
uint32_t RemoveHeader(Header &header)
Deserialize and remove the header from the internal buffer.
Definition: packet.cc:280
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
bool Aggregate(Ptr< const Packet > packet, Ptr< Packet > aggregatedPacket) const
#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:831
uint8_t CalculatePadding(Ptr< const Packet > packet) const
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_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
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:278
#define NS_LOG_FUNCTION_NOARGS()
Output the name of the function.
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 AggregateSingleMpdu(Ptr< const Packet > packet, Ptr< Packet > aggregatedPacket) const
Introspection did not find any typical Config paths.
uint16_t GetMaxAmpduSize(void) const
Returns the maximum A-MPDU size in bytes.
void AddHeaderAndPad(Ptr< Packet > packet, bool last, bool isSingleMpdu) const
void SetEof(bool eof)
Set the EOF field.
void SetLength(uint16_t length)
Set the length field.
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
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 GetLength(void) const
Return the length field.
std::list< std::pair< Ptr< Packet >, AmpduSubframeHeader > > DeaggregatedMpdus
A list of deaggregated packets and their A-MPDU subframe headers.
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:915
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.