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 
47 #include "ns3/core-module.h"
48 #include "ns3/internet-module.h"
49 #include "ns3/network-module.h"
50 #include "ns3/applications-module.h"
51 #include "ns3/wifi-module.h"
52 #include "ns3/mesh-module.h"
53 #include "ns3/mobility-module.h"
54 #include "ns3/mesh-helper.h"
55 
56 #include <iostream>
57 #include <sstream>
58 #include <fstream>
59 
60 using namespace ns3;
61 
62 NS_LOG_COMPONENT_DEFINE ("TestMeshScript");
63 
68 class MeshTest
69 {
70 public:
72  MeshTest ();
79  void Configure (int argc, char ** argv);
84  int Run ();
85 private:
86  int m_xSize;
87  int m_ySize;
88  double m_step;
89  double m_randomStart;
90  double m_totalTime;
92  uint16_t m_packetSize;
93  uint32_t m_nIfaces;
94  bool m_chan;
95  bool m_pcap;
96  std::string m_stack;
97  std::string m_root;
106 private:
108  void CreateNodes ();
110  void InstallInternetStack ();
112  void InstallApplication ();
114  void Report ();
115 };
117  m_xSize (3),
118  m_ySize (3),
119  m_step (100.0),
120  m_randomStart (0.1),
121  m_totalTime (100.0),
122  m_packetInterval (0.1),
123  m_packetSize (1024),
124  m_nIfaces (1),
125  m_chan (true),
126  m_pcap (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 ("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
147  cmd.AddValue ("root", "Mac address of root mesh point in HWMP", m_root);
148 
149  cmd.Parse (argc, argv);
150  NS_LOG_DEBUG ("Grid:" << m_xSize << "*" << m_ySize);
151  NS_LOG_DEBUG ("Simulation time: " << m_totalTime << " s");
152 }
153 void
155 {
156  /*
157  * Create m_ySize*m_xSize stations to form a grid topology
158  */
160  // Configure YansWifiChannel
161  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
162  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
163  wifiPhy.SetChannel (wifiChannel.Create ());
164  /*
165  * Create mesh helper and set stack installer to it
166  * Stack installer creates all needed protocols and install them to
167  * mesh point device
168  */
169  mesh = MeshHelper::Default ();
170  if (!Mac48Address (m_root.c_str ()).IsBroadcast ())
171  {
173  }
174  else
175  {
176  //If root is not set, we do not use "Root" attribute, because it
177  //is specified only for 11s
179  }
180  if (m_chan)
181  {
182  mesh.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);
183  }
184  else
185  {
186  mesh.SetSpreadInterfaceChannels (MeshHelper::ZERO_CHANNEL);
187  }
188  mesh.SetMacType ("RandomStart", TimeValue (Seconds (m_randomStart)));
189  // Set number of interfaces - default is single-interface mesh point
191  // Install protocols and return container if MeshPointDevices
192  meshDevices = mesh.Install (wifiPhy, nodes);
193  // Setup mobility - static grid topology
195  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
196  "MinX", DoubleValue (0.0),
197  "MinY", DoubleValue (0.0),
198  "DeltaX", DoubleValue (m_step),
199  "DeltaY", DoubleValue (m_step),
200  "GridWidth", UintegerValue (m_xSize),
201  "LayoutType", StringValue ("RowFirst"));
202  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
203  mobility.Install (nodes);
204  if (m_pcap)
205  wifiPhy.EnablePcapAll (std::string ("mp-"));
206 }
207 void
209 {
210  InternetStackHelper internetStack;
211  internetStack.Install (nodes);
213  address.SetBase ("10.1.1.0", "255.255.255.0");
214  interfaces = address.Assign (meshDevices);
215 }
216 void
218 {
220  ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
221  serverApps.Start (Seconds (0.0));
222  serverApps.Stop (Seconds (m_totalTime));
224  echoClient.SetAttribute ("MaxPackets", UintegerValue ((uint32_t)(m_totalTime*(1/m_packetInterval))));
225  echoClient.SetAttribute ("Interval", TimeValue (Seconds (m_packetInterval)));
226  echoClient.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
228  clientApps.Start (Seconds (0.0));
229  clientApps.Stop (Seconds (m_totalTime));
230 }
231 int
233 {
234  CreateNodes ();
237  Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this);
238  Simulator::Stop (Seconds (m_totalTime));
239  Simulator::Run ();
240  Simulator::Destroy ();
241  return 0;
242 }
243 void
245 {
246  unsigned n (0);
247  for (NetDeviceContainer::Iterator i = meshDevices.Begin (); i != meshDevices.End (); ++i, ++n)
248  {
249  std::ostringstream os;
250  os << "mp-report-" << n << ".xml";
251  std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str () << "\n";
252  std::ofstream of;
253  of.open (os.str ().c_str ());
254  if (!of.is_open ())
255  {
256  std::cerr << "Error: Can't open file " << os.str () << "\n";
257  return;
258  }
259  mesh.Report (*i, of);
260  of.close ();
261  }
262 }
263 int
264 main (int argc, char *argv[])
265 {
266  MeshTest t;
267  t.Configure (argc, argv);
268  return t.Run ();
269 }
holds a vector of ns3::Application pointers.
double m_totalTime
total time
Definition: mesh.cc:90
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
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:96
void Report(const ns3::Ptr< ns3::NetDevice > &device, std::ostream &os)
Print statistics.
Definition: mesh-helper.cc:195
tuple echoServer
Definition: first.py:43
Create an application which sends a UDP packet and waits for an echo of this packet.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
void SetNumberOfInterfaces(uint32_t nInterfaces)
Set a number of interfaces in a mesh network.
Definition: mesh-helper.cc:77
tuple cmd
Definition: second.py:35
tuple clientApps
Definition: first.py:54
ApplicationContainer Install(Ptr< Node > node) const
Create a UdpEchoServerApplication on the specified Node.
tuple nodes
Definition: first.py:25
void SetChannel(Ptr< YansWifiChannel > channel)
void SetSpreadInterfaceChannels(ChannelPolicy policy)
set the channel policy
Definition: mesh-helper.cc:44
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:101
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
int Run()
Run test.
Definition: mesh.cc:232
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:1055
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
MeshTest()
Init test.
Definition: mesh.cc:116
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
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:126
int m_ySize
Y size.
Definition: mesh.cc:87
MeshTest class.
Definition: mesh.cc:68
tuple echoClient
Definition: first.py:49
tuple serverApps
Definition: first.py:45
bool m_chan
channel
Definition: mesh.cc:94
void InstallApplication()
Install applications.
Definition: mesh.cc:217
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
uint16_t m_packetSize
packet size
Definition: mesh.cc:92
void SetMobilityModel(std::string type, 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())
void CreateNodes()
Create nodes and setup their mobility.
Definition: mesh.cc:154
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
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:49
Helper class used to assign positions and mobility models to nodes.
Ipv4InterfaceContainer interfaces
Addresses of interfaces:
Definition: mesh.cc:103
void Configure(int argc, char **argv)
Configure test from command line arguments.
Definition: mesh.cc:132
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
double m_step
step
Definition: mesh.cc:88
int m_xSize
X size.
Definition: mesh.cc:86
uint32_t m_nIfaces
number interfaces
Definition: mesh.cc:93
NetDeviceContainer Install(const WifiPhyHelper &phyHelper, NodeContainer c) const
Install 802.11s mesh device & protocols on given node list.
Definition: mesh-helper.cc:82
AttributeValue implementation for Mac48Address.
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
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:269
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:993
MeshHelper mesh
MeshHelper. Report is not static methods.
Definition: mesh.cc:105
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.
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:95
Helper to create IEEE 802.11s mesh networks.
Definition: mesh-helper.h:38
tuple address
Definition: first.py:37
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
NodeContainer nodes
List of network nodes.
Definition: mesh.cc:99
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
std::string m_stack
void InstallInternetStack()
Install internet m_stack on nodes.
Definition: mesh.cc:208
double m_randomStart
random start
Definition: mesh.cc:89
void Report()
Print mesh devices diagnostics.
Definition: mesh.cc:244
double m_packetInterval
packet interval
Definition: mesh.cc:91
std::string m_root
root
Definition: mesh.cc:97
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const