Sari la conținut

Enhanced Primary Connection Testing - Parallel Connectivity Testing

Problem: Testing Primary While Backup is Active

Current Issue:

# Current test only checks device availability
if [ -n "${RED_DEV}" ] && ip link show "${RED_DEV}" >/dev/null 2>&1; then
    return 0  # ❌ This doesn't test actual connectivity
fi

Problem: Device being available doesn't mean the connection works. We need to test actual connectivity with ping while backup connection is active.

Solution: Parallel Route Testing

Enhanced Primary Testing Strategy:

  1. 🔧 Temporary Route Setup: Create temporary routes for primary testing
  2. 🌐 Parallel Connectivity: Test primary connection without disrupting backup
  3. 📡 Real Ping Tests: Use actual ping tests through primary interface
  4. 🧹 Clean Cleanup: Remove temporary routes after testing

Enhanced Implementation

File: /usr/local/bin/mwan-daemon-universal (Enhanced Version)

#!/bin/bash
###############################################################################
# Universal MWAN Daemon - Enhanced with Parallel Primary Testing
###############################################################################

# Enhanced primary connection testing with parallel connectivity
test_primary_connection() {
    local backup_dir="${MWAN_STATE_DIR}/primary-backup"

    if [ ! -d "${backup_dir}" ]; then
        mwan_log "WARN" "No primary backup found, cannot test"
        return 1
    fi

    local primary_type=$(cat "${backup_dir}/connection-type" 2>/dev/null)

    mwan_log "DEBUG" "Testing primary connection type: ${primary_type} with parallel connectivity"

    case "${primary_type}" in
        "PPPOE"|"PPTP"|"PPOA"|"PPTPATM"|"PPPOEATM")
            test_ppp_connection_parallel "${backup_dir}"
            ;;
        "STATIC")
            test_static_connection_parallel "${backup_dir}"
            ;;
        "DHCP")
            test_dhcp_connection_parallel "${backup_dir}"
            ;;
        "QMI")
            test_qmi_connection_parallel "${backup_dir}"
            ;;
        *)
            mwan_log "WARN" "Unknown primary connection type: ${primary_type}"
            return 1
            ;;
    esac
}

# Test PPP connection with parallel connectivity
test_ppp_connection_parallel() {
    local backup_dir="$1"

    if [ ! -f "${backup_dir}/ppp-settings" ]; then
        return 1
    fi

    source "${backup_dir}/ppp-settings"

    # Check if underlying device is available
    if [ -z "${DEVICE}" ] || ! ip link show "${DEVICE}" >/dev/null 2>&1; then
        mwan_log "DEBUG" "PPP underlying device ${DEVICE} not available"
        return 1
    fi

    # For PPP, we need to temporarily establish connection to test
    mwan_log "DEBUG" "Testing PPP connection on ${DEVICE}"

    # Create temporary PPP connection for testing
    if test_ppp_connectivity_parallel "${DEVICE}" "${USERNAME}" "${PASSWORD}"; then
        mwan_log "DEBUG" "PPP connection test successful"
        return 0
    else
        mwan_log "DEBUG" "PPP connection test failed"
        return 1
    fi
}

# Test static connection with parallel connectivity
test_static_connection_parallel() {
    local backup_dir="$1"

    if [ ! -f "${backup_dir}/ethernet-settings" ]; then
        return 1
    fi

    source "${backup_dir}/ethernet-settings"

    # Check if device is available
    if [ -z "${RED_DEV}" ] || ! ip link show "${RED_DEV}" >/dev/null 2>&1; then
        mwan_log "DEBUG" "Static device ${RED_DEV} not available"
        return 1
    fi

    # Test static connection with parallel routes
    mwan_log "DEBUG" "Testing static connection on ${RED_DEV}"

    if test_static_connectivity_parallel "${RED_DEV}" "${RED_ADDRESS}" "${RED_NETMASK}" "${RED_GATEWAY}"; then
        mwan_log "DEBUG" "Static connection test successful"
        return 0
    else
        mwan_log "DEBUG" "Static connection test failed"
        return 1
    fi
}

# Test DHCP connection with parallel connectivity
test_dhcp_connection_parallel() {
    local backup_dir="$1"

    if [ ! -f "${backup_dir}/ethernet-settings" ]; then
        return 1
    fi

    source "${backup_dir}/ethernet-settings"

    # Check if device is available and has carrier
    if [ -z "${RED_DEV}" ] || ! ip link show "${RED_DEV}" >/dev/null 2>&1; then
        mwan_log "DEBUG" "DHCP device ${RED_DEV} not available"
        return 1
    fi

    # Check carrier (cable connected)
    if [ "$(cat /sys/class/net/${RED_DEV}/carrier 2>/dev/null)" != "1" ]; then
        mwan_log "DEBUG" "DHCP device ${RED_DEV} has no carrier"
        return 1
    fi

    # Test DHCP connection with parallel routes
    mwan_log "DEBUG" "Testing DHCP connection on ${RED_DEV}"

    if test_dhcp_connectivity_parallel "${RED_DEV}"; then
        mwan_log "DEBUG" "DHCP connection test successful"
        return 0
    else
        mwan_log "DEBUG" "DHCP connection test failed"
        return 1
    fi
}

# Test QMI connection with parallel connectivity
test_qmi_connection_parallel() {
    local backup_dir="$1"

    if [ ! -f "${backup_dir}/ethernet-settings" ]; then
        return 1
    fi

    source "${backup_dir}/ethernet-settings"

    # Check if QMI device exists
    if [ -z "${RED_DEV}" ] || [ ! -c "${RED_DEV}" ]; then
        mwan_log "DEBUG" "QMI device ${RED_DEV} not available"
        return 1
    fi

    # Test QMI connection
    mwan_log "DEBUG" "Testing QMI connection on ${RED_DEV}"

    if test_qmi_connectivity_parallel "${RED_DEV}"; then
        mwan_log "DEBUG" "QMI connection test successful"
        return 0
    else
        mwan_log "DEBUG" "QMI connection test failed"
        return 1
    fi
}

###############################################################################
# PARALLEL CONNECTIVITY TESTING FUNCTIONS
###############################################################################

# Test PPP connectivity in parallel with backup connection
test_ppp_connectivity_parallel() {
    local device="$1"
    local username="$2"
    local password="$3"

    # Create temporary PPP interface for testing
    local test_interface="ppp-test-$$"
    local test_table="100"  # Use routing table 100 for testing

    mwan_log "DEBUG" "Creating temporary PPP connection for testing"

    # Create temporary pppd configuration
    local temp_ppp_config="/tmp/ppp-test-$$"
    cat > "${temp_ppp_config}" << EOF
${device}
user "${username}"
password "${password}"
nodetach
noauth
defaultroute-metric 200
unit 99
linkname ${test_interface}
ipparam test
EOF

    # Start temporary PPP connection
    pppd file "${temp_ppp_config}" &
    local pppd_pid=$!

    # Wait for connection to establish (max 30 seconds)
    local attempts=0
    local ppp_interface=""
    while [ ${attempts} -lt 15 ]; do
        ppp_interface=$(ip link show | grep "ppp99:" | cut -d: -f2 | tr -d ' ')
        if [ -n "${ppp_interface}" ] && ip addr show "${ppp_interface}" | grep -q "inet.*peer"; then
            break
        fi
        sleep 2
        attempts=$((attempts + 1))
    done

    local test_result=1

    if [ -n "${ppp_interface}" ]; then
        # Get PPP interface IP and peer
        local ppp_ip=$(ip addr show "${ppp_interface}" | grep "inet.*peer" | awk '{print $2}' | cut -d/ -f1)
        local ppp_peer=$(ip addr show "${ppp_interface}" | grep "inet.*peer" | awk '{print $4}')

        if [ -n "${ppp_ip}" ] && [ -n "${ppp_peer}" ]; then
            mwan_log "DEBUG" "PPP test interface ${ppp_interface} established: ${ppp_ip} peer ${ppp_peer}"

            # Test connectivity through PPP interface
            if test_connectivity_through_interface "${ppp_interface}" "${ppp_ip}"; then
                test_result=0
            fi
        fi
    fi

    # Cleanup temporary PPP connection
    kill "${pppd_pid}" 2>/dev/null || true
    sleep 2
    rm -f "${temp_ppp_config}"

    # Remove any leftover interface
    if [ -n "${ppp_interface}" ]; then
        ip link delete "${ppp_interface}" 2>/dev/null || true
    fi

    return ${test_result}
}

# Test static connectivity in parallel with backup connection
test_static_connectivity_parallel() {
    local device="$1"
    local ip_address="$2"
    local netmask="$3"
    local gateway="$4"

    local test_table="101"  # Use routing table 101 for testing

    mwan_log "DEBUG" "Testing static connectivity on ${device}"

    # Save current device state
    local original_state=$(ip addr show "${device}" 2>/dev/null)

    # Configure device temporarily for testing
    ip addr flush dev "${device}" 2>/dev/null || true
    ip addr add "${ip_address}/${netmask}" dev "${device}"
    ip link set "${device}" up

    # Add temporary route to test table
    ip route add default via "${gateway}" dev "${device}" table "${test_table}" metric 200

    # Test connectivity through this interface
    local test_result=1
    if test_connectivity_through_interface "${device}" "${ip_address}" "${test_table}"; then
        test_result=0
    fi

    # Cleanup: restore original state
    ip route del default via "${gateway}" dev "${device}" table "${test_table}" 2>/dev/null || true
    ip addr flush dev "${device}" 2>/dev/null || true
    ip link set "${device}" down 2>/dev/null || true

    return ${test_result}
}

# Test DHCP connectivity in parallel with backup connection
test_dhcp_connectivity_parallel() {
    local device="$1"

    local test_table="102"  # Use routing table 102 for testing

    mwan_log "DEBUG" "Testing DHCP connectivity on ${device}"

    # Start temporary DHCP client
    ip link set "${device}" up

    # Use dhcpcd with custom options for testing
    local dhcp_pid_file="/tmp/dhcpcd-test-${device}.pid"
    local dhcp_lease_file="/tmp/dhcpcd-test-${device}.lease"

    dhcpcd -p "${dhcp_pid_file}" -l "${dhcp_lease_file}" -t 15 -A "${device}" &
    local dhcpcd_pid=$!

    # Wait for DHCP lease (max 20 seconds)
    local attempts=0
    local got_lease=0
    while [ ${attempts} -lt 10 ]; do
        if ip addr show "${device}" | grep -q "inet.*global"; then
            got_lease=1
            break
        fi
        sleep 2
        attempts=$((attempts + 1))
    done

    local test_result=1

    if [ ${got_lease} -eq 1 ]; then
        # Get DHCP assigned IP
        local dhcp_ip=$(ip addr show "${device}" | grep "inet.*global" | awk '{print $2}' | cut -d/ -f1)
        local dhcp_gw=$(ip route show dev "${device}" | grep "default" | awk '{print $3}')

        if [ -n "${dhcp_ip}" ] && [ -n "${dhcp_gw}" ]; then
            mwan_log "DEBUG" "DHCP test on ${device}: IP ${dhcp_ip}, GW ${dhcp_gw}"

            # Add route to test table
            ip route add default via "${dhcp_gw}" dev "${device}" table "${test_table}" metric 200

            # Test connectivity
            if test_connectivity_through_interface "${device}" "${dhcp_ip}" "${test_table}"; then
                test_result=0
            fi

            # Cleanup route
            ip route del default via "${dhcp_gw}" dev "${device}" table "${test_table}" 2>/dev/null || true
        fi
    fi

    # Cleanup DHCP test
    kill "${dhcpcd_pid}" 2>/dev/null || true
    rm -f "${dhcp_pid_file}" "${dhcp_lease_file}"
    ip addr flush dev "${device}" 2>/dev/null || true
    ip link set "${device}" down 2>/dev/null || true

    return ${test_result}
}

# Test QMI connectivity in parallel with backup connection
test_qmi_connectivity_parallel() {
    local qmi_device="$1"

    mwan_log "DEBUG" "Testing QMI connectivity on ${qmi_device}"

    # QMI testing requires qmicli tool
    if ! command -v qmicli >/dev/null 2>&1; then
        mwan_log "WARN" "qmicli not available, cannot test QMI connection"
        return 1
    fi

    # Test QMI device responsiveness
    if qmicli -d "${qmi_device}" --get-service-version-info >/dev/null 2>&1; then
        mwan_log "DEBUG" "QMI device ${qmi_device} responsive"
        return 0
    else
        mwan_log "DEBUG" "QMI device ${qmi_device} not responsive"
        return 1
    fi
}

# Core connectivity testing through specific interface
test_connectivity_through_interface() {
    local interface="$1"
    local source_ip="$2"
    local routing_table="${3:-main}"

    # Test targets (multiple for reliability)
    local test_targets=("8.8.8.8" "1.1.1.1" "208.67.222.222")
    local success_count=0
    local required_success=2  # Require 2 out of 3 to pass

    for target in "${test_targets[@]}"; do
        mwan_log "DEBUG" "Testing connectivity to ${target} via ${interface} (source: ${source_ip})"

        # Use ping with specific source and routing table
        if [ "${routing_table}" = "main" ]; then
            # Use source IP binding for main table
            if ping -c 2 -W 3 -I "${source_ip}" "${target}" >/dev/null 2>&1; then
                success_count=$((success_count + 1))
                mwan_log "DEBUG" "Ping to ${target} via ${interface} successful"
            else
                mwan_log "DEBUG" "Ping to ${target} via ${interface} failed"
            fi
        else
            # Use ip route for custom routing table
            if ip route get "${target}" table "${routing_table}" >/dev/null 2>&1; then
                # Route exists, test with source binding
                if ping -c 2 -W 3 -I "${source_ip}" "${target}" >/dev/null 2>&1; then
                    success_count=$((success_count + 1))
                    mwan_log "DEBUG" "Ping to ${target} via ${interface} (table ${routing_table}) successful"
                else
                    mwan_log "DEBUG" "Ping to ${target} via ${interface} (table ${routing_table}) failed"
                fi
            fi
        fi
    done

    if [ ${success_count} -ge ${required_success} ]; then
        mwan_log "DEBUG" "Connectivity test passed (${success_count}/${#test_targets[@]})"
        return 0
    else
        mwan_log "DEBUG" "Connectivity test failed (${success_count}/${#test_targets[@]})"
        return 1
    fi
}

# Enhanced failback with improved primary testing
failback_to_primary() {
    local current_profile=$(cat "${MWAN_STATE_DIR}/current-profile" 2>/dev/null)

    if [ -z "${current_profile}" ] || [ "${current_profile}" = "primary" ]; then
        return 0  # Already on primary
    fi

    mwan_log "INFO" "Testing primary connection for failback with parallel connectivity testing"

    # Test primary connection reliability with enhanced testing
    local success_count=0
    for i in $(seq 1 ${FAILBACK_ATTEMPTS:-3}); do
        mwan_log "DEBUG" "Primary connectivity test attempt ${i}/${FAILBACK_ATTEMPTS:-3}"
        if test_primary_connection; then
            success_count=$((success_count + 1))
            mwan_log "DEBUG" "Primary test ${i} passed"
        else
            mwan_log "DEBUG" "Primary test ${i} failed"
        fi
        sleep 5  # Longer delay between tests for stability
    done

    # Require majority of tests to pass
    local required_success=$(((${FAILBACK_ATTEMPTS:-3} / 2) + 1))
    if [ ${success_count} -ge ${required_success} ]; then
        mwan_log "INFO" "Primary connection reliable with real connectivity (${success_count}/${FAILBACK_ATTEMPTS:-3}), initiating failback"

        # Stop interference prevention
        if [ -f "${MWAN_STATE_DIR}/interference-prevention.pid" ]; then
            local prev_pid=$(cat "${MWAN_STATE_DIR}/interference-prevention.pid")
            kill "${prev_pid}" 2>/dev/null || true
            rm -f "${MWAN_STATE_DIR}/interference-prevention.pid"
        fi

        # Restore primary connection
        if restore_primary_connection; then
            # Clean up state
            rm -f "${MWAN_STATE_DIR}/current-profile"
            rm -f "${MWAN_STATE_DIR}/failover-time"

            # Restart original connectd for primary monitoring
            start_connectd

            mwan_log "INFO" "Failback completed successfully"
        else
            mwan_log "ERROR" "Failback failed, staying on backup"

            # Restart interference prevention
            prevent_system_interference &
            echo $! > "${MWAN_STATE_DIR}/interference-prevention.pid"
        fi
    else
        mwan_log "WARN" "Primary still unreliable with connectivity testing (${success_count}/${FAILBACK_ATTEMPTS:-3}), staying on backup"
    fi
}

# Rest of the functions remain the same...
# (All other functions from the universal implementation)

Summary of Changes for Parallel Testing

What Changed:

  1. 🔧 Enhanced test_primary_connection(): Now calls type-specific parallel testing functions
  2. 🌐 Parallel Testing Functions: New functions for each connection type that test actual connectivity
  3. 📡 Real Ping Tests: Uses ping with source IP binding and custom routing tables
  4. 🛣️ Temporary Routes: Creates temporary routing tables (100, 101, 102) for testing
  5. 🧹 Clean Cleanup: Properly cleans up temporary interfaces, routes, and processes

Key Features:

  • ✅ Parallel Operation: Tests primary without disrupting backup connection
  • ✅ Real Connectivity: Uses actual ping tests to external targets (8.8.8.8, 1.1.1.1, etc.)
  • ✅ Multiple Targets: Tests against multiple DNS servers for reliability
  • ✅ Source Binding: Uses specific source IPs to ensure traffic goes through correct interface
  • ✅ Temporary Infrastructure: Creates temporary PPP connections, DHCP leases, routing tables
  • ✅ Robust Cleanup: Ensures no leftover configuration after testing

How It Works:

  1. Backup Active: Current backup connection remains fully functional
  2. Parallel Setup: Temporarily configures primary interface for testing
  3. Connectivity Test: Pings external targets through primary interface
  4. Result Evaluation: Requires majority of tests to pass (2 out of 3 targets)
  5. Clean Cleanup: Removes all temporary configuration
  6. Decision: Only triggers failback if primary shows real connectivity

This ensures that failback only happens when the primary connection can actually reach the internet, not just when the device is available.