proto-shell: Model config parameter "no-proto-task" as a proto flag
[project/netifd.git] / bridge.c
index ce22b75..1c163cd 100644 (file)
--- a/bridge.c
+++ b/bridge.c
@@ -32,6 +32,12 @@ enum {
        BRIDGE_ATTR_HELLO_TIME,
        BRIDGE_ATTR_MAX_AGE,
        BRIDGE_ATTR_BRIDGE_EMPTY,
+       BRIDGE_ATTR_MULTICAST_QUERIER,
+       BRIDGE_ATTR_HASH_MAX,
+       BRIDGE_ATTR_ROBUSTNESS,
+       BRIDGE_ATTR_QUERY_INTERVAL,
+       BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL,
+       BRIDGE_ATTR_LAST_MEMBER_INTERVAL,
        __BRIDGE_ATTR_MAX
 };
 
@@ -45,6 +51,12 @@ static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
        [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 },
+       [BRIDGE_ATTR_MULTICAST_QUERIER] = { "multicast_querier", BLOBMSG_TYPE_BOOL },
+       [BRIDGE_ATTR_HASH_MAX] = { "hash_max", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_ROBUSTNESS] = { "robustness", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_QUERY_INTERVAL] = { "query_interval", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL] = { "query_response_interval", BLOBMSG_TYPE_INT32 },
+       [BRIDGE_ATTR_LAST_MEMBER_INTERVAL] = { "last_member_interval", BLOBMSG_TYPE_INT32 },
 };
 
 static const struct uci_blob_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
@@ -88,9 +100,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 {
@@ -146,6 +160,32 @@ bridge_disable_member(struct bridge_member *bm)
 }
 
 static int
+bridge_enable_interface(struct bridge_state *bst)
+{
+       int ret;
+
+       if (bst->active)
+               return 0;
+
+       ret = system_bridge_addbr(&bst->dev, &bst->config);
+       if (ret < 0)
+               return ret;
+
+       bst->active = true;
+       return 0;
+}
+
+static void
+bridge_disable_interface(struct bridge_state *bst)
+{
+       if (!bst->active)
+               return;
+
+       system_bridge_delbr(&bst->dev);
+       bst->active = false;
+}
+
+static int
 bridge_enable_member(struct bridge_member *bm)
 {
        struct bridge_state *bst = bm->bst;
@@ -154,6 +194,10 @@ bridge_enable_member(struct bridge_member *bm)
        if (!bm->present)
                return 0;
 
+       ret = bridge_enable_interface(bst);
+       if (ret)
+               goto error;
+
        /* Disable IPv6 for bridge members */
        if (!(bm->dev.dev->settings.flags & DEV_OPT_IPV6)) {
                bm->dev.dev->settings.ipv6 = 0;
@@ -170,13 +214,17 @@ bridge_enable_member(struct bridge_member *bm)
                goto error;
        }
 
+       device_set_present(&bst->dev, true);
        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;
 }
 
@@ -229,6 +277,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);
@@ -250,7 +307,7 @@ bridge_member_cb(struct device_user *dev, enum device_event ev)
                         * bridge mtu is set
                         */
                        system_if_apply_settings(&bst->dev, &bst->dev.settings,
-                                                DEV_OPT_MTU);
+                                                DEV_OPT_MTU | DEV_OPT_MTU6);
                }
 
                break;
@@ -279,7 +336,7 @@ bridge_set_down(struct bridge_state *bst)
        vlist_for_each_element(&bst->members, bm, node)
                bridge_disable_member(bm);
 
-       system_bridge_delbr(&bst->dev);
+       bridge_disable_interface(bst);
 
        return 0;
 }
@@ -290,19 +347,23 @@ bridge_set_up(struct bridge_state *bst)
        struct bridge_member *bm;
        int ret;
 
-       if (!bst->force_active && !bst->n_present)
-               return -ENOENT;
+       if (!bst->n_present) {
+               if (!bst->force_active)
+                       return -ENOENT;
 
-       ret = system_bridge_addbr(&bst->dev, &bst->config);
-       if (ret < 0)
-               goto out;
+               ret = bridge_enable_interface(bst);
+               if (ret)
+                       return ret;
+       }
 
+       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 */
-               system_bridge_delbr(&bst->dev);
+               bridge_disable_interface(bst);
                device_set_present(&bst->dev, false);
                return -ENOENT;
        }
@@ -312,7 +373,6 @@ bridge_set_up(struct bridge_state *bst)
        if (ret < 0)
                bridge_set_down(bst);
 
-out:
        return ret;
 }
 
@@ -443,6 +503,7 @@ bridge_free(struct device *dev)
 
        bst = container_of(dev, struct bridge_state, dev);
        vlist_flush_all(&bst->members);
+       free(bst->config_data);
        free(bst);
 }
 
@@ -478,6 +539,7 @@ bridge_config_init(struct device *dev)
                device_set_present(&bst->dev, true);
        }
 
+       bst->n_failed = 0;
        vlist_update(&bst->members);
        if (bst->ifnames) {
                blobmsg_for_each_attr(cur, bst->ifnames, rem) {
@@ -485,6 +547,7 @@ bridge_config_init(struct device *dev)
                }
        }
        vlist_flush(&bst->members);
+       bridge_check_retry(bst);
 }
 
 static void
@@ -497,6 +560,12 @@ bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
        cfg->stp = false;
        cfg->forward_delay = 2;
        cfg->igmp_snoop = true;
+       cfg->multicast_querier = true;
+       cfg->robustness = 2;
+       cfg->query_interval = 12500;
+       cfg->query_response_interval = 1000;
+       cfg->last_member_interval = 100;
+       cfg->hash_max = 512;
        cfg->bridge_empty = false;
        cfg->priority = 0x7FFF;
 
@@ -510,7 +579,33 @@ bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
                cfg->priority = blobmsg_get_u32(cur);
 
        if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
-               cfg->igmp_snoop = blobmsg_get_bool(cur);
+               cfg->multicast_querier = cfg->igmp_snoop = blobmsg_get_bool(cur);
+
+       if ((cur = tb[BRIDGE_ATTR_MULTICAST_QUERIER]))
+               cfg->multicast_querier = blobmsg_get_bool(cur);
+
+       if ((cur = tb[BRIDGE_ATTR_HASH_MAX]))
+               cfg->hash_max = blobmsg_get_u32(cur);
+
+       if ((cur = tb[BRIDGE_ATTR_ROBUSTNESS])) {
+               cfg->robustness = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_ROBUSTNESS;
+       }
+
+       if ((cur = tb[BRIDGE_ATTR_QUERY_INTERVAL])) {
+               cfg->query_interval = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_QUERY_INTERVAL;
+       }
+
+       if ((cur = tb[BRIDGE_ATTR_QUERY_RESPONSE_INTERVAL])) {
+               cfg->query_response_interval = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_QUERY_RESPONSE_INTERVAL;
+       }
+
+       if ((cur = tb[BRIDGE_ATTR_LAST_MEMBER_INTERVAL])) {
+               cfg->last_member_interval = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_LAST_MEMBER_INTERVAL;
+       }
 
        if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
                cfg->ageing_time = blobmsg_get_u32(cur);
@@ -544,6 +639,7 @@ bridge_reload(struct device *dev, struct blob_attr *attr)
        BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
 
        bst = container_of(dev, struct bridge_state, dev);
+       attr = blob_memdup(attr);
 
        blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
                blob_data(attr), blob_len(attr));
@@ -563,7 +659,7 @@ bridge_reload(struct device *dev, struct blob_attr *attr)
 
                diff = 0;
                uci_blob_diff(tb_dev, otb_dev, &device_attr_list, &diff);
-               if (diff & ~(1 << DEV_ATTR_IFNAME))
+               if (diff)
                    ret = DEV_CONFIG_RESTART;
 
                blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
@@ -577,10 +673,31 @@ bridge_reload(struct device *dev, struct blob_attr *attr)
                bridge_config_init(dev);
        }
 
+       free(bst->config_data);
        bst->config_data = 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;
+               bst->n_present++;
+               bridge_enable_member(bm);
+       }
+}
+
 static struct device *
 bridge_create(const char *name, struct blob_attr *attr)
 {
@@ -594,6 +711,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;