ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++,...

Post on 07-Oct-2018

226 views 0 download

Transcript of ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++,...

11

ns-2 The Network Simulator Fundamentals

Downloads and further info at:http://www.isi.edu/nsnam/ns

22

ns Primer

Basic nsn Architecturen Basic Tcl, OTcln Elements of ns

33

ns Architecture

Object-oriented (C++, OTcl)Scalability + Extensibilityn Control/”data” separationn Split C++/OTcl object

Modular approachn Fine-grained object composition

44

Object-Oriented

+ Reusability+ Maintenance

– Performance (speed and memory)– Careful planning of modularity

55

C++ and OTcl Separation

C++ for “data”n Per-packet action

OTcl for controln Periodic or triggered action

+ Compromise between composibility and speed

– Learning and debugging

66

OTcl and C++: The Duality

C++ OTcl

Pure C++objects

Pure OTclobjects

C++/OTcl split objects

ns

77

Extending Tcl InterpreterOTcl: object-oriented TclTclCL: C++ and OTcllinkageDiscrete event schedulerData network componentsn Link layer and upn Emulation support

Tcl

OTcl

TclCL

ns-2

Event Scheduler

Network Components

C/C++

88

Hello World - Interactive Modeswallow 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%

99

Hello World - Batch 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%

1010

Basic Tclset varname value

sets ‘varname’ to ‘value’set varname valueinspects the value of ‘varname’expr $varname*4multiplies ‘varname’ by 4[Tcl command] excutes the command and returns its outputset varname [expr $varname*4]multiplies ‘varname’ by 4 and update its value

1111

Basic Tcl – sample scriptset a 43

set b 27

proc test { a b } {

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 43 27

1212

Basic OTclClass Mom

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 mom [new Mom]

$mom set age_ 45

set kid [new Kid]

$kid set age_ 15

$mom greet

$kid greet

1313

Steps in a basic ns-2 simulation

Create the event scheduler[Turn on tracing]Create networkCreate transport connectionCreate traffic over transport connectionsTransmit application-level data

1414

Creating Event Scheduler

Create event schedulern set ns [new Simulator]

Schedule eventsn $ns at <time> <event>n <event>: any legitimate ns/tcl commands

Start schedulern $ns run

1515

Tracing

Trace packets on all linksn $ns trace-all [open test.out w]

<event> <time> <from> <to> <<event> <time> <from> <to> <pktpkt> <size> > <size> ---- <fid> <src> <dst> <<fid> <src> <dst> <seqseq> <> <attrattr>>

+ 1 0 2 cbr 210 + 1 0 2 cbr 210 -------------- 0 0.0 3.1 0 00 0.0 3.1 0 0

-- 1 0 2 cbr 210 1 0 2 cbr 210 -------------- 0 0.0 3.1 0 00 0.0 3.1 0 0

r 1.00234 0 2 cbr 210 r 1.00234 0 2 cbr 210 -------------- 0 0.0 3.1 0 00 0.0 3.1 0 0

Trace packets on all links in nam-1 formatn $ns namtrace-all [open test.nam w]Must appear immediately after creating scheduler

1616

Tracing

Turn on tracing on specific linksn $ns trace-queue $n0 $n1n $ns namtrace-queue $n0 $n1

1717

Creating NetworkNodesn set n0 [$ns node]n set n1 [$ns node]

Links and queuingn $ns duplex-link $n0 $n1 <bandwidth>

<delay> <queue_type>n <queue_type>: DropTail, RED, CBQ, FQ,

SFQ, DRR

1818

Creating Connection: TCPthe detailed mode

TCPn set tcp [new Agent/TCP]n set tcpsink [new Agent/TCPSink]n $ns attach-agent $n0 $tcpn $ns attach-agent $n1 $tcpsinkn $ns connect $tcp $tcpsink

1919

Creating Connection: TCPthe shortcut mode

Create agents and attach them to nodes using a single line of TclExample:set tcp [$ns create-connection TCP $n0

TCPSink $n1 2]Sets up both the TCP source and the TCP receiver, and attaches them to nodes n0 and n1

2020

Setting up connection parameters

Use the handle returned by the connection set up process, and define various TCP parameters (i.e., maxcwnd_, ssthresh_ etc.)$tcp set maxcwnd_ 10Sets the maximum congestion window of the TCP source as “10”

2121

Creating Traffic: On Top of TCPthe detailed mode

FTPn set ftp [new Application/FTP]n $ftp attach-agent $tcp

Telnetn set telnet [new Application/Telnet]n $telnet attach-agent $tcp

2222

Creating Traffic: On Top of TCPthe shortcut mode

Create applications and attach them to agents using a single line of TclExample:set ftp [$tcp attach-app FTP]

2323

Creating Connection: UDP

UDPn set udp [new Agent/UDP]n set null [new Agent/Null]n $ns attach-agent $n0 $udpn $ns attach-agent $n1 $nulln $ns connect $udp $null

2424

Creating Traffic: On Top of UDP

CBRn set src [new Application/Traffic/CBR]

Exponential or Pareto on-offn set src [new

Application/Traffic/Exponential]n set src [new Application/Traffic/Pareto]

2525

Summary: Generic Script Structure

set ns [new Simulator]set ns [new Simulator]

# [Turn on tracing]# [Turn on tracing]

# Create topology# Create topology

# Create: # Create:

# # -- protocol agentsprotocol agents

# # -- application and/or setup traffic application and/or setup traffic sourcessources

# Post# Post--processingprocessing procsprocs

# Start simulation# Start simulation

2626

Visualization Tools

nam-1 (Network AniMator Version 1)n Packet-level animationn Well supported by ns

xgraphn Conversion from ns trace to xgraph format

2727

nam

Basic visualizationn Topology layoutn Animation controln Synchronous replay

Fine-tune layoutTCP/SRM visualizationEditor: generate ns simulation scripts

2828

nsànam Interface

ColorNode manipulationLink manipulationTopology layoutProtocol stateMisc

2929

nam Interface: Color

Color mapping$ns color 40 red$ns color 40 red

$ns color 41 blue$ns color 41 blue

$ns color 42 chocolate$ns color 42 chocolate

Color ↔ flow id association$tcp0 set fid_ 40$tcp0 set fid_ 40 ;# red packets;# red packets

$tcp1 set fid_ 41$tcp1 set fid_ 41 ;# blue packets;# blue packets

3030

nam Interface: Nodes

Color$node color red$node color red

Shape (can’t be changed after sim starts)$node shape box$node shape box ;# circle, box, hexagon;# circle, box, hexagon

Marks (concentric “shapes”)$ns at 1.0 “$n0 add$ns at 1.0 “$n0 add--mark m0 blue box”mark m0 blue box”

$ns at 2.0 “$n0 delete$ns at 2.0 “$n0 delete--mark m0”mark m0”

Label (single string)$ns at 1.1 “$n0 label $ns at 1.1 “$n0 label \\”web cache 0”web cache 0\\””””

3131

nam Interfaces: Links

Color$ns duplex$ns duplex--linklink--op $n0 $n1 color "green"op $n0 $n1 color "green"

Label$ns duplex$ns duplex--linklink--op $n0 $n1 label "op $n0 $n1 label "abcedabced""

Dynamics (automatically handled)$ns$ns rtmodelrtmodel Deterministic {2.0 0.9 0.1} $n0 $n1Deterministic {2.0 0.9 0.1} $n0 $n1

Asymmetric links not allowed

3232

nam Interface: Topo Layout

“Manual” layout: specify everything

$ns duplex$ns duplex--linklink--op $n(0) $n(1) orient rightop $n(0) $n(1) orient right

$ns duplex$ns duplex--linklink--op $n(1) $n(2) orient rightop $n(1) $n(2) orient right

$ns duplex$ns duplex--linklink--op $n(2) $n(3) orient rightop $n(2) $n(3) orient right

$ns duplex$ns duplex--linklink--op $n(3) $n(4) orient 60degop $n(3) $n(4) orient 60deg

If anything missing à automatic layout

3333

nam Interface: Protocol State

Monitor values of agent variables$ns add$ns add--agentagent--trace $srm0trace $srm0 srmsrm_agent0_agent0

$ns monitor$ns monitor--agentagent--trace $srm0trace $srm0

$srm0$srm0 tracevartracevar C1_C1_

$srm0$srm0 tracevartracevar C2_C2_

# … …# … …

$ns delete$ns delete--agentagent--trace $tcp1trace $tcp1

3434

nam Interface: Misc

Annotationn Add textual explaination to your sim

$ns at 3.5 "$ns trace$ns at 3.5 "$ns trace--annotate annotate \\“packet drop“packet drop\\"“"“

Set animation rate

$ns at 0.0 "$ns set$ns at 0.0 "$ns set--animationanimation--rate 0.1ms"rate 0.1ms"