NS2 Shahin Shayandeh December 2004 Session 2 Ns
NS-2 Shahin Shayandeh December 2004 Session 2
Ns programming o o o o Create the event scheduler Turn on tracing Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data
Creating Event Scheduler o Create event scheduler set ns [new Simulator] o Schedule events $ns at <time> <event> n <event>: any legitimate ns/tcl commands $ns at 5. 0 “finish” o Start scheduler $ns run o Simulator User Interface 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
Event Scheduler o o Real-time, non-real-time List scheduler: default n o Heap and calendar queue scheduler Real-time scheduler n n Synchronize with real-time Network emulation set ns_ [new Simulator] $ns_ use-scheduler Heap $ns_ at 300. 5 “$self halt”
Discrete Event Scheduler
Setup Routing o Unicast $ns rtproto <type>: Static, Session, DV, cost, multi-path o Multicast $ns multicast (right after [new Simulator]) $ns mrtproto <type>: Ctr. Mcast, DM, ST, BST o Other types of routing supported: source routing, hierarchical routing
Inserting Errors o Creating Error Module set loss_module [new Error. Model] $loss_module set rate_ 0. 01 $loss_module unit pkt $loss_module ranvar [new Random. Variable/Uniform] $loss_module drop-target [new Agent/Null] o Inserting Error Module $ns lossmodel $loss_module $n 0 $n 1
Network Dynamics o o Link failures n Hooks in routing module to reflect routing changes Four models $ns $ns rtmodel Trace <config_file> $n 0 $n 1 rtmodel Exponential {<params>} $n 0 $n 1 rtmodel Deterministic {<params>} $n 0 $n 1 rtmodel-at <time> up|down $n 0 $n 1 n Parameter list [<start>] <up_interval> <down_interval> [<finish>] $ns rtmodel Deterministic {2. 0 0. 9 0. 1} $n 0 $n 1
Creating Connection and Traffic o UDP set udp [new Agent/UDP] set null [new Agent/Null] $ns attach-agent $n 0 $udp $ns attach-agent $n 1 $null $ns connect $udp $null o CBR set src [new Application/Traffic/CBR] o Exponential or Pareto on-off set src [new Application/Traffic/Exponenti al] set src [new Application/Traffic/Pareto]
Creating Connection and Traffic II o TCP set tcp [new Agent/TCP] set tcpsink [new Agent/TCPSink] $ns attach-agent $n 0 $tcp $ns attach-agent $n 1 $tcpsink $ns connect $tcpsink o FTP set ftp [new Application/FTP] $ftp attach-agent $tcp o Telnet set telnet [new Application/Telnet] $telnet attach-agent $tcp
Creating Traffic: Trace Driven o Trace driven set tfile [new Tracefile] $tfilename <file> set src [new Application/Traffic/Trace] $src attach-tracefile $tfile <file>: o o Binary format (native!) inter-packet time (msec) and packet size (byte)
Summary: Generic 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
ns nam Interface o o o Color Node manipulation Link manipulation Topology layout Misc
nam Interface: Color o Color mapping $ns color 40 red $ns color 41 blue $ns color 42 chocolate o Color flow id association $tcp 0 set fid_ 40 $tcp 1 set fid_ 41 ; # red packets ; # blue packets
nam Interface: Nodes o Color $node color red o Shape (can’t be changed after sim starts) $node shape box o ; # circle, box, hexagon Marks (concentric “shapes”) $ns at 1. 0 “$n 0 add-mark m 0 blue box” $ns at 2. 0 “$n 0 delete-mark m 0” o Label (single string) $ns at 1. 1 “$n 0 label ”web cache 0””
nam Interfaces: Links o Color $ns duplex-link-op $n 0 $n 1 color "green" o Label $ns duplex-link-op $n 0 $n 1 label "abced" o Dynamics (automatically handled) $ns rtmodel Deterministic {2. 0 0. 9 0. 1} $n 0 $n 1
nam Interface: Topo Layout o “Manual” layout: specify everything $ns $ns o duplex-link-op $n(0) $n(1) $n(2) $n(3) $n(4) orient right 60 deg If anything missing automatic layout
nam Interface: Misc o Annotation n Add textual explanation to your simulation $ns at 3. 5 "$ns trace-annotate “packet drop"“ o Set animation rate $ns at 0. 0 "$ns set-animation-rate 0. 1 ms"
- Slides: 18