link_config.in 11.1 KB
Newer Older
1 2
#!/usr/bin/perl -w
#
3
# Copyright (c) 2004, 2007 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 27 28 29 30 31 32 33 34 35
#
use English;
use Getopt::Std;

#
# Change delay params for a link.
#
# XXX - Does not handle vnodes yet.
#
sub usage()
{
    print(STDERR
	  "Usage: link_config [-m] [-d] [-s vnode] <pid> <eid> <link>".
36
	  " [PARAM=VALUE ...\n".
37 38 39 40 41 42 43 44
	  "Required: pid, eid, link, and at least one parameter to change!\n".
	  "  pid = Project ID\n".
	  "  eid = Experiment ID\n".
	  " link = link name from ns file, ie. 'link1' in\n".
	  "        'set link1 [\$ns duplex-link \$A \$B 10Kb 0ms DropTail]'\n".
	  "Options:\n".
	  "   -d = turn on debugging\n".
	  "   -s = Select the source of the link to determine which pipe\n".
45 46 47 48
	  "   -m = Modify the base experiment in addition to current state.\n".
	  "Special PARAM:\n".
	  " ENABLE=yes/no    - Bring the link up or down (or ENABLE=up/down)".
	  "\n");
49 50 51 52 53
    # Web interface cares about this return value!
    exit(2);
}
my  $optlist = "dms:";

54 55
sub ChangeVirtSettings();

56 57 58 59 60 61 62 63 64 65 66 67 68 69
#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
my $TEVC        = "$TB/bin/tevc";
my $debug	= 0;

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libdb;
use libtestbed;
70
use Experiment;
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 101

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

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/sbin:/usr/bin:/usr/sbin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
if (@ARGV < 4) {
    usage();
}
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}

my $pid    = shift(@ARGV);
my $eid    = shift(@ARGV);
my $link   = shift(@ARGV);
my $modify = 0;
102
my $enable;		# Special case for bringing interface up/down.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
my $srcvnode;
my $member;		# For operating on virt_lans table (vname:port)
my %physmap;		# For operating on the interface_settings table (pcXXX)
my %config;

if (defined($options{"m"})) {
    $modify = 1;
}
if (defined($options{"s"})) {
    $srcvnode = $options{"s"};

    if ($srcvnode =~ /^([-\w]+)$/) {
	$srcvnode = $1;
    }
    else {
	die("*** Bad srcvnode name: $srcvnode.\n");
    }
}
#
# Untaint args.
#
if ($pid =~ /^([-\w]+)$/) {
    $pid = $1;
}
else {
    die("*** Bad data in pid: $pid.\n");
}
if ($eid =~ /^([-\w]+)$/) {
    $eid = $1;
}
else {
    die("*** Bad data in eid: $eid.\n");
}
if ($link =~ /^([-\w]+)$/) {
    $link = $1;
}
else {
    die("*** Bad data in link: $link.\n");
}

#
# Permission check.
#
if ($UID && !TBAdmin($UID) &&
    ! TBExptAccessCheck($UID, $pid, $eid, TB_EXPT_MODIFY)) {
    die("*** $0:\n".
	"    You do not have permission to modify the delay parameters!\n");
}

152 153 154 155 156 157
my $exptidx;
if (!TBExptIDX($pid, $eid, \$exptidx)) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid exists!\n");
}

158 159 160 161 162 163 164 165 166 167 168 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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
#
# No transitional experiments.
#
my $estate = ExpState($pid, $eid);
if (! $estate) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid exists!\n");
}
if ($estate ne EXPTSTATE_ACTIVE &&
    $estate ne EXPTSTATE_SWAPPED) {
    die("*** $0:\n".
	"    Experiment $pid/$eid must be ACTIVE or SWAPPED\n".
	"    to alter its delay configuration.\n");
}

#
# To change the interface_settings table, we need to know the physnode names
# and the interface on that physnode. If a vnode provided, need to find just
# that one. If changing the entire lan, we need all of the physnodes/ifaces
# for that lan. This is a complicated operation since we need to go from the
# the virt_lans table (member) to the reserved table, to the interfaces table
# to the interface_settings table!
#
if ($estate eq EXPTSTATE_ACTIVE) {
    #
    # This is absurd. Who came up with this table representation!
    #  
    my $query_result =
	DBQueryFatal("select vl.member,vn.ips,r.node_id from virt_lans as vl ".
		     "left join virt_nodes as vn on vl.pid=vn.pid and ".
		     "     vl.eid=vn.eid and ".
		     "     vn.vname=SUBSTRING(vl.member, 1, ".
		     "                        POSITION(':' in vl.member)-1) ".
		     "left join reserved as r on r.pid=vl.pid and ".
		     "     r.eid=vl.eid and r.vname=vn.vname ".
		     "where vl.pid='$pid' and vl.eid='$eid' and ".
		     "      vl.vname='$link'");

    while (my ($member, $ips, $nodeid) = $query_result->fetchrow_array()) {
	my ($vname, $port) = split(":", $member);
	my @iplist         = split(" ", $ips);

	foreach my $ipmemb (@iplist) {
	    my ($ipport,$ipip) = split(":", $ipmemb);

	    if ($port == $ipport) {
		my $iface_result =
		    DBQueryFatal("select iface from interfaces ".
				 "where IP='$ipip' and node_id='$nodeid'");
		if (!$iface_result->numrows) {
		    die("*** $0:\n".
			"    Could not map IP to iface on $nodeid!\n");
		}
		my ($iface) = $iface_result->fetchrow_array();
		$physmap{$member}->{"nodeid"} = $nodeid;
		$physmap{$member}->{"iface"}  = $iface;
		last;
	    }
	}
    }
}

#
# Parse options, which will modify the existing configuration below.
#
while (@ARGV) {
    my $opt = shift(@ARGV);

    #
    # The parameters are completely add-hoc. Just make sure they are legal
    # for the DB. The user can basically say whatever they like, and if the
    # agent cannot make sense of it, too bad.
    #
    if ($opt =~ /^(.*)=(.*)$/) {
	my $key = $1;
	my $val = $2;

235 236 237 238 239 240 241 242 243 244 245 246 247 248
	if ($key eq "enable" || $key eq "ENABLE") {
	    if ($val eq "yes" || $val eq "up") {
		$enable = "yes";
	    }
	    elsif ($val eq "no" || $val eq "no") {
		$enable = "no";
	    }
	    else {
		die("*** $0:\n".
		    "    Should be ENABLE=yes/no or ENABLE=up/down!\n");
	    }
	    next;
	}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 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 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
	if (! TBcheck_dbslot($key, "virt_lan_settings", "capkey",
			     TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    die("*** $0:\n".
		"    Illegal characters in parameter key: $key\n");
	}
	if ($val eq "") {
	    $val = undef;
	}
	elsif (! TBcheck_dbslot($val, "virt_lan_settings", "capval",
			      TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR)) {
	    die("*** $0:\n".
		"    Illegal characters in parameter val: $val\n");
	}
	$config{$key} = $val;
    }
    else {
	die("*** $0:\n".
	    "    Must use PARAM=VALUE arguments\n");
    }
}
    
#
# Debug.
#
if ($debug) {
    foreach my $key (keys(%config)) {
	my $val = $config{$key};

	$val = ""
	    if (!defined($val));

	print "$key=$val\n";
    }
}

#
# Link or Lan!
# 
$query_result =
    DBQueryFatal("select member from virt_lans ".
		 "where pid='$pid' and eid='$eid' and vname='$link'");
if (!$query_result->numrows) {
    die("*** $0:\n".
	"    $link is not a link in $pid/$eid!\n");
}
my $islink = ($query_result->numrows == 2 ? 1 : 0);

#
# Find the actual member (vname:port) so we can plug it back in later.
#
if (defined($srcvnode)) {
    while (my ($memb) = $query_result->fetchrow_array()) {
	my ($vnode, $port) = split(":",$memb);

	if ($vnode eq $srcvnode) {
	    $member = $memb;
	    last;
	}
    }
    if (!defined($member)) {
	die("*** $0:\n".
	    "    Cannot map $srcvnode to a virt_lan member in $pid/$eid\n");
    }
}

#
# If experiment is not active, all we can do is change virt_lans.
#
if ($estate ne EXPTSTATE_ACTIVE) {
    if ($modify) {
	ChangeVirtSettings();
    }
    else {
	print "Experiment $pid/$eid is not active. If you want to change\n".
	    "the base experiment configuration, please use the -m option!\n";
    }
    exit(0);
}

#
# Deal with changing physical table (interface_settings) if experiment
# is currently active.
#
if (defined($srcvnode)) {
    #
    # Changing just one interface.
    #
    my $nodeid = $physmap{$member}->{"nodeid"};
    my $iface  = $physmap{$member}->{"iface"};

    foreach my $key (keys(%config)) {
	my $val = $config{$key};

	if (defined($val)) {
	    DBQueryFatal("replace into interface_settings ".
			 " (node_id, iface, capkey, capval) ".
			 "values ".
			 " ('$nodeid', '$iface', '$key', '$val')");
	}
	else {
	    DBQueryFatal("delete from interface_settings ".
			 "where node_id='$nodeid' and iface='$iface' and ".
			 "      capkey='$key'");
	}
    }
}
else {
    foreach my $key (keys(%config)) {
	my $val = $config{$key};

	foreach my $member (keys(%physmap)) {
	    my $nodeid = $physmap{$member}->{"nodeid"};
	    my $iface  = $physmap{$member}->{"iface"};

	    if (defined($val)) {
		DBQueryFatal("replace into interface_settings ".
			     " (node_id, iface, capkey, capval) ".
			     "values ".
			     " ('$nodeid', '$iface', '$key', '$val')");
	    }
	    else {
		DBQueryFatal("delete from interface_settings ".
			     "where node_id='$nodeid' and iface='$iface' and ".
			     "      capkey='$key'");
	    }
	}
    }
}

# Change base experiment.
if ($modify) {
    ChangeVirtSettings();
}

#
# Inject an event. 
#
my $inject_string = "$TEVC -e $pid/$eid now $link";

# Direct the event to the whoever is handling this particular delay.
$inject_string .= "-${srcvnode} "
    if (defined($srcvnode));

$inject_string .= " modify ";

394 395 396 397 398
# Add in enable=yes/no first.
if (defined($enable)) {
    $inject_string .= "enable=${enable} ";
}

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
# Append the parameters.
foreach my $key (keys(%config)) {
    my $val = $config{$key};

    $val = ""
	if (!defined($val));
    
    $inject_string .= "${key}=$val ";
}
if ($debug) {
    print "$inject_string\n";
}

system($inject_string) &&
    die("*** $0:\n".
	"    Failed to inject delay update event!\n");
    
exit(0);

#
# Change virt settings in the DB.
#
sub ChangeVirtSettings() {
    foreach my $key (keys(%config)) {
	my $val = $config{$key};

	#
	# When making a change to the lan, must remove any per-node
	# setting of the same name. 
	#
	if (!defined($srcvnode)) {
	    DBQueryFatal("delete from virt_lan_member_settings ".
			 "where pid='$pid' and eid='$eid' and ".
			 "      vname='$link' and capkey='$key'");

	    if (defined($val)) {
		DBQueryFatal("replace into virt_lan_settings ".
436
			     " (exptidx, pid, eid, vname, capkey, capval) ".
437
			     "values ".
438 439
			     " ('exptidx',".
			     "  '$pid','$eid','$link','$key','$val')");
440 441 442 443 444 445 446 447 448 449
	    }
	    else {
		DBQueryFatal("delete from virt_lan_settings ".
			     "where pid='$pid' and eid='$eid' and ".
			     "      vname='$link' and capkey='$key'");
	    }
	}
	else {
	    if (defined($val)) {
		DBQueryFatal("replace into virt_lan_member_settings ".
450
			     " (exptidx,pid,eid,vname,member,capkey,capval) ".
451
			     "values ".
452
			     " ('$exptidx','$pid','$eid','$link','$member', ".
453 454 455 456 457 458 459 460 461 462 463
			     "  '$key', '$val')");
	    }
	    else {
		DBQueryFatal("delete from virt_lan_member_settings ".
			     "where pid='$pid' and eid='$eid' and ".
			     "      vname='$link' and capkey='$key' and ".
			     "      member='$member'");
	    }
	}
    }
}