A Discrete-Event Network Simulator
API
wifi-hidden-terminal.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010 IITP RAS
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: Pavel Boyko <boyko@iitp.ru>
18  *
19  * Classical hidden terminal problem and its RTS/CTS solution.
20  *
21  * Topology: [node 0] <-- -50 dB --> [node 1] <-- -50 dB --> [node 2]
22  *
23  * This example illustrates the use of
24  * - Wifi in ad-hoc mode
25  * - Matrix propagation loss model
26  * - Use of OnOffApplication to generate CBR stream
27  * - IP flow monitor
28  */
29 
30 #include "ns3/boolean.h"
31 #include "ns3/command-line.h"
32 #include "ns3/config.h"
33 #include "ns3/constant-position-mobility-model.h"
34 #include "ns3/flow-monitor-helper.h"
35 #include "ns3/internet-stack-helper.h"
36 #include "ns3/ipv4-address-helper.h"
37 #include "ns3/ipv4-flow-classifier.h"
38 #include "ns3/on-off-helper.h"
39 #include "ns3/propagation-delay-model.h"
40 #include "ns3/propagation-loss-model.h"
41 #include "ns3/string.h"
42 #include "ns3/udp-echo-helper.h"
43 #include "ns3/uinteger.h"
44 #include "ns3/yans-wifi-channel.h"
45 #include "ns3/yans-wifi-helper.h"
46 
47 using namespace ns3;
48 
55 void
56 experiment(bool enableCtsRts, std::string wifiManager)
57 {
58  // 0. Enable or disable CTS/RTS
59  UintegerValue ctsThr = (enableCtsRts ? UintegerValue(100) : UintegerValue(2200));
60  Config::SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr);
61 
62  // 1. Create 3 nodes
64  nodes.Create(3);
65 
66  // 2. Place nodes somehow, this is required by every wireless simulation
67  for (uint8_t i = 0; i < 3; ++i)
68  {
69  nodes.Get(i)->AggregateObject(CreateObject<ConstantPositionMobilityModel>());
70  }
71 
72  // 3. Create propagation loss matrix
73  Ptr<MatrixPropagationLossModel> lossModel = CreateObject<MatrixPropagationLossModel>();
74  lossModel->SetDefaultLoss(200); // set default loss to 200 dB (no link)
75  lossModel->SetLoss(nodes.Get(0)->GetObject<MobilityModel>(),
77  50); // set symmetric loss 0 <-> 1 to 50 dB
78  lossModel->SetLoss(nodes.Get(2)->GetObject<MobilityModel>(),
80  50); // set symmetric loss 2 <-> 1 to 50 dB
81 
82  // 4. Create & setup wifi channel
83  Ptr<YansWifiChannel> wifiChannel = CreateObject<YansWifiChannel>();
84  wifiChannel->SetPropagationLossModel(lossModel);
85  wifiChannel->SetPropagationDelayModel(CreateObject<ConstantSpeedPropagationDelayModel>());
86 
87  // 5. Install wireless devices
89  wifi.SetStandard(WIFI_STANDARD_80211b);
90  wifi.SetRemoteStationManager("ns3::" + wifiManager + "WifiManager");
91  YansWifiPhyHelper wifiPhy;
92  wifiPhy.SetChannel(wifiChannel);
93  WifiMacHelper wifiMac;
94  wifiMac.SetType("ns3::AdhocWifiMac"); // use ad-hoc MAC
95  NetDeviceContainer devices = wifi.Install(wifiPhy, wifiMac, nodes);
96 
97  // uncomment the following to have athstats output
98  // AthstatsHelper athstats;
99  // athstats.EnableAthstats(enableCtsRts ? "rtscts-athstats-node" : "basic-athstats-node" ,
100  // nodes);
101 
102  // uncomment the following to have pcap output
103  // wifiPhy.SetPcapDataLinkType (WifiPhyHelper::DLT_IEEE802_11_RADIO);
104  // wifiPhy.EnablePcap (enableCtsRts ? "rtscts-pcap-node" : "basic-pcap-node" , nodes);
105 
106  // 6. Install TCP/IP stack & assign IP addresses
108  internet.Install(nodes);
110  ipv4.SetBase("10.0.0.0", "255.0.0.0");
111  ipv4.Assign(devices);
112 
113  // 7. Install applications: two CBR streams each saturating the channel
114  ApplicationContainer cbrApps;
115  uint16_t cbrPort = 12345;
116  OnOffHelper onOffHelper("ns3::UdpSocketFactory",
117  InetSocketAddress(Ipv4Address("10.0.0.2"), cbrPort));
118  onOffHelper.SetAttribute("PacketSize", UintegerValue(1400));
119  onOffHelper.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
120  onOffHelper.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
121 
122  // flow 1: node 0 -> node 1
123  onOffHelper.SetAttribute("DataRate", StringValue("3000000bps"));
124  onOffHelper.SetAttribute("StartTime", TimeValue(Seconds(1.000000)));
125  cbrApps.Add(onOffHelper.Install(nodes.Get(0)));
126 
127  // flow 2: node 2 -> node 1
132  onOffHelper.SetAttribute("DataRate", StringValue("3001100bps"));
133  onOffHelper.SetAttribute("StartTime", TimeValue(Seconds(1.001)));
134  cbrApps.Add(onOffHelper.Install(nodes.Get(2)));
135 
141  uint16_t echoPort = 9;
142  UdpEchoClientHelper echoClientHelper(Ipv4Address("10.0.0.2"), echoPort);
143  echoClientHelper.SetAttribute("MaxPackets", UintegerValue(1));
144  echoClientHelper.SetAttribute("Interval", TimeValue(Seconds(0.1)));
145  echoClientHelper.SetAttribute("PacketSize", UintegerValue(10));
146  ApplicationContainer pingApps;
147 
148  // again using different start times to workaround Bug 388 and Bug 912
149  echoClientHelper.SetAttribute("StartTime", TimeValue(Seconds(0.001)));
150  pingApps.Add(echoClientHelper.Install(nodes.Get(0)));
151  echoClientHelper.SetAttribute("StartTime", TimeValue(Seconds(0.006)));
152  pingApps.Add(echoClientHelper.Install(nodes.Get(2)));
153 
154  // 8. Install FlowMonitor on all nodes
155  FlowMonitorHelper flowmon;
156  Ptr<FlowMonitor> monitor = flowmon.InstallAll();
157 
158  // 9. Run simulation for 10 seconds
160  Simulator::Run();
161 
162  // 10. Print per flow statistics
163  monitor->CheckForLostPackets();
164  Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>(flowmon.GetClassifier());
165  FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats();
166  for (auto i = stats.begin(); i != stats.end(); ++i)
167  {
168  // first 2 FlowIds are for ECHO apps, we don't want to display them
169  //
170  // Duration for throughput measurement is 9.0 seconds, since
171  // StartTime of the OnOffApplication is at about "second 1"
172  // and
173  // Simulator::Stops at "second 10".
174  if (i->first > 2)
175  {
176  Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow(i->first);
177  std::cout << "Flow " << i->first - 2 << " (" << t.sourceAddress << " -> "
178  << t.destinationAddress << ")\n";
179  std::cout << " Tx Packets: " << i->second.txPackets << "\n";
180  std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
181  std::cout << " TxOffered: " << i->second.txBytes * 8.0 / 9.0 / 1000 / 1000
182  << " Mbps\n";
183  std::cout << " Rx Packets: " << i->second.rxPackets << "\n";
184  std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
185  std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 9.0 / 1000 / 1000
186  << " Mbps\n";
187  }
188  }
189 
190  // 11. Cleanup
192 }
193 
194 int
195 main(int argc, char** argv)
196 {
197  std::string wifiManager("Arf");
198  CommandLine cmd(__FILE__);
199  cmd.AddValue(
200  "wifiManager",
201  "Set wifi rate manager (Aarf, Aarfcd, Amrr, Arf, Cara, Ideal, Minstrel, Onoe, Rraa)",
202  wifiManager);
203  cmd.Parse(argc, argv);
204 
205  std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush;
206  experiment(false, wifiManager);
207  std::cout << "------------------------------------------------\n";
208  std::cout << "Hidden station experiment with RTS/CTS enabled:\n";
209  experiment(true, wifiManager);
210 
211  return 0;
212 }
holds a vector of ns3::Application pointers.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
Parse command-line arguments.
Definition: command-line.h:232
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.
Definition: flow-monitor.h:231
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.
Definition: ipv4-address.h:42
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:471
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
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
Hold variables of type string.
Definition: string.h:56
Create an application which sends a UDP packet and waits for an echo of this packet.
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
ApplicationContainer Install(Ptr< Node > node) const
Create a udp echo client application on the specified node.
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
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:890
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ WIFI_STANDARD_80211b
NodeContainer nodes
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
wifi
Definition: third.py:95
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.