summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/network/networkd-address.c13
-rw-r--r--src/network/networkd-link.c180
-rw-r--r--src/network/networkd-manager.c10
-rw-r--r--src/network/networkd-network.c16
-rw-r--r--src/network/networkd-route.c12
-rw-r--r--src/network/networkd.c4
-rw-r--r--src/network/networkd.h23
-rw-r--r--src/network/test-network.c3
8 files changed, 228 insertions, 33 deletions
diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 75a9bae201..9fb24c95c1 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -55,10 +55,13 @@ void address_free(Address *address) {
free(address);
}
-int address_configure(Manager *manager, Address *address, Link *link) {
+int address_configure(Address *address, Link *link,
+ sd_rtnl_message_handler_t callback) {
_cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
int r;
+ assert(link->manager);
+
r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
address->family, address->prefixlen,
IFA_F_PERMANENT, RT_SCOPE_UNIVERSE, &req);
@@ -97,13 +100,13 @@ int address_configure(Manager *manager, Address *address, Link *link) {
}
}
- r = sd_rtnl_call(manager->rtnl, req, 0, NULL);
+ r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
if (r < 0) {
- log_error("Could not configure address: %s", strerror(-r));
- return r != -EEXIST ? r : 0;
+ log_error("Could not send rtnetlink message: %s", strerror(-r));
+ return r;
}
- log_info("Configured interface address");
+ link->rtnl_messages ++;
return 0;
}
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index d0a3dccd9b..dcc5cfe65a 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -50,6 +50,7 @@ int link_new(Manager *manager, struct udev_device *device, Link **ret) {
memcpy(&link->mac.ether_addr_octet[0], ether_aton(mac), ETH_ALEN);
link->ifindex = ifindex;
link->manager = manager;
+ link->state = _LINK_STATE_INVALID;
r = hashmap_put(manager->links, &ifindex, link);
if (r < 0)
@@ -103,23 +104,194 @@ int link_add(Manager *m, struct udev_device *device) {
return 0;
}
-int link_up(Manager *manager, Link *link) {
+static int link_enter_configured(Link *link) {
+ log_info("Link configured successfully.");
+
+ link->state = LINK_STATE_CONFIGURED;
+
+ return 0;
+}
+
+static int link_enter_failed(Link *link) {
+ log_warning("Could not configure link.");
+
+ link->state = LINK_STATE_FAILED;
+
+ return 0;
+}
+
+static bool link_is_up(Link *link) {
+ return link->flags & IFF_UP;
+}
+
+static int link_enter_routes_set(Link *link) {
+ log_info("Routes set for link %d", link->ifindex);
+
+ if (link_is_up(link))
+ return link_enter_configured(link);
+
+ link->state = LINK_STATE_ROUTES_SET;
+
+ return 0;
+}
+
+static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+ Link *link = userdata;
+ int r;
+
+ assert(link->rtnl_messages > 0);
+ assert(link->state == LINK_STATE_SET_ROUTES || link->state == LINK_STATE_FAILED);
+
+ link->rtnl_messages --;
+
+ if (link->state == LINK_STATE_FAILED)
+ return 1;
+
+ r = sd_rtnl_message_get_errno(m);
+ if (r < 0 && r != -EEXIST) {
+ log_warning("Could not set route on interface %d: %s",
+ link->ifindex, strerror(-r));
+ return link_enter_failed(link);
+ }
+
+ if (link->rtnl_messages == 0)
+ return link_enter_routes_set(link);
+
+ return 1;
+}
+
+static int link_enter_set_routes(Link *link) {
+ Route *route;
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(link->rtnl_messages == 0);
+ assert(link->state == LINK_STATE_ADDRESSES_SET);
+
+ link->state = LINK_STATE_SET_ROUTES;
+
+ if (!link->network->routes)
+ return link_enter_routes_set(link);
+
+ LIST_FOREACH(routes, route, link->network->routes) {
+ r = route_configure(route, link, &route_handler);
+ if (r < 0)
+ link_enter_failed(link);
+ }
+
+ return 0;
+}
+
+static int link_enter_addresses_set(Link *link) {
+ log_info("Addresses set for link %d", link->ifindex);
+
+ link->state = LINK_STATE_ADDRESSES_SET;
+
+ return link_enter_set_routes(link);
+}
+
+static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+ Link *link = userdata;
+ int r;
+
+ assert(link->rtnl_messages > 0);
+ assert(link->state == LINK_STATE_SET_ADDRESSES || link->state == LINK_STATE_FAILED);
+
+ link->rtnl_messages --;
+
+ if (link->state == LINK_STATE_FAILED)
+ return 1;
+
+ r = sd_rtnl_message_get_errno(m);
+ if (r < 0 && r != -EEXIST) {
+ log_warning("Could not set address on interface %d: %s",
+ link->ifindex, strerror(-r));
+ link_enter_failed(link);
+ }
+
+ if (link->rtnl_messages == 0)
+ link_enter_addresses_set(link);
+
+ return 1;
+}
+
+static int link_enter_set_addresses(Link *link) {
+ Address *address;
+ int r;
+
+ assert(link);
+ assert(link->network);
+ assert(link->rtnl_messages == 0);
+
+ if (!link->network->addresses)
+ return link_enter_addresses_set(link);
+
+ link->state = LINK_STATE_SET_ADDRESSES;
+
+ LIST_FOREACH(addresses, address, link->network->addresses) {
+ r = address_configure(address, link, &address_handler);
+ if (r < 0)
+ link_enter_failed(link);
+ }
+
+ return 0;
+}
+
+static int link_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+ Link *link = userdata;
+ int r;
+
+ r = sd_rtnl_message_get_errno(m);
+ if (r < 0) {
+ log_warning("Could not bring up interface %d: %s",
+ link->ifindex, strerror(-r));
+ return link_enter_failed(link);
+ }
+
+ link->flags |= IFF_UP;
+
+ log_info("Link is UP.");
+
+ if (link->state == LINK_STATE_ROUTES_SET)
+ return link_enter_configured(link);
+
+ return 1;
+}
+
+static int link_up(Link *link) {
_cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
int r;
+ assert(link);
+ assert(link->manager);
+ assert(link->manager->rtnl);
+
r = sd_rtnl_message_link_new(RTM_NEWLINK, link->ifindex, 0, IFF_UP, &req);
if (r < 0) {
log_error("Could not allocate RTM_NEWLINK message");
return r;
}
- r = sd_rtnl_call(manager->rtnl, req, 0, NULL);
+ r = sd_rtnl_call_async(link->manager->rtnl, req, link_handler, link, 0, NULL);
if (r < 0) {
- log_error("Could not UP link: %s", strerror(-r));
+ log_error("Could not send rtnetlink message: %s", strerror(-r));
return r;
}
- log_info("Link is UP");
+ return 0;
+}
+
+int link_configure(Link *link) {
+ int r;
+
+ r = link_up(link);
+ if (r < 0)
+ return link_enter_failed(link);
+
+ r = link_enter_set_addresses(link);
+ if (r < 0)
+ return link_enter_failed(link);
return 0;
}
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 8378e92a81..5000674bd9 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -208,3 +208,13 @@ int manager_udev_listen(Manager *m) {
return 0;
}
+
+int manager_rtnl_listen(Manager *m) {
+ int r;
+
+ r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
+ if (r < 0)
+ return r;
+
+ return 0;
+}
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index 69c55edb8f..638343459c 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -157,8 +157,6 @@ int network_get(Manager *manager, struct udev_device *device, Network **ret) {
}
int network_apply(Manager *manager, Network *network, Link *link) {
- Address *address;
- Route *route;
int r;
log_info("Network '%s' being applied to link '%u'",
@@ -166,21 +164,9 @@ int network_apply(Manager *manager, Network *network, Link *link) {
link->network = network;
- LIST_FOREACH(addresses, address, network->addresses) {
- r = address_configure(manager, address, link);
- if (r < 0)
- return r;
- }
-
- r = link_up(manager, link);
+ r = link_configure(link);
if (r < 0)
return r;
- LIST_FOREACH(routes, route, network->routes) {
- r = route_configure(manager, route, link);
- if (r < 0)
- return r;
- }
-
return 0;
}
diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c
index 31586165c2..6ac5c52472 100644
--- a/src/network/networkd-route.c
+++ b/src/network/networkd-route.c
@@ -54,12 +54,14 @@ void route_free(Route *route) {
free(route);
}
-int route_configure(Manager *manager, Route *route, Link *link) {
+int route_configure(Route *route, Link *link,
+ sd_rtnl_message_handler_t callback) {
_cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
int r;
- assert(manager);
assert(link);
+ assert(link->manager);
+ assert(link->manager->rtnl);
assert(link->ifindex > 0);
assert(route->family == AF_INET || route->family == AF_INET6);
@@ -83,13 +85,13 @@ int route_configure(Manager *manager, Route *route, Link *link) {
return r;
}
- r = sd_rtnl_call(manager->rtnl, req, 0, NULL);
+ r = sd_rtnl_call_async(link->manager->rtnl, req, callback, link, 0, NULL);
if (r < 0) {
- log_error("Could not configure route: %s", strerror(-r));
+ log_error("Could not send rtnetlink message: %s", strerror(-r));
return r;
}
- log_info("Configured route");
+ link->rtnl_messages ++;
return 0;
}
diff --git a/src/network/networkd.c b/src/network/networkd.c
index ed93ffb3fc..769783000b 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -50,6 +50,10 @@ int main(int argc, char *argv[]) {
if (r < 0)
return EXIT_FAILURE;
+ r = manager_rtnl_listen(m);
+ if (r < 0)
+ return EXIT_FAILURE;
+
r = sd_event_loop(m->event);
if (r < 0)
return EXIT_FAILURE;
diff --git a/src/network/networkd.h b/src/network/networkd.h
index c019ceca76..7c23793cf9 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -87,6 +87,17 @@ struct Route {
LIST_FIELDS(Route, routes);
};
+typedef enum LinkState {
+ LINK_STATE_SET_ADDRESSES,
+ LINK_STATE_ADDRESSES_SET,
+ LINK_STATE_SET_ROUTES,
+ LINK_STATE_ROUTES_SET,
+ LINK_STATE_CONFIGURED,
+ LINK_STATE_FAILED,
+ _LINK_STATE_MAX,
+ _LINK_STATE_INVALID = -1
+} LinkState;
+
struct Link {
Manager *manager;
@@ -96,6 +107,10 @@ struct Link {
unsigned flags;
Network *network;
+
+ LinkState state;
+
+ unsigned rtnl_messages;
};
struct Manager {
@@ -120,6 +135,8 @@ void manager_free(Manager *m);
int manager_udev_enumerate_links(Manager *m);
int manager_udev_listen(Manager *m);
+int manager_rtnl_listen(Manager *m);
+
DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
#define _cleanup_manager_free_ _cleanup_(manager_freep)
@@ -141,7 +158,7 @@ const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned leng
/* Route */
int route_new(Network *network, Route **ret);
void route_free(Route *route);
-int route_configure(Manager *manager, Route *route, Link *link);
+int route_configure(Route *route, Link *link, sd_rtnl_message_handler_t callback);
DEFINE_TRIVIAL_CLEANUP_FUNC(Route*, route_free);
#define _cleanup_route_free_ _cleanup_(route_freep)
@@ -153,7 +170,7 @@ int config_parse_gateway(const char *unit, const char *filename, unsigned line,
/* Address */
int address_new(Network *network, Address **ret);
void address_free(Address *address);
-int address_configure(Manager *manager, Address *address, Link *link);
+int address_configure(Address *address, Link *link, sd_rtnl_message_handler_t callback);
DEFINE_TRIVIAL_CLEANUP_FUNC(Address*, address_free);
#define _cleanup_address_free_ _cleanup_(address_freep)
@@ -167,7 +184,7 @@ int config_parse_address(const char *unit, const char *filename, unsigned line,
int link_new(Manager *manager, struct udev_device *device, Link **ret);
void link_free(Link *link);
int link_add(Manager *manager, struct udev_device *device);
-int link_up(Manager *manager, Link *link);
+int link_configure(Link *link);
DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
#define _cleanup_link_free_ _cleanup_(link_freep)
diff --git a/src/network/test-network.c b/src/network/test-network.c
index 586b92a02f..7a7840ee16 100644
--- a/src/network/test-network.c
+++ b/src/network/test-network.c
@@ -71,8 +71,9 @@ int main(void) {
test_link(loopback);
- assert(manager_udev_enumerate_links(manager) >= 0);
assert(manager_udev_listen(manager) >= 0);
+ assert(manager_udev_enumerate_links(manager) >= 0);
+ assert(manager_rtnl_listen(manager) >= 0);
udev_device_unref(loopback);
udev_unref(udev);