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 bridge_reload(struct device *dev, struct blob_attr *attr);
53 const struct device_type bridge_device_type = {
55 .config_params = &bridge_attr_list,
57 .create = bridge_create,
58 .config_init = bridge_config_init,
59 .reload = bridge_reload,
61 .dump_info = bridge_dump_info,
66 device_state_cb set_state;
68 struct blob_attr *config_data;
69 struct bridge_config config;
70 struct blob_attr *ifnames;
74 struct vlist_tree members;
78 struct bridge_member {
79 struct vlist_node node;
80 struct bridge_state *bst;
81 struct device_user dev;
87 bridge_disable_member(struct bridge_member *bm)
89 struct bridge_state *bst = bm->bst;
94 system_bridge_delif(&bst->dev, bm->dev.dev);
95 device_release(&bm->dev);
101 bridge_enable_member(struct bridge_member *bm)
103 struct bridge_state *bst = bm->bst;
109 ret = device_claim(&bm->dev);
113 ret = system_bridge_addif(&bst->dev, bm->dev.dev);
115 D(DEVICE, "Bridge device %s could not be added\n", bm->dev.dev->ifname);
128 bridge_remove_member(struct bridge_member *bm)
130 struct bridge_state *bst = bm->bst;
136 bridge_disable_member(bm);
139 bm->bst->n_present--;
141 bst->force_active = false;
142 if (bst->n_present == 0)
143 device_set_present(&bst->dev, false);
147 bridge_member_cb(struct device_user *dev, enum device_event ev)
149 struct bridge_member *bm = container_of(dev, struct bridge_member, dev);
150 struct bridge_state *bst = bm->bst;
154 assert(!bm->present);
160 bridge_enable_member(bm);
161 else if (bst->n_present == 1)
162 device_set_present(&bst->dev, true);
165 case DEV_EVENT_REMOVE:
167 vlist_delete(&bst->members, &bm->node);
172 bridge_remove_member(bm);
181 bridge_set_down(struct bridge_state *bst)
183 struct bridge_member *bm;
185 bst->set_state(&bst->dev, false);
187 vlist_for_each_element(&bst->members, bm, node)
188 bridge_disable_member(bm);
190 system_bridge_delbr(&bst->dev);
196 bridge_set_up(struct bridge_state *bst)
198 struct bridge_member *bm;
201 if (!bst->force_active && !bst->n_present)
204 ret = system_bridge_addbr(&bst->dev, &bst->config);
208 vlist_for_each_element(&bst->members, bm, node)
209 bridge_enable_member(bm);
211 if (!bst->force_active && !bst->n_present) {
212 /* initialization of all member interfaces failed */
213 system_bridge_delbr(&bst->dev);
214 device_set_present(&bst->dev, false);
218 ret = bst->set_state(&bst->dev, true);
220 bridge_set_down(bst);
227 bridge_set_state(struct device *dev, bool up)
229 struct bridge_state *bst;
231 bst = container_of(dev, struct bridge_state, dev);
234 return bridge_set_up(bst);
236 return bridge_set_down(bst);
239 static struct bridge_member *
240 bridge_create_member(struct bridge_state *bst, struct device *dev, bool hotplug)
242 struct bridge_member *bm;
244 bm = calloc(1, sizeof(*bm) + strlen(dev->ifname) + 1);
246 bm->dev.cb = bridge_member_cb;
247 bm->dev.hotplug = hotplug;
248 strcpy(bm->name, dev->ifname);
250 vlist_add(&bst->members, &bm->node, bm->name);
252 bm->node.version = -1;
258 bridge_member_update(struct vlist_tree *tree, struct vlist_node *node_new,
259 struct vlist_node *node_old)
261 struct bridge_member *bm;
265 bm = container_of(node_new, struct bridge_member, node);
274 device_add_user(&bm->dev, dev);
279 bm = container_of(node_old, struct bridge_member, node);
280 bridge_remove_member(bm);
281 device_remove_user(&bm->dev);
288 bridge_add_member(struct bridge_state *bst, const char *name)
292 dev = device_get(name, true);
296 bridge_create_member(bst, dev, false);
300 bridge_hotplug_add(struct device *dev, struct device *member)
302 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
304 bridge_create_member(bst, member, true);
310 bridge_hotplug_del(struct device *dev, struct device *member)
312 struct bridge_state *bst = container_of(dev, struct bridge_state, dev);
313 struct bridge_member *bm;
315 bm = vlist_find(&bst->members, member->ifname, bm, node);
317 return UBUS_STATUS_NOT_FOUND;
319 vlist_delete(&bst->members, &bm->node);
324 bridge_hotplug_prepare(struct device *dev)
326 struct bridge_state *bst;
328 bst = container_of(dev, struct bridge_state, dev);
329 bst->force_active = true;
330 device_set_present(&bst->dev, true);
335 static const struct device_hotplug_ops bridge_ops = {
336 .prepare = bridge_hotplug_prepare,
337 .add = bridge_hotplug_add,
338 .del = bridge_hotplug_del
342 bridge_free(struct device *dev)
344 struct bridge_state *bst;
347 bst = container_of(dev, struct bridge_state, dev);
348 vlist_flush_all(&bst->members);
353 bridge_dump_info(struct device *dev, struct blob_buf *b)
355 struct bridge_state *bst;
356 struct bridge_member *bm;
359 bst = container_of(dev, struct bridge_state, dev);
361 system_if_dump_info(dev, b);
362 list = blobmsg_open_array(b, "bridge-members");
364 vlist_for_each_element(&bst->members, bm, node)
365 blobmsg_add_string(b, NULL, bm->dev.dev->ifname);
367 blobmsg_close_array(b, list);
371 bridge_config_init(struct device *dev)
373 struct bridge_state *bst;
374 struct blob_attr *cur;
377 bst = container_of(dev, struct bridge_state, dev);
382 vlist_update(&bst->members);
383 blobmsg_for_each_attr(cur, bst->ifnames, rem) {
384 bridge_add_member(bst, blobmsg_data(cur));
386 vlist_flush(&bst->members);
390 bridge_apply_settings(struct bridge_state *bst, struct blob_attr **tb)
392 struct bridge_config *cfg = &bst->config;
393 struct blob_attr *cur;
397 cfg->forward_delay = 1;
398 cfg->igmp_snoop = true;
400 if ((cur = tb[BRIDGE_ATTR_STP]))
401 cfg->stp = blobmsg_get_bool(cur);
403 if ((cur = tb[BRIDGE_ATTR_FORWARD_DELAY]))
404 cfg->forward_delay = blobmsg_get_u32(cur);
406 if ((cur = tb[BRIDGE_ATTR_IGMP_SNOOP]))
407 cfg->igmp_snoop = blobmsg_get_bool(cur);
409 if ((cur = tb[BRIDGE_ATTR_AGEING_TIME])) {
410 cfg->ageing_time = blobmsg_get_u32(cur);
411 cfg->flags |= BRIDGE_OPT_AGEING_TIME;
414 if ((cur = tb[BRIDGE_ATTR_HELLO_TIME])) {
415 cfg->hello_time = blobmsg_get_u32(cur);
416 cfg->flags |= BRIDGE_OPT_HELLO_TIME;
419 if ((cur = tb[BRIDGE_ATTR_MAX_AGE])) {
420 cfg->max_age = blobmsg_get_u32(cur);
421 cfg->flags |= BRIDGE_OPT_MAX_AGE;
426 bridge_reload(struct device *dev, struct blob_attr *attr)
428 struct blob_attr *tb_dev[__DEV_ATTR_MAX];
429 struct blob_attr *tb_br[__BRIDGE_ATTR_MAX];
430 enum dev_change_type ret = DEV_CONFIG_APPLIED;
432 struct bridge_state *bst;
434 BUILD_BUG_ON(sizeof(diff) < __BRIDGE_ATTR_MAX / 8);
435 BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
437 bst = container_of(dev, struct bridge_state, dev);
439 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, tb_dev,
440 blob_data(attr), blob_len(attr));
441 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, tb_br,
442 blob_data(attr), blob_len(attr));
444 bst->ifnames = tb_br[BRIDGE_ATTR_IFNAME];
445 device_init_settings(dev, tb_dev);
446 bridge_apply_settings(bst, tb_br);
448 if (bst->config_data) {
449 struct blob_attr *otb_dev[__DEV_ATTR_MAX];
450 struct blob_attr *otb_br[__BRIDGE_ATTR_MAX];
452 blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb_dev,
453 blob_data(bst->config_data), blob_len(bst->config_data));
456 config_diff(tb_dev, otb_dev, &device_attr_list, &diff);
457 if (diff & ~(1 << DEV_ATTR_IFNAME))
458 ret = DEV_CONFIG_RESTART;
460 blobmsg_parse(bridge_attrs, __BRIDGE_ATTR_MAX, otb_br,
461 blob_data(bst->config_data), blob_len(bst->config_data));
464 config_diff(tb_br, otb_br, &bridge_attr_list, &diff);
465 if (diff & ~(1 << BRIDGE_ATTR_IFNAME))
466 ret = DEV_CONFIG_RESTART;
468 bridge_config_init(dev);
471 bst->config_data = attr;
475 static struct device *
476 bridge_create(const char *name, struct blob_attr *attr)
478 struct bridge_state *bst;
479 struct device *dev = NULL;
481 bst = calloc(1, sizeof(*bst));
486 device_init(dev, &bridge_device_type, name);
487 dev->config_pending = true;
489 bst->set_state = dev->set_state;
490 dev->set_state = bridge_set_state;
492 dev->hotplug_ops = &bridge_ops;
494 vlist_init(&bst->members, avl_strcmp, bridge_member_update);
495 bst->members.keep_old = true;
496 bridge_reload(dev, attr);