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.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 }
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
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:719
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:661
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:170
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:204
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1070
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:299
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:707
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:1389
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:134
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:193
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:513
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:148
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:178
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1062
AttributeValue implementation for Ssid.
Definition: ssid.h:110
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:810
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:256
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:178
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