A Discrete-Event Network Simulator
API
wifi-tcp.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, IMDEA Networks Institute
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Hany Assasa <hany.assasa@gmail.com>
18 .*
19  * This is a simple example to test TCP over 802.11n (with MPDU aggregation enabled).
20  *
21  * Network topology:
22  *
23  * Ap STA
24  * * *
25  * | |
26  * n1 n2
27  *
28  * In this example, an HT station sends TCP packets to the access point.
29  * We report the total throughput received during a window of 100ms.
30  * The user can specify the application data rate and choose the variant
31  * of TCP i.e. congestion control algorithm to use.
32  */
33 
34 #include "ns3/command-line.h"
35 #include "ns3/config.h"
36 #include "ns3/internet-stack-helper.h"
37 #include "ns3/ipv4-address-helper.h"
38 #include "ns3/ipv4-global-routing-helper.h"
39 #include "ns3/log.h"
40 #include "ns3/mobility-helper.h"
41 #include "ns3/mobility-model.h"
42 #include "ns3/on-off-helper.h"
43 #include "ns3/packet-sink-helper.h"
44 #include "ns3/packet-sink.h"
45 #include "ns3/ssid.h"
46 #include "ns3/string.h"
47 #include "ns3/tcp-westwood-plus.h"
48 #include "ns3/yans-wifi-channel.h"
49 #include "ns3/yans-wifi-helper.h"
50 
51 NS_LOG_COMPONENT_DEFINE("wifi-tcp");
52 
53 using namespace ns3;
54 
56 uint64_t lastTotalRx = 0;
57 
61 void
63 {
64  Time now = Simulator::Now(); /* Return the simulator's virtual time. */
65  double cur = (sink->GetTotalRx() - lastTotalRx) * 8.0 /
66  1e5; /* Convert Application RX Packets to MBits. */
67  std::cout << now.GetSeconds() << "s: \t" << cur << " Mbit/s" << std::endl;
70 }
71 
72 int
73 main(int argc, char* argv[])
74 {
75  uint32_t payloadSize = 1472; /* Transport layer payload size in bytes. */
76  std::string dataRate = "100Mbps"; /* Application layer datarate. */
77  std::string tcpVariant = "TcpNewReno"; /* TCP variant type. */
78  std::string phyRate = "HtMcs7"; /* Physical layer bitrate. */
79  double simulationTime = 10; /* Simulation time in seconds. */
80  bool pcapTracing = false; /* PCAP Tracing is enabled or not. */
81 
82  /* Command line argument parser setup. */
83  CommandLine cmd(__FILE__);
84  cmd.AddValue("payloadSize", "Payload size in bytes", payloadSize);
85  cmd.AddValue("dataRate", "Application data ate", dataRate);
86  cmd.AddValue("tcpVariant",
87  "Transport protocol to use: TcpNewReno, "
88  "TcpHybla, TcpHighSpeed, TcpHtcp, TcpVegas, TcpScalable, TcpVeno, "
89  "TcpBic, TcpYeah, TcpIllinois, TcpWestwood, TcpWestwoodPlus, TcpLedbat ",
90  tcpVariant);
91  cmd.AddValue("phyRate", "Physical layer bitrate", phyRate);
92  cmd.AddValue("simulationTime", "Simulation time in seconds", simulationTime);
93  cmd.AddValue("pcap", "Enable/disable PCAP Tracing", pcapTracing);
94  cmd.Parse(argc, argv);
95 
96  tcpVariant = std::string("ns3::") + tcpVariant;
97  // Select TCP variant
98  TypeId tcpTid;
100  "TypeId " << tcpVariant << " not found");
101  Config::SetDefault("ns3::TcpL4Protocol::SocketType",
102  TypeIdValue(TypeId::LookupByName(tcpVariant)));
103 
104  /* Configure TCP Options */
105  Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
106 
107  WifiMacHelper wifiMac;
108  WifiHelper wifiHelper;
109  wifiHelper.SetStandard(WIFI_STANDARD_80211n);
110 
111  /* Set up Legacy Channel */
112  YansWifiChannelHelper wifiChannel;
113  wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
114  wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel", "Frequency", DoubleValue(5e9));
115 
116  /* Setup Physical Layer */
117  YansWifiPhyHelper wifiPhy;
118  wifiPhy.SetChannel(wifiChannel.Create());
119  wifiPhy.SetErrorRateModel("ns3::YansErrorRateModel");
120  wifiHelper.SetRemoteStationManager("ns3::ConstantRateWifiManager",
121  "DataMode",
122  StringValue(phyRate),
123  "ControlMode",
124  StringValue("HtMcs0"));
125 
126  NodeContainer networkNodes;
127  networkNodes.Create(2);
128  Ptr<Node> apWifiNode = networkNodes.Get(0);
129  Ptr<Node> staWifiNode = networkNodes.Get(1);
130 
131  /* Configure AP */
132  Ssid ssid = Ssid("network");
133  wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
134 
135  NetDeviceContainer apDevice;
136  apDevice = wifiHelper.Install(wifiPhy, wifiMac, apWifiNode);
137 
138  /* Configure STA */
139  wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
140 
142  staDevices = wifiHelper.Install(wifiPhy, wifiMac, staWifiNode);
143 
144  /* Mobility model */
146  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
147  positionAlloc->Add(Vector(0.0, 0.0, 0.0));
148  positionAlloc->Add(Vector(1.0, 1.0, 0.0));
149 
150  mobility.SetPositionAllocator(positionAlloc);
151  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
152  mobility.Install(apWifiNode);
153  mobility.Install(staWifiNode);
154 
155  /* Internet stack */
157  stack.Install(networkNodes);
158 
160  address.SetBase("10.0.0.0", "255.255.255.0");
161  Ipv4InterfaceContainer apInterface;
162  apInterface = address.Assign(apDevice);
163  Ipv4InterfaceContainer staInterface;
164  staInterface = address.Assign(staDevices);
165 
166  /* Populate routing table */
168 
169  /* Install TCP Receiver on the access point */
170  PacketSinkHelper sinkHelper("ns3::TcpSocketFactory",
172  ApplicationContainer sinkApp = sinkHelper.Install(apWifiNode);
173  sink = StaticCast<PacketSink>(sinkApp.Get(0));
174 
175  /* Install TCP/UDP Transmitter on the station */
176  OnOffHelper server("ns3::TcpSocketFactory", (InetSocketAddress(apInterface.GetAddress(0), 9)));
177  server.SetAttribute("PacketSize", UintegerValue(payloadSize));
178  server.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
179  server.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
180  server.SetAttribute("DataRate", DataRateValue(DataRate(dataRate)));
181  ApplicationContainer serverApp = server.Install(staWifiNode);
182 
183  /* Start Applications */
184  sinkApp.Start(Seconds(0.0));
185  serverApp.Start(Seconds(1.0));
187 
188  /* Enable Traces */
189  if (pcapTracing)
190  {
192  wifiPhy.EnablePcap("AccessPoint", apDevice);
193  wifiPhy.EnablePcap("Station", staDevices);
194  }
195 
196  /* Start Simulation */
197  Simulator::Stop(Seconds(simulationTime + 1));
198  Simulator::Run();
199 
200  double averageThroughput = ((sink->GetTotalRx() * 8) / (1e6 * simulationTime));
201 
203 
204  if (averageThroughput < 50)
205  {
206  NS_LOG_ERROR("Obtained throughput is not in the expected boundaries!");
207  exit(1);
208  }
209  std::cout << "\nAverage throughput: " << averageThroughput << " Mbit/s" << std::endl;
210  return 0;
211 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:96
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:835
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition: type-id.cc:844
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
void SetRemoteStationManager(std::string type, Args &&... args)
Helper function used to set the station manager.
Definition: wifi-helper.h:604
virtual void SetStandard(WifiStandard standard)
Definition: wifi-helper.cc:738
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:756
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void SetPcapDataLinkType(SupportedPcapDataLinkTypes dlt)
Set the data link type of PCAP traces to be used.
Definition: wifi-helper.cc:543
void SetErrorRateModel(std::string type, Args &&... args)
Helper function used to set the error rate model.
Definition: wifi-helper.h:550
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
Definition: wifi-helper.h:178
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:890
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition: abort.h:144
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition: log.h:254
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
@ WIFI_STANDARD_80211n
address
Definition: first.py:47
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
staDevices
Definition: third.py:100
ssid
Definition: third.py:93
mobility
Definition: third.py:105
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55
void CalculateThroughput()
Calculate the throughput.
Definition: wifi-tcp.cc:62
uint64_t lastTotalRx
The value of the last total received bytes.
Definition: wifi-tcp.cc:56