libvtop_stable.pm.in 311 KB
Newer Older
1
#!/usr/bin/perl -w
2
#
3
# Copyright (c) 2005-2016 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 32 33
	    $VTOP_FLAGS_REGRESSION $VTOP_FLAGS_FIXLANNODES
	    $VTOP_FLAGS_PREASSIGN);

34 35 36 37
@ISA    = "Exporter";
@EXPORT = qw( );

use libdb;
38
use libtblog_simple;
39 40
use libtestbed;
use Experiment;
41
use EmulabConstants;
42 43 44 45
use VirtExperiment;
use Node;
use NodeType;
use Lan;
46 47
use OSImage;
use Image;   # For datasets.
48
use Blockstore;
Leigh Stoller's avatar
Leigh Stoller committed
49
use Lease;
50
use Port;
51
use English;
52
use emutil qw(SpanningTree GenFakeMac);
53 54
use Data::Dumper;
use Carp;
55
use POSIX;
56
use XML::LibXML;
57
use XML::Simple;
58 59

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

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

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

90
@EXPORT_OK = qw($VTOP_FLAGS_UPDATE $VTOP_FLAGS_VERBOSE $VTOP_FLAGS_FIXNODES
91
		$VTOP_FLAGS_IMPOTENT $VTOP_FLAGS_REGRESSION $VTOP_FLAGS_QUIET
92 93
		$VTOP_FLAGS_ALLOCONLY $VTOP_FLAGS_FIXLANNODES
		$VTOP_FLAGS_PREASSIGN);
94 95 96 97

#
# Create an object representing the stuff we need to create the vtop file.
#
98
sub Create($$$$;$)
99
{
Leigh Stoller's avatar
Leigh Stoller committed
100
    my ($class, $experiment, $user, $flags, $realuser) = @_;
101 102 103 104 105 106

    my $virtexperiment = VirtExperiment->Lookup($experiment);
    if (!defined($virtexperiment)) {
	tberror("Could not load virtual experiment object for $experiment\n");
	return undef;
    }
Leigh Stoller's avatar
Leigh Stoller committed
107 108 109
    # Permission checks made for real user, not effective user.
    $realuser = $user
	if (!defined($realuser));
110 111 112

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

143 144 145
    # For forcing untyped nodes to an architecture.
    $self->{'FORCEARCH'}  = undef;
    
146
    # Mostly for update mode.
147 148
    $self->{'FIXEDNODES'}  = {};
    $self->{'CURRENT_V2P'} = {};
Leigh Stoller's avatar
Leigh Stoller committed
149
    $self->{'CURRENT_V2P'} = {};
150 151
    $self->{'CURRENT_P2V'} = {};
    $self->{'CURRENT_V2V'} = {};
152 153
    $self->{'OLDRSRVCLEAN_FLAG'}  = 0;
    $self->{'OLDRSRVCLEAN_NODES'} = {};
154

Leigh Stoller's avatar
Leigh Stoller committed
155 156 157
    # This is just for lannodes.
    $self->{'CURRENT_V2PMAP'} = {};

158
    # Below is for interpretation of assign results.
159
    $self->{'PNODES'}      = {};
160 161
    $self->{'SOLUTION'}    = {};
    $self->{'NEWRESERVED'} = {};	# Newly reserved nodes.
162
    $self->{'NORECOVER'}   = 0;	
163
    
164
    bless($self, $class);
165 166
    $virtexperiment->Dump()
	if (0 && $self->verbose());
167 168 169 170 171

    return $self;
}
# accessors
sub experiment($)       { return $_[0]->{'EXPERIMENT'}; }
172
sub user($)             { return $_[0]->{'USER'}; }
Leigh Stoller's avatar
Leigh Stoller committed
173
sub realuser($)         { return $_[0]->{'REALUSER'}; }
174 175
sub virtexperiment($)   { return $_[0]->{'VIRTEXPT'}; }
sub flags($)            { return $_[0]->{'FLAGS'}; }
176
sub types($)            { return $_[0]->{'TYPES'}; }
177
sub vnodes($)           { return $_[0]->{'VNODES'}; }
178
sub delaynodes($)       { return $_[0]->{'DELAYNODES'}; }
179
sub bridges($)          { return $_[0]->{'BRIDGES'}; }
180 181
sub blockstores($)      { return $_[0]->{'BLOCKSTORES'}; }
sub sanhosts($)         { return $_[0]->{'SANHOSTS'}; }
182
sub lannodes($)         { return $_[0]->{'LANNODES'}; }
183
sub vlans($)            { return $_[0]->{'VLANS'}; }
184
sub memberof($)         { return $_[0]->{'MEMBEROF'}; }
Leigh Stoller's avatar
Leigh Stoller committed
185
sub vpaths($)           { return $_[0]->{'VPATHS'}; }
186
sub counters($)         { return $_[0]->{'COUNTERS'}; }
187
sub counter($$)         { return $_[0]->{'COUNTERS'}->{$_[1]}; }
188
sub options($)          { return $_[0]->{'OPTIONS'}; }
189 190
sub option($$)          { return (exists($_[0]->{'OPTIONS'}->{$_[1]}) ?
				  $_[0]->{'OPTIONS'}->{$_[1]} : undef); }
191
sub exptstats($)        { return $_[0]->{'EXPTSTATS'}; }
Leigh Stoller's avatar
Leigh Stoller committed
192
sub vlinks($)           { return $_[0]->{'VLINKS'}; }
193 194 195
sub delaylinks($)       { return $_[0]->{'DELAYLINKS'}; }
sub delaynodecount()    { return scalar(keys(%{ $_[0]->delaynodes() })); }
sub portbw($)           { return $_[0]->{'PORTBW'}; }
196
sub results($)          { return $_[0]->{'RESULTS'}; }
197 198 199
sub current_v2p($)      { return $_[0]->{'CURRENT_V2P'}; }
sub current_p2v($)      { return $_[0]->{'CURRENT_P2V'}; }
sub current_v2v($)      { return $_[0]->{'CURRENT_V2V'}; }
Leigh Stoller's avatar
Leigh Stoller committed
200
sub current_v2pmap($)   { return $_[0]->{'CURRENT_V2PMAP'}; }
201 202
sub pnodes($)           { return $_[0]->{'PNODES'}; }
sub fixednodes($)       { return $_[0]->{'FIXEDNODES'}; }
203
sub newreserved($)      { return $_[0]->{'NEWRESERVED'}; }
204
sub rspec($)            { return $_[0]->{'RSPEC'}; }
205
sub genirspec($)        { return $_[0]->{'GENIRSPEC'}; }
206
sub forcedarchitecture($){ return $_[0]->{'FORCEARCH'}; }
207
sub newreservednodes($) { return keys(%{ $_[0]->{'NEWRESERVED'} }); }
208
sub oldreservednodes($) { return $_[0]->{'OLDRSRVCLEAN_NODES'}; }
209
sub norecover($)        { return $_[0]->{'norecover'}; }
210 211 212 213 214 215 216 217 218
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"); }
219 220
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
221
sub virt_paths($)       { return $_[0]->virt_table("virt_paths"); }
222
sub virt_bridges($)     { return $_[0]->virt_table("virt_bridges"); }
223
sub virt_desires($)	{ return $_[0]->virt_table("virt_node_desires"); }
Leigh Stoller's avatar
Leigh Stoller committed
224
sub virt_attributes($)	{ return $_[0]->virt_table("virt_node_attributes"); }
225
sub virt_startloc($)	{ return $_[0]->virt_table("virt_node_startloc"); }
226 227 228 229
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"); }
230 231 232
sub virt_blockstores($) { return $_[0]->virt_table("virt_blockstores"); }
sub virt_blockstore_attributes($) {
    return $_[0]->virt_table("virt_blockstore_attributes"); }
233 234 235 236

# 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]}); }
237
sub isasanhost($$)      { return exists($_[0]->sanhosts()->{$_[1]}); }
238 239 240

# Debug output.
sub verbose($)		{ return $_[0]->flags() & $VTOP_FLAGS_VERBOSE; }
241
sub quiet($)		{ return $_[0]->flags() & $VTOP_FLAGS_QUIET; }
242
sub updating($)		{ return $_[0]->flags() & $VTOP_FLAGS_UPDATE; }
243
sub fixcurrent($)	{ return $_[0]->flags() & $VTOP_FLAGS_FIXNODES; }
Leigh Stoller's avatar
Leigh Stoller committed
244
sub fixlanodes($)	{ return $_[0]->flags() & $VTOP_FLAGS_FIXLANNODES; }
245
sub impotent($)		{ return $_[0]->flags() & $VTOP_FLAGS_IMPOTENT; }
246
sub alloconly($)	{ return $_[0]->flags() & $VTOP_FLAGS_ALLOCONLY; }
247
sub regression($)	{ return $_[0]->flags() & $VTOP_FLAGS_REGRESSION; }
248
sub preassign($)	{ return $_[0]->flags() & $VTOP_FLAGS_PREASSIGN; }
249
sub printdb($$)		{ print $_[1] if ($_[0]->verbose()); return 1; }
250

251
# We name delay nodes internally as they are needed.
252
sub nextdelayname($)    { return "tbdelay" . $_[0]->{'DELAYID'}++; }
253
# For when the user wants a specific delay os. Use a desire.
254 255 256 257
# 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"); }
258 259
# For XML
sub nextifacenumber($)  { return $_[0]->{'IFACEID'}++; }
260
sub nextphostnumber($)  { return $_[0]->{'PHOSTID'}++; }
261

262 263 264 265
# Virtual Types. 
sub virttypeisvtype($$) { return $_[0]->virt_vtypes()->Find($_[1]); }
sub VirtTypes($)        { return $_[0]->virt_vtypes()->Rows(); }

266 267 268
# Caller will want these.
sub minimum_nodes($)    { return $_[0]->counter("minimum_nodes"); }
sub maximum_nodes($)    { return $_[0]->counter("maximum_nodes"); }
269
sub nodecount($)	{ return $_[0]->counter("nodecount"); }
270
sub plabcount($)	{ return $_[0]->counter("plabcount"); }
271
sub genicount($)	{ return $_[0]->counter("genicount"); }
272 273 274
sub virtnodecount($)	{ return $_[0]->counter("virtcount"); }
sub simnodecount($)	{ return $_[0]->counter("simcount"); }
sub remotenodecount($)	{ return $_[0]->counter("remotecount"); }
275
sub sharednodecount($)	{ return $_[0]->counter("sharedcount"); }
276
sub bstorecount($)	{ return $_[0]->counter("bstorecount"); }
277 278
sub gblinks($)		{ return $_[0]->counter("Gblinks"); }
sub mgblinks($)		{ return $_[0]->counter("MGblinks"); }
279

280
sub createLink($$$$$$$$$)
281
{
282 283 284
    # $others here will be a hashtable for the link flags.  The table
    # could also contain non-default values for latency and
    # packet_loss
285
    my ($self, $name, $plink, $cm, $src, $dst, $bw, $type, $others) = @_;
286
    my $ref = {
287 288 289
	'virtual_id'     => $name,
	'plink'		 => $plink,
	'manager_urn'    => $cm,
290
	'link_type'      => $type,
291 292 293 294 295 296 297 298 299
	'interface_ref'  => [$src, $dst],
	'capacity'       => $bw,
	'packet_loss'    => "0",
	'latency'        => "0"
    };
    
    if ($others) {
	while ( my ($key, $value) = each %{$others} ) {
	    $ref->{$key} = $value;
300
	}
301 302 303 304 305
    }
    if (!exists($self->rspec()->{'link'})) {
	$self->rspec()->{'link'} = [];
    }
    push(@{ $self->rspec()->{'link'} }, $ref);
306 307
}

308
sub createNode ($$$$$$$;$)
309
{
310 311 312
    # $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
313
    my ($self, $name, $cm, $type, $typecount, $desires, $others, $ifaces) = @_;
314
    my $ref = {
315 316
	'virtual_id'    => $name,
	'manager_urn'   => $cm,
317 318 319 320 321 322 323 324 325 326 327 328
    };
    
    my $typename = $type;
    my $typeslots = $typecount;
    my $isstatic = 0;
    if ($typename =~ /^\*([-\w]*)$/) {
	$typename = $1;
	$isstatic = 1;
    }
    if ($typecount eq '*') {
	$typeslots = 'unlimited';
    }
329

330 331
    # We will need this to generate interface names later
    $ref->{'interface_count'} = 0;
332
    $ref->{'interfaces'} = $ifaces;
333 334 335 336 337 338 339 340 341

    $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;
342
	}
343 344 345 346 347 348
    }
    $ref->{'desires'} = $desiretable;
    
    if ($others) {
	while ( my ($key, $value) = each %{$others} ) {
	    $ref->{$key} = $value;
349
	}
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
    }
    
    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,
376 377
		# XXX Going to need a URN in rspec output ...
		'pnode' => $pnode,
378 379 380 381 382 383 384
    };
    if (!exists($self->rspec()->{'fixed'})) {
	$self->rspec()->{'fixed'} = [];
    }
    push(@{ $self->rspec()->{'fixed'} }, $ref);
}

385 386 387 388 389 390 391
###############################################################################
# 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);
392
use overload ('""' => 'Stringify');
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

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

423 424 425 426 427
    $self->{'VIRTNODE'}   = $virt_node;
    $self->{'VTOP'}       = $vtop;
    $self->{'HASH'}       = {};
    # The virtlans this virtnode is a member of. 
    $self->{'MEMBERSHIP'} = {};
428
    
429 430 431 432 433 434
    return $self;
}
# accessors
sub virt_node($)	{ return $_[0]->{'VIRTNODE'}; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
435 436
sub membership($)       { return $_[0]->{'MEMBERSHIP'}; }
sub memberlist($)       { return values(%{ $_[0]->{'MEMBERSHIP'} }); }
437 438 439 440 441 442 443 444

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

    $self->{'VIRTNODE'}   = undef;
    $self->{'VTOP'}       = undef;
    $self->{'HASH'}       = undef;
445
    $self->{'MEMBERSHIP'} = undef;
446 447
}

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
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;
496 497
    $self->{'MEMBERHASH'}    = {};
    $self->{'MEMBERLIST'}    = [];
498 499 500 501 502 503 504
    $self->{'SHAPEDMEMBERS'} = {};
    $self->{'HASH'}          = {};

    return $self;
}
# accessors
sub virt_lanlan($)	{ return $_[0]->{'VIRTLANLAN'}; }
505 506
sub members($)		{ return $_[0]->{'MEMBERHASH'}; }
sub memberlist($)       { return @{ $_[0]->{'MEMBERLIST'} }; }
507
sub member($$)          { return @{ $_[0]->{'MEMBERLIST'} }[$_[1]]; }
508 509 510
sub shapedmembers($)	{ return $_[0]->{'SHAPEDMEMBERS'}; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
511
sub islink($)           { return (@{ $_[0]->{'MEMBERLIST'} } == 2 ? 1 : 0); }
512 513 514 515 516 517

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

    $self->{'VIRTLANLAN'} = undef;
518 519
    $self->{'MEMBERHASH'} = undef;
    $self->{'MEMBERLIST'} = undef;
520 521 522 523 524 525 526 527 528 529 530 531
    $self->{'VTOP'}       = undef;
    $self->{'HASH'}       = undef;
}

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

    return "[vlan:$vname]";
}

532 533 534 535 536
sub addmember($$)
{
    my ($self, $vlanmember) = @_;
    
    $self->members()->{$vlanmember->member()} = $vlanmember;
537 538

    @{ $self->{'MEMBERLIST'} }[$vlanmember->vindex()] = $vlanmember;
539 540 541 542

    return 0;
}

543 544 545 546 547 548 549 550
#
# Other support functions.
#
sub usevirtiface($)
{
    my ($self) = @_;
    my $encap  = $self->_encapstyle();

551
    return ($encap eq "veth-ne" || $encap eq "vlan");
552 553 554 555 556 557 558 559
}
sub membershaped($$) {
    my ($self, $member) = @_;
    return $self->shapedmembers()->{"$member"};
}
sub setmembershaped($$) {
    my ($self, $member) = @_;
    $self->shapedmembers()->{"$member"} = 1;
560
    $member->_shaped(1);
561 562 563 564 565 566 567 568 569
}

###############################################################################
# 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);
570
use overload ('""' => 'Stringify', 'eq' => 'StringEQ');
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 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635

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

636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
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
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 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
###############################################################################
# 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'}   = [];
695
    $self->{'IMPLEMENTS'}    = {};
Leigh Stoller's avatar
Leigh Stoller committed
696 697 698 699 700 701 702 703 704
    $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'} }; }
705
sub virtlanlist($)      { return @{ $_[0]->{'VIRTLANLIST'} }; }
Leigh Stoller's avatar
Leigh Stoller committed
706 707 708 709
sub member($$)		{ return $_[0]->{'MEMBERLIST'}->[$_[1]]; }
sub lanlink($$)		{ return $_[0]->{'VIRTLANLIST'}->[$_[1]]; }
sub vtop($)		{ return $_[0]->{'VTOP'}; }
sub hash($)		{ return $_[0]->{'HASH'}; }
710
sub implements($)	{ return $_[0]->{'IMPLEMENTS'}; }
Leigh Stoller's avatar
Leigh Stoller committed
711 712 713 714 715 716 717 718 719 720 721 722

# 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;
723
    $self->{'IMPLEMENTS'} = undef;
Leigh Stoller's avatar
Leigh Stoller committed
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 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
    $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;
}
775

776 777 778 779 780 781 782 783 784 785 786 787 788
sub addimplements($$)
{
    my ($self, $virtlan) = @_;

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

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

789 790 791
#############################################################################
# Back to the main package.
#
792
package libvtop_stable;
793

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
#
# 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 ".
830 831 832 833
		    "where n.type is not null and ".
		    "      (i.role='" . TBDB_IFACEROLE_EXPERIMENT . "' or ".
		    "       i.role='" . TBDB_IFACEROLE_CONTROL . "')");

834 835 836 837 838 839 840 841 842
    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)) {
843
	    warn("No type info for node type $node_type\n");
844 845 846 847 848 849 850 851 852 853
	    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"})) {
854
	    warn("No protocols listed in capabilities for $iface_type!\n");
855 856 857 858 859 860 861 862 863 864
	    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)) {
865
		warn("No default speed in capabilites for $iface_type!\n");
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891
		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;
		}
	    }
	}
    }
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

    #
    # 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.
    #
914 915 916
    foreach my $ref ($self->VirtTypes()) {
	my $vtype   = $ref->name();
	my @members = split(" ", $ref->members());
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944

	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");
	    }
	}
    }
    
945 946 947 948 949
    $self->{'IFACECAPS'}  = \%interface_capabilities;
    $self->{'TYPELINKBW'} = \%node_type_linkbw;
    
    return 0;
}
950 951 952
sub interfacespeedmbps($$$)
{
    my ($self, $type, $which) = @_;
953 954 955 956 957 958 959 960 961

    if (!exists($self->{'IFACECAPS'}->{$type})) {
	print STDERR "interfacespeedmbps: $type does not exist\n";
	return 0;
    }
    if (!exists($self->{'IFACECAPS'}->{$type}->{"${which}_defspeed"})) {
	print STDERR "interfacespeedmbps: $type $which does not exist\n";
	return 0;
    }
962 963
    return $self->{'IFACECAPS'}->{$type}->{"${which}_defspeed"}/1000.0;
}
964

965 966 967 968
#
# When updating with fixednodes turned on, we need the current set
# of nodes that need to be fixed.
#
969
sub LoadCurrentResources($)
970 971
{
    my ($self) = @_;
Leigh Stoller's avatar
Leigh Stoller committed
972
    my $exptidx = $self->exptidx();
973 974 975 976 977

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

978 979
    $self->printdb("Loading current resources" .
		   ($self->regression() ? " in regression mode" : "") . "\n");
980

981
    my @nodelist = $self->experiment()->NodeList(0, 1);
982 983 984
    return 0
	if (!@nodelist);

985 986 987 988 989 990 991 992 993
    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()) {
994
		$node_id = $pnode->phys_nodeid();
995
	    }
996 997
	    $self->fixednodes()->{$vname} = $node_id;
	    $self->printdb("  fixing $vname -> $node_id\n");
998 999 1000 1001
	}
	return 0;
    }

1002
    foreach my $pnode (@nodelist) {
1003 1004 1005 1006 1007 1008 1009 1010 1011
	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;

1012 1013 1014 1015
	#
	# WIDEAREA nodes are going to break.
	#
	if ($pnode->isremotenode() &&
1016
	    !($pnode->isplabdslice() || $pnode->isfednode() ||
Leigh Stoller's avatar
Leigh Stoller committed
1017
	      $pnode->isvirtnode() || $pnode->isdedicatedremote())) {
1018 1019 1020 1021 1022
	    tberror("Cannot update widearea nodes yet!\n");
	    return -1;
	}
	if ($pnode->isvirtnode()) {
	    $self->counters()->{'reserved_virtcount'}++;
1023 1024 1025 1026 1027 1028 1029

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

1032 1033 1034
	    $self->fixednodes()->{$vname} = $ppnode_id
		if ($self->fixcurrent());
	    
1035 1036 1037
	    #
	    # Record the mappings. 
	    #
1038 1039
	    $self->current_v2v()->{$vname} = $pnode->node_id();
	    $self->current_v2p()->{$vname} = $ppnode->node_id();
1040 1041 1042 1043 1044
	    push(@{ $self->current_p2v()->{$ppnode->node_id()} }, $vname);
	    
	    # Mark the node as unused until later.
	    $pnode->_reuse("unused");
	    $ppnode->_reuse("unused");
1045 1046 1047 1048 1049 1050 1051 1052 1053

	    #
	    # 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;
1054 1055
	    
	    $self->printdb("current v2p: $node_id ($ppnode_id) -> $vname\n");
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	}
	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 {
1066 1067 1068
		$self->fixednodes()->{$vname} = $pnode->node_id()
		    if ($self->fixcurrent());
		
1069
		$self->counters()->{'reserved_physcount'}++;
1070 1071 1072 1073

		#
		# Record the mapping. 
		#
1074
		$self->current_v2p()->{$vname} = $pnode->node_id();
1075 1076 1077 1078
		push(@{ $self->current_p2v()->{$node_id} }, $vname);
		# Mark the node as unused until later.
		$pnode->_reuse("unused");

1079 1080 1081 1082 1083 1084 1085
		#
		# 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;

1086
		$self->printdb("current v2p: $node_id -> $vname\n");
1087 1088 1089
	    }
	}
    }
Leigh Stoller's avatar
Leigh Stoller committed
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    #
    # 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;
    }
1104 1105 1106
    return 0;
}

1107 1108 1109
sub LoadVirtNodes($)
{
    my ($self) = @_;
1110
    my $gotgeninodes = 0;
1111 1112 1113

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

1114 1115 1116 1117
    my $pid    = $self->pid();
    my $eid    = $self->eid();
    my $table  = $self->virt_nodes();

1118
    foreach my $virt_node ($table->Rows()) {
1119 1120 1121
	my $vnode    = libvtop::virt_node->Create($self, $virt_node);
	my $vname    = $vnode->vname();
	my $desires  = {};
Leigh Stoller's avatar
Leigh Stoller committed
1122
	my $attrs    = {};
1123
	my $startloc = undef;
1124
	my $nodetype = undef;
1125 1126

	# Other fields we need.
1127
	my $ips     = $vnode->ips() || "";
1128 1129 1130
	my $type    = $vnode->type();
	my $fixed   = $vnode->fixed();
	my $osname  = $vnode->osname();
1131
	my $parent_osname  = $vnode->parent_osname();
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144

	# 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.
1145
	my $isvtyped= 0;
1146
	my $isded   = 0;
1147
	my $isgeni  = 0;
1148 1149

	# If we have a real type or auxtype ...
1150
	$nodetype = NodeType->LookupAny($type);
1151 1152 1153
	if (!defined($nodetype)) {
	    my $vtype = $self->virttypeisvtype($type);
	    if (!defined($vtype)) {
1154
		warn("Improper type $type for node $vnode!\n");
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
		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)) {
1165
		warn("Improper type $vtypename in vtypes for node $vnode!\n");
1166
		    return -1;
1167
	    }
1168
	    $isvtyped = 1;
1169
	}
1170 1171
	$vnode->_typeinfo($nodetype);
	
1172 1173 1174 1175
	$isremote = $nodetype->isremotenode();
	$isvirt   = $nodetype->isvirtnode();
	$issub    = $nodetype->issubnode();
	$isplab   = $nodetype->isplabdslice();
1176
	$isgeni   = $nodetype->isfednode();
1177 1178
	$issim    = $nodetype->issimnode();
	$isdyn    = $nodetype->isdynamic();
1179
	$isded    = $nodetype->isdedicatedremote();
1180

1181 1182 1183
	# Mark this as being a virtual typed node.
	$vnode->_isvtyped($isvtyped);

1184 1185 1186
	# dynamic inclusion below.
	$gotgeninodes++ if ($isgeni);

1187 1188 1189 1190 1191 1192
	# All this info is stashed in our local object.
	$vnode->_nodeweight(undef);
	$vnode->_isremotenode($isremote);
	$vnode->_isvirtnode($isvirt);
	$vnode->_issubnode($issub);
	$vnode->_isplabnode($isplab);
1193
	$vnode->_isgeninode($isgeni);
1194 1195
	$vnode->_issimnode($issim);
	$vnode->_isdynamic($isdyn);
1196
	$vnode->_isdedremote($isded);
1197 1198
	# Set below from a desire.
	$vnode->_sharedokay(0);
1199
	$vnode->_fixedvm(undef);
1200
	$vnode->_isbridge(($vnode->role() eq "bridge" ? 1 : 0));
1201 1202 1203
	$vnode->_blockstore(undef);
	$vnode->_blockstore_attributes(undef);
	$vnode->_sanhostname(undef);
1204
	$vnode->_forcedarchitecture(undef);
1205 1206 1207 1208 1209 1210 1211

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

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

1216 1217 1218
	# dynamic inclusion below.
	$gotgeninodes++ if ($isgeni);

1219 1220
	# The mapped osname to actual osinfo structure.
	$vnode->_osinfo(undef);
1221 1222
	# If the virtnode tries to specify its parent os in addition
	# to its own, store that osinfo here.
1223
	$vnode->_parent_osinfo(undef);
1224 1225 1226 1227 1228 1229 1230
	# Eventual physical mapping. 
	$vnode->_physnode(undef);
	# Handy to combine these.
	$vnode->_settings([ $vnode->cmd_line(),
			    $vnode->rpms(),
			    $vnode->startupcmd(),
			    $vnode->tarfiles(),
1231
			    $vnode->loadlist(),
1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
			    $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 "") {
1244
		warn("Subnode $vname must be fixed to its parent!\n");
1245 1246 1247
		return -1;
	    }
	    $vnode->_parent($fixed);
1248
	    $vnode->fixed(undef);
1249 1250 1251 1252 1253
	    undef($fixed);
	}

	# Can fixed really get set to ""?
	if (defined($fixed) && $fixed ne "") {
1254
	    # Store the name since we use FIXED_NODES for delay nodes too.
1255
	    $self->fixednodes()->{$vname} = $fixed;
1256 1257 1258 1259 1260

	    # 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)
1261
		if ($isvirt && !$isgeni);
1262
	}
Leigh Stoller's avatar
Leigh Stoller committed
1263 1264
	else {
	    $vnode->fixed(undef);
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
	    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
1291
	}
1292

1293 1294 1295
	#
	# Record the type selections for the mapper.
	#
1296
	if (!$vnode->_typeinfo()->IsClass()) {
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
	    if (!defined($self->types()->{$type})) {
		$self->types()->{$type} = 0;
	    }
	    $self->types()->{$type}++;
	}
	elsif (defined($self->forcedarchitecture())) {
	    my $arch = $self->forcedarchitecture();
	    if ($isvirt) {
		$arch .= "-vm";
	    }
	    $vnode->_forcedarchitecture($arch);
	}

1310
	$self->printdb("  $vname type:$type ips:$ips\n");
1311
	$self->printdb("         isrem:$isremote isvirt:$isvirt");
1312 1313
	$self->printdb(" architecture:" . $vnode->_forcedarchitecture())
	    if (defined($vnode->_forcedarchitecture()));
1314 1315
	$self->printdb(" fixed:$fixed")
	    if (defined($fixed) && $fixed ne "");
1316
	$self->printdb("\n");
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327
	
	# 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.
1328
	my @iplist  = split(" ", $ips);
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	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 "") {
1342
	    my $osimage;
1343
	    
1344
	    if ($osname =~ /^(ftp|http|https):/) {
1345
		$osimage = OSImage->LookupByURL($osname);
1346
	    }
1347
	    elsif ($osname =~ /^(.*)\/(.*)$/) {
1348
		$osimage = OSImage->Lookup($osname);
1349 1350
	    }
	    else {
1351 1352 1353
		$osimage = OSImage->Lookup($pid, $osname);
		if (!defined($osimage)) {
		    $osimage = OSImage->LookupByName($osname);
1354 1355
		}
	    }
1356
	    if (!defined($osimage)) {
1357 1358 1359 1360 1361 1362
		tberror({cause => 'user', type => 'primary',
			 severity => SEV_ERROR,
			 error => ['invalid_os', undef, $osname, $pid]},
			"Invalid OS $osname!");
		return -1;
	    }
1363
	    $vnode->_osinfo($osimage);
1364
	}
1365
	elsif ($isvirt) {
1366 1367
	    my $osimage;
	    
1368 1369
	    if ($nodetype->type() eq "pcvm") {
		# Silly default.
1370
		$osimage = OSImage->LookupByName("OPENVZ-STD");
1371 1372 1373
	    }
	    else {
		# Grab it from the typeinfo.
1374
		$osimage = OSImage->Lookup($nodetype->default_osid());
1375
	    }
1376
	    $vnode->_osinfo($osimage);
1377
	}
1378

1379 1380 1381 1382
	#
	# Map the parent_osname to an OSID now.
	#
	if (defined($parent_osname) && $parent_osname ne "") {
1383
	    my $osimage;
1384 1385
	    
	    if ($parent_osname =~ /^(.*)\/(.*)$/) {
1386
		$osimage = OSImage->Lookup($parent_osname);
1387
	    }
Leigh Stoller's avatar
Leigh Stoller committed
1388
	    else {
1389 1390 1391
		$osimage = OSImage->LookupByName($pid, $parent_osname);
		if (!defined($osimage)) {
		    $osimage = OSImage->LookupByName($parent_osname);
Leigh Stoller's avatar
Leigh Stoller committed
1392
		}
1393
		if (!defined($osimage)) {
1394 1395
		    tberror({cause => 'user', type => 'primary',
			     severity => SEV_ERROR,
1396
			     error => ['invalid_os', undef, $parent_osname, $pid]},
Leigh Stoller's avatar
Leigh Stoller committed
1397
			    "Invalid parent OS $parent_osname in project $pid!");
1398 1399 1400
		    return -1;
		}
	    }
1401
	    $vnode->_parent_osinfo($osimage);
1402
	}
1403 1404 1405 1406
	elsif ($isvirt && $vnode->_osinfo()->DefaultParent()) {
	    my $osimage = $vnode->_osinfo()->DefaultParent();
	    if (!defined($osimage)) {
		tberror("Invalid default parent os for " .
1407 1408 1409
			$vnode->_osinfo() . "\n");
		return -1;
	    }
1410
	    $vnode->_parent_osinfo($osimage);
1411
	}
1412

1413 1414 1415 1416 1417 1418
	#
	# Add in desires.
	#
	foreach my $desire ($self->virt_desires()->Rows()) {
	    next
		if ($desire->vname() ne $vname);
1419
	    
1420
	    $desires->{$desire->desire()} = $desire->weight();
1421 1422 1423 1424 1425

	    # 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");
1426 1427 1428
	}
	$vnode->_desires($desires);

Leigh Stoller's avatar
Leigh Stoller committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
	#
	# Add in attributes.
	#
	foreach my $attr ($self->virt_attributes()->Rows()) {
	    next
		if ($attr->vname() ne $vname);
	    
	    $attrs->{$attr->attrkey()} = $attr->attrvalue();

	    if ($attr->attrkey() eq "XEN_MEMSIZE" ||
		$attr->attrkey() eq "MEMORYSIZE") {
		$vnode->_desires()->{"?+ram"} = $attr->attrvalue();
	    }
Leigh Stoller's avatar
Leigh Stoller committed
1442 1443 1444 1445 1446 1447
	    elsif ($attr->attrkey() eq "XEN_CORES") {
		# Overridden below for shared nodes.
		$self->experiment()->SetVirtNodeAttribute($vname,
							  "VM_VCPUS",
							  $attr->attrvalue());
	    }
Leigh Stoller's avatar
Leigh Stoller committed
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
	}
	$vnode->_atttributes($attrs);

	#
	# Need to set a default. But this needs to be seen on
	# the client too, via virt_node_attributes.
	#
	# We need a way to associate these defaults with the virtualization
	# type, but that is a function of the image. Needs more thought.
	#
Leigh Stoller's avatar
Leigh Stoller committed
1458 1459 1460 1461
	if ($isvirt &&
	    !($vnode->type() eq "blockstore" ||
	      $vnode->type() eq "interconnect" ||
	      $vnode->type() eq "interconnect-vm")) {
Leigh Stoller's avatar
Leigh Stoller committed
1462
	    my $defmem = 128;
Leigh Stoller's avatar
Leigh Stoller committed
1463
	    my $minmem = 128;
Leigh Stoller's avatar
Leigh Stoller committed
1464 1465 1466
	    my $maxmem = 128;

	    if (defined($vnode->_parent_osinfo())
1467
		&& $vnode->_parent_osinfo()->FeatureSupported("xen-host")) {
Leigh Stoller's avatar
Leigh Stoller committed
1468 1469 1470
		$defmem = 1024;
		$minmem = 256;
		$maxmem = 1024 * 16;
Leigh Stoller's avatar
Leigh Stoller committed
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	    }	    
	    if (!exists($vnode->_desires()->{"?+ram"})) {
		$self->printdb("Setting VM memsize to $defmem for $vname\n");
		$vnode->_desires()->{"?+ram"} = $defmem;
	    }
	    else {
		#
		# If the user wants a shared node, they are not allowed to
		# ask for more then the max, unless its an admin. 
		#
		if ($vnode->_desires()->{"?+ram"} > $maxmem &&
		    $vnode->_sharedokay() &&
		    !$self->user()->IsAdmin()) {
Leigh Stoller's avatar
Leigh Stoller committed
1484 1485 1486 1487 1488 1489 1490 1491
		    tberror("You asked for too much memory (> $maxmem) ".
			    "on $vnode\n");
		    return -1;
		}
		# Ditto min memory.
		if ($vnode->_desires()->{"?+ram"} < $minmem) {
		    tberror("You asked for too little memory (< $minmem) ".
			    "on $vnode\n");
Leigh Stoller's avatar
Leigh Stoller committed
1492 1493 1494 1495 1496
		    return -1;
		}
	    }
	    $self->experiment()->SetVirtNodeAttribute($vname, "VM_MEMSIZE",
					      $vnode->_desires()->{"?+ram"});
Leigh Stoller's avatar
Leigh Stoller committed
1497 1498 1499 1500

	    # We will deal with later, but set a default=1 for now.
	    $self->experiment()->SetVirtNodeAttribute($vname, "VM_VCPUS", 1)
		if (0 && $vnode->_sharedokay() && !$self->user()->IsAdmin());
Leigh Stoller's avatar
Leigh Stoller committed
1501 1502
	}

1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
	#
	# 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);

1514
	# Counters
1515
	$self->{'COUNTERS'}->{'nodecount'}++;
1516 1517 1518 1519 1520 1521 1522 1523
	$self->{'COUNTERS'}->{'simcount'}++
	    if ($issim);
	$self->{'COUNTERS'}->{'remotecount'}++
	    if ($isremote);
	$self->{'COUNTERS'}->{'virtcount'}++
	    if ($isvirt);
	$self->{'COUNTERS'}->{'plabcount'}++
	    if ($isplab);
1524 1525
	$self->{'COUNTERS'}->{'genicount'}++
	    if ($isgeni);
1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537
	$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"});

1538
	# Add to the list.
1539
	$self->vnodes()->{$vname} = $vnode;
1540
    }
1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
    if ($gotgeninodes) {
	if ($PGENISUPPORT) {
	    require libGeni;
	    require GeniXML;
	    import GeniXML;
	}
	else {
	    tberror("Using geni nodes but PROTOGENI_SUPPORT is not defined\n");
	    return -1;
	}
    }
1552 1553 1554 1555 1556 1557 1558 1559 1560

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

1561 1562 1563
	next
	    if ($vnode->_isgeninode());

1564 1565
	next
	    if (! ($vnode->_isvirtnode() && $vnode->_fixedvm()));
1566

1567
	my $fixnode = $self->vnodes()->{$vnode->_fixedvm()};
1568

1569 1570 1571 1572 1573
	#
	# 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.
	#
1574 1575 1576 1577 1578
	# It is silly to fix a node to yourself, so what this probably
	# means is that the user really wants to fix to a physical node
	# of the same name. 
	#
	if (!defined($fixnode) || $vnode->_fixedvm() eq $vname) {
1579 1580 1581 1582
	    # Lets make sure its a real node.
	    my $fixvname = $vnode->_fixedvm();
	    
	    if (!defined(Node->Lookup($fixvname))) {
1583
		tberror("Cannot fix $vnode to $fixvname; No such phys node\n");
1584 1585 1586 1587 1588
		return -1;
	    }
	    $vnode->_fixedvm(undef);
	    next;
	}
1589
	my $osimage;
1590 1591

	if (defined($vnode->_parent_osinfo())) {
1592 1593 1594 1595
	    $osimage = $vnode->_parent_osinfo();
	}
	elsif ($self->option("jail_osid")) {
	    $osimage = OSImage->Lookup($self->option("jail_osid"));
1596 1597
	}
	else {
1598
	    $osimage = $self->nodejailosinfo($vnode);
1599 1600
	}
	return -1
1601
	    if (!defined($osimage));
1602
	
1603
	$fixnode->_osinfo($osimage);
1604 1605 1606
	# Convert from the name to the local object.
	$vnode->_fixedvm($fixnode);

1607
	$self->printdb("Fixing VM $vname to $fixnode, $osimage\n");
1608
    }
1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623

    #
    # Load up the blockstore stuff since we have to spit out the
    # attributes as desires.
    #
    foreach my $virt_bs ($self->virt_blockstores()->Rows()) {
	#
	# The blockstore is fixed to a node in the topo.
	#
	my $fixnode = $self->vnodes()->{$virt_bs->fixed()};
	if (!defined($fixnode)) {
	    tberror("Blockstore $virt_bs is fixed to non-existent node\n");
	    return -1;
	}

Leigh Stoller's avatar
Leigh Stoller committed
1624 1625 1626
	# XXX: If the virt blockstore object isn't fixed to a
	# blockstore pseudo-VM, then abort the bookkeeping and mapping
	# for it.  This means we are dealing with local node storage
Leigh Stoller's avatar
Leigh Stoller committed
1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
	# via a features and desires hack.  Check that the OS on the
	# host has the local blockstore feature.
	if ($fixnode->type() ne "blockstore") {
	    my $fosinfo = $fixnode->_osinfo();
	    if (!defined($fosinfo)) {
		tberror("OS unknown on node $fixnode: ".
			"Can't check for local blockstore support!");
		return -1;
	    } elsif (!$fosinfo->FeatureSupported("loc-bstore")) {
		tberror("OS $fosinfo on $fixnode hosting $virt_bs doesn't ".
			"support local blockstores.\n");
		return -1;
	    }
	    next;
	}
Leigh Stoller's avatar
Leigh Stoller committed
1642

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
	#
	# We need a place to hang the attributes, but they are split
	# between the blockstore and the blockstore_attributes. Another
	# wrinkle is that the blockstore is assigned to a node in the
	# topo, and it is that node that we have to hang the desires off.
	#
	$self->blockstores()->{$virt_bs->vname()} = $fixnode;
	$fixnode->_blockstore($virt_bs);
	$fixnode->_sanhostname("sanhost-" . $virt_bs->vname());
	$fixnode->_blockstore_attributes({});
	# XXX Are these always in shared mode?
	$fixnode->_sharedokay(1);
	
	$self->{'COUNTERS'}->{'bstorecount'}++;
    }
Leigh Stoller's avatar
Leigh Stoller committed
1658 1659

    #
1660 1661
    # Make a quick pass of the virt_blockstores looking for RO and RW clone
    # attributes.
Leigh Stoller's avatar
Leigh Stoller committed
1662 1663 1664 1665
    # We do this before we make the main pass below so that we have this
    # info available when we lookup a lease.
    #
    my %roleases = ();
1666
    my %cloneleases = ();
1667
    my %prereserves = ();
Leigh Stoller's avatar
Leigh Stoller committed
1668 1669 1670 1671
    foreach my $virt_bs_attr ($self->virt_blockstore_attributes()->Rows()) {
	if ($virt_bs_attr->attrkey() eq "readonly") {
	    $roleases{$virt_bs_attr->vname()} = $virt_bs_attr->attrvalue();
	}
1672 1673 1674
	elsif ($virt_bs_attr->attrkey() eq "rwclone") {
	    $cloneleases{$virt_bs_attr->vname()} = $virt_bs_attr->attrvalue();
	}
1675 1676 1677
	elsif ($virt_bs_attr->attrkey() eq "prereserve") {
	    $prereserves{$virt_bs_attr->vname()} = $virt_bs_attr->attrvalue();
	}
Leigh Stoller's avatar
Leigh Stoller committed
1678 1679
    }

1680 1681 1682 1683
    foreach my $virt_bs_attr ($self->virt_blockstore_attributes()->Rows()) {
	my $vname   = $virt_bs_attr->vname();
	my $attrkey = $virt_bs_attr->attrkey();
	my $attrval = $virt_bs_attr->attrvalue();
Leigh Stoller's avatar
Leigh Stoller committed
1684
	my $isdesire = $virt_bs_attr->isdesire();
1685

1686
	#
Leigh Stoller's avatar
Leigh Stoller committed
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714
	# If this is a persistent blockstore (identified by a lease
	# index attribute) then make sure the user has appropriate
	# access to the lease and that the associated blockstore is
	# not already mapped in a conflicting way.
	#
	# We also check and make sure that leases in the grace state are
	# only mounted RO. We make this check in the parser as well, but
	# the state of the lease can change between parses.
	#
	# Note: we do not make these checks during a pre-assign pass.
	#
	# Exclusive use semantics:
	#
	# If the sitevar "simultaneous_ro_datasets" is zero (or unset)
	# or the dataset has the "exclusive_use" attribute set, only one
	# mapping at a time can be in effect for a dataset whether it
	# is RO or RW. This condition is embodied in the IsExclusiveUse()
	# Lease method.
	#
	# Shared use semantics:
	#
	# To be shared, the global "simultaneous_ro_datasets" sitevar
	# must be non-zero and a dataset's "exclusive_use" attribute must
	# either not exist or be set to zero (!IsExclusiveUse). Additionally,
	# the dataset must have a snapshot. Currently a snapshot is created
	# upon termination of any RW mapping (see Blockstore->Release).
	# Snapshot creation may be explicit in the future.
	#
1715 1716
	# A RW clone or a RO mapping of a dataset always gets the most recent
	# snapshot of the dataset. This is true whether the dataset is currently
Leigh Stoller's avatar
Leigh Stoller committed
1717 1718 1719 1720 1721
	# in use or not. If the dataset does not have a snapshot, it is
	# an error.
	#
	# A RW mapping of a dataset always gets the dataset itself.
	# If a RW mapping already exists, it is an error.
1722 1723
	#
	if ($attrkey eq "lease" && !$self->preassign()) {
1724
	    my $lease = Lease->Lookup($attrval);
Leigh Stoller's avatar
Leigh Stoller committed
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741
	    # Valid lease?
	    if (!$lease) {
		warn("Lease $attrval for blockstore $vname does not exist!\n");
		return -1;
	    }

	    my $exclusive = $lease->IsExclusiveUse();
	    my $snapshot = $lease->HasResourceSnapshot();

	    # If sitevar or dataset disables simultaneous use, catch it now.
	    if ($exclusive && $lease->InUse()) {
		tberror("Exclusive-use dataset $lease is currently in use ".
			"and cannot be mapped at this time.\n");
		return -1;
	    }

	    # Do sanity/permission checks based on requested mode (RW/RO)
1742 1743 1744
	    if (exists($roleases{$vname}) && $roleases{$vname} == 1 ||
		exists($cloneleases{$vname}) && $cloneleases{$vname} == 1) {
		# Does user have read rights to this lease?
Leigh Stoller's avatar
Leigh Stoller committed
1745 1746
		if (!$lease->AccessCheck($self->realuser(),
					 LEASE_ACCESS_READ())) {
1747 1748
		    tberror("Not allowed to use dataset $lease in ".
			    "RO or RW-clone mode\n");
Leigh Stoller's avatar
Leigh Stoller committed
1749 1750 1751 1752 1753 1754
		    return -1;
		}

		# For shared use, a snapshot must exist
		if (!$exclusive) {
		    if (!$snapshot) {
1755 1756
			tberror("Dataset $lease has no snapshot and cannot ".
				"be mapped RO or cloned at this time.\n");
Leigh Stoller's avatar
Leigh Stoller committed
1757 1758 1759
			return -1;
		    }
		}
1760