parse.tcl.in 11.7 KB
Newer Older
Mac Newbold's avatar
Mac Newbold committed
1
#!/usr/local/bin/otclsh
Leigh B. Stoller's avatar
Leigh B. Stoller committed
2 3 4

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
6 7 8
# All rights reserved.
#

Leigh B. Stoller's avatar
Leigh B. Stoller committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22
######################################################################
# parse.tcl.in
#
# This is the testbed parser.  It takes a project id, an experiment
# id and a NS file.  It will parse the NS file and update the DB.
# It also displays warnings for unsupported functionality.
#
# See README for extensive discussion of the structure and 
# implementation.
#
# -q will supress all the unsupported messages.
# -n will cause the parser to output error/warning messages and exit
#    without updating the database.
######################################################################
Mac Newbold's avatar
Mac Newbold committed
23

Leigh B. Stoller's avatar
Leigh B. Stoller committed
24
package require sql
Mac Newbold's avatar
Mac Newbold committed
25

Leigh B. Stoller's avatar
Leigh B. Stoller committed
26
# Initial Procedures
27

Leigh B. Stoller's avatar
Leigh B. Stoller committed
28 29 30 31 32 33 34 35 36 37 38
###
# lpop <listname>
# This takes the *name* of a list variable and pops the first element
# off of it, returning that element.
###
proc lpop {lv} {
    upvar $lv l
    set ret [lindex $l 0]
    set l [lrange $l 1 end]
    return $ret
}
Mac Newbold's avatar
Mac Newbold committed
39 40

###
Leigh B. Stoller's avatar
Leigh B. Stoller committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54
# var_import <varspec>
# This procedure takes a fully qualified variable name (::x::y::z..) and
# creates a variable z which is the same as the variable specified.  This
# fills the lack of variable importing support in 'namespace import'.
#
# Example:
#  proc a {} {
#    var_import ::GLOBALS::verbose
#    if {$verbose == 1} {puts "verbose is on."}
#  }
# is functionally identical to:
#  proc a {} {
#    if {${::GLOBALS::verbose} == 1} {puts "verbose is on."}
#  }
Mac Newbold's avatar
Mac Newbold committed
55
###
Leigh B. Stoller's avatar
Leigh B. Stoller committed
56 57 58 59 60 61 62 63 64
proc var_import {varspec} {
    uplevel "upvar $varspec [namespace tail $varspec]"
}

###
# perror <msg>
# Print an error message and mark as failed run.
###
proc perror {msg} {
65
    var_import ::GLOBALS::errors 
66 67 68 69 70 71 72 73 74
    var_import ::GLOBALS::simulated

    # If this was a true error in specifying
    # the simulation, it would have been
    # caught when run with NSE
    if { $simulated == 1 } {
	return 0
    }

75
    global argv0
76 77
    puts stderr "*** $argv0: "
    puts stderr "    $msg"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
78 79 80
    set errors 1
}

81 82
###
# punsup {msg}
83
# Print an unsupported message.
84 85
###
proc punsup {msg} {
86
    var_import ::GLOBALS::verbose
87
    var_import ::GLOBALS::WARN_FILE
88 89 90 91 92 93
    var_import ::GLOBALS::simulated

    # If this was a true error in specifying
    # the simulation, it would have been
    # caught when run with NSE
    if {$simulated == 0 && $verbose == 1} {
94 95
	puts stderr "*** WARNING: Unsupported NS Statement!"
	puts stderr "    $msg"
96 97
	puts $WARN_FILE "*** WARNING: Unsupported NS Statement!"
	puts $WARN_FILE "    $msg"
98
    }
99
}	
Leigh B. Stoller's avatar
Leigh B. Stoller committed
100

101 102 103 104 105 106
#
# We ignore unsupported tcl commands if it is inside
# make-simulated else error is flagged i.e. we call 
# perror which does the right thing
#
proc unknown {args} {
107
    perror "Unknown: $args"
108 109
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
110 111 112 113 114 115 116
# Parse Arguments

# We setup a few globals that we need for argument parsing.
namespace eval GLOBALS {
variable verbose 1
variable impotent 0
variable anonymous 0
117
variable vtype_list {}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
}

while {$argv != {}} {
    set arg [lindex $argv 0]
    if {$arg == "-n"} {
	lpop argv
	set GLOBALS::impotent 1
    } elseif {$arg == "-q"} {
	lpop argv
	set GLOBALS::verbose 0
    } elseif {$arg == "-a"} {
	lpop argv
	set GLOBALS::anonymous 1
    } else {
	break
Mac Newbold's avatar
Mac Newbold committed
133
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
134 135 136 137 138 139 140 141 142 143
}

if {${GLOBALS::anonymous} && ! ${GLOBALS::impotent}} {
    puts stderr "-a can only be used with -n."
    exit 1
}

if {${GLOBALS::anonymous} && ([llength $argv] != 1)} {
    puts stderr "Syntax: $argv0 \[-q\] -n -a ns_file"
    exit 1
144 145
} elseif {(! ${GLOBALS::anonymous}) && ([llength $argv] != 4)} {
    puts stderr "Syntax: $argv0 \[-q\] \[-n \[-a\]\] pid gid eid ns_file"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
146 147 148 149 150 151 152 153
    exit 1
}

# Now we can set up the rest of our global variables.
namespace eval GLOBALS {
    # Remaining arguments
    if {$anonymous} {
	variable pid "PID"
154
	variable gid "GID"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
155 156
	variable eid "EID"
	variable nsfile [lindex $argv 0]
Mac Newbold's avatar
Mac Newbold committed
157
    } else {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
158
	variable pid [lindex $argv 0]
159 160 161
	variable gid [lindex $argv 1]
	variable eid [lindex $argv 2]
	variable nsfile [lindex $argv 3]
Mac Newbold's avatar
Mac Newbold committed
162
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
163 164 165 166 167 168 169 170 171
    
    # This is used to name class instances by the variables they
    # are stored in.  It contains the initial id of the most
    # recently created class.  See README
    variable last_class {}

    # Some settings taken from configure.
    variable tbroot @prefix@
    variable libdir @prefix@/lib/ns2ir
172
    variable disablense {@DISABLE_NSE@}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
173 174 175 176 177 178

    # This is the location of the tb_compat.tcl file.  It is copied
    # into the same directory is the ns file so that the initial
    # 'source tb_compat.tcl' statement succeeds.
    variable tbcompat "$libdir/tb_compat.tcl"

179 180 181
    # This is used in running the script through nse for syntax errors
    variable nstbcompat "$libdir/nstb_compat.tcl"

Leigh B. Stoller's avatar
Leigh B. Stoller committed
182 183 184 185 186 187 188 189 190 191 192 193
    # Is 1 if any errors have occured so far.
    variable errors 0
    
    # Is 1 after a 'Simulator run' command.
    variable ran 0

    # This is a counter used by the 'new' procedure to create null
    # classes.
    variable new_counter 0

    # This will hold the handle for the database connection
    variable DB {}
194 195 196

    # These are going to be default values within the NS file.
    variable default_ip_routing_type "none"
197

198 199 200
    # For remote nodes. Use latest widearea data.
    variable uselatestwadata 0

201 202 203
    # For remote nodes. Use tunnels
    variable usewatunnels 1

204 205 206 207 208 209
    # Use link delays instead of delay nodes.
    variable uselinkdelays 0

    # Force link delays (where a delay would not otherwise be inserted)
    variable forcelinkdelays 0

210 211 212 213 214
    # For remote nodes. The solver weights.
    variable wa_delay_solverweight 1
    variable wa_bw_solverweight	   7
    variable wa_plr_solverweight   500

215 216
    # This is the file handler for the warnings file
    variable WARN_FILE [open "$eid.warnings" w]
217 218 219 220 221

    # This distinguishes whether the script that
    # is being parsed should go into a NSE simulation or not
    variable simulated 0

222 223
    # Hidden variable to relax some restrictions for debugging.
    variable enforce_user_restrictions 1
Mac Newbold's avatar
Mac Newbold committed
224 225
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
226 227 228
# Connect to the DB
set GLOBALS::DB [sql conn]
sql selectdb ${GLOBALS::DB} @TBDBNAME@
Mac Newbold's avatar
Mac Newbold committed
229

230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
namespace eval GLOBALS {
    # A mapping of event objects.
    variable objtypes

    sql query $DB \
	    "select idx,type from event_objecttypes"
    
    while {[set row [sql fetchrow $DB]] != ""} {
	set idx [lindex $row 0]
	set type [lindex $row 1]
	set objtypes($type) $idx
    }
    sql endquery $DB
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
245 246 247 248 249 250 251
# Load all our classes
source ${GLOBALS::libdir}/nsobject.tcl
source ${GLOBALS::libdir}/sim.tcl
source ${GLOBALS::libdir}/lanlink.tcl
source ${GLOBALS::libdir}/node.tcl
source ${GLOBALS::libdir}/null.tcl
source ${GLOBALS::libdir}/traffic.tcl
252
source ${GLOBALS::libdir}/vtype.tcl
253
source ${GLOBALS::libdir}/program.tcl
Mac Newbold's avatar
Mac Newbold committed
254

Leigh B. Stoller's avatar
Leigh B. Stoller committed
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
##################################################
# Redifing Assignment
#
# Here we rewrite the set command.  The global variable 'last_class'
# holds the name instance created just before set.  If last_class is set
# and the value of the set call is last_class then the value should be
# changed to the variable and the class renamed to the variable.  I.e.
# we are making it so that NS objects are named by the variable they
# are stored in.
#
# We only do this if the level above is the global level.  I.e. if
# class are created in subroutines they keep their internal names
# no matter what.
#
# We munge array references from ARRAY(INDEX) to ARRAY-INDEX.
#
# Whenever we rename a class we call the rename method.  This method
# should update all references that it may have set up to itself.
#
# See README
##################################################
rename set real_set
proc set {args} {
    var_import GLOBALS::last_class
Mac Newbold's avatar
Mac Newbold committed
279

Leigh B. Stoller's avatar
Leigh B. Stoller committed
280 281 282
    # There are a bunch of cases where we just pass through to real set.
    if {[llength $args] == 1} {
	return [uplevel real_set \{[lindex $args 0]\}]
283
    } elseif {($last_class == {})} {
Leigh B. Stoller's avatar
Leigh B. Stoller committed
284 285
	return [uplevel real_set \{[lindex $args 0]\} \{[lindex $args 1]\}]
    }
Mac Newbold's avatar
Mac Newbold committed
286

Leigh B. Stoller's avatar
Leigh B. Stoller committed
287 288
    real_set var [lindex $args 0]
    real_set val [lindex $args 1]
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305

    # Run the set to make sure variables declared as global get registered
    # as global (does not happen until first set).
    real_set ret [uplevel real_set \{$var\} \{$val\}]

    # Rename happens only when assigning to a global variable. Because of
    # array syntax, must strip parens and indices to get the base variable
    # name (has no effect if not an array access).
    real_set l [split $var \(]
    real_set base_var [lindex $l 0]

    # Now check to see if its a global. No renaming if not a global.
    if {[uplevel info globals $base_var] == {}} {
        return $ret
    }
    
    # At this point this is an assignment immediately after class creation.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
306 307 308 309
    if {$val == $last_class} {
	# Here we change ARRAY(INDEX) to ARRAY-INDEX
	regsub -all {[\(]} $var {-} out
	regsub -all {[\)]} $out {} val
310

Leigh B. Stoller's avatar
Leigh B. Stoller committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
	# Sanity check
	if {! [catch "uplevel info args $val"]} {
	    error "Already have an object named $val."
	}
	# And now we rename the class.  After the class has been
	# renamed we call it its rename method.
	uplevel rename $last_class $val
	uplevel $val rename $last_class $val
    }
    
    # Reset last_class in all cases.
    real_set last_class {}
    
    # And finally we pass through to the actual assignment operator.
    return [uplevel real_set \{$var\} \{$val\}]
}
Mac Newbold's avatar
Mac Newbold committed
327

Leigh B. Stoller's avatar
Leigh B. Stoller committed
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
###
# new <class> ...
# NS defines the new command to create class instances.  If the call is
# for an object we know about we create and return an instance.  For 
# any classes we do not know about we create a null class and return it
# as well as display an unsupported message.
#
# new_classes is an array in globals that defines the classes
# new should support.  The index is the class name and the value
# is the argument list.
#
# TODO: Implement support for classes that take arguments.  None yet
# in supported NS subset.
###
namespace eval GLOBALS {
    variable new_classes
    set new_classes(Simulator) {}
}
proc new {class args} {
    var_import GLOBALS::new_counter
    var_import GLOBALS::new_classes
    if {! [info exists new_classes($class)]} {
350
	punsup "Object: $class"
Leigh B. Stoller's avatar
Leigh B. Stoller committed
351
	set id null[incr new_counter]
352
	NullClass $id $class
Leigh B. Stoller's avatar
Leigh B. Stoller committed
353 354
	return $id
    }
355

Leigh B. Stoller's avatar
Leigh B. Stoller committed
356
    set id $class[incr new_counter]
357 358 359 360 361 362 363 364 365

    # XXX Hack!
    if {[llength $args] > 1} {
	punsup "arguments for $class"
    } elseif {[llength $args] == 1} {
	eval $class $id [lindex $args 0]
    } else {
	eval $class $id 
    }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
366 367 368 369 370 371 372 373 374
    return $id
}

# Useful routines.

# parse_bw bspec 
# This takes a bandwidth specifier in the form of <amt><unit> where
# <unit> is any of b, bps, kb, kbps, Mb, Mbps, Gb, or Gbps.  If no
# unit is given then bytes (b) is assumed.  It returns the bandwidth
375
# in Kbps.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
376 377 378 379 380
proc parse_bw {bspec} {
    # Default to bytes
    if {[scan $bspec "%f%s" bw unit] == 1} {
	set unit b
    }
381

Leigh B. Stoller's avatar
Leigh B. Stoller committed
382
    switch -- $unit {
383 384 385 386 387 388 389 390
	b {set val [expr int($bw/1000)]} 
	bps {set val [expr int($bw/1000)]} 
	kb {set val [expr int($bw)]}
	kbps {set val [expr int($bw)]}
	Mb {set val [expr int($bw*1000)]}
	Mbps {set val [expr int($bw*1000)]}
	Gb {set val [expr int($bw*1000000)]}
	Gbps {set val [expr int($bw*1000000)]}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
391
	default {
392
	    perror "Unknown bandwidth unit $unit."
393
	    set val 100000
Leigh B. Stoller's avatar
Leigh B. Stoller committed
394 395
	}
    }
396 397 398 399 400 401

    if {$val < 10} {
	perror "Bandwidth of $val Kbs is too low."
	return 0
    }
    return $val
Leigh B. Stoller's avatar
Leigh B. Stoller committed
402
}
403

Leigh B. Stoller's avatar
Leigh B. Stoller committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417
# parse_delay dspec
# This takes a delay specifier in the form of <amt><unit> where <unit>
# is any of s, ms, ns.  If no unit is given then seconds (s) is
# assumed.  It returns the delay in ms.
proc parse_delay {dspec} {
    # Default to seconds
    if {[scan $dspec "%f%s" delay unit] == 1} {
	set unit s
    }
    switch $unit {
	s {return [expr int($delay*1000)]}
	ms {return [expr int($delay)]}
	ns {return [expr int($delay/1000)]}
	default {
418
	    perror "Unknown delay unit $unit."
Leigh B. Stoller's avatar
Leigh B. Stoller committed
419 420 421 422 423 424 425
	    return 0
	}
    }
}

# We now have all our infrastructure in place.  We are ready to load
# the NS file.
Mac Newbold's avatar
Mac Newbold committed
426

427 428 429 430 431 432 433
if { ${GLOBALS::errors} != 1 } {
    file copy -force ${GLOBALS::tbcompat} .
    source ${GLOBALS::nsfile}

    if {${GLOBALS::ran} == 0} {
	perror "No 'Simulator run' statement found."
    }
Mac Newbold's avatar
Mac Newbold committed
434

Leigh B. Stoller's avatar
Leigh B. Stoller committed
435
}
436

437 438 439
# Clean up
sql disconnect ${GLOBALS::DB}

440 441
close ${GLOBALS::WARN_FILE}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
442
exit ${GLOBALS::errors}