Network Simulator 2
Advanced Computer Network (CS5229)
Saeid Montazeri
September 8, 2011
Outline
Overview of Simulation
Overview of NS2
Steps for network simulation
How to make a scenario
How to change the implementation of NS2
References
September 8, 2011 2
What is simulation?
system boundary
exogenous inputs system under study
to system (has deterministic rules real life
(the environment) governing its behavior)
observer
program boundary
computer program
psuedo random inputs
simulates deterministic
to system simulated life
rules governing behavior
(models environment)
observer
September 8, 2011 3
Why Simulation?
Real-system not available, is complex/costly or
dangerous (eg: space simulations, flight
simulations)
Quickly evaluate design alternatives (eg: different
system configurations)
Evaluate complex functions for which closed form
formulas or numerical techniques are not available
September 8, 2011 4
Programming a simulation
What is in a simulation program?
Simulated time
Internal variable that keeps the time
System state
Variables maintained by simulation program
Number of packet in the queue, transmission time value
Events
occurs at an instant in time and marks a change of system state
Arrival packet to a queue, departure of packet from a queue
September 8, 2011 5
Simulator Structure
Simulation program maintains and updates list of
future events: event list
Requirements:
well defined set of events
for each event: simulated system action,
updating of event list
September 8, 2011 6
Simulator Block Diagram
initialize event list
get next (nearest future)
event from event list
time = event time
process event
(change state values, add/delete
future events from event list)
update statistics
n
done?
September 8, 2011 7
Outline
Overview of Simulation
Overview of NS2
Steps for network simulation
How to make a scenario
How to change the implementation of NS2
References
September 8, 2011 8
What is NS2?
Stands for Network Simulator version 2
Discrete event simulator for networking research
Work at packet level
Provide substantial support to simulate bunch of
protocols like TCP, UDP, FTP and HTTP
Simulate wired and wireless network.
Primarily Unix based
September 8, 2011 9
Otcl and C++
C++ for data
Per packet action
Otcl for control
Periodic and triggered action
September 8, 2011 10
September 8, 2011 11
How does NS2 work?
September 8, 2011 12
Installation
Ubuntu 11:
http://narnicles.wordpress.com/2011/05/10/installing-ns2-in-ubuntu-11-04/
Ubuntu 10:
Go the below link and choose the second procedure
http://www.scribd.com/doc/46784839/Install-NS2-34-in-Ubuntu10-10
Windows:
For example:
http://paulson.in/?p=49
September 8, 2011 13
Outline
Overview of Simulation
Overview of NS2
Steps for network simulation
How to make a scenario
How to change the implementation of NS2
References
September 8, 2011 14
Steps for network simulation
1. Implement the protocol models
2. Setup simulation scenario
3. Run the simulation
4. Analyze the simulation output
September 8, 2011 15
Simulation scenario
Write scenario in Otcl
Create topology
Nodes, links, protocol parameters
Traffic
CBR, VBR, Exponential On/Off
NS2 interprets script file and pass required parameters
to model
September 8, 2011 16
First example
Bandwidth:1Mbps
Latency: 10ms
n1 n2
September 8, 2011 17
First example (cont)
#create a new simulator object
set ns_ [new Simulator]
#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
September 8, 2011 18
}
First example (cont)
#create 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 secs of
simulated time
$ns_ at 5.0 "finish
#run the simulation
$ns_ run
September 8, 2011 19
Demo
September 8, 2011 20
Adding traffic
1Mbps,10ms
n1 n2
udp
null
cbr
node
agent
Packet Size: 500 bytes
rate: 800Kbps source
link
cbr traffic
0.5 4.5 time
0.0 5.0
September 8, 2011 21
First example (cont)
#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 to the sink
$ns_ connect $udp0 $null0
#Schedule events for CBR traffic
$ns_ at 0.5 "$cbr0 start"
$ns_
Septemberat 4.5 "$cbr0 stop
8, 2011 22
Demo
September 8, 2011 23
Outline
Overview of Simulation
Overview of NS2
Steps for network simulation
How to make a scenario
First simple example
Second example
Post processing
How to change the implementation of NS2
References
September 8, 2011 24
Second Scenario (NS by Example)
September 8, 2011 25
Second Scenario (cont)
#Create a simulator object
set ns_ [new Simulator]
#Define different colors for data flows (for NAM)
$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 NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
September 8, 2011 26
Second Scenario (cont)
#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 2Mb 10ms DropTail
$ns_ duplex-link $n1 $n2 2Mb 10ms DropTail
$ns_ duplex-link $n2 $n3 1.7Mb 20ms DropTail
#Set Queue Size of link (n2-n3) to 10
$ns_ queue-limit $n2 $n3 10
September 8, 2011 27
Second Scenario (cont)
#Give node position (for NAM)
$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 link (n2-n3). (for NAM)
$ns_ duplex-link-op $n2 $n3 queuePos 0.5
September 8, 2011 28
Second Scenario (cont)
#Setup a TCP connection
set tcp [new Agent/TCP]
$ns_ attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns_ attach-agent $n3 $sink
$ns_ connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
September 8, 2011 29
Second Scenario (cont)
#Setup a UDP connection
set udp [new Agent/UDP]
$ns_ attach-agent $n1 $udp
set null [new Agent/Null]
$ns_ attach-agent $n3 $null
$ns_ connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
$cbr
September set
8, 2011 random_ false 30
Second Scenario (cont)
#Schedule events for the CBR and FTP agents
$ns_ at 0.1 "$cbr start"
$ns_ at 1.0 "$ftp start"
$ns_ at 4.0 "$ftp stop"
$ns_ at 4.5 "$cbr stop"
#Call the finish procedure after 5 seconds of simulation
time
$ns_ at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns_ run
September 8, 2011 31
Demo
September 8, 2011 32
How does NS2 work?
September 8, 2011 33
How to get trace file?
#Create a simulator object
set ns_ [new Simulator]
#Define different colors for data flows (for NAM)
$ns_ color 1 Blue
$ns_ color 2 Red
#Open the NAM trace file
set nf [open out.nam w]
$ns_ namtrace-all $nf
#Open the traffic trace file to record all events
set nd [open out.tr w]
$ns_ trace-all $nd
#Define a 'finish' procedure
proc finish {} {
global ns_ nf nd
$ns flush-trace
#Close the NAM trace file
close $nf
close $nd
#Execute NAM on the trace file
#exec nam out.nam &
exit 0
Directory + Demo
September 8, 2011 35
Demo
Write script to measure packet loss
September 8, 2011 37
Outline
Overview of Simulation
Overview of NS2
Steps for network simulation
How to make a scenario
How to change the implementation of NS2
References
September 8, 2011 38
Second Scenario (changed)
#Create four nodes
set n0 [$ns_ node]
set n1 [$ns_ node]
set n2 [$ns_ node]
set n3 [$ns_ node]
#Create links between the nodes
Queue/RED set thresh_ 2
Queue/RED set maxthresh_ 6
Queue/RED set queue_in_bytes_ false
Queue/RED set gentle_ false
$ns_ duplex-link $n0 $n2 2Mb 10ms DropTail
$ns_ duplex-link $n1 $n2 2Mb 10ms DropTail
$ns_ duplex-link $n2 $n3 1.7Mb 20ms RED
September 8, 2011 39
How to change NS implementation
1.Save your changes in cc file
2.Go to ns-allinone-2.34/ns-2.34 in command
prompt
3.switch to root user
4.Type make clean
5.Type make
6.Type make install
September 8, 2011 40
Demo
September 8, 2011 41
Reference
Marc Greis tutorial
http://www.isi.edu/nsnam/ns/tutorial/nsscript2.html
NS2 by example
http://perform.wpi.edu/NS/
NS2
http://www.isi.edu/nsnam/ns
Simulation Overview (Jim Kurose, University of Massachusets, Amhers)
September 8, 2011 42