[systemd-commits] 2 commits - src/network

Tom Gundersen tomegun at kemper.freedesktop.org
Sun Nov 17 14:17:16 PST 2013


 src/network/networkd-address.c |   18 ++-
 src/network/networkd-link.c    |  186 ++++++++++++++++++++++++++++++++++++++++-
 src/network/networkd-manager.c |   10 ++
 src/network/networkd-network.c |   16 ---
 src/network/networkd-route.c   |   12 +-
 src/network/networkd.c         |    4 
 src/network/networkd.h         |   26 +++++
 src/network/test-network.c     |    3 
 8 files changed, 240 insertions(+), 35 deletions(-)

New commits:
commit f882c247ad59776c3a7753bb963c1f8e2386cb79
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Nov 14 16:22:51 2013 +0100

    networkd: make all calls async

diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 75a9bae..9fb24c9 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 d0a3dcc..dcc5cfe 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 8378e92..5000674 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 69c55ed..6383434 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 3158616..6ac5c52 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 ed93ffb..7697830 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 c019cec..7c23793 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 586b92a..7a7840e 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);

commit 8cd11a0f0f4ca05199e1166f6a07472b296f7455
Author: Tom Gundersen <teg at jklm.no>
Date:   Sun Nov 17 21:01:20 2013 +0100

    networkd: store netmask and mac address explicitly

diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index e02b1df..75a9bae 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -78,8 +78,7 @@ int address_configure(Manager *manager, Address *address, Link *link) {
         if (address->family == AF_INET) {
                 struct in_addr broadcast;
 
-                broadcast.s_addr = address->in_addr.in.s_addr |
-                                   htonl(0xfffffffflu >> address->prefixlen);
+                broadcast.s_addr = address->in_addr.in.s_addr | address->netmask.s_addr;
 
                 r = sd_rtnl_message_append(req, IFA_BROADCAST, &broadcast);
                 if (r < 0) {
@@ -147,6 +146,8 @@ int config_parse_address(const char *unit,
                 }
 
                 n->prefixlen = (unsigned char) i;
+                n->netmask.s_addr = htonl(0xfffffffflu >> n->prefixlen);
+
                 address = strndup(rvalue, e - rvalue);
                 if (!address)
                         return log_oom();
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 085b8db..d0a3dcc 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -29,6 +29,7 @@
 int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         _cleanup_link_free_ Link *link = NULL;
         uint64_t ifindex;
+        const char *mac;
         int r;
 
         assert(device);
@@ -42,6 +43,11 @@ int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         if (ifindex <= 0)
                 return -EINVAL;
 
+        mac = udev_device_get_sysattr_value(device, "address");
+        if (!mac)
+                return -EINVAL;
+
+        memcpy(&link->mac.ether_addr_octet[0], ether_aton(mac), ETH_ALEN);
         link->ifindex = ifindex;
         link->manager = manager;
 
diff --git a/src/network/networkd.h b/src/network/networkd.h
index fd3b60a..c019cec 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -64,6 +64,8 @@ struct Address {
         unsigned char prefixlen;
         char *label;
 
+        struct in_addr netmask;
+
         union {
                 struct in_addr in;
                 struct in6_addr in6;
@@ -89,6 +91,7 @@ struct Link {
         Manager *manager;
 
         int ifindex;
+        struct ether_addr mac;
 
         unsigned flags;
 



More information about the systemd-commits mailing list