A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  * Authors: 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/core-module.h"
35 #include "ns3/network-module.h"
36 #include "ns3/applications-module.h"
37 #include "ns3/wifi-module.h"
38 #include "ns3/mobility-module.h"
39 #include "ns3/ipv4-global-routing-helper.h"
40 #include "ns3/internet-module.h"
41 #include "ns3/gnuplot.h"
42 #include <fstream>
43 #include <vector>
44 #include <cmath>
45 
46 using namespace ns3;
47 
48 int main (int argc, char *argv[])
49 {
50  std::ofstream file ("80211n-mimo-throughput.plt");
51 
52  std::vector <std::string> modes;
53  modes.push_back ("HtMcs0");
54  modes.push_back ("HtMcs1");
55  modes.push_back ("HtMcs2");
56  modes.push_back ("HtMcs3");
57  modes.push_back ("HtMcs4");
58  modes.push_back ("HtMcs5");
59  modes.push_back ("HtMcs6");
60  modes.push_back ("HtMcs7");
61  modes.push_back ("HtMcs8");
62  modes.push_back ("HtMcs9");
63  modes.push_back ("HtMcs10");
64  modes.push_back ("HtMcs11");
65  modes.push_back ("HtMcs12");
66  modes.push_back ("HtMcs13");
67  modes.push_back ("HtMcs14");
68  modes.push_back ("HtMcs15");
69  modes.push_back ("HtMcs16");
70  modes.push_back ("HtMcs17");
71  modes.push_back ("HtMcs18");
72  modes.push_back ("HtMcs19");
73  modes.push_back ("HtMcs20");
74  modes.push_back ("HtMcs21");
75  modes.push_back ("HtMcs22");
76  modes.push_back ("HtMcs23");
77  modes.push_back ("HtMcs24");
78  modes.push_back ("HtMcs25");
79  modes.push_back ("HtMcs26");
80  modes.push_back ("HtMcs27");
81  modes.push_back ("HtMcs28");
82  modes.push_back ("HtMcs29");
83  modes.push_back ("HtMcs30");
84  modes.push_back ("HtMcs31");
85 
86  bool udp = true;
87  double simulationTime = 5; //seconds
88  double frequency = 5.0; //whether 2.4 or 5.0 GHz
89  double step = 5; //meters
90  bool shortGuardInterval = false;
91  bool channelBonding = false;
92 
94  cmd.AddValue ("step", "Granularity of the results to be plotted in meters", step);
95  cmd.AddValue ("channelBonding", "Enable/disable channel bonding (channel width = 20 MHz if false, channel width = 40 MHz if true)", channelBonding);
96  cmd.AddValue ("shortGuardInterval", "Enable/disable short guard interval", shortGuardInterval);
97  cmd.AddValue ("frequency", "Whether working in the 2.4 or 5.0 GHz band (other values gets rejected)", frequency);
98  cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
99  cmd.Parse (argc,argv);
100 
101  Gnuplot plot = Gnuplot ("80211n-mimo-throughput.eps");
102 
103  for (uint32_t i = 0; i < modes.size (); i++) //MCS
104  {
105  std::cout << modes[i] << std::endl;
106  Gnuplot2dDataset dataset (modes[i]);
107  for (int d = 0; d <= 100; ) //distance
108  {
109  std::cout << "Distance = " << d << "m: "<< std::endl;
110  uint32_t payloadSize; //1500 byte IP packet
111  if (udp)
112  {
113  payloadSize = 1472; //bytes
114  }
115  else
116  {
117  payloadSize = 1448; //bytes
118  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
119  }
120 
121  uint8_t nStreams = 1 + (i / 8); //number of MIMO streams
122 
123  NodeContainer wifiStaNode;
124  wifiStaNode.Create (1);
126  wifiApNode.Create (1);
127 
130  phy.SetChannel (channel.Create ());
131 
132  // Set guard interval
133  phy.Set ("ShortGuardEnabled", BooleanValue (shortGuardInterval));
134  // Set MIMO capabilities
135  phy.Set ("TxAntennas", UintegerValue (nStreams));
136  phy.Set ("RxAntennas", UintegerValue (nStreams));
137 
140  if (frequency == 5.0)
141  {
143  }
144  else if (frequency == 2.4)
145  {
147  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046));
148  }
149  else
150  {
151  std::cout<<"Wrong frequency value!"<<std::endl;
152  return 0;
153  }
154 
155  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", StringValue (modes[i]),
156  "ControlMode", StringValue (modes[i]));
157 
158  Ssid ssid = Ssid ("ns3-80211n");
159 
160  mac.SetType ("ns3::StaWifiMac",
161  "Ssid", SsidValue (ssid),
162  "ActiveProbing", BooleanValue (false));
163 
164  NetDeviceContainer staDevice;
165  staDevice = wifi.Install (phy, mac, wifiStaNode);
166 
167  mac.SetType ("ns3::ApWifiMac",
168  "Ssid", SsidValue (ssid));
169 
170  NetDeviceContainer apDevice;
171  apDevice = wifi.Install (phy, mac, wifiApNode);
172 
173  // Set channel width
174  if (channelBonding)
175  {
176  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (40));
177  }
178 
179  // mobility.
181  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
182 
183  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
184  positionAlloc->Add (Vector (d, 0.0, 0.0));
185  mobility.SetPositionAllocator (positionAlloc);
186 
187  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
188 
189  mobility.Install (wifiApNode);
190  mobility.Install (wifiStaNode);
191 
192  /* Internet stack*/
194  stack.Install (wifiApNode);
195  stack.Install (wifiStaNode);
196 
198 
199  address.SetBase ("192.168.1.0", "255.255.255.0");
200  Ipv4InterfaceContainer staNodeInterface;
201  Ipv4InterfaceContainer apNodeInterface;
202 
203  staNodeInterface = address.Assign (staDevice);
204  apNodeInterface = address.Assign (apDevice);
205 
206  /* Setting applications */
207  ApplicationContainer serverApp, sinkApp;
208  if (udp)
209  {
210  //UDP flow
211  UdpServerHelper myServer (9);
212  serverApp = myServer.Install (wifiStaNode.Get (0));
213  serverApp.Start (Seconds (0.0));
214  serverApp.Stop (Seconds (simulationTime + 1));
215 
216  UdpClientHelper myClient (staNodeInterface.GetAddress (0), 9);
217  myClient.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
218  myClient.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
219  myClient.SetAttribute ("PacketSize", UintegerValue (payloadSize));
220 
221  ApplicationContainer clientApp = myClient.Install (wifiApNode.Get (0));
222  clientApp.Start (Seconds (1.0));
223  clientApp.Stop (Seconds (simulationTime + 1));
224  }
225  else
226  {
227  //TCP flow
228  uint16_t port = 50000;
229  Address apLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
230  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", apLocalAddress);
231  sinkApp = packetSinkHelper.Install (wifiStaNode.Get (0));
232 
233  sinkApp.Start (Seconds (0.0));
234  sinkApp.Stop (Seconds (simulationTime + 1));
235 
236  OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
237  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
238  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
239  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
240  onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
242 
243  AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
244  onoff.SetAttribute ("Remote", remoteAddress);
245  apps.Add (onoff.Install (wifiApNode.Get (0)));
246  apps.Start (Seconds (1.0));
247  apps.Stop (Seconds (simulationTime + 1));
248  }
249 
251 
252  Simulator::Stop (Seconds (simulationTime + 1));
253  Simulator::Run ();
255 
256  double throughput = 0;
257  if (udp)
258  {
259  //UDP
260  uint32_t totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
261  throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); //Mbit/s
262  }
263  else
264  {
265  //TCP
266  uint32_t totalPacketsThrough = DynamicCast<PacketSink> (sinkApp.Get (0))->GetTotalRx ();
267  throughput = totalPacketsThrough * 8 / (simulationTime * 1000000.0); //Mbit/s
268  }
269  dataset.Add (d, throughput);
270  std::cout << throughput << " Mbit/s" <<std::endl;
271  d += step;
272  }
273  plot.AddDataset (dataset);
274  }
275 
276  plot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
277  plot.SetLegend ("Distance (Meters)", "Throughput (Mbit/s)");
278  plot.SetExtra ("set xrange [0:100]\n\
279 set yrange [0:600]\n\
280 set ytics 0,50,600\n\
281 set style line 1 dashtype 1 linewidth 5\n\
282 set style line 2 dashtype 1 linewidth 5\n\
283 set style line 3 dashtype 1 linewidth 5\n\
284 set style line 4 dashtype 1 linewidth 5\n\
285 set style line 5 dashtype 1 linewidth 5\n\
286 set style line 6 dashtype 1 linewidth 5\n\
287 set style line 7 dashtype 1 linewidth 5\n\
288 set style line 8 dashtype 1 linewidth 5\n\
289 set style line 9 dashtype 2 linewidth 5\n\
290 set style line 10 dashtype 2 linewidth 5\n\
291 set style line 11 dashtype 2 linewidth 5\n\
292 set style line 12 dashtype 2 linewidth 5\n\
293 set style line 13 dashtype 2 linewidth 5\n\
294 set style line 14 dashtype 2 linewidth 5\n\
295 set style line 15 dashtype 2 linewidth 5\n\
296 set style line 16 dashtype 2 linewidth 5\n\
297 set style line 17 dashtype 3 linewidth 5\n\
298 set style line 18 dashtype 3 linewidth 5\n\
299 set style line 19 dashtype 3 linewidth 5\n\
300 set style line 20 dashtype 3 linewidth 5\n\
301 set style line 21 dashtype 3 linewidth 5\n\
302 set style line 22 dashtype 3 linewidth 5\n\
303 set style line 23 dashtype 3 linewidth 5\n\
304 set style line 24 dashtype 3 linewidth 5\n\
305 set style line 25 dashtype 4 linewidth 5\n\
306 set style line 26 dashtype 4 linewidth 5\n\
307 set style line 27 dashtype 4 linewidth 5\n\
308 set style line 28 dashtype 4 linewidth 5\n\
309 set style line 29 dashtype 4 linewidth 5\n\
310 set style line 30 dashtype 4 linewidth 5\n\
311 set style line 31 dashtype 4 linewidth 5\n\
312 set style line 32 dashtype 4 linewidth 5\n\
313 set style increment user" );
314  plot.GenerateOutput (file);
315  file.close ();
316 
317  return 0;
318 }
319 
tuple channel
Definition: third.py:85
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:34
HT OFDM PHY for the 5 GHz band (clause 20)
Class to represent a 2D points plot.
Definition: gnuplot.h:113
holds a vector of std::pair of Ptr<Ipv4> 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:71
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:769
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
aggregate IP/TCP/UDP functionality to existing Nodes.
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
void Set(std::string name, const AttributeValue &v)
HT OFDM PHY for the 2.4 GHz band (clause 20)
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:76
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
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:100
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
void SetChannel(Ptr< YansWifiChannel > channel)
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
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:367
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:957
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:94
Create a server application which waits for input UDP packets and uses the information carried into t...
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
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:201
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:164
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.
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> 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.
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:743
tuple stack
Definition: first.py:34
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:38
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.
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.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:491
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
AttributeValue implementation for Ssid.
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
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.
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...
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
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