2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <net/ethernet.h>
24 #include <netinet/ether.h>
31 static struct avl_tree devices;
33 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
34 [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING },
35 [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY },
36 [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 },
37 [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING },
38 [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 },
39 [DEV_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_BOOL },
42 const struct config_param_list device_attr_list = {
43 .n_params = __DEV_ATTR_MAX,
47 static int __devlock = 0;
49 void device_lock(void)
54 void device_unlock(void)
58 device_free_unused(NULL);
61 static int set_device_state(struct device *dev, bool state)
72 simple_device_set_state(struct device *dev, bool state)
77 pdev = dev->parent.dev;
79 pdev = system_if_get_parent(dev);
81 device_add_user(&dev->parent, pdev);
86 ret = device_claim(&dev->parent);
88 device_release(&dev->parent);
93 return set_device_state(dev, state);
96 static struct device *
97 simple_device_create(const char *name, struct blob_attr *attr)
99 struct blob_attr *tb[__DEV_ATTR_MAX];
100 struct device *dev = NULL;
102 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
103 dev = device_get(name, true);
107 dev->set_state = simple_device_set_state;
108 device_init_settings(dev, tb);
113 static void simple_device_free(struct device *dev)
116 device_remove_user(&dev->parent);
120 const struct device_type simple_device_type = {
121 .name = "Network device",
122 .config_params = &device_attr_list,
124 .create = simple_device_create,
125 .check_state = system_if_check,
126 .free = simple_device_free,
130 device_merge_settings(struct device *dev, struct device_settings *n)
132 struct device_settings *os = &dev->orig_settings;
133 struct device_settings *s = &dev->settings;
135 memset(n, 0, sizeof(*n));
136 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
137 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
138 s->txqueuelen : os->txqueuelen;
140 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
142 n->flags = s->flags | os->flags;
146 device_init_settings(struct device *dev, struct blob_attr **tb)
148 struct device_settings *s = &dev->settings;
149 struct blob_attr *cur;
150 struct ether_addr *ea;
151 bool disabled = false;
154 if ((cur = tb[DEV_ATTR_ENABLED]))
155 disabled = !blobmsg_get_bool(cur);
157 if ((cur = tb[DEV_ATTR_MTU])) {
158 s->mtu = blobmsg_get_u32(cur);
159 s->flags |= DEV_OPT_MTU;
162 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
163 s->txqueuelen = blobmsg_get_u32(cur);
164 s->flags |= DEV_OPT_TXQUEUELEN;
167 if ((cur = tb[DEV_ATTR_MACADDR])) {
168 ea = ether_aton(blobmsg_data(cur));
170 memcpy(s->macaddr, ea, 6);
171 s->flags |= DEV_OPT_MACADDR;
175 device_set_disabled(dev, disabled);
178 static void __init dev_init(void)
180 avl_init(&devices, avl_strcmp, true, NULL);
184 static void __device_broadcast_event(struct list_head *head, enum device_event ev)
186 struct device_user *dep, *tmp;
188 list_for_each_entry_safe(dep, tmp, head, list) {
196 void device_broadcast_event(struct device *dev, enum device_event ev)
198 __device_broadcast_event(&dev->aliases, ev);
199 __device_broadcast_event(&dev->users, ev);
202 int device_claim(struct device_user *dep)
204 struct device *dev = dep->dev;
211 D(DEVICE, "Claim %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active + 1);
212 if (++dev->active != 1)
215 device_broadcast_event(dev, DEV_EVENT_SETUP);
216 ret = dev->set_state(dev, true);
218 device_broadcast_event(dev, DEV_EVENT_UP);
220 D(DEVICE, "claim device %s failed: %d\n", dev->ifname, ret);
222 dep->claimed = false;
228 void device_release(struct device_user *dep)
230 struct device *dev = dep->dev;
235 dep->claimed = false;
237 D(DEVICE, "Release %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active);
238 assert(dev->active >= 0);
243 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
245 dev->set_state(dev, false);
246 device_broadcast_event(dev, DEV_EVENT_DOWN);
249 int device_check_state(struct device *dev)
251 if (!dev->type->check_state)
254 return dev->type->check_state(dev);
257 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
263 strncpy(dev->ifname, name, IFNAMSIZ);
265 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
266 INIT_LIST_HEAD(&dev->users);
267 INIT_LIST_HEAD(&dev->aliases);
271 dev->set_state = set_device_state;
274 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
278 device_init_virtual(dev, type, ifname);
280 dev->avl.key = dev->ifname;
282 ret = avl_insert(&devices, &dev->avl);
286 system_if_clear_state(dev);
287 device_check_state(dev);
292 static struct device *
293 device_create_default(const char *name, bool external)
297 if (!external && system_if_force_external(name))
300 D(DEVICE, "Create simple device '%s'\n", name);
301 dev = calloc(1, sizeof(*dev));
302 dev->external = external;
303 dev->set_state = simple_device_set_state;
304 device_init(dev, &simple_device_type, name);
305 dev->default_config = true;
310 device_get(const char *name, int create)
314 if (strchr(name, '.'))
315 return get_vlan_device_chain(name, create);
318 return device_alias_get(name + 1);
320 dev = avl_find_element(&devices, name, dev, avl);
327 return device_create_default(name, create > 1);
331 device_delete(struct device *dev)
336 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
337 avl_delete(&devices, &dev->avl);
341 void device_cleanup(struct device *dev)
343 struct device_user *dep, *tmp;
345 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
346 list_for_each_entry_safe(dep, tmp, &dev->users, list) {
350 dep->cb(dep, DEV_EVENT_REMOVE);
357 static void __device_set_present(struct device *dev, bool state)
359 if (dev->present == state)
362 dev->present = state;
363 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
367 device_refresh_present(struct device *dev)
369 bool state = dev->sys_present;
371 if (dev->disabled || dev->deferred)
374 __device_set_present(dev, state);
377 void device_set_present(struct device *dev, bool state)
379 if (dev->sys_present == state)
382 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
383 dev->sys_present = state;
384 device_refresh_present(dev);
387 void device_add_user(struct device_user *dep, struct device *dev)
389 struct list_head *head;
392 device_remove_user(dep);
400 head = &dev->aliases;
403 list_add_tail(&dep->list, head);
405 if (dep->cb && dev->present) {
406 dep->cb(dep, DEV_EVENT_ADD);
408 dep->cb(dep, DEV_EVENT_UP);
413 device_free(struct device *dev)
418 dev->type->free(dev);
423 __device_free_unused(struct device *dev)
425 if (!list_empty(&dev->users) || dev->current_config || __devlock)
431 void device_remove_user(struct device_user *dep)
433 struct device *dev = dep->dev;
438 dep->hotplug = false;
442 list_del(&dep->list);
444 __device_free_unused(dev);
448 device_free_unused(struct device *dev)
453 return __device_free_unused(dev);
455 avl_for_each_element_safe(&devices, dev, avl, tmp)
456 __device_free_unused(dev);
460 device_init_pending(void)
462 struct device *dev, *tmp;
464 avl_for_each_element_safe(&devices, dev, avl, tmp) {
465 if (!dev->config_pending)
468 dev->type->config_init(dev);
469 dev->config_pending = false;
473 static enum dev_change_type
474 device_reload_config(struct device *dev, struct blob_attr *attr)
476 struct blob_attr *tb[__DEV_ATTR_MAX];
477 const struct config_param_list *cfg = dev->type->config_params;
479 if (config_check_equal(dev->config, attr, cfg))
480 return DEV_CONFIG_NO_CHANGE;
482 if (cfg == &device_attr_list) {
483 memset(tb, 0, sizeof(tb));
486 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
487 blob_data(attr), blob_len(attr));
489 device_init_settings(dev, tb);
490 return DEV_CONFIG_RESTART;
492 return DEV_CONFIG_RECREATE;
496 device_set_config(struct device *dev, const struct device_type *type,
497 struct blob_attr *attr)
499 if (type != dev->type)
500 return DEV_CONFIG_RECREATE;
502 if (dev->type->reload)
503 return dev->type->reload(dev, attr);
505 return device_reload_config(dev, attr);
509 device_replace(struct device *dev, struct device *odev)
511 struct device_user *dep, *tmp;
512 bool present = odev->present;
515 device_set_present(odev, false);
517 list_for_each_entry_safe(dep, tmp, &odev->users, list) {
519 list_move_tail(&dep->list, &dev->users);
525 device_set_present(dev, true);
529 device_reset_config(void)
533 avl_for_each_element(&devices, dev, avl)
534 dev->current_config = false;
538 device_reset_old(void)
540 struct device *dev, *tmp, *ndev;
542 avl_for_each_element_safe(&devices, dev, avl, tmp) {
543 if (dev->current_config || dev->default_config)
546 if (dev->type != &simple_device_type)
549 ndev = device_create_default(dev->ifname, dev->external);
550 device_replace(ndev, dev);
555 device_create(const char *name, const struct device_type *type,
556 struct blob_attr *config)
558 struct device *odev = NULL, *dev;
559 enum dev_change_type change;
561 config = config_memdup(config);
565 odev = device_get(name, false);
567 odev->current_config = true;
568 change = device_set_config(odev, type, config);
570 case DEV_CONFIG_RESTART:
571 case DEV_CONFIG_APPLIED:
572 D(DEVICE, "Device '%s': config applied\n", odev->ifname);
574 odev->config = config;
575 if (change == DEV_CONFIG_RESTART && odev->present) {
576 device_set_present(odev, false);
577 device_set_present(odev, true);
580 case DEV_CONFIG_NO_CHANGE:
581 D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
584 case DEV_CONFIG_RECREATE:
585 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
590 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
592 dev = type->create(name, config);
596 dev->current_config = true;
597 dev->config = config;
599 device_replace(dev, odev);
601 if (!config_init && dev->config_pending)
602 type->config_init(dev);
608 device_dump_status(struct blob_buf *b, struct device *dev)
610 struct device_settings st;
614 avl_for_each_element(&devices, dev, avl) {
617 c = blobmsg_open_table(b, dev->ifname);
618 device_dump_status(b, dev);
619 blobmsg_close_table(b, c);
625 blobmsg_add_u8(b, "external", dev->external);
626 blobmsg_add_u8(b, "present", dev->present);
627 blobmsg_add_string(b, "type", dev->type->name);
632 blobmsg_add_u8(b, "up", !!dev->active);
633 if (dev->type->dump_info)
634 dev->type->dump_info(dev, b);
636 system_if_dump_info(dev, b);
639 device_merge_settings(dev, &st);
640 if (st.flags & DEV_OPT_MTU)
641 blobmsg_add_u32(b, "mtu", st.mtu);
642 if (st.flags & DEV_OPT_MACADDR)
643 blobmsg_add_string(b, "macaddr", ether_ntoa((struct ether_addr *) st.macaddr));
644 if (st.flags & DEV_OPT_TXQUEUELEN)
645 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
648 s = blobmsg_open_table(b, "statistics");
649 if (dev->type->dump_stats)
650 dev->type->dump_stats(dev, b);
652 system_if_dump_stats(dev, b);
653 blobmsg_close_table(b, s);