A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-psdu.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 Universita' degli Studi di Napoli Federico II
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Stefano Avallone <stavallo@unina.it>
7 */
8
9#include "wifi-psdu.h"
10
12#include "mpdu-aggregator.h"
13#include "wifi-mac-trailer.h"
14#include "wifi-utils.h"
15
16#include "ns3/log.h"
17#include "ns3/packet.h"
18
19namespace ns3
20{
21
22NS_LOG_COMPONENT_DEFINE("WifiPsdu");
23
25 : m_isSingle(false)
26{
27 m_mpduList.push_back(Create<WifiMpdu>(p, header));
28 m_size = header.GetSerializedSize() + p->GetSize() + WIFI_MAC_FCS_LENGTH;
29}
30
32 : m_isSingle(isSingle)
33{
34 m_mpduList.push_back(mpdu);
35 m_size = mpdu->GetSize();
36
37 if (isSingle)
38 {
39 m_size += 4; // A-MPDU Subframe header size
40 }
41}
42
43WifiPsdu::WifiPsdu(Ptr<const WifiMpdu> mpdu, bool isSingle)
44 : WifiPsdu(Create<WifiMpdu>(*mpdu), isSingle)
45{
46}
47
48WifiPsdu::WifiPsdu(std::vector<Ptr<WifiMpdu>> mpduList)
49 : m_isSingle(mpduList.size() == 1),
50 m_mpduList(mpduList)
51{
52 NS_ABORT_MSG_IF(mpduList.empty(), "Cannot initialize a WifiPsdu with an empty MPDU list");
53
54 m_size = 0;
55 for (auto& mpdu : m_mpduList)
56 {
58 }
59}
60
64
65bool
67{
68 return m_isSingle;
69}
70
71bool
73{
74 return (m_mpduList.size() > 1 || m_isSingle);
75}
76
79{
80 Ptr<Packet> packet = Create<Packet>();
81 if (m_mpduList.size() == 1 && !m_isSingle)
82 {
83 packet = m_mpduList.at(0)->GetPacket()->Copy();
84 packet->AddHeader(m_mpduList.at(0)->GetHeader());
85 AddWifiMacTrailer(packet);
86 }
87 else if (m_isSingle)
88 {
89 MpduAggregator::Aggregate(m_mpduList.at(0), packet, true);
90 }
91 else
92 {
93 for (auto& mpdu : m_mpduList)
94 {
95 MpduAggregator::Aggregate(mpdu, packet, false);
96 }
97 }
98 return packet;
99}
100
103{
104 Mac48Address ra = m_mpduList.at(0)->GetHeader().GetAddr1();
105 // check that the other MPDUs have the same RA
106 for (std::size_t i = 1; i < m_mpduList.size(); i++)
107 {
108 if (m_mpduList.at(i)->GetHeader().GetAddr1() != ra)
109 {
110 NS_ABORT_MSG("MPDUs in an A-MPDU must have the same receiver address");
111 }
112 }
113 return ra;
114}
115
118{
119 Mac48Address ta = m_mpduList.at(0)->GetHeader().GetAddr2();
120 // check that the other MPDUs have the same TA
121 for (std::size_t i = 1; i < m_mpduList.size(); i++)
122 {
123 if (m_mpduList.at(i)->GetHeader().GetAddr2() != ta)
124 {
125 NS_ABORT_MSG("MPDUs in an A-MPDU must have the same transmitter address");
126 }
127 }
128 return ta;
129}
130
131bool
133{
134 // When the contents of a received Duration/ID field, treated as an unsigned integer,
135 // are greater than 32 768, the contents are interpreted as appropriate for the frame
136 // type and subtype or ignored if the receiving MAC entity does not have a defined
137 // interpretation for that type and subtype (IEEE 802.11-2016 sec. 10.27.3)
138 return (m_mpduList.at(0)->GetHeader().GetRawDuration() & 0x8000) == 0;
139}
140
141Time
143{
144 Time duration = m_mpduList.at(0)->GetHeader().GetDuration();
145 // check that the other MPDUs have the same Duration/ID
146 for (std::size_t i = 1; i < m_mpduList.size(); i++)
147 {
148 if (m_mpduList.at(i)->GetHeader().GetDuration() != duration)
149 {
150 NS_ABORT_MSG("MPDUs in an A-AMPDU must have the same Duration/ID");
151 }
152 }
153 return duration;
154}
155
156void
158{
159 NS_LOG_FUNCTION(this << duration);
160 for (auto& mpdu : m_mpduList)
161 {
162 mpdu->GetHeader().SetDuration(duration);
163 }
164}
165
166void
168{
169 NS_LOG_FUNCTION(this);
170 for (auto& mpdu : m_mpduList)
171 {
172 mpdu->IncrementRetryCount();
173 }
174}
175
176std::set<uint8_t>
178{
179 std::set<uint8_t> s;
180 for (auto& mpdu : m_mpduList)
181 {
182 if (mpdu->GetHeader().IsQosData())
183 {
184 s.insert(mpdu->GetHeader().GetQosTid());
185 }
186 }
187 return s;
188}
189
192{
193 NS_LOG_FUNCTION(this << +tid);
195 auto it = m_mpduList.begin();
196 bool found = false;
197
198 // find the first QoS Data frame with the given TID
199 do
200 {
201 if ((*it)->GetHeader().IsQosData() && (*it)->GetHeader().GetQosTid() == tid)
202 {
203 policy = (*it)->GetHeader().GetQosAckPolicy();
204 found = true;
205 }
206 it++;
207 } while (!found && it != m_mpduList.end());
208
209 NS_ABORT_MSG_IF(!found, "No QoS Data frame in the PSDU");
210
211 // check that the other QoS Data frames with the given TID have the same ack policy
212 while (it != m_mpduList.end())
213 {
214 if ((*it)->GetHeader().IsQosData() && (*it)->GetHeader().GetQosTid() == tid &&
215 (*it)->GetHeader().GetQosAckPolicy() != policy)
216 {
217 NS_ABORT_MSG("QoS Data frames with the same TID must have the same QoS Ack Policy");
218 }
219 it++;
220 }
221 return policy;
222}
223
224void
226{
227 NS_LOG_FUNCTION(this << +tid << policy);
228 for (auto& mpdu : m_mpduList)
229 {
230 if (mpdu->GetHeader().IsQosData() && mpdu->GetHeader().GetQosTid() == tid)
231 {
232 mpdu->GetHeader().SetQosAckPolicy(policy);
233 }
234 }
235}
236
237uint16_t
238WifiPsdu::GetMaxDistFromStartingSeq(uint16_t startingSeq) const
239{
240 NS_LOG_FUNCTION(this << startingSeq);
241
242 uint16_t maxDistFromStartingSeq = 0;
243 bool foundFirst = false;
244
245 for (auto& mpdu : m_mpduList)
246 {
247 uint16_t currSeqNum = mpdu->GetHeader().GetSequenceNumber();
248
249 if (mpdu->GetHeader().IsQosData() && !QosUtilsIsOldPacket(startingSeq, currSeqNum))
250 {
251 uint16_t currDistToStartingSeq =
252 (currSeqNum - startingSeq + SEQNO_SPACE_SIZE) % SEQNO_SPACE_SIZE;
253
254 if (!foundFirst || currDistToStartingSeq > maxDistFromStartingSeq)
255 {
256 foundFirst = true;
257 maxDistFromStartingSeq = currDistToStartingSeq;
258 }
259 }
260 }
261
262 if (!foundFirst)
263 {
264 NS_LOG_DEBUG("All QoS Data frames in this PSDU are old frames");
265 return SEQNO_SPACE_SIZE;
266 }
267 NS_LOG_DEBUG("Returning " << maxDistFromStartingSeq);
268 return maxDistFromStartingSeq;
269}
270
273{
274 return m_size;
275}
276
277const WifiMacHeader&
278WifiPsdu::GetHeader(std::size_t i) const
279{
280 return m_mpduList.at(i)->GetHeader();
281}
282
285{
286 return m_mpduList.at(i)->GetHeader();
287}
288
290WifiPsdu::GetPayload(std::size_t i) const
291{
292 return m_mpduList.at(i)->GetPacket();
293}
294
296WifiPsdu::GetAmpduSubframe(std::size_t i) const
297{
298 NS_ASSERT(i < m_mpduList.size());
299 Ptr<Packet> subframe = m_mpduList.at(i)->GetProtocolDataUnit();
300 subframe->AddHeader(
301 MpduAggregator::GetAmpduSubframeHeader(static_cast<uint16_t>(subframe->GetSize()),
302 m_isSingle));
303 size_t padding = GetAmpduSubframeSize(i) - subframe->GetSize();
304 if (padding > 0)
305 {
306 Ptr<Packet> pad = Create<Packet>(padding);
307 subframe->AddAtEnd(pad);
308 }
309 return subframe;
310}
311
312std::size_t
314{
315 NS_ASSERT(i < m_mpduList.size());
316 size_t subframeSize = 4; // A-MPDU Subframe header size
317 subframeSize += m_mpduList.at(i)->GetSize();
318 if (i != m_mpduList.size() - 1) // add padding if not last
319 {
320 subframeSize += MpduAggregator::CalculatePadding(subframeSize);
321 }
322 return subframeSize;
323}
324
325std::size_t
327{
328 return m_mpduList.size();
329}
330
331std::vector<Ptr<WifiMpdu>>::const_iterator
333{
334 return m_mpduList.begin();
335}
336
337std::vector<Ptr<WifiMpdu>>::iterator
339{
340 return m_mpduList.begin();
341}
342
343std::vector<Ptr<WifiMpdu>>::const_iterator
345{
346 return m_mpduList.end();
347}
348
349std::vector<Ptr<WifiMpdu>>::iterator
351{
352 return m_mpduList.end();
353}
354
355void
356WifiPsdu::Print(std::ostream& os) const
357{
358 os << "size=" << m_size;
359 if (IsAggregate())
360 {
361 os << ", A-MPDU of " << GetNMpdus() << " MPDUs";
362 for (const auto& mpdu : m_mpduList)
363 {
364 os << " (" << *mpdu << ")";
365 }
366 }
367 else
368 {
369 os << ", " << ((m_isSingle) ? "S-MPDU" : "normal MPDU") << " (" << *(m_mpduList.at(0))
370 << ")";
371 }
372}
373
374std::ostream&
375operator<<(std::ostream& os, const WifiPsdu& psdu)
376{
377 psdu.Print(os);
378 return os;
379}
380
381} // namespace ns3
an EUI-48 address
static uint8_t CalculatePadding(uint32_t ampduSize)
static void Aggregate(Ptr< const WifiMpdu > 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.
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...
Smart pointer class similar to boost::intrusive_ptr.
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
Implements the IEEE 802.11 MAC header.
uint32_t GetSerializedSize() const override
QosAckPolicy
Ack policy for QoS frames.
WifiMpdu stores a (const) packet along with a MAC header.
Definition wifi-mpdu.h:51
WifiPsdu stores an MPDU, S-MPDU or A-MPDU, by keeping header(s) and payload(s) separate for each cons...
Definition wifi-psdu.h:32
std::set< uint8_t > GetTids() const
Get the set of TIDs of the QoS Data frames included in the PSDU.
Definition wifi-psdu.cc:177
void SetAckPolicyForTid(uint8_t tid, WifiMacHeader::QosAckPolicy policy)
Set the QoS Ack Policy of the QoS Data frames included in the PSDU that have the given TID to the giv...
Definition wifi-psdu.cc:225
const WifiMacHeader & GetHeader(std::size_t i) const
Get the header of the i-th MPDU.
Definition wifi-psdu.cc:278
void Print(std::ostream &os) const
Print the PSDU contents.
Definition wifi-psdu.cc:356
Time GetDuration() const
Get the duration from the Duration/ID field, which is common to all the MPDUs.
Definition wifi-psdu.cc:142
Ptr< const Packet > GetPacket() const
Get the PSDU as a single packet.
Definition wifi-psdu.cc:78
Ptr< Packet > GetAmpduSubframe(std::size_t i) const
Get a copy of the i-th A-MPDU subframe (includes subframe header, MPDU, and possibly padding)
Definition wifi-psdu.cc:296
std::vector< Ptr< WifiMpdu > >::const_iterator end() const
Return a const iterator to past-the-last MPDU.
Definition wifi-psdu.cc:344
std::vector< Ptr< WifiMpdu > >::const_iterator begin() const
Return a const iterator to the first MPDU.
Definition wifi-psdu.cc:332
WifiPsdu(Ptr< const Packet > p, const WifiMacHeader &header)
Create a PSDU storing an MPDU.
Definition wifi-psdu.cc:24
Mac48Address GetAddr2() const
Get the Transmitter Address (TA), which is common to all the MPDUs.
Definition wifi-psdu.cc:117
bool HasNav() const
Definition wifi-psdu.cc:132
uint32_t m_size
the size of the PSDU in bytes
Definition wifi-psdu.h:250
virtual ~WifiPsdu()
Definition wifi-psdu.cc:61
uint32_t GetSize() const
Return the size of the PSDU in bytes.
Definition wifi-psdu.cc:272
Ptr< const Packet > GetPayload(std::size_t i) const
Get the payload of the i-th MPDU.
Definition wifi-psdu.cc:290
std::size_t GetAmpduSubframeSize(std::size_t i) const
Return the size of the i-th A-MPDU subframe.
Definition wifi-psdu.cc:313
bool m_isSingle
true for an S-MPDU
Definition wifi-psdu.h:248
Mac48Address GetAddr1() const
Get the Receiver Address (RA), which is common to all the MPDUs.
Definition wifi-psdu.cc:102
bool IsAggregate() const
Return true if the PSDU is an S-MPDU or A-MPDU.
Definition wifi-psdu.cc:72
bool IsSingle() const
Return true if the PSDU is an S-MPDU.
Definition wifi-psdu.cc:66
void SetDuration(Time duration)
Set the Duration/ID field on all the MPDUs.
Definition wifi-psdu.cc:157
void IncrementRetryCount()
Increment the frame retry count for all the MPDUs.
Definition wifi-psdu.cc:167
std::vector< Ptr< WifiMpdu > > m_mpduList
list of constituent MPDUs
Definition wifi-psdu.h:249
uint16_t GetMaxDistFromStartingSeq(uint16_t startingSeq) const
Get the maximum distance between the sequence number of any QoS Data frame included in this PSDU that...
Definition wifi-psdu.cc:238
std::size_t GetNMpdus() const
Return the number of MPDUs constituting the PSDU.
Definition wifi-psdu.cc:326
WifiMacHeader::QosAckPolicy GetAckPolicyForTid(uint8_t tid) const
Get the QoS Ack Policy of the QoS Data frames included in the PSDU that have the given TID.
Definition wifi-psdu.cc:191
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition abort.h:97
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
bool QosUtilsIsOldPacket(uint16_t startingSeq, uint16_t seqNumber)
This function checks if packet with sequence number seqNumber is an "old" packet.
Definition qos-utils.cc:156
Every class exported by the ns3 library is enclosed in the ns3 namespace.
static const uint16_t WIFI_MAC_FCS_LENGTH
The length in octets of the IEEE 802.11 MAC FCS field.
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition angles.cc:148
static constexpr uint16_t SEQNO_SPACE_SIZE
Size of the space of sequence numbers.
Definition wifi-utils.h:185
void AddWifiMacTrailer(Ptr< Packet > packet)
Add FCS trailer to a packet.