A Discrete-Event Network Simulator
API
 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  //
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",
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 ();
236  NS_LOG_INFO ("Done.");
237 }
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:141
an Inet address class
static Ipv4Address GetAny(void)
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:69
static void PopulateRoutingTables(void)
Build a routing database and initialize the routing tables of the nodes in the simulation.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
static void Run(void)
Run the simulation until one of:
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
aggregate IP/TCP/UDP functionality to existing Nodes.
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:215
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:223
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
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. ...
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
int main(int argc, char *argv[])
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
hold objects of type ns3::Time
Definition: nstime.h:1008
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
holds a vector of ns3::NetDevice pointers
Add capability to bridge multiple LAN segments (IEEE 802.1D bridging)
Definition: bridge-helper.h:36
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:177
static void Destroy(void)
Every event scheduled by the Simulator::insertAtDestroy method is invoked.
Definition: simulator.cc:121
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
keep track of a set of node pointers.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
NetDeviceContainer Install(Ptr< Node > node, NetDeviceContainer c)
This method creates an ns3::BridgeNetDevice with the attributes configured by BridgeHelper::SetDevice...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:38
hold objects of type ns3::Address
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
hold objects of type ns3::DataRate
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::PacketSinkApplication on each node of the input container configured with all the att...
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:318