15 BRIDGE_ATTR_FORWARD_DELAY,
16 BRIDGE_ATTR_IGMP_SNOOP,
17 BRIDGE_ATTR_AGEING_TIME,
18 BRIDGE_ATTR_HELLO_TIME,
23 static const struct blobmsg_policy bridge_attrs[__BRIDGE_ATTR_MAX] = {
24 [BRIDGE_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
25 [BRIDGE_ATTR_STP] = { "stp", BLOBMSG_TYPE_BOOL },
26 [BRIDGE_ATTR_FORWARD_DELAY] = { "forward_delay", BLOBMSG_TYPE_INT32 },
27 [BRIDGE_ATTR_AGEING_TIME] = { "ageing_time", BLOBMSG_TYPE_INT32 },
28 [BRIDGE_ATTR_HELLO_TIME] = { "hello_time", BLOBMSG_TYPE_INT32 },
29 [BRIDGE_ATTR_MAX_AGE] = { "max_age", BLOBMSG_TYPE_INT32 },
30 [BRIDGE_ATTR_IGMP_SNOOP] = { "igmp_snooping", BLOBMSG_TYPE_BOOL },
33 static const union config_param_info bridge_attr_info[__BRIDGE_ATTR_MAX] = {
34 [BRIDGE_ATTR_IFNAME] = { .type = BLOBMSG_TYPE_STRING },
37 static const struct config_param_list bridge_attr_list = {
38 .n_params = __BRIDGE_ATTR_MAX,
39 .params = bridge_attrs,
40 .info = bridge_attr_info,
43 .next = { &device_attr_list },
46 static struct device *bridge_create(const char *name, struct blob_attr *attr);
47 static void bridge_config_init(struct device *dev);
48 static void bridge_free(struct device *dev);
49 static void bridge_dump_info(struct device *dev, struct blob_buf *b);
51 const struct device_type bridge_device_type = {
53 .config_params = &bridge_attr_list,
55 .create = bridge_create,
56 .config_init = bridge_config_init,
58 .dump_info = bridge_dump_info,
63 device_state_cb set_state;
65 struct bridge_config config;
66 struct blob_attr *ifnames;
70 struct list_head members;
74 struct bridge_member {
75 struct list_head list;
76 struct bridge_state *bst;
77 struct device_user dev;
81 static void bridge_free_member(struct bridge_member *bm);
84 bridge_disable_member(struct bridge_member *bm)
86 struct bridge_state *bst = bm->bst;
91 system_bridge_delif(&bst->dev, bm->dev.dev);
92 device_release(&bm->dev);
98 bridge_enable_member(struct bridge_member *bm)
100 struct bridge_state *bst = bm->bst;
106 ret = device_claim(&bm->dev);
110 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
123 bridge_remove_member(struct bridge_member *bm)
125 struct bridge_state *bst = bm->bst;
131 bm->bst->n_present--;
133 bridge_disable_member(bm);
135 bst->force_active = false;
136 if (bst->n_present == 0)
137 device_set_present(&bst->dev, false);
141 bridge_member_cb(struct device_user *dev, enum device_event ev)
143 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
144 struct bridge_state *bst = bm->bst;
148 assert(!bm->present);
154 bridge_enable_member(bm);
155 else if (bst->n_present == 1)
156 device_set_present(&bst->dev, true);
159 case DEV_EVENT_REMOVE:
164 bridge_free_member(bm);
166 bridge_remove_member(bm);
175 bridge_set_down(struct bridge_state *bst)
177 struct bridge_member *bm;
179 bst->set_state(&bst->dev, false);
181 list_for_each_entry(bm, &bst->members, list)
182 bridge_disable_member(bm);
184 system_bridge_delbr(&bst->dev);
190 bridge_set_up(struct bridge_state *bst)
192 struct bridge_member *bm;
195 if (!bst->force_active && !bst->n_present)
198 ret = system_bridge_addbr(&bst->dev, &bst->config);
202 list_for_each_entry(bm, &bst->members, list)
203 bridge_enable_member(bm);
205 if (!bst->force_active && !bst->n_present) {
206 /* initialization of all member interfaces failed */
207 system_bridge_delbr(&bst->dev);
208 device_set_present(&bst->dev, false);
212 ret = bst->set_state(&bst->dev, true);
214 bridge_set_down(bst);
221 bridge_set_state(struct device *dev, bool up)
223 struct bridge_state *bst;
225 bst = container_of(dev, struct bridge_state, dev);
228 return bridge_set_up(bst);
230 return bridge_set_down(bst);
233 static struct bridge_member *
234 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
236 struct bridge_member *bm;
238 bm = calloc(1, sizeof(*bm));
240 bm->dev.cb = bridge_member_cb;
241 bm->dev.hotplug = hotplug;
242 list_add_tail(&bm->list, &bst->members);
244 device_add_user(&bm->dev, dev);
250 bridge_free_member(struct bridge_member *bm)
252 bridge_remove_member(bm);
253 device_remove_user(&bm->dev);
259 bridge_add_member(struct bridge_state *bst, const char *name)
263 dev = device_get(name, true);
267 bridge_create_member(bst, dev, false);
271 bridge_hotplug_add(struct device *dev, struct device *member)
273 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
275 bridge_create_member(bst, member, true);
281 bridge_hotplug_del(struct device *dev, struct device *member)
283 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
284 struct bridge_member *bm;
286 list_for_each_entry(bm, &bst->members, list) {
287 if (bm->dev.dev != member)
290 bridge_free_member(bm);
294 return UBUS_STATUS_NOT_FOUND;
298 bridge_hotplug_prepare(struct device *dev)
300 struct bridge_state *bst;
302 bst = container_of(dev, struct bridge_state, dev);
303 bst->force_active = true;
304 device_set_present(&bst->dev, true);
309 static const struct device_hotplug_ops bridge_ops = {
310 .prepare = bridge_hotplug_prepare,
311 .add = bridge_hotplug_add,
312 .del = bridge_hotplug_del
316 bridge_free(struct device *dev)
318 struct bridge_state *bst;
319 struct bridge_member *bm;
322 bst = container_of(dev, struct bridge_state, dev);
323 while (!list_empty(&bst->members)) {
324 bm = list_first_entry(&bst->members, struct bridge_member, list);
325 bridge_free_member(bm);
331 bridge_dump_info(struct device *dev, struct blob_buf *b)
333 struct bridge_state *bst;
334 struct bridge_member *bm;
337 bst = container_of(dev, struct bridge_state, dev);
339 system_if_dump_info(dev, b);
340 list = blobmsg_open_array(b, "bridge-members");
341 list_for_each_entry(bm, &bst->members, list) {
342 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
344 blobmsg_close_array(b, list);
348 bridge_config_init(struct device *dev)
350 struct bridge_state *bst;
351 struct blob_attr *cur;
354 bst = container_of(dev, struct bridge_state, dev);
359 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
360 bridge_add_member(bst, blobmsg_data(cur));
365 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
367 struct bridge_config *cfg = &bst->config;
368 struct blob_attr *cur;
372 cfg->forward_delay = 1;
373 cfg->igmp_snoop = true;
375 if ((cur = tb[BRIDGE_ATTR_STP]))
376 cfg->stp = blobmsg_get_bool(cur);
378 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
379 cfg->forward_delay = blobmsg_get_u32(cur);
381 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
382 cfg->igmp_snoop = blobmsg_get_bool(cur);
384 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
385 cfg->ageing_time = blobmsg_get_u32(cur);
386 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
389 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
390 cfg->hello_time = blobmsg_get_u32(cur);
391 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
394 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
395 cfg->max_age = blobmsg_get_u32(cur);
396 cfg->flags |= BRIDGE_OPT_MAX_AGE;
400 static struct device *
401 bridge_create(const char *name, struct blob_attr *attr)
403 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
404 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
405 struct bridge_state *bst;
406 struct device *dev = NULL;
408 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
409 blob_data(attr), blob_len(attr));
410 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
411 blob_data(attr), blob_len(attr));
413 bst = calloc(1, sizeof(*bst));
418 device_init(dev, &bridge_device_type, name);
419 device_init_settings(dev, tb_dev);
420 dev->config_pending = true;
421 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
422 bridge_apply_settings(bst, tb_br);
424 bst->set_state = dev->set_state;
425 dev->set_state = bridge_set_state;
427 dev->hotplug_ops = &bridge_ops;
429 INIT_LIST_HEAD(&bst->members);