From 971bab01ea794b8664d4f812d49dcdac0175c794 Mon Sep 17 00:00:00 2001 From: Dan Winship Date: Wed, 6 Mar 2013 14:58:03 -0500 Subject: [PATCH] core: simplify/rename nm_device_connection_match_config() nm_device_connection_match_config() sounded more generic than it really was; rename it to nm_device_find_assumable_connection(), which is what it really does. There was also a lot of redundancy/cut+paste in the subclass implementations of connection_match_config(); Improve things by moving the looping-over-connections code into NMDevice itself, and also doing the general-device-compatibility and IP-config checking there, leaving the device subclasses to just verify L2 properties. Which most of them aren't doing... https://bugzilla.gnome.org/show_bug.cgi?id=693684 --- src/nm-device-bond.c | 47 +++-------------------------------- src/nm-device-bridge.c | 47 +++-------------------------------- src/nm-device-ethernet.c | 41 ++++++++----------------------- src/nm-device-infiniband.c | 48 +++--------------------------------- src/nm-device-private.h | 2 -- src/nm-device-vlan.c | 50 ++++++-------------------------------- src/nm-device-wired.c | 18 -------------- src/nm-device.c | 37 ++++++++++++++++------------ src/nm-device.h | 6 ++--- src/nm-manager.c | 2 +- 10 files changed, 52 insertions(+), 246 deletions(-) diff --git a/src/nm-device-bond.c b/src/nm-device-bond.c index 064011ee2e..341efeb61b 100644 --- a/src/nm-device-bond.c +++ b/src/nm-device-bond.c @@ -224,53 +224,12 @@ complete_connection (NMDevice *device, } static gboolean -bond_match_config (NMDevice *self, NMConnection *connection) +match_l2_config (NMDevice *self, NMConnection *connection) { - NMSettingBond *s_bond; - const char *ifname; - - s_bond = nm_connection_get_setting_bond (connection); - if (!s_bond) - return FALSE; - - /* Interface name */ - ifname = nm_setting_bond_get_interface_name (s_bond); - if (g_strcmp0 (ifname, nm_device_get_ip_iface (self)) != 0) - return FALSE; - + /* FIXME */ return TRUE; } -static NMConnection * -connection_match_config (NMDevice *self, const GSList *connections) -{ - const GSList *iter; - GSList *bond_matches; - NMConnection *match; - - /* First narrow @connections down to those that match in their - * NMSettingBond configuration. - */ - bond_matches = NULL; - for (iter = connections; iter; iter = iter->next) { - NMConnection *candidate = NM_CONNECTION (iter->data); - - if (!nm_connection_is_type (candidate, NM_SETTING_BOND_SETTING_NAME)) - continue; - if (!bond_match_config (self, candidate)) - continue; - - bond_matches = g_slist_prepend (bond_matches, candidate); - } - - /* Now pass those to the super method, which will check IP config */ - bond_matches = g_slist_reverse (bond_matches); - match = NM_DEVICE_CLASS (nm_device_bond_parent_class)->connection_match_config (self, bond_matches); - g_slist_free (bond_matches); - - return match; -} - /******************************************************************/ static NMActStageReturn @@ -448,7 +407,7 @@ nm_device_bond_class_init (NMDeviceBondClass *klass) parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; - parent_class->connection_match_config = connection_match_config; + parent_class->match_l2_config = match_l2_config; parent_class->act_stage1_prepare = act_stage1_prepare; parent_class->enslave_slave = enslave_slave; diff --git a/src/nm-device-bridge.c b/src/nm-device-bridge.c index 82e9f565a9..93a11edd43 100644 --- a/src/nm-device-bridge.c +++ b/src/nm-device-bridge.c @@ -226,53 +226,12 @@ complete_connection (NMDevice *device, } static gboolean -bridge_match_config (NMDevice *self, NMConnection *connection) +match_l2_config (NMDevice *self, NMConnection *connection) { - NMSettingBridge *s_bridge; - const char *ifname; - - s_bridge = nm_connection_get_setting_bridge (connection); - if (!s_bridge) - return FALSE; - - /* Interface name */ - ifname = nm_setting_bridge_get_interface_name (s_bridge); - if (g_strcmp0 (ifname, nm_device_get_ip_iface (self)) != 0) - return FALSE; - + /* FIXME */ return TRUE; } -static NMConnection * -connection_match_config (NMDevice *self, const GSList *connections) -{ - const GSList *iter; - GSList *bridge_matches; - NMConnection *match; - - /* First narrow @connections down to those that match in their - * NMSettingBridge configuration. - */ - bridge_matches = NULL; - for (iter = connections; iter; iter = iter->next) { - NMConnection *candidate = NM_CONNECTION (iter->data); - - if (!nm_connection_is_type (candidate, NM_SETTING_BRIDGE_SETTING_NAME)) - continue; - if (!bridge_match_config (self, candidate)) - continue; - - bridge_matches = g_slist_prepend (bridge_matches, candidate); - } - - /* Now pass those to the super method, which will check IP config */ - bridge_matches = g_slist_reverse (bridge_matches); - match = NM_DEVICE_CLASS (nm_device_bridge_parent_class)->connection_match_config (self, bridge_matches); - g_slist_free (bridge_matches); - - return match; -} - /******************************************************************/ static void @@ -504,7 +463,7 @@ nm_device_bridge_class_init (NMDeviceBridgeClass *klass) parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; - parent_class->connection_match_config = connection_match_config; + parent_class->match_l2_config = match_l2_config; parent_class->act_stage1_prepare = act_stage1_prepare; parent_class->enslave_slave = enslave_slave; diff --git a/src/nm-device-ethernet.c b/src/nm-device-ethernet.c index 7279f191da..bc94e8df11 100644 --- a/src/nm-device-ethernet.c +++ b/src/nm-device-ethernet.c @@ -1359,39 +1359,18 @@ spec_match_list (NMDevice *device, const GSList *specs) return NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->spec_match_list (device, specs); } -static NMConnection * -connection_match_config (NMDevice *self, const GSList *connections) +static gboolean +match_l2_config (NMDevice *self, NMConnection *connection) { - const GSList *iter; - GSList *ether_matches; - NMConnection *match; - - /* First narrow @connections down to those that match in their - * NMSettingWired configuration. + /* Can't match 802.1x or PPPoE connections; they have too much state + * that's impossible to get on-the-fly from PPPoE or the supplicant. */ - ether_matches = NULL; - for (iter = connections; iter; iter = iter->next) { - NMConnection *candidate = NM_CONNECTION (iter->data); + if ( nm_connection_get_setting_802_1x (connection) + || nm_connection_get_setting_pppoe (connection)) + return FALSE; - /* Can't assume 802.1x or PPPoE connections; they have too much state - * that's impossible to get on-the-fly from PPPoE or the supplicant. - */ - if ( nm_connection_get_setting_802_1x (candidate) - || nm_connection_get_setting_pppoe (candidate)) - continue; - - if (!match_ethernet_connection (self, candidate, NULL)) - continue; - - ether_matches = g_slist_prepend (ether_matches, candidate); - } - - /* Now pass those to the super method, which will check IP config */ - ether_matches = g_slist_reverse (ether_matches); - match = NM_DEVICE_CLASS (nm_device_ethernet_parent_class)->connection_match_config (self, ether_matches); - g_slist_free (ether_matches); - - return match; + /* FIXME: do L2 checks */ + return TRUE; } static gboolean @@ -1506,7 +1485,7 @@ nm_device_ethernet_class_init (NMDeviceEthernetClass *klass) parent_class->ip4_config_pre_commit = ip4_config_pre_commit; parent_class->deactivate = deactivate; parent_class->spec_match_list = spec_match_list; - parent_class->connection_match_config = connection_match_config; + parent_class->match_l2_config = match_l2_config; parent_class->hwaddr_matches = hwaddr_matches; parent_class->state_changed = device_state_changed; diff --git a/src/nm-device-infiniband.c b/src/nm-device-infiniband.c index 03e37dc05d..4613c66a76 100644 --- a/src/nm-device-infiniband.c +++ b/src/nm-device-infiniband.c @@ -333,54 +333,12 @@ complete_connection (NMDevice *device, } static gboolean -infiniband_match_config (NMDevice *self, NMConnection *connection) +match_l2_config (NMDevice *self, NMConnection *connection) { - NMDeviceInfinibandPrivate *priv = NM_DEVICE_INFINIBAND_GET_PRIVATE (self); - NMSettingInfiniband *s_infiniband; - const GByteArray *s_mac; - - s_infiniband = nm_connection_get_setting_infiniband (connection); - if (!s_infiniband) - return FALSE; - - /* MAC address check */ - s_mac = nm_setting_infiniband_get_mac_address (s_infiniband); - if (s_mac && memcmp (s_mac->data, priv->hw_addr, INFINIBAND_ALEN)) - return FALSE; - + /* FIXME */ return TRUE; } -static NMConnection * -connection_match_config (NMDevice *self, const GSList *connections) -{ - const GSList *iter; - GSList *infiniband_matches; - NMConnection *match; - - /* First narrow @connections down to those that match in their - * NMSettingInfiniband configuration. - */ - infiniband_matches = NULL; - for (iter = connections; iter; iter = iter->next) { - NMConnection *candidate = NM_CONNECTION (iter->data); - - if (!nm_connection_is_type (candidate, NM_SETTING_INFINIBAND_SETTING_NAME)) - continue; - if (!infiniband_match_config (self, candidate)) - continue; - - infiniband_matches = g_slist_prepend (infiniband_matches, candidate); - } - - /* Now pass those to the super method, which will check IP config */ - infiniband_matches = g_slist_reverse (infiniband_matches); - match = NM_DEVICE_CLASS (nm_device_infiniband_parent_class)->connection_match_config (self, infiniband_matches); - g_slist_free (infiniband_matches); - - return match; -} - static gboolean hwaddr_matches (NMDevice *device, NMConnection *connection, @@ -462,7 +420,7 @@ nm_device_infiniband_class_init (NMDeviceInfinibandClass *klass) parent_class->act_stage1_prepare = act_stage1_prepare; parent_class->ip4_config_pre_commit = ip4_config_pre_commit; - parent_class->connection_match_config = connection_match_config; + parent_class->match_l2_config = match_l2_config; parent_class->hwaddr_matches = hwaddr_matches; /* properties */ diff --git a/src/nm-device-private.h b/src/nm-device-private.h index 8edae66022..3433c0e1af 100644 --- a/src/nm-device-private.h +++ b/src/nm-device-private.h @@ -75,8 +75,6 @@ void nm_device_set_dhcp_anycast_address (NMDevice *device, guint8 *addr); gboolean nm_device_dhcp4_renew (NMDevice *device, gboolean release); -gboolean nm_device_match_ip_config (NMDevice *device, NMConnection *connection); - NMConnectionProvider *nm_device_get_connection_provider (NMDevice *device); void nm_device_recheck_available_connections (NMDevice *device); diff --git a/src/nm-device-vlan.c b/src/nm-device-vlan.c index 159122f504..bfa5d5fe2f 100644 --- a/src/nm-device-vlan.c +++ b/src/nm-device-vlan.c @@ -355,37 +355,23 @@ complete_connection (NMDevice *device, } static gboolean -vlan_match_config (NMDevice *device, NMConnection *connection) +match_l2_config (NMDevice *device, NMConnection *connection) { NMDeviceVlanPrivate *priv = NM_DEVICE_VLAN_GET_PRIVATE (device); NMSettingVlan *s_vlan; - const char *ifname, *parent; gboolean fail_if_no_hwaddr = FALSE; s_vlan = nm_connection_get_setting_vlan (connection); - if (!s_vlan) - return FALSE; + g_assert (s_vlan); - /* Interface name */ - ifname = nm_setting_vlan_get_interface_name (s_vlan); - if (g_strcmp0 (ifname, nm_device_get_ip_iface (device)) != 0) - return FALSE; - - if (nm_setting_vlan_get_id (s_vlan) != priv->vlan_id) - return FALSE; - - parent = nm_setting_vlan_get_parent (s_vlan); - if (parent) { - if (!match_parent (NM_DEVICE_VLAN (device), parent, NULL)) - return FALSE; - } else { + if ( !nm_setting_vlan_get_parent (s_vlan) + && !nm_setting_vlan_get_interface_name (s_vlan)) { /* If there's no parent and no interface name given, then the only way * we have to identify the VLAN interface the connection matches is * a hardware-specific setting's hardware address property, so we want * to fail the match below if we there is none. */ - if (ifname == NULL) - fail_if_no_hwaddr = TRUE; + fail_if_no_hwaddr = TRUE; } /* MAC address check; we ask the parent to check our own MAC address, @@ -396,32 +382,10 @@ vlan_match_config (NMDevice *device, NMConnection *connection) if (!nm_device_hwaddr_matches (priv->parent, connection, priv->hw_addr, priv->hw_addr_len, fail_if_no_hwaddr)) return FALSE; + /* FIXME: any more L2 checks? */ return TRUE; } -static NMConnection * -connection_match_config (NMDevice *self, const GSList *connections) -{ - const GSList *iter; - - /* First narrow @connections down to those that match in their - * NMSettingVlan configuration. - */ - for (iter = connections; iter; iter = iter->next) { - NMConnection *candidate = iter->data; - - if (!nm_connection_is_type (candidate, NM_SETTING_VLAN_SETTING_NAME)) - continue; - if (!vlan_match_config (self, candidate)) - continue; - if (!nm_device_match_ip_config (self, candidate)) - continue; - - return candidate; - } - return NULL; -} - /******************************************************************/ static void @@ -658,7 +622,7 @@ nm_device_vlan_class_init (NMDeviceVlanClass *klass) parent_class->get_best_auto_connection = get_best_auto_connection; parent_class->check_connection_compatible = check_connection_compatible; parent_class->complete_connection = complete_connection; - parent_class->connection_match_config = connection_match_config; + parent_class->match_l2_config = match_l2_config; /* properties */ g_object_class_install_property diff --git a/src/nm-device-wired.c b/src/nm-device-wired.c index 1044488c3c..7bb19b0ad8 100644 --- a/src/nm-device-wired.c +++ b/src/nm-device-wired.c @@ -299,23 +299,6 @@ is_available (NMDevice *dev, gboolean need_carrier) return TRUE; } -static NMConnection * -connection_match_config (NMDevice *self, const GSList *connections) -{ - const GSList *iter; - - for (iter = connections; iter; iter = iter->next) { - NMConnection *candidate = NM_CONNECTION (iter->data); - - if (!nm_device_match_ip_config (self, candidate)) - continue; - - return candidate; - } - - return NULL; -} - static NMActStageReturn act_stage3_ip4_config_start (NMDevice *device, NMIP4Config **out_config, @@ -384,7 +367,6 @@ nm_device_wired_class_init (NMDeviceWiredClass *klass) parent_class->hw_bring_up = hw_bring_up; parent_class->can_interrupt_activation = can_interrupt_activation; parent_class->is_available = is_available; - parent_class->connection_match_config = connection_match_config; parent_class->act_stage3_ip4_config_start = act_stage3_ip4_config_start; parent_class->act_stage3_ip6_config_start = act_stage3_ip6_config_start; } diff --git a/src/nm-device.c b/src/nm-device.c index 386e5487c6..5da51e0ac7 100644 --- a/src/nm-device.c +++ b/src/nm-device.c @@ -1480,7 +1480,7 @@ nm_device_can_assume_connections (NMDevice *device) { g_return_val_if_fail (NM_IS_DEVICE (device), FALSE); - return !!NM_DEVICE_GET_CLASS (device)->connection_match_config; + return !!NM_DEVICE_GET_CLASS (device)->match_l2_config; } static void @@ -5569,24 +5569,31 @@ ip4_match_config (NMDevice *self, NMConnection *connection) return TRUE; } -gboolean -nm_device_match_ip_config (NMDevice *device, NMConnection *connection) -{ - if (!ip4_match_config (device, connection)) - return FALSE; - - /* FIXME: match IPv6 config */ - - return TRUE; -} - NMConnection * -nm_device_connection_match_config (NMDevice *device, const GSList *connections) +nm_device_find_assumable_connection (NMDevice *device, const GSList *connections) { + const GSList *iter; + g_return_val_if_fail (NM_IS_DEVICE (device), NULL); - if (NM_DEVICE_GET_CLASS (device)->connection_match_config) - return NM_DEVICE_GET_CLASS (device)->connection_match_config (NM_DEVICE (device), connections); + if (!NM_DEVICE_GET_CLASS (device)->match_l2_config) + return NULL; + + for (iter = connections; iter; iter = iter->next) { + NMConnection *candidate = NM_CONNECTION (iter->data); + + if (!nm_device_check_connection_compatible (device, candidate, NULL)) + continue; + + if (!ip4_match_config (device, candidate)) + continue; + + /* FIXME: match IPv6 config */ + + if (NM_DEVICE_GET_CLASS (device)->match_l2_config (device, candidate)) + return candidate; + } + return NULL; } diff --git a/src/nm-device.h b/src/nm-device.h index 678dcc5c1a..2445b8ee92 100644 --- a/src/nm-device.h +++ b/src/nm-device.h @@ -175,7 +175,7 @@ typedef struct { gboolean (* spec_match_list) (NMDevice *self, const GSList *specs); - NMConnection * (* connection_match_config) (NMDevice *self, const GSList *connections); + gboolean (* match_l2_config) (NMDevice *self, NMConnection *connection); gboolean (* hwaddr_matches) (NMDevice *self, NMConnection *connection, @@ -258,8 +258,8 @@ gboolean nm_device_check_connection_compatible (NMDevice *device, gboolean nm_device_can_assume_connections (NMDevice *device); -NMConnection * nm_device_connection_match_config (NMDevice *device, - const GSList *connections); +NMConnection * nm_device_find_assumable_connection (NMDevice *device, + const GSList *connections); gboolean nm_device_hwaddr_matches (NMDevice *device, NMConnection *connection, diff --git a/src/nm-manager.c b/src/nm-manager.c index 929fb46a35..bf172f4c70 100644 --- a/src/nm-manager.c +++ b/src/nm-manager.c @@ -1905,7 +1905,7 @@ add_device (NMManager *self, NMDevice *device) GSList *connections = NULL; connections = nm_settings_get_connections (priv->settings); - existing = nm_device_connection_match_config (device, (const GSList *) connections); + existing = nm_device_find_assumable_connection (device, connections); g_slist_free (connections); if (existing && !nm_device_is_available (device, TRUE)) {