A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
mesh-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2008,2009 IITP RAS
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Kirill Andreev <andreev@iitp.ru>
7 *
8 *
9 * By default this script creates m_xSize * m_ySize square grid topology with
10 * IEEE802.11s stack installed at each node with peering management
11 * and HWMP protocol.
12 * The side of the square cell is defined by m_step parameter.
13 * When topology is created, UDP ping is installed to opposite corners
14 * by diagonals. packet size of the UDP ping and interval between two
15 * successive packets is configurable.
16 *
17 * m_xSize * step
18 * |<--------->|
19 * step
20 * |<--->|
21 * * --- * --- * <---Ping sink _
22 * | \ | / | ^
23 * | \ | / | |
24 * * --- * --- * m_ySize * step |
25 * | / | \ | |
26 * | / | \ | |
27 * * --- * --- * _
28 * ^ Ping source
29 *
30 * By varying m_xSize and m_ySize, one can configure the route that is used.
31 * When the inter-nodal distance is small, the source can reach the sink
32 * directly. When the inter-nodal distance is intermediate, the route
33 * selected is diagonal (two hop). When the inter-nodal distance is a bit
34 * larger, the diagonals cannot be used and a four-hop route is selected.
35 * When the distance is a bit larger, the packets will fail to reach even the
36 * adjacent nodes.
37 *
38 * As of ns-3.36 release, with default configuration (mesh uses Wi-Fi 802.11a
39 * standard and the ArfWifiManager rate control by default), the maximum
40 * range is roughly 50m. The default step size in this program is set to 50m,
41 * so any mesh packets in the above diagram depiction will not be received
42 * successfully on the diagonal hops between two nodes but only on the
43 * horizontal and vertical hops. If the step size is reduced to 35m, then
44 * the shortest path will be on the diagonal hops. If the step size is reduced
45 * to 17m or less, then the source will be able to reach the sink directly
46 * without any mesh hops (for the default 3x3 mesh depicted above).
47 *
48 * The position allocator will lay out the nodes in the following order
49 * (corresponding to Node ID and to the diagram above):
50 *
51 * 6 - 7 - 8
52 * | | |
53 * 3 - 4 - 5
54 * | | |
55 * 0 - 1 - 2
56 *
57 * See also MeshTest::Configure to read more about configurable
58 * parameters.
59 */
60
61#include "ns3/applications-module.h"
62#include "ns3/core-module.h"
63#include "ns3/internet-module.h"
64#include "ns3/mesh-helper.h"
65#include "ns3/mesh-module.h"
66#include "ns3/mobility-module.h"
67#include "ns3/network-module.h"
68#include "ns3/yans-wifi-helper.h"
69
70#include <fstream>
71#include <iostream>
72#include <sstream>
73
74using namespace ns3;
75
76NS_LOG_COMPONENT_DEFINE("MeshExample");
77
78// Declaring these variables outside of main() for use in trace sinks
79uint32_t g_udpTxCount = 0; //!< Rx packet counter.
80uint32_t g_udpRxCount = 0; //!< Tx packet counter.
81
82/**
83 * Transmission trace sink.
84 *
85 * @param p The sent packet.
86 */
87void
89{
90 NS_LOG_DEBUG("Sent " << p->GetSize() << " bytes");
92}
93
94/**
95 * Reception trace sink,
96 *
97 * @param p The received packet.
98 */
99void
101{
102 NS_LOG_DEBUG("Received " << p->GetSize() << " bytes");
103 g_udpRxCount++;
104}
105
106/**
107 * @ingroup mesh
108 * @brief MeshTest class
109 */
111{
112 public:
113 /// Init test
114 MeshTest();
115 /**
116 * Configure test from command line arguments
117 *
118 * @param argc command line argument count
119 * @param argv command line arguments
120 */
121 void Configure(int argc, char** argv);
122 /**
123 * Run test
124 * @returns the test status
125 */
126 int Run();
127
128 private:
129 int m_xSize; ///< X size
130 int m_ySize; ///< Y size
131 double m_step; ///< step
132 double m_randomStart; ///< random start
133 double m_totalTime; ///< total time
134 double m_packetInterval; ///< packet interval
135 uint16_t m_packetSize; ///< packet size
136 uint32_t m_nIfaces; ///< number interfaces
137 bool m_chan; ///< channel
138 bool m_pcap; ///< PCAP
139 bool m_ascii; ///< ASCII
140 std::string m_stack; ///< stack
141 std::string m_root; ///< root
142 /// List of network nodes
144 /// List of all mesh point devices
146 /// Addresses of interfaces:
148 /// MeshHelper. Report is not static methods
150
151 private:
152 /// Create nodes and setup their mobility
153 void CreateNodes();
154 /// Install internet m_stack on nodes
156 /// Install applications
157 void InstallApplication();
158 /// Print mesh devices diagnostics
159 void Report();
160};
161
163 : m_xSize(3),
164 m_ySize(3),
165 m_step(50.0),
166 m_randomStart(0.1),
167 m_totalTime(100.0),
168 m_packetInterval(1),
169 m_packetSize(1024),
170 m_nIfaces(1),
171 m_chan(true),
172 m_pcap(false),
173 m_ascii(false),
174 m_stack("ns3::Dot11sStack"),
175 m_root("ff:ff:ff:ff:ff:ff")
176{
177}
178
179void
180MeshTest::Configure(int argc, char* argv[])
181{
182 CommandLine cmd(__FILE__);
183 cmd.AddValue("x-size", "Number of nodes in a row grid", m_xSize);
184 cmd.AddValue("y-size", "Number of rows in a grid", m_ySize);
185 cmd.AddValue("step", "Size of edge in our grid (meters)", m_step);
186 // Avoid starting all mesh nodes at the same time (beacons may collide)
187 cmd.AddValue("start", "Maximum random start delay for beacon jitter (sec)", m_randomStart);
188 cmd.AddValue("time", "Simulation time (sec)", m_totalTime);
189 cmd.AddValue("packet-interval", "Interval between packets in UDP ping (sec)", m_packetInterval);
190 cmd.AddValue("packet-size", "Size of packets in UDP ping (bytes)", m_packetSize);
191 cmd.AddValue("interfaces", "Number of radio interfaces used by each mesh point", m_nIfaces);
192 cmd.AddValue("channels", "Use different frequency channels for different interfaces", m_chan);
193 cmd.AddValue("pcap", "Enable PCAP traces on interfaces", m_pcap);
194 cmd.AddValue("ascii", "Enable Ascii traces on interfaces", m_ascii);
195 cmd.AddValue("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
196 cmd.AddValue("root", "Mac address of root mesh point in HWMP", m_root);
197
198 cmd.Parse(argc, argv);
199 NS_LOG_DEBUG("Grid:" << m_xSize << "*" << m_ySize);
200 NS_LOG_DEBUG("Simulation time: " << m_totalTime << " s");
201 if (m_ascii)
202 {
204 }
205}
206
207void
209{
210 /*
211 * Create m_ySize*m_xSize stations to form a grid topology
212 */
214 // Configure YansWifiChannel
215 YansWifiPhyHelper wifiPhy;
217 wifiPhy.SetChannel(wifiChannel.Create());
218 /*
219 * Create mesh helper and set stack installer to it
220 * Stack installer creates all needed protocols and install them to
221 * mesh point device
222 */
224 if (!Mac48Address(m_root.c_str()).IsBroadcast())
225 {
227 }
228 else
229 {
230 // If root is not set, we do not use "Root" attribute, because it
231 // is specified only for 11s
233 }
234 if (m_chan)
235 {
237 }
238 else
239 {
241 }
243 // Set number of interfaces - default is single-interface mesh point
245 // Install protocols and return container if MeshPointDevices
246 meshDevices = mesh.Install(wifiPhy, nodes);
247 // AssignStreams can optionally be used to control random variable streams
249 // Setup mobility - static grid topology
250 MobilityHelper mobility;
251 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
252 "MinX",
253 DoubleValue(0.0),
254 "MinY",
255 DoubleValue(0.0),
256 "DeltaX",
258 "DeltaY",
260 "GridWidth",
262 "LayoutType",
263 StringValue("RowFirst"));
264 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
265 mobility.Install(nodes);
266 if (m_pcap)
267 {
268 wifiPhy.EnablePcapAll(std::string("mp"));
269 }
270 if (m_ascii)
271 {
272 AsciiTraceHelper ascii;
273 wifiPhy.EnableAsciiAll(ascii.CreateFileStream("mesh.tr"));
274 }
275}
276
277void
279{
280 InternetStackHelper internetStack;
281 internetStack.Install(nodes);
282 Ipv4AddressHelper address;
283 address.SetBase("10.1.1.0", "255.255.255.0");
284 interfaces = address.Assign(meshDevices);
285}
286
287void
289{
290 uint16_t portNumber = 9;
291 UdpEchoServerHelper echoServer(portNumber);
292 uint16_t sinkNodeId = m_xSize * m_ySize - 1;
293 ApplicationContainer serverApps = echoServer.Install(nodes.Get(sinkNodeId));
294 serverApps.Start(Seconds(1));
295 serverApps.Stop(Seconds(m_totalTime + 1));
296 UdpEchoClientHelper echoClient(interfaces.GetAddress(sinkNodeId), portNumber);
297 echoClient.SetAttribute("MaxPackets",
299 echoClient.SetAttribute("Interval", TimeValue(Seconds(m_packetInterval)));
300 echoClient.SetAttribute("PacketSize", UintegerValue(m_packetSize));
301 ApplicationContainer clientApps = echoClient.Install(nodes.Get(0));
302 Ptr<UdpEchoClient> app = clientApps.Get(0)->GetObject<UdpEchoClient>();
303 app->TraceConnectWithoutContext("Tx", MakeCallback(&TxTrace));
304 app->TraceConnectWithoutContext("Rx", MakeCallback(&RxTrace));
305 clientApps.Start(Seconds(1));
306 clientApps.Stop(Seconds(m_totalTime + 1.5));
307}
308
309int
311{
312 CreateNodes();
319 std::cout << "UDP echo packets sent: " << g_udpTxCount << " received: " << g_udpRxCount
320 << std::endl;
321 return 0;
322}
323
324void
326{
327 unsigned n(0);
328 for (auto i = meshDevices.Begin(); i != meshDevices.End(); ++i, ++n)
329 {
330 std::ostringstream os;
331 os << "mp-report-" << n << ".xml";
332 std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str() << "\n";
333 std::ofstream of;
334 of.open(os.str().c_str());
335 if (!of.is_open())
336 {
337 std::cerr << "Error: Can't open file " << os.str() << "\n";
338 return;
339 }
340 mesh.Report(*i, of);
341 of.close();
342 }
343}
344
345int
346main(int argc, char* argv[])
347{
348 MeshTest t;
349 t.Configure(argc, argv);
350 return t.Run();
351}
MeshTest class.
std::string m_root
root
double m_step
step
int m_ySize
Y size.
bool m_pcap
PCAP.
uint32_t m_nIfaces
number interfaces
void InstallInternetStack()
Install internet m_stack on nodes.
int m_xSize
X size.
std::string m_stack
stack
NodeContainer nodes
List of network nodes.
Ipv4InterfaceContainer interfaces
Addresses of interfaces:
uint16_t m_packetSize
packet size
void CreateNodes()
Create nodes and setup their mobility.
double m_packetInterval
packet interval
bool m_ascii
ASCII.
double m_randomStart
random start
NetDeviceContainer meshDevices
List of all mesh point devices.
void Report()
Print mesh devices diagnostics.
bool m_chan
channel
void Configure(int argc, char **argv)
Configure test from command line arguments.
MeshHelper mesh
MeshHelper. Report is not static methods.
int Run()
Run test.
MeshTest()
Init test.
double m_totalTime
total time
void InstallApplication()
Install applications.
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.
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.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
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
bool IsBroadcast() const
AttributeValue implementation for Mac48Address.
Helper to create IEEE 802.11s mesh networks.
Definition mesh-helper.h:33
void SetSpreadInterfaceChannels(ChannelPolicy policy)
set the channel policy
void SetStackInstaller(std::string type, Ts &&... args)
Set the MeshStack type to use.
int64_t AssignStreams(NetDeviceContainer c, int64_t stream)
Assign a fixed random variable stream number to the random variables used by this model.
static MeshHelper Default()
Set the helper to the default values for the MAC type, remote station manager and channel policy.
void SetMacType(Ts &&... args)
Set the Mac Attributes.
NetDeviceContainer Install(const WifiPhyHelper &phyHelper, NodeContainer c) const
Install 802.11s mesh device & protocols on given node list.
void Report(const ns3::Ptr< ns3::NetDevice > &device, std::ostream &os)
Print statistics.
void SetNumberOfInterfaces(uint32_t nInterfaces)
Set a number of interfaces in a mesh network.
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.
static void Enable()
Enable the packet metadata.
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 ...
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
AttributeValue implementation for Time.
Definition nstime.h:1431
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:34
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
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:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
uint32_t g_udpTxCount
Rx packet counter.
void TxTrace(Ptr< const Packet > p)
Transmission trace sink.
uint32_t g_udpRxCount
Tx packet counter.
void RxTrace(Ptr< const Packet > p)
Reception trace sink,.
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:684