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
global-routing-test-suite.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
#include <vector>
18
#include "ns3/boolean.h"
19
#include "ns3/config.h"
20
#include "ns3/csma-helper.h"
21
#include "ns3/flow-monitor.h"
22
#include "ns3/flow-monitor-helper.h"
23
#include "ns3/inet-socket-address.h"
24
#include "ns3/internet-stack-helper.h"
25
#include "ns3/ipv4-address-helper.h"
26
#include "ns3/ipv4-global-routing-helper.h"
27
#include "ns3/ipv4-static-routing-helper.h"
28
#include "ns3/node.h"
29
#include "ns3/node-container.h"
30
#include "ns3/on-off-helper.h"
31
#include "ns3/packet.h"
32
#include "ns3/packet-sink-helper.h"
33
#include "ns3/packet-sink.h"
34
#include "ns3/packet-socket-helper.h"
35
#include "ns3/packet-socket-address.h"
36
#include "ns3/csma-net-device.h"
37
#include "ns3/point-to-point-helper.h"
38
#include "ns3/pointer.h"
39
#include "ns3/simple-channel.h"
40
#include "ns3/simulator.h"
41
#include "ns3/string.h"
42
#include "ns3/test.h"
43
#include "ns3/uinteger.h"
44
#include "ns3/ipv4-packet-info-tag.h"
45
46
using namespace
ns3;
47
48
class
DynamicGlobalRoutingTestCase
:
public
TestCase
49
{
50
public
:
51
DynamicGlobalRoutingTestCase
();
52
virtual
~
DynamicGlobalRoutingTestCase
();
53
54
private
:
55
void
SinkRx
(std::string path,
Ptr<const Packet>
p,
const
Address
&address);
56
void
HandleRead (
Ptr<Socket>
);
57
virtual
void
DoRun (
void
);
58
int
m_count
;
59
std::vector<uint8_t>
m_firstInterface
;
60
std::vector<uint8_t>
m_secondInterface
;
61
};
62
63
// Add some help text to this case to describe what it is intended to test
64
DynamicGlobalRoutingTestCase::DynamicGlobalRoutingTestCase
()
65
:
TestCase
(
"Dynamic global routing example"
), m_count (0)
66
{
67
m_firstInterface
.resize (16);
68
m_secondInterface
.resize (16);
69
}
70
71
DynamicGlobalRoutingTestCase::~DynamicGlobalRoutingTestCase
()
72
{
73
}
74
75
void
76
DynamicGlobalRoutingTestCase::SinkRx
(std::string path,
Ptr<const Packet>
p,
const
Address
& address)
77
{
78
Ipv4PacketInfoTag
tag;
79
bool
found;
80
found = p->
PeekPacketTag
(tag);
81
uint8_t now =
static_cast<
uint8_t
>
(
Simulator::Now
().
GetSeconds
());
82
if
(found)
83
{
84
;
85
}
86
m_firstInterface
[now]++;
87
m_count
++;
88
}
89
90
void
91
DynamicGlobalRoutingTestCase::HandleRead
(
Ptr<Socket>
socket)
92
{
93
Ptr<Packet>
packet;
94
Address
from;
95
while
((packet = socket->
RecvFrom
(from)))
96
{
97
if
(packet->
GetSize
() == 0)
98
{
//EOF
99
break
;
100
}
101
Ipv4PacketInfoTag
tag;
102
bool
found;
103
found = packet->
PeekPacketTag
(tag);
104
uint8_t now =
static_cast<
uint8_t
>
(
Simulator::Now
().
GetSeconds
());
105
if
(found)
106
{
107
if
(tag.
GetRecvIf
() == 1)
108
{
109
m_firstInterface
[now]++;
110
}
111
if
(tag.
GetRecvIf
() == 2)
112
{
113
m_secondInterface
[now]++;
114
}
115
m_count
++;
116
}
117
}
118
}
119
120
// Test derived from examples/routing/dynamic-global-routing.cc
121
//
122
// Network topology
123
//
124
// n0
125
// \ p-p
126
// \ (shared csma/cd)
127
// n2 -------------------------n3
128
// / | |
129
// / p-p n4 n5 ---------- n6
130
// n1 p-p
131
// | |
132
// ----------------------------------------
133
// p-p
134
//
135
// Test that for node n6, the interface facing n5 receives packets at
136
// times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
137
// facing n1 receives packets at times (2-4), (6-8), (12-13)
138
//
139
void
140
DynamicGlobalRoutingTestCase::DoRun
(
void
)
141
{
142
// The below value configures the default behavior of global routing.
143
// By default, it is disabled. To respond to interface events, set to true
144
Config::SetDefault
(
"ns3::Ipv4GlobalRouting::RespondToInterfaceEvents"
,
BooleanValue
(
true
));
145
146
NodeContainer
c;
147
c.
Create
(7);
148
NodeContainer
n0n2
=
NodeContainer
(c.
Get
(0), c.
Get
(2));
149
NodeContainer
n1n2
=
NodeContainer
(c.
Get
(1), c.
Get
(2));
150
NodeContainer
n5n6 =
NodeContainer
(c.
Get
(5), c.
Get
(6));
151
NodeContainer
n1n6 =
NodeContainer
(c.
Get
(1), c.
Get
(6));
152
NodeContainer
n2345 =
NodeContainer
(c.
Get
(2), c.
Get
(3), c.
Get
(4), c.
Get
(5));
153
154
InternetStackHelper
internet;
155
internet.
Install
(c);
156
157
// We create the channels first without any IP addressing information
158
PointToPointHelper
p2p;
159
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
160
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
161
NetDeviceContainer
d0d2 = p2p.
Install
(n0n2);
162
NetDeviceContainer
d1d6 = p2p.
Install
(n1n6);
163
164
NetDeviceContainer
d1d2 = p2p.
Install
(n1n2);
165
166
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"1500kbps"
));
167
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"10ms"
));
168
NetDeviceContainer
d5d6 = p2p.
Install
(n5n6);
169
170
// We create the channels first without any IP addressing information
171
CsmaHelper
csma;
172
csma.
SetChannelAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
173
csma.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
174
NetDeviceContainer
d2345 = csma.
Install
(n2345);
175
176
// Later, we add IP addresses.
177
Ipv4AddressHelper
ipv4;
178
ipv4.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
179
ipv4.
Assign
(d0d2);
180
181
ipv4.
SetBase
(
"10.1.2.0"
,
"255.255.255.0"
);
182
ipv4.
Assign
(d1d2);
183
184
ipv4.
SetBase
(
"10.1.3.0"
,
"255.255.255.0"
);
185
Ipv4InterfaceContainer
i5i6 = ipv4.
Assign
(d5d6);
186
187
ipv4.
SetBase
(
"10.250.1.0"
,
"255.255.255.0"
);
188
ipv4.
Assign
(d2345);
189
190
ipv4.
SetBase
(
"172.16.1.0"
,
"255.255.255.0"
);
191
Ipv4InterfaceContainer
i1i6 = ipv4.
Assign
(d1d6);
192
193
// Create router nodes, initialize routing database and set up the routing
194
// tables in the nodes.
195
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
196
197
// Create the OnOff application to send UDP datagrams of size
198
// 210 bytes at a rate of 448 Kb/s
199
uint16_t
port
= 9;
// Discard port (RFC 863)
200
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
201
InetSocketAddress
(i5i6.
GetAddress
(1),
port
));
202
onoff.
SetConstantRate
(
DataRate
(
"2kbps"
));
203
onoff.
SetAttribute
(
"PacketSize"
,
UintegerValue
(50));
204
205
ApplicationContainer
apps = onoff.
Install
(c.
Get
(1));
206
apps.
Start
(
Seconds
(1.0));
207
apps.
Stop
(
Seconds
(10.0));
208
209
// Create a second OnOff application to send UDP datagrams of size
210
// 210 bytes at a rate of 448 Kb/s
211
OnOffHelper
onoff2 (
"ns3::UdpSocketFactory"
,
212
InetSocketAddress
(i1i6.
GetAddress
(1),
port
));
213
onoff2.
SetAttribute
(
"OnTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=1]"
));
214
onoff2.
SetAttribute
(
"OffTime"
,
StringValue
(
"ns3::ConstantRandomVariable[Constant=0]"
));
215
onoff2.
SetAttribute
(
"DataRate"
,
StringValue
(
"2kbps"
));
216
onoff2.
SetAttribute
(
"PacketSize"
,
UintegerValue
(50));
217
218
ApplicationContainer
apps2 = onoff2.
Install
(c.
Get
(1));
219
apps2.
Start
(
Seconds
(11.0));
220
apps2.
Stop
(
Seconds
(16.0));
221
222
// Create an optional packet sink to receive these packets
223
TypeId
tid = TypeId::LookupByName (
"ns3::UdpSocketFactory"
);
224
Ptr<Socket>
sink2 = Socket::CreateSocket (c.
Get
(6), tid);
225
sink2->
Bind
(
Address
(
InetSocketAddress
(Ipv4Address::GetAny (), port)));
226
sink2->
Listen
();
227
sink2->
ShutdownSend
();
228
229
sink2->
SetRecvPktInfo
(
true
);
230
sink2->
SetRecvCallback
(
MakeCallback
(&
DynamicGlobalRoutingTestCase::HandleRead
,
this
));
231
232
Ptr<Node>
n1 = c.
Get
(1);
233
Ptr<Ipv4>
ipv41 = n1->
GetObject
<
Ipv4
> ();
234
// The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
235
// then the next p2p is numbered 2
236
uint32_t ipv4ifIndex1 = 2;
237
238
// Trace receptions
239
Config::Connect
(
"/NodeList/6/ApplicationList/*/$ns3::PacketSink/Rx"
,
240
MakeCallback
(&
DynamicGlobalRoutingTestCase::SinkRx
,
this
));
241
242
Simulator::Schedule (
Seconds
(2),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
243
Simulator::Schedule (
Seconds
(4),&
Ipv4::SetUp
,ipv41, ipv4ifIndex1);
244
245
Ptr<Node>
n6 = c.
Get
(6);
246
Ptr<Ipv4>
ipv46 = n6->
GetObject
<
Ipv4
> ();
247
// The first ifIndex is 0 for loopback, then the first p2p is numbered 1,
248
// then the next p2p is numbered 2
249
uint32_t ipv4ifIndex6 = 2;
250
Simulator::Schedule (
Seconds
(6),&Ipv4::SetDown,ipv46, ipv4ifIndex6);
251
Simulator::Schedule (
Seconds
(8),&
Ipv4::SetUp
,ipv46, ipv4ifIndex6);
252
253
Simulator::Schedule (
Seconds
(12),&Ipv4::SetDown,ipv41, ipv4ifIndex1);
254
Simulator::Schedule (
Seconds
(14),&
Ipv4::SetUp
,ipv41, ipv4ifIndex1);
255
256
Simulator::Run
();
257
258
NS_TEST_ASSERT_MSG_EQ
(
m_count
, 68,
"Dynamic global routing did not deliver all packets"
);
259
// Test that for node n6, the interface facing n5 receives packets at
260
// times (1-2), (4-6), (8-10), (11-12), (14-16) and the interface
261
// facing n1 receives packets at times (2-4), (6-8), (12-13)
262
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[1], 4,
"Dynamic global routing did not deliver all packets"
);
263
NS_TEST_ASSERT_MSG_EQ
(
m_secondInterface
[2], 5,
"Dynamic global routing did not deliver all packets"
);
264
NS_TEST_ASSERT_MSG_EQ
(
m_secondInterface
[3], 5,
"Dynamic global routing did not deliver all packets"
);
265
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[4], 5,
"Dynamic global routing did not deliver all packets"
);
266
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[5], 5,
"Dynamic global routing did not deliver all packets"
);
267
NS_TEST_ASSERT_MSG_EQ
(
m_secondInterface
[6], 5,
"Dynamic global routing did not deliver all packets"
);
268
NS_TEST_ASSERT_MSG_EQ
(
m_secondInterface
[7], 5,
"Dynamic global routing did not deliver all packets"
);
269
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[8], 5,
"Dynamic global routing did not deliver all packets"
);
270
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[9], 5,
"Dynamic global routing did not deliver all packets"
);
271
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[10], 0,
"Dynamic global routing did not deliver all packets"
);
272
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[11], 4,
"Dynamic global routing did not deliver all packets"
);
273
NS_TEST_ASSERT_MSG_EQ
(
m_secondInterface
[12], 5,
"Dynamic global routing did not deliver all packets"
);
274
NS_TEST_ASSERT_MSG_EQ
(
m_secondInterface
[13], 5,
"Dynamic global routing did not deliver all packets"
);
275
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[14], 5,
"Dynamic global routing did not deliver all packets"
);
276
NS_TEST_ASSERT_MSG_EQ
(
m_firstInterface
[15], 5,
"Dynamic global routing did not deliver all packets"
);
277
Simulator::Destroy ();
278
}
279
280
class
GlobalRoutingSlash32TestCase
:
public
TestCase
281
{
282
public
:
283
GlobalRoutingSlash32TestCase
();
284
virtual
~GlobalRoutingSlash32TestCase
();
285
286
private
:
287
virtual
void
DoRun
(
void
);
288
};
289
290
// Add some help text to this case to describe what it is intended to test
291
GlobalRoutingSlash32TestCase::GlobalRoutingSlash32TestCase
()
292
:
TestCase
(
"Slash 32 global routing example"
)
293
{
294
}
295
296
GlobalRoutingSlash32TestCase::~GlobalRoutingSlash32TestCase
()
297
{
298
}
299
300
// Test program for this 3-router scenario, using global routing
301
//
302
// (a.a.a.a/32)A<--x.x.x.0/30-->B<--y.y.y.0/30-->C(c.c.c.c/32)
303
//
304
void
305
GlobalRoutingSlash32TestCase::DoRun
(
void
)
306
{
307
Ptr<Node>
nA = CreateObject<Node> ();
308
Ptr<Node>
nB = CreateObject<Node> ();
309
Ptr<Node>
nC = CreateObject<Node> ();
310
311
NodeContainer
c =
NodeContainer
(nA, nB, nC);
312
313
InternetStackHelper
internet;
314
internet.
Install
(c);
315
316
// Point-to-point links
317
NodeContainer
nAnB =
NodeContainer
(nA, nB);
318
NodeContainer
nBnC =
NodeContainer
(nB, nC);
319
320
// We create the channels first without any IP addressing information
321
PointToPointHelper
p2p;
322
p2p.
SetDeviceAttribute
(
"DataRate"
,
StringValue
(
"5Mbps"
));
323
p2p.
SetChannelAttribute
(
"Delay"
,
StringValue
(
"2ms"
));
324
NetDeviceContainer
dAdB = p2p.
Install
(nAnB);
325
326
NetDeviceContainer
dBdC = p2p.
Install
(nBnC);;
327
328
Ptr<CsmaNetDevice>
deviceA = CreateObject<CsmaNetDevice> ();
329
deviceA->
SetAddress
(Mac48Address::Allocate ());
330
nA->
AddDevice
(deviceA);
331
332
Ptr<CsmaNetDevice>
deviceC = CreateObject<CsmaNetDevice> ();
333
deviceC->
SetAddress
(Mac48Address::Allocate ());
334
nC->AddDevice (deviceC);
335
336
// Later, we add IP addresses.
337
Ipv4AddressHelper
ipv4;
338
ipv4.
SetBase
(
"10.1.1.0"
,
"255.255.255.252"
);
339
Ipv4InterfaceContainer
iAiB = ipv4.
Assign
(dAdB);
340
341
ipv4.
SetBase
(
"10.1.1.4"
,
"255.255.255.252"
);
342
Ipv4InterfaceContainer
iBiC = ipv4.
Assign
(dBdC);
343
344
Ptr<Ipv4>
ipv4A = nA->
GetObject
<
Ipv4
> ();
345
Ptr<Ipv4>
ipv4C = nC->
GetObject
<
Ipv4
> ();
346
347
int32_t ifIndexA = ipv4A->
AddInterface
(deviceA);
348
int32_t ifIndexC = ipv4C->AddInterface (deviceC);
349
350
Ipv4InterfaceAddress
ifInAddrA =
Ipv4InterfaceAddress
(
Ipv4Address
(
"172.16.1.1"
),
Ipv4Mask
(
"255.255.255.255"
));
351
ipv4A->
AddAddress
(ifIndexA, ifInAddrA);
352
ipv4A->
SetMetric
(ifIndexA, 1);
353
ipv4A->
SetUp
(ifIndexA);
354
355
Ipv4InterfaceAddress
ifInAddrC =
Ipv4InterfaceAddress
(
Ipv4Address
(
"192.168.1.1"
),
Ipv4Mask
(
"255.255.255.255"
));
356
ipv4C->AddAddress (ifIndexC, ifInAddrC);
357
ipv4C->SetMetric (ifIndexC, 1);
358
ipv4C->SetUp (ifIndexC);
359
360
// Create router nodes, initialize routing database and set up the routing
361
// tables in the nodes.
362
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
363
364
// Create the OnOff application to send UDP datagrams of size
365
// 210 bytes at a rate of 448 Kb/s
366
uint16_t
port
= 9;
// Discard port (RFC 863)
367
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
368
Address
(
InetSocketAddress
(ifInAddrC.
GetLocal
(),
port
)));
369
onoff.
SetConstantRate
(
DataRate
(6000));
370
ApplicationContainer
apps = onoff.Install (nA);
371
apps.
Start
(
Seconds
(1.0));
372
apps.
Stop
(
Seconds
(10.0));
373
374
// Create a packet sink to receive these packets
375
PacketSinkHelper
sink (
"ns3::UdpSocketFactory"
,
376
Address
(
InetSocketAddress
(Ipv4Address::GetAny (), port)));
377
apps = sink.
Install
(nC);
378
apps.
Start
(
Seconds
(1.0));
379
apps.
Stop
(
Seconds
(10.0));
380
381
Simulator::Run
();
382
// Check that we received 13 * 512 = 6656 bytes
383
Ptr<PacketSink>
sinkPtr = DynamicCast <PacketSink> (apps.
Get
(0));
384
NS_TEST_ASSERT_MSG_EQ
(sinkPtr->
GetTotalRx
(), 6656,
"Static routing with /32 did not deliver all packets"
);
385
Simulator::Destroy ();
386
}
387
388
389
class
GlobalRoutingTestSuite
:
public
TestSuite
390
{
391
public
:
392
GlobalRoutingTestSuite
();
393
};
394
395
GlobalRoutingTestSuite::GlobalRoutingTestSuite
()
396
:
TestSuite
(
"global-routing"
, UNIT)
397
{
398
AddTestCase
(
new
DynamicGlobalRoutingTestCase
, TestCase::QUICK);
399
AddTestCase
(
new
GlobalRoutingSlash32TestCase
, TestCase::QUICK);
400
}
401
402
// Do not forget to allocate an instance of this TestSuite
403
static
GlobalRoutingTestSuite
globalRoutingTestSuite
;
src
test
global-routing-test-suite.cc
Generated on Tue May 14 2013 11:08:33 for ns-3 by
1.8.1.2