250 likes | 381 Views
Introduction to NS. Information. Main website http://www.isi.edu/nsnam/ Documentation, mailing list archive, tutorial Location of Source codes C++ files $NS-HOME/ns-allinone-2.28/ns-2.28 Tcl files $NS-HOME/ns-allinone-2.28/ns-2.28/tcl/lib. NS2 – Network Simulator .
E N D
Information • Main website http://www.isi.edu/nsnam/ • Documentation, mailing list archive, tutorial • Location of Source codes • C++ files • $NS-HOME/ns-allinone-2.28/ns-2.28 • Tcl files • $NS-HOME/ns-allinone-2.28/ns-2.28/tcl/lib
NS2 – Network Simulator • Event-Driven simulator • Maintain a sorted event queue • Dequeue head event • Packet arrival • Assign event to its handler • At TCP agent • Handler processes event • Update Window • Enqueue more events in the event queue • Schedule delivery of ACK
Basic Model • Back-end C++ • Protocols & Framework • Front-end Otcl (Object-Oriented Tcl) • Scenarios • Split Object • Object created in otcl has a corresponding object in C++ • This tutorial only deals with otcl
Tcl/OTcl Basics • Variables • set v1 10 • set v2 $v1 • Array (String indexed) • a($i) ≠ a( $i ) • Printing • puts $filename “string” (default filename is stdout) • Arithmetic Expressions • set value [expr $v1+($v2 * 5.2)] • Control Structures • if {condition} then {…….} • for {set i 0} {$i < 10} {incr i 2} {……} • Procedures • proc proc_name {arg1 arg2…} { ……}
OTcl Basics (contd.) • Creating a class • Class class_name • Class class_name –superclass Base_class • Defining instance procedures • class_name instproc proc_name {args} {…..} • Defining instance variables • $self instvar variable_name (inside a class method) • Creating an instance • set new_inst [new class_name] • Using value of an instance variable • $new_inst set v1 10 or set v2 [$new_inst set v1]
NS Communication Model • Nodes • Hosts, Routers • Links • Queue Management • Queue Monitoring • Agents • Protocol 2 1 3 TCP 5 4 TCP Sink
Node Slide taken from Hung-Yun Hsieh’s tutorial n0 Port Classifier Addr Classifier Node entry dmux_ entry_ classifier_ Link 1 Link 2 Unicast Node
n0 n1 duplex link n1 entry_ head_ enqT_ queue_ deqT_ link_ ttl_ drophead_ drpT_ tracing simplex link Link Slide taken from Hung-Yun Hsieh’s tutorial
n0 n1 Port Classifier Addr Classifier 0 Node entry n1 entry_ dmux_ head_ 1 entry_ enqT_ queue_ deqT_ link_ ttl_ classifier_ drophead_ drpT_ Routing Slide taken from Hung-Yun Hsieh’s tutorial
n0 n1 Port Classifier Addr Classifier 1 0 dmux_ 0 1 entry_ classifier_ Link n1-n0 Routing Slide taken from Hung-Yun Hsieh’s tutorial Port Classifier Addr Classifier dmux_ entry_ Link n0-n1 classifier_
Agent/TCP Agent/TCPSink 0 0 agents_ agents_ 0 1 1 0 Transport Slide taken from Hung-Yun Hsieh’s tutorial n0 n1 Port Classifier Port Classifier dst_= 0.0 dst_= 1.0 Addr Classifier Addr Classifier dmux_ dmux_ entry_ Link n0-n1 entry_ classifier_ classifier_ Link n1-n0
Application/FTP dst_=0.0 dst_=1.0 0 1 1 0 Application Slide taken from Hung-Yun Hsieh’s tutorial n0 n1 Port Classifier Port Classifier Agent/TCPSink Addr Classifier Addr Classifier Agent/TCP 0 0 agents_ agents_ dmux_ dmux_ entry_ Link n0-n1 entry_ classifier_ classifier_ Link n1-n0
dst_=0.0 dst_=1.0 0 1 1 0 Packet Flow Slide taken from Hung-Yun Hsieh’s tutorial n0 n1 Application/FTP Port Classifier Port Classifier Addr Classifier Addr Classifier Agent/TCP 0 0 Agent/TCPSink entry_ Link n0-n1 entry_ Link n1-n0
Starting off… • Things common to all simulation scripts: • Create a new simulator object set ns [new Simulator] • Finish procedure proc finish { } { …….. exit 0 } • Last line $ns run
set ns [new Simulator] proc finish { } { exit 0 } Topology set n1 [$ns node] set n2 [$ns node] $ns duplex-link $n1 $n2 1Mb 10ms DropTail Agents set a1 [new Agent/TCP] set a2 [new Agent/TCPSink] Attach and Connect Agents $ns attach-agent $n1 $a1 $ns attach-agent $n2 $a2 $ns connect $a1 $a2 Start and Finish Time $ns at 0.5 “$a1 advance 10” $ns at 1.5 “finish” $ns run Basic Script
set ns [new Simulator] set nam_file [open out.nam w] $ns namtrace-all $nam_file proc finish {} { global ns nam_file $ns flush-trace close $nam_file exec nam out.nam & exit 0 } set n1 [$ns node] set n2 [$ns node] $ns duplex-link $n1 $n2 1Mb 10ms DropTail set a1 [new Agent/TCP] set a2 [new Agent/TCPSink] $ns attach-agent $n1 $a1 $ns attach-agent $n2 $a2 $ns connect $a1 $a2 $ns at 0.5 “$a1 advance 10” $ns at 1.5 “finish” $ns run A more useful script
Add a new procedure proc record-data { duration } { global datafile a1 ns # data format -- time bytes-received puts $datafile “[$ns now] [$a1 set ndatabytes_]” $ns after $duration “record-data $duration” } set datafile [open “datafile.dat” w] $ns at 0.5 “record-data 0.1” Recording Data
More Complex Case • Bottleneck Link
Topology for {set i 0}{$i < 4} {incr i} { set border($i) [$ns node] } for {set i 0}{$i < 2} {incr i} { set core($i) [$ns node] } $ns duplex-link $border(0) $core(0) 10Mb 5ms DropTail $ns duplex-link $border(1) $core(0) 10Mb 5ms DropTail $ns duplex-link $border(2) $core(1) 10Mb 5ms DropTail $ns duplex-link $border(3) $core(1) 10Mb 5ms DropTail $ns duplex-link $core(0) $core(1) 1Mb 5ms DropTail $ns queue-limit $core(0) $core(1) 20 set qmon [$ns monitor-queue $core(0) $core(1) “”]
Agents set tcp [new Agent/TCP] $ns attach-agent $border(0) $tcp set tcpsink [new Agent/TCPSink] $ns attach-agent $border(3) $tcpsink set udp [new Agent/UDP] $ns attach-agent $border(1) $udp set lm [new Agent/LossMonitor] $ns attach-agent $border(2) $lm $tcp set fid_ 0 $udp set fid_ 1 $ns color 0 Red $ns color 1 Blue
Sources • Attach traffic source to agents and connect set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set rate_ 2Mb set ftp [$tcp attach-app FTP] $tcp set packetSize_ 100 $ns connect $tcp $tcpsink $ns connect $udp $lm
Data Collection proc record-data { duration } { global ns tcp tcp_file lm udp_file queue_file qmon puts $tcp_file “[$ns now] [expr [$tcp set ndatabytes_]/$duration]” $tcp set ndatabytes_ 0 puts $udp_file “[$ns now] [expr [$lm set bytes_]/$duration]” $lm set bytes_ 0 puts $queue_file “[$ns now] [$qmon set pkts_]” $ns after $duration “record-data $duration” }
Other details set ns [new Simulator] # Nam, TCP-data, UDP-data, Queue-data files # Finish method # Topology # Agents # Sources # Data Collection $ns at 0.1 “$ftp start” $ns at 4.0 “$ftp stop” $ns at 1.0 “$cbr start” $ns at 2.0 “$cbr stop” $ns at 0.1 “record-data 0.1” $ns at 4.1 “finish” $ns run
Things to Remember • Topology, agents, sources, start • Connect the agents • Slot not found error • Start/Stop the sources • In procedures, declare global variables before use • “$ns run”– last line