Beginning ns-2 - Iowa State University
Download
Report
Transcript Beginning ns-2 - Iowa State University
CprE 543x – ns-2 Tutorial
Zak Abichar, [email protected]
Dept of Electrical and Computer Engineering
Iowa State University
Ames, IA 50011
Based on a presentation by Polly Huang (2nd European ns-2 Workshop, April 2001)
Tutorial Goals
•
•
•
•
overview/intro/essentials/getting started
tcl/otcl/ns-2 fundamentals
designing a simulation
examples
2
Outline
• Introduction
–
–
–
–
The project, the software, the philosophy
Software architecture
Installation and getting started
About extending ns-2
• tcl/otcl/ns-2 fundamentals
– Programming tcl/otcl
• Running a ns-2 simulation
– Simulation design
– Example scripts
3
Multi-state collaboration
•
•
•
•
•
•
AT&T Research
Lawrence Berkeley National Laboratory
UC Berkeley
USC/ISI
Xerox PARC
ETH TIK (Swiss Federal Institute of
Technology)
5
Project Goal
• To support collaborative simulation effort
– promote sharing
• incorporate recent simulation models
– increase confidence in results
• establish regression test suite
– establish common reference
• current and periodic availability of source code
• Base software is ns-2
6
ns-2
•
•
•
•
Discrete event simulator
Packet level
Link layer and up
Wired and wireless
7
Development Status
•
•
•
•
Columbia NEST
UCB REAL
ns-1
ns-2
–
–
–
–
100K lines of C++ code
70K lines of otcl support code
30K lines of test suites
20K lines of documentation
8
Usage and Releases
• Users from approximately
– 600 institutes
– 50 countries
• Releases
– periodic official releases
– nightly snapshots (probably compiles and
works, but buyers beware)
– available from USC/ISI or UK mirror
9
Words of Caution
• While we have considerable confidence in ns, ns
is not a polished and finished product, but the
result of an ongoing effort of research and
development. In particular, bugs in the software
are still being discovered and corrected.
• Users of ns are responsible for verifying for
themselves that their simulations are not
invalidated by bugs.
11
Preliminary for NS-2
• Ability to write correct programs
• Familiarity with object-oriented programming
• Patience to debug NS source code when needed
– Simple usage will not need NS source code debugging
– More complex simulations may need modification to
NS source code
• Debugging skills
– NS uses C++ and Otcl
– User scripts are in Otcl
12
What you can do using NS-2
• Simulate different scenarios with existing
protocols (TCP/UDP)
• Wired Routing protocols - Distance Vector and
Link State (with the link state patch)
• Ad-Hoc Routing protocols - DSR, AODV, TORA
• MAC protocols - 802.3, 802.11 (Wireless MAC)
• Scheduling disciplines - DropTail, RED, WFQ,
DRR, LQD etc.
• Different traffic characterizations - Poisson,
Exponential, Pareto etc.
13
What you can do using NS-2
• Modify NS-2 to implement your own versions of
the above protocols or even code totally new
protocols
• Measurement of Statistics:
– Throughput, Delay, Jitter etc.
– Queue Monitoring, Drops at Queues.
– Literally all that you will need to know with your
simulations.
• Graphic visualization - using “nam” (Network
Animator)
14
The downside
• Cannot capture all the nuances of the real world
networks.
• Very large scale simulations take a lot of time –
they may not be feasible
• Still in the research phase, and there may be many
more bugs lurking out there
• Documentation not adequate
• No fancy user interface – often perceived as
“unfriendly” (at least by people who are new to
ns-2)
15
Outline
• Introduction
–
–
–
–
The project, the software, the philosophy
Software architecture
Installation and getting started
About extending ns-2
• tcl/otcl/ns-2 fundamentals
– Programming tcl/otcl
• Running a ns-2 simulation
– Simulation design
– Example scripts
16
Object-Oriented
+ Reusability
+ Maintainability
– Careful planning ahead
– Performance
17
C++ and otcl Separation
• C++ for data
– per packet action
• otcl for control
– periodic or triggered action
+ Compromize between composibility and
speed
– Learning & debugging
18
otcl and C++: The Duality
C++
otcl
19
tcl Interpreter With Extents
Event
Scheduler
otcl
tcl8.0
•
•
•
•
Network
Component
tclcl
ns-2
otcl: Object-oriented support
tclcl: C++ and otcl linkage
Discrete event scheduler
Data network components
20
Installation
• Getting the code:
– http://www.isi.edu/nsnam/ns/
• Installing ns-2
– http://csl.ee.iastate.edu/~cpre543/ns2_howToInstall.htm
21
About extending ns-2
• Implement new functionalities not covered in ns-2
– Essential for researchers
– Implementing and evaluating new protocols and
schemes
• Requires the understanding of the internal
architecture
– Not that hard
• Need more information?
22
Outline
• Introduction
–
–
–
–
The project, the software, the philosophy
Software architecture
Installation and getting started
About extending ns-2
• tcl/otcl/ns-2 fundamentals
– Programming tcl/otcl
• Running a ns-2 simulation
– Simulation design
– Example scripts
23
Hello World
simple.tcl
set ns [new Simulator]
$ns at 1 “puts \“Hello World!\””
$ns at 1.5 “exit”
$ns run
% ns simple.tcl
Hello World!
%
24
Fundamentals
• tcl
• otcl
– ftp://ftp.tns.lcs.mit.edu/pub/otcl/doc/tutorial.html
• ns-2
– http://www.isi.edu/nsnam/ns/ns_doc.ps.gz
– http://www.isi.edu/nsnam/ns/ns_doc.pdf
– http://www.isi.edu/nsnam/ns/doc/index.html
25
Basic tcl
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
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
26
Basic otcl
Class mom
mom instproc init {age} {
$self instvar age_
set age_ $age
}
mom instproc greet {} {
$self instvar age_
puts “$age_ years old mom:
How are you doing?”
}
34 years old mom: How are you doing?
6 years old kid: What's up?
Class kid -superclass mom
kid instproc greet {} {
$self instvar age_
puts “$age_ years old kid:
What’s up, dude?”
}
set a [new mom 34]
set b [new kid 6]
$a greet
$b greet
27
Outline
• Introduction
–
–
–
–
The project, the software, the philosophy
Software architecture
Installation and getting started
About extending ns-2
• tcl/otcl/ns-2 fundamentals
– Programming tcl/otcl
• Running a ns-2 simulation
– Simulation design
– Example scripts
28
NS input & output
29
Running a Simulation
•
•
•
•
•
Design your simulation
Build NS-2 scripts
Run simulation program
Analyze trace files
Visualize your simulation (Animation)
30
Design your simulation
• Goal and expected results
• Network topology
– Node
– Link
• Specify Agents
– Protocol
• Traffic
• Simulation Scenario
31
A simulation example
32
A TCL script example
n0
Network Topology
n1
Traffic Generation
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 0.2 "$ftp start"
$ns at 1.2 ”exit"
$ns duplex-link $n0 $n1 1.5Mb
10ms DropTail
$ns run
Transport Protocol
set tcp [$ns create-connection TCP $n0 TCPSink $n1 0]
33
Run simulation program
• Usage: ns file.tcl
• If the C++ source codes are modified, re-compilation
is required
• What ns-2 does:
–
–
–
–
–
Read the tcl file
Run the simulation program
Create trace files
NAM (network animator) input files
Statistics (you may need to write post processing scripts)
34
Analyze trace file
• Use some scripts like awk or Perl, to filter the
trace file
• Use Excel, xplot or xgraph to plot the results
35
Raw trace file
+ 0.001 2 0 tcp 1000 ------- 0 2.0 1.0 0 0
- 0.001 2 0 tcp 1000 ------- 0 2.0 1.0 0 0
r 0.0028 2 0 tcp 1000 ------- 0 2.0 1.0 0 0
+ 0.0028 0 1 tcp 1000 ------- 0 2.0 1.0 0 0
- 0.0028 0 1 tcp 1000 ------- 0 2.0 1.0 0 0
r 0.009884 0 1 tcp 1000 ------- 0 2.0 1.0 0 0
+ 0.009884 1 0 ack 40 ------- 0 1.0 2.0 0 1
- 0.009884 1 2 ack 40 ------- 0 1.0 2.0 0 1
r 0.013128 1 0 ack 40 ------- 0 1.0 2.0 0 1
+ 0.013128 0 2 ack 40 ------- 0 1.0 2.0 0 1
- 0.013128 0 2 ack 40 ------- 0 1.0 2.0 0 1
r 0.01416 0 2 ack 40 ------- 0 1.0 2.0 0 1
+ 0.01416 2 0 tcp 1000 ------- 0 2.0 1.0 1 2
- 0.01416 2 0 tcp 1000 ------- 0 2.0 1.0 1 2
36
Open trace.out in Excel
type
+
r
+
r
+
r
+
r
+
-
time
0.001
0.001
0.0028
0.0028
0.0028
0.009884
0.009884
0.009884
0.013128
0.013128
0.013128
0.01416
0.01416
0.01416
src node next dest protocol
2
0 tcp
2
0 tcp
2
0 tcp
0
1 tcp
0
1 tcp
0
1 tcp
1
0 ack
1
2 ack
1
0 ack
0
2 ack
0
2 ack
0
2 ack
2
0 tcp
2
0 tcp
size
flags
1000 ------1000 ------1000 ------1000 ------1000 ------1000 ------40 ------40 ------40 ------40 ------40 ------40 ------1000 ------1000 -------
flowid
src addr dest addr seq #
0
2.0
1.0
0
2.0
1.0
0
2.0
1.0
0
2.0
1.0
0
2.0
1.0
0
2.0
1.0
0
1.0
2.0
0
1.0
2.0
0
1.0
2.0
0
1.0
2.0
0
1.0
2.0
0
1.0
2.0
0
2.0
1.0
0
2.0
1.0
ns seq #
0
0
0
0
0
0
0
0
0
0
0
0
1
1
0
0
0
0
0
0
1
1
1
1
1
1
2
2
37
Plot results in Excel
38
Outline
• Introduction
–
–
–
–
The project, the software, the philosophy
Software architecture
Installation and getting started
About extending ns-2
• tcl/otcl/ns-2 fundamentals
– Programming tcl/otcl
• Running a ns-2 simulation
– Simulation design
– Example scripts
39
A Simulation Example
40
TCL Script Step 1
#Create a simulator object
set ns [new Simulator]
# has denotes a one line comment
ns holds the name of the new simulation
#Open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Open the general trace file
Set f [open out.tr w]
$ns trace-all $f
41
TCL Script Step 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
42
TCL Script Step 3
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Setup a TCP connection
set tcp [new Agent/TCP]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
43
TCL Script Step 4
#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
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packet_size_ 1000
$cbr set rate_ 1mb
44
TCL Script Step 5
#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"
#Call the finish procedure after 5 seconds of simulation
time
$ns at 5.0 "finish"
45
TCL Script Step 6
#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
}
#Run the simulation
$ns run
46