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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Authors: Mirko Banchi <mk.banchi@gmail.com>
7 * Sebastien Deronne <sebastien.deronne@gmail.com>
8 */
9
10#include "ns3/attribute-container.h"
11#include "ns3/boolean.h"
12#include "ns3/command-line.h"
13#include "ns3/config.h"
14#include "ns3/double.h"
15#include "ns3/enum.h"
16#include "ns3/ht-phy.h"
17#include "ns3/internet-stack-helper.h"
18#include "ns3/ipv4-address-helper.h"
19#include "ns3/ipv4-global-routing-helper.h"
20#include "ns3/log.h"
21#include "ns3/mobility-helper.h"
22#include "ns3/on-off-helper.h"
23#include "ns3/packet-sink-helper.h"
24#include "ns3/packet-sink.h"
25#include "ns3/ssid.h"
26#include "ns3/string.h"
27#include "ns3/tuple.h"
28#include "ns3/udp-client-server-helper.h"
29#include "ns3/udp-server.h"
30#include "ns3/uinteger.h"
31#include "ns3/yans-wifi-channel.h"
32#include "ns3/yans-wifi-helper.h"
33
34// This is a simple example in order to show how to configure an IEEE 802.11n Wi-Fi network.
35//
36// It outputs the UDP or TCP goodput for every HT MCS value, which depends on the MCS value (0 to
37// 7), the channel width (20 or 40 MHz) and the guard interval (long or short). The PHY bitrate is
38// constant over all the simulation run. The user can also specify the distance between the access
39// point and the station: the larger the distance the smaller the goodput.
40//
41// The simulation assumes a single station in an infrastructure network:
42//
43// STA AP
44// * *
45// | |
46// n1 n2
47//
48// Packets in this simulation belong to BestEffort Access Class (AC_BE).
49
50using namespace ns3;
51
52NS_LOG_COMPONENT_DEFINE("ht-wifi-network");
53
54int
55main(int argc, char* argv[])
56{
57 bool udp{true};
58 bool useRts{false};
59 Time simulationTime{"10s"};
60 meter_u distance{1.0};
61 double frequency{5}; // whether 2.4 or 5 GHz
62 int mcs{-1}; // -1 indicates an unset value
63 double minExpectedThroughput{0.0};
64 double maxExpectedThroughput{0.0};
65
66 CommandLine cmd(__FILE__);
67 cmd.AddValue("frequency",
68 "Whether working in the 2.4 or 5.0 GHz band (other values gets rejected)",
69 frequency);
70 cmd.AddValue("distance",
71 "Distance in meters between the station and the access point",
72 distance);
73 cmd.AddValue("simulationTime", "Simulation time", simulationTime);
74 cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
75 cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
76 cmd.AddValue("mcs", "if set, limit testing to a specific MCS (0-7)", mcs);
77 cmd.AddValue("minExpectedThroughput",
78 "if set, simulation fails if the lowest throughput is below this value",
79 minExpectedThroughput);
80 cmd.AddValue("maxExpectedThroughput",
81 "if set, simulation fails if the highest throughput is above this value",
82 maxExpectedThroughput);
83 cmd.Parse(argc, argv);
84
85 if (useRts)
86 {
87 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
88 }
89
90 double prevThroughput[8] = {0};
91
92 std::cout << "MCS value"
93 << "\t\t"
94 << "Channel width"
95 << "\t\t"
96 << "short GI"
97 << "\t\t"
98 << "Throughput" << '\n';
99 int minMcs = 0;
100 int maxMcs = 7;
101 if (mcs >= 0 && mcs <= 7)
102 {
103 minMcs = mcs;
104 maxMcs = mcs;
105 }
106 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
107 {
108 uint8_t index = 0;
109 double previous = 0;
110 for (int channelWidth = 20; channelWidth <= 40;)
111 {
112 for (auto sgi : {false, true})
113 {
114 uint32_t payloadSize; // 1500 byte IP packet
115 if (udp)
116 {
117 payloadSize = 1472; // bytes
118 }
119 else
120 {
121 payloadSize = 1448; // bytes
122 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
123 }
124
125 NodeContainer wifiStaNode;
126 wifiStaNode.Create(1);
128 wifiApNode.Create(1);
129
132 phy.SetChannel(channel.Create());
133
136 std::ostringstream ossControlMode;
137
138 if (frequency == 5.0)
139 {
140 ossControlMode << "OfdmRate";
141 wifi.SetStandard(WIFI_STANDARD_80211n);
142 }
143 else if (frequency == 2.4)
144 {
145 wifi.SetStandard(WIFI_STANDARD_80211n);
146 ossControlMode << "ErpOfdmRate";
147 Config::SetDefault("ns3::LogDistancePropagationLossModel::ReferenceLoss",
148 DoubleValue(40.046));
149 }
150 else
151 {
152 NS_FATAL_ERROR("Wrong frequency value!");
153 }
154
155 auto nonHtRefRateMbps = HtPhy::GetNonHtReferenceRate(mcs) / 1e6;
156 ossControlMode << nonHtRefRateMbps << "Mbps";
157
158 std::ostringstream ossDataMode;
159 ossDataMode << "HtMcs" << mcs;
160 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
161 "DataMode",
162 StringValue(ossDataMode.str()),
163 "ControlMode",
164 StringValue(ossControlMode.str()));
165 // Set guard interval
166 wifi.ConfigHtOptions("ShortGuardIntervalSupported", BooleanValue(sgi));
167
168 Ssid ssid = Ssid("ns3-80211n");
171 ';'>
172 channelValue;
173 WifiPhyBand band = (frequency == 5.0 ? WIFI_PHY_BAND_5GHZ : WIFI_PHY_BAND_2_4GHZ);
174 channelValue.Set(WifiPhy::ChannelSegments{{0, channelWidth, band, 0}});
175
176 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
177 phy.Set("ChannelSettings", channelValue);
178
179 NetDeviceContainer staDevice;
180 staDevice = wifi.Install(phy, mac, wifiStaNode);
181
182 mac.SetType("ns3::ApWifiMac",
183 "EnableBeaconJitter",
184 BooleanValue(false),
185 "Ssid",
186 SsidValue(ssid));
187
188 NetDeviceContainer apDevice;
189 apDevice = wifi.Install(phy, mac, wifiApNode);
190
191 int64_t streamNumber = 150;
192 streamNumber += WifiHelper::AssignStreams(apDevice, streamNumber);
193 streamNumber += WifiHelper::AssignStreams(staDevice, streamNumber);
194
195 // mobility.
198
199 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
200 positionAlloc->Add(Vector(distance, 0.0, 0.0));
201 mobility.SetPositionAllocator(positionAlloc);
202
203 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
204
205 mobility.Install(wifiApNode);
206 mobility.Install(wifiStaNode);
207
208 /* Internet stack*/
210 stack.Install(wifiApNode);
211 stack.Install(wifiStaNode);
212 streamNumber += stack.AssignStreams(wifiApNode, streamNumber);
213 streamNumber += stack.AssignStreams(wifiStaNode, streamNumber);
214
216 address.SetBase("192.168.1.0", "255.255.255.0");
217 Ipv4InterfaceContainer staNodeInterface;
218 Ipv4InterfaceContainer apNodeInterface;
219
220 staNodeInterface = address.Assign(staDevice);
221 apNodeInterface = address.Assign(apDevice);
222
223 /* Setting applications */
224 const auto maxLoad =
225 HtPhy::GetDataRate(mcs, channelWidth, NanoSeconds(sgi ? 400 : 800), 1);
226 ApplicationContainer serverApp;
227 if (udp)
228 {
229 // UDP flow
230 uint16_t port = 9;
232 serverApp = server.Install(wifiStaNode.Get(0));
233 streamNumber += server.AssignStreams(wifiStaNode.Get(0), streamNumber);
234
235 serverApp.Start(Seconds(0.0));
236 serverApp.Stop(simulationTime + Seconds(1.0));
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 streamNumber += client.AssignStreams(wifiApNode.Get(0), streamNumber);
245
246 clientApp.Start(Seconds(1.0));
247 clientApp.Stop(simulationTime + Seconds(1.0));
248 }
249 else
250 {
251 // TCP flow
252 uint16_t port = 50000;
254 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
255 serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
256 streamNumber +=
257 packetSinkHelper.AssignStreams(wifiStaNode.Get(0), streamNumber);
258
259 serverApp.Start(Seconds(0.0));
260 serverApp.Stop(simulationTime + Seconds(1.0));
261
262 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
263 onoff.SetAttribute("OnTime",
264 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
265 onoff.SetAttribute("OffTime",
266 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
267 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
268 onoff.SetAttribute("DataRate", DataRateValue(maxLoad));
270 InetSocketAddress(staNodeInterface.GetAddress(0), port));
271 onoff.SetAttribute("Remote", remoteAddress);
272 ApplicationContainer clientApp = onoff.Install(wifiApNode.Get(0));
273 streamNumber += onoff.AssignStreams(wifiApNode.Get(0), streamNumber);
274
275 clientApp.Start(Seconds(1.0));
276 clientApp.Stop(simulationTime + Seconds(1.0));
277 }
278
280
281 Simulator::Stop(simulationTime + Seconds(1.0));
283
284 auto rxBytes = 0.0;
285 if (udp)
286 {
287 rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
288 }
289 else
290 {
291 rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
292 }
293 auto throughput = (rxBytes * 8) / simulationTime.GetMicroSeconds(); // Mbit/s
294
296
297 std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << std::boolalpha
298 << sgi << "\t\t\t" << throughput << " Mbit/s" << std::endl;
299
300 // test first element
301 if (mcs == minMcs && channelWidth == 20 && !sgi)
302 {
303 if (throughput < minExpectedThroughput)
304 {
305 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
306 }
307 }
308 // test last element
309 if (mcs == maxMcs && channelWidth == 40 && sgi)
310 {
311 if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
312 {
313 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
314 }
315 }
316 // test previous throughput is smaller (for the same mcs)
317 if (throughput > previous)
318 {
319 previous = throughput;
320 }
321 else
322 {
323 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
324 }
325 // test previous throughput is smaller (for the same channel width and GI)
326 if (throughput > prevThroughput[index])
327 {
328 prevThroughput[index] = throughput;
329 }
330 else
331 {
332 NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
333 }
334 index++;
335 }
336 channelWidth *= 2;
337 }
338 }
339 return 0;
340}
a polymophic address class
Definition address.h:90
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.
A container for one type of attribute.
void Set(const T &c)
Copy items from container c.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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:725
static uint64_t GetDataRate(uint8_t mcsValue, MHz_u channelWidth, Time guardInterval, uint8_t nss)
Return the data rate corresponding to the supplied HT MCS index, channel width, guard interval,...
Definition ht-phy.cc:687
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.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
AttributeValue implementation for Tuple.
Definition tuple.h:67
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:34
helps to create WifiNetDevice objects
static int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by the PHY and MAC aspects ...
create MAC layers for a ns3::WifiNetDevice.
std::vector< ChannelTuple > ChannelSegments
segments identifying an operating channel
Definition wifi-phy.h:925
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:33
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time NanoSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
WifiPhyBand
Identifies the PHY band.
@ WIFI_STANDARD_80211n
@ WIFI_PHY_BAND_2_4GHZ
The 2.4 GHz band.
@ WIFI_PHY_BAND_5GHZ
The 5 GHz band.
address
Definition first.py:36
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
ssid
Definition third.py:82
channel
Definition third.py:77
mac
Definition third.py:81
wifi
Definition third.py:84
wifiApNode
Definition third.py:75
mobility
Definition third.py:92
phy
Definition third.py:78
std::ofstream throughput