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