libvtop_stable.pm.in 263 KB
Newer Older
1
#!/usr/bin/perl -w
2 3
#
# EMULAB-COPYRIGHT
4
# Copyright (c) 2005-2011 University of Utah and the Flux Group.
5 6
# All rights reserved.
#
7
package libvtop_stable;
8 9 10 11

use strict;
use Exporter;
use vars qw(@ISA @EXPORT @EXPORT_OK
12
	    $VTOP_FLAGS_UPDATE $VTOP_FLAGS_VERBOSE $VTOP_FLAGS_QUIET
13
	    $VTOP_FLAGS_FIXNODES $VTOP_FLAGS_IMPOTENT $VTOP_FLAGS_ALLOCONLY
14
	    $VTOP_FLAGS_REGRESSION);
15 16 17 18 19

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

use libdb;
20
use libtblog_simple;
21 22 23 24 25 26 27
use libtestbed;
use Experiment;
use VirtExperiment;
use Node;
use NodeType;
use Lan;
use OSinfo;
28
use Port;
29 30 31
use English;
use Data::Dumper;
use Carp;
32
use POSIX;
33
use XML::LibXML;
34
use XML::Simple;
35 36

# Configure variables
37 38
my $TB		  = "@prefix@";
my $BOSSNODE      = "@BOSSNODE@";
39
my $AVAIL         = "$TB/bin/avail";
40 41 42
my $NALLOC        = "$TB/bin/nalloc";
my $NFREE         = "$TB/bin/nfree";
my $OS_SELECT     = "$TB/bin/os_select";
43
my $DELAYCAPACITY = @DELAYCAPACITY@;	# Can be overridden by user.
44
my $DELAYTHRESH   = @DELAYTHRESH@;
45
my $PGENISUPPORT  = @PROTOGENI_SUPPORT@;
46
my $OURDOMAIN     = "@OURDOMAIN@";
47 48 49 50 51 52
my $mycmurn;

if ($PGENISUPPORT) {
    require GeniHRN;
    $mycmurn = GeniHRN::Generate("@OURDOMAIN@", "authority", "cm");
}
53 54 55 56

# Flags.
$VTOP_FLAGS_VERBOSE	= 0x01;
$VTOP_FLAGS_UPDATE	= 0x02;
57
$VTOP_FLAGS_FIXNODES	= 0x04;
58
$VTOP_FLAGS_IMPOTENT	= 0x08;
59
$VTOP_FLAGS_REGRESSION  = 0x10;
60
$VTOP_FLAGS_QUIET       = 0x20;
61
$VTOP_FLAGS_ALLOCONLY	= 0x40;
62

63
@EXPORT_OK = qw($VTOP_FLAGS_UPDATE $VTOP_FLAGS_VERBOSE $VTOP_FLAGS_FIXNODES
64 65
		$VTOP_FLAGS_IMPOTENT $VTOP_FLAGS_REGRESSION $VTOP_FLAGS_QUIET
		$VTOP_FLAGS_ALLOCONLY);
66 67 68 69

#
# Create an object representing the stuff we need to create the vtop file.
#
70
sub Create($$$$)
71
{
72
    my ($class, $experiment, $user, $flags) = @_;
73 74 75 76 77 78 79 80 81

    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;
82
    $self->{'USER'}       = $user;
83 84 85
    $self->{'VIRTEXPT'}   = $virtexperiment;
    $self->{'FLAGS'}      = $flags;
    $self->{'VNODES'}     = {};
86 87
    $self->{'DELAYNODES'} = {};
    $self->{'LANNODES'}   = {};
88
    $self->{'BRIDGES'}    = {};
89
    $self->{'VLANS'}      = {};
Leigh Stoller's avatar
Leigh Stoller committed
90
    $self->{'VPATHS'}     = {};
91 92 93 94
    $self->{'MEMBEROF'}   = {};
    $self->{'COUNTERS'}   = {};
    $self->{'EXPTSTATS'}  = {};
    $self->{'DELAYLINKS'} = {};
Leigh Stoller's avatar
Leigh Stoller committed
95
    $self->{'VLINKS'}     = {};
96 97
    $self->{'OPTIONS'}    = {};
    $self->{'DELAYID'}    = 0;
98
    $self->{'PHOSTID'}    = 0;
99
    $self->{'IFACEID'}    = 32768;
100
    $self->{'PORTBW'}     = {};
101 102 103 104
    $self->{'RESULTS'}    = { "nodes"  => [],
			      "links"  => [],
			      "class"  => [],
			      "fixed"  => [] };
105
    $self->{'RSPEC'}      = {};
106
    $self->{'GENIRSPEC'}  = undef;
107 108

    # Mostly for update mode.
109 110 111 112
    $self->{'FIXEDNODES'}  = {};
    $self->{'CURRENT_V2P'} = {};
    $self->{'CURRENT_P2V'} = {};
    $self->{'CURRENT_V2V'} = {};
113 114
    $self->{'OLDRSRVCLEAN_FLAG'}  = 0;
    $self->{'OLDRSRVCLEAN_NODES'} = {};
115 116

    # Below is for interpretation of assign results.
117
    $self->{'PNODES'}      = {};
118 119
    $self->{'SOLUTION'}    = {};
    $self->{'NEWRESERVED'} = {};	# Newly reserved nodes.
120
    $self->{'NORECOVER'}   = 0;	
121
    
122
    bless($self, $class);
123 124
    $virtexperiment->Dump()
	if (0 && $self->verbose());
125 126 127 128 129

    return $self;
}
# accessors
sub experiment($)       { return $_[0]->{'EXPERIMENT'}; }
130
sub user($)             { return $_[0]->{'USER'}; }
131 132 133
sub virtexperiment($)   { return $_[0]->{'VIRTEXPT'}; }
sub flags($)            { return $_[0]->{'FLAGS'}; }
sub vnodes($)           { return $_[0]->{'VNODES'}; }
134
sub delaynodes($)       { return $_[0]->{'DELAYNODES'}; }
135
sub bridges($)          { return $_[0]->{'BRIDGES'}; }
136
sub lannodes($)         { return $_[0]->{'LANNODES'}; }
137
sub vlans($)            { return $_[0]->{'VLANS'}; }
138
sub memberof($)         { return $_[0]->{'MEMBEROF'}; }
Leigh Stoller's avatar
Leigh Stoller committed
139
sub vpaths($)           { return $_[0]->{'VPATHS'}; }
140
sub counters($)         { return $_[0]->{'COUNTERS'}; }
141
sub counter($$)         { return $_[0]->{'COUNTERS'}->{$_[1]}; }
142
sub options($)          { return $_[0]->{'OPTIONS'}; }
143 144
sub option($$)          { return (exists($_[0]->{'OPTIONS'}->{$_[1]}) ?
				  $_[0]->{'OPTIONS'}->{$_[1]} : undef); }
145
sub exptstats($)        { return $_[0]->{'EXPTSTATS'}; }
Leigh Stoller's avatar
Leigh Stoller committed
146
sub vlinks($)           { return $_[0]->{'VLINKS'}; }
147 148 149
sub delaylinks($)       { return $_[0]->{'DELAYLINKS'}; }
sub delaynodecount()    { return scalar(keys(%{ $_[0]->delaynodes() })); }
sub portbw($)           { return $_[0]->{'PORTBW'}; }
150
sub results($)          { return $_[0]->{'RESULTS'}; }
151 152 153 154 155
sub current_v2p($)      { return $_[0]->{'CURRENT_V2P'}; }
sub current_p2v($)      { return $_[0]->{'CURRENT_P2V'}; }
sub current_v2v($)      { return $_[0]->{'CURRENT_V2V'}; }
sub pnodes($)           { return $_[0]->{'PNODES'}; }
sub fixednodes($)       { return $_[0]->{'FIXEDNODES'}; }
156
sub newreserved($)      { return $_[0]->{'NEWRESERVED'}; }
157
sub rspec($)            { return $_[0]->{'RSPEC'}; }
158
sub genirspec($)        { return $_[0]->{'GENIRSPEC'}; }
159
sub newreservednodes($) { return keys(%{ $_[0]->{'NEWRESERVED'} }); }
160
sub oldreservednodes($) { return $_[0]->{'OLDRSRVCLEAN_NODES'}; }
161
sub norecover($)        { return $_[0]->{'norecover'}; }
162 163 164 165 166 167 168 169 170
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"); }
171 172
sub virt_lans($)        { return $_[0]->virt_table("virt_lans"); }
sub virt_lan_lans($)    { return $_[0]->virt_table("virt_lan_lans"); }
Leigh Stoller's avatar
Leigh Stoller committed
173
sub virt_paths($)       { return $_[0]->virt_table("virt_paths"); }
174
sub virt_bridges($)     { return $_[0]->virt_table("virt_bridges"); }
175 176
sub virt_desires($)	{ return $_[0]->virt_table("virt_node_desires"); }
sub virt_startloc($)	{ return $_[0]->virt_table("virt_node_startloc"); }
177 178 179 180
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"); }
181 182 183 184

# 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]}); }
185 186 187

# Debug output.
sub verbose($)		{ return $_[0]->flags() & $VTOP_FLAGS_VERBOSE; }
188
sub quiet($)		{ return $_[0]->flags() & $VTOP_FLAGS_QUIET; }
189
sub updating($)		{ return $_[0]->flags() & $VTOP_FLAGS_UPDATE; }
190
sub fixcurrent($)	{ return $_[0]->flags() & $VTOP_FLAGS_FIXNODES; }
191
sub impotent($)		{ return $_[0]->flags() & $VTOP_FLAGS_IMPOTENT; }
192
sub alloconly($)	{ return $_[0]->flags() & $VTOP_FLAGS_ALLOCONLY; }
193
sub regression($)	{ return $_[0]->flags() & $VTOP_FLAGS_REGRESSION; }
194
sub printdb($$)		{ print $_[1] if ($_[0]->verbose()); return 1; }
195

196
# We name delay nodes internally as they are needed.
197
sub nextdelayname($)    { return "tbdelay" . $_[0]->{'DELAYID'}++; }
198
# For when the user wants a specific delay os. Use a desire.
199 200 201 202
# 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"); }
203 204
# For XML
sub nextifacenumber($)  { return $_[0]->{'IFACEID'}++; }
205
sub nextphostnumber($)  { return $_[0]->{'PHOSTID'}++; }
206

207 208 209 210
# Virtual Types. 
sub virttypeisvtype($$) { return $_[0]->virt_vtypes()->Find($_[1]); }
sub VirtTypes($)        { return $_[0]->virt_vtypes()->Rows(); }

211 212 213
# Caller will want these.
sub minimum_nodes($)    { return $_[0]->counter("minimum_nodes"); }
sub maximum_nodes($)    { return $_[0]->counter("maximum_nodes"); }
214
sub nodecount($)	{ return $_[0]->counter("nodecount"); }
215
sub plabcount($)	{ return $_[0]->counter("plabcount"); }
216
sub genicount($)	{ return $_[0]->counter("genicount"); }
217 218 219
sub virtnodecount($)	{ return $_[0]->counter("virtcount"); }
sub simnodecount($)	{ return $_[0]->counter("simcount"); }
sub remotenodecount($)	{ return $_[0]->counter("remotecount"); }
220
sub sharednodecount($)	{ return $_[0]->counter("sharedcount"); }
221

222
sub createLink($$$$$$$$$)
223
{
224 225 226
    # $others here will be a hashtable for the link flags.  The table
    # could also contain non-default values for latency and
    # packet_loss
227
    my ($self, $name, $plink, $cm, $src, $dst, $bw, $type, $others) = @_;
228
    my $ref = {
229 230 231
	'virtual_id'     => $name,
	'plink'		 => $plink,
	'manager_urn'    => $cm,
232
	'link_type'      => $type,
233 234 235 236 237 238 239 240 241
	'interface_ref'  => [$src, $dst],
	'capacity'       => $bw,
	'packet_loss'    => "0",
	'latency'        => "0"
    };
    
    if ($others) {
	while ( my ($key, $value) = each %{$others} ) {
	    $ref->{$key} = $value;
242
	}
243 244 245 246 247
    }
    if (!exists($self->rspec()->{'link'})) {
	$self->rspec()->{'link'} = [];
    }
    push(@{ $self->rspec()->{'link'} }, $ref);
248 249
}

250
sub createNode ($$$$$$$;$)
251
{
252 253 254
    # $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
255
    my ($self, $name, $cm, $type, $typecount, $desires, $others, $ifaces) = @_;
256
    my $ref = {
257 258
	'virtual_id'    => $name,
	'manager_urn'   => $cm,
259 260 261 262 263 264 265 266 267 268 269 270
    };
    
    my $typename = $type;
    my $typeslots = $typecount;
    my $isstatic = 0;
    if ($typename =~ /^\*([-\w]*)$/) {
	$typename = $1;
	$isstatic = 1;
    }
    if ($typecount eq '*') {
	$typeslots = 'unlimited';
    }
271

272 273
    # We will need this to generate interface names later
    $ref->{'interface_count'} = 0;
274
    $ref->{'interfaces'} = $ifaces;
275 276 277 278 279 280 281 282 283

    $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;
284
	}
285 286 287 288 289 290
    }
    $ref->{'desires'} = $desiretable;
    
    if ($others) {
	while ( my ($key, $value) = each %{$others} ) {
	    $ref->{$key} = $value;
291
	}
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    }
    
    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,
318 319
		# XXX Going to need a URN in rspec output ...
		'pnode' => $pnode,
320 321 322 323 324 325 326
    };
    if (!exists($self->rspec()->{'fixed'})) {
	$self->rspec()->{'fixed'} = [];
    }
    push(@{ $self->rspec()->{'fixed'} }, $ref);
}

327 328 329 330 331 332 333
###############################################################################
# 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);
334
use overload ('""' => 'Stringify');
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

# 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);

365 366 367 368 369
    $self->{'VIRTNODE'}   = $virt_node;
    $self->{'VTOP'}       = $vtop;
    $self->{'HASH'}       = {};
    # The virtlans this virtnode is a member of. 
    $self->{'MEMBERSHIP'} = {};
370
    
371 372 373 374 375 376
    return $self;
}
# accessors
sub virt_node($)	{ return $_[0]->{'VIRTNODE'}; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
377 378
sub membership($)       { return $_[0]->{'MEMBERSHIP'}; }
sub memberlist($)       { return values(%{ $_[0]->{'MEMBERSHIP'} }); }
379 380 381 382 383 384 385 386

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

    $self->{'VIRTNODE'}   = undef;
    $self->{'VTOP'}       = undef;
    $self->{'HASH'}       = undef;
387
    $self->{'MEMBERSHIP'} = undef;
388 389
}

390 391 392 393 394 395 396 397 398 399 400 401 402 403 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 429 430 431 432 433 434 435 436 437
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;
438 439
    $self->{'MEMBERHASH'}    = {};
    $self->{'MEMBERLIST'}    = [];
440 441 442 443 444 445 446
    $self->{'SHAPEDMEMBERS'} = {};
    $self->{'HASH'}          = {};

    return $self;
}
# accessors
sub virt_lanlan($)	{ return $_[0]->{'VIRTLANLAN'}; }
447 448
sub members($)		{ return $_[0]->{'MEMBERHASH'}; }
sub memberlist($)       { return @{ $_[0]->{'MEMBERLIST'} }; }
449
sub member($$)          { return @{ $_[0]->{'MEMBERLIST'} }[$_[1]]; }
450 451 452
sub shapedmembers($)	{ return $_[0]->{'SHAPEDMEMBERS'}; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
453
sub islink($)           { return (@{ $_[0]->{'MEMBERLIST'} } == 2 ? 1 : 0); }
454 455 456 457 458 459

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

    $self->{'VIRTLANLAN'} = undef;
460 461
    $self->{'MEMBERHASH'} = undef;
    $self->{'MEMBERLIST'} = undef;
462 463 464 465 466 467 468 469 470 471 472 473
    $self->{'VTOP'}       = undef;
    $self->{'HASH'}       = undef;
}

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

    return "[vlan:$vname]";
}

474 475 476 477 478
sub addmember($$)
{
    my ($self, $vlanmember) = @_;
    
    $self->members()->{$vlanmember->member()} = $vlanmember;
479 480

    @{ $self->{'MEMBERLIST'} }[$vlanmember->vindex()] = $vlanmember;
481 482 483 484

    return 0;
}

485 486 487 488 489 490 491 492
#
# Other support functions.
#
sub usevirtiface($)
{
    my ($self) = @_;
    my $encap  = $self->_encapstyle();

493
    return ($encap eq "veth-ne" || $encap eq "vlan");
494 495 496 497 498 499 500 501
}
sub membershaped($$) {
    my ($self, $member) = @_;
    return $self->shapedmembers()->{"$member"};
}
sub setmembershaped($$) {
    my ($self, $member) = @_;
    $self->shapedmembers()->{"$member"} = 1;
502
    $member->_shaped(1);
503 504 505 506 507 508 509 510 511
}

###############################################################################
# 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);
512
use overload ('""' => 'Stringify', 'eq' => 'StringEQ');
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577

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

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
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 Stoller's avatar
Leigh Stoller committed
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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
###############################################################################
# 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'}   = [];
637
    $self->{'IMPLEMENTS'}    = {};
Leigh Stoller's avatar
Leigh Stoller committed
638 639 640 641 642 643 644 645 646
    $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'} }; }
647
sub virtlanlist($)      { return @{ $_[0]->{'VIRTLANLIST'} }; }
Leigh Stoller's avatar
Leigh Stoller committed
648 649 650 651
sub member($$)		{ return $_[0]->{'MEMBERLIST'}->[$_[1]]; }
sub lanlink($$)		{ return $_[0]->{'VIRTLANLIST'}->[$_[1]]; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
652
sub implements($)	{ return $_[0]->{'IMPLEMENTS'}; }
Leigh Stoller's avatar
Leigh Stoller committed
653 654 655 656 657 658 659 660 661 662 663 664

# 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;
665
    $self->{'IMPLEMENTS'} = undef;
Leigh Stoller's avatar
Leigh Stoller committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    $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;
}
717

718 719 720 721 722 723 724 725 726 727 728 729 730
sub addimplements($$)
{
    my ($self, $virtlan) = @_;

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

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

731 732 733
#############################################################################
# Back to the main package.
#
734
package libvtop_stable;
735

736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787
#
# 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 = ();
    
    # XXX: PlanetLab hack - PlanetLab 'control' interfaces are also
    # 'experimental' interfaces! We probably need a way to express
    # this in the interfaces table or interface_types
    #
    $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 ".
		    "where i.role='" . TBDB_IFACEROLE_EXPERIMENT . "' ".
		    "      or (n.type='pcplabphys' and i.role='" .
		                 TBDB_IFACEROLE_CONTROL . "')");
    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)) {
788
	    warn("No type info for node type $node_type\n");
789 790 791 792 793 794 795 796 797 798
	    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"})) {
799
	    warn("No protocols listed in capabilities for $iface_type!\n");
800 801 802 803 804 805 806 807 808 809
	    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)) {
810
		warn("No default speed in capabilites for $iface_type!\n");
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836
		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;
		}
	    }
	}
    }
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858

    #
    # 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.
    #
859 860 861
    foreach my $ref ($self->VirtTypes()) {
	my $vtype   = $ref->name();
	my @members = split(" ", $ref->members());
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889

	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");
	    }
	}
    }
    
890 891 892 893 894
    $self->{'IFACECAPS'}  = \%interface_capabilities;
    $self->{'TYPELINKBW'} = \%node_type_linkbw;
    
    return 0;
}
895 896 897 898 899 900
sub interfacespeedmbps($$$)
{
    my ($self, $type, $which) = @_;
        
    return $self->{'IFACECAPS'}->{$type}->{"${which}_defspeed"}/1000.0;
}
901

902 903 904 905
#
# When updating with fixednodes turned on, we need the current set
# of nodes that need to be fixed.
#
906
sub LoadCurrentResources($)
907 908 909 910 911 912 913
{
    my ($self) = @_;

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

914 915
    $self->printdb("Loading current resources" .
		   ($self->regression() ? " in regression mode" : "") . "\n");
916

917
    my @nodelist = $self->experiment()->NodeList(0, 1);
918 919 920
    return 0
	if (!@nodelist);

921 922 923 924 925 926 927 928 929
    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()) {
930
		$node_id = $pnode->phys_nodeid();
931
	    }
932 933
	    $self->fixednodes()->{$vname} = $node_id;
	    $self->printdb("  fixing $vname -> $node_id\n");
934 935 936 937
	}
	return 0;
    }

938
    foreach my $pnode (@nodelist) {
939 940 941 942 943 944 945 946 947
	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;

948 949 950 951
	#
	# WIDEAREA nodes are going to break.
	#
	if ($pnode->isremotenode() &&
952 953
	    !($pnode->isplabdslice() || $pnode->isfednode() ||
	      $pnode->isdedicatedremote())) {
954 955 956 957 958
	    tberror("Cannot update widearea nodes yet!\n");
	    return -1;
	}
	if ($pnode->isvirtnode()) {
	    $self->counters()->{'reserved_virtcount'}++;
959 960 961 962 963 964 965

	    # 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;
	    }
966
	    my $ppnode_id = $ppnode->node_id();
967

968 969 970
	    $self->fixednodes()->{$vname} = $ppnode_id
		if ($self->fixcurrent());
	    
971 972 973
	    #
	    # Record the mappings. 
	    #
974 975
	    $self->current_v2v()->{$vname} = $pnode->node_id();
	    $self->current_v2p()->{$vname} = $ppnode->node_id();
976 977 978 979 980
	    push(@{ $self->current_p2v()->{$ppnode->node_id()} }, $vname);
	    
	    # Mark the node as unused until later.
	    $pnode->_reuse("unused");
	    $ppnode->_reuse("unused");
981 982 983 984 985 986 987 988 989

	    #
	    # 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;
990 991
	    
	    $self->printdb("current v2p: $node_id ($ppnode_id) -> $vname\n");
992 993 994 995 996 997 998 999 1000 1001
	}
	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 {
1002 1003 1004
		$self->fixednodes()->{$vname} = $pnode->node_id()
		    if ($self->fixcurrent());
		
1005
		$self->counters()->{'reserved_physcount'}++;
1006 1007 1008 1009

		#
		# Record the mapping. 
		#
1010
		$self->current_v2p()->{$vname} = $pnode->node_id();
1011 1012 1013 1014
		push(@{ $self->current_p2v()->{$node_id} }, $vname);
		# Mark the node as unused until later.
		$pnode->_reuse("unused");

1015 1016 1017 1018 1019 1020 1021
		#
		# 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;

1022
		$self->printdb("current v2p: $node_id -> $vname\n");
1023 1024 1025 1026 1027 1028
	    }
	}
    }
    return 0;
}

1029 1030 1031
sub LoadVirtNodes($)
{
    my ($self) = @_;
1032
    my $gotgeninodes = 0;
1033 1034 1035

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

1036 1037 1038 1039
    my $pid    = $self->pid();
    my $eid    = $self->eid();
    my $table  = $self->virt_nodes();

1040
    foreach my $virt_node ($table->Rows()) {
1041 1042 1043 1044
	my $vnode    = libvtop::virt_node->Create($self, $virt_node);
	my $vname    = $vnode->vname();
	my $desires  = {};
	my $startloc = undef;
1045 1046

	# Other fields we need.
1047
	my $ips     = $vnode->ips() || "";
1048 1049 1050
	my $type    = $vnode->type();
	my $fixed   = $vnode->fixed();
	my $osname  = $vnode->osname();
1051
	my $parent_osname  = $vnode->parent_osname();
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064

	# 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.
1065
	my $isvtyped= 0;
1066
	my $isded   = 0;
1067
	my $isgeni  = 0;
1068 1069 1070 1071 1072 1073

	# If we have a real type or auxtype ...
	my $nodetype = NodeType->LookupAny($type);
	if (!defined($nodetype)) {
	    my $vtype = $self->virttypeisvtype($type);
	    if (!defined($vtype)) {
1074
		warn("Improper type $type for node $vnode!\n");
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
		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)) {
1085
		warn("Improper type $vtypename in vtypes for node $vnode!\n");
1086 1087
		return -1;
	    }
1088
	    $isvtyped = 1;
1089
	}
1090 1091
	$vnode->_typeinfo($nodetype);
	
1092 1093 1094 1095
	$isremote = $nodetype->isremotenode();
	$isvirt   = $nodetype->isvirtnode();
	$issub    = $nodetype->issubnode();
	$isplab   = $nodetype->isplabdslice();
1096
	$isgeni   = $nodetype->isfednode();
1097 1098
	$issim    = $nodetype->issimnode();
	$isdyn    = $nodetype->isdynamic();
1099
	$isded    = $nodetype->isdedicatedremote();
1100

1101 1102 1103
	# Mark this as being a virtual typed node.
	$vnode->_isvtyped($isvtyped);

1104 1105 1106
	# dynamic inclusion below.
	$gotgeninodes++ if ($isgeni);

1107 1108 1109 1110 1111 1112
	# All this info is stashed in our local object.
	$vnode->_nodeweight(undef);
	$vnode->_isremotenode($isremote);
	$vnode->_isvirtnode($isvirt);
	$vnode->_issubnode($issub);
	$vnode->_isplabnode($isplab);
1113
	$vnode->_isgeninode($isgeni);
1114 1115
	$vnode->_issimnode($issim);
	$vnode->_isdynamic($isdyn);
1116
	$vnode->_isdedremote($isded);
1117 1118
	# Set below from a desire.
	$vnode->_sharedokay(0);
1119
	$vnode->_fixedvm(undef);
1120
	$vnode->_isbridge(($vnode->role() eq "bridge" ? 1 : 0));
1121

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

1125 1126 1127
	# dynamic inclusion below.
	$gotgeninodes++ if ($isgeni);

1128 1129
	# The mapped osname to actual osinfo structure.
	$vnode->_osinfo(undef);
1130 1131
	# If the virtnode tries to specify its parent os in addition
	# to its own, store that osinfo here.
1132
	$vnode->_parent_osinfo(undef);
1133 1134 1135 1136 1137 1138 1139
	# Eventual physical mapping. 
	$vnode->_physnode(undef);
	# Handy to combine these.
	$vnode->_settings([ $vnode->cmd_line(),
			    $vnode->rpms(),
			    $vnode->startupcmd(),
			    $vnode->tarfiles(),
1140
			    $vnode->loadlist(),
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
			    $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 "") {
1153
		warn("Subnode $vname must be fixed to its parent!\n");
1154 1155 1156
		return -1;
	    }
	    $vnode->_parent($fixed);
1157
	    $vnode->fixed(undef);
1158 1159 1160 1161 1162
	    undef($fixed);
	}

	# Can fixed really get set to ""?
	if (defined($fixed) && $fixed ne "") {
1163
	    # Store the name since we use FIXED_NODES for delay nodes too.
1164
	    $self->fixednodes()->{$vname} = $fixed;
1165 1166 1167 1168 1169

	    # 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)
1170
		if ($isvirt && !$isgeni);
1171
	}
Leigh Stoller's avatar
Leigh Stoller committed
1172 1173
	else {
	    $vnode->fixed(undef);
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199
	    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 Stoller's avatar
Leigh Stoller committed
1200
	}
1201 1202

	$self->printdb("  $vname type:$type ips:$ips\n");
1203
	$self->printdb("         isrem:$isremote isvirt:$isvirt");
1204 1205
	$self->printdb(" fixed:$fixed")
	    if (defined($fixed) && $fixed ne "");
1206
	$self->printdb("\n");
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
	
	# 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.
1218
	my @iplist  = split(" ", $ips);
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
	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 "") {
1232 1233 1234 1235 1236 1237 1238
	    my $ospid = $pid;
	    
	    if ($osname =~ /^(.*)\/(.*)$/) {
		$ospid  = $1;
		$osname = $2;
	    }
	    my $osinfo = OSinfo->Lookup("$ospid,$osname");
1239 1240 1241 1242 1243 1244
	    if (!defined($osinfo)) {
		$osinfo = OSinfo->LookupByName($osname);

		if (!defined($osinfo)) {
		    tberror({cause => 'user', type => 'primary',
			     severity => SEV_ERROR,
1245 1246
			     error => ['invalid_os', undef, $osname, $ospid]},
			    "Invalid OS $osname in project $ospid!");
1247 1248 1249 1250 1251
		    return -1;
		}
	    }
	    $vnode->_osinfo($osinfo);
	}
1252
	elsif ($isvirt) {
1253 1254 1255 1256 1257 1258 1259 1260 1261
	    my $osinfo;
	    if ($nodetype->type() eq "pcvm") {
		# Silly default.
		$osinfo = OSinfo->LookupByName("OPENVZ-STD");
	    }
	    else {
		# Grab it from the typeinfo.
		$osinfo = OSinfo->Lookup($nodetype->default_osid());
	    }
1262 1263
	    $vnode->_osinfo($osinfo);
	}
1264

1265 1266 1267 1268
	#
	# Map the parent_osname to an OSID now.
	#
	if (defined($parent_osname) && $parent_osname ne "") {
1269 1270 1271 1272 1273 1274 1275
	    my $ospid = $pid;
	    
	    if ($parent_osname =~ /^(.*)\/(.*)$/) {
		$ospid  = $1;
		$parent_osname = $2;
	    }
	    my $osinfo = OSinfo->Lookup("$ospid,$parent_osname");
1276 1277 1278 1279 1280 1281
	    if (!defined($osinfo)) {
		$osinfo = OSinfo->LookupByName($parent_osname);

		if (!defined($osinfo)) {
		    tberror({cause => 'user', type => 'primary',
			     severity => SEV_ERROR,
1282 1283 1284
			     error => ['invalid_os',
				       undef, $parent_osname, $ospid]},
		    "Invalid parent OS $parent_osname in project $ospid!");
1285 1286 1287 1288 1289 1290
		    return -1;
		}
	    }
	    $vnode->_parent_osinfo($osinfo);
	}

1291 1292 1293 1294 1295 1296
	#
	# Add in desires.
	#
	foreach my $desire ($self->virt_desires()->Rows()) {
	    next
		if ($desire->vname() ne $vname);
1297
	    
1298
	    $desires->{$desire->desire()} = $desire->weight();
1299 1300 1301 1302 1303

	    # User says a shared node is okay. We need this info later
	    # when generating links/lans for the vtop.
	    $vnode->_sharedokay(1)
		if ($isvirt && $desire->desire() eq "pcshared");
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
	}
	$vnode->_desires($desires);

	#
	# And the startloc, but doubt this is used anymore.
	#
	foreach my $startloc ($self->virt_startloc()->Rows()) {
	    if ($startloc->vname() eq $vname) {
		$startloc = $startloc->building();
		last;
	    }
	}
	$vnode->_startloc($startloc);

1318
	# Counters
1319
	$self->{'COUNTERS'}->{'nodecount'}++;
1320 1321 1322 1323 1324 1325 1326 1327
	$self->{'COUNTERS'}->{'simcount'}++
	    if ($issim);
	$self->{'COUNTERS'}->{'remotecount'}++
	    if ($isremote);
	$self->{'COUNTERS'}->{'virtcount'}++
	    if ($isvirt);
	$self->{'COUNTERS'}->{'plabcount'}++
	    if ($isplab);
1328 1329
	$self->{'COUNTERS'}->{'genicount'}++
	    if ($isgeni);
1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	$self->{'COUNTERS'}->{'physcount'}++
	    if (!$issim && !$isvirt);
	$self->{'COUNTERS'}->{'sharedcount'}++
	    if ($isvirt && $vnode->_sharedokay());
	# stats
	my $ipcount = scalar(@iplist);

	$self->exptstats()->{"maxlinks"} = $ipcount
	    if ($ipcount > $self->exptstats()->{"maxlinks"});
	$self->exptstats()->{"minlinks"} = $ipcount
	    if ($ipcount < $self->exptstats()->{"minlinks"});

1342
	# Add to the list.
1343
	$self->vnodes()->{$vname} = $vnode;
1344
    }
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355
    if ($gotgeninodes) {
	if ($PGENISUPPORT) {
	    require libGeni;
	    require GeniXML;
	    import GeniXML;
	}
	else {
	    tberror("Using geni nodes but PROTOGENI_SUPPORT is not defined\n");
	    return -1;
	}
    }
1356 1357 1358 1359 1360 1361 1362 1363 1364

    #
    # Go back and look for any VMs that were fixed to nodes. Need to make
    # the OS consistent.
    #
    foreach my $vname (sort(keys(%{ $self->{'VNODES'} }))) {
	my $vnode = $self->vnodes()->{$vname};
	my $osid;

1365 1366 1367
	next
	    if ($vnode->_isgeninode());

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 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
	next
	    if (! ($vnode->_isvirtnode() && $vnode->_fixedvm()));
	
	my $fixnode = $self->vnodes()->{$vnode->_fixedvm()};
	#
	# If the node is fixed to a physical node, skip. This is a
	# plain old fixnode operation, not our new support for fixing
	# a VM to a real node in the topology.
	#
	if (!defined($fixnode)) {
	    # Lets make sure its a real node.
	    my $fixvname = $vnode->_fixedvm();
	    
	    if (!defined(Node->Lookup($fixvname))) {
		tberror("Cannot fix $vnode to $fixvname; No such node\n");
		return -1;
	    }
	    $vnode->_fixedvm(undef);
	    next;
	}

	if (defined($vnode->_parent_osinfo())) {
	    $osid = $vnode->_parent_osinfo()->osid();
	}
	else {
	    $osid  = ($self->option("jail_osid") ||
		      $self->nodejailosid($vnode));
	}
	return -1
	    if (!defined($osid));
	my $osinfo = OSinfo->Lookup($osid);
	return -1
	    if (!defined($osinfo));
	
	$fixnode->_osinfo($osinfo);
	# Convert from the name to the local object.
	$vnode->_fixedvm($fixnode);

	$self->printdb("Fixing VM $vname to $fixnode, $osinfo\n");
    }
    
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431
    return 0;
}

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

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

    my $pid    = $self->pid();
    my $eid    = $self->eid();
    my $table  = $self->virt_lans();

    foreach my $virt_lan_member ($table->Rows()) {
	my $vlanname = $virt_lan_member->vname();

	# Local wrapper for virt_lan_lan table entry (the "lan").
	my $virtlan = $self->vlans()->{$vlanname};
	if (!defined($virtlan)) {
	    my $virt_lan_lan = $self->virt_lan_lans()->Find($vlanname);
	    $virtlan = libvtop::virt_lan->Create($self, $virt_lan_lan);
	    # Add it to the toplevel list of lans.
	    $self->vlans()->{$vlanname} = $virtlan;
1432
	    $virtlan->_accesspoint(undef);
1433 1434 1435 1436 1437 1438 1439 1440 1441
	}

	# Now the local wrapper for the virt_lan table entry (the "member").
	my $vlanmember = libvtop::virt_lan_member->Create($self,
							  $virt_lan_member,
							  $virtlan);

	# Which we add to the member hash for the lan by vnode:vport
	# Note that $vlanmember->member() returns vnode:port.
1442
	$virtlan->addmember($vlanmember);
1443 1444 1445 1446

	# Global map from vnode:port back to the lan object
	$self->memberof()->{$vlanmember->member()} = $virtlan;

1447 1448 1449
	# Each virt_node has a list of all virt_lans it belongs to.
	$vlanmember->virt_node()->membership()->{"$virtlan"} = $virtlan;

1450 1451 1452 1453
	# Other fields we need below
	my $delay          = $vlanmember->delay();
	my $bandwidth      = $vlanmember->bandwidth();
	my $est_bandwidth  = $vlanmember->est_bandwidth();
1454
	my $backfill       = $vlanmember->backfill();
1455 1456 1457 1458
	my $lossrate       = $vlanmember->lossrate();
	my $rdelay         = $vlanmember->rdelay();
	my $rbandwidth     = $vlanmember->rbandwidth();
	my $rest_bandwidth = $vlanmember->rest_bandwidth();
1459
	my $rbackfill      = $vlanmember->rbackfill();
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
	my $rlossrate      = $vlanmember->rlossrate();
	my $widearea       = $vlanmember->widearea();
	my $isemulated     = $vlanmember->emulated();
	my $uselinkdelay   = $vlanmember->uselinkdelay();
	my $nobwshaping    = $vlanmember->nobwshaping();
	my $trivial_ok     = $vlanmember->trivial_ok();
	my $protocol       = $vlanmember->protocol();
	my $mustdelay      = $vlanmember->mustdelay();
	my $encap          = $vlanmember->encap_style();
	my $mask           = $vlanmember->mask();
1470
	my $ip             = $vlanmember->ip();
1471
	my $layer          = $vlanmember->layer();
Leigh Stoller's avatar
Leigh Stoller committed
1472
	my $implemented_by = $vlanmember->implemented_by_path();
1473
	my $fixed_iface    = $vlanmember->fixed_iface();
1474 1475 1476 1477
	my ($vname,$vport) = split(":", $vlanmember->member());

	# virt_nodes:ips is actually deprecated; this overrides it.
	$self->{'IPS'}->{"$vname:$vport"} = $ip;
1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508

	#
	# So all this stuff is really per-lan state, but an artifact of
	# the original implementation is that it is duplicated in every
	# single member row. So, push the info up a level to make it easy
	# to figure out how each lan is set up.
	#

	# If RED, must insert traffic shapping.
	$virtlan->_mustdelay($mustdelay);
	# User has requested the link/lan be emulated. Not typical.
	$virtlan->_emulated($isemulated);
	# User has requested "endnodeshaping" (dummynet on end nodes).
	$virtlan->_uselinkdelay($uselinkdelay);
        # The nobwshaping flag is used in conjunction with emulated
        # links to turn off actual bw traffic shaping on an emulated
        # link. This allows assign to match the specified bws, but not
        # force them to be such with delay nodes (leaves it up to the
        # user to moderate the bw).
	$virtlan->_nobwshaping($nobwshaping);
	$virtlan->_encapstyle($encap);
	# User has said that colocating is okay. Not typical.
	$virtlan->_trivial_ok($trivial_ok);
	# Link is connected to a remote node, and gets a tunnel.
	$virtlan->_tunnel(0);
	# Netmask for the entire lan.
	$virtlan->_mask($mask);
	$virtlan->_widearea($widearea);
	# Whether all member nodes are simulated
	$virtlan->_allsim(0);
	$virtlan->_protocol($protocol);
1509
	$virtlan->_layer($layer);
1510 1511
	$virtlan->_accesspoint($vlanmember)
	    if ($vlanmember->is_accesspoint());
1512
	$virtlan->_sharednodes(0);
1513
	$virtlan->_geninodes(0);
Leigh Stoller's avatar
Leigh Stoller committed
1514 1515
	$virtlan->_implemented_by($implemented_by);
	$virtlan->_vpath(undef);
1516 1517
	$virtlan->_bridged(0);
	
1518 1519 1520 1521 1522 1523 1524 1525 1526
	if (defined($encap) &&
	    ($encap eq "vtun" || $encap eq "gre" || $encap eq "egre")) {
	    $virtlan->_tunnel(1);
	}

	# Store this stuff as a unit to make it easier to grab later.
	$vlanmember->_delayinfo([ $delay,
				  $bandwidth,
				  $est_bandwidth,
1527
				  $backfill,
1528 1529 1530 1531
				  $lossrate,
				  $rdelay,
				  $rbandwidth,
				  $rest_bandwidth,
1532
				  $rbackfill,
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
				  $rlossrate ]);

	#
	# Ditto for the Q stuff, which is not needed until the delay
	# links are created. There are no "r" params either; Queue
	# stuff is handled in just the to-switch direction.
	#
	$vlanmember->_queueinfo([$vlanmember->q_limit(),
				 $vlanmember->q_maxthresh(),
				 $vlanmember->q_minthresh(),
				 $vlanmember->q_weight(),
				 $vlanmember->q_linterm(),
				 $vlanmember->q_qinbytes(),
				 $vlanmember->q_bytes(),
				 $vlanmember->q_meanpsize(),
				 $vlanmember->q_wait(),
				 $vlanmember->q_setbit(),
				 $vlanmember->q_droptail(),
				 $vlanmember->q_red(),
				 $vlanmember->q_gentle() ]);

	#
	# The trace info is stored along with the QUEUEINFO, but its
	# easier if I split it out.
	#
	$vlanmember->_traceinfo([$vlanmember->traced(),
				 $vlanmember->trace_endnode(),
				 $vlanmember->trace_type(),
				 $vlanmember->trace_expr(),
				 $vlanmember->trace_snaplen(),
				 $vlanmember->trace_db() ]);

	#
	# This will get set on a per-member basis when it is determined
	# that the link is getting a delaynode cause its really being
	# shaped, or because it is being traced or monitored.
	#
	$virtlan->shapedmembers()->{"$vlanmember"} = 0;
1571
	$vlanmember->_shaped(0);
1572
	$virtlan->_delayed(0);
1573
	$vlanmember->_bridged(0);
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592

	#
	# XXX - Whenever a delay node is inserted, port speeds are set to
	#       the next fastest interface type we have, even if they requested
	#       exactly some speed that can be done without a delay node such
	#       as 10Mbs or 100Mbs. This is a simplification. At some point we
	#       might want to force all the ports along the way to 10Mbs, and
	#       have the delay node worry about delay only, and not bandwidth.
	#       That will be harder to to do in this mess. See companion XXX
	#       below where the delays table is initialized. Initially, we set
	#       the speed to 10Mbs, if a delay node is insterted below, it
	#       resets this to 100Mbs.
	#
	my $portbw = $self->getbandwidth($vlanmember, $virtlan, $bandwidth);
	$self->portbw()->{"$vlanmember"} = $portbw;
	
	$self->printdb("  $vlanname $vlanmember portbw:$portbw - ".
		       "$delay $bandwidth $lossrate ".
		       "$rdelay $rbandwidth $rlossrate\n");
1593 1594

	# Temporary, for generating rspecs.
1595
	push(@{ $vlanmember->virt_node()->_virtifaces() }, $vlanmember);
1596 1597 1598 1599

	# Terrible.
	$vlanmember->_reservebw(0);
	$vlanmember->_needtrunk(0);
1600 1601
	if ($vlanmember->virt_node()->type() eq "sppvm" ||
	    $vlanmem