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  * By varying m_xSize and m_ySize, one can configure the route that is used.
43  * When the inter-nodal distance is small, the source can reach the sink
44  * directly. When the inter-nodal distance is intermediate, the route
45  * selected is diagonal (two hop). When the inter-nodal distance is a bit
46  * larger, the diagonals cannot be used and a four-hop route is selected.
47  * When the distance is a bit larger, the packets will fail to reach even the
48  * adjacent nodes.
49  *
50  * As of ns-3.36 release, with default configuration (mesh uses Wi-Fi 802.11a
51  * standard and the ArfWifiManager rate control by default), the maximum
52  * range is roughly 50m. The default step size in this program is set to 50m,
53  * so any mesh packets in the above diagram depiction will not be received
54  * successfully on the diagonal hops between two nodes but only on the
55  * horizontal and vertical hops. If the step size is reduced to 35m, then
56  * the shortest path will be on the diagonal hops. If the step size is reduced
57  * to 17m or less, then the source will be able to reach the sink directly
58  * without any mesh hops (for the default 3x3 mesh depicted above).
59  *
60  * The position allocator will lay out the nodes in the following order
61  * (corresponding to Node ID and to the diagram above):
62  *
63  * 6 - 7 - 8
64  * | | |
65  * 3 - 4 - 5
66  * | | |
67  * 0 - 1 - 2
68  *
69  * See also MeshTest::Configure to read more about configurable
70  * parameters.
71  */
72 
73 #include <iostream>
74 #include <sstream>
75 #include <fstream>
76 #include "ns3/core-module.h"
77 #include "ns3/internet-module.h"
78 #include "ns3/network-module.h"
79 #include "ns3/applications-module.h"
80 #include "ns3/mesh-module.h"
81 #include "ns3/mobility-module.h"
82 #include "ns3/mesh-helper.h"
83 #include "ns3/yans-wifi-helper.h"
84 
85 using namespace ns3;
86 
87 NS_LOG_COMPONENT_DEFINE ("MeshExample");
88 
89 // Declaring these variables outside of main() for use in trace sinks
90 uint32_t g_udpTxCount = 0;
91 uint32_t g_udpRxCount = 0;
92 
93 void
95 {
96  NS_LOG_DEBUG ("Sent " << p->GetSize () << " bytes");
97  g_udpTxCount++;
98 }
99 
100 void
102 {
103  NS_LOG_DEBUG ("Received " << p->GetSize () << " bytes");
104  g_udpRxCount++;
105 }
106 
107 
112 class MeshTest
113 {
114 public:
116  MeshTest ();
123  void Configure (int argc, char ** argv);
128  int Run ();
129 private:
130  int m_xSize;
131  int m_ySize;
132  double m_step;
133  double m_randomStart;
134  double m_totalTime;
136  uint16_t m_packetSize;
137  uint32_t m_nIfaces;
138  bool m_chan;
139  bool m_pcap;
140  bool m_ascii;
141  std::string m_stack;
142  std::string m_root;
151 private:
153  void CreateNodes ();
155  void InstallInternetStack ();
157  void InstallApplication ();
159  void Report ();
160 };
162  m_xSize (3),
163  m_ySize (3),
164  m_step (50.0),
165  m_randomStart (0.1),
166  m_totalTime (100.0),
167  m_packetInterval (1),
168  m_packetSize (1024),
169  m_nIfaces (1),
170  m_chan (true),
171  m_pcap (false),
172  m_ascii (false),
173  m_stack ("ns3::Dot11sStack"),
174  m_root ("ff:ff:ff:ff:ff:ff")
175 {
176 }
177 void
178 MeshTest::Configure (int argc, char *argv[])
179 {
180  CommandLine cmd (__FILE__);
181  cmd.AddValue ("x-size", "Number of nodes in a row grid", m_xSize);
182  cmd.AddValue ("y-size", "Number of rows in a grid", m_ySize);
183  cmd.AddValue ("step", "Size of edge in our grid (meters)", m_step);
184  // Avoid starting all mesh nodes at the same time (beacons may collide)
185  cmd.AddValue ("start", "Maximum random start delay for beacon jitter (sec)", m_randomStart);
186  cmd.AddValue ("time", "Simulation time (sec)", m_totalTime);
187  cmd.AddValue ("packet-interval", "Interval between packets in UDP ping (sec)", m_packetInterval);
188  cmd.AddValue ("packet-size", "Size of packets in UDP ping (bytes)", m_packetSize);
189  cmd.AddValue ("interfaces", "Number of radio interfaces used by each mesh point", m_nIfaces);
190  cmd.AddValue ("channels", "Use different frequency channels for different interfaces", m_chan);
191  cmd.AddValue ("pcap", "Enable PCAP traces on interfaces", m_pcap);
192  cmd.AddValue ("ascii", "Enable Ascii traces on interfaces", m_ascii);
193  cmd.AddValue ("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
194  cmd.AddValue ("root", "Mac address of root mesh point in HWMP", m_root);
195 
196  cmd.Parse (argc, argv);
197  NS_LOG_DEBUG ("Grid:" << m_xSize << "*" << m_ySize);
198  NS_LOG_DEBUG ("Simulation time: " << m_totalTime << " s");
199  if (m_ascii)
200  {
201  PacketMetadata::Enable ();
202  }
203 }
204 void
206 {
207  /*
208  * Create m_ySize*m_xSize stations to form a grid topology
209  */
211  // Configure YansWifiChannel
212  YansWifiPhyHelper wifiPhy;
213  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
214  wifiPhy.SetChannel (wifiChannel.Create ());
215  /*
216  * Create mesh helper and set stack installer to it
217  * Stack installer creates all needed protocols and install them to
218  * mesh point device
219  */
220  mesh = MeshHelper::Default ();
221  if (!Mac48Address (m_root.c_str ()).IsBroadcast ())
222  {
224  }
225  else
226  {
227  //If root is not set, we do not use "Root" attribute, because it
228  //is specified only for 11s
230  }
231  if (m_chan)
232  {
233  mesh.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);
234  }
235  else
236  {
237  mesh.SetSpreadInterfaceChannels (MeshHelper::ZERO_CHANNEL);
238  }
239  mesh.SetMacType ("RandomStart", TimeValue (Seconds (m_randomStart)));
240  // Set number of interfaces - default is single-interface mesh point
242  // Install protocols and return container if MeshPointDevices
243  meshDevices = mesh.Install (wifiPhy, nodes);
244  // AssignStreams can optionally be used to control random variable streams
246  // Setup mobility - static grid topology
248  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
249  "MinX", DoubleValue (0.0),
250  "MinY", DoubleValue (0.0),
251  "DeltaX", DoubleValue (m_step),
252  "DeltaY", DoubleValue (m_step),
253  "GridWidth", UintegerValue (m_xSize),
254  "LayoutType", StringValue ("RowFirst"));
255  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
256  mobility.Install (nodes);
257  if (m_pcap)
258  wifiPhy.EnablePcapAll (std::string ("mp"));
259  if (m_ascii)
260  {
261  AsciiTraceHelper ascii;
262  wifiPhy.EnableAsciiAll (ascii.CreateFileStream ("mesh.tr"));
263  }
264 }
265 void
267 {
268  InternetStackHelper internetStack;
269  internetStack.Install (nodes);
271  address.SetBase ("10.1.1.0", "255.255.255.0");
272  interfaces = address.Assign (meshDevices);
273 }
274 void
276 {
277  uint16_t portNumber = 9;
278  UdpEchoServerHelper echoServer (portNumber);
279  uint16_t sinkNodeId = m_xSize * m_ySize - 1;
280  ApplicationContainer serverApps = echoServer.Install (nodes.Get (sinkNodeId));
281  serverApps.Start (Seconds (1.0));
282  serverApps.Stop (Seconds (m_totalTime + 1));
283  UdpEchoClientHelper echoClient (interfaces.GetAddress (sinkNodeId), portNumber);
284  echoClient.SetAttribute ("MaxPackets", UintegerValue ((uint32_t)(m_totalTime*(1/m_packetInterval))));
285  echoClient.SetAttribute ("Interval", TimeValue (Seconds (m_packetInterval)));
286  echoClient.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
288  Ptr<UdpEchoClient> app = clientApps.Get (0)->GetObject<UdpEchoClient> ();
289  app->TraceConnectWithoutContext ("Tx", MakeCallback (&TxTrace));
290  app->TraceConnectWithoutContext ("Rx", MakeCallback (&RxTrace));
291  clientApps.Start (Seconds (1.0));
292  clientApps.Stop (Seconds (m_totalTime + 1.5));
293 }
294 int
296 {
297  CreateNodes ();
300  Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this);
301  Simulator::Stop (Seconds (m_totalTime + 2));
302  Simulator::Run ();
303  Simulator::Destroy ();
304  std::cout << "UDP echo packets sent: " << g_udpTxCount << " received: " << g_udpRxCount << std::endl;
305  return 0;
306 }
307 void
309 {
310  unsigned n (0);
311  for (NetDeviceContainer::Iterator i = meshDevices.Begin (); i != meshDevices.End (); ++i, ++n)
312  {
313  std::ostringstream os;
314  os << "mp-report-" << n << ".xml";
315  std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str () << "\n";
316  std::ofstream of;
317  of.open (os.str ().c_str ());
318  if (!of.is_open ())
319  {
320  std::cerr << "Error: Can't open file " << os.str () << "\n";
321  return;
322  }
323  mesh.Report (*i, of);
324  of.close ();
325  }
326 }
327 int
328 main (int argc, char *argv[])
329 {
330  MeshTest t;
331  t.Configure (argc, argv);
332  return t.Run ();
333 }
MeshTest class.
Definition: mesh.cc:113
std::string m_root
root
Definition: mesh.cc:142
double m_step
step
Definition: mesh.cc:132
int m_ySize
Y size.
Definition: mesh.cc:131
bool m_pcap
PCAP.
Definition: mesh.cc:139
uint32_t m_nIfaces
number interfaces
Definition: mesh.cc:137
void InstallInternetStack()
Install internet m_stack on nodes.
Definition: mesh.cc:266
int m_xSize
X size.
Definition: mesh.cc:130
std::string m_stack
stack
Definition: mesh.cc:141
NodeContainer nodes
List of network nodes.
Definition: mesh.cc:144
Ipv4InterfaceContainer interfaces
Addresses of interfaces:
Definition: mesh.cc:148
uint16_t m_packetSize
packet size
Definition: mesh.cc:136
void CreateNodes()
Create nodes and setup their mobility.
Definition: mesh.cc:205
double m_packetInterval
packet interval
Definition: mesh.cc:135
bool m_ascii
ASCII.
Definition: mesh.cc:140
double m_randomStart
random start
Definition: mesh.cc:133
NetDeviceContainer meshDevices
List of all mesh point devices.
Definition: mesh.cc:146
void Report()
Print mesh devices diagnostics.
Definition: mesh.cc:308
bool m_chan
channel
Definition: mesh.cc:138
void Configure(int argc, char **argv)
Configure test from command line arguments.
Definition: mesh.cc:178
MeshHelper mesh
MeshHelper. Report is not static methods.
Definition: mesh.cc:150
int Run()
Run test.
Definition: mesh.cc:295
MeshTest()
Init test.
Definition: mesh.cc:161
double m_totalTime
total time
Definition: mesh.cc:134
void InstallApplication()
Install applications.
Definition: mesh.cc:275
holds a vector of ns3::Application pointers.
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...
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
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.
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
an EUI-48 address
Definition: mac48-address.h:44
bool IsBroadcast(void) const
AttributeValue implementation for Mac48Address.
Helper to create IEEE 802.11s mesh networks.
Definition: mesh-helper.h:44
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:130
void SetSpreadInterfaceChannels(ChannelPolicy policy)
set the channel policy
Definition: mesh-helper.cc:48
int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
Definition: mesh-helper.cc:235
NetDeviceContainer Install(const WifiPhyHelper &phyHelper, NodeContainer c) const
Install 802.11s mesh device & protocols on given node list.
Definition: mesh-helper.cc:86
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:53
void Report(const ns3::Ptr< ns3::NetDevice > &device, std::ostream &os)
Print statistics.
Definition: mesh-helper.cc:215
void SetNumberOfInterfaces(uint32_t nInterfaces)
Set a number of interfaces in a mesh network.
Definition: mesh-helper.cc:81
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Iterator Begin(void) const
Get an iterator which refers to the first NetDevice in the container.
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
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 ...
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.
A Udp Echo client.
Create a server application which waits for input UDP packets and sends them back to the original sen...
Hold an unsigned integer type.
Definition: uinteger.h:44
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create(void) const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
uint32_t g_udpTxCount
Definition: mesh.cc:90
void TxTrace(Ptr< const Packet > p)
Definition: mesh.cc:94
uint32_t g_udpRxCount
Definition: mesh.cc:91
void RxTrace(Ptr< const Packet > p)
Definition: mesh.cc:101
echoClient
Definition: first.py:56
address
Definition: first.py:44
serverApps
Definition: first.py:52
echoServer
Definition: first.py:50
clientApps
Definition: first.py:61
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
mobility
Definition: third.py:108
std::string m_stack