Sie sind auf Seite 1von 27

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

Introduction[2]

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
Time: 1.5 sec Time: 1.7 sec

1
Time: 2.0 sec

2
Time: 1.8 sec

Components of ns

ns, the simulator itself nam, the Network Animator


visualize ns (or other) output GUI input simple ns scenarios traffic and topology generators simple trace analysis, often in Awk, Perl, or Tcl

Pre-processing:

Post-processing:

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

Event Driven Simulation


RX Ack Event TX Pkt Event @ 1.5sec 2.0sec
Event Queue RX Ack Event TX Pkt Event RX TX @ 1.5sec 1.8sec 2.0sec 1.7sec Simulation Finished! TX Ack Event RX Pkt @ 1.7sec 1.8sec Node 2 Module Node 1 Module

Simplified Users View of NS-2

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

Creating Event Scheduler

Create event scheduler

set ns [new Simulator]

Schedule events

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

Start scheduler

Tracing

Trace packets on all links


#Open the NAM trace file

#Open the Trace file


set tf [open out.tr w] $ns trace-all $tf

set nf [open out.nam w] $ns namtrace-all $nf

Must appear immediately after creating scheduler


Turn on tracing on specific links

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

Creating Network

Nodes

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

n0

n1

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

Creating Connection Agents

UDP

set src [new Agent/UDP] set rcv [new Agent/Null] $ns connect $src $rcv set tcp [new Agent/TCP] set tcpsink [new Agent/TCPSink] $ns connect $tcp $tcpsink

TCP

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
RAP <-> RAP Rate Adaptation Protocol (ratebased AIMD + Slow Start) TFRC <-> TFRCSink TCP Friendly Rate Control protocol (based on TCP throughput-equation)

TCP-friendly connectionless protocols:


Creating Traffic Applications

Use app on Top of agent: $app start $app stop FTP


set ftp [new Application/FTP] $ftp attach-agent $tcp set telnet [new Application/Telnet] set session [new httpSession $ns <numPages> <clientNode>] set cbr [new Application/traffic/cbr]

Telnet

Web

Traffic generator

Computing Routes

Unicast

$ns rtproto <type> <type>: Static, Session, DV, cost, multipath Simulator set EnableMcast_ 1 Simulator set NumberInterfaces_ 1 $ns mrtproto <type> <type>: CtrMcast, DM, dynamicDM, pimDM

Multicast

Example 1: TCP (FTP from n0 to n1)


n0
set ns [new Simulator] #Create 2 nodes set n0 [$ns node] set n1 [$ns node]

n1
set tcp [new Agent/TCP] set tcpsink [new Agent/TCPSink] $ns attach-agent $n0 $tcp $ns attach-agent $n1 $tcpsink $ns connect $tcp $tcpsink $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 ftp [new Application/FTP]

Generic Script Structure


set ns [new Simulator] # [Turn on tracing] # Create topology # Setup packet loss, link dynamics # Create routing agents # Create: # - multicast groups # - protocol agents # - application and/or setup traffic sources # Post-processing procs # Start simulation

Screenshots

Tcl Basics

Variable substitution

Arithmetic Expressions

i: the character i. $i: the variable i.

Operation substitution

set value [expr $v1 + $v2] set i [expr 5 + 6] 11

Printing

Control Structures

puts $filename string (default is stdout)


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

Procedures

proc proc_name {arg1 arg2} { }

NS Models

Traffic models and Applications:

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


unicast: TCP, UDP Multicast: SRM, CtrMcast, DM, dynamicDM, pimDM Wired routing, ad hoc routing and directed diffusion queuing protocols: RED, drop-tail, etc Wired (point-to-point, LANs), wireless (multiple propagation models), satellite

Transport protocols:

Routing and queuing:


Physical media:

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

#Open the general trace file set tf [open out.tr w] # Start tracing $ns trace-all $tf

$ns run

Example 2 Routing (UDP)


#Create a simulator object
cbr0

n0 n1

UDP

n2
cbr1 UDP

UDP

n3

null0

#Define different colors for data flows #Create links between the nodes $ns color 1 Blue # only one sample line as in ex 1 $ns color 2 Red

#Open the nam trace file # as in example 1


#Define a 'finish' procedure # as in example 1 #Create four nodes set n0 [$ns node] set n3 [$ns node]

# Set orientation of the links $ns duplex-link-op $n0 $n2 orient rightdown #Monitor (Visualize) the queue for the # link between node 2 and node 3 $ns duplex-link-op $n2 $n3 queuePos 0.5

Example 2 Routing (Contd)


#Create a UDP agent and attach it to node n1 set 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 n3 set 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

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 loop

#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

for {set i 0} {$i < 7} {incr i} { set n($i) [$ns node] }

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

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

Das könnte Ihnen auch gefallen