libvtop_stable.pm.in 289 KB
Newer Older
1
#!/usr/bin/perl -w
2
#
3
# Copyright (c) 2005-2013 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 libvtop_stable;
25 26 27 28

use strict;
use Exporter;
use vars qw(@ISA @EXPORT @EXPORT_OK
29
	    $VTOP_FLAGS_UPDATE $VTOP_FLAGS_VERBOSE $VTOP_FLAGS_QUIET
30
	    $VTOP_FLAGS_FIXNODES $VTOP_FLAGS_IMPOTENT $VTOP_FLAGS_ALLOCONLY
31
	    $VTOP_FLAGS_REGRESSION $VTOP_FLAGS_FIXLANNODES);
32 33 34 35 36

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

use libdb;
37
use libtblog_simple;
38 39
use libtestbed;
use Experiment;
40
use EmulabConstants;
41 42 43 44 45
use VirtExperiment;
use Node;
use NodeType;
use Lan;
use OSinfo;
46 47
use Blockstore;
use Image;
48
use Port;
49
use English;
50
use emutil qw(SpanningTree GenFakeMac);
51 52
use Data::Dumper;
use Carp;
53
use POSIX;
54
use XML::LibXML;
55
use XML::Simple;
56 57

# Configure variables
58 59
my $TB		  = "@prefix@";
my $BOSSNODE      = "@BOSSNODE@";
60
my $AVAIL         = "$TB/bin/avail";
61 62 63
my $NALLOC        = "$TB/bin/nalloc";
my $NFREE         = "$TB/bin/nfree";
my $OS_SELECT     = "$TB/bin/os_select";
64
my $DELAYCAPACITY = @DELAYCAPACITY@;	# Can be overridden by user.
65
my $DELAYTHRESH   = @DELAYTHRESH@;
66
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
67
my $PGENIRACK     = @PROTOGENI_GENIRACK@;
68
my $OURDOMAIN     = "@OURDOMAIN@";
69 70
my $MAINSITE      = @TBMAINSITE@;
my $mycmurn       = "";
71 72

if ($PGENISUPPORT) {
73 74
  require GeniHRN;
  $mycmurn = GeniHRN::Generate("@OURDOMAIN@", "authority", "cm");  
75
}
76 77 78 79

# Flags.
$VTOP_FLAGS_VERBOSE	= 0x01;
$VTOP_FLAGS_UPDATE	= 0x02;
80
$VTOP_FLAGS_FIXNODES	= 0x04;
81
$VTOP_FLAGS_IMPOTENT	= 0x08;
82
$VTOP_FLAGS_REGRESSION  = 0x10;
83
$VTOP_FLAGS_QUIET       = 0x20;
84
$VTOP_FLAGS_ALLOCONLY	= 0x40;
85
$VTOP_FLAGS_FIXLANNODES	= 0x80;
86

87
@EXPORT_OK = qw($VTOP_FLAGS_UPDATE $VTOP_FLAGS_VERBOSE $VTOP_FLAGS_FIXNODES
88
		$VTOP_FLAGS_IMPOTENT $VTOP_FLAGS_REGRESSION $VTOP_FLAGS_QUIET
89
		$VTOP_FLAGS_ALLOCONLY $VTOP_FLAGS_FIXLANNODES);
90 91 92 93

#
# Create an object representing the stuff we need to create the vtop file.
#
94
sub Create($$$$)
95
{
96
    my ($class, $experiment, $user, $flags) = @_;
97 98 99 100 101 102 103 104 105

    my $virtexperiment = VirtExperiment->Lookup($experiment);
    if (!defined($virtexperiment)) {
	tberror("Could not load virtual experiment object for $experiment\n");
	return undef;
    }

    my $self              = {};
    $self->{'EXPERIMENT'} = $experiment;
106
    $self->{'USER'}       = $user;
107 108 109
    $self->{'VIRTEXPT'}   = $virtexperiment;
    $self->{'FLAGS'}      = $flags;
    $self->{'VNODES'}     = {};
110 111
    $self->{'DELAYNODES'} = {};
    $self->{'LANNODES'}   = {};
112
    $self->{'BRIDGES'}    = {};
113 114
    $self->{'BLOCKSTORES'}= {};
    $self->{'SANHOSTS'}   = {};
115
    $self->{'VLANS'}      = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
116
    $self->{'VPATHS'}     = {};
117 118 119 120
    $self->{'MEMBEROF'}   = {};
    $self->{'COUNTERS'}   = {};
    $self->{'EXPTSTATS'}  = {};
    $self->{'DELAYLINKS'} = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
121
    $self->{'VLINKS'}     = {};
122 123
    $self->{'OPTIONS'}    = {};
    $self->{'DELAYID'}    = 0;
124
    $self->{'PHOSTID'}    = 0;
125
    $self->{'IFACEID'}    = 32768;
126
    $self->{'PORTBW'}     = {};
127 128 129 130
    $self->{'RESULTS'}    = { "nodes"  => [],
			      "links"  => [],
			      "class"  => [],
			      "fixed"  => [] };
131
    $self->{'RSPEC'}      = {};
132
    $self->{'GENIRSPEC'}  = undef;
133 134

    # Mostly for update mode.
135 136
    $self->{'FIXEDNODES'}  = {};
    $self->{'CURRENT_V2P'} = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
137
    $self->{'CURRENT_V2P'} = {};
138 139
    $self->{'CURRENT_P2V'} = {};
    $self->{'CURRENT_V2V'} = {};
140 141
    $self->{'OLDRSRVCLEAN_FLAG'}  = 0;
    $self->{'OLDRSRVCLEAN_NODES'} = {};
142

Leigh B Stoller's avatar
Leigh B Stoller committed
143 144 145
    # This is just for lannodes.
    $self->{'CURRENT_V2PMAP'} = {};

146
    # Below is for interpretation of assign results.
147
    $self->{'PNODES'}      = {};
148 149
    $self->{'SOLUTION'}    = {};
    $self->{'NEWRESERVED'} = {};	# Newly reserved nodes.
150
    $self->{'NORECOVER'}   = 0;	
151
    
152
    bless($self, $class);
153 154
    $virtexperiment->Dump()
	if (0 && $self->verbose());
155 156 157 158 159

    return $self;
}
# accessors
sub experiment($)       { return $_[0]->{'EXPERIMENT'}; }
160
sub user($)             { return $_[0]->{'USER'}; }
161 162 163
sub virtexperiment($)   { return $_[0]->{'VIRTEXPT'}; }
sub flags($)            { return $_[0]->{'FLAGS'}; }
sub vnodes($)           { return $_[0]->{'VNODES'}; }
164
sub delaynodes($)       { return $_[0]->{'DELAYNODES'}; }
165
sub bridges($)          { return $_[0]->{'BRIDGES'}; }
166 167
sub blockstores($)      { return $_[0]->{'BLOCKSTORES'}; }
sub sanhosts($)         { return $_[0]->{'SANHOSTS'}; }
168
sub lannodes($)         { return $_[0]->{'LANNODES'}; }
169
sub vlans($)            { return $_[0]->{'VLANS'}; }
170
sub memberof($)         { return $_[0]->{'MEMBEROF'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
171
sub vpaths($)           { return $_[0]->{'VPATHS'}; }
172
sub counters($)         { return $_[0]->{'COUNTERS'}; }
173
sub counter($$)         { return $_[0]->{'COUNTERS'}->{$_[1]}; }
174
sub options($)          { return $_[0]->{'OPTIONS'}; }
175 176
sub option($$)          { return (exists($_[0]->{'OPTIONS'}->{$_[1]}) ?
				  $_[0]->{'OPTIONS'}->{$_[1]} : undef); }
177
sub exptstats($)        { return $_[0]->{'EXPTSTATS'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
178
sub vlinks($)           { return $_[0]->{'VLINKS'}; }
179 180 181
sub delaylinks($)       { return $_[0]->{'DELAYLINKS'}; }
sub delaynodecount()    { return scalar(keys(%{ $_[0]->delaynodes() })); }
sub portbw($)           { return $_[0]->{'PORTBW'}; }
182
sub results($)          { return $_[0]->{'RESULTS'}; }
183 184 185
sub current_v2p($)      { return $_[0]->{'CURRENT_V2P'}; }
sub current_p2v($)      { return $_[0]->{'CURRENT_P2V'}; }
sub current_v2v($)      { return $_[0]->{'CURRENT_V2V'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
186
sub current_v2pmap($)   { return $_[0]->{'CURRENT_V2PMAP'}; }
187 188
sub pnodes($)           { return $_[0]->{'PNODES'}; }
sub fixednodes($)       { return $_[0]->{'FIXEDNODES'}; }
189
sub newreserved($)      { return $_[0]->{'NEWRESERVED'}; }
190
sub rspec($)            { return $_[0]->{'RSPEC'}; }
191
sub genirspec($)        { return $_[0]->{'GENIRSPEC'}; }
192
sub newreservednodes($) { return keys(%{ $_[0]->{'NEWRESERVED'} }); }
193
sub oldreservednodes($) { return $_[0]->{'OLDRSRVCLEAN_NODES'}; }
194
sub norecover($)        { return $_[0]->{'norecover'}; }
195 196 197 198 199 200 201 202 203
sub pid($)		{ return $_[0]->experiment()->pid(); }
sub pid_idx($)		{ return $_[0]->experiment()->pid_idx(); }
sub eid($)		{ return $_[0]->experiment()->eid(); }
sub exptidx($)		{ return $_[0]->experiment()->idx(); }

# The virtual tables from the DB.
sub virt_table($$)      { return $_[0]->virtexperiment()->Table($_[1]); }
sub virt_vtypes($)	{ return $_[0]->virt_table("virt_vtypes"); }
sub virt_nodes($)       { return $_[0]->virt_table("virt_nodes"); }
204 205
sub virt_lans($)        { return $_[0]->virt_table("virt_lans"); }
sub virt_lan_lans($)    { return $_[0]->virt_table("virt_lan_lans"); }
Leigh B Stoller's avatar
Leigh B Stoller committed
206
sub virt_paths($)       { return $_[0]->virt_table("virt_paths"); }
207
sub virt_bridges($)     { return $_[0]->virt_table("virt_bridges"); }
208
sub virt_desires($)	{ return $_[0]->virt_table("virt_node_desires"); }
Leigh B Stoller's avatar
Leigh B Stoller committed
209
sub virt_attributes($)	{ return $_[0]->virt_table("virt_node_attributes"); }
210
sub virt_startloc($)	{ return $_[0]->virt_table("virt_node_startloc"); }
211 212 213 214
sub virt_trafgens($)	{ return $_[0]->virt_table("virt_trafgens"); }
sub virt_lan_settings($){ return $_[0]->virt_table("virt_lan_settings"); }
sub virt_lan_member_settings($)	{
    return $_[0]->virt_table("virt_lan_member_settings"); }
215 216 217
sub virt_blockstores($) { return $_[0]->virt_table("virt_blockstores"); }
sub virt_blockstore_attributes($) {
    return $_[0]->virt_table("virt_blockstore_attributes"); }
218 219 220 221

# Given a vname, is it a node in the topo (or something else like a delay).
sub isatoponode($$)     { return exists($_[0]->vnodes()->{$_[1]}); }
sub isadelaynode($$)    { return exists($_[0]->delaynodes()->{$_[1]}); }
222
sub isasanhost($$)      { return exists($_[0]->sanhosts()->{$_[1]}); }
223 224 225

# Debug output.
sub verbose($)		{ return $_[0]->flags() & $VTOP_FLAGS_VERBOSE; }
226
sub quiet($)		{ return $_[0]->flags() & $VTOP_FLAGS_QUIET; }
227
sub updating($)		{ return $_[0]->flags() & $VTOP_FLAGS_UPDATE; }
228
sub fixcurrent($)	{ return $_[0]->flags() & $VTOP_FLAGS_FIXNODES; }
Leigh B Stoller's avatar
Leigh B Stoller committed
229
sub fixlanodes($)	{ return $_[0]->flags() & $VTOP_FLAGS_FIXLANNODES; }
230
sub impotent($)		{ return $_[0]->flags() & $VTOP_FLAGS_IMPOTENT; }
231
sub alloconly($)	{ return $_[0]->flags() & $VTOP_FLAGS_ALLOCONLY; }
232
sub regression($)	{ return $_[0]->flags() & $VTOP_FLAGS_REGRESSION; }
233
sub printdb($$)		{ print $_[1] if ($_[0]->verbose()); return 1; }
234

235
# We name delay nodes internally as they are needed.
236
sub nextdelayname($)    { return "tbdelay" . $_[0]->{'DELAYID'}++; }
237
# For when the user wants a specific delay os. Use a desire.
238 239 240 241
# sub delay_desire($)	{ return $_[0]->option("delay_desire_string"); }
sub delay_desire_type($)	{ return $_[0]->option("delay_desire_type"); }
sub delay_desire_name($)	{ return $_[0]->option("delay_desire_name"); }
sub delay_desire_penalty($)	{ return $_[0]->option("delay_desire_penalty"); }
242 243
# For XML
sub nextifacenumber($)  { return $_[0]->{'IFACEID'}++; }
244
sub nextphostnumber($)  { return $_[0]->{'PHOSTID'}++; }
245

246 247 248 249
# Virtual Types. 
sub virttypeisvtype($$) { return $_[0]->virt_vtypes()->Find($_[1]); }
sub VirtTypes($)        { return $_[0]->virt_vtypes()->Rows(); }

250 251 252
# Caller will want these.
sub minimum_nodes($)    { return $_[0]->counter("minimum_nodes"); }
sub maximum_nodes($)    { return $_[0]->counter("maximum_nodes"); }
253
sub nodecount($)	{ return $_[0]->counter("nodecount"); }
254
sub plabcount($)	{ return $_[0]->counter("plabcount"); }
255
sub genicount($)	{ return $_[0]->counter("genicount"); }
256 257 258
sub virtnodecount($)	{ return $_[0]->counter("virtcount"); }
sub simnodecount($)	{ return $_[0]->counter("simcount"); }
sub remotenodecount($)	{ return $_[0]->counter("remotecount"); }
259
sub sharednodecount($)	{ return $_[0]->counter("sharedcount"); }
260
sub bstorecount($)	{ return $_[0]->counter("bstorecount"); }
261

262
sub createLink($$$$$$$$$)
263
{
264 265 266
    # $others here will be a hashtable for the link flags.  The table
    # could also contain non-default values for latency and
    # packet_loss
267
    my ($self, $name, $plink, $cm, $src, $dst, $bw, $type, $others) = @_;
Tarun Prabhu's avatar
Tarun Prabhu committed
268
    my $ref = {
269 270 271
	'virtual_id'     => $name,
	'plink'		 => $plink,
	'manager_urn'    => $cm,
272
	'link_type'      => $type,
Tarun Prabhu's avatar
Tarun Prabhu committed
273 274 275 276 277 278 279 280 281
	'interface_ref'  => [$src, $dst],
	'capacity'       => $bw,
	'packet_loss'    => "0",
	'latency'        => "0"
    };
    
    if ($others) {
	while ( my ($key, $value) = each %{$others} ) {
	    $ref->{$key} = $value;
282
	}
Tarun Prabhu's avatar
Tarun Prabhu committed
283 284 285 286 287
    }
    if (!exists($self->rspec()->{'link'})) {
	$self->rspec()->{'link'} = [];
    }
    push(@{ $self->rspec()->{'link'} }, $ref);
288 289
}

290
sub createNode ($$$$$$$;$)
291
{
292 293 294
    # $others here will be a hashtable for the desires
    # The key will be the desire name and the value will be a pair
    # of the desire type and the penalty
295
    my ($self, $name, $cm, $type, $typecount, $desires, $others, $ifaces) = @_;
Tarun Prabhu's avatar
Tarun Prabhu committed
296
    my $ref = {
297 298
	'virtual_id'    => $name,
	'manager_urn'   => $cm,
Tarun Prabhu's avatar
Tarun Prabhu committed
299 300 301 302 303 304 305 306 307 308 309 310
    };
    
    my $typename = $type;
    my $typeslots = $typecount;
    my $isstatic = 0;
    if ($typename =~ /^\*([-\w]*)$/) {
	$typename = $1;
	$isstatic = 1;
    }
    if ($typecount eq '*') {
	$typeslots = 'unlimited';
    }
311

Tarun Prabhu's avatar
Tarun Prabhu committed
312 313
    # We will need this to generate interface names later
    $ref->{'interface_count'} = 0;
314
    $ref->{'interfaces'} = $ifaces;
Tarun Prabhu's avatar
Tarun Prabhu committed
315 316 317 318 319 320 321 322 323

    $ref->{'node_type'} = $typename;
    $ref->{'type_slots'} = $typecount;
    $ref->{'node_static'} = $isstatic;

    my $desiretable = {};
    if ($desires) {
	while ( my ($key, $value) = each %{$desires} ) {
	    $desiretable->{$key} = $value;
324
	}
Tarun Prabhu's avatar
Tarun Prabhu committed
325 326 327 328 329 330
    }
    $ref->{'desires'} = $desiretable;
    
    if ($others) {
	while ( my ($key, $value) = each %{$others} ) {
	    $ref->{$key} = $value;
331
	}
Tarun Prabhu's avatar
Tarun Prabhu committed
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
    }
    
    if (!exists($self->rspec()->{'node'})) {
	$self->rspec()->{'node'} = [];
    }
    push(@{ $self->rspec()->{'node'} }, $ref);
}

sub createVClass ($$$$)
{
    my ($self, $name, $weight, $members) = @_;
    my $ref = {
	'name'    => $name,
	'weight'  => $weight,
	'members' => $members
    };
    if (!exists($self->rspec()->{'vclass'})) {
	$self->rspec()->{'vclass'} = [];
    }
    push(@{ $self->rspec()->{'vclass'} }, $ref);
}

sub createFixedNode ($$$)
{
    my ($self, $vnode, $pnode) = @_;
    my $ref = { 'vnode' => $vnode,
358 359
		# XXX Going to need a URN in rspec output ...
		'pnode' => $pnode,
Tarun Prabhu's avatar
Tarun Prabhu committed
360 361 362 363 364 365 366
    };
    if (!exists($self->rspec()->{'fixed'})) {
	$self->rspec()->{'fixed'} = [];
    }
    push(@{ $self->rspec()->{'fixed'} }, $ref);
}

367 368 369 370 371 372 373
###############################################################################
# Virtual Nodes. A separate package so we can create objects for each one
# and then add local stuff to them.
#
package libvtop::virt_node;
use Carp;
use vars qw($AUTOLOAD);
374
use overload ('""' => 'Stringify');
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

# To avoid wrtting out all the methods.
sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self) or croak "$self is not an object";

    my $name = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    if (@_) {
	return $self->{'HASH'}->{$name} = shift;
    }
    elsif (exists($self->{'HASH'}->{$name})) {
	return $self->{'HASH'}->{$name};
    }
    else {
	return $self->virt_node()->$name();
    }
}

#
# Wrap up a virt node.
#
sub Create($$$)
{
    my ($class, $vtop, $virt_node) = @_;

    my $self = {};
    bless($self, $class);

405 406 407 408 409
    $self->{'VIRTNODE'}   = $virt_node;
    $self->{'VTOP'}       = $vtop;
    $self->{'HASH'}       = {};
    # The virtlans this virtnode is a member of. 
    $self->{'MEMBERSHIP'} = {};
410
    
411 412 413 414 415 416
    return $self;
}
# accessors
sub virt_node($)	{ return $_[0]->{'VIRTNODE'}; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
417 418
sub membership($)       { return $_[0]->{'MEMBERSHIP'}; }
sub memberlist($)       { return values(%{ $_[0]->{'MEMBERSHIP'} }); }
419 420 421 422 423 424 425 426

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{'VIRTNODE'}   = undef;
    $self->{'VTOP'}       = undef;
    $self->{'HASH'}       = undef;
427
    $self->{'MEMBERSHIP'} = undef;
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
sub Stringify($)
{
    my ($self) = @_;
    my $vname  = $self->vname();

    return "[vnode:$vname]";
}

###############################################################################
# Virtual Lans. This wraps up the virt_lan_lan table, and allows storing
# the members (virt_lans table entries).
#
package libvtop::virt_lan;
use Carp;
use vars qw($AUTOLOAD);
use overload ('""' => 'Stringify');

# To avoid wrtting out all the methods.
sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self) or croak "$self is not an object";

    my $name = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    if (@_) {
	return $self->{'HASH'}->{$name} = shift;
    }
    elsif (exists($self->{'HASH'}->{$name})) {
	return $self->{'HASH'}->{$name};
    }
    else {
	return $self->virt_lanlan()->$name();
    }
}

#
# Wrap up a virt lan.
#
sub Create($$$$)
{
    my ($class, $vtop, $virt_lanlan) = @_;

    my $self = {};
    bless($self, $class);

    $self->{'VIRTLANLAN'}    = $virt_lanlan;
    $self->{'VTOP'}          = $vtop;
478 479
    $self->{'MEMBERHASH'}    = {};
    $self->{'MEMBERLIST'}    = [];
480 481 482 483 484 485 486
    $self->{'SHAPEDMEMBERS'} = {};
    $self->{'HASH'}          = {};

    return $self;
}
# accessors
sub virt_lanlan($)	{ return $_[0]->{'VIRTLANLAN'}; }
487 488
sub members($)		{ return $_[0]->{'MEMBERHASH'}; }
sub memberlist($)       { return @{ $_[0]->{'MEMBERLIST'} }; }
489
sub member($$)          { return @{ $_[0]->{'MEMBERLIST'} }[$_[1]]; }
490 491 492
sub shapedmembers($)	{ return $_[0]->{'SHAPEDMEMBERS'}; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
493
sub islink($)           { return (@{ $_[0]->{'MEMBERLIST'} } == 2 ? 1 : 0); }
494 495 496 497 498 499

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{'VIRTLANLAN'} = undef;
500 501
    $self->{'MEMBERHASH'} = undef;
    $self->{'MEMBERLIST'} = undef;
502 503 504 505 506 507 508 509 510 511 512 513
    $self->{'VTOP'}       = undef;
    $self->{'HASH'}       = undef;
}

sub Stringify($)
{
    my ($self) = @_;
    my $vname  = $self->vname();

    return "[vlan:$vname]";
}

514 515 516 517 518
sub addmember($$)
{
    my ($self, $vlanmember) = @_;
    
    $self->members()->{$vlanmember->member()} = $vlanmember;
519 520

    @{ $self->{'MEMBERLIST'} }[$vlanmember->vindex()] = $vlanmember;
521 522 523 524

    return 0;
}

525 526 527 528 529 530 531 532
#
# Other support functions.
#
sub usevirtiface($)
{
    my ($self) = @_;
    my $encap  = $self->_encapstyle();

533
    return ($encap eq "veth-ne" || $encap eq "vlan");
534 535 536 537 538 539 540 541
}
sub membershaped($$) {
    my ($self, $member) = @_;
    return $self->shapedmembers()->{"$member"};
}
sub setmembershaped($$) {
    my ($self, $member) = @_;
    $self->shapedmembers()->{"$member"} = 1;
542
    $member->_shaped(1);
543 544 545 546 547 548 549 550 551
}

###############################################################################
# Virtual Lans Member. A separate package so we can create objects for
# each one and then add local stuff to them.
#
package libvtop::virt_lan_member;
use Carp;
use vars qw($AUTOLOAD);
552
use overload ('""' => 'Stringify', 'eq' => 'StringEQ');
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 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617

# To avoid wrtting out all the methods.
sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self) or croak "$self is not an object";

    my $name = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    if (@_) {
	return $self->{'HASH'}->{$name} = shift;
    }
    elsif (exists($self->{'HASH'}->{$name})) {
	return $self->{'HASH'}->{$name};
    }
    else {
	return $self->virt_member()->$name();
    }
}

#
# Wrap up a virt lan member.
#
sub Create($$$$)
{
    my ($class, $vtop, $virt_member, $virt_lan) = @_;

    my $self = {};
    bless($self, $class);

    $self->{'VIRTMEMBER'} = $virt_member;
    $self->{'VIRTLAN'}    = $virt_lan;
    $self->{'VIRTNODE'}   = $vtop->vnodes()->{$virt_member->vnode()};
    $self->{'VTOP'}       = $vtop;
    $self->{'HASH'}       = {};

    return $self;
}
# accessors
sub virt_member($)	{ return $_[0]->{'VIRTMEMBER'}; }
sub virt_lan($)		{ return $_[0]->{'VIRTLAN'}; }
sub virt_node($)	{ return $_[0]->{'VIRTNODE'}; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{'VIRTLAN'}    = undef;
    $self->{'VIRTNODE'}   = undef;
    $self->{'VIRTMEMBER'} = undef;
    $self->{'VTOP'}       = undef;
    $self->{'HASH'}       = undef;
}

sub Stringify($)
{
    my ($self) = @_;
    my $vnode  = $self->vnode();
    my $vport  = $self->vport();

    return "$vnode:$vport";
}

618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
sub StringEQ($$)
{
    my ($self, $other) = @_;

    return ($self->Stringify() eq "$other" ? 1 : 0);
}

sub usevirtiface($)
{
    my ($self) = @_;
    my $encap  = $self->_encapstyle();

    return ($encap eq "veth-ne" || $encap eq "vlan");
}

Leigh B Stoller's avatar
Leigh B Stoller committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
###############################################################################
# Virtual Paths This wraps up the virt_paths table
#
package libvtop::virt_path;
use Carp;
use vars qw($AUTOLOAD);
use overload ('""' => 'Stringify');

# To avoid wrtting out all the methods.
sub AUTOLOAD {
    my $self = shift;
    my $type = ref($self) or croak "$self is not an object";

    my $name = $AUTOLOAD;
    $name =~ s/.*://;   # strip fully-qualified portion

    if (@_) {
	return $self->{'HASH'}->{$name} = shift;
    }
    elsif (exists($self->{'HASH'}->{$name})) {
	return $self->{'HASH'}->{$name};
    }
    else {
	return $self->virt_path()->$name();
    }
}

#
# Wrap up a virt path
#
sub Create($$$$)
{
    my ($class, $vtop, $pathname, $layer) = @_;

    my $self = {};
    bless($self, $class);

    $self->{'PATHNAME'}      = $pathname;
    $self->{'LAYER'}         = $layer;
    $self->{'VTOP'}          = $vtop;
    $self->{'MEMBERHASH'}    = {};
    $self->{'MEMBERLIST'}    = [];
    $self->{'VIRTLANHASH'}   = {};
    $self->{'VIRTLANLIST'}   = [];
677
    $self->{'IMPLEMENTS'}    = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
678 679 680 681 682 683 684 685 686
    $self->{'HASH'}          = {};

    return $self;
}
# accessors
sub pathname($)		{ return $_[0]->{'PATHNAME'}; }
sub layer($)		{ return $_[0]->{'LAYER'}; }
sub members($)		{ return $_[0]->{'MEMBERHASH'}; }
sub memberlist($)       { return @{ $_[0]->{'MEMBERLIST'} }; }
687
sub virtlanlist($)      { return @{ $_[0]->{'VIRTLANLIST'} }; }
Leigh B Stoller's avatar
Leigh B Stoller committed
688 689 690 691
sub member($$)		{ return $_[0]->{'MEMBERLIST'}->[$_[1]]; }
sub lanlink($$)		{ return $_[0]->{'VIRTLANLIST'}->[$_[1]]; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
692
sub implements($)	{ return $_[0]->{'IMPLEMENTS'}; }
Leigh B Stoller's avatar
Leigh B Stoller committed
693 694 695 696 697 698 699 700 701 702 703 704

# Break circular reference someplace to avoid exit errors.
sub DESTROY {
    my $self = shift;

    $self->{'LAYER'}      = undef;
    $self->{'PATHNAME'}   = undef;
    $self->{'MEMBERHASH'} = undef;
    $self->{'MEMBERLIST'} = undef;
    $self->{'VIRTLANHASH'} = undef;
    $self->{'VIRTLANLIST'} = undef;
    $self->{'VTOP'}       = undef;
705
    $self->{'IMPLEMENTS'} = undef;
Leigh B Stoller's avatar
Leigh B Stoller committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    $self->{'HASH'}       = undef;
}

sub Stringify($)
{
    my ($self) = @_;
    my $vname  = $self->pathname();
    my $layer  = $self->layer();

    return "[vpath:$vname:$layer]";
}

sub addmember($$$)
{
    my ($self, $vpath, $virtlan) = @_;
    
    $self->members()->{$vpath->segmentname()} = $vpath;
    $self->{'VIRTLANHASH'}->{$vpath->segmentname()} = $virtlan;
    # We care about this ordering.
    $self->{'MEMBERLIST'}->[$vpath->segmentindex()]  = $vpath;
    $self->{'VIRTLANLIST'}->[$vpath->segmentindex()] = $virtlan;
    return 0;
}

sub firstmember($)
{
    my ($self) = @_;

    # The lanlink for the first segment.
    my $virtlan = $self->lanlink(0);

    # Both members of a link.
    my ($member0,$member1) = $virtlan->memberlist();

    # The first member.
    return $member0;
}

sub lastmember($)
{
    my ($self) = @_;

    # The lanlink for the last segment.
    my $virtlan = $self->lanlink(scalar($self->memberlist()) - 1);

    # Both members of a link.
    my ($member0,$member1) = $virtlan->memberlist();

    # The last member.
    return $member1;
}
757

758 759 760 761 762 763 764 765 766 767 768 769 770
sub addimplements($$)
{
    my ($self, $virtlan) = @_;

    $self->implements()->{$virtlan->vname()} = $virtlan;
}
sub doesimplement($$)
{
    my ($self, $virtlan) = @_;

    return exists($self->implements()->{$virtlan->vname()});
}

771 772 773
#############################################################################
# Back to the main package.
#
774
package libvtop_stable;
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 806 807 808 809 810 811
#
# Load some physical info (for types, interfaces, speeds).
#
sub LoadPhysInfo($)
{
    my ($self) = @_;

    $self->printdb("Loading physical info\n");

    #
    # Interface capabilities, for getting speeds.
    #
    my %interface_capabilities = ();

    my $query_result =
	DBQueryWarn("select * from interface_capabilities");
    return -1
	if (!$query_result);

    while (my ($type, $capkey, $capval) = $query_result->fetchrow()) {
	$interface_capabilities{$type} = {}
  	    if (!defined($interface_capabilities{$type}));
	$interface_capabilities{$type}->{$capkey} = $capval;
    }

    #
    # Now get interface speeds for each type/class. We use this for
    # determining if a delay node is required. Very hacky, quite
    # wrong.
    #
    my %node_type_linkbw = ();
    
    $query_result =
	DBQueryWarn("select distinct i.interface_type,n.type ".
		    "  from interfaces as i ".
		    "left join nodes as n on n.node_id=i.node_id ".
812 813 814 815
		    "where n.type is not null and ".
		    "      (i.role='" . TBDB_IFACEROLE_EXPERIMENT . "' or ".
		    "       i.role='" . TBDB_IFACEROLE_CONTROL . "')");

816 817 818 819 820 821 822 823 824
    return -1
	if (!$query_result);

    # XXX Special hack for sim nodes.
    $node_type_linkbw{"sim"} = {};

    while (my ($iface_type, $node_type) = $query_result->fetchrow()) {
	my $typeinfo = NodeType->Lookup($node_type);
	if (!defined($typeinfo)) {
825
	    warn("No type info for node type $node_type\n");
826 827 828 829 830 831 832 833 834 835
	    return -1;
	}
	my $node_class = $typeinfo->class();

	$node_type_linkbw{$node_type} = {}
	    if (!defined($node_type_linkbw{$node_type}));
	$node_type_linkbw{$node_class} = {}
	    if (!defined($node_type_linkbw{$node_class}));

	if (!defined($interface_capabilities{$iface_type}->{"protocols"})) {
836
	    warn("No protocols listed in capabilities for $iface_type!\n");
837 838 839 840 841 842 843 844 845 846
	    return -1;
	}
	my @protolist =
	    split(",", $interface_capabilities{$iface_type}->{"protocols"});

	foreach my $proto (@protolist) {
	    my $def_speed =
		$interface_capabilities{$iface_type}->{"${proto}_defspeed"};

	    if (!defined($def_speed)) {
847
		warn("No default speed in capabilites for $iface_type!\n");
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
		return -1;
	    }

	    my $auxspeeds =
		$interface_capabilities{$iface_type}->{"${proto}_auxspeeds"};
	    my @auxspeedlist = ();
	    if ($auxspeeds) {
		@auxspeedlist = split(",", $auxspeeds);
	    }

	    foreach my $speed ($def_speed, @auxspeedlist) {
		$node_type_linkbw{$node_type}{$proto}->{$speed} = 1;
		$node_type_linkbw{$node_class}{$proto}->{$speed} = 1;

		#
		# If the type/class has a non-zero simnode capacity, then add
		# entries for the interface speed so that requires_delay can
		# figure out interface speeds the underlying node type
		# supports.
		#
		if ($typeinfo->simnode_capacity()) {
		    $node_type_linkbw{"sim"}{$proto}->{$speed} = 1;
		}
	    }
	}
    }
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

    #
    # Ug, it just gets worse and worse. We also need to map between the
    # auxtypes that a node (its physical type) can take on. For example,
    # a link between two pcvm nodes is really a link between a pc600 and
    # pc850.
    #
    $query_result =
	DBQueryFatal("select distinct n.type,at.type from node_auxtypes as at ".
		     "left join nodes as n on n.node_id=at.node_id");

    while (my ($phystype, $auxtype) = $query_result->fetchrow()) {
	next
	    if (!exists($node_type_linkbw{$phystype}));
	
	$node_type_linkbw{$auxtype} = $node_type_linkbw{$phystype};
    }

    #
    # Here it goes getting even worse - we have to do a similar thing for
    # vtypes.
    #
896 897 898
    foreach my $ref ($self->VirtTypes()) {
	my $vtype   = $ref->name();
	my @members = split(" ", $ref->members());
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926

	foreach my $phystype (@members) {
	    next
		if (!exists($node_type_linkbw{$phystype}));

	    $node_type_linkbw{$vtype} = {}
	        if (!defined($node_type_linkbw{$vtype}));

	    foreach my $protocol (keys(%{ $node_type_linkbw{$phystype} })) {
		my @list = keys(%{ $node_type_linkbw{$phystype}{$protocol} });
		foreach my $speed (@list) {
		    $node_type_linkbw{$vtype}{$protocol}->{$speed} = 1;
		}
	    }
	}
    }

    if ($self->verbose()) {
	$self->printdb("Interface Speeds:\n");
	foreach my $type (keys(%node_type_linkbw)) {
	    foreach my $protocol (keys(%{ $node_type_linkbw{$type} })) {
		my @list = keys(%{ $node_type_linkbw{$type}{$protocol} });

		$self->printdb(" $type:$protocol - @list\n");
	    }
	}
    }
    
927 928 929 930 931
    $self->{'IFACECAPS'}  = \%interface_capabilities;
    $self->{'TYPELINKBW'} = \%node_type_linkbw;
    
    return 0;
}
932 933 934 935 936 937
sub interfacespeedmbps($$$)
{
    my ($self, $type, $which) = @_;
        
    return $self->{'IFACECAPS'}->{$type}->{"${which}_defspeed"}/1000.0;
}
938

939 940 941 942
#
# When updating with fixednodes turned on, we need the current set
# of nodes that need to be fixed.
#
943
sub LoadCurrentResources($)
944 945
{
    my ($self) = @_;
Leigh B Stoller's avatar
Leigh B Stoller committed
946
    my $exptidx = $self->exptidx();
947 948 949 950 951

    $self->counters()->{'reserved_simcount'}  = 0;
    $self->counters()->{'reserved_virtcount'} = 0;
    $self->counters()->{'reserved_physcount'} = 0;

952 953
    $self->printdb("Loading current resources" .
		   ($self->regression() ? " in regression mode" : "") . "\n");
954

955
    my @nodelist = $self->experiment()->NodeList(0, 1);
956 957 958
    return 0
	if (!@nodelist);

959 960 961 962 963 964 965 966 967
    if ($self->regression()) {
	#
	# In regression mode, we just store the p2v mapping for fixnode.
	#
	foreach my $pnode (@nodelist) {
	    my $vname   = $pnode->vname();
	    my $node_id = $pnode->node_id();

	    if ($pnode->isvirtnode()) {
968
		$node_id = $pnode->phys_nodeid();
969
	    }
970 971
	    $self->fixednodes()->{$vname} = $node_id;
	    $self->printdb("  fixing $vname -> $node_id\n");
972 973 974 975
	}
	return 0;
    }

976
    foreach my $pnode (@nodelist) {
977 978 979 980 981 982 983 984 985
	my $vname   = $pnode->vname();
	my $node_id = $pnode->node_id();
	my $rsrv    = $pnode->ReservedTableEntry();

	# A list of vnodes on this pnode.
	$self->current_p2v()->{$pnode->phys_nodeid()} = []
	    if (! exists($self->current_p2v()->{$pnode->phys_nodeid()}));
	$self->pnodes()->{$node_id} = $pnode;

986 987 988 989
	#
	# WIDEAREA nodes are going to break.
	#
	if ($pnode->isremotenode() &&
990 991
	    !($pnode->isplabdslice() || $pnode->isfednode() ||
	      $pnode->isdedicatedremote())) {
992 993 994 995 996
	    tberror("Cannot update widearea nodes yet!\n");
	    return -1;
	}
	if ($pnode->isvirtnode()) {
	    $self->counters()->{'reserved_virtcount'}++;
997 998 999 1000 1001 1002 1003

	    # Get the underlying physical node.
	    my $ppnode = Node->Lookup($pnode->phys_nodeid());
	    if (!defined($ppnode)) {
		tberror("Cannot map $pnode to its real physnode");
		return -1;
	    }
1004
	    my $ppnode_id = $ppnode->node_id();
1005

1006 1007 1008
	    $self->fixednodes()->{$vname} = $ppnode_id
		if ($self->fixcurrent());
	    
1009 1010 1011
	    #
	    # Record the mappings. 
	    #
1012 1013
	    $self->current_v2v()->{$vname} = $pnode->node_id();
	    $self->current_v2p()->{$vname} = $ppnode->node_id();
1014 1015 1016 1017 1018
	    push(@{ $self->current_p2v()->{$ppnode->node_id()} }, $vname);
	    
	    # Mark the node as unused until later.
	    $pnode->_reuse("unused");
	    $ppnode->_reuse("unused");
1019 1020 1021 1022 1023 1024 1025 1026 1027

	    #
	    # Add the pnode node to the oldreserved list for nfree.
	    # See the comment below. We cannot use p2v because we
	    # might not own all those nodes, if on a shared node.
	    # We do not add the ppnode. It will get added in the
	    # next clause if we actually own it.
	    #
	    $self->oldreservednodes()->{$pnode->node_id()} = $pnode;
1028 1029
	    
	    $self->printdb("current v2p: $node_id ($ppnode_id) -> $vname\n");
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
	}
	else {
	    #
	    # All the sim stuff is bit rotting cause no one understands it.
	    #
	    if ($rsrv->{'erole'} eq TBDB_RSRVROLE_SIMHOST) {
		tberror("Cannot update sim nodes yet!\n");
		return -1;
	    }
	    else {
1040 1041 1042
		$self->fixednodes()->{$vname} = $pnode->node_id()
		    if ($self->fixcurrent());
		
1043
		$self->counters()->{'reserved_physcount'}++;
1044 1045 1046 1047

		#
		# Record the mapping. 
		#
1048
		$self->current_v2p()->{$vname} = $pnode->node_id();
1049 1050 1051 1052
		push(@{ $self->current_p2v()->{$node_id} }, $vname);
		# Mark the node as unused until later.
		$pnode->_reuse("unused");

1053 1054 1055 1056 1057 1058 1059
		#
		# Add the pnode node to the oldreserved list for nfree.
		# See the comment below. We cannot use p2v because we
		# might not own all those nodes, if on a shared node.
		#
		$self->oldreservednodes()->{$pnode->node_id()} = $pnode;

1060
		$self->printdb("current v2p: $node_id -> $vname\n");
1061 1062 1063
	    }
	}
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
    #
    # Grab the v2pmap table so we can find out where lan nodes were
    # assigned last time.
    #
    my $query_result =
	DBQueryWarn("select vname,node_id from v2pmap where exptidx='$exptidx'");
    return -1
	if (!$query_result);

    while (my ($vname,$nodeid) = $query_result->fetchrow_array()) {
	$self->printdb("current v2p: $nodeid -> $vname\n");
	
	$self->current_v2pmap()->{$vname} = $nodeid;
    }
1078 1079 1080
    return 0;
}

1081 1082 1083
sub LoadVirtNodes($)
{
    my ($self) = @_;
1084
    my $gotgeninodes = 0;
1085 1086 1087

    $self->printdb("Loading virtual nodes\n");

1088 1089 1090 1091
    my $pid    = $self->pid();
    my $eid    = $self->eid();
    my $table  = $self->virt_nodes();

1092
    foreach my $virt_node ($table->Rows()) {
1093 1094 1095
	my $vnode    = libvtop::virt_node->Create($self, $virt_node);
	my $vname    = $vnode->vname();
	my $desires  = {};
Leigh B Stoller's avatar
Leigh B Stoller committed
1096
	my $attrs    = {};
1097
	my $startloc = undef;
1098 1099

	# Other fields we need.
1100
	my $ips     = $vnode->ips() || "";
1101 1102 1103
	my $type    = $vnode->type();
	my $fixed   = $vnode->fixed();
	my $osname  = $vnode->osname();
1104
	my $parent_osname  = $vnode->parent_osname();
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117

	# XXX
	# If its a vtype, there will not be any node_type data. This
	# can break things, if one were to mix a virt/remote type with
	# a nonvirt/local type! Need to actually verify the vtypes
	# for consistency. 
	#
	my $isremote= 0;
	my $isvirt  = 0;
	my $issub   = 0;
	my $isplab  = 0;
	my $issim   = 0;
	my $isdyn   = 0;	# Only virtnodes are dynamic.
1118
	my $isvtyped= 0;
1119
	my $isded   = 0;
1120
	my $isgeni  = 0;
1121 1122 1123 1124 1125 1126

	# If we have a real type or auxtype ...
	my $nodetype = NodeType->LookupAny($type);
	if (!defined($nodetype)) {
	    my $vtype = $self->virttypeisvtype($type);
	    if (!defined($vtype)) {
1127
		warn("Improper type $type for node $vnode!\n");
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137
		return -1;
	    }
	    #
	    # For now, just pick the first member type.
	    #
	    my @memberlist = split(" ", $vtype->members());
	    my $vtypename  = $memberlist[0];

	    $nodetype = NodeType->LookupAny($vtypename);
	    if (!defined($nodetype)) {
1138
		warn("Improper type $vtypename in vtypes for node $vnode!\n");
1139 1140
		return -1;
	    }
1141
	    $isvtyped = 1;
1142
	}
1143 1144
	$vnode->_typeinfo($nodetype);
	
1145 1146 1147 1148
	$isremote = $nodetype->isremotenode();
	$isvirt   = $nodetype->isvirtnode();
	$issub    = $nodetype->issubnode();
	$isplab   = $nodetype->isplabdslice();
1149
	$isgeni   = $nodetype->isfednode();
1150 1151
	$issim    = $nodetype->issimnode();
	$isdyn    = $nodetype->isdynamic();
1152
	$isded    = $nodetype->isdedicatedremote();
1153

1154 1155 1156
	# Mark this as being a virtual typed node.
	$vnode->_isvtyped($isvtyped);

1157 1158 1159
	# dynamic inclusion below.
	$gotgeninodes++ if ($isgeni);

1160 1161 1162 1163 1164 1165
	# All this info is stashed in our local object.
	$vnode->_nodeweight(undef);
	$vnode->_isremotenode($isremote);
	$vnode->_isvirtnode($isvirt);
	$vnode->_issubnode($issub);
	$vnode->_isplabnode($isplab);
1166
	$vnode->_isgeninode($isgeni);
1167 1168
	$vnode->_issimnode($issim);
	$vnode->_isdynamic($isdyn);
1169
	$vnode->_isdedremote($isded);
1170 1171
	# Set below from a desire.
	$vnode->_sharedokay(0);
1172
	$vnode->_fixedvm(undef);
1173
	$vnode->_isbridge(($vnode->role() eq "bridge" ? 1 : 0));
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	$vnode->_blockstore(undef);
	$vnode->_blockstore_attributes(undef);
	$vnode->_sanhostname(undef);

	#
	# Explicit type request desire to match feature.
	#
	my $explict_type_request;
	$nodetype->GetAttribute("explicit_request", \$explict_type_request);
	$vnode->_explicit_type_request($explict_type_request);
1184

1185 1186 1187
	# For a list of interfaces on this node, as for rspec generation
	$vnode->_virtifaces([]);

1188 1189 1190
	# dynamic inclusion below.
	$gotgeninodes++ if ($isgeni);

1191 1192
	# The mapped osname to actual osinfo structure.
	$vnode->_osinfo(undef);
1193 1194
	# If the virtnode tries to specify its parent os in addition
	# to its own, store that osinfo here.
1195
	$vnode->_parent_osinfo(undef);
1196 1197 1198 1199 1200 1201 1202
	# Eventual physical mapping. 
	$vnode->_physnode(undef);
	# Handy to combine these.
	$vnode->_settings([ $vnode->cmd_line(),
			    $vnode->rpms(),
			    $vnode->startupcmd(),
			    $vnode->tarfiles(),
1203
			    $vnode->loadlist(),
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
			    $vnode->failureaction(),
			    $vnode->routertype() ]);

	#
	# If a subnode, kill the fixed mapping. That was just to
	# tell us the connection. We do not want to overload "fixed"
	# within assign wrapper since its already overloaded.
	#
	if ($issub) {
	    # Must be a parent. Set in the parser, either explicitly, or else
	    # one is created if the user leaves it out.
	    if (!defined($fixed) || $fixed eq "") {
1216
		warn("Subnode $vname must be fixed to its parent!\n");
1217 1218 1219
		return -1;
	    }
	    $vnode->_parent($fixed);
1220
	    $vnode->fixed(undef);
1221 1222 1223 1224 1225
	    undef($fixed);
	}

	# Can fixed really get set to ""?
	if (defined($fixed) && $fixed ne "") {
1226
	    # Store the name since we use FIXED_NODES for delay nodes too.
1227
	    $self->fixednodes()->{$vname} = $fixed;
1228 1229 1230 1231 1232

	    # We do not know until all the nodes are loaded, if fixing
	    # to a physical node or another node in the topology. See
	    # the post pass loop below.
	    $vnode->_fixedvm($fixed)
1233
		if ($isvirt && !$isgeni);
1234
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
1235 1236
	else {
	    $vnode->fixed(undef);
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
	    undef($fixed);
	}

	# For the moment, geni nodes must be bound to a CM.
	if ($isgeni) {
	    if (!defined($fixed) || $fixed eq "") {
		tberror("Geninode $vnode must be fixed to a CM.");
		return -1;
	    }
	    my ($authority,$gtype,undef) = GeniHRN::Parse($fixed);
	    #
	    # We allow an authority instead of a node urn. This is just a
	    # kludge so I can test against the AM interface. 
	    #
	    if ($gtype eq "node") {
		$vnode->_cmurn(GeniHRN::Generate($authority,
						 "authority", "cm"));
	    }
	    elsif ($gtype eq "authority") {
		$vnode->_cmurn($fixed);
		$fixed = GeniHRN::Generate($authority, "node", "*");
		$vnode->fixed($fixed);
	    }
	}
	else {
	    $vnode->_cmurn($mycmurn);
Leigh B Stoller's avatar
Leigh B Stoller committed
1263
	}
1264 1265

	$self->printdb("  $vname type:$type ips:$ips\n");
1266
	$self->printdb("         isrem:$isremote isvirt:$isvirt");
1267 1268
	$self->printdb(" fixed:$fixed")
	    if (defined($fixed) && $fixed ne "");
1269
	$self->printdb("\n");
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	
	# We need to check the names to make sure they do not clash with
	# our internal delay node names.
	if (($vname =~ /^tbdelay\d+/) ||
	    ($vname =~ /^tbsdelay\d+/)) {
	    print "Warning: $vname is a reserved name. Working around it.\n";
	    my ($num) = ($vname =~ /(\d+)/);
	    $self->delayid($num + 1);
	}

	# Take apart the IP list.
1281
	my @iplist  = split(" ", $ips);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294
	foreach my $ipinfo (@iplist) {
	    my ($port,$ip) = split(":",$ipinfo);
	    
	    $self->{'IPS'}->{"$vname:$port"} = $ip;
	}

	#
	# Map the osname to an OSID now so that we can check max_concurrent.
	# This also avoids the work and *check* later after we have done 90%
	# of assign_wrapper. If no osname was specified, we have to wait and
	# use the default for the type of phys node that assign picks.
	#
	if (defined($osname) && $osname ne "") {
1295
	    my $osinfo;
1296
	    
1297 1298 1299 1300 1301 1302
	    if ($osname =~ /^(ftp|http|https):/) {
		my $image = Image->LookupByURL($osname);
		if (!defined($image)) {
		    tberror("Invalid os url $osname!\n");
		}
		$osinfo = OSinfo->Lookup($image->imageid());
1303
	    }
1304 1305 1306 1307 1308
	    elsif ($osname =~ /^(.*)\/(.*)$/) {
		$osinfo = OSinfo->Lookup($osname);
	    }
	    else {
		$osinfo = OSinfo->Lookup($pid, $osname);
1309
		if (!defined($osinfo)) {
1310
		    $osinfo = OSinfo->LookupByName($osname);
1311 1312
		}
	    }
1313 1314 1315 1316 1317 1318 1319
	    if (!defined($osinfo)) {
		tberror({cause => 'user', type => 'primary',
			 severity => SEV_ERROR,
			 error => ['invalid_os', undef, $osname, $pid]},
			"Invalid OS $osname!");
		return -1;
	    }
1320 1321
	    $vnode->_osinfo($osinfo);</