delay_config.in 22.8 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1
#!/usr/bin/perl -wT
Leigh Stoller's avatar
Leigh Stoller committed
2 3

#
4
# Copyright (c) 2000-2016 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
# 
# {{{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/>.
# 
# }}}
Leigh Stoller's avatar
Leigh Stoller committed
24 25
#

26 27
use English;
use Getopt::Std;
28
use XML::Simple;
29

30 31 32 33 34
#
# Change delay params for a link.
#
sub usage()
{
35
    print(STDERR
36
	  "Usage: delay_config [-m] [-d] [-s vnode] <pid> <eid> <link | bridge>".
37
	  " PARAM=VALUE ...\n".
38
	  "       delay_config [-d] -X <xmlfile>\n".
39
	  "Required: pid, eid, link, and at least one parameter to change!\n".
40 41 42 43 44
	  "  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".
	  " bridge = or the bridge name, if explicitly using a bridge node\n".
45 46
	  "Options:\n".
	  "   -d = turn on debugging\n".
47
	  "   -b = bridge mode; operating on a bridge node instead of link\n".
48
	  "   -s = Select the source of the link to determine which pipe\n".
49
	  "        In bridge mode (-b) these are the link names attached\n".
50
	  "   -m = Modify the base experiment in addition to current state.\n".
51
	  "   -X = Get args and parameters from an XML file.\n".
52
	  "Parameters:\n".
53
	  " BANDWIDTH=NNN    - N=bandwidth (10-1000000 Kbits per second)\n",
54 55 56 57 58 59
	  " PLR=NNN          - N=lossrate (0 <= plr < 1)\n".
	  " DELAY=NNN        - N=delay (one-way delay in milliseconds > 0)\n".
	  " LIMIT=NNN        - The queue size in bytes or packets\n".
	  " QUEUE-IN-BYTES=N - 0 means in packets, 1 means in bytes\n".
	  "RED/GRED Options: (only if link was specified as RED/GRED)\n".
	  " MAXTHRESH=NNN    - Maximum threshold for the average queue size\n".
Leigh Stoller's avatar
Leigh Stoller committed
60
	  " THRESH=NNN       - Minimum threshold for the average queue size\n".
61 62
	  " LINTERM=NNN      - Packet dropping probability\n".
	  " Q_WEIGHT=NNN     - For calculating the average queue size\n");
63 64
    # Web interface cares about this return value!
    exit(2);
65
}
66
my  $optlist = "dms:cX:b";
67 68 69 70 71 72

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
73
my $TEVC        = "$TB/bin/tevc";
74
my $GENTOPO     = "$TB/libexec/gentopofile";
75 76 77 78 79 80 81 82 83 84 85 86

# Locals
my $pid;
my $eid;
my $link;
my $modify     = 0;
my $srcvnode;
my %config;
my $pipeno;
my $pipe;
my $debug      = 0;
my $compatmode = 0;
87
my $bridemode  = 0;
88 89 90 91

#
# Testbed Support libraries
#
92 93
use lib "@prefix@/lib";
use libdb;
94
use libtestbed;
95 96
use Experiment;
use User;
97

98 99 100 101 102 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
#
# Function prototypes
#
sub ChangeDelayConfig();
sub ChangeLinkDelayConfig();
sub ChangeVirtLans();
sub ParseXmlArgs($$$$$$);
sub fatal($);

#
# These are the fields that we allow to come in from the XMLfile.
#
my $SLOT_OPTIONAL	= 0x1;	# The field is not required.
my $SLOT_REQUIRED	= 0x2;  # The field is required and must be non-null.
my $SLOT_ADMINONLY	= 0x4;  # Only admins can set this field.
my %xmlfields =
    # XML Field Name        DB slot name         Flags             Default
    ("pid"		=> ["pid",		$SLOT_REQUIRED],
     "eid"		=> ["eid",		$SLOT_REQUIRED],
     "link"		=> ["vname",		$SLOT_REQUIRED],

     "vnode"		=> ["vnode",		$SLOT_OPTIONAL],
     "modify"		=> ["modbase",		$SLOT_OPTIONAL,	   0],
     "compat"		=> ["compat",		$SLOT_OPTIONAL,	   0],

     "bandwidth"	=> ["bandwidth",	$SLOT_OPTIONAL],
     "plr"		=> ["lossrate",		$SLOT_OPTIONAL],
     "delay"		=> ["delay",		$SLOT_OPTIONAL],
     "limit"		=> ["q_limit",		$SLOT_OPTIONAL],
     "queue-in-bytes"	=> ["q_bytes",		$SLOT_OPTIONAL],
128 129
     "thresh"		=> ["q_minthresh",	$SLOT_OPTIONAL],
     "maxthresh"	=> ["q_maxthresh",	$SLOT_OPTIONAL],
130 131 132 133
     "minthresh"	=> ["q_minthresh",	$SLOT_OPTIONAL],
     "linterm"		=> ["q_linterm",	$SLOT_OPTIONAL],
     "q_weight"		=> ["q_weight",		$SLOT_OPTIONAL]);

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
#
# 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.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
153 154 155
if (@ARGV < 4 && !defined($options{"X"}) ) {
    usage();
}
156
if (defined($options{"d"})) {
157 158
    $debug = 1;
}
159 160 161
if (defined($options{"c"})) {
    $compatmode = 1;
}
162 163 164
if (defined($options{"b"})) {
    $bridgemode = 1;
}
165
if (!defined($options{"X"})) {
166 167 168
    $pid  = shift(@ARGV);
    $eid  = shift(@ARGV);
    $link = shift(@ARGV);
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}
else {
    $xmlfile = $options{"X"};

    my %xmlargs = (); 
    my %errors = ();
    ParseXmlArgs($xmlfile, "virt_lans", \%xmlfields, $debug,
		 \%xmlargs, \%errors);
    if (keys(%errors)) {
	foreach my $errkey (keys(%errors)) {
	    my $errval = $errors{$errkey};
	    print "${errkey}: $errval\n";
	}
	fatal("XML arg error");
    }
184

185 186 187
    # There should be no other trailing args along with -X <xmlfile>.
    usage()
	if (@ARGV > 0);
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
    foreach my $arg (keys(%xmlargs)) {
	# Required.
	if ($arg eq "pid") {
	    $pid = $xmlargs{"pid"};
	}
	elsif ($arg eq "eid") {
	    $eid = $xmlargs{"eid"};
	}
	elsif ($arg eq "link") {
	    $link = $xmlargs{"link"};
	}
	# Optional.
	elsif ($arg eq "modify") {
	    $modify = $xmlargs{"modify"};
	}
	elsif ($arg eq "compat") {
	    $compatmode = $xmlargs{"compat"};
	}
	elsif ($arg eq "vnode") {
	    $srcvnode = $xmlargs{"vnode"};
	}
	# Push parameters onto ARGV for handling below.
	else {
	    push(@ARGV, "$arg=" . $xmlargs{$arg});
	}
    }
}
216 217 218
if (defined($options{"m"})) {
    $modify = 1;
}
219 220
if (defined($options{"s"})) {
    $srcvnode = $options{"s"};
Leigh Stoller's avatar
Leigh Stoller committed
221

222
    # Taint check cause it goes to a shell command.
Leigh Stoller's avatar
Leigh Stoller committed
223
    if ($srcvnode =~ /^([-\w]+)$/) {
Leigh Stoller's avatar
Leigh Stoller committed
224
	$srcvnode = $1;
Leigh Stoller's avatar
Leigh Stoller committed
225 226 227 228 229
    }
    else {
	die("*** Bad srcvnode name: $srcvnode.\n");
    }
}
230

231 232 233
my $experiment = Experiment->Lookup($pid, $eid);
if (!defined($experiment)) {
    fatal("No such experiment $pid,$eid");
Leigh Stoller's avatar
Leigh Stoller committed
234
}
235 236 237 238 239
# Untaint args for shell later.
$pid = $experiment->pid();
$eid = $experiment->eid();

# Taint check link cause it goes to a shell command.
Leigh Stoller's avatar
Leigh Stoller committed
240 241 242 243
if ($link =~ /^([-\w]+)$/) {
    $link = $1;
}
else {
244
    fatal("Bad link name: $link");
245
}
246

247 248 249
#
# Permission check.
#
250 251 252 253 254 255 256 257
my $this_user = User->ThisUser();
if (! defined($this_user)) {
    fatal("You ($UID) do not exist!");
}

if ($UID && !$this_user->IsAdmin() &&
    ! $experiment->AccessCheck($this_user, TB_EXPT_MODIFY)) {
    fatal("You do not have permission to modify the delay parameters!\n");
258 259 260 261 262
}

#
# No transitional experiments.
#
263
my $estate = $experiment->state();
264 265
if ($estate ne EXPTSTATE_ACTIVE &&
    $estate ne EXPTSTATE_SWAPPED) {
266
    fatal("Experiment must be ACTIVE or SWAPPED to change delays");
267 268
}

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

275 276 277 278 279 280 281 282 283 284
    #
    # The parameter names correspond roughly to the names that the user
    # uses in the NS file. The $config{} keys correspond to the names of
    # the slots in the DB table. Well, roughly correspond since the delays
    # table uses q0_ and q1_, but we handle that below.
    #
    # XXX If you add to this list, be sure to add to agentmap hash below.
    #
    SWITCH: for ($opt) {
	/^BANDWIDTH=([0-9]*)$/i && do {
285
	    if ($1 > 1000000 || $1 < 10) {
286 287 288 289 290 291 292 293 294 295 296 297 298
		usage();
	    }
	    $config{"bandwidth"} = $1;
	    last SWITCH;
	};
	/^DELAY=([0-9]*)$/i && do {
	    if ($1 < 0) {
		usage();
	    }
	    $config{"delay"} = $1;
	    last SWITCH;
	};
	/^PLR=([0-9\.]*)$/i && do {
299
	    if ($1 < 0 || $1 > 1) {
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
		usage();
	    }
	    $config{"lossrate"} = $1;
	    last SWITCH;
	};
	/^LIMIT=([0-9]*)$/i && do {
	    $config{"q_limit"} = $1;
	    last SWITCH;
	};
	/^QUEUE-IN-BYTES=(\d)$/i && do {
	    if ($1 != 0 && $1 != 1) {
		usage();
	    }
	    $config{"q_qinbytes"} = $1;
	    last SWITCH;
	};
	/^MAXTHRESH=(\d*)$/i && do {
	    $config{"q_maxthresh"} = $1;
	    last SWITCH;
	};
	/^THRESH=(\d*)$/i && do {
	    $config{"q_minthresh"} = $1;
	    last SWITCH;
	};
	/^LINTERM=([0-9\.]*)$/i && do {
	    $config{"q_linterm"} = $1;
	    last SWITCH;
	};
	/^Q_WEIGHT=([0-9\.]*)$/i && do {
	    $config{"q_weight"} = $1;
	    last SWITCH;
	};
	print "Invalid config option: $opt\n";
333 334
	usage();
    }
335
}
336

337 338 339 340 341 342 343 344 345 346 347 348
#
# More sanity checks.
#
foreach my $key (keys(%config)) {
    my $val = $config{$key};

    if ($debug) {
	print "$key=$val\n";
    }
}

#
349
# These map the names I am using in the config hash (which correspond
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
# to the DB slot names) into the event parameter names to send over to the
# delay agent.
#
my %agentmap =
    ( bandwidth		=> BANDWIDTH,
      delay             => DELAY,
      lossrate		=> PLR,
      q_limit		=> LIMIT,
      q_qinbytes	=> "QUEUE-IN-BYTES",
      q_maxthresh	=> MAXTHRESH,
      q_minthresh	=> THRESH,
      q_linterm		=> LINTERM,
      q_weight		=> Q_WEIGHT
    );

365
#
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
# Link or Lan or Bridge.
#
if ($bridgemode) {
    $query_result =
	DBQueryFatal("select * from virt_bridges where vname='$link'");

    if (!$query_result->numrows) {
	fatal("$link is not a bridge in $pid/$eid!\n");
    }
}
else {
    $query_result =
	DBQueryFatal("select member,bridge_vname from virt_lans ".
		     "where pid='$pid' and eid='$eid' and vname='$link'");
    if (!$query_result->numrows) {
	fatal("$link is not a link in $pid/$eid!\n");
    }
383
}
384
my $islink = ($query_result->numrows == 2 ? 1 : 0);
385

386 387 388 389 390 391 392 393 394 395 396 397 398
#
# When a link is bridged, must call this script with the bridge name
# instead of the link name, since a link can potentially be bridged on
# both sides, and because a bridge connects two *different* links.
#
if (!$bridgemode) {
    while (my ($member,$bridge_vname) = $query_result->fetchrow_array()) {
	if (defined($bridge_vname)) {
	    fatal("$link is bridged; please use the -b option instead.\n");
	}
    }
}

399
#
400
# If experiment is not active, all we can do is change virt_lans.
401
#
402 403 404 405 406 407 408 409 410
if ($estate ne EXPTSTATE_ACTIVE) {
    if ($modify) {
	ChangeVirtLans();
    }
    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);
411
}
412

413 414 415 416 417 418
#
# Check to see if linkdelays or normal delay nodes. This is a gross
# complication of this entire script!
#
if (ChangeDelayConfig() < 0 &&
    ChangeLinkDelayConfig() < 0) {
419
    die("*** $0:\n".
420
	"    $link is not a traffic shaped link in $pid/$eid!\n");
421
}
422 423 424
if ($modify) {
    ChangeVirtLans();
}
425

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
#
# 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 ";

# Append the parameters.
foreach my $key (keys(%config)) {
    my $val = $config{$key};
    my $str = $agentmap{$key};
441 442 443 444 445 446 447 448 449 450 451 452 453 454

    #
    # When changing an entire lan, we have to adjust delay/loss since
    # there is a delay *to* the lan and a delay *from* the lan.
    # We do not do this for links since there is just a single delay.
    # When changing a single node in a lan, we fall back to the way things
    # operate in the parser, which is that the user intended to change both
    # directions to exactly this value. 
    #
    if (!$islink && !defined($srcvnode) && !$compatmode) {
        if ($key eq "delay") {
	    # This follows what is done in the parser.
	    $val = $val / 2.0;
	}
Leigh Stoller's avatar
Leigh Stoller committed
455
        elsif ($key eq "lossrate") {
456 457 458 459
	    # This follows what is done in the parser.
	    $val = 1-sqrt(1-$val);
	}
    }
460 461 462 463 464 465 466 467 468
    $inject_string .= "${str}=$val ";
}
if ($debug) {
    print "$inject_string\n";
}

system($inject_string) &&
    die("*** $0:\n".
	"    Failed to inject delay update event!\n");
469 470 471 472 473 474 475

print "*** WARNING:\n";
print "    You should *always* test your links and lans to make sure they\n";
print "    are behaving as you expect. You can do this by hand with ping,\n";
print "    or you can use Emulab's LinkTest feature.\n";
print "    Type 'linktest' into the Emulab search box to find out how to ".
    "use it.\n";
476 477 478
    
exit(0);

479
#
480 481 482 483 484
# Get current delay configuration. 
# 
sub ChangeDelayConfig() {
    my $query_clause;
    my $query_string;
485

486
    $query_string = "select * from delays ".
487
	"where pid='$pid' and eid='$eid' and vname='$link' and noshaping=0 ";
488

489 490 491 492 493 494 495 496 497
    if (defined($srcvnode)) {
	if ($bridgemode) {
	    # The src is the name of the link.
	    $query_string .= "and (vlan0='$srcvnode' or vlan1='$srcvnode')";
	}
	else {
	    $query_string .= "and (vnode0='$srcvnode' or vnode1='$srcvnode')";
	}
    }
498

499 500 501 502 503 504
    #
    # Okay, see if there is a delay node.
    # 
    my $query_result = DBQueryFatal($query_string);
    if (! $query_result->numrows) {
	return -1;
505
    }
506 507 508 509 510 511 512 513 514 515 516 517 518 519
    
    if (defined($srcvnode) && $islink) {
	#
	# If given a source node of a duplex link, must map that into
	# the actual delay pipe side and the pipe number, since the
	# names of the slots in the DB table have a 0/1 appended. Big
	# Yuck.
	#
	if ($query_result->numrows != 1) {
	    die("*** $0:\n".
		"    Too many delay links for $link in $pid/$eid!\n");
	}
	my %row = $query_result->fetchhash();

520
	if ($row{'vnode0'} eq $srcvnode || $row{'vlan0'} eq $srcvnode) {
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
	    $pipe   = 0;
	}
	else {
	    $pipe   = 1;
	}
	$pipeno = $row{"pipe${pipe}"};
	
	foreach my $key (keys(%config)) {
	    my $val = $config{$key};

	    $query_clause .= ", "
		if (defined($query_clause));
		
	    if ($key =~ /^q_(.*)/) {
		$query_clause .= "q${pipe}_${1}=$val";
	    }
	    else {
		$query_clause .= "${key}${pipe}=$val";
	    }
	}
541
    }
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558
    else {
	#
	# We are changing a link or entire lan symmetrically, or we are
	# changing one node in a lan symmetrically. Note, we cannot yet
	# make an asymmetric change to an indvidual lan node. Sorry, too
	# much pain. 
	#
	foreach my $key (keys(%config)) {
	    my $val = $config{$key};

	    $query_clause .= ", "
		if (defined($query_clause));
		
	    if ($key =~ /^q_(.*)/) {
		$query_clause .= "q0_${1}=$val,q1_${1}=$val";
	    }
	    else {
559 560 561 562 563 564 565 566 567
		#
		# When changing an entire lan, we have to adjust delay/loss
		# since there is a delay *to* the lan and a delay *from*
		# the lan.  We do not do this for links since there is just
		# a single delay.  When changing a single node in a lan, we
		# fall back to the way things operate in the parser, which
		# is that the user intended to change both directions to
		# exactly this value.
		#
568 569
		if (!$bridgemode &&
		    !$islink && !defined($srcvnode) && !$compatmode) {
570 571 572 573
		    if ($key eq "delay") {
			# This follows what is done in the parser.
			$val = $val / 2.0;
		    }
Leigh Stoller's avatar
Leigh Stoller committed
574
		    elsif ($key eq "lossrate") {
575 576 577 578
			# This follows what is done in the parser.
			$val = 1-sqrt(1-$val);
		    }
		}
579 580 581
		$query_clause .= "${key}0=$val,${key}1=$val";
	    }
	}
582
    }
583 584 585 586 587 588 589 590 591 592
    
    #
    # Update the delays table.
    #
    $query_string =
	"update delays set $query_clause ".
	"where pid='$pid' and eid='$eid' and vname='$link' ";
    
    if (defined($srcvnode)) {
	$query_string .= "and (vnode0='$srcvnode' or vnode1='$srcvnode')";
593 594
    }
    if ($debug) {
595
	print "$query_string\n";
596
    }
597 598 599
    DBQueryFatal($query_string);
    return 0;
}    
600

601 602 603 604 605 606
#
# Change linkdelays.
# 
sub ChangeLinkDelayConfig() {
    my @query_clauses;
    my $query_string;
607

608
    $query_string = "select l.*,ov.OS from linkdelays as l ".
609 610
	"left join nodes as n on n.node_id=l.node_id ".
	"left join os_info as o on o.osid=n.def_boot_osid ".
611 612
	"left join os_info_versions as ov on o.osid=ov.osid and ".
	"  o.version=ov.vers and ov.deleted is null ".
613
	"where l.pid='$pid' and l.eid='$eid' and l.vlan='$link' ";
614

615
    $query_string .= "and l.vnode='$srcvnode'"
616
	if (defined($srcvnode));
617

618 619 620 621 622 623
    #
    # Okay, see if there is a linkdelay.
    # 
    my $query_result = DBQueryFatal($query_string);
    if (! $query_result->numrows) {
	return -1;
624
    }
625

626 627 628 629 630 631 632 633
    #
    # XXX Check for linux; temporary.
    #
    while (my $row = $query_result->fetchrow_hashref()) {
	my $vnode = $row->{'vnode'};
	my $OS    = $row->{'OS'};

	if ($OS eq "Linux") {
634 635 636 637
	    print STDERR
		"*** $0:\n".
		"    Oops, dynamic events do not yet work on Linux!\n";
	    exit(2);
638 639 640
	}
    }

641
    #
642 643 644 645 646 647
    # This is much easier than delay nodes! As above, we cannot change a
    # lan node asymmetrically yet.
    #
    foreach my $key (keys(%config)) {
	my $val = $config{$key};

648 649 650 651 652
	if (!$islink && !defined($srcvnode) && !$compatmode) {
	    if ($key eq "delay") {
		# This follows what is done in the parser.
		$val = $val / 2.0;
	    }
Leigh Stoller's avatar
Leigh Stoller committed
653
	    elsif ($key eq "lossrate") {
654 655 656 657
		# This follows what is done in the parser.
		$val = 1-sqrt(1-$val);
	    }
	}
658 659 660 661 662 663 664 665 666 667 668 669 670 671
	push(@query_clauses, "${key}=$val");
	
	#
	# For a lan node, must also set the "r" params.
	# 
	if (!$islink &&
	    ($key eq "delay" ||
	     $key eq "bandwidth" ||
	     $key eq "lossrate")) {

	    push(@query_clauses, "r${key}=$val");
	}
    }
    
672
    #
673
    # Update the delays table.
674
    #
675 676 677
    $query_string = "update linkdelays set ".
	join(",", @query_clauses) . 
	" where pid='$pid' and eid='$eid' and vlan='$link' ";
678
    
679 680 681 682 683 684 685 686
    if (defined($srcvnode)) {
	$query_string .= "and vnode='$srcvnode'";
    }
    if ($debug) {
	print "$query_string\n";
    }
    DBQueryFatal($query_string);
    return 0;
687 688 689
}

#
690
# Change the virt_lans entry.
691
#
692 693 694 695
sub ChangeVirtLans() {
    my @query_clauses;
    my $query_string;
    my @query_clauses2;
696

697
    #
698
    # The first case is for changing a node in a duplex link asymmetrically.
699
    #
700 701 702 703 704 705
    if ($islink && defined($srcvnode)) {
	#
	# In a duplex link the delay/bw/plr params are split between the
	# two virt_lan members using the "r" params to hold the "from
	# switch" half of the value. This makes it rather confusing.
	# 
706 707 708 709
	foreach my $key (keys(%config)) {
	    my $val = $config{$key};

	    if ($key eq "delay") {
710
		my $delay = $val / 2.0;
711

712 713
		push(@query_clauses,  "delay=$delay");
		push(@query_clauses2, "rdelay=$delay");
714 715
	    }
	    elsif ($key eq "lossrate") {
716
		my $lossrate = 1-sqrt(1-$val);
717 718

		push(@query_clauses, "lossrate=$lossrate");
719
		push(@query_clauses2, "rlossrate=$lossrate");
720 721 722
	    }
	    elsif ($key eq "bandwidth") {
		push(@query_clauses, "bandwidth=$val");
723
		push(@query_clauses2, "rbandwidth=$val");
724 725 726 727 728 729 730
	    }
	    else {
		push(@query_clauses, "$key=$val");
	    }
	}
	$query_string = "update virt_lans set ".
	    join(",", @query_clauses) .
731 732 733
	    " where pid='$pid' and eid='$eid' and ".
	    "      vname='$link' and member like '${srcvnode}:%'";
	
734 735 736 737
	if ($debug) {
	    print "$query_string\n";
	}
	DBQueryFatal($query_string);
738 739 740 741 742 743 744 745 746 747 748 749 750

	if (@query_clauses2) {
	    $query_string =
		"update virt_lans set ".
		join(",", @query_clauses2) .
		" where pid='$pid' and eid='$eid' and ".
		"      vname='$link' and member not like '${srcvnode}:%'";
	
	    if ($debug) {
		print "$query_string\n";
	    }
	    DBQueryFatal($query_string);
	}
751 752 753
    }
    else {
	#
754 755 756 757 758
	# When changing an entire link or lan symmetrically its easy; they can
	# be done the same. When changing one node in a lan (symmetrically),
	# the numbers are slightly different for delay/lossrate, but otherwise
	# its the same operation, except for operating on a single node.
	#
759 760 761 762
	foreach my $key (keys(%config)) {
	    my $val = $config{$key};

	    if ($key eq "delay") {
763
		my $delay = $val;
764

765 766 767 768 769 770 771
		# This follows what is done in the parser;
		# See tb-set-node-lan-delay
		$delay = $delay / 2.0
		    if (!defined($srcvnode));

		push(@query_clauses, "delay=$delay");
		push(@query_clauses, "rdelay=$delay");
772 773
	    }
	    elsif ($key eq "lossrate") {
774 775 776 777 778 779
		my $lossrate = $val;

		# This follows what is done in the parser.
		# See tb-set-node-lan-lossrate
		$lossrate = 1-sqrt(1-$lossrate)
		    if (!defined($srcvnode));
780 781

		push(@query_clauses, "lossrate=$lossrate");
782
		push(@query_clauses, "rlossrate=$lossrate");
783 784 785
	    }
	    elsif ($key eq "bandwidth") {
		push(@query_clauses, "bandwidth=$val");
786
		push(@query_clauses, "rbandwidth=$val");
787 788 789 790 791
	    }
	    else {
		push(@query_clauses, "$key=$val");
	    }
	}
792

793 794
	$query_string = "update virt_lans set ".
	    join(",", @query_clauses) .
795 796 797 798 799 800 801 802 803
	    " where pid='$pid' and eid='$eid' and vname='$link'";

	#
	# A lan node change since it cannot be a link if srcvnode defined.
	# 
	if (defined($srcvnode)) {
	    $query_string .= " and member like '${srcvnode}:%'";
	}

804 805 806 807 808
	if ($debug) {
	    print "$query_string\n";
	}
	DBQueryFatal($query_string);
    }
809 810 811 812 813 814 815 816
    #
    # Now we need to regen the linktest map file ...
    #
    system("$GENTOPO $pid $eid");
    if ($?) {
	die("*** $0:\n".
	    "    $GENTOPO failed!\n");
    }
817
}
818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860

sub ParseXmlArgs($$$$$$) {
    my ($xmlfile, $table_name, $fields_ref, $debug, 
	$args_ref, $errs_ref) = @_;
    #
    # Input args:
    #  $xmlfile	   - XML file path.
    #  $table_name - table_regex table_name for low-level checking patterns.
    #  $fields_ref - xmlfields specification (hash reference.)
    #  $debug
    #
    # Output args:
    #  $args_ref   - Parsed argument values (hash reference.)
    #  $errs_ref   - Error messages on failure (hash reference.)

    #
    # Must wrap the parser in eval since it exits on error.
    #
    my $xmlparse = eval { XMLin($xmlfile,
				VarAttr => 'name',
				ContentKey => '-content',
				SuppressEmpty => undef); };
    if ($@) {
	$errs_ref->{"XML Parse Error"} = "Return code $@";
	return;
    }

    #
    # Make sure all the required arguments were provided.
    #
    my $key;
    foreach $key (keys(%{ $fields_ref })) {
	my (undef, $required, undef) = @{$fields_ref->{$key}};

	$errs_ref->{$key} = "Required value not provided"
	    if ($required & $SLOT_REQUIRED  &&
		! exists($xmlparse->{'attribute'}->{"$key"}));
    }
    return
	if (keys(%{ $errs_ref }));

    foreach $key (keys(%{ $xmlparse->{'attribute'} })) {
	my $value = $xmlparse->{'attribute'}->{"$key"}->{'value'};
Russ Fish's avatar
Russ Fish committed
861 862 863
	if (!defined($value)) {	# Empty string comes from XML as an undef value.
	    $xmlparse->{'attribute'}->{"$key"}->{'value'} = $value = "";
	}
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 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912

	if ($debug) {
	    print STDERR "User attribute: '$key' -> '$value'\n";
	}

	$errs_ref->{$key} = "Unknown attribute"
	    if (!exists($fields_ref->{$key}));

	my ($dbslot, $required, $default) = @{$fields_ref->{$key}};

	if ($required & $SLOT_REQUIRED) {
	    # A slot that must be provided, so do not allow a null value.
	    if (!defined($value)) {
		$errs_ref->{$key} = "Must provide a non-null value";
		next;
	    }
	}
	if ($required & $SLOT_OPTIONAL) {
	    # Optional slot. If value is null skip it. Might not be the correct
	    # thing to do all the time?
	    if (!defined($value)) {
		next
		    if (!defined($default));
		$value = $default;
	    }
	}
	if ($required & $SLOT_ADMINONLY) {
	    # Admin implies optional, but thats probably not correct approach.
	    $errs_ref->{$key} = "Administrators only"
		if (! $this_user->IsAdmin());
	}

	# Now check that the value is legal.
	if (! TBcheck_dbslot($value, $table_name, $dbslot, 
			     TBDB_CHECKDBSLOT_ERROR)) {
	    $errs_ref->{$key} = TBFieldErrorString();
	    next;
	}

	$args_ref->{$key} = $value;
    }
}

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

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