1
Ns Tutorial
Alhussein A. AbouzeidElect. Eng. Dept./U. of Washington
Parts of this educational presentation use examples available from http://www.isi.edu/nsnam/ns/ns-documentation.html
2
Tutorial Goals Using NS (the Otcl world)
Complete examples of its usage Wired Wireless
Extending NS (the C++ world) Implementing new functionality
3
Schedule Day 1: Wired simulations
Fundamentals Supporting tools
Day 2: Wireless version of Day 1 Day 3: Extending ns capability
ns internals Extending ns Lab Assignments
4
Overview What is ns? (the evolution) Status of the code Resources Documentation Remark!
5
What is ns? A discrete event, packet-level simulator Targeted at networking research Supports the simulation of
intserv/diffserv, Multicast, Transport, Applications, Wireless (fixed, mobile, satellite)
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, Me!
6
Status ns-2
100K lines of C++ 70K lines of OTcl 50K+ lines of test suite, examples, docs
Platforms Most UNIX and UNIX-like systems (FreeBSD,
Linux, Sun Solaris) Window 95/98/NT (rocky) (Emulation only for FreeBSD for now)
7
Resources http://www.isi.edu/nsnam/ns Installed on all machines in High
Throughput Lab (EE/CSE 365)add to .cshrc: setenv TCL_LIBRARY
/condor/ns-allinone-2.1b7a/tcl8.3.2 [email protected]
[email protected] “subscribe ns-users” in body Archive: http://www.isi.edu/nsnam/ns
8
Documentation 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/~salehi/ns_doc.ps.gz
9
Remember! A simulator model of a real-world system
is necessarily a simplification. For example, in simulating TCP No SYN/FIN Equal size segments No variable window advertisement
Bugs: “Users of ns are responsible for verifying for themselves that their simulations are not invalidated by bugs”.
10
Day 1 Schedule Architecture Basic Tcl/Otcl commands Elements of an ns-2 simulation
Create the event scheduler [Turn on tracing] Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data
Example: Multicast routing nam Visualization
11
Architecture: Object-Oriented C++ for “data”
Per packet action OTcl for control
Periodic or triggered action Modularity (+), re-usability(+),
scalability(+) Speed(-), memory(-)
12
OTcl and C++: The Duality
C++ OTcl
Pure C++objects
Pure OTclobjects
C++/OTcl split objects
ns
13
Script in interactive modelinux21% ns
% set ns [new Simulator]
_o3
% $ns at 1 “puts \“Hello World!\””
1
% $ns at 1.5 “exit”
2
% $ns run
Hello World!
linux21%
14
A script in batch mode
#simple.tclset ns [new Simulator]
$ns at 1 “puts \“Hello World!\””
$ns at 1.5 “exit”
$ns run
linux21% ns simple.tcl
Hello World!
linux21%
15
Basic Tclset a 43set b 27proc test { a b } {
set c [expr $a + $b]set d [expr [expr $a - $b] * $c]for {set k 0} {$k < 10} {incr k} {
if {$k < 5} { puts “k < 5, pow = [expr pow($d, $k)]” } else { puts “k >= 5, mod = [expr $d % $k]” } }}test 43 27
16
Basic OTclClass MomMom instproc greet {} {
$self instvar age_puts “$age_ years old mom: How are you doing?”
}
Class Kid -superclass MomKid 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
17
Elements of ns-2 Simulation Create the event scheduler [Turn on tracing] Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data
18
Creating Event Scheduler Create event scheduler
set ns [new Simulator] Schedule events
$ns at <time> <event> <event>: any legitimate ns/tcl
commands Start scheduler
$ns run
19
Tracing
Trace packets on all links $ns trace-all [open test.out w]
<event> <time> <from> <to> <pkt> <size> -- <fid> <src> <dst> <seq> <attr><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- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0r 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
20
Tracing Turn on tracing on specific links
$ns trace-queue $n0 $n1 $ns namtrace-queue $n0 $n1
21
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
22
Creating Network: LAN LAN
$ns make-lan <node_list> <bandwidth> <delay> <ll_type> <ifq_type> <mac_type> <channel_type>
<ll_type>: LL <ifq_type>: Queue/DropTail, <mac_type>: MAC/802_3 <channel_type>: Channel
23
Inserting Packet Errors Creating Error Module
set loss_module [new ErrorModel] $loss_module set rate_ 0.01 $loss_module unit pkt $loss_module ranvar [new
RandomVariable/Uniform] $loss_module drop-target [new
Agent/Null] Inserting Error Module
$ns lossmodel $loss_module $n0 $n1
24
Network Dynamics Link failures
Hooks in routing module to reflect routing changes
Four models$ns rtmodel-at <time> up|down $n0 $n1$ns rtmodel-at <time> up|down $n0 $n1$ns rtmodel Trace <config_file> $n0 $n1$ns rtmodel Trace <config_file> $n0 $n1$ns rtmodel Exponential {<params>} $n0 $n1$ns rtmodel Exponential {<params>} $n0 $n1$ns rtmodel Deterministic {<params>} $n0 $n1$ns rtmodel Deterministic {<params>} $n0 $n1
Parameter list[<start>] <up_interval> <down_interval> [<finish>][<start>] <up_interval> <down_interval> [<finish>]
25
Setup Routing Unicast
$ns rtproto <type> <type>: Static, Session, DV, cost, multi-path
Multicast $ns multicast (right after [new Simulator])
or set ns [new Simulator –multicast on] $ns mrtproto <type> <type>: CtrMcast, DM, ST, BST
(centralized,dense mode, shared tree
26
Creating Connection: UDP UDP
set udp [new Agent/UDP] set null [new Agent/Null] $ns attach-agent $n0 $udp $ns attach-agent $n1 $null $ns connect $udp $null
27
Creating Traffic: On Top of UDP CBR
set src [new Application/Traffic/CBR] Exponential or Pareto on-off
set src [new Application/Traffic/Exponential]
set src [new Application/Traffic/Pareto]
28
Creating Connection: TCP TCP
set tcp [new Agent/TCP] set tcpsink [new Agent/TCPSink] $ns attach-agent $n0 $tcp $ns attach-agent $n1 $tcpsink $ns connect $tcp $tcpsink
29
Creating Traffic: On Top of TCP FTP
set ftp [new Application/FTP] $ftp attach-agent $tcp
Telnet set telnet [new Application/Telnet] $telnet attach-agent $tcp
30
Creating Traffic: Trace Driven Trace driven
set tfile [new Tracefile] $tfile filename <file> set src [new Application/Traffic/Trace] $src attach-tracefile $tfile
<file>: Binary format (native!) inter-packet time (msec) and packet size
(byte)
31
Application-Level Simulation Features
Build on top of existing transport protocol
Transmit user data, e.g., HTTP header Two different solutions
TCP: Application/TcpApp UDP: Agent/Message
32
Summary: Generic Script Structure
set ns [new Simulator]set ns [new Simulator]
# [Turn on tracing]# [Turn on tracing]
# Create topology# Create topology
# Setup packet loss, link dynamics# Setup packet loss, link dynamics
# 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-processing procs# Post-processing procs
# Start simulation# Start simulation
33
G2time1.3s
G2time1.2s
G1G2
time1.35s
Example: Multicast Routing Dynamic group membership
n0 n1
n2
n3
1.5Mb, 10ms
1.5Mb, 10ms
G1time1.25s
G2
1.5Mb, 10ms
34
Multicast: Step 1
Scheduler, tracing, and topology
# Create scheduler# Create scheduler
set ns [new Simulator]set ns [new Simulator]
# Turn on multicast# Turn on multicast
$ns multicast$ns multicast
# Turn on Tracing# Turn on Tracing
set fd [new “mcast.nam” w]set fd [new “mcast.nam” w]
$ns namtrace-all $fd$ns namtrace-all $fd
35
Multicast: Step 2
Topology
# Create nodes# Create nodes
set n0 [$ns node]set n0 [$ns node]
set n1 [$ns node]set n1 [$ns node]
set n2 [$ns node]set n2 [$ns node]
set n3 [$ns node]set n3 [$ns node]
# Create links# Create links
$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail
$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n0 $n3 1.5Mb 10ms DropTail$ns duplex-link $n0 $n3 1.5Mb 10ms DropTail
36
Multicast: Step 3 Routing and group setup
# Routing protocol: let’s run distance vector# Routing protocol: let’s run distance vector
$ns mrtproto DM$ns mrtproto DM
# Allocate group addresses# Allocate group addresses
set group1 [Node allocaddr]set group1 [Node allocaddr]
set group2 [Node allocaddr]set group2 [Node allocaddr]
37
Multicast: Step 4 Sender 0
# Transport agent for the traffic source# Transport agent for the traffic sourceset set udp0udp0 [new Agent/UDP] [new Agent/UDP]$ns attach-agent $$ns attach-agent $n1n1 $ $udp0udp0$$udp0udp0 set dst_addr_ $ set dst_addr_ $group1group1$$udp0udp0 set dst_port_ 0 set dst_port_ 0
# Constant Bit Rate source #0 # Constant Bit Rate source #0 set set cbr0cbr0 [new Application/Traffic/CBR] [new Application/Traffic/CBR]$$cbr0cbr0 attach-agent $ attach-agent $udp0udp0# Start at time 1.0 second# Start at time 1.0 second$ns at 1.0 "$$ns at 1.0 "$cbr0cbr0 start" start"
38
Multicast: Step 5 Sender 1
# Transport agent for the traffic source# Transport agent for the traffic sourceset set udp1udp1 [new Agent/UDP] [new Agent/UDP]$ns attach-agent $$ns attach-agent $n3n3 $ $udp1udp1$$udp1udp1 set dst_addr_ $ set dst_addr_ $group2group2$$udp1udp1 set dst_port_ 0 set dst_port_ 0
# Constant Bit Rate source #0 # Constant Bit Rate source #0 set set cbr1cbr1 [new Application/Traffic/CBR] [new Application/Traffic/CBR]$$cbr1cbr1 attach-agent $ attach-agent $udp1udp1# Start at time 1.1 second# Start at time 1.1 second$ns at 1.1 "$$ns at 1.1 "$cbr1cbr1 start" start"
39
Multicast: Step 6 Receiver with dynamic membership
# Can also be Agent/Null# Can also be Agent/Null
set rcvr [new Agent/LossMonitor]set rcvr [new Agent/LossMonitor]
# Assign it to node $n2# Assign it to node $n2
$ns at $ns at 1.21.2 "$n2 join-group $rcvr $ "$n2 join-group $rcvr $group2group2""
$ns at $ns at 1.251.25 "$n2 leave-group $rcvr $ "$n2 leave-group $rcvr $group2group2""
$ns at $ns at 1.31.3 "$n2 join-group $rcvr $ "$n2 join-group $rcvr $group2group2""
$ns at $ns at 1.351.35 "$n2 join-group $rcvr $ "$n2 join-group $rcvr $group1group1""
40
Multicast: Step 7 End-of-simulation wrapper (as usual)
$ns at 2.0 "finish"$ns at 2.0 "finish"proc finish {} {proc finish {} {
global ns fdglobal ns fdclose $fdclose $fd$ns flush-trace$ns flush-traceputs "running nam..."puts "running nam..."exec nam out.nam &exec nam out.nam &exit 0exit 0
}}$ns run$ns run