X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fnetifd.git;a=blobdiff_plain;f=device.c;h=b29d73b122704206b95325228b84900b12bda562;hp=9ef444e88a233f615c320fe3f3ec65d7f10d2f0b;hb=26cb3b2a0aebb1b9dc07daf7b54bd8400d6d659b;hpb=cf90523881521fe8396a728230169b6b8ea7e8da diff --git a/device.c b/device.c index 9ef444e..b29d73b 100644 --- a/device.c +++ b/device.c @@ -31,15 +31,17 @@ 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 }, + [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL }, + [DEV_ATTR_PROMISC] = { .name = "promisc", .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, }; @@ -60,6 +62,9 @@ void device_unlock(void) static int set_device_state(struct device *dev, bool state) { + if (dev->external) + return 0; + if (state) system_if_up(dev); else @@ -139,6 +144,8 @@ device_merge_settings(struct device *dev, struct device_settings *n) memcpy(n->macaddr, (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr), sizeof(n->macaddr)); + n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6; + n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc; n->flags = s->flags | os->flags; } @@ -172,6 +179,16 @@ device_init_settings(struct device *dev, struct blob_attr **tb) } } + if ((cur = tb[DEV_ATTR_IPV6])) { + s->ipv6 = blobmsg_get_bool(cur); + s->flags |= DEV_OPT_IPV6; + } + + if ((cur = tb[DEV_ATTR_PROMISC])) { + s->promisc = blobmsg_get_bool(cur); + s->flags |= DEV_OPT_PROMISC; + } + device_set_disabled(dev, disabled); } @@ -180,36 +197,26 @@ static void __init dev_init(void) avl_init(&devices, avl_strcmp, true, NULL); } - -static void __device_broadcast_event(struct list_head *head, enum device_event ev) +static int device_broadcast_cb(void *ctx, struct safe_list *list) { - struct device_user *dep; - static uint8_t idx[__DEV_EVENT_MAX]; - bool found; + struct device_user *dep = container_of(list, struct device_user, list); + int *ev = ctx; - idx[ev]++; - do { - found = false; - - list_for_each_entry(dep, head, list) { - if (!dep->cb) - continue; - - if (dep->ev_idx[ev] == idx[ev]) - continue; + /* device might have been removed by an earlier callback */ + if (!dep->dev) + return 0; - dep->cb(dep, ev); - dep->ev_idx[ev] = idx[ev]; - found = true; - break; - } - } while (found); + if (dep->cb) + dep->cb(dep, *ev); + return 0; } void device_broadcast_event(struct device *dev, enum device_event ev) { - __device_broadcast_event(&dev->aliases, ev); - __device_broadcast_event(&dev->users, 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) @@ -221,7 +228,7 @@ int device_claim(struct device_user *dep) return 0; dep->claimed = true; - D(DEVICE, "Claim %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active + 1); + D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1); if (++dev->active != 1) return 0; @@ -230,7 +237,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; } @@ -247,14 +254,14 @@ void device_release(struct device_user *dep) dep->claimed = false; dev->active--; - D(DEVICE, "Release %s %s, new refcount: %d\n", dev->type->name, dev->ifname, dev->active); + D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active); assert(dev->active >= 0); if (dev->active) return; device_broadcast_event(dev, DEV_EVENT_TEARDOWN); - if (!dep->hotplug) + if (!dev->external) dev->set_state(dev, false); device_broadcast_event(dev, DEV_EVENT_DOWN); } @@ -262,7 +269,7 @@ void device_release(struct device_user *dep) int device_check_state(struct device *dev) { if (!dev->type->check_state) - return 0; + return simple_device_type.check_state(dev); return dev->type->check_state(dev); } @@ -276,8 +283,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_LIST_HEAD(&dev->aliases); + INIT_SAFE_LIST(&dev->users); + INIT_SAFE_LIST(&dev->aliases); dev->type = type; if (!dev->set_state) @@ -356,19 +363,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); } @@ -402,12 +411,35 @@ void device_set_present(struct device *dev, bool state) device_refresh_present(dev); } +void device_set_link(struct device *dev, bool state) +{ + if (dev->link_active == state) + return; + + netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" ); + + dev->link_active = state; + device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN); +} + +void device_set_ifindex(struct device *dev, int ifindex) +{ + if (dev->ifindex == ifindex) + return; + + dev->ifindex = ifindex; + device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX); +} + static int device_refcount(struct device *dev) { struct list_head *list; int count = 0; - list_for_each(list, &dev->users) + list_for_each(list, &dev->users.list) + count++; + + list_for_each(list, &dev->aliases.list) count++; return count; @@ -415,7 +447,10 @@ static int device_refcount(struct device *dev) void device_add_user(struct device_user *dep, struct device *dev) { - struct list_head *head; + struct safe_list *head; + + if (dep->dev == dev) + return; if (dep->dev) device_remove_user(dep); @@ -429,13 +464,17 @@ void device_add_user(struct device_user *dep, struct device *dev) head = &dev->aliases; else head = &dev->users; - list_add_tail(&dep->list, head); + + 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) dep->cb(dep, DEV_EVENT_UP); + + if (dev->link_active) + dep->cb(dep, DEV_EVENT_LINK_UP); } } @@ -452,7 +491,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); @@ -469,7 +510,7 @@ 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); @@ -505,9 +546,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) { @@ -545,9 +586,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); @@ -589,7 +631,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; @@ -598,7 +640,7 @@ device_create(const char *name, const struct device_type *type, odev->current_config = true; change = device_set_config(odev, type, config); if (odev->external) { - system_if_apply_settings(odev, &odev->settings); + system_if_apply_settings(odev, &odev->settings, odev->settings.flags); change = DEV_CONFIG_APPLIED; } switch (change) { @@ -665,6 +707,8 @@ device_dump_status(struct blob_buf *b, struct device *dev) return; blobmsg_add_u8(b, "up", !!dev->active); + blobmsg_add_u8(b, "carrier", !!dev->link_active); + if (dev->type->dump_info) dev->type->dump_info(dev, b); else @@ -678,6 +722,10 @@ device_dump_status(struct blob_buf *b, struct device *dev) blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr)); if (st.flags & DEV_OPT_TXQUEUELEN) blobmsg_add_u32(b, "txqueuelen", st.txqueuelen); + if (st.flags & DEV_OPT_IPV6) + blobmsg_add_u8(b, "ipv6", st.ipv6); + if (st.flags & DEV_OPT_PROMISC) + blobmsg_add_u8(b, "promisc", st.promisc); } s = blobmsg_open_table(b, "statistics");