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 //        n0     n1
20 //        |      |
21 //       ----------
22 //       | Switch |
23 //       ----------
24 //        |      |
25 //        n2     n3
26 //
27 //
28 // - CBR/UDP flows from n0 to n1 and from n3 to n0
29 // - DropTail queues
30 // - Tracing of queues and packet receptions to file "csma-bridge.tr"
31 
32 #include <iostream>
33 #include <fstream>
34 
35 #include "ns3/core-module.h"
36 #include "ns3/network-module.h"
37 #include "ns3/applications-module.h"
38 #include "ns3/bridge-module.h"
39 #include "ns3/csma-module.h"
40 #include "ns3/internet-module.h"
41 
42 /**
43  * \file
44  * \ingroup bridge
45  * Bridge example connecting four nodes,
46  * with the bridge acting as a simple switch.
47  */
48 
49 using namespace ns3;
50 
51 NS_LOG_COMPONENT_DEFINE ("CsmaBridgeExample");
52 
53 int
main(int argc,char * argv[])54 main (int argc, char *argv[])
55 {
56   //
57   // Users may find it convenient to turn on explicit debugging
58   // for selected modules; the below lines suggest how to do this
59   //
60 #if 0
61   LogComponentEnable ("CsmaBridgeExample", LOG_LEVEL_INFO);
62 #endif
63 
64   //
65   // Allow the user to override any of the defaults and the above Bind() at
66   // run-time, via command-line arguments
67   //
68   CommandLine cmd (__FILE__);
69   cmd.Parse (argc, argv);
70 
71   //
72   // Explicitly create the nodes required by the topology (shown above).
73   //
74   NS_LOG_INFO ("Create nodes.");
75   NodeContainer terminals;
76   terminals.Create (4);
77 
78   NodeContainer csmaSwitch;
79   csmaSwitch.Create (1);
80 
81   NS_LOG_INFO ("Build Topology");
82   CsmaHelper csma;
83   csma.SetChannelAttribute ("DataRate", DataRateValue (5000000));
84   csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
85 
86   // Create the csma links, from each terminal to the switch
87 
88   NetDeviceContainer terminalDevices;
89   NetDeviceContainer switchDevices;
90 
91   for (int i = 0; i < 4; i++)
92     {
93       NetDeviceContainer link = csma.Install (NodeContainer (terminals.Get (i), csmaSwitch));
94       terminalDevices.Add (link.Get (0));
95       switchDevices.Add (link.Get (1));
96     }
97 
98   // Create the bridge netdevice, which will do the packet switching
99   Ptr<Node> switchNode = csmaSwitch.Get (0);
100   BridgeHelper bridge;
101   bridge.Install (switchNode, switchDevices);
102 
103   // Add internet stack to the terminals
104   InternetStackHelper internet;
105   internet.Install (terminals);
106 
107   // We've got the "hardware" in place.  Now we need to add IP addresses.
108   //
109   NS_LOG_INFO ("Assign IP Addresses.");
110   Ipv4AddressHelper ipv4;
111   ipv4.SetBase ("10.1.1.0", "255.255.255.0");
112   ipv4.Assign (terminalDevices);
113 
114   //
115   // Create an OnOff application to send UDP datagrams from node zero to node 1.
116   //
117   NS_LOG_INFO ("Create Applications.");
118   uint16_t port = 9;   // Discard port (RFC 863)
119 
120   OnOffHelper onoff ("ns3::UdpSocketFactory",
121                      Address (InetSocketAddress (Ipv4Address ("10.1.1.2"), port)));
122   onoff.SetConstantRate (DataRate ("500kb/s"));
123 
124   ApplicationContainer app = onoff.Install (terminals.Get (0));
125   // Start the application
126   app.Start (Seconds (1.0));
127   app.Stop (Seconds (10.0));
128 
129   // Create an optional packet sink to receive these packets
130   PacketSinkHelper sink ("ns3::UdpSocketFactory",
131                          Address (InetSocketAddress (Ipv4Address::GetAny (), port)));
132   app = sink.Install (terminals.Get (1));
133   app.Start (Seconds (0.0));
134 
135   //
136   // Create a similar flow from n3 to n0, starting at time 1.1 seconds
137   //
138   onoff.SetAttribute ("Remote",
139                       AddressValue (InetSocketAddress (Ipv4Address ("10.1.1.1"), port)));
140   app = onoff.Install (terminals.Get (3));
141   app.Start (Seconds (1.1));
142   app.Stop (Seconds (10.0));
143 
144   app = sink.Install (terminals.Get (0));
145   app.Start (Seconds (0.0));
146 
147   NS_LOG_INFO ("Configure Tracing.");
148 
149   //
150   // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
151   // Trace output will be sent to the file "csma-bridge.tr"
152   //
153   AsciiTraceHelper ascii;
154   csma.EnableAsciiAll (ascii.CreateFileStream ("csma-bridge.tr"));
155 
156   //
157   // Also configure some tcpdump traces; each interface will be traced.
158   // The output files will be named:
159   //     csma-bridge-<nodeId>-<interfaceId>.pcap
160   // and can be read by the "tcpdump -r" command (use "-tt" option to
161   // display timestamps correctly)
162   //
163   csma.EnablePcapAll ("csma-bridge", false);
164 
165   //
166   // Now, do the actual simulation.
167   //
168   NS_LOG_INFO ("Run Simulation.");
169   Simulator::Run ();
170   Simulator::Destroy ();
171   NS_LOG_INFO ("Done.");
172 }
173