Commit graph

29237 commits

Author SHA1 Message Date
Beniamino Galvani
e7fb9a682b tc: merge branch 'bg/tc-no-cache'
https://bugzilla.redhat.com/show_bug.cgi?id=1753677
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/981
2021-09-20 13:35:37 +02:00
Beniamino Galvani
d50c4eba9e core: disable tc cache by default
We no longer use tc objects from the platform cache; disable caching
by default.

The only exception where the cache is needed is in tc tests, as we
look into the platform there to check that objects look as expected.
2021-09-20 13:27:16 +02:00
Beniamino Galvani
864e4e6369 platform: allow disabling caching of tc objects
Introduce a construct-only property for platform objects to enable or
disable the caching of tc objects. When disabled, the netlink socket
doesn't receive netlink events for tc objects, and objects are never
added to the cache. This commit doesn't change behavior yet.
2021-09-20 13:27:16 +02:00
Beniamino Galvani
c896973deb platform: drop test-tc-fake
It doesn't seem useful to have a copy of the test which does nothing.
2021-09-20 13:27:15 +02:00
Beniamino Galvani
e0691f9528 device: ensure tc_commit() is called only once per activation
Stage2 can be called multiple times. Ensure that tc_commit() is only
called the first time. This is important now that tc synchronization
requires to clear all qdiscs and recreate them.
2021-09-20 13:27:15 +02:00
Beniamino Galvani
3981bff2a0 core: rework tc sync functions
Update nm_platform_qdisc_sync() and nm_platform_tfilter_sync() to
avoid looking into the platform cache, so that we no longer require to
keep tc and qdiscs in the cache.

There is no API in kernel to retrieve tc objects only for a specific
interface, so NM had to receive all tc events, even for unmanaged
interfaces.  This could cause high CPU usage in some scenarios with
many objects.

Instead, try to delete root qdiscs and filters and then add the known
ones.

Also, combine the two functions together since they are related. In
particular, removing all qdiscs also removes all attached filters.
2021-09-20 13:27:15 +02:00
Beniamino Galvani
d9b2e9d7ea platform: add methods to delete tc qdiscs and tfilters
Introduce two platform methods to delete tc qdiscs and filters by
ifindex and parent.
2021-09-20 13:27:15 +02:00
Beniamino Galvani
8003ca68f7 platform: preserve IPv6 multicast route added by kernel
Kernels < 5.11 add a route like:

  unicast ff00::/8 dev $IFACE proto boot scope global metric 256 pref medium

to allow sending and receiving IPv6 multicast traffic. Ensure it's not
removed it when we do a route sync in mode ALL.

In kernel 5.11 there were commits:

  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ceed9038b2783d14e0422bdc6fd04f70580efb4c
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=a826b04303a40d52439aa141035fca5654ccaccd

After those the route looks like

  multicast ff00::/8 dev $IFACE proto kernel metric 256 pref medium

As NM ignores routes with rtm_type multicast, the code in this commit
is not needed on newer kernels.

https://bugzilla.redhat.com/show_bug.cgi?id=2004212
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/984
2021-09-20 10:27:38 +02:00
Thomas Haller
0d4840c484
l3cfg: fix assertion in NM_IS_L3_CONFIG_DATA() to allow NULL 2021-09-16 20:54:49 +02:00
Thomas Haller
882168c728
l3cfg: accept %NULL argument to nm_l3_config_data_seal()
There is always a question between convenience of allowing %NULL (and
do nothing) and strictly require the user to check the argument to not
be %NULL. In this case, it's more convenient to accept NULL, than require
the callers to check for it.
2021-09-16 20:54:49 +02:00
Thomas Haller
6f2c69f484
contrib: improve nm-in-container.sh script (5) 2021-09-16 20:54:49 +02:00
Thomas Haller
e947053c23
cloud-setup: merge branch 'th/cloud-setup-fix-containers'
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/740
https://bugzilla.redhat.com/show_bug.cgi?id=1977984#c27

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/974
2021-09-16 17:35:18 +02:00
Thomas Haller
fe80b2d1ec
cloud-setup: use suppress_prefixlength rule to honor non-default-routes in the main table
Background
==========

Imagine you run a container on your machine. Then the routing table
might look like:

    default via 10.0.10.1 dev eth0 proto dhcp metric 100
    10.0.10.0/28 dev eth0 proto kernel scope link src 10.0.10.5 metric 100
    [...]
    10.42.0.0/24 via 10.42.0.0 dev flannel.1 onlink
    10.42.1.2 dev cali02ad7e68ce1 scope link
    10.42.1.3 dev cali8fcecf5aaff scope link
    10.42.2.0/24 via 10.42.2.0 dev flannel.1 onlink
    10.42.3.0/24 via 10.42.3.0 dev flannel.1 onlink

That is, there are another interfaces with subnets and specific routes.

If nm-cloud-setup now configures rules:

    0:  from all lookup local
    30400:  from 10.0.10.5 lookup 30400
    32766:  from all lookup main
    32767:  from all lookup default

and

    default via 10.0.10.1 dev eth0 table 30400 proto static metric 10
    10.0.10.1 dev eth0 table 30400 proto static scope link metric 10

then these other subnets will also be reached via the default route.

This container example is just one case where this is a problem. In
general, if you have specific routes on another interface, then the
default route in the 30400+ table will interfere badly.

The idea of nm-cloud-setup is to automatically configure the network for
secondary IP addresses. When the user has special requirements, then
they should disable nm-cloud-setup and configure whatever they want.
But the container use case is popular and important. It is not something
where the user actively configures the network. This case needs to work better,
out of the box. In general, nm-cloud-setup should work better with the
existing network configuration.

Change
======

Add new routing tables 30200+ with the individual subnets of the
interface:

    10.0.10.0/24 dev eth0 table 30200 proto static metric 10
    [...]
    default via 10.0.10.1 dev eth0 table 30400 proto static metric 10
    10.0.10.1 dev eth0 table 30400 proto static scope link metric 10

Also add more important routing rules with priority 30200+, which select
these tables based on the source address:

    30200:  from 10.0.10.5 lookup 30200

These will do source based routing for the subnets on these
interfaces.

Then, add a rule with priority 30350

    30350:  lookup main suppress_prefixlength 0

which processes the routes from the main table, but ignores the default
routes. 30350 was chosen, because it's in between the rules 30200+ and
30400+, leaving a range for the user to configure their own rules.

Then, as before, the rules 30400+ again look at the corresponding 30400+
table, to find a default route.

Finally, process the main table again, this time honoring the default
route. That is for packets that have a different source address.

This change means that the source based routing is used for the
subnets that are configured on the interface and for the default route.
Whereas, if there are any more specific routes in the main table, they will
be preferred over the default route.

Apparently Amazon Linux solves this differently, by not configuring a
routing table for addresses on interface "eth0". That might be an
alternative, but it's not clear to me what is special about eth0 to
warrant this treatment. It also would imply that we somehow recognize
this primary interface. In practise that would be doable by selecting
the interface with "iface_idx" zero.

Instead choose this approach. This is remotely similar to what WireGuard does
for configuring the default route ([1]), however WireGuard uses fwmark to match
the packets instead of the source address.

[1] https://www.wireguard.com/netns/#improved-rule-based-routing
2021-09-16 17:30:25 +02:00
Thomas Haller
0978be5e43
cloud-setup: cleanup configuring addresses/routes/rules in _nmc_mangle_connection() 2021-09-16 15:51:03 +02:00
Thomas Haller
b68d694b78
cloud-setup: limit number of supported interfaces to avoid overlapping table numbers
The table number is chosen as 30400 + iface_idx. That is, the range is
limited and we shouldn't handle more than 100 devices. Add a check for
that and error out.
2021-09-16 15:51:03 +02:00
Thomas Haller
580c244f04
glib-aux: add ref/unref function for down-cast NMRefString 2021-09-16 15:51:02 +02:00
Thomas Haller
a95ea0eb29
cloud-setup: process iface-datas in sorted order
The routes/rules that are configured are independent of the
order in which we process the devices. That is, because they
use the "iface_idx" for cases where there is ambiguity.

Still, it feels nicer to always process them in a defined order.
2021-09-16 15:51:02 +02:00
Thomas Haller
1c5cb9d3c2
cloud-setup: track sorted list of NMCSProviderGetConfigIfaceData
Sorted by iface_idx. The iface_idx is probably something useful and
stable, provided by the provider. E.g. it's the order in which
interfaces are exposed on the meta data.
2021-09-16 15:51:02 +02:00
Thomas Haller
ec56fe60fb
cloud-setup: add "hwaddr" to NMCSProviderGetConfigIfaceData struct
get-config() gives a NMCSProviderGetConfigResult structure, and the
main part of data is the GHashTable of MAC addresses and
NMCSProviderGetConfigIfaceData instances.

Let NMCSProviderGetConfigIfaceData also have a reference to the MAC
address. This way, I'll be able to create a (sorted) list of interface
datas, that also contain the MAC address.
2021-09-16 15:51:02 +02:00
Thomas Haller
5f047968d7
cloud-setup: skip configuring policy routing if there is only one interface/address
nm-cloud-setup automatically configures the network. That may conflict
with what the user wants. In case the user configures some specific
setup, they are encouraged to disable nm-cloud-setup (and its
automatism).

Still, what we do by default matters, and should play as well with
user's expectations. Configuring policy routing and a higher priority
table (30400+) that hijacks the traffic can cause problems.

If the system only has one IPv4 address and one interface, then there
is no point in configuring policy routing at all. Detect that, and skip
the change in that case.

Note that of course we need to handle the case where previously multiple
IP addresses were configured and an update gives only one address. In
that case we need to clear the previously configured rules/routes. The
patch achieves this.
2021-09-16 15:51:02 +02:00
Thomas Haller
7969ae1a82
cloud-setup: count numbers of valid IPv4 addresses in get-config result
Will be used next.
2021-09-16 15:51:02 +02:00
Thomas Haller
a3cd66d3fa
cloud-setup: cache number of valid interfaces in get-config result
Now that we return a struct from get_config(), we can have system-wide
properties returned.

Let it count and cache the number of valid iface_datas.

Currently that is not yet used, but it will be.
2021-09-16 15:51:02 +02:00
Thomas Haller
323e182768
cloud-setup: return structure for get_config() result instead of generic hash table
Returning a struct seems easier to understand, because then the result
is typed.

Also, we might return additional results, which are system wide and not
per-interface.
2021-09-16 15:51:02 +02:00
Thomas Haller
e1667650f4
l3cfg: fix leak of ObjStateData's os_plobj
Fixes: 6b92c89486 ('l3cfg: track platform object in NML3Cfg's object state')
2021-09-16 09:30:58 +02:00
Thomas Haller
3e80b4fa63
contrib: reformat by default from "nm-code-format.sh" script
The majority of times when I call this script, I want it to do the reformatting,
not the check-only mode. This is also because we use git, so I start with a
clean working directory and run the reformatting code. In the best case, there
is nothing to reformat, and all is good. I seldom want to only check.

Change the default of the script.
2021-09-16 09:01:50 +02:00
Thomas Haller
82a6f2c465
contrib: explicitly pass "-n" to "nm-code-format.sh" in gitlab-ci check-tree job
"nm-code-format.sh" is going to change the default behavior from "-n" to
"-i", that is, from check-only to reformat. Explicitly pass "-n" where
we want it.
2021-09-16 08:47:38 +02:00
Thomas Haller
1a56dcd4da
contrib: explicitly pass "-n" to "nm-code-format.sh" in "code-style-git-post-commit-hook"
"nm-code-format.sh" is going to change the default behavior from "-n" to
"-i", that is, from check-only to reformat. Explicitly pass "-n" where
we want it.
2021-09-16 08:47:38 +02:00
Thomas Haller
17e4da8bf3
device: suppress warning for external device if it is down (!IFF_UP)
External devices are not to be touched by NetworkManager. If it is down,
that is not something to warn about.
2021-09-16 08:40:04 +02:00
Thomas Haller
571ce653fd
device: set up device also while "assuming"
"assuming" means to gracefully take over after restart. The result
should be a working configuration with a device fully managed by
NetworkManager.

If we are assuming, and the interface is down we still want to set it
up.
2021-09-16 08:38:25 +02:00
Thomas Haller
c2ab21a1b9
ifupdown: downgrade warning about missing /etc/network/interfaces file
I don't think this warrants a warning. It's important to keep the number
of warnings and errors in the log low, and only print such messages if
there is really something that requires attention by the user. If you
run without /etc/network/interfaces, then this is pretty much expected
and the warning isn't going to tell you anything useful.
2021-09-16 08:35:05 +02:00
Thomas Haller
6b92c89486
l3cfg: track platform object in NML3Cfg's object state
NML3Cfg tracks the state of each object (that is addresses and routes).
Previously, it had a boolean flag "os_in_platform", that should be
true if (and only if) we have a corresponding NMPObject in the platform
cache.

But NMPObjects are immutable and ref-counted. That means, we can just as
well track the reference to the NMPObject from the cache. The advantage
is that we have an index (dictionary) to find the object state, and by
tracking the platform object, we have it easily accessible.
2021-09-15 23:23:19 +02:00
Thomas Haller
2ab0eba106
l3cfg/ipv6ll: add NM_L3_IPV6LL_STATE_DEFUNCT enum
This is not used by NML3IPv6LL, but is useful for the callers to have
an additional pseudo value at their disposal.
2021-09-15 22:08:42 +02:00
Thomas Haller
05c05b7a80
l3cfg: allow injecting default dns-priority for NML3ConfigData
NML3ConfigData is supposed to be immutable. It can be initialized from a
NMConnection, and its DNS priority property might be zero.

For the DNS priority, the value can be overwritten by global defaults.
We thus need to inject the default value at the right place.
2021-09-15 22:08:42 +02:00
Thomas Haller
45bcedb77e
core: move NM_DNS_PRIORITY_DEFAULT_{NORMAL,VPN} to libnm-base
We will use these values from NML3Cfg, and it seems wrong that NML3Cfg
would include "dns/nm-dns-manager.h" for this.

Enums are very "static". They have no logic, and there is less need to
separate the code well. Meaning, it doesn't hurt to define this enum
in "libnm-base/nm-base.h" which can be included by (almost) anybody.
2021-09-15 22:08:42 +02:00
Thomas Haller
ef7258eafe
contrib: improve nm-in-container.sh script (4) 2021-09-15 22:08:42 +02:00
Thomas Haller
a9f6f49c8a
gitignore: fix ignore file for nm-in-container.d 2021-09-15 22:08:41 +02:00
Thomas Haller
4c007c4c27
contrib: fix "nm-code-format.sh" to select files to format
There was always the idea that you could pass paths and filenames
to "nm-code-format.sh" to format only a subset. However, the script
also needs to honor files that should be excluded and don't need
formatting.

Previously, what was implemented via `git ls-files -- ':(exclude)...'`
command, but git-ls-files has a bug ([1]) and might not list all files.

Refactor and do the filtering ourselves.

[1] https://www.spinics.net/lists/git/msg397982.html
2021-09-15 22:08:13 +02:00
Philip Withnall
0ad77d05b9
nm-active-connection: Emit device-metered-changed if device changes
The new device might have a different metered status from the old one.

Signed-off-by: Philip Withnall <pwithnall@endlessos.org>

Fixes: 04d5804dd5 ('nm-manager: add 'metered' property')

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/982
2021-09-15 20:26:29 +02:00
Thomas Haller
756757102f
contrib: improve nm-in-container.sh script 2021-09-15 12:31:23 +02:00
Thomas Haller
dbcbb45224
contrib: improve nm-in-container.sh script 2021-09-14 22:26:12 +02:00
Thomas Haller
7fea431061
contrib: improve nm-in-container.sh script 2021-09-14 20:23:15 +02:00
Thomas Haller
3a3613b561
ovs: avoid asking nm-sudo if ovsdb socket does not exist
Starting with OVS plugin installed but OVS service stopped, would lead to

   <trace> [1631531732.8896] ovsdb: connect: opening /run/openvswitch/db.sock failed ("error connecting socket (No such file or directory)"). Retry with nm-sudo
   ...
   <trace> [1631531732.9751] ovsdb: connect: failure to get FD from nm-sudo: GDBus.Error:org.gtk.GDBus.UnmappedGError.Quark._g_2dio_2derror_2dquark.Code1: error connecting socket (No such file or directory)

If we already know that the socket file does not exist, we don't need to ask nm-sudo.
That would only make sense, if nm-sudo somehow saw a different file systemd than
NetworkManager, but that is (currently) not the case.
2021-09-13 22:45:40 +02:00
Thomas Haller
d7c0dcc7b4
contrib: improve nm-in-container.sh script 2021-09-13 22:18:51 +02:00
Thomas Haller
549424273a
contrib: add nm-in-container.sh script to build a (podman) container for testing
Only a first attempt. It needs more improvements.
2021-09-13 16:57:31 +02:00
Vojtech Bubela
195bef5bae
core: fix typo in function name nmp_object_ip_route_is_best_default_route() 2021-09-13 16:56:54 +02:00
Thomas Haller
4257cc1bee
platform/tests: fix test failure for "platform_ip_address_pretty_sort_cmp"
The memory layout of the NMPlatformIPAddress structure changed. The unit test
needs to be adjusted.

Fixes: 9ec9a92f17 ('platform: avoid bitfield at end of __NMPlatformIPAddress_COMMON macro')
2021-09-13 14:42:11 +02:00
Thomas Haller
d4a367b482
nmcli: make relatives path for nmcli connection load absolute
NetworkManager (the daemon) has no defined working directory, so
it can only handle absolute path names. This is in general and also for
the LoadConnections() D-Bus call.

That means, nmcli should make relative paths absolute.

We don't use g_canonicalize_filename() because that also cleans up
double slash and "/./". I don't think we should do that in this case, we
should only prepend $PWD to make the path absolute.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/794
2021-09-13 09:32:57 +02:00
Thomas Haller
60000c72c3
core/trivial: fix spelling in comment 2021-09-13 09:22:22 +02:00
josef radinger
3f5cb1f932
core/trivial: fix small typo Ipv vs IPv
https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/978
2021-09-13 09:22:17 +02:00
gaoxingwang
2a36f8c2f1
libnm: fix leak and return "failures" from nm_client_load_connections()
Due to this, `nmcli connection load` would also not print a warning
about failure to load obviously bogus files:

  $ nmcli connection load /bogus

Note that load is also used to unload files, so if the file name is a
possibly valid name for a non-existing file, there is no failure. For
example, we get no warning for

  $ nmcli connection load /etc/NetworkManager/system-connections/bogus

Even if currently no such file is loaded, then the operation would still
silently succeed, instead of succeeding the first time only. That is because
load should be idempotent.

[thaller@redhat.com: rewrote commit message]

Fixes: 4af6219226 ('libnm: implement nm_client_load_connections_async() by using GDBusConnection directly')

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/issues/794

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/979
2021-09-13 08:32:45 +02:00