使ってみよう NS2 - 慶應義塾大学 山中研究室

情報工学講義第3講義資料(第3回、4回、5回)
使ってみよう NS2
2007 年 5 月 30 日
改訂 2008 年 5 月 29 日
改訂 2009 年 4 月 15 日
慶應義塾大学理工学部情報工学科
岡本 聡
1. NS2 とは
NS (Network Simulator)は、米国 DARPA の研究プロジェクトの研究成果であり、カリフォル
ニア大学バークレイ校で開発された。現在バージョン 2 の開発が進められており、NS2 と呼ばれ
ている。
NS2 では、シナリオファイルとしてネットワーク構成を記述(TCL スクリプト)し、シナリオフ
ァイルを NS 本体に読み込ませると、シミュレーション結果が得られる。
シミュレーション結果は、ネットワークアニメータ(nam)でのアニメーションや、プロット形
式で出力されたデータから、gnuplot や xgraph を利用して可視化することができる。
2. NS2 のインストール
2.1. NS2 ソースパッケージのダウンロード
http://www.isi.edu/nsnam/ns/ にソースとドキュメントが存在している。
http://www.isi.edu/nsnam/ns/ns-build.html から ns-2 をダウンロードする。最新版は、2.33
(April 1, 2008) である。allinone を使用することが推奨されている。最終的に、SourceForge か
ら、ns-allinone-2.33.tar.gz をダウンロードすることになる。
2.2. NS2 のインストール
各自のホームディレクトリに、ダウンロードしてきたら、以下の命令でパッケージを展開する。
% tar zxvf ns-allinone-2.33.tar.gz
以下の命令を実行して NS2 をコンパイルして実装する
% cd ns-allinone-2.33
%./install (又は sh install)
2.3. PATH の設定
※細かなバージョンの数字は変わっているかもしれないので、それぞれの環境に合わせること。
tcsh の場合は、~/.tcshrc (又は~/.cshrc) に以下を追加する。
setenv NS_HOME ~/ns-allinone-2.33
set path=($path $NS_HOME/bin $NS_HOME/tcl8.4.18/unix\
$NS_HOME/tk8.4.18/unix)
以上を path のところに追加記述する。
setenv LD_LIBRARY_PATH /usr/lib:/usr/X11R6/lib:\
/usr/local/lib:$NS_HOME/otcl-1.13:$NS_HOME/lib
setenv TCL_LIBRARY $NS_HOME/tcl8.4.18/library
追加した後で、以下の命令を実行して設定変更を有効にする。
% source ~/.tcshrc
1/22
情報工学講義第3講義資料(第3回、4回、5回)
bash の場合は、~/.bashrc に以下を追加する。
export NS_HOME=$HOME/ns-allinone-2.33
PATH=$PATH:$NS_HOME/bin:$NS_HOME/tcl8.4.18/unix
export PATH=$PATH:$NS_HOME/tk8.4.18/unix
export LD_LIBRARY_PATH=\
$LD_LIBRARY_PATH:$NS_HOME/otcl-1.13:$NS_HOME/lib
export TCL_LIBRARY=$NS_HOME/tcl8.4.18/library
追加した後で、以下の命令を実行して設定変更を有効にする。
% source ~/.bashrc
※ \で次の行への継続を表している。無視して一行で入力すると確実。
2.4. NS2 のテスト
以下のコマンドを実行して、NAM 画面が立ち上がれば OK
% cd ~/ns-allinone-2.33/ns-2.33/tcl/ex
% ns simple.tcl
Play Forward ボタンを押して、シミュレーションアニメを楽しみましょう。
3. 参考書及びインターネット上の情報
参考書:森北出版 NS2 によるネットワークシミュレーション (定価 4,200 円+税)
ISBN4-627-84621-5
本文章は、上記参考書に基づいて記述されている。
http://www.cool.giti.waseda.ac.jp/~onoder/onoder/ns-tutorial.htm
http://netlab.ce.nihon-u.ac.jp/~sue/
http://www.lares.dti.ne.jp/~wyama/craft/learning-ns2.html
http://www.kensuke.org/ns/index.html
4. NS2 で Hello World
4.1. インタラクティブモード
Host% ns
% set ns [ new Simulator ]
_o4
% $ns at 1 “puts \”Hello NS2’s World!\””
1
% $ns at 1.5 “exit”
2
% $ns run
Hello NS2’s World!
Host%
2/22
情報工学講義第3講義資料(第3回、4回、5回)
4.2. バッチモード (こちらが一般的な使い方)
以下の内容のファイル Hello.tcl を作成する。
set ns [ new Simulator ]
$ns at 1 “puts \”Hello NS2’s World!\””
$ns at 1.5 “exit”
$ns run
このプログラムを以下のように実行する。
Host% ns Hello.tcl
5. シナリオスクリプトの構成
以下のような基本スタイルが使用される。
„ Simulator クラスオブジェクトの生成
„ トレース内容の定義
„ ネットワークトポロジーの定義
„ トラヒックエージェントとアプリケーションの定義
„ 後処理部の定義
„ シミュレーションの開始
# Simulator クラスオブジェクトの生成
set ns [ new Simulator ]
# トレース内容の定義 (以下は例)
set f [ open out.tr w ]
set nf [ open out.nam w ]
#
<ネットワークトポロジーの定義部>
#
<トラヒックエージェントとアプリケーションの定義部>
# 後処理部の定義
Proc finish {
… …
}{
exit 0
}
# finish プロシージャの呼び出し
$ns at Endtime “finish”
# シミュレーションの開始
$ns run
3/22
情報工学講義第3講義資料(第3回、4回、5回)
6.
simple.tcl の解読
~/ns-allinone-2.33/ns-2.33/tcl/ex/simple.tcl
set ns [new Simulator]
# クラスオブジェクトの生成
$ns color 0 blue
$ns color 1 red
$ns color 2 white
# 色番号 0 を青に設定
# 色番号 1 を赤に設定
# 色番号 2 を白に設定
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
#
#
#
#
ノード
ノード
ノード
ノード
n0
n1
n2
n3
を定義
を定義
を定義
を定義
# ノードの形は、$n0 shape square のようにして変更 (circult, square, hexagon)
# ノードの色は、$n0 color 1 のようにして変更
set f [open out.tr w]
ルハンドラ f を作成
$ns trace-all $f
set nf [open out.nam w]
みファイルハンドラ nf を作成
$ns namtrace-all $nf
る
# トレースファイル(ファイル名 out.tr)用書き込みファイ
# 全てのトレースをファイルハンドラ f に保存する
# NAM トレースファイル(ファイル名 out.nam)用書き込
# 全ての NAM とレースをファイルハンドラ nf に保存す
# ノード n0 と n2 の間に 帯域 5Mb/s 遅延時間 2ms の双方向全二重リンクを定義。
# リンクキューは、DropTail 制御 (キューが満杯のとき、最後に到着したパケットを
捨てる。
# 片方向は、simplex-link を使う。キュー長、や TTL も指定可能
# ns queue-limit $n0 $n1 20 ~ n0-n1 のリンクのキュー長を 20 に設定
$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
# リンクを表示する際の傾きを指定して、各ノードの表示位置を定義する (NAM 用)
$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
# リンク (n2-n3) 上のキューをモニタリングする (NAM 用)
4/22
情報工学講義第3講義資料(第3回、4回、5回)
# リンクの出力キューに溜って行くパケットが上方向(0.5π[rad]=90°方向)に伸びる
ように設定
$ns duplex-link-op $n2 $n3 queuePos 0.5
# トラヒックソースの定義
set udp0 [new Agent/UDP]
$ns attach-agent $n0 $udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 attach-agent $udp0
# UDP エージェントの作成
# ノード n0 に配備
#デフォルトで UDP0 のクラスは 0(青)
# CBR トラヒックオブジェクトの生成
# UDP エージェントに CBR を割当
set udp1 [new Agent/UDP]
$ns attach-agent $n3 $udp1
$udp1 set class_ 1
# UDP1のクラス(class_)を1(赤)に設定
set cbr1 [new Application/Traffic/CBR]
$cbr1 attach-agent $udp1
# トラヒックシンクの定義
set null0 [new Agent/Null]
$ns attach-agent $n3 $null0
# Null エージェントオブジェクト
# ノード n3 の受信を定義
set null1 [new Agent/Null]
$ns attach-agent $n1 $null1
$ns connect $udp0 $null0
$ns connect $udp1 $null1
# UDP0 のシンクが null0 (n0→n3)
# UDP1 のシンクが null1 (n3→n1)
$ns at 1.0 "$cbr0 start"
$ns at 1.1 "$cbr1 start"
# 時刻 1.0 に CBR0 が開始
# 時刻 1.1 に CBR1 が開始
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"
# TCP エージェントオブジェクト
# TCP のクラス(class_)を 2(白)に設定
# TCPSink エージェントオブジェクト
# TCP ソースがノード n0
# TCP シンクがノード n3
# ソースとシンクを関連付け
# アプリケーションとして FTP を使用
# FTP をノード n0 から n3 へ
# 時刻 1.2 に ftp 開始
$ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
# 時刻 1.35 に TCP 終了
5/22
情報工学講義第3講義資料(第3回、4回、5回)
puts [$cbr0 set packetSize_]
puts [$cbr0 set interval_]
# CBR0 のパケットサイズを標準出力に出力
# CBR0 の時間間隔を標準出力に出力
$ns at 3.0 "finish"
# 時刻 3.0 でシミュレーション終了
proc finish {} {
global ns f nf
$ns flush-trace
close $f
close $nf
# 全レースを吐き出す
# トレースファイルを閉じる
# NAM トレースファイルを閉じる
puts "running nam..."
exec nam out.nam &
exit 0
# 標準出力にメッセージを出力
# NAM を実行
# ns 終了
}
$ns run
#シミュレーション開始
7. 細かな記述
7.1. パケットロスの定義例
#n2、n3 間でパケットロスが発生するように設定
#誤り率の設定 (ここでは 0.01 = 1% と設定)
set lrate 0.01
set loss_module [new ErrorModel]
$loss_module unit pkt
# パケット単位でロス
$loss_module set rate_ $lrate
$loss_module ranvar [new RandomVariable/Uniform]
$loss_module drop-target [new Agent/Null]
$ns lossmodel $loss_module $n2 $n3
7.2.
CBR(Constant Bit Rate)フローの定義例
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_sise_ 1000
# パケットサイズ
$cbr set rate_ 1mb
# レート
$cbr set random_ false
# 固定長 (?)
7.3. トラヒックの開始と終了の指定
simple.tcl での指定方法以外に、以下の指定方法があります。
$ns at 1.0 “$cbr start”
6/22
# 一様乱数
情報工学講義第3講義資料(第3回、4回、5回)
$ns at 1.2 “$ftp start”
$ns at 1.5 “$ftp stop”
$ns at 2.0 “$cbr stop”
7.4.
キューのモニタ
set qmon [ $ns monitor-queue $n2 $n3 [ open queue-mon w] 0.05 ]
リンク n2-n3 を、0.05 秒の間隔でモニタリングして、ファイル queue-mon に出力する。
[ $ns link $n2 $n3] queue-sample-timeout
モニタの開始
[ $ns link $n2 $n3] start-tracing
でも良いが、前者が詳細データを出力してくれる。
„ 時刻
„ キューに入力されたノード
„ キューから出力されたノード
„ キューのサイズ (パケットのバイト数 size_ で参照)
„ キュー内のパケット数 (pkt_ で参照)
„ キューに到着したパケット数 (parrivals_ で参照)
„ 送出されたパケット数 (pdepartures_ で参照)
„ 損失したパケット数 (pdrops_ で参照)
„ キューに到着したバイト数 (baarivals_ で参照)
„ 送出されたバイト数 (bdepartures_ で参照)
„ 損失したバイト数 (bdrops_ で参照)
例:キューの帯域変化を記録
set queue_size [open queue-size.tr w]
set qmon [$ns monitor-queue $n2 $n3 [open queue-mon w] 0.05]
[$ns link $n2 $n3] queue-sample-timeout
proc recored { } {
global ns qmon queue-size n2 n3
set time 0.05
# モニタ時間間隔
set now [ $ns now ]
# 現在時刻を取得
qmon instvar paarivals_ pdepartures_ pdrops_
puts $queue_size “$now [expr $parrivals_ - $pdepartures_ - $pdrops_ ]”
# $time 後に、このプロシージャを再帰的に呼び出す
$ns at [expr $now+$time] “record”
}
7/22
情報工学講義第3講義資料(第3回、4回、5回)
7.5. トラヒックジェネレータ
NS2 では、トラヒックジェネレータとして、FTP、HTTP、TELNET、CBR の他に、Exponential
(指数分布)
、Pareto(パレート分布)がある。
$NS_HOME/tcl/lib/ns-default.tcl を見ると、パラメータの初期値が分かる。
Burst time: トラヒックが on になっている平均時間
Idle time: トラヒックが off になっている平均時間
Packet size: パケット長の平均値
Rate: パケット生成率 (bps)
ポアソン分布を作りたい場合には、Burst time を 0 にし、Rate を大きな値にする (NS2 の
C++コードでそう実装されているという話)
7.6. 乱数ジェネレータ
シード値を 0 にセットすると、毎回違う乱数系列に、シード値を 0 以外の整数にセットすると
同じ乱数系列を使用する。
乱数ジェネレータの定義
set MyRng [ new RNG ]
$MyRng seed 3 ← シードを 3 に設定した例
パレート分布乱数 (avg_, shape_)
set rnd [ new RandomVariable/Pareto ]
$rnd use-rng $MyRng
$rnd set avg_ 10.0
; # 平均値。デフォルトは 1.0
$rnd set shape_ 1.2
; # シャープ値。デフォルトは 1.5
一様分布乱数 (min_, max_)
set rnd [ new RandomVariable/Uniform ]
$rnd use-rng $MyRng
$rnd set min_ 0.0
; # 最小値。デフォルトは 0.0
$rnd set max_ 5.0
; # 最大値。デフォルトは 1.0
指数分布乱数 (avg_)
set rnd [ new RandomVariable/Exponential ]
$rnd use-rng $MyRng
$rnd set avg_ 5.0
; # 平均値。デフォルトは 1.0
正規分布乱数 (avg_, std_)
set rnd [ new RandomVariable/Normal ]
$rnd use-rng $MyRng
$rnd set avg_ 0.5
; # 平均値。デフォルトは 0.0
$rnd set std_ 1.2
; # 分散値。デフォルトは 1.0
8/22
情報工学講義第3講義資料(第3回、4回、5回)
定常分布乱数 (val_)
set rnd [ new RandomVariable/Constant ]
$rnd use-rng $MyRng
$rnd set val_ 5.0
; # 定数値。デフォルトは 1.0
9/22
情報工学講義第3講義資料(第3回、4回、5回)
8.
トレースファイルの情報
トレースファイルはテキストファイルであり、以下のような形式でトレース情報が記録されて
いる。
|event|time|snode|tnode|pkttype|pktsize|flags|fid|saddr|daddr|seqn|pktid|
event: イベントタイプ
r: 受信
+: キューに到着
-: キューから送出
d: キューから廃棄
time: イベントの発生時刻
snode, dnode: 発信元ノードと宛先ノードの ID
pkttype: イベント発生時のパケットタイプ (UDP, TCP 等)
pktsize: パケットのサイズ (byte)
flags: 7 ビットのフラグ
C: ECN (Explicit Congestion Notification) Echo
P: Priority
-:無し
A: Congestion Action
E: Congestion Experienced
F: Fast start
N: ECN capable
fid: IPv6 のフローID
saddr, daddr: 送信元、宛先のアドレス。node.port の形で与えられる。
seqn: パケットのシーケンス番号
pktid: パケットの識別番号
10/22
情報工学講義第3講義資料(第3回、4回、5回)
9. 簡単な実験
(1) 参考書の第2章実験 2 Queue Monitoring
ソースは、11 で述べる URL からダウンロード可能である。
FTP
ボトルネックリンク
TCP
n0
2Mbps, 10ms
Sink
1.7Mbps, 20ms
n2
n1
null
2Mbps, 10ms
CBR FTP
start start
UDP
CBR
n3
FTP CBR
end end
Packet size: 1kB, rate 1Mbps
#Create a simulator object
set ns [new Simulator]
#Define different colors for data flows (for NAM)
$ns color 1 Blue
$ns color 2 Red
#Open the NAM trace file
set nf [open queue-out.nam w]
$ns namtrace-all $nf
#Open the Trace file
set tf [open queue-out.tr w]
$ns trace-all $tf
#Open the Queue trace files
set queue_size [open queue-size.tr w]
set queue_band [open queue-band.tr w]
set queue_lost [open queue-drop.tr w]
#Define a 'finish' procedure
proc finish {} {
global ns nf tf queue_size queue_band queue_lost
$ns flush-trace
11/22
情報工学講義第3講義資料(第3回、4回、5回)
#Close the NAM trace file
close $nf
#Close the Trace file
close $tf
close $queue_size
close $queue_band
close $queue_lost
#Execute NAM on the trace file
#exec nam queue-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 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns duplex-link $n2 $n3 1.7Mb 20ms DropTail
#Set Queue Size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10
#Give node position (for 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
#Monitor the queue for link (n2-n3). (for NAM)
$ns duplex-link-op $n2 $n3 queuePos 0.5
#Setup a TCP connection
set tcp [new Agent/TCP]
$tcp set class_ 2
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink]
12/22
情報工学講義第3講義資料(第3回、4回、5回)
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1
#Setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP
#Setup a UDP connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
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
# Set Queue Moniroting
set qmon [$ns monitor-queue $n2 $n3 [open queue-mon w] 0.05]
[$ns link $n2 $n3] queue-sample-timeout
#[$ns link $n2 $n3] start-tracing
proc record { } {
global ns qmon queue_size queue_band queue_lost n2 n3
set time 0.05
set now [$ns now]
#
$qmon instvar parrivals_ pdepartures_ bdrops_ bdepartures_ pdrops_
puts $queue_size "$now [expr $parrivals_-$pdepartures_-$pdrops_]"
puts $queue_band "$now [expr $bdepartures_*8/1024/$time]"
set bdepartures_ 0
puts $queue_lost "$now [expr $pdrops_/$now]"
puts $queue_lost "$now [expr $pdrops_]"
13/22
情報工学講義第3講義資料(第3回、4回、5回)
$ns at [expr $now+$time] "record"
}
#Schedule events for the CBR and FTP agents
$ns at 0.1 "record"
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 4.0 "$ftp stop"
$ns at 4.5 "$cbr stop"
#Detach tcp and sink agents (not really necessary)
$ns at 4.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Print CBR packet size and interval
puts "CBR packet size = [$cbr set packet_size_]"
puts "CBR interval = [$cbr set interval_]"
#Run the simulation
$ns run
シミュレーション実行後、queue-size.tr、queue-band.tr、queue-drop.tr という三つのファイル
が生成されている。
xgraph コマンドを利用して、ファイルを可視化できる。
% xgraph queue-size.tr –geometry 800x400 –t “Queue size (pkts)” –x “secs” –y “pkts” –fg
darkblue &
% xgraph queue-band.tr –geometry 800x400 –t “Bandwidth (Kbps)” –x “secs” –y “Kbps” –fg
white &
% xgraph queue-drop.tr –geometry 800x400 –t “Drop rate (pkts/sec)” –x “secs” –y “pkts” –fg
red &
キューサイズ、遅延、帯域パラメータを変えて実験を行ってみよう!
14/22
情報工学講義第3講義資料(第3回、4回、5回)
(2) 簡単な TCP コネクション及びボトルネックルータの状態表示
ソースは手で入力しましょう。
行の左端の 1: とかは行番号なので入力不要です。ファイル名を 9tcp.tcl とします。
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
# create simulator
set ns [new Simulator]
# TCP parameter set
Agent/TCP set packetsize_ 1000
Agent/TCP set window_ 1000000
# create nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n5 [$ns node]; # There is no n4.
set n6 [$ns node]
# create links
## bottleneck link between n5 and n6
$ns duplex-link $n5 $n6 10Mb 50ms DropTail
$ns queue-limit $n5 $n6 100
## access link between n0 and n5
$ns duplex-link $n0 $n5 100Mb 5ms DropTail
$ns queue-limit $n0 $n5 10000
## access link between n1 and n5
$ns duplex-link $n1 $n5 100Mb 5ms DropTail
$ns queue-limit $n1 $n5 10000
## access link between n6 and n2
15/22
情報工学講義第3講義資料(第3回、4回、5回)
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
$ns duplex-link $n6 $n2 100Mb 5ms DropTail
$ns queue-limit $n6 $n2 10000
## access link between n6 and n3
$ns duplex-link $n6 $n3 100Mb 5ms DropTail
$ns queue-limit $n6 $n3 10000
# create TCP senders
set tcp_s1 [new Agent/TCP/Reno]
set tcp_s2 [new Agent/TCP/Reno]
# create TCP receivers
set tcp_r1 [new Agent/TCPSink]
set tcp_r2 [new Agent/TCPSink]
# attach TCP agents to nodes
$ns attach-agent $n0 $tcp_s1
$ns attach-agent $n1 $tcp_s2
$ns attach-agent $n2 $tcp_r1
$ns attach-agent $n3 $tcp_r2
# set TCP connections
$ns connect $tcp_s1 $tcp_r1
$ns connect $tcp_s2 $tcp_r2
# create FTP applications
set ftp1 [new Application/FTP]
set ftp2 [new Application/FTP]
# attach FTP agents to TCP connections
$ftp1 attach-agent $tcp_s1
$ftp2 attach-agent $tcp_s2
# Filename for trace output
set tcpf1 [open 9tcp1.tr w]
set tcpf2 [open 9tcp2.tr w]
# Trace setting
Agent/TCP set trace_all_oneline_ true
# Trace congestion Window for TCP connection 1 and 2
$tcp_s1 trace cwnd_
$tcp_s2 trace cwnd_
# set trace
16/22
情報工学講義第3講義資料(第3回、4回、5回)
72: $tcp_s1 attach-trace $tcpf1
73: $tcp_s2 attach-trace $tcpf2
74:
75: # Queue Trace
76: set queue_size [open 9tcpqueue.tr w]
77: set qmon [$ns monitor-queue $n5 $n6 [open queue-mon w] 0.05]
78: [$ns link $n5 $n6] queue-sample-timeout
79:
80: proc record { } {
81:
global ns qmon queue_size n5 n6
82:
83:
set time 0.05
84:
set now [$ns now]
85:
86:
$qmon instvar parrivals_ pdepartures_ bdrops_ bdepartures_ pdrops_
87:
puts $queue_size "$now [expr $parrivals_-$pdepartures_-$pdrops_]"
88:
89:
$ns at [expr $now+$time] "record"
90: }
91:
92: # set start time of FTP transmission
93: $ns at 0 “record”
94: $ns at 0 “$ftp1 start”
95: $ns at 30 “$ftp2 start”
96:
97: # finish
98: $ns at 100 “exit 0”
99:
100: $ns run
101: exit
シミュレーションは、
% ns 9tcp.tcl
で実行します。シミュレーション終了後、9tcp1.tr、9tcp2.tr、9tcpqueue.tr の三個のファイル
ができます。
%cat 9tcp1.tr |./column.pl 0 6 |xgraph
%cat 9tcp2.tr |./column.pl 0 6 |xgraph
で TCP congestion ウィンドウサイズの変化を見ることができます。column.pl は 11 に記述。
応用編:二つのグラフを重ねてみましょう。
% xgraph 9tcpqueue.tr –geometry 800x400 –t “Queue size (pkts)” –x “secs” –y “pkts” –fg
darkblue &
で、0.05 秒毎のキュー長の変化を見ることができます。
17/22
情報工学講義第3講義資料(第3回、4回、5回)
10. 指数分布トラヒックジェネレータの例
参考書の第2章実験 4Exponential Traffic Generators
ソースは、11 で述べる URL からダウンロード可能である。
n0
1Mbps, 100ms
1Mbps, 100ms
1Mbps, 100ms
EXP
UDP
n1
Packet size: 100kB
Burst: 2sec
Idle: 1sec
Rate: 100K, 200K, 300K
Sink0
n3
n2
1Mbps, 100ms
#Create a simulator object
set ns [new Simulator]
#Open the output files
set f1 [open out1.tr w]
set f2 [open out2.tr w]
set f3 [open out3.tr w]
#Create 5 nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
set n4 [$ns node]
#Connect the nodes
$ns duplex-link $n0 $n3 1Mb 100ms DropTail
$ns duplex-link $n1 $n3 1Mb 100ms DropTail
$ns duplex-link $n2 $n3 1Mb 100ms DropTail
$ns duplex-link $n3 $n4 1Mb 100ms DropTail
#Define a 'finish' procedure
proc finish {} {
global f1 f2 f3
#Close the output files
close $f1
18/22
n4 Sink1
Sink2
情報工学講義第3講義資料(第3回、4回、5回)
close $f2
close $f3
#Call xgraph to display the results
exec xgraph out1.tr out2.tr out3.tr -geometry 800x400 \
-t "Bandwidth" -x "Times" -y "MBps" &
exit 0
}
#Define a procedure that attaches a UDP agent to a previously created node
#'node' and attaches an Expoo traffic generator to the agent with the
#characteristic values 'size' for packet size 'burst' for burst time,
#'idle' for idle time and 'rate' for burst peak rate. The procedure connects
#the source with the previously defined traffic sink 'sink' and returns the
#source object.
proc attach-expoo-traffic { node sink size burst idle rate } {
#Get an instance of the simulator
set ns [Simulator instance]
#Create a UDP agent and attach it to the node
set source [new Agent/UDP]
$ns attach-agent $node $source
#Create an Expoo traffic agent and set its configuration parameters
set traffic [new Application/Traffic/Exponential]
$traffic set packetSize_ $size
$traffic set burst_time_ $burst
$traffic set idle_time_ $idle
$traffic set rate_ $rate
# Attach traffic source to the traffic generator
$traffic attach-agent $source
#Connect the source and the sink
$ns connect $source $sink
return $traffic
}
#Define a procedure which periodically records the bandwidth received by the
#three traffic sinks sink0/1/2 and writes it to the three files f1/2/3.
19/22
情報工学講義第3講義資料(第3回、4回、5回)
proc record {} {
global sink0 sink1 sink2 f1 f2 f3
#Get an instance of the simulator
set ns [Simulator instance]
#Set the time after which the procedure should be called again
set time 0.5
#How many bytes have been received by the traffic sinks?
set bw0 [$sink0 set bytes_]
set bw1 [$sink1 set bytes_]
set bw2 [$sink2 set bytes_]
#Get the current time
set now [$ns now]
#Calculate the bandwidth (in MBit/s) and write it to the files
puts $f1 "$now [expr $bw0/$time*8/1000000]"
puts $f2 "$now [expr $bw1/$time*8/1000000]"
puts $f3 "$now [expr $bw2/$time*8/1000000]"
#Reset the bytes_ values on the traffic sinks
$sink0 set bytes_ 0
$sink1 set bytes_ 0
$sink2 set bytes_ 0
#Re-schedule the procedure
$ns at [expr $now+$time] "record"
}
#Create three traffic sinks and attach them to the node n4
set sink0 [new Agent/LossMonitor]
set sink1 [new Agent/LossMonitor]
set sink2 [new Agent/LossMonitor]
$ns attach-agent $n4 $sink0
$ns attach-agent $n4 $sink1
$ns attach-agent $n4 $sink2
#Create three traffic sources
set source0 [attach-expoo-traffic $n0 $sink0 200 2s 1s 100k]
set source1 [attach-expoo-traffic $n1 $sink1 200 2s 1s 200k]
set source2 [attach-expoo-traffic $n2 $sink2 200 2s 1s 300k]
#Start logging the received bandwidth
$ns at 0.0 "record"
#Start the traffic sources
$ns at 10.0 "$source0 start"
20/22
情報工学講義第3講義資料(第3回、4回、5回)
$ns at 10.0 "$source1 start"
$ns at 10.0 "$source2 start"
#Stop the traffic sources
$ns at 50.0 "$source0 stop"
$ns at 50.0 "$source1 stop"
$ns at 50.0 "$source2 stop"
#Call the finish procedure after 60 seconds simulation time
$ns at 60.0 "finish"
#Run the simulation
$ns run
応用:フローのパラメータを変えるとどうなるか
応用:情報収集する時間間隔を 0.1, 0.3, 0.5, 0.7, 0.9 ms と変えると実験結果はどうなるか
11. サンプルと解析ツール
参考書:森北出版 NS2 によるネットワークシミュレーションで紹介されている ns2 シミュレ
ーションのサンプルとツールが、http://www.morikita.co.jp/soft/84621/ で公開されている。
主要なツールの使い方を紹介する。
11.1. throughput.pl
スループットを計算してくれる。
througput.pl <trace file> <flow type> <required node> <granularity>
% throughput.pl out.tr ucb 3 1 > out.thp … ファイル out.thp に出力
% throughput.pl out.tr ucb 3 1 | xgraph
… xgraph でグラフ化
11.2. column.pl
トレースファイルから列を分割して取り出す。
column.pl <col1> <col2> …
% cat out.tr |column.pl 1 3 8
11.3. jitter.pl
ジッタ解析してくれる。
jitter.pl [-h] [-t flow-type] [-l1 node#] [-l2 node#] trace_file
% jitter.pl –t cbr –l1 1 –l2 3 out.tr
% jitter.pl –t cbr –l1 1 –l2 3 out.tr | tail +2 |xgraph
11.4. stats.pl
ボトルネックリンクの解析をしてくれる。
Stats.pl [-h] [-t flow-type] [-v] [-m max-bandwidth] [-l1 node#] [-l2 node#] trace_file
パケットロス数、送信パケット数、スループット、使用率等が出力される。
21/22
情報工学講義第3講義資料(第3回、4回、5回)
12. 自由実験
a) 適当にサンプルを変更して、自由なトポロジーで、トラヒックパラメータを変更してキュー
長分布や遅延時間分布を調べてみよう。
b) ~/ns-allinone-2.33/ns-2.33/tcl/ex/ にはたくさんのサンプルが置いてあります。
simple-*.tcl を実行してから、スクリプトを解読したり、パラメータを変更して実行してみま
しょう。
22/22