1 ns-2 tutorial kumar viswanath & ignacio solis uc santa cruz

123
1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

Post on 19-Dec-2015

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

11

ns-2 Tutorial

Kumar Viswanath & Ignacio SolisUC Santa Cruz

Page 2: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

22UC Santa Cruz

What is NS

Discrete event simulatorPacket-levelLink layer and upWired and wireless

Page 3: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

33UC Santa Cruz

History and Status

Columbia NESTUCB REALns-1ns-2 100K lines of C++ 70K lines of OTcl 30K lines of test suite 20K lines of documentation

Page 4: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

44UC Santa Cruz

Platforms

Most UNIX and UNIX-like systems1 FreeBSD or *BSD2 Linux3 Sun Solaris4 HP, SGI

Window 95/98/NT Some work, some does not

(Emulation only for FreeBSD for now)

Page 5: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

55UC Santa Cruz

Related Research

Routing Unicast (DV, link state), multicast

intserv/diffservTransport TCP Congestion control Reliable multicast

Application Web caching Multimedia

Page 6: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

66UC Santa Cruz

Installation

Getting the piecesTcl/TK 8.x (8.0.5 preferred): http://dev.scriptics.com OTcl, TclCL, ns-2, nam-1:

http://www.isi.edu/nsnam/dist

Other utilities http://www.isi.edu/nsnam/ns/ns-build.ht

ml

Tcl-debug, GT-ITM, xgraph,

Page 7: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

77UC Santa Cruz

Getting Help

ns-2 build questionshttp://www.isi.edu/nsnam/ns/ns-build.html [email protected] (previously

[email protected]

) [email protected] subscribe ns-users in body Archive: http://www.isi.edu/nsnam/ns

Page 8: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

88UC Santa Cruz

Resources

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 9: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

99UC Santa Cruz

ns Architecture

Object-oriented (C++, OTcl)Scalability + Extensibility Control/data separation Split C++/OTcl object

Modular approach Fine-grained object composition

Page 10: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1010UC Santa Cruz

Object-Oriented

ReusabilityMaintenance

Performance (speed and memory)Careful planning of modularity

Page 11: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1111UC Santa Cruz

C++ and OTcl Separation

C++ for data Per packet action

OTcl for control Periodic or triggered action

Compromise between composibility and speedLearning and debugging

Page 12: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1212UC Santa Cruz

OTcl and C++: The Duality

C++ OTcl

Pure C++objects

Pure OTclobjects

C++/OTcl split objects

ns

Page 13: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1313UC Santa Cruz

Elements of ns-2

Create the event scheduler[Turn on tracing]Create networkSetup routingInsert errorsCreate transport connectionCreate trafficTransmit application-level data

Page 14: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1414UC Santa Cruz

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 15: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1515UC Santa Cruz

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 16: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1616UC Santa Cruz

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 17: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1717UC Santa Cruz

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 18: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1818UC Santa Cruz

Setup Routing

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 19: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

1919UC Santa Cruz

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 20: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2020UC Santa Cruz

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 21: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2121UC Santa Cruz

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 22: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2222UC Santa Cruz

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 23: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2323UC Santa Cruz

Application/TcpApp

Step 1: FullTcp connectionset tcp1 [new Agent/TCP/FullTcp]set tcp1 [new Agent/TCP/FullTcp]

set tcp2 [new Agent/TCP/FullTcp]set tcp2 [new Agent/TCP/FullTcp]

$ns attach-agent $n1 $tcp1$ns attach-agent $n1 $tcp1

$ns attach-agent $n2 $tcp2$ns attach-agent $n2 $tcp2

$ns connect $tcp1 $tcp2$ns connect $tcp1 $tcp2

$tcp2 listen$tcp2 listen

Page 24: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2424UC Santa Cruz

Summary: Generic Script Structure

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:

# - multicast groups# - multicast groups

# - 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 25: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2525UC Santa Cruz

ns Wired World

Basic nsA complete example Multicast routing

Visualization

Page 26: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2626UC Santa Cruz

Sample Nam

Page 27: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2727UC Santa Cruz

Nam Running (1)

Page 28: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2828UC Santa Cruz

Nam Running (2)

Page 29: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

2929UC Santa Cruz

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’ up, dude?”}

set mom [new Mom]

$mom set age_ 45

set kid [new Kid]

$kid set age_ 15

$mom greet

$kid greet

Page 30: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3030UC Santa Cruz

G2time1.3s G2time1.2s

G1G2

time1.35s

Example: Multicast Routing

Dynamic group membership under Dense Mode

n0 n1

n2

n3

1.5Mb, 10ms

1.5Mb, 10ms

G1time1.25s

G2

1.5Mb, 10ms

Page 31: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3131UC Santa Cruz

Multicast: Step 1

Scheduler, tracing, and topology

# Create scheduler# Create scheduler

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

# Turn on multicast# Turn on multicast

$ns multicast$ns multicast

# Turn on Tracing# Turn on Tracing

set fd [new “set fd [new “cast.nam”cast.nam”w]w]$ns namtrace-all $fd$ns namtrace-all $fd

Page 32: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3232UC Santa Cruz

Multicast: Step 2

Topology

# Create nodes# Create nodes

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

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

set n2 [$ns node]set n2 [$ns node]

set n3 [$ns node]set n3 [$ns node]

# Create links# Create links

$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail$ns duplex-link $n0 $n1 1.5Mb 10ms DropTail

$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail$ns duplex-link $n0 $n2 1.5Mb 10ms DropTail

$ns duplex-link $n0 $n3 1.5Mb 10ms DropTail$ns duplex-link $n0 $n3 1.5Mb 10ms DropTail

Page 33: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3333UC Santa Cruz

Multicast: Step 3

Routing and group setup

# Routing protocol: let’# Routing protocol: let’ run distance vector run distance vector

$ns mrtproto DM$ns mrtproto DM

# Allocate group addresses# Allocate group addresses

set group1 [Node allocaddr]set group1 [Node allocaddr]

set group2 [Node allocaddr]set group2 [Node allocaddr]

Page 34: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3434UC Santa Cruz

Multicast: Step 4

Sender 0

# Transport agent for the traffic source# Transport agent for the traffic sourceset uset udp0 dp0 [new Agent/UDP][new Agent/UDP]$ns attach-agent $n$ns attach-agent $n1 1 $u$udp0dp0$u$udp0 dp0 set dst_addr_ $gset dst_addr_ $group1roup1$u$udp0 dp0 set dst_port_ 0set dst_port_ 0

# Constant Bit Rate source #0 # Constant Bit Rate source #0 set cset cbr0 br0 [new Application/Traffic/CBR][new Application/Traffic/CBR]$c$cbr0 br0 attach-agent $uattach-agent $udp0dp0# Start at time 1.0 second# Start at time 1.0 second$ns at 1.0 "$c$ns at 1.0 "$cbr0 br0 start"start"

Page 35: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3535UC Santa Cruz

Multicast: Step 5

Sender 1

# Transport agent for the traffic source# Transport agent for the traffic sourceset uset udp1 dp1 [new Agent/UDP][new Agent/UDP]$ns attach-agent $n$ns attach-agent $n3 3 $u$udp1dp1$u$udp1 dp1 set dst_addr_ $gset dst_addr_ $group2roup2$u$udp1 dp1 set dst_port_ 0set dst_port_ 0

# Constant Bit Rate source #0 # Constant Bit Rate source #0 set cset cbr1 br1 [new Application/Traffic/CBR][new Application/Traffic/CBR]$c$cbr1 br1 attach-agent $uattach-agent $udp1dp1# Start at time 1.1 second# Start at time 1.1 second$ns at 1.1 "$c$ns at 1.1 "$cbr1 br1 start"start"

Page 36: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3636UC Santa Cruz

Multicast: Step 6

Receiver with dynamic membership

# Can also be Agent/Null# Can also be Agent/Null

set rcvr [new Agent/LossMonitor]set rcvr [new Agent/LossMonitor]

# Assign it to node $n2# Assign it to node $n2

$ns at 1$ns at 1.2 .2 "$n2 join-group $rcvr $g"$n2 join-group $rcvr $group2"roup2"

$ns at 1$ns at 1.25 .25 "$n2 leave-group $rcvr $g"$n2 leave-group $rcvr $group2"roup2"

$ns at 1$ns at 1.3 .3 "$n2 join-group $rcvr $g"$n2 join-group $rcvr $group2"roup2"

$ns at 1$ns at 1.35 .35 "$n2 join-group $rcvr $g"$n2 join-group $rcvr $group1"roup1"

Page 37: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3737UC Santa Cruz

Multicast: Step 7

End-of-simulation wrapper (as usual)

$ns at 2.0 "finish"$ns at 2.0 "finish"proc finish {} {proc finish {} {global ns fdglobal ns fdclose $fdclose $fd$ns flush-trace$ns flush-traceputs "running nam..."puts "running nam..."exec nam out.nam &exec nam out.nam &exit 0exit 0}}$ns run$ns run

Page 38: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3838UC Santa Cruz

Other Examples

Available in the Lab this afternoonWeb traffic modelMulticast routingREDQueueing

Page 39: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

3939UC Santa Cruz

ns Wired World

Basic nsTwo examples TCP, multicast routing

Visualization

Page 40: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4040UC Santa Cruz

Visualization Tools

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

xgraph Conversion from ns trace to xgraph

format

Page 41: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4141UC Santa Cruz

nam

Basic visualization Topology layout Animation control Synchronous replay

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

Page 42: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4242UC Santa Cruz

nsnam Interface

ColorNode manipulationLink manipulationTopology layoutProtocol stateMisc

Page 43: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4343UC Santa Cruz

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 44: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4444UC Santa Cruz

nam Interface: Nodes

Color$node color red$node color red

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

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

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

Page 45: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4545UC Santa Cruz

nam Interfaces: Links

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

Label$ns duplex-link-op $n0 $n1 label "abced"$ns duplex-link-op $n0 $n1 label "abced"

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

Asymmetric links not allowed

Page 46: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4646UC Santa Cruz

nam Interface: Topo Layout

Manual layout: specify everything

$ns duplex-link-op $n(0) $n(1) orient right$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(1) $n(2) orient right

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

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

If anything missing automatic layout

Page 47: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4747UC Santa Cruz

nam Interface: Protocol State

Monitor values of agent variables$ns add-agent-trace $srm0 srm_agent0$ns add-agent-trace $srm0 srm_agent0

$ns monitor-agent-trace $srm0$ns monitor-agent-trace $srm0

$srm0 tracevar C1_$srm0 tracevar C1_

$srm0 tracevar C2_$srm0 tracevar C2_

# # $ns delete-agent-trace $tcp1$ns delete-agent-trace $tcp1

Page 48: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4848UC Santa Cruz

nam Interface: Misc

Annotation Add textual explaination to your sim

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

Set animation rate

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

Page 49: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

4949UC Santa Cruz

Multicast Example: nam-Enhanced

Packet coloringNode colorNode labelLink labelAnnotationManual layoutQueueing

Page 50: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5050UC Santa Cruz

Multicast: Step 1.1

Define nam color

# Colors for packets from two mcast groups# Colors for packets from two mcast groups$ns color 10 blue$ns color 10 blue$ns color 11 red$ns color 11 red

# Prune packets (# Prune packets (predefinedpredefined))$ns color 30 purple$ns color 30 purple# Graft packets# Graft packets$ns color 31 green$ns color 31 green

Page 51: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5151UC Santa Cruz

Multicast: Step 2.1

Layout topology

# Manual layout: # Manual layout: order of the link is significantorder of the link is significant!!

$ns duplex-link-op $n0 $n1 orient right$ns duplex-link-op $n0 $n1 orient right

$ns duplex-link-op $n0 $n2 orient right-up$ns duplex-link-op $n0 $n2 orient right-up

$ns duplex-link-op $n0 $n3 orient right-down$ns duplex-link-op $n0 $n3 orient right-down

# Show queue on simplex link n0->n1# Show queue on simplex link n0->n1

$ns duplex-link-op $n0 $n1 queuePos 0.5$ns duplex-link-op $n0 $n1 queuePos 0.5

Page 52: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5252UC Santa Cruz

Multicast: Step 4.1, 5.1

Source coloring# Group 0# Group 0

$udp0 set fid_ 10$udp0 set fid_ 10

$n1 color blue$n1 color blue

$n1 label $n1 label Source for group 0Source for group 0

# Group 1# Group 1

$udp1 set fid_ 11$udp1 set fid_ 11

$n3 color red$n3 color red

$n3 label $n3 label Source for group 1Source for group 1

Page 53: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5353UC Santa Cruz

Multicast: Step 6.1

Receiver coloring$n2 label $n2 label ReceiverReceiver$ns at 1.2 "$n2 join-group $rcvr $group2; \$ns at 1.2 "$n2 join-group $rcvr $group2; \

$n2 add-mark m0 red"$n2 add-mark m0 red"

$ns at 1.25 "$n2 leave-group $rcvr $group2; \$ns at 1.25 "$n2 leave-group $rcvr $group2; \

$n2 delete-mark m0"$n2 delete-mark m0"

$ns at 1.3 "$n2 join-group $rcvr \ $group2; \$ns at 1.3 "$n2 join-group $rcvr \ $group2; \

$n2 add-mark m1 red"$n2 add-mark m1 red"

$ns at 1.35 "$n2 join-group $rcvr $group1; \$ns at 1.35 "$n2 join-group $rcvr $group1; \

$n2 add-mark m2 blue"$n2 add-mark m2 blue"

Page 54: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5454UC Santa Cruz

Multicast: Step 7.1

One final tweak

# Animation was too fast...# Animation was too fast...

$ns set-animation-rate 0.8ms$ns set-animation-rate 0.8ms

Page 55: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5555UC Santa Cruz

Outline

OverviewOverview

Worlds of ns SimulationWorlds of ns Simulation WiredWired WirelessWireless EmulationEmulation

Supporting Tools Supporting Tools

Extending ns in OTclExtending ns in OTcl

Page 56: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5656UC Santa Cruz

ns Wireless World

Ad hoc routingAd hoc routing

Mobile IPMobile IP

Satellite networkingSatellite networking

Page 57: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5757UC Santa Cruz

Ad Hoc Routing An Example

Scenario 3 mobile nodes moving within 670mX670m flat topology using DSDV ad hoc routing protocol Random Waypoint mobility model TCP and CBR traffic

ns-2/tcl/ex/wireless-demo-csci694.tcl

Page 58: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5858UC Santa Cruz

An Example Step 1# Define Global Variables# create simulatorset ns [new Simulator]

# create a topology in a 670m x 670m areaset topo [new Topography] $topo load_flatgrid 670 670

Page 59: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

5959UC Santa Cruz

An Example Step 2# Define standard ns/nam trace

# ns trace

set tracefd [open demo.tr w]

$ns trace-all $tracefd

# nam trace

set namtrace [open demo.nam w]

$ns namtrace-all-wireless $namtrace 670 670

Page 60: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6060UC Santa Cruz

An Example Step 3

# Create Godset god [create-god 3]$ns at 900.00 $god setdist 2 3 1

God: store an array of the smallest number of hops required to reach one node to an otherOptimal case against which to compare routing protocol performanceAutomatically generated by scenario file

Page 61: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6161UC Santa Cruz

An Example Step 4# Define how a mobile node should be created $ns node-config \

-adhocRouting DSDV \-llType LL \-macType Mac/802_11 \-ifqLen 50 \-ifqType Queue/DropTail/PriQueue \-antType Antenna/OmniAntenna \-propInstance [new Propagation/TwoRayGround] \-phyType Phy/WirelessPhy \-channel [new Channel/WirelessChannel] \-topoInstance $topo-agentTrace ON \-routerTrace OFF \-macTrace OFF

Page 62: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6262UC Santa Cruz

An Example Step 5# Create a mobile node, attach it to the channel

set node(0) [$ns node]# disable random motion $node(0) random-motion 0

Use for loop to create 3 nodes:

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

set node($i) [$ns node]

}

Page 63: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6363UC Santa Cruz

An Example Step 6

# Define node movement model source movement-scenario-files

# Define traffic modelsource traffic-scenario-files

Page 64: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6464UC Santa Cruz

Scenario Generator: Movement

Mobile Movement Generatorsetdest -n <num_of_nodes> -p setdest -n <num_of_nodes> -p pausetime -s <maxspeed> -t pausetime -s <maxspeed> -t <simtime> -x <maxx> -y <maxy><simtime> -x <maxx> -y <maxy>

Random movement $node start$node start Source: ns-2/indep-utils/cmu-ns-2/indep-utils/cmu-scen-gen/setdest/scen-gen/setdest/

Page 65: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6565UC Santa Cruz

A Movement File

$node_(2) set Z_ 0.000000000000$node_(2) set Y_ 199.373306816804$node_(2) set X_ 591.256560093833$node_(1) set Z_ 0.000000000000$node_(1) set Y_ 345.357731779204$node_(1) set X_ 257.046298323157$node_(0) set Z_ 0.000000000000$node_(0) set Y_ 239.438009831261$node_(0) set X_ 83.364418416244$ns_ at 50.000000000000 "$node_(2) setdest 369.463244915743

170.519203111152 3.371785899154"$ns_ at 51.000000000000 "$node_(1) setdest 221.826585497093

80.855495003839 14.909259208114"$ns_ at 33.000000000000 "$node_(0) setdest 89.663708107313

283.494644426442 19.153832288917"

Page 66: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6666UC Santa Cruz

Scenario Generator: Traffic

Generating traffic pattern files CBR traffic

ns cbrgen.tcl [-type cbf|tcp] [-nn ns cbrgen.tcl [-type cbf|tcp] [-nn nodes] [-seed seed] [-mc connections] nodes] [-seed seed] [-mc connections] [-rate rate][-rate rate]

TCP traffic

ns tcpgen.tcl [-nn nodes] [-seed seed]ns tcpgen.tcl [-nn nodes] [-seed seed] Source: ns-2/indep-utils/cmu-scen-gen/ns-2/indep-utils/cmu-scen-gen/

Page 67: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6767UC Santa Cruz

A Traffic Scenario

set udp_(0) [new Agent/UDP]$ns_ attach-agent $node_(0) $udp_(0)set null_(0) [new Agent/Null]$ns_ attach-agent $node_(2) $null_(0)set cbr_(0) [new Application/Traffic/CBR]$cbr_(0) set packetSize_ 512$cbr_(0) set interval_ 4.0$cbr_(0) set random_ 1$cbr_(0) set maxpkts_ 10000$cbr_(0) attach-agent $udp_(0)$ns_ connect $udp_(0) $null_(0)$ns_ at 127.93667922166023 "$cbr_(0) start".

Page 68: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6868UC Santa Cruz

An Example Step 7# Define node initial position in namfor {set i 0} {$i < 3 } { incr i} {

$ns initial_node_position $node($i) 20}

# Tell ns/nam the simulation stop time $ns at 200.0 $ns nam-end-wireless 200.0$ns at 200.0 $ns halt

# Start your simulation $ns run

Page 69: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

6969UC Santa Cruz

Energy Extension

Node is energy-awareDefine node by adding new options:

$ns_ node-config \

–eergyModel EnergyModel

-initialEnergy 100.0

-txPower 0.6

-rxPower 0.2

Page 70: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7070UC Santa Cruz

nam Visualization

Use nam to visualize: Mobile node position Mobile node moving direction and

speed Energy consumption at nodes (color

keyed)

Page 71: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7171UC Santa Cruz

nam Visualization

Replace$ns namtrace-all $fd$ns namtrace-all $fd

with$ns namtrace-all-wireless $fd$ns namtrace-all-wireless $fd

AAt the end of simulation, do$$ns nam-end-wireless [$ns now]ns nam-end-wireless [$ns now]

See an example:

Page 72: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7272UC Santa Cruz

Summary

Mac Layer: IEEE 802.11Address Resolution Protocol (ARP)Ad hoc routing protocols: DSDV, DSR,TORA, AODVRadio Propagation Model Friss-space attenuation at near distances Two ray ground at far distances

Antenna: an omni-directional antenna having unity gain

Page 73: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7373UC Santa Cruz

Summary

Energy consumption model for sensor networksVisualization of node movement, reachability, and energyValidation test suites

Page 74: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7474UC Santa Cruz

A Brief on Satellite Networking

Developed by Tom Henderson (UCB)Supported models Geostationary satellites: bent-pipe

and processing-payload Low-Earth-Orbit satellites

Example: tcl/ex/sat-*.tcltcl/ex/sat-*.tcl

Much in-development

Page 75: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7575UC Santa Cruz

A Brief on MobileIP Support

Developed by Sun Require a different Node structure than the

MobileNode Co-exists with wired world in ns

Standard MobileIP Home Agent, Foreign Agent, MobileHosts

Example ~ns/tcl/ex/wired-cum-wireless.tcl~ns/tcl/ex/wired-cum-wireless.tcl

Page 76: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7676UC Santa Cruz

Outline

OverviewOverview

Worlds of ns SimulationWorlds of ns Simulation WiredWired WirelessWireless EmulationEmulation

Supporting Tools Supporting Tools

Extending ns in OTclExtending ns in OTcl

Page 77: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7777UC Santa Cruz

ns Emulation

Simulator real network Inject received packets into simulation Emit packets on to live network

Usage Subject real implementations to

controlled conditions in the simulator Subject simulations to real-world traffic

Currently only works on FreeBSD

Page 78: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7878UC Santa Cruz

Sample Environment

EmulationMachine

InternetTest

Machine

DHCP for address/routing

Proxy ARP for Test Machine

web server

browser

Page 79: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

7979UC Santa Cruz

real network

Emulation Machine

BPFBPF BPFRAW IP

Simulator

Networkobjects

Network/Pcap/Live

Network/IP

Agent/Tap

Scheduler/RealTime

ns

world

Page 80: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8080UC Santa Cruz

Realtime Scheduler

Extended from Scheduler/ListSynchronizes simulation time to real timeFails when simulation time falls behind$ns use-scheduler RealTime$ns use-scheduler RealTime

Page 81: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8181UC Santa Cruz

Network Objects

Abstraction of real traffic source/sinkBase class for various network types Opened read-only, write-only, or read-write

Raw IP and UDP/IP network object Send/receive raw IP packets or UDP/IP IP multicast support

Pcap network object Send/receive link-layer frames Use BPF/libpcap filtering language

Page 82: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8282UC Santa Cruz

Tap Agents

networkobject

Tapagent

ns packets: to/from other nodes

ns

srcdst

userdata

IPpacketpayload

real network

IP/UDPpackets

Page 83: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8383UC Santa Cruz

Emulation Modes

Protocol mode Simulator interpret/generate live traffic Existing agents: ICMP ECHO, ICMP

Redirect, ARP, TCP NAT

Opaque mode Simulator does not interpret network

data Operations: packet

drop/reordering/delay

Page 84: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8484UC Santa Cruz

Protocol Mode: Ping Responder

n2

n0

n1

100Mb, 1000ms

100Mb, 1000ms

ICMP ECHOresponder

Input tap agent

Output tap agent

real traffic

real Pingapplication

ns

Page 85: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8585UC Santa Cruz

Ping: Step 1

Stage setup

# Create simulator# Create simulatorset ns [new Simulator]set ns [new Simulator]$ns use-scheduler RealTime$ns use-scheduler RealTime

# Emulator address# Emulator addressset me [exec hostname]set me [exec hostname]# Or an arbitrary one (may require ARP support)# Or an arbitrary one (may require ARP support)# set me “# set me “0.11.12.13”0.11.12.13”

Page 86: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8686UC Santa Cruz

Ping: Step 2

Create I/O network objects

## Packet input Packet inputset bpf0 [new Network/Pcap/Live]set bpf0 [new Network/Pcap/Live]$bpf0 set promisc_ true$bpf0 set promisc_ trueset nd0 [$bpf0 open readonly fxp0] set nd0 [$bpf0 open readonly fxp0] set filt “set filt “not ip host $me)”not ip host $me)”$bpf0 filter $filt$bpf0 filter $filt

# Packet output# Packet outputset ipnet [new Network/IP]set ipnet [new Network/IP]$ipnet open writeonly$ipnet open writeonly

Page 87: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8787UC Santa Cruz

Ping: Step 3

Agents# Input agent# Input agentset pfa [new Agent/Tap]set pfa [new Agent/Tap]$pfa network $bpf0$pfa network $bpf0

# Output agent# Output agentset ipa [new Agent/Tap]set ipa [new Agent/Tap]$ipa network $ipnet$ipa network $ipnet

# ICMP ECHO agent# ICMP ECHO agentset echoagent [new Agent/PingResponder]set echoagent [new Agent/PingResponder]

Page 88: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8888UC Santa Cruz

Ping: Step 4

Create network topology

set n0 [$ns node]set n0 [$ns node]set n1 [$ns node]set n1 [$ns node]set n2 [$ns node]set n2 [$ns node]$ns simplex-link $n0 $n2 100Mb 1000ms DropTail$ns simplex-link $n0 $n2 100Mb 1000ms DropTail$ns simplex-link $n2 $n1 100Mb 1000ms DropTail$ns simplex-link $n2 $n1 100Mb 1000ms DropTail

$ns attach-agent $n0 $pfa$ns attach-agent $n0 $pfa$ns attach-agent $n1 $ipa$ns attach-agent $n1 $ipa$ns attach-agent $n2 $echoagent$ns attach-agent $n2 $echoagent$ns simplex-connect $pfa $echoagent$ns simplex-connect $pfa $echoagent$ns simplex-connect $ipa $echoagent$ns simplex-connect $ipa $echoagent

Page 89: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

8989UC Santa Cruz

Ping: Step 5

Start# Wait for ping to come in...# Wait for ping to come in...

$ns run$ns run

Result 2000.052ms ± 1.021ms

Page 90: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9090UC Santa Cruz

Opaque Mode (TCP: 10 packet periodic drop)

Dropped SYN

ACK of SYN+ACK

RetransmittedSYN

Lost

Page 91: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9191UC Santa Cruz

More Examples

~ns/emulate

Example scripts Protocol mode:

~ns/emulate/empaper.tcl Opaque mode: ~ns/emulate/em3.tcl

Page 92: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9292UC Santa Cruz

Outline

OverviewOverview

Worlds of ns SimulationWorlds of ns Simulation WiredWired WirelessWireless EmulationEmulation

Supporting Tools Supporting Tools

Extending ns in OTclExtending ns in OTcl

Page 93: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9393UC Santa Cruz

ns Supporting Tools

Tcl debuggerTopology generationScenario generationWeb cache trace converter

Page 94: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9494UC Santa Cruz

Debugging Your ns Script

tcl-debug 1.9 http://expect.nist.gov/tcl-debug/ Works with Tcl 8.0.4 and below

Installation [make distclean] in ns ./configure --with-tcldebug=<dir> make

Page 95: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9595UC Santa Cruz

Debugging Your ns Script

Using tcl-debug Insert debug 1 into your scripts, e.g.:

set tcp [new Agent/TCP]set tcp [new Agent/TCP]

debug 1debug 1

$tcp set window_ 200$tcp set window_ 200 When debug 1 is executed, ns drops to:

vint/ns-2(121): ./ns t.tclvint/ns-2(121): ./ns t.tcl

2: lappend auto_path $dbg_library2: lappend auto_path $dbg_library

dbg2.0> dbg2.0>

Page 96: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9696UC Santa Cruz

Debugging Your ns Scriptdbg2.0> hdbg2.0> hs [#] step into procedures [#] step into proceduren [#] step over proceduren [#] step over procedureN [#] step over procedures, commands, and argumentsN [#] step over procedures, commands, and argumentsc continuec continuer continue until return to callerr continue until return to calleru [#] move scope up levelu [#] move scope up leveld [#] move scope down leveld [#] move scope down level go to absolute frame if # is prefaced by "#"go to absolute frame if # is prefaced by "#"w show stack ("where")w show stack ("where")w -w [#] show/set widthw -w [#] show/set widthw -c [0|1] show/set compressw -c [0|1] show/set compressb show breakpointsb show breakpointsb [-r regexp-pattern] [if expr] [then command]b [-r regexp-pattern] [if expr] [then command]b [-g glob-pattern] [if expr] [then command]b [-g glob-pattern] [if expr] [then command]b [[file:]#] [if expr] [then command]b [[file:]#] [if expr] [then command] if pattern given, break if command resembles patternif pattern given, break if command resembles pattern if # given, break on line #if # given, break on line # if expr given, break if expr trueif expr given, break if expr true if command given, execute command at breakpointif command given, execute command at breakpointb -# delete breakpointb -# delete breakpointb - delete all breakpointsb - delete all breakpoints

Page 97: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9797UC Santa Cruz

Topology Generation

http://www.isi.edu/nsnam/ns/ns-topogen.html Packages Graphs Edge Method

NTG n-level probabilistic

RTG Flat random Waxman

GT-ITM Flat random, n-

level, Transit-stub various

TIERS 3-level spanning tree

Page 98: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9898UC Santa Cruz

GT-ITM

Installation Comes with ns-allinone Require Knuths cweb and SGB

Usage itm <config_file>

Three graph models Flat random: Waxman n-level hierarchy Transit-stub

Page 99: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

9999UC Santa Cruz

GT-ITM: Transit-Stub Model

stubdomains

transitdomains

transit-transit

link

stub-stub link

Page 100: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

100100UC Santa Cruz

GT-ITM: Example

Transit-stub networkConfig file (e.g., ts1)

# <method keyword> <number of graphs> [<initial seed>]# <method keyword> <number of graphs> [<initial seed>]# <# stubs/trans node> <#rand. t-s edges> <#rand. s-s # <# stubs/trans node> <#rand. t-s edges> <#rand. s-s

edges>edges># {<n> <scale> <edgemethod> <alpha> [<beta>] [<gamma>]}# {<n> <scale> <edgemethod> <alpha> [<beta>] [<gamma>]}# (average!) number of nodes = 1x2x(1+3x4) = 26# (average!) number of nodes = 1x2x(1+3x4) = 26ts 10 47ts 10 47 # 10 graphs, init seed 47# 10 graphs, init seed 473 0 0 3 0 0 # 2 stubs per transit nodes# 2 stubs per transit nodes1 20 3 1.01 20 3 1.0 # n. of transit domains (pure random)# n. of transit domains (pure random)2 20 3 0.52 20 3 0.5 # n. of nodes per transit domain# n. of nodes per transit domain4 10 3 0.54 10 3 0.5 # n. nodes in each stub domain# n. nodes in each stub domain

Page 101: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

101101UC Santa Cruz

GT-ITM: Example

Run itm ts1itm ts1 Result: ts1-{0-9}.gb

Result files in SGB format

Page 102: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

102102UC Santa Cruz

Converters for GT-ITM

sgb2ns Convert SGB format to ns config file sgb2ns <SGB_file> <OTcl_file>sgb2ns <SGB_file> <OTcl_file> ts2nsts2ns: output lists of transit and stub

nodes

sgb2hier Convert transit-stub information into

hierarchical addresses sgb2hierns <SGBFile> <TclFile>sgb2hierns <SGBFile> <TclFile>

Page 103: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

103103UC Santa Cruz

Converters for GT-ITM

Format of generated ns config filesproc create-topology {nsns node linkBW} {proc create-topology {nsns node linkBW} {

upupvar $var $node nnode n

upupvar $var $nsns nsnsns ns

# Create nodes, links, # Create nodes, links,

............

}}

Usagesource <OTcl_file>source <OTcl_file>

create-topology ns nodes 1.5Mbcreate-topology ns nodes 1.5Mb

Page 104: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

104104UC Santa Cruz

See Your Topology

Create an ns wrapper

# Assume you’# Assume you’e done “e done “gb2ns ts1-0.gb ts1.tcl”gb2ns ts1-0.gb ts1.tcl”

source ts1.tclsource ts1.tcl

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

$ns n$ns namtrace-config amtrace-config [open ts1.nam w][open ts1.nam w]

create-topology ns node 1.5Mbcreate-topology ns node 1.5Mb

$ns at 1.0 “$ns at 1.0 “xit 0”xit 0”

$ns run$ns run

Page 105: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

105105UC Santa Cruz

Outline

OverviewOverview

Worlds of ns SimulationWorlds of ns Simulation WiredWired WirelessWireless EmulationEmulation

Supporting Tools Supporting Tools

Extending ns in OTclExtending ns in OTcl

Page 106: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

106106UC Santa Cruz

ns Directory Structure

TK8.0 OTcl tclclTcl8.0 ns-2 nam-1

tcl

ex test lib

...

...

examples validation tests

C++ code

OTcl code

ns-allinone

mcast

Page 107: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

107107UC Santa Cruz

Extending ns in OTcl

If you dont want to compile source your changes in your sim

scripts

Otherwise Modifying code; recompile Adding new files

Change Makefile (NS_TCL_LIB), tcl/lib/ns-lib.tcl

Recompile

Page 108: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

108108UC Santa Cruz

Example: Agent/Message

n0 n1

n4

n5

n2

n3

128Kb, 50ms

10Mb, 1ms 10Mb, 1ms

C Ccrosstraffic

S R

msg agent

Page 109: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

109109UC Santa Cruz

Agent/Message

A UDP agent (without UDP header)Up to 64 bytes user messageGood for fast prototyping a simple ideaUsage requires extending ns functionality

SS RR

pkt: 64 bytesof arbitrary string

Receiver-sideprocessing

Page 110: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

110110UC Santa Cruz

Agent/Message: Step 1

Define senderclass Sender –class Sender –uperclass Agent/Messageuperclass Agent/Message

# Message format: “# Message format: “ddr Op SeqNo”ddr Op SeqNo”

Sender instproc send-next {} {Sender instproc send-next {} {

$self instvar seq_ agent_addr_$self instvar seq_ agent_addr_

$self send “$$self send “$gent_addr_ send $seq_”gent_addr_ send $seq_”

incr seq_incr seq_

global nsglobal ns

$ns at [expr [$ns now]+0.1] "$self send-next"$ns at [expr [$ns now]+0.1] "$self send-next"

}}

Page 111: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

111111UC Santa Cruz

Agent/Message: Step 2

Define sender packet processing

Sender instproc rSender instproc recv ecv msg {msg {

$self instvar agent_addr_$self instvar agent_addr_

set sdr [lindex $msg 0]set sdr [lindex $msg 0]

set seq [lindex $msg 2]set seq [lindex $msg 2]

puts "Sender gets ack $seq from $sdr"puts "Sender gets ack $seq from $sdr"

}}

Page 112: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

112112UC Santa Cruz

Agent/Message: Step 3

Define receiver packet processing

Class Receiver –Class Receiver –uperclass Agent/Messageuperclass Agent/Message

Receiver instproc rReceiver instproc recv ecv msg {msg {

$self instvar agent_addr_$self instvar agent_addr_

set sdr [lindex $msg 0]set sdr [lindex $msg 0]

set seq [lindex $msg 2]set seq [lindex $msg 2]

puts “Rputs “Rceiver gets seq $seq from $sdr”ceiver gets seq $seq from $sdr”

$self send “$$self send “$ddr_ ack $seq”ddr_ ack $seq”

}}

Page 113: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

113113UC Santa Cruz

Agent/Message: Step 4

Scheduler and tracing

# Create scheduler# Create scheduler

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

# Turn on Tracing# Turn on Tracing

set fd [new “set fd [new “essage.nam”essage.nam”w]w]$ns namtrace-all $fd$ns namtrace-all $fd

Page 114: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

114114UC Santa Cruz

Agent/Message: Step 5

Topologyfor {set i 0} {$i < 6} {incr i} {for {set i 0} {$i < 6} {incr i} {set n($i) [$ns node]set n($i) [$ns node]}}$ns duplex-link $n(0) $n(1) 128kb 50ms DropTail$ns duplex-link $n(0) $n(1) 128kb 50ms DropTail$ns duplex-link $n(1) $n(4) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(4) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(5) 10Mb 1ms DropTail$ns duplex-link $n(1) $n(5) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(2) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(2) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(3) 10Mb 1ms DropTail$ns duplex-link $n(0) $n(3) 10Mb 1ms DropTail

$ns queue-limit $n(0) $n(1) 5$ns queue-limit $n(0) $n(1) 5$ns queue-limit $n(1) $n(0) 5$ns queue-limit $n(1) $n(0) 5

Page 115: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

115115UC Santa Cruz

Agent/Message: Step 6

Routing

# Packet loss produced by queueing# Packet loss produced by queueing

# Routing protocol: let’# Routing protocol: let’ run distance vector run distance vector

$ns rtproto DV$ns rtproto DV

Page 116: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

116116UC Santa Cruz

Agent/Message: Step 7

Cross trafficset udp0 [new Agent/UDP]set udp0 [new Agent/UDP]$ns attach-agent $n(2) $udp0$ns attach-agent $n(2) $udp0set null0 [new Agent/NULL]set null0 [new Agent/NULL]$ns attach-agent $n(4) $null0$ns attach-agent $n(4) $null0$ns connect $udp0 $null0$ns connect $udp0 $null0

set exp0 [new Application/Traffic/Exponential]set exp0 [new Application/Traffic/Exponential]$exp0 set rate_ 128k$exp0 set rate_ 128k$exp0 attach-agent $udp0$exp0 attach-agent $udp0$ns at 1.0 “$ns at 1.0 “exp0 start”exp0 start”

Page 117: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

117117UC Santa Cruz

Agent/Message: Step 8

Message agentsset sdr [new Sender]set sdr [new Sender]$sdr set packetSize_ 1000$sdr set packetSize_ 1000

set rcvr [new Receiver]set rcvr [new Receiver]$rcvr set packetSize_ 40$rcvr set packetSize_ 40

$ns attach $n(3) $sdr$ns attach $n(3) $sdr$ns attach $n(5) $rcvr$ns attach $n(5) $rcvr$ns connect $sdr $rcvr$ns connect $sdr $rcvr$ns connect $rcvr $sdr$ns connect $rcvr $sdr$ns at 1.1 “$ns at 1.1 “sdr send-next”sdr send-next”

Page 118: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

118118UC Santa Cruz

Agent/Message: Step 9

End-of-simulation wrapper (as usual)

$ns at 2.0 finish$ns at 2.0 finish

proc finish {} {proc finish {} {

global ns fdglobal ns fd

$ns flush-trace$ns flush-trace

close $fdclose $fd

exit 0exit 0

}}

Page 119: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

119119UC Santa Cruz

Agent/Message: Result

Example output> ./ns msg.tcl> ./ns msg.tclReceiver gets seq 0 from 0Receiver gets seq 0 from 0Sender gets ack 0 from 1Sender gets ack 0 from 1Receiver gets seq 1 from 0Receiver gets seq 1 from 0Sender gets ack 1 from 1Sender gets ack 1 from 1Receiver gets seq 2 from 0Receiver gets seq 2 from 0Sender gets ack 2 from 1Sender gets ack 2 from 1Receiver gets seq 3 from 0Receiver gets seq 3 from 0Sender gets ack 3 from 1Sender gets ack 3 from 1Receiver gets seq 4 from 0Receiver gets seq 4 from 0Sender gets ack 4 from 1Sender gets ack 4 from 1Receiver gets seq 5 from 0Receiver gets seq 5 from 0

Page 120: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

120120UC Santa Cruz

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 121: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

121121UC Santa Cruz

Add Your Change into ns

tcl/lib/ns-lib.tclClass SimulatorClass Simulator……source ../mysrc/msg.tclsource ../mysrc/msg.tcl

MakefileNS_TCL_LIB = \NS_TCL_LIB = \tcl/mysrc/msg.tcl \tcl/mysrc/msg.tcl \…… Or: change Makefile.in, make distcleanmake distclean,

then ./configure --enable-debug./configure --enable-debug

Page 122: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

122122UC Santa Cruz

Recap

OverviewOverview

Worlds of ns SimulationWorlds of ns Simulation WiredWired WirelessWireless EmulationEmulation

Supporting Tools Supporting Tools

Extending ns in OTclExtending ns in OTcl

Page 123: 1 ns-2 Tutorial Kumar Viswanath & Ignacio Solis UC Santa Cruz

123123UC Santa Cruz

Further Information

ns-2 build questions http://www.isi.edu/nsnam/ns/ns-build.

html

[email protected] [email protected] subscribe ns-users in body Search the mailing list archive at

http://www.isi.edu/nsnam/ns