490 likes | 709 Views
NS-2 網路模擬器介紹與使用. 吳賢明 國立中興大學 計算機及資訊網路中心 中華民國九十三年十月二十七日 . Agenda. 淺談網路模擬 NS2 簡介 NS2 架構 、 安裝與使用 視覺化實例演練 Q&A. Methods for network research. Analytical Model General expression or close form Emulation Network testbed Lab environment Real code Simulation Virtual network testbed.
E N D
NS-2 網路模擬器介紹與使用 吳賢明 國立中興大學 計算機及資訊網路中心 中華民國九十三年十月二十七日
Agenda • 淺談網路模擬 • NS2簡介 • NS2架構、安裝與使用 • 視覺化實例演練 • Q&A
Methods for network research • Analytical • Model • General expression or close form • Emulation • Network testbed • Lab environment • Real code • Simulation • Virtual network testbed
Why Simulation • Study of implemented protocols and algorithms • Behavior • Performance • Test of unimplemented new protocols and algorithms • Comparison of results across research efforts
Simulation -- Advantage • Inexpensive, Flexible and Reconfigurable • Network phenomena interested can be reproduced • Opportunity to study large-scale network • Easier comparison of results across research efforts
Simulation -- Disadvantage • Important network details may be missed • Protocols or algorithms must be “added” before simulation can be done • High start-up cost • Have to be carefully verified before the test results can be used
Network simulators • Specific Purpose • ATM • Wireless • TCP/IP • General Purpose • Modifiable modules • Modules extensibility
What features should a simulator provide ? • Easy network topology setup • Protocols and application implementation • TCP,UDP,… • FTP, Telnet, Web, CBR, VBR,… • Routing protocols • Queue management protocols • Configurability • Extensibility
Agenda • 淺談網路模擬 • NS2簡介 • NS2架構、安裝與使用 • 視覺化實例演練 • Q&A
What is NS-2 • NEST (Network Simulation Testbed) REAL (Realistic and Large) NS-1 NS-2 • NS-2 is a common network simulator • NS-2 is developed by the VINT project in order to reduce duplication of effort within the network research and develop community • NS2 is publicly available at http://www.isi.edu/nsnam/ns/ns-build.html
About NS2 • 1989: REAL network simulator • 1995: DARPA VINT project at LBL, Xerox PARC, UCB, and USC/ISI • Present: DARPA SAMAN project and NSF CONSER project • Collaboration with other researchers including CIRI
Ns Status • Periodical release (ns-2.27, Jan 2004) • ~200K LOC in C++ and Otcl, • ~120 test suites and 100+ examples • 392 pages of ns manual • Daily snapshot (with auto-validation) • Stability validation • http://www.isi.edu/nsnam/ns/ns-tests.html • Platform support • FreeBSD, Linux, Solaris, Windows and Mac • User base • > 1k institutes (50 countries), >10k users • About 300 posts to ns-users@isi.edu every month
Ns functionalities • Wired • Transportation: TCP,UDP,RTP,SRM • Traffic sources:web, ftp, telnet, cbr, stochastic • Queuing disciplines:drop-tail, RED, FQ, SFQ, DRR • QoS: IntServ and Diffserv • Emulation • Wireless • Ad hoc routing and mobile IP • Directed diffusion, sensor-MAC • Tracing, visualization, various utilities
“Ns” Components • Ns, the simulator itself • Nam, the network animator • Visualize ns (or other) output • Nam editor: GUI interface to generate ns scripts • Pre-processing: • Traffic and topology generators • Post-processing: • Simple trace analysis, often in Awk, Perl, or Tcl
Ns Models • Traffic models and applications: • Web, FTP, telnet, constant-bit rate, real audio • Transport protocols: • unicast: TCP (Reno, Vegas, etc.), UDP • Multicast: SRM • Routing and queueing: • Wired routing, ad hoc rtg and directed diffusion • queueing protocols: RED, drop-tail, etc • Physical media: • Wired (point-to-point, LANs), wireless (multiple propagation models), satellite
Agenda • 淺談網路模擬 • NS2簡介 • NS2架構、安裝與使用 • 視覺化實例演練 • Q&A
NS Architecture • Split-Programming Model • C++ • implement the simulation model • Implement data path • per packet processing, core of ns • fast to run, detailed, complete control • OTcl (Tcl script language with Object-oriented extensions developed at MIT ) • Simulation scenario configurations • Periodic or triggered action • Manipulating existing C++ objects • fast to write and change
NS Architecture • Advantage • Reusability • Maintenance • Disadvantage • Performance (speed and memory) • Careful planning of modularity
Resources and Documentations • NS2 and related component • Tcl release 8.4.5 (required component) • Tk release 8.4.5 (required component) • Otcl release 1.8 (required component) • TclCL release 1.15 (required component) • Ns release 2.27 (required component) • Nam release 1.10 (optional component) • Xgraph version 12 (optional component) • CWeb version 3.4g (optional component) • SGB version 1.0 (?) (optional component, builds sgblib for all UNIX type platforms) • Gt-itm gt-itm and sgb2ns 1.1 (optional component) • Zlib version 1.1.4 (optional, but required should Nam be used)
Resources and Documentation • Getting the pieces • Tcl/TK 8.x (Tcl-8.4.5 tk-8.4.5): http://www.isi.edu/nsnam/dist/tcltk • Otcl(1.8) and TclCL(1.15): http://sourceforge.net/project/showfiles.php?group_id=30174 • ns-2 and nam-1: http://www.isi.edu/nsnam/dist/ns-src-2.27.tar.gz • All-at-once http://www.isi.edu/nsnam/dist/ns-allinone-2.27.tar.gz
Resources and Documents • Ns and nam build questions • http://www.isi.edu/nsnam/ns/ns-build.html • Ns mailing list: ns-users@isi.edu • Ns manual and tutorial (in distribution) • TCL: http://dev.scriptics.com/scripting • Otcl tutorial (in distribution): ftp://ftp.tns.lcs.mit.edu/pub/otcl/doc/tutorial.html
Running ns-2 for Windows 9x/2000/XP under Cygwin • Cygwin • Cygwin is a Linux-like environment for Windows • ns-2 supports Cygwin • Get Cygwin from http://www.cygwin.com • ns-2 installation • From all pieces • All-at-once • http://www.isi.edu/nsnam/ns/ns-build.html
ns-2 installation • Get the file : http://www.isi.edu/nsnam/dist/ns-allinone-2.27.tar.gz • Install files #tar zxvf ns-allinone-2.27.tar.gz #cd ns-allinone-2.27 #install • Configure system • Update user environment (~/.bashrc) export NS_HOME=/home/XXX/ns-allinone-2.27/ export PATH=$NS_HOME/tcl8.4.5/unix:$NS_HOME/tk8.4.5/unix:$NS_HOME/bin:$PATH export LD_LIBRARY_PATH=$NS_HOME/tcl8.4.5/unix:$NS_HOME/tk8.4.5/unix:\ $NS_HOME/otcl-1.8:$NS_HOME/lib:$LD_LIBRARY_PATH export TCL_LIBRARY=$NS_HOME/tcl8.4.5/library • For detail, ref to : http://www.sims.berkeley.edu/~christin/ns-cygwin.shtml
ns-2 Directory Structure OTcl code
Tcl Basic – Hello World #Define a process named "helloworld" proc helloworld {} { puts "Hello world!!!" } # Call the process helloworld Helloworld
Tcl Basic – Another Example proc pow {x n} { if {$n == 1} { return $x } set part [pow $x [expr $n-1]] return [expr $x*$part] } set x 10 puts "x is $x" set y [pow $x 2] puts "y is $y" while { $x > 0 } { puts $x incr x -1 }
# Create a class call "mom" and # add a member function call "greet" Class mom mom instproc greet {} { $self instvar age_ puts "$age_ years old mom say: How are you doing?" } # Create a child class of "mom" called "kid" # and overide the member function "greet" Class kid -superclass mom kid instproc greet {} { $self instvar age_ puts "$age_ years old kid say: What's up, dude?" } # Create a mom and a kid object set each age set a [new mom] $a set age_ 45 set b [new kid] $b set age_ 15 # Calling member function "greet" of each object $a greet $b greet OTcl - An Examplefrom:http://nile.wpi.edu/NS/
Agenda • 淺談網路模擬 • NS2簡介 • NS2架構、安裝與使用 • 視覺化實例演練 • Q&A
Using ns Problem Result analysis Simulation model Modify ns Setup/run simulation with ns
Retransmit Timeout TCP congestion control Connection Initialization Slow Start SSTHRESH Reached CongestionWindow Reduced to 1 Congestion Notified Fast Retransmission Timeout Again Congestion Avoidance ExponentialBackoff
Simple Simulation Example IDisplay Result with nam Animation FTP n2 tcp 2 mbps, 10 ms 1.7 mbps, 20 ms n0 n1 sink FTP sec 4.0 0.5 3.0
#Create a simulator object set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue #Open the 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 the NAM trace file close $nf #Execute NAM on the trace file exec nam out.nam & exit 0 } #Create three nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n1 1.7Mb 20ms DropTail $ns duplex-link $n2 $n0 2Mb 10ms DropTail #Set Queue Size of link (n0-n1) to 5 $ns queue-limit $n0 $n1 5 #Give node position (for NAM) $ns duplex-link-op $n0 $n1 orient right $ns duplex-link-op $n2 $n0 orient right #Monitor the queue for link (n0-n1). (for NAM) $ns duplex-link-op $n0 $n1 queuePos 0.5 #Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n2 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n1 $sink $ns connect $tcp $sink $tcp set fid_ 1 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Schedule events for the FTP agents $ns at 0.5 "$ftp start" $ns at 3.0 "$ftp stop" #Call the finish procedure after 4 seconds of #simulation time $ns at 4.0 "finish" #Run the simulation $ns run Source of Example I
FTP1 n2 tcp1 sink1 n0 n1 sink2 n3 tcp2 FTP2 FTP2 FTP1 sec 4.0 0.5 3.0 3.5 Simple Simulation Example IIDisplay Result with nam Animation 2 mbps, 10 ms 2 mbps, 20 ms 2 mbps, 10 ms
….. #Create four nodes …. #Create links between the nodes …. #Set Queue Size of link (n0-n1) to 5 …. #Give node position (for NAM) …. #Monitor the queue for link (n0-n1). (for NAM) …. #Setup a TCP connection set tcp1 [new Agent/TCP] set tcp2 [new Agent/TCP] $tcp1 set class_ 2 $tcp2 set class_ 2 $ns attach-agent $n2 $tcp1 $ns attach-agent $n3 $tcp2 set sink1 [new Agent/TCPSink] set sink2 [new Agent/TCPSink] $ns attach-agent $n1 $sink1 $ns attach-agent $n1 $sink2 $ns connect $tcp1 $sink1 $ns connect $tcp2 $sink2 $tcp1 set fid_ 1 $tcp2 set fid_ 2 #Setup two FTP over TCP connection set ftp1 [new Application/FTP] set ftp2 [new Application/FTP] $ftp1 attach-agent $tcp1 $ftp2 attach-agent $tcp2 $ftp1 set type_ FTP $ftp2 set type_ FTP #Schedule events for the FTP agents $ns at 0 "$ftp2 start" $ns at 0.5 "$ftp1 start" $ns at 3.0 "$ftp1 stop" $ns at 3.5 "$ftp2 stop" #Call the finish procedure after 4 seconds of #simulation time $ns at 4.0 "finish" #Run the simulation $ns run Source of Example II
FTP n2 tcp sink1 n0 n1 null n3 udp cbr cbr ftp sec 4.0 0.5 3.0 3.5 Simple Simulation Example IIIDisplay Result with nam Animation 2 mbps, 10 ms 2 mbps, 20 ms 2 mbps, 10 ms Size : 1 kByte Rate: 1 mbps
…. #Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n2 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n1 $sink $ns connect $tcp $sink $tcp set fid_ 1 #Setup a FTP over TCP connection set ftp [new Application/FTP] $ftp attach-agent $tcp $ftp set type_ FTP #Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n3 $udp set null [new Agent/Null] $ns attach-agent $n1 $null $ns connect $udp $null $udp set fid_ 2 #Setup a CBR over UDP connection set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set type_ CBR $cbr set packet_size_ 1000 $cbr set rate_ 1mb $cbr set random_ false #Schedule events for the CBR and FTP agents $ns at 0 "$cbr start" $ns at 0.5 "$ftp start" $ns at 3.0 "$ftp stop" $ns at 3.5 "$cbr stop" #Call the finish procedure after 4 seconds of #simulation time $ns at 4.0 "finish" #Print CBR packet size and interval puts "CBR packet size = [$cbr set packet_size_]" puts "CBR interval = [$cbr set interval_]" #Run the simulation $ns run Source of Example III
Trace Analysis • Open NAM trace file set nam_file_var [open out_nam_file w] $ns namtrace-all $file_var • Open raw data trace file set data_file_var [open out_data_file w] $ns trace-all $data_file_var
NAM trace file example …. V -t * -v 1.0a5 -a 0 A -t * -n 1 -p 0 -o 0xffffffff -c 31 -a 1 A -t * -h 1 -m 2147483647 -s 0 c -t * -i 1 -n Blue n -t * -a 0 -s 0 -S UP -v circle -c black -i black n -t * -a 1 -s 1 -S UP -v circle -c black -i black n -t * -a 2 -s 2 -S UP -v circle -c black -i black l -t * -s 0 -d 1 -S UP -r 1700000 -D 0.02 -c black -o right l -t * -s 2 -d 0 -S UP -r 2000000 -D 0.01 -c black -o right q -t * -s 1 -d 0 -a 0.5 q -t * -s 0 -d 1 -a 0.5 + -t 0.5 -s 2 -d 0 -p tcp -e 40 -c 1 -i 0 -a 1 -x {2.0 1.0 0 ------- null} - -t 0.5 -s 2 -d 0 -p tcp -e 40 -c 1 -i 0 -a 1 -x {2.0 1.0 0 ------- null} h -t 0.5 -s 2 -d 0 -p tcp -e 40 -c 1 -i 0 -a 1 -x {2.0 1.0 -1 ------- null} r -t 0.51016 -s 2 -d 0 -p tcp -e 40 -c 1 -i 0 -a 1 -x {2.0 1.0 0 ------- null} + -t 0.51016 -s 0 -d 1 -p tcp -e 40 -c 1 -i 0 -a 1 -x {2.0 1.0 0 ------- null} - -t 0.51016 -s 0 -d 1 -p tcp -e 40 -c 1 -i 0 -a 1 -x {2.0 1.0 0 ------- null} h -t 0.51016 -s 0 -d 1 -p tcp -e 40 -c 1 -i 0 -a 1 -x {2.0 1.0 -1 ------- null} …. Read and interpreted by NAM for animation display
r : receive (at to node) • +: enqueue (at queue) • : dequeue (at queue) • d : drop (at queue) node.port Raw data trace file format and example (from example III) 1 2 6 8 10 11 3 4 5 7 9 12 …. + 0.884 1 2 cbr 1000 ------- 2 1.0 3.1 98 98 - 0.884 1 2 cbr 1000 ------- 2 1.0 3.1 98 98 r 0.89 1 2 cbr 1000 ------- 2 1.0 3.1 97 97 + 0.89 2 3 cbr 1000 ------- 2 1.0 3.1 97 97 - 0.89 2 3 cbr 1000 ------- 2 1.0 3.1 97 97 r 0.890706 2 3 cbr 1000 ------- 2 1.0 3.1 94 94 + 0.892 1 2 cbr 1000 ------- 2 1.0 3.1 99 99 - 0.892 1 2 cbr 1000 ------- 2 1.0 3.1 99 99 r 0.898 1 2 cbr 1000 ------- 2 1.0 3.1 98 98 + 0.898 2 3 cbr 1000 ------- 2 1.0 3.1 98 98 - 0.898 2 3 cbr 1000 ------- 2 1.0 3.1 98 98 r 0.898706 2 3 cbr 1000 ------- 2 1.0 3.1 95 95 ….
RED Queue Monitor ExampleAn example by Polly Huang from http://nile.wpi.edu.tw/NS/Example/red/tcl ftp1 sink1 s3 s1 tcp1 10 mbps, 2 ms sink2 max_win=15 10 mbps, 4 ms r1 1.5 mbps, 20 ms r2 RED(size=25) max_win=15 10 mbps, 3 ms s2 tcp2 ftp2 ftp1 ftp2 sec 10 3.0 0
.... set tcp1 [$ns create-connection TCP/Reno $node_(s1) TCPSink $node_(s3) 0] $tcp1 set window_ 15 set tcp2 [$ns create-connection TCP/Reno $node_(s2) TCPSink $node_(s3) 1] $tcp2 set window_ 15 set ftp1 [$tcp1 attach-source FTP] set ftp2 [$tcp2 attach-source FTP] # Tracing a queue set redq [[$ns link $node_(r1) $node_(r2)] queue] set tchan_ [open all.q w] $redq trace curq_ $redq trace ave_ $redq attach $tchan_ .... proc finish {} { global tchan_ set awkCode { { if ($1 == "Q" && NF>2) { print $2, $3 >> "temp.q"; set end $2 } else if ($1 == "a" && NF>2) print $2, $3 >> "temp.a"; } } set f [open temp.queue w] puts $f "TitleText: red" puts $f "Device: Postscript" if { [info exists tchan_] } { close $tchan_ } exec rm -f temp.q temp.a exec touch temp.a temp.q exec awk $awkCode all.q puts $f \"queue exec cat temp.q >@ $f puts $f \n\"ave_queue exec cat temp.a >@ $f close $f exec xgraph -bb -tk -x time -y queue temp.queue & exit 0 } $ns run
More … • OTcl linkage (with C++ classes) • Variable export • Control command export • Recompilation • Extend NS-2 • Modify existed object • Add new application • Add new agent • … • NS-2 main page • http://wwww.isi.edu/nsnam/ns