25
Introduction to NS

Introduction to NS. Information Main website Documentation, mailing list archive, tutorial Location of Source codes –C++ files

Embed Size (px)

Citation preview

Introduction to NS

Information

• Main website http://www.isi.edu/nsnam/

• Documentation, mailing list archive, tutorial

• Location of Source codes– C++ files

• $NS-HOME/ns-allinone-2.28/ns-2.28

– Tcl files• $NS-HOME/ns-allinone-2.28/ns-2.28/tcl/lib

NS2 – Network Simulator

• Event-Driven simulator– Maintain a sorted event queue – Dequeue head event

• Packet arrival

– Assign event to its handler • At TCP agent

– Handler processes event• Update Window

– Enqueue more events in the event queue• Schedule delivery of ACK

Basic Model

• Back-end C++– Protocols & Framework

• Front-end Otcl (Object-Oriented Tcl)– Scenarios

• Split Object– Object created in otcl has a corresponding object in

C++

• This tutorial only deals with otcl

Tcl/OTcl Basics• Variables

– set v1 10 – set v2 $v1

• Array (String indexed)– a($i) ≠ a( $i )

• Printing– puts $filename “string” (default filename is stdout)

• Arithmetic Expressions– set value [expr $v1+($v2 * 5.2)]

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

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

OTcl Basics (contd.) • Creating a class

– Class class_name– Class class_name –superclass Base_class

• Defining instance procedures– class_name instproc proc_name {args} {…..}

• Defining instance variables– $self instvar variable_name (inside a class method)

• Creating an instance– set new_inst [new class_name]

• Using value of an instance variable– $new_inst set v1 10 or set v2 [$new_inst set v1]

NS Communication Model

• Nodes– Hosts, Routers

• Links– Queue Management

– Queue Monitoring

• Agents– Protocol

1 2

3

4 5

TCP

TCP Sink

Node

n0

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

Node entry

Unicast Node

Link 1

Link 2

Slide taken from Hung-Yun Hsieh’s

tutorial

Link

n0 n1

enqT_ queue_ deqT_

drophead_ drpT_

link_ ttl_

n1 entry_

head_

tracing simplex link

duplex link

Slide taken from Hung-Yun Hsieh’s

tutorial

Routing

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

Node entry 0

1 enqT_ queue_ deqT_

drophead_ drpT_

link_ ttl_

n1 entry_

head_

Slide taken from Hung-Yun Hsieh’s

tutorial

Routing

n0 n1

Port Classifier

entry_

Addr Classifier

classifier_

dmux_1

0

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

0

1 Link n0-n1

Link n1-n0

Slide taken from Hung-Yun Hsieh’s

tutorial

Transport

0

1

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

0 Agent/TCP

agents_

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

1

0Link n0-n1

Link n1-n0

0 Agent/TCPSink

agents_

dst_= 1.0 dst_= 0.0

Slide taken from Hung-Yun Hsieh’s

tutorial

Application

0

1

n0 n1

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

0 Agent/TCP

agents_

Addr Classifier

Port Classifier

classifier_

dmux_

entry_

1

0Link n0-n1

Link n1-n0

0 Agent/TCPSink

agents_

dst_=1.0 dst_=0.0

Application/FTP

Slide taken from Hung-Yun Hsieh’s

tutorial

Packet Flow

0

1

n0 n1

Addr Classifier

Port Classifier

entry_

0 Agent/TCP Addr Classifier

Port Classifier

entry_

1

0Link n0-n1

Link n1-n0

0 Agent/TCPSink

dst_=1.0 dst_=0.0

Application/FTP

Slide taken from Hung-Yun Hsieh’s

tutorial

Starting off…

• Things common to all simulation scripts:• Create a new simulator object

set ns [new Simulator]

• Finish procedureproc finish { } { …….. exit 0}

• Last line $ns run

Basic Script

set ns [new Simulator]proc finish { } {

exit 0}

• Topologyset n1 [$ns node]set n2 [$ns node]$ns duplex-link $n1 $n2 1Mb

10ms DropTail

• Agentsset a1 [new Agent/TCP]set a2 [new Agent/TCPSink]

• Attach and Connect Agents

$ns attach-agent $n1 $a1

$ns attach-agent $n2 $a2

$ns connect $a1 $a2

• Start and Finish Time$ns at 0.5 “$a1 advance 10”

$ns at 1.5 “finish”

$ns run

A more useful scriptset ns [new Simulator]set nam_file [open out.nam w]$ns namtrace-all $nam_file

proc finish {} {global ns nam_file

$ns flush-trace close $nam_file

exec nam out.nam & exit 0}

set n1 [$ns node]set n2 [$ns node]$ns duplex-link $n1 $n2 1Mb

10ms DropTail

set a1 [new Agent/TCP]

set a2 [new Agent/TCPSink]

$ns attach-agent $n1 $a1

$ns attach-agent $n2 $a2

$ns connect $a1 $a2

$ns at 0.5 “$a1 advance 10”

$ns at 1.5 “finish”

$ns run

Recording Data

• Add a new procedure

proc record-data { duration } {global datafile a1 ns# data format -- time bytes-receivedputs $datafile “[$ns now] [$a1 set

ndatabytes_]”$ns after $duration “record-data $duration”

}

set datafile [open “datafile.dat” w]$ns at 0.5 “record-data 0.1”

More Complex Case

• Bottleneck Link

Topology

for {set i 0} {$i < 4} {incr i} {set border($i) [$ns node]

}

for {set i 0} {$i < 2} {incr i} {set core($i) [$ns node]

}

$ns duplex-link $border(0) $core(0) 10Mb 5ms DropTail$ns duplex-link $border(1) $core(0) 10Mb 5ms DropTail$ns duplex-link $border(2) $core(1) 10Mb 5ms DropTail$ns duplex-link $border(3) $core(1) 10Mb 5ms DropTail$ns duplex-link $core(0) $core(1) 1Mb 5ms DropTail$ns queue-limit $core(0) $core(1) 20set qmon [$ns monitor-queue $core(0) $core(1) “”]

Agentsset tcp [new Agent/TCP]

$ns attach-agent $border(0) $tcp

set tcpsink [new Agent/TCPSink]

$ns attach-agent $border(3) $tcpsink

set udp [new Agent/UDP]

$ns attach-agent $border(1) $udp

set lm [new Agent/LossMonitor]

$ns attach-agent $border(2) $lm

$tcp set fid_ 0

$udp set fid_ 1

$ns color 0 Red

$ns color 1 Blue

Sources

• Attach traffic source to agents and connect

set cbr [new Application/Traffic/CBR]

$cbr attach-agent $udp

$cbr set rate_ 2Mb

set ftp [$tcp attach-app FTP]

$tcp set packetSize_ 100

$ns connect $tcp $tcpsink

$ns connect $udp $lm

Data Collection

proc record-data { duration } {global ns tcp tcp_file lm udp_file queue_file qmon

puts $tcp_file “[$ns now] [expr [$tcp set ndatabytes_]/$duration]”$tcp set ndatabytes_ 0

puts $udp_file “[$ns now] [expr [$lm set bytes_]/$duration]”$lm set bytes_ 0

puts $queue_file “[$ns now] [$qmon set pkts_]”$ns after $duration “record-data $duration”

}

Other detailsset ns [new Simulator]

# Nam, TCP-data, UDP-data, Queue-data files# Finish method# Topology# Agents# Sources# Data Collection

$ns at 0.1 “$ftp start”$ns at 4.0 “$ftp stop”$ns at 1.0 “$cbr start”$ns at 2.0 “$cbr stop”$ns at 0.1 “record-data 0.1”$ns at 4.1 “finish”$ns run

Things to Remember

• Topology, agents, sources, start

• Connect the agents– Slot not found error

• Start/Stop the sources

• In procedures, declare global variables before use

• “$ns run” – last line