incast.tcl

  1 # Basic Incast Simulation
  2 # Check Args
  3 if {$argc != 5} {
  4   puts "Usage: ns incast <srv_num> <adv_win-pkt> <SRU-KB> <link_buf-pkt> <seed>"
  5   exit 1
  6 }
  7 
  8 #################################################################
  9 # Argments
 10 # ServerNum: $argv(0)
 11 set svr_num [lindex $argv 0]
 12 # Advertised Window size (pkt): $argv(1)
 13 set adv_win [lindex $argv 1]
 14 # SRU Size (Byte) ... only Payload: $argv(2)
 15 set SRU [expr [lindex $argv 2] * 1024]
 16 # Link Buffer (pkt): $argv(3)
 17 set link_buf [lindex $argv 3]
 18 # Random Seed: $argv(4)
 19 set seed [lindex $argv 4]
 20 
 21 ################################################################
 22 # Variables
 23 # Create a simulator object
 24 set ns [new Simulator]
 25 
 26 # Bandwidth (Gbps)
 27 set bw_Gbps 1
 28 
 29 # Link Delay (us)
 30 set link_del_us 25
 31 # Maximum Random Link Delay: 0--maxrand (us)
 32 set maxrand_link_del_us 20
 33 
 34 # SYN Interval Delay (us) for each Request
 35 set SYN_del_us 0
 36 ## For Aggressive Optimization for Goodput (may cause incast)
 37 # set SYN_del_us [expr int(${SRU} * 8 / (${bw_Gbps} * pow(10, 9)) * pow(10, 6))]
 38 ## For Conservative Optimization for Goodput (never cause incast)
 39 # set SYN_del_us [expr int(${SRU} * 8 / (${bw_Gbps} * pow(10, 9)) * pow(10, 6)
 40 #   + ${link_del_us} * 4 * (1 + 
 41 #   (log10( ${link_del_us} * 4 * pow(10, -6) * ${bw_Gbps} * pow(10, 9) 
 42 #   / (1500 * 8) ) / log10(2))))]
 43 
 44 # Maximum Random SYN Delay: 0--maxrand (us)
 45 set maxrand_SYN_del_us 0
 46 
 47 # Total Size of All Servers SRU with TCP/IP Header and Handshake
 48 set Block_trans [expr ((int($SRU / 1460) + 1)* 1500 + 40) * $svr_num]
 49 
 50 # Link Error Rate (Unit:pkt) 0.001 = 0.1% (a loss in 1000 pkt)
 51 # set err_rate 0.001
 52 set err_rate 0
 53 
 54 #############################################
 55 # Random Model
 56 set rng [new RNG]
 57 # seed 0 equal to current OS time (UTC)
 58 # so seed should be more than 1 for repeatability
 59 $rng seed [expr ${seed} * ${svr_num} + 1]
 60 
 61 #################################################################
 62 # Tracing Message
 63 puts -nonewline "Server: $svr_num, win: ${adv_win}pkt, "
 64 puts -nonewline "SRU: [lindex $argv 2]KB, link_buf: ${link_buf}pkt, "
 65 puts "Seed: $seed, "
 66 puts -nonewline "Block_trans: ${Block_trans}B, "
 67 puts -nonewline "RTT: [expr $link_del_us * 4]us, "
 68 puts -nonewline "RTT_rand: ${maxrand_link_del_us}us, "
 69 puts "SYN_del: ${SYN_del_us}-[expr $SYN_del_us + $maxrand_SYN_del_us]us"
 70 
 71 Agent/TCP set trace_all_oneline_ true
 72 Agent/TCP set packetSize_ 1460
 73 Agent/TCP set window_ $adv_win
 74 Agent/TCP set singledup_ 0 ;      # 0: Disabled Limited Transmit
 75 Agent/TCP set tcpTick_ 0.0000001 ;  # 100ns (default 0.01: 10ms)
 76 Agent/TCP set minrto_ 0.2
 77 
 78 #Open the ns trace file
 79 set nf [open out.ns w]
 80 $ns trace-all $nf
 81 set ef [open out.et w]
 82 $ns eventtrace-all $ef
 83 set tf [open out.tcp w]
 84 # For Queue Monitoring
 85 # set q_trans [open queue_trans.ns w]
 86 
 87 proc finish {} {
 88     global ns nf ef tf
 89     # For Queue Monitoring
 90     # global q_trans
 91     $ns flush-trace
 92     close $nf
 93     close $tf
 94     close $ef
 95     # close $q_trans
 96     puts "Done."
 97     exit 0
 98 }
 99 
100 #Create two nodes
101 set nx [$ns node]
102 set nc [$ns node]
103 $ns duplex-link $nx $nc ${bw_Gbps}Gb ${link_del_us}us DropTail
104 $ns queue-limit $nx $nc ${link_buf}
105 
106 # Link Error Module between Switch and Client
107 set loss_module [new ErrorModel]
108 $loss_module unit pkt
109 $loss_module set rate_ $err_rate
110 set loss_random_variable [new RandomVariable/Uniform]
111 $loss_random_variable use-rng ${rng}
112 $loss_module ranvar ${loss_random_variable}
113 $loss_module drop-target [new Agent/Null]
114 $ns lossmodel $loss_module $nx $nc
115 
116 for {set i 0} {$i < $svr_num} {incr i} {
117     set n_($i) [$ns node]
118     $ns duplex-link $nx $n_($i) ${bw_Gbps}Gb ${link_del_us}us DropTail
119     $ns queue-limit $n_($i) $nx 1000
120     set tcp_($i) [new Agent/TCP/Newreno]
121     $tcp_($i) set fid_ $i
122     $tcp_($i) attach-trace $tf
123     $tcp_($i) trace maxseq_
124     $tcp_($i) trace ack_
125     set ftp_($i) [new Application/FTP]
126     $ftp_($i) attach-agent $tcp_($i)
127     $ftp_($i) set type_ FTP
128     $ns attach-agent $n_($i) $tcp_($i)
129     set snk_($i) [new Agent/TCPSink]
130     $ns attach-agent $nc $snk_($i)
131     $ns connect $tcp_($i) $snk_($i)
132 
133     # Caluclate Delay (us)
134     set del_us [expr $link_del_us * 2 + $SYN_del_us * $i 
135                     + [$rng uniform 0 ${maxrand_SYN_del_us}]]
136 
137      $ns at [expr 1.0 + $del_us * 0.000001] "$ftp_($i) send $SRU"
138 }
139 $ns at 0.0 "debug"
140 $ns at 0.99 "check_trans"
141 $ns at 21.0 "finish"
142 
143 # For Queue Monitoring
144 # set q_mon [$ns monitor-queue $nx $nc [open queue_mon.ns w] 0.0001]
145 # [$ns link $nx $nc] queue-sample-timeout
146 
147 proc update_link_del {} {
148     global ns nx n_ link_del_us maxrand_link_del_us svr_num rng
149     for {set i 0} {$i < $svr_num} {incr i} {
150         $ns delay $nx $n_($i) [expr $link_del_us 
151                + [$rng uniform 0 ${maxrand_link_del_us}]]us duplex
152     }
153 }
154 
155 proc check_trans {} {
156     global ns Block_trans svr_num snk_
157     # 0.0001 = 100 us = 1 RTT
158     set time 0.0001
159     set now [$ns now]
160 
161     # check traffic to each TCP sink agent
162     # puts "$now: Server: 0, bytes: [$snk_(0) set bytes_]"
163     set total_bytes 0
164     for {set i 0} {$i < $svr_num} {incr i} {
165         set total_bytes [expr $total_bytes + [$snk_($i) set bytes_]]
166     }
167 
168     # Is any data to be transmitted?
169     if {$total_bytes >= $Block_trans} {
170         # All SRU is transmitted.
171         if {$total_bytes == $Block_trans} {
172             # Finish in just.
173         } else {
174             # Unnecessary Retransmit is exist.
175         }
176         flush stdout
177         $ns at [expr $now + 1] "finish"
178     }
179 
180     # For Queue Monitoring
181     # $q_mon instvar parrivals_ pdepartures_ bdrops_ bdepartures_ pdrops_
182     # puts $q_trans "$now $bdepartures_"
183 
184     # For update_link
185     update_link_del
186 
187     $ns at [expr $now+$time] "check_trans"
188 }
189 
190 proc debug {} {
191     global ns
192     set next_time 1.0
193     set now [$ns now]
194     puts -nonewline "$now."
195     flush stdout
196     $ns at [expr $now+$next_time] "debug"
197 }
198 
199 #Run the simulation
200 $ns run

 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

<NS2网络模拟器的原理和应用>-王辉

这本书P42的例子和P47的基本语法还是很有用的。

Agent简单地说是表示节点的协议Protocol。

原文地址:https://www.cnblogs.com/forcheryl/p/3981394.html