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_find(const char *name
)
484 return avl_find_element(&devices
, name
, dev
, avl
);
488 device_get(const char *name
, int create
)
492 if (strchr(name
, '.'))
493 return get_vlan_device_chain(name
, create
);
496 return device_alias_get(name
+ 1);
498 dev
= avl_find_element(&devices
, name
, dev
, avl
);
500 if (create
> 1 && !dev
->external
) {
501 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
502 dev
->external
= true;
503 device_set_present(dev
, true);
511 return device_create_default(name
, create
> 1);
515 device_delete(struct device
*dev
)
520 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
521 avl_delete(&devices
, &dev
->avl
);
525 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
527 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
529 dep
->cb(dep
, DEV_EVENT_REMOVE
);
535 void device_cleanup(struct device
*dev
)
537 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
538 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
539 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
543 static void __device_set_present(struct device
*dev
, bool state
)
545 if (dev
->present
== state
)
548 dev
->present
= state
;
549 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
553 device_refresh_present(struct device
*dev
)
555 bool state
= dev
->sys_present
;
557 if (dev
->disabled
|| dev
->deferred
)
560 __device_set_present(dev
, state
);
563 void device_set_present(struct device
*dev
, bool state
)
565 if (dev
->sys_present
== state
)
568 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
569 dev
->sys_present
= state
;
570 device_refresh_present(dev
);
573 void device_set_link(struct device
*dev
, bool state
)
575 if (dev
->link_active
== state
)
578 netifd_log_message(L_NOTICE
, "%s '%s' link is %s\n", dev
->type
->name
, dev
->ifname
, state
? "up" : "down" );
580 dev
->link_active
= state
;
581 device_broadcast_event(dev
, state
? DEV_EVENT_LINK_UP
: DEV_EVENT_LINK_DOWN
);
584 void device_set_ifindex(struct device
*dev
, int ifindex
)
586 if (dev
->ifindex
== ifindex
)
589 dev
->ifindex
= ifindex
;
590 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFINDEX
);
593 int device_set_ifname(struct device
*dev
, const char *name
)
597 if (!strcmp(dev
->ifname
, name
))
601 avl_delete(&devices
, &dev
->avl
);
603 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
606 ret
= avl_insert(&devices
, &dev
->avl
);
609 device_broadcast_event(dev
, DEV_EVENT_UPDATE_IFNAME
);
614 static int device_refcount(struct device
*dev
)
616 struct list_head
*list
;
619 list_for_each(list
, &dev
->users
.list
)
622 list_for_each(list
, &dev
->aliases
.list
)
629 __device_add_user(struct device_user
*dep
, struct device
*dev
)
631 struct safe_list
*head
;
636 head
= &dev
->aliases
;
640 safe_list_add(&dep
->list
, head
);
641 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
643 if (dep
->cb
&& dev
->present
) {
644 dep
->cb(dep
, DEV_EVENT_ADD
);
646 dep
->cb(dep
, DEV_EVENT_UP
);
648 if (dev
->link_active
)
649 dep
->cb(dep
, DEV_EVENT_LINK_UP
);
653 void device_add_user(struct device_user
*dep
, struct device
*dev
)
659 device_remove_user(dep
);
664 __device_add_user(dep
, dev
);
668 device_free(struct device
*dev
)
673 dev
->type
->free(dev
);
678 __device_free_unused(struct device
*dev
)
680 if (!safe_list_empty(&dev
->users
) ||
681 !safe_list_empty(&dev
->aliases
) ||
682 dev
->current_config
|| __devlock
)
688 void device_remove_user(struct device_user
*dep
)
690 struct device
*dev
= dep
->dev
;
695 dep
->hotplug
= false;
699 safe_list_del(&dep
->list
);
701 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
702 __device_free_unused(dev
);
706 device_free_unused(struct device
*dev
)
711 return __device_free_unused(dev
);
713 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
714 __device_free_unused(dev
);
718 device_init_pending(void)
720 struct device
*dev
, *tmp
;
722 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
723 if (!dev
->config_pending
)
726 dev
->type
->config_init(dev
);
727 dev
->config_pending
= false;
731 static enum dev_change_type
732 device_set_config(struct device
*dev
, const struct device_type
*type
,
733 struct blob_attr
*attr
)
735 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
736 const struct uci_blob_param_list
*cfg
= type
->config_params
;
738 if (type
!= dev
->type
)
739 return DEV_CONFIG_RECREATE
;
741 if (dev
->type
->reload
)
742 return dev
->type
->reload(dev
, attr
);
744 if (uci_blob_check_equal(dev
->config
, attr
, cfg
))
745 return DEV_CONFIG_NO_CHANGE
;
747 if (cfg
== &device_attr_list
) {
748 memset(tb
, 0, sizeof(tb
));
751 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
752 blob_data(attr
), blob_len(attr
));
754 device_init_settings(dev
, tb
);
755 return DEV_CONFIG_RESTART
;
757 return DEV_CONFIG_RECREATE
;
761 device_apply_config(struct device
*dev
, const struct device_type
*type
,
762 struct blob_attr
*config
)
764 enum dev_change_type change
;
766 change
= device_set_config(dev
, type
, config
);
768 system_if_apply_settings(dev
, &dev
->settings
, dev
->settings
.flags
);
769 change
= DEV_CONFIG_APPLIED
;
773 case DEV_CONFIG_RESTART
:
774 case DEV_CONFIG_APPLIED
:
775 D(DEVICE
, "Device '%s': config applied\n", dev
->ifname
);
776 config
= blob_memdup(config
);
778 dev
->config
= config
;
779 if (change
== DEV_CONFIG_RESTART
&& dev
->present
) {
782 device_set_present(dev
, false);
783 if (dev
->active
&& !dev
->external
) {
784 ret
= dev
->set_state(dev
, false);
786 ret
= dev
->set_state(dev
, true);
789 device_set_present(dev
, true);
792 case DEV_CONFIG_NO_CHANGE
:
793 D(DEVICE
, "Device '%s': no configuration change\n", dev
->ifname
);
795 case DEV_CONFIG_RECREATE
:
803 device_replace(struct device
*dev
, struct device
*odev
)
805 struct device_user
*dep
, *tmp
;
809 device_set_present(odev
, false);
811 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
813 safe_list_del(&dep
->list
);
814 __device_add_user(dep
, dev
);
822 device_reset_config(void)
826 avl_for_each_element(&devices
, dev
, avl
)
827 dev
->current_config
= false;
831 device_reset_old(void)
833 struct device
*dev
, *tmp
, *ndev
;
835 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
836 if (dev
->current_config
|| dev
->default_config
)
839 if (dev
->type
!= &simple_device_type
)
842 ndev
= device_create_default(dev
->ifname
, dev
->external
);
846 device_replace(ndev
, dev
);
851 device_set_default_ps(bool state
)
855 if (state
== default_ps
)
860 avl_for_each_element(&devices
, dev
, avl
) {
861 struct device_settings
*s
= &dev
->settings
;
862 unsigned int apply_mask
= 0;
864 if (!(s
->flags
& DEV_OPT_RPS
)) {
866 apply_mask
|= DEV_OPT_RPS
;
869 if (!(s
->flags
& DEV_OPT_XPS
)) {
871 apply_mask
|= DEV_OPT_XPS
;
877 if (!(dev
->external
|| (dev
->present
&& dev
->active
)) ||
881 system_if_apply_settings(dev
, s
, apply_mask
);
886 device_create(const char *name
, const struct device_type
*type
,
887 struct blob_attr
*config
)
889 struct device
*odev
= NULL
, *dev
;
890 enum dev_change_type change
;
892 odev
= device_get(name
, false);
894 odev
->current_config
= true;
895 change
= device_apply_config(odev
, type
, config
);
897 case DEV_CONFIG_RECREATE
:
898 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
905 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
907 config
= blob_memdup(config
);
911 dev
= type
->create(name
, config
);
915 dev
->current_config
= true;
916 dev
->config
= config
;
918 device_replace(dev
, odev
);
920 if (!config_init
&& dev
->config_pending
) {
921 type
->config_init(dev
);
922 dev
->config_pending
= false;
929 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
931 struct device_settings st
;
935 avl_for_each_element(&devices
, dev
, avl
) {
938 c
= blobmsg_open_table(b
, dev
->ifname
);
939 device_dump_status(b
, dev
);
940 blobmsg_close_table(b
, c
);
946 blobmsg_add_u8(b
, "external", dev
->external
);
947 blobmsg_add_u8(b
, "present", dev
->present
);
948 blobmsg_add_string(b
, "type", dev
->type
->name
);
953 blobmsg_add_u8(b
, "up", !!dev
->active
);
954 blobmsg_add_u8(b
, "carrier", !!dev
->link_active
);
956 if (dev
->type
->dump_info
)
957 dev
->type
->dump_info(dev
, b
);
959 system_if_dump_info(dev
, b
);
962 device_merge_settings(dev
, &st
);
963 if (st
.flags
& DEV_OPT_MTU
)
964 blobmsg_add_u32(b
, "mtu", st
.mtu
);
965 if (st
.flags
& DEV_OPT_MTU6
)
966 blobmsg_add_u32(b
, "mtu6", st
.mtu6
);
967 if (st
.flags
& DEV_OPT_MACADDR
)
968 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
969 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
970 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
971 if (st
.flags
& DEV_OPT_IPV6
)
972 blobmsg_add_u8(b
, "ipv6", st
.ipv6
);
973 if (st
.flags
& DEV_OPT_PROMISC
)
974 blobmsg_add_u8(b
, "promisc", st
.promisc
);
975 if (st
.flags
& DEV_OPT_RPFILTER
)
976 blobmsg_add_u32(b
, "rpfilter", st
.rpfilter
);
977 if (st
.flags
& DEV_OPT_ACCEPTLOCAL
)
978 blobmsg_add_u8(b
, "acceptlocal", st
.acceptlocal
);
979 if (st
.flags
& DEV_OPT_IGMPVERSION
)
980 blobmsg_add_u32(b
, "igmpversion", st
.igmpversion
);
981 if (st
.flags
& DEV_OPT_MLDVERSION
)
982 blobmsg_add_u32(b
, "mldversion", st
.mldversion
);
983 if (st
.flags
& DEV_OPT_NEIGHREACHABLETIME
) {
984 blobmsg_add_u32(b
, "neigh4reachabletime", st
.neigh4reachabletime
);
985 blobmsg_add_u32(b
, "neigh6reachabletime", st
.neigh6reachabletime
);
987 if (st
.flags
& DEV_OPT_NEIGHGCSTALETIME
) {
988 blobmsg_add_u32(b
, "neigh4gcstaletime", st
.neigh4gcstaletime
);
989 blobmsg_add_u32(b
, "neigh6gcstaletime", st
.neigh6gcstaletime
);
991 if (st
.flags
& DEV_OPT_DADTRANSMITS
)
992 blobmsg_add_u32(b
, "dadtransmits", st
.dadtransmits
);
993 if (st
.flags
& DEV_OPT_MULTICAST_TO_UNICAST
)
994 blobmsg_add_u8(b
, "multicast_to_unicast", st
.multicast_to_unicast
);
995 if (st
.flags
& DEV_OPT_MULTICAST_ROUTER
)
996 blobmsg_add_u32(b
, "multicast_router", st
.multicast_router
);
997 if (st
.flags
& DEV_OPT_MULTICAST
)
998 blobmsg_add_u8(b
, "multicast", st
.multicast
);
999 if (st
.flags
& DEV_OPT_LEARNING
)
1000 blobmsg_add_u8(b
, "learning", st
.learning
);
1001 if (st
.flags
& DEV_OPT_UNICAST_FLOOD
)
1002 blobmsg_add_u8(b
, "unicast_flood", st
.unicast_flood
);
1005 s
= blobmsg_open_table(b
, "statistics");
1006 if (dev
->type
->dump_stats
)
1007 dev
->type
->dump_stats(dev
, b
);
1009 system_if_dump_stats(dev
, b
);
1010 blobmsg_close_table(b
, s
);