libnm: always initialize default values for "direct" properties

We encode the default value "direct" properties in the GParamSpec.
But we also avoid CONSTRUCT properties, because they have an overhead
and they are generally odd for the settings.

So up to now, it was cumbersome to explicitly set the default value,
but it was also error prone.

Avoid that by always initializing the default value for our "direct"
properties.
This commit is contained in:
Thomas Haller
2021-06-29 08:52:02 +02:00
parent 3c801ec4f3
commit 56241f328f
11 changed files with 93 additions and 35 deletions

View File

@@ -1564,19 +1564,13 @@ nm_setting_bridge_init(NMSettingBridge *setting)
priv->multicast_last_member_interval = NM_BRIDGE_MULTICAST_LAST_MEMBER_INTERVAL_DEF;
priv->multicast_membership_interval = NM_BRIDGE_MULTICAST_MEMBERSHIP_INTERVAL_DEF;
priv->multicast_hash_max = NM_BRIDGE_MULTICAST_HASH_MAX_DEF;
priv->multicast_snooping = NM_BRIDGE_MULTICAST_SNOOPING_DEF;
priv->priority = NM_BRIDGE_PRIORITY_DEF;
priv->stp = NM_BRIDGE_STP_DEF;
priv->vlan_default_pvid = NM_BRIDGE_VLAN_DEFAULT_PVID_DEF;
priv->multicast_query_interval = NM_BRIDGE_MULTICAST_QUERY_INTERVAL_DEF;
priv->multicast_query_response_interval = NM_BRIDGE_MULTICAST_QUERY_RESPONSE_INTERVAL_DEF;
priv->multicast_querier_interval = NM_BRIDGE_MULTICAST_QUERIER_INTERVAL_DEF;
priv->multicast_startup_query_count = NM_BRIDGE_MULTICAST_STARTUP_QUERY_COUNT_DEF;
priv->multicast_startup_query_interval = NM_BRIDGE_MULTICAST_STARTUP_QUERY_INTERVAL_DEF;
nm_assert(priv->multicast_querier == NM_BRIDGE_MULTICAST_QUERIER_DEF);
nm_assert(priv->multicast_query_use_ifaddr == NM_BRIDGE_MULTICAST_QUERY_USE_IFADDR_DEF);
nm_assert(priv->vlan_stats_enabled == NM_BRIDGE_VLAN_STATS_ENABLED_DEF);
}
/**

View File

@@ -1801,7 +1801,6 @@ nm_setting_connection_init(NMSettingConnection *setting)
NMSettingConnectionPrivate *priv = NM_SETTING_CONNECTION_GET_PRIVATE(setting);
priv->auth_retries = -1;
priv->autoconnect = TRUE;
priv->autoconnect_priority = NM_SETTING_CONNECTION_AUTOCONNECT_PRIORITY_DEFAULT;
priv->autoconnect_retries = -1;
priv->autoconnect_slaves = NM_SETTING_CONNECTION_AUTOCONNECT_SLAVES_DEFAULT;

View File

@@ -6068,8 +6068,6 @@ _nm_setting_ip_config_private_init(gpointer self, NMSettingIPConfigPrivate *priv
priv->addresses = g_ptr_array_new_with_free_func((GDestroyNotify) nm_ip_address_unref);
priv->routes = g_ptr_array_new_with_free_func((GDestroyNotify) nm_ip_route_unref);
priv->route_metric = -1;
priv->dhcp_send_hostname = TRUE;
priv->may_fail = TRUE;
priv->dad_timeout = -1;
priv->required_timeout = -1;
}

View File

@@ -615,11 +615,7 @@ set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *ps
static void
nm_setting_ip_tunnel_init(NMSettingIPTunnel *self)
{
NMSettingIPTunnelPrivate *priv = NM_SETTING_IP_TUNNEL_GET_PRIVATE(self);
priv->path_mtu_discovery = TRUE;
}
{}
/**
* nm_setting_ip_tunnel_new:

View File

@@ -492,9 +492,7 @@ nm_setting_macsec_init(NMSettingMacsec *self)
NMSettingMacsecPrivate *priv = NM_SETTING_MACSEC_GET_PRIVATE(self);
nm_assert(priv->mode == NM_SETTING_MACSEC_MODE_PSK);
priv->encrypt = TRUE;
priv->port = 1;
priv->send_sci = TRUE;
priv->validation = NM_SETTING_MACSEC_VALIDATION_STRICT;
}

View File

@@ -234,11 +234,7 @@ set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *ps
static void
nm_setting_macvlan_init(NMSettingMacvlan *self)
{
NMSettingMacvlanPrivate *priv = NM_SETTING_MACVLAN_GET_PRIVATE(self);
priv->promiscuous = TRUE;
}
{}
/**
* nm_setting_macvlan_new:

View File

@@ -512,11 +512,7 @@ set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *ps
static void
nm_setting_ppp_init(NMSettingPpp *self)
{
NMSettingPppPrivate *priv = NM_SETTING_PPP_GET_PRIVATE(self);
priv->noauth = TRUE;
}
{}
/**
* nm_setting_ppp_new:

View File

@@ -536,7 +536,6 @@ nm_setting_vxlan_init(NMSettingVxlan *self)
priv->destination_port = DST_PORT_DEFAULT;
priv->ageing = 300;
priv->learning = TRUE;
}
/**

View File

@@ -2372,7 +2372,6 @@ nm_setting_wireguard_init(NMSettingWireGuard *setting)
priv->peers_arr = g_ptr_array_new();
priv->peers_hash = g_hash_table_new(nm_pstr_hash, nm_pstr_equal);
priv->peer_routes = TRUE;
priv->ip4_auto_default_route = NM_TERNARY_DEFAULT;
priv->ip6_auto_default_route = NM_TERNARY_DEFAULT;
}

View File

@@ -753,6 +753,59 @@ out_fail:
/*****************************************************************************/
static void
_init_direct(NMSetting *setting)
{
const NMSettInfoSetting *sett_info;
guint i;
sett_info = _nm_setting_class_get_sett_info(NM_SETTING_GET_CLASS(setting));
nm_assert(sett_info);
for (i = 0; i < sett_info->property_infos_len; i++) {
const NMSettInfoProperty *property_info = &sett_info->property_infos[i];
/* We don't emit any g_object_notify_by_pspec(), because this is
* only supposed to be called during initialization of the GObject
* instance. */
switch (property_info->property_type->direct_type) {
case NM_VALUE_TYPE_NONE:
break;
case NM_VALUE_TYPE_BOOL:
{
bool *p_val = _nm_setting_get_private(setting, sett_info, property_info->direct_offset);
gboolean def_val;
def_val = NM_G_PARAM_SPEC_GET_DEFAULT_BOOLEAN(property_info->param_spec);
nm_assert(*p_val == FALSE);
*p_val = def_val;
break;
}
case NM_VALUE_TYPE_UINT32:
{
guint32 *p_val =
_nm_setting_get_private(setting, sett_info, property_info->direct_offset);
guint def_val;
def_val = NM_G_PARAM_SPEC_GET_DEFAULT_UINT(property_info->param_spec);
nm_assert(*p_val == 0);
*p_val = def_val;
break;
}
case NM_VALUE_TYPE_STRING:
nm_assert(!NM_G_PARAM_SPEC_GET_DEFAULT_STRING(property_info->param_spec));
nm_assert(!(
*((const char *const *)
_nm_setting_get_private(setting, sett_info, property_info->direct_offset))));
break;
default:
nm_assert_not_reached();
break;
}
}
}
static void
_finalize_direct(NMSetting *setting)
{
@@ -3129,6 +3182,32 @@ static void
nm_setting_init(NMSetting *setting)
{}
static void
constructed(GObject *object)
{
NMSetting * self = NM_SETTING(object);
NMSettingClass *klass = NM_SETTING_GET_CLASS(self);
/* we don't support that NMSetting subclasses override constructed.
* They all must have no G_PARAM_CONSTRUCT/G_PARAM_CONSTRUCT_ONLY
* properties, otherwise the automatism of _init_direct() needs
* careful adjustment. */
nm_assert(G_OBJECT_CLASS(klass)->constructed == constructed);
/* we always initialize the defaults of the (direct) properties. Note that:
*
* - we don't use CONSTRUCT properties, because they have an overhead during
* each object creation. Via _init_direct() we can do it more efficiently.
*
* - we always call this, because we want to get all default values right.
* We even call this for NMSetting subclasses that (historically) are not
* yet aware of this happening.
*/
_init_direct(self);
G_OBJECT_CLASS(nm_setting_parent_class)->constructed(object);
}
static void
finalize(GObject *object)
{
@@ -3156,6 +3235,7 @@ nm_setting_class_init(NMSettingClass *setting_class)
g_type_class_add_private(setting_class, sizeof(NMSettingPrivate));
object_class->constructed = constructed;
object_class->get_property = get_property;
object_class->finalize = finalize;

View File

@@ -4566,6 +4566,9 @@ check_done:;
if (NM_FLAGS_HAS(sip->param_spec->flags, NM_SETTING_PARAM_TO_DBUS_IGNORE_FLAGS))
g_assert(sip->property_type->to_dbus_fcn);
g_assert(!NM_FLAGS_HAS(sip->param_spec->flags, G_PARAM_CONSTRUCT));
g_assert(!NM_FLAGS_HAS(sip->param_spec->flags, G_PARAM_CONSTRUCT_ONLY));
}
}