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