-
Notifications
You must be signed in to change notification settings - Fork 29
/
tcp-loss-probability.tcl
executable file
·158 lines (139 loc) · 3.86 KB
/
tcp-loss-probability.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#===================================
# Simulation parameters setup
#===================================
proc default_options {} {
global opts opt_wants_arg
set raw_opt_info {
duration 60
output out.tr
outnam out.nam
p 0.0
agent CTCP
nc_r 1
nc_field_size_ 256
size 16777216
queue 20
blksize 3
}
while {$raw_opt_info != ""} {
if {![regexp "^\[^\n\]*\n" $raw_opt_info line]} {
break
}
regsub "^\[^\n\]*\n" $raw_opt_info {} raw_opt_info
set line [string trim $line]
if {[regexp "^\[ \t\]*#" $line]} {
continue
}
if {$line == ""} {
continue
} elseif [regexp {^([^ ]+)[ ]+([^ ]+)$} $line dummy key value] {
set opts($key) $value
set opt_wants_arg($key) 1
} else {
set opt_wants_arg($key) 0
# die "unknown stuff in raw_opt_info\n"
}
}
}
proc process_args {} {
global argc argv opts opt_wants_arg
default_options
for {set i 0} {$i < $argc} {incr i} {
set key [lindex $argv $i]
regsub {^-} $key {} key
if {![info exists opt_wants_arg($key)]} {
puts stderr "unknown option $key";
}
if {$opt_wants_arg($key)} {
incr i
set opts($key) [lindex $argv $i]
} else {
set opts($key) [expr !opts($key)]
}
}
}
process_args
#===================================
# Initialization
#===================================
#Create a ns simulator
set ns [new Simulator]
#Open the NS trace file
set tracefile [open $opts(output) w]
$ns trace-all $tracefile
#Open the NAM trace file
set namfile [open $opts(outnam) w]
$ns namtrace-all $namfile
#===================================
# Nodes Definition
#===================================
#Create 2 nodes
set source [$ns node]
set router [$ns node]
set sink [$ns node]
#===================================
# Links Definition
#===================================
#Createlinks between nodes
$ns duplex-link $source $router 1Mb 100ms DropTail
$ns duplex-link $router $sink 1Mb 100ms DropTail
#Give node position (for NAM)
$ns duplex-link-op $source $router orient right-up
$ns duplex-link-op $router $sink orient right
#Monitor the queue for link (router-sink) (for NAM)
$ns queue-limit $router $sink $opts(queue)
$ns duplex-link-op $router $sink queuePos 0.5
#===================================
# Agents Definition
#===================================
set tcp0 [new Agent/TCP/$opts(agent)]
$tcp0 set class_ 1
$tcp0 set fid_ 1
$ns attach-agent $source $tcp0
if {$opts(agent) == {NC}} {
$tcp0 set nc_r_ $opts(nc_r)
$tcp0 set nc_field_size_ $opts(nc_field_size_)
}
if {$opts(agent) == {CTCP}} {
$tcp0 set blksize_ $opts(blksize)
}
$tcp0 set window_ 1000
# Sink
if {$opts(agent) == {NC}} {
set sink0 [new Agent/TCPSink/NC]
} elseif {$opts(agent) == {CTCP}} {
set sink0 [new Agent/TCPSink/CTCP]
} else {
set sink0 [new Agent/TCPSink]
}
$ns attach-agent $sink $sink0
$ns connect $tcp0 $sink0
# Packet loss
set em [new ErrorModel]
$em unit pkt
$em set rate_ $opts(p)
$em drop-target [new Agent/Null]
$ns lossmodel $em $router $sink
#===================================
# Applications Definition
#===================================
set app0 [new Application/FTP]
$app0 attach-agent $tcp0
$app0 set packet_size_ 1000
$ns at 1.0 "$app0 send $opts(size)"
#===================================
# Termination
#===================================
#Define a 'finish' procedure
proc finish {} {
global ns tracefile namfile
$ns flush-trace
close $tracefile
close $namfile
# exec nam out.nam &
exit 0
}
# $ns at $opts(duration) "$ns nam-end-wireless $opts(duration)"
# $ns at $opts(duration) "finish"
# $ns at $opts(duration) "puts \"done\" ; $ns halt"
$ns run