src/dce/example/quagga-rocketfuel.cc
author Hajime Tazaki <tazaki@sfc.wide.ad.jp>
Thu, 24 Nov 2011 19:03:11 +0900
changeset 6796 e0c3cc1af3d9
parent 6795 a3420f767e78
permissions -rw-r--r--
fix the wrong url issues

/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */

#include "ns3/core-module.h"
#include "ns3/helper-module.h"
#include "ns3/mobility-module.h"
#include "ns3/dce-module.h"
#include "ns3/quagga-helper.h"
#include "ns3/simulator-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/node-module.h"
#include "ns3/log.h"
#include "ns3/wifi-module.h"
#include "ns3/ipv4-l3-protocol.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/ipv6-l3-protocol.h"
#include "ns3/ipv6-header.h"
#include "ns3/icmpv6-header.h"
#include "ns3/ipv6-static-routing.h"
#include "ns3/udp-header.h"
#include "ns3/realtime-simulator-impl.h"
#include "ns3/visualizer.h"
#include "ns3/pyviz.h"
#include "ns3/v4ping-module.h"
#include "ns3/mpi-interface.h"

#include <sys/resource.h>

#ifdef NS3_MPI
#include <mpi.h>
#endif
using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("quagga-rocketfuel");

// Parameters
uint32_t nNodes = 2;
uint32_t stopTime = 600;

static void
SetRlimit ()
{
  int ret;
  struct rlimit limit;
  limit.rlim_cur = 1000000;
  limit.rlim_max = 1000000;

  ret = setrlimit(RLIMIT_NOFILE, &limit);
  if (ret == -1)
    {
       perror ("setrlimit");
    }
  return;
}

int
main (int argc, char *argv[])
{
  LogComponentEnable ("quagga-rocketfuel", LOG_LEVEL_INFO);
#ifdef NS3_MPI
  // Distributed simulation setup
  MpiInterface::Enable (&argc, &argv);
  GlobalValue::Bind ("SimulatorImplementationType",
                     StringValue ("ns3::DistributedSimulatorImpl"));

  uint32_t systemId = MpiInterface::GetSystemId ();
  uint32_t systemCount = MpiInterface::GetSize ();
#endif

  CommandLine cmd;
  cmd.AddValue ("nNodes", "Number of Router nodes", nNodes);
  cmd.AddValue ("stopTime", "Time to stop(seconds)", stopTime);
  cmd.Parse (argc,argv);

  SetRlimit ();

  // 
  // Step o
  // Read Topology information
  // ------------------------------------------------------------
  // -- Read topology data.
  // --------------------------------------------

  // Pick a topology reader based in the requested format.

  Ptr<TopologyReader> inFile = 0;
  TopologyReaderHelper topoHelp;
  
  NodeContainer nodes;
  
  string format ("Rocketfuel");
  string input ("examples/topology-read/RocketFuel_toposample_1239_weights.txt");
  //string input ("3967.weights.intra");

  topoHelp.SetFileName(input);
  topoHelp.SetFileType(format);
  inFile = topoHelp.GetTopologyReader();

  if (inFile != 0)
    {
      nodes = inFile->Read ();
    }

  if (nodes.GetN () == 0)
    {
      NS_LOG_ERROR ("Problems reading node information the topology file. Failing.");
      return -1;
    }
  if (inFile->LinksSize () == 0)
    {
      NS_LOG_ERROR ("Problems reading the topology file. Failing.");
      return -1;
    }
  NS_LOG_INFO ("Rocketfuel topology created with " << nodes.GetN () << " nodes and " << 
               inFile->LinksSize () << " links (from " << input << ")");

  // Tricky things XXX
  // Change systemId for MPI by hand...
#ifdef NS3_MPI
  for (uint32_t i = 0; i < nodes.GetN (); i++)
    {
      nodes.Get (i)->SetSystemId (i % systemCount);
    }
#endif

  // 
  //  Step 1
  //  Node Basic Configuration
  // 

  // Internet stack install
  InternetStackHelper stack;    // IPv4 is required for GlobalRouteMan
  stack.Install (nodes);

  // 
  // Step 2
  // Address Configuration
  // 
  // 
  Ipv4AddressHelper ipv4AddrHelper;

  NS_LOG_INFO ("creating ip4 addresses");
  Ipv4AddressHelper address;
  address.SetBase ("10.0.0.0", "255.255.255.252");

  int totlinks = inFile->LinksSize ();
  NS_LOG_INFO ("creating node containers");
  NodeContainer nc[totlinks];
  TopologyReader::ConstLinksIterator iter;
  int i = 0;
  for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
    {
      nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ());
    }


  NS_LOG_INFO ("creating net device containers");
  NetDeviceContainer ndc[totlinks];
  PointToPointHelper p2p;
  for (int i = 0; i < totlinks; i++)
    {
      // p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
      p2p.SetChannelAttribute ("Delay", StringValue ("1ns"));
      p2p.SetDeviceAttribute ("DataRate", StringValue ("100Mbps"));
      ndc[i] = p2p.Install (nc[i]);
    }
  //  p2p.EnablePcapAll ("quagga-rocketfuel");

  NS_LOG_INFO ("creating ipv4 interfaces");
  Ipv4InterfaceContainer ipic[totlinks];
  for (int i = 0; i < totlinks; i++)
    {
      ipic[i] = address.Assign (ndc[i]);
      address.NewNetwork ();
    }


  //  
  //  Application configuration for Nodes
  // 
  NS_LOG_INFO ("creating all the route");
  //  Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

  NS_LOG_INFO ("creating quagga process");
  //run quagga programs in every node
  DceManagerHelper processManager;
  QuaggaHelper quagga;

  // 
  // Step 3
  // Traffic configuration
  // 
#ifdef NS3_MPI
  if (systemId == 0)
#endif
    {
      Ptr<V4Ping> app = CreateObject<V4Ping> ();
      //Ptr<Ipv4> ipv4Server = nodes.Get (25)->GetObject<Ipv4> ();
      Ptr<Ipv4> ipv4Server = nodes.Get (nodes.GetN () - 1)->GetObject<Ipv4> ();
      app->SetAttribute ("Remote", Ipv4AddressValue (ipv4Server->GetAddress (1, 0).GetLocal ()));
      app->SetAttribute ("Verbose", BooleanValue (true));
      nodes.Get (0)->AddApplication (app);
      app->SetStartTime (Seconds (20.0));
      app->SetStopTime (Seconds (stopTime));
    }

  for (uint32_t i = 0; i < nodes.GetN (); i++)
    {
#ifdef NS3_MPI
      if (i % systemCount == systemId % systemCount)
#endif
        {
          //     std::cout << "[" << systemId << "] start quagga Node " << i << std::endl;
          processManager.Install (nodes.Get (i));
          quagga.EnableOspf (nodes.Get (i));
          quagga.EnableZebraDebug (nodes.Get (i));
          quagga.Install (nodes.Get (i));
        }
    }

  // 
  // Step 9
  // Now It's ready to GO!
  // 
  if (stopTime != 0)
    {
      Simulator::Stop (Seconds (stopTime));
    }
  //  Visualizer::Run ();
  Simulator::Run ();
  Simulator::Destroy ();


  std::cout << "End of experiment" << std::endl;

  return 0;
}