View File - UET Taxila
Download
Report
Transcript View File - UET Taxila
Wireless Communication : LAB 3
Background of Wireless
Communication
Wireless Communication
Technology
Wireless Networking and
Mobile IP
Wireless Local Area
Networks
Student Presentations
and Projects
Introduction to NS2 Programming
Outline
NS overview
NS structure and basics
Steps to create a typical NS script
Summary: Generic Script Structure
Vis Tools
Useful pointers
NS by Example (http://nile.wpi.edu/NS/)
Where is documentation and Tutorials:
http://www.isi.edu/nsnam/ns/ns-documentation.html
http://www.isi.edu/nsnam/ns/tutorial/
What is NS
Discrete event simulator targeted for
networking research
Discrete event simulation:
representing a system by a collection of states and a
set of events that describe state changes.
Discrete-Event/Continuous-Time
An event in NS
a packet ID that is unique for a packet with scheduled time
and the pointer to an network object that handles the
packet
What can ns simulate?
Topology: wired, wireless
Queue Scheduling Algorithms: RED, DropTail, …
Transport Protocols: TCP (all flavors), UDP, …
Routing: Static and dynamic routing, MPLS, …
Application: FTP, HTTP, Telnet, Traffic generators, …
Multicast
Various error models for link failures
Open source
NS Structure
NS is written in oTcl and C++;
Efficiency and simplicity
oTcl is an extension to oTcl (object Tcl):
Have to deal with objects
NS uses C++ for per-packet action
e.g. scheduler, TCP implementation
oTcl for control
Topology
Network objects
User interface-Interactive
Mode
adeel@laptop#> ./ns
#> set ns [new Simulator]
_o4
#> $ns at 1 "puts \"Hello World!\""
1
#> $ns at 1.5 "exit"
2
#> $ns run
Hello World!
User interface-Batch Mode
simple.tcl
set ns [new Simulator]
$ns at 1 "puts \"Hello World!\""
$ns at 1.5 "exit"
$ns run
adeel@laptop#> ns simple.tcl
Hello World!
Basic Tcl
proc test{ }
{
set a 43
set b 27
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
Basic oTcl
Class vehicle
vehicle instproc characterize{}
{
$self instvar NumberOfWheels_
puts "$NumberOfWheels_ -wheel vehicle is a vehicle“
}
Class car -superclass vehicle
car instproc characterize{}
{
$self instvar NumberOfWheels_
puts "$NumberOfWheels_ -wheel vehicle is a car"
}
set a [new vehicle]
set b [new car]
$a set NumberOfWheels_ 3
$b set NumberOfWheels_ 4
$a characterize
$b characterize
Tcl/oTcl Common Problem
Generally useless debugging information:
no object
(_o24 cmd line 1)
invoked from within
“_o24 cmd drop-target { }”
invoked from within
“catch “$self cmd $args” ret”
(procedure “_o24” line 2)
Fortunately, you do NOT need to be a Tcl expert
for using NS
Structure of a typical ns
script
Creating event scheduler
Opening trace files
Creating topology
Creating the transport layer “connection”
Creating application to generate traffic
Start and stop the traffic flows
Structure of a typical ns
script
Creating event scheduler
Opening trace files
Creating topology
Creating the transport layer “connection”
Creating application to generate traffic
Start and stop the traffic flows
Create Event Scheduler
Create scheduler
set ns [new Simulator]
Schedule event
$ns at <time> <event>
<event> is any legitimate ns/tcl commands
Start scheduler
$ns run
Structure of a typical ns
script
Creating event scheduler
Opening trace files
Creating topology
Creating the transport layer “connection”
Creating application to generate traffic
Start and stop the traffic flows
Open trace files Ex.1
Trace packets on all links
set fp [open test.out w]
$ns trace-all $fp
trace format:
<event> <time> <from> <to> <pkt_type> <size> <flag><flow_id><src> <dst><seq_no> <ack_seq_no>
r 1 0 2 tcp 1000 - - - - - - - - - - - - - - - - - 0
+ 1 0 2 tcp 1000 - - - - - - - - - - - - - - - - - 0
0.0 3.1 29
0.0 3.1 29
199
199
- 1 0 2 tcp 1000 - - - - - - - - - - - - - - - - - 0 0.0 3.1 29
199
199
d 1.00234 0 2 tcp 1000 - - - - - - - - - - - - 0
0.0 3.1 29
Open trace files Ex.2
#Open the Trace file
set tf [open out.tr w]
$ns trace-all $tf
Create Topology
Create nodes
set n0 [$ns node]
set n1 [$ns node]
Create links and queues
$ns <link-type> $n0de1 $node2 <bandwidth> <delay> <queue-type>
<queue-type>: DropTail, RED, WFQ, SFQ, …
<link-type>: duplex-link, simplex-link
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
Create Flow: UDP
UDP
Create agents
- set udp [new Agent/UDP]
- set null [new Agent/NULL]
Attach to the nodes
- $ns attach-agent $n0 $udp
- $ns attach-agent $n1 $null
Establish the “connection”
- $ns connect $udp $null
Create Connection: TCP
TCP
Create agents
- set tcp [new Agent/TCP]
- set tcpsink [new Agent/TCPSink]
Attach to the nodes
- $ns attach-agent $n0 $tcp
- $ns attach-agent $n1 $tcpsink
Establish the “connection”
- $ns connect $tcp $tcpsink
Create Application
On Top of UDP: CBR
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp
On Top of TCP: FTP
set ftp [new Application/FTP]
$ftp attach-agent $tcp
Start & Stop the traffic
Schedule to start the traffic
$ns at 0.0 “$cbr0 start”
Schedule to stop the traffic
$ns at 5.0 “$cbr0 stop”
$ns at 5.0 “finish”
proc finish { }
{
global ns fp
$ns flush-trace; close $fp
exec …. &
}
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
Example 1 - TCP
Simple scenario with TCP and UDP
connections
n0
TCP
5Mb
2ms
n2
n1
1.5Mb
10ms
n4
UDP
n5
UDP
recvr
5Mb
2ms
n3
TCPSink
TCP : Step 1
Scheduler & tracing
#Create scheduler
Set ns [new Simulator]
#Turn on tracing
set f [open out.tr w]
$ns trace-all $f
Set nf [open out.nam w]
$ns namtrace-all $nf
TCP : Step 2
Create topology
#create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
TCP : Step 3
#create links
$ns duplex-link $n0 $n1 5Mb 2ms DropTail
$ns duplex-link $n1 $n2 1.5Mb 10ms DropTail
$ns duplex-link $n2 $n3 5Mb 2ms DropTail
$ns queue-limit $n1 $n2 25
$ns queue-limit $n2 $n1 25
TCP : Step 4
Create TCP agents
set tcp [new Agent/TCP]
set sink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
TCP : Step 5
Attach traffic
set ftp [new Application/FTP]
$ftp attach-agent $tcp
#start application traffic
$ns at 1.1 “$ftp start”
TCP : Step 6
End of simulation wrapper (as usual)
$ns at 2.0 “finish”
Proc finish {} {
global ns f nf
close $f
close $nf
puts “Running nam…”
exec nam out.nam &
exit 0
}
$ns run
Example 2
Example 2
#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
Example 2
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the NAM trace file
close $nf
#Execute NAM on the trace file
exec nam out.nam &
exit 0
}
Example 2
#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
Example 2
#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
Example 2
#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
Example 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"
Example 2
#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
Tracing
cat out.tr | grep " 2 3 cbr " | grep ^r |
column 1 10 | awk '{dif = $2 - old2; if(dif==0)
dif = 1; if(dif > 0) {printf("%d\t%f\n", $2,
($1 - old1) / dif); old1 = $1; old2 = $2}}' >
jitter.txt
Viz Tools
Nam-1 (Network AniMator Version 1)
Packet-level animation
Well-supported by ns
Xgraph
Convert trace output into xgraph format
NAM
Ns-nam Interface
Color
Node manipulation
Link manipulation
Topology layout
Protocol state
Misc
Nam Interface: Color
Color mapping
$ns color 40 red
$ns color 41 blue
$ns color 42 chocolate
Color flow id association
$tcp0 set fid_ 40 ;# red packets
$tcp1 set fid_ 41 ;# blue packets
Nam Interface: Nodes
Color
$node color red
Shape (can’t be changed after sim starts)
$node shape box;# circle, box, hexagon
Marks (concentric “shapes”)
$ns at 1.0 “$n0 add-mark m0 blue box”
$ns at 2.0 “$n0 delete-mark m0”
Label (single string)
$ns at 1.1 “$n0 label \”web cache 0\””
Nam Interface: Links
Color
$ns duplex-link-op $n0 $n1 color "green"
Label
$ns duplex-link-op $n0 $n1 label "abced"
Dynamics (automatically handled)
$ns rtmodel Deterministic {2.0 0.9 0.1} $n0 $n1
Asymmetric links not allowed
Nam Interface: Topo Layout
“Manual” layout: specify everything
$ns
$ns
$ns
$ns
duplex-link-op
duplex-link-op
duplex-link-op
duplex-link-op
$n(0)
$n(1)
$n(2)
$n(3)
$n(1)
$n(2)
$n(3)
$n(4)
orient
orient
orient
orient
right
right-up
down
60deg
If anything missing automatic
layout
Nam Interface: Protocol
State
Monitor values of agent variables
$ns add-agent-trace $srm0 srm_agent0
$ns monitor-agent-trace $srm0
$srm0 tracevar C1_
$srm0 tracevar C2_
# … …
$ns delete-agent-trace $tcp1
Nam Interface: Misc
Annotation
Add textual explanation to your sim
$ns at 3.5 "$ns trace-annotate \“packet drop\"“
Set animation rate
$ns at 0.0 "$ns set-animation-rate 0.1ms"
Useful Pointers
Directory structure:
/ns-2.../
contains C++ code
/ns-2.../tcl/lib/
contains oTcl source code
examples simulation scripts
validation test scripts
/ns-2.../tcl/ex/
/ns-2.../tcl/test/
RED Queue Monitor Example
http://nile.wpi.edu/NS
Xgraph
Other Utilities in Ns
Nam editor
Available as part of nam-1
Tcl debugger
For source and documentation, see
http://www.isi.edu/nsnam/ns/ns-debugging.html
Topology generator
http://www.isi.edu/nsnam/ns/ns-topogen.html
Scenario generator
http://www.isi.edu/nsnam/ns/ns-scengeneration.html
Resources
Ns distribution download
http://www.isi.edu/nsnam/ns/ns-build.html
Installation problems and bug-fix
http://www.isi.edu/nsnam/ns/ns-problems.html
Ns-users mailing list
[email protected]
See http://www.isi.edu/nsnam/ns/ns-lists.html
Archives from above URL
Resources (contd..)
Marc Greis’ tutorial
http://www.isi.edu/nsnam/ns/tutorial
Ns-users archive
Ns-manual
http://www.isi.edu/nsnam/ns/ns-documentation.html
Tcl (Tool Command Language)
http://dev.scriptics.com/scripting
Practical programming in Tcl and Tk, Brent Welch
Otcl (MIT Object Tcl)
~otcl/doc/tutorial.html (in distribution)
Resources (contd..)
NS Frequently Asked Questions:
http://www.isi.edu/nsnam/ns/ns-faq.html
Lloyd Wood - introducing ns:
http://www.ee.surrey.ac.uk/Personal/L.Wood/ns/
QUESTIONS
???