]> git.ipfire.org Git - network.git/commitdiff
networkd: zones: Keep a permanent reference to links
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 12:02:40 +0000 (12:02 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 12:02:40 +0000 (12:02 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/zone.c

index 8fe07c5c06d17e00e01de21bd26a1266af6c3dac..4fda1e5f28157349ae994aff26cc9c0378775f4d 100644 (file)
@@ -25,6 +25,7 @@
 
 #include "config.h"
 #include "daemon.h"
+#include "link.h"
 #include "logging.h"
 #include "string.h"
 #include "zone.h"
@@ -33,6 +34,9 @@ struct nw_zone {
        nw_daemon* daemon;
        int nrefs;
 
+       // Link
+       nw_link* link;
+
        char name[NETWORK_ZONE_NAME_MAX_LENGTH];
 
        // Configuration
@@ -66,6 +70,8 @@ static int __nw_zone_path(nw_zone* zone, char* p, const size_t length,
 }
 
 static void nw_zone_free(nw_zone* zone) {
+       if (zone->link)
+               nw_link_unref(zone->link);
        if (zone->config)
                nw_config_unref(zone->config);
        if (zone->daemon)
@@ -78,6 +84,14 @@ static int nw_zone_setup(nw_zone* zone) {
        char path[PATH_MAX];
        int r;
 
+       // Find the link
+       zone->link = nw_daemon_get_link_by_name(zone->daemon, zone->name);
+       if (zone->link) {
+               DEBUG("%s: Found matching link %d\n", zone->name, nw_link_ifindex(zone->link));
+       } else {
+               DEBUG("%s: Could not find matching link\n", zone->name);
+       }
+
        // Compose the path to the main configuration file
        r = nw_zone_path(zone, path, "%s", "settings");
        if (r)
@@ -164,22 +178,19 @@ char* nw_zone_bus_path(nw_zone* zone) {
 }
 
 static nw_link* nw_zone_get_link(nw_zone* zone) {
-       return nw_daemon_get_link_by_name(zone->daemon, zone->name);
+       if (!zone->link)
+               return NULL;
+
+       return nw_link_ref(zone->link);
 }
 
 // Carrier
 
 int nw_zone_has_carrier(nw_zone* zone) {
-       int has_carrier = 0;
-
-       // Fetch link
-       nw_link* link = nw_zone_get_link(zone);
-       if (link) {
-               has_carrier = nw_link_has_carrier(link);
-               nw_link_unref(link);
-       }
+       if (!zone->link)
+               return 0;
 
-       return has_carrier;
+       return nw_link_has_carrier(zone->link);
 }
 
 /*