traffic.tcl 13.9 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2 3 4 5 6 7
######################################################################
# traffic.tcl
#
#
# This defines the various agents and applications needed to support
# traffic generation.  Specifically it defines Agent/UDP, Agent/Null,
# and Application/Traffic/CBR.
8 9 10 11 12
#
# Added: TCP traffic generation using NSE. Defines
# Agent/TCP/FullTcp, Agent/TCP/FullTcp/Reno, Agent/TCP/FullTcp/Newreno,
# Agent/TCP/FullTcp/Tahoe, Agent/TCP/FullTcp/Sack,
# Application/FTP and Application/Telnet
Leigh B. Stoller's avatar
Leigh B. Stoller committed
13 14 15 16
######################################################################

Class Agent -superclass NSObject
Class Agent/UDP -superclass Agent
17
Class Agent/TCP -superclass Agent
Leigh B. Stoller's avatar
Leigh B. Stoller committed
18
Class Agent/Null -superclass Agent
19
Class Agent/TCPSINK -superclass Agent
20 21 22 23 24
Class Agent/TCP/FullTcp -superclass Agent
Class Agent/TCP/FullTcp/Reno -superclass Agent/TCP/FullTcp
Class Agent/TCP/FullTcp/Newreno -superclass Agent/TCP/FullTcp
Class Agent/TCP/FullTcp/Tahoe -superclass Agent/TCP/FullTcp
Class Agent/TCP/FullTcp/Sack -superclass Agent/TCP/FullTcp
Leigh B. Stoller's avatar
Leigh B. Stoller committed
25 26 27

Class Application -superclass NSObject
Class Application/Traffic/CBR -superclass Application
28 29
Class Application/FTP -superclass Application
Class Application/Telnet -superclass Application
30
Class Application/Program -superclass Application
Leigh B. Stoller's avatar
Leigh B. Stoller committed
31 32 33 34

namespace eval GLOBALS {
    set new_classes(Agent/UDP) {}
    set new_classes(Agent/Null) {}
35 36
    set new_classes(Agent/TCP) {}
    set new_classes(Agent/TCPSINK) {}
37 38 39 40 41
    set new_classes(Agent/TCP/FullTcp) {}
    set new_classes(Agent/TCP/FullTcp/Reno) {}
    set new_classes(Agent/TCP/FullTcp/Newreno) {}
    set new_classes(Agent/TCP/FullTcp/Tahoe) {}
    set new_classes(Agent/TCP/FullTcp/Sack) {}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
42
    set new_classes(Application/Traffic/CBR) {}
43 44
    set new_classes(Application/FTP) {}
    set new_classes(Application/Telnet) {}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
45 46 47 48 49 50 51
}

# Agent
Agent instproc init {} {
    $self set node {}
    $self set application {}
    $self set destination {}
52 53 54
    $self set proto {}
    $self set role {}
    $self set port {}
55
    $self set generator "TG"
56 57
    global ::GLOBALS::last_class
    set ::GLOBALS::last_class $self
Leigh B. Stoller's avatar
Leigh B. Stoller committed
58 59 60
}
Agent instproc set_node {node} {
    $self set node $node
61
    $self set port [$node next_portnumber]
Leigh B. Stoller's avatar
Leigh B. Stoller committed
62
}
63 64 65 66
Agent instproc get_node {} {
    $self instvar node
    return $node
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
67 68
Agent instproc set_application {application} { 
    $self set application $application
69 70
    $self set role [$application set role]
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
71 72 73 74
}
Agent instproc connect {dst} {
    $self instvar destination
    if {$destination != {}} {
75
	perror "\[connect] $self already has a destination: $destination."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
76 77 78 79
	return
    }
    set destination $dst
}
80 81 82 83 84 85 86
Agent instproc rename {old new} {
    $self instvar application
    # In normal conditions this will never occur.
    if {$application != {}} {
	$application set agent $new
    }
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
87

88 89 90 91 92
# updatedb DB
# This adds rows to the virt_trafgens table corresponding to this agent.
Agent instproc updatedb {DB} {
    var_import ::GLOBALS::pid
    var_import ::GLOBALS::eid
93
    var_import ::GLOBALS::objtypes
94 95 96 97 98
    $self instvar application
    $self instvar destination
    $self instvar node
    $self instvar proto
    $self instvar role
99
    $self instvar generator
100 101 102 103 104 105 106 107 108 109 110 111 112 113
    $self instvar port

    if {$role == {}} {
	perror "\[updatedb] $self has no role."
	return
    }
    if {$destination == {}} {
	perror "\[updatedb] $self has no destination."
	return
    }
    set target_vnode [$destination set node]
    set target_port [$destination set port]

    if {$role == "sink"} {
114
	set application $self
115 116 117 118 119 120
	set proto [$destination set proto]
    }
#   set src_link [lindex [$node set portlist] 0]
#   set dst_link [lindex [$target_vnode set portlist] 0]

    # Update the DB
121
    sql exec $DB "insert into virt_trafgens (pid,eid,vnode,vname,role,proto,port,target_vnode,target_port,generator) values ('$pid','$eid','$node','$application','$role','$proto', $port,'$target_vnode',$target_port,'$generator')";
122

123
    sql exec $DB "insert into virt_agents (pid,eid,vnode,vname,objecttype) values ('$pid','$eid','$node','$application','$objtypes(TRAFGEN)')";
124 125 126 127 128 129
}

# get_nseconfig is only defined for subclasses that will be simulated by NSE
# Will be called from Node updatedb
Agent instproc get_nseconfig {} {
   return ""
130 131
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
132 133 134 135 136 137
# Agent/UDP 
Agent/UDP instproc connect {dst} {
    $self next $dst
    $self instvar node
    $self instvar application
    $self instvar destination
138

Leigh B. Stoller's avatar
Leigh B. Stoller committed
139 140
    set error 0
    if {$node == {}} {
141
	perror "\[connect] $self is not attached to a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
142 143 144
	set error 1
    }
    if {$application == {}} {
145
	perror "\[connect] $self does not have an attached application."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
146 147 148 149
	set error 1
    }
    set dest [$destination set node]
    if {$dest == {}} {
150
	perror "\[connect] $destination is not attached to a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
151 152 153
	set error 1
    }
    if {[llength [$node set portlist]] != 1} {
154
	perror "\[connect] $node must have exactly one link to be a traffic generator."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
155 156 157 158
	set error 1
    }
    if {$error} {return}

159 160
    $self set proto "udp"
    $node set osid "FBSD-STD"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
161 162
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
# Agent/TCP
Agent/TCP instproc connect {dst} {
    $self next $dst
    $self instvar node
    $self instvar application
    $self instvar destination

    set error 0
    if {$node == {}} {
	perror "\[connect] $self is not attached to a node."
	set error 1
    }
    if {$application == {}} {
	perror "\[connect] $self does not have an attached application."
	set error 1
    }
    set dest [$destination set node]
    if {$dest == {}} {
	perror "\[connect] $destination is not attached to a node."
	set error 1
    }
    if {[llength [$node set portlist]] != 1} {
	perror "\[connect] $node must have exactly one link to be a traffic generator."
	set error 1
    }
    if {$error} {return}

    $self set proto "tcp"
    $node set osid "FBSD-STD"
}

194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
# Agent/TCP/FullTcp
Agent/TCP/FullTcp instproc init args {

    eval $self next $args
    $self set simulated 1
    $self set tcptype ""
    $self set segsize_ 1460
    $self set role "source" 
    $self set generator "NSE"
}

# Agent/TCP/FullTcp/Newreno
Agent/TCP/FullTcp/Newreno instproc init args {
    eval $self next $args
    $self set tcptype "Newreno"
}

# Agent/TCP/FullTcp/Tahoe
Agent/TCP/FullTcp/Tahoe instproc init args {
    eval $self next $args
    $self set tcptype "Tahoe"
}

# Agent/TCP/FullTcp/Sack
Agent/TCP/FullTcp/Sack instproc init args {
    eval $self next $args
    $self set tcptype "Sack"
}

# Agent/TCP/FullTcp
Agent/TCP/FullTcp instproc listen {} {

   $self set role "sink"

}

# updatedb DB
# This adds rows to the virt_trafgens table corresponding to this agent.
# We do things differently from the base class version. For NSE, the
# vname in the table is the FullTcp agent object and not the application
# object connected to it. The important thing is using "$ns at", we can
# send events and control any of these objects
Agent/TCP/FullTcp instproc updatedb {DB} {
    var_import ::GLOBALS::pid
    var_import ::GLOBALS::eid
239
    var_import ::GLOBALS::objtypes
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
    $self instvar application
    $self instvar destination
    $self instvar node
    $self instvar proto
    $self instvar role
    $self instvar generator
    $self instvar port

    if {$role == {}} {
	perror "\[updatedb] $self has no role."
	return
    }
    if {$destination == {}} {
	perror "\[updatedb] $self has no destination."
	return
    }
    set target_vnode [$destination set node]
    set target_port [$destination set port]

    set vname $self

    # Update the DB
    sql exec $DB "insert into virt_trafgens (pid,eid,vnode,vname,role,proto,port,target_vnode,target_port,generator) values ('$pid','$eid','$node','$vname','$role','$proto', $port,'$target_vnode',$target_port,'$generator')";

264
    sql exec $DB "insert into virt_agents (pid,eid,vnode,vname,objecttype) values ('$pid','$eid','$node','$vname','$objtypes(TRAFGEN)')";
265

266 267 268
    if {$application != {}} {
	sql exec $DB "insert into virt_agents (pid,eid,vnode,vname,objecttype) values ('$pid','$eid','$node','$application','$objtypes(TRAFGEN)')";
    }
269 270 271 272 273 274 275 276 277
}

# Agent/TCP/FullTcp
Agent/TCP/FullTcp instproc get_nseconfig {} {

    $self instvar tcptype
    $self instvar role
    $self instvar simulated
    $self instvar application
278
    $self set objname $self
279 280 281 282 283 284 285 286 287 288 289 290 291 292
    
    set nseconfig ""

    # This agent could possibly be a real one in which case
    # we don't do NSE traffic generation
    if { $simulated != 1 } {
         return $nseconfig
    }

    if { ($tcptype == "") || ($tcptype == "Reno") } {
          set nseconfig "set $self \[new Agent/TCP/FullTcp]\n"
    } else {
          set nseconfig "set $self \[new Agent/TCP/FullTcp/$tcptype]\n"
    }
293

294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
    if { $role == "sink" } {
         append nseconfig "\$$self listen\n\n"
    }

    # We end up including variables present only in TB parser. However
    # that does not matter coz all NS variables end with _ character
    # Pruning can be done later
    foreach var [$self info vars] {
           if { [$self set $var] != {} } {
             append nseconfig "\$$self set $var [$self set $var]\n"
           }
    }

    if { $application != {} } {
         append nseconfig [$application get_nseconfig]
    } 

    return $nseconfig
}


# Agent/TCP/FullTcp
Agent/TCP/FullTcp instproc connect {dst} {
    $self next $dst
    $self instvar node
    $self instvar application
    $self instvar destination
    $self instvar role

    set error 0
    if {$node == {}} {
        perror "\[connect] $self is not attached to a node."
        set error 1
    }
    if { ($role == "source") && ($application == {}) } {
        perror "\[connect] $self does not have an attached application."
        set error 1
    }
    set dest [$destination set node]
    if {$dest == {}} {
        perror "\[connect] $destination is not attached to a node."
        set error 1
    }

# This was an artifact of SEND-CONSUME CBR model. Now that we run freebsd on the
# traffic generator nodes, all we need to do is either enable OSPF routing by
# default or setup static routes
    if {[llength [$node set portlist]] != 1} {
        perror "\[connect] $node must have exactly one link to be a traffic generator."
        set error 1
    }
    if {$error} {return}

    $self set proto "tcp"
    $dst set proto "tcp"
    $node set osid "FBSD-STD"
    $dest set osid "FBSD-STD"
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
353 354 355 356 357 358 359 360
# Agent/Null
Agent/Null instproc connect {dst} {
    $self next $dst
    $self instvar node
    $self instvar application
    $self instvar destination
    set error 0
    if {$node == {}} {
361
	perror "\[connect] $self is not attached to a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
362 363 364 365
	set error 1
    }
    set dest [$destination set node]
    if {$dest == {}} {
366
	perror "\[connect] $destination is not attached to a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
367 368 369
	set error 1
    }
    if {[llength [$node set portlist]] != 1} {
370
	perror "\[connect] $node must have exactly one link to be a traffic consumer."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
371 372 373 374
	set error 1
    }
    if {$error} {return}

375 376
    $self set role "sink"
    $node set osid "FBSD-STD"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
377 378
}

379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
# Agent/Null
Agent/TCPSINK instproc connect {dst} {
    $self next $dst
    $self instvar node
    $self instvar application
    $self instvar destination
    set error 0
    if {$node == {}} {
	perror "\[connect] $self is not attached to a node."
	set error 1
    }
    set dest [$destination set node]
    if {$dest == {}} {
	perror "\[connect] $destination is not attached to a node."
	set error 1
    }
    if {[llength [$node set portlist]] != 1} {
	perror "\[connect] $node must have exactly one link to be a traffic consumer."
	set error 1
    }
    if {$error} {return}

    $self set role "sink"
    $self set proto "tcp"
    $node set osid "FBSD-STD"
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
406 407 408
# Application
Application instproc init {} {
    $self set agent {}
409
    $self set role {}
410 411
    global ::GLOBALS::last_class
    set ::GLOBALS::last_class $self
Leigh B. Stoller's avatar
Leigh B. Stoller committed
412 413 414 415 416
}
Application instproc attach-agent {agent} {
    $self set agent $agent
    $agent set_application $self
}
417 418 419 420
Application instproc get_node {} {
    $self instvar agent
    return [$agent get_node]
}
421 422
Application instproc rename {old new} {
    $self instvar agent
423
    # In normal condition this will never occur.
424 425 426 427
    if {$agent != {}} {
	$agent set_application $self
    }
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
428 429 430 431 432 433 434

# Application/Traffic/CBR
Application/Traffic/CBR instproc init {} {
    $self set packetSize_ 210
    $self set rate_ "100Mbps"
    $self set interval_ {}
    $self next
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
    
    $self set role "source"
}

Application/Traffic/CBR instproc get_params {} {
    $self instvar packetSize_
    $self instvar rate_
    $self instvar interval_

    if {$rate_ != {} && $rate_ != 0} {
	set rate [parse_bw $rate_]
    } else {
	set rate -1
    }
    set param "PACKETSIZE=$packetSize_ RATE=$rate"

    if {$interval_ != {} && $interval_ != 0} {
	set param "$param INTERVAL=$interval_"
    } else {
	set param "$param INTERVAL=-1"
    }
    return $param
Leigh B. Stoller's avatar
Leigh B. Stoller committed
457
}
458 459 460 461 462 463 464 465 466 467 468 469 470

# Application/FTP
Application/FTP instproc init {} {
    $self next
   
    $self set role "source"
}

# Application/FTP
Application/FTP instproc get_nseconfig {} {

    $self instvar agent
    set nseconfig "set $self \[new Application/FTP]\n"
471 472
    
    append nseconfig "\$$self set objname $self\n"
473 474 475 476 477 478

    if { $agent != {} } {
         append nseconfig "\$$self attach-agent \$$agent\n\n"
    }

    # XXX temporary untill event system changes get in
479
    # append nseconfig "\[Simulator instance] at 30.0 \"\$$self start\"\n\n"
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505

   return $nseconfig
}

# Application/Telnet
Application/Telnet instproc init {} {
    $self next
    $self instvar role
    $self instvar interval_

    $self set role "source"

# NS's default is 1.0 and therefore would result in inter-packet times
# to be chosen from the exponential distribution. If it is 0, it would
# be chosen based on the tcplib distribution. I differ from NS in my
# default because of the realism of tcplib distribution
    $self set interval_ 0.0
}

# Application/Telnet
Application/Telnet instproc get_nseconfig {} {
   
    $self instvar agent
    $self instvar interval_

    set nseconfig "set $self \[new Application/Telnet]\n"
506 507

    append nseconfig "\$$self set objname $self\n"
508 509 510 511 512 513 514
               
    append nseconfig "\$$self set interval_ $interval_\n"
    if { $agent != {} } {
         append nseconfig "\$$self attach-agent \$$agent\n\n"
    }
        
    # XXX temporary untill event system changes get in
515
    # append nseconfig "\[Simulator instance] at 30.0 \"\$$self start\"\n\n"
516 517 518

    return $nseconfig
}
519