-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: unify how constant names are exposed and used
This patch moves all the various enum-to-string arrays into a single compilation unit instead of exposing them directly into headers, and exposes the data through simple functions. This approach has multiple advantages: * If we want to expose one or several of those conversion functions to the ABI, we now can add new constants without breaking the ABI, as the functions can deal with unknown constants in a sensible manner. Directly exposing the arrays in the headers as was previously done was a recipy for disaster, as the array is baked into the compiled client code, and will not be resized if we decide to add new values to the enums in new versions of the library. * It's marginally faster to compile, as the compiler doesn't have to copy around multiple times the same arrays, only to deduplicate them at link time (if it even does that). Note that the naming scheme for the new functions differs from all the previous API, using the `np_` prefix. This is because this patch is one of the first parts of a broader effort to create an actual library experience around libnetplan, which will need to basically change the whole current API. Using a new prefix allows me to quickly see if a symbol is from the legacy ABI or the new API/ABI. I'm using this scheme here as I'm anticipating that we'll want to expose them in our public API at some point (we already have internal consumers outside of the library, anyway).
- Loading branch information
1 parent
17d3848
commit d950faa
Showing
11 changed files
with
141 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#include <glib.h> | ||
|
||
#include "names.h" | ||
#include "parse.h" | ||
|
||
/* Non-static as we need it for ABI compatibility, see at the end of the file */ | ||
const char* const np_backend_to_str[NETPLAN_BACKEND_MAX_] = { | ||
[NETPLAN_BACKEND_NONE] = "none", | ||
[NETPLAN_BACKEND_NETWORKD] = "networkd", | ||
[NETPLAN_BACKEND_NM] = "NetworkManager", | ||
[NETPLAN_BACKEND_OVS] = "OpenVSwitch", | ||
}; | ||
|
||
static const char* const np_wifi_mode_to_str[NETPLAN_WIFI_MODE_MAX_] = { | ||
[NETPLAN_WIFI_MODE_INFRASTRUCTURE] = "infrastructure", | ||
[NETPLAN_WIFI_MODE_ADHOC] = "adhoc", | ||
[NETPLAN_WIFI_MODE_AP] = "ap", | ||
[NETPLAN_WIFI_MODE_OTHER] = NULL, | ||
}; | ||
|
||
|
||
static const char* const np_def_type_to_str[NETPLAN_DEF_TYPE_MAX_] = { | ||
[NETPLAN_DEF_TYPE_NONE] = NULL, | ||
[NETPLAN_DEF_TYPE_ETHERNET] = "ethernets", | ||
[NETPLAN_DEF_TYPE_WIFI] = "wifis", | ||
[NETPLAN_DEF_TYPE_MODEM] = "modems", | ||
[NETPLAN_DEF_TYPE_BRIDGE] = "bridges", | ||
[NETPLAN_DEF_TYPE_BOND] = "bonds", | ||
[NETPLAN_DEF_TYPE_VLAN] = "vlans", | ||
[NETPLAN_DEF_TYPE_TUNNEL] = "tunnels", | ||
[NETPLAN_DEF_TYPE_PORT] = NULL, | ||
[NETPLAN_DEF_TYPE_NM] = "nm-devices", | ||
}; | ||
|
||
static const char* const np_auth_key_management_type_to_str[NETPLAN_AUTH_KEY_MANAGEMENT_MAX] = { | ||
[NETPLAN_AUTH_KEY_MANAGEMENT_NONE] = "none", | ||
[NETPLAN_AUTH_KEY_MANAGEMENT_WPA_PSK] = "psk", | ||
[NETPLAN_AUTH_KEY_MANAGEMENT_WPA_EAP] = "eap", | ||
[NETPLAN_AUTH_KEY_MANAGEMENT_8021X] = "802.1x", | ||
}; | ||
|
||
static const char* const np_auth_eap_method_to_str[NETPLAN_AUTH_EAP_METHOD_MAX] = { | ||
[NETPLAN_AUTH_EAP_NONE] = NULL, | ||
[NETPLAN_AUTH_EAP_TLS] = "tls", | ||
[NETPLAN_AUTH_EAP_PEAP] = "peap", | ||
[NETPLAN_AUTH_EAP_TTLS] = "ttls", | ||
}; | ||
|
||
static const char* const np_tunnel_mode_to_str[NETPLAN_TUNNEL_MODE_MAX_] = { | ||
[NETPLAN_TUNNEL_MODE_UNKNOWN] = NULL, | ||
[NETPLAN_TUNNEL_MODE_IPIP] = "ipip", | ||
[NETPLAN_TUNNEL_MODE_GRE] = "gre", | ||
[NETPLAN_TUNNEL_MODE_SIT] = "sit", | ||
[NETPLAN_TUNNEL_MODE_ISATAP] = "isatap", | ||
[NETPLAN_TUNNEL_MODE_VTI] = "vti", | ||
[NETPLAN_TUNNEL_MODE_IP6IP6] = "ip6ip6", | ||
[NETPLAN_TUNNEL_MODE_IPIP6] = "ipip6", | ||
[NETPLAN_TUNNEL_MODE_IP6GRE] = "ip6gre", | ||
[NETPLAN_TUNNEL_MODE_VTI6] = "vti6", | ||
[NETPLAN_TUNNEL_MODE_GRETAP] = "gretap", | ||
[NETPLAN_TUNNEL_MODE_IP6GRETAP] = "ip6gretap", | ||
[NETPLAN_TUNNEL_MODE_WIREGUARD] = "wireguard", | ||
}; | ||
|
||
static const char* const np_addr_gen_mode_to_str[NETPLAN_ADDRGEN_MAX] = { | ||
[NETPLAN_ADDRGEN_DEFAULT] = NULL, | ||
[NETPLAN_ADDRGEN_EUI64] = "eui64", | ||
[NETPLAN_ADDRGEN_STABLEPRIVACY] = "stable-privacy" | ||
}; | ||
|
||
#define NAME_FUNCTION(_radical, _type) const char *np_ ## _radical ## _name( _type val) \ | ||
{ \ | ||
return (val < sizeof( np_ ## _radical ## _to_str )) ? np_ ## _radical ## _to_str [val] : NULL; \ | ||
} | ||
|
||
NAME_FUNCTION(backend, NetplanBackend); | ||
NAME_FUNCTION(def_type, NetplanDefType); | ||
NAME_FUNCTION(auth_key_management_type, NetplanAuthKeyManagementType); | ||
NAME_FUNCTION(auth_eap_method, NetplanAuthEAPMethod); | ||
NAME_FUNCTION(tunnel_mode, NetplanTunnelMode); | ||
NAME_FUNCTION(addr_gen_mode, NetplanAddrGenMode); | ||
NAME_FUNCTION(wifi_mode, NetplanWifiMode); | ||
|
||
/* ABI compatibility definitions */ | ||
|
||
const char * | ||
tunnel_mode_to_string(NetplanTunnelMode val) __attribute__ ((alias ("np_tunnel_mode_name"))); | ||
|
||
extern const char* netplan_backend_to_name __attribute__((alias("np_backend_to_str"))); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include "netplan.h" | ||
|
||
const char* | ||
np_backend_name(NetplanBackend val); | ||
|
||
const char* | ||
np_def_type_name(NetplanDefType val); | ||
|
||
const char* | ||
np_auth_key_management_type_name(NetplanAuthKeyManagementType val); | ||
|
||
const char* | ||
np_auth_eap_method_name(NetplanAuthEAPMethod val); | ||
|
||
const char* | ||
np_tunnel_mode_name(NetplanTunnelMode val); | ||
|
||
const char* | ||
np_addr_gen_mode_name(NetplanAddrGenMode val); | ||
|
||
const char* | ||
np_wifi_mode_name(NetplanWifiMode val); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.