platform: add duplicate of NMSettingWiredWakeOnLan to nm-base for platform

Currently src/platform depends on libnm-core. libnm-core is large
optimally we have a better separation between our code. That means
libnm-core does not depend on platform and vice versa.

However, nm-platform re-uses some enums from libnm-core for internal code.
To avoid that dependency, add _NMSettingWiredWakeOnLan as a duplicate to
nm-base/nm-base.h. nm-base can both be used by libnm-core, nm-platform
and src/platform.

The only problem is that NMSettingWiredWakeOnLan is also part of public
API of libnm. It means, we must duplicate the enum. But with several
static assertions in unit tests I think that is not a problem to do.
This commit is contained in:
Thomas Haller
2021-01-08 17:01:45 +01:00
parent e5d2a05ad5
commit 2bb5c8b13b
8 changed files with 110 additions and 23 deletions

View File

@@ -20,6 +20,7 @@
#error Cannot use this header.
#endif
#include "nm-base/nm-base.h"
#include "nm-connection.h"
#include "nm-core-enum-types.h"
#include "nm-core-types-internal.h"
@@ -215,6 +216,20 @@ nm_bluetooth_capability_to_string(NMBluetoothCapabilities capabilities, char *bu
/*****************************************************************************/
static inline _NMSettingWiredWakeOnLan
_NM_SETTING_WIRED_WAKE_ON_LAN_CAST(NMSettingWiredWakeOnLan v)
{
/* _NMSettingWiredWakeOnLan and NMSettingWiredWakeOnLan enums are really
* the same.
*
* The former is used by nm-platform (which should have no libnm-core dependency),
* the latter is used by libnm-core. A unit test ensures they are exactly the same,
* so we can just cast them. */
return (_NMSettingWiredWakeOnLan) v;
}
/*****************************************************************************/
typedef enum { /*< skip >*/
NM_SETTING_PARSE_FLAGS_NONE = 0,
NM_SETTING_PARSE_FLAGS_STRICT = 1LL << 0,

View File

@@ -17,6 +17,7 @@
#include "nm-glib-aux/nm-enum-utils.h"
#include "nm-glib-aux/nm-str-buf.h"
#include "nm-glib-aux/nm-json-aux.h"
#include "nm-base/nm-base.h"
#include "systemd/nm-sd-utils-shared.h"
#include "nm-utils.h"
@@ -97,6 +98,57 @@ test_nm_ascii_spaces(void)
/*****************************************************************************/
static void
test_wired_wake_on_lan_enum(void)
{
nm_auto_unref_gtypeclass GFlagsClass *flags_class = NULL;
gs_unref_hashtable GHashTable *vals = g_hash_table_new(nm_direct_hash, NULL);
guint i;
G_STATIC_ASSERT_EXPR(sizeof(NMSettingWiredWakeOnLan) == sizeof(_NMSettingWiredWakeOnLan));
G_STATIC_ASSERT_EXPR(sizeof(NMSettingWiredWakeOnLan) < sizeof(gint64));
G_STATIC_ASSERT_EXPR(sizeof(NMSettingWiredWakeOnLan) < sizeof(gint64));
g_assert((((gint64)((NMSettingWiredWakeOnLan) -1)) < 0)
== (((gint64)((_NMSettingWiredWakeOnLan) -1)) < 0));
#define _E(n) \
G_STMT_START \
{ \
G_STATIC_ASSERT_EXPR(n == (gint64) _##n); \
G_STATIC_ASSERT_EXPR(_##n == (gint64) n); \
g_assert(_##n == _NM_SETTING_WIRED_WAKE_ON_LAN_CAST(n)); \
if (!g_hash_table_add(vals, GUINT_TO_POINTER(n))) \
g_assert_not_reached(); \
} \
G_STMT_END
_E(NM_SETTING_WIRED_WAKE_ON_LAN_NONE);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_PHY);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_UNICAST);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_MULTICAST);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_BROADCAST);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_ARP);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_ALL);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_DEFAULT);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_IGNORE);
_E(NM_SETTING_WIRED_WAKE_ON_LAN_EXCLUSIVE_FLAGS);
#undef _E
flags_class = G_FLAGS_CLASS(g_type_class_ref(NM_TYPE_SETTING_WIRED_WAKE_ON_LAN));
for (i = 0; i < flags_class->n_values; i++) {
const GFlagsValue *value = &flags_class->values[i];
if (!g_hash_table_contains(vals, GUINT_TO_POINTER(value->value))) {
g_error("The enum value %s from NMSettingWiredWakeOnLan is not checked for "
"_NMSettingWiredWakeOnLan",
value->value_name);
}
}
}
/*****************************************************************************/
typedef struct _nm_packed {
int v0;
char v1;
@@ -1227,7 +1279,7 @@ _dedup_obj_destroy(NMDedupMultiObj *obj)
{
DedupObj *o = (DedupObj *) obj;
nm_assert(o->parent._ref_count == 0);
g_assert(o->parent._ref_count == 0);
o->parent._ref_count = 1;
o = _dedup_obj_assert(obj);
g_slice_free(DedupObj, o);
@@ -10222,6 +10274,7 @@ main(int argc, char **argv)
nmtst_init(&argc, &argv, TRUE);
g_test_add_func("/core/general/test_nm_ascii_spaces", test_nm_ascii_spaces);
g_test_add_func("/core/general/test_wired_wake_on_lan_enum", test_wired_wake_on_lan_enum);
g_test_add_func("/core/general/test_nm_hash", test_nm_hash);
g_test_add_func("/core/general/test_nm_g_slice_free_fcn", test_nm_g_slice_free_fcn);
g_test_add_func("/core/general/test_c_list_sort", test_c_list_sort);

View File

@@ -139,6 +139,24 @@ nm_ethtool_id_is_ring(NMEthtoolID id)
return id >= _NM_ETHTOOL_ID_RING_FIRST && id <= _NM_ETHTOOL_ID_RING_LAST;
}
/*****************************************************************************/
typedef enum {
_NM_SETTING_WIRED_WAKE_ON_LAN_NONE = 0,
_NM_SETTING_WIRED_WAKE_ON_LAN_PHY = 0x2,
_NM_SETTING_WIRED_WAKE_ON_LAN_UNICAST = 0x4,
_NM_SETTING_WIRED_WAKE_ON_LAN_MULTICAST = 0x8,
_NM_SETTING_WIRED_WAKE_ON_LAN_BROADCAST = 0x10,
_NM_SETTING_WIRED_WAKE_ON_LAN_ARP = 0x20,
_NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC = 0x40,
_NM_SETTING_WIRED_WAKE_ON_LAN_ALL = 0x7E,
_NM_SETTING_WIRED_WAKE_ON_LAN_DEFAULT = 0x1,
_NM_SETTING_WIRED_WAKE_ON_LAN_IGNORE = 0x8000,
_NM_SETTING_WIRED_WAKE_ON_LAN_EXCLUSIVE_FLAGS = 0x8001,
} _NMSettingWiredWakeOnLan;
/****************************************************************************/
#endif /* __NM_LIBNM_BASE_H__ */

View File

@@ -1392,7 +1392,7 @@ wake_on_lan_enable(NMDevice *device)
found:
return nm_platform_ethtool_set_wake_on_lan(nm_device_get_platform(device),
nm_device_get_ifindex(device),
wol,
_NM_SETTING_WIRED_WAKE_ON_LAN_CAST(wol),
password);
}

View File

@@ -1405,9 +1405,9 @@ nmp_utils_ethtool_set_link_settings(int ifindex,
}
gboolean
nmp_utils_ethtool_set_wake_on_lan(int ifindex,
NMSettingWiredWakeOnLan wol,
const char * wol_password)
nmp_utils_ethtool_set_wake_on_lan(int ifindex,
_NMSettingWiredWakeOnLan wol,
const char * wol_password)
{
struct ethtool_wolinfo wol_info = {
.cmd = ETHTOOL_SWOL,
@@ -1416,7 +1416,7 @@ nmp_utils_ethtool_set_wake_on_lan(int ifindex,
g_return_val_if_fail(ifindex > 0, FALSE);
if (wol == NM_SETTING_WIRED_WAKE_ON_LAN_IGNORE)
if (wol == _NM_SETTING_WIRED_WAKE_ON_LAN_IGNORE)
return TRUE;
nm_log_dbg(LOGD_PLATFORM,
@@ -1425,17 +1425,17 @@ nmp_utils_ethtool_set_wake_on_lan(int ifindex,
(unsigned) wol,
wol_password);
if (NM_FLAGS_HAS(wol, NM_SETTING_WIRED_WAKE_ON_LAN_PHY))
if (NM_FLAGS_HAS(wol, _NM_SETTING_WIRED_WAKE_ON_LAN_PHY))
wol_info.wolopts |= WAKE_PHY;
if (NM_FLAGS_HAS(wol, NM_SETTING_WIRED_WAKE_ON_LAN_UNICAST))
if (NM_FLAGS_HAS(wol, _NM_SETTING_WIRED_WAKE_ON_LAN_UNICAST))
wol_info.wolopts |= WAKE_UCAST;
if (NM_FLAGS_HAS(wol, NM_SETTING_WIRED_WAKE_ON_LAN_MULTICAST))
if (NM_FLAGS_HAS(wol, _NM_SETTING_WIRED_WAKE_ON_LAN_MULTICAST))
wol_info.wolopts |= WAKE_MCAST;
if (NM_FLAGS_HAS(wol, NM_SETTING_WIRED_WAKE_ON_LAN_BROADCAST))
if (NM_FLAGS_HAS(wol, _NM_SETTING_WIRED_WAKE_ON_LAN_BROADCAST))
wol_info.wolopts |= WAKE_BCAST;
if (NM_FLAGS_HAS(wol, NM_SETTING_WIRED_WAKE_ON_LAN_ARP))
if (NM_FLAGS_HAS(wol, _NM_SETTING_WIRED_WAKE_ON_LAN_ARP))
wol_info.wolopts |= WAKE_ARP;
if (NM_FLAGS_HAS(wol, NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC))
if (NM_FLAGS_HAS(wol, _NM_SETTING_WIRED_WAKE_ON_LAN_MAGIC))
wol_info.wolopts |= WAKE_MAGIC;
if (wol_password) {

View File

@@ -17,9 +17,9 @@ gboolean nmp_utils_ethtool_supports_carrier_detect(int ifindex);
gboolean nmp_utils_ethtool_supports_vlans(int ifindex);
int nmp_utils_ethtool_get_peer_ifindex(int ifindex);
gboolean nmp_utils_ethtool_get_wake_on_lan(int ifindex);
gboolean nmp_utils_ethtool_set_wake_on_lan(int ifindex,
NMSettingWiredWakeOnLan wol,
const char * wol_password);
gboolean nmp_utils_ethtool_set_wake_on_lan(int ifindex,
_NMSettingWiredWakeOnLan wol,
const char * wol_password);
gboolean nmp_utils_ethtool_get_link_settings(int ifindex,
gboolean * out_autoneg,

View File

@@ -3282,10 +3282,10 @@ _to_string_ifa_flags(guint32 ifa_flags, char *buf, gsize size)
/*****************************************************************************/
gboolean
nm_platform_ethtool_set_wake_on_lan(NMPlatform * self,
int ifindex,
NMSettingWiredWakeOnLan wol,
const char * wol_password)
nm_platform_ethtool_set_wake_on_lan(NMPlatform * self,
int ifindex,
_NMSettingWiredWakeOnLan wol,
const char * wol_password)
{
_CHECK_SELF_NETNS(self, klass, netns, FALSE);

View File

@@ -10,6 +10,7 @@
#include "nm-core-types-internal.h"
#include "nm-platform/nmp-base.h"
#include "nm-base/nm-base.h"
#include "nm-core-utils.h"
#include "nm-setting-vlan.h"
@@ -2327,10 +2328,10 @@ const char *nm_platform_route_scope2str(int scope, char *buf, gsize len);
int nm_platform_ip_address_cmp_expiry(const NMPlatformIPAddress *a, const NMPlatformIPAddress *b);
gboolean nm_platform_ethtool_set_wake_on_lan(NMPlatform * self,
int ifindex,
NMSettingWiredWakeOnLan wol,
const char * wol_password);
gboolean nm_platform_ethtool_set_wake_on_lan(NMPlatform * self,
int ifindex,
_NMSettingWiredWakeOnLan wol,
const char * wol_password);
gboolean nm_platform_ethtool_set_link_settings(NMPlatform * self,
int ifindex,
gboolean autoneg,