顯示具有 ns2 標籤的文章。 顯示所有文章
顯示具有 ns2 標籤的文章。 顯示所有文章

2008年9月1日 星期一

計算機網路實驗 - 以NS2模擬工具實作

這是我今天看了這一本書的大綱:

NS2是一套物件導向的網路模擬器,由UC Berkeley完成,具有模擬真實系統的架構和特性
網路架構:模擬router、link、end-point
網路特性:packet delay、packet drop

第二章:TCL語法
可以參考這裡~作者的網站

第三章:
TCP與UDP模擬實驗
呈現網路效能、評比指標:
1. End-to-End Delay
2. Jitter - 兩個封包傳送的延遲時間的差異
3. Packet Loss
4. Throughput
在NS2中,我們都透過awk去分析出來的報表

第四章:亂數產生器(Random Number Generator)
如何產生合乎現實的例子
NS2提供Pareto、Constant、Uniform、Exponential、HyperExponential

第五章:信賴區間(Confidence Interval)
這裡是說明什麼是信賴區間,這一個在網路上找有一堆資料
1. 母群體(population):為特定研究的所有元素所組成的集合
2. 樣本(sample):母群體的部分集合
3. 母群體平均數(population mean)
4. 樣本平均數(sample mean)
5. 變異數(variance)
6. 樣本變異數(sample variance):由母群體取出一部分樣本的變異數
7. 母群體標準差(standard deviation)
8. 樣本標準差(sample standard deviation)
9. 信賴區間(confidence interval)
求得一個機率範圍(probability bound)使母群體平均有很高的機率能在上限c1與下限c2的範圍內
(1). 信賴區間
(2). 顯著水準

中央極限定理 (Central Limit Theorem)
商用統計學

第六章:佇列管理機制
被動式佇列:DropTail
基於路由器的RED和Droptail算法比較
佇列管理演算法
主動式佇列:RED(Random Early Detection)
主動式會在佇列滿之前就把封包丟棄。

第七章:路由(Dynamic Routing)
靜態路由(static route):由網管人員手動輸入。優點:頻寬都用在傳送資料;缺點:路由器不會主動發現新的router
動能路由(dynamic route):根據協定(routing protocol)或演算法來動態維護路由表中的內容。優點:router彼此交換訊息不用手動維護routing table。缺點:需要消耗網路的頻寬。
預設路由(default router):預設路由是當router無法判斷IP封包轉送的路徑時,就會將封包送往一個預設的閘道器(default gateway)

在決定router table時,會依到達目地的時的成本來考量,這裡的成本可以是hop count、propagation delay、load、bandwidth、reliability

第八章:TCP提供reliable傳輸服務具flow control、congestion control

第十一章:同步化現象(TCP Global Synchronization)
由於網路上的TCP Connection可能會在不同的時間啟動,因此當一個新的TCP Connection進入網路時,在這一個時間點上,它的Congestion window (cwnd)和其它的TCP Connect的cwnd大小可能是不同的,那麼這一些TCP Connection要怎麼公平地競爭頻寬呢?

第十二章:影響TCP效能的幾個因素
1. Round-Trip (RTT):當同時有多個TCP Connection共同相同的Link時,由於RTT較短的Connection等待ACK開啟Congestion window的時間較短,因此和RTT時間較長的TCP Connection比較起來,資料傳輸的效能會比較好。
2. Timer Granulariy:一般而言,通常TCP在實際運作時,並不會真的隨時去注意封包是否過期,而是設定一個時間值,稱為"Coarse-grain timer",每隔一段時間才去檢查是否有封包已經Timeout了。因此,當有封包遺失時,這意謂著一個封包從遺失到直到timeout被重送的時間也會大大的增長。
3. Slow-start threshold:ssthresh的值決定終止slow-start、開始進入Congestion avoidance時window的大小。若設的太小,會使得TCP因為太早進入Congestion avoidance階段,而使得頻寬的使用率變的很差。
4. 封包大小、網路的佇列管理機制、有線/無線的環境。

第十五章:資料流整形器 (Traffic Shaper)
1. 漏桶演算法 (leaky bucket algorithm)
2. 令牌桶演算法 (token bucket algorithm)

2008年8月30日 星期六

NS2 - Wireless

wireless model主要是由MobileNode所組成,MobileNode由Node這個類別繼承而來,因此MobileNode除了有基本的Node能力,還有移動和無線傳送接收的能力。目前ad-hoc routing protocol有支援四種Destination Sequence Distance Vector (DSDV)、 Dynamic Source Routing (DSR) 、 Temporally Ordered Routing Algorithm (TORA)以及 Ad-hoc On-demand Distance Vector (AODV)。

在建立Mobile Node之前,我們必須定義Mobile Node所活動的topology大小。

set topo [ new Topography ]
$topo load_flatgrid $opt(x) $opt(y)

 

接著我們要建立一個God instance,這是用來建立一個矩陣,用以儲存topology的連線資訊。 create-god $opt(nn)

舊的API建立Mobile Node根據使用的routing protocol,而新的 API則是藉由$ns node-config這個instproc來設定,接著就使用產生一般的node的方式產生Mobile Node。



$ns node-config -adhocRouting $opt(adhocRouting) 
                -llType $opt(ll) 
                -macType $opt(mac) 
                -ifqType $opt(ifq) 
                -ifqLen $opt(ifqlen) 
                -antType $opt(ant) 
                -propInstance [new $opt(prop)] 
                -phyType $opt(netif) 
                -channel [new $opt(chan)] 
                -topoInstance $topo 
                -wiredRouting OFF 
                -agentTrace ON 
                -routerTrace OFF 
                -macTrace OFF

for { set i 0 } { $i < $opt(nn) } { incr i } {
  set node($i) [ $ns node ]
  $node($i) random-motion 0 ;# disable random motion
}

雖然Mobile Node被設計成可以在三維空間中移動(X,Y,Z),但是Z尚未被使用,因此我們假設只在平面上移動,也就是Z永遠等於0。我們藉由設定X,Y,Z來初始Mobile Node的初始位置,並且使用setdest來移動Mobile Node,也可以使用$node start來做random的移動,不過不建議使用此方式,如果要做random的scenario可以使用CMU提供的tool -- setdest (放在~ns/indep-utils/cmu-scen-gen/中)。

$node set X_ <x1>
$node set Y_ <x2>
$node set Z_ 0
$ns at <time> $node setdest <x2> <y2> <speed> ;# 在時間time秒, 以速度speed往(x2,y2)移動
example1.tcl # ======================================================================
# Default Script Options
# ======================================================================
set opt(chan) Channel/WirelessChannel                  ;#Channel Type
set opt(prop) Propagation/TwoRayGround                 ;# radio-propagation model
set opt(netif) Phy/WirelessPhy                         ;# network interface type
set opt(mac) Mac/Simple                                ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue                   ;# interface queue type
set opt(ll) LL                                         ;# link layer type
set opt(ant) Antenna/OmniAntenna                       ;# antenna(天線) model
set opt(ifqlen) 50                                     ;# max packet in ifq
set opt(nn) 2                                          ;# number of mobile nodes

set opt(rp) AODV                                       ;# routing protocol. DSDV, DSR, AODV.

#Destination-Sequenced Distance-Vector Routing(AODV)
#Dynamic Source Routing (DSR)
#destination sequenced distance vector (DSDV)
set opt(x) 670                                         ;# Topology大小
set opt(y) 670                                         ;# Topology大小
set opt(trace) wireless_basic.tr                       ;# trace file
set opt(nam.tr) wireless_basic.nam                     ;# the nam trace file

# ======================================================================
# Main Program
# ======================================================================
# Initialize Global Variables
set ns [new Simulator]                                 ;#建立一個新的模擬物件,並指向變數ns

# This command should be called before the universal trace command $ns trace-all
$ns use-newtrace                                       ;#建立一個新的trace資料

set namtrace [open $opt(nam.tr) w]                     ;#開起一個$opt(nam.tr)wireless_basic.nam,並且指向namtrace
#把模擬的過程寫入$namtrace,長為$opt(x),寬為opt(y),因為是wireless,所以,要改用namtrace-all-wireless指令
$ns namtrace-all-wireless $namtrace $opt(x) $opt(y)
;#開起一個$opt(trace)wireless_basic.tr,並且指向tracefd
set tracefd [open $opt(trace) w]
$ns trace-all $tracefd ;#把模擬器的封包過程寫入$tracefd

proc finish { } {                                      ;# new a procedure finish
global ns namtrace tracefd opt                         ;# set global variable
$ns flush-trace                                        ;#把trace清空,都寫入
close $namtrace                                        ;# close file
close $tracefd                                         ;# close file
exec nam $opt(nam.tr) &                                ;#在背景執行nam去打開$opt(nam.tr)
exit 0
}


# set up topography object 定義Mobile Node所活動的topology大小
set topo [new Topography]                              ;#建立一個新的地形
$topo load_flatgrid $opt(x) $opt(y)                    ;#設定長寬

#An omniscient observer
#Stores smallest number of hops from one node to another
#Optimal case to compare routing protocol performance
#Automatically generated by scenario file
#set god [create-god <no of mnodes>]設定node個數
#$god set-dist <from> <to> <#hops>
# Create God (General Operations Director)
# 加入這一行,會出現"num_nodes is set 2"的訊息
create-god $opt(nn)

# Create channel
set chan_ [new $opt(chan)]            ;#set chan_ [new Channel/WirelessChannel] 設定Channel

# Create node(0) and node(1)

# configure node, please note the change below.設定節點的參數
$ns node-config -adhocRouting $opt(rp) \                ;# -adhocRouting AODV 
                -llType $opt(ll) \                      ;# -llType LL
                -macType $opt(mac) \                    ;# -macType Mac/Simple
                -ifqType $opt(ifq) \                    ;# -ifqType Queue/DropTail/PriQueue
                -ifqLen $opt(ifqlen) \                  ;# -ifqLen 50
                -antType $opt(ant) \                    ;# -antType Antenna/OmniAntenna
                -propType $opt(prop) \                  ;# -propType Propagation/TwoRayGround
                -phyType $opt(netif) \                  ;# -phyType Phy/WirelessPhy
                -topoInstance $topo \                   ;# -topoInstance
                -agentTrace ON \                        ;# 在無線網路,要設定是否要打開agent Trace
                -routerTrace ON \                       ;# 在無線網路,要設定是否要打開router Trace
                -macTrace ON \                          ;# 在無線網路,要設定是否要打開mac Trace
                -movementTrace ON \                     ;# 在無線網路,要設定是否要打開movement Trace
                -channel $chan_                         ;# 設定channel 
#以下指令打完,會出現"INITIALIZE THE LIST xListHead"的訊息
for {set i 0} {$i < $opt(nn)} {incr i} {
set node($i) [$ns node]
$node($i) random-motion 0                               ;# disable random motion
$ns initial_node_pos $node($i) 20                       ;# 在nam中定義節點初始所在位置
}

#
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
#
$node(0) set X_ 15.0
$node(0) set Y_ 15.0
$node(0) set Z_ 0.0

$node(1) set X_ 150.0
$node(1) set Y_ 150.0
$node(1) set Z_ 0.0

#
# Now produce some simple node movements
# Node_(1) starts to move towards node_(0)
#
$ns at 0.0 "$node(0) setdest 50.0 50.0 5.0"              ;# 以5.0的速度移動到目標(50.0,50.0)
$ns at 0.0 "$node(1) setdest 60.0 40.0 10.0"             ;# 以10.0的速度移動到目標(60.0,40.0)


# Node(1) then starts to move away from node_(0)
# 若加入這一行指令,在執行這一個tcl時,會出現"SORTING LISTS ...DONE!"的訊息
$ns at 3.0 "$node(1) setdest 240.0 240.0 30.0" ;#以30.0的速度移動到目標(240.0,240.0)

# Setup traffic flow between nodes
# TCP connections between node(0) and node(1)

set tcp [new Agent/TCP]                                  ;#建立TCP物件
$tcp set class_ 2                                        ;#設定顏色
set sink [new Agent/TCPSink]                             ;#建立TCPSink物件
$ns attach-agent $node(0) $tcp                           ;#把node(0)建立在TCP通訊協定上
$ns attach-agent $node(1) $sink                          ;#把node(1)建立在TCPSink通訊協定上
$ns connect $tcp $sink                                   ;#把TCP與TCPSink相連
set ftp [new Application/FTP]                            ;#建立ftp通訊
$ftp attach-agent $tcp                                   ;#把ftp建立在tcp協定上
$ns at 0.5 "$ftp start"                                  ;#在時間0.5ftp開始傳送

#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $opt(nn) } {incr i} {
$ns at 6.0 "$node($i) reset";                            ;#會reset node上所有的agents
}
$ns at 6.0 "finish"
$ns at 6.01 "puts \"NS EXITING...\" ; $ns halt"          ;#ns停止

puts "Starting Simulation..."
$ns run

純程式碼
example.tcl

set ns [new Simulator]

$ns use-newtrace

set namtrace [open wireless_basic.nam w]
$ns namtrace-all-wireless $namtrace 670 670

set tracefd [open wireless_basic.tr w]
$ns trace-all $tracefd

proc finish {} {
  global ns namtrace tracefd
  $ns flush-trace
  close $namtrace
  close $tracefd
  exec nam wireless_basic.nam &
  exit 0
}

set topo [new Topography]
$topo load_flatgrid 670 670

create-god 2

set chan_ [new Channel/WirelessChannel]

$ns node-config -adhocRouting AODV \
                -llType LL \
                -macType Mac/Simple \
                -ifqType Queue/DropTail/PriQueue \
                -ifqLen 50 \
                -antType Antenna/OmniAntenna \
                -propType Propagation/TwoRayGround \
                -phyType Phy/WirelessPhy \
                -topoInstance $topo \
                -agentTrace ON \
                -routerTrace ON \
                -macTrace ON \
                -movementTrace ON \
                -channel $chan_

for {set i 0} {$i<2} {incr i} {
  set node($i) [$ns node]
  $node($i) random-motion 0
  $ns initial_node_pos $node($i) 20
}

$node(0) set X_ 15.0
$node(0) set Y_ 15.0
$node(0) set Z_ 0.0

$node(1) set X_ 150.0
$node(1) set Y_ 150.0
$node(1) set Z_ 0.0

$ns at 0.0 "$node(0) setdest 50.0 50.0 5.0"
$ns at 0.0 "$node(1) setdest 60.0 40.0 10.0"

$ns at 3.0 "$node(1) setdest 240.0 240.0 30.0"

set tcp [new Agent/TCP]
$ns color 2 Red
$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 0.5 "$ftp start"

for {set i 0} {$i<2} {incr i} {
  $ns at 6.0 "$node($i) reset"
}


$ns at 6.0 "finish"
$ns at 6.01 "puts \"NS EXITING...\";$ns half"

puts "Starting Simulator..."
$ns run

NS-2-TCP Agent



############################################################
# set option value
############################################################
set opt(trace) out.nam                  ;# 設定nam檔為out.nam
set opt(nodes) 10                       ;# 節點個數
set opt(rtproto) DV                     ;# 路由協定
set opt(src.packet.size) 1000           ;# 來源端封包大小
set opt(src.packet.int) 0.005           ;# 來源端封包間隔

############################################################
# main program
############################################################
set ns [new Simulator]                  ;# 在檔案一開始會先建立一個的模擬
$ns rtproto $opt(rtproto)               ;# 設定路由協定

set nf [open $opt(trace) w]             ;# 開啟一個可寫入的檔案out.nam
$ns namtrace-all $nf                    ;# 將所有模擬的資料都寫入$nf這個檔案中
$ns use-newtrace

proc finish {} {                        ;# 新增一個procedure "finish" 
  global ns nf opt                      ;# 讓這個proc能讀取這個scrpit的其他變數
  $ns flush-trace                       ;# Use new trace format 使用新的trace format

  close $nf
  exec nam $opt(trace) &
  exit 0
}

for {set i 0} {$i < $opt(nodes)} {incr i} { ;# 建立節點
  set n($i) [$ns node]
}

for {set i 0} {$i < $opt(nodes)} {incr i} { ;# 建立一個環狀的topology
$ns duplex-link $n($i) $n([expr ($i+1)%$opt(nodes)]) 1Mb 10ms DropTail
}

set tcpS0 [new Agent/TCP]                   ;# TCP Sender
$ns attach-agent $n(0) $tcpS0               ;# 將TCP Sender裝在$n(0)
set tcpR0 [new Agent/TCPSink]               ;# TCP Sink with one ACK per packet
$ns attach-agent $n(2) $tcpR0               ;# 將TCP Sink裝在$n(2)

$ns connect $tcpS0 $tcpR0                   ;# 將TCP Sender 和TCP Sink做連結

set ftp [$tcpS0 attach-app FTP]             ;# TCP上層的流量是FTP

$ns at 0.5 "$ftp start"
$ns rtmodel-at 1.5 down $n(1)               ;# 在第1.5秒$n(1)停掉所有的interface
$ns rtmodel-at 3.5 up $n(0) $n(1)           ;# 在第2.5秒,將$n(0)和$n(1)的介面up
$ns rtmodel-at 5.5 up $n(1) $n(2)           ;# 在第2.5秒,將$n(1)和$n(2)的介面up
$ns at 7.5 "$ftp stop"
$ns at 9.5 "finish"                         ;# 在第9.5秒,結束模擬

$ns run                                     ;# 開始執行模擬

2008年8月29日 星期五

NS-2 - UDP Agent

############################################################
# set option value

# 設定參數
############################################################

#利用陣利設定參數
set opt(trace) out.nam                                        ;# 設定檔案參數
set opt(nodes) 10                                             ;# 節點個數
set opt(rtproto) DV                                           ;# 路由協定
set opt(src.packet.size) 800                                  ;# 來源端封包大小
set opt(src.packet.int) 0.5                                   ;# 來源端封包間隔
set opt(udp.MMS) 100                                          ;# Max Sefment Size for UDP afent
set opt(udp.dest.addr) 1                                      ;# 還不知道這個怎麼用?
set opt(udp.dest.port) 80
set opt(udp.ttl) 6                                            ;# Time-To-Live

############################################################
# main program
############################################################
set ns [new Simulator]                                        ;# 在檔案一開始會先建立一個的模擬環境
$ns rtproto $opt(rtproto)                                     ;# 設定路由協定

set nf [open $opt(trace) w]                                   ;# 開啟一個可寫入的檔案out.nam
$ns namtrace-all $nf                                          ;# 將所有模擬的資料都寫入$nf這個檔案中
$ns use-newtrace                                              ;# Use new trace format 使用新的trace format

proc finish {} {                                              ;# 新增一個procedure "finish"
  global ns nf opt                                            ;# 讓這個proc能讀取這個scrpit的其他變數,其中opt是陣列,只要global這一個opt,陣列其它元素就可以使用
  $ns flush-trace                                             ;# 將所有的trace object 寫入 buffer
  close $nf                                                   ;# 關掉out.nam檔
  exec nam $opt(trace) &                                      ;# 透過背景執行nam來開起out.nam檔
  exit 0                                                      ;# 離開
}

for {set i 0} {$i < $opt(nodes)} {incr i} {
  set n($i) [$ns node]                                        ;# 建立節點
}

for {set i 0} {$i < $opt(nodes)} {incr i} {                   ;# 建立一個環狀的topology
$ns duplex-link $n($i) $n([expr ($i+1)%$opt(nodes)]) 1Mb 10ms DropTail
}

set udp0 [new Agent/UDP]                                      ;# 新增一個udp,第一個node裝UDP Agent
$udp0 set packetSize_ $opt(udp.MMS)                           ;# Max Segment Size for UDP agent
$udp0 set dst_addr_ $opt(udp.dest.addr)                       ;# 目的地IP
$udp0 set dst_port_ $opt(udp.dest.port)                       ;# 目的地Port number
$udp0 set ttl_ $opt(udp.ttl)                                  ;# Time-To-Live

$ns attach-agent $n(0) $udp0                                  ;# 在$n(0)上裝上$udp0

set cbr0 [new Application/Traffic/CBR]                        ;# 先建立一個cbr,然後在udp上裝CBR
$cbr0 set packetSize_ $opt(src.packet.size)                   ;# 設定CBR封包大小
$cbr0 set interval_ $opt(src.packet.int)                      ;# 設定CBR發射封包的間隔時間
$cbr0 attach-agent $udp0                                      ;# 把cbr0架在udp0上,CBR應用程式是在UDP協定上運作的

set null0 [new Agent/Null]                                    ;# 先建立一個NULL特性,在第3個node上裝Null Agent,NULL Agent是用來drop packet的
$ns attach-agent $n(2) $null0                                 ;# 把null0建立在n(2)上

$ns connect $udp0 $null0                                      ;# 將來源端和目的端連結起來

$ns at 0.5 "$cbr0 start"                                      ;# 在第0.5秒開始送封包
$ns rtmodel-at 1.5 down $n(1)                                 ;# 在第1.5秒$n(1)停掉所有的interface
$ns rtmodel-at 3.5 up $n(0) $n(1)                             ;# 在第2.5秒,將$n(0)和$n(1)的介面up
$ns rtmodel-at 5.5 up $n(1) $n(2)                             ;# 在第2.5秒,將$n(1)和$n(2)的介面up
$ns at 9.5 "finish"                                           ;# 在第9.5秒,結束模擬

$ns run                                                       ;# 開始執行模擬

ns2 - Link的基本指令



透過ns-2來模擬上面的網路情況
simple.tcl

set ns [new Simulator]
;# 在檔案一開始會先建立一個的模擬
;# step1:先建立一個Simulator物件
;# step2:把ns變數設定為物件:set ns 物件

$ns color 1 Blue
;# 定義color index 1=> Blue
$ns color 2 Red
;# 定義color index 2=> Red

set nf [open out.nam w]
;# 開啟一個可寫入的檔案out.nam
;# step1:先執行open out.nam w,並傳回這一個物件
;# step2:設定nf為記錄這一個物件

$ns namtrace-all $nf
;# 將所有模擬的資料都寫入$nf這個檔案中

;# 新增一個procedure "finish"
proc finish {} {
  global ns nf
  ;# 宣告ns與nf為在外面定義過的變數
  $ns flush-trace
  ;# 把trace的結果寫入檔案中
  close $nf
  ;# 關掉檔案
  exec nam out.nam &
  ;# 在背景中,透過nam去執行out.nam
  exit 0
  ;# 離開
}

#建立4個Node
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
;# 設定n0為一個node
;# step1:$ns node傳回一個node物件
;# step2:設定n0為這一個傳回的物件

#建立三個link
$ns duplex-link $n0 $n2 1Mb 10ms DropTail
#; node 0連到node 2 頻寬為1Mb,delay time為10ms 為DropTail
$ns duplex-link $n1 $n2 1Mb 10ms DropTail
#; node 1連到node 2 頻寬為1Mb,delay time為10ms 為DropTail
$ns duplex-link $n3 $n2 1Mb 10ms SFQ
#; node 2連到node 3 頻寬為1Mb,delay time為10ms 為SFQ

#設定link的屬性
$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
#; 把它想成一點,往右延伸

#Monitor the queue for the link between node 2 and node 3
$ns duplex-link-op $n2 $n3 queuePos 0.5
;# 觀測n2到n3之間queue的變化,這是要給NAM用的
;# 也就是說只查看安置由n2到n3的封包Queue的情況

set udp0 [new Agent/UDP]
;# 新增一個UDP agent
$udp0 set class_ 1
;# 將$upd1顏色設成1 (Blue)
$ns attach-agent $n0 $udp0
;# $n0架構在$udp0上

set cbr0 [new Application/Traffic/CBR]
;# 建立應用層的流量描述
$cbr0 set packetSize_ 500
;# 設定封包大小
$cbr0 set interval_ 0.005
;# 設定封包間隔時間
$cbr0 attach-agent $udp0
;# 依附在UDP agent上

set udp1 [new Agent/UDP]
$udp1 set class_ 2
;# 將$upd1顏色設成2 (Red)
$ns attach-agent $n1 $udp1

set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

set null0 [new Agent/Null]
;# 建立一個Null Agent
$ns attach-agent $n3 $null0
;# 將$n3架構在$null0

$ns connect $udp0 $null0
;# 把agent $udp0和agent $null0連在一起
$ns connect $udp1 $null0
;# 把agent $udp1和agent $null0連在一起

$ns at 0.5 "$cbr0 start"
$ns at 1.0 "$cbr1 start"
$ns at 4.0 "$cbr1 stop"
$ns at 4.5 "$cbr0 stop"
$ns at 5.0 "finish"

$ns run


注意在attach-agent的時候,有時候會不確定哪一個在前面,哪一個在後面~
反正,目前就這樣記,udp、tcp的份量比較大,放在後面

2008年8月27日 星期三

ns2 - 指令格式

建立一個simple-link

$ns simplex-link <node1> <node2> <bandwidth> <delay> <queue type> <args>


建立一個duplex-link

$ns duplex-link <node1> <node2> <bandwidth> <delay> <queue type> <args>

<queue type> := DropTail | RED | CBQ | FQ | SFQ | DRR

設定simple-link的屬性

$ns simplex-link-op <n1> <n2> <op> <args>


設定duplex-link的屬性

$ns duplex-link-op <n1> <n2> <op> <args>

<op> := orient | color | queuePos | label

為nam定義color index, 後面可以這個index來指名顏色。

$ns color <color-id> <color>


[UDP Agent]

Maximum Segment Size (MSS)

$udp set packetSize_ <pksize>


目的地IP

$udp set dst_addr_ <address>


目的地Port number

$udp set dst_port_ <portnum>


Time-To-Live

$udp set ttl_ <time-to-live>


[TCP Agent]

TCP Agent被實作在tcp.{cc, h}中,主要有兩種TCP agent:one-way agent和 two-way agent。One-way就是單純只扮演Sender或是 Receiver,Two-way同時是Sender也是Receiver。

[One-Way Sender]

Agent/TCP:a "tahoe" TCP sender

Agent/TCP/Reno: a "Reno" TCP sender

Agent/TCP/Newreno:a "Reno" TCP sender with a modification

Agent/TCP/Sack1:TCP with selective repeat (follows RFC2018)

Agent/TCP/Vegas:TCP Vegas

Agent/TCP/Fack:Reno TCP with "forward acknowledgement"

One-Way Receiver

Agent/TCPSink:TCP sink with one ACK per Packet

Agent/TCPSink/DelAck:TCP sink with configurable delay per ACK

Agent/TCPSink/Sack1:selective ACK sink (follows RFC2018)

Agent/TCPSink/Sack1/DelAck:Sack1 with DelAck

Two-way只有支援Reno:Agent/TCP/FullTcp

[Wireless]

wireless model主要是由MobileNode所組成,MobileNode由Node這個類別繼承而來,因此MobileNode除了有基本的Node能力,還有移動和無線傳送接收的能力。目前ad-hoc routing protocol有支援四種Destination Sequence Distance Vector (DSDV)、 Dynamic Source Routing (DSR) 、 Temporally Ordered Routing Algorithm (TORA)以及 Ad-hoc On-demand Distance Vector (AODV)。

在建立Mobile Node之前,我們必須定義Mobile Node所活動的topology大小。

set topo [ new Topography ]
$topo load_flatgrid $opt(x) $opt(y)


接著我們要建立一個God instance,這是用來建立一個矩陣,用以儲存topology的連線資訊。

create-god $opt(nn)


舊的API建立Mobile Node根據使用的routing protocol,而新的 API則是藉由$ns node-config這個instproc來設定,接著就使用產生一般的node的方式產生Mobile Node。

$ns_ node-config -adhocRouting $opt(adhocRouting)
                 -llType $opt(ll)
                 -macType $opt(mac)
                 -ifqType $opt(ifq)
                 -ifqLen $opt(ifqlen)
                 -antType $opt(ant)
                 -propInstance [new $opt(prop)]
                 -phyType $opt(netif)
                 -channel [new $opt(chan)]
                 -topoInstance $topo
                 -wiredRouting OFF
                 -agentTrace ON
                 -routerTrace OFF
                 -macTrace OFF

for { set i 0 } { $i < $opt(nn) } { incr i } {
set node($i) [ $ns node ]
$node($i) random-motion 0 ;# disable random motion
}


雖然Mobile Node被設計成可以在三維空間中移動(X,Y,Z),但是Z尚未被使用,因此我們假設只在平面上移動,也就是Z永遠等於0。我們藉由設定X,Y,Z來初始Mobile Node的初始位置,並且使用setdest來移動Mobile Node,也可以使用$node start來做random的移動,不過不建議使用此方式,如果要做random的scenario可以使用CMU提供的tool -- setdest (放在~ns/indep-utils/cmu-scen-gen/中)。

$node set X_ <x1>
$node set Y_ <x2>
$node set Z_ 0
$ns at <time> $node setdest <x2> <y2> <speed> ;# 在時間time秒, 以速度speed往(x2,y2)移動

Tcl 語法

一開始可以先參考這裡:TCL 簡介
書簽:
[變數(variable)和變數替換(variable substituion)]、[表示式(expressions)]、[指令替換(command substitution)]、[流程控制(control flow)]、[程序(procedures)]、[陣列(arrays)]、[字串(strings)]、[輸出(output)]

key words:$、set、puts、expr、ns、if、elseif、else、incr、foreach、while、switch、proc、global

因為ns2的劇本是由tcl語法寫成的~所以,我們要先了解tcl語法如何使用~

因為沒有時間,
下面的網頁就寫的很清楚了,所以,大部分還是去參考下面的網頁吧~若以後,我有空再補吧~
我只寫出我遇到的問題~

;#
上面兩個聯合符號是表示後面的東西就是註解

[變數(variable)和變數替換(variable substituion)]

[表示式(expressions)]

[指令替換(command substitution)]

[流程控制(control flow)]
寫完範例後,竟然出現:「extra characters after close-brace while executing...」
因為在tcl語法中:
if {$value > 10}{
要改成: if {$value > 10} {
也就是if {}空一格{}
也就是說:
{左邊一定要存在一個空格
}右邊一定要存在一個空格

而且不能用

if {...}
{
  // do something
}

必需要用

if {...} {
  // do something
}


[程序(procedures)]:應該就是副程式的意思啦~
宣告:
proc name {params1 params2 } {
  body
}
其中name為程序的名稱,params是參數列表,body則是程序的主體
例:

proc sum_proc {a b} {
  global myglobalvar
  puts "The value of the global variable is $myglobalvar"
  return [expr $a + $b]
}


呼叫:
name param1 param2
例:

set myglobalvar 79
set sum [sum_proc $num1 $num2]

說明:
這裡的中括號表示是先運算中括號裡面的值,中括號裡面運算出來是2,則再執行set sum 2這一行指令

這個範例主要在說明區域變數(local variable)和全域變數(global variable)的觀念。這樣要特別說明的是當程序中有需要用到程序外已經定義好的變數前,必需先使用global這個保留字做宣告,才能存取程序外已經定義好的變數。

[陣列(arrays)]


set myarray(0) "Zero"
set myarray(1) "One"
set myarray(2) "Two"
for {set i 0} {$i < 3} {incr i 1} {
  puts $myarray($i)
}

這裡的陣列是用小括號來包住引數

ex6_2.tcl

set person_info(name) "Fred Smith"
set person_info(age) "25"
set person_info(occupation) "Plumber"
foreach thing {name age occupation} {
  puts "$thing == $person_info($thing)"
}

foreach thing{name age occupation}就是把它想成
set thing name;
set thing age;
set thing occupation;
分三個階段設定

ex6_3.tcl

set person_info(name) "Fred Smith"
set person_info(age) "25"
set person_info(occupation) "Plumber"
foreach thing [array names person_info] {
  puts "$thing == $person_info($thing)"
}

第一個loop時,$thing為name;而後面的就變成$person_info(name)也就是"Fred Smith"
第二個loop時,$thing為age;而後面的就變成$person_info(age)也就是"25"
第三個loop時,$thing為occupation;而後面的就變成$person_info(occupation)也就是"Plumber"

這個範例跟6.2最大的不同是在foreach這個迴圈,在6.2中,陣列中每個元素存放的位置必須一一列出,才能秀出陣列中每個元素的值,但是在這個範例,使用了[array names 矩陣名稱](注意是中括號喔~),使用的這樣的方法就可以列出person_info中的name、age、occupation,這個在元素很多的時候特別好用。而這裡的thing代表陣列的引數。

[字串特殊函數(strings function)]
ex7_1.tcl

set str "This is a string"
puts "The string is: $str"
puts "The length of the string is: [string length $str]"
puts "The character at index 3 is: [string index $str 3]"
puts "The characters from index 4 through 8 are: [string range $str 4 8]"
puts "The index of the first occurrence of letter \"i\" is: [string first i $str]"

說明:
[string length $str]可以用來顯示字串長度
[string index $str 3]可以用來顯示字串中的第四個字母為何(第一個字母的index為0)
[string range $str 4 8]可以用顯示從字串中第五個到第九個的字母
[string first i $str]可以用來顯示字母i在字串中第一次出現時的index值,也就是第一次出現時在第(index+1)個位置。

[輸出(output)]
ex8_1.tcl

set f [open "/tmp/myfile" "w"]
puts $f "We live in Texas. It's already 110 degrees out here."
puts $f "456"
close $f


執行的結果:
請使用文字編輯器打開/tmp/myfile,其內容為:

We live in Texas. It's already 110 degrees out here.
456

說明:
一般來說,puts若是沒有指定輸出裝置的話,內定的輸出裝置是螢幕,但此範例中有指定輸出的裝置為檔案,所以會把要秀出的字串寫入檔案内。

參考網頁:
TCL 簡介

2008年8月26日 星期二

ns2 on Ubuntu - install

因為,之後要改學長姐的行車網路模擬器,
所以,會參考行之有年的ns-2摸擬器。
要學習就要有環境,一開始就先來個安裝吧~

先安裝一下相關套件:

[someone@host ~ ]# sudo apt-get install build-essential
[someone@host ~ ]# sudo apt-get install tcl8.4 tcl8.4-dev tk8.4 tk8.4-dev
[someone@host ~ ]# sduo apt-get install libxmu-dev libxmu-headers


接著到 ns2 的 SourceForge 去下載檔案,這邊我下載到的最新版是 2.33(要用就要用最近滴~)

把ns-2解壓縮到/usr/local資料夾下,因為權限的關係,最好在解壓縮之前,在/usr/local下建立一個資料夾ns-allinone-2.33,並且把它的權限改成你常用的一個普通使用者,避免一直需要root的權限

[someone@host ~ ]# mkdir /usr/local/ns-allinone-2.33
[someone@host ~ ]# tar -zxvf ns-allinone-2.32.tar.gz -C /usr/local
[someone@host ~ ]# cd /usr/local/ns-allinone-2.33
[someone@host ~ ]# ./install


然後,安裝最後會要你在.csbrc或.bashrc中加入一些指令
我是加入在~/.bashrc中
.bashrc

export PATH=$PATH:/usr/local/ns-allinone-2.33/bin:/usr/local/ns-allinone-2.33/tcl8.4.18/unix:/usr/local/ns-allinone-2.33/tk8.4.18/unix
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ns-allinone-2.33/otcl-1.13:/usr/local/ns-allinone-2.33/lib
export TCL_LIBRARY=$TCL_LIBRARY:/usr/local/ns-allinone-2.33/tcl8.4.18/library


安裝完後,就去驗証啦,應該會跑個半個小時吧~

[someone@host ~]# cd /usr/local/ns-allinone-2.33/ns-2.33/
[someone@host ~]# ./validate


若驗証完後,若沒有問題的話,它會說all pass;但是,沒有看到一個介面出來,可能會覺得魂身不對勁,好~
example2.tcl

#Create a simulator object
set ns [new Simulator]

#Define different colors for data flows
$ns color 1 Blue
$ns color 2 Red

#Open the nam trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute nam on the trace file
exec nam out.nam &
exit 0
}

#Create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

#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

$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

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

#Create a UDP agent and attach it to node n0
set udp0 [new Agent/UDP]
$udp0 set class_ 1
$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

#Create a UDP agent and attach it to node n1
set udp1 [new Agent/UDP]
$udp1 set class_ 2
$ns attach-agent $n1 $udp1

# Create a CBR traffic source and attach it to udp1
set cbr1 [new Application/Traffic/CBR]
$cbr1 set packetSize_ 500
$cbr1 set interval_ 0.005
$cbr1 attach-agent $udp1

#Create a Null agent (a traffic sink) and attach it to node n3
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0

#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"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"

#Run the simulation
$ns run


就執行它吧~

[someone@host ~]# ns example2.tcl

就會出現以下視窗啦~



參考資料:
阿駕零零壹 © 學習筆記
cygwin + NS2 2.28 安裝成功