Ns Tutorial smallkoee ncku edu tw http 140

  • Slides: 25
Download presentation
Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee. ncku. edu. tw http: //140. 116. 72. 80/~smallko/ns

Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee. ncku. edu. tw http: //140. 116. 72. 80/~smallko/ns 2. htm

The Network Simulator - ns-2 o http: //www. isi. edu/nsnam/ns/ o NS 2 is

The Network Simulator - ns-2 o http: //www. isi. edu/nsnam/ns/ o NS 2 is a discrete event simulator targeted at networking research o NS 2 is an object oriented simulator, written in C++, with an OTcl interpreter as a frontend n C++: fast to run , slower to change, => detailed protocol implementation. n Otcl: slower to run, fast to change(interactive), => simulation configuration. o Ns provides substantial support for simulation of TCP, routing, and multicast protocols over wired and wireless (local and satellite) networks

Documentation o introductory: Marc Greis's tutorial o reference: Ns Manual (formerly called "ns Notes

Documentation o introductory: Marc Greis's tutorial o reference: Ns Manual (formerly called "ns Notes and Documentation") o ns by Example o Practical Programming in Tcl and Tk (http: //www. beedub. com/book/) o http: //140. 116. 72. 80/~smallko/ns 2. htm

Tcl Fundamentals The basic syntax for a Tcl command is: command arg 1 arg

Tcl Fundamentals The basic syntax for a Tcl command is: command arg 1 arg 2 arg 3. . . 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] How to start #Define a 'finish'

#Create a simulator object set ns [new Simulator] How to start #Define a 'finish' procedure #proc name arglist body 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

Example 1 The first Tcl script #Create a simulator object set ns [new Simulator]

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 n 0 [$ns node] set n 1 [$ns node] Two

#Create two nodes set n 0 [$ns node] set n 1 [$ns node] Two nodes, one link #Create a duplex link between the nodes $ns duplex-link $n 0 $n 1 1 Mb 10 ms Drop. Tail #Create a UDP agent and attach it to node n 0 set udp 0 [new Agent/UDP] $ns attach-agent $n 0 $udp 0 # Create a CBR traffic source and attach it to udp 0 set cbr 0 [new Application/Traffic/CBR] $cbr 0 set packet. Size_ 500 $cbr 0 set interval_ 0. 005 $cbr 0 attach-agent $udp 0 #Create a Null agent (a traffic sink) and attach it to node n 1 set null 0 [new Agent/Null] $ns attach-agent $n 1 $null 0 #Connect the traffic source with the traffic sink $ns connect $udp 0 $null 0

tell the CBR agent when to send data and when to stop sending #Schedule

tell the CBR agent when to send data and when to stop sending #Schedule events for the CBR agent $ns at 0. 5 "$cbr 0 start" $ns at 4. 5 "$cbr 0 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] Example 2: TCP connections #Define different

#Create a simulator object set ns [new Simulator] Example 2: TCP connections #Define different colors for data flows (for NAM) $ns color 1 Blue $ns color 2 Red set set statevar cwnd_ record_interval 0. 02 file 1 [open file 1. ns w] file 2 [open file 2. ns w] #Define a 'finish' procedure proc finish {} { global ns nf file 1 file 2 statevar close $file 1 close $file 2 $ns flush-trace eval "exec xgraph file 1. ns file 2. ns -x time -y $statevar -t Reno_Test" & } exit 0 1

2 proc record {} { global ns tcp 1 file 1 tcp 2 file

2 proc record {} { global ns tcp 1 file 1 tcp 2 file 2 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 $file 1 "$now [$tcp 1 set $statevar]" puts $file 2 "$now [$tcp 2 set $statevar]" } #Re-schedule the procedure $ns at [expr $now+$time] "record"

3 #Create four nodes set n 0 [$ns node] set n 1 [$ns node]

3 #Create four nodes set n 0 [$ns node] set n 1 [$ns node] set n 2 [$ns node] set n 3 [$ns node] #Create links between the nodes $ns duplex-link $n 0 $n 1 10 Mb 0. 4 ms Drop. Tail $ns duplex-link $n 3 $n 1 10 Mb 0. 4 ms Drop. Tail $ns duplex-link $n 1 $n 2 1. 5 Mb 40 ms Drop. Tail #Set Queue Size of link (n 2 -n 3) to 20 $ns queue-limit $n 1 $n 2 20

#Setup two TCP connections set tcp 1 [new Agent/TCP/Reno] $ns attach-agent $n 0 $tcp

#Setup two TCP connections set tcp 1 [new Agent/TCP/Reno] $ns attach-agent $n 0 $tcp 1 set sink 1 [new Agent/TCPSink] $ns attach-agent $n 2 $sink 1 $ns connect $tcp 1 $sink 1 $tcp 1 set window_ 128 set tcp 2 [new Agent/TCP/Reno_debug] $ns attach-agent $n 3 $tcp 2 set sink 2 [new Agent/TCPSink] $ns attach-agent $n 2 $sink 2 $ns connect $tcp 2 $sink 2 $tcp 2 set window_ 64 #Setup two FTP over TCP connections set ftp 1 [new Application/FTP] $ftp 1 attach-agent $tcp 1 $ftp 1 set type_ FTP set ftp 2 [new Application/FTP] $ftp 2 attach-agent $tcp 2 $ftp 2 set type_ FTP 4

5 #Schedule events for the FTP agents $ns at 0. 0 "record" $ns at

5 #Schedule events for the FTP agents $ns at 0. 0 "record" $ns at 00. 0 "$ftp 1 start" $ns at 20. 0 "$ftp 1 stop" $ns at 00. 0 "$ftp 2 start" $ns at 20. 0 "$ftp 2 stop" #Call the finish procedure after 5 seconds of simulation time $ns at 20. 0 "finish" #Run the simulation $ns run

Example 3

Example 3

#Create a simulator object set ns [new Simulator] #Define different colors for data flows

#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 n 0 [$ns node] set n 1 [$ns node] set

#Create four nodes set n 0 [$ns node] set n 1 [$ns node] set n 2 [$ns node] set n 3 [$ns node] #Create links between the nodes $ns duplex-link $n 0 $n 2 2 Mb 10 ms Drop. Tail $ns duplex-link $n 1 $n 2 2 Mb 10 ms Drop. Tail $ns duplex-link $n 2 $n 3 1. 7 Mb 20 ms Drop. Tail #Set Queue Size of link (n 2 -n 3) to 10 $ns queue-limit $n 2 $n 3 10 #Give node position (for NAM) $ns duplex-link-op $n 0 $n 2 orient right-down $ns duplex-link-op $n 1 $n 2 orient right-up $ns duplex-link-op $n 2 $n 3 orient right #Monitor the queue for link (n 2 -n 3). (for NAM) $ns duplex-link-op $n 2 $n 3 queue. Pos 0. 5

#Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent

#Setup a TCP connection set tcp [new Agent/TCP] $tcp set class_ 2 $ns attach-agent $n 0 $tcp set sink [new Agent/TCPSink] $ns attach-agent $n 3 $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 $n 1 $udp set

#Setup a UDP connection set udp [new Agent/UDP] $ns attach-agent $n 1 $udp set null [new Agent/Null] $ns attach-agent $n 3 $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_ 1 mb $cbr set random_ false

#Schedule events for the CBR and FTP agents $ns at 0. 1 "$cbr start"

#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 $n 0 $tcp ; $ns detach-agent $n 3 $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

Trace File Format and Output Trace File + 0. 1 1 2 cbr 1000

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. . . . .

[End-to-End Delay] BEGIN { #程式初始化,設定一變數以記錄目前最高處理封包ID。 highest_packet_id = 0; } { action = $1; time

[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 %fn", start, packet_duration); } }

執行方法: ($為shell的提示符號) $awk -f measure-delay. awk out. tr 若是要把結果存到檔案,可使用導向的方式。(把結果存到cbr_delay檔案中) $awk -f measure-delay. awk out.

執行方法: ($為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] BEGIN { END { #程式初始化, 設定一變數記錄packet被drop的數目 printf("number of packets sent: %d lost: %dn",

[Loss] BEGIN { END { #程式初始化, 設定一變數記錄packet被drop的數目 printf("number of packets sent: %d lost: %dn", num. Fs, fs. Drops); fs. Drops = 0; num. Fs = 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; #統計從n 1送出多少packets if (node_1==1 && node_2==2 && action == "+") num. Fs++; #統計flow_id為 2, 且被drop的封包 執行方法: ($為shell的提示符號) if (flow_id==2 && action == "d") fs. Drops++; } $awk -f measure-drop. awk out. tr 執行結果: number of packets sent: 550 lost: 8 這代表CBR送出了550個封包,但其中 8個封包丟掉了。