Commit Graph

748 Commits

Author SHA1 Message Date
Thomas Haller
4bb76fb4c3 platform: set NLM_F_EXCL when adding links
When adding a link, set both NLM_F_CREATE and NLM_F_EXCL
flags.

`ip route add` and systemd-networkd sets NLM_F_EXCL too.
2016-04-11 11:33:08 +02:00
Thomas Haller
f8b2cadfc1 platform: ensure cache consistency for routes by workaround missing kernel event
Consider:

  unshare -n
  ip link add d0 type dummy
  ip link add d1 type dummy
  ip link set d0 up
  ip link set d1 up
  ip addr add 192.168.100.5/24 dev d0
  ip addr add 192.168.101.5/24 dev d1

  ip route add 192.168.200.0/24 via 192.168.100.1
  ip monitor &
  ip route change 192.168.200.0/24 via 192.168.101.1
  #prints 192.168.200.0/24 via 192.168.101.1 dev d1
  ip route show
  #192.168.100.0/24 dev d0  proto kernel  scope link  src 192.168.100.5
  #192.168.101.0/24 dev d1  proto kernel  scope link  src 192.168.101.5
  #192.168.200.0/24 via 192.168.101.1 dev d1

Note that `ip route change` replaced the exising route. "Replaced" in this
case means: the previous route on device "d0" got removed and a new route
on "d1" was added. However, kernel only sent one RTM_NEWROUTE event, no
RTM_DELROUTE that notifies about this change.

We need to workaround that by re-synching the routes when we receive a
RTM_NEWROUTE notification.
2016-04-11 11:32:48 +02:00
Thomas Haller
20618901dd platform: use _NM_UTILS_LOOKUP_DEFINE() 2016-04-11 11:30:56 +02:00
Thomas Haller
ff8c82e7e1 platform: track refresh-all actions that are in progress
We aim to keep the platform cache up-to-date only via the netlink
events. However, due to kernel shortcomings we often have to resync
by re-requesting the data, which especially for routes and addresses
means a full dump (as you cannot request only specific route/address
information).

Thus it makes sense to avoid expensive dumps whenever we can.

We schedule dumps via "delayed-actions" and that is already smart
so that muliple schedulings are combined. However, before requesting
a new dump, we clear the flag that indicates that a dump is scheduled.
Thus, while processing the result of of a dump, we would re-schedule
anew which can be necessary in some cases.

In certain cases, we don't require a full resync, when we are in the
middle of processing a dump, because that one dump will provide us
with the full picture. Thus, we can avoid scheduling a new dump if
  - we already scheduled a delayed action
  - we are in the middle or processing a dump.
This can now be checked via delayed_action_refresh_all_in_progress().
2016-04-11 11:30:56 +02:00
Thomas Haller
8bf635af0e platform: refactor calling event_seq_check()
This makes more sense with the next commit.
2016-04-11 11:30:56 +02:00
Thomas Haller
90550a276b platform: refactor delayed_action_wait_for_nl_response_complete() 2016-04-11 11:30:56 +02:00
Thomas Haller
c59687c6b2 platform: #ifdef out nlh_seq_last_handled if it's unused
This is not for performance. It's to separate the relavant
parts from a part that is only for logging/asserts.
2016-04-11 11:26:37 +02:00
Thomas Haller
15e357c30a platform: add macro FOR_EACH_DELAYED_ACTION() 2016-04-11 11:26:37 +02:00
Thomas Haller
198baca830 platform: expose nmp_utils_ip_config_source_to/from_rtprot()
Will be used also from the tests.
2016-04-11 11:26:37 +02:00
Thomas Haller
45a9a6b30b platform: add nm_linux_platform_new() constructor 2016-04-11 11:26:37 +02:00
Thomas Haller
44768f0311 platform: change @plen field of NMPlatformIPxAddress to type guint8
On netlink layer, this field is uint8_t/uchar.

A larger (signed) plen makes no sense. Adjust the signatures
to have only guint8.
2016-04-11 11:26:36 +02:00
Thomas Haller
14ee5dd2f8 platform: change @plen field of NMPlatformIPxRoute to type guint8
On netlink layer, this field is uint8_t/uchar.

A larger (signed) plen makes no sense. Adjust the signatures
to have only guint8.
2016-04-11 11:26:36 +02:00
Thomas Haller
207c0f5f81 platform: add nm_platform_link_can_assume() 2016-04-04 15:33:16 +02:00
Lubomir Rintel
05e454a98d platform: don't assume that netlink message is as big as at compile time
The link_stats structure grew between 4.5 and 4.6 and this would cause
the messages to me ignored when compiling with 4.6 headers and running
on 4.5.

https://bugzilla.gnome.org/show_bug.cgi?id=764332
2016-03-30 13:25:49 +02:00
Stjepan Gros
9995699116 platform: add nm_platform_link_set_netns() function
[thaller@redhat.com: cherry-picked original patch and modified
  slightly]
2016-03-15 12:56:58 +01:00
Thomas Haller
7df7619e22 platform: properly change netns in do_change_link() 2016-03-15 12:56:57 +01:00
Thomas Haller
b36d721525 all: drop str_if_set() in favor of the "?:" operator
str_if_set() was added to replace the non-standard gcc extension "?:".
However, "?:" is supported by clang as well and we already use it at
several places.

Also, str_if_set() did not follow our naming scheme and renaming to
nm_str_if_set() would be ugly. So just drop it.
2016-03-08 17:57:20 +01:00
Thomas Haller
d5cc42ff6f core/trivial: rename NM_ASSERT_VALID_PATH_COMPONENT() helper
All defines from header files must have a NM prefix.
2016-03-08 17:45:50 +01:00
Thomas Haller
c7b3862503 platform: add network namespace support to platform
Add a new NMPNetns class. This allows creation, deletion and
switching of network namespaces. The API only offers push/pop
operations to switch the namespace. This way the API enforces
the user to always restore the previous namespace.

A NMPlatform instance not only uses the netlink socket, but also
sysfs, udev, ethtool, mii. Still, a NMPlatform instance lives
entirely inside one namespace and is not spanning multiple namespaces.
To properly support network namespaces, the platform instance must
switch the namespace as necessary, transparent to the caller.
Udev is only supported in the main namespace.

For now, network namespaces are not actually used and are disabled
via the NM_PLATFORM_NETNS_SUPPORT argument.

https://bugzilla.gnome.org/show_bug.cgi?id=762408
2016-03-07 12:28:13 +01:00
Thomas Haller
1a1c5fb717 platform: don't create udev client if udev isn't used
Inside container, where we don't use udev we don't receive
any events from udev client. Thus the client only returns
devices when iterating it initially, but no events for newly
added devices that appear later.

Thus, inside containers we don't want to create a udev client
at all.
2016-03-07 11:58:01 +01:00
Thomas Haller
6165df788d core: move simple utils function from "nm-platform-utils.h"
Most functions defined in "nm-platform-utils.h" perform a lookup
of link properties, for example via ethtool or sysfs. Those functions
depend on the system configuration, such as the current network namespace.

Move the simple helper functions away to "nm-core-internal.h", so that
all remaining functions from "nm-platform-utils.h" are really related to
somthing that interacts with the system/kernel.
2016-03-07 11:49:52 +01:00
Thomas Haller
0e90f1ba83 platform: add and use nm_utils_ifname_cpy() helper
Coverity complains rightly about "strncpy (dst, ifname, IFNAMSIZ)"
because it might leave @dst non-NULL-terminated, in case @ifname
is too long (which already would be a bug in the first place).

Replace the strcpy() uses by a new helper nm_utils_ifname_cpy()
that asserts against valid arguments.
2016-03-07 11:36:57 +01:00
Thomas Haller
298ab55225 platform: cleanup logging for udev-add event 2016-03-04 13:19:34 +01:00
Thomas Haller
d17aedf91f all: explicitly ignore return values
Coverity warns that usually we check for the return value.
2016-03-04 09:06:21 +01:00
Thomas Haller
684e80b539 platform: add flags argument to nm_platform_ip4_address_add()
The argument is still always unset. We will need it later to set
IFA_F_NOPREFIXROUTE.
2016-03-01 15:09:16 +01:00
Thomas Haller
cc78e06393 platform: track ifa_flags for cached IPv4 addresses
It's useful to track the flags for IPv4 addresses too.

- we might want to sort IPv4 addresses according to whether they
  are permanent or dynamic.

- later we want to set IFA_F_NOPREFIXROUTE also for IPv4 addresses.
  While the ability to "set" a flag doesn't necessarily require that we
  also keep the flags present in NMPlatformIP4Address, it is more consistent.
2016-03-01 15:08:31 +01:00
Thomas Haller
adb56d137e core: split "nm-core-utils.h" out of "NetworkManagerUtils.h"
"NetworkManagerUtils.h" contains a bunch of helper tools for core
daemon ("src/").

Unfortunately, it has dependencies to other parts of core,
such as "nm-device.h" and "nm-platform.h". Split out a part
of tools that are independent so that they can be used without
dragging in other dependencies.

"nm-core-utils.h" should only use libnm-core, "nm-logging.h"
and shared.

"NetworkManagerUtils.h" should provide all "nm-core-utils.h" and
possibly other utilities that have larger dependencies.
2016-03-01 12:42:42 +01:00
Thomas Haller
e4a7502a2c logging: make logging independent of platform
"nm-logging.h" is a basic core module that should have few other
dependencies. Instead of letting nm-logging.c directly call a function
from "nm-linux-platform.c", let platform register a handler as needed.

This way, you can build a core library containing nm-logging but no
nm-platform.
2016-03-01 12:42:42 +01:00
Thomas Haller
7f43e0a7b3 platform/trivial: rename NMPlatformIP6Address's "flags" field to "n_ifa_flags"
There are far too many "flags". Rename the "flags" to "n_ifa_flags"
which reminds to "ifa_flags" in 'struct ifaddrmsg', but with a
distinctive "n_" prefix.
2016-02-29 16:27:08 +01:00
Thomas Haller
e18cdc10d3 platform: adjust type of IPv6 address flags to guint
Address flags inside 'struct ifaddrmsg' are uint8_t and they are
extended to uint32_t via IFA_FLAGS.

Change it to be guint32.
2016-02-29 16:27:08 +01:00
Thomas Haller
866925e65c platform/trivial: rename NMPlatformLink's "flags" field to "n_ifi_flags"
There are far too many "flags". Rename the "flags" to "n_ifi_flags"
which reminds to "ifi_flags" in 'struct ifinfomsg', but with a
distinctive "n_" prefix.
2016-02-29 15:56:01 +01:00
Thomas Haller
41b46f1393 platform: adjust type of link flags to guint
The type of ifi_flags is unsigned, not uint32_t.
2016-02-29 15:53:19 +01:00
Thomas Haller
b913e1d641 platform: optimize sysctl_set() to use stack allocated buffer
The value written to sysctl is usually a short string. It makes sense
to optimize for this case and avoid allocating a temporary string
on the heap.

An alternative would be to use writev(), which effectively does the same
and also creates a temporary buffer (preferably stack allocated).

https://mail.gnome.org/archives/networkmanager-list/2016-February/msg00070.html
2016-02-24 11:24:13 +01:00
Thomas Haller
8bace23beb all: cleanup includes and let "nm-default.h" include "config.h"
- All internal source files (except "examples", which are not internal)
  should include "config.h" first. As also all internal source
  files should include "nm-default.h", let "config.h" be included
  by "nm-default.h" and include "nm-default.h" as first in every
  source file.
  We already wanted to include "nm-default.h" before other headers
  because it might contains some fixes (like "nm-glib.h" compatibility)
  that is required first.

- After including "nm-default.h", we optinally allow for including the
  corresponding header file for the source file at hand. The idea
  is to ensure that each header file is self contained.

- Don't include "config.h" or "nm-default.h" in any header file
  (except "nm-sd-adapt.h"). Public headers anyway must not include
  these headers, and internal headers are never included after
  "nm-default.h", as of the first previous point.

- Include all internal headers with quotes instead of angle brackets.
  In practice it doesn't matter, because in our public headers we must
  include other headers with angle brackets. As we use our public
  headers also to compile our interal source files, effectively the
  result must be the same. Still do it for consistency.

- Except for <config.h> itself. Include it with angle brackets as suggested by
  https://www.gnu.org/software/autoconf/manual/autoconf.html#Configuration-Headers
2016-02-19 17:53:25 +01:00
Thomas Haller
328c733a6a platform: expose nmp_utils_ip4_address_is_link_local() function 2016-02-18 20:21:27 +01:00
Thomas Haller
3d759b1f11 platform: during @abort_parsing goto stop in event_handler_recvmsgs()
Now, that we no longer overwrite @err, we can jump to stop: instead
of out:.
2016-02-16 14:13:25 +01:00
Thomas Haller
329ac02d38 platform: don't set @err during stop: in event_handler_recvmsgs()
If we break the loop normally, @err must be already set to zero.
The only other way this can happen is when the credentials are
invalid. Move setting @err to there.
2016-02-16 14:13:25 +01:00
Thomas Haller
3f00899bde platform: continue reading in event_handler_recvmsgs() when not handling events
If @handle_events is FALSE, we want to drain the socket. In that case
even when encountering an error error we don't want to abort, but instead
continue reading the next message.
2016-02-16 14:13:25 +01:00
Thomas Haller
dc97a3b39b platform: fix error handling in event_handler_recvmsgs()
@abort_parsing is set TRUE at two places, which also explicitly
set @err to something. We don't want to reset @err and got to the
next @hdr. Instead error out first.
2016-02-16 14:13:25 +01:00
Thomas Haller
43381f9b85 platform: downgrade logging message to TRACE level in event_handler_recvmsgs()
Doesn't seem important and might be triggered by other processes.
2016-02-16 14:13:25 +01:00
Thomas Haller
9254cbe875 platform: don't return number of messages from event_handler_recvmsgs()
The value is not used by the callers. Also, with @handle_events set
to false, it is not clear what the value really means because we skip
over errors.
2016-02-16 14:13:25 +01:00
Thomas Haller
04805f659f platform: simplify event_handler_recvmsgs() by using cleanup attribute 2016-02-16 14:13:25 +01:00
Thomas Haller
9c0cfbbae6 platform: fix detection of link-type for netlink event
Often a netlink event doesn't contain enough information to determine
the link type. Then we consult sysctl or ethtool. However, if we already
have the same object cached, we want to reused the (once detected) link-type.

There was a bug in lookup of the cached object.
2016-02-15 17:19:22 +01:00
Thomas Haller
e663b88c59 all/trivial: rename STRLEN() macro to NM_STRLEN()
We should not have defines/macros in header files without a nm/NM
prefix. STRLEN() was one of the few offenders.

https://mail.gnome.org/archives/networkmanager-list/2016-February/msg00048.html
2016-02-14 11:34:42 +01:00
Beniamino Galvani
97be12b662 platform: always try to refetch new ethernet links
Due to a kernel bug [1], we sometimes receive spurious NEWLINK
messages after a wifi interface has disappeared. Since the link is not
present anymore we can't determine its type and thus it will show up
as a Ethernet one, with no address specified.  Request the link again
to check if it really exists.

[1] https://bugzilla.redhat.com/show_bug.cgi?id=1302037

https://bugzilla.gnome.org/show_bug.cgi?id=761151
2016-02-12 14:41:56 +01:00
Thomas Haller
04ec21ccc2 logging: add new logging macros _LOGx_err() to log errno
Not yet used, only implemented for nm-linux-platform.c as a
show case.

Use it like:

  _LOGW_err (errno, "failed to popen()");
2016-02-05 11:10:38 +01:00
Thomas Haller
bd17fc6d24 platform: assume support for IPv6 temporary addrsses if detection fails
We detect support for IPv6 temporary addresses (IFA_F_MANAGETEMPADDR) or /64 v6 prefixes
(IFA_F_NOPREFIXROUTE) based on the presence of extended address flags. For the most part
this just works, but it fails down if upon initialization no addresses are present.

In such a case we would have assumed no support. Change that to default to available
support as the feature is already 2 years in upstream kernel.
2016-02-04 18:04:43 +01:00
Thomas Haller
f00c472ee8 platform: increase netlink socket receive buffer size
When the receive buffer is too small, we easily can hit ENOBUFS during recvmsg()
and need to resync the platform cache. But even worse, we possibly also loose
ACKs for pending netlink requests so that requests seem to fail (although they
might have succeeded).

Avoid that harder by increasing the buffer size to 8MB. This is also
done by networkd:

  be660c371b
2016-01-25 15:48:09 +01:00
Thomas Haller
45df49ebdd platform: improve detecting ENOBUFS for recvmsg() on netlink socket
libnl3 maps different error codes to NLE_NOMEM. Only resync platform cache
when we really experienced a ENOBUFS from recvmsg().
2016-01-25 15:08:32 +01:00
Thomas Haller
4ba999e41e platform: refactor check for error in event_handler_recvmsgs() to switch statement 2016-01-25 15:07:10 +01:00