ns2 tutorial - how to bind tcl and otcl & structure of tcl scripting

54
An Introduction to NS- 2 * Gayatri Swamynathan CS 276 TA *some slides are from a presentation by Haobo Yu & Nader Salehi, USC/ISI

Upload: thilak

Post on 27-Nov-2014

279 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

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

Roadmap For Today’s Lecture

• ns Primer• Extending ns

Page 4: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

Part I: ns Primer

Page 5: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

ns Architecture

Separate data path and control path implementations.

Page 7: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

ns Architecture

Separate data path and control path implementations.

Page 9: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

ns Architecture

Page 10: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

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

Basic Tcl: ex-tcl.tcl

Page 13: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

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

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

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

Step2: Tracing

Page 18: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

Step 3: Create network

Two nodes, One link

n1

n0

Page 20: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

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

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

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

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

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

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

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

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

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

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

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

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
Page 34: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

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

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

Simulation Example

Page 38: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

Examples

Page 39: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

Part II: Extending ns

Page 40: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

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

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

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

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

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

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

Step 1: Export C++ class to OTcl

Page 48: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

Step 3: Export C++ Object Control Commands to OTcl

Page 50: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

Step 4: Execute an OTcl command from C++.

Page 51: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

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

Step 5: Run and Test “MyAgent”

Page 53: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

Step 5: Run and Test “MyAgent”

Page 54: Ns2 Tutorial - How to Bind Tcl and Otcl & Structure of Tcl Scripting

Roadmap For Today’s Lecture

• ns Primer• Extending ns