delaysetup 33.7 KB
Newer Older
1
#!/usr/bin/perl -w
2
#
3
# Copyright (c) 2000-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 25 26 27
#
use English;
use Getopt::Std;

#
28 29 30
# Setup/Update the system with new delays. Use -i for install mode, which
# means to run the scripts so that the delays are installed. Use -u for
# uninstall mode, which really only makes sense when used in conjunction
31 32
# with -j. On a real delay node, no real need to do an uninstall; an install
# flushes the current rules for the entire node. 
33 34 35
#
sub usage()
{
Mike Hibler's avatar
Mike Hibler committed
36
    print "Usage: delaysetup [-i | -u | -r] [-j <vnodeid>]\n";
37 38
    exit(1);
}
Mike Hibler's avatar
Mike Hibler committed
39
my $optlist	= "iurj:";
40 41
my $install	= 0;
my $uninstall	= 0;
Mike Hibler's avatar
Mike Hibler committed
42
my $reinstall   = 0;
43
my $vnodeid;
44 45 46 47 48 49 50 51 52 53 54

# Drag in path stuff so we can find emulab stuff.
BEGIN { require "/etc/emulab/paths.pm"; import emulabpaths; }

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

#
# Load the OS independent support library. It will load the OS dependent
55 56
# library and initialize itself.
#
57
use libsetup;
58
use libtmcc;
59

60 61
#
# Must be root.
62
#
63 64 65 66 67
if ($EUID) {
    die("*** $0:\n".
	"    Must be run as root!\n");
}

68 69 70 71 72 73 74 75
#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
%options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
76 77 78
if (defined($options{"i"})) {
    $install = 1;
}
79
if (defined($options{"u"})) {
80 81
    $uninstall = 1;
}
Mike Hibler's avatar
Mike Hibler committed
82 83 84
if (defined($options{"r"})) {
    $reinstall = 1;
}
85 86 87
if (defined($options{"j"})) {
    $vnodeid = $options{"j"};
    libsetup_setvnodeid($vnodeid);
88 89
    # Tell tmcc library, although thats already been done with previous call.
    configtmcc("subnode", $vnodeid);
90 91 92 93
}
if (@ARGV) {
    usage();
}
Mike Hibler's avatar
Mike Hibler committed
94 95 96 97 98 99

# XXX not yet
if (INXENVM()) {
    exit(0);
}

100
sub checkkernel($$);
101

102 103
$FBSD_VERSION = `uname -v`;

104
if ($FBSD_VERSION =~ /FreeBSD ([0-9]+).*/) {
105 106 107 108 109 110 111
    $FBSD_VERSION = int($1);
}
else {
    die("*** $0:\n".
	"    Cannot determine version\n");
}

112 113
my $canbackfill = 0;

114 115
#
# Delay node configuration goop.
Mike Hibler's avatar
Mike Hibler committed
116
# First figure out our kernels.
117
#
118 119 120 121 122 123
my $KERNELDELAY;	# kernel for delay nodes
my $KERNELDELAYHZ;	# HZ for delay node kernel
my $KERNELLDELAY;	# kernel for linkdelays
my $KERNELLDELAYHZ;	# HZ for linkdelay kernel
my $KERNELJAIL;		# kernel for jails (implies linkdelays)
my $KERNELJAILHZ;	# HZ for jail kernel
Mike Hibler's avatar
Mike Hibler committed
124 125 126 127

if ($FBSD_VERSION <= 4) {
    if (-e "/kernel") {
	# newer 4.x setup
128 129 130
	$KERNELDELAY  = "/kernel.delay";
	$KERNELLDELAY = "/kernel.linkdelay";
	$KERNELJAIL   = "/kernel.jail";
Mike Hibler's avatar
Mike Hibler committed
131 132
    } else {
	# old 4.x setup
133 134 135
	$KERNELDELAY  = "/kernel.10000HZ";
	$KERNELLDELAY = "/kernel.1000HZ";
	$KERNELJAIL   = "/kernel.1000HZ";
Mike Hibler's avatar
Mike Hibler committed
136
    }
137
    $canbackfill = 1;
138 139 140
}
elsif ($FBSD_VERSION == 5) {
    # 5.x
Mike Hibler's avatar
Mike Hibler committed
141 142 143 144
    $KERNELDELAY = "/boot/kernel.delay/kernel";
    $KERNELJAIL  = "/boot/kernel.linkdelay/kernel";
    $KERNELLDELAY= "/boot/kernel.linkdelay/kernel";
}
145
elsif ($FBSD_VERSION < 8 && ($FBSD_VERSION != 7 || -d "/boot/kernel.poll")) {
146
    # for 6.x and 7.x, we use the same kernel for all.
147 148 149 150
    # XXX ok not quite yet til we characterize the effect of polling.
    $KERNELDELAY = "/boot/kernel.poll/kernel";
    $KERNELJAIL = $KERNELLDELAY = "/boot/kernel/kernel";
}
151 152 153 154
else {
    # for 8.x and above, we really do use the same kernel for all.
    $KERNELDELAY = $KERNELJAIL = $KERNELLDELAY = "/boot/kernel/kernel";
}
155 156 157 158 159

# for all kernels right now
$KERNELDELAYHZ  = 10000;
$KERNELLDELAYHZ = 1000;
$KERNELJAILHZ   = 1000;
160 161

# This should never happen!
162
if ((REMOTE() && !REMOTEDED()) || MFS()) {
163
    print "Skipping delay configuration on remote/MFS node!\n";
164 165 166
    exit(0);
}

167 168
print "Checking Testbed delay configuration ... \n";

169
# Uninstall just looks for the files, runs them, and exits.
Mike Hibler's avatar
Mike Hibler committed
170
if ($reinstall || $uninstall) {
171 172
    system(TMDELAY . " disable")
	if (-e TMDELAY);
173

174 175
    system(TMLINKDELAY . " disable")
	if (-e TMLINKDELAY);
Mike Hibler's avatar
Mike Hibler committed
176 177
    exit(0)
	if ($uninstall);
178 179
}

180 181 182 183 184 185 186 187 188 189 190
#
# Figure out the current clock rate
#
my $currenthz = 0;
if ($FBSD_VERSION >= 6) {
    $currenthz = `sysctl -n kern.clockrate 2>/dev/null`;
    if ($currenthz =~ /hz = (\d+),/) {
	$currenthz = int($1);
    }
}

191 192
#
# Update the delays configuration. Also run the the commands to make
193 194
# the changes. Since a node cannot be doing delays and linkdelays at the
# same time, try each and exit.
195
#
196 197 198
if (DelaySetup()) {
    exit(1);
}
199 200
if (-e TMDELAY) {
    system(TMDELAY . " enable")
201
	if ($install || $reinstall);
202 203 204
    exit(0);
}

205 206 207
if (LinkDelaySetup()) {
    exit(1);
}
208
if (-e TMLINKDELAY) {
209
    system(TMLINKDELAY . " enable")
210
	if ($install || $reinstall);
211
    exit(0);
212 213 214 215
}
exit(0);

#
216 217 218
# This sets up delays on a delay node.
#
sub DelaySetup
219 220 221
{
    my @delays;
    my $kernel;
222
    my $kernelhz;
223 224 225

    # Lets clean out old instructions.
    unlink TMDELAY;
226
    unlink TMDELMAP;
227

228 229 230
    if (tmcc(TMCCCMD_DELAY, undef, \@delays) < 0) {
	warn("*** WARNING: Could not get delays from server!\n");
	return -1;
231 232
    }

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    #
    # Determine the native speed of the interfaces. We use this to decide
    # whether we need to set the BW in dummynet.
    #
    my %nativebw = ();
    my @ifinfo;
    if (tmcc(TMCCCMD_IFC, undef, \@ifinfo) >= 0) {
	#
	# Don't need fancy parsing ala getifconfig, we just need the
	# mac/name/speed.
	#
	foreach my $str (@ifinfo) {
	    if ($str =~ /^INTERFACE .*MAC=(\w+) SPEED=(\w+)/) {
		my $ifname = findiface($1);
		my $speed = $2;
		if ($speed =~ /(\d+)(\D*)/) {
		    if ($2 eq "Kbps") {
			$nativebw{$ifname} = $1;
		    } elsif ($2 eq "Mbps") {
			$nativebw{$ifname} = $1 * 1000;
		    }
		}
	    }
	}
    } else {
	warn("*** WARNING: Could not get interface config from server!\n");
    }

    # If we have any 10Gb interfaces, we may need to tweak some settings
    my $using10g = 0;
    foreach my $nif (keys %nativebw) {
	if ($nativebw{$nif} >= 10000) {
	    $using10g = 1;
	    last;
	}
    }

270 271
    if (@delays) {
	my $mindelay = 10000;
272
	my @bridges  = ();
273 274 275 276 277 278 279 280 281 282 283 284

	# before going any further, see if we need to boot a new kernel
 	if (-e $KERNELDELAY) {
 	    $kernel = $KERNELDELAY;
 	    $kernelhz = $KERNELDELAYHZ;
 	} else {
 	    $kernel = $KERNELLDELAY;
 	    $kernelhz = $KERNELLDELAYHZ;
 	}
	if (checkkernel($kernel, $kernelhz)) {
	    return -1;
	}
285

286 287
	open(MAP, ">" . TMDELMAP)
	    or die("Could not open " . TMDELMAP . ": $!");
288

289 290
	open(DEL, ">" . TMDELAY)
	    or die("Could not open " . TMDELAY . ": $!");
291

292
	my $bix = 1;
293 294 295 296
	foreach $delay (@delays) {
	    $delay =~ /DELAY INT0=([\d\w]+) INT1=([\d\w]+) /;
	    my $iface1 = findiface($1);
	    my $iface2 = findiface($2);
297 298
	    push @bridges, [ $bix, $iface1, $iface2 ];
	    $bix++;
299 300
	}

301
	print DEL "#!/bin/sh\n";
302 303 304 305 306 307 308 309 310 311 312 313 314 315
	print DEL "# auto-generated by delaysetup.pm, DO NOT EDIT\n";
	print DEL "if [ x\$1 = x ]; then action=enable; else action=\$1; fi\n";

	#
	# Shutdown: undo only the stuff that might get redone during
	# a reconfig.
	#
	print DEL "if [ \"\$action\" = \"disable\" ]; then\n";

	# bridging
	if ($FBSD_VERSION <= 4) {
	    print DEL "    sysctl -w net.link.ether.bridge=0\n";
	    print DEL "    sysctl -w net.link.ether.bridge_ipfw=0\n";
	}
316
	elsif ($FBSD_VERSION <= 6) {
317 318 319
	    print DEL "    sysctl -w net.link.ether.bridge.enable=0\n";
	    print DEL "    sysctl -w net.link.ether.bridge.ipfw=0\n";
	}
320 321 322 323 324 325 326
	else {
	    foreach my $bridge (@bridges) {
		my ($bix) = @$bridge;
		print DEL "    ifconfig bridge$bix down destroy\n";
	    }
	    print DEL "    sysctl -w net.link.bridge.ipfw=0\n";
	    print DEL "    sysctl -w net.link.bridge.ipfw_arp=0\n";
327 328 329
	    if ($FBSD_VERSION >= 10) {
		print DEL "    sysctl -w net.link.ether.ipfw=0\n";
	    }
330
	}
331 332 333 334 335 336 337 338 339

	# ipfw pipes
	print DEL "    ipfw -f pipe flush\n";
	print DEL "    ipfw -f flush\n";
	if ($FBSD_VERSION >= 6) {
	    print DEL "    ipfw add 65534 allow all from any to any\n";
	}

	# polling
340 341 342 343 344
	if ($FBSD_VERSION >= 8) {
	    print DEL "    if [ -n \"`sysctl kern.polling.phase 2>/dev/null`\" ]; then\n";
	} else {
	    print DEL "    if [ -n \"`sysctl kern.polling.enable 2>/dev/null`\" ]; then\n";
	}
345
	if ($FBSD_VERSION >= 6) {
346 347 348 349
	    foreach my $bridge (@bridges) {
		my (undef,$if1,$if2) = @$bridge;
		print DEL "        ifconfig $if1 -polling\n";
		print DEL "        ifconfig $if2 -polling\n";
350
	    }
351 352
	} else {
	    print DEL "        sysctl -w kern.polling.enable=0\n";
353 354 355 356 357 358 359 360 361 362 363 364
	}
	print DEL "    fi\n";

	print DEL "    exit 0\n";
	print DEL "fi\n";

	#
	# For FreeBSD 6 and beyond we must configure the number of kernel
	# mbuf clusters via sysctl, there is no param in the kernel anymore.
	#
	# We also load the bridge, IPFW2 and dummynet code if necessary.
	#
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 394 395 396
	if ($FBSD_VERSION >= 7) {
	    my $MPATH = $KERNELDELAY;
	    $MPATH =~ s/\/[^\/]+$//;

	    print DEL "if [ `sysctl -n kern.ipc.nmbclusters` -lt 65536 ]; then\n";
	    print DEL "    sysctl -w kern.ipc.nmbclusters=65536\n";
	    print DEL "fi\n";
	    print DEL "if ! kldstat -q -m if_bridge >/dev/null 2>&1; then\n";
	    print DEL "    echo \"Loading bridge module...\"\n";
	    print DEL "    kldload $MPATH/if_bridge.ko || {\n";
	    print DEL "        echo '  *** Could not load bridge code'\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "fi\n";
	    print DEL "if ! kldstat -q -m ipfw >/dev/null 2>&1; then\n";
	    print DEL "    echo \"Loading ipfw module...\"\n";
	    print DEL "    kldload $MPATH/ipfw.ko || {\n";
	    print DEL "        echo '  *** Could not load ipfw code'\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "    ipfw add 65534 allow all from any to any\n";
	    print DEL "fi\n";

	    print DEL "if ! kldstat -q -m dummynet >/dev/null 2>&1; then\n";
	    print DEL "    echo \"Loading dummynet module...\"\n";
	    print DEL "    kldload $MPATH/dummynet.ko || {\n";
	    print DEL "        echo '  *** Could not load dummynet code'\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "fi\n";

	} elsif ($FBSD_VERSION >= 6) {
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 423 424 425 426
	    my $MPATH = $KERNELDELAY;
	    $MPATH =~ s/\/[^\/]+$//;

	    print DEL "if [ `sysctl -n kern.ipc.nmbclusters` -lt 65536 ]; then\n";
	    print DEL "    sysctl -w kern.ipc.nmbclusters=65536\n";
	    print DEL "fi\n";

	    print DEL "if ! sysctl net.link.ether.bridge >/dev/null 2>&1; then\n";
	    print DEL "    kldload $MPATH/bridge.ko || {\n";
	    print DEL "        echo \"Could not load bridge code\"\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "fi\n";

	    print DEL "if ! sysctl net.inet.ip.fw.enable >/dev/null 2>&1; then\n";
	    print DEL "    kldload $MPATH/ipfw.ko || {\n";
	    print DEL "        echo \"Could not load IPFW code\"\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "    ipfw add 65534 allow all from any to any\n";
	    print DEL "fi\n";

	    print DEL "if ! sysctl net.inet.ip.dummynet.curr_time >/dev/null 2>&1; then\n";
	    print DEL "    kldload $MPATH/dummynet.ko || {\n";
	    print DEL "        echo \"Could not load dummynet code\"\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "fi\n";
	}

427 428 429 430 431 432 433
	#
	# Starting with FreeBSD 7, we use the new if_bridge module
	#
	if ($FBSD_VERSION >= 7) {
	    # Turn these off before setting bridge_cfg.
	    print DEL "sysctl -w net.link.bridge.ipfw=0\n";
	    print DEL "sysctl -w net.link.bridge.ipfw_arp=0\n";
434 435 436
	    if ($FBSD_VERSION >= 10) {
		print DEL "sysctl -w net.link.ether.ipfw=0\n";
	    }
437 438 439 440 441 442 443 444 445 446 447 448 449
	    
	    foreach my $bridge (@bridges) {
		my ($bix,$if1,$if2) = @$bridge;
		print DEL "ifconfig bridge$bix create || {\n";
		print DEL "    echo \"Could not create bridge$bix\"\n";
		print DEL "    exit 1\n";
		print DEL "}\n";
		print DEL "ifconfig bridge$bix addm $if1 addm $if2 up || {\n";
		print DEL "    echo \"Could not add $if1/$if2 to bridge$bix\"\n";
		print DEL "    ifconfig bridge$bix down destroy\n";
		print DEL "    exit 1\n";
		print DEL "}\n";
	    }
450

451 452 453
	    # Now turn them back on.
	    print DEL "sysctl -w net.link.bridge.ipfw=1\n";
	    print DEL "sysctl -w net.link.bridge.ipfw_arp=1\n";
454 455 456
	    if ($FBSD_VERSION >= 10) {
		print DEL "sysctl -w net.link.ether.ipfw=1\n";
	    }
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
	} else {
	    # Turn these off before setting bridge_cfg.
	    if ($FBSD_VERSION <= 4) {
		print DEL "sysctl -w net.link.ether.bridge=0\n";
		print DEL "sysctl -w net.link.ether.bridge_ipfw=0\n";
		print DEL "sysctl -w net.link.ether.bridge_cfg=";
	    }
	    else {
		print DEL "sysctl -w net.link.ether.bridge.enable=0\n";
		print DEL "sysctl -w net.link.ether.bridge.ipfw=0\n";
		print DEL "sysctl -w net.link.ether.bridge.config=";
	    }
	    foreach my $bridge (@bridges) {
		my ($count,$iface1,$iface2) = @$bridge;
		$count += 68;	# XXX backward compat
472

473 474 475 476 477 478 479 480 481 482 483 484
		print DEL "$iface1:$count,$iface2:$count,";
	    }
	    print DEL "\n";
	    # Now turn them back on.
	    if ($FBSD_VERSION <= 4) {
		print DEL "sysctl -w net.link.ether.bridge=1\n";
		print DEL "sysctl -w net.link.ether.bridge_ipfw=1\n";
	    }
	    else {
		print DEL "sysctl -w net.link.ether.bridge.enable=1\n";
		print DEL "sysctl -w net.link.ether.bridge.ipfw=1\n";
	    }
485
	}
486
	print DEL "ipfw -f pipe flush\n";
487
	print DEL "ipfw -f flush\n";
488 489 490
	if ($FBSD_VERSION >= 6) {
	    print DEL "ipfw add 65534 allow all from any to any\n";
	}
491 492

 	#
493 494 495 496
	# Turn on polling if possible.
	# For FreeBSD 6 and beyond, we turn it on only for the interfaces
	# in question.  For older FreeBSDs, we just have to turn it on
	# globally.
497
 	#
498
	if ($FBSD_VERSION >= 8) {
499 500 501

	    # XXX just in case we don't support polling
	    if ($using10g) {
Mike Hibler's avatar
Mike Hibler committed
502
		print DEL "sysctl hw.intr_storm_threshold=0\n";
503 504
	    }

Mike Hibler's avatar
Mike Hibler committed
505
	    print DEL "if [ -n \"`sysctl kern.polling.phase 2>/dev/null`\" ]; then\n";
506 507 508
	} else {
	    print DEL "if [ -n \"`sysctl kern.polling.enable 2>/dev/null`\" ]; then\n";
	}
509
	if ($FBSD_VERSION >= 6) {
510 511 512
	    if ($FBSD_VERSION < 8) {
		print DEL "    sysctl -w kern.polling.enable=0\n";
	    }
513 514 515 516
	    foreach my $bridge (@bridges) {
		my (undef,$if1,$if2) = @$bridge;
		print DEL "    ifconfig $if1 polling\n";
		print DEL "    ifconfig $if2 polling\n";
517
	    }
518 519 520 521 522 523 524 525 526 527 528 529 530 531

	    #
	    # Our kernel is running hot (10000HZ) so adjust some params.
	    # XXX this check should be done in the generated shell script,
	    # but I am too lazy to parse sysctl output with awk/sed and it
	    # doesn't matter right now as we always rerun this perl wrapper
	    # to produce the shell script.
	    #
	    if ($currenthz > 1000) {
		print DEL "    # adjustments for kernel at $currenthz HZ\n";
		# limit register accesses to 50 times a second
		print DEL "    sysctl -w kern.polling.reg_frac=200\n";

		# 80% of CPU can be used for polling activities
532
		print DEL "    sysctl -w kern.polling.user_frac=20\n";
533
	    }
534 535 536 537 538
	} else {
	    print DEL "    sysctl -w kern.polling.enable=1\n";
	}
	print DEL "else\n";
	print DEL "    echo '*** WARNING: no DEVICE_POLLING in delay kernel!'\n";
Mike Hibler's avatar
Mike Hibler committed
539
	print DEL "fi\n";
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

	foreach $delay (@delays) {
	    my $pat  = q(DELAY INT0=([\d\w]+) INT1=([\d\w]+) );
	    $pat .= q(PIPE0=(\d+) DELAY0=([\d\.]+) BW0=(\d+) PLR0=([\d\.]+) );
	    $pat .= q(PIPE1=(\d+) DELAY1=([\d\.]+) BW1=(\d+) PLR1=([\d\.]+) );
	    $pat .= q(LINKNAME=([-\d\w]+) );
	    $pat .= q(RED0=(\d) RED1=(\d) );
	    $pat .= q(LIMIT0=(\d+) );
	    $pat .= q(MAXTHRESH0=(\d+) MINTHRESH0=(\d+) WEIGHT0=([\d\.]+) );
	    $pat .= q(LINTERM0=(\d+) QINBYTES0=(\d+) BYTES0=(\d+) );
	    $pat .= q(MEANPSIZE0=(\d+) WAIT0=(\d+) SETBIT0=(\d+) );
	    $pat .= q(DROPTAIL0=(\d+) GENTLE0=(\d+) );
	    $pat .= q(LIMIT1=(\d+) );
	    $pat .= q(MAXTHRESH1=(\d+) MINTHRESH1=(\d+) WEIGHT1=([\d\.]+) );
	    $pat .= q(LINTERM1=(\d+) QINBYTES1=(\d+) BYTES1=(\d+) );
	    $pat .= q(MEANPSIZE1=(\d+) WAIT1=(\d+) SETBIT1=(\d+) );
	    $pat .= q(DROPTAIL1=(\d+) GENTLE1=(\d+) );
557
	    $pat .= q(VNODE0=([-\d\w]+) VNODE1=([-\d\w]+) );
558 559
	    $pat .= q(NOSHAPING=(\d+) );
	    $pat .= q(BACKFILL0=(\d+) BACKFILL1=(\d+));
560

561 562 563 564
	    $delay =~ /$pat/;

	    #
	    # tmcd returns the interfaces as MAC addrs.
565
	    #
566 567 568 569 570 571 572 573 574 575 576 577 578
	    my $iface1 = findiface($1);
	    my $iface2 = findiface($2);
	    my $p1        = $3;
	    my $delay1    = $4;
	    my $bandw1    = $5;
	    my $plr1      = $6;
	    my $p2        = $7;
	    my $delay2    = $8;
	    my $bandw2    = $9;
	    my $plr2      = $10;
	    my $linkname  = $11;
	    my $red1      = $12;
	    my $red2      = $13;
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
	    #
	    # Only a few of these NS RED params make sense for dummynet,
	    # but they all come through; someday they might be used.
	    #
	    my $limit1     = $14;
	    my $maxthresh1 = $15;
	    my $minthresh1 = $16;
	    my $weight1    = $17;
	    my $linterm1   = $18;
	    my $qinbytes1  = $19;
	    my $bytes1     = $20;
	    my $meanpsize1 = $21;
	    my $wait1      = $22;
	    my $setbit1    = $23;
	    my $droptail1  = $24;
	    my $gentle1    = $25;
	    my $limit2     = $26;
	    my $maxthresh2 = $27;
	    my $minthresh2 = $28;
	    my $weight2    = $29;
	    my $linterm2   = $30;
	    my $qinbytes2  = $31;
	    my $bytes2     = $32;
	    my $meanpsize2 = $33;
	    my $wait2      = $34;
	    my $setbit2    = $35;
	    my $droptail2  = $36;
	    my $gentle2    = $37;
	    my $vnode0     = $38;
	    my $vnode1     = $39;
610 611
	    my $noshaping  = $40;

612 613 614
	    my $backfill1  = $41;
	    my $backfill2  = $42;

615 616 617 618 619 620 621 622 623 624 625
	    #
	    # Noshaping. All we wanted was to set up the bridge (above).
	    # We do not want to actually delay the packets via a pipe,
	    # and we do not want a queuing!
	    #
	    next
		if ($noshaping);

	    #
	    # Set backfill if specified.
	    #
626 627 628 629 630 631 632 633 634 635 636
	    my $bf1arg = $bf2arg = "";
	    if ($backfill1 > 0) {
		$bf1arg = "backfill ${backfill1}Kbits/s";
	    }
	    if ($backfill2 > 0) {
		$bf2arg = "backfill ${backfill2}Kbits/s";
	    }
	    if (!$canbackfill && ($bf1arg || $bf2arg)) {
		print STDOUT "*** WARNING: IPFW does not support backfill\n";
	    }

637
	    #
638 639
	    # If the specified BW matches the native speed of the
	    # receive interface, leave the pipe bw arg as unlimited.
640
	    #
641 642 643 644 645 646 647
	    my $bw1arg = $bw2arg = "";
	    if (!exists($nativebw{$iface1}) || $bandw1 != $nativebw{$iface1}) {
		$bw1arg = "bw ${bandw1}Kbit/s";
	    }
	    if (!exists($nativebw{$iface2}) || $bandw2 != $nativebw{$iface2}) {
		$bw2arg = "bw ${bandw2}Kbit/s";
	    }
648

649 650 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 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
	    #
	    # Delays are floating point numbers (unit is ms). ipfw does not
	    # support floats, so apply a cheesy rounding function to convert
            # to an integer (since perl does not have a builtin way to
	    # properly round a floating point number to an integer).
	    #
	    $delay1 = int($delay1 + 0.5);
	    $delay2 = int($delay2 + 0.5);

	    #
	    # Qsizes are in slots or packets. My perusal of the 4.3 code
	    # shows the limits are 50 < slots <= 100 or 0 <= bytes <= 1MB.
	    #
	    my $queue1 = "";
	    my $queue2 = "";
	    if ($qinbytes1) {
		if ($limit1 <= 0 || $limit1 > (1024 * 1024)) {
		    print "Q limit $limit1 for pipe $p1 is bogus.\n";
		}
		else {
		    $queue1 = "queue ${limit1}bytes";
		}
	    }
	    elsif ($limit1 != 0) {
		if ($limit1 < 0 || $limit1 > 100) {
		    print "Q limit $limit1 for pipe $p1 is bogus.\n";
		}
		else {
		    $queue1 = "queue $limit1";
		}
	    }
	    if ($qinbytes2) {
		if ($limit2 <= 0 || $limit2 > (1024 * 1024)) {
		    print "Q limit $limit2 for pipe $p2 is bogus.\n";
		}
		else {
		    $queue2 = "queue ${limit2}bytes";
		}
	    }
	    elsif ($limit2 != 0) {
		if ($limit2 < 0 || $limit2 > 100) {
		    print "Q limit $limit2 for pipe $p2 is bogus.\n";
		}
		else {
		    $queue2 = "queue $limit2";
		}
	    }

	    #
	    # RED/GRED stuff
	    #
	    my $redparams1 = "";
	    my $redparams2 = "";
	    if ($red1) {
		if ($gentle1) {
		    $redparams1 = "gred ";
		}
		else {
		    $redparams1 = "red ";
		}
		my $max_p = 1 / $linterm1;
		$redparams1 .= "$weight1/$minthresh1/$maxthresh1/$max_p";
	    }
	    if ($red2) {
		if ($gentle2) {
		    $redparams2 = "gred ";
		}
		else {
		    $redparams2 = "red ";
		}
		my $max_p = 1 / $linterm2;
		$redparams2 .= "$weight2/$minthresh2/$maxthresh2/$max_p";
	    }

Mike Hibler's avatar
Mike Hibler committed
723 724 725 726 727 728 729 730 731 732 733 734
	    #
	    # To allow addition of more specific, per-interface rules
	    # (e.g., for pelab) we put the general rules toward the
	    # end of the table.  We don't know what the max general rule
	    # fed to us out of the DB will be, but lets assume that starting
	    # at 60000 provides us with enough room for those.
	    #
	    $p1 += 60000
		if ($p1 + 60000 < 65535);
	    $p2 += 60000
		if ($p2 + 60000 < 65535);

735 736 737 738 739 740 741 742 743 744 745 746
	    #
	    # As of FreeBSD 7, when filtering at the bridge level, packets are
	    # only processed by IPFW at the output interface.  Thus we must
	    # use "out" rather than "in" or packets won't match.
	    #
	    if ($FBSD_VERSION < 7) {
		print DEL "ipfw add $p1 pipe $p1 ip from any to any in recv $iface1\n";
		print DEL "ipfw add $p2 pipe $p2 ip from any to any in recv $iface2\n";
	    } else {
		print DEL "ipfw add $p1 pipe $p1 ip from any to any out recv $iface1\n";
		print DEL "ipfw add $p2 pipe $p2 ip from any to any out recv $iface2\n";
	    }
747
	    print DEL "ipfw pipe $p1 config delay ${delay1}ms ";
748
	    print DEL "$bw1arg $bf1arg plr $plr1 $queue1 $redparams1\n";
749
	    print DEL "ipfw pipe $p2 config delay ${delay2}ms ";
750
	    print DEL "$bw2arg $bf2arg plr $plr2 $queue2 $redparams2\n";
751 752

	    print STDOUT "  $iface1/$iface2 pipe $p1 config delay ";
753
	    print STDOUT "${delay1}ms $bw1arg plr $plr1 ";
754 755
	    print STDOUT "$queue1 $redparams1\n";
	    print STDOUT "  $iface1/$iface2 pipe $p2 config delay ";
756
	    print STDOUT "${delay2}ms $bw2arg plr $plr2 ";
757 758 759 760 761 762 763 764 765 766 767 768
	    print STDOUT "$queue2 $redparams2\n";

	    print MAP "$linkname duplex $vnode0 $vnode1 ".
		"$iface1 $iface2 $p1 $p2\n";
	}
	#
	# If a delay node, then lets report status and ready in so that batch
	# experiments do not become stuck.
	#
	printf DEL "%s %s 0\n", TMCC(), TMCCCMD_STARTSTAT();

	print DEL "echo \"Delay Configuration Complete\"\n";
769
	print DEL "exit 0\n";
770 771 772
	close(DEL);
	chmod(0755, TMDELAY);
	close(MAP);
773 774 775 776
	
	# Touch this file so that we globally know that the node is a delay
	# node. See routing setup. Need to avoid fastforwarding whenever we
	# have link delays.
777
	system("touch " . ISDELAYNODEPATH());
778 779 780 781 782 783
    }
    return 0;
}

#
# This sets up linkdelays on an experimental node.
784 785
#
sub LinkDelaySetup
786 787
{
    my @delays;
788
    my @jails;
789
    my $kernel;
790
    my $gotjails = (defined($vnodeid) ? 1 : 0);
791 792
    my $upcmds   = "";
    my $downcmds = "";
793 794 795

    # Lets clean out old instructions.
    unlink TMLINKDELAY;
796
    unlink TMDELMAP;
797

798 799
    #
    # We need to know if any jailed nodes. That changes which kernel
800 801 802
    # we want to boot.
    #
    # XXX Temporary until the jail stuff is better tested.
803
    #
804 805 806 807 808 809 810 811 812 813
    if (!$gotjails) {
	if (tmcc(TMCCCMD_VNODELIST, undef, \@jails) < 0) {
	    warn("*** WARNING: Could not get jails from server!\n");
	    return -1;
	}
	foreach my $str (@jails) {
	    if ($str =~ /^VNODEID=([-\w]+) JAILED=(\d)$/) {
		if ($2) {
		    $gotjails++;
		}
814 815 816 817 818
	    }
	}
    }
    if ($gotjails) {
	$kernel = $KERNELJAIL;
819 820 821 822
	$kernelhz = $KERNELJAILHZ;
    } else {
	$kernel = $KERNELLDELAY;
	$kernelhz = $KERNELLDELAYHZ;
823 824
    }

825 826 827 828
    # Get delay config.
    if (tmcc(TMCCCMD_LINKDELAYS, undef, \@delays) < 0) {
	warn("*** WARNING: Could not get link delays from server!\n");
	return -1;
829 830 831
    }

    if (@delays) {
832 833 834 835 836
	# before going any further, see if we need to boot a new kernel
	if (checkkernel($kernel, $kernelhz)) {
	    return -1;
	}

837 838
	open(MAP, ">" . TMDELMAP)
	    or die("Could not open " . TMDELMAP . ": $!");
839

840 841
	open(DEL, ">" . TMLINKDELAY)
	    or die("Could not open " . TMLINKDELAY . ": $!");
842

843 844 845 846 847 848 849 850 851 852 853
	foreach $delay (@delays) {
	    my $pat = q(LINKDELAY IFACE=([\d\w]+) TYPE=(simplex|duplex) );
	    $pat .= q(LINKNAME=([-\d\w]+) VNODE=([-\d\w]+) );
	    $pat .= q(INET=([0-9.]*) MASK=([0-9.]*) );
	    $pat .= q(PIPE=(\d+) DELAY=([\d\.]+) BW=(\d+) PLR=([\d\.]+) );
	    $pat .= q(RPIPE=(\d+) RDELAY=([\d\.]+) RBW=(\d+) RPLR=([\d\.]+) );
	    $pat .= q(RED=(\d) LIMIT=(\d+) );
	    $pat .= q(MAXTHRESH=(\d+) MINTHRESH=(\d+) WEIGHT=([\d\.]+) );
	    $pat .= q(LINTERM=(\d+) QINBYTES=(\d+) BYTES=(\d+) );
	    $pat .= q(MEANPSIZE=(\d+) WAIT=(\d+) SETBIT=(\d+) );
	    $pat .= q(DROPTAIL=(\d+) GENTLE=(\d+));
854

855 856 857 858
	    $delay =~ /$pat/;

	    #
	    # tmcd returns the interfaces as MAC addrs.
859
	    #
860
	    my $iface     = $1;
861 862 863 864 865 866 867 868 869 870 871 872 873 874
	    my $type      = $2;
	    my $linkname  = $3;
	    my $vnode     = $4;
	    my $inet      = $5;
	    my $mask      = $6;
	    my $pipeno    = $7;
	    my $delay     = $8;
	    my $bandw     = $9;
	    my $plr       = $10;
	    my $rpipeno   = $11;
	    my $rdelay    = $12;
	    my $rbandw    = $13;
	    my $rplr      = $14;
	    my $red       = $15;
875

876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	    #
	    # Only a few of these NS RED params make sense for dummynet,
	    # but they all come through; someday they might be used.
	    #
	    my $limit     = $16;
	    my $maxthresh = $17;
	    my $minthresh = $18;
	    my $weight    = $19;
	    my $linterm   = $20;
	    my $qinbytes  = $21;
	    my $bytes     = $22;
	    my $meanpsize = $23;
	    my $wait      = $24;
	    my $setbit    = $25;
	    my $droptail  = $26;
	    my $gentle    = $27;

893 894 895 896 897 898
	    #
	    # tmcd returns the interfaces as MAC addrs, so convert to
	    # an interface name.  Note that we also use the given IP
	    # address which is the unique characteristic for some forms
	    # of virtual interface.
	    # 
899 900 901 902
	    if (!($iface = findiface($iface,$inet))) {
		warn("*** WARNING: Could not map $1/$inet to an interface!\n");
		return -1;
	    }
903

904 905 906 907 908 909 910 911 912 913 914 915 916 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 945 946 947 948 949
	    #
	    # Delays are floating point numbers (unit is ms). ipfw does not
	    # support floats, so apply a cheesy rounding function to convert
            # to an integer (since perl does not have a builtin way to
	    # properly round a floating point number to an integer).
	    #
	    $delay  = int($delay + 0.5);
	    $rdelay = int($rdelay + 0.5);

	    #
	    # Qsizes are in slots or packets. My perusal of the 4.3 code
	    # shows the limits are 50 < slots <= 100 or 0 <= bytes <= 1MB.
	    #
	    my $queue = "";
	    if ($qinbytes) {
		if ($limit <= 0 || $limit > (1024 * 1024)) {
		    print "Q limit $limit for pipe $pipeno is bogus.\n";
		}
		else {
		    $queue = "queue ${limit}bytes";
		}
	    }
	    elsif ($limit != 0) {
		if ($limit < 0 || $limit > 100) {
		    print "Q limit $limit for pipe $pipeno is bogus.\n";
		}
		else {
		    $queue = "queue $limit";
		}
	    }

	    #
	    # RED/GRED stuff
	    #
	    my $redparams = "";
	    if ($red) {
		if ($gentle) {
		    $redparams = "gred ";
		}
		else {
		    $redparams = "red ";
		}
		my $max_p = 1 / $linterm;
		$redparams .= "$weight/$minthresh/$maxthresh/$max_p";
	    }

950 951 952 953 954 955 956 957 958
	    # rc.ifconfig (through the generated rc.ifc) does this
	    ##
	    ## XXX don't try to set the speed/duplex of virtual devices
	    ## XXX we should not be hardwiring the speed/duplex here anyway
	    ##
	    #if ($iface !~ /^(vlan|veth)/) {
	    #    $upcmds .= "ifconfig $iface media 100baseTX ".
	    #               "mediaopt full-duplex\n    ";
	    #}
959

960
	    $upcmds .= "ipfw add $pipeno pipe $pipeno ip from any to ".
961
		       "any out xmit $iface\n    ";
962

963
	    $upcmds .= "ipfw pipe $pipeno config delay ${delay}ms " .
964
	               "bw ${bandw}Kbit/s plr $plr $queue $redparams\n    ";
965

966 967 968 969 970
	    # not needed due to flush commands output below
	    if (defined($vnodeid)) {
		$downcmds .= "ipfw pipe delete $pipeno\n    ";
		$downcmds .= "ipfw delete $pipeno\n    ";
	    }
971

972
	    if ($type eq "duplex") {
973 974
		#
		# Want to force the reverse side to 1 queue slot to enforce
975
		# the proper bandwidth. Not ideal, especially since at 1000HZ
976
		# 1 queue slot is not enough. Make it 4 instead.
977 978
		#
		$upcmds .= "ipfw add $rpipeno pipe $rpipeno ip from any ".
979
		           "to any in recv $iface\n    ";
980

981
		$upcmds .= "ipfw pipe $rpipeno config delay ${rdelay}ms ".
982
		           "bw ${rbandw}Kbit/s plr $rplr queue 5\n    ";
983

984 985 986 987 988
		# not needed due to flush commands output below
		if (defined($vnodeid)) {
		    $downcmds .= "ipfw pipe delete $rpipeno\n    ";
		    $downcmds .= "ipfw delete $rpipeno\n    ";
		}
989 990 991 992 993 994 995 996 997 998
	    }

	    if ($type eq "duplex") {
		print MAP "$linkname duplex $vnode $vnode $iface $iface ".
		    "$pipeno $rpipeno\n";
	    }
	    else {
		print MAP "$linkname simplex $vnode $iface $pipeno\n";
	    }
	}
999 1000 1001
	close(MAP);

	print DEL "#!/bin/sh\n";
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	print DEL "# auto-generated by delaysetup.pm, DO NOT EDIT\n";

	#
	# For FreeBSD 6 and beyond we must configure the number of kernel
	# mbuf clusters via sysctl, there is no param in the kernel anymore.
	#
	# We also load the IPFW2 and dummynet code if necessary.
	#
	if ($FBSD_VERSION >= 6) {
	    my $MPATH = $KERNELLDELAY;
	    $MPATH =~ s/\/[^\/]+$//;

	    print DEL "if [ `sysctl -n kern.ipc.nmbclusters` -lt 65536 ]; then\n";
	    print DEL "    sysctl -w kern.ipc.nmbclusters=65536\n";
	    print DEL "fi\n";

	    print DEL "if ! sysctl net.inet.ip.fw.enable >/dev/null 2>&1; then\n";
	    print DEL "    kldload $MPATH/ipfw.ko || {\n";
	    print DEL "        echo \"Could not load IPFW code\"\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "    ipfw add 65534 allow all from any to any\n";
	    print DEL "fi\n";

	    print DEL "if ! sysctl net.inet.ip.dummynet.curr_time >/dev/null 2>&1; then\n";
	    print DEL "    kldload $MPATH/dummynet.ko || {\n";
	    print DEL "        echo \"Could not load dummynet code\"\n";
	    print DEL "        exit 1\n";
	    print DEL "    }\n";
	    print DEL "fi\n";
	}

1034
	# Main boot only.
1035
	if (!defined($vnodeid)) {
1036
	    print DEL "ipfw -f pipe flush\n";
1037
	    print DEL "ipfw -f flush\n";
1038 1039 1040
	    if ($FBSD_VERSION >= 6) {
		print DEL "ipfw add 65534 allow all from any to any\n";
	    }
1041 1042 1043 1044
	}
	print DEL "if [ x\$1 = x ]; then action=enable; else action=\$1; fi\n";
	print DEL "case \"\$action\" in\n";
	print DEL "  enable)\n";
1045
	print DEL "    $upcmds\n";
1046
	# Turn off Flow-based IP (fast) forwarding since it causes packets
1047
	# to bypass traffic shaping and ipfw. Stupid!
1048 1049
	print DEL "    sysctl -w net.inet.ip.fastforwarding=0\n";
	print DEL "    ;;\n";
1050
	print DEL "  disable)\n";
1051 1052
	print DEL "    $downcmds\n";
	print DEL "    ;;\n";
1053
	print DEL "esac\n";
1054 1055
	close(DEL);
	chmod(0755, TMLINKDELAY);
1056

1057 1058 1059
	# Touch this file so that we globally know that the node is a delay
	# node. See routing setup. Need to avoid fastforwarding whenever we
	# have link delays.
1060
	system("touch " . ISDELAYNODEPATH());
1061
    }
1062 1063
    return 0;
}
1064

1065 1066
#
# Check kernel config, and reboot.
1067
# Returns 0 if kernel is already ok, non-zero if kernel could not be fixed.
1068
#
1069
sub checkkernel($$)
1070
{
1071 1072 1073
    my ($kernel,$kernelhz) = @_;
    my $needreboot = 0;
    my $failed = 0;
1074

1075
    print STDOUT "Making sure node is running $kernel at ${kernelhz}HZ ...\n";
1076

1077 1078 1079 1080 1081 1082 1083 1084
    #
    # See if kernel is running at the correct HZ.
    # Update the loader.conf file and reboot if not.
    #
    # XXX we only do this for FreeBSD 6 and above since before since,
    # before that, we had special kernels with hardwired HZ for delay nodes.
    #
    if ($FBSD_VERSION >= 6) {
1085 1086 1087
	if ($currenthz > 0) {
	    if ($currenthz != $kernelhz) {
		print STDOUT "Updating kernel HZ ($currenthz -> $kernelhz) ...\n";
Mike Hibler's avatar
Mike Hibler committed
1088 1089 1090 1091 1092
		#
		# See if there is a custom Emulab command line setting.
		# If so, warn and do not change.
		#
		my $binfo = `$BINDIR/tmcc bootwhat`;
1093
		if ($binfo =~ /HZ=(\d+)/ && int($1) == $currenthz) {
Mike Hibler's avatar
Mike Hibler committed
1094 1095 1096 1097
		    print "*** WARNING: custom HZ value on command line, ".
			  "not changing to $kernelhz.\n";
		    return 0;
		}
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
		if (-e "/boot/loader.conf") {
		    #
		    # If already running a custom HZ rate, don't change it.
		    # Just warn and continue.
		    #
		    if (!system("grep '^kern.hz=' /boot/loader.conf 2>&1 >/dev/null")) {
			print "*** WARNING: kernel running custom HZ value, ".
			      "not changing to $kernelhz.\n";
			return 0;
		    }
		    #
		    # Otherwise back up the existing config before changing
		    #
		    if (system("cp -fp /boot/loader.conf /boot/loader.conf.bak")) {
			print "Could not backup loader.conf! ".
			    "Aborting HZ change.\n";
			return 1;
		    }
		}
		if (!open(FD, ">>/boot/loader.conf")) {
		    print "Could not set new HZ value in loader.conf! ".
			"Aborting HZ change.\n";
		    return 1;
		}
		print FD "\n# The following was added by delay setup.\n";
		print FD "kern.hz=$kernelhz\n";
		close(FD);
		$needreboot = 1;
	    }
	} else {
	    print "*** WARNING: Could not determine HZ value of kernel! ".
		  "Continuing anyway.\n";
	}
    }

    #
    # Make sure we are running the desired kernel.
    #
1136
    if (-e $kernel) {
Mike Hibler's avatar
Mike Hibler committed
1137 1138
	#
	# In FreeBSD 5 and above, the command line is used to ensure we
1139 1140
	# boot the correct kernel.  So just complain if it is not right,
	# as we have no way to fix it.
Mike Hibler's avatar
Mike Hibler committed
1141 1142 1143 1144 1145
	#
	if ($FBSD_VERSION >= 5) {
	    my $curkernel = `sysctl -n kern.bootfile`;
	    chomp($curkernel);
	    if ($curkernel ne $kernel) {
1146 1147 1148
		print "*** WARNING: FreeBSD 5+ running wrong kernel ".
		      "'$curkernel', should be '$kernel'. ".
		      "Continuing anyway.\n";
Mike Hibler's avatar
Mike Hibler committed
1149 1150
	    }
	}
1151 1152 1153
	elsif (system("cmp -s /kernel $kernel") != 0) {
	    print STDOUT "Copying $kernel to /kernel ...\n";
	    if (system("cp -fp /kernel /kernel.save")) {
1154
		print "Could not backup /kernel! Aborting kernel change\n";
1155
		$failed++;
1156
	    }
1157
	    elsif (!unlink("/kernel")) {
1158
		print "Could not unlink /kernel! Aborting kernel change\n";
1159 1160
		unlink("/kernel.save");
		$failed++;
1161
	    }
1162
	    elsif (system("cp -fp $kernel /kernel")) {
1163
		print "Could not cp $kernel to /kernel!\n";