[systemd-commits] 4 commits - Makefile.am src/libsystemd-rtnl src/network src/systemd

Tom Gundersen tomegun at kemper.freedesktop.org
Mon Nov 25 16:31:19 PST 2013


 Makefile.am                        |    2 
 src/libsystemd-rtnl/rtnl-message.c |  141 ++++++++++++++--
 src/libsystemd-rtnl/test-rtnl.c    |   26 ++
 src/network/networkd-address.c     |    7 
 src/network/networkd-bridge.c      |  321 +++++++++++++++++++++++++++++++++++++
 src/network/networkd-gperf.gperf   |    3 
 src/network/networkd-link.c        |  107 +++++++++---
 src/network/networkd-manager.c     |   30 +++
 src/network/networkd-network.c     |   46 ++++-
 src/network/networkd-route.c       |    2 
 src/network/networkd.c             |    4 
 src/network/networkd.h             |   61 ++++++-
 src/network/test-network.c         |    8 
 src/systemd/sd-rtnl.h              |    6 
 14 files changed, 704 insertions(+), 60 deletions(-)

New commits:
commit 02b59d57e0c08231645120077f651151f5bb2bab
Author: Tom Gundersen <teg at jklm.no>
Date:   Sun Nov 24 23:37:56 2013 +0100

    networkd: add bridge support
    
    A bridge is specified in a .netdev file with a section [Bridge]
    and at least the entry Name=.
    
    A link may be joined to a bridge if the .network applied to it has
    a Bridge= entry giving the name of the bridge in its [Network] section.
    
    We eagerly create all bridges on startup, and links are added to
    bridges as soon as they both appear.

diff --git a/Makefile.am b/Makefile.am
index 728b860..47c269d 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -3833,6 +3833,7 @@ systemd_networkd_SOURCES = \
 	src/network/networkd.h \
 	src/network/networkd.c \
 	src/network/networkd-link.c \
+	src/network/networkd-bridge.c \
 	src/network/networkd-network.c \
 	src/network/networkd-address.c \
 	src/network/networkd-route.c \
@@ -3859,6 +3860,7 @@ test_network_SOURCES = \
 	src/network/test-network.c \
 	src/network/networkd.h \
 	src/network/networkd-link.c \
+	src/network/networkd-bridge.c \
 	src/network/networkd-network.c \
 	src/network/networkd-address.c \
 	src/network/networkd-route.c \
diff --git a/src/network/networkd-bridge.c b/src/network/networkd-bridge.c
new file mode 100644
index 0000000..371c093
--- /dev/null
+++ b/src/network/networkd-bridge.c
@@ -0,0 +1,321 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2013 Tom Gundersen <teg at jklm.no>
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU Lesser General Public License as published by
+  the Free Software Foundation; either version 2.1 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  Lesser General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include "networkd.h"
+#include "net-util.h"
+#include "path-util.h"
+#include "conf-files.h"
+#include "conf-parser.h"
+#include "list.h"
+
+void bridge_free(Bridge *bridge) {
+        bridge_join_callback *callback;
+
+        if (!bridge)
+                return;
+
+        while ((callback = bridge->callbacks)) {
+                LIST_REMOVE(callbacks, bridge->callbacks, callback);
+                free(callback);
+        }
+
+        if (bridge->name)
+                hashmap_remove(bridge->manager->bridges, bridge->name);
+
+        free(bridge->filename);
+
+        free(bridge->description);
+        free(bridge->name);
+
+        free(bridge);
+}
+
+int bridge_get(Manager *manager, const char *name, Bridge **ret) {
+        Bridge *bridge;
+
+        assert(manager);
+        assert(name);
+        assert(ret);
+
+        if (manager_should_reload(manager))
+                manager_load_config(manager);
+
+        bridge = hashmap_get(manager->bridges, name);
+        if (!bridge) {
+                *ret = NULL;
+                return -ENOENT;
+        }
+
+        *ret = bridge;
+
+        return 0;
+}
+
+static int bridge_enter_failed(Bridge *bridge) {
+        bridge->state = BRIDGE_STATE_FAILED;
+
+        return 0;
+}
+
+static int bridge_join_ready(Bridge *bridge, Link* link, sd_rtnl_message_handler_t callback) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        int r;
+
+        assert(bridge);
+        assert(bridge->state == BRIDGE_STATE_READY);
+        assert(link);
+        assert(callback);
+
+        r = sd_rtnl_message_link_new(RTM_SETLINK, link->ifindex, 0, 0, &req);
+        if (r < 0) {
+                log_error("Could not allocate RTM_SETLINK message: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append(req, IFLA_MASTER, &bridge->link->ifindex);
+        if (r < 0) {
+                log_error("Could not append IFLA_MASTER attribute: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_call_async(bridge->manager->rtnl, req, callback, link, 0, NULL);
+        if (r < 0) {
+                log_error("Could not send rtnetlink message: %s", strerror(-r));
+                return r;
+        }
+
+        return 0;
+}
+
+static int bridge_enter_ready(Bridge *bridge) {
+        bridge_join_callback *callback;
+
+        bridge->state = BRIDGE_STATE_READY;
+
+        log_info("Bridge '%s' ready", bridge->name);
+
+        LIST_FOREACH(callbacks, callback, bridge->callbacks) {
+                /* join the links that were attempted to be joined befor the
+                 * link was ready */
+                bridge_join_ready(bridge, callback->link, callback->callback);
+        }
+
+        return 0;
+}
+
+static int bridge_create_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        Bridge *bridge = userdata;
+        int r;
+
+        assert(bridge->state == BRIDGE_STATE_CREATING);
+
+        r = sd_rtnl_message_get_errno(m);
+        if (r < 0) {
+                log_warning("Bridge '%s' failed: %s", bridge->name, strerror(-r));
+                return bridge_enter_failed(bridge);
+        }
+
+        if (bridge->link)
+                return bridge_enter_ready(bridge);
+
+        bridge->state = BRIDGE_STATE_CREATED;
+
+        return 0;
+}
+
+static int bridge_create(Bridge *bridge) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+        int r;
+
+        assert(bridge);
+        assert(bridge->state == _BRIDGE_STATE_INVALID);
+        assert(bridge->name);
+        assert(bridge->manager);
+        assert(bridge->manager->rtnl);
+
+        r = sd_rtnl_message_link_new(RTM_NEWLINK, 0, 0, 0, &req);
+        if (r < 0) {
+                log_error("Could not allocate RTM_NEWLINK message: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append(req, IFLA_IFNAME, bridge->name);
+        if (r < 0) {
+                log_error("Could not append IFLA_IFNAME attribute: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_open_container(req, IFLA_LINKINFO);
+        if (r < 0) {
+                log_error("Colud not open IFLA_LINKINFO container: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_append(req, IFLA_INFO_KIND, "bridge");
+        if (r < 0) {
+                log_error("Could not append IFLA_INFO_KIND attribute: %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_message_close_container(req);
+        if (r < 0) {
+                log_error("Could not close IFLA_LINKINFO container %s",
+                          strerror(-r));
+                return r;
+        }
+
+        r = sd_rtnl_call_async(bridge->manager->rtnl, req, &bridge_create_handler, bridge, 0, NULL);
+        if (r < 0) {
+                log_error("Could not send rtnetlink message: %s", strerror(-r));
+                return r;
+        }
+
+        log_info("Creating bridge '%s'", bridge->name);
+
+        bridge->state = BRIDGE_STATE_CREATING;
+
+        return 0;
+}
+
+int bridge_join(Bridge *bridge, Link *link, sd_rtnl_message_handler_t callback) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
+
+        if (bridge->state == BRIDGE_STATE_READY) {
+                bridge_join_ready(bridge, link, callback);
+        } else {
+                /* the bridge is not yet read, save this request for when it is*/
+                bridge_join_callback *cb;
+
+                cb = new0(bridge_join_callback, 1);
+                if (!cb)
+                        return log_oom();
+
+                cb->callback = callback;
+                cb->link = link;
+
+                LIST_PREPEND(callbacks, bridge->callbacks, cb);
+        }
+
+        return 0;
+}
+
+int bridge_set_link(Manager *m, Link *link) {
+        Bridge *bridge;
+
+        bridge = hashmap_get(m->bridges, link->ifname);
+        if (!bridge)
+                return -ENOENT;
+
+        if (bridge->link && bridge->link != link)
+                return -EEXIST;
+
+        bridge->link = link;
+
+        if (bridge->state == BRIDGE_STATE_CREATED)
+                bridge_enter_ready(bridge);
+
+        return 0;
+}
+
+static int bridge_load_one(Manager *manager, const char *filename) {
+        _cleanup_bridge_free_ Bridge *bridge = NULL;
+        _cleanup_fclose_ FILE *file = NULL;
+        int r;
+
+        file = fopen(filename, "re");
+        if (!file) {
+                if (errno == ENOENT)
+                        return 0;
+                else
+                        return errno;
+        }
+
+        bridge = new0(Bridge, 1);
+        if (!bridge)
+                return log_oom();
+
+        bridge->manager = manager;
+        bridge->state = _BRIDGE_STATE_INVALID;
+
+        r = config_parse(NULL, filename, file, "Bridge\0", config_item_perf_lookup,
+                        (void*) network_gperf_lookup, false, false, bridge);
+        if (r < 0) {
+                log_warning("Could not parse config file %s: %s", filename, strerror(-r));
+                return r;
+        } else
+                log_debug("Parsed configuration file %s", filename);
+
+        if (!bridge->name) {
+                log_warning("Bridge without Name configured in %s. Ignoring", filename);
+                return 0;
+        }
+
+        bridge->filename = strdup(filename);
+        if (!bridge->filename)
+                return log_oom();
+
+        r = hashmap_put(bridge->manager->bridges, bridge->name, bridge);
+        if (r < 0)
+                return r;
+
+        LIST_HEAD_INIT(bridge->callbacks);
+
+        r = bridge_create(bridge);
+        if (r < 0)
+                return r;
+
+        bridge = NULL;
+
+        return 0;
+}
+
+int bridge_load(Manager *manager) {
+        Bridge *bridge;
+        char **files, **f;
+        int r;
+
+        assert(manager);
+
+        while ((bridge = hashmap_first(manager->bridges)))
+                bridge_free(bridge);
+
+        r = conf_files_list_strv(&files, ".netdev", NULL, (const char **)manager->network_dirs);
+        if (r < 0) {
+                log_error("Failed to enumerate netdev files: %s", strerror(-r));
+                return r;
+        }
+
+        STRV_FOREACH_BACKWARDS(f, files) {
+                r = bridge_load_one(manager, *f);
+                if (r < 0)
+                        return r;
+        }
+
+        strv_free(files);
+
+        return 0;
+}
diff --git a/src/network/networkd-gperf.gperf b/src/network/networkd-gperf.gperf
index 7fbe466..385f1bb 100644
--- a/src/network/networkd-gperf.gperf
+++ b/src/network/networkd-gperf.gperf
@@ -21,7 +21,10 @@ Match.Driver,            config_parse_string,           0,       offsetof(Networ
 Match.Type,              config_parse_string,           0,       offsetof(Network, match_type)
 Match.Name,              config_parse_ifname,           0,       offsetof(Network, match_name)
 Network.Description,     config_parse_string,           0,       offsetof(Network, description)
+Network.Bridge,          config_parse_bridge,           0,       offsetof(Network, bridge)
 Network.Address,         config_parse_address,          0,       0
 Network.Gateway,         config_parse_gateway,          0,       0
 Address.Address,         config_parse_address,          0,       0
 Address.Label,           config_parse_label,            0,       0
+Bridge.Description,      config_parse_string,           0,       offsetof(Bridge, description)
+Bridge.Name,             config_parse_ifname,           0,       offsetof(Bridge, name)
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index 255b413..09de862 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -85,6 +85,7 @@ int link_add(Manager *m, struct udev_device *device) {
         Network *network;
         int r;
         uint64_t ifindex;
+        const char *devtype;
 
         assert(m);
         assert(device);
@@ -100,6 +101,13 @@ int link_add(Manager *m, struct udev_device *device) {
                 return r;
         }
 
+        devtype = udev_device_get_devtype(device);
+        if (streq_ptr(devtype, "bridge")) {
+                r = bridge_set_link(m, link);
+                if (r < 0)
+                        return r == -ENOENT ? 0 : r;
+        }
+
         r = network_get(m, device, &network);
         if (r < 0)
                 return r == -ENOENT ? 0 : r;
@@ -287,14 +295,59 @@ static int link_up(Link *link) {
         return 0;
 }
 
-int link_configure(Link *link) {
+static int link_enter_bridge_joined(Link *link) {
         int r;
 
         r = link_up(link);
         if (r < 0)
                 return link_enter_failed(link);
 
-        r = link_enter_set_addresses(link);
+        link->state = LINK_STATE_BRIDGE_JOINED;
+
+        return link_enter_set_addresses(link);
+}
+
+static int bridge_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
+        Link *link = userdata;
+        int r;
+
+        assert(link->state == LINK_STATE_JOIN_BRIDGE || link->state == LINK_STATE_FAILED);
+
+        if (link->state == LINK_STATE_FAILED)
+                return 1;
+
+        r = sd_rtnl_message_get_errno(m);
+        if (r < 0)
+                log_warning("Could not join interface '%s' to bridge: %s",
+                            link->ifname, strerror(-r));
+
+        link_enter_bridge_joined(link);
+
+        return 1;
+}
+
+static int link_enter_join_bridge(Link *link) {
+        int r;
+
+        assert(link);
+        assert(link->network);
+
+        if (!link->network->bridge)
+                return link_enter_bridge_joined(link);
+
+        link->state = LINK_STATE_JOIN_BRIDGE;
+
+        r = bridge_join(link->network->bridge, link, &bridge_handler);
+        if (r < 0)
+                return link_enter_failed(link);
+
+        return 0;
+}
+
+int link_configure(Link *link) {
+        int r;
+
+        r = link_enter_join_bridge(link);
         if (r < 0)
                 return link_enter_failed(link);
 
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index c28f3d9..1dc7e88 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -51,6 +51,10 @@ int manager_new(Manager **ret) {
         if (!m->links)
                 return -ENOMEM;
 
+        m->bridges = hashmap_new(string_hash_func, string_compare_func);
+        if (!m->bridges)
+                return -ENOMEM;
+
         LIST_HEAD_INIT(m->networks);
 
         m->network_dirs = strv_new("/etc/systemd/network/",
@@ -74,6 +78,7 @@ int manager_new(Manager **ret) {
 
 void manager_free(Manager *m) {
         Network *network;
+        Bridge *bridge;
         Link *link;
 
         udev_monitor_unref(m->udev_monitor);
@@ -88,12 +93,37 @@ void manager_free(Manager *m) {
                 link_free(link);
         hashmap_free(m->links);
 
+        while ((bridge = hashmap_first(m->bridges)))
+                bridge_free(bridge);
+        hashmap_free(m->bridges);
+
         strv_free(m->network_dirs);
         sd_rtnl_unref(m->rtnl);
 
         free(m);
 }
 
+int manager_load_config(Manager *m) {
+        int r;
+
+        /* update timestamp */
+        paths_check_timestamp(m->network_dirs, &m->network_dirs_ts_usec, true);
+
+        r = bridge_load(m);
+        if (r < 0)
+                return r;
+
+        r = network_load(m);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+bool manager_should_reload(Manager *m) {
+        return paths_check_timestamp(m->network_dirs, &m->network_dirs_ts_usec, false);
+}
+
 static int manager_process_link(Manager *m, struct udev_device *device) {
         Link *link;
         int r;
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index eb3ff23..ae8d4d1 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -83,9 +83,6 @@ int network_load(Manager *manager) {
         while ((network = manager->networks))
                 network_free(network);
 
-        /* update timestamp */
-        paths_check_timestamp(manager->network_dirs, &manager->network_dirs_ts_usec, true);
-
         r = conf_files_list_strv(&files, ".network", NULL, (const char **)manager->network_dirs);
         if (r < 0) {
                 log_error("failed to enumerate network files: %s", strerror(-r));
@@ -103,10 +100,6 @@ int network_load(Manager *manager) {
         return 0;
 }
 
-bool network_should_reload(Manager *manager) {
-        return paths_check_timestamp(manager->network_dirs, &manager->network_dirs_ts_usec, false);
-}
-
 void network_free(Network *network) {
         Route *route;
         Address *address;
@@ -145,8 +138,8 @@ int network_get(Manager *manager, struct udev_device *device, Network **ret) {
         assert(device);
         assert(ret);
 
-        if (network_should_reload(manager))
-                network_load(manager);
+        if (manager_should_reload(manager))
+                manager_load_config(manager);
 
         LIST_FOREACH(networks, network, manager->networks) {
                 if (net_match_config(network->match_mac, network->match_path,
@@ -173,8 +166,8 @@ int network_get(Manager *manager, struct udev_device *device, Network **ret) {
 int network_apply(Manager *manager, Network *network, Link *link) {
         int r;
 
-        log_info("Network '%s' being applied to link '%ju'",
-                        network->description, link->ifindex);
+        log_info("Network '%s' being applied to link '%s'",
+                        network->description, link->ifname);
 
         link->network = network;
 
@@ -184,3 +177,34 @@ int network_apply(Manager *manager, Network *network, Link *link) {
 
         return 0;
 }
+
+int config_parse_bridge(const char *unit,
+                const char *filename,
+                unsigned line,
+                const char *section,
+                unsigned section_line,
+                const char *lvalue,
+                int ltype,
+                const char *rvalue,
+                void *data,
+                void *userdata) {
+        Network *network = userdata;
+        Bridge *bridge;
+        int r;
+
+        assert(filename);
+        assert(lvalue);
+        assert(rvalue);
+        assert(data);
+
+        r = bridge_get(network->manager, rvalue, &bridge);
+        if (r < 0) {
+                log_syntax(unit, LOG_ERR, filename, line, EINVAL,
+                           "Bridge is invalid, ignoring assignment: %s", rvalue);
+                return 0;
+        }
+
+        network->bridge = bridge;
+
+        return 0;
+}
diff --git a/src/network/networkd.c b/src/network/networkd.c
index 9144f97..1d43361 100644
--- a/src/network/networkd.c
+++ b/src/network/networkd.c
@@ -44,6 +44,10 @@ int main(int argc, char *argv[]) {
         if (r < 0)
                 goto out;
 
+        r = manager_load_config(m);
+        if (r < 0)
+                return EXIT_FAILURE;
+
         r = manager_udev_listen(m);
         if (r < 0)
                 goto out;
diff --git a/src/network/networkd.h b/src/network/networkd.h
index 913f17d..1ab8a55 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -32,12 +32,45 @@
 #include "hashmap.h"
 #include "list.h"
 
+typedef struct Bridge Bridge;
 typedef struct Network Network;
 typedef struct Link Link;
 typedef struct Address Address;
 typedef struct Route Route;
 typedef struct Manager Manager;
 
+typedef struct bridge_join_callback bridge_join_callback;
+
+struct bridge_join_callback {
+        sd_rtnl_message_handler_t callback;
+        Link *link;
+
+        LIST_FIELDS(bridge_join_callback, callbacks);
+};
+
+typedef enum BridgeState {
+        BRIDGE_STATE_FAILED,
+        BRIDGE_STATE_CREATING,
+        BRIDGE_STATE_CREATED,
+        BRIDGE_STATE_READY,
+        _BRIDGE_STATE_MAX,
+        _BRIDGE_STATE_INVALID = -1,
+} BridgeState;
+
+struct Bridge {
+        Manager *manager;
+
+        char *filename;
+
+        char *description;
+        char *name;
+
+        Link *link;
+        BridgeState state;
+
+        LIST_HEAD(bridge_join_callback, callbacks);
+};
+
 struct Network {
         Manager *manager;
 
@@ -50,6 +83,7 @@ struct Network {
         char *match_name;
 
         char *description;
+        Bridge *bridge;
 
         LIST_HEAD(Address, addresses);
         LIST_HEAD(Route, routes);
@@ -99,6 +133,8 @@ struct Route {
 };
 
 typedef enum LinkState {
+        LINK_STATE_JOIN_BRIDGE,
+        LINK_STATE_BRIDGE_JOINED,
         LINK_STATE_SET_ADDRESSES,
         LINK_STATE_ADDRESSES_SET,
         LINK_STATE_SET_ROUTES,
@@ -133,6 +169,7 @@ struct Manager {
         sd_event_source *udev_event_source;
 
         Hashmap *links;
+        Hashmap *bridges;
         LIST_HEAD(Network, networks);
 
         char **network_dirs;
@@ -144,6 +181,9 @@ struct Manager {
 int manager_new(Manager **ret);
 void manager_free(Manager *m);
 
+int manager_load_config(Manager *m);
+bool manager_should_reload(Manager *m);
+
 int manager_udev_enumerate_links(Manager *m);
 int manager_udev_listen(Manager *m);
 
@@ -152,10 +192,22 @@ int manager_rtnl_listen(Manager *m);
 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_free);
 #define _cleanup_manager_free_ _cleanup_(manager_freep)
 
+/* Bridge */
+
+int bridge_load(Manager *manager);
+
+void bridge_free(Bridge *bridge);
+
+DEFINE_TRIVIAL_CLEANUP_FUNC(Bridge*, bridge_free);
+#define _cleanup_bridge_free_ _cleanup_(bridge_freep)
+
+int bridge_get(Manager *manager, const char *name, Bridge **ret);
+int bridge_set_link(Manager *m, Link *link);
+int bridge_join(Bridge *bridge, Link *link, sd_rtnl_message_handler_t cb);
+
 /* Network */
 
 int network_load(Manager *manager);
-bool network_should_reload(Manager *manager);
 
 void network_free(Network *network);
 
@@ -165,6 +217,12 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(Network*, network_free);
 int network_get(Manager *manager, struct udev_device *device, Network **ret);
 int network_apply(Manager *manager, Network *network, Link *link);
 
+int config_parse_bridge(const char *unit, const char *filename, unsigned line,
+                        const char *section, unsigned section_line, const char *lvalue,
+                        int ltype, const char *rvalue, void *data, void *userdata);
+
+/* gperf */
+
 const struct ConfigPerfItem* network_gperf_lookup(const char *key, unsigned length);
 
 /* Route */
diff --git a/src/network/test-network.c b/src/network/test-network.c
index 822a334..5e27997 100644
--- a/src/network/test-network.c
+++ b/src/network/test-network.c
@@ -31,15 +31,15 @@ static void test_link(struct udev_device *loopback) {
         assert(link);
 }
 
-static void test_network_load(Manager *manager) {
+static void test_load_config(Manager *manager) {
 /*  TODO: should_reload, is false if the config dirs do not exist, so
  *        so we can't do this test here, move it to a test for paths_check_timestamps
  *        directly
  *
  *        assert(network_should_reload(manager) == true);
 */
-        assert(network_load(manager) >= 0);
-        assert(network_should_reload(manager) == false);
+        assert(manager_load_config(manager) >= 0);
+        assert(manager_should_reload(manager) == false);
 }
 
 static void test_network_get(Manager *manager, struct udev_device *loopback) {
@@ -58,7 +58,7 @@ int main(void) {
 
         assert(manager_new(&manager) >= 0);
 
-        test_network_load(manager);
+        test_load_config(manager);
 
         udev = udev_new();
         assert(udev);

commit c166a070553511e402de5ad216d3fb62b49bdacb
Author: Tom Gundersen <teg at jklm.no>
Date:   Sun Nov 24 23:36:58 2013 +0100

    networkd: minor fixes
    
    In particular, store the ifname, though we should only use it carefully, as
    it is not guaranteed to be stable. Using it for logging is fine though.

diff --git a/src/network/networkd-address.c b/src/network/networkd-address.c
index 9cfc095..0555d31 100644
--- a/src/network/networkd-address.c
+++ b/src/network/networkd-address.c
@@ -79,7 +79,12 @@ int address_configure(Address *address, Link *link,
         _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *req = NULL;
         int r;
 
+        assert(address);
+        assert(address->family == AF_INET || address->family == AF_INET6);
+        assert(link);
+        assert(link->ifindex > 0);
         assert(link->manager);
+        assert(link->manager->rtnl);
 
         r = sd_rtnl_message_addr_new(RTM_NEWADDR, link->ifindex,
                         address->family, address->prefixlen,
@@ -125,8 +130,6 @@ int address_configure(Address *address, Link *link,
                 return r;
         }
 
-        link->rtnl_messages ++;
-
         return 0;
 }
 
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index ab5da36..255b413 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -30,6 +30,7 @@ int link_new(Manager *manager, struct udev_device *device, Link **ret) {
         _cleanup_link_free_ Link *link = NULL;
         const char *mac;
         struct ether_addr *mac_addr;
+        const char *ifname;
         int r;
 
         assert(device);
@@ -53,6 +54,9 @@ int link_new(Manager *manager, struct udev_device *device, Link **ret) {
                         memcpy(&link->mac, mac_addr, sizeof(struct ether_addr));
         }
 
+        ifname = udev_device_get_sysname(device);
+        link->ifname = strdup(ifname);
+
         r = hashmap_put(manager->links, &link->ifindex, link);
         if (r < 0)
                 return r;
@@ -71,6 +75,8 @@ void link_free(Link *link) {
 
         hashmap_remove(link->manager->links, &link->ifindex);
 
+        free(link->ifname);
+
         free(link);
 }
 
@@ -90,7 +96,7 @@ int link_add(Manager *m, struct udev_device *device) {
 
         r = link_new(m, device, &link);
         if (r < 0) {
-                log_error("could not create link: %s", strerror(-r));
+                log_error("Could not create link: %s", strerror(-r));
                 return r;
         }
 
@@ -106,7 +112,7 @@ int link_add(Manager *m, struct udev_device *device) {
 }
 
 static int link_enter_configured(Link *link) {
-        log_info("Link configured successfully.");
+        log_info("Link '%s' configured", link->ifname);
 
         link->state = LINK_STATE_CONFIGURED;
 
@@ -114,7 +120,7 @@ static int link_enter_configured(Link *link) {
 }
 
 static int link_enter_failed(Link *link) {
-        log_warning("Could not configure link.");
+        log_warning("Could not configure link '%s'", link->ifname);
 
         link->state = LINK_STATE_FAILED;
 
@@ -126,7 +132,7 @@ static bool link_is_up(Link *link) {
 }
 
 static int link_enter_routes_set(Link *link) {
-        log_info("Routes set for link %ju", link->ifindex);
+        log_info("Routes set for link '%s'", link->ifname);
 
         if (link_is_up(link))
                 return link_enter_configured(link);
@@ -149,11 +155,9 @@ static int route_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 return 1;
 
         r = sd_rtnl_message_get_errno(m);
-        if (r < 0 && r != -EEXIST) {
-                log_warning("Could not set route on interface %ju: %s",
-                            link->ifindex, strerror(-r));
-                return link_enter_failed(link);
-        }
+        if (r < 0 && r != -EEXIST)
+                log_warning("Could not set route on interface '%s': %s",
+                            link->ifname, strerror(-r));
 
         if (link->rtnl_messages == 0)
                 return link_enter_routes_set(link);
@@ -178,14 +182,16 @@ static int link_enter_set_routes(Link *link) {
         LIST_FOREACH(routes, route, link->network->routes) {
                 r = route_configure(route, link, &route_handler);
                 if (r < 0)
-                        link_enter_failed(link);
+                        return link_enter_failed(link);
+
+                link->rtnl_messages ++;
         }
 
         return 0;
 }
 
 static int link_enter_addresses_set(Link *link) {
-        log_info("Addresses set for link %ju", link->ifindex);
+        log_info("Addresses set for link '%s'", link->ifname);
 
         link->state = LINK_STATE_ADDRESSES_SET;
 
@@ -205,11 +211,9 @@ static int address_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
                 return 1;
 
         r = sd_rtnl_message_get_errno(m);
-        if (r < 0 && r != -EEXIST) {
-                log_warning("Could not set address on interface %ju: %s",
-                            link->ifindex, strerror(-r));
-                link_enter_failed(link);
-        }
+        if (r < 0 && r != -EEXIST)
+                log_warning("Could not set address on interface '%s': %s",
+                            link->ifname, strerror(-r));
 
         if (link->rtnl_messages == 0)
                 link_enter_addresses_set(link);
@@ -233,7 +237,9 @@ static int link_enter_set_addresses(Link *link) {
         LIST_FOREACH(addresses, address, link->network->addresses) {
                 r = address_configure(address, link, &address_handler);
                 if (r < 0)
-                        link_enter_failed(link);
+                        return link_enter_failed(link);
+
+                link->rtnl_messages ++;
         }
 
         return 0;
@@ -244,15 +250,13 @@ static int link_handler(sd_rtnl *rtnl, sd_rtnl_message *m, void *userdata) {
         int r;
 
         r = sd_rtnl_message_get_errno(m);
-        if (r < 0) {
-                log_warning("Could not bring up interface %ju: %s",
-                            link->ifindex, strerror(-r));
-                return link_enter_failed(link);
-        }
+        if (r < 0)
+                log_warning("Could not bring up interface '%s': %s",
+                            link->ifname, strerror(-r));
 
         link->flags |= IFF_UP;
 
-        log_info("Link is UP.");
+        log_info("Link '%s' is up", link->ifname);
 
         if (link->state == LINK_STATE_ROUTES_SET)
                 return link_enter_configured(link);
diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c
index b88c622..1166a99 100644
--- a/src/network/networkd-route.c
+++ b/src/network/networkd-route.c
@@ -118,8 +118,6 @@ int route_configure(Route *route, Link *link,
                 return r;
         }
 
-        link->rtnl_messages ++;
-
         return 0;
 }
 
diff --git a/src/network/networkd.h b/src/network/networkd.h
index 04a56ea..913f17d 100644
--- a/src/network/networkd.h
+++ b/src/network/networkd.h
@@ -113,6 +113,7 @@ struct Link {
         Manager *manager;
 
         uint64_t ifindex;
+        char *ifname;
         struct ether_addr mac;
 
         unsigned flags;

commit 46fabae6b33386277f5ee394cf0d5382376586da
Author: Tom Gundersen <teg at jklm.no>
Date:   Sun Nov 24 22:08:30 2013 +0100

    rtnl: add support for IFLA_MASTER

diff --git a/src/libsystemd-rtnl/rtnl-message.c b/src/libsystemd-rtnl/rtnl-message.c
index 800bf23..b2b7feb 100644
--- a/src/libsystemd-rtnl/rtnl-message.c
+++ b/src/libsystemd-rtnl/rtnl-message.c
@@ -320,6 +320,7 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
                                 case IFLA_IFALIAS:
                                 case IFLA_QDISC:
                                         return add_rtattr(m, type, data, strlen(data) + 1);
+                                case IFLA_MASTER:
                                 case IFLA_MTU:
                                 case IFLA_LINK:
                                         return add_rtattr(m, type, data, sizeof(uint32_t));

commit 33125ac50bafd93dd98934f5f6ff23a59da8d793
Author: Tom Gundersen <teg at jklm.no>
Date:   Thu Nov 21 20:44:29 2013 +0100

    rtnl: add preliminary support for containers
    
    For now, we only support one container type IFLA_LINKINFO, and we
    still lack support for parsing the containers again.

diff --git a/src/libsystemd-rtnl/rtnl-message.c b/src/libsystemd-rtnl/rtnl-message.c
index 26da7f2..800bf23 100644
--- a/src/libsystemd-rtnl/rtnl-message.c
+++ b/src/libsystemd-rtnl/rtnl-message.c
@@ -36,6 +36,8 @@ struct sd_rtnl_message {
 
         struct nlmsghdr *hdr;
 
+        struct rtattr *current_container;
+
         struct rtattr *next_rta;
         size_t remaining_size;
 
@@ -131,7 +133,7 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type,
 
         assert_return(nlmsg_type == RTM_NEWLINK || nlmsg_type == RTM_DELLINK ||
                       nlmsg_type == RTM_SETLINK || nlmsg_type == RTM_GETLINK, -EINVAL);
-        assert_return(index > 0, -EINVAL);
+        assert_return(nlmsg_type == RTM_NEWLINK || index > 0, -EINVAL);
         assert_return(ret, -EINVAL);
 
         r = message_new(ret, NLMSG_SPACE(sizeof(struct ifinfomsg)));
@@ -140,6 +142,8 @@ int sd_rtnl_message_link_new(uint16_t nlmsg_type, int index, unsigned int type,
 
         (*ret)->hdr->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
         (*ret)->hdr->nlmsg_type = nlmsg_type;
+        if (nlmsg_type == RTM_NEWLINK)
+                (*ret)->hdr->nlmsg_flags |= NLM_F_CREATE;
 
         ifi = NLMSG_DATA((*ret)->hdr);
 
@@ -203,6 +207,21 @@ int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type) {
         return 0;
 }
 
+int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex) {
+        struct ifinfomsg *ifi;
+
+        assert_return(m, -EINVAL);
+        assert_return(ifindex, -EINVAL);
+        assert_return(m->hdr->nlmsg_type == RTM_NEWLINK || m->hdr->nlmsg_type == RTM_DELLINK ||
+                      m->hdr->nlmsg_type == RTM_GETLINK || m->hdr->nlmsg_type == RTM_SETLINK, -EINVAL);
+
+        ifi = NLMSG_DATA(m->hdr);
+
+        *ifindex = ifi->ifi_index;
+
+        return 0;
+}
+
 /* If successful the updated message will be correctly aligned, if unsuccessful the old message is
    untouched */
 static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data, size_t data_length) {
@@ -211,11 +230,10 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         struct rtattr *rta;
         char *padding;
 
-        assert_return(m, -EINVAL);
-        assert_return(m->hdr, -EINVAL);
-        assert_return(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len, -EINVAL);
-        assert_return(data, -EINVAL);
-        assert_return(data_length > 0, -EINVAL);
+        assert(m);
+        assert(m->hdr);
+        assert(NLMSG_ALIGN(m->hdr->nlmsg_len) == m->hdr->nlmsg_len);
+        assert(!data || data_length > 0);
 
         /* get the size of the new rta attribute (with padding at the end) */
         rta_length = RTA_LENGTH(data_length);
@@ -234,15 +252,28 @@ static int add_rtattr(sd_rtnl_message *m, unsigned short type, const void *data,
         /* update message size */
         m->hdr->nlmsg_len = message_length;
 
+        /* we are inside a container, extend it */
+        if (m->current_container)
+                m->current_container->rta_len = (unsigned char *) m->hdr +
+                                                m->hdr->nlmsg_len -
+                                                (unsigned char *) m->current_container;
+
         /* fill in the attribute */
         rta->rta_type = type;
         rta->rta_len = rta_length;
-        /* we don't deal with the case where the user lies about the type and gives us
-         * too little data (so don't do that)
-         */
-        padding = mempcpy(RTA_DATA(rta), data, data_length);
-        /* make sure also the padding at the end of the message is initialized */
-        memset(padding, '\0', (unsigned char *) m->hdr + m->hdr->nlmsg_len - (unsigned char *) padding);
+        if (!data) {
+                /* this is a container, set pointer */
+                m->current_container = rta;
+        } else {
+                /* we don't deal with the case where the user lies about the type
+                 * and gives us too little data (so don't do that)
+                */
+                padding = mempcpy(RTA_DATA(rta), data, data_length);
+                /* make sure also the padding at the end of the message is initialized */
+                memset(padding, '\0', (unsigned char *) m->hdr +
+                                      m->hdr->nlmsg_len -
+                                      (unsigned char *) padding);
+        }
 
         return 0;
 }
@@ -257,6 +288,28 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
 
         sd_rtnl_message_get_type(m, &rtm_type);
 
+        if (m->current_container) {
+                switch (rtm_type) {
+                        case RTM_NEWLINK:
+                        case RTM_SETLINK:
+                        case RTM_GETLINK:
+                        case RTM_DELLINK:
+                                switch (m->current_container->rta_type) {
+                                        case IFLA_LINKINFO:
+                                                switch (type) {
+                                                        case IFLA_INFO_KIND:
+                                                                return add_rtattr(m, type, data, strlen(data) + 1);
+                                                        default:
+                                                                return -ENOTSUP;
+                                                }
+                                        default:
+                                                return -ENOTSUP;
+                                }
+                        default:
+                                return -ENOTSUP;
+                }
+        }
+
         switch (rtm_type) {
                 case RTM_NEWLINK:
                 case RTM_SETLINK:
@@ -329,13 +382,67 @@ int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *
         }
 }
 
-static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
+int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type) {
+        uint16_t rtm_type;
+
         assert_return(m, -EINVAL);
-        assert_return(data, -EINVAL);
+        assert_return(!m->current_container, -EINVAL);
+
+        sd_rtnl_message_get_type(m, &rtm_type);
+
+        switch (rtm_type) {
+                case RTM_NEWLINK:
+                case RTM_SETLINK:
+                case RTM_GETLINK:
+                case RTM_DELLINK:
+                        if (type == IFLA_LINKINFO)
+                                return add_rtattr(m, type, NULL, 0);
+                        else
+                                return -ENOTSUP;
+                default:
+                        return -ENOTSUP;
+        }
+
+        return 0;
+}
+
+int sd_rtnl_message_close_container(sd_rtnl_message *m) {
+        assert_return(m, -EINVAL);
+        assert_return(m->current_container, -EINVAL);
+
+        m->current_container = NULL;
+
+        return 0;
+}
+
+static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
+        uint16_t rtm_type;
+        int r;
+
+        assert(m);
+        assert(m->next_rta);
+        assert(type);
+        assert(data);
 
         if (!RTA_OK(m->next_rta, m->remaining_size))
                 return 0;
 
+        /* make sure we don't try to read a container
+         * TODO: add support for entering containers for reading */
+        r = sd_rtnl_message_get_type(m, &rtm_type);
+        if (r < 0)
+                return r;
+
+        switch (rtm_type) {
+                case RTM_NEWLINK:
+                case RTM_GETLINK:
+                case RTM_SETLINK:
+                case RTM_DELLINK:
+                        if (m->next_rta->rta_type == IFLA_LINKINFO) {
+                                return -EINVAL;
+                        }
+        }
+
         *data = RTA_DATA(m->next_rta);
         *type = m->next_rta->rta_type;
 
@@ -346,11 +453,14 @@ static int message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
 
 int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data) {
         uint16_t rtm_type;
+        int r;
 
         assert_return(m, -EINVAL);
         assert_return(data, -EINVAL);
 
-        sd_rtnl_message_get_type(m, &rtm_type);
+        r = sd_rtnl_message_get_type(m, &rtm_type);
+        if (r < 0)
+                return r;
 
         switch (rtm_type) {
                 case RTM_NEWLINK:
diff --git a/src/libsystemd-rtnl/test-rtnl.c b/src/libsystemd-rtnl/test-rtnl.c
index 75a210d..4b62c02 100644
--- a/src/libsystemd-rtnl/test-rtnl.c
+++ b/src/libsystemd-rtnl/test-rtnl.c
@@ -206,6 +206,30 @@ static void test_pipe(int ifindex) {
         }
 }
 
+static void test_container(void) {
+        _cleanup_sd_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+        uint16_t type;
+        void *data;
+
+        assert(sd_rtnl_message_link_new(RTM_NEWLINK, 0, 0, 0, &m) >= 0);
+
+        assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) >= 0);
+        assert(sd_rtnl_message_open_container(m, IFLA_LINKINFO) == -EINVAL);
+        assert(sd_rtnl_message_append(m, IFLA_INFO_KIND, "kind") >= 0);
+        assert(sd_rtnl_message_close_container(m) >= 0);
+        assert(sd_rtnl_message_close_container(m) == -EINVAL);
+
+        assert(sd_rtnl_message_read(m, &type, &data) == -EINVAL);
+
+/* TODO: add support for entering containers
+        assert(sd_rtnl_message_read(m, &type, &data) > 0);
+        assert(type == IFLA_INFO_KIND);
+        assert(streq("kind", (char *) data));
+
+        assert(sd_rtnl_message_read(m, &type, &data) == 0);
+*/
+}
+
 int main(void) {
         sd_rtnl *rtnl;
         sd_rtnl_message *m;
@@ -220,6 +244,8 @@ int main(void) {
 
         test_route();
 
+        test_container();
+
         assert(sd_rtnl_open(0, &rtnl) >= 0);
         assert(rtnl);
 
diff --git a/src/systemd/sd-rtnl.h b/src/systemd/sd-rtnl.h
index e5fa785..6db5d8a 100644
--- a/src/systemd/sd-rtnl.h
+++ b/src/systemd/sd-rtnl.h
@@ -76,7 +76,13 @@ sd_rtnl_message *sd_rtnl_message_unref(sd_rtnl_message *m);
 
 int sd_rtnl_message_get_errno(sd_rtnl_message *m);
 int sd_rtnl_message_get_type(sd_rtnl_message *m, uint16_t *type);
+
+int sd_rtnl_message_link_get_ifindex(sd_rtnl_message *m, int *ifindex);
+
 int sd_rtnl_message_append(sd_rtnl_message *m, unsigned short type, const void *data);
+int sd_rtnl_message_open_container(sd_rtnl_message *m, unsigned short type);
+int sd_rtnl_message_close_container(sd_rtnl_message *m);
+
 int sd_rtnl_message_read(sd_rtnl_message *m, unsigned short *type, void **data);
 
 _SD_END_DECLARATIONS;



More information about the systemd-commits mailing list