A Discrete-Event Network Simulator
API
wifi-he-network.cc
Go to the documentation of this file.
1/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2016 SEBASTIEN DERONNE
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Sebastien Deronne <sebastien.deronne@gmail.com>
19 */
20
21#include "ns3/command-line.h"
22#include "ns3/config.h"
23#include "ns3/uinteger.h"
24#include "ns3/boolean.h"
25#include "ns3/double.h"
26#include "ns3/string.h"
27#include "ns3/enum.h"
28#include "ns3/log.h"
29#include "ns3/yans-wifi-helper.h"
30#include "ns3/spectrum-wifi-helper.h"
31#include "ns3/ssid.h"
32#include "ns3/mobility-helper.h"
33#include "ns3/internet-stack-helper.h"
34#include "ns3/ipv4-address-helper.h"
35#include "ns3/udp-client-server-helper.h"
36#include "ns3/packet-sink-helper.h"
37#include "ns3/on-off-helper.h"
38#include "ns3/ipv4-global-routing-helper.h"
39#include "ns3/packet-sink.h"
40#include "ns3/yans-wifi-channel.h"
41#include "ns3/multi-model-spectrum-channel.h"
42#include "ns3/wifi-acknowledgment.h"
43#include "ns3/rng-seed-manager.h"
44
45// This is a simple example in order to show how to configure an IEEE 802.11ax Wi-Fi network.
46//
47// It outputs the UDP or TCP goodput for every HE MCS value, which depends on the MCS value (0 to 11),
48// the channel width (20, 40, 80 or 160 MHz) and the guard interval (800ns, 1600ns or 3200ns).
49// The PHY bitrate is constant over all the simulation run. The user can also specify the distance between
50// the access point and the station: the larger the distance the smaller the goodput.
51//
52// The simulation assumes a configurable number of stations in an infrastructure network:
53//
54// STA AP
55// * *
56// | |
57// n1 n2
58//
59// Packets in this simulation belong to BestEffort Access Class (AC_BE).
60// By selecting an acknowledgment sequence for DL MU PPDUs, it is possible to aggregate a
61// Round Robin scheduler to the AP, so that DL MU PPDUs are sent by the AP via DL OFDMA.
62
63using namespace ns3;
64
65NS_LOG_COMPONENT_DEFINE ("he-wifi-network");
66
67int main (int argc, char *argv[])
68{
69 bool udp {true};
70 bool useRts {false};
71 bool useExtendedBlockAck {false};
72 double simulationTime {10}; //seconds
73 double distance {1.0}; //meters
74 double frequency {5}; //whether 2.4, 5 or 6 GHz
75 std::size_t nStations {1};
76 std::string dlAckSeqType {"NO-OFDMA"};
77 bool enableUlOfdma {false};
78 bool enableBsrp {false};
79 int mcs {-1}; // -1 indicates an unset value
80 uint32_t payloadSize = 700; // must fit in the max TX duration when transmitting at MCS 0 over an RU of 26 tones
81 std::string phyModel {"Yans"};
82 double minExpectedThroughput {0};
83 double maxExpectedThroughput {0};
84
85 CommandLine cmd (__FILE__);
86 cmd.AddValue ("frequency", "Whether working in the 2.4, 5 or 6 GHz band (other values gets rejected)", frequency);
87 cmd.AddValue ("distance", "Distance in meters between the station and the access point", distance);
88 cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
89 cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
90 cmd.AddValue ("useRts", "Enable/disable RTS/CTS", useRts);
91 cmd.AddValue ("useExtendedBlockAck", "Enable/disable use of extended BACK", useExtendedBlockAck);
92 cmd.AddValue ("nStations", "Number of non-AP HE stations", nStations);
93 cmd.AddValue ("dlAckType", "Ack sequence type for DL OFDMA (NO-OFDMA, ACK-SU-FORMAT, MU-BAR, AGGR-MU-BAR)",
94 dlAckSeqType);
95 cmd.AddValue ("enableUlOfdma", "Enable UL OFDMA (useful if DL OFDMA is enabled and TCP is used)", enableUlOfdma);
96 cmd.AddValue ("enableBsrp", "Enable BSRP (useful if DL and UL OFDMA are enabled and TCP is used)", enableBsrp);
97 cmd.AddValue ("mcs", "if set, limit testing to a specific MCS (0-11)", mcs);
98 cmd.AddValue ("payloadSize", "The application payload size in bytes", payloadSize);
99 cmd.AddValue ("phyModel", "PHY model to use when OFDMA is disabled (Yans or Spectrum). If OFDMA is enabled then Spectrum is automatically selected", phyModel);
100 cmd.AddValue ("minExpectedThroughput", "if set, simulation fails if the lowest throughput is below this value", minExpectedThroughput);
101 cmd.AddValue ("maxExpectedThroughput", "if set, simulation fails if the highest throughput is above this value", maxExpectedThroughput);
102 cmd.Parse (argc,argv);
103
104 if (useRts)
105 {
106 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("0"));
107 }
108
109 if (dlAckSeqType == "ACK-SU-FORMAT")
110 {
111 Config::SetDefault ("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
112 EnumValue (WifiAcknowledgment::DL_MU_BAR_BA_SEQUENCE));
113 }
114 else if (dlAckSeqType == "MU-BAR")
115 {
116 Config::SetDefault ("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
117 EnumValue (WifiAcknowledgment::DL_MU_TF_MU_BAR));
118 }
119 else if (dlAckSeqType == "AGGR-MU-BAR")
120 {
121 Config::SetDefault ("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
122 EnumValue (WifiAcknowledgment::DL_MU_AGGREGATE_TF));
123 }
124 else if (dlAckSeqType != "NO-OFDMA")
125 {
126 NS_ABORT_MSG ("Invalid DL ack sequence type (must be NO-OFDMA, ACK-SU-FORMAT, MU-BAR or AGGR-MU-BAR)");
127 }
128
129 if (phyModel != "Yans" && phyModel != "Spectrum")
130 {
131 NS_ABORT_MSG ("Invalid PHY model (must be Yans or Spectrum)");
132 }
133 if (dlAckSeqType != "NO-OFDMA")
134 {
135 // SpectrumWifiPhy is required for OFDMA
136 phyModel = "Spectrum";
137 }
138
139 double prevThroughput [12];
140 for (uint32_t l = 0; l < 12; l++)
141 {
142 prevThroughput[l] = 0;
143 }
144 std::cout << "MCS value" << "\t\t" << "Channel width" << "\t\t" << "GI" << "\t\t\t" << "Throughput" << '\n';
145 int minMcs = 0;
146 int maxMcs = 11;
147 if (mcs >= 0 && mcs <= 11)
148 {
149 minMcs = mcs;
150 maxMcs = mcs;
151 }
152 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
153 {
154 uint8_t index = 0;
155 double previous = 0;
156 uint8_t maxChannelWidth = frequency == 2.4 ? 40 : 160;
157 for (int channelWidth = 20; channelWidth <= maxChannelWidth; ) //MHz
158 {
159 for (int gi = 3200; gi >= 800; ) //Nanoseconds
160 {
161 if (!udp)
162 {
163 Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
164 }
165
167 wifiStaNodes.Create (nStations);
169 wifiApNode.Create (1);
170
174 std::string channelStr ("{0, " + std::to_string (channelWidth) + ", ");
175
176 if (frequency == 6)
177 {
178 wifi.SetStandard (WIFI_STANDARD_80211ax);
179 channelStr += "BAND_6GHZ, 0}";
180 Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (48));
181 }
182 else if (frequency == 5)
183 {
184 wifi.SetStandard (WIFI_STANDARD_80211ax);
185 channelStr += "BAND_5GHZ, 0}";
186 }
187 else if (frequency == 2.4)
188 {
189 wifi.SetStandard (WIFI_STANDARD_80211ax);
190 channelStr += "BAND_2_4GHZ, 0}";
191 Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40));
192 }
193 else
194 {
195 std::cout << "Wrong frequency value!" << std::endl;
196 return 0;
197 }
198
199 std::ostringstream oss;
200 oss << "HeMcs" << mcs;
201 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", StringValue (oss.str ()),
202 "ControlMode", StringValue (oss.str ()));
203
204 Ssid ssid = Ssid ("ns3-80211ax");
205
206 if (phyModel == "Spectrum")
207 {
208 /*
209 * SingleModelSpectrumChannel cannot be used with 802.11ax because two
210 * spectrum models are required: one with 78.125 kHz bands for HE PPDUs
211 * and one with 312.5 kHz bands for, e.g., non-HT PPDUs (for more details,
212 * see issue #408 (CLOSED))
213 */
214 Ptr<MultiModelSpectrumChannel> spectrumChannel = CreateObject<MultiModelSpectrumChannel> ();
216 phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
217 phy.SetChannel (spectrumChannel);
218
219 mac.SetType ("ns3::StaWifiMac",
220 "Ssid", SsidValue (ssid));
221 phy.Set ("ChannelSettings", StringValue (channelStr));
222 staDevices = wifi.Install (phy, mac, wifiStaNodes);
223
224 if (dlAckSeqType != "NO-OFDMA")
225 {
226 mac.SetMultiUserScheduler ("ns3::RrMultiUserScheduler",
227 "EnableUlOfdma", BooleanValue (enableUlOfdma),
228 "EnableBsrp", BooleanValue (enableBsrp));
229 }
230 mac.SetType ("ns3::ApWifiMac",
231 "EnableBeaconJitter", BooleanValue (false),
232 "Ssid", SsidValue (ssid));
233 apDevice = wifi.Install (phy, mac, wifiApNode);
234 }
235 else
236 {
237 YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
239 phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
240 phy.SetChannel (channel.Create ());
241
242 mac.SetType ("ns3::StaWifiMac",
243 "Ssid", SsidValue (ssid));
244 phy.Set ("ChannelSettings", StringValue (channelStr));
245 staDevices = wifi.Install (phy, mac, wifiStaNodes);
246
247 mac.SetType ("ns3::ApWifiMac",
248 "EnableBeaconJitter", BooleanValue (false),
249 "Ssid", SsidValue (ssid));
250 apDevice = wifi.Install (phy, mac, wifiApNode);
251 }
252
253 RngSeedManager::SetSeed (1);
254 RngSeedManager::SetRun (1);
255 int64_t streamNumber = 100;
256 streamNumber += wifi.AssignStreams (apDevice, streamNumber);
257 streamNumber += wifi.AssignStreams (staDevices, streamNumber);
258
259 // Set guard interval and MPDU buffer size
260 Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HeConfiguration/GuardInterval", TimeValue (NanoSeconds (gi)));
261 Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HeConfiguration/MpduBufferSize", UintegerValue (useExtendedBlockAck ? 256 : 64));
262
263 // mobility.
265 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
266
267 positionAlloc->Add (Vector (0.0, 0.0, 0.0));
268 positionAlloc->Add (Vector (distance, 0.0, 0.0));
269 mobility.SetPositionAllocator (positionAlloc);
270
271 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
272
273 mobility.Install (wifiApNode);
274 mobility.Install (wifiStaNodes);
275
276 /* Internet stack*/
278 stack.Install (wifiApNode);
279 stack.Install (wifiStaNodes);
280
282 address.SetBase ("192.168.1.0", "255.255.255.0");
283 Ipv4InterfaceContainer staNodeInterfaces;
284 Ipv4InterfaceContainer apNodeInterface;
285
286 staNodeInterfaces = address.Assign (staDevices);
287 apNodeInterface = address.Assign (apDevice);
288
289 /* Setting applications */
290 ApplicationContainer serverApp;
291 if (udp)
292 {
293 //UDP flow
294 uint16_t port = 9;
295 UdpServerHelper server (port);
296 serverApp = server.Install (wifiStaNodes);
297 serverApp.Start (Seconds (0.0));
298 serverApp.Stop (Seconds (simulationTime + 1));
299
300 for (std::size_t i = 0; i < nStations; i++)
301 {
302 UdpClientHelper client (staNodeInterfaces.GetAddress (i), port);
303 client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
304 client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
305 client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
306 ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
307 clientApp.Start (Seconds (1.0));
308 clientApp.Stop (Seconds (simulationTime + 1));
309 }
310 }
311 else
312 {
313 //TCP flow
314 uint16_t port = 50000;
315 Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
316 PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
317 serverApp = packetSinkHelper.Install (wifiStaNodes);
318 serverApp.Start (Seconds (0.0));
319 serverApp.Stop (Seconds (simulationTime + 1));
320
321 for (std::size_t i = 0; i < nStations; i++)
322 {
323 OnOffHelper onoff ("ns3::TcpSocketFactory", Ipv4Address::GetAny ());
324 onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
325 onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
326 onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
327 onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
328 AddressValue remoteAddress (InetSocketAddress (staNodeInterfaces.GetAddress (i), port));
329 onoff.SetAttribute ("Remote", remoteAddress);
330 ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
331 clientApp.Start (Seconds (1.0));
332 clientApp.Stop (Seconds (simulationTime + 1));
333 }
334 }
335
336 Simulator::Schedule (Seconds (0), &Ipv4GlobalRoutingHelper::PopulateRoutingTables);
337
338 Simulator::Stop (Seconds (simulationTime + 1));
339 Simulator::Run ();
340
341 // When multiple stations are used, there are chances that association requests collide
342 // and hence the throughput may be lower than expected. Therefore, we relax the check
343 // that the throughput cannot decrease by introducing a scaling factor (or tolerance)
344 double tolerance = 0.10;
345 uint64_t rxBytes = 0;
346 if (udp)
347 {
348 for (uint32_t i = 0; i < serverApp.GetN (); i++)
349 {
350 rxBytes += payloadSize * DynamicCast<UdpServer> (serverApp.Get (i))->GetReceived ();
351 }
352 }
353 else
354 {
355 for (uint32_t i = 0; i < serverApp.GetN (); i++)
356 {
357 rxBytes += DynamicCast<PacketSink> (serverApp.Get (i))->GetTotalRx ();
358 }
359 }
360 double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); //Mbit/s
361
362 Simulator::Destroy ();
363
364 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << gi << " ns\t\t\t" << throughput << " Mbit/s" << std::endl;
365
366 //test first element
367 if (mcs == 0 && channelWidth == 20 && gi == 3200)
368 {
369 if (throughput * (1 + tolerance) < minExpectedThroughput)
370 {
371 NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
372 exit (1);
373 }
374 }
375 //test last element
376 if (mcs == 11 && channelWidth == 160 && gi == 800)
377 {
378 if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput * (1 + tolerance))
379 {
380 NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
381 exit (1);
382 }
383 }
384 //test previous throughput is smaller (for the same mcs)
385 if (throughput * (1 + tolerance) > previous)
386 {
387 previous = throughput;
388 }
389 else if (throughput > 0)
390 {
391 NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
392 exit (1);
393 }
394 //test previous throughput is smaller (for the same channel width and GI)
395 if (throughput * (1 + tolerance) > prevThroughput [index])
396 {
397 prevThroughput [index] = throughput;
398 }
399 else if (throughput > 0)
400 {
401 NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
402 exit (1);
403 }
404 index++;
405 gi /= 2;
406 }
407 channelWidth *= 2;
408 }
409 }
410 return 0;
411}
a polymophic address class
Definition: address.h:91
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.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
uint32_t GetN(void) 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:229
AttributeValue implementation for DataRate.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
Hold variables of type enum.
Definition: enum.h:55
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.
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
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:74
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:41
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
AttributeValue implementation for Time.
Definition: nstime.h:1308
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:44
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create WifiNetDevice objects
Definition: wifi-helper.h:323
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:849
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
@ WIFI_STANDARD_80211ax
address
Definition: first.py:44
stack
Definition: first.py:41
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35
staDevices
Definition: third.py:102
ssid
Definition: third.py:97
channel
Definition: third.py:92
mac
Definition: third.py:96
wifi
Definition: third.py:99
wifiApNode
Definition: third.py:90
mobility
Definition: third.py:107
wifiStaNodes
Definition: third.py:88
phy
Definition: third.py:93