A Discrete-Event Network Simulator
API
wifi-timing-attributes.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  * Authors: 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 example shows how to set Wi-Fi timing parameters through WifiMac attributes.
28 //
29 // Example: set slot time to 20 microseconds, while keeping other values as defined in the simulation script:
30 //
31 // ./waf --run "wifi-timing-attributes --slot=20"
32 //
33 // Network topology:
34 //
35 // Wifi 192.168.1.0
36 //
37 // AP
38 // * *
39 // | |
40 // n1 n2
41 
42 using namespace ns3;
43 
44 NS_LOG_COMPONENT_DEFINE ("wifi-timing-attributes");
45 
46 int main (int argc, char *argv[])
47 {
48  uint32_t slot = 9; //slot time in microseconds
49  uint32_t sifs = 10; //SIFS duration in microseconds
50  uint32_t ackTimeout = 88; //ACK timeout duration in microseconds
51  uint32_t ctsTimeout = 88; //CTS timeout duration in microseconds
52  uint32_t rifs = 2; //RIFS duration in microseconds
53  uint32_t basicBlockAckTimeout = 286; //Basic Block ACK timeout duration in microseconds
54  uint32_t compressedBlockAckTimeout = 112; //Compressed Block ACK timeout duration in microseconds
55  double simulationTime = 10; //simulation time in seconds
56 
58  cmd.AddValue ("slot", "Slot time in microseconds", slot);
59  cmd.AddValue ("sifs", "SIFS duration in microseconds", sifs);
60  cmd.AddValue ("ackTimeout", "ACK timeout duration in microseconds", ackTimeout);
61  cmd.AddValue ("ctsTimeout", "CTS timeout duration in microseconds", ctsTimeout);
62  cmd.AddValue ("rifs", "RIFS duration in microseconds", rifs);
63  cmd.AddValue ("basicBlockAckTimeoutTimeout", "Basic Block ACK timeout duration in microseconds", basicBlockAckTimeout);
64  cmd.AddValue ("compressedBlockAckTimeoutTimeout", "Compressed Block ACK timeout duration in microseconds", compressedBlockAckTimeout);
65  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
66  cmd.Parse (argc,argv);
67 
68  //Since default reference loss is defined for 5 GHz, it needs to be changed when operating at 2.4 GHz
69  Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046));
70 
71  //Create nodes
72  NodeContainer wifiStaNode;
73  wifiStaNode.Create (1);
75  wifiApNode.Create (1);
76 
77  //Create wireless channel
80  phy.SetChannel (channel.Create ());
81 
82  //Default IEEE 802.11n (2.4 GHz)
85  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
86  "DataMode", StringValue ("OfdmRate65MbpsBW20MHz"),
87  "ControlMode", StringValue ("OfdmRate6_5MbpsBW20MHz"));
89 
90  //Install PHY and MAC
91  Ssid ssid = Ssid ("ns3-wifi");
92  mac.SetType ("ns3::StaWifiMac",
93  "Ssid", SsidValue (ssid));
94 
95  NetDeviceContainer staDevice;
96  staDevice = wifi.Install (phy, mac, wifiStaNode);
97 
98  mac.SetType ("ns3::ApWifiMac",
99  "Ssid", SsidValue (ssid));
100 
101  NetDeviceContainer apDevice;
102  apDevice = wifi.Install (phy, mac, wifiApNode);
103 
104  //Once install is done, we overwrite the standard timing values
105  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/Slot", TimeValue (MicroSeconds (slot)));
106  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/Sifs", TimeValue (MicroSeconds (sifs)));
107  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/AckTimeout", TimeValue (MicroSeconds (ackTimeout)));
108  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/CtsTimeout", TimeValue (MicroSeconds (ctsTimeout)));
109  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/Rifs", TimeValue (MicroSeconds (rifs)));
110  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/BasicBlockAckTimeout", TimeValue (MicroSeconds (basicBlockAckTimeout)));
111  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/CompressedBlockAckTimeout", TimeValue (MicroSeconds (compressedBlockAckTimeout)));
112 
113  //Mobility
115  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
116 
117  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
118  positionAlloc->Add (Vector (1.0, 0.0, 0.0));
119  mobility.SetPositionAllocator (positionAlloc);
120 
121  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
122 
123  mobility.Install (wifiApNode);
124  mobility.Install (wifiStaNode);
125 
126  //Internet stack
128  stack.Install (wifiApNode);
129  stack.Install (wifiStaNode);
130 
132 
133  address.SetBase ("192.168.1.0", "255.255.255.0");
134  Ipv4InterfaceContainer staNodeInterface;
135  Ipv4InterfaceContainer apNodeInterface;
136 
137  staNodeInterface = address.Assign (staDevice);
138  apNodeInterface = address.Assign (apDevice);
139 
140  //Setting applications
141  uint16_t port = 9;
142  UdpServerHelper server (port);
143  ApplicationContainer serverApp = server.Install (wifiStaNode.Get (0));
144  serverApp.Start (Seconds (0.0));
145  serverApp.Stop (Seconds (simulationTime));
146 
147  UdpClientHelper client (staNodeInterface.GetAddress (0), port);
148  client.SetAttribute ("MaxPackets", UintegerValue (4294967295u));
149  client.SetAttribute ("Interval", TimeValue (Time ("0.00002"))); //packets/s
150  client.SetAttribute ("PacketSize", UintegerValue (1472)); //bytes
151 
152  ApplicationContainer clientApp = client.Install (wifiApNode.Get (0));
153  clientApp.Start (Seconds (1.0));
154  clientApp.Stop (Seconds (simulationTime));
155 
156  //Populate routing table
158 
159  //Set simulation time and launch simulation
160  Simulator::Stop (Seconds (simulationTime));
161  Simulator::Run ();
163 
164  //Get and print results
165  uint32_t totalPacketsThrough = DynamicCast<UdpServer> (serverApp.Get (0))->GetReceived ();
166  double throughput = totalPacketsThrough * 1472 * 8 / ((simulationTime - 1) * 1000000.0); //Mbit/s
167  std::cout << "Throughput: " << throughput << " Mbit/s" << std::endl;
168 
169  return 0;
170 }
tuple channel
Definition: third.py:85
holds a vector of ns3::Application pointers.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
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.
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
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.
HT PHY for the 2.4 GHz band (clause 20)
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:213
tuple cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:44
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:1055
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
Create a server application which waits for input UDP packets and uses the information carried into t...
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:748
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
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.
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...
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 SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
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.
Time MicroSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1009
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...
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
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