sharevlan.in 9.21 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2003-2013 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24 25 26
#
use strict;
use English;
use Getopt::Std;
27
use Data::Dumper;
28 29 30 31 32 33 34

#
# Grant and revoke shared vlan access.
#
sub usage()
{
    print STDERR "Usage: sharevlan [-o] eid lanname token\n";
35
    print STDERR "       sharevlan -r | -i token\n";
36
    print STDERR "       sharevlan -r eid lanname\n";
37
    print STDERR " -r    Revoke sharing instead\n";
38
    print STDERR " -i    Show members.\n";
39
    print STDERR " -o    Sharing is open to everyone.\n";
40
    print STDERR " -l    List all shared vlans\n";
41 42
    exit(-1);
}
43
my $optlist  = "hrRolif";
44 45
my $revoke   = 0;
my $open     = 0;
46
my $list     = 0;
47 48
my $info     = 0;
my $force    = 0;
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

# Protos
sub fatal($);

#
# Please do not run as root. Hard to track what has happened.
#
if ($UID == 0) {
    die("*** $0:\n".
	"    Please do not run this as root!\n");
}

#
# Configure variables
#
my $TB		 = "@prefix@";

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use emdb;
use libtestbed;
use Experiment;
use EmulabConstants;
use Lan;
use User;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:";

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{'h'})) {
    usage();
}
if (defined($options{'o'})) {
    $open = 1;
}
101 102 103 104 105 106 107 108 109 110
if (defined($options{'f'})) {
    $force = 1;
}
if (defined($options{'i'})) {
    $info = 1;

    usage()
	if (@ARGV != 1);
}
elsif (defined($options{'l'})) {
111 112 113
    $list = 1;
}
elsif (defined($options{'r'})) {
114 115
    $revoke = 1;

116 117 118
    if (defined($options{'R'})) {
	$force = 1;
    }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
}
else {
    usage()
	if (@ARGV != 3);
}

#
# Verify user, must be admin.
#
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}
if (!$this_user->IsAdmin()) {
    fatal("You are not a testbed administrator!");
}

136 137
if ($list) {
    my $query_result =
138 139 140
	DBQueryFatal("select * from shared_vlans as s ".
		     "left join vlans as v on v.id=s.lanid ".
		     "order by s.token");
141 142 143
    exit(0)
	if (! $query_result->numrows);

144 145 146 147
    printf("%-15s %-20s %-20s %-20s %-5s %s\n",
	   "PID", "EID", "Token", "Lan", "Tag", "Who");
    print "-------------------------------------------------------------";
    print "------------------------------\n";
148 149 150 151 152 153 154 155
	
    while (my $row = $query_result->fetchrow_hashref()) {
	my $pid      = $row->{'pid'};
	my $eid      = $row->{'eid'};
	my $token    = $row->{'token'};
	my $lan      = $row->{'vname'};
	my $created  = $row->{'created'};
	my $creator  = $row->{'creator'};
156
	my $tag      = $row->{'tag'} || "";
157

158 159
	printf("%-15s %-20s %-20s %-20s %-5s %s\n",
	       $pid, $eid, $token, $lan, "$tag", $creator);
160 161 162 163
    }
    exit(0);
}

164 165 166 167
if ($info) {
    my $token = $ARGV[0];
    my $query_result =
	DBQueryFatal("select lanid from shared_vlans where token='$token'");
168
    if (!$query_result->numrows) {
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
	fatal("No such shared vlan");
    }
    my ($lanid) = $query_result->fetchrow_array();
    my $vlan = VLan->Lookup($lanid);
    if (!defined($vlan)) {
	fatal("No such vlan $lanid");
    }
    my @members;
    if ($vlan->MemberList(\@members) != 0) {
	fatal("Could not get member list for $vlan");
    }
    foreach my $member (@members) {
	my $node;
	my $iface;
	$member->GetNodeIface(\$node, \$iface);
	print $node->node_id() . ":$iface";

	my $portlan_exptidx;
	my $portlan_lanname;
	my $experiment;
	$member->GetAttribute("portlan_exptidx", \$portlan_exptidx);
	$member->GetAttribute("portlan_lanname", \$portlan_lanname);
	
	if (defined($portlan_exptidx) && defined($portlan_lanname)) {
	    $experiment = Experiment->Lookup($portlan_exptidx);
	    if (defined($experiment)) {
		print " " . $experiment->pid() . ":" . $experiment->eid();
	    }
197 198 199
	    else {
		print " oops";
	    }
200 201 202 203 204 205 206 207 208 209 210 211
	}
	my $reservation = $node->Reservation();
	if (defined($reservation) &&
	    (!defined($experiment) ||
	     !$reservation->SameExperiment($experiment))) {
	    print " (" . $reservation->pid() . ":" . $reservation->eid() . ")";
	}
	print "\n";
    }
    exit(0);
}

212
if ($revoke) {
213
    my ($vlan,$lanid);
214 215
    my $busy  = 0;

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    if (@ARGV == 1) {
	my $token = $ARGV[0];

	my $query_result =
	    DBQueryFatal("select lanid from shared_vlans where token='$token'");
	if (!$query_result->numrows) {
	    fatal("No such shared vlan");
	}
	($lanid) = $query_result->fetchrow_array();
	$vlan = VLan->Lookup($lanid);
	if (!defined($vlan)) {
	    fatal("No such vlan $lanid");
	}
    }
    elsif (@ARGV == 2) {
	my $eid     = $ARGV[0];
	my $lanname = $ARGV[1];

	my $experiment = Experiment->Lookup($eid);
	if (!defined($experiment)) {
	    fatal("No such experiment $eid");
	}
	$vlan = VLan->Lookup($experiment, $lanname);
	if (!defined($vlan)) {
	    fatal("No such lan in $experiment");
	}
	$lanid = $vlan->lanid();
    }
    else {
	usage();
    }
    if (!$vlan->IsShared()) {
	fatal("Lan is not shared");
    }

251
    #
252 253 254 255
    # The lan is obviously shared, so we have to lock it.
    # It should not spend much time locked though, so the
    # timeout should not be too long; indicates an error if
    # it is.
256
    #
257 258
    if ($vlan->Lock(180) != 0) {
	fatal("Could not lock vlan $lanid");
259
    }
260 261 262 263 264
    
    #
    # Do not allow if the lan is actually being used by other experiments.
    #

265 266
    my @members;
    if ($vlan->MemberList(\@members) != 0) {
267
	$vlan->Unlock();
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	fatal("Could not get member list for $vlan");
    }
    foreach my $member (@members) {
	my $node;
	my $iface;
	$member->GetNodeIface(\$node, \$iface);

	my $portlan_exptidx;
	my $portlan_lanname;
	my $experiment;
	$member->GetAttribute("portlan_exptidx", \$portlan_exptidx);
	$member->GetAttribute("portlan_lanname", \$portlan_lanname);
	
	if (defined($portlan_exptidx) && defined($portlan_lanname)) {
	    $experiment = Experiment->Lookup($portlan_exptidx);
	    if (defined($experiment)) {
		print $node->node_id() . ":$iface in ";
		print $experiment->pid() . "/" . $experiment->eid() . " ";
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
		print "is using this vlan";
		if ($force) {
		    print ", but we are going to forcibly revoke it!\n";

		    my $portlan = Lan->Lookup($experiment, $portlan_lanname);
		    if (!defined($portlan)) {
			$vlan->Unlock();
			fatal("Could not revoke sharing of $vlan by ".
			      "$portlan_lanname in $experiment cause ".
			      "portlan_lanname does not exist!\n");
		    }
		    if ($experiment->ClearPortLans(1, $portlan)) {
			$vlan->Unlock();
			fatal("Could not revoke sharing of $vlan by ".
			      "$portlan_lanname in $experiment!\n");
		    }
		}
		else {
		    print "!\n";
		    $busy++;
		}
307 308
	    }
	    else {
309 310
		print $node->node_id() . ":$iface is marked as shared in this ";
		print "vlan but the experiment is gone!\n";
311 312 313 314 315
	    }
	}
    }
    if ($busy) {
	print "*** Refusing to delete shared vlan until sharers give it up!\n";
316
	$vlan->Unlock();
317 318
	exit(1);
    }
319
    
320 321
    $vlan->Unlock();
    DBQueryFatal("delete from shared_vlans where lanid='$lanid'");
322 323 324 325 326 327 328
    exit(0);
}

my $eid     = $ARGV[0];
my $lanname = $ARGV[1];
my $token   = $ARGV[2];

329 330 331 332
my $experiment = Experiment->Lookup($eid);
if (!defined($experiment)) {
    fatal("No such experiment $eid");
}
333 334
if ($experiment->state() ne EXPTSTATE_ACTIVE() && !$force) {
    fatal("Experiment is not active; use -f option to override");
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
}
my $vlan = VLan->Lookup($experiment, $lanname);
if (!defined($vlan)) {
    fatal("No such lan in $experiment");
}
my $lanid = $vlan->lanid();

my $query_result =
    DBQueryFatal("select * from shared_vlans where lanid='$lanid'");
if ($query_result->numrows) {
    fatal("$lanname is already shared");
}
if (! ($token =~ /^[-\w]*$/)) {
    fatal("Illegal characters in token"); 
}
$query_result =
    DBQueryFatal("select * from shared_vlans where token='token'");
if ($query_result->numrows) {
    fatal("$token is already used in the shared vlans table");
}
my $pid = $experiment->pid();
$eid    = $experiment->eid();
my $idx = $experiment->idx();
my $uid = $this_user->uid();
my $uid_idx = $this_user->uid_idx();

DBQueryFatal("insert into shared_vlans set open='$open', ".
	     "  pid='$pid', eid='$eid', exptidx='$idx', ".
	     "  token='$token', lanid='$lanid', vname='$lanname', ".
	     "  created=now(), creator='$uid', creator_idx='$uid_idx'");
365 366 367 368 369 370 371 372 373

#
# Since that worked, clear the switch path. Whatever the mapper
# computed will be wrong as soon as we start adding ports. By
# clearing the path, we are telling snmpit to compute a new path
# as ports are added or removed from the vlan.
#
$vlan->ClrSwitchPath();

374 375 376 377 378 379 380 381 382 383
exit(0);

sub fatal($)
{
    my ($mesg) = $_[0];

    die("*** $0:\n".
	"    $mesg\n");
}