]> git.ipfire.org Git - people/ms/network.git/commitdiff
ports: Implement destroying a port
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Apr 2023 11:46:22 +0000 (11:46 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 15 Apr 2023 11:46:22 +0000 (11:46 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/config.c
src/networkd/config.h
src/networkd/link.c
src/networkd/port.c
src/networkd/port.h
src/networkd/zone.c
src/networkd/zone.h

index 27cfad8ad76213cc983a7188d6313ff1863ad9ee..8269edeb1fe1fd5642a2c1cc1725f85b8b187d82 100644 (file)
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/queue.h>
+#include <unistd.h>
 
 #include "config.h"
 #include "logging.h"
@@ -135,6 +136,17 @@ nw_config* nw_config_unref(nw_config* config) {
        return NULL;
 }
 
+int nw_config_destroy(nw_config* config) {
+       int r;
+
+       // Drop all entries
+       r = nw_config_flush(config);
+       if (r)
+               return r;
+
+       return unlink(config->path);
+}
+
 const char* nw_config_path(nw_config* config) {
        if (*config->path)
                return config->path;
index 0b25f75467206b2395b598ba8ad2007f281c60ff..e17c0167fa32dfca2c1c0ef433b28ff475601916 100644 (file)
@@ -33,6 +33,8 @@ int nw_config_create(nw_config** config, const char* path);
 nw_config* nw_config_ref(nw_config* config);
 nw_config* nw_config_unref(nw_config* config);
 
+int nw_config_destroy(nw_config* config);
+
 const char* nw_config_path(nw_config* config);
 
 int nw_config_flush(nw_config* config);
index 1edf20d72594718020a9ae1e5ef9a7772be840fb..603aabe884f1a94e0ab446c903362ff8e1dd36f4 100644 (file)
@@ -308,12 +308,14 @@ static int nw_link_update_flags(nw_link* link, sd_netlink_message* message) {
                return 1;
        }
 
+#if 0
        // Fetch operstate
        r = sd_netlink_message_read_u8(message, IFLA_OPERSTATE, &operstate);
        if (r < 1) {
                ERROR("Could not read operstate: %m\n");
                return 1;
        }
+#endif
 
        // End here if there have been no changes
        if (link->flags == flags && link->operstate == operstate)
index a240c6118a6b0af501b23eef7aba7f32c8520b81..23ffd0bd59c3e6feacd27368f278feeeef0660d7 100644 (file)
@@ -314,6 +314,60 @@ nw_port* nw_port_unref(nw_port* port) {
        return NULL;
 }
 
+int nw_port_destroy(nw_port* port) {
+       int r;
+
+       DEBUG("Destroying port %s\n", port->name);
+
+       // Destroy the physical link (if exists)
+       if (port->link) {
+               r = nw_link_destroy(port->link);
+               if (r)
+                       return r;
+       }
+
+       // Dereference the port from other ports
+       r = nw_daemon_ports_walk(port->daemon, __nw_port_drop_port, port);
+       if (r)
+               return r;
+
+       // Dereference the port from other zones
+       r = nw_daemon_zones_walk(port->daemon, __nw_zone_drop_port, port);
+       if (r)
+               return r;
+
+       // Destroy the configuration
+       r = nw_config_destroy(port->config);
+       if (r)
+               return r;
+
+       // Reset type
+       port->type = NW_PORT_UNKNOWN;
+
+       return 0;
+}
+
+int __nw_port_drop_port(nw_daemon* daemon, nw_port* port, void* data) {
+       nw_port* dropped_port = (nw_port*)data;
+       int r;
+
+       switch (port->type) {
+               case NW_PORT_VLAN:
+                       if (port->vlan.parent == dropped_port) {
+                               r = nw_port_set_vlan_parent(port, NULL);
+                               if (r)
+                                       return r;
+                       }
+                       break;
+
+               case NW_PORT_DUMMY:
+               case NW_PORT_UNKNOWN:
+                       break;
+       }
+
+       return 0;
+}
+
 static int nw_port_save_vlan(nw_port* port) {
        int r;
 
index d89e1e0634a755080adb445e4262ffbb00b033c8..032d82c7f9cf31bc3b237770e4af0144704476ee 100644 (file)
@@ -47,6 +47,9 @@ int nw_port_create_from_config(nw_port** port, nw_daemon* daemon,
 nw_port* nw_port_ref(nw_port* port);
 nw_port* nw_port_unref(nw_port* port);
 
+int nw_port_destroy(nw_port* port);
+int __nw_port_drop_port(nw_daemon* daemon, nw_port* port, void* data);
+
 int nw_port_save(nw_port* port);
 
 const char* nw_port_name(nw_port* port);
index 3f47a26d6480fa6de125c14ac2c86d90e96d3b42..9f5b7f8cba45bfbb6afbe2bbfe1a9aa7e39216e9 100644 (file)
@@ -183,6 +183,15 @@ nw_zone* nw_zone_unref(nw_zone* zone) {
        return NULL;
 }
 
+int __nw_zone_drop_port(nw_daemon* daemon, nw_zone* zone, void* data) {
+       nw_port* dropped_port = (nw_port*)data;
+
+       // XXX TODO
+       (void)dropped_port;
+
+       return 0;
+}
+
 int nw_zone_save(nw_zone* zone) {
        int r;
 
index 9737b4591381e862685d6abc35e7c1a33b550d65..480440fbda58ae87cf47fd94ab58d26a42964eb9 100644 (file)
@@ -37,6 +37,8 @@ int nw_zone_create(nw_zone** zone, nw_daemon* daemon, const char* name);
 nw_zone* nw_zone_ref(nw_zone* zone);
 nw_zone* nw_zone_unref(nw_zone* zone);
 
+int __nw_zone_drop_port(nw_daemon* daemon, nw_zone* zone, void* data);
+
 int nw_zone_save(nw_zone* zone);
 
 const char* nw_zone_name(nw_zone* zone);