mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2026-02-04 04:50:30 +01:00
cli: deduplicate active connections sort
Both connections and devices need to sort active connections. Make the more sensible policy default.
This commit is contained in:
parent
522fd14251
commit
13d2d332dd
4 changed files with 6282 additions and 6089 deletions
|
|
@ -144,12 +144,65 @@ active_connection_get_state_ord (NMActiveConnection *active)
|
|||
int
|
||||
nmc_active_connection_cmp (NMActiveConnection *ac_a, NMActiveConnection *ac_b)
|
||||
{
|
||||
NMSettingIPConfig *s_ip;
|
||||
NMRemoteConnection *conn;
|
||||
NMIPConfig *da_ip;
|
||||
NMIPConfig *db_ip;
|
||||
int da_num_addrs;
|
||||
int db_num_addrs;
|
||||
int cmp = 0;
|
||||
|
||||
/* Non-active sort last. */
|
||||
NM_CMP_SELF (ac_a, ac_b);
|
||||
NM_CMP_DIRECT (active_connection_get_state_ord (ac_b),
|
||||
active_connection_get_state_ord (ac_a));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_active_connection_get_id (ac_a), nm_active_connection_get_id (ac_b));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_active_connection_get_connection_type (ac_a), nm_active_connection_get_connection_type (ac_b));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_object_get_path (NM_OBJECT (ac_a)), nm_object_get_path (NM_OBJECT (ac_b)));
|
||||
|
||||
/* Shared connections (likely hotspots) go on the top if possible */
|
||||
conn = nm_active_connection_get_connection (ac_a);
|
||||
s_ip = conn ? nm_connection_get_setting_ip6_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp++;
|
||||
conn = nm_active_connection_get_connection (ac_b);
|
||||
s_ip = conn ? nm_connection_get_setting_ip6_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp--;
|
||||
NM_CMP_RETURN (cmp);
|
||||
|
||||
conn = nm_active_connection_get_connection (ac_a);
|
||||
s_ip = conn ? nm_connection_get_setting_ip4_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp++;
|
||||
conn = nm_active_connection_get_connection (ac_b);
|
||||
s_ip = conn ? nm_connection_get_setting_ip4_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp--;
|
||||
NM_CMP_RETURN (cmp);
|
||||
|
||||
/* VPNs go next */
|
||||
NM_CMP_DIRECT (!!nm_active_connection_get_vpn (ac_a),
|
||||
!!nm_active_connection_get_vpn (ac_b));
|
||||
|
||||
/* Default devices are prioritized */
|
||||
NM_CMP_DIRECT (nm_active_connection_get_default (ac_a),
|
||||
nm_active_connection_get_default (ac_b));
|
||||
|
||||
/* Default IPv6 devices are prioritized */
|
||||
NM_CMP_DIRECT (nm_active_connection_get_default6 (ac_a),
|
||||
nm_active_connection_get_default6 (ac_b));
|
||||
|
||||
/* Sort by number of addresses. */
|
||||
da_ip = nm_active_connection_get_ip4_config (ac_a);
|
||||
da_num_addrs = da_ip ? nm_ip_config_get_addresses (da_ip)->len : 0;
|
||||
db_ip = nm_active_connection_get_ip4_config (ac_b);
|
||||
db_num_addrs = db_ip ? nm_ip_config_get_addresses (db_ip)->len : 0;
|
||||
|
||||
da_ip = nm_active_connection_get_ip6_config (ac_a);
|
||||
da_num_addrs += da_ip ? nm_ip_config_get_addresses (da_ip)->len : 0;
|
||||
db_ip = nm_active_connection_get_ip6_config (ac_b);
|
||||
db_num_addrs += db_ip ? nm_ip_config_get_addresses (db_ip)->len : 0;
|
||||
|
||||
NM_CMP_DIRECT (da_num_addrs, db_num_addrs);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -1144,12 +1197,20 @@ construct_header_name (const char *base, const char *spec)
|
|||
}
|
||||
|
||||
static int
|
||||
get_ac_for_connection_cmp (gconstpointer pa, gconstpointer pb, gpointer user_data)
|
||||
get_ac_for_connection_cmp (gconstpointer pa, gconstpointer pb)
|
||||
{
|
||||
NMActiveConnection *ac_a = *((NMActiveConnection *const*) pa);
|
||||
NMActiveConnection *ac_b = *((NMActiveConnection *const*) pb);
|
||||
|
||||
return nmc_active_connection_cmp (ac_a, ac_b);
|
||||
NM_CMP_RETURN (nmc_active_connection_cmp (ac_a, ac_b));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_active_connection_get_id (ac_a),
|
||||
nm_active_connection_get_id (ac_b));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_active_connection_get_connection_type (ac_a),
|
||||
nm_active_connection_get_connection_type (ac_b));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_object_get_path (NM_OBJECT (ac_a)),
|
||||
nm_object_get_path (NM_OBJECT (ac_b)));
|
||||
|
||||
g_return_val_if_reached (0);
|
||||
}
|
||||
|
||||
static NMActiveConnection *
|
||||
|
|
@ -1175,7 +1236,7 @@ get_ac_for_connection (const GPtrArray *active_cons, NMConnection *connection, G
|
|||
}
|
||||
|
||||
if (result) {
|
||||
g_ptr_array_sort_with_data (result, get_ac_for_connection_cmp, NULL);
|
||||
g_ptr_array_sort (result, get_ac_for_connection_cmp);
|
||||
best_candidate = result->pdata[0];
|
||||
}
|
||||
|
||||
|
|
@ -1676,13 +1737,12 @@ con_show_get_items_cmp (gconstpointer pa, gconstpointer pb, gpointer user_data)
|
|||
nm_connection_get_uuid (c_b));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_connection_get_path (c_a),
|
||||
nm_connection_get_path (c_b));
|
||||
|
||||
/* This line is not expected to be reached, because there shouldn't be two
|
||||
* different connections with the same path. Anyway, fall-through and compare by
|
||||
* active connections... */
|
||||
}
|
||||
|
||||
return nmc_active_connection_cmp (ac_a, ac_b);
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_object_get_path (NM_OBJECT (ac_a)),
|
||||
nm_object_get_path (NM_OBJECT (ac_b)));
|
||||
|
||||
g_return_val_if_reached (0);
|
||||
}
|
||||
|
||||
static GPtrArray *
|
||||
|
|
|
|||
|
|
@ -903,102 +903,19 @@ compare_devices (const void *a, const void *b)
|
|||
{
|
||||
NMDevice *da = *(NMDevice **)a;
|
||||
NMDevice *db = *(NMDevice **)b;
|
||||
NMActiveConnection *da_ac;
|
||||
NMActiveConnection *db_ac;
|
||||
NMSettingIPConfig *s_ip;
|
||||
NMRemoteConnection *conn;
|
||||
NMIPConfig *da_ip;
|
||||
NMIPConfig *db_ip;
|
||||
int da_num_addrs;
|
||||
int db_num_addrs;
|
||||
int cmp;
|
||||
NMActiveConnection *da_ac = nm_device_get_active_connection (da);
|
||||
NMActiveConnection *db_ac = nm_device_get_active_connection (db);
|
||||
|
||||
/* Sort by later device states first */
|
||||
cmp = nm_device_get_state (db) - nm_device_get_state (da);
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
NM_CMP_DIRECT (nm_device_get_state (db), nm_device_get_state (da));
|
||||
NM_CMP_RETURN (nmc_active_connection_cmp (db_ac, da_ac));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_device_get_type_description (da),
|
||||
nm_device_get_type_description (db));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_device_get_iface (da),
|
||||
nm_device_get_iface (db));
|
||||
NM_CMP_DIRECT_STRCMP0 (nm_object_get_path (NM_OBJECT (da)),
|
||||
nm_object_get_path (NM_OBJECT (db)));
|
||||
|
||||
da_ac = nm_device_get_active_connection (da);
|
||||
db_ac = nm_device_get_active_connection (db);
|
||||
|
||||
/* Prioritize devices with active connections */
|
||||
if (da_ac)
|
||||
cmp++;
|
||||
if (db_ac)
|
||||
cmp--;
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
/* Shared connections (likely hotspots) go on the top if possible */
|
||||
conn = da_ac ? nm_active_connection_get_connection (da_ac) : NULL;
|
||||
s_ip = conn ? nm_connection_get_setting_ip6_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp--;
|
||||
conn = db_ac ? nm_active_connection_get_connection (db_ac) : NULL;
|
||||
s_ip = conn ? nm_connection_get_setting_ip6_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP6_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp++;
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
conn = da_ac ? nm_active_connection_get_connection (da_ac) : NULL;
|
||||
s_ip = conn ? nm_connection_get_setting_ip4_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp--;
|
||||
conn = db_ac ? nm_active_connection_get_connection (db_ac) : NULL;
|
||||
s_ip = conn ? nm_connection_get_setting_ip4_config (NM_CONNECTION (conn)) : NULL;
|
||||
if (s_ip && strcmp (nm_setting_ip_config_get_method (s_ip), NM_SETTING_IP4_CONFIG_METHOD_SHARED) == 0)
|
||||
cmp++;
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
/* VPNs go next */
|
||||
if (da_ac && !nm_active_connection_get_vpn (da_ac))
|
||||
cmp++;
|
||||
if (db_ac && !nm_active_connection_get_vpn (db_ac))
|
||||
cmp--;
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
/* Default devices are prioritized */
|
||||
if (da_ac && !nm_active_connection_get_default (da_ac))
|
||||
cmp++;
|
||||
if (db_ac && !nm_active_connection_get_default (db_ac))
|
||||
cmp--;
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
/* Default IPv6 devices are prioritized */
|
||||
if (da_ac && !nm_active_connection_get_default6 (da_ac))
|
||||
cmp++;
|
||||
if (db_ac && !nm_active_connection_get_default6 (db_ac))
|
||||
cmp--;
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
/* Sort by number of addresses. */
|
||||
da_ip = da_ac ? nm_active_connection_get_ip4_config (da_ac) : NULL;
|
||||
da_num_addrs = da_ip ? nm_ip_config_get_addresses (da_ip)->len : 0;
|
||||
db_ip = db_ac ? nm_active_connection_get_ip4_config (db_ac) : NULL;
|
||||
db_num_addrs = db_ip ? nm_ip_config_get_addresses (db_ip)->len : 0;
|
||||
|
||||
da_ip = da_ac ? nm_active_connection_get_ip6_config (da_ac) : NULL;
|
||||
da_num_addrs += da_ip ? nm_ip_config_get_addresses (da_ip)->len : 0;
|
||||
db_ip = db_ac ? nm_active_connection_get_ip6_config (db_ac) : NULL;
|
||||
db_num_addrs += db_ip ? nm_ip_config_get_addresses (db_ip)->len : 0;
|
||||
|
||||
cmp = db_num_addrs - da_num_addrs;
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
/* Fall back to alphanumeric sort by description and interface. */
|
||||
cmp = g_strcmp0 (nm_device_get_type_description (da),
|
||||
nm_device_get_type_description (db));
|
||||
if (cmp != 0)
|
||||
return cmp;
|
||||
|
||||
return g_strcmp0 (nm_device_get_iface (da),
|
||||
nm_device_get_iface (db));
|
||||
g_return_val_if_reached (0);
|
||||
}
|
||||
|
||||
NMDevice **
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Add table
Reference in a new issue