sim.tcl.in 40.8 KB
Newer Older
1
# -*- tcl -*-
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
Timothy Stack's avatar
   
Timothy Stack committed
4
# Copyright (c) 2000-2005 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
    var_import ::GLOBALS::last_class
    set last_class $self
Timothy Stack's avatar
   
Timothy Stack committed
92
93
94
95

    $self instvar new_node_config;
    array set new_node_config {}
    $self node-config
96
97
98
99
100
101
}

# 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
102
103
}

Timothy Stack's avatar
   
Timothy Stack committed
104
105
106
107
108
109
110
111
112
113
114
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
115
116
117
118
119
# 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
120
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
121
122
123
    $self instvar id_counter
    $self instvar node_list

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

Shashi Guruprasad's avatar
Shashi Guruprasad committed
131
132
133
    # simulated nodes have type 'sim'
    if { $simulated == 1 } {
        tb-set-hardware $curnode sim
134
135
136
137
	# 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
138
    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
139
    set node_list($curnode) {}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
140
141
    set last_class $curnode

Timothy Stack's avatar
   
Timothy Stack committed
142
143
144
    $self instvar new_node_config;
    $curnode topography $new_node_config(-topography)

Leigh B. Stoller's avatar
Leigh B. Stoller committed
145
146
147
148
149
150
151
152
153
    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
154
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
155
156
157
158
    $self instvar id_counter
    $self instvar lanlink_list
    $self instvar link_map

159
160
    if {($args != {})} {
	punsup "Arguments for duplex-link: $args"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
161
162
163
    }
    set error 0
    if {! [$n1 info class Node]} {
164
	perror "\[duplex-link] $n1 is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
165
166
167
	set error 1
    }
    if {! [$n2 info class Node]} {
168
	perror "\[duplex-link] $n2 is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
169
170
	set error 1
    }
171
172
173
174
#    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
175
176
177
178
179
180
181
182

    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
183
184
185
186
187
188
    if {$error} {return}

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

189
    set curlink tblink-l[incr id_counter]
190

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
196
197
198
199
200
201
202
203
204
205
    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
206
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
207
208
209
    $self instvar id_counter
    $self instvar lanlink_list

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

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
    #
    # 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
232
233
234
235
236
237
    # 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 } {
238

Shashi Guruprasad's avatar
Shashi Guruprasad committed
239
240
241
242
243
244
245
	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 ""
	    }
246
247
	}
    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
248
249
250
251
252
253
254
255
256
257
258
259

    set curlan tblan-lan[incr id_counter]
    
    # Convert bandwidth and delay
    set rbw [parse_bw $bw]
    set rdelay [parse_delay $delay]
    
    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
260
261
}

Timothy Stack's avatar
   
Timothy Stack committed
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
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
294
295
296
297
298
299
	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
300
301
302
303
304
305
	set sequence_list($curseq) {}
    }

    return $curseq
}

Timothy Stack's avatar
   
Timothy Stack committed
306
Simulator instproc event-group {{list {}}} {
Timothy Stack's avatar
   
Timothy Stack committed
307
    set curgrp [new EventGroup $self]
Timothy Stack's avatar
   
Timothy Stack committed
308
309
    if {$list != {}} {
	foreach obj $list {
Timothy Stack's avatar
   
Timothy Stack committed
310
311
312
313
314
315
316
	    $curgrp add $obj
	}
    }

    return $curgrp
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
317
318
319
320
321
322
323
324
# 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
325
    $self instvar event_list
326
    $self instvar prog_list
Leigh B. Stoller's avatar
Leigh B. Stoller committed
327
    $self instvar eventgroup_list
328
    $self instvar firewall_list
Timothy Stack's avatar
   
Timothy Stack committed
329
330
    $self instvar timeline_list
    $self instvar sequence_list
Timothy Stack's avatar
   
Timothy Stack committed
331
332
    $self instvar console_list
    $self instvar tiptunnel_list
Timothy Stack's avatar
   
Timothy Stack committed
333
    $self instvar topography_list
Shashi Guruprasad's avatar
Shashi Guruprasad committed
334
335
    $self instvar simulated
    $self instvar nseconfig
336
337
    var_import ::GLOBALS::pid
    var_import ::GLOBALS::eid
Leigh B. Stoller's avatar
Leigh B. Stoller committed
338
339
340
341
    var_import ::GLOBALS::errors
    var_import ::GLOBALS::irfile
    var_import ::GLOBALS::ran
    var_import ::GLOBALS::impotent
342
    var_import ::GLOBALS::passmode
343
    var_import ::GLOBALS::vtypes
344
    var_import ::GLOBALS::uselatestwadata
345
    var_import ::GLOBALS::usewatunnels
346
347
348
    var_import ::GLOBALS::wa_delay_solverweight
    var_import ::GLOBALS::wa_bw_solverweight
    var_import ::GLOBALS::wa_plr_solverweight
349
350
    var_import ::GLOBALS::uselinkdelays
    var_import ::GLOBALS::forcelinkdelays
351
    var_import ::GLOBALS::multiplex_factor
352
    var_import ::GLOBALS::sync_server
353
354
    var_import ::GLOBALS::use_ipassign
    var_import ::GLOBALS::ipassign_args
355
356
    var_import ::GLOBALS::cpu_usage
    var_import ::GLOBALS::mem_usage
357
    var_import ::GLOBALS::fix_current_resources
358
    var_import ::GLOBALS::veth_encapsulate
359
360
    var_import ::GLOBALS::jail_osname
    var_import ::GLOBALS::delay_osname
361
    var_import ::GLOBALS::delay_capacity
362
363
    var_import ::TBCOMPAT::objtypes
    var_import ::TBCOMPAT::eventtypes
364
365
    var_import ::GLOBALS::modelnet_cores
    var_import ::GLOBALS::modelnet_edges
366
    var_import ::GLOBALS::elab_in_elab
367
    var_import ::GLOBALS::elabinelab_topo
368
    var_import ::GLOBALS::elabinelab_eid
369
    var_import ::GLOBALS::elabinelab_cvstag
370
    var_import ::GLOBALS::security_level
371
    
372
373
374
375
376
    if {$ran == 1} {
	perror "The Simulator 'run' statement can only be run once."
	return
    }

377
    if {$elab_in_elab && [llength [array names node_list]] == 0} {
378
379
380
381
382
383
	if {$elabinelab_topo == ""} {
	    set nsfilename "elabinelab.ns"
	} else {
	    set nsfilename "elabinelab-${elabinelab_topo}.ns"
	}
	uplevel 1 source "@prefix@/lib/ns2ir/${nsfilename}"
384
    }
385
386
387
    if {$security_level} {
	uplevel 1 source "@prefix@/lib/ns2ir/fw.ns"
    }
388

Leigh B. Stoller's avatar
Leigh B. Stoller committed
389
    # Fill out IPs
390
391
392
393
    if {! $use_ipassign } {
	foreach obj [concat [array names lanlink_list]] {
	    $obj fill_ips
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
394
395
    }

396
397
398
399
400
401
402
403
404
405
    # 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
	}
    }

406
407
    # Default sync server.
    set default_sync_server {}
408

Leigh B. Stoller's avatar
Leigh B. Stoller committed
409
410
411
412
413
414
    # 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]} {
415
	    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
416
417
418
419
	}
    }
    foreach lan [lsort [array names lanlink_list]] {
	if {! [regexp {^[-0-9A-Za-z]+$} $lan]} {
420
	    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
421
422
423
	}
    }

424
425
426
427
428
429
430
    # 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
431
432
433
    # If any errors occur stop here.
    if {$errors == 1} {return}

434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
    # 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
    }

454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
    # 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
473
    # If we are running in impotent mode we stop here
474
    if {$impotent == 1 && $passmode == 0} {return}
475
    
476
477
    $self spitxml_init

Shashi Guruprasad's avatar
Shashi Guruprasad committed
478
479
480
481
482
483
484
485
486
487
488
489
    # 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
490

491
	$self spitxml_data "nseconfigs" [list "vname" "nseconfig" ] [list fullsim $nsecfg_script ]
Shashi Guruprasad's avatar
Shashi Guruprasad committed
492
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
493
494
495
    
    # Update the DB
    foreach node [lsort [array names node_list]] {
496
	$node updatedb "sql"
497

Timothy Stack's avatar
   
Timothy Stack committed
498
	if { $default_sync_server == {} && ![$node set issubnode] } {
499
500
	    set default_sync_server $node
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
501
    }
502
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
503
    foreach lan [concat [array names lanlink_list]] {
504
	$lan updatedb "sql"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
505
    }
506
    foreach vtype [array names vtypes] {
507
	$vtype updatedb "sql"
508
    }
509
    foreach prog [array names prog_list] {
510
	$prog updatedb "sql"
511
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
512
513
514
    foreach egroup [array names eventgroup_list] {
	$egroup updatedb "sql"
    }
515
516
517
    foreach fw [array names firewall_list] {
	$fw updatedb "sql"
    }
Timothy Stack's avatar
   
Timothy Stack committed
518
519
520
521
522
523
    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
524
525
526
527
528
529
    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
530
531
532
    foreach tg [array names topography_list] {
	$tg updatedb "sql"
    }
533

534
535
    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]
536

537
    if { $multiplex_factor != {} } {
538
539
	lappend fields "multiplex_factor"
	lappend values $multiplex_factor
540
    }
541
    
542
543
544
    if { $sync_server != {} } {
	lappend fields "sync_server"
	lappend values $sync_server
545
546
547
    } elseif { $default_sync_server != {} } {
	lappend fields "sync_server"
	lappend values $default_sync_server
548
    }
549

550
551
552
553
554
555
556
557
    lappend fields "use_ipassign"
    lappend values $use_ipassign

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

558
559
560
561
562
563
564
565
    if { $jail_osname != {} } {
	lappend fields "jail_osname"
	lappend values $jail_osname
    }
    if { $delay_osname != {} } {
	lappend fields "delay_osname"
	lappend values $delay_osname
    }
566
567
568
569
    if { $delay_capacity != {} } {
	lappend fields "delay_capacity"
	lappend values $delay_capacity
    }
570
571
572
573
574
575
576
577
578

    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
    }
579
    
580
581
582
    if {$elab_in_elab} {
	lappend fields "elab_in_elab"
	lappend values 1
583
584
585
586
587

	if { $elabinelab_eid != {} } {
	    lappend fields "elabinelab_eid"
	    lappend values $elabinelab_eid
	}
588
589
590
591
592
	
	if { $elabinelab_cvstag != {} } {
	    lappend fields "elabinelab_cvstag"
	    lappend values $elabinelab_cvstag
	}
593
594
    }
    
595
596
597
598
599
    if {$security_level} {
	lappend fields "security_level"
	lappend values $security_level
    }
    
600
    $self spitxml_data "experiments" $fields $values
601
602

    # This could probably be elsewhere.
603
    $self spitxml_data "virt_agents" [list "vnode" "vname" "objecttype" ] [list "*" $self $objtypes(SIMULATOR) ]
604
605
606

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

    $self spitxml_data "virt_agents" [list "vnode" "vname" "objecttype" ] [list "*" "slothd" $objtypes(SLOTHD) ]
609
    
Timothy Stack's avatar
   
Timothy Stack committed
610
611
612
613
614
615
    if {[array exists ::opt]} {
	foreach {oname ovalue} [array get ::opt] {
	    $self spitxml_data "virt_user_environment" [list "name" "value" ] [list "$oname" "$ovalue" ]
	}
    }

616
    foreach event $event_list {
617
	$self spitxml_data "eventlist" [list "time" "vnode" "vname" "objecttype" "eventtype" "arguments" "atstring" ] [list [lindex $event 0] [lindex $event 1] [lindex $event 2] $objtypes([lindex $event 3]) $eventtypes([lindex $event 4]) [lindex $event 5] [lindex $event 6] ]
618
    }
619
620

    $self spitxml_finish
Leigh B. Stoller's avatar
Leigh B. Stoller committed
621
622
623
624
625
}

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
628
    if {! [$agent info class Agent]} {
629
	perror "\[attach-agent] $agent is not an Agent."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
630
631
632
	return
    }
    if {! [$node info class Node]} {
633
	perror "\[attach-agent] $node is not a Node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
634
635
	return
    }
636
637
638
639
640
641
642
643
644
645

    # 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 ""
    }

646
    $node attach-agent $agent
Leigh B. Stoller's avatar
Leigh B. Stoller committed
647
648
}

649
Simulator instproc agentinit {agent} {
650
651
    var_import ::TBCOMPAT::objtypes
    var_import ::TBCOMPAT::eventtypes
652
653

    if {[$agent info class Application/Traffic/CBR]} {
654
	$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"]
655
656
657
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
658
659
660
# connect <src> <dst>
# Connects two agents together.
Simulator instproc connect {src dst} {
Timothy Stack's avatar
   
Timothy Stack committed
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
    $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
680
681
    set error 0
    if {! [$src info class Agent]} {
682
	perror "\[connect] $src is not an Agent."
683
	set error 1
Leigh B. Stoller's avatar
Leigh B. Stoller committed
684
685
    }
    if {! [$dst info class Agent]} {
686
	perror "\[connect] $dst is not an Agent."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
687
688
689
690
691
692
693
	set error 1
    }
    if {$error} {return}
    $src connect $dst
    $dst connect $src
}

694
695
696
697
698
699
700
# at <time> <event>
# Known events:
#   <traffic> start
#   <traffic> stop
#   <link> up
#   <link> down
#   ...
701
Simulator instproc at {time eventstring} {
702
    var_import ::GLOBALS::simulated
Timothy Stack's avatar
   
Timothy Stack committed
703
    var_import ::TBCOMPAT::hwtype_class
704
705
706
707
708
709

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

Timothy Stack's avatar
   
Timothy Stack committed
710
711
    set ptime [::GLOBALS::reltime-to-secs $time]
    if {$ptime == -1} {
712
713
714
	perror "Invalid time spec: $time"
	return
    }
Timothy Stack's avatar
   
Timothy Stack committed
715
716
    set time $ptime

717
    $self instvar event_list
718
    $self instvar event_count
719

720
721
722
723
    if {$event_count > 4000} {
	perror "Too many events in your NS file!"
	exit 1
    }
724
    set eventlist [split $eventstring ";"]
725
    
Timothy Stack's avatar
   
Timothy Stack committed
726
727
728
729
730
731
    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]
732
733
734
735
	}
    }
}

736
#
737
# Routing control.
738
739
740
#
Simulator instproc rtproto {type args} {
    var_import ::GLOBALS::default_ip_routing_type
741
742
743
744
745
746
    var_import ::GLOBALS::simulated

    # ignore at statement for simulated case
    if { $simulated == 1 } {
	return
    }
747
748
749
750
751
752
753

    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
754
755
    } elseif {($type == "Manual")} {
	set default_ip_routing_type "manual"
756
757
    } elseif {($type == "Static")} {
	set default_ip_routing_type "static"
758
759
    } elseif {($type == "Static-ddijk")} {
	set default_ip_routing_type "static-ddijk"
760
761
    } elseif {($type == "Static-old")} {
	set default_ip_routing_type "static-old"
762
763
764
765
766
767
    } else {
	punsup "rtproto: unsupported routing protocol ignored: $type"
	return
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
768
769
770
771
772
773
774
# 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
775
    punsup "Object $m"
776
    NullClass tbnull-null[incr id_counter] $m
Leigh B. Stoller's avatar
Leigh B. Stoller committed
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
}

# 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
800
801
802
803
804
805

    # 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
806
807
}

808
809
810
811
812
813
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
814
815
816
817
818
819
Simulator instproc rename_eventgroup {old new} {
    $self instvar eventgroup_list
    unset eventgroup_list($old)
    set eventgroup_list($new) {}
}

820
821
822
823
824
825
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
826
827
828
829
830
831
832
833
834
835
836
837
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
838
839
840
841
842
843
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
844
845
846
847
848
849
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
850
851
852
853
854
855
856
857
858
859
860
861
# 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 ""
    }
}

862
863
Simulator instproc link {src dst} {
    set reallink [$self find_link $src $dst]
864
865
866
867
868
869
870
871
872
873
874
	
    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]
875
876
}

877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
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
897
898
899
# 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.
900
Simulator instproc get_subnet {netmask} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
901
902
903
    $self instvar subnet_base
    $self instvar subnets

904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
    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
966
967
	}
    }
968
    perror "Ran out of subnets."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
969
970
}

971
972
973
974
# 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 {} {
975
    $self instvar wa_subnet_counter
976

977
978
979
980
    if {$wa_subnet_counter > 255} {
	perror "Ran out of widearea subnets."
	return 0
    }
981
982
    set subnet $wa_subnet_counter
    incr wa_subnet_counter
983
    return "69.69.$subnet.0"
984
985
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
986
987
988
# 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.
989
Simulator instproc use_subnet {subnet netmask} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
990
    $self instvar subnets
991
992
993
994
995

    set subnetint [inet_atohl $subnet]
    set netmaskint [inet_atohl $netmask]
    
    set subnets($subnetint) $netmaskint
996
}
997

998
999
1000
# add_program
# Link to a new program object.
Simulator instproc add_program {prog} {
1001
    $self instvar prog_list
1002
    set prog_list($prog) {}
1003
}
1004

Leigh B. Stoller's avatar
Leigh B. Stoller committed
1005
1006
1007
1008
1009
1010
1011
# 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
1012
1013
1014
1015
1016
1017
1018
# add_console
# Link to a Console object.
Simulator instproc add_console {console} {
    $self instvar console_list
    set console_list($console) {}
}

1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
# 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
1033
1034
1035
1036
1037
1038
1039
1040
Simulator instproc add_topography {tg} {
    $self instvar topography_list

    set topography_list($tg) {}

    return 0
}

Timothy Stack's avatar
   
Timothy Stack committed
1041
1042
Simulator instproc make_event {outer event} {
    var_import ::GLOBALS::simulated
Timothy Stack's avatar
   
Timothy Stack committed
1043
    var_import ::TBCOMPAT::osids
Timothy Stack's avatar
   
Timothy Stack committed
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
    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
1056
    if {[string index $obj 0] == "#"} {
Timothy Stack's avatar
   
Timothy Stack committed
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
	return {}
    }

    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
1141
		    set etype RUN
Timothy Stack's avatar
   
Timothy Stack committed
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
		}
		"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
1161
		    set etype RUN
Timothy Stack's avatar
   
Timothy Stack committed
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
		}
		"reset" {
		    set etype RESET
		}
		unknown {
		    punsup "$obj $cmd $evargs"
		    return
		}
	    }
	    set vnode {}
	    set vname $obj
	}
	"Link" -
	"Lan" {
	    set otype LINK
1177
1178
1179
	    set vnode {}
	    set vname $obj
	    
Timothy Stack's avatar
   
Timothy Stack committed
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
	    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
		}
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
		"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 {
1226
1227
			"stop"	    {set etype STOP}
			"start"     {set etype START}
1228
1229
			"kill"	    {set etype KILL}
			"snapshot"  {set etype SNAPSHOT}
1230
1231
1232
1233
			unknown {
			    punsup "at $time $event"
			    return
			}
1234
1235
		    }
		}
Timothy Stack's avatar
   
Timothy Stack committed
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
		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
1256
1257
1258
1259
1260
1261
		    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
1262
1263
1264
1265
1266
1267
1268
1269
		    set args "IMAGE=${image}"
		}
		"reload" {
		    set etype RELOAD
		    ::GLOBALS::named-args $evargs {
			-image {}
		    }
		    if {$(-image) != {}} {
Timothy Stack's avatar
   
Timothy Stack committed
1270
1271
			if {! ${GLOBALS::anonymous} && 
			    ! ${GLOBALS::passmode} &&
1272
			    ! [info exists osids($(-image))]} {
Timothy Stack's avatar
   
Timothy Stack committed
1273
1274
1275
			    perror "Unknown image in reload event: $(-image)"
			    return
			}
Timothy Stack's avatar
   
Timothy Stack committed
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
			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
1292
		    if {! [$topo checkdest $self $x $y -showerror 1]} {
Timothy Stack's avatar
   
Timothy Stack committed
1293
1294
			return
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1295
		    set speed [lindex $evargs 2]
Timothy Stack's avatar
   
Timothy Stack committed
1296
1297
		    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
1298
1299
1300
1301
1302
			return
		    }
		    ::GLOBALS::named-args [lrange $evargs 3 end] {
			-orientation 0
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1303
		    set args "X=$x Y=$y SPEED=$speed ORIENTATION=$(-orientation)"
Timothy Stack's avatar
   
Timothy Stack committed
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
		}
		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]
1317
1318
	    set vnode {}
	    set vname "$lanlink-$node"
Timothy Stack's avatar
   
Timothy Stack committed
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
	    $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
			}
		    }
		}
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
		"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 {
1368
1369
			"stop"	    {set etype STOP}
			"start"     {set etype START}
1370
1371
			"kill"	    {set etype KILL}
			"snapshot"  {set etype SNAPSHOT}
1372
1373
1374
1375
			unknown {
			    punsup "at $time $event"
			    return
			}
1376
1377
		    }
		}
Timothy Stack's avatar
   
Timothy Stack committed
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
		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
1394
		"set" -
Timothy Stack's avatar
   
Timothy Stack committed
1395
1396
		"run" -
		"start" {
Timothy Stack's avatar
   
Timothy Stack committed
1397
1398
1399
1400
1401
1402
1403
		    switch -- $cmd {
			"set" {
			    set etype MODIFY
			}
			"run" {
			    set etype RUN
			}
Timothy Stack's avatar
   
Timothy Stack committed
1404
1405
1406
			"start" {
			    set etype START
			}
Timothy Stack's avatar
   
Timothy Stack committed
1407
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1408
1409
1410
1411
1412
		    if {[$obj info class] == "EventGroup"} {
			set default_command {}
		    } else {
			set default_command [$obj set command]
		    }
1413
1414
1415
1416
1417
1418
1419
		    ::GLOBALS::named-args $evargs [list \
			-command $default_command \
			-dir {} \
			-timeout {} \
			-expected-exit-code {} \
			-tag {} \
		    ]
Timothy Stack's avatar
   
Timothy Stack committed
1420
1421
1422
		    if {$(-dir) != {}} {
			set args "DIR={$(-dir)} "
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1423
1424
1425
1426
1427
1428
		    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
1429
1430
1431
1432
1433
1434
1435
1436
1437
		    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} "
			}
		    }
1438
1439
1440
		    if {$(-command) != {}} {
			set args "${args}COMMAND=$(-command)"
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
		}
		"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
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
	"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
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
	"Simulator" {
	    set vnode "*"
	    set vname $self
	    
	    switch -- $cmd {
		"bandwidth" {
		    set otype LINK
		    set etype MODIFY
		    set vnode {}
		    set vname {}
		}
		"halt" {
		    set otype SIMULATOR
		    set etype HALT
		}
		"terminate" {
		    set otype SIMULATOR
		    set etype HALT
		}
		"swapout" {
		    set otype SIMULATOR
		    set etype SWAPOUT
		}
		"trace-for" {
Timothy Stack's avatar
   
Timothy Stack committed
1503
		    set vname "slothd"
Timothy Stack's avatar
   
Timothy Stack committed
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
		    set otype SLOTHD
		    set etype START
		    if {[llength $event] < 3} {
			perror "Wrong number of arguments: $obj $cmd $evargs"
			return
		    }
		    set arg [lindex $event 2]
		    set args "DURATION=$arg"
		}
		"stabilize" {
		    set otype SIMULATOR
		    set etype MODIFY
		    set args "mode=stabilize"
		}
		"msg" -
		"message" {
		    set otype SIMULATOR
		    set etype MESSAGE
Timothy Stack's avatar
   
Timothy Stack committed
1522
		    set args "[join $evargs]"
Timothy Stack's avatar
   
Timothy Stack committed
1523
1524
1525
1526
		}
		"log" {
		    set otype SIMULATOR
		    set etype LOG
Timothy Stack's avatar
   
Timothy Stack committed
1527
		    set args "[join $evargs]"
Timothy Stack's avatar
   
Timothy Stack committed
1528
1529
1530
1531
		}
		"report" {
		    set otype SIMULATOR
		    set etype REPORT
Timothy Stack's avatar
   
Timothy Stack committed
1532
1533
1534
1535
1536
1537
		    ::GLOBALS::named-args $evargs {
			-digester {}
		    }
		    if {$(-digester) != {}} {
			set args "DIGESTER={$(-digester)}"
		    }
Timothy Stack's avatar
   
Timothy Stack committed
1538
		}
Timothy Stack's avatar
   
Timothy Stack committed
1539
1540
1541
1542
1543
		"cleanlogs" {
		    set otype SIMULATOR
		    set etype RESET
		    set args "ASPECT=LOGHOLE"
		}
Timothy Stack's avatar
   
Timothy Stack committed
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
		"linktest" {
		    set otype LINKTEST
		    set etype START
		    set stopat 3
		    set vname "linktest"
		    ::GLOBALS::named-args $evargs {
			-bw 0
		    }
		    if {$(-bw) != 0} {
			set stopat 4
		    }
		    set args "STARTAT=1 STOPAT=$stopat"
		}
Timothy Stack's avatar
   
Timothy Stack committed
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
		unknown {
		    punsup "$obj $cmd $evargs"
		    return
		}
	    }
	}
	unknown {
	    punsup "Unknown object type: $obj $cmd $evargs"
	    return
	}
    }

    return [list $vnode $vname $otype $etype $args $atstring]
}

1572
1573
1574
1575
1576
1577
# cost
# Set the cost for a link
Simulator instproc cost {src dst c} {
    set reallink [$self find_link $src $dst]
    $reallink set cost([list $src [$reallink get_port $src]]) $c
}
Shashi Guruprasad's avatar
Shashi Guruprasad committed
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600

# Now we have an experiment wide 
# simulation specification. Virtual to physical
# mapping will be done in later stages
Simulator instproc make-simulated {args} {

    var_import ::GLOBALS::simulated
    $self instvar nseconfig
    $self instvar simcode_present

    set simulated 1
    global script
    set script [string trim $args "\{\}"]

    if { $script == {} } {
        set simulated 0
        return
    }

    set simcode_present 1

    # we ignore any type of errors coz they have
    # been caught when we ran the script through NSE
1601
    uplevel 1 $script
Shashi Guruprasad's avatar
Shashi Guruprasad committed
1602
1603

    append nseconfig $script
1604
    append nseconfig \n
Shashi Guruprasad's avatar
Shashi Guruprasad committed
1605
1606
1607

    set simulated 0
}
1608
1609
1610
1611
1612
1613
1614
1615

#
# Spit out XML
#
Simulator instproc spitxml_init {} {
    var_import ::GLOBALS::pid
    var_import ::GLOBALS::eid

1616
1617
1618
    # Add a marker so xmlconvert can tell where user output stops and
    puts "#### BEGIN XML ####"
    # ... XML starts.
1619
1620
1621
1622
1623
1624
1625
1626
1627
    puts "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
    puts "<virtual_experiment pid='$pid' eid='$eid'>"
}

Simulator instproc spitxml_finish {} {
    puts "</virtual_experiment>"
}

Simulator instproc spitxml_data {tag fields values} {
1628
    ::spitxml_data $tag $fields $values
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
}

#
# Global function, cause some objects do not hold a sim pointer.
# Should fix.
# 
proc spitxml_data {tag fields values} {
    puts "  <$tag>"
    puts "    <row>"
    foreach field $fields {
	set value  [lindex $values 0]
	set values [lrange $values 1 end]
1641
	set value_esc [xmlencode $value]
1642

1643
	puts "      <$field>$value_esc</$field>"
1644
1645
1646
1647
    }
    puts "    </row>"
    puts "  </$tag>"
}
1648
1649

proc xmlencode {args} {
165