Wednesday, 11 July 2012

Runtime packet loss calculation in NS2

Hello friend, here I am going to present a method to calculate the runtime packet loss. I am going to show this using ns2 2.34 and AODV.
      Sometime we require packet loss to calculate trust value (in case of Trust Based protocols where it is done by no. of packets sent - no. of packets received).
       I am going to show the calculation for a particular pair of nodes.
Steps involved:-


A) We will have to add a node as a malicious node which will drop the packets
     intentionally. You can add a malicious node using this link.

B) Second, we'll set the AODV in promiscuous mode, where every node will
     listen to its neighbors.


1) We need to modify in total 3 files to set AODV in promiscuous mode, so it's
    good to take a backup of it.
    Files are:
    • ns-allinone-2.34/ns-2.34/aodv/aodv.cc
    • ns-allinone-2.34/ns-2.34/aodv/aodv.h
    • ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl
2) Open the file ns-allinone-2.34/ns-2.34/aodv/aodv.h in your favorite editor
    and make the changes as shown in blue color.
This code goes at top in header section.
  #include <mac.h>
 ..............................................
 ..............................................
//This code goes at line 128.
class MalTimer : public Handler {
public:
        MalTimer(AODV* a) : agent(a) {}
        void    handle(Event*);
private:
        AODV    *agent;
    Event    intr;
};
 
..............................................
 ..............................................
// This code is below the text 
/*
  The Routing Agent
*/
//Approximately at line 203

  class AODV: public Tap, public Agent {
  public:
  void  tap(const Packet *p);
  void  check_mal(void);
  int  fcount;
  int  rcount;
  ......
  protected:
  Mac *mac_;
  ......
 }
 ..............................................
 ..............................................
 //This code is below the text 
 /*
  * Timers
  */
// Approximately at line 308
 MalTimer        maltimer;
3) Open the file ns-allinone-2.34/ns-2.34/aodv/aodv.cc and make the changes as  
    shown in blue color.
 This blue code is approximately at line 94
if (strncasecmp(argv[1], "start", 2) == 0) {
            btimer.handle((Event*) 0);
            maltimer.handle((Event*) 0);
#ifndef AODV_LINK_LAYER_DETECTION
            htimer.handle((Event*) 0);
            ntimer.handle((Event*) 0);
#endif // LINK LAYER DETECTION
            rtimer.handle((Event*) 0);
            return TCL_OK;
        }
//  This blue code is approximately at line 133
int
AODV::command(int argc, const char* const * argv) {
......
else if(argc == 3) {
......
else if (strcmp(argv[1], "install-tap") == 0) {
mac_ = (Mac*)TclObject::lookup(argv[2]);
if (mac_ == 0) return TCL_ERROR;
mac_->installTap(this);
return TCL_OK;
}
}
return Agent::command(argc, argv);

}

 //Add the following blue code just below the above code

void
AODV::tap(const Packet *p) {
struct hdr_cmn* hdcmn = HDR_CMN(p);
    if (index == 0) {
        if (hdcmn->ptype_ == PT_CBR) {
            rcount++;
        }
    }
}
 ..............................................
 ..............................................
//This blue code is approximately at line 160 
AODV::AODV(nsaddr_t id) : Agent(PT_AODV),
              btimer(this), htimer(this), ntimer(this),
              rtimer(this), lrtimer(this), maltimer(this), rqueue() {

              
  index = id;
  seqno = 2;
  bid = 1;
  malicious = false;
  rcount = 0;
  fcount = 0;

  LIST_INIT(&nbhead);
  LIST_INIT(&bihead);

  logtarget = 0;
  ifqueue = 0;
}
 
 //This code is above the text 
/*
   Broadcast ID Management  Functions
*/
// Approximately at line 233
 void MalTimer::handle(Event*) {
    agent->check_mal();
    Scheduler::instance().schedule(this, &intr, 0.5);
}
void AODV::check_mal() {
    if (index == 0) {
        if (fcount > rcount + 1 ) {
                fprintf(stderr, "No. of packets dropped are %d\n", fcount-rcount);
        }   

    }
}
Note:- When you are adding malicious node module, please replace author code  
// if I am malicious node
 if (malicious == true ) {
    drop(p, DROP_RTR_ROUTE_LOOP);
    // DROP_RTR_ROUTE_LOOP is added for no reason.
 }
to
if (malicious == true) {
         if (ch->ptype_ == PT_CBR) {          
             drop(p, DROP_RTR_ROUTE_LOOP);

             return;//Required if you get pkt flow not specified error.
        // DROP_RTR_ROUTE_LOOP is added for no reason.
         }
     }
 //Add the following blue code just below the above code
if (index == 0) {
        if (ch->ptype_ == PT_CBR) {
            fcount++;
        }
    }
 
4) Open the file ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl and make the  
    changes as shown in blue color.

Node/MobileNode instproc add-target { agent port } {
$self instvar dmux_ imep_ toraDebug_ mac_
......
# Special processing for AODV
set aodvonly [string first "AODV" [$agent info class]]
if {$aodvonly != -1 } {
$agent if-queue [$self set ifq_(0)] ;
# ifq between LL and MAC

$agent install-tap $mac_(0)
......

}


6) Now, make the NS2 by using following commands in ns-allinone-2.34/ns-2.34 directory.

  • make clean
  • make depend
  • make
5) Here is my sample tcl file, aodv.tcl
#======================================================================
# Define options
#======================================================================
 set val(chan)         Channel/WirelessChannel  ;# channel type
 set val(prop)         Propagation/TwoRayGround ;# radio-propagation model
 set val(ant)          Antenna/OmniAntenna      ;# Antenna type
 set val(ll)           LL                       ;# Link layer type
 set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type
 set val(ifqlen)       50                       ;# max packet in ifq
 set val(netif)        Phy/WirelessPhy          ;# network interface type
 set val(mac)          Mac/802_11               ;# MAC type
 set val(nn)           6                        ;# number of mobilenodes
 set val(rp)           AODV                     ;# routing protocol
 set val(x)            800
 set val(y)            800



set ns [new Simulator]
#ns-random 0

set f [open out.tr w]
$ns trace-all $f
set namtrace [open out.nam w]
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
set topo [new Topography]
$topo load_flatgrid 800 800

create-god $val(nn)

set chan_1 [new $val(chan)]
set chan_2 [new $val(chan)]
set chan_3 [new $val(chan)]
set chan_4 [new $val(chan)]
set chan_5 [new $val(chan)]
set chan_6 [new $val(chan)]

# CONFIGURE AND CREATE NODES

$ns node-config  -adhocRouting $val(rp) \
          -llType $val(ll) \
                 -macType $val(mac) \
                 -ifqType $val(ifq) \
                 -ifqLen $val(ifqlen) \
                 -antType $val(ant) \
                 -propType $val(prop) \
                 -phyType $val(netif) \
                 #-channelType $val(chan) \
                 -topoInstance $topo \
                 -agentTrace ON \
                 -routerTrace ON \
                 -macTrace ON \
                 -movementTrace OFF \
                 -channel $chan_1

proc finish {} {
    global ns namtrace
    $ns flush-trace
        close $namtrace  
        exec nam -r 5m out.nam &
    exit 0
}

# define color index
$ns color 0 blue
$ns color 1 red
$ns color 2 chocolate
$ns color 3 red
$ns color 4 brown
$ns color 5 tan
$ns color 6 gold
$ns color 7 black
                       
set n(0) [$ns node]
$ns at 0.0 "$n(0) color blue"
$n(0) color "0"
$n(0) shape "circle"
set n(1) [$ns node]
$ns at 0.0 "$n(1) color red"
$n(1) color "blue"
$n(1) shape "circle"
set n(2) [$ns node]
$n(2) color "tan"
$n(2) shape "circle"
set n(3) [$ns node]
$n(3) color "red"
$n(3) shape "circle"
set n(4) [$ns node]
$n(4) color "tan"
$n(4) shape "circle"
set n(5) [$ns node]
$ns at 0.0 "$n(5) color blue"
$n(5) color "red"
$n(5) shape "circle"


for {set i 0} {$i < $val(nn)} {incr i} {
    $ns initial_node_pos $n($i) 30+i*100
}

$ns at 0.0 "[$n(1) set ragent_] hacker"            
$ns at 0.0 "$n(0) setdest 100.0 100.0 3000.0"
$ns at 0.0 "$n(1) setdest 200.0 200.0 3000.0"
$ns at 0.0 "$n(2) setdest 300.0 200.0 3000.0"
$ns at 0.0 "$n(3) setdest 400.0 300.0 3000.0"
$ns at 0.0 "$n(4) setdest 500.0 300.0 3000.0"
$ns at 0.0 "$n(5) setdest 600.0 400.0 3000.0"

# CONFIGURE AND SET UP A FLOW

set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
set sink3 [new Agent/LossMonitor]
set sink4 [new Agent/LossMonitor]
set sink5 [new Agent/LossMonitor]
$ns attach-agent $n(0) $sink0
$ns attach-agent $n(1) $sink1
$ns attach-agent $n(2) $sink2
$ns attach-agent $n(3) $sink3
$ns attach-agent $n(4) $sink4
$ns attach-agent $n(5) $sink5

#$ns attach-agent $sink2 $sink3
set tcp0 [new Agent/TCP]
$ns attach-agent $n(0) $tcp0
set tcp1 [new Agent/TCP]
$ns attach-agent $n(1) $tcp1
set tcp2 [new Agent/TCP]
$ns attach-agent $n(2) $tcp2
set tcp3 [new Agent/TCP]
$ns attach-agent $n(3) $tcp3
set tcp4 [new Agent/TCP]
$ns attach-agent $n(4) $tcp4
set tcp5 [new Agent/TCP]
$ns attach-agent $n(5) $tcp5


proc attach-CBR-traffic { node sink size interval } {
   #Get an instance of the simulator
   set ns [Simulator instance]
   #Create a CBR  agent and attach it to the node
   set cbr [new Agent/CBR]
   $ns attach-agent $node $cbr
   $cbr set packetSize_ $size
   $cbr set interval_ $interval

   #Attach CBR source to sink;
   $ns connect $cbr $sink
   return $cbr
  }

set cbr0 [attach-CBR-traffic $n(0) $sink5 1000 .030]
$ns at 0.5 "$cbr0 start"
$ns at 5.5 "finish"
puts "Start of simulation.."
$ns run
 

6) In above script, node 0 is source and node 5 is destination. Node 1 is set as
    malicious node.

Tuesday, 3 July 2012

How to color nodes in NS2

You can color the nodes in NS2. It is bit tricky to color them in a wireless network. We can do this by following code.

set node0 [$ns node]
$node0 color "blue"
$ns at 0.0 "$node0 color blue"

 

How to use promiscuous mode in AODV NS2

Introduction:-

          In Promiscuous mode each node in the network listens the packets transmitted by its own neighbor nodes. 
          In NS-2, some time we have to calculate the packet drops or analyze the process. For the same we need to set the network to operate in promiscuous mode. Here, I am going to explain how we can set our wireless network in promiscuous mode with AODV as Routing protocol using NS2 simulator.

1) We need to modify in total 3 files, so it's good to take a backup of it.
    Files are:
    • ns-allinone-2.34/ns-2.34/aodv/aodv.cc
    • ns-allinone-2.34/ns-2.34/aodv/aodv.h
    • ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl
2) Open the file ns-allinone-2.34/ns-2.34/aodv/aodv.h in your favorite editor
    and make the changes as shown in blue color.

  #include <mac.h>
  class AODV: public Tap, public Agent {
  public:
  void tap(const Packet *p);
  ......
  protected:
  Mac *mac_;
  ......
 }
3) Open the file ns-allinone-2.34/ns-2.34/aodv/aodv.cc and make the changes as  
    shown in blue color.
int
AODV::command(int argc, const char* const * argv) {
......
else if(argc == 3) {
......
else if (strcmp(argv[1], "install-tap") == 0) {
mac_ = (Mac*)TclObject::lookup(argv[2]);
if (mac_ == 0) return TCL_ERROR;
mac_->installTap(this);
return TCL_OK;
}
}
return Agent::command(argc, argv);

}
void
AODV::tap(const Packet *p) {
// put your code here
   
4) Open the file ns-allinone-2.34/ns-2.34/tcl/lib/ns-mobilenode.tcl and make the  
    changes as shown in blue color.

Node/MobileNode instproc add-target { agent port } {
$self instvar dmux_ imep_ toraDebug_ mac_
......
# Special processing for AODV
set aodvonly [string first "AODV" [$agent info class]]
if {$aodvonly != -1 } {
$agent if-queue [$self set ifq_(0)] ;
# ifq between LL and MAC

$agent install-tap $mac_(0)
......

}


Explanation:-

         The above procedure will put all the nodes in the wireless network in promiscuous mode. If a node A wants to listen the packets from a node B in promiscuous mode to check whether to check B is forwarding the packets which are sent by A to B or not we can write the code in function tap()


5) To simulate a node as a malicious node in AODV here is a nice blog
    http://elmurod.net/index.php/2009/10/24/adding-malicious-node-in-aodv/

Tuesday, 26 June 2012

How to configure Eclipse for NS-2 (2.34) in Linux.


1) Install the NS2.
2) Make a backup copy of your NS2 folder, i.e. ns-allinone-2.34 somewhere so
    that you can reset the changes by replacing.
3) Download the Eclipse SDK 3.7.2 Indigo (Classic) from here. This file is
     approx. of size 174 MB.
4) Download the CDT from here. This file is approx. of size 44 MB.
5) Copy the "eclipse-SDK-3.7.2-linux-gtk.tar.gz" file to user folder (/home/user)
     and extract.
6) Open the eclipse and set the workspace as /home/user/ns-allinone-2.34.
7) Now in Eclipse go to Help->Install New Software... A window with name
    "install" will pop up. Then go to Add. Put the values as shown below.
    Name:- CDT
    Location:- Browse to the "cdt-master-6.0.2.zip" file.
8) Check "CDT Main Features" and "CDT Optional Features", click next,
    then accept license and finish. At last it will ask you to restart Eclipse, then
    choose Restart.
9) Now choose /home/user/ns-allinone-2.34 as workspace. Click New->C++
    Project. Put Project name:- ns-2.34 (It will warn you that a directory already
    exists. Just ignore it.). Choose Project type as make project->Empty project
    and Toolchains linux GCC and click finish.
10) Now go to Project->clean->clean projects selected below->ns-2.34 and OK.
11) Go to Project->Build All.
12) After build is completed without any error, go to Run->Run configurations.
      Double click C/C++ Application. For Project: browse to ns-2.34. Go to
      Search Project and click ns from Program selection window, then Apply and
      Run. If you get % sign in console, you are done.

13) (Optional) If you want to speed up the make time of NS2, just don't clean it,
       directly build all from Eclipse. It will get compiled in less than 5 seconds.
       All the best!