27
Ns-2 Network Simulator An Introduction

Ns 2 Network Simulator An Introduction

Embed Size (px)

DESCRIPTION

Ns 2 Network Simulator An Introduction

Citation preview

Page 1: Ns 2 Network Simulator An Introduction

Ns-2 Network Simulator An Introduction

Page 2: Ns 2 Network Simulator An Introduction

Introduction[1] Ns-2 is a discrete event simulator

targeted at network research Focused on modeling network

protocols wired, wireless, satellite TCP, UDP, multicast, unicast web, telnet, ftp ad hoc routing, sensor networks

Page 3: Ns 2 Network Simulator An Introduction

Ns-2 is a discrete event driven simulation Physical activities are translated to events Events are queued and processed in the order of their scheduled

occurrences Time progresses as the events are processed

1 2

Time: 1.5 sec Time: 1.7 sec

Time: 1.8 secTime: 2.0 sec

Introduction[2]

Page 4: Ns 2 Network Simulator An Introduction

Components of ns ns, the simulator itself nam, the Network Animator

visualize ns (or other) output GUI input simple ns scenarios

Pre-processing: traffic and topology generators

Post-processing: simple trace analysis, often in Awk, Perl, or

Tcl

Page 5: Ns 2 Network Simulator An Introduction

ns Software Structure: C++ and Otcl Uses two languages C++ for packet-processing

fast to run, detailed, complete control OTcl for control

simulation setup, configuration, occasional actions

fast to write and change pros: trade-off running vs. writing speed,

powerful/documented config language cons: two languages to learn and debug in

Page 6: Ns 2 Network Simulator An Introduction

Event Driven Simulation

TX Pkt Event @ 1.5sec

Node 1 Module

Node 2 Module

TX Pkt Event @ 1.5sec

RX Pkt Event @ 1.7sec

RX Pkt Event @ 1.7sec

TX Ack Event @ 1.8sec

TX Ack Event @ 1.8sec

RX Ack Event @ 2.0sec

RX Ack Event @ 2.0sec

Event Queue

Simulation Finished!

Page 7: Ns 2 Network Simulator An Introduction

Simplified User’s View of NS-2

Page 8: Ns 2 Network Simulator An Introduction

NS-2 programming Create the event scheduler Turn on tracing Creating network

Computing routes and Setup routing - rtproto Creating transport connections – Agents Creating traffic – Applications

Transmit application-level data Monitoring

Page 9: Ns 2 Network Simulator An Introduction

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 10: Ns 2 Network Simulator An Introduction

Tracing Trace packets on all links #Open the NAM trace file #Open the Trace

file set nf [open out.nam w] set tf [open out.tr w] $ns namtrace-all $nf $ns trace-all $tf

Must appear immediately after creating scheduler

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

Page 11: Ns 2 Network Simulator An Introduction

Creating Network

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

n0 n1

Page 12: Ns 2 Network Simulator An Introduction

Creating Network

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

<delay> <queue_type> <bandwidth>: 1000b, 1kb, 0.001Mb,

… <delay>: 1ms, 0.001s, … <queue_type>: DropTail, RED, CBQ,

FQ, SFQ, DRR.n0 n1

Page 13: Ns 2 Network Simulator An Introduction

Creating Connection – Agents

UDP set src [new Agent/UDP] set rcv [new Agent/Null] $ns connect $src $rcv

TCP set tcp [new Agent/TCP] set tcpsink [new Agent/TCPSink] $ns connect $tcp $tcpsink

Page 14: Ns 2 Network Simulator An Introduction

Creating Connection – Agents (Contd)

TCP TCPsink• one-way Tahoe implementation• other flavors: TCP/Reno, TCP/NewReno,

TCP/Sack1,• TCP/Vegas• two-way: use FullTCP at both sides

TCP-friendly connectionless protocols:• RAP <-> RAP Rate Adaptation Protocol (rate-

based AIMD + Slow Start)• TFRC <-> TFRCSink• TCP Friendly Rate Control protocol (based on

TCP throughput-equation)

Page 15: Ns 2 Network Simulator An Introduction

Creating Traffic – Applications Use app on Top of agent: $app start $app stop FTP

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

Telnet set telnet [new Application/Telnet]

Web set session [new httpSession $ns <numPages>

<clientNode>] Traffic generator

set cbr [new Application/traffic/cbr]

Page 16: Ns 2 Network Simulator An Introduction

Computing Routes Unicast

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

Multicast Simulator set EnableMcast_ 1 Simulator set NumberInterfaces_ 1 $ns mrtproto <type> <type>: CtrMcast, DM, dynamicDM,

pimDM

Page 17: Ns 2 Network Simulator An Introduction

Example 1: TCP (FTP from n0 to n1)

set ns [new Simulator]

#Create 2 nodes

set n0 [$ns node]

set n1 [$ns node]

n0 n1

set ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 0.2 "$ftp start"$ns at 1.2 ”exit"$ns run

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

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 18: Ns 2 Network Simulator An Introduction

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 19: Ns 2 Network Simulator An Introduction

Screenshots

Page 20: Ns 2 Network Simulator An Introduction

Tcl Basics Variable substitution

i: the character ‘i’. $i: the variable i.

Arithmetic Expressions set value [expr $v1 + $v2]

Operation substitution set i [expr 5 + 6]

11 Printing

puts $filename “string” (default is stdout) Control Structures

if {condition} then {…….} for {set i 0} {$i < 10} {incr i 2} {……}

Procedures proc proc_name {arg1 arg2…} { ……}

Page 21: Ns 2 Network Simulator An Introduction

NS Models Traffic models and Applications:

Web, FTP, Telnet, constant-bit rate(CBR), real audio Transport protocols:

unicast: TCP, UDP Multicast: SRM, CtrMcast, DM, dynamicDM, pimDM

Routing and queuing: Wired routing, ad hoc routing and directed diffusion queuing protocols: RED, drop-tail, etc

Physical media: Wired (point-to-point, LANs), wireless (multiple propagation models), satellite

Page 22: Ns 2 Network Simulator An Introduction

Example 1 (contd.) – Basic tracing

#Create a simulator object

set ns [new Simulator]

#Open the nam trace file

set nf [open out.nam w]

# Start tracing

$ns namtrace-all $nf

#Open the general trace file

set tf [open out.tr w]

# Start tracing

$ns trace-all $tf

#Define a 'finish' procedure

proc finish {} {

global ns nf tf

$ns flush-trace

#Close the trace files

close $nf

close $tf

#Execute nam on the trace file

exec nam out.nam &

exit 0

}

# Say when to stop simulation

$ns at 5.0 "finish“

#Run the simulation

$ns run

Page 23: Ns 2 Network Simulator An Introduction

Example 2 – Routing (UDP)#Create a simulator object

#Define different colors for data flows

$ns color 1 Blue$ns color 2 Red

#Open the nam trace file# as in example 1

#Define a 'finish' procedure# as in example 1

#Create four nodesset n0 [$ns node]… …set n3 [$ns node]

#Create links between the nodes# only one sample line as in ex 1

# Set orientation of the links$ns duplex-link-op $n0 $n2 orient

right-down…

#Monitor (Visualize) the queue for the # link between node 2 and node 3

$ns duplex-link-op $n2 $n3 queuePos 0.5

n0

n1n2 n3

cbr0

cbr1

null0

UDPUDP

UDP

Page 24: Ns 2 Network Simulator An Introduction

Example 2 – Routing (Contd)#Create a UDP agent and attach it

to node n1set udp0 [new Agent/UDP]$udp0 set class_ 1$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0

set cbr0 [new Application/Traffic/CBR]

$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0

# Create a CBR traffic source cbr1 and udp1 and attach cbr1 to udp1

……#Create a Null agent (a traffic sink)

and attach it to node n3set null0 [new Agent/Null]$ns attach-agent $n3 $null0

#Connect the traffic sources with the traffic sink

$ns connect $udp0 $null0 $ns connect $udp1 $null0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"$ns at 1.0 "$cbr1 start"$ns at 4.0 "$cbr1 stop"$ns at 4.5 "$cbr0 stop"#Call the finish procedure after 5

seconds of simulation time$ns at 5.0 "finish"

#Run the simulation$ns run

Page 25: Ns 2 Network Simulator An Introduction

Example 3: Ring topology with link failure#Create a simulator object

#Tell the simulator to use dynamic routing

$ns rtproto DV

#Open the nam trace file# as in example 1

#Define a 'finish' procedure# as in example 1#Create seven nodes in a loopfor {set i 0} {$i < 7} {incr

i} { set n($i) [$ns node]}

#Create links between the nodes

for {set i 0} {$i < 7} {incr i} { $ns duplex-link $n($i)

$n([expr ($i+1)%7]) 1Mb 10ms DropTail

}

#Create a UDP agent udp0 and attach it to node n(0)

set udp0 [new Agent/UDP]$ns attach-agent $n(0) $udp0

Page 26: Ns 2 Network Simulator An Introduction

Example 3 (Contd.) – link failure

# Create a CBR traffic source and attach it to udp0

……

#Create a Null agent (a traffic sink) and attach it to node n(3)

#Connect the traffic source with the traffic sink

#Schedule events for the CBR agent and the network dynamics

$ns at 0.5 "$cbr0 start“

$ns rtmodel-at 1.0 down $n(1) $n(2)$ns rtmodel-at 2.0 up $n(1) $n(2)$ns at 4.5 "$cbr0 stop“

#Call the finish procedure after 5 #seconds of simulation time$ns at 5.0 "finish"

#Run the simulation$ns run

Page 27: Ns 2 Network Simulator An Introduction

How to Start ns? 1. Open terminal window in linux 2. To run ns type

$ ns file1.tcl Ns2 can be accessed from 1101 IPC

Lab.

Note: Wednesday 14/10/08 at 5:00 PM a practice lab session will be conducted in IPC @ 1101