You are on page 1of 55

Introduction to NS-2

Presented by
Sowmya.G & Sakila.K
Sr.Lecturers,ECE Department
The Oxford College Of Engineering,
Bangalore.
Outline
 Introduction to Simulation
 Advantages of simulation
 Network Simulators
 NS – 2 evolution and goals
 Discrete event simulation
 NS-2 Architecture
 C++ and OTCL
 Hierarchy
 Duality
 Naming conventions
 NS-2 components, Capabilities and Features
 Useful links
 Installation
 Generic script structure
 Simple topology
 Summary
Introduction to Simulation
 How to study a system?
• Measurements on an existing system
- What to do, if system does not exist really?
- What to do, if changes are very expensive or time consuming?
• Mathematical analysis
- Good solutions, but only feasible for simple systems.
- Real world systems are too complex, e.g., factory, computer,
network, etc.
• Modeling and Evaluation of Communication Systems
• Simulation
- Build the behavior of a system within a program
• Emulation, Emulator
- Not only the behavior is imitated, but the system itself is somehow
reproduced (prototype design)
Advantages of simulation
• Simulation enables the study of experiments with internal
interactions.
• To check the system’s behavior, the simulation can be used to see
Informational, organizational, and environmental changes.
• Observing results from simulation can give insight to which variables
are the most important ones.
• Simulation can be used to devise reinforcement learning mechanism.
• Simulations can be used to verify analytical results, e.g. queueing
systems.
• Animation of a simulation can show the system in action, so that
the plan can be visualized.
Advantages of simulation
• Policies, procedures, decision rules, information flows can be
explored without disrupting the real system.
• New hardware designs, physical layouts, transportation systems can
be tested without committing resources.
• Time can be compressed or expanded.
- Slow-down or Speed-up.
• Bottleneck analysis can be performed to detect excessive delays.
Network simulators
 OPNET (http://www.opnet.com)
• Leading Commercial Software
• Support Windows and Unix
• Graphical Interface

• GloMoSim (http://pcl.cs.ucla.edu/projects/glomosim)
• Simulation environment for wireless network
• Scalable to support thousands of nodes
• Free for educational users

• More Resources
http://www.icir.org/models/simulators.html
NS-2 Simulator evolution

1989: REAL network simulator-University of


California and Cornell University
1995: DARPA (Defense Advanced Research Project
Agency) , VINT (Virtual Internetwork Testbed),
project at LBL, Xerox PARC, UCB, and USC/ISI
 Present: DARPA SAMAN(Simulation Augmented

by Measurement and Analysis for Networks)


 NSF CONSER(Collaborative Simulation for

Education and Research) project


Collaboration with other
NS-2 Goals

Support networking research and education


Protocol design, Traffic studies/analysis, etc.
Protocol comparison
Provide a collaborative Environment
Freely distributed, open source
share code, protocols, models, etc.
Allow easy comparison of protocols
Increase confidence in results
Ns-2 Simulator

• Discrete Event Simulator.


• Simulator models world as events.
• Single thread of control.
• Modeling network protocols.
• Wired, wireless, satellite.
• Ad-hoc, sensor networks.
• Provides comparison of similar protocols.
Discrete Event Simulation
 Model world as events
 Simulator has list of events

 Process: take next one, run it, until done

 Each event

 happens in an instant of virtual (simulated) time,


 but takes an arbitrary amount of real time
 NS uses simple model: single thread of control
 no locking or race conditions
Discrete Event Example

Two nodes
on an Ethernet: simple t=1, A enqueues pkt on LAN
queuing t=1.01, LAN dequeues pkt
model: and triggers B

A B t=1.0 : A sends pkt to NIC, A’s NIC


starts carrier sense
detailed t=1.005 : A’s NIC concludes cs, starts tx
CSMA/CD t=1.006 : B’s NIC begins receiving pkt
model: t=1.01 : B’s NIC concludes pkt
B’s NIC passes pkt to app
NS Architecture: C++ and OTCL
 Uses two languages
 C++ for data
 per packet processing - core of ns
 fast to run, detailed, complete control
 OTCL for control
 Simulation scenario/topology configurations
 Periodic or triggered actions
 Fast to write and change

+ running vs. writing speed


- Learning vs. debugging
Why two language? (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.
Class TclObject: Hierarchy

SplitObject OTcl class C++ TclObject()


hierarchy class
hierarchy
Connector
Agent
Agent()

Agent/TCP TCPAgent()

Agent/TCP OTcl Agent/TCP


shadow object C++ object

14
14
OTcl and C++: The Duality
NS-2 Naming conventions
NS-2 implementation
Users Perspective

• Object-oriented, discrete event-driven network simulator.


• Written in C++ and Otcl.
NS2 Components

ns-allinone

Tcl8.0 TK8.0 OTcl tclcl ns-2 nam-1

tcl ... C++ code

ex test lib mcast ...

examples validation tests


OTcl code
NS 2 Components
 NS all-in-one package (e.g., ns-allinone 2.30) contains
 Main components
 NS release 2.30
 Tcl/Tk release 8.4.13
 Otcl release 1.12
 TclCL release 1.18
 Optional components
 NAM release 1.12
 Zlib version 1.2.3
 Xgraph version 12.1
 NS allinone package will be simple to install but it has more
downloading time and needs more memory to run
NS-2 Capabilities
 NS2 can
– Setup network topologies
– Generate packet traffic similar to Internet and measure various
parameters
• NS is needed
– To verify utility / feasibility of new algorithms / architectures.
– Actual topologies Expensive / Error prone / Time consuming to setup
NS – 2 Features

• Scheduling, routing and congestion control


• Wired networks: P2P links, LAN
• Wireless networks: terrestrial (ad-hoc, cellular; GPRS,
WLAN, Bluetooth), satellite
• It creates trace files
Platforms

• Most UNIX systems ( Linux, Solaris)


• Window 9x/NT/2000/----
NS2: Useful links
• Most of the NS2 source code is in C++
– http://www.cplusplus.com/doc/tutorial/
– http://www.isi.edu/nsnam/ns
– http://www.isi.edu/nsnam/ns/ns-documentation.html
• Tcl is a simple scripting language used in conjunction with Otcl to
setup simulation topologies and scenarios.
– http://dev.scriptics.com/man/tcl8.2.3/
• OTcl adds Object orientation to Tcl
– http://bmrc.berkeley.edu/research/cmt/cmtdoc/otcl/tutorial.html
• NAM – Network Animator is used to visualize simulations
– http://www.isi.edu/nsnam/nam
How to Install?

Downloading
NS2 is available from http://www.isi.edu/nsnam/ns/ns-
build.html
Download the ns-allinone package, ns-allinone-
2.33.tar.gz
Installing NS2
Go to directory of downloaded ns-allinone.tar.gz
Unpack the file using:
     tar -xzvf ns-allinone-2.33.tar.gz
Go to the ns-allinone-2.33 directory:
     cd ns-allinone-2.33
Install it with the command
     ./install
How to Install?

Setting ENV variables


PATH=$PATH:/usr/ns-allinone-2.33/bin:/usr/ns-allinone-
2.33/tcl8.4.18/unix:/usr/ns-allinone-2.33/tk8.4.18/unix
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/ns-allinone-
2.3/otcl-1.13:/usr/ns-allinone-2.33/lib
TCL_LIBRARY=$TCL_LIBRARY:/usr/ns-allinone-
2.33/tcl8.4.18
ns-2.33#./validate
How to run ns2
Type the following at the command prompt,
$ ns
% (for successful installation)
NS-2 Generic Script Structure

1) Create Simulator object

2) Turn on tracing

3) Create topology

4) Setup packet loss, link dynamics

5) Create application and/or traffic sources

6) Post-processing procedures (i.e. nam)

7) Start simulation
Step1: Create Simulator Object

• Create event scheduler


• set ns [new Simulator]
Step2: Tracing
• Insert immediately after scheduler !

• Trace packets on all links


set nf [open out.nam w]
$ns trace-all $nf
$ns namtrace-all $nf
Step2: Tracing
Step 3: Create network

• Two nodes, One link

n0

n1
Step 3: Create Network

n0
• Nodes
set n0 [$ns node]
set n1 [$ns node]

• Links and queuing n1


$ns duplex-link $n0 $n1 1Mb 10ms RED
$ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type>
<queue_type>: DropTail,….
Creating a larger topology
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms Droptail
}
Step 4: Network Dynamics

• Link failures
• Hooks in routing module to reflect routing changes
$ns rtmodel-at <time> up|down $n0 $n1

• For example:
$ns rtmodel-at 1.0 down $n0 $n1
$ns rtmodel-at 2.0 up $n0 $n1
Step 5: Creating UDP connection

set udp [new Agent/UDP]


udp
set null [new Agent/Null]

$ns attach-agent $n0 $udp


n0
$ns attach-agent $n1 $null

$ns connect $udp $null

n1

null
Step 6: Creating Traffic
(On Top of UDP) cbr

• CBR udp
set cbr [new Application/Traffic/CBR]

$cbr set packetSize_ 500 n0


$cbr set interval_ 0.005

$cbr attach-agent $udp

n1

null
Creating TCP connection tcp

set tcp [new Agent/TCP]


set tcpsink [new Agent/TCPSink]
n0

$ns attach-agent $n0 $tcp


$ns attach-agent $n1 $tcpsink

$ns connect $tcp $tcpsink n1

sink
Step 6: Creating Traffic
(On Top of TCP)
ftp

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

 Telnet
 set telnet [new Application/Telnet] n0
 $telnet attach-agent $tcp

n1

sink
Post-Processing Procedures

• Add a 'finish' procedure that closes the trace file and starts nam.

proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
Run Simulation
• Schedule Events
$ns at <time> <event>

$ns at 0.5 "$cbr start"


$ns at 4.5 "$cbr stop“

• Call ‘finish’
$ns at 5.0 "finish"

• Run the simulation


$ns run
Visualization Tools
• nam-1 (Network AniMator Version 1)
• Packet-level animation
• Well supported by ns
• xgraph
• Simulation results
nam Interface: Nodes
 Color
$node color red
 Shape (can’t be changed after sim starts)
$node shape box (circle, box, hexagon)
 Label (single string)
$ns at 1.1 “$n0 label \”web cache 0\””
nam Interfaces: Links

Color
$ns duplex-link-op $n0 $n1 color "green“

Label
$ns duplex-link-op $n0 $n1 label “backbone"
Application
 FTP
 set ftp [new Application/FTP]

$ftp attach-agent $tcp


 $tcp attach-app FTP

 CBR
 set cbr [new Application/Traffic/CBR]

 $cbr set packetSize_ 1000

$cbr set rate_ 16000


 Start traffic generation
 $ns at <time> “$app start”
Packet
 Packets are events
 Can be scheduled to “arrive”

 Packets contain header section and data


 Header section is a cascade of all in-use headers

 Each packet contains a common header


 packet size (used to compute transmission time)

 packet type

 timestamp, uid, …

 All in-use headers are included when simulation starts


 Change packet size to reflect header cascading
Simulation Example
 #Create a simulator object
set ns [new Simulator]

#Define different colors for data flows


$ns color 1 Blue
$ns color 2 Red

#Open the nam trace file


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

#Define a 'finish' procedure


proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
 #Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#Create links between the nodes


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

$ns duplex-link-op $n0 $n2 orient right-down


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

#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5

#Create a UDP agent and attach it to node n0


set udp0 [new Agent/UDP]
$udp0 set class_ 1
$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 UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1


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

#Create a Null agent (a traffic sink) and attach it to node n3


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

#Connect the traffic sources with the traffic sink


$ns connect $udp0 $null0
$ns connect $udp1 $null0

#Schedule events for the CBR agents


$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation


$ns run
 
Sample Script
 Create event scheduler
 set ns_ [new Simulator]

 Tracing
 set tracefd [open mytrace.tr w]
$ns_ trace-all $tracefd
set nf [open out.nam w]
$ns_ namtrace-all-wireless $nf $val(x) $val(y)

 Set up topography object


 set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
An example: topology
Node
- set n0 [$ns node]
- set n1 [$ns node]
- set n2 [$ns node]
Link
- $ns duplex-link $n0 $n1 1Mb 5ms DropTail
- $ns duplex-link $n1 $n2 400Kb 10ms DropTail
Summary
 NS-2 is an open source, discrete event, and packet level
network simulator
 NS-2 is written in C++ with OTcl interpreter as a front end
 TclCL provides linkage for class hierarchy, object
instantiation, variable binding and command dispatching
 NS-2 provides abundant implementations of protocols used in
wired and wireless networks
References
 J. Banks and I. J. S. Carson, Discrete-Event Systems Simulation. Prentice-Hall, Inc., 1984.
 The Network Simulator Wiki. [Online]. Available: http://nsnam.isi.edu/nsnam/index.php/
 The Network Simulator – ns-2. [Online]. Available: http://www.isi.edu/nsnam/ns/
 M. Greis. Tutorial for the Network Simulator NS2. [Online]. Available:
http://www.isi.edu/nsnam/ns/tutorial/
 J. Chung and M. Claypool. Ns by example. [Online]. Available: http://nile.wpi.edu/NS/
 The Network Simulator Wiki–Contributed Code. [Online]. Available:
http://nsnam.isi.edu/nsnam/index.php/Contributed Code
 H. Schildt, C++: The Complete Reference, 4th Edition (Kindle Edition), 4th ed. McGraw-
Hill/Osborne Media, 2002.
 K. Fall and K. Varadhan. (2007, Aug.) The ns manual (formerly known as ns notes and
documentation). [Online]. Available: http://www.isi.edu/nsnam/ns/ns-documentation.html
Thank You
and
Any questions ?

You might also like