device: fall back to simple interface status check if not provided by the device...
[project/netifd.git] / device.c
index e635b05..b16f156 100644 (file)
--- a/device.c
+++ b/device.c
 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 },
 };
 
 const struct uci_blob_param_list device_attr_list = {
@@ -60,6 +61,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 +143,7 @@ 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->flags = s->flags | os->flags;
 }
 
@@ -172,6 +177,11 @@ 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;
+       }
+
        device_set_disabled(dev, disabled);
 }
 
@@ -185,6 +195,10 @@ static int device_broadcast_cb(void *ctx, struct safe_list *list)
        struct device_user *dep = container_of(list, struct device_user, list);
        int *ev = ctx;
 
+       /* device might have been removed by an earlier callback */
+       if (!dep->dev)
+               return 0;
+
        if (dep->cb)
                dep->cb(dep, *ev);
        return 0;
@@ -207,7 +221,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;
 
@@ -216,7 +230,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;
        }
@@ -233,7 +247,7 @@ 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)
@@ -248,7 +262,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);
 }
@@ -390,6 +404,26 @@ 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;
@@ -408,6 +442,9 @@ 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);
 
@@ -428,6 +465,9 @@ void device_add_user(struct device_user *dep, struct device *dev)
                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);
        }
 }
 
@@ -593,7 +633,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) {
@@ -660,6 +700,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
@@ -673,6 +715,8 @@ 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);
        }
 
        s = blobmsg_open_table(b, "statistics");