Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting
Transcript of Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting
![Page 1: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/1.jpg)
An Introduction to NS-2*
Gayatri Swamynathan
CS 276 TA
*some slides are from a presentation by Haobo Yu & Nader Salehi, USC/ISI
![Page 2: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/2.jpg)
NS-2 Learning Resources
TA web page for 276:
http://www.cs.ucsb.edu/~gayatri/ta/cs276.html
• Installation instructions
• Using related tools (nam, xgraph, etc)
• NS-2 official website and documentation
• Tutorials to get you started
• Sample coding exercises
![Page 3: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/3.jpg)
Roadmap For Today’s Lecture
• ns Primer• Extending ns
![Page 4: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/4.jpg)
Part I: ns Primer
![Page 5: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/5.jpg)
What is ns?
Object-oriented, discrete event-driven network simulator Written in C++ and OTcl By VINT: Virtual InterNet Testbed
![Page 6: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/6.jpg)
ns Architecture
Separate data path and control path implementations.
![Page 7: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/7.jpg)
What is ns?
Object-oriented, discrete event-driven network simulator Written in C++ and OTcl By VINT: Virtual InterNet Testbed
![Page 8: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/8.jpg)
ns Architecture
Separate data path and control path implementations.
![Page 9: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/9.jpg)
ns Architecture
![Page 10: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/10.jpg)
Hello World – Interactive mode
bash-shell$ ns
% set ns [new Simulator]
_o3
% $ns at 1 “puts \“Hello World!\””
1
% $ns at 1.5 “exit”
2
% $ns run
Hello World!bash-shell$
![Page 11: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/11.jpg)
Hello World – Batch mode
simple.tclset ns [new Simulator]
$ns at 1 “puts \“Hello World!\””
$ns at 1.5 “exit”
$ns run
bash-shell$ ns simple.tcl
Hello World!bash-shell$
![Page 12: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/12.jpg)
Basic Tcl: ex-tcl.tcl
![Page 13: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/13.jpg)
Basic OTcl
Class MomMom instproc greet {} {
$self instvar age_puts “$age_ years old mom: How are you doing?”
}
Class Kid -superclass MomKid instproc greet {} {
$self instvar 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
![Page 14: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/14.jpg)
NS-2 Generic Script Structure
• Create Simulator objectCreate Simulator object• [Turn on tracing][Turn on tracing]• Create topologyCreate topology• [Setup packet loss, link dynamics][Setup packet loss, link dynamics]• Create routing agentsCreate routing agents• Create application and/or traffic sourcesCreate application and/or traffic sources• Post-processing procedures (i.e. nam)Post-processing procedures (i.e. nam)• Start simulationStart simulation
![Page 15: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/15.jpg)
Step1: Create Simulator Object
Create event scheduler set ns [new Simulator]
![Page 16: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/16.jpg)
Step2: Tracing
Insert immediately after scheduler!
Trace packets on all links
set nf [open out.nam w]
$ns trace-all $nf
$ns namtrace-all $nf
![Page 17: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/17.jpg)
Step2: Tracing
![Page 18: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/18.jpg)
NS-2 Generic Script Structure
• Create Simulator objectCreate Simulator object• [Turn on tracing][Turn on tracing]• Create topologyCreate topology• [Setup packet loss, link dynamics][Setup packet loss, link dynamics]• Create routing agentsCreate routing agents• Create application and/or traffic sourcesCreate application and/or traffic sources• Post-processing procedures (i.e. nam)Post-processing procedures (i.e. nam)• Start simulationStart simulation
![Page 19: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/19.jpg)
Step 3: Create network
Two nodes, One link
n1
n0
![Page 20: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/20.jpg)
Step 3: Create Network
Nodes set n0 [$ns node] set n1 [$ns node]
Links and queuing $ns duplex-link $n0 $n1 1Mb 10ms RED
$ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type>
<queue_type>: DropTail, RED, etc.
n1
n0
![Page 21: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/21.jpg)
Creating a larger topology
for {set i 0} {$i < 7} {incr i} {
set n($i) [$ns node]
}
for {set i 0} {$i < 7} {incr i} {
$ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms RED
}
![Page 22: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/22.jpg)
NS-2 Generic Script Structure
• Create Simulator objectCreate Simulator object• [Turn on tracing][Turn on tracing]• Create topologyCreate topology• [Setup packet loss, link dynamics][Setup packet loss, link dynamics]• Create routing agentsCreate routing agents• Create application and/or traffic sourcesCreate application and/or traffic sources• Post-processing procedures (i.e. nam)Post-processing procedures (i.e. nam)• Start simulationStart simulation
![Page 23: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/23.jpg)
Step 4: Network Dynamics
Link failures Hooks in routing module to reflect routing
changes $ns rtmodel-at <time> up|down $n0 $n1
For example:
$ns rtmodel-at 1.0 down $n0 $n1
$ns rtmodel-at 2.0 up $n0 $n1
![Page 24: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/24.jpg)
Step 5: Creating UDP connection
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns attach-agent $n0 $udp
$ns attach-agent $n1 $null
$ns connect $udp $null
n1
n0
udp
null
![Page 25: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/25.jpg)
Step 6: Creating Traffic (On Top of UDP)
CBR set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 500 $cbr set interval_ 0.005
$cbr attach-agent $udp
n1
n0
udp
cbr
null
![Page 26: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/26.jpg)
Creating TCP connection
set tcp [new Agent/TCP]
set tcpsink [new Agent/TCPSink]
$ns attach-agent $n0 $tcp
$ns attach-agent $n1 $tcpsink
$ns connect $tcp $tcpsink
n1
n0
tcp
sink
![Page 27: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/27.jpg)
Step 6: Creating Traffic (On Top of TCP)
FTP set ftp [new Application/FTP] $ftp attach-agent $tcp
Telnet set telnet [new Application/Telnet]
$telnet attach-agent $tcpn1
n0
tcp
ftp
sink
![Page 28: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/28.jpg)
Recall: Generic Script Structure
• set ns [new Simulator]set ns [new Simulator]• [Turn on tracing][Turn on tracing]• Create topologyCreate topology• [Setup packet loss, link dynamics][Setup packet loss, link dynamics]• Create agentsCreate agents• Create application and/or traffic sourcesCreate application and/or traffic sources• Post-processing procedures (i.e. nam)Post-processing procedures (i.e. nam)• Start simulationStart simulation
Examples
![Page 29: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/29.jpg)
Post-Processing Procedures
Add a 'finish' procedure that closes the trace file and starts nam.
proc finish {} { global ns nf$ns flush-trace close $nf exec nam out.nam & exit 0
}
![Page 30: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/30.jpg)
Run Simulation
Schedule Events
$ns at <time> <event> <event>: any legitimate ns/tcl commands
$ns at 0.5 "$cbr start"
$ns at 4.5 "$cbr stop“
Call ‘finish’
$ns at 5.0 "finish"
Run the simulation
$ns run
![Page 31: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/31.jpg)
Recall: Generic Script Structure
• set ns [new Simulator]set ns [new Simulator]• [Turn on tracing][Turn on tracing]• Create topologyCreate topology• [Setup packet loss, link dynamics][Setup packet loss, link dynamics]• Create routing agentsCreate routing agents• Create application and/or traffic sourcesCreate application and/or traffic sources• Post-processing procedures (i.e. nam)Post-processing procedures (i.e. nam)• Start simulationStart simulation
Examples
![Page 32: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/32.jpg)
Visualization Tools
nam-1 (Network AniMator Version 1) Packet-level animation Well supported by ns
xgraph Simulation results
![Page 33: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/33.jpg)
![Page 34: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/34.jpg)
nam Interface: Nodes
Color$node color red
Shape (can’t be changed after sim starts)$node shape box (circle, box, hexagon)
Label (single string)$ns at 1.1 “$n0 label \”web cache 0\””
![Page 35: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/35.jpg)
nam Interfaces: Links
Color
$ns duplex-link-op $n0 $n1 color "green"
Label
$ns duplex-link-op $n0 $n1 label “backbone"
![Page 36: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/36.jpg)
nam Interface: Topology Layout
“Manual” layout: specify everything
$ns duplex-link-op $n(0) $n(1) orient right$ns duplex-link-op $n(1) $n(2) orient right$ns duplex-link-op $n(2) $n(3) orient right$ns duplex-link-op $n(3) $n(4) orient 60deg
If anything missing automatic layout
![Page 37: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/37.jpg)
Simulation Example
![Page 38: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/38.jpg)
Examples
![Page 39: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/39.jpg)
Part II: Extending ns
![Page 40: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/40.jpg)
OTcl and C++: The Duality
C++ OTcl
Pure C++objects
Pure OTclobjects
C++/OTcl split objects
ns
![Page 41: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/41.jpg)
TclObject: Hierarchy and Shadowing
TclObject
Agent
Agent/TCP
Agent/TCP OTcl shadow object
_o123Agent/TCP C++
object
*tcp
TclObject
Agent
TcpAgent
OTcl classhierarchy
C++ classhierarchy
![Page 42: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/42.jpg)
Extending ns
In OTcl In C++
TK8.0 OTcl tclclTcl8.0 ns-2 nam-1
tcl
ex test lib
...
...
examples validation tests
C++ code
OTcl code
ns-allinone
mcast
![Page 43: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/43.jpg)
Extending ns in OTcl
If you don’t want to compile source your changes in your sim scripts
Modifying exisiting code Recompile
Adding new files Change Makefile (NS_TCL_LIB), Update tcl/lib/ns-lib.tcl Recompile
![Page 44: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/44.jpg)
Add Your Changes into ns
TK8.0 OTcl tclclTcl8.0 ns-2 nam-1
tcl
ex test lib
...
...
examples validation tests
C++ code
OTcl code
ns-allinone
mcastmysrc
msg.tcl
![Page 45: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/45.jpg)
Extending ns in C++
Modifying code `make depend` Recompile
Adding code in new files Change Makefile `make depend` Recompile
![Page 46: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/46.jpg)
OTcl Linkage
Lets create a new agent “MyAgent” Dummy agent Derived from the “Agent” class
![Page 47: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/47.jpg)
Step 1: Export C++ class to OTcl
![Page 48: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/48.jpg)
Step 2 : Export C++ class variables to OTcl
set the default value for the variables in the
"ns-2/tcl/lib/ns-lib.tcl" file
![Page 49: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/49.jpg)
Step 3: Export C++ Object Control Commands to OTcl
![Page 50: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/50.jpg)
Step 4: Execute an OTcl command from C++.
![Page 51: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/51.jpg)
Step 5: Compile
Save above code as “ex-linkage.cc”
Open "Makefile", add "ex-linkage.o" at the end of object file list.
Re-compile NS using the "make" command.
![Page 52: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/52.jpg)
Step 5: Run and Test “MyAgent”
![Page 53: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/53.jpg)
Step 5: Run and Test “MyAgent”
![Page 54: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting](https://reader033.fdocuments.us/reader033/viewer/2022061200/5476d6d2b4af9f48248b45a7/html5/thumbnails/54.jpg)
Roadmap For Today’s Lecture
• ns Primer• Extending ns