ns-2 Tutorial
description
Transcript of ns-2 Tutorial
![Page 1: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/1.jpg)
1
ns-2 Tutorial
Polly Huang
ETH Zurich
http://netweb.usc.edu/huang
February 25 & 29, 2000
![Page 2: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/2.jpg)
2
Topics
• First session– Introduction and perspective– Essentials and fundamentals– Examples - web, TCP, RED, multicast
• Second session– Special topics – ns-2 Internal– Extending ns-2
![Page 3: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/3.jpg)
3
The VINT Project
• Virtual InterNet Testbed– provide a common platform for network
research– special focus on multi-protocol interactions and
scale– http://netweb.usc.edu/vint
![Page 4: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/4.jpg)
4
Multi-state collaboration
• AT&T Research
• Lawrence Berkeley National Laboratory
• UC Berkeley
• USC/ISI
• Xerox PARC
• ETH TIK
![Page 5: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/5.jpg)
5
VINT People• Project leads and co-Pis
– Lee Breslau (AT&T Labs-Research)– Deborah Estrin (USC/ISI)– Kevin Fall (UC Berkeley)– Sally Floyd (AT&T/ACIRI)– Mark Handley (AT&T/ACIRI)– John Heidemann (USC/ISI)– Scott Shenker (AT&T/ACIRI)
• Graduate students and staff members
![Page 6: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/6.jpg)
6
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• And, yes, it’s free
![Page 7: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/7.jpg)
7
Development Status
• Some approximate numbers:– 99K lines of C++ code– 37K lines of otcl support code– 47K lines of test suites and examples– 13K lines of documentation (301 pages in
notes/docs)
![Page 8: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/8.jpg)
8
Usage and Releases• Users from approximately
– 600 institutes – 50 countries
• Releases– periodic official releases (currently 2.1b6, Jan
2000)– nightly snapshots (probably compiles and
works, but buyers beware)– available from Berkeley or UK mirror
![Page 9: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/9.jpg)
9
Platforms
• Most UNIX and UNIX-like systems FreeBSD or *BSD Linux Sun Solaris HP, SGI
• Window 32...
• (Emulation only for FreeBSD for now)
![Page 10: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/10.jpg)
10
Outlines
• Essentials
• Getting Started
• Fundamental tcl, otcl and ns
• Case Studies– Web, TCP, Routing, Queuing
![Page 11: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/11.jpg)
11
Object-Oriented
+ Reusability
+ Maintainability
– Careful Planning Ahead
![Page 12: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/12.jpg)
12
C++ and otcl Separation
• C++ for data– per packet action
• otcl for control– periodic or triggered action
+ Compromize between composibility and speed
– Learning & debugging
![Page 13: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/13.jpg)
13
otcl and C++: The Duality
C++
otcl
![Page 14: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/14.jpg)
14
tcl Interpreter With Extents
• otcl: Object-oriented support• tclcl: C++ and otcl linkage• Discrete event scheduler• Data network (the Internet) components
tcl8.0
otcl
tclcl
ns-2EventScheduler
Netw
orkC
omponent
![Page 15: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/15.jpg)
15
Outlines
• Essentials
• Getting Started
• Fundamental tcl, otcl and ns
• Case Studies– Web, TCP, Routing, Queuing
![Page 16: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/16.jpg)
16
Installation
• Getting the pieces– (tcl/tk8.0), otcl, tclcl, ns-2, (and nam-1)
• http://www-mash.cs.berkeley.edu/ns/
• [email protected]– [email protected]– subscribe ns-users yourname@address
![Page 17: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/17.jpg)
17
Hello World - Interactive Mode
swallow 71% ns
% set ns [new Simulator]
_o3
% $ns at 1 “puts \“Hello World!\””
1
% $ns at 1.5 “exit”
2
% $ns run
Hello World!
swallow 72%
![Page 18: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/18.jpg)
18
Hello World - Passive Mode
simple.tclset ns [new Simulator]
$ns at 1 “puts \“Hello World!\””
$ns at 1.5 “exit”
$ns run
swallow 74% ns simple.tcl
Hello World!
swallow 75%
![Page 19: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/19.jpg)
19
Outlines
• Essentials
• Getting Started
• Fundamental tcl, otcl and ns
• Case Studies– Web, TCP, Routing, Queuing, Wireless
![Page 20: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/20.jpg)
20
Fundamentals
• tcl
• otcl– ftp://ftp.tns.lcs.mit.edu/pub/otcl/doc/tutorial.html
• ns-2– http://www.isi.edu/~salehi/ns_doc.ps.gz
– http://www.isi.edu/~salehi/ns-doc/
![Page 21: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/21.jpg)
21
Basic tclproc 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
![Page 22: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/22.jpg)
22
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?”
}
Class kid -superclass mom
kid instproc greet {} {
$self instvar age_
puts “$age_ years old kid: What’s up, dude?”
}
set a [new mom 45]
set b [new kid 15]
$a greet
$b greet
![Page 23: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/23.jpg)
23
Basic ns-2
• Creating the event scheduler
• Creating network
• Computing routes
• Creating connection
• Creating traffic
• Inserting errors
• Tracing
![Page 24: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/24.jpg)
24
Creating Event Scheduler
• Create scheduler– set ns [new Simulator]
• Schedule event– $ns at <time> <event>– <event>: any legitimate ns/tcl commands
• Start scheduler– $ns run
![Page 25: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/25.jpg)
25
Creating Network• Nodes
– set n0 [$ns node]– set n1 [$ns node]
• Links & Queuing– $ns duplex-link $n0 $n1 <bandwidth> <delay>
<queue_type>– <queue_type>: DropTail, RED, CBQ, FQ,
SFQ, DRR
![Page 26: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/26.jpg)
26
Creating Network: LAN
• LAN– $ns make-lan <node_list> <bandwidth>
<delay> <ll_type> <ifq_type> <mac_type> <channel_type>
– <ll_type>: LL– <ifq_type>: Queue/DropTail,– <mac_type>: MAC/802_3– <channel_type>: Channel
![Page 27: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/27.jpg)
27
Computing routes
• Unicast– $ns rtproto <type>– <type>: Static, Session, DV, cost, multi-path
• Multicast– $ns multicast (right after [new Simulator])– $ns mrtproto <type>– <type>: CtrMcast, DM, ST, BST
![Page 28: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/28.jpg)
28
Creating Connection: UDP
• UDP– set udp [new Agent/UDP]– set null [new Agent/NULL]– $ns attach-agent $n0 $udp– $ns attach-agent $n1 $null– $ns connect $udp $null
![Page 29: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/29.jpg)
29
Creating Connection: 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
![Page 30: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/30.jpg)
30
Creating Traffic: On Top of TCP
• FTP– set ftp [new Application/FTP]– $ftp attach-agent $tcp– $ns at <time> “$ftp start”
• Telnet– set telnet [new Application/Telnet]– $telnet attach-agent $tcp
![Page 31: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/31.jpg)
31
Creating Traffic: On Top of UDP
• CBR– set src [new Application/Traffic/CBR]
• Exponential or Pareto on-off– set src [new Application/Traffic/Exponential]– set src [new Application/Traffic/Pareto]
![Page 32: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/32.jpg)
32
Creating Traffic: Trace Driven
• Trace driven– set tfile [new Tracefile]– $tfile filename <file>– set src [new Application/Traffic/Trace]– $src attach-tracefile $tfile
• <file>:– Binary format– inter-packet time (msec) and packet size (byte)
![Page 33: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/33.jpg)
33
Inserting Errors• Creating Error Module
– set loss_module [new ErrorModel]– $loss_module set rate_ 0.01– $loss_module unit pkt– $loss_module ranvar [new
RandomVariable/Uniform]– $loss_module drop-target [new Agent/Null]
• Inserting Error Module– $ns lossmodel $loss_module $n0 $n1
![Page 34: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/34.jpg)
34
Tracing
• Trace packets on all links– $ns trace-all [open test.out w]
<event> <time> <from> <to> <pkt> <size>--<flowid> <src> <dst> <seqno> <aseqno>
+ 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0
r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0
• Trace packets on all links in nam-1 format– $ns namtrace-all [open test.nam w]
![Page 35: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/35.jpg)
35
Outlines
• Essentials
• Getting Started
• Fundamental tcl, otcl and ns-2
• Case Studies
![Page 36: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/36.jpg)
36
Case Studies
• Routing - Multicast (mcast.tcl)
• TCP (tcp.tcl)
• Web (web.tcl & dumbbell.tcl)
• Queuing - RED (red.tcl)
![Page 37: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/37.jpg)
37
Visualization Tools
• nam-1 (Network AniMator Version 1)
• xgraph
![Page 38: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/38.jpg)
38
Session 2 Preview
• Application-level support
• Wireless support
• ns-2 internal
• Extending ns-2
![Page 39: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/39.jpg)
39
Lab Instruction
• Obtain a lab account– username:– passwd:
• Change passwd
• Start netscape– set home or bookmark:
http://www.tik.ee.ethz.ch/~huang/ns-tutorial/eth-tutorial.html
– you need this page throughout the lab sessions
![Page 40: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/40.jpg)
40
ns-2 Tutorial
Polly Huang
ETH Zurich
http://netweb.usc.edu/huang
February 25 & 29, 2000
![Page 41: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/41.jpg)
41
Outline
• Quick review
• Multicast simulation
• Special topics– Application-level support– Wireless support
• ns-2 Internal
• Extending ns-2
![Page 42: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/42.jpg)
42
tcl Interpreter With Extensions
tcl8.0
otcl
tclcl
ns-2EventScheduler
Netw
orkC
omponent
![Page 43: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/43.jpg)
43
Example Script
set ns [new Simulator]
set n0 [$ns node]
set n1 [$ns node]
n0 n1
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns at 0.2 "$ftp start"
$ns at 1.2 ”exit"
$ns run$ns duplex-link $n0 $n1 1.5Mb
10ms DropTailset tcp [$ns create-connection
TCP $n0 TCPSink $n1 0]
![Page 44: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/44.jpg)
44
Multicast Configuration• Enabling multicast capability
– set ns [new Simulator -multicast on]
• Configuring multicast routing– $ns mrtproto DM {}
• Creating a multicast connection– set udp [new Agent/UDP]– $ns attach-agent $n0 $udp– set group [Node allocaddr]– $udp set dst_addr_ $group
![Page 45: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/45.jpg)
45
Multicast Traffic
• Creating a receiver joining the group– set rcvr [new Agent/NULL]– $ns attach-agent $n1 $rcvr– $ns at <time> “$n1 join-group $rcvr $group”
• Attaching a traffic source– set cbr [new Application/Traffic/CBR]– $cbr attach-agent $udp – $ns at <time> “$cbr start”
![Page 46: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/46.jpg)
46
Special Topics
![Page 47: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/47.jpg)
47
Special Topics
• Application Level Support
• Wireless/Mobility Support
![Page 48: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/48.jpg)
48
Application: Two-way TCP
• FullTcp connection– set tcp1 [new Agent/TCP/FullTcp]– set tcp2 [new Agent/TCP/FullTcp]– $ns attach-agent $n1 $tcp1– $ns attach-agent $n2 $tcp2– $ns connect $tcp1 $tcp2– $tcp2 listen
![Page 49: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/49.jpg)
49
Application: TcpApp
• User data transfer– set app1 [new Application/TcpApp $tcp1]– set app2 [new Application/TcpApp $tcp2]– $app1 connect $app2– $ns at 1.0 “$app1 send <data_byte> \”<ns-2
command>\””– <ns-2 command>: will be executed when
received at the receiver TcpApp
![Page 50: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/50.jpg)
50
Wireless Support: Setup
• set ns [new Simulator]• set chan [new Channel/WirelessChannel]• set prop [new Propagation/TwoRayGround]• set topo [new Topography]• $topo load_flatgrid <length> <width>• $prop topology $topo
![Page 51: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/51.jpg)
51
Wireless Support: MobileNode
• Creating mobile nodes– set mnode [<routing>-create-mobile-node
<node_id>]– <routing>: dsdv or dsr
• Node coordinates– $mnode set X_ <x>– $mnode set Y_ <y>– $mnode set Z_ 0
![Page 52: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/52.jpg)
52
Wireless Support: Movement
• Specified– $ns at 1.0 “$mnode setdest <x> <y> <speed>”
• Random– $ns at 1.0 “$mnode start”
![Page 53: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/53.jpg)
53
ns-2 Internal
![Page 54: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/54.jpg)
54
Internals
• Discrete Event Scheduler
• Network Topology
• Routing
• Transport
• Application
• Packet Flow
• Packet Format
![Page 55: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/55.jpg)
55
Discrete Event Scheduler
time_, uid_, next_, handler_
head_ ->
handler_ -> handle()
time_, uid_, next_, handler_insert
head_ ->
![Page 56: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/56.jpg)
56
Network Topology - Node
n0 n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
Node entry
Unicast Node
Multicast Classifier
classifier_
dmux_
entry_
Node entry
Multicast Node
multiclassifier_
![Page 57: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/57.jpg)
57
Network Topology - Link
n0 n1
enqT_ queue_ deqT_
drophead_ drpT_
link_ ttl_
n1 entry_head_
![Page 58: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/58.jpg)
58
Routing
n0 n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
Node entry 0
1enqT_ queue_ deqT_
drophead_ drpT_
link_ ttl_
n1 entry_head_
![Page 59: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/59.jpg)
59
Routing (cont.)
n0 n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
0
1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
1
0Link n0-n1
Link n1-n0
![Page 60: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/60.jpg)
60
0
1
Transport
n0 n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
0 Agent/TCP
agents_
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
1
0Link n0-n1
Link n1-n0
0 Agent/TCPSink
agents_
dst_addr_=1dst_port_=0
dst_addr_=0dst_port_=0
![Page 61: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/61.jpg)
61
Application
0
1
n0 n1
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
0 Agent/TCP
agents_
Addr Classifier
Port Classifier
classifier_
dmux_
entry_
1
0Link n0-n1
Link n1-n0
0 Agent/TCPSink
agents_
Application/FTP
dst_addr_=1dst_port_=0
dst_addr_=0dst_port_=0
![Page 62: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/62.jpg)
62
Packet Flow
0
1
n0 n1
Addr Classifier
Port Classifier
entry_
0 Agent/TCP Addr Classifier
Port Classifier
entry_
1
0Link n0-n1
Link n1-n0
0 Agent/TCPSink
Application/FTP
dst_addr_=1dst_port_=0
dst_addr_=0dst_port_=0
![Page 63: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/63.jpg)
63
Packet Format
header
data
ip header
tcp header
rtp header
trace header
cmn header
...
ts_
ptype_
uid_
size_
iface_
![Page 64: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/64.jpg)
64
Extending ns-2 Simulator
![Page 65: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/65.jpg)
65
Outline
• Making changes
• Creating new components
![Page 66: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/66.jpg)
66
ns-2 Directory Structure
sim
tk8.0 otcl Tcltcl8.0 ns-2 nam-1
tcl
ex test lib
...
...
tcl code
example
validation test
C++ code
tcl code core
![Page 67: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/67.jpg)
67
Making Changes in C++ Space
• Existing code– recompile
• Addition– change Makefile and recompile
![Page 68: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/68.jpg)
68
Making Changes in otcl Space
• Existing code– source– recompile
• Addition– source– change Makefile (NS_TCL_LIB), tcl/ns-lib.tcl
(source) and recompile
![Page 69: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/69.jpg)
69
Outline
• Making changes
• Creating new components
![Page 70: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/70.jpg)
70
Creating New Components
• Guidelines
• Inheritance Hierarchy
• C++ and otcl Interface
• Examples– Network layer– Transport layer– Application layer
![Page 71: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/71.jpg)
71
Guidelines
• Decide its inheritance structure
• Create the class and fill in the API virtual functions
• Define otcl linkage functions
• Write the necessary otcl code to access your agent
![Page 72: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/72.jpg)
72
Class Hierarchy (Partial)
TclObject
NsObject
Connector Classifier
Delay AddrClassifierAgent McastClasifierQueue Trace
DropTail RED TCP Enq Dnq Drop
Reno SACK
![Page 73: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/73.jpg)
73
C++ and otcl Linkage
• TclClass
• TclObject: bind() method
• TclObject: command() method
• class Tcl
![Page 74: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/74.jpg)
74
TclClass
TclObject
Agent
Agent/TCP
TclObject
Agent
TcpAgent
NsObject ??
OTclC++ mirroringStatic class TcpClass : public TclClass {public:
TcpClass() : TclClass(“Agent/TCP”) {}TclObject* create(int, const char*const*) {
return (new TcpAgent());}
} class_tcp;
Static class TcpClass : public TclClass {public:
TcpClass() : TclClass(“Agent/TCP”) {}TclObject* create(int, const char*const*) {
return (new TcpAgent());}
} class_tcp;
![Page 75: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/75.jpg)
75
TclObject: bind()
• C++TcpAgent::TcpAgent() {
bind(“window_”, $wnd_);
…
}
• otclAgent/TCP set window_ 50
![Page 76: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/76.jpg)
76
TclObject: command()• C++
Int TcpAgent::command(int argc, const char*const* argv) {if (argc == 3) {
if (strcomp(argv[1], “advance”) == 0) {
int newseq = atoi(argv[2]);
…return(TCL_OK);
}}
return (Agent::command(argc, argv);}
• otclset tcp [new Agent/TCP]
$tcp advance 10
![Page 77: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/77.jpg)
77
Class Tcl
• C++Tcl& tcl = Tcl::instance();
if (argc==2) {
if (strcmp (argv[1], “now”) == 0) {
tcl.resultf(“%g”, clock());
return TCL_OK;
}
tcl.evalc(“puts hello, world”);
tcl.error(“command not found”);
}
• otclfoo now
foo whatever
![Page 78: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/78.jpg)
78
Debugging
• printf() and puts “”
• gdb
• tcl debugger– http://expect.nist.gov/tcl-debug/– place debug 1 at the appropriate location– trap to debugger from the script– single stepping through lines of codes– examine data and code using Tcl-ish commands
![Page 79: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/79.jpg)
79
Case Studies
• Network Layer: Network Interface (packet labeler)
• Transport Layer: TCP Jump Start
• Application Layer: Message agent (ping)
![Page 80: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/80.jpg)
80
Case 1: Network Layer
• Network Interface - Packet Labeler
n0 n1 n2
NetworkInterface A NetworkInterface B
![Page 81: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/81.jpg)
81
Class Hierarchy
TclObject
NsObject
Connector Classifier
Delay AddrClassifierAgent McastClasifierQueue Trace
DropTail RED TCP Enq Dnq Drop
Reno SACK
Network Interface
![Page 82: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/82.jpg)
82
Network Interface Labeler
• class NetworkInterface– NetworkInterface()– recv(pkt)
• pkt->iface() = label_
– send(pkt)• target->recv(pkt)
– command()• label argv {label_ = argv}
• TclClass(“networkinterface”)
C++ otcl
NetworkInterface networkinterface
set iface [new networkinteface]$iface label A
command(): label
![Page 83: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/83.jpg)
83
Case 2: Transport Layer
• TCP Jump Start– from cwnd += 1– to cwnd = maxwin
![Page 84: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/84.jpg)
84
Class Hierarchy
TclObject
NsObject
Connector Classifier
Delay AddrClassifierAgent McastClasifierQueue Trace
DropTail RED TCP Enq Dnq Drop
Reno SACK JS
![Page 85: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/85.jpg)
85
TCP Jump Start
• class JSTcpAgent– openwin()– slowdown()
• TclClass(“Agent/TCP/JS”)
![Page 86: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/86.jpg)
86
Case 3: Application Layer
• Message sender (ping)
n0 n1 n2
Message Sender Message Sender
![Page 87: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/87.jpg)
87
Class Hierarchy
TclObject
NsObject
Connector Classifier
Delay AddrClassifierAgent McastClasifierQueue Trace
DropTail RED TCP Enq Dnq Drop
Reno SACK MessageAgent
![Page 88: ns-2 Tutorial](https://reader035.fdocuments.in/reader035/viewer/2022062803/56814710550346895db447f0/html5/thumbnails/88.jpg)
88
Message Sender
• class MessageAgent– MessageAgent()– recv()– send()– command()
• send {send()}
• TclClass(“Agent/Message”)
C++ otcl
MessageAgent Agent/Message
set msg [new Agent/Message]$msg send
command(): send