Saturday, 26 December 2015

Mobile Adhoc network in NS3 / (MANET Implementation in NS3)

Mobile Adhoc network in NS3 / (MANET Implementation in NS3)

INTRODUCTION

•  MANETs are typically
–  wireless
–  mobile
–  little or no reliance on infrastructure
–  communication among peers
–  limited network resources

MANET Routing Protocols in ns-3

•  AODV:  ad-hoc on demand distance vector
•  DSDV:  destination-sequenced distance vector
•  DSR:  dynamic source routing 
•  OLSR:  optimised link state routing

Ad Hoc Routing Examples

OLSR in ns-3

OlsrHelper olsr;
Ipv4StaticRoutingHelper
staticRouting;
Ipv4ListRoutingHelper list;
list.Add (staticRouting,0);
list.Add (olsr,10);
InternetStackHelper internet;
internet.SetRoutingHelper (list);
internet.Install (nodes);

AODV in ns-3

AodvHelper aodv;
InternetStackHelper stack;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0","255.0.0.0");
address.Assign (devices);


Implementation in NS3:


#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/internet-module.h"
#include "ns3/aodv-module.h"
#include "ns3/olsr-module.h"
#include "ns3/dsdv-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/constant-velocity-mobility-model.h"
#include "ns3/flow-monitor-module.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>


NS_LOG_COMPONENT_DEFINE ("scenario");

using namespace ns3;

Ptr<ConstantVelocityMobilityModel> cvmm;
double position_interval = 1.0;
std::string tracebase = "scratch/scenario";

// two callbacks
void printPosition()
{
  Vector thePos = cvmm->GetPosition();
  Simulator::Schedule(Seconds(position_interval), &printPosition);
  std::cout << "position: " << thePos << std::endl;
}

void stopMover()
{
  cvmm -> SetVelocity(Vector(0,0,0));
}

int main (int argc, char *argv[])
{
  // Dsss options: DsssRate1Mbps, DsssRate2Mbps, DsssRate5_5Mbps, DsssRate11Mbps
  // also ErpOfdmRate to 54 Mbps and OfdmRate to 150Mbps w 40MHz band-width
  std::string phyMode = "DsssRate1Mbps";               
  //phyMode = "DsssRate11Mbps";

  int bottomrow = 20;            // number of bottom-row nodes
  int spacing = 200;            // between bottom-row nodes
  int mheight = 150;            // height of mover above bottom row
  int brheight = 50;            // height of bottom row

  int X = (5-1)*spacing+1;              // X and Y are the dimensions of the field
  //int Y = 300; a
  int packetsize = 500;
  double factor = 1.0;  // allows slowing down rate and extending runtime; same total # of packets
  int endtime = (int)100*factor;
  double speed = (X-1.0)/endtime;      
  double bitrate = 80*1000.0/factor;  // bits/sec
  uint32_t interval = 1000*packetsize*8/bitrate*1000;    // in microsec, computed from packetsize and bitrate
  uint32_t packetcount = 1000000*endtime/ interval;
  std::cout << "interval = " << interval <<", rate=" << bitrate << ", packetcount=" << packetcount << std::endl;

  CommandLine cmd;              // no options, actually
  cmd.Parse (argc, argv);

  // disable fragmentation for frames below 2200 bytes
  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
  // turn off RTS/CTS for frames below 2200 bytes
  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
  // Set non-unicast data rate to be the same as that of unicast
  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));

  // Create nodes
  NodeContainer fixedpos;
  fixedpos.Create(bottomrow);
  Ptr<Node> lowerleft = fixedpos.Get(0);

  // The below set of helpers will help us to put together the desired Wi-Fi behavior
  WifiHelper wifi;
  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); // Use AARF rate control
  // to view AARF rate changes, set in the shell NS_LOG=AarfWifiManager=level_debug

  // The PHY layer here is "yans"
  YansWifiPhyHelper wifiPhyHelper =  YansWifiPhyHelper::Default ();
  // for .pcap tracing
  // wifiPhyHelper.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);

  YansWifiChannelHelper wifiChannelHelper;              // *not* ::Default() !
  wifiChannelHelper.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); // pld: default?
  // the following has an absolute cutoff at distance > 250
  wifiChannelHelper.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue(250));
  Ptr<YansWifiChannel> pchan = wifiChannelHelper.Create ();
  wifiPhyHelper.SetChannel (pchan);

  // Add a non-QoS upper-MAC layer "AdhocWifiMac", and set rate control
  NqosWifiMacHelper wifiMacHelper = NqosWifiMacHelper::Default ();
  wifiMacHelper.SetType ("ns3::AdhocWifiMac");
  NetDeviceContainer devices = wifi.Install (wifiPhyHelper, wifiMacHelper, fixedpos);
  devices.Add (wifi.Install (wifiPhyHelper, wifiMacHelper, mover));

  // set positions.
  MobilityHelper sessile;               // for fixed nodes
  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  int Xpos = 0;
  for (int i=0; i<5; i++) {
        positionAlloc->Add(Vector(Xpos, brheight, 0.0));
        Xpos += spacing;
  }


  sessile.SetPositionAllocator (positionAlloc);
  sessile.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  sessile.Install (fixedpos);

  // ConstantVelocityMobilityModel is a subclass of MobilityModel
  Vector pos (0, mheight+brheight, 0);
  Vector vel (speed, 0, 0);
  MobilityHelper mobile;
  mobile.SetMobilityModel("ns3::ConstantVelocityMobilityModel");        // no Attributes
  mobile.Install(mover);
  cvmm = mover->GetObject<ConstantVelocityMobilityModel> ();
  cvmm->SetPosition(pos);
  cvmm->SetVelocity(vel);
 
  AodvHelper aodv;
  OlsrHelper olsr;
  DsdvHelper dsdv;
  Ipv4ListRoutingHelper listrouting;
  //listrouting.Add(olsr, 10);                          // generates less traffic
  listrouting.Add(aodv, 10);                            // fastest to find new routes

  InternetStackHelper internet;
  internet.SetRoutingHelper(listrouting);
  internet.Install (fixedpos);
  internet.Install (mover);

  Ipv4AddressHelper ipv4;
  NS_LOG_INFO ("Assign IP Addresses.");
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");           // there is only one subnet
  Ipv4InterfaceContainer i = ipv4.Assign (devices);
 
  uint16_t port = 80;

  // create a receiving application (UdpServer) on node mover


  Address sinkaddr(InetSocketAddress (Ipv4Address::GetAny(), port));
  Config::SetDefault("ns3::UdpServer::Port", UintegerValue(port));
//-----sample
  Ptr<UdpServer> UdpRecvApp = CreateObject<UdpServer>();
  UdpRecvApp->SetStartTime(Seconds(0.0));
  UdpRecvApp->SetStopTime(Seconds(endtime));
  mover->AddApplication(UdpRecvApp);
// mover definition needed

  // Tracing
  //wifiPhyHelper.EnablePcap (tracebase, devices);

  AsciiTraceHelper ascii;
  wifiPhyHelper.EnableAsciiAll (ascii.CreateFileStream (tracebase + ".tr"));

  // create animation file, to be run with 'netanim'
  AnimationInterface anim (tracebase + ".xml");
  anim.SetMobilityPollInterval(Seconds(0.1));

  // uncomment the next line to verify that node 'mover' is actually moving
  //Simulator::Schedule(Seconds(position_interval), &printPosition);

  Simulator::Schedule(Seconds(endtime), &stopMover);
anim.UpdateNodeDescription (fixedpos.Get(0), "AP");

  FlowMonitorHelper flowmon;
  Ptr<FlowMonitor> monitor = flowmon.InstallAll();
  Simulator::Stop(Seconds (endtime+60));
  Simulator::Run ();
monitor->CheckForLostPackets ();
static double throughput =0;
static double Total_tx =0;
static double Total_rx =0;
static double delay =0;
  Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
    {
      Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
      if ((1))
      {
       
// QOS Measurement
     }
  Simulator::Destroy ();


      }
  return 0;
}

 OUTPUT :

 








Video:

 

Mail to nsallinone@gmail.com to get the implementation

 

 

11 comments:

  1. hi, i am new on ns3, i try to run your code, but i got this error, could you help me please.

    ../scratch/escenario.cc: In function ‘int main(int, char**)’:
    ../scratch/escenario.cc:103:60: error: ‘mover’ was not declared in this scope
    devices.Add (wifi.Install (wifiPhyHelper, wifiMacHelper, mover));
    ^
    ../scratch/escenario.cc:125:56: error: expected primary-expression before ‘>’ token
    cvmm = mover->GetObject ();
    ^
    ../scratch/escenario.cc:125:59: error: expected primary-expression before ‘)’ token
    cvmm = mover->GetObject ();
    ^
    ../scratch/escenario.cc:180:15: error: unused variable ‘throughput’ [-Werror=unused-variable]
    static double throughput =0;
    ^
    ../scratch/escenario.cc:181:15: error: unused variable ‘Total_tx’ [-Werror=unused-variable]
    static double Total_tx =0;
    ^
    ../scratch/escenario.cc:182:15: error: unused variable ‘Total_rx’ [-Werror=unused-variable]
    static double Total_rx =0;
    ^
    ../scratch/escenario.cc:183:15: error: unused variable ‘delay’ [-Werror=unused-variable]
    static double delay =0;
    ^
    cc1plus: all warnings being treated as errors
    Waf: Leaving directory `/ns3/bake/source/ns-3.20/build'
    Build failed
    -> task in 'escenario' failed (exit status 1):
    {task 3062242572L: cxx escenario.cc -> escenario.cc.3.o}
    ['/usr/bin/g++', '-O0', '-ggdb', '-g3', '-Wall', '-Werror', '-Wno-error=deprecated-declarations', '-fstrict-aliasing', '-Wstrict-aliasing', '-pthread', '-fno-strict-aliasing', '-fwrapv', '-fstack-protector', '-fno-strict-aliasing', '-I.', '-I..', '-I/usr/include/python2.7', '-I/usr/include/i386-linux-gnu/python2.7', '-DNS3_ASSERT_ENABLE', '-DNS3_LOG_ENABLE', '-DHAVE_SYS_IOCTL_H=1', '-DHAVE_IF_NETS_H=1', '-DHAVE_NET_ETHERNET_H=1', '-DHAVE_PACKET_H=1', '-DHAVE_IF_TUN_H=1', '-DNDEBUG', '-D_FORTIFY_SOURCE=2', '../scratch/escenario.cc', '-c', '-o', 'scratch/escenario.cc.3.o']

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. PLS ADD THE FOLLOWING CODE
    "Ptr < Node> mover= CreateObject < Node>();"

    ReplyDelete
    Replies
    1. Hello!

      Then we get an error:

      ../scratch/first.cc: In function ‘int main(int, char**)’:
      ../scratch/first.cc:189:37: error: variable ‘t’ set but not used [-Werror=unused-but-set-variable]
      Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
      ^
      ../scratch/first.cc:181:15: error: unused variable ‘throughput’ [-Werror=unused-variable]
      static double throughput =0;
      ^
      ../scratch/first.cc:182:15: error: unused variable ‘Total_tx’ [-Werror=unused-variable]
      static double Total_tx =0;
      ^
      ../scratch/first.cc:183:15: error: unused variable ‘Total_rx’ [-Werror=unused-variable]
      static double Total_rx =0;
      ^
      ../scratch/first.cc:184:15: error: unused variable ‘delay’ [-Werror=unused-variable]
      static double delay =0;
      ^
      cc1plus: all warnings being treated as errors

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. I'm working in NS3.28.1
    and i have this error:
    ../scratch/adhoc.cc: In function ‘Results start()’:
    ../scratch/adhoc.cc:137:9: error: ‘NqosWifiMacHelper’ was not declared in this scope
    NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default();
    ^
    ../scratch/adhoc.cc:142:9: error: ‘wifiMac’ was not declared in this scope
    wifiMac.SetType("ns3::AdhocWifiMac");

    ReplyDelete
    Replies
    1. Have you solved this problem? I met the same problem and don't know how to fix it.

      Delete
    2. Have you solved this problem? I met the same problem and don't know how to fix it.

      Delete
    3. I have also the same problem. Have you found any solution? Thanks in advance

      Delete
    4. I had the same error:‘NqosWifiMacHelper’ was not declared in this scope.
      You can try to replace this to:
      WifiMacHelper wifiMacHelper;

      Delete
  6. I'm working in NS3.27
    and i have this error:
    Build failed
    -> task in 'first' failed (exit status 1):
    {task 3062140908L: cxx first.cc -> first.cc.6.o}
    ['/usr/bin/g++', '-O0', '-ggdb', '-g3', '-Wall', '-Werror', '-std=c++11', '-Wno-error=deprecated-declarations', '-fstrict-aliasing', '-Wstrict-aliasing', '-pthread', '-pthread', '-I.', '-I..', '-I/usr/include/gtk-2.0', '-I/usr/lib/i386-linux-gnu/gtk-2.0/include', '-I/usr/include/gio-unix-2.0', '-I/usr/include/cairo', '-I/usr/include/pango-1.0', '-I/usr/include/atk-1.0', '-I/usr/include/pixman-1', '-I/usr/include/libpng12', '-I/usr/include/gdk-pixbuf-2.0', '-I/usr/include/harfbuzz', '-I/usr/include/glib-2.0', '-I/usr/lib/i386-linux-gnu/glib-2.0/include', '-I/usr/include/freetype2', '-I/usr/include/libxml2', '-DNS3_BUILD_PROFILE_DEBUG', '-DNS3_ASSERT_ENABLE', '-DNS3_LOG_ENABLE', '-DHAVE_SYS_IOCTL_H=1', '-DHAVE_IF_NETS_H=1', '-DHAVE_NET_ETHERNET_H=1', '-DHAVE_PACKET_H=1', '-DHAVE_IF_TUN_H=1', '-DHAVE_GSL=1', '-DHAVE_SQLITE3=1', '-DHAVE_GTK2=1', '-DHAVE_LIBXML2=1', '../scratch/first.cc', '-c', '-o', '/home/naseer/repos22/ns-allinone-3.27/ns-3.27/build/scratch/first.cc.6.o']

    ReplyDelete