Flow monitor for csma-multicast

309 views
Skip to first unread message

Eddy

unread,
Jan 19, 2017, 1:16:06 AM1/19/17
to ns-3-users
Hi all,

I have simulated csma-multicast file with flow monitor module added in it, but the traffic statistics were not captured during this simulation. When I have gone through other threads, I came to know that flow monitor supports only for unicast and not for broadcast/multicast. Is it still the same? If so, could someone guide me how to get the traffic statistics with the help of flow montior for the csma-multicast file. I have provided the code below.


#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"
#include "ns3/flow-monitor-module.h"
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("CsmaMulticastExample");
void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon);

int 
main (int argc, char *argv[])
{
  //
  // 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;
  cmd.Parse (argc, argv);

  NS_LOG_INFO ("Create nodes.");
  NodeContainer c;
  c.Create (5);
  // 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));

  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);

  //
  // 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.");

  uint16_t multicastPort = 9;   // Discard port (RFC 863)

  // Configure a multicast packet generator that generates a packet
  // every few seconds
  OnOffHelper onoff ("ns3::UdpSocketFactory", 
                     Address (InetSocketAddress (multicastGroup, multicastPort)));
  onoff.SetConstantRate (DataRate ("255b/s"));
  onoff.SetAttribute ("PacketSize", UintegerValue (128));

  ApplicationContainer srcC = onoff.Install (c0.Get (0));

  //
  // Tell the application when to start and stop.
  //
  srcC.Start (Seconds (1.));
  srcC.Stop (Seconds (10.));

  // Create an optional packet sink to receive these packets
  PacketSinkHelper sink ("ns3::UdpSocketFactory",
                         InetSocketAddress (Ipv4Address::GetAny (), multicastPort));

  ApplicationContainer sinkC = sink.Install (c1.Get (2)); // Node n4 
  // Start the sink
  sinkC.Start (Seconds (1.0));
  sinkC.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);


  ///
  // FlowMonitor
  ///
  FlowMonitorHelper fmHelper;
  Ptr<FlowMonitor> allMon = fmHelper.InstallAll();
  Simulator::Schedule(Seconds(20),&ThroughputMonitor,&fmHelper, allMon);


  //
  // Now, do the actual simulation.
  //
    NS_LOG_INFO ("Run Simulation.");
    Simulator::Stop(Seconds(20.));
    Simulator::Run ();

   // ThroughputMonitor(&fmHelper, allMon);

    Simulator::Destroy ();
    NS_LOG_INFO ("Done.");
  }



  void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon)
  {
  std::map<FlowId, FlowMonitor::FlowStats> flowStats = flowMon->GetFlowStats();
  Ptr<Ipv4FlowClassifier> classing = DynamicCast<Ipv4FlowClassifier> (fmhelper->GetClassifier());
  for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator stats = flowStats.begin (); stats != flowStats.end (); ++stats)
  {
  Ipv4FlowClassifier::FiveTuple fiveTuple = classing->FindFlow (stats->first);
  std::cout<<"Flow ID : " << stats->first <<" ; "<< fiveTuple.sourceAddress <<" -----> "<<fiveTuple.destinationAddress<<std::endl;
  // std::cout<<"Tx Packets = " << stats->second.txPackets<<std::endl;
  // std::cout<<"Rx Packets = " << stats->second.rxPackets<<std::endl;
  std::cout<<"Duration : "<<stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds()<<std::endl;
  std::cout<<"Last Received Packet : "<< stats->second.timeLastRxPacket.GetSeconds()<<" Seconds"<<std::endl;
  std::cout<<"Throughput: " << stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024  << " Mbps"<<std::endl;
  std::cout<<"---------------------------------------------------------------------------"<<std::endl;
  }
  Simulator::Schedule(Seconds(1),&ThroughputMonitor, fmhelper, flowMon);

  }


Regards,
Rex Stefan Edberg.

Konstantinos

unread,
Jan 19, 2017, 3:52:03 AM1/19/17
to ns-3-users
Hi Rex,

As you have found out, FlowMonitor does NOT support multicast/broadcast traffic.
You can not do anything in your scenario to change that behaviour. You would have to modify the implementation of FlowMonitor to capture multicast traffic.
There are other methods of capturing statistics in ns-3 that can be used in multicast/broadcast traffic. See the data collection framework https://www.nsnam.org/docs/models/html/data-collection.html
if you search the list, you can find several discussions on the usage of stats module. There are also some examples in the code.

Just one comment on your code. You have a single sender and single receiver. How does this relate to multicast? 
Do you want to implement multi-path instead?

Regards
K


On Thursday, January 19, 2017 at 6:16:06 AM UTC, Eddy wrote:
Hi all,

I have simulated csma-multicast file with flow monitor module added in it, but the traffic statistics were not captured during this simulation. When I have gone through other threads, I came to know that flow monitor supports only for unicast and not for broadcast/multicast. Is it still the same? If so, could someone guide me how to get the traffic statistics with the help of flow montior for the csma-multicast file. I have provided the code below.
 

Regards,
Rex Stefan Edberg.

Eddy

unread,
Jan 21, 2017, 4:08:13 AM1/21/17
to ns-3-users
Dear Konstantinos,

Thank you so much for your reply. I have taken the csma-multicast example file from NS-3 folder and added the flow monitor part. As the packets are sent from one node to all other nodes simultaneously, I believe the code provided above is multicast. Since I haven't find any examples for stats module, could you please kindly help me where I can find one simple example of the usage of stats module. It would be a great help for me to proceed further. Thank you once again.

Regards,
Rex
Reply all
Reply to author
Forward
0 new messages