recreate devices if the type does not match
[project/netifd.git] / device.c
index b40f661..ce6f175 100644 (file)
--- a/device.c
+++ b/device.c
@@ -222,12 +222,24 @@ struct device *device_get(const char *name, bool create)
        if (!create)
                return NULL;
 
+       D(DEVICE, "Create simple device '%s'\n", name);
        dev = calloc(1, sizeof(*dev));
        device_init(dev, &simple_device_type, name);
 
        return dev;
 }
 
+static void
+device_delete(struct device *dev)
+{
+       if (!dev->avl.key)
+               return;
+
+       D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
+       avl_delete(&devices, &dev->avl);
+       dev->avl.key = NULL;
+}
+
 void device_cleanup(struct device *dev)
 {
        struct device_user *dep, *tmp;
@@ -240,8 +252,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)
@@ -333,8 +344,12 @@ device_reload_config(struct device *dev, struct blob_attr *attr)
 }
 
 static enum dev_change_type
-device_check_config(struct device *dev, struct blob_attr *attr)
+device_check_config(struct device *dev, const struct device_type *type,
+                   struct blob_attr *attr)
 {
+       if (type != dev->type)
+               return DEV_CONFIG_RECREATE;
+
        if (dev->type->reload)
                return dev->type->reload(dev, attr);
 
@@ -374,7 +389,7 @@ device_create(const char *name, const struct device_type *type,
 
        odev = device_get(name, false);
        if (odev) {
-               change = device_check_config(odev, config);
+               change = device_check_config(odev, type, config);
                switch (change) {
                case DEV_CONFIG_APPLIED:
                        D(DEVICE, "Device '%s': config applied\n", odev->ifname);
@@ -392,6 +407,7 @@ device_create(const char *name, const struct device_type *type,
                        return odev;
                case DEV_CONFIG_RECREATE:
                        D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
+                       device_delete(odev);
                        break;
                }
        }