You are on page 1of 71

NS2

cobrageo@ismp.csie.ncku.edu.tw 2011/07/22

NS2
Network Simulator (Open Source) OSI Layers Packet-Level Wired and Wireless
Ethernet, 802.11, WiMAX, etc.

NS2
Ns-allinone Download home terminal
su patch patch Y/N linux y

NS-2 Simulator

NS2
patch
yum install gcc yum install tcl-devel yum install autoconf yum install automake yum install gcc-c++ yum install libX11-devel yum install xorg-x11-proto-devel yum install libXt-devel yum install libXmu-devel yum install libtool

NS2
Ns-allinone
tar -xvf ns-allinone-2.34.tar.gz

home

G
cd ns-allinone-2.34/ ./install

NS2
Fedora 14
gcc tools/ranvar.cc: in member function `virtual double gammarandomvariable::value(): tools/ranvar.cc:219:70: error: cannot call constructor `GammaRandomVariable::GammaRandomVariable tools/ranvar.cc:219:70: error: for a function-style cast, remove the redundant `::GaammaRandomVariable make: *** [tools/ranvar.o] Error 1 ns make failed

NS2

NS2
ns-allinone-2.34/tools 219 ranvar.cc (:GaammaRandomVariable) ./install

Please put /opt/ns-allinone-2.34/bin:/opt/ns-allinone-2.34/tcl8.4.18/unix:/opt/ns-allinone-2.34/tk8.4.18/unix into your PATH environment; so that you'll be able to run itm/tclsh/wish/xgraph. IMPORTANT NOTICES: 1. You MUST put /opt/ns-allinone-2.34/otcl-1.13, /opt/ns-allinone-2.34/lib, into your LD_LIBRARY_PATH environment variable. If it complains about X libraries, add path to your X libraries into LD_LIBRARY_PATH. If you are using csh, you can set it like: setenv LD_LIBRARY_PATH If you are using sh, you can set it like: export LD_LIBRARY_PATH=

2. You MUST put /opt/ns-allinone-2.34/tcl8.4.18/library into your TCL_LIBRARY environmental variable. Otherwise ns/nam will complain during startup.

NS2

PATH=/home/Geo/ns-allinone-2.34/bin:/home/Geo/ns-allinone2.34/tcl8.4.18/unix:/home/Geo/ns-allinone-2.34/tk8.4.18/unix:$PATH LD_LIBRARY_PATH=/home/Geo/ns-allinone-2.34/otcl1.13:/home/Geo/ns-allinone-2.34/lib TCL_LIBRARY=home/Geo/ns-allinone-2.34/tcl8.4.18/library

ns

exit

NS2
NS2 simple.tcl
cd ns-allinone-2.34/ns-2.34/tcl/ex Ns simple.tcl

NS2

10

Compiler in NS2
NS2 use two languages
C++ TCL/OTCL

TCL is an interpreter language


Modify and execute again without compiling Execution speed is slower

C++
Modify and execute after compiling Execution speed is faster

NS2NS2-Package Components
Main Component
ns-2.34\

Virtual Simulator
nam-1.14\

TCL Languages
otcl-1.13\ tcl8.4.18\ tclcl-1.19\ tk8.4.18\

Graph tools
xgraph-12.1\

Main Source Code


ns2.34\
Physical and MAC Layer
mac\ aodv\ tcp\ mobile\ dsdv\ etc. dsr\ etc.

Network Layer Transport Layer Application Layer


app\

Main Source Code


Configuration files (in TCL Language)
tcl\
Examples code tcl\ex\

tcl\lib\
Some functions written in TCL\OTCL language. Defaults value. (ns-default.tcl)

TCL
TCL(Tool Command Language)
NS2
NS2 Topology Construction Parameter Initialization Algorithm Implementation Traffic Scheduling

15

TCL
hello.tcl:
set s "Hello World" puts $s

Command:
ns hello.tcl

Result:
Hello World

16

(variable)
set
Ex.: set a 0

unset
Ex.: unset a

17

(variable substituion)
Ex.: set name Jacky" puts "I am $name" Result: I am Jacky

18

(expressions)
:
expr arg ?arg arg ...?

:
Ex.:
set value [expr 2+3] puts $value

[]

Result:
5

19

incr
:
incr varName ?increment?

: :
set i 0 incr i incr i 4

:
0 1 5

20

append
:
append varName ?value value value ...?

: :
set i 1 append i 2 3 4 5

:
12345

21

exec
:
exec ?switches? arg ?arg ...?

: :
exec umane

:
CYGWIN_NT 5-1

22

list
:
list ?arg arg ...?

:
list a b c d

:
a b { c d}

23

eval
:
eval arg ?arg ...?

:
script,

:
eval [list set i] 3

:
3

24

lindex
:
lindex $list ?index...?

:
list

:
lindex {a b c} 2 lindex {a b c} end

:
c c

25

linsert
:
linsert $list index element ?element element ...?

:
set aa [list a b c d] linsert $aa end-1 1 2 3 abc123d

26

lappend
:
lappend varName ?value value value ...?

:
list

:
set var a lappend var b c d
:

abcd

27

concat
:
concat ?arg arg ...?

:
list

:
set a [concat "a b" 1 2 {c {d e}}] lindex $a 5

:
de

28

llength
:
llength $list

:
list

:
llength {a b}

:
2

29

lrange
:
lrange $list first last

:
list

:
lrange {a b c d} 1 end

:
bcd

30

:
array option arrayname ?arg1 arg2 ?

: :
set myarray(00) 456 puts $myarray(00)

:
456

31

option: set
:
array set arrayname $list

: :
array set myarray {0 123 00 456 a b}

:
0 123 00 456 a b

32

TCL
If- l it F rl F r l il l

33

(if-else) (ifEx.:
set grade 90 if {$grade >= 90} { puts "Your level is A } elseif {$grade < 90} {

" " "

puts "Your level isnt A } else { }

puts "You set wrong data

Result:
Your level is A

34

(switch)
Ex.:
set num 1 switch $num { 1 {puts "Im John."} 2 {puts "Im Mary."} 3 {puts "Im Andy."} default {puts "Whats your name?"} }

Result:
Im John.

35

(for loop)
1+2++100 Ex.:
set ans 0 for {set a 0} {$a < 101} {incr a 1} { set ans [expr $ans+$a] } puts $ans

Result:
5050

36

(foreach loop)
1+2++10 Ex.:
Set j 0 foreach i {1 2 3 4 5 6 7 8 9 10} { set j [expr $i+$j] } puts $j

Result:
55

37

(while loop)
1+2++100 Ex.:
set a 0 set ans 0 while {$a < 101} { set ans [expr $ans+$a] incr a 1 } puts $ans

Result:
5050

38

NS2
sample.tcl # set ns [new Simulator] # NAM $ns color 1 Blue $ns color 2 Red # NAM trace fileset nf [open out.nam w] $ns namtrace-all $nf

NS2
# trace file set nd [open out.tr w] $ns trace-all $nd # proc finish {} { global ns nf nd $ns flush-trace close $nf close $nd # exec nam out.nam & exit 0 }

NAM (The Network Animator)

NS2
# set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] # $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail # ns2 n3 Queue Size 10 $ns queue-limit $n2 $n3 10 # NAM $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right

NS2
# n2 n3 queue NAM $ns duplex-link-op $n2 $n3 queuePos 0.5 # TCP  set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink # NAM TCP  $tcp set fid_ 1 # TCP  FTP set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP

NS2
# UDP  set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n3 $null $ns connect $udp $null # NAM UDP  $udp set fid_ 2 # UDP  CBR set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false

NS2
# FTP CBR $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop # TCP ( ) $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink # 5 finish ( # 5 $ns at 5.0 "finish # $ns run

NS2

AWK
 (index)

$0 pipe C

, $1 shell

, $2 ...

AWK
Awk (awk

awk

AWK
awkf out.tr

AWK
Awk delay.awk

AWK

AWK
: ($ shell
$awk-f measure-delay.awk out.tr

) (

cbr_delay

$awk-f measure-delay.awk out.tr > cbr_delay

Gnuplot
gnuplot

Gnuplot
plot cdr_delay

nsBench
nsBench
http://www.mnlab.cs.depaul.edu/projects/nsbench/nsBench.jar

54

nsBench
link

TCP Agent

TCP Sink

55

nsBench
FTP traffic udp0 cbr0 null1 

ftp1-tcp0 tcp0-n0 tcp0-sink0

cbr0

56

nsBench
scenario Trace

57

nsBench

58

nsBench
TCL

nsBench

nsBench

nsBench

nsBench
nsBench nsBench nsBench GUI cygwin

nsBench
proc finish {} block flow ( exec nam out.nam & flow 1

$ns color 1 Blue Red Green )

63

nsBench
( duplex-link-op $n1 $n2 orient right-down ( ) queue $ns duplex-link-op $n2 $n3 queuePos 0.5 n2 n1 ) $ns

64

NS2 Scenarios Generator

NS2 Scenarios Generator

NS2 Scenarios Generator

NS2 Scenarios Generator

TCL Script Hidden Terminal Expose Terminal CSMA/CA CSMA/CA

awk
Packet delivery rate Fairness TCL, Awk/Gnuplot scripts comment

Hidden Terminal Problem

Exposed Terminal Problem

You might also like