A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-he-network.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2016 SEBASTIEN DERONNE
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Sebastien Deronne <sebastien.deronne@gmail.com>
18 */
19
20#include "ns3/boolean.h"
21#include "ns3/command-line.h"
22#include "ns3/config.h"
23#include "ns3/double.h"
24#include "ns3/enum.h"
25#include "ns3/he-phy.h"
26#include "ns3/internet-stack-helper.h"
27#include "ns3/ipv4-address-helper.h"
28#include "ns3/ipv4-global-routing-helper.h"
29#include "ns3/log.h"
30#include "ns3/mobility-helper.h"
31#include "ns3/multi-model-spectrum-channel.h"
32#include "ns3/on-off-helper.h"
33#include "ns3/packet-sink-helper.h"
34#include "ns3/packet-sink.h"
35#include "ns3/rng-seed-manager.h"
36#include "ns3/spectrum-wifi-helper.h"
37#include "ns3/ssid.h"
38#include "ns3/string.h"
39#include "ns3/udp-client-server-helper.h"
40#include "ns3/uinteger.h"
41#include "ns3/wifi-acknowledgment.h"
42#include "ns3/yans-wifi-channel.h"
43#include "ns3/yans-wifi-helper.h"
44
45#include <functional>
46
47// This is a simple example in order to show how to configure an IEEE 802.11ax Wi-Fi network.
48//
49// It outputs the UDP or TCP goodput for every HE MCS value, which depends on the MCS value (0 to
50// 11), the channel width (20, 40, 80 or 160 MHz) and the guard interval (800ns, 1600ns or 3200ns).
51// The PHY bitrate is constant over all the simulation run. The user can also specify the distance
52// between the access point and the station: the larger the distance the smaller the goodput.
53//
54// The simulation assumes a configurable number of stations in an infrastructure network:
55//
56// STA AP
57// * *
58// | |
59// n1 n2
60//
61// Packets in this simulation belong to BestEffort Access Class (AC_BE).
62// By selecting an acknowledgment sequence for DL MU PPDUs, it is possible to aggregate a
63// Round Robin scheduler to the AP, so that DL MU PPDUs are sent by the AP via DL OFDMA.
64
65using namespace ns3;
66
67NS_LOG_COMPONENT_DEFINE("he-wifi-network");
68
69int
70main(int argc, char* argv[])
71{
72 bool udp{true};
73 bool downlink{true};
74 bool useRts{false};
75 bool useExtendedBlockAck{false};
76 double simulationTime{10}; // seconds
77 double distance{1.0}; // meters
78 double frequency{5}; // whether 2.4, 5 or 6 GHz
79 std::size_t nStations{1};
80 std::string dlAckSeqType{"NO-OFDMA"};
81 bool enableUlOfdma{false};
82 bool enableBsrp{false};
83 int mcs{-1}; // -1 indicates an unset value
84 uint32_t payloadSize =
85 700; // must fit in the max TX duration when transmitting at MCS 0 over an RU of 26 tones
86 std::string phyModel{"Yans"};
87 double minExpectedThroughput{0};
88 double maxExpectedThroughput{0};
89 Time accessReqInterval{0};
90
91 CommandLine cmd(__FILE__);
92 cmd.AddValue("frequency",
93 "Whether working in the 2.4, 5 or 6 GHz band (other values gets rejected)",
94 frequency);
95 cmd.AddValue("distance",
96 "Distance in meters between the station and the access point",
97 distance);
98 cmd.AddValue("simulationTime", "Simulation time in seconds", simulationTime);
99 cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
100 cmd.AddValue("downlink",
101 "Generate downlink flows if set to 1, uplink flows otherwise",
102 downlink);
103 cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
104 cmd.AddValue("useExtendedBlockAck", "Enable/disable use of extended BACK", useExtendedBlockAck);
105 cmd.AddValue("nStations", "Number of non-AP HE stations", nStations);
106 cmd.AddValue("dlAckType",
107 "Ack sequence type for DL OFDMA (NO-OFDMA, ACK-SU-FORMAT, MU-BAR, AGGR-MU-BAR)",
108 dlAckSeqType);
109 cmd.AddValue("enableUlOfdma",
110 "Enable UL OFDMA (useful if DL OFDMA is enabled and TCP is used)",
111 enableUlOfdma);
112 cmd.AddValue("enableBsrp",
113 "Enable BSRP (useful if DL and UL OFDMA are enabled and TCP is used)",
114 enableBsrp);
115 cmd.AddValue(
116 "muSchedAccessReqInterval",
117 "Duration of the interval between two requests for channel access made by the MU scheduler",
118 accessReqInterval);
119 cmd.AddValue("mcs", "if set, limit testing to a specific MCS (0-11)", mcs);
120 cmd.AddValue("payloadSize", "The application payload size in bytes", payloadSize);
121 cmd.AddValue("phyModel",
122 "PHY model to use when OFDMA is disabled (Yans or Spectrum). If OFDMA is enabled "
123 "then Spectrum is automatically selected",
124 phyModel);
125 cmd.AddValue("minExpectedThroughput",
126 "if set, simulation fails if the lowest throughput is below this value",
127 minExpectedThroughput);
128 cmd.AddValue("maxExpectedThroughput",
129 "if set, simulation fails if the highest throughput is above this value",
130 maxExpectedThroughput);
131 cmd.Parse(argc, argv);
132
133 if (useRts)
134 {
135 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
136 Config::SetDefault("ns3::WifiDefaultProtectionManager::EnableMuRts", BooleanValue(true));
137 }
138
139 if (dlAckSeqType == "ACK-SU-FORMAT")
140 {
141 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
143 }
144 else if (dlAckSeqType == "MU-BAR")
145 {
146 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
148 }
149 else if (dlAckSeqType == "AGGR-MU-BAR")
150 {
151 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
153 }
154 else if (dlAckSeqType != "NO-OFDMA")
155 {
156 NS_ABORT_MSG("Invalid DL ack sequence type (must be NO-OFDMA, ACK-SU-FORMAT, MU-BAR or "
157 "AGGR-MU-BAR)");
158 }
159
160 if (phyModel != "Yans" && phyModel != "Spectrum")
161 {
162 NS_ABORT_MSG("Invalid PHY model (must be Yans or Spectrum)");
163 }
164 if (dlAckSeqType != "NO-OFDMA")
165 {
166 // SpectrumWifiPhy is required for OFDMA
167 phyModel = "Spectrum";
168 }
169
170 double prevThroughput[12] = {0};
171
172 std::cout << "MCS value"
173 << "\t\t"
174 << "Channel width"
175 << "\t\t"
176 << "GI"
177 << "\t\t\t"
178 << "Throughput" << '\n';
179 int minMcs = 0;
180 int maxMcs = 11;
181 if (mcs >= 0 && mcs <= 11)
182 {
183 minMcs = mcs;
184 maxMcs = mcs;
185 }
186 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
187 {
188 uint8_t index = 0;
189 double previous = 0;
190 uint8_t maxChannelWidth = frequency == 2.4 ? 40 : 160;
191 for (int channelWidth = 20; channelWidth <= maxChannelWidth;) // MHz
192 {
193 for (int gi = 3200; gi >= 800;) // Nanoseconds
194 {
195 if (!udp)
196 {
197 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
198 }
199
201 wifiStaNodes.Create(nStations);
203 wifiApNode.Create(1);
204
205 NetDeviceContainer apDevice;
209 std::string channelStr("{0, " + std::to_string(channelWidth) + ", ");
210 StringValue ctrlRate;
211 auto nonHtRefRateMbps = HePhy::GetNonHtReferenceRate(mcs) / 1e6;
212
213 std::ostringstream ossDataMode;
214 ossDataMode << "HeMcs" << mcs;
215
216 if (frequency == 6)
217 {
218 wifi.SetStandard(WIFI_STANDARD_80211ax);
219 ctrlRate = StringValue(ossDataMode.str());
220 channelStr += "BAND_6GHZ, 0}";
221 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
222 DoubleValue(48));
223 }
224 else if (frequency == 5)
225 {
226 wifi.SetStandard(WIFI_STANDARD_80211ax);
227 std::ostringstream ossControlMode;
228 ossControlMode << "OfdmRate" << nonHtRefRateMbps << "Mbps";
229 ctrlRate = StringValue(ossControlMode.str());
230 channelStr += "BAND_5GHZ, 0}";
231 }
232 else if (frequency == 2.4)
233 {
234 wifi.SetStandard(WIFI_STANDARD_80211ax);
235 std::ostringstream ossControlMode;
236 ossControlMode << "ErpOfdmRate" << nonHtRefRateMbps << "Mbps";
237 ctrlRate = StringValue(ossControlMode.str());
238 channelStr += "BAND_2_4GHZ, 0}";
239 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
240 DoubleValue(40));
241 }
242 else
243 {
244 std::cout << "Wrong frequency value!" << std::endl;
245 return 0;
246 }
247
248 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
249 "DataMode",
250 StringValue(ossDataMode.str()),
251 "ControlMode",
252 ctrlRate);
253 // Set guard interval and MPDU buffer size
254 wifi.ConfigHeOptions("GuardInterval",
256 "MpduBufferSize",
257 UintegerValue(useExtendedBlockAck ? 256 : 64));
258
259 Ssid ssid = Ssid("ns3-80211ax");
260
261 if (phyModel == "Spectrum")
262 {
263 /*
264 * SingleModelSpectrumChannel cannot be used with 802.11ax because two
265 * spectrum models are required: one with 78.125 kHz bands for HE PPDUs
266 * and one with 312.5 kHz bands for, e.g., non-HT PPDUs (for more details,
267 * see issue #408 (CLOSED))
268 */
269 Ptr<MultiModelSpectrumChannel> spectrumChannel =
270 CreateObject<MultiModelSpectrumChannel>();
271
273 CreateObject<LogDistancePropagationLossModel>();
274 spectrumChannel->AddPropagationLossModel(lossModel);
275
277 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
278 phy.SetChannel(spectrumChannel);
279
280 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
281 phy.Set("ChannelSettings", StringValue(channelStr));
282 staDevices = wifi.Install(phy, mac, wifiStaNodes);
283
284 if (dlAckSeqType != "NO-OFDMA")
285 {
286 mac.SetMultiUserScheduler("ns3::RrMultiUserScheduler",
287 "EnableUlOfdma",
288 BooleanValue(enableUlOfdma),
289 "EnableBsrp",
290 BooleanValue(enableBsrp),
291 "AccessReqInterval",
292 TimeValue(accessReqInterval));
293 }
294 mac.SetType("ns3::ApWifiMac",
295 "EnableBeaconJitter",
296 BooleanValue(false),
297 "Ssid",
298 SsidValue(ssid));
299 apDevice = wifi.Install(phy, mac, wifiApNode);
300 }
301 else
302 {
305 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
306 phy.SetChannel(channel.Create());
307
308 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
309 phy.Set("ChannelSettings", StringValue(channelStr));
310 staDevices = wifi.Install(phy, mac, wifiStaNodes);
311
312 mac.SetType("ns3::ApWifiMac",
313 "EnableBeaconJitter",
314 BooleanValue(false),
315 "Ssid",
316 SsidValue(ssid));
317 apDevice = wifi.Install(phy, mac, wifiApNode);
318 }
319
322 int64_t streamNumber = 150;
323 streamNumber += wifi.AssignStreams(apDevice, streamNumber);
324 streamNumber += wifi.AssignStreams(staDevices, streamNumber);
325
326 // mobility.
328 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
329
330 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
331 positionAlloc->Add(Vector(distance, 0.0, 0.0));
332 mobility.SetPositionAllocator(positionAlloc);
333
334 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
335
336 mobility.Install(wifiApNode);
337 mobility.Install(wifiStaNodes);
338
339 /* Internet stack*/
341 stack.Install(wifiApNode);
342 stack.Install(wifiStaNodes);
343
345 address.SetBase("192.168.1.0", "255.255.255.0");
346 Ipv4InterfaceContainer staNodeInterfaces;
347 Ipv4InterfaceContainer apNodeInterface;
348
349 staNodeInterfaces = address.Assign(staDevices);
350 apNodeInterface = address.Assign(apDevice);
351
352 /* Setting applications */
353 ApplicationContainer serverApp;
354 auto serverNodes = downlink ? std::ref(wifiStaNodes) : std::ref(wifiApNode);
356 NodeContainer clientNodes;
357 for (std::size_t i = 0; i < nStations; i++)
358 {
359 serverInterfaces.Add(downlink ? staNodeInterfaces.Get(i)
360 : apNodeInterface.Get(0));
361 clientNodes.Add(downlink ? wifiApNode.Get(0) : wifiStaNodes.Get(i));
362 }
363
364 if (udp)
365 {
366 // UDP flow
367 uint16_t port = 9;
369 serverApp = server.Install(serverNodes.get());
370 serverApp.Start(Seconds(0.0));
371 serverApp.Stop(Seconds(simulationTime + 1));
372
373 for (std::size_t i = 0; i < nStations; i++)
374 {
376 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
377 client.SetAttribute("Interval", TimeValue(Time("0.00001"))); // packets/s
378 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
379 ApplicationContainer clientApp = client.Install(clientNodes.Get(i));
380 clientApp.Start(Seconds(1.0));
381 clientApp.Stop(Seconds(simulationTime + 1));
382 }
383 }
384 else
385 {
386 // TCP flow
387 uint16_t port = 50000;
389 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
390 serverApp = packetSinkHelper.Install(serverNodes.get());
391 serverApp.Start(Seconds(0.0));
392 serverApp.Stop(Seconds(simulationTime + 1));
393
394 for (std::size_t i = 0; i < nStations; i++)
395 {
396 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
397 onoff.SetAttribute("OnTime",
398 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
399 onoff.SetAttribute("OffTime",
400 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
401 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
402 onoff.SetAttribute("DataRate", DataRateValue(1000000000)); // bit/s
404 InetSocketAddress(serverInterfaces.GetAddress(i), port));
405 onoff.SetAttribute("Remote", remoteAddress);
406 ApplicationContainer clientApp = onoff.Install(clientNodes.Get(i));
407 clientApp.Start(Seconds(1.0));
408 clientApp.Stop(Seconds(simulationTime + 1));
409 }
410 }
411
413
414 Simulator::Stop(Seconds(simulationTime + 1));
416
417 // When multiple stations are used, there are chances that association requests
418 // collide and hence the throughput may be lower than expected. Therefore, we relax
419 // the check that the throughput cannot decrease by introducing a scaling factor (or
420 // tolerance)
421 double tolerance = 0.10;
422 uint64_t rxBytes = 0;
423 if (udp)
424 {
425 for (uint32_t i = 0; i < serverApp.GetN(); i++)
426 {
427 rxBytes +=
428 payloadSize * DynamicCast<UdpServer>(serverApp.Get(i))->GetReceived();
429 }
430 }
431 else
432 {
433 for (uint32_t i = 0; i < serverApp.GetN(); i++)
434 {
435 rxBytes += DynamicCast<PacketSink>(serverApp.Get(i))->GetTotalRx();
436 }
437 }
438 double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); // Mbit/s
439
441
442 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << gi << " ns\t\t\t"
443 << throughput << " Mbit/s" << std::endl;
444
445 // test first element
446 if (mcs == 0 && channelWidth == 20 && gi == 3200)
447 {
448 if (throughput * (1 + tolerance) < minExpectedThroughput)
449 {
450 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
451 exit(1);
452 }
453 }
454 // test last element
455 if (mcs == 11 && channelWidth == 160 && gi == 800)
456 {
457 if (maxExpectedThroughput > 0 &&
458 throughput > maxExpectedThroughput * (1 + tolerance))
459 {
460 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
461 exit(1);
462 }
463 }
464 // Skip comparisons with previous cases if more than one stations are present
465 // because, e.g., random collisions in the establishment of Block Ack agreements
466 // have an impact on throughput
467 if (nStations == 1)
468 {
469 // test previous throughput is smaller (for the same mcs)
470 if (throughput * (1 + tolerance) > previous)
471 {
472 previous = throughput;
473 }
474 else if (throughput > 0)
475 {
476 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
477 exit(1);
478 }
479 // test previous throughput is smaller (for the same channel width and GI)
480 if (throughput * (1 + tolerance) > prevThroughput[index])
481 {
482 prevThroughput[index] = throughput;
483 }
484 else if (throughput > 0)
485 {
486 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
487 exit(1);
488 }
489 }
490 index++;
491 gi /= 2;
492 }
493 channelWidth *= 2;
494 }
495 }
496 return 0;
497}
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Address.
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.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
uint32_t GetN() const
Get the number of Ptr<Application> stored in this container.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
AttributeValue implementation for DataRate.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:56
static uint64_t GetNonHtReferenceRate(uint8_t mcsValue)
Calculate the rate in bps of the non-HT Reference Rate corresponding to the supplied HE MCS index.
Definition: he-phy.cc:1731
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.
std::pair< Ptr< Ipv4 >, uint32_t > Get(uint32_t i) const
Get the std::pair of an Ptr<Ipv4> and interface stored at the location specified by the index.
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Add(const NodeContainer &nc)
Append the contents of another NodeContainer to the end of this container.
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.
Definition: on-off-helper.h:44
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:78
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
Make it easy to create and manage PHY objects for the spectrum model.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1423
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:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
manage and create wifi channel objects for the YANS model.
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:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:891
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1372
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
@ WIFI_STANDARD_80211ax
ns address
Definition: first.py:40
ns stack
Definition: first.py:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:33
STL namespace.
ns wifi
Definition: third.py:88
ns ssid
Definition: third.py:86
ns staDevices
Definition: third.py:91
ns mac
Definition: third.py:85
ns wifiApNode
Definition: third.py:79
ns channel
Definition: third.py:81
ns mobility
Definition: third.py:96
ns wifiStaNodes
Definition: third.py:77
ns phy
Definition: third.py:82