Network Simulation Using NS2 : Network Simulation Using NS2 V. Vasantha M.E.,
Senior Lecturer,
Dept. Of Information Technology,
National Engineering College,
K. R. Nagar, Kovilpatti.
vivekvasantha@gmail.com
Network Simulation : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 2 Network Simulation Overview:
fundamentals of discrete event simulation
ns-2 simulation
Why Simulation? : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 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 not available
Simulation: advantages/drawbacks : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 4 Simulation: advantages/drawbacks advantages:
sometimes cheaper
find bugs (in design) in advance
generality: over analytic/numerical techniques
detail: can simulate system details at arbitrary level
drawbacks:
caution: does model reflect reality
large scale systems: lots of resources to simulate (especially accurately simulate)
may be slow (computationally expensive – 1 min real time could be hours of simulated time)
art: determining right level of model complexity
statistical uncertainty in results
NS2 Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 5 NS2 Outline What is it?
How do I get it?
How do I work in it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
NS2 Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 6 NS2 Outline What is it?
How do I get it?
How do I work in it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
What is NS2? : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 7 What is NS2? Network Simulator
A package of tools that simulates behavior of networks
Create Network Topologies
Log events that happen under any load
Analyze events to understand the network behavior
Functional Diagram of NS2 : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Functional Diagram of NS2 Problem Topology Setup/execute
simulation
with ns (.tcl) Result
Analysis/debug Modify
ns (.cpp/.tcl) 8
NS-2 Components : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 9 NS-2 Components NS, the simulator itself
Nam, the network animator
Visualize ns (or other) output (files .nam)
Nam editor: GUI interface to generate ns scripts
Pre-processing:
Traffic and topology generators
Post-processing:
Generates Output files
Simple trace analysis, often in Awk, Perl, or Tcl
NS-2 Components- Contd., : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 10 NS-2 Components- Contd.,
Simulation Process at a Glance : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Simulation Process at a Glance
Create your Topology : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Create your Topology Decide what do you want to simulate
Wired or wireless network
What are the protocols?
How many nodes, what are the measuring parameters?
What are the applications involved, etc?
Make a rough sketch of the topology
Figure out the concerned files (C++ or .tcl)
Based on the requirement do the following
Edit the existing C++ files and/or the .tcl files
You can create new C++ files 12
Creating Topologies : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 13 Creating Topologies Nodes
Set properties like queue length, location
Protocols, routing algorithms
Links
Set types of link – Simplex, duplex, wireless, satellite
Set bandwidth, latency etc.
Done through tcl Scripts
Observing Network Behavior : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 14 Observing Network Behavior Observe behavior by tracing “events”
Eg. packet received, packet drop etc. time Src Dst IP Address, Port
Observing Network Behavior : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 15 Observing Network Behavior NAM:
Network Animator
A visual aid showing how packets flow along the network
Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 16 Outline What is it?
How do I get it?
How do I work in it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
How Do I get NS2? : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 17 How Do I get NS2? http://www.isi.edu/nsnam/ns/ - NS Home Page
http://www.isi.edu/nsnam/ns/ns-build.html - NS Installation
http://www.isi.edu/nsnam/ns/ns-documentation.html - NS Documentation
Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 18 Outline What is it?
How do I get it?
How do I work in it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
How to work in NS2 ? : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti How to work in NS2 ? Download the software
Install NS2 in your home directory
Compile the latest version of NS2
Validate NS2
Create your topology
Need to understand the real topology and the directory structure in NS2
Modify the existing codes
C++ and/or .tcl files
Create your own .tcl script for this
Execute the script
Analyze your result 19
Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 20 Outline What is it?
How do I get it?
How do I work in it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
How Do I use it? : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 21 How Do I use it? Creating a Simple Topology
Getting Traces
Using NAM
Basics of using NS2 : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 22 Basics of using NS2 Define Network topology, load, output files in Tcl Script
To run,
$ ns simple_network.tcl
Internally, NS2 instantiates C++ classes based on the tcl scripts
Output is in form of trace files
Creating the topology : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 23 #create a new simulator object
set ns [new Simulator]
#open the trace file
set f [open out.tr w]
$ns trace-all $f
#open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf Creating the topology #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
}
Simple two node wired network : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Simple two node wired network n0 n1 Step 1: Step 2: #Open trace files
set f [open out.tr w]
$ns trace-all $f
set nf [open out.nam w]
$ns namtrace-all $nf 24
Simple two node wired network : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Simple two node wired network n0 n1 #Create two nodes
set n0 [$ns node]
set n1 [$ns node] Step 3: Step 4: #Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail 25
Simple two node wired network : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Simple two node wired network #Create a simulator object
set ns [new Simulator]
#Open trace files
set f [open out.tr w]
$ns trace-all $f
#Define a 'finish' procedure
proc finish {} {
global ns f
$ns flush-trace
exit 0
}
#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 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run 26
Adding traffic to the link : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 27 Adding traffic to the link n1 n2 1Mbps,10ms udp null cbr Packet Size: 500 bytes
rate: 800Kbps
Adding traffic to the link : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Adding traffic to the link n0 n1 udp #Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0 28
Adding traffic to the link : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Adding traffic to the link n0 n1 udp # 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 cbr 29
Adding traffic to the link : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Adding traffic to the link n0 n1 udp cbr #Create a Null agent (a traffic sink) and
attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0 null 30
Adding traffic to the link : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Adding traffic to the link n0 n1 udp cbr #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“ null 31
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Simulate a simple topology – UDP Traffic #Create a simulator object
set ns [new Simulator]
#Open trace files
set f [open out.tr w]
$ns trace-all $f
#Define a 'finish' procedure
proc finish {} {
global ns
$ns flush-trace
exit 0
}
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Simulate a simple topology – UDP Traffic #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
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti #Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n0 $udp0 Simulate a simple topology – UDP Traffic 34
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Simulate a simple topology – UDP Traffic # 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 35
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti #Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1 Simulate a simple topology – UDP Traffic 36
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti # 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 Simulate a simple topology – UDP Traffic 37
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti #Create a Null agent (a traffic sink) and attach it to node n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0 Simulate a simple topology – UDP Traffic
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti #Connect the traffic sources with the traffic sink
$ns connect $udp0 $null0
$ns connect $udp1 $null0 Simulate a simple topology – UDP Traffic
Simulate a simple topology – UDP Traffic : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti #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 Simulate a simple topology – UDP Traffic 40
A second Scenario * (from NS by Example) : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 41 A second Scenario * (from NS by Example) Taken from NS by Example by Jae ChungandMark Claypool
Slide 42 : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Trace Analysis http://nsnam.isi.edu/nsnam/index.php/NS-2_Trace_Formats
Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 43 Outline What is it?
How do I get it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
How can I add to NS2? : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 44 How can I add to NS2? Adding Protocols to NS2 is possible
Need to create the C++ class
Need to create the OTcl Linkage
More info at:
http://www.isi.edu/nsnam/ns/tutorial/index.html
Tutorial about how to add a simple protocol to NS2
Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 45 Outline What is it?
How do I get it?
How do I work in it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
Documentation – NS2 Documentation : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 46 Documentation – NS2 Documentation NS2 Manual
Information about Otcl interpreter, C++ class hierarchy, parameters for various protocols
http://www.isi.edu/nsnam/ns/doc/index.html
Very detailed, useful when looking for something specific, like:
What are the shadowing models available for wireless? How do I select them?
How do I make my routing strategy to be Distance Vector routing?
Documentation – NS2 documentation : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 47 Documentation – NS2 documentation NS2 Tutorial by Marc Greis
http://www.isi.edu/nsnam/ns/tutorial/index.html
Good starting point for understanding the overall structure of NS2
Examples:
What is the relation between c++ classes and Otcl classes?
basic info on instantiating NS2 instance, tcl scripting
Documentation – NS2 Documentation : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 48 Documentation – NS2 Documentation NS2 for beginners
http://www-sop.inria.fr/maestro/personnel/Eitan.Altman/COURS-NS/n3.pdf
More detailed than Marc Greis’ Tutorial
More info on getting it up and running – rather than internals
Examples:
What does each line of a tcl script do?
Most common examples of trace formats that are useful
Documentation – Tcl Documentation : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 49 Documentation – Tcl Documentation Tcl Tutorial
http://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html
Tcl Manual
All commands and their explanation
http://www.tcl.tk/man/tcl8.6/TclCmd/contents.htm
Outline : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 50 Outline What is it?
How do I get it?
How do I use it?
How do I add to it?
Documentation
Bug-Fixing
Bug-Fixing – When things go wrong.. : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 51 Bug-Fixing – When things go wrong.. Googling for the problem!
Extensive NS2 mailing lists
Chances are that other people have had the same problem are very high
Responsive forums
Bug-Fixing – When things go wrong.. : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 52 Bug-Fixing – When things go wrong.. NS2 in-built examples
Extensive inbuilt examples
“diffing” with the examples helps a lot
Sometimes a good idea to start from a script that does something similar
Bug-Fixing – When things go wrong.. : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 53 Bug-Fixing – When things go wrong.. Taking a look at the code
Everyone adds to NS2
May not always confirm to the norms
IP TTL set to 32 instead of 256
Golden Rules : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti Golden Rules Follow the template tcl like Bible.
All communication between 2 agents.
Upper layer to lower layer, we attach agents.
Same layer in two nodes, we connect agents.
Agents are tcp, udp, telnet, cbr…etc 54
Slide 55 : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti In UDP communication, data is flows from UDP agent to Null agent. 55
Slide 56 : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti In TCP communication, data is flows from TCP agent to TCPsink agent. 56
Slide 57 : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 57 Queries ???
Thank U !!! http://www.wiziq.com/vasantha
Slide 58 : 10/4/2010 V. Vasantha, National Engineering College, Kovilpatti 58