add interface users (similar to device users)
authorFelix Fietkau <nbd@openwrt.org>
Tue, 18 Oct 2011 15:06:17 +0000 (17:06 +0200)
committerFelix Fietkau <nbd@openwrt.org>
Tue, 18 Oct 2011 16:17:54 +0000 (18:17 +0200)
interface.c
interface.h

index 9d6a7c1..0a2afd0 100644 (file)
@@ -79,6 +79,11 @@ void interface_add_error(struct interface *iface, const char *subsystem,
 static void
 interface_event(struct interface *iface, enum interface_event ev)
 {
+       struct interface_user *dep, *tmp;
+
+       list_for_each_entry_safe(dep, tmp, &iface->users, list)
+               dep->cb(dep, IFEV_UP);
+
        interface_queue_event(iface, ev);
 }
 
@@ -168,6 +173,22 @@ interface_set_available(struct interface *iface, bool new_state)
                __interface_set_down(iface, true);
 }
 
+void
+interface_add_user(struct interface_user *dep, struct interface *iface)
+{
+       dep->iface = iface;
+       list_add(&dep->list, &iface->users);
+       if (iface->state == IFS_UP)
+               dep->cb(dep, IFEV_UP);
+}
+
+void
+interface_remove_user(struct interface_user *dep)
+{
+       list_del_init(&dep->list);
+       dep->iface = NULL;
+}
+
 static void
 interface_claim_device(struct interface *iface)
 {
@@ -185,6 +206,11 @@ interface_claim_device(struct interface *iface)
 static void
 interface_cleanup(struct interface *iface)
 {
+       struct interface_user *dep, *tmp;
+
+       list_for_each_entry_safe(dep, tmp, &iface->users, list)
+               interface_remove_user(dep);
+
        interface_clear_dns(iface);
        interface_clear_errors(iface);
        if (iface->main_dev.dev)
@@ -290,6 +316,7 @@ interface_init(struct interface *iface, const char *name,
 
        strncpy(iface->name, name, sizeof(iface->name) - 1);
        INIT_LIST_HEAD(&iface->errors);
+       INIT_LIST_HEAD(&iface->users);
        INIT_LIST_HEAD(&iface->hotplug_list);
        INIT_LIST_HEAD(&iface->proto_dns_search);
        INIT_LIST_HEAD(&iface->proto_dns_servers);
index 6620796..7f84060 100644 (file)
@@ -33,6 +33,12 @@ struct interface_error {
        const char *data[];
 };
 
+struct interface_user {
+       struct list_head list;
+       struct interface *iface;
+       void (*cb)(struct interface_user *dep, enum interface_event ev);
+};
+
 /*
  * interface configuration
  */
@@ -52,6 +58,8 @@ struct interface {
        enum interface_state state;
        enum interface_config_state config_state;
 
+       struct list_head users;
+
        /* main interface that the interface is bound to */
        struct device_user main_dev;
 
@@ -91,6 +99,9 @@ void interface_set_available(struct interface *iface, bool new_state);
 int interface_set_up(struct interface *iface);
 int interface_set_down(struct interface *iface);
 
+void interface_add_user(struct interface_user *dep, struct interface *iface);
+void interface_remove_user(struct interface_user *dep);
+
 int interface_add_link(struct interface *iface, struct device *llif);
 void interface_remove_link(struct interface *iface, struct device *llif);