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
csma-bridge-one-hop.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
// Network topology
18
//
19
// bridge1 The node named bridge1 (node 5 in the nodelist)
20
// ------------------ has three CMSA net devices that are bridged
21
// CSMA CSMA CSMA together using a BridgeNetDevice.
22
// | | |
23
// | | | The bridge node talks over three CSMA channels
24
// | | |
25
// CSMA CSMA CSMA to three other CSMA net devices
26
// ---- ---- ----
27
// n0 n1 n2 Node two acts as a router and talks to another
28
// ---- bridge that connects the remaining nodes.
29
// CSMA
30
// |
31
// n3 n4 |
32
// ---- ---- |
33
// CSMA CSMA |
34
// | | |
35
// | | |
36
// | | |
37
// CSMA CSMA CSMA The node named bridge2 (node 6 in the nodelist)
38
// ------------------ has three CMSA net devices that are bridged
39
// bridge2 together using a BridgeNetDevice.
40
//
41
// Or, more abstractly, recognizing that bridge 1 and bridge 2 are nodes
42
// with three net devices:
43
//
44
// n0 n1 (n0 = 10.1.1.2)
45
// | | (n1 = 10.1.1.3) Note odd addressing
46
// ----------- (n2 = 10.1.1.1)
47
// | bridge1 | <- n5
48
// -----------
49
// |
50
// router <- n2
51
// |
52
// -----------
53
// | bridge2 | <- n6
54
// ----------- (n2 = 10.1.2.1)
55
// | | (n3 = 10.1.2.2)
56
// n3 n4 (n4 = 10.1.2.3)
57
//
58
// So, this example shows two broadcast domains, each interconnected by a bridge
59
// with a router node (n2) interconnecting the layer-2 broadcast domains
60
//
61
// It is meant to mirror somewhat the csma-bridge example but adds another
62
// bridged link separated by a router.
63
//
64
// - CBR/UDP flows from n0 (10.1.1.2) to n1 (10.1.1.3) and from n3 (10.1.2.2) to n0 (10.1.1.3)
65
// - DropTail queues
66
// - Global static routing
67
// - Tracing of queues and packet receptions to file "csma-bridge-one-hop.tr"
68
69
#include <iostream>
70
#include <fstream>
71
72
#include "ns3/core-module.h"
73
#include "ns3/network-module.h"
74
#include "ns3/applications-module.h"
75
#include "ns3/bridge-module.h"
76
#include "ns3/csma-module.h"
77
#include "ns3/internet-module.h"
78
79
using namespace
ns3;
80
81
NS_LOG_COMPONENT_DEFINE
(
"CsmaBridgeOneHopExample"
);
82
83
int
84
main
(
int
argc,
char
*argv[])
85
{
86
//
87
// Users may find it convenient to turn on explicit debugging
88
// for selected modules; the below lines suggest how to do this
89
//
90
#if 0
91
LogComponentEnable
(
"CsmaBridgeOneHopExample"
,
LOG_LEVEL_INFO
);
92
#endif
93
94
//
95
// Allow the user to override any of the defaults and the above Bind() at
96
// run-time, via command-line arguments
97
//
98
CommandLine
cmd;
99
cmd.
Parse
(argc, argv);
100
101
//
102
// Explicitly create the nodes required by the topology (shown above).
103
//
104
NS_LOG_INFO
(
"Create nodes."
);
105
106
Ptr<Node>
n0 = CreateObject<Node> ();
107
Ptr<Node>
n1 = CreateObject<Node> ();
108
Ptr<Node>
n2 = CreateObject<Node> ();
109
Ptr<Node>
n3 = CreateObject<Node> ();
110
Ptr<Node>
n4 = CreateObject<Node> ();
111
112
Ptr<Node>
bridge1 = CreateObject<Node> ();
113
Ptr<Node>
bridge2 = CreateObject<Node> ();
114
115
NS_LOG_INFO
(
"Build Topology"
);
116
CsmaHelper
csma;
117
csma.
SetChannelAttribute
(
"DataRate"
,
DataRateValue
(5000000));
118
csma.
SetChannelAttribute
(
"Delay"
,
TimeValue
(
MilliSeconds
(2)));
119
120
// Create the csma links, from each terminal to the bridge
121
// This will create six network devices; we'll keep track separately
122
// of the devices on and off the bridge respectively, for later configuration
123
NetDeviceContainer
topLanDevices;
124
NetDeviceContainer
topBridgeDevices;
125
126
// It is easier to iterate the nodes in C++ if we put them into a container
127
NodeContainer
topLan (n2, n0, n1);
128
129
for
(
int
i = 0; i < 3; i++)
130
{
131
// install a csma channel between the ith toplan node and the bridge node
132
NetDeviceContainer
link = csma.
Install
(
NodeContainer
(topLan.
Get
(i), bridge1));
133
topLanDevices.
Add
(link.
Get
(0));
134
topBridgeDevices.
Add
(link.
Get
(1));
135
}
136
137
//
138
// Now, Create the bridge netdevice, which will do the packet switching. The
139
// bridge lives on the node bridge1 and bridges together the topBridgeDevices
140
// which are the three CSMA net devices on the node in the diagram above.
141
//
142
BridgeHelper
bridge;
143
bridge.
Install
(bridge1, topBridgeDevices);
144
145
// Add internet stack to the router nodes
146
NodeContainer
routerNodes (n0, n1, n2, n3, n4);
147
InternetStackHelper
internet;
148
internet.
Install
(routerNodes);
149
150
// Repeat for bottom bridged LAN
151
NetDeviceContainer
bottomLanDevices;
152
NetDeviceContainer
bottomBridgeDevices;
153
NodeContainer
bottomLan (n2, n3, n4);
154
for
(
int
i = 0; i < 3; i++)
155
{
156
NetDeviceContainer
link = csma.
Install
(
NodeContainer
(bottomLan.
Get
(i), bridge2));
157
bottomLanDevices.
Add
(link.
Get
(0));
158
bottomBridgeDevices.
Add
(link.
Get
(1));
159
}
160
bridge.
Install
(bridge2, bottomBridgeDevices);
161
162
// We've got the "hardware" in place. Now we need to add IP addresses.
163
NS_LOG_INFO
(
"Assign IP Addresses."
);
164
Ipv4AddressHelper
ipv4;
165
ipv4.
SetBase
(
"10.1.1.0"
,
"255.255.255.0"
);
166
ipv4.
Assign
(topLanDevices);
167
ipv4.
SetBase
(
"10.1.2.0"
,
"255.255.255.0"
);
168
ipv4.
Assign
(bottomLanDevices);
169
170
//
171
// Create router nodes, initialize routing database and set up the routing
172
// tables in the nodes. We excuse the bridge nodes from having to serve as
173
// routers, since they don't even have internet stacks on them.
174
//
175
Ipv4GlobalRoutingHelper::PopulateRoutingTables
();
176
177
//
178
// Create an OnOff application to send UDP datagrams from node zero to node 1.
179
//
180
NS_LOG_INFO
(
"Create Applications."
);
181
uint16_t
port
= 9;
// Discard port (RFC 863)
182
183
OnOffHelper
onoff (
"ns3::UdpSocketFactory"
,
184
Address
(
InetSocketAddress
(
Ipv4Address
(
"10.1.1.3"
), port)));
185
onoff.
SetConstantRate
(
DataRate
(
"500kb/s"
));
186
187
ApplicationContainer
app = onoff.
Install
(n0);
188
// Start the application
189
app.
Start
(
Seconds
(1.0));
190
app.
Stop
(
Seconds
(10.0));
191
192
// Create an optional packet sink to receive these packets
193
PacketSinkHelper
sink (
"ns3::UdpSocketFactory"
,
194
Address
(
InetSocketAddress
(
Ipv4Address::GetAny
(), port)));
195
ApplicationContainer
sink1 = sink.
Install
(n1);
196
sink1.
Start
(
Seconds
(1.0));
197
sink1.
Stop
(
Seconds
(10.0));
198
199
//
200
// Create a similar flow from n3 to n0, starting at time 1.1 seconds
201
//
202
onoff.
SetAttribute
(
"Remote"
,
203
AddressValue
(
InetSocketAddress
(
Ipv4Address
(
"10.1.1.2"
), port)));
204
ApplicationContainer
app2 = onoff.
Install
(n3);
205
app2.
Start
(
Seconds
(1.1));
206
app2.
Stop
(
Seconds
(10.0));
207
208
ApplicationContainer
sink2 = sink.
Install
(n0);
209
sink2.
Start
(
Seconds
(1.1));
210
sink2.
Stop
(
Seconds
(10.0));
211
212
NS_LOG_INFO
(
"Configure Tracing."
);
213
214
//
215
// Configure tracing of all enqueue, dequeue, and NetDevice receive events.
216
// Trace output will be sent to the file "csma-bridge-one-hop.tr"
217
//
218
AsciiTraceHelper
ascii;
219
csma.
EnableAsciiAll
(ascii.
CreateFileStream
(
"csma-bridge-one-hop.tr"
));
220
221
//
222
// Also configure some tcpdump traces; each interface will be traced.
223
// The output files will be named:
224
// csma-bridge-one-hop-<nodeId>-<interfaceId>.pcap
225
// and can be read by the "tcpdump -r" command (use "-tt" option to
226
// display timestamps correctly)
227
//
228
csma.
EnablePcapAll
(
"csma-bridge-one-hop"
,
false
);
229
230
//
231
// Now, do the actual simulation.
232
//
233
NS_LOG_INFO
(
"Run Simulation."
);
234
Simulator::Run
();
235
Simulator::Destroy
();
236
NS_LOG_INFO
(
"Done."
);
237
}
src
bridge
examples
csma-bridge-one-hop.cc
Generated on Fri Dec 21 2012 19:00:31 for ns-3 by
1.8.1.2