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;
36 static int default_rps_val;
37 static int default_rps_flow_cnt;
38 static int default_xps_val;
40 static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
41 [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
42 [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
43 [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 },
44 [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
45 [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
46 [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
47 [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
48 [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL },
49 [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING },
50 [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL },
51 [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 },
52 [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 },
53 [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 },
54 [DEV_ATTR_NEIGHGCSTALETIME] = { .name = "neighgcstaletime", .type = BLOBMSG_TYPE_INT32 },
55 [DEV_ATTR_RPS] = { .name = "rps", .type = BLOBMSG_TYPE_BOOL },
56 [DEV_ATTR_XPS] = { .name = "xps", .type = BLOBMSG_TYPE_BOOL },
57 [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 },
58 [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
59 [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 },
60 [DEV_ATTR_MULTICAST_FAST_LEAVE] = { .name = "multicast_fast_leave", . type = BLOBMSG_TYPE_BOOL },
61 [DEV_ATTR_MULTICAST] = { .name ="multicast", .type = BLOBMSG_TYPE_BOOL },
62 [DEV_ATTR_LEARNING] = { .name ="learning", .type = BLOBMSG_TYPE_BOOL },
63 [DEV_ATTR_UNICAST_FLOOD] = { .name ="unicast_flood", .type = BLOBMSG_TYPE_BOOL },
64 [DEV_ATTR_SENDREDIRECTS] = { .name = "sendredirects", .type = BLOBMSG_TYPE_BOOL },
65 [DEV_ATTR_NEIGHLOCKTIME] = { .name = "neighlocktime", .type = BLOBMSG_TYPE_INT32 },
68 const struct uci_blob_param_list device_attr_list = {
69 .n_params = __DEV_ATTR_MAX,
73 static int __devlock = 0;
75 int device_type_add(struct device_type *devtype)
77 if (device_type_get(devtype->name)) {
78 netifd_log_message(L_WARNING, "Device handler '%s' already exists\n",
83 netifd_log_message(L_NOTICE, "Added device handler type: %s\n",
86 list_add(&devtype->list, &devtypes);
90 /* Retrieve the device type for the given name. If 'bridge' is true, the type
91 * must have bridge capabilities
94 device_type_get(const char *tname)
96 struct device_type *cur;
98 list_for_each_entry(cur, &devtypes, list)
99 if (!strcmp(cur->name, tname))
105 void device_lock(void)
110 void device_unlock(void)
114 device_free_unused(NULL);
117 static int set_device_state(struct device *dev, bool state)
120 /* Get ifindex for all devices being enabled so a valid */
121 /* ifindex is in place avoiding possible race conditions */
122 device_set_ifindex(dev, system_if_resolve(dev));
135 simple_device_set_state(struct device *dev, bool state)
140 pdev = dev->parent.dev;
141 if (state && !pdev) {
142 pdev = system_if_get_parent(dev);
144 device_add_user(&dev->parent, pdev);
149 ret = device_claim(&dev->parent);
151 device_release(&dev->parent);
156 return set_device_state(dev, state);
159 static struct device *
160 simple_device_create(const char *name, struct device_type *devtype,
161 struct blob_attr *attr)
163 struct blob_attr *tb[__DEV_ATTR_MAX];
164 struct device *dev = NULL;
166 /* device type is unused for simple devices */
169 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
170 dev = device_get(name, true);
174 dev->set_state = simple_device_set_state;
175 device_init_settings(dev, tb);
180 static void simple_device_free(struct device *dev)
183 device_remove_user(&dev->parent);
187 struct device_type simple_device_type = {
188 .name = "Network device",
189 .config_params = &device_attr_list,
191 .create = simple_device_create,
192 .check_state = system_if_check,
193 .free = simple_device_free,
197 device_merge_settings(struct device *dev, struct device_settings *n)
199 struct device_settings *os = &dev->orig_settings;
200 struct device_settings *s = &dev->settings;
202 memset(n, 0, sizeof(*n));
203 n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
204 n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6;
205 n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
206 s->txqueuelen : os->txqueuelen;
208 (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
210 n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
211 n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc;
212 n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter;
213 n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal;
214 n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion;
215 n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion;
216 n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
217 s->neigh4reachabletime : os->neigh4reachabletime;
218 n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
219 s->neigh6reachabletime : os->neigh6reachabletime;
220 n->neigh4gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
221 s->neigh4gcstaletime : os->neigh4gcstaletime;
222 n->neigh6gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
223 s->neigh6gcstaletime : os->neigh6gcstaletime;
224 n->neigh4locktime = s->flags & DEV_OPT_NEIGHLOCKTIME ?
225 s->neigh4locktime : os->neigh4locktime;
226 n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ?
227 s->dadtransmits : os->dadtransmits;
228 n->multicast = s->flags & DEV_OPT_MULTICAST ?
229 s->multicast : os->multicast;
230 n->multicast_to_unicast = s->multicast_to_unicast;
231 n->multicast_router = s->multicast_router;
232 n->multicast_fast_leave = s->multicast_fast_leave;
233 n->learning = s->learning;
234 n->unicast_flood = s->unicast_flood;
235 n->sendredirects = s->flags & DEV_OPT_SENDREDIRECTS ?
236 s->sendredirects : os->sendredirects;
237 n->flags = s->flags | os->flags | os->valid_flags;
241 device_init_settings(struct device *dev, struct blob_attr **tb)
243 struct device_settings *s = &dev->settings;
244 struct blob_attr *cur;
245 struct ether_addr *ea;
246 bool disabled = false;
249 if ((cur = tb[DEV_ATTR_ENABLED]))
250 disabled = !blobmsg_get_bool(cur);
252 if ((cur = tb[DEV_ATTR_MTU]) && blobmsg_get_u32(cur) >= 68) {
253 s->mtu = blobmsg_get_u32(cur);
254 s->flags |= DEV_OPT_MTU;
257 if ((cur = tb[DEV_ATTR_MTU6]) && blobmsg_get_u32(cur) >= 1280) {
258 s->mtu6 = blobmsg_get_u32(cur);
259 s->flags |= DEV_OPT_MTU6;
262 if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
263 s->txqueuelen = blobmsg_get_u32(cur);
264 s->flags |= DEV_OPT_TXQUEUELEN;
267 if ((cur = tb[DEV_ATTR_MACADDR])) {
268 ea = ether_aton(blobmsg_data(cur));
270 memcpy(s->macaddr, ea, 6);
271 s->flags |= DEV_OPT_MACADDR;
275 if ((cur = tb[DEV_ATTR_IPV6])) {
276 s->ipv6 = blobmsg_get_bool(cur);
277 s->flags |= DEV_OPT_IPV6;
280 if ((cur = tb[DEV_ATTR_PROMISC])) {
281 s->promisc = blobmsg_get_bool(cur);
282 s->flags |= DEV_OPT_PROMISC;
285 if ((cur = tb[DEV_ATTR_RPFILTER])) {
286 if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter))
287 s->flags |= DEV_OPT_RPFILTER;
289 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur));
292 if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) {
293 s->acceptlocal = blobmsg_get_bool(cur);
294 s->flags |= DEV_OPT_ACCEPTLOCAL;
297 if ((cur = tb[DEV_ATTR_IGMPVERSION])) {
298 s->igmpversion = blobmsg_get_u32(cur);
299 if (s->igmpversion >= 1 && s->igmpversion <= 3)
300 s->flags |= DEV_OPT_IGMPVERSION;
302 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur));
305 if ((cur = tb[DEV_ATTR_MLDVERSION])) {
306 s->mldversion = blobmsg_get_u32(cur);
307 if (s->mldversion >= 1 && s->mldversion <= 2)
308 s->flags |= DEV_OPT_MLDVERSION;
310 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur));
313 if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) {
314 s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur);
315 s->flags |= DEV_OPT_NEIGHREACHABLETIME;
318 if ((cur = tb[DEV_ATTR_NEIGHGCSTALETIME])) {
319 s->neigh6gcstaletime = s->neigh4gcstaletime = blobmsg_get_u32(cur);
320 s->flags |= DEV_OPT_NEIGHGCSTALETIME;
323 if ((cur = tb[DEV_ATTR_NEIGHLOCKTIME])) {
324 s->neigh4locktime = blobmsg_get_u32(cur);
325 s->flags |= DEV_OPT_NEIGHLOCKTIME;
328 if ((cur = tb[DEV_ATTR_RPS])) {
329 s->rps = blobmsg_get_bool(cur);
330 s->flags |= DEV_OPT_RPS;
335 if ((cur = tb[DEV_ATTR_XPS])) {
336 s->xps = blobmsg_get_bool(cur);
337 s->flags |= DEV_OPT_XPS;
342 if ((cur = tb[DEV_ATTR_DADTRANSMITS])) {
343 s->dadtransmits = blobmsg_get_u32(cur);
344 s->flags |= DEV_OPT_DADTRANSMITS;
347 if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) {
348 s->multicast_to_unicast = blobmsg_get_bool(cur);
349 s->flags |= DEV_OPT_MULTICAST_TO_UNICAST;
352 if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) {
353 s->multicast_router = blobmsg_get_u32(cur);
354 if (s->multicast_router <= 2)
355 s->flags |= DEV_OPT_MULTICAST_ROUTER;
357 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur));
360 if ((cur = tb[DEV_ATTR_MULTICAST_FAST_LEAVE])) {
361 s->multicast_fast_leave = blobmsg_get_bool(cur);
362 s->flags |= DEV_OPT_MULTICAST_FAST_LEAVE;
365 if ((cur = tb[DEV_ATTR_MULTICAST])) {
366 s->multicast = blobmsg_get_bool(cur);
367 s->flags |= DEV_OPT_MULTICAST;
370 if ((cur = tb[DEV_ATTR_LEARNING])) {
371 s->learning = blobmsg_get_bool(cur);
372 s->flags |= DEV_OPT_LEARNING;
375 if ((cur = tb[DEV_ATTR_UNICAST_FLOOD])) {
376 s->unicast_flood = blobmsg_get_bool(cur);
377 s->flags |= DEV_OPT_UNICAST_FLOOD;
380 if ((cur = tb[DEV_ATTR_SENDREDIRECTS])) {
381 s->sendredirects = blobmsg_get_bool(cur);
382 s->flags |= DEV_OPT_SENDREDIRECTS;
385 device_set_disabled(dev, disabled);
388 static void __init dev_init(void)
390 avl_init(&devices, avl_strcmp, true, NULL);
393 static int device_broadcast_cb(void *ctx, struct safe_list *list)
395 struct device_user *dep = container_of(list, struct device_user, list);
398 /* device might have been removed by an earlier callback */
407 void device_broadcast_event(struct device *dev, enum device_event ev)
411 safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
412 safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
415 int device_claim(struct device_user *dep)
417 struct device *dev = dep->dev;
427 D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
428 if (++dev->active != 1)
431 device_broadcast_event(dev, DEV_EVENT_SETUP);
433 /* Get ifindex for external claimed devices so a valid */
434 /* ifindex is in place avoiding possible race conditions */
435 device_set_ifindex(dev, system_if_resolve(dev));
439 system_if_get_settings(dev, &dev->orig_settings);
441 ret = dev->set_state(dev, true);
444 device_broadcast_event(dev, DEV_EVENT_UP);
446 D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
448 dep->claimed = false;
454 void device_release(struct device_user *dep)
456 struct device *dev = dep->dev;
461 dep->claimed = false;
463 D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
464 assert(dev->active >= 0);
469 device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
471 dev->set_state(dev, false);
476 device_broadcast_event(dev, DEV_EVENT_DOWN);
479 int device_check_state(struct device *dev)
481 if (!dev->type->check_state)
482 return simple_device_type.check_state(dev);
484 return dev->type->check_state(dev);
487 void device_init_virtual(struct device *dev, struct device_type *type, const char *name)
492 D(DEVICE, "Initialize device '%s'\n", name ? name : "");
493 INIT_SAFE_LIST(&dev->users);
494 INIT_SAFE_LIST(&dev->aliases);
498 device_set_ifname(dev, name);
501 dev->set_state = set_device_state;
504 int device_init(struct device *dev, struct device_type *type, const char *ifname)
508 device_init_virtual(dev, type, ifname);
510 dev->avl.key = dev->ifname;
512 ret = avl_insert(&devices, &dev->avl);
516 system_if_clear_state(dev);
517 device_check_state(dev);
518 dev->settings.rps = default_ps;
519 dev->settings.xps = default_ps;
524 static struct device *
525 device_create_default(const char *name, bool external)
529 if (!external && system_if_force_external(name))
532 D(DEVICE, "Create simple device '%s'\n", name);
533 dev = calloc(1, sizeof(*dev));
537 dev->external = external;
538 dev->set_state = simple_device_set_state;
539 device_init(dev, &simple_device_type, name);
540 dev->default_config = true;
542 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
547 device_find(const char *name)
551 return avl_find_element(&devices, name, dev, avl);
555 device_get(const char *name, int create)
559 if (strchr(name, '.'))
560 return get_vlan_device_chain(name, create);
563 return device_alias_get(name + 1);
565 dev = avl_find_element(&devices, name, dev, avl);
567 if (create > 1 && !dev->external) {
568 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
569 dev->external = true;
570 device_set_present(dev, true);
578 return device_create_default(name, create > 1);
582 device_delete(struct device *dev)
587 D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
588 avl_delete(&devices, &dev->avl);
592 static int device_cleanup_cb(void *ctx, struct safe_list *list)
594 struct device_user *dep = container_of(list, struct device_user, list);
596 dep->cb(dep, DEV_EVENT_REMOVE);
602 void device_cleanup(struct device *dev)
604 D(DEVICE, "Clean up device '%s'\n", dev->ifname);
605 safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
606 safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
610 static void __device_set_present(struct device *dev, bool state)
612 if (dev->present == state)
615 dev->present = state;
616 device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
620 device_refresh_present(struct device *dev)
622 bool state = dev->sys_present;
624 if (dev->disabled || dev->deferred)
627 __device_set_present(dev, state);
630 void device_set_present(struct device *dev, bool state)
632 if (dev->sys_present == state)
635 D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
636 dev->sys_present = state;
637 device_refresh_present(dev);
640 void device_set_link(struct device *dev, bool state)
642 if (dev->link_active == state)
645 netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
647 dev->link_active = state;
648 device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
651 void device_set_ifindex(struct device *dev, int ifindex)
653 if (dev->ifindex == ifindex)
656 dev->ifindex = ifindex;
657 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
660 int device_set_ifname(struct device *dev, const char *name)
664 if (!strcmp(dev->ifname, name))
668 avl_delete(&devices, &dev->avl);
670 strncpy(dev->ifname, name, IFNAMSIZ);
673 ret = avl_insert(&devices, &dev->avl);
676 device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
681 static int device_refcount(struct device *dev)
683 struct list_head *list;
686 list_for_each(list, &dev->users.list)
689 list_for_each(list, &dev->aliases.list)
696 __device_add_user(struct device_user *dep, struct device *dev)
698 struct safe_list *head;
703 head = &dev->aliases;
707 safe_list_add(&dep->list, head);
708 D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
710 if (dep->cb && dev->present) {
711 dep->cb(dep, DEV_EVENT_ADD);
713 dep->cb(dep, DEV_EVENT_UP);
715 if (dev->link_active)
716 dep->cb(dep, DEV_EVENT_LINK_UP);
720 void device_add_user(struct device_user *dep, struct device *dev)
726 device_remove_user(dep);
731 __device_add_user(dep, dev);
735 device_free(struct device *dev)
740 dev->type->free(dev);
745 __device_free_unused(struct device *dev)
747 if (!safe_list_empty(&dev->users) ||
748 !safe_list_empty(&dev->aliases) ||
749 dev->current_config || __devlock)
755 void device_remove_user(struct device_user *dep)
757 struct device *dev = dep->dev;
762 dep->hotplug = false;
766 safe_list_del(&dep->list);
768 D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
769 __device_free_unused(dev);
773 device_free_unused(struct device *dev)
778 return __device_free_unused(dev);
780 avl_for_each_element_safe(&devices, dev, avl, tmp)
781 __device_free_unused(dev);
785 device_init_pending(void)
787 struct device *dev, *tmp;
789 avl_for_each_element_safe(&devices, dev, avl, tmp) {
790 if (!dev->config_pending)
793 dev->type->config_init(dev);
794 dev->config_pending = false;
798 static enum dev_change_type
799 device_set_config(struct device *dev, struct device_type *type,
800 struct blob_attr *attr)
802 struct blob_attr *tb[__DEV_ATTR_MAX];
803 const struct uci_blob_param_list *cfg = type->config_params;
805 if (type != dev->type)
806 return DEV_CONFIG_RECREATE;
808 if (dev->type->reload)
809 return dev->type->reload(dev, attr);
811 if (uci_blob_check_equal(dev->config, attr, cfg))
812 return DEV_CONFIG_NO_CHANGE;
814 if (cfg == &device_attr_list) {
815 memset(tb, 0, sizeof(tb));
818 blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
819 blob_data(attr), blob_len(attr));
821 device_init_settings(dev, tb);
822 return DEV_CONFIG_RESTART;
824 return DEV_CONFIG_RECREATE;
828 device_apply_config(struct device *dev, struct device_type *type,
829 struct blob_attr *config)
831 enum dev_change_type change;
833 change = device_set_config(dev, type, config);
835 system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
836 change = DEV_CONFIG_APPLIED;
840 case DEV_CONFIG_RESTART:
841 case DEV_CONFIG_APPLIED:
842 D(DEVICE, "Device '%s': config applied\n", dev->ifname);
843 config = blob_memdup(config);
845 dev->config = config;
846 if (change == DEV_CONFIG_RESTART && dev->present) {
849 device_set_present(dev, false);
850 if (dev->active && !dev->external) {
851 ret = dev->set_state(dev, false);
853 ret = dev->set_state(dev, true);
856 device_set_present(dev, true);
859 case DEV_CONFIG_NO_CHANGE:
860 D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
862 case DEV_CONFIG_RECREATE:
870 device_replace(struct device *dev, struct device *odev)
872 struct device_user *dep, *tmp;
876 device_set_present(odev, false);
878 list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
880 safe_list_del(&dep->list);
881 __device_add_user(dep, dev);
889 device_reset_config(void)
893 avl_for_each_element(&devices, dev, avl)
894 dev->current_config = false;
898 device_reset_old(void)
900 struct device *dev, *tmp, *ndev;
902 avl_for_each_element_safe(&devices, dev, avl, tmp) {
903 if (dev->current_config || dev->default_config)
906 if (dev->type != &simple_device_type)
909 ndev = device_create_default(dev->ifname, dev->external);
913 device_replace(ndev, dev);
918 device_set_default_ps(bool state, int xps, int rps, int rps_flow_cnt)
922 if ((state == default_ps) && (default_rps_val == rps) &&
923 (default_xps_val == xps) && (default_rps_flow_cnt == rps_flow_cnt))
927 default_rps_val = rps;
928 default_rps_flow_cnt = rps_flow_cnt;
929 default_xps_val = xps;
931 avl_for_each_element(&devices, dev, avl) {
932 struct device_settings *s = &dev->settings;
933 unsigned int apply_mask = 0;
935 if (!(s->flags & DEV_OPT_RPS)) {
937 s->rps_val = default_rps_val;
938 s->rps_flow_cnt = default_rps_flow_cnt;
939 apply_mask |= DEV_OPT_RPS;
942 if (!(s->flags & DEV_OPT_XPS)) {
944 s->xps_val = default_xps_val;
945 apply_mask |= DEV_OPT_XPS;
951 if (!(dev->external || (dev->present && dev->active)) ||
955 system_if_apply_settings(dev, s, apply_mask);
960 device_create(const char *name, struct device_type *type,
961 struct blob_attr *config)
963 struct device *odev = NULL, *dev;
964 enum dev_change_type change;
966 odev = device_find(name);
968 odev->current_config = true;
969 change = device_apply_config(odev, type, config);
971 case DEV_CONFIG_RECREATE:
972 D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
979 D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
981 config = blob_memdup(config);
985 dev = type->create(name, type, config);
989 dev->current_config = true;
990 dev->config = config;
992 device_replace(dev, odev);
994 if (!config_init && dev->config_pending) {
995 type->config_init(dev);
996 dev->config_pending = false;
1003 device_dump_status(struct blob_buf *b, struct device *dev)
1005 struct device_settings st;
1009 avl_for_each_element(&devices, dev, avl) {
1012 c = blobmsg_open_table(b, dev->ifname);
1013 device_dump_status(b, dev);
1014 blobmsg_close_table(b, c);
1020 blobmsg_add_u8(b, "external", dev->external);
1021 blobmsg_add_u8(b, "present", dev->present);
1022 blobmsg_add_string(b, "type", dev->type->name);
1027 blobmsg_add_u8(b, "up", !!dev->active);
1028 blobmsg_add_u8(b, "carrier", !!dev->link_active);
1030 if (dev->type->dump_info)
1031 dev->type->dump_info(dev, b);
1033 system_if_dump_info(dev, b);
1036 device_merge_settings(dev, &st);
1037 if (st.flags & DEV_OPT_MTU)
1038 blobmsg_add_u32(b, "mtu", st.mtu);
1039 if (st.flags & DEV_OPT_MTU6)
1040 blobmsg_add_u32(b, "mtu6", st.mtu6);
1041 if (st.flags & DEV_OPT_MACADDR)
1042 blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
1043 if (st.flags & DEV_OPT_TXQUEUELEN)
1044 blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
1045 if (st.flags & DEV_OPT_IPV6)
1046 blobmsg_add_u8(b, "ipv6", st.ipv6);
1047 if (st.flags & DEV_OPT_PROMISC)
1048 blobmsg_add_u8(b, "promisc", st.promisc);
1049 if (st.flags & DEV_OPT_RPFILTER)
1050 blobmsg_add_u32(b, "rpfilter", st.rpfilter);
1051 if (st.flags & DEV_OPT_ACCEPTLOCAL)
1052 blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
1053 if (st.flags & DEV_OPT_IGMPVERSION)
1054 blobmsg_add_u32(b, "igmpversion", st.igmpversion);
1055 if (st.flags & DEV_OPT_MLDVERSION)
1056 blobmsg_add_u32(b, "mldversion", st.mldversion);
1057 if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
1058 blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
1059 blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
1061 if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
1062 blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
1063 blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
1065 if (st.flags & DEV_OPT_NEIGHLOCKTIME)
1066 blobmsg_add_u32(b, "neigh4locktime", st.neigh4locktime);
1067 if (st.flags & DEV_OPT_DADTRANSMITS)
1068 blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
1069 if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
1070 blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
1071 if (st.flags & DEV_OPT_MULTICAST_ROUTER)
1072 blobmsg_add_u32(b, "multicast_router", st.multicast_router);
1073 if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE)
1074 blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave);
1075 if (st.flags & DEV_OPT_MULTICAST)
1076 blobmsg_add_u8(b, "multicast", st.multicast);
1077 if (st.flags & DEV_OPT_LEARNING)
1078 blobmsg_add_u8(b, "learning", st.learning);
1079 if (st.flags & DEV_OPT_UNICAST_FLOOD)
1080 blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
1081 if (st.flags & DEV_OPT_SENDREDIRECTS)
1082 blobmsg_add_u8(b, "sendredirects", st.sendredirects);
1085 s = blobmsg_open_table(b, "statistics");
1086 if (dev->type->dump_stats)
1087 dev->type->dump_stats(dev, b);
1089 system_if_dump_stats(dev, b);
1090 blobmsg_close_table(b, s);
1093 static void __init simple_device_type_init(void)
1095 device_type_add(&simple_device_type);