From 5579fca9163edc72cec443e2e6102015eda0743a Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 16 Dec 2022 18:07:07 +0100 Subject: [PATCH] platform: allow setting multi_idx instance for NMPlatform The major point of NMDedupMultiIndex is that it can de-duplicate the objects. It thus makes sense the everybody is using the same instance. Make the multi-idx instance of NMPlatform configurable. This is not used outside of unit tests, because the daemon currently always creates one platform instance and everybody then re-uses the instance of the platform. While this is (currently) only used by tests, and that the performance optimization of de-duplicating is irrelevant for tests, this is still useful. The test can then check whether two separate NMPlatform objects shared the same instance and whether it was de-duplicated. --- src/core/NetworkManagerUtils.c | 4 +-- src/core/platform/tests/test-link.c | 12 ++++---- .../platform/tests/test-platform-general.c | 4 +-- src/libnm-platform/nm-linux-platform.c | 7 ++++- src/libnm-platform/nm-linux-platform.h | 7 ++++- src/libnm-platform/nm-platform.c | 28 +++++++++++++++++-- src/libnm-platform/nm-platform.h | 5 ++-- 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/src/core/NetworkManagerUtils.c b/src/core/NetworkManagerUtils.c index f3c032ce9f..15a3e5ddcf 100644 --- a/src/core/NetworkManagerUtils.c +++ b/src/core/NetworkManagerUtils.c @@ -1848,11 +1848,11 @@ nm_platform_get(void) void nm_linux_platform_setup(void) { - nm_platform_setup(nm_linux_platform_new(FALSE, FALSE, FALSE)); + nm_platform_setup(nm_linux_platform_new(NULL, FALSE, FALSE, FALSE)); } void nm_linux_platform_setup_with_tc_cache(void) { - nm_platform_setup(nm_linux_platform_new(FALSE, FALSE, TRUE)); + nm_platform_setup(nm_linux_platform_new(NULL, FALSE, FALSE, TRUE)); } diff --git a/src/core/platform/tests/test-link.c b/src/core/platform/tests/test-link.c index c0321d3a3d..6c61bac2d2 100644 --- a/src/core/platform/tests/test-link.c +++ b/src/core/platform/tests/test-link.c @@ -2858,7 +2858,7 @@ _test_netns_create_platform(void) netns = nmp_netns_new(); g_assert(NMP_IS_NETNS(netns)); - platform = nm_linux_platform_new(TRUE, TRUE, TRUE); + platform = nm_linux_platform_new(NULL, TRUE, TRUE, TRUE); g_assert(NM_IS_LINUX_PLATFORM(platform)); nmp_netns_pop(netns); @@ -2947,7 +2947,7 @@ test_netns_general(gpointer fixture, gconstpointer test_data) if (_check_sysctl_skip()) return; - platform_1 = nm_linux_platform_new(TRUE, TRUE, TRUE); + platform_1 = nm_linux_platform_new(NULL, TRUE, TRUE, TRUE); platform_2 = _test_netns_create_platform(); /* add some dummy devices. The "other-*" devices are there to bump the ifindex */ @@ -3075,7 +3075,7 @@ test_netns_set_netns(gpointer fixture, gconstpointer test_data) if (_test_netns_check_skip()) return; - platforms[0] = platform_0 = nm_linux_platform_new(TRUE, TRUE, TRUE); + platforms[0] = platform_0 = nm_linux_platform_new(NULL, TRUE, TRUE, TRUE); platforms[1] = platform_1 = _test_netns_create_platform(); platforms[2] = platform_2 = _test_netns_create_platform(); @@ -3174,7 +3174,7 @@ test_netns_push(gpointer fixture, gconstpointer test_data) if (_check_sysctl_skip()) return; - pl[0].platform = platform_0 = nm_linux_platform_new(TRUE, TRUE, TRUE); + pl[0].platform = platform_0 = nm_linux_platform_new(NULL, TRUE, TRUE, TRUE); pl[1].platform = platform_1 = _test_netns_create_platform(); pl[2].platform = platform_2 = _test_netns_create_platform(); @@ -3321,7 +3321,7 @@ test_netns_bind_to_path(gpointer fixture, gconstpointer test_data) if (_test_netns_check_skip()) return; - platforms[0] = platform_0 = nm_linux_platform_new(TRUE, TRUE, TRUE); + platforms[0] = platform_0 = nm_linux_platform_new(NULL, TRUE, TRUE, TRUE); platforms[1] = platform_1 = _test_netns_create_platform(); platforms[2] = platform_2 = _test_netns_create_platform(); @@ -3486,7 +3486,7 @@ test_sysctl_netns_switch(void) if (_test_netns_check_skip()) return; - platforms[0] = platform_0 = nm_linux_platform_new(TRUE, TRUE, TRUE); + platforms[0] = platform_0 = nm_linux_platform_new(NULL, TRUE, TRUE, TRUE); platforms[1] = platform_1 = _test_netns_create_platform(); platforms[2] = platform_2 = _test_netns_create_platform(); PL = platforms[nmtst_get_rand_uint32() % 3]; diff --git a/src/core/platform/tests/test-platform-general.c b/src/core/platform/tests/test-platform-general.c index db2a705435..cebd44f65b 100644 --- a/src/core/platform/tests/test-platform-general.c +++ b/src/core/platform/tests/test-platform-general.c @@ -31,7 +31,7 @@ test_init_linux_platform(void) { gs_unref_object NMPlatform *platform = NULL; - platform = nm_linux_platform_new(TRUE, NM_PLATFORM_NETNS_SUPPORT_DEFAULT, TRUE); + platform = nm_linux_platform_new(NULL, TRUE, NM_PLATFORM_NETNS_SUPPORT_DEFAULT, TRUE); } /*****************************************************************************/ @@ -42,7 +42,7 @@ test_link_get_all(void) gs_unref_object NMPlatform *platform = NULL; gs_unref_ptrarray GPtrArray *links = NULL; - platform = nm_linux_platform_new(TRUE, NM_PLATFORM_NETNS_SUPPORT_DEFAULT, TRUE); + platform = nm_linux_platform_new(NULL, TRUE, NM_PLATFORM_NETNS_SUPPORT_DEFAULT, TRUE); links = nm_platform_link_get_all(platform, TRUE); } diff --git a/src/libnm-platform/nm-linux-platform.c b/src/libnm-platform/nm-linux-platform.c index 204c8b2028..762b1645e1 100644 --- a/src/libnm-platform/nm-linux-platform.c +++ b/src/libnm-platform/nm-linux-platform.c @@ -10991,7 +10991,10 @@ path_is_read_only_fs(const char *path) } NMPlatform * -nm_linux_platform_new(gboolean log_with_ptr, gboolean netns_support, gboolean cache_tc) +nm_linux_platform_new(NMDedupMultiIndex *multi_idx, + gboolean log_with_ptr, + gboolean netns_support, + gboolean cache_tc) { gboolean use_udev = FALSE; @@ -10999,6 +11002,8 @@ nm_linux_platform_new(gboolean log_with_ptr, gboolean netns_support, gboolean ca use_udev = TRUE; return g_object_new(NM_TYPE_LINUX_PLATFORM, + NM_PLATFORM_MULTI_IDX, + multi_idx, NM_PLATFORM_LOG_WITH_PTR, log_with_ptr, NM_PLATFORM_USE_UDEV, diff --git a/src/libnm-platform/nm-linux-platform.h b/src/libnm-platform/nm-linux-platform.h index 546387a9d5..08135a4acb 100644 --- a/src/libnm-platform/nm-linux-platform.h +++ b/src/libnm-platform/nm-linux-platform.h @@ -23,6 +23,11 @@ typedef struct _NMLinuxPlatformClass NMLinuxPlatformClass; GType nm_linux_platform_get_type(void); -NMPlatform *nm_linux_platform_new(gboolean log_with_ptr, gboolean netns_support, gboolean cache_tc); +struct _NMDedupMultiIndex; + +NMPlatform *nm_linux_platform_new(struct _NMDedupMultiIndex *multi_idx, + gboolean log_with_ptr, + gboolean netns_support, + gboolean cache_tc); #endif /* __NETWORKMANAGER_LINUX_PLATFORM_H__ */ diff --git a/src/libnm-platform/nm-platform.c b/src/libnm-platform/nm-platform.c index f5aaa6fcc9..6fad736cce 100644 --- a/src/libnm-platform/nm-platform.c +++ b/src/libnm-platform/nm-platform.c @@ -184,6 +184,7 @@ static guint signals[_NM_PLATFORM_SIGNAL_ID_LAST] = {0}; enum { PROP_0, + PROP_MULTI_IDX, PROP_NETNS_SUPPORT, PROP_USE_UDEV, PROP_LOG_WITH_PTR, @@ -9621,6 +9622,20 @@ set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *ps NMPlatformPrivate *priv = NM_PLATFORM_GET_PRIVATE(self); switch (prop_id) { + case PROP_MULTI_IDX: + /* construct-only */ + { + NMDedupMultiIndex *multi_idx; + + multi_idx = g_value_get_pointer(value); + if (!multi_idx) + multi_idx = nm_dedup_multi_index_new(); + else + multi_idx = nm_dedup_multi_index_ref(multi_idx); + + priv->multi_idx = multi_idx; + break; + } case PROP_NETNS_SUPPORT: /* construct-only */ if (g_value_get_boolean(value)) { @@ -9667,8 +9682,9 @@ constructor(GType type, guint n_construct_params, GObjectConstructParam *constru self = NM_PLATFORM(object); priv = NM_PLATFORM_GET_PRIVATE(self); - priv->multi_idx = nm_dedup_multi_index_new(); - priv->cache = nmp_cache_new(priv->multi_idx, priv->use_udev); + nm_assert(priv->multi_idx); + + priv->cache = nmp_cache_new(priv->multi_idx, priv->use_udev); c_list_init(&priv->ip6_dadfailed_lst_head); return object; @@ -9708,6 +9724,14 @@ nm_platform_class_init(NMPlatformClass *platform_class) platform_class->wifi_set_powersave = wifi_set_powersave; + g_object_class_install_property( + object_class, + PROP_MULTI_IDX, + g_param_spec_pointer(NM_PLATFORM_MULTI_IDX, + "", + "", + G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS)); + g_object_class_install_property( object_class, PROP_NETNS_SUPPORT, diff --git a/src/libnm-platform/nm-platform.h b/src/libnm-platform/nm-platform.h index ea46321534..dec38c1c93 100644 --- a/src/libnm-platform/nm-platform.h +++ b/src/libnm-platform/nm-platform.h @@ -23,10 +23,11 @@ /*****************************************************************************/ +#define NM_PLATFORM_CACHE_TC "cache-tc" +#define NM_PLATFORM_LOG_WITH_PTR "log-with-ptr" +#define NM_PLATFORM_MULTI_IDX "multi-idx" #define NM_PLATFORM_NETNS_SUPPORT "netns-support" #define NM_PLATFORM_USE_UDEV "use-udev" -#define NM_PLATFORM_LOG_WITH_PTR "log-with-ptr" -#define NM_PLATFORM_CACHE_TC "cache-tc" /*****************************************************************************/