A Discrete-Event Network Simulator
API
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",
142 EnumValue(WifiAcknowledgment::DL_MU_BAR_BA_SEQUENCE));
143 }
144 else if (dlAckSeqType == "MU-BAR")
145 {
146 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
147 EnumValue(WifiAcknowledgment::DL_MU_TF_MU_BAR));
148 }
149 else if (dlAckSeqType == "AGGR-MU-BAR")
150 {
151 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
152 EnumValue(WifiAcknowledgment::DL_MU_AGGREGATE_TF));
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];
171 for (uint32_t l = 0; l < 12; l++)
172 {
173 prevThroughput[l] = 0;
174 }
175 std::cout << "MCS value"
176 << "\t\t"
177 << "Channel width"
178 << "\t\t"
179 << "GI"
180 << "\t\t\t"
181 << "Throughput" << '\n';
182 int minMcs = 0;
183 int maxMcs = 11;
184 if (mcs >= 0 && mcs <= 11)
185 {
186 minMcs = mcs;
187 maxMcs = mcs;
188 }
189 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
190 {
191 uint8_t index = 0;
192 double previous = 0;
193 uint8_t maxChannelWidth = frequency == 2.4 ? 40 : 160;
194 for (int channelWidth = 20; channelWidth <= maxChannelWidth;) // MHz
195 {
196 for (int gi = 3200; gi >= 800;) // Nanoseconds
197 {
198 if (!udp)
199 {
200 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
201 }
202
204 wifiStaNodes.Create(nStations);
206 wifiApNode.Create(1);
207
208 NetDeviceContainer apDevice;
212 std::string channelStr("{0, " + std::to_string(channelWidth) + ", ");
213 StringValue ctrlRate;
214 auto nonHtRefRateMbps = HePhy::GetNonHtReferenceRate(mcs) / 1e6;
215
216 std::ostringstream ossDataMode;
217 ossDataMode << "HeMcs" << mcs;
218
219 if (frequency == 6)
220 {
221 wifi.SetStandard(WIFI_STANDARD_80211ax);
222 ctrlRate = StringValue(ossDataMode.str());
223 channelStr += "BAND_6GHZ, 0}";
224 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
225 DoubleValue(48));
226 }
227 else if (frequency == 5)
228 {
229 wifi.SetStandard(WIFI_STANDARD_80211ax);
230 std::ostringstream ossControlMode;
231 ossControlMode << "OfdmRate" << nonHtRefRateMbps << "Mbps";
232 ctrlRate = StringValue(ossControlMode.str());
233 channelStr += "BAND_5GHZ, 0}";
234 }
235 else if (frequency == 2.4)
236 {
237 wifi.SetStandard(WIFI_STANDARD_80211ax);
238 std::ostringstream ossControlMode;
239 ossControlMode << "ErpOfdmRate" << nonHtRefRateMbps << "Mbps";
240 ctrlRate = StringValue(ossControlMode.str());
241 channelStr += "BAND_2_4GHZ, 0}";
242 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
243 DoubleValue(40));
244 }
245 else
246 {
247 std::cout << "Wrong frequency value!" << std::endl;
248 return 0;
249 }
250
251 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
252 "DataMode",
253 StringValue(ossDataMode.str()),
254 "ControlMode",
255 ctrlRate);
256 // Set guard interval and MPDU buffer size
257 wifi.ConfigHeOptions("GuardInterval",
259 "MpduBufferSize",
260 UintegerValue(useExtendedBlockAck ? 256 : 64));
261
262 Ssid ssid = Ssid("ns3-80211ax");
263
264 if (phyModel == "Spectrum")
265 {
266 /*
267 * SingleModelSpectrumChannel cannot be used with 802.11ax because two
268 * spectrum models are required: one with 78.125 kHz bands for HE PPDUs
269 * and one with 312.5 kHz bands for, e.g., non-HT PPDUs (for more details,
270 * see issue #408 (CLOSED))
271 */
272 Ptr<MultiModelSpectrumChannel> spectrumChannel =
273 CreateObject<MultiModelSpectrumChannel>();
274
276 CreateObject<LogDistancePropagationLossModel>();
277 spectrumChannel->AddPropagationLossModel(lossModel);
278
280 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
281 phy.SetChannel(spectrumChannel);
282
283 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
284 phy.Set("ChannelSettings", StringValue(channelStr));
285 staDevices = wifi.Install(phy, mac, wifiStaNodes);
286
287 if (dlAckSeqType != "NO-OFDMA")
288 {
289 mac.SetMultiUserScheduler("ns3::RrMultiUserScheduler",
290 "EnableUlOfdma",
291 BooleanValue(enableUlOfdma),
292 "EnableBsrp",
293 BooleanValue(enableBsrp),
294 "AccessReqInterval",
295 TimeValue(accessReqInterval));
296 }
297 mac.SetType("ns3::ApWifiMac",
298 "EnableBeaconJitter",
299 BooleanValue(false),
300 "Ssid",
301 SsidValue(ssid));
302 apDevice = wifi.Install(phy, mac, wifiApNode);
303 }
304 else
305 {
306 YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
308 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
309 phy.SetChannel(channel.Create());
310
311 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
312 phy.Set("ChannelSettings", StringValue(channelStr));
313 staDevices = wifi.Install(phy, mac, wifiStaNodes);
314
315 mac.SetType("ns3::ApWifiMac",
316 "EnableBeaconJitter",
317 BooleanValue(false),
318 "Ssid",
319 SsidValue(ssid));
320 apDevice = wifi.Install(phy, mac, wifiApNode);
321 }
322
323 RngSeedManager::SetSeed(1);
324 RngSeedManager::SetRun(1);
325 int64_t streamNumber = 150;
326 streamNumber += wifi.AssignStreams(apDevice, streamNumber);
327 streamNumber += wifi.AssignStreams(staDevices, streamNumber);
328
329 // mobility.
331 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
332
333 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
334 positionAlloc->Add(Vector(distance, 0.0, 0.0));
335 mobility.SetPositionAllocator(positionAlloc);
336
337 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
338
339 mobility.Install(wifiApNode);
340 mobility.Install(wifiStaNodes);
341
342 /* Internet stack*/
344 stack.Install(wifiApNode);
345 stack.Install(wifiStaNodes);
346
348 address.SetBase("192.168.1.0", "255.255.255.0");
349 Ipv4InterfaceContainer staNodeInterfaces;
350 Ipv4InterfaceContainer apNodeInterface;
351
352 staNodeInterfaces = address.Assign(staDevices);
353 apNodeInterface = address.Assign(apDevice);
354
355 /* Setting applications */
356 ApplicationContainer serverApp;
357 auto serverNodes = downlink ? std::ref(wifiStaNodes) : std::ref(wifiApNode);
358 Ipv4InterfaceContainer serverInterfaces;
359 NodeContainer clientNodes;
360 for (std::size_t i = 0; i < nStations; i++)
361 {
362 serverInterfaces.Add(downlink ? staNodeInterfaces.Get(i)
363 : apNodeInterface.Get(0));
364 clientNodes.Add(downlink ? wifiApNode.Get(0) : wifiStaNodes.Get(i));
365 }
366
367 if (udp)
368 {
369 // UDP flow
370 uint16_t port = 9;
371 UdpServerHelper server(port);
372 serverApp = server.Install(serverNodes.get());
373 serverApp.Start(Seconds(0.0));
374 serverApp.Stop(Seconds(simulationTime + 1));
375
376 for (std::size_t i = 0; i < nStations; i++)
377 {
378 UdpClientHelper client(serverInterfaces.GetAddress(i), port);
379 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
380 client.SetAttribute("Interval", TimeValue(Time("0.00001"))); // packets/s
381 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
382 ApplicationContainer clientApp = client.Install(clientNodes.Get(i));
383 clientApp.Start(Seconds(1.0));
384 clientApp.Stop(Seconds(simulationTime + 1));
385 }
386 }
387 else
388 {
389 // TCP flow
390 uint16_t port = 50000;
391 Address localAddress(InetSocketAddress(Ipv4Address::GetAny(), port));
392 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
393 serverApp = packetSinkHelper.Install(serverNodes.get());
394 serverApp.Start(Seconds(0.0));
395 serverApp.Stop(Seconds(simulationTime + 1));
396
397 for (std::size_t i = 0; i < nStations; i++)
398 {
399 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
400 onoff.SetAttribute("OnTime",
401 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
402 onoff.SetAttribute("OffTime",
403 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
404 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
405 onoff.SetAttribute("DataRate", DataRateValue(1000000000)); // bit/s
406 AddressValue remoteAddress(
407 InetSocketAddress(serverInterfaces.GetAddress(i), port));
408 onoff.SetAttribute("Remote", remoteAddress);
409 ApplicationContainer clientApp = onoff.Install(clientNodes.Get(i));
410 clientApp.Start(Seconds(1.0));
411 clientApp.Stop(Seconds(simulationTime + 1));
412 }
413 }
414
415 Simulator::Schedule(Seconds(0), &Ipv4GlobalRoutingHelper::PopulateRoutingTables);
416
417 Simulator::Stop(Seconds(simulationTime + 1));
418 Simulator::Run();
419
420 // When multiple stations are used, there are chances that association requests
421 // collide and hence the throughput may be lower than expected. Therefore, we relax
422 // the check that the throughput cannot decrease by introducing a scaling factor (or
423 // tolerance)
424 double tolerance = 0.10;
425 uint64_t rxBytes = 0;
426 if (udp)
427 {
428 for (uint32_t i = 0; i < serverApp.GetN(); i++)
429 {
430 rxBytes +=
431 payloadSize * DynamicCast<UdpServer>(serverApp.Get(i))->GetReceived();
432 }
433 }
434 else
435 {
436 for (uint32_t i = 0; i < serverApp.GetN(); i++)
437 {
438 rxBytes += DynamicCast<PacketSink>(serverApp.Get(i))->GetTotalRx();
439 }
440 }
441 double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); // Mbit/s
442
443 Simulator::Destroy();
444
445 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << gi << " ns\t\t\t"
446 << throughput << " Mbit/s" << std::endl;
447
448 // test first element
449 if (mcs == 0 && channelWidth == 20 && gi == 3200)
450 {
451 if (throughput * (1 + tolerance) < minExpectedThroughput)
452 {
453 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
454 exit(1);
455 }
456 }
457 // test last element
458 if (mcs == 11 && channelWidth == 160 && gi == 800)
459 {
460 if (maxExpectedThroughput > 0 &&
461 throughput > maxExpectedThroughput * (1 + tolerance))
462 {
463 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
464 exit(1);
465 }
466 }
467 // Skip comparisons with previous cases if more than one stations are present
468 // because, e.g., random collisions in the establishment of Block Ack agreements
469 // have an impact on throughput
470 if (nStations == 1)
471 {
472 // test previous throughput is smaller (for the same mcs)
473 if (throughput * (1 + tolerance) > previous)
474 {
475 previous = throughput;
476 }
477 else if (throughput > 0)
478 {
479 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
480 exit(1);
481 }
482 // test previous throughput is smaller (for the same channel width and GI)
483 if (throughput * (1 + tolerance) > prevThroughput[index])
484 {
485 prevThroughput[index] = throughput;
486 }
487 else if (throughput > 0)
488 {
489 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
490 exit(1);
491 }
492 }
493 index++;
494 gi /= 2;
495 }
496 channelWidth *= 2;
497 }
498 }
499 return 0;
500}
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
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.
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.
void Add(const Ipv4InterfaceContainer &other)
Concatenate the entries in the other container with ours.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
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
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
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
manage and create wifi channel objects for the YANS model.
Make it easy to create and manage PHY objects for the YANS model.
uint16_t port
Definition: dsdv-manet.cc:45
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
address
Definition: first.py:40
stack
Definition: first.py:37
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:848
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:33
STL namespace.
staDevices
Definition: third.py:91
ssid
Definition: third.py:86
channel
Definition: third.py:81
mac
Definition: third.py:85
wifi
Definition: third.py:88
wifiApNode
Definition: third.py:79
mobility
Definition: third.py:96
wifiStaNodes
Definition: third.py:77
phy
Definition: third.py:82