#include <sys/socket.h>
#include <net/ethernet.h>
+#ifdef linux
+#include <netinet/ether.h>
+#endif
+
#include "netifd.h"
#include "system.h"
#include "config.h"
return 0;
dep->claimed = true;
- DPRINTF("claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1);
+ D(DEVICE, "claim device %s, new refcount: %d\n", dev->ifname, dev->active + 1);
if (++dev->active != 1)
return 0;
dep->claimed = false;
dev->active--;
- DPRINTF("release device %s, new refcount: %d\n", dev->ifname, dev->active);
+ D(DEVICE, "release device %s, new refcount: %d\n", dev->ifname, dev->active);
assert(dev->active >= 0);
if (dev->active)
device_broadcast_event(dev, DEV_EVENT_DOWN);
}
-int check_device_state(struct device *dev)
+int device_check_state(struct device *dev)
{
if (!dev->type->check_state)
return 0;
if (name)
strncpy(dev->ifname, name, IFNAMSIZ);
- fprintf(stderr, "Initialize device '%s'\n", dev->ifname);
+ D(DEVICE, "Initialize device '%s'\n", dev->ifname);
INIT_LIST_HEAD(&dev->users);
dev->type = type;
}
if (ret < 0)
return ret;
- check_device_state(dev);
+ device_check_state(dev);
return 0;
}
{
struct device_user *dep, *tmp;
- fprintf(stderr, "Clean up device '%s'\n", dev->ifname);
+ D(DEVICE, "Clean up device '%s'\n", dev->ifname);
list_for_each_entry_safe(dep, tmp, &dev->users, list) {
if (!dep->cb)
continue;
if (dev->present == state)
return;
- DPRINTF("Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" );
+ D(DEVICE, "Device '%s' %s present\n", dev->ifname, state ? "is now" : "is no longer" );
dev->present = state;
device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
}
}
}
+static void
+__device_free_unused(struct device *dev)
+{
+ if (!list_empty(&dev->users))
+ return;
+
+ device_free(dev);
+}
+
void device_remove_user(struct device_user *dep)
{
struct device *dev = dep->dev;
device_release(dep);
list_del(&dep->list);
-
- if (list_empty(&dev->users)) {
- /* all references have gone away, remove this device */
- device_free(dev);
- }
-
dep->dev = NULL;
-}
-
-static void
-__device_free_unused(struct device *dev)
-{
- if (!list_empty(&dev->users))
- return;
-
- device_free(dev);
+ __device_free_unused(dev);
}
void
avl_for_each_element_safe(&devices, dev, avl, tmp)
__device_free_unused(dev);
}
+
+enum dev_change_type
+device_reload_config(struct device *dev, struct blob_attr *attr)
+{
+ struct blob_attr *tb[__DEV_ATTR_MAX], *tb1[__DEV_ATTR_MAX];
+
+ blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
+ blob_data(attr), blob_len(attr));
+ if (dev->config)
+ blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb1,
+ blob_data(dev->config), blob_len(dev->config));
+ else
+ memset(tb1, 0, sizeof(tb1));
+
+ if (!config_diff(tb, tb1, &device_attr_list, NULL))
+ return DEV_CONFIG_NO_CHANGE;
+
+ device_init_settings(dev, tb);
+ return DEV_CONFIG_APPLIED;
+}
+
+static enum dev_change_type
+device_check_config(struct device *dev, struct blob_attr *attr)
+{
+ if (dev->type->reload)
+ return dev->type->reload(dev, attr);
+
+ return device_reload_config(dev, attr);
+}
+
+static void
+device_replace(struct device *dev, struct device *odev)
+{
+ struct device_user *dep, *tmp;
+ bool present = odev->present;
+
+ if (present)
+ device_set_present(odev, false);
+
+ list_for_each_entry_safe(dep, tmp, &odev->users, list) {
+ device_remove_user(dep);
+ device_add_user(dep, dev);
+ }
+ device_free(odev);
+
+ if (present)
+ device_set_present(dev, true);
+}
+
+struct device *
+device_create(const char *name, const struct device_type *type,
+ struct blob_attr *config)
+{
+ struct device *odev = NULL, *dev;
+ enum dev_change_type change;
+
+ odev = device_get(name, false);
+ if (odev) {
+ change = device_check_config(odev, config);
+ switch (change) {
+ case DEV_CONFIG_APPLIED:
+ free(odev->config);
+ odev->config = config_memdup(config);
+ if (odev->present) {
+ device_set_present(odev, false);
+ device_set_present(odev, true);
+ }
+ /* fall through */
+ case DEV_CONFIG_NO_CHANGE:
+ return odev;
+ case DEV_CONFIG_RECREATE:
+ break;
+ }
+ }
+
+ dev = type->create(config);
+ dev->config = config_memdup(config);
+ if (odev)
+ device_replace(dev, odev);
+
+ return dev;
+}