Professional Documents
Culture Documents
Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example
9/20/2012
What is NS-2
NS-2 stands for Network Simulator version 2.
Discrete event simulator for networking research Work at packet level. Provide substantial support to simulate bunch of protocols like TCP, UDP, FTP, HTTP. Simulate wired and wireless network. Is primarily Unix based. Use TCL as its scripting language.
9/20/2012
Share code, protocols, models, etc More people look at models in more situations Experts develop models > 1k institutes (50 countries), >10k users (2002) About 300 posts to the mailing list every month
Confidence in results
User base
9/20/2012
NS Software
NS, the simulator itself Nam, the network animator
Visualize NS (or other) output Nam editor: GUI interface to generate NS scripts Traffic and topology generators Simple trace analysis, often in Awk, Perl, or Tcl
Pre-processing: Post-processing:
9/20/2012
Architecture of NS
OTcl
Simulation scenario configurations A lot of things can be done without modifying existing C++ modules. per packet processing, core of ns byte manipulation, packet processing, algorithm implementation Needs to change behavior of existing module fast to run, detailed, complete control
C++
9/20/2012
C++ ns
9/20/2012
OTcl
Byte manipulation, packet processing, algorithm implementation Run time speed is important Turn around time (run simulation, find bug, fix bug, recompile, re-run) is slower
Quick explore a number of scenarios Iteration time (change the model and re-run) is more important
9/20/2012
9/20/2012
9/20/2012
10
Outline
Overview: architecture,working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example
9/20/2012
11
Scheduler and Events Scheduler core of the simulator. Its role scheduling of events NS-2 event driven simulator Event: An action to be done after a certain time in the future
Retransmit a packet when a timer expires Pass a packet to the node at the output of a link
after propagation delay time interval
9/20/2012
12
Physical activities are translated to events Events are queued and processed in the order of their scheduled occurrences Time progresses as the events are processed
1
Time: 2.0 sec
2
Time: 1.8 sec
9/20/2012
13
Event Scheduler Event Queue RX Ack Event TX Pkt Event RX TX @ 1.5sec 2.0sec 1.8sec 1.7sec Simulation Finished!
Node 1 Module
Node 2 Module
9/20/2012
14
Link object schedules event end of propagation when to start an FTP application when to finish a simulation
9/20/2012
15
Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example
9/20/2012
16
TCL
Tool Command Language
Commonly, tickle
9/20/2012
17
9/20/2012
18
Variables in TCL
Assignment: set <variable_name> <variable_value> set age 23 set name David Variable substitution $<variable_name> - means substitute the value of the variable name here
9/20/2012
19
Examples
set foo "john" puts "Hi my name is $foo" set month 2 set day 3 set year 97 set date "$month:$day:$year puts $date set foo "puts Hi eval $foo
9/20/2012
20
Math Evaluation
expr command evaluates expressions. Sample command Result set b 5 5 expr ($b*4)-3 17 expr $b <= 2 0 Many other math functions included, such as sin, cos, sqrt, and log.
9/20/2012
21
Eval Command
The 'eval' command Concatenate all its arguments in one string Splits this string using spaces as separators Evaluate the command sentence formed by all the substrings
set foo "set a 22" eval $foo puts $a Output: 22
9/20/2012
22
Command Substitution
Command substitution : substitute the resulting value of a command Invoked with [] Example:
set my_height 1.70 puts "If I was 5 santimeter taller, I would be \ [expr $my_height + (5.0 / 100.0)] meter tall
\ - continue line
9/20/2012 23
Tcl lists
List is an ordered collection of elements
set Lst set Lst "[ set Lst] [list a]" set Lst "[ set Lst] [list b]"
IF statement Syntax:
Rules:
if { expr } { } else {
9/20/2012
25
Examples of IF
set temp 18 if {$temp < 18} { puts "It's a little chilly." } else { puts "Warm enough for me. } puts This gets printed out either way set my_planet "earth" if {$my_planet == "earth"} { puts "I feel right at home." } elseif { $my_planet == "venus } { puts "This is not my home. } else { puts "I am neither from Earth, nor from Venus." }
9/20/2012 26
Execute only one of the blocks of instructions depending on the value of value_to_compare
9/20/2012
27
Example of switch
set num_legs 4 switch $num_legs { 2 {puts "It could be a human."} 4 {puts "It could be a cow."} 6 {puts "It could be an ant."} 8 {puts "It could be a spider."} default {puts "It could be anything."} }
9/20/2012
28
for {set i 0} {$i < 10} {incr i 1} { puts "In the for loop, and i equals $i if { $i > 6 } { puts i is greater than 6! } }
9/20/2012
31
Arrays
Collections of items in which each item is given a unique index for direct access Index must not be numeric - associative arrays Refer to individual element : array name, followed by the index of the element, enclosed in parentheses.
array A
set i david A(i) access the element of the array A associated with david
9/20/2012 32
9/20/2012
33
Function call
set c [function-name par1 par2]
9/20/2012
34
Basic oTCL
Class Person # constructor: Person instproc init {age} { $self instvar age_ set age_ $age } # method: Person instproc greet {} { $self instvar age_ puts $age_ years old: How are you doing? }
9/20/2012
# subclass: Class Kid -superclass Person Kid instproc greet {} { $self instvar age_ puts $age_ years old kid: Whats up, dude? } set a [new Person 45] set b [new Kid 15] $a greet $b greet
35
9/20/2012
36
Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example
9/20/2012
37
n0
Port Classifier Addr Classifier Node entry entry_ classifier_ dmux_
9/20/2012
38
n0 duplex link
n1
head_
n1 entry_
enqT_ tracing
queue_
drophead_
deqT_
drpT_
link_
ttl_
simplex link
9/20/2012
39
Routing
n0
Port Classifier Addr Classifier Node entry entry_
n1
0 1
classifier_
dmux_
n1 entry _
9/20/2012
40
Routing
n0
Port Classifier Addr Classifier
n1
Port Classifier Addr Classifier
entry_
0 1
classifier_
dmux_
1 0
classifier_
dmux_
Link n1-n0
9/20/2012
41
Transport
n0
Port Classifier Addr Classifier
n1
Port Classifier Addr Classifier Link n0-n1 entry_
0
dmux_
0
dmux_
entry_
0 1
classifier_
1 0
classifier_
Link n1-n0
9/20/2012
42
n0
Port Classifier Addr Classifier Application/FTP dst_=1.0 Agent/TCP agents_ Link n0-n1 entry_
n1
Port Classifier Addr Classifier
0
dmux_
0
dmux_
entry_
0 1
classifier_
1 0
classifier_
Link n1-n0
9/20/2012
43
Packet Flow
n0
Port Classifier Addr Classifier Application/FTP dst_=1.0 Agent/TCP
n1
Port Classifier Addr Classifier Link n0-n1 entry_
dst_=0.0 Agent/TCPSink
entry_
0 1
1 0
Link n1-n0
9/20/2012
44
Outline
What are we going to simulate? (Networks in a nutshell) Overview: architecture,working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example
9/20/2012
45
Basic structure of ns-scripts Creating the event scheduler [Tracing] Creating network topology Creating Transport Layer - Agents Creating Applications Events
9/20/2012
46
$ns run
9/20/2012
47
9/20/2012
48
9/20/2012
49
Simple two layers: transport and application. Transport: TCP, UDP etc. Applications: web, ftp, telnet etc.
9/20/2012 50
Agent/UDP
Agent/TCP (=Tahoe)
Other TCP flavors
9/20/2012
51
set udp [new Agent/UDP] set null [new Agent/NULL] $ns attach-agent $n0 $udp $ns attach-agent $n1 $null $ns connect $udp $null
9/20/2012 52
The transport layer: TCP TCP set tcp [new Agent/TCP] set tcpsink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp $ns attach-agent $n1 $tcpsink $ns connect $tcp $tcpsink
9/20/2012 53
Application Layer
Class Application
9/20/2012
54
set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at <time> $ftp start
Telnet
9/20/2012
56
set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $ns at <time> $cbr start
9/20/2012
57
set tfile [new Tracefile] $tfile filename <file> set src [new Application/Traffic/Trace] $src attach-tracefile $tfile
<file>:
Binary format (native!) inter-packet time (msec) and packet size (byte)
9/20/2012
58
9/20/2012
59
Creating Topology
Having simulation trace data at hand Transform a subset of the data of interest into a comprehensible information. Use Perl, awk etc Analyze it
9/20/2012 61
Trace queue. Tracing events over the link between nodes n1 and n2
#Open the Trace-queue file set tracefile2 [open out.tr w] $ns trace-queue $tracefile2
9/20/2012 62
Some NS-2 Programming Patterns Get current simulation time Parameterize the model
foreach argument $argv { # 3 parameters: double, string and integer scan $argument Par1=%lf Par1 scan $argument Par2=%s Par2 scan $argument Par3=%d Par3 } #Use parameters as $ParN #Run simulation: #ns prog.tcl Par1=0.9 Par2=qq Par3=5
9/20/2012 63
9/20/2012
64
NS-NAM Interface Color Node manipulation Link manipulation Topology layout Protocol state
9/20/2012
65
Color mapping
$ns color 40 red $ns color 41 blue $ns color 42 chocolate
9/20/2012
66
9/20/2012
67
Label
$ns duplex-link-op $n0 $n1 label "abcde"
9/20/2012
68
automatic
9/20/2012
69
Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example
9/20/2012
70
NS Example (1/4)
#1. Create a simulator object
set ns [new Simulator]
#2.
# set $ns # set $ns
Turn on tracing
Open the NAM trace file nf [open out.nam w] namtrace-all $nf Open the Trace file tf [open out.tr w] trace-all $tf
#3.
set set set set
9/20/2012
71
NS Example (2/4)
#4. #
$ns $ns $ns $ns
#5. #
Create transport agents set tcp [new Agent/TCP] set sink [new Agent/TCPSink] # Attach transport agents # to peer nodes $ns attach-agent $n0 $tcp $ns attach-agent $n3 $sink # Establish connection $ns connect $tcp $sink $tcp set fid_ 1
#6.
set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP
9/20/2012 72
NS Example (3/4)
#7. # Setup a UDP connection
Create transport agents set udp [new Agent/UDP] set null [new Agent/Null] # Attach transport agents # to peer nodes $ns attach-agent $n1 $udp $ns attach-agent $n3 $null # Establish connection $ns connect $udp $null $udp set fid_ 2
#8. #
set cbr \ [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb
9/20/2012
73
NS Example (4/4)
#9. #
$ns $ns $ns $ns
Start NAM
9/20/2012
74