[systemd-commits] 3 commits - src/libsystemd-network src/network src/systemd
Tom Gundersen
tomegun at kemper.freedesktop.org
Wed May 7 07:54:15 PDT 2014
src/libsystemd-network/network-internal.c | 14 ------
src/libsystemd-network/network-internal.h | 2
src/network/networkd-link.c | 64 +++++++++++++++++++----------
src/network/networkd-wait-online.c | 65 +++++-------------------------
src/network/sd-network.c | 62 +++++++++++++++++++++++++++-
src/systemd/sd-network.h | 22 ++++++++--
6 files changed, 132 insertions(+), 97 deletions(-)
New commits:
commit bc6345767e9f6864f1e1c0e87fe5da352c00e67f
Author: Tom Gundersen <teg at jklm.no>
Date: Wed May 7 16:35:52 2014 +0200
networkd-wait-online: rely purely on sd-network events and drop rtnl hooks
diff --git a/src/network/networkd-wait-online.c b/src/network/networkd-wait-online.c
index ba02586..a4729f4 100644
--- a/src/network/networkd-wait-online.c
+++ b/src/network/networkd-wait-online.c
@@ -116,6 +116,7 @@ static bool all_configured(Manager *m) {
if (n <= 0)
return false;
+ /* wait for networkd to be aware of all the links given on the commandline */
STRV_FOREACH(ifname, arg_interfaces) {
_cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
bool found = false;
@@ -167,11 +168,14 @@ static bool all_configured(Manager *m) {
}
}
+ /* wait for all links networkd manages to be in admin state 'configured'
+ and at least one link to gain a carrier */
for (i = 0; i < n; i++) {
- _cleanup_free_ char *state = NULL;
- _cleanup_rtnl_message_unref_ sd_rtnl_message *message = NULL, *reply = NULL;
- unsigned flags;
- uint8_t operstate;
+ _cleanup_free_ char *state = NULL, *oper_state = NULL;
+
+ if (sd_network_link_is_loopback(indices[i]))
+ /* ignore loopback devices */
+ continue;
r = sd_network_get_link_state(indices[i], &state);
if (r != -EUNATCH && (r < 0 || !streq(state, "configured"))) {
@@ -179,36 +183,11 @@ static bool all_configured(Manager *m) {
return false;
}
- 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)) {
+ r = sd_network_get_link_operational_state(indices[i], &oper_state);
+ if (r >= 0 && streq(oper_state, "carrier"))
/* we wait for at least one link to be ready,
regardless of who manages it */
one_ready = true;
- }
}
return one_ready;
@@ -227,18 +206,6 @@ static int monitor_event_handler(sd_event_source *s, int fd, uint32_t revents,
return 1;
}
-static int newlink_event_handler(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
- Manager *m = userdata;
-
- assert(m);
- assert(m->event);
-
- if (all_configured(m))
- sd_event_exit(m->event, 0);
-
- return 1;
-}
-
void manager_free(Manager *m) {
if (!m)
return;
@@ -302,22 +269,12 @@ int main(int argc, char *argv[]) {
goto out;
}
- r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK);
+ r = sd_rtnl_open(&m->rtnl, 0);
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;
commit 99b4cc3e75e757c07d573e2f4c29f47393e24fa1
Author: Tom Gundersen <teg at jklm.no>
Date: Wed May 7 16:42:47 2014 +0200
networkd: link - always maintain link operstate regardless of admin state
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 2f99b53..0f2004d 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -1139,9 +1139,6 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
assert(link);
- if (link->state == LINK_STATE_FAILED)
- return 0;
-
r = sd_rtnl_message_link_get_flags(m, &flags);
if (r < 0) {
log_warning_link(link, "Could not get link flags");
@@ -1232,6 +1229,10 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
link_save(link);
+ if (link->state == LINK_STATE_FAILED ||
+ link->state == LINK_STATE_UNMANAGED)
+ return 0;
+
if (carrier_gained) {
log_info_link(link, "gained carrier");
@@ -1621,9 +1622,6 @@ int link_update(Link *link, sd_rtnl_message *m) {
assert(link->ifname);
assert(m);
- if (link->state == LINK_STATE_FAILED || link->state == LINK_STATE_UNMANAGED)
- return 0;
-
r = sd_rtnl_message_read_string(m, IFLA_IFNAME, &ifname);
if (r >= 0 && !streq(ifname, link->ifname)) {
log_info_link(link, "renamed to %s", ifname);
commit deb2e5230b4dcbc0e2e02cc47a0b2d0d7179a044
Author: Tom Gundersen <teg at jklm.no>
Date: Wed May 7 16:35:05 2014 +0200
sd-network: expose both admin and operational state directly
Also add a call to check if a link is loopback, as this should commonly be ignored.
diff --git a/src/libsystemd-network/network-internal.c b/src/libsystemd-network/network-internal.c
index e7ba628..52e614c 100644
--- a/src/libsystemd-network/network-internal.c
+++ b/src/libsystemd-network/network-internal.c
@@ -326,17 +326,3 @@ int net_parse_inaddr(const char *address, unsigned char *family, void *dst) {
return 0;
}
-
-bool link_has_carrier(unsigned flags, uint8_t operstate) {
- /* see Documentation/networking/operstates.txt in the kernel sources */
-
- if (operstate == IF_OPER_UP)
- return true;
-
- if (operstate == IF_OPER_UNKNOWN)
- /* operstate may not be implemented, so fall back to flags */
- if ((flags & IFF_LOWER_UP) && !(flags & IFF_DORMANT))
- return true;
-
- return false;
-}
diff --git a/src/libsystemd-network/network-internal.h b/src/libsystemd-network/network-internal.h
index ef3cb8b..836472a 100644
--- a/src/libsystemd-network/network-internal.h
+++ b/src/libsystemd-network/network-internal.h
@@ -65,5 +65,3 @@ int config_parse_ifalias(const char *unit, const char *filename, unsigned line,
int net_parse_inaddr(const char *address, unsigned char *family, void *dst);
int net_get_unique_predictable_data(struct udev_device *device, uint8_t result[8]);
-
-bool link_has_carrier(unsigned flags, uint8_t operstate);
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 058fbc2..2f99b53 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -1117,6 +1117,20 @@ static int link_acquire_conf(Link *link) {
return 0;
}
+static bool link_has_carrier(unsigned flags, uint8_t operstate) {
+ /* see Documentation/networking/operstates.txt in the kernel sources */
+
+ if (operstate == IF_OPER_UP)
+ return true;
+
+ if (operstate == IF_OPER_UNKNOWN)
+ /* operstate may not be implemented, so fall back to flags */
+ if ((flags & IFF_LOWER_UP) && !(flags & IFF_DORMANT))
+ return true;
+
+ return false;
+}
+
static int link_update_flags(Link *link, sd_rtnl_message *m) {
unsigned flags, flags_added, flags_removed, generic_flags;
uint8_t operstate;
@@ -1216,6 +1230,8 @@ static int link_update_flags(Link *link, sd_rtnl_message *m) {
link->flags = flags;
link->operstate = operstate;
+ link_save(link);
+
if (carrier_gained) {
log_info_link(link, "gained carrier");
@@ -1514,8 +1530,7 @@ static int link_configure(Link *link) {
}
}
- if (link_has_carrier(link->flags, link->operstate))
- {
+ if (link_has_carrier(link->flags, link->operstate)) {
r = link_acquire_conf(link);
if (r < 0)
return r;
@@ -1669,16 +1684,26 @@ int link_update(Link *link, sd_rtnl_message *m) {
}
int link_save(Link *link) {
- _cleanup_free_ char *temp_path = NULL;
+ _cleanup_free_ char *temp_path = NULL, *lease_file = NULL;
_cleanup_fclose_ FILE *f = NULL;
- const char *state;
+ const char *admin_state, *oper_state = "unknown";
int r;
assert(link);
assert(link->state_file);
- state = link_state_to_string(link->state);
- assert(state);
+ admin_state = link_state_to_string(link->state);
+ assert(admin_state);
+
+ if (link->operstate & IF_OPER_DORMANT)
+ oper_state = "dormant";
+ else if (link_has_carrier(link->flags, link->operstate))
+ oper_state = "carrier";
+
+ r = asprintf(&lease_file, "/run/systemd/network/leases/%"PRIu64,
+ link->ifindex);
+ if (r < 0)
+ return -ENOMEM;
r = fopen_temporary(link->state_file, &f, &temp_path);
if (r < 0)
@@ -1688,22 +1713,19 @@ int link_save(Link *link) {
fprintf(f,
"# This is private data. Do not parse.\n"
- "STATE=%s\n", state);
+ "ADMIN_STATE=%s\n"
+ "OPER_STATE=%s\n"
+ "FLAGS=%u\n",
+ admin_state, oper_state, link->flags);
if (link->dhcp_lease) {
- _cleanup_free_ char *lease_file = NULL;
-
- r = asprintf(&lease_file, "/run/systemd/network/leases/%"PRIu64,
- link->ifindex);
- if (r < 0)
- return -ENOMEM;
-
r = dhcp_lease_save(link->dhcp_lease, lease_file);
if (r < 0)
goto finish;
fprintf(f, "DHCP_LEASE=%s\n", lease_file);
- }
+ } else
+ unlink(lease_file);
fflush(f);
@@ -1721,7 +1743,7 @@ finish:
}
static const char* const link_state_table[_LINK_STATE_MAX] = {
- [LINK_STATE_INITIALIZING] = "configuring",
+ [LINK_STATE_INITIALIZING] = "initializing",
[LINK_STATE_ENSLAVING] = "configuring",
[LINK_STATE_SETTING_ADDRESSES] = "configuring",
[LINK_STATE_SETTING_ROUTES] = "configuring",
diff --git a/src/network/sd-network.c b/src/network/sd-network.c
index 1dfb282..c867812 100644
--- a/src/network/sd-network.c
+++ b/src/network/sd-network.c
@@ -25,6 +25,7 @@
#include <errno.h>
#include <sys/inotify.h>
#include <sys/poll.h>
+#include <net/if.h>
#include "util.h"
#include "macro.h"
@@ -33,6 +34,38 @@
#include "sd-network.h"
#include "dhcp-lease-internal.h"
+static int link_get_flags(unsigned index, unsigned *flags) {
+ _cleanup_free_ char *s = NULL, *p = NULL;
+ int r;
+
+ assert(index);
+ assert(flags);
+
+ if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
+ return -ENOMEM;
+
+ r = parse_env_file(p, NEWLINE, "FLAGS", &s, NULL);
+ if (r == -ENOENT)
+ return -ENODATA;
+ else if (r < 0)
+ return r;
+ else if (!s)
+ return -EIO;
+
+ return safe_atou(s, flags);
+}
+
+_public_ int sd_network_link_is_loopback(unsigned index) {
+ unsigned flags;
+ int r;
+
+ r = link_get_flags(index, &flags);
+ if (r < 0)
+ return 0;
+
+ return flags & IFF_LOOPBACK;
+}
+
_public_ int sd_network_get_link_state(unsigned index, char **state) {
_cleanup_free_ char *s = NULL, *p = NULL;
int r;
@@ -43,8 +76,7 @@ _public_ int sd_network_get_link_state(unsigned index, char **state) {
if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
return -ENOMEM;
- r = parse_env_file(p, NEWLINE, "STATE", &s, NULL);
-
+ r = parse_env_file(p, NEWLINE, "ADMIN_STATE", &s, NULL);
if (r == -ENOENT)
return -ENODATA;
else if (r < 0)
@@ -54,6 +86,32 @@ _public_ int sd_network_get_link_state(unsigned index, char **state) {
if (streq(s, "unmanaged"))
return -EUNATCH;
+ else if (streq(s, "initializing"))
+ return -EBUSY;
+
+ *state = s;
+ s = NULL;
+
+ return 0;
+}
+
+_public_ int sd_network_get_link_operational_state(unsigned index, char **state) {
+ _cleanup_free_ char *s = NULL, *p = NULL;
+ int r;
+
+ assert_return(index, -EINVAL);
+ assert_return(state, -EINVAL);
+
+ if (asprintf(&p, "/run/systemd/network/links/%u", index) < 0)
+ return -ENOMEM;
+
+ r = parse_env_file(p, NEWLINE, "OPER_STATE", &s, NULL);
+ if (r == -ENOENT)
+ return -ENODATA;
+ else if (r < 0)
+ return r;
+ else if (!s)
+ return -EIO;
*state = s;
s = NULL;
diff --git a/src/systemd/sd-network.h b/src/systemd/sd-network.h
index 9562b78..40bd75a 100644
--- a/src/systemd/sd-network.h
+++ b/src/systemd/sd-network.h
@@ -51,8 +51,24 @@
_SD_BEGIN_DECLARATIONS;
-/* Get state from ifindex. Possible states: unknown, unmanaged, failed, configuring, configured */
-int sd_network_get_link_state(unsigned index, char**state);
+/* Get state from ifindex.
+ * Possible states: failed, configuring, configured
+ * Possible return codes:
+ * -ENODATA: networkd is not aware of the link
+ * -EUNATCH: networkd is not managing this link
+ * -EBUSY: udev is still processing the link, networkd does not yet know if it will manage it
+ */
+int sd_network_get_link_state(unsigned index, char **state);
+
+/* Get operatinal state from ifindex.
+ * Possible states: unknown, dormant, carrier
+ * Possible return codes:
+ * -ENODATA: networkd is not aware of the link
+ */
+int sd_network_get_link_operational_state(unsigned index, char **state);
+
+/* Returns true if link exists and is loopback, and false otherwise */
+int sd_network_link_is_loopback(unsigned index);
/* Get DHCPv4 lease from ifindex. */
int sd_network_get_dhcp_lease(unsigned index, sd_dhcp_lease **ret);
@@ -65,7 +81,7 @@ int sd_network_get_ifindices(unsigned **indices);
typedef struct sd_network_monitor sd_network_monitor;
/* Create a new monitor. Category must be NULL, "links" or "leases". */
-int sd_network_monitor_new(const char *category, sd_network_monitor** ret);
+int sd_network_monitor_new(const char *category, sd_network_monitor **ret);
/* Destroys the passed monitor. Returns NULL. */
sd_network_monitor* sd_network_monitor_unref(sd_network_monitor *m);
More information about the systemd-commits
mailing list