A Discrete-Event Network Simulator
API
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 
85 using namespace ns3;
86 
87 NS_LOG_COMPONENT_DEFINE ("CsmaBridgeOneHopExample");
88 
89 int
90 main (int argc, char *argv[])
91 {
92  //
93  // Users may find it convenient to turn on explicit debugging
94  // for selected modules; the below lines suggest how to do this
95  //
96 #if 0
97  LogComponentEnable ("CsmaBridgeOneHopExample", LOG_LEVEL_INFO);
98 #endif
99 
100  //
101  // Allow the user to override any of the defaults and the above Bind() at
102  // run-time, via command-line arguments
103  //
104  CommandLine cmd (__FILE__);
105  cmd.Parse (argc, argv);
106 
107  //
108  // Explicitly create the nodes required by the topology (shown above).
109  //
110  NS_LOG_INFO ("Create nodes.");
111 
112  Ptr<Node> n0 = CreateObject<Node> ();
113  Ptr<Node> n1 = CreateObject<Node> ();
114  Ptr<Node> n2 = CreateObject<Node> ();
115  Ptr<Node> n3 = CreateObject<Node> ();
116  Ptr<Node> n4 = CreateObject<Node> ();
117 
118  Ptr<Node> bridge1 = CreateObject<Node> ();
119  Ptr<Node> bridge2 = CreateObject<Node> ();
120 
121  NS_LOG_INFO ("Build Topology");
123  csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
124  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
125 
126  // Create the csma links, from each terminal to the bridge
127  // This will create six network devices; we'll keep track separately
128  // of the devices on and off the bridge respectively, for later configuration
129  NetDeviceContainer topLanDevices;
130  NetDeviceContainer topBridgeDevices;
131 
132  // It is easier to iterate the nodes in C++ if we put them into a container
133  NodeContainer topLan (n2, n0, n1);
134 
135  for (int i = 0; i < 3; i++)
136  {
137  // install a csma channel between the ith toplan node and the bridge node
138  NetDeviceContainer link = csma.Install (NodeContainer (topLan.Get (i), bridge1));
139  topLanDevices.Add (link.Get (0));
140  topBridgeDevices.Add (link.Get (1));
141  }
142 
143  //
144  // Now, Create the bridge netdevice, which will do the packet switching. The
145  // bridge lives on the node bridge1 and bridges together the topBridgeDevices
146  // which are the three CSMA net devices on the node in the diagram above.
147  //
148  BridgeHelper bridge;
149  bridge.Install (bridge1, topBridgeDevices);
150 
151  // Add internet stack to the router nodes
152  NodeContainer routerNodes (n0, n1, n2, n3, n4);
153  InternetStackHelper internet;
154  internet.Install (routerNodes);
155 
156  // Repeat for bottom bridged LAN
157  NetDeviceContainer bottomLanDevices;
158  NetDeviceContainer bottomBridgeDevices;
159  NodeContainer bottomLan (n2, n3, n4);
160  for (int i = 0; i < 3; i++)
161  {
162  NetDeviceContainer link = csma.Install (NodeContainer (bottomLan.Get (i), bridge2));
163  bottomLanDevices.Add (link.Get (0));
164  bottomBridgeDevices.Add (link.Get (1));
165  }
166  bridge.Install (bridge2, bottomBridgeDevices);
167 
168  // We've got the "hardware" in place. Now we need to add IP addresses.
169  NS_LOG_INFO ("Assign IP Addresses.");
170  Ipv4AddressHelper ipv4;
171  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
172  ipv4.Assign (topLanDevices);
173  ipv4.SetBase ("10.1.2.0", "255.255.255.0");
174  ipv4.Assign (bottomLanDevices);
175 
176  //
177  // Create router nodes, initialize routing database and set up the routing
178  // tables in the nodes. We excuse the bridge nodes from having to serve as
179  // routers, since they don't even have internet stacks on them.
180  //
182 
183  //
184  // Create an OnOff application to send UDP datagrams from node zero to node 1.
185  //
186  NS_LOG_INFO ("Create Applications.");
187  uint16_t port = 9; // Discard port (RFC 863)
188 
189  OnOffHelper onoff ("ns3::UdpSocketFactory",
190  Address (InetSocketAddress (Ipv4Address ("10.1.1.3"), port)));
191  onoff.SetConstantRate (DataRate ("500kb/s"));
192 
193  ApplicationContainer app = onoff.Install (n0);
194  // Start the application
195  app.Start (Seconds (1.0));
196  app.Stop (Seconds (10.0));
197 
198  // Create an optional packet sink to receive these packets
199  PacketSinkHelper sink ("ns3::UdpSocketFactory",
201  ApplicationContainer sink1 = sink.Install (n1);
202  sink1.Start (Seconds (1.0));
203  sink1.Stop (Seconds (10.0));
204 
205  //
206  // Create a similar flow from n3 to n0, starting at time 1.1 seconds
207  //
208  onoff.SetAttribute ("Remote",
209  AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.2"), port)));
210  ApplicationContainer app2 = onoff.Install (n3);
211  app2.Start (Seconds (1.1));
212  app2.Stop (Seconds (10.0));
213 
214  ApplicationContainer sink2 = sink.Install (n0);
215  sink2.Start (Seconds (1.1));
216  sink2.Stop (Seconds (10.0));
217 
218  NS_LOG_INFO ("Configure Tracing.");
219 
220  //
221  // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
222  // Trace output will be sent to the file "csma-bridge-one-hop.tr"
223  //
224  AsciiTraceHelper ascii;
225  csma.EnableAsciiAll (ascii.CreateFileStream ("csma-bridge-one-hop.tr"));
226 
227  //
228  // Also configure some tcpdump traces; each interface will be traced.
229  // The output files will be named:
230  // csma-bridge-one-hop-<nodeId>-<interfaceId>.pcap
231  // and can be read by the "tcpdump -r" command (use "-tt" option to
232  // display timestamps correctly)
233  //
234  csma.EnablePcapAll ("csma-bridge-one-hop", false);
235 
236  //
237  // Now, do the actual simulation.
238  //
239  NS_LOG_INFO ("Run Simulation.");
240  Simulator::Run ();
242  NS_LOG_INFO ("Done.");
243 }
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
Definition: address.h:278
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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:163
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:44
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:229
build a set of CsmaNetDevice objects
Definition: csma-helper.h:47
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
static Ipv4Address GetAny(void)
static void PopulateRoutingTables(void)
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.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
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:43
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
AttributeValue implementation for Time.
Definition: nstime.h:1308
uint16_t port
Definition: dsdv-manet.cc:45
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1252
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
csma
Definition: second.py:63
cmd
Definition: second.py:35
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56