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 * 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 */
19
20// The purpose of this example is to illustrate basic use of the
21// WifiPhyRxTraceHelper on a simple example program.
22//
23// This script configures two 802.11ax Wi-Fi nodes on a YansWifiChannel,
24// with devices in infrastructure mode, and by default, the station sends
25// one packet of 1000 (application) bytes to the access point. A simple
26// free-space path loss (Friis) propagation loss model is configured.
27// The lowest MCS ("HeMcs0") value is configured.
28//
29// The first packet is sent at time 1 sec. If more packets are configured
30// to be sent, they are separated in time by the 'interval' parameter, which
31// defaults to 1 sec. The simulation ends 1 sec. after the last packet is sent.
32//
33// This script can also be used to put the Wifi layer into full logging mode
34// at the LOG_LEVEL_INFO level; this command will turn on all logging in the
35// wifi module:
36//
37// ./ns3 run "wifi-phy-rx-trace-example --logging=1"
38//
39// Logging of the example program itself is enabled by default in this program;
40// to turn off the printing of packet send and receive events, disable the verbose mode:
41//
42// ./ns3 run "wifi-phy-rx-trace-example --verbose=0"
43//
44// When you are done, you will notice two pcap trace files in your directory.
45// If you have tcpdump installed, you can try this:
46//
47// tcpdump -r wifi-phy-rx-trace-example-0-0.pcap -nn -tt
48//
49// The STA is indexed as node 0, the AP is indexed as node 1
50//
51// Additionally, an ASCII trace file is created (wifi-phy-rx-trace-example.tr)
52//
53// Finally, the example demonstrates that two independent BSS can exist on the
54// same channel, with the trace helper configured to log rxs only on one
55// of the BSS. The outside BSS (OBSS) will send packets that are picked up
56// by the trace helper on the primary BSS's devices. The distance to the OBSS
57// can be configured to place the OBSS within or outside of carrier sense range.
58// The command-line options 'enableTwoBss' and 'distanceTwoBss' can be used
59// to optionally enable and configure the second BSS.
60
61#include "ns3/command-line.h"
62#include "ns3/config.h"
63#include "ns3/double.h"
64#include "ns3/internet-stack-helper.h"
65#include "ns3/ipv4-address-helper.h"
66#include "ns3/log.h"
67#include "ns3/mobility-helper.h"
68#include "ns3/mobility-model.h"
69#include "ns3/neighbor-cache-helper.h"
70#include "ns3/ssid.h"
71#include "ns3/string.h"
72#include "ns3/wifi-phy-rx-trace-helper.h"
73#include "ns3/yans-wifi-channel.h"
74#include "ns3/yans-wifi-helper.h"
75
76using namespace ns3;
77
78NS_LOG_COMPONENT_DEFINE("WifiPhyRxTraceExample");
79
80/**
81 * Function called when a packet is received.
82 *
83 * \param socket The receiving socket.
84 */
85void
87{
89 while ((p = socket->Recv()))
90 {
91 NS_LOG_INFO("Received packet with size " << p->GetSize());
92 }
93}
94
95void
97{
99 while ((p = socket->Recv()))
100 {
101 NS_LOG_INFO("Received packet on OBSS network with size " << p->GetSize());
102 }
103}
104
105/**
106 * Generate traffic.
107 *
108 * \param socket The sending socket.
109 * \param pktSize The packet size.
110 * \param pktCount The packet count.
111 * \param pktInterval The interval between two packets.
112 */
113static void
115{
116 if (pktCount > 0)
117 {
118 NS_LOG_INFO("Generating packet of size " << pktSize);
119 socket->Send(Create<Packet>(pktSize));
120 Simulator::Schedule(pktInterval,
122 socket,
123 pktSize,
124 pktCount - 1,
125 pktInterval);
126 }
127 else
128 {
129 socket->Close();
130 }
131}
132
133void
135{
136 NeighborCacheHelper neighborCache;
137 neighborCache.PopulateNeighborCache();
138}
139
140int
141main(int argc, char* argv[])
142{
143 uint32_t packetSize = 1000; // bytes
144 uint32_t numPackets = 1;
145 double distance = 1; // meters
146 bool enableTwoBss = false; // whether to enable a second (non-traced) BSS
147 double distanceTwoBss = 10; // meters (distance between APs if enableTwoBss is true)
148 Time interval = Seconds(1.0);
149 bool verbose = true;
150 bool logging = false;
151
152 CommandLine cmd(__FILE__);
153 cmd.AddValue("packetSize", "size of application packet sent", packetSize);
154 cmd.AddValue("numPackets", "number of packets generated", numPackets);
155 cmd.AddValue("interval", "interval between packets", interval);
156 cmd.AddValue("distance", "distance between AP and STA", distance);
157 cmd.AddValue("enableTwoBss", "enable a second BSS (not traced)", enableTwoBss);
158 cmd.AddValue("distanceTwoBss", "distance between BSS (meters)", distanceTwoBss);
159 cmd.AddValue("logging", "enable all wifi module log components", logging);
160 cmd.AddValue("verbose", "enable this program's log components", verbose);
161 cmd.Parse(argc, argv);
162
163 if (numPackets == 0)
164 {
165 std::cout << "No packets configured to be sent; exiting" << std::endl;
166 return 0;
167 }
168
170 c.Create(2);
171
172 NodeContainer c2;
173 if (enableTwoBss)
174 {
175 c2.Create(2);
176 }
177
179 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
180 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
181 positionAlloc->Add(Vector(distance, 0.0, 0.0));
182 mobility.SetPositionAllocator(positionAlloc);
183 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
184 mobility.Install(c);
185
186 if (enableTwoBss)
187 {
188 MobilityHelper obssMobility;
189 Ptr<ListPositionAllocator> obssPositionAlloc = CreateObject<ListPositionAllocator>();
190 obssPositionAlloc->Add(Vector(0.0, distanceTwoBss, 0.0));
191 obssPositionAlloc->Add(Vector(distance, distanceTwoBss, 0.0));
192 obssMobility.SetPositionAllocator(obssPositionAlloc);
193 obssMobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
194 obssMobility.Install(c2);
195 }
196
198 wifi.SetStandard(WIFI_STANDARD_80211ax);
199
200 YansWifiPhyHelper wifiPhy;
202
203 YansWifiChannelHelper wifiChannel;
204 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
205 wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
206 wifiPhy.SetChannel(wifiChannel.Create());
207
208 // Add a mac and disable rate control
209 WifiMacHelper wifiMac;
210 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
211 "DataMode",
212 StringValue("HeMcs0"),
213 "ControlMode",
214 StringValue("HeMcs0"));
215
216 // Setup the rest of the MAC
217 Ssid ssid = Ssid("wifi-default");
218 // setup STA
219 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
220 NetDeviceContainer staDevice = wifi.Install(wifiPhy, wifiMac, c.Get(0));
221 NetDeviceContainer devices = staDevice;
222 // setup AP to beacon roughly once per second (must be a multiple of 1024 us)
223 wifiMac.SetType("ns3::ApWifiMac",
224 "Ssid",
225 SsidValue(ssid),
226 "BeaconInterval",
227 TimeValue(MilliSeconds(1024)));
228 NetDeviceContainer apDevice = wifi.Install(wifiPhy, wifiMac, c.Get(1));
229 devices.Add(apDevice);
230
231 NetDeviceContainer obssDevices;
232 if (enableTwoBss)
233 {
234 ssid = Ssid("obss");
235 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
236 NetDeviceContainer obssStaDevice = wifi.Install(wifiPhy, wifiMac, c2.Get(0));
237 obssDevices = obssStaDevice;
238 wifiMac.SetType("ns3::ApWifiMac",
239 "Ssid",
240 SsidValue(ssid),
241 "BeaconInterval",
242 TimeValue(MilliSeconds(1024)));
243 NetDeviceContainer obssApDevice = wifi.Install(wifiPhy, wifiMac, c2.Get(1));
244 obssDevices.Add(obssApDevice);
245 }
246
248 internet.Install(c);
249
251 ipv4.SetBase("10.1.1.0", "255.255.255.0");
252 Ipv4InterfaceContainer i = ipv4.Assign(devices);
253
254 if (enableTwoBss)
255 {
256 internet.Install(c2);
257 ipv4.SetBase("10.1.2.0", "255.255.255.0");
258 ipv4.Assign(obssDevices);
259 }
260
261 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
262 Ptr<Socket> recvSink = Socket::CreateSocket(c.Get(1), tid);
264 recvSink->Bind(local);
265 recvSink->SetRecvCallback(MakeCallback(&ReceivePacket));
266
267 Ptr<Socket> source = Socket::CreateSocket(c.Get(0), tid);
268 InetSocketAddress remote = InetSocketAddress(Ipv4Address("10.1.1.2"), 80);
269 source->Connect(remote);
270
271 Ptr<Socket> obssRecvSink;
272 Ptr<Socket> obssSource;
273 if (enableTwoBss)
274 {
275 obssRecvSink = Socket::CreateSocket(c2.Get(1), tid);
277 obssRecvSink->Bind(obssLocal);
278 obssRecvSink->SetRecvCallback(MakeCallback(&ReceiveObssPacket));
279
280 obssSource = Socket::CreateSocket(c2.Get(0), tid);
281 InetSocketAddress obssRemote = InetSocketAddress(Ipv4Address("10.1.2.2"), 80);
282 obssSource->Connect(obssRemote);
283 }
284
285 // Use the NeighborCacheHelper to avoid ARP messages (ARP replies, since they are unicast,
286 // count in the statistics. The cache operation must be scheduled after WifiNetDevices are
287 // started, until issue #851 is fixed. The indirection through a normal function is
288 // necessary because NeighborCacheHelper::PopulateNeighborCache() is overloaded
290
291 // Tracing
292 wifiPhy.EnablePcap("wifi-phy-rx-trace-example", devices);
294 wifiPhy.EnableAsciiAll(asciiTrace.CreateFileStream("wifi-phy-rx-trace-example.tr"));
295
296 // Logging configuration
297 if (logging)
298 {
299 WifiHelper::EnableLogComponents(LOG_LEVEL_INFO); // Turn on all Wifi logging
300 }
301 if (verbose)
302 {
304 "WifiPhyRxTraceExample",
306 }
307 WifiPhyRxTraceHelper rxTraceHelper;
308 // Enable trace helper only on one BSS
309 rxTraceHelper.Enable(c);
310 rxTraceHelper.Start(MilliSeconds(999)); // 1 ms before applications
311 // The last packet will be sent at time 1 sec. + (numPackets - 1) * interval
312 // Configure the stop time to be 1 sec. later than this.
313 Time stopTime = Seconds(1) + (numPackets - 1) * interval + Seconds(1);
314 rxTraceHelper.Stop(stopTime);
315
316 Simulator::ScheduleWithContext(source->GetNode()->GetId(),
317 Seconds(1.0),
319 source,
321 numPackets,
322 interval);
323
324 if (enableTwoBss)
325 {
326 Simulator::ScheduleWithContext(obssSource->GetNode()->GetId(),
327 Seconds(1.5),
329 obssSource,
331 numPackets,
332 interval);
333 }
334
337
338 // The following provide some examples of how to access and print the trace helper contents.
339
340 std::cout << "*** Print statistics for all nodes using built-in print method:" << std::endl;
341 rxTraceHelper.PrintStatistics();
342 std::cout << std::endl;
343
344 std::cout << "*** Print statistics for the STA only using built-in print method:" << std::endl;
345 rxTraceHelper.PrintStatistics(c.Get(0)->GetId());
346 std::cout << std::endl;
347
348 std::cout << "*** Print statistics for the AP only using built-in print method:" << std::endl;
349 rxTraceHelper.PrintStatistics(c.Get(1));
350 std::cout << std::endl;
351
352 std::cout << "*** Get statistics object and print the fields one-by-one:" << std::endl;
353 auto stats = rxTraceHelper.GetStatistics();
354 std::cout << " overlapppingPpdu: " << stats.m_overlappingPpdus << std::endl;
355 std::cout << " nonOverlapppingPpdu: " << stats.m_nonOverlappingPpdus << std::endl;
356 std::cout << " receivedPpdus: " << stats.m_receivedPpdus << std::endl;
357 std::cout << " failedPpdus: " << stats.m_failedPpdus << std::endl;
358 std::cout << " receivedMpdus: " << stats.m_receivedMpdus << std::endl;
359 std::cout << " failedMpdus: " << stats.m_failedMpdus << std::endl;
360 std::cout << std::endl;
361
362 std::cout << "*** Get vector of reception records and print out some fields:" << std::endl;
363 auto optionalRecords = rxTraceHelper.GetPpduRecords(1);
364 if (optionalRecords.has_value())
365 {
366 auto records = optionalRecords->get(); // const std::vector<WifiPpduRxRecord>&
367 std::cout << "*** Records vector has size of " << records.size() << std::endl;
368 if (!records.empty())
369 {
370 // First record
371 std::cout << " First record:" << std::endl;
372 std::cout << " first PPDU's RSSI (dBm): " << records[0].m_rssi << std::endl;
373 std::cout << " first PPDU's receiver ID: " << records[0].m_receiverId << std::endl;
374 std::cout << " first PPDU's sender ID: " << records[0].m_senderId << std::endl;
375 std::cout << " first PPDU's start time: " << records[0].m_startTime.GetSeconds()
376 << std::endl;
377 std::cout << " first PPDU's end time: " << records[0].m_endTime.GetSeconds()
378 << std::endl;
379 std::cout << " first PPDU's number of MPDUs: " << records[0].m_statusPerMpdu.size()
380 << std::endl;
381 std::cout << " first PPDU's sender device ID: " << records[0].m_senderDeviceId
382 << std::endl;
383 }
384 if (records.size() > 1)
385 {
386 // Second record
387 std::cout << " Second record:" << std::endl;
388 std::cout << " second PPDU's RSSI (dBm): " << records[1].m_rssi << std::endl;
389 std::cout << " second PPDU's receiver ID: " << records[1].m_receiverId << std::endl;
390 std::cout << " second PPDU's sender ID: " << records[1].m_senderId << std::endl;
391 std::cout << " second PPDU's start time: " << records[1].m_startTime.GetSeconds()
392 << std::endl;
393 std::cout << " second PPDU's end time: " << records[1].m_endTime.GetSeconds()
394 << std::endl;
395 std::cout << " second PPDU's number of MPDUs: " << records[1].m_statusPerMpdu.size()
396 << std::endl;
397 std::cout << " second PPDU's sender device ID: " << records[1].m_senderDeviceId
398 << std::endl;
399 }
400 if (records.size() > 2)
401 {
402 // Third record
403 std::cout << " Third record:" << std::endl;
404 std::cout << " third PPDU's RSSI (dBm): " << records[2].m_rssi << std::endl;
405 std::cout << " third PPDU's receiver ID: " << records[2].m_receiverId << std::endl;
406 std::cout << " third PPDU's sender ID: " << records[2].m_senderId << std::endl;
407 std::cout << " third PPDU's start time: " << records[2].m_startTime.GetSeconds()
408 << std::endl;
409 std::cout << " third PPDU's end time: " << records[2].m_endTime.GetSeconds()
410 << std::endl;
411 std::cout << " third PPDU's number of MPDUs: " << records[2].m_statusPerMpdu.size()
412 << std::endl;
413 std::cout << " third PPDU's sender device ID: " << records[2].m_senderDeviceId
414 << std::endl;
415 }
416 std::cout << std::endl;
417 }
418 else
419 {
420 std::cout << "*** Records vector is empty" << std::endl;
421 std::cout << std::endl;
422 }
423
425
426 return 0;
427}
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.
Definition: trace-helper.h:174
Parse command-line arguments.
Definition: command-line.h:232
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.
Definition: ipv4-address.h:42
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:117
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.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void ScheduleWithContext(uint32_t context, const Time &delay, FUNC f, Ts &&... args)
Schedule an event with the given context.
Definition: simulator.h:588
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:72
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
static void EnableLogComponents(LogLevel logLevel=LOG_LEVEL_ALL)
Helper to enable all WifiNetDevice log components with one statement.
Definition: wifi-helper.cc:880
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.
Definition: wifi-helper.cc:543
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
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:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
@ WIFI_STANDARD_80211ax
ns devices
Definition: first.py:42
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:302
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:700
LogLevel
Logging severity classes and levels.
Definition: log.h:94
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition: log.h:118
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:40
ns wifi
Definition: third.py:95
ns ssid
Definition: third.py:93
ns mobility
Definition: third.py:103
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.