NS2 Shahin Shayandeh December 2004 Session 1 Ns2
NS-2 Shahin Shayandeh December 2004 Session 1
Ns-2, the Network Simulator o A discrete event simulator n o Simple model Focused on modeling network protocols n n n Wired, wireless, satellite Network protocols o TCP, UDP, multicast, unicast Traffic source behavior o Web, telnet, ftp, CBR, VBR Router queue management mechanism o Drop Tail, RED, CBQ Ad hoc routing, sensor networks Infrastructure: stats, tracing, error models, etc
Simplified User's View of NS
Discrete Event Simulation o Model world as events n n n o o o Simulator has list of events 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) Packet-handling delay Timers
Discrete Event Example simple queuing model: Consider two nodes on an Ethernet: A t=1, A enqueues pkt on LAN t=1. 01, LAN dequeues pkt and triggers B B
Ns Architecture o o Object-oriented (C++, OTcl) Modular approach n + + – – Fine-grained object composition Reusability Maintenance Performance (speed and memory) Careful planning of modularity
C++ and OTcl Separation o “data” / control separation n n + – C++ for “data”: o per packet processing, core of ns o fast to run, detailed, complete control OTcl for control: o Simulation scenario configurations o Periodic or triggered action o Manipulating existing C++ objects o fast to write and change running vs. writing speed Learning and debugging (two languages)
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] } control flow: if {$x > 0} { return $x } else { return [expr -$x] } while { $x > 0 } { puts $x incr x – 1 } Also lists, associative arrays, etc. => can use a real programming language to build network topologies, traffic models, etc.
Basic otcl Class Person # constructor: Person instproc init {age} { $self instvar age_ set age_ $age } # method: Person instproc greet {} { $self instvar age_ puts “$age_ years old: How are you doing? ” } # subclass: Class Kid -superclass Person Kid instproc greet {} { $self instvar age_ puts “$age_ years old kid: What’s up, dude? ” } set a [new Person 45] set b [new Kid 15] $a greet $b greet
Using ns Problem Result analysis Simulation model Setup/run simulation with ns Modify ns
Hello World - Interactive Mode Interactive mode: $ ns % set ns [new Simulator] _o 3 % $ns at 1 “puts “Hello World!”” 1 % $ns at 1. 5 “exit” 2 % $ns run Hello World! Batch mode: simple. tcl set ns [new Simulator] $ns at 1 “puts “Hello World!”” $ns at 1. 5 “exit” $ns run $ ns simple. tcl Hello World!
How to start? o Create a simulator object set ns [new Simulator] o NAM trace data set nf [open out. nam w] $ns namtrace-all $nf o 'finish' procedure proc finish {} { global ns nf $ns flush-trace close $nf exec nam out. nam & exit 0 } o Execute the 'finish' procedure after 5. 0 seconds $ns at 5. 0 "finish“ o starts the simulation $ns run
Creating Network o Nodes set n 0 [$ns node] set n 1 [$ns node] o Links and queuing $ns <link_type> $n 0 $n 1 <bandwidth> <delay> <queue_type> o o <link_type>: duplex-link, simplex-link <queue_type>: Drop. Tail, RED, CBQ, FQ, SFQ, DRR queues
Two nodes, one link set n 0 [$ns node] set n 1 [$ns node] $ns duplex-link $n 0 $n 1 1 Mb 10 ms Drop. Tail
Sending Data #Create a UDP agent and attach it to node n 0 set udp 0 [new Agent/UDP] $ns attach-agent $n 0 $udp 0 #Create a CBR traffic source and attach it to udp 0 set cbr 0 [new Application/Traffic/CBR] $cbr 0 set packet. Size_ 500 $cbr 0 set interval_ 0. 005 $cbr 0 attach-agent $udp 0 o Null agent which acts as traffic sink set null 0 [new Agent/Null] $ns attach-agent $n 1 $null 0 o Two agents have to be connected with each other $ns connect $udp 0 $null 0 o CBR agent when to send data and when to stop sending $ns at 0. 5 "$cbr 0 start“ $ns at 4. 5 "$cbr 0 stop"
NAM
- Slides: 16