Tuesday, 18 December 2012

NS2 simulation using Link State routing protocol

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set tr [open out.tr w]
$ns trace-all $tr

proc finish {} {
        global nf ns tr
        $ns flush-trace
        close $tr
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
$ns duplex-link $n2 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n1 $n3 orient right
$ns duplex-link-op $n2 $n1 orient right-up

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink

set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $n3 $null

$ns connect $tcp $sink
$ns connect $udp $null

$ns rtmodel-at 1.0 down $n1 $n3
$ns rtmodel-at 2.0 up $n1 $n3

$ns rtproto LS

$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

$ns run

NS2 simulation using Distance Vector routing protocol

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set tr [open out.tr w]
$ns trace-all $tr

proc finish {} {
        global nf ns tr
        $ns flush-trace
        close $tr
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n3 10Mb 10ms DropTail
$ns duplex-link $n2 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n1 $n3 orient right
$ns duplex-link-op $n2 $n1 orient right-up

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink

set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $n3 $null

$ns connect $tcp $sink
$ns connect $udp $null

$ns rtmodel-at 1.0 down $n1 $n3
$ns rtmodel-at 2.0 up $n1 $n3

$ns rtproto DV

$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

$ns run

NS2 simulation with TCP and UDP packets

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set nt [open out.tr w]
$ns trace-all $nt

proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
        }

set h1 [$ns node]
set r1 [$ns node]
set h2 [$ns node]
set h3 [$ns node]
set r2 [$ns node]
set h4 [$ns node]

$ns duplex-link $h1 $r1 10Mb 20ms DropTail
$ns duplex-link $h2 $r1 10Mb 20ms DropTail
$ns duplex-link $r1 $r2 1.5Mb 20ms DropTail
$ns duplex-link $r2 $h3 10Mb 20ms DropTail
$ns duplex-link $r2 $h4 5Mb 20ms DropTail

$ns duplex-link-op $h1 $r1 orient right-down
$ns duplex-link-op $h2 $r1 orient right-up
$ns duplex-link-op $r1 $r2 orient right
$ns duplex-link-op $r2 $h3 orient right-up
$ns duplex-link-op $r2 $h4 orient right-down

set udp [new Agent/UDP]
$ns attach-agent $h2 $udp

set cbr [new Application/Traffic/CBR]
#$cbr set interval_ 0.0005
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $h4 $null

set tcp [new Agent/TCP]
$ns attach-agent $h1 $tcp

set ftp [new Application/FTP]
#$ftp set interval_ 0.0005
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $h3 $sink

$ns connect $udp $null
$ns connect $tcp $sink

$ns at 0.0 "$cbr start"
$ns at 0.0 "$ftp start"
$ns at 10.0 "finish"

$ns run

NS2 simulation for UDP packets in a network

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set nt [open out.tr w]
$ns trace-all $nt

proc finish {} {
        global ns nf nt
        $ns flush-trace
        close $nf
        close $nt
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]

$ns duplex-link $n0 $n1 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-up
$ns duplex-link-op $n0 $n2 orient right

set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0

set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]
$ns attach-agent $n2 $null0

$ns connect $udp0 $null0

$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

$ns run

NS2 simulation for TCP packets in a network

set ns [new Simulator]

set nf [open out.nam w]
$ns namtrace-all $nf

set nt [open out.tr w]
$ns trace-all $nt

proc finish {} {
        global ns nf
        $ns flush-trace
        close $nf
        exec nam out.nam &
        exit 0
        }

set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns duplex-link $n0 $n1 10Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n1 10Mb 10ms DropTail

$ns duplex-link-op $n0 $n1 orient right-down
$ns duplex-link-op $n1 $n2 orient right
$ns duplex-link-op $n3 $n1 orient right-up

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

set ftp [new Application/FTP]
#$ftp set packet_size_ 4.5Mb
$ftp set interval_ 0.05
$ftp attach-agent $tcp

set sink [new Agent/TCPSink]
$ns attach-agent $n2 $sink

$ns connect $tcp $sink

$ns at 0.3 "$ftp start"
$ns at 3.0 "finish"

$ns run

Simple Script Explanation

To start we have to set a few variables like simulator object, trace file and object, nam file and object.

set ns [new Simulator]

This would set the simulator with the simulator object which is to be accessed in the script.

set nf [open out.nam w]
$ns namtrace-all $nf

This would set the nam file (network animation file) with the object and connect to ns.

set tr [open out.tr w]
$ns trace-all $tr

This would set the trace file and would connect to the simulator. The trace file is required to analyze the various packets which are send, received type of application used etc.


set n0 [$ns node]

Now the nodes could be set as many as you want, for loop could be used if many nodes are to be made.

$ns duplex-link $n0 $n1 10Mb 10ms DropTail

The connection for the various nodes with each other with the band width and rate.

$ns duplex-link-op $n0 $n1 orient right-up

The nodes could be given with various orientations with this option. right, right-up and right down could be used depending on the node.

For the application like tcp or udp to run, we need to set two agents and the application which should run in between.

When using tcp, we have ftp as the application and tcpsink as the end agent. connection must be made between tcp and tcpsink, same in udp with cbr and null respectively.

set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp

This would make a tcp agent and connect it with the node.

set ftp [new Application/FTP]
$ftp attach-agent $tcp

Now the ftp is connected with the tcp

set agent [new Agent/TCPSink]
$ns attach-agent $n3 $sink

Now the tcpsink is set to a node where the tcp packets are received.

The tcp and sink (agents) needs to be connected, such that the network flows.

$ns connect $tcp $sink
Same for udp

set udp [new Agent/UDP]
$ns attach-agent $n2 $udp

set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp

set null [new Agent/Null]
$ns attach-agent $n3 $null

$ns connect $udp $null

We can use the routing protocols in the simulator using rtmodel (to break the link), rtproto (to use the protocol)

$ns rtmodel-at 1.0 down $n1 $n2
$ns rtmodel-at 2.0 up $n1 $n3

For distance vector we could use

$ns rtproto DV

For linkstate we could use

$ns rtproto LS

When all this is done the tcp could be started at some point and could call the finish procedure to end. 

The out.tr file is used to trace the packets. A normal awk command could be used to analyse the packets.

$ns at 0.0 "$ftp start"
$ns at 0.0 "$cbr start"

$ns at 5.0 "finish"

We could also stop the tcp or udp in between using stop instead of start, hence nam out.nam need to be used if finish is not used.

run is used to run the whole simulation.

$ns run

The file should be saved in .tcl format and should use ns filename.tcl to run