A Discrete-Event Network Simulator
API
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/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 
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 guard interval
141  phy.Set ("ShortGuardEnabled", BooleanValue (shortGuardInterval));
142  // Set MIMO capabilities
143  phy.Set ("Antennas", UintegerValue (nStreams));
144  phy.Set ("MaxSupportedTxSpatialStreams", UintegerValue (nStreams));
145  phy.Set ("MaxSupportedRxSpatialStreams", UintegerValue (nStreams));
146 
149  if (frequency == 5.0)
150  {
151  wifi.SetStandard (WIFI_PHY_STANDARD_80211n_5GHZ);
152  }
153  else if (frequency == 2.4)
154  {
156  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046));
157  }
158  else
159  {
160  std::cout << "Wrong frequency value!" << std::endl;
161  return 0;
162  }
163 
164  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", StringValue (modes[i]),
165  "ControlMode", StringValue (modes[i]));
166 
167  Ssid ssid = Ssid ("ns3-80211n");
168 
169  mac.SetType ("ns3::StaWifiMac",
170  "Ssid", SsidValue (ssid));
171 
172  NetDeviceContainer staDevice;
173  staDevice = wifi.Install (phy, mac, wifiStaNode);
174 
175  mac.SetType ("ns3::ApWifiMac",
176  "Ssid", SsidValue (ssid));
177 
178  NetDeviceContainer apDevice;
179  apDevice = wifi.Install (phy, mac, wifiApNode);
180 
181  // Set channel width
182  if (channelBonding)
183  {
184  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (40));
185  }
186 
187  // mobility.
189  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
190 
191  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
192  positionAlloc->Add (Vector (d, 0.0, 0.0));
193  mobility.SetPositionAllocator (positionAlloc);
194 
195  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
196 
197  mobility.Install (wifiApNode);
198  mobility.Install (wifiStaNode);
199 
200  /* Internet stack*/
202  stack.Install (wifiApNode);
203  stack.Install (wifiStaNode);
204 
206  address.SetBase ("192.168.1.0", "255.255.255.0");
207  Ipv4InterfaceContainer staNodeInterface;
208  Ipv4InterfaceContainer apNodeInterface;
209 
210  staNodeInterface = address.Assign (staDevice);
211  apNodeInterface = address.Assign (apDevice);
212 
213  /* Setting applications */
214  ApplicationContainer serverApp;
215  if (udp)
216  {
217  //UDP flow
218  uint16_t port = 9;
219  UdpServerHelper server (port);
220  serverApp = server.Install (wifiStaNode.Get (0));
221  serverApp.Start (Seconds (0.0));
222  serverApp.Stop (Seconds (simulationTime + 1));
223 
224  UdpClientHelper client (staNodeInterface.GetAddress (0), port);
225  client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
226  client.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
227  client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
228  ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
229  clientApp.Start (Seconds (1.0));
230  clientApp.Stop (Seconds (simulationTime + 1));
231  }
232  else
233  {
234  //TCP flow
235  uint16_t port = 50000;
236  Address localAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
237  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", localAddress);
238  serverApp = packetSinkHelper.Install (wifiStaNode.Get (0));
239  serverApp.Start (Seconds (0.0));
240  serverApp.Stop (Seconds (simulationTime + 1));
241 
242  OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
243  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
244  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
245  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
246  onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
247  AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
248  onoff.SetAttribute ("Remote", remoteAddress);
249  ApplicationContainer clientApp = onoff.Install (wifiApNode.Get (0));
250  clientApp.Start (Seconds (1.0));
251  clientApp.Stop (Seconds (simulationTime + 1));
252  }
253 
255 
256  Simulator::Stop (Seconds (simulationTime + 1));
257  Simulator::Run ();
258 
259  double throughput = 0;
260  if (udp)
261  {
262  //UDP
263  uint64_t totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
264  throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); //Mbit/s
265  }
266  else
267  {
268  //TCP
269  uint64_t totalPacketsThrough = DynamicCast<PacketSink> (serverApp.Get (0))->GetTotalRx ();
270  throughput = totalPacketsThrough * 8 / (simulationTime * 1000000.0); //Mbit/s
271  }
272  dataset.Add (d, throughput);
273  std::cout << throughput << " Mbit/s" << std::endl;
274  d += step;
276  }
277  plot.AddDataset (dataset);
278  }
279 
280  plot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
281  plot.SetLegend ("Distance (Meters)", "Throughput (Mbit/s)");
282  plot.SetExtra ("set xrange [0:100]\n\
283 set yrange [0:600]\n\
284 set ytics 0,50,600\n\
285 set style line 1 dashtype 1 linewidth 5\n\
286 set style line 2 dashtype 1 linewidth 5\n\
287 set style line 3 dashtype 1 linewidth 5\n\
288 set style line 4 dashtype 1 linewidth 5\n\
289 set style line 5 dashtype 1 linewidth 5\n\
290 set style line 6 dashtype 1 linewidth 5\n\
291 set style line 7 dashtype 1 linewidth 5\n\
292 set style line 8 dashtype 1 linewidth 5\n\
293 set style line 9 dashtype 2 linewidth 5\n\
294 set style line 10 dashtype 2 linewidth 5\n\
295 set style line 11 dashtype 2 linewidth 5\n\
296 set style line 12 dashtype 2 linewidth 5\n\
297 set style line 13 dashtype 2 linewidth 5\n\
298 set style line 14 dashtype 2 linewidth 5\n\
299 set style line 15 dashtype 2 linewidth 5\n\
300 set style line 16 dashtype 2 linewidth 5\n\
301 set style line 17 dashtype 3 linewidth 5\n\
302 set style line 18 dashtype 3 linewidth 5\n\
303 set style line 19 dashtype 3 linewidth 5\n\
304 set style line 20 dashtype 3 linewidth 5\n\
305 set style line 21 dashtype 3 linewidth 5\n\
306 set style line 22 dashtype 3 linewidth 5\n\
307 set style line 23 dashtype 3 linewidth 5\n\
308 set style line 24 dashtype 3 linewidth 5\n\
309 set style line 25 dashtype 4 linewidth 5\n\
310 set style line 26 dashtype 4 linewidth 5\n\
311 set style line 27 dashtype 4 linewidth 5\n\
312 set style line 28 dashtype 4 linewidth 5\n\
313 set style line 29 dashtype 4 linewidth 5\n\
314 set style line 30 dashtype 4 linewidth 5\n\
315 set style line 31 dashtype 4 linewidth 5\n\
316 set style line 32 dashtype 4 linewidth 5\n\
317 set style increment user" );
318  plot.GenerateOutput (file);
319  file.close ();
320 
321  return 0;
322 }
323 
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
HT PHY for the 5 GHz band (clause 20)
Class to represent a 2D points plot.
Definition: gnuplot.h:117
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:777
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
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...
cmd
Definition: second.py:35
HT 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:230
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:34
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:85
mobility
Definition: third.py:101
phy
Definition: third.py:86
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:371
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:1076
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
ssid
Definition: third.py:93
holds a vector of ns3::NetDevice pointers
mac
Definition: third.py:92
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
wifiApNode
Definition: third.py:83
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:213
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:190
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:37
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
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:35
wifi
Definition: third.py:89
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:242
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
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.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
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
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.