Commit graph

28598 commits

Author SHA1 Message Date
Thomas Haller
0432c2bcdb
trivial: fix code formatting
Fixes: ac090edd87 ('libnm: add type enum for handling gprop differences in to_dbus_fcn')
(cherry picked from commit 6ac304b673)
2021-06-23 14:58:53 +02:00
Thomas Haller
11fd85b3e1
libnm: merge branch 'th/setting-to-dbus-fcn'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/900

(cherry picked from commit 74996782eb)
2021-06-23 13:02:34 +02:00
Thomas Haller
17fab764fc
libnm: use _nm_setting_property_define_string() for string NMSetting properties
(cherry picked from commit a3eb2c7026)
2021-06-23 13:02:30 +02:00
Thomas Haller
2e93f82746
libnm: use _nm_setting_property_define_boolean() for boolean NMSetting properties
(cherry picked from commit 22edf34ba3)
2021-06-23 13:02:28 +02:00
Thomas Haller
1cc3b9c753
libnm: use new including_default flag and boolean getter for "ethernet.auto-negotiate" property
(cherry picked from commit d5c4378cdf)
2021-06-23 13:02:27 +02:00
Thomas Haller
4cea3e4f44
libnm: add flag to control whether to_dbus_fcn() should handle default values
Usually, properties that are set to their default are not serialized on
D-Bus. That is, to_dbus_fcn() returns NULL.

In some cases, we explicitly want to always serialize the property. For
example, if we changed behavior and the libnm default value changed.
Then we want that the message on D-Bus is always clear about the used
value and not rely on the default value on the receiving side.

(cherry picked from commit acc3a66bf2)
2021-06-23 13:02:26 +02:00
Thomas Haller
b20941215e
libnm: extend to_dbus_fcn() property type for efficiently converting string property
(cherry picked from commit e435fdfedf)
2021-06-23 13:02:24 +02:00
Thomas Haller
7598eb6228
libnm: extend to_dbus_fcn() property type for efficiently converting boolean property
Most of our NMSetting properties are based around GObject properties,
and thus the tooling to convert a NMSetting to/from GVariant consists
of getting/setting a GValue.

We can do better.

For most of such properties we also define a C getter function, which
we can call with less overhead. All we need is to hook the C getter with
the property meta data.

As example, implement it for "connection.autoconnect".

The immediate goal of this is to reduce the overhead of to_dbus. But
note that also for comparison of two properties, there is the default
implementation which is used by the majority of properties. This
implementation converts the properties first to GVariant (via
to_dbus_fcn) and then compares the variants. What this commit also does,
is to hook up the property meta data with the C-getters. This is one step
towards also more efficiently compare properties using the naive C
getters. Likewise, the keyfile writer use g_object_get_property().
It also could do better.

(cherry picked from commit a832781a8a)
2021-06-23 13:02:23 +02:00
Thomas Haller
8416c42b9d
libnm: move gprop_to_dbus_fcn hook to NMSettInfoProperty
For each property we have meta data in form of NMSettInfoProperty.
Each meta data also has a NMSettInfoProperty.property_type
(NMSettInfoPropertType).

The property type is supposed to define common behaviors for properties,
while the property meta data has individual properties. The idea is that
several properties can share the same property-type, and that
per-property meta data is part of NMSettInfoProperty.

The distinction is not very strong, but note that all remaining uses
of NMSettInfoPropertType.gprop_to_dbus_fcn were part of a property
type that was only used for one single property. That lack of
reusability hints to a wrong use.

Move gprop_to_dbus_fcn to the property meta data as a new field
NMSettInfoProperty.to_dbus_data.

Note that NMSettInfoPropertType.gprop_from_dbus_fcn still suffers from
the same problem. But the from-dbus side is not yet addressed.

(cherry picked from commit 54cab39ac9)
2021-06-23 13:02:22 +02:00
Thomas Haller
7af962b330
libnm: change "nm_sett_info_propert_type_mac_address" to use gprop_type for to_dbus_fcn()
(cherry picked from commit ed57990b58)
2021-06-23 13:02:21 +02:00
Thomas Haller
7f21fbad31
libnm: change "nm_sett_info_propert_type_strdict" to use gprop_type for to_dbus_fcn()
(cherry picked from commit 6d39014660)
2021-06-23 13:02:20 +02:00
Thomas Haller
97a25c5965
libnm: change "nm_sett_info_propert_type_dcb_au" to use gprop_type for to_dbus_fcn()
(cherry picked from commit 8c0a8a6d9b)
2021-06-23 13:02:19 +02:00
Thomas Haller
3dfd563601
libnm: add type enum for handling gprop differences in to_dbus_fcn
For GBytes, GEnum, GFlags and others, we need special converters from the
default GObject properties to GVariant.

Previously, those were implemented by providing a special
gprop_to_dbus_fcn hook. But gprop_to_dbus_fcn should move
from NMSettInfoPropertType to NMSettInfoProperty, because it's
usually a per-property meta data, and not a per-property-type meta data.
The difference is whether the meta data can be shared between different
properties (of the same "type).

In these cases, this extra information is indeed part of the type.
We want to have a generic NM_SETT_INFO_PROPERT_TYPE_GPROP() property
(using _nm_setting_property_to_dbus_fcn_gprop()), but then we would like
to distinguish between special cases. So this was fine.

However, I find the approach of providing a gprop_to_dbus_fcn in this
case cumbersome. It makes it harder to understand what happens. Instead,
introduce a new "gprop_type" for the different types that
_nm_setting_property_to_dbus_fcn_gprop() can handle.

This new "gprop_type" is extra data of the property type, so
introduce a new field "typdata_to_dbus".

(cherry picked from commit ac090edd87)
2021-06-23 13:02:19 +02:00
Thomas Haller
d2e0a8cc0e
libnm: add and use NM_SETT_INFO_PROPERT_TYPE_*_INIT() macros
The advantage is that we use similar macros for initializing the
static structs like

   const NMSettInfoPropertType nm_sett_info_propert_type_cloned_mac_address;

and the ad-hoc locations that use NM_SETT_INFO_PROPERT_TYPE().

The former exist for property types that are used more than once.
The latter exist for convenience, where a property type is implemented
at only one place.

Also, there are few direct references to _nm_setting_property_to_dbus_fcn_gprop().
all users use NM_SETT_INFO_PROPERT_TYPE_GPROP() or
NM_SETT_INFO_PROPERT_TYPE_GPROP_INIT().

(cherry picked from commit 6fc2e03677)
2021-06-23 13:02:18 +02:00
Thomas Haller
0d95b3c300
libnm: add and use NM_SETT_INFO_PROPERT_TYPE_DBUS() macro
(cherry picked from commit 69597a67c1)
2021-06-23 13:02:17 +02:00
Thomas Haller
db9ae06cfd
libnm: let all property types implement to_dbus_fcn() handler
If a property can be converted to D-Bus, then always set the
to_dbus_fcn() handler. The only caller of to_dbus_fcn() is
property_to_dbus(), so this means that property_to_dbus()
has no more default implementation and always delegates to
to_dbus_fcn().

The code is easier to understand if all properties implement
to_dbus_fcn() the same way.

Also, there is supposed to be a split between NMSettInfoProperty (info about
the property) and NMSettInfoPropertType (the type). The idea is that
each property (obviously) requires its distinct NMSettInfoProperty, but
they can share a common type implementation.
With NMSettInfoPropertType.gprop_to_dbus_fcn that is often violated because
many properties that implement NMSettInfoPropertType.gprop_to_dbus_fcn
require a special type implementation. As such, gprop_to_dbus_fcn should
be part of the property info and not the property type. The first step towards
that is unifying all properties to use to_dbus_fcn().

(cherry picked from commit c161439b73)
2021-06-23 13:02:17 +02:00
Thomas Haller
e4898f98e0
libnm: add NM_SETTING_PARAM_NONE define
This completes other NM_SETTING_PARAM_* flags.

(cherry picked from commit c54be51f99)
2021-06-23 13:02:16 +02:00
Thomas Haller
1ebef50603
libnm: drop unused parameter "ignored_default" from property_to_dbus()
(cherry picked from commit 4065158491)
2021-06-23 13:02:16 +02:00
Thomas Haller
3e58861336
libnm: simplify assertions for valid NMSettInfoProperty
(cherry picked from commit 21638c54b0)
2021-06-23 13:02:16 +02:00
Thomas Haller
d19c0937d4
libnm: expose internal helper nm_utils_hwaddr_to_dbus()
(cherry picked from commit 8081e39ab6)
2021-06-23 13:02:15 +02:00
Thomas Haller
445d01e2c6
glib-aux: add NM_G_PARAM_SPEC_GET_DEFAULT_STRING() helper
(cherry picked from commit 87229b22ae)
2021-06-23 13:02:15 +02:00
Thomas Haller
d99e20e436
glib-aux: add nm_g_variant_singleton_s_empty() helper
(cherry picked from commit e2defd0115)
2021-06-23 13:02:15 +02:00
Thomas Haller
b33ba1ef5c
glib-aux: add nm_g_variant_singleton_b() helper
(cherry picked from commit edb31252cc)
2021-06-23 13:02:15 +02:00
Thomas Haller
aa1be0559d
config: merge branch 'th/config-match-lookup'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/894

(cherry picked from commit 3e7a589972)
2021-06-21 17:31:07 +02:00
Thomas Haller
e1d28afe4b
config: avoid cloning string during nm_config_data_get_connection_default() et al.
NMConfigData is immutable and with the previous commit are the strings
already cached internally. There is no need to clone it.

Of course, the callers must not assume that the string stays alive after
a config reload (SIGHUP), where the NMConfigData might change. So they
are not always alive, but long enough for all callers to avoid cloning
the string.

(cherry picked from commit 9452d69465)
2021-06-21 17:31:06 +02:00
Thomas Haller
ba2a6ee384
config: add lookup index for _match_section_infos_lookup()
Previously, we would call g_key_file_get_string(), which requires
two hash lookups (one for the group and one for the key).

We can do better. Especially since NMConfigData is immutable, it's
simple to build a lookup index of the values we have and then do binary
search.

Note that we call nm_config_data_get_connection_default() and similar
API *a lot*, so this is measurable.

(cherry picked from commit 2f9ab1d528)
2021-06-21 17:31:06 +02:00
Thomas Haller
34e4364558
core: use nm_config_data_get_device_config_int64() for getting integer setting
(cherry picked from commit b929caa95c)
2021-06-21 17:31:05 +02:00
Thomas Haller
6bbf1b14c9
config: add nm_config_data_get_device_config_int64() helper
(cherry picked from commit a6c9f2518e)
2021-06-21 17:31:05 +02:00
Thomas Haller
cc5a0f517e
glib-aux: cleanup nm_utils_named_value_clear_with_g_free() implementation
(cherry picked from commit 42ba464380)
2021-06-21 17:31:05 +02:00
Thomas Haller
2fe9d99fc0
all: merge branch 'th/connection-track-setting-rework'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/890

(cherry picked from commit 8f3892ba3e)
2021-06-21 17:25:46 +02:00
Thomas Haller
c11559ffa8
core: avoid checking sort order for cached settings list
We now have a cached list of NMSettingsConnection instances,
sorted by their autoconnect priority.

However, the sort order nm_settings_connection_cmp_autoconnect_priority()
depends on various properties of the connection:

 - "connection.autoconnect" and "connection.autoconnect-priority"
 - the timestamp
 - "connection.uuid"

These properties almost never change, so it's a waste that every call
to nm_settings_get_connections_sorted_by_autoconnect_priority() needs
to check whether the sort order is still satisfied.

We can do better by tracking when the sort order might have been
destroyed and only check in those (much fewer) cases.

Note that we end up calling nm_settings_get_connections_sorted_by_autoconnect_priority()
a lot, so this makes a difference.

(cherry picked from commit 877d2b236f)
2021-06-21 17:25:44 +02:00
Thomas Haller
124627151f
core: cache GVariant for result of GetSettings()
The GetSettings() call is not the only place where we convert a
NMConnection to D-Bus. However it is one of the most prominent ones
with a measurable performance overhead.

The connection seldom changes, so it makes sense to cache it.

Note that GetSettings() is the only caller that specifies an option,
thus it's the only caller that converts a NMConnection to variant
in this particular way. That means, other callers don't benefit from
this caching and we could not cache the variant in the NMConnection
instance itself, because those callers use different parameters.

(cherry picked from commit 252e4a676b)
2021-06-21 17:25:43 +02:00
Thomas Haller
b9d4f7dd00
libnm: add nm_connection_serialization_options_equal() helper
(cherry picked from commit cea52c7cbd)
2021-06-21 17:25:42 +02:00
Thomas Haller
91e6f4ff52
glib-aux: add NM_STRV_EMPTY_CC() helper macro
(cherry picked from commit 92daaff7d1)
2021-06-21 17:25:41 +02:00
Thomas Haller
feb262f24d
libnm: add helper functions for emitting signals in NMConnection
Not very useful, but it seems nicer to read. They anyway can be
inlined. After all, naming and structure is important and the places
where we emit signals are important. By having well-named helper
functions, these places are easier to find and reason about.

(cherry picked from commit 60957a4c8a)
2021-06-21 17:25:40 +02:00
Thomas Haller
bce9de78d9
core: add nm_settings_get_connection_sorted_by_autoconnect_priority()
Turns out, we call nm_settings_get_connection_clone() *a lot* with sort order
nm_settings_connection_cmp_autoconnect_priority_p_with_data().

As we cache the (differently sorted) list of connections, also cache
the presorted list. The only complication is that every time we still
need to check whether the list is still sorted, because it would be
more complicated to invalidate the list when an entry changes which
affects the sort order. Still, such a check is usually successful
and requires "only" N-1 comparisons.

(cherry picked from commit e7b5650eff)
2021-06-21 17:25:40 +02:00
Thomas Haller
ecdd64cb1d
core: add nm_settings_connection_cmp_autoconnect_priority_with_data() helper
(cherry picked from commit 1f09e13f43)
2021-06-21 17:25:39 +02:00
Thomas Haller
830c970196
glib-aux: add nm_utils_ptrarray_is_sorted() helper
(cherry picked from commit f51d3862f9)
2021-06-21 17:25:38 +02:00
Thomas Haller
c5914709f2
core: avoid undefined behavior comparing plain pointer values in _cmp_last_resort()
(cherry picked from commit 85df025e93)
2021-06-21 17:25:38 +02:00
Thomas Haller
5ccbe04788
libnm: optimize NM_CONNECTION_GET_PRIVATE() for NMSimpleConnection
NMConnection is a glib interface, implemented only by NMSimpleConnection
and NMRemoteConnection.

Inside the daemon, every NMConnection instance is always a NMSimpleConnection.

Using glib interfaces has an overhead, for example NM_IS_CONNECTION() needs
to search the implemented types for the pointer. And NM_CONNECTION_GET_PRIVATE()
is implemented by attaching user data to the GObject instance. Both have measurable
overhead.

Special case them for NMSimpleConnection.

This optimizes primarily the call to nm_connection_get_setting_connection(),
which easily gets called millions of times. This is easily measurable.

(cherry picked from commit 7a71aedf46)
2021-06-21 17:25:37 +02:00
Thomas Haller
1c6b50f6ea
libnm: add code comment about preserving ABI for libnm GObject structs
(cherry picked from commit f3abf2491a)
2021-06-21 17:25:36 +02:00
Thomas Haller
c4257c1c7f
libnm: avoid cloning buffer for nm_connection_get_settings() in nm_keyfile_write()
(cherry picked from commit b0f4bb84bf)
2021-06-21 17:25:36 +02:00
Thomas Haller
919618372c
libnm: add _nm_connection_get_settings_arr() helper
(cherry picked from commit 5aef93355f)
2021-06-21 17:25:36 +02:00
Thomas Haller
4949434dac
libnm: take reference to settings in nm_connection_for_each_setting_value()
As we iterate over the settings, let's ensure that they stay
alive while we call back to the user data.

(cherry picked from commit 207b101238)
2021-06-21 17:25:35 +02:00
Thomas Haller
eb10539b80
libnm: avoid cloning list of settings in nm_connection_to_dbus_full()
(cherry picked from commit d829849a7b)
2021-06-21 17:25:35 +02:00
Thomas Haller
07ba3cb0d7
libnm: implement nm_connection_get_setting*() via NMMetaSettingType
The NM_TYPE_SETTING_* macros are really function calls (to a GType/gsize which is
guarded by an atomic operation for thread safe initialization). Also, finding
the setting_info based on the GType requires additional lookups.

It's no longer necessary. We can directly find the setting using the
well known index.

(cherry picked from commit 97eef2bf6d)
2021-06-21 17:25:35 +02:00
Thomas Haller
61013c6995
libnm: avoid cloning list of settings in _nm_connection_verify()
(cherry picked from commit c8c606b323)
2021-06-21 17:25:35 +02:00
Thomas Haller
0a22f4e490
libnm: refactor tracking of NMSetting in NMConnection
A NMConnection tracks a list of NMSetting instances. For
each setting type, it only can track one instance, as is
clear by the API nm_connection_get_setting().

The number of different setting types is known at compile time,
currently it is 52. Also, we have an NMMetaSettingType enum,
which assigns each type a number.

Previously, we were tracking the settings in a GHashTable.
Rework that, to instead use a fixed size array.

Now every NMConnection instance consumes 52 * sizeof(pointer)
for the settings array. Previously, the GHashTable required to malloc
the "struct _GHashTable" (on 64bit that is about the size of 12
pointers) and for N settings it allocated two buffers (for
the key and the values) plus one buffer for the hash values. So,
it may or may not consume a bit more memory now, but also can lookup
settings directly without hashing.

When looking at all settings, we iterate the entire array. Most
entries will be NULL, so it's a question whether this could be done
better. But as the array is of a fixed, small size, naive iteration
is probably still faster and simpler than anything else.

---

Test: compiled with -O2, x86_64:

  $ T=src/core/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh; \
    make -j 8 "$T" && \
    "$T" 1>/dev/null && \
    perf stat -r 200 -B "$T" 1>/dev/null

Before:

 Performance counter stats for 'src/core/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh' (200 runs):

            338.39 msec task-clock:u              #    0.962 CPUs utilized            ( +-  0.68% )
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
             1,121      page-faults:u             #    0.003 M/sec                    ( +-  0.03% )
     1,060,001,815      cycles:u                  #    3.132 GHz                      ( +-  0.50% )
     1,877,905,122      instructions:u            #    1.77  insn per cycle           ( +-  0.01% )
       374,065,113      branches:u                # 1105.429 M/sec                    ( +-  0.01% )
         6,862,991      branch-misses:u           #    1.83% of all branches          ( +-  0.36% )

           0.35185 +- 0.00247 seconds time elapsed  ( +-  0.70% )

After:

 Performance counter stats for 'src/core/settings/plugins/ifcfg-rh/tests/test-ifcfg-rh' (200 runs):

            328.07 msec task-clock:u              #    0.959 CPUs utilized            ( +-  0.39% )
                 0      context-switches:u        #    0.000 K/sec
                 0      cpu-migrations:u          #    0.000 K/sec
             1,130      page-faults:u             #    0.003 M/sec                    ( +-  0.03% )
     1,034,858,368      cycles:u                  #    3.154 GHz                      ( +-  0.33% )
     1,846,714,951      instructions:u            #    1.78  insn per cycle           ( +-  0.00% )
       369,754,267      branches:u                # 1127.052 M/sec                    ( +-  0.01% )
         6,594,396      branch-misses:u           #    1.78% of all branches          ( +-  0.23% )

           0.34193 +- 0.00145 seconds time elapsed  ( +-  0.42% )

(cherry picked from commit 91aacbef41)
2021-06-21 17:25:34 +02:00
Thomas Haller
5e3401fa31
libnm/tests: test consistency for nm_meta_setting_types_by_priority
(cherry picked from commit 042cd99049)
2021-06-21 17:25:34 +02:00
Thomas Haller
481e229450
libnm: add nm_meta_setting_types_by_priority array for sorting settings
nm_meta_setting_infos is a list of all NMMetaSettingInfo, sorted by name.
Add nm_meta_setting_types_by_priority which provides a mapping with a
different sort order (first by priority). We need that sometimes.

(cherry picked from commit b7a7cc1b13)
2021-06-21 17:25:34 +02:00