device.c: use format_macaddr() helper to convert mac addresses to strings, ether_ntoa...
[project/netifd.git] / device.c
index 34ad9ad..97fb27f 100644 (file)
--- a/device.c
+++ b/device.c
@@ -180,11 +180,12 @@ static void __init dev_init(void)
        avl_init(&devices, avl_strcmp, true, NULL);
 }
 
-void device_broadcast_event(struct device *dev, enum device_event ev)
+
+static void __device_broadcast_event(struct list_head *head, enum device_event ev)
 {
        struct device_user *dep, *tmp;
 
-       list_for_each_entry_safe(dep, tmp, &dev->users, list) {
+       list_for_each_entry_safe(dep, tmp, head, list) {
                if (!dep->cb)
                        continue;
 
@@ -192,6 +193,12 @@ void device_broadcast_event(struct device *dev, enum device_event ev)
        }
 }
 
+void device_broadcast_event(struct device *dev, enum device_event ev)
+{
+       __device_broadcast_event(&dev->aliases, ev);
+       __device_broadcast_event(&dev->users, ev);
+}
+
 int device_claim(struct device_user *dep)
 {
        struct device *dev = dep->dev;
@@ -257,6 +264,7 @@ void device_init_virtual(struct device *dev, const struct device_type *type, con
 
        D(DEVICE, "Initialize device '%s'\n", dev->ifname);
        INIT_LIST_HEAD(&dev->users);
+       INIT_LIST_HEAD(&dev->aliases);
        dev->type = type;
 
        if (!dev->set_state)
@@ -310,8 +318,13 @@ device_get(const char *name, int create)
                return device_alias_get(name + 1);
 
        dev = avl_find_element(&devices, name, dev, avl);
-       if (dev)
+       if (dev) {
+               if (create > 1 && !dev->external) {
+                       dev->external = true;
+                       device_set_present(dev, true);
+               }
                return dev;
+       }
 
        if (!create)
                return NULL;
@@ -376,8 +389,21 @@ void device_set_present(struct device *dev, bool state)
        device_refresh_present(dev);
 }
 
+static int device_refcount(struct device *dev)
+{
+       struct list_head *list;
+       int count = 0;
+
+       list_for_each(list, &dev->users)
+               count++;
+
+       return count;
+}
+
 void device_add_user(struct device_user *dep, struct device *dev)
 {
+       struct list_head *head;
+
        if (dep->dev)
                device_remove_user(dep);
 
@@ -385,7 +411,14 @@ void device_add_user(struct device_user *dep, struct device *dev)
                return;
 
        dep->dev = dev;
-       list_add_tail(&dep->list, &dev->users);
+
+       if (dep->alias)
+               head = &dev->aliases;
+       else
+               head = &dev->users;
+       list_add_tail(&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)
@@ -425,6 +458,7 @@ void device_remove_user(struct device_user *dep)
 
        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);
 }
 
@@ -550,6 +584,10 @@ device_create(const char *name, const struct device_type *type,
        if (odev) {
                odev->current_config = true;
                change = device_set_config(odev, type, config);
+               if (odev->external) {
+                       system_if_apply_settings(odev, &odev->settings);
+                       change = DEV_CONFIG_APPLIED;
+               }
                switch (change) {
                case DEV_CONFIG_RESTART:
                case DEV_CONFIG_APPLIED:
@@ -624,7 +662,7 @@ device_dump_status(struct blob_buf *b, struct device *dev)
                if (st.flags & DEV_OPT_MTU)
                        blobmsg_add_u32(b, "mtu", st.mtu);
                if (st.flags & DEV_OPT_MACADDR)
-                       blobmsg_add_string(b, "macaddr", ether_ntoa((struct ether_addr *) st.macaddr));
+                       blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
                if (st.flags & DEV_OPT_TXQUEUELEN)
                        blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
        }