sim.tcl.in 43.3 KB
Newer Older
1
# -*- tcl -*-
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2006 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
5
6
7
# All rights reserved.
#

Leigh B. Stoller's avatar
Leigh B. Stoller committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
######################################################################
# sim.tcl
#
# Defines the Simulator class.  For our purpose a Simulator is a
# topology.  This contains a number nodes, lans, and links.  It
# provides methods for the creation of these objects as well as
# routines to locate the objects.  It also stores common state (such
# as IP subnet usage).  Finally it defines the import 'run' method
# which causes all remaining calculations to be done and updates the
# DB state.
#
# Note: Although NS distinguishs between LANs and Links, we do not.
# We merge both types of objects into a single one called LanLink.  
# See lanlink.tcl and README for more information.
######################################################################

Class Simulator
25
Class Program -superclass NSObject
Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
Class EventGroup -superclass NSObject
27
Class Firewall -superclass NSObject
Leigh B. Stoller's avatar
Leigh B. Stoller committed
28
29
30
31
32

Simulator instproc init {args} {
    # A counter for internal ids
    $self set id_counter 0

33
    # Counters for subnets. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
34
    $self set subnet_counter 1
35
    $self set wa_subnet_counter 1
Leigh B. Stoller's avatar
Leigh B. Stoller committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

    # This is the prefix used to fill any unassigned IP addresses.
    $self set subnet_base "@IPBASE@"

    # The following are sets.  I.e. they map to no value, all
    # we care about is membership.
    $self instvar node_list;		# Indexed by node id
    array set node_list {}
    $self instvar lanlink_list;		# Indexed by lanlink id
    array set lanlink_list {}
    $self instvar subnets;		# Indexed by IP subnet
    array set subnets {}

    # link_map is indexed by <node1>:<node2> and contains the
    # id of the lanlink connecting them.  In the case of
    # multiple links between two nodes this contains
    # the last one created.
    $self instvar link_map
    array set link_map {}
55
56
57

    # event list is a list of {time vnode vname otype etype args atstring}
    $self set event_list {}
58
    $self set event_count 0
59

Shashi Guruprasad's avatar
Shashi Guruprasad committed
60
61
62
    # global nse config file. to be split later
    $self set nseconfig ""

63
64
65
    # Program list.
    $self instvar prog_list;
    array set prog_list {}
66

Leigh B. Stoller's avatar
Leigh B. Stoller committed
67
68
69
70
    # EventGroup list.
    $self instvar eventgroup_list;
    array set eventgroup_list {}

71
72
73
74
    # Firewall.
    $self instvar firewall_list;
    array set firewall_list {}

Timothy Stack's avatar
   
Timothy Stack committed
75
76
77
78
79
80
    $self instvar timeline_list;
    array set timeline_list {}

    $self instvar sequence_list;
    array set sequence_list {}

Timothy Stack's avatar
   
Timothy Stack committed
81
82
83
84
85
86
    $self instvar console_list;
    array set console_list {}

    $self instvar tiptunnel_list;
    set tiptunnel_list {}

Timothy Stack's avatar
   
Timothy Stack committed
87
88
89
    $self instvar topography_list;
    array set topography_list {}

90
91
92
    $self instvar parameter_list;
    array set parameter_list {}

93
94
    var_import ::GLOBALS::last_class
    set last_class $self
Timothy Stack's avatar
   
Timothy Stack committed
95
96
97
98

    $self instvar new_node_config;
    array set new_node_config {}
    $self node-config
99
100
101
102
103
104
}

# renaming the simulator instance
# needed to find the name of the instance
# for use in NSE code
Simulator instproc rename {old new} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
105
106
}

Timothy Stack's avatar
   
Timothy Stack committed
107
108
109
110
111
112
113
114
115
116
117
Simulator instproc node-config {args} {
    ::GLOBALS::named-args $args {
	-topography ""
    }

    $self instvar new_node_config;
    foreach {key value} [array get ""] {
	set new_node_config($key) $value
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
118
119
120
121
122
# node
# This method adds a new node to the topology and returns the id
# of the node.
Simulator instproc node {args} {
    var_import ::GLOBALS::last_class
123
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
124
125
126
    $self instvar id_counter
    $self instvar node_list

127
128
    if {($args != {})} {
	punsup "Arguments for node: $args"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
129
130
    }
    
131
    set curnode tbnode-n[incr id_counter]
Leigh B. Stoller's avatar
Leigh B. Stoller committed
132
    Node $curnode $self
133

Shashi Guruprasad's avatar
Shashi Guruprasad committed
134
135
136
    # simulated nodes have type 'sim'
    if { $simulated == 1 } {
        tb-set-hardware $curnode sim
137
138
139
140
	# This allows assign to prefer pnodes
	# that already have FBSD-NSE as the default
	# boot osid over others
	$curnode add-desire "FBSD-NSE" 0.9
141
    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
142
    set node_list($curnode) {}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
143
144
    set last_class $curnode

Timothy Stack's avatar
   
Timothy Stack committed
145
146
147
    $self instvar new_node_config;
    $curnode topography $new_node_config(-topography)

Leigh B. Stoller's avatar
Leigh B. Stoller committed
148
149
150
151
152
153
154
155
156
    return $curnode
}

# duplex-link <node1> <node2> <bandwidth> <delay> <type>
# This adds a new link to the topology.  <bandwidth> can be in any
# form accepted by parse_bw and <delay> in any form accepted by
# parse_delay.  Currently only the type 'DropTail' is supported.
Simulator instproc duplex-link {n1 n2 bw delay type args} {
    var_import ::GLOBALS::last_class
157
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
158
159
160
161
    $self instvar id_counter
    $self instvar lanlink_list
    $self instvar link_map

162
163
    if {($args != {})} {
	punsup "Arguments for duplex-link: $args"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
164
165
166
    }
    set error 0
    if {! [$n1 info class Node]} {
167
	perror "\[duplex-link] $n1 is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
168
169
170
	set error 1
    }
    if {! [$n2 info class Node]} {
171
	perror "\[duplex-link] $n2 is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
172
173
	set error 1
    }
174
175
176
177
#    if { [$n1 set isvirt] != [$n2 set isvirt] } {
#	perror "\[duplex-link] Bad link between real and virtual node!"
#	set error 1
#    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
178
179
180
181
182
183
184
185

    if { $simulated == 1 && ( [$n1 set simulated] == 0 || [$n2 set simulated] == 0 ) } {
	set simulated 0
	perror "\[duplex-link] Please define links between real and simulated nodes outside make-simulated"
	set simulated 1
	set error 1
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
186
187
188
189
190
191
    if {$error} {return}

    # Convert bandwidth and delay
    set rbw [parse_bw $bw]
    set rdelay [parse_delay $delay]

192
    set curlink tblink-l[incr id_counter]
193

Shashi Guruprasad's avatar
Shashi Guruprasad committed
194
    Link $curlink $self "$n1 $n2" $rbw $rdelay $type	
195
    set lanlink_list($curlink) {}
Shashi Guruprasad's avatar
Shashi Guruprasad committed
196
197
198
    set link_map($n1:$n2) $curlink
    set link_map($n2:$n1) $curlink

Leigh B. Stoller's avatar
Leigh B. Stoller committed
199
200
201
202
203
204
205
206
207
208
    set last_class $curlink
    return $curlink
}

# make-lan <nodelist> <bw> <delay>
# This adds a new lan to the topology. <bandwidth> can be in any
# form accepted by parse_bw and <delay> in any form accepted by
# parse_delay.
Simulator instproc make-lan {nodelist bw delay args} {
    var_import ::GLOBALS::last_class
209
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
210
211
212
    $self instvar id_counter
    $self instvar lanlink_list

213
214
    if {($args != {})} {
	punsup "Arguments for make-lan: $args"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
215
    }
216

217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
    #
    # The number of virtual nodes has to be zero, or equal to the number
    # of nodes (In other word, no mixing of real and virtual nodes).
    #
#    set acount 0
#    set vcount 0
#    foreach node $nodelist {
#	if { [$node set isvirt] } {
#	    incr vcount
#	}
#	incr acount
#    }
#    if { ($vcount != 0) && ($vcount != $acount) } {
#	perror "\[duplex-link] Bad lan between real and virtual nodes!"
#	set error 1
#	return ""
#    }

Shashi Guruprasad's avatar
Shashi Guruprasad committed
235
236
237
238
239
240
    # At this point we have one of the nodes of
    # the lan to be real. We need to make sure
    # that this is not being defined in make-simulated.
    # In other words links or lans from real nodes and
    # simulated nodes should happen outside make-simulated
    if { $simulated == 1 } {
241

Shashi Guruprasad's avatar
Shashi Guruprasad committed
242
243
244
245
246
247
248
	foreach node $nodelist {
	    if { [$node set simulated] == 0 } {	
		set simulated 0
		perror "Please define lans between real and simulated nodes outside make-simulated"
		set simulated 1
		return ""
	    }
249
250
	}
    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
251
252
253
254
255
256

    set curlan tblan-lan[incr id_counter]
    
    # Convert bandwidth and delay
    set rbw [parse_bw $bw]
    set rdelay [parse_delay $delay]
257
258
259
260
261

    # Warn about potential rounding of delay values due to implementation
    if { ($rdelay % 2) == 1 } {
	puts stderr "*** WARNING: due to delay implementation, odd delay value $rdelay for LAN may be rounded up"
    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
262
263
264
265
266
267
    
    Lan $curlan $self $nodelist $rbw $rdelay {}
    set lanlink_list($curlan) {}
    set last_class $curlan
    
    return $curlan
Leigh B. Stoller's avatar
Leigh B. Stoller committed
268
269
}

270
Simulator instproc make-cloud {nodelist bw delay args} {
271
272
273
    $self instvar event_list
    $self instvar event_count

274
275
276
277
278
    if {($args != {})} {
	punsup "Arguments for make-cloud: $args"
    }

    set retval [$self make-lan $nodelist $bw $delay]
279
280
281
282
283
284
285

    $retval set iscloud 1
    $retval mustdelay

    return $retval
}

Timothy Stack's avatar
   
Timothy Stack committed
286
287
288
289
290
291
292
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
Simulator instproc event-timeline {args} {
    $self instvar id_counter
    $self instvar timeline_list

    set curtl tbtl-tl[incr id_counter]

    EventTimeline $curtl $self
    set timeline_list($curtl) {}

    return $curtl
}

Simulator instproc event-sequence {{seq {}} {catch {}} {catch_seq {}}} {
    $self instvar id_counter
    $self instvar sequence_list

    if {$catch != {}} {
	set mainseq tbseq-seq[incr id_counter]
	set catchseq tbseq-seq[incr id_counter]
	set curseq tbseq-seq[incr id_counter]

	EventSequence $mainseq $self [uplevel 1 subst [list $seq]]
	EventSequence $catchseq $self [uplevel 1 subst [list $catch_seq]]
	EventSequence $curseq $self [subst {
	    $mainseq run
	    $catchseq run
	}] -errorseq 1
	set sequence_list($mainseq) {}
	set sequence_list($catchseq) {}
	set sequence_list($curseq) {}
    } else {
	set curseq tbseq-seq[incr id_counter]
Timothy Stack's avatar
   
Timothy Stack committed
318
319
320
321
322
323
	set lines {}

	foreach line [split $seq "\n"] {
	    lappend lines [uplevel 1 eval list [list $line]]
	}
	EventSequence $curseq $self $lines
Timothy Stack's avatar
   
Timothy Stack committed
324
325
326
327
328
329
	set sequence_list($curseq) {}
    }

    return $curseq
}

Timothy Stack's avatar
   
Timothy Stack committed
330
Simulator instproc event-group {{list {}}} {
Timothy Stack's avatar
   
Timothy Stack committed
331
    set curgrp [new EventGroup $self]
Timothy Stack's avatar
   
Timothy Stack committed
332
333
    if {$list != {}} {
	foreach obj $list {
Timothy Stack's avatar
   
Timothy Stack committed
334
335
336
337
338
339
340
	    $curgrp add $obj
	}
    }

    return $curgrp
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
341
342
343
344
345
346
347
348
# run
# This method causes the fill_ips method to be invoked on all 
# lanlinks and then, if not running in impotent mode, calls the
# updatedb method on all nodes and lanlinks.  Invocation of this
# method casues the 'ran' variable to be set to 1.
Simulator instproc run {} {
    $self instvar lanlink_list
    $self instvar node_list
349
    $self instvar event_list
350
    $self instvar prog_list
Leigh B. Stoller's avatar
Leigh B. Stoller committed
351
    $self instvar eventgroup_list
352
    $self instvar firewall_list
Timothy Stack's avatar
   
Timothy Stack committed
353
354
    $self instvar timeline_list
    $self instvar sequence_list
Timothy Stack's avatar
   
Timothy Stack committed
355
356
    $self instvar console_list
    $self instvar tiptunnel_list
Timothy Stack's avatar
   
Timothy Stack committed
357
    $self instvar topography_list
358
    $self instvar parameter_list
Shashi Guruprasad's avatar
Shashi Guruprasad committed
359
360
    $self instvar simulated
    $self instvar nseconfig
361
362
    var_import ::GLOBALS::pid
    var_import ::GLOBALS::eid
Leigh B. Stoller's avatar
Leigh B. Stoller committed
363
364
365
366
    var_import ::GLOBALS::errors
    var_import ::GLOBALS::irfile
    var_import ::GLOBALS::ran
    var_import ::GLOBALS::impotent
367
    var_import ::GLOBALS::passmode
368
    var_import ::GLOBALS::vtypes
369
    var_import ::GLOBALS::uselatestwadata
370
    var_import ::GLOBALS::usewatunnels
371
372
373
    var_import ::GLOBALS::wa_delay_solverweight
    var_import ::GLOBALS::wa_bw_solverweight
    var_import ::GLOBALS::wa_plr_solverweight
374
375
    var_import ::GLOBALS::uselinkdelays
    var_import ::GLOBALS::forcelinkdelays
376
    var_import ::GLOBALS::multiplex_factor
377
    var_import ::GLOBALS::sync_server
378
379
    var_import ::GLOBALS::use_ipassign
    var_import ::GLOBALS::ipassign_args
380
381
    var_import ::GLOBALS::cpu_usage
    var_import ::GLOBALS::mem_usage
382
    var_import ::GLOBALS::fix_current_resources
383
    var_import ::GLOBALS::veth_encapsulate
384
385
    var_import ::GLOBALS::jail_osname
    var_import ::GLOBALS::delay_osname
386
    var_import ::GLOBALS::delay_capacity
387
388
    var_import ::TBCOMPAT::objtypes
    var_import ::TBCOMPAT::eventtypes
389
390
    var_import ::GLOBALS::modelnet_cores
    var_import ::GLOBALS::modelnet_edges
391
    var_import ::GLOBALS::elab_in_elab
392
    var_import ::GLOBALS::elabinelab_topo
393
    var_import ::GLOBALS::elabinelab_eid
394
    var_import ::GLOBALS::elabinelab_cvstag
395
    var_import ::GLOBALS::security_level
396
    var_import ::GLOBALS::explicit_firewall
397
    var_import ::GLOBALS::sourcefile_list
398
    
399
400
401
402
403
    if {$ran == 1} {
	perror "The Simulator 'run' statement can only be run once."
	return
    }

404
    if {$elab_in_elab && [llength [array names node_list]] == 0} {
405
406
407
408
409
	if {$elabinelab_topo == ""} {
	    set nsfilename "elabinelab.ns"
	} else {
	    set nsfilename "elabinelab-${elabinelab_topo}.ns"
	}
410
	uplevel 1 real_source "@prefix@/lib/ns2ir/${nsfilename}"
411
    }
412
    if {$security_level || $explicit_firewall} {
413
	uplevel 1 real_source "@prefix@/lib/ns2ir/fw.ns"
414
    }
415

Leigh B. Stoller's avatar
Leigh B. Stoller committed
416
    # Fill out IPs
417
418
419
420
    if {! $use_ipassign } {
	foreach obj [concat [array names lanlink_list]] {
	    $obj fill_ips
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
421
422
    }

423
424
425
426
427
428
429
430
431
432
    # Go through the list of nodes, and find subnode hosts - we have to add a
    # desire to them to have the hosts-<type-of-child> feature
    foreach node [lsort [array names node_list]] {
	if { [$node set subnodehost] == 1 } {
	    set child [$node set subnodechild]
	    set childtype [$child set type]
	    $node add-desire "hosts-$childtype" 1.0
	}
    }

433
434
435
436
437
438
439
440
    # If the experiment is firewalled, make sure that all nodes in the
    # experiment have the "firewallable" feature.
    if {[array size firewall_list] > 0} {
	foreach node [lsort [array names node_list]] {
	    $node add-desire "firewallable" 1.0
	}
    }

441
442
    # Default sync server.
    set default_sync_server {}
443

Leigh B. Stoller's avatar
Leigh B. Stoller committed
444
445
446
447
448
449
    # Mark that a run statement exists
    set ran 1

    # Check node names.
    foreach node [lsort [array names node_list]] {
	if {! [regexp {^[-0-9A-Za-z]+$} $node]} {
450
	    perror "\[run] Invalid node name $node.  Can only contain \[-0-9A-Za-z\] due to DNS limitations."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
451
452
453
454
	}
    }
    foreach lan [lsort [array names lanlink_list]] {
	if {! [regexp {^[-0-9A-Za-z]+$} $lan]} {
455
	    perror "\[run] Invalid lan/link name $lan.  Can only contain \[-0-9A-Za-z\] for symmetry with node DNS limitations."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
456
457
458
	}
    }

459
460
461
462
463
464
465
    # Check for one node lans
    foreach lan [array names lanlink_list] {
	if {[llength [$lan set nodelist]] <= 1} {
	    perror "\[run] $lan has only a single node.  LANs must have at least 2 nodes in them."
	}
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
466
467
468
    # If any errors occur stop here.
    if {$errors == 1} {return}

469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
    # Write out the feedback "bootstrap" file.
    var_import ::TBCOMPAT::expdir;
    var_import ::TBCOMPAT::BootstrapReservations;

    if {! [file isdirectory $expdir]} {
	# Experiment directory does not exist, so we cannot write the file...
    } elseif {[array size BootstrapReservations] > 0} {
	set file [open "$expdir/tbdata/bootstrap_data.tcl" w]
	puts $file "# -*- TCL -*-"
	puts $file "# Automatically generated feedback bootstrap file."
	puts $file "#"
	puts $file "# Generated at: [clock format [clock seconds]]"
	puts $file "#"
	puts $file ""
	foreach res [array names BootstrapReservations] {
	    puts $file "set Reservations($res) $BootstrapReservations($res)"
	}
	close $file
    }

489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
    # Write out the feedback "estimate" file.
    var_import ::TBCOMPAT::EstimatedReservations;

    if {! [file isdirectory $expdir]} {
	# Experiment directory does not exist, so we cannot write the file...
    } elseif {[array size EstimatedReservations] > 0} {
	set file [open "$expdir/tbdata/feedback_estimate.tcl" w]
	puts $file "# -*- TCL -*-"
	puts $file "# Automatically generated feedback estimated file."
	puts $file "#"
	puts $file "# Generated at: [clock format [clock seconds]]"
	puts $file "#"
	puts $file ""
	foreach res [array names EstimatedReservations] {
	    puts $file "set EstimatedReservations($res) $EstimatedReservations($res)"
	}
	close $file
    }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
508
    # If we are running in impotent mode we stop here
509
    if {$impotent == 1 && $passmode == 0} {return}
510
    
511
512
    $self spitxml_init

Shashi Guruprasad's avatar
Shashi Guruprasad committed
513
514
515
516
517
518
519
520
521
522
523
524
    # update the global nseconfigs using a bogus vname
    # i.e. instead of the node on which nse is gonna run
    # which was the original vname field, we just put $ns
    # for now. Once assign runs, the correct value will be
    # entered into the database
    if { $nseconfig != {} } {
 
 	set nsecfg_script ""
 	set simu [lindex [Simulator info instances] 0]
 	append nsecfg_script "set $simu \[new Simulator]\n"
 	append nsecfg_script "\$$simu use-scheduler RealTime\n\n"
 	append nsecfg_script $nseconfig
525

526
	$self spitxml_data "nseconfigs" [list "vname" "nseconfig" ] [list fullsim $nsecfg_script ]
Shashi Guruprasad's avatar
Shashi Guruprasad committed
527
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
528
529
530
    
    # Update the DB
    foreach node [lsort [array names node_list]] {
531
	$node updatedb "sql"
532

Timothy Stack's avatar
   
Timothy Stack committed
533
	if { $default_sync_server == {} && ![$node set issubnode] } {
534
535
	    set default_sync_server $node
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
536
    }
537
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
538
    foreach lan [concat [array names lanlink_list]] {
539
	$lan updatedb "sql"
540
541
542
543
	if {[$lan set iscloud] != 0} {
	    lappend event_list [list "0" "*" $lan LINK CLEAR "" "" "__ns_sequence"]
	    lappend event_list [list "1" "*" $lan LINK CREATE "" "" "__ns_sequence"]
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
544
    }
545
    foreach vtype [array names vtypes] {
546
	$vtype updatedb "sql"
547
    }
548
    foreach prog [array names prog_list] {
549
	$prog updatedb "sql"
550
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
551
552
553
    foreach egroup [array names eventgroup_list] {
	$egroup updatedb "sql"
    }
554
555
556
    foreach fw [array names firewall_list] {
	$fw updatedb "sql"
    }
Timothy Stack's avatar
   
Timothy Stack committed
557
558
559
560
561
562
    foreach tl [array names timeline_list] {
	$tl updatedb "sql"
    }
    foreach seq [array names sequence_list] {
	$seq updatedb "sql"
    }
Timothy Stack's avatar
   
Timothy Stack committed
563
564
565
566
567
568
    foreach con [array names console_list] {
	$con updatedb "sql"
    }
    foreach tt $tiptunnel_list {
	$self spitxml_data "virt_tiptunnels" [list "host" "vnode"] $tt
    }
Timothy Stack's avatar
   
Timothy Stack committed
569
570
571
    foreach tg [array names topography_list] {
	$tg updatedb "sql"
    }
572

573
574
    set fields [list "mem_usage" "cpu_usage" "forcelinkdelays" "uselinkdelays" "usewatunnels" "uselatestwadata" "wa_delay_solverweight" "wa_bw_solverweight" "wa_plr_solverweight" "veth_encapsulate" "allowfixnode"]
    set values [list $mem_usage $cpu_usage $forcelinkdelays $uselinkdelays $usewatunnels $uselatestwadata $wa_delay_solverweight $wa_bw_solverweight $wa_plr_solverweight $veth_encapsulate $fix_current_resources]
575

576
    if { $multiplex_factor != {} } {
577
578
	lappend fields "multiplex_factor"
	lappend values $multiplex_factor
579
    }
580
    
581
582
583
    if { $sync_server != {} } {
	lappend fields "sync_server"
	lappend values $sync_server
584
585
586
    } elseif { $default_sync_server != {} } {
	lappend fields "sync_server"
	lappend values $default_sync_server
587
    }
588

589
590
591
592
593
594
595
596
    lappend fields "use_ipassign"
    lappend values $use_ipassign

    if { $ipassign_args != {} } {
	lappend fields "ipassign_args"
	lappend values $ipassign_args
    }

597
598
599
600
601
602
603
604
    if { $jail_osname != {} } {
	lappend fields "jail_osname"
	lappend values $jail_osname
    }
    if { $delay_osname != {} } {
	lappend fields "delay_osname"
	lappend values $delay_osname
    }
605
606
607
608
    if { $delay_capacity != {} } {
	lappend fields "delay_capacity"
	lappend values $delay_capacity
    }
609
610
611
612
613
614
615
616
617

    if {$modelnet_cores > 0 && $modelnet_edges > 0} {
	lappend fields "usemodelnet"
	lappend values 1
	lappend fields "modelnet_cores"
	lappend values $modelnet_cores
	lappend fields "modelnet_edges"
	lappend values $modelnet_edges
    }
618
    
619
620
621
    if {$elab_in_elab} {
	lappend fields "elab_in_elab"
	lappend values 1
622
623
624
625
626

	if { $elabinelab_eid != {} } {
	    lappend fields "elabinelab_eid"
	    lappend values $elabinelab_eid
	}
627
628
629
630
631
	
	if { $elabinelab_cvstag != {} } {
	    lappend fields "elabinelab_cvstag"
	    lappend values $elabinelab_cvstag
	}
632
633
    }
    
634
635
636
637
638
    if {$security_level} {
	lappend fields "security_level"
	lappend values $security_level
    }
    
639
    $self spitxml_data "experiments" $fields $values
640
641

    # This could probably be elsewhere.
642
    $self spitxml_data "virt_agents" [list "vnode" "vname" "objecttype" ] [list "*" $self $objtypes(SIMULATOR) ]
643
644
645

    # This will eventually be under user control.
    $self spitxml_data "virt_agents" [list "vnode" "vname" "objecttype" ] [list "*" "linktest" $objtypes(LINKTEST) ]
646
647

    $self spitxml_data "virt_agents" [list "vnode" "vname" "objecttype" ] [list "*" "slothd" $objtypes(SLOTHD) ]
648
    
Timothy Stack's avatar
   
Timothy Stack committed
649
650
651
652
653
654
    if {[array exists ::opt]} {
	foreach {oname ovalue} [array get ::opt] {
	    $self spitxml_data "virt_user_environment" [list "name" "value" ] [list "$oname" "$ovalue" ]
	}
    }

655
    foreach event $event_list {
656
657
658
659
660
661
662
	set fields [list "time" "vnode" "vname" "objecttype" "eventtype" "arguments" "atstring" ]
	set values [list [lindex $event 0] [lindex $event 1] [lindex $event 2] $objtypes([lindex $event 3]) $eventtypes([lindex $event 4]) [lindex $event 5] [lindex $event 6]]
	if {[llength $event] > 7} {
	    lappend fields "parent"
	    lappend values [lindex $event 7]
	}
	$self spitxml_data "eventlist" $fields $values
663
    }
664

665
666
667
668
    foreach sourcefile $sourcefile_list {
	$self spitxml_data "external_sourcefiles" [list "pathname" ] [list $sourcefile ]
    }

669
670
671
672
673
674
675
676
677
678
    foreach name [array names parameter_list] {
	set default_value $parameter_list($name)

	$self spitxml_data "virt_parameters" [list "name" "value"] [list $name $default_value ]	
    }
	
    foreach sourcefile $sourcefile_list {
	$self spitxml_data "external_sourcefiles" [list "pathname" ] [list $sourcefile ]
    }

679
    $self spitxml_finish
Leigh B. Stoller's avatar
Leigh B. Stoller committed
680
681
682
683
684
}

# attach-agent <node> <agent>
# This creates an attachment between <node> and <agent>.
Simulator instproc attach-agent {node agent} {
685
686
    var_import ::GLOBALS::simulated

Leigh B. Stoller's avatar
Leigh B. Stoller committed
687
    if {! [$agent info class Agent]} {
688
	perror "\[attach-agent] $agent is not an Agent."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
689
690
691
	return
    }
    if {! [$node info class Node]} {
692
	perror "\[attach-agent] $node is not a Node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
693
694
	return
    }
695
696
697
698
699
700
701
702
703
704

    # If the node is real and yet this code is in make-simulated
    # we don't allow it
    if { [$node set simulated] == 0 && $simulated == 1 } {
	set simulated 0
	perror "Please attach agents on to real nodes outside make-simulated"
	set simulated 1
	return ""
    }

705
    $node attach-agent $agent
Leigh B. Stoller's avatar
Leigh B. Stoller committed
706
707
}

708
Simulator instproc agentinit {agent} {
709
710
    var_import ::TBCOMPAT::objtypes
    var_import ::TBCOMPAT::eventtypes
711
712

    if {[$agent info class Application/Traffic/CBR]} {
713
	$self spitxml_data "eventlist" [list "time" "vnode" "vname" "objecttype" "eventtype" "arguments" "atstring" "parent" ] [list "0" [$agent get_node] $agent $objtypes(TRAFGEN) $eventtypes(MODIFY) [$agent get_params] "" "__ns_sequence"]
714
715
716
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
717
718
719
# connect <src> <dst>
# Connects two agents together.
Simulator instproc connect {src dst} {
Timothy Stack's avatar
   
Timothy Stack committed
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
    $self instvar tiptunnel_list

    if {([$src info class Node] && [$dst info class Console]) ||
	([$src info class Console] && [$dst info class Node])} {
	if {[$src info class Node] && [$dst info class Console]} {
	    set node $src
	    set con $dst
	} else {
	    set node $dst
	    set con $src
	}
	if {[$con set connected]} {
	    perror "\[connect] $con is already connected"
	    return
	}
	$con set connected 1
	lappend tiptunnel_list [list $node [$con set node]]
	return
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
739
740
    set error 0
    if {! [$src info class Agent]} {
741
	perror "\[connect] $src is not an Agent."
742
	set error 1
Leigh B. Stoller's avatar
Leigh B. Stoller committed
743
744
    }
    if {! [$dst info class Agent]} {
745
	perror "\[connect] $dst is not an Agent."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
746
747
748
749
750
751
752
	set error 1
    }
    if {$error} {return}
    $src connect $dst
    $dst connect $src
}

753
754
755
756
757
758
759
# at <time> <event>
# Known events:
#   <traffic> start
#   <traffic> stop
#   <link> up
#   <link> down
#   ...
760
Simulator instproc at {time eventstring} {
761
    var_import ::GLOBALS::simulated
Timothy Stack's avatar
   
Timothy Stack committed
762
    var_import ::TBCOMPAT::hwtype_class
763
764
765
766
767
768

    # ignore at statement for simulated case
    if { $simulated == 1 } {
	return
    }

Timothy Stack's avatar
   
Timothy Stack committed
769
770
    set ptime [::GLOBALS::reltime-to-secs $time]
    if {$ptime == -1} {
771
772
773
	perror "Invalid time spec: $time"
	return
    }
Timothy Stack's avatar
   
Timothy Stack committed
774
775
    set time $ptime

776
    $self instvar event_list
777
    $self instvar event_count
778

779
    if {$event_count > 14000} {
780
781
782
	perror "Too many events in your NS file!"
	exit 1
    }
783
    set eventlist [split $eventstring ";"]
784
    
Timothy Stack's avatar
   
Timothy Stack committed
785
786
787
788
789
790
    foreach event $eventlist {
	set rc [$self make_event "sim" $event]

	if {$rc != {}} {
	    set event_count [expr $event_count + 1]
	    lappend event_list [linsert $rc 0 $time]
791
792
793
794
	}
    }
}

795
#
796
# Routing control.
797
798
799
#
Simulator instproc rtproto {type args} {
    var_import ::GLOBALS::default_ip_routing_type
800
801
802
803
804
805
    var_import ::GLOBALS::simulated

    # ignore at statement for simulated case
    if { $simulated == 1 } {
	return
    }
806
807
808
809
810
811
812

    if {$args != {}} {
	punsup "rtproto: arguments ignored: $args"
    }

    if {($type == "Session") ||	($type == "ospf")} {
	set default_ip_routing_type "ospf"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
813
814
    } elseif {($type == "Manual")} {
	set default_ip_routing_type "manual"
815
816
    } elseif {($type == "Static")} {
	set default_ip_routing_type "static"
817
818
    } elseif {($type == "Static-ddijk")} {
	set default_ip_routing_type "static-ddijk"
819
820
    } elseif {($type == "Static-old")} {
	set default_ip_routing_type "static-old"
821
822
823
824
825
826
    } else {
	punsup "rtproto: unsupported routing protocol ignored: $type"
	return
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
827
828
829
830
831
832
833
# unknown 
# This is invoked whenever any method is called on the simulator
# object that is not defined.  We interpret such a call to be a
# request to create an object of that type.  We create display an
# unsupported message and create a NullClass to fulfill the request.
Simulator instproc unknown {m args} {
    $self instvar id_counter
834
    punsup "Object $m"
835
    NullClass tbnull-null[incr id_counter] $m
Leigh B. Stoller's avatar
Leigh B. Stoller committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
}

# rename_* <old> <new>
# The following two procedures handle when an object is being renamed.
# They update the internal datastructures to reflect the new name.
Simulator instproc rename_lanlink {old new} {
    $self instvar lanlink_list
    $self instvar link_map

    unset lanlink_list($old)
    set lanlink_list($new) {}

    # In the case of a link we need to update the link_map as well.
    if {[$new info class] == "Link"} {
	$new instvar nodelist
	set src [lindex [lindex $nodelist 0] 0]
	set dst [lindex [lindex $nodelist 1] 0]
	set link_map($src:$dst) $new
	set link_map($dst:$src) $new
    }
}
Simulator instproc rename_node {old new} {
    $self instvar node_list
859
860
861
862
863
864

    # simulated nodes won't exist in the node_list
    if { [info exists node_list($old)] } {
	unset node_list($old)
	set node_list($new) {}
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
865
866
}

867
868
869
870
871
872
Simulator instproc rename_program {old new} {
    $self instvar prog_list
    unset prog_list($old)
    set prog_list($new) {}
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
873
874
875
876
877
878
Simulator instproc rename_eventgroup {old new} {
    $self instvar eventgroup_list
    unset eventgroup_list($old)
    set eventgroup_list($new) {}
}

879
880
881
882
883
884
Simulator instproc rename_firewall {old new} {
    $self instvar firewall_list
    unset firewall_list($old)
    set firewall_list($new) {}
}

Timothy Stack's avatar
   
Timothy Stack committed
885
886
887
888
889
890
891
892
893
894
895
896
Simulator instproc rename_timeline {old new} {
    $self instvar timeline_list
    unset timeline_list($old)
    set timeline_list($new) {}
}

Simulator instproc rename_sequence {old new} {
    $self instvar sequence_list
    unset sequence_list($old)
    set sequence_list($new) {}
}

Timothy Stack's avatar
   
Timothy Stack committed
897
898
899
900
901
902
Simulator instproc rename_console {old new} {
    $self instvar console_list
    unset console_list($old)
    set console_list($new) {}
}

Timothy Stack's avatar
   
Timothy Stack committed
903
904
905
906
907
908
Simulator instproc rename_topography {old new} {
    $self instvar topography_list
    unset topography_list($old)
    set topography_list($new) {}
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
909
910
911
912
913
914
915
916
917
918
919
920
# find_link <node1> <node2>
# This is just an accesor to the link_map datastructure.  If no
# link is known between <node1> and <node2> the empty list is returned.
Simulator instproc find_link {src dst} {
    $self instvar link_map
    if {[info exists link_map($src:$dst)]} {
	return $link_map($src:$dst)
    } else {
	return ""
    }
}

921
922
Simulator instproc link {src dst} {
    set reallink [$self find_link $src $dst]
923
924
925
926
927
928
929
930
931
932
933
	
    if {$src == [$reallink set src_node]} {
	set dir "to"
    } else {
	set dir "from"
    }
    
    var_import GLOBALS::new_counter
    set name sl[incr new_counter]
    
    return [SimplexLink $name $reallink $dir]
934
935
}

936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
Simulator instproc lanlink {lan node} {
    if {[$node info class] != "Node"} {
	perror "\[lanlink] $node is not a node."
	return
    }
    if {[$lan info class] != "Lan"} {
	perror "\[lanlink] $lan is not a lan."
	return
    }
    set port [$lan get_port $node]
    if {$port == {}} {
	perror "\[lanlink] $node is not in $lan."
	return
    }
    var_import GLOBALS::new_counter
    set name ll[incr new_counter]
    
    return [LLink $name $lan $node]
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
956
957
958
# get_subnet
# This is called by lanlinks.  When called get_subnet will find an available
# IP subnet, mark it as used, and return it to the caller.
959
Simulator instproc get_subnet {netmask} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
960
961
962
    $self instvar subnet_base
    $self instvar subnets

963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
    set netmaskint [inet_atohl $netmask]

    set A $subnet_base
    set C [expr ($netmaskint >> 8) & 0xff]
    set D [expr $netmaskint & 0xff]
    set minB 1
    set maxB 254
    set minC 0
    set maxC 1
    set incC 1
    set minD 0
    set maxD 1
    set incD 1

    # allow for 10. or 192.168. I know, could be more general.
    if {[expr [llength [split $subnet_base .]]] == 2} {
	# 192.168.
	set A    [expr [lindex [split $subnet_base .] 0]]
	set minB [expr [lindex [split $subnet_base .] 1]]
	set maxB [expr $minB + 1]
    }
    if {$C != 0} {	
	set minC [expr 256 - $C]
	set maxC 255
	set incC $minC
    } 
    if {$D != 0} {	
	set minD [expr 256 - $D]
	set maxD 255
	set incD $minD
    }

    # We never let the user change the second octet. See tb-set-netmask.
    for {set i $minB} {$i < $maxB} {incr i} {
	for {set j $minC} {$j < $maxC} {set j [expr $j + $incC]} {
	    for {set k $minD} {$k < $maxD} {set k [expr $k + $incD]} {
		set subnet "$A.$i.$j.$k"
		set subnetint [inet_atohl $subnet]

		# No such subnet exists?
		if {! [info exists subnets($subnetint)]} {
		    set okay 1

		    #
		    # See if this subnet violates any existing subnet masks
		    # Is this overly restrictive? Totally wrong?
		    #
		    foreach osubnetint [concat [array names subnets]] {
			set onetmaskint $subnets($osubnetint)

			if {[expr $subnetint & $onetmaskint] == $osubnetint ||
 			    [expr $osubnetint & $netmaskint] == $subnetint} {
			    set okay 0
			    break
			}
		    }
		    if {$okay} {
			$self use_subnet $subnet $netmask
			return $subnet
		    }
		}
	    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1025
1026
	}
    }
1027
    perror "Ran out of subnets."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1028
1029
}

1030
1031
1032
1033
# get_subnet_remote
# This is called by lanlinks.  When called get_subnet will find an available
# IP subnet, mark it as used, and return it to the caller.
Simulator instproc get_subnet_remote {} {
1034
    $self instvar wa_subnet_counter
1035

1036
1037
1038
1039
    if {$wa_subnet_counter > 255} {
	perror "Ran out of widearea subnets."
	return 0
    }
1040
1041
    set subnet $wa_subnet_counter
    incr wa_subnet_counter
1042
    return "69.69.$subnet.0"
1043
1044
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1045
1046
1047
# use_subnet
# This is called by the ip method of nodes.  It marks the passed subnet
# as used and thus should never be returned by get_subnet.
1048
Simulator instproc use_subnet {subnet netmask} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1049
    $self instvar subnets
1050
1051
1052
1053
1054

    set subnetint [inet_atohl $subnet]
    set netmaskint [inet_atohl $netmask]
    
    set subnets($subnetint) $netmaskint
1055
}
1056

1057
1058
1059
# add_program
# Link to a new program object.
Simulator instproc add_program {prog} {
1060
    $self instvar prog_list
1061
    set prog_list($prog) {}
1062
}
1063

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1064
1065
1066
1067
1068
1069
1070
# add_eventgroup
# Link to a EventGroup object.
Simulator instproc add_eventgroup {group} {
    $self instvar eventgroup_list
    set eventgroup_list($group) {}
}

Timothy Stack's avatar
   
Timothy Stack committed
1071
1072
1073
1074
1075
1076
1077
# add_console
# Link to a Console object.
Simulator instproc add_console {console} {
    $self instvar console_list
    set console_list($console) {}
}

1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
# add_firewall
# Link to a Firewall object.
Simulator instproc add_firewall {fw} {
    $self instvar firewall_list

    if {[array size firewall_list] > 0} {
	perror "\[add_firewall]: only one firewall per experiment right now"
	return -1
    }

    set firewall_list($fw) {}
    return 0
}

Timothy Stack's avatar
   
Timothy Stack committed
1092
1093
1094
1095
1096
1097
1098
1099
Simulator instproc add_topography {tg} {
    $self instvar topography_list

    set topography_list($tg) {}

    return 0
}

1100
Simulator instproc define-template-parameter {name value} {
1101
    $self instvar parameter_list
1102
    var_import ::TBCOMPAT::parameter_list_defaults
1103

1104
1105
    if {[info exists parameter_list_defaults($name)]} {
	set value $parameter_list_defaults($name)
1106
    }
1107
1108
1109
1110
1111
    set parameter_list($name) $value
    
    # And install the name/value in the outer environment.
    uplevel 1 real_set \{$name\} \{$value\}
    
1112
1113
1114
    return 0
}

Timothy Stack's avatar
   
Timothy Stack committed
1115
1116
Simulator instproc make_event {outer event} {
    var_import ::GLOBALS::simulated
Timothy Stack's avatar
   
Timothy Stack committed
1117
    var_import ::TBCOMPAT::osids
Timothy Stack's avatar
   
Timothy Stack committed
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
    var_import ::TBCOMPAT::hwtype_class

    set obj [lindex $event 0]
    set cmd [lindex $event 1]
    set evargs [lrange $event 2 end]
    set vnode "*"
    set vname ""
    set otype {}
    set etype {}
    set args {}
    set atstring ""

Timothy Stack's avatar
   
Timothy Stack committed
1130
    if {[string index $obj 0] == "#"} {
Timothy Stack's avatar
   
Timothy Stack committed
1131
1132
1133
	return {}
    }

1134
1135
1136
1137
1138
    if {$cmd == {}} {
	perror "Missing event type for $obj"
	return
    }

Timothy Stack's avatar
   
Timothy Stack committed
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
    if {[$obj info class] == "EventGroup"} {
	set cl [$obj set mytype]
    } else {
	set cl [$obj info class]
    }

    switch -- $cl {
	"Application/Traffic/CBR" {
	    set otype TRAFGEN
	    switch -- $cmd {
		"start" {
		    set etype START
		}
		"stop" {
		    set etype STOP
		}
		"reset" {
		    set etype RESET
		}
		"set" {
		    if {[llength $event] < 4} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    set etype MODIFY
		    set arg [lindex $event 3]
		    switch -- [lindex $event 2] {
			"packetSize_" {
			    set args  "PACKETSIZE=$arg"
			}
			"rate_" {
			    set bw [parse_bw $arg]
			    set args  "RATE=$bw"
			}
			"interval_" {
			    set args  "INTERVAL=$arg"
			}
			"iptos_" {
			    set args  "IPTOS=$arg"
			}
			unknown {
			    punsup "at $time $event"
			    return
			}
		    }
		}
		unknown {
		    punsup "at $time $event"
		    return
		}
	    }
	    set vnode [$obj get_node]
	    set vname $obj
	}
	"Agent/TCP/FullTcp" -
	"Agent/TCP/FullTcp/Reno" -
	"Agent/TCP/FullTcp/Newreno" -
	"Agent/TCP/FullTcp/Tahoe" -
	"Agent/TCP/FullTcp/Sack" - 
	"Application/FTP" -
	"Application/Telnet" {
	    # For events sent to NSE, we don't distinguish
	    # between START, STOP and MODIFY coz the entire
	    # string passed to '$ns at' is sent for evaluation to the node
	    # on which NSE is running: fix needed for the
	    # case when the above string has syntax errors. Maybe
	    # just have a way reporting errors back to the
	    # the user from the NSE that finds the syntax errors
	    set otype NSE
	    set etype NSEEVENT
	    set args "\$$obj $cmd [lrange $event 2 end]"
	    set vnode [$obj get_node]
	    set vname $obj
	}
	"EventSequence" {
	    set otype SEQUENCE
	    switch -- $cmd {
		"start" {
		    set etype START
		}
		"run" {
Timothy Stack's avatar
   
Timothy Stack committed
1220
		    set etype RUN
Timothy Stack's avatar
   
Timothy Stack committed
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
		}
		"reset" {
		    set etype RESET
		}
		unknown {
		    punsup "$obj $cmd $evargs"
		    return
		}
	    }
	    set vnode {}
	    set vname $obj
	}
	"EventTimeline" {
	    set otype TIMELINE
	    switch -- $cmd {
		"start" {
		    set etype START
		}
		"run" {
Timothy Stack's avatar
   
Timothy Stack committed
1240
		    set etype RUN
Timothy Stack's avatar
   
Timothy Stack committed
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
		}
		"reset" {
		    set etype RESET
		}
		unknown {
		    punsup "$obj $cmd $evargs"
		    return
		}
	    }
	    set vnode {}
	    set vname $obj
	}
	"Link" -
	"Lan" {
	    set otype LINK
1256
1257
1258
	    set vnode {}
	    set vname $obj
	    
Timothy Stack's avatar
   
Timothy Stack committed
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
	    switch -- $cmd {
		"up"	    {set etype UP}
		"down"	    {set etype DOWN}
		"bandwidth" {
		    if {[llength $event] < 4} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    set arg   [lindex $event 2]
		    set bw [parse_bw $arg]
		    set args  "BANDWIDTH=$bw"
		    set etype MODIFY
		}
		"delay" {
		    if {[llength $event] < 3} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    set arg   [lindex $event 2]
		    set args  "DELAY=$arg"
		    set etype MODIFY
		}
		"plr" {
		    if {[llength $event] < 3} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    if {[scan [lindex $event 2] "%f" plr] != 1 ||
		    $plr < 0 || $plr > 1} {
			perror "Improper argument: at $time $event"
			return
		    }
		    set args  "PLR=$plr"
		    set etype MODIFY
		}
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
		"trace" {
		    set otype LINKTRACE
		    set vname "${obj}-tracemon"

		    if {[llength $event] < 3} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    set action [lindex $event 2]

		    switch -- $action {
1305
1306
			"stop"	    {set etype STOP}
			"start"     {set etype START}
1307
1308
			"kill"	    {set etype KILL}
			"snapshot"  {set etype SNAPSHOT}
1309
1310
1311
1312
			unknown {
			    punsup "at $time $event"
			    return
			}
1313
1314
		    }
		}
Timothy Stack's avatar
   
Timothy Stack committed
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
		unknown {
		    punsup "at $time $event"
		    return
		}
	    }
	    $obj mustdelay
	}
	"Node" {
	    set otype NODE
	    switch -- $cmd {
		"reboot" {
		    set etype REBOOT
		}
		"snapshot-to" {
		    set etype SNAPSHOT
		    if {[llength $evargs] < 1} {
			perror "Wrong number of arguments: $obj $cmd $evargs"
			return
		    }
		    set image [lindex $evargs 0]
Timothy Stack's avatar
   
Timothy Stack committed
1335
1336
1337
1338
1339
1340
		    if {! ${GLOBALS::anonymous} && ! ${GLOBALS::passmode}} {
			if {![info exists osids($image)]} {
			    perror "Unknown image in snapshot-to event: $image"
			    return
			}
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1341
1342
1343
1344
1345
1346
1347
1348
		    set args "IMAGE=${image}"
		}
		"reload" {
		    set etype RELOAD
		    ::GLOBALS::named-args $evargs {
			-image {}
		    }
		    if {$(-image) != {}} {
Timothy Stack's avatar
   
Timothy Stack committed
1349
1350
			if {! ${GLOBALS::anonymous} && 
			    ! ${GLOBALS::passmode} &&
1351
			    ! [info exists osids($(-image))]} {
Timothy Stack's avatar
   
Timothy Stack committed
1352
1353
1354
			    perror "Unknown image in reload event: $(-image)"
			    return
			}
Timothy Stack's avatar
   
Timothy Stack committed
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
			set args "IMAGE=$(-image)"
		    }
		}
		"setdest" {
		    set etype SETDEST
		    set topo [$obj set topo]
		    if {$topo == ""} {
			perror "$obj is not located on a topography"
			return
		    }
		    if {[llength $evargs] < 3} {
			perror "Wrong number of arguments: $obj $cmd $evargs; expecting - <obj> setdest <x> <y> <speed>"
			return
		    }
		    set x [lindex $evargs 0]
		    set y [lindex $evargs 1]
Timothy Stack's avatar
   
Timothy Stack committed
1371
		    if {! [$topo checkdest $self $x $y -showerror 1]} {
Timothy Stack's avatar
   
Timothy Stack committed
1372
1373
			return
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1374
		    set speed [lindex $evargs 2]
Timothy Stack's avatar
   
Timothy Stack committed
1375
1376
		    if {$speed != 0.0 && ($speed < 0.1) && ($speed > 0.4)} {
			perror "Speed is currently locked at 0.0 or 0.1-0.4"
Timothy Stack's avatar
   
Timothy Stack committed
1377
1378
1379
1380
1381
			return
		    }
		    ::GLOBALS::named-args [lrange $evargs 3 end] {
			-orientation 0
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1382
		    set args "X=$x Y=$y SPEED=$speed ORIENTATION=$(-orientation)"
Timothy Stack's avatar
   
Timothy Stack committed
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
		}
		unknown {
		    punsup "$obj $cmd $evargs"
		    return
		}
	    }
	    set vnode {}
	    set vname $obj
	}
	"Queue" {
	    set otype LINK
	    set node [$obj get_node]
	    set lanlink [$obj get_link]
1396
1397
	    set vnode {}
	    set vname "$lanlink-$node"
Timothy Stack's avatar
   
Timothy Stack committed
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
	    $lanlink mustdelay
	    switch -- $cmd {
		"set" {
		    if {[llength $event] < 4} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    set etype MODIFY
		    set arg [lindex $event 3]
		    switch -- [lindex $event 2] {
			"queue-in-bytes_" {
			    set args  "QUEUE-IN-BYTES=$arg"
			}
			"limit_" {
			    set args  "LIMIT=$arg"
			}
			"maxthresh_" {
			    set args  "MAXTHRESH=$arg"
			}
			"thresh_" {
			    set args  "THRESH=$arg"
			}
			"linterm_" {
			    set args  "LINTERM=$arg"
			}
			"q_weight_" {
			    if {[scan $arg "%f" w] != 1} {
				perror "Improper argument: at $time $event"
				return
			    }
			    set args  "Q_WEIGHT=$w"
			}
			unknown {
			    punsup "at $time $event"
			    return
			}
		    }
		}
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
		"trace" {
		    set otype LINKTRACE
		    set vname "${vname}-tracemon"

		    if {[llength $event] < 3} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    set action [lindex $event 2]

		    switch -- $action {
1447
1448
			"stop"	    {set etype STOP}
			"start"     {set etype START}
1449
1450
			"kill"	    {set etype KILL}
			"snapshot"  {set etype SNAPSHOT}
1451
1452
1453
1454
			unknown {
			    punsup "at $time $event"
			    return
			}
1455
1456
		    }
		}
Timothy Stack's avatar
   
Timothy Stack committed
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
		unknown {
		    punsup "at $time $event"
		    return
		}
	    }
	}
	"Program" {
	    set otype PROGRAM
	    set vname $obj
	    if {[$obj info class] == "EventGroup"} {
		set vnode "*"
	    } else {
		set vnode [$obj set node]
	    }
	    
	    switch -- $cmd {
Timothy Stack's avatar
   
Timothy Stack committed
1473
		"set" -
Timothy Stack's avatar
   
Timothy Stack committed
1474
1475
		"run" -
		"start" {
Timothy Stack's avatar
   
Timothy Stack committed
1476
1477
1478
1479
1480
1481
1482
		    switch -- $cmd {
			"set" {
			    set etype MODIFY
			}
			"run" {
			    set etype RUN
			}
Timothy Stack's avatar
   
Timothy Stack committed
1483
1484
1485
			"start" {
			    set etype START
			}
Timothy Stack's avatar
   
Timothy Stack committed
1486
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1487
1488
1489
1490
1491
		    if {[$obj info class] == "EventGroup"} {
			set default_command {}
		    } else {
			set default_command [$obj set command]
		    }
1492
1493
1494
1495
1496
1497
1498
		    ::GLOBALS::named-args $evargs [list \
			-command $default_command \
			-dir {} \
			-timeout {} \
			-expected-exit-code {} \
			-tag {} \
		    ]
Timothy Stack's avatar
   
Timothy Stack committed
1499
1500
1501
		    if {$(-dir) != {}} {
			set args "DIR={$(-dir)} "
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1502
1503
1504
1505
1506
1507
		    if {$(-expected-exit-code) != {}} {
			set args "${args}EXPECTED_EXIT_CODE=$(-expected-exit-code) "
		    }
		    if {$(-tag) != {}} {
			set args "${args}TAG=$(-tag) "
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1508
1509
1510
1511
1512
1513
1514
1515
1516
		    if {$(-timeout) != {}} {
			set to [::GLOBALS::reltime-to-secs $(-timeout)]
			if {$to == -1} {
			    perror "-timeout value is not a relative time: $(-timeout)"
			    return
			} else {
			    set args "${args}TIMEOUT={$to} "
			}
		    }
1517
1518
1519
		    # Put the command last so the program-agent can assume everything
		    # up to the end of the string is part of the command and we don't
		    # have to deal with quoting...  XXX
1520
1521
1522
		    if {$(-command) != {}} {
			set args "${args}COMMAND=$(-command)"
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
		}
		"stop" {
		    set etype STOP
		}
		"kill" {
		    set etype KILL
		    if {[llength $event] < 3} {
			perror "Wrong number of arguments: at $time $event"
			return
		    }
		    set arg [lindex $event 2]
		    set args "SIGNAL=$arg"
		}
		unknown {
		    punsup "$obj $cmd $args"
		    return
		}
	    }
	}
Timothy Stack's avatar
   
Timothy Stack committed
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
	"Console" {
	    set otype CONSOLE
	    set vname $obj

	    switch -- $cmd {
		"start" {
		    set etype START
		}
		"stop" {
		    set etype STOP
		    if {[llength $event] < 3} {
			perror "Wrong number of arguments: $obj $cmd $evargs"
			return
		    }
		    set arg [lindex $event 2]
		    set args "FILE=$arg"
		}
	    }
	}
Timothy Stack's avatar
   
Timothy Stack committed
1561
1562
1563
1564
1565
1566
1567
1568