From b6d7a8bc9822667cb851bec465c3ad7cd6714e90 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Fri, 4 Nov 2011 01:47:13 +0100 Subject: [PATCH] add a hotplug op for preparing an interface bridge so that it's ready when an external process adds interfaces --- bridge.c | 21 ++++++++++++++++++--- device.h | 1 + ubus.c | 22 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/bridge.c b/bridge.c index 5ff7081..5337e3a 100644 --- a/bridge.c +++ b/bridge.c @@ -65,6 +65,7 @@ struct bridge_state { struct bridge_config config; struct blob_attr *ifnames; bool active; + bool force_active; struct list_head members; int n_present; @@ -131,6 +132,7 @@ bridge_remove_member(struct bridge_member *bm) if (bst->dev.active) bridge_disable_member(bm); + bst->force_active = false; if (bst->n_present == 0) device_set_present(&bst->dev, false); } @@ -190,7 +192,7 @@ bridge_set_up(struct bridge_state *bst) struct bridge_member *bm; int ret; - if (!bst->n_present) + if (!bst->force_active && !bst->n_present) return -ENOENT; ret = system_bridge_addbr(&bst->dev, &bst->config); @@ -200,7 +202,7 @@ bridge_set_up(struct bridge_state *bst) list_for_each_entry(bm, &bst->members, list) bridge_enable_member(bm); - if (!bst->n_present) { + if (!bst->force_active && !bst->n_present) { /* initialization of all member interfaces failed */ system_bridge_delbr(&bst->dev); device_set_present(&bst->dev, false); @@ -289,10 +291,23 @@ bridge_hotplug_del(struct device *dev, struct device *member) return 0; } - return -ENOENT; + return UBUS_STATUS_NOT_FOUND; +} + +static int +bridge_hotplug_prepare(struct device *dev) +{ + struct bridge_state *bst; + + bst = container_of(dev, struct bridge_state, dev); + bst->force_active = true; + device_set_present(&bst->dev, true); + + return 0; } static const struct device_hotplug_ops bridge_ops = { + .prepare = bridge_hotplug_prepare, .add = bridge_hotplug_add, .del = bridge_hotplug_del }; diff --git a/device.h b/device.h index a3728f3..0d9e6f4 100644 --- a/device.h +++ b/device.h @@ -111,6 +111,7 @@ struct device { }; struct device_hotplug_ops { + int (*prepare)(struct device *dev); int (*add)(struct device *main, struct device *member); int (*del)(struct device *main, struct device *member); }; diff --git a/ubus.c b/ubus.c index ce82a52..af5ba68 100644 --- a/ubus.c +++ b/ubus.c @@ -380,10 +380,32 @@ netifd_iface_remove(struct ubus_context *ctx, struct ubus_object *obj, return 0; } +static int +netifd_handle_iface_prepare(struct ubus_context *ctx, struct ubus_object *obj, + struct ubus_request_data *req, const char *method, + struct blob_attr *msg) +{ + struct interface *iface; + struct device *dev; + const struct device_hotplug_ops *ops; + + iface = container_of(obj, struct interface, ubus); + dev = iface->main_dev.dev; + if (!dev) + return 0; + + ops = dev->hotplug_ops; + if (!ops) + return 0; + + return ops->prepare(dev); +} + static struct ubus_method iface_object_methods[] = { { .name = "up", .handler = netifd_handle_up }, { .name = "down", .handler = netifd_handle_down }, { .name = "status", .handler = netifd_handle_status }, + { .name = "prepare", .handler = netifd_handle_iface_prepare }, UBUS_METHOD("add_device", netifd_iface_handle_device, dev_policy ), UBUS_METHOD("remove_device", netifd_iface_handle_device, dev_policy ), { .name = "notify_proto", .handler = netifd_iface_notify_proto }, -- 2.25.1