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 5251806651)
This commit is contained in:
Thomas Haller 2023-06-12 12:43:03 +02:00
parent 8ea3f77a3b
commit da77cca27f
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -1488,21 +1488,23 @@ global_dns_equal(NMGlobalDnsConfig *old, NMGlobalDnsConfig *new)
/*****************************************************************************/ /*****************************************************************************/
static const MatchSectionInfo * static const MatchSectionInfo *
_match_section_infos_lookup(const MatchSectionInfo *match_section_infos, _match_section_infos_lookup(const MatchSectionInfo *match_section_infos,
GKeyFile *keyfile, GKeyFile *keyfile,
const char *property, const char *property,
NMDevice *device, const NMMatchSpecDeviceData *match_data,
const NMPlatformLink *pllink, NMDevice *device,
const char *match_device_type, const char **out_value)
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) if (!match_section_infos)
goto out; goto out;
match_dhcp_plugin = nm_dhcp_manager_get_config(nm_dhcp_manager_get());
for (; match_section_infos->group_name; match_section_infos++) { for (; match_section_infos->group_name; match_section_infos++) {
const char *value; const char *value;
gboolean match; gboolean match;
@ -1519,16 +1521,17 @@ _match_section_infos_lookup(const MatchSectionInfo *match_section_infos,
continue; continue;
if (match_section_infos->match_device.has) { if (match_section_infos->match_device.has) {
if (device) NMMatchSpecMatchType m;
match = nm_device_spec_match_list(device, match_section_infos->match_device.spec);
else if (pllink) if (G_UNLIKELY(!match_data)) {
match = nm_match_spec_device_by_pllink(pllink, /* In most cases, we don't actually have any matches. So we "optimize"
match_device_type, * here by allowing the user to specify a NMDEvice directly, and only
match_dhcp_plugin, * initialize the match-data when needed. */
match_section_infos->match_device.spec, match_data = nm_match_spec_device_data_init_from_device(&match_data_local, device);
FALSE); }
else
match = FALSE; m = nm_match_spec_device(match_section_infos->match_device.spec, match_data);
match = nm_match_spec_match_type_to_bool(m, FALSE);
} else } else
match = TRUE; 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], connection_info = _match_section_infos_lookup(&priv->device_infos[0],
priv->keyfile, priv->keyfile,
property, property,
NULL,
device, device,
NULL,
NULL,
&value); &value);
NM_SET_OUT(has_match, !!connection_info); NM_SET_OUT(has_match, !!connection_info);
return value; return value;
@ -1581,18 +1583,23 @@ nm_config_data_get_device_config_by_pllink(const NMConfigData *self,
const NMConfigDataPrivate *priv; const NMConfigDataPrivate *priv;
const MatchSectionInfo *connection_info; const MatchSectionInfo *connection_info;
const char *value; const char *value;
NMMatchSpecDeviceData match_data;
g_return_val_if_fail(self, NULL); g_return_val_if_fail(self, NULL);
g_return_val_if_fail(property && *property, NULL); g_return_val_if_fail(property && *property, NULL);
priv = NM_CONFIG_DATA_GET_PRIVATE(self); 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], connection_info = _match_section_infos_lookup(&priv->device_infos[0],
priv->keyfile, priv->keyfile,
property, property,
&match_data,
NULL, NULL,
pllink,
match_device_type,
&value); &value);
NM_SET_OUT(has_match, !!connection_info); NM_SET_OUT(has_match, !!connection_info);
return value; 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], connection_info = _match_section_infos_lookup(&priv->device_infos[0],
priv->keyfile, priv->keyfile,
NM_CONFIG_KEYFILE_KEY_DEVICE_ALLOWED_CONNECTIONS, NM_CONFIG_KEYFILE_KEY_DEVICE_ALLOWED_CONNECTIONS,
NULL,
device, device,
NULL,
NULL,
NULL); NULL);
if (connection_info) { if (connection_info) {
@ -1696,9 +1702,8 @@ nm_config_data_get_connection_default(const NMConfigData *self,
_match_section_infos_lookup(&priv->connection_infos[0], _match_section_infos_lookup(&priv->connection_infos[0],
priv->keyfile, priv->keyfile,
property, property,
NULL,
device, device,
NULL,
NULL,
&value); &value);
return value; return value;
} }