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

#
4
# Copyright (c) 2000-2016 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

Leigh Stoller's avatar
Leigh Stoller committed
26 27 28 29 30 31 32 33 34 35
######################################################################
# 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.
#
36 37 38 39 40 41
# -q quiet mode: supress all the unsupported messages.
# -n impotent mode: parser will output error/warning messages and exit
#    without spitting out the actual parse results.
# -a anonymous mode: do not do project related checks. Turns on impotent
#    mode (-n).
# -p pass mode: Similar to anonymous mode, except that the parser *will*
42 43
#    spit out the parse results.
# -r rspecmode. 
Leigh Stoller's avatar
Leigh Stoller committed
44
######################################################################
Mac Newbold's avatar
Mac Newbold committed
45

46 47 48 49 50 51
proc usage {} {
    puts stderr "Syntax: $argv0 \[-q\] -a ns_file"    
    puts stderr "        $argv0 \[-q\] \[-p\] \[-n\] pid gid eid ns_file"
    exit 1
}

Leigh Stoller's avatar
Leigh Stoller committed
52
# Initial Procedures
53

Leigh Stoller's avatar
Leigh Stoller committed
54 55 56 57 58 59 60 61 62 63 64
###
# 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
65 66

###
Leigh Stoller's avatar
Leigh Stoller committed
67 68 69 70 71 72 73 74
# 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
75
#    if {$verbose == 1} {puts stderr "verbose is on."}
Leigh Stoller's avatar
Leigh Stoller committed
76 77 78
#  }
# is functionally identical to:
#  proc a {} {
79
#    if {${::GLOBALS::verbose} == 1} {puts stderr "verbose is on."}
Leigh Stoller's avatar
Leigh Stoller committed
80
#  }
Mac Newbold's avatar
Mac Newbold committed
81
###
Leigh Stoller's avatar
Leigh Stoller committed
82 83 84 85 86 87 88 89 90
proc var_import {varspec} {
    uplevel "upvar $varspec [namespace tail $varspec]"
}

###
# perror <msg>
# Print an error message and mark as failed run.
###
proc perror {msg} {
91
    var_import ::GLOBALS::errors 
92 93 94 95 96 97 98 99 100
    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
    }

101
    global argv0
102 103
    puts stderr "*** $argv0: "
    puts stderr "    $msg"
Leigh Stoller's avatar
Leigh Stoller committed
104 105 106
    set errors 1
}

107 108
###
# punsup {msg}
109
# Print an unsupported message.
110 111
###
proc punsup {msg} {
112
    var_import ::GLOBALS::verbose
113 114 115 116 117 118
    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} {
119 120 121
	puts stderr "*** WARNING: Unsupported NS Statement!"
	puts stderr "    $msg"
    }
122
}	
Leigh Stoller's avatar
Leigh Stoller committed
123

124 125 126 127 128 129
#
# 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} {
130
    error "Unknown: $args"
131 132
}

Leigh Stoller's avatar
Leigh Stoller committed
133 134 135 136 137 138 139
# 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
140
variable passmode 0
141
variable rspecmode 0
142
variable vtype_list {}
Leigh Stoller's avatar
Leigh Stoller committed
143 144 145 146 147 148 149 150 151 152 153 154 155
}

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
156 157 158 159
	set GLOBALS::impotent 1
    } elseif {$arg == "-p"} {
	lpop argv
	set GLOBALS::passmode 1
160 161 162
    } elseif {$arg == "-r"} {
	lpop argv
	set GLOBALS::rspecmode 1
Leigh Stoller's avatar
Leigh Stoller committed
163 164
    } else {
	break
Mac Newbold's avatar
Mac Newbold committed
165
    }
Leigh Stoller's avatar
Leigh Stoller committed
166 167 168
}

if {${GLOBALS::anonymous} && ([llength $argv] != 1)} {
169
    usage();
170
} elseif {(! ${GLOBALS::anonymous}) && ([llength $argv] != 4)} {
171
    usage();
Leigh Stoller's avatar
Leigh Stoller committed
172 173 174 175 176 177 178
}

# Now we can set up the rest of our global variables.
namespace eval GLOBALS {
    # Remaining arguments
    if {$anonymous} {
	variable pid "PID"
179
	variable gid "GID"
Leigh Stoller's avatar
Leigh Stoller committed
180 181
	variable eid "EID"
	variable nsfile [lindex $argv 0]
Mac Newbold's avatar
Mac Newbold committed
182
    } else {
Leigh Stoller's avatar
Leigh Stoller committed
183
	variable pid [lindex $argv 0]
184 185 186
	variable gid [lindex $argv 1]
	variable eid [lindex $argv 2]
	variable nsfile [lindex $argv 3]
Mac Newbold's avatar
Mac Newbold committed
187
    }
Leigh Stoller's avatar
Leigh Stoller committed
188 189 190 191 192 193 194 195 196
    
    # 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
197
    variable disablense {@DISABLE_NSE@}
Leigh Stoller's avatar
Leigh Stoller committed
198 199 200 201 202 203

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

204 205 206
    # This is used in running the script through nse for syntax errors
    variable nstbcompat "$libdir/nstb_compat.tcl"

Leigh Stoller's avatar
Leigh Stoller committed
207 208 209 210 211 212 213 214 215 216
    # 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

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

220
    # For remote nodes. Use latest widearea data.
221
    variable uselatestwadata 1
222

223
    # For remote nodes. Use tunnels
224
    variable usewatunnels 0
225

226 227 228 229 230 231
    # Use link delays instead of delay nodes.
    variable uselinkdelays 0

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

232 233 234
    # Control multiplex_factor for the experiment. Crude.
    variable multiplex_factor {}

235 236 237
    # Control packing strategy for the experiment. pack or balance.
    variable packing_strategy {}

238 239 240
    # The name of the sync_server
    variable sync_server {}

241 242 243 244 245 246
    # Whether or not use use ipassign
    variable use_ipassign 0

    # Arguments to pass to ipassign
    variable ipassign_args {}

247 248 249 250 251
    # For remote nodes. The solver weights.
    variable wa_delay_solverweight 1
    variable wa_bw_solverweight	   7
    variable wa_plr_solverweight   500

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

256 257
    # Hidden variable to relax some restrictions for debugging.
    variable enforce_user_restrictions 1
258 259 260

    # CPU and MEM usage values. Zero means ignore. For now, lets make
    # it an integer, 1 <= x <= 5.
261
    variable cpu_usage 3
262
    variable mem_usage 0
263

264 265 266
    # Flag to disable doing a fix-node
    variable fix_current_resources 1

267 268 269 270
    # Control virtual link encapsulation
    # XXX "default" is for backward compat so we can distinguish
    # specified vs. unspecified in assign_wrapper
    variable vlink_encapsulate "default"
271

272
    # Allow override of jail, delay, simnode osids.
273 274
    variable jail_osname {}
    variable delay_osname {}
275
    variable sim_osname "FBSD-STD"
276

277 278 279
    # Allow override of delay capacity.
    variable delay_capacity {}

280 281
    # Use phys naming
    variable use_physnaming 0
282 283 284 285 286

    # Modelnet support. Number of physical core and edge nodes that user
    # wants thrown at the problem.
    variable modelnet_cores 0
    variable modelnet_edges 0
287 288 289

    # Is an inner elab experiment.
    variable elab_in_elab 0
290
    variable elabinelab_topo ""
291
    variable elabinelab_eid {}
292
    variable elabinelab_cvstag {}
293
    variable elabinelab_singlenet 0
294 295 296 297
    variable elabinelab_fw_type "ipfw2-vlan"

    # Disable NFS mounts for experiment?
    variable nonfs 0
298

299 300 301
    # Does user want a per-experiment DB?
    variable dpdb 0

302 303 304 305
    # Security level. Defaults to 0 ("green")
    # If explicit_firewall is set, then you cannot also give a security level.
    # security_level_diskzap is the level at which we need to zap the disk
    #  at swapout time.  This value (2) is encoded in libdb.pm also.
306
    variable security_level 0
307 308
    variable explicit_firewall 0
    variable security_level_diskzap 2
309

310 311 312
    # List of source files.
    variable sourcefile_list {}

313 314 315 316
    variable optarray_order
    array set optarray_order {}
    variable optarray_count 0

317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
    #
    # Named argument helper function.
    #
    # Example:
    #
    #  proc replace {s args} {
    #    named $args {-from 0 -to end -with ""}
    #    string replace $s $(-from) $(-to) $(-with)
    #  }
    #
    #  % replace suchenwirth -from 4 -to 6 -with xx
    #  suchxxirth
    #  % replace suchenwirth -from 4 -to 6 -witha xx
    #  bad option '-witha', should be one of: -from -to -with
    #
    # @param args The optional arguments the caller received.
    # @param defaults The option list with default values.
    #
    # @see http://wiki.tcl.tk/10702
    #
    proc named-args {args defaults} {
	upvar 1 "" ""
	array set "" $defaults
	foreach {key value} $args {
	    if {![info exists ($key)]} {
		error "bad option '$key', should be one of: [lsort [array names {}]]"
	    }
	    set ($key) $value
	}
    }

    #
    # Convert a string that represents a relative time into the corresponding
    # number of seconds.  The input string can simply be a number or it can
    # be in a more human readable format.  The only format supported at the
    # moment is "<hours>h<mins>m<secs>s".
    #
    # Examples:
    #
    #  % puts "[reltime-to-secs 20]"
    #  20
    #  % puts "[reltime-to-secs 2m]"
    #  120
    #  % puts "[reltime-to-secs 1h20m3s]"
    #  4803
    #
    # @param reltime A string representing a relative time value.
    # @return The relative time in seconds or -1 if the string could not be
    # parsed.
    #
    proc reltime-to-secs {reltime} {
	if {[regexp {(^[0-9]+(\.[0-9]+)?$)|(^\.[0-9]+$)} $reltime]} {
	    set retval $reltime
	} elseif {
	    [regexp {^([0-9]+h)?([0-9]+m)?([0-9]+s)?$} $reltime d hours mins secs]} {
		if {$hours == ""} {
		    set hours "0h"
		}
		if {$mins == ""} {
		    set mins "0m"
		}
		if {$secs == ""} {
		    set secs "0s"
		}
		set hours [string trim $hours h]
		set mins [string trim $mins m]
		set secs [string trim $secs s]
		set retval [expr ($hours * 60 * 60) + ($mins * 60) + $secs]
	} else {
	    set retval -1
	}
	return $retval
    }
Mac Newbold's avatar
Mac Newbold committed
390 391
}

Leigh Stoller's avatar
Leigh Stoller committed
392 393 394 395
# Load all our classes
source ${GLOBALS::libdir}/nsobject.tcl
source ${GLOBALS::libdir}/sim.tcl
source ${GLOBALS::libdir}/lanlink.tcl
396
source ${GLOBALS::libdir}/path.tcl
Leigh Stoller's avatar
Leigh Stoller committed
397 398 399
source ${GLOBALS::libdir}/node.tcl
source ${GLOBALS::libdir}/null.tcl
source ${GLOBALS::libdir}/traffic.tcl
400
source ${GLOBALS::libdir}/vtype.tcl
401
source ${GLOBALS::libdir}/program.tcl
402
source ${GLOBALS::libdir}/event.tcl
403
source ${GLOBALS::libdir}/firewall.tcl
404 405
source ${GLOBALS::libdir}/timeline.tcl
source ${GLOBALS::libdir}/sequence.tcl
406
source ${GLOBALS::libdir}/console.tcl
407
source ${GLOBALS::libdir}/topography.tcl
408
source ${GLOBALS::libdir}/disk.tcl
409
source ${GLOBALS::libdir}/blockstore.tcl
410
source ${GLOBALS::libdir}/custom.tcl
Mac Newbold's avatar
Mac Newbold committed
411

Leigh Stoller's avatar
Leigh Stoller committed
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
##################################################
# 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
436 437
    var_import GLOBALS::optarray_order
    var_import GLOBALS::optarray_count
Mac Newbold's avatar
Mac Newbold committed
438

Leigh Stoller's avatar
Leigh Stoller committed
439 440 441
    # 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]\}]
442
    } elseif {($last_class == {})} {
443 444 445
	real_set var [lindex $args 0]
	real_set val [lindex $args 1]

446 447 448 449
	#
	# This is the special OPT array, which we need to keep ordered
	# when inserting into the DB.
	#
450
	if {[regexp {^opt\(([-_0-9a-zA-Z]+)\)} $var d optname]} {
451 452 453 454
	    real_set optarray_order($optarray_count) $optname
	    incr optarray_count
	}
	return [uplevel real_set \{$var\} \{$val\}]
Leigh Stoller's avatar
Leigh Stoller committed
455
    }
Mac Newbold's avatar
Mac Newbold committed
456

Leigh Stoller's avatar
Leigh Stoller committed
457 458
    real_set var [lindex $args 0]
    real_set val [lindex $args 1]
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

    # 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 Stoller's avatar
Leigh Stoller committed
476 477 478 479
    if {$val == $last_class} {
	# Here we change ARRAY(INDEX) to ARRAY-INDEX
	regsub -all {[\(]} $var {-} out
	regsub -all {[\)]} $out {} val
480

Leigh Stoller's avatar
Leigh Stoller committed
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	# 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
497

498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
##################################################
# Redifing source
#
# Trap the "source" command so that we can tell the boss-side caller about
# any ns files it has to capture and archive.
#
##################################################
rename source real_source
proc source {args} {
    var_import GLOBALS::sourcefile_list

    #
    # Record the name for later.
    #
    # XXX Do not record the tb_compat file ...
    #
    if {[lindex $args 0] != "tb_compat.tcl"} {
	lappend sourcefile_list [lindex $args 0]
    }

    #
    # The run real command returning value.
    #
    return [uplevel real_source \{[lindex $args 0]\}]
}

Leigh Stoller's avatar
Leigh Stoller committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
###
# 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)]} {
546
	punsup "Object: $class"
Leigh Stoller's avatar
Leigh Stoller committed
547
	set id null[incr new_counter]
548
	NullClass $id $class
Leigh Stoller's avatar
Leigh Stoller committed
549 550
	return $id
    }
551

Leigh Stoller's avatar
Leigh Stoller committed
552
    set id $class[incr new_counter]
553 554 555 556 557 558 559 560 561

    # 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 Stoller's avatar
Leigh Stoller committed
562 563 564 565 566 567 568 569 570
    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
571
# in Kbps.
572
proc parse_bw {bspec {islink 1}} {
573
    #
574
    # Special cases 
575
    #
576 577
    # "*" means let assign pick the bandwidth. Make it zero.
    # "~" means "best effort" bandwidth - not conservatively allocated.
Kirk Webb's avatar
Kirk Webb committed
578
    #
Kirk Webb's avatar
Kirk Webb committed
579
    if {"$bspec" == "*"} {
Kirk Webb's avatar
Kirk Webb committed
580
	return 0
581
    } 
Kirk Webb's avatar
Kirk Webb committed
582 583 584 585 586 587 588 589
    if {"$bspec" == "~"} {
	# XXX: this is not the right way to handle this.  However, since
	# it's only supposed to be used for sanlans with no shaping
	# (enforced in lanlink.tcl), this is WAAAY easier than trying
	# to feed some new flag or sentinel value down into the guts
	# of the mapper.
	return 10
    }
590

Leigh Stoller's avatar
Leigh Stoller committed
591 592 593 594
    # Default to bytes
    if {[scan $bspec "%f%s" bw unit] == 1} {
	set unit b
    }
595

596
    # We could do better below with a regexp match. But it is better to keep it simple.
Leigh Stoller's avatar
Leigh Stoller committed
597
    switch -- $unit {
598 599
	b {set val [expr int($bw/1000)]} 
	bps {set val [expr int($bw/1000)]} 
600
	k {set val [expr int($bw)]}
601 602
	kb {set val [expr int($bw)]}
	kbps {set val [expr int($bw)]}
603 604 605
	K {set val [expr int($bw)]}
	Kb {set val [expr int($bw)]}
	Kbps {set val [expr int($bw)]}
606
	M {set val [expr int($bw*1000)]}
607 608
	Mb {set val [expr int($bw*1000)]}
	Mbps {set val [expr int($bw*1000)]}
609
	G {set val [expr int($bw*1000000)]}
610 611
	Gb {set val [expr int($bw*1000000)]}
	Gbps {set val [expr int($bw*1000000)]}
Leigh Stoller's avatar
Leigh Stoller committed
612
	default {
613
	    perror "Unknown bandwidth unit $unit."
614
	    set val 100000
Leigh Stoller's avatar
Leigh Stoller committed
615 616
	}
    }
617

618
    if {$val < 10 && $islink} {
619 620 621 622
	perror "Bandwidth of $val Kbs is too low."
	return 0
    }
    return $val
Leigh Stoller's avatar
Leigh Stoller committed
623
}
624

625 626 627 628
proc parse_backfill {bspec} {
    return [parse_bw $bspec 0]
}

Leigh Stoller's avatar
Leigh Stoller committed
629 630 631 632 633 634 635 636 637 638
# 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 {
639 640 641
	s {set val [expr $delay*1000]}
	ms {set val $delay}
	ns {set val [expr $delay/1000]}
Leigh Stoller's avatar
Leigh Stoller committed
642
	default {
643
	    perror "Unknown delay unit $unit."
Leigh Stoller's avatar
Leigh Stoller committed
644 645 646
	    return 0
	}
    }
647 648 649 650 651
    if {$val != 0 && $val < @DELAYTHRESH@} {
	perror "The delay of ${val}ms is invalid, it must be either 0 or at least @DELAYTHRESH@ms."
	return 0
    }
    return [expr int($val)]
Leigh Stoller's avatar
Leigh Stoller committed
652 653
}

654
# convert_to_mebi
655 656 657
# This takes a data size specifier in the form of <amt><unit> where
# <unit> is any of [B, KB, KiB, MB, MiB, GB, GiB, TB, TiB].  If no
# unit is given then bytes (B) is assumed.  It returns the size
658 659
# in Mebibytes.  Data sizes in bits (lowercase b) are not handled (yet).
proc convert_to_mebi {size} {
660 661 662 663 664 665
    # Default to bytes
    if {[scan $size "%f%s" sz unit] == 1} {
	set unit B
    }

    switch -- $unit {
666 667 668 669 670 671 672 673 674
	B   {set val [expr int($sz / pow(2,20))}
	KB  {set val [expr int($sz * pow(10,3) / pow(2,20))]}
	KiB {set val [expr int($sz / pow(2,10))]}
	MB  {set val [expr int($sz * pow(10,6) / pow(2,20))]}
	MiB {set val [expr int($sz)]}
	GB  {set val [expr int($sz * pow(10,9) / pow(2,20))]}
	GiB {set val [expr int($sz * pow(2,10))]}
	TB  {set val [expr int($sz * pow(10,12) / pow(2,20))]}
	TiB {set val [expr int($sz * pow(2,20))]}
675 676 677 678 679 680 681 682 683
	default {
	    perror "Unknown size unit $unit."
	    set val 0
	}
    }

    return $val
}

Leigh Stoller's avatar
Leigh Stoller committed
684 685
# We now have all our infrastructure in place.  We are ready to load
# the NS file.
Mac Newbold's avatar
Mac Newbold committed
686

687 688
if { ${GLOBALS::errors} != 1 } {
    file copy -force ${GLOBALS::tbcompat} .
689
    real_source ${GLOBALS::nsfile}
690 691 692 693

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

Leigh Stoller's avatar
Leigh Stoller committed
695
}
696

Leigh Stoller's avatar
Leigh Stoller committed
697
exit ${GLOBALS::errors}