7 #include <sys/socket.h>
8 #include <net/ethernet.h>
11 #include <netinet/ether.h>
18 static struct avl_tree devices;
20 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
21 [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
22 [DEV_ATTR_NAME] = { "name", BLOBMSG_TYPE_STRING },
23 [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
24 [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 },
25 [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
26 [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 },
29 const struct config_param_list device_attr_list = {
30 .n_params = __DEV_ATTR_MAX,
34 static struct device *
35 simple_device_create(struct blob_attr *attr)
37 struct blob_attr *tb[__DEV_ATTR_MAX];
38 struct device *dev = NULL;
41 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
42 if (!tb[DEV_ATTR_NAME])
45 name = blobmsg_data(tb[DEV_ATTR_NAME]);
49 dev = device_get(name, true);
53 device_init_settings(dev, tb);
58 static void simple_device_free(struct device *dev)
64 const struct device_type simple_device_type = {
65 .name = "Network device",
66 .config_params = &device_attr_list,
68 .create = simple_device_create,
69 .check_state = system_if_check,
70 .free = simple_device_free,
74 device_init_settings(struct device *dev, struct blob_attr **tb)
76 struct blob_attr *cur;
77 struct ether_addr *ea;
81 if ((cur = tb[DEV_ATTR_MTU])) {
82 dev->mtu = blobmsg_get_u32(cur);
83 dev->flags |= DEV_OPT_MTU;
86 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
87 dev->txqueuelen = blobmsg_get_u32(cur);
88 dev->flags |= DEV_OPT_TXQUEUELEN;
91 if ((cur = tb[DEV_ATTR_MACADDR])) {
92 ea = ether_aton(blob_data(cur));
94 memcpy(dev->macaddr, ea, sizeof(dev->macaddr));
95 dev->flags |= DEV_OPT_MACADDR;
100 static void __init dev_init(void)
102 avl_init(&devices, avl_strcmp, true, NULL);
105 static void device_broadcast_event(struct device *dev, enum device_event ev)
107 struct device_user *dep, *tmp;
109 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
117 static int set_device_state(struct device *dev, bool state)
127 int device_claim(struct device_user *dep)
129 struct device *dev = dep->dev;
136 D(DEVICE, "claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1);
137 if (++dev->active != 1)
140 device_broadcast_event(dev, DEV_EVENT_SETUP);
141 ret = dev->set_state(dev, true);
143 device_broadcast_event(dev, DEV_EVENT_UP);
145 D(DEVICE, "claim device %s failed: %d\n", dev->ifname, ret);
147 dep->claimed = false;
153 void device_release(struct device_user *dep)
155 struct device *dev = dep->dev;
160 dep->claimed = false;
162 D(DEVICE, "release device %s, new refcount: %d\n", dev->ifname, dev->active);
163 assert(dev->active >= 0);
168 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
169 dev->set_state(dev, false);
170 device_broadcast_event(dev, DEV_EVENT_DOWN);
173 int device_check_state(struct device *dev)
175 if (!dev->type->check_state)
178 return dev->type->check_state(dev);
181 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
187 strncpy(dev->ifname, name, IFNAMSIZ);
189 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
190 INIT_LIST_HEAD(&dev->users);
194 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
198 device_init_virtual(dev, type, ifname);
201 dev->set_state = set_device_state;
203 dev->avl.key = dev->ifname;
205 ret = avl_insert(&devices, &dev->avl);
209 device_check_state(dev);
214 static struct device *
215 device_create_default(const char *name)
219 D(DEVICE, "Create simple device '%s'\n", name);
220 dev = calloc(1, sizeof(*dev));
221 device_init(dev, &simple_device_type, name);
222 dev->default_config = true;
227 device_get(const char *name, bool create)
231 if (strchr(name, '.'))
232 return get_vlan_device_chain(name, create);
234 dev = avl_find_element(&devices, name, dev, avl);
241 return device_create_default(name);
245 device_delete(struct device *dev)
250 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
251 avl_delete(&devices, &dev->avl);
255 void device_cleanup(struct device *dev)
257 struct device_user *dep, *tmp;
259 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
260 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
264 dep->cb(dep, DEV_EVENT_REMOVE);
271 void device_set_present(struct device *dev, bool state)
273 if (dev->present == state)
276 D(DEVICE, "Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" );
277 dev->present = state;
278 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
281 void device_add_user(struct device_user *dep, struct device *dev)
284 list_add_tail(&dep->list, &dev->users);
285 if (dep->cb && dev->present) {
286 dep->cb(dep, DEV_EVENT_ADD);
288 dep->cb(dep, DEV_EVENT_UP);
293 __device_free_unused(struct device *dev)
295 if (!list_empty(&dev->users) || dev->current_config || config_init)
301 void device_remove_user(struct device_user *dep)
303 struct device *dev = dep->dev;
308 list_del(&dep->list);
310 __device_free_unused(dev);
314 device_free_unused(struct device *dev)
319 return __device_free_unused(dev);
321 avl_for_each_element_safe(&devices, dev, avl, tmp)
322 __device_free_unused(dev);
326 device_init_pending(void)
328 struct device *dev, *tmp;
330 avl_for_each_element_safe(&devices, dev, avl, tmp) {
331 if (!dev->config_pending)
334 dev->type->config_init(dev);
335 dev->config_pending = false;
340 device_reload_config(struct device *dev, struct blob_attr *attr)
342 struct blob_attr *tb[__DEV_ATTR_MAX];
343 const struct config_param_list *cfg = dev->type->config_params;
345 if (config_check_equal(dev->config, attr, cfg))
346 return DEV_CONFIG_NO_CHANGE;
348 if (cfg == &device_attr_list) {
349 memset(tb, 0, sizeof(tb));
352 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
353 blob_data(attr), blob_len(attr));
355 device_init_settings(dev, tb);
356 return DEV_CONFIG_APPLIED;
358 return DEV_CONFIG_RECREATE;
361 static enum dev_change_type
362 device_check_config(struct device *dev, const struct device_type *type,
363 struct blob_attr *attr)
365 if (type != dev->type)
366 return DEV_CONFIG_RECREATE;
368 if (dev->type->reload)
369 return dev->type->reload(dev, attr);
371 return device_reload_config(dev, attr);
375 device_replace(struct device *dev, struct device *odev)
377 struct device_user *dep, *tmp;
378 bool present = odev->present;
381 device_set_present(odev, false);
383 list_for_each_entry_safe(dep, tmp, &odev->users, list) {
385 list_move_tail(&dep->list, &dev->users);
391 device_set_present(dev, true);
395 device_reset_config(void)
399 avl_for_each_element(&devices, dev, avl)
400 dev->current_config = false;
404 device_reset_old(void)
406 struct device *dev, *tmp, *ndev;
408 avl_for_each_element_safe(&devices, dev, avl, tmp) {
409 if (dev->current_config || dev->default_config)
412 if (dev->type != &simple_device_type)
415 ndev = device_create_default(dev->ifname);
416 device_replace(ndev, dev);
421 device_create(const char *name, const struct device_type *type,
422 struct blob_attr *config)
424 struct device *odev = NULL, *dev;
425 enum dev_change_type change;
427 config = config_memdup(config);
431 odev = device_get(name, false);
433 odev->current_config = true;
434 change = device_check_config(odev, type, config);
436 case DEV_CONFIG_APPLIED:
437 D(DEVICE, "Device '%s': config applied\n", odev->ifname);
439 odev->config = config;
441 device_set_present(odev, false);
442 device_set_present(odev, true);
445 case DEV_CONFIG_NO_CHANGE:
446 D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
449 case DEV_CONFIG_RECREATE:
450 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
455 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
457 dev = type->create(config);
461 dev->current_config = true;
462 dev->config = config;
464 device_replace(dev, odev);
466 if (!config_init && dev->config_pending)
467 type->config_init(dev);