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