A Discrete-Event Network Simulator
API
he-wifi-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/core-module.h"
22 #include "ns3/applications-module.h"
23 #include "ns3/wifi-module.h"
24 #include "ns3/mobility-module.h"
25 #include "ns3/internet-module.h"
26 
27 // This is a simple example in order to show how to configure an IEEE 802.11ax Wi-Fi network.
28 //
29 // It outputs the UDP or TCP goodput for every HE MCS value, which depends on the MCS value (0 to 11),
30 // the channel width (20, 40, 80 or 160 MHz) and the guard interval (800ns, 1600ns or 3200ns).
31 // The PHY bitrate is constant over all the simulation run. The user can also specify the distance between
32 // the access point and the station: the larger the distance the smaller the goodput.
33 //
34 // The simulation assumes a single station in an infrastructure network:
35 //
36 // STA AP
37 // * *
38 // | |
39 // n1 n2
40 //
41 //Packets in this simulation aren't marked with a QosTag so they are considered
42 //belonging to BestEffort Access Class (AC_BE).
43 
44 using namespace ns3;
45 
46 NS_LOG_COMPONENT_DEFINE ("he-wifi-network");
47 
48 int main (int argc, char *argv[])
49 {
50  bool udp = true;
51  bool useRts = false;
52  double simulationTime = 10; //seconds
53  double distance = 1.0; //meters
54  double frequency = 5.0; //whether 2.4 or 5.0 GHz
55  int mcs = -1; // -1 indicates an unset value
56  double minExpectedThroughput = 0;
57  double maxExpectedThroughput = 0;
58 
60  cmd.AddValue ("frequency", "Whether working in the 2.4 or 5.0 GHz band (other values gets rejected)", frequency);
61  cmd.AddValue ("distance", "Distance in meters between the station and the access point", distance);
62  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
63  cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
64  cmd.AddValue ("useRts", "Enable/disable RTS/CTS", useRts);
65  cmd.AddValue ("mcs", "if set, limit testing to a specific MCS (0-7)", mcs);
66  cmd.AddValue ("minExpectedThroughput", "if set, simulation fails if the lowest throughput is below this value", minExpectedThroughput);
67  cmd.AddValue ("maxExpectedThroughput", "if set, simulation fails if the highest throughput is above this value", maxExpectedThroughput);
68  cmd.Parse (argc,argv);
69 
70  if (useRts)
71  {
72  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("0"));
73  }
74 
75  double prevThroughput [12];
76  for (uint32_t l = 0; l < 12; l++)
77  {
78  prevThroughput[l] = 0;
79  }
80  std::cout << "MCS value" << "\t\t" << "Channel width" << "\t\t" << "GI" << "\t\t\t" << "Throughput" << '\n';
81  int minMcs = 0;
82  int maxMcs = 11;
83  if (mcs >= 0 && mcs <= 11)
84  {
85  minMcs = mcs;
86  maxMcs = mcs;
87  }
88  for (int mcs = minMcs; mcs <= maxMcs; mcs++)
89  {
90  uint8_t index = 0;
91  double previous = 0;
92  uint8_t maxChannelWidth = frequency == 2.4 ? 40 : 160;
93  for (int channelWidth = 20; channelWidth <= maxChannelWidth; ) //MHz
94  {
95  for (int gi = 3200; gi >= 800; ) //Nanoseconds
96  {
97  uint32_t payloadSize; //1500 byte IP packet
98  if (udp)
99  {
100  payloadSize = 1472; //bytes
101  }
102  else
103  {
104  payloadSize = 1448; //bytes
105  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
106  }
107 
108  NodeContainer wifiStaNode;
109  wifiStaNode.Create (1);
111  wifiApNode.Create (1);
112 
115  phy.SetChannel (channel.Create ());
116 
117  // Set guard interval
118  phy.Set ("GuardInterval", TimeValue (NanoSeconds (gi)));
119 
122  if (frequency == 5.0)
123  {
125  }
126  else if (frequency == 2.4)
127  {
129  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046));
130  }
131  else
132  {
133  std::cout << "Wrong frequency value!" << std::endl;
134  return 0;
135  }
136 
137  std::ostringstream oss;
138  oss << "HeMcs" << mcs;
139  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", StringValue (oss.str ()),
140  "ControlMode", StringValue (oss.str ()));
141 
142  Ssid ssid = Ssid ("ns3-80211ax");
143 
144  mac.SetType ("ns3::StaWifiMac",
145  "Ssid", SsidValue (ssid));
146 
147  NetDeviceContainer staDevice;
148  staDevice = wifi.Install (phy, mac, wifiStaNode);
149 
150  mac.SetType ("ns3::ApWifiMac",
151  "EnableBeaconJitter", BooleanValue (false),
152  "Ssid", SsidValue (ssid));
153 
154  NetDeviceContainer apDevice;
155  apDevice = wifi.Install (phy, mac, wifiApNode);
156 
157  // Set channel width
158  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (channelWidth));
159 
160  // mobility.
162  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
163 
164  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
165  positionAlloc->Add (Vector (distance, 0.0, 0.0));
166  mobility.SetPositionAllocator (positionAlloc);
167 
168  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
169 
170  mobility.Install (wifiApNode);
171  mobility.Install (wifiStaNode);
172 
173  /* Internet stack*/
175  stack.Install (wifiApNode);
176  stack.Install (wifiStaNode);
177 
179  address.SetBase ("192.168.1.0", "255.255.255.0");
180  Ipv4InterfaceContainer staNodeInterface;
181  Ipv4InterfaceContainer apNodeInterface;
182 
183  staNodeInterface = address.Assign (staDevice);
184  apNodeInterface = address.Assign (apDevice);
185 
186  /* Setting applications */
187  ApplicationContainer serverApp;
188  if (udp)
189  {
190  //UDP flow
191  uint16_t port = 9;
192  UdpServerHelper server (port);
193  serverApp = server.Install (wifiStaNode.Get (0));
194  serverApp.Start (Seconds (0.0));
195  serverApp.Stop (Seconds (simulationTime + 1));
196 
197  UdpClientHelper client (staNodeInterface.GetAddress (0), port);
198  client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
199  client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
200  client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
201  ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
202  clientApp.Start (Seconds (1.0));
203  clientApp.Stop (Seconds (simulationTime + 1));
204  }
205  else
206  {
207  //TCP flow
208  uint16_t port = 50000;
209  Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
210  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
211  serverApp = packetSinkHelper.Install (wifiStaNode.Get (0));
212  serverApp.Start (Seconds (0.0));
213  serverApp.Stop (Seconds (simulationTime + 1));
214 
215  OnOffHelper onoff ("ns3::TcpSocketFactory", Ipv4Address::GetAny ());
216  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
217  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
218  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
219  onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
220  AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
221  onoff.SetAttribute ("Remote", remoteAddress);
222  ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
223  clientApp.Start (Seconds (1.0));
224  clientApp.Stop (Seconds (simulationTime + 1));
225  }
226 
228 
229  Simulator::Stop (Seconds (simulationTime + 1));
230  Simulator::Run ();
232 
233  uint64_t rxBytes = 0;
234  if (udp)
235  {
236  rxBytes = payloadSize * DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
237  }
238  else
239  {
240  rxBytes = DynamicCast<PacketSink> (serverApp.Get (0))->GetTotalRx ();
241  }
242  double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); //Mbit/s
243  std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << gi << " ns\t\t\t" << throughput << " Mbit/s" << std::endl;
244  //test first element
245  if (mcs == 0 && channelWidth == 20 && gi == 3200)
246  {
247  if (throughput < minExpectedThroughput)
248  {
249  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
250  exit (1);
251  }
252  }
253  //test last element
254  if (mcs == 11 && channelWidth == 160 && gi == 800)
255  {
256  if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
257  {
258  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
259  exit (1);
260  }
261  }
262  //test previous throughput is smaller (for the same mcs)
263  if (throughput > previous)
264  {
265  previous = throughput;
266  }
267  else
268  {
269  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
270  exit (1);
271  }
272  //test previous throughput is smaller (for the same channel width and GI)
273  if (throughput > prevThroughput [index])
274  {
275  prevThroughput [index] = throughput;
276  }
277  else
278  {
279  NS_LOG_ERROR ("Obtained throughput " << throughput << " is not expected!");
280  exit (1);
281  }
282  index++;
283  gi /= 2;
284  }
285  channelWidth *= 2;
286  }
287  }
288  return 0;
289 }
tuple channel
Definition: third.py:85
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:133
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
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
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:700
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:777
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
HE PHY for the 2.4 GHz band (clause 26)
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:213
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
virtual void SetStandard(WifiPhyStandard standard)
Definition: wifi-helper.cc:723
void SetChannel(Ptr< YansWifiChannel > channel)
HE PHY for the 5 GHz band (clause 26)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
tuple phy
Definition: third.py:86
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:1069
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1031
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
Create a server application which waits for input UDP packets and uses the information carried into t...
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:729
tuple mac
Definition: third.py:92
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:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
tuple wifiApNode
Definition: third.py:83
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.
Ptr< Application > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
tuple ssid
Definition: third.py:93
manage and create wifi channel objects for the yans model.
create MAC layers for a ns3::WifiNetDevice.
tuple stack
Definition: first.py:34
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:35
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
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...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1007
AttributeValue implementation for Ssid.
Definition: ssid.h:110
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
void Add(Vector v)
Add a position to the list of positions.
void Parse(int argc, char *argv[])
Parse the program arguments.
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:253
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const