blob: 9c4f7e1528cbee9e0b5cdf47c89f055a39511593 [file] [log] [blame]
#!/bin/sh
#
# Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
set -e
# Only run this script on test machines that run in the lab.
# See autotest/server/hosts/site_host.py for more information.
if [ ! -f /mnt/stateful_partition/.labmachine ]; then
exit 0
fi
NON_ETHERNET_DRIVERS="cdc_ether"
# Returns the default gateway.
get_default_gateway() {
local ip_route="$(ip route get 1.0.0.0)"
echo ${ip_route} | head -n 1 | cut -f 3 -d ' '
}
# Returns 0 if $1 is a non-Ethernet driver, or 1 otherwise.
is_non_ethernet_driver() {
local driver="$1"
local non_ethernet_driver
for non_ethernet_driver in ${NON_ETHERNET_DRIVERS}; do
if [ "${driver}" = "${non_ethernet_driver}" ]; then
return 0
fi
done
return 1
}
# Shows the list of Ethernet interfaces found on the system.
find_ethernet_interfaces() {
local device_path
local driver_path
local driver
for device_path in /sys/class/net/eth*; do
driver_path="${device_path}/device/driver"
if [ -e "${driver_path}" ]; then
driver=$(basename $(readlink -f "${driver_path}"))
if ! is_non_ethernet_driver "${driver}"; then
basename "${device_path}"
fi
fi
done
}
# Pings the given ipaddress through the given ethernet device.
# $1 - The ethernet device to ping through.
# $2 - IP address to ping.
do_ping() {
local eth=$1
local ip_addr=$2
ping -I ${eth} -c 1 ${ip_addr}
}
# Restart all our ethernet devices and restart shill.
recover_ethernet_devices() {
local eth
for eth in $(find_ethernet_interfaces); do
ifconfig ${eth} down
ifconfig ${eth} up
done
initctl stop flimflam || echo "Flimflam was not running."
initctl start flimflam
sleep 30
}
# Loop through all ethernet devices and see if we can connect to our default
# gateway.
ping_default_gateway_over_ethernet() {
local eth default_gateway
default_gateway="$(get_default_gateway)" || default_gateway=
if [ -n "${default_gateway}" ]; then
for eth in $(find_ethernet_interfaces); do
if do_ping ${eth} ${default_gateway}; then
return 0
fi
done
fi
return 1
}
main() {
# Attempt to ping our default gateway over ethernet.
if ping_default_gateway_over_ethernet; then
return 0
fi
# We can't reach our default gateway through any ethernet devices.
recover_ethernet_devices
# Attempt to ping again. If successful, return 1 so that way log the fact
# that we need to take action to recover the dut.
if ping_default_gateway_over_ethernet; then
return 1
fi
# Last chance - reboot if we can't get any connectivity.
echo "All efforts to recover ethernet have been exhausted. Rebooting."
(sleep 5 && reboot) &
return 1
}
main