t cl script explanation

Upload: shanmugha-priya-balasubramanian

Post on 09-Jan-2016

223 views

Category:

Documents


0 download

DESCRIPTION

tcl

TRANSCRIPT

3.2. The script demystified In this section we present a walkthrough of the script which is used to run the simulation for analyzing the performance of routing protocols in MANETs. The script can be used as a skeleton to simulate any kind of routing protocol desired. (a) Set up the simulation and define the constantsThe first step in the simulation is to define the wireless physical medium parameters and initialize the simulation.#----------------------------------------------------------------# Definition of the physical layer#----------------------------------------------------------------set val(chan) Channel/WirelessChannelset val(prop) Propagation/TwoRayGroundset val(netif) Phy/WirelessPhyset val(mac) Mac/802_11set val(ifq) Queue/DropTail/PriQueueset val(ll) LLset val(ant) Antenna/OmniAntenna#-----------------------------------------------------------------# Scenario parameters#------------------------------------------------------------------set val(x) 2000 ;# X dimension of the topographyset val(y) 2000 ;# Y dimension of the topographyset val(ifqlen) 100 ;# max packet in queueset val(seed) 0.0 ;#random seed set val(adhocRouting) [routing protocol]set val(nn) [no. of nodes] ;# how many nodes are simulatedset val(cp) [traffic pattern file] set val(sc) [mobility scenario file] set val(stop) [simulation duration] ;# simulation time

(b) After setting up the initial parameters, we now create the simulator objects#----------------------------------------------------------------------# Set up simulator objects #----------------------------------------------------------------------# create simulator instanceset ns_[new Simulator]# setup topography objectset topo[new Topography]# create trace object for ns and namset tracefd[open output trace file name w]$ns_ use-newtrace ;# use the new wireless trace file formatset namtrace [open nam trace file name w]

$ns_ trace-all $tracefd$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)# define topology$topo load_flatgrid $val(x) $val(y)# Create Godset god_ [create-god $val(nn)]

NOTE: (i) GOD or General Operations Director is a ns-2 simulator object, which is used to store global information about the state of the environment, network, or nodes that an omniscient observer would have, but that should not be made known to any participant in the simulation. (ii) The load_flatgrid object is used to specify a 2-D terrain. Support is available for simulation of 3D terrains for more realistic depiction of scenarios.

(c) Define node properties Now we define the properties of a node in the ad hoc network through the following code snippet-$ns_ node-config -adhocRouting $val(adhocRouting) \ -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

A unicast node in ns-2 has the following structure [ ]

Fig.3. Structure of a unicast node in ns-2 [from the ns manual]By default, a node is specified as a unicast node. If a multicast protocol is desired, a separate clause has to be specified during simulator initialization-set ns [new Simulator -multicast on]

(d) Attach the nodes to the channel and specify their movements# Create the specified number of nodes [$val(nn)] and "attach" them# to the channel. for {set i 0} {$i < $val(nn) } {incr i} {set node_($i) [$ns_ node]$node_($i) random-motion 0;# disable random motion}# Define node movement modelputs "Loading connection pattern..."source $val(cp)# Define traffic modelputs "Loading scenario file..."source $val(sc)# Define node initial position in namfor {set i 0} {$i < $val(nn)} {incr i} {# 50 defines the node size in nam, must adjust it according to your scenario # The function must be called after mobility model is defined # puts "Processing node $i" $ns_ initial_node_pos $node_($i) 50}NOTE: The above code attaches the nodes to the channel and specifies the movement of the nodes and the traffic flow between them. The default random motion of the nodes must be disabled.(e) Finish up and run the simulation

## Tell nodes when the simulation ends#for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at $val(stop).0 "$node_($i) reset";}

$ns_ at $val(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"

# dump the initial simulation info to the trace file

puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(adhocRouting)"puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)"puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"

puts "Starting Simulation..."$ns_ run

Back Notes:1) Pt_ is the antenna power, and it is used to set the distance,for e.g.

Phy/WirelessPhy set Pt_ 7.214e-3

sets the antenna distance exactly at 100 m in FreeSpace and TwoRayGround Propagation model. You can also set the distance when you set RXThresh_(also can be done from tcl level), but you don't have to use it. (Pt_ and RXTresh_ are defined in wireless-phy.cc file) Pt_ is defined in wireless-phy.cc

2) rxPower and txPower are used in Energy Model You can set the initial battery capacity, and rxPower is the power used to receive a packet, and txPower i one used to transmit a packet.3) $ns_ at 0.0 "$node_(0) NodeLabel PAN Coor"

4) simple.tclset sim [new Simulator]$sim at 1 puts \Hello World!\$sim at 1.5 exit$sim runarches 74% ns simple.tcl Hello World!arches 75%

5) Discrete Event Simulation

6)

Where can you write Scripts?; Top

After login into terminal, follow step given below

1. vi filename.tcl It will open page, there you can write tcl scripts.

2. save file Press esc-> colon (shift + semicolon) ->wq (save and quit)It save the file

3. To run tcl script ns filename.tcl

Basically NS 2 programming contains the following steps. Top

1.Create the event scheduler

2.Turn on tracing

3.Creating network

a) Computing setup routing - rtproto b) Creating transport connection-Agents c) Creating traffic-Applications

4. Monitoring

a) Visualization using nam

Note: every tcl script must write in small letters only except protocol agents i.e. TCP, FTP

Template Top

Every ns2 script starts with creating simulator object set ns [new Simulator]

How to create node set n0 [$ns node] set n1 [$ns node]

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

This line tells the simulator object to connect the nodes n0 and n1 with a duplex link with the bandwidth.1Megabit, a delay of 10ms and a DropTail queue.

How to use Trace?

We use simulator to see results. How is it achieved? Using trace

Two types of trace

1. generic trace for use with xgraph, and other things

2. nam trace for use with visualization

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

#Open the nam trace file set nf [open out.nam w] $ns namtrace-all $nf

Since we have started tracing, we should end also. For this we use finish procedure.

#Define a 'finish' procedure proc finish {} { global ns tracefile nf $ns flush-trace close $nf close $tracefile # close tracefile exec nam out.nam & #Execute nam on the trace file exit 0 }

Finish procedure is forced to be called at the end with the line

$ns at 5.0 finish

Every tcl script must contain following statement

$ns run

Follow the template tcl like Bible.

All communication between 2 agents.

Upper layer to lower layer, we attach agents.

Same layer in two nodes, we connect agents.

Agents are tcp, udp, telnet, cbretc

UDP communication In UDP communication, data is flows from UDP agent to Null agent. #Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0

# create a null agent which act as traffic sink and attach it to node n1 set null0 [new Agent/Null] $ns attach-agent $n1 $null0

# connect two agents with each other $ns connect $udp0 $null0

TCP Communication

In TCP communication, data is flows from TCP agent to TCPsink agent.

# create Tcp agent and attach it to node no set tcp0 [new Agent/TCP] $ns attach-agent $n0 $tcp0

# create a tcpsink agent which act as traffic sink and attach it to node n1 set tcpsink0 [new Agent/TCPSink] $ns attach-agent $n1 $tcpsink0

# connect two agents with each other $ns connect $tcp0 $tcpsink0

Traffic generator

For actual data to flow, we need traffic generators.They simulate some application traffic. Simple example using CBR

# creating CBR agent set cbr0 [new Application/Traffic/CBR]

# Attach the CBR agent to some udp/tcp agent $cbr0 attach-agent $udp0

Scheduling the events

Here at place major role.

$ns at 1.0 $cbr0 start $ns at 5.0 finish"

cbr0 will start at a time of 1.0 ms and whole process will stops at 5.0ms.we can also stop each and traffic generator. for example

$ns at 4.0 $cbr0 stop

Traffic generator cbr0 will stops at 4.0

Simple Examples Top

So far we are not talking about data flow. Here we will explain with CBR protocol.

Example1:( CBR over UDP) set ns [new Simulator] Creatingns simulator object

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

set nf [open out.nam w]$ns namtrace-all $nf Open the nam trace file

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

set n0 [$ns node]set n1 [$ns node]$ns simplex-link $n0 $n1 1Mb 10ms DropTail Create your topology- set n0 nodes.- $ns duplex-links

set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr[new Application/Traffic/CBR]$cbr attach-agent $udp0set null0 [new Agent/Null]$ns attach-agent $n1 $null0$ns connect $udp0 $null0 Create your agents-transport layer and application layers stuff

$ns at 1.0 "$cbr start"$ns at 3.0 "finish" Scheduling Events - $ns at 1.0 start and at 3.0 finish

$ns run starts the simulation.

Result

Before 1.0ms

After 1.0ms

Example 2:(CBR over TCP) Top set ns [new Simulator] Creatingns simulator object

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

set nf [open out.nam w]$ns namtrace-all $nf Open the nam trace file

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

set n0 [$ns node]set n1 [$ns node]$ns simplex-link $n0 $n1 1Mb 10ms DropTail Create your topology- set n0 nodes.- $ns duplex-links

set tcp0 [new Agent/TCP]$ns attach-agent $n0 $tcp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $tcp0set tcpsink0 [new Agent/TCPSink]$ns attach-agent $n1 $tcpsink0$ns connect $tcp0 $tcpsink0 Create your agents-transport layer and application layers stuff

$ns at 1.0 "$cbr start"$ns at 3.0 "finish" Scheduling Events - $ns at 1.0 start and at 3.0 finish

$ns run starts the simulation.

Result:

Before 1.0ms

After 1.0ms

Example3:(FTP over TCP) Topset ns [new Simulator] Creatingns simulator object

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

set nf [open out.nam w]$ns namtrace-all $nf Open the nam trace file

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

set n0 [$ns node]set n1 [$ns node]$ns simplex-link $n0 $n1 1Mb 10ms DropTail Create your topology- set n0 nodes.- $ns duplex-links

set tcp0 [new Agent/TCP]$ns attach-agent $n0 $tcp0set ftp0 [new Application/FTP]$ftp0 attach-agent $tcp0set tcpsink0 [new Agent/TCPSink]$ns attach-agent $n1 $tcpsink0$ns connect $tcp0 $tcpsink0 Create your agents-transport layer and application layers stuff

$ns at 1.0 "$ftp0 start"$ns at 3.0 "finish" Scheduling Events - $ns at 1.0 start and at 3.0 finish

$ns run starts the simulation.

Result:

Here we are using FTP Application agent as a traffic generator instead of CBR. The difference is CBR traffic generator will produce constant bit rate where as FTP traffic generator produces maximum available bit rate.

We are writing code

set ftp0 [new Application/FTP]$ftp0 attach-agent $tcp0

and Similarly for telnet also

set telneto [new Application/TELNET]$ftp0 attach-agent $telnet0

Instead of CBR

set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $tcp0

Before 1.0ms

After 1.0ms

# Define optionsset val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagation modelset val(netif) Phy/WirelessPhy ;# network interface typeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface queue typeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 50 ;# max packet in ifqset val(nn) 50 ;# number of mobilenodesset val(rp) DSDV ;# routing protocolset val(x) 1000 ;# X dimension of topographyset val(y) 1000 ;# Y dimension of topography set val(stop) 150 ;# time of simulation end

set ns [new Simulator]set tracefd [open simple.tr w]set namtrace [open simwrls.nam w]

$ns trace-all $tracefd$ns namtrace-all-wireless $namtrace $val(x) $val(y)

# set up topography objectset topo [new Topography]

$topo load_flatgrid $val(x) $val(y)

create-god $val(nn)

# configure the 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 OFF \-movementTrace ON

for {set i 0} {$i < $val(nn) } { incr i } {set n($i) [$ns node] }

# Provide initial location of mobilenodes$n(0) set X_ 347.0$n(0) set Y_ 3.0$n(0) set Z_ 0.0

$n(1) set X_ 345.0$n(1) set Y_ 36.0$n(1) set Z_ 0.0

$n(2) set X_ 330.0$n(2) set Y_ 121.0$n(2) set Z_ 0.0

$n(3) set X_ 316.0$n(3) set Y_ 152.0$n(3) set Z_ 0.0

$n(4) set X_ 246.0$n(4) set Y_ 90.0$n(4) set Z_ 0.0

$n(5) set X_ 379.0$n(5) set Y_ 6.0$n(5) set Z_ 0.0

# Set a TCP connection between n(1) and n(31)set tcp [new Agent/TCP/Newreno]$tcp set class_ 2set sink [new Agent/TCPSink]$ns attach-agent $n(1) $tcp$ns attach-agent $n(31) $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 10.0 "$ftp start"

# Set a TCP connection between n(31) and n(43)set tcp [new Agent/TCP/Newreno]$tcp set class_ 2set sink [new Agent/TCPSink]$ns attach-agent $n(31) $tcp$ns attach-agent $n(43) $sink$ns connect $tcp $sink

#defining heads$ns at 0.0 "$n(0) label CH"$ns at 0.0 "$n(1) label Source"#$ns at 0.0 "$n(2) label N2"

$ns at 10.0 "$n(5) setdest 785.0 228.0 5.0" $ns at 13.0 "$n(26) setdest 700.0 20.0 5.0" $ns at 15.0 "$n(14) setdest 115.0 85.0 5.0"

#Color change while moving from one group to another$ns at 73.0 "$n(2) delete-mark N2"$ns at 73.0 "$n(2) add-mark N2 pink circle"$ns at 124.0 "$n(11) delete-mark N11"$ns at 124.0 "$n(11) add-mark N11 purple circle"$ns at 103.0 "$n(5) delete-mark N5"$ns at 103.0 "$n(5) add-mark N5 white circle"$ns at 87.0 "$n(26) delete-mark N26"$ns at 87.0 "$n(26) add-mark N26 yellow circle"$ns at 92.0 "$n(14) delete-mark N14"$ns at 92.0 "$n(14) add-mark N14 green circle"

# Define node initial position in namfor {set i 0} {$i < $val(nn)} { incr i } {# 20 defines the node size for nam$ns initial_node_pos $n($i) 20}

# Telling nodes when the simulation endsfor {set i 0} {$i < $val(nn) } { incr i } {$ns at $val(stop) "$n($i) reset";}

# ending nam and the simulation $ns at $val(stop) "$ns nam-end-wireless $val(stop)"$ns at $val(stop) "stop"$ns at 150.01 "puts \"end simulation\" ; $ns halt"proc stop {} {global ns tracefd namtrace$ns flush-traceclose $tracefdclose $namtraceexec nam simwrls.nam &}

$ns run

Tcl example] node0 ------- node1set val(chan) Channel/WirelessChannel ;# channel typeset val(prop) Propagation/TwoRayGround ;# radio-propagation modelset val(netif) Phy/WirelessPhy ;# network interface typeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface queue typeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 32768 ;# max packet in ifq

set val(rp) DumbAgent

set ns [new Simulator]Antenna/OmniAntenna set X_ 0Antenna/OmniAntenna set Y_ 0Antenna/OmniAntenna set Z_ 1.5 Antenna/OmniAntenna set Gt_ 1.0Antenna/OmniAntenna set Gr_ 1.0

Phy/WirelessPhy set CPThresh_ 10.0Phy/WirelessPhy set CSThresh_ 1.559e-11Phy/WirelessPhy set RXThresh_ 3.652e-10Phy/WirelessPhy set bandwidth_ 2e6Phy/WirelessPhy set Pt_ 0.28183815Phy/WirelessPhy set freq_ 914e+6Phy/WirelessPhy set L_ 1.0

set f [open test.tr w]$ns trace-all $f$ns eventtrace-allset nf [open test.nam w]$ns namtrace-all-wireless $nf 500 500

# set up topography objectset topo [new Topography]

$topo load_flatgrid 500 500

# Create Godcreate-god 2

# create channel set chan [new $val(chan)]

$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) \-channel $chan \ -topoInstance $topo \ -agentTrace ON \ -routerTrace OFF \ -macTrace ON \ -movementTrace OFF

for {set i 0} {$i < 2} {incr i} {set node_($i) [$ns node]$node_($i) random-motion 0}

$node_(0) set X_ 30.0$node_(0) set Y_ 30.0$node_(0) set Z_ 0.0

$node_(1) set X_ 280.0$node_(1) set Y_ 30.0$node_(1) set Z_ 0.0

set udp [new Agent/mUDP]#set the sender trace file name to sd$udp set_filename sd$ns attach-agent $node_(0) $udp

set null [new Agent/mUdpSink]#set the receiver filename to rd$null set_filename rd$ns attach-agent $node_(1) $null$ns connect $udp $null

set cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 1Mb$cbr set random_ false

$ns at 0.0 "$cbr start"$ns at 15.0 "$cbr stop"

for {set i 0} {$i < 2} {incr i} {$ns initial_node_pos $node_($i) 30$ns at 20.0 "$node_($i) reset";}

$ns at 20.0 "finish"$ns at 20.1 "puts \"NS EXITING...\"; $ns halt"

#INSERT ANNOTATIONS HEREproc finish {} { global ns f nf val $ns flush-trace close $f close $nf

}puts "Starting Simulation..."$ns run

[compile threshold.cc]$ cd ns/indep-utils/propagation/$ g++ -lm threshold.cc -o threshold[example]$ threshold -m TwoRayGround 250distance = 250propagation model: TwoRayGround

Selected parameters:transmit power: 0.281838frequency: 9.14e+08transmit antenna gain: 1receive antenna gain: 1system loss: 1transmit antenna height: 1.5receive antenna height: 1.5

Receiving threshold RXThresh_ is: 3.65262e-10$

p.s. (0.281838151.521.52) / (2504)=3.652e-10the more information can be found at http://140.116.164.80/~smallko/ns2/11b.htm[usage]$ thresholdUSAGE: find receiving threshold for certain communication range (distance)SYNOPSIS: threshold -m [other-options] distance

: FreeSpace, TwoRayGround or Shadowing[other-options]: set parameters other than default values:

Common parameters:-Pt -fr -Gt -Gr -L

For two-ray ground model:-ht -hr

For shadowing model:-pl -std -d0 -r How to generate nodes position, the initial network topology?I used this script: initial_topology.tcl The initial_topology.tcl script requires four parameters to be input."1.number of node >0" "2. X coordinate" "3. Y coordinate" "4. output file name"Ex: # ns initial_topology.tcl 100 1900 1900 topology_100_1900this example will generate positions and transmission range for 100 nodes in 1900x1900m area in the file topology_100_1900.$node_(0) set X_ 1267.6484042162301$node_(0) set Y_ 47.771891694409724 $node_(0) set Z_ 0.0$node_(0) radius 214.2403287413718 $node_(1) set X_ 804.46613477750975$node_(1) set Y_ 1733.0320414775199$node_(1) set Z_ 0.0$node_(1) radius 204.81749854740571