A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-p2p-test.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 DERONNE SOFTWARE ENGINEERING
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sébastien Deronne <sebastien.deronne@gmail.com>
7 */
8
9#include "ns3/adhoc-wifi-mac.h"
10#include "ns3/boolean.h"
11#include "ns3/config.h"
12#include "ns3/double.h"
13#include "ns3/mgt-headers.h"
14#include "ns3/mobility-helper.h"
15#include "ns3/packet-socket-client.h"
16#include "ns3/packet-socket-helper.h"
17#include "ns3/packet-socket-server.h"
18#include "ns3/pointer.h"
19#include "ns3/random-variable-stream.h"
20#include "ns3/rng-seed-manager.h"
21#include "ns3/test.h"
22#include "ns3/wifi-helper.h"
23#include "ns3/wifi-mac-helper.h"
24#include "ns3/wifi-net-device.h"
25#include "ns3/wifi-psdu.h"
26#include "ns3/yans-wifi-helper.h"
27
28#include <utility>
29#include <vector>
30
31using namespace ns3;
32
33NS_LOG_COMPONENT_DEFINE("WifiP2pTest");
34
35/**
36 * @ingroup wifi-test
37 * @ingroup tests
38 *
39 * @brief Test the beaconing operation in IBSS.
40 */
42{
43 public:
44 /**
45 * Constructor
46 */
48
49 private:
50 void DoSetup() override;
51 void DoRun() override;
52
53 /**
54 * Callback invoked when a FEM passes PSDUs to the PHY.
55 *
56 * @param psduMap the PSDU map
57 * @param txVector the TX vector
58 * @param txPower the TX power
59 */
60 virtual void Transmit(WifiConstPsduMap psduMap, WifiTxVector txVector, Watt_u txPower);
61
62 /**
63 * Update the BeaconJitter of the IBSS STAs based on the sender of the Beacon
64 * to have control over the randomness of the Beacon transmission time. If the sender is the
65 * same as the STA, the BeaconJitter is set to 1, otherwise it is set to 0.
66 *
67 * @param sender the address of the STA that just transmitted a Beacon
68 */
70
71 /**
72 * Check correctness of transmitted beacon frames
73 */
74 void CheckResults();
75
76 /// Information about transmitted beacon frames
78 {
79 WifiMacHeader header; ///< MAC header
80 MgtBeaconHeader beacon; ///< Beacon header
81 };
82
83 std::vector<BeaconFrameInfo> m_txBeacons; ///< transmitted beacons
84 std::vector<Ptr<AdhocWifiMac>> m_macs; ///< MAC of the IBSS STAs
85 std::vector<Ptr<ConstantRandomVariable>> m_beaconJitters; ///< BeaconJitter of the IBSS STAs
86};
87
89 : TestCase("Check beaconing operation in IBSS")
90{
91}
92
93void
95{
96 const auto& mpdu = *psduMap.cbegin()->second->begin();
97 NS_LOG_FUNCTION(this << *mpdu << txVector << txPower);
98 if (mpdu->GetHeader().IsBeacon())
99 {
100 MgtBeaconHeader beacon;
101 mpdu->GetPacket()->PeekHeader(beacon);
102 m_txBeacons.push_back({mpdu->GetHeader(), beacon});
103 UpdateBeaconJitters(mpdu->GetHeader().GetAddr2());
104 }
105}
106
107void
109{
110 NS_LOG_FUNCTION(this << sender);
111 for (std::size_t i = 0; i < m_macs.size(); ++i)
112 {
113 const auto value = (m_macs[i]->GetAddress() == sender) ? 1.0 : 0.0;
114 m_beaconJitters[i]->SetAttribute("Constant", DoubleValue(value));
115 }
116}
117
118void
120{
121 NS_TEST_EXPECT_MSG_EQ(m_txBeacons.size(), 10, "Expected 10 transmitted beacons");
122 for (std::size_t i = 0; i < m_txBeacons.size(); ++i)
123 {
124 NS_TEST_EXPECT_MSG_EQ(m_txBeacons[i].header.GetAddr2(),
125 m_macs.at(i % 2)->GetAddress(),
126 "Expected STAs to alternate Beacon transmissions");
127 const auto& capabilities = m_txBeacons[i].beacon.m_capability;
128 NS_TEST_EXPECT_MSG_EQ(capabilities.IsEss(), false, "ESS bit should not be set for IBSS");
129 NS_TEST_EXPECT_MSG_EQ(capabilities.IsIbss(), true, "IBSS bit should be set");
130 }
131}
132
133void
135{
136 // WifiHelper::EnableLogComponents();
137 // LogComponentEnable("WifiP2pTest", LOG_LEVEL_ALL);
138
141 int64_t streamNumber = 100;
142
143 NodeContainer ibssNodes;
144 ibssNodes.Create(2);
145
146 auto channel = YansWifiChannelHelper::Default();
148 phy.SetChannel(channel.Create());
149
150 WifiHelper wifi;
151 wifi.SetStandard(WIFI_STANDARD_80211ax);
152
153 WifiMacHelper mac;
154 mac.SetType("ns3::AdhocWifiMac",
155 "Ssid",
156 SsidValue(Ssid("ibss-ssid")),
157 "BeaconGeneration",
158 BooleanValue(true),
159 "EnableBeaconJitter",
160 BooleanValue(false));
161
162 auto ibssDevices = wifi.Install(phy, mac, ibssNodes);
163
164 WifiHelper::AssignStreams(ibssDevices, streamNumber);
165
166 for (uint32_t i = 0; i < ibssDevices.GetN(); ++i)
167 {
168 auto adhocMac =
169 DynamicCast<AdhocWifiMac>(DynamicCast<WifiNetDevice>(ibssDevices.Get(i))->GetMac());
171 jitter->SetAttribute("Constant", DoubleValue((i == 0) ? 0.0 : 1.0));
172 adhocMac->SetAttribute("BeaconJitter", PointerValue(jitter));
173 m_macs.push_back(adhocMac);
174 m_beaconJitters.push_back(jitter);
175 }
176
177 MobilityHelper mobility;
179
180 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
181 positionAlloc->Add(Vector(1.0, 0.0, 0.0));
182 mobility.SetPositionAllocator(positionAlloc);
183
184 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
185 mobility.Install(ibssNodes);
186
188 "/NodeList/*/DeviceList/0/$ns3::WifiNetDevice/Phys/0/PhyTxPsduBegin",
190}
191
192void
202
203/**
204 * @ingroup wifi-test
205 * @ingroup tests
206 *
207 * @brief Test transmissions in IBSS when peers have different capabilities.
208 */
210{
211 public:
212 /**
213 * Constructor
214 * @param txStandard the standard supported by the TX device
215 * @param rxStandard the standard supported by the RX device
216 * @param beaconing flag whether beaconing is used in IBSS
217 */
218 IbssCapabilitiesTest(WifiStandard txStandard, WifiStandard rxStandard, bool beaconing);
219
220 private:
221 void DoSetup() override;
222 void DoRun() override;
223
224 /**
225 * Callback invoked when a FEM passes PSDUs to the PHY.
226 *
227 * @param psduMap the PSDU map
228 * @param txVector the TX vector
229 * @param txPower the TX power
230 */
231 void Transmit(WifiConstPsduMap psduMap, WifiTxVector txVector, Watt_u txPower);
232
233 /**
234 * Callback invoked when a packet is dropped by the MAC prior to enqueueing.
235 *
236 * @param packet the dropped packet
237 */
238 void TxDrop(Ptr<const Packet> packet);
239
240 /**
241 * Callback invoked when a packet is generated by the packet socket client.
242 *
243 * @param pkt the packet
244 * @param addr the address
245 */
246 void TxGenerated(Ptr<const Packet> pkt, const Address& addr);
247
248 /**
249 * Callback invoked when a packet is received by the packet socket server.
250 *
251 * @param pkt the packet
252 * @param addr the address
253 */
254 void Receive(Ptr<const Packet> pkt, const Address& addr);
255
256 /**
257 * Check that the simulation produced the expected results.
258 */
259 void CheckResults();
260
261 WifiStandard m_txStandard; ///< the standard for the TX device
262 WifiStandard m_rxStandard; ///< the standard for the RX device
263 bool m_beaconing; ///< flag whether beacon generation is enabled
264
265 std::size_t m_nPackets; ///< number of packets to generate
266
267 std::vector<WifiTxVector> m_txVectors; ///< TXVECTOR of transmitted packets
268 std::vector<Ptr<const Packet>> m_droppedPackets; ///< dropped packets
269 std::vector<Ptr<const Packet>> m_generatedPackets; ///< generated packets
270 std::vector<Ptr<const Packet>> m_rxPackets; ///< received packets
271};
272
274 WifiStandard rxStandard,
275 bool beaconing)
276 : TestCase("Check transmissions in IBSS when peers have different capabilities"),
277 m_txStandard{txStandard},
278 m_rxStandard{rxStandard},
279 m_beaconing{beaconing},
280 m_nPackets{2}
281{
282}
283
284void
286{
287 const auto& mpdu = *psduMap.cbegin()->second->begin();
288 NS_LOG_FUNCTION(this << *mpdu << txVector << txPower);
289 if (mpdu->GetHeader().IsData())
290 {
291 m_txVectors.push_back(txVector);
292 }
293}
294
295void
297{
298 NS_LOG_FUNCTION(this << packet);
299 m_droppedPackets.push_back(packet);
300}
301
302void
304{
305 NS_LOG_FUNCTION(this << pkt << addr);
306 m_generatedPackets.push_back(pkt);
307}
308
309void
311{
312 NS_LOG_FUNCTION(this << pkt << addr);
313 m_rxPackets.push_back(pkt);
314}
315
316void
318{
321 "Unexpected amount of generated packets");
322
323 /*
324 * The first packet should get dropped if capabilities are learnt from beacon,
325 * since the first packet is transmitted before the first beacon is received
326 */
327 const std::size_t expectedDroppedPackets = m_beaconing ? 1 : 0;
329 expectedDroppedPackets,
330 "Incorrect amount of dropped packets");
331
332 /*
333 * The amount of transmitted packets correspond to the number of generated packet minus
334 * the amount of dropped packets. If capabilities are not learnt from beacons, TX device
335 * assumes RX device supports the same capabilities. If that is not the case, packets
336 * cannot be successfully received and each packet gets retransmitted 7 times (default
337 * configuration of retry counter)
338 */
339 const std::size_t expectedTxPackets =
340 m_beaconing ? (m_nPackets - 1)
343 expectedTxPackets,
344 "Incorrect amount of transmitted packets");
345
346 /*
347 * If capabilities are learnt from beacons, the first packet is dropped and hence only
348 * one packet can be received. Otherwise, the two generated packets are transmitted and
349 * can be received only if the RX device supports the capabilities of the TX device.
350 */
351 const std::size_t expectedRxPackets =
354 expectedRxPackets,
355 "Incorrect amount of received packets");
356
357 if (m_beaconing)
358 {
360 m_generatedPackets.at(0)->GetUid(),
361 "The first generated packet should have been dropped");
362 NS_TEST_EXPECT_MSG_EQ(m_rxPackets.at(0)->GetUid(),
363 m_generatedPackets.at(1)->GetUid(),
364 "The second generated packet should have been received");
365 }
366 else
367 {
368 for (std::size_t i = 0; i < m_rxPackets.size(); ++i)
369 {
370 NS_TEST_EXPECT_MSG_EQ(m_rxPackets.at(i)->GetUid(),
371 m_generatedPackets.at(i)->GetUid(),
372 "Received packets are not in correct order");
373 }
374 }
375
376 WifiModulationClass expectedModClass;
377 if (m_beaconing)
378 {
380 {
381 expectedModClass = WIFI_MOD_CLASS_HE;
382 }
383 else if (((m_txStandard == WIFI_STANDARD_80211ax) &&
387 {
388 expectedModClass = WIFI_MOD_CLASS_VHT;
389 }
390 else
391 {
392 NS_ABORT_MSG("TX and RX standards combination not covered by the test");
393 }
394 }
395 else
396 {
398 {
399 expectedModClass = WIFI_MOD_CLASS_HE;
400 }
402 {
403 expectedModClass = WIFI_MOD_CLASS_VHT;
404 }
405 else
406 {
407 NS_ABORT_MSG("TX standard not covered by the test");
408 }
409 }
410 NS_TEST_EXPECT_MSG_EQ(m_txVectors.at(0).GetModulationClass(),
411 expectedModClass,
412 "Unexpected modulation class");
413}
414
415void
417{
418 // WifiHelper::EnableLogComponents();
419 // LogComponentEnable("WifiP2pTest", LOG_LEVEL_ALL);
420
423 int64_t streamNumber = 100;
424
425 NodeContainer ibssNodes;
426 ibssNodes.Create(2);
427
428 auto channel = YansWifiChannelHelper::Default();
430 phy.SetChannel(channel.Create());
431
432 WifiHelper wifi;
433 wifi.SetStandard(m_txStandard);
434 wifi.SetRemoteStationManager("ns3::IdealWifiManager",
435 "IncrementRetryCountUnderBa",
436 BooleanValue(true));
437
438 WifiMacHelper mac;
439 mac.SetType("ns3::AdhocWifiMac",
440 "Ssid",
441 SsidValue(Ssid("ibss-ssid")),
442 "BeaconGeneration",
444
445 auto ibssDevices = wifi.Install(phy, mac, ibssNodes.Get(0));
446
447 wifi.SetStandard(m_rxStandard);
448 ibssDevices.Add(wifi.Install(phy, mac, ibssNodes.Get(1)));
449
450 WifiHelper::AssignStreams(ibssDevices, streamNumber);
451
452 MobilityHelper mobility;
454
455 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
456 positionAlloc->Add(Vector(1.0, 0.0, 0.0));
457 mobility.SetPositionAllocator(positionAlloc);
458
459 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
460 mobility.Install(ibssNodes);
461
462 PacketSocketHelper packetSocket;
463 packetSocket.Install(ibssNodes);
464
465 PacketSocketAddress socketAddr;
466 socketAddr.SetSingleDevice(ibssDevices.Get(0)->GetIfIndex());
467 socketAddr.SetPhysicalAddress(ibssDevices.Get(1)->GetAddress());
468
469 auto client = CreateObject<PacketSocketClient>();
470 client->SetAttribute("PacketSize", UintegerValue(1000));
471 client->SetAttribute("MaxPackets", UintegerValue(m_nPackets));
472 client->SetAttribute("Interval", TimeValue(Seconds(0.5)));
473 client->SetRemote(socketAddr);
474 ibssNodes.Get(0)->AddApplication(client);
475 client->SetStartTime(Seconds(0.0));
476 client->SetStopTime(Seconds(1.0));
477
478 auto server = CreateObject<PacketSocketServer>();
479 server->SetLocal(socketAddr);
480 ibssNodes.Get(1)->AddApplication(server);
481 server->SetStartTime(Seconds(0.0));
482 server->SetStopTime(Seconds(1.0));
483
484 Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::PacketSocketClient/Tx",
486
487 Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxPsduBegin",
489
490 Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/MacTxDrop",
492
493 Config::ConnectWithoutContext("/NodeList/*/ApplicationList/*/$ns3::PacketSocketServer/Rx",
495}
496
497void
507
508/**
509 * @ingroup wifi-test
510 * @ingroup tests
511 *
512 * @brief wifi P2P test suite
513 */
515{
516 public:
518};
519
521 : TestSuite("wifi-p2p", Type::UNIT)
522{
524 for (auto& [txStandard, rxStandard] : std::vector<std::pair<WifiStandard, WifiStandard>>{
528 {
529 for (auto beaconing : {true, false})
530 {
531 AddTestCase(new IbssCapabilitiesTest(txStandard, rxStandard, beaconing),
533 }
534 }
535}
536
537static WifiP2pTestSuite g_wifiP2pTestSuite; ///< the test suite
Test the beaconing operation in IBSS.
IbssBeaconingTest()
Constructor.
virtual void Transmit(WifiConstPsduMap psduMap, WifiTxVector txVector, Watt_u txPower)
Callback invoked when a FEM passes PSDUs to the PHY.
void DoRun() override
Implementation to actually run this TestCase.
void UpdateBeaconJitters(Mac48Address sender)
Update the BeaconJitter of the IBSS STAs based on the sender of the Beacon to have control over the r...
std::vector< Ptr< ConstantRandomVariable > > m_beaconJitters
BeaconJitter of the IBSS STAs.
std::vector< Ptr< AdhocWifiMac > > m_macs
MAC of the IBSS STAs.
void CheckResults()
Check correctness of transmitted beacon frames.
std::vector< BeaconFrameInfo > m_txBeacons
transmitted beacons
void DoSetup() override
Implementation to do any local setup required for this TestCase.
Test transmissions in IBSS when peers have different capabilities.
std::vector< Ptr< const Packet > > m_generatedPackets
generated packets
void DoRun() override
Implementation to actually run this TestCase.
WifiStandard m_rxStandard
the standard for the RX device
void DoSetup() override
Implementation to do any local setup required for this TestCase.
std::vector< Ptr< const Packet > > m_rxPackets
received packets
WifiStandard m_txStandard
the standard for the TX device
void TxGenerated(Ptr< const Packet > pkt, const Address &addr)
Callback invoked when a packet is generated by the packet socket client.
void TxDrop(Ptr< const Packet > packet)
Callback invoked when a packet is dropped by the MAC prior to enqueueing.
void CheckResults()
Check that the simulation produced the expected results.
bool m_beaconing
flag whether beacon generation is enabled
std::vector< WifiTxVector > m_txVectors
TXVECTOR of transmitted packets.
std::vector< Ptr< const Packet > > m_droppedPackets
dropped packets
IbssCapabilitiesTest(WifiStandard txStandard, WifiStandard rxStandard, bool beaconing)
Constructor.
std::size_t m_nPackets
number of packets to generate
void Receive(Ptr< const Packet > pkt, const Address &addr)
Callback invoked when a packet is received by the packet socket server.
void Transmit(WifiConstPsduMap psduMap, WifiTxVector txVector, Watt_u txPower)
Callback invoked when a FEM passes PSDUs to the PHY.
wifi P2P test suite
a polymophic address class
Definition address.h:114
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
an EUI-48 address
Implement the header for management frames of type beacon.
Helper class used to assign positions and mobility models to nodes.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition node.cc:153
an address for a packet socket
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
AttributeValue implementation for Pointer.
Definition pointer.h:37
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:169
The IEEE 802.11 SSID Information Element.
Definition ssid.h:27
AttributeValue implementation for Ssid.
Definition ssid.h:98
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
Definition test.cc:296
@ QUICK
Fast test.
Definition test.h:1057
TestCase(const TestCase &)=delete
Caller graph was not generated because of its size.
Type
Type of test.
Definition test.h:1271
TestSuite(std::string name, Type type=Type::UNIT)
Construct a new test suite.
Definition test.cc:494
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
static int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the PHY and MAC aspects ...
Implements the IEEE 802.11 MAC header.
create MAC layers for a ns3::WifiNetDevice.
This class mimics the TXVECTOR which is to be passed to the PHY in order to define the parameters whi...
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Make it easy to create and manage PHY objects for the YANS model.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:690
void ConnectWithoutContext(std::string path, const CallbackBase &cb)
Definition config.cc:942
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:201
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
#define NS_TEST_EXPECT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report if not.
Definition test.h:240
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1313
WifiStandard
Identifies the IEEE 802.11 specifications that a Wifi device can be configured to use.
WifiModulationClass
This enumeration defines the modulation classes per (Table 10-6 "Modulation classes"; IEEE 802....
@ WIFI_STANDARD_80211ax
@ WIFI_STANDARD_80211ac
@ 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.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:643
std::unordered_map< uint16_t, Ptr< const WifiPsdu > > WifiConstPsduMap
Map of const PSDUs indexed by STA-ID.
Definition wifi-ppdu.h:39
double Watt_u
Watt weak type.
Definition wifi-units.h:25
Information about transmitted beacon frames.
WifiMacHeader header
MAC header.
MgtBeaconHeader beacon
Beacon header.
static WifiP2pTestSuite g_wifiP2pTestSuite
the test suite