add -Wmissing-declarations to cflags
[project/netifd.git] / bridge.c
index 4ef98e6..1f1a220 100644 (file)
--- a/bridge.c
+++ b/bridge.c
@@ -9,10 +9,61 @@
 #include "interface.h"
 #include "system.h"
 
 #include "interface.h"
 #include "system.h"
 
+enum {
+       BRIDGE_ATTR_IFNAME,
+       BRIDGE_ATTR_STP,
+       BRIDGE_ATTR_FORWARD_DELAY,
+       BRIDGE_ATTR_IGMP_SNOOP,
+       BRIDGE_ATTR_AGEING_TIME,
+       BRIDGE_ATTR_HELLO_TIME,
+       BRIDGE_ATTR_MAX_AGE,
+       __BRIDGE_ATTR_MAX
+};
+
+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_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 },
+};
+
+static const union config_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
+       [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
+};
+
+static const struct config_param_list bridge_attr_list = {
+       .n_params = __BRIDGE_ATTR_MAX,
+       .params = bridge_attrs,
+       .info = bridge_attr_info,
+
+       .n_next = 1,
+       .next = { &device_attr_list },
+};
+
+static struct device *bridge_create(const char *name, struct blob_attr *attr);
+static void bridge_config_init(struct device *dev);
+static void bridge_free(struct device *dev);
+static void bridge_dump_info(struct device *dev, struct blob_buf *b);
+
+const struct device_type bridge_device_type = {
+       .name = "Bridge",
+       .config_params = &bridge_attr_list,
+
+       .create = bridge_create,
+       .config_init = bridge_config_init,
+       .free = bridge_free,
+       .dump_info = bridge_dump_info,
+};
+
 struct bridge_state {
        struct device dev;
        device_state_cb set_state;
 
 struct bridge_state {
        struct device dev;
        device_state_cb set_state;
 
+       struct bridge_config config;
+       struct blob_attr *ifnames;
        bool active;
 
        struct list_head members;
        bool active;
 
        struct list_head members;
@@ -26,6 +77,8 @@ struct bridge_member {
        bool present;
 };
 
        bool present;
 };
 
+static void bridge_free_member(struct bridge_member *bm);
+
 static int
 bridge_disable_member(struct bridge_member *bm)
 {
 static int
 bridge_disable_member(struct bridge_member *bm)
 {
@@ -35,7 +88,7 @@ bridge_disable_member(struct bridge_member *bm)
                return 0;
 
        system_bridge_delif(&bst->dev, bm->dev.dev);
                return 0;
 
        system_bridge_delif(&bst->dev, bm->dev.dev);
-       release_device(bm->dev.dev);
+       device_release(&bm->dev);
 
        return 0;
 }
 
        return 0;
 }
@@ -49,7 +102,7 @@ bridge_enable_member(struct bridge_member *bm)
        if (!bm->present)
                return 0;
 
        if (!bm->present)
                return 0;
 
-       ret = claim_device(bm->dev.dev);
+       ret = device_claim(&bm->dev);
        if (ret < 0)
                goto error;
 
        if (ret < 0)
                goto error;
 
@@ -81,7 +134,7 @@ bridge_member_cb(struct device_user *dev, enum device_event ev)
                if (bst->dev.active)
                        bridge_enable_member(bm);
                else if (bst->n_present == 1)
                if (bst->dev.active)
                        bridge_enable_member(bm);
                else if (bst->n_present == 1)
-                       set_device_present(&bst->dev, true);
+                       device_set_present(&bst->dev, true);
 
                break;
        case DEV_EVENT_REMOVE:
 
                break;
        case DEV_EVENT_REMOVE:
@@ -94,8 +147,10 @@ bridge_member_cb(struct device_user *dev, enum device_event ev)
                bm->present = false;
                bm->bst->n_present--;
                if (bst->n_present == 0)
                bm->present = false;
                bm->bst->n_present--;
                if (bst->n_present == 0)
-                       set_device_present(&bst->dev, false);
+                       device_set_present(&bst->dev, false);
 
 
+               if (dev->hotplug)
+                       bridge_free_member(bm);
                break;
        default:
                return;
                break;
        default:
                return;
@@ -126,7 +181,7 @@ bridge_set_up(struct bridge_state *bst)
        if (!bst->n_present)
                return -ENOENT;
 
        if (!bst->n_present)
                return -ENOENT;
 
-       ret = system_bridge_addbr(&bst->dev);
+       ret = system_bridge_addbr(&bst->dev, &bst->config);
        if (ret < 0)
                goto out;
 
        if (ret < 0)
                goto out;
 
@@ -136,7 +191,7 @@ bridge_set_up(struct bridge_state *bst)
        if (!bst->n_present) {
                /* initialization of all member interfaces failed */
                system_bridge_delbr(&bst->dev);
        if (!bst->n_present) {
                /* initialization of all member interfaces failed */
                system_bridge_delbr(&bst->dev);
-               set_device_present(&bst->dev, false);
+               device_set_present(&bst->dev, false);
                return -ENOENT;
        }
 
                return -ENOENT;
        }
 
@@ -162,16 +217,17 @@ bridge_set_state(struct device *dev, bool up)
 }
 
 static struct bridge_member *
 }
 
 static struct bridge_member *
-bridge_create_member(struct bridge_state *bst, struct device *dev)
+bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
 {
        struct bridge_member *bm;
 
        bm = calloc(1, sizeof(*bm));
        bm->bst = bst;
        bm->dev.cb = bridge_member_cb;
 {
        struct bridge_member *bm;
 
        bm = calloc(1, sizeof(*bm));
        bm->bst = bst;
        bm->dev.cb = bridge_member_cb;
-       add_device_user(&bm->dev, dev);
+       device_add_user(&bm->dev, dev);
+       bm->dev.hotplug = hotplug;
 
 
-       list_add(&bm->list, &bst->members);
+       list_add_tail(&bm->list, &bst->members);
 
        if (bst->dev.active)
                bridge_enable_member(bm);
 
        if (bst->dev.active)
                bridge_enable_member(bm);
@@ -199,11 +255,11 @@ bridge_add_member(struct bridge_state *bst, const char *name)
 {
        struct device *dev;
 
 {
        struct device *dev;
 
-       dev = get_device(name, true);
+       dev = device_get(name, true);
        if (!dev)
                return;
 
        if (!dev)
                return;
 
-       bridge_create_member(bst, dev);
+       bridge_create_member(bst, dev, false);
 }
 
 static int
 }
 
 static int
@@ -211,7 +267,7 @@ bridge_hotplug_add(struct device *dev, struct device *member)
 {
        struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
 
 {
        struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
 
-       bridge_create_member(bst, member);
+       bridge_create_member(bst, member, true);
 
        return 0;
 }
 
        return 0;
 }
@@ -239,53 +295,12 @@ static const struct device_hotplug_ops bridge_ops = {
 };
 
 static void
 };
 
 static void
-bridge_parse_config(struct bridge_state *bst, struct uci_section *s)
-{
-       struct uci_element *e;
-       struct uci_option *o;
-       char buf[IFNAMSIZ + 1];
-       char *p, *end;
-       int len;
-
-       o = uci_lookup_option(uci_ctx, s, "ifname");
-       if (!o)
-               return;
-
-       if (o->type == UCI_TYPE_LIST) {
-               uci_foreach_element(&o->v.list, e)
-                       bridge_add_member(bst, e->name);
-       } else {
-               p = o->v.string;
-               do {
-                       if (!*p)
-                               break;
-
-                       if (*p == ' ')
-                               continue;
-
-                       end = strchr(p, ' ');
-                       if (!end) {
-                               bridge_add_member(bst, p);
-                               break;
-                       }
-
-                       len = end - p;
-                       if (len <= IFNAMSIZ) {
-                               memcpy(buf, p, len);
-                               buf[len] = 0;
-                               bridge_add_member(bst, buf);
-                       }
-                       p = end;
-               } while (p++);
-       }
-}
-
-static void
 bridge_free(struct device *dev)
 {
        struct bridge_state *bst;
        struct bridge_member *bm;
 
 bridge_free(struct device *dev)
 {
        struct bridge_state *bst;
        struct bridge_member *bm;
 
+       device_cleanup(dev);
        bst = container_of(dev, struct bridge_state, dev);
        while (!list_empty(&bst->members)) {
                bm = list_first_entry(&bst->members, struct bridge_member, list);
        bst = container_of(dev, struct bridge_state, dev);
        while (!list_empty(&bst->members)) {
                bm = list_first_entry(&bst->members, struct bridge_member, list);
@@ -295,7 +310,7 @@ bridge_free(struct device *dev)
 }
 
 static void
 }
 
 static void
-bridge_dump_status(struct device *dev, struct blob_buf *b)
+bridge_dump_info(struct device *dev, struct blob_buf *b)
 {
        struct bridge_state *bst;
        struct bridge_member *bm;
 {
        struct bridge_state *bst;
        struct bridge_member *bm;
@@ -310,53 +325,90 @@ bridge_dump_status(struct device *dev, struct blob_buf *b)
        blobmsg_close_array(b, list);
 }
 
        blobmsg_close_array(b, list);
 }
 
-struct device *
-bridge_create(const char *name, struct uci_section *s)
+static void
+bridge_config_init(struct device *dev)
 {
 {
-       static const struct device_type bridge_type = {
-               .name = "Bridge",
-               .free = bridge_free,
-               .dump_status = bridge_dump_status,
-       };
        struct bridge_state *bst;
        struct bridge_state *bst;
-       struct device *dev;
+       struct blob_attr *cur;
+       int rem;
 
 
-       dev = get_device(name, false);
-       if (dev)
-               return NULL;
+       bst = container_of(dev, struct bridge_state, dev);
+       blobmsg_for_each_attr(cur, bst->ifnames, rem) {
+               bridge_add_member(bst, blobmsg_data(cur));
+       }
+}
 
 
-       bst = calloc(1, sizeof(*bst));
-       if (!bst)
-               return NULL;
+static void
+bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
+{
+       struct bridge_config *cfg = &bst->config;
+       struct blob_attr *cur;
 
 
-       init_device(&bst->dev, &bridge_type, name);
+       /* defaults */
+       cfg->stp = true;
+       cfg->forward_delay = 1;
+       cfg->igmp_snoop = true;
 
 
-       bst->set_state = bst->dev.set_state;
-       bst->dev.set_state = bridge_set_state;
+       if ((cur = tb[BRIDGE_ATTR_STP]))
+               cfg->stp = blobmsg_get_bool(cur);
 
 
-       bst->dev.hotplug_ops = &bridge_ops;
+       if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
+               cfg->forward_delay = blobmsg_get_u32(cur);
 
 
-       INIT_LIST_HEAD(&bst->members);
+       if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
+               cfg->igmp_snoop = blobmsg_get_bool(cur);
 
 
-       if (s)
-               bridge_parse_config(bst, s);
+       if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
+               cfg->ageing_time = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_AGEING_TIME;
+       }
 
 
-       return &bst->dev;
+       if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
+               cfg->hello_time = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_HELLO_TIME;
+       }
+
+       if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
+               cfg->max_age = blobmsg_get_u32(cur);
+               cfg->flags |= BRIDGE_OPT_MAX_AGE;
+       }
 }
 
 }
 
-int
-interface_attach_bridge(struct interface *iface, struct uci_section *s)
+static struct device *
+bridge_create(const char *name, struct blob_attr *attr)
 {
 {
-       struct device *dev;
-       char brname[IFNAMSIZ];
+       struct blob_attr *tb_dev[__DEV_ATTR_MAX];
+       struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
+       struct bridge_state *bst;
+       struct device *dev = NULL;
 
 
-       snprintf(brname, IFNAMSIZ - 1, "br-%s", iface->name);
-       brname[IFNAMSIZ - 1] = 0;
+       blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
+               blob_data(attr), blob_len(attr));
+       blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
+               blob_data(attr), blob_len(attr));
 
 
-       dev = bridge_create(brname, s);
-       if (!dev)
-               return -1;
+       if (!tb_br[BRIDGE_ATTR_IFNAME])
+               return NULL;
 
 
-       add_device_user(&iface->main_dev, dev);
-       return 0;
+       bst = calloc(1, sizeof(*bst));
+       if (!bst)
+               return NULL;
+
+       dev = &bst->dev;
+       device_init(dev, &bridge_device_type, name);
+       device_init_settings(dev, tb_dev);
+       dev->config_pending = true;
+       bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
+       bridge_apply_settings(bst, tb_br);
+
+       bst->set_state = dev->set_state;
+       dev->set_state = bridge_set_state;
+
+       dev->hotplug_ops = &bridge_ops;
+
+       INIT_LIST_HEAD(&bst->members);
+
+       return dev;
 }
 }
+
+