nse.parse.tcl.in 19.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
#!/usr/local/bin/otclsh

#
# EMULAB-COPYRIGHT
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
# All rights reserved.
#

######################################################################
# nse.parse.tcl
#
# This is the testbed nse parser.  It takes a project id and an experiment
# id. It will parse the code in the nseconfigs table to partition the
# global per experiment nseconfigs into code that needs to go on the
# different partitions
# It also displays warnings for unsupported functionality.
#
# See README for extensive discussion of the structure and 
# implementation.
#
# -n will cause the parser to output error/warning messages and exit
#    without updating the database.
######################################################################

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

# Initial Procedures

###
# 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."}
#  }
###
proc var_import {varspec} {
    uplevel "upvar $varspec [namespace tail $varspec]"
}

proc tb_nseparse_cleanup_and_exit {} {
    
    close ${GLOBALS::WARN_FILE}    
    exit ${GLOBALS::errors}
}

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

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

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

if {${GLOBALS::anonymous} && ([llength $argv] != 0)} {
    puts stderr "Syntax: $argv0 \[-q\] -n -a"
    exit 1
} elseif {(! ${GLOBALS::anonymous}) && ([llength $argv] != 4)} {
    puts stderr "Syntax: $argv0 \[-q\] \[-n \[-a\]\] pid eid defsfile nsfile"
    exit 1
}

# Now we can set up the rest of our global variables.
namespace eval GLOBALS {
    # Remaining arguments
    if {$anonymous} {
	variable pid "PID"
	variable eid "EID"
	variable defsfile [lindex $argv 0]
	variable nsfile [lindex $argv 1]
    } else {
	variable pid [lindex $argv 0]
	variable eid [lindex $argv 1]
	variable defsfile [lindex $argv 2]
	variable nsfile [lindex $argv 3]
    }

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

    # This is used to store the last tcl command that was evaluated
    # so that nseconfigs could be appropriately rebuilt
    variable last_cmd {}

    # Some settings taken from configure.
    variable tbroot @prefix@
    variable libnsedir @prefix@/lib/nseparse

    # Is 1 if any errors have occured so far.
    variable errors 0
    
    # This is a counter used by the 'new' procedure to create null
    # classes.
    variable new_counter 0

    # This is the file handler for the warnings file
    variable WARN_FILE [open "$eid.warnings" a]

    # This is indexed by pnode and contains duplex-link and make-lan
    # code for each pnode that will be appended to the nseconfigs
    # that will go into the DB
    variable nseconfiglanlinks

}

# We want NullClass to instantiate proxy objects for real nodes
# that were present outside make-simulated and
# thus won't be part of the global nseconfigs that
# gets evaluated. This ensures we can ignore code that references
# real nodes
source ${GLOBALS::libnsedir}/nse.null.tcl
source ${GLOBALS::defsfile}

namespace eval GLOBALS {
    # virtual to physical mapping
    variable v2pmap
    # n(0) -> n-0 and n-0 -> n(0) mappings
    variable v2vmap
    # physical to virtual mapping for nodes that support sim nodes
    variable p2vmapsim
    # virtual node to type
    variable v2type

    variable v2pmap

    variable vnodetoip
    variable iptovnode
    
    # The v2pmapinfo contains sim vnode names that are mapped to
    # the physical nodes having a generated vname in the
    # reserved table. For sim nodes, they correspond to
    # nsenode0, 1 and so on. v2pmapinfo came from the DB
    # via tcl code passed to us
    set searchid [array startsearch v2pmapinfo]
    set vnode [array nextelement v2pmapinfo $searchid]
    while { $vnode != {} } {

	set vtype [lindex $v2pmapinfo($vnode) 0]
	set ipports [lindex $v2pmapinfo($vnode) 1]
	# Note that pnode here is the vname from the reserved table
	# as opposed to physical pc. This is appropriate since
	# "tmcc nseconfigs" from a pnode will be based on the vname
	# from the reserved table
	set pnode [lindex $v2pmapinfo($vnode) 2]

	set v2pmap($vnode) $pnode

	if { $vtype == "sim" } {
	    lappend p2vmapsim($pnode) $vnode
	} else {
	    # For nodes not in nseconfigs (thus are non "sim" nodes),
	    # we create a set of dummy NullClass objects so that
	    # if they are references in code inside the make-simulated
	    # block, we just either ignore it or store useful information
	    # in case one of them is of "sim"
	    set $vnode [NullClass $vnode RealNode]
	}
	set v2type($vnode) $vtype

	set portiplist [split $ipports " "]
	foreach portip $portiplist {
	    scan $portip "%\[^:]:%s" port ip

	    # used later in breaking duplex links and lans
	    set ips("$vnode:$port") $ip
	    lappend vnodetoip($vnode) $ip
	    set iptovnode($ip) $vnode
	}
	set vnode [array nextelement v2pmapinfo $searchid]
    }
    array donesearch v2pmapinfo $searchid

    set searchid [array startsearch lanlinks]
    set lanlink [array nextelement lanlinks $searchid]
    while {  $lanlink != {} } {
	if { [llength $lanlinks($lanlink)] == 2 } {
	    scan [lindex $lanlinks($lanlink) 0] "%\[^:]:%s" node1 port1
	    scan [lindex $lanlinks($lanlink) 1] "%\[^:]:%s" node2 port2	    
	    set nodes12 [lsort [list $node1 $node2]]
	    set link_map($node1:$node2) $lanlink
	    set link_map($node2:$node1) $lanlink
	} else {
	    set nodelist {}
	    foreach member [lsort $lanlinks($lanlink)] {
		scan $member "%\[^:]:%s" node port
		lappend nodelist $node
	    }
	    set lanstring [join [lsort $nodelist] ":"]
	    set lan_map($lanstring) $lanlink
	}
	set lanlink [array nextelement lanlinks $searchid]
    }
    array donesearch lanlinks $searchid

    variable event_list
    variable virt_agents_list
}

###
# perror <msg>
# Print an error message and mark as failed run.
###
proc perror {msg} {
    var_import ::GLOBALS::errors 

    global argv0
    puts stderr "*** $argv0: "
    puts stderr "    $msg"
    set errors 1
}

###
# punsup {msg}
# Print an unsupported message.
###
proc punsup {msg} {
    var_import ::GLOBALS::verbose
    var_import ::GLOBALS::WARN_FILE

    # If this was a true error in specifying
    # the simulation, it would have been
    # caught when run with NSE
    if {$verbose == 1} {
	puts stderr "*** WARNING: Unsupported NSE Statement!"
	puts stderr "    $msg"
	puts $WARN_FILE "*** WARNING: Unsupported NSE Statement!"
	puts $WARN_FILE "    $msg"
    }
}	

# Load all our classes
source ${GLOBALS::libnsedir}/nse.sim.tcl
source ${GLOBALS::libnsedir}/nse.node.tcl
source ${GLOBALS::libnsedir}/nse.agent.tcl
277
source ${GLOBALS::libnsedir}/tb_compat.tcl
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

##################################################
# 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
    var_import GLOBALS::last_cmd
    var_import GLOBALS::v2vmap

    # 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]\}]
    } elseif {($last_class == {})} {
	return [uplevel real_set \{[lindex $args 0]\} \{[lindex $args 1]\}]
    }

    real_set var [lindex $args 0]
    real_set val [lindex $args 1]

    # 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\}]

    $last_class set objname $var

    regsub -all {[\(]} $var {-} out
    real_set sub [regsub -all {[\)]} $out {} outname]
    if { $sub > 0 } {
	real_set v2vmap($var) $outname
	real_set v2vmap($outname) $var
    } else {
	real_set v2vmap($var) $var
    }
    
    # Reset last_class in all cases.
    real_set last_class {}
    real_set last_cmd {}
    
    return $ret
}

###
# 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
    real_set new_classes(Simulator) {}
    real_set new_classes(Node) {}
    real_set new_classes(Lan) {}
    real_set new_classes(Link) {}
    real_set new_classes(Agent) {}
    real_set new_classes(Application) {}
}

proc new {class args} {
    var_import GLOBALS::new_counter
    var_import GLOBALS::new_classes
    var_import GLOBALS::last_cmd

    real_set classlist [array names new_classes]

    foreach cls $classlist {
	real_set ret1 [string match "$cls" $class]
	real_set ret2 [string match "$cls/*" $class]

	if { $ret1 || $ret2 } {
	    real_set id $cls[incr new_counter]
	    real_set last_cmd "\[new $class $args]"
	    eval $cls $id $args
	    $id set classname $class
	    return $id
	}
    }

    real_set id null[incr new_counter]
    NullClass $id $class
    return $id
}

# We now have all our infrastructure in place.  We are ready to load
# the NSE file from what was passed to us

if { ${GLOBALS::errors} != 1 } {
    source ${GLOBALS::nsfile}
}

if { ${GLOBALS::impotent} == 1 } {
    # this will clean up and exit
    tb_nseparse_cleanup_and_exit
}

namespace eval GLOBALS {

    # global nseconfig one per pnode
    variable nseconfig 
    
    # now get nseconfig per pnode and put it into the db accordingly
    real_set sim [lindex [Simulator info instances] 0]
    foreach pnode [array names p2vmapsim] {
	real_set nseconfig($pnode) {}
408 409 410
	if { [$sim info vars rtslop] != {} } {
		append nseconfig($pnode) "Scheduler/RealTime set maxslop_ [$sim set rtslop]\n\n"
	}
411
	append nseconfig($pnode) "set [$sim set objname] [$sim set createcmd]\n"
412 413 414
	# Since we will be adding IP address based routes, any ns default
	# routing causes problems
	append nseconfig($pnode) "\$[$sim set objname] rtproto Manual\n"
415 416
	append nseconfig($pnode) \
		    "\$[$sim set objname] set tbname \{$v2vmap([$sim set objname])\}\n"
417
	append nseconfig($pnode) "[$sim set nseconfig]\n\n"
418 419 420
	
	# XXX temporary hack
	append nseconfig($pnode) "Agent/TCP set QOption_ 1\n\n"
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 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 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
    }
    
    real_set ignore_class_vars(id_counter) {}
    real_set ignore_class_vars(nseconfig) {}
    real_set ignore_class_vars(classname) {}
    real_set ignore_class_vars(objname) {}
    real_set ignore_class_vars(createcmd) {}
    real_set ignore_class_vars(agentlist) {}
    real_set ignore_class_vars(node) {}
    real_set ignore_class_vars(application) {}
    real_set ignore_class_vars(destination) {}
    real_set ignore_class_vars(next_portnumber) {}
    real_set ignore_class_vars(ip) {}
    real_set ignore_class_vars(port) {}
    real_set ignore_class_vars(agent) {}
   
    foreach node [Node info instances] {
	real_set pnode $v2pmap($v2vmap([$node set objname]))
	
	if { $pnode != {} } {
	    append nseconfig($pnode) "set [$node set objname] [$node set createcmd]\n"

	    #foreach ip $vnodetoip([$node set objname]) {
		#append nseconfig($pnode) "\$[$sim set objname] add-ip $ip \$[$node set objname]\n"
	    #}

	    append nseconfig($pnode) \
		    "\$[$node set objname] set tbname \{$v2vmap([$node set objname])\}\n"

	    foreach var [$node info vars] {
		if { [info exists ignore_class_vars($var)] } {
		    continue
		}
		
		real_set array_names [$node array name $var]
		if { $array_names != {} } {
		    foreach name $array_names {
			append nseconfig($pnode) \
				"\$[$node set objname] set $var($name) \{[$node set $var($name)]\}\n"
		    }
		} else {
		    append nseconfig($pnode) \
			    "\$[$node set objname] set $var \{[$node set $var]\}\n"
		}
	    }
	    append nseconfig($pnode) "[$node set nseconfig]\n\n"
	    
	}
    }

    foreach pnode [array names nseconfiglanlinks] {
	append nseconfig($pnode) $nseconfiglanlinks($pnode)
    }
    
    foreach pnode [array names nseconfigrlinks] {
	append nseconfig($pnode) "\n"
	append nseconfig($pnode) $nseconfigrlinks($pnode)
    }

    variable link_visited
    # links between sim and real nodes
    foreach nodes [array names link_map] {
	scan $nodes "%\[^:]:%s" node0 node1
	if { ($v2type($node0) != "sim" && $v2type($node1) != "sim") ||
  	     ($v2type($node0) == "sim" && $v2type($node1) == "sim") } {
	    # We only want links between sim and real nodes, not
	    # sim-to-sim or real-to-real
	    continue
	}
	if { [info exists link_visited($node0:$node1)] } {
	    continue
	}
	real_set link_visited($node0:$node1) 1
	real_set link_visited($node1:$node0) 1
	real_set lanlink $link_map($node0:$node1)
	scan [lindex $lanlinks($lanlink) 0] "%\[^:]:%s" node0 port0
	scan [lindex $lanlinks($lanlink) 1] "%\[^:]:%s" node1 port1
	if { $v2type($node0) == "sim" } {
	    real_set srcvnode $node0
	    real_set srcport $port0
	    real_set dstvnode $node1
	    real_set dstport $port1
	} else {
	    real_set srcvnode $node1
	    real_set srcport $port1
	    real_set dstvnode $node0
	    real_set dstport $port0
	}
	real_set pnode $v2pmap($srcvnode)
	real_set srcip $ips("$srcvnode:$srcport")
	real_set dstip $ips("$dstvnode:$dstport")
	real_set srcvnode_actual $v2vmap($srcvnode)
	append nseconfig($pnode) "set $lanlink \[\$[$sim set objname] rlink \$$srcvnode_actual $dstip]\n"
	append nseconfig($pnode) "\$\{$lanlink\} set-ip $srcip\n"
    }

    foreach pnode [array names p2vmapsim] {
	append nseconfig($pnode) "\n"
    }

    foreach agent [Agent info instances] {
	set node [$agent get_node]
	set vnode_actual $v2vmap([$node set objname])
	set pnode $v2pmap($vnode_actual)
	append nseconfig($pnode) "set [$agent set objname] [$agent set createcmd]\n"

	append nseconfig($pnode) \
		"\$[$agent set objname] set tbname \{$v2vmap([$agent set objname])\}\n"
	
	foreach var [$agent info vars] {
	    if { [info exists ignore_class_vars($var)] } {
		continue
	    }

	    real_set array_names [$agent array name $var]
	    if { $array_names != {} } {
		foreach name $array_names {
		    append nseconfig($pnode) \
			    "\$[$agent set objname] set $var($name) \{[$agent set $var($name)]\}\n"
		}
	    } else {
		append nseconfig($pnode) \
			"\$[$agent set objname] set $var \{[$agent set $var]\}\n"
	    }
	}
	
	if { [$agent set nseconfig] != {} } {
	    append nseconfig($pnode) [$agent set nseconfig]
	}
	set app [$agent set application]
	if { $app != {} } {
	    append nseconfig($pnode) "set [$app set objname] [$app set createcmd]\n"

	    append nseconfig($pnode) \
		    "\$[$app set objname] set tbname \{$v2vmap([$app set objname])\}\n"
	    
	    foreach var [$app info vars] {
		if { [info exists ignore_class_vars($var)] } {
		    continue
		}

		real_set array_names [$app array name $var]
		if { $array_names != {} } {
		    foreach name $array_names {
			append nseconfig($pnode) \
				"\$[$app set objname] set $var($name) \{[$app set $var($name)]\}\n"
		    }
		} else {
		    append nseconfig($pnode) \
			    "\$[$app set objname] set $var \{[$app set $var]\}\n"
		}
	    }
	    
	    if { [$app set nseconfig] != {} } {
		append nseconfig($pnode) [$app set nseconfig]
	    }
	}
    }


    # lans

    $sim spitxml_init
    foreach pnode [array names nseconfig] {
	
	append nseconfig($pnode) "set simcode_present 1\n\n"
	
	$sim spitxml_data "nseconfigs" [list "vname" "nseconfig"] [list $pnode $nseconfig($pnode)]
    }

    if { [array exists event_list] } {
	real_set searchid [array startsearch event_list]
	real_set vname [array nextelement event_list $searchid]
	while { $vname != {} } {

	    foreach event $event_list($vname) {
597 598 599 600 601 602 603
               if {[string equal [lindex $event 0] "swapout"]} {
                       set event [lreplace $event 0 0 0]
                       set triggertype "SWAPOUT"
               } else {
                       set triggertype "TIMER"
                }
               set fields [list "time" "vnode" "vname" "objecttype" "eventtype" "arguments" "atstring" "triggertype" ]
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

		real_set time [lindex $event 0]
		real_set otype [lindex $event 1]
		real_set etype [lindex $event 2]
		real_set args [lindex $event 3]
		real_set atstring [lindex $event 4]

		# We can directly do a switch on the class type of vname
		switch -- [$vname info class] {
		    Agent -
		    Application {
			real_set vnode $v2vmap([[$vname get_node] set objname])
		    }
		    Node {
			real_set vnode $v2vmap([$vname set objname])
		    }
		}

		# Here pnode is the same as what is in the
		# reserved table
		real_set pnode $v2pmap($vnode)

		if { ! [info exists virt_agents_list($vname)] } {
		    $sim spitxml_data "virt_agents" [list "vnode" "vname" "objecttype"] [list $pnode [$vname set objname] $otype]
		    # We don't want duplicate entries in the virt_agents
		    # table
		    real_set virt_agents_list($vname) {}
631 632 633 634 635 636

		    real_set agt [$vname set agent]
		    if { ! [info exists virt_agents_list($agt)] } {
		    	$sim spitxml_data "virt_agents" [list "vnode" "vname" "objecttype"] [list $pnode [$agt set objname] $otype]
			real_set virt_agents_list($agt) {}
		    }
637
		}
638
		$sim spitxml_data "eventlist" [list "time" "vnode" "vname" "objecttype" "eventtype" "triggertype" "arguments" "atstring" ] [list $time $pnode [$vname set objname] $otype $etype $triggertype $args $atstring]
639 640 641 642 643 644
	    }

	    real_set vname [array nextelement event_list $searchid]
	}
	array donesearch event_list $searchid
    }
645 646 647 648 649
    real_set otype $objtypes(NSE)
    foreach pnode [array names p2vmapsim] {
	$sim spitxml_data "virt_agents" [list "vnode" "vname" "objecttype"] [list $pnode $pnode $otype]
    }

650 651 652
    $sim spitxml_finish
}
    
653
tb_nseparse_cleanup_and_exit