Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
-const nw_port_info_t nw_port_info_bonding = {
+const nw_port_type_t nw_port_type_bonding = {
- // 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) {
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);
-const nw_port_info_t nw_port_info_dummy = {
+const nw_port_type_t nw_port_type_dummy = {
-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 */
-const nw_port_info_t nw_port_info_vlan = {
+const nw_port_type_t nw_port_type_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,
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);
#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 },
-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)
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);
}
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
// Initialize reference counter
p->nrefs = 1;
// Initialize reference counter
p->nrefs = 1;
- // Store the type
- p->type = type;
-
- 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;
- 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);
nw_port* dropped_port = (nw_port*)data;
int r;
nw_port* dropped_port = (nw_port*)data;
int r;
+ 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);
}
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 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) {
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) {
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;
// 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);
-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;
- 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 (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);
#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;
// 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;
+ const nw_port_type_t* type;
char name[IF_NAMESIZE];
// Configuration
char name[IF_NAMESIZE];
// Configuration
// 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;
};
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);
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);