Professional Documents
Culture Documents
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
Pre-processing
Traffic, topology generation
Post-processing
Analyse trace output with perl, awk, TCL, Matlab, etc
Using ns2
Define problem
Simulation model
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
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
TclCL
ns2 Environment
Simulation Scenario
1 2
Tcl Script
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
Installation After successful downloading and unzipping install allinone package , install ns2 by running
./install in ~/ns-allinone-2.29/
#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
#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]
#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