A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-phy-rx-trace-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 The Boeing Company
3 * Copyright (c) 2024 University of Washington (updated to 802.11ax standard)
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9// The purpose of this example is to illustrate basic use of the
10// WifiPhyRxTraceHelper on a simple example program.
11//
12// This script configures two 802.11ax Wi-Fi nodes on a YansWifiChannel,
13// with devices in infrastructure mode, and by default, the station sends
14// one packet of 1000 (application) bytes to the access point. A simple
15// free-space path loss (Friis) propagation loss model is configured.
16// The lowest MCS ("HeMcs0") value is configured.
17//
18// The first packet is sent at time 1 sec. If more packets are configured
19// to be sent, they are separated in time by the 'interval' parameter, which
20// defaults to 1 sec. The simulation ends 1 sec. after the last packet is sent.
21//
22// This script can also be used to put the Wifi layer into full logging mode
23// at the LOG_LEVEL_INFO level; this command will turn on all logging in the
24// wifi module:
25//
26// ./ns3 run "wifi-phy-rx-trace-example --logging=1"
27//
28// Logging of the example program itself is enabled by default in this program;
29// to turn off the printing of packet send and receive events, disable the verbose mode:
30//
31// ./ns3 run "wifi-phy-rx-trace-example --verbose=0"
32//
33// When you are done, you will notice two pcap trace files in your directory.
34// If you have tcpdump installed, you can try this:
35//
36// tcpdump -r wifi-phy-rx-trace-example-0-0.pcap -nn -tt
37//
38// The STA is indexed as node 0, the AP is indexed as node 1
39//
40// Additionally, an ASCII trace file is created (wifi-phy-rx-trace-example.tr)
41//
42// Finally, the example demonstrates that two independent BSS can exist on the
43// same channel, with the trace helper configured to log rxs only on one
44// of the BSS. The outside BSS (OBSS) will send packets that are picked up
45// by the trace helper on the primary BSS's devices. The distance to the OBSS
46// can be configured to place the OBSS within or outside of carrier sense range.
47// The command-line options 'enableTwoBss' and 'distanceTwoBss' can be used
48// to optionally enable and configure the second BSS.
49
50#include "ns3/command-line.h"
51#include "ns3/config.h"
52#include "ns3/double.h"
53#include "ns3/internet-stack-helper.h"
54#include "ns3/ipv4-address-helper.h"
55#include "ns3/log.h"
56#include "ns3/mobility-helper.h"
57#include "ns3/mobility-model.h"
58#include "ns3/neighbor-cache-helper.h"
59#include "ns3/ssid.h"
60#include "ns3/string.h"
61#include "ns3/wifi-phy-rx-trace-helper.h"
62#include "ns3/yans-wifi-channel.h"
63#include "ns3/yans-wifi-helper.h"
64
65using namespace ns3;
66
67NS_LOG_COMPONENT_DEFINE("WifiPhyRxTraceExample");
68
69/**
70 * Function called when a packet is received.
71 *
72 * \param socket The receiving socket.
73 */
74void
76{
78 while ((p = socket->Recv()))
79 {
80 NS_LOG_INFO("Received packet with size " << p->GetSize());
81 }
82}
83
84void
86{
88 while ((p = socket->Recv()))
89 {
90 NS_LOG_INFO("Received packet on OBSS network with size " << p->GetSize());
91 }
92}
93
94/**
95 * Generate traffic.
96 *
97 * \param socket The sending socket.
98 * \param pktSize The packet size.
99 * \param pktCount The packet count.
100 * \param pktInterval The interval between two packets.
101 */
102static void
104{
105 if (pktCount > 0)
106 {
107 NS_LOG_INFO("Generating packet of size " << pktSize);
108 socket->Send(Create<Packet>(pktSize));
109 Simulator::Schedule(pktInterval,
111 socket,
112 pktSize,
113 pktCount - 1,
114 pktInterval);
115 }
116 else
117 {
118 socket->Close();
119 }
120}
121
122void
124{
125 NeighborCacheHelper neighborCache;
126 neighborCache.PopulateNeighborCache();
127}
128
129int
130main(int argc, char* argv[])
131{
132 uint32_t packetSize = 1000; // bytes
133 uint32_t numPackets = 1;
134 double distance = 1; // meters
135 bool enableTwoBss = false; // whether to enable a second (non-traced) BSS
136 double distanceTwoBss = 10; // meters (distance between APs if enableTwoBss is true)
137 Time interval = Seconds(1.0);
138 bool verbose = true;
139 bool logging = false;
140
141 CommandLine cmd(__FILE__);
142 cmd.AddValue("packetSize", "size of application packet sent", packetSize);
143 cmd.AddValue("numPackets", "number of packets generated", numPackets);
144 cmd.AddValue("interval", "interval between packets", interval);
145 cmd.AddValue("distance", "distance between AP and STA", distance);
146 cmd.AddValue("enableTwoBss", "enable a second BSS (not traced)", enableTwoBss);
147 cmd.AddValue("distanceTwoBss", "distance between BSS (meters)", distanceTwoBss);
148 cmd.AddValue("logging", "enable all wifi module log components", logging);
149 cmd.AddValue("verbose", "enable this program's log components", verbose);
150 cmd.Parse(argc, argv);
151
152 if (numPackets == 0)
153 {
154 std::cout << "No packets configured to be sent; exiting" << std::endl;
155 return 0;
156 }
157
159 c.Create(2);
160
161 NodeContainer c2;
162 if (enableTwoBss)
163 {
164 c2.Create(2);
165 }
166
169 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
170 positionAlloc->Add(Vector(distance, 0.0, 0.0));
171 mobility.SetPositionAllocator(positionAlloc);
172 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
173 mobility.Install(c);
174
175 if (enableTwoBss)
176 {
177 MobilityHelper obssMobility;
179 obssPositionAlloc->Add(Vector(0.0, distanceTwoBss, 0.0));
180 obssPositionAlloc->Add(Vector(distance, distanceTwoBss, 0.0));
181 obssMobility.SetPositionAllocator(obssPositionAlloc);
182 obssMobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
183 obssMobility.Install(c2);
184 }
185
187 wifi.SetStandard(WIFI_STANDARD_80211ax);
188
189 YansWifiPhyHelper wifiPhy;
191
192 YansWifiChannelHelper wifiChannel;
193 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
194 wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
195 wifiPhy.SetChannel(wifiChannel.Create());
196
197 // Add a mac and disable rate control
198 WifiMacHelper wifiMac;
199 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
200 "DataMode",
201 StringValue("HeMcs0"),
202 "ControlMode",
203 StringValue("HeMcs0"));
204
205 // Setup the rest of the MAC
206 Ssid ssid = Ssid("wifi-default");
207 // setup STA
208 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
209 NetDeviceContainer staDevice = wifi.Install(wifiPhy, wifiMac, c.Get(0));
210 NetDeviceContainer devices = staDevice;
211 // setup AP to beacon roughly once per second (must be a multiple of 1024 us)
212 wifiMac.SetType("ns3::ApWifiMac",
213 "Ssid",
214 SsidValue(ssid),
215 "BeaconInterval",
216 TimeValue(MilliSeconds(1024)));
217 NetDeviceContainer apDevice = wifi.Install(wifiPhy, wifiMac, c.Get(1));
218 devices.Add(apDevice);
219
220 NetDeviceContainer obssDevices;
221 if (enableTwoBss)
222 {
223 ssid = Ssid("obss");
224 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
225 NetDeviceContainer obssStaDevice = wifi.Install(wifiPhy, wifiMac, c2.Get(0));
226 obssDevices = obssStaDevice;
227 wifiMac.SetType("ns3::ApWifiMac",
228 "Ssid",
229 SsidValue(ssid),
230 "BeaconInterval",
231 TimeValue(MilliSeconds(1024)));
232 NetDeviceContainer obssApDevice = wifi.Install(wifiPhy, wifiMac, c2.Get(1));
233 obssDevices.Add(obssApDevice);
234 }
235
237 internet.Install(c);
238
240 ipv4.SetBase("10.1.1.0", "255.255.255.0");
241 Ipv4InterfaceContainer i = ipv4.Assign(devices);
242
243 if (enableTwoBss)
244 {
245 internet.Install(c2);
246 ipv4.SetBase("10.1.2.0", "255.255.255.0");
247 ipv4.Assign(obssDevices);
248 }
249
250 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
251 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(1), tid);
253 recvSink->Bind(local);
254 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
255
256 Ptr<Socket> source = Socket::CreateSocket(c.Get(0), tid);
257 InetSocketAddress remote = InetSocketAddress(Ipv4Address("10.1.1.2"), 80);
258 source->Connect(remote);
259
260 Ptr<Socket> obssRecvSink;
261 Ptr<Socket> obssSource;
262 if (enableTwoBss)
263 {
264 obssRecvSink = Socket::CreateSocket(c2.Get(1), tid);
266 obssRecvSink->Bind(obssLocal);
267 obssRecvSink->SetRecvCallback(MakeCallback(&ReceiveObssPacket));
268
269 obssSource = Socket::CreateSocket(c2.Get(0), tid);
270 InetSocketAddress obssRemote = InetSocketAddress(Ipv4Address("10.1.2.2"), 80);
271 obssSource->Connect(obssRemote);
272 }
273
274 // Use the NeighborCacheHelper to avoid ARP messages (ARP replies, since they are unicast,
275 // count in the statistics. The cache operation must be scheduled after WifiNetDevices are
276 // started, until issue #851 is fixed. The indirection through a normal function is
277 // necessary because NeighborCacheHelper::PopulateNeighborCache() is overloaded
279
280 // Tracing
281 wifiPhy.EnablePcap("wifi-phy-rx-trace-example", devices);
283 wifiPhy.EnableAsciiAll(asciiTrace.CreateFileStream("wifi-phy-rx-trace-example.tr"));
284
285 // Logging configuration
286 if (logging)
287 {
288 WifiHelper::EnableLogComponents(LOG_LEVEL_INFO); // Turn on all Wifi logging
289 }
290 if (verbose)
291 {
293 "WifiPhyRxTraceExample",
295 }
296 WifiPhyRxTraceHelper rxTraceHelper;
297 // Enable trace helper only on one BSS
298 rxTraceHelper.Enable(c);
299 rxTraceHelper.Start(MilliSeconds(999)); // 1 ms before applications
300 // The last packet will be sent at time 1 sec. + (numPackets - 1) * interval
301 // Configure the stop time to be 1 sec. later than this.
302 Time stopTime = Seconds(1) + (numPackets - 1) * interval + Seconds(1);
303 rxTraceHelper.Stop(stopTime);
304
305 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
306 Seconds(1.0),
308 source,
310 numPackets,
311 interval);
312
313 if (enableTwoBss)
314 {
315 Simulator::ScheduleWithContext(obssSource->GetNode()->GetId(),
316 Seconds(1.5),
318 obssSource,
320 numPackets,
321 interval);
322 }
323
326
327 // The following provide some examples of how to access and print the trace helper contents.
328
329 std::cout << "*** Print statistics for all nodes using built-in print method:" << std::endl;
330 rxTraceHelper.PrintStatistics();
331 std::cout << std::endl;
332
333 std::cout << "*** Print statistics for the STA only using built-in print method:" << std::endl;
334 rxTraceHelper.PrintStatistics(c.Get(0)->GetId());
335 std::cout << std::endl;
336
337 std::cout << "*** Print statistics for the AP only using built-in print method:" << std::endl;
338 rxTraceHelper.PrintStatistics(c.Get(1));
339 std::cout << std::endl;
340
341 std::cout << "*** Get statistics object and print the fields one-by-one:" << std::endl;
342 auto stats = rxTraceHelper.GetStatistics();
343 std::cout << " overlapppingPpdu: " << stats.m_overlappingPpdus << std::endl;
344 std::cout << " nonOverlapppingPpdu: " << stats.m_nonOverlappingPpdus << std::endl;
345 std::cout << " receivedPpdus: " << stats.m_receivedPpdus << std::endl;
346 std::cout << " failedPpdus: " << stats.m_failedPpdus << std::endl;
347 std::cout << " receivedMpdus: " << stats.m_receivedMpdus << std::endl;
348 std::cout << " failedMpdus: " << stats.m_failedMpdus << std::endl;
349 std::cout << std::endl;
350
351 std::cout << "*** Get vector of reception records and print out some fields:" << std::endl;
352 auto optionalRecords = rxTraceHelper.GetPpduRecords(1);
353 if (optionalRecords.has_value())
354 {
355 auto records = optionalRecords->get(); // const std::vector<WifiPpduRxRecord>&
356 std::cout << "*** Records vector has size of " << records.size() << std::endl;
357 if (!records.empty())
358 {
359 // First record
360 std::cout << " First record:" << std::endl;
361 std::cout << " first PPDU's RSSI (dBm): " << records[0].m_rssi << std::endl;
362 std::cout << " first PPDU's receiver ID: " << records[0].m_receiverId << std::endl;
363 std::cout << " first PPDU's sender ID: " << records[0].m_senderId << std::endl;
364 std::cout << " first PPDU's start time: " << records[0].m_startTime.GetSeconds()
365 << std::endl;
366 std::cout << " first PPDU's end time: " << records[0].m_endTime.GetSeconds()
367 << std::endl;
368 std::cout << " first PPDU's number of MPDUs: " << records[0].m_statusPerMpdu.size()
369 << std::endl;
370 std::cout << " first PPDU's sender device ID: " << records[0].m_senderDeviceId
371 << std::endl;
372 }
373 if (records.size() > 1)
374 {
375 // Second record
376 std::cout << " Second record:" << std::endl;
377 std::cout << " second PPDU's RSSI (dBm): " << records[1].m_rssi << std::endl;
378 std::cout << " second PPDU's receiver ID: " << records[1].m_receiverId << std::endl;
379 std::cout << " second PPDU's sender ID: " << records[1].m_senderId << std::endl;
380 std::cout << " second PPDU's start time: " << records[1].m_startTime.GetSeconds()
381 << std::endl;
382 std::cout << " second PPDU's end time: " << records[1].m_endTime.GetSeconds()
383 << std::endl;
384 std::cout << " second PPDU's number of MPDUs: " << records[1].m_statusPerMpdu.size()
385 << std::endl;
386 std::cout << " second PPDU's sender device ID: " << records[1].m_senderDeviceId
387 << std::endl;
388 }
389 if (records.size() > 2)
390 {
391 // Third record
392 std::cout << " Third record:" << std::endl;
393 std::cout << " third PPDU's RSSI (dBm): " << records[2].m_rssi << std::endl;
394 std::cout << " third PPDU's receiver ID: " << records[2].m_receiverId << std::endl;
395 std::cout << " third PPDU's sender ID: " << records[2].m_senderId << std::endl;
396 std::cout << " third PPDU's start time: " << records[2].m_startTime.GetSeconds()
397 << std::endl;
398 std::cout << " third PPDU's end time: " << records[2].m_endTime.GetSeconds()
399 << std::endl;
400 std::cout << " third PPDU's number of MPDUs: " << records[2].m_statusPerMpdu.size()
401 << std::endl;
402 std::cout << " third PPDU's sender device ID: " << records[2].m_senderDeviceId
403 << std::endl;
404 }
405 std::cout << std::endl;
406 }
407 else
408 {
409 std::cout << "*** Records vector is empty" << std::endl;
410 std::cout << std::endl;
411 }
412
414
415 return 0;
416}
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Parse command-line arguments.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
void SetMobilityModel(std::string type, Ts &&... args)
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
A helper class to populate neighbor cache.
void PopulateNeighborCache()
Populate neighbor ARP and NDISC caches for all devices.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
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 GetId() const
Definition node.cc:106
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition simulator.h:577
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
helps to create WifiNetDevice objects
static void EnableLogComponents(LogLevel logLevel=LOG_LEVEL_ALL)
Helper to enable all WifiNetDevice log components with one statement.
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Assists in tracing and analyzing Wi-Fi Physical Layer (PHY) receptions.
const std::vector< WifiPpduRxRecord > & GetPpduRecords() const
Accesses a vector of saved and completed PPDU reception records.
void PrintStatistics() const
Print statistics for all nodes, devices, and links during the collection period.
void Stop(Time stopTime)
Stops the collection of statistics at a specified time.
WifiPhyTraceStatistics GetStatistics() const
Retrieves current statistics of successful and failed data PPDUs and MPDUs receptions,...
void Enable(NodeContainer nodes)
Enables trace collection for all nodes and WifiNetDevices in the specified NodeContainer.
void Start(Time startTime)
Starts the collection of statistics from a specified start time.
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
Time stopTime
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1320
@ WIFI_STANDARD_80211ax
devices
Definition first.py:31
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:291
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:684
LogLevel
Logging severity classes and levels.
Definition log.h:83
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition log.h:107
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition log.h:108
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition log.h:109
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition log.h:93
ssid
Definition third.py:82
wifi
Definition third.py:84
mobility
Definition third.py:92
bool verbose
uint64_t m_overlappingPpdus
Number of PPDUs that overlapped in time with at least one other PPDU.
uint32_t pktSize
packet size used for the simulation (in bytes)
void ReceiveObssPacket(Ptr< Socket > socket)
static void GeneratePacket(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Generate traffic.
void ReceivePacket(Ptr< Socket > socket)
Function called when a packet is received.
void PopulateNeighborCache()
static const uint32_t packetSize
Packet size generated at the AP.