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  * Authors: Pavel Boyko <boyko@iitp.ru>
19  */
20 
21 /*
22  * Classical hidden terminal problem and its RTS/CTS solution.
23  *
24  * Topology: [node 0] <-- -50 dB --> [node 1] <-- -50 dB --> [node 2]
25  *
26  * This example illustrates the use of
27  * - Wifi in ad-hoc mode
28  * - Matrix propagation loss model
29  * - Use of OnOffApplication to generate CBR stream
30  * - IP flow monitor
31  */
32 #include "ns3/core-module.h"
33 #include "ns3/propagation-module.h"
34 #include "ns3/network-module.h"
35 #include "ns3/applications-module.h"
36 #include "ns3/mobility-module.h"
37 #include "ns3/internet-module.h"
38 #include "ns3/flow-monitor-module.h"
39 #include "ns3/wifi-module.h"
40 
41 using namespace ns3;
42 
44 void experiment (bool enableCtsRts)
45 {
46  // 0. Enable or disable CTS/RTS
47  UintegerValue ctsThr = (enableCtsRts ? UintegerValue (100) : UintegerValue (2200));
48  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr);
49 
50  // 1. Create 3 nodes
52  nodes.Create (3);
53 
54  // 2. Place nodes somehow, this is required by every wireless simulation
55  for (size_t i = 0; i < 3; ++i)
56  {
57  nodes.Get (i)->AggregateObject (CreateObject<ConstantPositionMobilityModel> ());
58  }
59 
60  // 3. Create propagation loss matrix
61  Ptr<MatrixPropagationLossModel> lossModel = CreateObject<MatrixPropagationLossModel> ();
62  lossModel->SetDefaultLoss (200); // set default loss to 200 dB (no link)
63  lossModel->SetLoss (nodes.Get (0)->GetObject<MobilityModel>(), nodes.Get (1)->GetObject<MobilityModel>(), 50); // set symmetric loss 0 <-> 1 to 50 dB
64  lossModel->SetLoss (nodes.Get (2)->GetObject<MobilityModel>(), nodes.Get (1)->GetObject<MobilityModel>(), 50); // set symmetric loss 2 <-> 1 to 50 dB
65 
66  // 4. Create & setup wifi channel
67  Ptr<YansWifiChannel> wifiChannel = CreateObject <YansWifiChannel> ();
68  wifiChannel->SetPropagationLossModel (lossModel);
69  wifiChannel->SetPropagationDelayModel (CreateObject <ConstantSpeedPropagationDelayModel> ());
70 
71  // 5. Install wireless devices
74  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
75  "DataMode",StringValue ("DsssRate2Mbps"),
76  "ControlMode",StringValue ("DsssRate1Mbps"));
78  wifiPhy.SetChannel (wifiChannel);
79  WifiMacHelper wifiMac;
80  wifiMac.SetType ("ns3::AdhocWifiMac"); // use ad-hoc MAC
81  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
82 
83  // uncomment the following to have athstats output
84  // AthstatsHelper athstats;
85  // athstats.EnableAthstats(enableCtsRts ? "rtscts-athstats-node" : "basic-athstats-node" , nodes);
86 
87  // uncomment the following to have pcap output
88  // wifiPhy.EnablePcap (enableCtsRts ? "rtscts-pcap-node" : "basic-pcap-node" , nodes);
89 
90 
91  // 6. Install TCP/IP stack & assign IP addresses
92  InternetStackHelper internet;
93  internet.Install (nodes);
94  Ipv4AddressHelper ipv4;
95  ipv4.SetBase ("10.0.0.0", "255.0.0.0");
96  ipv4.Assign (devices);
97 
98  // 7. Install applications: two CBR streams each saturating the channel
99  ApplicationContainer cbrApps;
100  uint16_t cbrPort = 12345;
101  OnOffHelper onOffHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address ("10.0.0.2"), cbrPort));
102  onOffHelper.SetAttribute ("PacketSize", UintegerValue (1400));
103  onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
104  onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
105 
106  // flow 1: node 0 -> node 1
107  onOffHelper.SetAttribute ("DataRate", StringValue ("3000000bps"));
108  onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.000000)));
109  cbrApps.Add (onOffHelper.Install (nodes.Get (0)));
110 
111  // flow 2: node 2 -> node 1
116  onOffHelper.SetAttribute ("DataRate", StringValue ("3001100bps"));
117  onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.001)));
118  cbrApps.Add (onOffHelper.Install (nodes.Get (2)));
119 
125  uint16_t echoPort = 9;
126  UdpEchoClientHelper echoClientHelper (Ipv4Address ("10.0.0.2"), echoPort);
127  echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (1));
128  echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (0.1)));
129  echoClientHelper.SetAttribute ("PacketSize", UintegerValue (10));
130  ApplicationContainer pingApps;
131 
132  // again using different start times to workaround Bug 388 and Bug 912
133  echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.001)));
134  pingApps.Add (echoClientHelper.Install (nodes.Get (0)));
135  echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.006)));
136  pingApps.Add (echoClientHelper.Install (nodes.Get (2)));
137 
138 
139 
140 
141  // 8. Install FlowMonitor on all nodes
142  FlowMonitorHelper flowmon;
143  Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
144 
145  // 9. Run simulation for 10 seconds
146  Simulator::Stop (Seconds (10));
147  Simulator::Run ();
148 
149  // 10. Print per flow statistics
150  monitor->CheckForLostPackets ();
151  Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
152  FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats ();
153  for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
154  {
155  // first 2 FlowIds are for ECHO apps, we don't want to display them
156  //
157  // Duration for throughput measurement is 9.0 seconds, since
158  // StartTime of the OnOffApplication is at about "second 1"
159  // and
160  // Simulator::Stops at "second 10".
161  if (i->first > 2)
162  {
163  Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
164  std::cout << "Flow " << i->first - 2 << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n";
165  std::cout << " Tx Packets: " << i->second.txPackets << "\n";
166  std::cout << " Tx Bytes: " << i->second.txBytes << "\n";
167  std::cout << " TxOffered: " << i->second.txBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n";
168  std::cout << " Rx Packets: " << i->second.rxPackets << "\n";
169  std::cout << " Rx Bytes: " << i->second.rxBytes << "\n";
170  std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n";
171  }
172  }
173 
174  // 11. Cleanup
176 }
177 
178 int main (int argc, char **argv)
179 {
181  cmd.Parse (argc, argv);
182 
183  std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush;
184  experiment (false);
185  std::cout << "------------------------------------------------\n";
186  std::cout << "Hidden station experiment with RTS/CTS enabled:\n";
187  experiment (true);
188 
189  return 0;
190 }
holds a vector of ns3::Application pointers.
void experiment(bool enableCtsRts)
Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism.
an Inet address class
const FlowStatsContainer & GetFlowStats() const
Retrieve all collected the flow statistics.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
tuple devices
Definition: first.py:32
void SetDefaultLoss(double defaultLoss)
Set the default propagation loss (in dB, positive) to be used, infinity if not set.
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:683
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
void CheckForLostPackets()
Check right now for packets that appear to be lost.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container. ...
Ipv4Address destinationAddress
Destination address.
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
void SetPropagationLossModel(Ptr< PropagationLossModel > loss)
Create an application which sends a UDP packet and waits for an echo of this packet.
void SetPropagationDelayModel(Ptr< PropagationDelayModel > delay)
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
aggregate IP/TCP/UDP functionality to existing Nodes.
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:231
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:712
tuple nodes
Definition: first.py:25
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
std::map< FlowId, FlowStats > FlowStatsContainer
Container: FlowId, FlowStats.
Definition: flow-monitor.h:216
AttributeValue implementation for Time.
Definition: nstime.h:957
FiveTuple FindFlow(FlowId flowId) const
Searches for the FiveTuple corresponding to the given flowId.
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:706
Ptr< FlowMonitor > InstallAll()
Enable flow monitoring on all nodes.
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:165
Ptr< FlowClassifier > GetClassifier()
Retrieve the FlowClassifier object for IPv4 created by the Install* methods.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper to enable IP flow monitoring on a set of Nodes.
create MAC layers for a ns3::WifiNetDevice.
Structure to classify a packet.
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:40
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void SetLoss(Ptr< MobilityModel > a, Ptr< MobilityModel > b, double loss, bool symmetric=true)
Set loss (in dB, positive) between pair of ns-3 objects (typically, nodes).
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
Ipv4Address sourceAddress
Source address.
ApplicationContainer Install(Ptr< Node > node) const
Create a udp echo client application on the specified node.
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.