A Discrete-Event Network Simulator
API
csma-bridge.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// 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
49using namespace ns3;
50
51NS_LOG_COMPONENT_DEFINE ("CsmaBridgeExample");
52
53int
54main (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");
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.");
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",
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}
a polymophic address class
Definition: address.h:91
AttributeValue implementation for Address.
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.
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
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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
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.
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