A Discrete-Event Network Simulator
API
wifi-rate-adaptation-distance.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Universidad de la República - Uruguay
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  * Author: Matías Richart <mrichart@fing.edu.uy>
18  */
19 
56 #include "ns3/boolean.h"
57 #include "ns3/command-line.h"
58 #include "ns3/config.h"
59 #include "ns3/gnuplot.h"
60 #include "ns3/internet-stack-helper.h"
61 #include "ns3/ipv4-address-helper.h"
62 #include "ns3/log.h"
63 #include "ns3/mobility-helper.h"
64 #include "ns3/mobility-model.h"
65 #include "ns3/on-off-helper.h"
66 #include "ns3/packet-sink-helper.h"
67 #include "ns3/ssid.h"
68 #include "ns3/uinteger.h"
69 #include "ns3/yans-wifi-channel.h"
70 #include "ns3/yans-wifi-helper.h"
71 
72 using namespace ns3;
73 
74 NS_LOG_COMPONENT_DEFINE("RateAdaptationDistance");
75 
77 class NodeStatistics
78 {
79  public:
86 
93  void RxCallback(std::string path, Ptr<const Packet> packet, const Address& from);
99  void SetPosition(Ptr<Node> node, Vector position);
106  void AdvancePosition(Ptr<Node> node, int stepsSize, int stepsTime);
112  Vector GetPosition(Ptr<Node> node);
117 
118  private:
119  uint32_t m_bytesTotal;
120  Gnuplot2dDataset m_output;
121 };
122 
124 {
125  m_bytesTotal = 0;
126 }
127 
128 void
129 NodeStatistics::RxCallback(std::string path, Ptr<const Packet> packet, const Address& from)
130 {
131  m_bytesTotal += packet->GetSize();
132 }
133 
134 void
135 NodeStatistics::SetPosition(Ptr<Node> node, Vector position)
136 {
138  mobility->SetPosition(position);
139 }
140 
141 Vector
143 {
145  return mobility->GetPosition();
146 }
147 
148 void
149 NodeStatistics::AdvancePosition(Ptr<Node> node, int stepsSize, int stepsTime)
150 {
151  Vector pos = GetPosition(node);
152  double mbs = ((m_bytesTotal * 8.0) / (1000000 * stepsTime));
153  m_bytesTotal = 0;
154  m_output.Add(pos.x, mbs);
155  pos.x += stepsSize;
156  SetPosition(node, pos);
157  Simulator::Schedule(Seconds(stepsTime),
159  this,
160  node,
161  stepsSize,
162  stepsTime);
163 }
164 
167 {
168  return m_output;
169 }
170 
171 void
172 RateCallback(std::string path, uint64_t rate, Mac48Address dest)
173 {
174  NS_LOG_INFO((Simulator::Now()).GetSeconds() << " " << dest << " Rate " << rate / 1000000.0);
175 }
176 
177 int
178 main(int argc, char* argv[])
179 {
180  uint32_t rtsThreshold = 65535;
181  std::string staManager = "ns3::MinstrelHtWifiManager";
182  std::string apManager = "ns3::MinstrelHtWifiManager";
183  std::string standard = "802.11n-5GHz";
184  std::string outputFileName = "minstrelHT";
185  uint32_t BeMaxAmpduSize = 65535;
186  bool shortGuardInterval = false;
187  uint32_t chWidth = 20;
188  int ap1_x = 0;
189  int ap1_y = 0;
190  int sta1_x = 5;
191  int sta1_y = 0;
192  int steps = 100;
193  int stepsSize = 1;
194  int stepsTime = 1;
195 
196  CommandLine cmd(__FILE__);
197  cmd.AddValue("staManager", "PRC Manager of the STA", staManager);
198  cmd.AddValue("apManager", "PRC Manager of the AP", apManager);
199  cmd.AddValue("standard", "Wifi Phy Standard", standard);
200  cmd.AddValue("shortGuardInterval",
201  "Enable Short Guard Interval in all stations",
202  shortGuardInterval);
203  cmd.AddValue("channelWidth", "Channel width of all the stations", chWidth);
204  cmd.AddValue("rtsThreshold", "RTS threshold", rtsThreshold);
205  cmd.AddValue("BeMaxAmpduSize", "BE Mac A-MPDU size", BeMaxAmpduSize);
206  cmd.AddValue("outputFileName", "Output filename", outputFileName);
207  cmd.AddValue("steps", "How many different distances to try", steps);
208  cmd.AddValue("stepsTime", "Time on each step", stepsTime);
209  cmd.AddValue("stepsSize", "Distance between steps", stepsSize);
210  cmd.AddValue("AP1_x", "Position of AP1 in x coordinate", ap1_x);
211  cmd.AddValue("AP1_y", "Position of AP1 in y coordinate", ap1_y);
212  cmd.AddValue("STA1_x", "Position of STA1 in x coordinate", sta1_x);
213  cmd.AddValue("STA1_y", "Position of STA1 in y coordinate", sta1_y);
214  cmd.Parse(argc, argv);
215 
216  int simuTime = steps * stepsTime;
217 
218  // Define the APs
219  NodeContainer wifiApNodes;
220  wifiApNodes.Create(1);
221 
222  // Define the STAs
224  wifiStaNodes.Create(1);
225 
226  YansWifiPhyHelper wifiPhy;
228  wifiPhy.SetChannel(wifiChannel.Create());
229 
230  NetDeviceContainer wifiApDevices;
231  NetDeviceContainer wifiStaDevices;
232  NetDeviceContainer wifiDevices;
233 
235  if (standard == "802.11a" || standard == "802.11b" || standard == "802.11g")
236  {
237  if (standard == "802.11a")
238  {
239  wifi.SetStandard(WIFI_STANDARD_80211a);
240  }
241  else if (standard == "802.11b")
242  {
243  wifi.SetStandard(WIFI_STANDARD_80211b);
244  }
245  else if (standard == "802.11g")
246  {
247  wifi.SetStandard(WIFI_STANDARD_80211g);
248  }
249  WifiMacHelper wifiMac;
250 
251  // Configure the STA node
252  wifi.SetRemoteStationManager(staManager, "RtsCtsThreshold", UintegerValue(rtsThreshold));
253 
254  Ssid ssid = Ssid("AP");
255  wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
256  wifiStaDevices.Add(wifi.Install(wifiPhy, wifiMac, wifiStaNodes.Get(0)));
257 
258  // Configure the AP node
259  wifi.SetRemoteStationManager(apManager, "RtsCtsThreshold", UintegerValue(rtsThreshold));
260 
261  ssid = Ssid("AP");
262  wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
263  wifiApDevices.Add(wifi.Install(wifiPhy, wifiMac, wifiApNodes.Get(0)));
264  }
265  else if (standard == "802.11n-2.4GHz" || standard == "802.11n-5GHz" || standard == "802.11ac")
266  {
267  if (standard == "802.11n-2.4GHz" || standard == "802.11n-5GHz")
268  {
269  wifi.SetStandard(WIFI_STANDARD_80211n);
270  }
271  else if (standard == "802.11ac")
272  {
273  wifi.SetStandard(WIFI_STANDARD_80211ac);
274  }
275 
276  WifiMacHelper wifiMac;
277 
278  // Configure the STA node
279  wifi.SetRemoteStationManager(staManager, "RtsCtsThreshold", UintegerValue(rtsThreshold));
280 
281  Ssid ssid = Ssid("AP");
282  wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
283  wifiStaDevices.Add(wifi.Install(wifiPhy, wifiMac, wifiStaNodes.Get(0)));
284 
285  // Configure the AP node
286  wifi.SetRemoteStationManager(apManager, "RtsCtsThreshold", UintegerValue(rtsThreshold));
287 
288  ssid = Ssid("AP");
289  wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
290  wifiApDevices.Add(wifi.Install(wifiPhy, wifiMac, wifiApNodes.Get(0)));
291 
292  Config::Set("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/BE_MaxAmpduSize",
293  UintegerValue(BeMaxAmpduSize));
294  }
295 
296  wifiDevices.Add(wifiStaDevices);
297  wifiDevices.Add(wifiApDevices);
298 
299  // Set channel width
300  Config::Set("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth",
301  UintegerValue(chWidth));
302 
303  // Set guard interval
304  Config::Set(
305  "/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported",
306  BooleanValue(shortGuardInterval));
307 
308  // Configure the mobility.
310  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
311  // Initial position of AP and STA
312  positionAlloc->Add(Vector(ap1_x, ap1_y, 0.0));
313  positionAlloc->Add(Vector(sta1_x, sta1_y, 0.0));
314  mobility.SetPositionAllocator(positionAlloc);
315  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
316  mobility.Install(wifiApNodes.Get(0));
317  mobility.Install(wifiStaNodes.Get(0));
318 
319  // Statistics counter
320  NodeStatistics atpCounter = NodeStatistics(wifiApDevices, wifiStaDevices);
321 
322  // Move the STA by stepsSize meters every stepsTime seconds
323  Simulator::Schedule(Seconds(0.5 + stepsTime),
325  &atpCounter,
326  wifiStaNodes.Get(0),
327  stepsSize,
328  stepsTime);
329 
330  // Configure the IP stack
332  stack.Install(wifiApNodes);
333  stack.Install(wifiStaNodes);
335  address.SetBase("10.1.1.0", "255.255.255.0");
336  Ipv4InterfaceContainer i = address.Assign(wifiDevices);
337  Ipv4Address sinkAddress = i.GetAddress(0);
338  uint16_t port = 9;
339 
340  // Configure the CBR generator
341  PacketSinkHelper sink("ns3::UdpSocketFactory", InetSocketAddress(sinkAddress, port));
342  ApplicationContainer apps_sink = sink.Install(wifiStaNodes.Get(0));
343 
344  OnOffHelper onoff("ns3::UdpSocketFactory", InetSocketAddress(sinkAddress, port));
345  onoff.SetConstantRate(DataRate("400Mb/s"), 1420);
346  onoff.SetAttribute("StartTime", TimeValue(Seconds(0.5)));
347  onoff.SetAttribute("StopTime", TimeValue(Seconds(simuTime)));
348  ApplicationContainer apps_source = onoff.Install(wifiApNodes.Get(0));
349 
350  apps_sink.Start(Seconds(0.5));
351  apps_sink.Stop(Seconds(simuTime));
352 
353  //------------------------------------------------------------
354  //-- Setup stats and data collection
355  //--------------------------------------------
356 
357  // Register packet receptions to calculate throughput
358  Config::Connect("/NodeList/1/ApplicationList/*/$ns3::PacketSink/Rx",
360 
361  // Callbacks to print every change of rate
362  Config::ConnectFailSafe("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" +
363  apManager + "/RateChange",
365 
366  Simulator::Stop(Seconds(simuTime));
367  Simulator::Run();
368 
369  std::ofstream outfile("throughput-" + outputFileName + ".plt");
370  Gnuplot gnuplot = Gnuplot("throughput-" + outputFileName + ".eps", "Throughput");
371  gnuplot.SetTerminal("post eps color enhanced");
372  gnuplot.SetLegend("Time (seconds)", "Throughput (Mb/s)");
373  gnuplot.SetTitle("Throughput (AP to STA) vs time");
374  gnuplot.AddDataset(atpCounter.GetDatafile());
375  gnuplot.GenerateOutput(outfile);
376 
378 
379  return 0;
380 }
Class to collect node statistics.
void SetPosition(Ptr< Node > node, Vector position)
Set node position.
Gnuplot2dDataset GetDatafile()
void RxCallback(std::string path, Ptr< const Packet > packet, const Address &from)
RX callback.
NodeStatistics(NetDeviceContainer aps, NetDeviceContainer stas)
Constructor.
Vector GetPosition(Ptr< Node > node)
Get node position.
void AdvancePosition(Ptr< Node > node, int stepsSize, int stepsTime)
Advance node position.
a polymophic address class
Definition: address.h:100
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.
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
Class to represent a 2D points plot.
Definition: gnuplot.h:116
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:776
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:764
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:802
void SetTitle(const std::string &title)
Definition: gnuplot.cc:770
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.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
an EUI-48 address
Definition: mac48-address.h:46
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:568
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:199
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.
AttributeValue implementation for Time.
Definition: nstime.h:1423
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:325
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
uint16_t port
Definition: dsdv-manet.cc:45
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:975
bool ConnectFailSafe(std::string path, const CallbackBase &cb)
Definition: config.cc:985
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:877
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
@ WIFI_STANDARD_80211a
@ WIFI_STANDARD_80211n
@ WIFI_STANDARD_80211g
@ WIFI_STANDARD_80211ac
@ WIFI_STANDARD_80211b
address
Definition: first.py:40
stack
Definition: first.py:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:707
cmd
Definition: second.py:33
ssid
Definition: third.py:86
wifi
Definition: third.py:88
mobility
Definition: third.py:96
wifiStaNodes
Definition: third.py:77
void RateCallback(std::string path, uint64_t rate, Mac48Address dest)
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55