From aee78ca788a6df66166505c6f7342655773409cc Mon Sep 17 00:00:00 2001 From: Beniamino Galvani Date: Mon, 27 Jan 2020 17:38:33 +0100 Subject: [PATCH] dhcp: derive the grace period duration from the timeout property Currently the duration of the DHCP grace period (in which we try to acquire a new lease after expiration) is hardcoded to 480 seconds. That value seems arbitrary and too long for the default configuration. Since we already have a property that allows the user to configure how long NM should try to get the lease initially, it makes sense to use it also for retries after lease expirations. In particular, setting the ipvx.dhcp-timeout to a high value extends also the grace period to a very long time, potentially forever. --- src/devices/nm-device.c | 42 ++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 8d2b4e1e28..dc6c2da95d 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -76,7 +76,7 @@ _LOG_DECLARE_SELF (NMDevice); /*****************************************************************************/ #define DEFAULT_AUTOCONNECT TRUE -#define DHCP_GRACE_PERIOD_SEC 480 +#define DHCP_GRACE_PERIOD_MULTIPLIER 2U #define CARRIER_WAIT_TIME_MS 6000 #define CARRIER_WAIT_TIME_AFTER_MTU_MS 10000 @@ -7860,12 +7860,24 @@ dhcp4_fail (NMDevice *self, NMDhcpState dhcp_state) * wait for some time before failing the IP method. */ if (!priv->dhcp4.grace_id) { - priv->dhcp4.grace_id = g_timeout_add_seconds (DHCP_GRACE_PERIOD_SEC, + guint32 timeout; + + /* Start a grace period equal to the DHCP timeout multiplied + * by a constant factor. */ + timeout = get_dhcp_timeout (self, AF_INET); + if (timeout < G_MAXUINT32 / DHCP_GRACE_PERIOD_MULTIPLIER) { + timeout *= DHCP_GRACE_PERIOD_MULTIPLIER; + _LOGI (LOGD_DHCP4, + "DHCPv4: trying to acquire a new lease within %u seconds", + timeout); + } else { + timeout = G_MAXUINT32; + _LOGI (LOGD_DHCP4, "DHCPv4: trying to acquire a new lease"); + } + + priv->dhcp4.grace_id = g_timeout_add_seconds (timeout, dhcp4_grace_period_expired, self); - _LOGI (LOGD_DHCP4, - "DHCPv4: %u seconds grace period started", - DHCP_GRACE_PERIOD_SEC); goto clear_config; } return; @@ -8653,12 +8665,24 @@ dhcp6_fail (NMDevice *self, NMDhcpState dhcp_state) * wait for some time before failing the IP method. */ if (!priv->dhcp6.grace_id) { - priv->dhcp6.grace_id = g_timeout_add_seconds (DHCP_GRACE_PERIOD_SEC, + guint32 timeout; + + /* Start a grace period equal to the DHCP timeout multiplied + * by a constant factor. */ + timeout = get_dhcp_timeout (self, AF_INET6); + if (timeout < G_MAXUINT32 / DHCP_GRACE_PERIOD_MULTIPLIER) { + timeout *= DHCP_GRACE_PERIOD_MULTIPLIER; + _LOGI (LOGD_DHCP6, + "DHCPv6: trying to acquire a new lease within %u seconds", + timeout); + } else { + timeout = G_MAXUINT32; + _LOGI (LOGD_DHCP6, "DHCPv6: trying to acquire a new lease"); + } + + priv->dhcp6.grace_id = g_timeout_add_seconds (timeout, dhcp6_grace_period_expired, self); - _LOGI (LOGD_DHCP6, - "DHCPv6: %u seconds grace period started", - DHCP_GRACE_PERIOD_SEC); goto clear_config; } } else {