580 likes | 693 Views
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
E N D
Network Simulator2 OTCL Analysis
Outline • Basic OTCL Introduction • Simple.tcl • Simple-wireless.tcl • Trace File Analysis • Reference
NS2 Topology (Use OTCL) Kernel (Use C++)
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
OTCL • if • if { $k>4 } { puts " k > 4 " }else { puts " k < = 4 " } • while • 下面程式, 代表一個 while 如在 i 大於等於0的情況下, 則將 b 的值和 i 相加並再回傳給 b, 然後 i 再減 1. • set b 0set i 100while {$i > = 0} { set b [expr $b+$i]incr i -1 }
OTCL • for • for {set i 100} {$i > =0} {incr i -1} { # for 迴圈內所要執行的程式碼 } • 副程式 • # 定義一個叫做 show 的 procedure • proc show {} { ... # 副程式內容 ... }
NAM • Nam 是一個能將 NS2 模擬結果視覺化顯示出來的工具, 他能顯示封包的流向和 Drop 等資訊. • 執行方式: nam < trace-file >
NAM • $node color red • # 設定 node 顏色 • $node shape square (circle, square, and hexagon) • # 設定 node 形狀(預設圓形) • $node label "Text“ • # 設定 node 的標籤 • $node label-color blue • # 設定 node 標籤的顏色
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 標籤的顏色
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 時間和 queue 的 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
Basic NS2語法 • $ns duplex-link < n0 > < n1 > < bandwidth > < delay > < queue_type > • 同上, 不過是建立一條 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
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 > 檔案中, 不過可以放在 nam 上去顯示模擬畫面, 格式也不太一樣
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
Basic範例 • Two nodes, one link set n0 [$ns node] set n1 [$ns node] $ns duplex-link $n0 $n1 1Mb 10ms DropTail
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 udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 set packetSize_ 500 $cbr0 set interval_ 0.005 $cbr0 attach-agent $udp0
Basic範例 • #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 • #Schedule events for the CBR agent $ns at 0.5 "$cbr0 start" $ns at 4.5 "$cbr0 stop"
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
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
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"
SimulatorObject Packet color Create four nodes Simple.tcl set ns [new Simulator] $ns color 0 blue $ns color 1 red $ns color 2 white set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]
Trace file name NAM file name Simple.tcl (con.) set f [open out.tr w] $ns trace-all $f set nf [open out.nam w] $ns namtrace-all $nf
Create three links Place links location 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 UDP traffic and CBR source node Create UDP traffic and CBR source node Simple.tcl (con.) set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp0 set cbr0 [new Application/Traffic/CBR] $cbr0 attach-agent $udp0 set udp1 [new Agent/UDP] $ns attach-agent $n3 $udp1 $udp1 set class_ 1 set cbr1 [new Application/Traffic/CBR] $cbr1 attach-agent $udp1
Create UDP traffic sink node Create UDP traffic sink node Simple.tcl (con.) set null0 [new Agent/Null] $ns attach-agent $n3 $null0 set null1 [new Agent/Null] $ns attach-agent $n1 $null1
Connect source node and sink node Simple.tcl (con.) $ns connect $udp0 $null0 $ns connect $udp1 $null1
Set time to forward packet traffic Simple.tcl (con.) $ns at 1.0 "$cbr0 start“ $ns at 1.1 "$cbr1 start"
Create TCP traffic and FTP Simple.tcl (con.) set tcp [new Agent/TCP] $tcp set class_ 2 set sink [new Agent/TCPSink] $ns attach-agent $n0 $tcp $ns attach-agent $n3 $sink $ns connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns at 1.2 "$ftp start"
Traffic End 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"
Execute NAM Execute NS Record traffic in file 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
Simple-wireless.tcl • C:\cygwin\home\smallfirefly\ns-allinone-2.26\ns-2.26\tcl\ex • Simple-wireless.tcl
Basic – create node • Create Node set $node [$ns node] • Start-position $node set X_ <x1> $node set Y_ <y1> $node set Z_ <z1>
Basic – node movement • Future destinations $ns_ at $time $node setdest <x2> <y2> <speed>
SimulatorObject Trace file name NAM file name 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)
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
Wired-cum-wireless MobileIP 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> \
Basic – node config (con.) -agentTrace ON or OFF \ -routerTrace ON or OFF \ -macTrace ON or OFF \ -movementTrace ON or OFF
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 motion } $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
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"
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_ 2 set sink [new Agent/TCPSink] $ns_ attach-agent $node_(0) $tcp $ns_ attach-agent $node_(1) $sink $ns_ connect $tcp $sink set ftp [new Application/FTP] $ftp attach-agent $tcp $ns_ at 10.0 "$ftp start"
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"
Simple-wireless.tcl (con.) proc stop {} { global ns_ tracefd $ns_ flush-trace close $tracefd } puts "Starting Simulation..." $ns_ run
Basic • Create Node for{set i 0} {$i < $val(nn)} {incr i} { set node_($i) [$ns_ node]} • Random initial node position 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} Only wireless node
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