You are on page 1of 74

Network Simulator NS-2

Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example

9/20/2012

What is NS-2
NS-2 stands for Network Simulator version 2.

Discrete event simulator for networking research Work at packet level. Provide substantial support to simulate bunch of protocols like TCP, UDP, FTP, HTTP. Simulate wired and wireless network. Is primarily Unix based. Use TCL as its scripting language.

NS-2 is a standard experiment environment in research community http://www.isi.edu/NSnam/NS

9/20/2012

NS-2 collaborative environment


Developed at UC Berkely Freely distributed, open source

Share code, protocols, models, etc More people look at models in more situations Experts develop models > 1k institutes (50 countries), >10k users (2002) About 300 posts to the mailing list every month

Confidence in results

User base

9/20/2012

NS Software
NS, the simulator itself Nam, the network animator

Visualize NS (or other) output Nam editor: GUI interface to generate NS scripts Traffic and topology generators Simple trace analysis, often in Awk, Perl, or Tcl

Pre-processing: Post-processing:

9/20/2012

Architecture of NS
OTcl

Simulation scenario configurations A lot of things can be done without modifying existing C++ modules. per packet processing, core of ns byte manipulation, packet processing, algorithm implementation Needs to change behavior of existing module fast to run, detailed, complete control

C++

9/20/2012

OTcl and C++: The Duality

Pure C++ objects

Pure OTcl objects

C++/OTcl split objects

C++ ns
9/20/2012

OTcl

Why two languages (TCL & C++)?


C++: Detailed protocol simulations require systems programming language

Byte manipulation, packet processing, algorithm implementation Run time speed is important Turn around time (run simulation, find bug, fix bug, recompile, re-run) is slower

Tcl: Simulation of slightly varying parameters or configurations

Quick explore a number of scenarios Iteration time (change the model and re-run) is more important

9/20/2012

Working with NS-2 (1/2)

9/20/2012

Working with NS-2 (2/2)


NScript, Nam-editor

9/20/2012

10

Outline
Overview: architecture,working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example

9/20/2012

11

Scheduler and Events Scheduler core of the simulator. Its role scheduling of events NS-2 event driven simulator Event: An action to be done after a certain time in the future

Retransmit a packet when a timer expires Pass a packet to the node at the output of a link
after propagation delay time interval

9/20/2012

12

Event Driven Simulation (1/3)

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

9/20/2012

13

Event Driven Simulation (2/3)


Simulator has list of events Process: take next one, run it, until done
RX Ack Event TX Pkt Event @ 1.5sec 2.0sec

Event Scheduler Event Queue RX Ack Event TX Pkt Event RX TX @ 1.5sec 2.0sec 1.8sec 1.7sec Simulation Finished!

Node 1 Module

TX Ack Event RX Pkt @ 1.8sec 1.7sec

Node 2 Module

9/20/2012

14

Event Driven Simulation (3/3)


Events scheduled by the objects of the model

Link object schedules event end of propagation when to start an FTP application when to finish a simulation

Events scheduled by user

Simulation scenario generation prior to a simulation run

9/20/2012

15

Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example

9/20/2012

16

TCL
Tool Command Language

Commonly, tickle

Scripted language, i.e. interpreted language

9/20/2012

17

Variable Types Tcl is an example of a "weakly typed" language.

Almost any type of data can be stored in any


variable. The same variable can be used to store a number, a date, a string, or even the text of another Tcl script (i.e. instructions of the program can set the value to any of the above)

9/20/2012

18

Variables in TCL
Assignment: set <variable_name> <variable_value> set age 23 set name David Variable substitution $<variable_name> - means substitute the value of the variable name here

set id 631 puts The ID is $id

Output: puts <what_to_print_out>

9/20/2012

19

Examples
set foo "john" puts "Hi my name is $foo" set month 2 set day 3 set year 97 set date "$month:$day:$year puts $date set foo "puts Hi eval $foo

9/20/2012

20

Math Evaluation
expr command evaluates expressions. Sample command Result set b 5 5 expr ($b*4)-3 17 expr $b <= 2 0 Many other math functions included, such as sin, cos, sqrt, and log.

9/20/2012

21

Eval Command
The 'eval' command Concatenate all its arguments in one string Splits this string using spaces as separators Evaluate the command sentence formed by all the substrings
set foo "set a 22" eval $foo puts $a Output: 22

9/20/2012

22

Command Substitution
Command substitution : substitute the resulting value of a command Invoked with [] Example:
set my_height 1.70 puts "If I was 5 santimeter taller, I would be \ [expr $my_height + (5.0 / 100.0)] meter tall

\ - continue line
9/20/2012 23

Tcl lists
List is an ordered collection of elements
set Lst set Lst "[ set Lst] [list a]" set Lst "[ set Lst] [list b]"

concat list list concatenate lists


concat {a b c} {d e f} a b c d e f

join list sep convert to string with separator


join {a b c} ", " a, b, c

Some list functions: lappend lindex,


linsert, llength, lrange
9/20/2012 24

IF statement Syntax:

if { expression } { instructions1 } if { expression } { instructions1 } else {


instructions2 }

Rules:

if { expr } { } else {

- has to be on the same line - has to be on the same line

9/20/2012

25

Examples of IF
set temp 18 if {$temp < 18} { puts "It's a little chilly." } else { puts "Warm enough for me. } puts This gets printed out either way set my_planet "earth" if {$my_planet == "earth"} { puts "I feel right at home." } elseif { $my_planet == "venus } { puts "This is not my home. } else { puts "I am neither from Earth, nor from Venus." }
9/20/2012 26

Decision-Making with switch


Syntax
switch value_to_compare { specific_value { instructions1 } specific_value { instructions2 } default { instructionsN } }

Execute only one of the blocks of instructions depending on the value of value_to_compare

9/20/2012

27

Example of switch
set num_legs 4 switch $num_legs { 2 {puts "It could be a human."} 4 {puts "It could be a cow."} 6 {puts "It could be an ant."} 8 {puts "It could be a spider."} default {puts "It could be anything."} }

9/20/2012

28

Looping with foreach


Syntax foreach varName list { instructions } For each element of the list 1. assign its value to the variable varname 2. execute instructions that likely deal with this new value of varname Example: foreach vowel {a e i o u} { puts "$vowel is a vowel" }
9/20/2012 29

Looping with while


Syntax while { expr } { instructions } Example: set i 0 while {$i < 10} { puts "In the while loop, and i == $i" incr i 1 }

incr i 1 means increment the variable i by 1


9/20/2012 30

Looping with for


Syntax for { init } { test } { reinit } { instructions } Example

for {set i 0} {$i < 10} {incr i 1} { puts "In the for loop, and i equals $i if { $i > 6 } { puts i is greater than 6! } }

9/20/2012

31

Arrays
Collections of items in which each item is given a unique index for direct access Index must not be numeric - associative arrays Refer to individual element : array name, followed by the index of the element, enclosed in parentheses.
array A

A (2) access the 2nd element of the

set i david A(i) access the element of the array A associated with david
9/20/2012 32

Example use of arrays


set myarray(0) "Zero set myarray(1) "One set myarray(2) "Two for {set i 0} {$i < 3} {incr i 1} { puts $myarray($i) }

9/20/2012

33

Functions Function declaration


proc function-name {par1 par2} { global a b return [expr a +par1] }

Function call
set c [function-name par1 par2]

9/20/2012

34

Basic oTCL
Class Person # constructor: Person instproc init {age} { $self instvar age_ set age_ $age } # method: Person instproc greet {} { $self instvar age_ puts $age_ years old: How are you doing? }
9/20/2012

# subclass: Class Kid -superclass Person Kid instproc greet {} { $self instvar age_ puts $age_ years old kid: Whats up, dude? } set a [new Person 45] set b [new Kid 15] $a greet $b greet
35

Classes Summary of Syntax


Create an instance of a class:
set obj [new Class1/Class2/Class3]

Call a method of an object without return


$obj method-name $par1 $par2

and with return


set a [$obj method-name $par1 $par2 ]

Assign a value to a variable of an object


$obj set var-name $a

Get a value of objects variable and assign it to a


set a [$obj set var-name]

9/20/2012

36

Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example

9/20/2012

37

Network Topology: Node

n0
Port Classifier Addr Classifier Node entry entry_ classifier_ dmux_

n1 Unicast Node Multicast Node classifier_


Node entry entry_ Multicast Classifier multiclassifier_ dmux_

Classifier : Address, Multicast, Multipath, Hash

9/20/2012

38

Network Topology: Link

n0 duplex link

n1

head_

n1 entry_

enqT_ tracing

queue_
drophead_

deqT_
drpT_

link_

ttl_

simplex link

9/20/2012

39

Routing

n0
Port Classifier Addr Classifier Node entry entry_

n1

0 1
classifier_

dmux_

head_ enqT_ queue_ drophead_ deqT_ drpT_ link_ ttl_

n1 entry _

9/20/2012

40

Routing
n0
Port Classifier Addr Classifier

n1
Port Classifier Addr Classifier

entry_

0 1
classifier_

dmux_

Link n0-n1 entry_

1 0
classifier_

dmux_

Link n1-n0

9/20/2012

41

Transport

n0
Port Classifier Addr Classifier

n1
Port Classifier Addr Classifier Link n0-n1 entry_

dst_=1.0 Agent/TCP agents_

dst_=0.0 Agent/TCPSink agents_

0
dmux_

0
dmux_

entry_

0 1
classifier_

1 0
classifier_

Link n1-n0

9/20/2012

42

Application : Traffic generator

n0
Port Classifier Addr Classifier Application/FTP dst_=1.0 Agent/TCP agents_ Link n0-n1 entry_

n1
Port Classifier Addr Classifier

dst_=0.0 Agent/TCPSink agents_

0
dmux_

0
dmux_

entry_

0 1
classifier_

1 0
classifier_

Link n1-n0

9/20/2012

43

Packet Flow

n0
Port Classifier Addr Classifier Application/FTP dst_=1.0 Agent/TCP

n1
Port Classifier Addr Classifier Link n0-n1 entry_

dst_=0.0 Agent/TCPSink

entry_

0 1

1 0

Link n1-n0

9/20/2012

44

Outline
What are we going to simulate? (Networks in a nutshell) Overview: architecture,working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example

9/20/2012

45

Basic structure of ns-scripts Creating the event scheduler [Tracing] Creating network topology Creating Transport Layer - Agents Creating Applications Events

9/20/2012

46

Creating Event Scheduler Create scheduler

set ns [new Simulator]


Schedule event

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


Start scheduler

$ns run

9/20/2012

47

Hello World in ns simple.tcl


set ns [new Simulator] $ns at 1 puts \Hello World!\ $ns at 1.5 exit $ns run

9/20/2012

48

Creating Network Nodes

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


Links & Queuing
$ns duplex-link $n0 $n1 <bandwidth> <delay> <queue_type>

Queue type: DropTail, RED, CBQ,

9/20/2012

49

Routing + traffic Unicast

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


Multicast support also. Traffic

Simple two layers: transport and application. Transport: TCP, UDP etc. Applications: web, ftp, telnet etc.
9/20/2012 50

Transport Layer Class Agent

Agent/UDP

Agent/TCP (=Tahoe)
Other TCP flavors

9/20/2012

51

The transport layer: 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
9/20/2012 52

The transport layer: 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
9/20/2012 53

Application Layer

Class Application

{Simulated Applications} (on top of TCP)

{Traffic generators} (on top of UDP)

9/20/2012

54

Creating Traffic: On Top of TCP FTP

set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at <time> $ftp start
Telnet

set telnet [new Application/Telnet] $telnet attach-agent $tcp


9/20/2012 55

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]

9/20/2012

56

Attaching a traffic source

set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $ns at <time> $cbr start

9/20/2012

57

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)

9/20/2012

58

Controlling object parameters Almost all ns objects have parameters

ex. Application/Traffic/Exponential has rate and


packetSize set parameters in OTcl
set etraf [new Application/Traffic/Exponential] $etraf set rate_ 1Mb $etraf set packetSize_ 1024

9/20/2012

59

Putting it all together


set ns [new Simulator] set n0 [$ns node] set n1 [$ns node] $ns duplex-link $n0 $n1 1.5Mb 10ms DropTail $ns trace-queue $n0 $n1 $f set tcp [$ns create-connection TCP $n0 TCPSink $n1 0] set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at 0.2 "$ftp start $ns at 1.2 exit $ns run
9/20/2012 60

Creating Topology

Creating Transport layer

Creating Applications Schedule Events

Trace File Format

Having simulation trace data at hand Transform a subset of the data of interest into a comprehensible information. Use Perl, awk etc Analyze it
9/20/2012 61

Types of Traces Trace all


#Open the Trace-all file set tracefile1 [open out.tr w] $ns trace-all $tracefile1

Trace queue. Tracing events over the link between nodes n1 and n2
#Open the Trace-queue file set tracefile2 [open out.tr w] $ns trace-queue $tracefile2
9/20/2012 62

Some NS-2 Programming Patterns Get current simulation time Parameterize the model
foreach argument $argv { # 3 parameters: double, string and integer scan $argument Par1=%lf Par1 scan $argument Par2=%s Par2 scan $argument Par3=%d Par3 } #Use parameters as $ParN #Run simulation: #ns prog.tcl Par1=0.9 Par2=qq Par3=5
9/20/2012 63

set t [$ns now]

NAM: Visualization tool Nam-1 (Network AniMator Version 1)

Packet-level animation Well-supported by ns

9/20/2012

64

NS-NAM Interface Color Node manipulation Link manipulation Topology layout Protocol state

9/20/2012

65

NAM Interface: Color

Color mapping
$ns color 40 red $ns color 41 blue $ns color 42 chocolate

Color flow id association


$tcp0 set fid_ 40 $tcp1 set fid_ 41 ;# red packets ;# blue packets

9/20/2012

66

NAM Interface: Nodes Color


$node color red

Shape (cant be changed after sim starts)


$node shape box ;# circle, box, hexagon

Marks (concentric shapes)


$ns at 1.0 $n0 add-mark m0 blue box $ns at 2.0 $n0 delete-mark m0

Label (single string)


$ns at 1.1 $n0 label \web cache 0\

9/20/2012

67

NAM Interface: Links Color


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

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

Asymmetric links not allowed

9/20/2012

68

NAM Interface: Topology Layout

Manual layout: specify everything


$ns duplex-link-op $n(0) $n(1) orient right $ns duplex-link-op $n(1) $n(2) orient right-up $ns duplex-link-op $n(2) $n(3) orient down $ns duplex-link-op $n(3) $n(4) orient 60deg

If anything missing layout

automatic

9/20/2012

69

Outline
Overview: architecture, working with NS-2 Scheduler and events TCL Programming Basics Network topology, routing and traffic in NS-2 Programming NS-2 model NS-2 example

9/20/2012

70

NS Example (1/4)
#1. Create a simulator object
set ns [new Simulator]

#2.
# set $ns # set $ns

Turn on tracing
Open the NAM trace file nf [open out.nam w] namtrace-all $nf Open the Trace file tf [open out.tr w] trace-all $tf

#3.
set set set set

Create four nodes


n0 n1 n2 n3 [$ns [$ns [$ns [$ns node] node] node] node]

9/20/2012

71

NS Example (2/4)
#4. #
$ns $ns $ns $ns

Create links between the nodes <bw> <delay> <type of buffer>


duplex-link duplex-link duplex-link queue-limit $n0 $n1 $n2 $n2 $n2 $n2 $n3 $n3 2Mb 10ms DropTail 2Mb 10ms DropTail 1.7Mb 20ms DropTail 100

#5. #

Setup a TCP connection

Create transport agents set tcp [new Agent/TCP] set sink [new Agent/TCPSink] # Attach transport agents # to peer nodes $ns attach-agent $n0 $tcp $ns attach-agent $n3 $sink # Establish connection $ns connect $tcp $sink $tcp set fid_ 1

#6.

Setup a FTP over TCP connection

set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP
9/20/2012 72

NS Example (3/4)
#7. # Setup a UDP connection
Create transport agents set udp [new Agent/UDP] set null [new Agent/Null] # Attach transport agents # to peer nodes $ns attach-agent $n1 $udp $ns attach-agent $n3 $null # Establish connection $ns connect $udp $null $udp set fid_ 2

#8. #

Setup a CBR over UDP connection

set cbr \ [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb

9/20/2012

73

NS Example (4/4)
#9. #
$ns $ns $ns $ns

Schedule events for the CBR and FTP agents


at at at at 0.1 1.0 4.0 4.5 "$cbr "$ftp "$ftp "$cbr start" start" stop" stop

#10. Define different colors # for data flows (for NAM)


$ns color 1 Blue $ns color 2 Red #11. Run the simulation $ns run

Start NAM

9/20/2012

74

You might also like