A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-hidden-terminal.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 IITP RAS
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Pavel Boyko <boyko@iitp.ru>
7 *
8 * Classical hidden terminal problem and its RTS/CTS solution.
9 *
10 * Topology: [node 0] <-- -50 dB --> [node 1] <-- -50 dB --> [node 2]
11 *
12 * This example illustrates the use of
13 * - Wifi in ad-hoc mode
14 * - Matrix propagation loss model
15 * - Use of OnOffApplication to generate CBR stream
16 * - IP flow monitor
17 */
18
19#include "ns3/boolean.h"
20#include "ns3/command-line.h"
21#include "ns3/config.h"
22#include "ns3/constant-position-mobility-model.h"
23#include "ns3/flow-monitor-helper.h"
24#include "ns3/internet-stack-helper.h"
25#include "ns3/ipv4-address-helper.h"
26#include "ns3/ipv4-flow-classifier.h"
27#include "ns3/on-off-helper.h"
28#include "ns3/propagation-delay-model.h"
29#include "ns3/propagation-loss-model.h"
30#include "ns3/string.h"
31#include "ns3/udp-echo-helper.h"
32#include "ns3/uinteger.h"
33#include "ns3/yans-wifi-channel.h"
34#include "ns3/yans-wifi-helper.h"
35
36using namespace ns3;
37
38/**
39 * Run single 10 seconds experiment
40 *
41 * \param enableCtsRts if true, enable RTS/CTS for packets larger than 100 bytes.
42 * \param wifiManager WiFi manager to use.
43 */
44void
45experiment(bool enableCtsRts, std::string wifiManager)
46{
47 // 0. Enable or disable CTS/RTS
48 UintegerValue ctsThr = (enableCtsRts ? UintegerValue(100) : UintegerValue(2200));
49 Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr);
50
51 // 1. Create 3 nodes
53 nodes.Create(3);
54
55 // 2. Place nodes somehow, this is required by every wireless simulation
56 for (uint8_t i = 0; i < 3; ++i)
57 {
59 }
60
61 // 3. Create propagation loss matrix
63 lossModel->SetDefaultLoss(200); // set default loss to 200 dB (no link)
64 lossModel->SetLoss(nodes.Get(0)->GetObject<MobilityModel>(),
66 50); // set symmetric loss 0 <-> 1 to 50 dB
67 lossModel->SetLoss(nodes.Get(2)->GetObject<MobilityModel>(),
69 50); // set symmetric loss 2 <-> 1 to 50 dB
70
71 // 4. Create & setup wifi channel
73 wifiChannel->SetPropagationLossModel(lossModel);
74 wifiChannel->SetPropagationDelayModel(CreateObject<ConstantSpeedPropagationDelayModel>());
75
76 // 5. Install wireless devices
77 WifiHelper wifi;
78 wifi.SetStandard(WIFI_STANDARD_80211b);
79 wifi.SetRemoteStationManager("ns3::" + wifiManager + "WifiManager");
80 YansWifiPhyHelper wifiPhy;
81 wifiPhy.SetChannel(wifiChannel);
82 WifiMacHelper wifiMac;
83 wifiMac.SetType("ns3::AdhocWifiMac"); // use ad-hoc MAC
84 NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
85
86 // uncomment the following to have athstats output
87 // AthstatsHelper athstats;
88 // athstats.EnableAthstats(enableCtsRts ? "rtscts-athstats-node" : "basic-athstats-node" ,
89 // nodes);
90
91 // uncomment the following to have pcap output
92 // wifiPhy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
93 // wifiPhy.EnablePcap (enableCtsRts ? "rtscts-pcap-node" : "basic-pcap-node" , nodes);
94
95 // 6. Install TCP/IP stack & assign IP addresses
96 InternetStackHelper internet;
97 internet.Install(nodes);
99 ipv4.SetBase("10.0.0.0", "255.0.0.0");
100 ipv4.Assign(devices);
101
102 // 7. Install applications: two CBR streams each saturating the channel
103 ApplicationContainer cbrApps;
104 uint16_t cbrPort = 12345;
105 OnOffHelper onOffHelper("ns3::UdpSocketFactory",
106 InetSocketAddress(Ipv4Address("10.0.0.2"), cbrPort));
107 onOffHelper.SetAttribute("PacketSize", UintegerValue(1400));
108 onOffHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
109 onOffHelper.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
110
111 // flow 1: node 0 -> node 1
112 onOffHelper.SetAttribute("DataRate", StringValue("3000000bps"));
113 onOffHelper.SetAttribute("StartTime", TimeValue(Seconds(1.000000)));
114 cbrApps.Add(onOffHelper.Install(nodes.Get(0)));
115
116 // flow 2: node 2 -> node 1
117 /**
118 * \internal
119 * The slightly different start times and data rates are a workaround
120 * for \bugid{388} and \bugid{912}
121 */
122 onOffHelper.SetAttribute("DataRate", StringValue("3001100bps"));
123 onOffHelper.SetAttribute("StartTime", TimeValue(Seconds(1.001)));
124 cbrApps.Add(onOffHelper.Install(nodes.Get(2)));
125
126 /**
127 * \internal
128 * We also use separate UDP applications that will send a single
129 * packet before the CBR flows start.
130 * This is a workaround for the lack of perfect ARP, see \bugid{187}
131 */
132 uint16_t echoPort = 9;
133 UdpEchoClientHelper echoClientHelper(Ipv4Address("10.0.0.2"), echoPort);
134 echoClientHelper.SetAttribute("MaxPackets", UintegerValue(1));
135 echoClientHelper.SetAttribute("Interval", TimeValue(Seconds(0.1)));
136 echoClientHelper.SetAttribute("PacketSize", UintegerValue(10));
137 ApplicationContainer pingApps;
138
139 // again using different start times to workaround Bug 388 and Bug 912
140 echoClientHelper.SetAttribute("StartTime", TimeValue(Seconds(0.001)));
141 pingApps.Add(echoClientHelper.Install(nodes.Get(0)));
142 echoClientHelper.SetAttribute("StartTime", TimeValue(Seconds(0.006)));
143 pingApps.Add(echoClientHelper.Install(nodes.Get(2)));
144
145 // 8. Install FlowMonitor on all nodes
146 FlowMonitorHelper flowmon;
147 Ptr<FlowMonitor> monitor = flowmon.InstallAll();
148
149 // 9. Run simulation for 10 seconds
152
153 // 10. Print per flow statistics
154 monitor->CheckForLostPackets();
156 FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats();
157 for (auto i = stats.begin(); i != stats.end(); ++i)
158 {
159 // first 2 FlowIds are for ECHO apps, we don't want to display them
160 //
161 // Duration for throughput measurement is 9.0 seconds, since
162 // StartTime of the OnOffApplication is at about "second 1"
163 // and
164 // Simulator::Stops at "second 10".
165 if (i->first > 2)
166 {
167 Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(i->first);
168 std::cout << "Flow " << i->first - 2 << " (" << t.sourceAddress << " -> "
169 << t.destinationAddress << ")\n";
170 std::cout << " Tx Packets: " << i->second.txPackets << "\n";
171 std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
172 std::cout << " TxOffered: " << i->second.txBytes * 8.0 / 9.0 / 1000 / 1000
173 << " Mbps\n";
174 std::cout << " Rx Packets: " << i->second.rxPackets << "\n";
175 std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
176 std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 9.0 / 1000 / 1000
177 << " Mbps\n";
178 }
179 }
180
181 // 11. Cleanup
183}
184
185int
186main(int argc, char** argv)
187{
188 std::string wifiManager("Arf");
189 CommandLine cmd(__FILE__);
190 cmd.AddValue(
191 "wifiManager",
192 "Set wifi rate manager (Aarf, Aarfcd, Amrr, Arf, Cara, Ideal, Minstrel, Onoe, Rraa)",
193 wifiManager);
194 cmd.Parse(argc, argv);
195
196 std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush;
197 experiment(false, wifiManager);
198 std::cout << "------------------------------------------------\n";
199 std::cout << "Hidden station experiment with RTS/CTS enabled:\n";
200 experiment(true, wifiManager);
201
202 return 0;
203}
holds a vector of ns3::Application pointers.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
void SetAttribute(const std::string &name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
Parse command-line arguments.
Helper to enable IP flow monitoring on a set of Nodes.
Ptr< FlowClassifier > GetClassifier()
Retrieve the FlowClassifier object for IPv4 created by the Install* methods.
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
std::map< FlowId, FlowStats > FlowStatsContainer
Container: FlowId, FlowStats.
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.
Ipv4 addresses are stored in host order in this class.
Keep track of the current position and velocity of an object.
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition object.h:511
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition object.cc:298
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
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
Hold variables of type string.
Definition string.h:45
Create an application which sends a UDP packet and waits for an echo of this packet.
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
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
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:1308
@ WIFI_STANDARD_80211b
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< T1 > DynamicCast(const Ptr< T2 > &p)
Cast a Ptr.
Definition ptr.h:580
Structure to classify a packet.
Ipv4Address sourceAddress
Source address.
Ipv4Address destinationAddress
Destination address.
void experiment(bool enableCtsRts, std::string wifiManager)
Run single 10 seconds experiment.