From: Michael Tremer Date: Thu, 8 Jun 2023 16:19:05 +0000 (+0000) Subject: ports: Unify type X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=c464b5d1e146402f153f98a5e5d04b76b4d74e10;p=network.git ports: Unify type Signed-off-by: Michael Tremer --- diff --git a/src/networkd/port-bonding.c b/src/networkd/port-bonding.c index 4e4c8a71..ad02b5d7 100644 --- a/src/networkd/port-bonding.c +++ b/src/networkd/port-bonding.c @@ -76,20 +76,17 @@ ERROR: return r; } -const nw_port_info_t nw_port_info_bonding = { +const nw_port_type_t nw_port_type_bonding = { .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) { diff --git a/src/networkd/port-bonding.h b/src/networkd/port-bonding.h index e21c251d..e5c8c322 100644 --- a/src/networkd/port-bonding.h +++ b/src/networkd/port-bonding.h @@ -39,7 +39,7 @@ struct nw_port_bonding { 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); diff --git a/src/networkd/port-dummy.c b/src/networkd/port-dummy.c index e71e740d..8a440083 100644 --- a/src/networkd/port-dummy.c +++ b/src/networkd/port-dummy.c @@ -20,6 +20,6 @@ #include "port-dummy.h" -const nw_port_info_t nw_port_info_dummy = { +const nw_port_type_t nw_port_type_dummy = { .kind = "dummy", }; diff --git a/src/networkd/port-dummy.h b/src/networkd/port-dummy.h index b74c991e..34a72656 100644 --- a/src/networkd/port-dummy.h +++ b/src/networkd/port-dummy.h @@ -23,6 +23,6 @@ #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 */ diff --git a/src/networkd/port-vlan.c b/src/networkd/port-vlan.c index 56b02749..06047aba 100644 --- a/src/networkd/port-vlan.c +++ b/src/networkd/port-vlan.c @@ -106,22 +106,19 @@ ERROR: return r; } -const nw_port_info_t nw_port_info_vlan = { +const nw_port_type_t nw_port_type_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, }; /* diff --git a/src/networkd/port-vlan.h b/src/networkd/port-vlan.h index b4df5627..6ecf6a40 100644 --- a/src/networkd/port-vlan.h +++ b/src/networkd/port-vlan.h @@ -45,7 +45,7 @@ struct nw_port_vlan { 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); diff --git a/src/networkd/port.c b/src/networkd/port.c index cab8fc5f..c4d6ffe3 100644 --- a/src/networkd/port.c +++ b/src/networkd/port.c @@ -37,26 +37,14 @@ #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_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) @@ -119,8 +107,8 @@ static int nw_port_setup(nw_port* port) { 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; } @@ -140,7 +128,7 @@ ERROR: } 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 @@ -154,21 +142,18 @@ int nw_port_create(nw_port** port, nw_daemon* daemon, // Initialize reference counter p->nrefs = 1; - // Store the type - p->type = type; - // Set operations - switch (p->type) { + switch (type) { case NW_PORT_BONDING: - p->info = &nw_port_info_bonding; + p->type = &nw_port_type_bonding; break; case NW_PORT_DUMMY: - p->info = &nw_port_info_dummy; + p->type = &nw_port_type_dummy; break; case NW_PORT_VLAN: - p->info = &nw_port_info_vlan; + p->type = &nw_port_type_vlan; break; } @@ -214,7 +199,7 @@ int nw_port_create_from_config(nw_port** port, nw_daemon* daemon, } // 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; @@ -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; - 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); @@ -392,10 +377,10 @@ static int nw_port_is_disabled(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 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) { @@ -444,6 +429,13 @@ static int nw_port_create_link(nw_port* port) { 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) { @@ -481,14 +473,14 @@ static int nw_port_create_link(nw_port* port) { 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; } - 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; @@ -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 { - 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; } @@ -588,8 +580,8 @@ int nw_port_update_stats(nw_port* port) { 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; @@ -616,7 +608,7 @@ int nw_port_to_json(nw_port* port, struct json_object** object) { 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; @@ -629,8 +621,8 @@ int nw_port_to_json(nw_port* port, struct json_object** object) { } // 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; } diff --git a/src/networkd/port.h b/src/networkd/port.h index ef68a892..c9add717 100644 --- a/src/networkd/port.h +++ b/src/networkd/port.h @@ -31,50 +31,49 @@ #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; -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; - 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; @@ -83,7 +82,7 @@ struct nw_port { // Link nw_link* link; - nw_port_type_t type; + const nw_port_type_t* type; char name[IF_NAMESIZE]; // Configuration @@ -92,9 +91,6 @@ struct nw_port { // Common attributes nw_address_t address; - // Type Operations - const nw_port_info_t* info; - // Bonding Settings struct nw_port_bonding bonding; @@ -103,7 +99,7 @@ struct nw_port { }; 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); @@ -130,7 +126,7 @@ int nw_port_reconfigure(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);