date : 10-10-2006
location : in ngi lab
처음으로 작성 시뮬레이션 코드들이다..
# Create by alan shin
# 2006/9/22
# in kyungnam university
# mail to: dif001@hanmail.net
# simulation for reno and tahoe
# set packet_size $pk_size
#set ns 0
#set tf 0
set ns [new Simulator]
set rate [lindex $argv 0]
# puts $rate
#Create a simulator object
#Open the ns trace file
# set tf [open out_tahoe_1460.tr w]
set packet_size [lindex $argv 1]
set tcp_protocol [lindex $argv 2]
#set tcp_protocol Tahoe
set a_b_delay [lindex $argv 3]
puts "$tcp_protocol $rate $packet_size"
set filename "$tcp_protocol\_$rate\_$packet_size\_$a_b_delay"
puts $filename
global tcp snk udp null cbr
set tf [open $filename.tr w]
#set nf [open $filename.nam w]
#set f_cwnd [open $filename-cwnd.tr w]
#$f_cwnd trace cwnd_
#$ns namtrace-all $nf
$ns trace-all $tf
#set t_cwndTf [open $filename-cwnd.tr w]
proc monitor {interval} {
global FlowNumber tcp ns
set nowtime [$ns now]
# for {set i 0} {$i < $FlowNumber} {incr i 1} {
set win [open result a]
# puts $win "$nowtime [$tcp($i) set cwnd_] [$tcp($i) set ack_]"
puts $win "$nowtime [$tcp set cwnd_]"
# [$tcp set ack_]"
close $win
# }
$ns after $interval "monitor $interval"
}
proc finish { } {
puts "inner end1"
global ns tf nf filename
# f_cwnd
$ns flush-trace
close $tf
# close $nf
# close $f_cwnd
puts "inner end"
exec nam $filename.nam &
exit 0;
}
#Create six nodes
#agent
set host1 [$ns node]
#puts "$ns $n0"
set host2 [$ns node]
#puts "$ns $n1"
set host3 [$ns node]
#puts "$ns $n2"
set host4 [$ns node]
#puts "$ns $n3"
set rout1 [$ns node]
#puts "$ns $n4"
set rout2 [$ns node]
set sender1 [$ns node]
set sender2 [$ns node]
set sender3 [$ns node]
set sender4 [$ns node]
set recv1 [$ns node]
set recv2 [$ns node]
set recv3 [$ns node]
set recv4 [$ns node]
#puts "$ns $n5"
#Create a duplex link between the nodes
#Links between source and bottleneck
$ns duplex-link $host1 $rout1 10Mb 1ms DropTail
$ns duplex-link $host2 $rout1 10Mb 1ms DropTail
$ns duplex-link $sender1 $rout1 10Mb 1ms DropTail
$ns duplex-link $sender2 $rout1 10Mb 1ms DropTail
$ns duplex-link $sender3 $rout1 10Mb 1ms DropTail
$ns duplex-link $sender4 $rout1 10Mb 1ms DropTail
#Links between dest and bottleneck
$ns duplex-link $rout2 $host3 10Mb 1ms DropTail
$ns duplex-link $rout2 $host4 10Mb 1ms DropTail
$ns duplex-link $rout2 $recv1 10Mb 1ms DropTail
$ns duplex-link $rout2 $recv2 10Mb 1ms DropTail
$ns duplex-link $rout2 $recv3 10Mb 1ms DropTail
$ns duplex-link $rout2 $recv4 10Mb 1ms DropTail
$ns duplex-link $rout1 $rout2 5Mb $a_b_delay RED
#Set Queue Size of link
$ns queue-limit $host1 $rout1 100
$ns queue-limit $host2 $rout1 100
$ns queue-limit $sender1 $rout1 100
$ns queue-limit $sender2 $rout1 100
$ns queue-limit $sender3 $rout1 100
$ns queue-limit $sender4 $rout1 100
$ns queue-limit $rout2 $host3 100
$ns queue-limit $rout2 $host4 100
$ns queue-limit $rout2 $recv1 100
$ns queue-limit $rout2 $recv2 100
$ns queue-limit $rout2 $recv3 100
$ns queue-limit $rout2 $recv4 100
$ns queue-limit $rout1 $rout2 100
set redq [[$ns link $rout1 $rout2] queue]
# set traceq [open reno-queue.tr w]
# $redq attach $traceq
#Create a TCP agent and attach it to node n0
set tcp [new Agent/TCP/$tcp_protocol]
# set tcp [new Agent/TCP]
$tcp set fid_ 1
$tcp set window_ 100
$tcp set packetSize_ $packet_size
# set tcp2 [new Agent/TCP/$tcp_protocol]
set tcp2 [new Agent/TCP]
$tcp2 set fid_ 3
$tcp2 set window_ 100
set tcp3 [new Agent/TCP]
$tcp3 set fid_ 4
$tcp3 set window_ 100
set tcp4 [new Agent/TCP]
$tcp4 set fid_ 5
$tcp4 set window_ 100
#set tcp [new Agent/TCP]
#$tcp set pace_packet_ 2
# $tcp set rtt 0.01s
#$tcp set rate_ 1mb
#set tcp [$ns create-connection TCP $n0 TCPSink $n2 0]
set snk [new Agent/TCPSink]
set snk2 [new Agent/TCPSink]
set snk3 [new Agent/TCPSink]
set snk4 [new Agent/TCPSink]
$tcp set syn_ true
$tcp2 set syn_ true
$tcp3 set syn_ true
$tcp4 set syn_ true
$ns attach-agent $host1 $tcp
$ns attach-agent $sender1 $tcp2
$ns attach-agent $sender2 $tcp3
$ns attach-agent $sender3 $tcp4
# $ns attach-agent $sender4 $tcp2
$ns attach-agent $host3 $snk
$ns attach-agent $recv1 $snk2
$ns attach-agent $recv2 $snk3
$ns attach-agent $recv3 $snk4
# $ns attach-agent $recv4 $snk2
$ns connect $tcp $snk
$ns connect $tcp2 $snk2
$ns connect $tcp3 $snk3
$ns connect $tcp4 $snk4
#Create a UDP agent and attach it to node n0
set udp [new Agent/UDP]
$ns attach-agent $host2 $udp
set null [new Agent/UDP]
$ns attach-agent $host4 $null
$ns connect $udp $null
$udp set fid_ 2
#Setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packet_size_ 512
#5MBps
# $cbr set rate_ 5000000
$cbr set rate_ $rate
# [lindex $argv 0]
#proc logTCPwnd {logfile tcpSource timeInterval} {
# global ns
# set now [$ns now]
# set cwnd [$tcpSource set cwnd_]
# set awnd [$tcpSource set awnd_]
# puts $logfile "$now $cwnd $awnd"
# $ns at [expr $now+$timeInterval] "logTCPwnd $logfile $tcpSource $timeInterval" }
#Schedule events for the CBR agents
#$ns at 0.0 "logTCPwnd $t_cwndTf $tcp 0.05"
#Setup a FTP over TCP Connection
set ftp [$tcp attach-source FTP]
$ftp set type_ FTP
# set ftp2 [new Application/FTP]
# $ftp attach-agent $tcp2
set ftp2 [$tcp2 attach-source FTP]
set ftp3 [$tcp3 attach-source FTP]
set ftp4 [$tcp4 attach-source FTP]
$ftp2 set type_ FTP
# $ftp2 set packet_size_ 512
$ftp3 set type_ FTP
# $ftp3 set packet_size_ 512
$ftp4 set type_ FTP
# $ftp4 set packet_size_ 512
# $ns at 0 "monitor 0.5"
$ns at 0.0 "$ftp start"
# $ns at 0.0 "$ftp2 start"
# $ns at 0.0 "$ftp3 start"
# $ns at 0.0 "$ftp4 start"
$ns at 10.0 "$cbr start"
$ns at 110.0 "finish"
# $ns at 10.0 "$ftp start"
# $ns at 1.0 "$cbr start"
# $ns at 10.0 "finish"
#Run the simulation
$ns run
return 1;
}
}
아래는 thoughput 스크립트
BEGIN {
init=0;
i=0;
recv = 0
}
{
# Trace line format: normal
# if ($2 != "-t") {
event = $1
time = $2
if (event == "+" || event == "-") node_id = $3
if (event == "r" || event == "d") node_id = $4
flow_id = $8
pkt_id = $12
pkt_size = $6
# }
# Trace line format: new
# if ($2 == "-t") {
# event = $1
# time = $3
# node_id = $5
# flow_id = $39
# pkt_id = $41
# pkt_size = $37
# }
# Calculate total received packets' size
if(event=="+" && node_id==0 && flow_id==1) {
pkt_byte_sum[i+1]=pkt_byte_sum[i]+pkt_size;
if(init==0){
# printf("%.2f\n",time);
start_time = time;
init = 1;
}
end_time[i] = time;
i=i+1;
}
}
END {
# simtime = 24;
# printf("%10g %10s %10g\n",flow,flow_t,(recv/simtime)*(8/1000))
#printf("%.2f\t%.2f\n",end_time[0],0);
for(j=1;j<i;j++){
th = pkt_byte_sum[j];
# printf("%2f\t%2f\n",end_time[j],th);
# printf("%d\t%2f\n",end_time[j],th);
}
# printf("%d\t%.2f\n",end_time[i-1],0);
# printf("%.2f\t %.2f",end_time[i-1],end_time[j]);
# printf("%.2f\t %.2f\n",end_time[i-1],start_time);
#*8/1000);
printf("\t%.2f \n",(th/(end_time[i-1]-start_time))*8/1000);
}
packet loss 구하는 스크립트...
BEGIN {
for (i in send) {
send[i] = 0
}
for (i in recv) {
recv[i] = 0
}
tx = 0
drop = 0
pkt_loss = 0
src = 0
dst = 2
flow = 1
}
{
# Trace line format: normal
if ($2 != "-t") {
event = $1
time = $2
if (event == "+" || event == "-") node_id = $3
if (event == "r" || event == "d") node_id = $4
flow_id = $8
pkt_id = $12
}
# Trace line format: new
if ($2 == "-t") {
event = $1
time = $3
node_id = $5
flow_id = $39
pkt_id = $41
}
# Store packets send time
if (flow_id == flow && node_id == src && send[pkt_id] == 0 &&
# if (flow_id == flow && send[pkt_id] == 0 &&
(event == "+" || event == "s")) {
send[pkt_id] = 1
#printf("send[%g] = 1\n",pkt_id)
}
# Store packets arrival time
if (flow_id == flow && node_id == dst && event == "r") {
# if (flow_id == flow && event == "r") {
recv[pkt_id] = 1
#printf("\t\trecv[%g] = 1\n",pkt_id)
}
}
END {
# printf("%10g ",flow)
for (i in send) {
if (send[i] == 1) {
tx ++
if (recv[i] == 0) {
drop ++
#printf("pkt %g not recvd\n",i)
}
}
}
if (tx != 0) {
pkt_loss = drop / tx
} else {
pkt_loss = 0
}
# printf("\t%%\t drop\t pks(%%)\n")
printf("%10g \t%10g\t %10g\n",tx,drop,pkt_loss*100)
printf("\t%10g\n",pkt_loss*100)
}