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 void device_broadcast_event(struct device
*dev
, enum device_event ev
)
185 struct device_user
*dep
, *tmp
;
187 list_for_each_entry_safe(dep
, tmp
, &dev
->users
, list
) {
195 int device_claim(struct device_user
*dep
)
197 struct device
*dev
= dep
->dev
;
204 D(DEVICE
, "Claim %s %s, new refcount: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
+ 1);
205 if (++dev
->active
!= 1)
208 device_broadcast_event(dev
, DEV_EVENT_SETUP
);
209 ret
= dev
->set_state(dev
, true);
211 device_broadcast_event(dev
, DEV_EVENT_UP
);
213 D(DEVICE
, "claim device %s failed: %d\n", dev
->ifname
, ret
);
215 dep
->claimed
= false;
221 void device_release(struct device_user
*dep
)
223 struct device
*dev
= dep
->dev
;
228 dep
->claimed
= false;
230 D(DEVICE
, "Release %s %s, new refcount: %d\n", dev
->type
->name
, dev
->ifname
, dev
->active
);
231 assert(dev
->active
>= 0);
236 device_broadcast_event(dev
, DEV_EVENT_TEARDOWN
);
238 dev
->set_state(dev
, false);
239 device_broadcast_event(dev
, DEV_EVENT_DOWN
);
242 int device_check_state(struct device
*dev
)
244 if (!dev
->type
->check_state
)
247 return dev
->type
->check_state(dev
);
250 void device_init_virtual(struct device
*dev
, const struct device_type
*type
, const char *name
)
256 strncpy(dev
->ifname
, name
, IFNAMSIZ
);
258 D(DEVICE
, "Initialize device '%s'\n", dev
->ifname
);
259 INIT_LIST_HEAD(&dev
->users
);
263 dev
->set_state
= set_device_state
;
266 int device_init(struct device
*dev
, const struct device_type
*type
, const char *ifname
)
270 device_init_virtual(dev
, type
, ifname
);
272 dev
->avl
.key
= dev
->ifname
;
274 ret
= avl_insert(&devices
, &dev
->avl
);
278 system_if_clear_state(dev
);
279 device_check_state(dev
);
284 static struct device
*
285 device_create_default(const char *name
, bool external
)
289 if (!external
&& system_if_force_external(name
))
292 D(DEVICE
, "Create simple device '%s'\n", name
);
293 dev
= calloc(1, sizeof(*dev
));
294 dev
->external
= external
;
295 dev
->set_state
= simple_device_set_state
;
296 device_init(dev
, &simple_device_type
, name
);
297 dev
->default_config
= true;
302 device_get(const char *name
, int create
)
306 if (strchr(name
, '.'))
307 return get_vlan_device_chain(name
, create
);
310 return device_alias_get(name
+ 1);
312 dev
= avl_find_element(&devices
, name
, dev
, avl
);
319 return device_create_default(name
, create
> 1);
323 device_delete(struct device
*dev
)
328 D(DEVICE
, "Delete device '%s' from list\n", dev
->ifname
);
329 avl_delete(&devices
, &dev
->avl
);
333 void device_cleanup(struct device
*dev
)
335 struct device_user
*dep
, *tmp
;
337 D(DEVICE
, "Clean up device '%s'\n", dev
->ifname
);
338 list_for_each_entry_safe(dep
, tmp
, &dev
->users
, list
) {
342 dep
->cb(dep
, DEV_EVENT_REMOVE
);
349 static void __device_set_present(struct device
*dev
, bool state
)
351 if (dev
->present
== state
)
354 dev
->present
= state
;
355 device_broadcast_event(dev
, state
? DEV_EVENT_ADD
: DEV_EVENT_REMOVE
);
359 device_refresh_present(struct device
*dev
)
361 bool state
= dev
->sys_present
;
363 if (dev
->disabled
|| dev
->deferred
)
366 __device_set_present(dev
, state
);
369 void device_set_present(struct device
*dev
, bool state
)
371 if (dev
->sys_present
== state
)
374 D(DEVICE
, "%s '%s' %s present\n", dev
->type
->name
, dev
->ifname
, state
? "is now" : "is no longer" );
375 dev
->sys_present
= state
;
376 device_refresh_present(dev
);
379 void device_add_user(struct device_user
*dep
, struct device
*dev
)
382 device_remove_user(dep
);
388 list_add_tail(&dep
->list
, &dev
->users
);
389 if (dep
->cb
&& dev
->present
) {
390 dep
->cb(dep
, DEV_EVENT_ADD
);
392 dep
->cb(dep
, DEV_EVENT_UP
);
397 device_free(struct device
*dev
)
402 dev
->type
->free(dev
);
407 __device_free_unused(struct device
*dev
)
409 if (!list_empty(&dev
->users
) || dev
->current_config
|| __devlock
)
415 void device_remove_user(struct device_user
*dep
)
417 struct device
*dev
= dep
->dev
;
422 dep
->hotplug
= false;
426 list_del(&dep
->list
);
428 __device_free_unused(dev
);
432 device_free_unused(struct device
*dev
)
437 return __device_free_unused(dev
);
439 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
)
440 __device_free_unused(dev
);
444 device_init_pending(void)
446 struct device
*dev
, *tmp
;
448 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
449 if (!dev
->config_pending
)
452 dev
->type
->config_init(dev
);
453 dev
->config_pending
= false;
457 static enum dev_change_type
458 device_reload_config(struct device
*dev
, struct blob_attr
*attr
)
460 struct blob_attr
*tb
[__DEV_ATTR_MAX
];
461 const struct config_param_list
*cfg
= dev
->type
->config_params
;
463 if (config_check_equal(dev
->config
, attr
, cfg
))
464 return DEV_CONFIG_NO_CHANGE
;
466 if (cfg
== &device_attr_list
) {
467 memset(tb
, 0, sizeof(tb
));
470 blobmsg_parse(dev_attrs
, __DEV_ATTR_MAX
, tb
,
471 blob_data(attr
), blob_len(attr
));
473 device_init_settings(dev
, tb
);
474 return DEV_CONFIG_RESTART
;
476 return DEV_CONFIG_RECREATE
;
480 device_set_config(struct device
*dev
, const struct device_type
*type
,
481 struct blob_attr
*attr
)
483 if (type
!= dev
->type
)
484 return DEV_CONFIG_RECREATE
;
486 if (dev
->type
->reload
)
487 return dev
->type
->reload(dev
, attr
);
489 return device_reload_config(dev
, attr
);
493 device_replace(struct device
*dev
, struct device
*odev
)
495 struct device_user
*dep
, *tmp
;
496 bool present
= odev
->present
;
499 device_set_present(odev
, false);
501 list_for_each_entry_safe(dep
, tmp
, &odev
->users
, list
) {
503 list_move_tail(&dep
->list
, &dev
->users
);
509 device_set_present(dev
, true);
513 device_reset_config(void)
517 avl_for_each_element(&devices
, dev
, avl
)
518 dev
->current_config
= false;
522 device_reset_old(void)
524 struct device
*dev
, *tmp
, *ndev
;
526 avl_for_each_element_safe(&devices
, dev
, avl
, tmp
) {
527 if (dev
->current_config
|| dev
->default_config
)
530 if (dev
->type
!= &simple_device_type
)
533 ndev
= device_create_default(dev
->ifname
, dev
->external
);
534 device_replace(ndev
, dev
);
539 device_create(const char *name
, const struct device_type
*type
,
540 struct blob_attr
*config
)
542 struct device
*odev
= NULL
, *dev
;
543 enum dev_change_type change
;
545 config
= config_memdup(config
);
549 odev
= device_get(name
, false);
551 odev
->current_config
= true;
552 change
= device_set_config(odev
, type
, config
);
554 case DEV_CONFIG_RESTART
:
555 case DEV_CONFIG_APPLIED
:
556 D(DEVICE
, "Device '%s': config applied\n", odev
->ifname
);
558 odev
->config
= config
;
559 if (change
== DEV_CONFIG_RESTART
&& odev
->present
) {
560 device_set_present(odev
, false);
561 device_set_present(odev
, true);
564 case DEV_CONFIG_NO_CHANGE
:
565 D(DEVICE
, "Device '%s': no configuration change\n", odev
->ifname
);
568 case DEV_CONFIG_RECREATE
:
569 D(DEVICE
, "Device '%s': recreate device\n", odev
->ifname
);
574 D(DEVICE
, "Create new device '%s' (%s)\n", name
, type
->name
);
576 dev
= type
->create(name
, config
);
580 dev
->current_config
= true;
581 dev
->config
= config
;
583 device_replace(dev
, odev
);
585 if (!config_init
&& dev
->config_pending
)
586 type
->config_init(dev
);
592 device_dump_status(struct blob_buf
*b
, struct device
*dev
)
594 struct device_settings st
;
598 avl_for_each_element(&devices
, dev
, avl
) {
601 c
= blobmsg_open_table(b
, dev
->ifname
);
602 device_dump_status(b
, dev
);
603 blobmsg_close_table(b
, c
);
609 blobmsg_add_u8(b
, "external", dev
->external
);
610 blobmsg_add_u8(b
, "present", dev
->present
);
611 blobmsg_add_string(b
, "type", dev
->type
->name
);
616 blobmsg_add_u8(b
, "up", !!dev
->active
);
617 if (dev
->type
->dump_info
)
618 dev
->type
->dump_info(dev
, b
);
620 system_if_dump_info(dev
, b
);
623 device_merge_settings(dev
, &st
);
624 if (st
.flags
& DEV_OPT_MTU
)
625 blobmsg_add_u32(b
, "mtu", st
.mtu
);
626 if (st
.flags
& DEV_OPT_MACADDR
)
627 blobmsg_add_string(b
, "macaddr", ether_ntoa((struct ether_addr
*) st
.macaddr
));
628 if (st
.flags
& DEV_OPT_TXQUEUELEN
)
629 blobmsg_add_u32(b
, "txqueuelen", st
.txqueuelen
);
632 s
= blobmsg_open_table(b
, "statistics");
633 if (dev
->type
->dump_stats
)
634 dev
->type
->dump_stats(dev
, b
);
636 system_if_dump_stats(dev
, b
);
637 blobmsg_close_table(b
, s
);