A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-ht-network.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 MIRKO BANCHI
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Authors: Mirko Banchi <mk.banchi@gmail.com>
18 * Sebastien Deronne <sebastien.deronne@gmail.com>
19 */
20
21#include "ns3/boolean.h"
22#include "ns3/command-line.h"
23#include "ns3/config.h"
24#include "ns3/double.h"
25#include "ns3/enum.h"
26#include "ns3/ht-phy.h"
27#include "ns3/internet-stack-helper.h"
28#include "ns3/ipv4-address-helper.h"
29#include "ns3/ipv4-global-routing-helper.h"
30#include "ns3/log.h"
31#include "ns3/mobility-helper.h"
32#include "ns3/on-off-helper.h"
33#include "ns3/packet-sink-helper.h"
34#include "ns3/packet-sink.h"
35#include "ns3/ssid.h"
36#include "ns3/string.h"
37#include "ns3/tuple.h"
38#include "ns3/udp-client-server-helper.h"
39#include "ns3/udp-server.h"
40#include "ns3/uinteger.h"
41#include "ns3/yans-wifi-channel.h"
42#include "ns3/yans-wifi-helper.h"
43
44// This is a simple example in order to show how to configure an IEEE 802.11n Wi-Fi network.
45//
46// It outputs the UDP or TCP goodput for every HT MCS value, which depends on the MCS value (0 to
47// 7), the channel width (20 or 40 MHz) and the guard interval (long or short). The PHY bitrate is
48// constant over all the simulation run. The user can also specify the distance between the access
49// point and the station: the larger the distance the smaller the goodput.
50//
51// The simulation assumes a single station in an infrastructure network:
52//
53// STA AP
54// * *
55// | |
56// n1 n2
57//
58// Packets in this simulation belong to BestEffort Access Class (AC_BE).
59
60using namespace ns3;
61
62NS_LOG_COMPONENT_DEFINE("ht-wifi-network");
63
64int
65main(int argc, char* argv[])
66{
67 bool udp{true};
68 bool useRts{false};
69 Time simulationTime{"10s"};
70 double distance{1.0}; // meters
71 double frequency{5}; // whether 2.4 or 5 GHz
72 int mcs{-1}; // -1 indicates an unset value
73 double minExpectedThroughput{0.0};
74 double maxExpectedThroughput{0.0};
75
76 CommandLine cmd(__FILE__);
77 cmd.AddValue("frequency",
78 "Whether working in the 2.4 or 5.0 GHz band (other values gets rejected)",
79 frequency);
80 cmd.AddValue("distance",
81 "Distance in meters between the station and the access point",
82 distance);
83 cmd.AddValue("simulationTime", "Simulation time", simulationTime);
84 cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
85 cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
86 cmd.AddValue("mcs", "if set, limit testing to a specific MCS (0-7)", mcs);
87 cmd.AddValue("minExpectedThroughput",
88 "if set, simulation fails if the lowest throughput is below this value",
89 minExpectedThroughput);
90 cmd.AddValue("maxExpectedThroughput",
91 "if set, simulation fails if the highest throughput is above this value",
92 maxExpectedThroughput);
93 cmd.Parse(argc, argv);
94
95 if (useRts)
96 {
97 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
98 }
99
100 double prevThroughput[8] = {0};
101
102 std::cout << "MCS value"
103 << "\t\t"
104 << "Channel width"
105 << "\t\t"
106 << "short GI"
107 << "\t\t"
108 << "Throughput" << '\n';
109 int minMcs = 0;
110 int maxMcs = 7;
111 if (mcs >= 0 && mcs <= 7)
112 {
113 minMcs = mcs;
114 maxMcs = mcs;
115 }
116 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
117 {
118 uint8_t index = 0;
119 double previous = 0;
120 for (int channelWidth = 20; channelWidth <= 40;)
121 {
122 for (auto sgi : {false, true})
123 {
124 uint32_t payloadSize; // 1500 byte IP packet
125 if (udp)
126 {
127 payloadSize = 1472; // bytes
128 }
129 else
130 {
131 payloadSize = 1448; // bytes
132 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
133 }
134
135 NodeContainer wifiStaNode;
136 wifiStaNode.Create(1);
138 wifiApNode.Create(1);
139
142 phy.SetChannel(channel.Create());
143
146 std::ostringstream ossControlMode;
147
148 if (frequency == 5.0)
149 {
150 ossControlMode << "OfdmRate";
151 wifi.SetStandard(WIFI_STANDARD_80211n);
152 }
153 else if (frequency == 2.4)
154 {
155 wifi.SetStandard(WIFI_STANDARD_80211n);
156 ossControlMode << "ErpOfdmRate";
157 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
158 DoubleValue(40.046));
159 }
160 else
161 {
162 NS_FATAL_ERROR("Wrong frequency value!");
163 }
164
165 auto nonHtRefRateMbps = HtPhy::GetNonHtReferenceRate(mcs) / 1e6;
166 ossControlMode << nonHtRefRateMbps << "Mbps";
167
168 std::ostringstream ossDataMode;
169 ossDataMode << "HtMcs" << mcs;
170 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
171 "DataMode",
172 StringValue(ossDataMode.str()),
173 "ControlMode",
174 StringValue(ossControlMode.str()));
175 // Set guard interval
176 wifi.ConfigHtOptions("ShortGuardIntervalSupported", BooleanValue(sgi));
177
178 Ssid ssid = Ssid("ns3-80211n");
180 channelValue;
181 WifiPhyBand band = (frequency == 5.0 ? WIFI_PHY_BAND_5GHZ : WIFI_PHY_BAND_2_4GHZ);
182 channelValue.Set(WifiPhy::ChannelTuple{0, channelWidth, band, 0});
183
184 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
185 phy.Set("ChannelSettings", channelValue);
186
187 NetDeviceContainer staDevice;
188 staDevice = wifi.Install(phy, mac, wifiStaNode);
189
190 mac.SetType("ns3::ApWifiMac",
191 "EnableBeaconJitter",
192 BooleanValue(false),
193 "Ssid",
194 SsidValue(ssid));
195
196 NetDeviceContainer apDevice;
197 apDevice = wifi.Install(phy, mac, wifiApNode);
198
199 int64_t streamNumber = 150;
200 streamNumber += wifi.AssignStreams(apDevice, streamNumber);
201 streamNumber += wifi.AssignStreams(staDevice, streamNumber);
202
203 // mobility.
205 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
206
207 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
208 positionAlloc->Add(Vector(distance, 0.0, 0.0));
209 mobility.SetPositionAllocator(positionAlloc);
210
211 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
212
213 mobility.Install(wifiApNode);
214 mobility.Install(wifiStaNode);
215
216 /* Internet stack*/
218 stack.Install(wifiApNode);
219 stack.Install(wifiStaNode);
220 streamNumber += stack.AssignStreams(wifiApNode, streamNumber);
221 streamNumber += stack.AssignStreams(wifiStaNode, streamNumber);
222
224 address.SetBase("192.168.1.0", "255.255.255.0");
225 Ipv4InterfaceContainer staNodeInterface;
226 Ipv4InterfaceContainer apNodeInterface;
227
228 staNodeInterface = address.Assign(staDevice);
229 apNodeInterface = address.Assign(apDevice);
230
231 /* Setting applications */
232 const auto maxLoad = HtPhy::GetDataRate(mcs, channelWidth, sgi ? 400 : 800, 1);
233 ApplicationContainer serverApp;
234 if (udp)
235 {
236 // UDP flow
237 uint16_t port = 9;
239 serverApp = server.Install(wifiStaNode.Get(0));
240 streamNumber += server.AssignStreams(wifiStaNode.Get(0), streamNumber);
241
242 serverApp.Start(Seconds(0.0));
243 serverApp.Stop(simulationTime + Seconds(1.0));
244 const auto packetInterval = payloadSize * 8.0 / maxLoad;
245
246 UdpClientHelper client(staNodeInterface.GetAddress(0), port);
247 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
248 client.SetAttribute("Interval", TimeValue(Seconds(packetInterval)));
249 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
250 ApplicationContainer clientApp = client.Install(wifiApNode.Get(0));
251 streamNumber += client.AssignStreams(wifiApNode.Get(0), streamNumber);
252
253 clientApp.Start(Seconds(1.0));
254 clientApp.Stop(simulationTime + Seconds(1.0));
255 }
256 else
257 {
258 // TCP flow
259 uint16_t port = 50000;
261 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
262 serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
263 streamNumber +=
264 packetSinkHelper.AssignStreams(wifiStaNode.Get(0), streamNumber);
265
266 serverApp.Start(Seconds(0.0));
267 serverApp.Stop(simulationTime + Seconds(1.0));
268
269 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
270 onoff.SetAttribute("OnTime",
271 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
272 onoff.SetAttribute("OffTime",
273 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
274 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
275 onoff.SetAttribute("DataRate", DataRateValue(maxLoad));
277 InetSocketAddress(staNodeInterface.GetAddress(0), port));
278 onoff.SetAttribute("Remote", remoteAddress);
279 ApplicationContainer clientApp = onoff.Install(wifiApNode.Get(0));
280 streamNumber += onoff.AssignStreams(wifiApNode.Get(0), streamNumber);
281
282 clientApp.Start(Seconds(1.0));
283 clientApp.Stop(simulationTime + Seconds(1.0));
284 }
285
287
288 Simulator::Stop(simulationTime + Seconds(1.0));
290
291 auto rxBytes = 0.0;
292 if (udp)
293 {
294 rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
295 }
296 else
297 {
298 rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
299 }
300 auto throughput = (rxBytes * 8) / simulationTime.GetMicroSeconds(); // Mbit/s
301
303
304 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << std::boolalpha
305 << sgi << "\t\t\t" << throughput << " Mbit/s" << std::endl;
306
307 // test first element
308 if (mcs == minMcs && channelWidth == 20 && !sgi)
309 {
310 if (throughput < minExpectedThroughput)
311 {
312 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
313 }
314 }
315 // test last element
316 if (mcs == maxMcs && channelWidth == 40 && sgi)
317 {
318 if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
319 {
320 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
321 }
322 }
323 // test previous throughput is smaller (for the same mcs)
324 if (throughput > previous)
325 {
326 previous = throughput;
327 }
328 else
329 {
330 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
331 }
332 // test previous throughput is smaller (for the same channel width and GI)
333 if (throughput > prevThroughput[index])
334 {
335 prevThroughput[index] = throughput;
336 }
337 else
338 {
339 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
340 }
341 index++;
342 }
343 channelWidth *= 2;
344 }
345 }
346 return 0;
347}
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
static uint64_t GetDataRate(uint8_t mcsValue, uint16_t channelWidth, uint16_t guardInterval, uint8_t nss)
Return the data rate corresponding to the supplied HT MCS index, channel width, guard interval,...
Definition: ht-phy.cc:696
static uint64_t GetNonHtReferenceRate(uint8_t mcsValue)
Calculate the rate in bps of the non-HT Reference Rate corresponding to the supplied HT MCS index.
Definition: ht-phy.cc:733
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.
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
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:37
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:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Tuple.
Definition: tuple.h:78
void Set(const result_type &value)
Set the stored values.
Definition: tuple.h:340
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
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
std::tuple< uint8_t, uint16_t, WifiPhyBand, uint8_t > ChannelTuple
Tuple identifying an operating channel.
Definition: wifi-phy.h:926
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Make it easy to create and manage PHY objects for the YANS model.
uint16_t port
Definition: dsdv-manet.cc:44
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
WifiPhyBand
Identifies the PHY band.
Definition: wifi-phy-band.h:33
@ WIFI_STANDARD_80211n
@ WIFI_PHY_BAND_2_4GHZ
The 2.4 GHz band.
Definition: wifi-phy-band.h:35
@ WIFI_PHY_BAND_5GHZ
The 5 GHz band.
Definition: wifi-phy-band.h:37
ns address
Definition: first.py:47
ns stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:40
ns wifi
Definition: third.py:95
ns ssid
Definition: third.py:93
ns mac
Definition: third.py:92
ns wifiApNode
Definition: third.py:86
ns channel
Definition: third.py:88
ns mobility
Definition: third.py:103
ns phy
Definition: third.py:89
std::ofstream throughput