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.
This commit is contained in:
Thomas Haller 2022-12-16 18:07:07 +01:00
parent 2c22c96235
commit 5579fca916
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
7 changed files with 51 additions and 16 deletions

View file

@ -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));
}

View file

@ -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];

View file

@ -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);
}

View file

@ -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,

View file

@ -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__ */

View file

@ -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,

View file

@ -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"
/*****************************************************************************/