You are on page 1of 12

Chng 5 NS Tutorial

Mc lc

5.1.1. Bt u nh th no . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.2. Hai node, mt lin kt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1.3. Gi d liu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2. Topo trong NS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.1. To topo trong NS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.2. To cc s kin. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.3. nh nhn cho lung d liu . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2.4. Gim st hng i. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3. Mng c tnh cht ng . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.1. To mt Topo ln hn. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.3.2. Lin kt li . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

co
84

5.1. Kch bn Tcl u tin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

84 85 86 87 88 88 89 91 91 92 93 94

Ngi dch: Trnh Vit Thi

Bin tp: H Tt Thnh, Trn Cng L Bn gc: Tutorial for the Network Simulator "ns", part 4,5,6 [3]

5.1. Kch bn Tcl u tin


Ghi ch: Trong phn ny, ti s hng dn bn s vit mt tp lnh Tcl cho ns m phng cu hnh mng n gin. Bn s bit c lm th no to ra nhng nt (nodes)

vn

te le

v nhng lin kt (links), lm th no gi d liu t mt node n node khc, lm th no iu khin hng i hay chy NAM t tp lnh va vit hin th qu trnh m phng.

5.1.1. Bt u nh th no
Chng ta s vit mt mu chung c th s dng li cho mi ln nh vit tp lnh Tcl mi. Bn c th vit tp lnh Tcl bng bt c trnh son tho no nh joe hoc emacs. Ti t tn cho v d ny l example1.tcl. u tin, cn to ra i tng m phng bng cu lnh:

Tip theo to tp tin phc v cho chc nng bm vt d liu (trace data) ca NAM (network animator) set nf [open out.nam w] $ns namtrace-all $nf

Dng lnh u tin s m tp tin out.nam vi nh danh l nf. Trong dng lnh th hai, truyn tham s l i tng c to ra trn ns ghi tt c d liu m phng c lin quan n nam vo trong tp tin ny.

vn te le
proc finish {} { global ns nf close $nf exit 0 $ns flush-trace exec nam out.nam & } ng. phng. $ns at 5.0 "finish" Dng cui cng l kt thc vic m phng

Bc tip theo thm th tc finish, kt thc cc tp tin bm vt v bt u nam,

th tc ny ng file bm vt v khi ng network animator.

Bn khng cn phi hiu tt c on m trn, s d hiu hn khi bn nhn thy n hot

Dng tip theo bo chng trnh m phng thc hin th tc finish sau 5.0 giy m

Nhn dng lnh bn c th hiu ngha ca n. NS cung cp cho bn mt cch n gin lp lch cho cc s kin bng cu lnh at .

co m
85

set ns [new Simulator]

$ns run By gi bn c th lu tp tin v chy th bng lnh ns example1.tcl. Bn c th gp thng bo li nh nam: empty trace file out.nam bi v chng ta cha nh ngha bt c i tng ( node, lin kt. . . ) hay s kin no. Chng ta s hc cch nh ngha cc i tng trong phn 5.2 v cc s kin trong phn ??. Bn s phi s dng li on m trong phn ny cc phn khc. Bn c th ti ti y hoc ti y

5.1.2. Hai node, mt lin kt


Trong phn ny chng ta s nh ngha mt cu hnh mng (topo) n gin vi hai node c kt ni vi nhau bi mt lin kt. Bn di c hai dng lnh nh ngha hai node. (Ch : bn phi chn on m ny trc dng lnh $ns run hay tt hn l trc dng lnh $ns at 5.0 "finish). set n0 [$ns node] set n1 [$ns node] Lnh $ns node l to ra 1 node mi, nh vy 2 cu lnh trn to ra hai node mi l n0 v n1. Dng tip theo kt ni hai node $ns duplex-link $n0 $n1 1Mb 10ms DropTail Dng ny thit lp lin kt song cng vi bng thng 1Megabit, tr 10ms v hng i DropTail gia node n0 v n1. By gi bn c th lu tp tin v bt u on m vi ns example1.tcl. Nam s t ng bt u v bn c th thy kt qu tng t nh hnh bn di.

Hnh 5.1: To kt ni gia n0 v n1

Bn c th ti v d y ti y hoc ti y nu n khng hot ng v bn ngh mc li.

86

5.1.3. Gi d liu
D nhin, v d trn l cha hon thin v bn ch c th thy c m hnh topo v cha c hot ng xy ra, v th bc tip theo l gi d liu t node n0 n node n1. Trong ns, d liu lun c gi t mt tc nhn (agent) n tc nhn khc. Do , cn phi to ra mt tc nhn gi d liu t node n0, mt tc nhn khc nhn c d liu trn node n1. #Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 # Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 Hai lnh u to ra tc nhn UDP (User Datagram Protocol:giao thc d liu ngn ngi dng) v gn n cho node n0. Nhng cu lnh tip theo to ra b to d liu tc khng i CBR (Constant Bit Rate:tn s bit bt bin) m c th t ng gi cc gi tin c di 500 byte cch nhau 0.005 giy (tc l 200 gi tin/giy). Bn c th tm thy cc thng s thch hp cho mi tc nhn ns manual page Dng tip theo l to mt tc nhn Null vi tc ng lm suy gim lu lng gn n vo vo node nt n1. set null0 [new Agent/Null] $ns attach-agent $n1 $null0 Hai tc nhn c kt ni vi nhau theo cu lnh: $ns connect $udp0 $null0 V by gi, ta cn bo trm CBR khi no th gi d liu, khi no th dng. Ch : Tt nht l t cc dng lnh ny ngay trc dng "$ns at 5.0 "finish" $ns at 0.5 "$cbr0 start" By gi bn c th lu tp tin v bt u m phng li. Khi bn lick vo nt play ca s nam, bn s thy 0.5 giy sau m phng node 0 bt u gi gi d liu ti node 1. Bn c th lm chm nam bng con trt Step $ns at 4.5 "$cbr0 stop"

87

Hnh 5.2: Gi d liu gia n0 v n1 Nh vy bn c mt cht kinh nghim vi nam v on m Tcl. Bn c th click bt k gi d liu trong ca s nam gim st n hay click trc tip ln cc lin kt c mt s th thng k. Bn nn thay i tham s packetsize_ v interval_ trong on m Tcl thy c iu g xy ra. Bn c th ti ton b v d ti y hoc ti y Hu ht cc thng tin cn thit m ti cn c th vit on m Tcl ny c ly trc tip t cc tp tin v d th mc tcl/ex/, khi thit lp cc thng s CBR (packetSize_, interval_) ti phi ly t ns manual page

5.2. Topo trong NS


Trong phn ny chng ta s nh ngha mt m hnh vi bn node trong mt node hot ng nh l mt router chuyn d liu m hai node khc ang gi n node th t. Ti s gii thch cch phn bit cc lung d liu t hai node ny vi nhau v ti s trnh by mt hng i c th c gim st nh th no c th thy c n b trn nh th no, v s c bao nhiu gi tin b loi

5.2.1. To topo trong NS


Nh mi khi, u tin l xc nh topo. Bn nn to mt ra tp tin example2.tcl s dng m ny t phn 5.1 nh l mt tp tin mu, Nh ti ni trn, on m ny s tng t vi mu trn. Bn s lun cn phi to ra mt i tng m phng, lun bt phi u bng cng mt cu lnh v nu mun chy nam t ng bn s lun phi m tp tin bm vt, khi to n, v nh ngha th tc ng n v khi ng nam. By gi chn dng lnh vo trong m to ra bn node. set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] Bn di l v d m Tcl to ra ba lin kt song cng gia cc nt.

88

$ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms DropTail Bn c th lu v khi ng on m by gi. Bn c th thy rng m hnh mng c v hi rc ri trong nam. Bn c th bm vo nt re-layout thy r hn nhng s tt hn nu c thm mt cht iu chnh trong vic b tr topo. Thm 3 dng sau vo on m Tcl ca bn v khi ng li n. $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 By gi, bn s c th hiu on m ny lm ci g khi nhn topo trong ca s nam. N trng ging nh hnh bn di

Hnh 5.3: Topo ca on m gm 4 nt

Ch rng cc phn lin quan n b tr t ng c b qua, do n b tr theo mnh. Ty chn cho hng ca cc cc lin kt l phi, tri, ln, xung v s kt hp cc hng . Bn c th th nghim vi cc ci t sau, nhng by gi hy thot khi topo mng theo ng cch.

5.2.2. To cc s kin
By gi chng ta to ra hai tc nhn UDP vi lung ti nguyn CBR v gn chng cho node n0 v n1. Ri to ra tc nhn Null v gn n cho node n3. #Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 # Create a CBR traffic source and attach it to udp0

89

set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 #Create a UDP agent and attach it to node n1 set udp1 [new Agent/UDP] $ns attach-agent $n1 $udp1 # Create a CBR traffic source and attach it to udp1 set cbr1 [new Application/Traffic/CBR] $cbr1 set packetSize_ 500 $cbr1 set interval_ 0.005 $cbr1 attach-agent $udp1 set null0 [new Agent/Null] $ns attach-agent $n3 $null0 Hai tc nhn CBR cn phi c kt ni vi tc nhn Null. $ns connect $udp0 $null0 $ns connect $udp1 $null0 Chng ta mun tc nhn CBR th nht bt u gi lc 0.5 giy v kt thc lc 4.5 giy trong khi tc nhn CBR th hai bt u lc 1.0 giy v kt thc lc 4.0 giy. $ns at 0.5 "$cbr0 start" $ns at 1.0 "$cbr1 start" $ns at 4.0 "$cbr1 stop" $ns at 4.5 "$cbr0 stop" Khi bn bt u on m ns example2.tcl ngay by gi, bn s rng lu lng trn cc knh truyn t n0 n n2 v t n1 n n2 nhiu hn lu lng m knh truyn t n2 n n3 c th mang c. Mt tnh ton n gin xc nhn vic ny: Chng ti ang gi 200 gi tin/giy trn mi knh trong hai knh truyn u tin vi kch thc gi tin l 500 byte. Iu ny n n mt bng thng 0.8 Mbps cho cc knh truyn t n0 n n2 v t n1 n n2.. Tng bng thng l 1.6 Mb/giy nhng knh truyn gia n2 v n3 ch c dung lng l 1Mb/giy, v th hin nhin l vi gi tin b loi ra. Nhng gi tin no b loi? C hai lung u mu en v th ch c th thy c iu g ang din ra i vi gi

90

tin l gim st chng trong nam bng cch click vo chng. Tip theo ca phn 2, ti s trnh by cch lm th no phn bit cc lung khc nhau v lm th no thy tht s iu g ang thc s xy ra hng i ti knh truyn t n2 n n3.

5.2.3. nh nhn cho lung d liu


Thm vo hai dng lnh bn di xc nh tc nhn CBR ca bn. $udp0 set class_ 1 $udp1 set class_ 2 Tham s fid_ l vit tt ca flow id. By gi thm vi cu lnh vo tp lnh Tcl, tt hn l lc bt u, ngay sau khi to i tng m phng, do y l mt phn ca s thit lp m phng $ns color 1 Blue $ns color 2 Red on m ny cho php thit lp cc mu sc khc nhau ca mi lung.

Hnh 5.4: Mu sc cc lung

By gi bn c th khi ng li tp lnh v mt lung c mu xanh, lung kia mu . Khi theo di kt ni t node n2 n node n3 v bn s rng sau mt thi gian truyn th cc phn b gia cc gi tin mu xanh v mu khng cn cn bng na (ch t l trn h thng ca ti). Trong phn tip theo ti s trnh by cho bn lm th no c th theo di bn trong hng i ca lin kt ny tm hiu chuyn g ang xy ra .

5.2.4. Gim st hng i


Bn ch cn thm dng lnh bn di vo on m ca bn gim st hng i cho kt ni t n2 ti n3

91

$ns duplex-link-op $n2 $n3 queuePos 0.5 Khi ng ns li v bn s thy mt hnh nh tng t bn di sau mt thi gian ngn

Hnh 5.5: Gim st hng i

By gi bn c th thy cc gi tin trong hng i v sau c th thy cc gi tin b loi b nh th no v i qua nh th no, mc d ch c gi tin mu xanh b loi (ch t trong h thng ca ti, ti on l c th khc nhau trong phin bn sau hoc trc ). Nhng bn khng th trong i qu nhiu vo s cng bng t mt hng DropTail n gin. V th hy c ci thin bng cch s dng hng i SFQ (stochastic fair queueing: hng i cng bng ngu nhin) cho kt ni t n2 n n3. Thay i nh ngha cho lin kt gia n2 v n3 vi dng lnh bn di $ns duplex-link $n3 $n2 1Mb 10ms SFQ By gi, vic xp hng c l l cn bng ri. S lng mu xanh v mu b loi l nh nhau.

Hnh 5.6: Cn bng trong gim st hng i

Bn c th ti v d y y hoc y

5.3. Mng c tnh cht ng


Trong phn ny ti s trnh by cho bn mt v d v mng ng vic nh tuyn chnh sa li cc knh truyn b li. Bng cch ny ti s ch cho bn c th gi s lng ln cc nt trong mt mng Tcl thay v cp cho mi nt mt ci tn ring.

92

5.3.1. To mt Topo ln hn
Ti gi cho bn gi tp lnh Tcl trong v d ny l example3.tcl. Bn c th chn vo mu ti y hoc ti y, trong phn 5.1 Thng thng, trc tin, mt topo c to ra, mc d ln ny chng ti c hng tip cn khc m bn s cm thy d dng hn khi mun to cc m hnh mng ln. on m bn di to ra by nt v lu chng trong mng n() for {set i 0} {$i < 7} {incr i} { set n($i) [$ns nt] } Cc bn tng gp vng lp for cc ngn ng lp trnh khc trc y, v ti chc rng cc bn hiu cu trc ny mt ln na. nhng mng , cng nh cc bin khc trong Tcl, chng khng cn phi c khai bo u tin . By gi chng ta s kt ni cc nt thnh mt topo vng trn. Ban u dng m bn di trng c v phc tp hn mt cht. for {set i 0} {$i < 7} {incr i} { $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail } Vng lp for kt ni tt c cc nt vi nt tip theo trong mng tr nt cui cng, nt ny s kt ni vi nt u tin.. thc hin iu , ti s dng ton t % By gi, khi bn cho chy on m, u tin topo c v l trong NAM nhng sau khi bn bm vo nt re-layout th n s trng ging hnh bn di.

Hnh 5.7: M hnh gm 7 nt

93

5.3.2. Lin kt li
Bc tip theo l gi d liu t nt n0 n nt n3. #Create a UDP agent and attach it to node n(0) set udp0 [new Agent/UDP] $ns attach-agent $n(0) $udp0 # Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 set null0 [new Agent/Null] $ns attach-agent $n(3) $null0 $ns connect $udp0 $null0 $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop" Gi y, on m trn c l quen thuc vi bn. Ch khc vi cc phn trc l by gi chng ta s dng cc thnh phn mng l cc nt. Nu bn khi ng tp lnh, bn s thy rng lu lng s chn ng ngn nht t nt 0 ti nt 3 i qua nt 1 v 2 ng nh mong i. By gi chng ti thm mt tnh nng th v khc. Chng ta hy lm cho lin kt gia nt 1 v 2 b t trong 1 giy. $ns rtmodel-at 1.0 down $n(1) $n(2) $ns rtmodel-at 2.0 up $n(1) $n(2) C l khng qu kh hiu hai dng lnh trn. By gi bn chy li tp lnh v bn s thy rng t giy th nht n giy th 2 knh truyn s b t v tt c d liu c gi i t nt 0 b mt. By gi ti s cho bn thy lm th no s dng nh tuyn ng gii quyt vn trn. Thm vo dng lnh bn di vo phn u ca on m Tcl ca bn sau khi i tng m phng c to ra. $ns rtproto DV

94

Hnh 5.8: Minh ha lin kt b li

Bt u m phng li, bn s thy u tin c rt nhiu gi tin nh truyn qua mng. Nu bn lm chm nam xung click vo mt trong s chng bn s thy rng cc chng l cc gi tin rtProtoDV c s dng thay i thng tin nh tuyn gia cc nt. Khi knh truyn b t tr li lc 1 giy, vic nh tuyn s c cp nht v lu lng s c nh tuyn li i qua nt 6, 5, v 4.

Hnh 5.9: Lu lng truyn theo nt 6, 5, 4

Bn c th ti chng y ny ti y hoc ti y tham kho

95