Download presentation
Presentation is loading. Please wait.
1
Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw http://140.116.72.80/~smallko http://140.116.72.80/~smallko/ns2/ns2.htm
2
The Network Simulator - ns-2 http://www.isi.edu/nsnam/ns/ http://www.isi.edu/nsnam/ns/ NS2 is a discrete event simulator targeted at networking research NS2 is an object oriented simulator, written in C++, with an OTcl interpreter as a frontend C++: fast to run,slower to change, => detailed protocol implementation. Otcl: slower to run, fast to change(interactive), => simulation configuration. Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks
3
Documentation introductory: Marc Greis's tutorialMarc Greis's tutorial reference: Ns Manual (formerly called "ns Notes and Documentation")Ns Manual ns by Example ns by Example Practical Programming in Tcl and Tk (http://www.beedub.com/book/)http://www.beedub.com/book/ http://140.116.72.80/~smallko/ns2/ns2.htm http://140.116.72.80/~smallko/ns2/ns2.htm
4
Tcl Fundamentals tcl>puts stdout {Hello, World!} Hello, World! The “Hello, World!” example The basic syntax for a Tcl command is: command arg1 arg2 arg3... tcl>expr 7.2 / 4 1.8 Math Expressions
5
#Create a simulator object set ns [new Simulator] #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 } # Insert your own code for topology creation # and agent definitions, etc. here #Call the finish procedure after 5 seconds simulation time $ns at 5.0 "finish" #Run the simulation $ns run How to start #proc name arglist body
6
# Create a simulator object set ns [new Simulator] #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 trace file close $nf #Execute nam on the trace file exec nam out.nam & exit 0 } Example 1The first Tcl script
7
#Create two nodes set n0 [$ns node] set n1 [$ns node] #Create a duplex link between the nodes $ns duplex-link $n0 $n1 1Mb 10ms DropTail #Create a UDP agent and attach it to node n0 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 # Create a CBR traffic source and attach it to udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0 #Create a Null agent (a traffic sink) and 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 Two nodes, one link
8
#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 tell the CBR agent when to send data and when to stop sending
9
#Create a simulator object set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red set statevar cwnd_ set record_interval 0.02 set file1 [open file1.ns w] set file2 [open file2.ns w] #Define a 'finish' procedure proc finish {} { global ns nf file1 file2 statevar close $file1 close $file2 $ns flush-trace eval "exec xgraph file1.ns file2.ns -x time -y $statevar -t Reno_Test" & exit 0 } Example 2: TCP connections 1
10
proc record {} { global ns tcp1 file1 tcp2 file2 statevar record_interval #Set the time after which the procedure should be called again set time $record_interval #Get the current time set now [$ns now] puts $file1 "$now [$tcp1 set $statevar]" puts $file2 "$now [$tcp2 set $statevar]" #Re-schedule the procedure $ns at [expr $now+$time] "record" } 2
11
#Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n1 10Mb 0.4ms DropTail $ns duplex-link $n3 $n1 10Mb 0.4ms DropTail $ns duplex-link $n1 $n2 1.5Mb 40ms DropTail #Set Queue Size of link (n2-n3) to 20 $ns queue-limit $n1 $n2 20 3
12
#Setup two TCP connections set tcp1 [new Agent/TCP/Reno] $ns attach-agent $n0 $tcp1 set sink1 [new Agent/TCPSink] $ns attach-agent $n2 $sink1 $ns connect $tcp1 $sink1 $tcp1 set window_ 128 set tcp2 [new Agent/TCP/Reno_debug] $ns attach-agent $n3 $tcp2 set sink2 [new Agent/TCPSink] $ns attach-agent $n2 $sink2 $ns connect $tcp2 $sink2 $tcp2 set window_ 64 #Setup two FTP over TCP connections set ftp1 [new Application/FTP] $ftp1 attach-agent $tcp1 $ftp1 set type_ FTP set ftp2 [new Application/FTP] $ftp2 attach-agent $tcp2 $ftp2 set type_ FTP 4
13
#Schedule events for the FTP agents $ns at 0.0 "record" $ns at 00.0 "$ftp1 start" $ns at 20.0 "$ftp1 stop" $ns at 00.0 "$ftp2 start" $ns at 20.0 "$ftp2 stop" #Call the finish procedure after 5 seconds of simulation time $ns at 20.0 "finish" #Run the simulation $ns run 5
15
Example 3
16
#Create a simulator object set ns [new Simulator] #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red #Open the NAM trace file set nf [open out.nam w] $ns namtrace-all $nf set nd [open out.tr w] $ns trace-all $nd #Define a 'finish' procedure proc finish {} { global ns nf nd $ns flush-trace #Close the NAM trace file close $nf close $nd #Execute NAM on the trace file exec nam out.nam & exit 0 }
17
#Create four nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node] #Create links between the nodes $ns duplex-link $n0 $n2 2Mb 10ms DropTail $ns duplex-link $n1 $n2 2Mb 10ms DropTail $ns duplex-link $n2 $n3 1.7Mb 20ms DropTail #Set Queue Size of link (n2-n3) to 10 $ns queue-limit $n2 $n3 10 #Give node position (for NAM) $ns duplex-link-op $n0 $n2 orient right-down $ns duplex-link-op $n1 $n2 orient right-up $ns duplex-link-op $n2 $n3 orient right #Monitor the queue for link (n2-n3). (for NAM) $ns duplex-link-op $n2 $n3 queuePos 0.5
18
#Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n3 $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
19
#Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n1 $udp set null [new Agent/Null] $ns attach-agent $n3 $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
20
#Schedule events for the CBR and FTP agents $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start" $ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop" #Detach tcp and sink agents (not really necessary) $ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink" #Call the finish procedure after 5 seconds of simulation time $ns at 5.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
21
Trace File Format and Output Trace File + 0.1 1 2 cbr 1000 ------- 2 1.0 3.1 0 0 - 0.1 1 2 cbr 1000 ------- 2 1.0 3.1 0 0 + 0.108 1 2 cbr 1000 ------- 2 1.0 3.1 1 1 - 0.108 1 2 cbr 1000 ------- 2 1.0 3.1 1 1 r 0.114 1 2 cbr 1000 ------- 2 1.0 3.1 0 0 + 0.114 2 3 cbr 1000 ------- 2 1.0 3.1 0 0 - 0.114 2 3 cbr 1000 ------- 2 1.0 3.1 0 0 + 0.116 1 2 cbr 1000 ------- 2 1.0 3.1 2 2 - 0.116 1 2 cbr 1000 ------- 2 1.0 3.1 2 2 r 0.122 1 2 cbr 1000 ------- 2 1.0 3.1 1 1 + 0.122 2 3 cbr 1000 ------- 2 1.0 3.1 1 1............................................................................
22
[End-to-End Delay] BEGIN { # 程式初始化,設定一變數以記錄目前最高處理封包 ID 。 highest_packet_id = 0; } { action = $1; time = $2; node_1 = $3; node_2 = $4; type = $5; flow_id = $8; node_1_address = $9; node_2_address = $10; seq_no = $11; packet_id = $12; # 記錄目前最高的 packet ID if ( packet_id > highest_packet_id ) highest_packet_id = packet_id; # 記錄封包的傳送時間 if ( start_time[packet_id] == 0 ) start_time[packet_id] = time; # 記錄 CBR (flow_id=2) 的接收時間 if ( flow_id == 2 && action != "d" ) { if ( action == "r" ) { end_time[packet_id] = time; } } else { # 把不是 flow_id=2 的封包或者是 flow_id=2 # 但此封包被 drop 的時間設為 -1 end_time[packet_id] = -1; } END { # 當資料列全部讀取完後,開始計算有效封包的端點到端點延遲時間 for ( packet_id = 0; packet_id <= highest_packet_id; packet_id++ ) { start = start_time[packet_id]; end = end_time[packet_id]; packet_duration = end - start; # 只把接收時間大於傳送時間的記錄列出來 if ( start < end ) printf("%f %f\n", start, packet_duration); }
23
執行方法 : ($ 為 shell 的提示符號 ) $awk -f measure-delay.awk out.tr 若是要把結果存到檔案,可使用導向的方式。 ( 把結果存到 cbr_delay 檔案中 ) $awk -f measure-delay.awk out.tr > cbr_delay 執行結果 :0.100000 0.038706 0.108000 0.038706 0.116000 0.038706 0.124000 0.038706 0.132000 0.038706 ………………………
25
[Loss] BEGIN { # 程式初始化, 設定一變數記錄 packet 被 drop 的數目 fsDrops = 0; numFs = 0; } { action = $1; time = $2; node_1 = $3; node_2 = $4; src = $5; flow_id = $8; node_1_address = $9; node_2_address = $10; seq_no = $11; packet_id = $12; # 統計從 n1 送出多少 packets if (node_1==1 && node_2==2 && action == "+") numFs++; # 統計 flow_id 為 2, 且被 drop 的封包 if (flow_id==2 && action == "d") fsDrops++; } END { printf("number of packets sent:%d lost:%d\n", numFs, fsDrops); } 執行方法 : ($ 為 shell 的提示符號 ) $awk -f measure-drop.awk out.tr 執行結果 : number of packets sent: 550 lost:8 這代表 CBR 送出了 550 個封包,但其中 8 個封包丟掉了。
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.