A Discrete-Event Network Simulator
API
wifi-tcp.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2015, IMDEA Networks Institute
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: Hany Assasa <hany.assasa@gmail.com>
19 .*
20  * This is a simple example to test TCP over 802.11n (with MPDU aggregation enabled).
21  *
22  * Network topology:
23  *
24  * Ap STA
25  * * *
26  * | |
27  * n1 n2
28  *
29  * In this example, an HT station sends TCP packets to the access point.
30  * We report the total throughput received during a window of 100ms.
31  * The user can specify the application data rate and choose the variant
32  * of TCP i.e. congestion control algorithm to use.
33  */
34 
35 #include "ns3/applications-module.h"
36 #include "ns3/core-module.h"
37 #include "ns3/internet-module.h"
38 #include "ns3/mobility-module.h"
39 #include "ns3/network-module.h"
40 #include "ns3/point-to-point-module.h"
41 #include "ns3/wifi-module.h"
42 
43 NS_LOG_COMPONENT_DEFINE ("wifi-tcp");
44 
45 using namespace ns3;
46 
47 Ptr<PacketSink> sink; /* Pointer to the packet sink application */
48 uint64_t lastTotalRx = 0; /* The value of the last total received bytes */
49 
50 void
52 {
53  Time now = Simulator::Now (); /* Return the simulator's virtual time. */
54  double cur = (sink->GetTotalRx() - lastTotalRx) * (double) 8/1e5; /* Convert Application RX Packets to MBits. */
55  std::cout << now.GetSeconds () << "s: \t" << cur << " Mbit/s" << std::endl;
56  lastTotalRx = sink->GetTotalRx ();
58 }
59 
60 int
61 main(int argc, char *argv[])
62 {
63  uint32_t payloadSize = 1472; /* Transport layer payload size in bytes. */
64  std::string dataRate = "100Mbps"; /* Application layer datarate. */
65  std::string tcpVariant = "ns3::TcpNewReno"; /* TCP variant type. */
66  std::string phyRate = "HtMcs7"; /* Physical layer bitrate. */
67  double simulationTime = 10; /* Simulation time in seconds. */
68  bool pcapTracing = false; /* PCAP Tracing is enabled or not. */
69 
70  /* Command line argument parser setup. */
72  cmd.AddValue ("payloadSize", "Payload size in bytes", payloadSize);
73  cmd.AddValue ("dataRate", "Application data ate", dataRate);
74  cmd.AddValue ("tcpVariant", "Transport protocol to use: TcpTahoe, TcpReno, TcpNewReno, TcpWestwood, TcpWestwoodPlus ", tcpVariant);
75  cmd.AddValue ("phyRate", "Physical layer bitrate", phyRate);
76  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
77  cmd.AddValue ("pcap", "Enable/disable PCAP Tracing", pcapTracing);
78  cmd.Parse (argc, argv);
79 
80  /* No fragmentation and no RTS/CTS */
81  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("999999"));
82  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("999999"));
83 
84  /* Configure TCP Options */
85  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
86 
87  WifiMacHelper wifiMac;
88  WifiHelper wifiHelper;
90 
91  /* Set up Legacy Channel */
92  YansWifiChannelHelper wifiChannel ;
93  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
94  wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel", "Frequency", DoubleValue (5e9));
95 
96  /* Setup Physical Layer */
98  wifiPhy.SetChannel (wifiChannel.Create ());
99  wifiPhy.Set ("TxPowerStart", DoubleValue (10.0));
100  wifiPhy.Set ("TxPowerEnd", DoubleValue (10.0));
101  wifiPhy.Set ("TxPowerLevels", UintegerValue (1));
102  wifiPhy.Set ("TxGain", DoubleValue (0));
103  wifiPhy.Set ("RxGain", DoubleValue (0));
104  wifiPhy.Set ("RxNoiseFigure", DoubleValue (10));
105  wifiPhy.Set ("CcaMode1Threshold", DoubleValue (-79));
106  wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (-79 + 3));
107  wifiPhy.SetErrorRateModel ("ns3::YansErrorRateModel");
108  wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
109  "DataMode", StringValue (phyRate),
110  "ControlMode", StringValue ("HtMcs0"));
111 
112  NodeContainer networkNodes;
113  networkNodes.Create (2);
114  Ptr<Node> apWifiNode = networkNodes.Get (0);
115  Ptr<Node> staWifiNode = networkNodes.Get (1);
116 
117  /* Configure AP */
118  Ssid ssid = Ssid ("network");
119  wifiMac.SetType ("ns3::ApWifiMac",
120  "Ssid", SsidValue (ssid));
121 
122  NetDeviceContainer apDevice;
123  apDevice = wifiHelper.Install (wifiPhy, wifiMac, apWifiNode);
124 
125  /* Configure STA */
126  wifiMac.SetType ("ns3::StaWifiMac",
127  "Ssid", SsidValue (ssid));
128 
130  staDevices = wifiHelper.Install (wifiPhy, wifiMac, staWifiNode);
131 
132  /* Mobility model */
134  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
135  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
136  positionAlloc->Add (Vector (1.0, 1.0, 0.0));
137 
138  mobility.SetPositionAllocator (positionAlloc);
139  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
140  mobility.Install (apWifiNode);
141  mobility.Install (staWifiNode);
142 
143  /* Internet stack */
145  stack.Install (networkNodes);
146 
148  address.SetBase ("10.0.0.0", "255.255.255.0");
149  Ipv4InterfaceContainer apInterface;
150  apInterface = address.Assign (apDevice);
151  Ipv4InterfaceContainer staInterface;
152  staInterface = address.Assign (staDevices);
153 
154  /* Populate routing table */
156 
157  /* Install TCP Receiver on the access point */
158  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));
159  ApplicationContainer sinkApp = sinkHelper.Install (apWifiNode);
160  sink = StaticCast<PacketSink> (sinkApp.Get (0));
161 
162  /* Install TCP/UDP Transmitter on the station */
163  OnOffHelper server ("ns3::TcpSocketFactory", (InetSocketAddress (apInterface.GetAddress (0), 9)));
164  server.SetAttribute ("PacketSize", UintegerValue (payloadSize));
165  server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
166  server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
167  server.SetAttribute ("DataRate", DataRateValue (DataRate (dataRate)));
168  ApplicationContainer serverApp = server.Install (staWifiNode);
169 
170  /* Start Applications */
171  sinkApp.Start (Seconds (0.0));
172  serverApp.Start (Seconds (1.0));
174 
175  /* Enable Traces */
176  if (pcapTracing)
177  {
179  wifiPhy.EnablePcap ("AccessPoint", apDevice);
180  wifiPhy.EnablePcap ("Station", staDevices);
181  }
182 
183  /* Start Simulation */
184  Simulator::Stop (Seconds (simulationTime + 1));
185  Simulator::Run ();
187 
188  double averageThroughput = ((sink->GetTotalRx() * 8) / (1e6 * simulationTime));
189  if (averageThroughput < 50)
190  {
191  NS_LOG_ERROR ("Obtained throughput is not in the expected boundaries!");
192  exit (1);
193  }
194  std::cout << "\nAverage throughtput: " << averageThroughput << " Mbit/s" << std::endl;
195  return 0;
196 }
void AddPropagationLoss(std::string name, 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())
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:47
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)
HT OFDM 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: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 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
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:903
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
void SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:530
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:712
Class for representing data rates.
Definition: data-rate.h:88
void SetChannel(Ptr< YansWifiChannel > channel)
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:341
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
uint64_t lastTotalRx
Definition: wifi-tcp.cc:48
tuple mobility
Definition: third.py:101
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
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
tuple staDevices
Definition: third.py:96
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
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 CalculateThroughput()
Definition: wifi-tcp.cc:51
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.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
void SetErrorRateModel(std::string name, 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:118
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.
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.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:220
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 SetPropagationDelay(std::string name, 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())
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
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
uint64_t GetTotalRx() const
Definition: packet-sink.cc:78
Include Radiotap link layer information.
Definition: wifi-helper.h:117
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
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