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] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
35 [DEV_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_ARRAY },
36 [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
37 [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
38 [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
39 [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
40 [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
43 const struct uci_blob_param_list device_attr_list = {
44 .n_params = __DEV_ATTR_MAX,
48 static int __devlock = 0;
50 void device_lock(void)
55 void device_unlock(void)
59 device_free_unused(NULL);
62 static int set_device_state(struct device *dev, bool state)
73 simple_device_set_state(struct device *dev, bool state)
78 pdev = dev->parent.dev;
80 pdev = system_if_get_parent(dev);
82 device_add_user(&dev->parent, pdev);
87 ret = device_claim(&dev->parent);
89 device_release(&dev->parent);
94 return set_device_state(dev, state);
97 static struct device *
98 simple_device_create(const char *name, struct blob_attr *attr)
100 struct blob_attr *tb[__DEV_ATTR_MAX];
101 struct device *dev = NULL;
103 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
104 dev = device_get(name, true);
108 dev->set_state = simple_device_set_state;
109 device_init_settings(dev, tb);
114 static void simple_device_free(struct device *dev)
117 device_remove_user(&dev->parent);
121 const struct device_type simple_device_type = {
122 .name = "Network device",
123 .config_params = &device_attr_list,
125 .create = simple_device_create,
126 .check_state = system_if_check,
127 .free = simple_device_free,
131 device_merge_settings(struct device *dev, struct device_settings *n)
133 struct device_settings *os = &dev->orig_settings;
134 struct device_settings *s = &dev->settings;
136 memset(n, 0, sizeof(*n));
137 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
138 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
139 s->txqueuelen : os->txqueuelen;
141 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
143 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
144 n->flags = s->flags | os->flags;
148 device_init_settings(struct device *dev, struct blob_attr **tb)
150 struct device_settings *s = &dev->settings;
151 struct blob_attr *cur;
152 struct ether_addr *ea;
153 bool disabled = false;
156 if ((cur = tb[DEV_ATTR_ENABLED]))
157 disabled = !blobmsg_get_bool(cur);
159 if ((cur = tb[DEV_ATTR_MTU])) {
160 s->mtu = blobmsg_get_u32(cur);
161 s->flags |= DEV_OPT_MTU;
164 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
165 s->txqueuelen = blobmsg_get_u32(cur);
166 s->flags |= DEV_OPT_TXQUEUELEN;
169 if ((cur = tb[DEV_ATTR_MACADDR])) {
170 ea = ether_aton(blobmsg_data(cur));
172 memcpy(s->macaddr, ea, 6);
173 s->flags |= DEV_OPT_MACADDR;
177 if ((cur = tb[DEV_ATTR_IPV6])) {
178 s->ipv6 = blobmsg_get_bool(cur);
179 s->flags |= DEV_OPT_IPV6;
182 device_set_disabled(dev, disabled);
185 static void __init dev_init(void)
187 avl_init(&devices, avl_strcmp, true, NULL);
190 static int device_broadcast_cb(void *ctx, struct safe_list *list)
192 struct device_user *dep = container_of(list, struct device_user, list);
195 /* device might have been removed by an earlier callback */
204 void device_broadcast_event(struct device *dev, enum device_event ev)
208 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
209 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
212 int device_claim(struct device_user *dep)
214 struct device *dev = dep->dev;
221 D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
222 if (++dev->active != 1)
225 device_broadcast_event(dev, DEV_EVENT_SETUP);
226 ret = dev->set_state(dev, true);
228 device_broadcast_event(dev, DEV_EVENT_UP);
230 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
232 dep->claimed = false;
238 void device_release(struct device_user *dep)
240 struct device *dev = dep->dev;
245 dep->claimed = false;
247 D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
248 assert(dev->active >= 0);
253 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
255 dev->set_state(dev, false);
256 device_broadcast_event(dev, DEV_EVENT_DOWN);
259 int device_check_state(struct device *dev)
261 if (!dev->type->check_state)
264 return dev->type->check_state(dev);
267 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
273 strncpy(dev->ifname, name, IFNAMSIZ);
275 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
276 INIT_SAFE_LIST(&dev->users);
277 INIT_SAFE_LIST(&dev->aliases);
281 dev->set_state = set_device_state;
284 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
288 device_init_virtual(dev, type, ifname);
290 dev->avl.key = dev->ifname;
292 ret = avl_insert(&devices, &dev->avl);
296 system_if_clear_state(dev);
297 device_check_state(dev);
302 static struct device *
303 device_create_default(const char *name, bool external)
307 if (!external && system_if_force_external(name))
310 D(DEVICE, "Create simple device '%s'\n", name);
311 dev = calloc(1, sizeof(*dev));
312 dev->external = external;
313 dev->set_state = simple_device_set_state;
314 device_init(dev, &simple_device_type, name);
315 dev->default_config = true;
320 device_get(const char *name, int create)
324 if (strchr(name, '.'))
325 return get_vlan_device_chain(name, create);
328 return device_alias_get(name + 1);
330 dev = avl_find_element(&devices, name, dev, avl);
332 if (create > 1 && !dev->external) {
333 dev->external = true;
334 device_set_present(dev, true);
342 return device_create_default(name, create > 1);
346 device_delete(struct device *dev)
351 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
352 avl_delete(&devices, &dev->avl);
356 static int device_cleanup_cb(void *ctx, struct safe_list *list)
358 struct device_user *dep = container_of(list, struct device_user, list);
360 dep->cb(dep, DEV_EVENT_REMOVE);
366 void device_cleanup(struct device *dev)
368 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
369 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
370 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
374 static void __device_set_present(struct device *dev, bool state)
376 if (dev->present == state)
379 dev->present = state;
380 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
384 device_refresh_present(struct device *dev)
386 bool state = dev->sys_present;
388 if (dev->disabled || dev->deferred)
391 __device_set_present(dev, state);
394 void device_set_present(struct device *dev, bool state)
396 if (dev->sys_present == state)
399 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
400 dev->sys_present = state;
401 device_refresh_present(dev);
404 void device_set_link(struct device *dev, bool state)
406 if (dev->link_active == state)
409 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
411 dev->link_active = state;
412 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
415 void device_set_ifindex(struct device *dev, int ifindex)
417 if (dev->ifindex == ifindex)
420 dev->ifindex = ifindex;
421 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
424 static int device_refcount(struct device *dev)
426 struct list_head *list;
429 list_for_each(list, &dev->users.list)
432 list_for_each(list, &dev->aliases.list)
438 void device_add_user(struct device_user *dep, struct device *dev)
440 struct safe_list *head;
446 device_remove_user(dep);
454 head = &dev->aliases;
458 safe_list_add(&dep->list, head);
459 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
461 if (dep->cb && dev->present) {
462 dep->cb(dep, DEV_EVENT_ADD);
464 dep->cb(dep, DEV_EVENT_UP);
466 if (dev->link_active)
467 dep->cb(dep, DEV_EVENT_LINK_UP);
472 device_free(struct device *dev)
477 dev->type->free(dev);
482 __device_free_unused(struct device *dev)
484 if (!safe_list_empty(&dev->users) ||
485 !safe_list_empty(&dev->aliases) ||
486 dev->current_config || __devlock)
492 void device_remove_user(struct device_user *dep)
494 struct device *dev = dep->dev;
499 dep->hotplug = false;
503 safe_list_del(&dep->list);
505 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
506 __device_free_unused(dev);
510 device_free_unused(struct device *dev)
515 return __device_free_unused(dev);
517 avl_for_each_element_safe(&devices, dev, avl, tmp)
518 __device_free_unused(dev);
522 device_init_pending(void)
524 struct device *dev, *tmp;
526 avl_for_each_element_safe(&devices, dev, avl, tmp) {
527 if (!dev->config_pending)
530 dev->type->config_init(dev);
531 dev->config_pending = false;
535 static enum dev_change_type
536 device_reload_config(struct device *dev, struct blob_attr *attr)
538 struct blob_attr *tb[__DEV_ATTR_MAX];
539 const struct uci_blob_param_list *cfg = dev->type->config_params;
541 if (uci_blob_check_equal(dev->config, attr, cfg))
542 return DEV_CONFIG_NO_CHANGE;
544 if (cfg == &device_attr_list) {
545 memset(tb, 0, sizeof(tb));
548 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
549 blob_data(attr), blob_len(attr));
551 device_init_settings(dev, tb);
552 return DEV_CONFIG_RESTART;
554 return DEV_CONFIG_RECREATE;
558 device_set_config(struct device *dev, const struct device_type *type,
559 struct blob_attr *attr)
561 if (type != dev->type)
562 return DEV_CONFIG_RECREATE;
564 if (dev->type->reload)
565 return dev->type->reload(dev, attr);
567 return device_reload_config(dev, attr);
571 device_replace(struct device *dev, struct device *odev)
573 struct device_user *dep, *tmp;
574 bool present = odev->present;
577 device_set_present(odev, false);
579 list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
581 safe_list_del(&dep->list);
582 safe_list_add(&dep->list, &dev->users);
588 device_set_present(dev, true);
592 device_reset_config(void)
596 avl_for_each_element(&devices, dev, avl)
597 dev->current_config = false;
601 device_reset_old(void)
603 struct device *dev, *tmp, *ndev;
605 avl_for_each_element_safe(&devices, dev, avl, tmp) {
606 if (dev->current_config || dev->default_config)
609 if (dev->type != &simple_device_type)
612 ndev = device_create_default(dev->ifname, dev->external);
613 device_replace(ndev, dev);
618 device_create(const char *name, const struct device_type *type,
619 struct blob_attr *config)
621 struct device *odev = NULL, *dev;
622 enum dev_change_type change;
624 config = blob_memdup(config);
628 odev = device_get(name, false);
630 odev->current_config = true;
631 change = device_set_config(odev, type, config);
632 if (odev->external) {
633 system_if_apply_settings(odev, &odev->settings, odev->settings.flags);
634 change = DEV_CONFIG_APPLIED;
637 case DEV_CONFIG_RESTART:
638 case DEV_CONFIG_APPLIED:
639 D(DEVICE, "Device '%s': config applied\n", odev->ifname);
641 odev->config = config;
642 if (change == DEV_CONFIG_RESTART && odev->present) {
643 device_set_present(odev, false);
644 device_set_present(odev, true);
647 case DEV_CONFIG_NO_CHANGE:
648 D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
651 case DEV_CONFIG_RECREATE:
652 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
657 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
659 dev = type->create(name, config);
663 dev->current_config = true;
664 dev->config = config;
666 device_replace(dev, odev);
668 if (!config_init && dev->config_pending)
669 type->config_init(dev);
675 device_dump_status(struct blob_buf *b, struct device *dev)
677 struct device_settings st;
681 avl_for_each_element(&devices, dev, avl) {
684 c = blobmsg_open_table(b, dev->ifname);
685 device_dump_status(b, dev);
686 blobmsg_close_table(b, c);
692 blobmsg_add_u8(b, "external", dev->external);
693 blobmsg_add_u8(b, "present", dev->present);
694 blobmsg_add_string(b, "type", dev->type->name);
699 blobmsg_add_u8(b, "up", !!dev->active);
700 blobmsg_add_u8(b, "carrier", !!dev->link_active);
702 if (dev->type->dump_info)
703 dev->type->dump_info(dev, b);
705 system_if_dump_info(dev, b);
708 device_merge_settings(dev, &st);
709 if (st.flags & DEV_OPT_MTU)
710 blobmsg_add_u32(b, "mtu", st.mtu);
711 if (st.flags & DEV_OPT_MACADDR)
712 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
713 if (st.flags & DEV_OPT_TXQUEUELEN)
714 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
715 if (st.flags & DEV_OPT_IPV6)
716 blobmsg_add_u8(b, "ipv6", st.ipv6);
719 s = blobmsg_open_table(b, "statistics");
720 if (dev->type->dump_stats)
721 dev->type->dump_stats(dev, b);
723 system_if_dump_stats(dev, b);
724 blobmsg_close_table(b, s);