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/uinteger.h"
40#include "ns3/yans-wifi-channel.h"
41#include "ns3/yans-wifi-helper.h"
42
43// This is a simple example in order to show how to configure an IEEE 802.11n Wi-Fi network.
44//
45// It outputs the UDP or TCP goodput for every HT MCS value, which depends on the MCS value (0 to
46// 7), the channel width (20 or 40 MHz) and the guard interval (long or short). The PHY bitrate is
47// constant over all the simulation run. The user can also specify the distance between the access
48// point and the station: the larger the distance the smaller the goodput.
49//
50// The simulation assumes a single station in an infrastructure network:
51//
52// STA AP
53// * *
54// | |
55// n1 n2
56//
57// Packets in this simulation belong to BestEffort Access Class (AC_BE).
58
59using namespace ns3;
60
61NS_LOG_COMPONENT_DEFINE("ht-wifi-network");
62
63int
64main(int argc, char* argv[])
65{
66 bool udp = true;
67 bool useRts = false;
68 double simulationTime = 10; // seconds
69 double distance = 1.0; // meters
70 double frequency = 5.0; // whether 2.4 or 5.0 GHz
71 int mcs = -1; // -1 indicates an unset value
72 double minExpectedThroughput = 0;
73 double maxExpectedThroughput = 0;
74
75 CommandLine cmd(__FILE__);
76 cmd.AddValue("frequency",
77 "Whether working in the 2.4 or 5.0 GHz band (other values gets rejected)",
78 frequency);
79 cmd.AddValue("distance",
80 "Distance in meters between the station and the access point",
81 distance);
82 cmd.AddValue("simulationTime", "Simulation time in seconds", simulationTime);
83 cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
84 cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
85 cmd.AddValue("mcs", "if set, limit testing to a specific MCS (0-7)", mcs);
86 cmd.AddValue("minExpectedThroughput",
87 "if set, simulation fails if the lowest throughput is below this value",
88 minExpectedThroughput);
89 cmd.AddValue("maxExpectedThroughput",
90 "if set, simulation fails if the highest throughput is above this value",
91 maxExpectedThroughput);
92 cmd.Parse(argc, argv);
93
94 if (useRts)
95 {
96 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
97 }
98
99 double prevThroughput[8] = {0};
100
101 std::cout << "MCS value"
102 << "\t\t"
103 << "Channel width"
104 << "\t\t"
105 << "short GI"
106 << "\t\t"
107 << "Throughput" << '\n';
108 int minMcs = 0;
109 int maxMcs = 7;
110 if (mcs >= 0 && mcs <= 7)
111 {
112 minMcs = mcs;
113 maxMcs = mcs;
114 }
115 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
116 {
117 uint8_t index = 0;
118 double previous = 0;
119 for (int channelWidth = 20; channelWidth <= 40;)
120 {
121 for (int sgi = 0; sgi < 2; sgi++)
122 {
123 uint32_t payloadSize; // 1500 byte IP packet
124 if (udp)
125 {
126 payloadSize = 1472; // bytes
127 }
128 else
129 {
130 payloadSize = 1448; // bytes
131 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
132 }
133
134 NodeContainer wifiStaNode;
135 wifiStaNode.Create(1);
137 wifiApNode.Create(1);
138
141 phy.SetChannel(channel.Create());
142
145 std::ostringstream ossControlMode;
146
147 if (frequency == 5.0)
148 {
149 ossControlMode << "OfdmRate";
150 wifi.SetStandard(WIFI_STANDARD_80211n);
151 }
152 else if (frequency == 2.4)
153 {
154 wifi.SetStandard(WIFI_STANDARD_80211n);
155 ossControlMode << "ErpOfdmRate";
156 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
157 DoubleValue(40.046));
158 }
159 else
160 {
161 std::cout << "Wrong frequency value!" << std::endl;
162 return 0;
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 WifiPhyBand band = (frequency == 5.0 ? WIFI_PHY_BAND_5GHZ : WIFI_PHY_BAND_2_4GHZ);
181 channelValue.Set(WifiPhy::ChannelTuple{0, channelWidth, band, 0});
182
183 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
184 phy.Set("ChannelSettings", channelValue);
185
186 NetDeviceContainer staDevice;
187 staDevice = wifi.Install(phy, mac, wifiStaNode);
188
189 mac.SetType("ns3::ApWifiMac",
190 "EnableBeaconJitter",
191 BooleanValue(false),
192 "Ssid",
193 SsidValue(ssid));
194
195 NetDeviceContainer apDevice;
196 apDevice = wifi.Install(phy, mac, wifiApNode);
197
198 // mobility.
200 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
201
202 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
203 positionAlloc->Add(Vector(distance, 0.0, 0.0));
204 mobility.SetPositionAllocator(positionAlloc);
205
206 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
207
208 mobility.Install(wifiApNode);
209 mobility.Install(wifiStaNode);
210
211 /* Internet stack*/
213 stack.Install(wifiApNode);
214 stack.Install(wifiStaNode);
215
217 address.SetBase("192.168.1.0", "255.255.255.0");
218 Ipv4InterfaceContainer staNodeInterface;
219 Ipv4InterfaceContainer apNodeInterface;
220
221 staNodeInterface = address.Assign(staDevice);
222 apNodeInterface = address.Assign(apDevice);
223
224 /* Setting applications */
225 ApplicationContainer serverApp;
226 if (udp)
227 {
228 // UDP flow
229 uint16_t port = 9;
231 serverApp = server.Install(wifiStaNode.Get(0));
232 serverApp.Start(Seconds(0.0));
233 serverApp.Stop(Seconds(simulationTime + 1));
234
235 UdpClientHelper client(staNodeInterface.GetAddress(0), port);
236 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
237 client.SetAttribute("Interval", TimeValue(Time("0.00001"))); // packets/s
238 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
239 ApplicationContainer clientApp = client.Install(wifiApNode.Get(0));
240 clientApp.Start(Seconds(1.0));
241 clientApp.Stop(Seconds(simulationTime + 1));
242 }
243 else
244 {
245 // TCP flow
246 uint16_t port = 50000;
248 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
249 serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
250 serverApp.Start(Seconds(0.0));
251 serverApp.Stop(Seconds(simulationTime + 1));
252
253 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
254 onoff.SetAttribute("OnTime",
255 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
256 onoff.SetAttribute("OffTime",
257 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
258 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
259 onoff.SetAttribute("DataRate", DataRateValue(200000000)); // bit/s
261 InetSocketAddress(staNodeInterface.GetAddress(0), port));
262 onoff.SetAttribute("Remote", remoteAddress);
263 ApplicationContainer clientApp = onoff.Install(wifiApNode.Get(0));
264 clientApp.Start(Seconds(1.0));
265 clientApp.Stop(Seconds(simulationTime + 1));
266 }
267
269
270 Simulator::Stop(Seconds(simulationTime + 1));
272
273 uint64_t rxBytes = 0;
274 if (udp)
275 {
276 rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
277 }
278 else
279 {
280 rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
281 }
282 double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); // Mbit/s
283
285
286 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << sgi << "\t\t\t"
287 << throughput << " Mbit/s" << std::endl;
288
289 // test first element
290 if (mcs == 0 && channelWidth == 20 && sgi == 0)
291 {
292 if (throughput < minExpectedThroughput)
293 {
294 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
295 }
296 }
297 // test last element
298 if (mcs == 7 && channelWidth == 40 && sgi == 1)
299 {
300 if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
301 {
302 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
303 }
304 }
305 // test previous throughput is smaller (for the same mcs)
306 if (throughput > previous)
307 {
308 previous = throughput;
309 }
310 else
311 {
312 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
313 }
314 // test previous throughput is smaller (for the same channel width and GI)
315 if (throughput > prevThroughput[index])
316 {
317 prevThroughput[index] = throughput;
318 }
319 else
320 {
321 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
322 }
323 index++;
324 }
325 channelWidth *= 2;
326 }
327 }
328 return 0;
329}
a polymophic address class
Definition: address.h:100
AttributeValue implementation for Address.
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.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
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:730
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: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
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:184
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
AttributeValue implementation for Time.
Definition: nstime.h:1423
Hold objects of type std::tuple<Args...>.
Definition: tuple.h:69
void Set(const result_type &value)
Set the stored values.
Definition: tuple.h:318
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, int, uint8_t > ChannelTuple
Tuple identifying an operating channel.
Definition: wifi-phy.h:872
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:891
#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:1336
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:40
ns stack
Definition: first.py:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns cmd
Definition: second.py:33
ns wifi
Definition: third.py:88
ns ssid
Definition: third.py:86
ns mac
Definition: third.py:85
ns wifiApNode
Definition: third.py:79
ns channel
Definition: third.py:81
ns mobility
Definition: third.py:96
ns phy
Definition: third.py:82