A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-vht-network.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 SEBASTIEN DERONNE
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Sebastien Deronne <sebastien.deronne@gmail.com>
7 */
8
9#include "ns3/boolean.h"
10#include "ns3/command-line.h"
11#include "ns3/config.h"
12#include "ns3/double.h"
13#include "ns3/internet-stack-helper.h"
14#include "ns3/ipv4-address-helper.h"
15#include "ns3/ipv4-global-routing-helper.h"
16#include "ns3/log.h"
17#include "ns3/mobility-helper.h"
18#include "ns3/multi-model-spectrum-channel.h"
19#include "ns3/on-off-helper.h"
20#include "ns3/packet-sink-helper.h"
21#include "ns3/packet-sink.h"
22#include "ns3/spectrum-wifi-helper.h"
23#include "ns3/ssid.h"
24#include "ns3/string.h"
25#include "ns3/udp-client-server-helper.h"
26#include "ns3/udp-server.h"
27#include "ns3/uinteger.h"
28#include "ns3/vht-phy.h"
29#include "ns3/yans-wifi-channel.h"
30#include "ns3/yans-wifi-helper.h"
31
32// This is a simple example in order to show how to configure an IEEE 802.11ac Wi-Fi network.
33//
34// It outputs the UDP or TCP goodput for every VHT MCS value, which depends on the MCS value (0 to
35// 9, where 9 is forbidden when the channel width is 20 MHz), the channel width (20, 40, 80 or 160
36// MHz) and the guard interval (long or short). The PHY bitrate is constant over all the simulation
37// run. The user can also specify the distance between the access point and the station: the larger
38// the distance the smaller the goodput.
39//
40// The simulation assumes a single station in an infrastructure network:
41//
42// STA AP
43// * *
44// | |
45// n1 n2
46//
47// Packets in this simulation belong to BestEffort Access Class (AC_BE).
48
49using namespace ns3;
50
51NS_LOG_COMPONENT_DEFINE("vht-wifi-network");
52
53int
54main(int argc, char* argv[])
55{
56 bool udp{true};
57 bool useRts{false};
58 bool use80Plus80{false};
59 Time simulationTime{"10s"};
60 meter_u distance{1.0};
61 int mcs{-1}; // -1 indicates an unset value
62 std::string phyModel{"Yans"};
63 double minExpectedThroughput{0.0};
64 double maxExpectedThroughput{0.0};
65
66 CommandLine cmd(__FILE__);
67 cmd.AddValue("distance",
68 "Distance in meters between the station and the access point",
69 distance);
70 cmd.AddValue("simulationTime", "Simulation time", simulationTime);
71 cmd.AddValue("udp", "UDP if set to 1, TCP otherwise", udp);
72 cmd.AddValue("useRts", "Enable/disable RTS/CTS", useRts);
73 cmd.AddValue("use80Plus80", "Enable/disable use of 80+80 MHz", use80Plus80);
74 cmd.AddValue("mcs", "if set, limit testing to a specific MCS (0-9)", mcs);
75 cmd.AddValue("phyModel",
76 "PHY model to use (Yans or Spectrum). If 80+80 MHz is enabled, then Spectrum is "
77 "automatically selected",
78 phyModel);
79 cmd.AddValue("minExpectedThroughput",
80 "if set, simulation fails if the lowest throughput is below this value",
81 minExpectedThroughput);
82 cmd.AddValue("maxExpectedThroughput",
83 "if set, simulation fails if the highest throughput is above this value",
84 maxExpectedThroughput);
85 cmd.Parse(argc, argv);
86
87 if (phyModel != "Yans" && phyModel != "Spectrum")
88 {
89 NS_ABORT_MSG("Invalid PHY model (must be Yans or Spectrum)");
90 }
91 if (use80Plus80)
92 {
93 // SpectrumWifiPhy is required for 80+80 MHz
94 phyModel = "Spectrum";
95 }
96
97 if (useRts)
98 {
99 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue("0"));
100 }
101
102 double prevThroughput[8] = {0};
103
104 std::cout << "MCS value"
105 << "\t\t"
106 << "Channel width"
107 << "\t\t"
108 << "short GI"
109 << "\t\t"
110 << "Throughput" << '\n';
111 int minMcs = 0;
112 int maxMcs = 9;
113 if (mcs >= 0 && mcs <= 9)
114 {
115 minMcs = mcs;
116 maxMcs = mcs;
117 }
118 for (int mcs = minMcs; mcs <= maxMcs; mcs++)
119 {
120 uint8_t index = 0;
121 double previous = 0;
122 for (int channelWidth = 20; channelWidth <= 160;)
123 {
124 if (mcs == 9 && channelWidth == 20)
125 {
126 channelWidth *= 2;
127 continue;
128 }
129 const auto is80Plus80 = (use80Plus80 && (channelWidth == 160));
130 const std::string widthStr = is80Plus80 ? "80+80" : std::to_string(channelWidth);
131 const auto segmentWidthStr = is80Plus80 ? "80" : widthStr;
132 for (auto sgi : {false, true})
133 {
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 NodeContainer wifiStaNode;
146 wifiStaNode.Create(1);
148 wifiApNode.Create(1);
149
150 NetDeviceContainer apDevice;
151 NetDeviceContainer staDevice;
154 std::string channelStr{"{0, " + segmentWidthStr + ", BAND_5GHZ, 0}"};
155
156 std::ostringstream ossControlMode;
157 auto nonHtRefRateMbps = VhtPhy::GetNonHtReferenceRate(mcs) / 1e6;
158 ossControlMode << "OfdmRate" << nonHtRefRateMbps << "Mbps";
159
160 std::ostringstream ossDataMode;
161 ossDataMode << "VhtMcs" << mcs;
162
163 if (is80Plus80)
164 {
165 channelStr += std::string(";") + channelStr;
166 }
167
168 wifi.SetStandard(WIFI_STANDARD_80211ac);
169 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
170 "DataMode",
171 StringValue(ossDataMode.str()),
172 "ControlMode",
173 StringValue(ossControlMode.str()));
174
175 // Set guard interval
176 wifi.ConfigHtOptions("ShortGuardIntervalSupported", BooleanValue(sgi));
177
178 Ssid ssid = Ssid("ns3-80211ac");
179
180 if (phyModel == "Spectrum")
181 {
182 auto spectrumChannel = CreateObject<MultiModelSpectrumChannel>();
184 spectrumChannel->AddPropagationLossModel(lossModel);
185
187 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
188 phy.SetChannel(spectrumChannel);
189
190 phy.Set("ChannelSettings",
191 StringValue("{0, " + std::to_string(channelWidth) + ", BAND_5GHZ, 0}"));
192
193 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
194 staDevice = wifi.Install(phy, mac, wifiStaNode);
195
196 mac.SetType("ns3::ApWifiMac",
197 "EnableBeaconJitter",
198 BooleanValue(false),
199 "Ssid",
200 SsidValue(ssid));
201 apDevice = wifi.Install(phy, mac, wifiApNode);
202 }
203 else
204 {
207 phy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
208 phy.SetChannel(channel.Create());
209
210 phy.Set("ChannelSettings",
211 StringValue("{0, " + std::to_string(channelWidth) + ", BAND_5GHZ, 0}"));
212
213 mac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
214 staDevice = wifi.Install(phy, mac, wifiStaNode);
215
216 mac.SetType("ns3::ApWifiMac",
217 "EnableBeaconJitter",
218 BooleanValue(false),
219 "Ssid",
220 SsidValue(ssid));
221 apDevice = wifi.Install(phy, mac, wifiApNode);
222 }
223
224 int64_t streamNumber = 150;
225 streamNumber += WifiHelper::AssignStreams(apDevice, streamNumber);
226 streamNumber += WifiHelper::AssignStreams(staDevice, streamNumber);
227
228 // mobility.
231
232 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
233 positionAlloc->Add(Vector(distance, 0.0, 0.0));
234 mobility.SetPositionAllocator(positionAlloc);
235
236 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
237
238 mobility.Install(wifiApNode);
239 mobility.Install(wifiStaNode);
240
241 /* Internet stack*/
243 stack.Install(wifiApNode);
244 stack.Install(wifiStaNode);
245 streamNumber += stack.AssignStreams(wifiApNode, streamNumber);
246 streamNumber += stack.AssignStreams(wifiStaNode, streamNumber);
247
249 address.SetBase("192.168.1.0", "255.255.255.0");
250 Ipv4InterfaceContainer staNodeInterface;
251 Ipv4InterfaceContainer apNodeInterface;
252
253 staNodeInterface = address.Assign(staDevice);
254 apNodeInterface = address.Assign(apDevice);
255
256 /* Setting applications */
257 const auto maxLoad =
258 VhtPhy::GetDataRate(mcs, channelWidth, NanoSeconds(sgi ? 400 : 800), 1);
259 ApplicationContainer serverApp;
260 if (udp)
261 {
262 // UDP flow
263 uint16_t port = 9;
265 serverApp = server.Install(wifiStaNode.Get(0));
266 streamNumber += server.AssignStreams(wifiStaNode.Get(0), streamNumber);
267
268 serverApp.Start(Seconds(0.0));
269 serverApp.Stop(simulationTime + Seconds(1.0));
270 const auto packetInterval = payloadSize * 8.0 / maxLoad;
271
272 UdpClientHelper client(staNodeInterface.GetAddress(0), port);
273 client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
274 client.SetAttribute("Interval", TimeValue(Seconds(packetInterval)));
275 client.SetAttribute("PacketSize", UintegerValue(payloadSize));
276 ApplicationContainer clientApp = client.Install(wifiApNode.Get(0));
277 streamNumber += client.AssignStreams(wifiApNode.Get(0), streamNumber);
278
279 clientApp.Start(Seconds(1.0));
280 clientApp.Stop(simulationTime + Seconds(1.0));
281 }
282 else
283 {
284 // TCP flow
285 uint16_t port = 50000;
287 PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
288 serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
289 streamNumber +=
290 packetSinkHelper.AssignStreams(wifiStaNode.Get(0), streamNumber);
291
292 serverApp.Start(Seconds(0.0));
293 serverApp.Stop(simulationTime + Seconds(1.0));
294
295 OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
296 onoff.SetAttribute("OnTime",
297 StringValue("ns3::ConstantRandomVariable[Constant=1]"));
298 onoff.SetAttribute("OffTime",
299 StringValue("ns3::ConstantRandomVariable[Constant=0]"));
300 onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
301 onoff.SetAttribute("DataRate", DataRateValue(maxLoad));
303 InetSocketAddress(staNodeInterface.GetAddress(0), port));
304 onoff.SetAttribute("Remote", remoteAddress);
305 ApplicationContainer clientApp = onoff.Install(wifiApNode.Get(0));
306 streamNumber += onoff.AssignStreams(wifiApNode.Get(0), streamNumber);
307
308 clientApp.Start(Seconds(1.0));
309 clientApp.Stop(simulationTime + Seconds(1.0));
310 }
311
313
314 Simulator::Stop(simulationTime + Seconds(1.0));
316
317 auto rxBytes = 0.0;
318 if (udp)
319 {
320 rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
321 }
322 else
323 {
324 rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
325 }
326 auto throughput = (rxBytes * 8) / simulationTime.GetMicroSeconds(); // Mbit/s
327
329
330 std::cout << +mcs << "\t\t\t" << widthStr << " MHz\t\t"
331 << (widthStr.size() > 3 ? "" : "\t") << (sgi ? "400 ns" : "800 ns")
332 << "\t\t\t" << throughput << " Mbit/s" << std::endl;
333
334 // test first element
335 if (mcs == minMcs && channelWidth == 20 && !sgi)
336 {
337 if (throughput < minExpectedThroughput)
338 {
339 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
340 exit(1);
341 }
342 }
343 // test last element
344 if (mcs == maxMcs && channelWidth == 160 && sgi)
345 {
346 if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
347 {
348 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
349 exit(1);
350 }
351 }
352 // test previous throughput is smaller (for the same mcs)
353 if (throughput > previous)
354 {
355 previous = throughput;
356 }
357 else
358 {
359 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
360 exit(1);
361 }
362 // test previous throughput is smaller (for the same channel width and GI)
363 if (throughput > prevThroughput[index])
364 {
365 prevThroughput[index] = throughput;
366 }
367 else
368 {
369 NS_LOG_ERROR("Obtained throughput " << throughput << " is not expected!");
370 exit(1);
371 }
372 index++;
373 }
374 channelWidth *= 2;
375 }
376 }
377 return 0;
378}
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.
Parse command-line arguments.
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
Make it easy to create and manage PHY objects for the spectrum model.
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
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
static uint64_t GetDataRate(uint8_t mcsValue, MHz_u channelWidth, Time guardInterval, uint8_t nss)
Return the data rate corresponding to the supplied VHT MCS index, channel width, guard interval,...
Definition vht-phy.cc:448
static uint64_t GetNonHtReferenceRate(uint8_t mcsValue)
Calculate the rate in bps of the non-HT Reference Rate corresponding to the supplied VHT MCS index.
Definition vht-phy.cc:478
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.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
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_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#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
@ WIFI_STANDARD_80211ac
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
STL namespace.
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