A Discrete-Event Network Simulator
API
vht-wifi-network.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015 SEBASTIEN DERONNE
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Sebastien Deronne <sebastien.deronne@gmail.com>
19  */
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/applications-module.h"
24 #include "ns3/wifi-module.h"
25 #include "ns3/mobility-module.h"
26 #include "ns3/ipv4-global-routing-helper.h"
27 #include "ns3/internet-module.h"
28 
29 // This is a simple example in order to show how to configure an IEEE 802.11ac Wi-Fi network.
30 //
31 // It ouputs the UDP or TCP goodput for every VHT bitrate value, which depends on the MCS value (0 to 9, where 9 is
32 // forbidden when the channel width is 20 MHz), the channel width (20, 40, 80 or 160 MHz) and the guard interval (long
33 // or short). The PHY bitrate is constant over all the simulation run. The user can also specify the distance between
34 // the access point and the station: the larger the distance the smaller the goodput.
35 //
36 // The simulation assumes a single station in an infrastructure network:
37 //
38 // STA AP
39 // * *
40 // | |
41 // n1 n2
42 //
43 //Packets in this simulation aren't marked with a QosTag so they are considered
44 //belonging to BestEffort Access Class (AC_BE).
45 
46 using namespace ns3;
47 
48 NS_LOG_COMPONENT_DEFINE ("vht-wifi-network");
49 
50 int main (int argc, char *argv[])
51 {
52  bool udp = true;
53  double simulationTime = 10; //seconds
54  double distance = 1.0; //meters
55 
57  cmd.AddValue ("distance", "Distance in meters between the station and the access point", distance);
58  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
59  cmd.AddValue ("udp", "UDP if set to 1, TCP otherwise", udp);
60  cmd.Parse (argc,argv);
61 
62  std::cout << "MCS value" << "\t\t" << "Channel width" << "\t\t" << "short GI" << "\t\t" << "Throughput" << '\n';
63  for (int i = 0; i <= 9; i++) //MCS
64  {
65  for (int j = 20; j <= 160; ) //channel width
66  {
67  if (i == 9 && j == 20)
68  {
69  j *= 2;
70  continue;
71  }
72  for (int k = 0; k < 2; k++) //GI: 0 and 1
73  {
74  uint32_t payloadSize; //1500 byte IP packet
75  if (udp)
76  {
77  payloadSize = 1472; //bytes
78  }
79  else
80  {
81  payloadSize = 1448; //bytes
82  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
83  }
84 
85  NodeContainer wifiStaNode;
86  wifiStaNode.Create (1);
88  wifiApNode.Create (1);
89 
92  phy.SetChannel (channel.Create ());
93 
94  // Set guard interval
95  phy.Set ("ShortGuardEnabled", BooleanValue (k));
96 
100 
101  std::ostringstream oss;
102  oss << "VhtMcs" << i;
103  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager","DataMode", StringValue (oss.str ()),
104  "ControlMode", StringValue (oss.str ()));
105 
106  Ssid ssid = Ssid ("ns3-80211ac");
107 
108  mac.SetType ("ns3::StaWifiMac",
109  "Ssid", SsidValue (ssid));
110 
111  NetDeviceContainer staDevice;
112  staDevice = wifi.Install (phy, mac, wifiStaNode);
113 
114  mac.SetType ("ns3::ApWifiMac",
115  "Ssid", SsidValue (ssid));
116 
117  NetDeviceContainer apDevice;
118  apDevice = wifi.Install (phy, mac, wifiApNode);
119 
120  // Set channel width
121  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (j));
122 
123  // mobility.
125  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
126 
127  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
128  positionAlloc->Add (Vector (distance, 0.0, 0.0));
129  mobility.SetPositionAllocator (positionAlloc);
130 
131  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
132 
133  mobility.Install (wifiApNode);
134  mobility.Install (wifiStaNode);
135 
136  /* Internet stack*/
138  stack.Install (wifiApNode);
139  stack.Install (wifiStaNode);
140 
142 
143  address.SetBase ("192.168.1.0", "255.255.255.0");
144  Ipv4InterfaceContainer staNodeInterface;
145  Ipv4InterfaceContainer apNodeInterface;
146 
147  staNodeInterface = address.Assign (staDevice);
148  apNodeInterface = address.Assign (apDevice);
149 
150  /* Setting applications */
151  ApplicationContainer serverApp, sinkApp;
152  if (udp)
153  {
154  //UDP flow
155  UdpServerHelper myServer (9);
156  serverApp = myServer.Install (wifiStaNode.Get (0));
157  serverApp.Start (Seconds (0.0));
158  serverApp.Stop (Seconds (simulationTime + 1));
159 
160  UdpClientHelper myClient (staNodeInterface.GetAddress (0), 9);
161  myClient.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
162  myClient.SetAttribute ("Interval", TimeValue (Time ("0.00001"))); //packets/s
163  myClient.SetAttribute ("PacketSize", UintegerValue (payloadSize));
164 
165  ApplicationContainer clientApp = myClient.Install (wifiApNode.Get (0));
166  clientApp.Start (Seconds (1.0));
167  clientApp.Stop (Seconds (simulationTime + 1));
168  }
169  else
170  {
171  //TCP flow
172  uint16_t port = 50000;
173  Address apLocalAddress (InetSocketAddress (Ipv4Address::GetAny (), port));
174  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", apLocalAddress);
175  sinkApp = packetSinkHelper.Install (wifiStaNode.Get (0));
176 
177  sinkApp.Start (Seconds (0.0));
178  sinkApp.Stop (Seconds (simulationTime + 1));
179 
180  OnOffHelper onoff ("ns3::TcpSocketFactory",Ipv4Address::GetAny ());
181  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
182  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
183  onoff.SetAttribute ("PacketSize", UintegerValue (payloadSize));
184  onoff.SetAttribute ("DataRate", DataRateValue (1000000000)); //bit/s
186 
187  AddressValue remoteAddress (InetSocketAddress (staNodeInterface.GetAddress (0), port));
188  onoff.SetAttribute ("Remote", remoteAddress);
189  apps.Add (onoff.Install (wifiApNode.Get (0)));
190  apps.Start (Seconds (1.0));
191  apps.Stop (Seconds (simulationTime + 1));
192  }
193 
195 
196  Simulator::Stop (Seconds (simulationTime + 1));
197  Simulator::Run ();
199 
200  double throughput = 0;
201  if (udp)
202  {
203  //UDP
204  uint32_t totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
205  throughput = totalPacketsThrough * payloadSize * 8 / (simulationTime * 1000000.0); //Mbit/s
206  }
207  else
208  {
209  //TCP
210  uint32_t totalPacketsThrough = DynamicCast<PacketSink> (sinkApp.Get (0))->GetTotalRx ();
211  throughput = totalPacketsThrough * 8 / (simulationTime * 1000000.0); //Mbit/s
212  }
213  std::cout << i << "\t\t\t" << j << " MHz\t\t\t" << k << "\t\t\t" << throughput << " Mbit/s" << std::endl;
214  }
215  j *= 2;
216  }
217  }
218  return 0;
219 }
tuple channel
Definition: third.py:85
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:112
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
an Inet address class
static Ipv4Address GetAny(void)
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:34
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:683
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:769
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:231
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:712
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
tuple phy
Definition: third.py:86
Create a client application which sends UDP packets carrying a 32bit sequence number and a 64 bit tim...
AttributeValue implementation for Time.
Definition: nstime.h:957
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:706
Create a server application which waits for input UDP packets and uses the information carried into t...
tuple mac
Definition: third.py:92
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
tuple wifiApNode
Definition: third.py:83
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
VHT OFDM PHY (clause 22)
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
tuple ssid
Definition: third.py:93
manage and create wifi channel objects for the yans model.
create MAC layers for a ns3::WifiNetDevice.
tuple stack
Definition: first.py:34
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:38
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
Helper class used to assign positions and mobility models to nodes.
AttributeValue implementation for Address.
Definition: address.h:278
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
AttributeValue implementation for Ssid.
Definition: ssid.h:95
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void Add(Vector v)
Add a position to the list of positions.
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const