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 
63 using namespace ns3;
64 
65 NS_LOG_COMPONENT_DEFINE ("he-wifi-network");
66 
67 int 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  int mcs {-1}; // -1 indicates an unset value
78  uint32_t payloadSize = 700; // must fit in the max TX duration when transmitting at MCS 0 over an RU of 26 tones
79  std::string phyModel {"Yans"};
80  double minExpectedThroughput {0};
81  double maxExpectedThroughput {0};
82 
83  CommandLine cmd (__FILE__);
84  cmd.AddValue ("frequency", "Whether working in the 2.4, 5 or 6 GHz band (other values gets rejected)", frequency);
85  cmd.AddValue ("distance", "Distance in meters between the station and the access point", distance);
86  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
87  cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
88  cmd.AddValue ("useRts", "Enable/disable RTS/CTS", useRts);
89  cmd.AddValue ("useExtendedBlockAck", "Enable/disable use of extended BACK", useExtendedBlockAck);
90  cmd.AddValue ("nStations", "Number of non-AP HE stations", nStations);
91  cmd.AddValue ("dlAckType", "Ack sequence type for DL OFDMA (NO-OFDMA, ACK-SU-FORMAT, MU-BAR, AGGR-MU-BAR)",
92  dlAckSeqType);
93  cmd.AddValue ("mcs", "if set, limit testing to a specific MCS (0-11)", mcs);
94  cmd.AddValue ("payloadSize", "The application payload size in bytes", payloadSize);
95  cmd.AddValue ("phyModel", "PHY model to use when OFDMA is disabled (Yans or Spectrum). If OFDMA is enabled then Spectrum is automatically selected", phyModel);
96  cmd.AddValue ("minExpectedThroughput", "if set, simulation fails if the lowest throughput is below this value", minExpectedThroughput);
97  cmd.AddValue ("maxExpectedThroughput", "if set, simulation fails if the highest throughput is above this value", maxExpectedThroughput);
98  cmd.Parse (argc,argv);
99 
100  if (useRts)
101  {
102  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("0"));
103  }
104 
105  if (dlAckSeqType == "ACK-SU-FORMAT")
106  {
107  Config::SetDefault ("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
109  }
110  else if (dlAckSeqType == "MU-BAR")
111  {
112  Config::SetDefault ("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
114  }
115  else if (dlAckSeqType == "AGGR-MU-BAR")
116  {
117  Config::SetDefault ("ns3::WifiDefaultAckManager::DlMuAckSequenceType",
119  }
120  else if (dlAckSeqType != "NO-OFDMA")
121  {
122  NS_ABORT_MSG ("Invalid DL ack sequence type (must be NO-OFDMA, ACK-SU-FORMAT, MU-BAR or AGGR-MU-BAR)");
123  }
124 
125  if (phyModel != "Yans" && phyModel != "Spectrum")
126  {
127  NS_ABORT_MSG ("Invalid PHY model (must be Yans or Spectrum)");
128  }
129  if (dlAckSeqType != "NO-OFDMA")
130  {
131  // SpectrumWifiPhy is required for OFDMA
132  phyModel = "Spectrum";
133  }
134 
135  double prevThroughput [12];
136  for (uint32_t l = 0; l < 12; l++)
137  {
138  prevThroughput[l] = 0;
139  }
140  std::cout << "MCS value" << "\t\t" << "Channel width" << "\t\t" << "GI" << "\t\t\t" << "Throughput" << '\n';
141  int minMcs = 0;
142  int maxMcs = 11;
143  if (mcs >= 0 && mcs <= 11)
144  {
145  minMcs = mcs;
146  maxMcs = mcs;
147  }
148  for (int mcs = minMcs; mcs <= maxMcs; mcs++)
149  {
150  uint8_t index = 0;
151  double previous = 0;
152  uint8_t maxChannelWidth = frequency == 2.4 ? 40 : 160;
153  for (int channelWidth = 20; channelWidth <= maxChannelWidth; ) //MHz
154  {
155  for (int gi = 3200; gi >= 800; ) //Nanoseconds
156  {
157  if (!udp)
158  {
159  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
160  }
161 
163  wifiStaNodes.Create (nStations);
165  wifiApNode.Create (1);
166 
167  NetDeviceContainer apDevice, staDevices;
170 
171  if (frequency == 6)
172  {
173  wifi.SetStandard (WIFI_STANDARD_80211ax_6GHZ);
174  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (48));
175  }
176  else if (frequency == 5)
177  {
178  wifi.SetStandard (WIFI_STANDARD_80211ax_5GHZ);
179  }
180  else if (frequency == 2.4)
181  {
182  wifi.SetStandard (WIFI_STANDARD_80211ax_2_4GHZ);
183  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40));
184  }
185  else
186  {
187  std::cout << "Wrong frequency value!" << std::endl;
188  return 0;
189  }
190 
191  std::ostringstream oss;
192  oss << "HeMcs" << mcs;
193  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", StringValue (oss.str ()),
194  "ControlMode", StringValue (oss.str ()));
195 
196  Ssid ssid = Ssid ("ns3-80211ax");
197 
198  if (phyModel == "Spectrum")
199  {
200  /*
201  * SingleModelSpectrumChannel cannot be used with 802.11ax because two
202  * spectrum models are required: one with 78.125 kHz bands for HE PPDUs
203  * and one with 312.5 kHz bands for, e.g., non-HT PPDUs (for more details,
204  * see issue #408 (CLOSED))
205  */
206  Ptr<MultiModelSpectrumChannel> spectrumChannel = CreateObject<MultiModelSpectrumChannel> ();
208  phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
209  phy.SetChannel (spectrumChannel);
210 
211  mac.SetType ("ns3::StaWifiMac",
212  "Ssid", SsidValue (ssid));
213  phy.Set ("ChannelWidth", UintegerValue (channelWidth));
214  staDevices = wifi.Install (phy, mac, wifiStaNodes);
215 
216  if (dlAckSeqType != "NO-OFDMA")
217  {
218  mac.SetMultiUserScheduler ("ns3::RrMultiUserScheduler",
219  "EnableUlOfdma", BooleanValue (false),
220  "EnableBsrp", BooleanValue (false));
221  }
222  mac.SetType ("ns3::ApWifiMac",
223  "EnableBeaconJitter", BooleanValue (false),
224  "Ssid", SsidValue (ssid));
225  phy.Set ("ChannelWidth", UintegerValue (channelWidth));
226  apDevice = wifi.Install (phy, mac, wifiApNode);
227  }
228  else
229  {
232  phy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
233  phy.SetChannel (channel.Create ());
234 
235  mac.SetType ("ns3::StaWifiMac",
236  "Ssid", SsidValue (ssid));
237  phy.Set ("ChannelWidth", UintegerValue (channelWidth));
238  staDevices = wifi.Install (phy, mac, wifiStaNodes);
239 
240  mac.SetType ("ns3::ApWifiMac",
241  "EnableBeaconJitter", BooleanValue (false),
242  "Ssid", SsidValue (ssid));
243  phy.Set ("ChannelWidth", UintegerValue (channelWidth));
244  apDevice = wifi.Install (phy, mac, wifiApNode);
245  }
246 
249  int64_t streamNumber = 100;
250  streamNumber += wifi.AssignStreams (apDevice, streamNumber);
251  streamNumber += wifi.AssignStreams (staDevices, streamNumber);
252 
253  // Set guard interval and MPDU buffer size
254  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HeConfiguration/GuardInterval", TimeValue (NanoSeconds (gi)));
255  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HeConfiguration/MpduBufferSize", UintegerValue (useExtendedBlockAck ? 256 : 64));
256 
257  // mobility.
259  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
260 
261  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
262  positionAlloc->Add (Vector (distance, 0.0, 0.0));
263  mobility.SetPositionAllocator (positionAlloc);
264 
265  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
266 
267  mobility.Install (wifiApNode);
268  mobility.Install (wifiStaNodes);
269 
270  /* Internet stack*/
272  stack.Install (wifiApNode);
273  stack.Install (wifiStaNodes);
274 
276  address.SetBase ("192.168.1.0", "255.255.255.0");
277  Ipv4InterfaceContainer staNodeInterfaces;
278  Ipv4InterfaceContainer apNodeInterface;
279 
280  staNodeInterfaces = address.Assign (staDevices);
281  apNodeInterface = address.Assign (apDevice);
282 
283  /* Setting applications */
284  ApplicationContainer serverApp;
285  if (udp)
286  {
287  //UDP flow
288  uint16_t port = 9;
289  UdpServerHelper server (port);
290  serverApp = server.Install (wifiStaNodes);
291  serverApp.Start (Seconds (0.0));
292  serverApp.Stop (Seconds (simulationTime + 1));
293 
294  for (std::size_t i = 0; i < nStations; i++)
295  {
296  UdpClientHelper client (staNodeInterfaces.GetAddress (i), port);
297  client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
298  client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
299  client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
300  ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
301  clientApp.Start (Seconds (1.0));
302  clientApp.Stop (Seconds (simulationTime + 1));
303  }
304  }
305  else
306  {
307  //TCP flow
308  uint16_t port = 50000;
309  Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
310  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
311  serverApp = packetSinkHelper.Install (wifiStaNodes);
312  serverApp.Start (Seconds (0.0));
313  serverApp.Stop (Seconds (simulationTime + 1));
314 
315  for (std::size_t i = 0; i < nStations; i++)
316  {
317  OnOffHelper onoff ("ns3::TcpSocketFactory", Ipv4Address::GetAny ());
318  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
319  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
320  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
321  onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
322  AddressValue remoteAddress (InetSocketAddress (staNodeInterfaces.GetAddress (i), port));
323  onoff.SetAttribute ("Remote", remoteAddress);
324  ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
325  clientApp.Start (Seconds (1.0));
326  clientApp.Stop (Seconds (simulationTime + 1));
327  }
328  }
329 
331 
332  Simulator::Stop (Seconds (simulationTime + 1));
333  Simulator::Run ();
334 
335  // When multiple stations are used, there are chances that association requests collide
336  // and hence the throughput may be lower than expected. Therefore, we relax the check
337  // that the throughput cannot decrease by introducing a scaling factor (or tolerance)
338  double tolerance = 0.10;
339  uint64_t rxBytes = 0;
340  if (udp)
341  {
342  for (uint32_t i = 0; i < serverApp.GetN (); i++)
343  {
344  rxBytes += payloadSize * DynamicCast<UdpServer> (serverApp.Get (i))->GetReceived ();
345  }
346  }
347  else
348  {
349  for (uint32_t i = 0; i < serverApp.GetN (); i++)
350  {
351  rxBytes += DynamicCast<PacketSink> (serverApp.Get (i))->GetTotalRx ();
352  }
353  }
354  double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); //Mbit/s
355 
357 
358  std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << gi << " ns\t\t\t" << throughput << " Mbit/s" << std::endl;
359 
360  //test first element
361  if (mcs == 0 && channelWidth == 20 && gi == 3200)
362  {
363  if (throughput * (1 + tolerance) < minExpectedThroughput)
364  {
365  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
366  exit (1);
367  }
368  }
369  //test last element
370  if (mcs == 11 && channelWidth == 160 && gi == 800)
371  {
372  if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput * (1 + tolerance))
373  {
374  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
375  exit (1);
376  }
377  }
378  //test previous throughput is smaller (for the same mcs)
379  if (throughput * (1 + tolerance) > previous)
380  {
381  previous = throughput;
382  }
383  else
384  {
385  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
386  exit (1);
387  }
388  //test previous throughput is smaller (for the same channel width and GI)
389  if (throughput * (1 + tolerance) > prevThroughput [index])
390  {
391  prevThroughput [index] = throughput;
392  }
393  else
394  {
395  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
396  exit (1);
397  }
398  index++;
399  gi /= 2;
400  }
401  channelWidth *= 2;
402  }
403  }
404  return 0;
405 }
holds a vector of ns3::Application pointers.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
an Inet address class
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:36
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
holds a vector of std::pair of Ptr<Ipv4> and interface index.
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the YANS model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
aggregate IP/TCP/UDP functionality to existing Nodes.
staDevices
Definition: third.py:103
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
helps to create WifiNetDevice objects
Definition: wifi-helper.h:326
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
stack
Definition: first.py:41
static void SetRun(uint64_t run)
Set the run number of simulation.
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:92
mobility
Definition: third.py:108
phy
Definition: third.py:93
Hold variables of type enum.
Definition: enum.h:54
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
AttributeValue implementation for Time.
Definition: nstime.h:1353
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1313
Hold an unsigned integer type.
Definition: uinteger.h:44
ssid
Definition: third.py:100
holds a vector of ns3::NetDevice pointers
mac
Definition: third.py:99
Create a server application which waits for input UDP packets and uses the information carried into t...
wifiApNode
Definition: third.py:90
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:227
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:44
manage and create wifi channel objects for the YANS model.
create MAC layers for a ns3::WifiNetDevice.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:35
static void SetSeed(uint32_t seed)
Set the seed.
wifi
Definition: third.py:96
Helper class used to assign positions and mobility models to nodes.
AttributeValue implementation for Address.
Definition: address.h:278
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 DataRate.
Definition: data-rate.h:298
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
AttributeValue implementation for Ssid.
Definition: ssid.h:105
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
void Add(Vector v)
Add a position to the list of positions.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
wifiStaNodes
Definition: third.py:88
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
Include Radiotap link layer information.
Definition: wifi-helper.h:180
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
uint32_t GetN(void) const
Get the number of Ptr<Application> stored in this container.
Make it easy to create and manage PHY objects for the spectrum model.