libptop_new.pm.in 63.1 KB
Newer Older
1 2
#!/usr/bin/perl -wT
#
3
# Copyright (c) 2010-2012 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{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/>.
# 
# }}}
23
#
24
package libptop_new;
25 26 27

use strict;
use Exporter;
28
use lib "@prefix@/lib";
29
use Node;
30
use NodeType;
31
use Interface;
32 33
use OSinfo;
use libdb qw(TBGetSiteVar DBQueryFatal TBResolveNextOSID);
34 35
use vars qw(@ISA @EXPORT @EXPORT_OK);

36 37 38 39
sub FD_ADDITIVE  { return "FD_ADDITIVE"; }
sub FD_FIRSTFREE { return "FD_FIRSTFREE"; }
sub FD_ONCEONLY  { return "FD_ONCEONLY"; }

40 41 42 43
our $PGENISUPPORT = @PROTOGENI_SUPPORT@;
my $OURDOMAIN = "@OURDOMAIN@";
my $MAINSITE  = @TBMAINSITE@;

44 45 46 47 48 49 50 51 52 53 54 55
my $cmuuid = TBGetSiteVar('protogeni/cm_uuid');
my $cmurn = "";
if ($PGENISUPPORT) {
    require GeniHRN;
    require GeniXML;
    $cmurn = GeniHRN::Generate($OURDOMAIN, "authority", "cm");
}


@ISA = "Exporter";
@EXPORT = qw( );

56
my $single_node = undef;
57 58
my $user_project = undef;
my $exempt_eid = undef;
59
my $available_only = 1;
60
my $print_widearea = 0;
61 62 63 64
my $print_shared = 0;
my $print_virtual = 0;
my $print_sim = 0;
my $genimode = 0;
65 66
my $delaycap_override = undef;
my $multiplex_override = undef;
67
my $debug = 0;
68
my $usecontrol = 0;
69 70 71 72

my $default_longitude = undef;
my $default_latitude = undef;
my $default_country = undef;
73

74 75 76 77 78 79 80 81
my %wireless_protocols = ("80211" => 1,
			  "80211a" => 1,
			  "80211b" => 1,
			  "80211g" => 1,
			  "flex900" => 1,
			  "xcvr2450" => 1);


82
our %nodeList = ();
83
our %nodeTypeList = ();
84
our %linkList = ();
85
#our %interfaceList = ();
86

87 88 89
# Table of which types the user project is allowed to have.
# Keyed by type where 1 = allowed, 0 = denied, and ! exists = allowed
our %permissions = ();
90

91 92
# Map from auxtype names to real type names
our %auxtypemap = ();
93

94 95
# Map from type names to lists of features
our %typefeatures = ();
96 97 98 99
# Mapping from types to the 'forwarding_protocols' node_type_attribute
our %type_forwarding = ();
# Mapping from types to the 'dedicated_widearea' node_type_attribute
our %type_dedicatedwa = ();
100

101 102
# Keyed by osids. Contains OsInfo structures
our %osinfo = ();
103

104 105 106
# Mapping between types and osids, and vice versa. Hash of arrays.
our %type_osid = ();
our %osid_type = ();
107

108 109
# Mapping between an osid and its subosids. Hash of arrays.
our %osid_subosid = ();
110

111 112 113 114
# Mapping between interface_type:base and bandwidth values based on
# interface_capabilities table. 'base' is the capkey prefix to one of
# the defspeed keys.
our %itype_bw = ();
115 116 117
# Mapping between interface_type and a list of protocols supported.
# Only includes wireless protocols (those listed in %wireless_protocols).
our %itype_protos = ();
118

119 120 121 122 123
# Mapping between interfaces and columns
our %iface_type = ();
our %iface_role = ();
our %iface_ip = ();

124 125 126 127 128 129
# Keyed by $node:$iface, this is the amount of shared bandwidth
# available on that interface. Overrides the remaining_bandwidth from
# Interface. It should be the remaining bandwidth with the
# exempt_eid's bandwidth added back in.
our %sharedbw = ();

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
sub ProcessArgs($)
{
    my ($optionsRef) = @_;
    my %options = %{ $optionsRef };
    if (defined($options{"p"})) {
	$user_project = $options{"p"};
    }
    if (defined($options{"e"})) {
	$exempt_eid = $options{"e"};
	usage()
	    if (!defined($user_project));
    }
    if (defined($options{"v"})) {
	$print_virtual = 1;
    }
    if (defined($options{"r"})) {
	$print_widearea = 1;
    }
    if (defined($options{"S"})) {
	$print_sim = 1;
    }
    if (defined($options{"h"})) {
	$print_shared = 1;
    }
    if (defined($options{"a"})) {
	$available_only = 0;
    }
    if (defined($options{"m"})) {
	$multiplex_override = $options{"m"};
    }
    if (defined($options{"d"})) {
	$debug = 1;
    }
    if (defined($options{"c"})) {
	$delaycap_override = $options{"c"};
    }
166 167 168
    if (defined($options{"C"})) {
	$usecontrol = 1;
    }
169 170 171 172 173
    if (defined($options{"g"})) {
	$genimode = 1;
	$print_shared = 1;
	$print_virtual = 1;
    }
174 175 176
    if (defined($options{"1"})) {
	$single_node = $options{"1"};
    }
177 178
}

179 180 181 182 183 184 185
#
# Initialize nodes hash based on nodes, reservations, and node_status tables
#
sub LookupNodes()
{
    my $dbresult;
    my $row;
186 187 188 189 190 191 192
    # Bulk lookup on node types
    $dbresult = DBQueryFatal("select * from node_types");
    while ($row = $dbresult->fetchrow_hashref()) {
	my $name = $row->{'type'};
	$nodeTypeList{$name} = NodeType->LookupRow($name, $row);
    }

193
    # Bulk lookup on nodes table
194
    $dbresult = DBQueryFatal("select * from nodes where role='testnode' or role='testswitch' or role='widearea_switch' or role='testnodefoo' or role='ctrlswitch'");
195
    while ($row = $dbresult->fetchrow_hashref()) {
196 197
	my $node = libptop::pnode->Create($row);
	if (defined($node)) {
198 199
	    $nodeList{$row->{"node_id"}} = $node;
	    $node->node()->PreloadNodeAttributes([]);
200
	    $node->set_eventstate($row->{'eventstate'});
201 202
	    if (! defined($node->type()) && $debug) {
		die("****Node $node had a type not in node_types table.\n");
203
	    }
204 205 206
	} elsif ($debug) {
	    print STDERR "Failed to create node $node from database row.\n";
	}
207 208
    }

209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    # Bulk lookup on node_attributes table
    $dbresult = DBQueryFatal("select * from node_attributes order by node_id");
    my $lastname = undef;
    my @attributes = ();
    while ($row = $dbresult->fetchrow_hashref()) {
	my $name = $row->{"node_id"};
	if (! defined ($lastname) || $name ne $lastname) {
	    if (defined($lastname)) {
		my $node = $nodeList{$lastname};
		if (defined($node)) {
		    $node->node()->PreloadNodeAttributes(\@attributes);
		} elsif ($debug) {
		    print STDERR "Node $lastname is in node_attributes ".
			"but not nodes\n";
		}
	    }
	    $lastname = $name;
	    @attributes = ();
	}
	push(@attributes, $row);
    }

231 232 233
    # Bulk lookup on reserved table
    $dbresult = DBQueryFatal("select * from reserved");
    while ($row = $dbresult->fetchrow_hashref()) {
234 235 236
	my $nodeName = $row->{"node_id"};
	my $node = $nodeList{$nodeName};
	if (defined($node)) {
237
	    $node->set_reserved($row);
238
	} elsif ($debug) {
239
	    print STDERR "Node $nodeName is in reserved but not in nodes\n";
240
	}
241 242
    }

243
    # Bulk lookup on node_status table
244 245
    $dbresult = DBQueryFatal("select node_id, status from node_status");
    while (my ($node_id, $status) = $dbresult->fetchrow()) {
246 247 248 249 250 251
	my $node = $nodeList{$node_id};
	if (defined($node)) {
	    $node->set_status($status);
	} elsif ($debug) {
	    print STDERR "Node $node_id is in node_status but not nodes\n";
	}
252 253
    }

254
    # Bulk lookup on widearea_nodeinfo table
255 256
    $dbresult = DBQueryFatal("select * from widearea_nodeinfo");
    while ($row = $dbresult->fetchrow_hashref()) {
257 258 259 260 261 262 263 264
	my $nodeName = $row->{'node_id'};
	my $node = $nodeList{$nodeName};
	if (defined($node)) {
	    $node->set_widearea($row);
	} elsif ($debug) {
	    print STDERR "Node $nodeName is in widearea_nodeinfo but ".
		"not nodes\n";
	}
265
    }
266

267 268 269 270 271 272 273 274 275 276 277 278 279
    # Bulk lookup on node_type_attributes
    $dbresult = DBQueryFatal("select type, attrkey, attrvalue ".
			     "from node_type_attributes ".
			     "where attrkey='forwarding_protocols' ".
			     "or attrkey='dedicated_widearea'");
    while (my ($type, $key, $value) = $dbresult->fetchrow_array()) {
	if ($key eq "forwarding_protocols") {
	    $type_forwarding{$type} = $value;
	} elsif ($key eq "dedicated_widearea") {
	    $type_dedicatedwa{$type} = $value
	}
    }

280 281 282
    TBGetSiteVar('general/default_longitude', \$default_longitude);
    TBGetSiteVar('general/default_latitude', \$default_latitude);
    TBGetSiteVar('general/default_country', \$default_country);
283 284
}

285 286 287
#
# Initialize project permissions table if the user specified a project.
#
288
sub LookupPermissions()
289 290 291 292 293
{
    if (defined($user_project)) {
        # By default a type is allowed for every project. If a type is
        # in the permissions table, it is allowed only for those
        # projects which it is attached to.
294 295
	my $dbresult =
	    DBQueryFatal("select distinct type ".
296
			 "from nodetypeXpid_permissions");
297
	while (my ($type) = $dbresult->fetchrow_array()) {
298 299
	    $permissions{$type} = 0;
	}
300 301
	$dbresult =
	    DBQueryFatal("select type ".
302 303
			 "from nodetypeXpid_permissions ".
			 "where pid='$user_project'");
304
	while (my ($type) = $dbresult->fetchrow_array()) {
305 306 307 308 309
	    $permissions{$type} = 1;
	}
    }
}

310 311 312
#
# Lookup global usage counts on virtual nodes.
#
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
sub LookupGlobalCounts()
{
    my $condition = " ";
    if (defined($exempt_eid)) {
	$condition = "and not (pid='$user_project' and eid='$exempt_eid') "
    }
    my $dbresult = 
	DBQueryFatal("select phys_nodeid,count(phys_nodeid) ".
		     "from reserved as r ".
		     "left join nodes as n on n.node_id=r.node_id ".
		     "where n.node_id!=n.phys_nodeid ".
		     $condition.
		     "group by phys_nodeid");
    while (my ($node_id, $count) = $dbresult->fetchrow_array()) {
	$nodeList{$node_id}->set_globalcount($count);
    }
}

331 332 333 334
#
# Auxtypes can be associated with the main type of a node or with the
# node itself. Lookup both of these cases.
#
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
sub LookupAuxtypes()
{
    my $dbresult;
    #
    # Read the auxtypes for each type.
    # 
    $dbresult = DBQueryFatal("select auxtype,type from node_types_auxtypes");
    while (my ($auxtype,$type) = $dbresult->fetchrow_array()) {
	$auxtypemap{$auxtype} = $type;
    }

    #
    # Read in the node_auxtypes table for each node.
    #
    $dbresult = DBQueryFatal("select node_id, type, count from node_auxtypes");
    while (my ($node_id, $type, $count) = $dbresult->fetchrow_array()) {
351 352 353 354 355 356
	my $node = $nodeList{$node_id};
	if (defined($node)) {
	    $node->addAuxtype($type, $count);
	} elsif ($debug) {
	    print STDERR "Node $node_id in node_auxtypes but not in nodes\n";
	}
357 358 359
    }
}

360 361 362 363 364 365
#
# Find features associated both with types and with nodes. Add the
# node features immediately and save the association between types and
# features. We will add those features to nodes when we iterate over
# all the nodes during processing.
#
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
sub LookupFeatures()
{
    my $dbresult;
    #
    # Read the features table for each type.
    # 
    $dbresult =
	DBQueryFatal("select type, feature, weight from node_type_features");
    while (my ($type, $feature, $weight) = $dbresult->fetchrow()) {
	if (! exists($typefeatures{$type})) {
	    $typefeatures{$type} = [];
	}
	push(@{ $typefeatures{$type} }, $feature.":".$weight);
    }

    #
    # Read the features table for each individual node
    #
    $dbresult =
	DBQueryFatal("select node_id, feature, weight from node_features");
    while (my ($node_id, $feature, $weight) = $dbresult->fetchrow()) {
	my $pnode = $nodeList{$node_id};
388
	if (defined($pnode)) {
389 390 391
#	    if ($pnode->iswidearea()
#		|| ($pnode->islocal() && ! $pnode->is_shared())) {
	    if ($pnode->willPrint()) {
392 393 394 395
		$pnode->addFeatureString($feature.":".$weight);
	    }
	} elsif ($debug) {
	    print STDERR "Node $node_id is in node_features but not nodes\n";
396 397 398 399
	}
    }
}

400 401 402 403
#
# Bulk lookup on the os_info table. Find the mapping between types and
# osids and between osids and subosids.
#
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
sub LookupOsids()
{
    my $dbresult;
    my $row;
    # Bulk lookup on os_info table
    $dbresult = DBQueryFatal("select * from os_info");
    while ($row = $dbresult->fetchrow_hashref()) {
	my $os = OSinfo->LookupRow($row);
	$osinfo{$os->osid()} = $os;
    }

    #
    # Read the table of which image types are supported on which
    # hardware - we limit this to global images and ones that match
    # the PID (if given) We do this limiting for two reasons:
    # 1) To avoid an explosion in the number of features for nodes
    # 2) To avoid information leaks, allowing projects to see each
    #    other's images
    # 
    my $pidos = "";
    if (defined($user_project)) {
	$pidos = "or i.pid='$user_project'";
    }
    $dbresult =
	DBQueryFatal("select distinct oi.osid, oi.type ".
429 430
		     "from os_info as o ".
		     "left join osidtoimageid as oi on o.osid=oi.osid ".
431 432 433
		     "left join images as i on oi.imageid = i.imageid ".
		     "where i.global = 1 $pidos");
    while (my ($osid, $typename) = $dbresult->fetchrow()) {
434 435 436 437
	if (defined($typename)) {
	    hashpush(\%type_osid, $typename, $osid);
	    hashpush(\%osid_type, $osid, $typename);
	}
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
    }

    #
    # We also have to resolve the 'generic' OSIDs, which use the
    # nextosid field to redirect to another OSID
    #
    $dbresult = DBQueryFatal("select osid from os_info where " .
			     "nextosid is not null");
    while (my ($osid) = $dbresult->fetchrow()) {
	#
	# Check to see if they were allowed to use the real OSID
	#
	my $realosid = TBResolveNextOSID($osid, $user_project, $exempt_eid);
	if (exists($osid_type{$realosid})) {
	    foreach my $type (@{ $osid_type{$realosid} }) {
		hashpush(\%type_osid, $type, $osid);
	    }
	}
    }

    #
    # For subOS support (i.e., vnode OSes running atop vhost OSes), we
    # have to check both the subosid and all the parent_osid it can
    # run on.
    #
    my $subosidquery = "select distinct o.osid,o.parent_osid ".
	"from os_submap as o " .
	"left join osidtoimageid as oi1 on o.osid = oi1.osid " .
	"left join osidtoimageid as oi2 on o.parent_osid = oi2.osid " .
	"left join images as i1 on oi1.imageid = i1.imageid ".
	"left join images as i2 on oi2.imageid = i2.imageid ".
469
	"where (i1.imageid is null or i1.global = 1";
470 471 472 473 474 475 476 477 478 479 480 481 482 483
    if (defined($user_project)) {
	$subosidquery .= " or i1.pid='$user_project'";
    }
    $subosidquery .= ") and (i2.global = 1";
    if (defined($user_project)) {
	$subosidquery .= " or i2.pid='$user_project'";
    }
    $subosidquery .= ")";
    $dbresult = DBQueryFatal($subosidquery);
    while (my ($subosid,$osid) = $dbresult->fetchrow()) {
	hashpush(\%osid_subosid, $osid, $subosid);
    }
}

484
#
485 486 487 488 489 490 491
# Lookup interfaces and interface types. Important for determining
# bandwidth on links below and for printing out on nodes in genimode.
#
sub LookupInterfaces()
{
    my $dbresult;
    my $row;
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
    $dbresult =
	DBQueryFatal("SELECT type,capkey,capval from interface_capabilities ".
		     "where capkey='protocols' or capkey like '%_defspeed'");
    while (my ($type,$capkey,$capval) = $dbresult->fetchrow_array()) {
	if ($capkey eq "protocols") {
	    my @protos = split(",", $capval);
	    foreach my $proto (@protos) {
		if (exists($wireless_protocols{$proto})) {
		    hashpush(\%itype_protos, $type, $proto);
		}
	    }
	} elsif ($capkey =~ /^([-\w]+)_defspeed$/) {
	    $itype_bw{$type.":".$1} = $capval;
	} else {
	    die("Improper defspeed $capval for $type!\n");
	}
    }

510 511 512 513 514 515 516
    $dbresult = DBQueryFatal("select i.node_id, i.iface, i.IP, i.role, ".
			     "i.interface_type, s.remaining_bandwidth ".
			     "from interfaces as i ".
			     "left join interface_state as s ".
			     "on i.node_id=s.node_id and i.iface=s.iface");
    while (my ($nodeName, $ifaceName, $ip, $role,
	       $type, $bw) = $dbresult->fetchrow_array()) {
517
	my $key = "$nodeName:$ifaceName";
518 519 520 521 522 523 524
	if (defined($bw)) {
	    $sharedbw{$key} = $bw;
	}
	$iface_ip{$key} = $ip;
	$iface_role{$key} = $role;
	$iface_type{$key} = $type;

525 526 527 528 529 530
	my $node = $nodeList{$nodeName};
	if (defined($node)) {
	    $node->addInterface($ifaceName);
	} elsif ($debug) {
	    print STDERR "Node $nodeName is in interfaces but not nodes\n";
	}
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
    }

    if (defined($exempt_eid)) {
	AddSharedBandwidth();
    }
}

#
# Bandwidth for shared nodes is calculated differently and already
# subtracts out all reserved capacity. So if the user specifies an
# exempt_eid, we must add back in that experiment's shared bandwidth.
#
sub AddSharedBandwidth()
{
    my $experiment = Experiment->Lookup($user_project, $exempt_eid);
    if (! defined($experiment)) {
	die("Could not look up experiment $user_project/$exempt_eid\n");
    }
    # Read the vinterfaces table to get any bandwidth in use by
    # this experiment.
    my $exptidx   = $experiment->idx();
    my $pstateDir = $experiment->WorkDir() . "/pstate";

    # This awful mess of creating a temporary table from a file on
    # boss is because the vinterfaces table itself will be purged of
    # experiment data when we are doing a swapmod. The only place
    # where the data remains is in the backup file.
    DBQueryFatal("create temporary table if not exists ".
		 "vinterfaces_${exptidx} like vinterfaces");

    DBQueryFatal("delete from vinterfaces_${exptidx}");
    
    DBQueryFatal("load data infile '$pstateDir/vinterfaces' ".
		 "into table vinterfaces_${exptidx}")
	    if (-e "$pstateDir/vinterfaces");
    
    my $result = DBQueryFatal("select * from vinterfaces_$exptidx ".
			      "where exptidx=$exptidx");

    while (my $row = $result->fetchrow_hashref()) {
	my $node      = $row->{'node_id'};
	my $iface     = $row->{'iface'};
	my $bandwidth = $row->{'bandwidth'};

	next
	    if (!defined($iface) || $bandwidth <= 0);
	next
578
	    if (! exists($iface_type{"$node:$iface"}));
579 580 581 582 583 584 585 586 587

	$sharedbw{"$node:$iface"} += $bandwidth;
    }
}

#
# Lookup wires. Let nodes know which other nodes they are connected to
# for later use when adding features. Add links between nodes for
# later printing.
588 589
#
sub LookupLinks()
590
{
591 592 593
    my $dbresult;
    my $row;
    $dbresult =
594
	DBQueryFatal("select w.type, w.node_id1, w.card1, w.port1, i1.iface, ".
595
		     "w.node_id2, w.card2, w.port2, i2.iface, w.trunkid ".
596
		     "from wires as w ".
597 598 599 600 601 602
		     "left join interfaces as i1 ".
		     "on w.node_id1=i1.node_id and w.card1=i1.card ".
		     "and w.port1=i1.port ".
		     "left join interfaces as i2 ".
		     "on w.node_id2=i2.node_id and w.card2=i2.card ".
		     "and w.port2=i2.port ".
603
		     "where w.logical=0");
604 605
    while (my ($type, $node_id1, $card1, $port1, $iface1, $node_id2, 
	       $card2, $port2, $iface2, $trunkid) = $dbresult->fetchrow()) {
606
	# Add connections between nodes for features later.
607 608 609
	if ($type ne 'Unused') {
	    my $node1 = $nodeList{$node_id1};
	    my $node2 = $nodeList{$node_id2};
610
	    if (defined($node1) && defined($node2)) {
611
		$node1->addConnection($node_id2);
612 613 614 615 616 617 618 619
		$node2->addConnection($node_id1);
	    } else {
		if (! defined($node1) && $debug) {
		    print STDERR "Node $node_id1 is in wires but not nodes\n";
		} elsif (! defined($node2) && $debug) {
		    print STDERR "Node $node_id2 is in wires but not nodes\n";
		}
		next;
620 621
	    }
	}
622 623
	if ($type eq 'Trunk') {
	    # This is a switch/switch link potentially trunked to other wires.
624
	    if (! defined($iface1) && $debug) {
625 626 627
		print STDERR "Undefined interface for ".
		    "$node_id1:$card1:$port1\n";
	    }
628
	    if (! defined($iface2) && $debug) {
629 630 631
		print STDERR "Undefined interface for ".
		    "$node_id2:$card2:$port2\n";
	    }
632 633 634 635 636 637 638 639 640 641
	    my $source = $node_id1;
	    my $dest = $node_id2;
	    my $sourcebw = SwitchBandwidth($node_id1, $iface1);
	    my $destbw = SwitchBandwidth($node_id2, $iface2);
	    if (! defined($trunkid) || $trunkid == 0) {
		die("DATABASE ERROR: Wire from $node_id1:$card1.$port1 to ".
		    "$node_id2:$card2.$port2 is of type trunk ".
		    "but does not have a valid trunkid.");
	    }
	    my $name = "link-trunk:$trunkid";
642 643 644 645 646
	    if (! exists($linkList{$name})) {
		$linkList{$name} = libptop::plink->CreateTrunk($name,
							       $source,
							       $dest);
	    }
647 648 649 650 651 652 653 654 655 656 657 658
	    my $link = $linkList{$name};
	    my $same_ends = (($source eq $link->source()
			      && $dest eq $link->dest()) ||
			     ($source eq $link->dest()
			      && $dest eq $link->source()));
	    if (! $same_ends) {
		die("DATABASE ERROR: Wire from $node_id1:$card1.$port1 to ".
		    "$node_id2:$card2.$port2 shares a trunkid ".
		    "with links from ".$link->source()." to ".
		    $link->dest());
	    }
	    $link->addTrunk($sourcebw, $destbw);
659 660
	} elsif ((!$usecontrol && $type eq 'Node') ||
		 ($usecontrol && $type eq 'Control')) {
661
	    # Add a switch/node or node/node link.
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
	    my $node1 = $nodeList{$node_id1};
	    my $node2 = $nodeList{$node_id2};
	    if (defined($node1) && defined($node2)
		&& $node1->willPrint() && $node2->willPrint()) {

		my $link = libptop::plink->Create();
		$link->set_source($node_id1);
		$link->set_sourceif($iface1);
		if (! defined($iface1)) {
		    $link->set_sourceif($card1.".".$port1);
		}
		$link->set_sourcecard($card1);
		$link->set_sourceport($port1);
		$link->set_dest($node_id2);
		$link->set_destif($iface2);
		if (! defined($iface2)) {
		    $link->set_destif($card2.".".$port2);
		}
		$link->set_destcard($card2);
		$link->set_destport($port2);
		$link->processLink();
		$linkList{$link->name()} = $link;
684 685
	    }
	}
686
    }
687 688
}

689 690 691 692 693 694 695 696 697 698 699 700 701
sub AddFakeNodes()
{
    my $node;
    if ($print_widearea || $genimode) {
	$node = libptop::pnode->CreateCloud("internet", "border", ["ipv4"]);
	$nodeList{$node->name()} = $node;
    }
    my @protos = keys(%wireless_protocols);
    $node = libptop::pnode->CreateCloud("airswitch", "air",
					\@protos);
    $nodeList{$node->name()} = $node;
}

702 703 704 705
# Accessors
sub Nodes() { return \%nodeList; }
sub Links() { return \%linkList; }

706 707 708 709 710 711 712 713
sub TypeAllowed($)
{
    my ($type) = @_;
    return (! defined($user_project)
	    || ! exists($permissions{$type})
	    || $permissions{$type});
}

714 715 716 717
sub NodeBandwidth($$)
{
    my ($nodename, $iface) = @_;
    my $node = $nodeList{$nodename};
718
    if ($node->is_shared()) {
719 720 721 722 723 724 725 726 727 728 729 730 731
	return ShareBandwidth($nodename, $iface);
    } else {
	return TypeBandwidth($nodename, $iface, "ethernet");
    }
}

sub SwitchBandwidth($$)
{
    my ($nodename, $iface) = @_;
    my $node = $nodeList{$nodename};
    # Default to 100 MBit. The default should never be used unless
    # there is an error in the database. If the wires table references
    # a node which doesn't exist, for instance.
732
    my $result = 100000;
733
    if (defined($node)) {
734
	my $basetype = $node->get_basetype();
735 736 737 738 739 740 741 742 743 744 745 746 747 748
	$result = TypeBandwidth($nodename, $iface, $basetype);
    }
    return $result;
}

sub TypeBandwidth($$$)
{
    my ($node, $iface, $base) = @_;
    # Default to 100 MBit. The default should never be used unless
    # there is an error in the database. If a trunk wire exists
    # without corresponding interface rows for both ends, for
    # instance.
    my $result = 100000;
    if (defined($iface)) {
749 750
	if (exists($iface_type{"$node:$iface"})) {
	    my $type = $iface_type{"$node:$iface"};
751 752 753 754 755
	    if (exists($itype_bw{"$type:$base"})) {
		$result = $itype_bw{"$type:$base"};
	    }
	}
    }
756
    return $result;
757 758 759 760 761
}

sub ShareBandwidth($$)
{
    my ($node, $iface) = @_;
762
    my $result = 0;
763 764 765
    if (exists($sharedbw{"$node:$iface"})) {
	$result = $sharedbw{"$node:$iface"};
    }
766
    return $result;
767 768
}

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
sub make_ip($)
{
    my ($in) = @_;
    my @octets = split(/\./, $in);
    my $result = 0;
    my $i = 0;
    for (; $i < scalar(@octets); ++$i) {
	$result = (($result << 8) | $octets[$i]);
    }
    return $result;
}

sub is_routable($)
{
    my @privateIps   = (make_ip("10.0.0.0"), make_ip("172.16.0.0"),
			make_ip("192.168.0.0"));
    my @privateMasks = (8, 12, 16);

    my ($in) = @_;
    my $result = 0;
    if (defined($in) && $in =~ /\d+\.\d+\.\d+\.\d+/) {
	$result = 1;
	my $ip = make_ip($in);
	my $i = 0;
	for ($i = 0; $i < scalar(@privateIps); ++$i) {
	    my $mask = (0xffffffff << (32 - $privateMasks[$i]));
	    if (($ip & $mask) == ($privateIps[$i] & $mask)) {
		$result = 0;
	    }
	}
    }
    return $result;
}

sub is_public_interface($)
{
    my ($key) = @_;
806 807 808
    my $result = (exists($iface_role{$key})
		  && $iface_role{$key} eq "ctrl"
		  && is_routable($iface_ip{$key}));
809 810 811
    return $result;
}

812 813 814 815 816 817 818 819 820 821 822
# Push a value onto an array contained within a hash
sub hashpush($$$)
{
    my ($hashref, $key, $value) = @_;
    if (! exists($hashref->{$key})) {
	$hashref->{$key} = [$value];
    } else {
	push(@{ $hashref->{$key} }, $value);
    }
}

823 824 825 826 827 828
###############################################################################
# Physical Nodes. These contain the all of the per-node state used to
# generate ptop or xml files.

package libptop::pnode;

829
use libdb qw(TBOSID TB_OPSPID TBDB_NODESTATE_ISUP TBDB_NODESTATE_PXEWAIT
830 831
             TBDB_NODESTATE_POWEROFF TBDB_NODESTATE_ALWAYSUP NODEDEAD_EID
             NODEDEAD_PID);
832 833 834 835 836

sub Create($$)
{
    my ($class, $row) = @_;

837
    my $typename = $row->{'type'};
838 839 840
    my $self = {};

    $self->{'NODE'} = Node->LookupRow($row);
841
    $self->{'TYPE'} = $nodeTypeList{$typename};
842
    $self->{'NAME'} = $self->{'NODE'}->node_id();
843
    $self->{'PTYPES'} = [];
844
    $self->{'FEATURES'} = [];
845
    $self->{'FLAGS'} = {};
846
    $self->{'AUXTYPES'} = {};
847
    $self->{'CONNECTIONS'} = {};
848
    $self->{'INTERFACES'} = [];
849 850 851
    $self->{'SHARED'} = 0;
    $self->{'CLOUD'} = 0;
#    $self->{'GLOBALCOUNT'} = undef;
852 853 854 855 856 857 858
#    $self->{'STATUS'} = undef;
#    $self->{'WIDEAREA'} = undef;
#    $self->{'EID'} = undef;
#    $self->{'PID'} = undef;
#    $self->{'EROLE'} = undef;
#    $self->{'EVENTSTATE'} = undef;
#    $self->{'WILL_PRINT'} = undef;
859
#    $self->{'AVAILABLE'} = undef;
860 861

    bless($self, $class);
862
    $self->node()->SetNodeTypeInfo($self->type());
863 864 865
    return $self;
}

866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
sub CreateCloud($$$$)
{
    my ($class, $name, $interface, $types) = @_;
    my $self = {};

    $self->{'NAME'} = $name;
    $self->{'PTYPES'} = [];
    $self->{'FEATURES'} = [];
    $self->{'FLAGS'} = {};
    $self->{'AUXTYPES'} = {};
    $self->{'CONNECTIONS'} = {};
    $self->{'INTERFACES'} = [];
    $self->{'SHARED'} = 0;
    $self->{'CLOUD'} = 1;
    $self->{'WILL_PRINT'} = ! defined($single_node);
    $self->{'AVAILABLE'} = 1;

    bless($self, $class);

    foreach my $type (@{ $types }) {
	$self->addPType($type, undef, 1);
    }
    $self->addInterface($interface);

    return $self;
}

893
# Accessors
894
sub name($)        { return $_[0]->{'NAME'}; }
895
sub node($)        { return $_[0]->{'NODE'}; }
896
sub type($)        { return $_[0]->{'TYPE'}; }
897
sub globalcount($) { return $_[0]->{'GLOBALCOUNT'}; }
898
sub status($)      { return $_[0]->{'STATUS'}; }
899
sub is_shared($)   { return $_[0]->{'SHARED'}; }
900
sub is_cloud($)    { return $_[0]->{'CLOUD'}; }
901 902 903
sub eid($)         { return $_[0]->{'EID'}; }
sub pid($)         { return $_[0]->{'PID'}; }
sub erole($)       { return $_[0]->{'EROLE'}; }
904
sub eventstate($)  { return $_[0]->{'EVENTSTATE'}; }
905

906 907 908 909
sub set_globalcount   { $_[0]->{'GLOBALCOUNT'} = $_[1]; }
sub set_status($)     { $_[0]->{'STATUS'} = $_[1]; }
sub set_widearea($)   { $_[0]->{'WIDEAREA'} = $_[1]; }
sub set_eventstate($) { $_[0]->{'EVENTSTATE'} = $_[1]; }
910

911 912 913
sub get_basetype($)
{
    my ($self) = @_;
914
    my $result = $type_forwarding{$self->node()->type()};
915 916 917 918 919 920 921 922 923
    if (! defined($result)) {
	$result = "ethernet";
    }
    return $result;
}

sub is_exclusive($)
{
    my ($self) = @_;
924 925 926 927 928 929 930
    return ! ($self->is_shared());
}

sub set_reserved($$)
{
    my ($self, $row) = @_;
    if (defined($row)) {
931 932 933 934 935
#	$self->node()->SetReservedRow($row);
	$self->{'SHARED'} = $row->{'sharing_mode'};
	$self->{'EID'} = $row->{'eid'};
	$self->{'PID'} = $row->{'pid'};
	$self->{'EROLE'} = $row->{'erole'};
936
    }
937 938
}

939
sub widearea($$)
940
{
941 942 943
    my ($self, $key) = @_;
    my $result = undef;
    if (defined($self->{'WIDEAREA'})) {
944
	$result = $self->{'WIDEAREA'}->{$key};
945 946 947 948 949 950 951
    }
    return $result;
}

sub isreserved($)
{
    my ($self) = @_;
952 953 954
    my $pre_pid = $self->node()->reserved_pid();
    my $reserved_eid = $self->eid();
    my $reserved_pid = $self->pid();
955 956
    # A node is reserved to a project if it has a reserved_pid, and
    # that pid is not the user's pid.
957 958 959
    my $pre_reserved = defined($pre_pid)
	               && (! defined($user_project)
			   || $user_project ne $pre_pid);
960 961 962 963 964 965 966
    # A node is reserved to an experiment if it has a reserved_eid,
    # a reserved_pid, and one of those is not the user's pid/eid.
    my $exp_reserved = defined($reserved_eid)
	               && defined($reserved_pid)
	               && (! defined($exempt_eid)
			   || $reserved_eid ne $exempt_eid
			   || $reserved_pid ne $user_project);
967 968 969
    return $pre_reserved || $exp_reserved;
}

970 971 972 973 974 975 976 977 978 979
sub available($)
{
    my ($self) = @_;
    if (! defined($self->{'AVAILABLE'})) {
	$self->{'AVAILABLE'} = $self->calculateAvailable();
    }
    return $self->{'AVAILABLE'};
}

sub calculateAvailable($;$)
980 981 982
{
    my ($self, $tagRef) = @_;
    my $node = $self->node();
983

984 985
    my $typeallowed = (libptop_new::TypeAllowed($node->class())
		       && libptop_new::TypeAllowed($node->type()));
986 987 988 989 990 991

    # Nodes are free if they are nonlocal, or if they are up and
    # not-reserved, or if they are shared.
    #
    # And they must also be allowed for the current project by the
    # nodetypeXpid_permissions table.
992 993 994 995
    my $isfree = ($typeallowed
		  && (!$self->islocal()
		      || (! $self->isreserved() && $self->isup())
		      || $self->issharedhost()));
996 997 998 999 1000 1001 1002

    # And if they are a subnode, their parent must be available:

    # A bit of recursion to ensure that subnodes are only listed as
    # available if their parent is. The tags bit is to try to ensure
    # we don't loop forever if there is a subnode-loop. See also willPrint().
    if ($isfree && $node->issubnode()) {
1003
	my %tags = ();
1004 1005 1006 1007 1008 1009
	if (defined($tagRef)) {
	    %tags = %{ $tagRef };
	}
	$tags{$self->name()} = 1;
	my $parent = $node->phys_nodeid();
	if (! exists($tags{$parent})) {
1010 1011
	    $isfree = $isfree
		&& $nodeList{$parent}->calculateAvailable(\%tags);
1012 1013 1014 1015 1016 1017
	}
    }
    return $isfree;
}

sub isswitch($)
1018 1019 1020 1021 1022 1023 1024 1025
{
    if (! defined($_[0]->{'IS_SWITCH'})) {
	$_[0]->{'IS_SWITCH'} = $_[0]->calculateSwitch();
    }
    return $_[0]->{'IS_SWITCH'};
}

sub calculateSwitch($)
1026 1027 1028
{
    my ($self) = @_;
    my $role = $self->node()->role();
1029 1030 1031 1032 1033 1034 1035
    if ($usecontrol) {
	return ($role eq 'ctrlswitch' && $self->node()->isswitch());
    }
    else {
	return ($role eq 'testswitch' || $role eq 'widearea_switch'
		|| ($role eq 'testnodefoo' && $self->node()->isswitch()));
    }
1036 1037
}

1038
sub islocal($)
1039 1040 1041 1042 1043 1044 1045 1046
{
    if (! defined($_[0]->{'IS_LOCAL'})) {
	$_[0]->{'IS_LOCAL'} = $_[0]->calculateLocal();
    }
    return $_[0]->{'IS_LOCAL'};
}

sub calculateLocal($)
1047 1048 1049 1050
{
    my ($self) = @_;
    my $node = $self->node();
    my $isremotenode = $node->isremotenode();
1051
    my $wa_attrvalue = $type_dedicatedwa{$node->type()};
1052 1053 1054 1055 1056 1057
    return ( $node->role() eq 'testnode'
	     && ((! defined($isremotenode) || $isremotenode == 0)
		 || (defined($wa_attrvalue) && $wa_attrvalue == 1)));
}

sub iswidearea($)
1058 1059 1060 1061 1062 1063 1064 1065
{
    if (! defined($_[0]->{'IS_WIDEAREA'})) {
	$_[0]->{'IS_WIDEAREA'} = $_[0]->calculateWidearea();
    }
    return $_[0]->{'IS_WIDEAREA'};
}

sub calculateWidearea($)
1066 1067 1068
{
    my ($self) = @_;
    my $node = $self->node();
1069 1070
    return 0
	if ($node->role() ne 'testnode');
1071
    my $isremotenode = $node->isremotenode();
1072 1073 1074 1075 1076 1077 1078
    return 0
	if (! defined($isremotenode) || ! $isremotenode);
    my $isvirtnode = $node->isvirtnode();
    return 0
	if (defined($isvirtnode) && $isvirtnode);
    return 0
	if $node->type() eq 'pcfedphys';
1079
    my $wa_attrvalue = $type_dedicatedwa{$node->type()};
1080 1081 1082 1083 1084 1085 1086 1087 1088
    return 0
	if (defined($wa_attrvalue) && $wa_attrvalue);
    return 1;
#    return ($node->role() eq 'testnode'
#	    && defined($isremotenode)
#	    && $isremotenode == 1
#	    && (! defined($isvirtnode) || $isvirtnode == 0)
#	    && $node->type() ne 'pcfedphys'
#	    && (! defined($wa_attrvalue) || $wa_attrvalue == 0));
1089 1090
}

1091
sub issharedhost($)
1092 1093 1094
{
    my ($self) = @_;
    # In shared mode, allow allocated nodes whose sharing_mode is set.
1095 1096
    return (defined($self->erole())
	    && $self->erole() eq "sharedhost"
1097 1098 1099 1100 1101 1102
	    && $self->isup());
}

sub isup($)
{
    my ($self) = @_;
1103
    my $eventstate = $self->eventstate();
1104 1105 1106 1107 1108 1109 1110
    return defined($eventstate)
	&& ($eventstate eq TBDB_NODESTATE_ISUP()
	    || $eventstate eq TBDB_NODESTATE_PXEWAIT()
	    || $eventstate eq TBDB_NODESTATE_POWEROFF()
	    || $eventstate eq TBDB_NODESTATE_ALWAYSUP());
}

1111 1112 1113 1114 1115 1116 1117 1118 1119
sub willPrint($)
{
    my ($self) = @_;
    if (! defined($self->{'WILL_PRINT'})) {
	$self->{'WILL_PRINT'} = $self->calculateWillPrint();
    }
    return $self->{'WILL_PRINT'};
}

1120
sub calculateWillPrint($;$)
1121 1122 1123
{
    my ($self, $tagRef) = @_;
    my $node = $self->node();
1124 1125 1126
    if (! defined($node)) { 
	return 1;
    }
1127

1128
    if (defined($single_node)) {
1129
	return ($single_node eq $self->name());
1130 1131
    }

1132 1133 1134
    # In geni mode, disallow nodes tagged protogeni_exclude from being printed.
    my $geniExclude = 0;
    $node->NodeAttribute("protogeni_exclude", \$geniExclude);
1135
    my $geniok = (! defined($geniExclude) || ! $geniExclude);
1136 1137 1138 1139 1140 1141 1142
    my $result = 
	(! $available_only || $self->available())
	&& (! $genimode || $geniok)
	&& ($self->isswitch()
	    || $self->islocal()
	    || ($print_widearea && $self->iswidearea())
	    || ($print_shared && $self->issharedhost()));
1143 1144 1145 1146 1147

    # A bit of recursion to ensure that subnodes are only printed if
    # their parent is. The tags bit is to try to ensure we don't loop
    # forever if there is a subnode-loop. See also available()).
    if ($result && $node->issubnode()) {
1148
	my %tags = ();
1149 1150 1151 1152 1153 1154
	if (defined($tagRef)) {
	    %tags = %{ $tagRef };
	}
	$tags{$self->name()} = 1;
	my $parent = $node->phys_nodeid();
	if (! exists($tags{$parent})) {
1155
	    $result = $result && $nodeList{$parent}->calculateWillPrint(\%tags);
1156 1157 1158 1159 1160
	}
    }
    return $result;
}

1161
sub addPType($$$;$)
1162
{
1163 1164 1165
    my ($self, $newname, $newvalue, $newstatic) = @_;
    push(@{ $self->{'PTYPES'} },
	 libptop::pnode_type->Create($newname, $newvalue, $newstatic));
1166 1167
}

1168
sub addFeature($$$;$$)
1169
{
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
    my ($self, $newname, $newvalue, $newflag, $newvolatile) = @_;
    push(@{ $self->{'FEATURES'} },
	 libptop::feature->Create($newname, $newvalue,
				  $newflag, $newvolatile));
}

sub addFeatureString($$)
{
    my ($self, $newfeature) = @_;
    push(@{ $self->{'FEATURES'} },
	 libptop::feature->CreateFromString($newfeature));
}

sub addFlag($$$)
{
    my ($self, $key, $value) = @_;
1186
    $self->{'FLAGS'}->{$key} = $value;
1187 1188 1189 1190 1191 1192
}

sub addAuxtype($$$)
{
    my ($self, $key, $value) = @_;
    $self->{'AUXTYPES'}->{$key} = $value;
1193 1194
}

1195 1196 1197 1198 1199 1200
sub addConnection($$)
{
    my ($self, $name) = @_;
    $self->{'CONNECTIONS'}->{$name} = 1;
}

1201 1202 1203 1204 1205 1206
sub addInterface($$)
{
    my ($self, $name) = @_;
    push(@{ $self->{'INTERFACES'} }, $name);
}

1207 1208 1209
sub processSwitch($)
{
    my ($self) = @_;
1210
    if (! $self->isswitch()) {# || ! $self->willPrint()) {
1211 1212 1213 1214
	return;
    }

    # Add switch and lan types
1215 1216 1217
    $self->addPType("switch", 1);
    if (!(defined($MAINSITE) && $MAINSITE && $self->name() eq "procurve1")) {
	$self->addPType("lan", undef, 1);
1218 1219 1220
    }

    # Add real-switch feature
1221
    $self->addFeature('real-switch', 0);
1222 1223 1224 1225 1226
}

sub processLocal($)
{
    my ($self) = @_;
1227
    if (! $self->islocal()) {# || ! $self->willPrint()) {
1228 1229
	return;
    }
1230 1231 1232 1233 1234 1235 1236 1237 1238
    my $node = $self->node();
    my $type = $self->type();
    # XXX temporary hack until node reboot avoidance 
    # is available. Nodes running the FBSD-NSE image
    # will have a feature def-osid-fbsd-nse 0.0
    # This is used by assign to prefer these pnodes
    # first before using others.
    if($node->def_boot_osid() && 
       $node->def_boot_osid() eq  TBOSID(TB_OPSPID, "FBSD-NSE")) { 
1239
	$self->addFeature('FBSD-NSE', 0.0);
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
    }
    $self->addPType($node->type(), 1);
    # Might be equal, which assign would sum as two, not one!
    #
    # XXX: Temporary hack - don't mark switches that are testnodes
    # as having class 'switch' - assign treats those specially. We
    # use the knowledge that 'real' switches don't hit this point!
    #
    if ($node->type() ne $node->class() && $node->class() ne "switch") {
	$self->addPType($node->class(), 1);
    }

    $self->addDelayCapacity();
1253
    if ($self->is_shared()) {
1254 1255 1256
	$self->addShared();
    }
    $self->processAuxtypes();
1257
    $self->processOsFeatures();
1258 1259 1260 1261
    $self->processConnections();
    # This is for the case that we are modifying an existing experiment - tell
    # assign to prefer nodes the user has already allocated
    if (defined($exempt_eid) && $available_only
1262
	&& defined($self->eid()) && defined($self->pid())) {
1263 1264 1265
	$self->addFeature('already_reserved', 0);
    }
    $self->processSubnode();
1266 1267 1268 1269 1270 1271 1272 1273
}

sub addDelayCapacity($)
{
    my ($self) = @_;
    my $delay = $self->type()->delay_capacity();
    if (defined($delaycap_override) &&
	$delaycap_override > 0 &&
1274 1275
	(! defined($delay) ||
	 $delaycap_override < $delay)) {
1276 1277
	$delay = $delaycap_override
    }
1278 1279 1280 1281
    if (defined($delay) && $delay > 0) {
	$self->addPType("delay", $delay);
	$self->addPType("delay-".$self->node()->type(), $delay);
    }
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
}

#
# Shared mode features
#
sub addShared($)
{
    my ($self) = @_;
    my $node = $self->node();
    #
    # Add a feature that says this node should not be picked
    # unless the corresponding desire is in the vtop. This
    # allows the node to be picked, subject to other type constraints.
    #
1296
    $self->addFeature('pcshared', 1.0, undef, 1);
1297 1298 1299 1300 1301 1302 1303

    #
    # The pool daemon may override the share weight.
    #
    my $sharedweight = undef;
    $node->NodeAttribute("shared_weight", \$sharedweight);
    if (defined($sharedweight)) {
1304
	$self->addFeature('shareweight', $sharedweight);
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
    } else {
	#
	# The point of this feature is to have assign favor shared nodes
	# that already have nodes on them, so that they are well packed.
	# Shared nodes with just a few vnodes on them are avoided so that
	# they will free up eventually. 
	#
	my $maxvnodes = 10;
	my $weight    = 0.5;
	my $gcount    = 0.0;
	if (defined($self->globalcount())) {
	    $gcount = $self->globalcount();
	}

	if (exists($self->{'AUXTYPES'}->{'pcvm'})) {
	    $maxvnodes = $self->{'AUXTYPES'}->{'pcvm'};
	}

	#
	# No point in the feature if no room left. 
	#
	if ($maxvnodes > $gcount) {
	    my $factor = ($gcount / $maxvnodes);
	    if ($factor < 0.25) {
		$weight = 0.8;
	    }
	    elsif ($factor > 0.75) {
		$weight = 0.1;
	    }
	    else {
		$weight = 0.3;
	    }
1337
	    #addFeature('shareweight', $weight);
1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	}
    }
}

#
# Add any auxiliary types
#
sub processAuxtypes($)
{
    my ($self) = @_;
    my $node = $self->node();
    my $needvirtgoo = 0;
    foreach my $auxtype (keys(%{ $self->{'AUXTYPES'} })) {
	my $count = $self->{'AUXTYPES'}->{$auxtype};
	my $realtypename;

	# Map an auxtype back to its real type, unless it is a real type.
	if (defined($auxtypemap{$auxtype})) {
	    $realtypename = $auxtypemap{$auxtype};
	} else {
	    $realtypename = $auxtype;
	}
	my $realtype = NodeType->Lookup($realtypename);
1361 1362 1363
	if (! defined($realtype)) {
	    if ($debug) {
		print STDERR "NodeType->Lookup failed for $realtypename ".
1364
		    "on node " . $self->name() . "\n";
1365 1366 1367
	    }
	    next;
	}
1368 1369 1370 1371 1372 1373 1374 1375 1376
	my $is_virtual = ($realtype->isvirtnode() && $count > 0);
	if (! $is_virtual) {
	    $self->addPType($auxtype, $count);
	} elsif ($print_virtual) {
	    $needvirtgoo = 1;
	    #
	    # If the node is shared, must subtract the current global count
	    # from the max first, to see if there is any room left.
	    #
1377
	    if ($self->is_shared() && defined($self->globalcount())) {
1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
		$count -= $self->globalcount();
	    }
	    if (defined($multiplex_override)
		&& $multiplex_override <= $count) {
		$count = $multiplex_override;
	    }

	    #
	    # Add in machine specific auxtypes that use the same count.
	    #
	    $self->addPType($node->type()."-vm", $count);

	    # And a legacy type.
	    my $legacy_type = $node->type();
	    if (($legacy_type =~ s/pc/pcvm/)) {
		$self->addPType($legacy_type, $count);
	    }
	    $self->addPType($auxtype, $count);
	}
    }
    if ($needvirtgoo) {
	$self->processVirtGoo();
    }
    my $simcap = $self->type()->simnode_capacity();
    my $needsim = ($print_sim && defined($simcap) && $simcap > 0);
    if ($needsim) {
	$self->processSim($simcap);
    }
    if ($needsim || $needvirtgoo) {
	$self->addPType("lan", undef, 1);
    }
1409
    if (($needvirtgoo && ! $self->is_shared())
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422
	|| $needsim) {
	$self->processCpuRam();
    }
}

sub processVirtGoo($)
{
    my ($self) = @_;
    # Add trivial bw spec., but only if the node type has it
    my $trivspeed = $self->type()->trivlink_maxspeed();
    if ($trivspeed) {
	$self->addFlag("trivial_bw", $trivspeed);
    }
1423
    if (! $self->is_shared()) {
1424
	# This number can be use for fine-tuning packing
1425
	$self->addFeature('virtpercent', 100, libptop_new::FD_ADDITIVE());
1426 1427 1428 1429
    }
    # Put this silly feature in so that we can try to keep vnodes
    # on the same pnode they were before - but only if updating
    if (defined($exempt_eid)) {
1430
	$self->addFeature($self->name(), 0.0);
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
    }
}

sub processSim($$)
{
    my ($self, $simcap) = @_;
    #
    # Use user specified multiplex factor
    #
    my $cap = $simcap;
    if (defined($multiplex_override) && $multiplex_override <= $simcap) {
	$cap = $multiplex_override;
    }
    $self->addPType("sim", $cap);
    # Add trivial bw spec.
    $self->addFlag("trivial_bw", 100000);
}

sub processCpuRam($)
{
    my ($self) = @_;
    my $cpu_speed = $self->type()->frequency();
    my $ram = $self->type()->memory();
    # Add CPU and RAM information
1455
    $self->addFeature('cpu', $cpu_speed, libptop_new::FD_ADDITIVE())
1456
	if (defined($cpu_speed));
1457
    $self->addFeature('ram', $ram, libptop_new::FD_ADDITIVE())
1458
	if (defined($ram));
1459 1460
    $self->addFeature('cpupercent', 92, libptop_new::FD_ADDITIVE()); # XXX Hack
    $self->addFeature('rampercent', 80, libptop_new::FD_ADDITIVE()); # XXX Hack
1461 1462
}

1463 1464 1465 1466 1467 1468 1469
#
# Add in OS features.
#
sub processOsFeatures($)
{
    my ($self) = @_;
    my $node = $self->node();
1470
    if ($self->is_shared()) {
1471 1472 1473 1474 1475 1476
	#
	# A shared node is running just one OS, and we put that in
	# so that the user can specify which of the current VM types
	# is wanted.
	#
	my $osid = $node->def_boot_osid();
1477
	$self->addFeature('OS-'.$osid, 0.5);
1478 1479 1480
	# Add any subOSes the shared node osid can support
	if (defined($osid_subosid{$osid})) {
	    foreach my $subosid (@{ $osid_subosid{$osid} }) {
1481
		$self->addFeature('OS-'.$osid.'-'.$subosid, 0);
1482 1483 1484
	    }
	}
    } elsif (exists($type_osid{$node->type()})) {
1485 1486
#	my $default = $self->type()->default_osid();
#	my $default_used = 0;
1487 1488 1489 1490 1491
	#
	# Add in features for all of the OSes that this node (as
	# evidenced by its type) can support
	#
	foreach my $o1 (@{ $type_osid{$node->type()} }) {
1492
	    $self->addFeature('OS-'.$o1, 0);
1493
	    foreach my $o2 (@{ $osid_subosid{$o1} }) {
1494
		$self->addFeature('OS-'.$o1.'-'.$o2, 0);
1495
	    }
1496 1497 1498 1499
#	    if ($o1 eq $default) {
#		print STDERR "Equal: OS-".$o1."\n";
#		$default_used = 1;
#	    }
1500
	}
1501 1502 1503 1504
#	if (! $default_used) {
#	    print STDERR "OS-".$default."\n";
#	    $self->addFeature('OS-'.$default, 0);
#	}
1505 1506 1507 1508 1509 1510 1511 1512 1513
    } elsif (! $self->type()->imageable() &&
	     defined($self->type()->default_osid())) {
	#
	# If node is not imageable (and thus no entries in osidtoimageid,
	# then assume it always has its default OSID loaded and ready to
	# go, so that assign will agree to the allocation (assign_wrapper
	# adds a desire that says it has to be running the OSID the user
	# has selected, or the default OSID from the node_types table).
	#
1514 1515 1516 1517 1518 1519 1520
	$self->addFeature('OS-'.$self->type()->default_osid(), 0);
    }
}

sub processConnections($)
{
    my ($self) = @_;
1521
    if (! $self->is_shared()) {
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533
	foreach my $name (keys(%{ $self->{'CONNECTIONS'} })) {
	    $self->addFeature('connected-to-'.$name, 0.0);
	}
    }
}

sub processSubnode($)
{
    my ($self) = @_;
    my $node = $self->node();
    if ($node->issubnode()) {
	$self->addFlag('subnode_of', $node->phys_nodeid());
1534 1535 1536
    }
}

1537 1538 1539
sub processWidearea($)
{
    my ($self) = @_;