You are on page 1of 31

NS-2 Tutorial

Demokritos University of Thrace Depts of: Data Analysis & Computer Networks

ns-2 overview

Collection of various protocols at multiple layers: TCP(reno, tahoe, vegas, sack) !C("#2$%%, "#2$&, T' !) !(-hoc )outing ('S'*, 'S), !+'*, T+)!) Sensor Net,ork ((iffusion, gaf) ulticast protocols, Satellite protocols, an( many others Co(es are contri-ute( from multiple research communities: .oo(: /arge set of simulation mo(ules 0a(: /evel of support an( (ocumentation varies The source co(e an( (ocumentation is currently maintaine( -y *1NT pro2ect at 1S1

ns-2 Environment
Simulation Scenario
1 2

set ns_ [new Simulator]

Tcl Script

set node_(0) [$ns_ node] set node_(1) [$ns_ node]

C++ Implementation

class MobileNode : public Node { friend class osition!andler" public: MobileNode() # $

ireless !imulation in ns-2

Contri"ute# from C$U%s $onarch pro&ect ' ireless e(tension to ns-2) *arious mo#ules were a##e# to ns-2 to simulate no#e mo"ility an# wireless networkin+ $o"ile No#e A#-hoc ,outin+'D!,- D!D*- T.,A- A.D*) $AC/02122 ,a#io 3ropa+ation $o#el Channel

$o"ile No#e $o#ules

A+ent ,esponsi"le for packet +enerations an# receptions Can think of it as an Application layer C4,'Constant 4it ,ate)- TC3- !ink- 5T3- etc1 ,Ta+ent'D!D*- T.,A- A.D*) or D!, A#-hoc network routin+ protocols Confi+ure multi hop routes for packets 66 '6ink 6ayer) ,uns #ata link protocols 5ra+mentation an# reassem"ly of packet ,uns A##ress ,esolution 3rotocol'A,3) to resolve 73 a##ress to $AC a##ress conversions

$o"ile No#e $o#ules 'Continue#)

758 '7nterface 9ueue) 3ri9ueue is implemente# to +ive priority to routin+ protocol packets !upports filter to remove packets #estine# to specific a##ress $ac 6ayer 7EEE /02122 protocol is implemente# Uses ,T!:CT!:DATA:AC; pattern for all unicast pkts an# DATA for "roa#cast pkts

$o"ile No#e $o#ules 'Continue#)

Net75 'Network 7nterfaces) <ar#ware interface use# "y mo"ileno#e to access the channel !imulates si+nal inte+rity- collision- t( error $ark each transmitte# packet with transmission powerwavelen+th etc1 ,a#io 3ropa+ation $o#el Uses 5riss-space attenuation'2:r2) at near #istance an# Two ray +roun# '2:r=) at far #istance Deci#es whether the packet can "e receive# "y the mo"ileno#e with +iven #istance- transmit power an# wavelen+th 7mplements .mni Directional Antenna mo#ule which has unity +ain for all #irection

ireless !imulation in ns-2 '$o"ile No#e Dia+ram - D!D*)


Agent (Src/Sink) Port Demux

Radio Propagation Model

Demux

R!agent (DSD")

Addr

LL

ARP

IFq

MAC

NetIF

C annel

ireless !imulation in ns-2 '$o"ile No#e Dia+ram - D!,)


Agent (Src/Sink) Port Demux

DSR LL ARP

IFq

MAC

Radio Propagation Model

NetIF

C annel

,unnin+ a simulation-!cenario
#$$m

TCP #$$m node_(0)

TCP

TCPsink

TCPsink TCPsink

node_(0)

node_(1)

node_(1)

!ettin+ Up *aria"les
%&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& % 'efine options %&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& set (al(c)an) *)annel+,ireless*)annel "% c)annel t-pe set (al(prop) ropa.ation+/wo0a-1round "% radio2propa.ation model set (al(ant) 3ntenna+4mni3ntenna "% 3ntenna t-pe set (al(ll) 55 "% 5in6 la-er t-pe set (al(if7) 8ueue+'rop/ail+ ri8ueue "% 9nterface 7ueue t-pe set (al(if7len) :0 "% ma; pac6et in if7 set (al(netif) )-+,ireless )"% networ6 interface t-pe set (al(mac) Mac+<0=_11 "% M3* t-pe set (al(rp) 'S'> "% ad2)oc routin. protocol set (al(nn) = "% number of mobilenodes

!ettin+ Up *aria"les
Create simulator o-2ect:
set ns_ [new Simulator]

Setup Trace 3ile:


set tracefd [open simple?tr w] $ns_ trace2all $tracefd

Create Topography:
set topo [new /opo.rap)-] $topo load_flat.rid :00 :00

Create +-2ect .o(:


create2.od $(al(nn)

Confi+urin+ $o"ileno#e
% *onfi.ure nodes $ns_ node2confi. 2ad)oc0outin. $(al(rp) @ 2ll/-pe $(al(ll) @ 2mac/-pe $(al(mac) @ 2if7/-pe $(al(if7) @ 2if75en $(al(if7len) @ 2ant/-pe $(al(ant) @ 2prop/-pe $(al(prop) @ 2p)-/-pe $(al(netif) @ 2topo9nstance $topo @ 2c)annel/-pe $(al(c)an) @ 2a.ent/race 4N @ 2router/race 4N @ 2mac/race 4AA @ 2mo(ement/race 4AA for {set i 0$ {$i B $(al(nn) $ {incr i$ { set node_($i) [$ns_ node ] $node_($i) random2motion 0 "% disable random motion $

Confi+urin+ $ovement
Configure 1nitial Position:
$node_(0) $node_(0) $node_(0) $node_(1) $node_(1) $node_(1) set set set set set set C_ D_ E_ C_ D_ E_ :?0 =?0 0?0 FG0?0 F<:?0 0?0

Create

ovement:

% Node_(1) starts to mo(e towards node_(0) $ns_ at :0?0 H$node_(1) setdest =:?0 =0?0 1:?0H $ns_ at 10?0 H$node_(0) setdest =0?0 1<?0 1?0H % Node_(1) t)en starts to mo(e awa- from node_(0) $ns_ at 100?0 H$node_(1) setdest IG0?0 I<0?0 1:?0H

!etup traffic flow


set tcp [new 3.ent+/* ] $tcp set class_ = set sin6 [new 3.ent+/* Sin6] $ns_ attac)2a.ent $node_(0) $tcp $ns_ attac)2a.ent $node_(1) $sin6 $ns_ connect $tcp $sin6 set ftp [new 3pplication+A/ ] $ftp attac)2a.ent $tcp $ns_ at 10?0 H$ftp startH

FTP

TCP

Sink

node_(0)

node_(1)

!et !top Time an# !tart !imulation


Set Simulation Stop Time:
for {set i 0$ {$i B $(al(nn) $ {incr i$ { $ns_ at 1:0?0 H$node_($i) resetH" $ $ns_ at 1:0?0001 HstopH $ns_ at 1:0?000= Hputs @HNS JC9/9N1???@H " $ns_ )altH proc stop {$ { .lobal ns_ tracefd close $tracefd $

3inally, Start The Simulation:


puts HStartin. Simulation???H $ns_ run

c# into the #irectory an# type >ns simple-wireless1tcl? to run the simulation The simulation will +enerate a trace file name# >simple1tr?

Trace 5ile
r 100.381997477 _1_ AGT 1:0 32 1] [32 0] 1 0 --- 82 tcp 1060 [13a 1 0 800] ------- [0:0

r:receive event%##$&"%445655:time stamps7%7:no#e 2!.T:trace +enerate# "y a+ent"2:event'pkt) i#tcp: tcp packet%#8#:packet si@e%&a(he9):e(pecte# #uration of pkt transmission 'not workin+)%:sen#er mac i##:transmitter mac i#"##:pkt type 73 '/0A for A,3)- #:#: sen#er a##ress:portB %:#: receiver a##ress:portB&2: TT6 %: ne(t hop a##ress:&2 #;: TC3 se8uence B- ack B

,ea#in+ Trace 5iles

Ne, <ireless Trace file: s -t %8&$##%=#&=2# ->s # ->( -2 -Ni # -N9 &##$## -Ny =##$## -N? #$## -Ne -%$###### -Nl !.T -N, --- - a # - ( # - s # - t # -1s #$# -1( 2$# -1t c-r -1l 2## -1f % -1i 55 -1v &2 -Pn c-r -Pi &2 -Pf # -Po # 3iel( #: event type s: sen( r: receive (: (rop 3ile( %: .eneral tag -t: time 3iel( &: Ne9t hop info ->s: i( for this no(e ->(: i( for ne9t hop to,ar(s the (estination f: for,ar(

,ea#in+ Trace 5iles


3iel( 6: No(e property type tag -Ni: no(e i( -N9 @Ny -N?: no(eAs 9ByB? coor(inate -Ne: no(e energy level -Nl: trace level, such as !.T, )T), !C -N,: reason for the event 3iel( =: Packet information at 1P level -1s: source a((ress$ Source port num-er -1(: (est a((ress$(est port num-er -1t: packet type -1l: packet si?e -1f: flo, i( -1i: uniCue i( -1v: ttl value

,ea#in+ Trace 5iles


3iel( 8: packet info at !C level - a: (uration - (: (estAs ethernet a((ress - s: srcAs ethernet a((ress - t: ethernet type 3iel( 5: Packet info at D!pplication levelE ,hich consists of the type of application like arp, tcp, the type of a(hoc routing protocol like 'S'*, 'S), !+'* etc$ The fiel( consists of a lea(ing @P an( the list of tags for (ifferent applications$

!imulation-!cenario 77
C C C %###mF%###m net,ork ,ith & no(e C0)BG'P traffic -et,een no(e # to no(e 2 !t 2##s, no(e % moves from (=##, =##) to (=##, 4##), an( then at =##s from (4##, =##) to (=##, %##)$ Simulation time %###s$
1 ( 500, 900)

0 ( 350, 500)

150m

1 ( 500, 500)

150m

2 ( 650, 500)

1000m *1000m

( 500, 100)

!ettin+ Up *aria"les
H 'efine options H IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII set val(chan) ChannelB<irelessChannel JH channel type set val(prop) PropagationBT,o)ay.roun( JH ra(io-propagation mo(el set val(netif) PhyB<irelessPhy JH net,ork interface type set val(mac) acB"#27%% JH !C type set val(ifC) KueueB'ropTailBPriKueue JH interface Cueue type set val(ll) // JH link layer type set val(ant) !ntennaB+mni!ntenna JH antenna mo(el set val(9) %### JH L (imension of topology set val(y) %### JH M (imension of topology set val(cp) NN JH no(e movement mo(el file set val(sc) NN JH traffic mo(el file set val(ifClen) =# JH ma9 packet in ifC set val(nn) & JH num-er of mo-ileno(es set val(see() #$# set val(stop) %###$# JH simulation time set val(tr) e9p$tr JH trace file name set val(rp) 'S'* JH routing protocol set !gentTrace +N set )outerTrace +N set acTrace +33

!ettin+ Up *aria"les
H 1nitiali?e .lo-al *aria-les set nsD Enew !imulatorF GnsD color 2 4lue GnsD color 2 ,e# H +pen trace file GnsD use-newtrace HB Use new trace format set namf# Eopen nam-e(p1tr wF GnsD namtrace-all-wireless Gnamf# Gval'() Gval'y) set tracef# Eopen Gval'tr) wF GnsD trace-all Gtracef# H set up topography o-2ect set topo Enew Topo+raphyF Gtopo loa#Dflat+ri# Gval'() Gval'y) H create channel set chan Enew Gval'chan)F H Create .o( set +o#D Ecreate-+o# Gval'nn)F

Confi+urin+ $o"ileno#es
H Create the specifie( num-er of mo-ile no(es :Oval(nn); an( NattachN them H to the channel$ Three no(es are create( : no(e(#), no(e(%) an( no(e(2) GnsD no#e-confi+ -a#hoc,outin+ Gval'rp) I -llType Gval'll) I -macType Gval'mac) I -if8Type Gval'if8) I -if86en Gval'if8len) I -antType Gval'ant) I -propType Gval'prop) I -phyType Gval'netif) I -channel Gchan I -topo7nstance Gtopo I -a+entTrace .N I -routerTrace .N I -macTrace .55 I -movementTrace .55 for Jset i 0K JGi L Gval'nn) K Jincr iK J set no#eD'Gi) EGnsD no#eF Gno#eD'Gi) ran#om-motion 0 K

JH (isa-le ran(om motion

Confi+urin+ $ovement
H Provi(e initial (L,M, for no, PI#) co-or(inates for mo-ileno(es H Gno#eD'0) set MD NO010 Gno#eD'0) set PD O0010 Gno#eD'0) set QD 010 Gno#eD'2) set MD O0010 Gno#eD'2) set PD O0010 Gno#eD'2) set QD 010 Gno#eD'2) set MD AO010 Gno#eD'2) set PD O0010 Gno#eD'2) set QD 010 H /oa( the go( o-2ect ,ith shortest hop information H G+o#D set-#ist 2 2 2 G+o#D set-#ist 0 2 2 G+o#D set-#ist 0 2 2

!etup traffic flow


H No, pro(uce some simple no(e movements H No(e7(%) starts to move up,ar( an( then (o,n,ar( set +o#D ERo# instanceF GnsD at 20010 SGno#eD'2) set#est O0010 T0010 210S GnsD at O0010 SGno#eD'2) set#est O0010 20010 210S H Setup traffic flo, -et,een no(es # connecting to 2 at time %##$# set u#pD'0) Enew A+ent:UD3F Gu#pD'0) set fi#D 2 GnsD attach-a+ent Gno#eD'0) Gu#pD'0) set nullD'0) Enew A+ent:NullF GnsD attach-a+ent Gno#eD'2) GnullD'0) set c"rD'0) Enew Application:Traffic:C4,F Gc"rD'0) set packet!i@eD 200 Gc"rD'0) set intervalD 210 Gc"rD'0) set ran#omD 2 Gc"rD'0) set ma(pktsD 20000 Gc"rD'0) attach-a+ent Gu#pD'0) GnsD connect Gu#pD'0) GnullD'0) GnsD at 20010 SGc"rD'0) startS

!ewin+ Up
H'efine no(e initial position in nam, only fro nam for Jset i 0K JGi L Gval'nn)K Jincr iK J H The function must -e calle( after mo-ility mo(el is (efine($ GnsD initialDno#eDpos Gno#eD'Gi) A0 K H Tell no(es ,hen the simulation en(s for Jset i 0K JGi L Gval'nn) K Jincr iK J GnsD at Gval'stop) SGno#eD'Gi) resetSH K GnsD at Gval'stop) SstopS GnsD at Gval'stop) Sputs ISN! EM7T7NR111IS H GnsD haltS proc stop JK J +lo"al nsD tracef# namf# GnsD flush-trace close Gtracef# close Gnamf# K puts Gtracef# S$ 010 nn Gval'nn) ( Gval'() y Gval'y) rp Gval'rp)S puts Gtracef# S$ 010 sc Gval'sc) cp Gval'cp) see# Gval'see#)S puts Gtracef# S$ 010 prop Gval'prop) ant Gval'ant)S puts S!tartin+ !imulation111S GnsD run

Analysin+ !imulation ,outin+ messa+e Up#ates


s -t #$##%=8#&"% ->s # ->( -% -Ni # -N9 &=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( # - s # - t # -1s #$2== -1( -%$2== -1t message -1l &2 -1f # -1i # -1v &2 r -t #$##2&&2""% ->s % ->( -% -Ni % -N9 =##$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( ffffffff - s # - t "## -1s #$2== -1( -%$2== -1t message -1l &2 -1f # -1i # -1v &2 s -t #$#&8=56#"2 ->s 2 ->( -% -Ni 2 -N9 8=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( # - s # - t # -1s 2$2== -1( -%$2== -1t message -1l &2 -1f # -1i % -1v &2 r -t #$#&55#8="2 ->s % ->( -% -Ni % -N9 =##$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( ffffffff - s 2 - t "## -1s 2$2== -1( -%$2== -1t message -1l &2 -1f # -1i % -1v &2 s -t %$%2#%26885 ->s % ->( -% -Ni % -N9 =##$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( # - s # - t # -1s %$2== -1( -%$2== -1t message -1l &2 -1f # -1i 2 -1v &2 r -t %$%2%6&5%85 ->s 2 ->( -% -Ni 2 -N9 8=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( ffffffff - s % - t "## -1s %$2== -1( -%$2== -1t message -1l &2 -1f # -1i 2 -1v &2 r -t %$%2%6&5%85 ->s # ->( -% -Ni # -N9 &=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( ffffffff - s % - t "## -1s %$2== -1( -%$2== -1t message -1l &2 -1f # -1i 2 -1v &2

Analysin+ !imulation 3acket Transmission


s -t %##$######### ->s # ->( -2 -Ni # -N9 &=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl !.T -N, --- - a # - ( # - s # - t # -1s #$# -1( 2$# -1t c-r -1l 2## -1f % -1i &# -1v &2 -Pn c-r -Pi # -Pf # -Po & r -t %##$######### ->s # ->( -2 -Ni # -N9 &=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( # - s # - t # -1s #$# -1( 2$# -1t c-r -1l 2## -1f % -1i &# -1v &2 -Pn c-r -Pi # -Pf # -Po & s -t %##$######### ->s # ->( % -Ni # -N9 &=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a # - ( # - s # - t # -1s #$# -1( 2$# -1t c-r -1l 22# -1f % -1i &# -1v &2 -Pn c-r -Pi # -Pf # -Po & r -t %##$##=44#=## ->s % ->( % -Ni % -N9 =##$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a %&a - ( % - s # - t "## -1s #$# -1( 2$# -1t c-r -1l 22# -1f % -1i &# -1v &2 -Pn c-r -Pi # -Pf % -Po & f -t %##$##=44#=## ->s % ->( 2 -Ni % -N9 =##$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, --- - a %&a - ( % - s # - t "## -1s #$# -1( 2$# -1t c-r -1l 22# -1f % -1i &# -1v &% -Pn c-r -Pi # -Pf % -Po & r -t %##$#%%58=### ->s 2 ->( 2 -Ni 2 -N9 8=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl !.T -N, --- - a %&a - ( 2 - s % - t "## -1s #$# -1( 2$# -1t c-r -1l 22# -1f % -1i &# -1v &% -Pn c-r -Pi # -Pf 2 -Po &

Analysin+ !imulation No#e movement an# 3acket Drop


C 2##$##### % (=##$##, =##$##, #$##), (=##$##, 4##$##), 2$##

C ( -t &#2$2"646"525 ->s # ->( % -Ni # -N9 &=#$## -Ny =##$## -N? #$## -Ne -%$###### -Nl )T) -N, C0Q - a %&a - ( % - s # - t "## -1s #$# -1( 2$# -1t c-r -1l 22# -1f % -1i %56 -1v &2 -Pn c-r -Pi 4" -Pf # -Po &

Thank you R

You might also like