Skip to content

Commit

Permalink
Check ipv6 connectivity for each interface
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbattat committed Dec 12, 2024
1 parent e2d6808 commit ef4a048
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions ic-os/components/networking/setup-networking/setup-networking.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,25 @@ function generate_addresses() {
}

function select_fastest_interface() {
echo "Selecting the fastest interface..."
echo "Selecting the fastest interface with actual IPv6 connectivity..."
INTERFACES=$(ip -o link show | awk -F': ' '{print $2}' | grep -v '^lo$')
BEST_IFACE=""
BEST_SPEED=0

for IFACE in $INTERFACES; do
echo "Checking interface: $IFACE"
if ip -6 addr show dev "$IFACE" 2>/dev/null | grep -q "inet6 fe80::"; then
echo "Bringing up interface: $IFACE"
ip link set dev "$IFACE" up || true
sleep 2

echo "Assigning IPv6 address to $IFACE"
ip -6 addr add "${IPV6_ADDR}/64" dev "$IFACE"

# Give the system time to configure the address
sleep 2

echo "Testing IPv6 connectivity on $IFACE by pinging $IPV6_GATEWAY"
if ping6 -c 3 -I "$IFACE" "$IPV6_GATEWAY" >/dev/null 2>&1; then
echo "$IFACE can reach IPv6 gateway."
SPEED_STR=$(ethtool "$IFACE" 2>/dev/null | grep "Speed:" || true)
SPEED=$(echo "$SPEED_STR" | grep -oP '\d+' || echo 0)
echo "Interface $IFACE speed: ${SPEED:-0} Mb/s"
Expand All @@ -67,14 +78,23 @@ function select_fastest_interface() {
BEST_IFACE="$IFACE"
echo "New best interface: $BEST_IFACE at $BEST_SPEED Mb/s"
fi
else
echo "$IFACE cannot reach IPv6 gateway."
fi

# Clean up the assigned IPv6 address before checking next interface
ip -6 addr flush dev "$IFACE"
ip link set dev "$IFACE" down
done

if [ -z "$BEST_IFACE" ]; then
echo "No suitable interface found with IPv6 connectivity."
exit 1
fi
echo "Using fastest interface: $BEST_IFACE at ${BEST_SPEED}Mb/s"
# Bring the best interface back up
ip link set dev "$BEST_IFACE" up
ip -6 addr add "${IPV6_ADDR}/64" dev "$BEST_IFACE"
}

function configure_netplan() {
Expand Down

0 comments on commit ef4a048

Please sign in to comment.