network simulator2

58
Network Simulator2 OTCL Analysis

Upload: richard-rivers

Post on 30-Dec-2015

30 views

Category:

Documents


1 download

DESCRIPTION

Network Simulator2. OTCL Analysis. Outline. Basic OTCL Introduction Simple.tcl Simple-wireless.tcl Trace File Analysis Reference. NS2. Topology (Use OTCL). Kernel (Use C++). Topology. OTCL. set x 100 # 設定 x 變數, x 值為 100( 注意這 100 是字串 ) set y 200 # 設定 y 變數, y 值為 200 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Network Simulator2

Network Simulator2

OTCL Analysis

Page 2: Network Simulator2

Outline

Basic OTCL Introduction Simple.tcl Simple-wireless.tcl Trace File Analysis Reference

Page 3: Network Simulator2

NS2

Kernel(Use C++)

Topology(Use OTCL)

Page 4: Network Simulator2

Topology

Page 5: Network Simulator2

OTCL set x 100

# 設定 x 變數, x 值為 100( 注意這 100 是字串 ) set y 200

# 設定 y 變數, y 值為 200 set z [expr $x+$y]

# 透過 expr 將 $x $y 當成數字作數學運算 , 並設定 z 變數為 300 set a [set b 100]

# 設定 a = b = 100 set array_(1) 21

# 設定一個陣列叫 array_, 並把 array_(1) 的值設為 27

Page 6: Network Simulator2

OTCL if

if { $k>4 } { puts " k > 4 " } else { puts " k < = 4 " }

while 下面程式 , 代表一個 while 如在 i 大於等於 0 的情況下 , 則將 b

的值和 i 相加並再回傳給 b, 然後 i 再減 1. set b 0 set i 100 while {$i > = 0} { set b [expr $b+$i] incr i -1

}

Page 7: Network Simulator2

OTCL for

for {set i 100} {$i > =0} {incr i -1} { # for 迴圈內所要執行的程式碼 }

副程式 # 定義一個叫做 show 的 procedure proc show {} { ... # 副程式內容 ... }

Page 8: Network Simulator2

NAM Nam 是一個能將 NS2 模擬結果視覺化顯示出來的工

具 , 他能顯示封包的流向和 Drop 等資訊 . 執行方式 :

nam < trace-file >

Page 9: Network Simulator2

NAM

$node color red # 設定 node 顏色

$node shape square (circle, square, and hexagon) # 設定 node 形狀 ( 預設圓形 )

$node label "Text“ # 設定 node 的標籤

$node label-color blue # 設定 node 標籤的顏色

Page 10: Network Simulator2

NAM

$ns duplex-link-op $n1 $n2 color green # 設定 Link 顏色

$ns duplex-link-op $n1 $n2 label "Text" # 設定 Link 的標籤

$ns duplex-link-op $n1 $n2 label-color blue # 設定 Link 標籤的顏色

Page 11: Network Simulator2

Basic NS2 語法 set ns [new Simulator]

目的在創造一個 NS2 模擬的物件 , 只要的功能在 1. 初使化封包格式 ( packet format) 2. 創造一個 Scheduler

set node_ [$ns node] 建立一個名稱叫做 node_ 的 Node # 建立 30 個 Nodes for {set i 0} {$i < 30} {incr i} { set n($i) [$ns node] }

$ns simplex-link < n0 > < n1 > < bandwidth > < delay > < queue_type > 建立一條 Node n0 到 n1 的一條實體連結 , 並設定頻寬、 delay 時間和 q

ueue 的 type, queue 的 type 有 DropTail(a FIFO queue) 、 FQ 、 SFQ 、DRR 、 RED 、 CBQ 、 CBQ/WRR 等 type

$ns duplex-link $n0 $n1 2Mb 20ms DropTail # 在 n0 及 n1 間建立一個頻寬為 2Mb, DropTail Queue 的 Link

Page 12: Network Simulator2

Basic NS2 語法 $ns duplex-link < n0 > < n1 > < bandwidth > < delay > < queue_ty

pe > 同上 , 不過是建立一條 duplex link 的連線

$ns attache-agent < node > < agent > 將一個 agent 結合到一個 node 上 , agent 簡單來說也就表示一個 node

上所用的 protocol, 而一開始建立一個 Node 預設的 agent 是 Null. 範例如下 :

# 創造一個 TCP 的 Agent set tcp [new Agent/TCP]

#TCP agent 結合到 node(n0) $ns attach-agent $n0 $tcp

# 但就此範例光是 TCP 無法產生任何 Traffic, 所以通常我們都會再建立一些 Application 的 Protocol 於 TCP 上 ( 如 FTP 、 Telnet)

set ftp [new Application/FTP] $ftp attach-agent $tcp

Page 13: Network Simulator2

Basic NS2 語法 $ns connect < agent1 > < agent2 >

在兩個 agent 中建立一條 logical 的連結 , 不同於 Simplex-link 等方式所建立的實體連結 , 如 agent1 和 agent2 之間可能相隔好幾個點

$ns trace-all < tracefile > 將 ns2 模擬的內容寫回到在 < tracefile > 檔案中 . 範例如下 : 建議此指令最好放在程式的前面 ( 在建立 node 和 link 之前 ), 以免模擬結

果無法完整寫回檔案 set nf [open out.tr w] $ns trace-all $nf

$ns namtrace-all < tracefile > 同樣是將 ns2 模擬的內容寫回到在 < tracefile > 檔案中 , 不過可以放在 n

am 上去顯示模擬畫面 , 格式也不太一樣

Page 14: Network Simulator2

Basic NS2 語法 $ns at < time > < event >

在特定的時間 < time > 讓這個事件 < event > 被執行 . 範例如下 : # 在 4.5 秒的時候執行 ftp $ns at 4.5 "$ftp start" # 在 5 秒時候執行我們自己所定義的 finish 函式 $ns at 5.0 "finish“

$ns run 開始執行 scheduler

Page 15: Network Simulator2

Basic 範例 Two nodes, one link

set n0 [$ns node]set n1 [$ns node]$ns duplex-link $n0 $n1 1Mb 10ms DropTail

Page 16: Network Simulator2
Page 17: Network Simulator2

Basic 範例 #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 udp0set cbr0 [new Application/Traffic/CBR]$cbr0 set packetSize_ 500$cbr0 set interval_ 0.005$cbr0 attach-agent $udp0

Page 18: Network Simulator2

Basic 範例 #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

#Schedule events for the CBR agent$ns at 0.5 "$cbr0 start"$ns at 4.5 "$cbr0 stop"

Page 19: Network Simulator2
Page 20: Network Simulator2

Basic 範例 #Create links between the nodes

$ns duplex-link $n0 $n2 1Mb 10ms DropTail$ns duplex-link $n1 $n2 1Mb 10ms DropTail$ns duplex-link $n3 $n2 1Mb 10ms SFQ

SFQ (stochastic fair queueing)

$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

Page 21: Network Simulator2
Page 22: Network Simulator2

Basic 範例 #Define different colors for data flows

$ns color 1 Blue$ns color 2 Red$udp0 set class_ 1 $udp1 set class_ 2

#Monitor the queue for the link between node 2 and node 3$ns duplex-link-op $n2 $n3 queuePos 0.5

Page 23: Network Simulator2

Basic 範例 #Connect the traffic sources with the traffic

sink$ns connect $udp0 $null0 $ns connect $udp1 $null0

#Schedule events for the CBR agents

$ns at 0.5 "$cbr0 start"

$ns at 1.0 "$cbr1 start"$ns at 4.0 "$cbr1 stop"$ns at 4.5 "$cbr0 stop"

Page 24: Network Simulator2
Page 25: Network Simulator2
Page 26: Network Simulator2

Simple.tcl

set ns [new Simulator]$ns color 0 blue$ns color 1 red$ns color 2 whiteset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]

Simulator Object

Packet color

Create four nodes

Page 27: Network Simulator2

Simple.tcl (con.)

set f [open out.tr w]$ns trace-all $fset nf [open out.nam w]$ns namtrace-all $nf

Trace file name

NAM file name

Page 28: Network Simulator2

Simple.tcl (con.)

$ns duplex-link $n0 $n2 5Mb 2ms DropTail$ns duplex-link $n1 $n2 5Mb 2ms DropTail$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail$ns duplex-link-op $n0 $n2 orient right-up$ns duplex-link-op $n1 $n2 orient right-down$ns duplex-link-op $n2 $n3 orient right$ns duplex-link-op $n2 $n3 queuePos 0.5

Create three links

Place links location

Page 29: Network Simulator2
Page 30: Network Simulator2

Simple.tcl (con.)

set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0

set udp1 [new Agent/UDP]$ns attach-agent $n3 $udp1$udp1 set class_ 1set cbr1 [new Application/Traffic/CBR]$cbr1 attach-agent $udp1

Create UDP traffic and CBR source node

Create UDP traffic and CBR source node

Page 31: Network Simulator2

Simple.tcl (con.)

set null0 [new Agent/Null]$ns attach-agent $n3 $null0

set null1 [new Agent/Null]$ns attach-agent $n1 $null1

Create UDP traffic sink node

Create UDP traffic sink node

Page 32: Network Simulator2

Simple.tcl (con.)

$ns connect $udp0 $null0$ns connect $udp1 $null1

Connect source node and sink node

Page 33: Network Simulator2

Simple.tcl (con.)

$ns at 1.0 "$cbr0 start“$ns at 1.1 "$cbr1 start"

Set time to forward packet traffic

Page 34: Network Simulator2

Simple.tcl (con.)

set tcp [new Agent/TCP]$tcp set class_ 2set sink [new Agent/TCPSink]$ns attach-agent $n0 $tcp$ns attach-agent $n3 $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 1.2 "$ftp start"

Create TCP traffic and FTP

Page 35: Network Simulator2

Simple.tcl (con.)

$ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink“

puts [$cbr0 set packetSize_]puts [$cbr0 set interval_]$ns at 3.0 "finish"

Traffic End

Page 36: Network Simulator2

Simple.tcl (con.)

proc finish {} { global ns f nf $ns flush-trace close $f close $nf

puts "running nam..." exec nam out.nam & exit 0}

$ns run

Record traffic in file

Execute NAM

Execute NS

Page 37: Network Simulator2

Simple-wireless.tcl C:\cygwin\home\smallfirefly\ns-allino

ne-2.26\ns-2.26\tcl\ex Simple-wireless.tcl

Page 38: Network Simulator2

Basic – create node

set $node [$ns node]

Create Node

$node set X_ <x1>

$node set Y_ <y1>

$node set Z_ <z1>

Start-position

Page 39: Network Simulator2

Basic – node movement

$ns_ at $time $node setdest <x2> <y2> <speed>

Future destinations

Page 40: Network Simulator2

Basic OTCL

set ns_ [new Simulator]

set tracefd [open simple.tr w]

$ns_ trace-all $tracefd

set namtrace [open simple.nam w]

$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)

Simulator Object

Trace file name

NAM file name

Page 41: Network Simulator2

Basic – creating wireless topology

set topo [new Topography]

$topo load_flatgrid $opt(x) $opt(y)

where opt(x) and opt(y) are the boundaries used in simulation.

create-god $val(nn) Number of node

Page 42: Network Simulator2

Basic – node config$ns_ node-config -adhocRouting DSDV or DSR or TORA or AODV \ -topoInstance $topo \ -addressType hierarchical \ -wiredRouting ON \ -mobileIP ON \ -llType LL \ -macType Mac/802_11 \ -antType Antenna/OmniAntenna \ -propType Propagation/TwoRayGround \ -phyType Phy/WirelessPhy \ -channelType Channel/WirelessChannel \ -ifqType Queue/DropTail/PriQueue \ -ifqLen <integer> \

Wired-cum-wirelessMobileIP

Page 43: Network Simulator2

Basic – node config (con.) -agentTrace ON or OFF \ -routerTrace ON or OFF \ -macTrace ON or OFF \ -movementTrace ON or OFF

Page 44: Network Simulator2

Simple-wireless.tcl (con.)for {set i 0} {$i < $val(nn) } {incr i} { set node_($i) [$ns_ node] $node_($i) random-motion 0;# disable random moti

on}$node_(0) set X_ 5.0$node_(0) set Y_ 2.0$node_(0) set Z_ 0.0

$node_(1) set X_ 390.0$node_(1) set Y_ 385.0$node_(1) set Z_ 0.0

Page 45: Network Simulator2

Simple-wireless.tcl (con.)## Now produce some simple node movements# Node_(1) starts to move towards node_(0)#$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"

# Node_(1) then starts to move away from node_(0)$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"

Page 46: Network Simulator2

Simple-wireless.tcl (con.)# Setup traffic flow between nodes# TCP connections between node_(0) and node_(1)set tcp [new Agent/TCP]$tcp set class_ 2set sink [new Agent/TCPSink]$ns_ attach-agent $node_(0) $tcp$ns_ attach-agent $node_(1) $sink$ns_ connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns_ at 10.0 "$ftp start"

Page 47: Network Simulator2

Simple-wireless.tcl (con.)## Tell nodes when the simulation ends#for {set i 0} {$i < $val(nn) } {incr i} { $ns_ at 150.0 "$node_($i) reset";}$ns_ at 150.0 "stop"$ns_ at 150.01 "puts \"NS EXITING...\" ; $ns_ halt"

Page 48: Network Simulator2

Simple-wireless.tcl (con.)proc stop {} { global ns_ tracefd $ns_ flush-trace close $tracefd}

puts "Starting Simulation..."$ns_ run

Page 49: Network Simulator2

Basic Create Node

for {set i 0} {$i < $val(nn)} {incr i}{ $node_($i) set X_ [expr { $val(x)*rand() } ]

$node_($i) set Y_ [expr { $val(y)*rand() } ]

$node_($i) set Z_ 0.0

$node_($i) radius 500}

Random initial node position

for {set i 0} {$i < $val(nn)} {incr i} { set node_($i) [$ns_ node]}

Only wireless node

Page 50: Network Simulator2

Node

Initial node size for {set i 0} {$i < $val(nn)} {incr i} { $ns_ initial_node_pos $node_($i) 50;}

Link $ns duplex-link $n0 $n2 2Mb 10ms DropTail $node_($i) radius 500

Page 51: Network Simulator2

Trace File

Page 52: Network Simulator2

Trace File

1. 代表事件的類別 r:代表目的端收到 packet + :代表 packet 放入 queue 中 -:代表 packet 從 queue 中取出 d:代表 queue 已經滿了,這個 packet

被 drop 掉

Page 53: Network Simulator2

Trace File

2. 代表事件發生的時間

3. 代表 packet 的 source node

4. 代表 packet 的 destination node

Page 54: Network Simulator2

Trace File

5. 代表 packet 的類別 (TCP or CBR)

6. 代表 packet 的大小 (encoded in IP head

er) 7.

代表 packet 的 flags

Page 55: Network Simulator2

Trace File

8. 代表 connection(flow) 的 id

9. 代表 source address ( node.port )

10. 代表 destinations address ( node.port )

Page 56: Network Simulator2

Trace File

11. 代表 packet 的 sequence number ( netw

ork layer protocol's ) 12.

代表 packet 的 id ( unique )

Page 57: Network Simulator2

Reference Cygwin

http://cygwin.com/ Cygwin+NS2安裝

http://www.sims.berkeley.edu/~christin/ns-cygwin.shtml

NS2基本語法教學 ( 中文 ) http://netlab.cse.yzu.edu.tw/ns2/ns2_website/

華玄明 http://ns2.mis.must.edu.tw/index.htm

Page 58: Network Simulator2

Reference (con.)

File download FTP

140.136.206.252 Port:21 Name: ns2 Password: ns2123