A Discrete-Event Network Simulator
API
mesh.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2008,2009 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: Kirill Andreev <andreev@iitp.ru>
19  *
20  *
21  * By default this script creates m_xSize * m_ySize square grid topology with
22  * IEEE802.11s stack installed at each node with peering management
23  * and HWMP protocol.
24  * The side of the square cell is defined by m_step parameter.
25  * When topology is created, UDP ping is installed to opposite corners
26  * by diagonals. packet size of the UDP ping and interval between two
27  * successive packets is configurable.
28  *
29  * m_xSize * step
30  * |<--------->|
31  * step
32  * |<--->|
33  * * --- * --- * <---Ping sink _
34  * | \ | / | ^
35  * | \ | / | |
36  * * --- * --- * m_ySize * step |
37  * | / | \ | |
38  * | / | \ | |
39  * * --- * --- * _
40  * ^ Ping source
41  *
42  * See also MeshTest::Configure to read more about configurable
43  * parameters.
44  */
45 
46 #include <iostream>
47 #include <sstream>
48 #include <fstream>
49 #include "ns3/core-module.h"
50 #include "ns3/internet-module.h"
51 #include "ns3/network-module.h"
52 #include "ns3/applications-module.h"
53 #include "ns3/mesh-module.h"
54 #include "ns3/mobility-module.h"
55 #include "ns3/mesh-helper.h"
56 #include "ns3/yans-wifi-helper.h"
57 
58 using namespace ns3;
59 
60 NS_LOG_COMPONENT_DEFINE ("TestMeshScript");
61 
66 class MeshTest
67 {
68 public:
70  MeshTest ();
77  void Configure (int argc, char ** argv);
82  int Run ();
83 private:
84  int m_xSize;
85  int m_ySize;
86  double m_step;
87  double m_randomStart;
88  double m_totalTime;
90  uint16_t m_packetSize;
91  uint32_t m_nIfaces;
92  bool m_chan;
93  bool m_pcap;
94  bool m_ascii;
95  std::string m_stack;
96  std::string m_root;
105 private:
107  void CreateNodes ();
109  void InstallInternetStack ();
111  void InstallApplication ();
113  void Report ();
114 };
116  m_xSize (3),
117  m_ySize (3),
118  m_step (100.0),
119  m_randomStart (0.1),
120  m_totalTime (100.0),
121  m_packetInterval (0.1),
122  m_packetSize (1024),
123  m_nIfaces (1),
124  m_chan (true),
125  m_pcap (false),
126  m_ascii (false),
127  m_stack ("ns3::Dot11sStack"),
128  m_root ("ff:ff:ff:ff:ff:ff")
129 {
130 }
131 void
132 MeshTest::Configure (int argc, char *argv[])
133 {
135  cmd.AddValue ("x-size", "Number of nodes in a row grid", m_xSize);
136  cmd.AddValue ("y-size", "Number of rows in a grid", m_ySize);
137  cmd.AddValue ("step", "Size of edge in our grid (meters)", m_step);
138  // Avoid starting all mesh nodes at the same time (beacons may collide)
139  cmd.AddValue ("start", "Maximum random start delay for beacon jitter (sec)", m_randomStart);
140  cmd.AddValue ("time", "Simulation time (sec)", m_totalTime);
141  cmd.AddValue ("packet-interval", "Interval between packets in UDP ping (sec)", m_packetInterval);
142  cmd.AddValue ("packet-size", "Size of packets in UDP ping (bytes)", m_packetSize);
143  cmd.AddValue ("interfaces", "Number of radio interfaces used by each mesh point", m_nIfaces);
144  cmd.AddValue ("channels", "Use different frequency channels for different interfaces", m_chan);
145  cmd.AddValue ("pcap", "Enable PCAP traces on interfaces", m_pcap);
146  cmd.AddValue ("ascii", "Enable Ascii traces on interfaces", m_ascii);
147  cmd.AddValue ("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
148  cmd.AddValue ("root", "Mac address of root mesh point in HWMP", m_root);
149 
150  cmd.Parse (argc, argv);
151  NS_LOG_DEBUG ("Grid:" << m_xSize << "*" << m_ySize);
152  NS_LOG_DEBUG ("Simulation time: " << m_totalTime << " s");
153  if (m_ascii)
154  {
155  PacketMetadata::Enable ();
156  }
157 }
158 void
160 {
161  /*
162  * Create m_ySize*m_xSize stations to form a grid topology
163  */
165  // Configure YansWifiChannel
166  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
167  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
168  wifiPhy.SetChannel (wifiChannel.Create ());
169  /*
170  * Create mesh helper and set stack installer to it
171  * Stack installer creates all needed protocols and install them to
172  * mesh point device
173  */
174  mesh = MeshHelper::Default ();
175  if (!Mac48Address (m_root.c_str ()).IsBroadcast ())
176  {
178  }
179  else
180  {
181  //If root is not set, we do not use "Root" attribute, because it
182  //is specified only for 11s
184  }
185  if (m_chan)
186  {
187  mesh.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);
188  }
189  else
190  {
191  mesh.SetSpreadInterfaceChannels (MeshHelper::ZERO_CHANNEL);
192  }
193  mesh.SetMacType ("RandomStart", TimeValue (Seconds (m_randomStart)));
194  // Set number of interfaces - default is single-interface mesh point
196  // Install protocols and return container if MeshPointDevices
197  meshDevices = mesh.Install (wifiPhy, nodes);
198  // Setup mobility - static grid topology
200  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
201  "MinX", DoubleValue (0.0),
202  "MinY", DoubleValue (0.0),
203  "DeltaX", DoubleValue (m_step),
204  "DeltaY", DoubleValue (m_step),
205  "GridWidth", UintegerValue (m_xSize),
206  "LayoutType", StringValue ("RowFirst"));
207  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
208  mobility.Install (nodes);
209  if (m_pcap)
210  wifiPhy.EnablePcapAll (std::string ("mp-"));
211  if (m_ascii)
212  {
213  AsciiTraceHelper ascii;
214  wifiPhy.EnableAsciiAll (ascii.CreateFileStream ("mesh.tr"));
215  }
216 }
217 void
219 {
220  InternetStackHelper internetStack;
221  internetStack.Install (nodes);
223  address.SetBase ("10.1.1.0", "255.255.255.0");
224  interfaces = address.Assign (meshDevices);
225 }
226 void
228 {
231  serverApps.Start (Seconds (0.0));
232  serverApps.Stop (Seconds (m_totalTime));
234  echoClient.SetAttribute ("MaxPackets", UintegerValue ((uint32_t)(m_totalTime*(1/m_packetInterval))));
235  echoClient.SetAttribute ("Interval", TimeValue (Seconds (m_packetInterval)));
236  echoClient.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
238  clientApps.Start (Seconds (0.0));
239  clientApps.Stop (Seconds (m_totalTime));
240 }
241 int
243 {
244  CreateNodes ();
247  Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this);
248  Simulator::Stop (Seconds (m_totalTime));
249  Simulator::Run ();
250  Simulator::Destroy ();
251  return 0;
252 }
253 void
255 {
256  unsigned n (0);
257  for (NetDeviceContainer::Iterator i = meshDevices.Begin (); i != meshDevices.End (); ++i, ++n)
258  {
259  std::ostringstream os;
260  os << "mp-report-" << n << ".xml";
261  std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str () << "\n";
262  std::ofstream of;
263  of.open (os.str ().c_str ());
264  if (!of.is_open ())
265  {
266  std::cerr << "Error: Can't open file " << os.str () << "\n";
267  return;
268  }
269  mesh.Report (*i, of);
270  of.close ();
271  }
272 }
273 int
274 main (int argc, char *argv[])
275 {
276  MeshTest t;
277  t.Configure (argc, argv);
278  return t.Run ();
279 }
holds a vector of ns3::Application pointers.
double m_totalTime
total time
Definition: mesh.cc:88
Manage ASCII trace files for device models.
Definition: trace-helper.h:161
bool m_ascii
ASCII.
Definition: mesh.cc:94
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
std::string m_stack
stack
Definition: mesh.cc:95
void Report(const ns3::Ptr< ns3::NetDevice > &device, std::ostream &os)
Print statistics.
Definition: mesh-helper.cc:196
serverApps
Definition: first.py:45
Create an application which sends a UDP packet and waits for an echo of this packet.
echoServer
Definition: first.py:43
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
aggregate IP/TCP/UDP functionality to existing Nodes.
cmd
Definition: second.py:35
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we&#39;ll use to write the traced bits. ...
void SetNumberOfInterfaces(uint32_t nInterfaces)
Set a number of interfaces in a mesh network.
Definition: mesh-helper.cc:78
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:101
void SetChannel(Ptr< YansWifiChannel > channel)
void SetSpreadInterfaceChannels(ChannelPolicy policy)
set the channel policy
Definition: mesh-helper.cc:45
Create a server application which waits for input UDP packets and sends them back to the original sen...
NetDeviceContainer meshDevices
List of all mesh point devices.
Definition: mesh.cc:100
nodes
Definition: first.py:25
int Run()
Run test.
Definition: mesh.cc:242
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
AttributeValue implementation for Time.
Definition: nstime.h:1076
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
echoClient
Definition: first.py:49
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
MeshTest()
Init test.
Definition: mesh.cc:115
Parse command-line arguments.
Definition: command-line.h:213
void SetMacType(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: mesh-helper.cc:127
int m_ySize
Y size.
Definition: mesh.cc:85
MeshTest class.
Definition: mesh.cc:66
bool m_chan
channel
Definition: mesh.cc:92
void InstallApplication()
Install applications.
Definition: mesh.cc:227
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:37
uint16_t m_packetSize
packet size
Definition: mesh.cc:90
void CreateNodes()
Create nodes and setup their mobility.
Definition: mesh.cc:159
an EUI-48 address
Definition: mac48-address.h:43
manage and create wifi channel objects for the yans model.
void SetStackInstaller(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: mesh-helper.cc:50
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Helper class used to assign positions and mobility models to nodes.
Ipv4InterfaceContainer interfaces
Addresses of interfaces:
Definition: mesh.cc:102
void Configure(int argc, char **argv)
Configure test from command line arguments.
Definition: mesh.cc:132
NetDeviceContainer Install(const WifiPhyHelper &phyHelper, NodeContainer c) const
Install 802.11s mesh device & protocols on given node list.
Definition: mesh-helper.cc:83
double m_step
step
Definition: mesh.cc:86
int m_xSize
X size.
Definition: mesh.cc:84
uint32_t m_nIfaces
number interfaces
Definition: mesh.cc:91
AttributeValue implementation for Mac48Address.
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
MeshHelper mesh
MeshHelper. Report is not static methods.
Definition: mesh.cc:104
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
clientApps
Definition: first.py:54
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
bool m_pcap
PCAP.
Definition: mesh.cc:93
Helper to create IEEE 802.11s mesh networks.
Definition: mesh-helper.h:42
NodeContainer nodes
List of network nodes.
Definition: mesh.cc:98
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
std::string m_stack
void InstallInternetStack()
Install internet m_stack on nodes.
Definition: mesh.cc:218
double m_randomStart
random start
Definition: mesh.cc:87
void Report()
Print mesh devices diagnostics.
Definition: mesh.cc:254
double m_packetInterval
packet interval
Definition: mesh.cc:89
std::string m_root
root
Definition: mesh.cc:96