Vous êtes sur la page 1sur 39

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

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)

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 Ad hoc routing and mobile IP Directed diffucion, sensor-MAC

Wireless

Tracing, visulization, various utilities

Simplified Users View of NS

OTcl and C++: The Duality

Architectural View of NS

<user>

Basic Tcl
variables: set x 10 puts x is $x functions and expressions: set y [pow x 2] set y [expr x*x]

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.

control flow: if {$x > 0} { return $x } else { return [expr -$x] } while { $x > 0 } { puts $x incr x 1 }

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 above test

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

A Sample OTcl Script


# Create a class call "mom" and # add a member function call "greet" Class mom mom 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 mom kid instproc greet {} { $self instvar age_ puts "$age_ years old kid say: What's up, dude?" } # Create a mom and a kid object set each age set a [new mom] $a set age_ 45 set 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: Whats up dude?

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.tcl


set ns [new Simulator] $ns at 1 puts \Hello World!\ $ns at 1.5 exit $ns run

sandra% ns simple.tcl Hello World! sandra%

The first NS script


set ns [new Simulator] set nf [open out.nam w] $ns namtrace-all $nf # 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 proc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & $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

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 and administer traffic


$ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop"

$ns at 5.0 "finish" $ns run

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

A more complicated script (2)


# Create a CBR traffic source and attach it to udp1 #Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1 # 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 set null0 [new Agent/Null] $ns attach-agent $n3 $null0

$cbr0 attach-agent $udp0

$ns connect $udp0 $null0


$ns connect $udp1 $null0

#Create a UDP agent and attach it to node n1 set udp1 [new Agent/UDP] $ns attach-agent $n1 $udp1 $ns at 0.5 "$cbr0 start" $ns at 1.0 "$cbr1 start" $ns at 4.0 "$cbr1 stop" $ns at 4.5 "$cbr0 stop"

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

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

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

A real Simulation 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 }

Real Simulation Script (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 #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

real Simulation Script (3)


#Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $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 $ftp set type_ FTP

real Simulation Script (4)


#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 type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false

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 interval puts "CBR packet size = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]" #Run the simulation $ns run

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"

Event Scheduler

Simulator-Scheduler Functions

Simulator instproc now

# return scheduler's notion of current time # schedule execution of code at specified time

Simulator instproc at args

Simulator instproc at-now args

# schedule execution of code at now


# schedule execution of code after n secs # start scheduler # stop (pause) scheduler

Simulator instproc after n args

Simulator instproc run args

Simulator instproc halt

Class Hierarchy (partial)

Unicast and Multicast Nodes

Links

Tracing

Queue Monitor

Packet Flow Example

NS Packet Format

Post Simulation Tasks


Trace Analysis Simulation Data Animation

Understanding a Trace File

LAN 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) set opt(namtr) set opt(seed) set opt(stop) set opt(node) set opt(qsize) set opt(bw) set opt(delay) set opt(ll) set opt(ifq) set opt(mac) set opt(chan) set opt(tcp) set opt(sink) "out.tr" "out.nam" 0 5 8 100 10Mb 1ms LL Queue/DropTail Mac/Csma/Ca Channel TCP/Reno TCPSink proc create-trace {} { global ns opt set trfd [open $opt(tr) w] $ns trace-all $trfd return $trfd } proc create-namtrace {} { global ns opt set ntrfd [open $opt(namtr) w] $ns namtrace-all $ntrfd } proc create-topology {} { global ns opt global 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

set opt(app)

FTP

proc finish {} { global ns opt trfd ntrfd $ns flush-trace close $trfd close $ntrfd exec nam $opt(namtr) & exit 0

}
}

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

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

Basic Script Structure


set ns [new Simulator] # [Turn on tracing] # Create topology # Setup packet loss, link dynamics # Create routing agents # Create: # - multicast groups # - protocol agents # - application and/or setup traffic sources # Post-processing procs # Start simulation

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

Vous aimerez peut-être aussi