power_whol.pm.in 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
#!/usr/bin/perl -wT

#
# EMULAB-COPYRIGHT
# Copyright (c) 2004, 2005 University of Utah and the Flux Group.
# All rights reserved.
#

#
# Handle "Whack on LAN" reset.
# Currently we serialize the operation, one node at a time.
# Eventually we could do this in groups by putting multiple node
# MAC addrs in the magic whol packet.
#

package power_whol;

use Exporter;
@ISA = ("Exporter");
@EXPORT = qw( wholctrl );

use lib "@prefix@/lib";
use libdb;

#
# Commands we run
#
my $TBROOT = '@prefix@';
my $bossnode = 'boss';

my $TBWHOLPOWER = "$TBROOT/sbin/whol";
my $SNMPIT = "$TBROOT/bin/snmpit -q";
my $whollan = "WhOL";

my $whackall = 1;
my $dowhack = 1;
my $debug = 0;

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

my %portinfo = ();

# usage: wholctrl(cmd, nodes)
# cmd = { "cycle" | "on" | "off" }
# nodes = list of one or more physcial node names
#
# Returns 0 on success. Non-zero on failure.
# 
sub wholctrl($$@) {
    my ($cmd, $iface, @nodes) = @_;
    my $exitval = 0;

    if ($cmd ne "cycle") {
	warn "WhOL module can only \"cycle\" nodes\n";
	return 1;
    }

    print STDERR "WhOL called with iface=$iface ", join(" ", @nodes), "\n"
	if ($debug);

    #
    # Locate the whackable interface on each node along with its mac
    # Find the boss interface while we are at it
    #
    for my $node (@nodes) {
	my $result =
	    DBQueryFatal("select card,mac from interfaces ".
		"where role='expt' and whol=1 and node_id='$node'");
	if ($result->num_rows() == 0) {
	    warn "No WhOL interface for $node, skipping\n";
	    $exitval++;
	    next;
	}
	($card, $mac) = $result->fetchrow();
	$portinfo{$node}{card} = $card;
	$portinfo{$node}{mac} = $mac;
	print STDERR "WhOL: $node: $card, $mac\n"
	    if ($debug);
    }

    # XXX locking

    if ($whackall) {
	if (whacksome($iface, @nodes)) {
	    $exitval++;
	}
    }
    else {
	for my $node (@nodes) {
	    my @nodelist = ($node);

	    if (whacksome($iface, @nodelist)) {
		$exitval++;
	    }
	}
    }

    # XXX unlocking

    return $exitval;
}

sub whacksome($@) {
    my ($iface, @nodelist) = @_;
    my $failed = 0;

    my @portlist = map { "$_:$portinfo{$_}{card}" } @nodelist;
    my $portstr = join(" ", @portlist);

    if ($debug) {
	print STDERR "Whackin: ", join(" ", @nodelist), "\n";
	print STDERR "Ports:   ", $portstr, "\n";
    }

    #
    # Save off the state of all ports
    #
    if (!open(SNMPIT, "$SNMPIT -b $portstr |")) {
	warn "WhOL: snmpit -b failed\n";
	return 1;
    }
    while (my $line = <SNMPIT>) {
	chomp($line);

	my %args = parseStatusString($line);
	my $port = $args{port};
	if (!$port) {
	    warn "WhOL: snmpit returned bogus status string\n";
	    return 1;
	}
	my ($node) = split(":", $port);
	# untaint
	if ($line =~ /(.*)/) {
	    $portinfo{$node}{status} = $1;
	}
    }
    if (!close(SNMPIT) || $?) {
	warn "WhOL: could not get status for ports\n";
	return 1;
    }
    if ($debug) {
	print STDERR "Old port status:\n";
	for my $key (keys(%portinfo)) {
	    print STDERR "  $key: ", $portinfo{$key}{status}, "\n"
		if ($portinfo{$key}{status});
	}
    }

150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    #
    # Divide the nodes up into used and unused based on the status of
    # their WhOL experimental interface:
    #
    # enabled=yes	port in use
    # enabled=no	not used
    #
    # If a port is "in use" we assume it has talked to the switch and
    # no speed/duplex changes are needed.
    #
    # If the port is unused, we assume that the port will try to auto
    # negotiate with the switch when enabled.  In this case we set
    # auto-negotiation on the switch to maximize the chance that we will
    # sucessfully communicate with the card.
    #
    my $autoneg = "";
    for my $key (keys(%portinfo)) {
	if ($portinfo{$key}{status} &&
	    $portinfo{$key}{status} =~ /enabled=no/) {
	    $autoneg .= "$key ";
	}
    }
    if ($autoneg ne "") {
	print STDERR "Setting autonegotiate on ports: $autoneg\n"
	    if ($debug);
	if (system("$SNMPIT -a $portstr")) {
	    warn "WhOL: could not set autoconfig for $portstr\n";
177 178
# not fatal for now til we fix autoneg on cisco2950s
#	    $failed++;
179 180 181
	}
    }

182 183 184
    #
    # Put all ports in the WhOL VLAN, snmpit will enable them.
    #
185
    if (system("$SNMPIT -f -m $whollan $portstr")) {
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	warn "WhOL: could not place ports in WHOL LAN\n";
	$failed++;
    }
    
    #
    # Strafe the VLAN with fiery packets of death.
    #
    if (!$failed && $dowhack) {
	my $macstr = join(" ", map { $portinfo{$_}{mac} } @nodelist);

	# XXX leave time for auto-negotiation if port had to be enabled
	sleep(5);

	if (system("$TBWHOLPOWER $iface $macstr")) {
	    warn "WhOL: could not send WhOL packets\n";
	    $failed++;
	} else {
	    print STDERR "Whacked ", @nodelist, "\n";
	}
    }

    #
    # Restore all ports to their previous state.
    # This will move ports back to their original VLANs,
    # disabling unused ports.
    #
    my $statstr = "";
    for my $node (@nodelist) {
	$statstr .= " -B '" . $portinfo{$node}{status} . "'";
    }
    if (system("$SNMPIT $statstr")) {
	warn "WhOL: could not restore port state:\n";
	for my $node (@nodelist) {
	    print STDERR "  ", $portinfo{$node}{status}, "\n";
	}
	$failed++;
    }

    return $failed;
}

#
# Snagged from snmpit.
# Parse a port status string. Returns a key-value hash pair
#
sub parseStatusString($) {
    my ($string) = @_;
    chomp $string;

    my %pairs;
    foreach my $pair (split /;/, $string) {
        my ($key, $value) = split /=/,$pair,2;
        if (!$key || !$value) {
            die "ERROR: Bad port status string: $string\n";
        } else {
            $pairs{$key} = $value;
        }
    }

    return %pairs;
}

1;