74
NS2 tutorial ChihHeng, Ke ( 柯柯柯 ) Phd Candidate, EE Department, NCKU Email: [email protected] MSN: [email protected] Skype: smallko2001

NS2 tutorial

Embed Size (px)

DESCRIPTION

NS2 tutorial. ChihHeng, Ke ( 柯志亨 ) Phd Candidate, EE Department, NCKU Email: [email protected] MSN: [email protected] Skype: smallko2001. Agenda. NS2 setup nsBench How to measure the loss rate, throughput, delay, jitter ? Parse the trace file Modify the C++ codes - PowerPoint PPT Presentation

Citation preview

Page 1: NS2 tutorial

NS2 tutorial

ChihHeng, Ke ( 柯志亨 )

Phd Candidate, EE Department, NCKUEmail: [email protected]: [email protected]: smallko2001

Page 2: NS2 tutorial

Agenda• NS2 setup • nsBench• How to measure the loss rate, throughput, delay, jitter ?

– Parse the trace file– Modify the C++ codes

• How to use the gnuplot to draw the simulation results?• Trace the C++ codes

– CBR– DropTail– 802.11

• Wireless Error Model• 802.11e (wireless network with QoS support---EDCF)• My research: video and audio simulations

Page 3: NS2 tutorial

NS2 setup --- 常見的問題• Cygwin 的安裝

– gcc 版本• NS2 的安裝

– 路徑設定– NAM

• 安裝多個 NS2 版本

Page 4: NS2 tutorial

nsBench

• 直接展示操作• 介紹 trace file format• 介紹如何使用 awk 來分析 trace file• 如何使用 gnuplot 來畫圖• 介紹如何安裝 mudp, mudpsink, mtcpsink來量測 UDP 或 TCP 的 Throughput, Delay,

Jitter, Loss Rate ( 不需要再分析原本的 trace file)

Page 5: NS2 tutorial

802.11 (DCF)

• How to send a multicast packet over 802_11 network.ppt

• How to receive a multicast packet.ppt

• How to send a unicast packet.ppt

• How to receive a unicast packet.ppt

Page 6: NS2 tutorial

802.11e (EDCF)

http://www.tkn.tu-berlin.de/research/802.11e_ns2/

Page 7: NS2 tutorial

# globals and flagsset ns [new Simulator]

#number of nodesset num_wired_nodes 1set num_mobile_nodes 1set num_bs_nodes 1 ;# number of base stationsset num_nodes [expr $num_wired_nodes + $num_mobile_nodes + $num_bs_nodes]set bs_id $num_wired_nodes

# Parameter for wireless nodesset opt(chan) Channel/WirelessChannel ;# channel typeset opt(prop) Propagation/TwoRayGround ;# radio-propagation modelset opt(netif) Phy/WirelessPhy ;# network interface typeset opt(mac) Mac/802_11e ;# MAC typeset opt(ifq) Queue/DTail/PriQ ;# interface queue typeset opt(ifqlen) 50set opt(ll) LL ;# link layer typeset opt(ant) Antenna/OmniAntenna ;# antenna modelset opt(ifqlen) 50 ;# max packet in ifqset opt(adhocRouting) NOAH ;# routing protocolset opt(x) 670 ;# X dimension of the topographyset opt(y) 670 ;# Y dimension of the topography

#smallko add the following two linesMac/802_11e set dataRate_ 1MbMac/802_11e set basicRate_ 1Mb

BS MH

multi_udpflows_802_11e.tc

Page 8: NS2 tutorial

#set up for hierarchical routing#(needed for routing over a basestation)$ns node-config -addressType hierarchicalAddrParams set domain_num_ 2 ;# domain numberlappend cluster_num 1 1 ;# cluster number for each domain AddrParams set cluster_num_ $cluster_numlappend eilastlevel $num_wired_nodes [expr $num_mobile_nodes + $num_bs_nodes] ;# number of nodes for each cluster AddrParams set nodes_num_ $eilastlevel

#Open the nam trace fileset nf [open out.nam w]$ns namtrace-all-wireless $nf $opt(x) $opt(y)set ntr [open out.tr w]$ns trace-all $ntr

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

# Create Godcreate-god [expr $num_mobile_nodes + $num_bs_nodes]

# creating wired nodesfor {set i 0} {$i < $num_wired_nodes} {incr i} {

set W($i) [$ns node 0.0.$i] puts "wired node $i created"}

Page 9: NS2 tutorial

# creating base station$ns node-config -adhocRouting $opt(adhocRouting) \ -llType $opt(ll) \ -macType $opt(mac) \ -ifqType $opt(ifq) \ -ifqLen $opt(ifqlen) \ -antType $opt(ant) \ -propType $opt(prop) \ -phyType $opt(netif) \ -channel $chan \ -topoInstance $topo \ -wiredRouting ON \ -agentTrace OFF \ -routerTrace OFF \ -macTrace OFF \ -movementTrace OFF

set BS(0) [$ns node 1.0.0]$BS(0) random-motion 0puts "Base-Station node $bs_id created"#provide some co-ord (fixed) to base station node$BS(0) set X_ 1.0$BS(0) set Y_ 2.0$BS(0) set Z_ 0.0 # creating mobile nodes$ns node-config -wiredRouting OFFfor {set i 0} {$i < $num_mobile_nodes} {incr i} { set wl_node_($i) [$ns node 1.0.[expr $i + 1]] $wl_node_($i) random-motion 0 ;# disable random motion puts "wireless node $i created ..." $wl_node_($i) base-station [AddrParams addr2id [$BS(0) node-addr]] $wl_node_($i) set X_ [expr $i * 10] $wl_node_($i) set Y_ [expr $i * 10] $wl_node_($i) set Z_ 0.0}

Page 10: NS2 tutorial

# linking of root to base-station node$ns duplex-link $W(0) $BS(0) 10Mb 2ms DropTail

# linking of wired nodes to root node#for {set i 1} {$i < $num_wired_nodes} {incr i} {# $ns duplex-link $W($i) $W(0) 10Mb 2ms DropTail#}

set src_udp0 [new Agent/UDP]$src_udp0 set class_ 0$src_udp0 set prio_ 0set dst_udp0 [new Agent/Null]$ns attach-agent $wl_node_(0) $src_udp0$ns attach-agent $W(0) $dst_udp0set app0 [new Application/Traffic/CBR]$app0 attach-agent $src_udp0$ns connect $src_udp0 $dst_udp0$ns at 0.0 "$app0 start"

set src_udp1 [new Agent/UDP]$src_udp1 set class_ 1$src_udp1 set prio_ 1set dst_udp1 [new Agent/Null]$ns attach-agent $wl_node_(0) $src_udp1$ns attach-agent $W(0) $dst_udp1set app1 [new Application/Traffic/CBR]$app1 attach-agent $src_udp1$ns connect $src_udp1 $dst_udp1$ns at 0.3 "$app1 start"

Page 11: NS2 tutorial

set src_udp2 [new Agent/UDP]$src_udp2 set class_ 2$src_udp2 set prio_ 2set dst_udp2 [new Agent/Null]$ns attach-agent $wl_node_(0) $src_udp2$ns attach-agent $W(0) $dst_udp2set app2 [new Application/Traffic/CBR]$app2 attach-agent $src_udp2$ns connect $src_udp2 $dst_udp2$ns at 0.1 "$app2 start"

set src_udp3 [new Agent/UDP]$src_udp3 set class_ 3$src_udp3 set prio_ 3set dst_udp3 [new Agent/Null]$ns attach-agent $wl_node_(0) $src_udp3$ns attach-agent $W(0) $dst_udp3set app3 [new Application/Traffic/CBR]$app3 attach-agent $src_udp3$ns connect $src_udp3 $dst_udp3$ns at 0.2 "$app3 start"

# Define node initial position in namfor {set i 0} {$i < $num_mobile_nodes} {incr i} { $ns initial_node_pos $wl_node_($i) 20 }

# Tell nodes when the simulation endsfor {set i 0} {$i < $num_mobile_nodes } {incr i} { $ns at 10.0 "$wl_node_($i) reset";}

$ns at 100.0 "$BS(0) reset";$ns at 100.0 "$app0 stop"$ns at 100.0 "$app1 stop"$ns at 100.0 "$app2 stop"$ns at 100.0 "$app3 stop"$ns at 110.0 "puts \"NS EXITING...\" ; $ns halt"

proc stop {} { global ns ntr nf close $ntr close $nf}

# run the simulation$ns run

Page 12: NS2 tutorial

# 這是測量 CBR 封包平均吞吐量 (average throughput) 的 awk 程式BEGIN {

i0=0;i1=0;i2=0;i3=0;

}{ action = $1; time = $2; from = $3; to = $4; type = $5; pktsize = $6; flow_id = $8; src = $9; dst = $10; seq_no = $11; packet_id = $12; if(action=="r" && from==1 && to==0 && flow_id==0) { pkt_byte_sum0[i0+1]=pkt_byte_sum0[i0]+ pktsize;

end_time0[i0] = time;i0 = i0+1;

}

if(action=="r" && from==1 && to==0 && flow_id==1) { pkt_byte_sum1[i1+1]=pkt_byte_sum1[i1]+ pktsize;

end_time1[i1] = time;i1 = i1+1;

}

measure-throughput.awk

Page 13: NS2 tutorial

if(action=="r" && from==1 && to==0 && flow_id==2) { pkt_byte_sum2[i2+1]=pkt_byte_sum2[i2]+ pktsize;

end_time2[i2] = time;i2 = i2+1;

}

if(action=="r" && from==1 && to==0 && flow_id==3) { pkt_byte_sum3[i3+1]=pkt_byte_sum3[i3]+ pktsize;

end_time3[i3] = time;i3 = i3+1;

}}END {

if(pkt_byte_sum0[i0]!=0)printf("average throughput of Flow 0:%f Bytes Per Second\n", pkt_byte_sum0[i0]/(end_tim

e0[i0-1]-end_time0[0]));if(pkt_byte_sum1[i1]!=0)

printf("average throughput of Flow 1:%f Bytes Per Second\n", pkt_byte_sum1[i1]/(end_time1[i1-1]-end_time1[0]));

if(pkt_byte_sum2[i2]!=0)printf("average throughput of Flow 2:%f Bytes Per Second\n", pkt_byte_sum2[i2]/(end_tim

e2[i2-1]-end_time2[0]));if(pkt_byte_sum3[i3]!=0)

printf("average throughput of Flow 3:%f Bytes Per Second\n", pkt_byte_sum3[i3]/(end_time3[i3-1]-end_time3[0]));

}

ns multi_udpflows_802_11e.tclawk –f measure-throughput.awk out.tr

average throughput of Flow 0:60341.412277 Bytes Per Secondaverage throughput of Flow 1:19359.667045 Bytes Per Secondaverage throughput of Flow 2:6124.881852 Bytes Per Secondaverage throughput of Flow 3:580.070331 Bytes Per Second

Page 14: NS2 tutorial

Wireless error model

• Random uniform model• Gilbert-Elliot (GE) model

Transport

IP

MAC

Phy

Transport

IP

MAC

Phy

Sender(BS/MH)Receiver(BS/MH)

Add Error modelAdd Error model

Page 15: NS2 tutorial

Uniform Distribution Model

NN

i

iCORECT pppp

1)1(1

1

Multicast (without any retransmission when packet is lost): Packet Loss Rate = PG

Unicast:The perceived correct rate at transport protocol is

where N is the maximum number of retransmission at MAC (DCF mode) and p is packet loss rate at physical layer. Consequently, the perceived lost rate at transport protocol is

NT pp

Page 16: NS2 tutorial

Gilbert-Elliott (GE) model

BBGG ppp

GBBG

BGG pp

p

G B

GBp

BGp

BBpGGp

0

1

0

1

Gp1

Gp1

Gp

GpG

0

1

0

1

B

a.

b. c.

Bp1

Bp1

Bp

Bp

In the “good” state (G) losses occur with low probability PG while in the “bad” state (B) they happen with high probability PB. The steady state probabilities of being in states G and B are and , respectively. The average packet loss rate produced by the Gilbert channel is

GBBG

GBB pp

p

Page 17: NS2 tutorial

UniformP~0.2

GE modelP~0.22

Page 18: NS2 tutorial

Simulation Topology

W(0) HA(base station)

MH

10Mbps, 10ms802.11

Page 19: NS2 tutorial

# 設定模擬結束時間set opt(stop) 250

# 設定 base station 的數目set opt(num_FA) 1

# 讀取使用者設定的參數proc getopt {argc argv} {

global opt lappend optlist nn for {set i 0} {$i < $argc} {incr i} {

set opt($i) [lindex $argv $i]}

}

getopt $argc $argv

set pGG $opt(0)set pBB $opt(1)set pG $opt(2)set pB $opt(3)set fname $opt(4)

set comm_type $opt(5)

set loss_model $opt(6)

Page 20: NS2 tutorial

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

# 使用 hierarchial addressing 的方式定址$ns_ node-config -addressType hierarchical

puts [ns-random 0]

# 設定有兩個 domain, 每個 domain 各有一個 cluster# 第一個 cluster(wired) 有一個 node, 第二個 cluster(wireles) 有兩個 node (base state + mobile node)AddrParams set domain_num_ 2lappend cluster_num 1 1AddrParams set cluster_num_ $cluster_numlappend eilastlevel 1 2AddrParams set nodes_num_ $eilastlevel

# 設定記錄檔 , 把模擬過程都記錄下來set tracefd [open test$fname w]$ns_ trace-all $tracefd

# 設定 mobile node 的個數set opt(nnn) 1

# 拓樸的範圍為 100m x 100mset topo [new Topography]$topo load_flatgrid 100 100

Tcl 程式碼

Page 21: NS2 tutorial

#create godset god_ [create-god [expr $opt(nnn)+$opt(num_FA)]]

# wired nodesset W(0) [$ns_ node 0.0.0]

# create channel set chan_ [new Channel/WirelessChannel]

# 設定節點參數$ns_ node-config -mobileIP ON \

-adhocRouting NOAH \ -llType LL \ -macType Mac/802_11 \ -ifqType Queue/DropTail/PriQueue \ -ifqLen 2000 \ -antType Antenna/OmniAntenna \

-propType Propagation/TwoRayGround \-phyType Phy/WirelessPhy \

-channel $chan_ \-topoInstance $topo \

-wiredRouting ON\-agentTrace ON \

-routerTrace ON \ -macTrace ON

Page 22: NS2 tutorial

# 設定 base station 節點set HA [$ns_ node 1.0.0]set HAnetif_ [$HA set netif_(0)]$HAnetif_ set-error-level $pGG $pBB $pG $pB $loss_model

# 設定 mobile node 的參數# 不需要 wired routing, 所以把此功能 off$ns_ node-config -wiredRouting OFFset MH(0) [$ns_ node 1.0.1]set MHnetif_(0) [$MH(0) set netif_(0)]$MHnetif_(0) set-error-level $pGG $pBB $pG $pB $loss_model# 把此 mobile node 跟前面的 base station 節點做連結[$MH(0) set regagent_] set home_agent_ [AddrParams addr2id [$HA node-addr]]

# 設定 base station 的位置在 (100.0, 100.0)$HA set X_ 100.0$HA set Y_ 100.0$HA set Z_ 0.0

# 設定 mobile node 的位置在 (80.0, 80.0)$MH(0) set X_ 80.0$MH(0) set Y_ 80.0$MH(0) set Z_ 0.0

Page 23: NS2 tutorial

# 在 wired node 和 base station 之間建立一條連線$ns_ duplex-link $W(0) $HA 10Mb 10ms DropTail

$ns_ at $opt(stop).1 "$MH(0) reset";$ns_ at $opt(stop).0001 "$W(0) reset"

# 建立一個 CBR 的應用程式 (wired node ---> base station)set udp0 [new Agent/UDP]$ns_ attach-agent $W(0) $udp0$udp0 set packetSize_ 1000set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0$cbr0 set rate_ 50000$cbr0 set packetSize_ 1000set null0 [new Agent/Null]$MH(0) attach $null0 3

# 當 base station 收到 cbr packet 時 , 可以根據使用者設定以 unicast 或 multicast 轉送封包到 mobile node)set forwarder_ [$HA set forwarder_]puts [$forwarder_ port]$ns_ connect $udp0 $forwarder_$forwarder_ dst-addr [AddrParams addr2id [$MH(0) node-addr]]$forwarder_ comm-type $comm_type

Page 24: NS2 tutorial

# 在 2.4 秒時 , 開始送出 cbr 封包$ns_ at 2.4 "$cbr0 start"

# 在 200.0 秒時 , 停止傳送$ns_ at 200.0 "$cbr0 stop"

$ns_ at $opt(stop).0002 "stop "$ns_ at $opt(stop).0003 "$ns_ halt"

# 設定一個 stop 的程序 proc stop {} { global ns_ tracefd # 關閉記錄檔 close $tracefd}

# 執行模擬$ns_ run

Page 25: NS2 tutorial

comm_type=1loss_model=0for x in 0.3 0.4 0.5 0.6do

pG=$xpB=0pGG=0pBB=0

ith=20for xx in $( seq 1 $ith )do

echo $xxecho $x

./ns.exe wireless_error.tcl $pGG $pBB $pG $pB $xx $comm_type $loss_modeldone./plot_err.pl test $ith $x

done

# ith is the number of iteration# if BSC channel packet loos rate =pG# loss_model: 0 for BSC, 1 for GE model# comm_type: 0 for broacdcast, 1 for unicast

run_bsc_error: 測試 uniform distribution 的 script

Page 26: NS2 tutorial

comm_type=0

loss_model=1 run_ge_error: 測試 GE model 的 script

for x in 0.4 0.5 0.6 0.7do

pG=0.01pB=$xpGG=0.96pBB=0.94

ith=20for xx in $( seq 1 $ith )do

echo $xxecho $x

./ns.exe wireless_error.tcl $pGG $pBB $pG $pB $xx $comm_type $loss_modeldone./plot_err.pl test $ith $x

done

# ith is the number of iteration# if BSC channel packet loos rate =pG# loss_model: 0 for BSC, 1 for GE model# comm_type: 0 for broacdcast, 1 for unicast

Page 27: NS2 tutorial

執行的方法 : ( 以 run_bsc_error 為例 )$./run_bsc_error

執行後會產生許多的記錄檔 , 底下為某次實驗的範例+ 2.4 0 1 cbr 1000 ------- 0 0.0.0.0 1.0.0.4 0 7 (wired trace file format)- 2.4 0 1 cbr 1000 ------- 0 0.0.0.0 1.0.0.4 0 7r 2.4108 0 1 cbr 1000 ------- 0 0.0.0.0 1.0.0.4 0 7r 2.410800000 _1_ AGT --- 7 cbr 1000 [0 0 0 0] ------- [0:0 4194304:4 31 0] [0] 0 0f 2.410800000 _1_ RTR --- 7 cbr 1020 [0 0 0 0] ------- [4194304:4 -1:3 32 0] [0] 0 0s 2.411475000 _1_ MAC --- 7 cbr 1072 [0 ffffffff 0 800] ------- [4194304:4 -1:3 32 0] [0] 0 0r 2.420051094 _2_ MAC --- 7 cbr 1020 [0 ffffffff 0 800] ------- [4194304:4 -1:3 32 0] [0] 1 0 (wireless)r 2.420076094 _2_ RTR --- 7 cbr 1020 [0 ffffffff 0 800] ------- [4194304:4 -1:3 32 0] [0] 1 0r 2.420076094 _2_ AGT --- 7 cbr 1020 [0 ffffffff 0 800] ------- [4194304:4 -1:3 31 0] [0] 1 0+ 2.56 0 1 cbr 1000 ------- 0 0.0.0.0 1.0.0.4 1 8- 2.56 0 1 cbr 1000 ------- 0 0.0.0.0 1.0.0.4 1 8r 2.5708 0 1 cbr 1000 ------- 0 0.0.0.0 1.0.0.4 1 8r 2.570800000 _1_ AGT --- 8 cbr 1000 [0 0 0 0] ------- [0:0 4194304:4 31 0] [1] 0 0f 2.570800000 _1_ RTR --- 8 cbr 1020 [0 0 0 0] ------- [4194304:4 -1:3 32 0] [1] 0 0s 2.571455000 _1_ MAC --- 8 cbr 1072 [0 ffffffff 0 800] ------- [4194304:4 -1:3 32 0] [1] 0 0r 2.580031094 _2_ MAC --- 8 cbr 1020 [0 ffffffff 0 800] ------- [4194304:4 -1:3 32 0] [1] 1 0r 2.580056094 _2_ RTR --- 8 cbr 1020 [0 ffffffff 0 800] ------- [4194304:4 -1:3 32 0] [1] 1 0r 2.580056094 _2_ AGT --- 8 cbr 1020 [0 ffffffff 0 800] ------- [4194304:4 -1:3 31 0] [1] 1 0

Page 28: NS2 tutorial

#!/usr/bin/perl

$event_filter = “[rs]”; 分析 trace file format的程式@ptype_filter = ("tcp", "cbr", "exp");

$ttype_filter = "MAC";

if (@ARGV < 1) { printf "usage plot_tp.pl [-s size] <trace file> [node1, ...]\n"; exit;}

$tracenametmp = shift;$numith =shift;$errorlevel=shift;@trace_type = ();

print "tracenametmp=$tracenametmp numith=$numith errorlevel=$errorlevel\n";

$tt=1;@iteration=();while($tt<=$numith){ push @iteration,$tt; $tt=$tt+1;}

Page 29: NS2 tutorial

$totalarverror=0.0;$indith=0;$maxseqno=0;

foreach $ith (@iteration){

$indith=$indith+1; $tracename="$tracenametmp$ith"; $totalpktsize=0; @src =(); @mh1 =();

open(infile, $tracename) or die "couldn't open $tracename"; while ($line = <infile>) {

++$line_no;

@entry = split(/[\s\(\)\[\]]+/, $line);

$event = $entry[0];$time = $entry[1];

Page 30: NS2 tutorial

# check for trace format and parse entry (wireless traffic trace format.)# The format of all records is not the same. if ($entry[2] =~ /_+(\d+)_+/) { # mobile $to = $1; next if ($entry[3] ne $ttype_filter); $pktid = $entry[5]; #die "packet error $pktid (line $line_no)" if ($packet_source{$pktid} == undef); $from = $packet_source{$pktid}; $type = $entry[6]; $size = $entry[7]; $packet_source{$pktid} = $to; $seqno=$entry[17];} else { # standard (wired traffic trace format) $to = $entry[3]; $pktid = $entry[11]; $from = $entry[2]; $type = $entry[4]; $size = $entry[5]; $seqno=$entry[10]; $packet_source{$pktid} = $to;}

Page 31: NS2 tutorial

# 判斷 event 是接收 (r) 且封包型態是 cbr 的記錄if(($event eq "r")&&($type eq "cbr")) {

$tmpname="mh1"; # 記錄從 HA 到 MH 的 packet seqno 到 $tmpname if ($to == 2) {

push @$tmpname,{ SEQNO => $seqno,TIME => $time}; } # 記錄從 W(0) 到 HA 的 packet seqno 到 $src

if($to == 1){push @src ,{ SEQNO => $seqno,TIME => $time};

}}

}

# 關閉檔案 close($infile); # 開啟檔案 (Open the file for writing) open(datafile, ">data_raw") or die "couldn't open data";

Page 32: NS2 tutorial

$error=0; $maxseqno=0; $tmpmaxseqno=0; foreach $srcdata (@src) {

$tmpname="mh1";$chk=0;

foreach $mhdata (@$tmpname) {

# 判斷 $src 中的記錄中是否可在 $tmpname 中找到相同的記錄 if($srcdata->{SEQNO}==$mhdata->{SEQNO}){

if ($chk==0) { # 把沒遺失的封包記錄為 0 print datafile "$srcdata->{SEQNO} 0\n"; $chk=1;}else { $dup=$dup+1;

} }

}

Page 33: NS2 tutorial

# 找不到相同記錄時 , 代表有封包遺失 if ($chk==0){ # 把遺失的封包記錄為 1 print datafile "$srcdata->{SEQNO} 1\n"; $error=$error+1;}

# 記錄目前處理封包的最大 seqno( 最後的值就是共傳送多少封包 )if($srcdata->{SEQNO}>$maxseqno){ $maxseqno=$srcdata->{SEQNO}; }

} print "errr $error\t $maxseqno\n"; # 單次平均的 error rate = error 發生的次數 / 總共的封包數 * 100% $arverror=$error/$maxseqno*100; $totalarverror=$totalarverror+$arverror;} # 總平均的 error rate = 全部 error rate 總和 / 模擬的字數$arverror=$totalarverror/$numith;print "Average error =\t$arverror \tpercent\n";# 開啟檔案 (Open the file for appending)open(cmd, ">>data.out");# 記錄 errorlevel 和模擬所得到的總平均 error rateprintf cmd "$errorlevel\t$arverror\n";# 關閉檔案close(cmd);

Page 34: NS2 tutorial

實驗結果 (1)p Simulated p (%)

1 0.1 10.0971659919028

2 0.2 20.2914979757085

3 0.3 30.2348178137652

4 0.4 39.7651821862348

5 0.5 50.085020242915

Uniform Distribution:I ran 20 iterations. Broadcasting is used.

Page 35: NS2 tutorial

實驗結果 (2)

Uniform Distribution:I ran 20 iterations. Unicast is used.Maximum Retransmission Times: 4

p Pr Simulated p (%)

1 0.3 0.0081 0.817813765182186

2 0.4 0.0256 2.47773279352227

3 0.5 0.0625 6.18218623481781

4 0.6 0.1296 12.7894736842105

Page 36: NS2 tutorial

實驗結果 (3)

PB Simulated p (%)

1 0.4 0.1660 16.170040485832 0.5 0.2060 22.13360323886643 0.6 0.2460 24.69230769230774 0.7 0.2860 28.0283400809717

GE model:Multicast is used. I ran 20 iterationsPG=0.01 PGG=0.96PBB=0.94

)/()( BGGBGBBBGG ppppppp

Page 37: NS2 tutorial

Toward more realistic network simulations of video transmission

Page 38: NS2 tutorial

Outline

• Introduction• Overview of Evalvid• Enhancement of Evalvid• One example

– Video transmission over BE and QoS network• Conclusion and Future works

Page 39: NS2 tutorial

Introduction (1)

• As mentioned in *, a survey of over 2246 research papers on the network published in the prominent IEEE journals and conferences revealed that over 51% of all publications on the network adopt computer simulation to verify their ideas and report network performance results.

• Therefore, network simulation tools are very important and helpful to modern network researchers.

* K. Pawlikowski, "Do Not Trust All Simulation Studies of Telecommunication Networks“, (invited paper) Proc. International Conference on Information Networking, ICOIN'03

Page 40: NS2 tutorial

Introduction (2)

• The ever-increasing demand for multimedia distribution in the Internet motivates research on how to provide better-delivered video quality through IP-based networks.

• Previous studies often use publicly available video traces to evaluate their proposed network mechanisms in a simulation environment.

• Results are usually presented using different performance metrics, such as the packet/frame loss rate, packet/frame jitter, effective frame loss rate, picture quality rating (PQR), and the fraction of decodable frames.

Page 41: NS2 tutorial

Introduction (3)

• Nevertheless, packet loss or jitter are network performance metrics and may be insufficient to adequately rate the perceived quality by an end user.

• In *, a 3% packet loss percentage could translate into a 30% frame error probability.

• Therefore, lower packet loss rate can not indicate better delivered video quality.

*J. M. Boyce, and R. D. Gaglianello, “Packet Loss Effects on MPEG Video Sent over the Public Internet,” In Proc. of the ACM Multimedia, September 1998.

Page 42: NS2 tutorial

Introduction (4)

• Although effective frame loss rate, PQR, and the fraction of decodable frames are application-level metrics, they are not as well known and acceptable as MOS (Mean Opinion Scores) and PSNR (Peak Signal Noise Ratio).

• To the best of my knowledge, no tool-set is publicly available to perform a comprehensive video quality evaluation of real video streams in network simulation environment.

• This drives me to develop one publicly available evaluation framework.

Page 43: NS2 tutorial

Introduction (5)

• My work was based on Evalvid.

• The primary aim of EvalVid is to assist researchers in evaluating their network designs or setups in terms of the perceived video quality by the end user over a real or simulated network.

• But the simulated environment provided by EvalVid is simply an error model to represent corrupted or missing packets in the real network.

• What I has done is to build the connecting interfaces between Evalvid and NS2. Then make it be more complete and useful to network researchers.

Page 44: NS2 tutorial

Overview of Evalvid

Coded Video

Page 45: NS2 tutorial

Video Source The video source can be either in the YUV QCIF (176 x 144)

or in the YUV CIF (352 x 288) formats.

QCIF

CIF

144 pixels

176 pixels 352 pixels

288 pixels

Page 46: NS2 tutorial

Video Encoder

• Currently, EvalVid supports two MPEG4 codecs, namely the NCTU codec [1] and ffmpeg [2].

[1]. http://megaera.ee.nctu.edu.tw/mpeg [2]. http://ffmpeg.sourceforge.net/index.php

Page 47: NS2 tutorial

Video Sender (VS)1. Reads the compressed video file from the output of the vide

o encoder

2. Fragments each large video frame into smaller segments

3. Transmits these segments via UDP/IP packets over a real or simulated network

4. For each transmitted UDP packet, the framework records the timestamp, the packet id, and the packet payload size in the sender trace file with the aid of third-party tools, such as tcp-dump or win-dump, if the network is a real link

Page 48: NS2 tutorial

Evaluate Trace (ET)• Based on the original encoded video file, the video trace file,

the sender trace file, and the receiver trace file, the ET component creates a frame/packet loss and frame/packet jitter report and generates a reconstructed video file, which corresponds to the possibly corrupted video found at the receiver side as it would be reproduced to an end user

• In principle, the generation of the possibly corrupted video can be regarded as a process of copying the original video file frame by frame, omitting frames indicated as lost or corrupted at the receiver side

Page 49: NS2 tutorial

Fix Video (FV)

• Digital video quality assessment is performed frame by frame.

• If the codec cannot handle missing frames (lost during transmission), the FV component is used to tackle this problem by inserting the last successfully decoded frame in the place of each lost frame as an error concealment technique

Page 50: NS2 tutorial

Peak Signal Noise Ratio (PSNR)• PSNR is one of the most widespread objective metrics to

assess the application-level QoS of video transmissions.• The following equation shows the definition of the PSNR

between the luminance component Y of source image S and destination image D:

2

0 0

1 ]j)i,(n,Yj)i,(n,[YNN

V

colN

=i

rowN

j=DS

rowcol

peak

PSNR(n)dB = 20 log10

where Vpeak = 2k-1 and k = number of bits per pixel (luminance component)

Page 51: NS2 tutorial

Mean Opinion Score (MOS)• MOS is a subjective metric to measure digital video quality

at the application level. This metric of the human quality impression is usually given on a scale that ranges from 1 (worst) to 5 (best).

PSNR[dB] MOS

>3731-3725-3120-25<20

5 (Excellent)4 (Good)3 (Fair)2 (Poor)1 (Bad)

Page 52: NS2 tutorial

Enhancement of Evalvid

Page 53: NS2 tutorial

Video transmission over BE and QoS network

• This example is provided in my web site(http://140.116.72.80/~smallko/ns2/Evalvid_in_NS2.htm)

Host A Host BRouter R1 Router R2

Best Effort Network: the queue management is DropTailQoS Network: the queue management is Weighted Random Early Detection (WRED) (I frame packets are marked as highest priority, P frame packets are marked as medium priority, B frame packets are marked as lowest priority)

Page 54: NS2 tutorial

Encoding the raw YUV video

• $mpeg4encoder.exe example.par //example.par…………………………………Source.Width = 176Source.Height = 144// set the first encoding frame to 0Source.FirstFrame = 0// set the last encoding frame to 399Source.LastFrame = 399//set the prefix file nameSource.FilePrefix = "foreman_qcif“

Scalability.Spatial.PredictionType = "PBB" // One of "PPP", "PBB"

After encoding, a compressed video file “foreman_qcif.cmp” can be found.

Page 55: NS2 tutorial

Get the Video Traffic Trace File• $MP4.exe –send 224.1.2.3 5555 1000 foreman_cif.cmp > st

This MP4.exe command will read the compressed file. Fragment each frameinto small packets with the maximum size of 1000 bytes. Then send this packets to IP:224.1.2.3 Port:5555. Here, the IP and Port is not important. Because we just need the frame information (stored in st file).

0 H 29 1 segm at 33 ms 1 I 3036 4 segm at 66 ms 2 P 659 1 segm at 99 ms 3 B 357 1 segm at 132 ms 4 B 374 1 segm at 165 ms 5 P 693 1 segm at 198 ms 6 B 420 1 segm at 231 ms 7 B 460 1 segm at 264 ms

st

Page 56: NS2 tutorial

Run the simulation script (1)

set ns [new Simulator]

set nd [open out.tr w]$ns trace-all $nd

set max_fragmented_size 1000

#add udp header(8 bytes) and IP header (20bytes)set packetSize 1028

set s1 [$ns node]set r1 [$ns node]set r2 [$ns node]set d1 [$ns node]

$ns duplex-link $s1 $r1 10Mb 1ms DropTail$ns simplex-link $r1 $r2 0.18Mb 10ms DropTail$ns simplex-link $r2 $r1 0.18Mb 10ms DropTail$ns duplex-link $r2 $d1 10Mb 1ms DropTail

set qr1r2 [[$ns link $r1 $r2] queue]$qr1r2 set limit_ 10

set udp1 [new Agent/myUDP]$ns attach-agent $s1 $udp1$udp1 set packetSize_ $packetSize$udp1 set_filename sd_beset null1 [new Agent/myUdpSink2] $ns attach-agent $d1 $null1$ns connect $udp1 $null1$null1 set_trace_filename rd_be

set original_file_name stset trace_file_name video1.datset original_file_id [open $original_file_name r]set trace_file_id [open $trace_file_name w]

Host A Host BRouter R1 Router R2

Page 57: NS2 tutorial

Run the simulation script (2)

set frame_count 0set last_time 0

while {[eof $original_file_id] == 0} { gets $original_file_id current_line scan $current_line "%d%s%d%s%s%s%d%s" no_ frametype_ length_ tmp1_ tmp2_ tmp3_ tmp4_ tmp5_ # 30 frames/sec. (1 sec -> 1000000us) set time [expr 1000 * 1000/30] if { $frametype_ == "I" } { set type_v 1 } if { $frametype_ == "P" } { set type_v 2 } if { $frametype_ == "B" } { set type_v 3 } if { $frametype_ == "H" } { set type_v 1 } puts $trace_file_id "$time $length_ $type_v $max_fragmented_size" incr frame_count}

Page 58: NS2 tutorial

Run the simulation script (3)

close $original_file_idclose $trace_file_idset end_sim_time [expr 1.0 * 1000/30 * ($frame_count + 1) / 1000]puts "$end_sim_time"set trace_file [new Tracefile]$trace_file filename $trace_file_nameset video1 [new Application/Traffic/myTrace2]$video1 attach-agent $udp1$video1 attach-tracefile $trace_file

proc finish {} { global ns nd $ns flush-trace close $nd exit 0}

$ns at 0.0 "$video1 start"$ns at $end_sim_time "$video1 stop"$ns at [expr $end_sim_time + 1.0] "$null1 closefile"$ns at [expr $end_sim_time + 1.0] "finish"

$ns run

Page 59: NS2 tutorial

Run the simulation script (4)

$ns be.tcl

0.033333 id 0 udp 29 0.066666 id 1 udp 1000 0.066666 id 2 udp 1000 0.066666 id 3 udp 1000 0.066666 id 4 udp 36 0.099999 id 5 udp 659 0.133332 id 6 udp 357 0.166665 id 7 udp 374 0.199998 id 8 udp 693 0.233331 id 9 udp 420 0.266664 id 10 udp 460

sd_be

0.047958 id 0 udp 29 0.126000 id 1 udp 1000 0.171689 id 2 udp 1000 0.217377 id 3 udp 1000 0.219451 id 4 udp 36 0.250482 id 5 udp 659 0.267352 id 6 udp 357 0.285232 id 7 udp 374 0.317532 id 8 udp 693 0.337225 id 9 udp 420 0.358945 id 10 udp 460

rd_be

Page 60: NS2 tutorial

Video Quality Evaluation (1)

• execute et program • $et.exe sd_be rd_be st foreman_cif.cmp err_be.cmp 0

p->nA:1022, p->nI:350, p->nP:222, p->nB:449 p->lA:161, p->lI:109, p->lP:22, p->lB:30 f->nA:301, f->nI:34, f->nP:67, f->nB:199 f->lA:54, f->lI:20, f->lP:13, f->lB:21

Packet/Frame lost report•The first line indicates that the total number of packets sent (p->nA) is 1022. It includes 350 I frame packets (p->nI); 222 P frame packets (p->nP); and 449 B frame packets (p->nB).•The second line indicates that the total number of packets lost (p->lA) is 161. It includes 109 I frame packets (p->lI); 22 P frame packets (p->lP); and 30 B frame packets (p->lB).

Page 61: NS2 tutorial

Video Quality Evaluation (2)

• Decode the video • $mpeg4decoder.exe err_be.cmp err_be  176  144  >  df_be

• Error concealment • $myfixyuv.exe df_be qcif 400 err_be.yuv myfix_be.yuv

• Calculate the average PSNR • $avgpsnr.exe 176 144 420 foreman_qcif.yuv myfix_be.yuv• avgerage psnr:24.363058

Page 62: NS2 tutorial

Run the qos.tcl and compare the results from be.tcl

Page 63: NS2 tutorial

Conclusion

• With the aid of this evaluation framework, researchers or practitioners have greater freedom to analyze their proposed network designs for video transmission without considering an appropriate tool-set.

Page 64: NS2 tutorial

Country / Company Research Group & Research Interests

America 1.Stanford CS grad student2.PhD candidate at Univ. of Houston

Australia 1.Lecture at La Trobe University

Canada 1. Carleton University: MPEG4 transmission evaluation over Resilient Packet Ring Networks

China

Egypt 1. search assistant at Computer division Faculty of Engineering, Suez Canal University

France 1. Doctorant student at Paris 13 university

Germany 1. TKN

Iceland 1. master student at University of Iceland: Modelling the Icelandic Health Network

India 1.Asst.Prof. at Engg. College:Wireless MANET and multimedia transmission2.PG student: Accelerating peer to peer video streaming on Multipoint to point communication

Indonesia 1. collage students at STTTelkom Bandung: video streaming over umts network

Israel 1. Ph.D : sumulation of the behaviour of the traffic manager in the MPLS-diffserv environment.

Japan 1. PhD student: real-time P2P video streaming

Korea 1. student at Kunsan National University: streaming video over the Internet

Lebanon 1. using Evalvid to investigate the performance of 802.11 and 802.11e

Mexico 1. IATM

Norway 1. PhD student at NTNU:

Singapore 1.PhD student at National University of Singapore2.student at Nanyang Technological University: video transmission over MANETs

Page 65: NS2 tutorial

Country / Company Research Group & Research Interests

Sweden 1. PhD student at KTH: cross-layer issues in wireless multimedia

Switzerland 1. PhD student at EPFL

Taiwan 1. Master students at National Yulin of University of Science & Technology2. Master student at National Taiwan University3. Master student at National Chung Hsing University

Tunisia 1. researcher in the tunisian supcom university: I have optimized QoS on ad hoc network and implement it in ns-2 and i want to experiment it with MPEG video and other multimedia applications

U.K. 1. PhD student at University of London2. master student: in the area of QoS3. postgraduate student from University of Hertfordshire: video evaluation over Mobile Ad hoc Network4. B(Eng) Computer Systems student at Brunel University: video in ad hoc network5. Research student at Sheffield Hallam University

Company 1.Ericsson: test the wireless channel and the channels error-probability that at what extent it affects a video transmission.2.Motorola:3.Nokia: video transmission project (in China)

Unknown No information left in the email

Page 66: NS2 tutorial

Audio Simulation

Reference: http://www.tkn.tu-berlin.de/research/voiptrace/qofis_v2.pdfSource code: http://www.tkn.tu-berlin.de/research/voiptrace/qofis_v2.tar.bz2

Page 67: NS2 tutorial
Page 68: NS2 tutorial

proc getopt {argc argv} {global opt

lappend optlist nn for {set i 0} {$i < $argc} {incr i} {

set opt($i) [lindex $argv $i]}

}

getopt $argc $argvset ns [new Simulator]

set num_wired_nodes 3set num_mobile_nodes 2set num_bs_nodes 1 ;# number of base stationsset num_nodes [expr $num_wired_nodes + $num_mobile_nodes + $num_bs_nodes]set bs_id $num_wired_nodes

set opt(chan) Channel/WirelessChannel ;# channel typeset opt(prop) Propagation/TwoRayGround ;# radio-propagation modelset opt(netif) Phy/WirelessPhy ;# network interface typeset opt(mac) Mac/802_11 ;# MAC typeset opt(ifq) Queue/DropTail/PriQueue ;# interface queue typeset opt(ifqlen) 50set opt(ll) LL ;# link layer typeset opt(ant) Antenna/OmniAntenna ;# antenna modelset opt(ifqlen) 50 ;# max packet in ifqset opt(adhocRouting) DSDV ;# routing protocol

set opt(x) 670 ;# X dimension of the topographyset opt(y) 670 ;# Y dimension of the topography

BSsender receiver

1VOIP_802_11.tcl

Page 69: NS2 tutorial

$ns node-config -addressType hierarchicalAddrParams set domain_num_ 2 ;# domain numberlappend cluster_num 1 1 ;# cluster number for each domain AddrParams set cluster_num_ $cluster_numlappend eilastlevel $num_wired_nodes [expr $num_mobile_nodes + $num_bs_nodes] ;# number of nodes for each cluster AddrParams set nodes_num_ $eilastlevel

#set nf [open out.nam w]set nf [open /dev/null w]$ns namtrace-all-wireless $nf $opt(x) $opt(y)#set ntr [open out.tr w]set ntr [open /dev/null w]$ns trace-all $ntr

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

create-god [expr $num_mobile_nodes + $num_bs_nodes]

for {set i 0} {$i < $num_wired_nodes} {incr i} {set W($i) [$ns node 0.0.$i]

puts "wired node $i created"}

Page 70: NS2 tutorial

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

set BS(0) [$ns node 1.0.0]$BS(0) random-motion 0puts "Base-Station node $bs_id created"#provide some co-ord (fixed) to base station nodeiset X_bstn 20.0set Y_bstn 20.0

$BS(0) set X_ $X_bstn$BS(0) set Y_ $Y_bstn$BS(0) set Z_ 0.0

Page 71: NS2 tutorial

set pi 3.14159265set om [expr 2 * $pi / $num_mobile_nodes]set radius 5 $ns node-config -wiredRouting OFFfor {set i 0} {$i < $num_mobile_nodes} {incr i} { set wl_node_($i) [$ns node 1.0.[expr $i + 1]]

$wl_node_($i) random-motion 0 ;# disable random motion #puts "wireless node $i created ..." $wl_node_($i) base-station [AddrParams addr2id [$BS(0) node-addr]] $wl_node_($i) set X_ [expr $X_bstn + [expr $radius * cos([expr $om * ($i + 1)])]] $wl_node_($i) set Y_ [expr $Y_bstn + [expr $radius * sin([expr $om * ($i + 1)])]] $wl_node_($i) set Z_ 0.0

}

# linking of root wired station to base-station node$ns duplex-link $W(0) $BS(0) 100Mb 0.00005ms DropTail

# linking of wired nodes to root node, link length 100mfor {set i 1} {$i < $num_wired_nodes} {incr i} { $ns duplex-link $W($i) $W(0) 100Mb 0.0005ms DropTail}

#loss model->(0:random uniform; 1:GE)set wl_phy [$wl_node_(0) set netif_(0)]$wl_phy set-error-level $opt(0) $opt(1) $opt(2) $opt(3) $opt(4)

Page 72: NS2 tutorial

set rtp0 [new Agent/RTPdata]$ns attach-agent $wl_node_(0) $rtp0$rtp0 set prio_ 0

set rtp1 [new Agent/RTPdata]$ns attach-agent $W(1) $rtp1$rtp1 set prio_ 0

set voip0 [new Application/Traffic/VOIP]$voip0 set status_ 0 ;# receiving$voip0 set_OUTFile rxstats_wl0_w1.out$voip0 attach-agent $rtp0

set voip1 [new Application/Traffic/VOIP]$voip1 set status_ 1 ; #sending$voip1 set_BITFile in.bit$voip1 set interval_ 0.02$voip1 set packetSize_ 160$voip1 attach-agent $rtp1

$ns connect $rtp0 $rtp1set jitter0 [uniform 0 0.02]set jitter1 [uniform 0 0.02]$ns at [expr $jitter0 + 1.0] "$voip0 start"$ns at [expr $jitter1 + 1.0] "$voip1 start"

Page 73: NS2 tutorial

# Define node initial position in namfor {set i 0} {$i < $num_mobile_nodes} {incr i} { $ns initial_node_pos $wl_node_($i) 60 }

## Tell nodes when the simulation ends#$ns at 700.0 "$voip0 stop"$ns at 700.0 "$voip1 stop"$ns at 701.0 "$wl_node_(0) reset";$ns at 701.0 "$wl_node_(1) reset";$ns at 701.0 "$BS(0) reset";$ns at 702.0 "puts \"NS EXITING...\" ; $ns halt"

proc stop {} { global ns ntr nf close $ntr close $nf}

# run the simulation$ns run

Page 74: NS2 tutorial

Script to run 1VOIP_802_11.tcl

#!/bin/shrm rxstats_wl0_w1.outrm __g729in.bitrm __g729out.bitrm result000.swrm result000.wavrm _pesq_itu_results.txtrm _pesq_results.txtrm playout_result.txtns 1VOIP_802_11.tcl 0 0 0.6 0 0./playout2.exe -c G729 -t rxstats_wl0_w1.out -d 0.150 -f 0.01 -m -r in.swsox result000.sw result000.wav

After simulation, you will get PesqMOS and Emodel (R-factor).

Also, you can use MediaPlayer to compare the original sound file with the distorted one.