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

#
# EMULAB-COPYRIGHT
5
# Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
6 7 8
# All rights reserved.
#

9 10
use English;
use Getopt::Std;
11

12 13 14 15 16
#
# Change delay params for a link.
#
sub usage()
{
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
    print(STDERR
	  "Usage: delay_config [-m] [-d] [-s vnode] <pid> <eid> <link>".
	  " PARAM=VALUE ...\n".
	  "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".
	  "        This is easier than using the -p option!\n".
	  "   -m = Modify the base experiment in addition to current state.\n".
	  "Parameters:\n".
	  " BANDWIDTH=NNN    - N=bandwidth (10-100000 Kbits per second)\n",
	  " 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".
	  " THRESH=NNN       - Maximum threshold for the average queue size\n".
	  " LINTERM=NNN      - Packet dropping probability\n".
	  " Q_WEIGHT=NNN     - For calculating the average queue size\n");
41 42
    # Web interface cares about this return value!
    exit(2);
43
}
44
my  $optlist = "dms:";
45 46 47 48 49 50

#
# Configure variables
#
my $TB		= "@prefix@";
my $TBOPS       = "@TBOPSEMAIL@";
51
my $TEVC        = "$TB/bin/tevc";
52 53 54 55 56
my $debug	= 0;

#
# Testbed Support libraries
#
57 58
use lib "@prefix@/lib";
use libdb;
59
use libtestbed;
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
#
# 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.
#
76 77 78
if (@ARGV < 4) {
    usage();
}
79 80 81 82
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
83
if (defined($options{"d"})) {
84 85 86
    $debug = 1;
}

87 88 89
my $pid    = shift(@ARGV);
my $eid    = shift(@ARGV);
my $link   = shift(@ARGV);
90
my $modify = 0;
91 92 93
my $srcvnode;
my %config;
my $pipeno;
94 95
my $pipe;

96 97 98
if (defined($options{"m"})) {
    $modify = 1;
}
99 100 101
if (defined($options{"s"})) {
    $srcvnode = $options{"s"};
}
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 128 129 130 131
#
# 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");
}

#
# No transitional experiments.
#
my $query_result =
    DBQueryFatal("select state,expt_locked from experiments ".
		 "where eid='$eid' and pid='$pid'");

if (! $query_result->numrows) {
    die("*** $0:\n".
	"    No such experiment $pid/$eid exists!\n");
}
my ($estate,$expt_locked) = $query_result->fetchrow_array();

if (defined($expt_locked)) {
    die("*** $0:\n".
	"    Experiment $pid/$eid went into transition at $expt_locked.\n".
	"    You will be notified via email when experiment transition ".
	"    is complete and you can alter its delay configuration.\n");
}

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

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    #
    # 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 {
	    if ($1 > 100000 || $1 < 10) {
		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 {
162
	    if ($1 < 0 || $1 > 1) {
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
		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";
196 197
	usage();
    }
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
#
# More sanity checks.
#
foreach my $key (keys(%config)) {
    my $val = $config{$key};

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

#
# These map the names I'm using in the config hash (which correspond
# 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
    );

228
#
229 230 231 232 233 234
# Link or Lan!
# 
$query_result =
    DBQueryFatal("select member from virt_lans ".
		 "where pid='$pid' and eid='$eid' and vname='$link'");
if (!$query_result->numrows) {
235
    die("*** $0:\n".
236
	"    $link is not a link in $pid/$eid!\n");
237
}
238
my $islink = ($query_result->numrows == 2 ? 1 : 0);
239

240
#
241
# If experiment is not active, all we can do is change virt_lans.
242
#
243 244 245 246 247 248 249 250 251
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);
252
}
253

254 255 256 257 258 259
#
# Check to see if linkdelays or normal delay nodes. This is a gross
# complication of this entire script!
#
if (ChangeDelayConfig() < 0 &&
    ChangeLinkDelayConfig() < 0) {
260
    die("*** $0:\n".
261
	"    $link is not a traffic shaped link in $pid/$eid!\n");
262
}
263 264 265
if ($modify) {
    ChangeVirtLans();
}
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
#
# 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};
    
    $inject_string .= "${str}=$val ";
}
if ($debug) {
    print "$inject_string\n";
}

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

295
#
296 297 298 299 300
# Get current delay configuration. 
# 
sub ChangeDelayConfig() {
    my $query_clause;
    my $query_string;
301

302 303
    $query_string = "select * from delays ".
	"where pid='$pid' and eid='$eid' and vname='$link' ";
304

305 306
    $query_string .= "and (vnode0='$srcvnode' or vnode1='$srcvnode')"
	if (defined($srcvnode));
307

308 309 310 311 312 313
    #
    # Okay, see if there is a delay node.
    # 
    my $query_result = DBQueryFatal($query_string);
    if (! $query_result->numrows) {
	return -1;
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
    
    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();

	if ($row{'vnode0'} eq $srcvnode) {
	    $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";
	    }
	}
350
    }
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    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 {
		$query_clause .= "${key}0=$val,${key}1=$val";
	    }
	}
371
    }
372 373 374 375 376 377 378 379 380 381
    
    #
    # 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')";
382 383
    }
    if ($debug) {
384
	print "$query_string\n";
385
    }
386 387 388
    DBQueryFatal($query_string);
    return 0;
}    
389

390 391 392 393 394 395
#
# Change linkdelays.
# 
sub ChangeLinkDelayConfig() {
    my @query_clauses;
    my $query_string;
396

397 398
    $query_string = "select * from linkdelays ".
	"where pid='$pid' and eid='$eid' and vlan='$link' ";
399

400 401
    $query_string .= "and vnode='$srcvnode'"
	if (defined($srcvnode));
402

403 404 405 406 407 408
    #
    # Okay, see if there is a linkdelay.
    # 
    my $query_result = DBQueryFatal($query_string);
    if (! $query_result->numrows) {
	return -1;
409
    }
410 411

    #
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
    # 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};

	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");
	}
    }
    
432
    #
433
    # Update the delays table.
434
    #
435 436 437
    $query_string = "update linkdelays set ".
	join(",", @query_clauses) . 
	" where pid='$pid' and eid='$eid' and vlan='$link' ";
438
    
439 440 441 442 443 444 445 446
    if (defined($srcvnode)) {
	$query_string .= "and vnode='$srcvnode'";
    }
    if ($debug) {
	print "$query_string\n";
    }
    DBQueryFatal($query_string);
    return 0;
447 448 449
}

#
450
# Change the virt_lans entry.
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 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
sub ChangeVirtLans() {
    my @query_clauses;
    my $query_string;
    my @query_clauses2;
    
    #
    # 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.
    #
    if (!defined($srcvnode) || !$islink) {
	foreach my $key (keys(%config)) {
	    my $val = $config{$key};

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

		# This follows what is done in the parser.
		$delay = $delay / 2.0
		    if ($islink);

		push(@query_clauses, "delay=$delay");
		push(@query_clauses, "rdelay=$delay");
	    }
	    elsif ($key eq "lossrate") {
		my $lossrate = $val;

		# This follows what is done in the parser.
		$lossrate = 1-sqrt(1-$lossrate)
		    if ($islink);

		push(@query_clauses, "lossrate=$lossrate");
		push(@query_clauses, "rlossrate=$lossrate");
	    }
	    elsif ($key eq "bandwidth") {
		push(@query_clauses, "bandwidth=$val");
		push(@query_clauses, "rbandwidth=$val");
	    }
	    else {
		push(@query_clauses, "$key=$val");
	    }
	}

	$query_string = "update virt_lans set ".
	    join(",", @query_clauses) .
	    " 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}:%'";
	}

	if ($debug) {
	    print "$query_string\n";
	}
	DBQueryFatal($query_string);
    }
    else {
	#
	# Here we change a duplex link asymmetrically. 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. 
	# 
	foreach my $key (keys(%config)) {
	    my $val = $config{$key};

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

		push(@query_clauses,  "delay=$delay");
		push(@query_clauses2, "rdelay=$delay");
	    }
	    elsif ($key eq "lossrate") {
		my $lossrate = 1-sqrt(1-$val);

		push(@query_clauses, "lossrate=$lossrate");
		push(@query_clauses2, "rlossrate=$lossrate");
	    }
	    elsif ($key eq "bandwidth") {
		push(@query_clauses, "bandwidth=$val");
		push(@query_clauses2, "rbandwidth=$val");
	    }
	    else {
		push(@query_clauses, "$key=$val");
	    }
	}
	$query_string = "update virt_lans set ".
	    join(",", @query_clauses) .
	    " where pid='$pid' and eid='$eid' and ".
	    "      vname='$link' and member like '${srcvnode}:%'";
	
	if ($debug) {
	    print "$query_string\n";
	}
	DBQueryFatal($query_string);

	$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);
    }
}