ns tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 [email protected]...

25
Ns Tutorial 成成成成成成成成成成成成成 成成成成成 成成成 [email protected] http://140.116.72.80/~smallko http://140.116.72.80/~smallko/ns2/ns2.htm

Upload: deshawn-field

Post on 14-Dec-2015

231 views

Category:

Documents


5 download

TRANSCRIPT

Page 1: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

Ns Tutorial

成功大學電機所電腦與網路組 博士候選人 柯志亨[email protected]

http://140.116.72.80/~smallko

http://140.116.72.80/~smallko/ns2/ns2.htm

Page 2: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

The Network Simulator - ns-2

http://www.isi.edu/nsnam/ns/ NS2 is a discrete event simulator targeted at networking re

search 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, routi

ng, and multicast protocols over wired and wireless (local and satellite) networks

Page 3: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

Documentation

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

nd Documentation") ns by Example Practical Programming in Tcl and Tk (http://www.

beedub.com/book/)

http://140.116.72.80/~smallko/ns2/ns2.htm

Page 4: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

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 / 41.8

Math Expressions

Page 5: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Create a simulator objectset ns [new Simulator]

#Define a 'finish' procedureproc 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

Page 6: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Create a simulator objectset ns [new Simulator]

#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all $nf

#Define a 'finish' procedureproc 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 1 The first Tcl script

Page 7: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Create two nodesset 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 n0set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0

# Create a CBR traffic source and attach it to udp0set 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 n1set 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

Page 8: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#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

Page 9: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Create a simulator objectset 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.02set file1 [open file1.ns w]set file2 [open file2.ns w]

#Define a 'finish' procedureproc 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 connections1

Page 10: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

proc record {} {global ns tcp1 file1 tcp2 file2 statevar record_interval

#Set the time after which the procedure should be called againset 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

Page 11: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Create four nodesset 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

Page 12: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Setup two TCP connectionsset tcp1 [new Agent/TCP/Reno]$ns attach-agent $n0 $tcp1set 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 $tcp2set sink2 [new Agent/TCPSink]$ns attach-agent $n2 $sink2$ns connect $tcp2 $sink2$tcp2 set window_ 64

#Setup two FTP over TCP connectionsset 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

Page 13: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#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

Page 14: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm
Page 15: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

Example 3

Page 16: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Create a simulator objectset ns [new Simulator]

#Define different colors for data flows (for NAM)$ns color 1 Blue$ns color 2 Red

#Open the NAM trace fileset nf [open out.nam w]$ns namtrace-all $nf

set nd [open out.tr w]$ns trace-all $nd

#Define a 'finish' procedureproc 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}

Page 17: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Create four nodesset 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

Page 18: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Setup a TCP connectionset tcp [new Agent/TCP]$tcp set class_ 2$ns attach-agent $n0 $tcpset sink [new Agent/TCPSink]$ns attach-agent $n3 $sink$ns connect $tcp $sink$tcp set fid_ 1

#Setup a FTP over TCP connectionset ftp [new Application/FTP]$ftp attach-agent $tcp$ftp set type_ FTP

Page 19: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#Setup a UDP connectionset udp [new Agent/UDP]$ns attach-agent $n1 $udpset null [new Agent/Null]$ns attach-agent $n3 $null$ns connect $udp $null$udp set fid_ 2

#Setup a CBR over UDP connectionset 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

Page 20: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

#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 intervalputs "CBR packet size = [$cbr set packet_size_]"puts "CBR interval = [$cbr set interval_]"

#Run the simulation$ns run

Page 21: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

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

............................................................................

Page 22: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

[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); }}

Page 23: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

執行方法 : ($ 為 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 ………………………

Page 24: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm
Page 25: Ns Tutorial 成功大學電機所電腦與網路組 博士候選人 柯志亨 smallko@ee.ncku.edu.tw smallko smallko/ns2/ns2.htm

[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 個封包丟掉了。