From a29d8b712f28993d3302b9e2ba1dff56fc9bb623 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 3 Aug 2021 18:53:25 +0200 Subject: [PATCH] l3cfg: set NMIPConfigSource for NML3ConfigData at construct time Each NML3ConfigData should have a source set, and in fact most callers would call nm_l3_config_data_set_source() right after creating the instance. Move the source parameter to the new() constructor function. Also remove the setter, making the source of an instance immutable. As every l3cfg instance generally has a clear purpose, the source should always be known from the start and doesn't need to change. --- src/core/devices/nm-device-private.h | 2 +- src/core/devices/nm-device.c | 4 ++-- src/core/ndisc/nm-ndisc.c | 4 +--- src/core/nm-l3-config-data.c | 24 +++++++----------------- src/core/nm-l3-config-data.h | 5 ++--- src/core/nm-l3-ipv4ll.c | 3 +-- src/core/nm-l3cfg.c | 3 ++- src/core/tests/test-l3cfg.c | 2 +- 8 files changed, 17 insertions(+), 30 deletions(-) diff --git a/src/core/devices/nm-device-private.h b/src/core/devices/nm-device-private.h index eb37b14ffb..b55e8b4380 100644 --- a/src/core/devices/nm-device-private.h +++ b/src/core/devices/nm-device-private.h @@ -143,7 +143,7 @@ NMIP6Config *nm_device_ip6_config_new(NMDevice *self); NMIPConfig *nm_device_ip_config_new(NMDevice *self, int addr_family); -NML3ConfigData *nm_device_create_l3_config_data(NMDevice *self); +NML3ConfigData *nm_device_create_l3_config_data(NMDevice *self, NMIPConfigSource source); /*****************************************************************************/ diff --git a/src/core/devices/nm-device.c b/src/core/devices/nm-device.c index 1fd6ba61da..3c1db20c42 100644 --- a/src/core/devices/nm-device.c +++ b/src/core/devices/nm-device.c @@ -2407,7 +2407,7 @@ nm_device_ip_config_new(NMDevice *self, int addr_family) } NML3ConfigData * -nm_device_create_l3_config_data(NMDevice *self) +nm_device_create_l3_config_data(NMDevice *self, NMIPConfigSource source) { int ifindex; @@ -2417,7 +2417,7 @@ nm_device_create_l3_config_data(NMDevice *self) if (ifindex <= 0) g_return_val_if_reached(NULL); - return nm_l3_config_data_new(nm_device_get_multi_index(self), ifindex); + return nm_l3_config_data_new(nm_device_get_multi_index(self), ifindex, source); } static void diff --git a/src/core/ndisc/nm-ndisc.c b/src/core/ndisc/nm-ndisc.c index cf1b58ecf4..655a2523ad 100644 --- a/src/core/ndisc/nm-ndisc.c +++ b/src/core/ndisc/nm-ndisc.c @@ -112,9 +112,7 @@ nm_ndisc_data_to_l3cd(NMDedupMultiIndex * multi_idx, guint i; const gint32 now_sec = nm_utils_get_monotonic_timestamp_sec(); - l3cd = nm_l3_config_data_new(multi_idx, ifindex); - - nm_l3_config_data_set_source(l3cd, NM_IP_CONFIG_SOURCE_NDISC); + l3cd = nm_l3_config_data_new(multi_idx, ifindex, NM_IP_CONFIG_SOURCE_NDISC); nm_l3_config_data_set_ip6_privacy(l3cd, ip6_privacy); diff --git a/src/core/nm-l3-config-data.c b/src/core/nm-l3-config-data.c index 407295e569..51ea92f634 100644 --- a/src/core/nm-l3-config-data.c +++ b/src/core/nm-l3-config-data.c @@ -639,12 +639,14 @@ _idx_type_init(DedupMultiIdxType *idx_type, NMPObjectType obj_type) } NML3ConfigData * -nm_l3_config_data_new(NMDedupMultiIndex *multi_idx, int ifindex) +nm_l3_config_data_new(NMDedupMultiIndex *multi_idx, int ifindex, NMIPConfigSource source) { NML3ConfigData *self; nm_assert(multi_idx); nm_assert(ifindex > 0); + nm_assert(source == NM_IP_CONFIG_SOURCE_UNKNOWN + || (source >= NM_IP_CONFIG_SOURCE_KERNEL && source <= NM_IP_CONFIG_SOURCE_USER)); self = g_slice_new(NML3ConfigData); *self = (NML3ConfigData){ @@ -659,7 +661,7 @@ nm_l3_config_data_new(NMDedupMultiIndex *multi_idx, int ifindex) .proxy_method = NM_PROXY_CONFIG_METHOD_UNKNOWN, .route_table_sync_4 = NM_IP_ROUTE_TABLE_SYNC_MODE_NONE, .route_table_sync_6 = NM_IP_ROUTE_TABLE_SYNC_MODE_NONE, - .source = NM_IP_CONFIG_SOURCE_UNKNOWN, + .source = source, .ip6_privacy = NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN, .ndisc_hop_limit_set = FALSE, .ndisc_reachable_time_msec_set = FALSE, @@ -1678,18 +1680,6 @@ nm_l3_config_data_set_ip6_mtu(NML3ConfigData *self, guint32 ip6_mtu) return TRUE; } -gboolean -nm_l3_config_data_set_source(NML3ConfigData *self, NMIPConfigSource source) -{ - nm_assert(_NM_IS_L3_CONFIG_DATA(self, FALSE)); - - if (self->source == source) - return FALSE; - - self->source = source; - return TRUE; -} - NMSettingIP6ConfigPrivacy nm_l3_config_data_get_ip6_privacy(const NML3ConfigData *self) { @@ -2570,7 +2560,7 @@ nm_l3_config_data_new_from_connection(NMDedupMultiIndex *multi_idx, NML3ConfigData *self; NMSettingProxy *s_proxy; - self = nm_l3_config_data_new(multi_idx, ifindex); + self = nm_l3_config_data_new(multi_idx, ifindex, NM_IP_CONFIG_SOURCE_USER); _init_from_connection_ip(self, AF_INET, connection, route_table_4, route_metric_4); _init_from_connection_ip(self, AF_INET6, connection, route_table_6, route_metric_6); @@ -2689,7 +2679,7 @@ nm_l3_config_data_new_from_platform(NMDedupMultiIndex * multi_idx, if (nm_platform_link_get_master(platform, ifindex) > 0) return NULL; - self = nm_l3_config_data_new(multi_idx, ifindex); + self = nm_l3_config_data_new(multi_idx, ifindex, NM_IP_CONFIG_SOURCE_KERNEL); _init_from_platform(self, AF_INET, platform, ipv6_privacy_rfc4941); _init_from_platform(self, AF_INET6, platform, ipv6_privacy_rfc4941); @@ -2929,7 +2919,7 @@ nm_l3_config_data_new_clone(const NML3ConfigData *src, int ifindex) if (ifindex <= 0) ifindex = src->ifindex; - self = nm_l3_config_data_new(src->multi_idx, ifindex); + self = nm_l3_config_data_new(src->multi_idx, ifindex, NM_IP_CONFIG_SOURCE_UNKNOWN); nm_l3_config_data_merge(self, src, NM_L3_CONFIG_MERGE_FLAGS_CLONE, diff --git a/src/core/nm-l3-config-data.h b/src/core/nm-l3-config-data.h index 9bed47e0f4..9274d8896d 100644 --- a/src/core/nm-l3-config-data.h +++ b/src/core/nm-l3-config-data.h @@ -82,7 +82,8 @@ typedef enum _nm_packed { static inline gboolean NM_IS_L3_CONFIG_DATA(const NML3ConfigData *self); -NML3ConfigData * nm_l3_config_data_new(NMDedupMultiIndex *multi_idx, int ifindex); +NML3ConfigData * +nm_l3_config_data_new(NMDedupMultiIndex *multi_idx, int ifindex, NMIPConfigSource source); const NML3ConfigData *nm_l3_config_data_ref(const NML3ConfigData *self); const NML3ConfigData *nm_l3_config_data_ref_and_seal(const NML3ConfigData *self); const NML3ConfigData *nm_l3_config_data_seal(const NML3ConfigData *self); @@ -363,8 +364,6 @@ nm_l3_config_data_unset_flags(NML3ConfigData *self, NML3ConfigDatFlags flags) /*****************************************************************************/ -gboolean nm_l3_config_data_set_source(NML3ConfigData *self, NMIPConfigSource source); - const NMPObject *nm_l3_config_data_get_first_obj(const NML3ConfigData *self, NMPObjectType obj_type, gboolean (*predicate)(const NMPObject *obj)); diff --git a/src/core/nm-l3-ipv4ll.c b/src/core/nm-l3-ipv4ll.c index 28ceb39e91..fcec44baf3 100644 --- a/src/core/nm-l3-ipv4ll.c +++ b/src/core/nm-l3-ipv4ll.c @@ -357,8 +357,7 @@ _l3cd_config_create(int ifindex, in_addr_t addr, NMDedupMultiIndex *multi_idx) nm_assert(ifindex > 0); nm_assert(multi_idx); - l3cd = nm_l3_config_data_new(multi_idx, ifindex); - nm_l3_config_data_set_source(l3cd, NM_IP_CONFIG_SOURCE_IP4LL); + l3cd = nm_l3_config_data_new(multi_idx, ifindex, NM_IP_CONFIG_SOURCE_IP4LL); nm_l3_config_data_add_address_4(l3cd, _l3cd_config_plat_init_addr(&a, ifindex, addr)); nm_l3_config_data_add_route_4(l3cd, _l3cd_config_plat_init_route(&r, ifindex)); diff --git a/src/core/nm-l3cfg.c b/src/core/nm-l3cfg.c index 340938fbc3..46bdac234b 100644 --- a/src/core/nm-l3cfg.c +++ b/src/core/nm-l3cfg.c @@ -3009,7 +3009,8 @@ _l3cfg_update_combined_config(NML3Cfg * self, }; l3cd = nm_l3_config_data_new(nm_platform_get_multi_idx(self->priv.platform), - self->priv.ifindex); + self->priv.ifindex, + NM_IP_CONFIG_SOURCE_UNKNOWN); for (i = 0; i < l3_config_datas_len; i++) { const L3ConfigData *l3cd_data = l3_config_datas_arr[i]; diff --git a/src/core/tests/test-l3cfg.c b/src/core/tests/test-l3cfg.c index ba065c09b1..d877b5dc5a 100644 --- a/src/core/tests/test-l3cfg.c +++ b/src/core/tests/test-l3cfg.c @@ -389,7 +389,7 @@ test_l3cfg(gconstpointer test_data) { nm_auto_unref_l3cd_init NML3ConfigData *l3cd = NULL; - l3cd = nm_l3_config_data_new(f->multiidx, f->ifindex0); + l3cd = nm_l3_config_data_new(f->multiidx, f->ifindex0, NM_IP_CONFIG_SOURCE_UNKNOWN); nm_l3_config_data_add_address_4( l3cd,