71
NS2 [email protected] 2011/07/22

8. NS2 - 1

Embed Size (px)

Citation preview

Page 2: 8. NS2 - 1

NS2

Network Simulator (Open Source)

OSI Layers

Packet-Level

Wired and WirelessEthernet, 802.11, WiMAX, etc.

Page 3: 8. NS2 - 1

NS2 安裝

首先,先從官方網站下載 Ns-allinone版本  NS-2 Simulator Download

下載好的檔案放在 home的根目錄中進入 terminal輸入 su命令並輸入管理員密碼,切換到管理員模式注意在開始安裝所需之 patch前要確定你的 linux要能上網在安裝 patch時,遇到【 Y/N】都選擇 “ y”

Page 4: 8. NS2 - 1

NS2 安裝

安裝 patchyum install gcc

yum install tcl-devel

yum install autoconf

yum install automake

yum install gcc-c++

yum install libX11-devel

yum install xorg-x11-proto-devel

yum install libXt-devel

yum install libXmu-devel

yum install libtool

Page 5: 8. NS2 - 1

NS2 安裝

把剛剛下載的 Ns-allinone解壓檔 到 home的根目錄解壓縮

tar -xvf ns-allinone-2.34.tar.gz

進入解完壓縮的目錄cd ns-allinone-2.34/

開始安裝./install

Page 6: 8. NS2 - 1

NS2 安裝

在 Fedora 14下安裝 NS2一般都不會太順利,會有些錯誤提示出現因為 gcc版本更新後對內部函數簡化所造成的不相容

正常情況下,第一次會出現的錯誤為:tools/ranvar.cc: in member function `virtual double gammarandomvariable::value()’:tools/ranvar.cc:219:70: error: cannot call constructor `GammaRandomVariable::GammaRandomVariable’tools/ranvar.cc:219:70: error: for a function-style cast, remove the redundant`::GaammaRandomVariable’make: *** [tools/ranvar.o] Error 1ns make failed

Page 7: 8. NS2 - 1

NS2 安裝

解決辦法在 ns-allinone-2.34/tools資料夾下,找到錯誤提示的 ranvar.cc檔案,打開並找到對應的 219行進行刪除(:GaammaRandomVariable),並存檔,重新再根目錄下打入 ./install 進行第二次安裝,等再次出現相似問題時,仿照剛剛所說的方法解決直到安裝成功

安裝成功時所出現的訊息Please put /opt/ns-allinone-2.34/bin:/opt/ns-allinone-2.34/tcl8.4.18/unix:/opt/ns-allinone-2.34/tk8.4.18/unix into your PATH environment; so that you'll be able to run itm/tclsh/wish/xgraph. IMPORTANT NOTICES:

1. You MUST put /opt/ns-allinone-2.34/otcl-1.13, /opt/ns-allinone-2.34/lib, into your LD_LIBRARY_PATH environment variable. If it complains about X libraries, add path to your X libraries into LD_LIBRARY_PATH. If you are using csh, you can set it like: setenv LD_LIBRARY_PATH If you are using sh, you can set it like: export LD_LIBRARY_PATH=

2. You MUST put /opt/ns-allinone-2.34/tcl8.4.18/library into your TCL_LIBRARY environmental variable. Otherwise ns/nam will complain during startup.

Page 8: 8. NS2 - 1

NS2 安裝

最後設置環境變數PATH=/home/Geo/ns-allinone-2.34/bin:/home/Geo/ns-allinone-2.34/tcl8.4.18/unix:/home/Geo/ns-allinone-2.34/tk8.4.18/unix:$PATH

LD_LIBRARY_PATH=/home/Geo/ns-allinone-2.34/otcl-1.13:/home/Geo/ns-allinone-2.34/lib

TCL_LIBRARY=home/Geo/ns-allinone-2.34/tcl8.4.18/library

輸入 ns指令,出現%則表示安裝完成,輸入 exit退出

Page 9: 8. NS2 - 1

測試 NS2是否能正確執行範例,若正確安裝便可看到圖形介面。測試 simple.tcl檔案到跟目錄下輸入

cd ns-allinone-2.34/ns-2.34/tcl/ex

Ns simple.tcl

NS2 安裝測試

9

Page 10: 8. NS2 - 1

NS2 安裝測試

10

Page 11: 8. NS2 - 1

Compiler in NS2

NS2 use two languagesC++

TCL/OTCL

TCL is an interpreter languageModify and execute again without compiling

Execution speed is slower

C++Modify and execute after compiling

Execution speed is faster

Page 12: 8. NS2 - 1

NS2-Package Components

Main Componentns-2.34\

Virtual Simulatornam-1.14\

TCL Languagesotcl-1.13\

tcl8.4.18\

tclcl-1.19\

tk8.4.18\

Graph toolsxgraph-12.1\

Page 13: 8. NS2 - 1

Main Source Code

ns2.34\Physical and MAC Layer

mac\ 、 mobile\ 、  etc.

Network Layeraodv\ 、  dsdv\ 、  dsr\ 、 etc.

Transport Layertcp\

Application Layerapp\

Page 14: 8. NS2 - 1

Main Source Code

Configuration files (in TCL Language)tcl\

Examples code tcl\ex\

tcl\lib\Some functions written in TCL\OTCL language.

Defaults value. (ns-default.tcl)

Page 15: 8. NS2 - 1

15

TCL語言

TCL(Tool Command Language)在 NS2中主要用來描述腳本,也就是用來描述模擬的網路環境和參數

NS2 Topology Construction

Parameter Initialization

Algorithm Implementation

Traffic Scheduling

Page 16: 8. NS2 - 1

16

簡單的 TCL程式

hello.tcl:

set s "Hello World"

puts $s

Command: ns hello.tcl

Result: Hello World

Page 17: 8. NS2 - 1

17

變數 (variable)的宣告

利用 set來指定變數值Ex.: set a 0

unset取消此變數Ex.: unset a

Page 18: 8. NS2 - 1

18

變數替換 (variable substituion)

變數替換Ex.:

set name “Jacky"

puts "I am $name"

Result:

I am Jacky

Page 19: 8. NS2 - 1

19

表示式 (expressions)

語法 : expr arg ?arg arg ...?

作用 :執行運算Ex.:

set value [expr 2+3]

puts $value

Result: 5

[]代表一個完整指令

Page 20: 8. NS2 - 1

20

incr指令語法 :

incr varName ?increment?

作用 :作加法運算

範例 : set i 0 incr i incr i 4結果 : 0 1 5

Page 21: 8. NS2 - 1

21

append指令

語法 :append varName ?value value value ...?

作用 :在變數後面附加資料

範例 : set i 1

append i 2 3 4 5

結果 : 12345

Page 22: 8. NS2 - 1

22

exec指令

語法 :exec ?switches? arg ?arg ...?

作用 :執行外部程式

範例 : exec umane

結果 : CYGWIN_NT 5-1

Page 23: 8. NS2 - 1

23

list指令

語法 :list ?arg arg ...?

作用 :建立清單 

範例 : list a b “ c d”

結果 : a b { c d}

Page 24: 8. NS2 - 1

24

eval指令

語法 :eval arg ?arg ...?

作用 :將參數當成 script, 用遞迴方式執行

範例 : eval [list set i] 3

結果 : 3

Page 25: 8. NS2 - 1

25

lindex指令

語法 :lindex $list ?index...?

作用 :在 list中抽出索引指定的資料

範例 : lindex {a b c} 2 lindex {a b c} end結果 : c c

Page 26: 8. NS2 - 1

26

linsert指令

語法 :linsert $list index element ?element element ...?

作用 :在指定的位置插入資料

範例 : set aa [list a b c d]

linsert $aa end-1 1 2 3

結果 a b c 1 2 3 d

Page 27: 8. NS2 - 1

27

lappend指令

語法 :lappend varName ?value value value ...?

作用 :在變數值後將引數當 list附加資料

範例 : set var a

lappend var b c d

結果 :

a b c d

Page 28: 8. NS2 - 1

28

concat指令

語法 :concat ?arg arg ...?

作用 :將多個 list合併

範例 : set a [concat "a b" 1 2 {c {d e}}]

lindex $a 5

結果 : d e

Page 29: 8. NS2 - 1

29

llength指令

語法 :llength $list

作用 :計算出 list元素的個數

範例 : llength {a b}

結果 : 2

Page 30: 8. NS2 - 1

30

lrange指令

語法 :lrange $list first last

作用 :在 list中將索引範圍內的元素列出

範例 : lrange {a b c d} 1 end

結果 : b c d

Page 31: 8. NS2 - 1

31

陣列

語法 :array option arrayname ?arg1 arg2 …?

作用 :設定陣列的元素

範例 : set myarray(00) 456 puts $myarray(00)

結果 : 456

Page 32: 8. NS2 - 1

32

option: set指令

語法 :array set arrayname $list

作用 :設定陣列元素

範例 : array set myarray {0 123 00 456 a b}

結果 : 0 123 00 456 a b

Page 33: 8. NS2 - 1

33

TCL之流程控制

If-else

Switch

For loop

Foreach loop

While loop

Page 34: 8. NS2 - 1

34

流程控制範例 (if-else)

Ex.: set grade 90 if {$grade >= 90} {

puts "Your level is A! " } elseif {$grade < 90} {

puts "Your level isn’t A! " } else {

puts "You set wrong data! " }

Result: Your level is A!

一定要空一格其它也一定要

Page 35: 8. NS2 - 1

35

流程控制範例 (switch)

Ex.: set num 1

switch $num {

1 {puts "I’m John."}

2 {puts "I’m Mary."}

3 {puts "I’m Andy."}

default {puts "What’s your name?"}

}

Result: I’m John.

Page 36: 8. NS2 - 1

36

流程控制範例 (for loop)

計算 1+2+…+100

Ex.: set ans 0

for {set a 0} {$a < 101} {incr a 1} {

set ans [expr $ans+$a]

}

puts $ans

Result: 5050

Page 37: 8. NS2 - 1

37

流程控制範例 (foreach loop)

計算 1+2+…+10

Ex.: Set j 0

foreach i {1 2 3 4 5 6 7 8 9 10} {

set j [expr $i+$j]

}

puts $j

Result: 55

Page 38: 8. NS2 - 1

38

流程控制範例 (while loop)

計算 1+2+…+100Ex.: set a 0 set ans 0 while {$a < 101} { set ans [expr $ans+$a] incr a 1 } puts $ans

Result: 5050

Page 39: 8. NS2 - 1

NS2 簡單範例

sample.tcl

# 產生一個模擬的物件set ns [new Simulator]

#針對不同的資料流定義不同的顏色,這是要給 NAM用的$ns color 1 Blue

$ns color 2 Red

#開啟一個 NAM trace fileset nf [open out.nam w]

$ns namtrace-all $nf

Page 40: 8. NS2 - 1

NS2 簡單範例

#開啟一個 trace file,用來記錄封包傳送的過程set nd [open out.tr w]

$ns trace-all $nd

#定義一個結束的程序proc finish {} {

global ns nf nd

$ns flush-trace

close $nf

close $nd

#以背景執行的方式去執行 NAM (The Network Animator)

exec nam out.nam &

exit 0

}

Page 41: 8. NS2 - 1

NS2 簡單範例

#產生四個網路節點set n0 [$ns node]

set n1 [$ns node]

set n2 [$ns node]

set n3 [$ns node]

#把節點連接起來$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

#設定 ns2到 n3之間的 Queue Size為 10個封包大小$ns queue-limit $n2 $n3 10

#設定節點的位置,這是要給 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

Page 42: 8. NS2 - 1

NS2 簡單範例

#觀測 n2到 n3之間 queue的變化,這是要給 NAM用的$ns duplex-link-op $n2 $n3 queuePos 0.5

#建立一條 TCP的連線set tcp [new Agent/TCP]

$tcp set class_ 2

$ns attach-agent $n0 $tcp

set sink [new Agent/TCPSink]

$ns attach-agent $n3 $sink

$ns connect $tcp $sink

#在 NAM中, TCP的連線會以藍色表示$tcp set fid_ 1

#在 TCP連線之上建立 FTP應用程式set ftp [new Application/FTP]

$ftp attach-agent $tcp

$ftp set type_ FTP

Page 43: 8. NS2 - 1

NS2 簡單範例

#建立一條 UDP的連線set udp [new Agent/UDP]

$ns attach-agent $n1 $udp

set null [new Agent/Null]

$ns attach-agent $n3 $null

$ns connect $udp $null

#在 NAM中, UDP的連線會以紅色表示$udp set fid_ 2

#在 UDP連線之上建立 CBR應用程式set 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 44: 8. NS2 - 1

NS2 簡單範例

#設定 FTP和 CBR資料傳送開始和結束時間$ns at 0.1 "$cbr start"

$ns at 1.0 "$ftp start"

$ns at 4.0 "$ftp stop"

$ns at 4.5 "$cbr stop“

#結束 TCP的連線 (不一定需要寫下面的程式碼來實際結束連線 )

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

#在模擬環境中, 5秒後去呼叫 finish來結束模擬 (這樣要注意模擬環境中#的 5秒並不一定等於實際模擬的時間$ns at 5.0 "finish“

#執行模擬$ns run

Page 45: 8. NS2 - 1

NS2 簡單範例

Page 46: 8. NS2 - 1

AWK

使用直譯器不需先行編譯無變數型別之分,也不用事先宣告可使用文字當陣列標籤 (index)

將檔案內一行的資料列,一筆一筆的讀進來每筆資料以空白字元分隔成許多欄位預設變數 $0整個資料列 , $1為第一欄 , $2為 ...

內建 pipe功能,能將資料與 shell間相互傳送用法與 C程式相似

Page 47: 8. NS2 - 1

AWK

Awk運用 (awk主要用來顯示模擬過程記錄檔進而做數值分析

當 awk讀入資料列後,會把每個欄位的值存入欄位變數

Page 48: 8. NS2 - 1

AWK

指令 awk–f out.tr

Page 49: 8. NS2 - 1

AWK

Awk範例– delay.awk

Page 50: 8. NS2 - 1

AWK

Page 51: 8. NS2 - 1

AWK

執行方法 : ($為 shell的提示符號 )$awk-f measure-delay.awk out.tr

若是要把結果存到檔案,可使用導向的方式。 (把結果存到 cbr_delay檔案中 )

$awk-f measure-delay.awk out.tr > cbr_delay

Page 52: 8. NS2 - 1

Gnuplot

圖形模式輸入 gnuplot

Page 53: 8. NS2 - 1

Gnuplot

plot “cdr_delay”

Page 54: 8. NS2 - 1

nsBench

先到 nsBench的官方網頁下載安裝檔 http://

www.mnlab.cs.depaul.edu/projects/nsbench/nsBench.jar

54

Page 55: 8. NS2 - 1

nsBench

新增四個節點,模擬實驗三之環境。

連結各點。

55

每個  link 點選兩下以設定參數。

新增  TCP Agent 及  TCP Sink。

Page 56: 8. NS2 - 1

nsBench

新增  FTP traffic。

連接  ftp1-tcp0 tcp0-n0 tcp0-sink0

56

同理建立  udp0 cbr0 null1 及連線。

點選兩下  cbr0 設定參數。

Page 57: 8. NS2 - 1

nsBench

新增並設定  scenario。

57

設定  Trace 的檔案名稱。

Page 58: 8. NS2 - 1

nsBench

產生程式碼並儲存專案。

58

Page 59: 8. NS2 - 1

nsBench

觀看 TCL程式碼

Page 60: 8. NS2 - 1

nsBench

Page 61: 8. NS2 - 1

nsBench

Page 62: 8. NS2 - 1

nsBench

Page 63: 8. NS2 - 1

nsBench 修改程式碼 

透過  nsBench 可讓使用者利用更友善的  GUI 介面來產生內部程式碼,但使用  nsBench 在  nsBench 平台上卻沒辦法執行,所以我們仍需要在 cygwin的繪圖模式中,輸入指令執行該程式碼。

由  nsBench 所產生出的程式碼需要做一些小小的設定如需將結果繪出,在  proc finish {} block 中加入  exec nam out.nam & 如下圖。

加入顏色區別  flow:在程式碼中加入  $ns color 1 Blue,將  flow 辨識為  1

的資料流以藍色表示  (視資料流數量加入  Red Green … 等 )。

63

Page 64: 8. NS2 - 1

nsBench 修改程式碼繪出時的圖形排列 (沒設定的話會很亂,搞不清楚哪個節點扮演的角色 ), $ns

duplex-link-op $n1 $n2 orient right-down (節點  n2 在節點  n1 的右下方,依此自行設定相對位置 )

顯示  queue 的變化, $ns duplex-link-op $n2 $n3 queuePos 0.5

64

Page 65: 8. NS2 - 1

NS2 Scenarios Generator

Page 66: 8. NS2 - 1

NS2 Scenarios Generator

Page 67: 8. NS2 - 1

NS2 Scenarios Generator

Page 68: 8. NS2 - 1

NS2 Scenarios Generator

Page 69: 8. NS2 - 1

作業

寫一個簡單的 TCL Script來表示  Hidden Terminal 與 Expose Terminal 再有 CSMA/CA與沒有 CSMA/CA的差別四個節點節點依時間點不同而移動移動當中造成上訴兩個問題

利用  awk 來分析並比較上訴兩個問題的效能Packet delivery rate

Fairness

報告TCL, Awk/Gnuplot scripts 都要加上 comment

說明上訴兩者的效能差異

Page 70: 8. NS2 - 1

Hidden Terminal Problem

Page 71: 8. NS2 - 1

Exposed Terminal Problem