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

通讯编程

开发平台:

Visual C++

  1. #--------- Creating the TOPOLOGY --------------------#
  2. #       n0                             
  3. #       :    Bottleneck            
  4. #       :  R0-----------------------R1
  5. #         /                            
  6. #       ni                         
  7. #
  8. Agent/TCP set minrto_ 1
  9. proc set-red-params { qsize } {
  10. Queue/RED set thresh_ [expr 0.6 * $qsize]
  11. Queue/RED set maxthresh_ [expr 0.8 * $qsize]
  12. Queue/RED set q_weight_ 0.001
  13. Queue/RED set linterm_ 10
  14. Queue/RED set bytes_ false ;
  15. Queue/RED set queue_in_bytes_ false ;
  16. Agent/TCP set old_ecn_ true
  17. Queue/RED set setbit_     true
  18. }
  19. proc create-topology2 { BW delay qtype qsize numSideLinks deltaDelay } {
  20.     global ns 
  21.     
  22.     #Set the queue size to the pipe's size assuming the packet size is 1000 KByte
  23.     if { $qsize == 0 } { set qsize [expr round([expr ($BW / 8) * 2 * $delay])] }
  24.     set i 0
  25.     while { $i < 2 } {
  26. global R$i
  27. set R$i [$ns node]
  28. incr i
  29.     }
  30.     
  31.     $ns duplex-link $R0 $R1 [set BW]Mb [set delay]ms $qtype
  32.     $ns queue-limit $R0 $R1 $qsize
  33.     $ns queue-limit $R1 $R0 $qsize
  34.     # Give a global handle to the Bottleneck Queue to allow 
  35.     # setting the RED paramters
  36.     global Bottleneck rBottleneck
  37.     set  Bottleneck  [[$ns link $R0 $R1] queue] 
  38.     set  rBottleneck [[$ns link $R1 $R0] queue]
  39.     global l rl
  40.     set  l  [$ns link $R0 $R1]  
  41.     set  rl [$ns link $R1 $R0]
  42.     global all_links 
  43.     set all_links "$l $rl "
  44.     # The side links  have the same BW as the Bottleneck
  45.     set i 0
  46.     while { $i < $numSideLinks } {
  47. global n$i q$i rq$i l$i rl$i
  48. set n$i  [$ns node]
  49. $ns duplex-link [set n$i]  $R0  [set BW]Mb [expr $delay + $i * $deltaDelay]ms $qtype
  50. $ns queue-limit [set n$i]  $R0  $qsize
  51. $ns queue-limit $R0 [set n$i]   $qsize
  52. set  q$i   [[$ns link [set n$i]  $R0] queue] 
  53. set  rq$i  [[$ns link $R0 [set n$i]] queue]
  54. set  l$i    [$ns link [set n$i]  $R0] 
  55. set  rl$i   [$ns link $R0 [set n$i]]
  56. set all_links "$all_links [set l$i] [set rl$i] "
  57. incr i
  58.     }
  59. }
  60. #------- Sender Class :
  61. # This is essentially an ftp sender
  62. Class GeneralSender  -superclass Agent 
  63. # otherparams are "startTime TCPclass .."
  64. GeneralSender instproc init { id node rcvrTCP otherparams } {
  65.     global  ns
  66.     $self next
  67.     $self instvar tcp_ id_ ftp_ node_ tcp_rcvr_ tcp_type_
  68.     set id_ $id
  69.     set node_ $node
  70.     if { [llength $otherparams] > 1 } {
  71. set TCP [lindex $otherparams 1]
  72.     } else { 
  73. set TCP "TCP/Reno"
  74.     }
  75.     set   tcp_type_ $TCP
  76.     set   tcp_ [new Agent/$TCP]
  77.     $tcp_ set  packetSize_ 1000   
  78.     $tcp_ set  class_  $id
  79.     set   ftp_ [new Source/FTP]
  80.     $ftp_ set agent_ $tcp_
  81.     $ns   attach-agent $node $tcp_
  82.     $ns   connect $tcp_  $rcvrTCP
  83.     set   tcp_rcvr_ $rcvrTCP
  84.     set   startTime [lindex $otherparams 0]
  85.     $ns   at $startTime "$ftp_ start"
  86.     puts  "initialized Sender $id_ at $startTime"
  87. }
  88. GeneralSender instproc trace-xcp parameters {
  89.     $self instvar tcp_ id_ tcpTrace_
  90.     global ftracetcp$id_ 
  91.     set ftracetcp$id_ [open  xcp$id_.tr  w]
  92.     set tcpTrace_ [set ftracetcp$id_]
  93.     $tcp_ attach-trace [set ftracetcp$id_]
  94.     if { -1 < [lsearch $parameters cwnd]  } { $tcp_ tracevar cwnd_ }
  95.     if { -1 < [lsearch $parameters seqno] } { $tcp_ tracevar t_seqno_ }
  96. }
  97. #--- Command line arguments
  98. proc  set-cmd-line-args { list_args } {
  99.     global argv
  100.     set i 0
  101.     foreach a $list_args {
  102. global $a
  103. set $a  [lindex $argv $i]
  104. puts "$a = [set $a]"
  105. incr i
  106.     }
  107. }
  108. #-------------- Plotting functions -----------#
  109. # plot a xcp traced var
  110. proc plot-xcp { TraceName nXCPs  PlotTime what } {
  111.     if {[string compare $what "cwnd_"] == 0} {
  112. exec rm -f xgraph_cwnd.tcp
  113. set f [open xgraph_cwnd.tcp w]
  114. set a cwnd
  115.     } else {
  116. exec rm -f xgraph_seqno.tcp
  117. set f [open xgraph_seqno.tcp w]
  118. set a seqno
  119.     }
  120.     puts $f "TitleText: $TraceName"
  121.     puts $f "Device: Postscript"
  122.     foreach i $nXCPs {
  123. #the TCP traces are flushed when the sources are stopped
  124. exec rm -f temp.tcp 
  125.         exec touch temp.tcp
  126. global ftracetcp$i 
  127. if [info exists ftracetcp$i] { flush [set ftracetcp$i] }
  128. set packetsize [expr 100 * ($i +10)]
  129.         set result [exec awk -v PlotTime=$PlotTime -v what=$what -v s=$packetsize {
  130.     {
  131. if (( $6 == what ) && ($1 > PlotTime)) {
  132. tmp=$7*s
  133. print $1, tmp >> "temp.tcp";
  134. }
  135.     }
  136. } xcp$i.tr]
  137.  
  138. puts "$i : $result"
  139. puts $f "$what$i 
  140. exec cat temp.tcp >@ $f
  141. puts $f "n"
  142. flush $f
  143.     }
  144.     close $f
  145.     exec xgraph  -nl -m  -x time -y $what xgraph_$a.tcp &
  146.     return 
  147. }
  148. # Takes as input the the label on the Y axis, the time it starts plotting, and the trace file tcl var
  149. proc plot-red-queue { TraceName PlotTime traceFile } {
  150.     
  151.     exec rm -f xgraph.red_queue
  152.     exec rm -f temp.q temp.a temp.p temp.avg_enqueued temp.avg_dequeued temp.x temp.y 
  153.     exec touch temp.q temp.a temp.p temp.avg_enqueued temp.avg_dequeued temp.x temp.y 
  154.     
  155.     exec awk -v PT=$PlotTime {
  156. {
  157.     if (($1 == "q" && NF>2) && ($2 > PT)) {
  158. print $2, $3 >> "temp.q"
  159.     }
  160.     else if (($1 == "a" && NF>2) && ($2 > PT)){
  161. print $2, $3 >> "temp.a"
  162.     }
  163.     else if (($1 == "p" && NF>2) && ($2 > PT)){
  164. print $2, $3 >> "temp.p"
  165.     }
  166. }
  167.     }  $traceFile
  168.     set ff [open xgraph.red_queue w]
  169.     puts $ff "TitleText: $TraceName"
  170.     puts $ff "Device: Postscript n"
  171.     puts $ff "queue
  172.     exec cat temp.q >@ $ff  
  173.     puts $ff n"ave_queue
  174.     exec cat temp.a >@ $ff
  175.     puts $ff n"prob_drop
  176.     exec cat temp.p >@ $ff
  177.     close $ff
  178.     exec xgraph  -P -x time -y queue xgraph.red_queue &
  179. }
  180. proc plot-red {varname filename PlotTime} {
  181.     
  182.     exec rm -f temp.$filename
  183.     exec touch temp.$filename
  184.     
  185.     set result [exec awk -v PlotTime=$PlotTime -v what=$varname -v file=temp.$filename {
  186. {
  187.     if (( $1 == what ) && ($2 > PlotTime)) {
  188. print $2, $3 >> file ;
  189.     }  
  190. }
  191.     } ft_red_Bottleneck.tr]
  192.     exec xgraph -P -x time -y $filename temp.$filename
  193. }
  194. proc MarkovErrorModel {lossylink durlist} {
  195. puts [lindex $durlist 0]
  196. puts [lindex $durlist 1]
  197. set errmodel [new ErrorModel/TwoStateMarkov $durlist time]
  198. $errmodel drop-target [new Agent/Null] 
  199. $lossylink errormodule $errmodel
  200. }
  201. proc ComplexMarkovErrorModel {lossylink durlist} {
  202. set errmodel [new ErrorModel/ComplexTwoStateMarkov $durlist time]
  203. $errmodel drop-target [new Agent/Null] 
  204. $lossylink errormodule $errmodel
  205. }
  206. #-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- Initializing Simulator -*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-#
  207. # BW is in Mbs and delay is in ms
  208. #set-cmd-line-args "seed qType BW nXCPs delay "
  209. set seed   472904
  210. set qType  XCP
  211. set BW     20; # in Mb/s
  212. set nXCPs  3; # Number of flows
  213. set delay  100; # in ms
  214. # define avg duration of blocked (state 1) and unblocked (state 0) states
  215. #set ubstate 11.2
  216. #set bstate 4.0
  217. #set ubstate 50.0
  218. #set bstate 1.0
  219. set m1_ubstate 27.0
  220. set m1_bstate 12.0
  221. set m2_ubstate 0.4
  222. set m2_bstate 0.4
  223. set ns          [new Simulator]
  224. $ns             use-scheduler Heap
  225. set rtg         [new RNG]
  226. $rtg seed       $seed
  227. set f_all [open out.tr w]
  228. $ns trace-all $f_all
  229. # for event-tracing
  230. $ns eventtrace-all
  231. set  qSize  [expr round([expr ($BW / 8.0) * 4 * $delay * 1.0])];#set buffer to the pipe size
  232. set tracedXCPs       "0 1 2"
  233. set SimStopTime      5000
  234. set PlotTime         0
  235. #---------- Create the simulation --------------------#
  236. # Create topology
  237. create-topology2 $BW $delay $qType $qSize $nXCPs 0.0
  238. foreach link $all_links {
  239.     set queue [$link queue]
  240.     switch $qType {
  241. "XCP" {
  242. $queue set-link-capacity [[$link set link_] set bandwidth_];
  243. }
  244. "DropTail/XCP" {
  245.     $queue set-link-capacity-Kbytes [expr [[$link set link_] set bandwidth_] / 8000];
  246. }
  247. default {
  248. puts "Incorrect qType $qType"
  249. exit 0
  250. }
  251.     }
  252. }
  253. # Create sources:
  254. #set i 0
  255. #while { $i < $nXCPs  } {
  256. #    set StartTime [expr [$rtg integer 1000] * 0.001 * (0.01 * $delay) + $i  * 0.0] 
  257.     # set rcvr_XCP      [new Agent/TCPSink/XCPSink]
  258. #     $ns attach-agent  $R1 $rcvr_XCP
  259. #     set src$i         [new GeneralSender $i [set n$i] $rcvr_XCP "$StartTime TCP/Reno/XCP"]
  260. #     [[set src$i] set tcp_]  set  packetSize_ [expr 100 * ($i +10)]
  261. #     [[set src$i] set tcp_]  set  window_     [expr $qSize * 10]
  262. #     incr i
  263. # }
  264. # add lossy link that models a 2 state markov model
  265. set lossylink [$ns link $n0 $R0]
  266. set durlist "$m1_ubstate $m1_bstate $m2_ubstate $m2_bstate"
  267. ComplexMarkovErrorModel $lossylink $durlist
  268. #---------- Trace --------------------#
  269. # Trace sources
  270. #foreach i $tracedXCPs {
  271. # [set src$i] trace-xcp "cwnd seqno"
  272. #}
  273. # Trace Queues
  274. #foreach queue_name "Bottleneck rBottleneck" {
  275. #  set queue [set "$queue_name"]
  276. #  if {[$queue info class] == "Queue/XCP"} {
  277. #  global "ft_red_$queue_name"
  278. #  set "ft_red_$queue_name" [open ft_red_[set queue_name].tr w]
  279.     
  280. #  set xcpq $queue ;#[$queue set vq_(0)]
  281. #  $xcpq attach       [set ft_red_$queue_name]
  282. #  }
  283. # }
  284. #---------------- Run the simulation ------------------------#
  285. proc flush-files {} {
  286.     set files "f_all ft_red_Bottleneck ft_red_rBottleneck"
  287.     global tracedXCPs
  288.     foreach file $files {
  289.         global $file
  290.         if {[info exists $file]} {
  291.             flush [set $file]
  292.             close [set $file]
  293.         }   
  294.     }
  295.     foreach i $tracedXCPs {
  296. global src$i
  297. set file [set src$i tcpTrace_]
  298. if {[info exists $file]} {
  299.             flush [set $file]
  300.             close [set $file]
  301.         }   
  302.     }
  303. }
  304. proc finish {} {
  305.     global ns 
  306.     if {[info exists f]} {
  307. $ns flush-trace
  308. close $f
  309.     }   
  310.     $ns halt
  311. }
  312. $ns at $SimStopTime "finish"
  313. $ns run
  314. flush-files
  315. #------------ Post Processing ---------------#
  316. set PostProcess 1
  317. if { $PostProcess } {
  318.     #--- Traced TCPs
  319.     set TraceName "Flows --$qType-QS$qSize"
  320. #    plot-xcp      $TraceName  $tracedXCPs  0.0  "cwnd_"
  321.  #   plot-xcp      $TraceName  $tracedXCPs  0.0  "t_seqno_"
  322. #    plot-red-queue  $TraceName  $PlotTime   ft_red_Bottleneck.tr
  323.  #   plot-red "u" util 0.0
  324.  #   plot-red "q" queue 0.0
  325. }