47
1 Introduction to Ns-2 Zhibin WU WINLAB, ECE Dept. Rutgers U. [email protected]

Ns2 Introduction

Embed Size (px)

Citation preview

Page 1: Ns2 Introduction

1

Introduction to Ns-2

Zhibin WUWINLAB, ECE Dept. Rutgers [email protected]

Page 2: Ns2 Introduction

2

Goals Understanding NS-2 Hands-on Experience with NS2

Ns-2 by Example Write your own scripts

Extending NS2 Implementing new functionality

Page 3: Ns2 Introduction

3

Schedule Presentation (60 min) Group Assignments (10 min) Practices (1 Hr) Q & A session (~15 min)

Page 4: Ns2 Introduction

4

Talk Overview What is ns-2? (the evolution) Architecture Basic Tcl/Otcl commands Elements of an ns-2 simulation Example Online Resources &

Documentation

Page 5: Ns2 Introduction

5

What is ns?

A discrete event, packet-level simulator Targeted at networking research Supports the simulation of

intserv/diffserv, Multicast, Transport, Applications, Wireless (fixed, mobile, satellite)

REAL variant (1989)DARPA (LBL, Xerox PARC, UCB, and USC/ISI) (1995)

Ns-3 Project (Ongoing)

Page 6: Ns2 Introduction

6

Status ns-2

100K lines of C++ 70K lines of OTcl 50K+ lines of test suite, examples, docs

Platforms Most UNIX and UNIX-like systems (FreeBSD,

Linux, Sun Solaris) Window 95/98/NT with Cygwin (Emulation only for FreeBSD for now)

Page 7: Ns2 Introduction

7

Remember! A simulator model of a real-world

system is necessarily a simplification. For example, in simulating TCP No SYN/FIN Equal size segments No variable window advertisement

Bugs: “Users of ns are responsible for verifying for themselves that their simulations are not invalidated by bugs”.

Page 8: Ns2 Introduction

8

Architecture: Object-Oriented C++ for “data”

Per packet action OTcl for control

Periodic or triggered action Modularity (+), re-usability(+),

scalability(+) Speed(-), memory(-)

Page 9: Ns2 Introduction

9

OTcl and C++: The Duality

C++ OTcl

Pure C++objects

Pure OTclobjects

C++/OTcl split objects

ns

Page 10: Ns2 Introduction

10

Script in interactive modelinux21% ns

% set ns [new Simulator]

_o3

% $ns at 1 “puts \“Hello World!\””

1

% $ns at 1.5 “exit”

2

% $ns run

Hello World!

linux21%

Page 11: Ns2 Introduction

11

A script in batch mode

#simple.tclset ns [new Simulator]

$ns at 1 “puts \“Hello World!\””

$ns at 1.5 “exit”

$ns run

linux21% ns simple.tcl

Hello World!

linux21%

Page 12: Ns2 Introduction

12

Basic Tclset a 43set b 27proc 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 13: Ns2 Introduction

13

Basic OTclClass 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 Introduction

SIMULATE WIRED NETWORK

14

Page 15: Ns2 Introduction

15

Elements of ns-2 Simulation Create the event scheduler [Turn on tracing] Create network Setup routing Insert errors Create transport connection Create traffic Transmit application-level data

Page 16: Ns2 Introduction

16

Creating Event Scheduler Create event scheduler

set ns [new Simulator] Schedule events

$ns at <time> <event> <event>: any legitimate ns/tcl

commands Start scheduler

$ns run

Page 17: Ns2 Introduction

17

Tracing

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

<event> <time> <from> <to> <pkt> <size> -- <fid> <src> <dst> <seq> <attr><event> <time> <from> <to> <pkt> <size> -- <fid> <src> <dst> <seq> <attr>+ 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0+ 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0- 1 0 2 cbr 210 ------- 0 0.0 3.1 0 0r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0r 1.00234 0 2 cbr 210 ------- 0 0.0 3.1 0 0

Trace packets on all links in nam format $ns namtrace-all [open test.nam w]

Must appear immediately after creating scheduler

Page 18: Ns2 Introduction

18

Tracing Turn on tracing on specific links

$ns trace-queue $n0 $n1 $ns namtrace-queue $n0 $n1

Page 19: Ns2 Introduction

19

Creating Network Nodes

set n0 [$ns node] set n1 [$ns node]

Links and queuing $ns duplex-link $n0 $n1

<bandwidth> <delay> <queue_type>

<queue_type>: DropTail, RED, CBQ, FQ, SFQ, DRR

Page 20: Ns2 Introduction

20

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 21: Ns2 Introduction

21

Inserting Packet 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 22: Ns2 Introduction

22

Network Dynamics Link failures

Hooks in routing module to reflect routing changes

Four models$ns rtmodel-at <time> up|down $n0 $n1$ns rtmodel-at <time> up|down $n0 $n1$ns rtmodel Trace <config_file> $n0 $n1$ns rtmodel Trace <config_file> $n0 $n1$ns rtmodel Exponential {<params>} $n0 $n1$ns rtmodel Exponential {<params>} $n0 $n1$ns rtmodel Deterministic {<params>} $n0 $n1$ns rtmodel Deterministic {<params>} $n0 $n1

Parameter list[<start>] <up_interval> <down_interval> [<finish>][<start>] <up_interval> <down_interval> [<finish>]

Page 23: Ns2 Introduction

23

Setup Routing Unicast

$ns rtproto <type> <type>: Static, Session, DV, cost, multi-

path Multicast

$ns multicast (right after [new Simulator]) or set ns [new Simulator –multicast on]

$ns mrtproto <type> <type>: CtrMcast, DM, ST, BST

(centralized,dense mode, shared tree

Page 24: Ns2 Introduction

24

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 25: Ns2 Introduction

25

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 26: Ns2 Introduction

26

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 27: Ns2 Introduction

27

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 $tcp

Page 28: Ns2 Introduction

28

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 (native!) inter-packet time (msec) and packet size

(byte)

Page 29: Ns2 Introduction

29

Application-Level Simulation Features

Build on top of existing transport protocol

Transmit user data, e.g., HTTP header Two different solutions

TCP: Application/TcpApp UDP: Agent/Message

Page 30: Ns2 Introduction

30

Script Structure for Wired Scenario

# parameters and options# parameters and options

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

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

# Create topology# Create topology

# Setup packet loss, link dynamics# Setup packet loss, link dynamics

# Create routing agents# Create routing agents

# Create: # Create:

# - protocol agents# - protocol agents

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

# Post-processing procs# Post-processing procs

# Start simulation# Start simulation

Page 31: Ns2 Introduction

SIMULATE WIRELESS NETWORK

31

Page 32: Ns2 Introduction

32

Script Structure: Wireless

# parameters and options# parameters and options

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

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

# create # create MobileNodeMobileNode object (PHY to layer 3 object (PHY to layer 3 configured) configured)

# Create topology# Create topology

# create mobility# create mobility

# Create Layer 4 and above# Create Layer 4 and above

# - UDP/TCP agents# - UDP/TCP agents

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

# Post-processing procedures# Post-processing procedures

# Start simulation# Start simulation

Page 33: Ns2 Introduction

33

Example: Wireless Scenario 4x4 grid

240m

4 7

8 11

13 14

2 3

11

Page 34: Ns2 Introduction

34

Example: Step 1

Define Parameters

set cbr_size 500set cbr_size 500set cbr_interval 0.002set cbr_interval 0.002set num_row 4set num_row 4set time_duration 100set time_duration 100

Page 35: Ns2 Introduction

35

Example: Step 2

Protocol Optionsset val(chan) Channel/WirelessChannel ;# channel set val(chan) Channel/WirelessChannel ;# channel typetypeset val(prop) Propagation/TwoRayGround ;# radio-set val(prop) Propagation/TwoRayGround ;# radio-propagation modelpropagation modelset val(netif) Phy/WirelessPhy ;# network interface set val(netif) Phy/WirelessPhy ;# network interface typetypeset val(mac) Mac/802_11 ;# MAC typeset val(mac) Mac/802_11 ;# MAC typeset val(ifq) Queue/DropTail/PriQueue ;# interface set val(ifq) Queue/DropTail/PriQueue ;# interface queue typequeue typeset val(ll) LL ;# link layer typeset val(ll) LL ;# link layer typeset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ant) Antenna/OmniAntenna ;# antenna modelset val(ifqlen) 50 ;# max packet in ifqset val(ifqlen) 50 ;# max packet in ifqset val(rp) DSDV ;# routing protocolset val(rp) DSDV ;# routing protocol

Page 36: Ns2 Introduction

36

Example: Step 3 Scheduler, Trace, Topo, God

# # # Initialize ns# Initialize ns##set ns_ [new Simulator]set ns_ [new Simulator]set tracefd [open simple.tr w]set tracefd [open simple.tr w]$ns_ trace-all $tracefd$ns_ trace-all $tracefd

# set up topography object# set up topography object set topo [new Topography]set topo [new Topography] $topo load_flatgrid 1000 1000$topo load_flatgrid 1000 1000

create-god [expr $num_row * $num_row ]create-god [expr $num_row * $num_row ]

Page 37: Ns2 Introduction

37

Example: Step 4 Create Node Object with protocols

$ns_ node-config -adhocRouting $val(rp) -llType $val(ll) \$ns_ node-config -adhocRouting $val(rp) -llType $val(ll) \

-macType $val(mac) -ifqType $val(ifq) \-macType $val(mac) -ifqType $val(ifq) \

-ifqLen $val(ifqlen) -antType $val(ant) \-ifqLen $val(ifqlen) -antType $val(ant) \

-propType $val(prop) -phyType $val(netif) \-propType $val(prop) -phyType $val(netif) \

-channel $chan1 -topoInstance $topo \-channel $chan1 -topoInstance $topo \

-agentTrace ON -routerTrace OFF\-agentTrace ON -routerTrace OFF\

-macTrace ON \-macTrace ON \

-movementTrace OFF-movementTrace OFF

for {set i 0} {$i < [expr $num_row*$num_row]} {incr i} {for {set i 0} {$i < [expr $num_row*$num_row]} {incr i} {

set node_($i) [$ns_ node]set node_($i) [$ns_ node]

}}

Page 38: Ns2 Introduction

38

Example: Step 5 Create Topologyset k 0;set k 0;

while {$k < $num_row } {while {$k < $num_row } {

for {set i 0} {$i < $num_row } {incr i} {for {set i 0} {$i < $num_row } {incr i} {

set m [expr $i+$k*$num_row];set m [expr $i+$k*$num_row];

$node_($m) set X_ [expr $i*240];$node_($m) set X_ [expr $i*240];

$node_($m) set Y_ [expr $k*240+20.0];$node_($m) set Y_ [expr $k*240+20.0];

$node_($m) set Z_ 0.0$node_($m) set Z_ 0.0

}}

incr k;incr k;

}; };

Page 39: Ns2 Introduction

Example: Step 6 Create Mobility

#Move node 11 from its original place to top-right corner#Move node 11 from its original place to top-right corner

$ns_ at 60.0 "$node_(11) setdest 990.0 990.0 15.0”$ns_ at 60.0 "$node_(11) setdest 990.0 990.0 15.0”

Page 40: Ns2 Introduction

40

Example: Step 7 Set up transport layer (UDP)

for {set i 0} {$i < $num_row } {incr i} {for {set i 0} {$i < $num_row } {incr i} {

set udp_($i) [new Agent/UDP]set udp_($i) [new Agent/UDP] set null_($i) [new Agent/Null]set null_($i) [new Agent/Null] } } $ns_ attach-agent $node_(8) $udp_(0)$ns_ attach-agent $node_(8) $udp_(0) $ns_ attach-agent $node_(4) $udp_(1)$ns_ attach-agent $node_(4) $udp_(1) $ns_ attach-agent $node_(13) $udp_(2)$ns_ attach-agent $node_(13) $udp_(2) $ns_ attach-agent $node_(14) $udp_(3)$ns_ attach-agent $node_(14) $udp_(3) $ns_ attach-agent $node_(11) $null_(0)$ns_ attach-agent $node_(11) $null_(0) $ns_ attach-agent $node_(7) $null_(1)$ns_ attach-agent $node_(7) $null_(1) $ns_ attach-agent $node_(2) $null_(2)$ns_ attach-agent $node_(2) $null_(2) $ns_ attach-agent $node_(3) $null_(3)$ns_ attach-agent $node_(3) $null_(3) for {set i 0} {$i < $num_row } {incr i} {for {set i 0} {$i < $num_row } {incr i} { $ns_ connect $udp_($i) $null_($i)$ns_ connect $udp_($i) $null_($i) }}

Page 41: Ns2 Introduction

41

Example: Step 8 Define Traffic Scenariofor {set i 0} {$i < $num_row } {incr i} {

set cbr_($i) [new Application/Traffic/CBR]$cbr_($i) set packetSize_ $cbr_size$cbr_($i) set interval_ 0.5$cbr_($i) attach-agent $udp_($i)

}}

$ns_ at 11.0234 "$cbr_(0) start"$ns_ at 10.4578 "$cbr_(1) start" $ns_ at 12.7184 "$cbr_(2) start"$ns_ at 12.2456 "$cbr_(3) start"

Page 42: Ns2 Introduction

42

Example: Step 9 End-of-simulation wrapper (as

usual) # Tell nodes when the simulation ends#for {set i 0} {$i < [expr $num_row*$num_row] } {incr i} {$ns_ at [expr $time_duration +10.0] "$node_($i) reset";}$ns_ at [expr $time_duration +10.0] "finish"$ns_ at [expr $time_duration +10.01] "puts \"NS Exiting...\"; $ns_ halt"

proc finish {} {global ns_ tracefd$ns_ flush-traceclose $tracefd}

puts "Starting Simulation..."$ns_ run

Page 43: Ns2 Introduction

43

Auxiliary Tools setdest

used to generate the positions of nodes and their moving speed and moving directions.setdest -v 1 -n 50 -p 0 -M 20 -t 900 -x 1500 -y 300

cbrgen.tclns cbrgen.tcl [-type cbr|tcp] [-nn nodes] [-seed seed] [-mc connections] [-rate rate]

Use “source <filename>.tcl” to add the generated file to scipts

Page 44: Ns2 Introduction

44

Resources http://www.isi.edu/nsnam/ns http://www.winlab.rutgers.edu/~zhibinwu Tutorials:

Marc Greis’s Tutorial (http://www.isi.edu/nsnam/ns/tutorial/index.html)

Ns by example (http://nile.wpi.edu/NS/) Wireless Tutorial (

http://www.isi.edu/nsnam/ns/ns-tutorial/wireless.ppt )

Page 45: Ns2 Introduction

45

Documentation Tcl (Tool Command Language)

http://dev.scriptics.com/scripting OTcl (MIT Object Tcl)

~otcl/doc/tutorial.html (in distribution)

ns manual Included in distribution: ~ns/doc http://www.isi.edu/~salehi/ns_doc.ps.

gz

Page 46: Ns2 Introduction

Advanced Topics Trace analysis Architecture of Mobilenode Object Extending NS-2 with new protocols

and algorithms More complex changes:

Hybrid Node A node with multiple interfaces

46

Page 47: Ns2 Introduction

Group Assignments Download simple.tcl

http://www.winlab.rutgers.edu/~zhibinwu/simple.tcl

Modifications Node-Configure changes Random topology generation 40-node

within 1000x1000 area Random Node Mobility Changeprotocol from UDP to TCP Dynamic Traffic change

47