You are on page 1of 48

Ns2

Network Simulator
Tutorial for Beginners
Udhay Prakash
Udhayprakash.blogspot.in
Uday3prakash @gmail.com
Presentation Agenda
Introduction to Linux
Importance of Open Source software
Network Simulators
Importance and usage of ns2 Importance and usage of ns2
Ns2 Installation
TCL scripting language
Introduction to NAM and Xgraph
Discussion about trace file format
04-Jul-14 2 udhayprakash.blogspot.in
Outcomes
To get ease with Linux platform
Open Source vs Free ware vs proprietary
To insight about a Network Simulator
Importance of ns2 network simulator Importance of ns2 network simulator
Scripting language vs programming language
Ability to install ns2, nam and xgraph
Ease in working with TCL scripting
Insight to NAMand Trace files
04-Jul-14 3 udhayprakash.blogspot.in
SESSION -1
Introducing to Linux and Ns2 Introducing to Linux and Ns2
04-Jul-14 4 udhayprakash.blogspot.in
Linux
Developed under GNU project.
Open Source software, except RedHat
Major Linux Families
Debian Debian, Ubuntu, Mint, .
Fedora
OpenSUSE
ArchLinux
CentOS & RedHat
04-Jul-14 5 udhayprakash.blogspot.in
Working in Linux
Are you new to Linux Operating System?
Are you more habituated to Windows or
Macintosh Operating Systems ?
Then, carefully follow me (switch on your PCs) Then, carefully follow me (switch on your PCs)
Creating a file
Reading/writing/editing a file
Operating System specific file types
04-Jul-14 6 udhayprakash.blogspot.in
Why Simulators?
Advantages
Complex scenarios can be easily tested
Cheap-does not require costly equipment
Results can be quickly obtained-more ideas can be tested in a
smaller timeframe smaller timeframe
Controlled experimental conditions
Repeatability helps aid debugging
Disadvantages
Not real
Real systems too complex to model
Consideration of practical conditions
04-Jul-14 7 udhayprakash.blogspot.in
NS2 Network Simulator
Open-source event-driven simulator
Designed specifically for research in computer
communication networks.
Contains modules for numerous network Contains modules for numerous network
components such as routing, transport layer
protocol, and application.
04-Jul-14 8 udhayprakash.blogspot.in
Ns2 sources
http://www.isi.edu/nsnam/ns/
The source code and documentation is currently maintained
by VINT project at ISI
http://nsnam.isi.edu/nsnam/index.php/Main_Page
Sept 3, 2007: ns-2.32 released. Sept 3, 2007: ns-2.32 released.
Mar 10, 2007: ns-2.31 released.
July 2, 2006: ns-3 project announced.
NS2 is a discrete event simulator targeted at networking research
NS2 is an object oriented simulator, written in C++, with an OTcl
interpreter as a frontend
04-Jul-14 9 udhayprakash.blogspot.in
NS2 related Documentation
introductory: Marc Greis's tutorial
reference: Ns Manual (formerly called "ns Notes
and Documentation")
ns by Example ns by Example
Practical Programming in Tcl and Tk
(http://www.beedub.com/book/)
http://hpds.ee.ncku.edu.tw/~smallko/ns2/ns2.htm
04-Jul-14 10 udhayprakash.blogspot.in
NS2- Features
Collection of various protocols at multiple layers
tahoe, vegas, sack)
MAC(802.11, 802.3, TDMA)
Ad-hoc Routing (DSDV, DSR, AODV, TORA)
Sensor Network (diffusion, gaf)
Multicast protocols, Satellite protocols, and TCP(reno, many others
Traffic Models: CBR, VBR, Web, Traffic Models: CBR, VBR, Web,
Error Models: Uniform, Busty,
Misc: Radio Propagation, Mobility models, Energy Models,
Topology Generation tools (nsg2.1, ..)
Visualization tools (NAM), Tracing
Codes are contributed from multiple research communities
Good: Large set of simulation modules
Bad: Level of support and documentation varies
04-Jul-14 11 udhayprakash.blogspot.in
NS2 Chronicle
1989: REAL by Keshav
1995: ns by Floyd, McCanne at LBL
1997: ns-2 by the VINT project (Virtual
InterNetwork Testbed) at LBL, Xerox PARC, UCB,
12
InterNetwork Testbed) at LBL, Xerox PARC, UCB,
USC/ISI
Now: ns-2.35 maintained at USC/ISI
Also there is NS-3, but it has no backward
compatibility with NS-2.
04-Jul-14 udhayprakash.blogspot.in
NS2 Chronicle
REAL Simulator NS (or NS1)NS2NS3.
NS-1
LNBL Simulator tool LNBL Simulator tool
Developed by the network research group at the
Lawrence
Berkeley National Laboratory (1989)
Derived from REAL Simulator
Simulation description language: TCL
04-Jul-14 13 udhayprakash.blogspot.in
NS2 Chronicle
NS-2
Object Oriented (OTcl instead of TCL)
Built in C++ and provides a simulation interface through
OTcl
Developed by UC Berkeley
Difficult for the first time users
Very few user friendly manuals. Ex: ns manual
Needs scripting language, queuing theory and modelling techniques
knowledge
Complaint about inconsistent results
And BUGS!!!!
But, large community support
04-Jul-14 14 udhayprakash.blogspot.in
NS3
No backward compatibility with NS2
YANS (Yet Another Network Simulator)NS-3 base
NS-3.1 (2008), NS-3.11(2011),
Built in C++ and python
Good support for LTE and Wimax Good support for LTE and Wimax
Simple to work with..and suitable for dumping into and
getting data from real-time devices.
No support for some protocols which were supported in
ns-2.
But, lesser community support
04-Jul-14 15 udhayprakash.blogspot.in
NS-2 Directory Structure
TK8.0 OTcl tclcl Tcl8.0 ns-2 nam-1
ns-allinone
tcl
ex test lib
...
...
examples validation tests
C++ code
OTcl code
mcast
simple.tcl
04-Jul-14 16 udhayprakash.blogspot.in
NS-2 Environment
Simulation
Scenario
Tcl Script
1 2
set ns_ [new Simulator]
set node_(0) [$ns_ node]
Tcl Script
C++
Implementation
set node_(0) [$ns_ node]
set node_(1) [$ns_ node]
class MobileNode : public Node
{
friend class PositionHandler;
public:
MobileNode();

}
04-Jul-14 17 udhayprakash.blogspot.in
NS2 Architecture
04-Jul-14 18 udhayprakash.blogspot.in
Why two languages? (Tcl & C++)
C++: Detailed protocol simulations require systems programming
language
byte manipulation, packet processing, algorithm implementation
Run time speed is important 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
04-Jul-14 19 udhayprakash.blogspot.in
What is Tcl/Tk ?
It is a Scripting language to describe a high-level programming language with
relatively transparent syntax
Tools Command Lanuage (TCL)
simple string based scripting language for controlling and extending applications.
provides generic programming facilities such as variables, loops, and
procedures. procedures.
Tcl syntax is very much similar to UNIX shell programming.
Tool kit (TK)
based on X-window system.
provides facilities (widgets) for building user interfaces.
Tcl/Tk
Excellent tools for Rapid prototyping.
Tcl/Tk is interpreted rather than compiled. ( The interpreter is in !/opt/bin/wish )
weakly typed language (strings and integer variables are interchangeable).
04-Jul-14 20 udhayprakash.blogspot.in
Basic Tcl
variables:
set x 10
puts x is $x
functions and expressions:
procedures:
proc pow {x n} {
if {$n == 1} { return $x }
set part [pow x [expr $n-1]]
return [expr $x*$part]
set y [pow x 2]
set y [expr x*x]
control flow:
if {$x > 0} { return $x } else {
return [expr -$x] }
while { $x > 0 } {
puts $x
incr x 1
}
return [expr $x*$part]
}
Also lists, associative arrays, etc.
=> can use a real programming
language to build network
topologies, traffic models, etc.
04-Jul-14 21 udhayprakash.blogspot.in
Basic oTcl
Class Person
# constructor:
Person instproc init {age} {
$self instvar age_
set age_ $age
}
# subclass:
Class Kid -superclass Person
Kid instproc greet {} {
$self instvar age_
puts $age_ years old kid: Whats
up, dude?
}
# method:
Person instproc greet {} {
$self instvar age_
puts $age_ years old: How are
you doing?
}
up, dude?
}
set a [new Person 45]
set b [new Kid 15]
$a greet
$b greet
# new in ns2 only
=> can easily make variations of existing things (TCP, TCP/Reno)
Person a 45
Kid b 15
a greet
b greet
04-Jul-14 22 udhayprakash.blogspot.in
Split Object Model
Split Object Model with shared class hierarchy
Control (OTcl) vs. Data (C++)
TclCL provides glue library to easily share functions, variables, etc
OTcl into C++: command(), tcl.result()
C++ into OTcl: tcl.eval()
Not all classes are mirrored exactly
OTcl C++
23
OTcl script
describing
simulation
T
c
l
C
L
udhayprakash.blogspot.in
C++ and OTcl Linkage
Class Tcl: instance of OTcl interpreter
Tcl& tcl = Tcl::instance();
tcl.evalc(puts stdout hello world);
tcl.result() and tcl.error()
Class TclObject and TclClass
Variable bindings
bind(rtt_, &t_rtt_)
Invoking command method in shadow class
$tcp advanceby 10
04-Jul-14 24 udhayprakash.blogspot.in
Calling Otcl functions from C++
OTcl
Agent/TCP instproc advance {num} {
set window_ [expr $window_ + $num]
return $window_
}}
C++
Tcl& tcl = Tcl::instance();
char *result;
tcl.evalf(%s advance %d, name_, size);
result = tcl.result();
wnd_ = atoi(result);
04-Jul-14 25 udhayprakash.blogspot.in
EmbeddedTcl
How it works
tcl2c++: provided by TclCL, converts tcl scripts
into a C++ static character array
Makefile.in: Makefile.in:
tclsh8.0 bin/tcl-expand.tcl tcl/lib/ns-lib.tcl |
tcl2c++ et_ns_lib > gen/ns_tcl.cc
04-Jul-14 26 udhayprakash.blogspot.in
C++ and OTcl Separation
data / control separation
C++ for data:
per packet processing, core of ns
fast to run, detailed, complete control
OTcl for control: OTcl for control:
Simulation scenario configurations
Periodic or triggered action
Manipulating existing C++ objects
fast to write and change
+ running vs. writing speed
Learning and debugging (two languages)
04-Jul-14 27 udhayprakash.blogspot.in
Object Granularity Tips
Functionality
Per-packet processing C++
Hooks, frequently changing code OTcl
Data management Data management
Complex/large data structure C++
One-time configuration variables OTcl
04-Jul-14 28 udhayprakash.blogspot.in
Ns2 Installation Procedure
Linux Installation (preferred):
Go to udhayprakash.blogspot.in
Search for ns2 installation procedure
Follow it ..! Follow it ..!
Windows Installation
Go to
http://nsnam.isi.edu/nsnam/index.php/Running_Ns
_and_Nam_Under_Windows_9x/2000/XP_Using_
Cygwin
04-Jul-14 29 udhayprakash.blogspot.in
Where to find/ns-2/tcl/lib
ns-lib.tcl:
The simulator class and most of its member function
definitions
ns-default.tcl:
The default values for configurable parameters The default values for configurable parameters
ns-packet.tcl:
The packet header format initialization implementation
other OTcl files:
implementation of compound network objects or the front end
(control part) of network objects
04-Jul-14 30 udhayprakash.blogspot.in
SESSION -2
Working with Ns2 Working with Ns2
04-Jul-14 31 udhayprakash.blogspot.in
A Simple Topology
n0 n1
Application
Agent Agent
0
1
1
0
0 0
head_ enqT_ queue_ deqT_ link_ ttl_
drophead_
rcvT_
drpT_
Link
n1 n0
1 0
04-Jul-14 32 udhayprakash.blogspot.in
Application
N1
N2
C
l
a
s
s
i
f
i
e
r
C
l
a
s
s
i
f
i
e
r
Agent
Node
Port
Local
C
l
a
s
s
i
f
i
e
r
C
l
a
s
s
i
f
i
e
r
Agent
Node
Port
Local
Application
Link
Link
Application
C
l
a
s
s
i
f
i
e
r
Addr
Port
C
l
a
s
s
i
f
i
e
r
Addr
Port
Enq Trace Deq Trace Rcv Trace
Drp Trace
Delay TTL
Drop head
Queue
Application
04-Jul-14 33 udhayprakash.blogspot.in
Using ns
Problem
Simulation
Result
Simulation
model
Setup/run
simulation
with ns
Result
analysis
Modify
ns
04-Jul-14 34 udhayprakash.blogspot.in
NS-2 Generic Script Structure
set ns [new Simulator] set ns [new Simulator]
# [Turn on tracing] # [Turn on tracing]
# [Turn on # [Turn on visualizer visualizer]]
# Create topology # Create topology
# Setup packet loss, link dynamics # Setup packet loss, link dynamics
# Create routing agents # Create routing agents # Create routing agents # Create routing agents
# Create: # Create:
# # -- multicast groups multicast groups
# # -- protocol agents protocol agents
# # -- application and/or setup traffic sources application and/or setup traffic sources
# Post # Post--processing processing procs procs
# Start simulation # Start simulation
04-Jul-14 35 udhayprakash.blogspot.in
Event Scheduler Creation
Event is a packet ID that is unique for a packet, which constitutes:
Scheduled time
The pointer to an object that handles the event
An event scheduler
Keeps track of simulation time
Fires all the events in the event queue
Create event scheduler: set ns [new simulator]
Schedule an event: $ns at <time> <event>
Event is any legitimate ns/tcl function
$ns at 5.0 finish
Start scheduler
$ns run
04-Jul-14 36 udhayprakash.blogspot.in
Tracing
04-Jul-14 37 udhayprakash.blogspot.in
Tracing
Packet tracing and event tracing
Objects that are inserted between nodes
Insert from OTcl using trace{} method
$ns trace-all $file, or
38
[$ns link $n0 $n1] trace $file
Monitoring
Record counters of interest for all packets or on a
per-flow basis
or roll your own
04-Jul-14 udhayprakash.blogspot.in
Tracing
ns and nam trace file formats
ns also supports show_tcphdr_ variable controlling display of TCP
flags
ns format
op op ts ts prevhop prevhop nexthop nexthop type type size size flags flags flowid flowid src src dst dst seqno seqno pktid pktid
++ 1.102 1.102 00 11 tcp tcp 40 40 00 0.0 0.0 1.0 1.0 11 11
39
Enque [+], dequeue [-], receive [r], drop [d]
Types are
Flags are
ECN-CE [E], -ECT [N], -CE [C], -CWR [A]; plus
priority [P] and TCP fast start [F]
++ 1.102 1.102 00 11 tcp tcp 40 40 00 0.0 0.0 1.0 1.0 11 11
04-Jul-14 udhayprakash.blogspot.in
Visualization (nam)
set nf [open out.nam w]
$ns namtrace-all $nf
Proc finish {} {
global ns nf global ns nf
close $nf
exec nam out.nam &
exit 0
}
04-Jul-14 40 udhayprakash.blogspot.in
Topology Creation
04-Jul-14 41 udhayprakash.blogspot.in
Sending Data
04-Jul-14 42 udhayprakash.blogspot.in
Sending Data
04-Jul-14 43 udhayprakash.blogspot.in
Creating TCP Connections
Create TCP agent and attach it to the node
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
Create a Null Agent and attach it to the node Create a Null Agent and attach it to the node
Set null0 [new Agent/TCPSink]
$ns attach-agent $n1 $null0
Connect the agents
$ns connect $tcp0 $null0
04-Jul-14 44 udhayprakash.blogspot.in
Traffic on top of TCP
FTP
set ftp [new Application/FTP]
$ftp attach-agent $tcp0
Telnet
Set telnet [new Application/Telnet]
$telnet attach-agent $tcp0
04-Jul-14 45 udhayprakash.blogspot.in
NS2 pre- and post processing
pre-processing:
traffic and topology generators
NSG 2.1 (for more details, go to udhayprakash.blogspot.in )
tclTutor
post-processing:
simple trace analysis, often in Awk, Perl, or Tcl.
Awk scripting is preferred.
04-Jul-14 46 udhayprakash.blogspot.in
Summary
Simulators help in easy verification of protocols in
less time, money.
NS2 offers support for simulating a variety of
protocols suites and scenarios
Front end is oTCL, back end is C++
NS2 is on-going effort of research and development.
04-Jul-14 47 udhayprakash.blogspot.in
Reference
1) ns2 website: http://www.isi.edu/nsnam/ns
2) Marc Greis' tutorial
3) Jae Chung tutorial
4) Ns manual
5) Tcl/Tk http://www.tcl.tk/software/tcltk/ 5) Tcl/Tk http://www.tcl.tk/software/tcltk/
6) Nsnam web pages http://www.isi.edu/nsnam/
7) NS by example http://nile.wpi.edu/NS/
8) Tutorial Marc Greis's tutorial
9) Discrete-event simulation software
http://www.topology.org/soft/sim.html
10) Introduction to NS2(PPT), Prepared by Changyong Jung
04-Jul-14 48 udhayprakash.blogspot.in

You might also like