A Discrete-Event Network Simulator
API
mesh.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008,2009 IITP RAS
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Kirill Andreev <andreev@iitp.ru>
18  *
19  *
20  * By default this script creates m_xSize * m_ySize square grid topology with
21  * IEEE802.11s stack installed at each node with peering management
22  * and HWMP protocol.
23  * The side of the square cell is defined by m_step parameter.
24  * When topology is created, UDP ping is installed to opposite corners
25  * by diagonals. packet size of the UDP ping and interval between two
26  * successive packets is configurable.
27  *
28  * m_xSize * step
29  * |<--------->|
30  * step
31  * |<--->|
32  * * --- * --- * <---Ping sink _
33  * | \ | / | ^
34  * | \ | / | |
35  * * --- * --- * m_ySize * step |
36  * | / | \ | |
37  * | / | \ | |
38  * * --- * --- * _
39  * ^ Ping source
40  *
41  * By varying m_xSize and m_ySize, one can configure the route that is used.
42  * When the inter-nodal distance is small, the source can reach the sink
43  * directly. When the inter-nodal distance is intermediate, the route
44  * selected is diagonal (two hop). When the inter-nodal distance is a bit
45  * larger, the diagonals cannot be used and a four-hop route is selected.
46  * When the distance is a bit larger, the packets will fail to reach even the
47  * adjacent nodes.
48  *
49  * As of ns-3.36 release, with default configuration (mesh uses Wi-Fi 802.11a
50  * standard and the ArfWifiManager rate control by default), the maximum
51  * range is roughly 50m. The default step size in this program is set to 50m,
52  * so any mesh packets in the above diagram depiction will not be received
53  * successfully on the diagonal hops between two nodes but only on the
54  * horizontal and vertical hops. If the step size is reduced to 35m, then
55  * the shortest path will be on the diagonal hops. If the step size is reduced
56  * to 17m or less, then the source will be able to reach the sink directly
57  * without any mesh hops (for the default 3x3 mesh depicted above).
58  *
59  * The position allocator will lay out the nodes in the following order
60  * (corresponding to Node ID and to the diagram above):
61  *
62  * 6 - 7 - 8
63  * | | |
64  * 3 - 4 - 5
65  * | | |
66  * 0 - 1 - 2
67  *
68  * See also MeshTest::Configure to read more about configurable
69  * parameters.
70  */
71 
72 #include "ns3/applications-module.h"
73 #include "ns3/core-module.h"
74 #include "ns3/internet-module.h"
75 #include "ns3/mesh-helper.h"
76 #include "ns3/mesh-module.h"
77 #include "ns3/mobility-module.h"
78 #include "ns3/network-module.h"
79 #include "ns3/yans-wifi-helper.h"
80 
81 #include <fstream>
82 #include <iostream>
83 #include <sstream>
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 
98 void
100 {
101  NS_LOG_DEBUG("Sent " << p->GetSize() << " bytes");
102  g_udpTxCount++;
103 }
104 
110 void
112 {
113  NS_LOG_DEBUG("Received " << p->GetSize() << " bytes");
114  g_udpRxCount++;
115 }
116 
121 class MeshTest
122 {
123  public:
125  MeshTest();
132  void Configure(int argc, char** argv);
137  int Run();
138 
139  private:
140  int m_xSize;
141  int m_ySize;
142  double m_step;
143  double m_randomStart;
144  double m_totalTime;
146  uint16_t m_packetSize;
147  uint32_t m_nIfaces;
148  bool m_chan;
149  bool m_pcap;
150  bool m_ascii;
151  std::string m_stack;
152  std::string m_root;
161 
162  private:
164  void CreateNodes();
166  void InstallInternetStack();
168  void InstallApplication();
170  void Report();
171 };
172 
174  : m_xSize(3),
175  m_ySize(3),
176  m_step(50.0),
177  m_randomStart(0.1),
178  m_totalTime(100.0),
179  m_packetInterval(1),
180  m_packetSize(1024),
181  m_nIfaces(1),
182  m_chan(true),
183  m_pcap(false),
184  m_ascii(false),
185  m_stack("ns3::Dot11sStack"),
186  m_root("ff:ff:ff:ff:ff:ff")
187 {
188 }
189 
190 void
191 MeshTest::Configure(int argc, char* argv[])
192 {
193  CommandLine cmd(__FILE__);
194  cmd.AddValue("x-size", "Number of nodes in a row grid", m_xSize);
195  cmd.AddValue("y-size", "Number of rows in a grid", m_ySize);
196  cmd.AddValue("step", "Size of edge in our grid (meters)", m_step);
197  // Avoid starting all mesh nodes at the same time (beacons may collide)
198  cmd.AddValue("start", "Maximum random start delay for beacon jitter (sec)", m_randomStart);
199  cmd.AddValue("time", "Simulation time (sec)", m_totalTime);
200  cmd.AddValue("packet-interval", "Interval between packets in UDP ping (sec)", m_packetInterval);
201  cmd.AddValue("packet-size", "Size of packets in UDP ping (bytes)", m_packetSize);
202  cmd.AddValue("interfaces", "Number of radio interfaces used by each mesh point", m_nIfaces);
203  cmd.AddValue("channels", "Use different frequency channels for different interfaces", m_chan);
204  cmd.AddValue("pcap", "Enable PCAP traces on interfaces", m_pcap);
205  cmd.AddValue("ascii", "Enable Ascii traces on interfaces", m_ascii);
206  cmd.AddValue("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
207  cmd.AddValue("root", "Mac address of root mesh point in HWMP", m_root);
208 
209  cmd.Parse(argc, argv);
210  NS_LOG_DEBUG("Grid:" << m_xSize << "*" << m_ySize);
211  NS_LOG_DEBUG("Simulation time: " << m_totalTime << " s");
212  if (m_ascii)
213  {
214  PacketMetadata::Enable();
215  }
216 }
217 
218 void
220 {
221  /*
222  * Create m_ySize*m_xSize stations to form a grid topology
223  */
225  // Configure YansWifiChannel
226  YansWifiPhyHelper wifiPhy;
227  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
228  wifiPhy.SetChannel(wifiChannel.Create());
229  /*
230  * Create mesh helper and set stack installer to it
231  * Stack installer creates all needed protocols and install them to
232  * mesh point device
233  */
234  mesh = MeshHelper::Default();
235  if (!Mac48Address(m_root.c_str()).IsBroadcast())
236  {
237  mesh.SetStackInstaller(m_stack, "Root", Mac48AddressValue(Mac48Address(m_root.c_str())));
238  }
239  else
240  {
241  // If root is not set, we do not use "Root" attribute, because it
242  // is specified only for 11s
244  }
245  if (m_chan)
246  {
247  mesh.SetSpreadInterfaceChannels(MeshHelper::SPREAD_CHANNELS);
248  }
249  else
250  {
251  mesh.SetSpreadInterfaceChannels(MeshHelper::ZERO_CHANNEL);
252  }
253  mesh.SetMacType("RandomStart", TimeValue(Seconds(m_randomStart)));
254  // Set number of interfaces - default is single-interface mesh point
256  // Install protocols and return container if MeshPointDevices
257  meshDevices = mesh.Install(wifiPhy, nodes);
258  // AssignStreams can optionally be used to control random variable streams
260  // Setup mobility - static grid topology
262  mobility.SetPositionAllocator("ns3::GridPositionAllocator",
263  "MinX",
264  DoubleValue(0.0),
265  "MinY",
266  DoubleValue(0.0),
267  "DeltaX",
269  "DeltaY",
271  "GridWidth",
273  "LayoutType",
274  StringValue("RowFirst"));
275  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
276  mobility.Install(nodes);
277  if (m_pcap)
278  {
279  wifiPhy.EnablePcapAll(std::string("mp"));
280  }
281  if (m_ascii)
282  {
283  AsciiTraceHelper ascii;
284  wifiPhy.EnableAsciiAll(ascii.CreateFileStream("mesh.tr"));
285  }
286 }
287 
288 void
290 {
291  InternetStackHelper internetStack;
292  internetStack.Install(nodes);
294  address.SetBase("10.1.1.0", "255.255.255.0");
295  interfaces = address.Assign(meshDevices);
296 }
297 
298 void
300 {
301  uint16_t portNumber = 9;
302  UdpEchoServerHelper echoServer(portNumber);
303  uint16_t sinkNodeId = m_xSize * m_ySize - 1;
304  ApplicationContainer serverApps = echoServer.Install(nodes.Get(sinkNodeId));
305  serverApps.Start(Seconds(1.0));
306  serverApps.Stop(Seconds(m_totalTime + 1));
307  UdpEchoClientHelper echoClient(interfaces.GetAddress(sinkNodeId), portNumber);
308  echoClient.SetAttribute("MaxPackets",
309  UintegerValue((uint32_t)(m_totalTime * (1 / m_packetInterval))));
310  echoClient.SetAttribute("Interval", TimeValue(Seconds(m_packetInterval)));
311  echoClient.SetAttribute("PacketSize", UintegerValue(m_packetSize));
313  Ptr<UdpEchoClient> app = clientApps.Get(0)->GetObject<UdpEchoClient>();
314  app->TraceConnectWithoutContext("Tx", MakeCallback(&TxTrace));
315  app->TraceConnectWithoutContext("Rx", MakeCallback(&RxTrace));
316  clientApps.Start(Seconds(1.0));
317  clientApps.Stop(Seconds(m_totalTime + 1.5));
318 }
319 
320 int
322 {
323  CreateNodes();
326  Simulator::Schedule(Seconds(m_totalTime), &MeshTest::Report, this);
327  Simulator::Stop(Seconds(m_totalTime + 2));
328  Simulator::Run();
329  Simulator::Destroy();
330  std::cout << "UDP echo packets sent: " << g_udpTxCount << " received: " << g_udpRxCount
331  << std::endl;
332  return 0;
333 }
334 
335 void
337 {
338  unsigned n(0);
339  for (auto i = meshDevices.Begin(); i != meshDevices.End(); ++i, ++n)
340  {
341  std::ostringstream os;
342  os << "mp-report-" << n << ".xml";
343  std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str() << "\n";
344  std::ofstream of;
345  of.open(os.str().c_str());
346  if (!of.is_open())
347  {
348  std::cerr << "Error: Can't open file " << os.str() << "\n";
349  return;
350  }
351  mesh.Report(*i, of);
352  of.close();
353  }
354 }
355 
356 int
357 main(int argc, char* argv[])
358 {
359  MeshTest t;
360  t.Configure(argc, argv);
361  return t.Run();
362 }
MeshTest class.
Definition: mesh.cc:122
std::string m_root
root
Definition: mesh.cc:152
double m_step
step
Definition: mesh.cc:142
int m_ySize
Y size.
Definition: mesh.cc:141
bool m_pcap
PCAP.
Definition: mesh.cc:149
uint32_t m_nIfaces
number interfaces
Definition: mesh.cc:147
void InstallInternetStack()
Install internet m_stack on nodes.
Definition: mesh.cc:289
int m_xSize
X size.
Definition: mesh.cc:140
std::string m_stack
stack
Definition: mesh.cc:151
NodeContainer nodes
List of network nodes.
Definition: mesh.cc:154
Ipv4InterfaceContainer interfaces
Addresses of interfaces:
Definition: mesh.cc:158
uint16_t m_packetSize
packet size
Definition: mesh.cc:146
void CreateNodes()
Create nodes and setup their mobility.
Definition: mesh.cc:219
double m_packetInterval
packet interval
Definition: mesh.cc:145
bool m_ascii
ASCII.
Definition: mesh.cc:150
double m_randomStart
random start
Definition: mesh.cc:143
NetDeviceContainer meshDevices
List of all mesh point devices.
Definition: mesh.cc:156
void Report()
Print mesh devices diagnostics.
Definition: mesh.cc:336
bool m_chan
channel
Definition: mesh.cc:148
void Configure(int argc, char **argv)
Configure test from command line arguments.
Definition: mesh.cc:191
MeshHelper mesh
MeshHelper. Report is not static methods.
Definition: mesh.cc:160
int Run()
Run test.
Definition: mesh.cc:321
MeshTest()
Init test.
Definition: mesh.cc:173
double m_totalTime
total time
Definition: mesh.cc:144
void InstallApplication()
Install applications.
Definition: mesh.cc:299
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:174
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:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
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:46
bool IsBroadcast() const
Helper to create IEEE 802.11s mesh networks.
Definition: mesh-helper.h:44
void SetSpreadInterfaceChannels(ChannelPolicy policy)
set the channel policy
Definition: mesh-helper.cc:51
void SetStackInstaller(std::string type, Ts &&... args)
Set the MeshStack type to use.
Definition: mesh-helper.h:210
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:178
void SetMacType(Ts &&... args)
Set the Mac Attributes.
Definition: mesh-helper.h:195
NetDeviceContainer Install(const WifiPhyHelper &phyHelper, NodeContainer c) const
Install 802.11s mesh device & protocols on given node list.
Definition: mesh-helper.cc:63
void Report(const ns3::Ptr< ns3::NetDevice > &device, std::ostream &os)
Print statistics.
Definition: mesh-helper.cc:156
void SetNumberOfInterfaces(uint32_t nInterfaces)
Set a number of interfaces in a mesh network.
Definition: mesh-helper.cc:57
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
Iterator Begin() const
Get an iterator which refers to the first NetDevice in the container.
Iterator End() const
Get an iterator which indicates past-the-last 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() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
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:56
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:45
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create() 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:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
uint32_t g_udpTxCount
Rx packet counter.
Definition: mesh.cc:90
void TxTrace(Ptr< const Packet > p)
Transmission trace sink.
Definition: mesh.cc:99
uint32_t g_udpRxCount
Tx packet counter.
Definition: mesh.cc:91
void RxTrace(Ptr< const Packet > p)
Reception trace sink,.
Definition: mesh.cc:111
echoClient
Definition: first.py:59
address
Definition: first.py:47
serverApps
Definition: first.py:54
echoServer
Definition: first.py:52
clientApps
Definition: first.py:64
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
cmd
Definition: second.py:40
mobility
Definition: third.py:105