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 vlist_tree members
;
74 struct bridge_member
{
75 struct vlist_node node
;
76 struct bridge_state
*bst
;
77 struct device_user dev
;
83 bridge_disable_member(struct bridge_member
*bm
)
85 struct bridge_state
*bst
= bm
->bst
;
90 system_bridge_delif(&bst
->dev
, bm
->dev
.dev
);
91 device_release(&bm
->dev
);
97 bridge_enable_member(struct bridge_member
*bm
)
99 struct bridge_state
*bst
= bm
->bst
;
105 ret
= device_claim(&bm
->dev
);
109 ret
= system_bridge_addif(&bst
->dev
, bm
->dev
.dev
);
122 bridge_remove_member(struct bridge_member
*bm
)
124 struct bridge_state
*bst
= bm
->bst
;
130 bm
->bst
->n_present
--;
132 bridge_disable_member(bm
);
134 bst
->force_active
= false;
135 if (bst
->n_present
== 0)
136 device_set_present(&bst
->dev
, false);
140 bridge_member_cb(struct device_user
*dev
, enum device_event ev
)
142 struct bridge_member
*bm
= container_of(dev
, struct bridge_member
, dev
);
143 struct bridge_state
*bst
= bm
->bst
;
147 assert(!bm
->present
);
153 bridge_enable_member(bm
);
154 else if (bst
->n_present
== 1)
155 device_set_present(&bst
->dev
, true);
158 case DEV_EVENT_REMOVE
:
163 vlist_delete(&bst
->members
, &bm
->node
);
165 bridge_remove_member(bm
);
174 bridge_set_down(struct bridge_state
*bst
)
176 struct bridge_member
*bm
;
178 bst
->set_state(&bst
->dev
, false);
180 vlist_for_each_element(&bst
->members
, bm
, node
)
181 bridge_disable_member(bm
);
183 system_bridge_delbr(&bst
->dev
);
189 bridge_set_up(struct bridge_state
*bst
)
191 struct bridge_member
*bm
;
194 if (!bst
->force_active
&& !bst
->n_present
)
197 ret
= system_bridge_addbr(&bst
->dev
, &bst
->config
);
201 vlist_for_each_element(&bst
->members
, bm
, node
)
202 bridge_enable_member(bm
);
204 if (!bst
->force_active
&& !bst
->n_present
) {
205 /* initialization of all member interfaces failed */
206 system_bridge_delbr(&bst
->dev
);
207 device_set_present(&bst
->dev
, false);
211 ret
= bst
->set_state(&bst
->dev
, true);
213 bridge_set_down(bst
);
220 bridge_set_state(struct device
*dev
, bool up
)
222 struct bridge_state
*bst
;
224 bst
= container_of(dev
, struct bridge_state
, dev
);
227 return bridge_set_up(bst
);
229 return bridge_set_down(bst
);
232 static struct bridge_member
*
233 bridge_create_member(struct bridge_state
*bst
, struct device
*dev
, bool hotplug
)
235 struct bridge_member
*bm
;
237 bm
= calloc(1, sizeof(*bm
) + strlen(dev
->ifname
) + 1);
239 bm
->dev
.cb
= bridge_member_cb
;
240 bm
->dev
.hotplug
= hotplug
;
241 strcpy(bm
->name
, dev
->ifname
);
243 vlist_add(&bst
->members
, &bm
->node
, bm
->name
);
249 bridge_member_update(struct vlist_tree
*tree
, struct vlist_node
*node_new
,
250 struct vlist_node
*node_old
)
252 struct bridge_member
*bm
;
256 bm
= container_of(node_new
, struct bridge_member
, node
);
265 device_add_user(&bm
->dev
, dev
);
270 bm
= container_of(node_old
, struct bridge_member
, node
);
271 bridge_remove_member(bm
);
272 device_remove_user(&bm
->dev
);
279 bridge_add_member(struct bridge_state
*bst
, const char *name
)
283 dev
= device_get(name
, true);
287 bridge_create_member(bst
, dev
, false);
291 bridge_hotplug_add(struct device
*dev
, struct device
*member
)
293 struct bridge_state
*bst
= container_of(dev
, struct bridge_state
, dev
);
295 bridge_create_member(bst
, member
, true);
301 bridge_hotplug_del(struct device
*dev
, struct device
*member
)
303 struct bridge_state
*bst
= container_of(dev
, struct bridge_state
, dev
);
304 struct bridge_member
*bm
;
306 bm
= vlist_find(&bst
->members
, member
->ifname
, bm
, node
);
308 return UBUS_STATUS_NOT_FOUND
;
310 vlist_delete(&bst
->members
, &bm
->node
);
315 bridge_hotplug_prepare(struct device
*dev
)
317 struct bridge_state
*bst
;
319 bst
= container_of(dev
, struct bridge_state
, dev
);
320 bst
->force_active
= true;
321 device_set_present(&bst
->dev
, true);
326 static const struct device_hotplug_ops bridge_ops
= {
327 .prepare
= bridge_hotplug_prepare
,
328 .add
= bridge_hotplug_add
,
329 .del
= bridge_hotplug_del
333 bridge_free(struct device
*dev
)
335 struct bridge_state
*bst
;
338 bst
= container_of(dev
, struct bridge_state
, dev
);
339 vlist_flush_all(&bst
->members
);
344 bridge_dump_info(struct device
*dev
, struct blob_buf
*b
)
346 struct bridge_state
*bst
;
347 struct bridge_member
*bm
;
350 bst
= container_of(dev
, struct bridge_state
, dev
);
352 system_if_dump_info(dev
, b
);
353 list
= blobmsg_open_array(b
, "bridge-members");
355 vlist_for_each_element(&bst
->members
, bm
, node
)
356 blobmsg_add_string(b
, NULL
, bm
->dev
.dev
->ifname
);
358 blobmsg_close_array(b
, list
);
362 bridge_config_init(struct device
*dev
)
364 struct bridge_state
*bst
;
365 struct blob_attr
*cur
;
368 bst
= container_of(dev
, struct bridge_state
, dev
);
373 blobmsg_for_each_attr(cur
, bst
->ifnames
, rem
) {
374 bridge_add_member(bst
, blobmsg_data(cur
));
379 bridge_apply_settings(struct bridge_state
*bst
, struct blob_attr
**tb
)
381 struct bridge_config
*cfg
= &bst
->config
;
382 struct blob_attr
*cur
;
386 cfg
->forward_delay
= 1;
387 cfg
->igmp_snoop
= true;
389 if ((cur
= tb
[BRIDGE_ATTR_STP
]))
390 cfg
->stp
= blobmsg_get_bool(cur
);
392 if ((cur
= tb
[BRIDGE_ATTR_FORWARD_DELAY
]))
393 cfg
->forward_delay
= blobmsg_get_u32(cur
);
395 if ((cur
= tb
[BRIDGE_ATTR_IGMP_SNOOP
]))
396 cfg
->igmp_snoop
= blobmsg_get_bool(cur
);
398 if ((cur
= tb
[BRIDGE_ATTR_AGEING_TIME
])) {
399 cfg
->ageing_time
= blobmsg_get_u32(cur
);
400 cfg
->flags
|= BRIDGE_OPT_AGEING_TIME
;
403 if ((cur
= tb
[BRIDGE_ATTR_HELLO_TIME
])) {
404 cfg
->hello_time
= blobmsg_get_u32(cur
);
405 cfg
->flags
|= BRIDGE_OPT_HELLO_TIME
;
408 if ((cur
= tb
[BRIDGE_ATTR_MAX_AGE
])) {
409 cfg
->max_age
= blobmsg_get_u32(cur
);
410 cfg
->flags
|= BRIDGE_OPT_MAX_AGE
;
414 static struct device
*
415 bridge_create(const char *name
, struct blob_attr
*attr
)
417 struct blob_attr
*tb_dev
[__DEV_ATTR_MAX
];
418 struct blob_attr
*tb_br
[__BRIDGE_ATTR_MAX
];
419 struct bridge_state
*bst
;
420 struct device
*dev
= NULL
;
422 blobmsg_parse(device_attr_list
.params
, __DEV_ATTR_MAX
, tb_dev
,
423 blob_data(attr
), blob_len(attr
));
424 blobmsg_parse(bridge_attrs
, __BRIDGE_ATTR_MAX
, tb_br
,
425 blob_data(attr
), blob_len(attr
));
427 bst
= calloc(1, sizeof(*bst
));
432 device_init(dev
, &bridge_device_type
, name
);
433 device_init_settings(dev
, tb_dev
);
434 dev
->config_pending
= true;
435 bst
->ifnames
= tb_br
[BRIDGE_ATTR_IFNAME
];
436 bridge_apply_settings(bst
, tb_br
);
438 bst
->set_state
= dev
->set_state
;
439 dev
->set_state
= bridge_set_state
;
441 dev
->hotplug_ops
= &bridge_ops
;
443 vlist_init(&bst
->members
, avl_strcmp
, bridge_member_update
);
444 bst
->members
.keep_old
= true;