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
85using namespace ns3;
86
87NS_LOG_COMPONENT_DEFINE ("MeshExample");
88
89// Declaring these variables outside of main() for use in trace sinks
92
98void
100{
101 NS_LOG_DEBUG ("Sent " << p->GetSize () << " bytes");
102 g_udpTxCount++;
103}
104
110void
112{
113 NS_LOG_DEBUG ("Received " << p->GetSize () << " bytes");
114 g_udpRxCount++;
115}
116
117
123{
124public:
126 MeshTest ();
133 void Configure (int argc, char ** argv);
138 int Run ();
139private:
142 double m_step;
144 double m_totalTime;
146 uint16_t m_packetSize;
148 bool m_chan;
149 bool m_pcap;
150 bool m_ascii;
151 std::string m_stack;
152 std::string m_root;
161private:
163 void CreateNodes ();
165 void InstallInternetStack ();
167 void InstallApplication ();
169 void Report ();
170};
172 m_xSize (3),
173 m_ySize (3),
174 m_step (50.0),
175 m_randomStart (0.1),
176 m_totalTime (100.0),
177 m_packetInterval (1),
178 m_packetSize (1024),
179 m_nIfaces (1),
180 m_chan (true),
181 m_pcap (false),
182 m_ascii (false),
183 m_stack ("ns3::Dot11sStack"),
184 m_root ("ff:ff:ff:ff:ff:ff")
185{
186}
187void
188MeshTest::Configure (int argc, char *argv[])
189{
190 CommandLine cmd (__FILE__);
191 cmd.AddValue ("x-size", "Number of nodes in a row grid", m_xSize);
192 cmd.AddValue ("y-size", "Number of rows in a grid", m_ySize);
193 cmd.AddValue ("step", "Size of edge in our grid (meters)", m_step);
194 // Avoid starting all mesh nodes at the same time (beacons may collide)
195 cmd.AddValue ("start", "Maximum random start delay for beacon jitter (sec)", m_randomStart);
196 cmd.AddValue ("time", "Simulation time (sec)", m_totalTime);
197 cmd.AddValue ("packet-interval", "Interval between packets in UDP ping (sec)", m_packetInterval);
198 cmd.AddValue ("packet-size", "Size of packets in UDP ping (bytes)", m_packetSize);
199 cmd.AddValue ("interfaces", "Number of radio interfaces used by each mesh point", m_nIfaces);
200 cmd.AddValue ("channels", "Use different frequency channels for different interfaces", m_chan);
201 cmd.AddValue ("pcap", "Enable PCAP traces on interfaces", m_pcap);
202 cmd.AddValue ("ascii", "Enable Ascii traces on interfaces", m_ascii);
203 cmd.AddValue ("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
204 cmd.AddValue ("root", "Mac address of root mesh point in HWMP", m_root);
205
206 cmd.Parse (argc, argv);
207 NS_LOG_DEBUG ("Grid:" << m_xSize << "*" << m_ySize);
208 NS_LOG_DEBUG ("Simulation time: " << m_totalTime << " s");
209 if (m_ascii)
210 {
211 PacketMetadata::Enable ();
212 }
213}
214void
216{
217 /*
218 * Create m_ySize*m_xSize stations to form a grid topology
219 */
221 // Configure YansWifiChannel
222 YansWifiPhyHelper wifiPhy;
223 YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
224 wifiPhy.SetChannel (wifiChannel.Create ());
225 /*
226 * Create mesh helper and set stack installer to it
227 * Stack installer creates all needed protocols and install them to
228 * mesh point device
229 */
230 mesh = MeshHelper::Default ();
231 if (!Mac48Address (m_root.c_str ()).IsBroadcast ())
232 {
234 }
235 else
236 {
237 //If root is not set, we do not use "Root" attribute, because it
238 //is specified only for 11s
240 }
241 if (m_chan)
242 {
243 mesh.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);
244 }
245 else
246 {
247 mesh.SetSpreadInterfaceChannels (MeshHelper::ZERO_CHANNEL);
248 }
249 mesh.SetMacType ("RandomStart", TimeValue (Seconds (m_randomStart)));
250 // Set number of interfaces - default is single-interface mesh point
252 // Install protocols and return container if MeshPointDevices
253 meshDevices = mesh.Install (wifiPhy, nodes);
254 // AssignStreams can optionally be used to control random variable streams
256 // Setup mobility - static grid topology
258 mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
259 "MinX", DoubleValue (0.0),
260 "MinY", DoubleValue (0.0),
261 "DeltaX", DoubleValue (m_step),
262 "DeltaY", DoubleValue (m_step),
263 "GridWidth", UintegerValue (m_xSize),
264 "LayoutType", StringValue ("RowFirst"));
265 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
266 mobility.Install (nodes);
267 if (m_pcap)
268 wifiPhy.EnablePcapAll (std::string ("mp"));
269 if (m_ascii)
270 {
271 AsciiTraceHelper ascii;
272 wifiPhy.EnableAsciiAll (ascii.CreateFileStream ("mesh.tr"));
273 }
274}
275void
277{
278 InternetStackHelper internetStack;
279 internetStack.Install (nodes);
281 address.SetBase ("10.1.1.0", "255.255.255.0");
282 interfaces = address.Assign (meshDevices);
283}
284void
286{
287 uint16_t portNumber = 9;
288 UdpEchoServerHelper echoServer (portNumber);
289 uint16_t sinkNodeId = m_xSize * m_ySize - 1;
290 ApplicationContainer serverApps = echoServer.Install (nodes.Get (sinkNodeId));
291 serverApps.Start (Seconds (1.0));
292 serverApps.Stop (Seconds (m_totalTime + 1));
293 UdpEchoClientHelper echoClient (interfaces.GetAddress (sinkNodeId), portNumber);
294 echoClient.SetAttribute ("MaxPackets", UintegerValue ((uint32_t)(m_totalTime*(1/m_packetInterval))));
295 echoClient.SetAttribute ("Interval", TimeValue (Seconds (m_packetInterval)));
296 echoClient.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
298 Ptr<UdpEchoClient> app = clientApps.Get (0)->GetObject<UdpEchoClient> ();
299 app->TraceConnectWithoutContext ("Tx", MakeCallback (&TxTrace));
300 app->TraceConnectWithoutContext ("Rx", MakeCallback (&RxTrace));
301 clientApps.Start (Seconds (1.0));
302 clientApps.Stop (Seconds (m_totalTime + 1.5));
303}
304int
306{
307 CreateNodes ();
310 Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this);
311 Simulator::Stop (Seconds (m_totalTime + 2));
313 Simulator::Destroy ();
314 std::cout << "UDP echo packets sent: " << g_udpTxCount << " received: " << g_udpRxCount << std::endl;
315 return 0;
316}
317void
319{
320 unsigned n (0);
321 for (NetDeviceContainer::Iterator i = meshDevices.Begin (); i != meshDevices.End (); ++i, ++n)
322 {
323 std::ostringstream os;
324 os << "mp-report-" << n << ".xml";
325 std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str () << "\n";
326 std::ofstream of;
327 of.open (os.str ().c_str ());
328 if (!of.is_open ())
329 {
330 std::cerr << "Error: Can't open file " << os.str () << "\n";
331 return;
332 }
333 mesh.Report (*i, of);
334 of.close ();
335 }
336}
337int
338main (int argc, char *argv[])
339{
340 MeshTest t;
341 t.Configure (argc, argv);
342 return t.Run ();
343}
void Run(ObjectFactory &factory, uint32_t pop, uint32_t total, uint32_t runs, Ptr< RandomVariableStream > eventStream, bool calRev)
Perform the runs for a single scheduler type.
MeshTest class.
Definition: mesh.cc:123
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:276
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:215
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:318
bool m_chan
channel
Definition: mesh.cc:148
void Configure(int argc, char **argv)
Configure test from command line arguments.
Definition: mesh.cc:188
MeshHelper mesh
MeshHelper. Report is not static methods.
Definition: mesh.cc:160
int Run()
Run test.
Definition: mesh.cc:305
MeshTest()
Init test.
Definition: mesh.cc:171
double m_totalTime
total time
Definition: mesh.cc:144
void InstallApplication()
Install applications.
Definition: mesh.cc:285
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 SetSpreadInterfaceChannels(ChannelPolicy policy)
set the channel policy
Definition: mesh-helper.cc:49
void SetStackInstaller(std::string type, Ts &&... args)
Set the MeshStack type to use.
Definition: mesh-helper.h:207
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:169
void SetMacType(Ts &&... args)
Set the Mac Attributes.
Definition: mesh-helper.h:194
NetDeviceContainer Install(const WifiPhyHelper &phyHelper, NodeContainer c) const
Install 802.11s mesh device & protocols on given node list.
Definition: mesh-helper.cc:60
void Report(const ns3::Ptr< ns3::NetDevice > &device, std::ostream &os)
Print statistics.
Definition: mesh-helper.cc:149
void SetNumberOfInterfaces(uint32_t nInterfaces)
Set a number of interfaces in a mesh network.
Definition: mesh-helper.cc:55
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
std::vector< Ptr< NetDevice > >::const_iterator Iterator
NetDevice container iterator.
Iterator End(void) const
Get an iterator which indicates past-the-last NetDevice in the container.
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:1309
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:1245
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:53
address
Definition: first.py:40
serverApps
Definition: first.py:48
echoServer
Definition: first.py:46
clientApps
Definition: first.py:58
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:661
cmd
Definition: second.py:43
mobility
Definition: third.py:98