A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-co-trace-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 University of Washington (updated to 802.11ax standard)
3 * Copyright (c) 2009 The Boeing Company
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 */
7
8// The purpose of this example is to illustrate basic use of the
9// WifiCoTraceHelper on a simple example program.
10//
11// This script configures four 802.11ax Wi-Fi STAs on a YansWifiChannel,
12// with devices in infrastructure mode, and each STA sends a saturating load
13// of UDP datagrams to the AP for a specified simulation duration. A simple
14// free-space path loss (Friis) propagation loss model is configured.
15// The lowest MCS ("HeMcs0") value is configured.
16//
17// At the end of the simulation, a channel occupancy report is printed for
18// each STA and for the AP. There are two program options:
19// -- duration:
20// -- useDifferentAc: (true or false)
21//
22// If 'useDifferentAc' has the value false, all STAs will have the same EDCA parameters
23// for best effort) and their channel utilization (the TX time output of the
24// channel access helper) will be close to equal. If 'useDifferentAc' is true,
25// then two of the four STAs will instead be configured to use the voice
26// access category, and channel utilization will be different due to the
27// different EDCA parameters.
28
29#include "ns3/boolean.h"
30#include "ns3/command-line.h"
31#include "ns3/config.h"
32#include "ns3/double.h"
33#include "ns3/internet-stack-helper.h"
34#include "ns3/ipv4-address-helper.h"
35#include "ns3/log.h"
36#include "ns3/mobility-helper.h"
37#include "ns3/mobility-model.h"
38#include "ns3/names.h"
39#include "ns3/neighbor-cache-helper.h"
40#include "ns3/on-off-helper.h"
41#include "ns3/packet-sink-helper.h"
42#include "ns3/ssid.h"
43#include "ns3/string.h"
44#include "ns3/uinteger.h"
45#include "ns3/wifi-co-trace-helper.h"
46#include "ns3/wifi-phy-rx-trace-helper.h"
47#include "ns3/yans-wifi-channel.h"
48#include "ns3/yans-wifi-helper.h"
49
50using namespace ns3;
51
52NS_LOG_COMPONENT_DEFINE("WifiCoTraceExample");
53
54// Function for runtime manual ARP configuration
55void
57{
58 NeighborCacheHelper neighborCache;
59 neighborCache.PopulateNeighborCache();
60}
61
62int
63main(int argc, char* argv[])
64{
65 bool useDifferentAc = false;
66 Time duration{Seconds(10)};
67 double distance = 1; // meters
68
69 CommandLine cmd(__FILE__);
70 cmd.AddValue("useDifferentAc",
71 "Uses VO AC on 2 STAs and BE on rest if true. Uses BE AC on all 4 STAs if false.",
72 useDifferentAc);
73 cmd.AddValue("duration", "Duration of data transfer", duration);
74 cmd.Parse(argc, argv);
75
76 NodeContainer apNode(1);
77 Names::Add("AP", apNode.Get(0));
78 NodeContainer staNodes(4);
79 Names::Add("STA0", staNodes.Get(0));
80 Names::Add("STA1", staNodes.Get(1));
81 Names::Add("STA2", staNodes.Get(2));
82 Names::Add("STA3", staNodes.Get(3));
83
86 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
87 mobility.SetPositionAllocator(positionAlloc);
88 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
89 mobility.Install(apNode);
91 positionAlloc->Add(Vector(distance, 0.0, 0.0));
92 positionAlloc->Add(Vector(0.0, distance, 0.0));
93 positionAlloc->Add(Vector(0.0, -distance, 0.0));
94 positionAlloc->Add(Vector(-distance, 0.0, 0.0));
95 mobility.Install(staNodes);
96
98 wifi.SetStandard(WIFI_STANDARD_80211ax);
99
100 YansWifiPhyHelper wifiPhy;
101 YansWifiChannelHelper wifiChannel;
102 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
103 wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel");
104 wifiPhy.SetChannel(wifiChannel.Create());
105
106 // Add a mac and disable rate control
107 WifiMacHelper wifiMac;
108 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
109 "DataMode",
110 StringValue("HeMcs0"),
111 "ControlMode",
112 StringValue("HeMcs0"));
113
114 // Setup the rest of the MAC
115 Ssid ssid = Ssid("wifi-default");
116 // setup AP to beacon roughly once per second (must be a multiple of 1024 us)
117 wifiMac.SetType("ns3::ApWifiMac",
118 "Ssid",
119 SsidValue(ssid),
120 "QosSupported",
121 BooleanValue(true),
122 "BeaconInterval",
123 TimeValue(MilliSeconds(1024)));
124 NetDeviceContainer apDevice = wifi.Install(wifiPhy, wifiMac, apNode);
125
126 // setup STA and disable the possible loss of association due to missed beacons
127 wifiMac.SetType("ns3::StaWifiMac",
128 "Ssid",
129 SsidValue(ssid),
130 "QosSupported",
131 BooleanValue(true),
132 "MaxMissedBeacons",
133 UintegerValue(std::numeric_limits<uint32_t>::max()));
134 NetDeviceContainer staDevices = wifi.Install(wifiPhy, wifiMac, staNodes);
135
136 NetDeviceContainer allDevices;
137 allDevices.Add(apDevice);
138 allDevices.Add(staDevices);
139
141 internet.Install(apNode);
142 internet.Install(staNodes);
143
145 ipv4.SetBase("10.1.1.0", "255.255.255.0");
146 Ipv4InterfaceContainer i = ipv4.Assign(allDevices);
147
148 uint16_t portNumber = 9;
149 std::vector<uint8_t> tosValues = {0x70, 0x28, 0xb8, 0xc0}; // AC_BE, AC_BK, AC_VI, AC_VO
150 auto ipv4ap = apNode.Get(0)->GetObject<Ipv4>();
151 const auto address = ipv4ap->GetAddress(1, 0).GetLocal();
152
153 ApplicationContainer sourceApplications;
154 ApplicationContainer sinkApplications;
155 for (uint32_t i = 0; i < 4; i++)
156 {
157 InetSocketAddress sinkAddress(address, portNumber + i);
158 PacketSinkHelper packetSinkHelper("ns3::UdpSocketFactory", sinkAddress);
159 sinkApplications.Add(packetSinkHelper.Install(apNode.Get(0)));
160 OnOffHelper onOffHelper("ns3::UdpSocketFactory", sinkAddress);
161 onOffHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
162 onOffHelper.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
163 onOffHelper.SetAttribute("DataRate", DataRateValue(2000000)); // bits/sec
164 onOffHelper.SetAttribute("PacketSize", UintegerValue(1472)); // bytes
165 if (!useDifferentAc)
166 {
167 onOffHelper.SetAttribute("Tos", UintegerValue(tosValues[0])); // AC_BE
168 }
169 else
170 {
171 onOffHelper.SetAttribute("Tos",
172 UintegerValue(tosValues[3 * (i % 2)])); // AC_BE and AC_VO
173 }
174 sourceApplications.Add(onOffHelper.Install(staNodes.Get(i)));
175 }
176
177 sinkApplications.Start(Seconds(0.0));
178 sinkApplications.Stop(Seconds(1.0) + duration + MilliSeconds(20));
179 sourceApplications.Start(Seconds(1.0));
180 sourceApplications.Stop(Seconds(1.0) + duration);
181
182 // Use the NeighborCacheHelper to avoid ARP messages (ARP replies, since they are unicast,
183 // count in the statistics. The cache operation must be scheduled after WifiNetDevices are
184 // started, until issue #851 is fixed. The indirection through a normal function is
185 // necessary because NeighborCacheHelper::PopulateNeighborCache() is overloaded
187
188 WifiCoTraceHelper wifiCoTraceHelper(Seconds(1), Seconds(1) + duration);
189 wifiCoTraceHelper.Enable(allDevices);
190
191 Simulator::Stop(duration + Seconds(2));
193
194 // The following provide some examples of how to access and print the trace helper contents.
195 std::cout << "*** Print statistics for all nodes using built-in print method:" << std::endl;
196 wifiCoTraceHelper.PrintStatistics(std::cout);
197
198 std::cout << "*** Print the statistics in your own way. Here, just sum the STAs total TX time:"
199 << std::endl
200 << std::endl;
201
202 auto records = wifiCoTraceHelper.GetDeviceRecords();
203 Time sumStaTxTime;
204 for (const auto& it : records)
205 {
206 if (it.m_nodeId > 0)
207 {
208 const auto it2 = it.m_linkStateDurations.at(0).find(WifiPhyState::TX);
209 if (it2 != it.m_linkStateDurations.at(0).end())
210 {
211 sumStaTxTime += it2->second;
212 }
213 }
214 }
215
216 std::cout << "Sum of STA time in TX state is " << sumStaTxTime.As(Time::S) << std::endl;
217
219
220 return 0;
221}
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
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.
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition ipv4.h:69
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
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 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
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
TimeWithUnit As(const Unit unit=Time::AUTO) const
Attach a unit to a Time, to facilitate output in a specific unit.
Definition time.cc:404
@ S
second
Definition nstime.h:105
Hold an unsigned integer type.
Definition uinteger.h:34
Track channel occupancy durations for WifiNetDevice.
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
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)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1356
@ WIFI_STANDARD_80211ax
address
Definition first.py:36
Every class exported by the ns3 library is enclosed in the ns3 namespace.
staDevices
Definition third.py:87
ssid
Definition third.py:82
wifi
Definition third.py:84
mobility
Definition third.py:92
void PopulateNeighborCache()