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/command-line.h"
36 #include "ns3/config.h"
37 #include "ns3/string.h"
38 #include "ns3/log.h"
39 #include "ns3/yans-wifi-helper.h"
40 #include "ns3/ssid.h"
41 #include "ns3/mobility-helper.h"
42 #include "ns3/on-off-helper.h"
43 #include "ns3/yans-wifi-channel.h"
44 #include "ns3/mobility-model.h"
45 #include "ns3/packet-sink.h"
46 #include "ns3/packet-sink-helper.h"
47 #include "ns3/tcp-westwood.h"
48 #include "ns3/internet-stack-helper.h"
49 #include "ns3/ipv4-address-helper.h"
50 #include "ns3/ipv4-global-routing-helper.h"
51 
52 NS_LOG_COMPONENT_DEFINE ("wifi-tcp");
53 
54 using namespace ns3;
55 
56 Ptr<PacketSink> sink; /* Pointer to the packet sink application */
57 uint64_t lastTotalRx = 0; /* The value of the last total received bytes */
58 
59 void
61 {
62  Time now = Simulator::Now (); /* Return the simulator's virtual time. */
63  double cur = (sink->GetTotalRx () - lastTotalRx) * (double) 8 / 1e5; /* Convert Application RX Packets to MBits. */
64  std::cout << now.GetSeconds () << "s: \t" << cur << " Mbit/s" << std::endl;
67 }
68 
69 int
70 main (int argc, char *argv[])
71 {
72  uint32_t payloadSize = 1472; /* Transport layer payload size in bytes. */
73  std::string dataRate = "100Mbps"; /* Application layer datarate. */
74  std::string tcpVariant = "TcpNewReno"; /* TCP variant type. */
75  std::string phyRate = "HtMcs7"; /* Physical layer bitrate. */
76  double simulationTime = 10; /* Simulation time in seconds. */
77  bool pcapTracing = false; /* PCAP Tracing is enabled or not. */
78 
79  /* Command line argument parser setup. */
81  cmd.AddValue ("payloadSize", "Payload size in bytes", payloadSize);
82  cmd.AddValue ("dataRate", "Application data ate", dataRate);
83  cmd.AddValue ("tcpVariant", "Transport protocol to use: TcpNewReno, "
84  "TcpHybla, TcpHighSpeed, TcpHtcp, TcpVegas, TcpScalable, TcpVeno, "
85  "TcpBic, TcpYeah, TcpIllinois, TcpWestwood, TcpWestwoodPlus, TcpLedbat ", tcpVariant);
86  cmd.AddValue ("phyRate", "Physical layer bitrate", phyRate);
87  cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
88  cmd.AddValue ("pcap", "Enable/disable PCAP Tracing", pcapTracing);
89  cmd.Parse (argc, argv);
90 
91  tcpVariant = std::string ("ns3::") + tcpVariant;
92  // Select TCP variant
93  if (tcpVariant.compare ("ns3::TcpWestwoodPlus") == 0)
94  {
95  // TcpWestwoodPlus is not an actual TypeId name; we need TcpWestwood here
96  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TcpWestwood::GetTypeId ()));
97  // the default protocol type in ns3::TcpWestwood is WESTWOOD
98  Config::SetDefault ("ns3::TcpWestwood::ProtocolType", EnumValue (TcpWestwood::WESTWOODPLUS));
99  }
100  else
101  {
102  TypeId tcpTid;
103  NS_ABORT_MSG_UNLESS (TypeId::LookupByNameFailSafe (tcpVariant, &tcpTid), "TypeId " << tcpVariant << " not found");
104  Config::SetDefault ("ns3::TcpL4Protocol::SocketType", TypeIdValue (TypeId::LookupByName (tcpVariant)));
105  }
106 
107  /* Configure TCP Options */
108  Config::SetDefault ("ns3::TcpSocket::SegmentSize", UintegerValue (payloadSize));
109 
110  WifiMacHelper wifiMac;
111  WifiHelper wifiHelper;
113 
114  /* Set up Legacy Channel */
115  YansWifiChannelHelper wifiChannel;
116  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
117  wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel", "Frequency", DoubleValue (5e9));
118 
119  /* Setup Physical Layer */
121  wifiPhy.SetChannel (wifiChannel.Create ());
122  wifiPhy.Set ("TxPowerStart", DoubleValue (10.0));
123  wifiPhy.Set ("TxPowerEnd", DoubleValue (10.0));
124  wifiPhy.Set ("TxPowerLevels", UintegerValue (1));
125  wifiPhy.Set ("TxGain", DoubleValue (0));
126  wifiPhy.Set ("RxGain", DoubleValue (0));
127  wifiPhy.Set ("RxNoiseFigure", DoubleValue (10));
128  wifiPhy.Set ("CcaMode1Threshold", DoubleValue (-79));
129  wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (-79 + 3));
130  wifiPhy.SetErrorRateModel ("ns3::YansErrorRateModel");
131  wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
132  "DataMode", StringValue (phyRate),
133  "ControlMode", StringValue ("HtMcs0"));
134 
135  NodeContainer networkNodes;
136  networkNodes.Create (2);
137  Ptr<Node> apWifiNode = networkNodes.Get (0);
138  Ptr<Node> staWifiNode = networkNodes.Get (1);
139 
140  /* Configure AP */
141  Ssid ssid = Ssid ("network");
142  wifiMac.SetType ("ns3::ApWifiMac",
143  "Ssid", SsidValue (ssid));
144 
145  NetDeviceContainer apDevice;
146  apDevice = wifiHelper.Install (wifiPhy, wifiMac, apWifiNode);
147 
148  /* Configure STA */
149  wifiMac.SetType ("ns3::StaWifiMac",
150  "Ssid", SsidValue (ssid));
151 
153  staDevices = wifiHelper.Install (wifiPhy, wifiMac, staWifiNode);
154 
155  /* Mobility model */
157  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
158  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
159  positionAlloc->Add (Vector (1.0, 1.0, 0.0));
160 
161  mobility.SetPositionAllocator (positionAlloc);
162  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
163  mobility.Install (apWifiNode);
164  mobility.Install (staWifiNode);
165 
166  /* Internet stack */
168  stack.Install (networkNodes);
169 
171  address.SetBase ("10.0.0.0", "255.255.255.0");
172  Ipv4InterfaceContainer apInterface;
173  apInterface = address.Assign (apDevice);
174  Ipv4InterfaceContainer staInterface;
175  staInterface = address.Assign (staDevices);
176 
177  /* Populate routing table */
179 
180  /* Install TCP Receiver on the access point */
181  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));
182  ApplicationContainer sinkApp = sinkHelper.Install (apWifiNode);
183  sink = StaticCast<PacketSink> (sinkApp.Get (0));
184 
185  /* Install TCP/UDP Transmitter on the station */
186  OnOffHelper server ("ns3::TcpSocketFactory", (InetSocketAddress (apInterface.GetAddress (0), 9)));
187  server.SetAttribute ("PacketSize", UintegerValue (payloadSize));
188  server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
189  server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
190  server.SetAttribute ("DataRate", DataRateValue (DataRate (dataRate)));
191  ApplicationContainer serverApp = server.Install (staWifiNode);
192 
193  /* Start Applications */
194  sinkApp.Start (Seconds (0.0));
195  serverApp.Start (Seconds (1.0));
197 
198  /* Enable Traces */
199  if (pcapTracing)
200  {
202  wifiPhy.EnablePcap ("AccessPoint", apDevice);
203  wifiPhy.EnablePcap ("Station", staDevices);
204  }
205 
206  /* Start Simulation */
207  Simulator::Stop (Seconds (simulationTime + 1));
208  Simulator::Run ();
209 
210  double averageThroughput = ((sink->GetTotalRx () * 8) / (1e6 * simulationTime));
211 
213 
214  if (averageThroughput < 50)
215  {
216  NS_LOG_ERROR ("Obtained throughput is not in the expected boundaries!");
217  exit (1);
218  }
219  std::cout << "\nAverage throughput: " << averageThroughput << " Mbit/s" << std::endl;
220  return 0;
221 }
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:56
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:134
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)
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:629
holds a vector of std::pair of Ptr<Ipv4> and interface index.
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:600
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.
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
uint64_t GetTotalRx() const
Definition: packet-sink.cc:81
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:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
aggregate IP/TCP/UDP functionality to existing Nodes.
staDevices
Definition: third.py:96
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:832
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:230
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
stack
Definition: first.py:34
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:101
Class for representing data rates.
Definition: data-rate.h:88
virtual void SetStandard(WifiPhyStandard standard)
Definition: wifi-helper.cc:623
void SetChannel(Ptr< YansWifiChannel > channel)
uint64_t lastTotalRx
Definition: wifi-tcp.cc:57
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:1381
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
ssid
Definition: third.py:93
holds a vector of ns3::NetDevice pointers
AttributeValue implementation for TypeId.
Definition: type-id.h:608
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:213
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.
address
Definition: first.py:37
void CalculateThroughput()
Definition: wifi-tcp.cc:60
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-westwood.cc:47
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 SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:453
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:140
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.
#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
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
AttributeValue implementation for Ssid.
Definition: ssid.h:110
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
void Add(Vector v)
Add a position to the list of positions.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
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:254
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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.
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
a unique identifier for an interface.
Definition: type-id.h:58
Include Radiotap link layer information.
Definition: wifi-helper.h:111
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:824