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 double simulationTime = 10; // seconds
70 double distance = 1.0; // meters
71 double frequency = 5.0; // whether 2.4 or 5.0 GHz
72 int mcs = -1; // -1 indicates an unset value
73 double minExpectedThroughput = 0;
74 double maxExpectedThroughput = 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 in seconds", 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 std::cout << "Wrong frequency value!" << std::endl;
163 return 0;
164 }
165
166 auto nonHtRefRateMbps = HtPhy::GetNonHtReferenceRate(mcs) / 1e6;
167 ossControlMode << nonHtRefRateMbps << "Mbps";
168
169 std::ostringstream ossDataMode;
170 ossDataMode << "HtMcs" << mcs;
171 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
172 "DataMode",
173 StringValue(ossDataMode.str()),
174 "ControlMode",
175 StringValue(ossControlMode.str()));
176 // Set guard interval
177 wifi.ConfigHtOptions("ShortGuardIntervalSupported", BooleanValue(sgi));
178
179 Ssid ssid = Ssid("ns3-80211n");
181 channelValue;
182 WifiPhyBand band = (frequency == 5.0 ? WIFI_PHY_BAND_5GHZ : WIFI_PHY_BAND_2_4GHZ);
183 channelValue.Set(WifiPhy::ChannelTuple{0, channelWidth, band, 0});
184
185 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
186 phy.Set("ChannelSettings", channelValue);
187
188 NetDeviceContainer staDevice;
189 staDevice = wifi.Install(phy, mac, wifiStaNode);
190
191 mac.SetType("ns3::ApWifiMac",
192 "EnableBeaconJitter",
193 BooleanValue(false),
194 "Ssid",
195 SsidValue(ssid));
196
197 NetDeviceContainer apDevice;
198 apDevice = wifi.Install(phy, mac, wifiApNode);
199
200 // mobility.
202 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
203
204 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
205 positionAlloc->Add(Vector(distance, 0.0, 0.0));
206 mobility.SetPositionAllocator(positionAlloc);
207
208 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
209
210 mobility.Install(wifiApNode);
211 mobility.Install(wifiStaNode);
212
213 /* Internet stack*/
215 stack.Install(wifiApNode);
216 stack.Install(wifiStaNode);
217
219 address.SetBase("192.168.1.0", "255.255.255.0");
220 Ipv4InterfaceContainer staNodeInterface;
221 Ipv4InterfaceContainer apNodeInterface;
222
223 staNodeInterface = address.Assign(staDevice);
224 apNodeInterface = address.Assign(apDevice);
225
226 /* Setting applications */
227 const auto maxLoad = HtPhy::GetDataRate(mcs, channelWidth, sgi ? 400 : 800, 1);
228 ApplicationContainer serverApp;
229 if (udp)
230 {
231 // UDP flow
232 uint16_t port = 9;
234 serverApp = server.Install(wifiStaNode.Get(0));
235 serverApp.Start(Seconds(0.0));
236 serverApp.Stop(Seconds(simulationTime + 1));
237 const auto packetInterval = payloadSize * 8.0 / maxLoad;
238
239 UdpClientHelper client(staNodeInterface.GetAddress(0), port);
240 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
241 client.SetAttribute("Interval", TimeValue(Seconds(packetInterval)));
242 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
243 ApplicationContainer clientApp = client.Install(wifiApNode.Get(0));
244 clientApp.Start(Seconds(1.0));
245 clientApp.Stop(Seconds(simulationTime + 1));
246 }
247 else
248 {
249 // TCP flow
250 uint16_t port = 50000;
252 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
253 serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
254 serverApp.Start(Seconds(0.0));
255 serverApp.Stop(Seconds(simulationTime + 1));
256
257 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
258 onoff.SetAttribute("OnTime",
259 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
260 onoff.SetAttribute("OffTime",
261 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
262 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
263 onoff.SetAttribute("DataRate", DataRateValue(maxLoad));
265 InetSocketAddress(staNodeInterface.GetAddress(0), port));
266 onoff.SetAttribute("Remote", remoteAddress);
267 ApplicationContainer clientApp = onoff.Install(wifiApNode.Get(0));
268 clientApp.Start(Seconds(1.0));
269 clientApp.Stop(Seconds(simulationTime + 1));
270 }
271
273
274 Simulator::Stop(Seconds(simulationTime + 1));
276
277 uint64_t rxBytes = 0;
278 if (udp)
279 {
280 rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
281 }
282 else
283 {
284 rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
285 }
286 double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); // Mbit/s
287
289
290 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << std::boolalpha
291 << sgi << "\t\t\t" << throughput << " Mbit/s" << std::endl;
292
293 // test first element
294 if (mcs == 0 && channelWidth == 20 && !sgi)
295 {
296 if (throughput < minExpectedThroughput)
297 {
298 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
299 }
300 }
301 // test last element
302 if (mcs == 7 && channelWidth == 40 && sgi)
303 {
304 if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
305 {
306 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
307 }
308 }
309 // test previous throughput is smaller (for the same mcs)
310 if (throughput > previous)
311 {
312 previous = throughput;
313 }
314 else
315 {
316 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
317 }
318 // test previous throughput is smaller (for the same channel width and GI)
319 if (throughput > prevThroughput[index])
320 {
321 prevThroughput[index] = throughput;
322 }
323 else
324 {
325 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
326 }
327 index++;
328 }
329 channelWidth *= 2;
330 }
331 }
332 return 0;
333}
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Address.
Definition: address.h:286
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:232
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
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
AttributeValue implementation for Ssid.
Definition: ssid.h:96
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1406
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:903
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:105
ns phy
Definition: third.py:89
std::ofstream throughput