Professional Documents
Culture Documents
NS3 Tutorial
NS3 Tutorial
Gustavo J. A. M. Carneiro
INESC Porto / Faculdade de Engenharia / Universidade do Porto
Rede Temtica de Comunicaes Mveis 2009-02- 3
RTCM
2009-02-13
!utline
"ntroduction/ NS-2
0!ver 102 o. $CM and "333 net(or# simulation %a%ers .rom 2000-2004 cite t)e use o. ns-2
Need to %arse trace .iles to e&tract results Trace .iles end u% eit)er
<avin, in.ormation researc)ers do not need= or Missin, in.ormation ">ts usual %ractice to add %rint.>s in t)e ns-2 code
RTCM 2009-02-13 3
"ntroduction/ NS-3
NS-3 Modules
.ode class .etDevice A/C Address types &+ v-, %AC, etc'( 0ueues Socket A/C + v- A/Cs acket Sockets
!igh,level $rappers #or everything else' .o smart pointers used' Aimed at scripting'
Smart pointers Dynamic type system Attributes Callbacks, Tracing Logging Random Variables
e&am%lesD.irst9cc 8 D B;
int main (int argc, char *argv[]) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_I !O)" LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_I !O)" #an$omVariable%%&'eGlobal(ee$ (), ), *, +, ,, -)" o$eContainer nodes" nodes.Create (2)"
nodes: NodeContainer .ode 1 .ode 2
RTCM
2009-02-13
e&am%lesD.irst9cc 82 D B;
/oint0o/oint1elper pointToPoint" pointToPoint.(et2evice3ttrib4te (5DataRate5, (tringVal4e (55Mbps5))" pointToPoint.(etChannel3ttrib4te (5Delay5, (tringVal4e (52ms5))" et2eviceContainer devices" devices 6 pointToPoint.Install (nodes)"
devices: NetDeviceContainer
.ode 1
.ode 2 7
e&am%lesD.irst9cc 83 D B;
Internet(tac71elper stack" stack.In'tall (nodes)" Ipv83$$re''1elper address" address.(et9a'e (510.1.1.05, 5255.255.255.05)" Ipv8Inter:aceContainer interfaces 6 address.Assign (devices)"
inter#aces: $%v4$nter&aceContainer
21'2'2'3 .ode 2
e&am%lesD.irst9cc 84 D B;
&$pEcho(erver1elper echoServer (;)" 3pplicationContainer serverApps 6 echoServer.In'tall (nodes.Get ()))" serverApps.Start ((econ$' ().<))" serverApps.Stop ((econ$' ()<.<))"
serverApps: '%%(icationContainer
RTCM
2009-02-13
e&am%lesD.irst9cc 81 D B;
&$pEchoClient1elper echoClient (interfaces.Get3$$re'' ()), ;)" echoClient.(et3ttrib4te (5=a>/ac7et'5, &integerVal4e ()))" echoClient.(et3ttrib4te (5Interval5, 0imeVal4e ((econ$' ().)))" echoClient.(et3ttrib4te (5/ac7et(i?e5, &integerVal4e ()<*8))" 3pplicationContainer clientApps 6 echoClient.Install (nodes.Get (<))" clientApps.(tart ((econ$' (*.<))" clientApps.(top ((econ$' ()<.<))"
clientApps: '%%(icationContainer De t: 10-1-1-2! %ort 9 )d%*c+oC(ient .ode 1 1 %ac.et! 1024 b/te )d%*c+o,erver .ode 2
RTCM
2009-02-13
10
e&am%lesD.irst9cc 8B D B;
[@] Simulator Simulator ret4rn <" A Run ()" Destroy ()"
)d%*c+oC(ient .ode 1 &21'2'2'2( )d%*c+o,erver .ode 2 &21'2'2'3(
)DP Pac.et
3 4
)DP Pac.et
B .CDa: EEr4n :ir't [@] (ent )<*8 bFte' to )<.).).* 2 #eceive$ )<*8 bFte' :rom )<.).).) 3 #eceive$ )<*8 bFte' :rom )<.).).* 4
RTCM 2009-02-13 11
import n'+
e&am%lesD.irst9cc in -+t)on
n'+.LogComponentEnable(5&$pEchoClient3pplication5, n'+.LOG_LEVEL_I !O) n'+.LogComponentEnable(5&$pEcho(erver3pplication5, n'+.LOG_LEVEL_I !O) n'+.#an$omVariable.&'eGlobal(ee$(), ), *, +, ,, -) no$e' 6 n'+. o$eContainer() no$e'.Create(*) point0o/oint 6 n'+./oint0o/oint1elper() point0o/oint.(et2evice3ttrib4te(52ata#ate5, n'+.(tringVal4e(5,=bp'5)) point0o/oint.(etChannel3ttrib4te(52elaF5, n'+.(tringVal4e(5*m'5)) $evice' 6 point0o/oint.In'tall(no$e') 'tac7 6 n'+.Internet(tac71elper() 'tac7.In'tall(no$e') a$$re'' 6 n'+.Ipv83$$re''1elper() a$$re''.(et9a'e(n'+.Ipv83$$re''(5)<.).).<5), n'+.Ipv8=a'7(5*,,.*,,.*,,.<5)) inter:ace' 6 a$$re''.3''ign($evice') echo(erver 6 n'+.&$pEcho(erver1elper(;) 'erver3pp' 6 echo(erver.In'tall(no$e'.Get())) 'erver3pp'.(tart(n'+.(econ$'().<)) 'erver3pp'.(top(n'+.(econ$'()<.<)) echoClient 6 n'+.&$pEchoClient1elper(inter:ace'.Get3$$re''()), ;) echoClient.(et3ttrib4te(5=a>/ac7et'5, n'+.&integerVal4e())) echoClient.(et3ttrib4te(5Interval5, n'+.0imeVal4e(n'+.(econ$' ().<))) echoClient.(et3ttrib4te(5/ac7et(i?e5, n'+.&integerVal4e()<*8)) client3pp' 6 echoClient.In'tall(no$e'.Get(<)) client3pp'.(tart(n'+.(econ$'(*.<)) client3pp'.(top(n'+.(econ$'()<.<)) n'+.(im4lator.#4n() n'+.(im4lator.2e'troF()
RTCM
2009-02-13
12
'o(er 'evel
RTCM
2009-02-13
13
Simulator Core
Time class su%%orts )i,) %recision 2E *it time values 8nanosecond %recision;
0ime t) 6 (econ$' ()<)" 0ime t* 6 t) G =illi(econ$' ()<<)" 't$%%co4t HH t*.Get(econ$' () HH 't$%%en$l" CC t* 6 )<.)
voi$ =FCallbac7 (0) param), 0* param*) {...A [...] (im4lator%%(che$4le ((econ$' (+), =FCallbac7, param), param*)" Falues para ! and para 2 %assed as call*ac# %arameters
14
Random Faria*les
@ni.orm/ values uni.orml+ distri*uted in an interval Constant/ value is al(a+s t)e same 8not reall+ random; SeGuential/ return a seGuential list o. %rede.ined values 3&%onential/ e&%onential distri*ution 8%oisson %rocess; Normal 8,aussian; 'o,-normal %areto= (ei*ull= trian,ular= H
012 032
rng 6 n'+.!ormal"ariable()<.<, ,.<) > 6 [rng.#et"alue() :or t in range()<<<<<)] pFlab.hi't(>, )<<) pFlab.'hoD() RTCM 2009-02-13 15
Memor+ Mana,ement
Smart %ointers
/ac7et *p 6 neD /ac7et" # refcount starts as 1 pEJ#e: ()" # refcount becomes pEJ&nre: ()" # refcount becomes 1 pEJ&nre: ()" # refcount becomes !" packet is freed
voi$ =F!4nction () { $trH/ac7etJ p 6 CreateH/ac7etJ ()<)" 't$%%cerr HH K/ac7et 'i?e% K HH pEJGet(i?e () HH 't$%%en$l" A # Packet is released #smart pointer goes out of scope$ RTCM 2009-02-13 16
-ac#ets
@nits o. in.ormation sent and received *+ a%%lications "n.ormation c)un#s o. ()at (ill *ecome a real %ac#et 8similar s#I*u.. in 'inu& #ernel; Simulated %ac#ets and '2D' .rames *ein, transmitted Create em%t+ %ac#et
Casic @sa,e
J6umm+J *+tes are simulated as *ein, t)ere= *ut do not actuall+ occu%+ an+ memor+ 8reduces memor+ .oot%rint;
/trH/ac7etJ pac7et 6 CreateH/ac7etJ (KhelloK, ,)" /trH/ac7etJ pac7et* 6 pac7et)EJCopF ()"
Co%+ a %ac#et
17
Nodes
Node class
Re%resents a net(or# element Ma+ )ave an IPv4 stack o*Aect Ma+ )ave a mobility model
Cut it is com%letel+ o%tionalK
'%%(ication
0--5
Node4i t
1
0--5
$%v4
Node
1 0--5
Mobi(it/Mode(
NetDevice
Trac#s all nodes ever created Node inde& LMN -tr conversions
RTCM 2009-02-13 1#
Call*ac# !*Aects
3&am%le
$o4ble =F!4nc (int >, :loat F) { ret4rn $o4ble (> G F) C *" A [...] Callbac%H$o4ble, int, :loatJ cb)" cb) 6 Ma%eCallbac% (=F!4nc)" $o4ble re'4lt 6 cb) &*, +'" %% result receives &' (&&&) cla'' =FCla'' { p4blic% $o4ble =F=etho$ (int >, :loat F) { ret4rn $o4ble (> G F) C *" A" (&&&) Callbac%H$o4ble, int, :loatJ cb)" =FCla'' mFobL" cb) 6 Ma%eCallbac% (I=FCla''%%=F=etho$, (myob))" $o4ble re'4lt 6 cb) &*, +'" %% result receives &' RTCM 2009-02-13 19
Node
0--5
Device4i t
C "aNetDevice
int main (int argc, char *argv[]) { [@] Con:ig%%Connect (5C o$eLi'tC*C2eviceLi'tC*C0>M4e4eC2rop5, =a7eCallbac7 (I0race2evM4e4e2rop))" [@] A
T867e7e
67e7e
RTCM
2009-02-13
20
Scalability .eatures
Oor du " a%%lication data t)at (e don>t care a*out No memor+ is allocated .or virtual Pero *+tes Reduces t)e memor+ .oot%rint o. t)e simulation
" $"tes;
No memor+ (aste in "-v4 stac# .or nodes t)at don>t need it Mo*ilit+ model ma+ not *e needed Ne( .eatures can *e easil+ added in t)e .uture
Oor e&am%le= ener,+ models 39,9 (ired netdevices do not need to #no( t)e node %osition at all
Cross-layer .eatures
-ac#et Ta,s
Tracin,
Small units o. in.ormation attac)ed to %ac#ets $llo( to re%ort events across non-conti,uous la+ers
RTCM 2009-02-13 21
Run unmodi.ied -!S"R %ro,rams under simulation Runnin, routin, daemons on NS-3 8%lanned;
RTCM 2009-02-13
S%ecial 3'O loader converts -!S"R $-" calls into NS-3 calls
22
RTCM
2009-02-13
23
FisualiPation
RTCM
2009-02-13
24
NS 3/ -er.ormance
Source/ 39 5ein,Srtner= <9 'e)n= and Q9 5e)rle= + per'or ance co parison o' recent net)or& si ulators= "333 "nternational Con.erence on Communications 20099
RTCM
2009-02-13
%ne sending node generates one pac&et ever" second and $roadcasts it to its neigh$ors The neigh$oring nodes rela" unseen essages a'ter a dela" o' one second( thus 'looding the entire net)or&* 25
Source/ 39 5ein,Srtner= <9 'e)n= and Q9 5e)rle= + per'or ance co parison o' recent net)or& si ulators= "333 "nternational Con.erence on Communications 20099 26 RTCM 2009-02-13
Source/ 39 5ein,Srtner= <9 'e)n= and Q9 5e)rle= + per'or ance co parison o' recent net)or& si ulators= "333 "nternational Con.erence on Communications 20099 27 RTCM 2009-02-13
Conclusion
!ne o. t)e .astest simulators around T)e most memor+ e..icient simulator around Not man+ models availa*le .or NS-3 +et No :@" to *uild to%olo,+ FisualiPation still e&%erimental
RTCM 2009-02-13 2#
<o(ever
Tuestions7
RTCM
2009-02-13
29
RTCM
2009-02-13
30
RTCM
2009-02-13
31
RTCM
2009-02-13
33
5rites t)e )eader in.ormation as a *+te streamU Reads t)e )eader in.ormation .rom a *+te streamU
<eaders and trailers used to im%lement %rotocols -ac#ets contain e&act %rotocol *+te contents
T)e+ are not Aust structures li#e in NS2 $llo(s (ritin, %ca% trace .iles= reada*le .rom (ires)ar#
RTCM
2009-02-13
34
4int+*_t Llc(nap1ea$er%%Get(eriali?e$(i?e (voi$) con't { ret4rn ) G ) G ) G + G *" A voi$ Llc(nap1ea$er%%(eriali?e (94::er%%Iterator 'tart) con't { 94::er%%Iterator i 6 'tart" 4int-_t b4:[] 6 {<>aa, <>aa, <><+, <, <, <A" i.*rite (b4:, N)" i.Orite+ton&)N (m_ether0Fpe)" A 4int+*_t Llc(nap1ea$er%%2e'eriali?e (94::er%%Iterator 'tart) { 94::er%%Iterator i 6 'tart" i.!e2t (,G))" %% skip * b+tes" don,t care about content m_ether0Fpe 6 i.#ea$!to,&)N ()" ret4rn #etSeriali3edSi3e ()" A
$ddin, a )eader/
Llc(nap1ea$er llc'nap" llc'nap.(et0Fpe (<><-<<)" Q Ipv8 pac7etEJAdd+eader (llc'nap)" Llc(nap1ea$er llc'nap" i: (pac7etEJRemove+eader (llc'nap) { 't$%%co4t HH llc'nap.Get0Fpe () HH 't$%%en$l" A
Removin, a )eader/
RTCM
2009-02-13
35
-ac#et/ Ta,s
Ta,s
Small c)un#s o. in.ormation $n+ num*er o. ta,s can *e attac)ed a %ac#et Ta,s are #e+ed *+ t)e a structure t+%e itsel.
Ne( ta, t+%es are de.ined similarl+ to )eader t+%es $ttac) conte&t in.ormation to a %ac#et
3&am%le/ Net6evice attac)es destination M$C address ()en Gueuein,= retrieves it ()en deGueuin, .or transmission
class !*Aect
!*Aect a,,re,ation
C!M-li#e inter.ace Guer+ mec)anism "nstead o. a )u,e class= s%lit class into several o*Aects/
Node= "%v4= V@d%DTc%WSoc#etOactor+= Mo*ilit+=999
3&am%le/ .rom a Node o*Aect= see i. it su%%orts "%v4 voi$ =F!4nction (/trH o$eJ no$e) { /trHIpv8J ipv8 6 no$eEJGetObLectHIpv8J ()" i: (ipv8 T6 &LL) 't$%%cerr HH K o$e ha' K HH ipv8EJGet #o4te' () HH Kro4te'.K HH 't$%%en$l" A
Tracin, )oo#s
RTCM
2009-02-13
37
"n C??= classes are not 'irst-class o$,ects T+%e name 'ist o. attri$utes or trace sources 3&am%le/ to create an o*Aect .rom t+%e name/
T+%e"d is an o*Aect t)at descri*es a class t+%e/ T+%e"d im%lements t)e Factor" 6esi,n -attern
RTCM
2009-02-13
3#
Cecause o. t)e T+%e"d s+stem= creatin, !*Aect instances s)ould *e done (it)/
39
6e*u,,in, Su%%ort
$*orts t)e %ro,ram i. e&%ression evaluates to .alse "ncludes source .ile name and line num*er Oorces an unconditional *rea#%oint= com%iled in -ur%ose
-ro%erties
39,9/
5)en de*u,,in, is done= lo,,in, does not ,et in t)e (a+ o. e&ecution %er.ormance
RTCM 2009-02-13 40
'o,,in, levels/
'o,,in, Jcom%onentsJ
NSI'!:I3RR!R 8999;/ serious error essages onl" NSI'!:I5$RN 8999;/ )arning essages NSI'!:I63C@: 8999;/ rare ad-hoc de$ug essages NSI'!:I"NO! 8999;/ in'or ational essages -eg* $anners/ NSI'!:IO@NCT"!N 8999;/ 'unction tracing NSI'!:I-$R$M 8999;/ para eters to 'unctions NSI'!:I'!:"C 8999;/ control 'lo) tracing )ithin 'unctions
-ro,ramaticall+/
'o,Com%onent3na*le8Y!lsr$,entY= '!:I'3F3'I$'';U
(_LOG65Ol'r3gent5 .CmFEprogram 2009-02-13 41
$%%lications are li#e processes in a normal s+stem Soc#ets re%resent communication end %oints NS-3 soc#ets modelled a.ter t)e CS6 soc#et $-" Tra..ic ,enerators 8e9,9 !n!..$%%lication; Tra..ic sin#s 8e9,9 to res%ond to connection reGuests; Routin, a,ents= )i,)er level %rotocols
42
NS-3 Soc#ets
-lain C soc#ets
NS-3 soc#ets
int '7" '7 6 soc%et(/!_I E0, (OCU_2G#3=, <)" 'tr4ct soc%addr5in 'rc" inet_pton(3!_I E0,K<.<.<.<K,I'rc.'in_ a$$r)" 'rc.'in_port 6 hton'(-<)" bind('7, ('tr4ct 'oc7a$$r *) I'rc, 'i?eo:('rc))" 'tr4ct 'oc7a$$r_in $e't" inet_pton(3!_I E0,K)<.<.<.)K,I$e't.'i n_a$$r)" $e't.'in_port 6 hton'(-<)" sendto('7, KhelloK, N, <, ('tr4ct 'oc7a$$r *) I$e't, 'i?eo:($e't))" char b4:[N]" recv('7, b4:, N, <)"
'7EJSetReceiveCallbac% (=a7eCallbac7 (-+Socket.eceive))" (/) #Simulator00.un #$$ voi$ -+Socket.eceive (/trH(oc7etJ '7, /trH/ac7etJ pac%et) { ... A
RTCM
2009-02-13
43
Mo*ilit+ Models
void Set-osition 8Fector %os; Fector :et-osition 8; Node is at a .i&ed locationU does not move on its o(n 8(or#s inside a rectan,ular *ounded area; Node %auses .or a certain random time Node selects a random (a+%oint and s%eed Node starts (al#in, to(ards t)e (a+%oint 5)en (a+%oint is reac)ed= ,oto .irst state 8(or#s inside a rectan,ular *ounded area; Node selects a random direction and s%eed Node (al#s in t)at direction until t)e ed,e Node %auses .or random time Re%eat RTCM 2009-02-13
StaticMo*ilit+Model
Random5a+%ointMo*ilit+Model
Random6irectionMo*ilit+Model
44
Cuildin, it
)) '4$o aptEget in'tall b4il$Ee''ential gGG pFthon merc4rial Q (&b4nt4) *) hg clone http%CCco$e.n'nam.orgCn'E+EallinoneC +) c$ n'E+Eallinone 8) .C$oDnloa$.pF # 1ill do1nload components ,) .Cb4il$.pF # 1ill build 2S34 N) c$ n'E+E$ev
Cuildin, it
)) In'tall b4il$ tool' )) CFgDin or =ingD GCC (gGG) *) /Fthon% http%CCDDD.pFthon.org +) =erc4rial% http%CCmerc4rial.ber7Doo$.comC *) hg clone http%CCco$e.n'nam.orgCn'E+.<.))C +) c$ n'E+.<.)) 8) Da: con:ig4re Q optional% E$ optimi?e$ ,) Da: chec7 Q r4n' 4nit te't'
RTCM
2009-02-13
46