Professional Documents
Culture Documents
Giao Trinh Thuc Hanh Ns2
Giao Trinh Thuc Hanh Ns2
MC LC
MC LC ...................................................................................................................1
MC LC HNH ........................................................................................................3
CHNG 1 TNG QUAN V NS-2........................................................................5
1.1 Gii thiu..............................................................................................................................5
1.2 Download v install NS-2 v NAM......................................................................................5
1.2.1 Cch 1: Ci t bng duy nht mt gi phn mm........................................................5
1.2.1.a Download ...............................................................................................................5
1.2.1.b Install......................................................................................................................6
1.2.2 Cch 2: Ci t bng cch ci tng gi phn mm ring l...........................................7
1.2.2.a Download................................................................................................................7
1.2.2.b Install......................................................................................................................7
1.3 Chy chng trnh NS-2 v NAM........................................................................................9
-1-
Mc lc
CHNG 4 OTCL SCRIPT TRONG NS-2...........................................................28
4.1 V d ...................................................................................................................................28
4.2 Khi to node......................................................................................................................33
4.3 Khi to link.......................................................................................................................33
4.3.1 Simplex-link................................................................................................................34
4.3.2 Duplex-link..................................................................................................................34
4.4 Khi to Network Agents...................................................................................................34
4.4.1 UDP ...........................................................................................................................34
4.4.2 TCP..............................................................................................................................35
4.5 Cc loi traffic.....................................................................................................................35
4.5.1 Tc bit c nh CBR...............................................................................................35
4.5.2 Traffic thay i theo phn b m Exponential............................................................36
4.5.3 Traffic thay i theo phn b Pareto...........................................................................36
4.5.4 TrafficTrace.................................................................................................................36
4.6 Cc dch v c bn trong Internet ......................................................................................37
4.6.1 Giao thc truyn tp tin FTP ......................................................................................37
4.6.2 Telnet...........................................................................................................................37
4.6.3 Giao thc truyn siu vn bn HTTP..........................................................................37
4.7 Tracing................................................................................................................................38
4.8 Routing................................................................................................................................39
4.8.1 Unicast.........................................................................................................................40
4.8.2 Multicast .....................................................................................................................40
CHNG 5 CC V D C BN.........................................................................41
5.1 Kch bn OTcl th nht ......................................................................................................41
5.2 Kch bn OTcl th hai.........................................................................................................43
5.3 Kch bn Otcl th ba ..........................................................................................................45
-2-
Mc lc hnh
MC LC HNH
-3-
Mc lc hnh
-4-
CHNG 1
ch
cweb
gt-itm
nam-1.11
ns-2.28
otcl-1.9
sgb
tcl8.4
tclcl-1.16
tk8.4
xgraph-1.1
zlib-1.1
install
install.win
readme install
-5-
{ng dn n th mc ns-allinone-2.28}/bin
{ng dn n th mc ns-allinone-2.28}/tcl8.4.5/unix
{ng dn n th mc ns-allinone-2.28}/tk8.4.5/unix
-6-
-7-
make
ns-2
./configure
make
nam
./configure
make
Gio trnh thc hnh Ns2
-8-
ns-allinone-2.28
Tcl8.4.5
TK8.4.5
OTcl
tclcl
tcl
ex
Cc v d
test
ns-2.28
...
lib
Cc kim tra
nam1.19
M C++
mcast
...
M OTcl
-9-
CHNG 2
OTcl Script
Simulation Program
OTcl
NS Simulation Library
Event Scheduler Objects
Network Component Objects
Network Setup Helping Modules
Plumbling Modules
Simulation Results
Analysis
NAM Network Animator
Kch bn OTcl
Chng trnh M phng
B bin dch Tcl m rng hng i tng
Th vin M phng NS
Cc i tng B lp lch S kin
Cc i tng Thnh phn Mng
Cc m un Tr gip Thit lp Mng
Cc m un Plumbling
Cc kt qu M phng
Phn tch
Minh ha Mng NAM
Trong hnh trn, NS l B bin dch Tcl m rng hng i tng; bao gm cc i
tng B lp lch S kin, cc i tng Thnh phn Mng v cc m un Tr gip
Thit lp Mng (hay cc m un Plumbing).
s dng NS-2, user lp trnh bng ngn ng kch bn OTcl. User c th thm cc
m ngun Otcl vo NS-2 bng cch vit cc lp i tng mi trong OTcl. Nhng lp
ny khi s c bin dch cng vi m ngun gc. Kch bn OTcl c th thc hin
nhng vic sau:
Khi to B lp lch S kin
M phng o NAM
M phng Ln vt v Gim st
Event
Scheduler
TclCL
C/C++
ns-2
CHNG 3
3.1 NAM
Khi m phng kt thc, nu cc script Tcl (OTcl) u vo yu cu th NS s to ra cc
file text lu chi tit d liu m phng. D liu c th c dng cho vic phn tch
hay c dng nh u vo cho cc cng c trnh din m phng ha NAM.
NAM thc hin li nhng s kin t file ln vt nam (nam tracefile). File ny c th l
khng l khi thi gian m phng ln v s kin xy ra nhiu. V th, phi ht sc cn
thn. Ngoi ra, cn ch rng cc thng tin ha li khng c dng phn tch
m phng mt cch chnh xc.
C th tm tt cc tnh nng ca NAM nh sau:
Cung cp trnh din o cho mng c to
C th thi hnh trc tip t kch bn Tcl
NAM c giao din ho bt mt ca CD player vi cc iu khin bao gm
play (chy), stop (ngng), fast forward (chy tip nhanh), rw (li li), pause
(tm ngng), iu khin tc trnh din v tnh nng gim st packet
Biu din thng tin nh throughput (thng lng), s packet trn tng link
Cung cp giao din r v th cho vic to ra cc topology (m hnh).
3.1.1 Chy NAM
Chy NAM trong Command Line:
$nam a nam_trace_file.nam
Chy nam trong kch bn ns-2:
Proc finish{} {
exec nam a nam_trace_file.nam &
exit }
Cc tu chn khi chy NAM:
nam [-a -S -s -f init_script -d display -j jump -r rate -k
initPort] tracefiles
-a
-S
-s
-j
to mi mt instance ca nam
ng b ha X
ng b ho nhiu trace (nhiu ln vt)
thi gian khi ng
tc minh ha khi to
kch bn OTcl khi to
s socket port khi to
New
to mi mt topology ns dng chng trnh son tho NAM
Open m cc file trace tn ti (m ca s minh ha nam)
WinList m ra ca s lit k tn ca tt c cc file trace ang m
Quit
thot nam
o Analysis
Active Sessions
Legend
Control bar: di control bar c 5 button, 1 nhn v 1 thanh trt Rate
o 5 button
<< - Rewind
-stk
-fitx
-fity
-fmtx
-fmty
-scale
-O fn
-o fn
-bb
-db
-lnx
-lny
-m -M
-nl
-ng
-nb
-p -P
-rv
-tk
-a
-dl
copy
packet
CHNG 4
4.1 V d
V c bn, NS l trnh bin dch OTcl vi cc th vin i tng m phng mng.
Bit lp trnh bng OTcl s gip s dng NS d dng. V d sau s gip chng ta tip
cn vi OTcl. Chy v d ny bng cch g lnh ns ns-simple.tcl tai du nhc h
thng.
$ns duplex-link node1 node2 bandwidth delay queuetype: to hai simplex link (lin kt n) vi bandwidth v delay xc nh
tuyn, ni hai node xc nh li vi nhau. Trong NS, hng i u ra ca node
c thc thi nh mt phn ca link, v vy user nn xc nh lun queue-type
khi khi to link. Ging nh node, link l i tng ghp, user c th to v ni
cc link vi cc node. M ngun link c tm trong file ns-2/tcl/lib/ns-lib.tcl
v ns-2/tcl/lib/ns-link.tcl. Cn ch l ta hon ton c th chn cc m un
li vo trong thnh phn link m phng mt link hay b mt gi cng nh
chn thm bt k i tng mng no.
4.5.4 TrafficTrace
TrafficTrace l traffic c to da trn file trace. File nh phn phi c 2x32 trng
trong th t byte mng.Trng u tin l khong thi gian cho n khi packet d tip
c to, tnh bng ms. Trng th hai l chiu di ca packet k tip, tnh bng byte.
Th tc filename ca lp Tracefile gn file trace vo i tng Tracefile.
M OTcl:
set t_file [new Tracefile]
$t_file filename <file>
set src [new Application/Traffic/Trace]
$src attach-tracefile $t_file
Trong $t_file l file nh phn v hai trng trong trong file cha cc ln interpackets, tnh bng mili giy v c packet, tnh bng byte.
4.6.2 Telnet
M OTcl dng Telnet trong m phng:
set telnet [new Application/Telnet]
$telnet attach-agent $tcp
Cc tham s cu hnh:
interval_: thi gian n ni (inter-arrival) trung bnh ca packet c to bi
i tng Telnet
if (interval_==0) S ln n ni theo phn b tcplib
if (interval_!=0) S ln n ni theo phn b m, gi tr trung bnh c gn
bng gi tr interval_
4.6.3 Giao thc truyn siu vn bn HTTP
M OTcl thc thi HTTP (server (ch) v client (khch)) trong m phng:
ng dng HTTP Node client:
set client [new HTTP/Client $ns $node0]
$client connect $server
ng dng HTTP Node server:
set server [new HTTP/Server $ns $node0]
o
o
o
o
o
o
flags: c
fid: m lung
src addr: a ch node ngun
dst addr: a ch node ch
seq num: s trnh t
pkt id: m packet duy nht
Chng hn nh:
Chng 5: Cc v d c bn
CHNG 5
CC V D C BN
Chng 5: Cc v d c bn
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.1
$cbr0 attach-agent $udp0
#Create a Null agent(a traffic sink), attach it to node n1
set null0 [new Agent/Null]
$ns attach-agent $n1 $null0
#Connect the traffic source with the traffic sink
$ns connect $udp0 $null0
# Schedule events for the CBR agent
$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
#Call the finish procedure after 5 seconds of simulation
time
$ns at 5.0 "finish"
# Run the simulation
$ns run
Kt qu ca kch bn trn:
Chng 5: Cc v d c bn
5.2 Kch bn OTcl th hai
Kch bn ny to bn loi traffic ca internet. Bn th tc sau miu t cc loi b khi
to traffic sn c. Hai l s node cn m phng cc dch v ny.
#Create a simulator object
set ns [new Simulator]
#Open a nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
close $nf
exec nam out.nam &
exit 0
}
set n0 [$ns node]
set n1 [$ns node]
#Connect the nodes with two links
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
proc www_traffic { node0 node1 } {
global ns
set www_UDP_agent [new Agent/UDP]
set www_UDP_sink [new Agent/Null]
$ns attach-agent $node0 $www_UDP_agent
$ns attach-agent $node1 $www_UDP_sink
$ns connect $www_UDP_agent $www_UDP_sink
set www_CBR_source [new Application/Traffic/CBR]
$www_CBR_source attach-agent $www_UDP_agent
$www_CBR_source set packetSize_ 48
$www_CBR_source set interval_ 5000ms
$ns at 0.0 "$www_CBR_source start"
}
proc smtp_traffic {node0 node1 } {
global ns
set smtp_UDP_agent [new Agent/UDP]
set smtp_UDP_sink [new Agent/UDP]
$ns attach-agent $node0 $smtp_UDP_agent
$ns attach-agent $node1 $smtp_UDP_sink
$ns connect $smtp_UDP_agent $smtp_UDP_sink
set smtp_UDP_source [new Application/Traffic/Exponential]
$smtp_UDP_source attach-agent $smtp_UDP_agent
$smtp_UDP_source set packetSize_ 210
$smtp_UDP_source set burst_time_ 50ms
$smtp_UDP_source set idle_time_ 50ms
Chng 5: Cc v d c bn
$smtp_UDP_source set rate_ 100k
$ns at 0.0 "$smtp_UDP_source start"
}
proc ftp_traffic {node0 node1 } {
global ns
set ftp_TCP_agent [new Agent/TCP]
set ftp_TCP_sink [new Agent/TCPSink]
$ns attach-agent $node0 $ftp_TCP_agent
$ns attach-agent $node1 $ftp_TCP_sink
$ns connect $ftp_TCP_agent $ftp_TCP_sink
set ftp_FTP_source [new Application/FTP]
$ftp_FTP_source attach-agent $ftp_TCP_agent
#$ns at 0.0 "$ftp_FTP_source start"
#$ns at 0.1 "$ftp_FTP_source send 1"
$ns at 0.1 "$ftp_FTP_source produce 3"
$ns at 0.9 "$ftp_FTP_source stop"
$ns at 1.2 "$ftp_FTP_source producemore 6"
#$ns at 2.0 "finish"
}
proc telnet_traffic {node0 node1 } {
global ns
set telnet_TCP_agent [new Agent/TCP]
set telnet_TCP_sink [new Agent/TCPSink]
$ns attach-agent $node0 $telnet_TCP_agent
$ns attach-agent $node1 $telnet_TCP_sink
$ns connect $telnet_TCP_agent $telnet_TCP_sink
set telnet_TELNET_source [new Application/Telnet]
$telnet_TELNET_source attach-agent $telnet_TCP_agent
$telnet_TELNET_source set interval_ 20
$ns at 0.0 "$telnet_TELNET_source start"
$ns at 0.8 "$telnet_TELNET_source stop"
}
proc generate_traffic {node0 node1 } {
puts "function calls in here, Choose one at a time per
simulation to use"
#smtp_traffic $node0 $node1
#telnet_traffic $node0 $node1
#www_traffic $node0 $node1
#ftp_traffic $node0 $node1
}
generate_traffic $n0 $n1
$ns at 2.0 "finish"
$ns run
Kt qu ca m phng:
Chng 5: Cc v d c bn
Chng 5: Cc v d c bn
$ns namtrace-all $nf
# Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}
# Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
# Pinging Nodes
set n4 [$ns node]
set n5 [$ns node]
set p0 [new Agent/Ping]
$ns attach-agent $n4 $p0
set p1 [new Agent/Ping]
$ns attach-agent $n5 $p1
set p2 [new Agent/Ping]
$ns attach-agent $n3 $p2
set p3 [new Agent/Ping]
$ns attach-agent $n3 $p3
#Connect the two agents
$ns connect $p0 $p2
#Connect the two agents
$ns connect $p1 $p3
$n2 color red
$n3 color blue
$n4 color SeaGreen
$n5 color SeaGreen
# Define a 'recv' function for the class 'Agent/Ping'
Agent/Ping instproc recv {from rtt} {
$self instvar node_
puts "node [$node_ id] received ping answer from \
$from with round-trip-time $rtt ms."
}
#Create links between the nodes
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
$ns duplex-link $n4 $n2 1Mb 10ms DropTail
$ns duplex-link $n5 $n2 1Mb 10ms DropTail
$ns duplex-link-op $n0 $n2 orient right-down
Chng 5: Cc v d c bn
$ns duplex-link-op $n1 $n2 orient right-up
$ns duplex-link-op $n2 $n3 orient right
# Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5
# Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$ns attach-agent $n0 $udp0
# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0
# Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1
# Create a CBR traffic source and attach it to udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1
#Create a Null agent (a traffic sink) and attach it to node
n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
#Connect the traffic sources with the traffic sink
$ns connect $udp0 $null0
$ns connect $udp1 $null0
#Schedule events for the CBR agents
$ns at 0.5 "$cbr0 start"
$ns at 0.6 "$p0 send"
$ns at 0.8 "$p0 send"
$ns at 0.9 "$p0 send"
$ns at 0.6 "$p1 send"
$ns at 0.8 "$p1 send"
$ns at 0.9 "$p1 send"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
#Call the finish proc. after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
Kt qu ca m phng:
Chng 5: Cc v d c bn
Thut ng vit tt
THUT NG VIT TT
CBR
Tc bit c nh
CBQ
Hng i da trn lp
FTP
HBT
Lu lng n lc ti a c u tin
cao nht
HTTP
ID
Identifier
M nhn dng
IEEE
Vin k thut in in t
Electronic Engineers
IP
Internet Protocol
LAN
Mng cc b
MAC
MMS
MPLS
NAM
Network Animator
Minh ha mng
NFS
Network FileSystems
NS
Network Simulator
M phng mng
OSPF
RED
RT
Realtime Traffic
SBT
Lu lng n lc ti a n gin
SPF
ST
Signaling Traffic
Lu lng tn hiu
TCP
TTL
Time-To-Live
Thi gian tn ti
UDP
VBR
Tc bit thay i c
WAN
WLAN
Mng cc b khng dy
WWW
[6] Peng Zhang, Raimo Kantola, Zhansong Ma. Design and Implement of a new
routing Simulator. Laboratory of Telecommunication Technology, Helsinki
University of Technology, Findland.
[7] Xipeng Xiao and Linoel M. Ni. Internet QoS: A big Picture. Departurement
of Computer Science, 3115 Engineering Building, Michigan State University.
[8]
Marc
Greis