A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-bridge-one-hop.cc
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License version 2 as
4 * published by the Free Software Foundation;
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 */
15
16// Network topology
17//
18// bridge1 The node named bridge1 (node 5 in the nodelist)
19// ------------------ has three CSMA net devices that are bridged
20// CSMA CSMA CSMA together using a BridgeNetDevice.
21// | | |
22// | | | The bridge node talks over three CSMA channels
23// | | |
24// CSMA CSMA CSMA to three other CSMA net devices
25// ---- ---- ----
26// n0 n1 n2 Node two acts as a router and talks to another
27// ---- bridge that connects the remaining nodes.
28// CSMA
29// |
30// n3 n4 |
31// ---- ---- |
32// CSMA CSMA |
33// | | |
34// | | |
35// | | |
36// CSMA CSMA CSMA The node named bridge2 (node 6 in the nodelist)
37// ------------------ has three CSMA net devices that are bridged
38// bridge2 together using a BridgeNetDevice.
39//
40// Or, more abstractly, recognizing that bridge 1 and bridge 2 are nodes
41// with three net devices:
42//
43// n0 n1 (n0 = 10.1.1.2)
44// | | (n1 = 10.1.1.3) Note odd addressing
45// ----------- (n2 = 10.1.1.1)
46// | bridge1 | <- n5
47// -----------
48// |
49// router <- n2
50// |
51// -----------
52// | bridge2 | <- n6
53// ----------- (n2 = 10.1.2.1)
54// | | (n3 = 10.1.2.2)
55// n3 n4 (n4 = 10.1.2.3)
56//
57// So, this example shows two broadcast domains, each interconnected by a bridge
58// with a router node (n2) interconnecting the layer-2 broadcast domains
59//
60// It is meant to mirror somewhat the csma-bridge example but adds another
61// bridged link separated by a router.
62//
63// - 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)
64// - DropTail queues
65// - Global static routing
66// - Tracing of queues and packet receptions to file "csma-bridge-one-hop.tr"
67
68#include "ns3/applications-module.h"
69#include "ns3/bridge-module.h"
70#include "ns3/core-module.h"
71#include "ns3/csma-module.h"
72#include "ns3/internet-module.h"
73#include "ns3/network-module.h"
74
75#include <fstream>
76#include <iostream>
77
78/**
79 * \file
80 * \ingroup bridge
81 * Bridge example connecting two broadcast domains.
82 */
83
84using namespace ns3;
85
86NS_LOG_COMPONENT_DEFINE("CsmaBridgeOneHopExample");
87
88int
89main(int argc, char* argv[])
90{
91 //
92 // Users may find it convenient to turn on explicit debugging
93 // for selected modules; the below lines suggest how to do this
94 //
95#if 0
96 LogComponentEnable ("CsmaBridgeOneHopExample", LOG_LEVEL_INFO);
97#endif
98
99 //
100 // Allow the user to override any of the defaults and the above Bind() at
101 // run-time, via command-line arguments
102 //
103 CommandLine cmd(__FILE__);
104 cmd.Parse(argc, argv);
105
106 //
107 // Explicitly create the nodes required by the topology (shown above).
108 //
109 NS_LOG_INFO("Create nodes.");
110
111 Ptr<Node> n0 = CreateObject<Node>();
112 Ptr<Node> n1 = CreateObject<Node>();
113 Ptr<Node> n2 = CreateObject<Node>();
114 Ptr<Node> n3 = CreateObject<Node>();
115 Ptr<Node> n4 = CreateObject<Node>();
116
117 Ptr<Node> bridge1 = CreateObject<Node>();
118 Ptr<Node> bridge2 = CreateObject<Node>();
119
120 NS_LOG_INFO("Build Topology");
122 csma.SetChannelAttribute("DataRate", DataRateValue(5000000));
123 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
124
125 // Create the csma links, from each terminal to the bridge
126 // This will create six network devices; we'll keep track separately
127 // of the devices on and off the bridge respectively, for later configuration
128 NetDeviceContainer topLanDevices;
129 NetDeviceContainer topBridgeDevices;
130
131 // It is easier to iterate the nodes in C++ if we put them into a container
132 NodeContainer topLan(n2, n0, n1);
133
134 for (int i = 0; i < 3; i++)
135 {
136 // install a csma channel between the ith toplan node and the bridge node
137 NetDeviceContainer link = csma.Install(NodeContainer(topLan.Get(i), bridge1));
138 topLanDevices.Add(link.Get(0));
139 topBridgeDevices.Add(link.Get(1));
140 }
141
142 //
143 // Now, Create the bridge netdevice, which will do the packet switching. The
144 // bridge lives on the node bridge1 and bridges together the topBridgeDevices
145 // which are the three CSMA net devices on the node in the diagram above.
146 //
147 BridgeHelper bridge;
148 bridge.Install(bridge1, topBridgeDevices);
149
150 // Add internet stack to the router nodes
151 NodeContainer routerNodes(n0, n1, n2, n3, n4);
153 internet.Install(routerNodes);
154
155 // Repeat for bottom bridged LAN
156 NetDeviceContainer bottomLanDevices;
157 NetDeviceContainer bottomBridgeDevices;
158 NodeContainer bottomLan(n2, n3, n4);
159 for (int i = 0; i < 3; i++)
160 {
161 NetDeviceContainer link = csma.Install(NodeContainer(bottomLan.Get(i), bridge2));
162 bottomLanDevices.Add(link.Get(0));
163 bottomBridgeDevices.Add(link.Get(1));
164 }
165 bridge.Install(bridge2, bottomBridgeDevices);
166
167 // We've got the "hardware" in place. Now we need to add IP addresses.
168 NS_LOG_INFO("Assign IP Addresses.");
170 ipv4.SetBase("10.1.1.0", "255.255.255.0");
171 ipv4.Assign(topLanDevices);
172 ipv4.SetBase("10.1.2.0", "255.255.255.0");
173 ipv4.Assign(bottomLanDevices);
174
175 //
176 // Create router nodes, initialize routing database and set up the routing
177 // tables in the nodes. We excuse the bridge nodes from having to serve as
178 // routers, since they don't even have internet stacks on them.
179 //
181
182 //
183 // Create an OnOff application to send UDP datagrams from node zero to node 1.
184 //
185 NS_LOG_INFO("Create Applications.");
186 uint16_t port = 9; // Discard port (RFC 863)
187
188 OnOffHelper onoff("ns3::UdpSocketFactory",
190 onoff.SetConstantRate(DataRate("500kb/s"));
191
192 ApplicationContainer app = onoff.Install(n0);
193 // Start the application
194 app.Start(Seconds(1.0));
195 app.Stop(Seconds(10.0));
196
197 // Create an optional packet sink to receive these packets
198 PacketSinkHelper sink("ns3::UdpSocketFactory",
200 ApplicationContainer sink1 = sink.Install(n1);
201 sink1.Start(Seconds(1.0));
202 sink1.Stop(Seconds(10.0));
203
204 //
205 // Create a similar flow from n3 to n0, starting at time 1.1 seconds
206 //
207 onoff.SetAttribute("Remote", AddressValue(InetSocketAddress(Ipv4Address("10.1.1.2"), port)));
208 ApplicationContainer app2 = onoff.Install(n3);
209 app2.Start(Seconds(1.1));
210 app2.Stop(Seconds(10.0));
211
212 ApplicationContainer sink2 = sink.Install(n0);
213 sink2.Start(Seconds(1.1));
214 sink2.Stop(Seconds(10.0));
215
216 NS_LOG_INFO("Configure Tracing.");
217
218 //
219 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
220 // Trace output will be sent to the file "csma-bridge-one-hop.tr"
221 //
222 AsciiTraceHelper ascii;
223 csma.EnableAsciiAll(ascii.CreateFileStream("csma-bridge-one-hop.tr"));
224
225 //
226 // Also configure some tcpdump traces; each interface will be traced.
227 // The output files will be named:
228 // csma-bridge-one-hop-<nodeId>-<interfaceId>.pcap
229 // and can be read by the "tcpdump -r" command (use "-tt" option to
230 // display timestamps correctly)
231 //
232 csma.EnablePcapAll("csma-bridge-one-hop", false);
233
234 //
235 // Now, do the actual simulation.
236 //
237 NS_LOG_INFO("Run Simulation.");
240 NS_LOG_INFO("Done.");
241
242 return 0;
243}
a polymophic address class
Definition: address.h:101
AttributeValue implementation for Address.
Definition: address.h:286
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Manage ASCII trace files for device models.
Definition: trace-helper.h:174
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.
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
Definition: bridge-helper.h:45
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:42
static Ipv4Address GetAny()
static void PopulateRoutingTables()
Build a routing database and initialize the routing tables of the nodes in the simulation.
holds a vector of ns3::NetDevice pointers
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
keep track of a set of node pointers.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:37
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
AttributeValue implementation for Time.
Definition: nstime.h:1406
uint16_t port
Definition: dsdv-manet.cc:44
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:275
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1319
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1331
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
ns cmd
Definition: second.py:40
ns csma
Definition: second.py:63
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55