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.
This commit is contained in:
Thomas Haller
2017-03-13 14:48:06 +01:00
parent 02f6bfeae2
commit 72de503d39
4 changed files with 61 additions and 39 deletions

View File

@@ -715,7 +715,7 @@ check_possible_match (NMConnection *orig,
* matches well enough. * matches well enough.
*/ */
NMConnection * NMConnection *
nm_utils_match_connection (GSList *connections, nm_utils_match_connection (NMConnection *const*connections,
NMConnection *original, NMConnection *original,
gboolean device_has_carrier, gboolean device_has_carrier,
gint64 default_v4_metric, gint64 default_v4_metric,
@@ -724,10 +724,12 @@ nm_utils_match_connection (GSList *connections,
gpointer match_filter_data) gpointer match_filter_data)
{ {
NMConnection *best_match = NULL; NMConnection *best_match = NULL;
GSList *iter;
for (iter = connections; iter; iter = iter->next) { if (!connections)
NMConnection *candidate = NM_CONNECTION (iter->data); return NULL;
for (; *connections; connections++) {
NMConnection *candidate = NM_CONNECTION (*connections);
GHashTable *diffs = NULL; GHashTable *diffs = NULL;
if (match_filter_func) { if (match_filter_func) {

View File

@@ -39,7 +39,7 @@ void nm_utils_complete_generic (NMPlatform *platform,
typedef gboolean (NMUtilsMatchFilterFunc) (NMConnection *connection, gpointer user_data); 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, NMConnection *original,
gboolean device_has_carrier, gboolean device_has_carrier,
gint64 default_v4_metric, gint64 default_v4_metric,

View File

@@ -1739,6 +1739,7 @@ get_existing_connection (NMManager *self,
GError *error = NULL; GError *error = NULL;
NMDevice *master = NULL; NMDevice *master = NULL;
int ifindex = nm_device_get_ifindex (device); int ifindex = nm_device_get_ifindex (device);
NMSettingsConnection *matched;
if (out_generated) if (out_generated)
*out_generated = FALSE; *out_generated = FALSE;
@@ -1782,20 +1783,15 @@ get_existing_connection (NMManager *self,
* When no configured connection matches the generated connection, we keep * When no configured connection matches the generated connection, we keep
* the generated connection instead. * the generated connection instead.
*/ */
if (assume_connection_uuid) { if ( assume_connection_uuid
gs_free_slist GSList *connections = NULL; && (matched = nm_settings_get_connection_by_uuid (priv->settings, assume_connection_uuid))
gs_free NMSettingsConnection **cons = NULL; && !active_connection_find_first (self, matched, NULL,
NMSettingsConnection *matched; NM_ACTIVE_CONNECTION_STATE_DEACTIVATING)) {
guint i, len; 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, matched = NM_SETTINGS_CONNECTION (nm_utils_match_connection (connections,
connection, connection,
nm_device_has_carrier (device), nm_device_has_carrier (device),

View File

@@ -309,6 +309,30 @@ _match_connection_new (void)
return connection; 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 static void
test_connection_match_basic (void) test_connection_match_basic (void)
{ {
@@ -320,7 +344,7 @@ test_connection_match_basic (void)
copy = nm_simple_connection_new_clone (orig); copy = nm_simple_connection_new_clone (orig);
connections = g_slist_append (connections, copy); 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); g_assert (matched == copy);
/* Now change a material property like IPv4 method and ensure matching fails */ /* 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), g_object_set (G_OBJECT (s_ip4),
NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP4_CONFIG_METHOD_LINK_LOCAL,
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 == NULL); g_assert (matched == NULL);
g_slist_free (connections); g_slist_free (connections);
@@ -365,7 +389,7 @@ test_connection_match_ip6_method (void)
NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
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_assert (matched == copy);
g_slist_free (connections); 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, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
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_assert (matched == copy);
g_slist_free (connections); 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, NM_SETTING_IP_CONFIG_METHOD, NM_SETTING_IP6_CONFIG_METHOD_IGNORE,
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_assert (matched == copy);
g_slist_free (connections); g_slist_free (connections);
@@ -469,11 +493,11 @@ test_connection_match_ip4_method (void)
NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE, NM_SETTING_IP_CONFIG_MAY_FAIL, TRUE,
NULL); 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); g_assert (matched == copy);
/* Ensure when carrier=true matching fails */ /* 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_assert (matched == NULL);
g_slist_free (connections); g_slist_free (connections);
@@ -507,7 +531,7 @@ test_connection_match_interface_name (void)
NM_SETTING_CONNECTION_INTERFACE_NAME, NULL, NM_SETTING_CONNECTION_INTERFACE_NAME, NULL,
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_assert (matched == copy);
g_slist_free (connections); g_slist_free (connections);
@@ -544,7 +568,7 @@ test_connection_match_wired (void)
NM_SETTING_WIRED_S390_NETTYPE, "qeth", NM_SETTING_WIRED_S390_NETTYPE, "qeth",
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_assert (matched == copy);
g_slist_free (connections); g_slist_free (connections);
@@ -576,7 +600,7 @@ test_connection_match_wired2 (void)
* the connections match. It can happen if assuming VLAN devices. */ * the connections match. It can happen if assuming VLAN devices. */
nm_connection_remove_setting (orig, NM_TYPE_SETTING_WIRED); 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_assert (matched == copy);
g_slist_free (connections); 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", NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "52:54:00:ab:db:23",
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 == fuzzy); g_assert (matched == fuzzy);
exact = nm_simple_connection_new_clone (orig); 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", NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "52:54:00:ab:db:23",
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 == exact); g_assert (matched == exact);
g_object_set (G_OBJECT (s_wired), g_object_set (G_OBJECT (s_wired),
NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "52:54:00:ab:db:24", NM_SETTING_WIRED_CLONED_MAC_ADDRESS, "52:54:00:ab:db:24",
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 == fuzzy); g_assert (matched == fuzzy);
g_slist_free (connections); 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_setting_ip_config_add_address (s_ip4, nm_addr);
nm_ip_address_unref (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_assert (matched != copy);
g_slist_free (connections); g_slist_free (connections);
@@ -725,7 +749,7 @@ test_connection_no_match_vlan (void)
NM_SETTING_VLAN_FLAGS, 0, NM_SETTING_VLAN_FLAGS, 0,
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_assert (matched != copy);
/* Check that the connections do not match if VLAN priorities differ */ /* 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); 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"); 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_assert (matched != copy);
g_slist_free (connections); 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); nmtst_setting_ip_config_add_route (s_ip4, "172.25.17.0", 24, "10.0.0.3", 20);
/* Try to match the connections */ /* 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); 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); 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 */ /* 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); 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); 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); 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 */ /* 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); 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); g_assert (matched == copy);
} }