A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
csma-multicast.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// Lan1
19// ===========
20// | | |
21// n0 n1 n2 n3 n4
22// | | |
23// ===========
24// Lan0
25//
26// - Multicast source is at node n0;
27// - Multicast forwarded by node n2 onto LAN1;
28// - Nodes n0, n1, n2, n3, and n4 receive the multicast frame.
29// - Node n4 listens for the data
30
31#include "ns3/applications-module.h"
32#include "ns3/core-module.h"
33#include "ns3/csma-module.h"
34#include "ns3/internet-module.h"
35#include "ns3/network-module.h"
36
37#include <fstream>
38#include <iostream>
39
40using namespace ns3;
41
42NS_LOG_COMPONENT_DEFINE("CsmaMulticastExample");
43
44int
45main(int argc, char* argv[])
46{
47 //
48 // Users may find it convenient to turn on explicit debugging
49 // for selected modules; the below lines suggest how to do this
50 //
51 // LogComponentEnable ("CsmaMulticastExample", LOG_LEVEL_INFO);
52
53 //
54 // Set up default values for the simulation.
55 //
56 // Select DIX/Ethernet II-style encapsulation (no LLC/Snap header)
57 Config::SetDefault("ns3::CsmaNetDevice::EncapsulationMode", StringValue("Dix"));
58
59 // Allow the user to override any of the defaults at
60 // run-time, via command-line arguments
61 CommandLine cmd(__FILE__);
62 cmd.Parse(argc, argv);
63
64 NS_LOG_INFO("Create nodes.");
66 c.Create(5);
67 // We will later want two subcontainers of these nodes, for the two LANs
68 NodeContainer c0 = NodeContainer(c.Get(0), c.Get(1), c.Get(2));
69 NodeContainer c1 = NodeContainer(c.Get(2), c.Get(3), c.Get(4));
70
71 NS_LOG_INFO("Build Topology.");
73 csma.SetChannelAttribute("DataRate", DataRateValue(DataRate(5000000)));
74 csma.SetChannelAttribute("Delay", TimeValue(MilliSeconds(2)));
75
76 // We will use these NetDevice containers later, for IP addressing
77 NetDeviceContainer nd0 = csma.Install(c0); // First LAN
78 NetDeviceContainer nd1 = csma.Install(c1); // Second LAN
79
80 NS_LOG_INFO("Add IP Stack.");
82 internet.Install(c);
83
84 NS_LOG_INFO("Assign IP Addresses.");
85 Ipv4AddressHelper ipv4Addr;
86 ipv4Addr.SetBase("10.1.1.0", "255.255.255.0");
87 ipv4Addr.Assign(nd0);
88 ipv4Addr.SetBase("10.1.2.0", "255.255.255.0");
89 ipv4Addr.Assign(nd1);
90
91 NS_LOG_INFO("Configure multicasting.");
92 //
93 // Now we can configure multicasting. As described above, the multicast
94 // source is at node zero, which we assigned the IP address of 10.1.1.1
95 // earlier. We need to define a multicast group to send packets to. This
96 // can be any multicast address from 224.0.0.0 through 239.255.255.255
97 // (avoiding the reserved routing protocol addresses).
98 //
99
100 Ipv4Address multicastSource("10.1.1.1");
101 Ipv4Address multicastGroup("225.1.2.4");
102
103 // Now, we will set up multicast routing. We need to do three things:
104 // 1) Configure a (static) multicast route on node n2
105 // 2) Set up a default multicast route on the sender n0
106 // 3) Have node n4 join the multicast group
107 // We have a helper that can help us with static multicast
108 Ipv4StaticRoutingHelper multicast;
109
110 // 1) Configure a (static) multicast route on node n2 (multicastRouter)
111 Ptr<Node> multicastRouter = c.Get(2); // The node in question
112 Ptr<NetDevice> inputIf = nd0.Get(2); // The input NetDevice
113 NetDeviceContainer outputDevices; // A container of output NetDevices
114 outputDevices.Add(nd1.Get(0)); // (we only need one NetDevice here)
115
116 multicast.AddMulticastRoute(multicastRouter,
117 multicastSource,
118 multicastGroup,
119 inputIf,
120 outputDevices);
121
122 // 2) Set up a default multicast route on the sender n0
123 Ptr<Node> sender = c.Get(0);
124 Ptr<NetDevice> senderIf = nd0.Get(0);
125 multicast.SetDefaultMulticastRoute(sender, senderIf);
126
127 //
128 // Create an OnOff application to send UDP datagrams from node zero to the
129 // multicast group (node four will be listening).
130 //
131 NS_LOG_INFO("Create Applications.");
132
133 uint16_t multicastPort = 9; // Discard port (RFC 863)
134
135 // Configure a multicast packet generator that generates a packet
136 // every few seconds
137 OnOffHelper onoff("ns3::UdpSocketFactory",
138 Address(InetSocketAddress(multicastGroup, multicastPort)));
139 onoff.SetConstantRate(DataRate("255b/s"));
140 onoff.SetAttribute("PacketSize", UintegerValue(128));
141
142 ApplicationContainer srcC = onoff.Install(c0.Get(0));
143
144 //
145 // Tell the application when to start and stop.
146 //
147 srcC.Start(Seconds(1.));
148 srcC.Stop(Seconds(10.));
149
150 // Create an optional packet sink to receive these packets
151 PacketSinkHelper sink("ns3::UdpSocketFactory",
152 InetSocketAddress(Ipv4Address::GetAny(), multicastPort));
153
154 ApplicationContainer sinkC = sink.Install(c1.Get(2)); // Node n4
155 // Start the sink
156 sinkC.Start(Seconds(1.0));
157 sinkC.Stop(Seconds(10.0));
158
159 NS_LOG_INFO("Configure Tracing.");
160 //
161 // Configure tracing of all enqueue, dequeue, and NetDevice receive events.
162 // Ascii trace output will be sent to the file "csma-multicast.tr"
163 //
164 AsciiTraceHelper ascii;
165 csma.EnableAsciiAll(ascii.CreateFileStream("csma-multicast.tr"));
166
167 // Also configure some tcpdump traces; each interface will be traced.
168 // The output files will be named:
169 // csma-multicast-<nodeId>-<interfaceId>.pcap
170 // and can be read by the "tcpdump -r" command (use "-tt" option to
171 // display timestamps correctly)
172 csma.EnablePcapAll("csma-multicast", false);
173
174 //
175 // Now, do the actual simulation.
176 //
177 NS_LOG_INFO("Run Simulation.");
180 NS_LOG_INFO("Done.");
181
182 return 0;
183}
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
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: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.
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.
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:42
static Ipv4Address GetAny()
Helper class that adds ns3::Ipv4StaticRouting objects.
void AddMulticastRoute(Ptr< Node > n, Ipv4Address source, Ipv4Address group, Ptr< NetDevice > input, NetDeviceContainer output)
Add a multicast route to a node and net device using explicit Ptr<Node> and Ptr<NetDevice>
void SetDefaultMulticastRoute(Ptr< Node > n, Ptr< NetDevice > nd)
Add a default route to the static routing protocol to forward packets out a particular interface.
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: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
Hold variables of type string.
Definition: string.h:56
AttributeValue implementation for Time.
Definition: nstime.h:1406
Hold an unsigned integer type.
Definition: uinteger.h:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:894
#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.
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