2 * netifd - network interface daemon
3 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <net/ethernet.h>
24 #include <netinet/ether.h>
31 static struct avl_tree devices
;
33 static const struct blobmsg_policy dev_attrs
[__DEV_ATTR_MAX
] = {
34 [DEV_ATTR_TYPE
] = { "type", BLOBMSG_TYPE_STRING
},
35 [DEV_ATTR_IFNAME
] = { "ifname", BLOBMSG_TYPE_ARRAY
},
36 [DEV_ATTR_MTU
] = { "mtu", BLOBMSG_TYPE_INT32
},
37 [DEV_ATTR_MACADDR
] = { "macaddr", BLOBMSG_TYPE_STRING
},
38 [DEV_ATTR_TXQUEUELEN
] = { "txqueuelen", BLOBMSG_TYPE_INT32
},
39 [DEV_ATTR_ENABLED
] = { "enabled", BLOBMSG_TYPE_BOOL
},
42 const struct config_param_list device_attr_list
= {
43 .n_params
= __DEV_ATTR_MAX
,
47 static int __devlock
= 0;
49 void device_lock(void)
54 void device_unlock(void)
58 device_free_unused(NULL
);
61 static int set_device_state(struct device
*dev
, bool state
)
72 simple_device_set_state(struct device
*dev
, bool state
)
77 pdev
= dev
->parent
.dev
;
79 pdev
= system_if_get_parent(dev
);
81 device_add_user(&dev
->parent
, pdev
);
86 ret
= device_claim(&dev
->parent
);
88 device_release(&dev
->parent
);
93 return set_device_state(dev
, state
);
96 static struct device
*
97 simple_device_create(const char *name
, struct blob_attr
*attr
)
99 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
100 struct device
*dev
= NULL
;
102 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
, blob_data(attr
), blob_len(attr
));
103 dev
= device_get(name
, true);
107 dev
->set_state
= simple_device_set_state
;
108 device_init_settings(dev
, tb
);
113 static void simple_device_free(struct device
*dev
)
116 device_remove_user(&dev
->parent
);
120 const struct device_type simple_device_type
= {
121 .name
= "Network device",
122 .config_params
= &device_attr_list
,
124 .create
= simple_device_create
,
125 .check_state
= system_if_check
,
126 .free
= simple_device_free
,
130 device_merge_settings(struct device
*dev
, struct device_settings
*n
)
132 struct device_settings
*os
= &dev
->orig_settings
;
133 struct device_settings
*s
= &dev
->settings
;
135 memset(n
, 0, sizeof(*n
));
136 n
->mtu
= s
->flags
& DEV_OPT_MTU
? s
->mtu
: os
->mtu
;
137 n
->txqueuelen
= s
->flags
& DEV_OPT_TXQUEUELEN
?
138 s
->txqueuelen
: os
->txqueuelen
;
140 (s
->flags
& DEV_OPT_MACADDR
? s
->macaddr
: os
->macaddr
),
142 n
->flags
= s
->flags
| os
->flags
;
146 device_init_settings(struct device
*dev
, struct blob_attr
**tb
)
148 struct device_settings
*s
= &dev
->settings
;
149 struct blob_attr
*cur
;
150 struct ether_addr
*ea
;
151 bool disabled
= false;
154 if ((cur
= tb
[DEV_ATTR_ENABLED
]))
155 disabled
= !blobmsg_get_bool(cur
);
157 if ((cur
= tb
[DEV_ATTR_MTU
])) {
158 s
->mtu
= blobmsg_get_u32(cur
);
159 s
->flags
|= DEV_OPT_MTU
;
162 if ((cur
= tb
[DEV_ATTR_TXQUEUELEN
])) {
163 s
->txqueuelen
= blobmsg_get_u32(cur
);
164 s
->flags
|= DEV_OPT_TXQUEUELEN
;
167 if ((cur
= tb
[DEV_ATTR_MACADDR
])) {
168 ea
= ether_aton(blobmsg_data(cur
));
170 memcpy(s
->macaddr
, ea
, 6);
171 s
->flags
|= DEV_OPT_MACADDR
;
175 device_set_disabled(dev
, disabled
);
178 static void __init
dev_init(void)
180 avl_init(&devices
, avl_strcmp
, true, NULL
);
183 static int device_broadcast_cb(void *ctx
, struct safe_list
*list
)
185 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
193 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
197 safe_list_for_each(&dev
->aliases
, device_broadcast_cb
, &dev_ev
);
198 safe_list_for_each(&dev
->users
, device_broadcast_cb
, &dev_ev
);
201 int device_claim(struct device_user
*dep
)
203 struct device
*dev
= dep
->dev
;
210 D(DEVICE
, "Claim %s %s, new refcount: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
211 if (++dev
->active
!= 1)
214 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
215 ret
= dev
->set_state(dev
, true);
217 device_broadcast_event(dev
, DEV_EVENT_UP
);
219 D(DEVICE
, "claim device %s failed: %d\n", dev
->ifname
, ret
);
221 dep
->claimed
= false;
227 void device_release(struct device_user
*dep
)
229 struct device
*dev
= dep
->dev
;
234 dep
->claimed
= false;
236 D(DEVICE
, "Release %s %s, new refcount: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
237 assert(dev
->active
>= 0);
242 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
244 dev
->set_state(dev
, false);
245 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
248 int device_check_state(struct device
*dev
)
250 if (!dev
->type
->check_state
)
253 return dev
->type
->check_state(dev
);
256 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
262 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
264 D(DEVICE
, "Initialize device '%s'\n", dev
->ifname
);
265 INIT_SAFE_LIST(&dev
->users
);
266 INIT_SAFE_LIST(&dev
->aliases
);
270 dev
->set_state
= set_device_state
;
273 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
277 device_init_virtual(dev
, type
, ifname
);
279 dev
->avl
.key
= dev
->ifname
;
281 ret
= avl_insert(&devices
, &dev
->avl
);
285 system_if_clear_state(dev
);
286 device_check_state(dev
);
291 static struct device
*
292 device_create_default(const char *name
, bool external
)
296 if (!external
&& system_if_force_external(name
))
299 D(DEVICE
, "Create simple device '%s'\n", name
);
300 dev
= calloc(1, sizeof(*dev
));
301 dev
->external
= external
;
302 dev
->set_state
= simple_device_set_state
;
303 device_init(dev
, &simple_device_type
, name
);
304 dev
->default_config
= true;
309 device_get(const char *name
, int create
)
313 if (strchr(name
, '.'))
314 return get_vlan_device_chain(name
, create
);
317 return device_alias_get(name
+ 1);
319 dev
= avl_find_element(&devices
, name
, dev
, avl
);
321 if (create
> 1 && !dev
->external
) {
322 dev
->external
= true;
323 device_set_present(dev
, true);
331 return device_create_default(name
, create
> 1);
335 device_delete(struct device
*dev
)
340 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
341 avl_delete(&devices
, &dev
->avl
);
345 static int device_cleanup_cb(void *ctx
, struct safe_list
*list
)
347 struct device_user
*dep
= container_of(list
, struct device_user
, list
);
349 dep
->cb(dep
, DEV_EVENT_REMOVE
);
355 void device_cleanup(struct device
*dev
)
357 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
358 safe_list_for_each(&dev
->users
, device_cleanup_cb
, NULL
);
359 safe_list_for_each(&dev
->aliases
, device_cleanup_cb
, NULL
);
363 static void __device_set_present(struct device
*dev
, bool state
)
365 if (dev
->present
== state
)
368 dev
->present
= state
;
369 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
373 device_refresh_present(struct device
*dev
)
375 bool state
= dev
->sys_present
;
377 if (dev
->disabled
|| dev
->deferred
)
380 __device_set_present(dev
, state
);
383 void device_set_present(struct device
*dev
, bool state
)
385 if (dev
->sys_present
== state
)
388 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
389 dev
->sys_present
= state
;
390 device_refresh_present(dev
);
393 static int device_refcount(struct device
*dev
)
395 struct list_head
*list
;
398 list_for_each(list
, &dev
->users
.list
)
401 list_for_each(list
, &dev
->aliases
.list
)
407 void device_add_user(struct device_user
*dep
, struct device
*dev
)
409 struct safe_list
*head
;
412 device_remove_user(dep
);
420 head
= &dev
->aliases
;
424 safe_list_add(&dep
->list
, head
);
425 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
427 if (dep
->cb
&& dev
->present
) {
428 dep
->cb(dep
, DEV_EVENT_ADD
);
430 dep
->cb(dep
, DEV_EVENT_UP
);
435 device_free(struct device
*dev
)
440 dev
->type
->free(dev
);
445 __device_free_unused(struct device
*dev
)
447 if (!safe_list_empty(&dev
->users
) ||
448 !safe_list_empty(&dev
->aliases
) ||
449 dev
->current_config
|| __devlock
)
455 void device_remove_user(struct device_user
*dep
)
457 struct device
*dev
= dep
->dev
;
462 dep
->hotplug
= false;
466 safe_list_del(&dep
->list
);
468 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
469 __device_free_unused(dev
);
473 device_free_unused(struct device
*dev
)
478 return __device_free_unused(dev
);
480 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
481 __device_free_unused(dev
);
485 device_init_pending(void)
487 struct device
*dev
, *tmp
;
489 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
490 if (!dev
->config_pending
)
493 dev
->type
->config_init(dev
);
494 dev
->config_pending
= false;
498 static enum dev_change_type
499 device_reload_config(struct device
*dev
, struct blob_attr
*attr
)
501 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
502 const struct config_param_list
*cfg
= dev
->type
->config_params
;
504 if (config_check_equal(dev
->config
, attr
, cfg
))
505 return DEV_CONFIG_NO_CHANGE
;
507 if (cfg
== &device_attr_list
) {
508 memset(tb
, 0, sizeof(tb
));
511 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
512 blob_data(attr
), blob_len(attr
));
514 device_init_settings(dev
, tb
);
515 return DEV_CONFIG_RESTART
;
517 return DEV_CONFIG_RECREATE
;
521 device_set_config(struct device
*dev
, const struct device_type
*type
,
522 struct blob_attr
*attr
)
524 if (type
!= dev
->type
)
525 return DEV_CONFIG_RECREATE
;
527 if (dev
->type
->reload
)
528 return dev
->type
->reload(dev
, attr
);
530 return device_reload_config(dev
, attr
);
534 device_replace(struct device
*dev
, struct device
*odev
)
536 struct device_user
*dep
, *tmp
;
537 bool present
= odev
->present
;
540 device_set_present(odev
, false);
542 list_for_each_entry_safe(dep
, tmp
, &odev
->users
.list
, list
.list
) {
544 safe_list_del(&dep
->list
);
545 safe_list_add(&dep
->list
, &dev
->users
);
551 device_set_present(dev
, true);
555 device_reset_config(void)
559 avl_for_each_element(&devices
, dev
, avl
)
560 dev
->current_config
= false;
564 device_reset_old(void)
566 struct device
*dev
, *tmp
, *ndev
;
568 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
569 if (dev
->current_config
|| dev
->default_config
)
572 if (dev
->type
!= &simple_device_type
)
575 ndev
= device_create_default(dev
->ifname
, dev
->external
);
576 device_replace(ndev
, dev
);
581 device_create(const char *name
, const struct device_type
*type
,
582 struct blob_attr
*config
)
584 struct device
*odev
= NULL
, *dev
;
585 enum dev_change_type change
;
587 config
= config_memdup(config
);
591 odev
= device_get(name
, false);
593 odev
->current_config
= true;
594 change
= device_set_config(odev
, type
, config
);
595 if (odev
->external
) {
596 system_if_apply_settings(odev
, &odev
->settings
);
597 change
= DEV_CONFIG_APPLIED
;
600 case DEV_CONFIG_RESTART
:
601 case DEV_CONFIG_APPLIED
:
602 D(DEVICE
, "Device '%s': config applied\n", odev
->ifname
);
604 odev
->config
= config
;
605 if (change
== DEV_CONFIG_RESTART
&& odev
->present
) {
606 device_set_present(odev
, false);
607 device_set_present(odev
, true);
610 case DEV_CONFIG_NO_CHANGE
:
611 D(DEVICE
, "Device '%s': no configuration change\n", odev
->ifname
);
614 case DEV_CONFIG_RECREATE
:
615 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
620 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
622 dev
= type
->create(name
, config
);
626 dev
->current_config
= true;
627 dev
->config
= config
;
629 device_replace(dev
, odev
);
631 if (!config_init
&& dev
->config_pending
)
632 type
->config_init(dev
);
638 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
640 struct device_settings st
;
644 avl_for_each_element(&devices
, dev
, avl
) {
647 c
= blobmsg_open_table(b
, dev
->ifname
);
648 device_dump_status(b
, dev
);
649 blobmsg_close_table(b
, c
);
655 blobmsg_add_u8(b
, "external", dev
->external
);
656 blobmsg_add_u8(b
, "present", dev
->present
);
657 blobmsg_add_string(b
, "type", dev
->type
->name
);
662 blobmsg_add_u8(b
, "up", !!dev
->active
);
663 if (dev
->type
->dump_info
)
664 dev
->type
->dump_info(dev
, b
);
666 system_if_dump_info(dev
, b
);
669 device_merge_settings(dev
, &st
);
670 if (st
.flags
& DEV_OPT_MTU
)
671 blobmsg_add_u32(b
, "mtu", st
.mtu
);
672 if (st
.flags
& DEV_OPT_MACADDR
)
673 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
674 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
675 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
678 s
= blobmsg_open_table(b
, "statistics");
679 if (dev
->type
->dump_stats
)
680 dev
->type
->dump_stats(dev
, b
);
682 system_if_dump_stats(dev
, b
);
683 blobmsg_close_table(b
, s
);