bridge: release failed member devices
[project/netifd.git] / bridge.c
index 1d02f83..4291e7c 100644 (file)
--- a/bridge.c
+++ b/bridge.c
@@ -26,10 +26,12 @@ enum {
        BRIDGE_ATTR_IFNAME,
        BRIDGE_ATTR_STP,
        BRIDGE_ATTR_FORWARD_DELAY,
+       BRIDGE_ATTR_PRIORITY,
        BRIDGE_ATTR_IGMP_SNOOP,
        BRIDGE_ATTR_AGEING_TIME,
        BRIDGE_ATTR_HELLO_TIME,
        BRIDGE_ATTR_MAX_AGE,
+       BRIDGE_ATTR_BRIDGE_EMPTY,
        __BRIDGE_ATTR_MAX
 };
 
@@ -37,17 +39,19 @@ static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
        [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
        [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
        [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_PRIORITY] = { "priority", BLOBMSG_TYPE_INT32 },
        [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
        [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
        [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
        [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
+       [BRIDGE_ATTR_BRIDGE_EMPTY] = { "bridge_empty", BLOBMSG_TYPE_BOOL },
 };
 
-static const union config_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
+static const struct uci_blob_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
        [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
 };
 
-static const struct config_param_list bridge_attr_list = {
+static const struct uci_blob_param_list bridge_attr_list = {
        .n_params = __BRIDGE_ATTR_MAX,
        .params = bridge_attrs,
        .info = bridge_attr_info,
@@ -84,9 +88,11 @@ struct bridge_state {
        bool active;
        bool force_active;
 
+       struct uloop_timeout retry;
        struct bridge_member *primary_port;
        struct vlist_tree members;
        int n_present;
+       int n_failed;
 };
 
 struct bridge_member {
@@ -136,6 +142,8 @@ bridge_disable_member(struct bridge_member *bm)
        system_bridge_delif(&bst->dev, bm->dev.dev);
        device_release(&bm->dev);
 
+       device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
+
        return 0;
 }
 
@@ -148,6 +156,12 @@ bridge_enable_member(struct bridge_member *bm)
        if (!bm->present)
                return 0;
 
+       /* Disable IPv6 for bridge members */
+       if (!(bm->dev.dev->settings.flags & DEV_OPT_IPV6)) {
+               bm->dev.dev->settings.ipv6 = 0;
+               bm->dev.dev->settings.flags |= DEV_OPT_IPV6;
+       }
+
        ret = device_claim(&bm->dev);
        if (ret < 0)
                goto error;
@@ -158,11 +172,16 @@ bridge_enable_member(struct bridge_member *bm)
                goto error;
        }
 
+       device_broadcast_event(&bst->dev, DEV_EVENT_TOPO_CHANGE);
+
        return 0;
 
 error:
+       bst->n_failed++;
        bm->present = false;
        bst->n_present--;
+       device_release(&bm->dev);
+
        return ret;
 }
 
@@ -174,7 +193,7 @@ bridge_remove_member(struct bridge_member *bm)
        if (!bm->present)
                return;
 
-       if (bm == bst->primary_port);
+       if (bm == bst->primary_port)
                bridge_reset_primary(bst);
 
        if (bst->dev.active)
@@ -183,6 +202,9 @@ bridge_remove_member(struct bridge_member *bm)
        bm->present = false;
        bm->bst->n_present--;
 
+       if (bst->config.bridge_empty)
+               return;
+
        bst->force_active = false;
        if (bst->n_present == 0)
                device_set_present(&bst->dev, false);
@@ -212,6 +234,15 @@ bridge_free_member(struct bridge_member *bm)
 }
 
 static void
+bridge_check_retry(struct bridge_state *bst)
+{
+       if (!bst->n_failed)
+               return;
+
+       uloop_timeout_set(&bst->retry, 100);
+}
+
+static void
 bridge_member_cb(struct device_user *dev, enum device_event ev)
 {
        struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
@@ -224,10 +255,17 @@ bridge_member_cb(struct device_user *dev, enum device_event ev)
                bm->present = true;
                bst->n_present++;
 
-               if (bst->dev.active)
-                       bridge_enable_member(bm);
-               else if (bst->n_present == 1)
+               if (bst->n_present == 1)
                        device_set_present(&bst->dev, true);
+               if (bst->dev.active && !bridge_enable_member(bm)) {
+                       /*
+                        * Adding a bridge member can overwrite the bridge mtu
+                        * in the kernel, apply the bridge settings in case the
+                        * bridge mtu is set
+                        */
+                       system_if_apply_settings(&bst->dev, &bst->dev.settings,
+                                                DEV_OPT_MTU);
+               }
 
                break;
        case DEV_EVENT_REMOVE:
@@ -273,8 +311,10 @@ bridge_set_up(struct bridge_state *bst)
        if (ret < 0)
                goto out;
 
+       bst->n_failed = 0;
        vlist_for_each_element(&bst->members, bm, node)
                bridge_enable_member(bm);
+       bridge_check_retry(bst);
 
        if (!bst->force_active && !bst->n_present) {
                /* initialization of all member interfaces failed */
@@ -311,13 +351,20 @@ bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
        struct bridge_member *bm;
 
        bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
+       if (!bm)
+               return NULL;
+
        bm->bst = bst;
        bm->dev.cb = bridge_member_cb;
        bm->dev.hotplug = hotplug;
        strcpy(bm->name, dev->ifname);
        bm->dev.dev = dev;
        vlist_add(&bst->members, &bm->node, bm->name);
-       if (hotplug)
+       // Need to look up the bridge member again as the above
+       // created pointer will be freed in case the bridge member
+       // already existed
+       bm = vlist_find(&bst->members, dev->ifname, bm, node);
+       if (hotplug && bm)
                bm->node.version = -1;
 
        return bm;
@@ -442,14 +489,20 @@ bridge_config_init(struct device *dev)
 
        bst = container_of(dev, struct bridge_state, dev);
 
-       if (!bst->ifnames)
-               return;
+       if (bst->config.bridge_empty) {
+               bst->force_active = true;
+               device_set_present(&bst->dev, true);
+       }
 
+       bst->n_failed = 0;
        vlist_update(&bst->members);
-       blobmsg_for_each_attr(cur, bst->ifnames, rem) {
-               bridge_add_member(bst, blobmsg_data(cur));
+       if (bst->ifnames) {
+               blobmsg_for_each_attr(cur, bst->ifnames, rem) {
+                       bridge_add_member(bst, blobmsg_data(cur));
+               }
        }
        vlist_flush(&bst->members);
+       bridge_check_retry(bst);
 }
 
 static void
@@ -462,6 +515,8 @@ bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
        cfg->stp = false;
        cfg->forward_delay = 2;
        cfg->igmp_snoop = true;
+       cfg->bridge_empty = false;
+       cfg->priority = 0x7FFF;
 
        if ((cur = tb[BRIDGE_ATTR_STP]))
                cfg->stp = blobmsg_get_bool(cur);
@@ -469,6 +524,9 @@ bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
        if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
                cfg->forward_delay = blobmsg_get_u32(cur);
 
+       if ((cur = tb[BRIDGE_ATTR_PRIORITY]))
+               cfg->priority = blobmsg_get_u32(cur);
+
        if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
                cfg->igmp_snoop = blobmsg_get_bool(cur);
 
@@ -486,6 +544,9 @@ bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
                cfg->max_age = blobmsg_get_u32(cur);
                cfg->flags |= BRIDGE_OPT_MAX_AGE;
        }
+
+       if ((cur = tb[BRIDGE_ATTR_BRIDGE_EMPTY]))
+               cfg->bridge_empty = blobmsg_get_bool(cur);
 }
 
 enum dev_change_type
@@ -519,7 +580,7 @@ bridge_reload(struct device *dev, struct blob_attr *attr)
                        blob_data(bst->config_data), blob_len(bst->config_data));
 
                diff = 0;
-               config_diff(tb_dev, otb_dev, &device_attr_list, &diff);
+               uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
                if (diff & ~(1 << DEV_ATTR_IFNAME))
                    ret = DEV_CONFIG_RESTART;
 
@@ -527,7 +588,7 @@ bridge_reload(struct device *dev, struct blob_attr *attr)
                        blob_data(bst->config_data), blob_len(bst->config_data));
 
                diff = 0;
-               config_diff(tb_br, otb_br, &bridge_attr_list, &diff);
+               uci_blob_diff(tb_br, otb_br, &bridge_attr_list, &diff);
                if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
                    ret = DEV_CONFIG_RESTART;
 
@@ -538,6 +599,25 @@ bridge_reload(struct device *dev, struct blob_attr *attr)
        return ret;
 }
 
+static void
+bridge_retry_members(struct uloop_timeout *timeout)
+{
+       struct bridge_state *bst = container_of(timeout, struct bridge_state, retry);
+       struct bridge_member *bm;
+
+       bst->n_failed = 0;
+       vlist_for_each_element(&bst->members, bm, node) {
+               if (bm->present)
+                       continue;
+
+               if (!bm->dev.dev->present)
+                       continue;
+
+               bm->present = true;
+               bridge_enable_member(bm);
+       }
+}
+
 static struct device *
 bridge_create(const char *name, struct blob_attr *attr)
 {
@@ -551,6 +631,7 @@ bridge_create(const char *name, struct blob_attr *attr)
        dev = &bst->dev;
        device_init(dev, &bridge_device_type, name);
        dev->config_pending = true;
+       bst->retry.cb = bridge_retry_members;
 
        bst->set_state = dev->set_state;
        dev->set_state = bridge_set_state;
@@ -563,5 +644,3 @@ bridge_create(const char *name, struct blob_attr *attr)
 
        return dev;
 }
-
-