Introduction to NS-2
Xiaohua Tian
Illinois Institute of Technology
What is NS-2 ?
NS is a event driven network simulator that simulates variety of
IP networks
NS implements
Network Protocols : TCP, UDP
Traffic behavior : FTP, Telnet, CBR, VBR
Router Queue Management : Drop Tail, RED, CBQ
Routing algorithms : Dijkstra
Adhoc routing, Sensor Networks
Multicasting , MAC layer protocols
Wired, Wireless, Satellite
NS-2 Software Structure
NS simulator is based on 2 languages
C++
Used to implement the protocols
For example implementation of Mac 802.11 in ~ns/ns2.30/mac/802_11.[cc,h]
Advantage
Reduce packet and event processing time
Otcl
Used to configure the topology, the nodes, the channel,
to schedule the events
Advantage
Easy to change model and re-run
Flexible for integration
C++ and Otcl Duality
C++ and Otcl Duality
Architectural View of NS
General Simulation Process
NS users
Add or modify
C++ classes
Recompile
NS
Write Otcl
scripts
Run NS
Modify Otcl
scripts
Modify C++
codes
Analyze
simulation
results
Final
results
Installation (windows environment)
1. Install cygwin:
www.cygwin.com
Cygwin installation
Cygwin installation
Cygwin installation
Cygwin installation
Cygwin installation
Cygwin installation
Cygwin installation
2. NS-2.30 installation
copy the ns-allinone-2.30.tar.gz (from Blackboard) file in your
Cygwin home directory (typically something like
C:\cygwin\home\John)
Launch Cygwin, a command line window should appear.
Type the following commands:
gzip -d -c ns-allinone-2.30.tar.gz | tar xvf –
(or use winrar to unpack the package)
cd ns-allinone-2.30
./install
2. Environment variable config
Environment variable config
Environment variable config
Environment variable config
Path: C:\cygwin\home\username\nsallinone-2.30\bin;
TCL_LIBRARY: C:\cygwin\home\userna
me\ns-allinone-2.30\tcl8.4.13
LD_LIBRARY_PATH: C:\cygwin\home\u
sername\ns-allinone-2.30\otcl-1.12
3.
Steps to set up the simulation
Initialize the simulator
Define files for output (tracing)
Set up the topology
Set up the “agents”
Set up the traffic between the nodes
Start the simulation
Analyze the trace files to compute the
parameters of interest
Setting Up Variables
Initialize the Simulator
set ns_ [new Simulator]
Setup Trace File
set tracefile [open simple.tr w]
$ns_ trace-all $tracefile
Setup NAM trace file
set namfile [open out.nam w]
$ns_ namtrace-all $namfile
Set up the topology
Create 2 nodes n0 and n1
set n0 [$ns_ node]
Set n1 [$ns_ node]
Create Duplex-Link between n0 and n1
10ms Propagation Delay
$ns_ duplex-link $n0 $n1 2Mb 10ms DropTail
2Mb of Capacity
Bi-directional link
n0
n1
Setup traffic flow
Setup a TCP connection
Set tcp [new Agent/TCP]
$ns_ attach-agent $n0 $tcp
Set sink [new Agent/TCPSink]
$ns_ attach-agent $n1 $sink
$ns_ connect $tcp $sink
Setup a FTP over TCP
Set ftp [new Application/FTP]
$ftp attach-agent $tcp
FTP
TCP
n0
Sink
n1
Set Stop Time and Start Simulation
$ns_ at 0.1 “$ftp start”
$ns_ at 10.0 “$ftp stop”
$ns_ at 10.1 “finish”
$ns run // Starting simulation
// Define a Finish procedure
Proc finish {} {
global ns_ tracefile namfile
$ns_ flush-trace
close $tracefile
close $namfile
exec nam out.nam & // Calling NAM
exit 0
}
Running the Script and Using NAM
ns filename.tcl
The simulation will generate the trace file simple.tr
NAM can be run in 2 ways
Inside TCL file “ exec nam out.nam &”
Outside TCL file “ nam out.nam”
NAM Screenshot
Trace Files
Working with Trace Files
To compute Throughput, Jitter, End to End Delay
Process trace files with any of these utilities
1) AWK
2) GREP
3) PERL
To plot the graph
1) gnuplot
2) xgraph
In Tcl : exec xgraph out.tr &
Out Tcl : xgraph out.tr
Screenshot of XGRAPH
Conclusion
References
http://www.cygwin.com/
http://www.isi.edu/nsnam/ns/nsdocumentation.html
Contains the NS manual detailed
documentation of the source code
http://nile.wpi.edu/NS/