fq-cbr.tcl
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:7k
源码类别:

通讯编程

开发平台:

Visual C++

  1. #
  2. # This file contains a preliminary cut at fair-queueing for ns
  3. # as well as a number of stubs for Homework 3 in CS268.
  4. #
  5. # $Header: /cvsroot/nsnam/ns-2/tcl/ex/fq-cbr.tcl,v 1.8 1998/09/01 17:02:48 tomh Exp $
  6. #
  7. # updated 8/5/98 by kfall to use hash classifier
  8. #
  9. set ns [new Simulator]
  10. # override built-in link allocator
  11. $ns proc simplex-link { n1 n2 bw delay type } {
  12. $self instvar link_ queueMap_ nullAgent_
  13. $self instvar traceAllFile_
  14. set sid [$n1 id]
  15. set did [$n2 id]
  16. if [info exists queueMap_($type)] {
  17. set type $queueMap_($type)
  18. }
  19. if { $type == "FQ" } {
  20. set link_($sid:$did) [new FQLink $n1 $n2 $bw $delay $nullAgent_]
  21. } else {
  22. set q [new Queue/$type]
  23. $q drop-target $nullAgent_
  24. set link_($sid:$did) [new SimpleLink $n1 $n2 $bw $delay $q]
  25. }
  26. $n1 add-neighbor $n2
  27. #XXX yuck
  28. if { $type == "RED" } {
  29.   set bw [[$link_($sid:$did) set link_] set bandwidth_]
  30. $q set ptc_ [expr $bw / (8. * [$q set meanPacketSize_])]
  31. }
  32. if [info exists traceAllFile_] {
  33. $self trace-queue $n1 $n2 $traceAllFile_
  34. }
  35. }
  36. Class Classifier/Hash/Fid/FQ -superclass Classifier/Hash/Fid
  37.      
  38. Classifier/Hash/Fid/FQ instproc unknown-flow { src dst fid buck } {
  39.         $self instvar fq_
  40.         $fq_ new-flow $src $dst $fid
  41. }    
  42. Class FQLink -superclass Link
  43. FQLink instproc init { src dst bw delay nullAgent } {
  44. $self next $src $dst
  45. $self instvar link_ queue_ head_ toNode_ ttl_ classifier_ 
  46. nactive_ drpT_ drophead_
  47. set drpT_ $nullAgent
  48. set nactive_ 0
  49. set queue_ [new Queue/FQ]
  50. set link_ [new DelayLink]
  51. $link_ set bandwidth_ $bw
  52. $link_ set delay_ $delay
  53. set classifier_ [new Classifier/Hash/Fid/FQ 33]
  54. $classifier_ set fq_ $self
  55. $queue_ target $link_
  56. $queue_ drop-target $nullAgent
  57. $link_ target [$toNode_ entry]
  58. set head_ $classifier_
  59. set drophead_ [new Connector]
  60. $drophead_ target [[Simulator instance] set nullAgent_]
  61. # XXX
  62. # put the ttl checker after the delay
  63. # so we don't have to worry about accounting
  64. # for ttl-drops within the trace and/or monitor
  65. # fabric
  66. #
  67. set ttl_ [new TTLChecker]
  68. $ttl_ target [$link_ target]
  69. $link_ target $ttl_
  70. $queue_ set secsPerByte_ [expr 8.0 / [$link_ set bandwidth_]]
  71. }
  72. Queue set limit_ 10
  73. FQLink set queueManagement_ RED
  74. FQLink set queueManagement_ DropTail
  75. FQLink instproc new-flow { src dst fid } {
  76. $self instvar classifier_ nactive_ queue_ link_ drpT_
  77. incr nactive_
  78. set type [$class set queueManagement_]
  79. set q [new Queue/$type]
  80. #XXX yuck
  81. if { $type == "RED" } {
  82.   set bw [$link_ set bandwidth_]
  83. $q set ptc_ [expr $bw / (8. * [$q set meanPacketSize_])]
  84. }
  85. $q drop-target $drpT_
  86.         set slot [$classifier_ installNext $q]
  87.         $classifier_ set-hash auto $src $dst $fid $slot
  88. $q target $queue_
  89. $queue_ install $fid $q
  90. }
  91. #XXX ask Kannan why this isn't in otcl base class.
  92. FQLink instproc up? { } {
  93. return up
  94. }
  95. #
  96. # should be called after SimpleLink::trace
  97. #
  98. FQLink instproc nam-trace { ns f } {
  99. $self instvar enqT_ deqT_ drpT_ rcvT_ dynT_
  100. if [info exists enqT_] {
  101. $enqT_ namattach $f
  102. if [info exists deqT_] {
  103. $deqT_ namattach $f
  104. }
  105. if [info exists drpT_] {
  106. $drpT_ namattach $f
  107. }
  108. if [info exists rcvT_] {
  109. $rcvT_ namattach $f
  110. }
  111. if [info exists dynT_] {
  112. foreach tr $dynT_ {
  113. $tr namattach $f
  114. }
  115. }
  116. } else {
  117. #XXX 
  118. # we use enqT_ as a flag of whether tracing has been
  119. # initialized
  120. $self trace $ns $f "nam"
  121. }
  122. }
  123. #
  124. # Support for link tracing
  125. # XXX only SimpleLink (and its children) can dump nam config, because Link
  126. # doesn't have bandwidth and delay.
  127. #
  128. FQLink instproc dump-namconfig {} {
  129. # make a duplex link in nam
  130. $self instvar link_ attr_ fromNode_ toNode_
  131. if ![info exists attr_(COLOR)] {
  132. set attr_(COLOR) "black"
  133. }
  134. if ![info exists attr_(ORIENTATION)] {
  135. set attr_(ORIENTATION) ""
  136. }
  137. set ns [Simulator instance]
  138. set bw [$link_ set bandwidth_]
  139. set delay [$link_ set delay_]
  140. $ns puts-nam-config 
  141. "l -t * -s [$fromNode_ id] -d [$toNode_ id] -S UP -r $bw -D $delay -o $attr_(ORIENTATION)"
  142. }
  143. FQLink instproc dump-nam-queueconfig {} {
  144. $self instvar attr_ fromNode_ toNode_
  145. set ns [Simulator instance]
  146. if [info exists attr_(QUEUE_POS)] {
  147. $ns puts-nam-config "q -t * -s [$fromNode_ id] -d [$toNode_ id] -a $attr_(QUEUE_POS)"
  148. } else {
  149. set attr_(QUEUE_POS) ""
  150. }
  151. }
  152. #
  153. # Build trace objects for this link and
  154. # update the object linkage
  155. #
  156. # create nam trace files if op == "nam"
  157. #
  158. FQLink instproc trace { ns f {op ""} } {
  159. $self instvar enqT_ deqT_ drpT_ queue_ link_ head_ fromNode_ toNode_
  160. $self instvar rcvT_ ttl_
  161. $self instvar drophead_ ;# idea stolen from CBQ and Kevin
  162. set enqT_ [$ns create-trace Enque $f $fromNode_ $toNode_ $op]
  163. set deqT_ [$ns create-trace Deque $f $fromNode_ $toNode_ $op]
  164. set drpT_ [$ns create-trace Drop $f $fromNode_ $toNode_ $op]
  165. set rcvT_ [$ns create-trace Recv $f $fromNode_ $toNode_ $op]
  166. $self instvar drpT_ drophead_
  167. set nxt [$drophead_ target]
  168. $drophead_ target $drpT_
  169. $drpT_ target $nxt
  170. $queue_ drop-target $drophead_
  171. # $drpT_ target [$queue_ drop-target]
  172. # $queue_ drop-target $drpT_
  173. $deqT_ target [$queue_ target]
  174. $queue_ target $deqT_
  175. #$enqT_ target $head_
  176. #set head_ $enqT_       -> replaced by the following
  177.         if { [$head_ info class] == "networkinterface" } {
  178.     $enqT_ target [$head_ target]
  179.     $head_ target $enqT_
  180.     # puts "head is i/f"
  181.         } else {
  182.     $enqT_ target $head_
  183.     set head_ $enqT_
  184.     # puts "head is not i/f"
  185. }
  186. # put recv trace after ttl checking, so that only actually 
  187. # received packets are recorded
  188. $rcvT_ target [$ttl_ target]
  189. $ttl_ target $rcvT_
  190. $self instvar dynamics_
  191. if [info exists dynamics_] {
  192. $self trace-dynamics $ns $f $op
  193. }
  194. }
  195. #
  196. # Insert objects that allow us to monitor the queue size
  197. # of this link.  Return the name of the object that
  198. # can be queried to determine the average queue size.
  199. #
  200. FQLink instproc init-monitor ns {
  201. puts stderr "FQLink::init-monitor not implemented"
  202. }
  203. #Queue/RED set thresh_ 3
  204. #Queue/RED set maxthresh_ 8
  205. proc build_topology { ns which } {
  206.         $ns color 1 red
  207.         $ns color 2 white
  208. foreach i "0 1 2 3" {
  209. global n$i
  210. set tmp [$ns node]
  211. set n$i $tmp
  212. }
  213. $ns duplex-link $n0 $n2 5Mb 2ms DropTail
  214. $ns duplex-link $n1 $n2 5Mb 2ms DropTail
  215. $ns duplex-link-op $n0 $n2 orient right-down
  216. $ns duplex-link-op $n1 $n2 orient right-up
  217. if { $which == "FIFO" } {
  218. $ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
  219. } elseif { $which == "RED" } {
  220. $ns duplex-link $n2 $n3 1.5Mb 10ms RED
  221. } else {
  222. $ns duplex-link $n2 $n3 1.5Mb 10ms FQ
  223. }
  224. $ns duplex-link-op $n2 $n3 orient right
  225. $ns duplex-link-op $n2 $n3 queuePos 0.5
  226. }
  227. proc build_cbr { from to startTime interval } {
  228. global ns
  229. set udp0 [new Agent/UDP]
  230. set src [new Application/Traffic/CBR]
  231. $src set packet-size 800
  232. $src set rate [expr 800 * 8/ [$ns delay_parse $interval]] 
  233. set sink [new Agent/Null]
  234. $ns attach-agent $from $udp0
  235. $src attach-agent $udp0
  236. $ns attach-agent $to $sink
  237. $ns connect $udp0 $sink
  238. $ns at $startTime "$src start"
  239. return $udp0
  240. }
  241. set f [open out.tr w]
  242. $ns trace-all $f
  243. build_topology $ns FQ
  244. set cbr1 [build_cbr $n0 $n3 0.1 4ms]
  245. $cbr1 set class_ 1
  246. set cbr2 [build_cbr $n1 $n3 0.11 8ms]
  247. $cbr2 set class_ 2
  248. #$ns at 40.0 "finish Output"
  249. $ns at 4.0 "xfinish"
  250. proc xfinish {} {
  251. global ns f
  252. $ns flush-trace
  253. close $f
  254. #XXX
  255. puts "running nam..."
  256. exec nam out.nam &
  257. exit 0
  258. }
  259. $ns run