You are on page 1of 17

NS2

Ing. Ardian Ulvan MSc.

ns2 Components
ns-2.29 tcl8.4.11 tcl doc ex test examples test cases core code

tk8.4.11
tclcl-1.17 otcl-1.11

ns-tutorial

lib

models

}
OTcl

C++

nam-1.11

Components
ns-2, the simulator itself
Specify simulation, generate traces Depends on Tcl/Tk, OTcl, TclCL

nam, the network animator


Animate traces from simulation GUI for constructing simple simulations

Pre-processing
Traffic, topology generation

Post-processing
Analyse trace output with perl, awk, TCL, Matlab, etc

Using ns2

Define problem

Simulation model

Extend simulator Post-process results Execute simulation

Using ns2
Create simulation
Describe network, protocols, sources, sinks Interface via OTcl which controls C++

Execute simulation
Simulator maintains event list (packet list), executes next event (packet), repeats until done Events happen instantly in virtual time but could take arbitrarily long real time Single thread of control, no locking, races, etc

Post-process results
Scripts (awk, perl, python) to process text output No standard library but some available on web

Users Perspective From the users perspective, NS2 is an OTcl interpreter that takes an OTcl script as input and produces a trace file as output.

Languages
C++ for data (creation of objects)
Per-packet processing, the core of ns Fast to run, detailed, complete control
speed and efficiency

OTcl for control (setup the simulator)


Simulation description Periodic or triggered actions Manipulating existing C++ objects Faster to write and change

ease of use

Why two languages? (Tcl & C++) C++: Detailed protocol simulations require systems programming language
byte manipulation, packet processing, algorithm implementation Run time speed is important Turn around time (run simulation, find bug, fix bug, recompile, re-run) is slower

Tcl: Simulation of slightly varying parameters or configurations


quickly exploring a number of scenarios iteration time (change the model and re-run) is more important

Split Object Model


Split Object Model with shared class hierarchy
Control (OTcl) vs. Data (C++)

TclCL provides glue library to easily share functions, variables, etc


OTcl into C++: command(), tcl.result() C++ into OTcl: tcl.eval()

Not all classes are mirrored exactly


OTcl OTcl script describing simulation C++

TclCL

ns2 Environment
Simulation Scenario
1 2

set ns_ [new Simulator]

Tcl Script

set node_(0) [$ns_ node] set node_(1) [$ns_ node]


class MobileNode : public Node { friend class PositionHandler; public: MobileNode(); }

C++ Implementation

Installation

Unix variants ( were using cygwin* as a unix like environment in windows platform)
Download NS-allinone-2.29 package Contains
TCL/TK 8.4.5 oTCL 1.8 Tclcl 1.15 Ns2 Nam -1

* In this class we do not carry out the cygwin installation

Installation After successful downloading and unzipping install allinone package , install ns2 by running
./install in ~/ns-allinone-2.29/

After successful installation , Validate the scripts by running


./validate in ~/ns-allinone-2.29/ns-2.29/

Its now all set to work with ns2

Creating a simple simulation with ns2

Code for simple script


#Creating a Simulator Object set ns [new Simulator]
#Open the name trace file set nf [open out.nam w] $ns nametrace_all $nf

#Closing trace file and starting NAM proc finish { } { global ns nf $ns flush-trace close $nf #close the trace file exec nam out.nam & #execute nam on trace file exit 0

}
#Call the finish procedure after 5 seconds simulation time $ns at 5.0 "finish #Run the simulation $ns run

#Creating a Simulator Object set ns [new Simulator] #Open the name trace file set nf [open out.nam w] $ns nametrace_all $nf

Code for create simple link and node

#Closing trace file and starting NAM proc finish { } { global ns nf $ns flush-trace close $nf #close the trace file exec nam out.nam & #execute nam on trace file exit 0

}
#Creating two nodes set n0 [$ns node] set n1 [$ns node]
#Create a duplex link between the nodes $ns duplex-link $n0 $n1 1Mb 10ms DropTail #Call the finish procedure after 5 seconds simulation time $ns at 5.0 "finish #Run the simulation $ns run

. . .
#Creating two nodes set n0 [$ns node] set n1 [$ns node]

Code for simple agent and events

#Create a duplex link between the nodes $ns duplex-link $n0 $n1 1Mb 10ms DropTail #Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 #Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 #Create a Null agent (a traffic sink) and attach it to node n1 set null0 [new Agent/Null] $ns attach-agent $n1 $null0 #Connect the traffic source with the traffic sink $ns connect $udp0 $null0 #Schedule events for the CBR agent $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop #Call the finish procedure after 5 seconds simulation time $ns at 5.0 "finish #Run the simulation $ns run

Result for simple script

You might also like