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)
76 simple_device_set_state(struct device *dev, bool state)
81 pdev = dev->parent.dev;
83 pdev = system_if_get_parent(dev);
85 device_add_user(&dev->parent, pdev);
90 ret = device_claim(&dev->parent);
92 device_release(&dev->parent);
97 return set_device_state(dev, state);
100 static struct device *
101 simple_device_create(const char *name, struct blob_attr *attr)
103 struct blob_attr *tb[__DEV_ATTR_MAX];
104 struct device *dev = NULL;
106 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
107 dev = device_get(name, true);
111 dev->set_state = simple_device_set_state;
112 device_init_settings(dev, tb);
117 static void simple_device_free(struct device *dev)
120 device_remove_user(&dev->parent);
124 const struct device_type simple_device_type = {
125 .name = "Network device",
126 .config_params = &device_attr_list,
128 .create = simple_device_create,
129 .check_state = system_if_check,
130 .free = simple_device_free,
134 device_merge_settings(struct device *dev, struct device_settings *n)
136 struct device_settings *os = &dev->orig_settings;
137 struct device_settings *s = &dev->settings;
139 memset(n, 0, sizeof(*n));
140 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
141 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
142 s->txqueuelen : os->txqueuelen;
144 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
146 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
147 n->flags = s->flags | os->flags;
151 device_init_settings(struct device *dev, struct blob_attr **tb)
153 struct device_settings *s = &dev->settings;
154 struct blob_attr *cur;
155 struct ether_addr *ea;
156 bool disabled = false;
159 if ((cur = tb[DEV_ATTR_ENABLED]))
160 disabled = !blobmsg_get_bool(cur);
162 if ((cur = tb[DEV_ATTR_MTU])) {
163 s->mtu = blobmsg_get_u32(cur);
164 s->flags |= DEV_OPT_MTU;
167 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
168 s->txqueuelen = blobmsg_get_u32(cur);
169 s->flags |= DEV_OPT_TXQUEUELEN;
172 if ((cur = tb[DEV_ATTR_MACADDR])) {
173 ea = ether_aton(blobmsg_data(cur));
175 memcpy(s->macaddr, ea, 6);
176 s->flags |= DEV_OPT_MACADDR;
180 if ((cur = tb[DEV_ATTR_IPV6])) {
181 s->ipv6 = blobmsg_get_bool(cur);
182 s->flags |= DEV_OPT_IPV6;
185 device_set_disabled(dev, disabled);
188 static void __init dev_init(void)
190 avl_init(&devices, avl_strcmp, true, NULL);
193 static int device_broadcast_cb(void *ctx, struct safe_list *list)
195 struct device_user *dep = container_of(list, struct device_user, list);
198 /* device might have been removed by an earlier callback */
207 void device_broadcast_event(struct device *dev, enum device_event ev)
211 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
212 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
215 int device_claim(struct device_user *dep)
217 struct device *dev = dep->dev;
224 D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
225 if (++dev->active != 1)
228 device_broadcast_event(dev, DEV_EVENT_SETUP);
229 ret = dev->set_state(dev, true);
231 device_broadcast_event(dev, DEV_EVENT_UP);
233 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
235 dep->claimed = false;
241 void device_release(struct device_user *dep)
243 struct device *dev = dep->dev;
248 dep->claimed = false;
250 D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
251 assert(dev->active >= 0);
256 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
258 dev->set_state(dev, false);
259 device_broadcast_event(dev, DEV_EVENT_DOWN);
262 int device_check_state(struct device *dev)
264 if (!dev->type->check_state)
265 return simple_device_type.check_state(dev);
267 return dev->type->check_state(dev);
270 void device_init_virtual(struct device *dev, const struct device_type *type, const char *name)
276 strncpy(dev->ifname, name, IFNAMSIZ);
278 D(DEVICE, "Initialize device '%s'\n", dev->ifname);
279 INIT_SAFE_LIST(&dev->users);
280 INIT_SAFE_LIST(&dev->aliases);
284 dev->set_state = set_device_state;
287 int device_init(struct device *dev, const struct device_type *type, const char *ifname)
291 device_init_virtual(dev, type, ifname);
293 dev->avl.key = dev->ifname;
295 ret = avl_insert(&devices, &dev->avl);
299 system_if_clear_state(dev);
300 device_check_state(dev);
305 static struct device *
306 device_create_default(const char *name, bool external)
310 if (!external && system_if_force_external(name))
313 D(DEVICE, "Create simple device '%s'\n", name);
314 dev = calloc(1, sizeof(*dev));
315 dev->external = external;
316 dev->set_state = simple_device_set_state;
317 device_init(dev, &simple_device_type, name);
318 dev->default_config = true;
323 device_get(const char *name, int create)
327 if (strchr(name, '.'))
328 return get_vlan_device_chain(name, create);
331 return device_alias_get(name + 1);
333 dev = avl_find_element(&devices, name, dev, avl);
335 if (create > 1 && !dev->external) {
336 dev->external = true;
337 device_set_present(dev, true);
345 return device_create_default(name, create > 1);
349 device_delete(struct device *dev)
354 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
355 avl_delete(&devices, &dev->avl);
359 static int device_cleanup_cb(void *ctx, struct safe_list *list)
361 struct device_user *dep = container_of(list, struct device_user, list);
363 dep->cb(dep, DEV_EVENT_REMOVE);
369 void device_cleanup(struct device *dev)
371 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
372 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
373 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
377 static void __device_set_present(struct device *dev, bool state)
379 if (dev->present == state)
382 dev->present = state;
383 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
387 device_refresh_present(struct device *dev)
389 bool state = dev->sys_present;
391 if (dev->disabled || dev->deferred)
394 __device_set_present(dev, state);
397 void device_set_present(struct device *dev, bool state)
399 if (dev->sys_present == state)
402 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
403 dev->sys_present = state;
404 device_refresh_present(dev);
407 void device_set_link(struct device *dev, bool state)
409 if (dev->link_active == state)
412 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
414 dev->link_active = state;
415 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
418 void device_set_ifindex(struct device *dev, int ifindex)
420 if (dev->ifindex == ifindex)
423 dev->ifindex = ifindex;
424 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
427 static int device_refcount(struct device *dev)
429 struct list_head *list;
432 list_for_each(list, &dev->users.list)
435 list_for_each(list, &dev->aliases.list)
441 void device_add_user(struct device_user *dep, struct device *dev)
443 struct safe_list *head;
449 device_remove_user(dep);
457 head = &dev->aliases;
461 safe_list_add(&dep->list, head);
462 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
464 if (dep->cb && dev->present) {
465 dep->cb(dep, DEV_EVENT_ADD);
467 dep->cb(dep, DEV_EVENT_UP);
469 if (dev->link_active)
470 dep->cb(dep, DEV_EVENT_LINK_UP);
475 device_free(struct device *dev)
480 dev->type->free(dev);
485 __device_free_unused(struct device *dev)
487 if (!safe_list_empty(&dev->users) ||
488 !safe_list_empty(&dev->aliases) ||
489 dev->current_config || __devlock)
495 void device_remove_user(struct device_user *dep)
497 struct device *dev = dep->dev;
502 dep->hotplug = false;
506 safe_list_del(&dep->list);
508 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
509 __device_free_unused(dev);
513 device_free_unused(struct device *dev)
518 return __device_free_unused(dev);
520 avl_for_each_element_safe(&devices, dev, avl, tmp)
521 __device_free_unused(dev);
525 device_init_pending(void)
527 struct device *dev, *tmp;
529 avl_for_each_element_safe(&devices, dev, avl, tmp) {
530 if (!dev->config_pending)
533 dev->type->config_init(dev);
534 dev->config_pending = false;
538 static enum dev_change_type
539 device_reload_config(struct device *dev, struct blob_attr *attr)
541 struct blob_attr *tb[__DEV_ATTR_MAX];
542 const struct uci_blob_param_list *cfg = dev->type->config_params;
544 if (uci_blob_check_equal(dev->config, attr, cfg))
545 return DEV_CONFIG_NO_CHANGE;
547 if (cfg == &device_attr_list) {
548 memset(tb, 0, sizeof(tb));
551 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
552 blob_data(attr), blob_len(attr));
554 device_init_settings(dev, tb);
555 return DEV_CONFIG_RESTART;
557 return DEV_CONFIG_RECREATE;
561 device_set_config(struct device *dev, const struct device_type *type,
562 struct blob_attr *attr)
564 if (type != dev->type)
565 return DEV_CONFIG_RECREATE;
567 if (dev->type->reload)
568 return dev->type->reload(dev, attr);
570 return device_reload_config(dev, attr);
574 device_replace(struct device *dev, struct device *odev)
576 struct device_user *dep, *tmp;
577 bool present = odev->present;
580 device_set_present(odev, false);
582 list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
584 safe_list_del(&dep->list);
585 safe_list_add(&dep->list, &dev->users);
591 device_set_present(dev, true);
595 device_reset_config(void)
599 avl_for_each_element(&devices, dev, avl)
600 dev->current_config = false;
604 device_reset_old(void)
606 struct device *dev, *tmp, *ndev;
608 avl_for_each_element_safe(&devices, dev, avl, tmp) {
609 if (dev->current_config || dev->default_config)
612 if (dev->type != &simple_device_type)
615 ndev = device_create_default(dev->ifname, dev->external);
616 device_replace(ndev, dev);
621 device_create(const char *name, const struct device_type *type,
622 struct blob_attr *config)
624 struct device *odev = NULL, *dev;
625 enum dev_change_type change;
627 config = blob_memdup(config);
631 odev = device_get(name, false);
633 odev->current_config = true;
634 change = device_set_config(odev, type, config);
635 if (odev->external) {
636 system_if_apply_settings(odev, &odev->settings, odev->settings.flags);
637 change = DEV_CONFIG_APPLIED;
640 case DEV_CONFIG_RESTART:
641 case DEV_CONFIG_APPLIED:
642 D(DEVICE, "Device '%s': config applied\n", odev->ifname);
644 odev->config = config;
645 if (change == DEV_CONFIG_RESTART && odev->present) {
646 device_set_present(odev, false);
647 device_set_present(odev, true);
650 case DEV_CONFIG_NO_CHANGE:
651 D(DEVICE, "Device '%s': no configuration change\n", odev->ifname);
654 case DEV_CONFIG_RECREATE:
655 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
660 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
662 dev = type->create(name, config);
666 dev->current_config = true;
667 dev->config = config;
669 device_replace(dev, odev);
671 if (!config_init && dev->config_pending)
672 type->config_init(dev);
678 device_dump_status(struct blob_buf *b, struct device *dev)
680 struct device_settings st;
684 avl_for_each_element(&devices, dev, avl) {
687 c = blobmsg_open_table(b, dev->ifname);
688 device_dump_status(b, dev);
689 blobmsg_close_table(b, c);
695 blobmsg_add_u8(b, "external", dev->external);
696 blobmsg_add_u8(b, "present", dev->present);
697 blobmsg_add_string(b, "type", dev->type->name);
702 blobmsg_add_u8(b, "up", !!dev->active);
703 blobmsg_add_u8(b, "carrier", !!dev->link_active);
705 if (dev->type->dump_info)
706 dev->type->dump_info(dev, b);
708 system_if_dump_info(dev, b);
711 device_merge_settings(dev, &st);
712 if (st.flags & DEV_OPT_MTU)
713 blobmsg_add_u32(b, "mtu", st.mtu);
714 if (st.flags & DEV_OPT_MACADDR)
715 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
716 if (st.flags & DEV_OPT_TXQUEUELEN)
717 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
718 if (st.flags & DEV_OPT_IPV6)
719 blobmsg_add_u8(b, "ipv6", st.ipv6);
722 s = blobmsg_open_table(b, "statistics");
723 if (dev->type->dump_stats)
724 dev->type->dump_stats(dev, b);
726 system_if_dump_stats(dev, b);
727 blobmsg_close_table(b, s);