X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=device.c;h=42b78e3584580a81e5573e877641908b1ef1232c;hp=3f83cb4700f99ee4bc608ae5990cc63477ee618c;hb=4f2a541e99b381c39b822ddaffb8f3941217463a;hpb=f026eb8247d74f88a6b704af866879867574f9f0 diff --git a/device.c b/device.c index 3f83cb4..42b78e3 100644 --- a/device.c +++ b/device.c @@ -7,6 +7,10 @@ #include #include +#ifdef linux +#include +#endif + #include "netifd.h" #include "system.h" #include "config.h" @@ -95,7 +99,7 @@ device_init_settings(struct device *dev, struct blob_attr **tb) static void __init dev_init(void) { - avl_init(&devices, avl_strcmp, false, NULL); + avl_init(&devices, avl_strcmp, true, NULL); } static void device_broadcast_event(struct device *dev, enum device_event ev) @@ -129,7 +133,7 @@ int device_claim(struct device_user *dep) return 0; dep->claimed = true; - DPRINTF("claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1); + D(DEVICE, "claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1); if (++dev->active != 1) return 0; @@ -152,7 +156,7 @@ void device_release(struct device_user *dep) dep->claimed = false; dev->active--; - DPRINTF("release device %s, new refcount: %d\n", dev->ifname, dev->active); + D(DEVICE, "release device %s, new refcount: %d\n", dev->ifname, dev->active); assert(dev->active >= 0); if (dev->active) @@ -163,7 +167,7 @@ void device_release(struct device_user *dep) device_broadcast_event(dev, DEV_EVENT_DOWN); } -int check_device_state(struct device *dev) +int device_check_state(struct device *dev) { if (!dev->type->check_state) return 0; @@ -179,7 +183,7 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con if (name) strncpy(dev->ifname, name, IFNAMSIZ); - fprintf(stderr, "Initialize device '%s'\n", dev->ifname); + D(DEVICE, "Initialize device '%s'\n", dev->ifname); INIT_LIST_HEAD(&dev->users); dev->type = type; } @@ -199,7 +203,7 @@ int device_init(struct device *dev, const struct device_type *type, const char * if (ret < 0) return ret; - check_device_state(dev); + device_check_state(dev); return 0; } @@ -224,11 +228,21 @@ struct device *device_get(const char *name, bool create) return dev; } +static void +device_delete(struct device *dev) +{ + if (!dev->avl.key) + return; + + avl_delete(&devices, &dev->avl); + dev->avl.key = NULL; +} + void device_cleanup(struct device *dev) { struct device_user *dep, *tmp; - fprintf(stderr, "Clean up device '%s'\n", dev->ifname); + D(DEVICE, "Clean up device '%s'\n", dev->ifname); list_for_each_entry_safe(dep, tmp, &dev->users, list) { if (!dep->cb) continue; @@ -236,8 +250,7 @@ void device_cleanup(struct device *dev) dep->cb(dep, DEV_EVENT_REMOVE); } - if (dev->avl.key) - avl_delete(&devices, &dev->avl); + device_delete(dev); } void device_set_present(struct device *dev, bool state) @@ -245,7 +258,7 @@ void device_set_present(struct device *dev, bool state) if (dev->present == state) return; - DPRINTF("Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" ); + D(DEVICE, "Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" ); dev->present = state; device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE); } @@ -293,3 +306,116 @@ device_free_unused(struct device *dev) avl_for_each_element_safe(&devices, dev, avl, tmp) __device_free_unused(dev); } + +void +device_init_pending(void) +{ + struct device *dev, *tmp; + + avl_for_each_element_safe(&devices, dev, avl, tmp) { + if (!dev->config_pending) + continue; + + dev->type->config_init(dev); + dev->config_pending = false; + } +} + +enum dev_change_type +device_reload_config(struct device *dev, struct blob_attr *attr) +{ + struct blob_attr *tb[__DEV_ATTR_MAX], *tb1[__DEV_ATTR_MAX]; + + blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, + blob_data(attr), blob_len(attr)); + if (dev->config) + blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb1, + blob_data(dev->config), blob_len(dev->config)); + else + memset(tb1, 0, sizeof(tb1)); + + if (!config_diff(tb, tb1, &device_attr_list, NULL)) + return DEV_CONFIG_NO_CHANGE; + + device_init_settings(dev, tb); + return DEV_CONFIG_APPLIED; +} + +static enum dev_change_type +device_check_config(struct device *dev, struct blob_attr *attr) +{ + if (dev->type->reload) + return dev->type->reload(dev, attr); + + return device_reload_config(dev, attr); +} + +static void +device_replace(struct device *dev, struct device *odev) +{ + struct device_user *dep, *tmp; + bool present = odev->present; + + if (present) + device_set_present(odev, false); + + list_for_each_entry_safe(dep, tmp, &odev->users, list) { + device_remove_user(dep); + device_add_user(dep, dev); + } + device_free(odev); + + if (present) + device_set_present(dev, true); +} + +struct device * +device_create(const char *name, const struct device_type *type, + struct blob_attr *config) +{ + struct device *odev = NULL, *dev; + enum dev_change_type change; + + D(DEVICE, "Create new device '%s' (%s)\n", name, type->name); + config = config_memdup(config); + if (!config) + return NULL; + + odev = device_get(name, false); + if (odev) { + change = device_check_config(odev, config); + switch (change) { + case DEV_CONFIG_APPLIED: + D(DEVICE, "Device '%s': config applied\n", odev->ifname); + free(odev->config); + odev->config = config; + if (odev->present) { + device_set_present(odev, false); + device_set_present(odev, true); + } + free(config); + return odev; + case DEV_CONFIG_NO_CHANGE: + D(DEVICE, "Device '%s': no configuration change\n", odev->ifname); + free(config); + return odev; + case DEV_CONFIG_RECREATE: + D(DEVICE, "Device '%s': recreate device\n", odev->ifname); + device_delete(odev); + break; + } + } + + dev = type->create(config); + if (!dev) + return NULL; + + dev->config = config; + if (odev) + device_replace(dev, odev); + + if (!config_init && dev->config_pending) + type->config_init(dev); + + return dev; +}