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/internet-stack-helper.h"
26#include "ns3/ipv4-address-helper.h"
27#include "ns3/ipv4-global-routing-helper.h"
28#include "ns3/log.h"
29#include "ns3/mobility-helper.h"
30#include "ns3/multi-model-spectrum-channel.h"
31#include "ns3/on-off-helper.h"
32#include "ns3/packet-sink-helper.h"
33#include "ns3/packet-sink.h"
34#include "ns3/rng-seed-manager.h"
35#include "ns3/spectrum-wifi-helper.h"
36#include "ns3/ssid.h"
37#include "ns3/string.h"
38#include "ns3/udp-client-server-helper.h"
39#include "ns3/uinteger.h"
40#include "ns3/wifi-acknowledgment.h"
41#include "ns3/yans-wifi-channel.h"
42#include "ns3/yans-wifi-helper.h"
43
44#include <functional>
45
46// This is a simple example in order to show how to configure an IEEE 802.11ax Wi-Fi network.
47//
48// It outputs the UDP or TCP goodput for every HE MCS value, which depends on the MCS value (0 to
49// 11), the channel width (20, 40, 80 or 160 MHz) and the guard interval (800ns, 1600ns or 3200ns).
50// The PHY bitrate is constant over all the simulation run. The user can also specify the distance
51// between the access point and the station: the larger the distance the smaller the goodput.
52//
53// The simulation assumes a configurable number of stations in an infrastructure network:
54//
55// STA AP
56// * *
57// | |
58// n1 n2
59//
60// Packets in this simulation belong to BestEffort Access Class (AC_BE).
61// By selecting an acknowledgment sequence for DL MU PPDUs, it is possible to aggregate a
62// Round Robin scheduler to the AP, so that DL MU PPDUs are sent by the AP via DL OFDMA.
63
64using namespace ns3;
65
66NS_LOG_COMPONENT_DEFINE("he-wifi-network");
67
68int
69main(int argc, char* argv[])
70{
71 bool udp{true};
72 bool downlink{true};
73 bool useRts{false};
74 bool useExtendedBlockAck{false};
75 double simulationTime{10}; // seconds
76 double distance{1.0}; // meters
77 double frequency{5}; // whether 2.4, 5 or 6 GHz
78 std::size_t nStations{1};
79 std::string dlAckSeqType{"NO-OFDMA"};
80 bool enableUlOfdma{false};
81 bool enableBsrp{false};
82 int mcs{-1}; // -1 indicates an unset value
83 uint32_t payloadSize =
84 700; // must fit in the max TX duration when transmitting at MCS 0 over an RU of 26 tones
85 std::string phyModel{"Yans"};
86 double minExpectedThroughput{0};
87 double maxExpectedThroughput{0};
88 Time accessReqInterval{0};
89
90 CommandLine cmd(__FILE__);
91 cmd.AddValue("frequency",
92 "Whether working in the 2.4, 5 or 6 GHz band (other values gets rejected)",
93 frequency);
94 cmd.AddValue("distance",
95 "Distance in meters between the station and the access point",
96 distance);
97 cmd.AddValue("simulationTime", "Simulation time in seconds", simulationTime);
98 cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
99 cmd.AddValue("downlink",
100 "Generate downlink flows if set to 1, uplink flows otherwise",
101 downlink);
102 cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
103 cmd.AddValue("useExtendedBlockAck", "Enable/disable use of extended BACK", useExtendedBlockAck);
104 cmd.AddValue("nStations", "Number of non-AP HE stations", nStations);
105 cmd.AddValue("dlAckType",
106 "Ack sequence type for DL OFDMA (NO-OFDMA, ACK-SU-FORMAT, MU-BAR, AGGR-MU-BAR)",
107 dlAckSeqType);
108 cmd.AddValue("enableUlOfdma",
109 "Enable UL OFDMA (useful if DL OFDMA is enabled and TCP is used)",
110 enableUlOfdma);
111 cmd.AddValue("enableBsrp",
112 "Enable BSRP (useful if DL and UL OFDMA are enabled and TCP is used)",
113 enableBsrp);
114 cmd.AddValue(
115 "muSchedAccessReqInterval",
116 "Duration of the interval between two requests for channel access made by the MU scheduler",
117 accessReqInterval);
118 cmd.AddValue("mcs", "if set, limit testing to a specific MCS (0-11)", mcs);
119 cmd.AddValue("payloadSize", "The application payload size in bytes", payloadSize);
120 cmd.AddValue("phyModel",
121 "PHY model to use when OFDMA is disabled (Yans or Spectrum). If OFDMA is enabled "
122 "then Spectrum is automatically selected",
123 phyModel);
124 cmd.AddValue("minExpectedThroughput",
125 "if set, simulation fails if the lowest throughput is below this value",
126 minExpectedThroughput);
127 cmd.AddValue("maxExpectedThroughput",
128 "if set, simulation fails if the highest throughput is above this value",
129 maxExpectedThroughput);
130 cmd.Parse(argc, argv);
131
132 if (useRts)
133 {
134 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
135 }
136
137 if (dlAckSeqType == "ACK-SU-FORMAT")
138 {
139 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
140 EnumValue(WifiAcknowledgment::DL_MU_BAR_BA_SEQUENCE));
141 }
142 else if (dlAckSeqType == "MU-BAR")
143 {
144 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
145 EnumValue(WifiAcknowledgment::DL_MU_TF_MU_BAR));
146 }
147 else if (dlAckSeqType == "AGGR-MU-BAR")
148 {
149 Config::SetDefault("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
150 EnumValue(WifiAcknowledgment::DL_MU_AGGREGATE_TF));
151 }
152 else if (dlAckSeqType != "NO-OFDMA")
153 {
154 NS_ABORT_MSG("Invalid DL ack sequence type (must be NO-OFDMA, ACK-SU-FORMAT, MU-BAR or "
155 "AGGR-MU-BAR)");
156 }
157
158 if (phyModel != "Yans" && phyModel != "Spectrum")
159 {
160 NS_ABORT_MSG("Invalid PHY model (must be Yans or Spectrum)");
161 }
162 if (dlAckSeqType != "NO-OFDMA")
163 {
164 // SpectrumWifiPhy is required for OFDMA
165 phyModel = "Spectrum";
166 }
167
168 double prevThroughput[12];
169 for (uint32_t l = 0; l < 12; l++)
170 {
171 prevThroughput[l] = 0;
172 }
173 std::cout << "MCS value"
174 << "\t\t"
175 << "Channel width"
176 << "\t\t"
177 << "GI"
178 << "\t\t\t"
179 << "Throughput" << '\n';
180 int minMcs = 0;
181 int maxMcs = 11;
182 if (mcs >= 0 && mcs <= 11)
183 {
184 minMcs = mcs;
185 maxMcs = mcs;
186 }
187 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
188 {
189 uint8_t index = 0;
190 double previous = 0;
191 uint8_t maxChannelWidth = frequency == 2.4 ? 40 : 160;
192 for (int channelWidth = 20; channelWidth <= maxChannelWidth;) // MHz
193 {
194 for (int gi = 3200; gi >= 800;) // Nanoseconds
195 {
196 if (!udp)
197 {
198 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
199 }
200
202 wifiStaNodes.Create(nStations);
204 wifiApNode.Create(1);
205
206 NetDeviceContainer apDevice;
210 std::string channelStr("{0, " + std::to_string(channelWidth) + ", ");
211
212 if (frequency == 6)
213 {
214 wifi.SetStandard(WIFI_STANDARD_80211ax);
215 channelStr += "BAND_6GHZ, 0}";
216 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
217 DoubleValue(48));
218 }
219 else if (frequency == 5)
220 {
221 wifi.SetStandard(WIFI_STANDARD_80211ax);
222 channelStr += "BAND_5GHZ, 0}";
223 }
224 else if (frequency == 2.4)
225 {
226 wifi.SetStandard(WIFI_STANDARD_80211ax);
227 channelStr += "BAND_2_4GHZ, 0}";
228 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
229 DoubleValue(40));
230 }
231 else
232 {
233 std::cout << "Wrong frequency value!" << std::endl;
234 return 0;
235 }
236
237 std::ostringstream oss;
238 oss << "HeMcs" << mcs;
239 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
240 "DataMode",
241 StringValue(oss.str()),
242 "ControlMode",
243 StringValue(oss.str()));
244 // Set guard interval and MPDU buffer size
245 wifi.ConfigHeOptions("GuardInterval",
247 "MpduBufferSize",
248 UintegerValue(useExtendedBlockAck ? 256 : 64));
249
250 Ssid ssid = Ssid("ns3-80211ax");
251
252 if (phyModel == "Spectrum")
253 {
254 /*
255 * SingleModelSpectrumChannel cannot be used with 802.11ax because two
256 * spectrum models are required: one with 78.125 kHz bands for HE PPDUs
257 * and one with 312.5 kHz bands for, e.g., non-HT PPDUs (for more details,
258 * see issue #408 (CLOSED))
259 */
260 Ptr<MultiModelSpectrumChannel> spectrumChannel =
261 CreateObject<MultiModelSpectrumChannel>();
263 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
264 phy.SetChannel(spectrumChannel);
265
266 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
267 phy.Set("ChannelSettings", StringValue(channelStr));
268 staDevices = wifi.Install(phy, mac, wifiStaNodes);
269
270 if (dlAckSeqType != "NO-OFDMA")
271 {
272 mac.SetMultiUserScheduler("ns3::RrMultiUserScheduler",
273 "EnableUlOfdma",
274 BooleanValue(enableUlOfdma),
275 "EnableBsrp",
276 BooleanValue(enableBsrp),
277 "AccessReqInterval",
278 TimeValue(accessReqInterval));
279 }
280 mac.SetType("ns3::ApWifiMac",
281 "EnableBeaconJitter",
282 BooleanValue(false),
283 "Ssid",
284 SsidValue(ssid));
285 apDevice = wifi.Install(phy, mac, wifiApNode);
286 }
287 else
288 {
289 YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
291 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
292 phy.SetChannel(channel.Create());
293
294 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
295 phy.Set("ChannelSettings", StringValue(channelStr));
296 staDevices = wifi.Install(phy, mac, wifiStaNodes);
297
298 mac.SetType("ns3::ApWifiMac",
299 "EnableBeaconJitter",
300 BooleanValue(false),
301 "Ssid",
302 SsidValue(ssid));
303 apDevice = wifi.Install(phy, mac, wifiApNode);
304 }
305
306 RngSeedManager::SetSeed(1);
307 RngSeedManager::SetRun(1);
308 int64_t streamNumber = 150;
309 streamNumber += wifi.AssignStreams(apDevice, streamNumber);
310 streamNumber += wifi.AssignStreams(staDevices, streamNumber);
311
312 // mobility.
314 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
315
316 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
317 positionAlloc->Add(Vector(distance, 0.0, 0.0));
318 mobility.SetPositionAllocator(positionAlloc);
319
320 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
321
322 mobility.Install(wifiApNode);
323 mobility.Install(wifiStaNodes);
324
325 /* Internet stack*/
327 stack.Install(wifiApNode);
328 stack.Install(wifiStaNodes);
329
331 address.SetBase("192.168.1.0", "255.255.255.0");
332 Ipv4InterfaceContainer staNodeInterfaces;
333 Ipv4InterfaceContainer apNodeInterface;
334
335 staNodeInterfaces = address.Assign(staDevices);
336 apNodeInterface = address.Assign(apDevice);
337
338 /* Setting applications */
339 ApplicationContainer serverApp;
340 auto serverNodes = downlink ? std::ref(wifiStaNodes) : std::ref(wifiApNode);
341 Ipv4InterfaceContainer serverInterfaces;
342 NodeContainer clientNodes;
343 for (std::size_t i = 0; i < nStations; i++)
344 {
345 serverInterfaces.Add(downlink ? staNodeInterfaces.Get(i)
346 : apNodeInterface.Get(0));
347 clientNodes.Add(downlink ? wifiApNode.Get(0) : wifiStaNodes.Get(i));
348 }
349
350 if (udp)
351 {
352 // UDP flow
353 uint16_t port = 9;
354 UdpServerHelper server(port);
355 serverApp = server.Install(serverNodes.get());
356 serverApp.Start(Seconds(0.0));
357 serverApp.Stop(Seconds(simulationTime + 1));
358
359 for (std::size_t i = 0; i < nStations; i++)
360 {
361 UdpClientHelper client(serverInterfaces.GetAddress(i), port);
362 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
363 client.SetAttribute("Interval", TimeValue(Time("0.00001"))); // packets/s
364 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
365 ApplicationContainer clientApp = client.Install(clientNodes.Get(i));
366 clientApp.Start(Seconds(1.0));
367 clientApp.Stop(Seconds(simulationTime + 1));
368 }
369 }
370 else
371 {
372 // TCP flow
373 uint16_t port = 50000;
374 Address localAddress(InetSocketAddress(Ipv4Address::GetAny(), port));
375 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
376 serverApp = packetSinkHelper.Install(serverNodes.get());
377 serverApp.Start(Seconds(0.0));
378 serverApp.Stop(Seconds(simulationTime + 1));
379
380 for (std::size_t i = 0; i < nStations; i++)
381 {
382 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
383 onoff.SetAttribute("OnTime",
384 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
385 onoff.SetAttribute("OffTime",
386 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
387 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
388 onoff.SetAttribute("DataRate", DataRateValue(1000000000)); // bit/s
389 AddressValue remoteAddress(
390 InetSocketAddress(serverInterfaces.GetAddress(i), port));
391 onoff.SetAttribute("Remote", remoteAddress);
392 ApplicationContainer clientApp = onoff.Install(clientNodes.Get(i));
393 clientApp.Start(Seconds(1.0));
394 clientApp.Stop(Seconds(simulationTime + 1));
395 }
396 }
397
398 Simulator::Schedule(Seconds(0), &Ipv4GlobalRoutingHelper::PopulateRoutingTables);
399
400 Simulator::Stop(Seconds(simulationTime + 1));
401 Simulator::Run();
402
403 // When multiple stations are used, there are chances that association requests
404 // collide and hence the throughput may be lower than expected. Therefore, we relax
405 // the check that the throughput cannot decrease by introducing a scaling factor (or
406 // tolerance)
407 double tolerance = 0.10;
408 uint64_t rxBytes = 0;
409 if (udp)
410 {
411 for (uint32_t i = 0; i < serverApp.GetN(); i++)
412 {
413 rxBytes +=
414 payloadSize * DynamicCast<UdpServer>(serverApp.Get(i))->GetReceived();
415 }
416 }
417 else
418 {
419 for (uint32_t i = 0; i < serverApp.GetN(); i++)
420 {
421 rxBytes += DynamicCast<PacketSink>(serverApp.Get(i))->GetTotalRx();
422 }
423 }
424 double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); // Mbit/s
425
426 Simulator::Destroy();
427
428 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << gi << " ns\t\t\t"
429 << throughput << " Mbit/s" << std::endl;
430
431 // test first element
432 if (mcs == 0 && channelWidth == 20 && gi == 3200)
433 {
434 if (throughput * (1 + tolerance) < minExpectedThroughput)
435 {
436 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
437 exit(1);
438 }
439 }
440 // test last element
441 if (mcs == 11 && channelWidth == 160 && gi == 800)
442 {
443 if (maxExpectedThroughput > 0 &&
444 throughput > maxExpectedThroughput * (1 + tolerance))
445 {
446 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
447 exit(1);
448 }
449 }
450 // Skip comparisons with previous cases if more than one stations are present
451 // because, e.g., random collisions in the establishment of Block Ack agreements
452 // have an impact on throughput
453 if (nStations == 1)
454 {
455 // test previous throughput is smaller (for the same mcs)
456 if (throughput * (1 + tolerance) > previous)
457 {
458 previous = throughput;
459 }
460 else if (throughput > 0)
461 {
462 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
463 exit(1);
464 }
465 // test previous throughput is smaller (for the same channel width and GI)
466 if (throughput * (1 + tolerance) > prevThroughput[index])
467 {
468 prevThroughput[index] = throughput;
469 }
470 else if (throughput > 0)
471 {
472 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
473 exit(1);
474 }
475 }
476 index++;
477 gi /= 2;
478 }
479 channelWidth *= 2;
480 }
481 }
482 return 0;
483}
a polymophic address class
Definition: address.h:92
AttributeValue implementation for Address.
holds a vector of ns3::Application pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
uint32_t GetN() const
Get the number of Ptr<Application> stored in this container.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
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:42
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1425
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:325
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:1374
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
@ 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:850
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