本文将展示一个 RED 队列监视示例。下面这段 OTcl 脚本建立了如图所示网络拓扑然后运行模拟场景,可以看到,在 r1 和 r2 间的 link 使用的 RED 队列可以保存至多 25 个 packet,我们通过测量当前队列和平均队列大小的动态变化,以观察 RED 队列是如何工作的。点击下载代码 red.tcl 。
red.tcl view raw 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 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 72 73 74 75 76 77 78 79 80 81 set ns [new Simulator]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) 10 Mb 2 ms DropTail $ns duplex-link $node_(s2) $node_(r1) 10 Mb 3 ms DropTail $ns duplex-link $node_(r1) $node_(r2) 1.5 Mb 20 ms RED $ns queue-limit $node_(r1) $node_(r2) 25 $ns queue-limit $node_(r2) $node_(r1) 25 $ns duplex-link $node_(s3) $node_(r2) 10 Mb 4 ms DropTail $ns duplex-link $node_(s4) $node_(r2) 10 Mb 5 ms 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]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" 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
有几件事需要注意。首先,更高级的 Simulator 对象成员函数 create-connection
被用于创建 TCP 连接。其次,仔细观察代码中的队列跟踪部分,可以发现我们创建了一个指向 RED 队列对象的变量,调用它的成员函数 trace
监视当前队列大小(cur_q)和平均队列大小(avg_),然后把结果写入文件 all.q
中。
运行结果:
参考:WPI