260 likes | 419 Views
Ns Tutorial. 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw http://140.116.72.80/~smallko http://140.116.72.80/~smallko/ns2/ns2.htm. The Network Simulator - ns-2. http://www.isi.edu/nsnam/ns/ NS2 is a discrete event simulator targeted at networking research
E N D
Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw http://140.116.72.80/~smallko http://140.116.72.80/~smallko/ns2/ns2.htm
The Network Simulator - ns-2 • http://www.isi.edu/nsnam/ns/ • NS2 is a discrete event simulator targeted at networking research • NS2is 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
Documentation • introductory: Marc Greis's tutorial • reference: Ns Manual (formerly called "ns Notes and Documentation") • ns by Example • Practical Programming in Tcl and Tk (http://www.beedub.com/book/) • http://140.116.72.80/~smallko/ns2/ns2.htm
Tcl Fundamentals The basic syntax for a Tcl command is: command arg1 arg2 arg3 ... The “Hello, World!” example tcl>puts stdout {Hello, World!} Hello, World! Math Expressions tcl>expr 7.2 / 4 1.8
#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
Example 1 The first Tcl script #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 }
#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
tell the CBR agent when to send data and when to stop sending #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
#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 } 1 Example 2: TCP connections
2 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" }
3 #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
#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
5 #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
#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 }
#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
#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
#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
#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
[End-to-End Delay] #記錄封包的傳送時間 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); } }
執行方法: ($為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 ………………………
[Loss] END { printf("number of packets sent:%d lost:%d\n", numFs, fsDrops); } 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++; } 執行方法: ($為shell的提示符號) $awk -f measure-drop.awk out.tr 執行結果: number of packets sent: 550 lost:8 這代表CBR送出了550個封包,但其中8個封包丟掉了。