Ns2 Programs
-
Upload
anusrini23 -
Category
Documents
-
view
34 -
download
5
description
Transcript of Ns2 Programs
To build a simple topology
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nt [open out.tr w]
$ns trace-all $nt
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf nt
$ns flush-trace
#Close the trace file
close $nf
close $nt
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
2. To create simple CBR traffic over UDP
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
set nt [open out.tr w]
$ns trace-all $nt
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# 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
$cbr0 attach-agent $udp0
#Create a Null agent (a traffic sink) and attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
#Schedule events for the CBR agent
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
3. CBR over UDP
set ns [new Simulator]
#Open trace file
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf tracefile
$ns flush-trace
#Close the NAM trace file
close $nf
close $tracefile
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
$ns simplex-link $n0 $n1 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
$ns connect $udp0 $null0
$ns at 1.0 "$cbr start"
$ns at 3.0 "finish"
$ns run
4. FTP over TCP
set ns [new Simulator]
set tracefile [open out.tr w]
$ns trace-all $tracefile
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf tracefile
$ns flush-trace
#Close the NAM trace file
close $nf
close $tracefile
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
set tcp0 [new Agent/TCP]
$ns attach-agent $n0 $tcp0
set ftp0 [new Application/FTP]
$ftp0 attach-agent $tcp0
set tcpsink0 [new Agent/TCPSink]
$ns attach-agent $n1 $tcpsink0
$ns connect $tcp0 $tcpsink0
$ns at 1.0 "$ftp0 start"
$ns at 3.0 "finish"
$ns run
5. Topology Description1. Network consists of 4 nodes (n0, n1, n2, n3). 2. The duplex links between n0 and n2, and n1 and n2 have 2 Mbps of bandwidth and 10 ms of
delay. 3. The duplex link between n2 and n3 has 1.7 Mbps of bandwidth and 20 ms of delay. 4. Each node uses a DropTail queue, of which the maximum size is 10. 5. A “tcp” agent is attached to n0, and a connection is established to a tcp “sink” agent attached
to n3. 6. As default, the maximum size of a packet that a “tcp” agent can generate is 1KByte. 7. A tcp “sink” agent generates and sends ACK packets to the sender (tcp agent) and frees the
received packets. 8. A “udp” agent that is attached to n1 is connected to a “null” agent attached to n3. 9. A “null” agent just frees the packets received. 10. A “ftp” and a “cbr” traffic generator are attached to “tcp” and “udp” agents
respectively, 11. The “cbr” is configured to generate 1 KByte packets at the rate of 1 Mbps. 12. The “cbr” is set to start at 0.1 sec and stop at 4.5 sec, and “ftp” is set to start at
1.0 sec and stop at 4.0 sec.
#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
set tracefile [open out.tr w]
$ns trace-all $tracefile
#Define a 'finish' procedure
proc finish {} {
global ns nf tracefile
$ns flush-trace
#Close the NAM trace file
close $nf
close $tracefile
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
#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
#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
#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
#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
# stop and wait protocol in normal situation
# features : labeling, annotation, nam-graph, and window size #monitoring
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
$ns at 0.0 "$n0 label Sender"
$ns at 0.0 "$n1 label Receiver"set nf [open A1-stop-n-wait.nam w]
$ns namtrace-all $nf
set f [open A1-stop-n-wait.tr w]
$ns trace-all $f
$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail
$ns duplex-link-op $n0 $n1 orient right
$ns queue-limit $n0 $n1 10Agent/TCP set nam_tracevar_ true
set tcp [new Agent/TCP]
$tcp set window_ 1
$tcp set maxcwnd_ 1
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n1 $sink
$ns connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns add-agent-trace $tcp tcp
$ns monitor-agent-trace $tcp
$tcp tracevar cwnd_
$ns at 0.1 "$ftp start"
$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"
$ns at 3.5 "finish"
$ns at 0.0 "$ns trace-annotate \"Stop and Wait with normal operation\""
$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""$ns at 0.11 "$ns trace-annotate \"Send Packet_0\""
$ns at 0.35 "$ns trace-annotate \"Receive Ack_0\""
$ns at 0.56 "$ns trace-annotate \"Send Packet_1\""
$ns at 0.79 "$ns trace-annotate \"Receive Ack_1\""
$ns at 0.99 "$ns trace-annotate \"Send Packet_2\""
$ns at 1.23 "$ns trace-annotate \"Receive Ack_2 \""
$ns at 1.43 "$ns trace-annotate \"Send Packet_3\""
$ns at 1.67 "$ns trace-annotate \"Receive Ack_3\""
$ns at 1.88 "$ns trace-annotate \"Send Packet_4\""
$ns at 2.11 "$ns trace-annotate \"Receive Ack_4\""
$ns at 2.32 "$ns trace-annotate \"Send Packet_5\""
$ns at 2.55 "$ns trace-annotate \"Receive Ack_5 \""
$ns at 2.75 "$ns trace-annotate \"Send Packet_6\""
$ns at 2.99 "$ns trace-annotate \"Receive Ack_6\""$ns at 3.1 "$ns trace-annotate \"FTP stops\""
proc finish {} { global ns nf $ns flush-trace close $nf puts "running nam..." exec nam A1-stop-n-wait.nam & exit 0}$ns run
# sliding window mechanism with some features# such as labeling, annotation, nam-graph, and window size monitoringset ns [new Simulator]set n0 [$ns node]set n1 [$ns node]$ns at 0.0 "$n0 label Sender"$ns at 0.0 "$n1 label Receiver"set nf [open out.nam w]$ns namtrace-all $nf
set f [open out.tr w]$ns trace-all $f$ns duplex-link $n0 $n1 0.2Mb 200ms DropTail$ns duplex-link-op $n0 $n1 orient right$ns queue-limit $n0 $n1 10Agent/TCP set nam_tracevar_ trueset tcp [new Agent/TCP]$tcp set windowInit_ 4$tcp set maxcwnd_ 4$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n1 $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns add-agent-trace $tcp tcp$ns monitor-agent-trace $tcp$tcp tracevar cwnd_$ns at 0.1 "$ftp start"$ns at 3.0 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n1 $sink"$ns at 3.5 "finish"$ns at 0.0 "$ns trace-annotate \"Sliding Window with window size 4 (normal operation)\""$ns at 0.05 "$ns trace-annotate \"FTP starts at 0.1\""$ns at 0.56 "$ns trace-annotate \"Send Packet_0,1,2,3\""$ns at 0.79 "$ns trace-annotate \"Receive Ack_0,1,2,3\""$ns at 0.99 "$ns trace-annotate \"Send Packet_4,5,6,7\""$ns at 1.23 "$ns trace-annotate \"Receive Ack_4,5,6,7\""$ns at 1.43 "$ns trace-annotate \"Send Packet_8,9,10,11\""$ns at 1.67 "$ns trace-annotate \"Receive Ack_8,9,10,11 \""$ns at 1.88 "$ns trace-annotate \"Send Packet_12,13,14,15\""$ns at 2.11 "$ns trace-annotate \"Receive Ack_12,13,14,15\""$ns at 2.32 "$ns trace-annotate \"Send Packet_16,17,18,19\""$ns at 2.56 "$ns trace-annotate \"Receive Ack_16,17,18,19\""$ns at 2.76 "$ns trace-annotate \"Send Packet_20,21,22,23\""$ns at 3.00 "$ns trace-annotate \"Receive Ack_23\""$ns at 3.1 "$ns trace-annotate \"FTP stops\""proc finish {} {global ns$ns flush-trace# puts "filtering..."puts "running nam..."exec nam out.nam &exit 0}$ns run
Simulation of Distance Vector Routing Algorithm
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
$ns namtrace-all $nf
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp0 $null0
set udp1 [new Agent/UDP]
$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0
$ns rtproto DV
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run
Simulation of Link State Routing Algorithm
set ns [new Simulator]
set nr [open thro.tr w]
$ns trace-all $nr
set nf [open thro.nam w]
$ns namtrace-all $nf
proc finish { } {
global ns nr nf
$ns flush-trace
close $nf
close $nr
exec nam thro.nam &
exit 0
}
for { set i 0 } { $i < 12} { incr i 1 } {
set n($i) [$ns node]}
for {set i 0} {$i < 8} {incr i} {
$ns duplex-link $n($i) $n([expr $i+1]) 1Mb 10ms DropTail }
$ns duplex-link $n(0) $n(8) 1Mb 10ms DropTail
$ns duplex-link $n(1) $n(10) 1Mb 10ms DropTail
$ns duplex-link $n(0) $n(9) 1Mb 10ms DropTail
$ns duplex-link $n(9) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(10) $n(11) 1Mb 10ms DropTail
$ns duplex-link $n(11) $n(5) 1Mb 10ms DropTail
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp0 $null0
set udp1 [new Agent/UDP]
$ns attach-agent $n(1) $udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
set null0 [new Agent/Null]
$ns attach-agent $n(5) $null0
$ns connect $udp1 $null0
$ns rtproto LS
$ns rtmodel-at 10.0 down $n(11) $n(5)
$ns rtmodel-at 15.0 down $n(7) $n(6)
$ns rtmodel-at 30.0 up $n(11) $n(5)
$ns rtmodel-at 20.0 up $n(7) $n(6)
$udp0 set fid_ 1
$udp1 set fid_ 2
$ns color 1 Red
$ns color 2 Green
$ns at 1.0 "$cbr0 start"
$ns at 2.0 "$cbr1 start"
$ns at 45 "finish"
$ns run
TRACE FILE ANALYSIS
AWK is a high level programming language which is used to process text files,named after its three orginal author's name:
A : Alfred AhoW : Peter WeinbergerK : Brian Kernighan AWK Scripts are very good in processing the data from the trace files which we get from NS2. If you want to process the trace file manually.Similar to C but more simple
Read the records line by line
$0 : the whole string in the corresponding line
$1 : the first data in the corresponding line
$2 : the second data in the corresponding line
AWK PROGRAM STRUCTUREAwk program structure contains mainly three parts;1. Begin2. Content3. End
BEGIN {<initialization>}
<pattern1> {<actionSet1>} <pattern2> {<actionSet2>} . . .
END {<final finalActionSet>}
BEGIN : Begin deals with what to be executed prior to text file processing,normally which is used to initialize variable values or constants.
CONTENT : Script which process the text file. In this part, AWK moves lines by lines (i.e., records by records) and executes the <actionSet> if the current line match with the pattern. The actions repeats until AWK reaches the end of the file.
END : This part explains what to be executed after the text file processing ie. what to print on the terminal or to show output in terminal.
An AWK example : samp.awk
BEGIN {
sum =0;
}
{
if ($1=="+")
{
sum++;
}
}