A Discrete-Event Network Simulator
QKDNetSim v2.0 (NS-3 v3.41) @ (+)
API
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
60 using namespace ns3;
61 
62 NS_LOG_COMPONENT_DEFINE("ht-wifi-network");
63 
64 int
65 main(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  ApplicationContainer serverApp;
228  if (udp)
229  {
230  // UDP flow
231  uint16_t port = 9;
233  serverApp = server.Install(wifiStaNode.Get(0));
234  serverApp.Start(Seconds(0.0));
235  serverApp.Stop(Seconds(simulationTime + 1));
236 
237  UdpClientHelper client(staNodeInterface.GetAddress(0), port);
238  client.SetAttribute("MaxPackets", UintegerValue(4294967295U));
239  client.SetAttribute("Interval", TimeValue(Time("0.00001"))); // packets/s
240  client.SetAttribute("PacketSize", UintegerValue(payloadSize));
241  ApplicationContainer clientApp = client.Install(wifiApNode.Get(0));
242  clientApp.Start(Seconds(1.0));
243  clientApp.Stop(Seconds(simulationTime + 1));
244  }
245  else
246  {
247  // TCP flow
248  uint16_t port = 50000;
250  PacketSinkHelper packetSinkHelper("ns3::TcpSocketFactory", localAddress);
251  serverApp = packetSinkHelper.Install(wifiStaNode.Get(0));
252  serverApp.Start(Seconds(0.0));
253  serverApp.Stop(Seconds(simulationTime + 1));
254 
255  OnOffHelper onoff("ns3::TcpSocketFactory", Ipv4Address::GetAny());
256  onoff.SetAttribute("OnTime",
257  StringValue("ns3::ConstantRandomVariable[Constant=1]"));
258  onoff.SetAttribute("OffTime",
259  StringValue("ns3::ConstantRandomVariable[Constant=0]"));
260  onoff.SetAttribute("PacketSize", UintegerValue(payloadSize));
261  onoff.SetAttribute("DataRate", DataRateValue(200000000)); // bit/s
262  AddressValue remoteAddress(
263  InetSocketAddress(staNodeInterface.GetAddress(0), port));
264  onoff.SetAttribute("Remote", remoteAddress);
265  ApplicationContainer clientApp = onoff.Install(wifiApNode.Get(0));
266  clientApp.Start(Seconds(1.0));
267  clientApp.Stop(Seconds(simulationTime + 1));
268  }
269 
271 
272  Simulator::Stop(Seconds(simulationTime + 1));
273  Simulator::Run();
274 
275  uint64_t rxBytes = 0;
276  if (udp)
277  {
278  rxBytes = payloadSize * DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
279  }
280  else
281  {
282  rxBytes = DynamicCast<PacketSink>(serverApp.Get(0))->GetTotalRx();
283  }
284  double throughput = (rxBytes * 8) / (simulationTime * 1000000.0); // Mbit/s
285 
287 
288  std::cout << mcs << "\t\t\t" << channelWidth << " MHz\t\t\t" << std::boolalpha
289  << sgi << "\t\t\t" << throughput << " Mbit/s" << std::endl;
290 
291  // test first element
292  if (mcs == 0 && channelWidth == 20 && !sgi)
293  {
294  if (throughput < minExpectedThroughput)
295  {
296  NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
297  }
298  }
299  // test last element
300  if (mcs == 7 && channelWidth == 40 && sgi)
301  {
302  if (maxExpectedThroughput > 0 && throughput > maxExpectedThroughput)
303  {
304  NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
305  }
306  }
307  // test previous throughput is smaller (for the same mcs)
308  if (throughput > previous)
309  {
310  previous = throughput;
311  }
312  else
313  {
314  NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
315  }
316  // test previous throughput is smaller (for the same channel width and GI)
317  if (throughput > prevThroughput[index])
318  {
319  prevThroughput[index] = throughput;
320  }
321  else
322  {
323  NS_FATAL_ERROR("Obtained throughput " << throughput << " is not expected!");
324  }
325  index++;
326  }
327  channelWidth *= 2;
328  }
329  }
330  return 0;
331 }
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 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: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
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, WifiPhyBand, uint8_t > ChannelTuple
Tuple identifying an operating channel.
Definition: wifi-phy.h:891
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:890
#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:1326
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
address
Definition: first.py:47
stack
Definition: first.py:44
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
ssid
Definition: third.py:93
channel
Definition: third.py:88
mac
Definition: third.py:92
wifi
Definition: third.py:95
wifiApNode
Definition: third.py:86
mobility
Definition: third.py:105
phy
Definition: third.py:89
std::ofstream throughput