initrd: set autoconnect-retries=1 and increase default DHCP timeout

By default a connection is retried 4 times before it is blocked from
autoconnecting. This means that if a user specifies an explicit DHCP
timeout in the initrd command line, NM will wait up to 4 times more.

Instead, set the "connection.autoconnect-retries" property of
connections always to 1, so that NM only waits for the time
specified.

Before this commit a default DHCP connection would take at most (45 x
4) seconds. Since the multiplier is now only 1, also increase the DHCP
timeout to have a total time of (90 x 1) seconds, which is the half
than before.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/559
This commit is contained in:
Beniamino Galvani 2021-02-12 09:50:48 +01:00
parent e258410c87
commit 7e126fe898
2 changed files with 64 additions and 1 deletions

View file

@ -53,6 +53,7 @@ reader_new(void)
g_hash_table_new_full(nm_direct_hash, NULL, g_object_unref, NULL),
.vlan_parents = g_ptr_array_new_with_free_func(g_free),
.array = g_ptr_array_new(),
.dhcp_timeout = 90,
};
return reader;
@ -147,6 +148,8 @@ reader_create_connection(Reader * reader,
type_name,
NM_SETTING_CONNECTION_MULTI_CONNECT,
multi_connect,
NM_SETTING_CONNECTION_AUTOCONNECT_RETRIES,
1,
NULL);
if (nm_streq0(type_name, NM_SETTING_INFINIBAND_SETTING_NAME)) {
@ -1075,7 +1078,8 @@ nmi_cmdline_reader_parse(const char * sysfs_dir,
else if (nm_streq(tag, "rd.peerdns"))
reader->ignore_auto_dns = !_nm_utils_ascii_str_to_bool(argument, TRUE);
else if (nm_streq(tag, "rd.net.timeout.dhcp")) {
reader->dhcp_timeout = _nm_utils_ascii_str_to_int64(argument, 10, 0, G_MAXINT32, 0);
reader->dhcp_timeout =
_nm_utils_ascii_str_to_int64(argument, 10, 1, G_MAXINT32, reader->dhcp_timeout);
} else if (nm_streq(tag, "rd.net.dhcp.vendor-class")) {
if (nm_utils_validate_dhcp4_vendor_class_id(argument, NULL))
nm_utils_strdup_reset(&reader->dhcp4_vci, argument);

View file

@ -229,11 +229,64 @@ test_dhcp_with_mtu(void)
}
}
static void
test_dhcp_timeout(void)
{
struct {
const char *const *cmdline;
int timeout;
} data[] = {
{NM_MAKE_STRV("ip=dhcp"), 90},
{NM_MAKE_STRV("ip=dhcp", "rd.net.timeout.dhcp=0"), 90},
{NM_MAKE_STRV("ip=dhcp", "rd.net.timeout.dhcp=foobar"), 90},
{NM_MAKE_STRV("ip=dhcp", "rd.net.timeout.dhcp=42"), 42},
};
guint i;
for (i = 0; i < G_N_ELEMENTS(data); i++) {
gs_unref_object NMConnection *connection = NULL;
NMSettingConnection * s_con;
NMSettingIPConfig * s_ip4;
NMSettingIPConfig * s_ip6;
connection = _parse_con(data[i].cmdline, "default_connection");
s_con = nm_connection_get_setting_connection(connection);
g_assert(s_con);
g_assert_cmpstr(nm_setting_connection_get_connection_type(s_con),
==,
NM_SETTING_WIRED_SETTING_NAME);
g_assert_cmpstr(nm_setting_connection_get_id(s_con), ==, "Wired Connection");
g_assert_cmpint(nm_setting_connection_get_timestamp(s_con), ==, 0);
g_assert_cmpint(nm_setting_connection_get_multi_connect(s_con),
==,
NM_CONNECTION_MULTI_CONNECT_MULTIPLE);
g_assert_cmpint(nm_setting_connection_get_wait_device_timeout(s_con), ==, -1);
g_assert_cmpint(nm_setting_connection_get_autoconnect_retries(s_con), ==, 1);
g_assert(nm_setting_connection_get_autoconnect(s_con));
s_ip4 = nm_connection_get_setting_ip4_config(connection);
g_assert(s_ip4);
g_assert_cmpstr(nm_setting_ip_config_get_method(s_ip4),
==,
NM_SETTING_IP4_CONFIG_METHOD_AUTO);
g_assert_cmpint(nm_setting_ip_config_get_dhcp_timeout(s_ip4), ==, data[i].timeout);
s_ip6 = nm_connection_get_setting_ip6_config(connection);
g_assert(s_ip6);
g_assert_cmpstr(nm_setting_ip_config_get_method(s_ip6),
==,
NM_SETTING_IP6_CONFIG_METHOD_AUTO);
g_assert_cmpint(nm_setting_ip_config_get_dhcp_timeout(s_ip6), ==, data[i].timeout);
}
}
static void
test_if_auto_with_mtu(void)
{
const char *const *ARGV = NM_MAKE_STRV("ip=eth0:auto:1666");
gs_unref_object NMConnection *connection = NULL;
NMSettingConnection * s_con;
NMSettingWired * s_wired;
NMSettingIPConfig * s_ip4;
NMSettingIPConfig * s_ip6;
@ -242,6 +295,10 @@ test_if_auto_with_mtu(void)
g_assert_cmpstr(nm_connection_get_id(connection), ==, "eth0");
s_con = nm_connection_get_setting_connection(connection);
g_assert(s_con);
g_assert_cmpint(nm_setting_connection_get_autoconnect_retries(s_con), ==, 1);
s_wired = nm_connection_get_setting_wired(connection);
g_assert(s_wired);
g_assert_cmpint(nm_setting_wired_get_mtu(s_wired), ==, 1666);
@ -250,6 +307,7 @@ test_if_auto_with_mtu(void)
g_assert(s_ip4);
g_assert_cmpstr(nm_setting_ip_config_get_method(s_ip4), ==, NM_SETTING_IP4_CONFIG_METHOD_AUTO);
g_assert(!nm_setting_ip_config_get_ignore_auto_dns(s_ip4));
g_assert_cmpint(nm_setting_ip_config_get_dhcp_timeout(s_ip4), ==, 90);
s_ip6 = nm_connection_get_setting_ip6_config(connection);
g_assert(s_ip6);
@ -2074,6 +2132,7 @@ main(int argc, char **argv)
g_test_add_func("/initrd/cmdline/auto", test_auto);
g_test_add_func("/initrd/cmdline/dhcp_with_hostname", test_dhcp_with_hostname);
g_test_add_func("/initrd/cmdline/dhcp_with_mtu", test_dhcp_with_mtu);
g_test_add_func("/initrd/cmdline/dhcp_timeout", test_dhcp_timeout);
g_test_add_func("/initrd/cmdline/if_auto_with_mtu", test_if_auto_with_mtu);
g_test_add_func("/initrd/cmdline/if_dhcp6", test_if_dhcp6);
g_test_add_func("/initrd/cmdline/if_auto_with_mtu_and_mac", test_if_auto_with_mtu_and_mac);