chiark / gitweb /
networkd: make all calls async
authorTom Gundersen <teg@jklm.no>
Thu, 14 Nov 2013 15:22:51 +0000 (16:22 +0100)
committerTom Gundersen <teg@jklm.no>
Sun, 17 Nov 2013 22:12:27 +0000 (23:12 +0100)
src/network/networkd-address.c
src/network/networkd-link.c
src/network/networkd-manager.c
src/network/networkd-network.c
src/network/networkd-route.c
src/network/networkd.c
src/network/networkd.h
src/network/test-network.c

index 75a9bae..9fb24c9 100644 (file)
@@ -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;
 }
index d0a3dcc..dcc5cfe 100644 (file)
@@ -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;
 }
index 8378e92..5000674 100644 (file)
@@ -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;
+}
index 69c55ed..6383434 100644 (file)
@@ -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;
 }
index 3158616..6ac5c52 100644 (file)
@@ -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;
 }
index ed93ffb..7697830 100644 (file)
@@ -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;
index c019cec..7c23793 100644 (file)
@@ -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)
index 586b92a..7a7840e 100644 (file)
@@ -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);