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