]> git.ipfire.org Git - people/ms/network.git/commitdiff
networkd: ports: Keep a permanent reference to links
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 11:57:48 +0000 (11:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Apr 2023 12:02:13 +0000 (12:02 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/port.c

index c6c8781d105136fdf05242fcd6f5c47d85723fe2..3c4f0b3eef9c7009b1f9e2c972cdf2a5d31ea33d 100644 (file)
@@ -36,6 +36,9 @@ struct nw_port {
        nw_daemon* daemon;
        int nrefs;
 
+       // Link
+       nw_link* link;
+
        char name[IF_NAMESIZE];
        nw_port_type_t type;
 
@@ -66,6 +69,8 @@ static nw_port_type_t nw_port_type_from_string(const char* s) {
 }
 
 static void nw_port_free(nw_port* port) {
+       if (port->link)
+               nw_link_unref(port->link);
        if (port->config)
                nw_config_unref(port->config);
        if (port->daemon)
@@ -134,6 +139,14 @@ static int nw_port_setup(nw_port* port) {
        char path[PATH_MAX];
        int r;
 
+       // Find the link
+       port->link = nw_daemon_get_link_by_name(port->daemon, port->name);
+       if (port->link) {
+               DEBUG("%s: Found matching link %d\n", port->name, nw_link_ifindex(port->link));
+       } else {
+               DEBUG("%s: Could not find matching link\n", port->name);
+       }
+
        // Compose the path to the main configuration file
        r = nw_path_join(path, PORT_CONFIG_DIR, port->name);
        if (r)
@@ -240,7 +253,10 @@ char* nw_port_bus_path(nw_port* port) {
 }
 
 static nw_link* nw_port_get_link(nw_port* port) {
-       return nw_daemon_get_link_by_name(port->daemon, port->name);
+       if (!port->link)
+               return NULL;
+
+       return nw_link_ref(port->link);
 }
 
 const nw_address_t* nw_port_get_address(nw_port* port) {
@@ -248,14 +264,8 @@ const nw_address_t* nw_port_get_address(nw_port* port) {
 }
 
 int nw_port_has_carrier(nw_port* port) {
-       int has_carrier = 0;
-
-       // Fetch link
-       nw_link* link = nw_port_get_link(port);
-       if (link) {
-               has_carrier = nw_link_has_carrier(link);
-               nw_link_unref(link);
-       }
+       if (!port->link)
+               return 0;
 
-       return has_carrier;
+       return nw_link_has_carrier(port->link);
 }