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
;
32 static bool default_ps
= true;
34 static const struct blobmsg_policy dev_attrs
[__DEV_ATTR_MAX
] = {
35 [DEV_ATTR_TYPE
] = { .name
= "type", .type
= BLOBMSG_TYPE_STRING
},
36 [DEV_ATTR_MTU
] = { .name
= "mtu", .type
= BLOBMSG_TYPE_INT32
},
37 [DEV_ATTR_MTU6
] = { .name
= "mtu6", .type
= BLOBMSG_TYPE_INT32
},
38 [DEV_ATTR_MACADDR
] = { .name
= "macaddr", .type
= BLOBMSG_TYPE_STRING
},
39 [DEV_ATTR_TXQUEUELEN
] = { .name
= "txqueuelen", .type
= BLOBMSG_TYPE_INT32
},
40 [DEV_ATTR_ENABLED
] = { .name
= "enabled", .type
= BLOBMSG_TYPE_BOOL
},
41 [DEV_ATTR_IPV6
] = { .name
= "ipv6", .type
= BLOBMSG_TYPE_BOOL
},
42 [DEV_ATTR_PROMISC
] = { .name
= "promisc", .type
= BLOBMSG_TYPE_BOOL
},
43 [DEV_ATTR_RPFILTER
] = { .name
= "rpfilter", .type
= BLOBMSG_TYPE_STRING
},
44 [DEV_ATTR_ACCEPTLOCAL
] = { .name
= "acceptlocal", .type
= BLOBMSG_TYPE_BOOL
},
45 [DEV_ATTR_IGMPVERSION
] = { .name
= "igmpversion", .type
= BLOBMSG_TYPE_INT32
},
46 [DEV_ATTR_MLDVERSION
] = { .name
= "mldversion", .type
= BLOBMSG_TYPE_INT32
},
47 [DEV_ATTR_NEIGHREACHABLETIME
] = { .name
= "neighreachabletime", .type
= BLOBMSG_TYPE_INT32
},
48 [DEV_ATTR_NEIGHGCSTALETIME
] = { .name
= "neighgcstaletime", .type
= BLOBMSG_TYPE_INT32
},
49 [DEV_ATTR_RPS
] = { .name
= "rps", .type
= BLOBMSG_TYPE_BOOL
},
50 [DEV_ATTR_XPS
] = { .name
= "xps", .type
= BLOBMSG_TYPE_BOOL
},
51 [DEV_ATTR_DADTRANSMITS
] = { .name
= "dadtransmits", .type
= BLOBMSG_TYPE_INT32
},
52 [DEV_ATTR_MULTICAST_TO_UNICAST
] = { .name
= "multicast_to_unicast", .type
= BLOBMSG_TYPE_BOOL
},
53 [DEV_ATTR_MULTICAST_ROUTER
] = { .name
= "multicast_router", .type
= BLOBMSG_TYPE_INT32
},
54 [DEV_ATTR_MULTICAST
] = { .name
="multicast", .type
= BLOBMSG_TYPE_BOOL
},
55 [DEV_ATTR_LEARNING
] = { .name
="learning", .type
= BLOBMSG_TYPE_BOOL
},
56 [DEV_ATTR_UNICAST_FLOOD
] = { .name
="unicast_flood", .type
= BLOBMSG_TYPE_BOOL
},
59 const struct uci_blob_param_list device_attr_list
= {
60 .n_params
= __DEV_ATTR_MAX
,
64 static int __devlock
= 0;
66 void device_lock(void)
71 void device_unlock(void)
75 device_free_unused(NULL
);
78 static int set_device_state(struct device
*dev
, bool state
)
81 /* Get ifindex for all devices being enabled so a valid */
82 /* ifindex is in place avoiding possible race conditions */
83 device_set_ifindex(dev
, system_if_resolve(dev
));
96 simple_device_set_state(struct device
*dev
, bool state
)
101 pdev
= dev
->parent
.dev
;
102 if (state
&& !pdev
) {
103 pdev
= system_if_get_parent(dev
);
105 device_add_user(&dev
->parent
, pdev
);
110 ret
= device_claim(&dev
->parent
);
112 device_release(&dev
->parent
);
117 return set_device_state(dev
, state
);
120 static struct device
*
121 simple_device_create(const char *name
, struct blob_attr
*attr
)
123 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
124 struct device
*dev
= NULL
;
126 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
127 dev
= device_get(name
, true);
131 dev
->set_state
= simple_device_set_state
;
132 device_init_settings(dev
, tb
);
137 static void simple_device_free(struct device
*dev
)
140 device_remove_user(&dev
->parent
);
144 const struct device_type simple_device_type
= {
145 .name
= "Network device",
146 .config_params
= &device_attr_list
,
148 .create
= simple_device_create
,
149 .check_state
= system_if_check
,
150 .free
= simple_device_free
,
154 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
156 struct device_settings
*os
= &dev
->orig_settings
;
157 struct device_settings
*s
= &dev
->settings
;
159 memset(n
, 0, sizeof(*n
));
160 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
161 n
->mtu6
= s
->flags
& DEV_OPT_MTU6
? s
->mtu6
: os
->mtu6
;
162 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
163 s
->txqueuelen
: os
->txqueuelen
;
165 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
167 n
->ipv6
= s
->flags
& DEV_OPT_IPV6
? s
->ipv6
: os
->ipv6
;
168 n
->promisc
= s
->flags
& DEV_OPT_PROMISC
? s
->promisc
: os
->promisc
;
169 n
->rpfilter
= s
->flags
& DEV_OPT_RPFILTER
? s
->rpfilter
: os
->rpfilter
;
170 n
->acceptlocal
= s
->flags
& DEV_OPT_ACCEPTLOCAL
? s
->acceptlocal
: os
->acceptlocal
;
171 n
->igmpversion
= s
->flags
& DEV_OPT_IGMPVERSION
? s
->igmpversion
: os
->igmpversion
;
172 n
->mldversion
= s
->flags
& DEV_OPT_MLDVERSION
? s
->mldversion
: os
->mldversion
;
173 n
->neigh4reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
174 s
->neigh4reachabletime
: os
->neigh4reachabletime
;
175 n
->neigh6reachabletime
= s
->flags
& DEV_OPT_NEIGHREACHABLETIME
?
176 s
->neigh6reachabletime
: os
->neigh6reachabletime
;
177 n
->neigh4gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
178 s
->neigh4gcstaletime
: os
->neigh4gcstaletime
;
179 n
->neigh6gcstaletime
= s
->flags
& DEV_OPT_NEIGHGCSTALETIME
?
180 s
->neigh6gcstaletime
: os
->neigh6gcstaletime
;
181 n
->dadtransmits
= s
->flags
& DEV_OPT_DADTRANSMITS
?
182 s
->dadtransmits
: os
->dadtransmits
;
183 n
->multicast
= s
->flags
& DEV_OPT_MULTICAST
?
184 s
->multicast
: os
->multicast
;
185 n
->multicast_to_unicast
= s
->multicast_to_unicast
;
186 n
->multicast_router
= s
->multicast_router
;
187 n
->learning
= s
->learning
;
188 n
->unicast_flood
= s
->unicast_flood
;
189 n
->flags
= s
->flags
| os
->flags
| os
->valid_flags
;
193 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
195 struct device_settings
*s
= &dev
->settings
;
196 struct blob_attr
*cur
;
197 struct ether_addr
*ea
;
198 bool disabled
= false;
201 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
202 disabled
= !blobmsg_get_bool(cur
);
204 if ((cur
= tb
[DEV_ATTR_MTU
])) {
205 s
->mtu
= blobmsg_get_u32(cur
);
206 s
->flags
|= DEV_OPT_MTU
;
209 if ((cur
= tb
[DEV_ATTR_MTU6
])) {
210 s
->mtu6
= blobmsg_get_u32(cur
);
211 s
->flags
|= DEV_OPT_MTU6
;
214 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
215 s
->txqueuelen
= blobmsg_get_u32(cur
);
216 s
->flags
|= DEV_OPT_TXQUEUELEN
;
219 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
220 ea
= ether_aton(blobmsg_data(cur
));
222 memcpy(s
->macaddr
, ea
, 6);
223 s
->flags
|= DEV_OPT_MACADDR
;
227 if ((cur
= tb
[DEV_ATTR_IPV6
])) {
228 s
->ipv6
= blobmsg_get_bool(cur
);
229 s
->flags
|= DEV_OPT_IPV6
;
232 if ((cur
= tb
[DEV_ATTR_PROMISC
])) {
233 s
->promisc
= blobmsg_get_bool(cur
);
234 s
->flags
|= DEV_OPT_PROMISC
;
237 if ((cur
= tb
[DEV_ATTR_RPFILTER
])) {
238 if (system_resolve_rpfilter(blobmsg_data(cur
), &s
->rpfilter
))
239 s
->flags
|= DEV_OPT_RPFILTER
;
241 DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur
));
244 if ((cur
= tb
[DEV_ATTR_ACCEPTLOCAL
])) {
245 s
->acceptlocal
= blobmsg_get_bool(cur
);
246 s
->flags
|= DEV_OPT_ACCEPTLOCAL
;
249 if ((cur
= tb
[DEV_ATTR_IGMPVERSION
])) {
250 s
->igmpversion
= blobmsg_get_u32(cur
);
251 if (s
->igmpversion
>= 1 && s
->igmpversion
<= 3)
252 s
->flags
|= DEV_OPT_IGMPVERSION
;
254 DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur
));
257 if ((cur
= tb
[DEV_ATTR_MLDVERSION
])) {
258 s
->mldversion
= blobmsg_get_u32(cur
);
259 if (s
->mldversion
>= 1 && s
->mldversion
<= 2)
260 s
->flags
|= DEV_OPT_MLDVERSION
;
262 DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur
));
265 if ((cur
= tb
[DEV_ATTR_NEIGHREACHABLETIME
])) {
266 s
->neigh6reachabletime
= s
->neigh4reachabletime
= blobmsg_get_u32(cur
);
267 s
->flags
|= DEV_OPT_NEIGHREACHABLETIME
;
270 if ((cur
= tb
[DEV_ATTR_NEIGHGCSTALETIME
])) {
271 s
->neigh6gcstaletime
= s
->neigh4gcstaletime
= blobmsg_get_u32(cur
);
272 s
->flags
|= DEV_OPT_NEIGHGCSTALETIME
;
275 if ((cur
= tb
[DEV_ATTR_RPS
])) {
276 s
->rps
= blobmsg_get_bool(cur
);
277 s
->flags
|= DEV_OPT_RPS
;
282 if ((cur
= tb
[DEV_ATTR_XPS
])) {
283 s
->xps
= blobmsg_get_bool(cur
);
284 s
->flags
|= DEV_OPT_XPS
;
289 if ((cur
= tb
[DEV_ATTR_DADTRANSMITS
])) {
290 s
->dadtransmits
= blobmsg_get_u32(cur
);
291 s
->flags
|= DEV_OPT_DADTRANSMITS
;
294 if ((cur
= tb
[DEV_ATTR_MULTICAST_TO_UNICAST
])) {
295 s
->multicast_to_unicast
= blobmsg_get_bool(cur
);
296 s
->flags
|= DEV_OPT_MULTICAST_TO_UNICAST
;
299 if ((cur
= tb
[DEV_ATTR_MULTICAST_ROUTER
])) {
300 s
->multicast_router
= blobmsg_get_u32(cur
);
301 if (s
->multicast_router
<= 2)
302 s
->flags
|= DEV_OPT_MULTICAST_ROUTER
;
304 DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur
));
307 if ((cur
= tb
[DEV_ATTR_MULTICAST
])) {
308 s
->multicast
= blobmsg_get_bool(cur
);
309 s
->flags
|= DEV_OPT_MULTICAST
;
312 if ((cur
= tb
[DEV_ATTR_LEARNING
])) {
313 s
->learning
= blobmsg_get_bool(cur
);
314 s
->flags
|= DEV_OPT_LEARNING
;
317 if ((cur
= tb
[DEV_ATTR_UNICAST_FLOOD
])) {
318 s
->unicast_flood
= blobmsg_get_bool(cur
);
319 s
->flags
|= DEV_OPT_UNICAST_FLOOD
;
322 device_set_disabled(dev
, disabled
);
325 static void __init
dev_init(void)
327 avl_init(&devices
, avl_strcmp
, true, NULL
);
330 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
332 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
335 /* device might have been removed by an earlier callback */
344 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
348 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
349 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
352 int device_claim(struct device_user
*dep
)
354 struct device
*dev
= dep
->dev
;
364 D(DEVICE
, "Claim %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
365 if (++dev
->active
!= 1)
368 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
370 /* Get ifindex for external claimed devices so a valid */
371 /* ifindex is in place avoiding possible race conditions */
372 device_set_ifindex(dev
, system_if_resolve(dev
));
376 system_if_get_settings(dev
, &dev
->orig_settings
);
378 ret
= dev
->set_state(dev
, true);
381 device_broadcast_event(dev
, DEV_EVENT_UP
);
383 D(DEVICE
, "claim %s %s failed: %d\n", dev
->type
->name
, dev
->ifname
, ret
);
385 dep
->claimed
= false;
391 void device_release(struct device_user
*dep
)
393 struct device
*dev
= dep
->dev
;
398 dep
->claimed
= false;
400 D(DEVICE
, "Release %s %s, new active count: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
401 assert(dev
->active
>= 0);
406 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
408 dev
->set_state(dev
, false);
409 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
412 int device_check_state(struct device
*dev
)
414 if (!dev
->type
->check_state
)
415 return simple_device_type
.check_state(dev
);
417 return dev
->type
->check_state(dev
);
420 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
425 D(DEVICE
, "Initialize device '%s'\n", name
? name
: "");
426 INIT_SAFE_LIST(&dev
->users
);
427 INIT_SAFE_LIST(&dev
->aliases
);
431 device_set_ifname(dev
, name
);
434 dev
->set_state
= set_device_state
;
437 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
441 device_init_virtual(dev
, type
, ifname
);
443 dev
->avl
.key
= dev
->ifname
;
445 ret
= avl_insert(&devices
, &dev
->avl
);
449 system_if_clear_state(dev
);
450 device_check_state(dev
);
451 dev
->settings
.rps
= default_ps
;
452 dev
->settings
.xps
= default_ps
;
457 static struct device
*
458 device_create_default(const char *name
, bool external
)
462 if (!external
&& system_if_force_external(name
))
465 D(DEVICE
, "Create simple device '%s'\n", name
);
466 dev
= calloc(1, sizeof(*dev
));
470 dev
->external
= external
;
471 dev
->set_state
= simple_device_set_state
;
472 device_init(dev
, &simple_device_type
, name
);
473 dev
->default_config
= true;
475 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
480 device_get(const char *name
, int create
)
484 if (strchr(name
, '.'))
485 return get_vlan_device_chain(name
, create
);
488 return device_alias_get(name
+ 1);
490 dev
= avl_find_element(&devices
, name
, dev
, avl
);
492 if (create
> 1 && !dev
->external
) {
493 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
494 dev
->external
= true;
495 device_set_present(dev
, true);
503 return device_create_default(name
, create
> 1);
507 device_delete(struct device
*dev
)
512 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
513 avl_delete(&devices
, &dev
->avl
);
517 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
519 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
521 dep
->cb(dep
, DEV_EVENT_REMOVE
);
527 void device_cleanup(struct device
*dev
)
529 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
530 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
531 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
535 static void __device_set_present(struct device
*dev
, bool state
)
537 if (dev
->present
== state
)
540 dev
->present
= state
;
541 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
545 device_refresh_present(struct device
*dev
)
547 bool state
= dev
->sys_present
;
549 if (dev
->disabled
|| dev
->deferred
)
552 __device_set_present(dev
, state
);
555 void device_set_present(struct device
*dev
, bool state
)
557 if (dev
->sys_present
== state
)
560 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
561 dev
->sys_present
= state
;
562 device_refresh_present(dev
);
565 void device_set_link(struct device
*dev
, bool state
)
567 if (dev
->link_active
== state
)
570 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
572 dev
->link_active
= state
;
573 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
576 void device_set_ifindex(struct device
*dev
, int ifindex
)
578 if (dev
->ifindex
== ifindex
)
581 dev
->ifindex
= ifindex
;
582 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
585 int device_set_ifname(struct device
*dev
, const char *name
)
589 if (!strcmp(dev
->ifname
, name
))
593 avl_delete(&devices
, &dev
->avl
);
595 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
598 ret
= avl_insert(&devices
, &dev
->avl
);
601 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
606 static int device_refcount(struct device
*dev
)
608 struct list_head
*list
;
611 list_for_each(list
, &dev
->users
.list
)
614 list_for_each(list
, &dev
->aliases
.list
)
621 __device_add_user(struct device_user
*dep
, struct device
*dev
)
623 struct safe_list
*head
;
628 head
= &dev
->aliases
;
632 safe_list_add(&dep
->list
, head
);
633 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
635 if (dep
->cb
&& dev
->present
) {
636 dep
->cb(dep
, DEV_EVENT_ADD
);
638 dep
->cb(dep
, DEV_EVENT_UP
);
640 if (dev
->link_active
)
641 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
645 void device_add_user(struct device_user
*dep
, struct device
*dev
)
651 device_remove_user(dep
);
656 __device_add_user(dep
, dev
);
660 device_free(struct device
*dev
)
665 dev
->type
->free(dev
);
670 __device_free_unused(struct device
*dev
)
672 if (!safe_list_empty(&dev
->users
) ||
673 !safe_list_empty(&dev
->aliases
) ||
674 dev
->current_config
|| __devlock
)
680 void device_remove_user(struct device_user
*dep
)
682 struct device
*dev
= dep
->dev
;
687 dep
->hotplug
= false;
691 safe_list_del(&dep
->list
);
693 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
694 __device_free_unused(dev
);
698 device_free_unused(struct device
*dev
)
703 return __device_free_unused(dev
);
705 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
706 __device_free_unused(dev
);
710 device_init_pending(void)
712 struct device
*dev
, *tmp
;
714 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
715 if (!dev
->config_pending
)
718 dev
->type
->config_init(dev
);
719 dev
->config_pending
= false;
723 static enum dev_change_type
724 device_set_config(struct device
*dev
, const struct device_type
*type
,
725 struct blob_attr
*attr
)
727 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
728 const struct uci_blob_param_list
*cfg
= type
->config_params
;
730 if (type
!= dev
->type
)
731 return DEV_CONFIG_RECREATE
;
733 if (dev
->type
->reload
)
734 return dev
->type
->reload(dev
, attr
);
736 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
737 return DEV_CONFIG_NO_CHANGE
;
739 if (cfg
== &device_attr_list
) {
740 memset(tb
, 0, sizeof(tb
));
743 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
744 blob_data(attr
), blob_len(attr
));
746 device_init_settings(dev
, tb
);
747 return DEV_CONFIG_RESTART
;
749 return DEV_CONFIG_RECREATE
;
753 device_apply_config(struct device
*dev
, const struct device_type
*type
,
754 struct blob_attr
*config
)
756 enum dev_change_type change
;
758 change
= device_set_config(dev
, type
, config
);
760 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
761 change
= DEV_CONFIG_APPLIED
;
765 case DEV_CONFIG_RESTART
:
766 case DEV_CONFIG_APPLIED
:
767 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
768 config
= blob_memdup(config
);
770 dev
->config
= config
;
771 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
774 device_set_present(dev
, false);
775 if (dev
->active
&& !dev
->external
) {
776 ret
= dev
->set_state(dev
, false);
778 ret
= dev
->set_state(dev
, true);
781 device_set_present(dev
, true);
784 case DEV_CONFIG_NO_CHANGE
:
785 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
787 case DEV_CONFIG_RECREATE
:
795 device_replace(struct device
*dev
, struct device
*odev
)
797 struct device_user
*dep
, *tmp
;
801 device_set_present(odev
, false);
803 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
805 safe_list_del(&dep
->list
);
806 __device_add_user(dep
, dev
);
814 device_reset_config(void)
818 avl_for_each_element(&devices
, dev
, avl
)
819 dev
->current_config
= false;
823 device_reset_old(void)
825 struct device
*dev
, *tmp
, *ndev
;
827 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
828 if (dev
->current_config
|| dev
->default_config
)
831 if (dev
->type
!= &simple_device_type
)
834 ndev
= device_create_default(dev
->ifname
, dev
->external
);
838 device_replace(ndev
, dev
);
843 device_set_default_ps(bool state
)
847 if (state
== default_ps
)
852 avl_for_each_element(&devices
, dev
, avl
) {
853 struct device_settings
*s
= &dev
->settings
;
854 unsigned int apply_mask
= 0;
856 if (!(s
->flags
& DEV_OPT_RPS
)) {
858 apply_mask
|= DEV_OPT_RPS
;
861 if (!(s
->flags
& DEV_OPT_XPS
)) {
863 apply_mask
|= DEV_OPT_XPS
;
869 if (!(dev
->external
|| (dev
->present
&& dev
->active
)) ||
873 system_if_apply_settings(dev
, s
, apply_mask
);
878 device_create(const char *name
, const struct device_type
*type
,
879 struct blob_attr
*config
)
881 struct device
*odev
= NULL
, *dev
;
882 enum dev_change_type change
;
884 odev
= device_get(name
, false);
886 odev
->current_config
= true;
887 change
= device_apply_config(odev
, type
, config
);
889 case DEV_CONFIG_RECREATE
:
890 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
897 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
899 config
= blob_memdup(config
);
903 dev
= type
->create(name
, config
);
907 dev
->current_config
= true;
908 dev
->config
= config
;
910 device_replace(dev
, odev
);
912 if (!config_init
&& dev
->config_pending
) {
913 type
->config_init(dev
);
914 dev
->config_pending
= false;
921 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
923 struct device_settings st
;
927 avl_for_each_element(&devices
, dev
, avl
) {
930 c
= blobmsg_open_table(b
, dev
->ifname
);
931 device_dump_status(b
, dev
);
932 blobmsg_close_table(b
, c
);
938 blobmsg_add_u8(b
, "external", dev
->external
);
939 blobmsg_add_u8(b
, "present", dev
->present
);
940 blobmsg_add_string(b
, "type", dev
->type
->name
);
945 blobmsg_add_u8(b
, "up", !!dev
->active
);
946 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
948 if (dev
->type
->dump_info
)
949 dev
->type
->dump_info(dev
, b
);
951 system_if_dump_info(dev
, b
);
954 device_merge_settings(dev
, &st
);
955 if (st
.flags
& DEV_OPT_MTU
)
956 blobmsg_add_u32(b
, "mtu", st
.mtu
);
957 if (st
.flags
& DEV_OPT_MTU6
)
958 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
959 if (st
.flags
& DEV_OPT_MACADDR
)
960 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
961 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
962 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
963 if (st
.flags
& DEV_OPT_IPV6
)
964 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
965 if (st
.flags
& DEV_OPT_PROMISC
)
966 blobmsg_add_u8(b
, "promisc", st
.promisc
);
967 if (st
.flags
& DEV_OPT_RPFILTER
)
968 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
969 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
970 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
971 if (st
.flags
& DEV_OPT_IGMPVERSION
)
972 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
973 if (st
.flags
& DEV_OPT_MLDVERSION
)
974 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
975 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
976 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
977 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
979 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
980 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
981 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
983 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
984 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
985 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
986 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
987 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
988 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
989 if (st
.flags
& DEV_OPT_MULTICAST
)
990 blobmsg_add_u8(b
, "multicast", st
.multicast
);
991 if (st
.flags
& DEV_OPT_LEARNING
)
992 blobmsg_add_u8(b
, "learning", st
.learning
);
993 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
994 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
997 s
= blobmsg_open_table(b
, "statistics");
998 if (dev
->type
->dump_stats
)
999 dev
->type
->dump_stats(dev
, b
);
1001 system_if_dump_stats(dev
, b
);
1002 blobmsg_close_table(b
, s
);