490 likes | 507 Views
Learn about NS-2, a discrete event simulator for network research, its evolution, architecture, programming languages, and installation procedures. Explore NS-2 elements like event scheduling, network creation, routing, errors, and traffic creation in this complete guide.
E N D
Introduction • NS is a discrete event simulator targeted at networking research. • NS provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks. • NS is not a polished and finished product, but the result of an on-going effort of research and development.
Introduction • Ns began as a variant of the REAL network simulator in 1989 and has evolved substantially over the past few years. • REAL is a network simulator originally intended for studying the dynamic behavior of flow and congestion control schemes in packet-switched data networks.
Introduction • In 1995 ns development was supported by DARPA through the VINT project at LBL, Xerox PARC, UC Berkeley, and USC/ISI. • VINT (Virtual InterNetwork Testbed ) is a DARPA-funded research project whose aim is to build a network simulator that will allow the study of scale and protocol interaction in the context of current and future network protocols.
NS Installation • OS: Linux, Unix. • You may install the Linux virtual machine “Cygwin” under windows (not recommended). • You will find the complete installation procedure at http://www.isi.edu/nsnam/ns/
NS-2 Programming Languages • Back-end C++ • Defining new agents, protocols and framework. • Manipulations at the byte/bit levels. • Front-end Otcl (Object-Oriented Tool Command Language) • Topologies, scenarios, simulations, … • Script language (easy topology modifications) • Split Object • Object created in Otcl has a corresponding object in C++.
OTcl and C++: The Duality Pure OTcl objects Pure C++ objects C++/OTcl split objects C++ OTcl ns
Network Components TclCL Event Scheduler OTcl NS-2 Architecture TclCL: C++ and OTcl linkage Tcl C/C++
Tcl Basics • Script Commands • Commandarg1 arg2 arg3 … • Arguments • Simple: a, v1, ns, … • Group: {1 2 3} • Command: [Command arg1 arg2 arg3 …] • Variables • set v1 10 • set v2 $v1 • Array (String indexed) • a($i)
Tcl Basics • Printing • puts $filename “string” (default filename is stdout) • Arithmetic Expressions • set value [expr $v1+ $v2] • Control Structures • if {condition} then {…….} • for {set i 0} {$i < 10} {incr i 2} {……} • Procedures • proc proc_name {arg1 arg2…} { ……}
Example set a 43 set b 27 proc test { a b } { 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]” } } } test 43 27
Results 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
Tcl Substitution • Variable substitution • i: the character ‘i’. • $i: the variable i. • Operation substitution • set i 5 + 6wrong number of arguments • set i {5 + 6}5 + 6 • set i [5 + 6]invalid command • set i [expr 5 + 6]11
Tcl Substitution • Anti-Slash substitution • set price $2.00can't read "2": no such variable • set price \$2.00 • Other anti-slash substitutions:\[, \], \{, \}, \”, \\, \n, \t.
OTcl Basics • Creating a class • Class class_name • Class class_name –superclass Base_class • Defining instance procedures • class_name instproc proc_name {args} {…..} • Defining instance variables • $selfinstvar variable_name (inside a class method)
OTcl Basics • Creating an instance • set new_inst [new class_name] • Calling an instance procedure • $new_inst proc_name {args} • Using an instance value • $new_inst set v1 10 • set v2 [$new_inst set v1]
Examples Class Mom Mom instproc greet {} { $selfinstvar age_ puts “$age_ years old mom: How are you doing?” } Class Kid -superclass Mom Kid instproc greet {} { $selfinstvar age_ puts “$age_ years old kid: What’s up, dude?” } set mom [new Mom] $mom set age_ 45 set kid [new Kid] $kid set age_ 15 $mom greet $kid greet 45 years old mom: How are you doing? 15 years old kid: What's up, dude?
NS-2 Elements • 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 • Create event scheduler • set ns [new Simulator] • Schedule events • $ns at <time> “<event>” • <event>: any legitimate ns/tcl commands • Start scheduler • $ns run
Example simple.tcl set ns [new Simulator] $ns at 1 “puts \“Hello World!\”” $ns at 1.5 “exit” $ns run swallow 74% ns simple.tcl Hello World! swallow 75%
Creating Network • Nodes • set n0 [$ns node] • set n1 [$ns node] n0 n1
Creating Network • Links and queuing $ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type> • <bandwidth>: 1000b, 1kb, 0.001Mb, … • <delay>: 1ms, 0.001s, … • <queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR. n0 n1
Tracing • Trace packets on all links • #Open the NAM trace file #Open the Trace file • set nf [open out.nam w] set tf [open out.tr w] • $ns namtrace-all $nf $ns trace-all $tf • Must appear immediately after creating scheduler • Turn on tracing on specific links • $ns trace-queue $n0 $n1 • $ns namtrace-queue $n0 $n1
Simulation Example 1 set ns [new Simulator] set nf [open out.nam w] $ns namtrace-all $nf set n0 [$ns node] set n1 [$ns node] $ns duplex-link $n0 $n1 1Mb 10ms DropTail $ns run
Creating Connection: UDP • UDP set udp0 [new Agent/UDP] set null [new Agent/Null] $ns attach-agent $n0 $udp0 $ns attach-agent $n1 $null $ns connect $udp0 $null UDP null n0 n1
Simulation Example 2 set ns [new Simulator] set nf [open out.nam w] $ns namtrace-all $nf set n0 [$ns node] set n1 [$ns node] $ns duplex-link $n0 $n1 1Mb 10ms DropTail set udp0 [new Agent/UDP] set null [new Agent/Null] $ns attach-agent $n0 $udp0 $ns attach-agent $n1 $null $ns connect $udp $null $ns run
Creating Traffic: On Top of UDP • CBR set src [new Application/Traffic/CBR] $src attach-agent $udp0 • Exponential or Pareto on-off set src [new Application/Traffic/Exponential] Or set src [new Application/Traffic/Pareto] $src attach-agent $udp0 CBR UDP null n0 n1
Setting Traffic • Setting Traffic Parameters set src [new Application/Traffic/CBR] $src set packetsize_ 500 $src set interval_ 0.005 $src attach-agent $udp0 • Scheduling Events $ns at 0.5 "$src start" $ns at 4.5 "$src stop"
The ‘finish()’ Procedure • Flush NS tracing • Close tracing files • Executing any post-analysis programs (display results, run Network Animator NAM, …) proc finish {} { global ns nf $ns flush-trace close $nf exec nam out.nam & exit 0 }
Simulation Example 3 • See example.tcl
Nodes Layout # create nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] # add links $ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms DropTail # layout $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 # right(0), right-up(45), right-down(315), left(180), # left-up(135),left-down(225), up(90), down(270) n0 n2 n3 n1
Marking the Flow cbr0 udp0 # create color classes $ns color 1 Blue $ns color 2 Red # add connection between n0-n2, # n1-n2, n2-n3 ………… # add udp agents ……… # add sink agent ………… # connect agents ………… # add CBRs (cbr0 and cbr1)and connect them to udp agents ………… # mark the two flows with different colors $udp0 set class_ 1 $udp1 set class_ 2 n0 null0 n2 n3 n1 udp1 cbr1
Monitoring the Queue # monitor the queue at 90 degree angle $ns duplex-link-op $n2 $n3 queuePos 0.5 n0 n2 n3 n1 # monitor the queue at 270 degree angle $ns duplex-link-op $n2 $n3 queuePos 1.5 n0 n2 n3 n1 #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10
SFQ: Statically Fair Queue # add links, with an SFQ on n2-n3 link $ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms SFQ n0 n2 n3 n1 Statically fair queues applies the “round robin” approach (fair share).
Larger Topologies # create 7 nodes for {set i 0} {$i < 7} {incr i} { set n($i) [$ns node] } # add 7 circular links for {set i 0} {$i < 7} {incr i} { $ns duplex-link $n($i) $n([expr [expr $i + 1]%7]) 1Mb 10ms DropTail } n0 n1 n6 n2 n3 n5 n4
Schedule Node/Link Failure #Node Failure/Restoration $ns rtmodel-at 1.0 down $n(1) $ns rtmodel-at 2.0 up $n(1) #Link Failure/Restoration $ns rtmodel-at 1.0 down $n(1) $n(2) $ns rtmodel-at 2.0 up $n(1) $n(2)
Routing # Enable static route strategy for the simulation $ns rtproto Static # Run DV agents on nodes $n1, $n2, and $n3 $ns rtproto DV $n1 $n2 $n3 # Run link state routing on specified nodes $ns rtproto LS $n1 $n2
Trace Files (event, time, from_node, to_node, pkt type, pkt size, flags, fid, src_addr, dst_addr, seq_num, pkt_id) Events:r received, + enqueued, - dequeued, d dropped r 1.152965 0 2 tcp 1040 ------- 1 0.0 3.0 4 137 + 1.152965 2 3 tcp 1040 ------- 1 0.0 3.0 4 137 r 1.154 1 2 cbr 1000 ------- 2 1.0 3.1 130 138 + 1.154 2 3 cbr 1000 ------- 2 1.0 3.1 130 138 r 1.154706 2 3 cbr 1000 ------- 2 1.0 3.1 127 133 - 1.1556 2 3 tcp 1040 ------- 1 0.0 3.0 4 137 ... d 1.337868 2 3 tcp 1040 ------- 1 0.0 3.0 26 193 r 1.338 1 2 cbr 1000 ------- 2 1.0 3.1 153 197 + 1.338 2 3 cbr 1000 ------- 2 1.0 3.1 153 197 d 1.338 2 3 cbr 1000 ------- 2 1.0 3.1 153 197
Post Analysis r 1.152965 0 2 tcp 1040 ------- 1 0.0 3.0 4 137 + 1.152965 2 3 tcp 1040 ------- 1 0.0 3.0 4 137 r 1.154 1 2 cbr 1000 ------- 2 1.0 3.1 130 138 + 1.154 2 3 cbr 1000 ------- 2 1.0 3.1 130 138 r 1.154706 2 3 cbr 1000 ------- 2 1.0 3.1 127 133 - 1.1556 2 3 tcp 1040 ------- 1 0.0 3.0 4 137 ... d 1.337868 2 3 tcp 1040 ------- 1 0.0 3.0 26 193 r 1.338 1 2 cbr 1000 ------- 2 1.0 3.1 153 197 + 1.338 2 3 cbr 1000 ------- 2 1.0 3.1 153 197 d 1.338 2 3 cbr 1000 ------- 2 1.0 3.1 153 197 1.1529651040 1.1541000 1.1547061000 1.3381000 …… Output file format to be viewed by xgraph
Example Example.tr
Exponential Traffic A new Exponential On/Off traffic generator can be created and parameterized as follows: set e [new Application/Traffic/Exponential] $e set packet-size 210 $e set burst-time 500ms $e set idle-time 500ms $e set rate 100k Where: packet-sizethe constant size of the packets generated burst-timethe average “on” time for the generator idle-timethe average “off” time for the generator rate the sending rate during “on” times See example: expo1.tcl
Parameterized Function that Returns an Exponential Traffic proc expo-traffic {size burst idle rate} { # create an Expo traffic agent and set its # configuration parameters set traffic [new Application/Traffic/Exponential] $traffic set packet-size $size $traffic set burst-time $burst $traffic set idle-time $idle $traffic set rate $rate # return the exponential traffic return $traffic } # usage set source0 [expo-traffic 200 2s 1s 100k] set source1 [expo-traffic 200 2s 1s 200k] set source2 [expo-traffic 200 2s 1s 300k]
Recording Data in Output Files (step 1) n0 n1 n3 n4 n2 # open files set f0 [open out0.tr w] set f1 [open out1.tr w] set f2 [open out2.tr w]
Recording Data in Output Files (step 2) proc finish {} { global f0 f1 f2 #Close the output files close $f0 close $f1 close $f2 #Call xgraph to display the results exec xgraph out0.tr out1.tr out2.tr -geometry 800x400 & exit 0 }
Recording Data in Output Files (step 3) proc attach-expo-traffic {node sink size burst idle rate } { #Get an instance of the simulator set ns [Simulator instance] #Create a UDP agent and attach it to the node set source [new Agent/UDP] $ns attach-agent $node $source #Create an Expo traffic agent and set its #configuration parameters set traffic [new Application/Traffic/Exponential] $traffic set packet-size $size $traffic set burst-time $burst $traffic set idle-time $idle $traffic set rate $rate # Attach traffic source to the traffic generator $traffic attach-agent $source #Connect the source and the sink $ns connect $source $sink return $traffic}
Recording Data in Output Files (step 4) # set the source traffics set source0 [attach-expo-traffic $n0 $sink0 200 2s 1s 100k] set source1 [attach-expo-traffic $n1 $sink1 200 2s 1s 200k] set source2 [attach-expo-traffic $n2 $sink2 200 2s 1s 300k] # set the sink node agents set sink0 [new Agent/LossMonitor] set sink1 [new Agent/LossMonitor] set sink2 [new Agent/LossMonitor] $ns attach-agent $n4 $sink0 $ns attach-agent $n4 $sink1 $ns attach-agent $n4 $sink2 record received bytes
Recording Data in Output Files (step 5) proc record {} { global sink0 sink1 sink2 f0 f1 f2 # Get an instance of the simulator set ns [Simulator instance] # Set the time after which the procedure # should be called again set time 0.5 # How many bytes have been received # by the traffic sinks? set bw0 [$sink0 set bytes_] set bw1 [$sink1 set bytes_] set bw2 [$sink2 set bytes_] read received bytes
Recording Data in Output Files (step 5: contd…) # Get the current time set now [$ns now] # Calculate the bandwidth (in MBit/s) # and write it to the files puts $f0 "$now [expr $bw0/$time*8/1000000]“ puts $f1 "$now [expr $bw1/$time*8/1000000]“ puts $f2 "$now [expr $bw2/$time*8/1000000]“ # Reset the bytes_ values on the traffic sinks $sink0 set bytes_ 0 $sink1 set bytes_ 0 $sink2 set bytes_ 0 # Re-schedule (recursively) the procedure $ns at [expr $now+$time] "record“ }
Recording Data in Output Files (step 6) # Schedule the events $ns at 0.0 "record" $ns at 10.0 "$source0 start“ $ns at 10.0 "$source1 start“ $ns at 10.0 "$source2 start“ $ns at 50.0 "$source0 stop“ $ns at 50.0 "$source1 stop“ $ns at 50.0 "$source2 stop“ $ns at 60.0 "finish" # Run the simulation $ns run See example: expo2.tcl