Commit graph

28625 commits

Author SHA1 Message Date
Thomas Haller
252e4a676b
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.
2021-06-17 17:49:44 +02:00
Thomas Haller
cea52c7cbd
libnm: add nm_connection_serialization_options_equal() helper 2021-06-17 17:48:16 +02:00
Thomas Haller
92daaff7d1
glib-aux: add NM_STRV_EMPTY_CC() helper macro 2021-06-17 17:48:15 +02:00
Thomas Haller
60957a4c8a
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.
2021-06-17 17:48:14 +02:00
Thomas Haller
e7b5650eff
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.
2021-06-17 17:48:13 +02:00
Thomas Haller
1f09e13f43
core: add nm_settings_connection_cmp_autoconnect_priority_with_data() helper 2021-06-17 17:48:13 +02:00
Thomas Haller
f51d3862f9
glib-aux: add nm_utils_ptrarray_is_sorted() helper 2021-06-17 17:48:12 +02:00
Thomas Haller
85df025e93
core: avoid undefined behavior comparing plain pointer values in _cmp_last_resort() 2021-06-17 17:48:11 +02:00
Thomas Haller
7a71aedf46
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.
2021-06-17 17:48:11 +02:00
Thomas Haller
f3abf2491a
libnm: add code comment about preserving ABI for libnm GObject structs 2021-06-17 17:48:10 +02:00
Thomas Haller
b0f4bb84bf
libnm: avoid cloning buffer for nm_connection_get_settings() in nm_keyfile_write() 2021-06-17 17:48:10 +02:00
Thomas Haller
5aef93355f
libnm: add _nm_connection_get_settings_arr() helper 2021-06-17 17:48:09 +02:00
Thomas Haller
207b101238
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.
2021-06-17 17:48:09 +02:00
Thomas Haller
d829849a7b
libnm: avoid cloning list of settings in nm_connection_to_dbus_full() 2021-06-17 17:48:09 +02:00
Thomas Haller
97eef2bf6d
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.
2021-06-17 17:48:08 +02:00
Thomas Haller
c8c606b323
libnm: avoid cloning list of settings in _nm_connection_verify() 2021-06-17 17:48:08 +02:00
Thomas Haller
91aacbef41
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% )
2021-06-17 17:48:08 +02:00
Thomas Haller
042cd99049
libnm/tests: test consistency for nm_meta_setting_types_by_priority 2021-06-17 17:48:08 +02:00
Thomas Haller
b7a7cc1b13
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.
2021-06-17 17:48:08 +02:00
Thomas Haller
1a5a4838f1
libnm: pack NMMetaSettingType enum
We keep the enum around in memory, so let's make it smaller/packed.
2021-06-17 17:48:08 +02:00
Thomas Haller
16b01233fa
libnm: add nm_meta_setting_info helpers 2021-06-17 17:48:07 +02:00
Thomas Haller
1b895d98c0
example: drop unused import from "nm-up-many.py"
lgtm.com complains about this. Avoid the warning.
2021-06-17 15:09:48 +02:00
Thomas Haller
161cf11cb1
release: bump version to 1.33.1 (development) 2021-06-16 20:26:45 +02:00
Thomas Haller
35b0b4c938
NEWS: update 2021-06-16 20:14:47 +02:00
Thomas Haller
5286170fa8
NEWS: update 2021-06-16 20:12:07 +02:00
Thomas Haller
d9c0d43879
release: bump version to 1.32.0 2021-06-16 19:48:59 +02:00
Thomas Haller
f330413f8a
NEWS: update 2021-06-16 19:06:47 +02:00
Thomas Haller
2d46c10fa3
contrib/rpm: configure defaults for iptables/nftables when generating distribution tarball
(cherry picked from commit e831a67cbc)
2021-06-16 19:03:28 +02:00
Beniamino Galvani
6914f4213f nm-daemon-helper: fix indentation
Ops.

Fixes: 9b802ff7f2 ('nm-daemon-helper: fix build with musl libc')
(cherry picked from commit e3e0afb011)
2021-06-16 18:33:59 +02:00
Beniamino Galvani
e3e0afb011 nm-daemon-helper: fix indentation
Ops.

Fixes: 9b802ff7f2 ('nm-daemon-helper: fix build with musl libc')
2021-06-16 18:32:44 +02:00
Beniamino Galvani
1d2f8f983c nm-daemon-helper: fix build with musl libc
NSS is not available in musl libc:

./src/nm-daemon-helper/nm-daemon-helper.c:9:10: fatal error: nss.h: No such file or directory
    9 | #include <nss.h>
      |          ^~~~~~~

(cherry picked from commit 9b802ff7f2)
2021-06-16 18:26:07 +02:00
Beniamino Galvani
9b802ff7f2 nm-daemon-helper: fix build with musl libc
NSS is not available in musl libc:

./src/nm-daemon-helper/nm-daemon-helper.c:9:10: fatal error: nss.h: No such file or directory
    9 | #include <nss.h>
      |          ^~~~~~~
2021-06-16 18:19:35 +02:00
Thomas Haller
798395ed33
NEWS: update
(cherry picked from commit 32bc29591c)
2021-06-16 16:58:44 +02:00
Thomas Haller
32bc29591c
NEWS: update 2021-06-16 16:26:15 +02:00
Beniamino Galvani
a06cd08430 NEWS: update
(cherry picked from commit e29d2f2200)
2021-06-16 16:19:23 +02:00
Beniamino Galvani
e29d2f2200 NEWS: update 2021-06-16 15:30:02 +02:00
Beniamino Galvani
098b4abe06 firewall: fix double free
Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
(cherry picked from commit 0cd20f94d0)
2021-06-15 19:45:48 +02:00
Beniamino Galvani
0cd20f94d0 firewall: fix double free
Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
2021-06-15 19:40:59 +02:00
Thomas Haller
46533e7da4
firewall: use nm_g_main_context_push_thread_default_if_necessary() in _fw_nft_call_communicate_cb()
There is no need to push (and pop) the same main-context.

(cherry picked from commit 654bb92a69)
2021-06-15 19:13:54 +02:00
Thomas Haller
4badc1f33a
firewall: fix signalling timeout error reason from _fw_nft_call()
During timeout we cancel the (internal) GCancellable. But the overall
error reason is not cancellation by the user, it's timeout. Fix
the error reason.

Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
(cherry picked from commit 897c6a5744)
2021-06-15 19:13:53 +02:00
Thomas Haller
eea912dfb3
libnm: expose nm_ip_address_dup(), nm_ip_route_dup() API in libnm
This fixes commit 21c8a6b20e ('libnm-core, all: merge IPv4 and IPv6
address/route types'), which introduced this API but didn't export it
in the library. In practice this API is thus only usable since 1.32.0.

(cherry picked from commit 05f2a0b024)
2021-06-15 19:13:27 +02:00
Thomas Haller
05f2a0b024
libnm: expose nm_ip_address_dup(), nm_ip_route_dup() API in libnm
This fixes commit 21c8a6b20e ('libnm-core, all: merge IPv4 and IPv6
address/route types'), which introduced this API but didn't export it
in the library. In practice this API is thus only usable since 1.32.0.
2021-06-15 19:11:57 +02:00
Thomas Haller
654bb92a69
firewall: use nm_g_main_context_push_thread_default_if_necessary() in _fw_nft_call_communicate_cb()
There is no need to push (and pop) the same main-context.
2021-06-15 18:25:22 +02:00
Thomas Haller
897c6a5744
firewall: fix signalling timeout error reason from _fw_nft_call()
During timeout we cancel the (internal) GCancellable. But the overall
error reason is not cancellation by the user, it's timeout. Fix
the error reason.

Fixes: 9ebdb967de ('firewall: implement masquerading for shared mode with nftables')
2021-06-15 18:15:56 +02:00
Thomas Haller
b8ae2dfa70
core/trivial: add comment about GMainContext to nm_shutdown_wait_obj_register_full() 2021-06-15 18:15:56 +02:00
Thomas Haller
123b408c32
firewall: change default for firewall-backend to "nftables""
It seems that the nftables backend works well. Let's change the default.
This will also be backported to nm-1-32 branch, for 1.32.0 release.

This reverts commit 0609f1f31c.

(cherry picked from commit 79d9441567)
2021-06-14 13:56:21 +02:00
Thomas Haller
79d9441567
firewall: change default for firewall-backend to "nftables""
It seems that the nftables backend works well. Let's change the default.
This will also be backported to nm-1-32 branch, for 1.32.0 release.

This reverts commit 0609f1f31c.
2021-06-14 11:17:25 +02:00
Thomas Haller
be4b997e4f
examples: add "examples/python/gi/nm-up-many.py"
It's an example for how to use libnm and asynchronous API.
But it's also a script I will use to test activating many
profiles in parallel.

Also add a test script that creates many veth interfaces and connection
profiles. So now you can do:

   sudo NUM_DEVS=100 contrib/scripts/test-create-many-device-setup.sh setup
   ./examples/python/gi/nm-up-many.py c-a{1..100}

and cleanup with

   nmcli connection down c-a{1..100}
   sudo contrib/scripts/test-create-many-device-setup.sh cleanup

Of course, be careful to do this on your production machine.
2021-06-11 22:48:41 +02:00
Thomas Haller
f353cb95e5
all: merge branch 'th/coverity-fixes'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/883

(cherry picked from commit c47ad0f754)
2021-06-11 22:44:32 +02:00
Thomas Haller
6df0c8e9bb
libnm/tests: avoid coverity warning in test_setting_connection_secondaries_verify()
nm_strvarray_get_strv() returns the input pointer itself, if _secondaries is NULL.
It does so intentionally and correctly to create an artificial empty strv array.

Coverity doesn't like this. Try to workaround the warning:

    Error: ARRAY_VS_SINGLETON (CWE-119): [#def484]
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: address_of: Taking address with "&_secondaries" yields a singleton pointer.
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: identity_transfer: Passing "&_secondaries" as argument 1 to function "nm_strvarray_get_strv", which returns that argument.
    NetworkManager-1.31.90/src/libnm-core-impl/tests/test-setting.c:4544: callee_ptr_arith: Passing "_Generic (nm_strvarray_get_strv(&_secondaries, NULL), char const * const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const ** : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char * const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char ** : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void const * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void * : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const * const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char const ** const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char * const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), char ** const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void const * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL), void * const : (char const * const *)nm_strvarray_get_strv(&_secondaries, NULL))" to function "_nm_utils_strv_cmp_n" which uses it as an array. This might corrupt or misinterpret adjacent memory locations.
    # 4542|       G_STMT_END
    # 4543|
    # 4544|->         _assert_secondaries(s_con, (const char *const *) arr->pdata);
    # 4545|
    # 4546|           /* reimplement the normalization that we expect to happen and

(cherry picked from commit 14ec96f262)
2021-06-11 22:44:32 +02:00