]> git.ipfire.org Git - people/ms/network.git/commitdiff
ports: Unify type
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Jun 2023 16:19:05 +0000 (16:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 8 Jun 2023 16:19:05 +0000 (16:19 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/port-bonding.c
src/networkd/port-bonding.h
src/networkd/port-dummy.c
src/networkd/port-dummy.h
src/networkd/port-vlan.c
src/networkd/port-vlan.h
src/networkd/port.c
src/networkd/port.h

index 4e4c8a71786ba688afa637aceb843e32cfc16a8b..ad02b5d7d2c2da7d0df275fb8bafee0c389cda39 100644 (file)
@@ -76,20 +76,17 @@ ERROR:
        return r;
 }
 
        return r;
 }
 
-const nw_port_info_t nw_port_info_bonding = {
+const nw_port_type_t nw_port_type_bonding = {
        .kind = "bond",
 
        .kind = "bond",
 
-       // Operations
-       .ops = {
-               // Configuration
-               .setup = nw_port_bonding_setup,
+       // Configuration
+       .setup = nw_port_bonding_setup,
 
 
-               // Link
-               .create_link = nw_port_bonding_create_link,
+       // Link
+       .create_link = nw_port_bonding_create_link,
 
 
-               // JSON
-               .to_json = nw_port_bonding_to_json,
-       },
+       // JSON
+       .to_json = nw_port_bonding_to_json,
 };
 
 const char* nw_port_bonding_get_mode(nw_port* port) {
 };
 
 const char* nw_port_bonding_get_mode(nw_port* port) {
index e21c251d131ee581ac0485855e12f7a472c32b06..e5c8c3221e2d28be34bd84c48537d991ae672686 100644 (file)
@@ -39,7 +39,7 @@ struct nw_port_bonding {
        nw_port_bonding_mode_t mode;
 };
 
        nw_port_bonding_mode_t mode;
 };
 
-extern const nw_port_info_t nw_port_info_bonding;
+extern const nw_port_type_t nw_port_type_bonding;
 
 const char* nw_port_bonding_get_mode(nw_port* port);
 int nw_port_bonding_set_mode(nw_port* port, const char* mode);
 
 const char* nw_port_bonding_get_mode(nw_port* port);
 int nw_port_bonding_set_mode(nw_port* port, const char* mode);
index e71e740dafb471dcf0fd56eb5af8f424b0c9cd85..8a4400837d09e13274728d50f1db752dc91e4c1a 100644 (file)
@@ -20,6 +20,6 @@
 
 #include "port-dummy.h"
 
 
 #include "port-dummy.h"
 
-const nw_port_info_t nw_port_info_dummy = {
+const nw_port_type_t nw_port_type_dummy = {
        .kind = "dummy",
 };
        .kind = "dummy",
 };
index b74c991eb80e58fddb3fa626fcee21e2fa506eb4..34a72656e96083a5b48ee170a3cb7a1bded66a42 100644 (file)
@@ -23,6 +23,6 @@
 
 #include "port.h"
 
 
 #include "port.h"
 
-extern const nw_port_info_t nw_port_info_dummy;
+extern const nw_port_type_t nw_port_type_dummy;
 
 #endif /* NETWORKD_PORT_DUMMY_H */
 
 #endif /* NETWORKD_PORT_DUMMY_H */
index 56b02749318df57ab03a5b747159ce18304ab5c7..06047abad66e9120d9740a556f0aeb0a332ff0f4 100644 (file)
@@ -106,22 +106,19 @@ ERROR:
        return r;
 }
 
        return r;
 }
 
-const nw_port_info_t nw_port_info_vlan = {
+const nw_port_type_t nw_port_type_vlan = {
        .kind = "vlan",
 
        .kind = "vlan",
 
-       // Operations
-       .ops = {
-               // Configuration
-               .setup = nw_port_vlan_setup,
+       // Configuration
+       .setup = nw_port_vlan_setup,
 
 
-               .get_parent_port = nw_port_get_vlan_parent,
+       .get_parent_port = nw_port_get_vlan_parent,
 
 
-               // Link
-               .create_link = nw_port_vlan_create_link,
+       // Link
+       .create_link = nw_port_vlan_create_link,
 
 
-               // JSON
-               .to_json = nw_port_vlan_to_json,
-       },
+       // JSON
+       .to_json = nw_port_vlan_to_json,
 };
 
 /*
 };
 
 /*
index b4df56270ccf28023ba250540d7c8a8b6c3809be..6ecf6a40881e9280da65b9d63c76f8e0cb219fae 100644 (file)
@@ -45,7 +45,7 @@ struct nw_port_vlan {
        char __parent_name[IF_NAMESIZE];
 };
 
        char __parent_name[IF_NAMESIZE];
 };
 
-extern const nw_port_info_t nw_port_info_vlan;
+extern const nw_port_type_t nw_port_type_vlan;
 
 // ID
 int nw_port_get_vlan_id(nw_port* port);
 
 // ID
 int nw_port_get_vlan_id(nw_port* port);
index cab8fc5f96003ff82704984284a84158fcdf132f..c4d6ffe38034849a69cdd411873410002466f5ba 100644 (file)
 #include "stats-collector.h"
 #include "string.h"
 
 #include "stats-collector.h"
 #include "string.h"
 
-static const struct nw_port_type_map {
-       nw_port_type_t type;
-       const char* name;
-} nw_port_type_map[] = {
+static const nw_string_table_t nw_port_type_id[] = {
        { NW_PORT_BONDING, "bonding" },
        { NW_PORT_DUMMY,   "dummy" },
        { NW_PORT_VLAN,    "vlan" },
        { NW_PORT_BONDING, "bonding" },
        { NW_PORT_DUMMY,   "dummy" },
        { NW_PORT_VLAN,    "vlan" },
-       { NW_PORT_UNKNOWN, NULL },
+       { -1, NULL },
 };
 
 };
 
-static nw_port_type_t nw_port_type_from_string(const char* s) {
-       const struct nw_port_type_map* map = NULL;
-
-       for (map = nw_port_type_map; *map->name; map++) {
-               if (strcmp(map->name, s) == 0)
-                       return map->type;
-       }
-
-       return NW_PORT_UNKNOWN;
-}
+NW_STRING_TABLE_LOOKUP(nw_port_type_id_t, nw_port_type_id)
 
 static void nw_port_free(nw_port* port) {
        if (port->link)
 
 static void nw_port_free(nw_port* port) {
        if (port->link)
@@ -119,8 +107,8 @@ static int nw_port_setup(nw_port* port) {
                goto ERROR;
 
        // Call any custom initialization
                goto ERROR;
 
        // Call any custom initialization
-       if (NW_PORT_OPS(port)->setup) {
-               r = NW_PORT_OPS(port)->setup(port);
+       if (NW_PORT_TYPE(port)->setup) {
+               r = NW_PORT_TYPE(port)->setup(port);
                if (r < 0)
                        goto ERROR;
        }
                if (r < 0)
                        goto ERROR;
        }
@@ -140,7 +128,7 @@ ERROR:
 }
 
 int nw_port_create(nw_port** port, nw_daemon* daemon,
 }
 
 int nw_port_create(nw_port** port, nw_daemon* daemon,
-               nw_port_type_t type, const char* name, nw_config* config) {
+               const nw_port_type_id_t type, const char* name, nw_config* config) {
        int r;
 
        // Allocate a new object
        int r;
 
        // Allocate a new object
@@ -154,21 +142,18 @@ int nw_port_create(nw_port** port, nw_daemon* daemon,
        // Initialize reference counter
        p->nrefs = 1;
 
        // Initialize reference counter
        p->nrefs = 1;
 
-       // Store the type
-       p->type = type;
-
        // Set operations
        // Set operations
-       switch (p->type) {
+       switch (type) {
                case NW_PORT_BONDING:
                case NW_PORT_BONDING:
-                       p->info = &nw_port_info_bonding;
+                       p->type = &nw_port_type_bonding;
                        break;
 
                case NW_PORT_DUMMY:
                        break;
 
                case NW_PORT_DUMMY:
-                       p->info = &nw_port_info_dummy;
+                       p->type = &nw_port_type_dummy;
                        break;
 
                case NW_PORT_VLAN:
                        break;
 
                case NW_PORT_VLAN:
-                       p->info = &nw_port_info_vlan;
+                       p->type = &nw_port_type_vlan;
                        break;
        }
 
                        break;
        }
 
@@ -214,7 +199,7 @@ int nw_port_create_from_config(nw_port** port, nw_daemon* daemon,
        }
 
        // Create a new port
        }
 
        // Create a new port
-       r = nw_port_create(port, daemon, nw_port_type_from_string(type), name, config);
+       r = nw_port_create(port, daemon, nw_port_type_id_from_string(type), name, config);
        if (r)
                goto ERROR;
 
        if (r)
                goto ERROR;
 
@@ -286,7 +271,7 @@ int __nw_port_drop_port(nw_daemon* daemon, nw_port* port, void* data) {
        nw_port* dropped_port = (nw_port*)data;
        int r;
 
        nw_port* dropped_port = (nw_port*)data;
        int r;
 
-       switch (port->type) {
+       switch (port->type->id) {
                case NW_PORT_VLAN:
                        if (port->vlan.parent == dropped_port) {
                                r = nw_port_set_vlan_parent(port, NULL);
                case NW_PORT_VLAN:
                        if (port->vlan.parent == dropped_port) {
                                r = nw_port_set_vlan_parent(port, NULL);
@@ -392,10 +377,10 @@ static int nw_port_is_disabled(nw_port* port) {
 }
 
 nw_port* nw_port_get_parent_port(nw_port* port) {
 }
 
 nw_port* nw_port_get_parent_port(nw_port* port) {
-       if (!NW_PORT_OPS(port)->get_parent_port)
+       if (!NW_PORT_TYPE(port)->get_parent_port)
                return NULL;
 
                return NULL;
 
-       return NW_PORT_OPS(port)->get_parent_port(port);
+       return NW_PORT_TYPE(port)->get_parent_port(port);
 }
 
 static nw_link* nw_port_get_parent_link(nw_port* port) {
 }
 
 static nw_link* nw_port_get_parent_link(nw_port* port) {
@@ -444,6 +429,13 @@ static int nw_port_create_link(nw_port* port) {
 
        DEBUG("Creating port %s...\n", port->name);
 
 
        DEBUG("Creating port %s...\n", port->name);
 
+       // Check the kind
+       if (!NW_PORT_TYPE(port)->kind) {
+               ERROR("Port type has no kind\n");
+               r = -ENOTSUP;
+               goto ERROR;
+       }
+
        // Create a new link
        r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
        if (r < 0) {
        // Create a new link
        r = sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0);
        if (r < 0) {
@@ -481,14 +473,14 @@ static int nw_port_create_link(nw_port* port) {
                goto ERROR;
 
        // Run the custom setup
                goto ERROR;
 
        // Run the custom setup
-       if (NW_PORT_OPS(port)->create_link) {
-               r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, NW_PORT_INFO(port)->kind);
+       if (NW_PORT_TYPE(port)->create_link) {
+               r = sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, NW_PORT_TYPE(port)->kind);
                if (r < 0) {
                        ERROR("Could not open IFLA_INFO_DATA container: %s\n", strerror(-r));
                        goto ERROR;
                }
 
                if (r < 0) {
                        ERROR("Could not open IFLA_INFO_DATA container: %s\n", strerror(-r));
                        goto ERROR;
                }
 
-               r = NW_PORT_OPS(port)->create_link(port, m);
+               r = NW_PORT_TYPE(port)->create_link(port, m);
                if (r) {
                        ERROR("Could not create port %s: %m\n", port->name);
                        goto ERROR;
                if (r) {
                        ERROR("Could not create port %s: %m\n", port->name);
                        goto ERROR;
@@ -501,7 +493,7 @@ static int nw_port_create_link(nw_port* port) {
 
        // Just set IFLA_INFO_KIND if there is no custom function
        } else {
 
        // Just set IFLA_INFO_KIND if there is no custom function
        } else {
-               r = sd_netlink_message_append_string(m, IFLA_INFO_KIND, NW_PORT_INFO(port)->kind);
+               r = sd_netlink_message_append_string(m, IFLA_INFO_KIND, NW_PORT_TYPE(port)->kind);
                if (r < 0)
                        goto ERROR;
        }
                if (r < 0)
                        goto ERROR;
        }
@@ -588,8 +580,8 @@ int nw_port_update_stats(nw_port* port) {
        return 0;
 }
 
        return 0;
 }
 
-int nw_port_check_type(nw_port* port, const nw_port_type_t type) {
-       if (port->type == type)
+int nw_port_check_type(nw_port* port, const nw_port_type_id_t type) {
+       if (port->type->id == type)
                return 0;
 
        errno = ENOTSUP;
                return 0;
 
        errno = ENOTSUP;
@@ -616,7 +608,7 @@ int nw_port_to_json(nw_port* port, struct json_object** object) {
                goto ERROR;
 
        // Add Type
                goto ERROR;
 
        // Add Type
-       r = json_object_add_string(o, "Type", NW_PORT_INFO(port)->kind);
+       r = json_object_add_string(o, "Type", nw_port_type_id_to_string(port->type->id));
        if (r < 0)
                goto ERROR;
 
        if (r < 0)
                goto ERROR;
 
@@ -629,8 +621,8 @@ int nw_port_to_json(nw_port* port, struct json_object** object) {
        }
 
        // Call custom stuff
        }
 
        // Call custom stuff
-       if (NW_PORT_OPS(port)->to_json) {
-               r = NW_PORT_OPS(port)->to_json(port, o);
+       if (NW_PORT_TYPE(port)->to_json) {
+               r = NW_PORT_TYPE(port)->to_json(port, o);
                if (r < 0)
                        goto ERROR;
        }
                if (r < 0)
                        goto ERROR;
        }
index ef68a892c4a474628a42faad1f2b1eebe600278e..c9add71708f6916a5f626b6bd64b1814f0ef923f 100644 (file)
 
 #define PORT_CONFIG_DIR                        CONFIG_DIR "/ports"
 
 
 #define PORT_CONFIG_DIR                        CONFIG_DIR "/ports"
 
-typedef enum nw_port_type {
-       NW_PORT_UNKNOWN = 0,
-       NW_PORT_BONDING,
-       NW_PORT_DUMMY,
-       NW_PORT_VLAN,
-} nw_port_type_t;
-
 // VLAN
 #define NW_VLAN_ID_INVALID             0
 #define NW_VLAN_ID_MIN                 1
 #define NW_VLAN_ID_MAX                 4096
 
 typedef struct nw_port nw_port;
 // VLAN
 #define NW_VLAN_ID_INVALID             0
 #define NW_VLAN_ID_MIN                 1
 #define NW_VLAN_ID_MAX                 4096
 
 typedef struct nw_port nw_port;
-typedef struct nw_port_info nw_port_info_t;
 
 
-#include "address.h"
-#include "config.h"
-#include "daemon.h"
-#include "json.h"
-#include "port-bonding.h"
-#include "port-vlan.h"
+typedef enum nw_port_type_id {
+       NW_PORT_UNKNOWN = 0,
+       NW_PORT_BONDING,
+       NW_PORT_DUMMY,
+       NW_PORT_VLAN,
+} nw_port_type_id_t;
+
+typedef struct nw_port_type {
+       // Type ID
+       nw_port_type_id_t id;
 
 
-struct nw_port_info {
        // IFLA_INFO_KIND/IFLA_INFO_DATA
        const char* kind;
 
        // IFLA_INFO_KIND/IFLA_INFO_DATA
        const char* kind;
 
-       struct nw_port_ops {
-               // Configuration
-               int (*setup)(nw_port* port);
+       // Configuration
+       int (*setup)(nw_port* port);
 
 
-               // Get Parent Port
-               nw_port* (*get_parent_port)(nw_port* port);
+       // Get Parent Port
+       nw_port* (*get_parent_port)(nw_port* port);
 
 
-               // Link
-               int (*create_link)(nw_port* port, sd_netlink_message* message);
-               int (*destroy_link)(nw_port* port);
+       // Link
+       int (*create_link)(nw_port* port, sd_netlink_message* message);
+       int (*destroy_link)(nw_port* port);
 
 
-               // JSON
-               int (*to_json)(nw_port* port, struct json_object* object);
-       } ops;
-};
+       // JSON
+       int (*to_json)(nw_port* port, struct json_object* object);
+} nw_port_type_t;
+
+#include "address.h"
+#include "config.h"
+#include "daemon.h"
+#include "json.h"
+#include "port-bonding.h"
+#include "port-vlan.h"
 
 
-#define NW_PORT_INFO(port) (port->info)
-#define NW_PORT_OPS(port) (&NW_PORT_INFO(port)->ops)
+#define NW_PORT_TYPE(port) (port->type)
 
 struct nw_port {
        nw_daemon* daemon;
 
 struct nw_port {
        nw_daemon* daemon;
@@ -83,7 +82,7 @@ struct nw_port {
        // Link
        nw_link* link;
 
        // Link
        nw_link* link;
 
-       nw_port_type_t type;
+       const nw_port_type_t* type;
        char name[IF_NAMESIZE];
 
        // Configuration
        char name[IF_NAMESIZE];
 
        // Configuration
@@ -92,9 +91,6 @@ struct nw_port {
        // Common attributes
        nw_address_t address;
 
        // Common attributes
        nw_address_t address;
 
-       // Type Operations
-       const nw_port_info_t* info;
-
        // Bonding Settings
        struct nw_port_bonding bonding;
 
        // Bonding Settings
        struct nw_port_bonding bonding;
 
@@ -103,7 +99,7 @@ struct nw_port {
 };
 
 int nw_port_create(nw_port** port, nw_daemon* daemon,
 };
 
 int nw_port_create(nw_port** port, nw_daemon* daemon,
-       nw_port_type_t type, const char* name, nw_config* config);
+       const nw_port_type_id_t type, const char* name, nw_config* config);
 int nw_port_create_from_config(nw_port** port, nw_daemon* daemon,
        const char* name, const char* path);
 
 int nw_port_create_from_config(nw_port** port, nw_daemon* daemon,
        const char* name, const char* path);
 
@@ -130,7 +126,7 @@ int nw_port_reconfigure(nw_port* port);
 
 int nw_port_has_carrier(nw_port* port);
 
 
 int nw_port_has_carrier(nw_port* port);
 
-int nw_port_check_type(nw_port* port, const nw_port_type_t type);
+int nw_port_check_type(nw_port* port, const nw_port_type_id_t type);
 
 // Stats
 const struct rtnl_link_stats64* nw_port_get_stats64(nw_port* port);
 
 // Stats
 const struct rtnl_link_stats64* nw_port_get_stats64(nw_port* port);