Friday, 15 May 2015

CUBIC implementation in NS2

The results graph which I am posting are the real time values as obtained to me and the factors that the link speed and the latency in the code I post are intended to induce congestion.Because the behavior of the congestion control algorithm can be observed only when there is congestion in the network.
However the soul purpose of this blog is to provide working NS2 codes for learning enthusiasts for free :)
Cubic     
Actually cubic TCP variant is used for high bandwidth and high latency.Important point to mention here is that the CUBIC flavor does not rely on the receipt of ACKs to increase the window size.Its window size is mainly dependent only on the last congestion event that occured.With standard TCP, flows with very short RTTs will receive ACKs faster and therefore have their congestion windows grow faster than other flows with longer RTTs. CUBIC allows for more fairness between flows since the window growth is independent of RTT.
 The code is as below



  set ns [new Simulator]

  set file1 [open out.nam w]
  $ns namtrace-all $file1
  set tf [open ui.tr w]  
  $ns trace-all $tf

# Open the Window trace file
  set winfile [open cubic.tr w]


  proc finish {} {
     global ns file1 tf
     $ns flush-trace
     close $tf
     close $file1
     exec nam out.nam &
     exit 0
  }

  set n0 [$ns node]
  set n1 [$ns node]
  set n2 [$ns node]
  set n3 [$ns node]
  set n4 [$ns node]
  set n5 [$ns node]

  $ns duplex-link $n0 $n2 2Gb 10ms DropTail
  $ns duplex-link $n1 $n2 2Gb 10ms DropTail
  $ns simplex-link $n2 $n3 1.0Gb 200ms DropTail
  $ns simplex-link $n3 $n2 1.0Gb 200ms DropTail
  $ns duplex-link $n3 $n4 2.0Gb 40ms DropTail
  $ns duplex-link $n3 $n5 2.0Mb 30ms DropTail

$ns duplex-link-op $n2 $n3 queuePos 0.1

# Give node position (for NAM)
  $ns duplex-link-op  $n0 $n2 orient right-down
  $ns duplex-link-op  $n1 $n2 orient right-up
  $ns simplex-link-op $n2 $n3 orient right
  $ns simplex-link-op $n3 $n2 orient left
  $ns duplex-link-op  $n3 $n4 orient right-up
  $ns duplex-link-op  $n3 $n5 orient right-down
  $ns queue-limit $n2 $n3 10


  set tcp [new Agent/TCP/Linux]
  $ns at 0 "$tcp select_ca cubic"
  $ns attach-agent $n0 $tcp

  set sink [new Agent/TCPSink/Sack1]
  $sink set ts_echo_rfc1323_ true

  $ns attach-agent $n4 $sink

  $ns connect $tcp $sink
  $tcp set fid_ 1
  $tcp set window_ 8000
  $tcp set packetSize_ 552


  set ftp [new Application/FTP]
  $ftp attach-agent $tcp
  $ftp set type_ FTP

  $ns at 0.1 "$ftp start"
  $ns at 500.0 "$ftp stop"


  proc plotWindow {tcpSource file} {
     global ns

     set time 0.1
     set now [$ns now]
     set cwnd [$tcpSource set cwnd_]
     set wnd [$tcpSource set window_]
     puts $file "$now $cwnd"
     $ns at [expr $now+$time] "plotWindow $tcpSource $file"
  }

  $ns at 0.1 "plotWindow $tcp $winfile"

  $ns at 140.0 "finish"

  $ns run
The graph of cwnd versus time is as below
For implementation in other topology or for NS2 programs for which you like me to post the code please mail me or comment below


 

2 comments: