A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
mixed-wireless.cc
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/*
3
* This program is free software; you can redistribute it and/or modify
4
* it under the terms of the GNU General Public License version 2 as
5
* published by the Free Software Foundation;
6
*
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
11
*
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15
*
16
*/
17
18
//
19
// This ns-3 example demonstrates the use of helper functions to ease
20
// the construction of simulation scenarios.
21
//
22
// The simulation topology consists of a mixed wired and wireless
23
// scenario in which a hierarchical mobility model is used.
24
//
25
// The simulation layout consists of N backbone routers interconnected
26
// by an ad hoc wifi network.
27
// Each backbone router also has a local 802.11 network and is connected
28
// to a local LAN. An additional set of (K-1) nodes are connected to
29
// this backbone. Finally, a local LAN is connected to each router
30
// on the backbone, with L-1 additional hosts.
31
//
32
// The nodes are populated with TCP/IP stacks, and OLSR unicast routing
33
// on the backbone. An example UDP transfer is shown. The simulator
34
// be configured to output tcpdumps or traces from different nodes.
35
//
36
//
37
// +--------------------------------------------------------+
38
// | |
39
// | 802.11 ad hoc, ns-2 mobility |
40
// | |
41
// +--------------------------------------------------------+
42
// | o o o (N backbone routers) |
43
// +--------+ +--------+
44
// wired LAN | mobile | wired LAN | mobile |
45
// -----------| router | -----------| router |
46
// --------- ---------
47
// | |
48
// +----------------+ +----------------+
49
// | 802.11 | | 802.11 |
50
// | infra net | | infra net |
51
// | K-1 hosts | | K-1 hosts |
52
// +----------------+ +----------------+
53
//
54
// We'll send data from the first wired LAN node on the first wired LAN
55
// to the last wireless STA on the last infrastructure net, thereby
56
// causing packets to traverse CSMA to adhoc to infrastructure links
57
//
58
// Note that certain mobility patterns may cause packet forwarding
59
// to fail (if nodes become disconnected)
60
61
#include <fstream>
62
#include <string>
63
#include "ns3/core-module.h"
64
#include "ns3/network-module.h"
65
#include "ns3/applications-module.h"
66
#include "ns3/mobility-module.h"
67
#include "ns3/config-store-module.h"
68
#include "ns3/wifi-module.h"
69
#include "ns3/csma-module.h"
70
#include "ns3/olsr-helper.h"
71
#include "ns3/internet-module.h"
72
73
using namespace
ns3;
74
75
//
76
// Define logging keyword for this file
77
//
78
NS_LOG_COMPONENT_DEFINE
(
"MixedWireless"
);
79
80
//
81
// This function will be used below as a trace sink, if the command-line
82
// argument or default value "useCourseChangeCallback" is set to true
83
//
84
static
void
85
CourseChangeCallback
(std::string path,
Ptr<const MobilityModel>
model)
86
{
87
Vector
position = model->
GetPosition
();
88
std::cout <<
"CourseChange "
<< path <<
" x="
<< position.
x
<<
", y="
<< position.
y
<<
", z="
<< position.
z
<< std::endl;
89
}
90
91
int
92
main
(
int
argc,
char
*argv[])
93
{
94
//
95
// First, we declare and initialize a few local variables that control some
96
// simulation parameters.
97
//
98
uint32_t backboneNodes = 10;
99
uint32_t infraNodes = 5;
100
uint32_t lanNodes = 5;
101
uint32_t
stopTime
= 10;
102
bool
useCourseChangeCallback =
false
;
103
bool
enableTracing =
false
;
104
105
//
106
// Simulation defaults are typically set next, before command line
107
// arguments are parsed.
108
//
109
Config::SetDefault
(
"ns3::OnOffApplication::PacketSize"
,
StringValue
(
"210"
));
110
Config::SetDefault
(
"ns3::OnOffApplication::DataRate"
,
StringValue
(
"10kb/s"
));
111
112
//
113
// For convenience, we add the local variables to the command line argument
114
// system so that they can be overridden with flags such as
115
// "--backboneNodes=20"
116
//
117
CommandLine
cmd;
118
cmd.
AddValue
(
"backboneNodes"
,
"number of backbone nodes"
, backboneNodes);
119
cmd.
AddValue
(
"infraNodes"
,
"number of leaf nodes"
, infraNodes);
120
cmd.
AddValue
(
"lanNodes"
,
"number of LAN nodes"
, lanNodes);
121
cmd.
AddValue
(
"stopTime"
,
"simulation stop time (seconds)"
, stopTime);
122
cmd.
AddValue
(
"useCourseChangeCallback"
,
"whether to enable course change tracing"
, useCourseChangeCallback);
123
cmd.
AddValue
(
"enableTracing"
,
"enable tracing"
, enableTracing);
124
125
//
126
// The system global variables and the local values added to the argument
127
// system can be overridden by command line arguments by using this call.
128
//
129
cmd.
Parse
(argc, argv);
130
132
// //
133
// Construct the backbone //
134
// //
136
137
//
138
// Create a container to manage the nodes of the adhoc (backbone) network.
139
// Later we'll create the rest of the nodes we'll need.
140
//
141
NodeContainer
backbone;
142
backbone.
Create
(backboneNodes);
143
//
144
// Create the backbone wifi net devices and install them into the nodes in
145
// our container
146
//
147
WifiHelper
wifi;
148
NqosWifiMacHelper
mac =
NqosWifiMacHelper::Default
();
149
mac.
SetType
(
"ns3::AdhocWifiMac"
);
150
wifi.
SetRemoteStationManager
(
"ns3::ConstantRateWifiManager"
,
151
"DataMode"
,
StringValue
(
"OfdmRate54Mbps"
));
152
YansWifiPhyHelper
wifiPhy =
YansWifiPhyHelper::Default
();
153
YansWifiChannelHelper
wifiChannel =
YansWifiChannelHelper::Default
();
154
wifiPhy.
SetChannel
(wifiChannel.
Create
());
155
NetDeviceContainer
backboneDevices = wifi.
Install
(wifiPhy, mac, backbone);
156
157
// We enable OLSR (which will be consulted at a higher priority than
158
// the global routing) on the backbone ad hoc nodes
159
NS_LOG_INFO
(
"Enabling OLSR routing on all backbone nodes"
);
160
OlsrHelper
olsr;
161
//
162
// Add the IPv4 protocol stack to the nodes in our container
163
//
164
InternetStackHelper
internet;
165
internet.
SetRoutingHelper
(olsr);
// has effect on the next Install ()
166
internet.
Install
(backbone);
167
168
// re-initialize for non-olsr routing.
169
internet.
Reset
();
170
171
//
172
// Assign IPv4 addresses to the device drivers (actually to the associated
173
// IPv4 interfaces) we just created.
174
//
175
Ipv4AddressHelper
ipAddrs;
176
ipAddrs.
SetBase
(
"192.168.0.0"
,
"255.255.255.0"
);
177
ipAddrs.
Assign
(backboneDevices);
178
179
//
180
// The ad-hoc network nodes need a mobility model so we aggregate one to
181
// each of the nodes we just finished building.
182
//
183
MobilityHelper
mobility;
184
Ptr<ListPositionAllocator>
positionAlloc =
185
CreateObject<ListPositionAllocator> ();
186
double
x
= 0.0;
187
for
(uint32_t i = 0; i < backboneNodes; ++i)
188
{
189
positionAlloc->
Add
(
Vector
(x, 0.0, 0.0));
190
x += 5.0;
191
}
192
mobility.
SetPositionAllocator
(positionAlloc);
193
mobility.
SetMobilityModel
(
"ns3::RandomDirection2dMobilityModel"
,
194
"Bounds"
,
RectangleValue
(
Rectangle
(0, 20, 0, 20)),
195
"Speed"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=2]"
),
196
"Pause"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=0.2]"
));
197
mobility.
Install
(backbone);
198
200
// //
201
// Construct the LANs //
202
// //
204
205
// Reset the address base-- all of the CSMA networks will be in
206
// the "172.16 address space
207
ipAddrs.
SetBase
(
"172.16.0.0"
,
"255.255.255.0"
);
208
209
210
for
(uint32_t i = 0; i < backboneNodes; ++i)
211
{
212
NS_LOG_INFO
(
"Configuring local area network for backbone node "
<< i);
213
//
214
// Create a container to manage the nodes of the LAN. We need
215
// two containers here; one with all of the new nodes, and one
216
// with all of the nodes including new and existing nodes
217
//
218
NodeContainer
newLanNodes;
219
newLanNodes.
Create
(lanNodes - 1);
220
// Now, create the container with all nodes on this link
221
NodeContainer
lan (backbone.
Get
(i), newLanNodes);
222
//
223
// Create the CSMA net devices and install them into the nodes in our
224
// collection.
225
//
226
CsmaHelper
csma;
227
csma.
SetChannelAttribute
(
"DataRate"
,
228
DataRateValue
(
DataRate
(5000000)));
229
csma.SetChannelAttribute (
"Delay"
,
TimeValue
(MilliSeconds (2)));
230
NetDeviceContainer
lanDevices = csma.Install (lan);
231
//
232
// Add the IPv4 protocol stack to the new LAN nodes
233
//
234
internet.
Install
(newLanNodes);
235
//
236
// Assign IPv4 addresses to the device drivers (actually to the
237
// associated IPv4 interfaces) we just created.
238
//
239
ipAddrs.
Assign
(lanDevices);
240
//
241
// Assign a new network prefix for the next LAN, according to the
242
// network mask initialized above
243
//
244
ipAddrs.
NewNetwork
();
245
}
246
248
// //
249
// Construct the mobile networks //
250
// //
252
253
// Reset the address base-- all of the 802.11 networks will be in
254
// the "10.0" address space
255
ipAddrs.
SetBase
(
"10.0.0.0"
,
"255.255.255.0"
);
256
257
for
(uint32_t i = 0; i < backboneNodes; ++i)
258
{
259
NS_LOG_INFO
(
"Configuring wireless network for backbone node "
<< i);
260
//
261
// Create a container to manage the nodes of the LAN. We need
262
// two containers here; one with all of the new nodes, and one
263
// with all of the nodes including new and existing nodes
264
//
265
NodeContainer
stas;
266
stas.
Create
(infraNodes - 1);
267
// Now, create the container with all nodes on this link
268
NodeContainer
infra (backbone.
Get
(i), stas);
269
//
270
// Create an infrastructure network
271
//
272
WifiHelper
wifiInfra =
WifiHelper::Default
();
273
NqosWifiMacHelper
macInfra =
NqosWifiMacHelper::Default
();
274
wifiPhy.
SetChannel
(wifiChannel.
Create
());
275
// Create unique ssids for these networks
276
std::string ssidString (
"wifi-infra"
);
277
std::stringstream ss;
278
ss << i;
279
ssidString += ss.str ();
280
Ssid
ssid =
Ssid
(ssidString);
281
wifiInfra.SetRemoteStationManager (
"ns3::ArfWifiManager"
);
282
// setup stas
283
macInfra.
SetType
(
"ns3::StaWifiMac"
,
284
"Ssid"
,
SsidValue
(ssid),
285
"ActiveProbing"
,
BooleanValue
(
false
));
286
NetDeviceContainer
staDevices = wifiInfra.Install (wifiPhy, macInfra, stas);
287
// setup ap.
288
macInfra.
SetType
(
"ns3::ApWifiMac"
,
289
"Ssid"
,
SsidValue
(ssid));
290
NetDeviceContainer
apDevices = wifiInfra.Install (wifiPhy, macInfra, backbone.
Get
(i));
291
// Collect all of these new devices
292
NetDeviceContainer
infraDevices (apDevices, staDevices);
293
294
// Add the IPv4 protocol stack to the nodes in our container
295
//
296
internet.
Install
(stas);
297
//
298
// Assign IPv4 addresses to the device drivers (actually to the associated
299
// IPv4 interfaces) we just created.
300
//
301
ipAddrs.
Assign
(infraDevices);
302
//
303
// Assign a new network prefix for each mobile network, according to
304
// the network mask initialized above
305
//
306
ipAddrs.
NewNetwork
();
307
//
308
// The new wireless nodes need a mobility model so we aggregate one
309
// to each of the nodes we just finished building.
310
//
311
Ptr<ListPositionAllocator>
subnetAlloc =
312
CreateObject<ListPositionAllocator> ();
313
for
(uint32_t j = 0; j < infra.GetN (); ++j)
314
{
315
subnetAlloc->
Add
(
Vector
(0.0, j, 0.0));
316
}
317
mobility.
PushReferenceMobilityModel
(backbone.
Get
(i));
318
mobility.
SetPositionAllocator
(subnetAlloc);
319
mobility.
SetMobilityModel
(
"ns3::RandomDirection2dMobilityModel"
,
320
"Bounds"
,
RectangleValue
(
Rectangle
(-10, 10, -10, 10)),
321
"Speed"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=3]"
),
322
"Pause"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=0.4]"
));
323
mobility.
Install
(infra);
324
}
326
// //
327
// Routing configuration //
328
// //
330
331
// The below global routing does not take into account wireless effects.
332
// However, it is useful for setting default routes for all of the nodes
333
// such as the LAN nodes.
334
NS_LOG_INFO
(
"Enabling global routing on all nodes"
);
335
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
336
338
// //
339
// Application configuration //
340
// //
342
343
// Create the OnOff application to send UDP datagrams of size
344
// 210 bytes at a rate of 10 Kb/s, between two nodes
345
// We'll send data from the first wired LAN node on the first wired LAN
346
// to the last wireless STA on the last infrastructure net, thereby
347
// causing packets to traverse CSMA to adhoc to infrastructure links
348
349
NS_LOG_INFO
(
"Create Applications."
);
350
uint16_t
port
= 9;
// Discard port (RFC 863)
351
352
// Let's make sure that the user does not define too few nodes
353
// to make this example work. We need lanNodes > 1 and infraNodes > 1
354
NS_ASSERT
(lanNodes > 1 && infraNodes > 1);
355
// We want the source to be the first node created outside of the backbone
356
// Conveniently, the variable "backboneNodes" holds this node index value
357
Ptr<Node>
appSource =
NodeList::GetNode
(backboneNodes);
358
// We want the sink to be the last node created in the topology.
359
uint32_t lastNodeIndex = backboneNodes + backboneNodes*(lanNodes - 1) + backboneNodes*(infraNodes - 1) - 1;
360
Ptr<Node>
appSink =
NodeList::GetNode
(lastNodeIndex);
361
// Let's fetch the IP address of the last node, which is on Ipv4Interface 1
362
Ipv4Address
remoteAddr = appSink->
GetObject
<
Ipv4
> ()->GetAddress (1, 0).GetLocal ();
363
364
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
365
Address
(
InetSocketAddress
(remoteAddr, port)));
366
onoff.
SetConstantRate
(
DataRate
(
"10kb/s"
));
367
368
ApplicationContainer
apps = onoff.
Install
(appSource);
369
apps.
Start
(Seconds (3.0));
370
apps.
Stop
(Seconds (20.0));
371
372
// Create a packet sink to receive these packets
373
PacketSinkHelper
sink (
"ns3::UdpSocketFactory"
,
374
InetSocketAddress
(
Ipv4Address::GetAny
(), port));
375
apps = sink.
Install
(appSink);
376
apps.
Start
(Seconds (3.0));
377
379
// //
380
// Tracing configuration //
381
// //
383
384
NS_LOG_INFO
(
"Configure Tracing."
);
385
if
(enableTracing ==
true
)
386
{
387
CsmaHelper
csma;
388
389
//
390
// Let's set up some ns-2-like ascii traces, using another helper class
391
//
392
AsciiTraceHelper
ascii;
393
Ptr<OutputStreamWrapper>
stream = ascii.
CreateFileStream
(
"mixed-wireless.tr"
);
394
wifiPhy.
EnableAsciiAll
(stream);
395
csma.
EnableAsciiAll
(stream);
396
internet.
EnableAsciiIpv4All
(stream);
397
398
// Let's do a pcap trace on the application source and sink, ifIndex 0
399
// Csma captures in non-promiscuous mode
400
#if 0
401
csma.
EnablePcap
(
"mixed-wireless"
, appSource->
GetId
(), 0,
false
);
402
#else
403
csma.
EnablePcapAll
(
"mixed-wireless"
,
false
);
404
#endif
405
wifiPhy.
EnablePcap
(
"mixed-wireless"
, appSink->
GetId
(), 0);
406
wifiPhy.
EnablePcap
(
"mixed-wireless"
, 9, 2);
407
wifiPhy.
EnablePcap
(
"mixed-wireless"
, 9, 0);
408
}
409
410
if
(useCourseChangeCallback ==
true
)
411
{
412
Config::Connect
(
"/NodeList/*/$ns3::MobilityModel/CourseChange"
,
MakeCallback
(&
CourseChangeCallback
));
413
}
414
416
// //
417
// Run simulation //
418
// //
420
421
NS_LOG_INFO
(
"Run Simulation."
);
422
Simulator::Stop
(Seconds (stopTime));
423
Simulator::Run
();
424
Simulator::Destroy
();
425
}
examples
wireless
mixed-wireless.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2