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>
27 #include <libubox/list.h>
33 static struct list_head devtypes = LIST_HEAD_INIT(devtypes);
34 static struct avl_tree devices;
35 static bool default_ps = true;
37 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
38 [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
39 [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
40 [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 },
41 [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
42 [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
43 [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
44 [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
45 [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL },
46 [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING },
47 [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL },
48 [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 },
49 [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 },
50 [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 },
51 [DEV_ATTR_NEIGHGCSTALETIME] = { .name = "neighgcstaletime", .type = BLOBMSG_TYPE_INT32 },
52 [DEV_ATTR_RPS] = { .name = "rps", .type = BLOBMSG_TYPE_BOOL },
53 [DEV_ATTR_XPS] = { .name = "xps", .type = BLOBMSG_TYPE_BOOL },
54 [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 },
55 [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
56 [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 },
57 [DEV_ATTR_MULTICAST_FAST_LEAVE] = { .name = "multicast_fast_leave", . type = BLOBMSG_TYPE_BOOL },
58 [DEV_ATTR_MULTICAST] = { .name ="multicast", .type = BLOBMSG_TYPE_BOOL },
59 [DEV_ATTR_LEARNING] = { .name ="learning", .type = BLOBMSG_TYPE_BOOL },
60 [DEV_ATTR_UNICAST_FLOOD] = { .name ="unicast_flood", .type = BLOBMSG_TYPE_BOOL },
63 const struct uci_blob_param_list device_attr_list = {
64 .n_params = __DEV_ATTR_MAX,
68 static int __devlock = 0;
70 int device_type_add(struct device_type *devtype)
72 if (device_type_get(devtype->name)) {
73 netifd_log_message(L_WARNING, "Device handler '%s' already exists\n",
78 netifd_log_message(L_NOTICE, "Added device handler type: %s\n",
81 list_add(&devtype->list, &devtypes);
85 /* initialize device type list and add known types */
86 static void __init devtypes_init(void)
88 device_type_add(&simple_device_type);
89 device_type_add(&bridge_device_type);
90 device_type_add(&tunnel_device_type);
91 device_type_add(&macvlan_device_type);
92 device_type_add(&vlandev_device_type);
95 /* Retrieve the device type for the given name. If 'bridge' is true, the type
96 * must have bridge capabilities
99 device_type_get(const char *tname)
101 struct device_type *cur;
103 list_for_each_entry(cur, &devtypes, list)
104 if (!strcmp(cur->name, tname))
110 void device_lock(void)
115 void device_unlock(void)
119 device_free_unused(NULL);
122 static int set_device_state(struct device *dev, bool state)
125 /* Get ifindex for all devices being enabled so a valid */
126 /* ifindex is in place avoiding possible race conditions */
127 device_set_ifindex(dev, system_if_resolve(dev));
140 simple_device_set_state(struct device *dev, bool state)
145 pdev = dev->parent.dev;
146 if (state && !pdev) {
147 pdev = system_if_get_parent(dev);
149 device_add_user(&dev->parent, pdev);
154 ret = device_claim(&dev->parent);
156 device_release(&dev->parent);
161 return set_device_state(dev, state);
164 static struct device *
165 simple_device_create(const char *name, struct device_type *devtype,
166 struct blob_attr *attr)
168 struct blob_attr *tb[__DEV_ATTR_MAX];
169 struct device *dev = NULL;
171 /* device type is unused for simple devices */
174 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
175 dev = device_get(name, true);
179 dev->set_state = simple_device_set_state;
180 device_init_settings(dev, tb);
185 static void simple_device_free(struct device *dev)
188 device_remove_user(&dev->parent);
192 struct device_type simple_device_type = {
193 .name = "Network device",
194 .config_params = &device_attr_list,
196 .create = simple_device_create,
197 .check_state = system_if_check,
198 .free = simple_device_free,
202 device_merge_settings(struct device *dev, struct device_settings *n)
204 struct device_settings *os = &dev->orig_settings;
205 struct device_settings *s = &dev->settings;
207 memset(n, 0, sizeof(*n));
208 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
209 n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6;
210 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
211 s->txqueuelen : os->txqueuelen;
213 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
215 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
216 n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc;
217 n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter;
218 n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal;
219 n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion;
220 n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion;
221 n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
222 s->neigh4reachabletime : os->neigh4reachabletime;
223 n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
224 s->neigh6reachabletime : os->neigh6reachabletime;
225 n->neigh4gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
226 s->neigh4gcstaletime : os->neigh4gcstaletime;
227 n->neigh6gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
228 s->neigh6gcstaletime : os->neigh6gcstaletime;
229 n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ?
230 s->dadtransmits : os->dadtransmits;
231 n->multicast = s->flags & DEV_OPT_MULTICAST ?
232 s->multicast : os->multicast;
233 n->multicast_to_unicast = s->multicast_to_unicast;
234 n->multicast_router = s->multicast_router;
235 n->multicast_fast_leave = s->multicast_fast_leave;
236 n->learning = s->learning;
237 n->unicast_flood = s->unicast_flood;
238 n->flags = s->flags | os->flags | os->valid_flags;
242 device_init_settings(struct device *dev, struct blob_attr **tb)
244 struct device_settings *s = &dev->settings;
245 struct blob_attr *cur;
246 struct ether_addr *ea;
247 bool disabled = false;
250 if ((cur = tb[DEV_ATTR_ENABLED]))
251 disabled = !blobmsg_get_bool(cur);
253 if ((cur = tb[DEV_ATTR_MTU])) {
254 s->mtu = blobmsg_get_u32(cur);
255 s->flags |= DEV_OPT_MTU;
258 if ((cur = tb[DEV_ATTR_MTU6])) {
259 s->mtu6 = blobmsg_get_u32(cur);
260 s->flags |= DEV_OPT_MTU6;
263 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
264 s->txqueuelen = blobmsg_get_u32(cur);
265 s->flags |= DEV_OPT_TXQUEUELEN;
268 if ((cur = tb[DEV_ATTR_MACADDR])) {
269 ea = ether_aton(blobmsg_data(cur));
271 memcpy(s->macaddr, ea, 6);
272 s->flags |= DEV_OPT_MACADDR;
276 if ((cur = tb[DEV_ATTR_IPV6])) {
277 s->ipv6 = blobmsg_get_bool(cur);
278 s->flags |= DEV_OPT_IPV6;
281 if ((cur = tb[DEV_ATTR_PROMISC])) {
282 s->promisc = blobmsg_get_bool(cur);
283 s->flags |= DEV_OPT_PROMISC;
286 if ((cur = tb[DEV_ATTR_RPFILTER])) {
287 if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter))
288 s->flags |= DEV_OPT_RPFILTER;
290 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur));
293 if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) {
294 s->acceptlocal = blobmsg_get_bool(cur);
295 s->flags |= DEV_OPT_ACCEPTLOCAL;
298 if ((cur = tb[DEV_ATTR_IGMPVERSION])) {
299 s->igmpversion = blobmsg_get_u32(cur);
300 if (s->igmpversion >= 1 && s->igmpversion <= 3)
301 s->flags |= DEV_OPT_IGMPVERSION;
303 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur));
306 if ((cur = tb[DEV_ATTR_MLDVERSION])) {
307 s->mldversion = blobmsg_get_u32(cur);
308 if (s->mldversion >= 1 && s->mldversion <= 2)
309 s->flags |= DEV_OPT_MLDVERSION;
311 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur));
314 if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) {
315 s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur);
316 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
319 if ((cur = tb[DEV_ATTR_NEIGHGCSTALETIME])) {
320 s->neigh6gcstaletime = s->neigh4gcstaletime = blobmsg_get_u32(cur);
321 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
324 if ((cur = tb[DEV_ATTR_RPS])) {
325 s->rps = blobmsg_get_bool(cur);
326 s->flags |= DEV_OPT_RPS;
331 if ((cur = tb[DEV_ATTR_XPS])) {
332 s->xps = blobmsg_get_bool(cur);
333 s->flags |= DEV_OPT_XPS;
338 if ((cur = tb[DEV_ATTR_DADTRANSMITS])) {
339 s->dadtransmits = blobmsg_get_u32(cur);
340 s->flags |= DEV_OPT_DADTRANSMITS;
343 if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) {
344 s->multicast_to_unicast = blobmsg_get_bool(cur);
345 s->flags |= DEV_OPT_MULTICAST_TO_UNICAST;
348 if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) {
349 s->multicast_router = blobmsg_get_u32(cur);
350 if (s->multicast_router <= 2)
351 s->flags |= DEV_OPT_MULTICAST_ROUTER;
353 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur));
356 if ((cur = tb[DEV_ATTR_MULTICAST_FAST_LEAVE])) {
357 s->multicast_fast_leave = blobmsg_get_bool(cur);
358 s->flags |= DEV_OPT_MULTICAST_FAST_LEAVE;
361 if ((cur = tb[DEV_ATTR_MULTICAST])) {
362 s->multicast = blobmsg_get_bool(cur);
363 s->flags |= DEV_OPT_MULTICAST;
366 if ((cur = tb[DEV_ATTR_LEARNING])) {
367 s->learning = blobmsg_get_bool(cur);
368 s->flags |= DEV_OPT_LEARNING;
371 if ((cur = tb[DEV_ATTR_UNICAST_FLOOD])) {
372 s->unicast_flood = blobmsg_get_bool(cur);
373 s->flags |= DEV_OPT_UNICAST_FLOOD;
376 device_set_disabled(dev, disabled);
379 static void __init dev_init(void)
381 avl_init(&devices, avl_strcmp, true, NULL);
384 static int device_broadcast_cb(void *ctx, struct safe_list *list)
386 struct device_user *dep = container_of(list, struct device_user, list);
389 /* device might have been removed by an earlier callback */
398 void device_broadcast_event(struct device *dev, enum device_event ev)
402 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
403 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
406 int device_claim(struct device_user *dep)
408 struct device *dev = dep->dev;
418 D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
419 if (++dev->active != 1)
422 device_broadcast_event(dev, DEV_EVENT_SETUP);
424 /* Get ifindex for external claimed devices so a valid */
425 /* ifindex is in place avoiding possible race conditions */
426 device_set_ifindex(dev, system_if_resolve(dev));
430 system_if_get_settings(dev, &dev->orig_settings);
432 ret = dev->set_state(dev, true);
435 device_broadcast_event(dev, DEV_EVENT_UP);
437 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
439 dep->claimed = false;
445 void device_release(struct device_user *dep)
447 struct device *dev = dep->dev;
452 dep->claimed = false;
454 D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
455 assert(dev->active >= 0);
460 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
462 dev->set_state(dev, false);
463 device_broadcast_event(dev, DEV_EVENT_DOWN);
466 int device_check_state(struct device *dev)
468 if (!dev->type->check_state)
469 return simple_device_type.check_state(dev);
471 return dev->type->check_state(dev);
474 void device_init_virtual(struct device *dev, struct device_type *type, const char *name)
479 D(DEVICE, "Initialize device '%s'\n", name ? name : "");
480 INIT_SAFE_LIST(&dev->users);
481 INIT_SAFE_LIST(&dev->aliases);
485 device_set_ifname(dev, name);
488 dev->set_state = set_device_state;
491 int device_init(struct device *dev, struct device_type *type, const char *ifname)
495 device_init_virtual(dev, type, ifname);
497 dev->avl.key = dev->ifname;
499 ret = avl_insert(&devices, &dev->avl);
503 system_if_clear_state(dev);
504 device_check_state(dev);
505 dev->settings.rps = default_ps;
506 dev->settings.xps = default_ps;
511 static struct device *
512 device_create_default(const char *name, bool external)
516 if (!external && system_if_force_external(name))
519 D(DEVICE, "Create simple device '%s'\n", name);
520 dev = calloc(1, sizeof(*dev));
524 dev->external = external;
525 dev->set_state = simple_device_set_state;
526 device_init(dev, &simple_device_type, name);
527 dev->default_config = true;
529 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
534 device_find(const char *name)
538 return avl_find_element(&devices, name, dev, avl);
542 device_get(const char *name, int create)
546 if (strchr(name, '.'))
547 return get_vlan_device_chain(name, create);
550 return device_alias_get(name + 1);
552 dev = avl_find_element(&devices, name, dev, avl);
554 if (create > 1 && !dev->external) {
555 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
556 dev->external = true;
557 device_set_present(dev, true);
565 return device_create_default(name, create > 1);
569 device_delete(struct device *dev)
574 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
575 avl_delete(&devices, &dev->avl);
579 static int device_cleanup_cb(void *ctx, struct safe_list *list)
581 struct device_user *dep = container_of(list, struct device_user, list);
583 dep->cb(dep, DEV_EVENT_REMOVE);
589 void device_cleanup(struct device *dev)
591 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
592 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
593 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
597 static void __device_set_present(struct device *dev, bool state)
599 if (dev->present == state)
602 dev->present = state;
603 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
607 device_refresh_present(struct device *dev)
609 bool state = dev->sys_present;
611 if (dev->disabled || dev->deferred)
614 __device_set_present(dev, state);
617 void device_set_present(struct device *dev, bool state)
619 if (dev->sys_present == state)
622 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
623 dev->sys_present = state;
624 device_refresh_present(dev);
627 void device_set_link(struct device *dev, bool state)
629 if (dev->link_active == state)
632 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
634 dev->link_active = state;
635 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
638 void device_set_ifindex(struct device *dev, int ifindex)
640 if (dev->ifindex == ifindex)
643 dev->ifindex = ifindex;
644 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
647 int device_set_ifname(struct device *dev, const char *name)
651 if (!strcmp(dev->ifname, name))
655 avl_delete(&devices, &dev->avl);
657 strncpy(dev->ifname, name, IFNAMSIZ);
660 ret = avl_insert(&devices, &dev->avl);
663 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
668 static int device_refcount(struct device *dev)
670 struct list_head *list;
673 list_for_each(list, &dev->users.list)
676 list_for_each(list, &dev->aliases.list)
683 __device_add_user(struct device_user *dep, struct device *dev)
685 struct safe_list *head;
690 head = &dev->aliases;
694 safe_list_add(&dep->list, head);
695 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
697 if (dep->cb && dev->present) {
698 dep->cb(dep, DEV_EVENT_ADD);
700 dep->cb(dep, DEV_EVENT_UP);
702 if (dev->link_active)
703 dep->cb(dep, DEV_EVENT_LINK_UP);
707 void device_add_user(struct device_user *dep, struct device *dev)
713 device_remove_user(dep);
718 __device_add_user(dep, dev);
722 device_free(struct device *dev)
727 dev->type->free(dev);
732 __device_free_unused(struct device *dev)
734 if (!safe_list_empty(&dev->users) ||
735 !safe_list_empty(&dev->aliases) ||
736 dev->current_config || __devlock)
742 void device_remove_user(struct device_user *dep)
744 struct device *dev = dep->dev;
749 dep->hotplug = false;
753 safe_list_del(&dep->list);
755 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
756 __device_free_unused(dev);
760 device_free_unused(struct device *dev)
765 return __device_free_unused(dev);
767 avl_for_each_element_safe(&devices, dev, avl, tmp)
768 __device_free_unused(dev);
772 device_init_pending(void)
774 struct device *dev, *tmp;
776 avl_for_each_element_safe(&devices, dev, avl, tmp) {
777 if (!dev->config_pending)
780 dev->type->config_init(dev);
781 dev->config_pending = false;
785 static enum dev_change_type
786 device_set_config(struct device *dev, struct device_type *type,
787 struct blob_attr *attr)
789 struct blob_attr *tb[__DEV_ATTR_MAX];
790 const struct uci_blob_param_list *cfg = type->config_params;
792 if (type != dev->type)
793 return DEV_CONFIG_RECREATE;
795 if (dev->type->reload)
796 return dev->type->reload(dev, attr);
798 if (uci_blob_check_equal(dev->config, attr, cfg))
799 return DEV_CONFIG_NO_CHANGE;
801 if (cfg == &device_attr_list) {
802 memset(tb, 0, sizeof(tb));
805 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
806 blob_data(attr), blob_len(attr));
808 device_init_settings(dev, tb);
809 return DEV_CONFIG_RESTART;
811 return DEV_CONFIG_RECREATE;
815 device_apply_config(struct device *dev, struct device_type *type,
816 struct blob_attr *config)
818 enum dev_change_type change;
820 change = device_set_config(dev, type, config);
822 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
823 change = DEV_CONFIG_APPLIED;
827 case DEV_CONFIG_RESTART:
828 case DEV_CONFIG_APPLIED:
829 D(DEVICE, "Device '%s': config applied\n", dev->ifname);
830 config = blob_memdup(config);
832 dev->config = config;
833 if (change == DEV_CONFIG_RESTART && dev->present) {
836 device_set_present(dev, false);
837 if (dev->active && !dev->external) {
838 ret = dev->set_state(dev, false);
840 ret = dev->set_state(dev, true);
843 device_set_present(dev, true);
846 case DEV_CONFIG_NO_CHANGE:
847 D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
849 case DEV_CONFIG_RECREATE:
857 device_replace(struct device *dev, struct device *odev)
859 struct device_user *dep, *tmp;
863 device_set_present(odev, false);
865 list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
867 safe_list_del(&dep->list);
868 __device_add_user(dep, dev);
876 device_reset_config(void)
880 avl_for_each_element(&devices, dev, avl)
881 dev->current_config = false;
885 device_reset_old(void)
887 struct device *dev, *tmp, *ndev;
889 avl_for_each_element_safe(&devices, dev, avl, tmp) {
890 if (dev->current_config || dev->default_config)
893 if (dev->type != &simple_device_type)
896 ndev = device_create_default(dev->ifname, dev->external);
900 device_replace(ndev, dev);
905 device_set_default_ps(bool state)
909 if (state == default_ps)
914 avl_for_each_element(&devices, dev, avl) {
915 struct device_settings *s = &dev->settings;
916 unsigned int apply_mask = 0;
918 if (!(s->flags & DEV_OPT_RPS)) {
920 apply_mask |= DEV_OPT_RPS;
923 if (!(s->flags & DEV_OPT_XPS)) {
925 apply_mask |= DEV_OPT_XPS;
931 if (!(dev->external || (dev->present && dev->active)) ||
935 system_if_apply_settings(dev, s, apply_mask);
940 device_create(const char *name, struct device_type *type,
941 struct blob_attr *config)
943 struct device *odev = NULL, *dev;
944 enum dev_change_type change;
946 odev = device_get(name, false);
948 odev->current_config = true;
949 change = device_apply_config(odev, type, config);
951 case DEV_CONFIG_RECREATE:
952 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
959 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
961 config = blob_memdup(config);
965 dev = type->create(name, type, config);
969 dev->current_config = true;
970 dev->config = config;
972 device_replace(dev, odev);
974 if (!config_init && dev->config_pending) {
975 type->config_init(dev);
976 dev->config_pending = false;
983 device_dump_status(struct blob_buf *b, struct device *dev)
985 struct device_settings st;
989 avl_for_each_element(&devices, dev, avl) {
992 c = blobmsg_open_table(b, dev->ifname);
993 device_dump_status(b, dev);
994 blobmsg_close_table(b, c);
1000 blobmsg_add_u8(b, "external", dev->external);
1001 blobmsg_add_u8(b, "present", dev->present);
1002 blobmsg_add_string(b, "type", dev->type->name);
1007 blobmsg_add_u8(b, "up", !!dev->active);
1008 blobmsg_add_u8(b, "carrier", !!dev->link_active);
1010 if (dev->type->dump_info)
1011 dev->type->dump_info(dev, b);
1013 system_if_dump_info(dev, b);
1016 device_merge_settings(dev, &st);
1017 if (st.flags & DEV_OPT_MTU)
1018 blobmsg_add_u32(b, "mtu", st.mtu);
1019 if (st.flags & DEV_OPT_MTU6)
1020 blobmsg_add_u32(b, "mtu6", st.mtu6);
1021 if (st.flags & DEV_OPT_MACADDR)
1022 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
1023 if (st.flags & DEV_OPT_TXQUEUELEN)
1024 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
1025 if (st.flags & DEV_OPT_IPV6)
1026 blobmsg_add_u8(b, "ipv6", st.ipv6);
1027 if (st.flags & DEV_OPT_PROMISC)
1028 blobmsg_add_u8(b, "promisc", st.promisc);
1029 if (st.flags & DEV_OPT_RPFILTER)
1030 blobmsg_add_u32(b, "rpfilter", st.rpfilter);
1031 if (st.flags & DEV_OPT_ACCEPTLOCAL)
1032 blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
1033 if (st.flags & DEV_OPT_IGMPVERSION)
1034 blobmsg_add_u32(b, "igmpversion", st.igmpversion);
1035 if (st.flags & DEV_OPT_MLDVERSION)
1036 blobmsg_add_u32(b, "mldversion", st.mldversion);
1037 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
1038 blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
1039 blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
1041 if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
1042 blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
1043 blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
1045 if (st.flags & DEV_OPT_DADTRANSMITS)
1046 blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
1047 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
1048 blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
1049 if (st.flags & DEV_OPT_MULTICAST_ROUTER)
1050 blobmsg_add_u32(b, "multicast_router", st.multicast_router);
1051 if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE)
1052 blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave);
1053 if (st.flags & DEV_OPT_MULTICAST)
1054 blobmsg_add_u8(b, "multicast", st.multicast);
1055 if (st.flags & DEV_OPT_LEARNING)
1056 blobmsg_add_u8(b, "learning", st.learning);
1057 if (st.flags & DEV_OPT_UNICAST_FLOOD)
1058 blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
1061 s = blobmsg_open_table(b, "statistics");
1062 if (dev->type->dump_stats)
1063 dev->type->dump_stats(dev, b);
1065 system_if_dump_stats(dev, b);
1066 blobmsg_close_table(b, s);