26
NS2 教教

NS2 教學

  • Upload
    maxine

  • View
    238

  • Download
    2

Embed Size (px)

DESCRIPTION

NS2 教學. Introduction. 為什麼要用 Simulator 作模擬? 為什麼要用 NS2 ? Open source Object-Oriented OTcl. Outline . 什麼是 NS2 Tcl 簡介 開始模擬 模擬結果分析 分析結果圖形化. 什麼是 NS2?. N etwork S imulator – Version 2 1989 年由 Real Network Simulator 改版,目前由 SAMAN 和 CONSER 維護 - PowerPoint PPT Presentation

Citation preview

Page 1: NS2  教學

NS2 教學

Page 2: NS2  教學

Introduction

為什麼要用 Simulator 作模擬?

為什麼要用 NS2 ? Open source Object-Oriented OTcl

Page 3: NS2  教學

Outline

什麼是 NS2 Tcl 簡介 開始模擬 模擬結果分析 分析結果圖形化

Page 4: NS2  教學

什麼是 NS2?

Network Simulator – Version 2

1989 年由 Real Network Simulator 改版,目前由 SAMAN 和 CONSER 維護

NS is a discrete event simulator targeted at networking research.

Page 5: NS2  教學

NS2 架構

UIEvent Scheduler

NetworkComponent

TCL

C++

Tclcl

OTcl

Page 6: NS2  教學

為什麼要用二種語言 ?

用 C++ : 處理封包傳送 更改一些底層或新增 protocols 之類的 C++

Class 不常更動,執行速度快

用 OTcl : 負責設定檔部分 運作已編譯過的 C++ Objects 常會更動,執行時需花一點直譯的時間

Page 7: NS2  教學

Tcl 簡介 (Tool Command Language)

ns 使用 MIT 發展的 OTcl (Object Tcl) 做為描述、配置、執行模擬的語言, OTcl 是 Tcl的物件導向延伸版本。在使用 ns 進行網路模擬之前,必須先學會這個語言,可以參考 ns/otcl/doc 目錄下有一份 OTcl Tutorial,是一份不錯的入門文件。

Page 8: NS2  教學

Tcl 簡介 (Tool Command Language) cont.

1: # 範例:遞迴解費氏數

2: set count 20 ; # The number of Fibonacci numbers to print

3: set num(0) 0; set num(1) 1 ; # First and second seed values

4: puts "The first $count Fibonacci numbers:"

5: for { set i 1 } { $i < $count } { incr i 1 } {

6: puts "$i:\t$num(0)"

7: set num(1) [ expr $num(0) + $num(1) ]

8: incr i 1

9: puts "$i:\t$num(1)"

10: set num(0) [ expr $num(0) + $num(1) ]

11: }

Page 9: NS2  教學

Tcl 簡介 (Tool Command Language) cont.

2: set count 20 ; # The number of Fibonacci numbers to print

3: set num(0) 0; set num(1) 1 ; # First and second seed values

set count 20

int count = 20;

set num(0) 0; set num(1) 1 ;

int num[2];

num[0] = 0; num[1] = 1;

Page 10: NS2  教學

Tcl 簡介 (Tool Command Language) cont.

puts "The first $count Fibonacci numbers:"

" …" do variable substitution

{…} variable substitution does not take place

Puts “the first $count Fibonacci numbers”

the first 20 Fibonacci numbers

Puts {the first $count Fibonacci numbers}

the first $count Fibonacci numbers

Page 11: NS2  教學

Tcl 簡介 (Tool Command Language) cont.

for { set i 1 } { $i < $count } { incr i 1 } {

for ( i = 1 ; i < count ; i++ ) {

流程控制

(control flow)

if-else 、 switch 、 while 、 for 、 foreach

set my_planet "earth"if {$my_planet == "earth"} { puts "I feel right at home."} elseif {$my_planet == "venus"} { puts "This is not my home."} else { puts "I am neither from Earth, nor from Venus."}

set num_legs 4switch $num_legs { 2 {puts "It could be a human."} 4 {puts "It could be a cow."} 6 {puts "It could be an ant."} 8 {puts "It could be a spider."} default {puts "It could be anything."}}

set i 0while {$i < 5} { puts "In the while loop, and i == $i" incr i 1}

Page 12: NS2  教學

Tcl 簡介 (Tool Command Language) cont.

set num(1) [ expr $num(0) + $num(1) ]

num[1] = num[0] + num[1];

[] indicate command substitution

puts "I am [expr 10 * 3] years old, and my I.Q. is [expr 100 + 75]"

執行的結果 :I am 30 years old, and my I.Q. is 175

Page 13: NS2  教學

開始 NS2 模擬:模擬環境介紹

Page 14: NS2  教學

NS2 模擬: Tcl Script

# 產生一個模擬的物件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

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

Page 15: NS2  教學

NS2 模擬: Tcl Script (cont.)

# 定義一個結束的程序proc finish {} { global ns nf nd $ns flush-trace close $nf close $nd # 以背景執行的方式去執行 NAM exec nam out.nam & exit 0}

Page 16: NS2  教學

NS2 模擬: Tcl Script (cont.)

# 產生四個網路節點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

Page 17: NS2  教學

NS2 模擬: Tcl Script (cont.)

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

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

# 建立一條 TCP 的連線set tcp [new Agent/TCP] # 創造一個 TCP 的 Agent $ns attach-agent $n0 $tcp # TCP agent 結合到 node(n0) # 但就此範例光是 TCP 無法產生任何 Traffic, 所以通常我們都會再建立一些# Application 的 Protocol 於 TCP 上 ( 如 FTP 、 Telnet) set ftp [new Agent/TCP] $ns attach-agent $n3 $sink

Page 18: NS2  教學

NS2 模擬: Tcl Script (cont.)

# 設定 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 at < time > < event ># 在模擬環境中, 5 秒後去呼叫 finish 來結束模擬$ns at 5.0 "finish“

# 開始執行 scheduler.$ns run

Page 19: NS2  教學

NS2 模擬:模擬結果+ 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 1r 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 2r 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.................................................................

event time fromto

nodepkt

nodepkttype

flagsize

fidsrc

addrdst

addrseqnum

pktid

一 二 三 四 五 六 七 八 九 十 十一 十二

Page 20: NS2  教學

awk 語言 – 數值分析

資料列: awk 從資料檔上讀取的基本單位,以 trace file 為例, awk 讀入的第一筆資料列為 ”+ 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”一般而言,一筆資料列相當於資料檔上的一行資料。

Page 21: NS2  教學

awk 語言 – 簡介

程式主要節構 :Pattern1 { Actions1 }Pattern2 { Actions2 }……………………………Patternx { Actionsx }

一般常用關係判斷式來當成Pattern 。例如:x > 3 用來判斷變數 x 是否大於3x == 5 用來判斷變數 x 是否等於 5awk 提供 c 語言常見的關係運算元,如:>、<、>=、<=、==、!=等等

Actions 是由許多 awk 指令所構成,而 awk 的指令與 c 語言中的指令非常類似 。

IO 指令: print 、 printf( ) 、 getline ......流程控制指令 : if ( ...) {...} else {…} 、 while(…){…} ……

Page 22: NS2  教學

awk 語言 – 執行流程 執行 awk 時 , 它會反複進行下列四步驟

自動從指定的資料檔中讀取一筆資料列。 自動更新 (Update) 相關的內建變數之值。 逐次執行程式中 所有 的 Pattern { Actions } 指

令。 當執行完程式中所有 Pattern { Actions } 時,若

資料檔中還有未讀取的料,則反覆執行步驟 1到步驟 4 。

Page 23: NS2  教學

awk 語言 – 例子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;

Page 24: NS2  教學

awk 語言 – 例子 (cont.)

# 記錄封包的傳送時間 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; }}

Page 25: NS2  教學

分析結果圖形化 [xgraph]

xgraph 的運作是把第一排當作 x軸的資料,第二排當作是 y軸的資料

[gnuplot] 命令導向的交談式繪圖程式 (command-driven

interactive function plotting program) 。

Page 26: NS2  教學

一些不錯介紹 NS2 的網站 NS2 使用說明

http://140.116.72.80/~smallko/ns2/ns2.htm http://netlab.cse.yzu.edu.tw/ns2/ns2_website/

一些已開發的模組 http://www.isi.edu/nsnam/ns/ns-contributed.html

Tcl 簡單介紹http://k-lug.org/~griswold/NS2/fib.html

AWK Tutorial Guide http://phi.sinica.edu.tw/aspac/reports/94/94011/

GNUPLOT 使用手冊 http://phi.sinica.edu.tw/aspac/reports/94/94002/