NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

39
NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος

Transcript of NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Page 1: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

NS-2 (network simulator)

NS by example

παρουσίαση Κων/νος Τρούλος

Page 2: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Σκοπός του Μαθήματος

How the simulator works How to setup simulation networks Where to look for further information How to create new network components

Simple examples + brief explanations => get started quickly

Page 3: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Overview (1)

NS (version 2) Object-oriented Discrete event driven network simulator Written in C++/OTcl LAN/WAN simulation Emulation Analysis/animation results (e.g. NAM)

Page 4: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Overview (2)

Wired world Routing (ip, multicast routing) Transportation: TCP και UDP, multicast transport Traffic sources: web, ftp, telnet, CBR, stochastic Queuing disciplines: drop-tail, RED, FQ, SFQ QoS: IntServ και Diffserv

Wireless Ad hoc routing and mobile IP Directed diffucion, sensor-MAC

Tracing, visulization, various utilities

Page 5: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Simplified User’s View of NS

Page 6: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

OTcl and C++: The Duality

Page 7: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Architectural View of NS

<user>

Page 8: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Basic Tcl

variables:set x 10puts “x is $x”

functions and expressions: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 $xincr x –1}

procedures:proc pow {x n} {if {$n == 1} { return $x }set part [pow x [expr $n-1]]return [expr $x*$part]}

χρήση όλων των γνωστών προγραμματιστικών τεχνικών. Μπορεί να χρησιμοποιηθεί για την κατασκευη διαφόρων network topologies και traffic models.

Page 9: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

A Sample Tcl Script# Writing a procedure called "test"proc test {} { set a 43 set b 27 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]"}

}}

# Calling the "test" procedure created abovetest

Sandra:> ns ex-tcl.tcl K < 5, pow = 1.0K < 5, pow = 1120.0K < 5, pow = 1254400.0K < 5, pow = 1404928000.0K < 5, pow = 1573519360000.0K >= 5, mod = 0K >= 5, mod = 4K >= 5, mod = 0K >= 5, mod = 0K >= 5, mod = 4

Page 10: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

A Sample OTcl Script# Create a class call "mom" and # add a member function call "greet"Class mommom instproc greet {} { $self instvar age_ puts "$age_ years old mom say: How are you doing?"}

# Create a child class of "mom" called "kid" # and overide the member function "greet"Class kid -superclass momkid instproc greet {} { $self instvar age_ puts "$age_ years old kid say: What's up, dude?"}

# Create a mom and a kid object set each ageset a [new mom]$a set age_ 45set b [new kid]$b set age_ 15

# Calling member function "greet" of each object$a greet$b greet

Sandra:> ns ex-otcl.tcl 45 year old mom say:

How are you doing?15 year old kid say:

What’s up dude?

Page 11: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

“Hello World”

Interactive mode:sandra% ns

% set ns [new Simulator]

_o3

% $ns at 1 “puts \“Hello World!\””

1

% $ns at 1.5 “exit”

2

% $ns run

Hello World!

sandra%

Batch mode:

simple.tclset ns [new Simulator]

$ns at 1 “puts \“Hello World!\””

$ns at 1.5 “exit”

$ns run

sandra% ns simple.tcl

Hello World!

sandra%

Page 12: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

The first NS script

set ns [new Simulator]

set nf [open out.nam w]

$ns namtrace-all $nf

proc finish {} {

global ns nf

$ns flush-trace

close $nf

exec nam out.nam &

exit 0

}

#Create nodes and link

set n0 [$ns node]

set n1 [$ns node]

$ns duplex-link $n0 $n1 1Mb 10ms DropTail

#Create a UDP agent and attach it to node n0

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

#Create null agent, connect it with UDP agent

set null0 [new Agent/Null]

$ns attach-agent $n1 $null0

$ns connect $udp0 $null0

#Create and administer traffic

$ns at 0.5 "$cbr0 start"

$ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish"

$ns run

Page 13: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

A bit more complicated script

set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

$ns duplex-link $n0 $n2 1Mb 10ms DropTail

$ns duplex-link $n1 $n2 1Mb 10ms DropTail

$ns duplex-link $n3 $n2 1Mb 10ms DropTail

$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

Page 14: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

A more complicated script (2)

#Create a UDP agent and attach it to node n0

set udp0 [new Agent/UDP]

$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500

$cbr0 set interval_ 0.005

$cbr0 attach-agent $udp0

#Create a UDP agent and attach it to node n1

set udp1 [new Agent/UDP]

$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1

set cbr1 [new Application/Traffic/CBR]

$cbr1 set packetSize_ 500

$cbr1 set interval_ 0.005

$cbr1 attach-agent $udp1

set null0 [new Agent/Null]

$ns attach-agent $n3 $null0

$ns connect $udp0 $null0

$ns connect $udp1 $null0

$ns at 0.5 "$cbr0 start"

$ns at 1.0 "$cbr1 start"

$ns at 4.0 "$cbr1 stop"

$ns at 4.5 "$cbr0 stop"

Page 15: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

A more complicated script (3)

#Additions to the CBR flows definition

$ns color 1 Blue

$ns color 2 Red

$udp0 set class_ 1

$udp1 set class_ 2

Page 16: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Queue management

$ns duplex-link-op $n2 $n3 queuePos 0.5

$ns duplex-link $n3 $n2 1Mb 10ms SFQ

Page 17: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

A “real” Simulation Example

Page 18: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

“real” Simulation Script (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

#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

}

Page 19: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

“Real” Simulation Script (2)#Create four nodesset 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

#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

Page 20: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

“real” Simulation Script (3)#Setup a TCP connectionset tcp [new Agent/TCP]$tcp set class_ 2$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n3 $sink$ns connect $tcp $sink$tcp set fid_ 1

#Setup a FTP over TCP connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP

Page 21: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

“real” Simulation Script (4)#Setup a UDP connectionset udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n3 $null$ns connect $udp $null$udp set fid_ 2

#Setup a CBR over UDP connectionset cbr [new Application/Traffic/CBR]$cbr attach-agent $udp$cbr set type_ CBR$cbr set packet_size_ 1000$cbr set rate_ 1mb$cbr set random_ false

Page 22: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

“Real” Simulation Script (5)#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"

#Detach tcp and sink agents (not really necessary)$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"

#Call the finish procedure after 5 seconds of simulation time$ns at 5.0 "finish"

#Print CBR packet size and intervalputs "CBR packet size = [$cbr set packet_size_]"puts "CBR interval = [$cbr set interval_]"

#Run the simulation$ns run

Page 23: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Basic Simulation Commands

set ns [new Simulator]

$ns color fid color

$ns namtrace-all file-descriptor

proc finish {}

set n0 [$ns node]

$ns duplex-link node1 node2 bandwidth delay queue-type

$ns queue-limit node1 node2 number

$ns duplex-link-op node1 node2 ...

set tcp [new Agent/TCP]

$ns attach-agent node agent

$ns connect agent1 agent2

$ns at time "string"

Page 24: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Event Scheduler

Page 25: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Simulator-Scheduler Functions Simulator instproc now 

# return scheduler's notion of current time Simulator instproc at args 

# schedule execution of code at specified time Simulator instproc at-now args 

# schedule execution of code at now Simulator instproc after n args 

# schedule execution of code after n secs Simulator instproc run args 

# start scheduler Simulator instproc halt

# stop (pause) scheduler

Page 26: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Class Hierarchy (partial)

Page 27: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Unicast and Multicast Nodes

Page 28: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Links

Page 29: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Tracing

Page 30: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Queue Monitor

Page 31: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Packet Flow Example

Page 32: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

NS Packet Format

Page 33: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Post Simulation Tasks

Trace Analysis Simulation Data

Animation

Page 34: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Understanding a Trace File

Page 35: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

LAN Example

Page 36: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

LAN Example Script (1)# Author: Jae Chung# Date: 7/20/99## This file is modified from # "ns-2/tcl/ex/lantest.tcl"

set opt(tr) "out.tr"set opt(namtr) "out.nam"set opt(seed) 0set opt(stop) 5set opt(node) 8

set opt(qsize) 100set opt(bw) 10Mbset opt(delay) 1msset opt(ll) LLset opt(ifq) Queue/DropTailset opt(mac) Mac/Csma/Caset opt(chan) Channelset opt(tcp) TCP/Renoset opt(sink) TCPSink

set opt(app) FTP

proc finish {} {global ns opt trfd ntrfd

$ns flush-traceclose $trfd

close $ntrfdexec nam $opt(namtr) &exit 0

}

proc create-trace {} {global ns opt

set trfd [open $opt(tr) w]$ns trace-all $trfdreturn $trfd

}

proc create-namtrace {} { global ns opt

set ntrfd [open $opt(namtr) w] $ns namtrace-all $ntrfd}

proc create-topology {} {global ns optglobal lan node source node0

set num $opt(node)for {set i 0} {$i < $num} {incr i} {

set node($i) [$ns node]lappend nodelist $node($i)

}

set lan [$ns newLan $nodelist $opt(bw) $opt(delay) \-llType $opt(ll) -ifqType

$opt(ifq) \-macType $opt(mac) -chanType

$opt(chan)]

set node0 [$ns node]$ns duplex-link $node0 $node(0) 2Mb 2ms DropTail

$ns duplex-link-op $node0 $node(0) orient right}

Page 37: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

LAN Example Script (2)## MAIN ##

set ns [new Simulator]set trfd [create-trace]set ntrfd [create-namtrace]

create-topology

set tcp0 [$ns create-connection TCP/Reno $node0 TCPSink $node(7) 0]$tcp0 set window_ 15

set ftp0 [$tcp0 attach-app FTP]

$ns at 0.0 "$ftp0 start"$ns at $opt(stop) "finish"

$ns run

Page 38: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Basic 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

Page 39: NS-2 (network simulator) NS by example παρουσίαση Κων/νος Τρούλος.

Περισσότερες Πληροφορίες

http://www.isi.edu/nsnam/ns http://www.isi.edu/nsnam/ns/ns-tutorial/index.html http://www.isi.edu/nsnam/vint http://sandra.netmode.ntua.gr http://www.isi.edu/nsnam/ns/edu/index.html