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