NS3 Multicast Example
Based on https://www.nsnam.org/doxygen/csma-multicast_8cc_source.html, I wrote this lab.
[topology]
csma-multicast.cc (put this file under scratch)
#include <iostream> #include <fstream>
#include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/csma-module.h" #include "ns3/applications-module.h" #include "ns3/internet-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("CsmaMulticastExample");
int main (int argc, char *argv[]) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
// // Users may find it convenient to turn on explicit debugging // for selected modules; the below lines suggest how to do this // // LogComponentEnable ("CsmaMulticastExample", LOG_LEVEL_INFO);
// // Set up default values for the simulation. // // Select DIX/Ethernet II-style encapsulation (no LLC/Snap header)
Config::SetDefault
("ns3::CsmaNetDevice::EncapsulationMode",
StringValue ("Dix"));
// Allow the user to override any of the defaults at // run-time, via command-line arguments CommandLine cmd (__FILE__); cmd.Parse (argc, argv);
NS_LOG_INFO ("Create nodes."); NodeContainer c; c.Create (6); // We will later want two subcontainers of these nodes, for the two LANs NodeContainer c0 = NodeContainer (c.Get (0), c.Get (1), c.Get (2)); NodeContainer c1 = NodeContainer (c.Get (2), c.Get (3), c.Get (4), c.Get (5));
NS_LOG_INFO ("Build Topology."); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
// We will use these NetDevice containers later, for IP addressing NetDeviceContainer nd0 = csma.Install (c0); // First LAN NetDeviceContainer nd1 = csma.Install (c1); // Second LAN
NS_LOG_INFO ("Add IP Stack."); InternetStackHelper internet; internet.Install (c);
NS_LOG_INFO ("Assign IP Addresses."); Ipv4AddressHelper ipv4Addr; ipv4Addr.SetBase ("10.1.1.0", "255.255.255.0"); ipv4Addr.Assign (nd0); ipv4Addr.SetBase ("10.1.2.0", "255.255.255.0"); ipv4Addr.Assign (nd1);
NS_LOG_INFO ("Configure multicasting."); // // Now we can configure multicasting. As described above, the multicast // source is at node zero, which we assigned the IP address of 10.1.1.1 // earlier. We need to define a multicast group to send packets to. This // can be any multicast address from 224.0.0.0 through 239.255.255.255 // (avoiding the reserved routing protocol addresses). //
Ipv4Address multicastSource
("10.1.1.1"); Ipv4Address multicastGroup
("225.1.2.4");
// Now, we will set up multicast routing. We need to do three things: // 1) Configure a (static) multicast route on node n2 // 2) Set up a default multicast route on the sender n0 // 3) Have node n4 join the multicast group // We have a helper that can help us with static multicast Ipv4StaticRoutingHelper multicast;
// 1) Configure a (static) multicast route on node n2 (multicastRouter)
Ptr<Node> multicastRouter = c.Get (2); //
The node in question Ptr<NetDevice> inputIf = nd0.Get
(2); // The input NetDevice NetDeviceContainer
outputDevices;
// A container of output NetDevices outputDevices.Add
(nd1.Get (0)); // (we only need
one NetDevice here) multicast.AddMulticastRoute
(multicastRouter, multicastSource,
multicastGroup, inputIf,
outputDevices);
// 2) Set up a default multicast route on the sender n0
Ptr<Node> sender = c.Get (0); Ptr<NetDevice> senderIf =
nd0.Get (0); multicast.SetDefaultMulticastRoute
(sender, senderIf);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
// // Create an OnOff application to send UDP datagrams from node zero to the // multicast group (node four will be listening). // NS_LOG_INFO ("Create Applications.");
UdpEchoClientHelper echoClient (Address (InetSocketAddress (multicastGroup, 9))); echoClient.SetAttribute ("MaxPackets", UintegerValue (1)); echoClient.SetAttribute ("Interval", TimeValue (Seconds (1.0))); echoClient.SetAttribute ("PacketSize", UintegerValue (1024)); ApplicationContainer clientApps = echoClient.Install (c0.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (10.0));
UdpEchoServerHelper echoServer (9);
ApplicationContainer serverApps = echoServer.Install
(c1.Get (1)); serverApps.Add(echoServer.Install (c1.Get (3))); serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (10.0));
NS_LOG_INFO ("Configure Tracing."); // // Configure tracing of all enqueue, dequeue, and NetDevice receive events. // Ascii trace output will be sent to the file "csma-multicast.tr" // AsciiTraceHelper ascii; csma.EnableAsciiAll (ascii.CreateFileStream ("csma-multicast.tr"));
// Also configure some tcpdump traces; each interface will be traced. // The output files will be named: // csma-multicast-<nodeId>-<interfaceId>.pcap // and can be read by the "tcpdump -r" command (use "-tt" option to // display timestamps correctly) csma.EnablePcapAll ("csma-multicast", false);
// // Now, do the actual simulation. // NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); } |
Execution
N0 (10.1.1.1) sends out a multicast packet with destination IP 225.1.2.4. But only N3 (10.1.2.2) and N5 (10.1.2.4) have an application that can receive the packet and send the echo packet back to the sender.
Last
Modified: 2022/2/12 done
[Author]
Dr. Chih-Heng Ke
Department of Computer Science
and Information Engineering, National Quemoy University, Kinmen, Taiwan
Email: smallko@gmail.com