A Discrete-Event Network Simulator
API
wifi-80211n-mimo.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License version 2 as
5  * published by the Free Software Foundation;
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15  *
16  * Author: S├ębastien Deronne <sebastien.deronne@gmail.com>
17  */
18 
19 // This example is used to validate 802.11n MIMO.
20 //
21 // It outputs plots of the throughput versus the distance
22 // for every HT MCS value and from 1 to 4 MIMO streams.
23 //
24 // The simulation assumes a single station in an infrastructure network:
25 //
26 // STA AP
27 // * *
28 // | |
29 // n1 n2
30 //
31 // The user can choose whether UDP or TCP should be used and can configure
32 // some 802.11n parameters (frequency, channel width and guard interval).
33 
34 #include "ns3/gnuplot.h"
35 #include "ns3/command-line.h"
36 #include "ns3/config.h"
37 #include "ns3/uinteger.h"
38 #include "ns3/boolean.h"
39 #include "ns3/double.h"
40 #include "ns3/string.h"
41 #include "ns3/yans-wifi-helper.h"
42 #include "ns3/ssid.h"
43 #include "ns3/mobility-helper.h"
44 #include "ns3/internet-stack-helper.h"
45 #include "ns3/ipv4-address-helper.h"
46 #include "ns3/udp-client-server-helper.h"
47 #include "ns3/packet-sink-helper.h"
48 #include "ns3/on-off-helper.h"
49 #include "ns3/ipv4-global-routing-helper.h"
50 #include "ns3/packet-sink.h"
51 #include "ns3/yans-wifi-channel.h"
52 
53 using namespace ns3;
54 
55 int main (int argc, char *argv[])
56 {
57  std::ofstream file ("80211n-mimo-throughput.plt");
58 
59  std::vector <std::string> modes;
60  modes.push_back ("HtMcs0");
61  modes.push_back ("HtMcs1");
62  modes.push_back ("HtMcs2");
63  modes.push_back ("HtMcs3");
64  modes.push_back ("HtMcs4");
65  modes.push_back ("HtMcs5");
66  modes.push_back ("HtMcs6");
67  modes.push_back ("HtMcs7");
68  modes.push_back ("HtMcs8");
69  modes.push_back ("HtMcs9");
70  modes.push_back ("HtMcs10");
71  modes.push_back ("HtMcs11");
72  modes.push_back ("HtMcs12");
73  modes.push_back ("HtMcs13");
74  modes.push_back ("HtMcs14");
75  modes.push_back ("HtMcs15");
76  modes.push_back ("HtMcs16");
77  modes.push_back ("HtMcs17");
78  modes.push_back ("HtMcs18");
79  modes.push_back ("HtMcs19");
80  modes.push_back ("HtMcs20");
81  modes.push_back ("HtMcs21");
82  modes.push_back ("HtMcs22");
83  modes.push_back ("HtMcs23");
84  modes.push_back ("HtMcs24");
85  modes.push_back ("HtMcs25");
86  modes.push_back ("HtMcs26");
87  modes.push_back ("HtMcs27");
88  modes.push_back ("HtMcs28");
89  modes.push_back ("HtMcs29");
90  modes.push_back ("HtMcs30");
91  modes.push_back ("HtMcs31");
92 
93  bool udp = true;
94  double simulationTime = 5; //seconds
95  double frequency = 5.0; //whether 2.4 or 5.0 GHz
96  double step = 5; //meters
97  bool shortGuardInterval = false;
98  bool channelBonding = false;
99 
100  CommandLine cmd (__FILE__);
101  cmd.AddValue ("step", "Granularity of the results to be plotted in meters", step);
102  cmd.AddValue ("simulationTime", "Simulation time per step (in seconds)", simulationTime);
103  cmd.AddValue ("channelBonding", "Enable/disable channel bonding (channel width = 20 MHz if false, channel width = 40 MHz if true)", channelBonding);
104  cmd.AddValue ("shortGuardInterval", "Enable/disable short guard interval", shortGuardInterval);
105  cmd.AddValue ("frequency", "Whether working in the 2.4 or 5.0 GHz band (other values gets rejected)", frequency);
106  cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
107  cmd.Parse (argc,argv);
108 
109  Gnuplot plot = Gnuplot ("80211n-mimo-throughput.eps");
110 
111  for (uint32_t i = 0; i < modes.size (); i++) //MCS
112  {
113  std::cout << modes[i] << std::endl;
114  Gnuplot2dDataset dataset (modes[i]);
115  for (double d = 0; d <= 100; ) //distance
116  {
117  std::cout << "Distance = " << d << "m: " << std::endl;
118  uint32_t payloadSize; //1500 byte IP packet
119  if (udp)
120  {
121  payloadSize = 1472; //bytes
122  }
123  else
124  {
125  payloadSize = 1448; //bytes
126  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
127  }
128 
129  uint8_t nStreams = 1 + (i / 8); //number of MIMO streams
130 
131  NodeContainer wifiStaNode;
132  wifiStaNode.Create (1);
134  wifiApNode.Create (1);
135 
138  phy.SetChannel (channel.Create ());
139 
140  // Set MIMO capabilities
141  phy.Set ("Antennas", UintegerValue (nStreams));
142  phy.Set ("MaxSupportedTxSpatialStreams", UintegerValue (nStreams));
143  phy.Set ("MaxSupportedRxSpatialStreams", UintegerValue (nStreams));
144 
147  if (frequency == 5.0)
148  {
149  wifi.SetStandard (WIFI_STANDARD_80211n_5GHZ);
150  }
151  else if (frequency == 2.4)
152  {
153  wifi.SetStandard (WIFI_STANDARD_80211n_2_4GHZ);
154  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046));
155  }
156  else
157  {
158  std::cout << "Wrong frequency value!" << std::endl;
159  return 0;
160  }
161 
162  StringValue ctrlRate;
163  if (frequency == 2.4)
164  {
165  ctrlRate = StringValue ("ErpOfdmRate24Mbps");
166  }
167  else
168  {
169  ctrlRate = StringValue ("OfdmRate24Mbps");
170  }
171  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
172  "DataMode", StringValue (modes[i]),
173  "ControlMode", ctrlRate);
174 
175  Ssid ssid = Ssid ("ns3-80211n");
176 
177  mac.SetType ("ns3::StaWifiMac",
178  "Ssid", SsidValue (ssid));
179 
180  NetDeviceContainer staDevice;
181  staDevice = wifi.Install (phy, mac, wifiStaNode);
182 
183  mac.SetType ("ns3::ApWifiMac",
184  "Ssid", SsidValue (ssid));
185 
186  NetDeviceContainer apDevice;
187  apDevice = wifi.Install (phy, mac, wifiApNode);
188 
189  // Set channel width
190  if (channelBonding)
191  {
192  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (40));
193  }
194 
195  // Set guard interval
196  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported", BooleanValue (shortGuardInterval));
197 
198  // mobility.
200  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
201 
202  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
203  positionAlloc->Add (Vector (d, 0.0, 0.0));
204  mobility.SetPositionAllocator (positionAlloc);
205 
206  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
207 
208  mobility.Install (wifiApNode);
209  mobility.Install (wifiStaNode);
210 
211  /* Internet stack*/
213  stack.Install (wifiApNode);
214  stack.Install (wifiStaNode);
215 
217  address.SetBase ("192.168.1.0", "255.255.255.0");
218  Ipv4InterfaceContainer staNodeInterface;
219  Ipv4InterfaceContainer apNodeInterface;
220 
221  staNodeInterface = address.Assign (staDevice);
222  apNodeInterface = address.Assign (apDevice);
223 
224  /* Setting applications */
225  ApplicationContainer serverApp;
226  if (udp)
227  {
228  //UDP flow
229  uint16_t port = 9;
230  UdpServerHelper server (port);
231  serverApp = server.Install (wifiStaNode.Get (0));
232  serverApp.Start (Seconds (0.0));
233  serverApp.Stop (Seconds (simulationTime + 1));
234 
235  UdpClientHelper client (staNodeInterface.GetAddress (0), port);
236  client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
237  client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
238  client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
239  ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
240  clientApp.Start (Seconds (1.0));
241  clientApp.Stop (Seconds (simulationTime + 1));
242  }
243  else
244  {
245  //TCP flow
246  uint16_t port = 50000;
247  Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
248  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
249  serverApp = packetSinkHelper.Install (wifiStaNode.Get (0));
250  serverApp.Start (Seconds (0.0));
251  serverApp.Stop (Seconds (simulationTime + 1));
252 
253  OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
254  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
255  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
256  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
257  onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
258  AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
259  onoff.SetAttribute ("Remote", remoteAddress);
260  ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
261  clientApp.Start (Seconds (1.0));
262  clientApp.Stop (Seconds (simulationTime + 1));
263  }
264 
266 
267  Simulator::Stop (Seconds (simulationTime + 1));
268  Simulator::Run ();
269 
270  double throughput = 0;
271  if (udp)
272  {
273  //UDP
274  uint64_t totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
275  throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); //Mbit/s
276  }
277  else
278  {
279  //TCP
280  uint64_t totalPacketsThrough = DynamicCast<PacketSink> (serverApp.Get (0))->GetTotalRx ();
281  throughput = totalPacketsThrough * 8 / (simulationTime * 1000000.0); //Mbit/s
282  }
283  dataset.Add (d, throughput);
284  std::cout << throughput << " Mbit/s" << std::endl;
285  d += step;
287  }
288  plot.AddDataset (dataset);
289  }
290 
291  plot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
292  plot.SetLegend ("Distance (Meters)", "Throughput (Mbit/s)");
293  plot.SetExtra ("set xrange [0:100]\n\
294 set yrange [0:600]\n\
295 set ytics 0,50,600\n\
296 set style line 1 dashtype 1 linewidth 5\n\
297 set style line 2 dashtype 1 linewidth 5\n\
298 set style line 3 dashtype 1 linewidth 5\n\
299 set style line 4 dashtype 1 linewidth 5\n\
300 set style line 5 dashtype 1 linewidth 5\n\
301 set style line 6 dashtype 1 linewidth 5\n\
302 set style line 7 dashtype 1 linewidth 5\n\
303 set style line 8 dashtype 1 linewidth 5\n\
304 set style line 9 dashtype 2 linewidth 5\n\
305 set style line 10 dashtype 2 linewidth 5\n\
306 set style line 11 dashtype 2 linewidth 5\n\
307 set style line 12 dashtype 2 linewidth 5\n\
308 set style line 13 dashtype 2 linewidth 5\n\
309 set style line 14 dashtype 2 linewidth 5\n\
310 set style line 15 dashtype 2 linewidth 5\n\
311 set style line 16 dashtype 2 linewidth 5\n\
312 set style line 17 dashtype 3 linewidth 5\n\
313 set style line 18 dashtype 3 linewidth 5\n\
314 set style line 19 dashtype 3 linewidth 5\n\
315 set style line 20 dashtype 3 linewidth 5\n\
316 set style line 21 dashtype 3 linewidth 5\n\
317 set style line 22 dashtype 3 linewidth 5\n\
318 set style line 23 dashtype 3 linewidth 5\n\
319 set style line 24 dashtype 3 linewidth 5\n\
320 set style line 25 dashtype 4 linewidth 5\n\
321 set style line 26 dashtype 4 linewidth 5\n\
322 set style line 27 dashtype 4 linewidth 5\n\
323 set style line 28 dashtype 4 linewidth 5\n\
324 set style line 29 dashtype 4 linewidth 5\n\
325 set style line 30 dashtype 4 linewidth 5\n\
326 set style line 31 dashtype 4 linewidth 5\n\
327 set style line 32 dashtype 4 linewidth 5\n\
328 set style increment user" );
329  plot.GenerateOutput (file);
330  file.close ();
331 
332  return 0;
333 }
334 
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::WIFI_STANDARD_80211n_5GHZ
@ WIFI_STANDARD_80211n_5GHZ
Definition: wifi-standards.h:132
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::ApplicationContainer::Get
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
Definition: application-container.cc:62
ns3::YansWifiChannelHelper::Default
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
Definition: yans-wifi-helper.cc:41
ns3::UdpClientHelper
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
Definition: udp-client-server-helper.h:94
ns3::YansWifiPhyHelper
Make it easy to create and manage PHY objects for the YANS model.
Definition: yans-wifi-helper.h:161
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::ListPositionAllocator::Add
void Add(Vector v)
Add a position to the list of positions.
Definition: position-allocator.cc:70
ns3::Config::Set
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
ns3::Gnuplot::SetLegend
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::ApplicationContainer::Stop
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Definition: application-container.cc:107
ns3::WifiHelper
helps to create WifiNetDevice objects
Definition: wifi-helper.h:327
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::AddressValue
AttributeValue implementation for Address.
Definition: address.h:278
third.channel
channel
Definition: third.py:92
ns3::Gnuplot
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:372
ns3::Gnuplot::SetTerminal
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
ns3::Gnuplot::GenerateOutput
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
third.mac
mac
Definition: third.py:99
ns3::Gnuplot::SetExtra
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:743
ns3::Gnuplot2dDataset
Class to represent a 2D points plot.
Definition: gnuplot.h:118
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::SsidValue
AttributeValue implementation for Ssid.
Definition: ssid.h:105
ns3::Ssid
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
ns3::UdpServerHelper
Create a server application which waits for input UDP packets and uses the information carried into t...
Definition: udp-client-server-helper.h:38
third.wifi
wifi
Definition: third.py:96
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition: ipv4-global-routing-helper.cc:61
first.stack
stack
Definition: first.py:41
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::Address
a polymophic address class
Definition: address.h:91
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
first.address
address
Definition: first.py:44
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
third.wifiApNode
wifiApNode
Definition: third.py:90
second.cmd
cmd
Definition: second.py:35
ns3::Gnuplot::AddDataset
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition: packet-sink-helper.h:36
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition: ipv4-address.cc:395
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
third.ssid
ssid
Definition: third.py:100
ns3::TimeValue
AttributeValue implementation for Time.
Definition: nstime.h:1353
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::YansWifiChannelHelper
manage and create wifi channel objects for the YANS model.
Definition: yans-wifi-helper.h:37
ns3::WifiMacHelper
create MAC layers for a ns3::WifiNetDevice.
Definition: wifi-mac-helper.h:48
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
ns3::WIFI_STANDARD_80211n_2_4GHZ
@ WIFI_STANDARD_80211n_2_4GHZ
Definition: wifi-standards.h:131
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
create-module.file
file
Definition: create-module.py:634
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43
third.mobility
mobility
Definition: third.py:108
third.phy
phy
Definition: third.py:93
port
uint16_t port
Definition: dsdv-manet.cc:45