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.h"
48#include "ns3/yans-wifi-channel.h"
49#include "ns3/yans-wifi-helper.h"
50
51NS_LOG_COMPONENT_DEFINE("wifi-tcp");
52
53using namespace ns3;
54
56uint64_t lastTotalRx = 0;
57
61void
63{
64 Time now = Simulator::Now(); /* Return the simulator's virtual time. */
65 double cur = (sink->GetTotalRx() - lastTotalRx) * (double)8 /
66 1e5; /* Convert Application RX Packets to MBits. */
67 std::cout << now.GetSeconds() << "s: \t" << cur << " Mbit/s" << std::endl;
69 Simulator::Schedule(MilliSeconds(100), &CalculateThroughput);
70}
71
72int
73main(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 if (tcpVariant == "ns3::TcpWestwoodPlus")
99 {
100 // TcpWestwoodPlus is not an actual TypeId name; we need TcpWestwood here
101 Config::SetDefault("ns3::TcpL4Protocol::SocketType", TypeIdValue(TcpWestwood::GetTypeId()));
102 // the default protocol type in ns3::TcpWestwood is WESTWOOD
103 Config::SetDefault("ns3::TcpWestwood::ProtocolType", EnumValue(TcpWestwood::WESTWOODPLUS));
104 }
105 else
106 {
107 TypeId tcpTid;
108 NS_ABORT_MSG_UNLESS(TypeId::LookupByNameFailSafe(tcpVariant, &tcpTid),
109 "TypeId " << tcpVariant << " not found");
110 Config::SetDefault("ns3::TcpL4Protocol::SocketType",
111 TypeIdValue(TypeId::LookupByName(tcpVariant)));
112 }
113
114 /* Configure TCP Options */
115 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
116
117 WifiMacHelper wifiMac;
118 WifiHelper wifiHelper;
120
121 /* Set up Legacy Channel */
122 YansWifiChannelHelper wifiChannel;
123 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
124 wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel", "Frequency", DoubleValue(5e9));
125
126 /* Setup Physical Layer */
127 YansWifiPhyHelper wifiPhy;
128 wifiPhy.SetChannel(wifiChannel.Create());
129 wifiPhy.SetErrorRateModel("ns3::YansErrorRateModel");
130 wifiHelper.SetRemoteStationManager("ns3::ConstantRateWifiManager",
131 "DataMode",
132 StringValue(phyRate),
133 "ControlMode",
134 StringValue("HtMcs0"));
135
136 NodeContainer networkNodes;
137 networkNodes.Create(2);
138 Ptr<Node> apWifiNode = networkNodes.Get(0);
139 Ptr<Node> staWifiNode = networkNodes.Get(1);
140
141 /* Configure AP */
142 Ssid ssid = Ssid("network");
143 wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
144
145 NetDeviceContainer apDevice;
146 apDevice = wifiHelper.Install(wifiPhy, wifiMac, apWifiNode);
147
148 /* Configure STA */
149 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
150
152 staDevices = wifiHelper.Install(wifiPhy, wifiMac, staWifiNode);
153
154 /* Mobility model */
156 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
157 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
158 positionAlloc->Add(Vector(1.0, 1.0, 0.0));
159
160 mobility.SetPositionAllocator(positionAlloc);
161 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
162 mobility.Install(apWifiNode);
163 mobility.Install(staWifiNode);
164
165 /* Internet stack */
167 stack.Install(networkNodes);
168
170 address.SetBase("10.0.0.0", "255.255.255.0");
171 Ipv4InterfaceContainer apInterface;
172 apInterface = address.Assign(apDevice);
173 Ipv4InterfaceContainer staInterface;
174 staInterface = address.Assign(staDevices);
175
176 /* Populate routing table */
177 Ipv4GlobalRoutingHelper::PopulateRoutingTables();
178
179 /* Install TCP Receiver on the access point */
180 PacketSinkHelper sinkHelper("ns3::TcpSocketFactory",
181 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));
196 Simulator::Schedule(Seconds(1.1), &CalculateThroughput);
197
198 /* Enable Traces */
199 if (pcapTracing)
200 {
201 wifiPhy.SetPcapDataLinkType(WifiPhyHelper::DLT_IEEE802_11_RADIO);
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
212 Simulator::Destroy();
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}
holds a vector of ns3::Application pointers.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
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:232
AttributeValue implementation for DataRate.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:56
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.
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.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
Hold variables of type string.
Definition: string.h:42
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:402
a unique identifier for an interface.
Definition: type-id.h:60
AttributeValue implementation for TypeId.
Definition: type-id.h:600
Hold an unsigned integer type.
Definition: uinteger.h:45
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create WifiNetDevice objects
Definition: wifi-helper.h:325
void SetRemoteStationManager(std::string type, Args &&... args)
Helper function used to set the station manager.
Definition: wifi-helper.h:590
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:536
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:891
#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:328
Time Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1338
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1350
@ WIFI_STANDARD_80211n
address
Definition: first.py:40
stack
Definition: first.py:37
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:33
staDevices
Definition: third.py:91
ssid
Definition: third.py:86
mobility
Definition: third.py:96
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55
void CalculateThroughput()
Calulate the throughput.
Definition: wifi-tcp.cc:62
uint64_t lastTotalRx
The value of the last total received bytes.
Definition: wifi-tcp.cc:56