A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-tcp.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, IMDEA Networks Institute
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Hany Assasa <hany.assasa@gmail.com>
7.*
8 * This is a simple example to test TCP over 802.11n (with MPDU aggregation enabled).
9 *
10 * Network topology:
11 *
12 * Ap STA
13 * * *
14 * | |
15 * n1 n2
16 *
17 * In this example, an HT station sends TCP packets to the access point.
18 * We report the total throughput received during a window of 100ms.
19 * The user can specify the application data rate and choose the variant
20 * of TCP i.e. congestion control algorithm to use.
21 */
22
23#include "ns3/command-line.h"
24#include "ns3/config.h"
25#include "ns3/internet-stack-helper.h"
26#include "ns3/ipv4-address-helper.h"
27#include "ns3/ipv4-global-routing-helper.h"
28#include "ns3/log.h"
29#include "ns3/mobility-helper.h"
30#include "ns3/mobility-model.h"
31#include "ns3/on-off-helper.h"
32#include "ns3/packet-sink-helper.h"
33#include "ns3/packet-sink.h"
34#include "ns3/ssid.h"
35#include "ns3/string.h"
36#include "ns3/tcp-westwood-plus.h"
37#include "ns3/yans-wifi-channel.h"
38#include "ns3/yans-wifi-helper.h"
39
40NS_LOG_COMPONENT_DEFINE("wifi-tcp");
41
42using namespace ns3;
43
44Ptr<PacketSink> sink; //!< Pointer to the packet sink application
45uint64_t lastTotalRx = 0; //!< The value of the last total received bytes
46
47/**
48 * Calculate the throughput
49 */
50void
52{
53 Time now = Simulator::Now(); /* Return the simulator's virtual time. */
54 double cur = (sink->GetTotalRx() - lastTotalRx) * 8.0 /
55 1e5; /* Convert Application RX Packets to MBits. */
56 std::cout << now.GetSeconds() << "s: \t" << cur << " Mbit/s" << std::endl;
57 lastTotalRx = sink->GetTotalRx();
59}
60
61int
62main(int argc, char* argv[])
63{
64 uint32_t payloadSize{1472}; /* Transport layer payload size in bytes. */
65 DataRate dataRate{"100Mb/s"}; /* Application layer datarate. */
66 std::string tcpVariant{"TcpNewReno"}; /* TCP variant type. */
67 std::string phyRate{"HtMcs7"}; /* Physical layer bitrate. */
68 Time simulationTime{"10s"}; /* Simulation time. */
69 bool pcapTracing{false}; /* PCAP Tracing is enabled or not. */
70
71 /* Command line argument parser setup. */
72 CommandLine cmd(__FILE__);
73 cmd.AddValue("payloadSize", "Payload size in bytes", payloadSize);
74 cmd.AddValue("dataRate", "Application data ate", dataRate);
75 cmd.AddValue("tcpVariant",
76 "Transport protocol to use: TcpNewReno, "
77 "TcpHybla, TcpHighSpeed, TcpHtcp, TcpVegas, TcpScalable, TcpVeno, "
78 "TcpBic, TcpYeah, TcpIllinois, TcpWestwood, TcpWestwoodPlus, TcpLedbat ",
79 tcpVariant);
80 cmd.AddValue("phyRate", "Physical layer bitrate", phyRate);
81 cmd.AddValue("simulationTime", "Simulation time in seconds", simulationTime);
82 cmd.AddValue("pcap", "Enable/disable PCAP Tracing", pcapTracing);
83 cmd.Parse(argc, argv);
84
85 tcpVariant = std::string("ns3::") + tcpVariant;
86 // Select TCP variant
87 TypeId tcpTid;
89 "TypeId " << tcpVariant << " not found");
90 Config::SetDefault("ns3::TcpL4Protocol::SocketType",
92
93 /* Configure TCP Options */
94 Config::SetDefault("ns3::TcpSocket::SegmentSize", UintegerValue(payloadSize));
95
96 WifiMacHelper wifiMac;
97 WifiHelper wifiHelper;
99
100 /* Set up Legacy Channel */
101 YansWifiChannelHelper wifiChannel;
102 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
103 wifiChannel.AddPropagationLoss("ns3::FriisPropagationLossModel", "Frequency", DoubleValue(5e9));
104
105 /* Setup Physical Layer */
106 YansWifiPhyHelper wifiPhy;
107 wifiPhy.SetChannel(wifiChannel.Create());
108 wifiPhy.SetErrorRateModel("ns3::YansErrorRateModel");
109 wifiHelper.SetRemoteStationManager("ns3::ConstantRateWifiManager",
110 "DataMode",
111 StringValue(phyRate),
112 "ControlMode",
113 StringValue("HtMcs0"));
114
115 NodeContainer networkNodes;
116 networkNodes.Create(2);
117 Ptr<Node> apWifiNode = networkNodes.Get(0);
118 Ptr<Node> staWifiNode = networkNodes.Get(1);
119
120 /* Configure AP */
121 Ssid ssid = Ssid("network");
122 wifiMac.SetType("ns3::ApWifiMac", "Ssid", SsidValue(ssid));
123
124 NetDeviceContainer apDevice;
125 apDevice = wifiHelper.Install(wifiPhy, wifiMac, apWifiNode);
126
127 /* Configure STA */
128 wifiMac.SetType("ns3::StaWifiMac", "Ssid", SsidValue(ssid));
129
131 staDevices = wifiHelper.Install(wifiPhy, wifiMac, staWifiNode);
132
133 /* Mobility model */
136 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
137 positionAlloc->Add(Vector(1.0, 1.0, 0.0));
138
139 mobility.SetPositionAllocator(positionAlloc);
140 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
141 mobility.Install(apWifiNode);
142 mobility.Install(staWifiNode);
143
144 /* Internet stack */
146 stack.Install(networkNodes);
147
149 address.SetBase("10.0.0.0", "255.255.255.0");
150 Ipv4InterfaceContainer apInterface;
151 apInterface = address.Assign(apDevice);
152 Ipv4InterfaceContainer staInterface;
153 staInterface = address.Assign(staDevices);
154
155 /* Populate routing table */
157
158 /* Install TCP Receiver on the access point */
159 PacketSinkHelper sinkHelper("ns3::TcpSocketFactory",
161 ApplicationContainer sinkApp = sinkHelper.Install(apWifiNode);
162 sink = StaticCast<PacketSink>(sinkApp.Get(0));
163
164 /* Install TCP/UDP Transmitter on the station */
165 OnOffHelper server("ns3::TcpSocketFactory", (InetSocketAddress(apInterface.GetAddress(0), 9)));
166 server.SetAttribute("PacketSize", UintegerValue(payloadSize));
167 server.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
168 server.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
169 server.SetAttribute("DataRate", DataRateValue(DataRate(dataRate)));
170 ApplicationContainer serverApp = server.Install(staWifiNode);
171
172 /* Start Applications */
173 sinkApp.Start(Seconds(0));
174 serverApp.Start(Seconds(1));
176
177 /* Enable Traces */
178 if (pcapTracing)
179 {
181 wifiPhy.EnablePcap("AccessPoint", apDevice);
182 wifiPhy.EnablePcap("Station", staDevices);
183 }
184
185 /* Start Simulation */
186 Simulator::Stop(simulationTime + Seconds(1));
188
189 auto averageThroughput =
190 (static_cast<double>(sink->GetTotalRx() * 8) / simulationTime.GetMicroSeconds());
191
193
194 if (averageThroughput < 50)
195 {
196 NS_LOG_ERROR("Obtained throughput is not in the expected boundaries!");
197 exit(1);
198 }
199 std::cout << "\nAverage throughput: " << averageThroughput << " Mbit/s" << std::endl;
200 return 0;
201}
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.
Class for representing data rates.
Definition data-rate.h:78
AttributeValue implementation for DataRate.
Definition data-rate.h:285
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
The IEEE 802.11 SSID Information Element.
Definition ssid.h:25
AttributeValue implementation for Ssid.
Definition ssid.h:85
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
static bool LookupByNameFailSafe(std::string name, TypeId *tid)
Get a TypeId by name.
Definition type-id.cc:886
AttributeValue implementation for TypeId.
Definition type-id.h:617
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
void SetRemoteStationManager(std::string type, Args &&... args)
Helper function used to set the station manager.
virtual void SetStandard(WifiStandard standard)
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
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.
void SetErrorRateModel(std::string type, Args &&... args)
Helper function used to set the error rate model.
@ DLT_IEEE802_11_RADIO
Include Radiotap link layer information.
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:883
#define NS_ABORT_MSG_UNLESS(cond, msg)
Abnormal program termination if a condition is false, with a message.
Definition abort.h:133
#define NS_LOG_ERROR(msg)
Use NS_LOG to output a message of level LOG_ERROR.
Definition log.h:243
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition nstime.h:1356
@ WIFI_STANDARD_80211n
address
Definition first.py:36
stack
Definition first.py:33
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > StaticCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:587
staDevices
Definition third.py:87
ssid
Definition third.py:82
mobility
Definition third.py:92
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44
void CalculateThroughput()
Calculate the throughput.
Definition wifi-tcp.cc:51
uint64_t lastTotalRx
The value of the last total received bytes.
Definition wifi-tcp.cc:45