A Discrete-Event Network Simulator
API
wifi-multi-tos.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2016
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/applications-module.h"
23 #include "ns3/wifi-module.h"
24 #include "ns3/mobility-module.h"
25 #include "ns3/internet-module.h"
26 
27 // This is a simple example in order to show how to configure an IEEE 802.11n Wi-Fi network
28 // with multiple TOS. It outputs the aggregated UDP throughput, which depends on the number of
29 // stations, the HT MCS value (0 to 7), the channel width (20 or 40 MHz) and the guard interval
30 // (long or short). The user can also specify the distance between the access point and the
31 // stations (in meters), and can specify whether RTS/CTS is used or not.
32 
33 using namespace ns3;
34 
35 NS_LOG_COMPONENT_DEFINE ("WifiMultiTos");
36 
37 int main (int argc, char *argv[])
38 {
39  uint32_t nWifi = 4;
40  double simulationTime = 10; //seconds
41  double distance = 1.0; //meters
42  uint16_t mcs = 7;
43  uint8_t channelWidth = 20; //MHz
44  bool useShortGuardInterval = false;
45  bool useRts = false;
46 
48  cmd.AddValue ("nWifi", "Number of stations", nWifi);
49  cmd.AddValue ("distance", "Distance in meters between the stations and the access point", distance);
50  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
51  cmd.AddValue ("useRts", "Enable/disable RTS/CTS", useRts);
52  cmd.AddValue ("mcs", "MCS value (0 - 7)", mcs);
53  cmd.AddValue ("channelWidth", "Channel width in MHz", channelWidth);
54  cmd.AddValue ("useShortGuardInterval", "Enable/disable short guard interval", useShortGuardInterval);
55  cmd.Parse (argc,argv);
56 
58  wifiStaNodes.Create (nWifi);
60  wifiApNode.Create (1);
61 
64  phy.SetChannel (channel.Create ());
65 
66  // Set guard interval
67  phy.Set ("ShortGuardEnabled", BooleanValue (useShortGuardInterval));
68 
72 
73  std::ostringstream oss;
74  oss << "HtMcs" << mcs;
75  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
76  "DataMode", StringValue (oss.str ()),
77  "ControlMode", StringValue (oss.str ()),
78  "RtsCtsThreshold", UintegerValue (useRts ? 0 : 999999));
79 
80  Ssid ssid = Ssid ("ns3-80211n");
81 
82  mac.SetType ("ns3::StaWifiMac",
83  "Ssid", SsidValue (ssid));
84 
86  staDevices = wifi.Install (phy, mac, wifiStaNodes);
87 
88  mac.SetType ("ns3::ApWifiMac",
89  "Ssid", SsidValue (ssid));
90 
91  NetDeviceContainer apDevice;
92  apDevice = wifi.Install (phy, mac, wifiApNode);
93 
94  // Set channel width
95  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (channelWidth));
96 
97  // mobility
99  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
100  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
101  for (uint32_t i = 0; i < nWifi; i++)
102  {
103  positionAlloc->Add (Vector (distance, 0.0, 0.0));
104  }
105  mobility.SetPositionAllocator (positionAlloc);
106  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
107  mobility.Install (wifiApNode);
108  mobility.Install (wifiStaNodes);
109 
110  // Internet stack
112  stack.Install (wifiApNode);
113  stack.Install (wifiStaNodes);
115 
116  address.SetBase ("192.168.1.0", "255.255.255.0");
117  Ipv4InterfaceContainer staNodeInterfaces, apNodeInterface;
118 
119  staNodeInterfaces = address.Assign (staDevices);
120  apNodeInterface = address.Assign (apDevice);
121 
122  // Setting applications
123  ApplicationContainer sourceApplications, sinkApplications;
124  std::vector<uint8_t> tosValues = {0x70, 0x28, 0xb8, 0xc0}; //AC_BE, AC_BK, AC_VI, AC_VO
125  uint32_t portNumber = 9;
126  for (uint8_t index = 0; index < nWifi; ++index)
127  {
128  for (uint8_t tosValue : tosValues)
129  {
130  auto ipv4 = wifiApNode.Get (0)->GetObject<Ipv4> ();
131  const auto address = ipv4->GetAddress (1, 0).GetLocal ();
132  InetSocketAddress sinkSocket (address, portNumber++);
133  sinkSocket.SetTos (tosValue);
134  OnOffHelper onOffHelper ("ns3::UdpSocketFactory", sinkSocket);
135  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
136  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
137  onOffHelper.SetAttribute ("DataRate", DataRateValue (50000000 / nWifi));
138  onOffHelper.SetAttribute ("PacketSize", UintegerValue (1472)); //bytes
139  sourceApplications.Add (onOffHelper.Install (wifiStaNodes.Get (index)));
140  PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", sinkSocket);
141  sinkApplications.Add (packetSinkHelper.Install (wifiApNode.Get (0)));
142  }
143  }
144 
145  sinkApplications.Start (Seconds (0.0));
146  sinkApplications.Stop (Seconds (simulationTime + 1));
147  sourceApplications.Start (Seconds (1.0));
148  sourceApplications.Stop (Seconds (simulationTime + 1));
149 
151 
152  Simulator::Stop (Seconds (simulationTime + 1));
153  Simulator::Run ();
155 
156  double throughput = 0;
157  for (unsigned index = 0; index < sinkApplications.GetN (); ++index)
158  {
159  uint64_t totalPacketsThrough = DynamicCast<PacketSink> (sinkApplications.Get (index))->GetTotalRx ();
160  throughput += ((totalPacketsThrough * 8) / (simulationTime * 1000000.0)); //Mbit/s
161  }
162  if (throughput > 0)
163  {
164  std::cout << "Aggregated throughput: " << throughput << " Mbit/s" << std::endl;
165  }
166  else
167  {
168  NS_LOG_ERROR ("Obtained throughput is 0!");
169  exit (1);
170  }
171  return 0;
172 }
tuple channel
Definition: third.py:85
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:132
holds a vector of ns3::Application pointers.
an Inet address class
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
AttributeValue implementation for Boolean.
Definition: boolean.h:36
HT PHY for the 5 GHz band (clause 20)
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:719
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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:777
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:226
#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.
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
tuple nWifi
Definition: third.py:52
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:213
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
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
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:742
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:748
tuple staDevices
Definition: third.py:96
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:190
Access to the IPv4 forwarding table, interfaces, and configuration.
Definition: ipv4.h:76
tuple wifiApNode
Definition: third.py:83
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.
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:35
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.
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:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
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:993
AttributeValue implementation for Ssid.
Definition: ssid.h:117
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.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:253
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.
tuple wifiStaNodes
Definition: third.py:81