move l3 device tracking to interface core to enforce proper order of address/route...
authorFelix Fietkau <nbd@openwrt.org>
Fri, 23 Mar 2012 12:41:53 +0000 (14:41 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Fri, 23 Mar 2012 12:41:53 +0000 (14:41 +0200)
device.c
interface-event.c
interface-ip.c
interface.c
interface.h
proto-shell.c
ubus.c

index 47b5875c230446f74fc321d8b845a861d9dd2688..0560a708705772cd74ea562179163cc1833c000c 100644 (file)
--- a/device.c
+++ b/device.c
@@ -466,6 +466,9 @@ void device_add_user(struct device_user *dep, struct device *dev)
        if (dep->dev)
                device_remove_user(dep);
 
+       if (!dev)
+               return;
+
        dep->dev = dev;
        list_add_tail(&dep->list, &dev->users);
        if (dep->cb && dev->present) {
index 4e0773df45adb1966ec522f85d572c1f61e249ba..5ab6240e963b4ab3aa817604651c7c5dda7f53bf 100644 (file)
@@ -57,8 +57,8 @@ call_hotplug(void)
        current_ev = current->hotplug_ev;
        list_del_init(&current->hotplug_list);
 
-       if (current_ev == IFEV_UP && current->l3_dev->dev)
-               device = current->l3_dev->dev->ifname;
+       if (current_ev == IFEV_UP && current->l3_dev.dev)
+               device = current->l3_dev.dev->ifname;
 
        D(SYSTEM, "Call hotplug handler for interface '%s' (%s)\n", current->name, device ? device : "none");
        run_cmd(current->name, device, current_ev == IFEV_UP);
index 8c4616896cc9aea9547a80c7fab8df4f0ce2f874..bbf2ccffd85dd10a9c035ce43bbc2eec011d140b 100644 (file)
@@ -128,7 +128,7 @@ interface_update_proto_addr(struct vlist_tree *tree,
 
        ip = container_of(tree, struct interface_ip_settings, addr);
        iface = ip->iface;
-       dev = iface->l3_dev->dev;
+       dev = iface->l3_dev.dev;
 
        if (node_new) {
                a_new = container_of(node_new, struct device_addr, node);
@@ -193,7 +193,7 @@ interface_update_proto_route(struct vlist_tree *tree,
 
        ip = container_of(tree, struct interface_ip_settings, route);
        iface = ip->iface;
-       dev = iface->l3_dev->dev;
+       dev = iface->l3_dev.dev;
 
        route_old = container_of(node_old, struct device_route, node);
        route_new = container_of(node_new, struct device_route, node);
@@ -353,7 +353,7 @@ void interface_ip_set_enabled(struct interface_ip_settings *ip, bool enabled)
        struct device *dev;
 
        ip->enabled = enabled;
-       dev = ip->iface->l3_dev->dev;
+       dev = ip->iface->l3_dev.dev;
        if (!dev)
                return;
 
index f281a9b4bbf2d1dd63de31b5375a8d296e531561..902f55b802e119819520b13cdd370252cf4739c5 100644 (file)
@@ -94,11 +94,10 @@ interface_event(struct interface *iface, enum interface_event ev)
 static void
 interface_flush_state(struct interface *iface)
 {
-       interface_ip_flush(&iface->proto_ip);
        if (iface->main_dev.dev)
                device_release(&iface->main_dev);
-       if (iface->l3_dev != &iface->main_dev && iface->l3_dev->dev)
-               device_release(iface->l3_dev);
+       if (iface->l3_dev.dev)
+               device_release(&iface->l3_dev);
 }
 
 static void
@@ -191,7 +190,7 @@ interface_claim_device(struct interface *iface)
                !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
                dev = device_get(iface->ifname, true);
                if (dev)
-                       device_add_user(&iface->main_dev, dev);
+                       interface_set_main_dev(iface, dev);
        }
        if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
                interface_set_available(iface, true);
@@ -211,8 +210,7 @@ interface_cleanup(struct interface *iface, bool reload)
        interface_clear_errors(iface);
        if (iface->main_dev.dev &&
            (!reload || !iface->main_dev.hotplug))
-               device_remove_user(&iface->main_dev);
-       iface->l3_dev = &iface->main_dev;
+               interface_set_main_dev(iface, NULL);
        interface_set_proto_state(iface, NULL);
 }
 
@@ -323,7 +321,6 @@ interface_init(struct interface *iface, const char *name,
        iface->config_ip.enabled = false;
 
        iface->main_dev.cb = interface_cb;
-       iface->l3_dev = &iface->main_dev;
 
        blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
                      blob_data(config), blob_len(config));
@@ -356,6 +353,46 @@ interface_add(struct interface *iface, struct blob_attr *config)
        vlist_add(&interfaces, &iface->node, iface->name);
 }
 
+void
+interface_set_l3_dev(struct interface *iface, struct device *dev)
+{
+       bool enabled = iface->config_ip.enabled;
+       bool claimed = iface->l3_dev.claimed;
+
+       if (iface->l3_dev.dev == dev)
+               return;
+
+       interface_ip_set_enabled(&iface->config_ip, false);
+       interface_ip_flush(&iface->proto_ip);
+       device_add_user(&iface->l3_dev, dev);
+
+       if (dev) {
+               if (claimed)
+                       device_claim(&iface->l3_dev);
+               interface_ip_set_enabled(&iface->config_ip, enabled);
+       }
+}
+
+void
+interface_set_main_dev(struct interface *iface, struct device *dev)
+{
+       bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
+       bool claimed = iface->l3_dev.claimed;
+
+       if (iface->main_dev.dev == dev)
+               return;
+
+       device_add_user(&iface->main_dev, dev);
+       if (set_l3)
+               interface_set_l3_dev(iface, dev);
+
+       if (claimed)
+               device_claim(&iface->l3_dev);
+
+       if (!iface->l3_dev.dev)
+               interface_set_l3_dev(iface, dev);
+}
+
 int
 interface_remove_link(struct interface *iface, struct device *dev)
 {
@@ -392,7 +429,7 @@ interface_add_link(struct interface *iface, struct device *dev)
                        return UBUS_STATUS_NOT_SUPPORTED;
        }
 
-       device_add_user(&iface->main_dev, dev);
+       interface_set_main_dev(iface, dev);
        iface->main_dev.hotplug = true;
        return 0;
 }
@@ -477,8 +514,8 @@ interface_update_complete(struct interface *iface)
 
        interface_ip_update_complete(&iface->proto_ip);
        vlist_for_each_element(&iface->config_ip.route, route, node) {
-               if (iface->l3_dev->dev) {
-                       system_add_route(iface->l3_dev->dev, route);
+               if (iface->l3_dev.dev) {
+                       system_add_route(iface->l3_dev.dev, route);
                        route->enabled = true;
                }
        }
index f0b26f5405da0addfbe20f7788e6a8258f5e3012..b2ecd23d0beeaff01c2d0779dbd8d9b237a5648b 100644 (file)
@@ -76,7 +76,7 @@ struct interface {
        struct device_user main_dev;
 
        /* interface that layer 3 communication will go through */
-       struct device_user *l3_dev;
+       struct device_user l3_dev;
 
        struct blob_attr *config;
 
@@ -111,6 +111,8 @@ int interface_set_up(struct interface *iface);
 int interface_set_down(struct interface *iface);
 void __interface_set_down(struct interface *iface, bool force);
 
+void interface_set_main_dev(struct interface *iface, struct device *dev);
+void interface_set_l3_dev(struct interface *iface, struct device *dev);
 
 void interface_add_user(struct interface_user *dep, struct interface *iface);
 void interface_remove_user(struct interface_user *dep);
index 8238c0d188f7154c11f978bf502975e2307154ec..b5d436ad7b743f501936c8462cd9a2310feacfa8 100644 (file)
@@ -41,8 +41,6 @@ struct proto_shell_state {
        struct proto_shell_handler *handler;
        struct blob_attr *config;
 
-       struct device_user l3_dev;
-
        struct uloop_timeout teardown_timeout;
 
        struct netifd_process script_task;
@@ -79,9 +77,6 @@ proto_shell_handler(struct interface_proto_state *proto,
                if (state->sm == S_TEARDOWN)
                        return 0;
 
-               if (state->l3_dev.dev)
-                       device_remove_user(&state->l3_dev);
-
                if (state->script_task.uloop.pending) {
                        if (state->sm != S_SETUP_ABORT) {
                                uloop_timeout_set(&state->teardown_timeout, 1000);
@@ -204,8 +199,6 @@ proto_shell_free(struct interface_proto_state *proto)
        struct proto_shell_state *state;
 
        state = container_of(proto, struct proto_shell_state, proto);
-       if (state->l3_dev.dev)
-               device_remove_user(&state->l3_dev);
        netifd_kill_process(&state->script_task);
        netifd_kill_process(&state->proto_task);
        free(state->config);
@@ -305,9 +298,6 @@ proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data,
                if (!iface->main_dev.dev)
                        return UBUS_STATUS_INVALID_ARGUMENT;
        } else {
-               if (state->l3_dev.dev)
-                       device_remove_user(&state->l3_dev);
-
                devname = blobmsg_data(tb[NOTIFY_IFNAME]);
                if (tb[NOTIFY_TUNNEL]) {
                        dev = proto_shell_create_tunnel(devname,
@@ -320,9 +310,8 @@ proto_shell_update_link(struct proto_shell_state *state, struct blob_attr *data,
                                return UBUS_STATUS_NOT_FOUND;
                }
 
-               device_add_user(&state->l3_dev, dev);
-               iface->l3_dev = &state->l3_dev;
-               device_claim(&state->l3_dev);
+               interface_set_l3_dev(iface, dev);
+               device_claim(&iface->l3_dev);
        }
 
        interface_update_start(iface);
diff --git a/ubus.c b/ubus.c
index 6d79a0a43054d06c52fe8586faf7c6a328130847..40c63742fe3bd36de3ec2e493ec3d4819662e329 100644 (file)
--- a/ubus.c
+++ b/ubus.c
@@ -303,7 +303,7 @@ netifd_handle_status(struct ubus_context *ctx, struct ubus_object *obj,
        if (iface->state == IFS_UP) {
                time_t cur = system_get_rtime();
                blobmsg_add_u32(&b, "uptime", cur - iface->start_time);
-               blobmsg_add_string(&b, "l3_device", iface->l3_dev->dev->ifname);
+               blobmsg_add_string(&b, "l3_device", iface->l3_dev.dev->ifname);
        }
 
        dev = iface->main_dev.dev;