diff options
author | Thomas Haller <thaller@redhat.com> | 2021-09-21 08:31:27 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2021-09-21 08:31:27 +0200 |
commit | c52bf43948e16a518a279882a6b16f5635acb672 (patch) | |
tree | 802c4bd0174ca08eaca75ccec119bf9044fc049b | |
parent | a8866095dd656ce187c4ed2951f60ebac7f440ff (diff) | |
parent | abeedad315297820947518ec6867ca5fd189f5be (diff) | |
download | NetworkManager-c52bf43948e16a518a279882a6b16f5635acb672.tar.gz |
initrd: merge branch 'th/initrd-ethtool'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/983
-rw-r--r-- | src/nm-initrd-generator/nmi-cmdline-reader.c | 76 | ||||
-rw-r--r-- | src/nm-initrd-generator/tests/test-cmdline-reader.c | 290 |
2 files changed, 154 insertions, 212 deletions
diff --git a/src/nm-initrd-generator/nmi-cmdline-reader.c b/src/nm-initrd-generator/nmi-cmdline-reader.c index 2afdb8dedd..ced1eeddf2 100644 --- a/src/nm-initrd-generator/nmi-cmdline-reader.c +++ b/src/nm-initrd-generator/nmi-cmdline-reader.c @@ -1173,60 +1173,60 @@ reader_parse_rd_znet(Reader *reader, char *argument, gboolean net_ifnames) static void reader_parse_ethtool(Reader *reader, char *argument) { - const char * interface = NULL; - NMConnection * connection = NULL; - NMSettingWired *s_wired = NULL; - const char * autoneg_str = NULL; - gboolean autoneg = FALSE; - const char * speed_str = NULL; - guint speed = 0; + NMConnection * connection; + NMSettingWired *s_wired; + const char * autoneg_str; + const char * speed_str; + const char * interface; + int autoneg; + guint speed; interface = get_word(&argument, ':'); if (!interface) { - _LOGW(LOGD_CORE, "Impossible to set rd.ethtool options: invalid format"); + _LOGW(LOGD_CORE, "rd.ethtool: interface unspecified. Ignore"); return; } - if (!*argument) { - _LOGW(LOGD_CORE, "Could not find rd.ethtool options to set"); - return; - } - - connection = reader_get_connection(reader, interface, NM_SETTING_WIRED_SETTING_NAME, TRUE); - s_wired = nm_connection_get_setting_wired(connection); - autoneg_str = get_word(&argument, ':'); + speed_str = get_word(&argument, ':'); + + autoneg = -1; if (autoneg_str) { autoneg = _nm_utils_ascii_str_to_bool(autoneg_str, -1); if (autoneg == -1) - _LOGW(LOGD_CORE, - "Invalid value for rd.ethtool.autoneg, rd.ethtool.autoneg was not set"); - else - g_object_set(s_wired, NM_SETTING_WIRED_AUTO_NEGOTIATE, autoneg, NULL); + _LOGW(LOGD_CORE, "rd.ethtool: autoneg invalid. Must be boolean or empty"); } - if (!*argument) - return; - speed_str = get_word(&argument, ':'); + speed = 0; if (speed_str) { - speed = _nm_utils_ascii_str_to_int64(speed_str, 10, 0, G_MAXUINT32, -1); - if (speed == -1) - _LOGW(LOGD_CORE, "Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - else - g_object_set(s_wired, - NM_SETTING_WIRED_SPEED, - speed, - NM_SETTING_WIRED_DUPLEX, - "full", - NULL); + speed = _nm_utils_ascii_str_to_int64(speed_str, 10, 0, G_MAXUINT32, 0); + if (errno) + _LOGW(LOGD_CORE, "rd.ethtool: speed invalid. Must be an integer or empty"); } - if (!*argument) - return; - else + if (speed == 0 && autoneg == FALSE) { _LOGW(LOGD_CORE, - "Invalid extra argument '%s' for rd.ethtool, this value was not set", - argument); + "rd.ethtool: autoneg ignored. Cannot disable autoneg without setting speed"); + } + + connection = reader_get_connection(reader, interface, NM_SETTING_WIRED_SETTING_NAME, TRUE); + + if (autoneg != -1 || speed != 0) { + if (autoneg == -1) + autoneg = FALSE; + s_wired = nm_connection_get_setting_wired(connection); + g_object_set(s_wired, + NM_SETTING_WIRED_AUTO_NEGOTIATE, + (gboolean) autoneg, + NM_SETTING_WIRED_SPEED, + speed, + NM_SETTING_WIRED_DUPLEX, + speed == 0 ? NULL : "full", + NULL); + } + + if (*argument) + _LOGW(LOGD_CORE, "rd.ethtool: extra argument ignored"); } static void diff --git a/src/nm-initrd-generator/tests/test-cmdline-reader.c b/src/nm-initrd-generator/tests/test-cmdline-reader.c index 6bc3052265..d8bbb5e86b 100644 --- a/src/nm-initrd-generator/tests/test-cmdline-reader.c +++ b/src/nm-initrd-generator/tests/test-cmdline-reader.c @@ -2272,203 +2272,145 @@ test_carrier_timeout(void) g_assert_cmpint(carrier_timeout_sec, ==, 20); } -/* Obs1.: this function is implemented as macro, and not as a function, - * to show the correct line in g_assert() debug */ -#define _ethtool_connection_check_and_get(connection) \ - ({ \ - NMSettingWired *_s_wired = NULL; \ - NMConnection * _connection = connection; \ - \ - g_assert(nm_connection_get_setting_connection(_connection)); \ - g_assert(nm_connection_is_type(_connection, NM_SETTING_WIRED_SETTING_NAME)); \ - g_assert(nm_connection_get_setting_ip4_config(_connection)); \ - g_assert(nm_connection_get_setting_ip6_config(_connection)); \ - _s_wired = nm_connection_get_setting_wired(_connection); \ - g_assert(NM_IS_SETTING_WIRED(_s_wired)); \ - \ - _s_wired; \ - }) +#define _ethtool_check_inval(arg) \ + G_STMT_START \ + { \ + gs_unref_hashtable GHashTable *_connections2 = NULL; \ + \ + _connections2 = _parse_cons(NM_MAKE_STRV(arg)); \ + \ + g_test_assert_expected_messages(); \ + \ + g_assert_cmpint(g_hash_table_size(_connections2), ==, 0); \ + } \ + G_STMT_END + +#define _ethtool_check_v(strv, autoneg, speed) \ + G_STMT_START \ + { \ + gs_unref_object NMConnection *_connection = NULL; \ + NMSettingWired * _s_wired; \ + typeof(speed) _speed = speed; \ + \ + _connection = _parse_con(strv, "eth0"); \ + \ + g_test_assert_expected_messages(); \ + \ + g_assert(nm_connection_get_setting_connection(_connection)); \ + g_assert(nm_connection_is_type(_connection, NM_SETTING_WIRED_SETTING_NAME)); \ + g_assert(nm_connection_get_setting_ip4_config(_connection)); \ + g_assert(nm_connection_get_setting_ip6_config(_connection)); \ + _s_wired = nm_connection_get_setting_wired(_connection); \ + g_assert(NM_IS_SETTING_WIRED(_s_wired)); \ + \ + g_assert_cmpint(nm_setting_wired_get_auto_negotiate(_s_wired), ==, (autoneg)); \ + g_assert_cmpint(nm_setting_wired_get_speed(_s_wired), ==, _speed); \ + g_assert_cmpstr(nm_setting_wired_get_duplex(_s_wired), ==, (_speed == 0 ? NULL : "full")); \ + } \ + G_STMT_END + +#define _ethtool_check(arg, autoneg, speed) \ + _ethtool_check_v(NM_MAKE_STRV("" arg ""), (autoneg), (speed)) static void test_rd_ethtool(void) { - const char *const *ARGV = NULL; - NMConnection * connection = NULL; - GHashTable * connections = NULL; - NMSettingWired * s_wired = NULL; + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: interface unspecified. Ignore"); + _ethtool_check_inval("rd.ethtool="); - ARGV = NM_MAKE_STRV("rd.ethtool="); - NMTST_EXPECT_NM_WARN("cmdline-reader: Impossible to set rd.ethtool options: invalid format"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_hash_table_unref(connections); - g_test_assert_expected_messages(); + _ethtool_check("rd.ethtool=eth0", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0"); - NMTST_EXPECT_NM_WARN("cmdline-reader: Could not find rd.ethtool options to set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_hash_table_unref(connections); - g_test_assert_expected_messages(); + _ethtool_check("rd.ethtool=eth0:", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:"); - NMTST_EXPECT_NM_WARN("cmdline-reader: Could not find rd.ethtool options to set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_hash_table_unref(connections); - g_test_assert_expected_messages(); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: interface unspecified. Ignore"); + _ethtool_check_inval("rd.ethtool=::"); - ARGV = NM_MAKE_STRV("rd.ethtool=::"); - NMTST_EXPECT_NM_WARN("cmdline-reader: Impossible to set rd.ethtool options: invalid format"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0:on", TRUE, 0); + _ethtool_check("rd.ethtool=eth0:on:", TRUE, 0); + _ethtool_check("rd.ethtool=eth0:on::", TRUE, 0); + _ethtool_check("rd.ethtool=eth0:on:0:", TRUE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:on"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check("rd.ethtool=eth0:off", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:off"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:true", TRUE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:true"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check("rd.ethtool=eth0:false", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:false"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + _ethtool_check("rd.ethtool=eth0:1", TRUE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:1"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check("rd.ethtool=eth0:0", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:0"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_object_unref(connection); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg invalid. Must be boolean or empty"); + _ethtool_check("rd.ethtool=eth0:randomstring", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:randomstring"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid value for rd.ethtool.autoneg, rd.ethtool.autoneg was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0::", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_hash_table_unref(connections); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: speed invalid. Must be an integer or empty"); + _ethtool_check("rd.ethtool=eth0::astring", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::astring"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: speed invalid. Must be an integer or empty"); + _ethtool_check("rd.ethtool=eth0::1000000000000000000000000000000000000", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::1000000000000000000000000000000000000"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: speed invalid. Must be an integer or empty"); + _ethtool_check("rd.ethtool=eth0::0.67", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::0.67"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: speed invalid. Must be an integer or empty"); + _ethtool_check("rd.ethtool=eth0::-23", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::-23"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid value for rd.ethtool.speed, rd.ethtool.speed was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: speed invalid. Must be an integer or empty"); + _ethtool_check("rd.ethtool=eth0::-23:", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:1:10"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 10); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_object_unref(connection); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: speed invalid. Must be an integer or empty"); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: extra argument ignored"); + _ethtool_check("rd.ethtool=eth0::-23::", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::100"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 100); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_object_unref(connection); - - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:::bogus"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 1); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: speed invalid. Must be an integer or empty"); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: extra argument ignored"); + _ethtool_check("rd.ethtool=eth0::-23::foo", FALSE, 0); - ARGV = NM_MAKE_STRV("rd.ethtool=eth0::10:bogus"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(!nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 10); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_test_assert_expected_messages(); - g_object_unref(connection); - - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:on:100:bogus"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 100); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_test_assert_expected_messages(); - g_object_unref(connection); - - ARGV = NM_MAKE_STRV("rd.ethtool=eth0:on:100:bogus"); - NMTST_EXPECT_NM_WARN( - "cmdline-reader: Invalid extra argument 'bogus' for rd.ethtool, this value was not set"); - connection = _parse_con(ARGV, "eth0"); - s_wired = _ethtool_connection_check_and_get(connection); - g_assert(nm_setting_wired_get_auto_negotiate(s_wired)); - g_assert_cmpint(nm_setting_wired_get_speed(s_wired), ==, 100); - g_assert_cmpstr(nm_setting_wired_get_duplex(s_wired), ==, "full"); - g_test_assert_expected_messages(); - g_object_unref(connection); - - ARGV = NM_MAKE_STRV("rd.ethtool=:::"); - NMTST_EXPECT_NM_WARN("cmdline-reader: Impossible to set rd.ethtool options: invalid format"); - connections = _parse_cons(ARGV); - g_assert_cmpint(g_hash_table_size(connections), ==, 0); - g_test_assert_expected_messages(); - g_hash_table_unref(connections); + _ethtool_check("rd.ethtool=eth0:1:10", TRUE, 10); + + _ethtool_check("rd.ethtool=eth0::100", FALSE, 100); + + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: extra argument ignored"); + _ethtool_check("rd.ethtool=eth0:::bogus", FALSE, 0); + + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: extra argument ignored"); + _ethtool_check("rd.ethtool=eth0::10:bogus", FALSE, 10); + + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: extra argument ignored"); + _ethtool_check("rd.ethtool=eth0:on:100:bogus", TRUE, 100); + + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: interface unspecified. Ignore"); + _ethtool_check_inval("rd.ethtool=:::"); + + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:off:0", "rd.ethtool=eth0:on"), TRUE, 0); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:off:0", "rd.ethtool=eth0:off"), FALSE, 0); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:on:0", "rd.ethtool=eth0:on"), TRUE, 0); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:on:0", "rd.ethtool=eth0:off"), FALSE, 0); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:off:100", "rd.ethtool=eth0:on"), TRUE, 0); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:off:100", "rd.ethtool=eth0:off"), FALSE, 0); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:on:100", "rd.ethtool=eth0:on"), TRUE, 0); + NMTST_EXPECT_NM_WARN("cmdline-reader: rd.ethtool: autoneg ignored. Cannot disable autoneg " + "without setting speed"); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:on:100", "rd.ethtool=eth0:off"), FALSE, 0); + _ethtool_check_v(NM_MAKE_STRV("rd.ethtool=eth0:off:100", "rd.ethtool=eth0:"), FALSE, 100); } /*****************************************************************************/ |