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

34
1 ns-2 The Network Simulator Fundamentals Downloads and further info at: http://www.isi.edu/nsnam/ns

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

Page 1: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

11

ns-2 The Network Simulator Fundamentals

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

Page 2: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

22

ns Primer

Basic nsn Architecturen Basic Tcl, OTcln Elements of ns

Page 3: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

33

ns Architecture

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

Modular approachn Fine-grained object composition

Page 4: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

44

Object-Oriented

+ Reusability+ Maintenance

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

Page 5: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 6: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

66

OTcl and C++: The Duality

C++ OTcl

Pure C++objects

Pure OTclobjects

C++/OTcl split objects

ns

Page 7: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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++

Page 8: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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%

Page 9: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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%

Page 10: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 11: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 12: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 13: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 14: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 15: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 16: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

1616

Tracing

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

Page 17: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 18: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 19: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 20: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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”

Page 21: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 22: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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]

Page 23: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 24: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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]

Page 25: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 26: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 27: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

2727

nam

Basic visualizationn Topology layoutn Animation controln Synchronous replay

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

Page 28: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

2828

nsànam Interface

ColorNode manipulationLink manipulationTopology layoutProtocol stateMisc

Page 29: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 30: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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\\””””

Page 31: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 32: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 33: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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

Page 34: ns-2 The Network Simulator Fundamentals - polito.it · 3 ns Architecture Object-oriented (C++, OTcl) Scalability + Extensibility nControl/”data” separation nSplit C++/OTcl object

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"