From da77cca27fcfa632689ef5b601cd08c95eb796f7 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 12 Jun 2023 12:43:03 +0200 Subject: [PATCH] core: refactor _match_section_infos_lookup() to accept match_data argument This makes the code more generic, where _match_section_infos_lookup() accepts a match_data argument. Previously, it supported two hard-coded approaches (from-device, from-platform). Note that _match_section_infos_lookup() still accepts either a "match_data" or a "device" argument. It might be nicer, if _match_section_infos_lookup() would only accept "match_data". If a caller wants lookup by "device", they would need to call nm_match_spec_device_data_init_from_device() first. However, it's done this way with a separate "device" argument, because often we don't have any configuration to search for, and the initialization of the match-data can be saved. So for the common case where we want to lookup by "device", we initialize the "match-data" lazy and on demand. (cherry picked from commit 52518066515321340a15224ca478429d14767d5f) --- src/core/nm-config-data.c | 61 +++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/core/nm-config-data.c b/src/core/nm-config-data.c index ff44bc46b3..62949ccbfb 100644 --- a/src/core/nm-config-data.c +++ b/src/core/nm-config-data.c @@ -1488,21 +1488,23 @@ global_dns_equal(NMGlobalDnsConfig *old, NMGlobalDnsConfig *new) /*****************************************************************************/ static const MatchSectionInfo * -_match_section_infos_lookup(const MatchSectionInfo *match_section_infos, - GKeyFile *keyfile, - const char *property, - NMDevice *device, - const NMPlatformLink *pllink, - const char *match_device_type, - const char **out_value) +_match_section_infos_lookup(const MatchSectionInfo *match_section_infos, + GKeyFile *keyfile, + const char *property, + const NMMatchSpecDeviceData *match_data, + NMDevice *device, + const char **out_value) { - const char *match_dhcp_plugin; + NMMatchSpecDeviceData match_data_local; + + /* Caller must either provide a "match_data" or a "device" (actually, + * neither is also fine, albeit unusual). */ + nm_assert(!match_data || !device); + nm_assert(!device || NM_IS_DEVICE(device)); if (!match_section_infos) goto out; - match_dhcp_plugin = nm_dhcp_manager_get_config(nm_dhcp_manager_get()); - for (; match_section_infos->group_name; match_section_infos++) { const char *value; gboolean match; @@ -1519,16 +1521,17 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos, continue; if (match_section_infos->match_device.has) { - if (device) - match = nm_device_spec_match_list(device, match_section_infos->match_device.spec); - else if (pllink) - match = nm_match_spec_device_by_pllink(pllink, - match_device_type, - match_dhcp_plugin, - match_section_infos->match_device.spec, - FALSE); - else - match = FALSE; + NMMatchSpecMatchType m; + + if (G_UNLIKELY(!match_data)) { + /* In most cases, we don't actually have any matches. So we "optimize" + * here by allowing the user to specify a NMDEvice directly, and only + * initialize the match-data when needed. */ + match_data = nm_match_spec_device_data_init_from_device(&match_data_local, device); + } + + m = nm_match_spec_device(match_section_infos->match_device.spec, match_data); + match = nm_match_spec_match_type_to_bool(m, FALSE); } else match = TRUE; @@ -1563,9 +1566,8 @@ nm_config_data_get_device_config(const NMConfigData *self, connection_info = _match_section_infos_lookup(&priv->device_infos[0], priv->keyfile, property, + NULL, device, - NULL, - NULL, &value); NM_SET_OUT(has_match, !!connection_info); return value; @@ -1581,18 +1583,23 @@ nm_config_data_get_device_config_by_pllink(const NMConfigData *self, const NMConfigDataPrivate *priv; const MatchSectionInfo *connection_info; const char *value; + NMMatchSpecDeviceData match_data; g_return_val_if_fail(self, NULL); g_return_val_if_fail(property && *property, NULL); priv = NM_CONFIG_DATA_GET_PRIVATE(self); + nm_match_spec_device_data_init_from_platform(&match_data, + pllink, + match_device_type, + nm_dhcp_manager_get_config(nm_dhcp_manager_get())); + connection_info = _match_section_infos_lookup(&priv->device_infos[0], priv->keyfile, property, + &match_data, NULL, - pllink, - match_device_type, &value); NM_SET_OUT(has_match, !!connection_info); return value; @@ -1651,9 +1658,8 @@ nm_config_data_get_device_allowed_connections_specs(const NMConfigData *self, connection_info = _match_section_infos_lookup(&priv->device_infos[0], priv->keyfile, NM_CONFIG_KEYFILE_KEY_DEVICE_ALLOWED_CONNECTIONS, + NULL, device, - NULL, - NULL, NULL); if (connection_info) { @@ -1696,9 +1702,8 @@ nm_config_data_get_connection_default(const NMConfigData *self, _match_section_infos_lookup(&priv->connection_infos[0], priv->keyfile, property, + NULL, device, - NULL, - NULL, &value); return value; }