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
);
184 static void __device_broadcast_event(struct list_head
*head
, enum device_event ev
)
186 struct device_user
*dep
;
187 static uint8_t idx
[__DEV_EVENT_MAX
];
194 list_for_each_entry(dep
, head
, list
) {
198 if (dep
->ev_idx
[ev
] == idx
[ev
])
202 dep
->ev_idx
[ev
] = idx
[ev
];
209 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
211 __device_broadcast_event(&dev
->aliases
, ev
);
212 __device_broadcast_event(&dev
->users
, ev
);
215 int device_claim(struct device_user
*dep
)
217 struct device
*dev
= dep
->dev
;
224 D(DEVICE
, "Claim %s %s, new refcount: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
225 if (++dev
->active
!= 1)
228 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
229 ret
= dev
->set_state(dev
, true);
231 device_broadcast_event(dev
, DEV_EVENT_UP
);
233 D(DEVICE
, "claim device %s failed: %d\n", dev
->ifname
, ret
);
235 dep
->claimed
= false;
241 void device_release(struct device_user
*dep
)
243 struct device
*dev
= dep
->dev
;
248 dep
->claimed
= false;
250 D(DEVICE
, "Release %s %s, new refcount: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
251 assert(dev
->active
>= 0);
256 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
258 dev
->set_state(dev
, false);
259 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
262 int device_check_state(struct device
*dev
)
264 if (!dev
->type
->check_state
)
267 return dev
->type
->check_state(dev
);
270 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
276 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
278 D(DEVICE
, "Initialize device '%s'\n", dev
->ifname
);
279 INIT_LIST_HEAD(&dev
->users
);
280 INIT_LIST_HEAD(&dev
->aliases
);
284 dev
->set_state
= set_device_state
;
287 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
291 device_init_virtual(dev
, type
, ifname
);
293 dev
->avl
.key
= dev
->ifname
;
295 ret
= avl_insert(&devices
, &dev
->avl
);
299 system_if_clear_state(dev
);
300 device_check_state(dev
);
305 static struct device
*
306 device_create_default(const char *name
, bool external
)
310 if (!external
&& system_if_force_external(name
))
313 D(DEVICE
, "Create simple device '%s'\n", name
);
314 dev
= calloc(1, sizeof(*dev
));
315 dev
->external
= external
;
316 dev
->set_state
= simple_device_set_state
;
317 device_init(dev
, &simple_device_type
, name
);
318 dev
->default_config
= true;
323 device_get(const char *name
, int create
)
327 if (strchr(name
, '.'))
328 return get_vlan_device_chain(name
, create
);
331 return device_alias_get(name
+ 1);
333 dev
= avl_find_element(&devices
, name
, dev
, avl
);
335 if (create
> 1 && !dev
->external
) {
336 dev
->external
= true;
337 device_set_present(dev
, true);
345 return device_create_default(name
, create
> 1);
349 device_delete(struct device
*dev
)
354 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
355 avl_delete(&devices
, &dev
->avl
);
359 void device_cleanup(struct device
*dev
)
361 struct device_user
*dep
, *tmp
;
363 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
364 list_for_each_entry_safe(dep
, tmp
, &dev
->users
, list
) {
368 dep
->cb(dep
, DEV_EVENT_REMOVE
);
375 static void __device_set_present(struct device
*dev
, bool state
)
377 if (dev
->present
== state
)
380 dev
->present
= state
;
381 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
385 device_refresh_present(struct device
*dev
)
387 bool state
= dev
->sys_present
;
389 if (dev
->disabled
|| dev
->deferred
)
392 __device_set_present(dev
, state
);
395 void device_set_present(struct device
*dev
, bool state
)
397 if (dev
->sys_present
== state
)
400 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
401 dev
->sys_present
= state
;
402 device_refresh_present(dev
);
405 static int device_refcount(struct device
*dev
)
407 struct list_head
*list
;
410 list_for_each(list
, &dev
->users
)
416 void device_add_user(struct device_user
*dep
, struct device
*dev
)
418 struct list_head
*head
;
421 device_remove_user(dep
);
429 head
= &dev
->aliases
;
432 list_add_tail(&dep
->list
, head
);
433 D(DEVICE
, "Add user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
435 if (dep
->cb
&& dev
->present
) {
436 dep
->cb(dep
, DEV_EVENT_ADD
);
438 dep
->cb(dep
, DEV_EVENT_UP
);
443 device_free(struct device
*dev
)
448 dev
->type
->free(dev
);
453 __device_free_unused(struct device
*dev
)
455 if (!list_empty(&dev
->users
) || dev
->current_config
|| __devlock
)
461 void device_remove_user(struct device_user
*dep
)
463 struct device
*dev
= dep
->dev
;
468 dep
->hotplug
= false;
472 list_del(&dep
->list
);
474 D(DEVICE
, "Remove user for device '%s', refcount=%d\n", dev
->ifname
, device_refcount(dev
));
475 __device_free_unused(dev
);
479 device_free_unused(struct device
*dev
)
484 return __device_free_unused(dev
);
486 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
487 __device_free_unused(dev
);
491 device_init_pending(void)
493 struct device
*dev
, *tmp
;
495 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
496 if (!dev
->config_pending
)
499 dev
->type
->config_init(dev
);
500 dev
->config_pending
= false;
504 static enum dev_change_type
505 device_reload_config(struct device
*dev
, struct blob_attr
*attr
)
507 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
508 const struct config_param_list
*cfg
= dev
->type
->config_params
;
510 if (config_check_equal(dev
->config
, attr
, cfg
))
511 return DEV_CONFIG_NO_CHANGE
;
513 if (cfg
== &device_attr_list
) {
514 memset(tb
, 0, sizeof(tb
));
517 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
518 blob_data(attr
), blob_len(attr
));
520 device_init_settings(dev
, tb
);
521 return DEV_CONFIG_RESTART
;
523 return DEV_CONFIG_RECREATE
;
527 device_set_config(struct device
*dev
, const struct device_type
*type
,
528 struct blob_attr
*attr
)
530 if (type
!= dev
->type
)
531 return DEV_CONFIG_RECREATE
;
533 if (dev
->type
->reload
)
534 return dev
->type
->reload(dev
, attr
);
536 return device_reload_config(dev
, attr
);
540 device_replace(struct device
*dev
, struct device
*odev
)
542 struct device_user
*dep
, *tmp
;
543 bool present
= odev
->present
;
546 device_set_present(odev
, false);
548 list_for_each_entry_safe(dep
, tmp
, &odev
->users
, list
) {
550 list_move_tail(&dep
->list
, &dev
->users
);
556 device_set_present(dev
, true);
560 device_reset_config(void)
564 avl_for_each_element(&devices
, dev
, avl
)
565 dev
->current_config
= false;
569 device_reset_old(void)
571 struct device
*dev
, *tmp
, *ndev
;
573 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
574 if (dev
->current_config
|| dev
->default_config
)
577 if (dev
->type
!= &simple_device_type
)
580 ndev
= device_create_default(dev
->ifname
, dev
->external
);
581 device_replace(ndev
, dev
);
586 device_create(const char *name
, const struct device_type
*type
,
587 struct blob_attr
*config
)
589 struct device
*odev
= NULL
, *dev
;
590 enum dev_change_type change
;
592 config
= config_memdup(config
);
596 odev
= device_get(name
, false);
598 odev
->current_config
= true;
599 change
= device_set_config(odev
, type
, config
);
600 if (odev
->external
) {
601 system_if_apply_settings(odev
, &odev
->settings
);
602 change
= DEV_CONFIG_APPLIED
;
605 case DEV_CONFIG_RESTART
:
606 case DEV_CONFIG_APPLIED
:
607 D(DEVICE
, "Device '%s': config applied\n", odev
->ifname
);
609 odev
->config
= config
;
610 if (change
== DEV_CONFIG_RESTART
&& odev
->present
) {
611 device_set_present(odev
, false);
612 device_set_present(odev
, true);
615 case DEV_CONFIG_NO_CHANGE
:
616 D(DEVICE
, "Device '%s': no configuration change\n", odev
->ifname
);
619 case DEV_CONFIG_RECREATE
:
620 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
625 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
627 dev
= type
->create(name
, config
);
631 dev
->current_config
= true;
632 dev
->config
= config
;
634 device_replace(dev
, odev
);
636 if (!config_init
&& dev
->config_pending
)
637 type
->config_init(dev
);
643 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
645 struct device_settings st
;
649 avl_for_each_element(&devices
, dev
, avl
) {
652 c
= blobmsg_open_table(b
, dev
->ifname
);
653 device_dump_status(b
, dev
);
654 blobmsg_close_table(b
, c
);
660 blobmsg_add_u8(b
, "external", dev
->external
);
661 blobmsg_add_u8(b
, "present", dev
->present
);
662 blobmsg_add_string(b
, "type", dev
->type
->name
);
667 blobmsg_add_u8(b
, "up", !!dev
->active
);
668 if (dev
->type
->dump_info
)
669 dev
->type
->dump_info(dev
, b
);
671 system_if_dump_info(dev
, b
);
674 device_merge_settings(dev
, &st
);
675 if (st
.flags
& DEV_OPT_MTU
)
676 blobmsg_add_u32(b
, "mtu", st
.mtu
);
677 if (st
.flags
& DEV_OPT_MACADDR
)
678 blobmsg_add_string(b
, "macaddr", format_macaddr(st
.macaddr
));
679 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
680 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
683 s
= blobmsg_open_table(b
, "statistics");
684 if (dev
->type
->dump_stats
)
685 dev
->type
->dump_stats(dev
, b
);
687 system_if_dump_stats(dev
, b
);
688 blobmsg_close_table(b
, s
);