What’s NS-2• Network simulator – Discrete event simulator • It covers multiple layers – Application layer, transport layer, network layer and link layer.. Network Topology: NodeAddr Clas
Trang 1NS-2 Tutorial
Trang 2Motivation
Trang 3What’s 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
Trang 4Model 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 =>
no locking or race conditions to worry about (very
easy)
Discrete Event Simulation
Trang 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
Trang 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++
Trang 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
Trang 8OTcl and C++: The Duality
Pure C++
objects
Pure OTcl objects
C++/OTcl split objects Split Object:
Object created in Otcl has a corresponding object in C++.
Trang 10Network Topology: Node
Addr Classifier
Port Classifier
Multicast Classifier
multiclassifier_
Trang 11Network Topology: Link
duplex link
Trang 12Routing
Addr Classifier
Port Classifier
Trang 13dmux_
entry_
0 1
Addr Classifier
Port Classifier
classifier_
dmux_ entry_
1 0 Link n0-n1
Link n1-n0
Trang 140 1
Addr
Classifier
Port Classifier
classifier_
dmux_
entry_
0 Agent/TCPagents_
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
1 0
Link n0-n1
Link n1-n0
0 Agent/TCPSin
k agents_
Trang 15Application: Traffic Generator
0 1
Addr
Classifier
Port Classifier
classifier_
dmux_
entry_
0 Agent/TCPagents_
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
1 0
Link n0-n1
Link n1-n0
0 Agent/TCPSinkagents_
Application/FTP
Trang 16Plumbing: Packet Flow
0 1
Addr Classifier
Port Classifier
entry_
0 Agent/TCP ClassifierAddr
Port Classifier
entry_
1 0
Trang 17Running NS-2 Program
Trang 18• 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
– documentation.html
Trang 20Basic Syntax
Tcl commands are evaluated in two steps:
First the Tcl interpreter parses the commands into words, performing
substitution along the way.
Trang 21Tcl Basics
• Basic syntax
– command arg1 arg2 arg3 …
– command is either the name of a built-in command
Trang 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
Trang 24Variable Substitution
• Variable substitution
– i: the character ‘i’
– $i: the variable i
– set v1 6
– set v2 $v1 (variable substitution)
Trang 27• 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:
• => The length of hello is 5
• =>The length of $a is [string length $a]
Trang 29set d [expr [expr $a - $b] * $c]
for {set k 0} {$k < 10} {incr k} {
Trang 31OTcl Basics
• Creating a class
• Defining instance procedures
– class_name instproc proc_name {args} {… }
• Defining instance variables
Trang 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]
Trang 33Class 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: What’s 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?
Trang 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:
Trang 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/lib
Trang 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
Trang 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/bin:$PATH
– If you are using sh or bash, you can set it like: export PATH=/usr/ns/bin:$PATH
Trang 38Elements of ns-2 Simulation
• Step 1: Create the event scheduler
• Step 2: Turn on tracing
• Step 3:Create network topology
• Step 4: Create transport connection
• Step 5: Create traffic on top of transport connection
Trang 39Topology
Trang 40Step 1: Creating Event Scheduler
• Create event scheduler
– set ns [new Simulator]
Trang 41Step 2: Tracing
• Trace packets on all links
– $ns trace-all [open test.out w]
<event> <time> <from> <to> <pkt> <size> <fid> <src> <dst> <seq> <attr>
+ 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
Trang 43Step 3: Creating Network
• Nodes
– set n0 [$ns node]
– set n1 [$ns node]
• Links and queuing
– $ns duplex-link $n0 $n1 <bandwidth> <delay>
<queue_type>
– <queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR
Trang 44#Create 6 nodes
for {set i 0} {$i < 6} {incr i} {
set n($i) [$ns node]
Trang 45Step 4: Creat transport conneciton
TCP
• TCP
#Create a TCP agent and attach it to node n(0)
#set up connection between node 0 and node 3set 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
Trang 46Step 4: Creat transport conneciton
UDP
• UDP
# UDP flow between n1 and n7
set src_udp [new Agent/UDP]
Trang 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
Trang 48Creating Traffic: (On Top of TCP)
• FTP
set ftp [new Application/FTP]
$ftp attach-agent $src_tcp
$ftp set packetSize_ 512
Trang 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