A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-vht-network.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 SEBASTIEN DERONNE
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sebastien Deronne <sebastien.deronne@gmail.com>
7 */
8
9#include "ns3/attribute-container.h"
10#include "ns3/boolean.h"
11#include "ns3/command-line.h"
12#include "ns3/config.h"
13#include "ns3/double.h"
14#include "ns3/internet-stack-helper.h"
15#include "ns3/ipv4-address-helper.h"
16#include "ns3/ipv4-global-routing-helper.h"
17#include "ns3/log.h"
18#include "ns3/mobility-helper.h"
19#include "ns3/multi-model-spectrum-channel.h"
20#include "ns3/neighbor-cache-helper.h"
21#include "ns3/on-off-helper.h"
22#include "ns3/packet-sink-helper.h"
23#include "ns3/packet-sink.h"
24#include "ns3/spectrum-wifi-helper.h"
25#include "ns3/ssid.h"
26#include "ns3/string.h"
27#include "ns3/udp-client-server-helper.h"
28#include "ns3/udp-server.h"
29#include "ns3/uinteger.h"
30#include "ns3/vht-phy.h"
31#include "ns3/wifi-static-setup-helper.h"
32#include "ns3/yans-wifi-channel.h"
33#include "ns3/yans-wifi-helper.h"
34
35#include <algorithm>
36#include <vector>
37
38// This is a simple example in order to show how to configure an IEEE 802.11ac Wi-Fi network.
39//
40// It outputs the UDP or TCP goodput for every VHT MCS value, which depends on the MCS value (0 to
41// 9, where 9 is forbidden when the channel width is 20 MHz), the channel width (20, 40, 80 or 160
42// MHz) and the guard interval (long or short). The PHY bitrate is constant over all the simulation
43// run. The user can also specify the distance between the access point and the station: the larger
44// the distance the smaller the goodput.
45//
46// The simulation assumes a single station in an infrastructure network:
47//
48// STA AP
49// * *
50// | |
51// n1 n2
52//
53// Packets in this simulation belong to BestEffort Access Class (AC_BE).
54
55using namespace ns3;
56
57NS_LOG_COMPONENT_DEFINE("vht-wifi-network");
58
59int
60main(int argc, char* argv[])
61{
62 bool udp{true};
63 bool useRts{false};
64 bool use80Plus80{false};
65 Time simulationTime{"10s"};
66 bool staticSetup{true};
67 auto clientAppStartTime = Seconds(1);
68 meter_u distance{1.0};
69 std::string mcsStr;
70 std::vector<uint64_t> mcsValues;
71 std::string phyModel{"Yans"};
72 int channelWidth{-1}; // in MHz, -1 indicates an unset value
73 int guardInterval{-1}; // in nanoseconds, -1 indicates an unset value
74 double minExpectedThroughput{0.0};
75 double maxExpectedThroughput{0.0};
76
77 CommandLine cmd(__FILE__);
78 cmd.AddValue("staticSetup",
79 "Whether devices are configured using the static setup helper",
80 staticSetup);
81 cmd.AddValue("distance",
82 "Distance in meters between the station and the access point",
83 distance);
84 cmd.AddValue("simulationTime", "Simulation time", simulationTime);
85 cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
86 cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
87 cmd.AddValue("use80Plus80", "Enable/disable use of 80+80 MHz", use80Plus80);
88 cmd.AddValue(
89 "mcs",
90 "list of comma separated MCS values to test; if unset, all MCS values (0-9) are tested",
91 mcsStr);
92 cmd.AddValue("phyModel",
93 "PHY model to use (Yans or Spectrum). If 80+80 MHz is enabled, then Spectrum is "
94 "automatically selected",
95 phyModel);
96 cmd.AddValue("channelWidth",
97 "if set, limit testing to a specific channel width expressed in MHz (20, 40, 80 "
98 "or 160 MHz)",
99 channelWidth);
100 cmd.AddValue("guardInterval",
101 "if set, limit testing to a specific guard interval duration expressed in "
102 "nanoseconds (800 or 400 ns)",
103 guardInterval);
104 cmd.AddValue("minExpectedThroughput",
105 "if set, simulation fails if the lowest throughput is below this value",
106 minExpectedThroughput);
107 cmd.AddValue("maxExpectedThroughput",
108 "if set, simulation fails if the highest throughput is above this value",
109 maxExpectedThroughput);
110 cmd.Parse(argc, argv);
111
112 if (phyModel != "Yans" && phyModel != "Spectrum")
113 {
114 NS_ABORT_MSG("Invalid PHY model (must be Yans or Spectrum)");
115 }
116 if (use80Plus80)
117 {
118 // SpectrumWifiPhy is required for 80+80 MHz
119 phyModel = "Spectrum";
120 }
121
122 if (useRts)
123 {
124 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
125 }
126
127 double prevThroughput[8] = {0};
128
129 std::cout << "MCS value"
130 << "\t\t"
131 << "Channel width"
132 << "\t\t"
133 << "short GI"
134 << "\t\t"
135 << "Throughput" << '\n';
136 uint8_t minMcs = 0;
137 uint8_t maxMcs = 9;
138
139 if (mcsStr.empty())
140 {
141 for (uint8_t mcs = minMcs; mcs <= maxMcs; ++mcs)
142 {
143 mcsValues.push_back(mcs);
144 }
145 }
146 else
147 {
148 AttributeContainerValue<UintegerValue, ',', std::vector> attr;
150 checker->SetItemChecker(MakeUintegerChecker<uint8_t>());
151 attr.DeserializeFromString(mcsStr, checker);
152 mcsValues = attr.Get();
153 std::sort(mcsValues.begin(), mcsValues.end());
154 }
155
156 int minChannelWidth = 20;
157 int maxChannelWidth = 160;
158 if ((channelWidth != -1) &&
159 ((channelWidth < minChannelWidth) || (channelWidth > maxChannelWidth)))
160 {
161 NS_FATAL_ERROR("Invalid channel width: " << channelWidth << " MHz");
162 }
163 if (channelWidth >= minChannelWidth && channelWidth <= maxChannelWidth)
164 {
165 minChannelWidth = channelWidth;
166 maxChannelWidth = channelWidth;
167 }
168 int minGi = 400;
169 int maxGi = 800;
170 if (guardInterval >= minGi && guardInterval <= maxGi)
171 {
172 minGi = guardInterval;
173 maxGi = guardInterval;
174 }
175
176 for (const auto mcs : mcsValues)
177 {
178 uint8_t index = 0;
179 double previous = 0;
180 for (int width = minChannelWidth; width <= maxChannelWidth; width *= 2) // MHz
181 {
182 if (mcs == 9 && width == 20)
183 {
184 continue;
185 }
186 const auto is80Plus80 = (use80Plus80 && (width == 160));
187 const std::string widthStr = is80Plus80 ? "80+80" : std::to_string(width);
188 const auto segmentWidthStr = is80Plus80 ? "80" : widthStr;
189 for (int gi = maxGi; gi >= minGi; gi /= 2) // Nanoseconds
190 {
191 const auto sgi = (gi == 400);
192 uint32_t payloadSize; // 1500 byte IP packet
193 if (udp)
194 {
195 payloadSize = 1472; // bytes
196 }
197 else
198 {
199 payloadSize = 1448; // bytes
200 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
201 }
202
203 NodeContainer wifiStaNode;
204 wifiStaNode.Create(1);
206 wifiApNode.Create(1);
207
208 NetDeviceContainer apDevice;
209 NetDeviceContainer staDevice;
212 std::string channelStr{"{0, " + segmentWidthStr + ", BAND_5GHZ, 0}"};
213
214 std::ostringstream ossControlMode;
215 auto nonHtRefRateMbps = VhtPhy::GetNonHtReferenceRate(mcs) / 1e6;
216 ossControlMode << "OfdmRate" << nonHtRefRateMbps << "Mbps";
217
218 std::ostringstream ossDataMode;
219 ossDataMode << "VhtMcs" << mcs;
220
221 if (is80Plus80)
222 {
223 channelStr += std::string(";") + channelStr;
224 }
225
226 wifi.SetStandard(WIFI_STANDARD_80211ac);
227 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
228 "DataMode",
229 StringValue(ossDataMode.str()),
230 "ControlMode",
231 StringValue(ossControlMode.str()));
232
233 // Set guard interval
234 wifi.ConfigHtOptions("ShortGuardIntervalSupported", BooleanValue(sgi));
235
236 Ssid ssid = Ssid("ns3-80211ac");
237
238 if (phyModel == "Spectrum")
239 {
240 auto spectrumChannel = CreateObject<MultiModelSpectrumChannel>();
242 spectrumChannel->AddPropagationLossModel(lossModel);
243
245 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
246 phy.SetChannel(spectrumChannel);
247
248 phy.Set("ChannelSettings",
249 StringValue("{0, " + std::to_string(width) + ", BAND_5GHZ, 0}"));
250
251 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
252 staDevice = wifi.Install(phy, mac, wifiStaNode);
253
254 mac.SetType("ns3::ApWifiMac",
255 "EnableBeaconJitter",
256 BooleanValue(false),
257 "BeaconGeneration",
258 BooleanValue(!staticSetup),
259 "Ssid",
260 SsidValue(ssid));
261 apDevice = wifi.Install(phy, mac, wifiApNode);
262 }
263 else
264 {
267 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
268 phy.SetChannel(channel.Create());
269
270 phy.Set("ChannelSettings",
271 StringValue("{0, " + std::to_string(width) + ", BAND_5GHZ, 0}"));
272
273 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
274 staDevice = wifi.Install(phy, mac, wifiStaNode);
275
276 mac.SetType("ns3::ApWifiMac",
277 "EnableBeaconJitter",
278 BooleanValue(false),
279 "Ssid",
280 SsidValue(ssid));
281 apDevice = wifi.Install(phy, mac, wifiApNode);
282 }
283
284 int64_t streamNumber = 150;
285 streamNumber += WifiHelper::AssignStreams(apDevice, streamNumber);
286 streamNumber += WifiHelper::AssignStreams(staDevice, streamNumber);
287
288 // mobility.
291
292 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
293 positionAlloc->Add(Vector(distance, 0.0, 0.0));
294 mobility.SetPositionAllocator(positionAlloc);
295
296 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
297
298 mobility.Install(wifiApNode);
299 mobility.Install(wifiStaNode);
300
301 if (staticSetup)
302 {
303 /* static setup of association and BA agreements */
304 auto apDev = DynamicCast<WifiNetDevice>(apDevice.Get(0));
305 NS_ASSERT(apDev);
307 WifiStaticSetupHelper::SetStaticBlockAck(apDev, staDevice, {0});
308 clientAppStartTime = MilliSeconds(1);
309 }
310
311 /* Internet stack*/
313 stack.Install(wifiApNode);
314 stack.Install(wifiStaNode);
315 streamNumber += stack.AssignStreams(wifiApNode, streamNumber);
316 streamNumber += stack.AssignStreams(wifiStaNode, streamNumber);
317
319 address.SetBase("192.168.1.0", "255.255.255.0");
320 Ipv4InterfaceContainer staNodeInterface;
321 Ipv4InterfaceContainer apNodeInterface;
322
323 staNodeInterface = address.Assign(staDevice);
324 apNodeInterface = address.Assign(apDevice);
325
326 if (staticSetup)
327 {
328 /* static setup of ARP cache */
329 NeighborCacheHelper nbCache;
330 nbCache.PopulateNeighborCache();
331 }
332
333 /* Setting applications */
334 const auto maxLoad = VhtPhy::GetDataRate(mcs,
335 MHz_u{static_cast<double>(width)},
336 NanoSeconds(sgi ? 400 : 800),
337 1);
338 ApplicationContainer serverApp;
339 if (udp)
340 {
341 // UDP flow
342 uint16_t port = 9;
344 serverApp = server.Install(wifiStaNode.Get(0));
345 streamNumber += server.AssignStreams(wifiStaNode.Get(0), streamNumber);
346
347 serverApp.Start(Seconds(0));
348 serverApp.Stop(simulationTime + clientAppStartTime);
349 const auto packetInterval = payloadSize * 8.0 / maxLoad;
350
351 UdpClientHelper client(staNodeInterface.GetAddress(0), port);
352 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
353 client.SetAttribute("Interval", TimeValue(Seconds(packetInterval)));
354 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
355 ApplicationContainer clientApp = client.Install(wifiApNode.Get(0));
356 streamNumber += client.AssignStreams(wifiApNode.Get(0), streamNumber);
357
358 clientApp.Start(clientAppStartTime);
359 clientApp.Stop(simulationTime + clientAppStartTime);
360 }
361 else
362 {
363 // TCP flow
364 uint16_t port = 50000;
366 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
367 serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
368 streamNumber +=
369 packetSinkHelper.AssignStreams(wifiStaNode.Get(0), streamNumber);
370
371 serverApp.Start(Seconds(0));
372 serverApp.Stop(simulationTime + clientAppStartTime);
373
374 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
375 onoff.SetAttribute("OnTime",
376 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
377 onoff.SetAttribute("OffTime",
378 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
379 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
380 onoff.SetAttribute("DataRate", DataRateValue(maxLoad));
382 InetSocketAddress(staNodeInterface.GetAddress(0), port));
383 onoff.SetAttribute("Remote", remoteAddress);
384 ApplicationContainer clientApp = onoff.Install(wifiApNode.Get(0));
385 streamNumber += onoff.AssignStreams(wifiApNode.Get(0), streamNumber);
386
387 clientApp.Start(clientAppStartTime);
388 clientApp.Stop(simulationTime + clientAppStartTime);
389 }
390
392
393 Simulator::Stop(simulationTime + clientAppStartTime);
395
396 auto rxBytes = 0.0;
397 if (udp)
398 {
399 rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
400 }
401 else
402 {
403 rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
404 }
405 auto throughput = (rxBytes * 8) / simulationTime.GetMicroSeconds(); // Mbit/s
406
408
409 std::cout << +mcs << "\t\t\t" << widthStr << " MHz\t\t"
410 << (widthStr.size() > 3 ? "" : "\t") << (sgi ? "400 ns" : "800 ns")
411 << "\t\t\t" << throughput << " Mbit/s" << std::endl;
412
413 // test first element
414 if (mcs == minMcs && width == 20 && !sgi)
415 {
416 if (throughput < minExpectedThroughput)
417 {
418 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
419 exit(1);
420 }
421 }
422 // test last element
423 if (mcs == maxMcs && width == 160 && sgi)
424 {
425 if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
426 {
427 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
428 exit(1);
429 }
430 }
431 // test previous throughput is smaller (for the same mcs)
432 if (throughput > previous)
433 {
434 previous = throughput;
435 }
436 else
437 {
438 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
439 exit(1);
440 }
441 // test previous throughput is smaller (for the same channel width and GI)
442 if (throughput > prevThroughput[index])
443 {
444 prevThroughput[index] = throughput;
445 }
446 else
447 {
448 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
449 exit(1);
450 }
451 index++;
452 }
453 }
454 }
455 return 0;
456}
a polymophic address class
Definition address.h:90
AttributeValue implementation for Address.
Definition address.h:275
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.
A container for one type of attribute.
AttributeValue implementation for Boolean.
Definition boolean.h:26
Parse command-line arguments.
AttributeValue implementation for DataRate.
Definition data-rate.h:285
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.
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
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
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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.
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.
Definition ptr.h:67
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
Make it easy to create and manage PHY objects for the spectrum model.
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
AttributeValue implementation for Ssid.
Definition ssid.h:85
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
AttributeValue implementation for Time.
Definition nstime.h:1456
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Create a server application which waits for input UDP packets and uses the information carried into t...
Hold an unsigned integer type.
Definition uinteger.h:34
static uint64_t GetDataRate(uint8_t mcsValue, MHz_u channelWidth, Time guardInterval, uint8_t nss)
Return the data rate corresponding to the supplied VHT MCS index, channel width, guard interval,...
Definition vht-phy.cc:438
static uint64_t GetNonHtReferenceRate(uint8_t mcsValue)
Calculate the rate in bps of the non-HT Reference Rate corresponding to the supplied VHT MCS index.
Definition vht-phy.cc:468
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 ...
create MAC layers for a ns3::WifiNetDevice.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
static void SetStaticAssociation(Ptr< WifiNetDevice > bssDev, const NetDeviceContainer &clientDevs)
Bypass static capabilities exchange for input devices.
static void SetStaticBlockAck(Ptr< WifiNetDevice > apDev, const NetDeviceContainer &clientDevs, const std::set< tid_t > &tids, std::optional< Mac48Address > gcrGroupAddr=std::nullopt)
Bypass ADDBA Request-Response exchange sequence between AP and STAs for given TIDs.
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.
uint16_t port
Definition dsdv-manet.cc:33
#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
Ptr< AttributeChecker > MakeAttributeContainerChecker()
Make uninitialized AttributeContainerChecker using explicit types.
Ptr< const AttributeChecker > MakeUintegerChecker()
Definition uinteger.h:85
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:886
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#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 NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1405
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1369
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
@ WIFI_STANDARD_80211ac
address
Definition first.py:36
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
double MHz_u
MHz weak type.
Definition wifi-units.h:31
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:585
double meter_u
meter weak type
Definition wifi-units.h:32
ssid
Definition third.py:82
channel
Definition third.py:77
mac
Definition third.py:81
wifi
Definition third.py:84
wifiApNode
Definition third.py:75
mobility
Definition third.py:92
phy
Definition third.py:78
std::ofstream throughput