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. */
80  CommandLine cmd (__FILE__);
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 */
120  YansWifiPhyHelper wifiPhy;
121  wifiPhy.SetChannel (wifiChannel.Create ());
122  wifiPhy.SetErrorRateModel ("ns3::YansErrorRateModel");
123  wifiHelper.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
124  "DataMode", StringValue (phyRate),
125  "ControlMode", StringValue ("HtMcs0"));
126 
127  NodeContainer networkNodes;
128  networkNodes.Create (2);
129  Ptr<Node> apWifiNode = networkNodes.Get (0);
130  Ptr<Node> staWifiNode = networkNodes.Get (1);
131 
132  /* Configure AP */
133  Ssid ssid = Ssid ("network");
134  wifiMac.SetType ("ns3::ApWifiMac",
135  "Ssid", SsidValue (ssid));
136 
137  NetDeviceContainer apDevice;
138  apDevice = wifiHelper.Install (wifiPhy, wifiMac, apWifiNode);
139 
140  /* Configure STA */
141  wifiMac.SetType ("ns3::StaWifiMac",
142  "Ssid", SsidValue (ssid));
143 
145  staDevices = wifiHelper.Install (wifiPhy, wifiMac, staWifiNode);
146 
147  /* Mobility model */
149  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
150  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
151  positionAlloc->Add (Vector (1.0, 1.0, 0.0));
152 
153  mobility.SetPositionAllocator (positionAlloc);
154  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
155  mobility.Install (apWifiNode);
156  mobility.Install (staWifiNode);
157 
158  /* Internet stack */
160  stack.Install (networkNodes);
161 
163  address.SetBase ("10.0.0.0", "255.255.255.0");
164  Ipv4InterfaceContainer apInterface;
165  apInterface = address.Assign (apDevice);
166  Ipv4InterfaceContainer staInterface;
167  staInterface = address.Assign (staDevices);
168 
169  /* Populate routing table */
171 
172  /* Install TCP Receiver on the access point */
173  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9));
174  ApplicationContainer sinkApp = sinkHelper.Install (apWifiNode);
175  sink = StaticCast<PacketSink> (sinkApp.Get (0));
176 
177  /* Install TCP/UDP Transmitter on the station */
178  OnOffHelper server ("ns3::TcpSocketFactory", (InetSocketAddress (apInterface.GetAddress (0), 9)));
179  server.SetAttribute ("PacketSize", UintegerValue (payloadSize));
180  server.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
181  server.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
182  server.SetAttribute ("DataRate", DataRateValue (DataRate (dataRate)));
183  ApplicationContainer serverApp = server.Install (staWifiNode);
184 
185  /* Start Applications */
186  sinkApp.Start (Seconds (0.0));
187  serverApp.Start (Seconds (1.0));
189 
190  /* Enable Traces */
191  if (pcapTracing)
192  {
194  wifiPhy.EnablePcap ("AccessPoint", apDevice);
195  wifiPhy.EnablePcap ("Station", staDevices);
196  }
197 
198  /* Start Simulation */
199  Simulator::Stop (Seconds (simulationTime + 1));
200  Simulator::Run ();
201 
202  double averageThroughput = ((sink->GetTotalRx () * 8) / (1e6 * simulationTime));
203 
205 
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 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::WIFI_STANDARD_80211n_5GHZ
@ WIFI_STANDARD_80211n_5GHZ
Definition: wifi-standards.h:132
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::InetSocketAddress
an Inet address class
Definition: inet-socket-address.h:41
ns3::DataRateValue
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
ns3::ApplicationContainer::Get
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
Definition: application-container.cc:62
ns3::YansWifiPhyHelper
Make it easy to create and manage PHY objects for the YANS model.
Definition: yans-wifi-helper.h:161
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::ListPositionAllocator::Add
void Add(Vector v)
Add a position to the list of positions.
Definition: position-allocator.cc:70
third.staDevices
staDevices
Definition: third.py:103
ns3::YansWifiChannelHelper::SetPropagationDelay
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())
Definition: yans-wifi-helper.cc:75
ns3::PcapHelperForDevice::EnablePcap
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Definition: trace-helper.cc:401
ns3::Simulator::Now
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::WifiHelper
helps to create WifiNetDevice objects
Definition: wifi-helper.h:323
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::YansWifiChannelHelper::AddPropagationLoss
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())
Definition: yans-wifi-helper.cc:51
ns3::WifiPhyHelper::DLT_IEEE802_11_RADIO
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:176
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
ns3::WifiPhyHelper::SetErrorRateModel
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:146
ns3::YansWifiPhyHelper::SetChannel
void SetChannel(Ptr< YansWifiChannel > channel)
Definition: yans-wifi-helper.cc:135
ns3::DoubleValue
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
ns3::NodeContainer::Create
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Definition: node-container.cc:98
ns3::EnumValue
Hold variables of type enum.
Definition: enum.h:55
ns3::SsidValue
AttributeValue implementation for Ssid.
Definition: ssid.h:105
ns3::Ssid
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
ns3::Ptr< PacketSink >
ns3::DataRate
Class for representing data rates.
Definition: data-rate.h:89
ns3::TypeIdValue
AttributeValue implementation for TypeId.
Definition: type-id.h:595
ns3::YansWifiChannelHelper::Create
Ptr< YansWifiChannel > Create(void) const
Definition: yans-wifi-helper.cc:99
ns3::Ipv4GlobalRoutingHelper::PopulateRoutingTables
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Definition: ipv4-global-routing-helper.cc:61
first.stack
stack
Definition: first.py:41
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::OnOffHelper
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
ns3::PacketSink::GetTotalRx
uint64_t GetTotalRx() const
Definition: packet-sink.cc:93
ns3::MilliSeconds
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1297
ns3::TcpWestwood::WESTWOODPLUS
@ WESTWOODPLUS
Definition: tcp-westwood.h:90
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
ns3::NodeContainer::Get
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Definition: node-container.cc:93
first.address
address
Definition: first.py:44
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
second.cmd
cmd
Definition: second.py:35
lastTotalRx
uint64_t lastTotalRx
Definition: wifi-tcp.cc:57
sink
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
ns3::TypeId::LookupByNameFailSafe
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:838
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::WifiHelper::SetRemoteStationManager
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:721
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
ns3::PacketSinkHelper
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Definition: packet-sink-helper.h:36
NS_LOG_ERROR
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:257
CalculateThroughput
void CalculateThroughput()
Definition: wifi-tcp.cc:60
NS_ABORT_MSG_UNLESS
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
ns3::WifiPhyHelper::SetPcapDataLinkType
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:573
ns3::Ipv4Address::GetAny
static Ipv4Address GetAny(void)
Definition: ipv4-address.cc:395
ns3::WifiHelper::SetStandard
virtual void SetStandard(WifiStandard standard)
Definition: wifi-helper.cc:767
ns3::ApplicationContainer::Start
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
Definition: application-container.cc:87
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::ApplicationContainer
holds a vector of ns3::Application pointers.
Definition: application-container.h:43
third.ssid
ssid
Definition: third.py:100
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::WifiMacHelper::SetType
void SetType(std::string type, Args &&... args)
Definition: wifi-mac-helper.h:130
ns3::TcpWestwood::GetTypeId
static TypeId GetTypeId(void)
Get the type ID.
Definition: tcp-westwood.cc:47
ns3::Ipv4InterfaceContainer::GetAddress
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Definition: ipv4-interface-container.cc:59
ns3::UintegerValue
Hold an unsigned integer type.
Definition: uinteger.h:44
ns3::YansWifiChannelHelper
manage and create wifi channel objects for the YANS model.
Definition: yans-wifi-helper.h:37
ns3::WifiMacHelper
create MAC layers for a ns3::WifiNetDevice.
Definition: wifi-mac-helper.h:48
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::TypeId::LookupByName
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
ns3::MobilityHelper
Helper class used to assign positions and mobility models to nodes.
Definition: mobility-helper.h:43
ns3::Time::GetSeconds
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:380
third.mobility
mobility
Definition: third.py:108
ns3::WifiHelper::Install
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:779