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/wifi-module.h"
40 
41 NS_LOG_COMPONENT_DEFINE ("wifi-tcp");
42 
43 using namespace ns3;
44 
45 Ptr<PacketSink> sink; /* Pointer to the packet sink application */
46 uint64_t lastTotalRx = 0; /* The value of the last total received bytes */
47 
48 void
50 {
51  Time now = Simulator::Now (); /* Return the simulator's virtual time. */
52  double cur = (sink->GetTotalRx () - lastTotalRx) * (double) 8 / 1e5; /* Convert Application RX Packets to MBits. */
53  std::cout << now.GetSeconds () << "s: \t" << cur << " Mbit/s" << std::endl;
54  lastTotalRx = sink->GetTotalRx ();
56 }
57 
58 int
59 main (int argc, char *argv[])
60 {
61  uint32_t payloadSize = 1472; /* Transport layer payload size in bytes. */
62  std::string dataRate = "100Mbps"; /* Application layer datarate. */
63  std::string tcpVariant = "TcpNewReno"; /* TCP variant type. */
64  std::string phyRate = "HtMcs7"; /* Physical layer bitrate. */
65  double simulationTime = 10; /* Simulation time in seconds. */
66  bool pcapTracing = false; /* PCAP Tracing is enabled or not. */
67 
68  /* Command line argument parser setup. */
70  cmd.AddValue ("payloadSize", "Payload size in bytes", payloadSize);
71  cmd.AddValue ("dataRate", "Application data ate", dataRate);
72  cmd.AddValue ("tcpVariant", "Transport protocol to use: TcpNewReno, "
73  "TcpHybla, TcpHighSpeed, TcpHtcp, TcpVegas, TcpScalable, TcpVeno, "
74  "TcpBic, TcpYeah, TcpIllinois, TcpWestwood, TcpWestwoodPlus, TcpLedbat ", 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  tcpVariant = std::string ("ns3::") + tcpVariant;
81 
82  /* No fragmentation and no RTS/CTS */
83  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("999999"));
84  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("999999"));
85 
86  // Select TCP variant
87  if (tcpVariant.compare ("ns3::TcpWestwoodPlus") == 0)
88  {
89  // TcpWestwoodPlus is not an actual TypeId name; we need TcpWestwood here
90  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
91  // the default protocol type in ns3::TcpWestwood is WESTWOOD
92  Config::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));
93  }
94  else
95  {
96  TypeId tcpTid;
97  NS_ABORT_MSG_UNLESS (TypeId::LookupByNameFailSafe (tcpVariant, &tcpTid), "TypeId " << tcpVariant << " not found");
98  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TypeId::LookupByName (tcpVariant)));
99  }
100 
101  /* Configure TCP Options */
102  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
103 
104  WifiMacHelper wifiMac;
105  WifiHelper wifiHelper;
107 
108  /* Set up Legacy Channel */
109  YansWifiChannelHelper wifiChannel;
110  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
111  wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel", "Frequency", DoubleValue (5e9));
112 
113  /* Setup Physical Layer */
115  wifiPhy.SetChannel (wifiChannel.Create ());
116  wifiPhy.Set ("TxPowerStart", DoubleValue (10.0));
117  wifiPhy.Set ("TxPowerEnd", DoubleValue (10.0));
118  wifiPhy.Set ("TxPowerLevels", UintegerValue (1));
119  wifiPhy.Set ("TxGain", DoubleValue (0));
120  wifiPhy.Set ("RxGain", DoubleValue (0));
121  wifiPhy.Set ("RxNoiseFigure", DoubleValue (10));
122  wifiPhy.Set ("CcaMode1Threshold", DoubleValue (-79));
123  wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (-79 + 3));
124  wifiPhy.SetErrorRateModel ("ns3::YansErrorRateModel");
125  wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
126  "DataMode", StringValue (phyRate),
127  "ControlMode", StringValue ("HtMcs0"));
128 
129  NodeContainer networkNodes;
130  networkNodes.Create (2);
131  Ptr<Node> apWifiNode = networkNodes.Get (0);
132  Ptr<Node> staWifiNode = networkNodes.Get (1);
133 
134  /* Configure AP */
135  Ssid ssid = Ssid ("network");
136  wifiMac.SetType ("ns3::ApWifiMac",
137  "Ssid", SsidValue (ssid));
138 
139  NetDeviceContainer apDevice;
140  apDevice = wifiHelper.Install (wifiPhy, wifiMac, apWifiNode);
141 
142  /* Configure STA */
143  wifiMac.SetType ("ns3::StaWifiMac",
144  "Ssid", SsidValue (ssid));
145 
147  staDevices = wifiHelper.Install (wifiPhy, wifiMac, staWifiNode);
148 
149  /* Mobility model */
151  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
152  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
153  positionAlloc->Add (Vector (1.0, 1.0, 0.0));
154 
155  mobility.SetPositionAllocator (positionAlloc);
156  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
157  mobility.Install (apWifiNode);
158  mobility.Install (staWifiNode);
159 
160  /* Internet stack */
162  stack.Install (networkNodes);
163 
165  address.SetBase ("10.0.0.0", "255.255.255.0");
166  Ipv4InterfaceContainer apInterface;
167  apInterface = address.Assign (apDevice);
168  Ipv4InterfaceContainer staInterface;
169  staInterface = address.Assign (staDevices);
170 
171  /* Populate routing table */
173 
174  /* Install TCP Receiver on the access point */
175  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));
176  ApplicationContainer sinkApp = sinkHelper.Install (apWifiNode);
177  sink = StaticCast<PacketSink> (sinkApp.Get (0));
178 
179  /* Install TCP/UDP Transmitter on the station */
180  OnOffHelper server ("ns3::TcpSocketFactory", (InetSocketAddress (apInterface.GetAddress (0), 9)));
181  server.SetAttribute ("PacketSize", UintegerValue (payloadSize));
182  server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
183  server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
184  server.SetAttribute ("DataRate", DataRateValue (DataRate (dataRate)));
185  ApplicationContainer serverApp = server.Install (staWifiNode);
186 
187  /* Start Applications */
188  sinkApp.Start (Seconds (0.0));
189  serverApp.Start (Seconds (1.0));
191 
192  /* Enable Traces */
193  if (pcapTracing)
194  {
196  wifiPhy.EnablePcap ("AccessPoint", apDevice);
197  wifiPhy.EnablePcap ("Station", staDevices);
198  }
199 
200  /* Start Simulation */
201  Simulator::Stop (Seconds (simulationTime + 1));
202  Simulator::Run ();
204 
205  double averageThroughput = ((sink->GetTotalRx () * 8) / (1e6 * simulationTime));
206  if (averageThroughput < 50)
207  {
208  NS_LOG_ERROR ("Obtained throughput is not in the expected boundaries!");
209  exit (1);
210  }
211  std::cout << "\nAverage throughput: " << averageThroughput << " Mbit/s" << std::endl;
212  return 0;
213 }
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:45
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:132
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 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.
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:226
#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:1001
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 bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:831
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 SetPcapDataLinkType(enum SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:566
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:46
tuple mobility
Definition: third.py:101
Hold variables of type enum.
Definition: enum.h:54
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1375
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
AttributeValue implementation for TypeId.
Definition: type-id.h:608
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
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
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:49
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...
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-westwood.cc:47
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:249
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:138
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.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
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 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.
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:253
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
a unique identifier for an interface.
Definition: type-id.h:58
Include Radiotap link layer information.
Definition: wifi-helper.h:111
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.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:823
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const