A Discrete-Event Network Simulator
API
wifi-hidden-terminal.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 IITP RAS
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Pavel Boyko <boyko@iitp.ru>
19  *
20  * Classical hidden terminal problem and its RTS/CTS solution.
21  *
22  * Topology: [node 0] <-- -50 dB --> [node 1] <-- -50 dB --> [node 2]
23  *
24  * This example illustrates the use of
25  * - Wifi in ad-hoc mode
26  * - Matrix propagation loss model
27  * - Use of OnOffApplication to generate CBR stream
28  * - IP flow monitor
29  */
30 
31 #include "ns3/command-line.h"
32 #include "ns3/config.h"
33 #include "ns3/uinteger.h"
34 #include "ns3/boolean.h"
35 #include "ns3/string.h"
36 #include "ns3/yans-wifi-helper.h"
37 #include "ns3/internet-stack-helper.h"
38 #include "ns3/ipv4-address-helper.h"
39 #include "ns3/udp-echo-helper.h"
40 #include "ns3/yans-wifi-channel.h"
41 #include "ns3/constant-position-mobility-model.h"
42 #include "ns3/propagation-loss-model.h"
43 #include "ns3/propagation-delay-model.h"
44 #include "ns3/on-off-helper.h"
45 #include "ns3/flow-monitor-helper.h"
46 #include "ns3/ipv4-flow-classifier.h"
47 
48 using namespace ns3;
49 
51 void experiment (bool enableCtsRts, std::string wifiManager)
52 {
53  // 0. Enable or disable CTS/RTS
54  UintegerValue ctsThr = (enableCtsRts ? UintegerValue (100) : UintegerValue (2200));
55  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr);
56 
57  // 1. Create 3 nodes
59  nodes.Create (3);
60 
61  // 2. Place nodes somehow, this is required by every wireless simulation
62  for (uint8_t i = 0; i < 3; ++i)
63  {
64  nodes.Get (i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
65  }
66 
67  // 3. Create propagation loss matrix
68  Ptr<MatrixPropagationLossModel> lossModel = CreateObject<MatrixPropagationLossModel> ();
69  lossModel->SetDefaultLoss (200); // set default loss to 200 dB (no link)
70  lossModel->SetLoss (nodes.Get (0)->GetObject<MobilityModel> (), nodes.Get (1)->GetObject<MobilityModel> (), 50); // set symmetric loss 0 <-> 1 to 50 dB
71  lossModel->SetLoss (nodes.Get (2)->GetObject<MobilityModel> (), nodes.Get (1)->GetObject<MobilityModel> (), 50); // set symmetric loss 2 <-> 1 to 50 dB
72 
73  // 4. Create & setup wifi channel
74  Ptr<YansWifiChannel> wifiChannel = CreateObject <YansWifiChannel> ();
75  wifiChannel->SetPropagationLossModel (lossModel);
76  wifiChannel->SetPropagationDelayModel (CreateObject <ConstantSpeedPropagationDelayModel> ());
77 
78  // 5. Install wireless devices
80  wifi.SetStandard (WIFI_STANDARD_80211b);
81  wifi.SetRemoteStationManager ("ns3::" + wifiManager + "WifiManager");
82  YansWifiPhyHelper wifiPhy;
83  wifiPhy.SetChannel (wifiChannel);
84  WifiMacHelper wifiMac;
85  wifiMac.SetType ("ns3::AdhocWifiMac"); // use ad-hoc MAC
86  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
87 
88  // uncomment the following to have athstats output
89  // AthstatsHelper athstats;
90  // athstats.EnableAthstats(enableCtsRts ? "rtscts-athstats-node" : "basic-athstats-node" , nodes);
91 
92  // uncomment the following to have pcap output
93  // wifiPhy.EnablePcap (enableCtsRts ? "rtscts-pcap-node" : "basic-pcap-node" , nodes);
94 
95 
96  // 6. Install TCP/IP stack & assign IP addresses
97  InternetStackHelper internet;
98  internet.Install (nodes);
99  Ipv4AddressHelper ipv4;
100  ipv4.SetBase ("10.0.0.0", "255.0.0.0");
101  ipv4.Assign (devices);
102 
103  // 7. Install applications: two CBR streams each saturating the channel
104  ApplicationContainer cbrApps;
105  uint16_t cbrPort = 12345;
106  OnOffHelper onOffHelper ("ns3::UdpSocketFactory", 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
121  onOffHelper.SetAttribute ("DataRate", StringValue ("3001100bps"));
122  onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.001)));
123  cbrApps.Add (onOffHelper.Install (nodes.Get (2)));
124 
130  uint16_t echoPort = 9;
131  UdpEchoClientHelper echoClientHelper (Ipv4Address ("10.0.0.2"), echoPort);
132  echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (1));
133  echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (0.1)));
134  echoClientHelper.SetAttribute ("PacketSize", UintegerValue (10));
135  ApplicationContainer pingApps;
136 
137  // again using different start times to workaround Bug 388 and Bug 912
138  echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.001)));
139  pingApps.Add (echoClientHelper.Install (nodes.Get (0)));
140  echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.006)));
141  pingApps.Add (echoClientHelper.Install (nodes.Get (2)));
142 
143  // 8. Install FlowMonitor on all nodes
144  FlowMonitorHelper flowmon;
145  Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
146 
147  // 9. Run simulation for 10 seconds
148  Simulator::Stop (Seconds (10));
149  Simulator::Run ();
150 
151  // 10. Print per flow statistics
152  monitor->CheckForLostPackets ();
153  Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
154  FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();
155  for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
156  {
157  // first 2 FlowIds are for ECHO apps, we don't want to display them
158  //
159  // Duration for throughput measurement is 9.0 seconds, since
160  // StartTime of the OnOffApplication is at about "second 1"
161  // and
162  // Simulator::Stops at "second 10".
163  if (i->first > 2)
164  {
165  Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
166  std::cout << "Flow " << i->first - 2 << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
167  std::cout << " Tx Packets: " << i->second.txPackets << "\n";
168  std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
169  std::cout << " TxOffered: " << i->second.txBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n";
170  std::cout << " Rx Packets: " << i->second.rxPackets << "\n";
171  std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
172  std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n";
173  }
174  }
175 
176  // 11. Cleanup
178 }
179 
180 int main (int argc, char **argv)
181 {
182  std::string wifiManager ("Arf");
183  CommandLine cmd (__FILE__);
184  cmd.AddValue ("wifiManager", "Set wifi rate manager (Aarf, Aarfcd, Amrr, Arf, Cara, Ideal, Minstrel, Onoe, Rraa)", wifiManager);
185  cmd.Parse (argc, argv);
186 
187  std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush;
188  experiment (false, wifiManager);
189  std::cout << "------------------------------------------------\n";
190  std::cout << "Hidden station experiment with RTS/CTS enabled:\n";
191  experiment (true, wifiManager);
192 
193  return 0;
194 }
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:229
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:218
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
AttributeValue implementation for Time.
Definition: nstime.h:1308
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:44
helps to create WifiNetDevice objects
Definition: wifi-helper.h:274
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:849
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
@ WIFI_STANDARD_80211b
devices
Definition: first.py:39
nodes
Definition: first.py:32
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35
wifi
Definition: third.py:96
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.