summaryrefslogtreecommitdiff
path: root/src/network/networkd-wait-online.c
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2014-04-23 17:42:55 +0200
committerTom Gundersen <teg@jklm.no>2014-04-24 00:23:07 +0200
commit3a67e927e3be7efb8edf314a31aa4f8f5cba4f53 (patch)
tree5736719c730b85db89f6be3135290c565a02d046 /src/network/networkd-wait-online.c
parentfa4f8f9bc1fbef6a658cff39ac185bbedea6caf4 (diff)
networkd-wait-online: improve interoptability and enable by default
To make sure we don't delay boot on systems where (some) network links are managed by someone else we don't block if something else has successfully brought up a link. We will still block until all links we are aware of that are managed by networkd have been configured, but if no such links exist, and someone else have configured a link sufficiently that it has a carrier, it may be that the link is ready so we should no longer block. Note that in all likelyhood the link is not ready (no addresses/routes configured), so whatever network managment daemon configured it should provide a similar wait-online service to block network-online.target until it is ready. The aim is to block as long as we know networking is not fully configured, but no longer. This will allow systemd-networkd-wait-online.service to be enabled on any system, even if we don't know whether networkd is the main/only network manager. Even in the case networking is fully configured by networkd, the default behavior may not be sufficient: if two links need to be configured, but the first is fully configured before the second one appears we will assume the network is up. To work around that, we allow specifying specific devices to wait for before considering the network up. This unit is enabled by default, just like systemd-networkd, but will only be pulled in if anyone pulls in network-online.target.
Diffstat (limited to 'src/network/networkd-wait-online.c')
-rw-r--r--src/network/networkd-wait-online.c195
1 files changed, 181 insertions, 14 deletions
diff --git a/src/network/networkd-wait-online.c b/src/network/networkd-wait-online.c
index e62859e1cf..6a7c62feae 100644
--- a/src/network/networkd-wait-online.c
+++ b/src/network/networkd-wait-online.c
@@ -19,16 +19,26 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
+#include <netinet/ether.h>
+#include <linux/if.h>
+
#include "sd-event.h"
#include "event-util.h"
+#include "sd-rtnl.h"
+#include "rtnl-util.h"
#include "sd-daemon.h"
#include "sd-network.h"
#include "network-util.h"
+#include "network-internal.h"
+#include "networkd-wait-online.h"
+#include "conf-parser.h"
+#include "strv.h"
#include "util.h"
-static bool all_configured(void) {
+static bool all_configured(Manager *m) {
_cleanup_free_ unsigned *indices = NULL;
+ char **ifname;
bool one_ready = false;
int r, n, i;
@@ -36,35 +46,168 @@ static bool all_configured(void) {
if (n <= 0)
return false;
+ STRV_FOREACH(ifname, m->expected_links) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
+ bool found = false;
+ int index;
+
+ r = sd_rtnl_message_new_link(m->rtnl, &message, RTM_GETLINK, 0);
+ if (r < 0) {
+ log_warning("colud not create GETLINK message: %s", strerror(-r));
+ return false;
+ }
+
+ r = sd_rtnl_message_append_string(message, IFLA_IFNAME, *ifname);
+ if (r < 0) {
+ log_warning("could not attach ifname to GETLINK message: %s", strerror(-r));
+ return false;
+ }
+
+ r = sd_rtnl_call(m->rtnl, message, 0, &reply);
+ if (r < 0) {
+ if (r != -ENODEV)
+ log_warning("could not get link info for %s: %s", *ifname,
+ strerror(-r));
+
+ /* link does not yet exist */
+ return false;
+ }
+
+ r = sd_rtnl_message_link_get_ifindex(reply, &index);
+ if (r < 0) {
+ log_warning("could not get ifindex: %s", strerror(-r));
+ return false;
+ }
+
+ if (index <= 0) {
+ log_warning("invalid ifindex %d for %s", index, *ifname);
+ return false;
+ }
+
+ for (i = 0; i < n; i++) {
+ if (indices[i] == (unsigned) index) {
+ found = true;
+ break;
+ }
+ }
+
+ if (!found)
+ /* link exists, but networkd is not yet aware of it */
+ return false;
+ }
+
for (i = 0; i < n; i++) {
_cleanup_free_ char *state = NULL;
r = sd_network_get_link_state(indices[i], &state);
- if (r == -EUNATCH)
+ if (r == -EUNATCH) {
+ _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
+ unsigned flags;
+ uint8_t operstate;
+
+ r = sd_rtnl_message_new_link(m->rtnl, &message, RTM_GETLINK, indices[i]);
+ if (r < 0) {
+ log_warning("could not create GETLINK message: %s", strerror(-r));
+ return false;
+ }
+
+ r = sd_rtnl_call(m->rtnl, message, 0, &reply);
+ if (r < 0) {
+ log_debug("could not get link %u: %s", indices[i], strerror(-r));
+ continue;
+ }
+
+ r = sd_rtnl_message_link_get_flags(reply, &flags);
+ if (r < 0) {
+ log_warning("could not get link flags: %s", strerror(-r));
+ return false;
+ }
+
+ r = sd_rtnl_message_read_u8(reply, IFLA_OPERSTATE, &operstate);
+ if (r < 0) {
+ log_debug("could not get link operational state: %s", strerror(-r));
+ operstate = IF_OPER_UNKNOWN;
+ }
+
+ if (!(flags & IFF_LOOPBACK) &&
+ link_has_carrier(flags, operstate)) {
+ /* this link is not managed by us,
+ but something else may have
+ made it ready, so don't block */
+ one_ready = true;
+ }
+
continue;
- if (r < 0 || !streq(state, "configured"))
+ } else if (r < 0 || !streq(state, "configured"))
return false;
+ /* we wait for at least one link to appear */
one_ready = true;
}
return one_ready;
}
-static int event_handler(sd_event_source *s, int fd, uint32_t revents,
+static int monitor_event_handler(sd_event_source *s, int fd, uint32_t revents,
void *userdata) {
- sd_event *event = userdata;
+ Manager *m = userdata;
+
+ assert(m);
+ assert(m->event);
+
+ if (all_configured(m))
+ sd_event_exit(m->event, 0);
+
+ return 1;
+}
- assert(event);
+static int newlink_event_handler(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
+ Manager *m = userdata;
- if (all_configured())
- sd_event_exit(event, 0);
+ assert(m);
+ assert(m->event);
+
+ if (all_configured(m))
+ sd_event_exit(m->event, 0);
return 1;
}
+static int parse_config_file(Manager *m) {
+ static const char fn[] = "/etc/systemd/networkd-wait-online.conf";
+ _cleanup_fclose_ FILE *f = NULL;
+ int r;
+
+ f = fopen(fn, "re");
+ if (!f) {
+ if (errno == ENOENT)
+ return 0;
+
+ log_warning("Failed to open configuration file %s: %m", fn);
+ return -errno;
+ }
+
+ r = config_parse(NULL, fn, f, "WaitOnline\0", config_item_perf_lookup,
+ (void*) wait_online_gperf_lookup, false, false, m);
+ if (r < 0)
+ log_warning("Failed to parse configuration file: %s", strerror(-r));
+
+ return r;
+}
+
+void manager_free(Manager *m) {
+ if (!m)
+ return;
+
+ sd_event_unref(m->event);
+ sd_rtnl_unref(m->rtnl);
+ strv_free(m->expected_links);
+
+ free(m);
+}
+
int main(int argc, char *argv[]) {
- _cleanup_event_unref_ sd_event *event = NULL;
+ _cleanup_manager_free_ Manager *m = NULL;
_cleanup_event_source_unref_ sd_event_source *event_source = NULL;
_cleanup_network_monitor_unref_ sd_network_monitor *monitor = NULL;
int r, fd, events;
@@ -81,13 +224,21 @@ int main(int argc, char *argv[]) {
goto out;
}
+ m = new0(Manager, 1);
+ if (!m)
+ return log_oom();
+
+ r = parse_config_file(m);
+ if (r < 0)
+ goto out;
+
r = sd_network_monitor_new(NULL, &monitor);
if (r < 0) {
log_error("Could not create monitor: %s", strerror(-r));
goto out;
}
- r = sd_event_new(&event);
+ r = sd_event_new(&m->event);
if (r < 0) {
log_error("Could not create event: %s", strerror(-r));
goto out;
@@ -105,14 +256,30 @@ int main(int argc, char *argv[]) {
goto out;
}
- r = sd_event_add_io(event, &event_source, fd, events, &event_handler,
- event);
+ r = sd_event_add_io(m->event, &event_source, fd, events, &monitor_event_handler,
+ m);
if (r < 0) {
log_error("Could not add io event source: %s", strerror(-r));
goto out;
}
- if (all_configured()) {
+ r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK);
+ if (r < 0) {
+ log_error("Could not create rtnl: %s", strerror(-r));
+ goto out;
+ }
+
+ r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
+ if (r < 0) {
+ log_error("Could not attach event to rtnl: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, newlink_event_handler, m);
+ if (r < 0)
+ return r;
+
+ if (all_configured(m)) {
r = 0;
goto out;
}
@@ -121,7 +288,7 @@ int main(int argc, char *argv[]) {
"READY=1\n"
"STATUS=Waiting for network connections...");
- r = sd_event_loop(event);
+ r = sd_event_loop(m->event);
if (r < 0) {
log_error("Event loop failed: %s", strerror(-r));
goto out;