From 72de503d3918506b17caba3265c593f433037d85 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 13 Mar 2017 14:48:06 +0100 Subject: [PATCH] manager: simplify searching assumed connection Now we only search for a candiate with matching UUID. No need to first lookup all activatable connections, just find the candidate by UUID and see if it is activatable. --- src/NetworkManagerUtils.c | 10 +++--- src/NetworkManagerUtils.h | 2 +- src/nm-manager.c | 22 ++++++------- src/tests/test-general.c | 66 ++++++++++++++++++++++++++------------- 4 files changed, 61 insertions(+), 39 deletions(-) diff --git a/src/NetworkManagerUtils.c b/src/NetworkManagerUtils.c index 5f40d392af..fbca3b82f3 100644 --- a/src/NetworkManagerUtils.c +++ b/src/NetworkManagerUtils.c @@ -715,7 +715,7 @@ check_possible_match (NMConnection *orig, * matches well enough. */ NMConnection * -nm_utils_match_connection (GSList *connections, +nm_utils_match_connection (NMConnection *const*connections, NMConnection *original, gboolean device_has_carrier, gint64 default_v4_metric, @@ -724,10 +724,12 @@ nm_utils_match_connection (GSList *connections, gpointer match_filter_data) { NMConnection *best_match = NULL; - GSList *iter; - for (iter = connections; iter; iter = iter->next) { - NMConnection *candidate = NM_CONNECTION (iter->data); + if (!connections) + return NULL; + + for (; *connections; connections++) { + NMConnection *candidate = NM_CONNECTION (*connections); GHashTable *diffs = NULL; if (match_filter_func) { diff --git a/src/NetworkManagerUtils.h b/src/NetworkManagerUtils.h index 9b5b9106fd..c20f2439d4 100644 --- a/src/NetworkManagerUtils.h +++ b/src/NetworkManagerUtils.h @@ -39,7 +39,7 @@ void nm_utils_complete_generic (NMPlatform *platform, typedef gboolean (NMUtilsMatchFilterFunc) (NMConnection *connection, gpointer user_data); -NMConnection *nm_utils_match_connection (GSList *connections, +NMConnection *nm_utils_match_connection (NMConnection *const*connections, NMConnection *original, gboolean device_has_carrier, gint64 default_v4_metric, diff --git a/src/nm-manager.c b/src/nm-manager.c index c76ffc2a82..c7e79c092a 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1739,6 +1739,7 @@ get_existing_connection (NMManager *self, GError *error = NULL; NMDevice *master = NULL; int ifindex = nm_device_get_ifindex (device); + NMSettingsConnection *matched; if (out_generated) *out_generated = FALSE; @@ -1782,20 +1783,15 @@ get_existing_connection (NMManager *self, * When no configured connection matches the generated connection, we keep * the generated connection instead. */ - if (assume_connection_uuid) { - gs_free_slist GSList *connections = NULL; - gs_free NMSettingsConnection **cons = NULL; - NMSettingsConnection *matched; - guint i, len; + if ( assume_connection_uuid + && (matched = nm_settings_get_connection_by_uuid (priv->settings, assume_connection_uuid)) + && !active_connection_find_first (self, matched, NULL, + NM_ACTIVE_CONNECTION_STATE_DEACTIVATING)) { + NMConnection *const connections[] = { + NM_CONNECTION (matched), + NULL, + }; - cons = nm_manager_get_activatable_connections (self, &len, FALSE); - for (i = 0; i < len; i++) { - if (nm_streq0 (assume_connection_uuid, nm_connection_get_uuid (NM_CONNECTION (cons[i])))) { - connections = g_slist_append (NULL, cons[i]); - break; - } - } - connections = g_slist_sort (connections, (GCompareFunc) nm_settings_connection_cmp_timestamp); matched = NM_SETTINGS_CONNECTION (nm_utils_match_connection (connections, connection, nm_device_has_carrier (device), diff --git a/src/tests/test-general.c b/src/tests/test-general.c index 643c912fa2..cecce83f2c 100644 --- a/src/tests/test-general.c +++ b/src/tests/test-general.c @@ -309,6 +309,30 @@ _match_connection_new (void) return connection; } +static NMConnection * +_match_connection (GSList *connections, + NMConnection *original, + gboolean device_has_carrier, + gint64 default_v4_metric, + gint64 default_v6_metric) +{ + NMConnection **list; + guint i, len; + + len = g_slist_length (connections); + g_assert (len < 10); + + list = g_alloca ((len + 1) * sizeof (NMConnection *)); + for (i = 0; i < len; i++, connections = connections->next) { + g_assert (connections); + g_assert (connections->data); + list[i] = connections->data; + } + list[i] = NULL; + + return nm_utils_match_connection (list, original, device_has_carrier, default_v4_metric, default_v6_metric, NULL, NULL); +} + static void test_connection_match_basic (void) { @@ -320,7 +344,7 @@ test_connection_match_basic (void) copy = nm_simple_connection_new_clone (orig); connections = g_slist_append (connections, copy); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == copy); /* Now change a material property like IPv4 method and ensure matching fails */ @@ -329,7 +353,7 @@ test_connection_match_basic (void) g_object_set (G_OBJECT (s_ip4), NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == NULL); g_slist_free (connections); @@ -365,7 +389,7 @@ test_connection_match_ip6_method (void) NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == copy); g_slist_free (connections); @@ -399,7 +423,7 @@ test_connection_match_ip6_method_ignore (void) NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == copy); g_slist_free (connections); @@ -433,7 +457,7 @@ test_connection_match_ip6_method_ignore_auto (void) NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE, NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == copy); g_slist_free (connections); @@ -469,11 +493,11 @@ test_connection_match_ip4_method (void) NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NULL); - matched = nm_utils_match_connection (connections, orig, FALSE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, FALSE, 0, 0); g_assert (matched == copy); /* Ensure when carrier=true matching fails */ - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == NULL); g_slist_free (connections); @@ -507,7 +531,7 @@ test_connection_match_interface_name (void) NM_SETTING_CONNECTION_INTERFACE_NAME, NULL, NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == copy); g_slist_free (connections); @@ -544,7 +568,7 @@ test_connection_match_wired (void) NM_SETTING_WIRED_S390_NETTYPE, "qeth", NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == copy); g_slist_free (connections); @@ -576,7 +600,7 @@ test_connection_match_wired2 (void) * the connections match. It can happen if assuming VLAN devices. */ nm_connection_remove_setting (orig, NM_TYPE_SETTING_WIRED); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == copy); g_slist_free (connections); @@ -601,7 +625,7 @@ test_connection_match_cloned_mac (void) NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "52:54:00:ab:db:23", NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == fuzzy); exact = nm_simple_connection_new_clone (orig); @@ -612,14 +636,14 @@ test_connection_match_cloned_mac (void) NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "52:54:00:ab:db:23", NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == exact); g_object_set (G_OBJECT (s_wired), NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "52:54:00:ab:db:24", NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched == fuzzy); g_slist_free (connections); @@ -679,7 +703,7 @@ test_connection_no_match_ip4_addr (void) nm_setting_ip_config_add_address (s_ip4, nm_addr); nm_ip_address_unref (nm_addr); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched != copy); g_slist_free (connections); @@ -725,7 +749,7 @@ test_connection_no_match_vlan (void) NM_SETTING_VLAN_FLAGS, 0, NULL); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched != copy); /* Check that the connections do not match if VLAN priorities differ */ @@ -735,7 +759,7 @@ test_connection_no_match_vlan (void) g_object_set (G_OBJECT (s_vlan_copy), NM_SETTING_VLAN_FLAGS, 0, NULL); nm_setting_vlan_add_priority_str (s_vlan_copy, NM_VLAN_INGRESS_MAP, "4:2"); - matched = nm_utils_match_connection (connections, orig, TRUE, 0, 0, NULL, NULL); + matched = _match_connection (connections, orig, TRUE, 0, 0); g_assert (matched != copy); g_slist_free (connections); @@ -775,7 +799,7 @@ test_connection_match_ip4_routes1 (void) nmtst_setting_ip_config_add_route (s_ip4, "172.25.17.0", 24, "10.0.0.3", 20); /* Try to match the connections */ - matched = nm_utils_match_connection (connections, orig, FALSE, 100, 0, NULL, NULL); + matched = _match_connection (connections, orig, FALSE, 100, 0); g_assert (matched == NULL); } @@ -812,9 +836,9 @@ test_connection_match_ip4_routes2 (void) nmtst_setting_ip_config_add_route (s_ip4, "172.25.16.0", 24, "10.0.0.2", 100); /* Try to match the connections using different default metrics */ - matched = nm_utils_match_connection (connections, orig, FALSE, 100, 0, NULL, NULL); + matched = _match_connection (connections, orig, FALSE, 100, 0); g_assert (matched == copy); - matched = nm_utils_match_connection (connections, orig, FALSE, 500, 0, NULL, NULL); + matched = _match_connection (connections, orig, FALSE, 500, 0); g_assert (matched == NULL); } @@ -849,9 +873,9 @@ test_connection_match_ip6_routes (void) nmtst_setting_ip_config_add_route (s_ip6, "2001:db8:a:b:0:0:0:0", 64, "fd01::16", 50); /* Try to match the connections */ - matched = nm_utils_match_connection (connections, orig, FALSE, 0, 100, NULL, NULL); + matched = _match_connection (connections, orig, FALSE, 0, 100); g_assert (matched == NULL); - matched = nm_utils_match_connection (connections, orig, FALSE, 0, 50, NULL, NULL); + matched = _match_connection (connections, orig, FALSE, 0, 50); g_assert (matched == copy); }