You are on page 1of 77

1. Gii thiu v cng cuC VIN ng mang NS.............................................................4 HO m pho CNG NGH VIN THNG 1.1 Tng quan.....................................................................................................

4 HOC H QUC T NGH TAO SAU AI HO KHOA QUAN VIN CNGVA AO VIN THNG C KHOA QUAN H QUC T VA AO TAO SAU AI HOC 1.2. B inh trinh s kin...................................................................................6 1.3. Cac thanh phn mang..................................................................................8 1.4. Goi d liu.................................................................................................10 2. Giao tip vi b thng dich: Lin kt OTcl..........................................................11 2.1 Cac khai nim.............................................................................................11 2.2 Tng quan v ma lnh................................................................................12 2.3 Lp Tcl.......................................................................................................12 2.3.1 Ly tham chiu ti instance cua lp Tcl..............................................12 2.3.2 Goi cac ham OTcl................................................................................12 2.3.3 Gi va nhn kt qua t b thng dich.................................................12 2.3.4 Thng bao li va thoat.........................................................................13 2.3.5 Cac ham bm trong b thng dich ......................................................13 2.4 Lp TclObject.............................................................................................13 2.4.1 Tao va huy i tng TclObject ..........................................................13 IP NG DUNG VA BAO MT 2.4.2 Kt hp bin........................................................................................14 IP NG DUNG VA BAO MT 2.4.3 Theo doi gia tri bin............................................................................14 2.4.4 Ham command: inh nghia va cach goi..............................................14 2.5 Lp TclClass...............................................................................................14 2.5.1 Kt hp cac bin thanh vin tinh cua lp C++....................................15 2.6 Lp TclCommand.......................................................................................15 2.7 Lp EmbeddedTcl......................................................................................16 2.8 Lp InstVar.................................................................................................16 3. Lp Simlator .........................................................................................................16 3.1 Khi to b m phng................................................................................16 3.2. B nh trnh v s kin............................................................................17 3.2.1 B nh trnh danh sch.......................................................................18 Thy hng dn: Hoang ng Hai 3.2.2. B nh trnh xp ng.......................................................................18 Hoc Vin: L ng Phong [1-22] 3.2.3 B nh trnh xp hng theo lch.........................................................18 Vu Anh Tun [23-44] 3.2.4 B nh trnh thi gian thc................................................................18 Nguyn Vit Thng [45-66] inh Hai ng [67-ht] 3.3 Cc phng thc khc................................................................................18 3.4. Lt qua v cc lnh.................................................................................19 4. Cc nt v chuyn gi............................................................................................20 4.1 Cc cch thc ca b m phng: To kin trc mng................................20 4.2 Cc cch thc ca nt: t cu hnh nt.....................................................22 4.3 Cu hnh nt v mt chc nng..................................................................23 4.3.1 Giao din cu hnh nt.........................................................................23 Ha Ni, thang 4 nm 2005 4.4 B phn lp................................................................................................25 4.4.1 B phn lp a ch.............................................................................27

TIU LUN MN HOC TIU LUN MN HOC

4.4.2 B phn lp a hng.........................................................................27 4.4.3 B phn lp a ng.........................................................................28 4.4.4 B phn lp bm.................................................................................29 4.4.5 B tao bn sao.....................................................................................30 4.5 Module nh tuyn v s t chc b phn lp...........................................31 4.5.1 Module nh tuyn...............................................................................31 4.5.2 Giao din nt.......................................................................................33 4.6 Xem qua mt vi lnh.................................................................................34 5. Cc lin kt: Cc lin kt n ..............................................................................35 5.1. Th tc cho Links v SimpleLinks............................................................36 5.2. Cc Connector...........................................................................................37 5.3 Phn cp i tng.....................................................................................38 6. Qun l hng i v nh trnh gi.......................................................................39 6.1. Lp Queue trong C++..............................................................................39 6.1.1 Kha hng i.....................................................................................40 6.1.2. Lp PacketQueue...............................................................................41 6.2. V d: Loi b phn cui (Drop Tail)........................................................42 6.3 Cc kiu khc nhau ca i tng Queue..................................................43 7. Tr v cc tuyn lin kt........................................................................................47 7.1 Lp LinkDelay...........................................................................................47 8.Cc dch v trong ns...............................................................................................49 8.1. Tng quan..................................................................................................49 8.2 S thi hnh (Implementation).....................................................................49 8.2.1 Hng i RED cho Diffserv................................................................49 8.3 Cu hnh.....................................................................................................51 9. Agents.....................................................................................................................52 9.1. Trng thi Agent .......................................................................................52 9.2. Cc phng thc ca Agent......................................................................52 9.3. Protocol Agents.........................................................................................53 9.4. Lin kt trong OTcl ..................................................................................54 9.4.1 To v sa i cc Agent....................................................................54 9.4.2. Cc gi tr mc nh............................................................................54 9.4.3. Cc phng thc trong OTcl..............................................................54 9.5. V d: Cc agent Tcp, TCPSink................................................................55 9.5.1. To Agent...........................................................................................55 9.5.2. Khi ng Agent................................................................................56 9.5.3. X l u vo b thu.......................................................................57 9.5.4. X l p ng bn gi.....................................................................58 9.5.5. Cc b nh thi.................................................................................58 9.6. To mt agent mi.....................................................................................59

ii

9.6.1. V d: Yu cu ping (Cu trc k tha)..........................................59 9.6.2. Cc phng thc recv() v timeout().................................................60 9.6.3. Lin kt agent ping vi OTcl..........................................................60 9.6.4. S dng agent nh OTcl.....................................................................62 9.7. Agent API..................................................................................................62 9.8. Cc i tng agent khac..........................................................................62 9.9. Cc lnh c bn.........................................................................................63 10. Cc b nh thi...................................................................................................63 10.1. Lp c s trong C++ TimerHandler........................................................63 10.1.1. nh ngha mt b nh thi mi.....................................................64 10.1.2. V d: B nh thi truyn li TCP..................................................65 10.2. Lp nh thi trong OTcl........................................................................67 10.3. Mt s lnh c bn..................................................................................68 11. Header gi v cc nh dng...............................................................................68 11.1. Header gi cho mt giao thc c th.......................................................68 11.1.1. Thm mt kiu header gi mi........................................................70 11.1.2. La chn cc header gi trong b m phng ..................................71 11.2. Cc lp gi..............................................................................................71 11.2.1. Lp Packet........................................................................................71 11.2.2. Lp p_info........................................................................................74 11.2.3. Lp hdr_cmn....................................................................................74 11.2.4. Lp PacketHeaderManager .............................................................75 11.3. Cc lnh c bn.......................................................................................77

iii

Gii thiu cng cu NS

1. Gii thiu v cng cu m phong mang NS


1.1 Tng quan

NS la b cng cu m phong mang iu khin theo cac s kin ri rac, c xy dng va phat trin bi trng ai hoc Berkeley My, cho phep m phong nhiu kiu mang IP khac nhau, m phong cac giao thc mang: TCP, UDP cung nh cac dang ngun lu lng: FTP, Telnet, Web, CBR, VBR, m phong cac hang i trong cac b inh tuyn: DropTail, RED, CBQ, m phong cac giai thut inh tuyn. Ngoai ra NS con cho phep thc hin vic phat a lung va mt s giao thc lp MAC i vi m phong LAN. NS c xy dng bng ngn ng lp trinh h thng C++ va ngn ng m phong OTcl. Otcl la dang ngn ng kich ban Tcl c m rng theo m hinh hng i tng.
Otcl: B thng dich Tcl vi phn m rng hng i tng Th vin m phong NS Cac i tng inh trinh s kin Cac i tng phn t mang Cac module tr giup thit lp mang
Phn tich Kt qua m phong

Otcl Script

NAM NetWork

Hinh 1: NS theo quan im ngi dung Theo quan im ngi dung thun tuy, NS la mt b thng dich cac kich ban Tcl hng i tng. NS gm co cac b inh trinh cac s kin m phong, cac th vin i tng thanh phn mang, th vin cac mdule tao lp mang (thc t vic kt ni cac module c thc hin bng cac ham thanh vin cua cac i tng m phong c ban). Khi s dung NS, ngi dung phai lp trinh bng ngn ng kich ban Tcl. tao lp va trin khai mt mang m phong, ngi dung vit mt kich ban Tcl khi tao mt b inh trinh s kin, thit lp topo mang thng qua vic s dung cac i tng thanh phn mang va cac hanh lin kt trong cac th vin cua NS. Vic thit lp mt mang la ghep ni cac ng d liu gia cac i tng mang bng cach t con tro cua mt i tng nay ti ia chi cua mt i tng khac tng ng. Khi mun tao mt i tng mang mi, thi ngi dung co th tao ra i tng o bng cach xy dng mt i tng mi hoc t hp cac i tng co sn trong cac th vin i tng cua NS va tao ra cac ng lin kt d liu gia chung. Mt thanh phn quan trong cua NS (bn canh cac i tng mang) la b inh trinh s kin. Mt s kin trong NS la mt ma nhn dang goi, ma nay la duy nht i vi mt goi, s kin c inh thi va gn vi mt con tro tro n i tng se x ly s kin o. Trong NS, b inh trinh s kin giam sat toan b thi gian m phong, quan ly va kich hoat cac s kin theo mt hang i. Khi mt s kin c kich hoat, b inh trinh se goi ti thanh phn i tng mang x ly tng ng (thng cung la i tng tao ra s kin o), cho phep i tng nay thc hin cac tac vu thich hp ln goi gn vi Bai tiu lun mn hoc IP-ng dung va bao mt 4

Gii thiu cng cu NS s kin. Cac thanh phn i tng mang truyn thng vi nhau bng cach trao i cac goi. V nguyn tc, tt ca cac thanh phn mang lun cn phai co mt khoang thi gian nht inh x ly goi (tr). Tr nay c th hin thng qua vic s dung cac b inh trinh s kin: gi i mt s kin gn vi goi va ch s kin o c kich hoat tr lai trc khi x ly cac goi tip theo. Vi du: mt thanh phn mang m phong mt thit bi chuyn mach goi vi 20 ms tr, no se gi i mt s kin tr 20 ms gn vi goi cn phai chuyn mach ti b inh trinh s kin, b inh trinh sau 20 ms se a s kin o ra khoi hang i va kich hoat no tr lai thanh phn mang chuyn mach, thanh phn chuyn mach se thc hin vic chuyn goi tng ng ti i tng lin kt u ra cua no. Mt ng dung na cua b inh trinh s kin la tao ra cac b inh thi. VD: giao thc TCP cn b inh thi giam sat thi gian ch khi phat i mt goi nhm thc hin vic phat lai goi. Khi o, b inh thi s dung b inh trinh tng t nh trong trng hp gia lp tr, s khac bit duy nht la b inh thi o gia tri thi gian gn vi mt goi, thc hin cac tac vu lin quan n goi o sau mt khoang thi gian nht inh tri qua ma khng thc hin vic m phong tr. NS c vit bng hai ngn ng OTcl va C++. am bao tinh hiu qua NS phn chia ng d liu va ng iu khin ring bit. giam thi gian chng trinh x ly cac s kin va goi (thi gian nay khng phai thi gian m phong),thi b inh trinh s kin va cac i tng mang c ban trong ng d liu c vit va bin dich bng ngn ng C++. Cac i tng sau bin dich vn co th c s dung bi b thng dich Otcl thng qua cac lin kt Otcl, cac lin kt nay tao ra i tng OTcl tng ng cho mi i tng C++, cung nh tao ra cac ham va cac bin thanh vin trong i tng Otcl tng ng vi cac bin va ham thanh vin trong i tng C++. Bng cach nay vic iu khin cac i tng C++ co th c thc hin thng quan OTcl, ngoai ra ta cung co th thm cac bin va ham thanh vin khac vao i tng OTcl. Cac i tng c vit bng C++ ma khng cn thit phai iu khin trc tip trong qua trinh m phong cung khng cn phai lin kt sang OTcl. Mt khac mt i tng khng nm trn ng s liu co th c xy dng hoan toan bng OTcl. Hinh 2 minh hoa mt cy phn cp i tng trong C++ va OTcl.

C++

OTcl
Hinh 2: S tng ng gia C++ va OTcl

Bai tiu lun mn hoc IP-ng dung va bao mt

Gii thiu cng cu NS B inh trinh s kin tctcl otcl Tcl8.0 ns-2 Th vin cac thanh phn mang

Hinh 3: M hinh kin truc cua NS Hinh 3 chi ra kin truc chung cua cng cu m phong NS. Trong hinh ve nay, ngi s dung thit k, trin khai cac m phong bng cac cu lnh Tcl, s dung cac i tng m phong t th vin Otcl, cac b inh trinh s kin va phn ln cac i tng mang c xy dng bng C++, va cac i tng nay vn co th c s dung nh la cac i tng Otcl thng qua cac lin kt. Toan b cac thanh phn trong hinh 3, kt hp lai tao nn mt b cng cu m phong mang NS, la mt b thng dich Tcl c m rng hng i tng va mt tp cac th vin i tng m phong mang. Cac kt qua at c sau khi kt thuc qua trinh m phong, nh trong hinh ve 1, la tp mt hoc nhiu file kt qua u ra di dang vn ban text, cha cac d liu m phong chi tit. Cac s liu nay co c dung phn tich m phong hoc dung lam u vao cho cac cng cu trinh din m phong mang dang hoa: Nam, Xgraph... Nam (Network Animator) co dang giao din tng t nh mt cng cu chi CD, cho phep hin thi thng tin dang hoac nh: thng lng, s goi bi loai bo trn mi link ...
1.2. B inh trinh s kin

Nh a cp n phn trn, i tng s dung chinh cua cac b inh trinh s kin la cac thanh phn mang, m phong vic x ly goi hoc co s dung cac b inh thi. Hinh 4 m ta cac i tng mang s dung b inh trinh s kin. Cn lu y rng: mt i tng gi i mt s kin nao o cung chinh la i tng x ly s kin o tai mt thi im inh sn va ng d liu gia cac i tng khac vi ng iu khin (ng s kin). Thc t, vic a cac goi t mt i tng mang nay n mt i tng mang khac c thc hin bng cach s dung ham send(Packet *p){target_-> recv(p)} cua i tng gi va ham recv(Packet *, Handler * h =0) cua i tng nhn.

Bai tiu lun mn hoc IP-ng dung va bao mt

Gii thiu cng cu NS

Hinh 4: B inh trinh s kin ri rac NS co hai kiu inh trinh s kin khac nhau: inh trinh thi gian thc va inh trinh khng theo thi gian thc. inh trinh khng theo thi gian thc bao gm 3 loai: List, Heap va Calendar. B inh trinh Calendar c s dung mc inh trong NS. Cac b inh trinh thi gian thc c s dung thc hin vic m phong tng tac vi mang thc t. Vi du: vic la chon mt b inh trinh s kin cu th c thc hin nh sau:
... set ns [new Simulator] $ns use-scheduler Heap ...

Mt ng dung na cua cac b inh trinh s kin la tao ra cac b inh thi s kin m phong nh: khi nao thi bt u mt ng dung FTP, khi nao thi kt thuc m phong, hoc tao ra cac kich ban m phong trc khi thc hin m phong. Mt i tng inh trinh s kin , ban thn no co cac ham thanh vin inh thi m phong, vi du nh ham at time "string" thc hin phat i s kin AtEvent tai mt thi im m phong inh sn: time. S kin AtEvent i tng lp con cua lp Event, co mt bin cha gia tri chui string a cho. Tuy nhin s kin AtEvent vn c x ly binh thng nh cac s kin lin quan n vic x ly goi khac trong b inh trinh s kin. Khi mt qua trinh m phong bt u, va khi n thi im inh sn i vi s kin AtEvent, s kin AtEvent c chuyn ti b x ly s kin AtEvent, va cu lnh OTcl nm trong chui string cua s kin AtEvent se c thc thi. Vi du:
set ns [new Simulator] $ns use-scheduler Heap $ns at 300 "finish_sim" ... proc finish_sim{}{ ... }

Trong vi du trn, ta thy rng ham at time "string" la ham thanh vin cua i tng Simulator, nhng cn phai lu y rng i tng Simulator chi ong vai tro giao Bai tiu lun mn hoc IP-ng dung va bao mt 7

Gii thiu cng cu NS tip ngi s dung, nn thc ra no se goi n cac ham thanh vin cua cac i tng mang hoc cac i tng inh trinh thc s thc hin cng vic o.
1.3. Cac thanh phn mang

Trong NS, hu ht cac thanh phn mang la cac i tng phc hp. Hinh 5 chi ra mt phn cua cy phn cp theo lp OTcl.

Hinh 5: Mt phn cua cy phn cp theo lp Gc cua cy phn cp nay la lp TclObject. L TclObject la lp c p s cua moi i tng trong th vin OTcl (inh trinh s kin, thanh phn mang, inh thi, va cac i tng khac). Lp NsObject la lp c s cua moi i tng thanh phn mang c ban x ly goi, cac thanh phn nay kt hp vi nhau tao thanh cac i tng phc hp, nh cac i tng nodes hoc links chng han. Cac thanh phn mang c ban lai c chia thanh hai phn lp con, Connector va Classifier, da trn s lng ng d liu u ra co th. Cac i tng mang c s chi co mt ng d liu u ra duy nht se thuc vao lp Connector, cac i tng chuyn mach co th co nhiu ng d liu u ra thuc vao lp Classifier. Nut va inh tuyn Nut la mt i tng phc hp, c hp thanh bi cac i tng u vao nut va cac b phn loai classifier. Co hai loai nut trong NS. Nut n hng chi co mt i tng phn loai ia chi thc hin inh tuyn n ng va mt i tng phn loai cng. Nut a hng, ngoai hai i tng phn loai ia chi va phn loai cng nh trong nut n hng, con co thm mt i tng phn loai goi a hng va mt i tng phn loai thc hin inh tuyn a lung.

Bai tiu lun mn hoc IP-ng dung va bao mt

Gii thiu cng cu NS Hinh 6. Nut (n hng va a hng) Trong NS, cac nut n hng c s dung mc inh. tao ra nut a hng, ngi s dung phai khai bao tng minh trong kich ban OTcl, ngay sau khi tao ra i tng inh trinh s kin, khi o moi nut c tao ra se la nut a hng. Sau khi chi ra kiu nut s dung, ngi s dung cung co th la chon kiu giao thc inh tuyn xac inh thay vi s dung giao thc ngm inh. ng kt ni ng kt ni cung la mt i tng phc hp chinh khac trong NS, i tng Link. Khi ngi s dung tao ra mt ng kt ni bng cach s dung ham thanh vin duplex-link cua i tng Simulator, hai kt ni n trn hai hng se c tao ra nh trong hinh ve 7.

Hinh 7: ng kt ni Cn lu y rng hang i u ra cua mt nut, thc ra la mt phn cua i tng Link n. Cac goi khi ra khoi mt hang i Queue, se c chuyn ti i tng Delay, i tng nay m phong tr cua link, con cac goi bi loai bo khoi hang i c gi ti i tng Null Agent va c huy tai o. Cui cung, i tng TTL tinh toan tham s Time To Live i vi mi goi nhn c va thay i gia tri trng TTL cua goi. Theo doi giam sat Trong NS, cac tac vu mang c theo doi trn mi ng kt ni n. Nu i tng m phong c chi thi theo doi cac tac vu mang ( s dung lnh $ns traceall file hoc $ns trace-nam file), thi sau o cac ng kt ni c tao ra se c thm vao cac i tng theo doi nh trong hinh ve 8. Ngi s dung co th tao ra i tng theo doi thuc mt dang nao o gia nut ngun va nut ich bng lnh create-trace {type file src dst}.

Hinh 8. Cac i tng theo doi c thm vao. Khi mi i tng theo doi nhn c mt goi, no se ghi cac thng s lin quan n goi ra file theo doi ma khng anh hng n thi gian m phong, va chuyn cac goi nay ti i tng mang tip theo. Giam sat hang i V c ban, cac i tng theo doi c thit k ghi lai thi gian n cua goi d liu . Mc du ngi dung co u cac thng tin t file theo doi, nhng co th ho vn Bai tiu lun mn hoc IP-ng dung va bao mt 9

Gii thiu cng cu NS quan tm n nhng gi xay ra bn trong mt hang i nao o. Vi du: mt ngi dung quan tm n cach thc x ly mt hang i RED chng han, anh ta co th mun o s thay i dai trung binh cua hang i va dai hin thi cua hang i RED, iu o co nghia la cn phai giam sat hang i o. Vic giam sat hang i c thc hin bng cach s dung cac i tng giam sat hang i va cac i tng gian ip nh trong hinh 10.

Hinh 9: Giam sat hang i Khi mt goi n, i tng gian ip se thng bao cho i tng giam sat hang i va nhng thng tin nay c s dung giam sat hang i.
1.4. Goi d liu

Mt goi trong NS c hinh thanh bi mt chng cac mao u, va mt khng gian d liu nh hinh 10

Hinh 10: Khun dang goi trong NS Khun dang mao u goi c tao ra ngay khi i tng Simulator c khi tao, bao gm mt tp tt ca cac mao u a ng ky, chng han cac mao u thng c s dung: IP, TCP, RTP va cac mao u theo doi. iu nay co nghia la cho du mt mao u cu th nao o co c s dung hay khng, thi mt chng gm tt ca cac mao u a ng ky vn c tao ra khi mt goi c cp phat bi mt i tng Agent, va cac i tng mang co th can thip ti bt ky mao u nao cua goi. Thng thi mt goi chi co duy nht mt chng mao u (va con tro khng gian d liu c bng null). Mc du mt goi co th mang d liu thc t (t mt ng dung nao o) bng vic cp phat khng gian d liu, nhng lai co rt it ng dung va Agent h tr vic nay, bi vi vic tai d liu thc trong mi trng m phong la iu v nghia. Tuy nhin, nu ban mun thc hin mt ng dung giao tip vi mt ng dung khac qua mang, thi co th ban mun s dung c tinh nay vi mt chut sa i i tng Agent c s. Bai tiu lun mn hoc IP-ng dung va bao mt 10

Gii thiu cng cu NS

2. Giao tip vi b thng dich: Lin kt OTcl


NS la mt cng cu m phong hng i tng, bao gm ban thn NS la mt i tng c vit bng C++ va mt b thng dich OTcl. NS h tr kin truc cy phn cp theo lp trong C++ (cy phn cp bin dich) va kin truc cy phn cp theo lp tng t trong b thng dich OTcl(cy phn cp thng dich). Hai cy phn cp nay co quan h cht che vi nhau. T goc nhin cua ngi dung, tn tai mi quan h 1-1 gia mt lp trong cy phn cp thng dich va mt lp trong cy phn cp bin dich. Gc cua ca hai cy phn cp nay la lp TclObject. Ngi dung se tao ra cac i tng m phong thng qua b thng dich, nhng i tng nay, thuc vao cy phn cp thng dich, c khi tao trong b thng dich va se c anh xa sang mt i tng tng ng trong cy phn cp bin dich. Cy phn cp thng dich c t ng thit lp thng qua cac ham c inh nghia trong lp TclClass. Cac i tng tao bi ngi dung c anh xa thng qua cac ham c inh nghia trong lp TclObject. Ngoai ra vn co cac cy phn cp khac trong C++ va OTcl, o la nhng cy thc s khng co quan h vi nhau.
2.1 Cac khai nim

Trc ht ta tim hiu tai sao NS lai s dung hai ngn ng C++ va OTcl. o la bi vi, trong qua trinh thc hin m phong co hai vic khac nhau cn phai thc hin: Mt mt, vic m phong chi tit c cac giao thc oi hoi phai s dung mt ngn ng lp trinh h thng, nhm x ly hiu qua vi cac d liu dang byte, cac mao u goi hay thc hin cac giai thut tinh toan trn mt s lng ln d liu. i vi nhng cng vic nh vy thi tc thc thi la rt quan trong, con thi gian thay i, nng cp (qua trinh chay m phong, tim li, sa li, bin dich va chay lai) se it quan trong hn. Mt khac, mt phn ln cac cng vic nghin cu mang lai lin quan n nhng thay i nho cac tham s u vao, thay i cu hinh hay vic tim hiu nhanh chong mt s m hinh mang nao o. Trong trng hp nay, thi gian lp lai (thay i tham s, cu hinh va chay lai) lai tr nn quan trong, con thi gian thc thi se it quan trong hn. NS ap ng ca hai yu cu trn bng cach s dung hai ngn ng C++ va OTcl. C++ co thi gian thc thi nhanh, nhng chm trong vic thay i nng cp, phu hp vi vic trin khai chi tit cac giao thc hay x ly goi. OTcl chay chm hn nhng lai d dang thay i nhanh, phu hp vi vic thit lp cu hinh m phong. NS (thng qua tclcl) a ra c ch cho phep s dung cac i tng va bin trong mi trng ca hai ngn ng C++ va OTcl. Muc ich s dung OTcl : - Thc hin thit lp cu hinh m phong va nhng cng vic chi thc hin mt ln. - Thc hin nhng vic co th giai quyt bng cach t hp cac cac i tng C++ va s dung C++ : - Thc hin nhng cng vic yu cu x ly trn mi goi cua lung d liu - Sa i nhng lp C++ co sn Vi du: links la nhng i tng OTcl, c lp rap t cac i tng C++: tr, xp hang, mt goi... i vi nhng cng vic yu cu x ly chi tit, cu th hn (nguyn ly hang i hay mt m hinh tr c thu nao o chng han) thi chung ta se cn phai xy dng mt i tng C++ mi.

Bai tiu lun mn hoc IP-ng dung va bao mt

11

Gii thiu cng cu NS


2.2 Tng quan v ma lnh

Chung ta s dung thut ng "b thng dich" noi n thng dich OTcl. Ma giao tip vi b thng dich c t trong th muc /tclcl va toan b phn con lai ma m phong c t trong th muc NS-2. Co mt s lp c inh nghia trong th muc /tclcl cho giao tip vi b thng dich OTcl, chung ta chi xem xet 6 lp trong s o c s dung trong NS: - Lp Tcl: cha cac ham cho phep ma C++ truy nhp ti b thng dich - Lp TclObject: la lp c s cua tt ca cac i tng m phong - Lp TclClass: inh nghia cy phn cp thng dich va cac ham cho phep ngi dung khi tao cac i tng TclObject - Lp TclCommand: inh nghia cac cu lnh thng dich n gian - Lp EmbeddedTcl: inh nghia cac ham nap cac cu lnh co sn muc cao hn, cho phep thit lp cu hinh m phong d dang hn. - Lp InstVar: inh nghia cac ham cho phep truy nhp ti cac bin thanh vin C++.
2.3 Lp Tcl

Lp Tcl bao boc toan b instance thc s cua b thng dich OTcl va a ra cac ham giao tip vi b thng dich o. Cac ham nay cho phep thc hin - Ly tham chiu ti Tcl instance - Goi cac ham OTcl qua b thng dich - Nhn va tra lai kt qua cho b thng dich - Thng bao cac tinh hung li va thoat - Lu tham chiu va tim cac i tng TclObject - Truy nhp trc tip ti b thng dich 2.3.1 Ly tham chiu ti instance cua lp Tcl Lun tn tai mt instance duy nht cua lp Tcl c khai bao nh la mt bin thanh vin tinh, ngi lp trinh co th ly c tham chiu ti instance nay qua cu lnh Tcl& tcl = Tcl:instance() 2.3.2 Goi cac ham OTcl Co 4 ham khac nhau goi mt cu lnh OTcl qua Tcl instance:
tcl.eval(char* s) tcl.evalc(const char* s) tcl.eval() tcl.evalf(const char * s,..)

Nhng ham nay chi khac nhau tham s u vao, mi ham se chuyn cu lnh di dang chui ti b thng dich, b thng dich se kim tra cu lnh o va tra v cac kt qua TCL_OK nu cu lnh ung va TCL_ERROR nu cu lnh sai. 2.3.3 Gi va nhn kt qua t b thng dich Khi b thng dich goi cac ham C++, no mong mun kt qua c tra v trong bin thanh vin ring tcl_->result. Co hai ham tra kt qua vao bin nay la
tcl.result(const char * s) tcl.result(const char * fmt,...)

Bai tiu lun mn hoc IP-ng dung va bao mt

12

Gii thiu cng cu NS Khi mt ham C++ thc hin mt cu lnh OTcl, b thng dich cung se tra kt qua v trong bin thanh vin ring tcl_->result. Va ly c kt qua nay s dung ham string tcl.result(void), ham nay tra v kt qua di dang chui. 2.3.4 Thng bao li va thoat. Ham tcl.error(const char * s) c s dung thng bao li trong ma bin dich, no thc hin vic ghi chui thng bao li s cung kt qua tra v tclresult ra lung stdout va thoat ra vi ma li bng 1. 2.3.5 Cac ham bm trong b thng dich NS lu tham chiu cua tt ca cac i tng TclObject cua cy bin dich trong mt bang bm. NS s dung tn cua cac i tng nay thm vao, tim kim hay xoa tham chiu cua i tng trong bang bm. Co 3 ham lam iu nay - tcl.enter (TclObject * o) - tcl.lookup(char * s) - tcl.remove(TclObject *o) Cac ham trn c s dung trong ni tai cac lp TclObject va TclClass quan ly cac i tng.
2.4 Lp TclObject

TclObject la lp c s cua phn ln cac lp con lai trong cy bin dich va thng dich. Mi i tng thuc lp TclObject c tao bi ngi s dung thng qua b thng dich, va se co mt i tng anh xa tng ng c tao ra trong cy bin dich. Hai i tng nay co quan h cht che vi nhau. Lp TclClass (c m ta phn sau) cha cac c ch cho phep thc hin vic anh xa nay. 2.4.1 Tao va huy i tng TclObject Khi ngi dung mun khi tao hay xoa bo mt i tng TclObject, hai ham new{} va delete{} se c s dung tao va huy cac i tng nay. Hai ham new{} va delete{} c s dung tao va huy cac i tng cua tt ca cac lp dn xut t lp TclObject. Tao i tng TclObject: bng cach dung ham new{}, ngi dung se tao ra mt i tng TclObject thng dich, b thng dich thc hin vic khi tao i tng o thng qua vic goi ham dng init{} vi cac tham s nhn c t ngi dung. Sau o NS se t ng tao ra mt i tng bin dich tng ng, i tng anh xa nay c tao ra bi ham dng cua lp TclObject c s. Vi vy khi tao mt i tng TclObject, trc tin cn phai goi ham dng cua lp cha trc khi thc hin cac khi tao i tng cua lp con. Ham new{} tra v the bai tro ti i tng c tao ra. Huy cac i tng TclObject: vic nay nhm xoa bo i tng thng dich va i tng bin dich anh xa tng ng. Cung ging nh vic tao i tng, khi huy i tng phai goi ham huy cua lp cha mt cach tng minh cu lnh cui cung trong ham huy cua lp con. Ham huy cua i tng TclObject se thc hin goi ham delete_shadow{} xoa bo i tng bin dich tng ng. Cui cung b thng dich se xoa bo i tng thng dich

Bai tiu lun mn hoc IP-ng dung va bao mt

13

Gii thiu cng cu NS 2.4.2 Kt hp bin Trong phn ln cac trng hp, chung ta khng th can thip trc tip vao cac bin thanh vin bin dich va vic can thip ti cac bin thng dich qua ma thong dich cung rt han ch. Tuy nhin, chung ta co th thit lp nn s kt hp hai chiu: ca hai bin thanh vin bin dich va thng dich cung truy nhp ti mt d liu duy nht, s thay i gia tri cua mt trong hai bin thanh vin se lam thay i gia tri cua bin thanh vin con lai. C ch kt hp nay thit lp bi ham dng bin dich khi i tng c khi tao. NS h tr 5 loai d liu khac nhau: kiu thc, kiu nguyn, kiu logic, kiu thi gian va kiu d liu gia tri bng thng (Kb, Mb..). Cu phap thc hin vic gan d liu cho cac bin nh sau
$object $object $object $object $object set set set set set realvar 1.2e3 intvar 123 bwvar 1.5mb timevar 1500m boolvar true

2.4.3 Theo doi gia tri bin Ngoai c ch kt hp bin, TclObject con h tr theo doi gia tri cua ca bin C++ va Tcl. Mt bin khi c tao ra trong C++ hoc trong Tcl u co th thit lp vic theo doi gia tri cua no. theo doi gia tri mt bin mc Tcl, thi bin o phai xut hin trong Tcl, iu o co nghia la no phai la mt bin Tcl thun tuy hoc la mt bin kt hp C++/Tcl. Ngoai ra, mt i tng co bin thanh vin c theo doi gia tri cung co th yu cu vic theo doi s dung ham trace{} cua i tng TclObject.Ham trace{} co hai tham s u vao, tham s vao u tin la tn cua bin, tham s tuy chon th hai chi ra i tng chiu trach nhim theo doi gia tri cua bin cn theo doi, nu i tng theo doi nay khng c chi ra, thi i tng s hu bin se chiu trach nhim theo doi gia tri cua bin. mt i tng TclObject co th theo doi c gia tri bin, no phai m rng ham C++ trace(), y la mt ham ao a c inh nghia trong lp TclObject. 2.4.4 Ham command: inh nghia va cach goi i vi mi i tng TclObject c tao ra, NS thit lp mt ham mu cmd{}, ong vai tro nh mt giao tip thc hin cac ham cua i tng bin dich anh xa. Ham cmd{} t ng goi ham command() cua i tng bin dich, va cac tham s u vao cua ham cmd{} cho ham command(). Ngi s dung co th goi ham cmd{} theo hai cach: hoc la goi tng minh ham va chi ra tac vu mong mun bin u vao th nht, hoc goi theo cach ngm inh nu tn tai mt ham mu trung tn vi tac vu mong mun. a phn cac kich ban m phong u s dung cach goi th hai,
2.5 Lp TclClass

Lp bin dich TclClass la mt lp ao thun tuy. Cac lp c dn xut t lp c s nay co hai chc nng: xy dng cy phn cp theo lp thng dich anh xa sang cy phn cp theo lp bin dich, va a ra cac ham khi khao cac i tng TclObjects mi. Mi lp dn xut nh vy c gn vi mt lp bin dich xac inh trong cy phn cp bin dich, va co th khi tao cac i tng mi cua lp bin dich tng ng. Bai tiu lun mn hoc IP-ng dung va bao mt 14

Gii thiu cng cu NS Vi du: chung ta xem xet lp RenoTcpClass, c dn xut t lp TclClass, va c gn vi lp bin dich RenoTcpAgent. Lp RenoTcpClass se khi tao cac i tng mi cua lp RenoTcpAgent. Lp bin dich RenoTcpAgent la lp c dn xut t lp TcpAgent, ban thn lp TcpAgent c dn xut t lp Agent, va cui cung lp Agent lai c dn xut t lp TclObject. Lp RenoTcpClass c inh nghia nh sau:
static class RenoTcpClass: public TclClass { public: RenoTcpClass() : TclClass("Agent/TCP/Reno") {} TclObject* create(int argc, const char*const* argv) { return (new RenoTcpAgent()); } } class_reno;

2.5.1 Kt hp cac bin thanh vin tinh cua lp C++ phn trc, chung ta a cp n cach thc nhin thy cac bin thanh vin cua cac i tng trong C++ trong mi trng OTcl, tuy nhin c ch nay lai khng th ap dung cho cac bin thanh vin tinh cua cac lp C++. ng nhin la, chung ta vn co th tao ra mt bin trong OTcl tng ng vi bin thanh vin tinh cua mi i tng C+ +, tuy nhin iu nay lai hoan toan pha hong i y nghia cua bin thanh vin tinh. Chung ta khng th giai quyt vn kt hp bin nay theo cac giai phap tng t nh vic kt hp bin trong TclObject, bi vi c ch kt hp bin trong TclObject da trn lp InstVar ma lp nay trong TclCl lai oi hoi s hin din cua mt i tng TclObject cu th. Tuy nhin, chung ta co th tao ra mt ham thuc lp TclClass tng ng va thc hin vic truy xut ti bin thanh vin tinh cua lp C++ thng qua ham nay. Cac bc thc hin vic nay nh sau co th tham khao trong tai liu hng dn NS, trang 30.
2.6 Lp TclCommand

Lp TclCommand a ra c ch cho phep NS chuyn cac cu lnh n gian ti b thng dich, va cac cu lnh nay co th c thc hin bi b thng dich trong mi trng chung. Co hai ham: ns-random va ns-version thng c s dung trong mi trng chung, ca hai ham nay c khi tao bi ham init_misc(void), va ham init_misc c goi bi ham Tcl_AppInit(void) trong qua trinh NS c nap vao b nh. Lp VersionCommand, inh nghia ham ns-version, ham nay khng co tham s u vao, va tra v phin ban NS hin thi dang chui. Lp RandomCommand, inh nghia ham ns-random, khng tham s u vao, tra v mt gia tri nguyn ngu nhin nm trong khoang [0, 231-1]. Vi du: % ns-version 2.0a12 % ns-random 12334556

Bai tiu lun mn hoc IP-ng dung va bao mt

15

Gii thiu cng cu NS


2.7 Lp EmbeddedTcl

NS cho phep phat trin cac chc nng hoc bng ma bin dich hoc bng ma thng dich, cac ma nay se c kim tra khi khi ng NS. Chng han nh cac th vin ma ~tclcl/tcl-object.tcl, ~ns/tcl/lib/*.tcl. Vic nap va kim tra cac ma kich ban nay se c thc hin thng qua cac i tng trong lp EmbeddedTcl. Cach n gian nht m rng NS la thm cac ma OTcl hoc la vao file tclobject.tcl hoc vao cac kich ban trong th muc ~ns/tcl/lib/. Cn lu y rng, i vi trng hp th hai, NS nap file ~ns/tcl/lib/ns-lib.tcl mt cach t ng, vi vy ngi lp trinh phai thm vao file nay hai dong am bao kich ban cua minh c t ng nap vao luc NS khi ng. Vi du thm vao kich ban tcl/mcast/srm.tcl inh nghia cac thu tuc cho phep chay SRM, trong file ns-lib.tcl xut hin dong lnh
source tcl/mcast/srm.tcl

Co ba im cn lu y i vi ma EmbeddedTcl la: th nht nu trong qua trinh kim tra ma bi li thi ns se khng chay, th hai ngi s dung co th ghi e ln bt ky oan ma nao trong th vin ma, cu th la chung co th c thay i. Cui dung, sau khi thm cac kich ban vao ns-lib.tcl, cung nh mi ln sa i sau o, ngi s dung phai bin dich lai ns nhng thay i o co hiu lc
2.8 Lp InstVar

Lp InstVar inh nghia cac ham va a ra cac c ch kt hp mt bin C+ + trong i tng bin dich vi mt bin OTcl xac inh trong i tng thng dich tng ng. Vic kt hp nay cho phep gia tri cua bin co th c thay i trong ca hai mi trng: thng dich va bin dich moi thi im. Co nm lp bin: InstVarReal, InstVarTime, InstVarBandwidth, InstVarInt va InstVarBool, c s dung kt hp 5 kiu bin: thc, nguyn, thi gian, logic va bng thng tng ng.

3. Lp Simlator
Tt c b m phng c m t bi lp Tcl ca b m phng. N cung cp mt tp cc giao din cho vic t cu hnh m phng v la chn kiu ca b nh trnh s kin s dng iu khin vic m phng. Mt kch bn m phng ni chung bt u bi vic to ra mt lp c th v gi cc phng thc khc nhau to cc nt, cc cu trc lin kt mng v cu hnh cc kha cnh khc ca s m phng.
3.1 Khi to b m phng

Khi mt i tng m phng c to ra trong tcl, th tc khi to c thc hin theo cc thao tc: - Khi to nh dng gi (gi hm create_packetformat) - To b nh trnh (mc nh l b nh trnh lch) - To mt "tc t rng"(null agent - ni hy gi trong cc v tr khc nhau) S khi to nh dng gi thit lp cc trng d vi cc gi c s dng trong sut qu trnh m phng. B nh trnh thc hin m phng theo cch iu khin s kin v c th c thay th bi b nh trnh lun phin m cung cp mt vi ng ngha khc nhau (xem cc phn di y c thm chi tit)

tc t rng c to ra theo li gi:

set nullAgent_ [new Agent/Null]

Bai tiu lun mn hoc IP-ng dung va bao mt

16

Gii thiu cng cu NS

Tc t ny ni chung l hu ch nh l mt ni hy gi hoc nh l mt ci ch cho cc gi khng c m hoc c ghi li.


3.2. B nh trnh v s kin

Mt b m phng l mt b m phng iu khin s kin, c 4 b nh trnh hin ti c trong b m phng, chng c thc hin s dng cu trc d liu khc nhau: danh sch kt ni n, cht ng, hng ch lch (mc nh) v mt kiu c bit c gi l "thi gian thc". Mi chng c m t di dy. B nh trnh hot ng bi vic la chn s kin sm nht tip theo, thc hin chng cho ti khi hon thnh sau quay tr li thc hin cc s kin tip theo. n v thi gian c tnh bi b nh trnh l giy. Hin ti, b m phng l tuyn on n v ch mt s kin c thc hin trong sut thi gian. Nu c hn mt s kin c nh trnh thc hin cng mt thi gian, s hot ng ca chng l c thc hin theo cch nh trnh u tin c gii quyt u tin, Nhng s kin ng thi khng c sp xp li bi b nh trnh (nh trong nhng phin bn trc) v tt c b nh trnh nn cung cp cng mt th t gii quyt c a ra cho cung loi u vo. Vic thc hin tng phn cc s kin hoc cc cng vic c u tin s khng c h tr. Mt s kin ni chung gm c "thi gian t chy" (firing time) v mt hm trnh x l. nh ngha tht s ca mt s kin nh sau:
class Event { public: Event* next_; /* event list */ Handler* handler_; /* handler to call when event ready */ double time_; /* time at which event is ready */ int uid_; /* unique ID */ Event() : time_(0), uid_(0) {} }; /* * The base class for all event handlers. When an events scheduled * time arrives, it is passed to handle which must consume it. * i.e., if it needs to be freed it, it must be freed by the handler. */ class Handler { public: virtual void handle(Event* event); };

Hai kiu ca i tng c dn xut t lp s kin gc: gi v "ang lc s kin" (at-event). Mt at-event l vic thc hin th tc tcl c nh trnh din ra ti mt thi im nht nh. iu ny thng xuyn c s dng trong kch bn m phng. Mt v d n gin v vic s dng chng nh sau:
... set ns_ [new Simulator] $ns_ use-scheduler Heap $ns_ at 300.5 "$self complete_sim" ...

on m tcl trn u tin to ra i tng m phng, sau thay i b nh trnh mc nh thnh b nh trnh da trn vic to ng (xem bn di) v cui cng nh trnh hm $self complete_sim s thc hin thi gian 300.5s. Cc at-event c thi hnh nh cc s kin m trnh x l thc hin mt cch hiu qu ca b thng dch tcl. Bai tiu lun mn hoc IP-ng dung va bao mt 17

Gii thiu cng cu NS 3.2.1 B nh trnh danh sch B nh trnh danh sch thc thi b nh trnh s dng cu trc danh sch lin kt n. Danh sch gi theo trt t thi gian (sm nht n mun nht) nhng s kin chn thm vo v xa i yu cu qut danh sch tm ra mc thch hp. La chn s kin tip theo thc hin yu cu ct mc u tin ra khi u danh sch. S thc thi ny gi cho vic thc hin s kin theo cch FIFO cho cc s kin ng thi. 3.2.2. B nh trnh xp ng B nh trnh xp ng thc thi b nh trnh s dng cu trc xp ng. Cu trc ny l cao cp hn cu trc danh sch vi s lng ln cc s kin, nh l thi gian xa v chn thm l O (log n) cho n s kin. S thc thi ny trong ns v2 c ly t b m phng MaRS-2.0 bn thn MaRS li c mn m t b NetSim cho d dng ny 3.2.3 B nh trnh xp hng theo lch B nh trnh xp hng theo lch s dng cu trc d liu tng t nh lch bn 1 nm, cc s kin cng ngy thng ca cc nm c th c ghi trong mt ngy. S thc thi ca xp hng theo lch trong ns v2 c ng gp bi David Wetherall 3.2.4 B nh trnh thi gian thc B nh trnh thi gian thc c gng ng b s thi hnh ca cc s kin vi thi gian thc. N thc thi nh l mt lp con ca b nh trnh danh sch. Kh nng v thi gian thc trong ns l ang c pht trin, nhng c s dng a ra mt ns m phng mng trong kin trc th gii thc c thc nghim vi kin trc mng n gin, lu lng cho, ... N ch hot ng vi tc d liu lu lng mng chm, nh mt b m phng cn phi c th theo c vi tc n ca gi trong th gii thc, v s ng b ny hin ti l khng c hiu lc.
3.3 Cc phng thc khc

Cc lp ca b m phng cung cp mt lng cc phng php s dng thit lp s m phng. Ni chung chng c chia thnh 3 loi: Cc phng php to, qun l kin trc v qun l cc lin kt, cc phng php thc hin vic theo vt, v trnh gip thc hin chc nng x l vi b nh trnh. Theo y l danh sch cc phng php khng c lin quan n kin trc mng ca b m phng:
Simulator instproc now ;# return schedulers notion of current time Simulator instproc at args ;# schedule execution of code at specified time Simulator Simulator Simulator Simulator buffers Simulator object Simulator packet format instproc cancel args ;# cancel event instproc run args ;# start scheduler instproc halt ;# stop (pause) the scheduler instproc flush-trace ;# flush all trace

object

write

instproc create-trace type files src dst ;# create trace instproc create_packetformat ;# set up the simulators

Bai tiu lun mn hoc IP-ng dung va bao mt

18

Gii thiu cng cu NS


3.4. Lt qua v cc lnh
Synopsis: ns <otclfile> <arg> <arg>..

M t: Lnh c bn chay mt kch bn m phng trong ns. B m phng ns gi thng qua b thng dch ns, mt s m rng ca lnh vanilla otclsh. S m phng c nh ngha bi mt kch bn Otcl (tp tin). Vi v d v kch bn Otcl c th thy trong th mc ns/tcl/ex Di y l danh sch chung nht cc lnh ca b m phng c s dng trong m phng
scripts: set ns_ [new Simulator] lnh ny to ra mt i tng c th ca b m phng. set now [$ns_ now] B nh trnh gi vt thi gian m phng. n tr v thng bo ca b nh trnh v thi gian hin ti. $ns_ halt dng li v tm dng b nh trnh. This stops or pauses the scheduler. $ns_ run Chy b nh trnh This starts the scheduler. $ns_ at <time> <event> nh trnh mt s kin <event> (thng thng l mt on m) c thc hin mt thi gian no . e.g $ns_ at $opt(stop) "puts NS EXITING.. ; $ns_ halt" or, $ns_ at 10.0 "$ftp start" $ns_ cancel <event> B qua s kin, s kin c loi ra khi danh sch b nh trnh ca nhng s kin sn sng hot ng. $ns_ create-trace <type> <file> <src> <dst> <optional arg: op> to mt i tng gim st kiu <type> gia i tng <src> v <dst> v gn i tng gim st ny vo mt tp tin <file> ghi li kt qu gim st. Nu op c nh ngha l nam, n to ra tp tin gim st nam; nu op khng c nh ngha cc tp tin gim st ns c to ra mt cch t ng. $ns_ flush-trace Flushes all trace object write buffers. $ns_ gen-map kt xut thng tin nh cc nt, cc thnh phn nt, lin kt, ... c to ra t s m phng cho. N c th ph v vi ng cnh (nh khng dy) $ns_ at-now <args> c hiu qu nh lnh "$ns_ at $now $args". Ch rng chc nng ny c th khng hot ng v phn gii s k t ca tcl. $ns_ use-scheduler <type> S dng ch ra kiu ca b nh trnh c s dng khi m phng. Kiu ca b nh trnh c th l List, Calendar, Heap v RealTime, hin ti b nh trnh Calendar c s dng l mc nh. $ns_ after <delay> <event> nh trnh mt s kin s c thc hin sau mt khong thi gian tr. $ns_ clearMemTrace S dng cho mc ch g ri b nh. $ns_ is-started Tr v gi tr ng nu b m phng chy v tr v si nu khng chy. $ns_ dumpq lnh cho vic kt xut cc s kin c xp hng trong b nh trnh trong khi b nh trnh treo.

Bai tiu lun mn hoc IP-ng dung va bao mt

19

Gii thiu cng cu NS


$ns_ create_packetformat Thit lp nh dng gi tin ca b m phng.

4. Cc nt v chuyn gi
Chng ny m t mt kha cnh ca vic to kin trc mng trong ns nh l to cc nt. Chng tip theo s m t kha cnh th hai ca vic to kin trc mng nh l kt ni cc nt thnh cc lin kt. Chng ny ko m t chi tit t chc bn trong ca mt nt (cho d mt vi lc c a ra), hoc l tng tc gia mt nt v module nh tuyn ca n. Nhc li rng mi m phng yu cu lp ca b m phng c th iu khin v thao tc m phng ny. Lp ny cung cp cc th tc c th to v qun l kin trc mng v lu tr bn trong cc tham chiu ti mi phn t ca kin trc mng. Chng ta bt u m t cc th tc trong lp B m phng (phn 4.1) sau m t cc th tc c th trong lp Node (phn 4.2) truy cp v thao tc trn nt ring bit. Chng ta kt thc vi cc m t chi tit ca B phn lp (phn 4.4) t cc i tng nt phc hp hn c hnh thnh.
4.1 Cc cch thc ca b m phng: To kin trc mng

Nguyn t c bn c c mt nt l:
set ns [new Simulator] $ns node

Th tc c th node dng nn mt nt t cc i tng phn lp n gin hn. Bn thn Node l mt lp ng c lp trong OTcl. Hn na, hu ht cc thnh phn ca nt l trong i tng Tcl. Cu trc in hnh ca mt nt c ch ra trong hnh 5.1 Trc ns phin bn 2.1b6 a ch ca mt tc t trong mt nt l rng 16bit: 8bit cao c nh ngha l node id_, 8 bit thp nhn dng tc t ring bit trong nt. Gii hn s nt trong mt ln m phng l 256 nt. Nu ngi s dng cn to mt kin trc mng ln hn 256 nt th u tin cn phi m rng khng gian a ch trc khi to bt c nt no.

nh l:
$ns set-address-format expanded

Bai tiu lun mn hoc IP-ng dung va bao mt

20

Gii thiu cng cu NS

Hnh 4.1: Cu trc ca mt nt n hng, ch rng entry_ l mt bin s nhn n thay v mt i tng tht v d nh l classifier_ M rng khng gian a ch ti 30 bit, 22 bt cao c s dng gn cho s nt. Ch : T bn 2.1b6, trn th khng cn thit di hn, ns s dng s nguyn 32 bit cho c a ch v cng, cn trc , gii hn trn 256 nt l khng th dng c Theo mc nh, cc nt trong ns c dng cho m phng n hng. m phng a hng, s m phng ny c th c to ra vi la chn "-multicast on", v d:
set ns [new Simulator -multicast on]

Cu trc bn trong ca nt a hng c ch ra trong hnh 5.2 Khi mt m phng s dng nh tuyn a hng, bit cao nht ca a ch ch th rng phn a ch l a ch a hng hay a ch n hng. Nu bit l 0, a ch i din l i ch n hng, ngc li l a ch a hng. N ng rng, theo mc nh, m phng a hng c gii hn trong 128 nt.

Bai tiu lun mn hoc IP-ng dung va bao mt

21

Gii thiu cng cu NS

Hnh 4.2 Cu trc bn trong ca nt a hng


4.2 Cc cch thc ca nt: t cu hnh nt

Cc th tc cu hnh mi nt ring bit c th c chia thnh: - Cc chc nng iu khin - Qun l s a ch v s cng, cc chc nng nh tuyn n hng - Qun l tc t - Thm ln cn Chng ta m t cc chc nng nh sau:

Cc chc nng iu khin:


$node entry tr v im u vo cho nt, y l phn t u tin s x l cc gi n trong nt ny. Bin s c th ca Node, entry_, lu tham chiu phn t ny. Vi nt n hng, y l b phn lp a ch nhm vo bit cao ca a ch ch. Bin s classifier_ cha tham chiu ti b phn lp ny. Tuy nhin, vi cc nt a hng, im vo l bin switch_ nhm vo bit u tin quyt hoc l chuyn gi ti b phn lp n hng hoc l b phn lp a hng cho ph hp. $node_reset s xc lp li ton b tc t ca nt.

Qun l a ch v s cng.
Th tc $node id tr v s id ca nt, s ny l t ng tng v gn ti mi nt ti lc to nt bi phng thc ca b m phng lp, $ns node. B m phng lp lun lu tr mt mng cc bin c th, Node_, c nh ch mc bi id ca nt, cha tham chiu ti nt bng id ny. Th tc $node agent (port) tr v vic x l ca tc t ti cng ch nh, nu khng c tc t no s cng ch nh l hp l, th tc tr v chui rng. Th tc alloc-port tr v s cng hp l tip theo, n s dng bin np_, theo di s cng cha c cp pht k tip. Th tc add-route and add-routes, c s dng b nh tuyn n hng v cng thm cc tuyn vo classifier_, c php s dng l $node add-route (destination id) Bai tiu lun mn hoc IP-ng dung va bao mt 22

Gii thiu cng cu NS (TclObject). TclObject l u vo ca dmux_, b gii ghp cng ca nt, nu id ch ging vi id ca nt ny, n thng l on u ca lin kt gi gi cho im ch, nhng cng c th l u vo cho cc b phn lp khc hoc kiu ca b phn lp. $node add-routes (destination id) (TclObjects) c s dng thm vo nhiu tuyn ti cng mt a ch ch m phi s dng ng thi vic tri rng bng thng theo cch quay trn t ti im ch t bn ny ti bn kia ca tt c cc lin kt mt cch tng ng. N c s dng ch nu bin s multiPath_ c gn bng 1, v chin lc nh tuyn ng chi tit l hiu qu, cng nh yu cu s s dng ca b phn lp a ng. i li vi add-routes{} l delete-routes{} N ly id, trong danh sch ca TclObjects v c tham chiu ti nullagent ca b m phng. N xa TclObjects trong danh sch t cc tuyn ci t trong b phn lp a ng. Nu u vo ca tuyn trong b phn lp khng tr n b phn lp a ng, thng l ch l xa u vo t classifier_ v ci t nullagent vo ch . nh tuyn ng chi tit cng s dng 2 phng thc cng thm vo: th tc c th init-routing{} thit lp bin s multiPath_ s tng ng vi bin s lp cng tn, n cng cng mt tham chiu ti i tng b iu khin tuyn ca nt trong bin s ny, rtObject_. Th tc rtObject{} tr v x l cho i tng tuyn ca nt, Cui cng, th tc intf-changed{} l c gi ln bi m ng trn mng nu mt lin kt ri vo nt thay i trng thi. Qun l tc t Mt tc t cho, th tc attach{} s cng thm tc t vo danh sch agents_, gn s cng ca tc t v t a ch ngun, t ch ca tc t cho n v cng vo mt con tr ti b gii ghp cng ca nt (dmux_) cho tc t ti khe tng ng trong b phn lp dmux_. Ngc li, detach{} s xa tc t t agent_, v ch n ch ca tc t v u vo nt dmux_ bng nullagent. Theo di nt ln cn (tracking Neighbors) Mi mt nt gi mt danh sch cc nt ln cn n trong bin neighbor_. Th tc add-neighbor{} cng thm mt nt ln cn vo danh sch. Th tc neighbors{} tr v danh sch ny.
4.3 Cu hnh nt v mt chc nng

4.3.1 Giao din cu hnh nt Giao din cu hnh nt gm c 2 phn. Phn th nht gii quyt cu hnh nt, phn th 2 thc s to ra nt theo kiu c th. Cu hnh nt gm c nh ngha cc c tnh khc nhau ca nt trc khi to ra chng, c th gm c kiu cu trc nh a ch trong vic m phng, nh ngha cc thnh phn mng cho cc nt di ng, bt hoc tt cc la chn theo di mc tc t, b nh tuyn hoc MAC, la chn kiu cc th tc nh tuyn thay i cho cc nt khng dy hoc nh ngha m hnh nng lng ca chng. Ton b API cu hnh nt nh sau:
OPTION_TYPE AVAILABLE OPTION_VALUES ------------- -------------------------$ns_ node-config -addressingType flat or hierarchical or expanded -adhocRouting DSDV or DSR or TORA or AODV -llType LL -macType Mac/802_11 -propType Propagation/TwoRayGround -ifqType Queue/DropTail/PriQueue

Bai tiu lun mn hoc IP-ng dung va bao mt

23

Gii thiu cng cu NS


-ifqLen 50 -phyType Phy/WirelessPhy -antType Antenna/OmniAntenna -channelType Channel/WirelessChannel -topoInstance $topo_instance -wiredRouting ON or OFF -mobileIP ON or OFF -energyModel EnergyModel -initialEnergy (in Joules) -rxPower (in W) -txPower (in W) -agentTrace ON or OFF -routerTrace ON or OFF -macTrace ON or OFF -movementTrace ON or OFF -reset

Gi tr mc nh cho tt c la chn trn l rng tr kiu nh a ch, -addressingType c gi tr mc nh l kiu phng, la chn -reset c th c la chn thit lp li ton b tham s cu hnh nt mang gi tr mc nh ca chng. Cu hnh nt cho nt khng dy, di ng chy AODV nh l giao thc nh tuyn adhoc trong cu trc mng phn cp c ch ra di y, chng ta quyt nh bt theo di ch mc tc t v mc b nh tuyn. Hn na chng ta gi s rng s kin trc mng c to ra vi lnh "set topo [new Topography]". Lnh cu hnh nt nh sau:
$ns_ node-config -addressingType hierarchical -adhocRouting AODV -llType LL -macType Mac/802_11 -ifqType Queue/DropTail/PriQueue -ifqLen 50 -antType Antenna/OmniAntenna -propType Propagation/TwoRayGround -phyType Phy/WirelessPhy -topoInstance $topo -channelType Channel/WirelessChannel -agentTrace ON -routerTrace ON -macTrace OFF -movementTrace OFF

Ch rng lnh cu hnh c th ngt thnh nhng dng ring bit nh:
$ns_ node-config -addressingType hier $ns_ node-config -macTrace ON

Cc la chn cn phi thay i ch thay i khi c gi n, v d sau khi cu hnh cho cc nt di ng AODV nh trn, chng ta c th cu hnh cho cc nt trm gc AODV theo cch sau:
$ns_ node-config -wiredRouting ON

Trong khi tt c cc c tnh khc ca cc nt trm gc v cc nt di ng l ging nhau, cc nt trm gc c kh nng nh tuyn hu tuyn, trong khi cc nt di ng th khng c kha nng ny. Trong trng hp ny chng ta cn phi thay i cu hnh nt ch khi n l bt buc. Tt c cc nt c th c to ra sau lnh cu hnh nt cho c cng thuc tnh tr khi mt phn hoc tt c lnh cu hnh nt c thc hin vi cc gi tr tham s khc nhau. Nhng sau khi to ra cc nt di ng v nt trm gc AODV, nu chng ta mun to ra cc nt n, chng ta s s dng lnh cu hnh nt sau:
$ns_ node-config -reset

Bai tiu lun mn hoc IP-ng dung va bao mt

24

Gii thiu cng cu NS iu ny s thit lp tt c gi tr cc tham s ti gi tr mc nh c bn c nh ngha khi cu hnh mt nt n.


4.4 B phn lp

Chc nng ca mt nt khi n nhn mt gi l xem xt cc trng ca gi , thng thng l a ch ch v mt vi trng hp l a ch ngun. Sau n nh x cc gi tr ti i tng giao din ra ca pha ngi nhn tip theo ca gi tin ny. Trong ns, nhim v ny c thc hin bi i tng classifier n, Cc i tng trong b a phn lp, xem xt vic phn chia c th ca vic chuyn gi tin qua nt. Mt nt trong ns s dng nhiu kiu khc nhau ca b phn lp cho nhiu mc nh khc nhau. Trong phn ny xem xt mt vi i tng phn lp chung hn, n gin hn trong ns. Chng ta bt u m t lp c s trong phn ny. on tip theo s m t b phn lp a ch (4.4.1), b phn lp a hng (4.4.2), b phn lp a ng (4.4.3) v cui cng b ti to (bn sao)(4.4.5) B phn lp cung cp cch i x vi mt gi tin da vo vi iu kin logic v ly tham chiu ti i tng m phng khc da trn kt qu tnh ton. Mi b phn lp gm c mt bng cc i tng m phng c nh ch mc bng s khe (slot number). Cng vic ca mt b phn lp l xc nh s khe kt hp vi gi tin nhn c v chuyn gi tin ny ti i tng c tham chiu bi khe lin quan. Lp Classifier cung cp mt lp c s t cc b phn lp khc c dn xut t lp ny.
class Classifier : public NsObject { public: ~Classifier(); void recv(Packet*, Handler* h = 0); protected: Classifier(); void install(int slot, NsObject*); void clear(int slot); virtual int command(int argc, const char*const* argv); virtual int classify(Packet *const) = 0; void alloc(int); NsObject** slot_; /* table that maps slot number to a NsObject */ int nslot_; int maxslot_; };

Phng thc classify() l hon ton o, ch nh lp Classifier c s dng nh mt lp gc, phng thc alloc() cp pht ng khng gian cho bng cha s c th ca cc khe. Phng thc install() v clear() thm vo hoc xa b cc i tng t bng ny. Phng thc recv() v giao din OTcl l c thc thi nh sau:
/* * objects only ever see "packet" events, which come either * from an incoming link or a local agent (i.e., packet source). */ void Classifier::recv(Packet* p, Handler*) { NsObject* node; int cl = classify(p); if (cl < 0 || cl >= nslot_ || (node = slot_[cl]) == 0) { Tcl::instance().evalf("%s no-slot %d", name(), cl); Packet::free(p);

Bai tiu lun mn hoc IP-ng dung va bao mt

25

Gii thiu cng cu NS


return; } node->recv(p); } int Classifier::command(int argc, const char*const* argv) { Tcl& tcl = Tcl::instance(); if (argc == 3) { /* * $classifier clear $slot */ if (strcmp(argv[1], "clear") == 0) { int slot = atoi(argv[2]); clear(slot); return (TCL_OK); } /* * $classifier installNext $node */ if (strcmp(argv[1], "installNext") == 0) { int slot = maxslot_ + 1; NsObject* node = (NsObject*)TclObject::lookup(argv[2]); install(slot, node); tcl.resultf("%u", slot); return TCL_OK; } if (strcmp(argv[1], "slot") == 0) { int slot = atoi(argv[2]); if ((slot >= 0) || (slot < nslot_)) { tcl.resultf("%s", slot_[slot]->name()); return TCL_OK; } tcl.resultf("Classifier: no object at slot %d", slot); return (TCL_ERROR); } } else if (argc == 4) { /* * $classifier install $slot $node */ if (strcmp(argv[1], "install") == 0) { int slot = atoi(argv[2]); NsObject* node = (NsObject*)TclObject::lookup(argv[3]); install(slot, node); return (TCL_OK); } } return (NsObject::command(argc, argv)); }

Khi b phn lp recv() nhn mt gi tin n gi phng thc classify(), khun mu thng thng l phng thc ny xc nh v tr v ch mc ca khe trong bng v s cc khe. Nu ch mc l hp l, v ch ti TclObject hp l, b phn lp s x l gi tin ny ti cc i tng s dng phng thc recv() ca i tng. Nu ch mc ny l khng hp l, b phn lp s gi n mt th tc no-slot{} c gng a n mt bng ph hp. Tuy vy trong lp gc Classifier::no-slot{} in ra thng bo li v ngt s thc hin. Phng thc command() cung cp theo cc th tc tng t ti b thng dch: clear{(slot)} xa im vo trong mt khe ring bit Bai tiu lun mn hoc IP-ng dung va bao mt 26

Gii thiu cng cu NS installNext {(object)} ci t i tng trong khe kh dng tip theo v tr v s khe. slot {(index)} tr v i tng c lu tr trong mt khe no install {(index), (object)} ci t mt i tng no trong mt khe Ch rng cc th tc tng t cng l c np chng bi mt th tc c th c cng tn m n lu tr mt tham chiu ti i tng c lu tr. 4.4.1 B phn lp a ch Mt b phn lp a ch c s dng h tr chuyn gi tin n hng. N p dng mt bit trng thi shift v mask thao tc a ch ch ca gi tin a ra s khe, s khe ny c tr v t phng thc classify(). Lp AddressClassifier c nh ngha nh sau:
class AddressClassifier : public Classifier { public: AddressClassifier() : mask_(~0), shift_(0) { bind("mask_", (int*)&mask_); bind("shift_", &shift_); } protected: int classify(Packet *const p) { IPHeader *h = IPHeader::access(p->bits()); return ((h->dst() >> shift_) & mask_); } nsaddr_t mask_; int shift_; };

Lp ny li dng vic khng c ngha v ng ngha trc tip trong trng a ch ch ca gi tin. ng hn l, n tr v vi s ca bit t trng dst_ ca gi tin nh l s khe c dng trong phng thc Classifier::recv(). Gi tr ca mask_ v shift_ c thit lp qua OTcl. 4.4.2 B phn lp a hng B phn lp a hng phn loi cc gi tin theo c hai a ch ngun v ch (nhm a ch). N duy tr bng nh x cp a ch ngun/ nhm v cc s khe. Khi m mt gi tin cha a ch ngun/nhm khng r n b phn lp, n gi n mt th tc Otcl Node::new-group{} thm mt im vo cho bng ny. Th tc OTcl ny c th s dng phng thc set-hash thm 3 phn (ngun, nhm, khe) vo bng ca b phn lp. B phn lp a hng c nh ngha nh sau:
static class MCastClassifierClass : public TclClass { public: MCastClassifierClass() : TclClass("Classifier/Multicast") {} TclObject* create(int argc, const char*const* argv) { return (new MCastClassifier()); } } class_mcast_classifier; class MCastClassifier : public Classifier { public: MCastClassifier(); ~MCastClassifier(); protected: int command(int argc, const char*const* argv); int classify(Packet *const p); int findslot();

Bai tiu lun mn hoc IP-ng dung va bao mt

27

Gii thiu cng cu NS


void set_hash(nsaddr_t src, nsaddr_t dst, int slot); int hash(nsaddr_t src, nsaddr_t dst) const { u_int32_t s = src ^ dst; s ^= s >> 16; s ^= s >> 8; return (s & 0xff); } struct hashnode { int slot; nsaddr_t src; nsaddr_t dst; hashnode* next; }; hashnode* ht_[256]; const hashnode* lookup(nsaddr_t src, nsaddr_t dst) const; }; int MCastClassifier::classify(Packet *const pkt) { IPHeader *h = IPHeader::access(pkt->bits()); nsaddr_t src = h->src() >> 8; /*XXX*/ nsaddr_t dst = h->dst(); const hashnode* p = lookup(src, dst); if (p == 0) { /* * Didnt find an entry. * Call tcl exactly once to install one. * If tcl doesnt come through then fail. */ Tcl::instance().evalf("%s new-group %u %u", name(), src, dst); p = lookup(src, dst); if (p == 0) return (-1); } return (p->slot); }

Lp MCastClassifier thc thi mt bng bm theo chui v s dng hm bm trong c ngun gi tin v a ch ch. Hm bm tr v s khe nh ch mc trong bng slot_ ti i tng u tin. Mt im bm thiu ng gi tin c giao ti nhm khng bit r trc , OTcl s c gi x l tnh hung ny. M OTcl mong mun chn mt im vo thch ng trong bng bm
4.4.3 B phn lp a ng i tng ny c ngh ra h tr chuyn a ng tng gi, khi m nt c nhiu nh tuyn tng gi ti cng mt a ch, v mun s dng tt c chng mt cch ng thi. i tng ny khng xem xt mi trng trong gi tin. Vi mi gi tin ni tip nhau, n d dng tr v khe c in k tip theo kiu quay trn. nh ngha cho lp ny c ch ra nh sau:
class MultiPathForwarder : public Classifier { public: MultiPathForwarder() : ns_(0), Classifier() {} virtual int classify(Packet* const) { int cl; int fail = ns_;

Bai tiu lun mn hoc IP-ng dung va bao mt

28

Gii thiu cng cu NS


do { cl = ns_++; ns_ %= (maxslot_ + 1); } while (slot_[cl] == 0 && ns_ != fail); return cl; } private: int ns_; /* next slot to be used. Probably a misnomer? */ };

4.4.4 B phn lp bm i tng ny c s dng phn lp gi tin nh l mt thnh vin ca mt lung ring bit, b phn lp bm s dng bng bm bn trong gn cc gi tin vo cc lung. Cc i tng ny c s dng khi thng tin mc lung c cn n (v d, trong cc nguyn tc hng i c trng ca lung v s thu thp thng tin thng k), cc gi c gn vo lung da trn ID ca lung, a ch ch, a ch ngun/ch hoc t hp ca a ch ngun/ch cng vi ID ca lung. Nhng trng c truy nhp bi b phn lp bm c gii hn ip header: src(),dst(), flowid()
(trong ip.h).

B phn lp bm c to ra vi i s nguyn ch nh kch thc khi to ca bng bm. Kch c bng bm hin ti c th c sa cha sau vi phng thc resize (xem bn di). Khi to ra, bin shift_ v mask_ c khi to vi cc gi tr ca NodeShift v NodeMask hin ti ca b m phng. Cc gi tr ny c ly t i tng AddrParams khi b phn lp bm c gi n, b phn lp bm s khng hot ng nu cu trc AddrParams khng c khi to, cc b dng di y c s dng cho nhiu b phn lp bm
Classifier/Hash/SrcDest Classifier/Hash/Dest Classifier/Hash/Fid Classifier/Hash/SrcDestFid

B phn lp bm nhn cc gi tin, phn loi chng theo iu kin v lung ca chng, v gi ra b phn lp slot ch th nt tip theo s nhn gi tin. Trong vi trng hp vi b phn lp bm, hu ht gi tin c kt hp vi mt khe n, trong khi ch vi lung c hng ti vi ni khc. b phn lp bm gm c bin default_ ch th khe no s c s dng cho cc gi tin khng ph hp vi mi iu kin trn mt lung. gi tr default_ c th c thit lp mt cch ty chn. Phng thc cho b phn lp bm nh sau:
$hashcl $hashcl $hashcl $hashcl set-hash buck src dst fid slot lookup buck src dst fid del-hash src dst fid resize nbuck

Phng thc set-hash()chn thm mt mc t mi vo bng bm vi b phn lp bm. i s buck ch r s thng bng bm c s dng cho vic chen thm mc t ny. Khi s thng l khng r rng, buck c th c ch ra l auto, i s src, dst v fid ch r ngun IP, ch, v ID ca lung tr thnh ph hp cho s phn loi lung. Cc trng khng c s dng bi b phn lp ring bit l c b qua, i s slot ch th ch mc trong bng khe u tin trong i tng Classifier gc t b phn lp bm c dn xut ra. Chc nng lookup tr v tn ca i tng c kt hp vi buck/src/dst/fid, i s buck c th auto, nh l cho set-hash. hm del-hash xa mc t theo danh ngha t bng bm. Hin ti vic ny c thc hin ch l nh du mc t Bai tiu lun mn hoc IP-ng dung va bao mt 29

Gii thiu cng cu NS nh l khng hot ng, tuy nhin n vn trong bng bm vi cc mc t khng dng n. Chc nng resize gim kch thc bng bm bao gm s cc thng c ch r bi i s nbuck. 4.4.5 B tao bn sao Bn sao l b phn lp khc vi nhng b phn lp chng ta m t trc , n khng s dng chc nng phn loi, ng hn l, n ch s dng b phn lp nh l mt bng cc khe, n np chng phng thc recv() sinh ra n bn sao ca gi tin, m c a n tt c n cc i tng tham chiu trong bng. h tr chuyn gi tin a hng, b phn lp nhn gi tin a hng t ngun S nh trc cho nhm G tnh ton bng hm bm h(S,G) a ra s khe trong bng i tng ca b phn lp. Trong vic phn phi a hng, gi tin phi c sao chp mt cho mi lin kt dn n cc nt ng k tr i 1. S sn sinh thm ra bn sao ca gi tin c thc hin bi lp Replicator c nh ngha trong replicator.cc:
/* * A replicator is not really a packet classifier but * we simply find convenience in leveraging its slot table. * (this object used to implement fan-out on a multicast * router as well as broadcast LANs) */ class Replicator : public Classifier { public: Replicator(); void recv(Packet*, Handler* h = 0); virtual int classify(Packet* const) {}; protected: int ignore_; }; void Replicator::recv(Packet* p, Handler*) { IPHeader *iph = IPHeader::access(p->bits()); if (maxslot_ < 0) { if (!ignore_) Tcl::instance().evalf("%s drop %u %u", name(), iph->src(), iph->dst()); Packet::free(p); return; } for (int i = 0; i < maxslot_; ++i) { NsObject* o = slot_[i]; if (o != 0) o->recv(p->copy()); } /* we know that maxslot is non-null */ slot_[maxslot_]->recv(p); }

Nh chng ta thy t m lnh trn, lp ny khng thc s phn loi gi tin, thay vo , n sao li gi tin, mt cho mi mc t trong bng ca n, v phn phi bn sao ti mi nt c lit k trong bng. Mc t cui cng trong bng cha gi tin nguyn bn.

Bai tiu lun mn hoc IP-ng dung va bao mt

30

Gii thiu cng cu NS


4.5 Module nh tuyn v s t chc b phn lp

Nh thy, mt nt ns l s thu thp cn thit ca cc b phn lp, mt nt n gii nht (n hng) cha ch mt b phn lp a ch v mt b phn lp cng nh trong hnh 5.1. Khi m rng chc nng ca nt, nhiu b phn lp hn c htm vo nt c s, v d. nt a hng trong hnh 5.2. Nhiu khi chc nng hn c thm vo v mi khi yu cu cho n mt hoc nhiu b phn lp, n tr thnh iu quan trng cho nt cung cp mt giao din ng b t chc cc b phn lp ny v ni chng vi cc khi tnh ton tuyn. Phng php c bn x l trng hp ny l thng qua lp k tha, v d, nu mt mun nt h tr phn cp nh tuyn, mt dn xut Node/Hier t nt c s v xp chng cc phng thc thit lp b phn lp v chn b phn lp phn cp. Phng thc ny hot ng tt khi cc khi chc nng mi l c lp v khng th ty pha trn..

Hnh 4.3 Tng tc gia nt, module nh tuyn, v nh tuyn. ng gch ngang ch ra chi tit ca mt module nh tuyn. Ch mt phng php gii quyt vn ny l kt cu i tng. Nt c s cn nh ngha mt tp cc giao din cho vic truy nhp v t chc b phn lp. Cc giao din c th l: Cho php cc module nh tuyn ring bit thc thi cc b phn lp ca chng v a cc b phn lp ny vo nt. Cho php cc khi tnh ton tuyn a cc tuyn ti b phn lp trong tt c cc module nh tuyn cn cc thng tin ny. Cung cp mt im qun l n cho cc module nh tuyn c. Thm na, chng ta cng nh ngha mt giao din ng b cho cc module nh tuyn kt ni vi cc giao din ca nt. Trong phn ny chng ta s m t thit k ca module nh tuyn cng nh cc giao din nt tng ng. 4.5.1 Module nh tuyn Ni chung, mi mt s thc thi nh tuyn trong ns gm c 3 khi chc nng: - Tc t nh tuyn chuyn gi tin nh tuyn ti phn t bn cnh. - Logic tuyn s dng thng tin thu nht bi tc t nh tuyn (hoc t c s d liu kin trc mng chung trong trng hp nh tuyn tnh) thc hin tnh ton tuyn thc t. - B phn lp bn trong mt Nt. Chng s dng bng nh tuyn c tnh ton thc hin vic chuyn gi tin i. Bai tiu lun mn hoc IP-ng dung va bao mt 31

Gii thiu cng cu NS Ch rng khi thc thi mt giao thc nh tuyn mi, khng cn nht thit phi thc thi c 3 khi trn. V d, khi thc thi mt giao thc nh tuyn theo trng thi ca kt ni, s thc thi n gin l mt tc t nh tuyn theo cch chuyn thng tin v trng thi ca kt ni, v mt logic tuyn nh l thut ton tm ng Dijkstra da trn kt qu c s d liu kin trc mng. N c th sau s dng cng b phn lp nh cc giao thc nh tuyn n hng khc. Khi mt giao thc nh tuyn mi c thc thi gm c nhiu hn mt khi chc nng, c bit khi n cha b phn lp, n yu cu c i tng khc, gi l module nh tuyn, m qun l tt cc cc khi chc nng v giao din vi nt t chc cc b phn lp ca n. Hnh 5.3 ch ra s lin quan v chc nng ca cc i tng ny. Ch rng cc module nh tuyn c th c quan h trc tip vi cc khi tnh ton tuyn v d logic tuyn v/hoc tc t nh tuyn. Tuy nhin. tnh ton tuyn c th khng ci t cc tuyn ca n mt cch trc tip qua module nh tuyn, bi v c th tn ti nhng module khc t hc v nhng nt mi. iu ny l khng bt buc, tuy nhin bi v c th rng s tnh ton mt vi tuyn c ch r ti mt module nh tuyn c th no . V d, s ci t nhn trong module MPLS. Mt module nh tuyn cha 3 chc nng chnh: - Module nh tuyn khi to s kt ni ca n ti mt nt thng qua register{}, v hy b thng qua unregister{}. Thng thng, trong register{}. Mt module nh tuyn ni vi nt n quan tm n s cp nht tuyn mi bit v s tham gia ca tc t truyn ti hay khng, v to ra b phn lp v ci t chng trong nt. trong unregister{}, mt module nh tuyn thc hin ngc li: n xa cc b phn lp ca n v b mc cp nht nh tuyn trong nt . - Nu mt module nh tuyn quan tm n s cp nht tuyn mi bit, nt ny s thng bo cho module qua RtModule::add-route{dst, target} v RtModule::deleteroute{dst, nullagent} - Nu mt module nh tuyn quan tm n s gn vo ca tc t truyn ti v s tch ra trong mt nt, nt ny s thng bo cho module qua RtModule::attach{agent, port} v RtModule::detach{agent, nullagent} C 2 bc vit ring module nh tuyn: - Cn phi khai bo phn C++ trong module nh tuyn ca bn, cho nhiu module iu ny ch c ngha khai bo mt phng thc o name(), n tr v b m t chui ca module. Tuy nhin, bn t do thc hin nhiu chc nng nh bn mun trong C++, nu cn thit bn c th sau chuyn v mt chc nng t OTcl vo C++ cho c hiu qu hn. - Bn cn nhn cc giao din pha trn c thc thi trong module nh tuyn c s v quyt nh ci no bn s k tha, ci no bn s vit chng v a chng vo cc giao din OTcl ca module ca bn. C mt vi module nh tuyn dn xut, n c dng nh l cc mu cho module ca bn, c 6 module nh tuyn c thc thi trong ns: Tn module RtModule/Base RtModule/Mcast Chc nng Giao din cho cc giao thc nh tuyn n hng, cung cp chc nng c s cho thm/xa tuyn v gn/ tch cc tc t Giao din cho cc giao thc nh tuyn a hng, N ch c mc nh l thit lp cc b phn lp a hng. Cc 32

Bai tiu lun mn hoc IP-ng dung va bao mt

Gii thiu cng cu NS chc nng a hng khc c thc thi nh l cc th tc c th ca nt. nh tuyn phn cp, n l b bc cho qun l cc b phn lp phn cp v s ci t tuyn. c th c kt hp vi cc giao thc nh tuyn khc, nh nh tuyn adhoc nh tuyn nhn cng S dng cc b phn lp o. Thc thi cc chc nng MPLS.

RtModule/Hier RtModule/Manual RtModule/VC RtModule/MPLS 4.5.2 Giao din nt

Kt ni ti cc giao din bn trn ca module nh tuyn, mt nt cung cp mt tp cc giao din: Theo th t cc module ng k trong khi kin to, lp Nt gi mt danh sch cc module nh l mt bit lp. Gi tr mc nh ca danh sch ny gm nhng module nh tuyn c s. lp nt cung cp 2 th tc di y iu khin danh sch module: Node::enable-module{[name]} Nu module RtModule/[name] tn ti th th tc ny a [name] vo trong danh sch module Node::disable-module{[name]} Nu module [name] trong danh sch, xa n khi danh sch . Khi mt nt c to ra, n i qua danh sch module ca lp Nt, to tt c cc module cha trong danh sch v ng k nhng module ny vi nt . Sau khi nt c to ra, chng c th s dng nhng th tc di y lit k nhng module ng k vi nt , hoc gi n x l module: Node::list-modules{} tr v danh sch iu khin ca tt c cc module c ng k. Node::get-module{[name]} tr v iu khin ca module ng k vi tn module cho. Ch rng tt c cc module nh tuyn ch c mt ng k n trong tt c cc nt. Cho php cc module nh tuyn ng k s cp nht tuyn chng quan tm, i tng nt cung cp cc th tc di y: Node::route-notify{module} thm module vo danh sch thng bo cp nht tuyn. Node::unreg-route-notify{module} xa module khi danh sch thng bo cp nht tuyn. Tng t, th tc di y mc vo s tham gia ca cc tc t truyn ti. Node::port-notify{module} thm module vo danh sch thng bo tham gia ca tc t Node::unreg-port-notify{module} b module ra khi danh sch thng bo s tham gia ca tc t. Nt cung cp cc th tc di y iu khin b phn lp a ch v cng: Node::insert-entry{module, clsfr, hook} Chn b phn lp clsfr vo im mc ca nt. N cng c kt hp vi b phn lp mi ny vi module m nu b phn lp ny sau b loi b th module cng s khng c ng k na. Nu hook c ch r nh l mt s, b phn lp c s chn vo khe hook ca b phn lp mi. Theo cch ny, c th thit lp mt "dy" Bai tiu lun mn hoc IP-ng dung va bao mt 33

Gii thiu cng cu NS cc b phn lp, xem hnh 5.2 l mt v d. Ch : clsfr khng cn phi l mt b phn lp. Trong vi trng hp, c th mun a vo mt tc t, hoc nhiu lp dn xut t u ni, ti im mc ca mt nt. Trong trng hp nh vy, cung cp mt cch n gin target cho tham s hook. - Node::install-entry{module, clsfr, hook} khc vi Node::insert-entry{module, clsfr, hook}l n xa b phn lp c ti im mc ca nt , b ng k tt c kt hp module nh tuyn, v ci t b phn lp mi ti im ny. Nu hook c chuo, v b phn lp c c kt ni n mt chui b phn lp, n s kt ni chui ny vo khe hook ca b phn lp mi. Nh trn, nu hook tng ng target, clsfr s c xem nh mt i tng dn xut t u ni thay v mt b phn lp. - Node::install-demux{demux, port} t b phn lp demux cho nh l b gii ghp mc nh. Nu port cho, n cm b gii ghp c ny vo khe port ca mt b mi. Ch rng trong trng hp ny hay trng hp kia n khng xa b gii ghp c.
4.6 Xem qua mt vi lnh

$ns_ node [<hier_addr>] Lnh ny to ra v tr v mt nt. Nu <hier_addr> cho, gn a ch nt thnh <hier_addr>. Ch rng <hier_addr> ch c s dng khi cho php nh a ch phn cp thng qua hoc set-address-format hierarchical{} hoc node-config -addressType hierarchical{}. $ns_ node-config -<config-parameter> <optional-val> Lnh ny c s dng cu hnh cc nt. Cc tham s cu hnh khc nhau l kiu nh a ch, kiu ca cc thnh phn ngn xp mng, d vic gim st c bt ln hay khng, c mobileIP c bt hay khng, m hnh nng lng s c s dng hay khng, ... La chn - reset c th c dng thit lp cu hnh nt v trng thi mc nh. Gi tr mc nh c thit lp cho cu hnh nt nu khng c gi tr no c ch r $node id tr v s id ca nt. $node node-addr tr v a ch ca mt nt. Trong trng hp nh a ch phng. a ch nt ging vi node-id. Trong trng hp nh a ch phn cp, a ch nt theo khun dng mt chui (v d "1.4.3") c tr v. $node reset Thit lp li ton b cc tc t gn km cng nt. $node agent <port_num> Tr v iu khin ca tc t ti cng no . Nu khng c tc t no c tm thy ti cng cho, mt chui rng c tr v. $node entry Tr v im vo ca nt, y l i tng u tin iu khin nhn gi tin ti nt ny. $node attach <agent> <optional:port_num> Bai tiu lun mn hoc IP-ng dung va bao mt 34

Gii thiu cng cu NS Gn <agent> vo nt, trong trng hp nu khng ch r s cng, nt s nh v s cng v ng tc t vo cng ny. Nh th mt tc t c gn vo, n nhn cc gi tin nh trc cho cng v nt ny. $node detach <agent> <null_agent> Ngc vi attach c m t trn, n g b tc t t nt ny v ci t mt tc t rng vo cng m tc t ny c gn n, kt thc x l cc gi tin chuyn tip ti tc t g b. Nhng gi tin ang trn ng n sau a vo tc t rng. $node neighbors tr v danh sch ca cc nt xung quanh. $node add-neighbor <neighbor_node> y l lnh thm <neighbor_node> vo danh sch cc nt xung quanh duy tr bi nt. Di y l danh sch cc phng thc bn trong nt: $node add-route <destination_id> <target> Lnh ny s dng trong nh tuyn n hng b phn lp. target l mt i tng Tcl, n c th l im mc ca dmux_(b gii ghp cng trong nt) trong trng hp <destination_id> l ging vi node-id. Mt khc n l u ca lin kt n, n cng l im mc cho cc b phn lp khc. $node alloc-port <null_agent> tr v s cng kh dng tip theo. $node incr-rtgtable-size Bin s rtsize_ is c dng gi vt ca kch c bng nh tuyn trong mi nt. Lnh ny s dng tng kch c bng nh tuyn mi ln mt im mc nh tuyn c thm vo cc b phn lp. Cn c nhiu lnh nt khc h tr nh tuyn phn cp, nh tuyn ng chi tit, nh tuyn a ng gi tr tng ng, nh tuyn nhn cng, v m hnh nng lng cho cc nt di ng. Cc phng thc trn v nhng phng thc khc c th thy trong ~ns/tcl/lib/ns-node.tcl and ~ns/tcl/lib/nsmobilenode.tcl.

5. Cc lin kt: Cc lin kt n


y l vn th hai trong vic xc nh cu trc mng. Mc 5 chng ta m t cch to ra mt node trong cu trc ns, mc ny chng ta s m t cch to ra cc lin kt lin kt cc node v hon thin cu trc mng trong ns. y chng ta s hn ch ch m t lin kt im im n. ns h tr nhiu mi trng khc nhau, bao gm m phng a truy nhp LAN s dng cc lin kt n hnh li, cc m phng thc t ca mi trng truyn thng khng dy v truyn thng qung b khc. Chng s c m t trong cc mc ring bit. Lin kt CBQlink c hnh thnh t cc lin kt n v l dng lin kt phc tp hn nhiu v cng khng c m t trong mc ny. Chng ta bt u bng cch m t cc lnh thit lp mt lin kt. Vi node bao gm cc b phn loi, mt lin kt n c dng ln t mt chui cc b connector. Chng ta s m t mt cch s lc cc connector trong mt lin kt n, sau s m t cc th tc hot ng da trn cc thnh phn khc nhau ca cc connector, i tng connector, bao gm c m t tm tt v cc connector lin kt thng dng. Lp Link l lp ng c lp trong OTcl, n cung cp mt vi nguyn t ban u n gin. Lp SimpleLink cho php ni 2 node bng mt lin kt im - im. ns cung cp th tc simplex-link{} to nn lin kt n hng t mt node ny ti mt node Bai tiu lun mn hoc IP-ng dung va bao mt 35

Gii thiu cng cu NS khc. Lin kt ny nm trong lp SimpleLink. Dng lnh di y m t c php ca mt lin kt mt chiu:
set ns [new Simulator] $ns simplex-link <node0> <queue_type> <node1> < bandwidth> <delay>

Lnh to lin kt t <node0> n <node1>, vi c tnh <bandwidth> v <delay> c th. Lin kt s dng mt loi hng i <queue_type>. Th tc ny cng thm vo lin kt b kim tra TTL . 5 bin s nh ngha cho mt lin kt bao gm: head_ im bt u lin kt, tr n i tng u tin trong lin kt. queue_ tham chiu ti phn t hng i chnh ca lin kt. Cc lin kt n thng c mt hng i trn lin kt. Cc loi lin kt phc tp hn c th c nhiu phn t hng i trn lin kt. link_ tham chiu n phn t thc s hnh thnh nn lin kt, xt trn cc kha cnh ca cc c tnh tr v bng thng ca lin kt. ttl_ tham chiu n phn t iu khin ttl trong tt c mi gi. drophead_ tham chiu n mt i tng ng u hng i ca cc phn t thc hin x l loi b lin kt. Hnh 6.1: Cc thnh phn cu trc ca tuyn lin kt mt hng: Thm vo , nu nu bin $traceAllFile ca b m phng c nh ngha, th tc s a thm phn t tm du thc hin vic tm du vt khi mt gi c xp vo hng i v loi b khi hng i t queue_. Hn na, vic tm vt t mt phn t tm vt sau drophead_. Cc bin sau cho cc cch tm vt trong cc phn t tm vt: enqT_ Tham chiu n phn t tm vt cc gi i vo queue_. deqT_ Tham chiu n phn t tm vt cc gi ri khi queue_. drpT_ Tham chiu n phn t tm vt cc gi c loi b khi queue_. rcvT_ Thm chiu n phn t tm vt cc gi c nhn bi node tip theo. Lu rng, nu ngi s dng cho php tm du nhiu ln trn lin kt, cc bin ny s ch lu gi mt tham chiu n phn t cui cng c chn vo. Cc cu hnh khc c th a thm cc thnh phn vo mt lin kt n l cc giao tip mng (c s dng trong nh tuyn a ng), cc m hnh tuyn lin kt ng, ln vt v gim st. Th tc duplex-link{} to ra lin kt hai chiu t hai lin kt mt chiu.
5.1. Th tc cho Links v SimpleLinks

Cc th tc Link: Lp Link c thc hin hon ton trong OTcl. Lp SimpleLink trong OTcl s dng lp C++ LinkDelay m phng tr chuyn pht gi. Cc th tc trong lp Link l: head{} tr v knh iu khin cho head_. queue{} tr v knh iu khin cho queue_. link{} tr knh iu khin cho phn t tr, link_. up{} thit lp trng thi up trong phn t dynamics_. ng thi ghi li du vt vo mi file ring bit thng qua th tc trace-dynamics{}. down{} Ging nh up{}, thit lp trng thi down trong phn t dynamics_. ng thi ghi du vt cho mi file ring bit thng qua th tc tracedynamics{}. 36

Bai tiu lun mn hoc IP-ng dung va bao mt

Gii thiu cng cu NS up?{} tr v trng thi ca lin kt. Trng thi ca lin kt l up hoc down, trng thi l up nu tuyn lin kt ng khng c kch hot. all-connectors{} p dng hot ng c th cho tt c cc connector trn lin kt. V d v p dng l all-connectors reset. cost{} Thit lp chi ph cho tuyn kt ni vi mt gi tr nht nh. cost?{} Tr v gi tr chi ph ca tuyn lin kt. Mc nh gi tr chi ph ca tuyn lin kt l 1 nu khng c gi tr c th c thit lp trc . Cc th tc SimpleLink. Lp SimpleLink ca Otcl thc hin mt lin kt n im-im vi hng i v tr c lin quan. Lp ny c tha k t lp c s Link ca Otcl nh sau: Class SimpleLink -superclass Link SimpleLink instproc init { src dst bw delay q { lltype "DelayLink" } } { $self next $src $dst $self instvar link_ queue_ head_ toNode_ ttl_ ... set queue_ $q set link_ [new Delay/Link] $link_ set bandwidth_ $bw $link_ set delay_ $delay $queue_ target $link_ $link_ target [$toNode_ entry] ... # XXX # put the ttl checker after the delay # so we dont have to worry about accounting # for ttl-drops within the trace and/or monitor # fabric # set ttl_ [new TTLChecker] $ttl_ target [$link_ target] $link_ target $ttl_ } Ch rng khi mt i tng SimpleLink c to lp, cc i tng Delay/Link v TTLChecker mi cng c to lp. Cng cn phi lu rng i tng Queue phi c to lp. C 2 phng php b sung khc c thc hin (trong OTcl) nh l mt phn ca lp SimpleLink: trace v init-monitor.
5.2. Cc Connector

Cc Connector, cc b phn loi khng lin kt, ch to ra s liu cho mt ngui nhn, cc gi c a ra ti target_ ln cn hay n c gi ti drop-target_. Mt connector s nhn mt gi, thc hin mt vi chc nng v a gi n nt ln cn hoc loi b gi. C mt s loi connector khc nhau trong ns. Mi Connector thc hin mt chc nng khc nhau. networkinterface: dn nhn cc gi vi vic nh dng giao tip ti, n c s dng trong mt s giao thc nh tuyn nhiu ng. Lp c th thay i m phng s Bai tiu lun mn hoc IP-ng dung va bao mt 37

Gii thiu cng cu NS u giao tip -1 ch ra rng ns chn giao tip ny, v sau n c chn phn cn li trong luyn lin kt n. i tng DynaLink: to cng lu lng ph thuc vic lin kt l up hay down. Chng ta mong mun t i tng ny u ca lin kt v c chn vo lin kt trc khi bt u m phng. Bin status_ iu khin trng thi ca lin kt l up hoc down. i tng DelayLink: Lp m hnh cho cc c tnh bng thng v tr ca tuyn lin kt. Nu tuyn lin kt khng phi l lin kt ng, th i tng ny s nh trnh mt cch n gin cc s kin nhn gi cho i tng lung xung vi mi gi n nhn c thi im tng ng ca gi . Tuy nhin, nu l lin kt ng th i tng xp cc gi vo hng i bn trong n v t nh trnh mt s kin nhn gi cho gi phi c pht i k tip. Bi vy nu tuyn lin kt c trng thi down ti mt s im, phng thc reset() ca i tng c thc hin, v i tng s loi b tt c cc gi ang truyn dn trong lc tuyn lin kt c s c. Cc Queue: lp m hnh ca b m u ra c gn vo lin kt trong mt b nh tuyn thc trong mng. Trong ns, chng ta gn vo v c xem nh mt phn ca lin kt. TTLChecker: s gim gi tr ca ttl trong mi gi m n nhn c. Nu ttl sau c gi tr dng, gi s c chuyn tip ti phn t tip theo trn lin kt. Trong cc lin kt n, TTLChecker c t ng thm vo v v tr ca chng l phn t cui cng trong tuyn lin kt, gia phn t tr v im vo cho node k tip.
5.3 Phn cp i tng.

Lp c s c s dng th hin cc tuyn lin kt c gi l Link. Cc phng thc ca lp ny c lit k trong phn k tip. Cc i tng lin kt khc c dn xut t lp c s bao gm: - i tng SimpleLink: c s dng th hin mt tuyn lin kt mt chiu. Khng c bin trng thi hoc tham s cu hnh no c lin quan n i tng ny. Cc phng thc cho lp ny l: $simplelink enable-mcast <src> <dst>. Phng thc ny kch hot a hng cho lin kt bng vic to ra mt giao tip mng u vo cho ch n v thm vo giao din u ra cho ngun. $simplelink trace <ns> <file> <optional:op> Thit lp cc i tng tm vt cho tuyn lin kt v cp nht lin kt i tng. Nu op c nh ngha l nam th to ra cc file ln vt l nam. $simplelink nam-trace <ns> <file> Thit lp ln vt nam trong tuyn lin kt $simplelink trace-dynamics <ns> <file> <optional:op> Thit lp ny c bit ln vt cho cc tuyn lin kt ng. <op> cng cho php thit lp qu trnh ln vt theo nam. $simplelink init-monitor <ns> <qtrace> <sampleInterval> Chn cc i tng cho php chng ta gim st kch thc hng i ca tuyn lin kt ny. Tr li tn ca i tng c th truy vn xc nh kch thc trung bnh ca hng i. $simplelink attach-monitors <insnoop> <outsnoop> <dropsnoop> <qmon> Tng t init-monitor, nhng cho php ch nh nhiu thng s hn. $simplelink dynamic Thit lp c dynamics cho tuyn lin kt ny. $simplelink errormodule <args> Bai tiu lun mn hoc IP-ng dung va bao mt 38

Gii thiu cng cu NS Chn mt module li trc hng i. $simpleilnk insert-linkloss <args> Chn mt module li sau hng i. // Cc i tng ca tuyn lin kt khc c dn xut t lp SimpleLink bao gm: FQLink, CBQLink v IntServLink. Tham s cu hnh cho FQLink gm: queueManagement_ Kiu qun l hng i c s dng trong lin kt. Gi tr mc nh l DropTail. Khng c tham s cu hnh c ch nh cho CBQLink v IntServLink. i tng DelayLink: cc i tng ny xc nh thi tng gian cn thit mt gi i qua mt lin kt. Thi gian ny c tnh theo cng thc size/bw + delay vi size l kch thc gi, bw l rng bng ca lin kt v delay l tr lan truyn ca lin kt. Khng c phng thc hay bin trng thi no c lin quan ti i tng ny. Cc tham s cu hnh l: bandwidth_ rng bng thng ca tuyn lin kt tnh bng bit/giy (bps). delay_ Tr lan truyn ca lin kt tnh bng giy.

6. Qun l hng i v nh trnh gi.


Hng i m t v tr m ti cc gi c th c ct gi (hoc b loi b). nh trnh gi c lin quan ti qu trnh quyt nh c s dng chn cc gi no c phc v hoc b loi b. Qun l b m c lin quan n bt k quy nh c th no c s dng iu chnh thi gian b nh ca mt hng i ring bit. Hin ti, cc h tr v hng i bao gm: hng i loi b phn cui (drop-tail queueuing FIFO), qun l b m RED, CBQ (bao gm mt b nh trnh u tin v quay vng) v cc loi khc nhau ca hng i cn bng (Fair Queueing) bao gm hng i cn bng (Fair Queueing - FQ), hng i cn bng ngu nhin (Stochastic Fair Queueing - SFQ), v Deficit Round-Robin (DRR). Trong trng hp chung khi mt phn t tr l lung xung t hng i, hng i c th b kha ti khi n c th thc hin li bi lung xung ln cn n. y chnh l c ch m phng tr truyn dn. Hn na, hng i c th b kha hoc m kha cng bc ti cc thi im bt k bi lung ln cn (c s dng thc hin cc hng i hp nht t nhiu hng i vi s iu khin lung gia cc hng i). S loi b gi c thc hin theo mt cch m hng i c cha mt im gi cc gi c loi b, tc l, mt i lng thu gi tt c cc gi b loi b ra khi hng i. iu ny c th hu ch khi thng k v cc gi b loi b.
6.1. Lp Queue trong C++

Lp Queue tha k t lp c s Connector. N cung cp mt lp c s c s dng bi cc kiu lp hng i c th, cng nh hm gi li thc hin kha. Cc nh ngha ny c phc v trong queue.h: class Queue : public Connector { public: virtual void enque(Packet*) = 0; virtual Packet* deque() = 0; void recv(Packet*, Handler*); void resume(); int blocked(); void unblock(); Bai tiu lun mn hoc IP-ng dung va bao mt 39

Gii thiu cng cu NS void block(); protected: Queue(); Int command(int argc,const char*const*argv); int qlim_; * maximum allowed pkts in queue */ int blocked_; int unblock_on_resume_; /* unblock q on idle? */ QueueHandler qh_; }; Cc hm enque v deque l thun ty o, cho thy rng lp Queue c s dng nh l lp c s, cc hng i c th nhn c t Queue v thc hin 2 chc nng ny khi cn thit. Nhn chung, cc hng i c th khng np chng hm recv bi v n gi n enque v deque c th. Lp Queue khng c nhiu trng thi ni ti. Thng thng cc trng thi ni ti ny l cc i tng gim st c bit. Hm thnh vin qlim_ c dng ch th gii hn chim gi hng i ti a, nhng t thn lp Queue khng bt buc iu ny; n c s dng bi cc lp con hng i c th nu chng cn gi tr ny. Hm thnh vin blocked_ l hm tr v gi tr logic Bool ch ra hng i c th c th gi mt gi tc th ti lung xung ln cn ca n hay khng. Khi mt hng i b kha, n c th a cc gi vo hng i nhng khng th gi cc gi . 6.1.1 Kha hng i Mt hng i c th b kha v c m kha ti bt k thi im nh trc no. Nhn chung, mt hng i b kha khi mt gi ang chuyn i gia n v lung xung ln cn ca n (phn ln thi gian nu hng i b y). Mt hng i b kha s vn b kha trong khi lin kt lung xung ca n bn v hng i c t nht mt gi c gi i. Mt hng i c m kha ch khi hm resume c gi (theo cch m mt lung xung ln cn nh trnh n thng qua mt hm gi li), thng l khi khng c gi no c a vo hng i. Vic gi li c thc hin bng cch s dng lp lung v cc phng thc sau: class QueueHandler : public Handler { public: inline QueueHandler(Queue& q) : queue_(q) {} void handle(Event*); /* calls queue_.resume() */ private: Queue& queue_; }; void QueueHandler::handle(Event*) { queue_.resume(); } Queue::Queue() : drop_(0), blocked_(0), qh_(*this) { Tcl& tcl = Tcl::instance(); bind("limit_", &qlim_); } void Queue::recv(Packet* p, Handler*) { Bai tiu lun mn hoc IP-ng dung va bao mt 40

Gii thiu cng cu NS enque(p); if (!blocked_) { /* * Were not block. Get a packet and send it on. * We perform an extra check because the queue * might drop the packet even if it was * previously empty! (e.g., RED can do this.) */ p = deque(); if (p != 0) { blocked_ = 1; target_->recv(p, &qh_); } } } void Queue::resume() { Packet* p = deque(); if (p != 0) target_->recv(p, &qh_); else { if (unblock_on_resume_) blocked_ = 0; else blocked_ = 1; } } Vic qun l cc trnh x l li y i khi rt tinh t. Khi to ra mt i tng Queue mi, n bao gm mt i tng QueueHandler (qh_) c khi to cha mt tham chiu n i tng Queue mi (Queue& Queuehandler: :queue). iu ny c thc hin bi hm dng Queue s dng tham s biu thc qh_(*this). Khi mt Queue nhn mt gi, n s gi phin bn lp con (tc l to hng i theo quy nh c th) ca hm enque vi gi . Nu hng i khng b kha, n c php gi mt gi v gi hm deque xem xt gi gi, kha hng i (v mt gi ang c chuyn i) v gi gi n hng i ca lung xung ln cn. Lu l mi gi nhn c sau ca lung ln ln cn s n mt hng i b kha. Khi mt lung xung ln cn mun lm cho hng i c m kha, n nh trnh hm handle ca QueueHandler bng cch truyn &qh_ n b nh trnh m phng. Hm handle gi hm resume, hm ny s gi gi c nh trnh k tip n lung xung (v vn hng i b kha), hoc m kha hng i khi khng c gi no sn sng c gi i. Qu trnh ny c thc hin r hn khi tham kho phng thc LinkDelay::recv(). 6.1.2. Lp PacketQueue Lp Queue c th thc hin qun l v nh trnh b nh m nhng khng thc hin cc chc nng mc thp trn mt hng i c th. Lp PacketQueue c s dng cho mc ch ny v c nh ngha nh sau (xem queue.h): class PacketQueue { public: Bai tiu lun mn hoc IP-ng dung va bao mt 41

Gii thiu cng cu NS PacketQueue(); int length(); /* queue length in packets */ void enque(Packet* p); Packet* deque(); Packet* lookup(int n); /* remove a specific packet, which must be in the queue */ void remove(Packet*); protected: Packet* head_; Packet** tail_; int len_; // packet count }; Lp ny duy tr mt danh sch lin kt (linked-list) ca cc gi v thng c s dng bi cc nguyn tc nh trnh c th v qun l b nh m lu gi mt tp hp th t cc gi. Cc s nh trnh c th v qun l b nh m c th s dng nhiu i tng PacketQueue. Lp PacketQueue duy tr s m cc gi c lu gi trong hng i c tr v bi phng thc length(). Hm enque t mt gi c th vo cui hng i v cp nht bin thnh vin len_. Hm deque tr v gi u hng i v xa n khi hng i (v cp nht b m), hoc tr v NULL nu hng i rng. Hm lookup tr v gi th n thnh t u hng i, hay NULL nu khng c. Hm remove xa gi c lu gi ti mt a ch cho trc khi hng i (v cp nht b m). Hm ny gy ra s kt thc chng bt thng nu gi khng tn ti.
6.2. V d: Loi b phn cui (Drop Tail)

V d sau y minh ha hot ng ca i tng Queue/DropTail l i tng thc hin nh trnh FIFO v s qun l b nh m kiu loi b khi trn b nh (dropon-overflow) in hnh ca phn ln cc b nh tuyn Internet ngy nay. nh ngha sau khai bo lp v lin kt OTcl ca n: /* * A bounded, drop-tail queue */ class DropTail : public Queue { protected: void enque(Packet*); Packet* deque(); PacketQueue q_; }; Lp c s Queue m t DropTail c tha k cung cp hu ht cc chc nng cn thit. Hng i drop-tail duy tr chnh xc mt hng i FIFO, thc hin bng cch gp mt i tng ca lp PacketQueue. Drop-tail thc thi phin bn enque v deque ca ring n nh sau: /* * drop-tail */ void DropTail::enque(Packet* p) { q_.enque(p); if (q_.length() >= qlim_) { Bai tiu lun mn hoc IP-ng dung va bao mt 42

Gii thiu cng cu NS q_.remove(p); drop(p); } } Packet* DropTail::deque() { return (q_.deque()); } y, hm enque ban u lu gi trong hng i gi ni ti (khng c gii hn v kch thc), v sau kim tra ca hng i gi so vi qlim_. Kiu loi b khi trn b nh c thc hin bng cch loi b gi c chn vo hng i gi gn y nht nu nh dt n hay vt qu gii hn. nh trnh FIFO n gin c thc hin trong hm deque bng cch lun tr v gi u tin trong hng i gi.
6.3 Cc kiu khc nhau ca i tng Queue

Mt i tng hng i l mt lp i tng chung c kh nng lu gi v c th nh du hay loi b cc gi khi chng i qua qua topo m phng. Cc tham s cu hnh c s dng cho i tng hng i l: limit_ Kch thc hng i trong cc gi. blocked_ Mc nh c thit lp gi tr false, gi tr ny l true nu hng i b kha (khng th gi gi n lung xung ln cn ca n). unblocked_on_resume_ Mc nh c thit lp gi tr true, ch ra rng mt hng i cn t m kha lc gi cui cng c gi truyn i (nhng khng cn thit phi nhn c). Cc i tng hng i khc nhn c t lp c s Queue l cc i tng hng i drop-tail, FQ, SFQ, DRR, RED v CBQ. Mi i tng c m t nh sau: i tng drop-tail: l lp con ca i tng Queue thc thi cc hng i FIFO n gin. Khng c phng thc, tham s cu hnh, hay bin trng thi no c ch nh. i tng FQ: l lp con ca i tng Queue thc thi hng i cn bng. Khng c phng thc no c ch nh i vi i tng FQ. Cc tham s cu hnh l: secsPerByte_ Khng c bin trng thi no c lin quan n i tng ny. i tng SFQ: l lp con ca i tng Queue thc thi hng i cn bng ngu nhin. Khng c phng thc no c ch nh cho cc i tng SFQ. Cc tham s cu hnh l: maxqueue_ buckets_ Khng c bin trng thi no c lin quan n i tng. i tng DRR: l lp con ca i tng Queue thc thi nh trnh Deficit Round Robin. Cc i tng ny thc hin nh trnh DDR trong s cc lung khc nhau (mt lung c th l lung c cc gi vi cng mt node v port id hoc cc gi ch c cng node id). Khng nh cc i tng a hng i khc, i tng hng i ny thc hin mt khng gian b nh m dng chung cho cc lung khc nhau ca n. Cc tham s cu hnh l: buckets_ ch ra tng s vng nh d liu c dng bm nh cc lung. blimit_ ch ra kch thc b nh m dng chung theo byte. Bai tiu lun mn hoc IP-ng dung va bao mt 43

Gii thiu cng cu NS quantum_ ch ra bao nhiu byte mt lung c th gi trong lt ca n. mask_ mt n, khi c thit lp 1, ngha l mt lung c th bao gm cc gi c cha cng node id (v c th c cc port id khc nhau), nu khc, mt lung cha cc gi c cng node v port id. i tng RED: l lp con ca i tng Queue thc thi cc cng pht hin sm ngu nhin (random early-detection gateway). i tng c th c lp cu hnh hoc loi b hoc mt n cc gi . Khng c phng thc no c ch nh cho cc i tng RED. Cc tham s cu hnh l: bytes_ Thit lp true cho php byte-mode RED, trong kch thc ca cc gi n c nh hng ti kh nng to mt n (loi b) cc gi. queue-in-bytes_ Thit lp true nh gi kch thc trung bnh ca hng i theo byte hn l cc gi. Cho php ty chn ny cng lm cho thresh_ v maxthresh_ c iu chnh t ng bi mean_pksize_ ,c th nh sau: thresh_ ngng ti thiu cho kch thc trung bnh hng i trong cc gi. maxthresh_ ngng ti a cho kch thc trung bnh hng i trong cc gi. mean_pksize_ c lng th v kch thc trung bnh ca hng i theo byte. Dng cp nht kch thc trung bnh ca hng i tnh c sau mt khong thi gian ri. q_weight_ Trng s hng i, c dng trong vic tnh trung bnh ng trng s theo hm m tnh ton kch thc trung bnh ca hng i. wait_ Thit lp true duy tr mt khong thi gian gia cc gi c loi b. linterm_ Khi kch thc trung bnh ca hng i bin i gia thresh_ v maxthresh_, xc sut ly gi ra bin i gia 0 v 1/linterm. setbit_ Thit lp true nh du cc gi bng cch thit lp bit ch th nghenxtrong cc mo u gi hn l cc gi c loi b. drop-tail_ Thit lp true s dng drop-tail hn l random-drop khi c s trn b nh hng i hoc kch thc trung bnh ca hng i vt qu maxthresh_. c s gii thch k hn v cc bin ny. Khng th truy nhp vo cc bin trng thi ca i tng RED. i tng CBQ: l lp con ca i tng Queue thc thi to hng i lp c s. $cbq insert <class> Chn lu lng lp <class> vo cu trc dng chung lin kt c lin quan ti cbq i tng lin kt. $cbq algorithm <alg> Chn thut ton ni b cbq. <alg> c th c thit lp vi mt trong cc gi tr: ancestor-only, top-level hoc formal. i tng CBQ/WRR: l lp con ca i tng Queue thc thi nh trnh lun chuyn theo trng s gia cc lp c cng mc u tin. Ngc li, cc i tng CBQ thc thi nh trnh lun chuyn tng gi gia cc lp c cng mc u tin. cc tham s cu hnh l: maxpkt_ Kch thc ti a ca gi theo byte. Ch c s dng bi cc i tng CBQ/WRR trong vic tnh ton cp pht bng thng ti a cho b nh trnh lun chuyn theo trng s. CC I TNG CBQCLASS Cc i tng CBQClass thc thi cc lp lu lng c lin quan ti cc i tng CBQ. $cbqclass setparams <parent> <okborrow> <allot> <maxidle> <prio> <level> Bai tiu lun mn hoc IP-ng dung va bao mt 44

Gii thiu cng cu NS Thit lp nhiu tham s cu hnh cho lp lu lng CBQ (xem di y). $cbqclass parent <cbqcl|none> Ch nh lp cha ca ca lp ny trong cy lin kt dng chung. Lp cha c th c ch nh l none ch ra y l mt gc. $cbqclass newallot <a> Thay i cp pht lin kt ca lp ny ti mt s lng c th (trong phm vi 0.0 n 1.0). Ch nhng lp c ch nh b nh hng. $cbqclass install-queue <q> Ci t mt i tng Queue vo trong cu trc lin kt CBQ hay CBQ/WRR ghp. Khi mt i tng c to ra ban u, n khng c hng i ni ti (ch c mt b phn loi v nh trnh gi). Cc tham s cu hnh l: okborrow_ l gi tr logic Bool ch ra lp c php mn bng thng t cha ca n. allot_ l t s bng thng c cp pht ti a cho mt lp c th hin nh mt s thc gia 0.0 v 1.0. maxidle_ l tng thi gian ti a mt lp c th i hi a cc gi ca n vo hng i trc khi chng c php chuyn tip. priority_ l mc u tin ca lp so vi lp khc. Gi tr ny c th bin i t 0 n 10, v nhiu hn mt lp c th tn ti cng mc u tin. u tin 0 l u tin cao nht. level_ l mc ca lp ny trong cy lin kt dng chung. Cc node l trong cy c xem l mc 1; cha ca chng l mc 2, v.v. extradelay_ tng thi gian tr bi mt lp b tr trong mt khong thi gian c th. CC I TNG GIM ST HNG I (QUEUE-MONITOR ) Cc i tng QueueMonitor c dng gim st tp hp cc b m ca im i, n v loi b ca gi v byte. N cng bao gm h tr cho vic thng k chung nh kch thc hng i, v.v. $queuemonitor thit lp li tt c cc b m tch ly c m t di y (i, n v loi b) v zero. N cng thit lp li cc b tch hp v cc b ly mu tr nu c ch nh. $queuemonitor set-delay-samples <delaySamp_> Thit lp i tng ly mu delaySamp_ ghi li cc thng k v tr hng i. delaySamp_ cng l b x l li i vi i tng Sample tc l i tng Sample c to ra. $queuemonitor get-bytes-integrator Tr v mt i tng Intrgrator m c th c dng tm ra ton b ca kch thc hng i trong cc byte. $queuemonitor get-pkts-integrator Tr v mt i tng Integrator m c th c dng tm ra ton b kch thc hng i trong gi. $queuemonitor get-delay-samples Tr v mt i tng Samples delaySamp_ ghi li cc thng k v tr hng i. Khng c tham s cu hnh no c ch nh cho i tng ny. Cc bin trng thi l: Bai tiu lun mn hoc IP-ng dung va bao mt 45

Gii thiu cng cu NS size_ kch thc tc thi ca hng i trong cc byte. pkts_ kch thc tc thi ca hng i trong cc gi. parrivals_ tng s hin c ca cc gi i barrivals_ Tng s byte hin c cha trong cc gi i. pdeparture_ Tng s hin c cc gi n (khng phi gi c loi b). bdepartures_ Tng s byte hin c cha trong cc gi n (khng phi gi c loi b). pdrops_ tng s gi c loi b. bdrops_ tng s byte c loi b. bytesInt_ i tng Integrator tnh ton tng kch thc hng i trong cc byte. Bin sum_ ca i tng ny c tng hin ti (tng s) ca kch thc hng i trong cc byte. pktsInt_ i tng Integrator tnh ton tng s kch thc hng i trong cc gi. Bin sum_ ca i tng ny c tng hin ti (tng s) ca kch thc hng i trong cc gi. CC I TNG QUEUEMONITOR/ED Cc i tng tha k ny c th phn bit vic loi b gi thng thng so vi gi loi b sm. Mt vi hng i phn bit vic loi b thng thng (v d loi b ht b nh m) so vi cc trng hp loi b khc (v d loi b ngu nhin trong cc hng i RED). trong mt trng hp, vic phn bit c hai kiu loi b ny rt c ch. Cc bin trng thi l: epdrops_ S gi c loi b sm. ebdrops_ S byte cha cc gi c loi b sm. Ch : v lp ny l mt lp con ca QueueMonitor, cc i tng kiu ny cng c cc trng nh pdrops_ v bdrops_. Cc trng ny m t tng s cc gi v byte c loi b, bao gm c loi b sm v loi b khng sm. CC I TNG QUEUEMONITOR/ED/FLOWMON Cc i tng ny c th c dng trong v tr ca i tng QueueMonitor quy c khi mun thu thp cc bin m v thng k trn lung (per-flow) so vi cc bin m v thng k tng s c cung cp bi QueueMonitor c s. $fmon classifier <cl> Chn (c) cc b phn loi c th vo (t) i tng gim st lung. c dng nh x cc gi n vo cc lung c lin quan n cc gi . $fmon dump s liu t cc b m trn lung v thng k i vi cc knh I/O c ch nh trong mt hot ng nh km trc . $fmon flows Tr v chui k t cha tn ca tt c cc i tng lung c bit n bi qu trnh gim st lung. Mi i tng ny c kiu QueueMonitor/ED/Flow. $fmon attach <chan> nh km mt knh tcl I/O vo qu trnh gim st lung. Thng k lung c ghi vo knh khi hot ng s liu c thi hnh. Cc tham s cu hnh l:

Bai tiu lun mn hoc IP-ng dung va bao mt

46

Gii thiu cng cu NS enable_in_ Mc nh c thit lp true, ch ra rng trng thi i trn lung cn c gi qu trnh gim st lung. Nu thit lp false, ch c thng tin i tng th c gi. enable_out_ Mc nh thit lp true, ch ra trng thi n trn lung cn c gi bi qu trnh gim st lung. Nu thit lp false, ch c thng tin n tng th c gi. enable_drop_ Mc nh thit lp true, ch ra trng thi loi b trn lung cn c gi bi qu trnh gim st lung. Nu thit lp false, ch c thng tin loi b tng th c gi. enable_edrop_ Mc nh thit lp true, ch ra trng thi loi b sm trn lung cn c gi bi qu trnh gim st lung. Nu thit lp false, ch c thng tin loi b sm tng th c gi. CC I TNG QUEUEMONITOR/ED/FLOW Cc i tng ny cha s m v thng k trn lung c qun l bi i tng QueueMonitor/ED/FlowMon. Chng thng c to ra trong mt th tc goi li OTcl khi mt qu trnh gim st lung c a ra trong mt gi khng th nh x vo mt lung bit. Lu rng b phn loi ca qu trnh gim st lung chu trch nhim nh x cc gi vo cc lung theo cch ty . Do , ty vo kiu b phn laoij c s dng, khng phi tt c cc bin trng thi u c lin quan (v d mt bin c th phn loi cc gi ch da vo id ca lung, khi a ch ngun v ch c th khng quan trng). Cc bin trng thi l: src_ a ch ngun ca gi thuc lung ny. dst_ a ch ch ca gi thuc lung ny. flowid_ id lung ca gi thuc lung ny.

7. Tr v cc tuyn lin kt
Tr biu din thi gian cn thit gi di chuyn qua mt tuyn lin kt. Dng c bit ca tr (lin kt ng) cng bao hm c kh nng li ca tuyn lin kt. Thi gian cn thit cho mt gi di chuyn qua mt tuyn c nh ngha l s/b + d vi s l kch thc ca gi, b l tc ca tuyn tnh bng bits/giy, v d l tr ca tuyn tnh bng giy.
7.1 Lp LinkDelay

Lp LinkDelay c dn xut t lp c s Connector. N c nh ngha trong ~ns/delay.cc v c m t vn tt nh sau: class LinkDelay : public Connector { public: LinkDelay(); void recv(Packet* p, Handler*); void send(Packet* p, Handler*); void handle(Event* e); double delay(); /* line latency on this link */ double bandwidth(); /* bandwidth on this link */ inline double txtime(Packet* p) { /* time to send pkt p on this link */ hdr_cmn* hdr = (hdr_cmn*) p->access(off_cmn_); return (hdr->size() * 8. / bandwidth_); } Bai tiu lun mn hoc IP-ng dung va bao mt 47

Gii thiu cng cu NS protected: double bandwidth_; double delay_; int dynamic_; Event inTransit_; PacketQueue* itq_; Packet* nextPacket_; */ Event intr_; }; Phng thc recv() np chng t lp c s Connector. N c nh ngha nh sau: void LinkDelay::recv(Packet* p, Handler* h) { double txt = txtime(p); Scheduler& s = Scheduler::instance(); if (dynamic_) { Event* e = (Event*)p; e->time_ = s.clock() + txt + delay_; itq_->enque(p); schedule_next(); } else { s.schedule(target_, p, txt + delay_); } /* XXX only need one intr_ since upstream object should * block until its handler is called * * This only holds if the link is not dynamic. If it is, then * the link itself will hold the packet, and call the upstream * object at the appropriate time. This second interrupt is * called inTransit_, and is invoked through schedule_next() */ s.schedule(h, &intr_, txt); } i tng ny h tr mt th tc ci t (instproc-like), $object dynamic, thit lp bin dynamic_ ca n. Bin ny xc nh xem lin kt c phi l ng hay khng (tc l kh nng xy ra li/phc hi ti cc thi im tng ng). Cch x l ni ti ca mi lin kt l khc nhau. Vi trng hp cc lin kt khng ng (non-dynamic), phng thc ny hot ng bng cch nhn mt gi, p, v nh trnh hai s kin. Gi s hai s kin ny l E1 v E2, v s kin E1 c nh trnh xy ra trc s kin E2. E1 c nh trnh xy ra khi node lung ln nh vi phn t tr ny hon thnh vic gi gi hin ti (c thi gian tng ng vi kch thc gi chia cho bng thng ca lin kt). E1 thng c lin quan ti i tng Queue, v s khin cho n (c th) b kha (xem Phn 7.1.1). E2 th hin s kin n ca mt gi ti lung xung ln cn ca phn t tr. S kin E2 xy ra chm hn s kin E1 vi giy tng ng vi tr ca lin kt. Bai tiu lun mn hoc IP-ng dung va bao mt 48 /* bandwidth of underlying link (bits/sec) */ /* line latency */ /* indicates whether or not link is ~ */ /* internal packet queue for dynamic links */ /* to be delivered for a dynamic link.

Gii thiu cng cu NS C ln lt nh vy, khi lin kt l ng, v nhn p, sau n s nh trnh E1 c th m kha hng i ti thi im tng ng. Tuy nhin, E2 c nh trnh ch nu p l gi duy nht hin ang c truyn i. Mt khc, c t nht mt gi ang truyn i trn lin kt phi c nhn trc p ti E2. Do , gi p c gi trong hng i inTransit ca i tng, itq_. Khi gi ngay trc p ang truyn trn lin kt c nhn ti node ln cn, i tng DelayLink s t nh trnh mt s kin pht ra ti E2. Ti thi im tng ng , phng thc handle() ca n s gi p trc tip ti ch ca mnh. Phng thc ni ti schedule_next() ca i tng s nh trnh cc s kin ny cho cc gi ang c truyn ti thi im tng ng

8.Cc dch v trong ns.


Mc ny m t cc dch v c ngun gc t cc dch v ca Nortel trong ns. Cc dch v hoc Diffserv l cu trc IP QoS c s trong nhn gi m n cho php mc u tin ca cc gi theo yu cu ngi s dng. Trong thi gian tc nghn cc gi c mc u tin thp s b loi b nhiu hn cc gi c mc tin cao.
8.1. Tng quan

Cu trc Diffserv cung cp QoS bng vic phn chia lu lng thnh cc loi khc nhau, nh du mi gi vi mt m con tr nhn dng cc loi ny v nh trnh cc gi thng qua m con tr ca chng. Khi Diffserv trong ns hin ti nh ngha 4 lp lu lng , mi lp trong chng c th thuc 3 mc loi b khc nhau. Mi mc loi b cho php c th s l vi mt lp lu lng. Mt lp lu lng bao gm trong 3 lp hng i o (mt trong mi loi c loi b theo mc u tin) c xp trong hng tng ng vi lp vt l hng i RED. Cc c tnh khc nhau ca RED c s dng cho cc hnh i o, do cc gi t mt hng i o c th b loi b thng xuyn hn cc gi ca hng khc. Mt gi vi mc loi b thp hn s c u tin hn trong thi gian tc nghn bi v n c gn cho mt m con tr ph hp vi mt hng i o lin quan vi cc c tnh ca RED. Khi Diffserv trong ns bao gm 3 thnh phn chnh: policy: Mc dch v lp lu lng c thu trong mng c qun tr vin mng xc lp edge routers: nh du cc gi vi m con tr ty thuc vo policy c xc lp. core routers: Kim tra vic nh du m con tr v chuyn chng i tip mt cch ph hp Diffserv c gng hn ch s phc tp cho edge routers.
8.2 S thi hnh (Implementation)

Th tc v chc nng c m t trong mc ny c th tm thy trong a ch ~ns/diffserv/dsred, dsredq, dsEdge, dsCore, dsPolicy.{cc, h}. 8.2.1 Hng i RED cho Diffserv Trong ns, cc chc nng ca Diffserv nhn c trong phn t Queue m n thc hin nh l s la chn t cc loi hng i DropTail, CBQ, and RED. Mt hnh i Diffserv (dsREDQueue tha k t lp Queue c s, xem trong dsred.{h,cc}) bao gm cc kh nng sau: Bai tiu lun mn hoc IP-ng dung va bao mt 49

Gii thiu cng cu NS - thc hin nhiu hng i RED vt l trong cng mt tuyt kt ni n. - Thc hin nhiu cc hng i o trog mt hng i vt l vi vic thit lp cc c tnh ring cho m[ix hng i o. - Quyt nh mt gi trong tuyn vt l v hng i o no c xp vo hng, ty thuc policy c xc lp. Lp dsREDQueue bao gm 4 hng i RED vt l (c nh ngha nh numQueues_), mi hng li bao gm 3 hng i o ( c nh ngha nh numPrec) c lin quan n mc u tin. Mi mt hng i vt l tng ng vi mt lp lu lng v mi s kt hp ca mt hng i v mt s th t l s kt hp vi mt m con tr (hoc mt mc u tin loi b) xc nh mc ca hng i. Hng i RED vt l c nh ngha trong lp redQueue(xem dsredq.{h,cc}). Lp redQueue cho php s khc nhau v lu lng bng cch xc lp cc hng i RED o, mi hng i RED c c tnh cu hnh v trng thi ring bit. Chng hn, di ca mi hng i o ch c tnh ton bng cc gi c ghp vo trong hng i. Bi vy, vic quyt nh loi b gi c th c p dng trn c tnh cu hnh v trng thi ca hng i o. Lp redQueue l khng tng ng vi lp REDQueue c gii thiu trong ns. Thay vo n l mt phin bn ca lp REDQueue bao gm cc kha nim ca cc hng i o. Lp redQueue ch tn ti bn trong lp dsREDQueue. Tt c cc thao tc ca ngi sm dng vi lp redQueue c thc hin thng qua lnh giao tip ca lp dsREDQueue. Lp dsREDQueue bao gm cu trc d liu c bit nh bng PHB (qua cch chuyn bng). Thit b cnh nh giu cc gi vi m con tr v m thit b n gin ng vi m con tr tn ti. Tuy nhin, c 2 thit b cn c quyt nh ghp cc m con tr cho mt hnhg i c th v mc u tin. Bng PHB iu khin vic ghp ny bng vic xc nh mt hng vi 3 th mc: struct phbParam { int codePt_; // corresponding code point int queue_; // physical queue int prec_; // virtual queue (drop precedence) }; 9.2.2 Core v edge routers. Diffserv edge v core routers c nh ngha trong edgeQueue v lp coreQueue, c 2 u c tha k t lp dsREDQueue, xem dsEdge,dsCore.{h,cc}. 9.2.3 Policy Lp Policy (c nh ngha nh dsPolicy.{cc, h}) c s dng trong lp edgeQueue iu khin tt cc cc chc nng policy. Mt policy c thit lp gia mt ngun v ch n. Tt c cc lung c thit lp t ngun ti ch theo tng i c coi nh l ngun lu lng n. Mi mt polycy thit lp mt kiu chnh sch, mt tc mong mun, v cc c tnh nh hng ring. Ti thiu mi policy thit lp 2 m con tr v chn mt ty thuc vo vic so gia tc mong mun v tc tc gi hin ti. Mi mt ngun lu lng c mt kiu chnh sch kt hp, kiu o v m con tr ban u. Kiu o c trng cho phng php nh gi cc trng thi cn ca chnh sch. V d TSW Tagger l mt lai ng h dng nh gi tc lu lng trung bnh , s dng cc c tnh ca s thi gian. Khi mt gi i n mt edge router, n s c nh gi quyt nh xem n thuc vo ngun no. ng h c trng cho ngun n thit lp li tt c cc trng Bai tiu lun mn hoc IP-ng dung va bao mt 50

Gii thiu cng cu NS tha ca cc bin. Sau khi chnh sch c a ra quyt nh cch nh du gi, ty thuc vo trng thi cc bin ca ngun, c tnh ban u hoc c tnh gim cp ca m con tr s c s dng; v gi s c xp vo hng mt cch tng ng. Lp s Policy s dng Policy Table lu tr cc chnh sch cho mi ngun lu lng, Bng ny l mt cc dy bao gm cc flie cho ngun v im ch, loi chnh sch, loi ng h, m con tr ban u v cc loi thng tin trng thi khc. Lp s Policy s dng Policer Table lu tr quastrinhf xp xp t mt loi chnh sch v 2 m con tr ban u kt hp vi m con tr gim cp. Chi tit ca phn ny c m t trong dsPolicy.{cc, h} Hin ti c 6 kiu chnh sch khc nhau c nh ngha bao gm: 1. TSW2CM (TSW2CMPolicer): s dng mt CIR v 2 mc u tin. Mc u tin thp c kh nng c s dng khi m CIR vt ngng 2. TSW3CM (TSW3CMPolicer): s dng mt CIR, mt PIR, v 3 mc u tin . Mc u tin trung bnh c s dng khi CIR vt ngng v mc u tin thp nht c s dng khi PIR vt ngng. 3. Token Bucket (tokenBucketPolicer): s dng mt CIR v mt CBS cng 2 mc u tin . Mt gi n c nh du vi mc u tin thp nu v ch nu n ln hn token bucket. 4. Mt tc nh du 3 mu (srTCMPolicer): s dng mt CIR, CBS, v mt EBS chn chn t 3 mc u tin. 5. Hai tc nh du 3 mu (trTCMPolicer): s dng mt CIR, CBS, PIR v mt EBS chn chn t 3 mc u tin. Mt tham chiu ti lp PolicyClassifier c lp edgeQueue s dng a ra cc chnh sch khc nhau. Mi mt chnh sch c nh ngha l mt lp con v c tha k t lp dsPolicy.Phng php applyMeter v applyPolicer cn c xc nh cho mi chnh sch. Cc chnh sch do ngi s dng xc lp cng c th c a vo theo cc tng t. Xem chi tit trong dsPolicy.{cc, h}.
8.3 Cu hnh

S lng cc hng i vt l v hng i o c th c thit lp nh sau: $dsredq set numQueues_ 1 $dsredq setNumPrec 2 $dsredq configQ 0 1 10 20 0.10 V d trn xc nh c tnh RED cho mt hng i o (hng i vt l 0/ hng i o 1) $dsredq addPHBEntry 11 0 1 Lnh addPHBEntry a mt cng vo ti bng PHB. Trong v d ny, m con tr 11 c ghp vo hng i vt l 0 v hng i o 1. Trong ns, gi tr mc nh ca m con tr l 0, bi vy iu khin mt lu lng mc c th cn thit trn mt PHB u vo cho gi tr 0 ca m con tr. $dsredq meanPktSize 1500 Lnh ny thit lp kch c gi mean theo byte c s dng cho tnh ton di hng i RED. Thm na, lnh ny cho php ngi dng c kh nng la chn cc kiu nh trnh gia cc hng i.

Bai tiu lun mn hoc IP-ng dung va bao mt

51

Gii thiu cng cu NS

9. Agents
Cc agent ng vai tr cc im cui (endpoint) ni cc gi d liu lp mng c hnh thnh hoc s dng, chng thng c dng thc hin cc giao thc nhiu lp khc nhau. Lp Agent c thc hin mt phn bng ngn ng OTcl v mt phn bng C++. Phn thc hin bng C++ nm trong ~ns/agent.cc v ~ns/agent.h, cn phn thc hin bng ngn ng h tr OTcl nm trong ~ns/tcl/lib/ns-agent.tcl.
9.1. Trng thi Agent

Lp agent c trng thi bn trong c dng n nh nhiu trng khc nhau cho gi tin c m phng trc khi gi n i. Trng thi ny bao gm:
addr_ gi) dst_ size_ chung) type_ packet.h) fid_ prio_ flags_ defttl_ ni ti c gi n kch thc gi (t vo phn header ca gi dng gi (trong phn header chung, xem a ch nt ca chnh ti (a ch ngun trong

nh danh dng IP (class trc y trong ns-1) trng u tin IP cc c gi (tng t nh ns-1) gi tr ttl IP mc nh

Cc bin ny c th b thay i bi cc class xut t Agent.


9.2. Cc phng thc ca Agent

Lp Agent h tr vic to v nhn cc gi. Cc hm b phn sau y c thc hin nh lp Agent, lp ny thng khng b chim dng (override) bi cc lp dn xut:
Packet* allocpkt() cp gi mi v n nh trng ca n Packet* allocpkt(int) cp gi mi vi ti tin n bytes v n nh trng ca n.

Cc hm b phn sau y cng c nh ngha bi lp Agent nhng s b chim dng bi cc lp dn xut t Agent:
void timeout(timeout number) void recv(Packet*, Handler*) ca agent phng thc qu hn lp con nhn ng dn nhn chnh

Phng thc allocpkt() c dng nh cc lp dn xut to ra cc gi. Hm ny in vo tt c cc trng trong phn header gi chung (Section 12): uid, ptype, size v cc trng trong phn header IP: src, dst, flowid, prio, ttl. N cng in 0 vo cc trng ca header ca cc c (flag): ecn, pri, usr1, usr2. Thng tin header ca gi bt k khng nm trong danh sch ny th phi c bao hm trong lp dn xut t Agent. Phng thc recv() l im u vo chnh ca mt Agent, n nhn cc gi v lin h vi nt pha trn khi gi gi. Trong hu ht cc trng hp, cc Agent khng s dng i s th hai (iu khin c nh ngha bi nt pha trn).

Bai tiu lun mn hoc IP-ng dung va bao mt

52

Gii thiu cng cu NS


9.3. Protocol Agents

Sau y l mt s agent c h tr trong b m phng (tn ca chng trong OTcl): TCP TCP/Reno TCP/Newreno TCP/Sack1 TCP/Fack TCP/Vegas TCP/Vegas/RBP TCP/Vegas/RBP TCP/Asym TCP/Reno/Asym TCPSink TCPSink/DelAck TCPSink/Asym TCPSink/Sack1 UDP RTP RTCP IVS/Source IVS/Receiver CtrMcast/Encap CtrMcast/Decap Message SRM SRM/Adaptive Tahoe TCP sender (cwnd = 1 on any loss) Reno TCP sender (with fast recovery) sa i Reno TCP sender (khi phc nhanh s thay i) SACK TCP sender forward SACK sender TCP Vegas TCP sender Vegas TCP with rate based pacing Reno TCP with rate based pacing an experimental Tahoe TCP for asymmetric links an experimental Reno TCP for asymmetric links Reno or Tahoe TCP receiver (not used for FullTcp) TCP delayed-ACK receiver an experimental TCP sink for asymmetric links SACK TCP receiver basic UDP agent RTP sender and receiver RTCP sender and receiver IVS source IVS receiver centralised multicast encapsulator centralised multicast de-encapsulator protocol to carry textual messages SRM agent with non-adaptive timers SRM agent with adaptive timers

TCP/FullTcp more full-functioned TCP with 2-way traffic

TCP/Newreno/Asym an experimental Newreno TCP for asymmetric links

TCPSink/Sack1/DelAck a delayed-ACK SACK TCP receiver

LossMonitor packet sink which checks for losses

Message/Prune processes multicast routing prune messages

Tap interfaces the simulator to a live network Null a degenerate agent which discards packets rtProto/DV distance-vector routing protocol agent Bai tiu lun mn hoc IP-ng dung va bao mt 53

Gii thiu cng cu NS Cc Agent c dng thc hin cc giao thc nhiu lp khc nhau. V vy, i vi mt s giao thc vn chuyn (v d, UDP) vic phn phi kch thc gi v/hoc cc thi gian khi hnh c th c iu khin bi mt s i tng ring r c trng cho yu cu ca ng dng. Nh vy, cc agent biu hin l mt API i vi ng dng. i vi cc agent c s dng thc hin cc giao thc cc lp thp (v d, cc agent nh tuyn) th kch thc v nh thi i thng c iu khin bi vic x l bn tin giao thc ca chnh agent .
9.4. Lin kt trong OTcl

Cc Agent c th c to ra trong OTcl, trng thi bn trong ca agent c th thay i bng cch s dng hm set ca Tcl v cc hm Tcl ca Agent (hoc cc lp c s ca n). Ch rng, mt s trng thi bn trong ca Agent c th ch tn ti trong OTcl v nh vy khng th truy nhp trc tip t C++. 9.4.1 To v sa i cc Agent

V d sau y m t vic to v sa i mt Agent trong OTcl:


set newtcp [new Agent/TCP] ;# create new object (and C++ shadow object) $newtcp set window_ 20 ;# sets the tcp agents window to 20 $newtcp target Connector class $dest ;# target is implemented in

$newtcp set portID_ 1 ;# exists only in OTcl, not in C++

9.4.2. Cc gi tr mc nh Cc gi tr mc nh ca cc bin b phn, cc bin ny ch thy trong OTcl dng lin kt OTcl v C++ c khi to trong file ~ns/tcl/lib/ns-default.tcl. V d, Agent c khi to nh sau:
Agent set fid_ 0 Agent set prio_ 0 Agent set addr_ 0 Agent set dst_ 0 Agent set flags_ 0

Thng thng, vic khi to cc bin ny c thc hin trong OTcl trc khi cc i tng ca chng c to. Nh vy, khi mt i tng Agent c to, lnh lin kt cc i tng s dn n vic cc bin b phn tng ng c gn gi tr mc nh. 9.4.3. Cc phng thc trong OTcl Cc th tc c nh ngha cho lp Agent trong OTcl nm trong ~ns/tcl/lib/nsagent.tcl. Chng bao gm:
port dst-port attach-source<stype> nh danh cng ca agent nh danh cng ch to, gn mt i tng vo Agent

Bai tiu lun mn hoc IP-ng dung va bao mt

54

Gii thiu cng cu NS


9.5. V d: Cc agent Tcp, TCPSink

Lp TCP biu din dng n gin ca bn gi TCP. N gi d liu n agent TCPSink v x l cc xc nhn. N c mt i tng ring phi hp vi n th hin nhu cu ca ng dng. Vi vic xem xt cc lp TCPAgent v TCPSinkAgent, ta c th thy c rng cc agent c xy dng kh phc tp. Sau y l mt v d v Tahoe TCPAgent s dng cc b nh thi (timer). 9.5.1. To Agent on m OTcl sau y m t vic to TCP agent v thit lp cho n:
set tcp [new Agent/TCP] ;# create sender agent $tcp set fid_ 2 ;# set IP-layer flow ID set sink [new Agent/TCPSink] ;# create receiver agent $ns attach-agent $n0 $tcp ;# put sender on node $n0 $ns attach-agent $n3 $sink ;# put receiver on node $n3 $ns connect $tcp $sink ;# establish TCP connection set ftp [new Application/FTP] ;# create an FTP source "application" $ftp sender attach-agent $tcp ;# associate FTP with the TCP

$ns at 1.2 "$ftp start" ;#arrange for FTP to start at time 1.2 sec

Cu lnh OTcl new Agent/TCP to ra mt i tng TcpAgent trong C++. Hm khi to u tin gi hm khi to ca lp c s sau thc hin lin kt. Hai hm khi to bao gm: Hm khi to TcpSimpleAgent (~ns/tcp.cc):
TcpAgent::TcpAgent() : Agent(PT_TCP), rtt_active_(0), rtt_seq_(-1), rtx_timer_(this), delsnd_timer_(this) { bind("window_", &wnd_); bind("windowInit_", &wnd_init_); bind("windowOption_", &wnd_option_); bind("windowConstant_", &wnd_const_); ... bind("off_ip_", &off_ip_); bind("off_tcp_", &off_tcp_); ... }

Hm khi to Agent (~ns/agent.cc):


Agent::Agent(int pkttype): addr_(-1), dst_(-1), size_(0), type_(pkttype), fid_(-1), prio_(-1), flags_(0) { memset(pending_, 0, sizeof(pending_)); /* timers */ // this is really an IP agent, so set up // for generating the appropriate IP fields. . .

Bai tiu lun mn hoc IP-ng dung va bao mt

55

Gii thiu cng cu NS


bind("addr_", (int*)&addr_); bind("dst_", (int*)&dst_); bind("fid_", (int*)&fid_); bind("prio_", (int*)&prio_); bind("flags_", (int*)&flags_); ... }

on m ny m t trng hp chung khi m hm khi to Agent cho nh danh kiu gi vo hm khi to Agent. Cc gi tr kiu gi khc nhau c s dng bi cng c to vt gi (Section 22.5) v c nh ngha trong ~ns/trace.h. Cc bin b gii hn trong hm khi to TcpAgent l cc bin thng/b phn ca lp tr cc gi tr nguyn c bit off_tcp_ v off_ip_. Hai bin ny tng ng dng truy nhp vo header TCP v IP. Chi tit xem trong phn header ca gi (Section 12.1). Ch rng hm khi to TcpAgent bao gm vic khi to cho 2 b nh thi rtx_timer_ v delsnd_timer_. Cc i tng TimerHandler c khi to bng cch to ra mt con tr (pointer) n agent tng ng. 9.5.2. Khi ng Agent Agent TcpAgent c khi ng nh trong v d sau y khi ngun FTP nhn c lnh khi ng thi im 1.2. Qu trnh khi ng l mt th tc c nh ngha trong lp Application/FTP (Section 32.4). nh ngha trong ~ns/tcl/lib/nssource.tcl nh sau:
Application/FTP instproc start {} { [$self agent] send -1 }

Trong trng hp ny, agent tham chiu n agent TCP n gin v gi 1 l tn hiu tng t bo hiu s gi 1 file ln. V lnh gi lm bn gi TCP n gin to ra cc gi. u ra hm thc hin nh sau:
void TcpAgent::output(int seqno, int reason) { Packet* p = allocpkt(); hdr_tcp *tcph = (hdr_tcp*)p->access(off_tcp_); double now = Scheduler::instance().clock(); tcph->seqno() = seqno; tcph->ts() = now; tcph->reason() = reason; Connector::send(p, 0); ... if (!(rtx_timer_.status() == TIMER_PENDING)) /* No timer pending. Schedule one. */ set_rtx_timer(); }

Bai tiu lun mn hoc IP-ng dung va bao mt

56

Gii thiu cng cu NS y chng ta xem xt vic s dng phng thc Agent::allocpkt(). Tin trnh u ra u tin t mt gi mi (c header chung v IP c in), sau phi in vo trng header lp TCP. tm header trong mt gi (gi thit c php (Section 12.2.4)) th off_tcp_ phi c khi to xong, nh m t trong hm khi to. Phng thc packet access() tr v con tr n header TCP, cc trng s th t v du thi gian in, v phng thc send() ca lp Connector c gi gi gi xung lp di. Ch rng, ton t ngn cch :: c s dng trnh vic gi TcpSimpleAgent::send() ( c nh ngha). Vic kim tra nh thi s dng phng thc nh thi status() c nh ngha trong lp TimerHandler. N c s dng y thit lp nh thi truyn li nu n cha c thit lp (pha gi TCP ch thit lp mt b nh thi cho mi ca s trn mi kt ni). 9.5.3. X l u vo b thu Nhiu agent TCP c th dng lp TCPSink. Lp ny nh ngha cc phng thc recv() v ack() nh sau:
void TcpSink::recv(Packet* pkt, Handler*) { hdr_tcp *th = (hdr_tcp*)pkt->access(off_tcp_); acker_->update(th->seqno()); ack(pkt); Packet::free(pkt); } void TcpSink::ack(Packet* opkt) { Packet* npkt = allocpkt(); hdr_tcp *otcp = (hdr_tcp*)opkt->access(off_tcp_); hdr_tcp *ntcp = (hdr_tcp*)npkt->access(off_tcp_); ntcp->seqno() = acker_->Seqno(); ntcp->ts() = otcp->ts(); hdr_ip* oip = (hdr_ip*)opkt->access(off_ip_); hdr_ip* nip = (hdr_ip*)npkt->access(off_ip_); nip->flowid() = oip->flowid(); hdr_flags* of = (hdr_flags*)opkt>access(off_flags_); hdr_flags* nf = (hdr_flags*)npkt>access(off_flags_); nf->ecn_ = of->ecn_; acker_->append_ack((hdr_cmn*)npkt->access(off_cmn_), ntcp, otcp->seqno()); send(npkt, 0); }

Phng thc recv() chim dng phng thc Agent::recv() (n gin l loi b cc gi nhn). N cp nht mt s trng thi ni vi s th t ca gi nhn (v nh vy yu cu bin off_tcp_ c khi to). Sau , n to ra mt xc nhn cho gi nhn c. Phng thc ack() c s dng truy nhp cc trng header gi c 2 chiu bao gm v truy nhp ring r n header TCP, header IP, header Flags v header chung. Lnh send() thc cht l gi phng thc Connector::send().

Bai tiu lun mn hoc IP-ng dung va bao mt

57

Gii thiu cng cu NS 9.5.4. X l p ng bn gi Khi mt pha TCP n gin nhn d liu v to ra ACK, pha gi thng phi x l ACK. Trong agent TcpAgent agent, qu trnh ny c thc hin nh sau:
/* * main reception path - should only see acks, otherwise the * network connections are misconfigured */ void TcpAgent::recv(Packet *pkt, Handler*) { hdr_tcp *tcph = (hdr_tcp*)pkt->access(off_tcp_); hdr_ip* iph = (hdr_ip*)pkt->access(off_ip_); ... if (((hdr_flags*)pkt->access(off_flags_))->ecn_) quench(1); if (tcph->seqno() > last_ack_) { newack(pkt); opencwnd(); } else if (tcph->seqno() == last_ack_) { if (++dupacks_ == NUMDUPACKS) { ... } } Packet::free(pkt); send(0, 0, maxburst_); }

Qu trnh ny c thc hin khi ACK n pha gi. Trong trng hp ny, thng tin trong ACK c x l, gi khng cn na v tr v cho b phnchia b nh cho gi. ng thi, vic nhn c CK cho thy kh nng gi gi b sung, nh vy phng thc TcpSimpleAgent::send() s c gng gi thm d liu nu ca s TCP cho php. 9.5.5. Cc b nh thi Nh c m t trong chng sau (Chapter 11), cc lp nh thi phi c dn xut t lp c s TimerHandler nh ngha trong ~ns/timer-handler.h. Cc lp con ny c th c s dng nh cc agent nh thi. Mt agent c th chim dng phng thc Agent::timeout(). Trong trng hp agent Tahoe TCP, s dng 2 b nh thi: b nh thi gi tr delsnd_timer_ v b nh thi truyn li rtx_timer_. Ta m t mt v d s dng b nh thi truyn li trong TCP (Section 11.1.2). Bai tiu lun mn hoc IP-ng dung va bao mt 58

Gii thiu cng cu NS


9.6. To mt agent mi

to mt agent mi, ta thc hin nh sau: 1. quyt nh cu trc k tha ca n (Section 10.6.1), v to cc nh ngha lp thch hp, 2. nh ngha cc phng thc recv() v timeout() (Section 10.6.2), 3. nh ngha cc lp nh thi cn thit, 4. nh ngha cc hm lin kt trong OTcl (Section 10.6.3), 5. vit m OTcl cn thit truy nhp agent (Section 10.6.4). 9.6.1. V d: Yu cu ping (Cu trc k tha) Vic quyt nh chn cu trc k tha l mt vn c nhn nhng c v lin quan n lp ti agent hot ng, gi thit lp pha di hot ng. Dng n gin nht ca agent, chuyn datagram khng kt ni, l lp c s Agent/UDP. B to lu lng c th d dng kt ni ti cc agent UDP. i vi cc giao thc mun s dng vic chuyn dng d liu c kt ni (nh TCP), phi s dng cc agent TCP khc. Cui cng, nu mt giao thc vn chuyn mi hoc vn chuyn con c pht trin, th vic s dng agent lm lp c s c v l la chn tt nht. Trong v d sau y, ta dng agent lm lp c s, vi gi thit rng ta xy dng mt agent thuc v lp IP (hoc ngay phia strn n). Ta c th s dng cc nh ngha lp sau y:
class ECHO_Timer; class ECHO_Agent : public Agent { public: ECHO_Agent(); int command(int argc, const char*const* argv); protected: void timeout(int); void sendit(); double interval_; ECHO_Timer echo_timer_; }; class ECHO_Timer : public TimerHandler { public: ECHO_Timer(ECHO_Agent a; } protected: virtual void expire(Event *e); ECHO_Agent *a_; }; *a) : TimerHandler() {a_ =

Bai tiu lun mn hoc IP-ng dung va bao mt

59

Gii thiu cng cu NS 9.6.2. Cc phng thc recv() v timeout() Phng thc recv() khng c nh ngha y, bi v n biu din mt hm yu cu v n s khng nhn cc s kin hay packets1. Do phng thc recv() khng c nh ngha nn phi s dng bn lp c s (Connector::recv()). Phng thc timeout() c s dng nh k gi cc gi yu cu. Phng thc timeout() c s dng cng vi sendit():
void ECHO_Agent::timeout(int) { sendit(); echo_timer_.resched(interval_); } void ECHO_Agent::sendit() { Packet* p = allocpkt(); ECHOHeader *eh = ECHOHeader::access(p->bits()); eh->timestamp() = Scheduler::instance().clock(); send(p, 0); // Connector::send() } void ECHO_Timer::expire(Event *e) { a_->timeout(0); }

Phng thc timeout() c tnh ton sendit() hot ng sau mi interval_ seconds. Phng thc sendit() to ra mt gi mi vi cc trng header ca gi c thit lp bi allocpkt(). Gi khng c du thi gian hin thi. Cuc gi n access() to ra mt giao din c cu trc cho cc trng header ca gi v n c s dng thit lp trng du thi gian. Ch rng agent ny s dng header c bit cu chnh n (ECHOHeader). Vic to v s dng cc header gi c m t trong chng sau (Chapter 12); gi gi n nt ngay pha di, tin hnh gi Connector::send() khng cn iu khin (handler). 9.6.3. Lin kt agent ping vi OTcl Ta c cc phng thc v c ch thit lp lin kt trong OTcl (Chapter 3). Phn ny tm li cc c tnh c bn ca chng trc, v m t cc chc nng ti thiu to agent ping. C 3 hng mc ta phi iu khin lin kt agent vi Otcl. u tin ta cn phi thit lp mt nh x gia tn OTcl cho lp v i tng tht c to ra khi lp oc yu cu trong OTcl. Vic ny c thc hin nh sau:
static class ECHOClass : public TclClass { public: ECHOClass() : TclClass("Agent/ECHO") {}

Bai tiu lun mn hoc IP-ng dung va bao mt

60

Gii thiu cng cu NS


TclObject* create(int argc, const char*const* argv) { return (new ECHO_Agent()); } } class_echo;

y, mt i tng tnh class_echo c to ra. Hm khi to ca n (c thc hin ngay khi b m phng chy) t tn lp Agent/ECHO vo khng gian tn OTcl. nh ngha ca phng thc create() quy nh cch i tng bng C++ c to ra khi trnh bin dch OTcl c chy to ra mt i tng ca lp Agent/ECHO. Trong trng hp ny, n tr v mt i tng c cp pht ng. y l cch thng thng to ra mt i bng C++ mi. Khi c mt i tng mi, ta mun lin kt cc bin b phn C++ vi cc bin tng ng trong khng gian tn OTcl, nh vy vic truy nhp cc bin OTcl thc t l cc bin b phn trong C++. Gi thit ta mun OTcl iu chnh khong thi gian gi v kch thc gi. Vic ny c hon thnh trong hm khi to lp:
ECHO_Agent::ECHO_Agent() : Agent(PT_ECHO) { bind_time("interval_", &interval_); bind("packetSize_", &size_); }

y, cc bin C++ interval_ v size_ c lin kt vi cc bin OTcl tng ng interval_ v packetSize_. Bt c thao thc c hay thay i i vi cc bin OTcl no cng s to ra trong mt truy nhp tng ng n cc bin C++. Chi tit ca cc phng thc bind() c m t trn (Section 3.4.2). Hng PT_ECHO b hm khi to Agent() b qua nn phng thc Agent::allocpkt() c th thit lp trng kiu gi (Section 22.5). Trong trng hp ny, PT_ECHO biu din mt kiu gi mi v phi nh ngha trong ~ns/trace.h (Section 22.4). Khi i tng c to ra v vic lin kt bin c thc hin, ta c th mun to ra cc phng thc thc hin trong C++ m c th gi t OTcl (Section 3.4.4). Cc phng thc ny iu khin cc chc nng khi to, hu hoc thay i. Trong v d ny, ta c th khi ng agent t OTcl s dng lnh start. Thc hin nh sau:
int ECHO_Agent::command(int argc, const char*const* argv) { if (argc == 2) { if (strcmp(argv[1], "start") == 0) { timeout(0); return (TCL_OK); } } return (Agent::command(argc, argv));

Bai tiu lun mn hoc IP-ng dung va bao mt

61

Gii thiu cng cu NS


}

y, phng thc start() sn sng OTcl gi hm b phn C++ timeout(), hm ny khi to vic to gi u tin v nh trnh gi tip theo. Ch rng lp ny rt n gin, n thm ch khng c cch dng li. 9.6.4. S dng agent nh OTcl Agent ta to ra phi c gn vi mt nt. Ch rng nt v i tng m phng c gi thit l c to ri. on m OTcl sau y biu din cc chc nng ny:
set echoagent [new Agent/ECHO] $simulator attach-agent $node $echoagent

thit lp khong thi gian v kch thc gi, v bt u to gi, thc hin on m sau:
$echoagent set dst_ $dest $echoagent set fid_ 0 $echoagent set prio_ 0 $echoagent set flags_ 0 $echoagent set interval_ 1.5 $echoagent set packetSize_ 1024 $echoagent start

Nh vy agent s to ra 1 gi 1024-byte chuyn n nt $dest sau mi 1.5s.


9.7. Agent API

Cc ng dng c m phng c th c thc hin trong phn u ca cc agent giao thc. Chng 32 m t API c cc ng dng s dng truy nhp cc dch v c agent giao thc cung cp.
9.8. Cc i tng agent khac

Lp agent to nn lp c s t nhiu loi i tng khc nhau nh Nullobject, TCP... c dn xut. Cc phng thc ca lp agent c m t trong phn sau. Cc tham s cu hnh: fid_ Flowid. prio_ Priority. agent_addr_ Address of this agent. agent_port_ Port adress of this agent. dst_addr_ Destination address for the agent. dst_port_ Destination port address for the agent. flags_ ttl_ TTL defaults to 32.

Bai tiu lun mn hoc IP-ng dung va bao mt

62

Gii thiu cng cu NS


9.9. Cc lnh c bn

Sau y l cc lnh lin quan n agent s dng trong kch bn m phng:


ns_ attach-agent <node> <agent> $agent port $agent dst-port $agent attach-app <s_type> $agent attach-source <s_type> $agent attach-tbf <tbf> $ns_ connect <src> <dst> $ns_ create-connection <pktclass> <srctype> <src> <dsttype> <dst>

$ns_ create-connection-list <srctype> <src> <dsttype> <dst> <pktclass> $ns_ simplex-connect <src> <dst> $agent set <args> $agent attach-trace <file>

10. Cc b nh thi
Cc b nh thi c th c thc hin bng C++ hoc OTcl. Trong C++, cc b nh thi da trn mt lp c s c nh ngha trong ~ns/timer-handler.h. Cc b nh thi thng c s dng trong cc agent, nhng chng cng c th c s dng bi mt s i tng khc. Phn sau y s m t vic s dng cc b nh thi trong cc agent. Cc th tc v hm trnh by trong phn ny c th ly trong ~ns/tcl/ex/timer.tcl, v ~ns/timer-handler.{cc, h}. Trong OTcl, lp nh thi n gin c nh ngha trong ~ns/tcl/ex/timer.tcl. Cc lp con c th c dn xut to ra mt c ch n gin nh trnh cc s kin mc ca OTcl.
10.1. Lp c s trong C++ TimerHandler

Lp c s TimerHandler bao gm cc hm b phn chung nh sau:


void sched(double delay) schedule a timer to expire delay seconds in the future void resched(double delay) reschedule a timer (similar to sched(), but timer may be pending) void cancel() cancel a pending timer int status() TIMER_PENDING, or returns timer status (either TIMER_IDLE,

TIMER_HANDLING)

Lp c s TimerHandler bao gm cc b phn c bo v nh sau:


virtual void expire(Event* filled in by the timer client e) =0 this method must be

Bai tiu lun mn hoc IP-ng dung va bao mt

63

Gii thiu cng cu NS


virtual void handle(Event* e) consumes an event; invokes expire() and sets status_ of the timer appropriately int status_ keeps track of the current timer status Event event_ event to be consumed upon timer expiration

Hm o expire() c nh ngha bng cc lp nh thi dn xut t lp c s ny. Sau cng, hai hm ni tuyn ring c nh ngha:
inline void _sched(double delay) { (void)Scheduler::instance().schedule(this,&event_, delay); } inline void _cancel() { (void)Scheduler::instance().cancel(&event_); }

T on m ny ta c th thy rng cc b nh thi s dng cc phng thc ca lp Scheduler. 10.1.1. nh ngha mt b nh thi mi nh ngha mt b nh thi mi, tin hnh phn lp hm ny v nh ngha hm handle() nu cn (thng khng yu cu hm handle()):
class MyTimer : public TimerHandler { public: MyTimer(MyAgentClass a; } virtual double expire(Event *e); protected: MyAgentClass *a_; }; *a) : TimerHandler() { a_ =

Sau nh ngha hiu lc:


double MyTimer::expire(Event *e) { // do the work // timer // return delay; // => reschedule timer after delay } return TIMER_HANDLED; // => do not reschedule

Ch rng expire() c th tr v c TIMER_HANDLED hoc mt gi tr tr, tu theo yu cu i vi b nh thi.

Bai tiu lun mn hoc IP-ng dung va bao mt

64

Gii thiu cng cu NS MyTimer s trong cng nhm vi MyAgentClass, hoc expire() s ch gi n hm chung ca MyAgentClass. Cc b nh thi khng th c truy cp trc tip t mc OTcl, mc d ngi dng c t do thit lp phng thc kt ni. 10.1.2. V d: B nh thi truyn li TCP TCP l mt v d v agent cn cc b nh thi. C 3 b nh thi c nh ngha trong agent c bn Tahoe TCP trong tcp.cc:
rtx_timer_; /* Retransmission timer */ delsnd_timer_; /* Delays sending of packets by a small random amount of time, */ /* to avoid phase effects */ burstsnd_timer_; /* Helps TCP to stagger the transmission of a large window */ /* into several smaller bursts */

Trong ~ns/tcp.h, ba lp c dn xut t lp c s TimerHandler:


class RtxTimer : public TimerHandler { public: RtxTimer(TcpAgent *a) : TimerHandler() { a_ = a; } protected: virtual void expire(Event *e); TcpAgent *a_; }; class DelSndTimer : public TimerHandler { public: DelSndTimer(TcpAgent a; } protected: virtual void expire(Event *e); TcpAgent *a_; }; class BurstSndTimer : public TimerHandler { public: BurstSndTimer(TcpAgent *a):TimerHandler(){ a_ = a; } protected: virtual void expire(Event *e); TcpAgent *a_; }; *a) : TimerHandler() { a_ =

Bai tiu lun mn hoc IP-ng dung va bao mt

65

Gii thiu cng cu NS Trong hm khi to cho TcpAgent trong tcp.cc, mi b nh thi ny c khi to vi mt con tr ca n, c n nh n con tr a_.
TcpAgent::TcpAgent() rtt_seq_(-1),... rtx_timer_(this), burstsnd_timer_(this) { ... } : Agent(PT_TCP), rtt_active_(0),

delsnd_timer_(this),

Sau y, ta ch tp trung vo b nh thi truyn li (retransmission timer). Cc phng thc tr gip khc nhau c th c nh ngha cho cc s kin nh thi, v d:
/* * Set retransmit timer using current rtt estimate. By calling resched() * it running. */ void TcpAgent::set_rtx_timer() { rtx_timer_.resched(rtt_timeout()); } /* * Set new retransmission timer if not all outstanding * data has been acked. Otherwise, if a timer is still * outstanding, cancel it. */ void TcpAgent::newtimer(Packet* pkt) { hdr_tcp *tcph = (hdr_tcp*)pkt->access(off_tcp_); if (t_seqno_ > tcph->seqno()) set_rtx_timer(); else if (rtx_timer_.status() == TIMER_PENDING) rtx_timer_.cancel(); } does not matter whether the timer was already

Trong on m trn, phng thc set_rtx_timer() thit lp nh thi li cho b nh thi truyn li bng cch gi rtx_timer_.resched(). Ch rng, nu khng r b nh thi c chy hay khng th vic gi resched() s hu b nh thi. Trong hm th hai, v d s dng cc phng thc status() v cancel(void). Bai tiu lun mn hoc IP-ng dung va bao mt 66

Gii thiu cng cu NS Cui cng, phi nh ngha phng thc expire(void) cho lp RtxTimer. Trong trng hp ny, expire(void) gi phng thc timeout(void) i vi TcpAgent. C th xy ra iu ny bi v timeout() l mt hm b phn chung; nu khng xy ra th RtxTimer phi c khai bo l cng lp ca TcpAgent.
void TcpAgent::timeout(int tno) { /* retransmit timer */ if (tno == TCP_TIMER_RTX) { if (highest_ack_ slow_start_restart_) { /* * TCP option: * If no outstanding data, then dont do anything. */ return; }; recover_ = maxseq_; recover_cause_ = 2; closecwnd(0); reset_rtx_timer(0,1); send_much(0, TCP_REASON_TIMEOUT, maxburst_); } else { /* * delayed-send timer, with random overhead * to avoid phase effects */ send_much(1, TCP_REASON_TIMEOUT, maxburst_); } } void RtxTimer::expire(Event *e) { a_->timeout(TCP_TIMER_RTX); } 10.2. Lp nh thi trong OTcl == maxseq_ && !

Mt lp nh thi n gin c nh ngha trong ~ns/tcl/mcast/timer.tcl. Cc lp con ca ca Timer c th c nh ngha nu cn. Khng nh cc hm API nh thi trong C++ (hm sched() b b qua nu b nh thi c thit lp), cc hm sched() v resched() l nh nhau; c ngha l trng thi ca cc b nh thi OTcl khng c gi. Trong lp c s Timer, c nh ngha cc phng thc sau y: Bai tiu lun mn hoc IP-ng dung va bao mt 67

Gii thiu cng cu NS


$self sched $delay ;# causes "$self timeout" to be called $delay seconds in the future $self resched $delay ;# same as "$self sched $delay" $self cancel ;# cancels any pending scheduled callback $self destroy ;# same as "$self cancel" $self expire ;# calls "$self timeout" immediately 10.3. Mt s lnh c bn $timer sched <delay> $timer resched <delay> $timer cancel $timer destroy $timer expire

11. Header gi v cc nh dng


Cc th tc v hm m t trong chng ny c th tm trong ~ns/tcl/lib/ns-lib.tcl, ~ns/tcl/lib/ns-packet.tcl v ~ns/packet.{cc, h}. Cc i tng trong lp Packet l n v c bn ca vic chuyn i gia cc i tng khi m phng. Lp Packet cung cp thng tin lin kt mt gi vo danh mc (c ngha l, trong mt PacketQueue hoc mt danh mc gi trng), tham chiu n mt b m cha cc header gi c nh ngha trn c s mi giao thc v tham chiu n b m ca d liu gi. Cc giao thc mi cps th nh ngha cc header gi ca chnh n hoc c th m rng cc header hin c bng cc trng b sung. Cc header gi mi c a vo b m phng bng cch nh ngha mt cu trc C++ c cc trng cn thit, nh ngha mt lp tnh to lin kt OTcl v sau thay i mt s m khi to b m phng n nh mt byte offset trong mi gi ni m header mi d nh c t. Khi b m phng c khi to nh OTcl, ngi dng c th chn ch cho php mt tp con ca cc nh dng gi, nh th s tit kim c b nh khi thc hin m phng. Hin nay, hu ht cc nh dng gi u c cho php. Vic qun l cc nh dng gi c cho php trong b m phng c iu khin bng mt i tng qun l head gi c bit nh m t di y. i tng ny h tr mt phng thc OTcl c s dng quy nh cc header gi c dng trong khi m phng. Nu mt i tng trong b m phng s dng mt trng trong 1 header khng c cho php, th chng trnh s ngng li.
11.1. Header gi cho mt giao thc c th

Cc nh pht trin giao thc thng mun to mt kiu header s dng trong cc gi. iu cho php thc hin mt giao thc mi m khng chng cho ln cc trng header c. Ta xem xt bn n gin ca giao thc RTP. Header RTP yu cu cc trng s th t v mt trng nh danh ngun. Cc lp sau y to ra header cn thit (xem trong ~ns/rtp.h v ~ns/rtp.cc): Trong file rtp.h: Bai tiu lun mn hoc IP-ng dung va bao mt 68

Gii thiu cng cu NS


/* rtp packet. For now, just have srcid + seqno. */ struct hdr_rtp { u_int32_t srcid_; int seqno_; /* per-field member functions */ u_int32_t& srcid() { return (srcid_); } int& seqno() { return (seqno_); } /* Packet header access functions */ static int offset_; inline static int& offset() { return offset_; } inline static hdr_rtp* access(const Packet* p) { return (hdr_rtp*) p->access(offset_); } };

Trong file rtp.cc:


class RTPHeaderClass : public PacketHeaderClass { public: RTPHeaderClass() PacketHeaderClass("PacketHeader/RTP", sizeof(hdr_rtp)) { bind_offset(&hdr_rtp::offset_); } } class_rtphdr; void RTPAgent::sendpkt() { Packet* p = allocpkt(); hdr_rtp *rh = hdr_rtp::access(p); lastpkttime_ = Scheduler::instance().clock(); /* Fill in srcid_ and seqno */ rh->seqno() = seqno_++; rh->srcid() = session_->srcid(); target_->recv(p, 0); } RTPAgent::RTPAgent() : session_(0), lastpkttime_(-1e6) { type_ = PT_RTP; :

Bai tiu lun mn hoc IP-ng dung va bao mt

69

Gii thiu cng cu NS


bind("seqno_", &seqno_); }

Cu trc u tin, hdr_rtp, nh ngha cch b tr header gi ca RTP (v khi nim v ch ng ca n): cc trng cn thit v kch thc ca chng. nh ngha cu trc ny ch c trnh bin dch s dng tnh byte offset ca trng; khng c cc i tng ca cu trc ny c nh v trc tip. Cu trc ny cng to ra cc hm b phn to ra mt lp che du cc cc d liu i vi cc i tng mun c hoc thay i trng header ca cc gi. Ch rng cc bin lp tnh offset_ c dng tm cc byte offset ti header rtp c t trong mt nspacket. C 2 phng thc s dng bin ny truy nhp header ny trong gi bt k: offset() v access(). Phng thc trc c hu ht ngi dng la chn truy nhp header c th ny trong mt gi; phng thc sau c s dng bi lp qun l header gi nhng cng t khi dng. V d, truy nhp header gi RTP trong mt gi c tr bi p, ta ch cn s dng hdr_rtp::access(p). Lin kt thc ca offset_ n v tr ca header ny trong gi c thc hin trong ~ns/tcl/lib/ns-packet.tcl v ~ns/packet.cc. Hng s trong lnh access() to mt truy nhp read-only n hng Packet, mc d thuc tnh read-only b bt buc do con tr tr v khng phi l hng s. Mt cch chnh xc lm iu ny l to ra 2 phng thc, mt phng thc truy nhp ghi, mt phng thc truy nhp readonly. Tuy nhin, hin nay cha thc hin iu ny. i tng tnh class_rtphdr ca lp RTPHeaderClass c dng lin kt n OTcl khi header RTP c cho php ti thi im cu hnh. Khi b m phng chy, i tng tnh ny gi hm khi to PacketHeaderClass vi lnh "PacketHeader/RTP" v sizeof(hdr_rtp). Nh vy, kch thc ca header RTP c lu li v sn sng phc v i tng qun l header gi ti thi im cu hnh (xem Section 12.2.4). Ch rng bind_offset() phi c gi trong hm khi to ca lp ny, nh vy i tng qun l header gi bit c ni lu gi offset ca header gi ny. Phng thc sendpkt() ca hm b phn v d ca RTPAgent to ra mt gi mi gi i bng cch gi allocpkt(), n iu khin vic n nh tt c cc trng header gi lp mng (trong trng hp ny l IP). Cc header khc ngoi IP c iu khin ring. Trong trng hp ny, agent s dng RTPHeader nh nh ngha trn. Hm b phn Packet::access(void) tr v a ch ca byte u tin trong b m c dng mang thng tin header. Gi tr tr v ca n l con tr n header quan tm, sau s dng hm b phn ca i tng RTPHeader truy nhp cc trng ring. 11.1.1. Thm mt kiu header gi mi Gi thit ta mun to mt header mi l newhdr, thc hin cc bc sau y: 1. to mt cu trc mi nh ngha cc trng (gi l hdr_newhdr), nh ngha offset_ v cc phng thc truy nhp. 2. nh ngha cc hm b phn cho cc trng cn thit. 3. to mt lp tnh thc hin lin kt vi OTcl (nh ngha PacketHeader/Newhdr), thc hin bind_offset() trong hm khi to ca n. 4. sa ~ns/tcl/lib/ns-packet.tcl cho php nh dng gi mi (xem 12.2.2, 12.2.4).

Bai tiu lun mn hoc IP-ng dung va bao mt

70

Gii thiu cng cu NS 11.1.2. La chn cc header gi trong b m phng Mc nh ns bao gm tt c cc header gi ca tt c cc giao thc trong ns tt c cc gi trong b m phng. Nh vy c rt nhiu overhead, v n s tng theo s giao thc c thm vo ns. Vi cc m phng gi nhy cm, overhead s rt ln. V d, hin nay (2000), kch thc head gi ca tt c cc giao thc trong ns khong 1,9 Kb; tuy nhin, nu bn ch dng heder gi chung, header IP v header TCP th ch mt vo khong 100 Bytes. Nu bn thc hin m phng lu lng web c ln c nhiu ng dn ln th vic gim header gi khng s dng s gim c phn ln b nh. tnh header gi ta quan tm trong m phng, ta lm nh sau (v d bn mun loi cc header AODV v ARP khi m phng):
remove-packet-header AODV ARP ...... set ns [new Simulator]

Ch rng remove-packet-header phi c thc hin trc khi b m phn c to ra. Tt c cc tn header gi nm trong PacketHeader/[hdr]. Bn ch cn cung cp phn [hdr]. tm tn ca cc header gi, bn c th xem trong ~ns/tcl/lib/nspacket.tcl, hoc chy lnh sau trong ns:
foreach cl [PacketHeader info subclass] { puts $cl }

tnh mt tp hp cc header trong m phng, v d IP v TCP, lm nh sau:


remove-all-packet-headers add-packet-header IP TCP ...... set ns [new Simulator]

Quang trng: Bn khng bao gi c loi header chung khi m phng. Bn c th thy trong ~ns/tcl/lib/ns-packet.tcl.
11.2. Cc lp gi

C 4 lp gi C++ tng ng iu khin gi v cc header gi: Packet, p_info PacketHeader v PacketHeaderManager. Lp Packet nh ngha kiu ca tt c cc gi trong m phng; n l mt lp con ca Event nn cc gi c th c nh trnh (v d n sau vi hng). Lp packet_info gi tt c cc biu din vn bn cho tn gi. Lp PacketHeader cung cp mt lp c s cho header gi bt k c cu hnh cho m phng. N cung cp trng thi ni cn thit nh v header gi bt k trong tp hp header gi. Lp PacketHeaderManager nh ngha mt lp c dng thu thp v qun l cc gi c cu hnh. N c gi bng mt phng thc OTcl ti thi im cu hnh m phng cho php mt tp con ca header gi bin dch. 11.2.1. Lp Packet Lp Packet nh ngha cu trc ca gi v cung cp hm b phn iu khin danh sch i tng trng. N c nh ngha trong packet.h nh sau: Bai tiu lun mn hoc IP-ng dung va bao mt 71

Gii thiu cng cu NS


class Packet : public Event { private: friend class PacketQueue; u_char* bits_; u_char* data_; /* variable size buffer for data */ u_int datalen_; /* length of variable size buffer */ protected: static Packet* free_; public: Packet* next_; /* for queues and the free list */ static int hdrlen_; Packet() : bits_(0), datalen_(0), next_(0) {} u_char* const bits() { return (bits_); } Packet* copy() const; static Packet* alloc(); static Packet* alloc(int); inline void allocdata(int); static void free(Packet*); inline u_char* access(int off) { if (off < 0) abort(); return (&bits_[off]); } inline u_char* accessdata() { return data_; } };

Lp ny gi mt con tr n mt mng k t cha c nh du (thng c gi l ti bit hoc BOB) ti lu cc trng header gi. N cng gi mt con tr ti gi d liu (thng khng c s dng trong m phng). Bin bits_ cha a ch ca byte u tin ca BOB. BOB l mt dy gm tt c cc cu trc c nh ngha cho tng header gi (theo tho thun, cc cu trc c tn bt u bng hdr_) c cu hnh. BOB thng c kch thc xc nh trong ton b m phng v kch thc ny c ghi trong mt bin b phn Packet::hdrlen_. Thch thc ny c cp nht khi cu hnh b m phng bng OTcl. Cc phng thc khc ca lp Packet dng to ra cc gi mi v lu tr cc gi c (khng s dng) trong mt danh sch ring. Vic nh v v gii to c thc hin bng on m sau (trong ~ns/packet.h):
inline Packet* Packet::alloc() { Packet* p = free_; if (p != 0)

Bai tiu lun mn hoc IP-ng dung va bao mt

72

Gii thiu cng cu NS


free_ = p->next_; else { p = new Packet; p->bits_ = new u_char[hdrsize_]; if (p == 0 || p->bits_ == 0) abort(); } return (p); } /* allocate a packet with an n byte data buffer */ inline Packet* Packet::alloc(int n) { Packet* p = alloc(); if (n > 0) p->allocdata(n); return (p); } /* allocate an n byte data buffer to an existing packet */ inline void Packet::allocdata(int n) { datalen_ = n; data_ = new u_char[n]; if (data_ == 0) abort(); } inline void Packet::free(Packet* p) { p->next_ = free_; free_ = p; if (p->datalen_) { delete p->data_; p->datalen_ = 0; } } inline Packet* Packet::copy() const { Packet* p = alloc(); memcpy(p->bits(), bits_, hdrlen_); if (datalen_) { p->datalen_ = datalen_; p->data_ = new u_char[datalen_]; memcpy(p->data_, data_, datalen_); } return (p); }

Phng thc alloc() l mt hm h tr c dng to cc gi mi. N c gi bng phng thc Agent::allocpkt() thay mt cho agent v nh vy n khng c cc i tng gi trc tip. u tin n c gng nh v mt gi c vo danh sch trng v nu khng c n nh v mt gi mi bng ton t new trong C++. Ch rng cc i tng lp Packet v cc BOB c nh v ring r. Phng thc free() gii phng gi bng cch chuyn n vo danh sch trng. Ch rng cc gi khng c tr li Bai tiu lun mn hoc IP-ng dung va bao mt 73

Gii thiu cng cu NS cho b cp pht nh ca h thng. Thay vo , chng c lu trn mt danh sch trng khi Packet::free() c gi. Hm b phn copy() to ra mt bn mi ca gi vi khc bit trng uid_. Hm ny c dng bi cc i tng Replicator h tr phn tn a lng v cc mng LAN. 11.2.2. Lp p_info Lp ny c gi l keo dn dng kt nicc giatr kiu gi dng s vi tn tng trng ca chng. Khi mt kiu gi mi c nh ngha, m dng s ca n cn uc thm vo bng packet_t (xem ~ns/packet.h) v tn tng trng ca n cn c thm bo hm khi to ca p_info:
enum packet_t { PT_TCP, ... PT_NTYPE // This MUST be the LAST one }; class p_info public: p_info() { name_[PT_TCP]= "tcp"; ... } }

11.2.3. Lp hdr_cmn Cc gi trong m phng c mt header chung c nh ngha trong ~ns/packet.h nh sau:
struct hdr_cmn { double ts_; /* timestamp: for q-delay measurement */ packet_t ptype_; /* packet type (see above) */ int uid_; /* unique id */ int size_; /* simulated packet size */ int iface_; /* receiving interface (label) */ /* Packet header access functions */ static int offset_; inline static int& offset() { return offset_; } inline static hdr_cmn* access(Packet* p) { return (hdr_cmn*) p->access(offset_); } /* per-field member functions */ int& ptype() { return (ptype_); }

Bai tiu lun mn hoc IP-ng dung va bao mt

74

Gii thiu cng cu NS


int& uid() { return (uid_); } int& size() { return (size_); } int& iface() { return (iface_); } double& timestamp() { return (ts_); } };

Cu trc ny nh ngha cc trng c dng ghi li dng gi hoc o cc i lng khc. Trng du thi gian c dng o tr hng i ti mt nt chuyn mch. Trng ptype_ c dng nh danh kiu gi. Trng uid_ c dng bi b nh trnh nh trnh thi gian gi n. Trng size_ c s dng chung, cung cp kch thc ca gi tnh bng byte. Ch rng mt s byte trong m phng c th khng lin quan n gi tr ca trng (c ngha l size_ khng c mi quan h vi sizeof(struct hdr_cmn) hoc cc cu trc khc ca ns). Hn na, n thng c dng tnh thi gian chuyn gi trn mng. 11.2.4. Lp PacketHeaderManager Mt i tng ca lp PacketHeaderManager c dng qun l tp hp cc kiu header gi v n nh cho chng mt offset duy nht trong BOB. N c nh ngha bng c C++ v OTcl: T file tcl/lib/ns-packet.tcl:
PacketHeaderManager set hdrlen_ 0 ...... foreach prot { AODV ARP aSRM Common CtrMcast Diffusion ...... TORA UMP } { add-packet-header $prot } Simulator instproc create_packetformat {} { PacketHeaderManager instvar tab_ set pm [new PacketHeaderManager] foreach cl [PacketHeader info subclass] { if [info exists tab_($cl)] {

Bai tiu lun mn hoc IP-ng dung va bao mt

75

Gii thiu cng cu NS


set off [$pm allochdr $cl] $cl offset $off } } $self set packetManager_ $pm } PacketHeaderManager instproc allochdr cl { set size [$cl set hdrlen_] $self instvar hdrlen_ set NS_ALIGN 8 ;# round up to nearest NS_ALIGN bytes, (needed on sparc/solaris) set incr ~($NS_ALIGN-1)] [expr ($size + ($NS_ALIGN-1)) &

set base $hdrlen_ incr hdrlen_ $incr return $base }

T file packet.cc:
/* manages active packet header types */ class PacketHeaderManager : public TclObject { public: PacketHeaderManager() { bind("hdrlen_", &Packet::hdrlen_); } };

on m trong ~ns/tcl/lib/ns-packet.tcl c thc hin khi khi to b m phng. Nh vy, cu lnh ny c thc hin trc khi m phng bt u v khi to mng lp OTcl tab_ cha nh x gia lp tn v tn ca cc lp header gi hin hnh. Nh trnh by trong 12.1, header gi c th truy nhp bng cch s dng hdr_> hdrname? ::access(). Th tc create_packetformat{} l mt phn ca lp c bn Simulator v c gi mt ln khi cu hnh b m phng. u tin n to i tng PacketHeaderManager. Hm khi to trong C++ lin kt bin OTcl hdrlen_ (ca lp PacketHeaderManager) vo bin C++ Packet::hdrlen_ (mt b phn tnh ca lp Packet). Sau khi to i tng qun l gi, vng lnh cho php cc header gi quan tm. Vng lp ny lp li danh mc cc header gi c nh ngha dng @)ACBED_F1BHG trong ACB l tn ca header th I v F1B l tn ca bin cha v tr ca header AJB trong BOB. Vic t cc header c thc hin bi th tc allochdr ca lp PacketHeaderManager trong OTcl. Th tc ny gi bin chy hdrlen_ Bai tiu lun mn hoc IP-ng dung va bao mt 76

Gii thiu cng cu NS v chiu di hin thi ca BOB khi cc header gi mi c cho php. N cng sp xp cc khong 8-byte cho cc header gi mi c cho php. iu ny rt cn m bo rng khi s dng i lng c chiu di 2-word v cn sp xp cc khong 2-word th s khng b li khi truy nhp.
11.3. Cc lnh c bn

Sau y l danh mc cc th tc lin quan n header gi:


Simulator::create_packetformat PacketHeaderManager::allochdr

Bai tiu lun mn hoc IP-ng dung va bao mt

77