11
NS Fundamentals
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
2
OTcl and C++: The Duality
C++
OTcl
Pure C++
objects
Pure OTcl
objects
C++/OTcl split objects
ns
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
3
NS input & output
 
C:\CS591\Presentation\view.gif
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
4
Basic Tcl
Defining a variable.
set var1 1set var2 “Hello“
Variable Reference
puts "var1=$var1, var2=$var2"
Assign the results of a function
set var3 [expr 5*10]
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
5
Basic Tcl
For Loop
for {set i 0} {$i < 100} {incr i} { puts “I = $i "
}
While Loop
   set i 0while {$i < 10} {    set n($i) [new Node]    incr i}
Procedure
proc proc1 {}  {    puts "in procedure proc1"}
 proc1
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
6
Basic OTcl
Rectangle: Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
Class Mom
Mom instproc greet {} {
$self instvar age_
puts “$age_ years oldmom: How are you doing?”
}
Class Kid -superclass Mom
Kid instproc greet {} {
$self instvar age_
puts “$age_ years oldkid: What’s up, dude?”
}
Rectangle: Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
set mom [new Mom]
$mom set age_ 45
set kid [new Kid]
$kid set age_ 15
$mom greet
$kid greet
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
7
Simple Simulation Example
 
C:\CS591\fig4.gif
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
8
Summary: Generic Script Structure
set ns [new Simulator]set ns [new Simulator]
[Turn on tracing][Turn on tracing]
Create topologyCreate topology
Setup packet loss, link dynamicsSetup packet loss, link dynamics
Create routing agentsCreate 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 procsPost-processing procs
Start simulationStart simulation
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
9
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
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
10
Tracing
Trace packets on all links
$ns trace-all [open out.tr w]
+ 0.112731 1 3 tcp 512 ------- 0 0.1 3.1 0 0
- 0.112731 1 3 tcp 512 ------- 0 0.1 3.1 0 0
r 0.125461 1 3 tcp 512 ------- 0 0.1 3.1 0 0
  
C:\CS591\fig13.gif
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
11
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
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
12
Setup Routing
Unicast
$ns rtproto <type>
<type>: Static, Session, DV, cost, multi-path
Multicast
$ns multicast (right after [new Simulator])
$ns mrtproto <type>
<type>: CtrMcast, DM, ST, BST
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
13
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
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
14
Creating Traffic: On Top of UDP
CBR
set src [new Application/Traffic/CBR]
Exponential or Pareto on-off
set src [newApplication/Traffic/Exponential]
set src [new Application/Traffic/Pareto]
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
15
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
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
16
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
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
17
Simple Simulation Example
 
C:\CS591\fig4.gif
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
18
TCL Script  Step 1
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
 
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the All trace file
Set f [open out.tr w]
$ns trace-all $f
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
19
TCL Script  Step 2
#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
 
#Create links between the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
20
TCL Script Step 3
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
 
#Give node position (for NAM)
$ns duplex-link-op $n0 $n2 orient right-down
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
 
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
 
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
21
TCL Script Step 4
#Setup a TCP connection
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
 
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
22
TCL Script Step 5
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
 
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
 
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
23
TCL Script Step 6
#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
 
#Call the finish procedure after 5 seconds ofsimulation time
$ns at 5.0 "finish"
 
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
 
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
24
TCL Script Step 7
#Define a 'finish' procedure
proc finish {} {
        global ns nf
        $ns flush-trace
        #Close the NAM trace file
        close $nf
        #Execute NAM on the trace file
        exec nam out.nam &
        exit 0
}
 
#Run the simulation
$ns run
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
25
NS Directory map
C:\CS591\fig17.gif
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
26
ns Primer – Wired World
Basic ns
A complete example
Multicast routing
Visualization
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
27
G2
time
1.3s
G2
time
1.2s
G1
G2
time
1.35s
Example: Multicast Routing
Dynamic group membership underDense Mode
n0
n1
n2
n3
1.5Mb, 10ms
1.5Mb, 10ms
G1
time
1.25s
G2
1.5Mb, 10ms
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
28
Multicast: Step 1
Scheduler, tracing, and topology
Create schedulerCreate scheduler
set ns [new Simulator]set ns [new Simulator]
Turn on multicastTurn on multicast
$ns multicast$ns multicast
Turn on TracingTurn on Tracing
set fd [new “mcast.nam” w]set fd [new “mcast.nam” w]
$ns namtrace-all $fd$ns namtrace-all $fd
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
29
Multicast: Step 2
Topology
Create nodesCreate 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 linksCreate 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
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
30
Multicast: Step 3
Routing and group setup
Routing protocol: let’s run distance vectorRouting protocol: let’s run distance vector
$ns mrtproto DM$ns mrtproto DM
Allocate group addressesAllocate group addresses
set group1 [Node allocaddr]set group1 [Node allocaddr]
set group2 [Node allocaddr]set group2 [Node allocaddr]
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
31
Multicast: Step 4
Sender 0
Transport agent for the traffic sourceTransport agent for the traffic source
set udp0 [new Agent/UDP]set udp0 [new Agent/UDP]
$ns attach-agent $n1 $udp0$ns attach-agent $n1 $udp0
$udp0 set dst_addr_ $group1$udp0 set dst_addr_ $group1
$udp0 set dst_port_ 0$udp0 set dst_port_ 0
Constant Bit Rate source #0Constant Bit Rate source #0
set cbr0 [new Application/Traffic/CBR]set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0$cbr0 attach-agent $udp0
Start at time 1.0 secondStart at time 1.0 second
$ns at 1.0 "$cbr0 start"$ns at 1.0 "$cbr0 start"
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
32
Multicast: Step 5
Sender 1
Transport agent for the traffic sourceTransport agent for the traffic source
set udp1 [new Agent/UDP]set udp1 [new Agent/UDP]
$ns attach-agent $n3 $udp1$ns attach-agent $n3 $udp1
$udp1 set dst_addr_ $group2$udp1 set dst_addr_ $group2
$udp1 set dst_port_ 0$udp1 set dst_port_ 0
Constant Bit Rate source #0Constant Bit Rate source #0
set cbr1 [new Application/Traffic/CBR]set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1$cbr1 attach-agent $udp1
Start at time 1.1 secondStart at time 1.1 second
$ns at 1.1 "$cbr1 start"$ns at 1.1 "$cbr1 start"
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
33
Multicast: Step 6
Receiver with dynamic membership
Can also be Agent/NullCan also be Agent/Null
set rcvr [new Agent/LossMonitor]set rcvr [new Agent/LossMonitor]
Assign it to node $n2Assign it to node $n2
$ns at 1.2 "$n2 join-group $rcvr $group2"$ns at 1.2 "$n2 join-group $rcvr $group2"
$ns at 1.25 "$n2 leave-group $rcvr $group2"$ns at 1.25 "$n2 leave-group $rcvr $group2"
$ns at 1.3 "$n2 join-group $rcvr $group2"$ns at 1.3 "$n2 join-group $rcvr $group2"
$ns at 1.35 "$n2 join-group $rcvr $group1"$ns at 1.35 "$n2 join-group $rcvr $group1"
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
34
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 fd
close $fdclose $fd
$ns flush-trace$ns flush-trace
puts "running nam..."puts "running nam..."
exec nam out.nam &exec nam out.nam &
exit 0exit 0
}}
$ns run$ns run
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
35
ns Primer – Wired World
Basic ns
Two examples
TCP, multicast routing
Visualization
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
36
Visualization Tools
nam-1 (Network AniMator Version 1)
Packet-level animation
Well supported by ns
xgraph
Conversion from ns trace to xgraph format
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
37
nam Interface: Color
Color mapping
$ns color 40 red$ns color 40 red
$ns color 41 blue$ns color 41 blue
$ns color 42 chocolate$ns color 42 chocolate
Color  flow id association
$tcp0 set fid_ 40;# red packets$tcp0 set fid_ 40;# red packets
$tcp1 set fid_ 41;# blue packets$tcp1 set fid_ 41;# blue packets
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
38
Multicast:
Define nam color
Colors for packets from two mcast groupsColors for packets from two mcast groups
$ns color 10 blue$ns color 10 blue
$ns color 11 red$ns color 11 red
Prune packets (predefined)Prune packets (predefined)
$ns color 30 purple$ns color 30 purple
Graft packetsGraft packets
$ns color 31 green$ns color 31 green
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
39
Tracefile for cwnd, rtt
# trace file for RTT
set f3 [open rtt.tr w]
set tcp0 [new Agent/TCP]
$tcp0 attach $f3
$tcp0 trace rtt_
# trace file for Congestion Window Size
set f4 [open cwnd.tr w]
set tcp0 [new Agent/TCP]
$tcp0 attach $f4
$tcp0 trace cwnd_
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
40
GNUPLOT
 #simple.gnu
set term x11
set xrange [0:200]
set yrange [0:0.4]
set xlabel 'Time (s)'
set ylabel 'Throughput (Mbit/s)'
plot 'blm.tr' title 'blm' with l 1, 'tcp.tr' title 'tcp' with l 5
set term postscript
set output 'blm.ps'
rep
 %gnuplot simple.gnu
60%
USC  INFORMATION SCIENCES INSTITUTEUSC  INFORMATION SCIENCES INSTITUTE
41
C:\CS591\blm