RED Queue Monitor Example
Example 5中OTcl脚本建立了网络拓扑并运行Figure 15所描述的模拟场景。
备注RED队列可以持有最多25个数据包用于连接r1-r2, 并且我们可以看到RED队列如何通过测量当前队列大小和平均队列大小的动态变化来运行。
Figure 15. RED Queue Monitor Example Setup
Example 5. RED Queue Monitor Simulation Script
上面的脚本中要注意到
- 高级的模拟对象的成员函数create-connection用来建立TCP连接。
- 在tracing a queue(monitoring)那段代码中, 首先给variable带入RED queue对象, 调用它的成员函数trace去监听当前队列大小(curq_)和平均队列大小(avg_), 然后让他们把结果写入到"all.q"文件中。下面是两个队列追踪信息输出的格式,分别是平均队列大小average queue size和当前队列大小current queue size.
a time avg_q_size Q time crnt_q_size |
当模拟完成时,除了关闭"all.q"文件的以外,所有RED队列监听需要的命令都已经完成, 所以剩下的工作就是设计并编写post-simulation处理。 这个脚本使用awk去把监听信息转换到XGraph(a plotter)输入的格式, 然后启动它去根据已给信息画出分析图(见 Figure 16)。
Figure 16. Red Queue Trace Graph
red.tcl代码
# Polly Huang 8-7-98
set ns [new Simulator]
#
# Create a simple six node topology:
#
# s1 s3
# \ /
# 10Mb,2ms \ 1.5Mb,20ms / 10Mb,4ms
# r1 --------- r2
# 10Mb,3ms / \ 10Mb,5ms
# / \
# s2 s4
#
set node_(s1) [$ns node]
set node_(s2) [$ns node]
set node_(r1) [$ns node]
set node_(r2) [$ns node]
set node_(s3) [$ns node]
set node_(s4) [$ns node]
$ns duplex-link $node_(s1) $node_(r1) 10Mb 2ms DropTail
$ns duplex-link $node_(s2) $node_(r1) 10Mb 3ms DropTail
$ns duplex-link $node_(r1) $node_(r2) 1.5Mb 20ms RED
$ns queue-limit $node_(r1) $node_(r2) 25
$ns queue-limit $node_(r2) $node_(r1) 25
$ns duplex-link $node_(s3) $node_(r2) 10Mb 4ms DropTail
$ns duplex-link $node_(s4) $node_(r2) 10Mb 5ms DropTail
$ns duplex-link-op $node_(s1) $node_(r1) orient right-down
$ns duplex-link-op $node_(s2) $node_(r1) orient right-up
$ns duplex-link-op $node_(r1) $node_(r2) orient right
$ns duplex-link-op $node_(r1) $node_(r2) queuePos 0
$ns duplex-link-op $node_(r2) $node_(r1) queuePos 0
$ns duplex-link-op $node_(s3) $node_(r2) orient left-down
$ns duplex-link-op $node_(s4) $node_(r2) orient left-up
set tcp1 [$ns create-connection TCP/Reno $node_(s1) TCPSink $node_(s3) 0]
$tcp1 set window_ 15
set tcp2 [$ns create-connection TCP/Reno $node_(s2) TCPSink $node_(s3) 1]
$tcp2 set window_ 15
set ftp1 [$tcp1 attach-source FTP]
set ftp2 [$tcp2 attach-source FTP]
# Tracing a queue
set redq [[$ns link $node_(r1) $node_(r2)] queue]
set tchan_ [open all.q w]
$redq trace curq_
$redq trace ave_
$redq attach $tchan_
$ns at 0.0 "$ftp1 start"
$ns at 3.0 "$ftp2 start"
$ns at 10 "finish"
# Define 'finish' procedure (include post-simulation processes)
proc finish {} {
global tchan_
set awkCode {
{
if ($1 == "Q" && NF>2) {
print $2, $3 >> "temp.q";
set end $2
}
else if ($1 == "a" && NF>2)
print $2, $3 >> "temp.a";
}
}
set f [open temp.queue w]
puts $f "TitleText: red"
puts $f "Device: Postscript"
if { [info exists tchan_] } {
close $tchan_
}
exec rm -f temp.q temp.a
exec touch temp.a temp.q
exec awk $awkCode all.q
puts $f \"queue
exec cat temp.q >@ $f
puts $f \n\"ave_queue
exec cat temp.a >@ $f
close $f
exec xgraph -bb -tk -x time -y queue temp.queue &
exit 0
}
$ns run