X-Git-Url: https://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=device.c;h=56fc3f787713ce5eb0d9e3bf6833b460d1d264cd;hp=34ad9adb1603c19558d02851b4064a8fcec72d2e;hb=134775b1f41fa19a816768268b9d0f8dad86bd90;hpb=4bf10a76e01d2f2971a86765722a4c4a65b584a7 diff --git a/device.c b/device.c index 34ad9ad..56fc3f7 100644 --- a/device.c +++ b/device.c @@ -31,15 +31,15 @@ static struct avl_tree devices; static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = { - [DEV_ATTR_TYPE] = { "type", BLOBMSG_TYPE_STRING }, - [DEV_ATTR_IFNAME] = { "ifname", BLOBMSG_TYPE_ARRAY }, - [DEV_ATTR_MTU] = { "mtu", BLOBMSG_TYPE_INT32 }, - [DEV_ATTR_MACADDR] = { "macaddr", BLOBMSG_TYPE_STRING }, - [DEV_ATTR_TXQUEUELEN] = { "txqueuelen", BLOBMSG_TYPE_INT32 }, - [DEV_ATTR_ENABLED] = { "enabled", BLOBMSG_TYPE_BOOL }, + [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING }, + [DEV_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_ARRAY }, + [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 }, + [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING }, + [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 }, + [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL }, }; -const struct config_param_list device_attr_list = { +const struct uci_blob_param_list device_attr_list = { .n_params = __DEV_ATTR_MAX, .params = dev_attrs, }; @@ -180,16 +180,22 @@ static void __init dev_init(void) avl_init(&devices, avl_strcmp, true, NULL); } -void device_broadcast_event(struct device *dev, enum device_event ev) +static int device_broadcast_cb(void *ctx, struct safe_list *list) { - struct device_user *dep, *tmp; + struct device_user *dep = container_of(list, struct device_user, list); + int *ev = ctx; - list_for_each_entry_safe(dep, tmp, &dev->users, list) { - if (!dep->cb) - continue; + if (dep->cb) + dep->cb(dep, *ev); + return 0; +} - dep->cb(dep, ev); - } +void device_broadcast_event(struct device *dev, enum device_event ev) +{ + int dev_ev = ev; + + safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev); + safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev); } int device_claim(struct device_user *dep) @@ -210,7 +216,7 @@ int device_claim(struct device_user *dep) if (ret == 0) device_broadcast_event(dev, DEV_EVENT_UP); else { - D(DEVICE, "claim device %s failed: %d\n", dev->ifname, ret); + D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret); dev->active = 0; dep->claimed = false; } @@ -256,7 +262,8 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con strncpy(dev->ifname, name, IFNAMSIZ); D(DEVICE, "Initialize device '%s'\n", dev->ifname); - INIT_LIST_HEAD(&dev->users); + INIT_SAFE_LIST(&dev->users); + INIT_SAFE_LIST(&dev->aliases); dev->type = type; if (!dev->set_state) @@ -310,8 +317,13 @@ device_get(const char *name, int create) return device_alias_get(name + 1); dev = avl_find_element(&devices, name, dev, avl); - if (dev) + if (dev) { + if (create > 1 && !dev->external) { + dev->external = true; + device_set_present(dev, true); + } return dev; + } if (!create) return NULL; @@ -330,19 +342,21 @@ device_delete(struct device *dev) dev->avl.key = NULL; } -void device_cleanup(struct device *dev) +static int device_cleanup_cb(void *ctx, struct safe_list *list) { - struct device_user *dep, *tmp; - - D(DEVICE, "Clean up device '%s'\n", dev->ifname); - list_for_each_entry_safe(dep, tmp, &dev->users, list) { - if (!dep->cb) - continue; - + struct device_user *dep = container_of(list, struct device_user, list); + if (dep->cb) dep->cb(dep, DEV_EVENT_REMOVE); - device_release(dep); - } + device_release(dep); + return 0; +} + +void device_cleanup(struct device *dev) +{ + D(DEVICE, "Clean up device '%s'\n", dev->ifname); + safe_list_for_each(&dev->users, device_cleanup_cb, NULL); + safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL); device_delete(dev); } @@ -376,8 +390,27 @@ void device_set_present(struct device *dev, bool state) device_refresh_present(dev); } +static int device_refcount(struct device *dev) +{ + struct list_head *list; + int count = 0; + + list_for_each(list, &dev->users.list) + count++; + + list_for_each(list, &dev->aliases.list) + count++; + + return count; +} + void device_add_user(struct device_user *dep, struct device *dev) { + struct safe_list *head; + + if (dep->dev == dev) + return; + if (dep->dev) device_remove_user(dep); @@ -385,7 +418,15 @@ void device_add_user(struct device_user *dep, struct device *dev) return; dep->dev = dev; - list_add_tail(&dep->list, &dev->users); + + if (dep->alias) + head = &dev->aliases; + else + head = &dev->users; + + safe_list_add(&dep->list, head); + D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev)); + if (dep->cb && dev->present) { dep->cb(dep, DEV_EVENT_ADD); if (dev->active) @@ -406,7 +447,9 @@ device_free(struct device *dev) static void __device_free_unused(struct device *dev) { - if (!list_empty(&dev->users) || dev->current_config || __devlock) + if (!safe_list_empty(&dev->users) || + !safe_list_empty(&dev->aliases) || + dev->current_config || __devlock) return; device_free(dev); @@ -423,8 +466,9 @@ void device_remove_user(struct device_user *dep) if (dep->claimed) device_release(dep); - list_del(&dep->list); + safe_list_del(&dep->list); dep->dev = NULL; + D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev)); __device_free_unused(dev); } @@ -458,9 +502,9 @@ static enum dev_change_type device_reload_config(struct device *dev, struct blob_attr *attr) { struct blob_attr *tb[__DEV_ATTR_MAX]; - const struct config_param_list *cfg = dev->type->config_params; + const struct uci_blob_param_list *cfg = dev->type->config_params; - if (config_check_equal(dev->config, attr, cfg)) + if (uci_blob_check_equal(dev->config, attr, cfg)) return DEV_CONFIG_NO_CHANGE; if (cfg == &device_attr_list) { @@ -498,9 +542,10 @@ device_replace(struct device *dev, struct device *odev) if (present) device_set_present(odev, false); - list_for_each_entry_safe(dep, tmp, &odev->users, list) { + list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) { device_release(dep); - list_move_tail(&dep->list, &dev->users); + safe_list_del(&dep->list); + safe_list_add(&dep->list, &dev->users); dep->dev = dev; } device_free(odev); @@ -542,7 +587,7 @@ device_create(const char *name, const struct device_type *type, struct device *odev = NULL, *dev; enum dev_change_type change; - config = config_memdup(config); + config = blob_memdup(config); if (!config) return NULL; @@ -550,6 +595,10 @@ device_create(const char *name, const struct device_type *type, if (odev) { odev->current_config = true; change = device_set_config(odev, type, config); + if (odev->external) { + system_if_apply_settings(odev, &odev->settings); + change = DEV_CONFIG_APPLIED; + } switch (change) { case DEV_CONFIG_RESTART: case DEV_CONFIG_APPLIED: @@ -624,7 +673,7 @@ device_dump_status(struct blob_buf *b, struct device *dev) if (st.flags & DEV_OPT_MTU) blobmsg_add_u32(b, "mtu", st.mtu); if (st.flags & DEV_OPT_MACADDR) - blobmsg_add_string(b, "macaddr", ether_ntoa((struct ether_addr *) st.macaddr)); + blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr)); if (st.flags & DEV_OPT_TXQUEUELEN) blobmsg_add_u32(b, "txqueuelen", st.txqueuelen); }