summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTom Gundersen <teg@jklm.no>2013-11-24 23:37:56 +0100
committerTom Gundersen <teg@jklm.no>2013-11-26 01:32:25 +0100
commit02b59d57e0c08231645120077f651151f5bb2bab (patch)
tree10549c36b26f6fda4daf7b9d11c994adfc6a88ff /src
parentc166a070553511e402de5ad216d3fb62b49bdacb (diff)
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.
Diffstat (limited to 'src')
-rw-r--r--src/network/networkd-bridge.c321
-rw-r--r--src/network/networkd-gperf.gperf3
-rw-r--r--src/network/networkd-link.c57
-rw-r--r--src/network/networkd-manager.c30
-rw-r--r--src/network/networkd-network.c46
-rw-r--r--src/network/networkd.c4
-rw-r--r--src/network/networkd.h60
-rw-r--r--src/network/test-network.c8
8 files changed, 511 insertions, 18 deletions
diff --git a/src/network/networkd-bridge.c b/src/network/networkd-bridge.c
new file mode 100644
index 0000000000..371c093ca1
--- /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@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 7fbe4669ac..385f1bb856 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 255b413586..09de862819 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 c28f3d9813..1dc7e88dfd 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 eb3ff2316d..ae8d4d160d 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 9144f97dc1..1d43361201 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 913f17d3a0..1ab8a55a88 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 822a334607..5e279974cb 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);