Skip to content

Commit e0e474c

Browse files
committed
utils: net: ensure_network_online: add bounded Wi-Fi/Ethernet retry + nmcli→wpa_supplicant fallback
This change hardens connectivity bring-up for flaky first-attempt failures: - Keep fast-path: return immediately if already online; sync clock when ok. - Ethernet first: link check, interface bring-up, DHCP, status triage (0/2/1). - Wi-Fi path adds bounded retries with cooldowns and cleanup between tries. - Prefer nmcli; on failure, fall back to wpa_supplicant + DHCP automatically. - Refresh udhcpc default.script if present to repair route/DNS, then recheck. - Preserve tri-state semantics: 0=online, 2=IP but no internet, 1=offline. Signed-off-by: Srikanth Muppandam <[email protected]>
1 parent 7915fd1 commit e0e474c

File tree

1 file changed

+69
-36
lines changed

1 file changed

+69
-36
lines changed

Runner/utils/functestlib.sh

Lines changed: 69 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3715,7 +3715,7 @@ ensure_network_online() {
37153715
esac
37163716
done
37173717

3718-
# -------- Wi-Fi pass --------
3718+
# -------- Wi-Fi pass (with bounded retry) --------
37193719
net_wifi=""
37203720
if command -v get_wifi_interface >/dev/null 2>&1; then
37213721
net_wifi="$(get_wifi_interface 2>/dev/null || echo "")"
@@ -3733,49 +3733,82 @@ ensure_network_online() {
37333733
net_creds="$(get_wifi_credentials "" "" 2>/dev/null || true)"
37343734
fi
37353735

3736-
if [ -n "$net_creds" ]; then
3737-
net_ssid="$(printf '%s\n' "$net_creds" | awk '{print $1}')"
3738-
net_pass="$(printf '%s\n' "$net_creds" | awk '{print $2}')"
3739-
log_info "[NET] ${net_wifi}: trying nmcli for SSID='${net_ssid}'"
3740-
if command -v wifi_connect_nmcli >/dev/null 2>&1; then
3741-
wifi_connect_nmcli "$net_wifi" "$net_ssid" "$net_pass" || true
3736+
# ---- New: retry knobs (env-overridable) ----
3737+
wifi_max_attempts="${NET_WIFI_RETRIES:-2}"
3738+
wifi_retry_delay="${NET_WIFI_RETRY_DELAY:-5}"
3739+
if [ -z "$wifi_max_attempts" ] || [ "$wifi_max_attempts" -lt 1 ] 2>/dev/null; then
3740+
wifi_max_attempts=1
3741+
fi
3742+
if [ -z "$wifi_retry_delay" ] || [ "$wifi_retry_delay" -lt 0 ] 2>/dev/null; then
3743+
wifi_retry_delay=0
3744+
fi
3745+
3746+
wifi_attempt=1
3747+
while [ "$wifi_attempt" -le "$wifi_max_attempts" ]; do
3748+
if [ "$wifi_max_attempts" -gt 1 ]; then
3749+
log_info "[NET] ${net_wifi}: Wi-Fi attempt ${wifi_attempt}/${wifi_max_attempts}"
37423750
fi
37433751

3744-
# If nmcli brought us up, do NOT fall back to wpa_supplicant
3745-
check_network_status_rc; net_rc=$?
3746-
if [ "$net_rc" -ne 0 ]; then
3747-
log_info "[NET] ${net_wifi}: falling back to wpa_supplicant + DHCP"
3748-
if command -v wifi_connect_wpa_supplicant >/dev/null 2>&1; then
3749-
wifi_connect_wpa_supplicant "$net_wifi" "$net_ssid" "$net_pass" || true
3752+
if [ -n "$net_creds" ]; then
3753+
net_ssid="$(printf '%s\n' "$net_creds" | awk '{print $1}')"
3754+
net_pass="$(printf '%s\n' "$net_creds" | awk '{print $2}')"
3755+
log_info "[NET] ${net_wifi}: trying nmcli for SSID='${net_ssid}'"
3756+
if command -v wifi_connect_nmcli >/dev/null 2>&1; then
3757+
wifi_connect_nmcli "$net_wifi" "$net_ssid" "$net_pass" || true
3758+
fi
3759+
3760+
# If nmcli brought us up, do NOT fall back to wpa_supplicant
3761+
check_network_status_rc; net_rc=$?
3762+
if [ "$net_rc" -ne 0 ]; then
3763+
log_info "[NET] ${net_wifi}: falling back to wpa_supplicant + DHCP"
3764+
if command -v wifi_connect_wpa_supplicant >/dev/null 2>&1; then
3765+
wifi_connect_wpa_supplicant "$net_wifi" "$net_ssid" "$net_pass" || true
3766+
fi
3767+
if command -v run_dhcp_client >/dev/null 2>&1; then
3768+
run_dhcp_client "$net_wifi" 10 >/dev/null 2>&1 || true
3769+
fi
37503770
fi
3771+
else
3772+
log_info "[NET] ${net_wifi}: no credentials provided → DHCP only"
37513773
if command -v run_dhcp_client >/dev/null 2>&1; then
3752-
run_dhcp_client "$net_wifi" 10 >/dev/null 2>&1 || true
3774+
run_dhcp_client "$net_wifi" 10 >/devnull 2>&1 || true
37533775
fi
37543776
fi
3755-
else
3756-
log_info "[NET] ${net_wifi}: no credentials provided → DHCP only"
3757-
if command -v run_dhcp_client >/dev/null 2>&1; then
3758-
run_dhcp_client "$net_wifi" 10 >/dev/null 2>&1 || true
3777+
3778+
net_log_iface_snapshot "$net_wifi"
3779+
check_network_status_rc; net_rc=$?
3780+
case "$net_rc" in
3781+
0)
3782+
log_pass "[NET] ${net_wifi}: internet reachable"
3783+
ensure_reasonable_clock || log_warn "Proceeding in limited-network mode."
3784+
unset net_wifi net_ifaces net_ifc net_rc net_had_any_ip net_creds net_ssid net_pass wifi_attempt wifi_max_attempts wifi_retry_delay
3785+
return 0
3786+
;;
3787+
2)
3788+
log_warn "[NET] ${net_wifi}: IP assigned but internet not reachable"
3789+
net_had_any_ip=1
3790+
;;
3791+
1)
3792+
log_info "[NET] ${net_wifi}: still no IP after connect/DHCP attempt"
3793+
;;
3794+
esac
3795+
3796+
# If not last attempt, cooldown + cleanup before retry
3797+
if [ "$wifi_attempt" -lt "$wifi_max_attempts" ]; then
3798+
if command -v wifi_cleanup >/dev/null 2>&1; then
3799+
wifi_cleanup "$net_wifi" || true
3800+
fi
3801+
if command -v bringup_interface >/dev/null 2>&1; then
3802+
bringup_interface "$net_wifi" 2 2 || true
3803+
fi
3804+
if [ "$wifi_retry_delay" -gt 0 ] 2>/dev/null; then
3805+
log_info "[NET] ${net_wifi}: retrying in ${wifi_retry_delay}s…"
3806+
sleep "$wifi_retry_delay"
3807+
fi
37593808
fi
3760-
fi
37613809

3762-
net_log_iface_snapshot "$net_wifi"
3763-
check_network_status_rc; net_rc=$?
3764-
case "$net_rc" in
3765-
0)
3766-
log_pass "[NET] ${net_wifi}: internet reachable"
3767-
ensure_reasonable_clock || log_warn "Proceeding in limited-network mode."
3768-
unset net_wifi net_ifaces net_ifc net_rc net_had_any_ip net_creds net_ssid net_pass
3769-
return 0
3770-
;;
3771-
2)
3772-
log_warn "[NET] ${net_wifi}: IP assigned but internet not reachable"
3773-
net_had_any_ip=1
3774-
;;
3775-
1)
3776-
log_info "[NET] ${net_wifi}: still no IP after connect/DHCP attempt"
3777-
;;
3778-
esac
3810+
wifi_attempt=$((wifi_attempt + 1))
3811+
done
37793812
fi
37803813

37813814
# -------- DHCP/route/DNS fixup (udhcpc script) --------

0 commit comments

Comments
 (0)