Title: NS2 Tutorial
1NS-2 Tutorial
2Motivation
3Whats NS-2
- Network simulator
- Discrete event simulator
- It covers multiple layers
- Application layer, transport layer, network layer
and link layer. - Supports the simulation of
- intserv/diffserv, Multicast, Transport,
Applications, Wireless (fixed, mobile, satellite) - Packet level
4Discrete Event Simulation
Model world as events simulator has list of
events ordered by simulation time 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 gt no
locking or race conditions to worry about
(very easy)
5History and Status
- REAL variant (1989)?DARPA (LBL, Xerox PARC, UCB,
and USC/ISI) (1995) - Currently DARPA NSF collaboration with
researchers ACIRI, UCB Daedelus, CMU, Sun
Microsystems, NORTEL, Cisco - ns-1
- ns-2
- 100K lines of C
- 70K lines of OTcl
- 30K lines of test suite
- 20K lines of documentation
- Current version is 2.28
6ns-2 Programming Languages
- NS-2 is an object oriented simulator, written in
C, with an OTcl (Object Tool Command
Language) interpreter as a front-end. - Back-end C
- Defining new agents, protocols and framework.
- Manipulations at the byte/bit levels.
- if you have to change the behaviour of an
existing C class in ways that weren't
anticipated - Front-end Otcl Topologies, scenarios,
simulations, - Script language (easy topology modifications)
- if you can do what you want by manipulating
existing C objects
7Why Two Languages
- Simulator had two distinct requirements
- Detailed simulation of Protocol(Run-time speed)
- Varying parameters or configuration(Change model
rerun) - C is fast to run but slower to change
- Otcl runs much slower but can be changed quickly
8OTcl and C The Duality
Split Object Object created in Otcl has a
corresponding object in C.
Pure OTcl objects
Pure C objects
C/OTcl split objects
C
OTcl
ns
9ns Directory Structure
ns-allinone
TK8.0
OTcl
tclcl
Tcl8.0
ns-2
nam-1
C code
...
tcl
ex
test
mcast
lib
...
examples
validation tests
OTcl code
10Network Topology Node
Unicast Node
Classifier Address, Multicast, Multipath, Hash
11Network Topology Link
12Routing
13Routing (cont)
Link n0-n1
14Transport
n0
n1
Port Classifier
Port Classifier
Addr Classifier
Addr Classifier
0
0
dmux_
dmux_
Link n0-n1
entry_
entry_
classifier_
classifier_
Link n1-n0
15Application Traffic Generator
n0
n1
Port Classifier
Port Classifier
Agent/TCPSink
Addr Classifier
Agent/TCP
Addr Classifier
0
0
agents_
agents_
dmux_
dmux_
Link n0-n1
entry_
entry_
classifier_
classifier_
Link n1-n0
16Plumbing Packet Flow
n0
n1
Application/FTP
Port Classifier
Port Classifier
Agent/TCPSink
Addr Classifier
Agent/TCP
Addr Classifier
0
0
Link n0-n1
entry_
entry_
Link n1-n0
17Running NS-2 Program
18Resources
- http//www.isi.edu/nsnam/ns
- Tcl (Tool Command Language)
- http//dev.scriptics.com/scripting
- OTcl (MIT Object Tcl)
- otcl/doc/tutorial.html (in distribution)
- ns manual
- Included in distribution ns/doc
- http//www.isi.edu/nsnam/ns/ns-documentation.html
19Outline
- Whats NS-2
- NS-2 Structures
- TCL(Tool Command Language) Basics
- OTCL Basics
- Examples
20Basic Syntax
Tcl commands are evaluated in two steps First
the Tcl interpreter parses the commands into
words, performing substitution along the
way. Then a command procedure process the words
to produce a result string. Each command has a
separate command procedure.
21Tcl Basics
- Basic syntax
- command arg1 arg2 arg3
- command is either the name of a built-in command
or a Tcl procedure - Three basic steps
- Argument grouping
- variable substitution, command substitution
- Command invocation
22First example hello world!
- Hello world!
- puts stdout Hello World!
- Two points to emphasize
- Arguments are interpreted by the command
- Curly braces are used to group words together
into a single argument
23More examples
- set var 5
- gt 5
- set b var
- gt 5
- The set command is used to assign a value to a
variable
24Variable Substitution
- Variable substitution
- i the character i.
- i the variable i.
- set v1 6
- set v2 v1 (variable substitution)
25Command Substitution
- Command Substitution
- set value expr v1 v2
- gt set value 12
- Rewirte the outer command by using the result of
the nested command - Operation substitution
- set i 5 6wrong number of arguments
- set i 5 65 6
- set i 5 6invalid command
- set i expr 5 611
26Math Expressions
- Math Expressions
- expr 8 / 2
- gt 4
- set len expr string length foobar 7
- gt13
27Grouping
- Grouping group words (argument) together into
one argument - Grouping with curly braces
- Curly braces prevent substitution in the group
- Grouping with double quotes
- Double quotes allow substitution to occur in the
group - Example
- set a hello gt hello
- puts stdout The length of a is string length
a ." - gt The length of hello is 5
- puts stdout The length of a is string length
a . - gtThe length of a is string length a
28More on Tcl
- Control Structures
- if condition then .
- for set i 0 i lt 10 incr i 2
- Procedures
- proc proc_name arg1 arg2
29Example
- set a 43
- set b 27
- proc test a b
- set c expr a b
- set d expr expr a - b c
- for set k 0 k lt 10 incr k
- if k lt 5
- puts k lt 5, pow expr pow(d, k)
- else
- puts k gt 5, mod expr d k
-
-
-
- test 43 27
30Results
- k lt 5, pow 1.0
- k lt 5, pow 1120.0
- k lt 5, pow 1254400.0
- k lt 5, pow 1404928000.0
- k lt 5, pow 1573519360000.0
- k gt 5, mod 0
- k gt 5, mod 4
- k gt 5, mod 0
- k gt 5, mod 0
- k gt 5, mod 4
31OTcl Basics
- Creating a class
- Class class_name
- Class class_name superclass Base_class
- Defining instance procedures
- class_name instproc proc_name args ..
- Defining instance variables
- self instvar variable_name (inside a class
method)
32OTcl Basics
- Creating an instance
- set new_inst new class_name
- Calling an instance procedure
- new_inst proc_name args
- Using an instance value
- new_inst set v1 10
- set v2 new_inst set v1
33Examples
Class Mom Mom instproc greet self instvar
age_ puts age_ years old mom How are you
doing? Class Kid -superclass Mom Kid instproc
greet self instvar age_ puts age_ years
old kid Whats up, dude?
set mom new Mom mom set age_ 45 set kid new
Kid kid set age_ 15 mom greet kid greet
- 45 years old mom How are you doing?
- 15 years old kid What's up, dude?
34- NS2, and all of it's peer programs dependencies
are installed in/usr/ns/ on the Fedora machines
in the Main Undergraduate Lab in Math Sciences.
It is also available on the compute servers csc,
csd, and cse. Some notes
35- (1) You must put /usr/ns/otcl-1.9 and
/usr/ns/lib into your LD_LIBRARY_PATH environment
variable. - If you are using csh or tcsh, you can set it
like setenv LD_LIBRARY_PATH /usr/ns/otcl-1.9/usr
/ns/lib - If you are using sh or bash, you can set it like
export LD_LIBRARY_PATH/usr/ns/otcl-1.9/usr/ns/li
b
36- (2) You must put /usr/ns/tcl8.4.5/library into
your TCL_LIBRARY environment variable. Otherwise
ns/nam will complain during startup. - If you are using csh or tcsh, you can set it
like setenv TCL_LIBRARY /usr/ns/tcl8.4.5/library - If you are using sh or bash, you can set it like
export TCL_LIBRARY/usr/ns/tcl8.4.5/library
37- (3)You should put /usr/ns/bin into your PATH
environment variable. Otherwise you will have to
type the full path to ns/nam when you want to run
them. - If you are using csh or tcsh, you can set it
like setenv PATH /usr/ns/binPATH - If you are using sh or bash, you can set it like
export PATH/usr/ns/binPATH
38Elements of ns-2 Simulation
- Step 1 Create the event scheduler
- Step 2 Turn on tracing
- Step 3Create network topology
- Step 4 Create transport connection
- Step 5 Create traffic on top of transport
connection
39Topology
40Step 1 Creating Event Scheduler
- Create event scheduler
- set ns new Simulator
- Schedule events
- ns at lttimegt lteventgt
- lteventgt any legitimate ns/tcl commands
- Start scheduler
- ns run
41Step 2 Tracing
- Trace packets on all links
- ns trace-all open test.out w
- lteventgt lttimegt ltfromgt lttogt ltpktgt ltsizegt -- ltfidgt
ltsrcgt ltdstgt ltseqgt ltattrgt - 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- - 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- Trace packets on all links in nam format
- ns namtrace-all open test.nam w
- Must appear immediately after creating scheduler
42Tracing
- Trace all
- set nf open out.ns w
- ns trace-all nf
- Turn on tracing on specific links
43Step 3 Creating Network
- Nodes
- set n0 ns node
- set n1 ns node
- Links and queuing
- ns duplex-link n0 n1 ltbandwidthgt ltdelaygt
ltqueue_typegt - ltqueue_typegt DropTail, RED, CBQ, FQ, SFQ, DRR
44Create 6 nodes for set i 0 i lt 6 incr i
set n(i) ns node Create a duplex
link between the nodes ns duplex-link n(0)
n(4) 10Mb 10ms DropTail ns duplex-link n(1)
n(4) 10Mb 10ms DropTail ns duplex-link n(4)
n(5) 1.5Mb 10ms DropTail ns duplex-link n(5)
n(3) 10Mb 10ms DropTail ns duplex-link n(5)
n(2) 10Mb 10ms DropTail ns queue-limit n(4)
n(5) 20
45Step 4 Creat transport connecitonTCP
- TCP
- Create a TCP agent and attach it to node n(0)
- set up connection between node 0 and node 3
- set src_tcp new Agent/TCP/Reno
- ns attach-agent n(0) src_tcp
- set dst_tcp new Agent/TCPSink/DelAck
- ns attach-agent n(3) dst_tcp
- ns connect src_tcp dst_tcp
46Step 4 Creat transport connecitonUDP
- UDP
- UDP flow between n1 and n7
- set src_udp new Agent/UDP
- ns attach-agent n(1) src_udp
- set dst_udp new Agent/LossMonitor
- ns attach-agent n(2) dst_udp
- ns connect src_udp dst_udp
47Step 5 Creating Traffic (On Top of UDP)
- CBR
- Setup a CBR over UDP connection
- set cbr new Application/Traffic/CBR
- cbr attach-agent src_udp
- cbr set type_ CBR
- cbr set packetSize_ 512
- cbr set rate_ 0.8Mb
48Creating Traffic (On Top of TCP)
- FTP
- set ftp new Application/FTP
- ftp attach-agent src_tcp
- ftp set packetSize_ 512
49Things to Remember
- Topology, agents, sources, start
- Connect the agents
- Slot not found error
- Start/Stop the sources
- In procedures, declare global variables before
use - ns run last line