sim.tcl.in 26.5 KB
Newer Older
1
# -*- tcl -*-
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2
3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2000-2004 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
27
28
29
30

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

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

    # 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 {}
53
54
55

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

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

61
62
63
    # Program list.
    $self instvar prog_list;
    array set prog_list {}
64
65
66
67
68
69
70
71
72

    var_import ::GLOBALS::last_class
    set last_class $self
}

# 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
73
74
75
76
77
78
79
}

# 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
80
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
81
82
83
    $self instvar id_counter
    $self instvar node_list

84
85
    if {($args != {})} {
	punsup "Arguments for node: $args"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
86
87
    }
    
88
    set curnode tbnode-n[incr id_counter]
Leigh B. Stoller's avatar
Leigh B. Stoller committed
89
    Node $curnode $self
90

Shashi Guruprasad's avatar
Shashi Guruprasad committed
91
92
93
    # simulated nodes have type 'sim'
    if { $simulated == 1 } {
        tb-set-hardware $curnode sim
94
95
96
97
	# 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
98
    } 
Shashi Guruprasad's avatar
Shashi Guruprasad committed
99
    set node_list($curnode) {}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
100
101
102
103
104
105
106
107
108
109
110
    set last_class $curnode

    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
111
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
112
113
114
115
    $self instvar id_counter
    $self instvar lanlink_list
    $self instvar link_map

116
117
    if {($args != {})} {
	punsup "Arguments for duplex-link: $args"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
118
119
120
    }
    set error 0
    if {! [$n1 info class Node]} {
121
	perror "\[duplex-link] $n1 is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
122
123
124
	set error 1
    }
    if {! [$n2 info class Node]} {
125
	perror "\[duplex-link] $n2 is not a node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
126
127
	set error 1
    }
128
129
130
131
#    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
132
133
134
135
136
137
138
139

    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
140
141
142
143
144
145
    if {$error} {return}

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

146
    set curlink tblink-l[incr id_counter]
147

Shashi Guruprasad's avatar
Shashi Guruprasad committed
148
    Link $curlink $self "$n1 $n2" $rbw $rdelay $type	
149
    set lanlink_list($curlink) {}
Shashi Guruprasad's avatar
Shashi Guruprasad committed
150
151
152
    set link_map($n1:$n2) $curlink
    set link_map($n2:$n1) $curlink

Leigh B. Stoller's avatar
Leigh B. Stoller committed
153
154
155
156
157
158
159
160
161
162
    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
163
    var_import ::GLOBALS::simulated
Leigh B. Stoller's avatar
Leigh B. Stoller committed
164
165
166
    $self instvar id_counter
    $self instvar lanlink_list

167
168
    if {($args != {})} {
	punsup "Arguments for make-lan: $args"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
169
    }
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    #
    # 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
189
190
191
192
193
194
    # 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 } {
195

Shashi Guruprasad's avatar
Shashi Guruprasad committed
196
197
198
199
200
201
202
	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 ""
	    }
203
204
	}
    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
205
206
207
208
209
210
211
212
213
214
215
216

    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
217
218
219
220
221
222
223
224
225
226
}

# 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
227
    $self instvar event_list
228
    $self instvar prog_list
Shashi Guruprasad's avatar
Shashi Guruprasad committed
229
230
    $self instvar simulated
    $self instvar nseconfig
231
232
    var_import ::GLOBALS::pid
    var_import ::GLOBALS::eid
Leigh B. Stoller's avatar
Leigh B. Stoller committed
233
234
235
236
    var_import ::GLOBALS::errors
    var_import ::GLOBALS::irfile
    var_import ::GLOBALS::ran
    var_import ::GLOBALS::impotent
237
    var_import ::GLOBALS::passmode
238
    var_import ::GLOBALS::vtypes
239
    var_import ::GLOBALS::uselatestwadata
240
    var_import ::GLOBALS::usewatunnels
241
242
243
    var_import ::GLOBALS::wa_delay_solverweight
    var_import ::GLOBALS::wa_bw_solverweight
    var_import ::GLOBALS::wa_plr_solverweight
244
245
    var_import ::GLOBALS::uselinkdelays
    var_import ::GLOBALS::forcelinkdelays
246
    var_import ::GLOBALS::multiplex_factor
247
    var_import ::GLOBALS::sync_server
248
249
    var_import ::GLOBALS::cpu_usage
    var_import ::GLOBALS::mem_usage
250
    var_import ::GLOBALS::veth_encapsulate
251
252
    var_import ::GLOBALS::jail_osname
    var_import ::GLOBALS::delay_osname
253
254
255
    var_import ::TBCOMPAT::objtypes
    var_import ::TBCOMPAT::eventtypes
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
256
257
258
259
260
    # Fill out IPs
    foreach obj [concat [array names lanlink_list]] {
	$obj fill_ips
    }

261
262
263
264
265
266
267
268
269
270
    # 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
	}
    }

271
272
    # Default sync server.
    set default_sync_server {}
273

Leigh B. Stoller's avatar
Leigh B. Stoller committed
274
275
276
277
278
279
    # 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]} {
280
	    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
281
282
283
284
	}
    }
    foreach lan [lsort [array names lanlink_list]] {
	if {! [regexp {^[-0-9A-Za-z]+$} $lan]} {
285
	    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
286
287
288
	}
    }

289
290
291
292
293
294
295
    # 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
296
297
298
299
    # If any errors occur stop here.
    if {$errors == 1} {return}

    # If we are running in impotent mode we stop here
300
    if {$impotent == 1 && $passmode == 0} {return}
301
    
302
303
    $self spitxml_init

Shashi Guruprasad's avatar
Shashi Guruprasad committed
304
305
306
307
308
309
310
311
312
313
314
315
    # 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
316

317
	$self spitxml_data "nseconfigs" [list "vname" "nseconfig" ] [list fullsim $nsecfg_script ]
Shashi Guruprasad's avatar
Shashi Guruprasad committed
318
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
319
320
321
    
    # Update the DB
    foreach node [lsort [array names node_list]] {
322
	$node updatedb "sql"
323
324
325
326

	if { $default_sync_server == {} } {
	    set default_sync_server $node
	}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
327
    }
328
    
Leigh B. Stoller's avatar
Leigh B. Stoller committed
329
    foreach lan [concat [array names lanlink_list]] {
330
	$lan updatedb "sql"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
331
    }
332
    foreach vtype [array names vtypes] {
333
	$vtype updatedb "sql"
334
    }
335
    foreach prog [array names prog_list] {
336
	$prog updatedb "sql"
337
    }
338

339
340
    set fields [list "mem_usage" "cpu_usage" "forcelinkdelays" "uselinkdelays" "usewatunnels" "uselatestwadata" "wa_delay_solverweight" "wa_bw_solverweight" "wa_plr_solverweight" "veth_encapsulate"]
    set values [list $mem_usage $cpu_usage $forcelinkdelays $uselinkdelays $usewatunnels $uselatestwadata $wa_delay_solverweight $wa_bw_solverweight $wa_plr_solverweight $veth_encapsulate]
341

342
    if { $multiplex_factor != {} } {
343
344
	lappend fields "multiplex_factor"
	lappend values $multiplex_factor
345
    }
346
    
347
348
349
    if { $sync_server != {} } {
	lappend fields "sync_server"
	lappend values $sync_server
350
351
352
    } elseif { $default_sync_server != {} } {
	lappend fields "sync_server"
	lappend values $default_sync_server
353
    }
354
355
356
357
358
359
360
361
362

    if { $jail_osname != {} } {
	lappend fields "jail_osname"
	lappend values $jail_osname
    }
    if { $delay_osname != {} } {
	lappend fields "delay_osname"
	lappend values $delay_osname
    }
363
    
364
    $self spitxml_data "experiments" $fields $values
365
366

    # This could probably be elsewhere.
367
    $self spitxml_data "virt_agents" [list "vnode" "vname" "objecttype" ] [list "*" $self $objtypes(SIMULATOR) ]
368
369
370

    # This will eventually be under user control.
    $self spitxml_data "virt_agents" [list "vnode" "vname" "objecttype" ] [list "*" "linktest" $objtypes(LINKTEST) ]
371
372
    
    foreach event $event_list {
373
	$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] ]
374
    }
375
376

    $self spitxml_finish
Leigh B. Stoller's avatar
Leigh B. Stoller committed
377
378
379
380
381
}

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

Leigh B. Stoller's avatar
Leigh B. Stoller committed
384
    if {! [$agent info class Agent]} {
385
	perror "\[attach-agent] $agent is not an Agent."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
386
387
388
	return
    }
    if {! [$node info class Node]} {
389
	perror "\[attach-agent] $node is not a Node."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
390
391
	return
    }
392
393
394
395
396
397
398
399
400
401

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

402
    $node attach-agent $agent
Leigh B. Stoller's avatar
Leigh B. Stoller committed
403
404
405
406
407
408
409
}

# connect <src> <dst>
# Connects two agents together.
Simulator instproc connect {src dst} {
    set error 0
    if {! [$src info class Agent]} {
410
	perror "\[connect] $src is not an Agent."
411
	set error 1
Leigh B. Stoller's avatar
Leigh B. Stoller committed
412
413
    }
    if {! [$dst info class Agent]} {
414
	perror "\[connect] $dst is not an Agent."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
415
416
417
418
419
420
421
	set error 1
    }
    if {$error} {return}
    $src connect $dst
    $dst connect $src
}

422
423
424
425
426
427
428
# at <time> <event>
# Known events:
#   <traffic> start
#   <traffic> stop
#   <link> up
#   <link> down
#   ...
429
Simulator instproc at {time eventstring} {
430
431
432
433
434
435
436
    var_import ::GLOBALS::simulated

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

437
438
439
440
441
    # Check that time is float
    if {[regexp {(^[0-9]+(\.[0-9]+)?$)|(^\.[0-9]+$)} $time] == 0} {
	perror "Invalid time spec: $time"
	return
    }
442
    $self instvar event_list
443
    $self instvar event_count
444

445
446
447
448
    if {$event_count > 4000} {
	perror "Too many events in your NS file!"
	exit 1
    }
449
    set eventlist [split $eventstring ";"]
450

451
  foreach event $eventlist {
452
453
454
    # Check otype/command
    set obj [lindex $event 0]
    set cmd [lindex $event 1]
455
    set atstring "$event"
456
    set args {}
457
458
459
460
461
    set okargs 0
    switch -- [$obj info class] {
	"Application/Traffic/CBR" {
	    set otype TRAFGEN
	    switch -- $cmd {
462
463
464
465
466
467
468
469
		"start" {
                    set params [$obj get_params]
		    set etype START
		    set args  $params
		}
		"stop" {
		    set etype STOP
		}
470
471
472
		"reset" {
		    set etype RESET
		}
473
474
                "set" {
		    if {[llength $event] < 4} {
475
			perror "Wrong number of arguments: at $time $event"
476
477
478
479
480
481
482
483
484
			return
		    }
		    set etype MODIFY
	            set arg [lindex $event 3]
                    switch -- [lindex $event 2] {
                        "packetSize_" {
	                    set args  "PACKETSIZE=$arg"
                        }
                        "rate_" {
485
486
			    set bw [parse_bw $arg]
	                    set args  "RATE=$bw"
487
488
489
490
                        }
                        "interval_" {
	                    set args  "INTERVAL=$arg"
                        }
491
                        "iptos_" {
492
	                    set args  "IPTOS=$arg"
493
                        }
494
495
496
497
498
499
			unknown {
		            punsup "at $time $event"
		            return
			}
		    }
                }
500
501
502
503
504
		unknown {
		    punsup "at $time $event"
		    return
		}
	    }
505
506
	    set vnode [$obj get_node]
	    set vname $obj
507
	}
508
509
510
511
512
513
514
515
516
517
518
519
520
521
	"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
522
523
	    set otype NSE
	    set etype NSEEVENT
524
525
526
527
	    set args "\$$obj $cmd [lrange $event 2 end]"
	    set vnode [$obj get_node]
	    set vname $obj
	}
528
529
530
	"Link" {
	    set otype LINK
	    switch -- $cmd {
531
532
533
534
		"up"	    {set etype UP}
		"down"	    {set etype DOWN}
		"bandwidth" {
		    if {[llength $event] < 4} {
535
			perror "Wrong number of arguments: at $time $event"
536
537
			return
		    }
538
                    set arg   [lindex $event 2]
539
540
		    set bw [parse_bw $arg]
                    set args  "BANDWIDTH=$bw"
541
542
543
544
		    set etype MODIFY
		}
		"delay" {
		    if {[llength $event] < 3} {
545
			perror "Wrong number of arguments: at $time $event"
546
547
548
549
			return
		    }
                    set arg   [lindex $event 2]
                    set args  "DELAY=$arg"
550
551
		    set etype MODIFY
		}
552
553
		"plr" {
		    if {[llength $event] < 3} {
554
			perror "Wrong number of arguments: at $time $event"
555
556
557
558
			return
		    }
                    if {[scan [lindex $event 2] "%f" plr] != 1 ||
                        $plr < 0 || $plr > 1} {
559
			perror "Improper argument: at $time $event"
560
561
562
563
564
			return
                    }
                    set args  "PLR=$plr"
		    set etype MODIFY
		}
565
566
567
568
569
570
571
572
573
574
		unknown {
		    punsup "at $time $event"
		    return
		}
	    }
	    set vnode {}
	    set vname $obj
	}
	"Queue" {
	    set otype LINK
575
	    set pipe [$obj get_pipe]
576
577
578
	    set obj [$obj get_link]
	    switch -- $cmd {
                "set" {
579
		    if {[llength $event] < 4} {
580
			perror "Wrong number of arguments: at $time $event"
581
582
583
			return
		    }
		    set etype MODIFY
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
	            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
			    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
606
	                    set args  "Q_WEIGHT=$w"
607
608
609
610
611
                        }
			unknown {
		            punsup "at $time $event"
		            return
			}
612
		    }
613
                }
614
615
616
617
618
		unknown {
		    punsup "at $time $event"
		    return
		}
	    }
619
	    set args "PIPE=$pipe $args"
620
621
622
	    set vnode {}
	    set vname $obj
	}
623
624
625
626
627
628
629
630
	"Program" {
	    set otype PROGRAM
	    set vname $obj
	    set vnode [$obj set node]
    
	    switch -- $cmd {
		"start" {
		    set etype START
631
632
		    # Must leave this here for the benefit of old
		    # images that expect the command line in the event. 
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
		    set arg  [$obj set command]
		    set args "COMMAND=$arg"
		}
		"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 "at $time $event"
		    return
		}
	    }
	    set okargs 1
	}
655
	"Simulator" {
656
657
658
	    set vnode "*"
	    set vname $self
	    
659
660
	    switch -- $cmd {
		"bandwidth" {
661
		    set otype LINK
662
		    set etype MODIFY
663
664
665
666
667
668
669
		    set vnode {}
		    set vname {}
		}
		"halt" {
		    set otype SIMULATOR
		    set etype HALT
		}
670
671
672
673
		"terminate" {
		    set otype SIMULATOR
		    set etype HALT
		}
674
675
676
		"swapout" {
		    set otype SIMULATOR
		    set etype SWAPOUT
677
678
679
680
681
682
683
684
685
686
687
688
689
		}
		unknown {
		    punsup "at $time $event"
		    return
		}
	    }
	    set okargs 1
	}
	unknown {
	    punsup "Unknown object type: at $time $event"
	    return
	}
    }
Shashi Guruprasad's avatar
Shashi Guruprasad committed
690

691
    set event_count [expr $event_count + 1]
692
    lappend event_list [list $time $vnode $vname $otype $etype $args $atstring]
693
  }
694
695
}

696
#
697
# Routing control.
698
699
700
#
Simulator instproc rtproto {type args} {
    var_import ::GLOBALS::default_ip_routing_type
701
702
703
704
705
706
    var_import ::GLOBALS::simulated

    # ignore at statement for simulated case
    if { $simulated == 1 } {
	return
    }
707
708
709
710
711
712
713

    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
714
715
    } elseif {($type == "Manual")} {
	set default_ip_routing_type "manual"
716
717
    } elseif {($type == "Static")} {
	set default_ip_routing_type "static"
718
719
720
721
722
723
    } else {
	punsup "rtproto: unsupported routing protocol ignored: $type"
	return
    }
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
724
725
726
727
728
729
730
# 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
731
    punsup "Object $m"
732
    NullClass tbnull-null[incr id_counter] $m
Leigh B. Stoller's avatar
Leigh B. Stoller committed
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
}

# 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
756
757
758
759
760
761

    # 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
762
763
}

764
765
766
767
768
769
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
770
771
772
773
774
775
776
777
778
779
780
781
# 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 ""
    }
}

782
783
Simulator instproc link {src dst} {
    set reallink [$self find_link $src $dst]
784
785
786
787
788
789
790
791
792
793
794
	
    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]
795
796
}

797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
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
817
818
819
# 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.
820
Simulator instproc get_subnet {netmask} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
821
822
823
    $self instvar subnet_base
    $self instvar subnets

824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
    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
886
887
	}
    }
888
    perror "Ran out of subnets."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
889
890
}

891
892
893
894
# 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 {} {
895
    $self instvar wa_subnet_counter
896

897
898
899
900
    if {$wa_subnet_counter > 255} {
	perror "Ran out of widearea subnets."
	return 0
    }
901
902
    set subnet $wa_subnet_counter
    incr wa_subnet_counter
903
    return "69.69.$subnet.0"
904
905
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
906
907
908
# 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.
909
Simulator instproc use_subnet {subnet netmask} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
910
    $self instvar subnets
911
912
913
914
915

    set subnetint [inet_atohl $subnet]
    set netmaskint [inet_atohl $netmask]
    
    set subnets($subnetint) $netmaskint
916
}
917

918
919
920
# add_program
# Link to a new program object.
Simulator instproc add_program {prog} {
921
    $self instvar prog_list
922
    set prog_list($prog) {}
923
}
924
925
926
927
928
929
930

# 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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953

# 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
954
    uplevel 1 $script
Shashi Guruprasad's avatar
Shashi Guruprasad committed
955
956

    append nseconfig $script
957
    append nseconfig \n
Shashi Guruprasad's avatar
Shashi Guruprasad committed
958
959
960

    set simulated 0
}
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977

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

    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} {
978
    ::spitxml_data $tag $fields $values
979
980
981
982
983
984
985
986
987
988
989
990
}

#
# 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]
991
	set value_esc [xmlencode $value]
992

993
	puts "      <$field>$value_esc</$field>"
994
995
996
997
    }
    puts "    </row>"
    puts "  </$tag>"
}
998
999

proc xmlencode {args} {
1000
1001
1002
1003
1004
1005
    set retval [eval append retval $args]
    regsub -all "&" $retval "\\&amp;" retval
    regsub -all "<" $retval "\\&lt;" retval
    regsub -all ">" $retval "\\&gt;" retval
    regsub -all "\"" $retval "\\&\#34;" retval
    regsub -all "]" $retval "\\&\#93;" retval
1006
    
1007
    return $retval
1008
}