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
dynamic-global-routing.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
* Contributed by: Luis Cortes (cortes@gatech.edu)
17
*/
18
19
20
// This script exercises global routing code in a mixed point-to-point
21
// and csma/cd environment. We bring up and down interfaces and observe
22
// the effect on global routing. We explicitly enable the attribute
23
// to respond to interface events, so that routes are recomputed
24
// automatically.
25
//
26
// Network topology
27
//
28
// n0
29
// \ p-p
30
// \ (shared csma/cd)
31
// n2 -------------------------n3
32
// / | |
33
// / p-p n4 n5 ---------- n6
34
// n1 p-p
35
// | |
36
// ----------------------------------------
37
// p-p
38
//
39
// - at time 1 CBR/UDP flow from n1 to n6's IP address on the n5/n6 link
40
// - at time 10, start similar flow from n1 to n6's address on the n1/n6 link
41
//
42
// Order of events
43
// At pre-simulation time, configure global routes. Shortest path from
44
// n1 to n6 is via the direct point-to-point link
45
// At time 1s, start CBR traffic flow from n1 to n6
46
// At time 2s, set the n1 point-to-point interface to down. Packets
47
// will be diverted to the n1-n2-n5-n6 path
48
// At time 4s, re-enable the n1/n6 interface to up. n1-n6 route restored.
49
// At time 6s, set the n6-n1 point-to-point Ipv4 interface to down (note, this
50
// keeps the point-to-point link "up" from n1's perspective). Traffic will
51
// flow through the path n1-n2-n5-n6
52
// At time 8s, bring the interface back up. Path n1-n6 is restored
53
// At time 10s, stop the first flow.
54
// At time 11s, start a new flow, but to n6's other IP address (the one
55
// on the n1/n6 p2p link)
56
// At time 12s, bring the n1 interface down between n1 and n6. Packets
57
// will be diverted to the alternate path
58
// At time 14s, re-enable the n1/n6 interface to up. This will change
59
// routing back to n1-n6 since the interface up notification will cause
60
// a new local interface route, at higher priority than global routing
61
// At time 16s, stop the second flow.
62
63
// - Tracing of queues and packet receptions to file "dynamic-global-routing.tr"
64
65
#include <iostream>
66
#include <fstream>
67
#include <string>
68
#include <cassert>
69
70
#include "ns3/core-module.h"
71
#include "ns3/network-module.h"
72
#include "ns3/csma-module.h"
73
#include "ns3/internet-module.h"
74
#include "ns3/point-to-point-module.h"
75
#include "ns3/applications-module.h"
76
#include "ns3/ipv4-global-routing-helper.h"
77
78
using namespace
ns3;
79
80
NS_LOG_COMPONENT_DEFINE
(
"DynamicGlobalRoutingExample"
);
81
82
int
83
main
(
int
argc,
char
*argv[])
84
{
85
// The below value configures the default behavior of global routing.
86
// By default, it is disabled. To respond to interface events, set to true
87
Config::SetDefault
(
"ns3::Ipv4GlobalRouting::RespondToInterfaceEvents"
,
BooleanValue
(
true
));
88
89
// Allow the user to override any of the defaults and the above
90
// Bind ()s at run-time, via command-line arguments
91
CommandLine
cmd;
92
cmd.
Parse
(argc, argv);
93
94
NS_LOG_INFO
(
"Create nodes."
);
95
NodeContainer
c;
96
c.
Create
(7);
97
NodeContainer
n0n2
=
NodeContainer
(c.
Get
(0), c.
Get
(2));
98
NodeContainer
n1n2
=
NodeContainer
(c.
Get
(1), c.
Get
(2));
99
NodeContainer
n5n6 =
NodeContainer
(c.
Get
(5), c.
Get
(6));
100
NodeContainer
n1n6 =
NodeContainer
(c.
Get
(1), c.
Get
(6));
101
NodeContainer
n2345 =
NodeContainer
(c.
Get
(2), c.
Get
(3), c.
Get
(4), c.
Get
(5));
102
103
InternetStackHelper
internet;
104
internet.
Install
(c);
105
106
// We create the channels first without any IP addressing information
107
NS_LOG_INFO
(
"Create channels."
);
108
PointToPointHelper
p2p;
109
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
110
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
111
NetDeviceContainer
d0d2 = p2p.
Install
(n0n2);
112
NetDeviceContainer
d1d6 = p2p.
Install
(n1n6);
113
114
NetDeviceContainer
d1d2 = p2p.
Install
(n1n2);
115
116
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"1500kbps"
));
117
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"10ms"
));
118
NetDeviceContainer
d5d6 = p2p.
Install
(n5n6);
119
120
// We create the channels first without any IP addressing information
121
CsmaHelper
csma;
122
csma.
SetChannelAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
123
csma.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
124
NetDeviceContainer
d2345 = csma.
Install
(n2345);
125
126
// Later, we add IP addresses.
127
NS_LOG_INFO
(
"Assign IP Addresses."
);
128
Ipv4AddressHelper
ipv4;
129
ipv4.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
130
ipv4.
Assign
(d0d2);
131
132
ipv4.
SetBase
(
"10.1.2.0"
,
"255.255.255.0"
);
133
ipv4.
Assign
(d1d2);
134
135
ipv4.
SetBase
(
"10.1.3.0"
,
"255.255.255.0"
);
136
Ipv4InterfaceContainer
i5i6 = ipv4.
Assign
(d5d6);
137
138
ipv4.
SetBase
(
"10.250.1.0"
,
"255.255.255.0"
);
139
ipv4.
Assign
(d2345);
140
141
ipv4.
SetBase
(
"172.16.1.0"
,
"255.255.255.0"
);
142
Ipv4InterfaceContainer
i1i6 = ipv4.
Assign
(d1d6);
143
144
// Create router nodes, initialize routing database and set up the routing
145
// tables in the nodes.
146
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
147
148
// Create the OnOff application to send UDP datagrams of size
149
// 210 bytes at a rate of 448 Kb/s
150
NS_LOG_INFO
(
"Create Applications."
);
151
uint16_t
port
= 9;
// Discard port (RFC 863)
152
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
153
InetSocketAddress
(i5i6.
GetAddress
(1),
port
));
154
onoff.
SetConstantRate
(
DataRate
(
"2kbps"
));
155
onoff.
SetAttribute
(
"PacketSize"
,
UintegerValue
(50));
156
157
ApplicationContainer
apps = onoff.
Install
(c.
Get
(1));
158
apps.
Start
(Seconds (1.0));
159
apps.
Stop
(Seconds (10.0));
160
161
// Create a second OnOff application to send UDP datagrams of size
162
// 210 bytes at a rate of 448 Kb/s
163
OnOffHelper
onoff2 (
"ns3::UdpSocketFactory"
,
164
InetSocketAddress
(i1i6.
GetAddress
(1),
port
));
165
onoff2.
SetAttribute
(
"OnTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=1]"
));
166
onoff2.
SetAttribute
(
"OffTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=0]"
));
167
onoff2.
SetAttribute
(
"DataRate"
,
StringValue
(
"2kbps"
));
168
onoff2.
SetAttribute
(
"PacketSize"
,
UintegerValue
(50));
169
170
ApplicationContainer
apps2 = onoff2.
Install
(c.
Get
(1));
171
apps2.
Start
(Seconds (11.0));
172
apps2.
Stop
(Seconds (16.0));
173
174
// Create an optional packet sink to receive these packets
175
PacketSinkHelper
sink (
"ns3::UdpSocketFactory"
,
176
Address
(
InetSocketAddress
(
Ipv4Address::GetAny
(), port)));
177
apps = sink.
Install
(c.
Get
(6));
178
apps.
Start
(Seconds (1.0));
179
apps.
Stop
(Seconds (10.0));
180
181
PacketSinkHelper
sink2 (
"ns3::UdpSocketFactory"
,
182
Address
(
InetSocketAddress
(
Ipv4Address::GetAny
(), port)));
183
apps2 = sink2.
Install
(c.
Get
(6));
184
apps2.
Start
(Seconds (11.0));
185
apps2.
Stop
(Seconds (16.0));
186
187
188
AsciiTraceHelper
ascii;
189
Ptr<OutputStreamWrapper>
stream = ascii.
CreateFileStream
(
"dynamic-global-routing.tr"
);
190
p2p.
EnableAsciiAll
(stream);
191
csma.
EnableAsciiAll
(stream);
192
internet.EnableAsciiIpv4All (stream);
193
194
p2p.
EnablePcapAll
(
"dynamic-global-routing"
);
195
csma.
EnablePcapAll
(
"dynamic-global-routing"
,
false
);
196
197
Ptr<Node>
n1 = c.
Get
(1);
198
Ptr<Ipv4>
ipv41 = n1->
GetObject
<
Ipv4
> ();
199
// The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
200
// then the next p2p is numbered 2
201
uint32_t ipv4ifIndex1 = 2;
202
203
Simulator::Schedule
(Seconds (2),&
Ipv4::SetDown
,ipv41, ipv4ifIndex1);
204
Simulator::Schedule
(Seconds (4),&
Ipv4::SetUp
,ipv41, ipv4ifIndex1);
205
206
Ptr<Node>
n6 = c.
Get
(6);
207
Ptr<Ipv4>
ipv46 = n6->
GetObject
<
Ipv4
> ();
208
// The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
209
// then the next p2p is numbered 2
210
uint32_t ipv4ifIndex6 = 2;
211
Simulator::Schedule
(Seconds (6),&
Ipv4::SetDown
,ipv46, ipv4ifIndex6);
212
Simulator::Schedule
(Seconds (8),&
Ipv4::SetUp
,ipv46, ipv4ifIndex6);
213
214
Simulator::Schedule
(Seconds (12),&
Ipv4::SetDown
,ipv41, ipv4ifIndex1);
215
Simulator::Schedule
(Seconds (14),&
Ipv4::SetUp
,ipv41, ipv4ifIndex1);
216
217
// Trace routing tables
218
Ipv4GlobalRoutingHelper
g;
219
Ptr<OutputStreamWrapper>
routingStream = Create<OutputStreamWrapper> (
"dynamic-global-routing.routes"
, std::ios::out);
220
g.
PrintRoutingTableAllAt
(Seconds (12), routingStream);
221
222
NS_LOG_INFO
(
"Run Simulation."
);
223
Simulator::Run
();
224
Simulator::Destroy
();
225
NS_LOG_INFO
(
"Done."
);
226
}
examples
routing
dynamic-global-routing.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2