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-AMPDU 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-AMPDU 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
166std::set<uint8_t>
168{
169 std::set<uint8_t> s;
170 for (auto& mpdu : m_mpduList)
171 {
172 if (mpdu->GetHeader().IsQosData())
173 {
174 s.insert(mpdu->GetHeader().GetQosTid());
175 }
176 }
177 return s;
178}
179
182{
183 NS_LOG_FUNCTION(this << +tid);
185 auto it = m_mpduList.begin();
186 bool found = false;
187
188 // find the first QoS Data frame with the given TID
189 do
190 {
191 if ((*it)->GetHeader().IsQosData() && (*it)->GetHeader().GetQosTid() == tid)
192 {
193 policy = (*it)->GetHeader().GetQosAckPolicy();
194 found = true;
195 }
196 it++;
197 } while (!found && it != m_mpduList.end());
198
199 NS_ABORT_MSG_IF(!found, "No QoS Data frame in the PSDU");
200
201 // check that the other QoS Data frames with the given TID have the same ack policy
202 while (it != m_mpduList.end())
203 {
204 if ((*it)->GetHeader().IsQosData() && (*it)->GetHeader().GetQosTid() == tid &&
205 (*it)->GetHeader().GetQosAckPolicy() != policy)
206 {
207 NS_ABORT_MSG("QoS Data frames with the same TID must have the same QoS Ack Policy");
208 }
209 it++;
210 }
211 return policy;
212}
213
214void
216{
217 NS_LOG_FUNCTION(this << +tid << policy);
218 for (auto& mpdu : m_mpduList)
219 {
220 if (mpdu->GetHeader().IsQosData() && mpdu->GetHeader().GetQosTid() == tid)
221 {
222 mpdu->GetHeader().SetQosAckPolicy(policy);
223 }
224 }
225}
226
227uint16_t
228WifiPsdu::GetMaxDistFromStartingSeq(uint16_t startingSeq) const
229{
230 NS_LOG_FUNCTION(this << startingSeq);
231
232 uint16_t maxDistFromStartingSeq = 0;
233 bool foundFirst = false;
234
235 for (auto& mpdu : m_mpduList)
236 {
237 uint16_t currSeqNum = mpdu->GetHeader().GetSequenceNumber();
238
239 if (mpdu->GetHeader().IsQosData() && !QosUtilsIsOldPacket(startingSeq, currSeqNum))
240 {
241 uint16_t currDistToStartingSeq =
242 (currSeqNum - startingSeq + SEQNO_SPACE_SIZE) % SEQNO_SPACE_SIZE;
243
244 if (!foundFirst || currDistToStartingSeq > maxDistFromStartingSeq)
245 {
246 foundFirst = true;
247 maxDistFromStartingSeq = currDistToStartingSeq;
248 }
249 }
250 }
251
252 if (!foundFirst)
253 {
254 NS_LOG_DEBUG("All QoS Data frames in this PSDU are old frames");
255 return SEQNO_SPACE_SIZE;
256 }
257 NS_LOG_DEBUG("Returning " << maxDistFromStartingSeq);
258 return maxDistFromStartingSeq;
259}
260
263{
264 return m_size;
265}
266
267const WifiMacHeader&
268WifiPsdu::GetHeader(std::size_t i) const
269{
270 return m_mpduList.at(i)->GetHeader();
271}
272
275{
276 return m_mpduList.at(i)->GetHeader();
277}
278
280WifiPsdu::GetPayload(std::size_t i) const
281{
282 return m_mpduList.at(i)->GetPacket();
283}
284
286WifiPsdu::GetAmpduSubframe(std::size_t i) const
287{
288 NS_ASSERT(i < m_mpduList.size());
289 Ptr<Packet> subframe = m_mpduList.at(i)->GetProtocolDataUnit();
290 subframe->AddHeader(
291 MpduAggregator::GetAmpduSubframeHeader(static_cast<uint16_t>(subframe->GetSize()),
292 m_isSingle));
293 size_t padding = GetAmpduSubframeSize(i) - subframe->GetSize();
294 if (padding > 0)
295 {
296 Ptr<Packet> pad = Create<Packet>(padding);
297 subframe->AddAtEnd(pad);
298 }
299 return subframe;
300}
301
302std::size_t
304{
305 NS_ASSERT(i < m_mpduList.size());
306 size_t subframeSize = 4; // A-MPDU Subframe header size
307 subframeSize += m_mpduList.at(i)->GetSize();
308 if (i != m_mpduList.size() - 1) // add padding if not last
309 {
310 subframeSize += MpduAggregator::CalculatePadding(subframeSize);
311 }
312 return subframeSize;
313}
314
315std::size_t
317{
318 return m_mpduList.size();
319}
320
321std::vector<Ptr<WifiMpdu>>::const_iterator
323{
324 return m_mpduList.begin();
325}
326
327std::vector<Ptr<WifiMpdu>>::iterator
329{
330 return m_mpduList.begin();
331}
332
333std::vector<Ptr<WifiMpdu>>::const_iterator
335{
336 return m_mpduList.end();
337}
338
339std::vector<Ptr<WifiMpdu>>::iterator
341{
342 return m_mpduList.end();
343}
344
345void
346WifiPsdu::Print(std::ostream& os) const
347{
348 os << "size=" << m_size;
349 if (IsAggregate())
350 {
351 os << ", A-MPDU of " << GetNMpdus() << " MPDUs";
352 for (const auto& mpdu : m_mpduList)
353 {
354 os << " (" << *mpdu << ")";
355 }
356 }
357 else
358 {
359 os << ", " << ((m_isSingle) ? "S-MPDU" : "normal MPDU") << " (" << *(m_mpduList.at(0))
360 << ")";
361 }
362}
363
364std::ostream&
365operator<<(std::ostream& os, const WifiPsdu& psdu)
366{
367 psdu.Print(os);
368 return os;
369}
370
371} // 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:167
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:215
const WifiMacHeader & GetHeader(std::size_t i) const
Get the header of the i-th MPDU.
Definition wifi-psdu.cc:268
void Print(std::ostream &os) const
Print the PSDU contents.
Definition wifi-psdu.cc:346
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:286
std::vector< Ptr< WifiMpdu > >::const_iterator end() const
Return a const iterator to past-the-last MPDU.
Definition wifi-psdu.cc:334
std::vector< Ptr< WifiMpdu > >::const_iterator begin() const
Return a const iterator to the first MPDU.
Definition wifi-psdu.cc:322
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:245
virtual ~WifiPsdu()
Definition wifi-psdu.cc:61
uint32_t GetSize() const
Return the size of the PSDU in bytes.
Definition wifi-psdu.cc:262
Ptr< const Packet > GetPayload(std::size_t i) const
Get the payload of the i-th MPDU.
Definition wifi-psdu.cc:280
std::size_t GetAmpduSubframeSize(std::size_t i) const
Return the size of the i-th A-MPDU subframe.
Definition wifi-psdu.cc:303
bool m_isSingle
true for an S-MPDU
Definition wifi-psdu.h:243
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
std::vector< Ptr< WifiMpdu > > m_mpduList
list of constituent MPDUs
Definition wifi-psdu.h:244
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:228
std::size_t GetNMpdus() const
Return the number of MPDUs constituting the PSDU.
Definition wifi-psdu.cc:316
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:181
#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:176
void AddWifiMacTrailer(Ptr< Packet > packet)
Add FCS trailer to a packet.