Presentation is loading. Please wait.

Presentation is loading. Please wait.

NS2 - Basic Ming-Feng Yang.

Similar presentations


Presentation on theme: "NS2 - Basic Ming-Feng Yang."— Presentation transcript:

1 NS2 - Basic Ming-Feng Yang

2 Outline NS2 Elements Example 1-1 Example 1-2 Example 2-1 Example 2-2
Example 3 - Static Routing Example 4 - Dynamic Routing

3 NS2 Elements set ns [new Simulator] set node_ [$ns node]
目的在創造一個 NS2 的模擬物件,主要的功能為初始化封包格式並建立一個 Scheduler set node_ [$ns node] 建立一個名稱叫做 node_ 的 Node for {set i 0} {$i < 30} {incr i} { set n($i) [$ns node] } #利用 TCL 迴圈,建立 30 個 Nodes

4 NS2 Elements $ns simplex-link < n0 > < n1 > < bandwidth > < delay > < queue_type > 建立一條 Node n0 到 n1 間的 physical Link,並設定頻寬、delay 時間和 queue 型態(DropTail、FQ、SFQ、DRR、RED、CBQ、CBQ/WRR …) $ns simplex-link $n0 $n1 2Mb 20ms DropTail # 在 n0 及 n1 間建立一個頻寬為 2Mb,DropTail queue 的 Link $ns duplex-link < n0 > < n1 > < bandwidth > < delay > < queue_type > 同前範例,但為建立一條 duplex link 的Link

5 NS2 Elements $ns attache-agent < node > < agent >
將一個 Agent 結合到一個 Node 上,Agent 表示一個 Node 上所用的 protocol set tcp [new Agent/TCP] # 建立一個TCP的Agent $ns attach-agent $n0 $tcp # 將TCP Agent 結合到 Node n0 set ftp [new Application/FTP] $ftp attach-agent $tcp # 再建立 Application protocol(FTP)於 TCP 上,以產生Traffic

6 NS2 Elements $ns connect < agent1 > < agent2 >
在兩個 Agent 中建立一條 logical 的連結,agent1 和 agent2 之間可能相隔好幾個點

7 NS2 Elements $ns trace-all < tracefile >
將 NS2的模擬結果寫至 < tracefile > 檔案中 此指令應置於 Node 和 Link 的建立之前,以避免模擬結果無法完整寫回檔案 set nf [open out.tr w] $ns trace-all $nf $ns namtrace-all < tracefile > 同樣將 NS2 的模擬結果寫至 < tracefile > 檔案中,但可以用於 NAM 來顯示模擬動畫(格式與 trace-all 不同)

8 NS2 Elements $ns at < time > < event > $ns run
$ns at 4.5 "$ftp start“ # 在4.5秒的時候執行 ftp $ns at 5.0 "finish“ # 在5秒時候執行自行定義的 finish 程序 $ns run 開始執行 scheduler

9 Example 1-1

10 Example 1-1 set ns [new Simulator] # 建立一個 NS2 模擬的物件
set nf [open out.nam w] $ns namtrace-all $nf # 建立一個 NAM 所能執行的 trace file set n0 [$ns node] set n1 [$ns node] # 建立二個 Nodes $ns duplex-link $n0 $n1 1Mb 10ms DropTail # 建立Node 和 Node 之間的Link

11 Example 1-1 proc finish {} { global ns nf $ns flush-trace
close $nf ;# 關閉 NAM trace file exec nam out.nam & ;# 將 trace file 代入 NAM 中執行 exit 0 } # 定義一個叫做 “finish” 的程序 $ns at 5.0 “finish” # 在5秒時候呼叫 “finish” 程序來結束 NS2 的模擬 $ns run # 開始執行模擬

12 Example 1-2

13 Example 1-2 set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0
set null0 [new Agent/Null] $ns attach-agent $n1 $null0 $ns connect $udp0 $null0 # 建立一個 UDP Agent 和一個 NULL Agent 並作連結

14 Example 1-2 set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0 $cbr0 set packet_size_ 1000 $cbr0 set rate_ 1mb # 在 UDP Agent 加上一個 CBR Agent $ns at 0.5 “$cbr0 start” $ns at 4.5 “$cbr0 stop” # CBR Agent 排程

15 Example 2-1

16 Example 2-1 set n0 [$ns node] set n1 [$ns node] set n2 [$ns node]
$ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms DropTail # 建立 Node 和 Node 之間的Links

17 Example 2-1 $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 # 設定在 NAM 中每個 Node 所在的相關位置

18 Example 2-2

19 Example 2-2

20 Example 2-2 $ns color 1 Blue $ns color 2 Red # 定義 NAM 中 Traffic 的顏色
$ns duplex-link-op $n2 $n3 queuePos 0.5 # 設定 Node 2 與 Node 3 之間 Link queue 的位置

21 Example 2-2 set tcp [new Agent/TCP] $ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink] $ns attach-agent $n3 $sink $ns connect $tcp $sink $tcp set fid_ 1 # 建立一個 TCP Agent 和一個 TCPSink Agent 並作連結 set ftp [new Application/FTP] $ftp attach-agent $tcp #在 TCP Agent 加上一個 FTP Agent

22 Example 2-2 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 # 建立一個 UDP Agent 和一個 NULL Agent 並作連結 set cbr [new Application/Traffic/CBR] $cbr attach-agent $udp $cbr set packet_size_ 1000 $cbr set rate_ 1mb # 在 UDP Agent 加上一個 CBR Agent

23 Example 2-2 $ns at 0.1 "$cbr start" $ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop" $ns at 4.5 "$cbr stop" # CBR&FTP Traffics 排程

24 Example 3 - Static Routing
10 10 10 10 1 2 5

25 Example 3 - Static Routing
10 10 10 10 1 2 5

26 Example 3 - Static Routing
10 10 10 10 1 2 5

27 Example 3 - Static Routing
10 10 10 10 1 2 5

28 Example 3 - Static Routing
for {set i 0} {$i < 6} {incr i} { set n($i) [$ns node] } # 建立六個 Nodes for {set i 0 } {$i < 6} {incr i} { $ns duplex-link $n($i) $n([expr ($i+1)%6]) 1Mb 10ms DropTail } $ns duplex-link $n(0) $n(4) 1Mb 10ms DropTail # 建立 Node 和 Node 之間的Links $ns cost $n(0) $n(1) 10 $ns cost $n(1) $n(0) 10 $ns cost $n(1) $n(2) 1 $ns cost $n(2) $n(1) 1

29 Example 3 - Static Routing
$ns cost $n(2) $n(3) 5 $ns cost $n(3) $n(2) 5 $ns cost $n(3) $n(4) 2 $ns cost $n(4) $n(3) 2 $ns cost $n(4) $n(5) 10 $ns cost $n(5) $n(4) 10 $ns cost $n(5) $n(0) 10 $ns cost $n(0) $n(5) 10 $ns cost $n(4) $n(0) 10 $ns cost $n(0) $n(4) 10 # 設定每一條 Link 的 cost

30 Example 3 - Static Routing
set udp0 [new Agent/UDP] $ns attach-agent $n(0) $udp0 # 在 Node n(0) 上建立 UDP Agent $udp0 set fid_ 0 # 設定 flow id為 0 $ns color 0 darkgreen # 設定 flow id = 0 的封包顏色

31 Example 3 - Static Routing
set udp1 [new Agent/UDP] $ns attach-agent $n(1) $udp1 # 在 Node n(1) 上建立 UDP Agent $udp1 set fid_ 1 # 設定 flow id為 1 $ns color 1 red # 設定 flow id = 1 的封包顏色 set null4 [new Agent/Null] $ns attach-agent $n(4) $null4 #在 Node n(4) 上建立 NULL Agent

32 Example 3 - Static Routing
$ns connect $udp0 $null4 $ns connect $udp1 $null4 # 連結UDP Agent & NULL Agent set cbr0 [new Application/Traffic/CBR] $cbr0 attach-agent $udp0 # 在node n(0)上產生CBR Traffic set cbr1 [new Application/Traffic/CBR] $cbr1 attach-agent $udp1 # 在node n(1)上產生CBR Traffic

33 Example 3 - Static Routing
$ns at 0.1 "$cbr0 start" $ns at 0.3 "$cbr1 start" # CBR Traffics 排程 $ns rtmodel-at 0.7 down $n(0) $n(4) # 0.7 秒時 Node n(0) 到 Node n(4) 間的 Link 斷掉 $ns rtmodel-at 1 up $n(0) $n(4) # 1 秒時 Node n(0) 到 Node n(4) 間的 Link 恢復

34 Example 4 - Dynamic Routing
10 10 10 10 1 2 5

35 Example 4 - Dynamic Routing
10 10 10 10 1 2 5

36 Example 4 - Dynamic Routing
$ns rtproto DV # 告訴 NS2 使用 Distance Vector routing(dynamic routing)


Download ppt "NS2 - Basic Ming-Feng Yang."

Similar presentations


Ads by Google