interface: do not release device on link down
[project/netifd.git] / device.c
index b3d0e85f8550de45fbabeb8f17028ddf8af1573e..24175564f8558df03aa54470ac72aaea23fc697c 100644 (file)
--- a/device.c
+++ b/device.c
@@ -63,6 +63,12 @@ static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
        [DEV_ATTR_AUTH] = { .name = "auth", .type = BLOBMSG_TYPE_BOOL },
        [DEV_ATTR_SPEED] = { .name = "speed", .type = BLOBMSG_TYPE_INT32 },
        [DEV_ATTR_DUPLEX] = { .name = "duplex", .type = BLOBMSG_TYPE_BOOL },
+       [DEV_ATTR_VLAN] = { .name = "vlan", .type = BLOBMSG_TYPE_ARRAY },
+       [DEV_ATTR_PAUSE] = { .name = "pause", .type = BLOBMSG_TYPE_BOOL },
+       [DEV_ATTR_ASYM_PAUSE] = { .name = "asym_pause", .type = BLOBMSG_TYPE_BOOL },
+       [DEV_ATTR_RXPAUSE] = { .name = "rxpause", .type = BLOBMSG_TYPE_BOOL },
+       [DEV_ATTR_TXPAUSE] = { .name = "txpause", .type = BLOBMSG_TYPE_BOOL },
+       [DEV_ATTR_AUTONEG] = { .name = "autoneg", .type = BLOBMSG_TYPE_BOOL },
 };
 
 const struct uci_blob_param_list device_attr_list = {
@@ -280,9 +286,55 @@ device_merge_settings(struct device *dev, struct device_settings *n)
        n->auth = s->flags & DEV_OPT_AUTH ? s->auth : os->auth;
        n->speed = s->flags & DEV_OPT_SPEED ? s->speed : os->speed;
        n->duplex = s->flags & DEV_OPT_DUPLEX ? s->duplex : os->duplex;
+       n->pause = s->flags & DEV_OPT_PAUSE ? s->pause : os->pause;
+       n->asym_pause = s->flags & DEV_OPT_ASYM_PAUSE ? s->asym_pause : os->asym_pause;
+       n->rxpause = s->flags & DEV_OPT_RXPAUSE ? s->rxpause : os->rxpause;
+       n->txpause = s->flags & DEV_OPT_TXPAUSE ? s->txpause : os->txpause;
+       n->autoneg = s->flags & DEV_OPT_AUTONEG ? s->autoneg : os->autoneg;
        n->flags = s->flags | os->flags | os->valid_flags;
 }
 
+static bool device_fill_vlan_range(struct device_vlan_range *r, const char *val)
+{
+       unsigned long cur_start, cur_end;
+       char *sep;
+
+       cur_start = strtoul(val, &sep, 0);
+       cur_end = cur_start;
+
+       if (*sep == '-')
+               cur_end = strtoul(sep + 1, &sep, 0);
+       if (*sep || cur_end < cur_start)
+               return false;
+
+       r->start = cur_start;
+       r->end = cur_end;
+
+       return true;
+}
+
+static void
+device_set_extra_vlans(struct device *dev, struct blob_attr *data)
+{
+       struct blob_attr *cur;
+       int n_vlans;
+       size_t rem;
+
+       dev->n_extra_vlan = 0;
+       if (!data)
+               return;
+
+       n_vlans = blobmsg_check_array(data, BLOBMSG_TYPE_STRING);
+       if (n_vlans < 1)
+               return;
+
+       dev->extra_vlan = realloc(dev->extra_vlan, n_vlans * sizeof(*dev->extra_vlan));
+       blobmsg_for_each_attr(cur, data, rem)
+               if (device_fill_vlan_range(&dev->extra_vlan[dev->n_extra_vlan],
+                                          blobmsg_get_string(cur)))
+                       dev->n_extra_vlan++;
+}
+
 void
 device_init_settings(struct device *dev, struct blob_attr **tb)
 {
@@ -464,6 +516,31 @@ device_init_settings(struct device *dev, struct blob_attr **tb)
                s->flags |= DEV_OPT_DUPLEX;
        }
 
+       if ((cur = tb[DEV_ATTR_PAUSE])) {
+               s->pause = blobmsg_get_bool(cur);
+               s->flags |= DEV_OPT_PAUSE;
+       }
+
+       if ((cur = tb[DEV_ATTR_ASYM_PAUSE])) {
+               s->asym_pause = blobmsg_get_bool(cur);
+               s->flags |= DEV_OPT_ASYM_PAUSE;
+       }
+
+       if ((cur = tb[DEV_ATTR_RXPAUSE])) {
+               s->rxpause = blobmsg_get_bool(cur);
+               s->flags |= DEV_OPT_RXPAUSE;
+       }
+
+       if ((cur = tb[DEV_ATTR_TXPAUSE])) {
+               s->txpause = blobmsg_get_bool(cur);
+               s->flags |= DEV_OPT_TXPAUSE;
+       }
+
+       if ((cur = tb[DEV_ATTR_AUTONEG])) {
+               s->autoneg = blobmsg_get_bool(cur);
+               s->flags |= DEV_OPT_AUTONEG;
+       }
+       device_set_extra_vlans(dev, tb[DEV_ATTR_VLAN]);
        device_set_disabled(dev, disabled);
 }
 
@@ -472,6 +549,17 @@ static void __init dev_init(void)
        avl_init(&devices, avl_strcmp, true, NULL);
 }
 
+static int device_release_cb(void *ctx, struct safe_list *list)
+{
+       struct device_user *dep = container_of(list, struct device_user, list);
+
+       if (!dep->dev || !dep->claimed)
+               return 0;
+
+       device_release(dep);
+       return 0;
+}
+
 static int device_broadcast_cb(void *ctx, struct safe_list *list)
 {
        struct device_user *dep = container_of(list, struct device_user, list);
@@ -771,6 +859,8 @@ void device_set_present(struct device *dev, bool state)
        D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
        dev->sys_present = state;
        device_refresh_present(dev);
+       if (!state)
+               safe_list_for_each(&dev->users, device_release_cb, NULL);
 }
 
 void device_set_link(struct device *dev, bool state)
@@ -878,6 +968,7 @@ device_free(struct device *dev)
        __devlock++;
        free(dev->config);
        device_cleanup(dev);
+       free(dev->extra_vlan);
        dev->type->free(dev);
        __devlock--;
 }
@@ -1024,14 +1115,18 @@ device_apply_config(struct device *dev, struct device_type *type,
 static void
 device_replace(struct device *dev, struct device *odev)
 {
-       struct device_user *dep, *tmp;
+       struct device_user *dep;
 
        __devlock++;
        if (odev->present)
                device_set_present(odev, false);
 
-       list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
+       while (!list_empty(&odev->users.list)) {
+               dep = list_first_entry(&odev->users.list, struct device_user, list.list);
                device_release(dep);
+               if (!dep->dev)
+                       continue;
+
                safe_list_del(&dep->list);
                __device_add_user(dep, dev);
        }