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 
64 class MeshTest
65 {
66 public:
68  MeshTest ();
70  void Configure (int argc, char ** argv);
72  int Run ();
73 private:
74  int m_xSize;
75  int m_ySize;
76  double m_step;
77  double m_randomStart;
78  double m_totalTime;
80  uint16_t m_packetSize;
81  uint32_t m_nIfaces;
82  bool m_chan;
83  bool m_pcap;
84  std::string m_stack;
85  std::string m_root;
90  //Addresses of interfaces:
92  // MeshHelper. Report is not static methods
94 private:
96  void CreateNodes ();
98  void InstallInternetStack ();
100  void InstallApplication ();
102  void Report ();
103 };
105  m_xSize (3),
106  m_ySize (3),
107  m_step (100.0),
108  m_randomStart (0.1),
109  m_totalTime (100.0),
110  m_packetInterval (0.1),
111  m_packetSize (1024),
112  m_nIfaces (1),
113  m_chan (true),
114  m_pcap (false),
115  m_stack ("ns3::Dot11sStack"),
116  m_root ("ff:ff:ff:ff:ff:ff")
117 {
118 }
119 void
120 MeshTest::Configure (int argc, char *argv[])
121 {
123  cmd.AddValue ("x-size", "Number of nodes in a row grid. [6]", m_xSize);
124  cmd.AddValue ("y-size", "Number of rows in a grid. [6]", m_ySize);
125  cmd.AddValue ("step", "Size of edge in our grid, meters. [100 m]", m_step);
126  /*
127  * As soon as starting node means that it sends a beacon,
128  * simultaneous start is not good.
129  */
130  cmd.AddValue ("start", "Maximum random start delay, seconds. [0.1 s]", m_randomStart);
131  cmd.AddValue ("time", "Simulation time, seconds [100 s]", m_totalTime);
132  cmd.AddValue ("packet-interval", "Interval between packets in UDP ping, seconds [0.001 s]", m_packetInterval);
133  cmd.AddValue ("packet-size", "Size of packets in UDP ping", m_packetSize);
134  cmd.AddValue ("interfaces", "Number of radio interfaces used by each mesh point. [1]", m_nIfaces);
135  cmd.AddValue ("channels", "Use different frequency channels for different interfaces. [0]", m_chan);
136  cmd.AddValue ("pcap", "Enable PCAP traces on interfaces. [0]", m_pcap);
137  cmd.AddValue ("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
138  cmd.AddValue ("root", "Mac address of root mesh point in HWMP", m_root);
139 
140  cmd.Parse (argc, argv);
141  NS_LOG_DEBUG ("Grid:" << m_xSize << "*" << m_ySize);
142  NS_LOG_DEBUG ("Simulation time: " << m_totalTime << " s");
143 }
144 void
146 {
147  /*
148  * Create m_ySize*m_xSize stations to form a grid topology
149  */
151  // Configure YansWifiChannel
152  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
153  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
154  wifiPhy.SetChannel (wifiChannel.Create ());
155  /*
156  * Create mesh helper and set stack installer to it
157  * Stack installer creates all needed protocols and install them to
158  * mesh point device
159  */
160  mesh = MeshHelper::Default ();
161  if (!Mac48Address (m_root.c_str ()).IsBroadcast ())
162  {
164  }
165  else
166  {
167  //If root is not set, we do not use "Root" attribute, because it
168  //is specified only for 11s
170  }
171  if (m_chan)
172  {
173  mesh.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);
174  }
175  else
176  {
177  mesh.SetSpreadInterfaceChannels (MeshHelper::ZERO_CHANNEL);
178  }
179  mesh.SetMacType ("RandomStart", TimeValue (Seconds (m_randomStart)));
180  // Set number of interfaces - default is single-interface mesh point
182  // Install protocols and return container if MeshPointDevices
183  meshDevices = mesh.Install (wifiPhy, nodes);
184  // Setup mobility - static grid topology
186  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
187  "MinX", DoubleValue (0.0),
188  "MinY", DoubleValue (0.0),
189  "DeltaX", DoubleValue (m_step),
190  "DeltaY", DoubleValue (m_step),
191  "GridWidth", UintegerValue (m_xSize),
192  "LayoutType", StringValue ("RowFirst"));
193  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
194  mobility.Install (nodes);
195  if (m_pcap)
196  wifiPhy.EnablePcapAll (std::string ("mp-"));
197 }
198 void
200 {
201  InternetStackHelper internetStack;
202  internetStack.Install (nodes);
204  address.SetBase ("10.1.1.0", "255.255.255.0");
205  interfaces = address.Assign (meshDevices);
206 }
207 void
209 {
211  ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
212  serverApps.Start (Seconds (0.0));
213  serverApps.Stop (Seconds (m_totalTime));
215  echoClient.SetAttribute ("MaxPackets", UintegerValue ((uint32_t)(m_totalTime*(1/m_packetInterval))));
216  echoClient.SetAttribute ("Interval", TimeValue (Seconds (m_packetInterval)));
217  echoClient.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
219  clientApps.Start (Seconds (0.0));
220  clientApps.Stop (Seconds (m_totalTime));
221 }
222 int
224 {
225  CreateNodes ();
228  Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this);
229  Simulator::Stop (Seconds (m_totalTime));
230  Simulator::Run ();
231  Simulator::Destroy ();
232  return 0;
233 }
234 void
236 {
237  unsigned n (0);
238  for (NetDeviceContainer::Iterator i = meshDevices.Begin (); i != meshDevices.End (); ++i, ++n)
239  {
240  std::ostringstream os;
241  os << "mp-report-" << n << ".xml";
242  std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str () << "\n";
243  std::ofstream of;
244  of.open (os.str ().c_str ());
245  if (!of.is_open ())
246  {
247  std::cerr << "Error: Can't open file " << os.str () << "\n";
248  return;
249  }
250  mesh.Report (*i, of);
251  of.close ();
252  }
253 }
254 int
255 main (int argc, char *argv[])
256 {
257  MeshTest t;
258  t.Configure (argc, argv);
259  return t.Run ();
260 }
holds a vector of ns3::Application pointers.
double m_totalTime
Definition: mesh.cc:78
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
Definition: mesh.cc:84
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 Report(const ns3::Ptr< ns3::NetDevice > &, std::ostream &)
Print statistics.
Definition: mesh-helper.cc:195
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.
void SetChannel(Ptr< YansWifiChannel > channel)
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:89
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:223
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:957
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
MeshTest()
Init test.
Definition: mesh.cc:104
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
Definition: mesh.cc:75
Definition: mesh.cc:64
tuple echoClient
Definition: first.py:49
tuple serverApps
Definition: first.py:45
bool m_chan
Definition: mesh.cc:82
void InstallApplication()
Install applications.
Definition: mesh.cc:208
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
Definition: mesh.cc:80
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:145
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 SetSpreadInterfaceChannels(ChannelPolicy)
set the channel policy
Definition: mesh-helper.cc:44
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
Definition: mesh.cc:91
void Configure(int argc, char **argv)
Configure test from command line arguments.
Definition: mesh.cc:120
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
Definition: mesh.cc:76
int m_xSize
Definition: mesh.cc:74
uint32_t m_nIfaces
Definition: mesh.cc:81
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:495
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:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
MeshHelper mesh
Definition: mesh.cc:93
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
Definition: mesh.cc:83
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:87
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:199
double m_randomStart
Definition: mesh.cc:77
void Report()
Print mesh devices diagnostics.
Definition: mesh.cc:235
double m_packetInterval
Definition: mesh.cc:79
std::string m_root
Definition: mesh.cc:85
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