#Create a simulator object. set ns [new Simulator] $ns color 0 Blue $ns color 1 Red set duration 10; # duration of simulation set windowSize 30 set queueSize 10 set link01Capacity [lindex $argv 0] set link12Capacity [lindex $argv 1] set link01Delay [lindex $argv 2] set link12Delay [lindex $argv 3] #Open the NAM trace file set file1 [open out.nam w] $ns namtrace-all $file1 #Open log files set monfile0 [open Fairness2Mon0.tr w] set monfile1 [open Fairness2Mon1.tr w] # select random seed for NS ns-random 0 #Open the trace file set file2 [open outWindow.tr w] set winfile0 [open Fairness2Window0.tr w] set winfile1 [open Fairness2Window1.tr w] $ns trace-all $file2 #Define a 'finish' procedure proc finish {} { global ns file1 file2 monfile0 monfile1 $ns flush-trace close $file1 close $file2 close $monfile0 close $monfile1 exec nam out.nam & exit 0 } #Create 3 nodes set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] #Connect them in a line 0 -- 1 -- 2 $ns duplex-link $n0 $n1 $link01Capacity $link01Delay DropTail $ns duplex-link $n1 $n2 $link12Capacity $link12Delay DropTail $ns queue-limit $n0 $n1 $queueSize set flink0 [$ns link $n0 $n1] $ns queue-limit $n1 $n2 $queueSize set flink1 [$ns link $n1 $n2] #Set flow monitors set fmon0 [$ns makeflowmon Fid] $fmon0 reset set fmon1 [$ns makeflowmon Fid] $fmon1 reset $ns attach-fmon $flink0 $fmon0 $ns attach-fmon $flink1 $fmon1 #Create a TCP agent and attach it to node n0 set tcp0 [new Agent/TCP/Sack1] $tcp0 set window_ $windowSize $tcp0 set fid_ 0 $ns attach-agent $n0 $tcp0 #Create a TCP agent and attach it to node n0 set tcp1 [new Agent/TCP/Sack1] $tcp1 set window_ $windowSize $tcp1 set fid_ 1 $ns attach-agent $n0 $tcp1 # Create a FTP traffic source and attach it to tcp0 set ftp0 [new Application/FTP] $ftp0 set packetSize_ 500 $ftp0 set interval_ 0.001 $ftp0 attach-agent $tcp0 # Create a FTP traffic source and attach it to tcp1 set ftp1 [new Application/FTP] $ftp1 set packetSize_ 500 $ftp1 set interval_ 0.001 $ftp1 attach-agent $tcp1 #Create a TCP Sink agent (a traffic sink) and attach it to node n1 set tcpSink0 [new Agent/TCPSink/Sack1] $ns attach-agent $n1 $tcpSink0 #Create a TCP Sink agent (a traffic sink) and attach it to node n2 set tcpSink1 [new Agent/TCPSink/Sack1] $ns attach-agent $n2 $tcpSink1 #Connect the traffic sources with the traffic sinks $ns connect $tcp0 $tcpSink0 $ns connect $tcp1 $tcpSink1 #Schedule events for the FTP agent $ns at 1.5 "$ftp0 start" $ns at 0.5 "$ftp1 start" proc plotWindow {tcpSource file} { global ns windowSize set time 0.02 set now [$ns now] set cwnd [$tcpSource set cwnd_] if { $cwnd < $windowSize } then { puts $file "$now $cwnd" } else { puts $file "$now $windowSize" } $ns at [expr $now+$time] "plotWindow $tcpSource $file" } proc plotTPut {flowmon lastpktsd n file} { global ns laspktsd set time 0.2 set now [$ns now] # get flow classifier set fcl [$flowmon classifier] # get flow number 1 set flow [$fcl lookup auto 0 0 $n] if { $flow !="" } then { set drops [$flow set pdrops_] set pktsa [$flow set parrivals_] set pktsd [$flow set pdepartures_] set queue [$flow set pkts_] set tput [expr 1.0*($pktsd -$lastpktsd)/$time] set lastpktsd $pktsd if { $pktsa > 0 } then { set droprate [expr 1.0*$drops / $pktsa] } else { set droprate 0 } puts $file "$now $drops $pktsa $pktsd $droprate $tput $queue" } $ns at [expr $now+$time] "plotTPut $flowmon $lastpktsd $n $file" } set lpktsd(0) 0 set lpktsd(1) 0 $ns at 0.1 "plotWindow $tcp0 $winfile0" $ns at 0.1 "plotWindow $tcp1 $winfile1" $ns at 0.1 "plotTPut $fmon0 $lpktsd(0) 0 $monfile0" $ns at 0.1 "plotTPut $fmon1 $lpktsd(1) 1 $monfile1" #Call the finish procedure after $duration seconds of simulation time $ns at $duration "finish" #Run the simulation $ns run